]>
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* |
136 | Expression::convert_for_assignment(Gogo* gogo, Type* lhs_type, | |
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. |
160 | mpz_t zval; | |
161 | mpz_init_set_ui(zval, 0UL); | |
162 | Expression* zero = Expression::make_integer(&zval, NULL, location); | |
163 | mpz_clear(zval); | |
164 | Expression* nil = Expression::make_nil(location); | |
165 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); | |
e440a328 | 166 | } |
167 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 168 | return Expression::make_nil(location); |
169 | else if (Type::are_identical(lhs_type, rhs_type, false, NULL)) | |
e440a328 | 170 | { |
171 | // No conversion is needed. | |
2c809f8f | 172 | return rhs; |
173 | } | |
174 | else if (lhs_type->points_to() != NULL) | |
175 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
176 | else if (lhs_type->is_numeric_type()) | |
177 | return Expression::make_cast(lhs_type, rhs, location); | |
178 | else if ((lhs_type->struct_type() != NULL | |
179 | && rhs_type->struct_type() != NULL) | |
180 | || (lhs_type->array_type() != NULL | |
181 | && rhs_type->array_type() != NULL)) | |
e440a328 | 182 | { |
bb92f513 | 183 | // Avoid confusion from zero sized variables which may be |
184 | // represented as non-zero-sized. | |
2c809f8f | 185 | // TODO(cmang): This check is for a GCC-specific issue, and should be |
186 | // removed from the frontend. FIXME. | |
187 | size_t lhs_size = gogo->backend()->type_size(lhs_type->get_backend(gogo)); | |
188 | size_t rhs_size = gogo->backend()->type_size(rhs_type->get_backend(gogo)); | |
189 | if (rhs_size == 0 || lhs_size == 0) | |
190 | return rhs; | |
bb92f513 | 191 | |
e440a328 | 192 | // This conversion must be permitted by Go, or we wouldn't have |
193 | // gotten here. | |
2c809f8f | 194 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 195 | } |
196 | else | |
2c809f8f | 197 | return rhs; |
e440a328 | 198 | } |
199 | ||
2c809f8f | 200 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 201 | // interface type. |
202 | ||
2c809f8f | 203 | Expression* |
204 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
205 | Location location) | |
e440a328 | 206 | { |
e440a328 | 207 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
208 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
209 | ||
210 | // Since RHS_TYPE is a static type, we can create the interface | |
211 | // method table at compile time. | |
212 | ||
213 | // When setting an interface to nil, we just set both fields to | |
214 | // NULL. | |
2c809f8f | 215 | Type* rhs_type = rhs->type(); |
e440a328 | 216 | if (rhs_type->is_nil_type()) |
63697958 | 217 | { |
2c809f8f | 218 | Expression* nil = Expression::make_nil(location); |
219 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 220 | } |
e440a328 | 221 | |
222 | // This should have been checked already. | |
c484d925 | 223 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 224 | |
e440a328 | 225 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
226 | // then the first field is the type descriptor for RHS_TYPE. | |
227 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 228 | Expression* first_field; |
e440a328 | 229 | if (lhs_is_empty) |
2c809f8f | 230 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 231 | else |
232 | { | |
233 | // Build the interface method table for this interface and this | |
234 | // object type: a list of function pointers for each interface | |
235 | // method. | |
236 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 237 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 238 | bool is_pointer = false; |
c0cab2ec | 239 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 240 | { |
241 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 242 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 243 | is_pointer = true; |
244 | } | |
c0cab2ec | 245 | if (rhs_named_type != NULL) |
2c809f8f | 246 | first_field = |
247 | rhs_named_type->interface_method_table(lhs_interface_type, | |
248 | is_pointer); | |
c0cab2ec | 249 | else if (rhs_struct_type != NULL) |
2c809f8f | 250 | first_field = |
251 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
252 | is_pointer); | |
c0cab2ec | 253 | else |
2c809f8f | 254 | first_field = Expression::make_nil(location); |
e440a328 | 255 | } |
e440a328 | 256 | |
2c809f8f | 257 | Expression* obj; |
e440a328 | 258 | if (rhs_type->points_to() != NULL) |
259 | { | |
2c809f8f | 260 | // We are assigning a pointer to the interface; the interface |
e440a328 | 261 | // holds the pointer itself. |
2c809f8f | 262 | obj = rhs; |
263 | } | |
264 | else | |
265 | { | |
266 | // We are assigning a non-pointer value to the interface; the | |
267 | // interface gets a copy of the value in the heap. | |
268 | obj = Expression::make_heap_expression(rhs, location); | |
e440a328 | 269 | } |
270 | ||
2c809f8f | 271 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
272 | } | |
e440a328 | 273 | |
2c809f8f | 274 | // Return an expression for the type descriptor of RHS. |
e440a328 | 275 | |
2c809f8f | 276 | Expression* |
277 | Expression::get_interface_type_descriptor(Expression* rhs) | |
278 | { | |
279 | go_assert(rhs->type()->interface_type() != NULL); | |
280 | Location location = rhs->location(); | |
e440a328 | 281 | |
2c809f8f | 282 | // The type descriptor is the first field of an empty interface. |
283 | if (rhs->type()->interface_type()->is_empty()) | |
284 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
285 | location); | |
286 | ||
287 | Expression* mtable = | |
288 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 289 | |
2c809f8f | 290 | Expression* descriptor = |
291 | Expression::make_unary(OPERATOR_MULT, mtable, location); | |
292 | descriptor = Expression::make_field_reference(descriptor, 0, location); | |
293 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 294 | |
2c809f8f | 295 | Expression* eq = |
296 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
297 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 298 | } |
299 | ||
2c809f8f | 300 | // Return an expression for the conversion of an interface type to an |
e440a328 | 301 | // interface type. |
302 | ||
2c809f8f | 303 | Expression* |
304 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
305 | bool for_type_guard, | |
306 | Location location) | |
e440a328 | 307 | { |
e440a328 | 308 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
309 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
310 | ||
e440a328 | 311 | // In the general case this requires runtime examination of the type |
312 | // method table to match it up with the interface methods. | |
313 | ||
314 | // FIXME: If all of the methods in the right hand side interface | |
315 | // also appear in the left hand side interface, then we don't need | |
316 | // to do a runtime check, although we still need to build a new | |
317 | // method table. | |
318 | ||
319 | // Get the type descriptor for the right hand side. This will be | |
320 | // NULL for a nil interface. | |
2c809f8f | 321 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
322 | Expression* lhs_type_expr = | |
323 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 324 | |
2c809f8f | 325 | Expression* first_field; |
e440a328 | 326 | if (for_type_guard) |
327 | { | |
328 | // A type assertion fails when converting a nil interface. | |
2c809f8f | 329 | first_field = |
330 | Runtime::make_call(Runtime::ASSERT_INTERFACE, location, 2, | |
331 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 332 | } |
333 | else if (lhs_is_empty) | |
334 | { | |
2c809f8f | 335 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 336 | // first field is just the type descriptor of the object. |
2c809f8f | 337 | first_field = rhs_type_expr; |
e440a328 | 338 | } |
339 | else | |
340 | { | |
341 | // A conversion to a non-empty interface may fail, but unlike a | |
342 | // type assertion converting nil will always succeed. | |
2c809f8f | 343 | first_field = |
344 | Runtime::make_call(Runtime::CONVERT_INTERFACE, location, 2, | |
345 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 346 | } |
347 | ||
348 | // The second field is simply the object pointer. | |
2c809f8f | 349 | Expression* obj = |
350 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
351 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 352 | } |
353 | ||
2c809f8f | 354 | // Return an expression for the conversion of an interface type to a |
e440a328 | 355 | // non-interface type. |
356 | ||
2c809f8f | 357 | Expression* |
358 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
359 | Location location) | |
e440a328 | 360 | { |
e440a328 | 361 | // Call a function to check that the type is valid. The function |
362 | // will panic with an appropriate runtime type error if the type is | |
363 | // not valid. | |
2c809f8f | 364 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
365 | location); | |
366 | Expression* rhs_descriptor = | |
367 | Expression::get_interface_type_descriptor(rhs); | |
368 | ||
369 | Type* rhs_type = rhs->type(); | |
370 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
371 | location); | |
372 | ||
373 | Expression* check_iface = Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE, | |
374 | location, 3, lhs_type_expr, | |
375 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 376 | |
377 | // If the call succeeds, pull out the value. | |
2c809f8f | 378 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
379 | location); | |
e440a328 | 380 | |
381 | // If the value is a pointer, then it is the value we want. | |
382 | // Otherwise it points to the value. | |
383 | if (lhs_type->points_to() == NULL) | |
384 | { | |
2c809f8f | 385 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
386 | location); | |
387 | obj = Expression::make_unary(OPERATOR_MULT, obj, location); | |
e440a328 | 388 | } |
2c809f8f | 389 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 390 | } |
391 | ||
ea664253 | 392 | // Convert an expression to its backend representation. This is implemented by |
393 | // the child class. Not that it is not in general safe to call this multiple | |
e440a328 | 394 | // times for a single expression, but that we don't catch such errors. |
395 | ||
ea664253 | 396 | Bexpression* |
397 | Expression::get_backend(Translate_context* context) | |
e440a328 | 398 | { |
399 | // The child may have marked this expression as having an error. | |
400 | if (this->classification_ == EXPRESSION_ERROR) | |
ea664253 | 401 | return context->backend()->error_expression(); |
e440a328 | 402 | |
ea664253 | 403 | return this->do_get_backend(context); |
e440a328 | 404 | } |
405 | ||
48c2a53a | 406 | // Return a backend expression for VAL. |
407 | Bexpression* | |
408 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
409 | Numeric_constant* val) | |
e440a328 | 410 | { |
48c2a53a | 411 | Gogo* gogo = context->gogo(); |
412 | Type* type = val->type(); | |
413 | if (type == NULL) | |
414 | return gogo->backend()->error_expression(); | |
e440a328 | 415 | |
48c2a53a | 416 | Btype* btype = type->get_backend(gogo); |
417 | Bexpression* ret; | |
418 | if (type->integer_type() != NULL) | |
e440a328 | 419 | { |
420 | mpz_t ival; | |
48c2a53a | 421 | if (!val->to_int(&ival)) |
422 | { | |
423 | go_assert(saw_errors()); | |
424 | return gogo->backend()->error_expression(); | |
425 | } | |
426 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 427 | mpz_clear(ival); |
e440a328 | 428 | } |
48c2a53a | 429 | else if (type->float_type() != NULL) |
e440a328 | 430 | { |
48c2a53a | 431 | mpfr_t fval; |
432 | if (!val->to_float(&fval)) | |
433 | { | |
434 | go_assert(saw_errors()); | |
435 | return gogo->backend()->error_expression(); | |
436 | } | |
437 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
438 | mpfr_clear(fval); | |
e440a328 | 439 | } |
48c2a53a | 440 | else if (type->complex_type() != NULL) |
e440a328 | 441 | { |
48c2a53a | 442 | mpfr_t real; |
443 | mpfr_t imag; | |
444 | if (!val->to_complex(&real, &imag)) | |
445 | { | |
446 | go_assert(saw_errors()); | |
447 | return gogo->backend()->error_expression(); | |
448 | } | |
449 | ret = gogo->backend()->complex_constant_expression(btype, real, imag); | |
450 | mpfr_clear(real); | |
451 | mpfr_clear(imag); | |
e440a328 | 452 | } |
453 | else | |
c3e6f413 | 454 | go_unreachable(); |
e440a328 | 455 | |
48c2a53a | 456 | return ret; |
e440a328 | 457 | } |
458 | ||
2c809f8f | 459 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
460 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 461 | |
2c809f8f | 462 | Expression* |
463 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 464 | { |
2c809f8f | 465 | Type* val_type = val->type(); |
466 | Type* bound_type = Type::lookup_integer_type("int"); | |
467 | ||
468 | int val_type_size; | |
469 | bool val_is_unsigned = false; | |
470 | if (val_type->integer_type() != NULL) | |
471 | { | |
472 | val_type_size = val_type->integer_type()->bits(); | |
473 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
474 | } | |
475 | else | |
476 | { | |
477 | if (!val_type->is_numeric_type() | |
478 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
479 | { | |
480 | go_assert(saw_errors()); | |
481 | return Expression::make_boolean(true, loc); | |
482 | } | |
e440a328 | 483 | |
2c809f8f | 484 | if (val_type->complex_type() != NULL) |
485 | val_type_size = val_type->complex_type()->bits(); | |
486 | else | |
487 | val_type_size = val_type->float_type()->bits(); | |
488 | } | |
489 | ||
490 | Expression* negative_index = Expression::make_boolean(false, loc); | |
491 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
492 | if (!val_is_unsigned) | |
e440a328 | 493 | { |
2c809f8f | 494 | mpz_t zval; |
495 | mpz_init_set_ui(zval, 0UL); | |
496 | Expression* zero = Expression::make_integer(&zval, val_type, loc); | |
497 | mpz_clear(zval); | |
498 | ||
499 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); | |
e440a328 | 500 | } |
501 | ||
2c809f8f | 502 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 503 | if (val_type_size > bound_type_size |
504 | || (val_type_size == bound_type_size | |
2c809f8f | 505 | && val_is_unsigned)) |
506 | { | |
507 | mpz_t one; | |
508 | mpz_init_set_ui(one, 1UL); | |
509 | ||
510 | // maxval = 2^(bound_type_size - 1) - 1 | |
511 | mpz_t maxval; | |
512 | mpz_init(maxval); | |
513 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
514 | mpz_sub_ui(maxval, maxval, 1); | |
515 | Expression* max = Expression::make_integer(&maxval, val_type, loc); | |
516 | mpz_clear(one); | |
517 | mpz_clear(maxval); | |
518 | ||
519 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 520 | } |
521 | ||
2c809f8f | 522 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
523 | loc); | |
e440a328 | 524 | } |
525 | ||
d751bb78 | 526 | void |
527 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
528 | { | |
529 | this->do_dump_expression(ast_dump_context); | |
530 | } | |
531 | ||
e440a328 | 532 | // Error expressions. This are used to avoid cascading errors. |
533 | ||
534 | class Error_expression : public Expression | |
535 | { | |
536 | public: | |
b13c66cd | 537 | Error_expression(Location location) |
e440a328 | 538 | : Expression(EXPRESSION_ERROR, location) |
539 | { } | |
540 | ||
541 | protected: | |
542 | bool | |
543 | do_is_constant() const | |
544 | { return true; } | |
545 | ||
0e168074 | 546 | bool |
547 | do_is_immutable() const | |
548 | { return true; } | |
549 | ||
e440a328 | 550 | bool |
0c77715b | 551 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 552 | { |
0c77715b | 553 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 554 | return true; |
555 | } | |
556 | ||
4f2138d7 | 557 | bool |
e440a328 | 558 | do_discarding_value() |
4f2138d7 | 559 | { return true; } |
e440a328 | 560 | |
561 | Type* | |
562 | do_type() | |
563 | { return Type::make_error_type(); } | |
564 | ||
565 | void | |
566 | do_determine_type(const Type_context*) | |
567 | { } | |
568 | ||
569 | Expression* | |
570 | do_copy() | |
571 | { return this; } | |
572 | ||
573 | bool | |
574 | do_is_addressable() const | |
575 | { return true; } | |
576 | ||
ea664253 | 577 | Bexpression* |
578 | do_get_backend(Translate_context* context) | |
579 | { return context->backend()->error_expression(); } | |
d751bb78 | 580 | |
581 | void | |
582 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 583 | }; |
584 | ||
d751bb78 | 585 | // Dump the ast representation for an error expression to a dump context. |
586 | ||
587 | void | |
588 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
589 | { | |
590 | ast_dump_context->ostream() << "_Error_" ; | |
591 | } | |
592 | ||
e440a328 | 593 | Expression* |
b13c66cd | 594 | Expression::make_error(Location location) |
e440a328 | 595 | { |
596 | return new Error_expression(location); | |
597 | } | |
598 | ||
599 | // An expression which is really a type. This is used during parsing. | |
600 | // It is an error if these survive after lowering. | |
601 | ||
602 | class | |
603 | Type_expression : public Expression | |
604 | { | |
605 | public: | |
b13c66cd | 606 | Type_expression(Type* type, Location location) |
e440a328 | 607 | : Expression(EXPRESSION_TYPE, location), |
608 | type_(type) | |
609 | { } | |
610 | ||
611 | protected: | |
612 | int | |
613 | do_traverse(Traverse* traverse) | |
614 | { return Type::traverse(this->type_, traverse); } | |
615 | ||
616 | Type* | |
617 | do_type() | |
618 | { return this->type_; } | |
619 | ||
620 | void | |
621 | do_determine_type(const Type_context*) | |
622 | { } | |
623 | ||
624 | void | |
625 | do_check_types(Gogo*) | |
626 | { this->report_error(_("invalid use of type")); } | |
627 | ||
628 | Expression* | |
629 | do_copy() | |
630 | { return this; } | |
631 | ||
ea664253 | 632 | Bexpression* |
633 | do_get_backend(Translate_context*) | |
c3e6f413 | 634 | { go_unreachable(); } |
e440a328 | 635 | |
d751bb78 | 636 | void do_dump_expression(Ast_dump_context*) const; |
637 | ||
e440a328 | 638 | private: |
639 | // The type which we are representing as an expression. | |
640 | Type* type_; | |
641 | }; | |
642 | ||
d751bb78 | 643 | void |
644 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
645 | { | |
646 | ast_dump_context->dump_type(this->type_); | |
647 | } | |
648 | ||
e440a328 | 649 | Expression* |
b13c66cd | 650 | Expression::make_type(Type* type, Location location) |
e440a328 | 651 | { |
652 | return new Type_expression(type, location); | |
653 | } | |
654 | ||
e03bdf36 | 655 | // Class Parser_expression. |
656 | ||
657 | Type* | |
658 | Parser_expression::do_type() | |
659 | { | |
660 | // We should never really ask for the type of a Parser_expression. | |
661 | // However, it can happen, at least when we have an invalid const | |
662 | // whose initializer refers to the const itself. In that case we | |
663 | // may ask for the type when lowering the const itself. | |
c484d925 | 664 | go_assert(saw_errors()); |
e03bdf36 | 665 | return Type::make_error_type(); |
666 | } | |
667 | ||
e440a328 | 668 | // Class Var_expression. |
669 | ||
670 | // Lower a variable expression. Here we just make sure that the | |
671 | // initialization expression of the variable has been lowered. This | |
672 | // ensures that we will be able to determine the type of the variable | |
673 | // if necessary. | |
674 | ||
675 | Expression* | |
ceeb4318 | 676 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
677 | Statement_inserter* inserter, int) | |
e440a328 | 678 | { |
679 | if (this->variable_->is_variable()) | |
680 | { | |
681 | Variable* var = this->variable_->var_value(); | |
682 | // This is either a local variable or a global variable. A | |
683 | // reference to a variable which is local to an enclosing | |
684 | // function will be a reference to a field in a closure. | |
685 | if (var->is_global()) | |
ceeb4318 | 686 | { |
687 | function = NULL; | |
688 | inserter = NULL; | |
689 | } | |
690 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 691 | } |
692 | return this; | |
693 | } | |
694 | ||
e440a328 | 695 | // Return the type of a reference to a variable. |
696 | ||
697 | Type* | |
698 | Var_expression::do_type() | |
699 | { | |
700 | if (this->variable_->is_variable()) | |
701 | return this->variable_->var_value()->type(); | |
702 | else if (this->variable_->is_result_variable()) | |
703 | return this->variable_->result_var_value()->type(); | |
704 | else | |
c3e6f413 | 705 | go_unreachable(); |
e440a328 | 706 | } |
707 | ||
0ab09e06 | 708 | // Determine the type of a reference to a variable. |
709 | ||
710 | void | |
711 | Var_expression::do_determine_type(const Type_context*) | |
712 | { | |
713 | if (this->variable_->is_variable()) | |
714 | this->variable_->var_value()->determine_type(); | |
715 | } | |
716 | ||
e440a328 | 717 | // Something takes the address of this variable. This means that we |
718 | // may want to move the variable onto the heap. | |
719 | ||
720 | void | |
721 | Var_expression::do_address_taken(bool escapes) | |
722 | { | |
723 | if (!escapes) | |
f325319b | 724 | { |
725 | if (this->variable_->is_variable()) | |
726 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
727 | else if (this->variable_->is_result_variable()) | |
728 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
729 | else | |
730 | go_unreachable(); | |
731 | } | |
e440a328 | 732 | else |
f325319b | 733 | { |
734 | if (this->variable_->is_variable()) | |
735 | this->variable_->var_value()->set_address_taken(); | |
736 | else if (this->variable_->is_result_variable()) | |
737 | this->variable_->result_var_value()->set_address_taken(); | |
738 | else | |
739 | go_unreachable(); | |
740 | } | |
e440a328 | 741 | } |
742 | ||
ea664253 | 743 | // Get the backend representation for a reference to a variable. |
e440a328 | 744 | |
ea664253 | 745 | Bexpression* |
746 | Var_expression::do_get_backend(Translate_context* context) | |
e440a328 | 747 | { |
fe2f84cf | 748 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
749 | context->function()); | |
fe2f84cf | 750 | bool is_in_heap; |
c6777780 | 751 | Location loc = this->location(); |
9b27b43c | 752 | Btype* btype; |
753 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 754 | if (this->variable_->is_variable()) |
9b27b43c | 755 | { |
756 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
757 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
758 | } | |
fe2f84cf | 759 | else if (this->variable_->is_result_variable()) |
9b27b43c | 760 | { |
761 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
762 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
763 | } | |
fe2f84cf | 764 | else |
c3e6f413 | 765 | go_unreachable(); |
c6777780 | 766 | |
767 | Bexpression* ret = context->backend()->var_expression(bvar, loc); | |
fe2f84cf | 768 | if (is_in_heap) |
9b27b43c | 769 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
ea664253 | 770 | return ret; |
e440a328 | 771 | } |
772 | ||
d751bb78 | 773 | // Ast dump for variable expression. |
774 | ||
775 | void | |
776 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
777 | { | |
778 | ast_dump_context->ostream() << this->variable_->name() ; | |
779 | } | |
780 | ||
e440a328 | 781 | // Make a reference to a variable in an expression. |
782 | ||
783 | Expression* | |
b13c66cd | 784 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 785 | { |
786 | if (var->is_sink()) | |
787 | return Expression::make_sink(location); | |
788 | ||
789 | // FIXME: Creating a new object for each reference to a variable is | |
790 | // wasteful. | |
791 | return new Var_expression(var, location); | |
792 | } | |
793 | ||
794 | // Class Temporary_reference_expression. | |
795 | ||
796 | // The type. | |
797 | ||
798 | Type* | |
799 | Temporary_reference_expression::do_type() | |
800 | { | |
801 | return this->statement_->type(); | |
802 | } | |
803 | ||
804 | // Called if something takes the address of this temporary variable. | |
805 | // We never have to move temporary variables to the heap, but we do | |
806 | // need to know that they must live in the stack rather than in a | |
807 | // register. | |
808 | ||
809 | void | |
810 | Temporary_reference_expression::do_address_taken(bool) | |
811 | { | |
812 | this->statement_->set_is_address_taken(); | |
813 | } | |
814 | ||
ea664253 | 815 | // Get a backend expression referring to the variable. |
e440a328 | 816 | |
ea664253 | 817 | Bexpression* |
818 | Temporary_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 819 | { |
cd440cff | 820 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 821 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
cd440cff | 822 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 823 | |
cd440cff | 824 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 825 | // that the Go frontend can. In some cases this means that a |
826 | // temporary variable won't have the right backend type. Correct | |
827 | // that here by adding a type cast. We need to use base() to push | |
828 | // the circularity down one level. | |
cd440cff | 829 | Type* stype = this->statement_->type(); |
ceeb4318 | 830 | if (!this->is_lvalue_ |
cd440cff | 831 | && stype->has_pointer() |
832 | && stype->deref()->is_void_type()) | |
eefc1ed3 | 833 | { |
cd440cff | 834 | Btype* btype = this->type()->base()->get_backend(gogo); |
835 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 836 | } |
ea664253 | 837 | return ret; |
e440a328 | 838 | } |
839 | ||
d751bb78 | 840 | // Ast dump for temporary reference. |
841 | ||
842 | void | |
843 | Temporary_reference_expression::do_dump_expression( | |
844 | Ast_dump_context* ast_dump_context) const | |
845 | { | |
846 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
847 | } | |
848 | ||
e440a328 | 849 | // Make a reference to a temporary variable. |
850 | ||
ceeb4318 | 851 | Temporary_reference_expression* |
e440a328 | 852 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 853 | Location location) |
e440a328 | 854 | { |
855 | return new Temporary_reference_expression(statement, location); | |
856 | } | |
857 | ||
e9d3367e | 858 | // Class Set_and_use_temporary_expression. |
859 | ||
860 | // Return the type. | |
861 | ||
862 | Type* | |
863 | Set_and_use_temporary_expression::do_type() | |
864 | { | |
865 | return this->statement_->type(); | |
866 | } | |
867 | ||
0afbb937 | 868 | // Determine the type of the expression. |
869 | ||
870 | void | |
871 | Set_and_use_temporary_expression::do_determine_type( | |
872 | const Type_context* context) | |
873 | { | |
874 | this->expr_->determine_type(context); | |
875 | } | |
876 | ||
e9d3367e | 877 | // Take the address. |
878 | ||
879 | void | |
880 | Set_and_use_temporary_expression::do_address_taken(bool) | |
881 | { | |
882 | this->statement_->set_is_address_taken(); | |
883 | } | |
884 | ||
885 | // Return the backend representation. | |
886 | ||
ea664253 | 887 | Bexpression* |
888 | Set_and_use_temporary_expression::do_get_backend(Translate_context* context) | |
e9d3367e | 889 | { |
e9d3367e | 890 | Location loc = this->location(); |
a302c105 | 891 | Gogo* gogo = context->gogo(); |
892 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
893 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); | |
894 | ||
ea664253 | 895 | Bexpression* bexpr = this->expr_->get_backend(context); |
a302c105 | 896 | Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc); |
897 | var_ref = gogo->backend()->var_expression(bvar, loc); | |
898 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); | |
ea664253 | 899 | return ret; |
e9d3367e | 900 | } |
901 | ||
902 | // Dump. | |
903 | ||
904 | void | |
905 | Set_and_use_temporary_expression::do_dump_expression( | |
906 | Ast_dump_context* ast_dump_context) const | |
907 | { | |
908 | ast_dump_context->ostream() << '('; | |
909 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
910 | ast_dump_context->ostream() << " = "; | |
911 | this->expr_->dump_expression(ast_dump_context); | |
912 | ast_dump_context->ostream() << ')'; | |
913 | } | |
914 | ||
915 | // Make a set-and-use temporary. | |
916 | ||
917 | Set_and_use_temporary_expression* | |
918 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
919 | Expression* expr, Location location) | |
920 | { | |
921 | return new Set_and_use_temporary_expression(statement, expr, location); | |
922 | } | |
923 | ||
e440a328 | 924 | // A sink expression--a use of the blank identifier _. |
925 | ||
926 | class Sink_expression : public Expression | |
927 | { | |
928 | public: | |
b13c66cd | 929 | Sink_expression(Location location) |
e440a328 | 930 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 931 | type_(NULL), bvar_(NULL) |
e440a328 | 932 | { } |
933 | ||
934 | protected: | |
4f2138d7 | 935 | bool |
e440a328 | 936 | do_discarding_value() |
4f2138d7 | 937 | { return true; } |
e440a328 | 938 | |
939 | Type* | |
940 | do_type(); | |
941 | ||
942 | void | |
943 | do_determine_type(const Type_context*); | |
944 | ||
945 | Expression* | |
946 | do_copy() | |
947 | { return new Sink_expression(this->location()); } | |
948 | ||
ea664253 | 949 | Bexpression* |
950 | do_get_backend(Translate_context*); | |
e440a328 | 951 | |
d751bb78 | 952 | void |
953 | do_dump_expression(Ast_dump_context*) const; | |
954 | ||
e440a328 | 955 | private: |
956 | // The type of this sink variable. | |
957 | Type* type_; | |
958 | // The temporary variable we generate. | |
aa93217a | 959 | Bvariable* bvar_; |
e440a328 | 960 | }; |
961 | ||
962 | // Return the type of a sink expression. | |
963 | ||
964 | Type* | |
965 | Sink_expression::do_type() | |
966 | { | |
967 | if (this->type_ == NULL) | |
968 | return Type::make_sink_type(); | |
969 | return this->type_; | |
970 | } | |
971 | ||
972 | // Determine the type of a sink expression. | |
973 | ||
974 | void | |
975 | Sink_expression::do_determine_type(const Type_context* context) | |
976 | { | |
977 | if (context->type != NULL) | |
978 | this->type_ = context->type; | |
979 | } | |
980 | ||
981 | // Return a temporary variable for a sink expression. This will | |
982 | // presumably be a write-only variable which the middle-end will drop. | |
983 | ||
ea664253 | 984 | Bexpression* |
985 | Sink_expression::do_get_backend(Translate_context* context) | |
e440a328 | 986 | { |
aa93217a | 987 | Location loc = this->location(); |
988 | Gogo* gogo = context->gogo(); | |
989 | if (this->bvar_ == NULL) | |
e440a328 | 990 | { |
c484d925 | 991 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 992 | Named_object* fn = context->function(); |
993 | go_assert(fn != NULL); | |
994 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 995 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 996 | Bstatement* decl; |
997 | this->bvar_ = | |
998 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
999 | false, loc, &decl); | |
1000 | Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc); | |
1001 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); | |
ea664253 | 1002 | return var_ref; |
e440a328 | 1003 | } |
ea664253 | 1004 | return gogo->backend()->var_expression(this->bvar_, loc); |
e440a328 | 1005 | } |
1006 | ||
d751bb78 | 1007 | // Ast dump for sink expression. |
1008 | ||
1009 | void | |
1010 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1011 | { | |
1012 | ast_dump_context->ostream() << "_" ; | |
1013 | } | |
1014 | ||
e440a328 | 1015 | // Make a sink expression. |
1016 | ||
1017 | Expression* | |
b13c66cd | 1018 | Expression::make_sink(Location location) |
e440a328 | 1019 | { |
1020 | return new Sink_expression(location); | |
1021 | } | |
1022 | ||
1023 | // Class Func_expression. | |
1024 | ||
1025 | // FIXME: Can a function expression appear in a constant expression? | |
1026 | // The value is unchanging. Initializing a constant to the address of | |
1027 | // a function seems like it could work, though there might be little | |
1028 | // point to it. | |
1029 | ||
e440a328 | 1030 | // Traversal. |
1031 | ||
1032 | int | |
1033 | Func_expression::do_traverse(Traverse* traverse) | |
1034 | { | |
1035 | return (this->closure_ == NULL | |
1036 | ? TRAVERSE_CONTINUE | |
1037 | : Expression::traverse(&this->closure_, traverse)); | |
1038 | } | |
1039 | ||
1040 | // Return the type of a function expression. | |
1041 | ||
1042 | Type* | |
1043 | Func_expression::do_type() | |
1044 | { | |
1045 | if (this->function_->is_function()) | |
1046 | return this->function_->func_value()->type(); | |
1047 | else if (this->function_->is_function_declaration()) | |
1048 | return this->function_->func_declaration_value()->type(); | |
1049 | else | |
c3e6f413 | 1050 | go_unreachable(); |
e440a328 | 1051 | } |
1052 | ||
ea664253 | 1053 | // Get the backend representation for the code of a function expression. |
e440a328 | 1054 | |
97267c39 | 1055 | Bexpression* |
8381eda7 | 1056 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1057 | { |
1058 | Function_type* fntype; | |
8381eda7 | 1059 | if (no->is_function()) |
1060 | fntype = no->func_value()->type(); | |
1061 | else if (no->is_function_declaration()) | |
1062 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1063 | else |
c3e6f413 | 1064 | go_unreachable(); |
e440a328 | 1065 | |
1066 | // Builtin functions are handled specially by Call_expression. We | |
1067 | // can't take their address. | |
1068 | if (fntype->is_builtin()) | |
1069 | { | |
8381eda7 | 1070 | error_at(loc, |
cb0e02f3 | 1071 | "invalid use of special builtin function %qs; must be called", |
8381eda7 | 1072 | no->message_name().c_str()); |
97267c39 | 1073 | return gogo->backend()->error_expression(); |
e440a328 | 1074 | } |
1075 | ||
97267c39 | 1076 | Bfunction* fndecl; |
e440a328 | 1077 | if (no->is_function()) |
cf3cae55 | 1078 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1079 | else if (no->is_function_declaration()) |
cf3cae55 | 1080 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1081 | else |
c3e6f413 | 1082 | go_unreachable(); |
e440a328 | 1083 | |
97267c39 | 1084 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1085 | } |
1086 | ||
ea664253 | 1087 | // Get the backend representation for a function expression. This is used when |
1088 | // we take the address of a function rather than simply calling it. A func | |
8381eda7 | 1089 | // value is represented as a pointer to a block of memory. The first |
1090 | // word of that memory is a pointer to the function code. The | |
1091 | // remaining parts of that memory are the addresses of variables that | |
1092 | // the function closes over. | |
e440a328 | 1093 | |
ea664253 | 1094 | Bexpression* |
1095 | Func_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1096 | { |
8381eda7 | 1097 | // If there is no closure, just use the function descriptor. |
2010c17a | 1098 | if (this->closure_ == NULL) |
8381eda7 | 1099 | { |
1100 | Gogo* gogo = context->gogo(); | |
1101 | Named_object* no = this->function_; | |
1102 | Expression* descriptor; | |
1103 | if (no->is_function()) | |
1104 | descriptor = no->func_value()->descriptor(gogo, no); | |
1105 | else if (no->is_function_declaration()) | |
1106 | { | |
1107 | if (no->func_declaration_value()->type()->is_builtin()) | |
1108 | { | |
1109 | error_at(this->location(), | |
1110 | ("invalid use of special builtin function %qs; " | |
1111 | "must be called"), | |
1112 | no->message_name().c_str()); | |
ea664253 | 1113 | return gogo->backend()->error_expression(); |
8381eda7 | 1114 | } |
1115 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1116 | } | |
1117 | else | |
1118 | go_unreachable(); | |
2010c17a | 1119 | |
ea664253 | 1120 | Bexpression* bdesc = descriptor->get_backend(context); |
1121 | return gogo->backend()->address_expression(bdesc, this->location()); | |
8381eda7 | 1122 | } |
e440a328 | 1123 | |
8381eda7 | 1124 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1125 | |
8381eda7 | 1126 | // If there is a closure, then the closure is itself the function |
1127 | // expression. It is a pointer to a struct whose first field points | |
1128 | // to the function code and whose remaining fields are the addresses | |
1129 | // of the closed-over variables. | |
ea664253 | 1130 | return this->closure_->get_backend(context); |
e440a328 | 1131 | } |
1132 | ||
d751bb78 | 1133 | // Ast dump for function. |
1134 | ||
1135 | void | |
1136 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1137 | { | |
8b1c301d | 1138 | ast_dump_context->ostream() << this->function_->name(); |
1139 | if (this->closure_ != NULL) | |
1140 | { | |
1141 | ast_dump_context->ostream() << " {closure = "; | |
1142 | this->closure_->dump_expression(ast_dump_context); | |
1143 | ast_dump_context->ostream() << "}"; | |
1144 | } | |
d751bb78 | 1145 | } |
1146 | ||
e440a328 | 1147 | // Make a reference to a function in an expression. |
1148 | ||
1149 | Expression* | |
1150 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1151 | Location location) |
e440a328 | 1152 | { |
1153 | return new Func_expression(function, closure, location); | |
1154 | } | |
1155 | ||
c6837989 | 1156 | // Class Func_descriptor_expression. |
8381eda7 | 1157 | |
c6837989 | 1158 | // Constructor. |
8381eda7 | 1159 | |
c6837989 | 1160 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1161 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1162 | fn_(fn), dvar_(NULL) |
c6837989 | 1163 | { |
1164 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1165 | } | |
8381eda7 | 1166 | |
c6837989 | 1167 | // Traversal. |
8381eda7 | 1168 | |
c6837989 | 1169 | int |
1170 | Func_descriptor_expression::do_traverse(Traverse*) | |
1171 | { | |
1172 | return TRAVERSE_CONTINUE; | |
1173 | } | |
8381eda7 | 1174 | |
1175 | // All function descriptors have the same type. | |
1176 | ||
1177 | Type* Func_descriptor_expression::descriptor_type; | |
1178 | ||
1179 | void | |
1180 | Func_descriptor_expression::make_func_descriptor_type() | |
1181 | { | |
1182 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1183 | return; | |
1184 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1185 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1186 | Func_descriptor_expression::descriptor_type = | |
1187 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1188 | } | |
1189 | ||
1190 | Type* | |
1191 | Func_descriptor_expression::do_type() | |
1192 | { | |
1193 | Func_descriptor_expression::make_func_descriptor_type(); | |
1194 | return Func_descriptor_expression::descriptor_type; | |
1195 | } | |
1196 | ||
ea664253 | 1197 | // The backend representation for a function descriptor. |
8381eda7 | 1198 | |
ea664253 | 1199 | Bexpression* |
1200 | Func_descriptor_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1201 | { |
8381eda7 | 1202 | Named_object* no = this->fn_; |
1203 | Location loc = no->location(); | |
ea664253 | 1204 | if (this->dvar_ != NULL) |
1205 | return context->backend()->var_expression(this->dvar_, loc); | |
8381eda7 | 1206 | |
ea664253 | 1207 | Gogo* gogo = context->gogo(); |
8381eda7 | 1208 | std::string var_name; |
1209 | if (no->package() == NULL) | |
1210 | var_name = gogo->pkgpath_symbol(); | |
1211 | else | |
1212 | var_name = no->package()->pkgpath_symbol(); | |
1213 | var_name.push_back('.'); | |
1214 | var_name.append(Gogo::unpack_hidden_name(no->name())); | |
1215 | var_name.append("$descriptor"); | |
1216 | ||
1217 | Btype* btype = this->type()->get_backend(gogo); | |
1218 | ||
1219 | Bvariable* bvar; | |
1220 | if (no->package() != NULL | |
1221 | || Linemap::is_predeclared_location(no->location())) | |
f8bdf81a | 1222 | bvar = context->backend()->immutable_struct_reference(var_name, btype, |
1223 | loc); | |
8381eda7 | 1224 | else |
1225 | { | |
1226 | Location bloc = Linemap::predeclared_location(); | |
1227 | bool is_hidden = ((no->is_function() | |
1228 | && no->func_value()->enclosing() != NULL) | |
1229 | || Gogo::is_thunk(no)); | |
1230 | bvar = context->backend()->immutable_struct(var_name, is_hidden, false, | |
1231 | btype, bloc); | |
1232 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1233 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1234 | Expression* init = |
1235 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1236 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1237 | bcontext.set_is_const(); | |
ea664253 | 1238 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1239 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1240 | false, btype, bloc, binit); | |
1241 | } | |
1242 | ||
1243 | this->dvar_ = bvar; | |
ea664253 | 1244 | return gogo->backend()->var_expression(bvar, loc); |
8381eda7 | 1245 | } |
1246 | ||
c6837989 | 1247 | // Print a function descriptor expression. |
1248 | ||
1249 | void | |
1250 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1251 | { | |
1252 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1253 | } | |
1254 | ||
8381eda7 | 1255 | // Make a function descriptor expression. |
1256 | ||
c6837989 | 1257 | Func_descriptor_expression* |
1258 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1259 | { |
c6837989 | 1260 | return new Func_descriptor_expression(fn); |
8381eda7 | 1261 | } |
1262 | ||
1263 | // Make the function descriptor type, so that it can be converted. | |
1264 | ||
1265 | void | |
1266 | Expression::make_func_descriptor_type() | |
1267 | { | |
1268 | Func_descriptor_expression::make_func_descriptor_type(); | |
1269 | } | |
1270 | ||
1271 | // A reference to just the code of a function. | |
1272 | ||
1273 | class Func_code_reference_expression : public Expression | |
1274 | { | |
1275 | public: | |
1276 | Func_code_reference_expression(Named_object* function, Location location) | |
1277 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1278 | function_(function) | |
1279 | { } | |
1280 | ||
1281 | protected: | |
1282 | int | |
1283 | do_traverse(Traverse*) | |
1284 | { return TRAVERSE_CONTINUE; } | |
1285 | ||
f9ca30f9 | 1286 | bool |
1287 | do_is_immutable() const | |
1288 | { return true; } | |
1289 | ||
8381eda7 | 1290 | Type* |
1291 | do_type() | |
1292 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1293 | ||
1294 | void | |
1295 | do_determine_type(const Type_context*) | |
1296 | { } | |
1297 | ||
1298 | Expression* | |
1299 | do_copy() | |
1300 | { | |
1301 | return Expression::make_func_code_reference(this->function_, | |
1302 | this->location()); | |
1303 | } | |
1304 | ||
ea664253 | 1305 | Bexpression* |
1306 | do_get_backend(Translate_context*); | |
8381eda7 | 1307 | |
1308 | void | |
1309 | do_dump_expression(Ast_dump_context* context) const | |
1310 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1311 | ||
1312 | private: | |
1313 | // The function. | |
1314 | Named_object* function_; | |
1315 | }; | |
1316 | ||
ea664253 | 1317 | // Get the backend representation for a reference to function code. |
8381eda7 | 1318 | |
ea664253 | 1319 | Bexpression* |
1320 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1321 | { |
ea664253 | 1322 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1323 | this->location()); | |
8381eda7 | 1324 | } |
1325 | ||
1326 | // Make a reference to the code of a function. | |
1327 | ||
1328 | Expression* | |
1329 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1330 | { | |
1331 | return new Func_code_reference_expression(function, location); | |
1332 | } | |
1333 | ||
e440a328 | 1334 | // Class Unknown_expression. |
1335 | ||
1336 | // Return the name of an unknown expression. | |
1337 | ||
1338 | const std::string& | |
1339 | Unknown_expression::name() const | |
1340 | { | |
1341 | return this->named_object_->name(); | |
1342 | } | |
1343 | ||
1344 | // Lower a reference to an unknown name. | |
1345 | ||
1346 | Expression* | |
ceeb4318 | 1347 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1348 | { |
b13c66cd | 1349 | Location location = this->location(); |
e440a328 | 1350 | Named_object* no = this->named_object_; |
deded542 | 1351 | Named_object* real; |
1352 | if (!no->is_unknown()) | |
1353 | real = no; | |
1354 | else | |
e440a328 | 1355 | { |
deded542 | 1356 | real = no->unknown_value()->real_named_object(); |
1357 | if (real == NULL) | |
1358 | { | |
1359 | if (this->is_composite_literal_key_) | |
1360 | return this; | |
acf8e158 | 1361 | if (!this->no_error_message_) |
1362 | error_at(location, "reference to undefined name %qs", | |
1363 | this->named_object_->message_name().c_str()); | |
deded542 | 1364 | return Expression::make_error(location); |
1365 | } | |
e440a328 | 1366 | } |
1367 | switch (real->classification()) | |
1368 | { | |
1369 | case Named_object::NAMED_OBJECT_CONST: | |
1370 | return Expression::make_const_reference(real, location); | |
1371 | case Named_object::NAMED_OBJECT_TYPE: | |
1372 | return Expression::make_type(real->type_value(), location); | |
1373 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1374 | if (this->is_composite_literal_key_) | |
1375 | return this; | |
acf8e158 | 1376 | if (!this->no_error_message_) |
1377 | error_at(location, "reference to undefined type %qs", | |
1378 | real->message_name().c_str()); | |
e440a328 | 1379 | return Expression::make_error(location); |
1380 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1381 | real->var_value()->set_is_used(); |
e440a328 | 1382 | return Expression::make_var_reference(real, location); |
1383 | case Named_object::NAMED_OBJECT_FUNC: | |
1384 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1385 | return Expression::make_func_reference(real, NULL, location); | |
1386 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1387 | if (this->is_composite_literal_key_) | |
1388 | return this; | |
acf8e158 | 1389 | if (!this->no_error_message_) |
1390 | error_at(location, "unexpected reference to package"); | |
e440a328 | 1391 | return Expression::make_error(location); |
1392 | default: | |
c3e6f413 | 1393 | go_unreachable(); |
e440a328 | 1394 | } |
1395 | } | |
1396 | ||
d751bb78 | 1397 | // Dump the ast representation for an unknown expression to a dump context. |
1398 | ||
1399 | void | |
1400 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1401 | { | |
1402 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1403 | << ")"; | |
d751bb78 | 1404 | } |
1405 | ||
e440a328 | 1406 | // Make a reference to an unknown name. |
1407 | ||
acf8e158 | 1408 | Unknown_expression* |
b13c66cd | 1409 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1410 | { |
e440a328 | 1411 | return new Unknown_expression(no, location); |
1412 | } | |
1413 | ||
1414 | // A boolean expression. | |
1415 | ||
1416 | class Boolean_expression : public Expression | |
1417 | { | |
1418 | public: | |
b13c66cd | 1419 | Boolean_expression(bool val, Location location) |
e440a328 | 1420 | : Expression(EXPRESSION_BOOLEAN, location), |
1421 | val_(val), type_(NULL) | |
1422 | { } | |
1423 | ||
1424 | static Expression* | |
1425 | do_import(Import*); | |
1426 | ||
1427 | protected: | |
1428 | bool | |
1429 | do_is_constant() const | |
1430 | { return true; } | |
1431 | ||
0e168074 | 1432 | bool |
1433 | do_is_immutable() const | |
1434 | { return true; } | |
1435 | ||
e440a328 | 1436 | Type* |
1437 | do_type(); | |
1438 | ||
1439 | void | |
1440 | do_determine_type(const Type_context*); | |
1441 | ||
1442 | Expression* | |
1443 | do_copy() | |
1444 | { return this; } | |
1445 | ||
ea664253 | 1446 | Bexpression* |
1447 | do_get_backend(Translate_context* context) | |
1448 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1449 | |
1450 | void | |
1451 | do_export(Export* exp) const | |
1452 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1453 | ||
d751bb78 | 1454 | void |
1455 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1456 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1457 | ||
e440a328 | 1458 | private: |
1459 | // The constant. | |
1460 | bool val_; | |
1461 | // The type as determined by context. | |
1462 | Type* type_; | |
1463 | }; | |
1464 | ||
1465 | // Get the type. | |
1466 | ||
1467 | Type* | |
1468 | Boolean_expression::do_type() | |
1469 | { | |
1470 | if (this->type_ == NULL) | |
1471 | this->type_ = Type::make_boolean_type(); | |
1472 | return this->type_; | |
1473 | } | |
1474 | ||
1475 | // Set the type from the context. | |
1476 | ||
1477 | void | |
1478 | Boolean_expression::do_determine_type(const Type_context* context) | |
1479 | { | |
1480 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1481 | ; | |
1482 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1483 | this->type_ = context->type; | |
1484 | else if (!context->may_be_abstract) | |
1485 | this->type_ = Type::lookup_bool_type(); | |
1486 | } | |
1487 | ||
1488 | // Import a boolean constant. | |
1489 | ||
1490 | Expression* | |
1491 | Boolean_expression::do_import(Import* imp) | |
1492 | { | |
1493 | if (imp->peek_char() == 't') | |
1494 | { | |
1495 | imp->require_c_string("true"); | |
1496 | return Expression::make_boolean(true, imp->location()); | |
1497 | } | |
1498 | else | |
1499 | { | |
1500 | imp->require_c_string("false"); | |
1501 | return Expression::make_boolean(false, imp->location()); | |
1502 | } | |
1503 | } | |
1504 | ||
1505 | // Make a boolean expression. | |
1506 | ||
1507 | Expression* | |
b13c66cd | 1508 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1509 | { |
1510 | return new Boolean_expression(val, location); | |
1511 | } | |
1512 | ||
1513 | // Class String_expression. | |
1514 | ||
1515 | // Get the type. | |
1516 | ||
1517 | Type* | |
1518 | String_expression::do_type() | |
1519 | { | |
1520 | if (this->type_ == NULL) | |
1521 | this->type_ = Type::make_string_type(); | |
1522 | return this->type_; | |
1523 | } | |
1524 | ||
1525 | // Set the type from the context. | |
1526 | ||
1527 | void | |
1528 | String_expression::do_determine_type(const Type_context* context) | |
1529 | { | |
1530 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1531 | ; | |
1532 | else if (context->type != NULL && context->type->is_string_type()) | |
1533 | this->type_ = context->type; | |
1534 | else if (!context->may_be_abstract) | |
1535 | this->type_ = Type::lookup_string_type(); | |
1536 | } | |
1537 | ||
1538 | // Build a string constant. | |
1539 | ||
ea664253 | 1540 | Bexpression* |
1541 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1542 | { |
2c809f8f | 1543 | Gogo* gogo = context->gogo(); |
1544 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1545 | ||
1546 | Location loc = this->location(); | |
1547 | std::vector<Bexpression*> init(2); | |
1548 | Bexpression* str_cst = | |
1549 | gogo->backend()->string_constant_expression(this->val_); | |
1550 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1551 | ||
1552 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1553 | mpz_t lenval; | |
1554 | mpz_init_set_ui(lenval, this->val_.length()); | |
1555 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1556 | mpz_clear(lenval); | |
1557 | ||
ea664253 | 1558 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1559 | } |
1560 | ||
8b1c301d | 1561 | // Write string literal to string dump. |
e440a328 | 1562 | |
1563 | void | |
8b1c301d | 1564 | String_expression::export_string(String_dump* exp, |
1565 | const String_expression* str) | |
e440a328 | 1566 | { |
1567 | std::string s; | |
8b1c301d | 1568 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1569 | s += '"'; |
8b1c301d | 1570 | for (std::string::const_iterator p = str->val_.begin(); |
1571 | p != str->val_.end(); | |
e440a328 | 1572 | ++p) |
1573 | { | |
1574 | if (*p == '\\' || *p == '"') | |
1575 | { | |
1576 | s += '\\'; | |
1577 | s += *p; | |
1578 | } | |
1579 | else if (*p >= 0x20 && *p < 0x7f) | |
1580 | s += *p; | |
1581 | else if (*p == '\n') | |
1582 | s += "\\n"; | |
1583 | else if (*p == '\t') | |
1584 | s += "\\t"; | |
1585 | else | |
1586 | { | |
1587 | s += "\\x"; | |
1588 | unsigned char c = *p; | |
1589 | unsigned int dig = c >> 4; | |
1590 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1591 | dig = c & 0xf; | |
1592 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1593 | } | |
1594 | } | |
1595 | s += '"'; | |
1596 | exp->write_string(s); | |
1597 | } | |
1598 | ||
8b1c301d | 1599 | // Export a string expression. |
1600 | ||
1601 | void | |
1602 | String_expression::do_export(Export* exp) const | |
1603 | { | |
1604 | String_expression::export_string(exp, this); | |
1605 | } | |
1606 | ||
e440a328 | 1607 | // Import a string expression. |
1608 | ||
1609 | Expression* | |
1610 | String_expression::do_import(Import* imp) | |
1611 | { | |
1612 | imp->require_c_string("\""); | |
1613 | std::string val; | |
1614 | while (true) | |
1615 | { | |
1616 | int c = imp->get_char(); | |
1617 | if (c == '"' || c == -1) | |
1618 | break; | |
1619 | if (c != '\\') | |
1620 | val += static_cast<char>(c); | |
1621 | else | |
1622 | { | |
1623 | c = imp->get_char(); | |
1624 | if (c == '\\' || c == '"') | |
1625 | val += static_cast<char>(c); | |
1626 | else if (c == 'n') | |
1627 | val += '\n'; | |
1628 | else if (c == 't') | |
1629 | val += '\t'; | |
1630 | else if (c == 'x') | |
1631 | { | |
1632 | c = imp->get_char(); | |
1633 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1634 | c = imp->get_char(); | |
1635 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1636 | char v = (vh << 4) | vl; | |
1637 | val += v; | |
1638 | } | |
1639 | else | |
1640 | { | |
1641 | error_at(imp->location(), "bad string constant"); | |
1642 | return Expression::make_error(imp->location()); | |
1643 | } | |
1644 | } | |
1645 | } | |
1646 | return Expression::make_string(val, imp->location()); | |
1647 | } | |
1648 | ||
d751bb78 | 1649 | // Ast dump for string expression. |
1650 | ||
1651 | void | |
1652 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1653 | { | |
8b1c301d | 1654 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1655 | } |
1656 | ||
e440a328 | 1657 | // Make a string expression. |
1658 | ||
1659 | Expression* | |
b13c66cd | 1660 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1661 | { |
1662 | return new String_expression(val, location); | |
1663 | } | |
1664 | ||
2c809f8f | 1665 | // An expression that evaluates to some characteristic of a string. |
1666 | // This is used when indexing, bound-checking, or nil checking a string. | |
1667 | ||
1668 | class String_info_expression : public Expression | |
1669 | { | |
1670 | public: | |
1671 | String_info_expression(Expression* string, String_info string_info, | |
1672 | Location location) | |
1673 | : Expression(EXPRESSION_STRING_INFO, location), | |
1674 | string_(string), string_info_(string_info) | |
1675 | { } | |
1676 | ||
1677 | protected: | |
1678 | Type* | |
1679 | do_type(); | |
1680 | ||
1681 | void | |
1682 | do_determine_type(const Type_context*) | |
1683 | { go_unreachable(); } | |
1684 | ||
1685 | Expression* | |
1686 | do_copy() | |
1687 | { | |
1688 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1689 | this->location()); | |
1690 | } | |
1691 | ||
ea664253 | 1692 | Bexpression* |
1693 | do_get_backend(Translate_context* context); | |
2c809f8f | 1694 | |
1695 | void | |
1696 | do_dump_expression(Ast_dump_context*) const; | |
1697 | ||
1698 | void | |
1699 | do_issue_nil_check() | |
1700 | { this->string_->issue_nil_check(); } | |
1701 | ||
1702 | private: | |
1703 | // The string for which we are getting information. | |
1704 | Expression* string_; | |
1705 | // What information we want. | |
1706 | String_info string_info_; | |
1707 | }; | |
1708 | ||
1709 | // Return the type of the string info. | |
1710 | ||
1711 | Type* | |
1712 | String_info_expression::do_type() | |
1713 | { | |
1714 | switch (this->string_info_) | |
1715 | { | |
1716 | case STRING_INFO_DATA: | |
1717 | { | |
1718 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1719 | return Type::make_pointer_type(byte_type); | |
1720 | } | |
1721 | case STRING_INFO_LENGTH: | |
1722 | return Type::lookup_integer_type("int"); | |
1723 | default: | |
1724 | go_unreachable(); | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | // Return string information in GENERIC. | |
1729 | ||
ea664253 | 1730 | Bexpression* |
1731 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1732 | { |
1733 | Gogo* gogo = context->gogo(); | |
1734 | ||
ea664253 | 1735 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1736 | switch (this->string_info_) |
1737 | { | |
1738 | case STRING_INFO_DATA: | |
1739 | case STRING_INFO_LENGTH: | |
ea664253 | 1740 | return gogo->backend()->struct_field_expression(bstring, |
1741 | this->string_info_, | |
1742 | this->location()); | |
2c809f8f | 1743 | break; |
1744 | default: | |
1745 | go_unreachable(); | |
1746 | } | |
2c809f8f | 1747 | } |
1748 | ||
1749 | // Dump ast representation for a type info expression. | |
1750 | ||
1751 | void | |
1752 | String_info_expression::do_dump_expression( | |
1753 | Ast_dump_context* ast_dump_context) const | |
1754 | { | |
1755 | ast_dump_context->ostream() << "stringinfo("; | |
1756 | this->string_->dump_expression(ast_dump_context); | |
1757 | ast_dump_context->ostream() << ","; | |
1758 | ast_dump_context->ostream() << | |
1759 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1760 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1761 | : "unknown"); | |
1762 | ast_dump_context->ostream() << ")"; | |
1763 | } | |
1764 | ||
1765 | // Make a string info expression. | |
1766 | ||
1767 | Expression* | |
1768 | Expression::make_string_info(Expression* string, String_info string_info, | |
1769 | Location location) | |
1770 | { | |
1771 | return new String_info_expression(string, string_info, location); | |
1772 | } | |
1773 | ||
e440a328 | 1774 | // Make an integer expression. |
1775 | ||
1776 | class Integer_expression : public Expression | |
1777 | { | |
1778 | public: | |
5d4b8566 | 1779 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1780 | Location location) | |
e440a328 | 1781 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1782 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1783 | { mpz_init_set(this->val_, *val); } |
1784 | ||
1785 | static Expression* | |
1786 | do_import(Import*); | |
1787 | ||
8b1c301d | 1788 | // Write VAL to string dump. |
e440a328 | 1789 | static void |
8b1c301d | 1790 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1791 | |
d751bb78 | 1792 | // Write VAL to dump context. |
1793 | static void | |
1794 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1795 | ||
e440a328 | 1796 | protected: |
1797 | bool | |
1798 | do_is_constant() const | |
1799 | { return true; } | |
1800 | ||
0e168074 | 1801 | bool |
1802 | do_is_immutable() const | |
1803 | { return true; } | |
1804 | ||
e440a328 | 1805 | bool |
0c77715b | 1806 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1807 | |
1808 | Type* | |
1809 | do_type(); | |
1810 | ||
1811 | void | |
1812 | do_determine_type(const Type_context* context); | |
1813 | ||
1814 | void | |
1815 | do_check_types(Gogo*); | |
1816 | ||
ea664253 | 1817 | Bexpression* |
1818 | do_get_backend(Translate_context*); | |
e440a328 | 1819 | |
1820 | Expression* | |
1821 | do_copy() | |
5d4b8566 | 1822 | { |
1823 | if (this->is_character_constant_) | |
1824 | return Expression::make_character(&this->val_, this->type_, | |
1825 | this->location()); | |
1826 | else | |
1827 | return Expression::make_integer(&this->val_, this->type_, | |
1828 | this->location()); | |
1829 | } | |
e440a328 | 1830 | |
1831 | void | |
1832 | do_export(Export*) const; | |
1833 | ||
d751bb78 | 1834 | void |
1835 | do_dump_expression(Ast_dump_context*) const; | |
1836 | ||
e440a328 | 1837 | private: |
1838 | // The integer value. | |
1839 | mpz_t val_; | |
1840 | // The type so far. | |
1841 | Type* type_; | |
5d4b8566 | 1842 | // Whether this is a character constant. |
1843 | bool is_character_constant_; | |
e440a328 | 1844 | }; |
1845 | ||
0c77715b | 1846 | // Return a numeric constant for this expression. We have to mark |
1847 | // this as a character when appropriate. | |
e440a328 | 1848 | |
1849 | bool | |
0c77715b | 1850 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1851 | { |
0c77715b | 1852 | if (this->is_character_constant_) |
1853 | nc->set_rune(this->type_, this->val_); | |
1854 | else | |
1855 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1856 | return true; |
1857 | } | |
1858 | ||
1859 | // Return the current type. If we haven't set the type yet, we return | |
1860 | // an abstract integer type. | |
1861 | ||
1862 | Type* | |
1863 | Integer_expression::do_type() | |
1864 | { | |
1865 | if (this->type_ == NULL) | |
5d4b8566 | 1866 | { |
1867 | if (this->is_character_constant_) | |
1868 | this->type_ = Type::make_abstract_character_type(); | |
1869 | else | |
1870 | this->type_ = Type::make_abstract_integer_type(); | |
1871 | } | |
e440a328 | 1872 | return this->type_; |
1873 | } | |
1874 | ||
1875 | // Set the type of the integer value. Here we may switch from an | |
1876 | // abstract type to a real type. | |
1877 | ||
1878 | void | |
1879 | Integer_expression::do_determine_type(const Type_context* context) | |
1880 | { | |
1881 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1882 | ; | |
0c77715b | 1883 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1884 | this->type_ = context->type; |
1885 | else if (!context->may_be_abstract) | |
5d4b8566 | 1886 | { |
1887 | if (this->is_character_constant_) | |
1888 | this->type_ = Type::lookup_integer_type("int32"); | |
1889 | else | |
1890 | this->type_ = Type::lookup_integer_type("int"); | |
1891 | } | |
e440a328 | 1892 | } |
1893 | ||
e440a328 | 1894 | // Check the type of an integer constant. |
1895 | ||
1896 | void | |
1897 | Integer_expression::do_check_types(Gogo*) | |
1898 | { | |
0c77715b | 1899 | Type* type = this->type_; |
1900 | if (type == NULL) | |
e440a328 | 1901 | return; |
0c77715b | 1902 | Numeric_constant nc; |
1903 | if (this->is_character_constant_) | |
1904 | nc.set_rune(NULL, this->val_); | |
1905 | else | |
1906 | nc.set_int(NULL, this->val_); | |
1907 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1908 | this->set_is_error(); |
1909 | } | |
1910 | ||
ea664253 | 1911 | // Get the backend representation for an integer constant. |
e440a328 | 1912 | |
ea664253 | 1913 | Bexpression* |
1914 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1915 | { |
48c2a53a | 1916 | Type* resolved_type = NULL; |
e440a328 | 1917 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 1918 | resolved_type = this->type_; |
e440a328 | 1919 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
1920 | { | |
1921 | // We are converting to an abstract floating point type. | |
48c2a53a | 1922 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 1923 | } |
1924 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1925 | { | |
1926 | // We are converting to an abstract complex type. | |
48c2a53a | 1927 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 1928 | } |
1929 | else | |
1930 | { | |
1931 | // If we still have an abstract type here, then this is being | |
1932 | // used in a constant expression which didn't get reduced for | |
1933 | // some reason. Use a type which will fit the value. We use <, | |
1934 | // not <=, because we need an extra bit for the sign bit. | |
1935 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 1936 | Type* int_type = Type::lookup_integer_type("int"); |
1937 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 1938 | resolved_type = int_type; |
e440a328 | 1939 | else if (bits < 64) |
48c2a53a | 1940 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 1941 | else |
48c2a53a | 1942 | { |
1943 | if (!saw_errors()) | |
1944 | error_at(this->location(), | |
1945 | "unknown type for large integer constant"); | |
ea664253 | 1946 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 1947 | } |
e440a328 | 1948 | } |
48c2a53a | 1949 | Numeric_constant nc; |
1950 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 1951 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 1952 | } |
1953 | ||
1954 | // Write VAL to export data. | |
1955 | ||
1956 | void | |
8b1c301d | 1957 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 1958 | { |
1959 | char* s = mpz_get_str(NULL, 10, val); | |
1960 | exp->write_c_string(s); | |
1961 | free(s); | |
1962 | } | |
1963 | ||
1964 | // Export an integer in a constant expression. | |
1965 | ||
1966 | void | |
1967 | Integer_expression::do_export(Export* exp) const | |
1968 | { | |
1969 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 1970 | if (this->is_character_constant_) |
1971 | exp->write_c_string("'"); | |
e440a328 | 1972 | // A trailing space lets us reliably identify the end of the number. |
1973 | exp->write_c_string(" "); | |
1974 | } | |
1975 | ||
1976 | // Import an integer, floating point, or complex value. This handles | |
1977 | // all these types because they all start with digits. | |
1978 | ||
1979 | Expression* | |
1980 | Integer_expression::do_import(Import* imp) | |
1981 | { | |
1982 | std::string num = imp->read_identifier(); | |
1983 | imp->require_c_string(" "); | |
1984 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1985 | { | |
1986 | mpfr_t real; | |
1987 | size_t plus_pos = num.find('+', 1); | |
1988 | size_t minus_pos = num.find('-', 1); | |
1989 | size_t pos; | |
1990 | if (plus_pos == std::string::npos) | |
1991 | pos = minus_pos; | |
1992 | else if (minus_pos == std::string::npos) | |
1993 | pos = plus_pos; | |
1994 | else | |
1995 | { | |
1996 | error_at(imp->location(), "bad number in import data: %qs", | |
1997 | num.c_str()); | |
1998 | return Expression::make_error(imp->location()); | |
1999 | } | |
2000 | if (pos == std::string::npos) | |
2001 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2002 | else | |
2003 | { | |
2004 | std::string real_str = num.substr(0, pos); | |
2005 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2006 | { | |
2007 | error_at(imp->location(), "bad number in import data: %qs", | |
2008 | real_str.c_str()); | |
2009 | return Expression::make_error(imp->location()); | |
2010 | } | |
2011 | } | |
2012 | ||
2013 | std::string imag_str; | |
2014 | if (pos == std::string::npos) | |
2015 | imag_str = num; | |
2016 | else | |
2017 | imag_str = num.substr(pos); | |
2018 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2019 | mpfr_t imag; | |
2020 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2021 | { | |
2022 | error_at(imp->location(), "bad number in import data: %qs", | |
2023 | imag_str.c_str()); | |
2024 | return Expression::make_error(imp->location()); | |
2025 | } | |
2026 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
2027 | imp->location()); | |
2028 | mpfr_clear(real); | |
2029 | mpfr_clear(imag); | |
2030 | return ret; | |
2031 | } | |
2032 | else if (num.find('.') == std::string::npos | |
2033 | && num.find('E') == std::string::npos) | |
2034 | { | |
5d4b8566 | 2035 | bool is_character_constant = (!num.empty() |
2036 | && num[num.length() - 1] == '\''); | |
2037 | if (is_character_constant) | |
2038 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2039 | mpz_t val; |
2040 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2041 | { | |
2042 | error_at(imp->location(), "bad number in import data: %qs", | |
2043 | num.c_str()); | |
2044 | return Expression::make_error(imp->location()); | |
2045 | } | |
5d4b8566 | 2046 | Expression* ret; |
2047 | if (is_character_constant) | |
2048 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2049 | else | |
2050 | ret = Expression::make_integer(&val, NULL, imp->location()); | |
e440a328 | 2051 | mpz_clear(val); |
2052 | return ret; | |
2053 | } | |
2054 | else | |
2055 | { | |
2056 | mpfr_t val; | |
2057 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2058 | { | |
2059 | error_at(imp->location(), "bad number in import data: %qs", | |
2060 | num.c_str()); | |
2061 | return Expression::make_error(imp->location()); | |
2062 | } | |
2063 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2064 | mpfr_clear(val); | |
2065 | return ret; | |
2066 | } | |
2067 | } | |
d751bb78 | 2068 | // Ast dump for integer expression. |
2069 | ||
2070 | void | |
2071 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2072 | { | |
5d4b8566 | 2073 | if (this->is_character_constant_) |
2074 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2075 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2076 | if (this->is_character_constant_) |
2077 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2078 | } |
2079 | ||
e440a328 | 2080 | // Build a new integer value. |
2081 | ||
2082 | Expression* | |
5d4b8566 | 2083 | Expression::make_integer(const mpz_t* val, Type* type, Location location) |
2084 | { | |
2085 | return new Integer_expression(val, type, false, location); | |
2086 | } | |
2087 | ||
2088 | // Build a new character constant value. | |
2089 | ||
2090 | Expression* | |
2091 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2092 | { |
5d4b8566 | 2093 | return new Integer_expression(val, type, true, location); |
e440a328 | 2094 | } |
2095 | ||
2096 | // Floats. | |
2097 | ||
2098 | class Float_expression : public Expression | |
2099 | { | |
2100 | public: | |
b13c66cd | 2101 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2102 | : Expression(EXPRESSION_FLOAT, location), |
2103 | type_(type) | |
2104 | { | |
2105 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2106 | } | |
2107 | ||
e440a328 | 2108 | // Write VAL to export data. |
2109 | static void | |
8b1c301d | 2110 | export_float(String_dump* exp, const mpfr_t val); |
2111 | ||
d751bb78 | 2112 | // Write VAL to dump file. |
2113 | static void | |
2114 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2115 | |
2116 | protected: | |
2117 | bool | |
2118 | do_is_constant() const | |
2119 | { return true; } | |
2120 | ||
0e168074 | 2121 | bool |
2122 | do_is_immutable() const | |
2123 | { return true; } | |
2124 | ||
e440a328 | 2125 | bool |
0c77715b | 2126 | do_numeric_constant_value(Numeric_constant* nc) const |
2127 | { | |
2128 | nc->set_float(this->type_, this->val_); | |
2129 | return true; | |
2130 | } | |
e440a328 | 2131 | |
2132 | Type* | |
2133 | do_type(); | |
2134 | ||
2135 | void | |
2136 | do_determine_type(const Type_context*); | |
2137 | ||
2138 | void | |
2139 | do_check_types(Gogo*); | |
2140 | ||
2141 | Expression* | |
2142 | do_copy() | |
2143 | { return Expression::make_float(&this->val_, this->type_, | |
2144 | this->location()); } | |
2145 | ||
ea664253 | 2146 | Bexpression* |
2147 | do_get_backend(Translate_context*); | |
e440a328 | 2148 | |
2149 | void | |
2150 | do_export(Export*) const; | |
2151 | ||
d751bb78 | 2152 | void |
2153 | do_dump_expression(Ast_dump_context*) const; | |
2154 | ||
e440a328 | 2155 | private: |
2156 | // The floating point value. | |
2157 | mpfr_t val_; | |
2158 | // The type so far. | |
2159 | Type* type_; | |
2160 | }; | |
2161 | ||
e440a328 | 2162 | // Return the current type. If we haven't set the type yet, we return |
2163 | // an abstract float type. | |
2164 | ||
2165 | Type* | |
2166 | Float_expression::do_type() | |
2167 | { | |
2168 | if (this->type_ == NULL) | |
2169 | this->type_ = Type::make_abstract_float_type(); | |
2170 | return this->type_; | |
2171 | } | |
2172 | ||
2173 | // Set the type of the float value. Here we may switch from an | |
2174 | // abstract type to a real type. | |
2175 | ||
2176 | void | |
2177 | Float_expression::do_determine_type(const Type_context* context) | |
2178 | { | |
2179 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2180 | ; | |
2181 | else if (context->type != NULL | |
2182 | && (context->type->integer_type() != NULL | |
2183 | || context->type->float_type() != NULL | |
2184 | || context->type->complex_type() != NULL)) | |
2185 | this->type_ = context->type; | |
2186 | else if (!context->may_be_abstract) | |
48080209 | 2187 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2188 | } |
2189 | ||
e440a328 | 2190 | // Check the type of a float value. |
2191 | ||
2192 | void | |
2193 | Float_expression::do_check_types(Gogo*) | |
2194 | { | |
0c77715b | 2195 | Type* type = this->type_; |
2196 | if (type == NULL) | |
e440a328 | 2197 | return; |
0c77715b | 2198 | Numeric_constant nc; |
2199 | nc.set_float(NULL, this->val_); | |
2200 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2201 | this->set_is_error(); |
e440a328 | 2202 | } |
2203 | ||
ea664253 | 2204 | // Get the backend representation for a float constant. |
e440a328 | 2205 | |
ea664253 | 2206 | Bexpression* |
2207 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2208 | { |
48c2a53a | 2209 | Type* resolved_type; |
e440a328 | 2210 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2211 | resolved_type = this->type_; |
e440a328 | 2212 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2213 | { | |
2214 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2215 | resolved_type = Type::lookup_integer_type("int"); |
2216 | } | |
2217 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2218 | { | |
2219 | // We are converting to an abstract complex type. | |
2220 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2221 | } |
2222 | else | |
2223 | { | |
2224 | // If we still have an abstract type here, then this is being | |
2225 | // used in a constant expression which didn't get reduced. We | |
2226 | // just use float64 and hope for the best. | |
48c2a53a | 2227 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2228 | } |
48c2a53a | 2229 | |
2230 | Numeric_constant nc; | |
2231 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2232 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2233 | } |
2234 | ||
8b1c301d | 2235 | // Write a floating point number to a string dump. |
e440a328 | 2236 | |
2237 | void | |
8b1c301d | 2238 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2239 | { |
2240 | mp_exp_t exponent; | |
2241 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2242 | if (*s == '-') | |
2243 | exp->write_c_string("-"); | |
2244 | exp->write_c_string("0."); | |
2245 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2246 | mpfr_free_str(s); | |
2247 | char buf[30]; | |
2248 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2249 | exp->write_c_string(buf); | |
2250 | } | |
2251 | ||
2252 | // Export a floating point number in a constant expression. | |
2253 | ||
2254 | void | |
2255 | Float_expression::do_export(Export* exp) const | |
2256 | { | |
2257 | Float_expression::export_float(exp, this->val_); | |
2258 | // A trailing space lets us reliably identify the end of the number. | |
2259 | exp->write_c_string(" "); | |
2260 | } | |
2261 | ||
d751bb78 | 2262 | // Dump a floating point number to the dump file. |
2263 | ||
2264 | void | |
2265 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2266 | { | |
8b1c301d | 2267 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2268 | } |
2269 | ||
e440a328 | 2270 | // Make a float expression. |
2271 | ||
2272 | Expression* | |
b13c66cd | 2273 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2274 | { |
2275 | return new Float_expression(val, type, location); | |
2276 | } | |
2277 | ||
2278 | // Complex numbers. | |
2279 | ||
2280 | class Complex_expression : public Expression | |
2281 | { | |
2282 | public: | |
2283 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2284 | Location location) |
e440a328 | 2285 | : Expression(EXPRESSION_COMPLEX, location), |
2286 | type_(type) | |
2287 | { | |
2288 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2289 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2290 | } | |
2291 | ||
8b1c301d | 2292 | // Write REAL/IMAG to string dump. |
e440a328 | 2293 | static void |
8b1c301d | 2294 | export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val); |
e440a328 | 2295 | |
d751bb78 | 2296 | // Write REAL/IMAG to dump context. |
2297 | static void | |
2298 | dump_complex(Ast_dump_context* ast_dump_context, | |
2299 | const mpfr_t real, const mpfr_t val); | |
2300 | ||
e440a328 | 2301 | protected: |
2302 | bool | |
2303 | do_is_constant() const | |
2304 | { return true; } | |
2305 | ||
0e168074 | 2306 | bool |
2307 | do_is_immutable() const | |
2308 | { return true; } | |
2309 | ||
e440a328 | 2310 | bool |
0c77715b | 2311 | do_numeric_constant_value(Numeric_constant* nc) const |
2312 | { | |
2313 | nc->set_complex(this->type_, this->real_, this->imag_); | |
2314 | return true; | |
2315 | } | |
e440a328 | 2316 | |
2317 | Type* | |
2318 | do_type(); | |
2319 | ||
2320 | void | |
2321 | do_determine_type(const Type_context*); | |
2322 | ||
2323 | void | |
2324 | do_check_types(Gogo*); | |
2325 | ||
2326 | Expression* | |
2327 | do_copy() | |
2328 | { | |
2329 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2330 | this->location()); | |
2331 | } | |
2332 | ||
ea664253 | 2333 | Bexpression* |
2334 | do_get_backend(Translate_context*); | |
e440a328 | 2335 | |
2336 | void | |
2337 | do_export(Export*) const; | |
2338 | ||
d751bb78 | 2339 | void |
2340 | do_dump_expression(Ast_dump_context*) const; | |
2341 | ||
e440a328 | 2342 | private: |
2343 | // The real part. | |
2344 | mpfr_t real_; | |
2345 | // The imaginary part; | |
2346 | mpfr_t imag_; | |
2347 | // The type if known. | |
2348 | Type* type_; | |
2349 | }; | |
2350 | ||
e440a328 | 2351 | // Return the current type. If we haven't set the type yet, we return |
2352 | // an abstract complex type. | |
2353 | ||
2354 | Type* | |
2355 | Complex_expression::do_type() | |
2356 | { | |
2357 | if (this->type_ == NULL) | |
2358 | this->type_ = Type::make_abstract_complex_type(); | |
2359 | return this->type_; | |
2360 | } | |
2361 | ||
2362 | // Set the type of the complex value. Here we may switch from an | |
2363 | // abstract type to a real type. | |
2364 | ||
2365 | void | |
2366 | Complex_expression::do_determine_type(const Type_context* context) | |
2367 | { | |
2368 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2369 | ; | |
2370 | else if (context->type != NULL | |
2371 | && context->type->complex_type() != NULL) | |
2372 | this->type_ = context->type; | |
2373 | else if (!context->may_be_abstract) | |
48080209 | 2374 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2375 | } |
2376 | ||
e440a328 | 2377 | // Check the type of a complex value. |
2378 | ||
2379 | void | |
2380 | Complex_expression::do_check_types(Gogo*) | |
2381 | { | |
0c77715b | 2382 | Type* type = this->type_; |
2383 | if (type == NULL) | |
e440a328 | 2384 | return; |
0c77715b | 2385 | Numeric_constant nc; |
2386 | nc.set_complex(NULL, this->real_, this->imag_); | |
2387 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2388 | this->set_is_error(); |
2389 | } | |
2390 | ||
ea664253 | 2391 | // Get the backend representation for a complex constant. |
e440a328 | 2392 | |
ea664253 | 2393 | Bexpression* |
2394 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2395 | { |
48c2a53a | 2396 | Type* resolved_type; |
e440a328 | 2397 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2398 | resolved_type = this->type_; |
2399 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2400 | { | |
2401 | // We are converting to an abstract integer type. | |
2402 | resolved_type = Type::lookup_integer_type("int"); | |
2403 | } | |
2404 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2405 | { | |
2406 | // We are converting to an abstract float type. | |
2407 | resolved_type = Type::lookup_float_type("float64"); | |
2408 | } | |
e440a328 | 2409 | else |
2410 | { | |
2411 | // If we still have an abstract type here, this this is being | |
2412 | // used in a constant expression which didn't get reduced. We | |
2413 | // just use complex128 and hope for the best. | |
48c2a53a | 2414 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2415 | } |
48c2a53a | 2416 | |
2417 | Numeric_constant nc; | |
2418 | nc.set_complex(resolved_type, this->real_, this->imag_); | |
ea664253 | 2419 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2420 | } |
2421 | ||
2422 | // Write REAL/IMAG to export data. | |
2423 | ||
2424 | void | |
8b1c301d | 2425 | Complex_expression::export_complex(String_dump* exp, const mpfr_t real, |
e440a328 | 2426 | const mpfr_t imag) |
2427 | { | |
2428 | if (!mpfr_zero_p(real)) | |
2429 | { | |
2430 | Float_expression::export_float(exp, real); | |
2431 | if (mpfr_sgn(imag) > 0) | |
2432 | exp->write_c_string("+"); | |
2433 | } | |
2434 | Float_expression::export_float(exp, imag); | |
2435 | exp->write_c_string("i"); | |
2436 | } | |
2437 | ||
2438 | // Export a complex number in a constant expression. | |
2439 | ||
2440 | void | |
2441 | Complex_expression::do_export(Export* exp) const | |
2442 | { | |
2443 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2444 | // A trailing space lets us reliably identify the end of the number. | |
2445 | exp->write_c_string(" "); | |
2446 | } | |
2447 | ||
d751bb78 | 2448 | // Dump a complex expression to the dump file. |
2449 | ||
2450 | void | |
2451 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2452 | { | |
8b1c301d | 2453 | Complex_expression::export_complex(ast_dump_context, |
d751bb78 | 2454 | this->real_, |
2455 | this->imag_); | |
2456 | } | |
2457 | ||
e440a328 | 2458 | // Make a complex expression. |
2459 | ||
2460 | Expression* | |
2461 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2462 | Location location) |
e440a328 | 2463 | { |
2464 | return new Complex_expression(real, imag, type, location); | |
2465 | } | |
2466 | ||
d5b605df | 2467 | // Find a named object in an expression. |
2468 | ||
2469 | class Find_named_object : public Traverse | |
2470 | { | |
2471 | public: | |
2472 | Find_named_object(Named_object* no) | |
2473 | : Traverse(traverse_expressions), | |
2474 | no_(no), found_(false) | |
2475 | { } | |
2476 | ||
2477 | // Whether we found the object. | |
2478 | bool | |
2479 | found() const | |
2480 | { return this->found_; } | |
2481 | ||
2482 | protected: | |
2483 | int | |
2484 | expression(Expression**); | |
2485 | ||
2486 | private: | |
2487 | // The object we are looking for. | |
2488 | Named_object* no_; | |
2489 | // Whether we found it. | |
2490 | bool found_; | |
2491 | }; | |
2492 | ||
e440a328 | 2493 | // A reference to a const in an expression. |
2494 | ||
2495 | class Const_expression : public Expression | |
2496 | { | |
2497 | public: | |
b13c66cd | 2498 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2499 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2500 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2501 | { } |
2502 | ||
d5b605df | 2503 | Named_object* |
2504 | named_object() | |
2505 | { return this->constant_; } | |
2506 | ||
a7f064d5 | 2507 | // Check that the initializer does not refer to the constant itself. |
2508 | void | |
2509 | check_for_init_loop(); | |
2510 | ||
e440a328 | 2511 | protected: |
ba4aedd4 | 2512 | int |
2513 | do_traverse(Traverse*); | |
2514 | ||
e440a328 | 2515 | Expression* |
ceeb4318 | 2516 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2517 | |
2518 | bool | |
2519 | do_is_constant() const | |
2520 | { return true; } | |
2521 | ||
0e168074 | 2522 | bool |
2523 | do_is_immutable() const | |
2524 | { return true; } | |
2525 | ||
e440a328 | 2526 | bool |
0c77715b | 2527 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2528 | |
2529 | bool | |
af6b489a | 2530 | do_string_constant_value(std::string* val) const; |
e440a328 | 2531 | |
2532 | Type* | |
2533 | do_type(); | |
2534 | ||
2535 | // The type of a const is set by the declaration, not the use. | |
2536 | void | |
2537 | do_determine_type(const Type_context*); | |
2538 | ||
2539 | void | |
2540 | do_check_types(Gogo*); | |
2541 | ||
2542 | Expression* | |
2543 | do_copy() | |
2544 | { return this; } | |
2545 | ||
ea664253 | 2546 | Bexpression* |
2547 | do_get_backend(Translate_context* context); | |
e440a328 | 2548 | |
2549 | // When exporting a reference to a const as part of a const | |
2550 | // expression, we export the value. We ignore the fact that it has | |
2551 | // a name. | |
2552 | void | |
2553 | do_export(Export* exp) const | |
2554 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2555 | ||
d751bb78 | 2556 | void |
2557 | do_dump_expression(Ast_dump_context*) const; | |
2558 | ||
e440a328 | 2559 | private: |
2560 | // The constant. | |
2561 | Named_object* constant_; | |
2562 | // The type of this reference. This is used if the constant has an | |
2563 | // abstract type. | |
2564 | Type* type_; | |
13e818f5 | 2565 | // Used to prevent infinite recursion when a constant incorrectly |
2566 | // refers to itself. | |
2567 | mutable bool seen_; | |
e440a328 | 2568 | }; |
2569 | ||
ba4aedd4 | 2570 | // Traversal. |
2571 | ||
2572 | int | |
2573 | Const_expression::do_traverse(Traverse* traverse) | |
2574 | { | |
2575 | if (this->type_ != NULL) | |
2576 | return Type::traverse(this->type_, traverse); | |
2577 | return TRAVERSE_CONTINUE; | |
2578 | } | |
2579 | ||
e440a328 | 2580 | // Lower a constant expression. This is where we convert the |
2581 | // predeclared constant iota into an integer value. | |
2582 | ||
2583 | Expression* | |
ceeb4318 | 2584 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2585 | Statement_inserter*, int iota_value) | |
e440a328 | 2586 | { |
2587 | if (this->constant_->const_value()->expr()->classification() | |
2588 | == EXPRESSION_IOTA) | |
2589 | { | |
2590 | if (iota_value == -1) | |
2591 | { | |
2592 | error_at(this->location(), | |
2593 | "iota is only defined in const declarations"); | |
2594 | iota_value = 0; | |
2595 | } | |
2596 | mpz_t val; | |
2597 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2598 | Expression* ret = Expression::make_integer(&val, NULL, | |
2599 | this->location()); | |
2600 | mpz_clear(val); | |
2601 | return ret; | |
2602 | } | |
2603 | ||
2604 | // Make sure that the constant itself has been lowered. | |
2605 | gogo->lower_constant(this->constant_); | |
2606 | ||
2607 | return this; | |
2608 | } | |
2609 | ||
0c77715b | 2610 | // Return a numeric constant value. |
e440a328 | 2611 | |
2612 | bool | |
0c77715b | 2613 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2614 | { |
13e818f5 | 2615 | if (this->seen_) |
2616 | return false; | |
2617 | ||
e440a328 | 2618 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2619 | |
13e818f5 | 2620 | this->seen_ = true; |
2621 | ||
0c77715b | 2622 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2623 | |
13e818f5 | 2624 | this->seen_ = false; |
2625 | ||
e440a328 | 2626 | Type* ctype; |
2627 | if (this->type_ != NULL) | |
2628 | ctype = this->type_; | |
2629 | else | |
2630 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2631 | if (r && ctype != NULL) |
2632 | { | |
0c77715b | 2633 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2634 | return false; |
e440a328 | 2635 | } |
e440a328 | 2636 | |
e440a328 | 2637 | return r; |
2638 | } | |
2639 | ||
af6b489a | 2640 | bool |
2641 | Const_expression::do_string_constant_value(std::string* val) const | |
2642 | { | |
2643 | if (this->seen_) | |
2644 | return false; | |
2645 | ||
2646 | Expression* e = this->constant_->const_value()->expr(); | |
2647 | ||
2648 | this->seen_ = true; | |
2649 | bool ok = e->string_constant_value(val); | |
2650 | this->seen_ = false; | |
2651 | ||
2652 | return ok; | |
2653 | } | |
2654 | ||
e440a328 | 2655 | // Return the type of the const reference. |
2656 | ||
2657 | Type* | |
2658 | Const_expression::do_type() | |
2659 | { | |
2660 | if (this->type_ != NULL) | |
2661 | return this->type_; | |
13e818f5 | 2662 | |
2f78f012 | 2663 | Named_constant* nc = this->constant_->const_value(); |
2664 | ||
2665 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2666 | { |
2667 | this->report_error(_("constant refers to itself")); | |
2668 | this->type_ = Type::make_error_type(); | |
2669 | return this->type_; | |
2670 | } | |
2671 | ||
2672 | this->seen_ = true; | |
2673 | ||
e440a328 | 2674 | Type* ret = nc->type(); |
13e818f5 | 2675 | |
e440a328 | 2676 | if (ret != NULL) |
13e818f5 | 2677 | { |
2678 | this->seen_ = false; | |
2679 | return ret; | |
2680 | } | |
2681 | ||
e440a328 | 2682 | // During parsing, a named constant may have a NULL type, but we |
2683 | // must not return a NULL type here. | |
13e818f5 | 2684 | ret = nc->expr()->type(); |
2685 | ||
2686 | this->seen_ = false; | |
2687 | ||
2688 | return ret; | |
e440a328 | 2689 | } |
2690 | ||
2691 | // Set the type of the const reference. | |
2692 | ||
2693 | void | |
2694 | Const_expression::do_determine_type(const Type_context* context) | |
2695 | { | |
2696 | Type* ctype = this->constant_->const_value()->type(); | |
2697 | Type* cetype = (ctype != NULL | |
2698 | ? ctype | |
2699 | : this->constant_->const_value()->expr()->type()); | |
2700 | if (ctype != NULL && !ctype->is_abstract()) | |
2701 | ; | |
2702 | else if (context->type != NULL | |
0c77715b | 2703 | && context->type->is_numeric_type() |
2704 | && cetype->is_numeric_type()) | |
e440a328 | 2705 | this->type_ = context->type; |
2706 | else if (context->type != NULL | |
2707 | && context->type->is_string_type() | |
2708 | && cetype->is_string_type()) | |
2709 | this->type_ = context->type; | |
2710 | else if (context->type != NULL | |
2711 | && context->type->is_boolean_type() | |
2712 | && cetype->is_boolean_type()) | |
2713 | this->type_ = context->type; | |
2714 | else if (!context->may_be_abstract) | |
2715 | { | |
2716 | if (cetype->is_abstract()) | |
2717 | cetype = cetype->make_non_abstract_type(); | |
2718 | this->type_ = cetype; | |
2719 | } | |
2720 | } | |
2721 | ||
a7f064d5 | 2722 | // Check for a loop in which the initializer of a constant refers to |
2723 | // the constant itself. | |
e440a328 | 2724 | |
2725 | void | |
a7f064d5 | 2726 | Const_expression::check_for_init_loop() |
e440a328 | 2727 | { |
5c13bd80 | 2728 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2729 | return; |
2730 | ||
a7f064d5 | 2731 | if (this->seen_) |
2732 | { | |
2733 | this->report_error(_("constant refers to itself")); | |
2734 | this->type_ = Type::make_error_type(); | |
2735 | return; | |
2736 | } | |
2737 | ||
d5b605df | 2738 | Expression* init = this->constant_->const_value()->expr(); |
2739 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2740 | |
2741 | this->seen_ = true; | |
d5b605df | 2742 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2743 | this->seen_ = false; |
2744 | ||
d5b605df | 2745 | if (find_named_object.found()) |
2746 | { | |
5c13bd80 | 2747 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2748 | { |
2749 | this->report_error(_("constant refers to itself")); | |
2750 | this->type_ = Type::make_error_type(); | |
2751 | } | |
d5b605df | 2752 | return; |
2753 | } | |
a7f064d5 | 2754 | } |
2755 | ||
2756 | // Check types of a const reference. | |
2757 | ||
2758 | void | |
2759 | Const_expression::do_check_types(Gogo*) | |
2760 | { | |
5c13bd80 | 2761 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2762 | return; |
2763 | ||
2764 | this->check_for_init_loop(); | |
d5b605df | 2765 | |
0c77715b | 2766 | // Check that numeric constant fits in type. |
2767 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2768 | { |
0c77715b | 2769 | Numeric_constant nc; |
2770 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2771 | { |
0c77715b | 2772 | if (!nc.set_type(this->type_, true, this->location())) |
2773 | this->set_is_error(); | |
e440a328 | 2774 | } |
e440a328 | 2775 | } |
2776 | } | |
2777 | ||
ea664253 | 2778 | // Return the backend representation for a const reference. |
e440a328 | 2779 | |
ea664253 | 2780 | Bexpression* |
2781 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2782 | { |
2c809f8f | 2783 | if (this->type_ != NULL && this->type_->is_error()) |
ea664253 | 2784 | return context->backend()->error_expression(); |
e440a328 | 2785 | |
2786 | // If the type has been set for this expression, but the underlying | |
2787 | // object is an abstract int or float, we try to get the abstract | |
2788 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2789 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2790 | if (this->type_ != NULL |
0c77715b | 2791 | && this->type_->is_numeric_type() |
a68492b4 | 2792 | && (this->constant_->const_value()->type() == NULL |
2793 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2794 | { |
0c77715b | 2795 | Numeric_constant nc; |
2796 | if (expr->numeric_constant_value(&nc) | |
2797 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2798 | { |
0c77715b | 2799 | Expression* e = nc.expression(this->location()); |
ea664253 | 2800 | return e->get_backend(context); |
e440a328 | 2801 | } |
e440a328 | 2802 | } |
2803 | ||
2c809f8f | 2804 | if (this->type_ != NULL) |
f2de4532 | 2805 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 2806 | return expr->get_backend(context); |
e440a328 | 2807 | } |
2808 | ||
d751bb78 | 2809 | // Dump ast representation for constant expression. |
2810 | ||
2811 | void | |
2812 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2813 | { | |
2814 | ast_dump_context->ostream() << this->constant_->name(); | |
2815 | } | |
2816 | ||
e440a328 | 2817 | // Make a reference to a constant in an expression. |
2818 | ||
2819 | Expression* | |
2820 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2821 | Location location) |
e440a328 | 2822 | { |
2823 | return new Const_expression(constant, location); | |
2824 | } | |
2825 | ||
d5b605df | 2826 | // Find a named object in an expression. |
2827 | ||
2828 | int | |
2829 | Find_named_object::expression(Expression** pexpr) | |
2830 | { | |
2831 | switch ((*pexpr)->classification()) | |
2832 | { | |
2833 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2834 | { |
2835 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2836 | if (ce->named_object() == this->no_) | |
2837 | break; | |
2838 | ||
2839 | // We need to check a constant initializer explicitly, as | |
2840 | // loops here will not be caught by the loop checking for | |
2841 | // variable initializers. | |
2842 | ce->check_for_init_loop(); | |
2843 | ||
2844 | return TRAVERSE_CONTINUE; | |
2845 | } | |
2846 | ||
d5b605df | 2847 | case Expression::EXPRESSION_VAR_REFERENCE: |
2848 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2849 | break; | |
2850 | return TRAVERSE_CONTINUE; | |
2851 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2852 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2853 | break; | |
2854 | return TRAVERSE_CONTINUE; | |
2855 | default: | |
2856 | return TRAVERSE_CONTINUE; | |
2857 | } | |
2858 | this->found_ = true; | |
2859 | return TRAVERSE_EXIT; | |
2860 | } | |
2861 | ||
e440a328 | 2862 | // The nil value. |
2863 | ||
2864 | class Nil_expression : public Expression | |
2865 | { | |
2866 | public: | |
b13c66cd | 2867 | Nil_expression(Location location) |
e440a328 | 2868 | : Expression(EXPRESSION_NIL, location) |
2869 | { } | |
2870 | ||
2871 | static Expression* | |
2872 | do_import(Import*); | |
2873 | ||
2874 | protected: | |
2875 | bool | |
2876 | do_is_constant() const | |
2877 | { return true; } | |
2878 | ||
f9ca30f9 | 2879 | bool |
2880 | do_is_immutable() const | |
2881 | { return true; } | |
2882 | ||
e440a328 | 2883 | Type* |
2884 | do_type() | |
2885 | { return Type::make_nil_type(); } | |
2886 | ||
2887 | void | |
2888 | do_determine_type(const Type_context*) | |
2889 | { } | |
2890 | ||
2891 | Expression* | |
2892 | do_copy() | |
2893 | { return this; } | |
2894 | ||
ea664253 | 2895 | Bexpression* |
2896 | do_get_backend(Translate_context* context) | |
2897 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 2898 | |
2899 | void | |
2900 | do_export(Export* exp) const | |
2901 | { exp->write_c_string("nil"); } | |
d751bb78 | 2902 | |
2903 | void | |
2904 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2905 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 2906 | }; |
2907 | ||
2908 | // Import a nil expression. | |
2909 | ||
2910 | Expression* | |
2911 | Nil_expression::do_import(Import* imp) | |
2912 | { | |
2913 | imp->require_c_string("nil"); | |
2914 | return Expression::make_nil(imp->location()); | |
2915 | } | |
2916 | ||
2917 | // Make a nil expression. | |
2918 | ||
2919 | Expression* | |
b13c66cd | 2920 | Expression::make_nil(Location location) |
e440a328 | 2921 | { |
2922 | return new Nil_expression(location); | |
2923 | } | |
2924 | ||
2925 | // The value of the predeclared constant iota. This is little more | |
2926 | // than a marker. This will be lowered to an integer in | |
2927 | // Const_expression::do_lower, which is where we know the value that | |
2928 | // it should have. | |
2929 | ||
2930 | class Iota_expression : public Parser_expression | |
2931 | { | |
2932 | public: | |
b13c66cd | 2933 | Iota_expression(Location location) |
e440a328 | 2934 | : Parser_expression(EXPRESSION_IOTA, location) |
2935 | { } | |
2936 | ||
2937 | protected: | |
2938 | Expression* | |
ceeb4318 | 2939 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 2940 | { go_unreachable(); } |
e440a328 | 2941 | |
2942 | // There should only ever be one of these. | |
2943 | Expression* | |
2944 | do_copy() | |
c3e6f413 | 2945 | { go_unreachable(); } |
d751bb78 | 2946 | |
2947 | void | |
2948 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2949 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 2950 | }; |
2951 | ||
2952 | // Make an iota expression. This is only called for one case: the | |
2953 | // value of the predeclared constant iota. | |
2954 | ||
2955 | Expression* | |
2956 | Expression::make_iota() | |
2957 | { | |
b13c66cd | 2958 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 2959 | return &iota_expression; |
2960 | } | |
2961 | ||
2962 | // A type conversion expression. | |
2963 | ||
2964 | class Type_conversion_expression : public Expression | |
2965 | { | |
2966 | public: | |
2967 | Type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 2968 | Location location) |
e440a328 | 2969 | : Expression(EXPRESSION_CONVERSION, location), |
2970 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2971 | { } | |
2972 | ||
2973 | // Return the type to which we are converting. | |
2974 | Type* | |
2975 | type() const | |
2976 | { return this->type_; } | |
2977 | ||
2978 | // Return the expression which we are converting. | |
2979 | Expression* | |
2980 | expr() const | |
2981 | { return this->expr_; } | |
2982 | ||
2983 | // Permit converting from one function type to another. This is | |
2984 | // used internally for method expressions. | |
2985 | void | |
2986 | set_may_convert_function_types() | |
2987 | { | |
2988 | this->may_convert_function_types_ = true; | |
2989 | } | |
2990 | ||
2991 | // Import a type conversion expression. | |
2992 | static Expression* | |
2993 | do_import(Import*); | |
2994 | ||
2995 | protected: | |
2996 | int | |
2997 | do_traverse(Traverse* traverse); | |
2998 | ||
2999 | Expression* | |
ceeb4318 | 3000 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 3001 | |
35a54f17 | 3002 | Expression* |
3003 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
3004 | ||
e440a328 | 3005 | bool |
1ca01a59 | 3006 | do_is_constant() const; |
e440a328 | 3007 | |
0e168074 | 3008 | bool |
3009 | do_is_immutable() const; | |
3010 | ||
e440a328 | 3011 | bool |
0c77715b | 3012 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 3013 | |
3014 | bool | |
3015 | do_string_constant_value(std::string*) const; | |
3016 | ||
3017 | Type* | |
3018 | do_type() | |
3019 | { return this->type_; } | |
3020 | ||
3021 | void | |
3022 | do_determine_type(const Type_context*) | |
3023 | { | |
3024 | Type_context subcontext(this->type_, false); | |
3025 | this->expr_->determine_type(&subcontext); | |
3026 | } | |
3027 | ||
3028 | void | |
3029 | do_check_types(Gogo*); | |
3030 | ||
3031 | Expression* | |
3032 | do_copy() | |
3033 | { | |
3034 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
3035 | this->location()); | |
3036 | } | |
3037 | ||
ea664253 | 3038 | Bexpression* |
3039 | do_get_backend(Translate_context* context); | |
e440a328 | 3040 | |
3041 | void | |
3042 | do_export(Export*) const; | |
3043 | ||
d751bb78 | 3044 | void |
3045 | do_dump_expression(Ast_dump_context*) const; | |
3046 | ||
e440a328 | 3047 | private: |
3048 | // The type to convert to. | |
3049 | Type* type_; | |
3050 | // The expression to convert. | |
3051 | Expression* expr_; | |
3052 | // True if this is permitted to convert function types. This is | |
3053 | // used internally for method expressions. | |
3054 | bool may_convert_function_types_; | |
3055 | }; | |
3056 | ||
3057 | // Traversal. | |
3058 | ||
3059 | int | |
3060 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3061 | { | |
3062 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3063 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3064 | return TRAVERSE_EXIT; | |
3065 | return TRAVERSE_CONTINUE; | |
3066 | } | |
3067 | ||
3068 | // Convert to a constant at lowering time. | |
3069 | ||
3070 | Expression* | |
ceeb4318 | 3071 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3072 | Statement_inserter*, int) | |
e440a328 | 3073 | { |
3074 | Type* type = this->type_; | |
3075 | Expression* val = this->expr_; | |
b13c66cd | 3076 | Location location = this->location(); |
e440a328 | 3077 | |
0c77715b | 3078 | if (type->is_numeric_type()) |
e440a328 | 3079 | { |
0c77715b | 3080 | Numeric_constant nc; |
3081 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3082 | { |
0c77715b | 3083 | if (!nc.set_type(type, true, location)) |
3084 | return Expression::make_error(location); | |
3085 | return nc.expression(location); | |
e440a328 | 3086 | } |
e440a328 | 3087 | } |
3088 | ||
55072f2b | 3089 | if (type->is_slice_type()) |
e440a328 | 3090 | { |
3091 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3092 | bool is_byte = (element_type->integer_type() != NULL |
3093 | && element_type->integer_type()->is_byte()); | |
3094 | bool is_rune = (element_type->integer_type() != NULL | |
3095 | && element_type->integer_type()->is_rune()); | |
3096 | if (is_byte || is_rune) | |
e440a328 | 3097 | { |
3098 | std::string s; | |
3099 | if (val->string_constant_value(&s)) | |
3100 | { | |
3101 | Expression_list* vals = new Expression_list(); | |
3102 | if (is_byte) | |
3103 | { | |
3104 | for (std::string::const_iterator p = s.begin(); | |
3105 | p != s.end(); | |
3106 | p++) | |
3107 | { | |
3108 | mpz_t val; | |
3109 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
3110 | Expression* v = Expression::make_integer(&val, | |
3111 | element_type, | |
3112 | location); | |
3113 | vals->push_back(v); | |
3114 | mpz_clear(val); | |
3115 | } | |
3116 | } | |
3117 | else | |
3118 | { | |
3119 | const char *p = s.data(); | |
3120 | const char *pend = s.data() + s.length(); | |
3121 | while (p < pend) | |
3122 | { | |
3123 | unsigned int c; | |
3124 | int adv = Lex::fetch_char(p, &c); | |
3125 | if (adv == 0) | |
3126 | { | |
3127 | warning_at(this->location(), 0, | |
3128 | "invalid UTF-8 encoding"); | |
3129 | adv = 1; | |
3130 | } | |
3131 | p += adv; | |
3132 | mpz_t val; | |
3133 | mpz_init_set_ui(val, c); | |
3134 | Expression* v = Expression::make_integer(&val, | |
3135 | element_type, | |
3136 | location); | |
3137 | vals->push_back(v); | |
3138 | mpz_clear(val); | |
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 | { | |
2c809f8f | 3158 | if (((this->type()->is_string_type() |
3159 | && this->expr_->type()->is_slice_type()) | |
3160 | || (this->type()->interface_type() != NULL | |
3161 | && this->expr_->type()->interface_type() != NULL)) | |
35a54f17 | 3162 | && !this->expr_->is_variable()) |
3163 | { | |
3164 | Temporary_statement* temp = | |
3165 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3166 | inserter->insert(temp); | |
3167 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3168 | } | |
3169 | return this; | |
3170 | } | |
3171 | ||
1ca01a59 | 3172 | // Return whether a type conversion is a constant. |
3173 | ||
3174 | bool | |
3175 | Type_conversion_expression::do_is_constant() const | |
3176 | { | |
3177 | if (!this->expr_->is_constant()) | |
3178 | return false; | |
3179 | ||
3180 | // A conversion to a type that may not be used as a constant is not | |
3181 | // a constant. For example, []byte(nil). | |
3182 | Type* type = this->type_; | |
3183 | if (type->integer_type() == NULL | |
3184 | && type->float_type() == NULL | |
3185 | && type->complex_type() == NULL | |
3186 | && !type->is_boolean_type() | |
3187 | && !type->is_string_type()) | |
3188 | return false; | |
3189 | ||
3190 | return true; | |
3191 | } | |
3192 | ||
0e168074 | 3193 | // Return whether a type conversion is immutable. |
3194 | ||
3195 | bool | |
3196 | Type_conversion_expression::do_is_immutable() const | |
3197 | { | |
3198 | Type* type = this->type_; | |
3199 | Type* expr_type = this->expr_->type(); | |
3200 | ||
3201 | if (type->interface_type() != NULL | |
3202 | || expr_type->interface_type() != NULL) | |
3203 | return false; | |
3204 | ||
3205 | if (!this->expr_->is_immutable()) | |
3206 | return false; | |
3207 | ||
3208 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3209 | return true; | |
3210 | ||
3211 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3212 | } | |
3213 | ||
0c77715b | 3214 | // Return the constant numeric value if there is one. |
e440a328 | 3215 | |
3216 | bool | |
0c77715b | 3217 | Type_conversion_expression::do_numeric_constant_value( |
3218 | Numeric_constant* nc) const | |
e440a328 | 3219 | { |
0c77715b | 3220 | if (!this->type_->is_numeric_type()) |
e440a328 | 3221 | return false; |
0c77715b | 3222 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3223 | return false; |
0c77715b | 3224 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3225 | } |
3226 | ||
3227 | // Return the constant string value if there is one. | |
3228 | ||
3229 | bool | |
3230 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3231 | { | |
3232 | if (this->type_->is_string_type() | |
3233 | && this->expr_->type()->integer_type() != NULL) | |
3234 | { | |
0c77715b | 3235 | Numeric_constant nc; |
3236 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3237 | { |
0c77715b | 3238 | unsigned long ival; |
3239 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3240 | { |
0c77715b | 3241 | val->clear(); |
3242 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3243 | return true; |
3244 | } | |
3245 | } | |
e440a328 | 3246 | } |
3247 | ||
3248 | // FIXME: Could handle conversion from const []int here. | |
3249 | ||
3250 | return false; | |
3251 | } | |
3252 | ||
3253 | // Check that types are convertible. | |
3254 | ||
3255 | void | |
3256 | Type_conversion_expression::do_check_types(Gogo*) | |
3257 | { | |
3258 | Type* type = this->type_; | |
3259 | Type* expr_type = this->expr_->type(); | |
3260 | std::string reason; | |
3261 | ||
5c13bd80 | 3262 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3263 | { |
842f6425 | 3264 | this->set_is_error(); |
3265 | return; | |
3266 | } | |
3267 | ||
e440a328 | 3268 | if (this->may_convert_function_types_ |
3269 | && type->function_type() != NULL | |
3270 | && expr_type->function_type() != NULL) | |
3271 | return; | |
3272 | ||
3273 | if (Type::are_convertible(type, expr_type, &reason)) | |
3274 | return; | |
3275 | ||
3276 | error_at(this->location(), "%s", reason.c_str()); | |
3277 | this->set_is_error(); | |
3278 | } | |
3279 | ||
ea664253 | 3280 | // Get the backend representation for a type conversion. |
e440a328 | 3281 | |
ea664253 | 3282 | Bexpression* |
3283 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3284 | { |
e440a328 | 3285 | Type* type = this->type_; |
3286 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3287 | |
3288 | Gogo* gogo = context->gogo(); | |
3289 | Btype* btype = type->get_backend(gogo); | |
ea664253 | 3290 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3291 | Location loc = this->location(); |
3292 | ||
3293 | if (Type::are_identical(type, expr_type, false, NULL)) | |
ea664253 | 3294 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
2c809f8f | 3295 | else if (type->interface_type() != NULL |
3296 | || expr_type->interface_type() != NULL) | |
e440a328 | 3297 | { |
2c809f8f | 3298 | Expression* conversion = |
3299 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3300 | this->location()); | |
ea664253 | 3301 | return conversion->get_backend(context); |
e440a328 | 3302 | } |
3303 | else if (type->is_string_type() | |
3304 | && expr_type->integer_type() != NULL) | |
3305 | { | |
2c809f8f | 3306 | mpz_t intval; |
3307 | Numeric_constant nc; | |
3308 | if (this->expr_->numeric_constant_value(&nc) | |
3309 | && nc.to_int(&intval) | |
3310 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3311 | { |
e440a328 | 3312 | std::string s; |
2c809f8f | 3313 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3314 | mpz_clear(intval); | |
3315 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3316 | return se->get_backend(context); |
e440a328 | 3317 | } |
3318 | ||
f16ab008 | 3319 | Expression* i2s_expr = |
2c809f8f | 3320 | Runtime::make_call(Runtime::INT_TO_STRING, loc, 1, this->expr_); |
ea664253 | 3321 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3322 | } |
55072f2b | 3323 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3324 | { |
55072f2b | 3325 | Array_type* a = expr_type->array_type(); |
e440a328 | 3326 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3327 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3328 | go_assert(this->expr_->is_variable()); |
3329 | ||
3330 | Runtime::Function code; | |
60963afd | 3331 | if (e->integer_type()->is_byte()) |
35a54f17 | 3332 | code = Runtime::BYTE_ARRAY_TO_STRING; |
e440a328 | 3333 | else |
35a54f17 | 3334 | { |
3335 | go_assert(e->integer_type()->is_rune()); | |
3336 | code = Runtime::INT_ARRAY_TO_STRING; | |
3337 | } | |
3338 | Expression* valptr = a->get_value_pointer(gogo, this->expr_); | |
3339 | Expression* len = a->get_length(gogo, this->expr_); | |
ea664253 | 3340 | return Runtime::make_call(code, loc, 2, valptr, |
3341 | len)->get_backend(context); | |
e440a328 | 3342 | } |
411eb89e | 3343 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3344 | { |
3345 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3346 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3347 | |
2c809f8f | 3348 | Runtime::Function code; |
60963afd | 3349 | if (e->integer_type()->is_byte()) |
2c809f8f | 3350 | code = Runtime::STRING_TO_BYTE_ARRAY; |
e440a328 | 3351 | else |
3352 | { | |
60963afd | 3353 | go_assert(e->integer_type()->is_rune()); |
2c809f8f | 3354 | code = Runtime::STRING_TO_INT_ARRAY; |
e440a328 | 3355 | } |
2c809f8f | 3356 | Expression* s2a = Runtime::make_call(code, loc, 1, this->expr_); |
ea664253 | 3357 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3358 | } |
3359 | else if (type->is_numeric_type()) | |
3360 | { | |
3361 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
ea664253 | 3362 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3363 | } |
3364 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3365 | && (expr_type->points_to() != NULL |
3366 | || expr_type->integer_type())) | |
3367 | || (expr_type->is_unsafe_pointer_type() | |
3368 | && type->points_to() != NULL) | |
3369 | || (this->may_convert_function_types_ | |
3370 | && type->function_type() != NULL | |
3371 | && expr_type->function_type() != NULL)) | |
ea664253 | 3372 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3373 | else |
2c809f8f | 3374 | { |
3375 | Expression* conversion = | |
3376 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3377 | return conversion->get_backend(context); |
2c809f8f | 3378 | } |
e440a328 | 3379 | } |
3380 | ||
3381 | // Output a type conversion in a constant expression. | |
3382 | ||
3383 | void | |
3384 | Type_conversion_expression::do_export(Export* exp) const | |
3385 | { | |
3386 | exp->write_c_string("convert("); | |
3387 | exp->write_type(this->type_); | |
3388 | exp->write_c_string(", "); | |
3389 | this->expr_->export_expression(exp); | |
3390 | exp->write_c_string(")"); | |
3391 | } | |
3392 | ||
3393 | // Import a type conversion or a struct construction. | |
3394 | ||
3395 | Expression* | |
3396 | Type_conversion_expression::do_import(Import* imp) | |
3397 | { | |
3398 | imp->require_c_string("convert("); | |
3399 | Type* type = imp->read_type(); | |
3400 | imp->require_c_string(", "); | |
3401 | Expression* val = Expression::import_expression(imp); | |
3402 | imp->require_c_string(")"); | |
3403 | return Expression::make_cast(type, val, imp->location()); | |
3404 | } | |
3405 | ||
d751bb78 | 3406 | // Dump ast representation for a type conversion expression. |
3407 | ||
3408 | void | |
3409 | Type_conversion_expression::do_dump_expression( | |
3410 | Ast_dump_context* ast_dump_context) const | |
3411 | { | |
3412 | ast_dump_context->dump_type(this->type_); | |
3413 | ast_dump_context->ostream() << "("; | |
3414 | ast_dump_context->dump_expression(this->expr_); | |
3415 | ast_dump_context->ostream() << ") "; | |
3416 | } | |
3417 | ||
e440a328 | 3418 | // Make a type cast expression. |
3419 | ||
3420 | Expression* | |
b13c66cd | 3421 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3422 | { |
3423 | if (type->is_error_type() || val->is_error_expression()) | |
3424 | return Expression::make_error(location); | |
3425 | return new Type_conversion_expression(type, val, location); | |
3426 | } | |
3427 | ||
9581e91d | 3428 | // An unsafe type conversion, used to pass values to builtin functions. |
3429 | ||
3430 | class Unsafe_type_conversion_expression : public Expression | |
3431 | { | |
3432 | public: | |
3433 | Unsafe_type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 3434 | Location location) |
9581e91d | 3435 | : Expression(EXPRESSION_UNSAFE_CONVERSION, location), |
3436 | type_(type), expr_(expr) | |
3437 | { } | |
3438 | ||
3439 | protected: | |
3440 | int | |
3441 | do_traverse(Traverse* traverse); | |
3442 | ||
3443 | Type* | |
3444 | do_type() | |
3445 | { return this->type_; } | |
3446 | ||
3447 | void | |
3448 | do_determine_type(const Type_context*) | |
a9182619 | 3449 | { this->expr_->determine_type_no_context(); } |
9581e91d | 3450 | |
3451 | Expression* | |
3452 | do_copy() | |
3453 | { | |
3454 | return new Unsafe_type_conversion_expression(this->type_, | |
3455 | this->expr_->copy(), | |
3456 | this->location()); | |
3457 | } | |
3458 | ||
ea664253 | 3459 | Bexpression* |
3460 | do_get_backend(Translate_context*); | |
9581e91d | 3461 | |
d751bb78 | 3462 | void |
3463 | do_dump_expression(Ast_dump_context*) const; | |
3464 | ||
9581e91d | 3465 | private: |
3466 | // The type to convert to. | |
3467 | Type* type_; | |
3468 | // The expression to convert. | |
3469 | Expression* expr_; | |
3470 | }; | |
3471 | ||
3472 | // Traversal. | |
3473 | ||
3474 | int | |
3475 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3476 | { | |
3477 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3478 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3479 | return TRAVERSE_EXIT; | |
3480 | return TRAVERSE_CONTINUE; | |
3481 | } | |
3482 | ||
3483 | // Convert to backend representation. | |
3484 | ||
ea664253 | 3485 | Bexpression* |
3486 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3487 | { |
3488 | // We are only called for a limited number of cases. | |
3489 | ||
3490 | Type* t = this->type_; | |
3491 | Type* et = this->expr_->type(); | |
2c809f8f | 3492 | if (t->array_type() != NULL) |
3493 | go_assert(et->array_type() != NULL | |
3494 | && t->is_slice_type() == et->is_slice_type()); | |
3495 | else if (t->struct_type() != NULL) | |
9581e91d | 3496 | { |
2c809f8f | 3497 | if (t->named_type() != NULL |
3498 | && et->named_type() != NULL | |
3499 | && !Type::are_convertible(t, et, NULL)) | |
3500 | { | |
3501 | go_assert(saw_errors()); | |
ea664253 | 3502 | return context->backend()->error_expression(); |
2c809f8f | 3503 | } |
3504 | ||
3505 | go_assert(et->struct_type() != NULL | |
3506 | && Type::are_convertible(t, et, NULL)); | |
3507 | } | |
3508 | else if (t->map_type() != NULL) | |
c484d925 | 3509 | go_assert(et->map_type() != NULL); |
9581e91d | 3510 | else if (t->channel_type() != NULL) |
c484d925 | 3511 | go_assert(et->channel_type() != NULL); |
09ea332d | 3512 | else if (t->points_to() != NULL) |
2c809f8f | 3513 | go_assert(et->points_to() != NULL |
3514 | || et->channel_type() != NULL | |
3515 | || et->map_type() != NULL | |
3516 | || et->function_type() != NULL | |
3517 | || et->is_nil_type()); | |
9581e91d | 3518 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3519 | go_assert(t->points_to() != NULL); |
2c809f8f | 3520 | else if (t->interface_type() != NULL) |
9581e91d | 3521 | { |
2c809f8f | 3522 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3523 | go_assert(et->interface_type() != NULL |
2c809f8f | 3524 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3525 | } |
588e3cf9 | 3526 | else if (t->integer_type() != NULL) |
2c809f8f | 3527 | go_assert(et->is_boolean_type() |
3528 | || et->integer_type() != NULL | |
3529 | || et->function_type() != NULL | |
3530 | || et->points_to() != NULL | |
3531 | || et->map_type() != NULL | |
3532 | || et->channel_type() != NULL); | |
9581e91d | 3533 | else |
c3e6f413 | 3534 | go_unreachable(); |
9581e91d | 3535 | |
2c809f8f | 3536 | Gogo* gogo = context->gogo(); |
3537 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3538 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3539 | Location loc = this->location(); |
ea664253 | 3540 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3541 | } |
3542 | ||
d751bb78 | 3543 | // Dump ast representation for an unsafe type conversion expression. |
3544 | ||
3545 | void | |
3546 | Unsafe_type_conversion_expression::do_dump_expression( | |
3547 | Ast_dump_context* ast_dump_context) const | |
3548 | { | |
3549 | ast_dump_context->dump_type(this->type_); | |
3550 | ast_dump_context->ostream() << "("; | |
3551 | ast_dump_context->dump_expression(this->expr_); | |
3552 | ast_dump_context->ostream() << ") "; | |
3553 | } | |
3554 | ||
9581e91d | 3555 | // Make an unsafe type conversion expression. |
3556 | ||
3557 | Expression* | |
3558 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3559 | Location location) |
9581e91d | 3560 | { |
3561 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3562 | } | |
3563 | ||
76f85fd6 | 3564 | // Class Unary_expression. |
e440a328 | 3565 | |
3566 | // If we are taking the address of a composite literal, and the | |
2c809f8f | 3567 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3568 | // instead. |
3569 | ||
3570 | Expression* | |
ceeb4318 | 3571 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3572 | { |
b13c66cd | 3573 | Location loc = this->location(); |
e440a328 | 3574 | Operator op = this->op_; |
3575 | Expression* expr = this->expr_; | |
3576 | ||
3577 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3578 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3579 | ||
3580 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3581 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3582 | // analysis here? This case is found in math/unsafe.go and is | |
3583 | // therefore worth special casing. | |
3584 | if (op == OPERATOR_MULT) | |
3585 | { | |
3586 | Expression* e = expr; | |
3587 | while (e->classification() == EXPRESSION_CONVERSION) | |
3588 | { | |
3589 | Type_conversion_expression* te | |
3590 | = static_cast<Type_conversion_expression*>(e); | |
3591 | e = te->expr(); | |
3592 | } | |
3593 | ||
3594 | if (e->classification() == EXPRESSION_UNARY) | |
3595 | { | |
3596 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3597 | if (ue->op_ == OPERATOR_AND) | |
3598 | { | |
3599 | if (e == expr) | |
3600 | { | |
3601 | // *&x == x. | |
f4dea966 | 3602 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3603 | { | |
3604 | error_at(ue->location(), | |
3605 | "invalid operand for unary %<&%>"); | |
3606 | this->set_is_error(); | |
3607 | } | |
e440a328 | 3608 | return ue->expr_; |
3609 | } | |
3610 | ue->set_does_not_escape(); | |
3611 | } | |
3612 | } | |
3613 | } | |
3614 | ||
55661ce9 | 3615 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3616 | // having to deal with TYPE_VOID in other places. | |
3617 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3618 | { | |
3619 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3620 | return Expression::make_error(this->location()); | |
3621 | } | |
3622 | ||
59a401fe | 3623 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3624 | { |
0c77715b | 3625 | Numeric_constant nc; |
3626 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3627 | { |
0c77715b | 3628 | Numeric_constant result; |
3629 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3630 | return result.expression(loc); | |
e440a328 | 3631 | } |
3632 | } | |
3633 | ||
3634 | return this; | |
3635 | } | |
3636 | ||
f9ca30f9 | 3637 | // Flatten expression if a nil check must be performed and create temporary |
3638 | // variables if necessary. | |
3639 | ||
3640 | Expression* | |
3641 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3642 | Statement_inserter* inserter) | |
3643 | { | |
f4dea966 | 3644 | if (this->is_error_expression() || this->expr_->is_error_expression()) |
3645 | return Expression::make_error(this->location()); | |
3646 | ||
f9ca30f9 | 3647 | Location location = this->location(); |
3648 | if (this->op_ == OPERATOR_MULT | |
3649 | && !this->expr_->is_variable()) | |
3650 | { | |
3651 | go_assert(this->expr_->type()->points_to() != NULL); | |
3652 | Type* ptype = this->expr_->type()->points_to(); | |
3653 | if (!ptype->is_void_type()) | |
3654 | { | |
3655 | Btype* pbtype = ptype->get_backend(gogo); | |
3656 | size_t s = gogo->backend()->type_size(pbtype); | |
3657 | if (s >= 4096 || this->issue_nil_check_) | |
3658 | { | |
3659 | Temporary_statement* temp = | |
3660 | Statement::make_temporary(NULL, this->expr_, location); | |
3661 | inserter->insert(temp); | |
3662 | this->expr_ = | |
3663 | Expression::make_temporary_reference(temp, location); | |
3664 | } | |
3665 | } | |
3666 | } | |
3667 | ||
3668 | if (this->create_temp_ && !this->expr_->is_variable()) | |
3669 | { | |
3670 | Temporary_statement* temp = | |
3671 | Statement::make_temporary(NULL, this->expr_, location); | |
3672 | inserter->insert(temp); | |
3673 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3674 | } | |
3675 | ||
3676 | return this; | |
3677 | } | |
3678 | ||
e440a328 | 3679 | // Return whether a unary expression is a constant. |
3680 | ||
3681 | bool | |
3682 | Unary_expression::do_is_constant() const | |
3683 | { | |
3684 | if (this->op_ == OPERATOR_MULT) | |
3685 | { | |
3686 | // Indirecting through a pointer is only constant if the object | |
3687 | // to which the expression points is constant, but we currently | |
3688 | // have no way to determine that. | |
3689 | return false; | |
3690 | } | |
3691 | else if (this->op_ == OPERATOR_AND) | |
3692 | { | |
3693 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3694 | // global variable, not constant otherwise. In other cases taking the |
3695 | // address is probably not a constant. | |
e440a328 | 3696 | Var_expression* ve = this->expr_->var_expression(); |
3697 | if (ve != NULL) | |
3698 | { | |
3699 | Named_object* no = ve->named_object(); | |
3700 | return no->is_variable() && no->var_value()->is_global(); | |
3701 | } | |
3702 | return false; | |
3703 | } | |
3704 | else | |
3705 | return this->expr_->is_constant(); | |
3706 | } | |
3707 | ||
0c77715b | 3708 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3709 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3710 | |
3711 | bool | |
0c77715b | 3712 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3713 | Location location, Numeric_constant* nc) | |
e440a328 | 3714 | { |
3715 | switch (op) | |
3716 | { | |
3717 | case OPERATOR_PLUS: | |
0c77715b | 3718 | *nc = *unc; |
e440a328 | 3719 | return true; |
0c77715b | 3720 | |
e440a328 | 3721 | case OPERATOR_MINUS: |
0c77715b | 3722 | if (unc->is_int() || unc->is_rune()) |
3723 | break; | |
3724 | else if (unc->is_float()) | |
3725 | { | |
3726 | mpfr_t uval; | |
3727 | unc->get_float(&uval); | |
3728 | mpfr_t val; | |
3729 | mpfr_init(val); | |
3730 | mpfr_neg(val, uval, GMP_RNDN); | |
3731 | nc->set_float(unc->type(), val); | |
3732 | mpfr_clear(uval); | |
3733 | mpfr_clear(val); | |
3734 | return true; | |
3735 | } | |
3736 | else if (unc->is_complex()) | |
3737 | { | |
3738 | mpfr_t ureal, uimag; | |
3739 | unc->get_complex(&ureal, &uimag); | |
3740 | mpfr_t real, imag; | |
3741 | mpfr_init(real); | |
3742 | mpfr_init(imag); | |
3743 | mpfr_neg(real, ureal, GMP_RNDN); | |
3744 | mpfr_neg(imag, uimag, GMP_RNDN); | |
3745 | nc->set_complex(unc->type(), real, imag); | |
3746 | mpfr_clear(ureal); | |
3747 | mpfr_clear(uimag); | |
3748 | mpfr_clear(real); | |
3749 | mpfr_clear(imag); | |
3750 | return true; | |
3751 | } | |
e440a328 | 3752 | else |
0c77715b | 3753 | go_unreachable(); |
e440a328 | 3754 | |
0c77715b | 3755 | case OPERATOR_XOR: |
3756 | break; | |
68448d53 | 3757 | |
59a401fe | 3758 | case OPERATOR_NOT: |
e440a328 | 3759 | case OPERATOR_AND: |
3760 | case OPERATOR_MULT: | |
3761 | return false; | |
0c77715b | 3762 | |
e440a328 | 3763 | default: |
c3e6f413 | 3764 | go_unreachable(); |
e440a328 | 3765 | } |
e440a328 | 3766 | |
0c77715b | 3767 | if (!unc->is_int() && !unc->is_rune()) |
3768 | return false; | |
3769 | ||
3770 | mpz_t uval; | |
8387e1df | 3771 | if (unc->is_rune()) |
3772 | unc->get_rune(&uval); | |
3773 | else | |
3774 | unc->get_int(&uval); | |
0c77715b | 3775 | mpz_t val; |
3776 | mpz_init(val); | |
e440a328 | 3777 | |
e440a328 | 3778 | switch (op) |
3779 | { | |
e440a328 | 3780 | case OPERATOR_MINUS: |
0c77715b | 3781 | mpz_neg(val, uval); |
3782 | break; | |
3783 | ||
e440a328 | 3784 | case OPERATOR_NOT: |
0c77715b | 3785 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3786 | break; | |
3787 | ||
e440a328 | 3788 | case OPERATOR_XOR: |
0c77715b | 3789 | { |
3790 | Type* utype = unc->type(); | |
3791 | if (utype->integer_type() == NULL | |
3792 | || utype->integer_type()->is_abstract()) | |
3793 | mpz_com(val, uval); | |
3794 | else | |
3795 | { | |
3796 | // The number of HOST_WIDE_INTs that it takes to represent | |
3797 | // UVAL. | |
3798 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3799 | + HOST_BITS_PER_WIDE_INT | |
3800 | - 1) | |
3801 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3802 | |
0c77715b | 3803 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3804 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3805 | ||
3806 | size_t obits = utype->integer_type()->bits(); | |
3807 | ||
3808 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3809 | { | |
3810 | mpz_t adj; | |
3811 | mpz_init_set_ui(adj, 1); | |
3812 | mpz_mul_2exp(adj, adj, obits); | |
3813 | mpz_add(uval, uval, adj); | |
3814 | mpz_clear(adj); | |
3815 | } | |
3816 | ||
3817 | size_t ecount; | |
3818 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3819 | go_assert(ecount <= count); | |
3820 | ||
3821 | // Trim down to the number of words required by the type. | |
3822 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3823 | / HOST_BITS_PER_WIDE_INT); | |
3824 | go_assert(ocount <= count); | |
3825 | ||
3826 | for (size_t i = 0; i < ocount; ++i) | |
3827 | phwi[i] = ~phwi[i]; | |
3828 | ||
3829 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3830 | if (clearbits != 0) | |
3831 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3832 | >> clearbits); | |
3833 | ||
3834 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3835 | ||
3836 | if (!utype->integer_type()->is_unsigned() | |
3837 | && mpz_tstbit(val, obits - 1)) | |
3838 | { | |
3839 | mpz_t adj; | |
3840 | mpz_init_set_ui(adj, 1); | |
3841 | mpz_mul_2exp(adj, adj, obits); | |
3842 | mpz_sub(val, val, adj); | |
3843 | mpz_clear(adj); | |
3844 | } | |
3845 | ||
3846 | delete[] phwi; | |
3847 | } | |
3848 | } | |
3849 | break; | |
e440a328 | 3850 | |
e440a328 | 3851 | default: |
c3e6f413 | 3852 | go_unreachable(); |
e440a328 | 3853 | } |
e440a328 | 3854 | |
0c77715b | 3855 | if (unc->is_rune()) |
3856 | nc->set_rune(NULL, val); | |
e440a328 | 3857 | else |
0c77715b | 3858 | nc->set_int(NULL, val); |
e440a328 | 3859 | |
0c77715b | 3860 | mpz_clear(uval); |
3861 | mpz_clear(val); | |
e440a328 | 3862 | |
0c77715b | 3863 | return nc->set_type(unc->type(), true, location); |
e440a328 | 3864 | } |
3865 | ||
0c77715b | 3866 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 3867 | |
3868 | bool | |
0c77715b | 3869 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 3870 | { |
0c77715b | 3871 | Numeric_constant unc; |
3872 | if (!this->expr_->numeric_constant_value(&unc)) | |
3873 | return false; | |
3874 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
3875 | nc); | |
e440a328 | 3876 | } |
3877 | ||
3878 | // Return the type of a unary expression. | |
3879 | ||
3880 | Type* | |
3881 | Unary_expression::do_type() | |
3882 | { | |
3883 | switch (this->op_) | |
3884 | { | |
3885 | case OPERATOR_PLUS: | |
3886 | case OPERATOR_MINUS: | |
3887 | case OPERATOR_NOT: | |
3888 | case OPERATOR_XOR: | |
3889 | return this->expr_->type(); | |
3890 | ||
3891 | case OPERATOR_AND: | |
3892 | return Type::make_pointer_type(this->expr_->type()); | |
3893 | ||
3894 | case OPERATOR_MULT: | |
3895 | { | |
3896 | Type* subtype = this->expr_->type(); | |
3897 | Type* points_to = subtype->points_to(); | |
3898 | if (points_to == NULL) | |
3899 | return Type::make_error_type(); | |
3900 | return points_to; | |
3901 | } | |
3902 | ||
3903 | default: | |
c3e6f413 | 3904 | go_unreachable(); |
e440a328 | 3905 | } |
3906 | } | |
3907 | ||
3908 | // Determine abstract types for a unary expression. | |
3909 | ||
3910 | void | |
3911 | Unary_expression::do_determine_type(const Type_context* context) | |
3912 | { | |
3913 | switch (this->op_) | |
3914 | { | |
3915 | case OPERATOR_PLUS: | |
3916 | case OPERATOR_MINUS: | |
3917 | case OPERATOR_NOT: | |
3918 | case OPERATOR_XOR: | |
3919 | this->expr_->determine_type(context); | |
3920 | break; | |
3921 | ||
3922 | case OPERATOR_AND: | |
3923 | // Taking the address of something. | |
3924 | { | |
3925 | Type* subtype = (context->type == NULL | |
3926 | ? NULL | |
3927 | : context->type->points_to()); | |
3928 | Type_context subcontext(subtype, false); | |
3929 | this->expr_->determine_type(&subcontext); | |
3930 | } | |
3931 | break; | |
3932 | ||
3933 | case OPERATOR_MULT: | |
3934 | // Indirecting through a pointer. | |
3935 | { | |
3936 | Type* subtype = (context->type == NULL | |
3937 | ? NULL | |
3938 | : Type::make_pointer_type(context->type)); | |
3939 | Type_context subcontext(subtype, false); | |
3940 | this->expr_->determine_type(&subcontext); | |
3941 | } | |
3942 | break; | |
3943 | ||
3944 | default: | |
c3e6f413 | 3945 | go_unreachable(); |
e440a328 | 3946 | } |
3947 | } | |
3948 | ||
3949 | // Check types for a unary expression. | |
3950 | ||
3951 | void | |
3952 | Unary_expression::do_check_types(Gogo*) | |
3953 | { | |
9fe897ef | 3954 | Type* type = this->expr_->type(); |
5c13bd80 | 3955 | if (type->is_error()) |
9fe897ef | 3956 | { |
3957 | this->set_is_error(); | |
3958 | return; | |
3959 | } | |
3960 | ||
e440a328 | 3961 | switch (this->op_) |
3962 | { | |
3963 | case OPERATOR_PLUS: | |
3964 | case OPERATOR_MINUS: | |
9fe897ef | 3965 | if (type->integer_type() == NULL |
3966 | && type->float_type() == NULL | |
3967 | && type->complex_type() == NULL) | |
3968 | this->report_error(_("expected numeric type")); | |
e440a328 | 3969 | break; |
3970 | ||
3971 | case OPERATOR_NOT: | |
59a401fe | 3972 | if (!type->is_boolean_type()) |
3973 | this->report_error(_("expected boolean type")); | |
3974 | break; | |
3975 | ||
e440a328 | 3976 | case OPERATOR_XOR: |
9fe897ef | 3977 | if (type->integer_type() == NULL |
3978 | && !type->is_boolean_type()) | |
3979 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 3980 | break; |
3981 | ||
3982 | case OPERATOR_AND: | |
3983 | if (!this->expr_->is_addressable()) | |
09ea332d | 3984 | { |
3985 | if (!this->create_temp_) | |
f4dea966 | 3986 | { |
3987 | error_at(this->location(), "invalid operand for unary %<&%>"); | |
3988 | this->set_is_error(); | |
3989 | } | |
09ea332d | 3990 | } |
e440a328 | 3991 | else |
56080003 | 3992 | { |
3993 | this->expr_->address_taken(this->escapes_); | |
3994 | this->expr_->issue_nil_check(); | |
3995 | } | |
e440a328 | 3996 | break; |
3997 | ||
3998 | case OPERATOR_MULT: | |
3999 | // Indirecting through a pointer. | |
9fe897ef | 4000 | if (type->points_to() == NULL) |
4001 | this->report_error(_("expected pointer")); | |
e440a328 | 4002 | break; |
4003 | ||
4004 | default: | |
c3e6f413 | 4005 | go_unreachable(); |
e440a328 | 4006 | } |
4007 | } | |
4008 | ||
ea664253 | 4009 | // Get the backend representation for a unary expression. |
e440a328 | 4010 | |
ea664253 | 4011 | Bexpression* |
4012 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4013 | { |
1b1f2abf | 4014 | Gogo* gogo = context->gogo(); |
e9d3367e | 4015 | Location loc = this->location(); |
4016 | ||
4017 | // Taking the address of a set-and-use-temporary expression requires | |
4018 | // setting the temporary and then taking the address. | |
4019 | if (this->op_ == OPERATOR_AND) | |
4020 | { | |
4021 | Set_and_use_temporary_expression* sut = | |
4022 | this->expr_->set_and_use_temporary_expression(); | |
4023 | if (sut != NULL) | |
4024 | { | |
4025 | Temporary_statement* temp = sut->temporary(); | |
4026 | Bvariable* bvar = temp->get_backend_variable(context); | |
f9ca30f9 | 4027 | Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4028 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4029 | |
4030 | Bstatement* bassign = | |
4031 | gogo->backend()->assignment_statement(bvar_expr, bval, loc); | |
4032 | Bexpression* bvar_addr = | |
4033 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4034 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4035 | } |
4036 | } | |
4037 | ||
f9ca30f9 | 4038 | Bexpression* ret; |
ea664253 | 4039 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4040 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4041 | switch (this->op_) |
4042 | { | |
4043 | case OPERATOR_PLUS: | |
f9ca30f9 | 4044 | ret = bexpr; |
4045 | break; | |
e440a328 | 4046 | |
4047 | case OPERATOR_MINUS: | |
f9ca30f9 | 4048 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4049 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4050 | break; | |
e440a328 | 4051 | |
4052 | case OPERATOR_NOT: | |
e440a328 | 4053 | case OPERATOR_XOR: |
f9ca30f9 | 4054 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4055 | break; | |
e440a328 | 4056 | |
4057 | case OPERATOR_AND: | |
09ea332d | 4058 | if (!this->create_temp_) |
4059 | { | |
4060 | // We should not see a non-constant constructor here; cases | |
4061 | // where we would see one should have been moved onto the | |
4062 | // heap at parse time. Taking the address of a nonconstant | |
4063 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4064 | |
4065 | go_assert(!this->expr_->is_composite_literal() | |
4066 | || this->expr_->is_immutable()); | |
24060bf9 | 4067 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4068 | { | |
4069 | Unary_expression* ue = | |
4070 | static_cast<Unary_expression*>(this->expr_); | |
4071 | go_assert(ue->op() != OPERATOR_AND); | |
4072 | } | |
09ea332d | 4073 | } |
e440a328 | 4074 | |
f23d7786 | 4075 | static unsigned int counter; |
4076 | char buf[100]; | |
4077 | if (this->is_gc_root_ || this->is_slice_init_) | |
76f85fd6 | 4078 | { |
f23d7786 | 4079 | bool copy_to_heap = false; |
4080 | if (this->is_gc_root_) | |
4081 | { | |
4082 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4083 | // they cannot be represented as an immutable_struct in the | |
4084 | // backend. | |
4085 | static unsigned int root_counter; | |
4086 | snprintf(buf, sizeof buf, "gc%u", root_counter); | |
4087 | ++root_counter; | |
4088 | } | |
4089 | else | |
4090 | { | |
4091 | // Build a decl for a slice value initializer. An immutable slice | |
4092 | // value initializer may have to be copied to the heap if it | |
4093 | // contains pointers in a non-constant context. | |
4094 | snprintf(buf, sizeof buf, "C%u", counter); | |
4095 | ++counter; | |
4096 | ||
4097 | Array_type* at = this->expr_->type()->array_type(); | |
4098 | go_assert(at != NULL); | |
4099 | ||
4100 | // If we are not copying the value to the heap, we will only | |
4101 | // initialize the value once, so we can use this directly | |
4102 | // rather than copying it. In that case we can't make it | |
4103 | // read-only, because the program is permitted to change it. | |
4104 | copy_to_heap = (at->element_type()->has_pointer() | |
4105 | && !context->is_const()); | |
4106 | } | |
4107 | Bvariable* implicit = | |
5892f89f | 4108 | gogo->backend()->implicit_variable(buf, btype, bexpr, copy_to_heap, |
4109 | false, 0); | |
f23d7786 | 4110 | bexpr = gogo->backend()->var_expression(implicit, loc); |
76f85fd6 | 4111 | } |
4112 | else if ((this->expr_->is_composite_literal() | |
f9ca30f9 | 4113 | || this->expr_->string_expression() != NULL) |
4114 | && this->expr_->is_immutable()) | |
4115 | { | |
76f85fd6 | 4116 | // Build a decl for a constant constructor. |
f9ca30f9 | 4117 | snprintf(buf, sizeof buf, "C%u", counter); |
4118 | ++counter; | |
4119 | ||
4120 | Bvariable* decl = | |
4121 | gogo->backend()->immutable_struct(buf, true, false, btype, loc); | |
4122 | gogo->backend()->immutable_struct_set_init(decl, buf, true, false, | |
4123 | btype, loc, bexpr); | |
4124 | bexpr = gogo->backend()->var_expression(decl, loc); | |
4125 | } | |
09ea332d | 4126 | |
f9ca30f9 | 4127 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4128 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4129 | break; | |
e440a328 | 4130 | |
4131 | case OPERATOR_MULT: | |
4132 | { | |
f9ca30f9 | 4133 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4134 | |
4135 | // If we are dereferencing the pointer to a large struct, we | |
4136 | // need to check for nil. We don't bother to check for small | |
4137 | // structs because we expect the system to crash on a nil | |
56080003 | 4138 | // pointer dereference. However, if we know the address of this |
4139 | // expression is being taken, we must always check for nil. | |
f9ca30f9 | 4140 | |
4141 | Type* ptype = this->expr_->type()->points_to(); | |
4142 | Btype* pbtype = ptype->get_backend(gogo); | |
4143 | if (!ptype->is_void_type()) | |
e440a328 | 4144 | { |
f9ca30f9 | 4145 | size_t s = gogo->backend()->type_size(pbtype); |
4146 | if (s >= 4096 || this->issue_nil_check_) | |
19b4f09b | 4147 | { |
f9ca30f9 | 4148 | go_assert(this->expr_->is_variable()); |
ea664253 | 4149 | Bexpression* nil = |
4150 | Expression::make_nil(loc)->get_backend(context); | |
f9ca30f9 | 4151 | Bexpression* compare = |
4152 | gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr, | |
4153 | nil, loc); | |
f9ca30f9 | 4154 | Bexpression* crash = |
ea664253 | 4155 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4156 | loc)->get_backend(context); | |
f9ca30f9 | 4157 | bexpr = gogo->backend()->conditional_expression(btype, compare, |
4158 | crash, bexpr, | |
4159 | loc); | |
4160 | ||
19b4f09b | 4161 | } |
e440a328 | 4162 | } |
9b27b43c | 4163 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); |
e440a328 | 4164 | } |
f9ca30f9 | 4165 | break; |
e440a328 | 4166 | |
4167 | default: | |
c3e6f413 | 4168 | go_unreachable(); |
e440a328 | 4169 | } |
f9ca30f9 | 4170 | |
ea664253 | 4171 | return ret; |
e440a328 | 4172 | } |
4173 | ||
4174 | // Export a unary expression. | |
4175 | ||
4176 | void | |
4177 | Unary_expression::do_export(Export* exp) const | |
4178 | { | |
4179 | switch (this->op_) | |
4180 | { | |
4181 | case OPERATOR_PLUS: | |
4182 | exp->write_c_string("+ "); | |
4183 | break; | |
4184 | case OPERATOR_MINUS: | |
4185 | exp->write_c_string("- "); | |
4186 | break; | |
4187 | case OPERATOR_NOT: | |
4188 | exp->write_c_string("! "); | |
4189 | break; | |
4190 | case OPERATOR_XOR: | |
4191 | exp->write_c_string("^ "); | |
4192 | break; | |
4193 | case OPERATOR_AND: | |
4194 | case OPERATOR_MULT: | |
4195 | default: | |
c3e6f413 | 4196 | go_unreachable(); |
e440a328 | 4197 | } |
4198 | this->expr_->export_expression(exp); | |
4199 | } | |
4200 | ||
4201 | // Import a unary expression. | |
4202 | ||
4203 | Expression* | |
4204 | Unary_expression::do_import(Import* imp) | |
4205 | { | |
4206 | Operator op; | |
4207 | switch (imp->get_char()) | |
4208 | { | |
4209 | case '+': | |
4210 | op = OPERATOR_PLUS; | |
4211 | break; | |
4212 | case '-': | |
4213 | op = OPERATOR_MINUS; | |
4214 | break; | |
4215 | case '!': | |
4216 | op = OPERATOR_NOT; | |
4217 | break; | |
4218 | case '^': | |
4219 | op = OPERATOR_XOR; | |
4220 | break; | |
4221 | default: | |
c3e6f413 | 4222 | go_unreachable(); |
e440a328 | 4223 | } |
4224 | imp->require_c_string(" "); | |
4225 | Expression* expr = Expression::import_expression(imp); | |
4226 | return Expression::make_unary(op, expr, imp->location()); | |
4227 | } | |
4228 | ||
d751bb78 | 4229 | // Dump ast representation of an unary expression. |
4230 | ||
4231 | void | |
4232 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4233 | { | |
4234 | ast_dump_context->dump_operator(this->op_); | |
4235 | ast_dump_context->ostream() << "("; | |
4236 | ast_dump_context->dump_expression(this->expr_); | |
4237 | ast_dump_context->ostream() << ") "; | |
4238 | } | |
4239 | ||
e440a328 | 4240 | // Make a unary expression. |
4241 | ||
4242 | Expression* | |
b13c66cd | 4243 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4244 | { |
4245 | return new Unary_expression(op, expr, location); | |
4246 | } | |
4247 | ||
4248 | // If this is an indirection through a pointer, return the expression | |
4249 | // being pointed through. Otherwise return this. | |
4250 | ||
4251 | Expression* | |
4252 | Expression::deref() | |
4253 | { | |
4254 | if (this->classification_ == EXPRESSION_UNARY) | |
4255 | { | |
4256 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4257 | if (ue->op() == OPERATOR_MULT) | |
4258 | return ue->operand(); | |
4259 | } | |
4260 | return this; | |
4261 | } | |
4262 | ||
4263 | // Class Binary_expression. | |
4264 | ||
4265 | // Traversal. | |
4266 | ||
4267 | int | |
4268 | Binary_expression::do_traverse(Traverse* traverse) | |
4269 | { | |
4270 | int t = Expression::traverse(&this->left_, traverse); | |
4271 | if (t == TRAVERSE_EXIT) | |
4272 | return TRAVERSE_EXIT; | |
4273 | return Expression::traverse(&this->right_, traverse); | |
4274 | } | |
4275 | ||
0c77715b | 4276 | // Return the type to use for a binary operation on operands of |
4277 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4278 | // such may be NULL or abstract. | |
4279 | ||
4280 | bool | |
4281 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4282 | Type* right_type, Type** result_type) | |
4283 | { | |
4284 | if (left_type != right_type | |
4285 | && !left_type->is_abstract() | |
4286 | && !right_type->is_abstract() | |
4287 | && left_type->base() != right_type->base() | |
4288 | && op != OPERATOR_LSHIFT | |
4289 | && op != OPERATOR_RSHIFT) | |
4290 | { | |
4291 | // May be a type error--let it be diagnosed elsewhere. | |
4292 | return false; | |
4293 | } | |
4294 | ||
4295 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4296 | { | |
4297 | if (left_type->integer_type() != NULL) | |
4298 | *result_type = left_type; | |
4299 | else | |
4300 | *result_type = Type::make_abstract_integer_type(); | |
4301 | } | |
4302 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4303 | *result_type = left_type; | |
4304 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4305 | *result_type = right_type; | |
4306 | else if (!left_type->is_abstract()) | |
4307 | *result_type = left_type; | |
4308 | else if (!right_type->is_abstract()) | |
4309 | *result_type = right_type; | |
4310 | else if (left_type->complex_type() != NULL) | |
4311 | *result_type = left_type; | |
4312 | else if (right_type->complex_type() != NULL) | |
4313 | *result_type = right_type; | |
4314 | else if (left_type->float_type() != NULL) | |
4315 | *result_type = left_type; | |
4316 | else if (right_type->float_type() != NULL) | |
4317 | *result_type = right_type; | |
4318 | else if (left_type->integer_type() != NULL | |
4319 | && left_type->integer_type()->is_rune()) | |
4320 | *result_type = left_type; | |
4321 | else if (right_type->integer_type() != NULL | |
4322 | && right_type->integer_type()->is_rune()) | |
4323 | *result_type = right_type; | |
4324 | else | |
4325 | *result_type = left_type; | |
4326 | ||
4327 | return true; | |
4328 | } | |
4329 | ||
4330 | // Convert an integer comparison code and an operator to a boolean | |
4331 | // value. | |
e440a328 | 4332 | |
4333 | bool | |
0c77715b | 4334 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4335 | { |
e440a328 | 4336 | switch (op) |
4337 | { | |
4338 | case OPERATOR_EQEQ: | |
0c77715b | 4339 | return cmp == 0; |
4340 | break; | |
e440a328 | 4341 | case OPERATOR_NOTEQ: |
0c77715b | 4342 | return cmp != 0; |
4343 | break; | |
e440a328 | 4344 | case OPERATOR_LT: |
0c77715b | 4345 | return cmp < 0; |
4346 | break; | |
e440a328 | 4347 | case OPERATOR_LE: |
0c77715b | 4348 | return cmp <= 0; |
e440a328 | 4349 | case OPERATOR_GT: |
0c77715b | 4350 | return cmp > 0; |
e440a328 | 4351 | case OPERATOR_GE: |
0c77715b | 4352 | return cmp >= 0; |
e440a328 | 4353 | default: |
c3e6f413 | 4354 | go_unreachable(); |
e440a328 | 4355 | } |
4356 | } | |
4357 | ||
0c77715b | 4358 | // Compare constants according to OP. |
e440a328 | 4359 | |
4360 | bool | |
0c77715b | 4361 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4362 | Numeric_constant* right_nc, | |
4363 | Location location, bool* result) | |
e440a328 | 4364 | { |
0c77715b | 4365 | Type* left_type = left_nc->type(); |
4366 | Type* right_type = right_nc->type(); | |
4367 | ||
4368 | Type* type; | |
4369 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4370 | return false; | |
4371 | ||
4372 | // When comparing an untyped operand to a typed operand, we are | |
4373 | // effectively coercing the untyped operand to the other operand's | |
4374 | // type, so make sure that is valid. | |
4375 | if (!left_nc->set_type(type, true, location) | |
4376 | || !right_nc->set_type(type, true, location)) | |
4377 | return false; | |
4378 | ||
4379 | bool ret; | |
4380 | int cmp; | |
4381 | if (type->complex_type() != NULL) | |
4382 | { | |
4383 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4384 | return false; | |
4385 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4386 | } | |
4387 | else if (type->float_type() != NULL) | |
4388 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4389 | else |
0c77715b | 4390 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4391 | ||
4392 | if (ret) | |
4393 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4394 | ||
4395 | return ret; | |
4396 | } | |
4397 | ||
4398 | // Compare integer constants. | |
4399 | ||
4400 | bool | |
4401 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4402 | const Numeric_constant* right_nc, | |
4403 | int* cmp) | |
4404 | { | |
4405 | mpz_t left_val; | |
4406 | if (!left_nc->to_int(&left_val)) | |
4407 | return false; | |
4408 | mpz_t right_val; | |
4409 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4410 | { |
0c77715b | 4411 | mpz_clear(left_val); |
4412 | return false; | |
e440a328 | 4413 | } |
0c77715b | 4414 | |
4415 | *cmp = mpz_cmp(left_val, right_val); | |
4416 | ||
4417 | mpz_clear(left_val); | |
4418 | mpz_clear(right_val); | |
4419 | ||
4420 | return true; | |
4421 | } | |
4422 | ||
4423 | // Compare floating point constants. | |
4424 | ||
4425 | bool | |
4426 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4427 | const Numeric_constant* right_nc, | |
4428 | int* cmp) | |
4429 | { | |
4430 | mpfr_t left_val; | |
4431 | if (!left_nc->to_float(&left_val)) | |
4432 | return false; | |
4433 | mpfr_t right_val; | |
4434 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4435 | { |
0c77715b | 4436 | mpfr_clear(left_val); |
4437 | return false; | |
4438 | } | |
4439 | ||
4440 | // We already coerced both operands to the same type. If that type | |
4441 | // is not an abstract type, we need to round the values accordingly. | |
4442 | Type* type = left_nc->type(); | |
4443 | if (!type->is_abstract() && type->float_type() != NULL) | |
4444 | { | |
4445 | int bits = type->float_type()->bits(); | |
4446 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4447 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4448 | } |
0c77715b | 4449 | |
4450 | *cmp = mpfr_cmp(left_val, right_val); | |
4451 | ||
4452 | mpfr_clear(left_val); | |
4453 | mpfr_clear(right_val); | |
4454 | ||
4455 | return true; | |
e440a328 | 4456 | } |
4457 | ||
0c77715b | 4458 | // Compare complex constants. Complex numbers may only be compared |
4459 | // for equality. | |
e440a328 | 4460 | |
4461 | bool | |
0c77715b | 4462 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4463 | const Numeric_constant* right_nc, | |
4464 | int* cmp) | |
e440a328 | 4465 | { |
0c77715b | 4466 | mpfr_t left_real, left_imag; |
4467 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4468 | return false; | |
4469 | mpfr_t right_real, right_imag; | |
4470 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4471 | { |
0c77715b | 4472 | mpfr_clear(left_real); |
4473 | mpfr_clear(left_imag); | |
4474 | return false; | |
e440a328 | 4475 | } |
0c77715b | 4476 | |
4477 | // We already coerced both operands to the same type. If that type | |
4478 | // is not an abstract type, we need to round the values accordingly. | |
4479 | Type* type = left_nc->type(); | |
4480 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4481 | { |
0c77715b | 4482 | int bits = type->complex_type()->bits(); |
4483 | mpfr_prec_round(left_real, bits / 2, GMP_RNDN); | |
4484 | mpfr_prec_round(left_imag, bits / 2, GMP_RNDN); | |
4485 | mpfr_prec_round(right_real, bits / 2, GMP_RNDN); | |
4486 | mpfr_prec_round(right_imag, bits / 2, GMP_RNDN); | |
e440a328 | 4487 | } |
0c77715b | 4488 | |
4489 | *cmp = (mpfr_cmp(left_real, right_real) != 0 | |
4490 | || mpfr_cmp(left_imag, right_imag) != 0); | |
4491 | ||
4492 | mpfr_clear(left_real); | |
4493 | mpfr_clear(left_imag); | |
4494 | mpfr_clear(right_real); | |
4495 | mpfr_clear(right_imag); | |
4496 | ||
4497 | return true; | |
e440a328 | 4498 | } |
4499 | ||
0c77715b | 4500 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4501 | // true if this could be done, false if not. Issue errors at LOCATION | |
4502 | // as appropriate. | |
e440a328 | 4503 | |
4504 | bool | |
0c77715b | 4505 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4506 | Numeric_constant* right_nc, | |
4507 | Location location, Numeric_constant* nc) | |
e440a328 | 4508 | { |
e440a328 | 4509 | switch (op) |
4510 | { | |
4511 | case OPERATOR_OROR: | |
4512 | case OPERATOR_ANDAND: | |
4513 | case OPERATOR_EQEQ: | |
4514 | case OPERATOR_NOTEQ: | |
4515 | case OPERATOR_LT: | |
4516 | case OPERATOR_LE: | |
4517 | case OPERATOR_GT: | |
4518 | case OPERATOR_GE: | |
9767e2d3 | 4519 | // These return boolean values, not numeric. |
4520 | return false; | |
0c77715b | 4521 | default: |
4522 | break; | |
4523 | } | |
4524 | ||
4525 | Type* left_type = left_nc->type(); | |
4526 | Type* right_type = right_nc->type(); | |
4527 | ||
4528 | Type* type; | |
4529 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4530 | return false; | |
4531 | ||
4532 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4533 | ||
4534 | // When combining an untyped operand with a typed operand, we are | |
4535 | // effectively coercing the untyped operand to the other operand's | |
4536 | // type, so make sure that is valid. | |
4537 | if (!left_nc->set_type(type, true, location)) | |
4538 | return false; | |
4539 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4540 | return false; | |
4541 | ||
4542 | bool r; | |
4543 | if (type->complex_type() != NULL) | |
4544 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4545 | else if (type->float_type() != NULL) | |
4546 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4547 | else | |
4548 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4549 | ||
4550 | if (r) | |
4551 | r = nc->set_type(type, true, location); | |
4552 | ||
4553 | return r; | |
4554 | } | |
4555 | ||
4556 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4557 | // integer operations. Return true if this could be done, false if | |
4558 | // not. | |
4559 | ||
4560 | bool | |
4561 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4562 | const Numeric_constant* right_nc, | |
4563 | Location location, Numeric_constant* nc) | |
4564 | { | |
4565 | mpz_t left_val; | |
4566 | if (!left_nc->to_int(&left_val)) | |
4567 | return false; | |
4568 | mpz_t right_val; | |
4569 | if (!right_nc->to_int(&right_val)) | |
4570 | { | |
4571 | mpz_clear(left_val); | |
e440a328 | 4572 | return false; |
0c77715b | 4573 | } |
4574 | ||
4575 | mpz_t val; | |
4576 | mpz_init(val); | |
4577 | ||
4578 | switch (op) | |
4579 | { | |
e440a328 | 4580 | case OPERATOR_PLUS: |
4581 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4582 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4583 | { | |
4584 | error_at(location, "constant addition overflow"); | |
4585 | mpz_set_ui(val, 1); | |
4586 | } | |
e440a328 | 4587 | break; |
4588 | case OPERATOR_MINUS: | |
4589 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4590 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4591 | { | |
4592 | error_at(location, "constant subtraction overflow"); | |
4593 | mpz_set_ui(val, 1); | |
4594 | } | |
e440a328 | 4595 | break; |
4596 | case OPERATOR_OR: | |
4597 | mpz_ior(val, left_val, right_val); | |
4598 | break; | |
4599 | case OPERATOR_XOR: | |
4600 | mpz_xor(val, left_val, right_val); | |
4601 | break; | |
4602 | case OPERATOR_MULT: | |
4603 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4604 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4605 | { | |
4606 | error_at(location, "constant multiplication overflow"); | |
4607 | mpz_set_ui(val, 1); | |
4608 | } | |
e440a328 | 4609 | break; |
4610 | case OPERATOR_DIV: | |
4611 | if (mpz_sgn(right_val) != 0) | |
4612 | mpz_tdiv_q(val, left_val, right_val); | |
4613 | else | |
4614 | { | |
4615 | error_at(location, "division by zero"); | |
4616 | mpz_set_ui(val, 0); | |
e440a328 | 4617 | } |
4618 | break; | |
4619 | case OPERATOR_MOD: | |
4620 | if (mpz_sgn(right_val) != 0) | |
4621 | mpz_tdiv_r(val, left_val, right_val); | |
4622 | else | |
4623 | { | |
4624 | error_at(location, "division by zero"); | |
4625 | mpz_set_ui(val, 0); | |
e440a328 | 4626 | } |
4627 | break; | |
4628 | case OPERATOR_LSHIFT: | |
4629 | { | |
4630 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4631 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4632 | mpz_mul_2exp(val, left_val, shift); | |
4633 | else | |
e440a328 | 4634 | { |
4635 | error_at(location, "shift count overflow"); | |
2c809f8f | 4636 | mpz_set_ui(val, 1); |
e440a328 | 4637 | } |
e440a328 | 4638 | break; |
4639 | } | |
4640 | break; | |
4641 | case OPERATOR_RSHIFT: | |
4642 | { | |
4643 | unsigned long shift = mpz_get_ui(right_val); | |
4644 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4645 | { | |
4646 | error_at(location, "shift count overflow"); | |
2c809f8f | 4647 | mpz_set_ui(val, 1); |
e440a328 | 4648 | } |
e440a328 | 4649 | else |
0c77715b | 4650 | { |
4651 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4652 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4653 | else | |
4654 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4655 | } | |
e440a328 | 4656 | break; |
4657 | } | |
4658 | break; | |
4659 | case OPERATOR_AND: | |
4660 | mpz_and(val, left_val, right_val); | |
4661 | break; | |
4662 | case OPERATOR_BITCLEAR: | |
4663 | { | |
4664 | mpz_t tval; | |
4665 | mpz_init(tval); | |
4666 | mpz_com(tval, right_val); | |
4667 | mpz_and(val, left_val, tval); | |
4668 | mpz_clear(tval); | |
4669 | } | |
4670 | break; | |
4671 | default: | |
c3e6f413 | 4672 | go_unreachable(); |
e440a328 | 4673 | } |
4674 | ||
0c77715b | 4675 | mpz_clear(left_val); |
4676 | mpz_clear(right_val); | |
e440a328 | 4677 | |
0c77715b | 4678 | if (left_nc->is_rune() |
4679 | || (op != OPERATOR_LSHIFT | |
4680 | && op != OPERATOR_RSHIFT | |
4681 | && right_nc->is_rune())) | |
4682 | nc->set_rune(NULL, val); | |
4683 | else | |
4684 | nc->set_int(NULL, val); | |
4685 | ||
4686 | mpz_clear(val); | |
e440a328 | 4687 | |
4688 | return true; | |
4689 | } | |
4690 | ||
0c77715b | 4691 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4692 | // floating point operations. Return true if this could be done, | |
4693 | // false if not. | |
e440a328 | 4694 | |
4695 | bool | |
0c77715b | 4696 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4697 | const Numeric_constant* right_nc, | |
4698 | Location location, Numeric_constant* nc) | |
e440a328 | 4699 | { |
0c77715b | 4700 | mpfr_t left_val; |
4701 | if (!left_nc->to_float(&left_val)) | |
4702 | return false; | |
4703 | mpfr_t right_val; | |
4704 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4705 | { |
0c77715b | 4706 | mpfr_clear(left_val); |
e440a328 | 4707 | return false; |
0c77715b | 4708 | } |
4709 | ||
4710 | mpfr_t val; | |
4711 | mpfr_init(val); | |
4712 | ||
4713 | bool ret = true; | |
4714 | switch (op) | |
4715 | { | |
e440a328 | 4716 | case OPERATOR_PLUS: |
4717 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4718 | break; | |
4719 | case OPERATOR_MINUS: | |
4720 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4721 | break; | |
4722 | case OPERATOR_OR: | |
4723 | case OPERATOR_XOR: | |
4724 | case OPERATOR_AND: | |
4725 | case OPERATOR_BITCLEAR: | |
0c77715b | 4726 | case OPERATOR_MOD: |
4727 | case OPERATOR_LSHIFT: | |
4728 | case OPERATOR_RSHIFT: | |
4729 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4730 | ret = false; | |
4731 | break; | |
e440a328 | 4732 | case OPERATOR_MULT: |
4733 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4734 | break; | |
4735 | case OPERATOR_DIV: | |
0c77715b | 4736 | if (!mpfr_zero_p(right_val)) |
4737 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4738 | else | |
4739 | { | |
4740 | error_at(location, "division by zero"); | |
4741 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4742 | } | |
e440a328 | 4743 | break; |
e440a328 | 4744 | default: |
c3e6f413 | 4745 | go_unreachable(); |
e440a328 | 4746 | } |
4747 | ||
0c77715b | 4748 | mpfr_clear(left_val); |
4749 | mpfr_clear(right_val); | |
e440a328 | 4750 | |
0c77715b | 4751 | nc->set_float(NULL, val); |
4752 | mpfr_clear(val); | |
e440a328 | 4753 | |
0c77715b | 4754 | return ret; |
e440a328 | 4755 | } |
4756 | ||
0c77715b | 4757 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4758 | // complex operations. Return true if this could be done, false if | |
4759 | // not. | |
e440a328 | 4760 | |
4761 | bool | |
0c77715b | 4762 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4763 | const Numeric_constant* right_nc, | |
4764 | Location location, Numeric_constant* nc) | |
e440a328 | 4765 | { |
0c77715b | 4766 | mpfr_t left_real, left_imag; |
4767 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4768 | return false; | |
4769 | mpfr_t right_real, right_imag; | |
4770 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4771 | { |
0c77715b | 4772 | mpfr_clear(left_real); |
4773 | mpfr_clear(left_imag); | |
e440a328 | 4774 | return false; |
0c77715b | 4775 | } |
4776 | ||
4777 | mpfr_t real, imag; | |
4778 | mpfr_init(real); | |
4779 | mpfr_init(imag); | |
4780 | ||
4781 | bool ret = true; | |
4782 | switch (op) | |
4783 | { | |
e440a328 | 4784 | case OPERATOR_PLUS: |
4785 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4786 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4787 | break; | |
4788 | case OPERATOR_MINUS: | |
4789 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4790 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4791 | break; | |
4792 | case OPERATOR_OR: | |
4793 | case OPERATOR_XOR: | |
4794 | case OPERATOR_AND: | |
4795 | case OPERATOR_BITCLEAR: | |
0c77715b | 4796 | case OPERATOR_MOD: |
4797 | case OPERATOR_LSHIFT: | |
4798 | case OPERATOR_RSHIFT: | |
4799 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4800 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4801 | ret = false; | |
4802 | break; | |
e440a328 | 4803 | case OPERATOR_MULT: |
4804 | { | |
4805 | // You might think that multiplying two complex numbers would | |
4806 | // be simple, and you would be right, until you start to think | |
4807 | // about getting the right answer for infinity. If one | |
4808 | // operand here is infinity and the other is anything other | |
4809 | // than zero or NaN, then we are going to wind up subtracting | |
4810 | // two infinity values. That will give us a NaN, but the | |
4811 | // correct answer is infinity. | |
4812 | ||
4813 | mpfr_t lrrr; | |
4814 | mpfr_init(lrrr); | |
4815 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4816 | ||
4817 | mpfr_t lrri; | |
4818 | mpfr_init(lrri); | |
4819 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4820 | ||
4821 | mpfr_t lirr; | |
4822 | mpfr_init(lirr); | |
4823 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4824 | ||
4825 | mpfr_t liri; | |
4826 | mpfr_init(liri); | |
4827 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4828 | ||
4829 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4830 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4831 | ||
4832 | // If we get NaN on both sides, check whether it should really | |
4833 | // be infinity. The rule is that if either side of the | |
4834 | // complex number is infinity, then the whole value is | |
4835 | // infinity, even if the other side is NaN. So the only case | |
4836 | // we have to fix is the one in which both sides are NaN. | |
4837 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4838 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4839 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4840 | { | |
4841 | bool is_infinity = false; | |
4842 | ||
4843 | mpfr_t lr; | |
4844 | mpfr_t li; | |
4845 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4846 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4847 | ||
4848 | mpfr_t rr; | |
4849 | mpfr_t ri; | |
4850 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4851 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4852 | ||
4853 | // If the left side is infinity, then the result is | |
4854 | // infinity. | |
4855 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4856 | { | |
4857 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4858 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4859 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4860 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4861 | if (mpfr_nan_p(rr)) | |
4862 | { | |
4863 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4864 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4865 | } | |
4866 | if (mpfr_nan_p(ri)) | |
4867 | { | |
4868 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4869 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4870 | } | |
4871 | is_infinity = true; | |
4872 | } | |
4873 | ||
4874 | // If the right side is infinity, then the result is | |
4875 | // infinity. | |
4876 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4877 | { | |
4878 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4879 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4880 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4881 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4882 | if (mpfr_nan_p(lr)) | |
4883 | { | |
4884 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4885 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4886 | } | |
4887 | if (mpfr_nan_p(li)) | |
4888 | { | |
4889 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4890 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4891 | } | |
4892 | is_infinity = true; | |
4893 | } | |
4894 | ||
4895 | // If we got an overflow in the intermediate computations, | |
4896 | // then the result is infinity. | |
4897 | if (!is_infinity | |
4898 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4899 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4900 | { | |
4901 | if (mpfr_nan_p(lr)) | |
4902 | { | |
4903 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4904 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4905 | } | |
4906 | if (mpfr_nan_p(li)) | |
4907 | { | |
4908 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4909 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4910 | } | |
4911 | if (mpfr_nan_p(rr)) | |
4912 | { | |
4913 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4914 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4915 | } | |
4916 | if (mpfr_nan_p(ri)) | |
4917 | { | |
4918 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4919 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4920 | } | |
4921 | is_infinity = true; | |
4922 | } | |
4923 | ||
4924 | if (is_infinity) | |
4925 | { | |
4926 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4927 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4928 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4929 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4930 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4931 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4932 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4933 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4934 | } | |
4935 | ||
4936 | mpfr_clear(lr); | |
4937 | mpfr_clear(li); | |
4938 | mpfr_clear(rr); | |
4939 | mpfr_clear(ri); | |
4940 | } | |
4941 | ||
4942 | mpfr_clear(lrrr); | |
4943 | mpfr_clear(lrri); | |
4944 | mpfr_clear(lirr); | |
4945 | mpfr_clear(liri); | |
4946 | } | |
4947 | break; | |
4948 | case OPERATOR_DIV: | |
4949 | { | |
4950 | // For complex division we want to avoid having an | |
4951 | // intermediate overflow turn the whole result in a NaN. We | |
4952 | // scale the values to try to avoid this. | |
4953 | ||
4954 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
0c77715b | 4955 | { |
4956 | error_at(location, "division by zero"); | |
4957 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4958 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4959 | break; | |
4960 | } | |
e440a328 | 4961 | |
4962 | mpfr_t rra; | |
4963 | mpfr_t ria; | |
4964 | mpfr_init(rra); | |
4965 | mpfr_init(ria); | |
4966 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4967 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4968 | mpfr_t t; | |
4969 | mpfr_init(t); | |
4970 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4971 | ||
4972 | mpfr_t rr; | |
4973 | mpfr_t ri; | |
4974 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4975 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4976 | long ilogbw = 0; | |
4977 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4978 | { | |
4979 | ilogbw = mpfr_get_exp(t); | |
4980 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4981 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4982 | } | |
4983 | ||
4984 | mpfr_t denom; | |
4985 | mpfr_init(denom); | |
4986 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4987 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4988 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4989 | ||
4990 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4991 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4992 | mpfr_add(real, real, t, GMP_RNDN); | |
4993 | mpfr_div(real, real, denom, GMP_RNDN); | |
4994 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4995 | ||
4996 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4997 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4998 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4999 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
5000 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
5001 | ||
5002 | // If we wind up with NaN on both sides, check whether we | |
5003 | // should really have infinity. The rule is that if either | |
5004 | // side of the complex number is infinity, then the whole | |
5005 | // value is infinity, even if the other side is NaN. So the | |
5006 | // only case we have to fix is the one in which both sides are | |
5007 | // NaN. | |
5008 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
5009 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
5010 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
5011 | { | |
5012 | if (mpfr_zero_p(denom)) | |
5013 | { | |
5014 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
5015 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
5016 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
5017 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
5018 | } | |
5019 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
5020 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
5021 | { | |
5022 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
5023 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
5024 | ||
5025 | mpfr_t t2; | |
5026 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
5027 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
5028 | ||
5029 | mpfr_t t3; | |
5030 | mpfr_init(t3); | |
5031 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
5032 | ||
5033 | mpfr_t t4; | |
5034 | mpfr_init(t4); | |
5035 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
5036 | ||
5037 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5038 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
5039 | ||
5040 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
5041 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
5042 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5043 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
5044 | ||
5045 | mpfr_clear(t2); | |
5046 | mpfr_clear(t3); | |
5047 | mpfr_clear(t4); | |
5048 | } | |
5049 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
5050 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
5051 | { | |
5052 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
5053 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
5054 | ||
5055 | mpfr_t t2; | |
5056 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5057 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
5058 | ||
5059 | mpfr_t t3; | |
5060 | mpfr_init(t3); | |
5061 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
5062 | ||
5063 | mpfr_t t4; | |
5064 | mpfr_init(t4); | |
5065 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
5066 | ||
5067 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5068 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5069 | mpfr_mul(real, real, t3, GMP_RNDN); | |
5070 | ||
5071 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
5072 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
5073 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5074 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5075 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
5076 | ||
5077 | mpfr_clear(t2); | |
5078 | mpfr_clear(t3); | |
5079 | mpfr_clear(t4); | |
5080 | } | |
5081 | } | |
5082 | ||
5083 | mpfr_clear(denom); | |
5084 | mpfr_clear(rr); | |
5085 | mpfr_clear(ri); | |
5086 | mpfr_clear(t); | |
5087 | mpfr_clear(rra); | |
5088 | mpfr_clear(ria); | |
5089 | } | |
5090 | break; | |
e440a328 | 5091 | default: |
c3e6f413 | 5092 | go_unreachable(); |
e440a328 | 5093 | } |
5094 | ||
0c77715b | 5095 | mpfr_clear(left_real); |
5096 | mpfr_clear(left_imag); | |
5097 | mpfr_clear(right_real); | |
5098 | mpfr_clear(right_imag); | |
e440a328 | 5099 | |
0c77715b | 5100 | nc->set_complex(NULL, real, imag); |
5101 | mpfr_clear(real); | |
5102 | mpfr_clear(imag); | |
e440a328 | 5103 | |
0c77715b | 5104 | return ret; |
e440a328 | 5105 | } |
5106 | ||
5107 | // Lower a binary expression. We have to evaluate constant | |
5108 | // expressions now, in order to implement Go's unlimited precision | |
5109 | // constants. | |
5110 | ||
5111 | Expression* | |
e9d3367e | 5112 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5113 | Statement_inserter* inserter, int) | |
e440a328 | 5114 | { |
b13c66cd | 5115 | Location location = this->location(); |
e440a328 | 5116 | Operator op = this->op_; |
5117 | Expression* left = this->left_; | |
5118 | Expression* right = this->right_; | |
5119 | ||
5120 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5121 | || op == OPERATOR_NOTEQ | |
5122 | || op == OPERATOR_LT | |
5123 | || op == OPERATOR_LE | |
5124 | || op == OPERATOR_GT | |
5125 | || op == OPERATOR_GE); | |
5126 | ||
0c77715b | 5127 | // Numeric constant expressions. |
e440a328 | 5128 | { |
0c77715b | 5129 | Numeric_constant left_nc; |
5130 | Numeric_constant right_nc; | |
5131 | if (left->numeric_constant_value(&left_nc) | |
5132 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5133 | { |
0c77715b | 5134 | if (is_comparison) |
e440a328 | 5135 | { |
0c77715b | 5136 | bool result; |
5137 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5138 | &right_nc, location, | |
5139 | &result)) | |
5140 | return this; | |
e90c9dfc | 5141 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5142 | Expression::make_boolean(result, |
5143 | location), | |
5144 | location); | |
e440a328 | 5145 | } |
5146 | else | |
5147 | { | |
0c77715b | 5148 | Numeric_constant nc; |
5149 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5150 | location, &nc)) | |
5151 | return this; | |
5152 | return nc.expression(location); | |
e440a328 | 5153 | } |
5154 | } | |
e440a328 | 5155 | } |
5156 | ||
5157 | // String constant expressions. | |
315fa98d | 5158 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5159 | { |
5160 | std::string left_string; | |
5161 | std::string right_string; | |
5162 | if (left->string_constant_value(&left_string) | |
5163 | && right->string_constant_value(&right_string)) | |
315fa98d | 5164 | { |
5165 | if (op == OPERATOR_PLUS) | |
5166 | return Expression::make_string(left_string + right_string, | |
5167 | location); | |
5168 | else if (is_comparison) | |
5169 | { | |
5170 | int cmp = left_string.compare(right_string); | |
0c77715b | 5171 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5172 | return Expression::make_boolean(r, location); |
b40dc774 | 5173 | } |
5174 | } | |
b40dc774 | 5175 | } |
5176 | ||
ceeb12d7 | 5177 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5178 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5179 | { | |
5180 | if (left->type()->struct_type() != NULL) | |
5181 | return this->lower_struct_comparison(gogo, inserter); | |
5182 | else if (left->type()->array_type() != NULL | |
5183 | && !left->type()->is_slice_type()) | |
5184 | return this->lower_array_comparison(gogo, inserter); | |
ceeb12d7 | 5185 | else if ((left->type()->interface_type() != NULL |
5186 | && right->type()->interface_type() == NULL) | |
5187 | || (left->type()->interface_type() == NULL | |
5188 | && right->type()->interface_type() != NULL)) | |
5189 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5190 | } |
5191 | ||
e440a328 | 5192 | return this; |
5193 | } | |
5194 | ||
e9d3367e | 5195 | // Lower a struct comparison. |
5196 | ||
5197 | Expression* | |
5198 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5199 | Statement_inserter* inserter) | |
5200 | { | |
5201 | Struct_type* st = this->left_->type()->struct_type(); | |
5202 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5203 | if (st2 == NULL) | |
5204 | return this; | |
5205 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5206 | return this; | |
5207 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5208 | this->right_->type(), NULL)) | |
5209 | return this; | |
5210 | ||
5211 | // See if we can compare using memcmp. As a heuristic, we use | |
5212 | // memcmp rather than field references and comparisons if there are | |
5213 | // more than two fields. | |
113ef6a5 | 5214 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5215 | return this->lower_compare_to_memcmp(gogo, inserter); |
5216 | ||
5217 | Location loc = this->location(); | |
5218 | ||
5219 | Expression* left = this->left_; | |
5220 | Temporary_statement* left_temp = NULL; | |
5221 | if (left->var_expression() == NULL | |
5222 | && left->temporary_reference_expression() == NULL) | |
5223 | { | |
5224 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5225 | inserter->insert(left_temp); | |
5226 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5227 | } | |
5228 | ||
5229 | Expression* right = this->right_; | |
5230 | Temporary_statement* right_temp = NULL; | |
5231 | if (right->var_expression() == NULL | |
5232 | && right->temporary_reference_expression() == NULL) | |
5233 | { | |
5234 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5235 | inserter->insert(right_temp); | |
5236 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5237 | } | |
5238 | ||
5239 | Expression* ret = Expression::make_boolean(true, loc); | |
5240 | const Struct_field_list* fields = st->fields(); | |
5241 | unsigned int field_index = 0; | |
5242 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5243 | pf != fields->end(); | |
5244 | ++pf, ++field_index) | |
5245 | { | |
f5165c05 | 5246 | if (Gogo::is_sink_name(pf->field_name())) |
5247 | continue; | |
5248 | ||
e9d3367e | 5249 | if (field_index > 0) |
5250 | { | |
5251 | if (left_temp == NULL) | |
5252 | left = left->copy(); | |
5253 | else | |
5254 | left = Expression::make_temporary_reference(left_temp, loc); | |
5255 | if (right_temp == NULL) | |
5256 | right = right->copy(); | |
5257 | else | |
5258 | right = Expression::make_temporary_reference(right_temp, loc); | |
5259 | } | |
5260 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5261 | loc); | |
5262 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5263 | loc); | |
5264 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5265 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5266 | } | |
5267 | ||
5268 | if (this->op_ == OPERATOR_NOTEQ) | |
5269 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5270 | ||
5271 | return ret; | |
5272 | } | |
5273 | ||
5274 | // Lower an array comparison. | |
5275 | ||
5276 | Expression* | |
5277 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5278 | Statement_inserter* inserter) | |
5279 | { | |
5280 | Array_type* at = this->left_->type()->array_type(); | |
5281 | Array_type* at2 = this->right_->type()->array_type(); | |
5282 | if (at2 == NULL) | |
5283 | return this; | |
5284 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5285 | return this; | |
5286 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5287 | this->right_->type(), NULL)) | |
5288 | return this; | |
5289 | ||
5290 | // Call memcmp directly if possible. This may let the middle-end | |
5291 | // optimize the call. | |
113ef6a5 | 5292 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5293 | return this->lower_compare_to_memcmp(gogo, inserter); |
5294 | ||
5295 | // Call the array comparison function. | |
5296 | Named_object* hash_fn; | |
5297 | Named_object* equal_fn; | |
5298 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5299 | &hash_fn, &equal_fn); | |
5300 | ||
5301 | Location loc = this->location(); | |
5302 | ||
5303 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5304 | ||
5305 | Expression_list* args = new Expression_list(); | |
5306 | args->push_back(this->operand_address(inserter, this->left_)); | |
5307 | args->push_back(this->operand_address(inserter, this->right_)); | |
5308 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5309 | ||
5310 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5311 | ||
5312 | if (this->op_ == OPERATOR_NOTEQ) | |
5313 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5314 | ||
5315 | return ret; | |
5316 | } | |
5317 | ||
ceeb12d7 | 5318 | // Lower an interface to value comparison. |
5319 | ||
5320 | Expression* | |
5321 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5322 | Statement_inserter* inserter) | |
5323 | { | |
5324 | Type* left_type = this->left_->type(); | |
5325 | Type* right_type = this->right_->type(); | |
5326 | Interface_type* ift; | |
5327 | if (left_type->interface_type() != NULL) | |
5328 | { | |
5329 | ift = left_type->interface_type(); | |
5330 | if (!ift->implements_interface(right_type, NULL)) | |
5331 | return this; | |
5332 | } | |
5333 | else | |
5334 | { | |
5335 | ift = right_type->interface_type(); | |
5336 | if (!ift->implements_interface(left_type, NULL)) | |
5337 | return this; | |
5338 | } | |
5339 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5340 | return this; | |
5341 | ||
5342 | Location loc = this->location(); | |
5343 | ||
5344 | if (left_type->interface_type() == NULL | |
5345 | && left_type->points_to() == NULL | |
5346 | && !this->left_->is_addressable()) | |
5347 | { | |
5348 | Temporary_statement* temp = | |
5349 | Statement::make_temporary(left_type, NULL, loc); | |
5350 | inserter->insert(temp); | |
5351 | this->left_ = | |
5352 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5353 | } | |
5354 | ||
5355 | if (right_type->interface_type() == NULL | |
5356 | && right_type->points_to() == NULL | |
5357 | && !this->right_->is_addressable()) | |
5358 | { | |
5359 | Temporary_statement* temp = | |
5360 | Statement::make_temporary(right_type, NULL, loc); | |
5361 | inserter->insert(temp); | |
5362 | this->right_ = | |
5363 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5364 | } | |
5365 | ||
5366 | return this; | |
5367 | } | |
5368 | ||
e9d3367e | 5369 | // Lower a struct or array comparison to a call to memcmp. |
5370 | ||
5371 | Expression* | |
5372 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5373 | { | |
5374 | Location loc = this->location(); | |
5375 | ||
5376 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5377 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5378 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5379 | TYPE_INFO_SIZE); | |
5380 | ||
5381 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
5382 | ||
5383 | mpz_t zval; | |
5384 | mpz_init_set_ui(zval, 0); | |
5385 | Expression* zero = Expression::make_integer(&zval, NULL, loc); | |
5386 | mpz_clear(zval); | |
5387 | ||
5388 | return Expression::make_binary(this->op_, call, zero, loc); | |
5389 | } | |
5390 | ||
a32698ee | 5391 | Expression* |
5c3f3470 | 5392 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5393 | Statement_inserter* inserter) |
5394 | { | |
5395 | Location loc = this->location(); | |
5396 | Temporary_statement* temp; | |
5397 | if (this->left_->type()->is_string_type() | |
5398 | && this->op_ == OPERATOR_PLUS) | |
5399 | { | |
5400 | if (!this->left_->is_variable()) | |
5401 | { | |
5402 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5403 | inserter->insert(temp); | |
5404 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5405 | } | |
5406 | if (!this->right_->is_variable()) | |
5407 | { | |
5408 | temp = | |
5409 | Statement::make_temporary(this->left_->type(), this->right_, loc); | |
5410 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5411 | inserter->insert(temp); | |
5412 | } | |
5413 | } | |
5414 | ||
5415 | Type* left_type = this->left_->type(); | |
5416 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5417 | || this->op_ == OPERATOR_RSHIFT); | |
5418 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5419 | left_type->integer_type() != NULL) | |
5420 | || this->op_ == OPERATOR_MOD); | |
5421 | ||
a32698ee | 5422 | if (is_shift_op |
5c3f3470 | 5423 | || (is_idiv_op |
5424 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5425 | { |
5426 | if (!this->left_->is_variable()) | |
5427 | { | |
5428 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5429 | inserter->insert(temp); | |
5430 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5431 | } | |
5432 | if (!this->right_->is_variable()) | |
5433 | { | |
5434 | temp = | |
5435 | Statement::make_temporary(NULL, this->right_, loc); | |
5436 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5437 | inserter->insert(temp); | |
5438 | } | |
5439 | } | |
5440 | return this; | |
5441 | } | |
5442 | ||
5443 | ||
e9d3367e | 5444 | // Return the address of EXPR, cast to unsafe.Pointer. |
5445 | ||
5446 | Expression* | |
5447 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5448 | Expression* expr) | |
5449 | { | |
5450 | Location loc = this->location(); | |
5451 | ||
5452 | if (!expr->is_addressable()) | |
5453 | { | |
5454 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5455 | loc); | |
5456 | inserter->insert(temp); | |
5457 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5458 | } | |
5459 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5460 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5461 | Type* void_type = Type::make_void_type(); | |
5462 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5463 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5464 | } | |
5465 | ||
0c77715b | 5466 | // Return the numeric constant value, if it has one. |
e440a328 | 5467 | |
5468 | bool | |
0c77715b | 5469 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5470 | { |
0c77715b | 5471 | Numeric_constant left_nc; |
5472 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5473 | return false; | |
5474 | Numeric_constant right_nc; | |
5475 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5476 | return false; | |
9767e2d3 | 5477 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5478 | this->location(), nc); |
e440a328 | 5479 | } |
5480 | ||
5481 | // Note that the value is being discarded. | |
5482 | ||
4f2138d7 | 5483 | bool |
e440a328 | 5484 | Binary_expression::do_discarding_value() |
5485 | { | |
5486 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5487 | return this->right_->discarding_value(); |
e440a328 | 5488 | else |
4f2138d7 | 5489 | { |
5490 | this->unused_value_error(); | |
5491 | return false; | |
5492 | } | |
e440a328 | 5493 | } |
5494 | ||
5495 | // Get type. | |
5496 | ||
5497 | Type* | |
5498 | Binary_expression::do_type() | |
5499 | { | |
5f5fea79 | 5500 | if (this->classification() == EXPRESSION_ERROR) |
5501 | return Type::make_error_type(); | |
5502 | ||
e440a328 | 5503 | switch (this->op_) |
5504 | { | |
e440a328 | 5505 | case OPERATOR_EQEQ: |
5506 | case OPERATOR_NOTEQ: | |
5507 | case OPERATOR_LT: | |
5508 | case OPERATOR_LE: | |
5509 | case OPERATOR_GT: | |
5510 | case OPERATOR_GE: | |
e90c9dfc | 5511 | if (this->type_ == NULL) |
5512 | this->type_ = Type::make_boolean_type(); | |
5513 | return this->type_; | |
e440a328 | 5514 | |
5515 | case OPERATOR_PLUS: | |
5516 | case OPERATOR_MINUS: | |
5517 | case OPERATOR_OR: | |
5518 | case OPERATOR_XOR: | |
5519 | case OPERATOR_MULT: | |
5520 | case OPERATOR_DIV: | |
5521 | case OPERATOR_MOD: | |
5522 | case OPERATOR_AND: | |
5523 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5524 | case OPERATOR_OROR: |
5525 | case OPERATOR_ANDAND: | |
e440a328 | 5526 | { |
0c77715b | 5527 | Type* type; |
5528 | if (!Binary_expression::operation_type(this->op_, | |
5529 | this->left_->type(), | |
5530 | this->right_->type(), | |
5531 | &type)) | |
5532 | return Type::make_error_type(); | |
5533 | return type; | |
e440a328 | 5534 | } |
5535 | ||
5536 | case OPERATOR_LSHIFT: | |
5537 | case OPERATOR_RSHIFT: | |
5538 | return this->left_->type(); | |
5539 | ||
5540 | default: | |
c3e6f413 | 5541 | go_unreachable(); |
e440a328 | 5542 | } |
5543 | } | |
5544 | ||
5545 | // Set type for a binary expression. | |
5546 | ||
5547 | void | |
5548 | Binary_expression::do_determine_type(const Type_context* context) | |
5549 | { | |
5550 | Type* tleft = this->left_->type(); | |
5551 | Type* tright = this->right_->type(); | |
5552 | ||
5553 | // Both sides should have the same type, except for the shift | |
5554 | // operations. For a comparison, we should ignore the incoming | |
5555 | // type. | |
5556 | ||
5557 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5558 | || this->op_ == OPERATOR_RSHIFT); | |
5559 | ||
5560 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5561 | || this->op_ == OPERATOR_NOTEQ | |
5562 | || this->op_ == OPERATOR_LT | |
5563 | || this->op_ == OPERATOR_LE | |
5564 | || this->op_ == OPERATOR_GT | |
5565 | || this->op_ == OPERATOR_GE); | |
5566 | ||
5567 | Type_context subcontext(*context); | |
5568 | ||
5569 | if (is_comparison) | |
5570 | { | |
5571 | // In a comparison, the context does not determine the types of | |
5572 | // the operands. | |
5573 | subcontext.type = NULL; | |
5574 | } | |
5575 | ||
02ffd97f | 5576 | if (this->op_ == OPERATOR_ANDAND || this->op_ == OPERATOR_OROR) |
5577 | { | |
5578 | // For a logical operation, the context does not determine the | |
5579 | // types of the operands. The operands must be some boolean | |
5580 | // type but if the context has a boolean type they do not | |
5581 | // inherit it. See http://golang.org/issue/3924. | |
5582 | subcontext.type = NULL; | |
5583 | } | |
5584 | ||
e440a328 | 5585 | // Set the context for the left hand operand. |
5586 | if (is_shift_op) | |
5587 | { | |
b40dc774 | 5588 | // The right hand operand of a shift plays no role in |
5589 | // determining the type of the left hand operand. | |
e440a328 | 5590 | } |
5591 | else if (!tleft->is_abstract()) | |
5592 | subcontext.type = tleft; | |
5593 | else if (!tright->is_abstract()) | |
5594 | subcontext.type = tright; | |
5595 | else if (subcontext.type == NULL) | |
5596 | { | |
5597 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5598 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5599 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5600 | { | |
5601 | // Both sides have an abstract integer, abstract float, or | |
5602 | // abstract complex type. Just let CONTEXT determine | |
5603 | // whether they may remain abstract or not. | |
5604 | } | |
5605 | else if (tleft->complex_type() != NULL) | |
5606 | subcontext.type = tleft; | |
5607 | else if (tright->complex_type() != NULL) | |
5608 | subcontext.type = tright; | |
5609 | else if (tleft->float_type() != NULL) | |
5610 | subcontext.type = tleft; | |
5611 | else if (tright->float_type() != NULL) | |
5612 | subcontext.type = tright; | |
5613 | else | |
5614 | subcontext.type = tleft; | |
f58a23ae | 5615 | |
5616 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5617 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5618 | } |
5619 | ||
5620 | this->left_->determine_type(&subcontext); | |
5621 | ||
e440a328 | 5622 | if (is_shift_op) |
5623 | { | |
b40dc774 | 5624 | // We may have inherited an unusable type for the shift operand. |
5625 | // Give a useful error if that happened. | |
5626 | if (tleft->is_abstract() | |
5627 | && subcontext.type != NULL | |
8ab6effb | 5628 | && !subcontext.may_be_abstract |
f6bc81e6 | 5629 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5630 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5631 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5632 | "for left operand of shift")); |
b40dc774 | 5633 | |
5634 | // The context for the right hand operand is the same as for the | |
5635 | // left hand operand, except for a shift operator. | |
e440a328 | 5636 | subcontext.type = Type::lookup_integer_type("uint"); |
5637 | subcontext.may_be_abstract = false; | |
5638 | } | |
5639 | ||
5640 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5641 | |
5642 | if (is_comparison) | |
5643 | { | |
5644 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5645 | ; | |
5646 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5647 | this->type_ = context->type; | |
5648 | else if (!context->may_be_abstract) | |
5649 | this->type_ = Type::lookup_bool_type(); | |
5650 | } | |
e440a328 | 5651 | } |
5652 | ||
5653 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5654 | // OTYPE is the type of the other operand. Return whether the |
5655 | // operation is OK. This should not be used for shift. | |
e440a328 | 5656 | |
5657 | bool | |
be8b5eee | 5658 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5659 | Location location) |
e440a328 | 5660 | { |
5661 | switch (op) | |
5662 | { | |
5663 | case OPERATOR_OROR: | |
5664 | case OPERATOR_ANDAND: | |
5665 | if (!type->is_boolean_type()) | |
5666 | { | |
5667 | error_at(location, "expected boolean type"); | |
5668 | return false; | |
5669 | } | |
5670 | break; | |
5671 | ||
5672 | case OPERATOR_EQEQ: | |
5673 | case OPERATOR_NOTEQ: | |
e9d3367e | 5674 | { |
5675 | std::string reason; | |
5676 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5677 | { | |
5678 | error_at(location, "%s", reason.c_str()); | |
5679 | return false; | |
5680 | } | |
5681 | } | |
e440a328 | 5682 | break; |
5683 | ||
5684 | case OPERATOR_LT: | |
5685 | case OPERATOR_LE: | |
5686 | case OPERATOR_GT: | |
5687 | case OPERATOR_GE: | |
e9d3367e | 5688 | { |
5689 | std::string reason; | |
5690 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5691 | { | |
5692 | error_at(location, "%s", reason.c_str()); | |
5693 | return false; | |
5694 | } | |
5695 | } | |
e440a328 | 5696 | break; |
5697 | ||
5698 | case OPERATOR_PLUS: | |
5699 | case OPERATOR_PLUSEQ: | |
5700 | if (type->integer_type() == NULL | |
5701 | && type->float_type() == NULL | |
5702 | && type->complex_type() == NULL | |
5703 | && !type->is_string_type()) | |
5704 | { | |
5705 | error_at(location, | |
5706 | "expected integer, floating, complex, or string type"); | |
5707 | return false; | |
5708 | } | |
5709 | break; | |
5710 | ||
5711 | case OPERATOR_MINUS: | |
5712 | case OPERATOR_MINUSEQ: | |
5713 | case OPERATOR_MULT: | |
5714 | case OPERATOR_MULTEQ: | |
5715 | case OPERATOR_DIV: | |
5716 | case OPERATOR_DIVEQ: | |
5717 | if (type->integer_type() == NULL | |
5718 | && type->float_type() == NULL | |
5719 | && type->complex_type() == NULL) | |
5720 | { | |
5721 | error_at(location, "expected integer, floating, or complex type"); | |
5722 | return false; | |
5723 | } | |
5724 | break; | |
5725 | ||
5726 | case OPERATOR_MOD: | |
5727 | case OPERATOR_MODEQ: | |
5728 | case OPERATOR_OR: | |
5729 | case OPERATOR_OREQ: | |
5730 | case OPERATOR_AND: | |
5731 | case OPERATOR_ANDEQ: | |
5732 | case OPERATOR_XOR: | |
5733 | case OPERATOR_XOREQ: | |
5734 | case OPERATOR_BITCLEAR: | |
5735 | case OPERATOR_BITCLEAREQ: | |
5736 | if (type->integer_type() == NULL) | |
5737 | { | |
5738 | error_at(location, "expected integer type"); | |
5739 | return false; | |
5740 | } | |
5741 | break; | |
5742 | ||
5743 | default: | |
c3e6f413 | 5744 | go_unreachable(); |
e440a328 | 5745 | } |
5746 | ||
5747 | return true; | |
5748 | } | |
5749 | ||
5750 | // Check types. | |
5751 | ||
5752 | void | |
5753 | Binary_expression::do_check_types(Gogo*) | |
5754 | { | |
5f5fea79 | 5755 | if (this->classification() == EXPRESSION_ERROR) |
5756 | return; | |
5757 | ||
e440a328 | 5758 | Type* left_type = this->left_->type(); |
5759 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5760 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5761 | { |
5762 | this->set_is_error(); | |
5763 | return; | |
5764 | } | |
e440a328 | 5765 | |
5766 | if (this->op_ == OPERATOR_EQEQ | |
5767 | || this->op_ == OPERATOR_NOTEQ | |
5768 | || this->op_ == OPERATOR_LT | |
5769 | || this->op_ == OPERATOR_LE | |
5770 | || this->op_ == OPERATOR_GT | |
5771 | || this->op_ == OPERATOR_GE) | |
5772 | { | |
907c5ecd | 5773 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5774 | { | |
5775 | this->report_error(_("invalid comparison of nil with nil")); | |
5776 | return; | |
5777 | } | |
e440a328 | 5778 | if (!Type::are_assignable(left_type, right_type, NULL) |
5779 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5780 | { | |
5781 | this->report_error(_("incompatible types in binary expression")); | |
5782 | return; | |
5783 | } | |
5784 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5785 | right_type, |
e440a328 | 5786 | this->location()) |
5787 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5788 | left_type, |
e440a328 | 5789 | this->location())) |
5790 | { | |
5791 | this->set_is_error(); | |
5792 | return; | |
5793 | } | |
5794 | } | |
5795 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5796 | { | |
5797 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5798 | { | |
5799 | this->report_error(_("incompatible types in binary expression")); | |
5800 | return; | |
5801 | } | |
5802 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5803 | right_type, |
e440a328 | 5804 | this->location())) |
5805 | { | |
5806 | this->set_is_error(); | |
5807 | return; | |
5808 | } | |
5c65b19d | 5809 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5810 | { | |
5811 | // Division by a zero integer constant is an error. | |
5812 | Numeric_constant rconst; | |
5813 | unsigned long rval; | |
5814 | if (left_type->integer_type() != NULL | |
5815 | && this->right_->numeric_constant_value(&rconst) | |
5816 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5817 | && rval == 0) | |
5818 | { | |
5819 | this->report_error(_("integer division by zero")); | |
5820 | return; | |
5821 | } | |
5822 | } | |
e440a328 | 5823 | } |
5824 | else | |
5825 | { | |
5826 | if (left_type->integer_type() == NULL) | |
5827 | this->report_error(_("shift of non-integer operand")); | |
5828 | ||
5829 | if (!right_type->is_abstract() | |
5830 | && (right_type->integer_type() == NULL | |
5831 | || !right_type->integer_type()->is_unsigned())) | |
5832 | this->report_error(_("shift count not unsigned integer")); | |
5833 | else | |
5834 | { | |
0c77715b | 5835 | Numeric_constant nc; |
5836 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5837 | { |
0c77715b | 5838 | mpz_t val; |
5839 | if (!nc.to_int(&val)) | |
5840 | this->report_error(_("shift count not unsigned integer")); | |
5841 | else | |
a4eba91b | 5842 | { |
0c77715b | 5843 | if (mpz_sgn(val) < 0) |
5844 | { | |
5845 | this->report_error(_("negative shift count")); | |
5846 | mpz_set_ui(val, 0); | |
5847 | Location rloc = this->right_->location(); | |
5848 | this->right_ = Expression::make_integer(&val, right_type, | |
5849 | rloc); | |
5850 | } | |
5851 | mpz_clear(val); | |
a4eba91b | 5852 | } |
e440a328 | 5853 | } |
e440a328 | 5854 | } |
5855 | } | |
5856 | } | |
5857 | ||
ea664253 | 5858 | // Get the backend representation for a binary expression. |
e440a328 | 5859 | |
ea664253 | 5860 | Bexpression* |
5861 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 5862 | { |
1b1f2abf | 5863 | Gogo* gogo = context->gogo(); |
a32698ee | 5864 | Location loc = this->location(); |
5865 | Type* left_type = this->left_->type(); | |
5866 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5867 | |
e440a328 | 5868 | bool use_left_type = true; |
5869 | bool is_shift_op = false; | |
29a2d1d8 | 5870 | bool is_idiv_op = false; |
e440a328 | 5871 | switch (this->op_) |
5872 | { | |
5873 | case OPERATOR_EQEQ: | |
5874 | case OPERATOR_NOTEQ: | |
5875 | case OPERATOR_LT: | |
5876 | case OPERATOR_LE: | |
5877 | case OPERATOR_GT: | |
5878 | case OPERATOR_GE: | |
ea664253 | 5879 | return Expression::comparison(context, this->type_, this->op_, |
5880 | this->left_, this->right_, loc); | |
e440a328 | 5881 | |
5882 | case OPERATOR_OROR: | |
e440a328 | 5883 | case OPERATOR_ANDAND: |
e440a328 | 5884 | use_left_type = false; |
5885 | break; | |
5886 | case OPERATOR_PLUS: | |
e440a328 | 5887 | case OPERATOR_MINUS: |
e440a328 | 5888 | case OPERATOR_OR: |
e440a328 | 5889 | case OPERATOR_XOR: |
e440a328 | 5890 | case OPERATOR_MULT: |
e440a328 | 5891 | break; |
5892 | case OPERATOR_DIV: | |
a32698ee | 5893 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
5894 | break; | |
e440a328 | 5895 | case OPERATOR_MOD: |
29a2d1d8 | 5896 | is_idiv_op = true; |
e440a328 | 5897 | break; |
5898 | case OPERATOR_LSHIFT: | |
e440a328 | 5899 | case OPERATOR_RSHIFT: |
e440a328 | 5900 | is_shift_op = true; |
5901 | break; | |
e440a328 | 5902 | case OPERATOR_BITCLEAR: |
a32698ee | 5903 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
5904 | case OPERATOR_AND: | |
e440a328 | 5905 | break; |
5906 | default: | |
c3e6f413 | 5907 | go_unreachable(); |
e440a328 | 5908 | } |
5909 | ||
a32698ee | 5910 | if (left_type->is_string_type()) |
e440a328 | 5911 | { |
c484d925 | 5912 | go_assert(this->op_ == OPERATOR_PLUS); |
a32698ee | 5913 | Expression* string_plus = |
5914 | Runtime::make_call(Runtime::STRING_PLUS, loc, 2, | |
5915 | this->left_, this->right_); | |
ea664253 | 5916 | return string_plus->get_backend(context); |
a32698ee | 5917 | } |
5918 | ||
5919 | // For complex division Go might want slightly different results than the | |
5920 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 5921 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
5922 | { | |
a32698ee | 5923 | Runtime::Function complex_code; |
1850e20c | 5924 | switch (this->left_->type()->complex_type()->bits()) |
5925 | { | |
5926 | case 64: | |
a32698ee | 5927 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 5928 | break; |
5929 | case 128: | |
a32698ee | 5930 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 5931 | break; |
5932 | default: | |
5933 | go_unreachable(); | |
5934 | } | |
a32698ee | 5935 | Expression* complex_div = |
5936 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 5937 | return complex_div->get_backend(context); |
1850e20c | 5938 | } |
5939 | ||
ea664253 | 5940 | Bexpression* left = this->left_->get_backend(context); |
5941 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 5942 | |
a32698ee | 5943 | Type* type = use_left_type ? left_type : right_type; |
5944 | Btype* btype = type->get_backend(gogo); | |
5945 | ||
5946 | Bexpression* ret = | |
5947 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
5948 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 5949 | |
a32698ee | 5950 | // Initialize overflow constants. |
5951 | Bexpression* overflow; | |
5952 | mpz_t zero; | |
5953 | mpz_init_set_ui(zero, 0UL); | |
5954 | mpz_t one; | |
5955 | mpz_init_set_ui(one, 1UL); | |
5956 | mpz_t neg_one; | |
5957 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 5958 | |
a32698ee | 5959 | Btype* left_btype = left_type->get_backend(gogo); |
5960 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 5961 | |
5962 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 5963 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 5964 | if (is_shift_op) |
5965 | { | |
a32698ee | 5966 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 5967 | |
a32698ee | 5968 | mpz_t bitsval; |
5969 | int bits = left_type->integer_type()->bits(); | |
5970 | mpz_init_set_ui(bitsval, bits); | |
5971 | Bexpression* bits_expr = | |
5972 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
5973 | Bexpression* compare = | |
5974 | gogo->backend()->binary_expression(OPERATOR_LT, | |
5975 | right, bits_expr, loc); | |
e440a328 | 5976 | |
a32698ee | 5977 | Bexpression* zero_expr = |
5978 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
5979 | overflow = zero_expr; | |
e440a328 | 5980 | if (this->op_ == OPERATOR_RSHIFT |
a32698ee | 5981 | && !left_type->integer_type()->is_unsigned()) |
e440a328 | 5982 | { |
a32698ee | 5983 | Bexpression* neg_expr = |
5984 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
5985 | zero_expr, loc); | |
5986 | Bexpression* neg_one_expr = | |
5987 | gogo->backend()->integer_constant_expression(left_btype, neg_one); | |
5988 | overflow = gogo->backend()->conditional_expression(btype, neg_expr, | |
5989 | neg_one_expr, | |
5990 | zero_expr, loc); | |
29a2d1d8 | 5991 | } |
a32698ee | 5992 | ret = gogo->backend()->conditional_expression(btype, compare, ret, |
5993 | overflow, loc); | |
5994 | mpz_clear(bitsval); | |
29a2d1d8 | 5995 | } |
5996 | ||
5997 | // Add checks for division by zero and division overflow as needed. | |
5998 | if (is_idiv_op) | |
5999 | { | |
5c3f3470 | 6000 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 6001 | { |
6002 | // right == 0 | |
a32698ee | 6003 | Bexpression* zero_expr = |
6004 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
6005 | Bexpression* check = | |
6006 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6007 | right, zero_expr, loc); | |
29a2d1d8 | 6008 | |
a32698ee | 6009 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 6010 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 6011 | Bexpression* crash = gogo->runtime_error(errcode, |
6012 | loc)->get_backend(context); | |
29a2d1d8 | 6013 | |
6014 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
ea664253 | 6015 | ret = gogo->backend()->conditional_expression(btype, check, crash, |
6016 | ret, loc); | |
b13c66cd | 6017 | } |
6018 | ||
5c3f3470 | 6019 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 6020 | { |
6021 | // right == -1 | |
6022 | // FIXME: It would be nice to say that this test is expected | |
6023 | // to return false. | |
a32698ee | 6024 | |
6025 | Bexpression* neg_one_expr = | |
6026 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6027 | Bexpression* check = | |
6028 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6029 | right, neg_one_expr, loc); | |
6030 | ||
6031 | Bexpression* zero_expr = | |
6032 | gogo->backend()->integer_constant_expression(btype, zero); | |
6033 | Bexpression* one_expr = | |
6034 | gogo->backend()->integer_constant_expression(btype, one); | |
6035 | ||
6036 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6037 | { |
6038 | // An unsigned -1 is the largest possible number, so | |
6039 | // dividing is always 1 or 0. | |
a32698ee | 6040 | |
6041 | Bexpression* cmp = | |
6042 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6043 | left, right, loc); | |
29a2d1d8 | 6044 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6045 | overflow = |
6046 | gogo->backend()->conditional_expression(btype, cmp, | |
6047 | one_expr, zero_expr, | |
6048 | loc); | |
29a2d1d8 | 6049 | else |
a32698ee | 6050 | overflow = |
6051 | gogo->backend()->conditional_expression(btype, cmp, | |
6052 | zero_expr, left, | |
6053 | loc); | |
29a2d1d8 | 6054 | } |
6055 | else | |
6056 | { | |
6057 | // Computing left / -1 is the same as computing - left, | |
6058 | // which does not overflow since Go sets -fwrapv. | |
6059 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6060 | { |
6061 | Expression* negate_expr = | |
6062 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 6063 | overflow = negate_expr->get_backend(context); |
a32698ee | 6064 | } |
29a2d1d8 | 6065 | else |
a32698ee | 6066 | overflow = zero_expr; |
29a2d1d8 | 6067 | } |
a32698ee | 6068 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6069 | |
6070 | // right == -1 ? - left : ret | |
a32698ee | 6071 | ret = gogo->backend()->conditional_expression(btype, check, overflow, |
6072 | ret, loc); | |
29a2d1d8 | 6073 | } |
e440a328 | 6074 | } |
6075 | ||
a32698ee | 6076 | mpz_clear(zero); |
6077 | mpz_clear(one); | |
6078 | mpz_clear(neg_one); | |
ea664253 | 6079 | return ret; |
e440a328 | 6080 | } |
6081 | ||
6082 | // Export a binary expression. | |
6083 | ||
6084 | void | |
6085 | Binary_expression::do_export(Export* exp) const | |
6086 | { | |
6087 | exp->write_c_string("("); | |
6088 | this->left_->export_expression(exp); | |
6089 | switch (this->op_) | |
6090 | { | |
6091 | case OPERATOR_OROR: | |
6092 | exp->write_c_string(" || "); | |
6093 | break; | |
6094 | case OPERATOR_ANDAND: | |
6095 | exp->write_c_string(" && "); | |
6096 | break; | |
6097 | case OPERATOR_EQEQ: | |
6098 | exp->write_c_string(" == "); | |
6099 | break; | |
6100 | case OPERATOR_NOTEQ: | |
6101 | exp->write_c_string(" != "); | |
6102 | break; | |
6103 | case OPERATOR_LT: | |
6104 | exp->write_c_string(" < "); | |
6105 | break; | |
6106 | case OPERATOR_LE: | |
6107 | exp->write_c_string(" <= "); | |
6108 | break; | |
6109 | case OPERATOR_GT: | |
6110 | exp->write_c_string(" > "); | |
6111 | break; | |
6112 | case OPERATOR_GE: | |
6113 | exp->write_c_string(" >= "); | |
6114 | break; | |
6115 | case OPERATOR_PLUS: | |
6116 | exp->write_c_string(" + "); | |
6117 | break; | |
6118 | case OPERATOR_MINUS: | |
6119 | exp->write_c_string(" - "); | |
6120 | break; | |
6121 | case OPERATOR_OR: | |
6122 | exp->write_c_string(" | "); | |
6123 | break; | |
6124 | case OPERATOR_XOR: | |
6125 | exp->write_c_string(" ^ "); | |
6126 | break; | |
6127 | case OPERATOR_MULT: | |
6128 | exp->write_c_string(" * "); | |
6129 | break; | |
6130 | case OPERATOR_DIV: | |
6131 | exp->write_c_string(" / "); | |
6132 | break; | |
6133 | case OPERATOR_MOD: | |
6134 | exp->write_c_string(" % "); | |
6135 | break; | |
6136 | case OPERATOR_LSHIFT: | |
6137 | exp->write_c_string(" << "); | |
6138 | break; | |
6139 | case OPERATOR_RSHIFT: | |
6140 | exp->write_c_string(" >> "); | |
6141 | break; | |
6142 | case OPERATOR_AND: | |
6143 | exp->write_c_string(" & "); | |
6144 | break; | |
6145 | case OPERATOR_BITCLEAR: | |
6146 | exp->write_c_string(" &^ "); | |
6147 | break; | |
6148 | default: | |
c3e6f413 | 6149 | go_unreachable(); |
e440a328 | 6150 | } |
6151 | this->right_->export_expression(exp); | |
6152 | exp->write_c_string(")"); | |
6153 | } | |
6154 | ||
6155 | // Import a binary expression. | |
6156 | ||
6157 | Expression* | |
6158 | Binary_expression::do_import(Import* imp) | |
6159 | { | |
6160 | imp->require_c_string("("); | |
6161 | ||
6162 | Expression* left = Expression::import_expression(imp); | |
6163 | ||
6164 | Operator op; | |
6165 | if (imp->match_c_string(" || ")) | |
6166 | { | |
6167 | op = OPERATOR_OROR; | |
6168 | imp->advance(4); | |
6169 | } | |
6170 | else if (imp->match_c_string(" && ")) | |
6171 | { | |
6172 | op = OPERATOR_ANDAND; | |
6173 | imp->advance(4); | |
6174 | } | |
6175 | else if (imp->match_c_string(" == ")) | |
6176 | { | |
6177 | op = OPERATOR_EQEQ; | |
6178 | imp->advance(4); | |
6179 | } | |
6180 | else if (imp->match_c_string(" != ")) | |
6181 | { | |
6182 | op = OPERATOR_NOTEQ; | |
6183 | imp->advance(4); | |
6184 | } | |
6185 | else if (imp->match_c_string(" < ")) | |
6186 | { | |
6187 | op = OPERATOR_LT; | |
6188 | imp->advance(3); | |
6189 | } | |
6190 | else if (imp->match_c_string(" <= ")) | |
6191 | { | |
6192 | op = OPERATOR_LE; | |
6193 | imp->advance(4); | |
6194 | } | |
6195 | else if (imp->match_c_string(" > ")) | |
6196 | { | |
6197 | op = OPERATOR_GT; | |
6198 | imp->advance(3); | |
6199 | } | |
6200 | else if (imp->match_c_string(" >= ")) | |
6201 | { | |
6202 | op = OPERATOR_GE; | |
6203 | imp->advance(4); | |
6204 | } | |
6205 | else if (imp->match_c_string(" + ")) | |
6206 | { | |
6207 | op = OPERATOR_PLUS; | |
6208 | imp->advance(3); | |
6209 | } | |
6210 | else if (imp->match_c_string(" - ")) | |
6211 | { | |
6212 | op = OPERATOR_MINUS; | |
6213 | imp->advance(3); | |
6214 | } | |
6215 | else if (imp->match_c_string(" | ")) | |
6216 | { | |
6217 | op = OPERATOR_OR; | |
6218 | imp->advance(3); | |
6219 | } | |
6220 | else if (imp->match_c_string(" ^ ")) | |
6221 | { | |
6222 | op = OPERATOR_XOR; | |
6223 | imp->advance(3); | |
6224 | } | |
6225 | else if (imp->match_c_string(" * ")) | |
6226 | { | |
6227 | op = OPERATOR_MULT; | |
6228 | imp->advance(3); | |
6229 | } | |
6230 | else if (imp->match_c_string(" / ")) | |
6231 | { | |
6232 | op = OPERATOR_DIV; | |
6233 | imp->advance(3); | |
6234 | } | |
6235 | else if (imp->match_c_string(" % ")) | |
6236 | { | |
6237 | op = OPERATOR_MOD; | |
6238 | imp->advance(3); | |
6239 | } | |
6240 | else if (imp->match_c_string(" << ")) | |
6241 | { | |
6242 | op = OPERATOR_LSHIFT; | |
6243 | imp->advance(4); | |
6244 | } | |
6245 | else if (imp->match_c_string(" >> ")) | |
6246 | { | |
6247 | op = OPERATOR_RSHIFT; | |
6248 | imp->advance(4); | |
6249 | } | |
6250 | else if (imp->match_c_string(" & ")) | |
6251 | { | |
6252 | op = OPERATOR_AND; | |
6253 | imp->advance(3); | |
6254 | } | |
6255 | else if (imp->match_c_string(" &^ ")) | |
6256 | { | |
6257 | op = OPERATOR_BITCLEAR; | |
6258 | imp->advance(4); | |
6259 | } | |
6260 | else | |
6261 | { | |
6262 | error_at(imp->location(), "unrecognized binary operator"); | |
6263 | return Expression::make_error(imp->location()); | |
6264 | } | |
6265 | ||
6266 | Expression* right = Expression::import_expression(imp); | |
6267 | ||
6268 | imp->require_c_string(")"); | |
6269 | ||
6270 | return Expression::make_binary(op, left, right, imp->location()); | |
6271 | } | |
6272 | ||
d751bb78 | 6273 | // Dump ast representation of a binary expression. |
6274 | ||
6275 | void | |
6276 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6277 | { | |
6278 | ast_dump_context->ostream() << "("; | |
6279 | ast_dump_context->dump_expression(this->left_); | |
6280 | ast_dump_context->ostream() << " "; | |
6281 | ast_dump_context->dump_operator(this->op_); | |
6282 | ast_dump_context->ostream() << " "; | |
6283 | ast_dump_context->dump_expression(this->right_); | |
6284 | ast_dump_context->ostream() << ") "; | |
6285 | } | |
6286 | ||
e440a328 | 6287 | // Make a binary expression. |
6288 | ||
6289 | Expression* | |
6290 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6291 | Location location) |
e440a328 | 6292 | { |
6293 | return new Binary_expression(op, left, right, location); | |
6294 | } | |
6295 | ||
6296 | // Implement a comparison. | |
6297 | ||
a32698ee | 6298 | Bexpression* |
6299 | Expression::comparison(Translate_context* context, Type* result_type, | |
6300 | Operator op, Expression* left, Expression* right, | |
6301 | Location location) | |
e440a328 | 6302 | { |
2387f644 | 6303 | Type* left_type = left->type(); |
6304 | Type* right_type = right->type(); | |
ceeb12d7 | 6305 | |
6306 | mpz_t zval; | |
6307 | mpz_init_set_ui(zval, 0UL); | |
6308 | Expression* zexpr = Expression::make_integer(&zval, NULL, location); | |
6309 | mpz_clear(zval); | |
1b1f2abf | 6310 | |
15c67ee2 | 6311 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6312 | { |
2387f644 | 6313 | left = Runtime::make_call(Runtime::STRCMP, location, 2, |
6314 | left, right); | |
6315 | right = zexpr; | |
e440a328 | 6316 | } |
15c67ee2 | 6317 | else if ((left_type->interface_type() != NULL |
6318 | && right_type->interface_type() == NULL | |
6319 | && !right_type->is_nil_type()) | |
6320 | || (left_type->interface_type() == NULL | |
6321 | && !left_type->is_nil_type() | |
6322 | && right_type->interface_type() != NULL)) | |
e440a328 | 6323 | { |
6324 | // Comparing an interface value to a non-interface value. | |
6325 | if (left_type->interface_type() == NULL) | |
6326 | { | |
6327 | std::swap(left_type, right_type); | |
2387f644 | 6328 | std::swap(left, right); |
e440a328 | 6329 | } |
6330 | ||
6331 | // The right operand is not an interface. We need to take its | |
6332 | // address if it is not a pointer. | |
ceeb12d7 | 6333 | Expression* pointer_arg = NULL; |
e440a328 | 6334 | if (right_type->points_to() != NULL) |
2387f644 | 6335 | pointer_arg = right; |
e440a328 | 6336 | else |
6337 | { | |
2387f644 | 6338 | go_assert(right->is_addressable()); |
6339 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6340 | location); |
e440a328 | 6341 | } |
e440a328 | 6342 | |
2387f644 | 6343 | Expression* descriptor = |
6344 | Expression::make_type_descriptor(right_type, location); | |
6345 | left = | |
ceeb12d7 | 6346 | Runtime::make_call((left_type->interface_type()->is_empty() |
6347 | ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE | |
6348 | : Runtime::INTERFACE_VALUE_COMPARE), | |
2387f644 | 6349 | location, 3, left, descriptor, |
ceeb12d7 | 6350 | pointer_arg); |
2387f644 | 6351 | right = zexpr; |
e440a328 | 6352 | } |
6353 | else if (left_type->interface_type() != NULL | |
6354 | && right_type->interface_type() != NULL) | |
6355 | { | |
ceeb12d7 | 6356 | Runtime::Function compare_function; |
739bad04 | 6357 | if (left_type->interface_type()->is_empty() |
6358 | && right_type->interface_type()->is_empty()) | |
ceeb12d7 | 6359 | compare_function = Runtime::EMPTY_INTERFACE_COMPARE; |
739bad04 | 6360 | else if (!left_type->interface_type()->is_empty() |
6361 | && !right_type->interface_type()->is_empty()) | |
ceeb12d7 | 6362 | compare_function = Runtime::INTERFACE_COMPARE; |
739bad04 | 6363 | else |
6364 | { | |
6365 | if (left_type->interface_type()->is_empty()) | |
6366 | { | |
c484d925 | 6367 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
739bad04 | 6368 | std::swap(left_type, right_type); |
2387f644 | 6369 | std::swap(left, right); |
739bad04 | 6370 | } |
c484d925 | 6371 | go_assert(!left_type->interface_type()->is_empty()); |
6372 | go_assert(right_type->interface_type()->is_empty()); | |
ceeb12d7 | 6373 | compare_function = Runtime::INTERFACE_EMPTY_COMPARE; |
739bad04 | 6374 | } |
6375 | ||
2387f644 | 6376 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6377 | right = zexpr; | |
e440a328 | 6378 | } |
6379 | ||
6380 | if (left_type->is_nil_type() | |
6381 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6382 | { | |
6383 | std::swap(left_type, right_type); | |
2387f644 | 6384 | std::swap(left, right); |
e440a328 | 6385 | } |
6386 | ||
6387 | if (right_type->is_nil_type()) | |
6388 | { | |
2387f644 | 6389 | right = Expression::make_nil(location); |
e440a328 | 6390 | if (left_type->array_type() != NULL |
6391 | && left_type->array_type()->length() == NULL) | |
6392 | { | |
6393 | Array_type* at = left_type->array_type(); | |
2387f644 | 6394 | left = at->get_value_pointer(context->gogo(), left); |
e440a328 | 6395 | } |
6396 | else if (left_type->interface_type() != NULL) | |
6397 | { | |
6398 | // An interface is nil if the first field is nil. | |
2387f644 | 6399 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6400 | } |
6401 | } | |
6402 | ||
ea664253 | 6403 | Bexpression* left_bexpr = left->get_backend(context); |
6404 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6405 | |
a32698ee | 6406 | Gogo* gogo = context->gogo(); |
6407 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6408 | right_bexpr, location); | |
6409 | if (result_type != NULL) | |
6410 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6411 | ret, location); | |
e440a328 | 6412 | return ret; |
6413 | } | |
6414 | ||
6415 | // Class Bound_method_expression. | |
6416 | ||
6417 | // Traversal. | |
6418 | ||
6419 | int | |
6420 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6421 | { | |
e0659c9e | 6422 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6423 | } |
6424 | ||
0afbb937 | 6425 | // Lower the expression. If this is a method value rather than being |
6426 | // called, and the method is accessed via a pointer, we may need to | |
6427 | // add nil checks. Introduce a temporary variable so that those nil | |
6428 | // checks do not cause multiple evaluation. | |
6429 | ||
6430 | Expression* | |
6431 | Bound_method_expression::do_lower(Gogo*, Named_object*, | |
6432 | Statement_inserter* inserter, int) | |
6433 | { | |
6434 | // For simplicity we use a temporary for every call to an embedded | |
6435 | // method, even though some of them might be pure value methods and | |
6436 | // not require a temporary. | |
6437 | if (this->expr_->var_expression() == NULL | |
6438 | && this->expr_->temporary_reference_expression() == NULL | |
6439 | && this->expr_->set_and_use_temporary_expression() == NULL | |
6440 | && (this->method_->field_indexes() != NULL | |
6441 | || (this->method_->is_value_method() | |
6442 | && this->expr_->type()->points_to() != NULL))) | |
6443 | { | |
6444 | Temporary_statement* temp = | |
6445 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
6446 | inserter->insert(temp); | |
6447 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
6448 | this->location()); | |
6449 | } | |
6450 | return this; | |
6451 | } | |
6452 | ||
e440a328 | 6453 | // Return the type of a bound method expression. The type of this |
0afbb937 | 6454 | // object is simply the type of the method with no receiver. |
e440a328 | 6455 | |
6456 | Type* | |
6457 | Bound_method_expression::do_type() | |
6458 | { | |
0afbb937 | 6459 | Named_object* fn = this->method_->named_object(); |
6460 | Function_type* fntype; | |
6461 | if (fn->is_function()) | |
6462 | fntype = fn->func_value()->type(); | |
6463 | else if (fn->is_function_declaration()) | |
6464 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6465 | else |
6466 | return Type::make_error_type(); | |
0afbb937 | 6467 | return fntype->copy_without_receiver(); |
e440a328 | 6468 | } |
6469 | ||
6470 | // Determine the types of a method expression. | |
6471 | ||
6472 | void | |
6473 | Bound_method_expression::do_determine_type(const Type_context*) | |
6474 | { | |
0afbb937 | 6475 | Named_object* fn = this->method_->named_object(); |
6476 | Function_type* fntype; | |
6477 | if (fn->is_function()) | |
6478 | fntype = fn->func_value()->type(); | |
6479 | else if (fn->is_function_declaration()) | |
6480 | fntype = fn->func_declaration_value()->type(); | |
6481 | else | |
6482 | fntype = NULL; | |
e440a328 | 6483 | if (fntype == NULL || !fntype->is_method()) |
6484 | this->expr_->determine_type_no_context(); | |
6485 | else | |
6486 | { | |
6487 | Type_context subcontext(fntype->receiver()->type(), false); | |
6488 | this->expr_->determine_type(&subcontext); | |
6489 | } | |
6490 | } | |
6491 | ||
6492 | // Check the types of a method expression. | |
6493 | ||
6494 | void | |
6495 | Bound_method_expression::do_check_types(Gogo*) | |
6496 | { | |
0afbb937 | 6497 | Named_object* fn = this->method_->named_object(); |
6498 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6499 | { | |
6500 | this->report_error(_("object is not a method")); | |
6501 | return; | |
6502 | } | |
6503 | ||
6504 | Function_type* fntype; | |
6505 | if (fn->is_function()) | |
6506 | fntype = fn->func_value()->type(); | |
6507 | else if (fn->is_function_declaration()) | |
6508 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6509 | else |
0afbb937 | 6510 | go_unreachable(); |
6511 | Type* rtype = fntype->receiver()->type()->deref(); | |
6512 | Type* etype = (this->expr_type_ != NULL | |
6513 | ? this->expr_type_ | |
6514 | : this->expr_->type()); | |
6515 | etype = etype->deref(); | |
6516 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6517 | this->report_error(_("method type does not match object type")); | |
6518 | } | |
6519 | ||
6520 | // If a bound method expression is not simply called, then it is | |
6521 | // represented as a closure. The closure will hold a single variable, | |
6522 | // the receiver to pass to the method. The function will be a simple | |
6523 | // thunk that pulls that value from the closure and calls the method | |
6524 | // with the remaining arguments. | |
6525 | // | |
6526 | // Because method values are not common, we don't build all thunks for | |
6527 | // every methods, but instead only build them as we need them. In | |
6528 | // particular, we even build them on demand for methods defined in | |
6529 | // other packages. | |
6530 | ||
6531 | Bound_method_expression::Method_value_thunks | |
6532 | Bound_method_expression::method_value_thunks; | |
6533 | ||
6534 | // Find or create the thunk for METHOD. | |
6535 | ||
6536 | Named_object* | |
6537 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6538 | Named_object* fn) | |
6539 | { | |
6540 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6541 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6542 | Bound_method_expression::method_value_thunks.insert(val); | |
6543 | if (!ins.second) | |
6544 | { | |
6545 | // We have seen this method before. | |
6546 | go_assert(ins.first->second != NULL); | |
6547 | return ins.first->second; | |
6548 | } | |
6549 | ||
6550 | Location loc = fn->location(); | |
6551 | ||
6552 | Function_type* orig_fntype; | |
6553 | if (fn->is_function()) | |
6554 | orig_fntype = fn->func_value()->type(); | |
6555 | else if (fn->is_function_declaration()) | |
6556 | orig_fntype = fn->func_declaration_value()->type(); | |
6557 | else | |
6558 | orig_fntype = NULL; | |
6559 | ||
6560 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6561 | { |
0afbb937 | 6562 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6563 | return ins.first->second; | |
e440a328 | 6564 | } |
0afbb937 | 6565 | |
6566 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6567 | // The type here is wrong--it should be the C function type. But it |
6568 | // doesn't really matter. | |
0afbb937 | 6569 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6570 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6571 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6572 | orig_fntype->receiver()->type(), | |
6573 | loc))); | |
6574 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
6575 | closure_type = Type::make_pointer_type(closure_type); | |
6576 | ||
f8bdf81a | 6577 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6578 | |
6579 | Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, | |
6580 | false, loc); | |
6581 | ||
f8bdf81a | 6582 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6583 | cvar->set_is_used(); | |
6584 | Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); | |
6585 | new_no->func_value()->set_closure_var(cp); | |
0afbb937 | 6586 | |
f8bdf81a | 6587 | gogo->start_block(loc); |
0afbb937 | 6588 | |
6589 | // Field 0 of the closure is the function code pointer, field 1 is | |
6590 | // the value on which to invoke the method. | |
6591 | Expression* arg = Expression::make_var_reference(cp, loc); | |
6592 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
6593 | arg = Expression::make_field_reference(arg, 1, loc); | |
6594 | ||
6595 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6596 | ||
6597 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6598 | Expression_list* args; | |
6599 | if (orig_params == NULL || orig_params->empty()) | |
6600 | args = NULL; | |
6601 | else | |
6602 | { | |
6603 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6604 | args = new Expression_list(); | |
6605 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6606 | p != new_params->end(); |
0afbb937 | 6607 | ++p) |
6608 | { | |
6609 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6610 | go_assert(p_no != NULL | |
6611 | && p_no->is_variable() | |
6612 | && p_no->var_value()->is_parameter()); | |
6613 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6614 | } | |
6615 | } | |
6616 | ||
6617 | Call_expression* call = Expression::make_call(bme, args, | |
6618 | orig_fntype->is_varargs(), | |
6619 | loc); | |
6620 | call->set_varargs_are_lowered(); | |
6621 | ||
6622 | Statement* s = Statement::make_return_from_call(call, loc); | |
6623 | gogo->add_statement(s); | |
6624 | Block* b = gogo->finish_block(loc); | |
6625 | gogo->add_block(b, loc); | |
6626 | gogo->lower_block(new_no, b); | |
a32698ee | 6627 | gogo->flatten_block(new_no, b); |
0afbb937 | 6628 | gogo->finish_function(loc); |
6629 | ||
6630 | ins.first->second = new_no; | |
6631 | return new_no; | |
6632 | } | |
6633 | ||
6634 | // Return an expression to check *REF for nil while dereferencing | |
6635 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6636 | // reference. This is a static function so that we don't have to | |
6637 | // worry about declaring Field_indexes in expressions.h. | |
6638 | ||
6639 | static Expression* | |
6640 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6641 | Expression** ref) | |
6642 | { | |
6643 | if (field_indexes == NULL) | |
6644 | return Expression::make_boolean(false, loc); | |
6645 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6646 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6647 | go_assert(stype != NULL | |
6648 | && field_indexes->field_index < stype->field_count()); | |
6649 | if ((*ref)->type()->struct_type() == NULL) | |
6650 | { | |
6651 | go_assert((*ref)->type()->points_to() != NULL); | |
6652 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6653 | Expression::make_nil(loc), | |
6654 | loc); | |
6655 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
6656 | *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); | |
6657 | go_assert((*ref)->type()->struct_type() == stype); | |
6658 | } | |
6659 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6660 | loc); | |
6661 | return cond; | |
e440a328 | 6662 | } |
6663 | ||
ea664253 | 6664 | // Get the backend representation for a method value. |
e440a328 | 6665 | |
ea664253 | 6666 | Bexpression* |
6667 | Bound_method_expression::do_get_backend(Translate_context* context) | |
e440a328 | 6668 | { |
0afbb937 | 6669 | Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(), |
6670 | this->method_, | |
6671 | this->function_); | |
6672 | if (thunk->is_erroneous()) | |
6673 | { | |
6674 | go_assert(saw_errors()); | |
ea664253 | 6675 | return context->backend()->error_expression(); |
0afbb937 | 6676 | } |
6677 | ||
6678 | // FIXME: We should lower this earlier, but we can't lower it in the | |
6679 | // lowering pass because at that point we don't know whether we need | |
6680 | // to create the thunk or not. If the expression is called, we | |
6681 | // don't need the thunk. | |
6682 | ||
6683 | Location loc = this->location(); | |
6684 | ||
6685 | // If the method expects a value, and we have a pointer, we need to | |
6686 | // dereference the pointer. | |
6687 | ||
6688 | Named_object* fn = this->method_->named_object(); | |
6689 | Function_type* fntype; | |
6690 | if (fn->is_function()) | |
6691 | fntype = fn->func_value()->type(); | |
6692 | else if (fn->is_function_declaration()) | |
6693 | fntype = fn->func_declaration_value()->type(); | |
6694 | else | |
6695 | go_unreachable(); | |
6696 | ||
6697 | Expression* val = this->expr_; | |
6698 | if (fntype->receiver()->type()->points_to() == NULL | |
6699 | && val->type()->points_to() != NULL) | |
6700 | val = Expression::make_unary(OPERATOR_MULT, val, loc); | |
6701 | ||
6702 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
6703 | // expect a closure whose second field has type this->expr_type_ (if | |
6704 | // that is not NULL). We are going to pass it a closure whose | |
6705 | // second field has type this->expr_->type(). Since | |
6706 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
6707 | // away with this. | |
6708 | ||
6709 | Struct_field_list* fields = new Struct_field_list(); | |
6710 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
6711 | thunk->func_value()->type(), | |
6712 | loc))); | |
6713 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
6714 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6715 | ||
6716 | Expression_list* vals = new Expression_list(); | |
6717 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
6718 | vals->push_back(val); | |
6719 | ||
6720 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
2c809f8f | 6721 | ret = Expression::make_heap_expression(ret, loc); |
0afbb937 | 6722 | |
0afbb937 | 6723 | // See whether the expression or any embedded pointers are nil. |
6724 | ||
df7ef1fd | 6725 | Expression* nil_check = NULL; |
0afbb937 | 6726 | Expression* expr = this->expr_; |
6727 | if (this->method_->field_indexes() != NULL) | |
6728 | { | |
6729 | // Note that we are evaluating this->expr_ twice, but that is OK | |
6730 | // because in the lowering pass we forced it into a temporary | |
6731 | // variable. | |
6732 | Expression* ref = expr; | |
6733 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
6734 | expr = ref; | |
6735 | } | |
6736 | ||
6737 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
6738 | { | |
6739 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
6740 | Expression::make_nil(loc), | |
6741 | loc); | |
6742 | if (nil_check == NULL) | |
6743 | nil_check = n; | |
6744 | else | |
6745 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
6746 | } | |
6747 | ||
ea664253 | 6748 | Bexpression* bme = ret->get_backend(context); |
0afbb937 | 6749 | if (nil_check != NULL) |
6750 | { | |
df7ef1fd | 6751 | Gogo* gogo = context->gogo(); |
ea664253 | 6752 | Bexpression* crash = |
6753 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
6754 | loc)->get_backend(context); | |
df7ef1fd | 6755 | Btype* btype = ret->type()->get_backend(gogo); |
ea664253 | 6756 | Bexpression* bcheck = nil_check->get_backend(context); |
6757 | bme = gogo->backend()->conditional_expression(btype, bcheck, crash, | |
df7ef1fd | 6758 | bme, loc); |
6759 | } | |
ea664253 | 6760 | return bme; |
e440a328 | 6761 | } |
6762 | ||
d751bb78 | 6763 | // Dump ast representation of a bound method expression. |
6764 | ||
6765 | void | |
6766 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6767 | const | |
6768 | { | |
6769 | if (this->expr_type_ != NULL) | |
6770 | ast_dump_context->ostream() << "("; | |
6771 | ast_dump_context->dump_expression(this->expr_); | |
6772 | if (this->expr_type_ != NULL) | |
6773 | { | |
6774 | ast_dump_context->ostream() << ":"; | |
6775 | ast_dump_context->dump_type(this->expr_type_); | |
6776 | ast_dump_context->ostream() << ")"; | |
6777 | } | |
6778 | ||
0afbb937 | 6779 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 6780 | } |
6781 | ||
e440a328 | 6782 | // Make a method expression. |
6783 | ||
6784 | Bound_method_expression* | |
0afbb937 | 6785 | Expression::make_bound_method(Expression* expr, const Method* method, |
6786 | Named_object* function, Location location) | |
e440a328 | 6787 | { |
0afbb937 | 6788 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 6789 | } |
6790 | ||
6791 | // Class Builtin_call_expression. This is used for a call to a | |
6792 | // builtin function. | |
6793 | ||
6794 | class Builtin_call_expression : public Call_expression | |
6795 | { | |
6796 | public: | |
6797 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6798 | bool is_varargs, Location location); |
e440a328 | 6799 | |
6800 | protected: | |
6801 | // This overrides Call_expression::do_lower. | |
6802 | Expression* | |
ceeb4318 | 6803 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6804 | |
35a54f17 | 6805 | Expression* |
6806 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
6807 | ||
e440a328 | 6808 | bool |
6809 | do_is_constant() const; | |
6810 | ||
6811 | bool | |
0c77715b | 6812 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6813 | |
4f2138d7 | 6814 | bool |
a7549a6a | 6815 | do_discarding_value(); |
6816 | ||
e440a328 | 6817 | Type* |
6818 | do_type(); | |
6819 | ||
6820 | void | |
6821 | do_determine_type(const Type_context*); | |
6822 | ||
6823 | void | |
6824 | do_check_types(Gogo*); | |
6825 | ||
6826 | Expression* | |
6827 | do_copy() | |
6828 | { | |
6829 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6830 | this->args()->copy(), | |
6831 | this->is_varargs(), | |
6832 | this->location()); | |
6833 | } | |
6834 | ||
ea664253 | 6835 | Bexpression* |
6836 | do_get_backend(Translate_context*); | |
e440a328 | 6837 | |
6838 | void | |
6839 | do_export(Export*) const; | |
6840 | ||
6841 | virtual bool | |
6842 | do_is_recover_call() const; | |
6843 | ||
6844 | virtual void | |
6845 | do_set_recover_arg(Expression*); | |
6846 | ||
6847 | private: | |
6848 | // The builtin functions. | |
6849 | enum Builtin_function_code | |
6850 | { | |
6851 | BUILTIN_INVALID, | |
6852 | ||
6853 | // Predeclared builtin functions. | |
6854 | BUILTIN_APPEND, | |
6855 | BUILTIN_CAP, | |
6856 | BUILTIN_CLOSE, | |
48080209 | 6857 | BUILTIN_COMPLEX, |
e440a328 | 6858 | BUILTIN_COPY, |
1cce762f | 6859 | BUILTIN_DELETE, |
e440a328 | 6860 | BUILTIN_IMAG, |
6861 | BUILTIN_LEN, | |
6862 | BUILTIN_MAKE, | |
6863 | BUILTIN_NEW, | |
6864 | BUILTIN_PANIC, | |
6865 | BUILTIN_PRINT, | |
6866 | BUILTIN_PRINTLN, | |
6867 | BUILTIN_REAL, | |
6868 | BUILTIN_RECOVER, | |
6869 | ||
6870 | // Builtin functions from the unsafe package. | |
6871 | BUILTIN_ALIGNOF, | |
6872 | BUILTIN_OFFSETOF, | |
6873 | BUILTIN_SIZEOF | |
6874 | }; | |
6875 | ||
6876 | Expression* | |
6877 | one_arg() const; | |
6878 | ||
6879 | bool | |
6880 | check_one_arg(); | |
6881 | ||
6882 | static Type* | |
6883 | real_imag_type(Type*); | |
6884 | ||
6885 | static Type* | |
48080209 | 6886 | complex_type(Type*); |
e440a328 | 6887 | |
a9182619 | 6888 | Expression* |
6889 | lower_make(); | |
6890 | ||
6891 | bool | |
1ad00fd4 | 6892 | check_int_value(Expression*, bool is_length); |
a9182619 | 6893 | |
e440a328 | 6894 | // A pointer back to the general IR structure. This avoids a global |
6895 | // variable, or passing it around everywhere. | |
6896 | Gogo* gogo_; | |
6897 | // The builtin function being called. | |
6898 | Builtin_function_code code_; | |
0f914071 | 6899 | // Used to stop endless loops when the length of an array uses len |
6900 | // or cap of the array itself. | |
6901 | mutable bool seen_; | |
e440a328 | 6902 | }; |
6903 | ||
6904 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6905 | Expression* fn, | |
6906 | Expression_list* args, | |
6907 | bool is_varargs, | |
b13c66cd | 6908 | Location location) |
e440a328 | 6909 | : Call_expression(fn, args, is_varargs, location), |
0f914071 | 6910 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 6911 | { |
6912 | Func_expression* fnexp = this->fn()->func_expression(); | |
c484d925 | 6913 | go_assert(fnexp != NULL); |
e440a328 | 6914 | const std::string& name(fnexp->named_object()->name()); |
6915 | if (name == "append") | |
6916 | this->code_ = BUILTIN_APPEND; | |
6917 | else if (name == "cap") | |
6918 | this->code_ = BUILTIN_CAP; | |
6919 | else if (name == "close") | |
6920 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6921 | else if (name == "complex") |
6922 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6923 | else if (name == "copy") |
6924 | this->code_ = BUILTIN_COPY; | |
1cce762f | 6925 | else if (name == "delete") |
6926 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 6927 | else if (name == "imag") |
6928 | this->code_ = BUILTIN_IMAG; | |
6929 | else if (name == "len") | |
6930 | this->code_ = BUILTIN_LEN; | |
6931 | else if (name == "make") | |
6932 | this->code_ = BUILTIN_MAKE; | |
6933 | else if (name == "new") | |
6934 | this->code_ = BUILTIN_NEW; | |
6935 | else if (name == "panic") | |
6936 | this->code_ = BUILTIN_PANIC; | |
6937 | else if (name == "print") | |
6938 | this->code_ = BUILTIN_PRINT; | |
6939 | else if (name == "println") | |
6940 | this->code_ = BUILTIN_PRINTLN; | |
6941 | else if (name == "real") | |
6942 | this->code_ = BUILTIN_REAL; | |
6943 | else if (name == "recover") | |
6944 | this->code_ = BUILTIN_RECOVER; | |
6945 | else if (name == "Alignof") | |
6946 | this->code_ = BUILTIN_ALIGNOF; | |
6947 | else if (name == "Offsetof") | |
6948 | this->code_ = BUILTIN_OFFSETOF; | |
6949 | else if (name == "Sizeof") | |
6950 | this->code_ = BUILTIN_SIZEOF; | |
6951 | else | |
c3e6f413 | 6952 | go_unreachable(); |
e440a328 | 6953 | } |
6954 | ||
6955 | // Return whether this is a call to recover. This is a virtual | |
6956 | // function called from the parent class. | |
6957 | ||
6958 | bool | |
6959 | Builtin_call_expression::do_is_recover_call() const | |
6960 | { | |
6961 | if (this->classification() == EXPRESSION_ERROR) | |
6962 | return false; | |
6963 | return this->code_ == BUILTIN_RECOVER; | |
6964 | } | |
6965 | ||
6966 | // Set the argument for a call to recover. | |
6967 | ||
6968 | void | |
6969 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6970 | { | |
6971 | const Expression_list* args = this->args(); | |
c484d925 | 6972 | go_assert(args == NULL || args->empty()); |
e440a328 | 6973 | Expression_list* new_args = new Expression_list(); |
6974 | new_args->push_back(arg); | |
6975 | this->set_args(new_args); | |
6976 | } | |
6977 | ||
e440a328 | 6978 | // Lower a builtin call expression. This turns new and make into |
6979 | // specific expressions. We also convert to a constant if we can. | |
6980 | ||
6981 | Expression* | |
ceeb4318 | 6982 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
6983 | Statement_inserter* inserter, int) | |
e440a328 | 6984 | { |
a9182619 | 6985 | if (this->classification() == EXPRESSION_ERROR) |
6986 | return this; | |
6987 | ||
b13c66cd | 6988 | Location loc = this->location(); |
1cce762f | 6989 | |
a8725655 | 6990 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
6991 | { | |
6992 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 6993 | return Expression::make_error(loc); |
a8725655 | 6994 | } |
6995 | ||
393ba00b | 6996 | if (this->code_ == BUILTIN_OFFSETOF) |
6997 | { | |
6998 | Expression* arg = this->one_arg(); | |
12e69faa | 6999 | |
7000 | if (arg->bound_method_expression() != NULL | |
7001 | || arg->interface_field_reference_expression() != NULL) | |
7002 | { | |
7003 | this->report_error(_("invalid use of method value as argument " | |
7004 | "of Offsetof")); | |
7005 | return this; | |
7006 | } | |
7007 | ||
393ba00b | 7008 | Field_reference_expression* farg = arg->field_reference_expression(); |
7009 | while (farg != NULL) | |
7010 | { | |
7011 | if (!farg->implicit()) | |
7012 | break; | |
7013 | // When the selector refers to an embedded field, | |
7014 | // it must not be reached through pointer indirections. | |
7015 | if (farg->expr()->deref() != farg->expr()) | |
7016 | { | |
12e69faa | 7017 | this->report_error(_("argument of Offsetof implies " |
7018 | "indirection of an embedded field")); | |
393ba00b | 7019 | return this; |
7020 | } | |
7021 | // Go up until we reach the original base. | |
7022 | farg = farg->expr()->field_reference_expression(); | |
7023 | } | |
7024 | } | |
7025 | ||
1cce762f | 7026 | if (this->is_constant()) |
e440a328 | 7027 | { |
0c77715b | 7028 | Numeric_constant nc; |
7029 | if (this->numeric_constant_value(&nc)) | |
7030 | return nc.expression(loc); | |
e440a328 | 7031 | } |
1cce762f | 7032 | |
7033 | switch (this->code_) | |
e440a328 | 7034 | { |
1cce762f | 7035 | default: |
7036 | break; | |
7037 | ||
7038 | case BUILTIN_NEW: | |
7039 | { | |
7040 | const Expression_list* args = this->args(); | |
7041 | if (args == NULL || args->size() < 1) | |
7042 | this->report_error(_("not enough arguments")); | |
7043 | else if (args->size() > 1) | |
7044 | this->report_error(_("too many arguments")); | |
7045 | else | |
7046 | { | |
7047 | Expression* arg = args->front(); | |
7048 | if (!arg->is_type_expression()) | |
7049 | { | |
7050 | error_at(arg->location(), "expected type"); | |
7051 | this->set_is_error(); | |
7052 | } | |
7053 | else | |
7054 | return Expression::make_allocation(arg->type(), loc); | |
7055 | } | |
7056 | } | |
7057 | break; | |
7058 | ||
7059 | case BUILTIN_MAKE: | |
7060 | return this->lower_make(); | |
7061 | ||
7062 | case BUILTIN_RECOVER: | |
e440a328 | 7063 | if (function != NULL) |
7064 | function->func_value()->set_calls_recover(); | |
7065 | else | |
7066 | { | |
7067 | // Calling recover outside of a function always returns the | |
7068 | // nil empty interface. | |
823c7e3d | 7069 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7070 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7071 | } |
1cce762f | 7072 | break; |
7073 | ||
7074 | case BUILTIN_APPEND: | |
7075 | { | |
7076 | // Lower the varargs. | |
7077 | const Expression_list* args = this->args(); | |
7078 | if (args == NULL || args->empty()) | |
e440a328 | 7079 | return this; |
1cce762f | 7080 | Type* slice_type = args->front()->type(); |
7081 | if (!slice_type->is_slice_type()) | |
7082 | { | |
3ff4863b | 7083 | if (slice_type->is_nil_type()) |
7084 | error_at(args->front()->location(), "use of untyped nil"); | |
7085 | else | |
7086 | error_at(args->front()->location(), | |
7087 | "argument 1 must be a slice"); | |
1cce762f | 7088 | this->set_is_error(); |
7089 | return this; | |
7090 | } | |
19fd40c3 | 7091 | Type* element_type = slice_type->array_type()->element_type(); |
7092 | this->lower_varargs(gogo, function, inserter, | |
7093 | Type::make_array_type(element_type, NULL), | |
7094 | 2); | |
1cce762f | 7095 | } |
7096 | break; | |
7097 | ||
7098 | case BUILTIN_DELETE: | |
7099 | { | |
7100 | // Lower to a runtime function call. | |
7101 | const Expression_list* args = this->args(); | |
7102 | if (args == NULL || args->size() < 2) | |
7103 | this->report_error(_("not enough arguments")); | |
7104 | else if (args->size() > 2) | |
7105 | this->report_error(_("too many arguments")); | |
7106 | else if (args->front()->type()->map_type() == NULL) | |
7107 | this->report_error(_("argument 1 must be a map")); | |
7108 | else | |
7109 | { | |
7110 | // Since this function returns no value it must appear in | |
7111 | // a statement by itself, so we don't have to worry about | |
7112 | // order of evaluation of values around it. Evaluate the | |
7113 | // map first to get order of evaluation right. | |
7114 | Map_type* mt = args->front()->type()->map_type(); | |
7115 | Temporary_statement* map_temp = | |
7116 | Statement::make_temporary(mt, args->front(), loc); | |
7117 | inserter->insert(map_temp); | |
7118 | ||
7119 | Temporary_statement* key_temp = | |
7120 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7121 | inserter->insert(key_temp); | |
7122 | ||
7123 | Expression* e1 = Expression::make_temporary_reference(map_temp, | |
7124 | loc); | |
7125 | Expression* e2 = Expression::make_temporary_reference(key_temp, | |
7126 | loc); | |
7127 | e2 = Expression::make_unary(OPERATOR_AND, e2, loc); | |
7128 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), | |
7129 | 2, e1, e2); | |
7130 | } | |
7131 | } | |
7132 | break; | |
e440a328 | 7133 | } |
7134 | ||
7135 | return this; | |
7136 | } | |
7137 | ||
35a54f17 | 7138 | // Flatten a builtin call expression. This turns the arguments of copy and |
7139 | // append into temporary expressions. | |
7140 | ||
7141 | Expression* | |
7142 | Builtin_call_expression::do_flatten(Gogo*, Named_object*, | |
7143 | Statement_inserter* inserter) | |
7144 | { | |
7145 | if (this->code_ == BUILTIN_APPEND | |
7146 | || this->code_ == BUILTIN_COPY) | |
7147 | { | |
7148 | Location loc = this->location(); | |
7149 | Type* at = this->args()->front()->type(); | |
7150 | for (Expression_list::iterator pa = this->args()->begin(); | |
7151 | pa != this->args()->end(); | |
7152 | ++pa) | |
7153 | { | |
7154 | if ((*pa)->is_nil_expression()) | |
7155 | *pa = Expression::make_slice_composite_literal(at, NULL, loc); | |
7156 | if (!(*pa)->is_variable()) | |
7157 | { | |
7158 | Temporary_statement* temp = | |
7159 | Statement::make_temporary(NULL, *pa, loc); | |
7160 | inserter->insert(temp); | |
7161 | *pa = Expression::make_temporary_reference(temp, loc); | |
7162 | } | |
7163 | } | |
7164 | } | |
7165 | return this; | |
7166 | } | |
7167 | ||
a9182619 | 7168 | // Lower a make expression. |
7169 | ||
7170 | Expression* | |
7171 | Builtin_call_expression::lower_make() | |
7172 | { | |
b13c66cd | 7173 | Location loc = this->location(); |
a9182619 | 7174 | |
7175 | const Expression_list* args = this->args(); | |
7176 | if (args == NULL || args->size() < 1) | |
7177 | { | |
7178 | this->report_error(_("not enough arguments")); | |
7179 | return Expression::make_error(this->location()); | |
7180 | } | |
7181 | ||
7182 | Expression_list::const_iterator parg = args->begin(); | |
7183 | ||
7184 | Expression* first_arg = *parg; | |
7185 | if (!first_arg->is_type_expression()) | |
7186 | { | |
7187 | error_at(first_arg->location(), "expected type"); | |
7188 | this->set_is_error(); | |
7189 | return Expression::make_error(this->location()); | |
7190 | } | |
7191 | Type* type = first_arg->type(); | |
7192 | ||
7193 | bool is_slice = false; | |
7194 | bool is_map = false; | |
7195 | bool is_chan = false; | |
411eb89e | 7196 | if (type->is_slice_type()) |
a9182619 | 7197 | is_slice = true; |
7198 | else if (type->map_type() != NULL) | |
7199 | is_map = true; | |
7200 | else if (type->channel_type() != NULL) | |
7201 | is_chan = true; | |
7202 | else | |
7203 | { | |
7204 | this->report_error(_("invalid type for make function")); | |
7205 | return Expression::make_error(this->location()); | |
7206 | } | |
7207 | ||
ac84c822 | 7208 | bool have_big_args = false; |
7209 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
7210 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
7211 | ||
f6bc81e6 | 7212 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7213 | ||
a9182619 | 7214 | ++parg; |
7215 | Expression* len_arg; | |
7216 | if (parg == args->end()) | |
7217 | { | |
7218 | if (is_slice) | |
7219 | { | |
7220 | this->report_error(_("length required when allocating a slice")); | |
7221 | return Expression::make_error(this->location()); | |
7222 | } | |
7223 | ||
7224 | mpz_t zval; | |
7225 | mpz_init_set_ui(zval, 0); | |
7226 | len_arg = Expression::make_integer(&zval, NULL, loc); | |
7227 | mpz_clear(zval); | |
7228 | } | |
7229 | else | |
7230 | { | |
7231 | len_arg = *parg; | |
f6bc81e6 | 7232 | len_arg->determine_type(&int_context); |
1ad00fd4 | 7233 | if (!this->check_int_value(len_arg, true)) |
7234 | return Expression::make_error(this->location()); | |
ac84c822 | 7235 | if (len_arg->type()->integer_type() != NULL |
7236 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
7237 | have_big_args = true; | |
a9182619 | 7238 | ++parg; |
7239 | } | |
7240 | ||
7241 | Expression* cap_arg = NULL; | |
7242 | if (is_slice && parg != args->end()) | |
7243 | { | |
7244 | cap_arg = *parg; | |
f6bc81e6 | 7245 | cap_arg->determine_type(&int_context); |
1ad00fd4 | 7246 | if (!this->check_int_value(cap_arg, false)) |
7247 | return Expression::make_error(this->location()); | |
7248 | ||
7249 | Numeric_constant nclen; | |
7250 | Numeric_constant nccap; | |
7251 | unsigned long vlen; | |
7252 | unsigned long vcap; | |
7253 | if (len_arg->numeric_constant_value(&nclen) | |
7254 | && cap_arg->numeric_constant_value(&nccap) | |
7255 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7256 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7257 | && vlen > vcap) | |
a9182619 | 7258 | { |
1ad00fd4 | 7259 | this->report_error(_("len larger than cap")); |
a9182619 | 7260 | return Expression::make_error(this->location()); |
7261 | } | |
1ad00fd4 | 7262 | |
ac84c822 | 7263 | if (cap_arg->type()->integer_type() != NULL |
7264 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
7265 | have_big_args = true; | |
a9182619 | 7266 | ++parg; |
7267 | } | |
7268 | ||
7269 | if (parg != args->end()) | |
7270 | { | |
7271 | this->report_error(_("too many arguments to make")); | |
7272 | return Expression::make_error(this->location()); | |
7273 | } | |
7274 | ||
b13c66cd | 7275 | Location type_loc = first_arg->location(); |
a9182619 | 7276 | Expression* type_arg; |
7277 | if (is_slice || is_chan) | |
7278 | type_arg = Expression::make_type_descriptor(type, type_loc); | |
7279 | else if (is_map) | |
7280 | type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); | |
7281 | else | |
7282 | go_unreachable(); | |
7283 | ||
7284 | Expression* call; | |
7285 | if (is_slice) | |
7286 | { | |
7287 | if (cap_arg == NULL) | |
ac84c822 | 7288 | call = Runtime::make_call((have_big_args |
7289 | ? Runtime::MAKESLICE1BIG | |
7290 | : Runtime::MAKESLICE1), | |
7291 | loc, 2, type_arg, len_arg); | |
a9182619 | 7292 | else |
ac84c822 | 7293 | call = Runtime::make_call((have_big_args |
7294 | ? Runtime::MAKESLICE2BIG | |
7295 | : Runtime::MAKESLICE2), | |
7296 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7297 | } |
7298 | else if (is_map) | |
ac84c822 | 7299 | call = Runtime::make_call((have_big_args |
7300 | ? Runtime::MAKEMAPBIG | |
7301 | : Runtime::MAKEMAP), | |
7302 | loc, 2, type_arg, len_arg); | |
a9182619 | 7303 | else if (is_chan) |
ac84c822 | 7304 | call = Runtime::make_call((have_big_args |
7305 | ? Runtime::MAKECHANBIG | |
7306 | : Runtime::MAKECHAN), | |
7307 | loc, 2, type_arg, len_arg); | |
a9182619 | 7308 | else |
7309 | go_unreachable(); | |
7310 | ||
7311 | return Expression::make_unsafe_cast(type, call, loc); | |
7312 | } | |
7313 | ||
7314 | // Return whether an expression has an integer value. Report an error | |
7315 | // if not. This is used when handling calls to the predeclared make | |
7316 | // function. | |
7317 | ||
7318 | bool | |
1ad00fd4 | 7319 | Builtin_call_expression::check_int_value(Expression* e, bool is_length) |
a9182619 | 7320 | { |
0c77715b | 7321 | Numeric_constant nc; |
1ad00fd4 | 7322 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7323 | { |
1ad00fd4 | 7324 | unsigned long v; |
7325 | switch (nc.to_unsigned_long(&v)) | |
7326 | { | |
7327 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7328 | break; |
1ad00fd4 | 7329 | case Numeric_constant::NC_UL_NOTINT: |
7330 | error_at(e->location(), "non-integer %s argument to make", | |
7331 | is_length ? "len" : "cap"); | |
7332 | return false; | |
7333 | case Numeric_constant::NC_UL_NEGATIVE: | |
7334 | error_at(e->location(), "negative %s argument to make", | |
7335 | is_length ? "len" : "cap"); | |
7336 | return false; | |
7337 | case Numeric_constant::NC_UL_BIG: | |
7338 | // We don't want to give a compile-time error for a 64-bit | |
7339 | // value on a 32-bit target. | |
1b10c5e7 | 7340 | break; |
1ad00fd4 | 7341 | } |
1b10c5e7 | 7342 | |
7343 | mpz_t val; | |
7344 | if (!nc.to_int(&val)) | |
7345 | go_unreachable(); | |
7346 | int bits = mpz_sizeinbase(val, 2); | |
7347 | mpz_clear(val); | |
7348 | Type* int_type = Type::lookup_integer_type("int"); | |
7349 | if (bits >= int_type->integer_type()->bits()) | |
7350 | { | |
7351 | error_at(e->location(), "%s argument too large for make", | |
7352 | is_length ? "len" : "cap"); | |
7353 | return false; | |
7354 | } | |
7355 | ||
7356 | return true; | |
a9182619 | 7357 | } |
7358 | ||
1ad00fd4 | 7359 | if (e->type()->integer_type() != NULL) |
7360 | return true; | |
7361 | ||
7362 | error_at(e->location(), "non-integer %s argument to make", | |
7363 | is_length ? "len" : "cap"); | |
a9182619 | 7364 | return false; |
7365 | } | |
7366 | ||
e440a328 | 7367 | // Return the type of the real or imag functions, given the type of |
7368 | // the argument. We need to map complex to float, complex64 to | |
7369 | // float32, and complex128 to float64, so it has to be done by name. | |
7370 | // This returns NULL if it can't figure out the type. | |
7371 | ||
7372 | Type* | |
7373 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7374 | { | |
7375 | if (arg_type == NULL || arg_type->is_abstract()) | |
7376 | return NULL; | |
7377 | Named_type* nt = arg_type->named_type(); | |
7378 | if (nt == NULL) | |
7379 | return NULL; | |
7380 | while (nt->real_type()->named_type() != NULL) | |
7381 | nt = nt->real_type()->named_type(); | |
48080209 | 7382 | if (nt->name() == "complex64") |
e440a328 | 7383 | return Type::lookup_float_type("float32"); |
7384 | else if (nt->name() == "complex128") | |
7385 | return Type::lookup_float_type("float64"); | |
7386 | else | |
7387 | return NULL; | |
7388 | } | |
7389 | ||
48080209 | 7390 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7391 | // argments. Like real_imag_type, we have to map by name. |
7392 | ||
7393 | Type* | |
48080209 | 7394 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7395 | { |
7396 | if (arg_type == NULL || arg_type->is_abstract()) | |
7397 | return NULL; | |
7398 | Named_type* nt = arg_type->named_type(); | |
7399 | if (nt == NULL) | |
7400 | return NULL; | |
7401 | while (nt->real_type()->named_type() != NULL) | |
7402 | nt = nt->real_type()->named_type(); | |
48080209 | 7403 | if (nt->name() == "float32") |
e440a328 | 7404 | return Type::lookup_complex_type("complex64"); |
7405 | else if (nt->name() == "float64") | |
7406 | return Type::lookup_complex_type("complex128"); | |
7407 | else | |
7408 | return NULL; | |
7409 | } | |
7410 | ||
7411 | // Return a single argument, or NULL if there isn't one. | |
7412 | ||
7413 | Expression* | |
7414 | Builtin_call_expression::one_arg() const | |
7415 | { | |
7416 | const Expression_list* args = this->args(); | |
aa615cb3 | 7417 | if (args == NULL || args->size() != 1) |
e440a328 | 7418 | return NULL; |
7419 | return args->front(); | |
7420 | } | |
7421 | ||
83921647 | 7422 | // A traversal class which looks for a call or receive expression. |
7423 | ||
7424 | class Find_call_expression : public Traverse | |
7425 | { | |
7426 | public: | |
7427 | Find_call_expression() | |
7428 | : Traverse(traverse_expressions), | |
7429 | found_(false) | |
7430 | { } | |
7431 | ||
7432 | int | |
7433 | expression(Expression**); | |
7434 | ||
7435 | bool | |
7436 | found() | |
7437 | { return this->found_; } | |
7438 | ||
7439 | private: | |
7440 | bool found_; | |
7441 | }; | |
7442 | ||
7443 | int | |
7444 | Find_call_expression::expression(Expression** pexpr) | |
7445 | { | |
7446 | if ((*pexpr)->call_expression() != NULL | |
7447 | || (*pexpr)->receive_expression() != NULL) | |
7448 | { | |
7449 | this->found_ = true; | |
7450 | return TRAVERSE_EXIT; | |
7451 | } | |
7452 | return TRAVERSE_CONTINUE; | |
7453 | } | |
7454 | ||
7455 | // Return whether this is constant: len of a string constant, or len | |
7456 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7457 | // unsafe.Alignof. | |
e440a328 | 7458 | |
7459 | bool | |
7460 | Builtin_call_expression::do_is_constant() const | |
7461 | { | |
12e69faa | 7462 | if (this->is_error_expression()) |
7463 | return true; | |
e440a328 | 7464 | switch (this->code_) |
7465 | { | |
7466 | case BUILTIN_LEN: | |
7467 | case BUILTIN_CAP: | |
7468 | { | |
0f914071 | 7469 | if (this->seen_) |
7470 | return false; | |
7471 | ||
e440a328 | 7472 | Expression* arg = this->one_arg(); |
7473 | if (arg == NULL) | |
7474 | return false; | |
7475 | Type* arg_type = arg->type(); | |
7476 | ||
7477 | if (arg_type->points_to() != NULL | |
7478 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7479 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7480 | arg_type = arg_type->points_to(); |
7481 | ||
83921647 | 7482 | // The len and cap functions are only constant if there are no |
7483 | // function calls or channel operations in the arguments. | |
7484 | // Otherwise we have to make the call. | |
7485 | if (!arg->is_constant()) | |
7486 | { | |
7487 | Find_call_expression find_call; | |
7488 | Expression::traverse(&arg, &find_call); | |
7489 | if (find_call.found()) | |
7490 | return false; | |
7491 | } | |
7492 | ||
e440a328 | 7493 | if (arg_type->array_type() != NULL |
7494 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7495 | return true; |
e440a328 | 7496 | |
7497 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7498 | { |
7499 | this->seen_ = true; | |
7500 | bool ret = arg->is_constant(); | |
7501 | this->seen_ = false; | |
7502 | return ret; | |
7503 | } | |
e440a328 | 7504 | } |
7505 | break; | |
7506 | ||
7507 | case BUILTIN_SIZEOF: | |
7508 | case BUILTIN_ALIGNOF: | |
7509 | return this->one_arg() != NULL; | |
7510 | ||
7511 | case BUILTIN_OFFSETOF: | |
7512 | { | |
7513 | Expression* arg = this->one_arg(); | |
7514 | if (arg == NULL) | |
7515 | return false; | |
7516 | return arg->field_reference_expression() != NULL; | |
7517 | } | |
7518 | ||
48080209 | 7519 | case BUILTIN_COMPLEX: |
e440a328 | 7520 | { |
7521 | const Expression_list* args = this->args(); | |
7522 | if (args != NULL && args->size() == 2) | |
7523 | return args->front()->is_constant() && args->back()->is_constant(); | |
7524 | } | |
7525 | break; | |
7526 | ||
7527 | case BUILTIN_REAL: | |
7528 | case BUILTIN_IMAG: | |
7529 | { | |
7530 | Expression* arg = this->one_arg(); | |
7531 | return arg != NULL && arg->is_constant(); | |
7532 | } | |
7533 | ||
7534 | default: | |
7535 | break; | |
7536 | } | |
7537 | ||
7538 | return false; | |
7539 | } | |
7540 | ||
0c77715b | 7541 | // Return a numeric constant if possible. |
e440a328 | 7542 | |
7543 | bool | |
0c77715b | 7544 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7545 | { |
7546 | if (this->code_ == BUILTIN_LEN | |
7547 | || this->code_ == BUILTIN_CAP) | |
7548 | { | |
7549 | Expression* arg = this->one_arg(); | |
7550 | if (arg == NULL) | |
7551 | return false; | |
7552 | Type* arg_type = arg->type(); | |
7553 | ||
7554 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7555 | { | |
7556 | std::string sval; | |
7557 | if (arg->string_constant_value(&sval)) | |
7558 | { | |
0c77715b | 7559 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7560 | sval.length()); | |
e440a328 | 7561 | return true; |
7562 | } | |
7563 | } | |
7564 | ||
7565 | if (arg_type->points_to() != NULL | |
7566 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7567 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7568 | arg_type = arg_type->points_to(); |
7569 | ||
7570 | if (arg_type->array_type() != NULL | |
7571 | && arg_type->array_type()->length() != NULL) | |
7572 | { | |
0f914071 | 7573 | if (this->seen_) |
7574 | return false; | |
e440a328 | 7575 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7576 | this->seen_ = true; |
0c77715b | 7577 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7578 | this->seen_ = false; |
7579 | if (r) | |
e440a328 | 7580 | { |
0c77715b | 7581 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7582 | this->location())) | |
7583 | r = false; | |
e440a328 | 7584 | } |
0c77715b | 7585 | return r; |
e440a328 | 7586 | } |
7587 | } | |
7588 | else if (this->code_ == BUILTIN_SIZEOF | |
7589 | || this->code_ == BUILTIN_ALIGNOF) | |
7590 | { | |
7591 | Expression* arg = this->one_arg(); | |
7592 | if (arg == NULL) | |
7593 | return false; | |
7594 | Type* arg_type = arg->type(); | |
5c13bd80 | 7595 | if (arg_type->is_error()) |
e440a328 | 7596 | return false; |
7597 | if (arg_type->is_abstract()) | |
7598 | return false; | |
2c809f8f | 7599 | if (this->seen_) |
7600 | return false; | |
927a01eb | 7601 | |
5892f89f | 7602 | unsigned long ret; |
e440a328 | 7603 | if (this->code_ == BUILTIN_SIZEOF) |
7604 | { | |
2c809f8f | 7605 | this->seen_ = true; |
7606 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
7607 | this->seen_ = false; | |
7608 | if (!ok) | |
e440a328 | 7609 | return false; |
7610 | } | |
7611 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7612 | { | |
2c809f8f | 7613 | bool ok; |
7614 | this->seen_ = true; | |
637bd3af | 7615 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 7616 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 7617 | else |
e440a328 | 7618 | { |
7619 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7620 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 7621 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 7622 | } |
2c809f8f | 7623 | this->seen_ = false; |
7624 | if (!ok) | |
7625 | return false; | |
e440a328 | 7626 | } |
7627 | else | |
c3e6f413 | 7628 | go_unreachable(); |
927a01eb | 7629 | |
5892f89f | 7630 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), ret); |
e440a328 | 7631 | return true; |
7632 | } | |
7633 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7634 | { | |
7635 | Expression* arg = this->one_arg(); | |
7636 | if (arg == NULL) | |
7637 | return false; | |
7638 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7639 | if (farg == NULL) | |
7640 | return false; | |
2c809f8f | 7641 | if (this->seen_) |
7642 | return false; | |
7643 | ||
9a4bd570 | 7644 | unsigned int total_offset = 0; |
7645 | while (true) | |
7646 | { | |
7647 | Expression* struct_expr = farg->expr(); | |
7648 | Type* st = struct_expr->type(); | |
7649 | if (st->struct_type() == NULL) | |
7650 | return false; | |
7651 | if (st->named_type() != NULL) | |
7652 | st->named_type()->convert(this->gogo_); | |
7653 | unsigned int offset; | |
2c809f8f | 7654 | this->seen_ = true; |
7655 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
7656 | farg->field_index(), | |
7657 | &offset); | |
7658 | this->seen_ = false; | |
7659 | if (!ok) | |
7660 | return false; | |
9a4bd570 | 7661 | total_offset += offset; |
7662 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
7663 | { | |
7664 | // Go up until we reach the original base. | |
7665 | farg = struct_expr->field_reference_expression(); | |
7666 | continue; | |
7667 | } | |
7668 | break; | |
7669 | } | |
7ba86326 | 7670 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
9a4bd570 | 7671 | static_cast<unsigned long>(total_offset)); |
e440a328 | 7672 | return true; |
7673 | } | |
0c77715b | 7674 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7675 | { |
7676 | Expression* arg = this->one_arg(); | |
7677 | if (arg == NULL) | |
7678 | return false; | |
7679 | ||
0c77715b | 7680 | Numeric_constant argnc; |
7681 | if (!arg->numeric_constant_value(&argnc)) | |
7682 | return false; | |
7683 | ||
e440a328 | 7684 | mpfr_t real; |
7685 | mpfr_t imag; | |
0c77715b | 7686 | if (!argnc.to_complex(&real, &imag)) |
7687 | return false; | |
e440a328 | 7688 | |
0c77715b | 7689 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7690 | if (this->code_ == BUILTIN_REAL) | |
7691 | nc->set_float(type, real); | |
7692 | else | |
7693 | nc->set_float(type, imag); | |
7694 | return true; | |
e440a328 | 7695 | } |
0c77715b | 7696 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7697 | { |
7698 | const Expression_list* args = this->args(); | |
7699 | if (args == NULL || args->size() != 2) | |
7700 | return false; | |
7701 | ||
0c77715b | 7702 | Numeric_constant rnc; |
7703 | if (!args->front()->numeric_constant_value(&rnc)) | |
7704 | return false; | |
7705 | Numeric_constant inc; | |
7706 | if (!args->back()->numeric_constant_value(&inc)) | |
7707 | return false; | |
7708 | ||
7709 | if (rnc.type() != NULL | |
7710 | && !rnc.type()->is_abstract() | |
7711 | && inc.type() != NULL | |
7712 | && !inc.type()->is_abstract() | |
7713 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7714 | return false; | |
7715 | ||
e440a328 | 7716 | mpfr_t r; |
0c77715b | 7717 | if (!rnc.to_float(&r)) |
7718 | return false; | |
7719 | mpfr_t i; | |
7720 | if (!inc.to_float(&i)) | |
e440a328 | 7721 | { |
7722 | mpfr_clear(r); | |
7723 | return false; | |
7724 | } | |
7725 | ||
0c77715b | 7726 | Type* arg_type = rnc.type(); |
7727 | if (arg_type == NULL || arg_type->is_abstract()) | |
7728 | arg_type = inc.type(); | |
e440a328 | 7729 | |
0c77715b | 7730 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7731 | nc->set_complex(type, r, i); | |
e440a328 | 7732 | |
7733 | mpfr_clear(r); | |
7734 | mpfr_clear(i); | |
7735 | ||
0c77715b | 7736 | return true; |
e440a328 | 7737 | } |
7738 | ||
7739 | return false; | |
7740 | } | |
7741 | ||
a7549a6a | 7742 | // Give an error if we are discarding the value of an expression which |
7743 | // should not normally be discarded. We don't give an error for | |
7744 | // discarding the value of an ordinary function call, but we do for | |
7745 | // builtin functions, purely for consistency with the gc compiler. | |
7746 | ||
4f2138d7 | 7747 | bool |
a7549a6a | 7748 | Builtin_call_expression::do_discarding_value() |
7749 | { | |
7750 | switch (this->code_) | |
7751 | { | |
7752 | case BUILTIN_INVALID: | |
7753 | default: | |
7754 | go_unreachable(); | |
7755 | ||
7756 | case BUILTIN_APPEND: | |
7757 | case BUILTIN_CAP: | |
7758 | case BUILTIN_COMPLEX: | |
7759 | case BUILTIN_IMAG: | |
7760 | case BUILTIN_LEN: | |
7761 | case BUILTIN_MAKE: | |
7762 | case BUILTIN_NEW: | |
7763 | case BUILTIN_REAL: | |
7764 | case BUILTIN_ALIGNOF: | |
7765 | case BUILTIN_OFFSETOF: | |
7766 | case BUILTIN_SIZEOF: | |
7767 | this->unused_value_error(); | |
4f2138d7 | 7768 | return false; |
a7549a6a | 7769 | |
7770 | case BUILTIN_CLOSE: | |
7771 | case BUILTIN_COPY: | |
1cce762f | 7772 | case BUILTIN_DELETE: |
a7549a6a | 7773 | case BUILTIN_PANIC: |
7774 | case BUILTIN_PRINT: | |
7775 | case BUILTIN_PRINTLN: | |
7776 | case BUILTIN_RECOVER: | |
4f2138d7 | 7777 | return true; |
a7549a6a | 7778 | } |
7779 | } | |
7780 | ||
e440a328 | 7781 | // Return the type. |
7782 | ||
7783 | Type* | |
7784 | Builtin_call_expression::do_type() | |
7785 | { | |
7786 | switch (this->code_) | |
7787 | { | |
7788 | case BUILTIN_INVALID: | |
7789 | default: | |
c3e6f413 | 7790 | go_unreachable(); |
e440a328 | 7791 | |
7792 | case BUILTIN_NEW: | |
7793 | case BUILTIN_MAKE: | |
7794 | { | |
7795 | const Expression_list* args = this->args(); | |
7796 | if (args == NULL || args->empty()) | |
7797 | return Type::make_error_type(); | |
7798 | return Type::make_pointer_type(args->front()->type()); | |
7799 | } | |
7800 | ||
7801 | case BUILTIN_CAP: | |
7802 | case BUILTIN_COPY: | |
7803 | case BUILTIN_LEN: | |
7ba86326 | 7804 | return Type::lookup_integer_type("int"); |
7805 | ||
e440a328 | 7806 | case BUILTIN_ALIGNOF: |
7807 | case BUILTIN_OFFSETOF: | |
7808 | case BUILTIN_SIZEOF: | |
7ba86326 | 7809 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7810 | |
7811 | case BUILTIN_CLOSE: | |
1cce762f | 7812 | case BUILTIN_DELETE: |
e440a328 | 7813 | case BUILTIN_PANIC: |
7814 | case BUILTIN_PRINT: | |
7815 | case BUILTIN_PRINTLN: | |
7816 | return Type::make_void_type(); | |
7817 | ||
e440a328 | 7818 | case BUILTIN_RECOVER: |
823c7e3d | 7819 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7820 | |
7821 | case BUILTIN_APPEND: | |
7822 | { | |
7823 | const Expression_list* args = this->args(); | |
7824 | if (args == NULL || args->empty()) | |
7825 | return Type::make_error_type(); | |
3ff4863b | 7826 | Type *ret = args->front()->type(); |
7827 | if (!ret->is_slice_type()) | |
7828 | return Type::make_error_type(); | |
7829 | return ret; | |
e440a328 | 7830 | } |
7831 | ||
7832 | case BUILTIN_REAL: | |
7833 | case BUILTIN_IMAG: | |
7834 | { | |
7835 | Expression* arg = this->one_arg(); | |
7836 | if (arg == NULL) | |
7837 | return Type::make_error_type(); | |
7838 | Type* t = arg->type(); | |
7839 | if (t->is_abstract()) | |
7840 | t = t->make_non_abstract_type(); | |
7841 | t = Builtin_call_expression::real_imag_type(t); | |
7842 | if (t == NULL) | |
7843 | t = Type::make_error_type(); | |
7844 | return t; | |
7845 | } | |
7846 | ||
48080209 | 7847 | case BUILTIN_COMPLEX: |
e440a328 | 7848 | { |
7849 | const Expression_list* args = this->args(); | |
7850 | if (args == NULL || args->size() != 2) | |
7851 | return Type::make_error_type(); | |
7852 | Type* t = args->front()->type(); | |
7853 | if (t->is_abstract()) | |
7854 | { | |
7855 | t = args->back()->type(); | |
7856 | if (t->is_abstract()) | |
7857 | t = t->make_non_abstract_type(); | |
7858 | } | |
48080209 | 7859 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7860 | if (t == NULL) |
7861 | t = Type::make_error_type(); | |
7862 | return t; | |
7863 | } | |
7864 | } | |
7865 | } | |
7866 | ||
7867 | // Determine the type. | |
7868 | ||
7869 | void | |
7870 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7871 | { | |
fb94b0ca | 7872 | if (!this->determining_types()) |
7873 | return; | |
7874 | ||
e440a328 | 7875 | this->fn()->determine_type_no_context(); |
7876 | ||
7877 | const Expression_list* args = this->args(); | |
7878 | ||
7879 | bool is_print; | |
7880 | Type* arg_type = NULL; | |
7881 | switch (this->code_) | |
7882 | { | |
7883 | case BUILTIN_PRINT: | |
7884 | case BUILTIN_PRINTLN: | |
7885 | // Do not force a large integer constant to "int". | |
7886 | is_print = true; | |
7887 | break; | |
7888 | ||
7889 | case BUILTIN_REAL: | |
7890 | case BUILTIN_IMAG: | |
48080209 | 7891 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 7892 | if (arg_type == NULL) |
7893 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 7894 | is_print = false; |
7895 | break; | |
7896 | ||
48080209 | 7897 | case BUILTIN_COMPLEX: |
e440a328 | 7898 | { |
48080209 | 7899 | // For the complex function the type of one operand can |
e440a328 | 7900 | // determine the type of the other, as in a binary expression. |
7901 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 7902 | if (arg_type == NULL) |
7903 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 7904 | if (args != NULL && args->size() == 2) |
7905 | { | |
7906 | Type* t1 = args->front()->type(); | |
c849bb59 | 7907 | Type* t2 = args->back()->type(); |
e440a328 | 7908 | if (!t1->is_abstract()) |
7909 | arg_type = t1; | |
7910 | else if (!t2->is_abstract()) | |
7911 | arg_type = t2; | |
7912 | } | |
7913 | is_print = false; | |
7914 | } | |
7915 | break; | |
7916 | ||
7917 | default: | |
7918 | is_print = false; | |
7919 | break; | |
7920 | } | |
7921 | ||
7922 | if (args != NULL) | |
7923 | { | |
7924 | for (Expression_list::const_iterator pa = args->begin(); | |
7925 | pa != args->end(); | |
7926 | ++pa) | |
7927 | { | |
7928 | Type_context subcontext; | |
7929 | subcontext.type = arg_type; | |
7930 | ||
7931 | if (is_print) | |
7932 | { | |
7933 | // We want to print large constants, we so can't just | |
7934 | // use the appropriate nonabstract type. Use uint64 for | |
7935 | // an integer if we know it is nonnegative, otherwise | |
7936 | // use int64 for a integer, otherwise use float64 for a | |
7937 | // float or complex128 for a complex. | |
7938 | Type* want_type = NULL; | |
7939 | Type* atype = (*pa)->type(); | |
7940 | if (atype->is_abstract()) | |
7941 | { | |
7942 | if (atype->integer_type() != NULL) | |
7943 | { | |
0c77715b | 7944 | Numeric_constant nc; |
7945 | if (this->numeric_constant_value(&nc)) | |
7946 | { | |
7947 | mpz_t val; | |
7948 | if (nc.to_int(&val)) | |
7949 | { | |
7950 | if (mpz_sgn(val) >= 0) | |
7951 | want_type = Type::lookup_integer_type("uint64"); | |
7952 | mpz_clear(val); | |
7953 | } | |
7954 | } | |
7955 | if (want_type == NULL) | |
e440a328 | 7956 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 7957 | } |
7958 | else if (atype->float_type() != NULL) | |
7959 | want_type = Type::lookup_float_type("float64"); | |
7960 | else if (atype->complex_type() != NULL) | |
7961 | want_type = Type::lookup_complex_type("complex128"); | |
7962 | else if (atype->is_abstract_string_type()) | |
7963 | want_type = Type::lookup_string_type(); | |
7964 | else if (atype->is_abstract_boolean_type()) | |
7965 | want_type = Type::lookup_bool_type(); | |
7966 | else | |
c3e6f413 | 7967 | go_unreachable(); |
e440a328 | 7968 | subcontext.type = want_type; |
7969 | } | |
7970 | } | |
7971 | ||
7972 | (*pa)->determine_type(&subcontext); | |
7973 | } | |
7974 | } | |
7975 | } | |
7976 | ||
7977 | // If there is exactly one argument, return true. Otherwise give an | |
7978 | // error message and return false. | |
7979 | ||
7980 | bool | |
7981 | Builtin_call_expression::check_one_arg() | |
7982 | { | |
7983 | const Expression_list* args = this->args(); | |
7984 | if (args == NULL || args->size() < 1) | |
7985 | { | |
7986 | this->report_error(_("not enough arguments")); | |
7987 | return false; | |
7988 | } | |
7989 | else if (args->size() > 1) | |
7990 | { | |
7991 | this->report_error(_("too many arguments")); | |
7992 | return false; | |
7993 | } | |
7994 | if (args->front()->is_error_expression() | |
5c13bd80 | 7995 | || args->front()->type()->is_error()) |
e440a328 | 7996 | { |
7997 | this->set_is_error(); | |
7998 | return false; | |
7999 | } | |
8000 | return true; | |
8001 | } | |
8002 | ||
8003 | // Check argument types for a builtin function. | |
8004 | ||
8005 | void | |
8006 | Builtin_call_expression::do_check_types(Gogo*) | |
8007 | { | |
375646ea | 8008 | if (this->is_error_expression()) |
8009 | return; | |
e440a328 | 8010 | switch (this->code_) |
8011 | { | |
8012 | case BUILTIN_INVALID: | |
8013 | case BUILTIN_NEW: | |
8014 | case BUILTIN_MAKE: | |
cd238b8d | 8015 | case BUILTIN_DELETE: |
e440a328 | 8016 | return; |
8017 | ||
8018 | case BUILTIN_LEN: | |
8019 | case BUILTIN_CAP: | |
8020 | { | |
8021 | // The single argument may be either a string or an array or a | |
8022 | // map or a channel, or a pointer to a closed array. | |
8023 | if (this->check_one_arg()) | |
8024 | { | |
8025 | Type* arg_type = this->one_arg()->type(); | |
8026 | if (arg_type->points_to() != NULL | |
8027 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8028 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8029 | arg_type = arg_type->points_to(); |
8030 | if (this->code_ == BUILTIN_CAP) | |
8031 | { | |
5c13bd80 | 8032 | if (!arg_type->is_error() |
e440a328 | 8033 | && arg_type->array_type() == NULL |
8034 | && arg_type->channel_type() == NULL) | |
8035 | this->report_error(_("argument must be array or slice " | |
8036 | "or channel")); | |
8037 | } | |
8038 | else | |
8039 | { | |
5c13bd80 | 8040 | if (!arg_type->is_error() |
e440a328 | 8041 | && !arg_type->is_string_type() |
8042 | && arg_type->array_type() == NULL | |
8043 | && arg_type->map_type() == NULL | |
8044 | && arg_type->channel_type() == NULL) | |
8045 | this->report_error(_("argument must be string or " | |
8046 | "array or slice or map or channel")); | |
8047 | } | |
8048 | } | |
8049 | } | |
8050 | break; | |
8051 | ||
8052 | case BUILTIN_PRINT: | |
8053 | case BUILTIN_PRINTLN: | |
8054 | { | |
8055 | const Expression_list* args = this->args(); | |
8056 | if (args == NULL) | |
8057 | { | |
8058 | if (this->code_ == BUILTIN_PRINT) | |
8059 | warning_at(this->location(), 0, | |
8060 | "no arguments for builtin function %<%s%>", | |
8061 | (this->code_ == BUILTIN_PRINT | |
8062 | ? "print" | |
8063 | : "println")); | |
8064 | } | |
8065 | else | |
8066 | { | |
8067 | for (Expression_list::const_iterator p = args->begin(); | |
8068 | p != args->end(); | |
8069 | ++p) | |
8070 | { | |
8071 | Type* type = (*p)->type(); | |
5c13bd80 | 8072 | if (type->is_error() |
e440a328 | 8073 | || type->is_string_type() |
8074 | || type->integer_type() != NULL | |
8075 | || type->float_type() != NULL | |
8076 | || type->complex_type() != NULL | |
8077 | || type->is_boolean_type() | |
8078 | || type->points_to() != NULL | |
8079 | || type->interface_type() != NULL | |
8080 | || type->channel_type() != NULL | |
8081 | || type->map_type() != NULL | |
8082 | || type->function_type() != NULL | |
411eb89e | 8083 | || type->is_slice_type()) |
e440a328 | 8084 | ; |
acf8e158 | 8085 | else if ((*p)->is_type_expression()) |
8086 | { | |
8087 | // If this is a type expression it's going to give | |
8088 | // an error anyhow, so we don't need one here. | |
8089 | } | |
e440a328 | 8090 | else |
8091 | this->report_error(_("unsupported argument type to " | |
8092 | "builtin function")); | |
8093 | } | |
8094 | } | |
8095 | } | |
8096 | break; | |
8097 | ||
8098 | case BUILTIN_CLOSE: | |
e440a328 | 8099 | if (this->check_one_arg()) |
8100 | { | |
8101 | if (this->one_arg()->type()->channel_type() == NULL) | |
8102 | this->report_error(_("argument must be channel")); | |
5202d986 | 8103 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8104 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8105 | } |
8106 | break; | |
8107 | ||
8108 | case BUILTIN_PANIC: | |
8109 | case BUILTIN_SIZEOF: | |
8110 | case BUILTIN_ALIGNOF: | |
8111 | this->check_one_arg(); | |
8112 | break; | |
8113 | ||
8114 | case BUILTIN_RECOVER: | |
8115 | if (this->args() != NULL && !this->args()->empty()) | |
8116 | this->report_error(_("too many arguments")); | |
8117 | break; | |
8118 | ||
8119 | case BUILTIN_OFFSETOF: | |
8120 | if (this->check_one_arg()) | |
8121 | { | |
8122 | Expression* arg = this->one_arg(); | |
8123 | if (arg->field_reference_expression() == NULL) | |
8124 | this->report_error(_("argument must be a field reference")); | |
8125 | } | |
8126 | break; | |
8127 | ||
8128 | case BUILTIN_COPY: | |
8129 | { | |
8130 | const Expression_list* args = this->args(); | |
8131 | if (args == NULL || args->size() < 2) | |
8132 | { | |
8133 | this->report_error(_("not enough arguments")); | |
8134 | break; | |
8135 | } | |
8136 | else if (args->size() > 2) | |
8137 | { | |
8138 | this->report_error(_("too many arguments")); | |
8139 | break; | |
8140 | } | |
8141 | Type* arg1_type = args->front()->type(); | |
8142 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8143 | if (arg1_type->is_error() || arg2_type->is_error()) |
e440a328 | 8144 | break; |
8145 | ||
8146 | Type* e1; | |
411eb89e | 8147 | if (arg1_type->is_slice_type()) |
e440a328 | 8148 | e1 = arg1_type->array_type()->element_type(); |
8149 | else | |
8150 | { | |
8151 | this->report_error(_("left argument must be a slice")); | |
8152 | break; | |
8153 | } | |
8154 | ||
411eb89e | 8155 | if (arg2_type->is_slice_type()) |
60963afd | 8156 | { |
8157 | Type* e2 = arg2_type->array_type()->element_type(); | |
8158 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8159 | this->report_error(_("element types must be the same")); | |
8160 | } | |
e440a328 | 8161 | else if (arg2_type->is_string_type()) |
e440a328 | 8162 | { |
60963afd | 8163 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8164 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8165 | } |
60963afd | 8166 | else |
8167 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8168 | } |
8169 | break; | |
8170 | ||
8171 | case BUILTIN_APPEND: | |
8172 | { | |
8173 | const Expression_list* args = this->args(); | |
b0d311a1 | 8174 | if (args == NULL || args->size() < 2) |
e440a328 | 8175 | { |
8176 | this->report_error(_("not enough arguments")); | |
8177 | break; | |
8178 | } | |
0b7755ec | 8179 | if (args->size() > 2) |
8180 | { | |
8181 | this->report_error(_("too many arguments")); | |
8182 | break; | |
8183 | } | |
cd238b8d | 8184 | if (args->front()->type()->is_error() |
8185 | || args->back()->type()->is_error()) | |
8186 | break; | |
8187 | ||
8188 | Array_type* at = args->front()->type()->array_type(); | |
8189 | Type* e = at->element_type(); | |
4fd4fcf4 | 8190 | |
8191 | // The language permits appending a string to a []byte, as a | |
8192 | // special case. | |
8193 | if (args->back()->type()->is_string_type()) | |
8194 | { | |
60963afd | 8195 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 8196 | break; |
8197 | } | |
8198 | ||
19fd40c3 | 8199 | // The language says that the second argument must be |
8200 | // assignable to a slice of the element type of the first | |
8201 | // argument. We already know the first argument is a slice | |
8202 | // type. | |
cd238b8d | 8203 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 8204 | std::string reason; |
19fd40c3 | 8205 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 8206 | { |
8207 | if (reason.empty()) | |
19fd40c3 | 8208 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 8209 | else |
8210 | { | |
19fd40c3 | 8211 | error_at(this->location(), "argument 2 has invalid type (%s)", |
e440a328 | 8212 | reason.c_str()); |
8213 | this->set_is_error(); | |
8214 | } | |
8215 | } | |
8216 | break; | |
8217 | } | |
8218 | ||
8219 | case BUILTIN_REAL: | |
8220 | case BUILTIN_IMAG: | |
8221 | if (this->check_one_arg()) | |
8222 | { | |
8223 | if (this->one_arg()->type()->complex_type() == NULL) | |
8224 | this->report_error(_("argument must have complex type")); | |
8225 | } | |
8226 | break; | |
8227 | ||
48080209 | 8228 | case BUILTIN_COMPLEX: |
e440a328 | 8229 | { |
8230 | const Expression_list* args = this->args(); | |
8231 | if (args == NULL || args->size() < 2) | |
8232 | this->report_error(_("not enough arguments")); | |
8233 | else if (args->size() > 2) | |
8234 | this->report_error(_("too many arguments")); | |
8235 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8236 | || args->front()->type()->is_error() |
e440a328 | 8237 | || args->back()->is_error_expression() |
5c13bd80 | 8238 | || args->back()->type()->is_error()) |
e440a328 | 8239 | this->set_is_error(); |
8240 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8241 | args->back()->type(), true, NULL)) |
48080209 | 8242 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8243 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8244 | this->report_error(_("complex arguments must have " |
e440a328 | 8245 | "floating-point type")); |
8246 | } | |
8247 | break; | |
8248 | ||
8249 | default: | |
c3e6f413 | 8250 | go_unreachable(); |
e440a328 | 8251 | } |
8252 | } | |
8253 | ||
ea664253 | 8254 | // Return the backend representation for a builtin function. |
e440a328 | 8255 | |
ea664253 | 8256 | Bexpression* |
8257 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8258 | { |
8259 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8260 | Location location = this->location(); |
e440a328 | 8261 | switch (this->code_) |
8262 | { | |
8263 | case BUILTIN_INVALID: | |
8264 | case BUILTIN_NEW: | |
8265 | case BUILTIN_MAKE: | |
c3e6f413 | 8266 | go_unreachable(); |
e440a328 | 8267 | |
8268 | case BUILTIN_LEN: | |
8269 | case BUILTIN_CAP: | |
8270 | { | |
8271 | const Expression_list* args = this->args(); | |
c484d925 | 8272 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8273 | Expression* arg = args->front(); |
e440a328 | 8274 | Type* arg_type = arg->type(); |
0f914071 | 8275 | |
8276 | if (this->seen_) | |
8277 | { | |
c484d925 | 8278 | go_assert(saw_errors()); |
ea664253 | 8279 | return context->backend()->error_expression(); |
0f914071 | 8280 | } |
8281 | this->seen_ = true; | |
0f914071 | 8282 | this->seen_ = false; |
e440a328 | 8283 | if (arg_type->points_to() != NULL) |
8284 | { | |
8285 | arg_type = arg_type->points_to(); | |
c484d925 | 8286 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8287 | && !arg_type->is_slice_type()); |
2c809f8f | 8288 | arg = Expression::make_unary(OPERATOR_MULT, arg, location); |
e440a328 | 8289 | } |
8290 | ||
1b1f2abf | 8291 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8292 | Expression* val; |
e440a328 | 8293 | if (this->code_ == BUILTIN_LEN) |
8294 | { | |
8295 | if (arg_type->is_string_type()) | |
2c809f8f | 8296 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8297 | location); | |
e440a328 | 8298 | else if (arg_type->array_type() != NULL) |
0f914071 | 8299 | { |
8300 | if (this->seen_) | |
8301 | { | |
c484d925 | 8302 | go_assert(saw_errors()); |
ea664253 | 8303 | return context->backend()->error_expression(); |
0f914071 | 8304 | } |
8305 | this->seen_ = true; | |
2c809f8f | 8306 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8307 | this->seen_ = false; |
8308 | } | |
e440a328 | 8309 | else if (arg_type->map_type() != NULL) |
2c809f8f | 8310 | val = Runtime::make_call(Runtime::MAP_LEN, location, 1, arg); |
e440a328 | 8311 | else if (arg_type->channel_type() != NULL) |
2c809f8f | 8312 | val = Runtime::make_call(Runtime::CHAN_LEN, location, 1, arg); |
e440a328 | 8313 | else |
c3e6f413 | 8314 | go_unreachable(); |
e440a328 | 8315 | } |
8316 | else | |
8317 | { | |
8318 | if (arg_type->array_type() != NULL) | |
0f914071 | 8319 | { |
8320 | if (this->seen_) | |
8321 | { | |
c484d925 | 8322 | go_assert(saw_errors()); |
ea664253 | 8323 | return context->backend()->error_expression(); |
0f914071 | 8324 | } |
8325 | this->seen_ = true; | |
2c809f8f | 8326 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8327 | this->seen_ = false; |
8328 | } | |
e440a328 | 8329 | else if (arg_type->channel_type() != NULL) |
2c809f8f | 8330 | val = Runtime::make_call(Runtime::CHAN_CAP, location, 1, arg); |
e440a328 | 8331 | else |
c3e6f413 | 8332 | go_unreachable(); |
e440a328 | 8333 | } |
8334 | ||
2c809f8f | 8335 | return Expression::make_cast(int_type, val, |
ea664253 | 8336 | location)->get_backend(context); |
e440a328 | 8337 | } |
8338 | ||
8339 | case BUILTIN_PRINT: | |
8340 | case BUILTIN_PRINTLN: | |
8341 | { | |
8342 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
2c809f8f | 8343 | Expression* print_stmts = NULL; |
e440a328 | 8344 | |
8345 | const Expression_list* call_args = this->args(); | |
8346 | if (call_args != NULL) | |
8347 | { | |
8348 | for (Expression_list::const_iterator p = call_args->begin(); | |
8349 | p != call_args->end(); | |
8350 | ++p) | |
8351 | { | |
8352 | if (is_ln && p != call_args->begin()) | |
8353 | { | |
2c809f8f | 8354 | Expression* print_space = |
8355 | Runtime::make_call(Runtime::PRINT_SPACE, | |
8356 | this->location(), 0); | |
e440a328 | 8357 | |
2c809f8f | 8358 | print_stmts = |
8359 | Expression::make_compound(print_stmts, print_space, | |
8360 | location); | |
8361 | } | |
e440a328 | 8362 | |
2c809f8f | 8363 | Expression* arg = *p; |
8364 | Type* type = arg->type(); | |
8365 | Runtime::Function code; | |
e440a328 | 8366 | if (type->is_string_type()) |
2c809f8f | 8367 | code = Runtime::PRINT_STRING; |
e440a328 | 8368 | else if (type->integer_type() != NULL |
8369 | && type->integer_type()->is_unsigned()) | |
8370 | { | |
e440a328 | 8371 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 8372 | arg = Expression::make_cast(itype, arg, location); |
8373 | code = Runtime::PRINT_UINT64; | |
e440a328 | 8374 | } |
8375 | else if (type->integer_type() != NULL) | |
8376 | { | |
e440a328 | 8377 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 8378 | arg = Expression::make_cast(itype, arg, location); |
8379 | code = Runtime::PRINT_INT64; | |
e440a328 | 8380 | } |
8381 | else if (type->float_type() != NULL) | |
8382 | { | |
2c809f8f | 8383 | Type* dtype = Type::lookup_float_type("float64"); |
8384 | arg = Expression::make_cast(dtype, arg, location); | |
8385 | code = Runtime::PRINT_DOUBLE; | |
e440a328 | 8386 | } |
8387 | else if (type->complex_type() != NULL) | |
8388 | { | |
2c809f8f | 8389 | Type* ctype = Type::lookup_complex_type("complex128"); |
8390 | arg = Expression::make_cast(ctype, arg, location); | |
8391 | code = Runtime::PRINT_COMPLEX; | |
e440a328 | 8392 | } |
8393 | else if (type->is_boolean_type()) | |
2c809f8f | 8394 | code = Runtime::PRINT_BOOL; |
e440a328 | 8395 | else if (type->points_to() != NULL |
8396 | || type->channel_type() != NULL | |
8397 | || type->map_type() != NULL | |
8398 | || type->function_type() != NULL) | |
8399 | { | |
2c809f8f | 8400 | arg = Expression::make_cast(type, arg, location); |
8401 | code = Runtime::PRINT_POINTER; | |
e440a328 | 8402 | } |
8403 | else if (type->interface_type() != NULL) | |
8404 | { | |
8405 | if (type->interface_type()->is_empty()) | |
2c809f8f | 8406 | code = Runtime::PRINT_EMPTY_INTERFACE; |
e440a328 | 8407 | else |
2c809f8f | 8408 | code = Runtime::PRINT_INTERFACE; |
e440a328 | 8409 | } |
411eb89e | 8410 | else if (type->is_slice_type()) |
2c809f8f | 8411 | code = Runtime::PRINT_SLICE; |
e440a328 | 8412 | else |
cd238b8d | 8413 | { |
8414 | go_assert(saw_errors()); | |
ea664253 | 8415 | return context->backend()->error_expression(); |
cd238b8d | 8416 | } |
e440a328 | 8417 | |
2c809f8f | 8418 | Expression* call = Runtime::make_call(code, location, 1, arg); |
8419 | if (print_stmts == NULL) | |
8420 | print_stmts = call; | |
8421 | else | |
8422 | print_stmts = Expression::make_compound(print_stmts, call, | |
8423 | location); | |
e440a328 | 8424 | } |
8425 | } | |
8426 | ||
8427 | if (is_ln) | |
8428 | { | |
2c809f8f | 8429 | Expression* print_nl = |
8430 | Runtime::make_call(Runtime::PRINT_NL, location, 0); | |
8431 | if (print_stmts == NULL) | |
8432 | print_stmts = print_nl; | |
8433 | else | |
8434 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
8435 | location); | |
e440a328 | 8436 | } |
8437 | ||
ea664253 | 8438 | return print_stmts->get_backend(context); |
e440a328 | 8439 | } |
8440 | ||
8441 | case BUILTIN_PANIC: | |
8442 | { | |
8443 | const Expression_list* args = this->args(); | |
c484d925 | 8444 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8445 | Expression* arg = args->front(); |
b13c66cd | 8446 | Type *empty = |
823c7e3d | 8447 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 8448 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
8449 | ||
8450 | Expression* panic = | |
8451 | Runtime::make_call(Runtime::PANIC, location, 1, arg); | |
ea664253 | 8452 | return panic->get_backend(context); |
e440a328 | 8453 | } |
8454 | ||
8455 | case BUILTIN_RECOVER: | |
8456 | { | |
8457 | // The argument is set when building recover thunks. It's a | |
8458 | // boolean value which is true if we can recover a value now. | |
8459 | const Expression_list* args = this->args(); | |
c484d925 | 8460 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8461 | Expression* arg = args->front(); |
b13c66cd | 8462 | Type *empty = |
823c7e3d | 8463 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8464 | |
e440a328 | 8465 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 8466 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 8467 | |
8468 | // We need to handle a deferred call to recover specially, | |
8469 | // because it changes whether it can recover a panic or not. | |
8470 | // See test7 in test/recover1.go. | |
2c809f8f | 8471 | Expression* recover = Runtime::make_call((this->is_deferred() |
8472 | ? Runtime::DEFERRED_RECOVER | |
8473 | : Runtime::RECOVER), | |
8474 | location, 0); | |
8475 | Expression* cond = | |
8476 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 8477 | return cond->get_backend(context); |
e440a328 | 8478 | } |
8479 | ||
8480 | case BUILTIN_CLOSE: | |
e440a328 | 8481 | { |
8482 | const Expression_list* args = this->args(); | |
c484d925 | 8483 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8484 | Expression* arg = args->front(); |
2c809f8f | 8485 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
8486 | 1, arg); | |
ea664253 | 8487 | return close->get_backend(context); |
e440a328 | 8488 | } |
8489 | ||
8490 | case BUILTIN_SIZEOF: | |
8491 | case BUILTIN_OFFSETOF: | |
8492 | case BUILTIN_ALIGNOF: | |
8493 | { | |
0c77715b | 8494 | Numeric_constant nc; |
8495 | unsigned long val; | |
8496 | if (!this->numeric_constant_value(&nc) | |
8497 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8498 | { |
c484d925 | 8499 | go_assert(saw_errors()); |
ea664253 | 8500 | return context->backend()->error_expression(); |
7f1d9abd | 8501 | } |
7ba86326 | 8502 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 8503 | mpz_t ival; |
8504 | nc.get_int(&ival); | |
8505 | Expression* int_cst = | |
8506 | Expression::make_integer(&ival, uintptr_type, location); | |
8507 | mpz_clear(ival); | |
ea664253 | 8508 | return int_cst->get_backend(context); |
e440a328 | 8509 | } |
8510 | ||
8511 | case BUILTIN_COPY: | |
8512 | { | |
8513 | const Expression_list* args = this->args(); | |
c484d925 | 8514 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8515 | Expression* arg1 = args->front(); |
8516 | Expression* arg2 = args->back(); | |
8517 | ||
e440a328 | 8518 | Type* arg1_type = arg1->type(); |
8519 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 8520 | go_assert(arg1->is_variable()); |
2c809f8f | 8521 | Expression* arg1_val = at->get_value_pointer(gogo, arg1); |
8522 | Expression* arg1_len = at->get_length(gogo, arg1); | |
e440a328 | 8523 | |
8524 | Type* arg2_type = arg2->type(); | |
2c809f8f | 8525 | go_assert(arg2->is_variable()); |
8526 | Expression* arg2_val; | |
8527 | Expression* arg2_len; | |
411eb89e | 8528 | if (arg2_type->is_slice_type()) |
e440a328 | 8529 | { |
8530 | at = arg2_type->array_type(); | |
2c809f8f | 8531 | arg2_val = at->get_value_pointer(gogo, arg2); |
8532 | arg2_len = at->get_length(gogo, arg2); | |
e440a328 | 8533 | } |
8534 | else | |
8535 | { | |
2c809f8f | 8536 | go_assert(arg2->is_variable()); |
8537 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, | |
8538 | location); | |
8539 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8540 | location); | |
e440a328 | 8541 | } |
2c809f8f | 8542 | Expression* cond = |
8543 | Expression::make_binary(OPERATOR_LT, arg1_len, arg2_len, location); | |
8544 | Expression* length = | |
8545 | Expression::make_conditional(cond, arg1_len, arg2_len, location); | |
e440a328 | 8546 | |
8547 | Type* element_type = at->element_type(); | |
9f0e0513 | 8548 | Btype* element_btype = element_type->get_backend(gogo); |
e440a328 | 8549 | |
2c809f8f | 8550 | mpz_t size; |
8551 | size_t element_size = gogo->backend()->type_size(element_btype); | |
8552 | mpz_init_set_ui(size, element_size); | |
8553 | Expression* size_expr = Expression::make_integer(&size, length->type(), location); | |
8554 | mpz_clear(size); | |
8555 | ||
8556 | Expression* bytecount = | |
8557 | Expression::make_binary(OPERATOR_MULT, size_expr, length, location); | |
8558 | Expression* copy = Runtime::make_call(Runtime::COPY, location, 3, | |
8559 | arg1_val, arg2_val, bytecount); | |
8560 | ||
8561 | Expression* compound = Expression::make_compound(copy, length, location); | |
ea664253 | 8562 | return compound->get_backend(context); |
e440a328 | 8563 | } |
8564 | ||
8565 | case BUILTIN_APPEND: | |
8566 | { | |
8567 | const Expression_list* args = this->args(); | |
c484d925 | 8568 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8569 | Expression* arg1 = args->front(); |
8570 | Expression* arg2 = args->back(); | |
8571 | ||
9d44fbe3 | 8572 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8573 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8574 | |
2c809f8f | 8575 | go_assert(arg2->is_variable()); |
8576 | Expression* arg2_val; | |
8577 | Expression* arg2_len; | |
8578 | mpz_t size; | |
4fd4fcf4 | 8579 | if (arg2->type()->is_string_type() |
60963afd | 8580 | && element_type->integer_type() != NULL |
8581 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8582 | { |
2c809f8f | 8583 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, |
8584 | location); | |
8585 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8586 | location); | |
8587 | mpz_init_set_ui(size, 1UL); | |
4fd4fcf4 | 8588 | } |
8589 | else | |
8590 | { | |
2c809f8f | 8591 | arg2_val = at->get_value_pointer(gogo, arg2); |
8592 | arg2_len = at->get_length(gogo, arg2); | |
35a54f17 | 8593 | Btype* element_btype = element_type->get_backend(gogo); |
2c809f8f | 8594 | size_t element_size = gogo->backend()->type_size(element_btype); |
8595 | mpz_init_set_ui(size, element_size); | |
4fd4fcf4 | 8596 | } |
2c809f8f | 8597 | Expression* element_size = |
8598 | Expression::make_integer(&size, NULL, location); | |
8599 | mpz_clear(size); | |
8600 | ||
8601 | Expression* append = Runtime::make_call(Runtime::APPEND, location, 4, | |
8602 | arg1, arg2_val, arg2_len, | |
8603 | element_size); | |
8604 | append = Expression::make_unsafe_cast(arg1->type(), append, location); | |
ea664253 | 8605 | return append->get_backend(context); |
e440a328 | 8606 | } |
8607 | ||
8608 | case BUILTIN_REAL: | |
8609 | case BUILTIN_IMAG: | |
8610 | { | |
8611 | const Expression_list* args = this->args(); | |
c484d925 | 8612 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8613 | |
8614 | Bexpression* ret; | |
ea664253 | 8615 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 8616 | if (this->code_ == BUILTIN_REAL) |
8617 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
8618 | else | |
8619 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 8620 | return ret; |
e440a328 | 8621 | } |
8622 | ||
48080209 | 8623 | case BUILTIN_COMPLEX: |
e440a328 | 8624 | { |
8625 | const Expression_list* args = this->args(); | |
c484d925 | 8626 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 8627 | Bexpression* breal = args->front()->get_backend(context); |
8628 | Bexpression* bimag = args->back()->get_backend(context); | |
8629 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 8630 | } |
8631 | ||
8632 | default: | |
c3e6f413 | 8633 | go_unreachable(); |
e440a328 | 8634 | } |
8635 | } | |
8636 | ||
8637 | // We have to support exporting a builtin call expression, because | |
8638 | // code can set a constant to the result of a builtin expression. | |
8639 | ||
8640 | void | |
8641 | Builtin_call_expression::do_export(Export* exp) const | |
8642 | { | |
0c77715b | 8643 | Numeric_constant nc; |
8644 | if (!this->numeric_constant_value(&nc)) | |
8645 | { | |
8646 | error_at(this->location(), "value is not constant"); | |
8647 | return; | |
8648 | } | |
e440a328 | 8649 | |
0c77715b | 8650 | if (nc.is_int()) |
e440a328 | 8651 | { |
0c77715b | 8652 | mpz_t val; |
8653 | nc.get_int(&val); | |
e440a328 | 8654 | Integer_expression::export_integer(exp, val); |
0c77715b | 8655 | mpz_clear(val); |
e440a328 | 8656 | } |
0c77715b | 8657 | else if (nc.is_float()) |
e440a328 | 8658 | { |
8659 | mpfr_t fval; | |
0c77715b | 8660 | nc.get_float(&fval); |
8661 | Float_expression::export_float(exp, fval); | |
e440a328 | 8662 | mpfr_clear(fval); |
8663 | } | |
0c77715b | 8664 | else if (nc.is_complex()) |
e440a328 | 8665 | { |
8666 | mpfr_t real; | |
8667 | mpfr_t imag; | |
0c77715b | 8668 | Complex_expression::export_complex(exp, real, imag); |
e440a328 | 8669 | mpfr_clear(real); |
8670 | mpfr_clear(imag); | |
8671 | } | |
0c77715b | 8672 | else |
8673 | go_unreachable(); | |
e440a328 | 8674 | |
8675 | // A trailing space lets us reliably identify the end of the number. | |
8676 | exp->write_c_string(" "); | |
8677 | } | |
8678 | ||
8679 | // Class Call_expression. | |
8680 | ||
8381eda7 | 8681 | // A Go function can be viewed in a couple of different ways. The |
8682 | // code of a Go function becomes a backend function with parameters | |
8683 | // whose types are simply the backend representation of the Go types. | |
8684 | // If there are multiple results, they are returned as a backend | |
8685 | // struct. | |
8686 | ||
8687 | // However, when Go code refers to a function other than simply | |
8688 | // calling it, the backend type of that function is actually a struct. | |
8689 | // The first field of the struct points to the Go function code | |
8690 | // (sometimes a wrapper as described below). The remaining fields | |
8691 | // hold addresses of closed-over variables. This struct is called a | |
8692 | // closure. | |
8693 | ||
8694 | // There are a few cases to consider. | |
8695 | ||
8696 | // A direct function call of a known function in package scope. In | |
8697 | // this case there are no closed-over variables, and we know the name | |
8698 | // of the function code. We can simply produce a backend call to the | |
8699 | // function directly, and not worry about the closure. | |
8700 | ||
8701 | // A direct function call of a known function literal. In this case | |
8702 | // we know the function code and we know the closure. We generate the | |
8703 | // function code such that it expects an additional final argument of | |
8704 | // the closure type. We pass the closure as the last argument, after | |
8705 | // the other arguments. | |
8706 | ||
8707 | // An indirect function call. In this case we have a closure. We | |
8708 | // load the pointer to the function code from the first field of the | |
8709 | // closure. We pass the address of the closure as the last argument. | |
8710 | ||
8711 | // A call to a method of an interface. Type methods are always at | |
8712 | // package scope, so we call the function directly, and don't worry | |
8713 | // about the closure. | |
8714 | ||
8715 | // This means that for a function at package scope we have two cases. | |
8716 | // One is the direct call, which has no closure. The other is the | |
8717 | // indirect call, which does have a closure. We can't simply ignore | |
8718 | // the closure, even though it is the last argument, because that will | |
8719 | // fail on targets where the function pops its arguments. So when | |
8720 | // generating a closure for a package-scope function we set the | |
8721 | // function code pointer in the closure to point to a wrapper | |
8722 | // function. This wrapper function accepts a final argument that | |
8723 | // points to the closure, ignores it, and calls the real function as a | |
8724 | // direct function call. This wrapper will normally be efficient, and | |
8725 | // can often simply be a tail call to the real function. | |
8726 | ||
8727 | // We don't use GCC's static chain pointer because 1) we don't need | |
8728 | // it; 2) GCC only permits using a static chain to call a known | |
8729 | // function, so we can't use it for an indirect call anyhow. Since we | |
8730 | // can't use it for an indirect call, we may as well not worry about | |
8731 | // using it for a direct call either. | |
8732 | ||
8733 | // We pass the closure last rather than first because it means that | |
8734 | // the function wrapper we put into a closure for a package-scope | |
8735 | // function can normally just be a tail call to the real function. | |
8736 | ||
8737 | // For method expressions we generate a wrapper that loads the | |
8738 | // receiver from the closure and then calls the method. This | |
8739 | // unfortunately forces reshuffling the arguments, since there is a | |
8740 | // new first argument, but we can't avoid reshuffling either for | |
8741 | // method expressions or for indirect calls of package-scope | |
8742 | // functions, and since the latter are more common we reshuffle for | |
8743 | // method expressions. | |
8744 | ||
8745 | // Note that the Go code retains the Go types. The extra final | |
8746 | // argument only appears when we convert to the backend | |
8747 | // representation. | |
8748 | ||
e440a328 | 8749 | // Traversal. |
8750 | ||
8751 | int | |
8752 | Call_expression::do_traverse(Traverse* traverse) | |
8753 | { | |
8754 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8755 | return TRAVERSE_EXIT; | |
8756 | if (this->args_ != NULL) | |
8757 | { | |
8758 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8759 | return TRAVERSE_EXIT; | |
8760 | } | |
8761 | return TRAVERSE_CONTINUE; | |
8762 | } | |
8763 | ||
8764 | // Lower a call statement. | |
8765 | ||
8766 | Expression* | |
ceeb4318 | 8767 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
8768 | Statement_inserter* inserter, int) | |
e440a328 | 8769 | { |
b13c66cd | 8770 | Location loc = this->location(); |
09ea332d | 8771 | |
ceeb4318 | 8772 | // A type cast can look like a function call. |
e440a328 | 8773 | if (this->fn_->is_type_expression() |
8774 | && this->args_ != NULL | |
8775 | && this->args_->size() == 1) | |
8776 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 8777 | loc); |
e440a328 | 8778 | |
88f06749 | 8779 | // Because do_type will return an error type and thus prevent future |
8780 | // errors, check for that case now to ensure that the error gets | |
8781 | // reported. | |
37448b10 | 8782 | Function_type* fntype = this->get_function_type(); |
8783 | if (fntype == NULL) | |
88f06749 | 8784 | { |
8785 | if (!this->fn_->type()->is_error()) | |
8786 | this->report_error(_("expected function")); | |
8787 | return Expression::make_error(loc); | |
8788 | } | |
8789 | ||
e440a328 | 8790 | // Handle an argument which is a call to a function which returns |
8791 | // multiple results. | |
8792 | if (this->args_ != NULL | |
8793 | && this->args_->size() == 1 | |
37448b10 | 8794 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 8795 | { |
e440a328 | 8796 | size_t rc = this->args_->front()->call_expression()->result_count(); |
8797 | if (rc > 1 | |
37448b10 | 8798 | && ((fntype->parameters() != NULL |
8799 | && (fntype->parameters()->size() == rc | |
8800 | || (fntype->is_varargs() | |
8801 | && fntype->parameters()->size() - 1 <= rc))) | |
8802 | || fntype->is_builtin())) | |
e440a328 | 8803 | { |
8804 | Call_expression* call = this->args_->front()->call_expression(); | |
8805 | Expression_list* args = new Expression_list; | |
8806 | for (size_t i = 0; i < rc; ++i) | |
8807 | args->push_back(Expression::make_call_result(call, i)); | |
8808 | // We can't return a new call expression here, because this | |
42535814 | 8809 | // one may be referenced by Call_result expressions. We |
8810 | // also can't delete the old arguments, because we may still | |
8811 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8812 | this->args_ = args; |
8813 | } | |
8814 | } | |
8815 | ||
37448b10 | 8816 | // Recognize a call to a builtin function. |
8817 | if (fntype->is_builtin()) | |
8818 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
8819 | this->is_varargs_, loc); | |
8820 | ||
ceeb4318 | 8821 | // If this call returns multiple results, create a temporary |
8822 | // variable for each result. | |
8823 | size_t rc = this->result_count(); | |
8824 | if (rc > 1 && this->results_ == NULL) | |
8825 | { | |
8826 | std::vector<Temporary_statement*>* temps = | |
8827 | new std::vector<Temporary_statement*>; | |
8828 | temps->reserve(rc); | |
37448b10 | 8829 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 8830 | for (Typed_identifier_list::const_iterator p = results->begin(); |
8831 | p != results->end(); | |
8832 | ++p) | |
8833 | { | |
8834 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 8835 | NULL, loc); |
ceeb4318 | 8836 | inserter->insert(temp); |
8837 | temps->push_back(temp); | |
8838 | } | |
8839 | this->results_ = temps; | |
8840 | } | |
8841 | ||
e440a328 | 8842 | // Handle a call to a varargs function by packaging up the extra |
8843 | // parameters. | |
37448b10 | 8844 | if (fntype->is_varargs()) |
e440a328 | 8845 | { |
e440a328 | 8846 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 8847 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 8848 | Type* varargs_type = parameters->back().type(); |
09ea332d | 8849 | this->lower_varargs(gogo, function, inserter, varargs_type, |
8850 | parameters->size()); | |
8851 | } | |
8852 | ||
8853 | // If this is call to a method, call the method directly passing the | |
8854 | // object as the first parameter. | |
8855 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8856 | if (bme != NULL) | |
8857 | { | |
0afbb937 | 8858 | Named_object* methodfn = bme->function(); |
09ea332d | 8859 | Expression* first_arg = bme->first_argument(); |
8860 | ||
8861 | // We always pass a pointer when calling a method. | |
8862 | if (first_arg->type()->points_to() == NULL | |
8863 | && !first_arg->type()->is_error()) | |
8864 | { | |
8865 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
8866 | // We may need to create a temporary variable so that we can | |
8867 | // take the address. We can't do that here because it will | |
8868 | // mess up the order of evaluation. | |
8869 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
8870 | ue->set_create_temp(); | |
8871 | } | |
8872 | ||
8873 | // If we are calling a method which was inherited from an | |
8874 | // embedded struct, and the method did not get a stub, then the | |
8875 | // first type may be wrong. | |
8876 | Type* fatype = bme->first_argument_type(); | |
8877 | if (fatype != NULL) | |
8878 | { | |
8879 | if (fatype->points_to() == NULL) | |
8880 | fatype = Type::make_pointer_type(fatype); | |
8881 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
8882 | } | |
8883 | ||
8884 | Expression_list* new_args = new Expression_list(); | |
8885 | new_args->push_back(first_arg); | |
8886 | if (this->args_ != NULL) | |
8887 | { | |
8888 | for (Expression_list::const_iterator p = this->args_->begin(); | |
8889 | p != this->args_->end(); | |
8890 | ++p) | |
8891 | new_args->push_back(*p); | |
8892 | } | |
8893 | ||
8894 | // We have to change in place because this structure may be | |
8895 | // referenced by Call_result_expressions. We can't delete the | |
8896 | // old arguments, because we may be traversing them up in some | |
8897 | // caller. FIXME. | |
8898 | this->args_ = new_args; | |
0afbb937 | 8899 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 8900 | bme->location()); |
e440a328 | 8901 | } |
8902 | ||
8903 | return this; | |
8904 | } | |
8905 | ||
8906 | // Lower a call to a varargs function. FUNCTION is the function in | |
8907 | // which the call occurs--it's not the function we are calling. | |
8908 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8909 | // PARAM_COUNT is the number of parameters of the function we are | |
8910 | // calling; the last of these parameters will be the varargs | |
8911 | // parameter. | |
8912 | ||
09ea332d | 8913 | void |
e440a328 | 8914 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 8915 | Statement_inserter* inserter, |
e440a328 | 8916 | Type* varargs_type, size_t param_count) |
8917 | { | |
8918 | if (this->varargs_are_lowered_) | |
09ea332d | 8919 | return; |
e440a328 | 8920 | |
b13c66cd | 8921 | Location loc = this->location(); |
e440a328 | 8922 | |
c484d925 | 8923 | go_assert(param_count > 0); |
411eb89e | 8924 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 8925 | |
8926 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8927 | if (arg_count < param_count - 1) | |
8928 | { | |
8929 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 8930 | return; |
e440a328 | 8931 | } |
8932 | ||
8933 | Expression_list* old_args = this->args_; | |
8934 | Expression_list* new_args = new Expression_list(); | |
8935 | bool push_empty_arg = false; | |
8936 | if (old_args == NULL || old_args->empty()) | |
8937 | { | |
c484d925 | 8938 | go_assert(param_count == 1); |
e440a328 | 8939 | push_empty_arg = true; |
8940 | } | |
8941 | else | |
8942 | { | |
8943 | Expression_list::const_iterator pa; | |
8944 | int i = 1; | |
8945 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8946 | { | |
8947 | if (static_cast<size_t>(i) == param_count) | |
8948 | break; | |
8949 | new_args->push_back(*pa); | |
8950 | } | |
8951 | ||
8952 | // We have reached the varargs parameter. | |
8953 | ||
8954 | bool issued_error = false; | |
8955 | if (pa == old_args->end()) | |
8956 | push_empty_arg = true; | |
8957 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8958 | new_args->push_back(*pa); | |
8959 | else if (this->is_varargs_) | |
8960 | { | |
a6645f74 | 8961 | if ((*pa)->type()->is_slice_type()) |
8962 | this->report_error(_("too many arguments")); | |
8963 | else | |
8964 | { | |
8965 | error_at(this->location(), | |
8966 | _("invalid use of %<...%> with non-slice")); | |
8967 | this->set_is_error(); | |
8968 | } | |
09ea332d | 8969 | return; |
e440a328 | 8970 | } |
e440a328 | 8971 | else |
8972 | { | |
8973 | Type* element_type = varargs_type->array_type()->element_type(); | |
8974 | Expression_list* vals = new Expression_list; | |
8975 | for (; pa != old_args->end(); ++pa, ++i) | |
8976 | { | |
8977 | // Check types here so that we get a better message. | |
8978 | Type* patype = (*pa)->type(); | |
b13c66cd | 8979 | Location paloc = (*pa)->location(); |
e440a328 | 8980 | if (!this->check_argument_type(i, element_type, patype, |
8981 | paloc, issued_error)) | |
8982 | continue; | |
8983 | vals->push_back(*pa); | |
8984 | } | |
8985 | Expression* val = | |
8986 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
09ea332d | 8987 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 8988 | new_args->push_back(val); |
8989 | } | |
8990 | } | |
8991 | ||
8992 | if (push_empty_arg) | |
8993 | new_args->push_back(Expression::make_nil(loc)); | |
8994 | ||
8995 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 8996 | // be referenced by Call_result expressions. FIXME. We can't |
8997 | // delete OLD_ARGS because we may have both a Call_expression and a | |
8998 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 8999 | this->args_ = new_args; |
9000 | this->varargs_are_lowered_ = true; | |
e440a328 | 9001 | } |
9002 | ||
2c809f8f | 9003 | // Flatten a call with multiple results into a temporary. |
9004 | ||
9005 | Expression* | |
9006 | Call_expression::do_flatten(Gogo*, Named_object*, Statement_inserter* inserter) | |
9007 | { | |
9008 | size_t rc = this->result_count(); | |
9009 | if (rc > 1 && this->call_temp_ == NULL) | |
9010 | { | |
9011 | Struct_field_list* sfl = new Struct_field_list(); | |
9012 | Function_type* fntype = this->get_function_type(); | |
9013 | const Typed_identifier_list* results = fntype->results(); | |
9014 | Location loc = this->location(); | |
9015 | ||
9016 | int i = 0; | |
9017 | char buf[10]; | |
9018 | for (Typed_identifier_list::const_iterator p = results->begin(); | |
9019 | p != results->end(); | |
9020 | ++p, ++i) | |
9021 | { | |
9022 | snprintf(buf, sizeof buf, "res%d", i); | |
9023 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9024 | } | |
9025 | ||
9026 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9027 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9028 | inserter->insert(this->call_temp_); | |
9029 | } | |
9030 | ||
9031 | return this; | |
9032 | } | |
9033 | ||
ceeb4318 | 9034 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9035 | |
9036 | Function_type* | |
9037 | Call_expression::get_function_type() const | |
9038 | { | |
9039 | return this->fn_->type()->function_type(); | |
9040 | } | |
9041 | ||
9042 | // Return the number of values which this call will return. | |
9043 | ||
9044 | size_t | |
9045 | Call_expression::result_count() const | |
9046 | { | |
9047 | const Function_type* fntype = this->get_function_type(); | |
9048 | if (fntype == NULL) | |
9049 | return 0; | |
9050 | if (fntype->results() == NULL) | |
9051 | return 0; | |
9052 | return fntype->results()->size(); | |
9053 | } | |
9054 | ||
ceeb4318 | 9055 | // Return the temporary which holds a result. |
9056 | ||
9057 | Temporary_statement* | |
9058 | Call_expression::result(size_t i) const | |
9059 | { | |
cd238b8d | 9060 | if (this->results_ == NULL || this->results_->size() <= i) |
9061 | { | |
9062 | go_assert(saw_errors()); | |
9063 | return NULL; | |
9064 | } | |
ceeb4318 | 9065 | return (*this->results_)[i]; |
9066 | } | |
9067 | ||
1373401e | 9068 | // Set the number of results expected from a call expression. |
9069 | ||
9070 | void | |
9071 | Call_expression::set_expected_result_count(size_t count) | |
9072 | { | |
9073 | go_assert(this->expected_result_count_ == 0); | |
9074 | this->expected_result_count_ = count; | |
9075 | } | |
9076 | ||
e440a328 | 9077 | // Return whether this is a call to the predeclared function recover. |
9078 | ||
9079 | bool | |
9080 | Call_expression::is_recover_call() const | |
9081 | { | |
9082 | return this->do_is_recover_call(); | |
9083 | } | |
9084 | ||
9085 | // Set the argument to the recover function. | |
9086 | ||
9087 | void | |
9088 | Call_expression::set_recover_arg(Expression* arg) | |
9089 | { | |
9090 | this->do_set_recover_arg(arg); | |
9091 | } | |
9092 | ||
9093 | // Virtual functions also implemented by Builtin_call_expression. | |
9094 | ||
9095 | bool | |
9096 | Call_expression::do_is_recover_call() const | |
9097 | { | |
9098 | return false; | |
9099 | } | |
9100 | ||
9101 | void | |
9102 | Call_expression::do_set_recover_arg(Expression*) | |
9103 | { | |
c3e6f413 | 9104 | go_unreachable(); |
e440a328 | 9105 | } |
9106 | ||
ceeb4318 | 9107 | // We have found an error with this call expression; return true if |
9108 | // we should report it. | |
9109 | ||
9110 | bool | |
9111 | Call_expression::issue_error() | |
9112 | { | |
9113 | if (this->issued_error_) | |
9114 | return false; | |
9115 | else | |
9116 | { | |
9117 | this->issued_error_ = true; | |
9118 | return true; | |
9119 | } | |
9120 | } | |
9121 | ||
e440a328 | 9122 | // Get the type. |
9123 | ||
9124 | Type* | |
9125 | Call_expression::do_type() | |
9126 | { | |
9127 | if (this->type_ != NULL) | |
9128 | return this->type_; | |
9129 | ||
9130 | Type* ret; | |
9131 | Function_type* fntype = this->get_function_type(); | |
9132 | if (fntype == NULL) | |
9133 | return Type::make_error_type(); | |
9134 | ||
9135 | const Typed_identifier_list* results = fntype->results(); | |
9136 | if (results == NULL) | |
9137 | ret = Type::make_void_type(); | |
9138 | else if (results->size() == 1) | |
9139 | ret = results->begin()->type(); | |
9140 | else | |
9141 | ret = Type::make_call_multiple_result_type(this); | |
9142 | ||
9143 | this->type_ = ret; | |
9144 | ||
9145 | return this->type_; | |
9146 | } | |
9147 | ||
9148 | // Determine types for a call expression. We can use the function | |
9149 | // parameter types to set the types of the arguments. | |
9150 | ||
9151 | void | |
9152 | Call_expression::do_determine_type(const Type_context*) | |
9153 | { | |
fb94b0ca | 9154 | if (!this->determining_types()) |
9155 | return; | |
9156 | ||
e440a328 | 9157 | this->fn_->determine_type_no_context(); |
9158 | Function_type* fntype = this->get_function_type(); | |
9159 | const Typed_identifier_list* parameters = NULL; | |
9160 | if (fntype != NULL) | |
9161 | parameters = fntype->parameters(); | |
9162 | if (this->args_ != NULL) | |
9163 | { | |
9164 | Typed_identifier_list::const_iterator pt; | |
9165 | if (parameters != NULL) | |
9166 | pt = parameters->begin(); | |
09ea332d | 9167 | bool first = true; |
e440a328 | 9168 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9169 | pa != this->args_->end(); | |
9170 | ++pa) | |
9171 | { | |
09ea332d | 9172 | if (first) |
9173 | { | |
9174 | first = false; | |
9175 | // If this is a method, the first argument is the | |
9176 | // receiver. | |
9177 | if (fntype != NULL && fntype->is_method()) | |
9178 | { | |
9179 | Type* rtype = fntype->receiver()->type(); | |
9180 | // The receiver is always passed as a pointer. | |
9181 | if (rtype->points_to() == NULL) | |
9182 | rtype = Type::make_pointer_type(rtype); | |
9183 | Type_context subcontext(rtype, false); | |
9184 | (*pa)->determine_type(&subcontext); | |
9185 | continue; | |
9186 | } | |
9187 | } | |
9188 | ||
e440a328 | 9189 | if (parameters != NULL && pt != parameters->end()) |
9190 | { | |
9191 | Type_context subcontext(pt->type(), false); | |
9192 | (*pa)->determine_type(&subcontext); | |
9193 | ++pt; | |
9194 | } | |
9195 | else | |
9196 | (*pa)->determine_type_no_context(); | |
9197 | } | |
9198 | } | |
9199 | } | |
9200 | ||
fb94b0ca | 9201 | // Called when determining types for a Call_expression. Return true |
9202 | // if we should go ahead, false if they have already been determined. | |
9203 | ||
9204 | bool | |
9205 | Call_expression::determining_types() | |
9206 | { | |
9207 | if (this->types_are_determined_) | |
9208 | return false; | |
9209 | else | |
9210 | { | |
9211 | this->types_are_determined_ = true; | |
9212 | return true; | |
9213 | } | |
9214 | } | |
9215 | ||
e440a328 | 9216 | // Check types for parameter I. |
9217 | ||
9218 | bool | |
9219 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9220 | const Type* argument_type, | |
b13c66cd | 9221 | Location argument_location, |
e440a328 | 9222 | bool issued_error) |
9223 | { | |
9224 | std::string reason; | |
053ee6ca | 9225 | bool ok; |
9226 | if (this->are_hidden_fields_ok_) | |
9227 | ok = Type::are_assignable_hidden_ok(parameter_type, argument_type, | |
9228 | &reason); | |
9229 | else | |
9230 | ok = Type::are_assignable(parameter_type, argument_type, &reason); | |
9231 | if (!ok) | |
e440a328 | 9232 | { |
9233 | if (!issued_error) | |
9234 | { | |
9235 | if (reason.empty()) | |
9236 | error_at(argument_location, "argument %d has incompatible type", i); | |
9237 | else | |
9238 | error_at(argument_location, | |
9239 | "argument %d has incompatible type (%s)", | |
9240 | i, reason.c_str()); | |
9241 | } | |
9242 | this->set_is_error(); | |
9243 | return false; | |
9244 | } | |
9245 | return true; | |
9246 | } | |
9247 | ||
9248 | // Check types. | |
9249 | ||
9250 | void | |
9251 | Call_expression::do_check_types(Gogo*) | |
9252 | { | |
a6645f74 | 9253 | if (this->classification() == EXPRESSION_ERROR) |
9254 | return; | |
9255 | ||
e440a328 | 9256 | Function_type* fntype = this->get_function_type(); |
9257 | if (fntype == NULL) | |
9258 | { | |
5c13bd80 | 9259 | if (!this->fn_->type()->is_error()) |
e440a328 | 9260 | this->report_error(_("expected function")); |
9261 | return; | |
9262 | } | |
9263 | ||
1373401e | 9264 | if (this->expected_result_count_ != 0 |
9265 | && this->expected_result_count_ != this->result_count()) | |
9266 | { | |
9267 | if (this->issue_error()) | |
9268 | this->report_error(_("function result count mismatch")); | |
9269 | this->set_is_error(); | |
9270 | return; | |
9271 | } | |
9272 | ||
09ea332d | 9273 | bool is_method = fntype->is_method(); |
9274 | if (is_method) | |
e440a328 | 9275 | { |
09ea332d | 9276 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9277 | Type* rtype = fntype->receiver()->type(); | |
9278 | Expression* first_arg = this->args_->front(); | |
9279 | // The language permits copying hidden fields for a method | |
9280 | // receiver. We dereference the values since receivers are | |
9281 | // always passed as pointers. | |
9282 | std::string reason; | |
9283 | if (!Type::are_assignable_hidden_ok(rtype->deref(), | |
9284 | first_arg->type()->deref(), | |
9285 | &reason)) | |
e440a328 | 9286 | { |
09ea332d | 9287 | if (reason.empty()) |
9288 | this->report_error(_("incompatible type for receiver")); | |
9289 | else | |
e440a328 | 9290 | { |
09ea332d | 9291 | error_at(this->location(), |
9292 | "incompatible type for receiver (%s)", | |
9293 | reason.c_str()); | |
9294 | this->set_is_error(); | |
e440a328 | 9295 | } |
9296 | } | |
9297 | } | |
9298 | ||
9299 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 9300 | // we don't have to worry about it here unless something is wrong. |
9301 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
9302 | { | |
9303 | if (!fntype->is_varargs()) | |
9304 | { | |
9305 | error_at(this->location(), | |
9306 | _("invalid use of %<...%> calling non-variadic function")); | |
9307 | this->set_is_error(); | |
9308 | return; | |
9309 | } | |
9310 | } | |
e440a328 | 9311 | |
9312 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9313 | if (this->args_ == NULL) | |
9314 | { | |
9315 | if (parameters != NULL && !parameters->empty()) | |
9316 | this->report_error(_("not enough arguments")); | |
9317 | } | |
9318 | else if (parameters == NULL) | |
09ea332d | 9319 | { |
9320 | if (!is_method || this->args_->size() > 1) | |
9321 | this->report_error(_("too many arguments")); | |
9322 | } | |
1373401e | 9323 | else if (this->args_->size() == 1 |
9324 | && this->args_->front()->call_expression() != NULL | |
9325 | && this->args_->front()->call_expression()->result_count() > 1) | |
9326 | { | |
9327 | // This is F(G()) when G returns more than one result. If the | |
9328 | // results can be matched to parameters, it would have been | |
9329 | // lowered in do_lower. If we get here we know there is a | |
9330 | // mismatch. | |
9331 | if (this->args_->front()->call_expression()->result_count() | |
9332 | < parameters->size()) | |
9333 | this->report_error(_("not enough arguments")); | |
9334 | else | |
9335 | this->report_error(_("too many arguments")); | |
9336 | } | |
e440a328 | 9337 | else |
9338 | { | |
9339 | int i = 0; | |
09ea332d | 9340 | Expression_list::const_iterator pa = this->args_->begin(); |
9341 | if (is_method) | |
9342 | ++pa; | |
9343 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9344 | pt != parameters->end(); | |
9345 | ++pt, ++pa, ++i) | |
e440a328 | 9346 | { |
09ea332d | 9347 | if (pa == this->args_->end()) |
e440a328 | 9348 | { |
09ea332d | 9349 | this->report_error(_("not enough arguments")); |
e440a328 | 9350 | return; |
9351 | } | |
9352 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9353 | (*pa)->location(), false); | |
9354 | } | |
09ea332d | 9355 | if (pa != this->args_->end()) |
9356 | this->report_error(_("too many arguments")); | |
e440a328 | 9357 | } |
9358 | } | |
9359 | ||
9360 | // Return whether we have to use a temporary variable to ensure that | |
9361 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9362 | // results then it will inevitably be executed last. |
e440a328 | 9363 | |
9364 | bool | |
9365 | Call_expression::do_must_eval_in_order() const | |
9366 | { | |
ceeb4318 | 9367 | return this->result_count() > 0; |
e440a328 | 9368 | } |
9369 | ||
e440a328 | 9370 | // Get the function and the first argument to use when calling an |
9371 | // interface method. | |
9372 | ||
2387f644 | 9373 | Expression* |
e440a328 | 9374 | Call_expression::interface_method_function( |
e440a328 | 9375 | Interface_field_reference_expression* interface_method, |
2387f644 | 9376 | Expression** first_arg_ptr) |
e440a328 | 9377 | { |
2387f644 | 9378 | *first_arg_ptr = interface_method->get_underlying_object(); |
9379 | return interface_method->get_function(); | |
e440a328 | 9380 | } |
9381 | ||
9382 | // Build the call expression. | |
9383 | ||
ea664253 | 9384 | Bexpression* |
9385 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 9386 | { |
2c809f8f | 9387 | if (this->call_ != NULL) |
ea664253 | 9388 | return this->call_; |
e440a328 | 9389 | |
9390 | Function_type* fntype = this->get_function_type(); | |
9391 | if (fntype == NULL) | |
ea664253 | 9392 | return context->backend()->error_expression(); |
e440a328 | 9393 | |
9394 | if (this->fn_->is_error_expression()) | |
ea664253 | 9395 | return context->backend()->error_expression(); |
e440a328 | 9396 | |
9397 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9398 | Location location = this->location(); |
e440a328 | 9399 | |
9400 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9401 | Interface_field_reference_expression* interface_method = |
9402 | this->fn_->interface_field_reference_expression(); | |
9403 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9404 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9405 | |
f8bdf81a | 9406 | bool has_closure_arg; |
8381eda7 | 9407 | if (has_closure) |
f8bdf81a | 9408 | has_closure_arg = true; |
8381eda7 | 9409 | else if (func != NULL) |
f8bdf81a | 9410 | has_closure_arg = false; |
8381eda7 | 9411 | else if (is_interface_method) |
f8bdf81a | 9412 | has_closure_arg = false; |
8381eda7 | 9413 | else |
f8bdf81a | 9414 | has_closure_arg = true; |
8381eda7 | 9415 | |
e440a328 | 9416 | int nargs; |
2c809f8f | 9417 | std::vector<Bexpression*> fn_args; |
e440a328 | 9418 | if (this->args_ == NULL || this->args_->empty()) |
9419 | { | |
f8bdf81a | 9420 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 9421 | if (nargs > 0) |
9422 | fn_args.resize(1); | |
e440a328 | 9423 | } |
09ea332d | 9424 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9425 | { | |
9426 | // Passing a receiver parameter. | |
9427 | go_assert(!is_interface_method | |
9428 | && fntype->is_method() | |
9429 | && this->args_->size() == 1); | |
f8bdf81a | 9430 | nargs = 1; |
2c809f8f | 9431 | fn_args.resize(1); |
ea664253 | 9432 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 9433 | } |
e440a328 | 9434 | else |
9435 | { | |
9436 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9437 | |
9438 | nargs = this->args_->size(); | |
09ea332d | 9439 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9440 | nargs += i; |
2c809f8f | 9441 | fn_args.resize(nargs); |
e440a328 | 9442 | |
9443 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9444 | Expression_list::const_iterator pe = this->args_->begin(); |
9445 | if (!is_interface_method && fntype->is_method()) | |
9446 | { | |
ea664253 | 9447 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 9448 | ++pe; |
9449 | ++i; | |
9450 | } | |
9451 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9452 | { |
c484d925 | 9453 | go_assert(pp != params->end()); |
2c809f8f | 9454 | Expression* arg = |
9455 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
9456 | location); | |
ea664253 | 9457 | fn_args[i] = arg->get_backend(context); |
e440a328 | 9458 | } |
c484d925 | 9459 | go_assert(pp == params->end()); |
f8bdf81a | 9460 | go_assert(i == nargs); |
e440a328 | 9461 | } |
9462 | ||
2c809f8f | 9463 | Expression* fn; |
9464 | Expression* closure = NULL; | |
8381eda7 | 9465 | if (func != NULL) |
9466 | { | |
9467 | Named_object* no = func->named_object(); | |
2c809f8f | 9468 | fn = Expression::make_func_code_reference(no, location); |
9469 | if (has_closure) | |
9470 | closure = func->closure(); | |
8381eda7 | 9471 | } |
09ea332d | 9472 | else if (!is_interface_method) |
8381eda7 | 9473 | { |
2c809f8f | 9474 | closure = this->fn_; |
9475 | ||
9476 | // The backend representation of this function type is a pointer | |
9477 | // to a struct whose first field is the actual function to call. | |
9478 | Type* pfntype = | |
9479 | Type::make_pointer_type( | |
9480 | Type::make_pointer_type(Type::make_void_type())); | |
9481 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
9482 | fn = Expression::make_unary(OPERATOR_MULT, fn, location); | |
9483 | } | |
e440a328 | 9484 | else |
cf609de4 | 9485 | { |
2387f644 | 9486 | Expression* first_arg; |
2c809f8f | 9487 | fn = this->interface_method_function(interface_method, &first_arg); |
ea664253 | 9488 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 9489 | } |
9490 | ||
f8bdf81a | 9491 | if (!has_closure_arg) |
2c809f8f | 9492 | go_assert(closure == NULL); |
f8bdf81a | 9493 | else |
9494 | { | |
9495 | // Pass the closure argument by calling the function function | |
9496 | // __go_set_closure. In the order_evaluations pass we have | |
9497 | // ensured that if any parameters contain call expressions, they | |
9498 | // will have been moved out to temporary variables. | |
2c809f8f | 9499 | go_assert(closure != NULL); |
9500 | Expression* set_closure = | |
9501 | Runtime::make_call(Runtime::SET_CLOSURE, location, 1, closure); | |
9502 | fn = Expression::make_compound(set_closure, fn, location); | |
f8bdf81a | 9503 | } |
9504 | ||
ea664253 | 9505 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 9506 | |
9507 | // When not calling a named function directly, use a type conversion | |
9508 | // in case the type of the function is a recursive type which refers | |
9509 | // to itself. We don't do this for an interface method because 1) | |
9510 | // an interface method never refers to itself, so we always have a | |
9511 | // function type here; 2) we pass an extra first argument to an | |
9512 | // interface method, so fntype is not correct. | |
9513 | if (func == NULL && !is_interface_method) | |
9514 | { | |
9515 | Btype* bft = fntype->get_backend_fntype(gogo); | |
9516 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
9517 | } | |
9518 | ||
2c809f8f | 9519 | Bexpression* call = gogo->backend()->call_expression(bfn, fn_args, location); |
e440a328 | 9520 | |
2c809f8f | 9521 | if (this->results_ != NULL) |
e440a328 | 9522 | { |
2c809f8f | 9523 | go_assert(this->call_temp_ != NULL); |
9524 | Expression* call_ref = | |
9525 | Expression::make_temporary_reference(this->call_temp_, location); | |
ea664253 | 9526 | Bexpression* bcall_ref = call_ref->get_backend(context); |
2c809f8f | 9527 | Bstatement* assn_stmt = |
9528 | gogo->backend()->assignment_statement(bcall_ref, call, location); | |
e440a328 | 9529 | |
2c809f8f | 9530 | this->call_ = this->set_results(context, bcall_ref); |
e440a328 | 9531 | |
2c809f8f | 9532 | Bexpression* set_and_call = |
9533 | gogo->backend()->compound_expression(assn_stmt, this->call_, | |
9534 | location); | |
ea664253 | 9535 | return set_and_call; |
2c809f8f | 9536 | } |
e440a328 | 9537 | |
2c809f8f | 9538 | this->call_ = call; |
ea664253 | 9539 | return this->call_; |
e440a328 | 9540 | } |
9541 | ||
ceeb4318 | 9542 | // Set the result variables if this call returns multiple results. |
9543 | ||
2c809f8f | 9544 | Bexpression* |
9545 | Call_expression::set_results(Translate_context* context, Bexpression* call) | |
ceeb4318 | 9546 | { |
2c809f8f | 9547 | Gogo* gogo = context->gogo(); |
ceeb4318 | 9548 | |
2c809f8f | 9549 | Bexpression* results = NULL; |
b13c66cd | 9550 | Location loc = this->location(); |
2c809f8f | 9551 | |
ceeb4318 | 9552 | size_t rc = this->result_count(); |
2c809f8f | 9553 | for (size_t i = 0; i < rc; ++i) |
ceeb4318 | 9554 | { |
ceeb4318 | 9555 | Temporary_statement* temp = this->result(i); |
cd238b8d | 9556 | if (temp == NULL) |
9557 | { | |
9558 | go_assert(saw_errors()); | |
2c809f8f | 9559 | return gogo->backend()->error_expression(); |
cd238b8d | 9560 | } |
ceeb4318 | 9561 | Temporary_reference_expression* ref = |
9562 | Expression::make_temporary_reference(temp, loc); | |
9563 | ref->set_is_lvalue(); | |
ceeb4318 | 9564 | |
ea664253 | 9565 | Bexpression* result_ref = ref->get_backend(context); |
2c809f8f | 9566 | Bexpression* call_result = |
9567 | gogo->backend()->struct_field_expression(call, i, loc); | |
9568 | Bstatement* assn_stmt = | |
9569 | gogo->backend()->assignment_statement(result_ref, call_result, loc); | |
ceeb4318 | 9570 | |
2c809f8f | 9571 | Bexpression* result = |
9572 | gogo->backend()->compound_expression(assn_stmt, call_result, loc); | |
ceeb4318 | 9573 | |
2c809f8f | 9574 | if (results == NULL) |
9575 | results = result; | |
9576 | else | |
9577 | { | |
9578 | Bstatement* expr_stmt = gogo->backend()->expression_statement(result); | |
9579 | results = | |
9580 | gogo->backend()->compound_expression(expr_stmt, results, loc); | |
9581 | } | |
9582 | } | |
9583 | return results; | |
ceeb4318 | 9584 | } |
9585 | ||
d751bb78 | 9586 | // Dump ast representation for a call expressin. |
9587 | ||
9588 | void | |
9589 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9590 | { | |
9591 | this->fn_->dump_expression(ast_dump_context); | |
9592 | ast_dump_context->ostream() << "("; | |
9593 | if (args_ != NULL) | |
9594 | ast_dump_context->dump_expression_list(this->args_); | |
9595 | ||
9596 | ast_dump_context->ostream() << ") "; | |
9597 | } | |
9598 | ||
e440a328 | 9599 | // Make a call expression. |
9600 | ||
9601 | Call_expression* | |
9602 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9603 | Location location) |
e440a328 | 9604 | { |
9605 | return new Call_expression(fn, args, is_varargs, location); | |
9606 | } | |
9607 | ||
9608 | // A single result from a call which returns multiple results. | |
9609 | ||
9610 | class Call_result_expression : public Expression | |
9611 | { | |
9612 | public: | |
9613 | Call_result_expression(Call_expression* call, unsigned int index) | |
9614 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
9615 | call_(call), index_(index) | |
9616 | { } | |
9617 | ||
9618 | protected: | |
9619 | int | |
9620 | do_traverse(Traverse*); | |
9621 | ||
9622 | Type* | |
9623 | do_type(); | |
9624 | ||
9625 | void | |
9626 | do_determine_type(const Type_context*); | |
9627 | ||
9628 | void | |
9629 | do_check_types(Gogo*); | |
9630 | ||
9631 | Expression* | |
9632 | do_copy() | |
9633 | { | |
9634 | return new Call_result_expression(this->call_->call_expression(), | |
9635 | this->index_); | |
9636 | } | |
9637 | ||
9638 | bool | |
9639 | do_must_eval_in_order() const | |
9640 | { return true; } | |
9641 | ||
ea664253 | 9642 | Bexpression* |
9643 | do_get_backend(Translate_context*); | |
e440a328 | 9644 | |
d751bb78 | 9645 | void |
9646 | do_dump_expression(Ast_dump_context*) const; | |
9647 | ||
e440a328 | 9648 | private: |
9649 | // The underlying call expression. | |
9650 | Expression* call_; | |
9651 | // Which result we want. | |
9652 | unsigned int index_; | |
9653 | }; | |
9654 | ||
9655 | // Traverse a call result. | |
9656 | ||
9657 | int | |
9658 | Call_result_expression::do_traverse(Traverse* traverse) | |
9659 | { | |
9660 | if (traverse->remember_expression(this->call_)) | |
9661 | { | |
9662 | // We have already traversed the call expression. | |
9663 | return TRAVERSE_CONTINUE; | |
9664 | } | |
9665 | return Expression::traverse(&this->call_, traverse); | |
9666 | } | |
9667 | ||
9668 | // Get the type. | |
9669 | ||
9670 | Type* | |
9671 | Call_result_expression::do_type() | |
9672 | { | |
425dd051 | 9673 | if (this->classification() == EXPRESSION_ERROR) |
9674 | return Type::make_error_type(); | |
9675 | ||
e440a328 | 9676 | // THIS->CALL_ can be replaced with a temporary reference due to |
9677 | // Call_expression::do_must_eval_in_order when there is an error. | |
9678 | Call_expression* ce = this->call_->call_expression(); | |
9679 | if (ce == NULL) | |
5e85f268 | 9680 | { |
9681 | this->set_is_error(); | |
9682 | return Type::make_error_type(); | |
9683 | } | |
e440a328 | 9684 | Function_type* fntype = ce->get_function_type(); |
9685 | if (fntype == NULL) | |
5e85f268 | 9686 | { |
e37658e2 | 9687 | if (ce->issue_error()) |
99b3f06f | 9688 | { |
9689 | if (!ce->fn()->type()->is_error()) | |
9690 | this->report_error(_("expected function")); | |
9691 | } | |
5e85f268 | 9692 | this->set_is_error(); |
9693 | return Type::make_error_type(); | |
9694 | } | |
e440a328 | 9695 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9696 | if (results == NULL || results->size() < 2) |
7b8d861f | 9697 | { |
ceeb4318 | 9698 | if (ce->issue_error()) |
9699 | this->report_error(_("number of results does not match " | |
9700 | "number of values")); | |
7b8d861f | 9701 | return Type::make_error_type(); |
9702 | } | |
e440a328 | 9703 | Typed_identifier_list::const_iterator pr = results->begin(); |
9704 | for (unsigned int i = 0; i < this->index_; ++i) | |
9705 | { | |
9706 | if (pr == results->end()) | |
425dd051 | 9707 | break; |
e440a328 | 9708 | ++pr; |
9709 | } | |
9710 | if (pr == results->end()) | |
425dd051 | 9711 | { |
ceeb4318 | 9712 | if (ce->issue_error()) |
9713 | this->report_error(_("number of results does not match " | |
9714 | "number of values")); | |
425dd051 | 9715 | return Type::make_error_type(); |
9716 | } | |
e440a328 | 9717 | return pr->type(); |
9718 | } | |
9719 | ||
425dd051 | 9720 | // Check the type. Just make sure that we trigger the warning in |
9721 | // do_type. | |
e440a328 | 9722 | |
9723 | void | |
9724 | Call_result_expression::do_check_types(Gogo*) | |
9725 | { | |
425dd051 | 9726 | this->type(); |
e440a328 | 9727 | } |
9728 | ||
9729 | // Determine the type. We have nothing to do here, but the 0 result | |
9730 | // needs to pass down to the caller. | |
9731 | ||
9732 | void | |
9733 | Call_result_expression::do_determine_type(const Type_context*) | |
9734 | { | |
fb94b0ca | 9735 | this->call_->determine_type_no_context(); |
e440a328 | 9736 | } |
9737 | ||
ea664253 | 9738 | // Return the backend representation. We just refer to the temporary set by the |
9739 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 9740 | // hard to evaluate the call at the right time. |
e440a328 | 9741 | |
ea664253 | 9742 | Bexpression* |
9743 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 9744 | { |
ceeb4318 | 9745 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 9746 | if (ce == NULL) |
9747 | { | |
9748 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 9749 | return context->backend()->error_expression(); |
cd238b8d | 9750 | } |
ceeb4318 | 9751 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 9752 | if (ts == NULL) |
9753 | { | |
9754 | go_assert(saw_errors()); | |
ea664253 | 9755 | return context->backend()->error_expression(); |
cd238b8d | 9756 | } |
ceeb4318 | 9757 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
ea664253 | 9758 | return ref->get_backend(context); |
e440a328 | 9759 | } |
9760 | ||
d751bb78 | 9761 | // Dump ast representation for a call result expression. |
9762 | ||
9763 | void | |
9764 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9765 | const | |
9766 | { | |
9767 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
9768 | // (struct) and the fields are referenced instead. | |
9769 | ast_dump_context->ostream() << this->index_ << "@("; | |
9770 | ast_dump_context->dump_expression(this->call_); | |
9771 | ast_dump_context->ostream() << ")"; | |
9772 | } | |
9773 | ||
e440a328 | 9774 | // Make a reference to a single result of a call which returns |
9775 | // multiple results. | |
9776 | ||
9777 | Expression* | |
9778 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9779 | { | |
9780 | return new Call_result_expression(call, index); | |
9781 | } | |
9782 | ||
9783 | // Class Index_expression. | |
9784 | ||
9785 | // Traversal. | |
9786 | ||
9787 | int | |
9788 | Index_expression::do_traverse(Traverse* traverse) | |
9789 | { | |
9790 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9791 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9792 | || (this->end_ != NULL | |
acf2b673 | 9793 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
9794 | || (this->cap_ != NULL | |
9795 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 9796 | return TRAVERSE_EXIT; |
9797 | return TRAVERSE_CONTINUE; | |
9798 | } | |
9799 | ||
9800 | // Lower an index expression. This converts the generic index | |
9801 | // expression into an array index, a string index, or a map index. | |
9802 | ||
9803 | Expression* | |
ceeb4318 | 9804 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 9805 | { |
b13c66cd | 9806 | Location location = this->location(); |
e440a328 | 9807 | Expression* left = this->left_; |
9808 | Expression* start = this->start_; | |
9809 | Expression* end = this->end_; | |
acf2b673 | 9810 | Expression* cap = this->cap_; |
e440a328 | 9811 | |
9812 | Type* type = left->type(); | |
5c13bd80 | 9813 | if (type->is_error()) |
e440a328 | 9814 | return Expression::make_error(location); |
b0cf7ddd | 9815 | else if (left->is_type_expression()) |
9816 | { | |
9817 | error_at(location, "attempt to index type expression"); | |
9818 | return Expression::make_error(location); | |
9819 | } | |
e440a328 | 9820 | else if (type->array_type() != NULL) |
acf2b673 | 9821 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 9822 | else if (type->points_to() != NULL |
9823 | && type->points_to()->array_type() != NULL | |
411eb89e | 9824 | && !type->points_to()->is_slice_type()) |
e440a328 | 9825 | { |
9826 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9827 | location); | |
38092374 | 9828 | |
9829 | // For an ordinary index into the array, the pointer will be | |
9830 | // dereferenced. For a slice it will not--the resulting slice | |
9831 | // will simply reuse the pointer, which is incorrect if that | |
9832 | // pointer is nil. | |
9833 | if (end != NULL || cap != NULL) | |
9834 | deref->issue_nil_check(); | |
9835 | ||
acf2b673 | 9836 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 9837 | } |
9838 | else if (type->is_string_type()) | |
acf2b673 | 9839 | { |
9840 | if (cap != NULL) | |
9841 | { | |
9842 | error_at(location, "invalid 3-index slice of string"); | |
9843 | return Expression::make_error(location); | |
9844 | } | |
9845 | return Expression::make_string_index(left, start, end, location); | |
9846 | } | |
e440a328 | 9847 | else if (type->map_type() != NULL) |
9848 | { | |
acf2b673 | 9849 | if (end != NULL || cap != NULL) |
e440a328 | 9850 | { |
9851 | error_at(location, "invalid slice of map"); | |
9852 | return Expression::make_error(location); | |
9853 | } | |
6d4c2432 | 9854 | Map_index_expression* ret = Expression::make_map_index(left, start, |
9855 | location); | |
e440a328 | 9856 | if (this->is_lvalue_) |
9857 | ret->set_is_lvalue(); | |
9858 | return ret; | |
9859 | } | |
9860 | else | |
9861 | { | |
9862 | error_at(location, | |
9863 | "attempt to index object which is not array, string, or map"); | |
9864 | return Expression::make_error(location); | |
9865 | } | |
9866 | } | |
9867 | ||
acf2b673 | 9868 | // Write an indexed expression |
9869 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 9870 | |
9871 | void | |
9872 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
9873 | const Expression* expr, | |
9874 | const Expression* start, | |
acf2b673 | 9875 | const Expression* end, |
9876 | const Expression* cap) | |
d751bb78 | 9877 | { |
9878 | expr->dump_expression(ast_dump_context); | |
9879 | ast_dump_context->ostream() << "["; | |
9880 | start->dump_expression(ast_dump_context); | |
9881 | if (end != NULL) | |
9882 | { | |
9883 | ast_dump_context->ostream() << ":"; | |
9884 | end->dump_expression(ast_dump_context); | |
9885 | } | |
acf2b673 | 9886 | if (cap != NULL) |
9887 | { | |
9888 | ast_dump_context->ostream() << ":"; | |
9889 | cap->dump_expression(ast_dump_context); | |
9890 | } | |
d751bb78 | 9891 | ast_dump_context->ostream() << "]"; |
9892 | } | |
9893 | ||
9894 | // Dump ast representation for an index expression. | |
9895 | ||
9896 | void | |
9897 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9898 | const | |
9899 | { | |
9900 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 9901 | this->start_, this->end_, this->cap_); |
d751bb78 | 9902 | } |
9903 | ||
e440a328 | 9904 | // Make an index expression. |
9905 | ||
9906 | Expression* | |
9907 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 9908 | Expression* cap, Location location) |
e440a328 | 9909 | { |
acf2b673 | 9910 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 9911 | } |
9912 | ||
9913 | // An array index. This is used for both indexing and slicing. | |
9914 | ||
9915 | class Array_index_expression : public Expression | |
9916 | { | |
9917 | public: | |
9918 | Array_index_expression(Expression* array, Expression* start, | |
acf2b673 | 9919 | Expression* end, Expression* cap, Location location) |
e440a328 | 9920 | : Expression(EXPRESSION_ARRAY_INDEX, location), |
acf2b673 | 9921 | array_(array), start_(start), end_(end), cap_(cap), type_(NULL) |
e440a328 | 9922 | { } |
9923 | ||
9924 | protected: | |
9925 | int | |
9926 | do_traverse(Traverse*); | |
9927 | ||
2c809f8f | 9928 | Expression* |
9929 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
9930 | ||
e440a328 | 9931 | Type* |
9932 | do_type(); | |
9933 | ||
9934 | void | |
9935 | do_determine_type(const Type_context*); | |
9936 | ||
9937 | void | |
9938 | do_check_types(Gogo*); | |
9939 | ||
9940 | Expression* | |
9941 | do_copy() | |
9942 | { | |
9943 | return Expression::make_array_index(this->array_->copy(), | |
9944 | this->start_->copy(), | |
9945 | (this->end_ == NULL | |
9946 | ? NULL | |
9947 | : this->end_->copy()), | |
acf2b673 | 9948 | (this->cap_ == NULL |
9949 | ? NULL | |
9950 | : this->cap_->copy()), | |
e440a328 | 9951 | this->location()); |
9952 | } | |
9953 | ||
baef9f7a | 9954 | bool |
9955 | do_must_eval_subexpressions_in_order(int* skip) const | |
9956 | { | |
9957 | *skip = 1; | |
9958 | return true; | |
9959 | } | |
9960 | ||
e440a328 | 9961 | bool |
9962 | do_is_addressable() const; | |
9963 | ||
9964 | void | |
9965 | do_address_taken(bool escapes) | |
9966 | { this->array_->address_taken(escapes); } | |
9967 | ||
56080003 | 9968 | void |
9969 | do_issue_nil_check() | |
9970 | { this->array_->issue_nil_check(); } | |
9971 | ||
ea664253 | 9972 | Bexpression* |
9973 | do_get_backend(Translate_context*); | |
e440a328 | 9974 | |
d751bb78 | 9975 | void |
9976 | do_dump_expression(Ast_dump_context*) const; | |
9977 | ||
e440a328 | 9978 | private: |
9979 | // The array we are getting a value from. | |
9980 | Expression* array_; | |
9981 | // The start or only index. | |
9982 | Expression* start_; | |
9983 | // The end index of a slice. This may be NULL for a simple array | |
9984 | // index, or it may be a nil expression for the length of the array. | |
9985 | Expression* end_; | |
acf2b673 | 9986 | // The capacity argument of a slice. This may be NULL for an array index or |
9987 | // slice. | |
9988 | Expression* cap_; | |
e440a328 | 9989 | // The type of the expression. |
9990 | Type* type_; | |
9991 | }; | |
9992 | ||
9993 | // Array index traversal. | |
9994 | ||
9995 | int | |
9996 | Array_index_expression::do_traverse(Traverse* traverse) | |
9997 | { | |
9998 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9999 | return TRAVERSE_EXIT; | |
10000 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10001 | return TRAVERSE_EXIT; | |
10002 | if (this->end_ != NULL) | |
10003 | { | |
10004 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10005 | return TRAVERSE_EXIT; | |
10006 | } | |
acf2b673 | 10007 | if (this->cap_ != NULL) |
10008 | { | |
10009 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10010 | return TRAVERSE_EXIT; | |
10011 | } | |
e440a328 | 10012 | return TRAVERSE_CONTINUE; |
10013 | } | |
10014 | ||
10015 | // Return the type of an array index. | |
10016 | ||
10017 | Type* | |
10018 | Array_index_expression::do_type() | |
10019 | { | |
10020 | if (this->type_ == NULL) | |
10021 | { | |
10022 | Array_type* type = this->array_->type()->array_type(); | |
10023 | if (type == NULL) | |
10024 | this->type_ = Type::make_error_type(); | |
10025 | else if (this->end_ == NULL) | |
10026 | this->type_ = type->element_type(); | |
411eb89e | 10027 | else if (type->is_slice_type()) |
e440a328 | 10028 | { |
10029 | // A slice of a slice has the same type as the original | |
10030 | // slice. | |
10031 | this->type_ = this->array_->type()->deref(); | |
10032 | } | |
10033 | else | |
10034 | { | |
10035 | // A slice of an array is a slice. | |
10036 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10037 | } | |
10038 | } | |
10039 | return this->type_; | |
10040 | } | |
10041 | ||
10042 | // Set the type of an array index. | |
10043 | ||
10044 | void | |
10045 | Array_index_expression::do_determine_type(const Type_context*) | |
10046 | { | |
10047 | this->array_->determine_type_no_context(); | |
7917ad68 | 10048 | this->start_->determine_type_no_context(); |
e440a328 | 10049 | if (this->end_ != NULL) |
7917ad68 | 10050 | this->end_->determine_type_no_context(); |
acf2b673 | 10051 | if (this->cap_ != NULL) |
10052 | this->cap_->determine_type_no_context(); | |
e440a328 | 10053 | } |
10054 | ||
10055 | // Check types of an array index. | |
10056 | ||
10057 | void | |
10058 | Array_index_expression::do_check_types(Gogo*) | |
10059 | { | |
f6bc81e6 | 10060 | Numeric_constant nc; |
10061 | unsigned long v; | |
10062 | if (this->start_->type()->integer_type() == NULL | |
10063 | && !this->start_->type()->is_error() | |
10064 | && (!this->start_->numeric_constant_value(&nc) | |
10065 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10066 | this->report_error(_("index must be integer")); |
10067 | if (this->end_ != NULL | |
10068 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10069 | && !this->end_->type()->is_error() |
10070 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10071 | && !this->end_->is_error_expression() |
10072 | && (!this->end_->numeric_constant_value(&nc) | |
10073 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10074 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10075 | if (this->cap_ != NULL |
10076 | && this->cap_->type()->integer_type() == NULL | |
10077 | && !this->cap_->type()->is_error() | |
10078 | && !this->cap_->is_nil_expression() | |
10079 | && !this->cap_->is_error_expression() | |
10080 | && (!this->cap_->numeric_constant_value(&nc) | |
10081 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10082 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10083 | |
10084 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10085 | if (array_type == NULL) |
10086 | { | |
c484d925 | 10087 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10088 | return; |
10089 | } | |
e440a328 | 10090 | |
10091 | unsigned int int_bits = | |
10092 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10093 | ||
0c77715b | 10094 | Numeric_constant lvalnc; |
e440a328 | 10095 | mpz_t lval; |
e440a328 | 10096 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10097 | && array_type->length()->numeric_constant_value(&lvalnc) |
10098 | && lvalnc.to_int(&lval)); | |
10099 | Numeric_constant inc; | |
e440a328 | 10100 | mpz_t ival; |
0bd5d859 | 10101 | bool ival_valid = false; |
0c77715b | 10102 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10103 | { |
0bd5d859 | 10104 | ival_valid = true; |
e440a328 | 10105 | if (mpz_sgn(ival) < 0 |
10106 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10107 | || (lval_valid | |
10108 | && (this->end_ == NULL | |
10109 | ? mpz_cmp(ival, lval) >= 0 | |
10110 | : mpz_cmp(ival, lval) > 0))) | |
10111 | { | |
10112 | error_at(this->start_->location(), "array index out of bounds"); | |
10113 | this->set_is_error(); | |
10114 | } | |
10115 | } | |
10116 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10117 | { | |
0c77715b | 10118 | Numeric_constant enc; |
10119 | mpz_t eval; | |
acf2b673 | 10120 | bool eval_valid = false; |
0c77715b | 10121 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10122 | { |
acf2b673 | 10123 | eval_valid = true; |
0c77715b | 10124 | if (mpz_sgn(eval) < 0 |
10125 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10126 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10127 | { |
10128 | error_at(this->end_->location(), "array index out of bounds"); | |
10129 | this->set_is_error(); | |
10130 | } | |
0bd5d859 | 10131 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10132 | this->report_error(_("inverted slice range")); | |
e440a328 | 10133 | } |
acf2b673 | 10134 | |
10135 | Numeric_constant cnc; | |
10136 | mpz_t cval; | |
10137 | if (this->cap_ != NULL | |
10138 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10139 | { | |
10140 | if (mpz_sgn(cval) < 0 | |
10141 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10142 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10143 | { | |
10144 | error_at(this->cap_->location(), "array index out of bounds"); | |
10145 | this->set_is_error(); | |
10146 | } | |
10147 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10148 | { | |
10149 | error_at(this->cap_->location(), | |
10150 | "invalid slice index: capacity less than start"); | |
10151 | this->set_is_error(); | |
10152 | } | |
10153 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10154 | { | |
10155 | error_at(this->cap_->location(), | |
10156 | "invalid slice index: capacity less than length"); | |
10157 | this->set_is_error(); | |
10158 | } | |
10159 | mpz_clear(cval); | |
10160 | } | |
10161 | ||
10162 | if (eval_valid) | |
10163 | mpz_clear(eval); | |
e440a328 | 10164 | } |
0bd5d859 | 10165 | if (ival_valid) |
10166 | mpz_clear(ival); | |
0c77715b | 10167 | if (lval_valid) |
10168 | mpz_clear(lval); | |
e440a328 | 10169 | |
10170 | // A slice of an array requires an addressable array. A slice of a | |
10171 | // slice is always possible. | |
411eb89e | 10172 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10173 | { |
10174 | if (!this->array_->is_addressable()) | |
8da39c3b | 10175 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10176 | else |
10177 | this->array_->address_taken(true); | |
10178 | } | |
e440a328 | 10179 | } |
10180 | ||
2c809f8f | 10181 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10182 | |
10183 | Expression* | |
10184 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10185 | Statement_inserter* inserter) | |
10186 | { | |
10187 | Location loc = this->location(); | |
2c809f8f | 10188 | Temporary_statement* temp; |
35a54f17 | 10189 | if (this->array_->type()->is_slice_type() && !this->array_->is_variable()) |
10190 | { | |
2c809f8f | 10191 | temp = Statement::make_temporary(NULL, this->array_, loc); |
35a54f17 | 10192 | inserter->insert(temp); |
10193 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10194 | } | |
2c809f8f | 10195 | if (!this->start_->is_variable()) |
10196 | { | |
10197 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
10198 | inserter->insert(temp); | |
10199 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10200 | } | |
10201 | if (this->end_ != NULL | |
10202 | && !this->end_->is_nil_expression() | |
10203 | && !this->end_->is_variable()) | |
10204 | { | |
10205 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
10206 | inserter->insert(temp); | |
10207 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10208 | } | |
10209 | if (this->cap_ != NULL && !this->cap_->is_variable()) | |
10210 | { | |
10211 | temp = Statement::make_temporary(NULL, this->cap_, loc); | |
10212 | inserter->insert(temp); | |
10213 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10214 | } | |
10215 | ||
35a54f17 | 10216 | return this; |
10217 | } | |
10218 | ||
e440a328 | 10219 | // Return whether this expression is addressable. |
10220 | ||
10221 | bool | |
10222 | Array_index_expression::do_is_addressable() const | |
10223 | { | |
10224 | // A slice expression is not addressable. | |
10225 | if (this->end_ != NULL) | |
10226 | return false; | |
10227 | ||
10228 | // An index into a slice is addressable. | |
411eb89e | 10229 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10230 | return true; |
10231 | ||
10232 | // An index into an array is addressable if the array is | |
10233 | // addressable. | |
10234 | return this->array_->is_addressable(); | |
10235 | } | |
10236 | ||
ea664253 | 10237 | // Get the backend representation for an array index. |
e440a328 | 10238 | |
ea664253 | 10239 | Bexpression* |
10240 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10241 | { |
e440a328 | 10242 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10243 | if (array_type == NULL) |
10244 | { | |
c484d925 | 10245 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10246 | return context->backend()->error_expression(); |
d8cd8e2d | 10247 | } |
35a54f17 | 10248 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10249 | |
2c809f8f | 10250 | Location loc = this->location(); |
10251 | Gogo* gogo = context->gogo(); | |
10252 | ||
6dfedc16 | 10253 | Type* int_type = Type::lookup_integer_type("int"); |
10254 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10255 | |
2c809f8f | 10256 | // We need to convert the length and capacity to the Go "int" type here |
10257 | // because the length of a fixed-length array could be of type "uintptr" | |
10258 | // and gimple disallows binary operations between "uintptr" and other | |
10259 | // integer types. FIXME. | |
10260 | Bexpression* length = NULL; | |
a04bfdfc | 10261 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10262 | { | |
35a54f17 | 10263 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10264 | length = len->get_backend(context); |
2c809f8f | 10265 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10266 | } |
10267 | ||
2c809f8f | 10268 | Bexpression* capacity = NULL; |
a04bfdfc | 10269 | if (this->end_ != NULL) |
10270 | { | |
35a54f17 | 10271 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10272 | capacity = cap->get_backend(context); |
2c809f8f | 10273 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10274 | } |
10275 | ||
2c809f8f | 10276 | Bexpression* cap_arg = capacity; |
acf2b673 | 10277 | if (this->cap_ != NULL) |
10278 | { | |
ea664253 | 10279 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10280 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10281 | } |
10282 | ||
2c809f8f | 10283 | if (length == NULL) |
10284 | length = cap_arg; | |
e440a328 | 10285 | |
10286 | int code = (array_type->length() != NULL | |
10287 | ? (this->end_ == NULL | |
10288 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10289 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10290 | : (this->end_ == NULL | |
10291 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10292 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10293 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10294 | |
6dfedc16 | 10295 | if (this->start_->type()->integer_type() == NULL |
10296 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10297 | { | |
10298 | go_assert(saw_errors()); | |
10299 | return context->backend()->error_expression(); | |
10300 | } | |
10301 | Expression* start_expr = Expression::make_cast(int_type, this->start_, loc); | |
ea664253 | 10302 | Bexpression* bad_index = |
6dfedc16 | 10303 | Expression::check_bounds(start_expr, loc)->get_backend(context); |
2c809f8f | 10304 | |
ea664253 | 10305 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10306 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10307 | Bexpression* start_too_large = | |
10308 | gogo->backend()->binary_expression((this->end_ == NULL | |
10309 | ? OPERATOR_GE | |
10310 | : OPERATOR_GT), | |
10311 | start, | |
10312 | (this->end_ == NULL | |
10313 | ? length | |
10314 | : capacity), | |
10315 | loc); | |
10316 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10317 | bad_index, loc); | |
e440a328 | 10318 | |
10319 | if (this->end_ == NULL) | |
10320 | { | |
10321 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10322 | // wrap the index check into START. |
10323 | start = | |
10324 | gogo->backend()->conditional_expression(int_btype, bad_index, | |
10325 | crash, start, loc); | |
e440a328 | 10326 | |
2c809f8f | 10327 | Bexpression* ret; |
e440a328 | 10328 | if (array_type->length() != NULL) |
10329 | { | |
ea664253 | 10330 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10331 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10332 | } |
10333 | else | |
10334 | { | |
2c809f8f | 10335 | // Slice. |
10336 | Expression* valptr = | |
35a54f17 | 10337 | array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10338 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 10339 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 10340 | |
10341 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
10342 | Btype* ele_btype = ele_type->get_backend(gogo); | |
10343 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 10344 | } |
ea664253 | 10345 | return ret; |
e440a328 | 10346 | } |
10347 | ||
10348 | // Array slice. | |
10349 | ||
acf2b673 | 10350 | if (this->cap_ != NULL) |
10351 | { | |
2c809f8f | 10352 | Bexpression* bounds_bcheck = |
ea664253 | 10353 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 10354 | bad_index = |
10355 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10356 | bad_index, loc); | |
10357 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
10358 | ||
10359 | Bexpression* cap_too_small = | |
10360 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
10361 | Bexpression* cap_too_large = | |
10362 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
10363 | Bexpression* bad_cap = | |
10364 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
10365 | cap_too_large, loc); | |
10366 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
10367 | bad_index, loc); | |
10368 | } | |
10369 | ||
10370 | Bexpression* end; | |
e440a328 | 10371 | if (this->end_->is_nil_expression()) |
2c809f8f | 10372 | end = length; |
e440a328 | 10373 | else |
10374 | { | |
2c809f8f | 10375 | Bexpression* bounds_bcheck = |
ea664253 | 10376 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 10377 | |
2c809f8f | 10378 | bad_index = |
10379 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10380 | bad_index, loc); | |
e440a328 | 10381 | |
ea664253 | 10382 | end = this->end_->get_backend(context); |
2c809f8f | 10383 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
10384 | Bexpression* end_too_small = | |
10385 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
10386 | Bexpression* end_too_large = | |
10387 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
10388 | Bexpression* bad_end = | |
10389 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
10390 | end_too_large, loc); | |
10391 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
10392 | bad_index, loc); | |
e440a328 | 10393 | } |
10394 | ||
35a54f17 | 10395 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10396 | Bexpression* val = valptr->get_backend(context); |
2c809f8f | 10397 | val = gogo->backend()->pointer_offset_expression(val, start, loc); |
e440a328 | 10398 | |
2c809f8f | 10399 | Bexpression* result_length = |
10400 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 10401 | |
2c809f8f | 10402 | Bexpression* result_capacity = |
10403 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 10404 | |
2c809f8f | 10405 | Btype* struct_btype = this->type()->get_backend(gogo); |
10406 | std::vector<Bexpression*> init; | |
10407 | init.push_back(val); | |
10408 | init.push_back(result_length); | |
10409 | init.push_back(result_capacity); | |
e440a328 | 10410 | |
2c809f8f | 10411 | Bexpression* ctor = |
10412 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
ea664253 | 10413 | return gogo->backend()->conditional_expression(struct_btype, bad_index, |
10414 | crash, ctor, loc); | |
e440a328 | 10415 | } |
10416 | ||
d751bb78 | 10417 | // Dump ast representation for an array index expression. |
10418 | ||
10419 | void | |
10420 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10421 | const | |
10422 | { | |
10423 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 10424 | this->start_, this->end_, this->cap_); |
d751bb78 | 10425 | } |
10426 | ||
acf2b673 | 10427 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 10428 | |
10429 | Expression* | |
10430 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 10431 | Expression* end, Expression* cap, |
10432 | Location location) | |
e440a328 | 10433 | { |
acf2b673 | 10434 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 10435 | } |
10436 | ||
10437 | // A string index. This is used for both indexing and slicing. | |
10438 | ||
10439 | class String_index_expression : public Expression | |
10440 | { | |
10441 | public: | |
10442 | String_index_expression(Expression* string, Expression* start, | |
b13c66cd | 10443 | Expression* end, Location location) |
e440a328 | 10444 | : Expression(EXPRESSION_STRING_INDEX, location), |
10445 | string_(string), start_(start), end_(end) | |
10446 | { } | |
10447 | ||
10448 | protected: | |
10449 | int | |
10450 | do_traverse(Traverse*); | |
10451 | ||
2c809f8f | 10452 | Expression* |
10453 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
10454 | ||
e440a328 | 10455 | Type* |
10456 | do_type(); | |
10457 | ||
10458 | void | |
10459 | do_determine_type(const Type_context*); | |
10460 | ||
10461 | void | |
10462 | do_check_types(Gogo*); | |
10463 | ||
10464 | Expression* | |
10465 | do_copy() | |
10466 | { | |
10467 | return Expression::make_string_index(this->string_->copy(), | |
10468 | this->start_->copy(), | |
10469 | (this->end_ == NULL | |
10470 | ? NULL | |
10471 | : this->end_->copy()), | |
10472 | this->location()); | |
10473 | } | |
10474 | ||
baef9f7a | 10475 | bool |
10476 | do_must_eval_subexpressions_in_order(int* skip) const | |
10477 | { | |
10478 | *skip = 1; | |
10479 | return true; | |
10480 | } | |
10481 | ||
ea664253 | 10482 | Bexpression* |
10483 | do_get_backend(Translate_context*); | |
e440a328 | 10484 | |
d751bb78 | 10485 | void |
10486 | do_dump_expression(Ast_dump_context*) const; | |
10487 | ||
e440a328 | 10488 | private: |
10489 | // The string we are getting a value from. | |
10490 | Expression* string_; | |
10491 | // The start or only index. | |
10492 | Expression* start_; | |
10493 | // The end index of a slice. This may be NULL for a single index, | |
10494 | // or it may be a nil expression for the length of the string. | |
10495 | Expression* end_; | |
10496 | }; | |
10497 | ||
10498 | // String index traversal. | |
10499 | ||
10500 | int | |
10501 | String_index_expression::do_traverse(Traverse* traverse) | |
10502 | { | |
10503 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10504 | return TRAVERSE_EXIT; | |
10505 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10506 | return TRAVERSE_EXIT; | |
10507 | if (this->end_ != NULL) | |
10508 | { | |
10509 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10510 | return TRAVERSE_EXIT; | |
10511 | } | |
10512 | return TRAVERSE_CONTINUE; | |
10513 | } | |
10514 | ||
2c809f8f | 10515 | Expression* |
10516 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
10517 | Statement_inserter* inserter) | |
e440a328 | 10518 | { |
2c809f8f | 10519 | Temporary_statement* temp; |
10520 | Location loc = this->location(); | |
10521 | if (!this->string_->is_variable()) | |
10522 | { | |
10523 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
10524 | inserter->insert(temp); | |
10525 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
10526 | } | |
10527 | if (!this->start_->is_variable()) | |
10528 | { | |
10529 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
10530 | inserter->insert(temp); | |
10531 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10532 | } | |
10533 | if (this->end_ != NULL | |
10534 | && !this->end_->is_nil_expression() | |
10535 | && !this->end_->is_variable()) | |
10536 | { | |
10537 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
10538 | inserter->insert(temp); | |
10539 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10540 | } | |
10541 | ||
10542 | return this; | |
10543 | } | |
10544 | ||
10545 | // Return the type of a string index. | |
10546 | ||
10547 | Type* | |
10548 | String_index_expression::do_type() | |
10549 | { | |
10550 | if (this->end_ == NULL) | |
10551 | return Type::lookup_integer_type("uint8"); | |
10552 | else | |
10553 | return this->string_->type(); | |
10554 | } | |
10555 | ||
10556 | // Determine the type of a string index. | |
10557 | ||
10558 | void | |
10559 | String_index_expression::do_determine_type(const Type_context*) | |
10560 | { | |
10561 | this->string_->determine_type_no_context(); | |
10562 | this->start_->determine_type_no_context(); | |
e440a328 | 10563 | if (this->end_ != NULL) |
93000773 | 10564 | this->end_->determine_type_no_context(); |
e440a328 | 10565 | } |
10566 | ||
10567 | // Check types of a string index. | |
10568 | ||
10569 | void | |
10570 | String_index_expression::do_check_types(Gogo*) | |
10571 | { | |
acdc230d | 10572 | Numeric_constant nc; |
10573 | unsigned long v; | |
10574 | if (this->start_->type()->integer_type() == NULL | |
10575 | && !this->start_->type()->is_error() | |
10576 | && (!this->start_->numeric_constant_value(&nc) | |
10577 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10578 | this->report_error(_("index must be integer")); |
10579 | if (this->end_ != NULL | |
10580 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 10581 | && !this->end_->type()->is_error() |
10582 | && !this->end_->is_nil_expression() | |
10583 | && !this->end_->is_error_expression() | |
10584 | && (!this->end_->numeric_constant_value(&nc) | |
10585 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10586 | this->report_error(_("slice end must be integer")); |
10587 | ||
10588 | std::string sval; | |
10589 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10590 | ||
0c77715b | 10591 | Numeric_constant inc; |
e440a328 | 10592 | mpz_t ival; |
0bd5d859 | 10593 | bool ival_valid = false; |
0c77715b | 10594 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10595 | { |
0bd5d859 | 10596 | ival_valid = true; |
e440a328 | 10597 | if (mpz_sgn(ival) < 0 |
10598 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
10599 | { | |
10600 | error_at(this->start_->location(), "string index out of bounds"); | |
10601 | this->set_is_error(); | |
10602 | } | |
10603 | } | |
10604 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10605 | { | |
0c77715b | 10606 | Numeric_constant enc; |
10607 | mpz_t eval; | |
10608 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10609 | { |
0c77715b | 10610 | if (mpz_sgn(eval) < 0 |
10611 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10612 | { |
10613 | error_at(this->end_->location(), "string index out of bounds"); | |
10614 | this->set_is_error(); | |
10615 | } | |
0bd5d859 | 10616 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10617 | this->report_error(_("inverted slice range")); | |
0c77715b | 10618 | mpz_clear(eval); |
e440a328 | 10619 | } |
10620 | } | |
0bd5d859 | 10621 | if (ival_valid) |
10622 | mpz_clear(ival); | |
e440a328 | 10623 | } |
10624 | ||
ea664253 | 10625 | // Get the backend representation for a string index. |
e440a328 | 10626 | |
ea664253 | 10627 | Bexpression* |
10628 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10629 | { |
b13c66cd | 10630 | Location loc = this->location(); |
2c809f8f | 10631 | Expression* string_arg = this->string_; |
10632 | if (this->string_->type()->points_to() != NULL) | |
10633 | string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc); | |
e440a328 | 10634 | |
2c809f8f | 10635 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 10636 | |
2c809f8f | 10637 | int code = (this->end_ == NULL |
10638 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10639 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 10640 | |
2c809f8f | 10641 | Gogo* gogo = context->gogo(); |
ea664253 | 10642 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 10643 | |
10644 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 10645 | |
2c809f8f | 10646 | // It is possible that an error occurred earlier because the start index |
10647 | // cannot be represented as an integer type. In this case, we shouldn't | |
10648 | // try casting the starting index into an integer since | |
10649 | // Type_conversion_expression will fail to get the backend representation. | |
10650 | // FIXME. | |
10651 | if (this->start_->type()->integer_type() == NULL | |
10652 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10653 | { | |
10654 | go_assert(saw_errors()); | |
ea664253 | 10655 | return context->backend()->error_expression(); |
2c809f8f | 10656 | } |
e440a328 | 10657 | |
2c809f8f | 10658 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
e440a328 | 10659 | |
2c809f8f | 10660 | if (this->end_ == NULL) |
10661 | { | |
10662 | Expression* length = | |
10663 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 10664 | |
2c809f8f | 10665 | Expression* start_too_large = |
10666 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
10667 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
10668 | bad_index, loc); | |
10669 | Expression* bytes = | |
10670 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 10671 | |
ea664253 | 10672 | Bexpression* bstart = start->get_backend(context); |
10673 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 10674 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 10675 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
10676 | Bexpression* index = | |
10677 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 10678 | |
2c809f8f | 10679 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 10680 | Bexpression* index_error = bad_index->get_backend(context); |
10681 | return gogo->backend()->conditional_expression(byte_btype, index_error, | |
10682 | crash, index, loc); | |
2c809f8f | 10683 | } |
10684 | ||
10685 | Expression* end = NULL; | |
10686 | if (this->end_->is_nil_expression()) | |
e440a328 | 10687 | { |
2c809f8f | 10688 | mpz_t neg_one; |
10689 | mpz_init_set_si(neg_one, -1); | |
10690 | end = Expression::make_integer(&neg_one, int_type, loc); | |
10691 | mpz_clear(neg_one); | |
e440a328 | 10692 | } |
10693 | else | |
10694 | { | |
2c809f8f | 10695 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
10696 | bad_index = | |
10697 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
10698 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 10699 | } |
2c809f8f | 10700 | |
10701 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
10702 | string_arg, start, end); | |
ea664253 | 10703 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 10704 | |
10705 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 10706 | Bexpression* index_error = bad_index->get_backend(context); |
10707 | return gogo->backend()->conditional_expression(str_btype, index_error, | |
10708 | crash, bstrslice, loc); | |
e440a328 | 10709 | } |
10710 | ||
d751bb78 | 10711 | // Dump ast representation for a string index expression. |
10712 | ||
10713 | void | |
10714 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10715 | const | |
10716 | { | |
acf2b673 | 10717 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
10718 | this->start_, this->end_, NULL); | |
d751bb78 | 10719 | } |
10720 | ||
e440a328 | 10721 | // Make a string index expression. END may be NULL. |
10722 | ||
10723 | Expression* | |
10724 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10725 | Expression* end, Location location) |
e440a328 | 10726 | { |
10727 | return new String_index_expression(string, start, end, location); | |
10728 | } | |
10729 | ||
10730 | // Class Map_index. | |
10731 | ||
10732 | // Get the type of the map. | |
10733 | ||
10734 | Map_type* | |
10735 | Map_index_expression::get_map_type() const | |
10736 | { | |
10737 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 10738 | if (mt == NULL) |
c484d925 | 10739 | go_assert(saw_errors()); |
e440a328 | 10740 | return mt; |
10741 | } | |
10742 | ||
10743 | // Map index traversal. | |
10744 | ||
10745 | int | |
10746 | Map_index_expression::do_traverse(Traverse* traverse) | |
10747 | { | |
10748 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
10749 | return TRAVERSE_EXIT; | |
10750 | return Expression::traverse(&this->index_, traverse); | |
10751 | } | |
10752 | ||
2c809f8f | 10753 | // We need to pass in a pointer to the key, so flatten the index into a |
10754 | // temporary variable if it isn't already. The value pointer will be | |
10755 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
10756 | // recomputation. | |
10757 | ||
10758 | Expression* | |
10759 | Map_index_expression::do_flatten(Gogo*, Named_object*, | |
10760 | Statement_inserter* inserter) | |
10761 | { | |
10762 | Map_type* mt = this->get_map_type(); | |
10763 | if (this->index_->type() != mt->key_type()) | |
10764 | this->index_ = Expression::make_cast(mt->key_type(), this->index_, | |
10765 | this->location()); | |
10766 | ||
10767 | if (!this->index_->is_variable()) | |
10768 | { | |
10769 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
10770 | this->location()); | |
10771 | inserter->insert(temp); | |
10772 | this->index_ = Expression::make_temporary_reference(temp, | |
10773 | this->location()); | |
10774 | } | |
10775 | ||
10776 | if (this->value_pointer_ == NULL) | |
10777 | this->get_value_pointer(this->is_lvalue_); | |
10778 | if (!this->value_pointer_->is_variable()) | |
10779 | { | |
10780 | Temporary_statement* temp = | |
10781 | Statement::make_temporary(NULL, this->value_pointer_, | |
10782 | this->location()); | |
10783 | inserter->insert(temp); | |
10784 | this->value_pointer_ = | |
10785 | Expression::make_temporary_reference(temp, this->location()); | |
10786 | } | |
10787 | ||
10788 | return this; | |
10789 | } | |
10790 | ||
e440a328 | 10791 | // Return the type of a map index. |
10792 | ||
10793 | Type* | |
10794 | Map_index_expression::do_type() | |
10795 | { | |
c7524fae | 10796 | Map_type* mt = this->get_map_type(); |
10797 | if (mt == NULL) | |
10798 | return Type::make_error_type(); | |
10799 | Type* type = mt->val_type(); | |
e440a328 | 10800 | // If this map index is in a tuple assignment, we actually return a |
10801 | // pointer to the value type. Tuple_map_assignment_statement is | |
10802 | // responsible for handling this correctly. We need to get the type | |
10803 | // right in case this gets assigned to a temporary variable. | |
10804 | if (this->is_in_tuple_assignment_) | |
10805 | type = Type::make_pointer_type(type); | |
10806 | return type; | |
10807 | } | |
10808 | ||
10809 | // Fix the type of a map index. | |
10810 | ||
10811 | void | |
10812 | Map_index_expression::do_determine_type(const Type_context*) | |
10813 | { | |
10814 | this->map_->determine_type_no_context(); | |
c7524fae | 10815 | Map_type* mt = this->get_map_type(); |
10816 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
10817 | Type_context subcontext(key_type, false); | |
e440a328 | 10818 | this->index_->determine_type(&subcontext); |
10819 | } | |
10820 | ||
10821 | // Check types of a map index. | |
10822 | ||
10823 | void | |
10824 | Map_index_expression::do_check_types(Gogo*) | |
10825 | { | |
10826 | std::string reason; | |
c7524fae | 10827 | Map_type* mt = this->get_map_type(); |
10828 | if (mt == NULL) | |
10829 | return; | |
10830 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 10831 | { |
10832 | if (reason.empty()) | |
10833 | this->report_error(_("incompatible type for map index")); | |
10834 | else | |
10835 | { | |
10836 | error_at(this->location(), "incompatible type for map index (%s)", | |
10837 | reason.c_str()); | |
10838 | this->set_is_error(); | |
10839 | } | |
10840 | } | |
10841 | } | |
10842 | ||
ea664253 | 10843 | // Get the backend representation for a map index. |
e440a328 | 10844 | |
ea664253 | 10845 | Bexpression* |
10846 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10847 | { |
10848 | Map_type* type = this->get_map_type(); | |
c7524fae | 10849 | if (type == NULL) |
2c809f8f | 10850 | { |
10851 | go_assert(saw_errors()); | |
ea664253 | 10852 | return context->backend()->error_expression(); |
2c809f8f | 10853 | } |
e440a328 | 10854 | |
2c809f8f | 10855 | go_assert(this->value_pointer_ != NULL |
10856 | && this->value_pointer_->is_variable()); | |
e440a328 | 10857 | |
2c809f8f | 10858 | Bexpression* ret; |
e440a328 | 10859 | if (this->is_lvalue_) |
2c809f8f | 10860 | { |
10861 | Expression* val = | |
10862 | Expression::make_unary(OPERATOR_MULT, this->value_pointer_, | |
10863 | this->location()); | |
ea664253 | 10864 | ret = val->get_backend(context); |
2c809f8f | 10865 | } |
e440a328 | 10866 | else if (this->is_in_tuple_assignment_) |
10867 | { | |
10868 | // Tuple_map_assignment_statement is responsible for using this | |
10869 | // appropriately. | |
ea664253 | 10870 | ret = this->value_pointer_->get_backend(context); |
e440a328 | 10871 | } |
10872 | else | |
10873 | { | |
2c809f8f | 10874 | Location loc = this->location(); |
10875 | ||
10876 | Expression* nil_check = | |
10877 | Expression::make_binary(OPERATOR_EQEQ, this->value_pointer_, | |
10878 | Expression::make_nil(loc), loc); | |
ea664253 | 10879 | Bexpression* bnil_check = nil_check->get_backend(context); |
2c809f8f | 10880 | Expression* val = |
10881 | Expression::make_unary(OPERATOR_MULT, this->value_pointer_, loc); | |
ea664253 | 10882 | Bexpression* bval = val->get_backend(context); |
2c809f8f | 10883 | |
63697958 | 10884 | Gogo* gogo = context->gogo(); |
10885 | Btype* val_btype = type->val_type()->get_backend(gogo); | |
10886 | Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); | |
2c809f8f | 10887 | ret = gogo->backend()->conditional_expression(val_btype, bnil_check, |
10888 | val_zero, bval, loc); | |
e440a328 | 10889 | } |
ea664253 | 10890 | return ret; |
e440a328 | 10891 | } |
10892 | ||
2c809f8f | 10893 | // Get an expression for the map index. This returns an expression which |
10894 | // evaluates to a pointer to a value. The pointer will be NULL if the key is | |
e440a328 | 10895 | // not in the map. |
10896 | ||
2c809f8f | 10897 | Expression* |
10898 | Map_index_expression::get_value_pointer(bool insert) | |
e440a328 | 10899 | { |
2c809f8f | 10900 | if (this->value_pointer_ == NULL) |
746d2e73 | 10901 | { |
2c809f8f | 10902 | Map_type* type = this->get_map_type(); |
10903 | if (type == NULL) | |
746d2e73 | 10904 | { |
2c809f8f | 10905 | go_assert(saw_errors()); |
10906 | return Expression::make_error(this->location()); | |
746d2e73 | 10907 | } |
e440a328 | 10908 | |
2c809f8f | 10909 | Location loc = this->location(); |
10910 | Expression* map_ref = this->map_; | |
10911 | if (this->map_->type()->points_to() != NULL) | |
10912 | map_ref = Expression::make_unary(OPERATOR_MULT, map_ref, loc); | |
e440a328 | 10913 | |
2c809f8f | 10914 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, this->index_, |
10915 | loc); | |
10916 | Expression* map_index = | |
10917 | Runtime::make_call(Runtime::MAP_INDEX, loc, 3, | |
10918 | map_ref, index_ptr, | |
10919 | Expression::make_boolean(insert, loc)); | |
10920 | ||
10921 | Type* val_type = type->val_type(); | |
10922 | this->value_pointer_ = | |
10923 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
10924 | map_index, this->location()); | |
10925 | } | |
10926 | return this->value_pointer_; | |
e440a328 | 10927 | } |
10928 | ||
d751bb78 | 10929 | // Dump ast representation for a map index expression |
10930 | ||
10931 | void | |
10932 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10933 | const | |
10934 | { | |
acf2b673 | 10935 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
10936 | this->index_, NULL, NULL); | |
d751bb78 | 10937 | } |
10938 | ||
e440a328 | 10939 | // Make a map index expression. |
10940 | ||
10941 | Map_index_expression* | |
10942 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 10943 | Location location) |
e440a328 | 10944 | { |
10945 | return new Map_index_expression(map, index, location); | |
10946 | } | |
10947 | ||
10948 | // Class Field_reference_expression. | |
10949 | ||
149eabc5 | 10950 | // Lower a field reference expression. There is nothing to lower, but |
10951 | // this is where we generate the tracking information for fields with | |
10952 | // the magic go:"track" tag. | |
10953 | ||
10954 | Expression* | |
10955 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
10956 | Statement_inserter* inserter, int) | |
10957 | { | |
10958 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
10959 | if (struct_type == NULL) | |
10960 | { | |
10961 | // Error will be reported elsewhere. | |
10962 | return this; | |
10963 | } | |
10964 | const Struct_field* field = struct_type->field(this->field_index_); | |
10965 | if (field == NULL) | |
10966 | return this; | |
10967 | if (!field->has_tag()) | |
10968 | return this; | |
10969 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
10970 | return this; | |
10971 | ||
10972 | // We have found a reference to a tracked field. Build a call to | |
10973 | // the runtime function __go_fieldtrack with a string that describes | |
10974 | // the field. FIXME: We should only call this once per referenced | |
10975 | // field per function, not once for each reference to the field. | |
10976 | ||
10977 | if (this->called_fieldtrack_) | |
10978 | return this; | |
10979 | this->called_fieldtrack_ = true; | |
10980 | ||
10981 | Location loc = this->location(); | |
10982 | ||
10983 | std::string s = "fieldtrack \""; | |
10984 | Named_type* nt = this->expr_->type()->named_type(); | |
10985 | if (nt == NULL || nt->named_object()->package() == NULL) | |
10986 | s.append(gogo->pkgpath()); | |
10987 | else | |
10988 | s.append(nt->named_object()->package()->pkgpath()); | |
10989 | s.push_back('.'); | |
10990 | if (nt != NULL) | |
5c29ad36 | 10991 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 10992 | s.push_back('.'); |
10993 | s.append(field->field_name()); | |
10994 | s.push_back('"'); | |
10995 | ||
10996 | // We can't use a string here, because internally a string holds a | |
10997 | // pointer to the actual bytes; when the linker garbage collects the | |
10998 | // string, it won't garbage collect the bytes. So we use a | |
10999 | // [...]byte. | |
11000 | ||
11001 | mpz_t val; | |
11002 | mpz_init_set_ui(val, s.length()); | |
11003 | Expression* length_expr = Expression::make_integer(&val, NULL, loc); | |
11004 | mpz_clear(val); | |
11005 | ||
11006 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
11007 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
11008 | ||
11009 | Expression_list* bytes = new Expression_list(); | |
11010 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11011 | { | |
11012 | mpz_init_set_ui(val, *p); | |
11013 | Expression* byte = Expression::make_integer(&val, NULL, loc); | |
11014 | mpz_clear(val); | |
11015 | bytes->push_back(byte); | |
11016 | } | |
11017 | ||
11018 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11019 | bytes, false, loc); |
149eabc5 | 11020 | |
11021 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11022 | ||
11023 | static int count; | |
11024 | char buf[50]; | |
11025 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11026 | ++count; | |
11027 | ||
11028 | Named_object* no = gogo->add_variable(buf, var); | |
11029 | e = Expression::make_var_reference(no, loc); | |
11030 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11031 | ||
11032 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
11033 | inserter->insert(Statement::make_statement(call, false)); | |
11034 | ||
11035 | // Put this function, and the global variable we just created, into | |
11036 | // unique sections. This will permit the linker to garbage collect | |
11037 | // them if they are not referenced. The effect is that the only | |
11038 | // strings, indicating field references, that will wind up in the | |
11039 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11040 | if (function != NULL) |
11041 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11042 | var->set_in_unique_section(); |
11043 | ||
11044 | return this; | |
11045 | } | |
11046 | ||
e440a328 | 11047 | // Return the type of a field reference. |
11048 | ||
11049 | Type* | |
11050 | Field_reference_expression::do_type() | |
11051 | { | |
b0e628fb | 11052 | Type* type = this->expr_->type(); |
5c13bd80 | 11053 | if (type->is_error()) |
b0e628fb | 11054 | return type; |
11055 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11056 | go_assert(struct_type != NULL); |
e440a328 | 11057 | return struct_type->field(this->field_index_)->type(); |
11058 | } | |
11059 | ||
11060 | // Check the types for a field reference. | |
11061 | ||
11062 | void | |
11063 | Field_reference_expression::do_check_types(Gogo*) | |
11064 | { | |
b0e628fb | 11065 | Type* type = this->expr_->type(); |
5c13bd80 | 11066 | if (type->is_error()) |
b0e628fb | 11067 | return; |
11068 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11069 | go_assert(struct_type != NULL); |
11070 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11071 | } |
11072 | ||
ea664253 | 11073 | // Get the backend representation for a field reference. |
e440a328 | 11074 | |
ea664253 | 11075 | Bexpression* |
11076 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11077 | { |
ea664253 | 11078 | Bexpression* bstruct = this->expr_->get_backend(context); |
11079 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11080 | this->field_index_, | |
11081 | this->location()); | |
e440a328 | 11082 | } |
11083 | ||
d751bb78 | 11084 | // Dump ast representation for a field reference expression. |
11085 | ||
11086 | void | |
11087 | Field_reference_expression::do_dump_expression( | |
11088 | Ast_dump_context* ast_dump_context) const | |
11089 | { | |
11090 | this->expr_->dump_expression(ast_dump_context); | |
11091 | ast_dump_context->ostream() << "." << this->field_index_; | |
11092 | } | |
11093 | ||
e440a328 | 11094 | // Make a reference to a qualified identifier in an expression. |
11095 | ||
11096 | Field_reference_expression* | |
11097 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11098 | Location location) |
e440a328 | 11099 | { |
11100 | return new Field_reference_expression(expr, field_index, location); | |
11101 | } | |
11102 | ||
11103 | // Class Interface_field_reference_expression. | |
11104 | ||
2387f644 | 11105 | // Return an expression for the pointer to the function to call. |
e440a328 | 11106 | |
2387f644 | 11107 | Expression* |
11108 | Interface_field_reference_expression::get_function() | |
e440a328 | 11109 | { |
2387f644 | 11110 | Expression* ref = this->expr_; |
11111 | Location loc = this->location(); | |
11112 | if (ref->type()->points_to() != NULL) | |
11113 | ref = Expression::make_unary(OPERATOR_MULT, ref, loc); | |
e440a328 | 11114 | |
2387f644 | 11115 | Expression* mtable = |
11116 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11117 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11118 | |
11119 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11120 | unsigned int index; |
11121 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11122 | go_assert(field != NULL); | |
11123 | mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc); | |
11124 | return Expression::make_field_reference(mtable, index, loc); | |
e440a328 | 11125 | } |
11126 | ||
2387f644 | 11127 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11128 | // function. |
11129 | ||
2387f644 | 11130 | Expression* |
11131 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11132 | { |
2387f644 | 11133 | Expression* expr = this->expr_; |
11134 | if (expr->type()->points_to() != NULL) | |
11135 | expr = Expression::make_unary(OPERATOR_MULT, expr, this->location()); | |
11136 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, | |
11137 | this->location()); | |
e440a328 | 11138 | } |
11139 | ||
11140 | // Traversal. | |
11141 | ||
11142 | int | |
11143 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11144 | { | |
11145 | return Expression::traverse(&this->expr_, traverse); | |
11146 | } | |
11147 | ||
0afbb937 | 11148 | // Lower the expression. If this expression is not called, we need to |
11149 | // evaluate the expression twice when converting to the backend | |
11150 | // interface. So introduce a temporary variable if necessary. | |
11151 | ||
11152 | Expression* | |
11153 | Interface_field_reference_expression::do_lower(Gogo*, Named_object*, | |
11154 | Statement_inserter* inserter, | |
11155 | int) | |
11156 | { | |
2387f644 | 11157 | if (!this->expr_->is_variable()) |
0afbb937 | 11158 | { |
11159 | Temporary_statement* temp = | |
11160 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11161 | inserter->insert(temp); | |
11162 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11163 | this->location()); | |
11164 | } | |
11165 | return this; | |
11166 | } | |
11167 | ||
e440a328 | 11168 | // Return the type of an interface field reference. |
11169 | ||
11170 | Type* | |
11171 | Interface_field_reference_expression::do_type() | |
11172 | { | |
11173 | Type* expr_type = this->expr_->type(); | |
11174 | ||
11175 | Type* points_to = expr_type->points_to(); | |
11176 | if (points_to != NULL) | |
11177 | expr_type = points_to; | |
11178 | ||
11179 | Interface_type* interface_type = expr_type->interface_type(); | |
11180 | if (interface_type == NULL) | |
11181 | return Type::make_error_type(); | |
11182 | ||
11183 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11184 | if (method == NULL) | |
11185 | return Type::make_error_type(); | |
11186 | ||
11187 | return method->type(); | |
11188 | } | |
11189 | ||
11190 | // Determine types. | |
11191 | ||
11192 | void | |
11193 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11194 | { | |
11195 | this->expr_->determine_type_no_context(); | |
11196 | } | |
11197 | ||
11198 | // Check the types for an interface field reference. | |
11199 | ||
11200 | void | |
11201 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11202 | { | |
11203 | Type* type = this->expr_->type(); | |
11204 | ||
11205 | Type* points_to = type->points_to(); | |
11206 | if (points_to != NULL) | |
11207 | type = points_to; | |
11208 | ||
11209 | Interface_type* interface_type = type->interface_type(); | |
11210 | if (interface_type == NULL) | |
5c491127 | 11211 | { |
11212 | if (!type->is_error_type()) | |
11213 | this->report_error(_("expected interface or pointer to interface")); | |
11214 | } | |
e440a328 | 11215 | else |
11216 | { | |
11217 | const Typed_identifier* method = | |
11218 | interface_type->find_method(this->name_); | |
11219 | if (method == NULL) | |
11220 | { | |
11221 | error_at(this->location(), "method %qs not in interface", | |
11222 | Gogo::message_name(this->name_).c_str()); | |
11223 | this->set_is_error(); | |
11224 | } | |
11225 | } | |
11226 | } | |
11227 | ||
0afbb937 | 11228 | // If an interface field reference is not simply called, then it is |
11229 | // represented as a closure. The closure will hold a single variable, | |
11230 | // the value of the interface on which the method should be called. | |
11231 | // The function will be a simple thunk that pulls the value from the | |
11232 | // closure and calls the method with the remaining arguments. | |
11233 | ||
11234 | // Because method values are not common, we don't build all thunks for | |
11235 | // all possible interface methods, but instead only build them as we | |
11236 | // need them. In particular, we even build them on demand for | |
11237 | // interface methods defined in other packages. | |
11238 | ||
11239 | Interface_field_reference_expression::Interface_method_thunks | |
11240 | Interface_field_reference_expression::interface_method_thunks; | |
11241 | ||
11242 | // Find or create the thunk to call method NAME on TYPE. | |
11243 | ||
11244 | Named_object* | |
11245 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11246 | Interface_type* type, | |
11247 | const std::string& name) | |
11248 | { | |
11249 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11250 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11251 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11252 | if (ins.second) | |
11253 | { | |
11254 | // This is the first time we have seen this interface. | |
11255 | ins.first->second = new Method_thunks(); | |
11256 | } | |
11257 | ||
11258 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11259 | p != ins.first->second->end(); | |
11260 | p++) | |
11261 | if (p->first == name) | |
11262 | return p->second; | |
11263 | ||
11264 | Location loc = type->location(); | |
11265 | ||
11266 | const Typed_identifier* method_id = type->find_method(name); | |
11267 | if (method_id == NULL) | |
11268 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11269 | ||
11270 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11271 | if (orig_fntype == NULL) | |
11272 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11273 | ||
11274 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11275 | // The type here is wrong--it should be the C function type. But it |
11276 | // doesn't really matter. | |
0afbb937 | 11277 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11278 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11279 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
11280 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
11281 | closure_type = Type::make_pointer_type(closure_type); | |
11282 | ||
f8bdf81a | 11283 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11284 | |
11285 | Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, | |
11286 | false, loc); | |
11287 | ||
f8bdf81a | 11288 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11289 | cvar->set_is_used(); | |
11290 | Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); | |
11291 | new_no->func_value()->set_closure_var(cp); | |
0afbb937 | 11292 | |
f8bdf81a | 11293 | gogo->start_block(loc); |
0afbb937 | 11294 | |
11295 | // Field 0 of the closure is the function code pointer, field 1 is | |
11296 | // the value on which to invoke the method. | |
11297 | Expression* arg = Expression::make_var_reference(cp, loc); | |
11298 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
11299 | arg = Expression::make_field_reference(arg, 1, loc); | |
11300 | ||
11301 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11302 | loc); | |
11303 | ||
11304 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11305 | Expression_list* args; | |
11306 | if (orig_params == NULL || orig_params->empty()) | |
11307 | args = NULL; | |
11308 | else | |
11309 | { | |
11310 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11311 | args = new Expression_list(); | |
11312 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11313 | p != new_params->end(); |
0afbb937 | 11314 | ++p) |
11315 | { | |
11316 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11317 | go_assert(p_no != NULL | |
11318 | && p_no->is_variable() | |
11319 | && p_no->var_value()->is_parameter()); | |
11320 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11321 | } | |
11322 | } | |
11323 | ||
11324 | Call_expression* call = Expression::make_call(ifre, args, | |
11325 | orig_fntype->is_varargs(), | |
11326 | loc); | |
11327 | call->set_varargs_are_lowered(); | |
11328 | ||
11329 | Statement* s = Statement::make_return_from_call(call, loc); | |
11330 | gogo->add_statement(s); | |
11331 | Block* b = gogo->finish_block(loc); | |
11332 | gogo->add_block(b, loc); | |
11333 | gogo->lower_block(new_no, b); | |
a32698ee | 11334 | gogo->flatten_block(new_no, b); |
0afbb937 | 11335 | gogo->finish_function(loc); |
11336 | ||
11337 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11338 | return new_no; | |
11339 | } | |
11340 | ||
ea664253 | 11341 | // Get the backend representation for a method value. |
e440a328 | 11342 | |
ea664253 | 11343 | Bexpression* |
11344 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11345 | { |
0afbb937 | 11346 | Interface_type* type = this->expr_->type()->interface_type(); |
11347 | if (type == NULL) | |
11348 | { | |
11349 | go_assert(saw_errors()); | |
ea664253 | 11350 | return context->backend()->error_expression(); |
0afbb937 | 11351 | } |
11352 | ||
11353 | Named_object* thunk = | |
11354 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11355 | type, this->name_); | |
11356 | if (thunk->is_erroneous()) | |
11357 | { | |
11358 | go_assert(saw_errors()); | |
ea664253 | 11359 | return context->backend()->error_expression(); |
0afbb937 | 11360 | } |
11361 | ||
11362 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11363 | // the lowering pass because at that point we don't know whether we | |
11364 | // need to create the thunk or not. If the expression is called, we | |
11365 | // don't need the thunk. | |
11366 | ||
11367 | Location loc = this->location(); | |
11368 | ||
11369 | Struct_field_list* fields = new Struct_field_list(); | |
11370 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11371 | thunk->func_value()->type(), | |
11372 | loc))); | |
11373 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11374 | this->expr_->type(), | |
11375 | loc))); | |
11376 | Struct_type* st = Type::make_struct_type(fields, loc); | |
11377 | ||
11378 | Expression_list* vals = new Expression_list(); | |
11379 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
11380 | vals->push_back(this->expr_); | |
11381 | ||
11382 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 11383 | Bexpression* bclosure = |
11384 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 11385 | |
2387f644 | 11386 | Expression* nil_check = |
11387 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
11388 | Expression::make_nil(loc), loc); | |
ea664253 | 11389 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 11390 | |
2387f644 | 11391 | Gogo* gogo = context->gogo(); |
ea664253 | 11392 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
11393 | loc)->get_backend(context); | |
2387f644 | 11394 | |
11395 | Bexpression* bcond = | |
a32698ee | 11396 | gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc); |
2387f644 | 11397 | Bstatement* cond_statement = gogo->backend()->expression_statement(bcond); |
ea664253 | 11398 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 11399 | } |
11400 | ||
d751bb78 | 11401 | // Dump ast representation for an interface field reference. |
11402 | ||
11403 | void | |
11404 | Interface_field_reference_expression::do_dump_expression( | |
11405 | Ast_dump_context* ast_dump_context) const | |
11406 | { | |
11407 | this->expr_->dump_expression(ast_dump_context); | |
11408 | ast_dump_context->ostream() << "." << this->name_; | |
11409 | } | |
11410 | ||
e440a328 | 11411 | // Make a reference to a field in an interface. |
11412 | ||
11413 | Expression* | |
11414 | Expression::make_interface_field_reference(Expression* expr, | |
11415 | const std::string& field, | |
b13c66cd | 11416 | Location location) |
e440a328 | 11417 | { |
11418 | return new Interface_field_reference_expression(expr, field, location); | |
11419 | } | |
11420 | ||
11421 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
11422 | // is lowered after we know the type of the left hand side. | |
11423 | ||
11424 | class Selector_expression : public Parser_expression | |
11425 | { | |
11426 | public: | |
11427 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 11428 | Location location) |
e440a328 | 11429 | : Parser_expression(EXPRESSION_SELECTOR, location), |
11430 | left_(left), name_(name) | |
11431 | { } | |
11432 | ||
11433 | protected: | |
11434 | int | |
11435 | do_traverse(Traverse* traverse) | |
11436 | { return Expression::traverse(&this->left_, traverse); } | |
11437 | ||
11438 | Expression* | |
ceeb4318 | 11439 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 11440 | |
11441 | Expression* | |
11442 | do_copy() | |
11443 | { | |
11444 | return new Selector_expression(this->left_->copy(), this->name_, | |
11445 | this->location()); | |
11446 | } | |
11447 | ||
d751bb78 | 11448 | void |
11449 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
11450 | ||
e440a328 | 11451 | private: |
11452 | Expression* | |
11453 | lower_method_expression(Gogo*); | |
11454 | ||
11455 | // The expression on the left hand side. | |
11456 | Expression* left_; | |
11457 | // The name on the right hand side. | |
11458 | std::string name_; | |
11459 | }; | |
11460 | ||
11461 | // Lower a selector expression once we know the real type of the left | |
11462 | // hand side. | |
11463 | ||
11464 | Expression* | |
ceeb4318 | 11465 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
11466 | int) | |
e440a328 | 11467 | { |
11468 | Expression* left = this->left_; | |
11469 | if (left->is_type_expression()) | |
11470 | return this->lower_method_expression(gogo); | |
11471 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
11472 | this->location()); | |
11473 | } | |
11474 | ||
11475 | // Lower a method expression T.M or (*T).M. We turn this into a | |
11476 | // function literal. | |
11477 | ||
11478 | Expression* | |
11479 | Selector_expression::lower_method_expression(Gogo* gogo) | |
11480 | { | |
b13c66cd | 11481 | Location location = this->location(); |
e440a328 | 11482 | Type* type = this->left_->type(); |
11483 | const std::string& name(this->name_); | |
11484 | ||
11485 | bool is_pointer; | |
11486 | if (type->points_to() == NULL) | |
11487 | is_pointer = false; | |
11488 | else | |
11489 | { | |
11490 | is_pointer = true; | |
11491 | type = type->points_to(); | |
11492 | } | |
11493 | Named_type* nt = type->named_type(); | |
11494 | if (nt == NULL) | |
11495 | { | |
11496 | error_at(location, | |
11497 | ("method expression requires named type or " | |
11498 | "pointer to named type")); | |
11499 | return Expression::make_error(location); | |
11500 | } | |
11501 | ||
11502 | bool is_ambiguous; | |
11503 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 11504 | const Typed_identifier* imethod = NULL; |
dcc8506b | 11505 | if (method == NULL && !is_pointer) |
ab1468c3 | 11506 | { |
11507 | Interface_type* it = nt->interface_type(); | |
11508 | if (it != NULL) | |
11509 | imethod = it->find_method(name); | |
11510 | } | |
11511 | ||
11512 | if (method == NULL && imethod == NULL) | |
e440a328 | 11513 | { |
11514 | if (!is_ambiguous) | |
dcc8506b | 11515 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
11516 | is_pointer ? "*" : "", | |
e440a328 | 11517 | nt->message_name().c_str(), |
11518 | Gogo::message_name(name).c_str()); | |
11519 | else | |
dcc8506b | 11520 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 11521 | Gogo::message_name(name).c_str(), |
dcc8506b | 11522 | is_pointer ? "*" : "", |
e440a328 | 11523 | nt->message_name().c_str()); |
11524 | return Expression::make_error(location); | |
11525 | } | |
11526 | ||
ab1468c3 | 11527 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 11528 | { |
11529 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
11530 | nt->message_name().c_str(), | |
11531 | Gogo::message_name(name).c_str()); | |
11532 | return Expression::make_error(location); | |
11533 | } | |
11534 | ||
11535 | // Build a new function type in which the receiver becomes the first | |
11536 | // argument. | |
ab1468c3 | 11537 | Function_type* method_type; |
11538 | if (method != NULL) | |
11539 | { | |
11540 | method_type = method->type(); | |
c484d925 | 11541 | go_assert(method_type->is_method()); |
ab1468c3 | 11542 | } |
11543 | else | |
11544 | { | |
11545 | method_type = imethod->type()->function_type(); | |
c484d925 | 11546 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 11547 | } |
e440a328 | 11548 | |
11549 | const char* const receiver_name = "$this"; | |
11550 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
11551 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
11552 | location)); | |
11553 | ||
11554 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
11555 | if (method_parameters != NULL) | |
11556 | { | |
f470da59 | 11557 | int i = 0; |
e440a328 | 11558 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
11559 | p != method_parameters->end(); | |
f470da59 | 11560 | ++p, ++i) |
11561 | { | |
68883531 | 11562 | if (!p->name().empty()) |
f470da59 | 11563 | parameters->push_back(*p); |
11564 | else | |
11565 | { | |
11566 | char buf[20]; | |
11567 | snprintf(buf, sizeof buf, "$param%d", i); | |
11568 | parameters->push_back(Typed_identifier(buf, p->type(), | |
11569 | p->location())); | |
11570 | } | |
11571 | } | |
e440a328 | 11572 | } |
11573 | ||
11574 | const Typed_identifier_list* method_results = method_type->results(); | |
11575 | Typed_identifier_list* results; | |
11576 | if (method_results == NULL) | |
11577 | results = NULL; | |
11578 | else | |
11579 | { | |
11580 | results = new Typed_identifier_list(); | |
11581 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
11582 | p != method_results->end(); | |
11583 | ++p) | |
11584 | results->push_back(*p); | |
11585 | } | |
11586 | ||
11587 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
11588 | location); | |
11589 | if (method_type->is_varargs()) | |
11590 | fntype->set_is_varargs(); | |
11591 | ||
11592 | // We generate methods which always takes a pointer to the receiver | |
11593 | // as their first argument. If this is for a pointer type, we can | |
11594 | // simply reuse the existing function. We use an internal hack to | |
11595 | // get the right type. | |
8381eda7 | 11596 | // FIXME: This optimization is disabled because it doesn't yet work |
11597 | // with function descriptors when the method expression is not | |
11598 | // directly called. | |
11599 | if (method != NULL && is_pointer && false) | |
e440a328 | 11600 | { |
11601 | Named_object* mno = (method->needs_stub_method() | |
11602 | ? method->stub_object() | |
11603 | : method->named_object()); | |
11604 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
11605 | f = Expression::make_cast(fntype, f, location); | |
11606 | Type_conversion_expression* tce = | |
11607 | static_cast<Type_conversion_expression*>(f); | |
11608 | tce->set_may_convert_function_types(); | |
11609 | return f; | |
11610 | } | |
11611 | ||
11612 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
11613 | location); | |
11614 | ||
11615 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 11616 | go_assert(vno != NULL); |
e440a328 | 11617 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 11618 | Expression* bm; |
11619 | if (method != NULL) | |
11620 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
11621 | else | |
11622 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 11623 | |
11624 | // Even though we found the method above, if it has an error type we | |
11625 | // may see an error here. | |
11626 | if (bm->is_error_expression()) | |
463fe805 | 11627 | { |
11628 | gogo->finish_function(location); | |
11629 | return bm; | |
11630 | } | |
e440a328 | 11631 | |
11632 | Expression_list* args; | |
f470da59 | 11633 | if (parameters->size() <= 1) |
e440a328 | 11634 | args = NULL; |
11635 | else | |
11636 | { | |
11637 | args = new Expression_list(); | |
f470da59 | 11638 | Typed_identifier_list::const_iterator p = parameters->begin(); |
11639 | ++p; | |
11640 | for (; p != parameters->end(); ++p) | |
e440a328 | 11641 | { |
11642 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 11643 | go_assert(vno != NULL); |
e440a328 | 11644 | args->push_back(Expression::make_var_reference(vno, location)); |
11645 | } | |
11646 | } | |
11647 | ||
ceeb4318 | 11648 | gogo->start_block(location); |
11649 | ||
e440a328 | 11650 | Call_expression* call = Expression::make_call(bm, args, |
11651 | method_type->is_varargs(), | |
11652 | location); | |
11653 | ||
0afbb937 | 11654 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 11655 | gogo->add_statement(s); |
11656 | ||
ceeb4318 | 11657 | Block* b = gogo->finish_block(location); |
11658 | ||
11659 | gogo->add_block(b, location); | |
11660 | ||
11661 | // Lower the call in case there are multiple results. | |
11662 | gogo->lower_block(no, b); | |
a32698ee | 11663 | gogo->flatten_block(no, b); |
ceeb4318 | 11664 | |
e440a328 | 11665 | gogo->finish_function(location); |
11666 | ||
11667 | return Expression::make_func_reference(no, NULL, location); | |
11668 | } | |
11669 | ||
d751bb78 | 11670 | // Dump the ast for a selector expression. |
11671 | ||
11672 | void | |
11673 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11674 | const | |
11675 | { | |
11676 | ast_dump_context->dump_expression(this->left_); | |
11677 | ast_dump_context->ostream() << "."; | |
11678 | ast_dump_context->ostream() << this->name_; | |
11679 | } | |
11680 | ||
e440a328 | 11681 | // Make a selector expression. |
11682 | ||
11683 | Expression* | |
11684 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 11685 | Location location) |
e440a328 | 11686 | { |
11687 | return new Selector_expression(left, name, location); | |
11688 | } | |
11689 | ||
11690 | // Implement the builtin function new. | |
11691 | ||
11692 | class Allocation_expression : public Expression | |
11693 | { | |
11694 | public: | |
b13c66cd | 11695 | Allocation_expression(Type* type, Location location) |
e440a328 | 11696 | : Expression(EXPRESSION_ALLOCATION, location), |
11697 | type_(type) | |
11698 | { } | |
11699 | ||
11700 | protected: | |
11701 | int | |
11702 | do_traverse(Traverse* traverse) | |
11703 | { return Type::traverse(this->type_, traverse); } | |
11704 | ||
11705 | Type* | |
11706 | do_type() | |
11707 | { return Type::make_pointer_type(this->type_); } | |
11708 | ||
11709 | void | |
11710 | do_determine_type(const Type_context*) | |
11711 | { } | |
11712 | ||
e440a328 | 11713 | Expression* |
11714 | do_copy() | |
11715 | { return new Allocation_expression(this->type_, this->location()); } | |
11716 | ||
ea664253 | 11717 | Bexpression* |
11718 | do_get_backend(Translate_context*); | |
e440a328 | 11719 | |
d751bb78 | 11720 | void |
11721 | do_dump_expression(Ast_dump_context*) const; | |
11722 | ||
e440a328 | 11723 | private: |
11724 | // The type we are allocating. | |
11725 | Type* type_; | |
11726 | }; | |
11727 | ||
ea664253 | 11728 | // Return the backend representation for an allocation expression. |
e440a328 | 11729 | |
ea664253 | 11730 | Bexpression* |
11731 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11732 | { |
2c809f8f | 11733 | Gogo* gogo = context->gogo(); |
11734 | Location loc = this->location(); | |
ea664253 | 11735 | Bexpression* space = |
11736 | gogo->allocate_memory(this->type_, loc)->get_backend(context); | |
2c809f8f | 11737 | Btype* pbtype = gogo->backend()->pointer_type(this->type_->get_backend(gogo)); |
ea664253 | 11738 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 11739 | } |
11740 | ||
d751bb78 | 11741 | // Dump ast representation for an allocation expression. |
11742 | ||
11743 | void | |
11744 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11745 | const | |
11746 | { | |
11747 | ast_dump_context->ostream() << "new("; | |
11748 | ast_dump_context->dump_type(this->type_); | |
11749 | ast_dump_context->ostream() << ")"; | |
11750 | } | |
11751 | ||
e440a328 | 11752 | // Make an allocation expression. |
11753 | ||
11754 | Expression* | |
b13c66cd | 11755 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 11756 | { |
11757 | return new Allocation_expression(type, location); | |
11758 | } | |
11759 | ||
e440a328 | 11760 | // Construct a struct. |
11761 | ||
11762 | class Struct_construction_expression : public Expression | |
11763 | { | |
11764 | public: | |
11765 | Struct_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 11766 | Location location) |
e440a328 | 11767 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), |
0c4f5a19 | 11768 | type_(type), vals_(vals), traverse_order_(NULL) |
e440a328 | 11769 | { } |
11770 | ||
0c4f5a19 | 11771 | // Set the traversal order, used to ensure that we implement the |
11772 | // order of evaluation rules. Takes ownership of the argument. | |
11773 | void | |
11774 | set_traverse_order(std::vector<int>* traverse_order) | |
11775 | { this->traverse_order_ = traverse_order; } | |
11776 | ||
e440a328 | 11777 | // Return whether this is a constant initializer. |
11778 | bool | |
11779 | is_constant_struct() const; | |
11780 | ||
11781 | protected: | |
11782 | int | |
11783 | do_traverse(Traverse* traverse); | |
11784 | ||
f9ca30f9 | 11785 | bool |
11786 | do_is_immutable() const; | |
11787 | ||
e440a328 | 11788 | Type* |
11789 | do_type() | |
11790 | { return this->type_; } | |
11791 | ||
11792 | void | |
11793 | do_determine_type(const Type_context*); | |
11794 | ||
11795 | void | |
11796 | do_check_types(Gogo*); | |
11797 | ||
11798 | Expression* | |
11799 | do_copy() | |
11800 | { | |
0c4f5a19 | 11801 | Struct_construction_expression* ret = |
11802 | new Struct_construction_expression(this->type_, this->vals_->copy(), | |
11803 | this->location()); | |
11804 | if (this->traverse_order_ != NULL) | |
11805 | ret->set_traverse_order(this->traverse_order_); | |
11806 | return ret; | |
e440a328 | 11807 | } |
11808 | ||
ea664253 | 11809 | Bexpression* |
11810 | do_get_backend(Translate_context*); | |
e440a328 | 11811 | |
11812 | void | |
11813 | do_export(Export*) const; | |
11814 | ||
d751bb78 | 11815 | void |
11816 | do_dump_expression(Ast_dump_context*) const; | |
11817 | ||
e440a328 | 11818 | private: |
11819 | // The type of the struct to construct. | |
11820 | Type* type_; | |
11821 | // The list of values, in order of the fields in the struct. A NULL | |
11822 | // entry means that the field should be zero-initialized. | |
11823 | Expression_list* vals_; | |
0c4f5a19 | 11824 | // If not NULL, the order in which to traverse vals_. This is used |
11825 | // so that we implement the order of evaluation rules correctly. | |
11826 | std::vector<int>* traverse_order_; | |
e440a328 | 11827 | }; |
11828 | ||
11829 | // Traversal. | |
11830 | ||
11831 | int | |
11832 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
11833 | { | |
0c4f5a19 | 11834 | if (this->vals_ != NULL) |
11835 | { | |
11836 | if (this->traverse_order_ == NULL) | |
11837 | { | |
11838 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11839 | return TRAVERSE_EXIT; | |
11840 | } | |
11841 | else | |
11842 | { | |
11843 | for (std::vector<int>::const_iterator p = | |
11844 | this->traverse_order_->begin(); | |
11845 | p != this->traverse_order_->end(); | |
11846 | ++p) | |
11847 | { | |
11848 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
11849 | == TRAVERSE_EXIT) | |
11850 | return TRAVERSE_EXIT; | |
11851 | } | |
11852 | } | |
11853 | } | |
e440a328 | 11854 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
11855 | return TRAVERSE_EXIT; | |
11856 | return TRAVERSE_CONTINUE; | |
11857 | } | |
11858 | ||
11859 | // Return whether this is a constant initializer. | |
11860 | ||
11861 | bool | |
11862 | Struct_construction_expression::is_constant_struct() const | |
11863 | { | |
11864 | if (this->vals_ == NULL) | |
11865 | return true; | |
11866 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11867 | pv != this->vals_->end(); | |
11868 | ++pv) | |
11869 | { | |
11870 | if (*pv != NULL | |
11871 | && !(*pv)->is_constant() | |
11872 | && (!(*pv)->is_composite_literal() | |
11873 | || (*pv)->is_nonconstant_composite_literal())) | |
11874 | return false; | |
11875 | } | |
11876 | ||
11877 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11878 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11879 | pf != fields->end(); | |
11880 | ++pf) | |
11881 | { | |
11882 | // There are no constant constructors for interfaces. | |
11883 | if (pf->type()->interface_type() != NULL) | |
11884 | return false; | |
11885 | } | |
11886 | ||
11887 | return true; | |
11888 | } | |
11889 | ||
f9ca30f9 | 11890 | // Return whether this struct is immutable. |
11891 | ||
11892 | bool | |
11893 | Struct_construction_expression::do_is_immutable() const | |
11894 | { | |
11895 | if (this->vals_ == NULL) | |
11896 | return true; | |
11897 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11898 | pv != this->vals_->end(); | |
11899 | ++pv) | |
11900 | { | |
11901 | if (*pv != NULL && !(*pv)->is_immutable()) | |
11902 | return false; | |
11903 | } | |
11904 | return true; | |
11905 | } | |
11906 | ||
e440a328 | 11907 | // Final type determination. |
11908 | ||
11909 | void | |
11910 | Struct_construction_expression::do_determine_type(const Type_context*) | |
11911 | { | |
11912 | if (this->vals_ == NULL) | |
11913 | return; | |
11914 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11915 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11916 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11917 | pf != fields->end(); | |
11918 | ++pf, ++pv) | |
11919 | { | |
11920 | if (pv == this->vals_->end()) | |
11921 | return; | |
11922 | if (*pv != NULL) | |
11923 | { | |
11924 | Type_context subcontext(pf->type(), false); | |
11925 | (*pv)->determine_type(&subcontext); | |
11926 | } | |
11927 | } | |
a6cb4c0e | 11928 | // Extra values are an error we will report elsewhere; we still want |
11929 | // to determine the type to avoid knockon errors. | |
11930 | for (; pv != this->vals_->end(); ++pv) | |
11931 | (*pv)->determine_type_no_context(); | |
e440a328 | 11932 | } |
11933 | ||
11934 | // Check types. | |
11935 | ||
11936 | void | |
11937 | Struct_construction_expression::do_check_types(Gogo*) | |
11938 | { | |
11939 | if (this->vals_ == NULL) | |
11940 | return; | |
11941 | ||
11942 | Struct_type* st = this->type_->struct_type(); | |
11943 | if (this->vals_->size() > st->field_count()) | |
11944 | { | |
11945 | this->report_error(_("too many expressions for struct")); | |
11946 | return; | |
11947 | } | |
11948 | ||
11949 | const Struct_field_list* fields = st->fields(); | |
11950 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11951 | int i = 0; | |
11952 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11953 | pf != fields->end(); | |
11954 | ++pf, ++pv, ++i) | |
11955 | { | |
11956 | if (pv == this->vals_->end()) | |
11957 | { | |
11958 | this->report_error(_("too few expressions for struct")); | |
11959 | break; | |
11960 | } | |
11961 | ||
11962 | if (*pv == NULL) | |
11963 | continue; | |
11964 | ||
11965 | std::string reason; | |
11966 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
11967 | { | |
11968 | if (reason.empty()) | |
11969 | error_at((*pv)->location(), | |
11970 | "incompatible type for field %d in struct construction", | |
11971 | i + 1); | |
11972 | else | |
11973 | error_at((*pv)->location(), | |
11974 | ("incompatible type for field %d in " | |
11975 | "struct construction (%s)"), | |
11976 | i + 1, reason.c_str()); | |
11977 | this->set_is_error(); | |
11978 | } | |
11979 | } | |
c484d925 | 11980 | go_assert(pv == this->vals_->end()); |
e440a328 | 11981 | } |
11982 | ||
ea664253 | 11983 | // Return the backend representation for constructing a struct. |
e440a328 | 11984 | |
ea664253 | 11985 | Bexpression* |
11986 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11987 | { |
11988 | Gogo* gogo = context->gogo(); | |
11989 | ||
2c809f8f | 11990 | Btype* btype = this->type_->get_backend(gogo); |
e440a328 | 11991 | if (this->vals_ == NULL) |
ea664253 | 11992 | return gogo->backend()->zero_expression(btype); |
e440a328 | 11993 | |
e440a328 | 11994 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e440a328 | 11995 | Expression_list::const_iterator pv = this->vals_->begin(); |
2c809f8f | 11996 | std::vector<Bexpression*> init; |
11997 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11998 | pf != fields->end(); | |
11999 | ++pf) | |
e440a328 | 12000 | { |
63697958 | 12001 | Btype* fbtype = pf->type()->get_backend(gogo); |
e440a328 | 12002 | if (pv == this->vals_->end()) |
2c809f8f | 12003 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12004 | else if (*pv == NULL) |
12005 | { | |
2c809f8f | 12006 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12007 | ++pv; |
12008 | } | |
12009 | else | |
12010 | { | |
2c809f8f | 12011 | Expression* val = |
12012 | Expression::convert_for_assignment(gogo, pf->type(), | |
12013 | *pv, this->location()); | |
ea664253 | 12014 | init.push_back(val->get_backend(context)); |
e440a328 | 12015 | ++pv; |
12016 | } | |
e440a328 | 12017 | } |
ea664253 | 12018 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12019 | } |
12020 | ||
12021 | // Export a struct construction. | |
12022 | ||
12023 | void | |
12024 | Struct_construction_expression::do_export(Export* exp) const | |
12025 | { | |
12026 | exp->write_c_string("convert("); | |
12027 | exp->write_type(this->type_); | |
12028 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12029 | pv != this->vals_->end(); | |
12030 | ++pv) | |
12031 | { | |
12032 | exp->write_c_string(", "); | |
12033 | if (*pv != NULL) | |
12034 | (*pv)->export_expression(exp); | |
12035 | } | |
12036 | exp->write_c_string(")"); | |
12037 | } | |
12038 | ||
d751bb78 | 12039 | // Dump ast representation of a struct construction expression. |
12040 | ||
12041 | void | |
12042 | Struct_construction_expression::do_dump_expression( | |
12043 | Ast_dump_context* ast_dump_context) const | |
12044 | { | |
d751bb78 | 12045 | ast_dump_context->dump_type(this->type_); |
12046 | ast_dump_context->ostream() << "{"; | |
12047 | ast_dump_context->dump_expression_list(this->vals_); | |
12048 | ast_dump_context->ostream() << "}"; | |
12049 | } | |
12050 | ||
e440a328 | 12051 | // Make a struct composite literal. This used by the thunk code. |
12052 | ||
12053 | Expression* | |
12054 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12055 | Location location) |
e440a328 | 12056 | { |
c484d925 | 12057 | go_assert(type->struct_type() != NULL); |
e440a328 | 12058 | return new Struct_construction_expression(type, vals, location); |
12059 | } | |
12060 | ||
12061 | // Construct an array. This class is not used directly; instead we | |
12062 | // use the child classes, Fixed_array_construction_expression and | |
2c809f8f | 12063 | // Slice_construction_expression. |
e440a328 | 12064 | |
12065 | class Array_construction_expression : public Expression | |
12066 | { | |
12067 | protected: | |
12068 | Array_construction_expression(Expression_classification classification, | |
ffe743ca | 12069 | Type* type, |
12070 | const std::vector<unsigned long>* indexes, | |
12071 | Expression_list* vals, Location location) | |
e440a328 | 12072 | : Expression(classification, location), |
ffe743ca | 12073 | type_(type), indexes_(indexes), vals_(vals) |
12074 | { go_assert(indexes == NULL || indexes->size() == vals->size()); } | |
e440a328 | 12075 | |
12076 | public: | |
12077 | // Return whether this is a constant initializer. | |
12078 | bool | |
12079 | is_constant_array() const; | |
12080 | ||
12081 | // Return the number of elements. | |
12082 | size_t | |
12083 | element_count() const | |
12084 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
12085 | ||
12086 | protected: | |
12087 | int | |
12088 | do_traverse(Traverse* traverse); | |
12089 | ||
f9ca30f9 | 12090 | bool |
12091 | do_is_immutable() const; | |
12092 | ||
e440a328 | 12093 | Type* |
12094 | do_type() | |
12095 | { return this->type_; } | |
12096 | ||
12097 | void | |
12098 | do_determine_type(const Type_context*); | |
12099 | ||
12100 | void | |
12101 | do_check_types(Gogo*); | |
12102 | ||
e440a328 | 12103 | void |
12104 | do_export(Export*) const; | |
12105 | ||
ffe743ca | 12106 | // The indexes. |
12107 | const std::vector<unsigned long>* | |
12108 | indexes() | |
12109 | { return this->indexes_; } | |
12110 | ||
e440a328 | 12111 | // The list of values. |
12112 | Expression_list* | |
12113 | vals() | |
12114 | { return this->vals_; } | |
12115 | ||
2c809f8f | 12116 | // Get the backend constructor for the array values. |
12117 | Bexpression* | |
12118 | get_constructor(Translate_context* context, Btype* btype); | |
e440a328 | 12119 | |
d751bb78 | 12120 | void |
12121 | do_dump_expression(Ast_dump_context*) const; | |
12122 | ||
e440a328 | 12123 | private: |
12124 | // The type of the array to construct. | |
12125 | Type* type_; | |
ffe743ca | 12126 | // The list of indexes into the array, one for each value. This may |
12127 | // be NULL, in which case the indexes start at zero and increment. | |
12128 | const std::vector<unsigned long>* indexes_; | |
12129 | // The list of values. This may be NULL if there are no values. | |
e440a328 | 12130 | Expression_list* vals_; |
12131 | }; | |
12132 | ||
12133 | // Traversal. | |
12134 | ||
12135 | int | |
12136 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12137 | { | |
12138 | if (this->vals_ != NULL | |
12139 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12140 | return TRAVERSE_EXIT; | |
12141 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12142 | return TRAVERSE_EXIT; | |
12143 | return TRAVERSE_CONTINUE; | |
12144 | } | |
12145 | ||
12146 | // Return whether this is a constant initializer. | |
12147 | ||
12148 | bool | |
12149 | Array_construction_expression::is_constant_array() const | |
12150 | { | |
12151 | if (this->vals_ == NULL) | |
12152 | return true; | |
12153 | ||
12154 | // There are no constant constructors for interfaces. | |
12155 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12156 | return false; | |
12157 | ||
12158 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12159 | pv != this->vals_->end(); | |
12160 | ++pv) | |
12161 | { | |
12162 | if (*pv != NULL | |
12163 | && !(*pv)->is_constant() | |
12164 | && (!(*pv)->is_composite_literal() | |
12165 | || (*pv)->is_nonconstant_composite_literal())) | |
12166 | return false; | |
12167 | } | |
12168 | return true; | |
12169 | } | |
12170 | ||
f9ca30f9 | 12171 | // Return whether this is an immutable array initializer. |
12172 | ||
12173 | bool | |
12174 | Array_construction_expression::do_is_immutable() const | |
12175 | { | |
12176 | if (this->vals_ == NULL) | |
12177 | return true; | |
12178 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12179 | pv != this->vals_->end(); | |
12180 | ++pv) | |
12181 | { | |
12182 | if (*pv != NULL && !(*pv)->is_immutable()) | |
12183 | return false; | |
12184 | } | |
12185 | return true; | |
12186 | } | |
12187 | ||
e440a328 | 12188 | // Final type determination. |
12189 | ||
12190 | void | |
12191 | Array_construction_expression::do_determine_type(const Type_context*) | |
12192 | { | |
12193 | if (this->vals_ == NULL) | |
12194 | return; | |
12195 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
12196 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12197 | pv != this->vals_->end(); | |
12198 | ++pv) | |
12199 | { | |
12200 | if (*pv != NULL) | |
12201 | (*pv)->determine_type(&subcontext); | |
12202 | } | |
12203 | } | |
12204 | ||
12205 | // Check types. | |
12206 | ||
12207 | void | |
12208 | Array_construction_expression::do_check_types(Gogo*) | |
12209 | { | |
12210 | if (this->vals_ == NULL) | |
12211 | return; | |
12212 | ||
12213 | Array_type* at = this->type_->array_type(); | |
12214 | int i = 0; | |
12215 | Type* element_type = at->element_type(); | |
12216 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12217 | pv != this->vals_->end(); | |
12218 | ++pv, ++i) | |
12219 | { | |
12220 | if (*pv != NULL | |
12221 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12222 | { | |
12223 | error_at((*pv)->location(), | |
12224 | "incompatible type for element %d in composite literal", | |
12225 | i + 1); | |
12226 | this->set_is_error(); | |
12227 | } | |
12228 | } | |
e440a328 | 12229 | } |
12230 | ||
2c809f8f | 12231 | // Get a constructor expression for the array values. |
e440a328 | 12232 | |
2c809f8f | 12233 | Bexpression* |
12234 | Array_construction_expression::get_constructor(Translate_context* context, | |
12235 | Btype* array_btype) | |
e440a328 | 12236 | { |
e440a328 | 12237 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12238 | |
12239 | std::vector<unsigned long> indexes; | |
12240 | std::vector<Bexpression*> vals; | |
12241 | Gogo* gogo = context->gogo(); | |
e440a328 | 12242 | if (this->vals_ != NULL) |
12243 | { | |
12244 | size_t i = 0; | |
ffe743ca | 12245 | std::vector<unsigned long>::const_iterator pi; |
12246 | if (this->indexes_ != NULL) | |
12247 | pi = this->indexes_->begin(); | |
e440a328 | 12248 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12249 | pv != this->vals_->end(); | |
12250 | ++pv, ++i) | |
12251 | { | |
ffe743ca | 12252 | if (this->indexes_ != NULL) |
12253 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12254 | |
12255 | if (this->indexes_ == NULL) | |
2c809f8f | 12256 | indexes.push_back(i); |
ffe743ca | 12257 | else |
2c809f8f | 12258 | indexes.push_back(*pi); |
e440a328 | 12259 | if (*pv == NULL) |
63697958 | 12260 | { |
63697958 | 12261 | Btype* ebtype = element_type->get_backend(gogo); |
12262 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12263 | vals.push_back(zv); |
63697958 | 12264 | } |
e440a328 | 12265 | else |
12266 | { | |
2c809f8f | 12267 | Expression* val_expr = |
12268 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12269 | this->location()); | |
ea664253 | 12270 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12271 | } |
ffe743ca | 12272 | if (this->indexes_ != NULL) |
12273 | ++pi; | |
e440a328 | 12274 | } |
ffe743ca | 12275 | if (this->indexes_ != NULL) |
12276 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12277 | } |
2c809f8f | 12278 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12279 | vals, this->location()); | |
e440a328 | 12280 | } |
12281 | ||
12282 | // Export an array construction. | |
12283 | ||
12284 | void | |
12285 | Array_construction_expression::do_export(Export* exp) const | |
12286 | { | |
12287 | exp->write_c_string("convert("); | |
12288 | exp->write_type(this->type_); | |
12289 | if (this->vals_ != NULL) | |
12290 | { | |
ffe743ca | 12291 | std::vector<unsigned long>::const_iterator pi; |
12292 | if (this->indexes_ != NULL) | |
12293 | pi = this->indexes_->begin(); | |
e440a328 | 12294 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12295 | pv != this->vals_->end(); | |
12296 | ++pv) | |
12297 | { | |
12298 | exp->write_c_string(", "); | |
ffe743ca | 12299 | |
12300 | if (this->indexes_ != NULL) | |
12301 | { | |
12302 | char buf[100]; | |
12303 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12304 | exp->write_c_string(buf); | |
12305 | exp->write_c_string(":"); | |
12306 | } | |
12307 | ||
e440a328 | 12308 | if (*pv != NULL) |
12309 | (*pv)->export_expression(exp); | |
ffe743ca | 12310 | |
12311 | if (this->indexes_ != NULL) | |
12312 | ++pi; | |
e440a328 | 12313 | } |
12314 | } | |
12315 | exp->write_c_string(")"); | |
12316 | } | |
12317 | ||
d751bb78 | 12318 | // Dump ast representation of an array construction expressin. |
12319 | ||
12320 | void | |
12321 | Array_construction_expression::do_dump_expression( | |
12322 | Ast_dump_context* ast_dump_context) const | |
12323 | { | |
ffe743ca | 12324 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12325 | |
12326 | ast_dump_context->ostream() << "[" ; | |
12327 | if (length != NULL) | |
12328 | { | |
12329 | ast_dump_context->dump_expression(length); | |
12330 | } | |
12331 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12332 | ast_dump_context->dump_type(this->type_); |
12333 | ast_dump_context->ostream() << "{" ; | |
ffe743ca | 12334 | if (this->indexes_ == NULL) |
12335 | ast_dump_context->dump_expression_list(this->vals_); | |
12336 | else | |
12337 | { | |
12338 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12339 | for (std::vector<unsigned long>::const_iterator pi = | |
12340 | this->indexes_->begin(); | |
12341 | pi != this->indexes_->end(); | |
12342 | ++pi, ++pv) | |
12343 | { | |
12344 | if (pi != this->indexes_->begin()) | |
12345 | ast_dump_context->ostream() << ", "; | |
12346 | ast_dump_context->ostream() << *pi << ':'; | |
12347 | ast_dump_context->dump_expression(*pv); | |
12348 | } | |
12349 | } | |
d751bb78 | 12350 | ast_dump_context->ostream() << "}" ; |
12351 | ||
12352 | } | |
12353 | ||
e440a328 | 12354 | // Construct a fixed array. |
12355 | ||
12356 | class Fixed_array_construction_expression : | |
12357 | public Array_construction_expression | |
12358 | { | |
12359 | public: | |
ffe743ca | 12360 | Fixed_array_construction_expression(Type* type, |
12361 | const std::vector<unsigned long>* indexes, | |
12362 | Expression_list* vals, Location location) | |
e440a328 | 12363 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, |
ffe743ca | 12364 | type, indexes, vals, location) |
12365 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12366 | |
12367 | protected: | |
12368 | Expression* | |
12369 | do_copy() | |
12370 | { | |
12371 | return new Fixed_array_construction_expression(this->type(), | |
ffe743ca | 12372 | this->indexes(), |
e440a328 | 12373 | (this->vals() == NULL |
12374 | ? NULL | |
12375 | : this->vals()->copy()), | |
12376 | this->location()); | |
12377 | } | |
12378 | ||
ea664253 | 12379 | Bexpression* |
12380 | do_get_backend(Translate_context*); | |
e440a328 | 12381 | }; |
12382 | ||
ea664253 | 12383 | // Return the backend representation for constructing a fixed array. |
e440a328 | 12384 | |
ea664253 | 12385 | Bexpression* |
12386 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12387 | { |
9f0e0513 | 12388 | Type* type = this->type(); |
12389 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 12390 | return this->get_constructor(context, btype); |
e440a328 | 12391 | } |
12392 | ||
76f85fd6 | 12393 | Expression* |
12394 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
12395 | Location location) | |
12396 | { | |
12397 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
12398 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
12399 | } | |
12400 | ||
2c809f8f | 12401 | // Construct a slice. |
e440a328 | 12402 | |
2c809f8f | 12403 | class Slice_construction_expression : public Array_construction_expression |
e440a328 | 12404 | { |
12405 | public: | |
2c809f8f | 12406 | Slice_construction_expression(Type* type, |
12407 | const std::vector<unsigned long>* indexes, | |
12408 | Expression_list* vals, Location location) | |
12409 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
12410 | type, indexes, vals, location), | |
12411 | valtype_(NULL) | |
ffe743ca | 12412 | { go_assert(type->is_slice_type()); } |
e440a328 | 12413 | |
12414 | protected: | |
2c809f8f | 12415 | // Note that taking the address of a slice literal is invalid. |
e440a328 | 12416 | |
12417 | Expression* | |
12418 | do_copy() | |
12419 | { | |
2c809f8f | 12420 | return new Slice_construction_expression(this->type(), this->indexes(), |
12421 | (this->vals() == NULL | |
12422 | ? NULL | |
12423 | : this->vals()->copy()), | |
12424 | this->location()); | |
e440a328 | 12425 | } |
12426 | ||
ea664253 | 12427 | Bexpression* |
12428 | do_get_backend(Translate_context*); | |
2c809f8f | 12429 | |
12430 | private: | |
12431 | // The type of the values in this slice. | |
12432 | Type* valtype_; | |
e440a328 | 12433 | }; |
12434 | ||
ea664253 | 12435 | // Return the backend representation for constructing a slice. |
e440a328 | 12436 | |
ea664253 | 12437 | Bexpression* |
12438 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12439 | { |
f9c68f17 | 12440 | Array_type* array_type = this->type()->array_type(); |
12441 | if (array_type == NULL) | |
12442 | { | |
c484d925 | 12443 | go_assert(this->type()->is_error()); |
ea664253 | 12444 | return context->backend()->error_expression(); |
f9c68f17 | 12445 | } |
12446 | ||
f23d7786 | 12447 | Location loc = this->location(); |
f9c68f17 | 12448 | Type* element_type = array_type->element_type(); |
2c809f8f | 12449 | if (this->valtype_ == NULL) |
12450 | { | |
12451 | mpz_t lenval; | |
12452 | Expression* length; | |
12453 | if (this->vals() == NULL || this->vals()->empty()) | |
12454 | mpz_init_set_ui(lenval, 0); | |
12455 | else | |
12456 | { | |
12457 | if (this->indexes() == NULL) | |
12458 | mpz_init_set_ui(lenval, this->vals()->size()); | |
12459 | else | |
12460 | mpz_init_set_ui(lenval, this->indexes()->back() + 1); | |
12461 | } | |
2c809f8f | 12462 | Type* int_type = Type::lookup_integer_type("int"); |
12463 | length = Expression::make_integer(&lenval, int_type, loc); | |
12464 | mpz_clear(lenval); | |
12465 | this->valtype_ = Type::make_array_type(element_type, length); | |
12466 | } | |
3d60812e | 12467 | |
f23d7786 | 12468 | Expression_list* vals = this->vals(); |
e440a328 | 12469 | if (this->vals() == NULL || this->vals()->empty()) |
12470 | { | |
f23d7786 | 12471 | // We need to create a unique value for the empty array literal. |
12472 | vals = new Expression_list; | |
12473 | vals->push_back(NULL); | |
e440a328 | 12474 | } |
f23d7786 | 12475 | Expression* array_val = |
12476 | new Fixed_array_construction_expression(this->valtype_, this->indexes(), | |
12477 | vals, loc); | |
e440a328 | 12478 | |
f23d7786 | 12479 | bool is_constant_initializer = array_val->is_immutable(); |
d8829beb | 12480 | |
12481 | // We have to copy the initial values into heap memory if we are in | |
12482 | // a function or if the values are not constants. We also have to | |
12483 | // copy them if they may contain pointers in a non-constant context, | |
12484 | // as otherwise the garbage collector won't see them. | |
12485 | bool copy_to_heap = (context->function() != NULL | |
12486 | || !is_constant_initializer | |
12487 | || (element_type->has_pointer() | |
12488 | && !context->is_const())); | |
e440a328 | 12489 | |
f23d7786 | 12490 | Expression* space; |
d8829beb | 12491 | if (!copy_to_heap) |
e440a328 | 12492 | { |
f23d7786 | 12493 | // The initializer will only run once. |
12494 | space = Expression::make_unary(OPERATOR_AND, array_val, loc); | |
12495 | space->unary_expression()->set_is_slice_init(); | |
e440a328 | 12496 | } |
12497 | else | |
f23d7786 | 12498 | space = Expression::make_heap_expression(array_val, loc); |
e440a328 | 12499 | |
2c809f8f | 12500 | // Build a constructor for the slice. |
e440a328 | 12501 | |
f23d7786 | 12502 | Expression* len = this->valtype_->array_type()->length(); |
12503 | Expression* slice_val = | |
12504 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 12505 | return slice_val->get_backend(context); |
e440a328 | 12506 | } |
12507 | ||
12508 | // Make a slice composite literal. This is used by the type | |
12509 | // descriptor code. | |
12510 | ||
12511 | Expression* | |
12512 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12513 | Location location) |
e440a328 | 12514 | { |
411eb89e | 12515 | go_assert(type->is_slice_type()); |
2c809f8f | 12516 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 12517 | } |
12518 | ||
12519 | // Construct a map. | |
12520 | ||
12521 | class Map_construction_expression : public Expression | |
12522 | { | |
12523 | public: | |
12524 | Map_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 12525 | Location location) |
e440a328 | 12526 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), |
2c809f8f | 12527 | type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL) |
c484d925 | 12528 | { go_assert(vals == NULL || vals->size() % 2 == 0); } |
e440a328 | 12529 | |
12530 | protected: | |
12531 | int | |
12532 | do_traverse(Traverse* traverse); | |
12533 | ||
2c809f8f | 12534 | Expression* |
12535 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
12536 | ||
e440a328 | 12537 | Type* |
12538 | do_type() | |
12539 | { return this->type_; } | |
12540 | ||
12541 | void | |
12542 | do_determine_type(const Type_context*); | |
12543 | ||
12544 | void | |
12545 | do_check_types(Gogo*); | |
12546 | ||
12547 | Expression* | |
12548 | do_copy() | |
12549 | { | |
12550 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
12551 | this->location()); | |
12552 | } | |
12553 | ||
ea664253 | 12554 | Bexpression* |
12555 | do_get_backend(Translate_context*); | |
e440a328 | 12556 | |
12557 | void | |
12558 | do_export(Export*) const; | |
12559 | ||
d751bb78 | 12560 | void |
12561 | do_dump_expression(Ast_dump_context*) const; | |
12562 | ||
e440a328 | 12563 | private: |
12564 | // The type of the map to construct. | |
12565 | Type* type_; | |
12566 | // The list of values. | |
12567 | Expression_list* vals_; | |
2c809f8f | 12568 | // The type of the key-value pair struct for each map element. |
12569 | Struct_type* element_type_; | |
12570 | // A temporary reference to the variable storing the constructor initializer. | |
12571 | Temporary_statement* constructor_temp_; | |
e440a328 | 12572 | }; |
12573 | ||
12574 | // Traversal. | |
12575 | ||
12576 | int | |
12577 | Map_construction_expression::do_traverse(Traverse* traverse) | |
12578 | { | |
12579 | if (this->vals_ != NULL | |
12580 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12581 | return TRAVERSE_EXIT; | |
12582 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12583 | return TRAVERSE_EXIT; | |
12584 | return TRAVERSE_CONTINUE; | |
12585 | } | |
12586 | ||
2c809f8f | 12587 | // Flatten constructor initializer into a temporary variable since |
12588 | // we need to take its address for __go_construct_map. | |
12589 | ||
12590 | Expression* | |
12591 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
12592 | Statement_inserter* inserter) | |
12593 | { | |
12594 | if (!this->is_error_expression() | |
12595 | && this->vals_ != NULL | |
12596 | && !this->vals_->empty() | |
12597 | && this->constructor_temp_ == NULL) | |
12598 | { | |
12599 | Map_type* mt = this->type_->map_type(); | |
12600 | Type* key_type = mt->key_type(); | |
12601 | Type* val_type = mt->val_type(); | |
12602 | this->element_type_ = Type::make_builtin_struct_type(2, | |
12603 | "__key", key_type, | |
12604 | "__val", val_type); | |
12605 | ||
12606 | Expression_list* value_pairs = new Expression_list(); | |
12607 | Location loc = this->location(); | |
12608 | ||
12609 | size_t i = 0; | |
12610 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12611 | pv != this->vals_->end(); | |
12612 | ++pv, ++i) | |
12613 | { | |
12614 | Expression_list* key_value_pair = new Expression_list(); | |
12615 | Expression* key = | |
12616 | Expression::convert_for_assignment(gogo, key_type, *pv, loc); | |
12617 | ||
12618 | ++pv; | |
12619 | Expression* val = | |
12620 | Expression::convert_for_assignment(gogo, val_type, *pv, loc); | |
12621 | ||
12622 | key_value_pair->push_back(key); | |
12623 | key_value_pair->push_back(val); | |
12624 | value_pairs->push_back( | |
12625 | Expression::make_struct_composite_literal(this->element_type_, | |
12626 | key_value_pair, loc)); | |
12627 | } | |
12628 | ||
12629 | mpz_t lenval; | |
12630 | mpz_init_set_ui(lenval, i); | |
12631 | Expression* element_count = Expression::make_integer(&lenval, NULL, loc); | |
12632 | mpz_clear(lenval); | |
12633 | ||
12634 | Type* ctor_type = | |
12635 | Type::make_array_type(this->element_type_, element_count); | |
12636 | Expression* constructor = | |
12637 | new Fixed_array_construction_expression(ctor_type, NULL, | |
12638 | value_pairs, loc); | |
12639 | ||
12640 | this->constructor_temp_ = | |
12641 | Statement::make_temporary(NULL, constructor, loc); | |
12642 | constructor->issue_nil_check(); | |
12643 | this->constructor_temp_->set_is_address_taken(); | |
12644 | inserter->insert(this->constructor_temp_); | |
12645 | } | |
12646 | ||
12647 | return this; | |
12648 | } | |
12649 | ||
e440a328 | 12650 | // Final type determination. |
12651 | ||
12652 | void | |
12653 | Map_construction_expression::do_determine_type(const Type_context*) | |
12654 | { | |
12655 | if (this->vals_ == NULL) | |
12656 | return; | |
12657 | ||
12658 | Map_type* mt = this->type_->map_type(); | |
12659 | Type_context key_context(mt->key_type(), false); | |
12660 | Type_context val_context(mt->val_type(), false); | |
12661 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12662 | pv != this->vals_->end(); | |
12663 | ++pv) | |
12664 | { | |
12665 | (*pv)->determine_type(&key_context); | |
12666 | ++pv; | |
12667 | (*pv)->determine_type(&val_context); | |
12668 | } | |
12669 | } | |
12670 | ||
12671 | // Check types. | |
12672 | ||
12673 | void | |
12674 | Map_construction_expression::do_check_types(Gogo*) | |
12675 | { | |
12676 | if (this->vals_ == NULL) | |
12677 | return; | |
12678 | ||
12679 | Map_type* mt = this->type_->map_type(); | |
12680 | int i = 0; | |
12681 | Type* key_type = mt->key_type(); | |
12682 | Type* val_type = mt->val_type(); | |
12683 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12684 | pv != this->vals_->end(); | |
12685 | ++pv, ++i) | |
12686 | { | |
12687 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
12688 | { | |
12689 | error_at((*pv)->location(), | |
12690 | "incompatible type for element %d key in map construction", | |
12691 | i + 1); | |
12692 | this->set_is_error(); | |
12693 | } | |
12694 | ++pv; | |
12695 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
12696 | { | |
12697 | error_at((*pv)->location(), | |
12698 | ("incompatible type for element %d value " | |
12699 | "in map construction"), | |
12700 | i + 1); | |
12701 | this->set_is_error(); | |
12702 | } | |
12703 | } | |
12704 | } | |
12705 | ||
ea664253 | 12706 | // Return the backend representation for constructing a map. |
e440a328 | 12707 | |
ea664253 | 12708 | Bexpression* |
12709 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12710 | { |
2c809f8f | 12711 | if (this->is_error_expression()) |
ea664253 | 12712 | return context->backend()->error_expression(); |
2c809f8f | 12713 | Location loc = this->location(); |
e440a328 | 12714 | |
e440a328 | 12715 | size_t i = 0; |
2c809f8f | 12716 | Expression* ventries; |
e440a328 | 12717 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 12718 | ventries = Expression::make_nil(loc); |
e440a328 | 12719 | else |
12720 | { | |
2c809f8f | 12721 | go_assert(this->constructor_temp_ != NULL); |
12722 | i = this->vals_->size() / 2; | |
e440a328 | 12723 | |
2c809f8f | 12724 | Expression* ctor_ref = |
12725 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
12726 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
12727 | } | |
e440a328 | 12728 | |
2c809f8f | 12729 | Map_type* mt = this->type_->map_type(); |
12730 | if (this->element_type_ == NULL) | |
12731 | this->element_type_ = | |
12732 | Type::make_builtin_struct_type(2, | |
12733 | "__key", mt->key_type(), | |
12734 | "__val", mt->val_type()); | |
12735 | Expression* descriptor = Expression::make_map_descriptor(mt, loc); | |
12736 | ||
12737 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
12738 | mpz_t countval; | |
12739 | mpz_init_set_ui(countval, i); | |
12740 | Expression* count = Expression::make_integer(&countval, uintptr_t, loc); | |
12741 | mpz_clear(countval); | |
12742 | ||
12743 | Expression* entry_size = | |
12744 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
12745 | ||
12746 | unsigned int field_index; | |
12747 | const Struct_field* valfield = | |
12748 | this->element_type_->find_local_field("__val", &field_index); | |
12749 | Expression* val_offset = | |
12750 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
12751 | Expression* val_size = | |
12752 | Expression::make_type_info(mt->val_type(), TYPE_INFO_SIZE); | |
12753 | ||
12754 | Expression* map_ctor = | |
12755 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 6, descriptor, count, | |
12756 | entry_size, val_offset, val_size, ventries); | |
ea664253 | 12757 | return map_ctor->get_backend(context); |
2c809f8f | 12758 | } |
e440a328 | 12759 | |
2c809f8f | 12760 | // Export an array construction. |
e440a328 | 12761 | |
2c809f8f | 12762 | void |
12763 | Map_construction_expression::do_export(Export* exp) const | |
12764 | { | |
12765 | exp->write_c_string("convert("); | |
12766 | exp->write_type(this->type_); | |
12767 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12768 | pv != this->vals_->end(); | |
12769 | ++pv) | |
12770 | { | |
12771 | exp->write_c_string(", "); | |
12772 | (*pv)->export_expression(exp); | |
12773 | } | |
12774 | exp->write_c_string(")"); | |
12775 | } | |
e440a328 | 12776 | |
2c809f8f | 12777 | // Dump ast representation for a map construction expression. |
d751bb78 | 12778 | |
12779 | void | |
12780 | Map_construction_expression::do_dump_expression( | |
12781 | Ast_dump_context* ast_dump_context) const | |
12782 | { | |
d751bb78 | 12783 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 12784 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 12785 | ast_dump_context->ostream() << "}"; |
12786 | } | |
12787 | ||
e440a328 | 12788 | // A general composite literal. This is lowered to a type specific |
12789 | // version. | |
12790 | ||
12791 | class Composite_literal_expression : public Parser_expression | |
12792 | { | |
12793 | public: | |
12794 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
62750cd5 | 12795 | Expression_list* vals, bool all_are_names, |
12796 | Location location) | |
e440a328 | 12797 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), |
62750cd5 | 12798 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys), |
12799 | all_are_names_(all_are_names) | |
e440a328 | 12800 | { } |
12801 | ||
12802 | protected: | |
12803 | int | |
12804 | do_traverse(Traverse* traverse); | |
12805 | ||
12806 | Expression* | |
ceeb4318 | 12807 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12808 | |
12809 | Expression* | |
12810 | do_copy() | |
12811 | { | |
12812 | return new Composite_literal_expression(this->type_, this->depth_, | |
12813 | this->has_keys_, | |
12814 | (this->vals_ == NULL | |
12815 | ? NULL | |
12816 | : this->vals_->copy()), | |
62750cd5 | 12817 | this->all_are_names_, |
e440a328 | 12818 | this->location()); |
12819 | } | |
12820 | ||
d751bb78 | 12821 | void |
12822 | do_dump_expression(Ast_dump_context*) const; | |
12823 | ||
e440a328 | 12824 | private: |
12825 | Expression* | |
81c4b26b | 12826 | lower_struct(Gogo*, Type*); |
e440a328 | 12827 | |
12828 | Expression* | |
113ef6a5 | 12829 | lower_array(Type*); |
e440a328 | 12830 | |
12831 | Expression* | |
ffe743ca | 12832 | make_array(Type*, const std::vector<unsigned long>*, Expression_list*); |
e440a328 | 12833 | |
12834 | Expression* | |
ceeb4318 | 12835 | lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); |
e440a328 | 12836 | |
12837 | // The type of the composite literal. | |
12838 | Type* type_; | |
12839 | // The depth within a list of composite literals within a composite | |
12840 | // literal, when the type is omitted. | |
12841 | int depth_; | |
12842 | // The values to put in the composite literal. | |
12843 | Expression_list* vals_; | |
12844 | // If this is true, then VALS_ is a list of pairs: a key and a | |
12845 | // value. In an array initializer, a missing key will be NULL. | |
12846 | bool has_keys_; | |
62750cd5 | 12847 | // If this is true, then HAS_KEYS_ is true, and every key is a |
12848 | // simple identifier. | |
12849 | bool all_are_names_; | |
e440a328 | 12850 | }; |
12851 | ||
12852 | // Traversal. | |
12853 | ||
12854 | int | |
12855 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
12856 | { | |
dbffccfc | 12857 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 12858 | return TRAVERSE_EXIT; |
dbffccfc | 12859 | |
12860 | // If this is a struct composite literal with keys, then the keys | |
12861 | // are field names, not expressions. We don't want to traverse them | |
12862 | // in that case. If we do, we can give an erroneous error "variable | |
12863 | // initializer refers to itself." See bug482.go in the testsuite. | |
12864 | if (this->has_keys_ && this->vals_ != NULL) | |
12865 | { | |
12866 | // The type may not be resolvable at this point. | |
12867 | Type* type = this->type_; | |
a01f2481 | 12868 | |
12869 | for (int depth = this->depth_; depth > 0; --depth) | |
12870 | { | |
12871 | if (type->array_type() != NULL) | |
12872 | type = type->array_type()->element_type(); | |
12873 | else if (type->map_type() != NULL) | |
12874 | type = type->map_type()->val_type(); | |
12875 | else | |
12876 | { | |
12877 | // This error will be reported during lowering. | |
12878 | return TRAVERSE_CONTINUE; | |
12879 | } | |
12880 | } | |
12881 | ||
dbffccfc | 12882 | while (true) |
12883 | { | |
12884 | if (type->classification() == Type::TYPE_NAMED) | |
12885 | type = type->named_type()->real_type(); | |
12886 | else if (type->classification() == Type::TYPE_FORWARD) | |
12887 | { | |
12888 | Type* t = type->forwarded(); | |
12889 | if (t == type) | |
12890 | break; | |
12891 | type = t; | |
12892 | } | |
12893 | else | |
12894 | break; | |
12895 | } | |
12896 | ||
12897 | if (type->classification() == Type::TYPE_STRUCT) | |
12898 | { | |
12899 | Expression_list::iterator p = this->vals_->begin(); | |
12900 | while (p != this->vals_->end()) | |
12901 | { | |
12902 | // Skip key. | |
12903 | ++p; | |
12904 | go_assert(p != this->vals_->end()); | |
12905 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12906 | return TRAVERSE_EXIT; | |
12907 | ++p; | |
12908 | } | |
12909 | return TRAVERSE_CONTINUE; | |
12910 | } | |
12911 | } | |
12912 | ||
12913 | if (this->vals_ != NULL) | |
12914 | return this->vals_->traverse(traverse); | |
12915 | ||
12916 | return TRAVERSE_CONTINUE; | |
e440a328 | 12917 | } |
12918 | ||
12919 | // Lower a generic composite literal into a specific version based on | |
12920 | // the type. | |
12921 | ||
12922 | Expression* | |
ceeb4318 | 12923 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
12924 | Statement_inserter* inserter, int) | |
e440a328 | 12925 | { |
12926 | Type* type = this->type_; | |
12927 | ||
12928 | for (int depth = this->depth_; depth > 0; --depth) | |
12929 | { | |
12930 | if (type->array_type() != NULL) | |
12931 | type = type->array_type()->element_type(); | |
12932 | else if (type->map_type() != NULL) | |
12933 | type = type->map_type()->val_type(); | |
12934 | else | |
12935 | { | |
5c13bd80 | 12936 | if (!type->is_error()) |
e440a328 | 12937 | error_at(this->location(), |
12938 | ("may only omit types within composite literals " | |
12939 | "of slice, array, or map type")); | |
12940 | return Expression::make_error(this->location()); | |
12941 | } | |
12942 | } | |
12943 | ||
e00772b3 | 12944 | Type *pt = type->points_to(); |
12945 | bool is_pointer = false; | |
12946 | if (pt != NULL) | |
12947 | { | |
12948 | is_pointer = true; | |
12949 | type = pt; | |
12950 | } | |
12951 | ||
12952 | Expression* ret; | |
5c13bd80 | 12953 | if (type->is_error()) |
e440a328 | 12954 | return Expression::make_error(this->location()); |
12955 | else if (type->struct_type() != NULL) | |
e00772b3 | 12956 | ret = this->lower_struct(gogo, type); |
e440a328 | 12957 | else if (type->array_type() != NULL) |
113ef6a5 | 12958 | ret = this->lower_array(type); |
e440a328 | 12959 | else if (type->map_type() != NULL) |
e00772b3 | 12960 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 12961 | else |
12962 | { | |
12963 | error_at(this->location(), | |
12964 | ("expected struct, slice, array, or map type " | |
12965 | "for composite literal")); | |
12966 | return Expression::make_error(this->location()); | |
12967 | } | |
e00772b3 | 12968 | |
12969 | if (is_pointer) | |
2c809f8f | 12970 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 12971 | |
12972 | return ret; | |
e440a328 | 12973 | } |
12974 | ||
12975 | // Lower a struct composite literal. | |
12976 | ||
12977 | Expression* | |
81c4b26b | 12978 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 12979 | { |
b13c66cd | 12980 | Location location = this->location(); |
e440a328 | 12981 | Struct_type* st = type->struct_type(); |
12982 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 12983 | { |
e6013c28 | 12984 | if (this->vals_ != NULL |
12985 | && !this->vals_->empty() | |
12986 | && type->named_type() != NULL | |
12987 | && type->named_type()->named_object()->package() != NULL) | |
12988 | { | |
12989 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
12990 | pf != st->fields()->end(); | |
12991 | ++pf) | |
07daa4e7 | 12992 | { |
e6013c28 | 12993 | if (Gogo::is_hidden_name(pf->field_name())) |
07daa4e7 | 12994 | error_at(this->location(), |
e6013c28 | 12995 | "assignment of unexported field %qs in %qs literal", |
12996 | Gogo::message_name(pf->field_name()).c_str(), | |
12997 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12998 | } |
12999 | } | |
13000 | ||
13001 | return new Struct_construction_expression(type, this->vals_, location); | |
13002 | } | |
e440a328 | 13003 | |
13004 | size_t field_count = st->field_count(); | |
13005 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 13006 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 13007 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13008 | Expression* external_expr = NULL; |
13009 | const Named_object* external_no = NULL; | |
e440a328 | 13010 | while (p != this->vals_->end()) |
13011 | { | |
13012 | Expression* name_expr = *p; | |
13013 | ||
13014 | ++p; | |
c484d925 | 13015 | go_assert(p != this->vals_->end()); |
e440a328 | 13016 | Expression* val = *p; |
13017 | ||
13018 | ++p; | |
13019 | ||
13020 | if (name_expr == NULL) | |
13021 | { | |
13022 | error_at(val->location(), "mixture of field and value initializers"); | |
13023 | return Expression::make_error(location); | |
13024 | } | |
13025 | ||
13026 | bool bad_key = false; | |
13027 | std::string name; | |
81c4b26b | 13028 | const Named_object* no = NULL; |
e440a328 | 13029 | switch (name_expr->classification()) |
13030 | { | |
13031 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13032 | name = name_expr->unknown_expression()->name(); | |
13033 | break; | |
13034 | ||
13035 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13036 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13037 | break; |
13038 | ||
13039 | case EXPRESSION_TYPE: | |
13040 | { | |
13041 | Type* t = name_expr->type(); | |
13042 | Named_type* nt = t->named_type(); | |
13043 | if (nt == NULL) | |
13044 | bad_key = true; | |
13045 | else | |
81c4b26b | 13046 | no = nt->named_object(); |
e440a328 | 13047 | } |
13048 | break; | |
13049 | ||
13050 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13051 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13052 | break; |
13053 | ||
13054 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 13055 | no = name_expr->func_expression()->named_object(); |
e440a328 | 13056 | break; |
13057 | ||
13058 | case EXPRESSION_UNARY: | |
13059 | // If there is a local variable around with the same name as | |
13060 | // the field, and this occurs in the closure, then the | |
13061 | // parser may turn the field reference into an indirection | |
13062 | // through the closure. FIXME: This is a mess. | |
13063 | { | |
13064 | bad_key = true; | |
13065 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
13066 | if (ue->op() == OPERATOR_MULT) | |
13067 | { | |
13068 | Field_reference_expression* fre = | |
13069 | ue->operand()->field_reference_expression(); | |
13070 | if (fre != NULL) | |
13071 | { | |
13072 | Struct_type* st = | |
13073 | fre->expr()->type()->deref()->struct_type(); | |
13074 | if (st != NULL) | |
13075 | { | |
13076 | const Struct_field* sf = st->field(fre->field_index()); | |
13077 | name = sf->field_name(); | |
2d29d278 | 13078 | |
13079 | // See below. FIXME. | |
13080 | if (!Gogo::is_hidden_name(name) | |
13081 | && name[0] >= 'a' | |
13082 | && name[0] <= 'z') | |
13083 | { | |
13084 | if (gogo->lookup_global(name.c_str()) != NULL) | |
13085 | name = gogo->pack_hidden_name(name, false); | |
13086 | } | |
13087 | ||
e440a328 | 13088 | char buf[20]; |
13089 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
13090 | size_t buflen = strlen(buf); | |
13091 | if (name.compare(name.length() - buflen, buflen, buf) | |
13092 | == 0) | |
13093 | { | |
13094 | name = name.substr(0, name.length() - buflen); | |
13095 | bad_key = false; | |
13096 | } | |
13097 | } | |
13098 | } | |
13099 | } | |
13100 | } | |
13101 | break; | |
13102 | ||
13103 | default: | |
13104 | bad_key = true; | |
13105 | break; | |
13106 | } | |
13107 | if (bad_key) | |
13108 | { | |
13109 | error_at(name_expr->location(), "expected struct field name"); | |
13110 | return Expression::make_error(location); | |
13111 | } | |
13112 | ||
81c4b26b | 13113 | if (no != NULL) |
13114 | { | |
62750cd5 | 13115 | if (no->package() != NULL && external_expr == NULL) |
13116 | { | |
13117 | external_expr = name_expr; | |
13118 | external_no = no; | |
13119 | } | |
13120 | ||
81c4b26b | 13121 | name = no->name(); |
13122 | ||
13123 | // A predefined name won't be packed. If it starts with a | |
13124 | // lower case letter we need to check for that case, because | |
2d29d278 | 13125 | // the field name will be packed. FIXME. |
81c4b26b | 13126 | if (!Gogo::is_hidden_name(name) |
13127 | && name[0] >= 'a' | |
13128 | && name[0] <= 'z') | |
13129 | { | |
13130 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13131 | if (gno == no) | |
13132 | name = gogo->pack_hidden_name(name, false); | |
13133 | } | |
13134 | } | |
13135 | ||
e440a328 | 13136 | unsigned int index; |
13137 | const Struct_field* sf = st->find_local_field(name, &index); | |
13138 | if (sf == NULL) | |
13139 | { | |
13140 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
13141 | Gogo::message_name(name).c_str(), | |
13142 | (type->named_type() != NULL | |
13143 | ? type->named_type()->message_name().c_str() | |
13144 | : "unnamed struct")); | |
13145 | return Expression::make_error(location); | |
13146 | } | |
13147 | if (vals[index] != NULL) | |
13148 | { | |
13149 | error_at(name_expr->location(), | |
13150 | "duplicate value for field %qs in %qs", | |
13151 | Gogo::message_name(name).c_str(), | |
13152 | (type->named_type() != NULL | |
13153 | ? type->named_type()->message_name().c_str() | |
13154 | : "unnamed struct")); | |
13155 | return Expression::make_error(location); | |
13156 | } | |
13157 | ||
07daa4e7 | 13158 | if (type->named_type() != NULL |
13159 | && type->named_type()->named_object()->package() != NULL | |
13160 | && Gogo::is_hidden_name(sf->field_name())) | |
13161 | error_at(name_expr->location(), | |
13162 | "assignment of unexported field %qs in %qs literal", | |
13163 | Gogo::message_name(sf->field_name()).c_str(), | |
13164 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13165 | |
e440a328 | 13166 | vals[index] = val; |
0c4f5a19 | 13167 | traverse_order->push_back(index); |
e440a328 | 13168 | } |
13169 | ||
62750cd5 | 13170 | if (!this->all_are_names_) |
13171 | { | |
13172 | // This is a weird case like bug462 in the testsuite. | |
13173 | if (external_expr == NULL) | |
13174 | error_at(this->location(), "unknown field in %qs literal", | |
13175 | (type->named_type() != NULL | |
13176 | ? type->named_type()->message_name().c_str() | |
13177 | : "unnamed struct")); | |
13178 | else | |
13179 | error_at(external_expr->location(), "unknown field %qs in %qs", | |
13180 | external_no->message_name().c_str(), | |
13181 | (type->named_type() != NULL | |
13182 | ? type->named_type()->message_name().c_str() | |
13183 | : "unnamed struct")); | |
13184 | return Expression::make_error(location); | |
13185 | } | |
13186 | ||
e440a328 | 13187 | Expression_list* list = new Expression_list; |
13188 | list->reserve(field_count); | |
13189 | for (size_t i = 0; i < field_count; ++i) | |
13190 | list->push_back(vals[i]); | |
13191 | ||
0c4f5a19 | 13192 | Struct_construction_expression* ret = |
13193 | new Struct_construction_expression(type, list, location); | |
13194 | ret->set_traverse_order(traverse_order); | |
13195 | return ret; | |
e440a328 | 13196 | } |
13197 | ||
00773463 | 13198 | // Used to sort an index/value array. |
13199 | ||
13200 | class Index_value_compare | |
13201 | { | |
13202 | public: | |
13203 | bool | |
13204 | operator()(const std::pair<unsigned long, Expression*>& a, | |
13205 | const std::pair<unsigned long, Expression*>& b) | |
13206 | { return a.first < b.first; } | |
13207 | }; | |
13208 | ||
e440a328 | 13209 | // Lower an array composite literal. |
13210 | ||
13211 | Expression* | |
113ef6a5 | 13212 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13213 | { |
b13c66cd | 13214 | Location location = this->location(); |
e440a328 | 13215 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13216 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13217 | |
ffe743ca | 13218 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13219 | indexes->reserve(this->vals_->size()); | |
00773463 | 13220 | bool indexes_out_of_order = false; |
ffe743ca | 13221 | Expression_list* vals = new Expression_list(); |
13222 | vals->reserve(this->vals_->size()); | |
e440a328 | 13223 | unsigned long index = 0; |
13224 | Expression_list::const_iterator p = this->vals_->begin(); | |
13225 | while (p != this->vals_->end()) | |
13226 | { | |
13227 | Expression* index_expr = *p; | |
13228 | ||
13229 | ++p; | |
c484d925 | 13230 | go_assert(p != this->vals_->end()); |
e440a328 | 13231 | Expression* val = *p; |
13232 | ||
13233 | ++p; | |
13234 | ||
ffe743ca | 13235 | if (index_expr == NULL) |
13236 | { | |
13237 | if (!indexes->empty()) | |
13238 | indexes->push_back(index); | |
13239 | } | |
13240 | else | |
e440a328 | 13241 | { |
ffe743ca | 13242 | if (indexes->empty() && !vals->empty()) |
13243 | { | |
13244 | for (size_t i = 0; i < vals->size(); ++i) | |
13245 | indexes->push_back(i); | |
13246 | } | |
13247 | ||
0c77715b | 13248 | Numeric_constant nc; |
13249 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13250 | { |
e440a328 | 13251 | error_at(index_expr->location(), |
13252 | "index expression is not integer constant"); | |
13253 | return Expression::make_error(location); | |
13254 | } | |
6f6d9955 | 13255 | |
0c77715b | 13256 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13257 | { |
0c77715b | 13258 | case Numeric_constant::NC_UL_VALID: |
13259 | break; | |
13260 | case Numeric_constant::NC_UL_NOTINT: | |
13261 | error_at(index_expr->location(), | |
13262 | "index expression is not integer constant"); | |
13263 | return Expression::make_error(location); | |
13264 | case Numeric_constant::NC_UL_NEGATIVE: | |
e440a328 | 13265 | error_at(index_expr->location(), "index expression is negative"); |
13266 | return Expression::make_error(location); | |
0c77715b | 13267 | case Numeric_constant::NC_UL_BIG: |
e440a328 | 13268 | error_at(index_expr->location(), "index value overflow"); |
13269 | return Expression::make_error(location); | |
0c77715b | 13270 | default: |
13271 | go_unreachable(); | |
e440a328 | 13272 | } |
6f6d9955 | 13273 | |
13274 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13275 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13276 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13277 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13278 | { |
6f6d9955 | 13279 | error_at(index_expr->location(), "index value overflow"); |
13280 | return Expression::make_error(location); | |
13281 | } | |
13282 | ||
ffe743ca | 13283 | if (std::find(indexes->begin(), indexes->end(), index) |
13284 | != indexes->end()) | |
e440a328 | 13285 | { |
ffe743ca | 13286 | error_at(index_expr->location(), "duplicate value for index %lu", |
e440a328 | 13287 | index); |
13288 | return Expression::make_error(location); | |
13289 | } | |
ffe743ca | 13290 | |
00773463 | 13291 | if (!indexes->empty() && index < indexes->back()) |
13292 | indexes_out_of_order = true; | |
13293 | ||
ffe743ca | 13294 | indexes->push_back(index); |
e440a328 | 13295 | } |
13296 | ||
ffe743ca | 13297 | vals->push_back(val); |
13298 | ||
e440a328 | 13299 | ++index; |
13300 | } | |
13301 | ||
ffe743ca | 13302 | if (indexes->empty()) |
13303 | { | |
13304 | delete indexes; | |
13305 | indexes = NULL; | |
13306 | } | |
e440a328 | 13307 | |
00773463 | 13308 | if (indexes_out_of_order) |
13309 | { | |
13310 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
13311 | ||
13312 | V v; | |
13313 | v.reserve(indexes->size()); | |
13314 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
13315 | for (Expression_list::const_iterator pe = vals->begin(); | |
13316 | pe != vals->end(); | |
13317 | ++pe, ++pi) | |
13318 | v.push_back(std::make_pair(*pi, *pe)); | |
13319 | ||
13320 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
13321 | ||
13322 | delete indexes; | |
13323 | delete vals; | |
13324 | indexes = new std::vector<unsigned long>(); | |
13325 | indexes->reserve(v.size()); | |
13326 | vals = new Expression_list(); | |
13327 | vals->reserve(v.size()); | |
13328 | ||
13329 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13330 | { | |
13331 | indexes->push_back(p->first); | |
13332 | vals->push_back(p->second); | |
13333 | } | |
13334 | } | |
13335 | ||
ffe743ca | 13336 | return this->make_array(type, indexes, vals); |
e440a328 | 13337 | } |
13338 | ||
13339 | // Actually build the array composite literal. This handles | |
13340 | // [...]{...}. | |
13341 | ||
13342 | Expression* | |
ffe743ca | 13343 | Composite_literal_expression::make_array( |
13344 | Type* type, | |
13345 | const std::vector<unsigned long>* indexes, | |
13346 | Expression_list* vals) | |
e440a328 | 13347 | { |
b13c66cd | 13348 | Location location = this->location(); |
e440a328 | 13349 | Array_type* at = type->array_type(); |
ffe743ca | 13350 | |
e440a328 | 13351 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13352 | { | |
ffe743ca | 13353 | size_t size; |
13354 | if (vals == NULL) | |
13355 | size = 0; | |
00773463 | 13356 | else if (indexes != NULL) |
13357 | size = indexes->back() + 1; | |
13358 | else | |
ffe743ca | 13359 | { |
13360 | size = vals->size(); | |
13361 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13362 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13363 | && size >> (it->bits() - 1) != 0) | |
13364 | { | |
13365 | error_at(location, "too many elements in composite literal"); | |
13366 | return Expression::make_error(location); | |
13367 | } | |
13368 | } | |
ffe743ca | 13369 | |
e440a328 | 13370 | mpz_t vlen; |
13371 | mpz_init_set_ui(vlen, size); | |
13372 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
13373 | mpz_clear(vlen); | |
13374 | at = Type::make_array_type(at->element_type(), elen); | |
13375 | type = at; | |
13376 | } | |
ffe743ca | 13377 | else if (at->length() != NULL |
13378 | && !at->length()->is_error_expression() | |
13379 | && this->vals_ != NULL) | |
13380 | { | |
13381 | Numeric_constant nc; | |
13382 | unsigned long val; | |
13383 | if (at->length()->numeric_constant_value(&nc) | |
13384 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13385 | { | |
13386 | if (indexes == NULL) | |
13387 | { | |
13388 | if (this->vals_->size() > val) | |
13389 | { | |
13390 | error_at(location, "too many elements in composite literal"); | |
13391 | return Expression::make_error(location); | |
13392 | } | |
13393 | } | |
13394 | else | |
13395 | { | |
00773463 | 13396 | unsigned long max = indexes->back(); |
ffe743ca | 13397 | if (max >= val) |
13398 | { | |
13399 | error_at(location, | |
13400 | ("some element keys in composite literal " | |
13401 | "are out of range")); | |
13402 | return Expression::make_error(location); | |
13403 | } | |
13404 | } | |
13405 | } | |
13406 | } | |
13407 | ||
e440a328 | 13408 | if (at->length() != NULL) |
ffe743ca | 13409 | return new Fixed_array_construction_expression(type, indexes, vals, |
13410 | location); | |
e440a328 | 13411 | else |
2c809f8f | 13412 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13413 | } |
13414 | ||
13415 | // Lower a map composite literal. | |
13416 | ||
13417 | Expression* | |
a287720d | 13418 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13419 | Statement_inserter* inserter, |
a287720d | 13420 | Type* type) |
e440a328 | 13421 | { |
b13c66cd | 13422 | Location location = this->location(); |
e440a328 | 13423 | if (this->vals_ != NULL) |
13424 | { | |
13425 | if (!this->has_keys_) | |
13426 | { | |
13427 | error_at(location, "map composite literal must have keys"); | |
13428 | return Expression::make_error(location); | |
13429 | } | |
13430 | ||
a287720d | 13431 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13432 | p != this->vals_->end(); |
13433 | p += 2) | |
13434 | { | |
13435 | if (*p == NULL) | |
13436 | { | |
13437 | ++p; | |
13438 | error_at((*p)->location(), | |
13439 | "map composite literal must have keys for every value"); | |
13440 | return Expression::make_error(location); | |
13441 | } | |
a287720d | 13442 | // Make sure we have lowered the key; it may not have been |
13443 | // lowered in order to handle keys for struct composite | |
13444 | // literals. Lower it now to get the right error message. | |
13445 | if ((*p)->unknown_expression() != NULL) | |
13446 | { | |
13447 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13448 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 13449 | go_assert((*p)->is_error_expression()); |
a287720d | 13450 | return Expression::make_error(location); |
13451 | } | |
e440a328 | 13452 | } |
13453 | } | |
13454 | ||
13455 | return new Map_construction_expression(type, this->vals_, location); | |
13456 | } | |
13457 | ||
d751bb78 | 13458 | // Dump ast representation for a composite literal expression. |
13459 | ||
13460 | void | |
13461 | Composite_literal_expression::do_dump_expression( | |
13462 | Ast_dump_context* ast_dump_context) const | |
13463 | { | |
8b1c301d | 13464 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13465 | ast_dump_context->dump_type(this->type_); |
13466 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13467 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13468 | ast_dump_context->ostream() << "})"; |
13469 | } | |
13470 | ||
e440a328 | 13471 | // Make a composite literal expression. |
13472 | ||
13473 | Expression* | |
13474 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 13475 | Expression_list* vals, bool all_are_names, |
b13c66cd | 13476 | Location location) |
e440a328 | 13477 | { |
13478 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 13479 | all_are_names, location); |
e440a328 | 13480 | } |
13481 | ||
13482 | // Return whether this expression is a composite literal. | |
13483 | ||
13484 | bool | |
13485 | Expression::is_composite_literal() const | |
13486 | { | |
13487 | switch (this->classification_) | |
13488 | { | |
13489 | case EXPRESSION_COMPOSITE_LITERAL: | |
13490 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13491 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 13492 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13493 | case EXPRESSION_MAP_CONSTRUCTION: |
13494 | return true; | |
13495 | default: | |
13496 | return false; | |
13497 | } | |
13498 | } | |
13499 | ||
13500 | // Return whether this expression is a composite literal which is not | |
13501 | // constant. | |
13502 | ||
13503 | bool | |
13504 | Expression::is_nonconstant_composite_literal() const | |
13505 | { | |
13506 | switch (this->classification_) | |
13507 | { | |
13508 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13509 | { | |
13510 | const Struct_construction_expression *psce = | |
13511 | static_cast<const Struct_construction_expression*>(this); | |
13512 | return !psce->is_constant_struct(); | |
13513 | } | |
13514 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13515 | { | |
13516 | const Fixed_array_construction_expression *pace = | |
13517 | static_cast<const Fixed_array_construction_expression*>(this); | |
13518 | return !pace->is_constant_array(); | |
13519 | } | |
2c809f8f | 13520 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13521 | { |
2c809f8f | 13522 | const Slice_construction_expression *pace = |
13523 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 13524 | return !pace->is_constant_array(); |
13525 | } | |
13526 | case EXPRESSION_MAP_CONSTRUCTION: | |
13527 | return true; | |
13528 | default: | |
13529 | return false; | |
13530 | } | |
13531 | } | |
13532 | ||
35a54f17 | 13533 | // Return true if this is a variable or temporary_variable. |
13534 | ||
13535 | bool | |
13536 | Expression::is_variable() const | |
13537 | { | |
13538 | switch (this->classification_) | |
13539 | { | |
13540 | case EXPRESSION_VAR_REFERENCE: | |
13541 | case EXPRESSION_TEMPORARY_REFERENCE: | |
13542 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
13543 | return true; | |
13544 | default: | |
13545 | return false; | |
13546 | } | |
13547 | } | |
13548 | ||
e440a328 | 13549 | // Return true if this is a reference to a local variable. |
13550 | ||
13551 | bool | |
13552 | Expression::is_local_variable() const | |
13553 | { | |
13554 | const Var_expression* ve = this->var_expression(); | |
13555 | if (ve == NULL) | |
13556 | return false; | |
13557 | const Named_object* no = ve->named_object(); | |
13558 | return (no->is_result_variable() | |
13559 | || (no->is_variable() && !no->var_value()->is_global())); | |
13560 | } | |
13561 | ||
13562 | // Class Type_guard_expression. | |
13563 | ||
13564 | // Traversal. | |
13565 | ||
13566 | int | |
13567 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13568 | { | |
13569 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13570 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13571 | return TRAVERSE_EXIT; | |
13572 | return TRAVERSE_CONTINUE; | |
13573 | } | |
13574 | ||
2c809f8f | 13575 | Expression* |
13576 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
13577 | Statement_inserter* inserter) | |
13578 | { | |
13579 | if (!this->expr_->is_variable()) | |
13580 | { | |
13581 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
13582 | this->location()); | |
13583 | inserter->insert(temp); | |
13584 | this->expr_ = | |
13585 | Expression::make_temporary_reference(temp, this->location()); | |
13586 | } | |
13587 | return this; | |
13588 | } | |
13589 | ||
e440a328 | 13590 | // Check types of a type guard expression. The expression must have |
13591 | // an interface type, but the actual type conversion is checked at run | |
13592 | // time. | |
13593 | ||
13594 | void | |
13595 | Type_guard_expression::do_check_types(Gogo*) | |
13596 | { | |
e440a328 | 13597 | Type* expr_type = this->expr_->type(); |
7e9da23f | 13598 | if (expr_type->interface_type() == NULL) |
f725ade8 | 13599 | { |
5c13bd80 | 13600 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 13601 | this->report_error(_("type assertion only valid for interface types")); |
13602 | this->set_is_error(); | |
13603 | } | |
e440a328 | 13604 | else if (this->type_->interface_type() == NULL) |
13605 | { | |
13606 | std::string reason; | |
13607 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
13608 | &reason)) | |
13609 | { | |
5c13bd80 | 13610 | if (!this->type_->is_error()) |
e440a328 | 13611 | { |
f725ade8 | 13612 | if (reason.empty()) |
13613 | this->report_error(_("impossible type assertion: " | |
13614 | "type does not implement interface")); | |
13615 | else | |
13616 | error_at(this->location(), | |
13617 | ("impossible type assertion: " | |
13618 | "type does not implement interface (%s)"), | |
13619 | reason.c_str()); | |
e440a328 | 13620 | } |
f725ade8 | 13621 | this->set_is_error(); |
e440a328 | 13622 | } |
13623 | } | |
13624 | } | |
13625 | ||
ea664253 | 13626 | // Return the backend representation for a type guard expression. |
e440a328 | 13627 | |
ea664253 | 13628 | Bexpression* |
13629 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13630 | { |
2c809f8f | 13631 | Expression* conversion; |
7e9da23f | 13632 | if (this->type_->interface_type() != NULL) |
2c809f8f | 13633 | conversion = |
13634 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
13635 | true, this->location()); | |
e440a328 | 13636 | else |
2c809f8f | 13637 | conversion = |
13638 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
13639 | this->expr_, this->location()); | |
13640 | ||
ea664253 | 13641 | return conversion->get_backend(context); |
e440a328 | 13642 | } |
13643 | ||
d751bb78 | 13644 | // Dump ast representation for a type guard expression. |
13645 | ||
13646 | void | |
2c809f8f | 13647 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 13648 | const |
13649 | { | |
13650 | this->expr_->dump_expression(ast_dump_context); | |
13651 | ast_dump_context->ostream() << "."; | |
13652 | ast_dump_context->dump_type(this->type_); | |
13653 | } | |
13654 | ||
e440a328 | 13655 | // Make a type guard expression. |
13656 | ||
13657 | Expression* | |
13658 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 13659 | Location location) |
e440a328 | 13660 | { |
13661 | return new Type_guard_expression(expr, type, location); | |
13662 | } | |
13663 | ||
2c809f8f | 13664 | // Class Heap_expression. |
e440a328 | 13665 | |
2c809f8f | 13666 | // When you take the address of an escaping expression, it is allocated |
e440a328 | 13667 | // on the heap. This class implements that. |
13668 | ||
2c809f8f | 13669 | class Heap_expression : public Expression |
e440a328 | 13670 | { |
13671 | public: | |
2c809f8f | 13672 | Heap_expression(Expression* expr, Location location) |
13673 | : Expression(EXPRESSION_HEAP, location), | |
e440a328 | 13674 | expr_(expr) |
13675 | { } | |
13676 | ||
13677 | protected: | |
13678 | int | |
13679 | do_traverse(Traverse* traverse) | |
13680 | { return Expression::traverse(&this->expr_, traverse); } | |
13681 | ||
13682 | Type* | |
13683 | do_type() | |
13684 | { return Type::make_pointer_type(this->expr_->type()); } | |
13685 | ||
13686 | void | |
13687 | do_determine_type(const Type_context*) | |
13688 | { this->expr_->determine_type_no_context(); } | |
13689 | ||
13690 | Expression* | |
13691 | do_copy() | |
13692 | { | |
2c809f8f | 13693 | return Expression::make_heap_expression(this->expr_->copy(), |
13694 | this->location()); | |
e440a328 | 13695 | } |
13696 | ||
ea664253 | 13697 | Bexpression* |
13698 | do_get_backend(Translate_context*); | |
e440a328 | 13699 | |
13700 | // We only export global objects, and the parser does not generate | |
13701 | // this in global scope. | |
13702 | void | |
13703 | do_export(Export*) const | |
c3e6f413 | 13704 | { go_unreachable(); } |
e440a328 | 13705 | |
d751bb78 | 13706 | void |
13707 | do_dump_expression(Ast_dump_context*) const; | |
13708 | ||
e440a328 | 13709 | private: |
2c809f8f | 13710 | // The expression which is being put on the heap. |
e440a328 | 13711 | Expression* expr_; |
13712 | }; | |
13713 | ||
ea664253 | 13714 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 13715 | |
ea664253 | 13716 | Bexpression* |
13717 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13718 | { |
02c19a1a | 13719 | if (this->expr_->is_error_expression() || this->expr_->type()->is_error()) |
ea664253 | 13720 | return context->backend()->error_expression(); |
2c809f8f | 13721 | |
02c19a1a | 13722 | Location loc = this->location(); |
2c809f8f | 13723 | Gogo* gogo = context->gogo(); |
02c19a1a | 13724 | Btype* btype = this->type()->get_backend(gogo); |
ea664253 | 13725 | Bexpression* space = Expression::make_allocation(this->expr_->type(), |
13726 | loc)->get_backend(context); | |
02c19a1a | 13727 | |
13728 | Bstatement* decl; | |
13729 | Named_object* fn = context->function(); | |
13730 | go_assert(fn != NULL); | |
13731 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
13732 | Bvariable* space_temp = | |
13733 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
13734 | space, true, loc, &decl); | |
13735 | space = gogo->backend()->var_expression(space_temp, loc); | |
9b27b43c | 13736 | Btype* expr_btype = this->expr_->type()->get_backend(gogo); |
13737 | Bexpression* ref = | |
13738 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
02c19a1a | 13739 | |
ea664253 | 13740 | Bexpression* bexpr = this->expr_->get_backend(context); |
02c19a1a | 13741 | Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc); |
13742 | decl = gogo->backend()->compound_statement(decl, assn); | |
13743 | space = gogo->backend()->var_expression(space_temp, loc); | |
ea664253 | 13744 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 13745 | } |
13746 | ||
2c809f8f | 13747 | // Dump ast representation for a heap expression. |
d751bb78 | 13748 | |
13749 | void | |
2c809f8f | 13750 | Heap_expression::do_dump_expression( |
d751bb78 | 13751 | Ast_dump_context* ast_dump_context) const |
13752 | { | |
13753 | ast_dump_context->ostream() << "&("; | |
13754 | ast_dump_context->dump_expression(this->expr_); | |
13755 | ast_dump_context->ostream() << ")"; | |
13756 | } | |
13757 | ||
2c809f8f | 13758 | // Allocate an expression on the heap. |
e440a328 | 13759 | |
13760 | Expression* | |
2c809f8f | 13761 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 13762 | { |
2c809f8f | 13763 | return new Heap_expression(expr, location); |
e440a328 | 13764 | } |
13765 | ||
13766 | // Class Receive_expression. | |
13767 | ||
13768 | // Return the type of a receive expression. | |
13769 | ||
13770 | Type* | |
13771 | Receive_expression::do_type() | |
13772 | { | |
13773 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13774 | if (channel_type == NULL) | |
13775 | return Type::make_error_type(); | |
13776 | return channel_type->element_type(); | |
13777 | } | |
13778 | ||
13779 | // Check types for a receive expression. | |
13780 | ||
13781 | void | |
13782 | Receive_expression::do_check_types(Gogo*) | |
13783 | { | |
13784 | Type* type = this->channel_->type(); | |
5c13bd80 | 13785 | if (type->is_error()) |
e440a328 | 13786 | { |
13787 | this->set_is_error(); | |
13788 | return; | |
13789 | } | |
13790 | if (type->channel_type() == NULL) | |
13791 | { | |
13792 | this->report_error(_("expected channel")); | |
13793 | return; | |
13794 | } | |
13795 | if (!type->channel_type()->may_receive()) | |
13796 | { | |
13797 | this->report_error(_("invalid receive on send-only channel")); | |
13798 | return; | |
13799 | } | |
13800 | } | |
13801 | ||
2c809f8f | 13802 | // Flattening for receive expressions creates a temporary variable to store |
13803 | // received data in for receives. | |
13804 | ||
13805 | Expression* | |
13806 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
13807 | Statement_inserter* inserter) | |
13808 | { | |
13809 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13810 | if (channel_type == NULL) | |
13811 | { | |
13812 | go_assert(saw_errors()); | |
13813 | return this; | |
13814 | } | |
13815 | ||
13816 | Type* element_type = channel_type->element_type(); | |
13817 | if (this->temp_receiver_ == NULL) | |
13818 | { | |
13819 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
13820 | this->location()); | |
13821 | this->temp_receiver_->set_is_address_taken(); | |
13822 | inserter->insert(this->temp_receiver_); | |
13823 | } | |
13824 | ||
13825 | return this; | |
13826 | } | |
13827 | ||
ea664253 | 13828 | // Get the backend representation for a receive expression. |
e440a328 | 13829 | |
ea664253 | 13830 | Bexpression* |
13831 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13832 | { |
f24f10bb | 13833 | Location loc = this->location(); |
13834 | ||
e440a328 | 13835 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 13836 | if (channel_type == NULL) |
13837 | { | |
c484d925 | 13838 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 13839 | return context->backend()->error_expression(); |
5b8368f4 | 13840 | } |
f24f10bb | 13841 | Expression* td = Expression::make_type_descriptor(channel_type, loc); |
e440a328 | 13842 | |
2c809f8f | 13843 | Expression* recv_ref = |
13844 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13845 | Expression* recv_addr = | |
13846 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13847 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
13848 | Expression* recv = | |
13849 | Runtime::make_call(Runtime::RECEIVE, loc, 3, | |
13850 | td, this->channel_, recv_addr); | |
ea664253 | 13851 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 13852 | } |
13853 | ||
d751bb78 | 13854 | // Dump ast representation for a receive expression. |
13855 | ||
13856 | void | |
13857 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13858 | { | |
13859 | ast_dump_context->ostream() << " <- " ; | |
13860 | ast_dump_context->dump_expression(channel_); | |
13861 | } | |
13862 | ||
e440a328 | 13863 | // Make a receive expression. |
13864 | ||
13865 | Receive_expression* | |
b13c66cd | 13866 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 13867 | { |
13868 | return new Receive_expression(channel, location); | |
13869 | } | |
13870 | ||
e440a328 | 13871 | // An expression which evaluates to a pointer to the type descriptor |
13872 | // of a type. | |
13873 | ||
13874 | class Type_descriptor_expression : public Expression | |
13875 | { | |
13876 | public: | |
b13c66cd | 13877 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 13878 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
13879 | type_(type) | |
13880 | { } | |
13881 | ||
13882 | protected: | |
13883 | Type* | |
13884 | do_type() | |
13885 | { return Type::make_type_descriptor_ptr_type(); } | |
13886 | ||
f9ca30f9 | 13887 | bool |
13888 | do_is_immutable() const | |
13889 | { return true; } | |
13890 | ||
e440a328 | 13891 | void |
13892 | do_determine_type(const Type_context*) | |
13893 | { } | |
13894 | ||
13895 | Expression* | |
13896 | do_copy() | |
13897 | { return this; } | |
13898 | ||
ea664253 | 13899 | Bexpression* |
13900 | do_get_backend(Translate_context* context) | |
a1d23b41 | 13901 | { |
ea664253 | 13902 | return this->type_->type_descriptor_pointer(context->gogo(), |
13903 | this->location()); | |
a1d23b41 | 13904 | } |
e440a328 | 13905 | |
d751bb78 | 13906 | void |
13907 | do_dump_expression(Ast_dump_context*) const; | |
13908 | ||
e440a328 | 13909 | private: |
13910 | // The type for which this is the descriptor. | |
13911 | Type* type_; | |
13912 | }; | |
13913 | ||
d751bb78 | 13914 | // Dump ast representation for a type descriptor expression. |
13915 | ||
13916 | void | |
13917 | Type_descriptor_expression::do_dump_expression( | |
13918 | Ast_dump_context* ast_dump_context) const | |
13919 | { | |
13920 | ast_dump_context->dump_type(this->type_); | |
13921 | } | |
13922 | ||
e440a328 | 13923 | // Make a type descriptor expression. |
13924 | ||
13925 | Expression* | |
b13c66cd | 13926 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 13927 | { |
13928 | return new Type_descriptor_expression(type, location); | |
13929 | } | |
13930 | ||
13931 | // An expression which evaluates to some characteristic of a type. | |
13932 | // This is only used to initialize fields of a type descriptor. Using | |
13933 | // a new expression class is slightly inefficient but gives us a good | |
13934 | // separation between the frontend and the middle-end with regard to | |
13935 | // how types are laid out. | |
13936 | ||
13937 | class Type_info_expression : public Expression | |
13938 | { | |
13939 | public: | |
13940 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 13941 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 13942 | type_(type), type_info_(type_info) |
13943 | { } | |
13944 | ||
13945 | protected: | |
0e168074 | 13946 | bool |
13947 | do_is_immutable() const | |
13948 | { return true; } | |
13949 | ||
e440a328 | 13950 | Type* |
13951 | do_type(); | |
13952 | ||
13953 | void | |
13954 | do_determine_type(const Type_context*) | |
13955 | { } | |
13956 | ||
13957 | Expression* | |
13958 | do_copy() | |
13959 | { return this; } | |
13960 | ||
ea664253 | 13961 | Bexpression* |
13962 | do_get_backend(Translate_context* context); | |
e440a328 | 13963 | |
d751bb78 | 13964 | void |
13965 | do_dump_expression(Ast_dump_context*) const; | |
13966 | ||
e440a328 | 13967 | private: |
13968 | // The type for which we are getting information. | |
13969 | Type* type_; | |
13970 | // What information we want. | |
13971 | Type_info type_info_; | |
13972 | }; | |
13973 | ||
13974 | // The type is chosen to match what the type descriptor struct | |
13975 | // expects. | |
13976 | ||
13977 | Type* | |
13978 | Type_info_expression::do_type() | |
13979 | { | |
13980 | switch (this->type_info_) | |
13981 | { | |
13982 | case TYPE_INFO_SIZE: | |
13983 | return Type::lookup_integer_type("uintptr"); | |
13984 | case TYPE_INFO_ALIGNMENT: | |
13985 | case TYPE_INFO_FIELD_ALIGNMENT: | |
13986 | return Type::lookup_integer_type("uint8"); | |
13987 | default: | |
c3e6f413 | 13988 | go_unreachable(); |
e440a328 | 13989 | } |
13990 | } | |
13991 | ||
ea664253 | 13992 | // Return the backend representation for type information. |
e440a328 | 13993 | |
ea664253 | 13994 | Bexpression* |
13995 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13996 | { |
927a01eb | 13997 | Btype* btype = this->type_->get_backend(context->gogo()); |
13998 | Gogo* gogo = context->gogo(); | |
13999 | size_t val; | |
14000 | switch (this->type_info_) | |
e440a328 | 14001 | { |
927a01eb | 14002 | case TYPE_INFO_SIZE: |
14003 | val = gogo->backend()->type_size(btype); | |
14004 | break; | |
14005 | case TYPE_INFO_ALIGNMENT: | |
14006 | val = gogo->backend()->type_alignment(btype); | |
14007 | break; | |
14008 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14009 | val = gogo->backend()->type_field_alignment(btype); | |
14010 | break; | |
14011 | default: | |
14012 | go_unreachable(); | |
e440a328 | 14013 | } |
287cdcf4 | 14014 | mpz_t cst; |
14015 | mpz_init_set_ui(cst, val); | |
14016 | Btype* int_btype = this->type()->get_backend(gogo); | |
14017 | Bexpression* ret = | |
14018 | gogo->backend()->integer_constant_expression(int_btype, cst); | |
14019 | mpz_clear(cst); | |
ea664253 | 14020 | return ret; |
e440a328 | 14021 | } |
14022 | ||
d751bb78 | 14023 | // Dump ast representation for a type info expression. |
14024 | ||
14025 | void | |
14026 | Type_info_expression::do_dump_expression( | |
14027 | Ast_dump_context* ast_dump_context) const | |
14028 | { | |
14029 | ast_dump_context->ostream() << "typeinfo("; | |
14030 | ast_dump_context->dump_type(this->type_); | |
14031 | ast_dump_context->ostream() << ","; | |
14032 | ast_dump_context->ostream() << | |
14033 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14034 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
14035 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
14036 | : "unknown"); | |
14037 | ast_dump_context->ostream() << ")"; | |
14038 | } | |
14039 | ||
e440a328 | 14040 | // Make a type info expression. |
14041 | ||
14042 | Expression* | |
14043 | Expression::make_type_info(Type* type, Type_info type_info) | |
14044 | { | |
14045 | return new Type_info_expression(type, type_info); | |
14046 | } | |
14047 | ||
35a54f17 | 14048 | // An expression that evaluates to some characteristic of a slice. |
14049 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14050 | ||
14051 | class Slice_info_expression : public Expression | |
14052 | { | |
14053 | public: | |
14054 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14055 | Location location) | |
14056 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14057 | slice_(slice), slice_info_(slice_info) | |
14058 | { } | |
14059 | ||
14060 | protected: | |
14061 | Type* | |
14062 | do_type(); | |
14063 | ||
14064 | void | |
14065 | do_determine_type(const Type_context*) | |
14066 | { } | |
14067 | ||
14068 | Expression* | |
14069 | do_copy() | |
14070 | { | |
14071 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14072 | this->location()); | |
14073 | } | |
14074 | ||
ea664253 | 14075 | Bexpression* |
14076 | do_get_backend(Translate_context* context); | |
35a54f17 | 14077 | |
14078 | void | |
14079 | do_dump_expression(Ast_dump_context*) const; | |
14080 | ||
14081 | void | |
14082 | do_issue_nil_check() | |
14083 | { this->slice_->issue_nil_check(); } | |
14084 | ||
14085 | private: | |
14086 | // The slice for which we are getting information. | |
14087 | Expression* slice_; | |
14088 | // What information we want. | |
14089 | Slice_info slice_info_; | |
14090 | }; | |
14091 | ||
14092 | // Return the type of the slice info. | |
14093 | ||
14094 | Type* | |
14095 | Slice_info_expression::do_type() | |
14096 | { | |
14097 | switch (this->slice_info_) | |
14098 | { | |
14099 | case SLICE_INFO_VALUE_POINTER: | |
14100 | return Type::make_pointer_type( | |
14101 | this->slice_->type()->array_type()->element_type()); | |
14102 | case SLICE_INFO_LENGTH: | |
14103 | case SLICE_INFO_CAPACITY: | |
14104 | return Type::lookup_integer_type("int"); | |
14105 | default: | |
14106 | go_unreachable(); | |
14107 | } | |
14108 | } | |
14109 | ||
ea664253 | 14110 | // Return the backend information for slice information. |
35a54f17 | 14111 | |
ea664253 | 14112 | Bexpression* |
14113 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 14114 | { |
14115 | Gogo* gogo = context->gogo(); | |
ea664253 | 14116 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 14117 | switch (this->slice_info_) |
14118 | { | |
14119 | case SLICE_INFO_VALUE_POINTER: | |
14120 | case SLICE_INFO_LENGTH: | |
14121 | case SLICE_INFO_CAPACITY: | |
ea664253 | 14122 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
14123 | this->location()); | |
35a54f17 | 14124 | break; |
14125 | default: | |
14126 | go_unreachable(); | |
14127 | } | |
35a54f17 | 14128 | } |
14129 | ||
14130 | // Dump ast representation for a type info expression. | |
14131 | ||
14132 | void | |
14133 | Slice_info_expression::do_dump_expression( | |
14134 | Ast_dump_context* ast_dump_context) const | |
14135 | { | |
14136 | ast_dump_context->ostream() << "sliceinfo("; | |
14137 | this->slice_->dump_expression(ast_dump_context); | |
14138 | ast_dump_context->ostream() << ","; | |
14139 | ast_dump_context->ostream() << | |
14140 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14141 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14142 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14143 | : "unknown"); | |
14144 | ast_dump_context->ostream() << ")"; | |
14145 | } | |
14146 | ||
14147 | // Make a slice info expression. | |
14148 | ||
14149 | Expression* | |
14150 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14151 | Location location) | |
14152 | { | |
14153 | return new Slice_info_expression(slice, slice_info, location); | |
14154 | } | |
14155 | ||
2c809f8f | 14156 | // An expression that represents a slice value: a struct with value pointer, |
14157 | // length, and capacity fields. | |
14158 | ||
14159 | class Slice_value_expression : public Expression | |
14160 | { | |
14161 | public: | |
14162 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14163 | Expression* cap, Location location) | |
14164 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14165 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14166 | { } | |
14167 | ||
14168 | protected: | |
14169 | int | |
14170 | do_traverse(Traverse*); | |
14171 | ||
14172 | Type* | |
14173 | do_type() | |
14174 | { return this->type_; } | |
14175 | ||
14176 | void | |
14177 | do_determine_type(const Type_context*) | |
14178 | { go_unreachable(); } | |
14179 | ||
14180 | Expression* | |
14181 | do_copy() | |
14182 | { | |
14183 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14184 | this->len_->copy(), this->cap_->copy(), | |
14185 | this->location()); | |
14186 | } | |
14187 | ||
ea664253 | 14188 | Bexpression* |
14189 | do_get_backend(Translate_context* context); | |
2c809f8f | 14190 | |
14191 | void | |
14192 | do_dump_expression(Ast_dump_context*) const; | |
14193 | ||
14194 | private: | |
14195 | // The type of the slice value. | |
14196 | Type* type_; | |
14197 | // The pointer to the values in the slice. | |
14198 | Expression* valptr_; | |
14199 | // The length of the slice. | |
14200 | Expression* len_; | |
14201 | // The capacity of the slice. | |
14202 | Expression* cap_; | |
14203 | }; | |
14204 | ||
14205 | int | |
14206 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14207 | { | |
14208 | if (Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
14209 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT | |
14210 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14211 | return TRAVERSE_EXIT; | |
14212 | return TRAVERSE_CONTINUE; | |
14213 | } | |
14214 | ||
ea664253 | 14215 | Bexpression* |
14216 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14217 | { |
14218 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14219 | vals[0] = this->valptr_->get_backend(context); |
14220 | vals[1] = this->len_->get_backend(context); | |
14221 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14222 | |
14223 | Gogo* gogo = context->gogo(); | |
14224 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14225 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14226 | } |
14227 | ||
14228 | void | |
14229 | Slice_value_expression::do_dump_expression( | |
14230 | Ast_dump_context* ast_dump_context) const | |
14231 | { | |
14232 | ast_dump_context->ostream() << "slicevalue("; | |
14233 | ast_dump_context->ostream() << "values: "; | |
14234 | this->valptr_->dump_expression(ast_dump_context); | |
14235 | ast_dump_context->ostream() << ", length: "; | |
14236 | this->len_->dump_expression(ast_dump_context); | |
14237 | ast_dump_context->ostream() << ", capacity: "; | |
14238 | this->cap_->dump_expression(ast_dump_context); | |
14239 | ast_dump_context->ostream() << ")"; | |
14240 | } | |
14241 | ||
14242 | Expression* | |
14243 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14244 | Expression* cap, Location location) | |
14245 | { | |
14246 | go_assert(at->is_slice_type()); | |
14247 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14248 | } | |
2387f644 | 14249 | |
14250 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14251 | // This is used to access the method table or underlying object of an interface. | |
14252 | ||
14253 | class Interface_info_expression : public Expression | |
14254 | { | |
14255 | public: | |
14256 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 14257 | Location location) |
2387f644 | 14258 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
14259 | iface_(iface), iface_info_(iface_info) | |
14260 | { } | |
14261 | ||
14262 | protected: | |
14263 | Type* | |
14264 | do_type(); | |
14265 | ||
14266 | void | |
14267 | do_determine_type(const Type_context*) | |
14268 | { } | |
14269 | ||
14270 | Expression* | |
14271 | do_copy() | |
14272 | { | |
14273 | return new Interface_info_expression(this->iface_->copy(), | |
14274 | this->iface_info_, this->location()); | |
14275 | } | |
14276 | ||
ea664253 | 14277 | Bexpression* |
14278 | do_get_backend(Translate_context* context); | |
2387f644 | 14279 | |
14280 | void | |
14281 | do_dump_expression(Ast_dump_context*) const; | |
14282 | ||
14283 | void | |
14284 | do_issue_nil_check() | |
14285 | { this->iface_->issue_nil_check(); } | |
14286 | ||
14287 | private: | |
14288 | // The interface for which we are getting information. | |
14289 | Expression* iface_; | |
14290 | // What information we want. | |
14291 | Interface_info iface_info_; | |
14292 | }; | |
14293 | ||
14294 | // Return the type of the interface info. | |
14295 | ||
14296 | Type* | |
14297 | Interface_info_expression::do_type() | |
14298 | { | |
14299 | switch (this->iface_info_) | |
14300 | { | |
14301 | case INTERFACE_INFO_METHODS: | |
14302 | { | |
2c809f8f | 14303 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
14304 | if (this->iface_->type()->interface_type()->is_empty()) | |
14305 | return pdt; | |
14306 | ||
2387f644 | 14307 | Location loc = this->location(); |
14308 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 14309 | sfl->push_back( |
14310 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
14311 | ||
14312 | Interface_type* itype = this->iface_->type()->interface_type(); | |
14313 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); | |
14314 | p != itype->methods()->end(); | |
14315 | ++p) | |
14316 | { | |
14317 | Function_type* ft = p->type()->function_type(); | |
14318 | go_assert(ft->receiver() == NULL); | |
14319 | ||
14320 | const Typed_identifier_list* params = ft->parameters(); | |
14321 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
14322 | if (params != NULL) | |
14323 | mparams->reserve(params->size() + 1); | |
14324 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
14325 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
14326 | if (params != NULL) | |
14327 | { | |
14328 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
14329 | pp != params->end(); | |
14330 | ++pp) | |
14331 | mparams->push_back(*pp); | |
14332 | } | |
14333 | ||
14334 | Typed_identifier_list* mresults = (ft->results() == NULL | |
14335 | ? NULL | |
14336 | : ft->results()->copy()); | |
14337 | Backend_function_type* mft = | |
14338 | Type::make_backend_function_type(NULL, mparams, mresults, | |
14339 | ft->location()); | |
14340 | ||
14341 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
14342 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
14343 | } | |
14344 | ||
14345 | return Type::make_pointer_type(Type::make_struct_type(sfl, loc)); | |
14346 | } | |
14347 | case INTERFACE_INFO_OBJECT: | |
14348 | return Type::make_pointer_type(Type::make_void_type()); | |
14349 | default: | |
14350 | go_unreachable(); | |
14351 | } | |
14352 | } | |
14353 | ||
ea664253 | 14354 | // Return the backend representation for interface information. |
2387f644 | 14355 | |
ea664253 | 14356 | Bexpression* |
14357 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 14358 | { |
14359 | Gogo* gogo = context->gogo(); | |
ea664253 | 14360 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 14361 | switch (this->iface_info_) |
14362 | { | |
14363 | case INTERFACE_INFO_METHODS: | |
14364 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 14365 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
14366 | this->location()); | |
2387f644 | 14367 | break; |
14368 | default: | |
14369 | go_unreachable(); | |
14370 | } | |
2387f644 | 14371 | } |
14372 | ||
14373 | // Dump ast representation for an interface info expression. | |
14374 | ||
14375 | void | |
14376 | Interface_info_expression::do_dump_expression( | |
14377 | Ast_dump_context* ast_dump_context) const | |
14378 | { | |
2c809f8f | 14379 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 14380 | ast_dump_context->ostream() << "interfaceinfo("; |
14381 | this->iface_->dump_expression(ast_dump_context); | |
14382 | ast_dump_context->ostream() << ","; | |
14383 | ast_dump_context->ostream() << | |
2c809f8f | 14384 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
14385 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 14386 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
14387 | : "unknown"); | |
14388 | ast_dump_context->ostream() << ")"; | |
14389 | } | |
14390 | ||
14391 | // Make an interface info expression. | |
14392 | ||
14393 | Expression* | |
14394 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
14395 | Location location) | |
14396 | { | |
14397 | return new Interface_info_expression(iface, iface_info, location); | |
14398 | } | |
14399 | ||
2c809f8f | 14400 | // An expression that represents an interface value. The first field is either |
14401 | // a type descriptor for an empty interface or a pointer to the interface method | |
14402 | // table for a non-empty interface. The second field is always the object. | |
14403 | ||
14404 | class Interface_value_expression : public Expression | |
14405 | { | |
14406 | public: | |
14407 | Interface_value_expression(Type* type, Expression* first_field, | |
14408 | Expression* obj, Location location) | |
14409 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
14410 | type_(type), first_field_(first_field), obj_(obj) | |
14411 | { } | |
14412 | ||
14413 | protected: | |
14414 | int | |
14415 | do_traverse(Traverse*); | |
14416 | ||
14417 | Type* | |
14418 | do_type() | |
14419 | { return this->type_; } | |
14420 | ||
14421 | void | |
14422 | do_determine_type(const Type_context*) | |
14423 | { go_unreachable(); } | |
14424 | ||
14425 | Expression* | |
14426 | do_copy() | |
14427 | { | |
14428 | return new Interface_value_expression(this->type_, | |
14429 | this->first_field_->copy(), | |
14430 | this->obj_->copy(), this->location()); | |
14431 | } | |
14432 | ||
ea664253 | 14433 | Bexpression* |
14434 | do_get_backend(Translate_context* context); | |
2c809f8f | 14435 | |
14436 | void | |
14437 | do_dump_expression(Ast_dump_context*) const; | |
14438 | ||
14439 | private: | |
14440 | // The type of the interface value. | |
14441 | Type* type_; | |
14442 | // The first field of the interface (either a type descriptor or a pointer | |
14443 | // to the method table. | |
14444 | Expression* first_field_; | |
14445 | // The underlying object of the interface. | |
14446 | Expression* obj_; | |
14447 | }; | |
14448 | ||
14449 | int | |
14450 | Interface_value_expression::do_traverse(Traverse* traverse) | |
14451 | { | |
14452 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
14453 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
14454 | return TRAVERSE_EXIT; | |
14455 | return TRAVERSE_CONTINUE; | |
14456 | } | |
14457 | ||
ea664253 | 14458 | Bexpression* |
14459 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14460 | { |
14461 | std::vector<Bexpression*> vals(2); | |
ea664253 | 14462 | vals[0] = this->first_field_->get_backend(context); |
14463 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 14464 | |
14465 | Gogo* gogo = context->gogo(); | |
14466 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14467 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14468 | } |
14469 | ||
14470 | void | |
14471 | Interface_value_expression::do_dump_expression( | |
14472 | Ast_dump_context* ast_dump_context) const | |
14473 | { | |
14474 | ast_dump_context->ostream() << "interfacevalue("; | |
14475 | ast_dump_context->ostream() << | |
14476 | (this->type_->interface_type()->is_empty() | |
14477 | ? "type_descriptor: " | |
14478 | : "methods: "); | |
14479 | this->first_field_->dump_expression(ast_dump_context); | |
14480 | ast_dump_context->ostream() << ", object: "; | |
14481 | this->obj_->dump_expression(ast_dump_context); | |
14482 | ast_dump_context->ostream() << ")"; | |
14483 | } | |
14484 | ||
14485 | Expression* | |
14486 | Expression::make_interface_value(Type* type, Expression* first_value, | |
14487 | Expression* object, Location location) | |
14488 | { | |
14489 | return new Interface_value_expression(type, first_value, object, location); | |
14490 | } | |
14491 | ||
14492 | // An interface method table for a pair of types: an interface type and a type | |
14493 | // that implements that interface. | |
14494 | ||
14495 | class Interface_mtable_expression : public Expression | |
14496 | { | |
14497 | public: | |
14498 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
14499 | bool is_pointer, Location location) | |
14500 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
14501 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
14502 | method_table_type_(NULL), bvar_(NULL) | |
14503 | { } | |
14504 | ||
14505 | protected: | |
14506 | int | |
14507 | do_traverse(Traverse*); | |
14508 | ||
14509 | Type* | |
14510 | do_type(); | |
14511 | ||
14512 | bool | |
14513 | is_immutable() const | |
14514 | { return true; } | |
14515 | ||
14516 | void | |
14517 | do_determine_type(const Type_context*) | |
14518 | { go_unreachable(); } | |
14519 | ||
14520 | Expression* | |
14521 | do_copy() | |
14522 | { | |
14523 | return new Interface_mtable_expression(this->itype_, this->type_, | |
14524 | this->is_pointer_, this->location()); | |
14525 | } | |
14526 | ||
14527 | bool | |
14528 | do_is_addressable() const | |
14529 | { return true; } | |
14530 | ||
ea664253 | 14531 | Bexpression* |
14532 | do_get_backend(Translate_context* context); | |
2c809f8f | 14533 | |
14534 | void | |
14535 | do_dump_expression(Ast_dump_context*) const; | |
14536 | ||
14537 | private: | |
14538 | // The interface type for which the methods are defined. | |
14539 | Interface_type* itype_; | |
14540 | // The type to construct the interface method table for. | |
14541 | Type* type_; | |
14542 | // Whether this table contains the method set for the receiver type or the | |
14543 | // pointer receiver type. | |
14544 | bool is_pointer_; | |
14545 | // The type of the method table. | |
14546 | Type* method_table_type_; | |
14547 | // The backend variable that refers to the interface method table. | |
14548 | Bvariable* bvar_; | |
14549 | }; | |
14550 | ||
14551 | int | |
14552 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
14553 | { | |
14554 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
14555 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14556 | return TRAVERSE_EXIT; | |
14557 | return TRAVERSE_CONTINUE; | |
14558 | } | |
14559 | ||
14560 | Type* | |
14561 | Interface_mtable_expression::do_type() | |
14562 | { | |
14563 | if (this->method_table_type_ != NULL) | |
14564 | return this->method_table_type_; | |
14565 | ||
14566 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14567 | go_assert(!interface_methods->empty()); | |
14568 | ||
14569 | Struct_field_list* sfl = new Struct_field_list; | |
14570 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
14571 | this->location()); | |
14572 | sfl->push_back(Struct_field(tid)); | |
14573 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14574 | p != interface_methods->end(); | |
14575 | ++p) | |
14576 | sfl->push_back(Struct_field(*p)); | |
14577 | this->method_table_type_ = Type::make_struct_type(sfl, this->location()); | |
14578 | return this->method_table_type_; | |
14579 | } | |
14580 | ||
ea664253 | 14581 | Bexpression* |
14582 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14583 | { |
14584 | Gogo* gogo = context->gogo(); | |
2c809f8f | 14585 | Location loc = Linemap::predeclared_location(); |
14586 | if (this->bvar_ != NULL) | |
ea664253 | 14587 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 14588 | |
14589 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14590 | go_assert(!interface_methods->empty()); | |
14591 | ||
14592 | std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_") | |
14593 | + this->itype_->mangled_name(gogo) | |
14594 | + "__" | |
14595 | + this->type_->mangled_name(gogo)); | |
14596 | ||
14597 | // See whether this interface has any hidden methods. | |
14598 | bool has_hidden_methods = false; | |
14599 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14600 | p != interface_methods->end(); | |
14601 | ++p) | |
14602 | { | |
14603 | if (Gogo::is_hidden_name(p->name())) | |
14604 | { | |
14605 | has_hidden_methods = true; | |
14606 | break; | |
14607 | } | |
14608 | } | |
14609 | ||
14610 | // We already know that the named type is convertible to the | |
14611 | // interface. If the interface has hidden methods, and the named | |
14612 | // type is defined in a different package, then the interface | |
14613 | // conversion table will be defined by that other package. | |
14614 | if (has_hidden_methods | |
14615 | && this->type_->named_type() != NULL | |
14616 | && this->type_->named_type()->named_object()->package() != NULL) | |
14617 | { | |
14618 | Btype* btype = this->type()->get_backend(gogo); | |
14619 | this->bvar_ = | |
14620 | gogo->backend()->immutable_struct_reference(mangled_name, btype, loc); | |
ea664253 | 14621 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 14622 | } |
14623 | ||
14624 | // The first element is the type descriptor. | |
14625 | Type* td_type; | |
14626 | if (!this->is_pointer_) | |
14627 | td_type = this->type_; | |
14628 | else | |
14629 | td_type = Type::make_pointer_type(this->type_); | |
14630 | ||
14631 | // Build an interface method table for a type: a type descriptor followed by a | |
14632 | // list of function pointers, one for each interface method. This is used for | |
14633 | // interfaces. | |
14634 | Expression_list* svals = new Expression_list(); | |
14635 | svals->push_back(Expression::make_type_descriptor(td_type, loc)); | |
14636 | ||
14637 | Named_type* nt = this->type_->named_type(); | |
14638 | Struct_type* st = this->type_->struct_type(); | |
14639 | go_assert(nt != NULL || st != NULL); | |
14640 | ||
14641 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14642 | p != interface_methods->end(); | |
14643 | ++p) | |
14644 | { | |
14645 | bool is_ambiguous; | |
14646 | Method* m; | |
14647 | if (nt != NULL) | |
14648 | m = nt->method_function(p->name(), &is_ambiguous); | |
14649 | else | |
14650 | m = st->method_function(p->name(), &is_ambiguous); | |
14651 | go_assert(m != NULL); | |
14652 | Named_object* no = m->named_object(); | |
14653 | ||
14654 | go_assert(no->is_function() || no->is_function_declaration()); | |
14655 | svals->push_back(Expression::make_func_code_reference(no, loc)); | |
14656 | } | |
14657 | ||
14658 | Btype* btype = this->type()->get_backend(gogo); | |
14659 | Expression* mtable = Expression::make_struct_composite_literal(this->type(), | |
14660 | svals, loc); | |
ea664253 | 14661 | Bexpression* ctor = mtable->get_backend(context); |
2c809f8f | 14662 | |
14663 | bool is_public = has_hidden_methods && this->type_->named_type() != NULL; | |
14664 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, false, | |
14665 | !is_public, btype, loc); | |
14666 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
14667 | !is_public, btype, loc, ctor); | |
ea664253 | 14668 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 14669 | } |
14670 | ||
14671 | void | |
14672 | Interface_mtable_expression::do_dump_expression( | |
14673 | Ast_dump_context* ast_dump_context) const | |
14674 | { | |
14675 | ast_dump_context->ostream() << "__go_" | |
14676 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
14677 | ast_dump_context->dump_type(this->itype_); | |
14678 | ast_dump_context->ostream() << "__"; | |
14679 | ast_dump_context->dump_type(this->type_); | |
14680 | } | |
14681 | ||
14682 | Expression* | |
14683 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
14684 | bool is_pointer, Location location) | |
14685 | { | |
14686 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
14687 | } | |
14688 | ||
e440a328 | 14689 | // An expression which evaluates to the offset of a field within a |
14690 | // struct. This, like Type_info_expression, q.v., is only used to | |
14691 | // initialize fields of a type descriptor. | |
14692 | ||
14693 | class Struct_field_offset_expression : public Expression | |
14694 | { | |
14695 | public: | |
14696 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 14697 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
14698 | Linemap::predeclared_location()), | |
e440a328 | 14699 | type_(type), field_(field) |
14700 | { } | |
14701 | ||
14702 | protected: | |
f23d7786 | 14703 | bool |
14704 | do_is_immutable() const | |
14705 | { return true; } | |
14706 | ||
e440a328 | 14707 | Type* |
14708 | do_type() | |
14709 | { return Type::lookup_integer_type("uintptr"); } | |
14710 | ||
14711 | void | |
14712 | do_determine_type(const Type_context*) | |
14713 | { } | |
14714 | ||
14715 | Expression* | |
14716 | do_copy() | |
14717 | { return this; } | |
14718 | ||
ea664253 | 14719 | Bexpression* |
14720 | do_get_backend(Translate_context* context); | |
e440a328 | 14721 | |
d751bb78 | 14722 | void |
14723 | do_dump_expression(Ast_dump_context*) const; | |
14724 | ||
e440a328 | 14725 | private: |
14726 | // The type of the struct. | |
14727 | Struct_type* type_; | |
14728 | // The field. | |
14729 | const Struct_field* field_; | |
14730 | }; | |
14731 | ||
ea664253 | 14732 | // Return the backend representation for a struct field offset. |
e440a328 | 14733 | |
ea664253 | 14734 | Bexpression* |
14735 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14736 | { |
e440a328 | 14737 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 14738 | Struct_field_list::const_iterator p; |
2c8bda43 | 14739 | unsigned i = 0; |
e440a328 | 14740 | for (p = fields->begin(); |
14741 | p != fields->end(); | |
2c8bda43 | 14742 | ++p, ++i) |
14743 | if (&*p == this->field_) | |
14744 | break; | |
c484d925 | 14745 | go_assert(&*p == this->field_); |
e440a328 | 14746 | |
2c8bda43 | 14747 | Gogo* gogo = context->gogo(); |
14748 | Btype* btype = this->type_->get_backend(gogo); | |
14749 | ||
14750 | size_t offset = gogo->backend()->type_field_offset(btype, i); | |
14751 | mpz_t offsetval; | |
14752 | mpz_init_set_ui(offsetval, offset); | |
14753 | Type* uptr_type = Type::lookup_integer_type("uintptr"); | |
14754 | Expression* ret = Expression::make_integer(&offsetval, uptr_type, | |
14755 | Linemap::predeclared_location()); | |
14756 | mpz_clear(offsetval); | |
ea664253 | 14757 | return ret->get_backend(context); |
e440a328 | 14758 | } |
14759 | ||
d751bb78 | 14760 | // Dump ast representation for a struct field offset expression. |
14761 | ||
14762 | void | |
14763 | Struct_field_offset_expression::do_dump_expression( | |
14764 | Ast_dump_context* ast_dump_context) const | |
14765 | { | |
14766 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 14767 | ast_dump_context->dump_type(this->type_); |
14768 | ast_dump_context->ostream() << '.'; | |
14769 | ast_dump_context->ostream() << | |
14770 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 14771 | ast_dump_context->ostream() << ")"; |
14772 | } | |
14773 | ||
e440a328 | 14774 | // Make an expression for a struct field offset. |
14775 | ||
14776 | Expression* | |
14777 | Expression::make_struct_field_offset(Struct_type* type, | |
14778 | const Struct_field* field) | |
14779 | { | |
14780 | return new Struct_field_offset_expression(type, field); | |
14781 | } | |
14782 | ||
a9182619 | 14783 | // An expression which evaluates to a pointer to the map descriptor of |
14784 | // a map type. | |
14785 | ||
14786 | class Map_descriptor_expression : public Expression | |
14787 | { | |
14788 | public: | |
b13c66cd | 14789 | Map_descriptor_expression(Map_type* type, Location location) |
a9182619 | 14790 | : Expression(EXPRESSION_MAP_DESCRIPTOR, location), |
14791 | type_(type) | |
14792 | { } | |
14793 | ||
14794 | protected: | |
14795 | Type* | |
14796 | do_type() | |
14797 | { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } | |
14798 | ||
14799 | void | |
14800 | do_determine_type(const Type_context*) | |
14801 | { } | |
14802 | ||
14803 | Expression* | |
14804 | do_copy() | |
14805 | { return this; } | |
14806 | ||
ea664253 | 14807 | Bexpression* |
14808 | do_get_backend(Translate_context* context) | |
a9182619 | 14809 | { |
ea664253 | 14810 | return this->type_->map_descriptor_pointer(context->gogo(), |
14811 | this->location()); | |
a9182619 | 14812 | } |
14813 | ||
d751bb78 | 14814 | void |
14815 | do_dump_expression(Ast_dump_context*) const; | |
14816 | ||
a9182619 | 14817 | private: |
14818 | // The type for which this is the descriptor. | |
14819 | Map_type* type_; | |
14820 | }; | |
14821 | ||
d751bb78 | 14822 | // Dump ast representation for a map descriptor expression. |
14823 | ||
14824 | void | |
14825 | Map_descriptor_expression::do_dump_expression( | |
14826 | Ast_dump_context* ast_dump_context) const | |
14827 | { | |
14828 | ast_dump_context->ostream() << "map_descriptor("; | |
14829 | ast_dump_context->dump_type(this->type_); | |
14830 | ast_dump_context->ostream() << ")"; | |
14831 | } | |
14832 | ||
a9182619 | 14833 | // Make a map descriptor expression. |
14834 | ||
14835 | Expression* | |
b13c66cd | 14836 | Expression::make_map_descriptor(Map_type* type, Location location) |
a9182619 | 14837 | { |
14838 | return new Map_descriptor_expression(type, location); | |
14839 | } | |
14840 | ||
e440a328 | 14841 | // An expression which evaluates to the address of an unnamed label. |
14842 | ||
14843 | class Label_addr_expression : public Expression | |
14844 | { | |
14845 | public: | |
b13c66cd | 14846 | Label_addr_expression(Label* label, Location location) |
e440a328 | 14847 | : Expression(EXPRESSION_LABEL_ADDR, location), |
14848 | label_(label) | |
14849 | { } | |
14850 | ||
14851 | protected: | |
14852 | Type* | |
14853 | do_type() | |
14854 | { return Type::make_pointer_type(Type::make_void_type()); } | |
14855 | ||
14856 | void | |
14857 | do_determine_type(const Type_context*) | |
14858 | { } | |
14859 | ||
14860 | Expression* | |
14861 | do_copy() | |
14862 | { return new Label_addr_expression(this->label_, this->location()); } | |
14863 | ||
ea664253 | 14864 | Bexpression* |
14865 | do_get_backend(Translate_context* context) | |
14866 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 14867 | |
d751bb78 | 14868 | void |
14869 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14870 | { ast_dump_context->ostream() << this->label_->name(); } | |
14871 | ||
e440a328 | 14872 | private: |
14873 | // The label whose address we are taking. | |
14874 | Label* label_; | |
14875 | }; | |
14876 | ||
14877 | // Make an expression for the address of an unnamed label. | |
14878 | ||
14879 | Expression* | |
b13c66cd | 14880 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 14881 | { |
14882 | return new Label_addr_expression(label, location); | |
14883 | } | |
14884 | ||
283a177b | 14885 | // Conditional expressions. |
14886 | ||
14887 | class Conditional_expression : public Expression | |
14888 | { | |
14889 | public: | |
14890 | Conditional_expression(Expression* cond, Expression* then_expr, | |
14891 | Expression* else_expr, Location location) | |
14892 | : Expression(EXPRESSION_CONDITIONAL, location), | |
14893 | cond_(cond), then_(then_expr), else_(else_expr) | |
14894 | {} | |
14895 | ||
14896 | protected: | |
2c809f8f | 14897 | int |
14898 | do_traverse(Traverse*); | |
14899 | ||
283a177b | 14900 | Type* |
14901 | do_type(); | |
14902 | ||
14903 | void | |
2c809f8f | 14904 | do_determine_type(const Type_context*); |
283a177b | 14905 | |
14906 | Expression* | |
14907 | do_copy() | |
14908 | { | |
14909 | return new Conditional_expression(this->cond_->copy(), this->then_->copy(), | |
14910 | this->else_->copy(), this->location()); | |
14911 | } | |
14912 | ||
ea664253 | 14913 | Bexpression* |
14914 | do_get_backend(Translate_context* context); | |
283a177b | 14915 | |
14916 | void | |
14917 | do_dump_expression(Ast_dump_context*) const; | |
14918 | ||
14919 | private: | |
14920 | // The condition to be checked. | |
14921 | Expression* cond_; | |
14922 | // The expression to execute if the condition is true. | |
14923 | Expression* then_; | |
14924 | // The expression to execute if the condition is false. | |
14925 | Expression* else_; | |
14926 | }; | |
14927 | ||
2c809f8f | 14928 | // Traversal. |
14929 | ||
14930 | int | |
14931 | Conditional_expression::do_traverse(Traverse* traverse) | |
14932 | { | |
14933 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
14934 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
14935 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
14936 | return TRAVERSE_EXIT; | |
14937 | return TRAVERSE_CONTINUE; | |
14938 | } | |
14939 | ||
283a177b | 14940 | // Return the type of the conditional expression. |
14941 | ||
14942 | Type* | |
14943 | Conditional_expression::do_type() | |
14944 | { | |
14945 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 14946 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
14947 | NULL)) | |
283a177b | 14948 | result_type = this->then_->type(); |
14949 | else if (this->then_->is_nil_expression() | |
14950 | || this->else_->is_nil_expression()) | |
14951 | result_type = (!this->then_->is_nil_expression() | |
14952 | ? this->then_->type() | |
14953 | : this->else_->type()); | |
14954 | return result_type; | |
14955 | } | |
14956 | ||
2c809f8f | 14957 | // Determine type for a conditional expression. |
14958 | ||
14959 | void | |
14960 | Conditional_expression::do_determine_type(const Type_context* context) | |
14961 | { | |
14962 | this->cond_->determine_type_no_context(); | |
14963 | this->then_->determine_type(context); | |
14964 | this->else_->determine_type(context); | |
14965 | } | |
14966 | ||
283a177b | 14967 | // Get the backend representation of a conditional expression. |
14968 | ||
ea664253 | 14969 | Bexpression* |
14970 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 14971 | { |
14972 | Gogo* gogo = context->gogo(); | |
14973 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 14974 | Bexpression* cond = this->cond_->get_backend(context); |
14975 | Bexpression* then = this->then_->get_backend(context); | |
14976 | Bexpression* belse = this->else_->get_backend(context); | |
14977 | return gogo->backend()->conditional_expression(result_btype, cond, then, | |
14978 | belse, this->location()); | |
283a177b | 14979 | } |
14980 | ||
14981 | // Dump ast representation of a conditional expression. | |
14982 | ||
14983 | void | |
14984 | Conditional_expression::do_dump_expression( | |
14985 | Ast_dump_context* ast_dump_context) const | |
14986 | { | |
14987 | ast_dump_context->ostream() << "("; | |
14988 | ast_dump_context->dump_expression(this->cond_); | |
14989 | ast_dump_context->ostream() << " ? "; | |
14990 | ast_dump_context->dump_expression(this->then_); | |
14991 | ast_dump_context->ostream() << " : "; | |
14992 | ast_dump_context->dump_expression(this->else_); | |
14993 | ast_dump_context->ostream() << ") "; | |
14994 | } | |
14995 | ||
14996 | // Make a conditional expression. | |
14997 | ||
14998 | Expression* | |
14999 | Expression::make_conditional(Expression* cond, Expression* then, | |
15000 | Expression* else_expr, Location location) | |
15001 | { | |
15002 | return new Conditional_expression(cond, then, else_expr, location); | |
15003 | } | |
15004 | ||
2c809f8f | 15005 | // Compound expressions. |
15006 | ||
15007 | class Compound_expression : public Expression | |
15008 | { | |
15009 | public: | |
15010 | Compound_expression(Expression* init, Expression* expr, Location location) | |
15011 | : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr) | |
15012 | {} | |
15013 | ||
15014 | protected: | |
15015 | int | |
15016 | do_traverse(Traverse*); | |
15017 | ||
15018 | Type* | |
15019 | do_type(); | |
15020 | ||
15021 | void | |
15022 | do_determine_type(const Type_context*); | |
15023 | ||
15024 | Expression* | |
15025 | do_copy() | |
15026 | { | |
15027 | return new Compound_expression(this->init_->copy(), this->expr_->copy(), | |
15028 | this->location()); | |
15029 | } | |
15030 | ||
ea664253 | 15031 | Bexpression* |
15032 | do_get_backend(Translate_context* context); | |
2c809f8f | 15033 | |
15034 | void | |
15035 | do_dump_expression(Ast_dump_context*) const; | |
15036 | ||
15037 | private: | |
15038 | // The expression that is evaluated first and discarded. | |
15039 | Expression* init_; | |
15040 | // The expression that is evaluated and returned. | |
15041 | Expression* expr_; | |
15042 | }; | |
15043 | ||
15044 | // Traversal. | |
15045 | ||
15046 | int | |
15047 | Compound_expression::do_traverse(Traverse* traverse) | |
15048 | { | |
15049 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15050 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15051 | return TRAVERSE_EXIT; | |
15052 | return TRAVERSE_CONTINUE; | |
15053 | } | |
15054 | ||
15055 | // Return the type of the compound expression. | |
15056 | ||
15057 | Type* | |
15058 | Compound_expression::do_type() | |
15059 | { | |
15060 | return this->expr_->type(); | |
15061 | } | |
15062 | ||
15063 | // Determine type for a compound expression. | |
15064 | ||
15065 | void | |
15066 | Compound_expression::do_determine_type(const Type_context* context) | |
15067 | { | |
15068 | this->init_->determine_type_no_context(); | |
15069 | this->expr_->determine_type(context); | |
15070 | } | |
15071 | ||
15072 | // Get the backend representation of a compound expression. | |
15073 | ||
ea664253 | 15074 | Bexpression* |
15075 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15076 | { |
15077 | Gogo* gogo = context->gogo(); | |
ea664253 | 15078 | Bexpression* binit = this->init_->get_backend(context); |
2c809f8f | 15079 | Bstatement* init_stmt = gogo->backend()->expression_statement(binit); |
ea664253 | 15080 | Bexpression* bexpr = this->expr_->get_backend(context); |
15081 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15082 | this->location()); | |
2c809f8f | 15083 | } |
15084 | ||
15085 | // Dump ast representation of a conditional expression. | |
15086 | ||
15087 | void | |
15088 | Compound_expression::do_dump_expression( | |
15089 | Ast_dump_context* ast_dump_context) const | |
15090 | { | |
15091 | ast_dump_context->ostream() << "("; | |
15092 | ast_dump_context->dump_expression(this->init_); | |
15093 | ast_dump_context->ostream() << ","; | |
15094 | ast_dump_context->dump_expression(this->expr_); | |
15095 | ast_dump_context->ostream() << ") "; | |
15096 | } | |
15097 | ||
15098 | // Make a compound expression. | |
15099 | ||
15100 | Expression* | |
15101 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15102 | { | |
15103 | return new Compound_expression(init, expr, location); | |
15104 | } | |
15105 | ||
e440a328 | 15106 | // Import an expression. This comes at the end in order to see the |
15107 | // various class definitions. | |
15108 | ||
15109 | Expression* | |
15110 | Expression::import_expression(Import* imp) | |
15111 | { | |
15112 | int c = imp->peek_char(); | |
15113 | if (imp->match_c_string("- ") | |
15114 | || imp->match_c_string("! ") | |
15115 | || imp->match_c_string("^ ")) | |
15116 | return Unary_expression::do_import(imp); | |
15117 | else if (c == '(') | |
15118 | return Binary_expression::do_import(imp); | |
15119 | else if (imp->match_c_string("true") | |
15120 | || imp->match_c_string("false")) | |
15121 | return Boolean_expression::do_import(imp); | |
15122 | else if (c == '"') | |
15123 | return String_expression::do_import(imp); | |
15124 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15125 | { | |
15126 | // This handles integers, floats and complex constants. | |
15127 | return Integer_expression::do_import(imp); | |
15128 | } | |
15129 | else if (imp->match_c_string("nil")) | |
15130 | return Nil_expression::do_import(imp); | |
15131 | else if (imp->match_c_string("convert")) | |
15132 | return Type_conversion_expression::do_import(imp); | |
15133 | else | |
15134 | { | |
15135 | error_at(imp->location(), "import error: expected expression"); | |
15136 | return Expression::make_error(imp->location()); | |
15137 | } | |
15138 | } | |
15139 | ||
15140 | // Class Expression_list. | |
15141 | ||
15142 | // Traverse the list. | |
15143 | ||
15144 | int | |
15145 | Expression_list::traverse(Traverse* traverse) | |
15146 | { | |
15147 | for (Expression_list::iterator p = this->begin(); | |
15148 | p != this->end(); | |
15149 | ++p) | |
15150 | { | |
15151 | if (*p != NULL) | |
15152 | { | |
15153 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15154 | return TRAVERSE_EXIT; | |
15155 | } | |
15156 | } | |
15157 | return TRAVERSE_CONTINUE; | |
15158 | } | |
15159 | ||
15160 | // Copy the list. | |
15161 | ||
15162 | Expression_list* | |
15163 | Expression_list::copy() | |
15164 | { | |
15165 | Expression_list* ret = new Expression_list(); | |
15166 | for (Expression_list::iterator p = this->begin(); | |
15167 | p != this->end(); | |
15168 | ++p) | |
15169 | { | |
15170 | if (*p == NULL) | |
15171 | ret->push_back(NULL); | |
15172 | else | |
15173 | ret->push_back((*p)->copy()); | |
15174 | } | |
15175 | return ret; | |
15176 | } | |
15177 | ||
15178 | // Return whether an expression list has an error expression. | |
15179 | ||
15180 | bool | |
15181 | Expression_list::contains_error() const | |
15182 | { | |
15183 | for (Expression_list::const_iterator p = this->begin(); | |
15184 | p != this->end(); | |
15185 | ++p) | |
15186 | if (*p != NULL && (*p)->is_error_expression()) | |
15187 | return true; | |
15188 | return false; | |
15189 | } | |
0c77715b | 15190 | |
15191 | // Class Numeric_constant. | |
15192 | ||
15193 | // Destructor. | |
15194 | ||
15195 | Numeric_constant::~Numeric_constant() | |
15196 | { | |
15197 | this->clear(); | |
15198 | } | |
15199 | ||
15200 | // Copy constructor. | |
15201 | ||
15202 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15203 | : classification_(a.classification_), type_(a.type_) | |
15204 | { | |
15205 | switch (a.classification_) | |
15206 | { | |
15207 | case NC_INVALID: | |
15208 | break; | |
15209 | case NC_INT: | |
15210 | case NC_RUNE: | |
15211 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15212 | break; | |
15213 | case NC_FLOAT: | |
15214 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15215 | break; | |
15216 | case NC_COMPLEX: | |
15217 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
15218 | GMP_RNDN); | |
15219 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
15220 | GMP_RNDN); | |
15221 | break; | |
15222 | default: | |
15223 | go_unreachable(); | |
15224 | } | |
15225 | } | |
15226 | ||
15227 | // Assignment operator. | |
15228 | ||
15229 | Numeric_constant& | |
15230 | Numeric_constant::operator=(const Numeric_constant& a) | |
15231 | { | |
15232 | this->clear(); | |
15233 | this->classification_ = a.classification_; | |
15234 | this->type_ = a.type_; | |
15235 | switch (a.classification_) | |
15236 | { | |
15237 | case NC_INVALID: | |
15238 | break; | |
15239 | case NC_INT: | |
15240 | case NC_RUNE: | |
15241 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15242 | break; | |
15243 | case NC_FLOAT: | |
15244 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15245 | break; | |
15246 | case NC_COMPLEX: | |
15247 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
15248 | GMP_RNDN); | |
15249 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
15250 | GMP_RNDN); | |
15251 | break; | |
15252 | default: | |
15253 | go_unreachable(); | |
15254 | } | |
15255 | return *this; | |
15256 | } | |
15257 | ||
15258 | // Clear the contents. | |
15259 | ||
15260 | void | |
15261 | Numeric_constant::clear() | |
15262 | { | |
15263 | switch (this->classification_) | |
15264 | { | |
15265 | case NC_INVALID: | |
15266 | break; | |
15267 | case NC_INT: | |
15268 | case NC_RUNE: | |
15269 | mpz_clear(this->u_.int_val); | |
15270 | break; | |
15271 | case NC_FLOAT: | |
15272 | mpfr_clear(this->u_.float_val); | |
15273 | break; | |
15274 | case NC_COMPLEX: | |
15275 | mpfr_clear(this->u_.complex_val.real); | |
15276 | mpfr_clear(this->u_.complex_val.imag); | |
15277 | break; | |
15278 | default: | |
15279 | go_unreachable(); | |
15280 | } | |
15281 | this->classification_ = NC_INVALID; | |
15282 | } | |
15283 | ||
15284 | // Set to an unsigned long value. | |
15285 | ||
15286 | void | |
15287 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15288 | { | |
15289 | this->clear(); | |
15290 | this->classification_ = NC_INT; | |
15291 | this->type_ = type; | |
15292 | mpz_init_set_ui(this->u_.int_val, val); | |
15293 | } | |
15294 | ||
15295 | // Set to an integer value. | |
15296 | ||
15297 | void | |
15298 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15299 | { | |
15300 | this->clear(); | |
15301 | this->classification_ = NC_INT; | |
15302 | this->type_ = type; | |
15303 | mpz_init_set(this->u_.int_val, val); | |
15304 | } | |
15305 | ||
15306 | // Set to a rune value. | |
15307 | ||
15308 | void | |
15309 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15310 | { | |
15311 | this->clear(); | |
15312 | this->classification_ = NC_RUNE; | |
15313 | this->type_ = type; | |
15314 | mpz_init_set(this->u_.int_val, val); | |
15315 | } | |
15316 | ||
15317 | // Set to a floating point value. | |
15318 | ||
15319 | void | |
15320 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15321 | { | |
15322 | this->clear(); | |
15323 | this->classification_ = NC_FLOAT; | |
15324 | this->type_ = type; | |
833b523c | 15325 | // Numeric constants do not have negative zero values, so remove |
15326 | // them here. They also don't have infinity or NaN values, but we | |
15327 | // should never see them here. | |
15328 | if (mpfr_zero_p(val)) | |
15329 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
15330 | else | |
15331 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 15332 | } |
15333 | ||
15334 | // Set to a complex value. | |
15335 | ||
15336 | void | |
15337 | Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag) | |
15338 | { | |
15339 | this->clear(); | |
15340 | this->classification_ = NC_COMPLEX; | |
15341 | this->type_ = type; | |
15342 | mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN); | |
15343 | mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN); | |
15344 | } | |
15345 | ||
15346 | // Get an int value. | |
15347 | ||
15348 | void | |
15349 | Numeric_constant::get_int(mpz_t* val) const | |
15350 | { | |
15351 | go_assert(this->is_int()); | |
15352 | mpz_init_set(*val, this->u_.int_val); | |
15353 | } | |
15354 | ||
15355 | // Get a rune value. | |
15356 | ||
15357 | void | |
15358 | Numeric_constant::get_rune(mpz_t* val) const | |
15359 | { | |
15360 | go_assert(this->is_rune()); | |
15361 | mpz_init_set(*val, this->u_.int_val); | |
15362 | } | |
15363 | ||
15364 | // Get a floating point value. | |
15365 | ||
15366 | void | |
15367 | Numeric_constant::get_float(mpfr_t* val) const | |
15368 | { | |
15369 | go_assert(this->is_float()); | |
15370 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15371 | } | |
15372 | ||
15373 | // Get a complex value. | |
15374 | ||
15375 | void | |
15376 | Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const | |
15377 | { | |
15378 | go_assert(this->is_complex()); | |
15379 | mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN); | |
15380 | mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN); | |
15381 | } | |
15382 | ||
15383 | // Express value as unsigned long if possible. | |
15384 | ||
15385 | Numeric_constant::To_unsigned_long | |
15386 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
15387 | { | |
15388 | switch (this->classification_) | |
15389 | { | |
15390 | case NC_INT: | |
15391 | case NC_RUNE: | |
15392 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
15393 | case NC_FLOAT: | |
15394 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
15395 | case NC_COMPLEX: | |
15396 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15397 | return NC_UL_NOTINT; | |
15398 | return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val); | |
15399 | default: | |
15400 | go_unreachable(); | |
15401 | } | |
15402 | } | |
15403 | ||
15404 | // Express integer value as unsigned long if possible. | |
15405 | ||
15406 | Numeric_constant::To_unsigned_long | |
15407 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
15408 | unsigned long *val) const | |
15409 | { | |
15410 | if (mpz_sgn(ival) < 0) | |
15411 | return NC_UL_NEGATIVE; | |
15412 | unsigned long ui = mpz_get_ui(ival); | |
15413 | if (mpz_cmp_ui(ival, ui) != 0) | |
15414 | return NC_UL_BIG; | |
15415 | *val = ui; | |
15416 | return NC_UL_VALID; | |
15417 | } | |
15418 | ||
15419 | // Express floating point value as unsigned long if possible. | |
15420 | ||
15421 | Numeric_constant::To_unsigned_long | |
15422 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
15423 | unsigned long *val) const | |
15424 | { | |
15425 | if (!mpfr_integer_p(fval)) | |
15426 | return NC_UL_NOTINT; | |
15427 | mpz_t ival; | |
15428 | mpz_init(ival); | |
15429 | mpfr_get_z(ival, fval, GMP_RNDN); | |
15430 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
15431 | mpz_clear(ival); | |
15432 | return ret; | |
15433 | } | |
15434 | ||
15435 | // Convert value to integer if possible. | |
15436 | ||
15437 | bool | |
15438 | Numeric_constant::to_int(mpz_t* val) const | |
15439 | { | |
15440 | switch (this->classification_) | |
15441 | { | |
15442 | case NC_INT: | |
15443 | case NC_RUNE: | |
15444 | mpz_init_set(*val, this->u_.int_val); | |
15445 | return true; | |
15446 | case NC_FLOAT: | |
15447 | if (!mpfr_integer_p(this->u_.float_val)) | |
15448 | return false; | |
15449 | mpz_init(*val); | |
15450 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
15451 | return true; | |
15452 | case NC_COMPLEX: | |
15453 | if (!mpfr_zero_p(this->u_.complex_val.imag) | |
15454 | || !mpfr_integer_p(this->u_.complex_val.real)) | |
15455 | return false; | |
15456 | mpz_init(*val); | |
15457 | mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN); | |
15458 | return true; | |
15459 | default: | |
15460 | go_unreachable(); | |
15461 | } | |
15462 | } | |
15463 | ||
15464 | // Convert value to floating point if possible. | |
15465 | ||
15466 | bool | |
15467 | Numeric_constant::to_float(mpfr_t* val) const | |
15468 | { | |
15469 | switch (this->classification_) | |
15470 | { | |
15471 | case NC_INT: | |
15472 | case NC_RUNE: | |
15473 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
15474 | return true; | |
15475 | case NC_FLOAT: | |
15476 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15477 | return true; | |
15478 | case NC_COMPLEX: | |
15479 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15480 | return false; | |
15481 | mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN); | |
15482 | return true; | |
15483 | default: | |
15484 | go_unreachable(); | |
15485 | } | |
15486 | } | |
15487 | ||
15488 | // Convert value to complex. | |
15489 | ||
15490 | bool | |
15491 | Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const | |
15492 | { | |
15493 | switch (this->classification_) | |
15494 | { | |
15495 | case NC_INT: | |
15496 | case NC_RUNE: | |
15497 | mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN); | |
15498 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
15499 | return true; | |
15500 | case NC_FLOAT: | |
15501 | mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN); | |
15502 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
15503 | return true; | |
15504 | case NC_COMPLEX: | |
15505 | mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN); | |
15506 | mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN); | |
15507 | return true; | |
15508 | default: | |
15509 | go_unreachable(); | |
15510 | } | |
15511 | } | |
15512 | ||
15513 | // Get the type. | |
15514 | ||
15515 | Type* | |
15516 | Numeric_constant::type() const | |
15517 | { | |
15518 | if (this->type_ != NULL) | |
15519 | return this->type_; | |
15520 | switch (this->classification_) | |
15521 | { | |
15522 | case NC_INT: | |
15523 | return Type::make_abstract_integer_type(); | |
15524 | case NC_RUNE: | |
15525 | return Type::make_abstract_character_type(); | |
15526 | case NC_FLOAT: | |
15527 | return Type::make_abstract_float_type(); | |
15528 | case NC_COMPLEX: | |
15529 | return Type::make_abstract_complex_type(); | |
15530 | default: | |
15531 | go_unreachable(); | |
15532 | } | |
15533 | } | |
15534 | ||
15535 | // If the constant can be expressed in TYPE, then set the type of the | |
15536 | // constant to TYPE and return true. Otherwise return false, and, if | |
15537 | // ISSUE_ERROR is true, report an appropriate error message. | |
15538 | ||
15539 | bool | |
15540 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
15541 | { | |
15542 | bool ret; | |
15543 | if (type == NULL) | |
15544 | ret = true; | |
15545 | else if (type->integer_type() != NULL) | |
15546 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
15547 | else if (type->float_type() != NULL) | |
15548 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
15549 | else if (type->complex_type() != NULL) | |
15550 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
15551 | else | |
15552 | go_unreachable(); | |
15553 | if (ret) | |
15554 | this->type_ = type; | |
15555 | return ret; | |
15556 | } | |
15557 | ||
15558 | // Check whether the constant can be expressed in an integer type. | |
15559 | ||
15560 | bool | |
15561 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
15562 | Location location) const | |
15563 | { | |
15564 | mpz_t val; | |
15565 | switch (this->classification_) | |
15566 | { | |
15567 | case NC_INT: | |
15568 | case NC_RUNE: | |
15569 | mpz_init_set(val, this->u_.int_val); | |
15570 | break; | |
15571 | ||
15572 | case NC_FLOAT: | |
15573 | if (!mpfr_integer_p(this->u_.float_val)) | |
15574 | { | |
15575 | if (issue_error) | |
15576 | error_at(location, "floating point constant truncated to integer"); | |
15577 | return false; | |
15578 | } | |
15579 | mpz_init(val); | |
15580 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
15581 | break; | |
15582 | ||
15583 | case NC_COMPLEX: | |
15584 | if (!mpfr_integer_p(this->u_.complex_val.real) | |
15585 | || !mpfr_zero_p(this->u_.complex_val.imag)) | |
15586 | { | |
15587 | if (issue_error) | |
15588 | error_at(location, "complex constant truncated to integer"); | |
15589 | return false; | |
15590 | } | |
15591 | mpz_init(val); | |
15592 | mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN); | |
15593 | break; | |
15594 | ||
15595 | default: | |
15596 | go_unreachable(); | |
15597 | } | |
15598 | ||
15599 | bool ret; | |
15600 | if (type->is_abstract()) | |
15601 | ret = true; | |
15602 | else | |
15603 | { | |
15604 | int bits = mpz_sizeinbase(val, 2); | |
15605 | if (type->is_unsigned()) | |
15606 | { | |
15607 | // For an unsigned type we can only accept a nonnegative | |
15608 | // number, and we must be able to represents at least BITS. | |
15609 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
15610 | } | |
15611 | else | |
15612 | { | |
15613 | // For a signed type we need an extra bit to indicate the | |
15614 | // sign. We have to handle the most negative integer | |
15615 | // specially. | |
15616 | ret = (bits + 1 <= type->bits() | |
15617 | || (bits <= type->bits() | |
15618 | && mpz_sgn(val) < 0 | |
15619 | && (mpz_scan1(val, 0) | |
15620 | == static_cast<unsigned long>(type->bits() - 1)) | |
15621 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
15622 | } | |
15623 | } | |
15624 | ||
15625 | if (!ret && issue_error) | |
15626 | error_at(location, "integer constant overflow"); | |
15627 | ||
15628 | return ret; | |
15629 | } | |
15630 | ||
15631 | // Check whether the constant can be expressed in a floating point | |
15632 | // type. | |
15633 | ||
15634 | bool | |
15635 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 15636 | Location location) |
0c77715b | 15637 | { |
15638 | mpfr_t val; | |
15639 | switch (this->classification_) | |
15640 | { | |
15641 | case NC_INT: | |
15642 | case NC_RUNE: | |
15643 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
15644 | break; | |
15645 | ||
15646 | case NC_FLOAT: | |
15647 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
15648 | break; | |
15649 | ||
15650 | case NC_COMPLEX: | |
15651 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15652 | { | |
15653 | if (issue_error) | |
15654 | error_at(location, "complex constant truncated to float"); | |
15655 | return false; | |
15656 | } | |
15657 | mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN); | |
15658 | break; | |
15659 | ||
15660 | default: | |
15661 | go_unreachable(); | |
15662 | } | |
15663 | ||
15664 | bool ret; | |
15665 | if (type->is_abstract()) | |
15666 | ret = true; | |
15667 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
15668 | { | |
15669 | // A NaN or Infinity always fits in the range of the type. | |
15670 | ret = true; | |
15671 | } | |
15672 | else | |
15673 | { | |
15674 | mp_exp_t exp = mpfr_get_exp(val); | |
15675 | mp_exp_t max_exp; | |
15676 | switch (type->bits()) | |
15677 | { | |
15678 | case 32: | |
15679 | max_exp = 128; | |
15680 | break; | |
15681 | case 64: | |
15682 | max_exp = 1024; | |
15683 | break; | |
15684 | default: | |
15685 | go_unreachable(); | |
15686 | } | |
15687 | ||
15688 | ret = exp <= max_exp; | |
d0bcce51 | 15689 | |
15690 | if (ret) | |
15691 | { | |
15692 | // Round the constant to the desired type. | |
15693 | mpfr_t t; | |
15694 | mpfr_init(t); | |
15695 | switch (type->bits()) | |
15696 | { | |
15697 | case 32: | |
15698 | mpfr_set_prec(t, 24); | |
15699 | break; | |
15700 | case 64: | |
15701 | mpfr_set_prec(t, 53); | |
15702 | break; | |
15703 | default: | |
15704 | go_unreachable(); | |
15705 | } | |
15706 | mpfr_set(t, val, GMP_RNDN); | |
15707 | mpfr_set(val, t, GMP_RNDN); | |
15708 | mpfr_clear(t); | |
15709 | ||
15710 | this->set_float(type, val); | |
15711 | } | |
0c77715b | 15712 | } |
15713 | ||
15714 | mpfr_clear(val); | |
15715 | ||
15716 | if (!ret && issue_error) | |
15717 | error_at(location, "floating point constant overflow"); | |
15718 | ||
15719 | return ret; | |
15720 | } | |
15721 | ||
15722 | // Check whether the constant can be expressed in a complex type. | |
15723 | ||
15724 | bool | |
15725 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 15726 | Location location) |
0c77715b | 15727 | { |
15728 | if (type->is_abstract()) | |
15729 | return true; | |
15730 | ||
15731 | mp_exp_t max_exp; | |
15732 | switch (type->bits()) | |
15733 | { | |
15734 | case 64: | |
15735 | max_exp = 128; | |
15736 | break; | |
15737 | case 128: | |
15738 | max_exp = 1024; | |
15739 | break; | |
15740 | default: | |
15741 | go_unreachable(); | |
15742 | } | |
15743 | ||
15744 | mpfr_t real; | |
d0bcce51 | 15745 | mpfr_t imag; |
0c77715b | 15746 | switch (this->classification_) |
15747 | { | |
15748 | case NC_INT: | |
15749 | case NC_RUNE: | |
15750 | mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN); | |
d0bcce51 | 15751 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 15752 | break; |
15753 | ||
15754 | case NC_FLOAT: | |
15755 | mpfr_init_set(real, this->u_.float_val, GMP_RNDN); | |
d0bcce51 | 15756 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 15757 | break; |
15758 | ||
15759 | case NC_COMPLEX: | |
0c77715b | 15760 | mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN); |
d0bcce51 | 15761 | mpfr_init_set(imag, this->u_.complex_val.imag, GMP_RNDN); |
0c77715b | 15762 | break; |
15763 | ||
15764 | default: | |
15765 | go_unreachable(); | |
15766 | } | |
15767 | ||
d0bcce51 | 15768 | bool ret = true; |
15769 | if (!mpfr_nan_p(real) | |
15770 | && !mpfr_inf_p(real) | |
15771 | && !mpfr_zero_p(real) | |
15772 | && mpfr_get_exp(real) > max_exp) | |
15773 | { | |
15774 | if (issue_error) | |
15775 | error_at(location, "complex real part overflow"); | |
15776 | ret = false; | |
15777 | } | |
0c77715b | 15778 | |
d0bcce51 | 15779 | if (!mpfr_nan_p(imag) |
15780 | && !mpfr_inf_p(imag) | |
15781 | && !mpfr_zero_p(imag) | |
15782 | && mpfr_get_exp(imag) > max_exp) | |
15783 | { | |
15784 | if (issue_error) | |
15785 | error_at(location, "complex imaginary part overflow"); | |
15786 | ret = false; | |
15787 | } | |
0c77715b | 15788 | |
d0bcce51 | 15789 | if (ret) |
15790 | { | |
15791 | // Round the constant to the desired type. | |
15792 | mpfr_t t; | |
15793 | mpfr_init(t); | |
15794 | switch (type->bits()) | |
15795 | { | |
15796 | case 64: | |
15797 | mpfr_set_prec(t, 24); | |
15798 | break; | |
15799 | case 128: | |
15800 | mpfr_set_prec(t, 53); | |
15801 | break; | |
15802 | default: | |
15803 | go_unreachable(); | |
15804 | } | |
15805 | mpfr_set(t, real, GMP_RNDN); | |
15806 | mpfr_set(real, t, GMP_RNDN); | |
15807 | mpfr_set(t, imag, GMP_RNDN); | |
15808 | mpfr_set(imag, t, GMP_RNDN); | |
15809 | mpfr_clear(t); | |
15810 | ||
15811 | this->set_complex(type, real, imag); | |
15812 | } | |
15813 | ||
15814 | mpfr_clear(real); | |
15815 | mpfr_clear(imag); | |
0c77715b | 15816 | |
15817 | return ret; | |
15818 | } | |
15819 | ||
15820 | // Return an Expression for this value. | |
15821 | ||
15822 | Expression* | |
15823 | Numeric_constant::expression(Location loc) const | |
15824 | { | |
15825 | switch (this->classification_) | |
15826 | { | |
15827 | case NC_INT: | |
15828 | return Expression::make_integer(&this->u_.int_val, this->type_, loc); | |
15829 | case NC_RUNE: | |
15830 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
15831 | case NC_FLOAT: | |
15832 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
15833 | case NC_COMPLEX: | |
15834 | return Expression::make_complex(&this->u_.complex_val.real, | |
15835 | &this->u_.complex_val.imag, | |
15836 | this->type_, loc); | |
15837 | default: | |
15838 | go_unreachable(); | |
15839 | } | |
15840 | } |