]>
Commit | Line | Data |
---|---|---|
e440a328 | 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 | ||
ffe743ca | 9 | #include <algorithm> |
10 | ||
e440a328 | 11 | #include "go-c.h" |
12 | #include "gogo.h" | |
13 | #include "types.h" | |
14 | #include "export.h" | |
15 | #include "import.h" | |
16 | #include "statements.h" | |
17 | #include "lex.h" | |
a9182619 | 18 | #include "runtime.h" |
6e193e6f | 19 | #include "backend.h" |
e440a328 | 20 | #include "expressions.h" |
d751bb78 | 21 | #include "ast-dump.h" |
e440a328 | 22 | |
23 | // Class Expression. | |
24 | ||
25 | Expression::Expression(Expression_classification classification, | |
b13c66cd | 26 | Location location) |
e440a328 | 27 | : classification_(classification), location_(location) |
28 | { | |
29 | } | |
30 | ||
31 | Expression::~Expression() | |
32 | { | |
33 | } | |
34 | ||
e440a328 | 35 | // Traverse the expressions. |
36 | ||
37 | int | |
38 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
39 | { | |
40 | Expression* expr = *pexpr; | |
41 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
42 | { | |
43 | int t = traverse->expression(pexpr); | |
44 | if (t == TRAVERSE_EXIT) | |
45 | return TRAVERSE_EXIT; | |
46 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
47 | return TRAVERSE_CONTINUE; | |
48 | } | |
49 | return expr->do_traverse(traverse); | |
50 | } | |
51 | ||
52 | // Traverse subexpressions of this expression. | |
53 | ||
54 | int | |
55 | Expression::traverse_subexpressions(Traverse* traverse) | |
56 | { | |
57 | return this->do_traverse(traverse); | |
58 | } | |
59 | ||
60 | // Default implementation for do_traverse for child classes. | |
61 | ||
62 | int | |
63 | Expression::do_traverse(Traverse*) | |
64 | { | |
65 | return TRAVERSE_CONTINUE; | |
66 | } | |
67 | ||
68 | // This virtual function is called by the parser if the value of this | |
a7549a6a | 69 | // expression is being discarded. By default, we give an error. |
70 | // Expressions with side effects override. | |
e440a328 | 71 | |
4f2138d7 | 72 | bool |
e440a328 | 73 | Expression::do_discarding_value() |
74 | { | |
a7549a6a | 75 | this->unused_value_error(); |
4f2138d7 | 76 | return false; |
e440a328 | 77 | } |
78 | ||
79 | // This virtual function is called to export expressions. This will | |
80 | // only be used by expressions which may be constant. | |
81 | ||
82 | void | |
83 | Expression::do_export(Export*) const | |
84 | { | |
c3e6f413 | 85 | go_unreachable(); |
e440a328 | 86 | } |
87 | ||
a7549a6a | 88 | // Give an error saying that the value of the expression is not used. |
e440a328 | 89 | |
90 | void | |
a7549a6a | 91 | Expression::unused_value_error() |
e440a328 | 92 | { |
4f2138d7 | 93 | this->report_error(_("value computed is not used")); |
e440a328 | 94 | } |
95 | ||
96 | // Note that this expression is an error. This is called by children | |
97 | // when they discover an error. | |
98 | ||
99 | void | |
100 | Expression::set_is_error() | |
101 | { | |
102 | this->classification_ = EXPRESSION_ERROR; | |
103 | } | |
104 | ||
105 | // For children to call to report an error conveniently. | |
106 | ||
107 | void | |
108 | Expression::report_error(const char* msg) | |
109 | { | |
110 | error_at(this->location_, "%s", msg); | |
111 | this->set_is_error(); | |
112 | } | |
113 | ||
114 | // Set types of variables and constants. This is implemented by the | |
115 | // child class. | |
116 | ||
117 | void | |
118 | Expression::determine_type(const Type_context* context) | |
119 | { | |
120 | this->do_determine_type(context); | |
121 | } | |
122 | ||
123 | // Set types when there is no context. | |
124 | ||
125 | void | |
126 | Expression::determine_type_no_context() | |
127 | { | |
128 | Type_context context; | |
129 | this->do_determine_type(&context); | |
130 | } | |
131 | ||
2c809f8f | 132 | // Return an expression handling any conversions which must be done during |
e440a328 | 133 | // assignment. |
134 | ||
2c809f8f | 135 | Expression* |
b4a33049 | 136 | Expression::convert_for_assignment(Gogo*, Type* lhs_type, |
2c809f8f | 137 | Expression* rhs, Location location) |
e440a328 | 138 | { |
2c809f8f | 139 | Type* rhs_type = rhs->type(); |
140 | if (lhs_type->is_error() | |
141 | || rhs_type->is_error() | |
142 | || rhs->is_error_expression()) | |
143 | return Expression::make_error(location); | |
e440a328 | 144 | |
54211955 | 145 | if (lhs_type->forwarded() != rhs_type->forwarded() |
146 | && lhs_type->interface_type() != NULL) | |
e440a328 | 147 | { |
148 | if (rhs_type->interface_type() == NULL) | |
2c809f8f | 149 | return Expression::convert_type_to_interface(lhs_type, rhs, location); |
e440a328 | 150 | else |
2c809f8f | 151 | return Expression::convert_interface_to_interface(lhs_type, rhs, false, |
152 | location); | |
e440a328 | 153 | } |
54211955 | 154 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
155 | && rhs_type->interface_type() != NULL) | |
2c809f8f | 156 | return Expression::convert_interface_to_type(lhs_type, rhs, location); |
411eb89e | 157 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 158 | { |
2c809f8f | 159 | // Assigning nil to a slice. |
2c809f8f | 160 | Expression* nil = Expression::make_nil(location); |
e67508fa | 161 | Expression* zero = Expression::make_integer_ul(0, NULL, location); |
2c809f8f | 162 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); |
e440a328 | 163 | } |
164 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 165 | return Expression::make_nil(location); |
166 | else if (Type::are_identical(lhs_type, rhs_type, false, NULL)) | |
e440a328 | 167 | { |
168 | // No conversion is needed. | |
2c809f8f | 169 | return rhs; |
170 | } | |
171 | else if (lhs_type->points_to() != NULL) | |
172 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
173 | else if (lhs_type->is_numeric_type()) | |
174 | return Expression::make_cast(lhs_type, rhs, location); | |
175 | else if ((lhs_type->struct_type() != NULL | |
176 | && rhs_type->struct_type() != NULL) | |
177 | || (lhs_type->array_type() != NULL | |
178 | && rhs_type->array_type() != NULL)) | |
e440a328 | 179 | { |
180 | // This conversion must be permitted by Go, or we wouldn't have | |
181 | // gotten here. | |
2c809f8f | 182 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 183 | } |
184 | else | |
2c809f8f | 185 | return rhs; |
e440a328 | 186 | } |
187 | ||
2c809f8f | 188 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 189 | // interface type. |
190 | ||
2c809f8f | 191 | Expression* |
192 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
193 | Location location) | |
e440a328 | 194 | { |
e440a328 | 195 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
196 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
197 | ||
198 | // Since RHS_TYPE is a static type, we can create the interface | |
199 | // method table at compile time. | |
200 | ||
201 | // When setting an interface to nil, we just set both fields to | |
202 | // NULL. | |
2c809f8f | 203 | Type* rhs_type = rhs->type(); |
e440a328 | 204 | if (rhs_type->is_nil_type()) |
63697958 | 205 | { |
2c809f8f | 206 | Expression* nil = Expression::make_nil(location); |
207 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 208 | } |
e440a328 | 209 | |
210 | // This should have been checked already. | |
c484d925 | 211 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 212 | |
e440a328 | 213 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
214 | // then the first field is the type descriptor for RHS_TYPE. | |
215 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 216 | Expression* first_field; |
e440a328 | 217 | if (lhs_is_empty) |
2c809f8f | 218 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 219 | else |
220 | { | |
221 | // Build the interface method table for this interface and this | |
222 | // object type: a list of function pointers for each interface | |
223 | // method. | |
224 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 225 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 226 | bool is_pointer = false; |
c0cab2ec | 227 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 228 | { |
229 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 230 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 231 | is_pointer = true; |
232 | } | |
c0cab2ec | 233 | if (rhs_named_type != NULL) |
2c809f8f | 234 | first_field = |
235 | rhs_named_type->interface_method_table(lhs_interface_type, | |
236 | is_pointer); | |
c0cab2ec | 237 | else if (rhs_struct_type != NULL) |
2c809f8f | 238 | first_field = |
239 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
240 | is_pointer); | |
c0cab2ec | 241 | else |
2c809f8f | 242 | first_field = Expression::make_nil(location); |
e440a328 | 243 | } |
e440a328 | 244 | |
2c809f8f | 245 | Expression* obj; |
e440a328 | 246 | if (rhs_type->points_to() != NULL) |
247 | { | |
2c809f8f | 248 | // We are assigning a pointer to the interface; the interface |
e440a328 | 249 | // holds the pointer itself. |
2c809f8f | 250 | obj = rhs; |
251 | } | |
252 | else | |
253 | { | |
254 | // We are assigning a non-pointer value to the interface; the | |
255 | // interface gets a copy of the value in the heap. | |
256 | obj = Expression::make_heap_expression(rhs, location); | |
e440a328 | 257 | } |
258 | ||
2c809f8f | 259 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
260 | } | |
e440a328 | 261 | |
2c809f8f | 262 | // Return an expression for the type descriptor of RHS. |
e440a328 | 263 | |
2c809f8f | 264 | Expression* |
265 | Expression::get_interface_type_descriptor(Expression* rhs) | |
266 | { | |
267 | go_assert(rhs->type()->interface_type() != NULL); | |
268 | Location location = rhs->location(); | |
e440a328 | 269 | |
2c809f8f | 270 | // The type descriptor is the first field of an empty interface. |
271 | if (rhs->type()->interface_type()->is_empty()) | |
272 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
273 | location); | |
274 | ||
275 | Expression* mtable = | |
276 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 277 | |
2c809f8f | 278 | Expression* descriptor = |
279 | Expression::make_unary(OPERATOR_MULT, mtable, location); | |
280 | descriptor = Expression::make_field_reference(descriptor, 0, location); | |
281 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 282 | |
2c809f8f | 283 | Expression* eq = |
284 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
285 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 286 | } |
287 | ||
2c809f8f | 288 | // Return an expression for the conversion of an interface type to an |
e440a328 | 289 | // interface type. |
290 | ||
2c809f8f | 291 | Expression* |
292 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
293 | bool for_type_guard, | |
294 | Location location) | |
e440a328 | 295 | { |
8ba8cc87 | 296 | if (Type::are_identical(lhs_type, rhs->type(), false, NULL)) |
297 | return rhs; | |
298 | ||
e440a328 | 299 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
300 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
301 | ||
e440a328 | 302 | // In the general case this requires runtime examination of the type |
303 | // method table to match it up with the interface methods. | |
304 | ||
305 | // FIXME: If all of the methods in the right hand side interface | |
306 | // also appear in the left hand side interface, then we don't need | |
307 | // to do a runtime check, although we still need to build a new | |
308 | // method table. | |
309 | ||
8ba8cc87 | 310 | // We are going to evaluate RHS multiple times. |
311 | go_assert(rhs->is_variable()); | |
312 | ||
e440a328 | 313 | // Get the type descriptor for the right hand side. This will be |
314 | // NULL for a nil interface. | |
2c809f8f | 315 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
316 | Expression* lhs_type_expr = | |
317 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 318 | |
2c809f8f | 319 | Expression* first_field; |
e440a328 | 320 | if (for_type_guard) |
321 | { | |
322 | // A type assertion fails when converting a nil interface. | |
2c809f8f | 323 | first_field = |
324 | Runtime::make_call(Runtime::ASSERT_INTERFACE, location, 2, | |
325 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 326 | } |
327 | else if (lhs_is_empty) | |
328 | { | |
2c809f8f | 329 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 330 | // first field is just the type descriptor of the object. |
2c809f8f | 331 | first_field = rhs_type_expr; |
e440a328 | 332 | } |
333 | else | |
334 | { | |
335 | // A conversion to a non-empty interface may fail, but unlike a | |
336 | // type assertion converting nil will always succeed. | |
2c809f8f | 337 | first_field = |
338 | Runtime::make_call(Runtime::CONVERT_INTERFACE, location, 2, | |
339 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 340 | } |
341 | ||
342 | // The second field is simply the object pointer. | |
2c809f8f | 343 | Expression* obj = |
344 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
345 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 346 | } |
347 | ||
2c809f8f | 348 | // Return an expression for the conversion of an interface type to a |
e440a328 | 349 | // non-interface type. |
350 | ||
2c809f8f | 351 | Expression* |
352 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
353 | Location location) | |
e440a328 | 354 | { |
8ba8cc87 | 355 | // We are going to evaluate RHS multiple times. |
356 | go_assert(rhs->is_variable()); | |
357 | ||
e440a328 | 358 | // Call a function to check that the type is valid. The function |
359 | // will panic with an appropriate runtime type error if the type is | |
360 | // not valid. | |
2c809f8f | 361 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
362 | location); | |
363 | Expression* rhs_descriptor = | |
364 | Expression::get_interface_type_descriptor(rhs); | |
365 | ||
366 | Type* rhs_type = rhs->type(); | |
367 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
368 | location); | |
369 | ||
370 | Expression* check_iface = Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE, | |
371 | location, 3, lhs_type_expr, | |
372 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 373 | |
374 | // If the call succeeds, pull out the value. | |
2c809f8f | 375 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
376 | location); | |
e440a328 | 377 | |
378 | // If the value is a pointer, then it is the value we want. | |
379 | // Otherwise it points to the value. | |
380 | if (lhs_type->points_to() == NULL) | |
381 | { | |
2c809f8f | 382 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
383 | location); | |
384 | obj = Expression::make_unary(OPERATOR_MULT, obj, location); | |
e440a328 | 385 | } |
2c809f8f | 386 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 387 | } |
388 | ||
ea664253 | 389 | // Convert an expression to its backend representation. This is implemented by |
390 | // the child class. Not that it is not in general safe to call this multiple | |
e440a328 | 391 | // times for a single expression, but that we don't catch such errors. |
392 | ||
ea664253 | 393 | Bexpression* |
394 | Expression::get_backend(Translate_context* context) | |
e440a328 | 395 | { |
396 | // The child may have marked this expression as having an error. | |
397 | if (this->classification_ == EXPRESSION_ERROR) | |
ea664253 | 398 | return context->backend()->error_expression(); |
e440a328 | 399 | |
ea664253 | 400 | return this->do_get_backend(context); |
e440a328 | 401 | } |
402 | ||
48c2a53a | 403 | // Return a backend expression for VAL. |
404 | Bexpression* | |
405 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
406 | Numeric_constant* val) | |
e440a328 | 407 | { |
48c2a53a | 408 | Gogo* gogo = context->gogo(); |
409 | Type* type = val->type(); | |
410 | if (type == NULL) | |
411 | return gogo->backend()->error_expression(); | |
e440a328 | 412 | |
48c2a53a | 413 | Btype* btype = type->get_backend(gogo); |
414 | Bexpression* ret; | |
415 | if (type->integer_type() != NULL) | |
e440a328 | 416 | { |
417 | mpz_t ival; | |
48c2a53a | 418 | if (!val->to_int(&ival)) |
419 | { | |
420 | go_assert(saw_errors()); | |
421 | return gogo->backend()->error_expression(); | |
422 | } | |
423 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 424 | mpz_clear(ival); |
e440a328 | 425 | } |
48c2a53a | 426 | else if (type->float_type() != NULL) |
e440a328 | 427 | { |
48c2a53a | 428 | mpfr_t fval; |
429 | if (!val->to_float(&fval)) | |
430 | { | |
431 | go_assert(saw_errors()); | |
432 | return gogo->backend()->error_expression(); | |
433 | } | |
434 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
435 | mpfr_clear(fval); | |
e440a328 | 436 | } |
48c2a53a | 437 | else if (type->complex_type() != NULL) |
e440a328 | 438 | { |
fcbea5e4 | 439 | mpc_t cval; |
440 | if (!val->to_complex(&cval)) | |
48c2a53a | 441 | { |
442 | go_assert(saw_errors()); | |
443 | return gogo->backend()->error_expression(); | |
444 | } | |
fcbea5e4 | 445 | ret = gogo->backend()->complex_constant_expression(btype, cval); |
446 | mpc_clear(cval); | |
e440a328 | 447 | } |
448 | else | |
c3e6f413 | 449 | go_unreachable(); |
e440a328 | 450 | |
48c2a53a | 451 | return ret; |
e440a328 | 452 | } |
453 | ||
2c809f8f | 454 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
455 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 456 | |
2c809f8f | 457 | Expression* |
458 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 459 | { |
2c809f8f | 460 | Type* val_type = val->type(); |
461 | Type* bound_type = Type::lookup_integer_type("int"); | |
462 | ||
463 | int val_type_size; | |
464 | bool val_is_unsigned = false; | |
465 | if (val_type->integer_type() != NULL) | |
466 | { | |
467 | val_type_size = val_type->integer_type()->bits(); | |
468 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
469 | } | |
470 | else | |
471 | { | |
472 | if (!val_type->is_numeric_type() | |
473 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
474 | { | |
475 | go_assert(saw_errors()); | |
476 | return Expression::make_boolean(true, loc); | |
477 | } | |
e440a328 | 478 | |
2c809f8f | 479 | if (val_type->complex_type() != NULL) |
480 | val_type_size = val_type->complex_type()->bits(); | |
481 | else | |
482 | val_type_size = val_type->float_type()->bits(); | |
483 | } | |
484 | ||
485 | Expression* negative_index = Expression::make_boolean(false, loc); | |
486 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
487 | if (!val_is_unsigned) | |
e440a328 | 488 | { |
e67508fa | 489 | Expression* zero = Expression::make_integer_ul(0, val_type, loc); |
2c809f8f | 490 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); |
e440a328 | 491 | } |
492 | ||
2c809f8f | 493 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 494 | if (val_type_size > bound_type_size |
495 | || (val_type_size == bound_type_size | |
2c809f8f | 496 | && val_is_unsigned)) |
497 | { | |
498 | mpz_t one; | |
499 | mpz_init_set_ui(one, 1UL); | |
500 | ||
501 | // maxval = 2^(bound_type_size - 1) - 1 | |
502 | mpz_t maxval; | |
503 | mpz_init(maxval); | |
504 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
505 | mpz_sub_ui(maxval, maxval, 1); | |
e67508fa | 506 | Expression* max = Expression::make_integer_z(&maxval, val_type, loc); |
2c809f8f | 507 | mpz_clear(one); |
508 | mpz_clear(maxval); | |
509 | ||
510 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 511 | } |
512 | ||
2c809f8f | 513 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
514 | loc); | |
e440a328 | 515 | } |
516 | ||
d751bb78 | 517 | void |
518 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
519 | { | |
520 | this->do_dump_expression(ast_dump_context); | |
521 | } | |
522 | ||
e440a328 | 523 | // Error expressions. This are used to avoid cascading errors. |
524 | ||
525 | class Error_expression : public Expression | |
526 | { | |
527 | public: | |
b13c66cd | 528 | Error_expression(Location location) |
e440a328 | 529 | : Expression(EXPRESSION_ERROR, location) |
530 | { } | |
531 | ||
532 | protected: | |
533 | bool | |
534 | do_is_constant() const | |
535 | { return true; } | |
536 | ||
0e168074 | 537 | bool |
538 | do_is_immutable() const | |
539 | { return true; } | |
540 | ||
e440a328 | 541 | bool |
0c77715b | 542 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 543 | { |
0c77715b | 544 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 545 | return true; |
546 | } | |
547 | ||
4f2138d7 | 548 | bool |
e440a328 | 549 | do_discarding_value() |
4f2138d7 | 550 | { return true; } |
e440a328 | 551 | |
552 | Type* | |
553 | do_type() | |
554 | { return Type::make_error_type(); } | |
555 | ||
556 | void | |
557 | do_determine_type(const Type_context*) | |
558 | { } | |
559 | ||
560 | Expression* | |
561 | do_copy() | |
562 | { return this; } | |
563 | ||
564 | bool | |
565 | do_is_addressable() const | |
566 | { return true; } | |
567 | ||
ea664253 | 568 | Bexpression* |
569 | do_get_backend(Translate_context* context) | |
570 | { return context->backend()->error_expression(); } | |
d751bb78 | 571 | |
572 | void | |
573 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 574 | }; |
575 | ||
d751bb78 | 576 | // Dump the ast representation for an error expression to a dump context. |
577 | ||
578 | void | |
579 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
580 | { | |
581 | ast_dump_context->ostream() << "_Error_" ; | |
582 | } | |
583 | ||
e440a328 | 584 | Expression* |
b13c66cd | 585 | Expression::make_error(Location location) |
e440a328 | 586 | { |
587 | return new Error_expression(location); | |
588 | } | |
589 | ||
590 | // An expression which is really a type. This is used during parsing. | |
591 | // It is an error if these survive after lowering. | |
592 | ||
593 | class | |
594 | Type_expression : public Expression | |
595 | { | |
596 | public: | |
b13c66cd | 597 | Type_expression(Type* type, Location location) |
e440a328 | 598 | : Expression(EXPRESSION_TYPE, location), |
599 | type_(type) | |
600 | { } | |
601 | ||
602 | protected: | |
603 | int | |
604 | do_traverse(Traverse* traverse) | |
605 | { return Type::traverse(this->type_, traverse); } | |
606 | ||
607 | Type* | |
608 | do_type() | |
609 | { return this->type_; } | |
610 | ||
611 | void | |
612 | do_determine_type(const Type_context*) | |
613 | { } | |
614 | ||
615 | void | |
616 | do_check_types(Gogo*) | |
617 | { this->report_error(_("invalid use of type")); } | |
618 | ||
619 | Expression* | |
620 | do_copy() | |
621 | { return this; } | |
622 | ||
ea664253 | 623 | Bexpression* |
624 | do_get_backend(Translate_context*) | |
c3e6f413 | 625 | { go_unreachable(); } |
e440a328 | 626 | |
d751bb78 | 627 | void do_dump_expression(Ast_dump_context*) const; |
628 | ||
e440a328 | 629 | private: |
630 | // The type which we are representing as an expression. | |
631 | Type* type_; | |
632 | }; | |
633 | ||
d751bb78 | 634 | void |
635 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
636 | { | |
637 | ast_dump_context->dump_type(this->type_); | |
638 | } | |
639 | ||
e440a328 | 640 | Expression* |
b13c66cd | 641 | Expression::make_type(Type* type, Location location) |
e440a328 | 642 | { |
643 | return new Type_expression(type, location); | |
644 | } | |
645 | ||
e03bdf36 | 646 | // Class Parser_expression. |
647 | ||
648 | Type* | |
649 | Parser_expression::do_type() | |
650 | { | |
651 | // We should never really ask for the type of a Parser_expression. | |
652 | // However, it can happen, at least when we have an invalid const | |
653 | // whose initializer refers to the const itself. In that case we | |
654 | // may ask for the type when lowering the const itself. | |
c484d925 | 655 | go_assert(saw_errors()); |
e03bdf36 | 656 | return Type::make_error_type(); |
657 | } | |
658 | ||
e440a328 | 659 | // Class Var_expression. |
660 | ||
661 | // Lower a variable expression. Here we just make sure that the | |
662 | // initialization expression of the variable has been lowered. This | |
663 | // ensures that we will be able to determine the type of the variable | |
664 | // if necessary. | |
665 | ||
666 | Expression* | |
ceeb4318 | 667 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
668 | Statement_inserter* inserter, int) | |
e440a328 | 669 | { |
670 | if (this->variable_->is_variable()) | |
671 | { | |
672 | Variable* var = this->variable_->var_value(); | |
673 | // This is either a local variable or a global variable. A | |
674 | // reference to a variable which is local to an enclosing | |
675 | // function will be a reference to a field in a closure. | |
676 | if (var->is_global()) | |
ceeb4318 | 677 | { |
678 | function = NULL; | |
679 | inserter = NULL; | |
680 | } | |
681 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 682 | } |
683 | return this; | |
684 | } | |
685 | ||
e440a328 | 686 | // Return the type of a reference to a variable. |
687 | ||
688 | Type* | |
689 | Var_expression::do_type() | |
690 | { | |
691 | if (this->variable_->is_variable()) | |
692 | return this->variable_->var_value()->type(); | |
693 | else if (this->variable_->is_result_variable()) | |
694 | return this->variable_->result_var_value()->type(); | |
695 | else | |
c3e6f413 | 696 | go_unreachable(); |
e440a328 | 697 | } |
698 | ||
0ab09e06 | 699 | // Determine the type of a reference to a variable. |
700 | ||
701 | void | |
702 | Var_expression::do_determine_type(const Type_context*) | |
703 | { | |
704 | if (this->variable_->is_variable()) | |
705 | this->variable_->var_value()->determine_type(); | |
706 | } | |
707 | ||
e440a328 | 708 | // Something takes the address of this variable. This means that we |
709 | // may want to move the variable onto the heap. | |
710 | ||
711 | void | |
712 | Var_expression::do_address_taken(bool escapes) | |
713 | { | |
714 | if (!escapes) | |
f325319b | 715 | { |
716 | if (this->variable_->is_variable()) | |
717 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
718 | else if (this->variable_->is_result_variable()) | |
719 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
720 | else | |
721 | go_unreachable(); | |
722 | } | |
e440a328 | 723 | else |
f325319b | 724 | { |
725 | if (this->variable_->is_variable()) | |
726 | this->variable_->var_value()->set_address_taken(); | |
727 | else if (this->variable_->is_result_variable()) | |
728 | this->variable_->result_var_value()->set_address_taken(); | |
729 | else | |
730 | go_unreachable(); | |
731 | } | |
e440a328 | 732 | } |
733 | ||
ea664253 | 734 | // Get the backend representation for a reference to a variable. |
e440a328 | 735 | |
ea664253 | 736 | Bexpression* |
737 | Var_expression::do_get_backend(Translate_context* context) | |
e440a328 | 738 | { |
fe2f84cf | 739 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
740 | context->function()); | |
fe2f84cf | 741 | bool is_in_heap; |
c6777780 | 742 | Location loc = this->location(); |
9b27b43c | 743 | Btype* btype; |
744 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 745 | if (this->variable_->is_variable()) |
9b27b43c | 746 | { |
747 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
748 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
749 | } | |
fe2f84cf | 750 | else if (this->variable_->is_result_variable()) |
9b27b43c | 751 | { |
752 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
753 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
754 | } | |
fe2f84cf | 755 | else |
c3e6f413 | 756 | go_unreachable(); |
c6777780 | 757 | |
758 | Bexpression* ret = context->backend()->var_expression(bvar, loc); | |
fe2f84cf | 759 | if (is_in_heap) |
9b27b43c | 760 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
ea664253 | 761 | return ret; |
e440a328 | 762 | } |
763 | ||
d751bb78 | 764 | // Ast dump for variable expression. |
765 | ||
766 | void | |
767 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
768 | { | |
769 | ast_dump_context->ostream() << this->variable_->name() ; | |
770 | } | |
771 | ||
e440a328 | 772 | // Make a reference to a variable in an expression. |
773 | ||
774 | Expression* | |
b13c66cd | 775 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 776 | { |
777 | if (var->is_sink()) | |
778 | return Expression::make_sink(location); | |
779 | ||
780 | // FIXME: Creating a new object for each reference to a variable is | |
781 | // wasteful. | |
782 | return new Var_expression(var, location); | |
783 | } | |
784 | ||
785 | // Class Temporary_reference_expression. | |
786 | ||
787 | // The type. | |
788 | ||
789 | Type* | |
790 | Temporary_reference_expression::do_type() | |
791 | { | |
792 | return this->statement_->type(); | |
793 | } | |
794 | ||
795 | // Called if something takes the address of this temporary variable. | |
796 | // We never have to move temporary variables to the heap, but we do | |
797 | // need to know that they must live in the stack rather than in a | |
798 | // register. | |
799 | ||
800 | void | |
801 | Temporary_reference_expression::do_address_taken(bool) | |
802 | { | |
803 | this->statement_->set_is_address_taken(); | |
804 | } | |
805 | ||
ea664253 | 806 | // Get a backend expression referring to the variable. |
e440a328 | 807 | |
ea664253 | 808 | Bexpression* |
809 | Temporary_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 810 | { |
cd440cff | 811 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 812 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
cd440cff | 813 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 814 | |
cd440cff | 815 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 816 | // that the Go frontend can. In some cases this means that a |
817 | // temporary variable won't have the right backend type. Correct | |
818 | // that here by adding a type cast. We need to use base() to push | |
819 | // the circularity down one level. | |
cd440cff | 820 | Type* stype = this->statement_->type(); |
ceeb4318 | 821 | if (!this->is_lvalue_ |
cd440cff | 822 | && stype->has_pointer() |
823 | && stype->deref()->is_void_type()) | |
eefc1ed3 | 824 | { |
cd440cff | 825 | Btype* btype = this->type()->base()->get_backend(gogo); |
826 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 827 | } |
ea664253 | 828 | return ret; |
e440a328 | 829 | } |
830 | ||
d751bb78 | 831 | // Ast dump for temporary reference. |
832 | ||
833 | void | |
834 | Temporary_reference_expression::do_dump_expression( | |
835 | Ast_dump_context* ast_dump_context) const | |
836 | { | |
837 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
838 | } | |
839 | ||
e440a328 | 840 | // Make a reference to a temporary variable. |
841 | ||
ceeb4318 | 842 | Temporary_reference_expression* |
e440a328 | 843 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 844 | Location location) |
e440a328 | 845 | { |
846 | return new Temporary_reference_expression(statement, location); | |
847 | } | |
848 | ||
e9d3367e | 849 | // Class Set_and_use_temporary_expression. |
850 | ||
851 | // Return the type. | |
852 | ||
853 | Type* | |
854 | Set_and_use_temporary_expression::do_type() | |
855 | { | |
856 | return this->statement_->type(); | |
857 | } | |
858 | ||
0afbb937 | 859 | // Determine the type of the expression. |
860 | ||
861 | void | |
862 | Set_and_use_temporary_expression::do_determine_type( | |
863 | const Type_context* context) | |
864 | { | |
865 | this->expr_->determine_type(context); | |
866 | } | |
867 | ||
e9d3367e | 868 | // Take the address. |
869 | ||
870 | void | |
871 | Set_and_use_temporary_expression::do_address_taken(bool) | |
872 | { | |
873 | this->statement_->set_is_address_taken(); | |
874 | } | |
875 | ||
876 | // Return the backend representation. | |
877 | ||
ea664253 | 878 | Bexpression* |
879 | Set_and_use_temporary_expression::do_get_backend(Translate_context* context) | |
e9d3367e | 880 | { |
e9d3367e | 881 | Location loc = this->location(); |
a302c105 | 882 | Gogo* gogo = context->gogo(); |
883 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
884 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); | |
885 | ||
ea664253 | 886 | Bexpression* bexpr = this->expr_->get_backend(context); |
a302c105 | 887 | Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc); |
888 | var_ref = gogo->backend()->var_expression(bvar, loc); | |
889 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); | |
ea664253 | 890 | return ret; |
e9d3367e | 891 | } |
892 | ||
893 | // Dump. | |
894 | ||
895 | void | |
896 | Set_and_use_temporary_expression::do_dump_expression( | |
897 | Ast_dump_context* ast_dump_context) const | |
898 | { | |
899 | ast_dump_context->ostream() << '('; | |
900 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
901 | ast_dump_context->ostream() << " = "; | |
902 | this->expr_->dump_expression(ast_dump_context); | |
903 | ast_dump_context->ostream() << ')'; | |
904 | } | |
905 | ||
906 | // Make a set-and-use temporary. | |
907 | ||
908 | Set_and_use_temporary_expression* | |
909 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
910 | Expression* expr, Location location) | |
911 | { | |
912 | return new Set_and_use_temporary_expression(statement, expr, location); | |
913 | } | |
914 | ||
e440a328 | 915 | // A sink expression--a use of the blank identifier _. |
916 | ||
917 | class Sink_expression : public Expression | |
918 | { | |
919 | public: | |
b13c66cd | 920 | Sink_expression(Location location) |
e440a328 | 921 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 922 | type_(NULL), bvar_(NULL) |
e440a328 | 923 | { } |
924 | ||
925 | protected: | |
4f2138d7 | 926 | bool |
e440a328 | 927 | do_discarding_value() |
4f2138d7 | 928 | { return true; } |
e440a328 | 929 | |
930 | Type* | |
931 | do_type(); | |
932 | ||
933 | void | |
934 | do_determine_type(const Type_context*); | |
935 | ||
936 | Expression* | |
937 | do_copy() | |
938 | { return new Sink_expression(this->location()); } | |
939 | ||
ea664253 | 940 | Bexpression* |
941 | do_get_backend(Translate_context*); | |
e440a328 | 942 | |
d751bb78 | 943 | void |
944 | do_dump_expression(Ast_dump_context*) const; | |
945 | ||
e440a328 | 946 | private: |
947 | // The type of this sink variable. | |
948 | Type* type_; | |
949 | // The temporary variable we generate. | |
aa93217a | 950 | Bvariable* bvar_; |
e440a328 | 951 | }; |
952 | ||
953 | // Return the type of a sink expression. | |
954 | ||
955 | Type* | |
956 | Sink_expression::do_type() | |
957 | { | |
958 | if (this->type_ == NULL) | |
959 | return Type::make_sink_type(); | |
960 | return this->type_; | |
961 | } | |
962 | ||
963 | // Determine the type of a sink expression. | |
964 | ||
965 | void | |
966 | Sink_expression::do_determine_type(const Type_context* context) | |
967 | { | |
968 | if (context->type != NULL) | |
969 | this->type_ = context->type; | |
970 | } | |
971 | ||
972 | // Return a temporary variable for a sink expression. This will | |
973 | // presumably be a write-only variable which the middle-end will drop. | |
974 | ||
ea664253 | 975 | Bexpression* |
976 | Sink_expression::do_get_backend(Translate_context* context) | |
e440a328 | 977 | { |
aa93217a | 978 | Location loc = this->location(); |
979 | Gogo* gogo = context->gogo(); | |
980 | if (this->bvar_ == NULL) | |
e440a328 | 981 | { |
c484d925 | 982 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 983 | Named_object* fn = context->function(); |
984 | go_assert(fn != NULL); | |
985 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 986 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 987 | Bstatement* decl; |
988 | this->bvar_ = | |
989 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
990 | false, loc, &decl); | |
991 | Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc); | |
992 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); | |
ea664253 | 993 | return var_ref; |
e440a328 | 994 | } |
ea664253 | 995 | return gogo->backend()->var_expression(this->bvar_, loc); |
e440a328 | 996 | } |
997 | ||
d751bb78 | 998 | // Ast dump for sink expression. |
999 | ||
1000 | void | |
1001 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1002 | { | |
1003 | ast_dump_context->ostream() << "_" ; | |
1004 | } | |
1005 | ||
e440a328 | 1006 | // Make a sink expression. |
1007 | ||
1008 | Expression* | |
b13c66cd | 1009 | Expression::make_sink(Location location) |
e440a328 | 1010 | { |
1011 | return new Sink_expression(location); | |
1012 | } | |
1013 | ||
1014 | // Class Func_expression. | |
1015 | ||
1016 | // FIXME: Can a function expression appear in a constant expression? | |
1017 | // The value is unchanging. Initializing a constant to the address of | |
1018 | // a function seems like it could work, though there might be little | |
1019 | // point to it. | |
1020 | ||
e440a328 | 1021 | // Traversal. |
1022 | ||
1023 | int | |
1024 | Func_expression::do_traverse(Traverse* traverse) | |
1025 | { | |
1026 | return (this->closure_ == NULL | |
1027 | ? TRAVERSE_CONTINUE | |
1028 | : Expression::traverse(&this->closure_, traverse)); | |
1029 | } | |
1030 | ||
1031 | // Return the type of a function expression. | |
1032 | ||
1033 | Type* | |
1034 | Func_expression::do_type() | |
1035 | { | |
1036 | if (this->function_->is_function()) | |
1037 | return this->function_->func_value()->type(); | |
1038 | else if (this->function_->is_function_declaration()) | |
1039 | return this->function_->func_declaration_value()->type(); | |
1040 | else | |
c3e6f413 | 1041 | go_unreachable(); |
e440a328 | 1042 | } |
1043 | ||
ea664253 | 1044 | // Get the backend representation for the code of a function expression. |
e440a328 | 1045 | |
97267c39 | 1046 | Bexpression* |
8381eda7 | 1047 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1048 | { |
1049 | Function_type* fntype; | |
8381eda7 | 1050 | if (no->is_function()) |
1051 | fntype = no->func_value()->type(); | |
1052 | else if (no->is_function_declaration()) | |
1053 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1054 | else |
c3e6f413 | 1055 | go_unreachable(); |
e440a328 | 1056 | |
1057 | // Builtin functions are handled specially by Call_expression. We | |
1058 | // can't take their address. | |
1059 | if (fntype->is_builtin()) | |
1060 | { | |
8381eda7 | 1061 | error_at(loc, |
cb0e02f3 | 1062 | "invalid use of special builtin function %qs; must be called", |
8381eda7 | 1063 | no->message_name().c_str()); |
97267c39 | 1064 | return gogo->backend()->error_expression(); |
e440a328 | 1065 | } |
1066 | ||
97267c39 | 1067 | Bfunction* fndecl; |
e440a328 | 1068 | if (no->is_function()) |
cf3cae55 | 1069 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1070 | else if (no->is_function_declaration()) |
cf3cae55 | 1071 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1072 | else |
c3e6f413 | 1073 | go_unreachable(); |
e440a328 | 1074 | |
97267c39 | 1075 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1076 | } |
1077 | ||
ea664253 | 1078 | // Get the backend representation for a function expression. This is used when |
1079 | // we take the address of a function rather than simply calling it. A func | |
8381eda7 | 1080 | // value is represented as a pointer to a block of memory. The first |
1081 | // word of that memory is a pointer to the function code. The | |
1082 | // remaining parts of that memory are the addresses of variables that | |
1083 | // the function closes over. | |
e440a328 | 1084 | |
ea664253 | 1085 | Bexpression* |
1086 | Func_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1087 | { |
8381eda7 | 1088 | // If there is no closure, just use the function descriptor. |
2010c17a | 1089 | if (this->closure_ == NULL) |
8381eda7 | 1090 | { |
1091 | Gogo* gogo = context->gogo(); | |
1092 | Named_object* no = this->function_; | |
1093 | Expression* descriptor; | |
1094 | if (no->is_function()) | |
1095 | descriptor = no->func_value()->descriptor(gogo, no); | |
1096 | else if (no->is_function_declaration()) | |
1097 | { | |
1098 | if (no->func_declaration_value()->type()->is_builtin()) | |
1099 | { | |
1100 | error_at(this->location(), | |
1101 | ("invalid use of special builtin function %qs; " | |
1102 | "must be called"), | |
1103 | no->message_name().c_str()); | |
ea664253 | 1104 | return gogo->backend()->error_expression(); |
8381eda7 | 1105 | } |
1106 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1107 | } | |
1108 | else | |
1109 | go_unreachable(); | |
2010c17a | 1110 | |
ea664253 | 1111 | Bexpression* bdesc = descriptor->get_backend(context); |
1112 | return gogo->backend()->address_expression(bdesc, this->location()); | |
8381eda7 | 1113 | } |
e440a328 | 1114 | |
8381eda7 | 1115 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1116 | |
8381eda7 | 1117 | // If there is a closure, then the closure is itself the function |
1118 | // expression. It is a pointer to a struct whose first field points | |
1119 | // to the function code and whose remaining fields are the addresses | |
1120 | // of the closed-over variables. | |
ea664253 | 1121 | return this->closure_->get_backend(context); |
e440a328 | 1122 | } |
1123 | ||
d751bb78 | 1124 | // Ast dump for function. |
1125 | ||
1126 | void | |
1127 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1128 | { | |
8b1c301d | 1129 | ast_dump_context->ostream() << this->function_->name(); |
1130 | if (this->closure_ != NULL) | |
1131 | { | |
1132 | ast_dump_context->ostream() << " {closure = "; | |
1133 | this->closure_->dump_expression(ast_dump_context); | |
1134 | ast_dump_context->ostream() << "}"; | |
1135 | } | |
d751bb78 | 1136 | } |
1137 | ||
e440a328 | 1138 | // Make a reference to a function in an expression. |
1139 | ||
1140 | Expression* | |
1141 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1142 | Location location) |
e440a328 | 1143 | { |
1144 | return new Func_expression(function, closure, location); | |
1145 | } | |
1146 | ||
c6837989 | 1147 | // Class Func_descriptor_expression. |
8381eda7 | 1148 | |
c6837989 | 1149 | // Constructor. |
8381eda7 | 1150 | |
c6837989 | 1151 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1152 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1153 | fn_(fn), dvar_(NULL) |
c6837989 | 1154 | { |
1155 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1156 | } | |
8381eda7 | 1157 | |
c6837989 | 1158 | // Traversal. |
8381eda7 | 1159 | |
c6837989 | 1160 | int |
1161 | Func_descriptor_expression::do_traverse(Traverse*) | |
1162 | { | |
1163 | return TRAVERSE_CONTINUE; | |
1164 | } | |
8381eda7 | 1165 | |
1166 | // All function descriptors have the same type. | |
1167 | ||
1168 | Type* Func_descriptor_expression::descriptor_type; | |
1169 | ||
1170 | void | |
1171 | Func_descriptor_expression::make_func_descriptor_type() | |
1172 | { | |
1173 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1174 | return; | |
1175 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1176 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1177 | Func_descriptor_expression::descriptor_type = | |
1178 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1179 | } | |
1180 | ||
1181 | Type* | |
1182 | Func_descriptor_expression::do_type() | |
1183 | { | |
1184 | Func_descriptor_expression::make_func_descriptor_type(); | |
1185 | return Func_descriptor_expression::descriptor_type; | |
1186 | } | |
1187 | ||
ea664253 | 1188 | // The backend representation for a function descriptor. |
8381eda7 | 1189 | |
ea664253 | 1190 | Bexpression* |
1191 | Func_descriptor_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1192 | { |
8381eda7 | 1193 | Named_object* no = this->fn_; |
1194 | Location loc = no->location(); | |
ea664253 | 1195 | if (this->dvar_ != NULL) |
1196 | return context->backend()->var_expression(this->dvar_, loc); | |
8381eda7 | 1197 | |
ea664253 | 1198 | Gogo* gogo = context->gogo(); |
8381eda7 | 1199 | std::string var_name; |
09e57698 | 1200 | bool is_descriptor = false; |
1201 | if (no->is_function_declaration() | |
1202 | && !no->func_declaration_value()->asm_name().empty() | |
1203 | && Linemap::is_predeclared_location(no->location())) | |
1204 | { | |
1205 | var_name = no->func_declaration_value()->asm_name() + "_descriptor"; | |
1206 | is_descriptor = true; | |
1207 | } | |
8381eda7 | 1208 | else |
09e57698 | 1209 | { |
1210 | if (no->package() == NULL) | |
1211 | var_name = gogo->pkgpath_symbol(); | |
1212 | else | |
1213 | var_name = no->package()->pkgpath_symbol(); | |
1214 | var_name.push_back('.'); | |
1215 | var_name.append(Gogo::unpack_hidden_name(no->name())); | |
1216 | var_name.append("$descriptor"); | |
1217 | } | |
8381eda7 | 1218 | |
1219 | Btype* btype = this->type()->get_backend(gogo); | |
1220 | ||
1221 | Bvariable* bvar; | |
09e57698 | 1222 | if (no->package() != NULL || is_descriptor) |
f8bdf81a | 1223 | bvar = context->backend()->immutable_struct_reference(var_name, btype, |
1224 | loc); | |
8381eda7 | 1225 | else |
1226 | { | |
1227 | Location bloc = Linemap::predeclared_location(); | |
1228 | bool is_hidden = ((no->is_function() | |
1229 | && no->func_value()->enclosing() != NULL) | |
1230 | || Gogo::is_thunk(no)); | |
1231 | bvar = context->backend()->immutable_struct(var_name, is_hidden, false, | |
1232 | btype, bloc); | |
1233 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1234 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1235 | Expression* init = |
1236 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1237 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1238 | bcontext.set_is_const(); | |
ea664253 | 1239 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1240 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1241 | false, btype, bloc, binit); | |
1242 | } | |
1243 | ||
1244 | this->dvar_ = bvar; | |
ea664253 | 1245 | return gogo->backend()->var_expression(bvar, loc); |
8381eda7 | 1246 | } |
1247 | ||
c6837989 | 1248 | // Print a function descriptor expression. |
1249 | ||
1250 | void | |
1251 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1252 | { | |
1253 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1254 | } | |
1255 | ||
8381eda7 | 1256 | // Make a function descriptor expression. |
1257 | ||
c6837989 | 1258 | Func_descriptor_expression* |
1259 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1260 | { |
c6837989 | 1261 | return new Func_descriptor_expression(fn); |
8381eda7 | 1262 | } |
1263 | ||
1264 | // Make the function descriptor type, so that it can be converted. | |
1265 | ||
1266 | void | |
1267 | Expression::make_func_descriptor_type() | |
1268 | { | |
1269 | Func_descriptor_expression::make_func_descriptor_type(); | |
1270 | } | |
1271 | ||
1272 | // A reference to just the code of a function. | |
1273 | ||
1274 | class Func_code_reference_expression : public Expression | |
1275 | { | |
1276 | public: | |
1277 | Func_code_reference_expression(Named_object* function, Location location) | |
1278 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1279 | function_(function) | |
1280 | { } | |
1281 | ||
1282 | protected: | |
1283 | int | |
1284 | do_traverse(Traverse*) | |
1285 | { return TRAVERSE_CONTINUE; } | |
1286 | ||
f9ca30f9 | 1287 | bool |
1288 | do_is_immutable() const | |
1289 | { return true; } | |
1290 | ||
8381eda7 | 1291 | Type* |
1292 | do_type() | |
1293 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1294 | ||
1295 | void | |
1296 | do_determine_type(const Type_context*) | |
1297 | { } | |
1298 | ||
1299 | Expression* | |
1300 | do_copy() | |
1301 | { | |
1302 | return Expression::make_func_code_reference(this->function_, | |
1303 | this->location()); | |
1304 | } | |
1305 | ||
ea664253 | 1306 | Bexpression* |
1307 | do_get_backend(Translate_context*); | |
8381eda7 | 1308 | |
1309 | void | |
1310 | do_dump_expression(Ast_dump_context* context) const | |
1311 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1312 | ||
1313 | private: | |
1314 | // The function. | |
1315 | Named_object* function_; | |
1316 | }; | |
1317 | ||
ea664253 | 1318 | // Get the backend representation for a reference to function code. |
8381eda7 | 1319 | |
ea664253 | 1320 | Bexpression* |
1321 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1322 | { |
ea664253 | 1323 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1324 | this->location()); | |
8381eda7 | 1325 | } |
1326 | ||
1327 | // Make a reference to the code of a function. | |
1328 | ||
1329 | Expression* | |
1330 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1331 | { | |
1332 | return new Func_code_reference_expression(function, location); | |
1333 | } | |
1334 | ||
e440a328 | 1335 | // Class Unknown_expression. |
1336 | ||
1337 | // Return the name of an unknown expression. | |
1338 | ||
1339 | const std::string& | |
1340 | Unknown_expression::name() const | |
1341 | { | |
1342 | return this->named_object_->name(); | |
1343 | } | |
1344 | ||
1345 | // Lower a reference to an unknown name. | |
1346 | ||
1347 | Expression* | |
ceeb4318 | 1348 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1349 | { |
b13c66cd | 1350 | Location location = this->location(); |
e440a328 | 1351 | Named_object* no = this->named_object_; |
deded542 | 1352 | Named_object* real; |
1353 | if (!no->is_unknown()) | |
1354 | real = no; | |
1355 | else | |
e440a328 | 1356 | { |
deded542 | 1357 | real = no->unknown_value()->real_named_object(); |
1358 | if (real == NULL) | |
1359 | { | |
1360 | if (this->is_composite_literal_key_) | |
1361 | return this; | |
acf8e158 | 1362 | if (!this->no_error_message_) |
1363 | error_at(location, "reference to undefined name %qs", | |
1364 | this->named_object_->message_name().c_str()); | |
deded542 | 1365 | return Expression::make_error(location); |
1366 | } | |
e440a328 | 1367 | } |
1368 | switch (real->classification()) | |
1369 | { | |
1370 | case Named_object::NAMED_OBJECT_CONST: | |
1371 | return Expression::make_const_reference(real, location); | |
1372 | case Named_object::NAMED_OBJECT_TYPE: | |
1373 | return Expression::make_type(real->type_value(), location); | |
1374 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1375 | if (this->is_composite_literal_key_) | |
1376 | return this; | |
acf8e158 | 1377 | if (!this->no_error_message_) |
1378 | error_at(location, "reference to undefined type %qs", | |
1379 | real->message_name().c_str()); | |
e440a328 | 1380 | return Expression::make_error(location); |
1381 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1382 | real->var_value()->set_is_used(); |
e440a328 | 1383 | return Expression::make_var_reference(real, location); |
1384 | case Named_object::NAMED_OBJECT_FUNC: | |
1385 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1386 | return Expression::make_func_reference(real, NULL, location); | |
1387 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1388 | if (this->is_composite_literal_key_) | |
1389 | return this; | |
acf8e158 | 1390 | if (!this->no_error_message_) |
1391 | error_at(location, "unexpected reference to package"); | |
e440a328 | 1392 | return Expression::make_error(location); |
1393 | default: | |
c3e6f413 | 1394 | go_unreachable(); |
e440a328 | 1395 | } |
1396 | } | |
1397 | ||
d751bb78 | 1398 | // Dump the ast representation for an unknown expression to a dump context. |
1399 | ||
1400 | void | |
1401 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1402 | { | |
1403 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1404 | << ")"; | |
d751bb78 | 1405 | } |
1406 | ||
e440a328 | 1407 | // Make a reference to an unknown name. |
1408 | ||
acf8e158 | 1409 | Unknown_expression* |
b13c66cd | 1410 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1411 | { |
e440a328 | 1412 | return new Unknown_expression(no, location); |
1413 | } | |
1414 | ||
1415 | // A boolean expression. | |
1416 | ||
1417 | class Boolean_expression : public Expression | |
1418 | { | |
1419 | public: | |
b13c66cd | 1420 | Boolean_expression(bool val, Location location) |
e440a328 | 1421 | : Expression(EXPRESSION_BOOLEAN, location), |
1422 | val_(val), type_(NULL) | |
1423 | { } | |
1424 | ||
1425 | static Expression* | |
1426 | do_import(Import*); | |
1427 | ||
1428 | protected: | |
1429 | bool | |
1430 | do_is_constant() const | |
1431 | { return true; } | |
1432 | ||
0e168074 | 1433 | bool |
1434 | do_is_immutable() const | |
1435 | { return true; } | |
1436 | ||
e440a328 | 1437 | Type* |
1438 | do_type(); | |
1439 | ||
1440 | void | |
1441 | do_determine_type(const Type_context*); | |
1442 | ||
1443 | Expression* | |
1444 | do_copy() | |
1445 | { return this; } | |
1446 | ||
ea664253 | 1447 | Bexpression* |
1448 | do_get_backend(Translate_context* context) | |
1449 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1450 | |
1451 | void | |
1452 | do_export(Export* exp) const | |
1453 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1454 | ||
d751bb78 | 1455 | void |
1456 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1457 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1458 | ||
e440a328 | 1459 | private: |
1460 | // The constant. | |
1461 | bool val_; | |
1462 | // The type as determined by context. | |
1463 | Type* type_; | |
1464 | }; | |
1465 | ||
1466 | // Get the type. | |
1467 | ||
1468 | Type* | |
1469 | Boolean_expression::do_type() | |
1470 | { | |
1471 | if (this->type_ == NULL) | |
1472 | this->type_ = Type::make_boolean_type(); | |
1473 | return this->type_; | |
1474 | } | |
1475 | ||
1476 | // Set the type from the context. | |
1477 | ||
1478 | void | |
1479 | Boolean_expression::do_determine_type(const Type_context* context) | |
1480 | { | |
1481 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1482 | ; | |
1483 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1484 | this->type_ = context->type; | |
1485 | else if (!context->may_be_abstract) | |
1486 | this->type_ = Type::lookup_bool_type(); | |
1487 | } | |
1488 | ||
1489 | // Import a boolean constant. | |
1490 | ||
1491 | Expression* | |
1492 | Boolean_expression::do_import(Import* imp) | |
1493 | { | |
1494 | if (imp->peek_char() == 't') | |
1495 | { | |
1496 | imp->require_c_string("true"); | |
1497 | return Expression::make_boolean(true, imp->location()); | |
1498 | } | |
1499 | else | |
1500 | { | |
1501 | imp->require_c_string("false"); | |
1502 | return Expression::make_boolean(false, imp->location()); | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | // Make a boolean expression. | |
1507 | ||
1508 | Expression* | |
b13c66cd | 1509 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1510 | { |
1511 | return new Boolean_expression(val, location); | |
1512 | } | |
1513 | ||
1514 | // Class String_expression. | |
1515 | ||
1516 | // Get the type. | |
1517 | ||
1518 | Type* | |
1519 | String_expression::do_type() | |
1520 | { | |
1521 | if (this->type_ == NULL) | |
1522 | this->type_ = Type::make_string_type(); | |
1523 | return this->type_; | |
1524 | } | |
1525 | ||
1526 | // Set the type from the context. | |
1527 | ||
1528 | void | |
1529 | String_expression::do_determine_type(const Type_context* context) | |
1530 | { | |
1531 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1532 | ; | |
1533 | else if (context->type != NULL && context->type->is_string_type()) | |
1534 | this->type_ = context->type; | |
1535 | else if (!context->may_be_abstract) | |
1536 | this->type_ = Type::lookup_string_type(); | |
1537 | } | |
1538 | ||
1539 | // Build a string constant. | |
1540 | ||
ea664253 | 1541 | Bexpression* |
1542 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1543 | { |
2c809f8f | 1544 | Gogo* gogo = context->gogo(); |
1545 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1546 | ||
1547 | Location loc = this->location(); | |
1548 | std::vector<Bexpression*> init(2); | |
1549 | Bexpression* str_cst = | |
1550 | gogo->backend()->string_constant_expression(this->val_); | |
1551 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1552 | ||
1553 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1554 | mpz_t lenval; | |
1555 | mpz_init_set_ui(lenval, this->val_.length()); | |
1556 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1557 | mpz_clear(lenval); | |
1558 | ||
ea664253 | 1559 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1560 | } |
1561 | ||
8b1c301d | 1562 | // Write string literal to string dump. |
e440a328 | 1563 | |
1564 | void | |
8b1c301d | 1565 | String_expression::export_string(String_dump* exp, |
1566 | const String_expression* str) | |
e440a328 | 1567 | { |
1568 | std::string s; | |
8b1c301d | 1569 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1570 | s += '"'; |
8b1c301d | 1571 | for (std::string::const_iterator p = str->val_.begin(); |
1572 | p != str->val_.end(); | |
e440a328 | 1573 | ++p) |
1574 | { | |
1575 | if (*p == '\\' || *p == '"') | |
1576 | { | |
1577 | s += '\\'; | |
1578 | s += *p; | |
1579 | } | |
1580 | else if (*p >= 0x20 && *p < 0x7f) | |
1581 | s += *p; | |
1582 | else if (*p == '\n') | |
1583 | s += "\\n"; | |
1584 | else if (*p == '\t') | |
1585 | s += "\\t"; | |
1586 | else | |
1587 | { | |
1588 | s += "\\x"; | |
1589 | unsigned char c = *p; | |
1590 | unsigned int dig = c >> 4; | |
1591 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1592 | dig = c & 0xf; | |
1593 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1594 | } | |
1595 | } | |
1596 | s += '"'; | |
1597 | exp->write_string(s); | |
1598 | } | |
1599 | ||
8b1c301d | 1600 | // Export a string expression. |
1601 | ||
1602 | void | |
1603 | String_expression::do_export(Export* exp) const | |
1604 | { | |
1605 | String_expression::export_string(exp, this); | |
1606 | } | |
1607 | ||
e440a328 | 1608 | // Import a string expression. |
1609 | ||
1610 | Expression* | |
1611 | String_expression::do_import(Import* imp) | |
1612 | { | |
1613 | imp->require_c_string("\""); | |
1614 | std::string val; | |
1615 | while (true) | |
1616 | { | |
1617 | int c = imp->get_char(); | |
1618 | if (c == '"' || c == -1) | |
1619 | break; | |
1620 | if (c != '\\') | |
1621 | val += static_cast<char>(c); | |
1622 | else | |
1623 | { | |
1624 | c = imp->get_char(); | |
1625 | if (c == '\\' || c == '"') | |
1626 | val += static_cast<char>(c); | |
1627 | else if (c == 'n') | |
1628 | val += '\n'; | |
1629 | else if (c == 't') | |
1630 | val += '\t'; | |
1631 | else if (c == 'x') | |
1632 | { | |
1633 | c = imp->get_char(); | |
1634 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1635 | c = imp->get_char(); | |
1636 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1637 | char v = (vh << 4) | vl; | |
1638 | val += v; | |
1639 | } | |
1640 | else | |
1641 | { | |
1642 | error_at(imp->location(), "bad string constant"); | |
1643 | return Expression::make_error(imp->location()); | |
1644 | } | |
1645 | } | |
1646 | } | |
1647 | return Expression::make_string(val, imp->location()); | |
1648 | } | |
1649 | ||
d751bb78 | 1650 | // Ast dump for string expression. |
1651 | ||
1652 | void | |
1653 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1654 | { | |
8b1c301d | 1655 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1656 | } |
1657 | ||
e440a328 | 1658 | // Make a string expression. |
1659 | ||
1660 | Expression* | |
b13c66cd | 1661 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1662 | { |
1663 | return new String_expression(val, location); | |
1664 | } | |
1665 | ||
2c809f8f | 1666 | // An expression that evaluates to some characteristic of a string. |
1667 | // This is used when indexing, bound-checking, or nil checking a string. | |
1668 | ||
1669 | class String_info_expression : public Expression | |
1670 | { | |
1671 | public: | |
1672 | String_info_expression(Expression* string, String_info string_info, | |
1673 | Location location) | |
1674 | : Expression(EXPRESSION_STRING_INFO, location), | |
1675 | string_(string), string_info_(string_info) | |
1676 | { } | |
1677 | ||
1678 | protected: | |
1679 | Type* | |
1680 | do_type(); | |
1681 | ||
1682 | void | |
1683 | do_determine_type(const Type_context*) | |
1684 | { go_unreachable(); } | |
1685 | ||
1686 | Expression* | |
1687 | do_copy() | |
1688 | { | |
1689 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1690 | this->location()); | |
1691 | } | |
1692 | ||
ea664253 | 1693 | Bexpression* |
1694 | do_get_backend(Translate_context* context); | |
2c809f8f | 1695 | |
1696 | void | |
1697 | do_dump_expression(Ast_dump_context*) const; | |
1698 | ||
1699 | void | |
1700 | do_issue_nil_check() | |
1701 | { this->string_->issue_nil_check(); } | |
1702 | ||
1703 | private: | |
1704 | // The string for which we are getting information. | |
1705 | Expression* string_; | |
1706 | // What information we want. | |
1707 | String_info string_info_; | |
1708 | }; | |
1709 | ||
1710 | // Return the type of the string info. | |
1711 | ||
1712 | Type* | |
1713 | String_info_expression::do_type() | |
1714 | { | |
1715 | switch (this->string_info_) | |
1716 | { | |
1717 | case STRING_INFO_DATA: | |
1718 | { | |
1719 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1720 | return Type::make_pointer_type(byte_type); | |
1721 | } | |
1722 | case STRING_INFO_LENGTH: | |
1723 | return Type::lookup_integer_type("int"); | |
1724 | default: | |
1725 | go_unreachable(); | |
1726 | } | |
1727 | } | |
1728 | ||
1729 | // Return string information in GENERIC. | |
1730 | ||
ea664253 | 1731 | Bexpression* |
1732 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1733 | { |
1734 | Gogo* gogo = context->gogo(); | |
1735 | ||
ea664253 | 1736 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1737 | switch (this->string_info_) |
1738 | { | |
1739 | case STRING_INFO_DATA: | |
1740 | case STRING_INFO_LENGTH: | |
ea664253 | 1741 | return gogo->backend()->struct_field_expression(bstring, |
1742 | this->string_info_, | |
1743 | this->location()); | |
2c809f8f | 1744 | break; |
1745 | default: | |
1746 | go_unreachable(); | |
1747 | } | |
2c809f8f | 1748 | } |
1749 | ||
1750 | // Dump ast representation for a type info expression. | |
1751 | ||
1752 | void | |
1753 | String_info_expression::do_dump_expression( | |
1754 | Ast_dump_context* ast_dump_context) const | |
1755 | { | |
1756 | ast_dump_context->ostream() << "stringinfo("; | |
1757 | this->string_->dump_expression(ast_dump_context); | |
1758 | ast_dump_context->ostream() << ","; | |
1759 | ast_dump_context->ostream() << | |
1760 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1761 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1762 | : "unknown"); | |
1763 | ast_dump_context->ostream() << ")"; | |
1764 | } | |
1765 | ||
1766 | // Make a string info expression. | |
1767 | ||
1768 | Expression* | |
1769 | Expression::make_string_info(Expression* string, String_info string_info, | |
1770 | Location location) | |
1771 | { | |
1772 | return new String_info_expression(string, string_info, location); | |
1773 | } | |
1774 | ||
e440a328 | 1775 | // Make an integer expression. |
1776 | ||
1777 | class Integer_expression : public Expression | |
1778 | { | |
1779 | public: | |
5d4b8566 | 1780 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1781 | Location location) | |
e440a328 | 1782 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1783 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1784 | { mpz_init_set(this->val_, *val); } |
1785 | ||
1786 | static Expression* | |
1787 | do_import(Import*); | |
1788 | ||
8b1c301d | 1789 | // Write VAL to string dump. |
e440a328 | 1790 | static void |
8b1c301d | 1791 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1792 | |
d751bb78 | 1793 | // Write VAL to dump context. |
1794 | static void | |
1795 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1796 | ||
e440a328 | 1797 | protected: |
1798 | bool | |
1799 | do_is_constant() const | |
1800 | { return true; } | |
1801 | ||
0e168074 | 1802 | bool |
1803 | do_is_immutable() const | |
1804 | { return true; } | |
1805 | ||
e440a328 | 1806 | bool |
0c77715b | 1807 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1808 | |
1809 | Type* | |
1810 | do_type(); | |
1811 | ||
1812 | void | |
1813 | do_determine_type(const Type_context* context); | |
1814 | ||
1815 | void | |
1816 | do_check_types(Gogo*); | |
1817 | ||
ea664253 | 1818 | Bexpression* |
1819 | do_get_backend(Translate_context*); | |
e440a328 | 1820 | |
1821 | Expression* | |
1822 | do_copy() | |
5d4b8566 | 1823 | { |
1824 | if (this->is_character_constant_) | |
1825 | return Expression::make_character(&this->val_, this->type_, | |
1826 | this->location()); | |
1827 | else | |
e67508fa | 1828 | return Expression::make_integer_z(&this->val_, this->type_, |
1829 | this->location()); | |
5d4b8566 | 1830 | } |
e440a328 | 1831 | |
1832 | void | |
1833 | do_export(Export*) const; | |
1834 | ||
d751bb78 | 1835 | void |
1836 | do_dump_expression(Ast_dump_context*) const; | |
1837 | ||
e440a328 | 1838 | private: |
1839 | // The integer value. | |
1840 | mpz_t val_; | |
1841 | // The type so far. | |
1842 | Type* type_; | |
5d4b8566 | 1843 | // Whether this is a character constant. |
1844 | bool is_character_constant_; | |
e440a328 | 1845 | }; |
1846 | ||
0c77715b | 1847 | // Return a numeric constant for this expression. We have to mark |
1848 | // this as a character when appropriate. | |
e440a328 | 1849 | |
1850 | bool | |
0c77715b | 1851 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1852 | { |
0c77715b | 1853 | if (this->is_character_constant_) |
1854 | nc->set_rune(this->type_, this->val_); | |
1855 | else | |
1856 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1857 | return true; |
1858 | } | |
1859 | ||
1860 | // Return the current type. If we haven't set the type yet, we return | |
1861 | // an abstract integer type. | |
1862 | ||
1863 | Type* | |
1864 | Integer_expression::do_type() | |
1865 | { | |
1866 | if (this->type_ == NULL) | |
5d4b8566 | 1867 | { |
1868 | if (this->is_character_constant_) | |
1869 | this->type_ = Type::make_abstract_character_type(); | |
1870 | else | |
1871 | this->type_ = Type::make_abstract_integer_type(); | |
1872 | } | |
e440a328 | 1873 | return this->type_; |
1874 | } | |
1875 | ||
1876 | // Set the type of the integer value. Here we may switch from an | |
1877 | // abstract type to a real type. | |
1878 | ||
1879 | void | |
1880 | Integer_expression::do_determine_type(const Type_context* context) | |
1881 | { | |
1882 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1883 | ; | |
0c77715b | 1884 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1885 | this->type_ = context->type; |
1886 | else if (!context->may_be_abstract) | |
5d4b8566 | 1887 | { |
1888 | if (this->is_character_constant_) | |
1889 | this->type_ = Type::lookup_integer_type("int32"); | |
1890 | else | |
1891 | this->type_ = Type::lookup_integer_type("int"); | |
1892 | } | |
e440a328 | 1893 | } |
1894 | ||
e440a328 | 1895 | // Check the type of an integer constant. |
1896 | ||
1897 | void | |
1898 | Integer_expression::do_check_types(Gogo*) | |
1899 | { | |
0c77715b | 1900 | Type* type = this->type_; |
1901 | if (type == NULL) | |
e440a328 | 1902 | return; |
0c77715b | 1903 | Numeric_constant nc; |
1904 | if (this->is_character_constant_) | |
1905 | nc.set_rune(NULL, this->val_); | |
1906 | else | |
1907 | nc.set_int(NULL, this->val_); | |
1908 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1909 | this->set_is_error(); |
1910 | } | |
1911 | ||
ea664253 | 1912 | // Get the backend representation for an integer constant. |
e440a328 | 1913 | |
ea664253 | 1914 | Bexpression* |
1915 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1916 | { |
12373dd5 | 1917 | if (this->is_error_expression() |
1918 | || (this->type_ != NULL && this->type_->is_error_type())) | |
1919 | { | |
1920 | go_assert(saw_errors()); | |
1921 | return context->gogo()->backend()->error_expression(); | |
1922 | } | |
1923 | ||
48c2a53a | 1924 | Type* resolved_type = NULL; |
e440a328 | 1925 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 1926 | resolved_type = this->type_; |
e440a328 | 1927 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
1928 | { | |
1929 | // We are converting to an abstract floating point type. | |
48c2a53a | 1930 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 1931 | } |
1932 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1933 | { | |
1934 | // We are converting to an abstract complex type. | |
48c2a53a | 1935 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 1936 | } |
1937 | else | |
1938 | { | |
1939 | // If we still have an abstract type here, then this is being | |
1940 | // used in a constant expression which didn't get reduced for | |
1941 | // some reason. Use a type which will fit the value. We use <, | |
1942 | // not <=, because we need an extra bit for the sign bit. | |
1943 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 1944 | Type* int_type = Type::lookup_integer_type("int"); |
1945 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 1946 | resolved_type = int_type; |
e440a328 | 1947 | else if (bits < 64) |
48c2a53a | 1948 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 1949 | else |
48c2a53a | 1950 | { |
1951 | if (!saw_errors()) | |
1952 | error_at(this->location(), | |
1953 | "unknown type for large integer constant"); | |
ea664253 | 1954 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 1955 | } |
e440a328 | 1956 | } |
48c2a53a | 1957 | Numeric_constant nc; |
1958 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 1959 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 1960 | } |
1961 | ||
1962 | // Write VAL to export data. | |
1963 | ||
1964 | void | |
8b1c301d | 1965 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 1966 | { |
1967 | char* s = mpz_get_str(NULL, 10, val); | |
1968 | exp->write_c_string(s); | |
1969 | free(s); | |
1970 | } | |
1971 | ||
1972 | // Export an integer in a constant expression. | |
1973 | ||
1974 | void | |
1975 | Integer_expression::do_export(Export* exp) const | |
1976 | { | |
1977 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 1978 | if (this->is_character_constant_) |
1979 | exp->write_c_string("'"); | |
e440a328 | 1980 | // A trailing space lets us reliably identify the end of the number. |
1981 | exp->write_c_string(" "); | |
1982 | } | |
1983 | ||
1984 | // Import an integer, floating point, or complex value. This handles | |
1985 | // all these types because they all start with digits. | |
1986 | ||
1987 | Expression* | |
1988 | Integer_expression::do_import(Import* imp) | |
1989 | { | |
1990 | std::string num = imp->read_identifier(); | |
1991 | imp->require_c_string(" "); | |
1992 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1993 | { | |
1994 | mpfr_t real; | |
1995 | size_t plus_pos = num.find('+', 1); | |
1996 | size_t minus_pos = num.find('-', 1); | |
1997 | size_t pos; | |
1998 | if (plus_pos == std::string::npos) | |
1999 | pos = minus_pos; | |
2000 | else if (minus_pos == std::string::npos) | |
2001 | pos = plus_pos; | |
2002 | else | |
2003 | { | |
2004 | error_at(imp->location(), "bad number in import data: %qs", | |
2005 | num.c_str()); | |
2006 | return Expression::make_error(imp->location()); | |
2007 | } | |
2008 | if (pos == std::string::npos) | |
2009 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2010 | else | |
2011 | { | |
2012 | std::string real_str = num.substr(0, pos); | |
2013 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2014 | { | |
2015 | error_at(imp->location(), "bad number in import data: %qs", | |
2016 | real_str.c_str()); | |
2017 | return Expression::make_error(imp->location()); | |
2018 | } | |
2019 | } | |
2020 | ||
2021 | std::string imag_str; | |
2022 | if (pos == std::string::npos) | |
2023 | imag_str = num; | |
2024 | else | |
2025 | imag_str = num.substr(pos); | |
2026 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2027 | mpfr_t imag; | |
2028 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2029 | { | |
2030 | error_at(imp->location(), "bad number in import data: %qs", | |
2031 | imag_str.c_str()); | |
2032 | return Expression::make_error(imp->location()); | |
2033 | } | |
fcbea5e4 | 2034 | mpc_t cval; |
2035 | mpc_init2(cval, mpc_precision); | |
2036 | mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); | |
e440a328 | 2037 | mpfr_clear(real); |
2038 | mpfr_clear(imag); | |
fcbea5e4 | 2039 | Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); |
2040 | mpc_clear(cval); | |
e440a328 | 2041 | return ret; |
2042 | } | |
2043 | else if (num.find('.') == std::string::npos | |
2044 | && num.find('E') == std::string::npos) | |
2045 | { | |
5d4b8566 | 2046 | bool is_character_constant = (!num.empty() |
2047 | && num[num.length() - 1] == '\''); | |
2048 | if (is_character_constant) | |
2049 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2050 | mpz_t val; |
2051 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2052 | { | |
2053 | error_at(imp->location(), "bad number in import data: %qs", | |
2054 | num.c_str()); | |
2055 | return Expression::make_error(imp->location()); | |
2056 | } | |
5d4b8566 | 2057 | Expression* ret; |
2058 | if (is_character_constant) | |
2059 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2060 | else | |
e67508fa | 2061 | ret = Expression::make_integer_z(&val, NULL, imp->location()); |
e440a328 | 2062 | mpz_clear(val); |
2063 | return ret; | |
2064 | } | |
2065 | else | |
2066 | { | |
2067 | mpfr_t val; | |
2068 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2069 | { | |
2070 | error_at(imp->location(), "bad number in import data: %qs", | |
2071 | num.c_str()); | |
2072 | return Expression::make_error(imp->location()); | |
2073 | } | |
2074 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2075 | mpfr_clear(val); | |
2076 | return ret; | |
2077 | } | |
2078 | } | |
d751bb78 | 2079 | // Ast dump for integer expression. |
2080 | ||
2081 | void | |
2082 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2083 | { | |
5d4b8566 | 2084 | if (this->is_character_constant_) |
2085 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2086 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2087 | if (this->is_character_constant_) |
2088 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2089 | } |
2090 | ||
e67508fa | 2091 | // Build a new integer value from a multi-precision integer. |
e440a328 | 2092 | |
2093 | Expression* | |
e67508fa | 2094 | Expression::make_integer_z(const mpz_t* val, Type* type, Location location) |
5d4b8566 | 2095 | { |
2096 | return new Integer_expression(val, type, false, location); | |
2097 | } | |
2098 | ||
e67508fa | 2099 | // Build a new integer value from an unsigned long. |
2100 | ||
2101 | Expression* | |
2102 | Expression::make_integer_ul(unsigned long val, Type *type, Location location) | |
2103 | { | |
2104 | mpz_t zval; | |
2105 | mpz_init_set_ui(zval, val); | |
2106 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2107 | mpz_clear(zval); | |
2108 | return ret; | |
2109 | } | |
2110 | ||
2111 | // Build a new integer value from a signed long. | |
2112 | ||
2113 | Expression* | |
2114 | Expression::make_integer_sl(long val, Type *type, Location location) | |
2115 | { | |
2116 | mpz_t zval; | |
2117 | mpz_init_set_si(zval, val); | |
2118 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2119 | mpz_clear(zval); | |
2120 | return ret; | |
2121 | } | |
2122 | ||
3f378015 | 2123 | // Store an int64_t in an uninitialized mpz_t. |
2124 | ||
2125 | static void | |
2126 | set_mpz_from_int64(mpz_t* zval, int64_t val) | |
2127 | { | |
2128 | if (val >= 0) | |
2129 | { | |
2130 | unsigned long ul = static_cast<unsigned long>(val); | |
2131 | if (static_cast<int64_t>(ul) == val) | |
2132 | { | |
2133 | mpz_init_set_ui(*zval, ul); | |
2134 | return; | |
2135 | } | |
2136 | } | |
2137 | uint64_t uv; | |
2138 | if (val >= 0) | |
2139 | uv = static_cast<uint64_t>(val); | |
2140 | else | |
2141 | uv = static_cast<uint64_t>(- val); | |
2142 | unsigned long ul = uv & 0xffffffffUL; | |
2143 | mpz_init_set_ui(*zval, ul); | |
2144 | mpz_t hval; | |
2145 | mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); | |
2146 | mpz_mul_2exp(hval, hval, 32); | |
2147 | mpz_add(*zval, *zval, hval); | |
2148 | mpz_clear(hval); | |
2149 | if (val < 0) | |
2150 | mpz_neg(*zval, *zval); | |
2151 | } | |
2152 | ||
2153 | // Build a new integer value from an int64_t. | |
2154 | ||
2155 | Expression* | |
2156 | Expression::make_integer_int64(int64_t val, Type* type, Location location) | |
2157 | { | |
2158 | mpz_t zval; | |
2159 | set_mpz_from_int64(&zval, val); | |
2160 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2161 | mpz_clear(zval); | |
2162 | return ret; | |
2163 | } | |
2164 | ||
5d4b8566 | 2165 | // Build a new character constant value. |
2166 | ||
2167 | Expression* | |
2168 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2169 | { |
5d4b8566 | 2170 | return new Integer_expression(val, type, true, location); |
e440a328 | 2171 | } |
2172 | ||
2173 | // Floats. | |
2174 | ||
2175 | class Float_expression : public Expression | |
2176 | { | |
2177 | public: | |
b13c66cd | 2178 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2179 | : Expression(EXPRESSION_FLOAT, location), |
2180 | type_(type) | |
2181 | { | |
2182 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2183 | } | |
2184 | ||
e440a328 | 2185 | // Write VAL to export data. |
2186 | static void | |
8b1c301d | 2187 | export_float(String_dump* exp, const mpfr_t val); |
2188 | ||
d751bb78 | 2189 | // Write VAL to dump file. |
2190 | static void | |
2191 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2192 | |
2193 | protected: | |
2194 | bool | |
2195 | do_is_constant() const | |
2196 | { return true; } | |
2197 | ||
0e168074 | 2198 | bool |
2199 | do_is_immutable() const | |
2200 | { return true; } | |
2201 | ||
e440a328 | 2202 | bool |
0c77715b | 2203 | do_numeric_constant_value(Numeric_constant* nc) const |
2204 | { | |
2205 | nc->set_float(this->type_, this->val_); | |
2206 | return true; | |
2207 | } | |
e440a328 | 2208 | |
2209 | Type* | |
2210 | do_type(); | |
2211 | ||
2212 | void | |
2213 | do_determine_type(const Type_context*); | |
2214 | ||
2215 | void | |
2216 | do_check_types(Gogo*); | |
2217 | ||
2218 | Expression* | |
2219 | do_copy() | |
2220 | { return Expression::make_float(&this->val_, this->type_, | |
2221 | this->location()); } | |
2222 | ||
ea664253 | 2223 | Bexpression* |
2224 | do_get_backend(Translate_context*); | |
e440a328 | 2225 | |
2226 | void | |
2227 | do_export(Export*) const; | |
2228 | ||
d751bb78 | 2229 | void |
2230 | do_dump_expression(Ast_dump_context*) const; | |
2231 | ||
e440a328 | 2232 | private: |
2233 | // The floating point value. | |
2234 | mpfr_t val_; | |
2235 | // The type so far. | |
2236 | Type* type_; | |
2237 | }; | |
2238 | ||
e440a328 | 2239 | // Return the current type. If we haven't set the type yet, we return |
2240 | // an abstract float type. | |
2241 | ||
2242 | Type* | |
2243 | Float_expression::do_type() | |
2244 | { | |
2245 | if (this->type_ == NULL) | |
2246 | this->type_ = Type::make_abstract_float_type(); | |
2247 | return this->type_; | |
2248 | } | |
2249 | ||
2250 | // Set the type of the float value. Here we may switch from an | |
2251 | // abstract type to a real type. | |
2252 | ||
2253 | void | |
2254 | Float_expression::do_determine_type(const Type_context* context) | |
2255 | { | |
2256 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2257 | ; | |
2258 | else if (context->type != NULL | |
2259 | && (context->type->integer_type() != NULL | |
2260 | || context->type->float_type() != NULL | |
2261 | || context->type->complex_type() != NULL)) | |
2262 | this->type_ = context->type; | |
2263 | else if (!context->may_be_abstract) | |
48080209 | 2264 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2265 | } |
2266 | ||
e440a328 | 2267 | // Check the type of a float value. |
2268 | ||
2269 | void | |
2270 | Float_expression::do_check_types(Gogo*) | |
2271 | { | |
0c77715b | 2272 | Type* type = this->type_; |
2273 | if (type == NULL) | |
e440a328 | 2274 | return; |
0c77715b | 2275 | Numeric_constant nc; |
2276 | nc.set_float(NULL, this->val_); | |
2277 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2278 | this->set_is_error(); |
e440a328 | 2279 | } |
2280 | ||
ea664253 | 2281 | // Get the backend representation for a float constant. |
e440a328 | 2282 | |
ea664253 | 2283 | Bexpression* |
2284 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2285 | { |
12373dd5 | 2286 | if (this->is_error_expression() |
2287 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2288 | { | |
2289 | go_assert(saw_errors()); | |
2290 | return context->gogo()->backend()->error_expression(); | |
2291 | } | |
2292 | ||
48c2a53a | 2293 | Type* resolved_type; |
e440a328 | 2294 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2295 | resolved_type = this->type_; |
e440a328 | 2296 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2297 | { | |
2298 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2299 | resolved_type = Type::lookup_integer_type("int"); |
2300 | } | |
2301 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2302 | { | |
2303 | // We are converting to an abstract complex type. | |
2304 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2305 | } |
2306 | else | |
2307 | { | |
2308 | // If we still have an abstract type here, then this is being | |
2309 | // used in a constant expression which didn't get reduced. We | |
2310 | // just use float64 and hope for the best. | |
48c2a53a | 2311 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2312 | } |
48c2a53a | 2313 | |
2314 | Numeric_constant nc; | |
2315 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2316 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2317 | } |
2318 | ||
8b1c301d | 2319 | // Write a floating point number to a string dump. |
e440a328 | 2320 | |
2321 | void | |
8b1c301d | 2322 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2323 | { |
2324 | mp_exp_t exponent; | |
2325 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2326 | if (*s == '-') | |
2327 | exp->write_c_string("-"); | |
2328 | exp->write_c_string("0."); | |
2329 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2330 | mpfr_free_str(s); | |
2331 | char buf[30]; | |
2332 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2333 | exp->write_c_string(buf); | |
2334 | } | |
2335 | ||
2336 | // Export a floating point number in a constant expression. | |
2337 | ||
2338 | void | |
2339 | Float_expression::do_export(Export* exp) const | |
2340 | { | |
2341 | Float_expression::export_float(exp, this->val_); | |
2342 | // A trailing space lets us reliably identify the end of the number. | |
2343 | exp->write_c_string(" "); | |
2344 | } | |
2345 | ||
d751bb78 | 2346 | // Dump a floating point number to the dump file. |
2347 | ||
2348 | void | |
2349 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2350 | { | |
8b1c301d | 2351 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2352 | } |
2353 | ||
e440a328 | 2354 | // Make a float expression. |
2355 | ||
2356 | Expression* | |
b13c66cd | 2357 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2358 | { |
2359 | return new Float_expression(val, type, location); | |
2360 | } | |
2361 | ||
2362 | // Complex numbers. | |
2363 | ||
2364 | class Complex_expression : public Expression | |
2365 | { | |
2366 | public: | |
fcbea5e4 | 2367 | Complex_expression(const mpc_t* val, Type* type, Location location) |
e440a328 | 2368 | : Expression(EXPRESSION_COMPLEX, location), |
2369 | type_(type) | |
2370 | { | |
fcbea5e4 | 2371 | mpc_init2(this->val_, mpc_precision); |
2372 | mpc_set(this->val_, *val, MPC_RNDNN); | |
e440a328 | 2373 | } |
2374 | ||
fcbea5e4 | 2375 | // Write VAL to string dump. |
e440a328 | 2376 | static void |
fcbea5e4 | 2377 | export_complex(String_dump* exp, const mpc_t val); |
e440a328 | 2378 | |
d751bb78 | 2379 | // Write REAL/IMAG to dump context. |
2380 | static void | |
fcbea5e4 | 2381 | dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); |
d751bb78 | 2382 | |
e440a328 | 2383 | protected: |
2384 | bool | |
2385 | do_is_constant() const | |
2386 | { return true; } | |
2387 | ||
0e168074 | 2388 | bool |
2389 | do_is_immutable() const | |
2390 | { return true; } | |
2391 | ||
e440a328 | 2392 | bool |
0c77715b | 2393 | do_numeric_constant_value(Numeric_constant* nc) const |
2394 | { | |
fcbea5e4 | 2395 | nc->set_complex(this->type_, this->val_); |
0c77715b | 2396 | return true; |
2397 | } | |
e440a328 | 2398 | |
2399 | Type* | |
2400 | do_type(); | |
2401 | ||
2402 | void | |
2403 | do_determine_type(const Type_context*); | |
2404 | ||
2405 | void | |
2406 | do_check_types(Gogo*); | |
2407 | ||
2408 | Expression* | |
2409 | do_copy() | |
2410 | { | |
fcbea5e4 | 2411 | return Expression::make_complex(&this->val_, this->type_, |
e440a328 | 2412 | this->location()); |
2413 | } | |
2414 | ||
ea664253 | 2415 | Bexpression* |
2416 | do_get_backend(Translate_context*); | |
e440a328 | 2417 | |
2418 | void | |
2419 | do_export(Export*) const; | |
2420 | ||
d751bb78 | 2421 | void |
2422 | do_dump_expression(Ast_dump_context*) const; | |
abd26de0 | 2423 | |
e440a328 | 2424 | private: |
fcbea5e4 | 2425 | // The complex value. |
2426 | mpc_t val_; | |
e440a328 | 2427 | // The type if known. |
2428 | Type* type_; | |
2429 | }; | |
2430 | ||
e440a328 | 2431 | // Return the current type. If we haven't set the type yet, we return |
2432 | // an abstract complex type. | |
2433 | ||
2434 | Type* | |
2435 | Complex_expression::do_type() | |
2436 | { | |
2437 | if (this->type_ == NULL) | |
2438 | this->type_ = Type::make_abstract_complex_type(); | |
2439 | return this->type_; | |
2440 | } | |
2441 | ||
2442 | // Set the type of the complex value. Here we may switch from an | |
2443 | // abstract type to a real type. | |
2444 | ||
2445 | void | |
2446 | Complex_expression::do_determine_type(const Type_context* context) | |
2447 | { | |
2448 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2449 | ; | |
abd26de0 | 2450 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2451 | this->type_ = context->type; |
2452 | else if (!context->may_be_abstract) | |
48080209 | 2453 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2454 | } |
2455 | ||
e440a328 | 2456 | // Check the type of a complex value. |
2457 | ||
2458 | void | |
2459 | Complex_expression::do_check_types(Gogo*) | |
2460 | { | |
0c77715b | 2461 | Type* type = this->type_; |
2462 | if (type == NULL) | |
e440a328 | 2463 | return; |
0c77715b | 2464 | Numeric_constant nc; |
fcbea5e4 | 2465 | nc.set_complex(NULL, this->val_); |
0c77715b | 2466 | if (!nc.set_type(this->type_, true, this->location())) |
e440a328 | 2467 | this->set_is_error(); |
2468 | } | |
2469 | ||
ea664253 | 2470 | // Get the backend representation for a complex constant. |
e440a328 | 2471 | |
ea664253 | 2472 | Bexpression* |
2473 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2474 | { |
12373dd5 | 2475 | if (this->is_error_expression() |
2476 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2477 | { | |
2478 | go_assert(saw_errors()); | |
2479 | return context->gogo()->backend()->error_expression(); | |
2480 | } | |
2481 | ||
48c2a53a | 2482 | Type* resolved_type; |
e440a328 | 2483 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2484 | resolved_type = this->type_; |
2485 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2486 | { | |
2487 | // We are converting to an abstract integer type. | |
2488 | resolved_type = Type::lookup_integer_type("int"); | |
2489 | } | |
2490 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2491 | { | |
2492 | // We are converting to an abstract float type. | |
2493 | resolved_type = Type::lookup_float_type("float64"); | |
2494 | } | |
e440a328 | 2495 | else |
2496 | { | |
47ae02b7 | 2497 | // If we still have an abstract type here, this is being |
e440a328 | 2498 | // used in a constant expression which didn't get reduced. We |
2499 | // just use complex128 and hope for the best. | |
48c2a53a | 2500 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2501 | } |
48c2a53a | 2502 | |
2503 | Numeric_constant nc; | |
fcbea5e4 | 2504 | nc.set_complex(resolved_type, this->val_); |
ea664253 | 2505 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2506 | } |
2507 | ||
2508 | // Write REAL/IMAG to export data. | |
2509 | ||
2510 | void | |
fcbea5e4 | 2511 | Complex_expression::export_complex(String_dump* exp, const mpc_t val) |
e440a328 | 2512 | { |
fcbea5e4 | 2513 | if (!mpfr_zero_p(mpc_realref(val))) |
e440a328 | 2514 | { |
fcbea5e4 | 2515 | Float_expression::export_float(exp, mpc_realref(val)); |
d1db782d | 2516 | if (mpfr_sgn(mpc_imagref(val)) >= 0) |
e440a328 | 2517 | exp->write_c_string("+"); |
2518 | } | |
fcbea5e4 | 2519 | Float_expression::export_float(exp, mpc_imagref(val)); |
e440a328 | 2520 | exp->write_c_string("i"); |
2521 | } | |
2522 | ||
2523 | // Export a complex number in a constant expression. | |
2524 | ||
2525 | void | |
2526 | Complex_expression::do_export(Export* exp) const | |
2527 | { | |
fcbea5e4 | 2528 | Complex_expression::export_complex(exp, this->val_); |
e440a328 | 2529 | // A trailing space lets us reliably identify the end of the number. |
2530 | exp->write_c_string(" "); | |
2531 | } | |
2532 | ||
d751bb78 | 2533 | // Dump a complex expression to the dump file. |
2534 | ||
2535 | void | |
2536 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2537 | { | |
fcbea5e4 | 2538 | Complex_expression::export_complex(ast_dump_context, this->val_); |
d751bb78 | 2539 | } |
2540 | ||
e440a328 | 2541 | // Make a complex expression. |
2542 | ||
2543 | Expression* | |
fcbea5e4 | 2544 | Expression::make_complex(const mpc_t* val, Type* type, Location location) |
e440a328 | 2545 | { |
fcbea5e4 | 2546 | return new Complex_expression(val, type, location); |
e440a328 | 2547 | } |
2548 | ||
d5b605df | 2549 | // Find a named object in an expression. |
2550 | ||
2551 | class Find_named_object : public Traverse | |
2552 | { | |
2553 | public: | |
2554 | Find_named_object(Named_object* no) | |
2555 | : Traverse(traverse_expressions), | |
2556 | no_(no), found_(false) | |
2557 | { } | |
2558 | ||
2559 | // Whether we found the object. | |
2560 | bool | |
2561 | found() const | |
2562 | { return this->found_; } | |
2563 | ||
2564 | protected: | |
2565 | int | |
2566 | expression(Expression**); | |
2567 | ||
2568 | private: | |
2569 | // The object we are looking for. | |
2570 | Named_object* no_; | |
2571 | // Whether we found it. | |
2572 | bool found_; | |
2573 | }; | |
2574 | ||
e440a328 | 2575 | // A reference to a const in an expression. |
2576 | ||
2577 | class Const_expression : public Expression | |
2578 | { | |
2579 | public: | |
b13c66cd | 2580 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2581 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2582 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2583 | { } |
2584 | ||
d5b605df | 2585 | Named_object* |
2586 | named_object() | |
2587 | { return this->constant_; } | |
2588 | ||
a7f064d5 | 2589 | // Check that the initializer does not refer to the constant itself. |
2590 | void | |
2591 | check_for_init_loop(); | |
2592 | ||
e440a328 | 2593 | protected: |
ba4aedd4 | 2594 | int |
2595 | do_traverse(Traverse*); | |
2596 | ||
e440a328 | 2597 | Expression* |
ceeb4318 | 2598 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2599 | |
2600 | bool | |
2601 | do_is_constant() const | |
2602 | { return true; } | |
2603 | ||
0e168074 | 2604 | bool |
2605 | do_is_immutable() const | |
2606 | { return true; } | |
2607 | ||
e440a328 | 2608 | bool |
0c77715b | 2609 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2610 | |
2611 | bool | |
af6b489a | 2612 | do_string_constant_value(std::string* val) const; |
e440a328 | 2613 | |
2614 | Type* | |
2615 | do_type(); | |
2616 | ||
2617 | // The type of a const is set by the declaration, not the use. | |
2618 | void | |
2619 | do_determine_type(const Type_context*); | |
2620 | ||
2621 | void | |
2622 | do_check_types(Gogo*); | |
2623 | ||
2624 | Expression* | |
2625 | do_copy() | |
2626 | { return this; } | |
2627 | ||
ea664253 | 2628 | Bexpression* |
2629 | do_get_backend(Translate_context* context); | |
e440a328 | 2630 | |
2631 | // When exporting a reference to a const as part of a const | |
2632 | // expression, we export the value. We ignore the fact that it has | |
2633 | // a name. | |
2634 | void | |
2635 | do_export(Export* exp) const | |
2636 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2637 | ||
d751bb78 | 2638 | void |
2639 | do_dump_expression(Ast_dump_context*) const; | |
2640 | ||
e440a328 | 2641 | private: |
2642 | // The constant. | |
2643 | Named_object* constant_; | |
2644 | // The type of this reference. This is used if the constant has an | |
2645 | // abstract type. | |
2646 | Type* type_; | |
13e818f5 | 2647 | // Used to prevent infinite recursion when a constant incorrectly |
2648 | // refers to itself. | |
2649 | mutable bool seen_; | |
e440a328 | 2650 | }; |
2651 | ||
ba4aedd4 | 2652 | // Traversal. |
2653 | ||
2654 | int | |
2655 | Const_expression::do_traverse(Traverse* traverse) | |
2656 | { | |
2657 | if (this->type_ != NULL) | |
2658 | return Type::traverse(this->type_, traverse); | |
2659 | return TRAVERSE_CONTINUE; | |
2660 | } | |
2661 | ||
e440a328 | 2662 | // Lower a constant expression. This is where we convert the |
2663 | // predeclared constant iota into an integer value. | |
2664 | ||
2665 | Expression* | |
ceeb4318 | 2666 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2667 | Statement_inserter*, int iota_value) | |
e440a328 | 2668 | { |
2669 | if (this->constant_->const_value()->expr()->classification() | |
2670 | == EXPRESSION_IOTA) | |
2671 | { | |
2672 | if (iota_value == -1) | |
2673 | { | |
2674 | error_at(this->location(), | |
2675 | "iota is only defined in const declarations"); | |
2676 | iota_value = 0; | |
2677 | } | |
e67508fa | 2678 | return Expression::make_integer_ul(iota_value, NULL, this->location()); |
e440a328 | 2679 | } |
2680 | ||
2681 | // Make sure that the constant itself has been lowered. | |
2682 | gogo->lower_constant(this->constant_); | |
2683 | ||
2684 | return this; | |
2685 | } | |
2686 | ||
0c77715b | 2687 | // Return a numeric constant value. |
e440a328 | 2688 | |
2689 | bool | |
0c77715b | 2690 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2691 | { |
13e818f5 | 2692 | if (this->seen_) |
2693 | return false; | |
2694 | ||
e440a328 | 2695 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2696 | |
13e818f5 | 2697 | this->seen_ = true; |
2698 | ||
0c77715b | 2699 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2700 | |
13e818f5 | 2701 | this->seen_ = false; |
2702 | ||
e440a328 | 2703 | Type* ctype; |
2704 | if (this->type_ != NULL) | |
2705 | ctype = this->type_; | |
2706 | else | |
2707 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2708 | if (r && ctype != NULL) |
2709 | { | |
0c77715b | 2710 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2711 | return false; |
e440a328 | 2712 | } |
e440a328 | 2713 | |
e440a328 | 2714 | return r; |
2715 | } | |
2716 | ||
af6b489a | 2717 | bool |
2718 | Const_expression::do_string_constant_value(std::string* val) const | |
2719 | { | |
2720 | if (this->seen_) | |
2721 | return false; | |
2722 | ||
2723 | Expression* e = this->constant_->const_value()->expr(); | |
2724 | ||
2725 | this->seen_ = true; | |
2726 | bool ok = e->string_constant_value(val); | |
2727 | this->seen_ = false; | |
2728 | ||
2729 | return ok; | |
2730 | } | |
2731 | ||
e440a328 | 2732 | // Return the type of the const reference. |
2733 | ||
2734 | Type* | |
2735 | Const_expression::do_type() | |
2736 | { | |
2737 | if (this->type_ != NULL) | |
2738 | return this->type_; | |
13e818f5 | 2739 | |
2f78f012 | 2740 | Named_constant* nc = this->constant_->const_value(); |
2741 | ||
2742 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2743 | { |
2744 | this->report_error(_("constant refers to itself")); | |
2745 | this->type_ = Type::make_error_type(); | |
2746 | return this->type_; | |
2747 | } | |
2748 | ||
2749 | this->seen_ = true; | |
2750 | ||
e440a328 | 2751 | Type* ret = nc->type(); |
13e818f5 | 2752 | |
e440a328 | 2753 | if (ret != NULL) |
13e818f5 | 2754 | { |
2755 | this->seen_ = false; | |
2756 | return ret; | |
2757 | } | |
2758 | ||
e440a328 | 2759 | // During parsing, a named constant may have a NULL type, but we |
2760 | // must not return a NULL type here. | |
13e818f5 | 2761 | ret = nc->expr()->type(); |
2762 | ||
2763 | this->seen_ = false; | |
2764 | ||
2765 | return ret; | |
e440a328 | 2766 | } |
2767 | ||
2768 | // Set the type of the const reference. | |
2769 | ||
2770 | void | |
2771 | Const_expression::do_determine_type(const Type_context* context) | |
2772 | { | |
2773 | Type* ctype = this->constant_->const_value()->type(); | |
2774 | Type* cetype = (ctype != NULL | |
2775 | ? ctype | |
2776 | : this->constant_->const_value()->expr()->type()); | |
2777 | if (ctype != NULL && !ctype->is_abstract()) | |
2778 | ; | |
2779 | else if (context->type != NULL | |
0c77715b | 2780 | && context->type->is_numeric_type() |
2781 | && cetype->is_numeric_type()) | |
e440a328 | 2782 | this->type_ = context->type; |
2783 | else if (context->type != NULL | |
2784 | && context->type->is_string_type() | |
2785 | && cetype->is_string_type()) | |
2786 | this->type_ = context->type; | |
2787 | else if (context->type != NULL | |
2788 | && context->type->is_boolean_type() | |
2789 | && cetype->is_boolean_type()) | |
2790 | this->type_ = context->type; | |
2791 | else if (!context->may_be_abstract) | |
2792 | { | |
2793 | if (cetype->is_abstract()) | |
2794 | cetype = cetype->make_non_abstract_type(); | |
2795 | this->type_ = cetype; | |
2796 | } | |
2797 | } | |
2798 | ||
a7f064d5 | 2799 | // Check for a loop in which the initializer of a constant refers to |
2800 | // the constant itself. | |
e440a328 | 2801 | |
2802 | void | |
a7f064d5 | 2803 | Const_expression::check_for_init_loop() |
e440a328 | 2804 | { |
5c13bd80 | 2805 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2806 | return; |
2807 | ||
a7f064d5 | 2808 | if (this->seen_) |
2809 | { | |
2810 | this->report_error(_("constant refers to itself")); | |
2811 | this->type_ = Type::make_error_type(); | |
2812 | return; | |
2813 | } | |
2814 | ||
d5b605df | 2815 | Expression* init = this->constant_->const_value()->expr(); |
2816 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2817 | |
2818 | this->seen_ = true; | |
d5b605df | 2819 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2820 | this->seen_ = false; |
2821 | ||
d5b605df | 2822 | if (find_named_object.found()) |
2823 | { | |
5c13bd80 | 2824 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2825 | { |
2826 | this->report_error(_("constant refers to itself")); | |
2827 | this->type_ = Type::make_error_type(); | |
2828 | } | |
d5b605df | 2829 | return; |
2830 | } | |
a7f064d5 | 2831 | } |
2832 | ||
2833 | // Check types of a const reference. | |
2834 | ||
2835 | void | |
2836 | Const_expression::do_check_types(Gogo*) | |
2837 | { | |
5c13bd80 | 2838 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2839 | return; |
2840 | ||
2841 | this->check_for_init_loop(); | |
d5b605df | 2842 | |
0c77715b | 2843 | // Check that numeric constant fits in type. |
2844 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2845 | { |
0c77715b | 2846 | Numeric_constant nc; |
2847 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2848 | { |
0c77715b | 2849 | if (!nc.set_type(this->type_, true, this->location())) |
2850 | this->set_is_error(); | |
e440a328 | 2851 | } |
e440a328 | 2852 | } |
2853 | } | |
2854 | ||
ea664253 | 2855 | // Return the backend representation for a const reference. |
e440a328 | 2856 | |
ea664253 | 2857 | Bexpression* |
2858 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2859 | { |
12373dd5 | 2860 | if (this->is_error_expression() |
2861 | || (this->type_ != NULL && this->type_->is_error())) | |
2862 | { | |
2863 | go_assert(saw_errors()); | |
2864 | return context->backend()->error_expression(); | |
2865 | } | |
e440a328 | 2866 | |
2867 | // If the type has been set for this expression, but the underlying | |
2868 | // object is an abstract int or float, we try to get the abstract | |
2869 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2870 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2871 | if (this->type_ != NULL |
0c77715b | 2872 | && this->type_->is_numeric_type() |
a68492b4 | 2873 | && (this->constant_->const_value()->type() == NULL |
2874 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2875 | { |
0c77715b | 2876 | Numeric_constant nc; |
2877 | if (expr->numeric_constant_value(&nc) | |
2878 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2879 | { |
0c77715b | 2880 | Expression* e = nc.expression(this->location()); |
ea664253 | 2881 | return e->get_backend(context); |
e440a328 | 2882 | } |
e440a328 | 2883 | } |
2884 | ||
2c809f8f | 2885 | if (this->type_ != NULL) |
f2de4532 | 2886 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 2887 | return expr->get_backend(context); |
e440a328 | 2888 | } |
2889 | ||
d751bb78 | 2890 | // Dump ast representation for constant expression. |
2891 | ||
2892 | void | |
2893 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2894 | { | |
2895 | ast_dump_context->ostream() << this->constant_->name(); | |
2896 | } | |
2897 | ||
e440a328 | 2898 | // Make a reference to a constant in an expression. |
2899 | ||
2900 | Expression* | |
2901 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2902 | Location location) |
e440a328 | 2903 | { |
2904 | return new Const_expression(constant, location); | |
2905 | } | |
2906 | ||
d5b605df | 2907 | // Find a named object in an expression. |
2908 | ||
2909 | int | |
2910 | Find_named_object::expression(Expression** pexpr) | |
2911 | { | |
2912 | switch ((*pexpr)->classification()) | |
2913 | { | |
2914 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2915 | { |
2916 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2917 | if (ce->named_object() == this->no_) | |
2918 | break; | |
2919 | ||
2920 | // We need to check a constant initializer explicitly, as | |
2921 | // loops here will not be caught by the loop checking for | |
2922 | // variable initializers. | |
2923 | ce->check_for_init_loop(); | |
2924 | ||
2925 | return TRAVERSE_CONTINUE; | |
2926 | } | |
2927 | ||
d5b605df | 2928 | case Expression::EXPRESSION_VAR_REFERENCE: |
2929 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2930 | break; | |
2931 | return TRAVERSE_CONTINUE; | |
2932 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2933 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2934 | break; | |
2935 | return TRAVERSE_CONTINUE; | |
2936 | default: | |
2937 | return TRAVERSE_CONTINUE; | |
2938 | } | |
2939 | this->found_ = true; | |
2940 | return TRAVERSE_EXIT; | |
2941 | } | |
2942 | ||
e440a328 | 2943 | // The nil value. |
2944 | ||
2945 | class Nil_expression : public Expression | |
2946 | { | |
2947 | public: | |
b13c66cd | 2948 | Nil_expression(Location location) |
e440a328 | 2949 | : Expression(EXPRESSION_NIL, location) |
2950 | { } | |
2951 | ||
2952 | static Expression* | |
2953 | do_import(Import*); | |
2954 | ||
2955 | protected: | |
2956 | bool | |
2957 | do_is_constant() const | |
2958 | { return true; } | |
2959 | ||
f9ca30f9 | 2960 | bool |
2961 | do_is_immutable() const | |
2962 | { return true; } | |
2963 | ||
e440a328 | 2964 | Type* |
2965 | do_type() | |
2966 | { return Type::make_nil_type(); } | |
2967 | ||
2968 | void | |
2969 | do_determine_type(const Type_context*) | |
2970 | { } | |
2971 | ||
2972 | Expression* | |
2973 | do_copy() | |
2974 | { return this; } | |
2975 | ||
ea664253 | 2976 | Bexpression* |
2977 | do_get_backend(Translate_context* context) | |
2978 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 2979 | |
2980 | void | |
2981 | do_export(Export* exp) const | |
2982 | { exp->write_c_string("nil"); } | |
d751bb78 | 2983 | |
2984 | void | |
2985 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2986 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 2987 | }; |
2988 | ||
2989 | // Import a nil expression. | |
2990 | ||
2991 | Expression* | |
2992 | Nil_expression::do_import(Import* imp) | |
2993 | { | |
2994 | imp->require_c_string("nil"); | |
2995 | return Expression::make_nil(imp->location()); | |
2996 | } | |
2997 | ||
2998 | // Make a nil expression. | |
2999 | ||
3000 | Expression* | |
b13c66cd | 3001 | Expression::make_nil(Location location) |
e440a328 | 3002 | { |
3003 | return new Nil_expression(location); | |
3004 | } | |
3005 | ||
3006 | // The value of the predeclared constant iota. This is little more | |
3007 | // than a marker. This will be lowered to an integer in | |
3008 | // Const_expression::do_lower, which is where we know the value that | |
3009 | // it should have. | |
3010 | ||
3011 | class Iota_expression : public Parser_expression | |
3012 | { | |
3013 | public: | |
b13c66cd | 3014 | Iota_expression(Location location) |
e440a328 | 3015 | : Parser_expression(EXPRESSION_IOTA, location) |
3016 | { } | |
3017 | ||
3018 | protected: | |
3019 | Expression* | |
ceeb4318 | 3020 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 3021 | { go_unreachable(); } |
e440a328 | 3022 | |
3023 | // There should only ever be one of these. | |
3024 | Expression* | |
3025 | do_copy() | |
c3e6f413 | 3026 | { go_unreachable(); } |
d751bb78 | 3027 | |
3028 | void | |
3029 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3030 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3031 | }; |
3032 | ||
3033 | // Make an iota expression. This is only called for one case: the | |
3034 | // value of the predeclared constant iota. | |
3035 | ||
3036 | Expression* | |
3037 | Expression::make_iota() | |
3038 | { | |
b13c66cd | 3039 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3040 | return &iota_expression; |
3041 | } | |
3042 | ||
da244e59 | 3043 | // Class Type_conversion_expression. |
e440a328 | 3044 | |
3045 | // Traversal. | |
3046 | ||
3047 | int | |
3048 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3049 | { | |
3050 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3051 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3052 | return TRAVERSE_EXIT; | |
3053 | return TRAVERSE_CONTINUE; | |
3054 | } | |
3055 | ||
3056 | // Convert to a constant at lowering time. | |
3057 | ||
3058 | Expression* | |
ceeb4318 | 3059 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3060 | Statement_inserter*, int) | |
e440a328 | 3061 | { |
3062 | Type* type = this->type_; | |
3063 | Expression* val = this->expr_; | |
b13c66cd | 3064 | Location location = this->location(); |
e440a328 | 3065 | |
0c77715b | 3066 | if (type->is_numeric_type()) |
e440a328 | 3067 | { |
0c77715b | 3068 | Numeric_constant nc; |
3069 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3070 | { |
0c77715b | 3071 | if (!nc.set_type(type, true, location)) |
3072 | return Expression::make_error(location); | |
3073 | return nc.expression(location); | |
e440a328 | 3074 | } |
e440a328 | 3075 | } |
3076 | ||
d7739c9a | 3077 | // According to the language specification on string conversions |
3078 | // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): | |
3079 | // When converting an integer into a string, the string will be a UTF-8 | |
3080 | // representation of the integer and integers "outside the range of valid | |
3081 | // Unicode code points are converted to '\uFFFD'." | |
3082 | if (type->is_string_type()) | |
3083 | { | |
3084 | Numeric_constant nc; | |
3085 | if (val->numeric_constant_value(&nc) && nc.is_int()) | |
3086 | { | |
3087 | // An integer value doesn't fit in the Unicode code point range if it | |
3088 | // overflows the Go "int" type or is negative. | |
3089 | unsigned long ul; | |
3090 | if (!nc.set_type(Type::lookup_integer_type("int"), false, location) | |
3091 | || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) | |
3092 | return Expression::make_string("\ufffd", location); | |
3093 | } | |
3094 | } | |
3095 | ||
55072f2b | 3096 | if (type->is_slice_type()) |
e440a328 | 3097 | { |
3098 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3099 | bool is_byte = (element_type->integer_type() != NULL |
3100 | && element_type->integer_type()->is_byte()); | |
3101 | bool is_rune = (element_type->integer_type() != NULL | |
3102 | && element_type->integer_type()->is_rune()); | |
3103 | if (is_byte || is_rune) | |
e440a328 | 3104 | { |
3105 | std::string s; | |
3106 | if (val->string_constant_value(&s)) | |
3107 | { | |
3108 | Expression_list* vals = new Expression_list(); | |
3109 | if (is_byte) | |
3110 | { | |
3111 | for (std::string::const_iterator p = s.begin(); | |
3112 | p != s.end(); | |
3113 | p++) | |
3114 | { | |
e67508fa | 3115 | unsigned char c = static_cast<unsigned char>(*p); |
3116 | vals->push_back(Expression::make_integer_ul(c, | |
3117 | element_type, | |
3118 | location)); | |
e440a328 | 3119 | } |
3120 | } | |
3121 | else | |
3122 | { | |
3123 | const char *p = s.data(); | |
3124 | const char *pend = s.data() + s.length(); | |
3125 | while (p < pend) | |
3126 | { | |
3127 | unsigned int c; | |
3128 | int adv = Lex::fetch_char(p, &c); | |
3129 | if (adv == 0) | |
3130 | { | |
3131 | warning_at(this->location(), 0, | |
3132 | "invalid UTF-8 encoding"); | |
3133 | adv = 1; | |
3134 | } | |
3135 | p += adv; | |
e67508fa | 3136 | vals->push_back(Expression::make_integer_ul(c, |
3137 | element_type, | |
3138 | location)); | |
e440a328 | 3139 | } |
3140 | } | |
3141 | ||
3142 | return Expression::make_slice_composite_literal(type, vals, | |
3143 | location); | |
3144 | } | |
3145 | } | |
3146 | } | |
3147 | ||
3148 | return this; | |
3149 | } | |
3150 | ||
35a54f17 | 3151 | // Flatten a type conversion by using a temporary variable for the slice |
3152 | // in slice to string conversions. | |
3153 | ||
3154 | Expression* | |
3155 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3156 | Statement_inserter* inserter) | |
3157 | { | |
5bf8be8b | 3158 | if (this->type()->is_error_type() || this->expr_->is_error_expression()) |
3159 | { | |
3160 | go_assert(saw_errors()); | |
3161 | return Expression::make_error(this->location()); | |
3162 | } | |
3163 | ||
2c809f8f | 3164 | if (((this->type()->is_string_type() |
3165 | && this->expr_->type()->is_slice_type()) | |
8ba8cc87 | 3166 | || this->expr_->type()->interface_type() != NULL) |
35a54f17 | 3167 | && !this->expr_->is_variable()) |
3168 | { | |
3169 | Temporary_statement* temp = | |
3170 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3171 | inserter->insert(temp); | |
3172 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3173 | } | |
3174 | return this; | |
3175 | } | |
3176 | ||
1ca01a59 | 3177 | // Return whether a type conversion is a constant. |
3178 | ||
3179 | bool | |
3180 | Type_conversion_expression::do_is_constant() const | |
3181 | { | |
3182 | if (!this->expr_->is_constant()) | |
3183 | return false; | |
3184 | ||
3185 | // A conversion to a type that may not be used as a constant is not | |
3186 | // a constant. For example, []byte(nil). | |
3187 | Type* type = this->type_; | |
3188 | if (type->integer_type() == NULL | |
3189 | && type->float_type() == NULL | |
3190 | && type->complex_type() == NULL | |
3191 | && !type->is_boolean_type() | |
3192 | && !type->is_string_type()) | |
3193 | return false; | |
3194 | ||
3195 | return true; | |
3196 | } | |
3197 | ||
0e168074 | 3198 | // Return whether a type conversion is immutable. |
3199 | ||
3200 | bool | |
3201 | Type_conversion_expression::do_is_immutable() const | |
3202 | { | |
3203 | Type* type = this->type_; | |
3204 | Type* expr_type = this->expr_->type(); | |
3205 | ||
3206 | if (type->interface_type() != NULL | |
3207 | || expr_type->interface_type() != NULL) | |
3208 | return false; | |
3209 | ||
3210 | if (!this->expr_->is_immutable()) | |
3211 | return false; | |
3212 | ||
3213 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3214 | return true; | |
3215 | ||
3216 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3217 | } | |
3218 | ||
0c77715b | 3219 | // Return the constant numeric value if there is one. |
e440a328 | 3220 | |
3221 | bool | |
0c77715b | 3222 | Type_conversion_expression::do_numeric_constant_value( |
3223 | Numeric_constant* nc) const | |
e440a328 | 3224 | { |
0c77715b | 3225 | if (!this->type_->is_numeric_type()) |
e440a328 | 3226 | return false; |
0c77715b | 3227 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3228 | return false; |
0c77715b | 3229 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3230 | } |
3231 | ||
3232 | // Return the constant string value if there is one. | |
3233 | ||
3234 | bool | |
3235 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3236 | { | |
3237 | if (this->type_->is_string_type() | |
3238 | && this->expr_->type()->integer_type() != NULL) | |
3239 | { | |
0c77715b | 3240 | Numeric_constant nc; |
3241 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3242 | { |
0c77715b | 3243 | unsigned long ival; |
3244 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3245 | { |
0c77715b | 3246 | val->clear(); |
3247 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3248 | return true; |
3249 | } | |
3250 | } | |
e440a328 | 3251 | } |
3252 | ||
3253 | // FIXME: Could handle conversion from const []int here. | |
3254 | ||
3255 | return false; | |
3256 | } | |
3257 | ||
da244e59 | 3258 | // Determine the resulting type of the conversion. |
3259 | ||
3260 | void | |
3261 | Type_conversion_expression::do_determine_type(const Type_context*) | |
3262 | { | |
3263 | Type_context subcontext(this->type_, false); | |
3264 | this->expr_->determine_type(&subcontext); | |
3265 | } | |
3266 | ||
e440a328 | 3267 | // Check that types are convertible. |
3268 | ||
3269 | void | |
3270 | Type_conversion_expression::do_check_types(Gogo*) | |
3271 | { | |
3272 | Type* type = this->type_; | |
3273 | Type* expr_type = this->expr_->type(); | |
3274 | std::string reason; | |
3275 | ||
5c13bd80 | 3276 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3277 | { |
842f6425 | 3278 | this->set_is_error(); |
3279 | return; | |
3280 | } | |
3281 | ||
e440a328 | 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 | ||
ea664253 | 3294 | // Get the backend representation for a type conversion. |
e440a328 | 3295 | |
ea664253 | 3296 | Bexpression* |
3297 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3298 | { |
e440a328 | 3299 | Type* type = this->type_; |
3300 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3301 | |
3302 | Gogo* gogo = context->gogo(); | |
3303 | Btype* btype = type->get_backend(gogo); | |
ea664253 | 3304 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3305 | Location loc = this->location(); |
3306 | ||
3307 | if (Type::are_identical(type, expr_type, false, NULL)) | |
ea664253 | 3308 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
2c809f8f | 3309 | else if (type->interface_type() != NULL |
3310 | || expr_type->interface_type() != NULL) | |
e440a328 | 3311 | { |
2c809f8f | 3312 | Expression* conversion = |
3313 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3314 | this->location()); | |
ea664253 | 3315 | return conversion->get_backend(context); |
e440a328 | 3316 | } |
3317 | else if (type->is_string_type() | |
3318 | && expr_type->integer_type() != NULL) | |
3319 | { | |
2c809f8f | 3320 | mpz_t intval; |
3321 | Numeric_constant nc; | |
3322 | if (this->expr_->numeric_constant_value(&nc) | |
3323 | && nc.to_int(&intval) | |
3324 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3325 | { |
e440a328 | 3326 | std::string s; |
2c809f8f | 3327 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3328 | mpz_clear(intval); | |
3329 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3330 | return se->get_backend(context); |
e440a328 | 3331 | } |
3332 | ||
f16ab008 | 3333 | Expression* i2s_expr = |
2c809f8f | 3334 | Runtime::make_call(Runtime::INT_TO_STRING, loc, 1, this->expr_); |
ea664253 | 3335 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3336 | } |
55072f2b | 3337 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3338 | { |
55072f2b | 3339 | Array_type* a = expr_type->array_type(); |
e440a328 | 3340 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3341 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3342 | go_assert(this->expr_->is_variable()); |
3343 | ||
3344 | Runtime::Function code; | |
60963afd | 3345 | if (e->integer_type()->is_byte()) |
35a54f17 | 3346 | code = Runtime::BYTE_ARRAY_TO_STRING; |
e440a328 | 3347 | else |
35a54f17 | 3348 | { |
3349 | go_assert(e->integer_type()->is_rune()); | |
3350 | code = Runtime::INT_ARRAY_TO_STRING; | |
3351 | } | |
3352 | Expression* valptr = a->get_value_pointer(gogo, this->expr_); | |
3353 | Expression* len = a->get_length(gogo, this->expr_); | |
ea664253 | 3354 | return Runtime::make_call(code, loc, 2, valptr, |
3355 | len)->get_backend(context); | |
e440a328 | 3356 | } |
411eb89e | 3357 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3358 | { |
3359 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3360 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3361 | |
2c809f8f | 3362 | Runtime::Function code; |
60963afd | 3363 | if (e->integer_type()->is_byte()) |
2c809f8f | 3364 | code = Runtime::STRING_TO_BYTE_ARRAY; |
e440a328 | 3365 | else |
3366 | { | |
60963afd | 3367 | go_assert(e->integer_type()->is_rune()); |
2c809f8f | 3368 | code = Runtime::STRING_TO_INT_ARRAY; |
e440a328 | 3369 | } |
2c809f8f | 3370 | Expression* s2a = Runtime::make_call(code, loc, 1, this->expr_); |
ea664253 | 3371 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3372 | } |
3373 | else if (type->is_numeric_type()) | |
3374 | { | |
3375 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
ea664253 | 3376 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3377 | } |
3378 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3379 | && (expr_type->points_to() != NULL |
3380 | || expr_type->integer_type())) | |
3381 | || (expr_type->is_unsafe_pointer_type() | |
3382 | && type->points_to() != NULL) | |
3383 | || (this->may_convert_function_types_ | |
3384 | && type->function_type() != NULL | |
3385 | && expr_type->function_type() != NULL)) | |
ea664253 | 3386 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3387 | else |
2c809f8f | 3388 | { |
3389 | Expression* conversion = | |
3390 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3391 | return conversion->get_backend(context); |
2c809f8f | 3392 | } |
e440a328 | 3393 | } |
3394 | ||
3395 | // Output a type conversion in a constant expression. | |
3396 | ||
3397 | void | |
3398 | Type_conversion_expression::do_export(Export* exp) const | |
3399 | { | |
3400 | exp->write_c_string("convert("); | |
3401 | exp->write_type(this->type_); | |
3402 | exp->write_c_string(", "); | |
3403 | this->expr_->export_expression(exp); | |
3404 | exp->write_c_string(")"); | |
3405 | } | |
3406 | ||
3407 | // Import a type conversion or a struct construction. | |
3408 | ||
3409 | Expression* | |
3410 | Type_conversion_expression::do_import(Import* imp) | |
3411 | { | |
3412 | imp->require_c_string("convert("); | |
3413 | Type* type = imp->read_type(); | |
3414 | imp->require_c_string(", "); | |
3415 | Expression* val = Expression::import_expression(imp); | |
3416 | imp->require_c_string(")"); | |
3417 | return Expression::make_cast(type, val, imp->location()); | |
3418 | } | |
3419 | ||
d751bb78 | 3420 | // Dump ast representation for a type conversion expression. |
3421 | ||
3422 | void | |
3423 | Type_conversion_expression::do_dump_expression( | |
3424 | Ast_dump_context* ast_dump_context) const | |
3425 | { | |
3426 | ast_dump_context->dump_type(this->type_); | |
3427 | ast_dump_context->ostream() << "("; | |
3428 | ast_dump_context->dump_expression(this->expr_); | |
3429 | ast_dump_context->ostream() << ") "; | |
3430 | } | |
3431 | ||
e440a328 | 3432 | // Make a type cast expression. |
3433 | ||
3434 | Expression* | |
b13c66cd | 3435 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3436 | { |
3437 | if (type->is_error_type() || val->is_error_expression()) | |
3438 | return Expression::make_error(location); | |
3439 | return new Type_conversion_expression(type, val, location); | |
3440 | } | |
3441 | ||
98f62f7a | 3442 | // Class Unsafe_type_conversion_expression. |
9581e91d | 3443 | |
3444 | // Traversal. | |
3445 | ||
3446 | int | |
3447 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3448 | { | |
3449 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3450 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3451 | return TRAVERSE_EXIT; | |
3452 | return TRAVERSE_CONTINUE; | |
3453 | } | |
3454 | ||
aa5ae575 | 3455 | // Return whether an unsafe type conversion is immutable. |
3456 | ||
3457 | bool | |
3458 | Unsafe_type_conversion_expression::do_is_immutable() const | |
3459 | { | |
3460 | Type* type = this->type_; | |
3461 | Type* expr_type = this->expr_->type(); | |
3462 | ||
3463 | if (type->interface_type() != NULL | |
3464 | || expr_type->interface_type() != NULL) | |
3465 | return false; | |
3466 | ||
3467 | if (!this->expr_->is_immutable()) | |
3468 | return false; | |
3469 | ||
3470 | if (Type::are_convertible(type, expr_type, NULL)) | |
3471 | return true; | |
3472 | ||
3473 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3474 | } | |
3475 | ||
9581e91d | 3476 | // Convert to backend representation. |
3477 | ||
ea664253 | 3478 | Bexpression* |
3479 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3480 | { |
3481 | // We are only called for a limited number of cases. | |
3482 | ||
3483 | Type* t = this->type_; | |
3484 | Type* et = this->expr_->type(); | |
5c4802f1 | 3485 | |
3486 | if (t->is_error_type() | |
3487 | || this->expr_->is_error_expression() | |
3488 | || et->is_error_type()) | |
3489 | { | |
3490 | go_assert(saw_errors()); | |
3491 | return context->backend()->error_expression(); | |
3492 | } | |
3493 | ||
2c809f8f | 3494 | if (t->array_type() != NULL) |
3495 | go_assert(et->array_type() != NULL | |
3496 | && t->is_slice_type() == et->is_slice_type()); | |
3497 | else if (t->struct_type() != NULL) | |
9581e91d | 3498 | { |
2c809f8f | 3499 | if (t->named_type() != NULL |
3500 | && et->named_type() != NULL | |
3501 | && !Type::are_convertible(t, et, NULL)) | |
3502 | { | |
3503 | go_assert(saw_errors()); | |
ea664253 | 3504 | return context->backend()->error_expression(); |
2c809f8f | 3505 | } |
3506 | ||
3507 | go_assert(et->struct_type() != NULL | |
3508 | && Type::are_convertible(t, et, NULL)); | |
3509 | } | |
3510 | else if (t->map_type() != NULL) | |
c484d925 | 3511 | go_assert(et->map_type() != NULL); |
9581e91d | 3512 | else if (t->channel_type() != NULL) |
c484d925 | 3513 | go_assert(et->channel_type() != NULL); |
09ea332d | 3514 | else if (t->points_to() != NULL) |
2c809f8f | 3515 | go_assert(et->points_to() != NULL |
3516 | || et->channel_type() != NULL | |
3517 | || et->map_type() != NULL | |
3518 | || et->function_type() != NULL | |
3519 | || et->is_nil_type()); | |
9581e91d | 3520 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3521 | go_assert(t->points_to() != NULL); |
2c809f8f | 3522 | else if (t->interface_type() != NULL) |
9581e91d | 3523 | { |
2c809f8f | 3524 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3525 | go_assert(et->interface_type() != NULL |
2c809f8f | 3526 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3527 | } |
588e3cf9 | 3528 | else if (t->integer_type() != NULL) |
2c809f8f | 3529 | go_assert(et->is_boolean_type() |
3530 | || et->integer_type() != NULL | |
3531 | || et->function_type() != NULL | |
3532 | || et->points_to() != NULL | |
3533 | || et->map_type() != NULL | |
8ba8cc87 | 3534 | || et->channel_type() != NULL |
3535 | || et->is_nil_type()); | |
9581e91d | 3536 | else |
c3e6f413 | 3537 | go_unreachable(); |
9581e91d | 3538 | |
2c809f8f | 3539 | Gogo* gogo = context->gogo(); |
3540 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3541 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3542 | Location loc = this->location(); |
ea664253 | 3543 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3544 | } |
3545 | ||
d751bb78 | 3546 | // Dump ast representation for an unsafe type conversion expression. |
3547 | ||
3548 | void | |
3549 | Unsafe_type_conversion_expression::do_dump_expression( | |
3550 | Ast_dump_context* ast_dump_context) const | |
3551 | { | |
3552 | ast_dump_context->dump_type(this->type_); | |
3553 | ast_dump_context->ostream() << "("; | |
3554 | ast_dump_context->dump_expression(this->expr_); | |
3555 | ast_dump_context->ostream() << ") "; | |
3556 | } | |
3557 | ||
9581e91d | 3558 | // Make an unsafe type conversion expression. |
3559 | ||
3560 | Expression* | |
3561 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3562 | Location location) |
9581e91d | 3563 | { |
3564 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3565 | } | |
3566 | ||
76f85fd6 | 3567 | // Class Unary_expression. |
e440a328 | 3568 | |
3569 | // If we are taking the address of a composite literal, and the | |
2c809f8f | 3570 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3571 | // instead. |
3572 | ||
3573 | Expression* | |
ceeb4318 | 3574 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3575 | { |
b13c66cd | 3576 | Location loc = this->location(); |
e440a328 | 3577 | Operator op = this->op_; |
3578 | Expression* expr = this->expr_; | |
3579 | ||
3580 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3581 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3582 | ||
3583 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3584 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3585 | // analysis here? This case is found in math/unsafe.go and is | |
3586 | // therefore worth special casing. | |
3587 | if (op == OPERATOR_MULT) | |
3588 | { | |
3589 | Expression* e = expr; | |
3590 | while (e->classification() == EXPRESSION_CONVERSION) | |
3591 | { | |
3592 | Type_conversion_expression* te | |
3593 | = static_cast<Type_conversion_expression*>(e); | |
3594 | e = te->expr(); | |
3595 | } | |
3596 | ||
3597 | if (e->classification() == EXPRESSION_UNARY) | |
3598 | { | |
3599 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3600 | if (ue->op_ == OPERATOR_AND) | |
3601 | { | |
3602 | if (e == expr) | |
3603 | { | |
3604 | // *&x == x. | |
f4dea966 | 3605 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3606 | { | |
3607 | error_at(ue->location(), | |
3608 | "invalid operand for unary %<&%>"); | |
3609 | this->set_is_error(); | |
3610 | } | |
e440a328 | 3611 | return ue->expr_; |
3612 | } | |
3613 | ue->set_does_not_escape(); | |
3614 | } | |
3615 | } | |
3616 | } | |
3617 | ||
55661ce9 | 3618 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3619 | // having to deal with TYPE_VOID in other places. | |
3620 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3621 | { | |
3622 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3623 | return Expression::make_error(this->location()); | |
3624 | } | |
3625 | ||
d9f3743a | 3626 | // Check for an invalid pointer dereference. We need to do this |
3627 | // here because Unary_expression::do_type will return an error type | |
3628 | // in this case. That can cause code to appear erroneous, and | |
3629 | // therefore disappear at lowering time, without any error message. | |
3630 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3631 | { | |
3632 | this->report_error(_("expected pointer")); | |
3633 | return Expression::make_error(this->location()); | |
3634 | } | |
3635 | ||
59a401fe | 3636 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3637 | { |
0c77715b | 3638 | Numeric_constant nc; |
3639 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3640 | { |
0c77715b | 3641 | Numeric_constant result; |
3642 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3643 | return result.expression(loc); | |
e440a328 | 3644 | } |
3645 | } | |
3646 | ||
3647 | return this; | |
3648 | } | |
3649 | ||
f9ca30f9 | 3650 | // Flatten expression if a nil check must be performed and create temporary |
3651 | // variables if necessary. | |
3652 | ||
3653 | Expression* | |
3654 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3655 | Statement_inserter* inserter) | |
3656 | { | |
5bf8be8b | 3657 | if (this->is_error_expression() |
3658 | || this->expr_->is_error_expression() | |
3659 | || this->expr_->type()->is_error_type()) | |
3660 | { | |
3661 | go_assert(saw_errors()); | |
3662 | return Expression::make_error(this->location()); | |
3663 | } | |
f4dea966 | 3664 | |
f9ca30f9 | 3665 | Location location = this->location(); |
3666 | if (this->op_ == OPERATOR_MULT | |
3667 | && !this->expr_->is_variable()) | |
3668 | { | |
3669 | go_assert(this->expr_->type()->points_to() != NULL); | |
3670 | Type* ptype = this->expr_->type()->points_to(); | |
3671 | if (!ptype->is_void_type()) | |
3672 | { | |
2a305b85 | 3673 | int64_t s; |
3674 | bool ok = ptype->backend_type_size(gogo, &s); | |
3675 | if (!ok) | |
3676 | { | |
3677 | go_assert(saw_errors()); | |
3678 | return Expression::make_error(this->location()); | |
3679 | } | |
f9ca30f9 | 3680 | if (s >= 4096 || this->issue_nil_check_) |
3681 | { | |
3682 | Temporary_statement* temp = | |
3683 | Statement::make_temporary(NULL, this->expr_, location); | |
3684 | inserter->insert(temp); | |
3685 | this->expr_ = | |
3686 | Expression::make_temporary_reference(temp, location); | |
3687 | } | |
3688 | } | |
3689 | } | |
3690 | ||
da244e59 | 3691 | if (this->op_ == OPERATOR_AND) |
3692 | { | |
8ff995b5 | 3693 | // If this->escapes_ is false at this point, then it was set to |
3694 | // false by an explicit call to set_does_not_escape, and the | |
3695 | // value does not escape. If this->escapes_ is true, we may be | |
3696 | // able to set it to false if taking the address of a variable | |
3697 | // that does not escape. | |
3698 | if (this->escapes_ && this->expr_->var_expression() != NULL) | |
da244e59 | 3699 | { |
3700 | Named_object* var = this->expr_->var_expression()->named_object(); | |
3701 | if (var->is_variable()) | |
3702 | this->escapes_ = var->var_value()->escapes(); | |
3703 | if (var->is_result_variable()) | |
3704 | this->escapes_ = var->result_var_value()->escapes(); | |
3705 | } | |
3706 | this->expr_->address_taken(this->escapes_); | |
3707 | } | |
3708 | ||
f9ca30f9 | 3709 | if (this->create_temp_ && !this->expr_->is_variable()) |
3710 | { | |
3711 | Temporary_statement* temp = | |
3712 | Statement::make_temporary(NULL, this->expr_, location); | |
3713 | inserter->insert(temp); | |
3714 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3715 | } | |
3716 | ||
3717 | return this; | |
3718 | } | |
3719 | ||
e440a328 | 3720 | // Return whether a unary expression is a constant. |
3721 | ||
3722 | bool | |
3723 | Unary_expression::do_is_constant() const | |
3724 | { | |
3725 | if (this->op_ == OPERATOR_MULT) | |
3726 | { | |
3727 | // Indirecting through a pointer is only constant if the object | |
3728 | // to which the expression points is constant, but we currently | |
3729 | // have no way to determine that. | |
3730 | return false; | |
3731 | } | |
3732 | else if (this->op_ == OPERATOR_AND) | |
3733 | { | |
3734 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3735 | // global variable, not constant otherwise. In other cases taking the |
3736 | // address is probably not a constant. | |
e440a328 | 3737 | Var_expression* ve = this->expr_->var_expression(); |
3738 | if (ve != NULL) | |
3739 | { | |
3740 | Named_object* no = ve->named_object(); | |
3741 | return no->is_variable() && no->var_value()->is_global(); | |
3742 | } | |
3743 | return false; | |
3744 | } | |
3745 | else | |
3746 | return this->expr_->is_constant(); | |
3747 | } | |
3748 | ||
0c77715b | 3749 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3750 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3751 | |
3752 | bool | |
0c77715b | 3753 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3754 | Location location, Numeric_constant* nc) | |
e440a328 | 3755 | { |
3756 | switch (op) | |
3757 | { | |
3758 | case OPERATOR_PLUS: | |
0c77715b | 3759 | *nc = *unc; |
e440a328 | 3760 | return true; |
0c77715b | 3761 | |
e440a328 | 3762 | case OPERATOR_MINUS: |
0c77715b | 3763 | if (unc->is_int() || unc->is_rune()) |
3764 | break; | |
3765 | else if (unc->is_float()) | |
3766 | { | |
3767 | mpfr_t uval; | |
3768 | unc->get_float(&uval); | |
3769 | mpfr_t val; | |
3770 | mpfr_init(val); | |
3771 | mpfr_neg(val, uval, GMP_RNDN); | |
3772 | nc->set_float(unc->type(), val); | |
3773 | mpfr_clear(uval); | |
3774 | mpfr_clear(val); | |
3775 | return true; | |
3776 | } | |
3777 | else if (unc->is_complex()) | |
3778 | { | |
fcbea5e4 | 3779 | mpc_t uval; |
3780 | unc->get_complex(&uval); | |
3781 | mpc_t val; | |
3782 | mpc_init2(val, mpc_precision); | |
3783 | mpc_neg(val, uval, MPC_RNDNN); | |
3784 | nc->set_complex(unc->type(), val); | |
3785 | mpc_clear(uval); | |
3786 | mpc_clear(val); | |
0c77715b | 3787 | return true; |
3788 | } | |
e440a328 | 3789 | else |
0c77715b | 3790 | go_unreachable(); |
e440a328 | 3791 | |
0c77715b | 3792 | case OPERATOR_XOR: |
3793 | break; | |
68448d53 | 3794 | |
59a401fe | 3795 | case OPERATOR_NOT: |
e440a328 | 3796 | case OPERATOR_AND: |
3797 | case OPERATOR_MULT: | |
3798 | return false; | |
0c77715b | 3799 | |
e440a328 | 3800 | default: |
c3e6f413 | 3801 | go_unreachable(); |
e440a328 | 3802 | } |
e440a328 | 3803 | |
0c77715b | 3804 | if (!unc->is_int() && !unc->is_rune()) |
3805 | return false; | |
3806 | ||
3807 | mpz_t uval; | |
8387e1df | 3808 | if (unc->is_rune()) |
3809 | unc->get_rune(&uval); | |
3810 | else | |
3811 | unc->get_int(&uval); | |
0c77715b | 3812 | mpz_t val; |
3813 | mpz_init(val); | |
e440a328 | 3814 | |
e440a328 | 3815 | switch (op) |
3816 | { | |
e440a328 | 3817 | case OPERATOR_MINUS: |
0c77715b | 3818 | mpz_neg(val, uval); |
3819 | break; | |
3820 | ||
e440a328 | 3821 | case OPERATOR_NOT: |
0c77715b | 3822 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3823 | break; | |
3824 | ||
e440a328 | 3825 | case OPERATOR_XOR: |
0c77715b | 3826 | { |
3827 | Type* utype = unc->type(); | |
3828 | if (utype->integer_type() == NULL | |
3829 | || utype->integer_type()->is_abstract()) | |
3830 | mpz_com(val, uval); | |
3831 | else | |
3832 | { | |
3833 | // The number of HOST_WIDE_INTs that it takes to represent | |
3834 | // UVAL. | |
3835 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3836 | + HOST_BITS_PER_WIDE_INT | |
3837 | - 1) | |
3838 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3839 | |
0c77715b | 3840 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3841 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3842 | ||
3843 | size_t obits = utype->integer_type()->bits(); | |
3844 | ||
3845 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3846 | { | |
3847 | mpz_t adj; | |
3848 | mpz_init_set_ui(adj, 1); | |
3849 | mpz_mul_2exp(adj, adj, obits); | |
3850 | mpz_add(uval, uval, adj); | |
3851 | mpz_clear(adj); | |
3852 | } | |
3853 | ||
3854 | size_t ecount; | |
3855 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3856 | go_assert(ecount <= count); | |
3857 | ||
3858 | // Trim down to the number of words required by the type. | |
3859 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3860 | / HOST_BITS_PER_WIDE_INT); | |
3861 | go_assert(ocount <= count); | |
3862 | ||
3863 | for (size_t i = 0; i < ocount; ++i) | |
3864 | phwi[i] = ~phwi[i]; | |
3865 | ||
3866 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3867 | if (clearbits != 0) | |
3868 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3869 | >> clearbits); | |
3870 | ||
3871 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3872 | ||
3873 | if (!utype->integer_type()->is_unsigned() | |
3874 | && mpz_tstbit(val, obits - 1)) | |
3875 | { | |
3876 | mpz_t adj; | |
3877 | mpz_init_set_ui(adj, 1); | |
3878 | mpz_mul_2exp(adj, adj, obits); | |
3879 | mpz_sub(val, val, adj); | |
3880 | mpz_clear(adj); | |
3881 | } | |
3882 | ||
3883 | delete[] phwi; | |
3884 | } | |
3885 | } | |
3886 | break; | |
e440a328 | 3887 | |
e440a328 | 3888 | default: |
c3e6f413 | 3889 | go_unreachable(); |
e440a328 | 3890 | } |
e440a328 | 3891 | |
0c77715b | 3892 | if (unc->is_rune()) |
3893 | nc->set_rune(NULL, val); | |
e440a328 | 3894 | else |
0c77715b | 3895 | nc->set_int(NULL, val); |
e440a328 | 3896 | |
0c77715b | 3897 | mpz_clear(uval); |
3898 | mpz_clear(val); | |
e440a328 | 3899 | |
0c77715b | 3900 | return nc->set_type(unc->type(), true, location); |
e440a328 | 3901 | } |
3902 | ||
0c77715b | 3903 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 3904 | |
3905 | bool | |
0c77715b | 3906 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 3907 | { |
0c77715b | 3908 | Numeric_constant unc; |
3909 | if (!this->expr_->numeric_constant_value(&unc)) | |
3910 | return false; | |
3911 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
3912 | nc); | |
e440a328 | 3913 | } |
3914 | ||
3915 | // Return the type of a unary expression. | |
3916 | ||
3917 | Type* | |
3918 | Unary_expression::do_type() | |
3919 | { | |
3920 | switch (this->op_) | |
3921 | { | |
3922 | case OPERATOR_PLUS: | |
3923 | case OPERATOR_MINUS: | |
3924 | case OPERATOR_NOT: | |
3925 | case OPERATOR_XOR: | |
3926 | return this->expr_->type(); | |
3927 | ||
3928 | case OPERATOR_AND: | |
3929 | return Type::make_pointer_type(this->expr_->type()); | |
3930 | ||
3931 | case OPERATOR_MULT: | |
3932 | { | |
3933 | Type* subtype = this->expr_->type(); | |
3934 | Type* points_to = subtype->points_to(); | |
3935 | if (points_to == NULL) | |
3936 | return Type::make_error_type(); | |
3937 | return points_to; | |
3938 | } | |
3939 | ||
3940 | default: | |
c3e6f413 | 3941 | go_unreachable(); |
e440a328 | 3942 | } |
3943 | } | |
3944 | ||
3945 | // Determine abstract types for a unary expression. | |
3946 | ||
3947 | void | |
3948 | Unary_expression::do_determine_type(const Type_context* context) | |
3949 | { | |
3950 | switch (this->op_) | |
3951 | { | |
3952 | case OPERATOR_PLUS: | |
3953 | case OPERATOR_MINUS: | |
3954 | case OPERATOR_NOT: | |
3955 | case OPERATOR_XOR: | |
3956 | this->expr_->determine_type(context); | |
3957 | break; | |
3958 | ||
3959 | case OPERATOR_AND: | |
3960 | // Taking the address of something. | |
3961 | { | |
3962 | Type* subtype = (context->type == NULL | |
3963 | ? NULL | |
3964 | : context->type->points_to()); | |
3965 | Type_context subcontext(subtype, false); | |
3966 | this->expr_->determine_type(&subcontext); | |
3967 | } | |
3968 | break; | |
3969 | ||
3970 | case OPERATOR_MULT: | |
3971 | // Indirecting through a pointer. | |
3972 | { | |
3973 | Type* subtype = (context->type == NULL | |
3974 | ? NULL | |
3975 | : Type::make_pointer_type(context->type)); | |
3976 | Type_context subcontext(subtype, false); | |
3977 | this->expr_->determine_type(&subcontext); | |
3978 | } | |
3979 | break; | |
3980 | ||
3981 | default: | |
c3e6f413 | 3982 | go_unreachable(); |
e440a328 | 3983 | } |
3984 | } | |
3985 | ||
3986 | // Check types for a unary expression. | |
3987 | ||
3988 | void | |
3989 | Unary_expression::do_check_types(Gogo*) | |
3990 | { | |
9fe897ef | 3991 | Type* type = this->expr_->type(); |
5c13bd80 | 3992 | if (type->is_error()) |
9fe897ef | 3993 | { |
3994 | this->set_is_error(); | |
3995 | return; | |
3996 | } | |
3997 | ||
e440a328 | 3998 | switch (this->op_) |
3999 | { | |
4000 | case OPERATOR_PLUS: | |
4001 | case OPERATOR_MINUS: | |
9fe897ef | 4002 | if (type->integer_type() == NULL |
4003 | && type->float_type() == NULL | |
4004 | && type->complex_type() == NULL) | |
4005 | this->report_error(_("expected numeric type")); | |
e440a328 | 4006 | break; |
4007 | ||
4008 | case OPERATOR_NOT: | |
59a401fe | 4009 | if (!type->is_boolean_type()) |
4010 | this->report_error(_("expected boolean type")); | |
4011 | break; | |
4012 | ||
e440a328 | 4013 | case OPERATOR_XOR: |
b3b1474e | 4014 | if (type->integer_type() == NULL) |
4015 | this->report_error(_("expected integer")); | |
e440a328 | 4016 | break; |
4017 | ||
4018 | case OPERATOR_AND: | |
4019 | if (!this->expr_->is_addressable()) | |
09ea332d | 4020 | { |
4021 | if (!this->create_temp_) | |
f4dea966 | 4022 | { |
4023 | error_at(this->location(), "invalid operand for unary %<&%>"); | |
4024 | this->set_is_error(); | |
4025 | } | |
09ea332d | 4026 | } |
e440a328 | 4027 | else |
da244e59 | 4028 | this->expr_->issue_nil_check(); |
e440a328 | 4029 | break; |
4030 | ||
4031 | case OPERATOR_MULT: | |
4032 | // Indirecting through a pointer. | |
9fe897ef | 4033 | if (type->points_to() == NULL) |
4034 | this->report_error(_("expected pointer")); | |
7661d702 | 4035 | if (type->points_to()->is_error()) |
4036 | this->set_is_error(); | |
e440a328 | 4037 | break; |
4038 | ||
4039 | default: | |
c3e6f413 | 4040 | go_unreachable(); |
e440a328 | 4041 | } |
4042 | } | |
4043 | ||
ea664253 | 4044 | // Get the backend representation for a unary expression. |
e440a328 | 4045 | |
ea664253 | 4046 | Bexpression* |
4047 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4048 | { |
1b1f2abf | 4049 | Gogo* gogo = context->gogo(); |
e9d3367e | 4050 | Location loc = this->location(); |
4051 | ||
4052 | // Taking the address of a set-and-use-temporary expression requires | |
4053 | // setting the temporary and then taking the address. | |
4054 | if (this->op_ == OPERATOR_AND) | |
4055 | { | |
4056 | Set_and_use_temporary_expression* sut = | |
4057 | this->expr_->set_and_use_temporary_expression(); | |
4058 | if (sut != NULL) | |
4059 | { | |
4060 | Temporary_statement* temp = sut->temporary(); | |
4061 | Bvariable* bvar = temp->get_backend_variable(context); | |
f9ca30f9 | 4062 | Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4063 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4064 | |
4065 | Bstatement* bassign = | |
4066 | gogo->backend()->assignment_statement(bvar_expr, bval, loc); | |
4067 | Bexpression* bvar_addr = | |
4068 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4069 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4070 | } |
4071 | } | |
4072 | ||
f9ca30f9 | 4073 | Bexpression* ret; |
ea664253 | 4074 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4075 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4076 | switch (this->op_) |
4077 | { | |
4078 | case OPERATOR_PLUS: | |
f9ca30f9 | 4079 | ret = bexpr; |
4080 | break; | |
e440a328 | 4081 | |
4082 | case OPERATOR_MINUS: | |
f9ca30f9 | 4083 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4084 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4085 | break; | |
e440a328 | 4086 | |
4087 | case OPERATOR_NOT: | |
e440a328 | 4088 | case OPERATOR_XOR: |
f9ca30f9 | 4089 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4090 | break; | |
e440a328 | 4091 | |
4092 | case OPERATOR_AND: | |
09ea332d | 4093 | if (!this->create_temp_) |
4094 | { | |
4095 | // We should not see a non-constant constructor here; cases | |
4096 | // where we would see one should have been moved onto the | |
4097 | // heap at parse time. Taking the address of a nonconstant | |
4098 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4099 | |
4100 | go_assert(!this->expr_->is_composite_literal() | |
4101 | || this->expr_->is_immutable()); | |
24060bf9 | 4102 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4103 | { | |
4104 | Unary_expression* ue = | |
4105 | static_cast<Unary_expression*>(this->expr_); | |
4106 | go_assert(ue->op() != OPERATOR_AND); | |
4107 | } | |
09ea332d | 4108 | } |
e440a328 | 4109 | |
f23d7786 | 4110 | static unsigned int counter; |
4111 | char buf[100]; | |
4112 | if (this->is_gc_root_ || this->is_slice_init_) | |
76f85fd6 | 4113 | { |
f23d7786 | 4114 | bool copy_to_heap = false; |
4115 | if (this->is_gc_root_) | |
4116 | { | |
4117 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4118 | // they cannot be represented as an immutable_struct in the | |
4119 | // backend. | |
4120 | static unsigned int root_counter; | |
4121 | snprintf(buf, sizeof buf, "gc%u", root_counter); | |
4122 | ++root_counter; | |
4123 | } | |
4124 | else | |
4125 | { | |
4126 | // Build a decl for a slice value initializer. An immutable slice | |
4127 | // value initializer may have to be copied to the heap if it | |
4128 | // contains pointers in a non-constant context. | |
4129 | snprintf(buf, sizeof buf, "C%u", counter); | |
4130 | ++counter; | |
4131 | ||
4132 | Array_type* at = this->expr_->type()->array_type(); | |
4133 | go_assert(at != NULL); | |
4134 | ||
4135 | // If we are not copying the value to the heap, we will only | |
4136 | // initialize the value once, so we can use this directly | |
4137 | // rather than copying it. In that case we can't make it | |
4138 | // read-only, because the program is permitted to change it. | |
4139 | copy_to_heap = (at->element_type()->has_pointer() | |
4140 | && !context->is_const()); | |
4141 | } | |
4142 | Bvariable* implicit = | |
aa5ae575 | 4143 | gogo->backend()->implicit_variable(buf, btype, true, copy_to_heap, |
5892f89f | 4144 | false, 0); |
aa5ae575 | 4145 | gogo->backend()->implicit_variable_set_init(implicit, buf, btype, |
4146 | true, copy_to_heap, false, | |
4147 | bexpr); | |
f23d7786 | 4148 | bexpr = gogo->backend()->var_expression(implicit, loc); |
76f85fd6 | 4149 | } |
4150 | else if ((this->expr_->is_composite_literal() | |
f9ca30f9 | 4151 | || this->expr_->string_expression() != NULL) |
4152 | && this->expr_->is_immutable()) | |
4153 | { | |
76f85fd6 | 4154 | // Build a decl for a constant constructor. |
f9ca30f9 | 4155 | snprintf(buf, sizeof buf, "C%u", counter); |
4156 | ++counter; | |
4157 | ||
4158 | Bvariable* decl = | |
4159 | gogo->backend()->immutable_struct(buf, true, false, btype, loc); | |
4160 | gogo->backend()->immutable_struct_set_init(decl, buf, true, false, | |
4161 | btype, loc, bexpr); | |
4162 | bexpr = gogo->backend()->var_expression(decl, loc); | |
4163 | } | |
09ea332d | 4164 | |
f9ca30f9 | 4165 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4166 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4167 | break; | |
e440a328 | 4168 | |
4169 | case OPERATOR_MULT: | |
4170 | { | |
f9ca30f9 | 4171 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4172 | |
4173 | // If we are dereferencing the pointer to a large struct, we | |
4174 | // need to check for nil. We don't bother to check for small | |
4175 | // structs because we expect the system to crash on a nil | |
56080003 | 4176 | // pointer dereference. However, if we know the address of this |
4177 | // expression is being taken, we must always check for nil. | |
f9ca30f9 | 4178 | |
4179 | Type* ptype = this->expr_->type()->points_to(); | |
4180 | Btype* pbtype = ptype->get_backend(gogo); | |
4181 | if (!ptype->is_void_type()) | |
e440a328 | 4182 | { |
2a305b85 | 4183 | int64_t s; |
4184 | bool ok = ptype->backend_type_size(gogo, &s); | |
4185 | if (!ok) | |
4186 | { | |
4187 | go_assert(saw_errors()); | |
4188 | return gogo->backend()->error_expression(); | |
4189 | } | |
f9ca30f9 | 4190 | if (s >= 4096 || this->issue_nil_check_) |
19b4f09b | 4191 | { |
f9ca30f9 | 4192 | go_assert(this->expr_->is_variable()); |
ea664253 | 4193 | Bexpression* nil = |
4194 | Expression::make_nil(loc)->get_backend(context); | |
f9ca30f9 | 4195 | Bexpression* compare = |
4196 | gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr, | |
4197 | nil, loc); | |
f9ca30f9 | 4198 | Bexpression* crash = |
ea664253 | 4199 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4200 | loc)->get_backend(context); | |
f9ca30f9 | 4201 | bexpr = gogo->backend()->conditional_expression(btype, compare, |
4202 | crash, bexpr, | |
4203 | loc); | |
4204 | ||
19b4f09b | 4205 | } |
e440a328 | 4206 | } |
9b27b43c | 4207 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); |
e440a328 | 4208 | } |
f9ca30f9 | 4209 | break; |
e440a328 | 4210 | |
4211 | default: | |
c3e6f413 | 4212 | go_unreachable(); |
e440a328 | 4213 | } |
f9ca30f9 | 4214 | |
ea664253 | 4215 | return ret; |
e440a328 | 4216 | } |
4217 | ||
4218 | // Export a unary expression. | |
4219 | ||
4220 | void | |
4221 | Unary_expression::do_export(Export* exp) const | |
4222 | { | |
4223 | switch (this->op_) | |
4224 | { | |
4225 | case OPERATOR_PLUS: | |
4226 | exp->write_c_string("+ "); | |
4227 | break; | |
4228 | case OPERATOR_MINUS: | |
4229 | exp->write_c_string("- "); | |
4230 | break; | |
4231 | case OPERATOR_NOT: | |
4232 | exp->write_c_string("! "); | |
4233 | break; | |
4234 | case OPERATOR_XOR: | |
4235 | exp->write_c_string("^ "); | |
4236 | break; | |
4237 | case OPERATOR_AND: | |
4238 | case OPERATOR_MULT: | |
4239 | default: | |
c3e6f413 | 4240 | go_unreachable(); |
e440a328 | 4241 | } |
4242 | this->expr_->export_expression(exp); | |
4243 | } | |
4244 | ||
4245 | // Import a unary expression. | |
4246 | ||
4247 | Expression* | |
4248 | Unary_expression::do_import(Import* imp) | |
4249 | { | |
4250 | Operator op; | |
4251 | switch (imp->get_char()) | |
4252 | { | |
4253 | case '+': | |
4254 | op = OPERATOR_PLUS; | |
4255 | break; | |
4256 | case '-': | |
4257 | op = OPERATOR_MINUS; | |
4258 | break; | |
4259 | case '!': | |
4260 | op = OPERATOR_NOT; | |
4261 | break; | |
4262 | case '^': | |
4263 | op = OPERATOR_XOR; | |
4264 | break; | |
4265 | default: | |
c3e6f413 | 4266 | go_unreachable(); |
e440a328 | 4267 | } |
4268 | imp->require_c_string(" "); | |
4269 | Expression* expr = Expression::import_expression(imp); | |
4270 | return Expression::make_unary(op, expr, imp->location()); | |
4271 | } | |
4272 | ||
d751bb78 | 4273 | // Dump ast representation of an unary expression. |
4274 | ||
4275 | void | |
4276 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4277 | { | |
4278 | ast_dump_context->dump_operator(this->op_); | |
4279 | ast_dump_context->ostream() << "("; | |
4280 | ast_dump_context->dump_expression(this->expr_); | |
4281 | ast_dump_context->ostream() << ") "; | |
4282 | } | |
4283 | ||
e440a328 | 4284 | // Make a unary expression. |
4285 | ||
4286 | Expression* | |
b13c66cd | 4287 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4288 | { |
4289 | return new Unary_expression(op, expr, location); | |
4290 | } | |
4291 | ||
4292 | // If this is an indirection through a pointer, return the expression | |
4293 | // being pointed through. Otherwise return this. | |
4294 | ||
4295 | Expression* | |
4296 | Expression::deref() | |
4297 | { | |
4298 | if (this->classification_ == EXPRESSION_UNARY) | |
4299 | { | |
4300 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4301 | if (ue->op() == OPERATOR_MULT) | |
4302 | return ue->operand(); | |
4303 | } | |
4304 | return this; | |
4305 | } | |
4306 | ||
4307 | // Class Binary_expression. | |
4308 | ||
4309 | // Traversal. | |
4310 | ||
4311 | int | |
4312 | Binary_expression::do_traverse(Traverse* traverse) | |
4313 | { | |
4314 | int t = Expression::traverse(&this->left_, traverse); | |
4315 | if (t == TRAVERSE_EXIT) | |
4316 | return TRAVERSE_EXIT; | |
4317 | return Expression::traverse(&this->right_, traverse); | |
4318 | } | |
4319 | ||
0c77715b | 4320 | // Return the type to use for a binary operation on operands of |
4321 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4322 | // such may be NULL or abstract. | |
4323 | ||
4324 | bool | |
4325 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4326 | Type* right_type, Type** result_type) | |
4327 | { | |
4328 | if (left_type != right_type | |
4329 | && !left_type->is_abstract() | |
4330 | && !right_type->is_abstract() | |
4331 | && left_type->base() != right_type->base() | |
4332 | && op != OPERATOR_LSHIFT | |
4333 | && op != OPERATOR_RSHIFT) | |
4334 | { | |
4335 | // May be a type error--let it be diagnosed elsewhere. | |
4336 | return false; | |
4337 | } | |
4338 | ||
4339 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4340 | { | |
4341 | if (left_type->integer_type() != NULL) | |
4342 | *result_type = left_type; | |
4343 | else | |
4344 | *result_type = Type::make_abstract_integer_type(); | |
4345 | } | |
4346 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4347 | *result_type = left_type; | |
4348 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4349 | *result_type = right_type; | |
4350 | else if (!left_type->is_abstract()) | |
4351 | *result_type = left_type; | |
4352 | else if (!right_type->is_abstract()) | |
4353 | *result_type = right_type; | |
4354 | else if (left_type->complex_type() != NULL) | |
4355 | *result_type = left_type; | |
4356 | else if (right_type->complex_type() != NULL) | |
4357 | *result_type = right_type; | |
4358 | else if (left_type->float_type() != NULL) | |
4359 | *result_type = left_type; | |
4360 | else if (right_type->float_type() != NULL) | |
4361 | *result_type = right_type; | |
4362 | else if (left_type->integer_type() != NULL | |
4363 | && left_type->integer_type()->is_rune()) | |
4364 | *result_type = left_type; | |
4365 | else if (right_type->integer_type() != NULL | |
4366 | && right_type->integer_type()->is_rune()) | |
4367 | *result_type = right_type; | |
4368 | else | |
4369 | *result_type = left_type; | |
4370 | ||
4371 | return true; | |
4372 | } | |
4373 | ||
4374 | // Convert an integer comparison code and an operator to a boolean | |
4375 | // value. | |
e440a328 | 4376 | |
4377 | bool | |
0c77715b | 4378 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4379 | { |
e440a328 | 4380 | switch (op) |
4381 | { | |
4382 | case OPERATOR_EQEQ: | |
0c77715b | 4383 | return cmp == 0; |
4384 | break; | |
e440a328 | 4385 | case OPERATOR_NOTEQ: |
0c77715b | 4386 | return cmp != 0; |
4387 | break; | |
e440a328 | 4388 | case OPERATOR_LT: |
0c77715b | 4389 | return cmp < 0; |
4390 | break; | |
e440a328 | 4391 | case OPERATOR_LE: |
0c77715b | 4392 | return cmp <= 0; |
e440a328 | 4393 | case OPERATOR_GT: |
0c77715b | 4394 | return cmp > 0; |
e440a328 | 4395 | case OPERATOR_GE: |
0c77715b | 4396 | return cmp >= 0; |
e440a328 | 4397 | default: |
c3e6f413 | 4398 | go_unreachable(); |
e440a328 | 4399 | } |
4400 | } | |
4401 | ||
0c77715b | 4402 | // Compare constants according to OP. |
e440a328 | 4403 | |
4404 | bool | |
0c77715b | 4405 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4406 | Numeric_constant* right_nc, | |
4407 | Location location, bool* result) | |
e440a328 | 4408 | { |
0c77715b | 4409 | Type* left_type = left_nc->type(); |
4410 | Type* right_type = right_nc->type(); | |
4411 | ||
4412 | Type* type; | |
4413 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4414 | return false; | |
4415 | ||
4416 | // When comparing an untyped operand to a typed operand, we are | |
4417 | // effectively coercing the untyped operand to the other operand's | |
4418 | // type, so make sure that is valid. | |
4419 | if (!left_nc->set_type(type, true, location) | |
4420 | || !right_nc->set_type(type, true, location)) | |
4421 | return false; | |
4422 | ||
4423 | bool ret; | |
4424 | int cmp; | |
4425 | if (type->complex_type() != NULL) | |
4426 | { | |
4427 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4428 | return false; | |
4429 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4430 | } | |
4431 | else if (type->float_type() != NULL) | |
4432 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4433 | else |
0c77715b | 4434 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4435 | ||
4436 | if (ret) | |
4437 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4438 | ||
4439 | return ret; | |
4440 | } | |
4441 | ||
4442 | // Compare integer constants. | |
4443 | ||
4444 | bool | |
4445 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4446 | const Numeric_constant* right_nc, | |
4447 | int* cmp) | |
4448 | { | |
4449 | mpz_t left_val; | |
4450 | if (!left_nc->to_int(&left_val)) | |
4451 | return false; | |
4452 | mpz_t right_val; | |
4453 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4454 | { |
0c77715b | 4455 | mpz_clear(left_val); |
4456 | return false; | |
e440a328 | 4457 | } |
0c77715b | 4458 | |
4459 | *cmp = mpz_cmp(left_val, right_val); | |
4460 | ||
4461 | mpz_clear(left_val); | |
4462 | mpz_clear(right_val); | |
4463 | ||
4464 | return true; | |
4465 | } | |
4466 | ||
4467 | // Compare floating point constants. | |
4468 | ||
4469 | bool | |
4470 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4471 | const Numeric_constant* right_nc, | |
4472 | int* cmp) | |
4473 | { | |
4474 | mpfr_t left_val; | |
4475 | if (!left_nc->to_float(&left_val)) | |
4476 | return false; | |
4477 | mpfr_t right_val; | |
4478 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4479 | { |
0c77715b | 4480 | mpfr_clear(left_val); |
4481 | return false; | |
4482 | } | |
4483 | ||
4484 | // We already coerced both operands to the same type. If that type | |
4485 | // is not an abstract type, we need to round the values accordingly. | |
4486 | Type* type = left_nc->type(); | |
4487 | if (!type->is_abstract() && type->float_type() != NULL) | |
4488 | { | |
4489 | int bits = type->float_type()->bits(); | |
4490 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4491 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4492 | } |
0c77715b | 4493 | |
4494 | *cmp = mpfr_cmp(left_val, right_val); | |
4495 | ||
4496 | mpfr_clear(left_val); | |
4497 | mpfr_clear(right_val); | |
4498 | ||
4499 | return true; | |
e440a328 | 4500 | } |
4501 | ||
0c77715b | 4502 | // Compare complex constants. Complex numbers may only be compared |
4503 | // for equality. | |
e440a328 | 4504 | |
4505 | bool | |
0c77715b | 4506 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4507 | const Numeric_constant* right_nc, | |
4508 | int* cmp) | |
e440a328 | 4509 | { |
fcbea5e4 | 4510 | mpc_t left_val; |
4511 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4512 | return false; |
fcbea5e4 | 4513 | mpc_t right_val; |
4514 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4515 | { |
fcbea5e4 | 4516 | mpc_clear(left_val); |
0c77715b | 4517 | return false; |
e440a328 | 4518 | } |
0c77715b | 4519 | |
4520 | // We already coerced both operands to the same type. If that type | |
4521 | // is not an abstract type, we need to round the values accordingly. | |
4522 | Type* type = left_nc->type(); | |
4523 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4524 | { |
0c77715b | 4525 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4526 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4527 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4528 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4529 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4530 | } |
0c77715b | 4531 | |
fcbea5e4 | 4532 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4533 | |
fcbea5e4 | 4534 | mpc_clear(left_val); |
4535 | mpc_clear(right_val); | |
0c77715b | 4536 | |
4537 | return true; | |
e440a328 | 4538 | } |
4539 | ||
0c77715b | 4540 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4541 | // true if this could be done, false if not. Issue errors at LOCATION | |
4542 | // as appropriate. | |
e440a328 | 4543 | |
4544 | bool | |
0c77715b | 4545 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4546 | Numeric_constant* right_nc, | |
4547 | Location location, Numeric_constant* nc) | |
e440a328 | 4548 | { |
e440a328 | 4549 | switch (op) |
4550 | { | |
4551 | case OPERATOR_OROR: | |
4552 | case OPERATOR_ANDAND: | |
4553 | case OPERATOR_EQEQ: | |
4554 | case OPERATOR_NOTEQ: | |
4555 | case OPERATOR_LT: | |
4556 | case OPERATOR_LE: | |
4557 | case OPERATOR_GT: | |
4558 | case OPERATOR_GE: | |
9767e2d3 | 4559 | // These return boolean values, not numeric. |
4560 | return false; | |
0c77715b | 4561 | default: |
4562 | break; | |
4563 | } | |
4564 | ||
4565 | Type* left_type = left_nc->type(); | |
4566 | Type* right_type = right_nc->type(); | |
4567 | ||
4568 | Type* type; | |
4569 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4570 | return false; | |
4571 | ||
4572 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4573 | ||
4574 | // When combining an untyped operand with a typed operand, we are | |
4575 | // effectively coercing the untyped operand to the other operand's | |
4576 | // type, so make sure that is valid. | |
4577 | if (!left_nc->set_type(type, true, location)) | |
4578 | return false; | |
4579 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4580 | return false; | |
85334a21 | 4581 | if (is_shift |
4582 | && ((left_type->integer_type() == NULL | |
4583 | && !left_type->is_abstract()) | |
4584 | || (right_type->integer_type() == NULL | |
4585 | && !right_type->is_abstract()))) | |
4586 | return false; | |
0c77715b | 4587 | |
4588 | bool r; | |
4589 | if (type->complex_type() != NULL) | |
4590 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4591 | else if (type->float_type() != NULL) | |
4592 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4593 | else | |
4594 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4595 | ||
4596 | if (r) | |
4597 | r = nc->set_type(type, true, location); | |
4598 | ||
4599 | return r; | |
4600 | } | |
4601 | ||
4602 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4603 | // integer operations. Return true if this could be done, false if | |
4604 | // not. | |
4605 | ||
4606 | bool | |
4607 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4608 | const Numeric_constant* right_nc, | |
4609 | Location location, Numeric_constant* nc) | |
4610 | { | |
4611 | mpz_t left_val; | |
4612 | if (!left_nc->to_int(&left_val)) | |
4613 | return false; | |
4614 | mpz_t right_val; | |
4615 | if (!right_nc->to_int(&right_val)) | |
4616 | { | |
4617 | mpz_clear(left_val); | |
e440a328 | 4618 | return false; |
0c77715b | 4619 | } |
4620 | ||
4621 | mpz_t val; | |
4622 | mpz_init(val); | |
4623 | ||
4624 | switch (op) | |
4625 | { | |
e440a328 | 4626 | case OPERATOR_PLUS: |
4627 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4628 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4629 | { | |
4630 | error_at(location, "constant addition overflow"); | |
71a45216 | 4631 | nc->set_invalid(); |
2c809f8f | 4632 | mpz_set_ui(val, 1); |
4633 | } | |
e440a328 | 4634 | break; |
4635 | case OPERATOR_MINUS: | |
4636 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4637 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4638 | { | |
4639 | error_at(location, "constant subtraction overflow"); | |
71a45216 | 4640 | nc->set_invalid(); |
2c809f8f | 4641 | mpz_set_ui(val, 1); |
4642 | } | |
e440a328 | 4643 | break; |
4644 | case OPERATOR_OR: | |
4645 | mpz_ior(val, left_val, right_val); | |
4646 | break; | |
4647 | case OPERATOR_XOR: | |
4648 | mpz_xor(val, left_val, right_val); | |
4649 | break; | |
4650 | case OPERATOR_MULT: | |
4651 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4652 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4653 | { | |
4654 | error_at(location, "constant multiplication overflow"); | |
71a45216 | 4655 | nc->set_invalid(); |
2c809f8f | 4656 | mpz_set_ui(val, 1); |
4657 | } | |
e440a328 | 4658 | break; |
4659 | case OPERATOR_DIV: | |
4660 | if (mpz_sgn(right_val) != 0) | |
4661 | mpz_tdiv_q(val, left_val, right_val); | |
4662 | else | |
4663 | { | |
4664 | error_at(location, "division by zero"); | |
71a45216 | 4665 | nc->set_invalid(); |
e440a328 | 4666 | mpz_set_ui(val, 0); |
e440a328 | 4667 | } |
4668 | break; | |
4669 | case OPERATOR_MOD: | |
4670 | if (mpz_sgn(right_val) != 0) | |
4671 | mpz_tdiv_r(val, left_val, right_val); | |
4672 | else | |
4673 | { | |
4674 | error_at(location, "division by zero"); | |
71a45216 | 4675 | nc->set_invalid(); |
e440a328 | 4676 | mpz_set_ui(val, 0); |
e440a328 | 4677 | } |
4678 | break; | |
4679 | case OPERATOR_LSHIFT: | |
4680 | { | |
4681 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4682 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4683 | mpz_mul_2exp(val, left_val, shift); | |
4684 | else | |
e440a328 | 4685 | { |
4686 | error_at(location, "shift count overflow"); | |
71a45216 | 4687 | nc->set_invalid(); |
2c809f8f | 4688 | mpz_set_ui(val, 1); |
e440a328 | 4689 | } |
e440a328 | 4690 | break; |
4691 | } | |
4692 | break; | |
4693 | case OPERATOR_RSHIFT: | |
4694 | { | |
4695 | unsigned long shift = mpz_get_ui(right_val); | |
4696 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4697 | { | |
4698 | error_at(location, "shift count overflow"); | |
71a45216 | 4699 | nc->set_invalid(); |
2c809f8f | 4700 | mpz_set_ui(val, 1); |
e440a328 | 4701 | } |
e440a328 | 4702 | else |
0c77715b | 4703 | { |
4704 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4705 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4706 | else | |
4707 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4708 | } | |
e440a328 | 4709 | break; |
4710 | } | |
4711 | break; | |
4712 | case OPERATOR_AND: | |
4713 | mpz_and(val, left_val, right_val); | |
4714 | break; | |
4715 | case OPERATOR_BITCLEAR: | |
4716 | { | |
4717 | mpz_t tval; | |
4718 | mpz_init(tval); | |
4719 | mpz_com(tval, right_val); | |
4720 | mpz_and(val, left_val, tval); | |
4721 | mpz_clear(tval); | |
4722 | } | |
4723 | break; | |
4724 | default: | |
c3e6f413 | 4725 | go_unreachable(); |
e440a328 | 4726 | } |
4727 | ||
0c77715b | 4728 | mpz_clear(left_val); |
4729 | mpz_clear(right_val); | |
e440a328 | 4730 | |
0c77715b | 4731 | if (left_nc->is_rune() |
4732 | || (op != OPERATOR_LSHIFT | |
4733 | && op != OPERATOR_RSHIFT | |
4734 | && right_nc->is_rune())) | |
4735 | nc->set_rune(NULL, val); | |
4736 | else | |
4737 | nc->set_int(NULL, val); | |
4738 | ||
4739 | mpz_clear(val); | |
e440a328 | 4740 | |
4741 | return true; | |
4742 | } | |
4743 | ||
0c77715b | 4744 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4745 | // floating point operations. Return true if this could be done, | |
4746 | // false if not. | |
e440a328 | 4747 | |
4748 | bool | |
0c77715b | 4749 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4750 | const Numeric_constant* right_nc, | |
4751 | Location location, Numeric_constant* nc) | |
e440a328 | 4752 | { |
0c77715b | 4753 | mpfr_t left_val; |
4754 | if (!left_nc->to_float(&left_val)) | |
4755 | return false; | |
4756 | mpfr_t right_val; | |
4757 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4758 | { |
0c77715b | 4759 | mpfr_clear(left_val); |
e440a328 | 4760 | return false; |
0c77715b | 4761 | } |
4762 | ||
4763 | mpfr_t val; | |
4764 | mpfr_init(val); | |
4765 | ||
4766 | bool ret = true; | |
4767 | switch (op) | |
4768 | { | |
e440a328 | 4769 | case OPERATOR_PLUS: |
4770 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4771 | break; | |
4772 | case OPERATOR_MINUS: | |
4773 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4774 | break; | |
4775 | case OPERATOR_OR: | |
4776 | case OPERATOR_XOR: | |
4777 | case OPERATOR_AND: | |
4778 | case OPERATOR_BITCLEAR: | |
0c77715b | 4779 | case OPERATOR_MOD: |
4780 | case OPERATOR_LSHIFT: | |
4781 | case OPERATOR_RSHIFT: | |
4782 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4783 | ret = false; | |
4784 | break; | |
e440a328 | 4785 | case OPERATOR_MULT: |
4786 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4787 | break; | |
4788 | case OPERATOR_DIV: | |
0c77715b | 4789 | if (!mpfr_zero_p(right_val)) |
4790 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4791 | else | |
4792 | { | |
4793 | error_at(location, "division by zero"); | |
71a45216 | 4794 | nc->set_invalid(); |
0c77715b | 4795 | mpfr_set_ui(val, 0, GMP_RNDN); |
4796 | } | |
e440a328 | 4797 | break; |
e440a328 | 4798 | default: |
c3e6f413 | 4799 | go_unreachable(); |
e440a328 | 4800 | } |
4801 | ||
0c77715b | 4802 | mpfr_clear(left_val); |
4803 | mpfr_clear(right_val); | |
e440a328 | 4804 | |
0c77715b | 4805 | nc->set_float(NULL, val); |
4806 | mpfr_clear(val); | |
e440a328 | 4807 | |
0c77715b | 4808 | return ret; |
e440a328 | 4809 | } |
4810 | ||
0c77715b | 4811 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4812 | // complex operations. Return true if this could be done, false if | |
4813 | // not. | |
e440a328 | 4814 | |
4815 | bool | |
0c77715b | 4816 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4817 | const Numeric_constant* right_nc, | |
4818 | Location location, Numeric_constant* nc) | |
e440a328 | 4819 | { |
fcbea5e4 | 4820 | mpc_t left_val; |
4821 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4822 | return false; |
fcbea5e4 | 4823 | mpc_t right_val; |
4824 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4825 | { |
fcbea5e4 | 4826 | mpc_clear(left_val); |
e440a328 | 4827 | return false; |
0c77715b | 4828 | } |
4829 | ||
fcbea5e4 | 4830 | mpc_t val; |
4831 | mpc_init2(val, mpc_precision); | |
0c77715b | 4832 | |
4833 | bool ret = true; | |
4834 | switch (op) | |
4835 | { | |
e440a328 | 4836 | case OPERATOR_PLUS: |
fcbea5e4 | 4837 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 4838 | break; |
4839 | case OPERATOR_MINUS: | |
fcbea5e4 | 4840 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 4841 | break; |
4842 | case OPERATOR_OR: | |
4843 | case OPERATOR_XOR: | |
4844 | case OPERATOR_AND: | |
4845 | case OPERATOR_BITCLEAR: | |
0c77715b | 4846 | case OPERATOR_MOD: |
4847 | case OPERATOR_LSHIFT: | |
4848 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 4849 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 4850 | ret = false; |
4851 | break; | |
e440a328 | 4852 | case OPERATOR_MULT: |
fcbea5e4 | 4853 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 4854 | break; |
4855 | case OPERATOR_DIV: | |
fcbea5e4 | 4856 | if (mpc_cmp_si(right_val, 0) == 0) |
4857 | { | |
4858 | error_at(location, "division by zero"); | |
71a45216 | 4859 | nc->set_invalid(); |
fcbea5e4 | 4860 | mpc_set_ui(val, 0, MPC_RNDNN); |
4861 | break; | |
4862 | } | |
4863 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 4864 | break; |
e440a328 | 4865 | default: |
c3e6f413 | 4866 | go_unreachable(); |
e440a328 | 4867 | } |
4868 | ||
fcbea5e4 | 4869 | mpc_clear(left_val); |
4870 | mpc_clear(right_val); | |
e440a328 | 4871 | |
fcbea5e4 | 4872 | nc->set_complex(NULL, val); |
4873 | mpc_clear(val); | |
e440a328 | 4874 | |
0c77715b | 4875 | return ret; |
e440a328 | 4876 | } |
4877 | ||
4878 | // Lower a binary expression. We have to evaluate constant | |
4879 | // expressions now, in order to implement Go's unlimited precision | |
4880 | // constants. | |
4881 | ||
4882 | Expression* | |
e9d3367e | 4883 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
4884 | Statement_inserter* inserter, int) | |
e440a328 | 4885 | { |
b13c66cd | 4886 | Location location = this->location(); |
e440a328 | 4887 | Operator op = this->op_; |
4888 | Expression* left = this->left_; | |
4889 | Expression* right = this->right_; | |
4890 | ||
4891 | const bool is_comparison = (op == OPERATOR_EQEQ | |
4892 | || op == OPERATOR_NOTEQ | |
4893 | || op == OPERATOR_LT | |
4894 | || op == OPERATOR_LE | |
4895 | || op == OPERATOR_GT | |
4896 | || op == OPERATOR_GE); | |
4897 | ||
0c77715b | 4898 | // Numeric constant expressions. |
e440a328 | 4899 | { |
0c77715b | 4900 | Numeric_constant left_nc; |
4901 | Numeric_constant right_nc; | |
4902 | if (left->numeric_constant_value(&left_nc) | |
4903 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 4904 | { |
0c77715b | 4905 | if (is_comparison) |
e440a328 | 4906 | { |
0c77715b | 4907 | bool result; |
4908 | if (!Binary_expression::compare_constant(op, &left_nc, | |
4909 | &right_nc, location, | |
4910 | &result)) | |
4911 | return this; | |
e90c9dfc | 4912 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 4913 | Expression::make_boolean(result, |
4914 | location), | |
4915 | location); | |
e440a328 | 4916 | } |
4917 | else | |
4918 | { | |
0c77715b | 4919 | Numeric_constant nc; |
4920 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
4921 | location, &nc)) | |
71a45216 | 4922 | return this; |
0c77715b | 4923 | return nc.expression(location); |
e440a328 | 4924 | } |
4925 | } | |
e440a328 | 4926 | } |
4927 | ||
4928 | // String constant expressions. | |
315fa98d | 4929 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 4930 | { |
4931 | std::string left_string; | |
4932 | std::string right_string; | |
4933 | if (left->string_constant_value(&left_string) | |
4934 | && right->string_constant_value(&right_string)) | |
315fa98d | 4935 | { |
4936 | if (op == OPERATOR_PLUS) | |
4937 | return Expression::make_string(left_string + right_string, | |
4938 | location); | |
4939 | else if (is_comparison) | |
4940 | { | |
4941 | int cmp = left_string.compare(right_string); | |
0c77715b | 4942 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 4943 | return Expression::make_boolean(r, location); |
b40dc774 | 4944 | } |
4945 | } | |
b40dc774 | 4946 | } |
4947 | ||
ceeb12d7 | 4948 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 4949 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
4950 | { | |
b79832ca | 4951 | if (left->type()->struct_type() != NULL |
4952 | && right->type()->struct_type() != NULL) | |
e9d3367e | 4953 | return this->lower_struct_comparison(gogo, inserter); |
4954 | else if (left->type()->array_type() != NULL | |
b79832ca | 4955 | && !left->type()->is_slice_type() |
4956 | && right->type()->array_type() != NULL | |
4957 | && !right->type()->is_slice_type()) | |
e9d3367e | 4958 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 4959 | else if ((left->type()->interface_type() != NULL |
4960 | && right->type()->interface_type() == NULL) | |
4961 | || (left->type()->interface_type() == NULL | |
4962 | && right->type()->interface_type() != NULL)) | |
4963 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 4964 | } |
4965 | ||
e440a328 | 4966 | return this; |
4967 | } | |
4968 | ||
e9d3367e | 4969 | // Lower a struct comparison. |
4970 | ||
4971 | Expression* | |
4972 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
4973 | Statement_inserter* inserter) | |
4974 | { | |
4975 | Struct_type* st = this->left_->type()->struct_type(); | |
4976 | Struct_type* st2 = this->right_->type()->struct_type(); | |
4977 | if (st2 == NULL) | |
4978 | return this; | |
4979 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
4980 | return this; | |
4981 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
4982 | this->right_->type(), NULL)) | |
4983 | return this; | |
4984 | ||
4985 | // See if we can compare using memcmp. As a heuristic, we use | |
4986 | // memcmp rather than field references and comparisons if there are | |
4987 | // more than two fields. | |
113ef6a5 | 4988 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 4989 | return this->lower_compare_to_memcmp(gogo, inserter); |
4990 | ||
4991 | Location loc = this->location(); | |
4992 | ||
4993 | Expression* left = this->left_; | |
4994 | Temporary_statement* left_temp = NULL; | |
4995 | if (left->var_expression() == NULL | |
4996 | && left->temporary_reference_expression() == NULL) | |
4997 | { | |
4998 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
4999 | inserter->insert(left_temp); | |
5000 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5001 | } | |
5002 | ||
5003 | Expression* right = this->right_; | |
5004 | Temporary_statement* right_temp = NULL; | |
5005 | if (right->var_expression() == NULL | |
5006 | && right->temporary_reference_expression() == NULL) | |
5007 | { | |
5008 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5009 | inserter->insert(right_temp); | |
5010 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5011 | } | |
5012 | ||
5013 | Expression* ret = Expression::make_boolean(true, loc); | |
5014 | const Struct_field_list* fields = st->fields(); | |
5015 | unsigned int field_index = 0; | |
5016 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5017 | pf != fields->end(); | |
5018 | ++pf, ++field_index) | |
5019 | { | |
f5165c05 | 5020 | if (Gogo::is_sink_name(pf->field_name())) |
5021 | continue; | |
5022 | ||
e9d3367e | 5023 | if (field_index > 0) |
5024 | { | |
5025 | if (left_temp == NULL) | |
5026 | left = left->copy(); | |
5027 | else | |
5028 | left = Expression::make_temporary_reference(left_temp, loc); | |
5029 | if (right_temp == NULL) | |
5030 | right = right->copy(); | |
5031 | else | |
5032 | right = Expression::make_temporary_reference(right_temp, loc); | |
5033 | } | |
5034 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5035 | loc); | |
5036 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5037 | loc); | |
5038 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5039 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5040 | } | |
5041 | ||
5042 | if (this->op_ == OPERATOR_NOTEQ) | |
5043 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5044 | ||
5045 | return ret; | |
5046 | } | |
5047 | ||
5048 | // Lower an array comparison. | |
5049 | ||
5050 | Expression* | |
5051 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5052 | Statement_inserter* inserter) | |
5053 | { | |
5054 | Array_type* at = this->left_->type()->array_type(); | |
5055 | Array_type* at2 = this->right_->type()->array_type(); | |
5056 | if (at2 == NULL) | |
5057 | return this; | |
5058 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5059 | return this; | |
5060 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5061 | this->right_->type(), NULL)) | |
5062 | return this; | |
5063 | ||
5064 | // Call memcmp directly if possible. This may let the middle-end | |
5065 | // optimize the call. | |
113ef6a5 | 5066 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5067 | return this->lower_compare_to_memcmp(gogo, inserter); |
5068 | ||
5069 | // Call the array comparison function. | |
5070 | Named_object* hash_fn; | |
5071 | Named_object* equal_fn; | |
5072 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5073 | &hash_fn, &equal_fn); | |
5074 | ||
5075 | Location loc = this->location(); | |
5076 | ||
5077 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5078 | ||
5079 | Expression_list* args = new Expression_list(); | |
5080 | args->push_back(this->operand_address(inserter, this->left_)); | |
5081 | args->push_back(this->operand_address(inserter, this->right_)); | |
5082 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5083 | ||
5084 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5085 | ||
5086 | if (this->op_ == OPERATOR_NOTEQ) | |
5087 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5088 | ||
5089 | return ret; | |
5090 | } | |
5091 | ||
ceeb12d7 | 5092 | // Lower an interface to value comparison. |
5093 | ||
5094 | Expression* | |
5095 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5096 | Statement_inserter* inserter) | |
5097 | { | |
5098 | Type* left_type = this->left_->type(); | |
5099 | Type* right_type = this->right_->type(); | |
5100 | Interface_type* ift; | |
5101 | if (left_type->interface_type() != NULL) | |
5102 | { | |
5103 | ift = left_type->interface_type(); | |
5104 | if (!ift->implements_interface(right_type, NULL)) | |
5105 | return this; | |
5106 | } | |
5107 | else | |
5108 | { | |
5109 | ift = right_type->interface_type(); | |
5110 | if (!ift->implements_interface(left_type, NULL)) | |
5111 | return this; | |
5112 | } | |
5113 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5114 | return this; | |
5115 | ||
5116 | Location loc = this->location(); | |
5117 | ||
5118 | if (left_type->interface_type() == NULL | |
5119 | && left_type->points_to() == NULL | |
5120 | && !this->left_->is_addressable()) | |
5121 | { | |
5122 | Temporary_statement* temp = | |
5123 | Statement::make_temporary(left_type, NULL, loc); | |
5124 | inserter->insert(temp); | |
5125 | this->left_ = | |
5126 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5127 | } | |
5128 | ||
5129 | if (right_type->interface_type() == NULL | |
5130 | && right_type->points_to() == NULL | |
5131 | && !this->right_->is_addressable()) | |
5132 | { | |
5133 | Temporary_statement* temp = | |
5134 | Statement::make_temporary(right_type, NULL, loc); | |
5135 | inserter->insert(temp); | |
5136 | this->right_ = | |
5137 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5138 | } | |
5139 | ||
5140 | return this; | |
5141 | } | |
5142 | ||
e9d3367e | 5143 | // Lower a struct or array comparison to a call to memcmp. |
5144 | ||
5145 | Expression* | |
5146 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5147 | { | |
5148 | Location loc = this->location(); | |
5149 | ||
5150 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5151 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5152 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5153 | TYPE_INFO_SIZE); | |
5154 | ||
5155 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5156 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5157 | return Expression::make_binary(this->op_, call, zero, loc); |
5158 | } | |
5159 | ||
a32698ee | 5160 | Expression* |
5c3f3470 | 5161 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5162 | Statement_inserter* inserter) |
5163 | { | |
5164 | Location loc = this->location(); | |
5bf8be8b | 5165 | if (this->left_->type()->is_error_type() |
5166 | || this->right_->type()->is_error_type() | |
5167 | || this->left_->is_error_expression() | |
5168 | || this->right_->is_error_expression()) | |
5169 | { | |
5170 | go_assert(saw_errors()); | |
5171 | return Expression::make_error(loc); | |
5172 | } | |
5173 | ||
a32698ee | 5174 | Temporary_statement* temp; |
5175 | if (this->left_->type()->is_string_type() | |
5176 | && this->op_ == OPERATOR_PLUS) | |
5177 | { | |
0108f7b4 | 5178 | if (!this->left_->is_variable() |
5179 | && !this->left_->is_constant()) | |
a32698ee | 5180 | { |
5181 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5182 | inserter->insert(temp); | |
5183 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5184 | } | |
0108f7b4 | 5185 | if (!this->right_->is_variable() |
5186 | && !this->right_->is_constant()) | |
a32698ee | 5187 | { |
5188 | temp = | |
5189 | Statement::make_temporary(this->left_->type(), this->right_, loc); | |
5190 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5191 | inserter->insert(temp); | |
5192 | } | |
5193 | } | |
5194 | ||
5195 | Type* left_type = this->left_->type(); | |
5196 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5197 | || this->op_ == OPERATOR_RSHIFT); | |
5198 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5199 | left_type->integer_type() != NULL) | |
5200 | || this->op_ == OPERATOR_MOD); | |
5201 | ||
a32698ee | 5202 | if (is_shift_op |
5c3f3470 | 5203 | || (is_idiv_op |
5204 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5205 | { |
545ab43b | 5206 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5207 | { |
5208 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5209 | inserter->insert(temp); | |
5210 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5211 | } | |
545ab43b | 5212 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5213 | { |
5214 | temp = | |
5215 | Statement::make_temporary(NULL, this->right_, loc); | |
5216 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5217 | inserter->insert(temp); | |
5218 | } | |
5219 | } | |
5220 | return this; | |
5221 | } | |
5222 | ||
5223 | ||
e9d3367e | 5224 | // Return the address of EXPR, cast to unsafe.Pointer. |
5225 | ||
5226 | Expression* | |
5227 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5228 | Expression* expr) | |
5229 | { | |
5230 | Location loc = this->location(); | |
5231 | ||
5232 | if (!expr->is_addressable()) | |
5233 | { | |
5234 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5235 | loc); | |
5236 | inserter->insert(temp); | |
5237 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5238 | } | |
5239 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5240 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5241 | Type* void_type = Type::make_void_type(); | |
5242 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5243 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5244 | } | |
5245 | ||
0c77715b | 5246 | // Return the numeric constant value, if it has one. |
e440a328 | 5247 | |
5248 | bool | |
0c77715b | 5249 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5250 | { |
0c77715b | 5251 | Numeric_constant left_nc; |
5252 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5253 | return false; | |
5254 | Numeric_constant right_nc; | |
5255 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5256 | return false; | |
9767e2d3 | 5257 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5258 | this->location(), nc); |
e440a328 | 5259 | } |
5260 | ||
5261 | // Note that the value is being discarded. | |
5262 | ||
4f2138d7 | 5263 | bool |
e440a328 | 5264 | Binary_expression::do_discarding_value() |
5265 | { | |
5266 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5267 | return this->right_->discarding_value(); |
e440a328 | 5268 | else |
4f2138d7 | 5269 | { |
5270 | this->unused_value_error(); | |
5271 | return false; | |
5272 | } | |
e440a328 | 5273 | } |
5274 | ||
5275 | // Get type. | |
5276 | ||
5277 | Type* | |
5278 | Binary_expression::do_type() | |
5279 | { | |
5f5fea79 | 5280 | if (this->classification() == EXPRESSION_ERROR) |
5281 | return Type::make_error_type(); | |
5282 | ||
e440a328 | 5283 | switch (this->op_) |
5284 | { | |
e440a328 | 5285 | case OPERATOR_EQEQ: |
5286 | case OPERATOR_NOTEQ: | |
5287 | case OPERATOR_LT: | |
5288 | case OPERATOR_LE: | |
5289 | case OPERATOR_GT: | |
5290 | case OPERATOR_GE: | |
e90c9dfc | 5291 | if (this->type_ == NULL) |
5292 | this->type_ = Type::make_boolean_type(); | |
5293 | return this->type_; | |
e440a328 | 5294 | |
5295 | case OPERATOR_PLUS: | |
5296 | case OPERATOR_MINUS: | |
5297 | case OPERATOR_OR: | |
5298 | case OPERATOR_XOR: | |
5299 | case OPERATOR_MULT: | |
5300 | case OPERATOR_DIV: | |
5301 | case OPERATOR_MOD: | |
5302 | case OPERATOR_AND: | |
5303 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5304 | case OPERATOR_OROR: |
5305 | case OPERATOR_ANDAND: | |
e440a328 | 5306 | { |
0c77715b | 5307 | Type* type; |
5308 | if (!Binary_expression::operation_type(this->op_, | |
5309 | this->left_->type(), | |
5310 | this->right_->type(), | |
5311 | &type)) | |
5312 | return Type::make_error_type(); | |
5313 | return type; | |
e440a328 | 5314 | } |
5315 | ||
5316 | case OPERATOR_LSHIFT: | |
5317 | case OPERATOR_RSHIFT: | |
5318 | return this->left_->type(); | |
5319 | ||
5320 | default: | |
c3e6f413 | 5321 | go_unreachable(); |
e440a328 | 5322 | } |
5323 | } | |
5324 | ||
5325 | // Set type for a binary expression. | |
5326 | ||
5327 | void | |
5328 | Binary_expression::do_determine_type(const Type_context* context) | |
5329 | { | |
5330 | Type* tleft = this->left_->type(); | |
5331 | Type* tright = this->right_->type(); | |
5332 | ||
5333 | // Both sides should have the same type, except for the shift | |
5334 | // operations. For a comparison, we should ignore the incoming | |
5335 | // type. | |
5336 | ||
5337 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5338 | || this->op_ == OPERATOR_RSHIFT); | |
5339 | ||
5340 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5341 | || this->op_ == OPERATOR_NOTEQ | |
5342 | || this->op_ == OPERATOR_LT | |
5343 | || this->op_ == OPERATOR_LE | |
5344 | || this->op_ == OPERATOR_GT | |
5345 | || this->op_ == OPERATOR_GE); | |
5346 | ||
c999c2a7 | 5347 | // For constant expressions, the context of the result is not useful in |
5348 | // determining the types of the operands. It is only legal to use abstract | |
5349 | // boolean, numeric, and string constants as operands where it is legal to | |
5350 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5351 | // Any issues with the operation will be resolved in the check_types pass. | |
5352 | bool is_constant_expr = (this->left_->is_constant() | |
5353 | && this->right_->is_constant()); | |
5354 | ||
e440a328 | 5355 | Type_context subcontext(*context); |
5356 | ||
5357 | if (is_comparison) | |
5358 | { | |
5359 | // In a comparison, the context does not determine the types of | |
5360 | // the operands. | |
5361 | subcontext.type = NULL; | |
5362 | } | |
5363 | ||
5364 | // Set the context for the left hand operand. | |
5365 | if (is_shift_op) | |
5366 | { | |
b40dc774 | 5367 | // The right hand operand of a shift plays no role in |
5368 | // determining the type of the left hand operand. | |
e440a328 | 5369 | } |
5370 | else if (!tleft->is_abstract()) | |
5371 | subcontext.type = tleft; | |
5372 | else if (!tright->is_abstract()) | |
5373 | subcontext.type = tright; | |
5374 | else if (subcontext.type == NULL) | |
5375 | { | |
5376 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5377 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5378 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5379 | { | |
5380 | // Both sides have an abstract integer, abstract float, or | |
5381 | // abstract complex type. Just let CONTEXT determine | |
5382 | // whether they may remain abstract or not. | |
5383 | } | |
5384 | else if (tleft->complex_type() != NULL) | |
5385 | subcontext.type = tleft; | |
5386 | else if (tright->complex_type() != NULL) | |
5387 | subcontext.type = tright; | |
5388 | else if (tleft->float_type() != NULL) | |
5389 | subcontext.type = tleft; | |
5390 | else if (tright->float_type() != NULL) | |
5391 | subcontext.type = tright; | |
5392 | else | |
5393 | subcontext.type = tleft; | |
f58a23ae | 5394 | |
5395 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5396 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5397 | } |
5398 | ||
c999c2a7 | 5399 | if (!is_constant_expr) |
5400 | this->left_->determine_type(&subcontext); | |
e440a328 | 5401 | |
e440a328 | 5402 | if (is_shift_op) |
5403 | { | |
b40dc774 | 5404 | // We may have inherited an unusable type for the shift operand. |
5405 | // Give a useful error if that happened. | |
5406 | if (tleft->is_abstract() | |
5407 | && subcontext.type != NULL | |
8ab6effb | 5408 | && !subcontext.may_be_abstract |
f6bc81e6 | 5409 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5410 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5411 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5412 | "for left operand of shift")); |
b40dc774 | 5413 | |
5414 | // The context for the right hand operand is the same as for the | |
5415 | // left hand operand, except for a shift operator. | |
e440a328 | 5416 | subcontext.type = Type::lookup_integer_type("uint"); |
5417 | subcontext.may_be_abstract = false; | |
5418 | } | |
5419 | ||
c999c2a7 | 5420 | if (!is_constant_expr) |
5421 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5422 | |
5423 | if (is_comparison) | |
5424 | { | |
5425 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5426 | ; | |
5427 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5428 | this->type_ = context->type; | |
5429 | else if (!context->may_be_abstract) | |
5430 | this->type_ = Type::lookup_bool_type(); | |
5431 | } | |
e440a328 | 5432 | } |
5433 | ||
5434 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5435 | // OTYPE is the type of the other operand. Return whether the |
5436 | // operation is OK. This should not be used for shift. | |
e440a328 | 5437 | |
5438 | bool | |
be8b5eee | 5439 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5440 | Location location) |
e440a328 | 5441 | { |
5442 | switch (op) | |
5443 | { | |
5444 | case OPERATOR_OROR: | |
5445 | case OPERATOR_ANDAND: | |
c999c2a7 | 5446 | if (!type->is_boolean_type() |
5447 | || !otype->is_boolean_type()) | |
e440a328 | 5448 | { |
5449 | error_at(location, "expected boolean type"); | |
5450 | return false; | |
5451 | } | |
5452 | break; | |
5453 | ||
5454 | case OPERATOR_EQEQ: | |
5455 | case OPERATOR_NOTEQ: | |
e9d3367e | 5456 | { |
5457 | std::string reason; | |
5458 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5459 | { | |
5460 | error_at(location, "%s", reason.c_str()); | |
5461 | return false; | |
5462 | } | |
5463 | } | |
e440a328 | 5464 | break; |
5465 | ||
5466 | case OPERATOR_LT: | |
5467 | case OPERATOR_LE: | |
5468 | case OPERATOR_GT: | |
5469 | case OPERATOR_GE: | |
e9d3367e | 5470 | { |
5471 | std::string reason; | |
5472 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5473 | { | |
5474 | error_at(location, "%s", reason.c_str()); | |
5475 | return false; | |
5476 | } | |
5477 | } | |
e440a328 | 5478 | break; |
5479 | ||
5480 | case OPERATOR_PLUS: | |
5481 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5482 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5483 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5484 | { |
5485 | error_at(location, | |
5486 | "expected integer, floating, complex, or string type"); | |
5487 | return false; | |
5488 | } | |
5489 | break; | |
5490 | ||
5491 | case OPERATOR_MINUS: | |
5492 | case OPERATOR_MINUSEQ: | |
5493 | case OPERATOR_MULT: | |
5494 | case OPERATOR_MULTEQ: | |
5495 | case OPERATOR_DIV: | |
5496 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5497 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5498 | { |
5499 | error_at(location, "expected integer, floating, or complex type"); | |
5500 | return false; | |
5501 | } | |
5502 | break; | |
5503 | ||
5504 | case OPERATOR_MOD: | |
5505 | case OPERATOR_MODEQ: | |
5506 | case OPERATOR_OR: | |
5507 | case OPERATOR_OREQ: | |
5508 | case OPERATOR_AND: | |
5509 | case OPERATOR_ANDEQ: | |
5510 | case OPERATOR_XOR: | |
5511 | case OPERATOR_XOREQ: | |
5512 | case OPERATOR_BITCLEAR: | |
5513 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5514 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5515 | { |
5516 | error_at(location, "expected integer type"); | |
5517 | return false; | |
5518 | } | |
5519 | break; | |
5520 | ||
5521 | default: | |
c3e6f413 | 5522 | go_unreachable(); |
e440a328 | 5523 | } |
5524 | ||
5525 | return true; | |
5526 | } | |
5527 | ||
5528 | // Check types. | |
5529 | ||
5530 | void | |
5531 | Binary_expression::do_check_types(Gogo*) | |
5532 | { | |
5f5fea79 | 5533 | if (this->classification() == EXPRESSION_ERROR) |
5534 | return; | |
5535 | ||
e440a328 | 5536 | Type* left_type = this->left_->type(); |
5537 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5538 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5539 | { |
5540 | this->set_is_error(); | |
5541 | return; | |
5542 | } | |
e440a328 | 5543 | |
5544 | if (this->op_ == OPERATOR_EQEQ | |
5545 | || this->op_ == OPERATOR_NOTEQ | |
5546 | || this->op_ == OPERATOR_LT | |
5547 | || this->op_ == OPERATOR_LE | |
5548 | || this->op_ == OPERATOR_GT | |
5549 | || this->op_ == OPERATOR_GE) | |
5550 | { | |
907c5ecd | 5551 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5552 | { | |
5553 | this->report_error(_("invalid comparison of nil with nil")); | |
5554 | return; | |
5555 | } | |
e440a328 | 5556 | if (!Type::are_assignable(left_type, right_type, NULL) |
5557 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5558 | { | |
5559 | this->report_error(_("incompatible types in binary expression")); | |
5560 | return; | |
5561 | } | |
5562 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5563 | right_type, |
e440a328 | 5564 | this->location()) |
5565 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5566 | left_type, |
e440a328 | 5567 | this->location())) |
5568 | { | |
5569 | this->set_is_error(); | |
5570 | return; | |
5571 | } | |
5572 | } | |
5573 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5574 | { | |
5575 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5576 | { | |
5577 | this->report_error(_("incompatible types in binary expression")); | |
5578 | return; | |
5579 | } | |
5580 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5581 | right_type, |
e440a328 | 5582 | this->location())) |
5583 | { | |
5584 | this->set_is_error(); | |
5585 | return; | |
5586 | } | |
5c65b19d | 5587 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5588 | { | |
5589 | // Division by a zero integer constant is an error. | |
5590 | Numeric_constant rconst; | |
5591 | unsigned long rval; | |
5592 | if (left_type->integer_type() != NULL | |
5593 | && this->right_->numeric_constant_value(&rconst) | |
5594 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5595 | && rval == 0) | |
5596 | { | |
5597 | this->report_error(_("integer division by zero")); | |
5598 | return; | |
5599 | } | |
5600 | } | |
e440a328 | 5601 | } |
5602 | else | |
5603 | { | |
5604 | if (left_type->integer_type() == NULL) | |
5605 | this->report_error(_("shift of non-integer operand")); | |
5606 | ||
6b5e0fac | 5607 | if (right_type->is_string_type()) |
5608 | this->report_error(_("shift count not unsigned integer")); | |
5609 | else if (!right_type->is_abstract() | |
e440a328 | 5610 | && (right_type->integer_type() == NULL |
5611 | || !right_type->integer_type()->is_unsigned())) | |
5612 | this->report_error(_("shift count not unsigned integer")); | |
5613 | else | |
5614 | { | |
0c77715b | 5615 | Numeric_constant nc; |
5616 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5617 | { |
0c77715b | 5618 | mpz_t val; |
5619 | if (!nc.to_int(&val)) | |
5620 | this->report_error(_("shift count not unsigned integer")); | |
5621 | else | |
a4eba91b | 5622 | { |
0c77715b | 5623 | if (mpz_sgn(val) < 0) |
5624 | { | |
5625 | this->report_error(_("negative shift count")); | |
0c77715b | 5626 | Location rloc = this->right_->location(); |
e67508fa | 5627 | this->right_ = Expression::make_integer_ul(0, right_type, |
5628 | rloc); | |
0c77715b | 5629 | } |
5630 | mpz_clear(val); | |
a4eba91b | 5631 | } |
e440a328 | 5632 | } |
e440a328 | 5633 | } |
5634 | } | |
5635 | } | |
5636 | ||
ea664253 | 5637 | // Get the backend representation for a binary expression. |
e440a328 | 5638 | |
ea664253 | 5639 | Bexpression* |
5640 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 5641 | { |
1b1f2abf | 5642 | Gogo* gogo = context->gogo(); |
a32698ee | 5643 | Location loc = this->location(); |
5644 | Type* left_type = this->left_->type(); | |
5645 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5646 | |
e440a328 | 5647 | bool use_left_type = true; |
5648 | bool is_shift_op = false; | |
29a2d1d8 | 5649 | bool is_idiv_op = false; |
e440a328 | 5650 | switch (this->op_) |
5651 | { | |
5652 | case OPERATOR_EQEQ: | |
5653 | case OPERATOR_NOTEQ: | |
5654 | case OPERATOR_LT: | |
5655 | case OPERATOR_LE: | |
5656 | case OPERATOR_GT: | |
5657 | case OPERATOR_GE: | |
ea664253 | 5658 | return Expression::comparison(context, this->type_, this->op_, |
5659 | this->left_, this->right_, loc); | |
e440a328 | 5660 | |
5661 | case OPERATOR_OROR: | |
e440a328 | 5662 | case OPERATOR_ANDAND: |
e440a328 | 5663 | use_left_type = false; |
5664 | break; | |
5665 | case OPERATOR_PLUS: | |
e440a328 | 5666 | case OPERATOR_MINUS: |
e440a328 | 5667 | case OPERATOR_OR: |
e440a328 | 5668 | case OPERATOR_XOR: |
e440a328 | 5669 | case OPERATOR_MULT: |
e440a328 | 5670 | break; |
5671 | case OPERATOR_DIV: | |
a32698ee | 5672 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
5673 | break; | |
e440a328 | 5674 | case OPERATOR_MOD: |
29a2d1d8 | 5675 | is_idiv_op = true; |
e440a328 | 5676 | break; |
5677 | case OPERATOR_LSHIFT: | |
e440a328 | 5678 | case OPERATOR_RSHIFT: |
e440a328 | 5679 | is_shift_op = true; |
5680 | break; | |
e440a328 | 5681 | case OPERATOR_BITCLEAR: |
a32698ee | 5682 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
5683 | case OPERATOR_AND: | |
e440a328 | 5684 | break; |
5685 | default: | |
c3e6f413 | 5686 | go_unreachable(); |
e440a328 | 5687 | } |
5688 | ||
a32698ee | 5689 | if (left_type->is_string_type()) |
e440a328 | 5690 | { |
c484d925 | 5691 | go_assert(this->op_ == OPERATOR_PLUS); |
a32698ee | 5692 | Expression* string_plus = |
5693 | Runtime::make_call(Runtime::STRING_PLUS, loc, 2, | |
5694 | this->left_, this->right_); | |
ea664253 | 5695 | return string_plus->get_backend(context); |
a32698ee | 5696 | } |
5697 | ||
5698 | // For complex division Go might want slightly different results than the | |
5699 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 5700 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
5701 | { | |
a32698ee | 5702 | Runtime::Function complex_code; |
1850e20c | 5703 | switch (this->left_->type()->complex_type()->bits()) |
5704 | { | |
5705 | case 64: | |
a32698ee | 5706 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 5707 | break; |
5708 | case 128: | |
a32698ee | 5709 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 5710 | break; |
5711 | default: | |
5712 | go_unreachable(); | |
5713 | } | |
a32698ee | 5714 | Expression* complex_div = |
5715 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 5716 | return complex_div->get_backend(context); |
1850e20c | 5717 | } |
5718 | ||
ea664253 | 5719 | Bexpression* left = this->left_->get_backend(context); |
5720 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 5721 | |
a32698ee | 5722 | Type* type = use_left_type ? left_type : right_type; |
5723 | Btype* btype = type->get_backend(gogo); | |
5724 | ||
5725 | Bexpression* ret = | |
5726 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
5727 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 5728 | |
a32698ee | 5729 | // Initialize overflow constants. |
5730 | Bexpression* overflow; | |
5731 | mpz_t zero; | |
5732 | mpz_init_set_ui(zero, 0UL); | |
5733 | mpz_t one; | |
5734 | mpz_init_set_ui(one, 1UL); | |
5735 | mpz_t neg_one; | |
5736 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 5737 | |
a32698ee | 5738 | Btype* left_btype = left_type->get_backend(gogo); |
5739 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 5740 | |
5741 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 5742 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 5743 | if (is_shift_op) |
5744 | { | |
a32698ee | 5745 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 5746 | |
a32698ee | 5747 | mpz_t bitsval; |
5748 | int bits = left_type->integer_type()->bits(); | |
5749 | mpz_init_set_ui(bitsval, bits); | |
5750 | Bexpression* bits_expr = | |
5751 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
5752 | Bexpression* compare = | |
5753 | gogo->backend()->binary_expression(OPERATOR_LT, | |
5754 | right, bits_expr, loc); | |
e440a328 | 5755 | |
a32698ee | 5756 | Bexpression* zero_expr = |
5757 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
5758 | overflow = zero_expr; | |
e440a328 | 5759 | if (this->op_ == OPERATOR_RSHIFT |
a32698ee | 5760 | && !left_type->integer_type()->is_unsigned()) |
e440a328 | 5761 | { |
a32698ee | 5762 | Bexpression* neg_expr = |
5763 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
5764 | zero_expr, loc); | |
5765 | Bexpression* neg_one_expr = | |
5766 | gogo->backend()->integer_constant_expression(left_btype, neg_one); | |
5767 | overflow = gogo->backend()->conditional_expression(btype, neg_expr, | |
5768 | neg_one_expr, | |
5769 | zero_expr, loc); | |
29a2d1d8 | 5770 | } |
a32698ee | 5771 | ret = gogo->backend()->conditional_expression(btype, compare, ret, |
5772 | overflow, loc); | |
5773 | mpz_clear(bitsval); | |
29a2d1d8 | 5774 | } |
5775 | ||
5776 | // Add checks for division by zero and division overflow as needed. | |
5777 | if (is_idiv_op) | |
5778 | { | |
5c3f3470 | 5779 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 5780 | { |
5781 | // right == 0 | |
a32698ee | 5782 | Bexpression* zero_expr = |
5783 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
5784 | Bexpression* check = | |
5785 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5786 | right, zero_expr, loc); | |
29a2d1d8 | 5787 | |
a32698ee | 5788 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 5789 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 5790 | Bexpression* crash = gogo->runtime_error(errcode, |
5791 | loc)->get_backend(context); | |
29a2d1d8 | 5792 | |
5793 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
ea664253 | 5794 | ret = gogo->backend()->conditional_expression(btype, check, crash, |
5795 | ret, loc); | |
b13c66cd | 5796 | } |
5797 | ||
5c3f3470 | 5798 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 5799 | { |
5800 | // right == -1 | |
5801 | // FIXME: It would be nice to say that this test is expected | |
5802 | // to return false. | |
a32698ee | 5803 | |
5804 | Bexpression* neg_one_expr = | |
5805 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
5806 | Bexpression* check = | |
5807 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5808 | right, neg_one_expr, loc); | |
5809 | ||
5810 | Bexpression* zero_expr = | |
5811 | gogo->backend()->integer_constant_expression(btype, zero); | |
5812 | Bexpression* one_expr = | |
5813 | gogo->backend()->integer_constant_expression(btype, one); | |
5814 | ||
5815 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 5816 | { |
5817 | // An unsigned -1 is the largest possible number, so | |
5818 | // dividing is always 1 or 0. | |
a32698ee | 5819 | |
5820 | Bexpression* cmp = | |
5821 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5822 | left, right, loc); | |
29a2d1d8 | 5823 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 5824 | overflow = |
5825 | gogo->backend()->conditional_expression(btype, cmp, | |
5826 | one_expr, zero_expr, | |
5827 | loc); | |
29a2d1d8 | 5828 | else |
a32698ee | 5829 | overflow = |
5830 | gogo->backend()->conditional_expression(btype, cmp, | |
5831 | zero_expr, left, | |
5832 | loc); | |
29a2d1d8 | 5833 | } |
5834 | else | |
5835 | { | |
5836 | // Computing left / -1 is the same as computing - left, | |
5837 | // which does not overflow since Go sets -fwrapv. | |
5838 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 5839 | { |
5840 | Expression* negate_expr = | |
5841 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 5842 | overflow = negate_expr->get_backend(context); |
a32698ee | 5843 | } |
29a2d1d8 | 5844 | else |
a32698ee | 5845 | overflow = zero_expr; |
29a2d1d8 | 5846 | } |
a32698ee | 5847 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 5848 | |
5849 | // right == -1 ? - left : ret | |
a32698ee | 5850 | ret = gogo->backend()->conditional_expression(btype, check, overflow, |
5851 | ret, loc); | |
29a2d1d8 | 5852 | } |
e440a328 | 5853 | } |
5854 | ||
a32698ee | 5855 | mpz_clear(zero); |
5856 | mpz_clear(one); | |
5857 | mpz_clear(neg_one); | |
ea664253 | 5858 | return ret; |
e440a328 | 5859 | } |
5860 | ||
5861 | // Export a binary expression. | |
5862 | ||
5863 | void | |
5864 | Binary_expression::do_export(Export* exp) const | |
5865 | { | |
5866 | exp->write_c_string("("); | |
5867 | this->left_->export_expression(exp); | |
5868 | switch (this->op_) | |
5869 | { | |
5870 | case OPERATOR_OROR: | |
5871 | exp->write_c_string(" || "); | |
5872 | break; | |
5873 | case OPERATOR_ANDAND: | |
5874 | exp->write_c_string(" && "); | |
5875 | break; | |
5876 | case OPERATOR_EQEQ: | |
5877 | exp->write_c_string(" == "); | |
5878 | break; | |
5879 | case OPERATOR_NOTEQ: | |
5880 | exp->write_c_string(" != "); | |
5881 | break; | |
5882 | case OPERATOR_LT: | |
5883 | exp->write_c_string(" < "); | |
5884 | break; | |
5885 | case OPERATOR_LE: | |
5886 | exp->write_c_string(" <= "); | |
5887 | break; | |
5888 | case OPERATOR_GT: | |
5889 | exp->write_c_string(" > "); | |
5890 | break; | |
5891 | case OPERATOR_GE: | |
5892 | exp->write_c_string(" >= "); | |
5893 | break; | |
5894 | case OPERATOR_PLUS: | |
5895 | exp->write_c_string(" + "); | |
5896 | break; | |
5897 | case OPERATOR_MINUS: | |
5898 | exp->write_c_string(" - "); | |
5899 | break; | |
5900 | case OPERATOR_OR: | |
5901 | exp->write_c_string(" | "); | |
5902 | break; | |
5903 | case OPERATOR_XOR: | |
5904 | exp->write_c_string(" ^ "); | |
5905 | break; | |
5906 | case OPERATOR_MULT: | |
5907 | exp->write_c_string(" * "); | |
5908 | break; | |
5909 | case OPERATOR_DIV: | |
5910 | exp->write_c_string(" / "); | |
5911 | break; | |
5912 | case OPERATOR_MOD: | |
5913 | exp->write_c_string(" % "); | |
5914 | break; | |
5915 | case OPERATOR_LSHIFT: | |
5916 | exp->write_c_string(" << "); | |
5917 | break; | |
5918 | case OPERATOR_RSHIFT: | |
5919 | exp->write_c_string(" >> "); | |
5920 | break; | |
5921 | case OPERATOR_AND: | |
5922 | exp->write_c_string(" & "); | |
5923 | break; | |
5924 | case OPERATOR_BITCLEAR: | |
5925 | exp->write_c_string(" &^ "); | |
5926 | break; | |
5927 | default: | |
c3e6f413 | 5928 | go_unreachable(); |
e440a328 | 5929 | } |
5930 | this->right_->export_expression(exp); | |
5931 | exp->write_c_string(")"); | |
5932 | } | |
5933 | ||
5934 | // Import a binary expression. | |
5935 | ||
5936 | Expression* | |
5937 | Binary_expression::do_import(Import* imp) | |
5938 | { | |
5939 | imp->require_c_string("("); | |
5940 | ||
5941 | Expression* left = Expression::import_expression(imp); | |
5942 | ||
5943 | Operator op; | |
5944 | if (imp->match_c_string(" || ")) | |
5945 | { | |
5946 | op = OPERATOR_OROR; | |
5947 | imp->advance(4); | |
5948 | } | |
5949 | else if (imp->match_c_string(" && ")) | |
5950 | { | |
5951 | op = OPERATOR_ANDAND; | |
5952 | imp->advance(4); | |
5953 | } | |
5954 | else if (imp->match_c_string(" == ")) | |
5955 | { | |
5956 | op = OPERATOR_EQEQ; | |
5957 | imp->advance(4); | |
5958 | } | |
5959 | else if (imp->match_c_string(" != ")) | |
5960 | { | |
5961 | op = OPERATOR_NOTEQ; | |
5962 | imp->advance(4); | |
5963 | } | |
5964 | else if (imp->match_c_string(" < ")) | |
5965 | { | |
5966 | op = OPERATOR_LT; | |
5967 | imp->advance(3); | |
5968 | } | |
5969 | else if (imp->match_c_string(" <= ")) | |
5970 | { | |
5971 | op = OPERATOR_LE; | |
5972 | imp->advance(4); | |
5973 | } | |
5974 | else if (imp->match_c_string(" > ")) | |
5975 | { | |
5976 | op = OPERATOR_GT; | |
5977 | imp->advance(3); | |
5978 | } | |
5979 | else if (imp->match_c_string(" >= ")) | |
5980 | { | |
5981 | op = OPERATOR_GE; | |
5982 | imp->advance(4); | |
5983 | } | |
5984 | else if (imp->match_c_string(" + ")) | |
5985 | { | |
5986 | op = OPERATOR_PLUS; | |
5987 | imp->advance(3); | |
5988 | } | |
5989 | else if (imp->match_c_string(" - ")) | |
5990 | { | |
5991 | op = OPERATOR_MINUS; | |
5992 | imp->advance(3); | |
5993 | } | |
5994 | else if (imp->match_c_string(" | ")) | |
5995 | { | |
5996 | op = OPERATOR_OR; | |
5997 | imp->advance(3); | |
5998 | } | |
5999 | else if (imp->match_c_string(" ^ ")) | |
6000 | { | |
6001 | op = OPERATOR_XOR; | |
6002 | imp->advance(3); | |
6003 | } | |
6004 | else if (imp->match_c_string(" * ")) | |
6005 | { | |
6006 | op = OPERATOR_MULT; | |
6007 | imp->advance(3); | |
6008 | } | |
6009 | else if (imp->match_c_string(" / ")) | |
6010 | { | |
6011 | op = OPERATOR_DIV; | |
6012 | imp->advance(3); | |
6013 | } | |
6014 | else if (imp->match_c_string(" % ")) | |
6015 | { | |
6016 | op = OPERATOR_MOD; | |
6017 | imp->advance(3); | |
6018 | } | |
6019 | else if (imp->match_c_string(" << ")) | |
6020 | { | |
6021 | op = OPERATOR_LSHIFT; | |
6022 | imp->advance(4); | |
6023 | } | |
6024 | else if (imp->match_c_string(" >> ")) | |
6025 | { | |
6026 | op = OPERATOR_RSHIFT; | |
6027 | imp->advance(4); | |
6028 | } | |
6029 | else if (imp->match_c_string(" & ")) | |
6030 | { | |
6031 | op = OPERATOR_AND; | |
6032 | imp->advance(3); | |
6033 | } | |
6034 | else if (imp->match_c_string(" &^ ")) | |
6035 | { | |
6036 | op = OPERATOR_BITCLEAR; | |
6037 | imp->advance(4); | |
6038 | } | |
6039 | else | |
6040 | { | |
6041 | error_at(imp->location(), "unrecognized binary operator"); | |
6042 | return Expression::make_error(imp->location()); | |
6043 | } | |
6044 | ||
6045 | Expression* right = Expression::import_expression(imp); | |
6046 | ||
6047 | imp->require_c_string(")"); | |
6048 | ||
6049 | return Expression::make_binary(op, left, right, imp->location()); | |
6050 | } | |
6051 | ||
d751bb78 | 6052 | // Dump ast representation of a binary expression. |
6053 | ||
6054 | void | |
6055 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6056 | { | |
6057 | ast_dump_context->ostream() << "("; | |
6058 | ast_dump_context->dump_expression(this->left_); | |
6059 | ast_dump_context->ostream() << " "; | |
6060 | ast_dump_context->dump_operator(this->op_); | |
6061 | ast_dump_context->ostream() << " "; | |
6062 | ast_dump_context->dump_expression(this->right_); | |
6063 | ast_dump_context->ostream() << ") "; | |
6064 | } | |
6065 | ||
e440a328 | 6066 | // Make a binary expression. |
6067 | ||
6068 | Expression* | |
6069 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6070 | Location location) |
e440a328 | 6071 | { |
6072 | return new Binary_expression(op, left, right, location); | |
6073 | } | |
6074 | ||
6075 | // Implement a comparison. | |
6076 | ||
a32698ee | 6077 | Bexpression* |
6078 | Expression::comparison(Translate_context* context, Type* result_type, | |
6079 | Operator op, Expression* left, Expression* right, | |
6080 | Location location) | |
e440a328 | 6081 | { |
2387f644 | 6082 | Type* left_type = left->type(); |
6083 | Type* right_type = right->type(); | |
ceeb12d7 | 6084 | |
e67508fa | 6085 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6086 | |
15c67ee2 | 6087 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6088 | { |
2387f644 | 6089 | left = Runtime::make_call(Runtime::STRCMP, location, 2, |
6090 | left, right); | |
6091 | right = zexpr; | |
e440a328 | 6092 | } |
15c67ee2 | 6093 | else if ((left_type->interface_type() != NULL |
6094 | && right_type->interface_type() == NULL | |
6095 | && !right_type->is_nil_type()) | |
6096 | || (left_type->interface_type() == NULL | |
6097 | && !left_type->is_nil_type() | |
6098 | && right_type->interface_type() != NULL)) | |
e440a328 | 6099 | { |
6100 | // Comparing an interface value to a non-interface value. | |
6101 | if (left_type->interface_type() == NULL) | |
6102 | { | |
6103 | std::swap(left_type, right_type); | |
2387f644 | 6104 | std::swap(left, right); |
e440a328 | 6105 | } |
6106 | ||
6107 | // The right operand is not an interface. We need to take its | |
6108 | // address if it is not a pointer. | |
ceeb12d7 | 6109 | Expression* pointer_arg = NULL; |
e440a328 | 6110 | if (right_type->points_to() != NULL) |
2387f644 | 6111 | pointer_arg = right; |
e440a328 | 6112 | else |
6113 | { | |
2387f644 | 6114 | go_assert(right->is_addressable()); |
6115 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6116 | location); |
e440a328 | 6117 | } |
e440a328 | 6118 | |
2387f644 | 6119 | Expression* descriptor = |
6120 | Expression::make_type_descriptor(right_type, location); | |
6121 | left = | |
ceeb12d7 | 6122 | Runtime::make_call((left_type->interface_type()->is_empty() |
6123 | ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE | |
6124 | : Runtime::INTERFACE_VALUE_COMPARE), | |
2387f644 | 6125 | location, 3, left, descriptor, |
ceeb12d7 | 6126 | pointer_arg); |
2387f644 | 6127 | right = zexpr; |
e440a328 | 6128 | } |
6129 | else if (left_type->interface_type() != NULL | |
6130 | && right_type->interface_type() != NULL) | |
6131 | { | |
ceeb12d7 | 6132 | Runtime::Function compare_function; |
739bad04 | 6133 | if (left_type->interface_type()->is_empty() |
6134 | && right_type->interface_type()->is_empty()) | |
ceeb12d7 | 6135 | compare_function = Runtime::EMPTY_INTERFACE_COMPARE; |
739bad04 | 6136 | else if (!left_type->interface_type()->is_empty() |
6137 | && !right_type->interface_type()->is_empty()) | |
ceeb12d7 | 6138 | compare_function = Runtime::INTERFACE_COMPARE; |
739bad04 | 6139 | else |
6140 | { | |
6141 | if (left_type->interface_type()->is_empty()) | |
6142 | { | |
c484d925 | 6143 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
739bad04 | 6144 | std::swap(left_type, right_type); |
2387f644 | 6145 | std::swap(left, right); |
739bad04 | 6146 | } |
c484d925 | 6147 | go_assert(!left_type->interface_type()->is_empty()); |
6148 | go_assert(right_type->interface_type()->is_empty()); | |
ceeb12d7 | 6149 | compare_function = Runtime::INTERFACE_EMPTY_COMPARE; |
739bad04 | 6150 | } |
6151 | ||
2387f644 | 6152 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6153 | right = zexpr; | |
e440a328 | 6154 | } |
6155 | ||
6156 | if (left_type->is_nil_type() | |
6157 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6158 | { | |
6159 | std::swap(left_type, right_type); | |
2387f644 | 6160 | std::swap(left, right); |
e440a328 | 6161 | } |
6162 | ||
6163 | if (right_type->is_nil_type()) | |
6164 | { | |
2387f644 | 6165 | right = Expression::make_nil(location); |
e440a328 | 6166 | if (left_type->array_type() != NULL |
6167 | && left_type->array_type()->length() == NULL) | |
6168 | { | |
6169 | Array_type* at = left_type->array_type(); | |
2387f644 | 6170 | left = at->get_value_pointer(context->gogo(), left); |
e440a328 | 6171 | } |
6172 | else if (left_type->interface_type() != NULL) | |
6173 | { | |
6174 | // An interface is nil if the first field is nil. | |
2387f644 | 6175 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6176 | } |
6177 | } | |
6178 | ||
ea664253 | 6179 | Bexpression* left_bexpr = left->get_backend(context); |
6180 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6181 | |
a32698ee | 6182 | Gogo* gogo = context->gogo(); |
6183 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6184 | right_bexpr, location); | |
6185 | if (result_type != NULL) | |
6186 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6187 | ret, location); | |
e440a328 | 6188 | return ret; |
6189 | } | |
6190 | ||
6191 | // Class Bound_method_expression. | |
6192 | ||
6193 | // Traversal. | |
6194 | ||
6195 | int | |
6196 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6197 | { | |
e0659c9e | 6198 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6199 | } |
6200 | ||
0afbb937 | 6201 | // Lower the expression. If this is a method value rather than being |
6202 | // called, and the method is accessed via a pointer, we may need to | |
6203 | // add nil checks. Introduce a temporary variable so that those nil | |
6204 | // checks do not cause multiple evaluation. | |
6205 | ||
6206 | Expression* | |
6207 | Bound_method_expression::do_lower(Gogo*, Named_object*, | |
6208 | Statement_inserter* inserter, int) | |
6209 | { | |
6210 | // For simplicity we use a temporary for every call to an embedded | |
6211 | // method, even though some of them might be pure value methods and | |
6212 | // not require a temporary. | |
6213 | if (this->expr_->var_expression() == NULL | |
6214 | && this->expr_->temporary_reference_expression() == NULL | |
6215 | && this->expr_->set_and_use_temporary_expression() == NULL | |
6216 | && (this->method_->field_indexes() != NULL | |
6217 | || (this->method_->is_value_method() | |
6218 | && this->expr_->type()->points_to() != NULL))) | |
6219 | { | |
6220 | Temporary_statement* temp = | |
6221 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
6222 | inserter->insert(temp); | |
6223 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
6224 | this->location()); | |
6225 | } | |
6226 | return this; | |
6227 | } | |
6228 | ||
e440a328 | 6229 | // Return the type of a bound method expression. The type of this |
0afbb937 | 6230 | // object is simply the type of the method with no receiver. |
e440a328 | 6231 | |
6232 | Type* | |
6233 | Bound_method_expression::do_type() | |
6234 | { | |
0afbb937 | 6235 | Named_object* fn = this->method_->named_object(); |
6236 | Function_type* fntype; | |
6237 | if (fn->is_function()) | |
6238 | fntype = fn->func_value()->type(); | |
6239 | else if (fn->is_function_declaration()) | |
6240 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6241 | else |
6242 | return Type::make_error_type(); | |
0afbb937 | 6243 | return fntype->copy_without_receiver(); |
e440a328 | 6244 | } |
6245 | ||
6246 | // Determine the types of a method expression. | |
6247 | ||
6248 | void | |
6249 | Bound_method_expression::do_determine_type(const Type_context*) | |
6250 | { | |
0afbb937 | 6251 | Named_object* fn = this->method_->named_object(); |
6252 | Function_type* fntype; | |
6253 | if (fn->is_function()) | |
6254 | fntype = fn->func_value()->type(); | |
6255 | else if (fn->is_function_declaration()) | |
6256 | fntype = fn->func_declaration_value()->type(); | |
6257 | else | |
6258 | fntype = NULL; | |
e440a328 | 6259 | if (fntype == NULL || !fntype->is_method()) |
6260 | this->expr_->determine_type_no_context(); | |
6261 | else | |
6262 | { | |
6263 | Type_context subcontext(fntype->receiver()->type(), false); | |
6264 | this->expr_->determine_type(&subcontext); | |
6265 | } | |
6266 | } | |
6267 | ||
6268 | // Check the types of a method expression. | |
6269 | ||
6270 | void | |
6271 | Bound_method_expression::do_check_types(Gogo*) | |
6272 | { | |
0afbb937 | 6273 | Named_object* fn = this->method_->named_object(); |
6274 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6275 | { | |
6276 | this->report_error(_("object is not a method")); | |
6277 | return; | |
6278 | } | |
6279 | ||
6280 | Function_type* fntype; | |
6281 | if (fn->is_function()) | |
6282 | fntype = fn->func_value()->type(); | |
6283 | else if (fn->is_function_declaration()) | |
6284 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6285 | else |
0afbb937 | 6286 | go_unreachable(); |
6287 | Type* rtype = fntype->receiver()->type()->deref(); | |
6288 | Type* etype = (this->expr_type_ != NULL | |
6289 | ? this->expr_type_ | |
6290 | : this->expr_->type()); | |
6291 | etype = etype->deref(); | |
6292 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6293 | this->report_error(_("method type does not match object type")); | |
6294 | } | |
6295 | ||
6296 | // If a bound method expression is not simply called, then it is | |
6297 | // represented as a closure. The closure will hold a single variable, | |
6298 | // the receiver to pass to the method. The function will be a simple | |
6299 | // thunk that pulls that value from the closure and calls the method | |
6300 | // with the remaining arguments. | |
6301 | // | |
6302 | // Because method values are not common, we don't build all thunks for | |
6303 | // every methods, but instead only build them as we need them. In | |
6304 | // particular, we even build them on demand for methods defined in | |
6305 | // other packages. | |
6306 | ||
6307 | Bound_method_expression::Method_value_thunks | |
6308 | Bound_method_expression::method_value_thunks; | |
6309 | ||
6310 | // Find or create the thunk for METHOD. | |
6311 | ||
6312 | Named_object* | |
6313 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6314 | Named_object* fn) | |
6315 | { | |
6316 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6317 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6318 | Bound_method_expression::method_value_thunks.insert(val); | |
6319 | if (!ins.second) | |
6320 | { | |
6321 | // We have seen this method before. | |
6322 | go_assert(ins.first->second != NULL); | |
6323 | return ins.first->second; | |
6324 | } | |
6325 | ||
6326 | Location loc = fn->location(); | |
6327 | ||
6328 | Function_type* orig_fntype; | |
6329 | if (fn->is_function()) | |
6330 | orig_fntype = fn->func_value()->type(); | |
6331 | else if (fn->is_function_declaration()) | |
6332 | orig_fntype = fn->func_declaration_value()->type(); | |
6333 | else | |
6334 | orig_fntype = NULL; | |
6335 | ||
6336 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6337 | { |
0afbb937 | 6338 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6339 | return ins.first->second; | |
e440a328 | 6340 | } |
0afbb937 | 6341 | |
6342 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6343 | // The type here is wrong--it should be the C function type. But it |
6344 | // doesn't really matter. | |
0afbb937 | 6345 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6346 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6347 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6348 | orig_fntype->receiver()->type(), | |
6349 | loc))); | |
6350 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
6351 | closure_type = Type::make_pointer_type(closure_type); | |
6352 | ||
f8bdf81a | 6353 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6354 | |
da244e59 | 6355 | std::string thunk_name = Gogo::thunk_name(); |
6356 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 6357 | false, loc); |
6358 | ||
f8bdf81a | 6359 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6360 | cvar->set_is_used(); | |
1ecc6157 | 6361 | cvar->set_is_closure(); |
da244e59 | 6362 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6363 | NULL, cvar); | |
f8bdf81a | 6364 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6365 | |
f8bdf81a | 6366 | gogo->start_block(loc); |
0afbb937 | 6367 | |
6368 | // Field 0 of the closure is the function code pointer, field 1 is | |
6369 | // the value on which to invoke the method. | |
6370 | Expression* arg = Expression::make_var_reference(cp, loc); | |
6371 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
6372 | arg = Expression::make_field_reference(arg, 1, loc); | |
6373 | ||
6374 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6375 | ||
6376 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6377 | Expression_list* args; | |
6378 | if (orig_params == NULL || orig_params->empty()) | |
6379 | args = NULL; | |
6380 | else | |
6381 | { | |
6382 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6383 | args = new Expression_list(); | |
6384 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6385 | p != new_params->end(); |
0afbb937 | 6386 | ++p) |
6387 | { | |
6388 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6389 | go_assert(p_no != NULL | |
6390 | && p_no->is_variable() | |
6391 | && p_no->var_value()->is_parameter()); | |
6392 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6393 | } | |
6394 | } | |
6395 | ||
6396 | Call_expression* call = Expression::make_call(bme, args, | |
6397 | orig_fntype->is_varargs(), | |
6398 | loc); | |
6399 | call->set_varargs_are_lowered(); | |
6400 | ||
6401 | Statement* s = Statement::make_return_from_call(call, loc); | |
6402 | gogo->add_statement(s); | |
6403 | Block* b = gogo->finish_block(loc); | |
6404 | gogo->add_block(b, loc); | |
6405 | gogo->lower_block(new_no, b); | |
a32698ee | 6406 | gogo->flatten_block(new_no, b); |
0afbb937 | 6407 | gogo->finish_function(loc); |
6408 | ||
6409 | ins.first->second = new_no; | |
6410 | return new_no; | |
6411 | } | |
6412 | ||
6413 | // Return an expression to check *REF for nil while dereferencing | |
6414 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6415 | // reference. This is a static function so that we don't have to | |
6416 | // worry about declaring Field_indexes in expressions.h. | |
6417 | ||
6418 | static Expression* | |
6419 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6420 | Expression** ref) | |
6421 | { | |
6422 | if (field_indexes == NULL) | |
6423 | return Expression::make_boolean(false, loc); | |
6424 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6425 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6426 | go_assert(stype != NULL | |
6427 | && field_indexes->field_index < stype->field_count()); | |
6428 | if ((*ref)->type()->struct_type() == NULL) | |
6429 | { | |
6430 | go_assert((*ref)->type()->points_to() != NULL); | |
6431 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6432 | Expression::make_nil(loc), | |
6433 | loc); | |
6434 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
6435 | *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); | |
6436 | go_assert((*ref)->type()->struct_type() == stype); | |
6437 | } | |
6438 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6439 | loc); | |
6440 | return cond; | |
e440a328 | 6441 | } |
6442 | ||
ea664253 | 6443 | // Get the backend representation for a method value. |
e440a328 | 6444 | |
ea664253 | 6445 | Bexpression* |
6446 | Bound_method_expression::do_get_backend(Translate_context* context) | |
e440a328 | 6447 | { |
0afbb937 | 6448 | Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(), |
6449 | this->method_, | |
6450 | this->function_); | |
6451 | if (thunk->is_erroneous()) | |
6452 | { | |
6453 | go_assert(saw_errors()); | |
ea664253 | 6454 | return context->backend()->error_expression(); |
0afbb937 | 6455 | } |
6456 | ||
6457 | // FIXME: We should lower this earlier, but we can't lower it in the | |
6458 | // lowering pass because at that point we don't know whether we need | |
6459 | // to create the thunk or not. If the expression is called, we | |
6460 | // don't need the thunk. | |
6461 | ||
6462 | Location loc = this->location(); | |
6463 | ||
6464 | // If the method expects a value, and we have a pointer, we need to | |
6465 | // dereference the pointer. | |
6466 | ||
6467 | Named_object* fn = this->method_->named_object(); | |
6468 | Function_type* fntype; | |
6469 | if (fn->is_function()) | |
6470 | fntype = fn->func_value()->type(); | |
6471 | else if (fn->is_function_declaration()) | |
6472 | fntype = fn->func_declaration_value()->type(); | |
6473 | else | |
6474 | go_unreachable(); | |
6475 | ||
6476 | Expression* val = this->expr_; | |
6477 | if (fntype->receiver()->type()->points_to() == NULL | |
6478 | && val->type()->points_to() != NULL) | |
6479 | val = Expression::make_unary(OPERATOR_MULT, val, loc); | |
6480 | ||
6481 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
6482 | // expect a closure whose second field has type this->expr_type_ (if | |
6483 | // that is not NULL). We are going to pass it a closure whose | |
6484 | // second field has type this->expr_->type(). Since | |
6485 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
6486 | // away with this. | |
6487 | ||
6488 | Struct_field_list* fields = new Struct_field_list(); | |
6489 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
6490 | thunk->func_value()->type(), | |
6491 | loc))); | |
6492 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
6493 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6494 | ||
6495 | Expression_list* vals = new Expression_list(); | |
6496 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
6497 | vals->push_back(val); | |
6498 | ||
6499 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
2c809f8f | 6500 | ret = Expression::make_heap_expression(ret, loc); |
0afbb937 | 6501 | |
0afbb937 | 6502 | // See whether the expression or any embedded pointers are nil. |
6503 | ||
df7ef1fd | 6504 | Expression* nil_check = NULL; |
0afbb937 | 6505 | Expression* expr = this->expr_; |
6506 | if (this->method_->field_indexes() != NULL) | |
6507 | { | |
6508 | // Note that we are evaluating this->expr_ twice, but that is OK | |
6509 | // because in the lowering pass we forced it into a temporary | |
6510 | // variable. | |
6511 | Expression* ref = expr; | |
6512 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
6513 | expr = ref; | |
6514 | } | |
6515 | ||
6516 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
6517 | { | |
6518 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
6519 | Expression::make_nil(loc), | |
6520 | loc); | |
6521 | if (nil_check == NULL) | |
6522 | nil_check = n; | |
6523 | else | |
6524 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
6525 | } | |
6526 | ||
ea664253 | 6527 | Bexpression* bme = ret->get_backend(context); |
0afbb937 | 6528 | if (nil_check != NULL) |
6529 | { | |
df7ef1fd | 6530 | Gogo* gogo = context->gogo(); |
ea664253 | 6531 | Bexpression* crash = |
6532 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
6533 | loc)->get_backend(context); | |
df7ef1fd | 6534 | Btype* btype = ret->type()->get_backend(gogo); |
ea664253 | 6535 | Bexpression* bcheck = nil_check->get_backend(context); |
6536 | bme = gogo->backend()->conditional_expression(btype, bcheck, crash, | |
df7ef1fd | 6537 | bme, loc); |
6538 | } | |
ea664253 | 6539 | return bme; |
e440a328 | 6540 | } |
6541 | ||
d751bb78 | 6542 | // Dump ast representation of a bound method expression. |
6543 | ||
6544 | void | |
6545 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6546 | const | |
6547 | { | |
6548 | if (this->expr_type_ != NULL) | |
6549 | ast_dump_context->ostream() << "("; | |
6550 | ast_dump_context->dump_expression(this->expr_); | |
6551 | if (this->expr_type_ != NULL) | |
6552 | { | |
6553 | ast_dump_context->ostream() << ":"; | |
6554 | ast_dump_context->dump_type(this->expr_type_); | |
6555 | ast_dump_context->ostream() << ")"; | |
6556 | } | |
6557 | ||
0afbb937 | 6558 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 6559 | } |
6560 | ||
e440a328 | 6561 | // Make a method expression. |
6562 | ||
6563 | Bound_method_expression* | |
0afbb937 | 6564 | Expression::make_bound_method(Expression* expr, const Method* method, |
6565 | Named_object* function, Location location) | |
e440a328 | 6566 | { |
0afbb937 | 6567 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 6568 | } |
6569 | ||
6570 | // Class Builtin_call_expression. This is used for a call to a | |
6571 | // builtin function. | |
6572 | ||
6573 | class Builtin_call_expression : public Call_expression | |
6574 | { | |
6575 | public: | |
6576 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6577 | bool is_varargs, Location location); |
e440a328 | 6578 | |
6579 | protected: | |
6580 | // This overrides Call_expression::do_lower. | |
6581 | Expression* | |
ceeb4318 | 6582 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6583 | |
35a54f17 | 6584 | Expression* |
6585 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
6586 | ||
e440a328 | 6587 | bool |
6588 | do_is_constant() const; | |
6589 | ||
6590 | bool | |
0c77715b | 6591 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6592 | |
4f2138d7 | 6593 | bool |
a7549a6a | 6594 | do_discarding_value(); |
6595 | ||
e440a328 | 6596 | Type* |
6597 | do_type(); | |
6598 | ||
6599 | void | |
6600 | do_determine_type(const Type_context*); | |
6601 | ||
6602 | void | |
6603 | do_check_types(Gogo*); | |
6604 | ||
6605 | Expression* | |
72666aed | 6606 | do_copy(); |
e440a328 | 6607 | |
ea664253 | 6608 | Bexpression* |
6609 | do_get_backend(Translate_context*); | |
e440a328 | 6610 | |
6611 | void | |
6612 | do_export(Export*) const; | |
6613 | ||
6614 | virtual bool | |
6615 | do_is_recover_call() const; | |
6616 | ||
6617 | virtual void | |
6618 | do_set_recover_arg(Expression*); | |
6619 | ||
6620 | private: | |
6621 | // The builtin functions. | |
6622 | enum Builtin_function_code | |
6623 | { | |
6624 | BUILTIN_INVALID, | |
6625 | ||
6626 | // Predeclared builtin functions. | |
6627 | BUILTIN_APPEND, | |
6628 | BUILTIN_CAP, | |
6629 | BUILTIN_CLOSE, | |
48080209 | 6630 | BUILTIN_COMPLEX, |
e440a328 | 6631 | BUILTIN_COPY, |
1cce762f | 6632 | BUILTIN_DELETE, |
e440a328 | 6633 | BUILTIN_IMAG, |
6634 | BUILTIN_LEN, | |
6635 | BUILTIN_MAKE, | |
6636 | BUILTIN_NEW, | |
6637 | BUILTIN_PANIC, | |
6638 | BUILTIN_PRINT, | |
6639 | BUILTIN_PRINTLN, | |
6640 | BUILTIN_REAL, | |
6641 | BUILTIN_RECOVER, | |
6642 | ||
6643 | // Builtin functions from the unsafe package. | |
6644 | BUILTIN_ALIGNOF, | |
6645 | BUILTIN_OFFSETOF, | |
6646 | BUILTIN_SIZEOF | |
6647 | }; | |
6648 | ||
6649 | Expression* | |
6650 | one_arg() const; | |
6651 | ||
6652 | bool | |
6653 | check_one_arg(); | |
6654 | ||
6655 | static Type* | |
6656 | real_imag_type(Type*); | |
6657 | ||
6658 | static Type* | |
48080209 | 6659 | complex_type(Type*); |
e440a328 | 6660 | |
a9182619 | 6661 | Expression* |
6662 | lower_make(); | |
6663 | ||
6664 | bool | |
1ad00fd4 | 6665 | check_int_value(Expression*, bool is_length); |
a9182619 | 6666 | |
e440a328 | 6667 | // A pointer back to the general IR structure. This avoids a global |
6668 | // variable, or passing it around everywhere. | |
6669 | Gogo* gogo_; | |
6670 | // The builtin function being called. | |
6671 | Builtin_function_code code_; | |
0f914071 | 6672 | // Used to stop endless loops when the length of an array uses len |
6673 | // or cap of the array itself. | |
6674 | mutable bool seen_; | |
6334270b | 6675 | // Whether the argument is set for calls to BUILTIN_RECOVER. |
6676 | bool recover_arg_is_set_; | |
e440a328 | 6677 | }; |
6678 | ||
6679 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6680 | Expression* fn, | |
6681 | Expression_list* args, | |
6682 | bool is_varargs, | |
b13c66cd | 6683 | Location location) |
e440a328 | 6684 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 6685 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
6686 | recover_arg_is_set_(false) | |
e440a328 | 6687 | { |
6688 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 6689 | if (fnexp == NULL) |
6690 | { | |
6691 | this->code_ = BUILTIN_INVALID; | |
6692 | return; | |
6693 | } | |
e440a328 | 6694 | const std::string& name(fnexp->named_object()->name()); |
6695 | if (name == "append") | |
6696 | this->code_ = BUILTIN_APPEND; | |
6697 | else if (name == "cap") | |
6698 | this->code_ = BUILTIN_CAP; | |
6699 | else if (name == "close") | |
6700 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6701 | else if (name == "complex") |
6702 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6703 | else if (name == "copy") |
6704 | this->code_ = BUILTIN_COPY; | |
1cce762f | 6705 | else if (name == "delete") |
6706 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 6707 | else if (name == "imag") |
6708 | this->code_ = BUILTIN_IMAG; | |
6709 | else if (name == "len") | |
6710 | this->code_ = BUILTIN_LEN; | |
6711 | else if (name == "make") | |
6712 | this->code_ = BUILTIN_MAKE; | |
6713 | else if (name == "new") | |
6714 | this->code_ = BUILTIN_NEW; | |
6715 | else if (name == "panic") | |
6716 | this->code_ = BUILTIN_PANIC; | |
6717 | else if (name == "print") | |
6718 | this->code_ = BUILTIN_PRINT; | |
6719 | else if (name == "println") | |
6720 | this->code_ = BUILTIN_PRINTLN; | |
6721 | else if (name == "real") | |
6722 | this->code_ = BUILTIN_REAL; | |
6723 | else if (name == "recover") | |
6724 | this->code_ = BUILTIN_RECOVER; | |
6725 | else if (name == "Alignof") | |
6726 | this->code_ = BUILTIN_ALIGNOF; | |
6727 | else if (name == "Offsetof") | |
6728 | this->code_ = BUILTIN_OFFSETOF; | |
6729 | else if (name == "Sizeof") | |
6730 | this->code_ = BUILTIN_SIZEOF; | |
6731 | else | |
c3e6f413 | 6732 | go_unreachable(); |
e440a328 | 6733 | } |
6734 | ||
6735 | // Return whether this is a call to recover. This is a virtual | |
6736 | // function called from the parent class. | |
6737 | ||
6738 | bool | |
6739 | Builtin_call_expression::do_is_recover_call() const | |
6740 | { | |
6741 | if (this->classification() == EXPRESSION_ERROR) | |
6742 | return false; | |
6743 | return this->code_ == BUILTIN_RECOVER; | |
6744 | } | |
6745 | ||
6746 | // Set the argument for a call to recover. | |
6747 | ||
6748 | void | |
6749 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6750 | { | |
6751 | const Expression_list* args = this->args(); | |
c484d925 | 6752 | go_assert(args == NULL || args->empty()); |
e440a328 | 6753 | Expression_list* new_args = new Expression_list(); |
6754 | new_args->push_back(arg); | |
6755 | this->set_args(new_args); | |
6334270b | 6756 | this->recover_arg_is_set_ = true; |
e440a328 | 6757 | } |
6758 | ||
e440a328 | 6759 | // Lower a builtin call expression. This turns new and make into |
6760 | // specific expressions. We also convert to a constant if we can. | |
6761 | ||
6762 | Expression* | |
ceeb4318 | 6763 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
6764 | Statement_inserter* inserter, int) | |
e440a328 | 6765 | { |
79651b1f | 6766 | if (this->is_error_expression()) |
a9182619 | 6767 | return this; |
6768 | ||
b13c66cd | 6769 | Location loc = this->location(); |
1cce762f | 6770 | |
a8725655 | 6771 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
6772 | { | |
6773 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 6774 | return Expression::make_error(loc); |
a8725655 | 6775 | } |
6776 | ||
393ba00b | 6777 | if (this->code_ == BUILTIN_OFFSETOF) |
6778 | { | |
6779 | Expression* arg = this->one_arg(); | |
12e69faa | 6780 | |
6781 | if (arg->bound_method_expression() != NULL | |
6782 | || arg->interface_field_reference_expression() != NULL) | |
6783 | { | |
6784 | this->report_error(_("invalid use of method value as argument " | |
6785 | "of Offsetof")); | |
6786 | return this; | |
6787 | } | |
6788 | ||
393ba00b | 6789 | Field_reference_expression* farg = arg->field_reference_expression(); |
6790 | while (farg != NULL) | |
6791 | { | |
6792 | if (!farg->implicit()) | |
6793 | break; | |
6794 | // When the selector refers to an embedded field, | |
6795 | // it must not be reached through pointer indirections. | |
6796 | if (farg->expr()->deref() != farg->expr()) | |
6797 | { | |
12e69faa | 6798 | this->report_error(_("argument of Offsetof implies " |
6799 | "indirection of an embedded field")); | |
393ba00b | 6800 | return this; |
6801 | } | |
6802 | // Go up until we reach the original base. | |
6803 | farg = farg->expr()->field_reference_expression(); | |
6804 | } | |
6805 | } | |
6806 | ||
1cce762f | 6807 | if (this->is_constant()) |
e440a328 | 6808 | { |
0c77715b | 6809 | Numeric_constant nc; |
6810 | if (this->numeric_constant_value(&nc)) | |
6811 | return nc.expression(loc); | |
e440a328 | 6812 | } |
1cce762f | 6813 | |
6814 | switch (this->code_) | |
e440a328 | 6815 | { |
1cce762f | 6816 | default: |
6817 | break; | |
6818 | ||
6819 | case BUILTIN_NEW: | |
6820 | { | |
6821 | const Expression_list* args = this->args(); | |
6822 | if (args == NULL || args->size() < 1) | |
6823 | this->report_error(_("not enough arguments")); | |
6824 | else if (args->size() > 1) | |
6825 | this->report_error(_("too many arguments")); | |
6826 | else | |
6827 | { | |
6828 | Expression* arg = args->front(); | |
6829 | if (!arg->is_type_expression()) | |
6830 | { | |
6831 | error_at(arg->location(), "expected type"); | |
6832 | this->set_is_error(); | |
6833 | } | |
6834 | else | |
6835 | return Expression::make_allocation(arg->type(), loc); | |
6836 | } | |
6837 | } | |
6838 | break; | |
6839 | ||
6840 | case BUILTIN_MAKE: | |
6841 | return this->lower_make(); | |
6842 | ||
6843 | case BUILTIN_RECOVER: | |
e440a328 | 6844 | if (function != NULL) |
6845 | function->func_value()->set_calls_recover(); | |
6846 | else | |
6847 | { | |
6848 | // Calling recover outside of a function always returns the | |
6849 | // nil empty interface. | |
823c7e3d | 6850 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 6851 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 6852 | } |
1cce762f | 6853 | break; |
6854 | ||
6855 | case BUILTIN_APPEND: | |
6856 | { | |
6857 | // Lower the varargs. | |
6858 | const Expression_list* args = this->args(); | |
6859 | if (args == NULL || args->empty()) | |
e440a328 | 6860 | return this; |
1cce762f | 6861 | Type* slice_type = args->front()->type(); |
6862 | if (!slice_type->is_slice_type()) | |
6863 | { | |
3ff4863b | 6864 | if (slice_type->is_nil_type()) |
6865 | error_at(args->front()->location(), "use of untyped nil"); | |
6866 | else | |
6867 | error_at(args->front()->location(), | |
6868 | "argument 1 must be a slice"); | |
1cce762f | 6869 | this->set_is_error(); |
6870 | return this; | |
6871 | } | |
19fd40c3 | 6872 | Type* element_type = slice_type->array_type()->element_type(); |
6873 | this->lower_varargs(gogo, function, inserter, | |
6874 | Type::make_array_type(element_type, NULL), | |
6875 | 2); | |
1cce762f | 6876 | } |
6877 | break; | |
6878 | ||
6879 | case BUILTIN_DELETE: | |
6880 | { | |
6881 | // Lower to a runtime function call. | |
6882 | const Expression_list* args = this->args(); | |
6883 | if (args == NULL || args->size() < 2) | |
6884 | this->report_error(_("not enough arguments")); | |
6885 | else if (args->size() > 2) | |
6886 | this->report_error(_("too many arguments")); | |
6887 | else if (args->front()->type()->map_type() == NULL) | |
6888 | this->report_error(_("argument 1 must be a map")); | |
6889 | else | |
6890 | { | |
6891 | // Since this function returns no value it must appear in | |
6892 | // a statement by itself, so we don't have to worry about | |
6893 | // order of evaluation of values around it. Evaluate the | |
6894 | // map first to get order of evaluation right. | |
6895 | Map_type* mt = args->front()->type()->map_type(); | |
6896 | Temporary_statement* map_temp = | |
6897 | Statement::make_temporary(mt, args->front(), loc); | |
6898 | inserter->insert(map_temp); | |
6899 | ||
6900 | Temporary_statement* key_temp = | |
6901 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
6902 | inserter->insert(key_temp); | |
6903 | ||
6904 | Expression* e1 = Expression::make_temporary_reference(map_temp, | |
6905 | loc); | |
6906 | Expression* e2 = Expression::make_temporary_reference(key_temp, | |
6907 | loc); | |
6908 | e2 = Expression::make_unary(OPERATOR_AND, e2, loc); | |
6909 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), | |
6910 | 2, e1, e2); | |
6911 | } | |
6912 | } | |
6913 | break; | |
e440a328 | 6914 | } |
6915 | ||
6916 | return this; | |
6917 | } | |
6918 | ||
35a54f17 | 6919 | // Flatten a builtin call expression. This turns the arguments of copy and |
6920 | // append into temporary expressions. | |
6921 | ||
6922 | Expression* | |
6923 | Builtin_call_expression::do_flatten(Gogo*, Named_object*, | |
6924 | Statement_inserter* inserter) | |
6925 | { | |
16cb7fec | 6926 | Location loc = this->location(); |
6927 | ||
6928 | switch (this->code_) | |
35a54f17 | 6929 | { |
16cb7fec | 6930 | default: |
6931 | break; | |
6932 | ||
6933 | case BUILTIN_APPEND: | |
6934 | case BUILTIN_COPY: | |
6935 | { | |
6936 | Type* at = this->args()->front()->type(); | |
6937 | for (Expression_list::iterator pa = this->args()->begin(); | |
6938 | pa != this->args()->end(); | |
6939 | ++pa) | |
6940 | { | |
6941 | if ((*pa)->is_nil_expression()) | |
6942 | { | |
6943 | Expression* nil = Expression::make_nil(loc); | |
6944 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
6945 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
6946 | } | |
6947 | if (!(*pa)->is_variable()) | |
6948 | { | |
6949 | Temporary_statement* temp = | |
6950 | Statement::make_temporary(NULL, *pa, loc); | |
6951 | inserter->insert(temp); | |
6952 | *pa = Expression::make_temporary_reference(temp, loc); | |
6953 | } | |
6954 | } | |
6955 | } | |
6956 | break; | |
6957 | ||
6958 | case BUILTIN_PANIC: | |
35a54f17 | 6959 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 6960 | pa != this->args()->end(); |
6961 | ++pa) | |
6962 | { | |
6963 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 6964 | { |
16cb7fec | 6965 | Temporary_statement* temp = |
6966 | Statement::make_temporary(NULL, *pa, loc); | |
6967 | inserter->insert(temp); | |
6968 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 6969 | } |
16cb7fec | 6970 | } |
35a54f17 | 6971 | } |
16cb7fec | 6972 | |
35a54f17 | 6973 | return this; |
6974 | } | |
6975 | ||
a9182619 | 6976 | // Lower a make expression. |
6977 | ||
6978 | Expression* | |
6979 | Builtin_call_expression::lower_make() | |
6980 | { | |
b13c66cd | 6981 | Location loc = this->location(); |
a9182619 | 6982 | |
6983 | const Expression_list* args = this->args(); | |
6984 | if (args == NULL || args->size() < 1) | |
6985 | { | |
6986 | this->report_error(_("not enough arguments")); | |
6987 | return Expression::make_error(this->location()); | |
6988 | } | |
6989 | ||
6990 | Expression_list::const_iterator parg = args->begin(); | |
6991 | ||
6992 | Expression* first_arg = *parg; | |
6993 | if (!first_arg->is_type_expression()) | |
6994 | { | |
6995 | error_at(first_arg->location(), "expected type"); | |
6996 | this->set_is_error(); | |
6997 | return Expression::make_error(this->location()); | |
6998 | } | |
6999 | Type* type = first_arg->type(); | |
7000 | ||
7001 | bool is_slice = false; | |
7002 | bool is_map = false; | |
7003 | bool is_chan = false; | |
411eb89e | 7004 | if (type->is_slice_type()) |
a9182619 | 7005 | is_slice = true; |
7006 | else if (type->map_type() != NULL) | |
7007 | is_map = true; | |
7008 | else if (type->channel_type() != NULL) | |
7009 | is_chan = true; | |
7010 | else | |
7011 | { | |
7012 | this->report_error(_("invalid type for make function")); | |
7013 | return Expression::make_error(this->location()); | |
7014 | } | |
7015 | ||
ac84c822 | 7016 | bool have_big_args = false; |
7017 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
7018 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
7019 | ||
f6bc81e6 | 7020 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7021 | ||
a9182619 | 7022 | ++parg; |
7023 | Expression* len_arg; | |
7024 | if (parg == args->end()) | |
7025 | { | |
7026 | if (is_slice) | |
7027 | { | |
7028 | this->report_error(_("length required when allocating a slice")); | |
7029 | return Expression::make_error(this->location()); | |
7030 | } | |
e67508fa | 7031 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
a9182619 | 7032 | } |
7033 | else | |
7034 | { | |
7035 | len_arg = *parg; | |
f6bc81e6 | 7036 | len_arg->determine_type(&int_context); |
1ad00fd4 | 7037 | if (!this->check_int_value(len_arg, true)) |
7038 | return Expression::make_error(this->location()); | |
ac84c822 | 7039 | if (len_arg->type()->integer_type() != NULL |
7040 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
7041 | have_big_args = true; | |
a9182619 | 7042 | ++parg; |
7043 | } | |
7044 | ||
7045 | Expression* cap_arg = NULL; | |
7046 | if (is_slice && parg != args->end()) | |
7047 | { | |
7048 | cap_arg = *parg; | |
f6bc81e6 | 7049 | cap_arg->determine_type(&int_context); |
1ad00fd4 | 7050 | if (!this->check_int_value(cap_arg, false)) |
7051 | return Expression::make_error(this->location()); | |
7052 | ||
7053 | Numeric_constant nclen; | |
7054 | Numeric_constant nccap; | |
7055 | unsigned long vlen; | |
7056 | unsigned long vcap; | |
7057 | if (len_arg->numeric_constant_value(&nclen) | |
7058 | && cap_arg->numeric_constant_value(&nccap) | |
7059 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7060 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7061 | && vlen > vcap) | |
a9182619 | 7062 | { |
1ad00fd4 | 7063 | this->report_error(_("len larger than cap")); |
a9182619 | 7064 | return Expression::make_error(this->location()); |
7065 | } | |
1ad00fd4 | 7066 | |
ac84c822 | 7067 | if (cap_arg->type()->integer_type() != NULL |
7068 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
7069 | have_big_args = true; | |
a9182619 | 7070 | ++parg; |
7071 | } | |
7072 | ||
7073 | if (parg != args->end()) | |
7074 | { | |
7075 | this->report_error(_("too many arguments to make")); | |
7076 | return Expression::make_error(this->location()); | |
7077 | } | |
7078 | ||
b13c66cd | 7079 | Location type_loc = first_arg->location(); |
a9182619 | 7080 | Expression* type_arg; |
7081 | if (is_slice || is_chan) | |
7082 | type_arg = Expression::make_type_descriptor(type, type_loc); | |
7083 | else if (is_map) | |
7084 | type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); | |
7085 | else | |
7086 | go_unreachable(); | |
7087 | ||
7088 | Expression* call; | |
7089 | if (is_slice) | |
7090 | { | |
7091 | if (cap_arg == NULL) | |
ac84c822 | 7092 | call = Runtime::make_call((have_big_args |
7093 | ? Runtime::MAKESLICE1BIG | |
7094 | : Runtime::MAKESLICE1), | |
7095 | loc, 2, type_arg, len_arg); | |
a9182619 | 7096 | else |
ac84c822 | 7097 | call = Runtime::make_call((have_big_args |
7098 | ? Runtime::MAKESLICE2BIG | |
7099 | : Runtime::MAKESLICE2), | |
7100 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7101 | } |
7102 | else if (is_map) | |
ac84c822 | 7103 | call = Runtime::make_call((have_big_args |
7104 | ? Runtime::MAKEMAPBIG | |
7105 | : Runtime::MAKEMAP), | |
7106 | loc, 2, type_arg, len_arg); | |
a9182619 | 7107 | else if (is_chan) |
ac84c822 | 7108 | call = Runtime::make_call((have_big_args |
7109 | ? Runtime::MAKECHANBIG | |
7110 | : Runtime::MAKECHAN), | |
7111 | loc, 2, type_arg, len_arg); | |
a9182619 | 7112 | else |
7113 | go_unreachable(); | |
7114 | ||
7115 | return Expression::make_unsafe_cast(type, call, loc); | |
7116 | } | |
7117 | ||
7118 | // Return whether an expression has an integer value. Report an error | |
7119 | // if not. This is used when handling calls to the predeclared make | |
7120 | // function. | |
7121 | ||
7122 | bool | |
1ad00fd4 | 7123 | Builtin_call_expression::check_int_value(Expression* e, bool is_length) |
a9182619 | 7124 | { |
0c77715b | 7125 | Numeric_constant nc; |
1ad00fd4 | 7126 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7127 | { |
1ad00fd4 | 7128 | unsigned long v; |
7129 | switch (nc.to_unsigned_long(&v)) | |
7130 | { | |
7131 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7132 | break; |
1ad00fd4 | 7133 | case Numeric_constant::NC_UL_NOTINT: |
7134 | error_at(e->location(), "non-integer %s argument to make", | |
7135 | is_length ? "len" : "cap"); | |
7136 | return false; | |
7137 | case Numeric_constant::NC_UL_NEGATIVE: | |
7138 | error_at(e->location(), "negative %s argument to make", | |
7139 | is_length ? "len" : "cap"); | |
7140 | return false; | |
7141 | case Numeric_constant::NC_UL_BIG: | |
7142 | // We don't want to give a compile-time error for a 64-bit | |
7143 | // value on a 32-bit target. | |
1b10c5e7 | 7144 | break; |
1ad00fd4 | 7145 | } |
1b10c5e7 | 7146 | |
7147 | mpz_t val; | |
7148 | if (!nc.to_int(&val)) | |
7149 | go_unreachable(); | |
7150 | int bits = mpz_sizeinbase(val, 2); | |
7151 | mpz_clear(val); | |
7152 | Type* int_type = Type::lookup_integer_type("int"); | |
7153 | if (bits >= int_type->integer_type()->bits()) | |
7154 | { | |
7155 | error_at(e->location(), "%s argument too large for make", | |
7156 | is_length ? "len" : "cap"); | |
7157 | return false; | |
7158 | } | |
7159 | ||
7160 | return true; | |
a9182619 | 7161 | } |
7162 | ||
1ad00fd4 | 7163 | if (e->type()->integer_type() != NULL) |
7164 | return true; | |
7165 | ||
7166 | error_at(e->location(), "non-integer %s argument to make", | |
7167 | is_length ? "len" : "cap"); | |
a9182619 | 7168 | return false; |
7169 | } | |
7170 | ||
e440a328 | 7171 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7172 | // the argument. We need to map complex64 to float32 and complex128 |
7173 | // to float64, so it has to be done by name. This returns NULL if it | |
7174 | // can't figure out the type. | |
e440a328 | 7175 | |
7176 | Type* | |
7177 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7178 | { | |
7179 | if (arg_type == NULL || arg_type->is_abstract()) | |
7180 | return NULL; | |
7181 | Named_type* nt = arg_type->named_type(); | |
7182 | if (nt == NULL) | |
7183 | return NULL; | |
7184 | while (nt->real_type()->named_type() != NULL) | |
7185 | nt = nt->real_type()->named_type(); | |
48080209 | 7186 | if (nt->name() == "complex64") |
e440a328 | 7187 | return Type::lookup_float_type("float32"); |
7188 | else if (nt->name() == "complex128") | |
7189 | return Type::lookup_float_type("float64"); | |
7190 | else | |
7191 | return NULL; | |
7192 | } | |
7193 | ||
48080209 | 7194 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7195 | // argments. Like real_imag_type, we have to map by name. |
7196 | ||
7197 | Type* | |
48080209 | 7198 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7199 | { |
7200 | if (arg_type == NULL || arg_type->is_abstract()) | |
7201 | return NULL; | |
7202 | Named_type* nt = arg_type->named_type(); | |
7203 | if (nt == NULL) | |
7204 | return NULL; | |
7205 | while (nt->real_type()->named_type() != NULL) | |
7206 | nt = nt->real_type()->named_type(); | |
48080209 | 7207 | if (nt->name() == "float32") |
e440a328 | 7208 | return Type::lookup_complex_type("complex64"); |
7209 | else if (nt->name() == "float64") | |
7210 | return Type::lookup_complex_type("complex128"); | |
7211 | else | |
7212 | return NULL; | |
7213 | } | |
7214 | ||
7215 | // Return a single argument, or NULL if there isn't one. | |
7216 | ||
7217 | Expression* | |
7218 | Builtin_call_expression::one_arg() const | |
7219 | { | |
7220 | const Expression_list* args = this->args(); | |
aa615cb3 | 7221 | if (args == NULL || args->size() != 1) |
e440a328 | 7222 | return NULL; |
7223 | return args->front(); | |
7224 | } | |
7225 | ||
83921647 | 7226 | // A traversal class which looks for a call or receive expression. |
7227 | ||
7228 | class Find_call_expression : public Traverse | |
7229 | { | |
7230 | public: | |
7231 | Find_call_expression() | |
7232 | : Traverse(traverse_expressions), | |
7233 | found_(false) | |
7234 | { } | |
7235 | ||
7236 | int | |
7237 | expression(Expression**); | |
7238 | ||
7239 | bool | |
7240 | found() | |
7241 | { return this->found_; } | |
7242 | ||
7243 | private: | |
7244 | bool found_; | |
7245 | }; | |
7246 | ||
7247 | int | |
7248 | Find_call_expression::expression(Expression** pexpr) | |
7249 | { | |
7250 | if ((*pexpr)->call_expression() != NULL | |
7251 | || (*pexpr)->receive_expression() != NULL) | |
7252 | { | |
7253 | this->found_ = true; | |
7254 | return TRAVERSE_EXIT; | |
7255 | } | |
7256 | return TRAVERSE_CONTINUE; | |
7257 | } | |
7258 | ||
7259 | // Return whether this is constant: len of a string constant, or len | |
7260 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7261 | // unsafe.Alignof. | |
e440a328 | 7262 | |
7263 | bool | |
7264 | Builtin_call_expression::do_is_constant() const | |
7265 | { | |
12e69faa | 7266 | if (this->is_error_expression()) |
7267 | return true; | |
e440a328 | 7268 | switch (this->code_) |
7269 | { | |
7270 | case BUILTIN_LEN: | |
7271 | case BUILTIN_CAP: | |
7272 | { | |
0f914071 | 7273 | if (this->seen_) |
7274 | return false; | |
7275 | ||
e440a328 | 7276 | Expression* arg = this->one_arg(); |
7277 | if (arg == NULL) | |
7278 | return false; | |
7279 | Type* arg_type = arg->type(); | |
7280 | ||
7281 | if (arg_type->points_to() != NULL | |
7282 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7283 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7284 | arg_type = arg_type->points_to(); |
7285 | ||
83921647 | 7286 | // The len and cap functions are only constant if there are no |
7287 | // function calls or channel operations in the arguments. | |
7288 | // Otherwise we have to make the call. | |
7289 | if (!arg->is_constant()) | |
7290 | { | |
7291 | Find_call_expression find_call; | |
7292 | Expression::traverse(&arg, &find_call); | |
7293 | if (find_call.found()) | |
7294 | return false; | |
7295 | } | |
7296 | ||
e440a328 | 7297 | if (arg_type->array_type() != NULL |
7298 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7299 | return true; |
e440a328 | 7300 | |
7301 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7302 | { |
7303 | this->seen_ = true; | |
7304 | bool ret = arg->is_constant(); | |
7305 | this->seen_ = false; | |
7306 | return ret; | |
7307 | } | |
e440a328 | 7308 | } |
7309 | break; | |
7310 | ||
7311 | case BUILTIN_SIZEOF: | |
7312 | case BUILTIN_ALIGNOF: | |
7313 | return this->one_arg() != NULL; | |
7314 | ||
7315 | case BUILTIN_OFFSETOF: | |
7316 | { | |
7317 | Expression* arg = this->one_arg(); | |
7318 | if (arg == NULL) | |
7319 | return false; | |
7320 | return arg->field_reference_expression() != NULL; | |
7321 | } | |
7322 | ||
48080209 | 7323 | case BUILTIN_COMPLEX: |
e440a328 | 7324 | { |
7325 | const Expression_list* args = this->args(); | |
7326 | if (args != NULL && args->size() == 2) | |
7327 | return args->front()->is_constant() && args->back()->is_constant(); | |
7328 | } | |
7329 | break; | |
7330 | ||
7331 | case BUILTIN_REAL: | |
7332 | case BUILTIN_IMAG: | |
7333 | { | |
7334 | Expression* arg = this->one_arg(); | |
7335 | return arg != NULL && arg->is_constant(); | |
7336 | } | |
7337 | ||
7338 | default: | |
7339 | break; | |
7340 | } | |
7341 | ||
7342 | return false; | |
7343 | } | |
7344 | ||
0c77715b | 7345 | // Return a numeric constant if possible. |
e440a328 | 7346 | |
7347 | bool | |
0c77715b | 7348 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7349 | { |
7350 | if (this->code_ == BUILTIN_LEN | |
7351 | || this->code_ == BUILTIN_CAP) | |
7352 | { | |
7353 | Expression* arg = this->one_arg(); | |
7354 | if (arg == NULL) | |
7355 | return false; | |
7356 | Type* arg_type = arg->type(); | |
7357 | ||
7358 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7359 | { | |
7360 | std::string sval; | |
7361 | if (arg->string_constant_value(&sval)) | |
7362 | { | |
0c77715b | 7363 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7364 | sval.length()); | |
e440a328 | 7365 | return true; |
7366 | } | |
7367 | } | |
7368 | ||
7369 | if (arg_type->points_to() != NULL | |
7370 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7371 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7372 | arg_type = arg_type->points_to(); |
7373 | ||
7374 | if (arg_type->array_type() != NULL | |
7375 | && arg_type->array_type()->length() != NULL) | |
7376 | { | |
0f914071 | 7377 | if (this->seen_) |
7378 | return false; | |
e440a328 | 7379 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7380 | this->seen_ = true; |
0c77715b | 7381 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7382 | this->seen_ = false; |
7383 | if (r) | |
e440a328 | 7384 | { |
0c77715b | 7385 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7386 | this->location())) | |
7387 | r = false; | |
e440a328 | 7388 | } |
0c77715b | 7389 | return r; |
e440a328 | 7390 | } |
7391 | } | |
7392 | else if (this->code_ == BUILTIN_SIZEOF | |
7393 | || this->code_ == BUILTIN_ALIGNOF) | |
7394 | { | |
7395 | Expression* arg = this->one_arg(); | |
7396 | if (arg == NULL) | |
7397 | return false; | |
7398 | Type* arg_type = arg->type(); | |
5c13bd80 | 7399 | if (arg_type->is_error()) |
e440a328 | 7400 | return false; |
7401 | if (arg_type->is_abstract()) | |
7402 | return false; | |
2c809f8f | 7403 | if (this->seen_) |
7404 | return false; | |
927a01eb | 7405 | |
3f378015 | 7406 | int64_t ret; |
e440a328 | 7407 | if (this->code_ == BUILTIN_SIZEOF) |
7408 | { | |
2c809f8f | 7409 | this->seen_ = true; |
7410 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
7411 | this->seen_ = false; | |
7412 | if (!ok) | |
e440a328 | 7413 | return false; |
7414 | } | |
7415 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7416 | { | |
2c809f8f | 7417 | bool ok; |
7418 | this->seen_ = true; | |
637bd3af | 7419 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 7420 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 7421 | else |
e440a328 | 7422 | { |
7423 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7424 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 7425 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 7426 | } |
2c809f8f | 7427 | this->seen_ = false; |
7428 | if (!ok) | |
7429 | return false; | |
e440a328 | 7430 | } |
7431 | else | |
c3e6f413 | 7432 | go_unreachable(); |
927a01eb | 7433 | |
3f378015 | 7434 | mpz_t zval; |
7435 | set_mpz_from_int64(&zval, ret); | |
7436 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
7437 | mpz_clear(zval); | |
e440a328 | 7438 | return true; |
7439 | } | |
7440 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7441 | { | |
7442 | Expression* arg = this->one_arg(); | |
7443 | if (arg == NULL) | |
7444 | return false; | |
7445 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7446 | if (farg == NULL) | |
7447 | return false; | |
2c809f8f | 7448 | if (this->seen_) |
7449 | return false; | |
7450 | ||
3f378015 | 7451 | int64_t total_offset = 0; |
9a4bd570 | 7452 | while (true) |
7453 | { | |
7454 | Expression* struct_expr = farg->expr(); | |
7455 | Type* st = struct_expr->type(); | |
7456 | if (st->struct_type() == NULL) | |
7457 | return false; | |
7458 | if (st->named_type() != NULL) | |
7459 | st->named_type()->convert(this->gogo_); | |
3f378015 | 7460 | int64_t offset; |
2c809f8f | 7461 | this->seen_ = true; |
7462 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
7463 | farg->field_index(), | |
7464 | &offset); | |
7465 | this->seen_ = false; | |
7466 | if (!ok) | |
7467 | return false; | |
9a4bd570 | 7468 | total_offset += offset; |
7469 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
7470 | { | |
7471 | // Go up until we reach the original base. | |
7472 | farg = struct_expr->field_reference_expression(); | |
7473 | continue; | |
7474 | } | |
7475 | break; | |
7476 | } | |
3f378015 | 7477 | mpz_t zval; |
7478 | set_mpz_from_int64(&zval, total_offset); | |
7479 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
7480 | mpz_clear(zval); | |
e440a328 | 7481 | return true; |
7482 | } | |
0c77715b | 7483 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7484 | { |
7485 | Expression* arg = this->one_arg(); | |
7486 | if (arg == NULL) | |
7487 | return false; | |
7488 | ||
0c77715b | 7489 | Numeric_constant argnc; |
7490 | if (!arg->numeric_constant_value(&argnc)) | |
7491 | return false; | |
7492 | ||
fcbea5e4 | 7493 | mpc_t val; |
7494 | if (!argnc.to_complex(&val)) | |
0c77715b | 7495 | return false; |
e440a328 | 7496 | |
0c77715b | 7497 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7498 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 7499 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 7500 | else |
fcbea5e4 | 7501 | nc->set_float(type, mpc_imagref(val)); |
7502 | mpc_clear(val); | |
0c77715b | 7503 | return true; |
e440a328 | 7504 | } |
0c77715b | 7505 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7506 | { |
7507 | const Expression_list* args = this->args(); | |
7508 | if (args == NULL || args->size() != 2) | |
7509 | return false; | |
7510 | ||
0c77715b | 7511 | Numeric_constant rnc; |
7512 | if (!args->front()->numeric_constant_value(&rnc)) | |
7513 | return false; | |
7514 | Numeric_constant inc; | |
7515 | if (!args->back()->numeric_constant_value(&inc)) | |
7516 | return false; | |
7517 | ||
7518 | if (rnc.type() != NULL | |
7519 | && !rnc.type()->is_abstract() | |
7520 | && inc.type() != NULL | |
7521 | && !inc.type()->is_abstract() | |
7522 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7523 | return false; | |
7524 | ||
e440a328 | 7525 | mpfr_t r; |
0c77715b | 7526 | if (!rnc.to_float(&r)) |
7527 | return false; | |
7528 | mpfr_t i; | |
7529 | if (!inc.to_float(&i)) | |
e440a328 | 7530 | { |
7531 | mpfr_clear(r); | |
7532 | return false; | |
7533 | } | |
7534 | ||
0c77715b | 7535 | Type* arg_type = rnc.type(); |
7536 | if (arg_type == NULL || arg_type->is_abstract()) | |
7537 | arg_type = inc.type(); | |
e440a328 | 7538 | |
fcbea5e4 | 7539 | mpc_t val; |
7540 | mpc_init2(val, mpc_precision); | |
7541 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 7542 | mpfr_clear(r); |
7543 | mpfr_clear(i); | |
7544 | ||
fcbea5e4 | 7545 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7546 | nc->set_complex(type, val); | |
7547 | ||
7548 | mpc_clear(val); | |
7549 | ||
0c77715b | 7550 | return true; |
e440a328 | 7551 | } |
7552 | ||
7553 | return false; | |
7554 | } | |
7555 | ||
a7549a6a | 7556 | // Give an error if we are discarding the value of an expression which |
7557 | // should not normally be discarded. We don't give an error for | |
7558 | // discarding the value of an ordinary function call, but we do for | |
7559 | // builtin functions, purely for consistency with the gc compiler. | |
7560 | ||
4f2138d7 | 7561 | bool |
a7549a6a | 7562 | Builtin_call_expression::do_discarding_value() |
7563 | { | |
7564 | switch (this->code_) | |
7565 | { | |
7566 | case BUILTIN_INVALID: | |
7567 | default: | |
7568 | go_unreachable(); | |
7569 | ||
7570 | case BUILTIN_APPEND: | |
7571 | case BUILTIN_CAP: | |
7572 | case BUILTIN_COMPLEX: | |
7573 | case BUILTIN_IMAG: | |
7574 | case BUILTIN_LEN: | |
7575 | case BUILTIN_MAKE: | |
7576 | case BUILTIN_NEW: | |
7577 | case BUILTIN_REAL: | |
7578 | case BUILTIN_ALIGNOF: | |
7579 | case BUILTIN_OFFSETOF: | |
7580 | case BUILTIN_SIZEOF: | |
7581 | this->unused_value_error(); | |
4f2138d7 | 7582 | return false; |
a7549a6a | 7583 | |
7584 | case BUILTIN_CLOSE: | |
7585 | case BUILTIN_COPY: | |
1cce762f | 7586 | case BUILTIN_DELETE: |
a7549a6a | 7587 | case BUILTIN_PANIC: |
7588 | case BUILTIN_PRINT: | |
7589 | case BUILTIN_PRINTLN: | |
7590 | case BUILTIN_RECOVER: | |
4f2138d7 | 7591 | return true; |
a7549a6a | 7592 | } |
7593 | } | |
7594 | ||
e440a328 | 7595 | // Return the type. |
7596 | ||
7597 | Type* | |
7598 | Builtin_call_expression::do_type() | |
7599 | { | |
79651b1f | 7600 | if (this->is_error_expression()) |
7601 | return Type::make_error_type(); | |
e440a328 | 7602 | switch (this->code_) |
7603 | { | |
7604 | case BUILTIN_INVALID: | |
7605 | default: | |
79651b1f | 7606 | return Type::make_error_type(); |
e440a328 | 7607 | |
7608 | case BUILTIN_NEW: | |
7609 | case BUILTIN_MAKE: | |
7610 | { | |
7611 | const Expression_list* args = this->args(); | |
7612 | if (args == NULL || args->empty()) | |
7613 | return Type::make_error_type(); | |
7614 | return Type::make_pointer_type(args->front()->type()); | |
7615 | } | |
7616 | ||
7617 | case BUILTIN_CAP: | |
7618 | case BUILTIN_COPY: | |
7619 | case BUILTIN_LEN: | |
7ba86326 | 7620 | return Type::lookup_integer_type("int"); |
7621 | ||
e440a328 | 7622 | case BUILTIN_ALIGNOF: |
7623 | case BUILTIN_OFFSETOF: | |
7624 | case BUILTIN_SIZEOF: | |
7ba86326 | 7625 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7626 | |
7627 | case BUILTIN_CLOSE: | |
1cce762f | 7628 | case BUILTIN_DELETE: |
e440a328 | 7629 | case BUILTIN_PANIC: |
7630 | case BUILTIN_PRINT: | |
7631 | case BUILTIN_PRINTLN: | |
7632 | return Type::make_void_type(); | |
7633 | ||
e440a328 | 7634 | case BUILTIN_RECOVER: |
823c7e3d | 7635 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7636 | |
7637 | case BUILTIN_APPEND: | |
7638 | { | |
7639 | const Expression_list* args = this->args(); | |
7640 | if (args == NULL || args->empty()) | |
7641 | return Type::make_error_type(); | |
3ff4863b | 7642 | Type *ret = args->front()->type(); |
7643 | if (!ret->is_slice_type()) | |
7644 | return Type::make_error_type(); | |
7645 | return ret; | |
e440a328 | 7646 | } |
7647 | ||
7648 | case BUILTIN_REAL: | |
7649 | case BUILTIN_IMAG: | |
7650 | { | |
7651 | Expression* arg = this->one_arg(); | |
7652 | if (arg == NULL) | |
7653 | return Type::make_error_type(); | |
7654 | Type* t = arg->type(); | |
7655 | if (t->is_abstract()) | |
7656 | t = t->make_non_abstract_type(); | |
7657 | t = Builtin_call_expression::real_imag_type(t); | |
7658 | if (t == NULL) | |
7659 | t = Type::make_error_type(); | |
7660 | return t; | |
7661 | } | |
7662 | ||
48080209 | 7663 | case BUILTIN_COMPLEX: |
e440a328 | 7664 | { |
7665 | const Expression_list* args = this->args(); | |
7666 | if (args == NULL || args->size() != 2) | |
7667 | return Type::make_error_type(); | |
7668 | Type* t = args->front()->type(); | |
7669 | if (t->is_abstract()) | |
7670 | { | |
7671 | t = args->back()->type(); | |
7672 | if (t->is_abstract()) | |
7673 | t = t->make_non_abstract_type(); | |
7674 | } | |
48080209 | 7675 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7676 | if (t == NULL) |
7677 | t = Type::make_error_type(); | |
7678 | return t; | |
7679 | } | |
7680 | } | |
7681 | } | |
7682 | ||
7683 | // Determine the type. | |
7684 | ||
7685 | void | |
7686 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7687 | { | |
fb94b0ca | 7688 | if (!this->determining_types()) |
7689 | return; | |
7690 | ||
e440a328 | 7691 | this->fn()->determine_type_no_context(); |
7692 | ||
7693 | const Expression_list* args = this->args(); | |
7694 | ||
7695 | bool is_print; | |
7696 | Type* arg_type = NULL; | |
7697 | switch (this->code_) | |
7698 | { | |
7699 | case BUILTIN_PRINT: | |
7700 | case BUILTIN_PRINTLN: | |
7701 | // Do not force a large integer constant to "int". | |
7702 | is_print = true; | |
7703 | break; | |
7704 | ||
7705 | case BUILTIN_REAL: | |
7706 | case BUILTIN_IMAG: | |
48080209 | 7707 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 7708 | if (arg_type == NULL) |
7709 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 7710 | is_print = false; |
7711 | break; | |
7712 | ||
48080209 | 7713 | case BUILTIN_COMPLEX: |
e440a328 | 7714 | { |
48080209 | 7715 | // For the complex function the type of one operand can |
e440a328 | 7716 | // determine the type of the other, as in a binary expression. |
7717 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 7718 | if (arg_type == NULL) |
7719 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 7720 | if (args != NULL && args->size() == 2) |
7721 | { | |
7722 | Type* t1 = args->front()->type(); | |
c849bb59 | 7723 | Type* t2 = args->back()->type(); |
e440a328 | 7724 | if (!t1->is_abstract()) |
7725 | arg_type = t1; | |
7726 | else if (!t2->is_abstract()) | |
7727 | arg_type = t2; | |
7728 | } | |
7729 | is_print = false; | |
7730 | } | |
7731 | break; | |
7732 | ||
7733 | default: | |
7734 | is_print = false; | |
7735 | break; | |
7736 | } | |
7737 | ||
7738 | if (args != NULL) | |
7739 | { | |
7740 | for (Expression_list::const_iterator pa = args->begin(); | |
7741 | pa != args->end(); | |
7742 | ++pa) | |
7743 | { | |
7744 | Type_context subcontext; | |
7745 | subcontext.type = arg_type; | |
7746 | ||
7747 | if (is_print) | |
7748 | { | |
7749 | // We want to print large constants, we so can't just | |
7750 | // use the appropriate nonabstract type. Use uint64 for | |
7751 | // an integer if we know it is nonnegative, otherwise | |
7752 | // use int64 for a integer, otherwise use float64 for a | |
7753 | // float or complex128 for a complex. | |
7754 | Type* want_type = NULL; | |
7755 | Type* atype = (*pa)->type(); | |
7756 | if (atype->is_abstract()) | |
7757 | { | |
7758 | if (atype->integer_type() != NULL) | |
7759 | { | |
0c77715b | 7760 | Numeric_constant nc; |
7761 | if (this->numeric_constant_value(&nc)) | |
7762 | { | |
7763 | mpz_t val; | |
7764 | if (nc.to_int(&val)) | |
7765 | { | |
7766 | if (mpz_sgn(val) >= 0) | |
7767 | want_type = Type::lookup_integer_type("uint64"); | |
7768 | mpz_clear(val); | |
7769 | } | |
7770 | } | |
7771 | if (want_type == NULL) | |
e440a328 | 7772 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 7773 | } |
7774 | else if (atype->float_type() != NULL) | |
7775 | want_type = Type::lookup_float_type("float64"); | |
7776 | else if (atype->complex_type() != NULL) | |
7777 | want_type = Type::lookup_complex_type("complex128"); | |
7778 | else if (atype->is_abstract_string_type()) | |
7779 | want_type = Type::lookup_string_type(); | |
7780 | else if (atype->is_abstract_boolean_type()) | |
7781 | want_type = Type::lookup_bool_type(); | |
7782 | else | |
c3e6f413 | 7783 | go_unreachable(); |
e440a328 | 7784 | subcontext.type = want_type; |
7785 | } | |
7786 | } | |
7787 | ||
7788 | (*pa)->determine_type(&subcontext); | |
7789 | } | |
7790 | } | |
7791 | } | |
7792 | ||
7793 | // If there is exactly one argument, return true. Otherwise give an | |
7794 | // error message and return false. | |
7795 | ||
7796 | bool | |
7797 | Builtin_call_expression::check_one_arg() | |
7798 | { | |
7799 | const Expression_list* args = this->args(); | |
7800 | if (args == NULL || args->size() < 1) | |
7801 | { | |
7802 | this->report_error(_("not enough arguments")); | |
7803 | return false; | |
7804 | } | |
7805 | else if (args->size() > 1) | |
7806 | { | |
7807 | this->report_error(_("too many arguments")); | |
7808 | return false; | |
7809 | } | |
7810 | if (args->front()->is_error_expression() | |
5c13bd80 | 7811 | || args->front()->type()->is_error()) |
e440a328 | 7812 | { |
7813 | this->set_is_error(); | |
7814 | return false; | |
7815 | } | |
7816 | return true; | |
7817 | } | |
7818 | ||
7819 | // Check argument types for a builtin function. | |
7820 | ||
7821 | void | |
7822 | Builtin_call_expression::do_check_types(Gogo*) | |
7823 | { | |
375646ea | 7824 | if (this->is_error_expression()) |
7825 | return; | |
e440a328 | 7826 | switch (this->code_) |
7827 | { | |
7828 | case BUILTIN_INVALID: | |
7829 | case BUILTIN_NEW: | |
7830 | case BUILTIN_MAKE: | |
cd238b8d | 7831 | case BUILTIN_DELETE: |
e440a328 | 7832 | return; |
7833 | ||
7834 | case BUILTIN_LEN: | |
7835 | case BUILTIN_CAP: | |
7836 | { | |
7837 | // The single argument may be either a string or an array or a | |
7838 | // map or a channel, or a pointer to a closed array. | |
7839 | if (this->check_one_arg()) | |
7840 | { | |
7841 | Type* arg_type = this->one_arg()->type(); | |
7842 | if (arg_type->points_to() != NULL | |
7843 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7844 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7845 | arg_type = arg_type->points_to(); |
7846 | if (this->code_ == BUILTIN_CAP) | |
7847 | { | |
5c13bd80 | 7848 | if (!arg_type->is_error() |
e440a328 | 7849 | && arg_type->array_type() == NULL |
7850 | && arg_type->channel_type() == NULL) | |
7851 | this->report_error(_("argument must be array or slice " | |
7852 | "or channel")); | |
7853 | } | |
7854 | else | |
7855 | { | |
5c13bd80 | 7856 | if (!arg_type->is_error() |
e440a328 | 7857 | && !arg_type->is_string_type() |
7858 | && arg_type->array_type() == NULL | |
7859 | && arg_type->map_type() == NULL | |
7860 | && arg_type->channel_type() == NULL) | |
7861 | this->report_error(_("argument must be string or " | |
7862 | "array or slice or map or channel")); | |
7863 | } | |
7864 | } | |
7865 | } | |
7866 | break; | |
7867 | ||
7868 | case BUILTIN_PRINT: | |
7869 | case BUILTIN_PRINTLN: | |
7870 | { | |
7871 | const Expression_list* args = this->args(); | |
7872 | if (args == NULL) | |
7873 | { | |
7874 | if (this->code_ == BUILTIN_PRINT) | |
7875 | warning_at(this->location(), 0, | |
7876 | "no arguments for builtin function %<%s%>", | |
7877 | (this->code_ == BUILTIN_PRINT | |
7878 | ? "print" | |
7879 | : "println")); | |
7880 | } | |
7881 | else | |
7882 | { | |
7883 | for (Expression_list::const_iterator p = args->begin(); | |
7884 | p != args->end(); | |
7885 | ++p) | |
7886 | { | |
7887 | Type* type = (*p)->type(); | |
5c13bd80 | 7888 | if (type->is_error() |
e440a328 | 7889 | || type->is_string_type() |
7890 | || type->integer_type() != NULL | |
7891 | || type->float_type() != NULL | |
7892 | || type->complex_type() != NULL | |
7893 | || type->is_boolean_type() | |
7894 | || type->points_to() != NULL | |
7895 | || type->interface_type() != NULL | |
7896 | || type->channel_type() != NULL | |
7897 | || type->map_type() != NULL | |
7898 | || type->function_type() != NULL | |
411eb89e | 7899 | || type->is_slice_type()) |
e440a328 | 7900 | ; |
acf8e158 | 7901 | else if ((*p)->is_type_expression()) |
7902 | { | |
7903 | // If this is a type expression it's going to give | |
7904 | // an error anyhow, so we don't need one here. | |
7905 | } | |
e440a328 | 7906 | else |
7907 | this->report_error(_("unsupported argument type to " | |
7908 | "builtin function")); | |
7909 | } | |
7910 | } | |
7911 | } | |
7912 | break; | |
7913 | ||
7914 | case BUILTIN_CLOSE: | |
e440a328 | 7915 | if (this->check_one_arg()) |
7916 | { | |
7917 | if (this->one_arg()->type()->channel_type() == NULL) | |
7918 | this->report_error(_("argument must be channel")); | |
5202d986 | 7919 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
7920 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 7921 | } |
7922 | break; | |
7923 | ||
7924 | case BUILTIN_PANIC: | |
7925 | case BUILTIN_SIZEOF: | |
7926 | case BUILTIN_ALIGNOF: | |
7927 | this->check_one_arg(); | |
7928 | break; | |
7929 | ||
7930 | case BUILTIN_RECOVER: | |
6334270b | 7931 | if (this->args() != NULL |
7932 | && !this->args()->empty() | |
7933 | && !this->recover_arg_is_set_) | |
e440a328 | 7934 | this->report_error(_("too many arguments")); |
7935 | break; | |
7936 | ||
7937 | case BUILTIN_OFFSETOF: | |
7938 | if (this->check_one_arg()) | |
7939 | { | |
7940 | Expression* arg = this->one_arg(); | |
7941 | if (arg->field_reference_expression() == NULL) | |
7942 | this->report_error(_("argument must be a field reference")); | |
7943 | } | |
7944 | break; | |
7945 | ||
7946 | case BUILTIN_COPY: | |
7947 | { | |
7948 | const Expression_list* args = this->args(); | |
7949 | if (args == NULL || args->size() < 2) | |
7950 | { | |
7951 | this->report_error(_("not enough arguments")); | |
7952 | break; | |
7953 | } | |
7954 | else if (args->size() > 2) | |
7955 | { | |
7956 | this->report_error(_("too many arguments")); | |
7957 | break; | |
7958 | } | |
7959 | Type* arg1_type = args->front()->type(); | |
7960 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 7961 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 7962 | { |
7963 | this->set_is_error(); | |
7964 | break; | |
7965 | } | |
e440a328 | 7966 | |
7967 | Type* e1; | |
411eb89e | 7968 | if (arg1_type->is_slice_type()) |
e440a328 | 7969 | e1 = arg1_type->array_type()->element_type(); |
7970 | else | |
7971 | { | |
7972 | this->report_error(_("left argument must be a slice")); | |
7973 | break; | |
7974 | } | |
7975 | ||
411eb89e | 7976 | if (arg2_type->is_slice_type()) |
60963afd | 7977 | { |
7978 | Type* e2 = arg2_type->array_type()->element_type(); | |
7979 | if (!Type::are_identical(e1, e2, true, NULL)) | |
7980 | this->report_error(_("element types must be the same")); | |
7981 | } | |
e440a328 | 7982 | else if (arg2_type->is_string_type()) |
e440a328 | 7983 | { |
60963afd | 7984 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
7985 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 7986 | } |
60963afd | 7987 | else |
7988 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 7989 | } |
7990 | break; | |
7991 | ||
7992 | case BUILTIN_APPEND: | |
7993 | { | |
7994 | const Expression_list* args = this->args(); | |
b0d311a1 | 7995 | if (args == NULL || args->size() < 2) |
e440a328 | 7996 | { |
7997 | this->report_error(_("not enough arguments")); | |
7998 | break; | |
7999 | } | |
0b7755ec | 8000 | if (args->size() > 2) |
8001 | { | |
8002 | this->report_error(_("too many arguments")); | |
8003 | break; | |
8004 | } | |
cd238b8d | 8005 | if (args->front()->type()->is_error() |
8006 | || args->back()->type()->is_error()) | |
6bebb39d | 8007 | { |
8008 | this->set_is_error(); | |
8009 | break; | |
8010 | } | |
cd238b8d | 8011 | |
8012 | Array_type* at = args->front()->type()->array_type(); | |
8013 | Type* e = at->element_type(); | |
4fd4fcf4 | 8014 | |
8015 | // The language permits appending a string to a []byte, as a | |
8016 | // special case. | |
8017 | if (args->back()->type()->is_string_type()) | |
8018 | { | |
60963afd | 8019 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 8020 | break; |
8021 | } | |
8022 | ||
19fd40c3 | 8023 | // The language says that the second argument must be |
8024 | // assignable to a slice of the element type of the first | |
8025 | // argument. We already know the first argument is a slice | |
8026 | // type. | |
cd238b8d | 8027 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 8028 | std::string reason; |
19fd40c3 | 8029 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 8030 | { |
8031 | if (reason.empty()) | |
19fd40c3 | 8032 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 8033 | else |
8034 | { | |
19fd40c3 | 8035 | error_at(this->location(), "argument 2 has invalid type (%s)", |
e440a328 | 8036 | reason.c_str()); |
8037 | this->set_is_error(); | |
8038 | } | |
8039 | } | |
8040 | break; | |
8041 | } | |
8042 | ||
8043 | case BUILTIN_REAL: | |
8044 | case BUILTIN_IMAG: | |
8045 | if (this->check_one_arg()) | |
8046 | { | |
8047 | if (this->one_arg()->type()->complex_type() == NULL) | |
8048 | this->report_error(_("argument must have complex type")); | |
8049 | } | |
8050 | break; | |
8051 | ||
48080209 | 8052 | case BUILTIN_COMPLEX: |
e440a328 | 8053 | { |
8054 | const Expression_list* args = this->args(); | |
8055 | if (args == NULL || args->size() < 2) | |
8056 | this->report_error(_("not enough arguments")); | |
8057 | else if (args->size() > 2) | |
8058 | this->report_error(_("too many arguments")); | |
8059 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8060 | || args->front()->type()->is_error() |
e440a328 | 8061 | || args->back()->is_error_expression() |
5c13bd80 | 8062 | || args->back()->type()->is_error()) |
e440a328 | 8063 | this->set_is_error(); |
8064 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8065 | args->back()->type(), true, NULL)) |
48080209 | 8066 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8067 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8068 | this->report_error(_("complex arguments must have " |
e440a328 | 8069 | "floating-point type")); |
8070 | } | |
8071 | break; | |
8072 | ||
8073 | default: | |
c3e6f413 | 8074 | go_unreachable(); |
e440a328 | 8075 | } |
8076 | } | |
8077 | ||
72666aed | 8078 | Expression* |
8079 | Builtin_call_expression::do_copy() | |
8080 | { | |
8081 | Call_expression* bce = | |
8082 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8083 | (this->args() == NULL |
8084 | ? NULL | |
8085 | : this->args()->copy()), | |
72666aed | 8086 | this->is_varargs(), |
8087 | this->location()); | |
8088 | ||
8089 | if (this->varargs_are_lowered()) | |
8090 | bce->set_varargs_are_lowered(); | |
8091 | return bce; | |
8092 | } | |
8093 | ||
ea664253 | 8094 | // Return the backend representation for a builtin function. |
e440a328 | 8095 | |
ea664253 | 8096 | Bexpression* |
8097 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8098 | { |
8099 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8100 | Location location = this->location(); |
a0d8874e | 8101 | |
8102 | if (this->is_erroneous_call()) | |
8103 | { | |
8104 | go_assert(saw_errors()); | |
8105 | return gogo->backend()->error_expression(); | |
8106 | } | |
8107 | ||
e440a328 | 8108 | switch (this->code_) |
8109 | { | |
8110 | case BUILTIN_INVALID: | |
8111 | case BUILTIN_NEW: | |
8112 | case BUILTIN_MAKE: | |
c3e6f413 | 8113 | go_unreachable(); |
e440a328 | 8114 | |
8115 | case BUILTIN_LEN: | |
8116 | case BUILTIN_CAP: | |
8117 | { | |
8118 | const Expression_list* args = this->args(); | |
c484d925 | 8119 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8120 | Expression* arg = args->front(); |
e440a328 | 8121 | Type* arg_type = arg->type(); |
0f914071 | 8122 | |
8123 | if (this->seen_) | |
8124 | { | |
c484d925 | 8125 | go_assert(saw_errors()); |
ea664253 | 8126 | return context->backend()->error_expression(); |
0f914071 | 8127 | } |
8128 | this->seen_ = true; | |
0f914071 | 8129 | this->seen_ = false; |
e440a328 | 8130 | if (arg_type->points_to() != NULL) |
8131 | { | |
8132 | arg_type = arg_type->points_to(); | |
c484d925 | 8133 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8134 | && !arg_type->is_slice_type()); |
2c809f8f | 8135 | arg = Expression::make_unary(OPERATOR_MULT, arg, location); |
e440a328 | 8136 | } |
8137 | ||
1b1f2abf | 8138 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8139 | Expression* val; |
e440a328 | 8140 | if (this->code_ == BUILTIN_LEN) |
8141 | { | |
8142 | if (arg_type->is_string_type()) | |
2c809f8f | 8143 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8144 | location); | |
e440a328 | 8145 | else if (arg_type->array_type() != NULL) |
0f914071 | 8146 | { |
8147 | if (this->seen_) | |
8148 | { | |
c484d925 | 8149 | go_assert(saw_errors()); |
ea664253 | 8150 | return context->backend()->error_expression(); |
0f914071 | 8151 | } |
8152 | this->seen_ = true; | |
2c809f8f | 8153 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8154 | this->seen_ = false; |
8155 | } | |
e440a328 | 8156 | else if (arg_type->map_type() != NULL) |
2c809f8f | 8157 | val = Runtime::make_call(Runtime::MAP_LEN, location, 1, arg); |
e440a328 | 8158 | else if (arg_type->channel_type() != NULL) |
2c809f8f | 8159 | val = Runtime::make_call(Runtime::CHAN_LEN, location, 1, arg); |
e440a328 | 8160 | else |
c3e6f413 | 8161 | go_unreachable(); |
e440a328 | 8162 | } |
8163 | else | |
8164 | { | |
8165 | if (arg_type->array_type() != NULL) | |
0f914071 | 8166 | { |
8167 | if (this->seen_) | |
8168 | { | |
c484d925 | 8169 | go_assert(saw_errors()); |
ea664253 | 8170 | return context->backend()->error_expression(); |
0f914071 | 8171 | } |
8172 | this->seen_ = true; | |
2c809f8f | 8173 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8174 | this->seen_ = false; |
8175 | } | |
e440a328 | 8176 | else if (arg_type->channel_type() != NULL) |
2c809f8f | 8177 | val = Runtime::make_call(Runtime::CHAN_CAP, location, 1, arg); |
e440a328 | 8178 | else |
c3e6f413 | 8179 | go_unreachable(); |
e440a328 | 8180 | } |
8181 | ||
2c809f8f | 8182 | return Expression::make_cast(int_type, val, |
ea664253 | 8183 | location)->get_backend(context); |
e440a328 | 8184 | } |
8185 | ||
8186 | case BUILTIN_PRINT: | |
8187 | case BUILTIN_PRINTLN: | |
8188 | { | |
8189 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
2c809f8f | 8190 | Expression* print_stmts = NULL; |
e440a328 | 8191 | |
8192 | const Expression_list* call_args = this->args(); | |
8193 | if (call_args != NULL) | |
8194 | { | |
8195 | for (Expression_list::const_iterator p = call_args->begin(); | |
8196 | p != call_args->end(); | |
8197 | ++p) | |
8198 | { | |
8199 | if (is_ln && p != call_args->begin()) | |
8200 | { | |
2c809f8f | 8201 | Expression* print_space = |
8202 | Runtime::make_call(Runtime::PRINT_SPACE, | |
8203 | this->location(), 0); | |
e440a328 | 8204 | |
2c809f8f | 8205 | print_stmts = |
8206 | Expression::make_compound(print_stmts, print_space, | |
8207 | location); | |
8208 | } | |
e440a328 | 8209 | |
2c809f8f | 8210 | Expression* arg = *p; |
8211 | Type* type = arg->type(); | |
8212 | Runtime::Function code; | |
e440a328 | 8213 | if (type->is_string_type()) |
2c809f8f | 8214 | code = Runtime::PRINT_STRING; |
e440a328 | 8215 | else if (type->integer_type() != NULL |
8216 | && type->integer_type()->is_unsigned()) | |
8217 | { | |
e440a328 | 8218 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 8219 | arg = Expression::make_cast(itype, arg, location); |
8220 | code = Runtime::PRINT_UINT64; | |
e440a328 | 8221 | } |
8222 | else if (type->integer_type() != NULL) | |
8223 | { | |
e440a328 | 8224 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 8225 | arg = Expression::make_cast(itype, arg, location); |
8226 | code = Runtime::PRINT_INT64; | |
e440a328 | 8227 | } |
8228 | else if (type->float_type() != NULL) | |
8229 | { | |
2c809f8f | 8230 | Type* dtype = Type::lookup_float_type("float64"); |
8231 | arg = Expression::make_cast(dtype, arg, location); | |
8232 | code = Runtime::PRINT_DOUBLE; | |
e440a328 | 8233 | } |
8234 | else if (type->complex_type() != NULL) | |
8235 | { | |
2c809f8f | 8236 | Type* ctype = Type::lookup_complex_type("complex128"); |
8237 | arg = Expression::make_cast(ctype, arg, location); | |
8238 | code = Runtime::PRINT_COMPLEX; | |
e440a328 | 8239 | } |
8240 | else if (type->is_boolean_type()) | |
2c809f8f | 8241 | code = Runtime::PRINT_BOOL; |
e440a328 | 8242 | else if (type->points_to() != NULL |
8243 | || type->channel_type() != NULL | |
8244 | || type->map_type() != NULL | |
8245 | || type->function_type() != NULL) | |
8246 | { | |
2c809f8f | 8247 | arg = Expression::make_cast(type, arg, location); |
8248 | code = Runtime::PRINT_POINTER; | |
e440a328 | 8249 | } |
8250 | else if (type->interface_type() != NULL) | |
8251 | { | |
8252 | if (type->interface_type()->is_empty()) | |
2c809f8f | 8253 | code = Runtime::PRINT_EMPTY_INTERFACE; |
e440a328 | 8254 | else |
2c809f8f | 8255 | code = Runtime::PRINT_INTERFACE; |
e440a328 | 8256 | } |
411eb89e | 8257 | else if (type->is_slice_type()) |
2c809f8f | 8258 | code = Runtime::PRINT_SLICE; |
e440a328 | 8259 | else |
cd238b8d | 8260 | { |
8261 | go_assert(saw_errors()); | |
ea664253 | 8262 | return context->backend()->error_expression(); |
cd238b8d | 8263 | } |
e440a328 | 8264 | |
2c809f8f | 8265 | Expression* call = Runtime::make_call(code, location, 1, arg); |
8266 | if (print_stmts == NULL) | |
8267 | print_stmts = call; | |
8268 | else | |
8269 | print_stmts = Expression::make_compound(print_stmts, call, | |
8270 | location); | |
e440a328 | 8271 | } |
8272 | } | |
8273 | ||
8274 | if (is_ln) | |
8275 | { | |
2c809f8f | 8276 | Expression* print_nl = |
8277 | Runtime::make_call(Runtime::PRINT_NL, location, 0); | |
8278 | if (print_stmts == NULL) | |
8279 | print_stmts = print_nl; | |
8280 | else | |
8281 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
8282 | location); | |
e440a328 | 8283 | } |
8284 | ||
32e3ff69 | 8285 | // There aren't any arguments to the print builtin. The compiler |
8286 | // issues a warning for this so we should avoid getting the backend | |
8287 | // representation for this call. Instead, perform a no-op. | |
8288 | if (print_stmts == NULL) | |
8289 | return context->backend()->boolean_constant_expression(false); | |
8290 | ||
ea664253 | 8291 | return print_stmts->get_backend(context); |
e440a328 | 8292 | } |
8293 | ||
8294 | case BUILTIN_PANIC: | |
8295 | { | |
8296 | const Expression_list* args = this->args(); | |
c484d925 | 8297 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8298 | Expression* arg = args->front(); |
b13c66cd | 8299 | Type *empty = |
823c7e3d | 8300 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 8301 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
8302 | ||
8303 | Expression* panic = | |
8304 | Runtime::make_call(Runtime::PANIC, location, 1, arg); | |
ea664253 | 8305 | return panic->get_backend(context); |
e440a328 | 8306 | } |
8307 | ||
8308 | case BUILTIN_RECOVER: | |
8309 | { | |
8310 | // The argument is set when building recover thunks. It's a | |
8311 | // boolean value which is true if we can recover a value now. | |
8312 | const Expression_list* args = this->args(); | |
c484d925 | 8313 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8314 | Expression* arg = args->front(); |
b13c66cd | 8315 | Type *empty = |
823c7e3d | 8316 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8317 | |
e440a328 | 8318 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 8319 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 8320 | |
8321 | // We need to handle a deferred call to recover specially, | |
8322 | // because it changes whether it can recover a panic or not. | |
8323 | // See test7 in test/recover1.go. | |
2c809f8f | 8324 | Expression* recover = Runtime::make_call((this->is_deferred() |
8325 | ? Runtime::DEFERRED_RECOVER | |
8326 | : Runtime::RECOVER), | |
8327 | location, 0); | |
8328 | Expression* cond = | |
8329 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 8330 | return cond->get_backend(context); |
e440a328 | 8331 | } |
8332 | ||
8333 | case BUILTIN_CLOSE: | |
e440a328 | 8334 | { |
8335 | const Expression_list* args = this->args(); | |
c484d925 | 8336 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8337 | Expression* arg = args->front(); |
2c809f8f | 8338 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
8339 | 1, arg); | |
ea664253 | 8340 | return close->get_backend(context); |
e440a328 | 8341 | } |
8342 | ||
8343 | case BUILTIN_SIZEOF: | |
8344 | case BUILTIN_OFFSETOF: | |
8345 | case BUILTIN_ALIGNOF: | |
8346 | { | |
0c77715b | 8347 | Numeric_constant nc; |
8348 | unsigned long val; | |
8349 | if (!this->numeric_constant_value(&nc) | |
8350 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8351 | { |
c484d925 | 8352 | go_assert(saw_errors()); |
ea664253 | 8353 | return context->backend()->error_expression(); |
7f1d9abd | 8354 | } |
7ba86326 | 8355 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 8356 | mpz_t ival; |
8357 | nc.get_int(&ival); | |
8358 | Expression* int_cst = | |
e67508fa | 8359 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 8360 | mpz_clear(ival); |
ea664253 | 8361 | return int_cst->get_backend(context); |
e440a328 | 8362 | } |
8363 | ||
8364 | case BUILTIN_COPY: | |
8365 | { | |
8366 | const Expression_list* args = this->args(); | |
c484d925 | 8367 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8368 | Expression* arg1 = args->front(); |
8369 | Expression* arg2 = args->back(); | |
8370 | ||
e440a328 | 8371 | Type* arg1_type = arg1->type(); |
8372 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 8373 | go_assert(arg1->is_variable()); |
2c809f8f | 8374 | Expression* arg1_val = at->get_value_pointer(gogo, arg1); |
8375 | Expression* arg1_len = at->get_length(gogo, arg1); | |
e440a328 | 8376 | |
8377 | Type* arg2_type = arg2->type(); | |
2c809f8f | 8378 | go_assert(arg2->is_variable()); |
8379 | Expression* arg2_val; | |
8380 | Expression* arg2_len; | |
411eb89e | 8381 | if (arg2_type->is_slice_type()) |
e440a328 | 8382 | { |
8383 | at = arg2_type->array_type(); | |
2c809f8f | 8384 | arg2_val = at->get_value_pointer(gogo, arg2); |
8385 | arg2_len = at->get_length(gogo, arg2); | |
e440a328 | 8386 | } |
8387 | else | |
8388 | { | |
2c809f8f | 8389 | go_assert(arg2->is_variable()); |
8390 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, | |
8391 | location); | |
8392 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8393 | location); | |
e440a328 | 8394 | } |
2c809f8f | 8395 | Expression* cond = |
8396 | Expression::make_binary(OPERATOR_LT, arg1_len, arg2_len, location); | |
8397 | Expression* length = | |
8398 | Expression::make_conditional(cond, arg1_len, arg2_len, location); | |
e440a328 | 8399 | |
8400 | Type* element_type = at->element_type(); | |
2a305b85 | 8401 | int64_t element_size; |
8402 | bool ok = element_type->backend_type_size(gogo, &element_size); | |
8403 | if (!ok) | |
8404 | { | |
8405 | go_assert(saw_errors()); | |
8406 | return gogo->backend()->error_expression(); | |
8407 | } | |
8408 | ||
3f378015 | 8409 | Expression* size_expr = Expression::make_integer_int64(element_size, |
8410 | length->type(), | |
8411 | location); | |
2c809f8f | 8412 | Expression* bytecount = |
8413 | Expression::make_binary(OPERATOR_MULT, size_expr, length, location); | |
8414 | Expression* copy = Runtime::make_call(Runtime::COPY, location, 3, | |
8415 | arg1_val, arg2_val, bytecount); | |
8416 | ||
8417 | Expression* compound = Expression::make_compound(copy, length, location); | |
ea664253 | 8418 | return compound->get_backend(context); |
e440a328 | 8419 | } |
8420 | ||
8421 | case BUILTIN_APPEND: | |
8422 | { | |
8423 | const Expression_list* args = this->args(); | |
c484d925 | 8424 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8425 | Expression* arg1 = args->front(); |
8426 | Expression* arg2 = args->back(); | |
8427 | ||
9d44fbe3 | 8428 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8429 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8430 | |
2c809f8f | 8431 | go_assert(arg2->is_variable()); |
8432 | Expression* arg2_val; | |
8433 | Expression* arg2_len; | |
3f378015 | 8434 | int64_t size; |
4fd4fcf4 | 8435 | if (arg2->type()->is_string_type() |
60963afd | 8436 | && element_type->integer_type() != NULL |
8437 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8438 | { |
2c809f8f | 8439 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, |
8440 | location); | |
8441 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8442 | location); | |
e67508fa | 8443 | size = 1; |
4fd4fcf4 | 8444 | } |
8445 | else | |
8446 | { | |
2c809f8f | 8447 | arg2_val = at->get_value_pointer(gogo, arg2); |
8448 | arg2_len = at->get_length(gogo, arg2); | |
2a305b85 | 8449 | bool ok = element_type->backend_type_size(gogo, &size); |
8450 | if (!ok) | |
8451 | { | |
8452 | go_assert(saw_errors()); | |
8453 | return gogo->backend()->error_expression(); | |
8454 | } | |
4fd4fcf4 | 8455 | } |
2c809f8f | 8456 | Expression* element_size = |
3f378015 | 8457 | Expression::make_integer_int64(size, NULL, location); |
2c809f8f | 8458 | |
8459 | Expression* append = Runtime::make_call(Runtime::APPEND, location, 4, | |
8460 | arg1, arg2_val, arg2_len, | |
8461 | element_size); | |
8462 | append = Expression::make_unsafe_cast(arg1->type(), append, location); | |
ea664253 | 8463 | return append->get_backend(context); |
e440a328 | 8464 | } |
8465 | ||
8466 | case BUILTIN_REAL: | |
8467 | case BUILTIN_IMAG: | |
8468 | { | |
8469 | const Expression_list* args = this->args(); | |
c484d925 | 8470 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8471 | |
8472 | Bexpression* ret; | |
ea664253 | 8473 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 8474 | if (this->code_ == BUILTIN_REAL) |
8475 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
8476 | else | |
8477 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 8478 | return ret; |
e440a328 | 8479 | } |
8480 | ||
48080209 | 8481 | case BUILTIN_COMPLEX: |
e440a328 | 8482 | { |
8483 | const Expression_list* args = this->args(); | |
c484d925 | 8484 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 8485 | Bexpression* breal = args->front()->get_backend(context); |
8486 | Bexpression* bimag = args->back()->get_backend(context); | |
8487 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 8488 | } |
8489 | ||
8490 | default: | |
c3e6f413 | 8491 | go_unreachable(); |
e440a328 | 8492 | } |
8493 | } | |
8494 | ||
8495 | // We have to support exporting a builtin call expression, because | |
8496 | // code can set a constant to the result of a builtin expression. | |
8497 | ||
8498 | void | |
8499 | Builtin_call_expression::do_export(Export* exp) const | |
8500 | { | |
0c77715b | 8501 | Numeric_constant nc; |
8502 | if (!this->numeric_constant_value(&nc)) | |
8503 | { | |
8504 | error_at(this->location(), "value is not constant"); | |
8505 | return; | |
8506 | } | |
e440a328 | 8507 | |
0c77715b | 8508 | if (nc.is_int()) |
e440a328 | 8509 | { |
0c77715b | 8510 | mpz_t val; |
8511 | nc.get_int(&val); | |
e440a328 | 8512 | Integer_expression::export_integer(exp, val); |
0c77715b | 8513 | mpz_clear(val); |
e440a328 | 8514 | } |
0c77715b | 8515 | else if (nc.is_float()) |
e440a328 | 8516 | { |
8517 | mpfr_t fval; | |
0c77715b | 8518 | nc.get_float(&fval); |
8519 | Float_expression::export_float(exp, fval); | |
e440a328 | 8520 | mpfr_clear(fval); |
8521 | } | |
0c77715b | 8522 | else if (nc.is_complex()) |
e440a328 | 8523 | { |
fcbea5e4 | 8524 | mpc_t cval; |
8525 | nc.get_complex(&cval); | |
8526 | Complex_expression::export_complex(exp, cval); | |
8527 | mpc_clear(cval); | |
e440a328 | 8528 | } |
0c77715b | 8529 | else |
8530 | go_unreachable(); | |
e440a328 | 8531 | |
8532 | // A trailing space lets us reliably identify the end of the number. | |
8533 | exp->write_c_string(" "); | |
8534 | } | |
8535 | ||
8536 | // Class Call_expression. | |
8537 | ||
8381eda7 | 8538 | // A Go function can be viewed in a couple of different ways. The |
8539 | // code of a Go function becomes a backend function with parameters | |
8540 | // whose types are simply the backend representation of the Go types. | |
8541 | // If there are multiple results, they are returned as a backend | |
8542 | // struct. | |
8543 | ||
8544 | // However, when Go code refers to a function other than simply | |
8545 | // calling it, the backend type of that function is actually a struct. | |
8546 | // The first field of the struct points to the Go function code | |
8547 | // (sometimes a wrapper as described below). The remaining fields | |
8548 | // hold addresses of closed-over variables. This struct is called a | |
8549 | // closure. | |
8550 | ||
8551 | // There are a few cases to consider. | |
8552 | ||
8553 | // A direct function call of a known function in package scope. In | |
8554 | // this case there are no closed-over variables, and we know the name | |
8555 | // of the function code. We can simply produce a backend call to the | |
8556 | // function directly, and not worry about the closure. | |
8557 | ||
8558 | // A direct function call of a known function literal. In this case | |
8559 | // we know the function code and we know the closure. We generate the | |
8560 | // function code such that it expects an additional final argument of | |
8561 | // the closure type. We pass the closure as the last argument, after | |
8562 | // the other arguments. | |
8563 | ||
8564 | // An indirect function call. In this case we have a closure. We | |
8565 | // load the pointer to the function code from the first field of the | |
8566 | // closure. We pass the address of the closure as the last argument. | |
8567 | ||
8568 | // A call to a method of an interface. Type methods are always at | |
8569 | // package scope, so we call the function directly, and don't worry | |
8570 | // about the closure. | |
8571 | ||
8572 | // This means that for a function at package scope we have two cases. | |
8573 | // One is the direct call, which has no closure. The other is the | |
8574 | // indirect call, which does have a closure. We can't simply ignore | |
8575 | // the closure, even though it is the last argument, because that will | |
8576 | // fail on targets where the function pops its arguments. So when | |
8577 | // generating a closure for a package-scope function we set the | |
8578 | // function code pointer in the closure to point to a wrapper | |
8579 | // function. This wrapper function accepts a final argument that | |
8580 | // points to the closure, ignores it, and calls the real function as a | |
8581 | // direct function call. This wrapper will normally be efficient, and | |
8582 | // can often simply be a tail call to the real function. | |
8583 | ||
8584 | // We don't use GCC's static chain pointer because 1) we don't need | |
8585 | // it; 2) GCC only permits using a static chain to call a known | |
8586 | // function, so we can't use it for an indirect call anyhow. Since we | |
8587 | // can't use it for an indirect call, we may as well not worry about | |
8588 | // using it for a direct call either. | |
8589 | ||
8590 | // We pass the closure last rather than first because it means that | |
8591 | // the function wrapper we put into a closure for a package-scope | |
8592 | // function can normally just be a tail call to the real function. | |
8593 | ||
8594 | // For method expressions we generate a wrapper that loads the | |
8595 | // receiver from the closure and then calls the method. This | |
8596 | // unfortunately forces reshuffling the arguments, since there is a | |
8597 | // new first argument, but we can't avoid reshuffling either for | |
8598 | // method expressions or for indirect calls of package-scope | |
8599 | // functions, and since the latter are more common we reshuffle for | |
8600 | // method expressions. | |
8601 | ||
8602 | // Note that the Go code retains the Go types. The extra final | |
8603 | // argument only appears when we convert to the backend | |
8604 | // representation. | |
8605 | ||
e440a328 | 8606 | // Traversal. |
8607 | ||
8608 | int | |
8609 | Call_expression::do_traverse(Traverse* traverse) | |
8610 | { | |
8611 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8612 | return TRAVERSE_EXIT; | |
8613 | if (this->args_ != NULL) | |
8614 | { | |
8615 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8616 | return TRAVERSE_EXIT; | |
8617 | } | |
8618 | return TRAVERSE_CONTINUE; | |
8619 | } | |
8620 | ||
8621 | // Lower a call statement. | |
8622 | ||
8623 | Expression* | |
ceeb4318 | 8624 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
8625 | Statement_inserter* inserter, int) | |
e440a328 | 8626 | { |
b13c66cd | 8627 | Location loc = this->location(); |
09ea332d | 8628 | |
ceeb4318 | 8629 | // A type cast can look like a function call. |
e440a328 | 8630 | if (this->fn_->is_type_expression() |
8631 | && this->args_ != NULL | |
8632 | && this->args_->size() == 1) | |
8633 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 8634 | loc); |
e440a328 | 8635 | |
88f06749 | 8636 | // Because do_type will return an error type and thus prevent future |
8637 | // errors, check for that case now to ensure that the error gets | |
8638 | // reported. | |
37448b10 | 8639 | Function_type* fntype = this->get_function_type(); |
8640 | if (fntype == NULL) | |
88f06749 | 8641 | { |
8642 | if (!this->fn_->type()->is_error()) | |
8643 | this->report_error(_("expected function")); | |
5f1045b5 | 8644 | this->set_is_error(); |
8645 | return this; | |
88f06749 | 8646 | } |
8647 | ||
e440a328 | 8648 | // Handle an argument which is a call to a function which returns |
8649 | // multiple results. | |
8650 | if (this->args_ != NULL | |
8651 | && this->args_->size() == 1 | |
37448b10 | 8652 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 8653 | { |
e440a328 | 8654 | size_t rc = this->args_->front()->call_expression()->result_count(); |
8655 | if (rc > 1 | |
37448b10 | 8656 | && ((fntype->parameters() != NULL |
8657 | && (fntype->parameters()->size() == rc | |
8658 | || (fntype->is_varargs() | |
8659 | && fntype->parameters()->size() - 1 <= rc))) | |
8660 | || fntype->is_builtin())) | |
e440a328 | 8661 | { |
8662 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 8663 | call->set_is_multi_value_arg(); |
c33af8e4 | 8664 | if (this->is_varargs_) |
8665 | { | |
8666 | // It is not clear which result of a multiple result call | |
8667 | // the ellipsis operator should be applied to. If we unpack the | |
8668 | // the call into its individual results here, the ellipsis will be | |
8669 | // applied to the last result. | |
8670 | error_at(call->location(), | |
8671 | _("multiple-value argument in single-value context")); | |
8672 | return Expression::make_error(call->location()); | |
8673 | } | |
8674 | ||
e440a328 | 8675 | Expression_list* args = new Expression_list; |
8676 | for (size_t i = 0; i < rc; ++i) | |
8677 | args->push_back(Expression::make_call_result(call, i)); | |
8678 | // We can't return a new call expression here, because this | |
42535814 | 8679 | // one may be referenced by Call_result expressions. We |
8680 | // also can't delete the old arguments, because we may still | |
8681 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8682 | this->args_ = args; |
8683 | } | |
8684 | } | |
8685 | ||
37448b10 | 8686 | // Recognize a call to a builtin function. |
8687 | if (fntype->is_builtin()) | |
8688 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
8689 | this->is_varargs_, loc); | |
8690 | ||
ceeb4318 | 8691 | // If this call returns multiple results, create a temporary |
8692 | // variable for each result. | |
8693 | size_t rc = this->result_count(); | |
8694 | if (rc > 1 && this->results_ == NULL) | |
8695 | { | |
8696 | std::vector<Temporary_statement*>* temps = | |
8697 | new std::vector<Temporary_statement*>; | |
8698 | temps->reserve(rc); | |
37448b10 | 8699 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 8700 | for (Typed_identifier_list::const_iterator p = results->begin(); |
8701 | p != results->end(); | |
8702 | ++p) | |
8703 | { | |
8704 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 8705 | NULL, loc); |
ceeb4318 | 8706 | inserter->insert(temp); |
8707 | temps->push_back(temp); | |
8708 | } | |
8709 | this->results_ = temps; | |
8710 | } | |
8711 | ||
e440a328 | 8712 | // Handle a call to a varargs function by packaging up the extra |
8713 | // parameters. | |
37448b10 | 8714 | if (fntype->is_varargs()) |
e440a328 | 8715 | { |
e440a328 | 8716 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 8717 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 8718 | Type* varargs_type = parameters->back().type(); |
09ea332d | 8719 | this->lower_varargs(gogo, function, inserter, varargs_type, |
8720 | parameters->size()); | |
8721 | } | |
8722 | ||
8723 | // If this is call to a method, call the method directly passing the | |
8724 | // object as the first parameter. | |
8725 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8726 | if (bme != NULL) | |
8727 | { | |
0afbb937 | 8728 | Named_object* methodfn = bme->function(); |
09ea332d | 8729 | Expression* first_arg = bme->first_argument(); |
8730 | ||
8731 | // We always pass a pointer when calling a method. | |
8732 | if (first_arg->type()->points_to() == NULL | |
8733 | && !first_arg->type()->is_error()) | |
8734 | { | |
8735 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
8736 | // We may need to create a temporary variable so that we can | |
8737 | // take the address. We can't do that here because it will | |
8738 | // mess up the order of evaluation. | |
8739 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
8740 | ue->set_create_temp(); | |
8741 | } | |
8742 | ||
8743 | // If we are calling a method which was inherited from an | |
8744 | // embedded struct, and the method did not get a stub, then the | |
8745 | // first type may be wrong. | |
8746 | Type* fatype = bme->first_argument_type(); | |
8747 | if (fatype != NULL) | |
8748 | { | |
8749 | if (fatype->points_to() == NULL) | |
8750 | fatype = Type::make_pointer_type(fatype); | |
8751 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
8752 | } | |
8753 | ||
8754 | Expression_list* new_args = new Expression_list(); | |
8755 | new_args->push_back(first_arg); | |
8756 | if (this->args_ != NULL) | |
8757 | { | |
8758 | for (Expression_list::const_iterator p = this->args_->begin(); | |
8759 | p != this->args_->end(); | |
8760 | ++p) | |
8761 | new_args->push_back(*p); | |
8762 | } | |
8763 | ||
8764 | // We have to change in place because this structure may be | |
8765 | // referenced by Call_result_expressions. We can't delete the | |
8766 | // old arguments, because we may be traversing them up in some | |
8767 | // caller. FIXME. | |
8768 | this->args_ = new_args; | |
0afbb937 | 8769 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 8770 | bme->location()); |
e440a328 | 8771 | } |
8772 | ||
8773 | return this; | |
8774 | } | |
8775 | ||
8776 | // Lower a call to a varargs function. FUNCTION is the function in | |
8777 | // which the call occurs--it's not the function we are calling. | |
8778 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8779 | // PARAM_COUNT is the number of parameters of the function we are | |
8780 | // calling; the last of these parameters will be the varargs | |
8781 | // parameter. | |
8782 | ||
09ea332d | 8783 | void |
e440a328 | 8784 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 8785 | Statement_inserter* inserter, |
e440a328 | 8786 | Type* varargs_type, size_t param_count) |
8787 | { | |
8788 | if (this->varargs_are_lowered_) | |
09ea332d | 8789 | return; |
e440a328 | 8790 | |
b13c66cd | 8791 | Location loc = this->location(); |
e440a328 | 8792 | |
c484d925 | 8793 | go_assert(param_count > 0); |
411eb89e | 8794 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 8795 | |
8796 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8797 | if (arg_count < param_count - 1) | |
8798 | { | |
8799 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 8800 | return; |
e440a328 | 8801 | } |
8802 | ||
8803 | Expression_list* old_args = this->args_; | |
8804 | Expression_list* new_args = new Expression_list(); | |
8805 | bool push_empty_arg = false; | |
8806 | if (old_args == NULL || old_args->empty()) | |
8807 | { | |
c484d925 | 8808 | go_assert(param_count == 1); |
e440a328 | 8809 | push_empty_arg = true; |
8810 | } | |
8811 | else | |
8812 | { | |
8813 | Expression_list::const_iterator pa; | |
8814 | int i = 1; | |
8815 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8816 | { | |
8817 | if (static_cast<size_t>(i) == param_count) | |
8818 | break; | |
8819 | new_args->push_back(*pa); | |
8820 | } | |
8821 | ||
8822 | // We have reached the varargs parameter. | |
8823 | ||
8824 | bool issued_error = false; | |
8825 | if (pa == old_args->end()) | |
8826 | push_empty_arg = true; | |
8827 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8828 | new_args->push_back(*pa); | |
8829 | else if (this->is_varargs_) | |
8830 | { | |
a6645f74 | 8831 | if ((*pa)->type()->is_slice_type()) |
8832 | this->report_error(_("too many arguments")); | |
8833 | else | |
8834 | { | |
8835 | error_at(this->location(), | |
8836 | _("invalid use of %<...%> with non-slice")); | |
8837 | this->set_is_error(); | |
8838 | } | |
09ea332d | 8839 | return; |
e440a328 | 8840 | } |
e440a328 | 8841 | else |
8842 | { | |
8843 | Type* element_type = varargs_type->array_type()->element_type(); | |
8844 | Expression_list* vals = new Expression_list; | |
8845 | for (; pa != old_args->end(); ++pa, ++i) | |
8846 | { | |
8847 | // Check types here so that we get a better message. | |
8848 | Type* patype = (*pa)->type(); | |
b13c66cd | 8849 | Location paloc = (*pa)->location(); |
e440a328 | 8850 | if (!this->check_argument_type(i, element_type, patype, |
8851 | paloc, issued_error)) | |
8852 | continue; | |
8853 | vals->push_back(*pa); | |
8854 | } | |
8855 | Expression* val = | |
8856 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
09ea332d | 8857 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 8858 | new_args->push_back(val); |
8859 | } | |
8860 | } | |
8861 | ||
8862 | if (push_empty_arg) | |
8863 | new_args->push_back(Expression::make_nil(loc)); | |
8864 | ||
8865 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 8866 | // be referenced by Call_result expressions. FIXME. We can't |
8867 | // delete OLD_ARGS because we may have both a Call_expression and a | |
8868 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 8869 | this->args_ = new_args; |
8870 | this->varargs_are_lowered_ = true; | |
e440a328 | 8871 | } |
8872 | ||
2c809f8f | 8873 | // Flatten a call with multiple results into a temporary. |
8874 | ||
8875 | Expression* | |
b8e86a51 | 8876 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
8877 | Statement_inserter* inserter) | |
2c809f8f | 8878 | { |
5bf8be8b | 8879 | if (this->is_erroneous_call()) |
8880 | { | |
8881 | go_assert(saw_errors()); | |
8882 | return Expression::make_error(this->location()); | |
8883 | } | |
b8e86a51 | 8884 | |
91c0fd76 | 8885 | if (this->is_flattened_) |
8886 | return this; | |
8887 | this->is_flattened_ = true; | |
8888 | ||
b8e86a51 | 8889 | // Add temporary variables for all arguments that require type |
8890 | // conversion. | |
8891 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 8892 | if (fntype == NULL) |
8893 | { | |
8894 | go_assert(saw_errors()); | |
8895 | return this; | |
8896 | } | |
b8e86a51 | 8897 | if (this->args_ != NULL && !this->args_->empty() |
8898 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
8899 | { | |
8900 | bool is_interface_method = | |
8901 | this->fn_->interface_field_reference_expression() != NULL; | |
8902 | ||
8903 | Expression_list *args = new Expression_list(); | |
8904 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
8905 | Expression_list::const_iterator pa = this->args_->begin(); | |
8906 | if (!is_interface_method && fntype->is_method()) | |
8907 | { | |
8908 | // The receiver argument. | |
8909 | args->push_back(*pa); | |
8910 | ++pa; | |
8911 | } | |
8912 | for (; pa != this->args_->end(); ++pa, ++pp) | |
8913 | { | |
8914 | go_assert(pp != fntype->parameters()->end()); | |
8915 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
8916 | args->push_back(*pa); | |
8917 | else | |
8918 | { | |
8919 | Location loc = (*pa)->location(); | |
8ba8cc87 | 8920 | Expression* arg = *pa; |
8921 | if (!arg->is_variable()) | |
8922 | { | |
8923 | Temporary_statement *temp = | |
8924 | Statement::make_temporary(NULL, arg, loc); | |
8925 | inserter->insert(temp); | |
8926 | arg = Expression::make_temporary_reference(temp, loc); | |
8927 | } | |
8928 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
8929 | loc); | |
8930 | args->push_back(arg); | |
b8e86a51 | 8931 | } |
8932 | } | |
8933 | delete this->args_; | |
8934 | this->args_ = args; | |
8935 | } | |
8936 | ||
2c809f8f | 8937 | size_t rc = this->result_count(); |
8938 | if (rc > 1 && this->call_temp_ == NULL) | |
8939 | { | |
8940 | Struct_field_list* sfl = new Struct_field_list(); | |
8941 | Function_type* fntype = this->get_function_type(); | |
8942 | const Typed_identifier_list* results = fntype->results(); | |
8943 | Location loc = this->location(); | |
8944 | ||
8945 | int i = 0; | |
8946 | char buf[10]; | |
8947 | for (Typed_identifier_list::const_iterator p = results->begin(); | |
8948 | p != results->end(); | |
8949 | ++p, ++i) | |
8950 | { | |
8951 | snprintf(buf, sizeof buf, "res%d", i); | |
8952 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
8953 | } | |
8954 | ||
8955 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
8956 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
8957 | inserter->insert(this->call_temp_); | |
8958 | } | |
8959 | ||
8960 | return this; | |
8961 | } | |
8962 | ||
ceeb4318 | 8963 | // Get the function type. This can return NULL in error cases. |
e440a328 | 8964 | |
8965 | Function_type* | |
8966 | Call_expression::get_function_type() const | |
8967 | { | |
8968 | return this->fn_->type()->function_type(); | |
8969 | } | |
8970 | ||
8971 | // Return the number of values which this call will return. | |
8972 | ||
8973 | size_t | |
8974 | Call_expression::result_count() const | |
8975 | { | |
8976 | const Function_type* fntype = this->get_function_type(); | |
8977 | if (fntype == NULL) | |
8978 | return 0; | |
8979 | if (fntype->results() == NULL) | |
8980 | return 0; | |
8981 | return fntype->results()->size(); | |
8982 | } | |
8983 | ||
ceeb4318 | 8984 | // Return the temporary which holds a result. |
8985 | ||
8986 | Temporary_statement* | |
8987 | Call_expression::result(size_t i) const | |
8988 | { | |
cd238b8d | 8989 | if (this->results_ == NULL || this->results_->size() <= i) |
8990 | { | |
8991 | go_assert(saw_errors()); | |
8992 | return NULL; | |
8993 | } | |
ceeb4318 | 8994 | return (*this->results_)[i]; |
8995 | } | |
8996 | ||
1373401e | 8997 | // Set the number of results expected from a call expression. |
8998 | ||
8999 | void | |
9000 | Call_expression::set_expected_result_count(size_t count) | |
9001 | { | |
9002 | go_assert(this->expected_result_count_ == 0); | |
9003 | this->expected_result_count_ = count; | |
9004 | } | |
9005 | ||
e440a328 | 9006 | // Return whether this is a call to the predeclared function recover. |
9007 | ||
9008 | bool | |
9009 | Call_expression::is_recover_call() const | |
9010 | { | |
9011 | return this->do_is_recover_call(); | |
9012 | } | |
9013 | ||
9014 | // Set the argument to the recover function. | |
9015 | ||
9016 | void | |
9017 | Call_expression::set_recover_arg(Expression* arg) | |
9018 | { | |
9019 | this->do_set_recover_arg(arg); | |
9020 | } | |
9021 | ||
9022 | // Virtual functions also implemented by Builtin_call_expression. | |
9023 | ||
9024 | bool | |
9025 | Call_expression::do_is_recover_call() const | |
9026 | { | |
9027 | return false; | |
9028 | } | |
9029 | ||
9030 | void | |
9031 | Call_expression::do_set_recover_arg(Expression*) | |
9032 | { | |
c3e6f413 | 9033 | go_unreachable(); |
e440a328 | 9034 | } |
9035 | ||
ceeb4318 | 9036 | // We have found an error with this call expression; return true if |
9037 | // we should report it. | |
9038 | ||
9039 | bool | |
9040 | Call_expression::issue_error() | |
9041 | { | |
9042 | if (this->issued_error_) | |
9043 | return false; | |
9044 | else | |
9045 | { | |
9046 | this->issued_error_ = true; | |
9047 | return true; | |
9048 | } | |
9049 | } | |
9050 | ||
5bf8be8b | 9051 | // Whether or not this call contains errors, either in the call or the |
9052 | // arguments to the call. | |
9053 | ||
9054 | bool | |
9055 | Call_expression::is_erroneous_call() | |
9056 | { | |
9057 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9058 | return true; | |
9059 | ||
9060 | if (this->args() == NULL) | |
9061 | return false; | |
9062 | for (Expression_list::iterator pa = this->args()->begin(); | |
9063 | pa != this->args()->end(); | |
9064 | ++pa) | |
9065 | { | |
9066 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9067 | return true; | |
9068 | } | |
9069 | return false; | |
9070 | } | |
9071 | ||
e440a328 | 9072 | // Get the type. |
9073 | ||
9074 | Type* | |
9075 | Call_expression::do_type() | |
9076 | { | |
9077 | if (this->type_ != NULL) | |
9078 | return this->type_; | |
9079 | ||
9080 | Type* ret; | |
9081 | Function_type* fntype = this->get_function_type(); | |
9082 | if (fntype == NULL) | |
9083 | return Type::make_error_type(); | |
9084 | ||
9085 | const Typed_identifier_list* results = fntype->results(); | |
9086 | if (results == NULL) | |
9087 | ret = Type::make_void_type(); | |
9088 | else if (results->size() == 1) | |
9089 | ret = results->begin()->type(); | |
9090 | else | |
9091 | ret = Type::make_call_multiple_result_type(this); | |
9092 | ||
9093 | this->type_ = ret; | |
9094 | ||
9095 | return this->type_; | |
9096 | } | |
9097 | ||
9098 | // Determine types for a call expression. We can use the function | |
9099 | // parameter types to set the types of the arguments. | |
9100 | ||
9101 | void | |
9102 | Call_expression::do_determine_type(const Type_context*) | |
9103 | { | |
fb94b0ca | 9104 | if (!this->determining_types()) |
9105 | return; | |
9106 | ||
e440a328 | 9107 | this->fn_->determine_type_no_context(); |
9108 | Function_type* fntype = this->get_function_type(); | |
9109 | const Typed_identifier_list* parameters = NULL; | |
9110 | if (fntype != NULL) | |
9111 | parameters = fntype->parameters(); | |
9112 | if (this->args_ != NULL) | |
9113 | { | |
9114 | Typed_identifier_list::const_iterator pt; | |
9115 | if (parameters != NULL) | |
9116 | pt = parameters->begin(); | |
09ea332d | 9117 | bool first = true; |
e440a328 | 9118 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9119 | pa != this->args_->end(); | |
9120 | ++pa) | |
9121 | { | |
09ea332d | 9122 | if (first) |
9123 | { | |
9124 | first = false; | |
9125 | // If this is a method, the first argument is the | |
9126 | // receiver. | |
9127 | if (fntype != NULL && fntype->is_method()) | |
9128 | { | |
9129 | Type* rtype = fntype->receiver()->type(); | |
9130 | // The receiver is always passed as a pointer. | |
9131 | if (rtype->points_to() == NULL) | |
9132 | rtype = Type::make_pointer_type(rtype); | |
9133 | Type_context subcontext(rtype, false); | |
9134 | (*pa)->determine_type(&subcontext); | |
9135 | continue; | |
9136 | } | |
9137 | } | |
9138 | ||
e440a328 | 9139 | if (parameters != NULL && pt != parameters->end()) |
9140 | { | |
9141 | Type_context subcontext(pt->type(), false); | |
9142 | (*pa)->determine_type(&subcontext); | |
9143 | ++pt; | |
9144 | } | |
9145 | else | |
9146 | (*pa)->determine_type_no_context(); | |
9147 | } | |
9148 | } | |
9149 | } | |
9150 | ||
fb94b0ca | 9151 | // Called when determining types for a Call_expression. Return true |
9152 | // if we should go ahead, false if they have already been determined. | |
9153 | ||
9154 | bool | |
9155 | Call_expression::determining_types() | |
9156 | { | |
9157 | if (this->types_are_determined_) | |
9158 | return false; | |
9159 | else | |
9160 | { | |
9161 | this->types_are_determined_ = true; | |
9162 | return true; | |
9163 | } | |
9164 | } | |
9165 | ||
e440a328 | 9166 | // Check types for parameter I. |
9167 | ||
9168 | bool | |
9169 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9170 | const Type* argument_type, | |
b13c66cd | 9171 | Location argument_location, |
e440a328 | 9172 | bool issued_error) |
9173 | { | |
9174 | std::string reason; | |
1eae365b | 9175 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 9176 | { |
9177 | if (!issued_error) | |
9178 | { | |
9179 | if (reason.empty()) | |
9180 | error_at(argument_location, "argument %d has incompatible type", i); | |
9181 | else | |
9182 | error_at(argument_location, | |
9183 | "argument %d has incompatible type (%s)", | |
9184 | i, reason.c_str()); | |
9185 | } | |
9186 | this->set_is_error(); | |
9187 | return false; | |
9188 | } | |
9189 | return true; | |
9190 | } | |
9191 | ||
9192 | // Check types. | |
9193 | ||
9194 | void | |
9195 | Call_expression::do_check_types(Gogo*) | |
9196 | { | |
a6645f74 | 9197 | if (this->classification() == EXPRESSION_ERROR) |
9198 | return; | |
9199 | ||
e440a328 | 9200 | Function_type* fntype = this->get_function_type(); |
9201 | if (fntype == NULL) | |
9202 | { | |
5c13bd80 | 9203 | if (!this->fn_->type()->is_error()) |
e440a328 | 9204 | this->report_error(_("expected function")); |
9205 | return; | |
9206 | } | |
9207 | ||
1373401e | 9208 | if (this->expected_result_count_ != 0 |
9209 | && this->expected_result_count_ != this->result_count()) | |
9210 | { | |
9211 | if (this->issue_error()) | |
9212 | this->report_error(_("function result count mismatch")); | |
9213 | this->set_is_error(); | |
9214 | return; | |
9215 | } | |
9216 | ||
09ea332d | 9217 | bool is_method = fntype->is_method(); |
9218 | if (is_method) | |
e440a328 | 9219 | { |
09ea332d | 9220 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9221 | Type* rtype = fntype->receiver()->type(); | |
9222 | Expression* first_arg = this->args_->front(); | |
1eae365b | 9223 | // We dereference the values since receivers are always passed |
9224 | // as pointers. | |
09ea332d | 9225 | std::string reason; |
1eae365b | 9226 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
9227 | &reason)) | |
e440a328 | 9228 | { |
09ea332d | 9229 | if (reason.empty()) |
9230 | this->report_error(_("incompatible type for receiver")); | |
9231 | else | |
e440a328 | 9232 | { |
09ea332d | 9233 | error_at(this->location(), |
9234 | "incompatible type for receiver (%s)", | |
9235 | reason.c_str()); | |
9236 | this->set_is_error(); | |
e440a328 | 9237 | } |
9238 | } | |
9239 | } | |
9240 | ||
9241 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 9242 | // we don't have to worry about it here unless something is wrong. |
9243 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
9244 | { | |
9245 | if (!fntype->is_varargs()) | |
9246 | { | |
9247 | error_at(this->location(), | |
9248 | _("invalid use of %<...%> calling non-variadic function")); | |
9249 | this->set_is_error(); | |
9250 | return; | |
9251 | } | |
9252 | } | |
e440a328 | 9253 | |
9254 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9255 | if (this->args_ == NULL) | |
9256 | { | |
9257 | if (parameters != NULL && !parameters->empty()) | |
9258 | this->report_error(_("not enough arguments")); | |
9259 | } | |
9260 | else if (parameters == NULL) | |
09ea332d | 9261 | { |
9262 | if (!is_method || this->args_->size() > 1) | |
9263 | this->report_error(_("too many arguments")); | |
9264 | } | |
1373401e | 9265 | else if (this->args_->size() == 1 |
9266 | && this->args_->front()->call_expression() != NULL | |
9267 | && this->args_->front()->call_expression()->result_count() > 1) | |
9268 | { | |
9269 | // This is F(G()) when G returns more than one result. If the | |
9270 | // results can be matched to parameters, it would have been | |
9271 | // lowered in do_lower. If we get here we know there is a | |
9272 | // mismatch. | |
9273 | if (this->args_->front()->call_expression()->result_count() | |
9274 | < parameters->size()) | |
9275 | this->report_error(_("not enough arguments")); | |
9276 | else | |
9277 | this->report_error(_("too many arguments")); | |
9278 | } | |
e440a328 | 9279 | else |
9280 | { | |
9281 | int i = 0; | |
09ea332d | 9282 | Expression_list::const_iterator pa = this->args_->begin(); |
9283 | if (is_method) | |
9284 | ++pa; | |
9285 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9286 | pt != parameters->end(); | |
9287 | ++pt, ++pa, ++i) | |
e440a328 | 9288 | { |
09ea332d | 9289 | if (pa == this->args_->end()) |
e440a328 | 9290 | { |
09ea332d | 9291 | this->report_error(_("not enough arguments")); |
e440a328 | 9292 | return; |
9293 | } | |
9294 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9295 | (*pa)->location(), false); | |
9296 | } | |
09ea332d | 9297 | if (pa != this->args_->end()) |
9298 | this->report_error(_("too many arguments")); | |
e440a328 | 9299 | } |
9300 | } | |
9301 | ||
72666aed | 9302 | Expression* |
9303 | Call_expression::do_copy() | |
9304 | { | |
9305 | Call_expression* call = | |
9306 | Expression::make_call(this->fn_->copy(), | |
9307 | (this->args_ == NULL | |
9308 | ? NULL | |
9309 | : this->args_->copy()), | |
9310 | this->is_varargs_, this->location()); | |
9311 | ||
9312 | if (this->varargs_are_lowered_) | |
9313 | call->set_varargs_are_lowered(); | |
9314 | return call; | |
9315 | } | |
9316 | ||
e440a328 | 9317 | // Return whether we have to use a temporary variable to ensure that |
9318 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9319 | // results then it will inevitably be executed last. |
e440a328 | 9320 | |
9321 | bool | |
9322 | Call_expression::do_must_eval_in_order() const | |
9323 | { | |
ceeb4318 | 9324 | return this->result_count() > 0; |
e440a328 | 9325 | } |
9326 | ||
e440a328 | 9327 | // Get the function and the first argument to use when calling an |
9328 | // interface method. | |
9329 | ||
2387f644 | 9330 | Expression* |
e440a328 | 9331 | Call_expression::interface_method_function( |
e440a328 | 9332 | Interface_field_reference_expression* interface_method, |
2387f644 | 9333 | Expression** first_arg_ptr) |
e440a328 | 9334 | { |
2387f644 | 9335 | *first_arg_ptr = interface_method->get_underlying_object(); |
9336 | return interface_method->get_function(); | |
e440a328 | 9337 | } |
9338 | ||
9339 | // Build the call expression. | |
9340 | ||
ea664253 | 9341 | Bexpression* |
9342 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 9343 | { |
2c809f8f | 9344 | if (this->call_ != NULL) |
ea664253 | 9345 | return this->call_; |
e440a328 | 9346 | |
9347 | Function_type* fntype = this->get_function_type(); | |
9348 | if (fntype == NULL) | |
ea664253 | 9349 | return context->backend()->error_expression(); |
e440a328 | 9350 | |
9351 | if (this->fn_->is_error_expression()) | |
ea664253 | 9352 | return context->backend()->error_expression(); |
e440a328 | 9353 | |
9354 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9355 | Location location = this->location(); |
e440a328 | 9356 | |
9357 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9358 | Interface_field_reference_expression* interface_method = |
9359 | this->fn_->interface_field_reference_expression(); | |
9360 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9361 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9362 | |
f8bdf81a | 9363 | bool has_closure_arg; |
8381eda7 | 9364 | if (has_closure) |
f8bdf81a | 9365 | has_closure_arg = true; |
8381eda7 | 9366 | else if (func != NULL) |
f8bdf81a | 9367 | has_closure_arg = false; |
8381eda7 | 9368 | else if (is_interface_method) |
f8bdf81a | 9369 | has_closure_arg = false; |
8381eda7 | 9370 | else |
f8bdf81a | 9371 | has_closure_arg = true; |
8381eda7 | 9372 | |
e440a328 | 9373 | int nargs; |
2c809f8f | 9374 | std::vector<Bexpression*> fn_args; |
e440a328 | 9375 | if (this->args_ == NULL || this->args_->empty()) |
9376 | { | |
f8bdf81a | 9377 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 9378 | if (nargs > 0) |
9379 | fn_args.resize(1); | |
e440a328 | 9380 | } |
09ea332d | 9381 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9382 | { | |
9383 | // Passing a receiver parameter. | |
9384 | go_assert(!is_interface_method | |
9385 | && fntype->is_method() | |
9386 | && this->args_->size() == 1); | |
f8bdf81a | 9387 | nargs = 1; |
2c809f8f | 9388 | fn_args.resize(1); |
ea664253 | 9389 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 9390 | } |
e440a328 | 9391 | else |
9392 | { | |
9393 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9394 | |
9395 | nargs = this->args_->size(); | |
09ea332d | 9396 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9397 | nargs += i; |
2c809f8f | 9398 | fn_args.resize(nargs); |
e440a328 | 9399 | |
9400 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9401 | Expression_list::const_iterator pe = this->args_->begin(); |
9402 | if (!is_interface_method && fntype->is_method()) | |
9403 | { | |
ea664253 | 9404 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 9405 | ++pe; |
9406 | ++i; | |
9407 | } | |
9408 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9409 | { |
c484d925 | 9410 | go_assert(pp != params->end()); |
2c809f8f | 9411 | Expression* arg = |
9412 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
9413 | location); | |
ea664253 | 9414 | fn_args[i] = arg->get_backend(context); |
e440a328 | 9415 | } |
c484d925 | 9416 | go_assert(pp == params->end()); |
f8bdf81a | 9417 | go_assert(i == nargs); |
e440a328 | 9418 | } |
9419 | ||
2c809f8f | 9420 | Expression* fn; |
9421 | Expression* closure = NULL; | |
8381eda7 | 9422 | if (func != NULL) |
9423 | { | |
9424 | Named_object* no = func->named_object(); | |
2c809f8f | 9425 | fn = Expression::make_func_code_reference(no, location); |
9426 | if (has_closure) | |
9427 | closure = func->closure(); | |
8381eda7 | 9428 | } |
09ea332d | 9429 | else if (!is_interface_method) |
8381eda7 | 9430 | { |
2c809f8f | 9431 | closure = this->fn_; |
9432 | ||
9433 | // The backend representation of this function type is a pointer | |
9434 | // to a struct whose first field is the actual function to call. | |
9435 | Type* pfntype = | |
9436 | Type::make_pointer_type( | |
9437 | Type::make_pointer_type(Type::make_void_type())); | |
9438 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
9439 | fn = Expression::make_unary(OPERATOR_MULT, fn, location); | |
9440 | } | |
e440a328 | 9441 | else |
cf609de4 | 9442 | { |
2387f644 | 9443 | Expression* first_arg; |
2c809f8f | 9444 | fn = this->interface_method_function(interface_method, &first_arg); |
ea664253 | 9445 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 9446 | } |
9447 | ||
1ecc6157 | 9448 | Bexpression* bclosure = NULL; |
9449 | if (has_closure_arg) | |
9450 | bclosure = closure->get_backend(context); | |
f8bdf81a | 9451 | else |
1ecc6157 | 9452 | go_assert(closure == NULL); |
f8bdf81a | 9453 | |
ea664253 | 9454 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 9455 | |
9456 | // When not calling a named function directly, use a type conversion | |
9457 | // in case the type of the function is a recursive type which refers | |
9458 | // to itself. We don't do this for an interface method because 1) | |
9459 | // an interface method never refers to itself, so we always have a | |
9460 | // function type here; 2) we pass an extra first argument to an | |
9461 | // interface method, so fntype is not correct. | |
9462 | if (func == NULL && !is_interface_method) | |
9463 | { | |
9464 | Btype* bft = fntype->get_backend_fntype(gogo); | |
9465 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
9466 | } | |
9467 | ||
1ecc6157 | 9468 | Bexpression* call = gogo->backend()->call_expression(bfn, fn_args, |
9469 | bclosure, location); | |
e440a328 | 9470 | |
2c809f8f | 9471 | if (this->results_ != NULL) |
e440a328 | 9472 | { |
2c809f8f | 9473 | go_assert(this->call_temp_ != NULL); |
9474 | Expression* call_ref = | |
9475 | Expression::make_temporary_reference(this->call_temp_, location); | |
ea664253 | 9476 | Bexpression* bcall_ref = call_ref->get_backend(context); |
2c809f8f | 9477 | Bstatement* assn_stmt = |
9478 | gogo->backend()->assignment_statement(bcall_ref, call, location); | |
e440a328 | 9479 | |
2c809f8f | 9480 | this->call_ = this->set_results(context, bcall_ref); |
e440a328 | 9481 | |
2c809f8f | 9482 | Bexpression* set_and_call = |
9483 | gogo->backend()->compound_expression(assn_stmt, this->call_, | |
9484 | location); | |
ea664253 | 9485 | return set_and_call; |
2c809f8f | 9486 | } |
e440a328 | 9487 | |
2c809f8f | 9488 | this->call_ = call; |
ea664253 | 9489 | return this->call_; |
e440a328 | 9490 | } |
9491 | ||
ceeb4318 | 9492 | // Set the result variables if this call returns multiple results. |
9493 | ||
2c809f8f | 9494 | Bexpression* |
9495 | Call_expression::set_results(Translate_context* context, Bexpression* call) | |
ceeb4318 | 9496 | { |
2c809f8f | 9497 | Gogo* gogo = context->gogo(); |
ceeb4318 | 9498 | |
2c809f8f | 9499 | Bexpression* results = NULL; |
b13c66cd | 9500 | Location loc = this->location(); |
2c809f8f | 9501 | |
ceeb4318 | 9502 | size_t rc = this->result_count(); |
2c809f8f | 9503 | for (size_t i = 0; i < rc; ++i) |
ceeb4318 | 9504 | { |
ceeb4318 | 9505 | Temporary_statement* temp = this->result(i); |
cd238b8d | 9506 | if (temp == NULL) |
9507 | { | |
9508 | go_assert(saw_errors()); | |
2c809f8f | 9509 | return gogo->backend()->error_expression(); |
cd238b8d | 9510 | } |
ceeb4318 | 9511 | Temporary_reference_expression* ref = |
9512 | Expression::make_temporary_reference(temp, loc); | |
9513 | ref->set_is_lvalue(); | |
ceeb4318 | 9514 | |
ea664253 | 9515 | Bexpression* result_ref = ref->get_backend(context); |
2c809f8f | 9516 | Bexpression* call_result = |
9517 | gogo->backend()->struct_field_expression(call, i, loc); | |
9518 | Bstatement* assn_stmt = | |
9519 | gogo->backend()->assignment_statement(result_ref, call_result, loc); | |
ceeb4318 | 9520 | |
2c809f8f | 9521 | Bexpression* result = |
9522 | gogo->backend()->compound_expression(assn_stmt, call_result, loc); | |
ceeb4318 | 9523 | |
2c809f8f | 9524 | if (results == NULL) |
9525 | results = result; | |
9526 | else | |
9527 | { | |
9528 | Bstatement* expr_stmt = gogo->backend()->expression_statement(result); | |
9529 | results = | |
9530 | gogo->backend()->compound_expression(expr_stmt, results, loc); | |
9531 | } | |
9532 | } | |
9533 | return results; | |
ceeb4318 | 9534 | } |
9535 | ||
d751bb78 | 9536 | // Dump ast representation for a call expressin. |
9537 | ||
9538 | void | |
9539 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9540 | { | |
9541 | this->fn_->dump_expression(ast_dump_context); | |
9542 | ast_dump_context->ostream() << "("; | |
9543 | if (args_ != NULL) | |
9544 | ast_dump_context->dump_expression_list(this->args_); | |
9545 | ||
9546 | ast_dump_context->ostream() << ") "; | |
9547 | } | |
9548 | ||
e440a328 | 9549 | // Make a call expression. |
9550 | ||
9551 | Call_expression* | |
9552 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9553 | Location location) |
e440a328 | 9554 | { |
9555 | return new Call_expression(fn, args, is_varargs, location); | |
9556 | } | |
9557 | ||
da244e59 | 9558 | // Class Call_result_expression. |
e440a328 | 9559 | |
9560 | // Traverse a call result. | |
9561 | ||
9562 | int | |
9563 | Call_result_expression::do_traverse(Traverse* traverse) | |
9564 | { | |
9565 | if (traverse->remember_expression(this->call_)) | |
9566 | { | |
9567 | // We have already traversed the call expression. | |
9568 | return TRAVERSE_CONTINUE; | |
9569 | } | |
9570 | return Expression::traverse(&this->call_, traverse); | |
9571 | } | |
9572 | ||
9573 | // Get the type. | |
9574 | ||
9575 | Type* | |
9576 | Call_result_expression::do_type() | |
9577 | { | |
425dd051 | 9578 | if (this->classification() == EXPRESSION_ERROR) |
9579 | return Type::make_error_type(); | |
9580 | ||
e440a328 | 9581 | // THIS->CALL_ can be replaced with a temporary reference due to |
9582 | // Call_expression::do_must_eval_in_order when there is an error. | |
9583 | Call_expression* ce = this->call_->call_expression(); | |
9584 | if (ce == NULL) | |
5e85f268 | 9585 | { |
9586 | this->set_is_error(); | |
9587 | return Type::make_error_type(); | |
9588 | } | |
e440a328 | 9589 | Function_type* fntype = ce->get_function_type(); |
9590 | if (fntype == NULL) | |
5e85f268 | 9591 | { |
e37658e2 | 9592 | if (ce->issue_error()) |
99b3f06f | 9593 | { |
9594 | if (!ce->fn()->type()->is_error()) | |
9595 | this->report_error(_("expected function")); | |
9596 | } | |
5e85f268 | 9597 | this->set_is_error(); |
9598 | return Type::make_error_type(); | |
9599 | } | |
e440a328 | 9600 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9601 | if (results == NULL || results->size() < 2) |
7b8d861f | 9602 | { |
ceeb4318 | 9603 | if (ce->issue_error()) |
9604 | this->report_error(_("number of results does not match " | |
9605 | "number of values")); | |
7b8d861f | 9606 | return Type::make_error_type(); |
9607 | } | |
e440a328 | 9608 | Typed_identifier_list::const_iterator pr = results->begin(); |
9609 | for (unsigned int i = 0; i < this->index_; ++i) | |
9610 | { | |
9611 | if (pr == results->end()) | |
425dd051 | 9612 | break; |
e440a328 | 9613 | ++pr; |
9614 | } | |
9615 | if (pr == results->end()) | |
425dd051 | 9616 | { |
ceeb4318 | 9617 | if (ce->issue_error()) |
9618 | this->report_error(_("number of results does not match " | |
9619 | "number of values")); | |
425dd051 | 9620 | return Type::make_error_type(); |
9621 | } | |
e440a328 | 9622 | return pr->type(); |
9623 | } | |
9624 | ||
425dd051 | 9625 | // Check the type. Just make sure that we trigger the warning in |
9626 | // do_type. | |
e440a328 | 9627 | |
9628 | void | |
9629 | Call_result_expression::do_check_types(Gogo*) | |
9630 | { | |
425dd051 | 9631 | this->type(); |
e440a328 | 9632 | } |
9633 | ||
9634 | // Determine the type. We have nothing to do here, but the 0 result | |
9635 | // needs to pass down to the caller. | |
9636 | ||
9637 | void | |
9638 | Call_result_expression::do_determine_type(const Type_context*) | |
9639 | { | |
fb94b0ca | 9640 | this->call_->determine_type_no_context(); |
e440a328 | 9641 | } |
9642 | ||
ea664253 | 9643 | // Return the backend representation. We just refer to the temporary set by the |
9644 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 9645 | // hard to evaluate the call at the right time. |
e440a328 | 9646 | |
ea664253 | 9647 | Bexpression* |
9648 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 9649 | { |
ceeb4318 | 9650 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 9651 | if (ce == NULL) |
9652 | { | |
9653 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 9654 | return context->backend()->error_expression(); |
cd238b8d | 9655 | } |
ceeb4318 | 9656 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 9657 | if (ts == NULL) |
9658 | { | |
9659 | go_assert(saw_errors()); | |
ea664253 | 9660 | return context->backend()->error_expression(); |
cd238b8d | 9661 | } |
ceeb4318 | 9662 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
ea664253 | 9663 | return ref->get_backend(context); |
e440a328 | 9664 | } |
9665 | ||
d751bb78 | 9666 | // Dump ast representation for a call result expression. |
9667 | ||
9668 | void | |
9669 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9670 | const | |
9671 | { | |
9672 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
9673 | // (struct) and the fields are referenced instead. | |
9674 | ast_dump_context->ostream() << this->index_ << "@("; | |
9675 | ast_dump_context->dump_expression(this->call_); | |
9676 | ast_dump_context->ostream() << ")"; | |
9677 | } | |
9678 | ||
e440a328 | 9679 | // Make a reference to a single result of a call which returns |
9680 | // multiple results. | |
9681 | ||
9682 | Expression* | |
9683 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9684 | { | |
9685 | return new Call_result_expression(call, index); | |
9686 | } | |
9687 | ||
9688 | // Class Index_expression. | |
9689 | ||
9690 | // Traversal. | |
9691 | ||
9692 | int | |
9693 | Index_expression::do_traverse(Traverse* traverse) | |
9694 | { | |
9695 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9696 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9697 | || (this->end_ != NULL | |
acf2b673 | 9698 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
9699 | || (this->cap_ != NULL | |
9700 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 9701 | return TRAVERSE_EXIT; |
9702 | return TRAVERSE_CONTINUE; | |
9703 | } | |
9704 | ||
9705 | // Lower an index expression. This converts the generic index | |
9706 | // expression into an array index, a string index, or a map index. | |
9707 | ||
9708 | Expression* | |
ceeb4318 | 9709 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 9710 | { |
b13c66cd | 9711 | Location location = this->location(); |
e440a328 | 9712 | Expression* left = this->left_; |
9713 | Expression* start = this->start_; | |
9714 | Expression* end = this->end_; | |
acf2b673 | 9715 | Expression* cap = this->cap_; |
e440a328 | 9716 | |
9717 | Type* type = left->type(); | |
5c13bd80 | 9718 | if (type->is_error()) |
d9f3743a | 9719 | { |
9720 | go_assert(saw_errors()); | |
9721 | return Expression::make_error(location); | |
9722 | } | |
b0cf7ddd | 9723 | else if (left->is_type_expression()) |
9724 | { | |
9725 | error_at(location, "attempt to index type expression"); | |
9726 | return Expression::make_error(location); | |
9727 | } | |
e440a328 | 9728 | else if (type->array_type() != NULL) |
acf2b673 | 9729 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 9730 | else if (type->points_to() != NULL |
9731 | && type->points_to()->array_type() != NULL | |
411eb89e | 9732 | && !type->points_to()->is_slice_type()) |
e440a328 | 9733 | { |
9734 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9735 | location); | |
38092374 | 9736 | |
9737 | // For an ordinary index into the array, the pointer will be | |
9738 | // dereferenced. For a slice it will not--the resulting slice | |
9739 | // will simply reuse the pointer, which is incorrect if that | |
9740 | // pointer is nil. | |
9741 | if (end != NULL || cap != NULL) | |
9742 | deref->issue_nil_check(); | |
9743 | ||
acf2b673 | 9744 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 9745 | } |
9746 | else if (type->is_string_type()) | |
acf2b673 | 9747 | { |
9748 | if (cap != NULL) | |
9749 | { | |
9750 | error_at(location, "invalid 3-index slice of string"); | |
9751 | return Expression::make_error(location); | |
9752 | } | |
9753 | return Expression::make_string_index(left, start, end, location); | |
9754 | } | |
e440a328 | 9755 | else if (type->map_type() != NULL) |
9756 | { | |
acf2b673 | 9757 | if (end != NULL || cap != NULL) |
e440a328 | 9758 | { |
9759 | error_at(location, "invalid slice of map"); | |
9760 | return Expression::make_error(location); | |
9761 | } | |
6d4c2432 | 9762 | Map_index_expression* ret = Expression::make_map_index(left, start, |
9763 | location); | |
e440a328 | 9764 | if (this->is_lvalue_) |
9765 | ret->set_is_lvalue(); | |
9766 | return ret; | |
9767 | } | |
9768 | else | |
9769 | { | |
9770 | error_at(location, | |
9771 | "attempt to index object which is not array, string, or map"); | |
9772 | return Expression::make_error(location); | |
9773 | } | |
9774 | } | |
9775 | ||
acf2b673 | 9776 | // Write an indexed expression |
9777 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 9778 | |
9779 | void | |
9780 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
9781 | const Expression* expr, | |
9782 | const Expression* start, | |
acf2b673 | 9783 | const Expression* end, |
9784 | const Expression* cap) | |
d751bb78 | 9785 | { |
9786 | expr->dump_expression(ast_dump_context); | |
9787 | ast_dump_context->ostream() << "["; | |
9788 | start->dump_expression(ast_dump_context); | |
9789 | if (end != NULL) | |
9790 | { | |
9791 | ast_dump_context->ostream() << ":"; | |
9792 | end->dump_expression(ast_dump_context); | |
9793 | } | |
acf2b673 | 9794 | if (cap != NULL) |
9795 | { | |
9796 | ast_dump_context->ostream() << ":"; | |
9797 | cap->dump_expression(ast_dump_context); | |
9798 | } | |
d751bb78 | 9799 | ast_dump_context->ostream() << "]"; |
9800 | } | |
9801 | ||
9802 | // Dump ast representation for an index expression. | |
9803 | ||
9804 | void | |
9805 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9806 | const | |
9807 | { | |
9808 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 9809 | this->start_, this->end_, this->cap_); |
d751bb78 | 9810 | } |
9811 | ||
e440a328 | 9812 | // Make an index expression. |
9813 | ||
9814 | Expression* | |
9815 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 9816 | Expression* cap, Location location) |
e440a328 | 9817 | { |
acf2b673 | 9818 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 9819 | } |
9820 | ||
da244e59 | 9821 | // Class Array_index_expression. |
e440a328 | 9822 | |
9823 | // Array index traversal. | |
9824 | ||
9825 | int | |
9826 | Array_index_expression::do_traverse(Traverse* traverse) | |
9827 | { | |
9828 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9829 | return TRAVERSE_EXIT; | |
9830 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9831 | return TRAVERSE_EXIT; | |
9832 | if (this->end_ != NULL) | |
9833 | { | |
9834 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9835 | return TRAVERSE_EXIT; | |
9836 | } | |
acf2b673 | 9837 | if (this->cap_ != NULL) |
9838 | { | |
9839 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
9840 | return TRAVERSE_EXIT; | |
9841 | } | |
e440a328 | 9842 | return TRAVERSE_CONTINUE; |
9843 | } | |
9844 | ||
9845 | // Return the type of an array index. | |
9846 | ||
9847 | Type* | |
9848 | Array_index_expression::do_type() | |
9849 | { | |
9850 | if (this->type_ == NULL) | |
9851 | { | |
9852 | Array_type* type = this->array_->type()->array_type(); | |
9853 | if (type == NULL) | |
9854 | this->type_ = Type::make_error_type(); | |
9855 | else if (this->end_ == NULL) | |
9856 | this->type_ = type->element_type(); | |
411eb89e | 9857 | else if (type->is_slice_type()) |
e440a328 | 9858 | { |
9859 | // A slice of a slice has the same type as the original | |
9860 | // slice. | |
9861 | this->type_ = this->array_->type()->deref(); | |
9862 | } | |
9863 | else | |
9864 | { | |
9865 | // A slice of an array is a slice. | |
9866 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
9867 | } | |
9868 | } | |
9869 | return this->type_; | |
9870 | } | |
9871 | ||
9872 | // Set the type of an array index. | |
9873 | ||
9874 | void | |
9875 | Array_index_expression::do_determine_type(const Type_context*) | |
9876 | { | |
9877 | this->array_->determine_type_no_context(); | |
f77aa642 | 9878 | |
9879 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
9880 | if (this->start_->is_constant()) | |
9881 | this->start_->determine_type(&index_context); | |
9882 | else | |
9883 | this->start_->determine_type_no_context(); | |
e440a328 | 9884 | if (this->end_ != NULL) |
f77aa642 | 9885 | { |
9886 | if (this->end_->is_constant()) | |
9887 | this->end_->determine_type(&index_context); | |
9888 | else | |
9889 | this->end_->determine_type_no_context(); | |
9890 | } | |
acf2b673 | 9891 | if (this->cap_ != NULL) |
f77aa642 | 9892 | { |
9893 | if (this->cap_->is_constant()) | |
9894 | this->cap_->determine_type(&index_context); | |
9895 | else | |
9896 | this->cap_->determine_type_no_context(); | |
9897 | } | |
e440a328 | 9898 | } |
9899 | ||
9900 | // Check types of an array index. | |
9901 | ||
9902 | void | |
9903 | Array_index_expression::do_check_types(Gogo*) | |
9904 | { | |
f6bc81e6 | 9905 | Numeric_constant nc; |
9906 | unsigned long v; | |
9907 | if (this->start_->type()->integer_type() == NULL | |
9908 | && !this->start_->type()->is_error() | |
9909 | && (!this->start_->numeric_constant_value(&nc) | |
9910 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 9911 | this->report_error(_("index must be integer")); |
9912 | if (this->end_ != NULL | |
9913 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 9914 | && !this->end_->type()->is_error() |
9915 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 9916 | && !this->end_->is_error_expression() |
9917 | && (!this->end_->numeric_constant_value(&nc) | |
9918 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 9919 | this->report_error(_("slice end must be integer")); |
acf2b673 | 9920 | if (this->cap_ != NULL |
9921 | && this->cap_->type()->integer_type() == NULL | |
9922 | && !this->cap_->type()->is_error() | |
9923 | && !this->cap_->is_nil_expression() | |
9924 | && !this->cap_->is_error_expression() | |
9925 | && (!this->cap_->numeric_constant_value(&nc) | |
9926 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
9927 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 9928 | |
9929 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 9930 | if (array_type == NULL) |
9931 | { | |
c484d925 | 9932 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 9933 | return; |
9934 | } | |
e440a328 | 9935 | |
9936 | unsigned int int_bits = | |
9937 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
9938 | ||
0c77715b | 9939 | Numeric_constant lvalnc; |
e440a328 | 9940 | mpz_t lval; |
e440a328 | 9941 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 9942 | && array_type->length()->numeric_constant_value(&lvalnc) |
9943 | && lvalnc.to_int(&lval)); | |
9944 | Numeric_constant inc; | |
e440a328 | 9945 | mpz_t ival; |
0bd5d859 | 9946 | bool ival_valid = false; |
0c77715b | 9947 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 9948 | { |
0bd5d859 | 9949 | ival_valid = true; |
e440a328 | 9950 | if (mpz_sgn(ival) < 0 |
9951 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9952 | || (lval_valid | |
9953 | && (this->end_ == NULL | |
9954 | ? mpz_cmp(ival, lval) >= 0 | |
9955 | : mpz_cmp(ival, lval) > 0))) | |
9956 | { | |
9957 | error_at(this->start_->location(), "array index out of bounds"); | |
9958 | this->set_is_error(); | |
9959 | } | |
9960 | } | |
9961 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9962 | { | |
0c77715b | 9963 | Numeric_constant enc; |
9964 | mpz_t eval; | |
acf2b673 | 9965 | bool eval_valid = false; |
0c77715b | 9966 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 9967 | { |
acf2b673 | 9968 | eval_valid = true; |
0c77715b | 9969 | if (mpz_sgn(eval) < 0 |
9970 | || mpz_sizeinbase(eval, 2) >= int_bits | |
9971 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 9972 | { |
9973 | error_at(this->end_->location(), "array index out of bounds"); | |
9974 | this->set_is_error(); | |
9975 | } | |
0bd5d859 | 9976 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
9977 | this->report_error(_("inverted slice range")); | |
e440a328 | 9978 | } |
acf2b673 | 9979 | |
9980 | Numeric_constant cnc; | |
9981 | mpz_t cval; | |
9982 | if (this->cap_ != NULL | |
9983 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
9984 | { | |
9985 | if (mpz_sgn(cval) < 0 | |
9986 | || mpz_sizeinbase(cval, 2) >= int_bits | |
9987 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
9988 | { | |
9989 | error_at(this->cap_->location(), "array index out of bounds"); | |
9990 | this->set_is_error(); | |
9991 | } | |
9992 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
9993 | { | |
9994 | error_at(this->cap_->location(), | |
9995 | "invalid slice index: capacity less than start"); | |
9996 | this->set_is_error(); | |
9997 | } | |
9998 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
9999 | { | |
10000 | error_at(this->cap_->location(), | |
10001 | "invalid slice index: capacity less than length"); | |
10002 | this->set_is_error(); | |
10003 | } | |
10004 | mpz_clear(cval); | |
10005 | } | |
10006 | ||
10007 | if (eval_valid) | |
10008 | mpz_clear(eval); | |
e440a328 | 10009 | } |
0bd5d859 | 10010 | if (ival_valid) |
10011 | mpz_clear(ival); | |
0c77715b | 10012 | if (lval_valid) |
10013 | mpz_clear(lval); | |
e440a328 | 10014 | |
10015 | // A slice of an array requires an addressable array. A slice of a | |
10016 | // slice is always possible. | |
411eb89e | 10017 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10018 | { |
10019 | if (!this->array_->is_addressable()) | |
8da39c3b | 10020 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10021 | else |
10022 | this->array_->address_taken(true); | |
10023 | } | |
e440a328 | 10024 | } |
10025 | ||
2c809f8f | 10026 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10027 | |
10028 | Expression* | |
10029 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10030 | Statement_inserter* inserter) | |
10031 | { | |
10032 | Location loc = this->location(); | |
5bf8be8b | 10033 | Expression* array = this->array_; |
10034 | Expression* start = this->start_; | |
10035 | Expression* end = this->end_; | |
10036 | Expression* cap = this->cap_; | |
10037 | if (array->is_error_expression() | |
10038 | || array->type()->is_error_type() | |
10039 | || start->is_error_expression() | |
10040 | || start->type()->is_error_type() | |
10041 | || (end != NULL | |
10042 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10043 | || (cap != NULL | |
10044 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10045 | { | |
10046 | go_assert(saw_errors()); | |
10047 | return Expression::make_error(loc); | |
10048 | } | |
10049 | ||
2c809f8f | 10050 | Temporary_statement* temp; |
5bf8be8b | 10051 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10052 | { |
5bf8be8b | 10053 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10054 | inserter->insert(temp); |
10055 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10056 | } | |
5bf8be8b | 10057 | if (!start->is_variable()) |
2c809f8f | 10058 | { |
5bf8be8b | 10059 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10060 | inserter->insert(temp); |
10061 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10062 | } | |
5bf8be8b | 10063 | if (end != NULL |
10064 | && !end->is_nil_expression() | |
10065 | && !end->is_variable()) | |
2c809f8f | 10066 | { |
5bf8be8b | 10067 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10068 | inserter->insert(temp); |
10069 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10070 | } | |
5bf8be8b | 10071 | if (cap!= NULL && !cap->is_variable()) |
2c809f8f | 10072 | { |
5bf8be8b | 10073 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10074 | inserter->insert(temp); |
10075 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10076 | } | |
10077 | ||
35a54f17 | 10078 | return this; |
10079 | } | |
10080 | ||
e440a328 | 10081 | // Return whether this expression is addressable. |
10082 | ||
10083 | bool | |
10084 | Array_index_expression::do_is_addressable() const | |
10085 | { | |
10086 | // A slice expression is not addressable. | |
10087 | if (this->end_ != NULL) | |
10088 | return false; | |
10089 | ||
10090 | // An index into a slice is addressable. | |
411eb89e | 10091 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10092 | return true; |
10093 | ||
10094 | // An index into an array is addressable if the array is | |
10095 | // addressable. | |
10096 | return this->array_->is_addressable(); | |
10097 | } | |
10098 | ||
ea664253 | 10099 | // Get the backend representation for an array index. |
e440a328 | 10100 | |
ea664253 | 10101 | Bexpression* |
10102 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10103 | { |
e440a328 | 10104 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10105 | if (array_type == NULL) |
10106 | { | |
c484d925 | 10107 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10108 | return context->backend()->error_expression(); |
d8cd8e2d | 10109 | } |
35a54f17 | 10110 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10111 | |
2c809f8f | 10112 | Location loc = this->location(); |
10113 | Gogo* gogo = context->gogo(); | |
10114 | ||
6dfedc16 | 10115 | Type* int_type = Type::lookup_integer_type("int"); |
10116 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10117 | |
2c809f8f | 10118 | // We need to convert the length and capacity to the Go "int" type here |
10119 | // because the length of a fixed-length array could be of type "uintptr" | |
10120 | // and gimple disallows binary operations between "uintptr" and other | |
10121 | // integer types. FIXME. | |
10122 | Bexpression* length = NULL; | |
a04bfdfc | 10123 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10124 | { | |
35a54f17 | 10125 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10126 | length = len->get_backend(context); |
2c809f8f | 10127 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10128 | } |
10129 | ||
2c809f8f | 10130 | Bexpression* capacity = NULL; |
a04bfdfc | 10131 | if (this->end_ != NULL) |
10132 | { | |
35a54f17 | 10133 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10134 | capacity = cap->get_backend(context); |
2c809f8f | 10135 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10136 | } |
10137 | ||
2c809f8f | 10138 | Bexpression* cap_arg = capacity; |
acf2b673 | 10139 | if (this->cap_ != NULL) |
10140 | { | |
ea664253 | 10141 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10142 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10143 | } |
10144 | ||
2c809f8f | 10145 | if (length == NULL) |
10146 | length = cap_arg; | |
e440a328 | 10147 | |
10148 | int code = (array_type->length() != NULL | |
10149 | ? (this->end_ == NULL | |
10150 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10151 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10152 | : (this->end_ == NULL | |
10153 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10154 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10155 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10156 | |
6dfedc16 | 10157 | if (this->start_->type()->integer_type() == NULL |
10158 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10159 | { | |
10160 | go_assert(saw_errors()); | |
10161 | return context->backend()->error_expression(); | |
10162 | } | |
d9f3743a | 10163 | |
ea664253 | 10164 | Bexpression* bad_index = |
d9f3743a | 10165 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 10166 | |
ea664253 | 10167 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10168 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10169 | Bexpression* start_too_large = | |
10170 | gogo->backend()->binary_expression((this->end_ == NULL | |
10171 | ? OPERATOR_GE | |
10172 | : OPERATOR_GT), | |
10173 | start, | |
10174 | (this->end_ == NULL | |
10175 | ? length | |
10176 | : capacity), | |
10177 | loc); | |
10178 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10179 | bad_index, loc); | |
e440a328 | 10180 | |
10181 | if (this->end_ == NULL) | |
10182 | { | |
10183 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10184 | // wrap the index check into START. |
10185 | start = | |
10186 | gogo->backend()->conditional_expression(int_btype, bad_index, | |
10187 | crash, start, loc); | |
e440a328 | 10188 | |
2c809f8f | 10189 | Bexpression* ret; |
e440a328 | 10190 | if (array_type->length() != NULL) |
10191 | { | |
ea664253 | 10192 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10193 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10194 | } |
10195 | else | |
10196 | { | |
2c809f8f | 10197 | // Slice. |
10198 | Expression* valptr = | |
35a54f17 | 10199 | array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10200 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 10201 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 10202 | |
10203 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
10204 | Btype* ele_btype = ele_type->get_backend(gogo); | |
10205 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 10206 | } |
ea664253 | 10207 | return ret; |
e440a328 | 10208 | } |
10209 | ||
10210 | // Array slice. | |
10211 | ||
acf2b673 | 10212 | if (this->cap_ != NULL) |
10213 | { | |
2c809f8f | 10214 | Bexpression* bounds_bcheck = |
ea664253 | 10215 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 10216 | bad_index = |
10217 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10218 | bad_index, loc); | |
10219 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
10220 | ||
10221 | Bexpression* cap_too_small = | |
10222 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
10223 | Bexpression* cap_too_large = | |
10224 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
10225 | Bexpression* bad_cap = | |
10226 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
10227 | cap_too_large, loc); | |
10228 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
10229 | bad_index, loc); | |
10230 | } | |
10231 | ||
10232 | Bexpression* end; | |
e440a328 | 10233 | if (this->end_->is_nil_expression()) |
2c809f8f | 10234 | end = length; |
e440a328 | 10235 | else |
10236 | { | |
2c809f8f | 10237 | Bexpression* bounds_bcheck = |
ea664253 | 10238 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 10239 | |
2c809f8f | 10240 | bad_index = |
10241 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10242 | bad_index, loc); | |
e440a328 | 10243 | |
ea664253 | 10244 | end = this->end_->get_backend(context); |
2c809f8f | 10245 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
10246 | Bexpression* end_too_small = | |
10247 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
10248 | Bexpression* end_too_large = | |
10249 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
10250 | Bexpression* bad_end = | |
10251 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
10252 | end_too_large, loc); | |
10253 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
10254 | bad_index, loc); | |
e440a328 | 10255 | } |
10256 | ||
35a54f17 | 10257 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10258 | Bexpression* val = valptr->get_backend(context); |
2c809f8f | 10259 | val = gogo->backend()->pointer_offset_expression(val, start, loc); |
e440a328 | 10260 | |
2c809f8f | 10261 | Bexpression* result_length = |
10262 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 10263 | |
2c809f8f | 10264 | Bexpression* result_capacity = |
10265 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 10266 | |
2c809f8f | 10267 | Btype* struct_btype = this->type()->get_backend(gogo); |
10268 | std::vector<Bexpression*> init; | |
10269 | init.push_back(val); | |
10270 | init.push_back(result_length); | |
10271 | init.push_back(result_capacity); | |
e440a328 | 10272 | |
2c809f8f | 10273 | Bexpression* ctor = |
10274 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
ea664253 | 10275 | return gogo->backend()->conditional_expression(struct_btype, bad_index, |
10276 | crash, ctor, loc); | |
e440a328 | 10277 | } |
10278 | ||
d751bb78 | 10279 | // Dump ast representation for an array index expression. |
10280 | ||
10281 | void | |
10282 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10283 | const | |
10284 | { | |
10285 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 10286 | this->start_, this->end_, this->cap_); |
d751bb78 | 10287 | } |
10288 | ||
acf2b673 | 10289 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 10290 | |
10291 | Expression* | |
10292 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 10293 | Expression* end, Expression* cap, |
10294 | Location location) | |
e440a328 | 10295 | { |
acf2b673 | 10296 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 10297 | } |
10298 | ||
10299 | // A string index. This is used for both indexing and slicing. | |
10300 | ||
10301 | class String_index_expression : public Expression | |
10302 | { | |
10303 | public: | |
10304 | String_index_expression(Expression* string, Expression* start, | |
b13c66cd | 10305 | Expression* end, Location location) |
e440a328 | 10306 | : Expression(EXPRESSION_STRING_INDEX, location), |
10307 | string_(string), start_(start), end_(end) | |
10308 | { } | |
10309 | ||
10310 | protected: | |
10311 | int | |
10312 | do_traverse(Traverse*); | |
10313 | ||
2c809f8f | 10314 | Expression* |
10315 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
10316 | ||
e440a328 | 10317 | Type* |
10318 | do_type(); | |
10319 | ||
10320 | void | |
10321 | do_determine_type(const Type_context*); | |
10322 | ||
10323 | void | |
10324 | do_check_types(Gogo*); | |
10325 | ||
10326 | Expression* | |
10327 | do_copy() | |
10328 | { | |
10329 | return Expression::make_string_index(this->string_->copy(), | |
10330 | this->start_->copy(), | |
10331 | (this->end_ == NULL | |
10332 | ? NULL | |
10333 | : this->end_->copy()), | |
10334 | this->location()); | |
10335 | } | |
10336 | ||
baef9f7a | 10337 | bool |
10338 | do_must_eval_subexpressions_in_order(int* skip) const | |
10339 | { | |
10340 | *skip = 1; | |
10341 | return true; | |
10342 | } | |
10343 | ||
ea664253 | 10344 | Bexpression* |
10345 | do_get_backend(Translate_context*); | |
e440a328 | 10346 | |
d751bb78 | 10347 | void |
10348 | do_dump_expression(Ast_dump_context*) const; | |
10349 | ||
e440a328 | 10350 | private: |
10351 | // The string we are getting a value from. | |
10352 | Expression* string_; | |
10353 | // The start or only index. | |
10354 | Expression* start_; | |
10355 | // The end index of a slice. This may be NULL for a single index, | |
10356 | // or it may be a nil expression for the length of the string. | |
10357 | Expression* end_; | |
10358 | }; | |
10359 | ||
10360 | // String index traversal. | |
10361 | ||
10362 | int | |
10363 | String_index_expression::do_traverse(Traverse* traverse) | |
10364 | { | |
10365 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10366 | return TRAVERSE_EXIT; | |
10367 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10368 | return TRAVERSE_EXIT; | |
10369 | if (this->end_ != NULL) | |
10370 | { | |
10371 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10372 | return TRAVERSE_EXIT; | |
10373 | } | |
10374 | return TRAVERSE_CONTINUE; | |
10375 | } | |
10376 | ||
2c809f8f | 10377 | Expression* |
10378 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
10379 | Statement_inserter* inserter) | |
e440a328 | 10380 | { |
2c809f8f | 10381 | Location loc = this->location(); |
5bf8be8b | 10382 | Expression* string = this->string_; |
10383 | Expression* start = this->start_; | |
10384 | Expression* end = this->end_; | |
10385 | if (string->is_error_expression() | |
10386 | || string->type()->is_error_type() | |
10387 | || start->is_error_expression() | |
10388 | || start->type()->is_error_type() | |
10389 | || (end != NULL | |
10390 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
10391 | { | |
10392 | go_assert(saw_errors()); | |
10393 | return Expression::make_error(loc); | |
10394 | } | |
10395 | ||
10396 | Temporary_statement* temp; | |
2c809f8f | 10397 | if (!this->string_->is_variable()) |
10398 | { | |
10399 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
10400 | inserter->insert(temp); | |
10401 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
10402 | } | |
10403 | if (!this->start_->is_variable()) | |
10404 | { | |
10405 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
10406 | inserter->insert(temp); | |
10407 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10408 | } | |
10409 | if (this->end_ != NULL | |
10410 | && !this->end_->is_nil_expression() | |
10411 | && !this->end_->is_variable()) | |
10412 | { | |
10413 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
10414 | inserter->insert(temp); | |
10415 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10416 | } | |
10417 | ||
10418 | return this; | |
10419 | } | |
10420 | ||
10421 | // Return the type of a string index. | |
10422 | ||
10423 | Type* | |
10424 | String_index_expression::do_type() | |
10425 | { | |
10426 | if (this->end_ == NULL) | |
10427 | return Type::lookup_integer_type("uint8"); | |
10428 | else | |
10429 | return this->string_->type(); | |
10430 | } | |
10431 | ||
10432 | // Determine the type of a string index. | |
10433 | ||
10434 | void | |
10435 | String_index_expression::do_determine_type(const Type_context*) | |
10436 | { | |
10437 | this->string_->determine_type_no_context(); | |
f77aa642 | 10438 | |
10439 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10440 | if (this->start_->is_constant()) | |
10441 | this->start_->determine_type(&index_context); | |
10442 | else | |
10443 | this->start_->determine_type_no_context(); | |
e440a328 | 10444 | if (this->end_ != NULL) |
f77aa642 | 10445 | { |
10446 | if (this->end_->is_constant()) | |
10447 | this->end_->determine_type(&index_context); | |
10448 | else | |
10449 | this->end_->determine_type_no_context(); | |
10450 | } | |
e440a328 | 10451 | } |
10452 | ||
10453 | // Check types of a string index. | |
10454 | ||
10455 | void | |
10456 | String_index_expression::do_check_types(Gogo*) | |
10457 | { | |
acdc230d | 10458 | Numeric_constant nc; |
10459 | unsigned long v; | |
10460 | if (this->start_->type()->integer_type() == NULL | |
10461 | && !this->start_->type()->is_error() | |
10462 | && (!this->start_->numeric_constant_value(&nc) | |
10463 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10464 | this->report_error(_("index must be integer")); |
10465 | if (this->end_ != NULL | |
10466 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 10467 | && !this->end_->type()->is_error() |
10468 | && !this->end_->is_nil_expression() | |
10469 | && !this->end_->is_error_expression() | |
10470 | && (!this->end_->numeric_constant_value(&nc) | |
10471 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10472 | this->report_error(_("slice end must be integer")); |
10473 | ||
10474 | std::string sval; | |
10475 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10476 | ||
0c77715b | 10477 | Numeric_constant inc; |
e440a328 | 10478 | mpz_t ival; |
0bd5d859 | 10479 | bool ival_valid = false; |
0c77715b | 10480 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10481 | { |
0bd5d859 | 10482 | ival_valid = true; |
e440a328 | 10483 | if (mpz_sgn(ival) < 0 |
b10f32fb | 10484 | || (sval_valid |
10485 | && (this->end_ == NULL | |
10486 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
10487 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 10488 | { |
10489 | error_at(this->start_->location(), "string index out of bounds"); | |
10490 | this->set_is_error(); | |
10491 | } | |
10492 | } | |
10493 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10494 | { | |
0c77715b | 10495 | Numeric_constant enc; |
10496 | mpz_t eval; | |
10497 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10498 | { |
0c77715b | 10499 | if (mpz_sgn(eval) < 0 |
10500 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10501 | { |
10502 | error_at(this->end_->location(), "string index out of bounds"); | |
10503 | this->set_is_error(); | |
10504 | } | |
0bd5d859 | 10505 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10506 | this->report_error(_("inverted slice range")); | |
0c77715b | 10507 | mpz_clear(eval); |
e440a328 | 10508 | } |
10509 | } | |
0bd5d859 | 10510 | if (ival_valid) |
10511 | mpz_clear(ival); | |
e440a328 | 10512 | } |
10513 | ||
ea664253 | 10514 | // Get the backend representation for a string index. |
e440a328 | 10515 | |
ea664253 | 10516 | Bexpression* |
10517 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10518 | { |
b13c66cd | 10519 | Location loc = this->location(); |
2c809f8f | 10520 | Expression* string_arg = this->string_; |
10521 | if (this->string_->type()->points_to() != NULL) | |
10522 | string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc); | |
e440a328 | 10523 | |
2c809f8f | 10524 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 10525 | |
2c809f8f | 10526 | int code = (this->end_ == NULL |
10527 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10528 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 10529 | |
2c809f8f | 10530 | Gogo* gogo = context->gogo(); |
ea664253 | 10531 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 10532 | |
10533 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 10534 | |
2c809f8f | 10535 | // It is possible that an error occurred earlier because the start index |
10536 | // cannot be represented as an integer type. In this case, we shouldn't | |
10537 | // try casting the starting index into an integer since | |
10538 | // Type_conversion_expression will fail to get the backend representation. | |
10539 | // FIXME. | |
10540 | if (this->start_->type()->integer_type() == NULL | |
10541 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10542 | { | |
10543 | go_assert(saw_errors()); | |
ea664253 | 10544 | return context->backend()->error_expression(); |
2c809f8f | 10545 | } |
e440a328 | 10546 | |
2c809f8f | 10547 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
e440a328 | 10548 | |
2c809f8f | 10549 | if (this->end_ == NULL) |
10550 | { | |
10551 | Expression* length = | |
10552 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 10553 | |
2c809f8f | 10554 | Expression* start_too_large = |
10555 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
10556 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
10557 | bad_index, loc); | |
10558 | Expression* bytes = | |
10559 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 10560 | |
ea664253 | 10561 | Bexpression* bstart = start->get_backend(context); |
10562 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 10563 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 10564 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
10565 | Bexpression* index = | |
10566 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 10567 | |
2c809f8f | 10568 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 10569 | Bexpression* index_error = bad_index->get_backend(context); |
10570 | return gogo->backend()->conditional_expression(byte_btype, index_error, | |
10571 | crash, index, loc); | |
2c809f8f | 10572 | } |
10573 | ||
10574 | Expression* end = NULL; | |
10575 | if (this->end_->is_nil_expression()) | |
e67508fa | 10576 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 10577 | else |
10578 | { | |
2c809f8f | 10579 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
10580 | bad_index = | |
10581 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
10582 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 10583 | } |
2c809f8f | 10584 | |
10585 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
10586 | string_arg, start, end); | |
ea664253 | 10587 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 10588 | |
10589 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 10590 | Bexpression* index_error = bad_index->get_backend(context); |
10591 | return gogo->backend()->conditional_expression(str_btype, index_error, | |
10592 | crash, bstrslice, loc); | |
e440a328 | 10593 | } |
10594 | ||
d751bb78 | 10595 | // Dump ast representation for a string index expression. |
10596 | ||
10597 | void | |
10598 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10599 | const | |
10600 | { | |
acf2b673 | 10601 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
10602 | this->start_, this->end_, NULL); | |
d751bb78 | 10603 | } |
10604 | ||
e440a328 | 10605 | // Make a string index expression. END may be NULL. |
10606 | ||
10607 | Expression* | |
10608 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10609 | Expression* end, Location location) |
e440a328 | 10610 | { |
10611 | return new String_index_expression(string, start, end, location); | |
10612 | } | |
10613 | ||
10614 | // Class Map_index. | |
10615 | ||
10616 | // Get the type of the map. | |
10617 | ||
10618 | Map_type* | |
10619 | Map_index_expression::get_map_type() const | |
10620 | { | |
10621 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 10622 | if (mt == NULL) |
c484d925 | 10623 | go_assert(saw_errors()); |
e440a328 | 10624 | return mt; |
10625 | } | |
10626 | ||
10627 | // Map index traversal. | |
10628 | ||
10629 | int | |
10630 | Map_index_expression::do_traverse(Traverse* traverse) | |
10631 | { | |
10632 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
10633 | return TRAVERSE_EXIT; | |
10634 | return Expression::traverse(&this->index_, traverse); | |
10635 | } | |
10636 | ||
2c809f8f | 10637 | // We need to pass in a pointer to the key, so flatten the index into a |
10638 | // temporary variable if it isn't already. The value pointer will be | |
10639 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
10640 | // recomputation. | |
10641 | ||
10642 | Expression* | |
91c0fd76 | 10643 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 10644 | Statement_inserter* inserter) |
10645 | { | |
91c0fd76 | 10646 | Location loc = this->location(); |
2c809f8f | 10647 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 10648 | if (this->index()->is_error_expression() |
10649 | || this->index()->type()->is_error_type() | |
10650 | || mt->is_error_type()) | |
10651 | { | |
10652 | go_assert(saw_errors()); | |
10653 | return Expression::make_error(loc); | |
10654 | } | |
10655 | ||
91c0fd76 | 10656 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
10657 | { | |
10658 | if (this->index_->type()->interface_type() != NULL | |
10659 | && !this->index_->is_variable()) | |
10660 | { | |
10661 | Temporary_statement* temp = | |
10662 | Statement::make_temporary(NULL, this->index_, loc); | |
10663 | inserter->insert(temp); | |
10664 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
10665 | } | |
10666 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
10667 | this->index_, loc); | |
10668 | } | |
2c809f8f | 10669 | |
10670 | if (!this->index_->is_variable()) | |
10671 | { | |
10672 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 10673 | loc); |
2c809f8f | 10674 | inserter->insert(temp); |
91c0fd76 | 10675 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 10676 | } |
10677 | ||
10678 | if (this->value_pointer_ == NULL) | |
10679 | this->get_value_pointer(this->is_lvalue_); | |
5bf8be8b | 10680 | if (this->value_pointer_->is_error_expression() |
10681 | || this->value_pointer_->type()->is_error_type()) | |
10682 | return Expression::make_error(loc); | |
2c809f8f | 10683 | if (!this->value_pointer_->is_variable()) |
10684 | { | |
10685 | Temporary_statement* temp = | |
91c0fd76 | 10686 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 10687 | inserter->insert(temp); |
91c0fd76 | 10688 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 10689 | } |
10690 | ||
10691 | return this; | |
10692 | } | |
10693 | ||
e440a328 | 10694 | // Return the type of a map index. |
10695 | ||
10696 | Type* | |
10697 | Map_index_expression::do_type() | |
10698 | { | |
c7524fae | 10699 | Map_type* mt = this->get_map_type(); |
10700 | if (mt == NULL) | |
10701 | return Type::make_error_type(); | |
10702 | Type* type = mt->val_type(); | |
e440a328 | 10703 | // If this map index is in a tuple assignment, we actually return a |
10704 | // pointer to the value type. Tuple_map_assignment_statement is | |
10705 | // responsible for handling this correctly. We need to get the type | |
10706 | // right in case this gets assigned to a temporary variable. | |
10707 | if (this->is_in_tuple_assignment_) | |
10708 | type = Type::make_pointer_type(type); | |
10709 | return type; | |
10710 | } | |
10711 | ||
10712 | // Fix the type of a map index. | |
10713 | ||
10714 | void | |
10715 | Map_index_expression::do_determine_type(const Type_context*) | |
10716 | { | |
10717 | this->map_->determine_type_no_context(); | |
c7524fae | 10718 | Map_type* mt = this->get_map_type(); |
10719 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
10720 | Type_context subcontext(key_type, false); | |
e440a328 | 10721 | this->index_->determine_type(&subcontext); |
10722 | } | |
10723 | ||
10724 | // Check types of a map index. | |
10725 | ||
10726 | void | |
10727 | Map_index_expression::do_check_types(Gogo*) | |
10728 | { | |
10729 | std::string reason; | |
c7524fae | 10730 | Map_type* mt = this->get_map_type(); |
10731 | if (mt == NULL) | |
10732 | return; | |
10733 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 10734 | { |
10735 | if (reason.empty()) | |
10736 | this->report_error(_("incompatible type for map index")); | |
10737 | else | |
10738 | { | |
10739 | error_at(this->location(), "incompatible type for map index (%s)", | |
10740 | reason.c_str()); | |
10741 | this->set_is_error(); | |
10742 | } | |
10743 | } | |
10744 | } | |
10745 | ||
ea664253 | 10746 | // Get the backend representation for a map index. |
e440a328 | 10747 | |
ea664253 | 10748 | Bexpression* |
10749 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10750 | { |
10751 | Map_type* type = this->get_map_type(); | |
c7524fae | 10752 | if (type == NULL) |
2c809f8f | 10753 | { |
10754 | go_assert(saw_errors()); | |
ea664253 | 10755 | return context->backend()->error_expression(); |
2c809f8f | 10756 | } |
e440a328 | 10757 | |
2c809f8f | 10758 | go_assert(this->value_pointer_ != NULL |
10759 | && this->value_pointer_->is_variable()); | |
e440a328 | 10760 | |
2c809f8f | 10761 | Bexpression* ret; |
e440a328 | 10762 | if (this->is_lvalue_) |
2c809f8f | 10763 | { |
10764 | Expression* val = | |
10765 | Expression::make_unary(OPERATOR_MULT, this->value_pointer_, | |
10766 | this->location()); | |
ea664253 | 10767 | ret = val->get_backend(context); |
2c809f8f | 10768 | } |
e440a328 | 10769 | else if (this->is_in_tuple_assignment_) |
10770 | { | |
10771 | // Tuple_map_assignment_statement is responsible for using this | |
10772 | // appropriately. | |
ea664253 | 10773 | ret = this->value_pointer_->get_backend(context); |
e440a328 | 10774 | } |
10775 | else | |
10776 | { | |
2c809f8f | 10777 | Location loc = this->location(); |
10778 | ||
10779 | Expression* nil_check = | |
10780 | Expression::make_binary(OPERATOR_EQEQ, this->value_pointer_, | |
10781 | Expression::make_nil(loc), loc); | |
ea664253 | 10782 | Bexpression* bnil_check = nil_check->get_backend(context); |
2c809f8f | 10783 | Expression* val = |
10784 | Expression::make_unary(OPERATOR_MULT, this->value_pointer_, loc); | |
ea664253 | 10785 | Bexpression* bval = val->get_backend(context); |
2c809f8f | 10786 | |
63697958 | 10787 | Gogo* gogo = context->gogo(); |
10788 | Btype* val_btype = type->val_type()->get_backend(gogo); | |
10789 | Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); | |
2c809f8f | 10790 | ret = gogo->backend()->conditional_expression(val_btype, bnil_check, |
10791 | val_zero, bval, loc); | |
e440a328 | 10792 | } |
ea664253 | 10793 | return ret; |
e440a328 | 10794 | } |
10795 | ||
2c809f8f | 10796 | // Get an expression for the map index. This returns an expression which |
10797 | // evaluates to a pointer to a value. The pointer will be NULL if the key is | |
e440a328 | 10798 | // not in the map. |
10799 | ||
2c809f8f | 10800 | Expression* |
10801 | Map_index_expression::get_value_pointer(bool insert) | |
e440a328 | 10802 | { |
2c809f8f | 10803 | if (this->value_pointer_ == NULL) |
746d2e73 | 10804 | { |
2c809f8f | 10805 | Map_type* type = this->get_map_type(); |
10806 | if (type == NULL) | |
746d2e73 | 10807 | { |
2c809f8f | 10808 | go_assert(saw_errors()); |
10809 | return Expression::make_error(this->location()); | |
746d2e73 | 10810 | } |
e440a328 | 10811 | |
2c809f8f | 10812 | Location loc = this->location(); |
10813 | Expression* map_ref = this->map_; | |
10814 | if (this->map_->type()->points_to() != NULL) | |
10815 | map_ref = Expression::make_unary(OPERATOR_MULT, map_ref, loc); | |
e440a328 | 10816 | |
2c809f8f | 10817 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, this->index_, |
10818 | loc); | |
10819 | Expression* map_index = | |
10820 | Runtime::make_call(Runtime::MAP_INDEX, loc, 3, | |
10821 | map_ref, index_ptr, | |
10822 | Expression::make_boolean(insert, loc)); | |
10823 | ||
10824 | Type* val_type = type->val_type(); | |
10825 | this->value_pointer_ = | |
10826 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
10827 | map_index, this->location()); | |
10828 | } | |
10829 | return this->value_pointer_; | |
e440a328 | 10830 | } |
10831 | ||
d751bb78 | 10832 | // Dump ast representation for a map index expression |
10833 | ||
10834 | void | |
10835 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10836 | const | |
10837 | { | |
acf2b673 | 10838 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
10839 | this->index_, NULL, NULL); | |
d751bb78 | 10840 | } |
10841 | ||
e440a328 | 10842 | // Make a map index expression. |
10843 | ||
10844 | Map_index_expression* | |
10845 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 10846 | Location location) |
e440a328 | 10847 | { |
10848 | return new Map_index_expression(map, index, location); | |
10849 | } | |
10850 | ||
10851 | // Class Field_reference_expression. | |
10852 | ||
149eabc5 | 10853 | // Lower a field reference expression. There is nothing to lower, but |
10854 | // this is where we generate the tracking information for fields with | |
10855 | // the magic go:"track" tag. | |
10856 | ||
10857 | Expression* | |
10858 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
10859 | Statement_inserter* inserter, int) | |
10860 | { | |
10861 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
10862 | if (struct_type == NULL) | |
10863 | { | |
10864 | // Error will be reported elsewhere. | |
10865 | return this; | |
10866 | } | |
10867 | const Struct_field* field = struct_type->field(this->field_index_); | |
10868 | if (field == NULL) | |
10869 | return this; | |
10870 | if (!field->has_tag()) | |
10871 | return this; | |
10872 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
10873 | return this; | |
10874 | ||
604e278d | 10875 | // References from functions generated by the compiler don't count. |
c6292d1d | 10876 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 10877 | return this; |
10878 | ||
149eabc5 | 10879 | // We have found a reference to a tracked field. Build a call to |
10880 | // the runtime function __go_fieldtrack with a string that describes | |
10881 | // the field. FIXME: We should only call this once per referenced | |
10882 | // field per function, not once for each reference to the field. | |
10883 | ||
10884 | if (this->called_fieldtrack_) | |
10885 | return this; | |
10886 | this->called_fieldtrack_ = true; | |
10887 | ||
10888 | Location loc = this->location(); | |
10889 | ||
10890 | std::string s = "fieldtrack \""; | |
10891 | Named_type* nt = this->expr_->type()->named_type(); | |
10892 | if (nt == NULL || nt->named_object()->package() == NULL) | |
10893 | s.append(gogo->pkgpath()); | |
10894 | else | |
10895 | s.append(nt->named_object()->package()->pkgpath()); | |
10896 | s.push_back('.'); | |
10897 | if (nt != NULL) | |
5c29ad36 | 10898 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 10899 | s.push_back('.'); |
10900 | s.append(field->field_name()); | |
10901 | s.push_back('"'); | |
10902 | ||
10903 | // We can't use a string here, because internally a string holds a | |
10904 | // pointer to the actual bytes; when the linker garbage collects the | |
10905 | // string, it won't garbage collect the bytes. So we use a | |
10906 | // [...]byte. | |
10907 | ||
e67508fa | 10908 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 10909 | |
10910 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
10911 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
10912 | ||
10913 | Expression_list* bytes = new Expression_list(); | |
10914 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
10915 | { | |
e67508fa | 10916 | unsigned char c = static_cast<unsigned char>(*p); |
10917 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 10918 | } |
10919 | ||
10920 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 10921 | bytes, false, loc); |
149eabc5 | 10922 | |
10923 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
10924 | ||
10925 | static int count; | |
10926 | char buf[50]; | |
10927 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
10928 | ++count; | |
10929 | ||
10930 | Named_object* no = gogo->add_variable(buf, var); | |
10931 | e = Expression::make_var_reference(no, loc); | |
10932 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
10933 | ||
10934 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 10935 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 10936 | inserter->insert(Statement::make_statement(call, false)); |
10937 | ||
10938 | // Put this function, and the global variable we just created, into | |
10939 | // unique sections. This will permit the linker to garbage collect | |
10940 | // them if they are not referenced. The effect is that the only | |
10941 | // strings, indicating field references, that will wind up in the | |
10942 | // executable will be those for functions that are actually needed. | |
66a6be58 | 10943 | if (function != NULL) |
10944 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 10945 | var->set_in_unique_section(); |
10946 | ||
10947 | return this; | |
10948 | } | |
10949 | ||
e440a328 | 10950 | // Return the type of a field reference. |
10951 | ||
10952 | Type* | |
10953 | Field_reference_expression::do_type() | |
10954 | { | |
b0e628fb | 10955 | Type* type = this->expr_->type(); |
5c13bd80 | 10956 | if (type->is_error()) |
b0e628fb | 10957 | return type; |
10958 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 10959 | go_assert(struct_type != NULL); |
e440a328 | 10960 | return struct_type->field(this->field_index_)->type(); |
10961 | } | |
10962 | ||
10963 | // Check the types for a field reference. | |
10964 | ||
10965 | void | |
10966 | Field_reference_expression::do_check_types(Gogo*) | |
10967 | { | |
b0e628fb | 10968 | Type* type = this->expr_->type(); |
5c13bd80 | 10969 | if (type->is_error()) |
b0e628fb | 10970 | return; |
10971 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 10972 | go_assert(struct_type != NULL); |
10973 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 10974 | } |
10975 | ||
ea664253 | 10976 | // Get the backend representation for a field reference. |
e440a328 | 10977 | |
ea664253 | 10978 | Bexpression* |
10979 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10980 | { |
ea664253 | 10981 | Bexpression* bstruct = this->expr_->get_backend(context); |
10982 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
10983 | this->field_index_, | |
10984 | this->location()); | |
e440a328 | 10985 | } |
10986 | ||
d751bb78 | 10987 | // Dump ast representation for a field reference expression. |
10988 | ||
10989 | void | |
10990 | Field_reference_expression::do_dump_expression( | |
10991 | Ast_dump_context* ast_dump_context) const | |
10992 | { | |
10993 | this->expr_->dump_expression(ast_dump_context); | |
10994 | ast_dump_context->ostream() << "." << this->field_index_; | |
10995 | } | |
10996 | ||
e440a328 | 10997 | // Make a reference to a qualified identifier in an expression. |
10998 | ||
10999 | Field_reference_expression* | |
11000 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11001 | Location location) |
e440a328 | 11002 | { |
11003 | return new Field_reference_expression(expr, field_index, location); | |
11004 | } | |
11005 | ||
11006 | // Class Interface_field_reference_expression. | |
11007 | ||
2387f644 | 11008 | // Return an expression for the pointer to the function to call. |
e440a328 | 11009 | |
2387f644 | 11010 | Expression* |
11011 | Interface_field_reference_expression::get_function() | |
e440a328 | 11012 | { |
2387f644 | 11013 | Expression* ref = this->expr_; |
11014 | Location loc = this->location(); | |
11015 | if (ref->type()->points_to() != NULL) | |
11016 | ref = Expression::make_unary(OPERATOR_MULT, ref, loc); | |
e440a328 | 11017 | |
2387f644 | 11018 | Expression* mtable = |
11019 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11020 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11021 | |
11022 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11023 | unsigned int index; |
11024 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11025 | go_assert(field != NULL); | |
11026 | mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc); | |
11027 | return Expression::make_field_reference(mtable, index, loc); | |
e440a328 | 11028 | } |
11029 | ||
2387f644 | 11030 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11031 | // function. |
11032 | ||
2387f644 | 11033 | Expression* |
11034 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11035 | { |
2387f644 | 11036 | Expression* expr = this->expr_; |
11037 | if (expr->type()->points_to() != NULL) | |
11038 | expr = Expression::make_unary(OPERATOR_MULT, expr, this->location()); | |
11039 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, | |
11040 | this->location()); | |
e440a328 | 11041 | } |
11042 | ||
11043 | // Traversal. | |
11044 | ||
11045 | int | |
11046 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11047 | { | |
11048 | return Expression::traverse(&this->expr_, traverse); | |
11049 | } | |
11050 | ||
0afbb937 | 11051 | // Lower the expression. If this expression is not called, we need to |
11052 | // evaluate the expression twice when converting to the backend | |
11053 | // interface. So introduce a temporary variable if necessary. | |
11054 | ||
11055 | Expression* | |
9782d556 | 11056 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11057 | Statement_inserter* inserter) | |
0afbb937 | 11058 | { |
5bf8be8b | 11059 | if (this->expr_->is_error_expression() |
11060 | || this->expr_->type()->is_error_type()) | |
11061 | { | |
11062 | go_assert(saw_errors()); | |
11063 | return Expression::make_error(this->location()); | |
11064 | } | |
11065 | ||
2387f644 | 11066 | if (!this->expr_->is_variable()) |
0afbb937 | 11067 | { |
11068 | Temporary_statement* temp = | |
11069 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11070 | inserter->insert(temp); | |
11071 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11072 | this->location()); | |
11073 | } | |
11074 | return this; | |
11075 | } | |
11076 | ||
e440a328 | 11077 | // Return the type of an interface field reference. |
11078 | ||
11079 | Type* | |
11080 | Interface_field_reference_expression::do_type() | |
11081 | { | |
11082 | Type* expr_type = this->expr_->type(); | |
11083 | ||
11084 | Type* points_to = expr_type->points_to(); | |
11085 | if (points_to != NULL) | |
11086 | expr_type = points_to; | |
11087 | ||
11088 | Interface_type* interface_type = expr_type->interface_type(); | |
11089 | if (interface_type == NULL) | |
11090 | return Type::make_error_type(); | |
11091 | ||
11092 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11093 | if (method == NULL) | |
11094 | return Type::make_error_type(); | |
11095 | ||
11096 | return method->type(); | |
11097 | } | |
11098 | ||
11099 | // Determine types. | |
11100 | ||
11101 | void | |
11102 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11103 | { | |
11104 | this->expr_->determine_type_no_context(); | |
11105 | } | |
11106 | ||
11107 | // Check the types for an interface field reference. | |
11108 | ||
11109 | void | |
11110 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11111 | { | |
11112 | Type* type = this->expr_->type(); | |
11113 | ||
11114 | Type* points_to = type->points_to(); | |
11115 | if (points_to != NULL) | |
11116 | type = points_to; | |
11117 | ||
11118 | Interface_type* interface_type = type->interface_type(); | |
11119 | if (interface_type == NULL) | |
5c491127 | 11120 | { |
11121 | if (!type->is_error_type()) | |
11122 | this->report_error(_("expected interface or pointer to interface")); | |
11123 | } | |
e440a328 | 11124 | else |
11125 | { | |
11126 | const Typed_identifier* method = | |
11127 | interface_type->find_method(this->name_); | |
11128 | if (method == NULL) | |
11129 | { | |
11130 | error_at(this->location(), "method %qs not in interface", | |
11131 | Gogo::message_name(this->name_).c_str()); | |
11132 | this->set_is_error(); | |
11133 | } | |
11134 | } | |
11135 | } | |
11136 | ||
0afbb937 | 11137 | // If an interface field reference is not simply called, then it is |
11138 | // represented as a closure. The closure will hold a single variable, | |
11139 | // the value of the interface on which the method should be called. | |
11140 | // The function will be a simple thunk that pulls the value from the | |
11141 | // closure and calls the method with the remaining arguments. | |
11142 | ||
11143 | // Because method values are not common, we don't build all thunks for | |
11144 | // all possible interface methods, but instead only build them as we | |
11145 | // need them. In particular, we even build them on demand for | |
11146 | // interface methods defined in other packages. | |
11147 | ||
11148 | Interface_field_reference_expression::Interface_method_thunks | |
11149 | Interface_field_reference_expression::interface_method_thunks; | |
11150 | ||
11151 | // Find or create the thunk to call method NAME on TYPE. | |
11152 | ||
11153 | Named_object* | |
11154 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11155 | Interface_type* type, | |
11156 | const std::string& name) | |
11157 | { | |
11158 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11159 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11160 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11161 | if (ins.second) | |
11162 | { | |
11163 | // This is the first time we have seen this interface. | |
11164 | ins.first->second = new Method_thunks(); | |
11165 | } | |
11166 | ||
11167 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11168 | p != ins.first->second->end(); | |
11169 | p++) | |
11170 | if (p->first == name) | |
11171 | return p->second; | |
11172 | ||
11173 | Location loc = type->location(); | |
11174 | ||
11175 | const Typed_identifier* method_id = type->find_method(name); | |
11176 | if (method_id == NULL) | |
11177 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11178 | ||
11179 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11180 | if (orig_fntype == NULL) | |
11181 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11182 | ||
11183 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11184 | // The type here is wrong--it should be the C function type. But it |
11185 | // doesn't really matter. | |
0afbb937 | 11186 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11187 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11188 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
11189 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
11190 | closure_type = Type::make_pointer_type(closure_type); | |
11191 | ||
f8bdf81a | 11192 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11193 | |
da244e59 | 11194 | std::string thunk_name = Gogo::thunk_name(); |
11195 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 11196 | false, loc); |
11197 | ||
f8bdf81a | 11198 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11199 | cvar->set_is_used(); | |
1ecc6157 | 11200 | cvar->set_is_closure(); |
da244e59 | 11201 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
11202 | NULL, cvar); | |
f8bdf81a | 11203 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 11204 | |
f8bdf81a | 11205 | gogo->start_block(loc); |
0afbb937 | 11206 | |
11207 | // Field 0 of the closure is the function code pointer, field 1 is | |
11208 | // the value on which to invoke the method. | |
11209 | Expression* arg = Expression::make_var_reference(cp, loc); | |
11210 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
11211 | arg = Expression::make_field_reference(arg, 1, loc); | |
11212 | ||
11213 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11214 | loc); | |
11215 | ||
11216 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11217 | Expression_list* args; | |
11218 | if (orig_params == NULL || orig_params->empty()) | |
11219 | args = NULL; | |
11220 | else | |
11221 | { | |
11222 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11223 | args = new Expression_list(); | |
11224 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11225 | p != new_params->end(); |
0afbb937 | 11226 | ++p) |
11227 | { | |
11228 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11229 | go_assert(p_no != NULL | |
11230 | && p_no->is_variable() | |
11231 | && p_no->var_value()->is_parameter()); | |
11232 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11233 | } | |
11234 | } | |
11235 | ||
11236 | Call_expression* call = Expression::make_call(ifre, args, | |
11237 | orig_fntype->is_varargs(), | |
11238 | loc); | |
11239 | call->set_varargs_are_lowered(); | |
11240 | ||
11241 | Statement* s = Statement::make_return_from_call(call, loc); | |
11242 | gogo->add_statement(s); | |
11243 | Block* b = gogo->finish_block(loc); | |
11244 | gogo->add_block(b, loc); | |
11245 | gogo->lower_block(new_no, b); | |
a32698ee | 11246 | gogo->flatten_block(new_no, b); |
0afbb937 | 11247 | gogo->finish_function(loc); |
11248 | ||
11249 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11250 | return new_no; | |
11251 | } | |
11252 | ||
ea664253 | 11253 | // Get the backend representation for a method value. |
e440a328 | 11254 | |
ea664253 | 11255 | Bexpression* |
11256 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11257 | { |
0afbb937 | 11258 | Interface_type* type = this->expr_->type()->interface_type(); |
11259 | if (type == NULL) | |
11260 | { | |
11261 | go_assert(saw_errors()); | |
ea664253 | 11262 | return context->backend()->error_expression(); |
0afbb937 | 11263 | } |
11264 | ||
11265 | Named_object* thunk = | |
11266 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11267 | type, this->name_); | |
11268 | if (thunk->is_erroneous()) | |
11269 | { | |
11270 | go_assert(saw_errors()); | |
ea664253 | 11271 | return context->backend()->error_expression(); |
0afbb937 | 11272 | } |
11273 | ||
11274 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11275 | // the lowering pass because at that point we don't know whether we | |
11276 | // need to create the thunk or not. If the expression is called, we | |
11277 | // don't need the thunk. | |
11278 | ||
11279 | Location loc = this->location(); | |
11280 | ||
11281 | Struct_field_list* fields = new Struct_field_list(); | |
11282 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11283 | thunk->func_value()->type(), | |
11284 | loc))); | |
11285 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11286 | this->expr_->type(), | |
11287 | loc))); | |
11288 | Struct_type* st = Type::make_struct_type(fields, loc); | |
11289 | ||
11290 | Expression_list* vals = new Expression_list(); | |
11291 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
11292 | vals->push_back(this->expr_); | |
11293 | ||
11294 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 11295 | Bexpression* bclosure = |
11296 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 11297 | |
2387f644 | 11298 | Expression* nil_check = |
11299 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
11300 | Expression::make_nil(loc), loc); | |
ea664253 | 11301 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 11302 | |
2387f644 | 11303 | Gogo* gogo = context->gogo(); |
ea664253 | 11304 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
11305 | loc)->get_backend(context); | |
2387f644 | 11306 | |
11307 | Bexpression* bcond = | |
a32698ee | 11308 | gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc); |
2387f644 | 11309 | Bstatement* cond_statement = gogo->backend()->expression_statement(bcond); |
ea664253 | 11310 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 11311 | } |
11312 | ||
d751bb78 | 11313 | // Dump ast representation for an interface field reference. |
11314 | ||
11315 | void | |
11316 | Interface_field_reference_expression::do_dump_expression( | |
11317 | Ast_dump_context* ast_dump_context) const | |
11318 | { | |
11319 | this->expr_->dump_expression(ast_dump_context); | |
11320 | ast_dump_context->ostream() << "." << this->name_; | |
11321 | } | |
11322 | ||
e440a328 | 11323 | // Make a reference to a field in an interface. |
11324 | ||
11325 | Expression* | |
11326 | Expression::make_interface_field_reference(Expression* expr, | |
11327 | const std::string& field, | |
b13c66cd | 11328 | Location location) |
e440a328 | 11329 | { |
11330 | return new Interface_field_reference_expression(expr, field, location); | |
11331 | } | |
11332 | ||
11333 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
11334 | // is lowered after we know the type of the left hand side. | |
11335 | ||
11336 | class Selector_expression : public Parser_expression | |
11337 | { | |
11338 | public: | |
11339 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 11340 | Location location) |
e440a328 | 11341 | : Parser_expression(EXPRESSION_SELECTOR, location), |
11342 | left_(left), name_(name) | |
11343 | { } | |
11344 | ||
11345 | protected: | |
11346 | int | |
11347 | do_traverse(Traverse* traverse) | |
11348 | { return Expression::traverse(&this->left_, traverse); } | |
11349 | ||
11350 | Expression* | |
ceeb4318 | 11351 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 11352 | |
11353 | Expression* | |
11354 | do_copy() | |
11355 | { | |
11356 | return new Selector_expression(this->left_->copy(), this->name_, | |
11357 | this->location()); | |
11358 | } | |
11359 | ||
d751bb78 | 11360 | void |
11361 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
11362 | ||
e440a328 | 11363 | private: |
11364 | Expression* | |
11365 | lower_method_expression(Gogo*); | |
11366 | ||
11367 | // The expression on the left hand side. | |
11368 | Expression* left_; | |
11369 | // The name on the right hand side. | |
11370 | std::string name_; | |
11371 | }; | |
11372 | ||
11373 | // Lower a selector expression once we know the real type of the left | |
11374 | // hand side. | |
11375 | ||
11376 | Expression* | |
ceeb4318 | 11377 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
11378 | int) | |
e440a328 | 11379 | { |
11380 | Expression* left = this->left_; | |
11381 | if (left->is_type_expression()) | |
11382 | return this->lower_method_expression(gogo); | |
11383 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
11384 | this->location()); | |
11385 | } | |
11386 | ||
11387 | // Lower a method expression T.M or (*T).M. We turn this into a | |
11388 | // function literal. | |
11389 | ||
11390 | Expression* | |
11391 | Selector_expression::lower_method_expression(Gogo* gogo) | |
11392 | { | |
b13c66cd | 11393 | Location location = this->location(); |
e440a328 | 11394 | Type* type = this->left_->type(); |
11395 | const std::string& name(this->name_); | |
11396 | ||
11397 | bool is_pointer; | |
11398 | if (type->points_to() == NULL) | |
11399 | is_pointer = false; | |
11400 | else | |
11401 | { | |
11402 | is_pointer = true; | |
11403 | type = type->points_to(); | |
11404 | } | |
11405 | Named_type* nt = type->named_type(); | |
11406 | if (nt == NULL) | |
11407 | { | |
11408 | error_at(location, | |
11409 | ("method expression requires named type or " | |
11410 | "pointer to named type")); | |
11411 | return Expression::make_error(location); | |
11412 | } | |
11413 | ||
11414 | bool is_ambiguous; | |
11415 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 11416 | const Typed_identifier* imethod = NULL; |
dcc8506b | 11417 | if (method == NULL && !is_pointer) |
ab1468c3 | 11418 | { |
11419 | Interface_type* it = nt->interface_type(); | |
11420 | if (it != NULL) | |
11421 | imethod = it->find_method(name); | |
11422 | } | |
11423 | ||
11424 | if (method == NULL && imethod == NULL) | |
e440a328 | 11425 | { |
11426 | if (!is_ambiguous) | |
dcc8506b | 11427 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
11428 | is_pointer ? "*" : "", | |
e440a328 | 11429 | nt->message_name().c_str(), |
11430 | Gogo::message_name(name).c_str()); | |
11431 | else | |
dcc8506b | 11432 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 11433 | Gogo::message_name(name).c_str(), |
dcc8506b | 11434 | is_pointer ? "*" : "", |
e440a328 | 11435 | nt->message_name().c_str()); |
11436 | return Expression::make_error(location); | |
11437 | } | |
11438 | ||
ab1468c3 | 11439 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 11440 | { |
11441 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
11442 | nt->message_name().c_str(), | |
11443 | Gogo::message_name(name).c_str()); | |
11444 | return Expression::make_error(location); | |
11445 | } | |
11446 | ||
11447 | // Build a new function type in which the receiver becomes the first | |
11448 | // argument. | |
ab1468c3 | 11449 | Function_type* method_type; |
11450 | if (method != NULL) | |
11451 | { | |
11452 | method_type = method->type(); | |
c484d925 | 11453 | go_assert(method_type->is_method()); |
ab1468c3 | 11454 | } |
11455 | else | |
11456 | { | |
11457 | method_type = imethod->type()->function_type(); | |
c484d925 | 11458 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 11459 | } |
e440a328 | 11460 | |
11461 | const char* const receiver_name = "$this"; | |
11462 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
11463 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
11464 | location)); | |
11465 | ||
11466 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
11467 | if (method_parameters != NULL) | |
11468 | { | |
f470da59 | 11469 | int i = 0; |
e440a328 | 11470 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
11471 | p != method_parameters->end(); | |
f470da59 | 11472 | ++p, ++i) |
11473 | { | |
68883531 | 11474 | if (!p->name().empty()) |
f470da59 | 11475 | parameters->push_back(*p); |
11476 | else | |
11477 | { | |
11478 | char buf[20]; | |
11479 | snprintf(buf, sizeof buf, "$param%d", i); | |
11480 | parameters->push_back(Typed_identifier(buf, p->type(), | |
11481 | p->location())); | |
11482 | } | |
11483 | } | |
e440a328 | 11484 | } |
11485 | ||
11486 | const Typed_identifier_list* method_results = method_type->results(); | |
11487 | Typed_identifier_list* results; | |
11488 | if (method_results == NULL) | |
11489 | results = NULL; | |
11490 | else | |
11491 | { | |
11492 | results = new Typed_identifier_list(); | |
11493 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
11494 | p != method_results->end(); | |
11495 | ++p) | |
11496 | results->push_back(*p); | |
11497 | } | |
11498 | ||
11499 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
11500 | location); | |
11501 | if (method_type->is_varargs()) | |
11502 | fntype->set_is_varargs(); | |
11503 | ||
11504 | // We generate methods which always takes a pointer to the receiver | |
11505 | // as their first argument. If this is for a pointer type, we can | |
11506 | // simply reuse the existing function. We use an internal hack to | |
11507 | // get the right type. | |
8381eda7 | 11508 | // FIXME: This optimization is disabled because it doesn't yet work |
11509 | // with function descriptors when the method expression is not | |
11510 | // directly called. | |
11511 | if (method != NULL && is_pointer && false) | |
e440a328 | 11512 | { |
11513 | Named_object* mno = (method->needs_stub_method() | |
11514 | ? method->stub_object() | |
11515 | : method->named_object()); | |
11516 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
11517 | f = Expression::make_cast(fntype, f, location); | |
11518 | Type_conversion_expression* tce = | |
11519 | static_cast<Type_conversion_expression*>(f); | |
11520 | tce->set_may_convert_function_types(); | |
11521 | return f; | |
11522 | } | |
11523 | ||
11524 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
11525 | location); | |
11526 | ||
11527 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 11528 | go_assert(vno != NULL); |
e440a328 | 11529 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 11530 | Expression* bm; |
11531 | if (method != NULL) | |
11532 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
11533 | else | |
11534 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 11535 | |
11536 | // Even though we found the method above, if it has an error type we | |
11537 | // may see an error here. | |
11538 | if (bm->is_error_expression()) | |
463fe805 | 11539 | { |
11540 | gogo->finish_function(location); | |
11541 | return bm; | |
11542 | } | |
e440a328 | 11543 | |
11544 | Expression_list* args; | |
f470da59 | 11545 | if (parameters->size() <= 1) |
e440a328 | 11546 | args = NULL; |
11547 | else | |
11548 | { | |
11549 | args = new Expression_list(); | |
f470da59 | 11550 | Typed_identifier_list::const_iterator p = parameters->begin(); |
11551 | ++p; | |
11552 | for (; p != parameters->end(); ++p) | |
e440a328 | 11553 | { |
11554 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 11555 | go_assert(vno != NULL); |
e440a328 | 11556 | args->push_back(Expression::make_var_reference(vno, location)); |
11557 | } | |
11558 | } | |
11559 | ||
ceeb4318 | 11560 | gogo->start_block(location); |
11561 | ||
e440a328 | 11562 | Call_expression* call = Expression::make_call(bm, args, |
11563 | method_type->is_varargs(), | |
11564 | location); | |
11565 | ||
0afbb937 | 11566 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 11567 | gogo->add_statement(s); |
11568 | ||
ceeb4318 | 11569 | Block* b = gogo->finish_block(location); |
11570 | ||
11571 | gogo->add_block(b, location); | |
11572 | ||
11573 | // Lower the call in case there are multiple results. | |
11574 | gogo->lower_block(no, b); | |
a32698ee | 11575 | gogo->flatten_block(no, b); |
ceeb4318 | 11576 | |
e440a328 | 11577 | gogo->finish_function(location); |
11578 | ||
11579 | return Expression::make_func_reference(no, NULL, location); | |
11580 | } | |
11581 | ||
d751bb78 | 11582 | // Dump the ast for a selector expression. |
11583 | ||
11584 | void | |
11585 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11586 | const | |
11587 | { | |
11588 | ast_dump_context->dump_expression(this->left_); | |
11589 | ast_dump_context->ostream() << "."; | |
11590 | ast_dump_context->ostream() << this->name_; | |
11591 | } | |
11592 | ||
e440a328 | 11593 | // Make a selector expression. |
11594 | ||
11595 | Expression* | |
11596 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 11597 | Location location) |
e440a328 | 11598 | { |
11599 | return new Selector_expression(left, name, location); | |
11600 | } | |
11601 | ||
da244e59 | 11602 | // Class Allocation_expression. |
e440a328 | 11603 | |
da244e59 | 11604 | int |
11605 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 11606 | { |
da244e59 | 11607 | return Type::traverse(this->type_, traverse); |
11608 | } | |
e440a328 | 11609 | |
da244e59 | 11610 | Type* |
11611 | Allocation_expression::do_type() | |
11612 | { | |
11613 | return Type::make_pointer_type(this->type_); | |
11614 | } | |
e440a328 | 11615 | |
da244e59 | 11616 | // Make a copy of an allocation expression. |
e440a328 | 11617 | |
da244e59 | 11618 | Expression* |
11619 | Allocation_expression::do_copy() | |
11620 | { | |
11621 | Allocation_expression* alloc = | |
11622 | new Allocation_expression(this->type_, this->location()); | |
11623 | if (this->allocate_on_stack_) | |
11624 | alloc->set_allocate_on_stack(); | |
11625 | return alloc; | |
11626 | } | |
e440a328 | 11627 | |
ea664253 | 11628 | // Return the backend representation for an allocation expression. |
e440a328 | 11629 | |
ea664253 | 11630 | Bexpression* |
11631 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11632 | { |
2c809f8f | 11633 | Gogo* gogo = context->gogo(); |
11634 | Location loc = this->location(); | |
da244e59 | 11635 | |
d5d1c295 | 11636 | if (this->allocate_on_stack_) |
da244e59 | 11637 | { |
2a305b85 | 11638 | int64_t size; |
11639 | bool ok = this->type_->backend_type_size(gogo, &size); | |
11640 | if (!ok) | |
11641 | { | |
11642 | go_assert(saw_errors()); | |
11643 | return gogo->backend()->error_expression(); | |
11644 | } | |
d5d1c295 | 11645 | return gogo->backend()->stack_allocation_expression(size, loc); |
da244e59 | 11646 | } |
11647 | ||
2a305b85 | 11648 | Btype* btype = this->type_->get_backend(gogo); |
11649 | Bexpression* space = | |
ea664253 | 11650 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 11651 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 11652 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 11653 | } |
11654 | ||
d751bb78 | 11655 | // Dump ast representation for an allocation expression. |
11656 | ||
11657 | void | |
11658 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11659 | const | |
11660 | { | |
11661 | ast_dump_context->ostream() << "new("; | |
11662 | ast_dump_context->dump_type(this->type_); | |
11663 | ast_dump_context->ostream() << ")"; | |
11664 | } | |
11665 | ||
e440a328 | 11666 | // Make an allocation expression. |
11667 | ||
11668 | Expression* | |
b13c66cd | 11669 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 11670 | { |
11671 | return new Allocation_expression(type, location); | |
11672 | } | |
11673 | ||
da244e59 | 11674 | // Class Struct_construction_expression. |
e440a328 | 11675 | |
11676 | // Traversal. | |
11677 | ||
11678 | int | |
11679 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
11680 | { | |
0c4f5a19 | 11681 | if (this->vals_ != NULL) |
11682 | { | |
11683 | if (this->traverse_order_ == NULL) | |
11684 | { | |
11685 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11686 | return TRAVERSE_EXIT; | |
11687 | } | |
11688 | else | |
11689 | { | |
11690 | for (std::vector<int>::const_iterator p = | |
11691 | this->traverse_order_->begin(); | |
11692 | p != this->traverse_order_->end(); | |
11693 | ++p) | |
11694 | { | |
11695 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
11696 | == TRAVERSE_EXIT) | |
11697 | return TRAVERSE_EXIT; | |
11698 | } | |
11699 | } | |
11700 | } | |
e440a328 | 11701 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
11702 | return TRAVERSE_EXIT; | |
11703 | return TRAVERSE_CONTINUE; | |
11704 | } | |
11705 | ||
11706 | // Return whether this is a constant initializer. | |
11707 | ||
11708 | bool | |
11709 | Struct_construction_expression::is_constant_struct() const | |
11710 | { | |
11711 | if (this->vals_ == NULL) | |
11712 | return true; | |
11713 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11714 | pv != this->vals_->end(); | |
11715 | ++pv) | |
11716 | { | |
11717 | if (*pv != NULL | |
11718 | && !(*pv)->is_constant() | |
11719 | && (!(*pv)->is_composite_literal() | |
11720 | || (*pv)->is_nonconstant_composite_literal())) | |
11721 | return false; | |
11722 | } | |
11723 | ||
11724 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11725 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11726 | pf != fields->end(); | |
11727 | ++pf) | |
11728 | { | |
11729 | // There are no constant constructors for interfaces. | |
11730 | if (pf->type()->interface_type() != NULL) | |
11731 | return false; | |
11732 | } | |
11733 | ||
11734 | return true; | |
11735 | } | |
11736 | ||
f9ca30f9 | 11737 | // Return whether this struct is immutable. |
11738 | ||
11739 | bool | |
11740 | Struct_construction_expression::do_is_immutable() const | |
11741 | { | |
11742 | if (this->vals_ == NULL) | |
11743 | return true; | |
11744 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11745 | pv != this->vals_->end(); | |
11746 | ++pv) | |
11747 | { | |
11748 | if (*pv != NULL && !(*pv)->is_immutable()) | |
11749 | return false; | |
11750 | } | |
11751 | return true; | |
11752 | } | |
11753 | ||
e440a328 | 11754 | // Final type determination. |
11755 | ||
11756 | void | |
11757 | Struct_construction_expression::do_determine_type(const Type_context*) | |
11758 | { | |
11759 | if (this->vals_ == NULL) | |
11760 | return; | |
11761 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11762 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11763 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11764 | pf != fields->end(); | |
11765 | ++pf, ++pv) | |
11766 | { | |
11767 | if (pv == this->vals_->end()) | |
11768 | return; | |
11769 | if (*pv != NULL) | |
11770 | { | |
11771 | Type_context subcontext(pf->type(), false); | |
11772 | (*pv)->determine_type(&subcontext); | |
11773 | } | |
11774 | } | |
a6cb4c0e | 11775 | // Extra values are an error we will report elsewhere; we still want |
11776 | // to determine the type to avoid knockon errors. | |
11777 | for (; pv != this->vals_->end(); ++pv) | |
11778 | (*pv)->determine_type_no_context(); | |
e440a328 | 11779 | } |
11780 | ||
11781 | // Check types. | |
11782 | ||
11783 | void | |
11784 | Struct_construction_expression::do_check_types(Gogo*) | |
11785 | { | |
11786 | if (this->vals_ == NULL) | |
11787 | return; | |
11788 | ||
11789 | Struct_type* st = this->type_->struct_type(); | |
11790 | if (this->vals_->size() > st->field_count()) | |
11791 | { | |
11792 | this->report_error(_("too many expressions for struct")); | |
11793 | return; | |
11794 | } | |
11795 | ||
11796 | const Struct_field_list* fields = st->fields(); | |
11797 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11798 | int i = 0; | |
11799 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11800 | pf != fields->end(); | |
11801 | ++pf, ++pv, ++i) | |
11802 | { | |
11803 | if (pv == this->vals_->end()) | |
11804 | { | |
11805 | this->report_error(_("too few expressions for struct")); | |
11806 | break; | |
11807 | } | |
11808 | ||
11809 | if (*pv == NULL) | |
11810 | continue; | |
11811 | ||
11812 | std::string reason; | |
11813 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
11814 | { | |
11815 | if (reason.empty()) | |
11816 | error_at((*pv)->location(), | |
11817 | "incompatible type for field %d in struct construction", | |
11818 | i + 1); | |
11819 | else | |
11820 | error_at((*pv)->location(), | |
11821 | ("incompatible type for field %d in " | |
11822 | "struct construction (%s)"), | |
11823 | i + 1, reason.c_str()); | |
11824 | this->set_is_error(); | |
11825 | } | |
11826 | } | |
c484d925 | 11827 | go_assert(pv == this->vals_->end()); |
e440a328 | 11828 | } |
11829 | ||
8ba8cc87 | 11830 | // Flatten a struct construction expression. Store the values into |
11831 | // temporaries in case they need interface conversion. | |
11832 | ||
11833 | Expression* | |
11834 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
11835 | Statement_inserter* inserter) | |
11836 | { | |
11837 | if (this->vals_ == NULL) | |
11838 | return this; | |
11839 | ||
11840 | // If this is a constant struct, we don't need temporaries. | |
11841 | if (this->is_constant_struct()) | |
11842 | return this; | |
11843 | ||
11844 | Location loc = this->location(); | |
11845 | for (Expression_list::iterator pv = this->vals_->begin(); | |
11846 | pv != this->vals_->end(); | |
11847 | ++pv) | |
11848 | { | |
11849 | if (*pv != NULL) | |
11850 | { | |
5bf8be8b | 11851 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
11852 | { | |
11853 | go_assert(saw_errors()); | |
11854 | return Expression::make_error(loc); | |
11855 | } | |
8ba8cc87 | 11856 | if (!(*pv)->is_variable()) |
11857 | { | |
11858 | Temporary_statement* temp = | |
11859 | Statement::make_temporary(NULL, *pv, loc); | |
11860 | inserter->insert(temp); | |
11861 | *pv = Expression::make_temporary_reference(temp, loc); | |
11862 | } | |
11863 | } | |
11864 | } | |
11865 | return this; | |
11866 | } | |
11867 | ||
ea664253 | 11868 | // Return the backend representation for constructing a struct. |
e440a328 | 11869 | |
ea664253 | 11870 | Bexpression* |
11871 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11872 | { |
11873 | Gogo* gogo = context->gogo(); | |
11874 | ||
2c809f8f | 11875 | Btype* btype = this->type_->get_backend(gogo); |
e440a328 | 11876 | if (this->vals_ == NULL) |
ea664253 | 11877 | return gogo->backend()->zero_expression(btype); |
e440a328 | 11878 | |
e440a328 | 11879 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e440a328 | 11880 | Expression_list::const_iterator pv = this->vals_->begin(); |
2c809f8f | 11881 | std::vector<Bexpression*> init; |
11882 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11883 | pf != fields->end(); | |
11884 | ++pf) | |
e440a328 | 11885 | { |
63697958 | 11886 | Btype* fbtype = pf->type()->get_backend(gogo); |
e440a328 | 11887 | if (pv == this->vals_->end()) |
2c809f8f | 11888 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 11889 | else if (*pv == NULL) |
11890 | { | |
2c809f8f | 11891 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 11892 | ++pv; |
11893 | } | |
11894 | else | |
11895 | { | |
2c809f8f | 11896 | Expression* val = |
11897 | Expression::convert_for_assignment(gogo, pf->type(), | |
11898 | *pv, this->location()); | |
ea664253 | 11899 | init.push_back(val->get_backend(context)); |
e440a328 | 11900 | ++pv; |
11901 | } | |
e440a328 | 11902 | } |
ea664253 | 11903 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 11904 | } |
11905 | ||
11906 | // Export a struct construction. | |
11907 | ||
11908 | void | |
11909 | Struct_construction_expression::do_export(Export* exp) const | |
11910 | { | |
11911 | exp->write_c_string("convert("); | |
11912 | exp->write_type(this->type_); | |
11913 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11914 | pv != this->vals_->end(); | |
11915 | ++pv) | |
11916 | { | |
11917 | exp->write_c_string(", "); | |
11918 | if (*pv != NULL) | |
11919 | (*pv)->export_expression(exp); | |
11920 | } | |
11921 | exp->write_c_string(")"); | |
11922 | } | |
11923 | ||
d751bb78 | 11924 | // Dump ast representation of a struct construction expression. |
11925 | ||
11926 | void | |
11927 | Struct_construction_expression::do_dump_expression( | |
11928 | Ast_dump_context* ast_dump_context) const | |
11929 | { | |
d751bb78 | 11930 | ast_dump_context->dump_type(this->type_); |
11931 | ast_dump_context->ostream() << "{"; | |
11932 | ast_dump_context->dump_expression_list(this->vals_); | |
11933 | ast_dump_context->ostream() << "}"; | |
11934 | } | |
11935 | ||
e440a328 | 11936 | // Make a struct composite literal. This used by the thunk code. |
11937 | ||
11938 | Expression* | |
11939 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 11940 | Location location) |
e440a328 | 11941 | { |
c484d925 | 11942 | go_assert(type->struct_type() != NULL); |
e440a328 | 11943 | return new Struct_construction_expression(type, vals, location); |
11944 | } | |
11945 | ||
da244e59 | 11946 | // Class Array_construction_expression. |
e440a328 | 11947 | |
11948 | // Traversal. | |
11949 | ||
11950 | int | |
11951 | Array_construction_expression::do_traverse(Traverse* traverse) | |
11952 | { | |
11953 | if (this->vals_ != NULL | |
11954 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11955 | return TRAVERSE_EXIT; | |
11956 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11957 | return TRAVERSE_EXIT; | |
11958 | return TRAVERSE_CONTINUE; | |
11959 | } | |
11960 | ||
11961 | // Return whether this is a constant initializer. | |
11962 | ||
11963 | bool | |
11964 | Array_construction_expression::is_constant_array() const | |
11965 | { | |
11966 | if (this->vals_ == NULL) | |
11967 | return true; | |
11968 | ||
11969 | // There are no constant constructors for interfaces. | |
11970 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
11971 | return false; | |
11972 | ||
11973 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11974 | pv != this->vals_->end(); | |
11975 | ++pv) | |
11976 | { | |
11977 | if (*pv != NULL | |
11978 | && !(*pv)->is_constant() | |
11979 | && (!(*pv)->is_composite_literal() | |
11980 | || (*pv)->is_nonconstant_composite_literal())) | |
11981 | return false; | |
11982 | } | |
11983 | return true; | |
11984 | } | |
11985 | ||
f9ca30f9 | 11986 | // Return whether this is an immutable array initializer. |
11987 | ||
11988 | bool | |
11989 | Array_construction_expression::do_is_immutable() const | |
11990 | { | |
11991 | if (this->vals_ == NULL) | |
11992 | return true; | |
11993 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11994 | pv != this->vals_->end(); | |
11995 | ++pv) | |
11996 | { | |
11997 | if (*pv != NULL && !(*pv)->is_immutable()) | |
11998 | return false; | |
11999 | } | |
12000 | return true; | |
12001 | } | |
12002 | ||
e440a328 | 12003 | // Final type determination. |
12004 | ||
12005 | void | |
12006 | Array_construction_expression::do_determine_type(const Type_context*) | |
12007 | { | |
12008 | if (this->vals_ == NULL) | |
12009 | return; | |
12010 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
12011 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12012 | pv != this->vals_->end(); | |
12013 | ++pv) | |
12014 | { | |
12015 | if (*pv != NULL) | |
12016 | (*pv)->determine_type(&subcontext); | |
12017 | } | |
12018 | } | |
12019 | ||
12020 | // Check types. | |
12021 | ||
12022 | void | |
12023 | Array_construction_expression::do_check_types(Gogo*) | |
12024 | { | |
12025 | if (this->vals_ == NULL) | |
12026 | return; | |
12027 | ||
12028 | Array_type* at = this->type_->array_type(); | |
12029 | int i = 0; | |
12030 | Type* element_type = at->element_type(); | |
12031 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12032 | pv != this->vals_->end(); | |
12033 | ++pv, ++i) | |
12034 | { | |
12035 | if (*pv != NULL | |
12036 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12037 | { | |
12038 | error_at((*pv)->location(), | |
12039 | "incompatible type for element %d in composite literal", | |
12040 | i + 1); | |
12041 | this->set_is_error(); | |
12042 | } | |
12043 | } | |
e440a328 | 12044 | } |
12045 | ||
8ba8cc87 | 12046 | // Flatten an array construction expression. Store the values into |
12047 | // temporaries in case they need interface conversion. | |
12048 | ||
12049 | Expression* | |
12050 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12051 | Statement_inserter* inserter) | |
12052 | { | |
12053 | if (this->vals_ == NULL) | |
12054 | return this; | |
12055 | ||
12056 | // If this is a constant array, we don't need temporaries. | |
12057 | if (this->is_constant_array()) | |
12058 | return this; | |
12059 | ||
12060 | Location loc = this->location(); | |
12061 | for (Expression_list::iterator pv = this->vals_->begin(); | |
12062 | pv != this->vals_->end(); | |
12063 | ++pv) | |
12064 | { | |
12065 | if (*pv != NULL) | |
12066 | { | |
5bf8be8b | 12067 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12068 | { | |
12069 | go_assert(saw_errors()); | |
12070 | return Expression::make_error(loc); | |
12071 | } | |
8ba8cc87 | 12072 | if (!(*pv)->is_variable()) |
12073 | { | |
12074 | Temporary_statement* temp = | |
12075 | Statement::make_temporary(NULL, *pv, loc); | |
12076 | inserter->insert(temp); | |
12077 | *pv = Expression::make_temporary_reference(temp, loc); | |
12078 | } | |
12079 | } | |
12080 | } | |
12081 | return this; | |
12082 | } | |
12083 | ||
2c809f8f | 12084 | // Get a constructor expression for the array values. |
e440a328 | 12085 | |
2c809f8f | 12086 | Bexpression* |
12087 | Array_construction_expression::get_constructor(Translate_context* context, | |
12088 | Btype* array_btype) | |
e440a328 | 12089 | { |
e440a328 | 12090 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12091 | |
12092 | std::vector<unsigned long> indexes; | |
12093 | std::vector<Bexpression*> vals; | |
12094 | Gogo* gogo = context->gogo(); | |
e440a328 | 12095 | if (this->vals_ != NULL) |
12096 | { | |
12097 | size_t i = 0; | |
ffe743ca | 12098 | std::vector<unsigned long>::const_iterator pi; |
12099 | if (this->indexes_ != NULL) | |
12100 | pi = this->indexes_->begin(); | |
e440a328 | 12101 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12102 | pv != this->vals_->end(); | |
12103 | ++pv, ++i) | |
12104 | { | |
ffe743ca | 12105 | if (this->indexes_ != NULL) |
12106 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12107 | |
12108 | if (this->indexes_ == NULL) | |
2c809f8f | 12109 | indexes.push_back(i); |
ffe743ca | 12110 | else |
2c809f8f | 12111 | indexes.push_back(*pi); |
e440a328 | 12112 | if (*pv == NULL) |
63697958 | 12113 | { |
63697958 | 12114 | Btype* ebtype = element_type->get_backend(gogo); |
12115 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12116 | vals.push_back(zv); |
63697958 | 12117 | } |
e440a328 | 12118 | else |
12119 | { | |
2c809f8f | 12120 | Expression* val_expr = |
12121 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12122 | this->location()); | |
ea664253 | 12123 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12124 | } |
ffe743ca | 12125 | if (this->indexes_ != NULL) |
12126 | ++pi; | |
e440a328 | 12127 | } |
ffe743ca | 12128 | if (this->indexes_ != NULL) |
12129 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12130 | } |
2c809f8f | 12131 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12132 | vals, this->location()); | |
e440a328 | 12133 | } |
12134 | ||
12135 | // Export an array construction. | |
12136 | ||
12137 | void | |
12138 | Array_construction_expression::do_export(Export* exp) const | |
12139 | { | |
12140 | exp->write_c_string("convert("); | |
12141 | exp->write_type(this->type_); | |
12142 | if (this->vals_ != NULL) | |
12143 | { | |
ffe743ca | 12144 | std::vector<unsigned long>::const_iterator pi; |
12145 | if (this->indexes_ != NULL) | |
12146 | pi = this->indexes_->begin(); | |
e440a328 | 12147 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12148 | pv != this->vals_->end(); | |
12149 | ++pv) | |
12150 | { | |
12151 | exp->write_c_string(", "); | |
ffe743ca | 12152 | |
12153 | if (this->indexes_ != NULL) | |
12154 | { | |
12155 | char buf[100]; | |
12156 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12157 | exp->write_c_string(buf); | |
12158 | exp->write_c_string(":"); | |
12159 | } | |
12160 | ||
e440a328 | 12161 | if (*pv != NULL) |
12162 | (*pv)->export_expression(exp); | |
ffe743ca | 12163 | |
12164 | if (this->indexes_ != NULL) | |
12165 | ++pi; | |
e440a328 | 12166 | } |
12167 | } | |
12168 | exp->write_c_string(")"); | |
12169 | } | |
12170 | ||
d751bb78 | 12171 | // Dump ast representation of an array construction expressin. |
12172 | ||
12173 | void | |
12174 | Array_construction_expression::do_dump_expression( | |
12175 | Ast_dump_context* ast_dump_context) const | |
12176 | { | |
ffe743ca | 12177 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12178 | |
12179 | ast_dump_context->ostream() << "[" ; | |
12180 | if (length != NULL) | |
12181 | { | |
12182 | ast_dump_context->dump_expression(length); | |
12183 | } | |
12184 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12185 | ast_dump_context->dump_type(this->type_); |
12186 | ast_dump_context->ostream() << "{" ; | |
ffe743ca | 12187 | if (this->indexes_ == NULL) |
12188 | ast_dump_context->dump_expression_list(this->vals_); | |
12189 | else | |
12190 | { | |
12191 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12192 | for (std::vector<unsigned long>::const_iterator pi = | |
12193 | this->indexes_->begin(); | |
12194 | pi != this->indexes_->end(); | |
12195 | ++pi, ++pv) | |
12196 | { | |
12197 | if (pi != this->indexes_->begin()) | |
12198 | ast_dump_context->ostream() << ", "; | |
12199 | ast_dump_context->ostream() << *pi << ':'; | |
12200 | ast_dump_context->dump_expression(*pv); | |
12201 | } | |
12202 | } | |
d751bb78 | 12203 | ast_dump_context->ostream() << "}" ; |
12204 | ||
12205 | } | |
12206 | ||
da244e59 | 12207 | // Class Fixed_array_construction_expression. |
e440a328 | 12208 | |
da244e59 | 12209 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
12210 | Type* type, const std::vector<unsigned long>* indexes, | |
12211 | Expression_list* vals, Location location) | |
12212 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
12213 | type, indexes, vals, location) | |
12214 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12215 | |
ea664253 | 12216 | // Return the backend representation for constructing a fixed array. |
e440a328 | 12217 | |
ea664253 | 12218 | Bexpression* |
12219 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12220 | { |
9f0e0513 | 12221 | Type* type = this->type(); |
12222 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 12223 | return this->get_constructor(context, btype); |
e440a328 | 12224 | } |
12225 | ||
76f85fd6 | 12226 | Expression* |
12227 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
12228 | Location location) | |
12229 | { | |
12230 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
12231 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
12232 | } | |
12233 | ||
da244e59 | 12234 | // Class Slice_construction_expression. |
e440a328 | 12235 | |
da244e59 | 12236 | Slice_construction_expression::Slice_construction_expression( |
12237 | Type* type, const std::vector<unsigned long>* indexes, | |
12238 | Expression_list* vals, Location location) | |
12239 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
12240 | type, indexes, vals, location), | |
12241 | valtype_(NULL) | |
e440a328 | 12242 | { |
da244e59 | 12243 | go_assert(type->is_slice_type()); |
23d77f91 | 12244 | |
da244e59 | 12245 | unsigned long lenval; |
12246 | Expression* length; | |
12247 | if (vals == NULL || vals->empty()) | |
12248 | lenval = 0; | |
12249 | else | |
12250 | { | |
12251 | if (this->indexes() == NULL) | |
12252 | lenval = vals->size(); | |
12253 | else | |
12254 | lenval = indexes->back() + 1; | |
12255 | } | |
12256 | Type* int_type = Type::lookup_integer_type("int"); | |
12257 | length = Expression::make_integer_ul(lenval, int_type, location); | |
12258 | Type* element_type = type->array_type()->element_type(); | |
12259 | this->valtype_ = Type::make_array_type(element_type, length); | |
12260 | } | |
e440a328 | 12261 | |
e440a328 | 12262 | |
23d77f91 | 12263 | // Traversal. |
12264 | ||
12265 | int | |
12266 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
12267 | { | |
12268 | if (this->Array_construction_expression::do_traverse(traverse) | |
12269 | == TRAVERSE_EXIT) | |
12270 | return TRAVERSE_EXIT; | |
12271 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
12272 | return TRAVERSE_EXIT; | |
12273 | return TRAVERSE_CONTINUE; | |
12274 | } | |
12275 | ||
ea664253 | 12276 | // Return the backend representation for constructing a slice. |
e440a328 | 12277 | |
ea664253 | 12278 | Bexpression* |
12279 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12280 | { |
f9c68f17 | 12281 | Array_type* array_type = this->type()->array_type(); |
12282 | if (array_type == NULL) | |
12283 | { | |
c484d925 | 12284 | go_assert(this->type()->is_error()); |
ea664253 | 12285 | return context->backend()->error_expression(); |
f9c68f17 | 12286 | } |
12287 | ||
f23d7786 | 12288 | Location loc = this->location(); |
f9c68f17 | 12289 | Type* element_type = array_type->element_type(); |
23d77f91 | 12290 | go_assert(this->valtype_ != NULL); |
3d60812e | 12291 | |
f23d7786 | 12292 | Expression_list* vals = this->vals(); |
e440a328 | 12293 | if (this->vals() == NULL || this->vals()->empty()) |
12294 | { | |
f23d7786 | 12295 | // We need to create a unique value for the empty array literal. |
12296 | vals = new Expression_list; | |
12297 | vals->push_back(NULL); | |
e440a328 | 12298 | } |
f23d7786 | 12299 | Expression* array_val = |
12300 | new Fixed_array_construction_expression(this->valtype_, this->indexes(), | |
12301 | vals, loc); | |
e440a328 | 12302 | |
f23d7786 | 12303 | bool is_constant_initializer = array_val->is_immutable(); |
d8829beb | 12304 | |
12305 | // We have to copy the initial values into heap memory if we are in | |
12306 | // a function or if the values are not constants. We also have to | |
12307 | // copy them if they may contain pointers in a non-constant context, | |
12308 | // as otherwise the garbage collector won't see them. | |
12309 | bool copy_to_heap = (context->function() != NULL | |
12310 | || !is_constant_initializer | |
12311 | || (element_type->has_pointer() | |
12312 | && !context->is_const())); | |
e440a328 | 12313 | |
f23d7786 | 12314 | Expression* space; |
d8829beb | 12315 | if (!copy_to_heap) |
e440a328 | 12316 | { |
f23d7786 | 12317 | // The initializer will only run once. |
12318 | space = Expression::make_unary(OPERATOR_AND, array_val, loc); | |
12319 | space->unary_expression()->set_is_slice_init(); | |
e440a328 | 12320 | } |
12321 | else | |
f23d7786 | 12322 | space = Expression::make_heap_expression(array_val, loc); |
e440a328 | 12323 | |
2c809f8f | 12324 | // Build a constructor for the slice. |
e440a328 | 12325 | |
f23d7786 | 12326 | Expression* len = this->valtype_->array_type()->length(); |
12327 | Expression* slice_val = | |
12328 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 12329 | return slice_val->get_backend(context); |
e440a328 | 12330 | } |
12331 | ||
12332 | // Make a slice composite literal. This is used by the type | |
12333 | // descriptor code. | |
12334 | ||
12335 | Expression* | |
12336 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12337 | Location location) |
e440a328 | 12338 | { |
411eb89e | 12339 | go_assert(type->is_slice_type()); |
2c809f8f | 12340 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 12341 | } |
12342 | ||
da244e59 | 12343 | // Class Map_construction_expression. |
e440a328 | 12344 | |
12345 | // Traversal. | |
12346 | ||
12347 | int | |
12348 | Map_construction_expression::do_traverse(Traverse* traverse) | |
12349 | { | |
12350 | if (this->vals_ != NULL | |
12351 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12352 | return TRAVERSE_EXIT; | |
12353 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12354 | return TRAVERSE_EXIT; | |
12355 | return TRAVERSE_CONTINUE; | |
12356 | } | |
12357 | ||
2c809f8f | 12358 | // Flatten constructor initializer into a temporary variable since |
12359 | // we need to take its address for __go_construct_map. | |
12360 | ||
12361 | Expression* | |
12362 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
12363 | Statement_inserter* inserter) | |
12364 | { | |
12365 | if (!this->is_error_expression() | |
12366 | && this->vals_ != NULL | |
12367 | && !this->vals_->empty() | |
12368 | && this->constructor_temp_ == NULL) | |
12369 | { | |
12370 | Map_type* mt = this->type_->map_type(); | |
12371 | Type* key_type = mt->key_type(); | |
12372 | Type* val_type = mt->val_type(); | |
12373 | this->element_type_ = Type::make_builtin_struct_type(2, | |
12374 | "__key", key_type, | |
12375 | "__val", val_type); | |
12376 | ||
12377 | Expression_list* value_pairs = new Expression_list(); | |
12378 | Location loc = this->location(); | |
12379 | ||
12380 | size_t i = 0; | |
12381 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12382 | pv != this->vals_->end(); | |
12383 | ++pv, ++i) | |
12384 | { | |
12385 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 12386 | Expression* key = *pv; |
5bf8be8b | 12387 | if (key->is_error_expression() || key->type()->is_error_type()) |
12388 | { | |
12389 | go_assert(saw_errors()); | |
12390 | return Expression::make_error(loc); | |
12391 | } | |
91c0fd76 | 12392 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
12393 | { | |
12394 | Temporary_statement* temp = | |
12395 | Statement::make_temporary(NULL, key, loc); | |
12396 | inserter->insert(temp); | |
12397 | key = Expression::make_temporary_reference(temp, loc); | |
12398 | } | |
12399 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 12400 | |
12401 | ++pv; | |
91c0fd76 | 12402 | Expression* val = *pv; |
5bf8be8b | 12403 | if (val->is_error_expression() || val->type()->is_error_type()) |
12404 | { | |
12405 | go_assert(saw_errors()); | |
12406 | return Expression::make_error(loc); | |
12407 | } | |
91c0fd76 | 12408 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
12409 | { | |
12410 | Temporary_statement* temp = | |
12411 | Statement::make_temporary(NULL, val, loc); | |
12412 | inserter->insert(temp); | |
12413 | val = Expression::make_temporary_reference(temp, loc); | |
12414 | } | |
12415 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 12416 | |
12417 | key_value_pair->push_back(key); | |
12418 | key_value_pair->push_back(val); | |
12419 | value_pairs->push_back( | |
12420 | Expression::make_struct_composite_literal(this->element_type_, | |
12421 | key_value_pair, loc)); | |
12422 | } | |
12423 | ||
e67508fa | 12424 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
2c809f8f | 12425 | Type* ctor_type = |
12426 | Type::make_array_type(this->element_type_, element_count); | |
12427 | Expression* constructor = | |
12428 | new Fixed_array_construction_expression(ctor_type, NULL, | |
12429 | value_pairs, loc); | |
12430 | ||
12431 | this->constructor_temp_ = | |
12432 | Statement::make_temporary(NULL, constructor, loc); | |
12433 | constructor->issue_nil_check(); | |
12434 | this->constructor_temp_->set_is_address_taken(); | |
12435 | inserter->insert(this->constructor_temp_); | |
12436 | } | |
12437 | ||
12438 | return this; | |
12439 | } | |
12440 | ||
e440a328 | 12441 | // Final type determination. |
12442 | ||
12443 | void | |
12444 | Map_construction_expression::do_determine_type(const Type_context*) | |
12445 | { | |
12446 | if (this->vals_ == NULL) | |
12447 | return; | |
12448 | ||
12449 | Map_type* mt = this->type_->map_type(); | |
12450 | Type_context key_context(mt->key_type(), false); | |
12451 | Type_context val_context(mt->val_type(), false); | |
12452 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12453 | pv != this->vals_->end(); | |
12454 | ++pv) | |
12455 | { | |
12456 | (*pv)->determine_type(&key_context); | |
12457 | ++pv; | |
12458 | (*pv)->determine_type(&val_context); | |
12459 | } | |
12460 | } | |
12461 | ||
12462 | // Check types. | |
12463 | ||
12464 | void | |
12465 | Map_construction_expression::do_check_types(Gogo*) | |
12466 | { | |
12467 | if (this->vals_ == NULL) | |
12468 | return; | |
12469 | ||
12470 | Map_type* mt = this->type_->map_type(); | |
12471 | int i = 0; | |
12472 | Type* key_type = mt->key_type(); | |
12473 | Type* val_type = mt->val_type(); | |
12474 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12475 | pv != this->vals_->end(); | |
12476 | ++pv, ++i) | |
12477 | { | |
12478 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
12479 | { | |
12480 | error_at((*pv)->location(), | |
12481 | "incompatible type for element %d key in map construction", | |
12482 | i + 1); | |
12483 | this->set_is_error(); | |
12484 | } | |
12485 | ++pv; | |
12486 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
12487 | { | |
12488 | error_at((*pv)->location(), | |
12489 | ("incompatible type for element %d value " | |
12490 | "in map construction"), | |
12491 | i + 1); | |
12492 | this->set_is_error(); | |
12493 | } | |
12494 | } | |
12495 | } | |
12496 | ||
ea664253 | 12497 | // Return the backend representation for constructing a map. |
e440a328 | 12498 | |
ea664253 | 12499 | Bexpression* |
12500 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12501 | { |
2c809f8f | 12502 | if (this->is_error_expression()) |
ea664253 | 12503 | return context->backend()->error_expression(); |
2c809f8f | 12504 | Location loc = this->location(); |
e440a328 | 12505 | |
e440a328 | 12506 | size_t i = 0; |
2c809f8f | 12507 | Expression* ventries; |
e440a328 | 12508 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 12509 | ventries = Expression::make_nil(loc); |
e440a328 | 12510 | else |
12511 | { | |
2c809f8f | 12512 | go_assert(this->constructor_temp_ != NULL); |
12513 | i = this->vals_->size() / 2; | |
e440a328 | 12514 | |
2c809f8f | 12515 | Expression* ctor_ref = |
12516 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
12517 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
12518 | } | |
e440a328 | 12519 | |
2c809f8f | 12520 | Map_type* mt = this->type_->map_type(); |
12521 | if (this->element_type_ == NULL) | |
12522 | this->element_type_ = | |
12523 | Type::make_builtin_struct_type(2, | |
12524 | "__key", mt->key_type(), | |
12525 | "__val", mt->val_type()); | |
12526 | Expression* descriptor = Expression::make_map_descriptor(mt, loc); | |
12527 | ||
12528 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 12529 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 12530 | |
12531 | Expression* entry_size = | |
12532 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
12533 | ||
12534 | unsigned int field_index; | |
12535 | const Struct_field* valfield = | |
12536 | this->element_type_->find_local_field("__val", &field_index); | |
12537 | Expression* val_offset = | |
12538 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
12539 | Expression* val_size = | |
12540 | Expression::make_type_info(mt->val_type(), TYPE_INFO_SIZE); | |
12541 | ||
12542 | Expression* map_ctor = | |
12543 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 6, descriptor, count, | |
12544 | entry_size, val_offset, val_size, ventries); | |
ea664253 | 12545 | return map_ctor->get_backend(context); |
2c809f8f | 12546 | } |
e440a328 | 12547 | |
2c809f8f | 12548 | // Export an array construction. |
e440a328 | 12549 | |
2c809f8f | 12550 | void |
12551 | Map_construction_expression::do_export(Export* exp) const | |
12552 | { | |
12553 | exp->write_c_string("convert("); | |
12554 | exp->write_type(this->type_); | |
12555 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12556 | pv != this->vals_->end(); | |
12557 | ++pv) | |
12558 | { | |
12559 | exp->write_c_string(", "); | |
12560 | (*pv)->export_expression(exp); | |
12561 | } | |
12562 | exp->write_c_string(")"); | |
12563 | } | |
e440a328 | 12564 | |
2c809f8f | 12565 | // Dump ast representation for a map construction expression. |
d751bb78 | 12566 | |
12567 | void | |
12568 | Map_construction_expression::do_dump_expression( | |
12569 | Ast_dump_context* ast_dump_context) const | |
12570 | { | |
d751bb78 | 12571 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 12572 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 12573 | ast_dump_context->ostream() << "}"; |
12574 | } | |
12575 | ||
e440a328 | 12576 | // A general composite literal. This is lowered to a type specific |
12577 | // version. | |
12578 | ||
12579 | class Composite_literal_expression : public Parser_expression | |
12580 | { | |
12581 | public: | |
12582 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
62750cd5 | 12583 | Expression_list* vals, bool all_are_names, |
12584 | Location location) | |
e440a328 | 12585 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), |
62750cd5 | 12586 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys), |
12587 | all_are_names_(all_are_names) | |
e440a328 | 12588 | { } |
12589 | ||
12590 | protected: | |
12591 | int | |
12592 | do_traverse(Traverse* traverse); | |
12593 | ||
12594 | Expression* | |
ceeb4318 | 12595 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12596 | |
12597 | Expression* | |
12598 | do_copy() | |
12599 | { | |
12600 | return new Composite_literal_expression(this->type_, this->depth_, | |
12601 | this->has_keys_, | |
12602 | (this->vals_ == NULL | |
12603 | ? NULL | |
12604 | : this->vals_->copy()), | |
62750cd5 | 12605 | this->all_are_names_, |
e440a328 | 12606 | this->location()); |
12607 | } | |
12608 | ||
d751bb78 | 12609 | void |
12610 | do_dump_expression(Ast_dump_context*) const; | |
12611 | ||
e440a328 | 12612 | private: |
12613 | Expression* | |
81c4b26b | 12614 | lower_struct(Gogo*, Type*); |
e440a328 | 12615 | |
12616 | Expression* | |
113ef6a5 | 12617 | lower_array(Type*); |
e440a328 | 12618 | |
12619 | Expression* | |
ffe743ca | 12620 | make_array(Type*, const std::vector<unsigned long>*, Expression_list*); |
e440a328 | 12621 | |
12622 | Expression* | |
ceeb4318 | 12623 | lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); |
e440a328 | 12624 | |
12625 | // The type of the composite literal. | |
12626 | Type* type_; | |
12627 | // The depth within a list of composite literals within a composite | |
12628 | // literal, when the type is omitted. | |
12629 | int depth_; | |
12630 | // The values to put in the composite literal. | |
12631 | Expression_list* vals_; | |
12632 | // If this is true, then VALS_ is a list of pairs: a key and a | |
12633 | // value. In an array initializer, a missing key will be NULL. | |
12634 | bool has_keys_; | |
62750cd5 | 12635 | // If this is true, then HAS_KEYS_ is true, and every key is a |
12636 | // simple identifier. | |
12637 | bool all_are_names_; | |
e440a328 | 12638 | }; |
12639 | ||
12640 | // Traversal. | |
12641 | ||
12642 | int | |
12643 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
12644 | { | |
dbffccfc | 12645 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 12646 | return TRAVERSE_EXIT; |
dbffccfc | 12647 | |
12648 | // If this is a struct composite literal with keys, then the keys | |
12649 | // are field names, not expressions. We don't want to traverse them | |
12650 | // in that case. If we do, we can give an erroneous error "variable | |
12651 | // initializer refers to itself." See bug482.go in the testsuite. | |
12652 | if (this->has_keys_ && this->vals_ != NULL) | |
12653 | { | |
12654 | // The type may not be resolvable at this point. | |
12655 | Type* type = this->type_; | |
a01f2481 | 12656 | |
12657 | for (int depth = this->depth_; depth > 0; --depth) | |
12658 | { | |
12659 | if (type->array_type() != NULL) | |
12660 | type = type->array_type()->element_type(); | |
12661 | else if (type->map_type() != NULL) | |
12662 | type = type->map_type()->val_type(); | |
12663 | else | |
12664 | { | |
12665 | // This error will be reported during lowering. | |
12666 | return TRAVERSE_CONTINUE; | |
12667 | } | |
12668 | } | |
12669 | ||
dbffccfc | 12670 | while (true) |
12671 | { | |
12672 | if (type->classification() == Type::TYPE_NAMED) | |
12673 | type = type->named_type()->real_type(); | |
12674 | else if (type->classification() == Type::TYPE_FORWARD) | |
12675 | { | |
12676 | Type* t = type->forwarded(); | |
12677 | if (t == type) | |
12678 | break; | |
12679 | type = t; | |
12680 | } | |
12681 | else | |
12682 | break; | |
12683 | } | |
12684 | ||
12685 | if (type->classification() == Type::TYPE_STRUCT) | |
12686 | { | |
12687 | Expression_list::iterator p = this->vals_->begin(); | |
12688 | while (p != this->vals_->end()) | |
12689 | { | |
12690 | // Skip key. | |
12691 | ++p; | |
12692 | go_assert(p != this->vals_->end()); | |
12693 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12694 | return TRAVERSE_EXIT; | |
12695 | ++p; | |
12696 | } | |
12697 | return TRAVERSE_CONTINUE; | |
12698 | } | |
12699 | } | |
12700 | ||
12701 | if (this->vals_ != NULL) | |
12702 | return this->vals_->traverse(traverse); | |
12703 | ||
12704 | return TRAVERSE_CONTINUE; | |
e440a328 | 12705 | } |
12706 | ||
12707 | // Lower a generic composite literal into a specific version based on | |
12708 | // the type. | |
12709 | ||
12710 | Expression* | |
ceeb4318 | 12711 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
12712 | Statement_inserter* inserter, int) | |
e440a328 | 12713 | { |
12714 | Type* type = this->type_; | |
12715 | ||
12716 | for (int depth = this->depth_; depth > 0; --depth) | |
12717 | { | |
12718 | if (type->array_type() != NULL) | |
12719 | type = type->array_type()->element_type(); | |
12720 | else if (type->map_type() != NULL) | |
12721 | type = type->map_type()->val_type(); | |
12722 | else | |
12723 | { | |
5c13bd80 | 12724 | if (!type->is_error()) |
e440a328 | 12725 | error_at(this->location(), |
12726 | ("may only omit types within composite literals " | |
12727 | "of slice, array, or map type")); | |
12728 | return Expression::make_error(this->location()); | |
12729 | } | |
12730 | } | |
12731 | ||
e00772b3 | 12732 | Type *pt = type->points_to(); |
12733 | bool is_pointer = false; | |
12734 | if (pt != NULL) | |
12735 | { | |
12736 | is_pointer = true; | |
12737 | type = pt; | |
12738 | } | |
12739 | ||
12740 | Expression* ret; | |
5c13bd80 | 12741 | if (type->is_error()) |
e440a328 | 12742 | return Expression::make_error(this->location()); |
12743 | else if (type->struct_type() != NULL) | |
e00772b3 | 12744 | ret = this->lower_struct(gogo, type); |
e440a328 | 12745 | else if (type->array_type() != NULL) |
113ef6a5 | 12746 | ret = this->lower_array(type); |
e440a328 | 12747 | else if (type->map_type() != NULL) |
e00772b3 | 12748 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 12749 | else |
12750 | { | |
12751 | error_at(this->location(), | |
12752 | ("expected struct, slice, array, or map type " | |
12753 | "for composite literal")); | |
12754 | return Expression::make_error(this->location()); | |
12755 | } | |
e00772b3 | 12756 | |
12757 | if (is_pointer) | |
2c809f8f | 12758 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 12759 | |
12760 | return ret; | |
e440a328 | 12761 | } |
12762 | ||
12763 | // Lower a struct composite literal. | |
12764 | ||
12765 | Expression* | |
81c4b26b | 12766 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 12767 | { |
b13c66cd | 12768 | Location location = this->location(); |
e440a328 | 12769 | Struct_type* st = type->struct_type(); |
12770 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 12771 | { |
e6013c28 | 12772 | if (this->vals_ != NULL |
12773 | && !this->vals_->empty() | |
12774 | && type->named_type() != NULL | |
12775 | && type->named_type()->named_object()->package() != NULL) | |
12776 | { | |
12777 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
12778 | pf != st->fields()->end(); | |
12779 | ++pf) | |
07daa4e7 | 12780 | { |
07ba7f26 | 12781 | if (Gogo::is_hidden_name(pf->field_name()) |
12782 | || pf->is_embedded_builtin(gogo)) | |
07daa4e7 | 12783 | error_at(this->location(), |
e6013c28 | 12784 | "assignment of unexported field %qs in %qs literal", |
12785 | Gogo::message_name(pf->field_name()).c_str(), | |
12786 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12787 | } |
12788 | } | |
12789 | ||
12790 | return new Struct_construction_expression(type, this->vals_, location); | |
12791 | } | |
e440a328 | 12792 | |
12793 | size_t field_count = st->field_count(); | |
12794 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 12795 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 12796 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 12797 | Expression* external_expr = NULL; |
12798 | const Named_object* external_no = NULL; | |
e440a328 | 12799 | while (p != this->vals_->end()) |
12800 | { | |
12801 | Expression* name_expr = *p; | |
12802 | ||
12803 | ++p; | |
c484d925 | 12804 | go_assert(p != this->vals_->end()); |
e440a328 | 12805 | Expression* val = *p; |
12806 | ||
12807 | ++p; | |
12808 | ||
12809 | if (name_expr == NULL) | |
12810 | { | |
12811 | error_at(val->location(), "mixture of field and value initializers"); | |
12812 | return Expression::make_error(location); | |
12813 | } | |
12814 | ||
12815 | bool bad_key = false; | |
12816 | std::string name; | |
81c4b26b | 12817 | const Named_object* no = NULL; |
e440a328 | 12818 | switch (name_expr->classification()) |
12819 | { | |
12820 | case EXPRESSION_UNKNOWN_REFERENCE: | |
12821 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 12822 | if (type->named_type() != NULL) |
12823 | { | |
12824 | // If the named object found for this field name comes from a | |
12825 | // different package than the struct it is a part of, do not count | |
12826 | // this incorrect lookup as a usage of the object's package. | |
12827 | no = name_expr->unknown_expression()->named_object(); | |
12828 | if (no->package() != NULL | |
12829 | && no->package() != type->named_type()->named_object()->package()) | |
12830 | no->package()->forget_usage(name_expr); | |
12831 | } | |
e440a328 | 12832 | break; |
12833 | ||
12834 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 12835 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 12836 | break; |
12837 | ||
12838 | case EXPRESSION_TYPE: | |
12839 | { | |
12840 | Type* t = name_expr->type(); | |
12841 | Named_type* nt = t->named_type(); | |
12842 | if (nt == NULL) | |
12843 | bad_key = true; | |
12844 | else | |
81c4b26b | 12845 | no = nt->named_object(); |
e440a328 | 12846 | } |
12847 | break; | |
12848 | ||
12849 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 12850 | no = name_expr->var_expression()->named_object(); |
e440a328 | 12851 | break; |
12852 | ||
12853 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 12854 | no = name_expr->func_expression()->named_object(); |
e440a328 | 12855 | break; |
12856 | ||
12857 | case EXPRESSION_UNARY: | |
12858 | // If there is a local variable around with the same name as | |
12859 | // the field, and this occurs in the closure, then the | |
12860 | // parser may turn the field reference into an indirection | |
12861 | // through the closure. FIXME: This is a mess. | |
12862 | { | |
12863 | bad_key = true; | |
12864 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
12865 | if (ue->op() == OPERATOR_MULT) | |
12866 | { | |
12867 | Field_reference_expression* fre = | |
12868 | ue->operand()->field_reference_expression(); | |
12869 | if (fre != NULL) | |
12870 | { | |
12871 | Struct_type* st = | |
12872 | fre->expr()->type()->deref()->struct_type(); | |
12873 | if (st != NULL) | |
12874 | { | |
12875 | const Struct_field* sf = st->field(fre->field_index()); | |
12876 | name = sf->field_name(); | |
2d29d278 | 12877 | |
12878 | // See below. FIXME. | |
12879 | if (!Gogo::is_hidden_name(name) | |
12880 | && name[0] >= 'a' | |
12881 | && name[0] <= 'z') | |
12882 | { | |
12883 | if (gogo->lookup_global(name.c_str()) != NULL) | |
12884 | name = gogo->pack_hidden_name(name, false); | |
12885 | } | |
12886 | ||
e440a328 | 12887 | char buf[20]; |
12888 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
12889 | size_t buflen = strlen(buf); | |
12890 | if (name.compare(name.length() - buflen, buflen, buf) | |
12891 | == 0) | |
12892 | { | |
12893 | name = name.substr(0, name.length() - buflen); | |
12894 | bad_key = false; | |
12895 | } | |
12896 | } | |
12897 | } | |
12898 | } | |
12899 | } | |
12900 | break; | |
12901 | ||
12902 | default: | |
12903 | bad_key = true; | |
12904 | break; | |
12905 | } | |
12906 | if (bad_key) | |
12907 | { | |
12908 | error_at(name_expr->location(), "expected struct field name"); | |
12909 | return Expression::make_error(location); | |
12910 | } | |
12911 | ||
81c4b26b | 12912 | if (no != NULL) |
12913 | { | |
62750cd5 | 12914 | if (no->package() != NULL && external_expr == NULL) |
12915 | { | |
12916 | external_expr = name_expr; | |
12917 | external_no = no; | |
12918 | } | |
12919 | ||
81c4b26b | 12920 | name = no->name(); |
12921 | ||
12922 | // A predefined name won't be packed. If it starts with a | |
12923 | // lower case letter we need to check for that case, because | |
2d29d278 | 12924 | // the field name will be packed. FIXME. |
81c4b26b | 12925 | if (!Gogo::is_hidden_name(name) |
12926 | && name[0] >= 'a' | |
12927 | && name[0] <= 'z') | |
12928 | { | |
12929 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
12930 | if (gno == no) | |
12931 | name = gogo->pack_hidden_name(name, false); | |
12932 | } | |
12933 | } | |
12934 | ||
e440a328 | 12935 | unsigned int index; |
12936 | const Struct_field* sf = st->find_local_field(name, &index); | |
12937 | if (sf == NULL) | |
12938 | { | |
12939 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
12940 | Gogo::message_name(name).c_str(), | |
12941 | (type->named_type() != NULL | |
12942 | ? type->named_type()->message_name().c_str() | |
12943 | : "unnamed struct")); | |
12944 | return Expression::make_error(location); | |
12945 | } | |
12946 | if (vals[index] != NULL) | |
12947 | { | |
12948 | error_at(name_expr->location(), | |
12949 | "duplicate value for field %qs in %qs", | |
12950 | Gogo::message_name(name).c_str(), | |
12951 | (type->named_type() != NULL | |
12952 | ? type->named_type()->message_name().c_str() | |
12953 | : "unnamed struct")); | |
12954 | return Expression::make_error(location); | |
12955 | } | |
12956 | ||
07daa4e7 | 12957 | if (type->named_type() != NULL |
12958 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 12959 | && (Gogo::is_hidden_name(sf->field_name()) |
12960 | || sf->is_embedded_builtin(gogo))) | |
07daa4e7 | 12961 | error_at(name_expr->location(), |
12962 | "assignment of unexported field %qs in %qs literal", | |
12963 | Gogo::message_name(sf->field_name()).c_str(), | |
12964 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12965 | |
e440a328 | 12966 | vals[index] = val; |
0c4f5a19 | 12967 | traverse_order->push_back(index); |
e440a328 | 12968 | } |
12969 | ||
62750cd5 | 12970 | if (!this->all_are_names_) |
12971 | { | |
12972 | // This is a weird case like bug462 in the testsuite. | |
12973 | if (external_expr == NULL) | |
12974 | error_at(this->location(), "unknown field in %qs literal", | |
12975 | (type->named_type() != NULL | |
12976 | ? type->named_type()->message_name().c_str() | |
12977 | : "unnamed struct")); | |
12978 | else | |
12979 | error_at(external_expr->location(), "unknown field %qs in %qs", | |
12980 | external_no->message_name().c_str(), | |
12981 | (type->named_type() != NULL | |
12982 | ? type->named_type()->message_name().c_str() | |
12983 | : "unnamed struct")); | |
12984 | return Expression::make_error(location); | |
12985 | } | |
12986 | ||
e440a328 | 12987 | Expression_list* list = new Expression_list; |
12988 | list->reserve(field_count); | |
12989 | for (size_t i = 0; i < field_count; ++i) | |
12990 | list->push_back(vals[i]); | |
12991 | ||
0c4f5a19 | 12992 | Struct_construction_expression* ret = |
12993 | new Struct_construction_expression(type, list, location); | |
12994 | ret->set_traverse_order(traverse_order); | |
12995 | return ret; | |
e440a328 | 12996 | } |
12997 | ||
00773463 | 12998 | // Used to sort an index/value array. |
12999 | ||
13000 | class Index_value_compare | |
13001 | { | |
13002 | public: | |
13003 | bool | |
13004 | operator()(const std::pair<unsigned long, Expression*>& a, | |
13005 | const std::pair<unsigned long, Expression*>& b) | |
13006 | { return a.first < b.first; } | |
13007 | }; | |
13008 | ||
e440a328 | 13009 | // Lower an array composite literal. |
13010 | ||
13011 | Expression* | |
113ef6a5 | 13012 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13013 | { |
b13c66cd | 13014 | Location location = this->location(); |
e440a328 | 13015 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13016 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13017 | |
ffe743ca | 13018 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13019 | indexes->reserve(this->vals_->size()); | |
00773463 | 13020 | bool indexes_out_of_order = false; |
ffe743ca | 13021 | Expression_list* vals = new Expression_list(); |
13022 | vals->reserve(this->vals_->size()); | |
e440a328 | 13023 | unsigned long index = 0; |
13024 | Expression_list::const_iterator p = this->vals_->begin(); | |
13025 | while (p != this->vals_->end()) | |
13026 | { | |
13027 | Expression* index_expr = *p; | |
13028 | ||
13029 | ++p; | |
c484d925 | 13030 | go_assert(p != this->vals_->end()); |
e440a328 | 13031 | Expression* val = *p; |
13032 | ||
13033 | ++p; | |
13034 | ||
ffe743ca | 13035 | if (index_expr == NULL) |
13036 | { | |
13037 | if (!indexes->empty()) | |
13038 | indexes->push_back(index); | |
13039 | } | |
13040 | else | |
e440a328 | 13041 | { |
ffe743ca | 13042 | if (indexes->empty() && !vals->empty()) |
13043 | { | |
13044 | for (size_t i = 0; i < vals->size(); ++i) | |
13045 | indexes->push_back(i); | |
13046 | } | |
13047 | ||
0c77715b | 13048 | Numeric_constant nc; |
13049 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13050 | { |
e440a328 | 13051 | error_at(index_expr->location(), |
13052 | "index expression is not integer constant"); | |
13053 | return Expression::make_error(location); | |
13054 | } | |
6f6d9955 | 13055 | |
0c77715b | 13056 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13057 | { |
0c77715b | 13058 | case Numeric_constant::NC_UL_VALID: |
13059 | break; | |
13060 | case Numeric_constant::NC_UL_NOTINT: | |
13061 | error_at(index_expr->location(), | |
13062 | "index expression is not integer constant"); | |
13063 | return Expression::make_error(location); | |
13064 | case Numeric_constant::NC_UL_NEGATIVE: | |
e440a328 | 13065 | error_at(index_expr->location(), "index expression is negative"); |
13066 | return Expression::make_error(location); | |
0c77715b | 13067 | case Numeric_constant::NC_UL_BIG: |
e440a328 | 13068 | error_at(index_expr->location(), "index value overflow"); |
13069 | return Expression::make_error(location); | |
0c77715b | 13070 | default: |
13071 | go_unreachable(); | |
e440a328 | 13072 | } |
6f6d9955 | 13073 | |
13074 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13075 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13076 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13077 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13078 | { |
6f6d9955 | 13079 | error_at(index_expr->location(), "index value overflow"); |
13080 | return Expression::make_error(location); | |
13081 | } | |
13082 | ||
ffe743ca | 13083 | if (std::find(indexes->begin(), indexes->end(), index) |
13084 | != indexes->end()) | |
e440a328 | 13085 | { |
ffe743ca | 13086 | error_at(index_expr->location(), "duplicate value for index %lu", |
e440a328 | 13087 | index); |
13088 | return Expression::make_error(location); | |
13089 | } | |
ffe743ca | 13090 | |
00773463 | 13091 | if (!indexes->empty() && index < indexes->back()) |
13092 | indexes_out_of_order = true; | |
13093 | ||
ffe743ca | 13094 | indexes->push_back(index); |
e440a328 | 13095 | } |
13096 | ||
ffe743ca | 13097 | vals->push_back(val); |
13098 | ||
e440a328 | 13099 | ++index; |
13100 | } | |
13101 | ||
ffe743ca | 13102 | if (indexes->empty()) |
13103 | { | |
13104 | delete indexes; | |
13105 | indexes = NULL; | |
13106 | } | |
e440a328 | 13107 | |
00773463 | 13108 | if (indexes_out_of_order) |
13109 | { | |
13110 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
13111 | ||
13112 | V v; | |
13113 | v.reserve(indexes->size()); | |
13114 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
13115 | for (Expression_list::const_iterator pe = vals->begin(); | |
13116 | pe != vals->end(); | |
13117 | ++pe, ++pi) | |
13118 | v.push_back(std::make_pair(*pi, *pe)); | |
13119 | ||
13120 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
13121 | ||
13122 | delete indexes; | |
13123 | delete vals; | |
13124 | indexes = new std::vector<unsigned long>(); | |
13125 | indexes->reserve(v.size()); | |
13126 | vals = new Expression_list(); | |
13127 | vals->reserve(v.size()); | |
13128 | ||
13129 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13130 | { | |
13131 | indexes->push_back(p->first); | |
13132 | vals->push_back(p->second); | |
13133 | } | |
13134 | } | |
13135 | ||
ffe743ca | 13136 | return this->make_array(type, indexes, vals); |
e440a328 | 13137 | } |
13138 | ||
13139 | // Actually build the array composite literal. This handles | |
13140 | // [...]{...}. | |
13141 | ||
13142 | Expression* | |
ffe743ca | 13143 | Composite_literal_expression::make_array( |
13144 | Type* type, | |
13145 | const std::vector<unsigned long>* indexes, | |
13146 | Expression_list* vals) | |
e440a328 | 13147 | { |
b13c66cd | 13148 | Location location = this->location(); |
e440a328 | 13149 | Array_type* at = type->array_type(); |
ffe743ca | 13150 | |
e440a328 | 13151 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13152 | { | |
ffe743ca | 13153 | size_t size; |
13154 | if (vals == NULL) | |
13155 | size = 0; | |
00773463 | 13156 | else if (indexes != NULL) |
13157 | size = indexes->back() + 1; | |
13158 | else | |
ffe743ca | 13159 | { |
13160 | size = vals->size(); | |
13161 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13162 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13163 | && size >> (it->bits() - 1) != 0) | |
13164 | { | |
13165 | error_at(location, "too many elements in composite literal"); | |
13166 | return Expression::make_error(location); | |
13167 | } | |
13168 | } | |
ffe743ca | 13169 | |
e67508fa | 13170 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 13171 | at = Type::make_array_type(at->element_type(), elen); |
13172 | type = at; | |
13173 | } | |
ffe743ca | 13174 | else if (at->length() != NULL |
13175 | && !at->length()->is_error_expression() | |
13176 | && this->vals_ != NULL) | |
13177 | { | |
13178 | Numeric_constant nc; | |
13179 | unsigned long val; | |
13180 | if (at->length()->numeric_constant_value(&nc) | |
13181 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13182 | { | |
13183 | if (indexes == NULL) | |
13184 | { | |
13185 | if (this->vals_->size() > val) | |
13186 | { | |
13187 | error_at(location, "too many elements in composite literal"); | |
13188 | return Expression::make_error(location); | |
13189 | } | |
13190 | } | |
13191 | else | |
13192 | { | |
00773463 | 13193 | unsigned long max = indexes->back(); |
ffe743ca | 13194 | if (max >= val) |
13195 | { | |
13196 | error_at(location, | |
13197 | ("some element keys in composite literal " | |
13198 | "are out of range")); | |
13199 | return Expression::make_error(location); | |
13200 | } | |
13201 | } | |
13202 | } | |
13203 | } | |
13204 | ||
e440a328 | 13205 | if (at->length() != NULL) |
ffe743ca | 13206 | return new Fixed_array_construction_expression(type, indexes, vals, |
13207 | location); | |
e440a328 | 13208 | else |
2c809f8f | 13209 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13210 | } |
13211 | ||
13212 | // Lower a map composite literal. | |
13213 | ||
13214 | Expression* | |
a287720d | 13215 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13216 | Statement_inserter* inserter, |
a287720d | 13217 | Type* type) |
e440a328 | 13218 | { |
b13c66cd | 13219 | Location location = this->location(); |
e440a328 | 13220 | if (this->vals_ != NULL) |
13221 | { | |
13222 | if (!this->has_keys_) | |
13223 | { | |
13224 | error_at(location, "map composite literal must have keys"); | |
13225 | return Expression::make_error(location); | |
13226 | } | |
13227 | ||
a287720d | 13228 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13229 | p != this->vals_->end(); |
13230 | p += 2) | |
13231 | { | |
13232 | if (*p == NULL) | |
13233 | { | |
13234 | ++p; | |
13235 | error_at((*p)->location(), | |
13236 | "map composite literal must have keys for every value"); | |
13237 | return Expression::make_error(location); | |
13238 | } | |
a287720d | 13239 | // Make sure we have lowered the key; it may not have been |
13240 | // lowered in order to handle keys for struct composite | |
13241 | // literals. Lower it now to get the right error message. | |
13242 | if ((*p)->unknown_expression() != NULL) | |
13243 | { | |
13244 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13245 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 13246 | go_assert((*p)->is_error_expression()); |
a287720d | 13247 | return Expression::make_error(location); |
13248 | } | |
e440a328 | 13249 | } |
13250 | } | |
13251 | ||
13252 | return new Map_construction_expression(type, this->vals_, location); | |
13253 | } | |
13254 | ||
d751bb78 | 13255 | // Dump ast representation for a composite literal expression. |
13256 | ||
13257 | void | |
13258 | Composite_literal_expression::do_dump_expression( | |
13259 | Ast_dump_context* ast_dump_context) const | |
13260 | { | |
8b1c301d | 13261 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13262 | ast_dump_context->dump_type(this->type_); |
13263 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13264 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13265 | ast_dump_context->ostream() << "})"; |
13266 | } | |
13267 | ||
e440a328 | 13268 | // Make a composite literal expression. |
13269 | ||
13270 | Expression* | |
13271 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 13272 | Expression_list* vals, bool all_are_names, |
b13c66cd | 13273 | Location location) |
e440a328 | 13274 | { |
13275 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 13276 | all_are_names, location); |
e440a328 | 13277 | } |
13278 | ||
13279 | // Return whether this expression is a composite literal. | |
13280 | ||
13281 | bool | |
13282 | Expression::is_composite_literal() const | |
13283 | { | |
13284 | switch (this->classification_) | |
13285 | { | |
13286 | case EXPRESSION_COMPOSITE_LITERAL: | |
13287 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13288 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 13289 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13290 | case EXPRESSION_MAP_CONSTRUCTION: |
13291 | return true; | |
13292 | default: | |
13293 | return false; | |
13294 | } | |
13295 | } | |
13296 | ||
13297 | // Return whether this expression is a composite literal which is not | |
13298 | // constant. | |
13299 | ||
13300 | bool | |
13301 | Expression::is_nonconstant_composite_literal() const | |
13302 | { | |
13303 | switch (this->classification_) | |
13304 | { | |
13305 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13306 | { | |
13307 | const Struct_construction_expression *psce = | |
13308 | static_cast<const Struct_construction_expression*>(this); | |
13309 | return !psce->is_constant_struct(); | |
13310 | } | |
13311 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13312 | { | |
13313 | const Fixed_array_construction_expression *pace = | |
13314 | static_cast<const Fixed_array_construction_expression*>(this); | |
13315 | return !pace->is_constant_array(); | |
13316 | } | |
2c809f8f | 13317 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13318 | { |
2c809f8f | 13319 | const Slice_construction_expression *pace = |
13320 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 13321 | return !pace->is_constant_array(); |
13322 | } | |
13323 | case EXPRESSION_MAP_CONSTRUCTION: | |
13324 | return true; | |
13325 | default: | |
13326 | return false; | |
13327 | } | |
13328 | } | |
13329 | ||
35a54f17 | 13330 | // Return true if this is a variable or temporary_variable. |
13331 | ||
13332 | bool | |
13333 | Expression::is_variable() const | |
13334 | { | |
13335 | switch (this->classification_) | |
13336 | { | |
13337 | case EXPRESSION_VAR_REFERENCE: | |
13338 | case EXPRESSION_TEMPORARY_REFERENCE: | |
13339 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
13340 | return true; | |
13341 | default: | |
13342 | return false; | |
13343 | } | |
13344 | } | |
13345 | ||
e440a328 | 13346 | // Return true if this is a reference to a local variable. |
13347 | ||
13348 | bool | |
13349 | Expression::is_local_variable() const | |
13350 | { | |
13351 | const Var_expression* ve = this->var_expression(); | |
13352 | if (ve == NULL) | |
13353 | return false; | |
13354 | const Named_object* no = ve->named_object(); | |
13355 | return (no->is_result_variable() | |
13356 | || (no->is_variable() && !no->var_value()->is_global())); | |
13357 | } | |
13358 | ||
13359 | // Class Type_guard_expression. | |
13360 | ||
13361 | // Traversal. | |
13362 | ||
13363 | int | |
13364 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13365 | { | |
13366 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13367 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13368 | return TRAVERSE_EXIT; | |
13369 | return TRAVERSE_CONTINUE; | |
13370 | } | |
13371 | ||
2c809f8f | 13372 | Expression* |
13373 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
13374 | Statement_inserter* inserter) | |
13375 | { | |
5bf8be8b | 13376 | if (this->expr_->is_error_expression() |
13377 | || this->expr_->type()->is_error_type()) | |
13378 | { | |
13379 | go_assert(saw_errors()); | |
13380 | return Expression::make_error(this->location()); | |
13381 | } | |
13382 | ||
2c809f8f | 13383 | if (!this->expr_->is_variable()) |
13384 | { | |
13385 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
13386 | this->location()); | |
13387 | inserter->insert(temp); | |
13388 | this->expr_ = | |
13389 | Expression::make_temporary_reference(temp, this->location()); | |
13390 | } | |
13391 | return this; | |
13392 | } | |
13393 | ||
e440a328 | 13394 | // Check types of a type guard expression. The expression must have |
13395 | // an interface type, but the actual type conversion is checked at run | |
13396 | // time. | |
13397 | ||
13398 | void | |
13399 | Type_guard_expression::do_check_types(Gogo*) | |
13400 | { | |
e440a328 | 13401 | Type* expr_type = this->expr_->type(); |
7e9da23f | 13402 | if (expr_type->interface_type() == NULL) |
f725ade8 | 13403 | { |
5c13bd80 | 13404 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 13405 | this->report_error(_("type assertion only valid for interface types")); |
13406 | this->set_is_error(); | |
13407 | } | |
e440a328 | 13408 | else if (this->type_->interface_type() == NULL) |
13409 | { | |
13410 | std::string reason; | |
13411 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
13412 | &reason)) | |
13413 | { | |
5c13bd80 | 13414 | if (!this->type_->is_error()) |
e440a328 | 13415 | { |
f725ade8 | 13416 | if (reason.empty()) |
13417 | this->report_error(_("impossible type assertion: " | |
13418 | "type does not implement interface")); | |
13419 | else | |
13420 | error_at(this->location(), | |
13421 | ("impossible type assertion: " | |
13422 | "type does not implement interface (%s)"), | |
13423 | reason.c_str()); | |
e440a328 | 13424 | } |
f725ade8 | 13425 | this->set_is_error(); |
e440a328 | 13426 | } |
13427 | } | |
13428 | } | |
13429 | ||
ea664253 | 13430 | // Return the backend representation for a type guard expression. |
e440a328 | 13431 | |
ea664253 | 13432 | Bexpression* |
13433 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13434 | { |
2c809f8f | 13435 | Expression* conversion; |
7e9da23f | 13436 | if (this->type_->interface_type() != NULL) |
2c809f8f | 13437 | conversion = |
13438 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
13439 | true, this->location()); | |
e440a328 | 13440 | else |
2c809f8f | 13441 | conversion = |
13442 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
13443 | this->expr_, this->location()); | |
13444 | ||
ea664253 | 13445 | return conversion->get_backend(context); |
e440a328 | 13446 | } |
13447 | ||
d751bb78 | 13448 | // Dump ast representation for a type guard expression. |
13449 | ||
13450 | void | |
2c809f8f | 13451 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 13452 | const |
13453 | { | |
13454 | this->expr_->dump_expression(ast_dump_context); | |
13455 | ast_dump_context->ostream() << "."; | |
13456 | ast_dump_context->dump_type(this->type_); | |
13457 | } | |
13458 | ||
e440a328 | 13459 | // Make a type guard expression. |
13460 | ||
13461 | Expression* | |
13462 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 13463 | Location location) |
e440a328 | 13464 | { |
13465 | return new Type_guard_expression(expr, type, location); | |
13466 | } | |
13467 | ||
2c809f8f | 13468 | // Class Heap_expression. |
e440a328 | 13469 | |
da244e59 | 13470 | // Return the type of the expression stored on the heap. |
e440a328 | 13471 | |
da244e59 | 13472 | Type* |
13473 | Heap_expression::do_type() | |
13474 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 13475 | |
ea664253 | 13476 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 13477 | |
ea664253 | 13478 | Bexpression* |
13479 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13480 | { |
02c19a1a | 13481 | if (this->expr_->is_error_expression() || this->expr_->type()->is_error()) |
ea664253 | 13482 | return context->backend()->error_expression(); |
2c809f8f | 13483 | |
02c19a1a | 13484 | Location loc = this->location(); |
2c809f8f | 13485 | Gogo* gogo = context->gogo(); |
02c19a1a | 13486 | Btype* btype = this->type()->get_backend(gogo); |
ea664253 | 13487 | Bexpression* space = Expression::make_allocation(this->expr_->type(), |
13488 | loc)->get_backend(context); | |
02c19a1a | 13489 | |
13490 | Bstatement* decl; | |
13491 | Named_object* fn = context->function(); | |
13492 | go_assert(fn != NULL); | |
13493 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
13494 | Bvariable* space_temp = | |
13495 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
13496 | space, true, loc, &decl); | |
13497 | space = gogo->backend()->var_expression(space_temp, loc); | |
9b27b43c | 13498 | Btype* expr_btype = this->expr_->type()->get_backend(gogo); |
13499 | Bexpression* ref = | |
13500 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
02c19a1a | 13501 | |
ea664253 | 13502 | Bexpression* bexpr = this->expr_->get_backend(context); |
02c19a1a | 13503 | Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc); |
13504 | decl = gogo->backend()->compound_statement(decl, assn); | |
13505 | space = gogo->backend()->var_expression(space_temp, loc); | |
ea664253 | 13506 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 13507 | } |
13508 | ||
2c809f8f | 13509 | // Dump ast representation for a heap expression. |
d751bb78 | 13510 | |
13511 | void | |
2c809f8f | 13512 | Heap_expression::do_dump_expression( |
d751bb78 | 13513 | Ast_dump_context* ast_dump_context) const |
13514 | { | |
13515 | ast_dump_context->ostream() << "&("; | |
13516 | ast_dump_context->dump_expression(this->expr_); | |
13517 | ast_dump_context->ostream() << ")"; | |
13518 | } | |
13519 | ||
2c809f8f | 13520 | // Allocate an expression on the heap. |
e440a328 | 13521 | |
13522 | Expression* | |
2c809f8f | 13523 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 13524 | { |
2c809f8f | 13525 | return new Heap_expression(expr, location); |
e440a328 | 13526 | } |
13527 | ||
13528 | // Class Receive_expression. | |
13529 | ||
13530 | // Return the type of a receive expression. | |
13531 | ||
13532 | Type* | |
13533 | Receive_expression::do_type() | |
13534 | { | |
e429e3bd | 13535 | if (this->is_error_expression()) |
13536 | return Type::make_error_type(); | |
e440a328 | 13537 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
13538 | if (channel_type == NULL) | |
e429e3bd | 13539 | { |
13540 | this->report_error(_("expected channel")); | |
13541 | return Type::make_error_type(); | |
13542 | } | |
e440a328 | 13543 | return channel_type->element_type(); |
13544 | } | |
13545 | ||
13546 | // Check types for a receive expression. | |
13547 | ||
13548 | void | |
13549 | Receive_expression::do_check_types(Gogo*) | |
13550 | { | |
13551 | Type* type = this->channel_->type(); | |
5c13bd80 | 13552 | if (type->is_error()) |
e440a328 | 13553 | { |
e429e3bd | 13554 | go_assert(saw_errors()); |
e440a328 | 13555 | this->set_is_error(); |
13556 | return; | |
13557 | } | |
13558 | if (type->channel_type() == NULL) | |
13559 | { | |
13560 | this->report_error(_("expected channel")); | |
13561 | return; | |
13562 | } | |
13563 | if (!type->channel_type()->may_receive()) | |
13564 | { | |
13565 | this->report_error(_("invalid receive on send-only channel")); | |
13566 | return; | |
13567 | } | |
13568 | } | |
13569 | ||
2c809f8f | 13570 | // Flattening for receive expressions creates a temporary variable to store |
13571 | // received data in for receives. | |
13572 | ||
13573 | Expression* | |
13574 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
13575 | Statement_inserter* inserter) | |
13576 | { | |
13577 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13578 | if (channel_type == NULL) | |
13579 | { | |
13580 | go_assert(saw_errors()); | |
13581 | return this; | |
13582 | } | |
5bf8be8b | 13583 | else if (this->channel_->is_error_expression()) |
13584 | { | |
13585 | go_assert(saw_errors()); | |
13586 | return Expression::make_error(this->location()); | |
13587 | } | |
2c809f8f | 13588 | |
13589 | Type* element_type = channel_type->element_type(); | |
13590 | if (this->temp_receiver_ == NULL) | |
13591 | { | |
13592 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
13593 | this->location()); | |
13594 | this->temp_receiver_->set_is_address_taken(); | |
13595 | inserter->insert(this->temp_receiver_); | |
13596 | } | |
13597 | ||
13598 | return this; | |
13599 | } | |
13600 | ||
ea664253 | 13601 | // Get the backend representation for a receive expression. |
e440a328 | 13602 | |
ea664253 | 13603 | Bexpression* |
13604 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13605 | { |
f24f10bb | 13606 | Location loc = this->location(); |
13607 | ||
e440a328 | 13608 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 13609 | if (channel_type == NULL) |
13610 | { | |
c484d925 | 13611 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 13612 | return context->backend()->error_expression(); |
5b8368f4 | 13613 | } |
f24f10bb | 13614 | Expression* td = Expression::make_type_descriptor(channel_type, loc); |
e440a328 | 13615 | |
2c809f8f | 13616 | Expression* recv_ref = |
13617 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13618 | Expression* recv_addr = | |
13619 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13620 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
13621 | Expression* recv = | |
13622 | Runtime::make_call(Runtime::RECEIVE, loc, 3, | |
13623 | td, this->channel_, recv_addr); | |
ea664253 | 13624 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 13625 | } |
13626 | ||
d751bb78 | 13627 | // Dump ast representation for a receive expression. |
13628 | ||
13629 | void | |
13630 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13631 | { | |
13632 | ast_dump_context->ostream() << " <- " ; | |
13633 | ast_dump_context->dump_expression(channel_); | |
13634 | } | |
13635 | ||
e440a328 | 13636 | // Make a receive expression. |
13637 | ||
13638 | Receive_expression* | |
b13c66cd | 13639 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 13640 | { |
13641 | return new Receive_expression(channel, location); | |
13642 | } | |
13643 | ||
e440a328 | 13644 | // An expression which evaluates to a pointer to the type descriptor |
13645 | // of a type. | |
13646 | ||
13647 | class Type_descriptor_expression : public Expression | |
13648 | { | |
13649 | public: | |
b13c66cd | 13650 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 13651 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
13652 | type_(type) | |
13653 | { } | |
13654 | ||
13655 | protected: | |
4b686186 | 13656 | int |
13657 | do_traverse(Traverse*); | |
13658 | ||
e440a328 | 13659 | Type* |
13660 | do_type() | |
13661 | { return Type::make_type_descriptor_ptr_type(); } | |
13662 | ||
f9ca30f9 | 13663 | bool |
13664 | do_is_immutable() const | |
13665 | { return true; } | |
13666 | ||
e440a328 | 13667 | void |
13668 | do_determine_type(const Type_context*) | |
13669 | { } | |
13670 | ||
13671 | Expression* | |
13672 | do_copy() | |
13673 | { return this; } | |
13674 | ||
ea664253 | 13675 | Bexpression* |
13676 | do_get_backend(Translate_context* context) | |
a1d23b41 | 13677 | { |
ea664253 | 13678 | return this->type_->type_descriptor_pointer(context->gogo(), |
13679 | this->location()); | |
a1d23b41 | 13680 | } |
e440a328 | 13681 | |
d751bb78 | 13682 | void |
13683 | do_dump_expression(Ast_dump_context*) const; | |
13684 | ||
e440a328 | 13685 | private: |
13686 | // The type for which this is the descriptor. | |
13687 | Type* type_; | |
13688 | }; | |
13689 | ||
4b686186 | 13690 | int |
13691 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
13692 | { | |
13693 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13694 | return TRAVERSE_EXIT; | |
13695 | return TRAVERSE_CONTINUE; | |
13696 | } | |
13697 | ||
d751bb78 | 13698 | // Dump ast representation for a type descriptor expression. |
13699 | ||
13700 | void | |
13701 | Type_descriptor_expression::do_dump_expression( | |
13702 | Ast_dump_context* ast_dump_context) const | |
13703 | { | |
13704 | ast_dump_context->dump_type(this->type_); | |
13705 | } | |
13706 | ||
e440a328 | 13707 | // Make a type descriptor expression. |
13708 | ||
13709 | Expression* | |
b13c66cd | 13710 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 13711 | { |
13712 | return new Type_descriptor_expression(type, location); | |
13713 | } | |
13714 | ||
aa5ae575 | 13715 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
13716 | // of a type. | |
13717 | ||
13718 | class GC_symbol_expression : public Expression | |
13719 | { | |
13720 | public: | |
13721 | GC_symbol_expression(Type* type) | |
13722 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
13723 | type_(type) | |
13724 | {} | |
13725 | ||
13726 | protected: | |
13727 | Type* | |
13728 | do_type() | |
23d77f91 | 13729 | { return Type::lookup_integer_type("uintptr"); } |
aa5ae575 | 13730 | |
13731 | bool | |
13732 | do_is_immutable() const | |
13733 | { return true; } | |
13734 | ||
13735 | void | |
13736 | do_determine_type(const Type_context*) | |
13737 | { } | |
13738 | ||
13739 | Expression* | |
13740 | do_copy() | |
13741 | { return this; } | |
13742 | ||
13743 | Bexpression* | |
13744 | do_get_backend(Translate_context* context) | |
13745 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
13746 | ||
13747 | void | |
13748 | do_dump_expression(Ast_dump_context*) const; | |
13749 | ||
13750 | private: | |
13751 | // The type which this gc symbol describes. | |
13752 | Type* type_; | |
13753 | }; | |
13754 | ||
13755 | // Dump ast representation for a gc symbol expression. | |
13756 | ||
13757 | void | |
13758 | GC_symbol_expression::do_dump_expression( | |
13759 | Ast_dump_context* ast_dump_context) const | |
13760 | { | |
13761 | ast_dump_context->ostream() << "gcdata("; | |
13762 | ast_dump_context->dump_type(this->type_); | |
13763 | ast_dump_context->ostream() << ")"; | |
13764 | } | |
13765 | ||
13766 | // Make a gc symbol expression. | |
13767 | ||
13768 | Expression* | |
13769 | Expression::make_gc_symbol(Type* type) | |
13770 | { | |
13771 | return new GC_symbol_expression(type); | |
13772 | } | |
13773 | ||
e440a328 | 13774 | // An expression which evaluates to some characteristic of a type. |
13775 | // This is only used to initialize fields of a type descriptor. Using | |
13776 | // a new expression class is slightly inefficient but gives us a good | |
13777 | // separation between the frontend and the middle-end with regard to | |
13778 | // how types are laid out. | |
13779 | ||
13780 | class Type_info_expression : public Expression | |
13781 | { | |
13782 | public: | |
13783 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 13784 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 13785 | type_(type), type_info_(type_info) |
13786 | { } | |
13787 | ||
13788 | protected: | |
0e168074 | 13789 | bool |
13790 | do_is_immutable() const | |
13791 | { return true; } | |
13792 | ||
e440a328 | 13793 | Type* |
13794 | do_type(); | |
13795 | ||
13796 | void | |
13797 | do_determine_type(const Type_context*) | |
13798 | { } | |
13799 | ||
13800 | Expression* | |
13801 | do_copy() | |
13802 | { return this; } | |
13803 | ||
ea664253 | 13804 | Bexpression* |
13805 | do_get_backend(Translate_context* context); | |
e440a328 | 13806 | |
d751bb78 | 13807 | void |
13808 | do_dump_expression(Ast_dump_context*) const; | |
13809 | ||
e440a328 | 13810 | private: |
13811 | // The type for which we are getting information. | |
13812 | Type* type_; | |
13813 | // What information we want. | |
13814 | Type_info type_info_; | |
13815 | }; | |
13816 | ||
13817 | // The type is chosen to match what the type descriptor struct | |
13818 | // expects. | |
13819 | ||
13820 | Type* | |
13821 | Type_info_expression::do_type() | |
13822 | { | |
13823 | switch (this->type_info_) | |
13824 | { | |
13825 | case TYPE_INFO_SIZE: | |
13826 | return Type::lookup_integer_type("uintptr"); | |
13827 | case TYPE_INFO_ALIGNMENT: | |
13828 | case TYPE_INFO_FIELD_ALIGNMENT: | |
13829 | return Type::lookup_integer_type("uint8"); | |
13830 | default: | |
c3e6f413 | 13831 | go_unreachable(); |
e440a328 | 13832 | } |
13833 | } | |
13834 | ||
ea664253 | 13835 | // Return the backend representation for type information. |
e440a328 | 13836 | |
ea664253 | 13837 | Bexpression* |
13838 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13839 | { |
927a01eb | 13840 | Gogo* gogo = context->gogo(); |
2a305b85 | 13841 | bool ok = true; |
3f378015 | 13842 | int64_t val; |
927a01eb | 13843 | switch (this->type_info_) |
e440a328 | 13844 | { |
927a01eb | 13845 | case TYPE_INFO_SIZE: |
2a305b85 | 13846 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 13847 | break; |
13848 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 13849 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 13850 | break; |
13851 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 13852 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 13853 | break; |
13854 | default: | |
13855 | go_unreachable(); | |
e440a328 | 13856 | } |
2a305b85 | 13857 | if (!ok) |
13858 | { | |
13859 | go_assert(saw_errors()); | |
13860 | return gogo->backend()->error_expression(); | |
13861 | } | |
3f378015 | 13862 | Expression* e = Expression::make_integer_int64(val, this->type(), |
13863 | this->location()); | |
13864 | return e->get_backend(context); | |
e440a328 | 13865 | } |
13866 | ||
d751bb78 | 13867 | // Dump ast representation for a type info expression. |
13868 | ||
13869 | void | |
13870 | Type_info_expression::do_dump_expression( | |
13871 | Ast_dump_context* ast_dump_context) const | |
13872 | { | |
13873 | ast_dump_context->ostream() << "typeinfo("; | |
13874 | ast_dump_context->dump_type(this->type_); | |
13875 | ast_dump_context->ostream() << ","; | |
13876 | ast_dump_context->ostream() << | |
13877 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
13878 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
13879 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
13880 | : "unknown"); | |
13881 | ast_dump_context->ostream() << ")"; | |
13882 | } | |
13883 | ||
e440a328 | 13884 | // Make a type info expression. |
13885 | ||
13886 | Expression* | |
13887 | Expression::make_type_info(Type* type, Type_info type_info) | |
13888 | { | |
13889 | return new Type_info_expression(type, type_info); | |
13890 | } | |
13891 | ||
35a54f17 | 13892 | // An expression that evaluates to some characteristic of a slice. |
13893 | // This is used when indexing, bound-checking, or nil checking a slice. | |
13894 | ||
13895 | class Slice_info_expression : public Expression | |
13896 | { | |
13897 | public: | |
13898 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
13899 | Location location) | |
13900 | : Expression(EXPRESSION_SLICE_INFO, location), | |
13901 | slice_(slice), slice_info_(slice_info) | |
13902 | { } | |
13903 | ||
13904 | protected: | |
13905 | Type* | |
13906 | do_type(); | |
13907 | ||
13908 | void | |
13909 | do_determine_type(const Type_context*) | |
13910 | { } | |
13911 | ||
13912 | Expression* | |
13913 | do_copy() | |
13914 | { | |
13915 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
13916 | this->location()); | |
13917 | } | |
13918 | ||
ea664253 | 13919 | Bexpression* |
13920 | do_get_backend(Translate_context* context); | |
35a54f17 | 13921 | |
13922 | void | |
13923 | do_dump_expression(Ast_dump_context*) const; | |
13924 | ||
13925 | void | |
13926 | do_issue_nil_check() | |
13927 | { this->slice_->issue_nil_check(); } | |
13928 | ||
13929 | private: | |
13930 | // The slice for which we are getting information. | |
13931 | Expression* slice_; | |
13932 | // What information we want. | |
13933 | Slice_info slice_info_; | |
13934 | }; | |
13935 | ||
13936 | // Return the type of the slice info. | |
13937 | ||
13938 | Type* | |
13939 | Slice_info_expression::do_type() | |
13940 | { | |
13941 | switch (this->slice_info_) | |
13942 | { | |
13943 | case SLICE_INFO_VALUE_POINTER: | |
13944 | return Type::make_pointer_type( | |
13945 | this->slice_->type()->array_type()->element_type()); | |
13946 | case SLICE_INFO_LENGTH: | |
13947 | case SLICE_INFO_CAPACITY: | |
13948 | return Type::lookup_integer_type("int"); | |
13949 | default: | |
13950 | go_unreachable(); | |
13951 | } | |
13952 | } | |
13953 | ||
ea664253 | 13954 | // Return the backend information for slice information. |
35a54f17 | 13955 | |
ea664253 | 13956 | Bexpression* |
13957 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 13958 | { |
13959 | Gogo* gogo = context->gogo(); | |
ea664253 | 13960 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 13961 | switch (this->slice_info_) |
13962 | { | |
13963 | case SLICE_INFO_VALUE_POINTER: | |
13964 | case SLICE_INFO_LENGTH: | |
13965 | case SLICE_INFO_CAPACITY: | |
ea664253 | 13966 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
13967 | this->location()); | |
35a54f17 | 13968 | break; |
13969 | default: | |
13970 | go_unreachable(); | |
13971 | } | |
35a54f17 | 13972 | } |
13973 | ||
13974 | // Dump ast representation for a type info expression. | |
13975 | ||
13976 | void | |
13977 | Slice_info_expression::do_dump_expression( | |
13978 | Ast_dump_context* ast_dump_context) const | |
13979 | { | |
13980 | ast_dump_context->ostream() << "sliceinfo("; | |
13981 | this->slice_->dump_expression(ast_dump_context); | |
13982 | ast_dump_context->ostream() << ","; | |
13983 | ast_dump_context->ostream() << | |
13984 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
13985 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
13986 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
13987 | : "unknown"); | |
13988 | ast_dump_context->ostream() << ")"; | |
13989 | } | |
13990 | ||
13991 | // Make a slice info expression. | |
13992 | ||
13993 | Expression* | |
13994 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
13995 | Location location) | |
13996 | { | |
13997 | return new Slice_info_expression(slice, slice_info, location); | |
13998 | } | |
13999 | ||
2c809f8f | 14000 | // An expression that represents a slice value: a struct with value pointer, |
14001 | // length, and capacity fields. | |
14002 | ||
14003 | class Slice_value_expression : public Expression | |
14004 | { | |
14005 | public: | |
14006 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14007 | Expression* cap, Location location) | |
14008 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14009 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14010 | { } | |
14011 | ||
14012 | protected: | |
14013 | int | |
14014 | do_traverse(Traverse*); | |
14015 | ||
14016 | Type* | |
14017 | do_type() | |
14018 | { return this->type_; } | |
14019 | ||
14020 | void | |
14021 | do_determine_type(const Type_context*) | |
14022 | { go_unreachable(); } | |
14023 | ||
14024 | Expression* | |
14025 | do_copy() | |
14026 | { | |
14027 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14028 | this->len_->copy(), this->cap_->copy(), | |
14029 | this->location()); | |
14030 | } | |
14031 | ||
ea664253 | 14032 | Bexpression* |
14033 | do_get_backend(Translate_context* context); | |
2c809f8f | 14034 | |
14035 | void | |
14036 | do_dump_expression(Ast_dump_context*) const; | |
14037 | ||
14038 | private: | |
14039 | // The type of the slice value. | |
14040 | Type* type_; | |
14041 | // The pointer to the values in the slice. | |
14042 | Expression* valptr_; | |
14043 | // The length of the slice. | |
14044 | Expression* len_; | |
14045 | // The capacity of the slice. | |
14046 | Expression* cap_; | |
14047 | }; | |
14048 | ||
14049 | int | |
14050 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14051 | { | |
55e8ba6a | 14052 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
14053 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 14054 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
14055 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14056 | return TRAVERSE_EXIT; | |
14057 | return TRAVERSE_CONTINUE; | |
14058 | } | |
14059 | ||
ea664253 | 14060 | Bexpression* |
14061 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14062 | { |
14063 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14064 | vals[0] = this->valptr_->get_backend(context); |
14065 | vals[1] = this->len_->get_backend(context); | |
14066 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14067 | |
14068 | Gogo* gogo = context->gogo(); | |
14069 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14070 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14071 | } |
14072 | ||
14073 | void | |
14074 | Slice_value_expression::do_dump_expression( | |
14075 | Ast_dump_context* ast_dump_context) const | |
14076 | { | |
14077 | ast_dump_context->ostream() << "slicevalue("; | |
14078 | ast_dump_context->ostream() << "values: "; | |
14079 | this->valptr_->dump_expression(ast_dump_context); | |
14080 | ast_dump_context->ostream() << ", length: "; | |
14081 | this->len_->dump_expression(ast_dump_context); | |
14082 | ast_dump_context->ostream() << ", capacity: "; | |
14083 | this->cap_->dump_expression(ast_dump_context); | |
14084 | ast_dump_context->ostream() << ")"; | |
14085 | } | |
14086 | ||
14087 | Expression* | |
14088 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14089 | Expression* cap, Location location) | |
14090 | { | |
14091 | go_assert(at->is_slice_type()); | |
14092 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14093 | } | |
2387f644 | 14094 | |
14095 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14096 | // This is used to access the method table or underlying object of an interface. | |
14097 | ||
14098 | class Interface_info_expression : public Expression | |
14099 | { | |
14100 | public: | |
14101 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 14102 | Location location) |
2387f644 | 14103 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
14104 | iface_(iface), iface_info_(iface_info) | |
14105 | { } | |
14106 | ||
14107 | protected: | |
14108 | Type* | |
14109 | do_type(); | |
14110 | ||
14111 | void | |
14112 | do_determine_type(const Type_context*) | |
14113 | { } | |
14114 | ||
14115 | Expression* | |
14116 | do_copy() | |
14117 | { | |
14118 | return new Interface_info_expression(this->iface_->copy(), | |
14119 | this->iface_info_, this->location()); | |
14120 | } | |
14121 | ||
ea664253 | 14122 | Bexpression* |
14123 | do_get_backend(Translate_context* context); | |
2387f644 | 14124 | |
14125 | void | |
14126 | do_dump_expression(Ast_dump_context*) const; | |
14127 | ||
14128 | void | |
14129 | do_issue_nil_check() | |
14130 | { this->iface_->issue_nil_check(); } | |
14131 | ||
14132 | private: | |
14133 | // The interface for which we are getting information. | |
14134 | Expression* iface_; | |
14135 | // What information we want. | |
14136 | Interface_info iface_info_; | |
14137 | }; | |
14138 | ||
14139 | // Return the type of the interface info. | |
14140 | ||
14141 | Type* | |
14142 | Interface_info_expression::do_type() | |
14143 | { | |
14144 | switch (this->iface_info_) | |
14145 | { | |
14146 | case INTERFACE_INFO_METHODS: | |
14147 | { | |
2c809f8f | 14148 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
14149 | if (this->iface_->type()->interface_type()->is_empty()) | |
14150 | return pdt; | |
14151 | ||
2387f644 | 14152 | Location loc = this->location(); |
14153 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 14154 | sfl->push_back( |
14155 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
14156 | ||
14157 | Interface_type* itype = this->iface_->type()->interface_type(); | |
14158 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); | |
14159 | p != itype->methods()->end(); | |
14160 | ++p) | |
14161 | { | |
14162 | Function_type* ft = p->type()->function_type(); | |
14163 | go_assert(ft->receiver() == NULL); | |
14164 | ||
14165 | const Typed_identifier_list* params = ft->parameters(); | |
14166 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
14167 | if (params != NULL) | |
14168 | mparams->reserve(params->size() + 1); | |
14169 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
14170 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
14171 | if (params != NULL) | |
14172 | { | |
14173 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
14174 | pp != params->end(); | |
14175 | ++pp) | |
14176 | mparams->push_back(*pp); | |
14177 | } | |
14178 | ||
14179 | Typed_identifier_list* mresults = (ft->results() == NULL | |
14180 | ? NULL | |
14181 | : ft->results()->copy()); | |
14182 | Backend_function_type* mft = | |
14183 | Type::make_backend_function_type(NULL, mparams, mresults, | |
14184 | ft->location()); | |
14185 | ||
14186 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
14187 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
14188 | } | |
14189 | ||
14190 | return Type::make_pointer_type(Type::make_struct_type(sfl, loc)); | |
14191 | } | |
14192 | case INTERFACE_INFO_OBJECT: | |
14193 | return Type::make_pointer_type(Type::make_void_type()); | |
14194 | default: | |
14195 | go_unreachable(); | |
14196 | } | |
14197 | } | |
14198 | ||
ea664253 | 14199 | // Return the backend representation for interface information. |
2387f644 | 14200 | |
ea664253 | 14201 | Bexpression* |
14202 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 14203 | { |
14204 | Gogo* gogo = context->gogo(); | |
ea664253 | 14205 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 14206 | switch (this->iface_info_) |
14207 | { | |
14208 | case INTERFACE_INFO_METHODS: | |
14209 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 14210 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
14211 | this->location()); | |
2387f644 | 14212 | break; |
14213 | default: | |
14214 | go_unreachable(); | |
14215 | } | |
2387f644 | 14216 | } |
14217 | ||
14218 | // Dump ast representation for an interface info expression. | |
14219 | ||
14220 | void | |
14221 | Interface_info_expression::do_dump_expression( | |
14222 | Ast_dump_context* ast_dump_context) const | |
14223 | { | |
2c809f8f | 14224 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 14225 | ast_dump_context->ostream() << "interfaceinfo("; |
14226 | this->iface_->dump_expression(ast_dump_context); | |
14227 | ast_dump_context->ostream() << ","; | |
14228 | ast_dump_context->ostream() << | |
2c809f8f | 14229 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
14230 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 14231 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
14232 | : "unknown"); | |
14233 | ast_dump_context->ostream() << ")"; | |
14234 | } | |
14235 | ||
14236 | // Make an interface info expression. | |
14237 | ||
14238 | Expression* | |
14239 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
14240 | Location location) | |
14241 | { | |
14242 | return new Interface_info_expression(iface, iface_info, location); | |
14243 | } | |
14244 | ||
2c809f8f | 14245 | // An expression that represents an interface value. The first field is either |
14246 | // a type descriptor for an empty interface or a pointer to the interface method | |
14247 | // table for a non-empty interface. The second field is always the object. | |
14248 | ||
14249 | class Interface_value_expression : public Expression | |
14250 | { | |
14251 | public: | |
14252 | Interface_value_expression(Type* type, Expression* first_field, | |
14253 | Expression* obj, Location location) | |
14254 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
14255 | type_(type), first_field_(first_field), obj_(obj) | |
14256 | { } | |
14257 | ||
14258 | protected: | |
14259 | int | |
14260 | do_traverse(Traverse*); | |
14261 | ||
14262 | Type* | |
14263 | do_type() | |
14264 | { return this->type_; } | |
14265 | ||
14266 | void | |
14267 | do_determine_type(const Type_context*) | |
14268 | { go_unreachable(); } | |
14269 | ||
14270 | Expression* | |
14271 | do_copy() | |
14272 | { | |
14273 | return new Interface_value_expression(this->type_, | |
14274 | this->first_field_->copy(), | |
14275 | this->obj_->copy(), this->location()); | |
14276 | } | |
14277 | ||
ea664253 | 14278 | Bexpression* |
14279 | do_get_backend(Translate_context* context); | |
2c809f8f | 14280 | |
14281 | void | |
14282 | do_dump_expression(Ast_dump_context*) const; | |
14283 | ||
14284 | private: | |
14285 | // The type of the interface value. | |
14286 | Type* type_; | |
14287 | // The first field of the interface (either a type descriptor or a pointer | |
14288 | // to the method table. | |
14289 | Expression* first_field_; | |
14290 | // The underlying object of the interface. | |
14291 | Expression* obj_; | |
14292 | }; | |
14293 | ||
14294 | int | |
14295 | Interface_value_expression::do_traverse(Traverse* traverse) | |
14296 | { | |
14297 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
14298 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
14299 | return TRAVERSE_EXIT; | |
14300 | return TRAVERSE_CONTINUE; | |
14301 | } | |
14302 | ||
ea664253 | 14303 | Bexpression* |
14304 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14305 | { |
14306 | std::vector<Bexpression*> vals(2); | |
ea664253 | 14307 | vals[0] = this->first_field_->get_backend(context); |
14308 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 14309 | |
14310 | Gogo* gogo = context->gogo(); | |
14311 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14312 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14313 | } |
14314 | ||
14315 | void | |
14316 | Interface_value_expression::do_dump_expression( | |
14317 | Ast_dump_context* ast_dump_context) const | |
14318 | { | |
14319 | ast_dump_context->ostream() << "interfacevalue("; | |
14320 | ast_dump_context->ostream() << | |
14321 | (this->type_->interface_type()->is_empty() | |
14322 | ? "type_descriptor: " | |
14323 | : "methods: "); | |
14324 | this->first_field_->dump_expression(ast_dump_context); | |
14325 | ast_dump_context->ostream() << ", object: "; | |
14326 | this->obj_->dump_expression(ast_dump_context); | |
14327 | ast_dump_context->ostream() << ")"; | |
14328 | } | |
14329 | ||
14330 | Expression* | |
14331 | Expression::make_interface_value(Type* type, Expression* first_value, | |
14332 | Expression* object, Location location) | |
14333 | { | |
14334 | return new Interface_value_expression(type, first_value, object, location); | |
14335 | } | |
14336 | ||
14337 | // An interface method table for a pair of types: an interface type and a type | |
14338 | // that implements that interface. | |
14339 | ||
14340 | class Interface_mtable_expression : public Expression | |
14341 | { | |
14342 | public: | |
14343 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
14344 | bool is_pointer, Location location) | |
14345 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
14346 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
14347 | method_table_type_(NULL), bvar_(NULL) | |
14348 | { } | |
14349 | ||
14350 | protected: | |
14351 | int | |
14352 | do_traverse(Traverse*); | |
14353 | ||
14354 | Type* | |
14355 | do_type(); | |
14356 | ||
14357 | bool | |
14358 | is_immutable() const | |
14359 | { return true; } | |
14360 | ||
14361 | void | |
14362 | do_determine_type(const Type_context*) | |
14363 | { go_unreachable(); } | |
14364 | ||
14365 | Expression* | |
14366 | do_copy() | |
14367 | { | |
14368 | return new Interface_mtable_expression(this->itype_, this->type_, | |
14369 | this->is_pointer_, this->location()); | |
14370 | } | |
14371 | ||
14372 | bool | |
14373 | do_is_addressable() const | |
14374 | { return true; } | |
14375 | ||
ea664253 | 14376 | Bexpression* |
14377 | do_get_backend(Translate_context* context); | |
2c809f8f | 14378 | |
14379 | void | |
14380 | do_dump_expression(Ast_dump_context*) const; | |
14381 | ||
14382 | private: | |
14383 | // The interface type for which the methods are defined. | |
14384 | Interface_type* itype_; | |
14385 | // The type to construct the interface method table for. | |
14386 | Type* type_; | |
14387 | // Whether this table contains the method set for the receiver type or the | |
14388 | // pointer receiver type. | |
14389 | bool is_pointer_; | |
14390 | // The type of the method table. | |
14391 | Type* method_table_type_; | |
14392 | // The backend variable that refers to the interface method table. | |
14393 | Bvariable* bvar_; | |
14394 | }; | |
14395 | ||
14396 | int | |
14397 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
14398 | { | |
14399 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
14400 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14401 | return TRAVERSE_EXIT; | |
14402 | return TRAVERSE_CONTINUE; | |
14403 | } | |
14404 | ||
14405 | Type* | |
14406 | Interface_mtable_expression::do_type() | |
14407 | { | |
14408 | if (this->method_table_type_ != NULL) | |
14409 | return this->method_table_type_; | |
14410 | ||
14411 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14412 | go_assert(!interface_methods->empty()); | |
14413 | ||
14414 | Struct_field_list* sfl = new Struct_field_list; | |
14415 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
14416 | this->location()); | |
14417 | sfl->push_back(Struct_field(tid)); | |
14418 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14419 | p != interface_methods->end(); | |
14420 | ++p) | |
14421 | sfl->push_back(Struct_field(*p)); | |
14422 | this->method_table_type_ = Type::make_struct_type(sfl, this->location()); | |
14423 | return this->method_table_type_; | |
14424 | } | |
14425 | ||
ea664253 | 14426 | Bexpression* |
14427 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14428 | { |
14429 | Gogo* gogo = context->gogo(); | |
2c809f8f | 14430 | Location loc = Linemap::predeclared_location(); |
14431 | if (this->bvar_ != NULL) | |
ea664253 | 14432 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 14433 | |
14434 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14435 | go_assert(!interface_methods->empty()); | |
14436 | ||
14437 | std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_") | |
14438 | + this->itype_->mangled_name(gogo) | |
14439 | + "__" | |
14440 | + this->type_->mangled_name(gogo)); | |
14441 | ||
14442 | // See whether this interface has any hidden methods. | |
14443 | bool has_hidden_methods = false; | |
14444 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14445 | p != interface_methods->end(); | |
14446 | ++p) | |
14447 | { | |
14448 | if (Gogo::is_hidden_name(p->name())) | |
14449 | { | |
14450 | has_hidden_methods = true; | |
14451 | break; | |
14452 | } | |
14453 | } | |
14454 | ||
14455 | // We already know that the named type is convertible to the | |
14456 | // interface. If the interface has hidden methods, and the named | |
14457 | // type is defined in a different package, then the interface | |
14458 | // conversion table will be defined by that other package. | |
14459 | if (has_hidden_methods | |
14460 | && this->type_->named_type() != NULL | |
14461 | && this->type_->named_type()->named_object()->package() != NULL) | |
14462 | { | |
14463 | Btype* btype = this->type()->get_backend(gogo); | |
14464 | this->bvar_ = | |
14465 | gogo->backend()->immutable_struct_reference(mangled_name, btype, loc); | |
ea664253 | 14466 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 14467 | } |
14468 | ||
14469 | // The first element is the type descriptor. | |
14470 | Type* td_type; | |
14471 | if (!this->is_pointer_) | |
14472 | td_type = this->type_; | |
14473 | else | |
14474 | td_type = Type::make_pointer_type(this->type_); | |
14475 | ||
14476 | // Build an interface method table for a type: a type descriptor followed by a | |
14477 | // list of function pointers, one for each interface method. This is used for | |
14478 | // interfaces. | |
14479 | Expression_list* svals = new Expression_list(); | |
14480 | svals->push_back(Expression::make_type_descriptor(td_type, loc)); | |
14481 | ||
14482 | Named_type* nt = this->type_->named_type(); | |
14483 | Struct_type* st = this->type_->struct_type(); | |
14484 | go_assert(nt != NULL || st != NULL); | |
14485 | ||
14486 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14487 | p != interface_methods->end(); | |
14488 | ++p) | |
14489 | { | |
14490 | bool is_ambiguous; | |
14491 | Method* m; | |
14492 | if (nt != NULL) | |
14493 | m = nt->method_function(p->name(), &is_ambiguous); | |
14494 | else | |
14495 | m = st->method_function(p->name(), &is_ambiguous); | |
14496 | go_assert(m != NULL); | |
14497 | Named_object* no = m->named_object(); | |
14498 | ||
14499 | go_assert(no->is_function() || no->is_function_declaration()); | |
14500 | svals->push_back(Expression::make_func_code_reference(no, loc)); | |
14501 | } | |
14502 | ||
14503 | Btype* btype = this->type()->get_backend(gogo); | |
14504 | Expression* mtable = Expression::make_struct_composite_literal(this->type(), | |
14505 | svals, loc); | |
ea664253 | 14506 | Bexpression* ctor = mtable->get_backend(context); |
2c809f8f | 14507 | |
14508 | bool is_public = has_hidden_methods && this->type_->named_type() != NULL; | |
14509 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, false, | |
14510 | !is_public, btype, loc); | |
14511 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
14512 | !is_public, btype, loc, ctor); | |
ea664253 | 14513 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 14514 | } |
14515 | ||
14516 | void | |
14517 | Interface_mtable_expression::do_dump_expression( | |
14518 | Ast_dump_context* ast_dump_context) const | |
14519 | { | |
14520 | ast_dump_context->ostream() << "__go_" | |
14521 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
14522 | ast_dump_context->dump_type(this->itype_); | |
14523 | ast_dump_context->ostream() << "__"; | |
14524 | ast_dump_context->dump_type(this->type_); | |
14525 | } | |
14526 | ||
14527 | Expression* | |
14528 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
14529 | bool is_pointer, Location location) | |
14530 | { | |
14531 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
14532 | } | |
14533 | ||
e440a328 | 14534 | // An expression which evaluates to the offset of a field within a |
14535 | // struct. This, like Type_info_expression, q.v., is only used to | |
14536 | // initialize fields of a type descriptor. | |
14537 | ||
14538 | class Struct_field_offset_expression : public Expression | |
14539 | { | |
14540 | public: | |
14541 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 14542 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
14543 | Linemap::predeclared_location()), | |
e440a328 | 14544 | type_(type), field_(field) |
14545 | { } | |
14546 | ||
14547 | protected: | |
f23d7786 | 14548 | bool |
14549 | do_is_immutable() const | |
14550 | { return true; } | |
14551 | ||
e440a328 | 14552 | Type* |
14553 | do_type() | |
14554 | { return Type::lookup_integer_type("uintptr"); } | |
14555 | ||
14556 | void | |
14557 | do_determine_type(const Type_context*) | |
14558 | { } | |
14559 | ||
14560 | Expression* | |
14561 | do_copy() | |
14562 | { return this; } | |
14563 | ||
ea664253 | 14564 | Bexpression* |
14565 | do_get_backend(Translate_context* context); | |
e440a328 | 14566 | |
d751bb78 | 14567 | void |
14568 | do_dump_expression(Ast_dump_context*) const; | |
14569 | ||
e440a328 | 14570 | private: |
14571 | // The type of the struct. | |
14572 | Struct_type* type_; | |
14573 | // The field. | |
14574 | const Struct_field* field_; | |
14575 | }; | |
14576 | ||
ea664253 | 14577 | // Return the backend representation for a struct field offset. |
e440a328 | 14578 | |
ea664253 | 14579 | Bexpression* |
14580 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14581 | { |
e440a328 | 14582 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 14583 | Struct_field_list::const_iterator p; |
2c8bda43 | 14584 | unsigned i = 0; |
e440a328 | 14585 | for (p = fields->begin(); |
14586 | p != fields->end(); | |
2c8bda43 | 14587 | ++p, ++i) |
14588 | if (&*p == this->field_) | |
14589 | break; | |
c484d925 | 14590 | go_assert(&*p == this->field_); |
e440a328 | 14591 | |
2c8bda43 | 14592 | Gogo* gogo = context->gogo(); |
14593 | Btype* btype = this->type_->get_backend(gogo); | |
14594 | ||
3f378015 | 14595 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 14596 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 14597 | Expression* ret = |
3f378015 | 14598 | Expression::make_integer_int64(offset, uptr_type, |
14599 | Linemap::predeclared_location()); | |
ea664253 | 14600 | return ret->get_backend(context); |
e440a328 | 14601 | } |
14602 | ||
d751bb78 | 14603 | // Dump ast representation for a struct field offset expression. |
14604 | ||
14605 | void | |
14606 | Struct_field_offset_expression::do_dump_expression( | |
14607 | Ast_dump_context* ast_dump_context) const | |
14608 | { | |
14609 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 14610 | ast_dump_context->dump_type(this->type_); |
14611 | ast_dump_context->ostream() << '.'; | |
14612 | ast_dump_context->ostream() << | |
14613 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 14614 | ast_dump_context->ostream() << ")"; |
14615 | } | |
14616 | ||
e440a328 | 14617 | // Make an expression for a struct field offset. |
14618 | ||
14619 | Expression* | |
14620 | Expression::make_struct_field_offset(Struct_type* type, | |
14621 | const Struct_field* field) | |
14622 | { | |
14623 | return new Struct_field_offset_expression(type, field); | |
14624 | } | |
14625 | ||
a9182619 | 14626 | // An expression which evaluates to a pointer to the map descriptor of |
14627 | // a map type. | |
14628 | ||
14629 | class Map_descriptor_expression : public Expression | |
14630 | { | |
14631 | public: | |
b13c66cd | 14632 | Map_descriptor_expression(Map_type* type, Location location) |
a9182619 | 14633 | : Expression(EXPRESSION_MAP_DESCRIPTOR, location), |
14634 | type_(type) | |
14635 | { } | |
14636 | ||
14637 | protected: | |
14638 | Type* | |
14639 | do_type() | |
14640 | { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } | |
14641 | ||
14642 | void | |
14643 | do_determine_type(const Type_context*) | |
14644 | { } | |
14645 | ||
14646 | Expression* | |
14647 | do_copy() | |
14648 | { return this; } | |
14649 | ||
ea664253 | 14650 | Bexpression* |
14651 | do_get_backend(Translate_context* context) | |
a9182619 | 14652 | { |
ea664253 | 14653 | return this->type_->map_descriptor_pointer(context->gogo(), |
14654 | this->location()); | |
a9182619 | 14655 | } |
14656 | ||
d751bb78 | 14657 | void |
14658 | do_dump_expression(Ast_dump_context*) const; | |
14659 | ||
a9182619 | 14660 | private: |
14661 | // The type for which this is the descriptor. | |
14662 | Map_type* type_; | |
14663 | }; | |
14664 | ||
d751bb78 | 14665 | // Dump ast representation for a map descriptor expression. |
14666 | ||
14667 | void | |
14668 | Map_descriptor_expression::do_dump_expression( | |
14669 | Ast_dump_context* ast_dump_context) const | |
14670 | { | |
14671 | ast_dump_context->ostream() << "map_descriptor("; | |
14672 | ast_dump_context->dump_type(this->type_); | |
14673 | ast_dump_context->ostream() << ")"; | |
14674 | } | |
14675 | ||
a9182619 | 14676 | // Make a map descriptor expression. |
14677 | ||
14678 | Expression* | |
b13c66cd | 14679 | Expression::make_map_descriptor(Map_type* type, Location location) |
a9182619 | 14680 | { |
14681 | return new Map_descriptor_expression(type, location); | |
14682 | } | |
14683 | ||
e440a328 | 14684 | // An expression which evaluates to the address of an unnamed label. |
14685 | ||
14686 | class Label_addr_expression : public Expression | |
14687 | { | |
14688 | public: | |
b13c66cd | 14689 | Label_addr_expression(Label* label, Location location) |
e440a328 | 14690 | : Expression(EXPRESSION_LABEL_ADDR, location), |
14691 | label_(label) | |
14692 | { } | |
14693 | ||
14694 | protected: | |
14695 | Type* | |
14696 | do_type() | |
14697 | { return Type::make_pointer_type(Type::make_void_type()); } | |
14698 | ||
14699 | void | |
14700 | do_determine_type(const Type_context*) | |
14701 | { } | |
14702 | ||
14703 | Expression* | |
14704 | do_copy() | |
14705 | { return new Label_addr_expression(this->label_, this->location()); } | |
14706 | ||
ea664253 | 14707 | Bexpression* |
14708 | do_get_backend(Translate_context* context) | |
14709 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 14710 | |
d751bb78 | 14711 | void |
14712 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14713 | { ast_dump_context->ostream() << this->label_->name(); } | |
14714 | ||
e440a328 | 14715 | private: |
14716 | // The label whose address we are taking. | |
14717 | Label* label_; | |
14718 | }; | |
14719 | ||
14720 | // Make an expression for the address of an unnamed label. | |
14721 | ||
14722 | Expression* | |
b13c66cd | 14723 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 14724 | { |
14725 | return new Label_addr_expression(label, location); | |
14726 | } | |
14727 | ||
da244e59 | 14728 | // Class Conditional_expression. |
283a177b | 14729 | |
2c809f8f | 14730 | // Traversal. |
14731 | ||
14732 | int | |
14733 | Conditional_expression::do_traverse(Traverse* traverse) | |
14734 | { | |
14735 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
14736 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
14737 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
14738 | return TRAVERSE_EXIT; | |
14739 | return TRAVERSE_CONTINUE; | |
14740 | } | |
14741 | ||
283a177b | 14742 | // Return the type of the conditional expression. |
14743 | ||
14744 | Type* | |
14745 | Conditional_expression::do_type() | |
14746 | { | |
14747 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 14748 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
14749 | NULL)) | |
283a177b | 14750 | result_type = this->then_->type(); |
14751 | else if (this->then_->is_nil_expression() | |
14752 | || this->else_->is_nil_expression()) | |
14753 | result_type = (!this->then_->is_nil_expression() | |
14754 | ? this->then_->type() | |
14755 | : this->else_->type()); | |
14756 | return result_type; | |
14757 | } | |
14758 | ||
2c809f8f | 14759 | // Determine type for a conditional expression. |
14760 | ||
14761 | void | |
14762 | Conditional_expression::do_determine_type(const Type_context* context) | |
14763 | { | |
14764 | this->cond_->determine_type_no_context(); | |
14765 | this->then_->determine_type(context); | |
14766 | this->else_->determine_type(context); | |
14767 | } | |
14768 | ||
283a177b | 14769 | // Get the backend representation of a conditional expression. |
14770 | ||
ea664253 | 14771 | Bexpression* |
14772 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 14773 | { |
14774 | Gogo* gogo = context->gogo(); | |
14775 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 14776 | Bexpression* cond = this->cond_->get_backend(context); |
14777 | Bexpression* then = this->then_->get_backend(context); | |
14778 | Bexpression* belse = this->else_->get_backend(context); | |
14779 | return gogo->backend()->conditional_expression(result_btype, cond, then, | |
14780 | belse, this->location()); | |
283a177b | 14781 | } |
14782 | ||
14783 | // Dump ast representation of a conditional expression. | |
14784 | ||
14785 | void | |
14786 | Conditional_expression::do_dump_expression( | |
14787 | Ast_dump_context* ast_dump_context) const | |
14788 | { | |
14789 | ast_dump_context->ostream() << "("; | |
14790 | ast_dump_context->dump_expression(this->cond_); | |
14791 | ast_dump_context->ostream() << " ? "; | |
14792 | ast_dump_context->dump_expression(this->then_); | |
14793 | ast_dump_context->ostream() << " : "; | |
14794 | ast_dump_context->dump_expression(this->else_); | |
14795 | ast_dump_context->ostream() << ") "; | |
14796 | } | |
14797 | ||
14798 | // Make a conditional expression. | |
14799 | ||
14800 | Expression* | |
14801 | Expression::make_conditional(Expression* cond, Expression* then, | |
14802 | Expression* else_expr, Location location) | |
14803 | { | |
14804 | return new Conditional_expression(cond, then, else_expr, location); | |
14805 | } | |
14806 | ||
da244e59 | 14807 | // Class Compound_expression. |
2c809f8f | 14808 | |
14809 | // Traversal. | |
14810 | ||
14811 | int | |
14812 | Compound_expression::do_traverse(Traverse* traverse) | |
14813 | { | |
14814 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
14815 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
14816 | return TRAVERSE_EXIT; | |
14817 | return TRAVERSE_CONTINUE; | |
14818 | } | |
14819 | ||
14820 | // Return the type of the compound expression. | |
14821 | ||
14822 | Type* | |
14823 | Compound_expression::do_type() | |
14824 | { | |
14825 | return this->expr_->type(); | |
14826 | } | |
14827 | ||
14828 | // Determine type for a compound expression. | |
14829 | ||
14830 | void | |
14831 | Compound_expression::do_determine_type(const Type_context* context) | |
14832 | { | |
14833 | this->init_->determine_type_no_context(); | |
14834 | this->expr_->determine_type(context); | |
14835 | } | |
14836 | ||
14837 | // Get the backend representation of a compound expression. | |
14838 | ||
ea664253 | 14839 | Bexpression* |
14840 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14841 | { |
14842 | Gogo* gogo = context->gogo(); | |
ea664253 | 14843 | Bexpression* binit = this->init_->get_backend(context); |
2c809f8f | 14844 | Bstatement* init_stmt = gogo->backend()->expression_statement(binit); |
ea664253 | 14845 | Bexpression* bexpr = this->expr_->get_backend(context); |
14846 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
14847 | this->location()); | |
2c809f8f | 14848 | } |
14849 | ||
14850 | // Dump ast representation of a conditional expression. | |
14851 | ||
14852 | void | |
14853 | Compound_expression::do_dump_expression( | |
14854 | Ast_dump_context* ast_dump_context) const | |
14855 | { | |
14856 | ast_dump_context->ostream() << "("; | |
14857 | ast_dump_context->dump_expression(this->init_); | |
14858 | ast_dump_context->ostream() << ","; | |
14859 | ast_dump_context->dump_expression(this->expr_); | |
14860 | ast_dump_context->ostream() << ") "; | |
14861 | } | |
14862 | ||
14863 | // Make a compound expression. | |
14864 | ||
14865 | Expression* | |
14866 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
14867 | { | |
14868 | return new Compound_expression(init, expr, location); | |
14869 | } | |
14870 | ||
e440a328 | 14871 | // Import an expression. This comes at the end in order to see the |
14872 | // various class definitions. | |
14873 | ||
14874 | Expression* | |
14875 | Expression::import_expression(Import* imp) | |
14876 | { | |
14877 | int c = imp->peek_char(); | |
14878 | if (imp->match_c_string("- ") | |
14879 | || imp->match_c_string("! ") | |
14880 | || imp->match_c_string("^ ")) | |
14881 | return Unary_expression::do_import(imp); | |
14882 | else if (c == '(') | |
14883 | return Binary_expression::do_import(imp); | |
14884 | else if (imp->match_c_string("true") | |
14885 | || imp->match_c_string("false")) | |
14886 | return Boolean_expression::do_import(imp); | |
14887 | else if (c == '"') | |
14888 | return String_expression::do_import(imp); | |
14889 | else if (c == '-' || (c >= '0' && c <= '9')) | |
14890 | { | |
14891 | // This handles integers, floats and complex constants. | |
14892 | return Integer_expression::do_import(imp); | |
14893 | } | |
14894 | else if (imp->match_c_string("nil")) | |
14895 | return Nil_expression::do_import(imp); | |
14896 | else if (imp->match_c_string("convert")) | |
14897 | return Type_conversion_expression::do_import(imp); | |
14898 | else | |
14899 | { | |
14900 | error_at(imp->location(), "import error: expected expression"); | |
14901 | return Expression::make_error(imp->location()); | |
14902 | } | |
14903 | } | |
14904 | ||
14905 | // Class Expression_list. | |
14906 | ||
14907 | // Traverse the list. | |
14908 | ||
14909 | int | |
14910 | Expression_list::traverse(Traverse* traverse) | |
14911 | { | |
14912 | for (Expression_list::iterator p = this->begin(); | |
14913 | p != this->end(); | |
14914 | ++p) | |
14915 | { | |
14916 | if (*p != NULL) | |
14917 | { | |
14918 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
14919 | return TRAVERSE_EXIT; | |
14920 | } | |
14921 | } | |
14922 | return TRAVERSE_CONTINUE; | |
14923 | } | |
14924 | ||
14925 | // Copy the list. | |
14926 | ||
14927 | Expression_list* | |
14928 | Expression_list::copy() | |
14929 | { | |
14930 | Expression_list* ret = new Expression_list(); | |
14931 | for (Expression_list::iterator p = this->begin(); | |
14932 | p != this->end(); | |
14933 | ++p) | |
14934 | { | |
14935 | if (*p == NULL) | |
14936 | ret->push_back(NULL); | |
14937 | else | |
14938 | ret->push_back((*p)->copy()); | |
14939 | } | |
14940 | return ret; | |
14941 | } | |
14942 | ||
14943 | // Return whether an expression list has an error expression. | |
14944 | ||
14945 | bool | |
14946 | Expression_list::contains_error() const | |
14947 | { | |
14948 | for (Expression_list::const_iterator p = this->begin(); | |
14949 | p != this->end(); | |
14950 | ++p) | |
14951 | if (*p != NULL && (*p)->is_error_expression()) | |
14952 | return true; | |
14953 | return false; | |
14954 | } | |
0c77715b | 14955 | |
14956 | // Class Numeric_constant. | |
14957 | ||
14958 | // Destructor. | |
14959 | ||
14960 | Numeric_constant::~Numeric_constant() | |
14961 | { | |
14962 | this->clear(); | |
14963 | } | |
14964 | ||
14965 | // Copy constructor. | |
14966 | ||
14967 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
14968 | : classification_(a.classification_), type_(a.type_) | |
14969 | { | |
14970 | switch (a.classification_) | |
14971 | { | |
14972 | case NC_INVALID: | |
14973 | break; | |
14974 | case NC_INT: | |
14975 | case NC_RUNE: | |
14976 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
14977 | break; | |
14978 | case NC_FLOAT: | |
14979 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
14980 | break; | |
14981 | case NC_COMPLEX: | |
fcbea5e4 | 14982 | mpc_init2(this->u_.complex_val, mpc_precision); |
14983 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 14984 | break; |
14985 | default: | |
14986 | go_unreachable(); | |
14987 | } | |
14988 | } | |
14989 | ||
14990 | // Assignment operator. | |
14991 | ||
14992 | Numeric_constant& | |
14993 | Numeric_constant::operator=(const Numeric_constant& a) | |
14994 | { | |
14995 | this->clear(); | |
14996 | this->classification_ = a.classification_; | |
14997 | this->type_ = a.type_; | |
14998 | switch (a.classification_) | |
14999 | { | |
15000 | case NC_INVALID: | |
15001 | break; | |
15002 | case NC_INT: | |
15003 | case NC_RUNE: | |
15004 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15005 | break; | |
15006 | case NC_FLOAT: | |
15007 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15008 | break; | |
15009 | case NC_COMPLEX: | |
fcbea5e4 | 15010 | mpc_init2(this->u_.complex_val, mpc_precision); |
15011 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15012 | break; |
15013 | default: | |
15014 | go_unreachable(); | |
15015 | } | |
15016 | return *this; | |
15017 | } | |
15018 | ||
15019 | // Clear the contents. | |
15020 | ||
15021 | void | |
15022 | Numeric_constant::clear() | |
15023 | { | |
15024 | switch (this->classification_) | |
15025 | { | |
15026 | case NC_INVALID: | |
15027 | break; | |
15028 | case NC_INT: | |
15029 | case NC_RUNE: | |
15030 | mpz_clear(this->u_.int_val); | |
15031 | break; | |
15032 | case NC_FLOAT: | |
15033 | mpfr_clear(this->u_.float_val); | |
15034 | break; | |
15035 | case NC_COMPLEX: | |
fcbea5e4 | 15036 | mpc_clear(this->u_.complex_val); |
0c77715b | 15037 | break; |
15038 | default: | |
15039 | go_unreachable(); | |
15040 | } | |
15041 | this->classification_ = NC_INVALID; | |
15042 | } | |
15043 | ||
15044 | // Set to an unsigned long value. | |
15045 | ||
15046 | void | |
15047 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15048 | { | |
15049 | this->clear(); | |
15050 | this->classification_ = NC_INT; | |
15051 | this->type_ = type; | |
15052 | mpz_init_set_ui(this->u_.int_val, val); | |
15053 | } | |
15054 | ||
15055 | // Set to an integer value. | |
15056 | ||
15057 | void | |
15058 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15059 | { | |
15060 | this->clear(); | |
15061 | this->classification_ = NC_INT; | |
15062 | this->type_ = type; | |
15063 | mpz_init_set(this->u_.int_val, val); | |
15064 | } | |
15065 | ||
15066 | // Set to a rune value. | |
15067 | ||
15068 | void | |
15069 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15070 | { | |
15071 | this->clear(); | |
15072 | this->classification_ = NC_RUNE; | |
15073 | this->type_ = type; | |
15074 | mpz_init_set(this->u_.int_val, val); | |
15075 | } | |
15076 | ||
15077 | // Set to a floating point value. | |
15078 | ||
15079 | void | |
15080 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15081 | { | |
15082 | this->clear(); | |
15083 | this->classification_ = NC_FLOAT; | |
15084 | this->type_ = type; | |
833b523c | 15085 | // Numeric constants do not have negative zero values, so remove |
15086 | // them here. They also don't have infinity or NaN values, but we | |
15087 | // should never see them here. | |
15088 | if (mpfr_zero_p(val)) | |
15089 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
15090 | else | |
15091 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 15092 | } |
15093 | ||
15094 | // Set to a complex value. | |
15095 | ||
15096 | void | |
fcbea5e4 | 15097 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 15098 | { |
15099 | this->clear(); | |
15100 | this->classification_ = NC_COMPLEX; | |
15101 | this->type_ = type; | |
fcbea5e4 | 15102 | mpc_init2(this->u_.complex_val, mpc_precision); |
15103 | mpc_set(this->u_.complex_val, val, MPC_RNDNN); | |
0c77715b | 15104 | } |
15105 | ||
15106 | // Get an int value. | |
15107 | ||
15108 | void | |
15109 | Numeric_constant::get_int(mpz_t* val) const | |
15110 | { | |
15111 | go_assert(this->is_int()); | |
15112 | mpz_init_set(*val, this->u_.int_val); | |
15113 | } | |
15114 | ||
15115 | // Get a rune value. | |
15116 | ||
15117 | void | |
15118 | Numeric_constant::get_rune(mpz_t* val) const | |
15119 | { | |
15120 | go_assert(this->is_rune()); | |
15121 | mpz_init_set(*val, this->u_.int_val); | |
15122 | } | |
15123 | ||
15124 | // Get a floating point value. | |
15125 | ||
15126 | void | |
15127 | Numeric_constant::get_float(mpfr_t* val) const | |
15128 | { | |
15129 | go_assert(this->is_float()); | |
15130 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15131 | } | |
15132 | ||
15133 | // Get a complex value. | |
15134 | ||
15135 | void | |
fcbea5e4 | 15136 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 15137 | { |
15138 | go_assert(this->is_complex()); | |
fcbea5e4 | 15139 | mpc_init2(*val, mpc_precision); |
15140 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 15141 | } |
15142 | ||
15143 | // Express value as unsigned long if possible. | |
15144 | ||
15145 | Numeric_constant::To_unsigned_long | |
15146 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
15147 | { | |
15148 | switch (this->classification_) | |
15149 | { | |
15150 | case NC_INT: | |
15151 | case NC_RUNE: | |
15152 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
15153 | case NC_FLOAT: | |
15154 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
15155 | case NC_COMPLEX: | |
fcbea5e4 | 15156 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15157 | return NC_UL_NOTINT; |
fcbea5e4 | 15158 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
15159 | val); | |
0c77715b | 15160 | default: |
15161 | go_unreachable(); | |
15162 | } | |
15163 | } | |
15164 | ||
15165 | // Express integer value as unsigned long if possible. | |
15166 | ||
15167 | Numeric_constant::To_unsigned_long | |
15168 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
15169 | unsigned long *val) const | |
15170 | { | |
15171 | if (mpz_sgn(ival) < 0) | |
15172 | return NC_UL_NEGATIVE; | |
15173 | unsigned long ui = mpz_get_ui(ival); | |
15174 | if (mpz_cmp_ui(ival, ui) != 0) | |
15175 | return NC_UL_BIG; | |
15176 | *val = ui; | |
15177 | return NC_UL_VALID; | |
15178 | } | |
15179 | ||
15180 | // Express floating point value as unsigned long if possible. | |
15181 | ||
15182 | Numeric_constant::To_unsigned_long | |
15183 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
15184 | unsigned long *val) const | |
15185 | { | |
15186 | if (!mpfr_integer_p(fval)) | |
15187 | return NC_UL_NOTINT; | |
15188 | mpz_t ival; | |
15189 | mpz_init(ival); | |
15190 | mpfr_get_z(ival, fval, GMP_RNDN); | |
15191 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
15192 | mpz_clear(ival); | |
15193 | return ret; | |
15194 | } | |
15195 | ||
15196 | // Convert value to integer if possible. | |
15197 | ||
15198 | bool | |
15199 | Numeric_constant::to_int(mpz_t* val) const | |
15200 | { | |
15201 | switch (this->classification_) | |
15202 | { | |
15203 | case NC_INT: | |
15204 | case NC_RUNE: | |
15205 | mpz_init_set(*val, this->u_.int_val); | |
15206 | return true; | |
15207 | case NC_FLOAT: | |
15208 | if (!mpfr_integer_p(this->u_.float_val)) | |
15209 | return false; | |
15210 | mpz_init(*val); | |
15211 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
15212 | return true; | |
15213 | case NC_COMPLEX: | |
fcbea5e4 | 15214 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
15215 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 15216 | return false; |
15217 | mpz_init(*val); | |
fcbea5e4 | 15218 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15219 | return true; |
15220 | default: | |
15221 | go_unreachable(); | |
15222 | } | |
15223 | } | |
15224 | ||
15225 | // Convert value to floating point if possible. | |
15226 | ||
15227 | bool | |
15228 | Numeric_constant::to_float(mpfr_t* val) const | |
15229 | { | |
15230 | switch (this->classification_) | |
15231 | { | |
15232 | case NC_INT: | |
15233 | case NC_RUNE: | |
15234 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
15235 | return true; | |
15236 | case NC_FLOAT: | |
15237 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15238 | return true; | |
15239 | case NC_COMPLEX: | |
fcbea5e4 | 15240 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15241 | return false; |
fcbea5e4 | 15242 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15243 | return true; |
15244 | default: | |
15245 | go_unreachable(); | |
15246 | } | |
15247 | } | |
15248 | ||
15249 | // Convert value to complex. | |
15250 | ||
15251 | bool | |
fcbea5e4 | 15252 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 15253 | { |
fcbea5e4 | 15254 | mpc_init2(*val, mpc_precision); |
0c77715b | 15255 | switch (this->classification_) |
15256 | { | |
15257 | case NC_INT: | |
15258 | case NC_RUNE: | |
fcbea5e4 | 15259 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 15260 | return true; |
15261 | case NC_FLOAT: | |
fcbea5e4 | 15262 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 15263 | return true; |
15264 | case NC_COMPLEX: | |
fcbea5e4 | 15265 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 15266 | return true; |
15267 | default: | |
15268 | go_unreachable(); | |
15269 | } | |
15270 | } | |
15271 | ||
15272 | // Get the type. | |
15273 | ||
15274 | Type* | |
15275 | Numeric_constant::type() const | |
15276 | { | |
15277 | if (this->type_ != NULL) | |
15278 | return this->type_; | |
15279 | switch (this->classification_) | |
15280 | { | |
15281 | case NC_INT: | |
15282 | return Type::make_abstract_integer_type(); | |
15283 | case NC_RUNE: | |
15284 | return Type::make_abstract_character_type(); | |
15285 | case NC_FLOAT: | |
15286 | return Type::make_abstract_float_type(); | |
15287 | case NC_COMPLEX: | |
15288 | return Type::make_abstract_complex_type(); | |
15289 | default: | |
15290 | go_unreachable(); | |
15291 | } | |
15292 | } | |
15293 | ||
15294 | // If the constant can be expressed in TYPE, then set the type of the | |
15295 | // constant to TYPE and return true. Otherwise return false, and, if | |
15296 | // ISSUE_ERROR is true, report an appropriate error message. | |
15297 | ||
15298 | bool | |
15299 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
15300 | { | |
15301 | bool ret; | |
f11c2155 | 15302 | if (type == NULL || type->is_error()) |
0c77715b | 15303 | ret = true; |
15304 | else if (type->integer_type() != NULL) | |
15305 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
15306 | else if (type->float_type() != NULL) | |
15307 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
15308 | else if (type->complex_type() != NULL) | |
15309 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
15310 | else | |
5706ab68 | 15311 | { |
15312 | ret = false; | |
15313 | if (issue_error) | |
15314 | go_assert(saw_errors()); | |
15315 | } | |
0c77715b | 15316 | if (ret) |
15317 | this->type_ = type; | |
15318 | return ret; | |
15319 | } | |
15320 | ||
15321 | // Check whether the constant can be expressed in an integer type. | |
15322 | ||
15323 | bool | |
15324 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 15325 | Location location) |
0c77715b | 15326 | { |
15327 | mpz_t val; | |
15328 | switch (this->classification_) | |
15329 | { | |
15330 | case NC_INT: | |
15331 | case NC_RUNE: | |
15332 | mpz_init_set(val, this->u_.int_val); | |
15333 | break; | |
15334 | ||
15335 | case NC_FLOAT: | |
15336 | if (!mpfr_integer_p(this->u_.float_val)) | |
15337 | { | |
15338 | if (issue_error) | |
71a45216 | 15339 | { |
15340 | error_at(location, | |
15341 | "floating point constant truncated to integer"); | |
15342 | this->set_invalid(); | |
15343 | } | |
0c77715b | 15344 | return false; |
15345 | } | |
15346 | mpz_init(val); | |
15347 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
15348 | break; | |
15349 | ||
15350 | case NC_COMPLEX: | |
fcbea5e4 | 15351 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
15352 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 15353 | { |
15354 | if (issue_error) | |
71a45216 | 15355 | { |
15356 | error_at(location, "complex constant truncated to integer"); | |
15357 | this->set_invalid(); | |
15358 | } | |
0c77715b | 15359 | return false; |
15360 | } | |
15361 | mpz_init(val); | |
fcbea5e4 | 15362 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15363 | break; |
15364 | ||
15365 | default: | |
15366 | go_unreachable(); | |
15367 | } | |
15368 | ||
15369 | bool ret; | |
15370 | if (type->is_abstract()) | |
15371 | ret = true; | |
15372 | else | |
15373 | { | |
15374 | int bits = mpz_sizeinbase(val, 2); | |
15375 | if (type->is_unsigned()) | |
15376 | { | |
15377 | // For an unsigned type we can only accept a nonnegative | |
15378 | // number, and we must be able to represents at least BITS. | |
15379 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
15380 | } | |
15381 | else | |
15382 | { | |
15383 | // For a signed type we need an extra bit to indicate the | |
15384 | // sign. We have to handle the most negative integer | |
15385 | // specially. | |
15386 | ret = (bits + 1 <= type->bits() | |
15387 | || (bits <= type->bits() | |
15388 | && mpz_sgn(val) < 0 | |
15389 | && (mpz_scan1(val, 0) | |
15390 | == static_cast<unsigned long>(type->bits() - 1)) | |
15391 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
15392 | } | |
15393 | } | |
15394 | ||
15395 | if (!ret && issue_error) | |
71a45216 | 15396 | { |
15397 | error_at(location, "integer constant overflow"); | |
15398 | this->set_invalid(); | |
15399 | } | |
0c77715b | 15400 | |
15401 | return ret; | |
15402 | } | |
15403 | ||
15404 | // Check whether the constant can be expressed in a floating point | |
15405 | // type. | |
15406 | ||
15407 | bool | |
15408 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 15409 | Location location) |
0c77715b | 15410 | { |
15411 | mpfr_t val; | |
15412 | switch (this->classification_) | |
15413 | { | |
15414 | case NC_INT: | |
15415 | case NC_RUNE: | |
15416 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
15417 | break; | |
15418 | ||
15419 | case NC_FLOAT: | |
15420 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
15421 | break; | |
15422 | ||
15423 | case NC_COMPLEX: | |
fcbea5e4 | 15424 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15425 | { |
15426 | if (issue_error) | |
71a45216 | 15427 | { |
15428 | this->set_invalid(); | |
15429 | error_at(location, "complex constant truncated to float"); | |
15430 | } | |
0c77715b | 15431 | return false; |
15432 | } | |
fcbea5e4 | 15433 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15434 | break; |
15435 | ||
15436 | default: | |
15437 | go_unreachable(); | |
15438 | } | |
15439 | ||
15440 | bool ret; | |
15441 | if (type->is_abstract()) | |
15442 | ret = true; | |
15443 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
15444 | { | |
15445 | // A NaN or Infinity always fits in the range of the type. | |
15446 | ret = true; | |
15447 | } | |
15448 | else | |
15449 | { | |
15450 | mp_exp_t exp = mpfr_get_exp(val); | |
15451 | mp_exp_t max_exp; | |
15452 | switch (type->bits()) | |
15453 | { | |
15454 | case 32: | |
15455 | max_exp = 128; | |
15456 | break; | |
15457 | case 64: | |
15458 | max_exp = 1024; | |
15459 | break; | |
15460 | default: | |
15461 | go_unreachable(); | |
15462 | } | |
15463 | ||
15464 | ret = exp <= max_exp; | |
d0bcce51 | 15465 | |
15466 | if (ret) | |
15467 | { | |
15468 | // Round the constant to the desired type. | |
15469 | mpfr_t t; | |
15470 | mpfr_init(t); | |
15471 | switch (type->bits()) | |
15472 | { | |
15473 | case 32: | |
15474 | mpfr_set_prec(t, 24); | |
15475 | break; | |
15476 | case 64: | |
15477 | mpfr_set_prec(t, 53); | |
15478 | break; | |
15479 | default: | |
15480 | go_unreachable(); | |
15481 | } | |
15482 | mpfr_set(t, val, GMP_RNDN); | |
15483 | mpfr_set(val, t, GMP_RNDN); | |
15484 | mpfr_clear(t); | |
15485 | ||
15486 | this->set_float(type, val); | |
15487 | } | |
0c77715b | 15488 | } |
15489 | ||
15490 | mpfr_clear(val); | |
15491 | ||
15492 | if (!ret && issue_error) | |
71a45216 | 15493 | { |
15494 | error_at(location, "floating point constant overflow"); | |
15495 | this->set_invalid(); | |
15496 | } | |
0c77715b | 15497 | |
15498 | return ret; | |
15499 | } | |
15500 | ||
15501 | // Check whether the constant can be expressed in a complex type. | |
15502 | ||
15503 | bool | |
15504 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 15505 | Location location) |
0c77715b | 15506 | { |
15507 | if (type->is_abstract()) | |
15508 | return true; | |
15509 | ||
15510 | mp_exp_t max_exp; | |
15511 | switch (type->bits()) | |
15512 | { | |
15513 | case 64: | |
15514 | max_exp = 128; | |
15515 | break; | |
15516 | case 128: | |
15517 | max_exp = 1024; | |
15518 | break; | |
15519 | default: | |
15520 | go_unreachable(); | |
15521 | } | |
15522 | ||
fcbea5e4 | 15523 | mpc_t val; |
15524 | mpc_init2(val, mpc_precision); | |
0c77715b | 15525 | switch (this->classification_) |
15526 | { | |
15527 | case NC_INT: | |
15528 | case NC_RUNE: | |
fcbea5e4 | 15529 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 15530 | break; |
15531 | ||
15532 | case NC_FLOAT: | |
fcbea5e4 | 15533 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 15534 | break; |
15535 | ||
15536 | case NC_COMPLEX: | |
fcbea5e4 | 15537 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 15538 | break; |
15539 | ||
15540 | default: | |
15541 | go_unreachable(); | |
15542 | } | |
15543 | ||
d0bcce51 | 15544 | bool ret = true; |
fcbea5e4 | 15545 | if (!mpfr_nan_p(mpc_realref(val)) |
15546 | && !mpfr_inf_p(mpc_realref(val)) | |
15547 | && !mpfr_zero_p(mpc_realref(val)) | |
15548 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 15549 | { |
15550 | if (issue_error) | |
71a45216 | 15551 | { |
15552 | error_at(location, "complex real part overflow"); | |
15553 | this->set_invalid(); | |
15554 | } | |
d0bcce51 | 15555 | ret = false; |
15556 | } | |
0c77715b | 15557 | |
fcbea5e4 | 15558 | if (!mpfr_nan_p(mpc_imagref(val)) |
15559 | && !mpfr_inf_p(mpc_imagref(val)) | |
15560 | && !mpfr_zero_p(mpc_imagref(val)) | |
15561 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 15562 | { |
15563 | if (issue_error) | |
71a45216 | 15564 | { |
15565 | error_at(location, "complex imaginary part overflow"); | |
15566 | this->set_invalid(); | |
15567 | } | |
d0bcce51 | 15568 | ret = false; |
15569 | } | |
0c77715b | 15570 | |
d0bcce51 | 15571 | if (ret) |
15572 | { | |
15573 | // Round the constant to the desired type. | |
fcbea5e4 | 15574 | mpc_t t; |
d0bcce51 | 15575 | switch (type->bits()) |
15576 | { | |
15577 | case 64: | |
fcbea5e4 | 15578 | mpc_init2(t, 24); |
d0bcce51 | 15579 | break; |
15580 | case 128: | |
fcbea5e4 | 15581 | mpc_init2(t, 53); |
d0bcce51 | 15582 | break; |
15583 | default: | |
15584 | go_unreachable(); | |
15585 | } | |
fcbea5e4 | 15586 | mpc_set(t, val, MPC_RNDNN); |
15587 | mpc_set(val, t, MPC_RNDNN); | |
15588 | mpc_clear(t); | |
d0bcce51 | 15589 | |
fcbea5e4 | 15590 | this->set_complex(type, val); |
d0bcce51 | 15591 | } |
15592 | ||
fcbea5e4 | 15593 | mpc_clear(val); |
0c77715b | 15594 | |
15595 | return ret; | |
15596 | } | |
15597 | ||
15598 | // Return an Expression for this value. | |
15599 | ||
15600 | Expression* | |
15601 | Numeric_constant::expression(Location loc) const | |
15602 | { | |
15603 | switch (this->classification_) | |
15604 | { | |
15605 | case NC_INT: | |
e67508fa | 15606 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 15607 | case NC_RUNE: |
15608 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
15609 | case NC_FLOAT: | |
15610 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
15611 | case NC_COMPLEX: | |
fcbea5e4 | 15612 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 15613 | case NC_INVALID: |
15614 | go_assert(saw_errors()); | |
15615 | return Expression::make_error(loc); | |
0c77715b | 15616 | default: |
15617 | go_unreachable(); | |
15618 | } | |
15619 | } |