]>
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" | |
631d5788 | 13 | #include "go-diagnostics.h" |
e440a328 | 14 | #include "types.h" |
15 | #include "export.h" | |
16 | #include "import.h" | |
17 | #include "statements.h" | |
18 | #include "lex.h" | |
a9182619 | 19 | #include "runtime.h" |
6e193e6f | 20 | #include "backend.h" |
e440a328 | 21 | #include "expressions.h" |
d751bb78 | 22 | #include "ast-dump.h" |
e440a328 | 23 | |
24 | // Class Expression. | |
25 | ||
26 | Expression::Expression(Expression_classification classification, | |
b13c66cd | 27 | Location location) |
e440a328 | 28 | : classification_(classification), location_(location) |
29 | { | |
30 | } | |
31 | ||
32 | Expression::~Expression() | |
33 | { | |
34 | } | |
35 | ||
e440a328 | 36 | // Traverse the expressions. |
37 | ||
38 | int | |
39 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
40 | { | |
41 | Expression* expr = *pexpr; | |
42 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
43 | { | |
44 | int t = traverse->expression(pexpr); | |
45 | if (t == TRAVERSE_EXIT) | |
46 | return TRAVERSE_EXIT; | |
47 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
48 | return TRAVERSE_CONTINUE; | |
49 | } | |
50 | return expr->do_traverse(traverse); | |
51 | } | |
52 | ||
53 | // Traverse subexpressions of this expression. | |
54 | ||
55 | int | |
56 | Expression::traverse_subexpressions(Traverse* traverse) | |
57 | { | |
58 | return this->do_traverse(traverse); | |
59 | } | |
60 | ||
61 | // Default implementation for do_traverse for child classes. | |
62 | ||
63 | int | |
64 | Expression::do_traverse(Traverse*) | |
65 | { | |
66 | return TRAVERSE_CONTINUE; | |
67 | } | |
68 | ||
69 | // This virtual function is called by the parser if the value of this | |
a7549a6a | 70 | // expression is being discarded. By default, we give an error. |
71 | // Expressions with side effects override. | |
e440a328 | 72 | |
4f2138d7 | 73 | bool |
e440a328 | 74 | Expression::do_discarding_value() |
75 | { | |
a7549a6a | 76 | this->unused_value_error(); |
4f2138d7 | 77 | return false; |
e440a328 | 78 | } |
79 | ||
80 | // This virtual function is called to export expressions. This will | |
81 | // only be used by expressions which may be constant. | |
82 | ||
83 | void | |
84 | Expression::do_export(Export*) const | |
85 | { | |
c3e6f413 | 86 | go_unreachable(); |
e440a328 | 87 | } |
88 | ||
a7549a6a | 89 | // Give an error saying that the value of the expression is not used. |
e440a328 | 90 | |
91 | void | |
a7549a6a | 92 | Expression::unused_value_error() |
e440a328 | 93 | { |
4f2138d7 | 94 | this->report_error(_("value computed is not used")); |
e440a328 | 95 | } |
96 | ||
97 | // Note that this expression is an error. This is called by children | |
98 | // when they discover an error. | |
99 | ||
100 | void | |
101 | Expression::set_is_error() | |
102 | { | |
103 | this->classification_ = EXPRESSION_ERROR; | |
104 | } | |
105 | ||
106 | // For children to call to report an error conveniently. | |
107 | ||
108 | void | |
109 | Expression::report_error(const char* msg) | |
110 | { | |
631d5788 | 111 | go_error_at(this->location_, "%s", msg); |
e440a328 | 112 | this->set_is_error(); |
113 | } | |
114 | ||
115 | // Set types of variables and constants. This is implemented by the | |
116 | // child class. | |
117 | ||
118 | void | |
119 | Expression::determine_type(const Type_context* context) | |
120 | { | |
121 | this->do_determine_type(context); | |
122 | } | |
123 | ||
124 | // Set types when there is no context. | |
125 | ||
126 | void | |
127 | Expression::determine_type_no_context() | |
128 | { | |
129 | Type_context context; | |
130 | this->do_determine_type(&context); | |
131 | } | |
132 | ||
2c809f8f | 133 | // Return an expression handling any conversions which must be done during |
e440a328 | 134 | // assignment. |
135 | ||
2c809f8f | 136 | Expression* |
b4a33049 | 137 | Expression::convert_for_assignment(Gogo*, Type* lhs_type, |
2c809f8f | 138 | Expression* rhs, Location location) |
e440a328 | 139 | { |
2c809f8f | 140 | Type* rhs_type = rhs->type(); |
141 | if (lhs_type->is_error() | |
142 | || rhs_type->is_error() | |
143 | || rhs->is_error_expression()) | |
144 | return Expression::make_error(location); | |
e440a328 | 145 | |
54211955 | 146 | if (lhs_type->forwarded() != rhs_type->forwarded() |
147 | && lhs_type->interface_type() != NULL) | |
e440a328 | 148 | { |
149 | if (rhs_type->interface_type() == NULL) | |
2c809f8f | 150 | return Expression::convert_type_to_interface(lhs_type, rhs, location); |
e440a328 | 151 | else |
2c809f8f | 152 | return Expression::convert_interface_to_interface(lhs_type, rhs, false, |
153 | location); | |
e440a328 | 154 | } |
54211955 | 155 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
156 | && rhs_type->interface_type() != NULL) | |
2c809f8f | 157 | return Expression::convert_interface_to_type(lhs_type, rhs, location); |
411eb89e | 158 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 159 | { |
2c809f8f | 160 | // Assigning nil to a slice. |
2c809f8f | 161 | Expression* nil = Expression::make_nil(location); |
e67508fa | 162 | Expression* zero = Expression::make_integer_ul(0, NULL, location); |
2c809f8f | 163 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); |
e440a328 | 164 | } |
165 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 166 | return Expression::make_nil(location); |
167 | else if (Type::are_identical(lhs_type, rhs_type, false, NULL)) | |
e440a328 | 168 | { |
169 | // No conversion is needed. | |
2c809f8f | 170 | return rhs; |
171 | } | |
172 | else if (lhs_type->points_to() != NULL) | |
173 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
174 | else if (lhs_type->is_numeric_type()) | |
175 | return Expression::make_cast(lhs_type, rhs, location); | |
176 | else if ((lhs_type->struct_type() != NULL | |
177 | && rhs_type->struct_type() != NULL) | |
178 | || (lhs_type->array_type() != NULL | |
179 | && rhs_type->array_type() != NULL)) | |
e440a328 | 180 | { |
181 | // This conversion must be permitted by Go, or we wouldn't have | |
182 | // gotten here. | |
2c809f8f | 183 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 184 | } |
185 | else | |
2c809f8f | 186 | return rhs; |
e440a328 | 187 | } |
188 | ||
2c809f8f | 189 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 190 | // interface type. |
191 | ||
2c809f8f | 192 | Expression* |
193 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
194 | Location location) | |
e440a328 | 195 | { |
e440a328 | 196 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
197 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
198 | ||
199 | // Since RHS_TYPE is a static type, we can create the interface | |
200 | // method table at compile time. | |
201 | ||
202 | // When setting an interface to nil, we just set both fields to | |
203 | // NULL. | |
2c809f8f | 204 | Type* rhs_type = rhs->type(); |
e440a328 | 205 | if (rhs_type->is_nil_type()) |
63697958 | 206 | { |
2c809f8f | 207 | Expression* nil = Expression::make_nil(location); |
208 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 209 | } |
e440a328 | 210 | |
211 | // This should have been checked already. | |
c484d925 | 212 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 213 | |
e440a328 | 214 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
215 | // then the first field is the type descriptor for RHS_TYPE. | |
216 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 217 | Expression* first_field; |
e440a328 | 218 | if (lhs_is_empty) |
2c809f8f | 219 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 220 | else |
221 | { | |
222 | // Build the interface method table for this interface and this | |
223 | // object type: a list of function pointers for each interface | |
224 | // method. | |
225 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 226 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 227 | bool is_pointer = false; |
c0cab2ec | 228 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 229 | { |
230 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 231 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 232 | is_pointer = true; |
233 | } | |
c0cab2ec | 234 | if (rhs_named_type != NULL) |
2c809f8f | 235 | first_field = |
236 | rhs_named_type->interface_method_table(lhs_interface_type, | |
237 | is_pointer); | |
c0cab2ec | 238 | else if (rhs_struct_type != NULL) |
2c809f8f | 239 | first_field = |
240 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
241 | is_pointer); | |
c0cab2ec | 242 | else |
2c809f8f | 243 | first_field = Expression::make_nil(location); |
e440a328 | 244 | } |
e440a328 | 245 | |
2c809f8f | 246 | Expression* obj; |
e440a328 | 247 | if (rhs_type->points_to() != NULL) |
248 | { | |
2c809f8f | 249 | // We are assigning a pointer to the interface; the interface |
e440a328 | 250 | // holds the pointer itself. |
2c809f8f | 251 | obj = rhs; |
252 | } | |
253 | else | |
254 | { | |
255 | // We are assigning a non-pointer value to the interface; the | |
45ff893b | 256 | // interface gets a copy of the value in the heap if it escapes. |
257 | // TODO(cmang): Associate escape state state of RHS with newly | |
258 | // created OBJ. | |
2c809f8f | 259 | obj = Expression::make_heap_expression(rhs, location); |
e440a328 | 260 | } |
261 | ||
2c809f8f | 262 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
263 | } | |
e440a328 | 264 | |
2c809f8f | 265 | // Return an expression for the type descriptor of RHS. |
e440a328 | 266 | |
2c809f8f | 267 | Expression* |
268 | Expression::get_interface_type_descriptor(Expression* rhs) | |
269 | { | |
270 | go_assert(rhs->type()->interface_type() != NULL); | |
271 | Location location = rhs->location(); | |
e440a328 | 272 | |
2c809f8f | 273 | // The type descriptor is the first field of an empty interface. |
274 | if (rhs->type()->interface_type()->is_empty()) | |
275 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
276 | location); | |
277 | ||
278 | Expression* mtable = | |
279 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 280 | |
2c809f8f | 281 | Expression* descriptor = |
282 | Expression::make_unary(OPERATOR_MULT, mtable, location); | |
283 | descriptor = Expression::make_field_reference(descriptor, 0, location); | |
284 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 285 | |
2c809f8f | 286 | Expression* eq = |
287 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
288 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 289 | } |
290 | ||
2c809f8f | 291 | // Return an expression for the conversion of an interface type to an |
e440a328 | 292 | // interface type. |
293 | ||
2c809f8f | 294 | Expression* |
295 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
296 | bool for_type_guard, | |
297 | Location location) | |
e440a328 | 298 | { |
8ba8cc87 | 299 | if (Type::are_identical(lhs_type, rhs->type(), false, NULL)) |
300 | return rhs; | |
301 | ||
e440a328 | 302 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
303 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
304 | ||
e440a328 | 305 | // In the general case this requires runtime examination of the type |
306 | // method table to match it up with the interface methods. | |
307 | ||
308 | // FIXME: If all of the methods in the right hand side interface | |
309 | // also appear in the left hand side interface, then we don't need | |
310 | // to do a runtime check, although we still need to build a new | |
311 | // method table. | |
312 | ||
8ba8cc87 | 313 | // We are going to evaluate RHS multiple times. |
314 | go_assert(rhs->is_variable()); | |
315 | ||
e440a328 | 316 | // Get the type descriptor for the right hand side. This will be |
317 | // NULL for a nil interface. | |
2c809f8f | 318 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
319 | Expression* lhs_type_expr = | |
320 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 321 | |
2c809f8f | 322 | Expression* first_field; |
e440a328 | 323 | if (for_type_guard) |
324 | { | |
325 | // A type assertion fails when converting a nil interface. | |
6098d6cb | 326 | first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2, |
327 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 328 | } |
329 | else if (lhs_is_empty) | |
330 | { | |
2c809f8f | 331 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 332 | // first field is just the type descriptor of the object. |
2c809f8f | 333 | first_field = rhs_type_expr; |
e440a328 | 334 | } |
335 | else | |
336 | { | |
337 | // A conversion to a non-empty interface may fail, but unlike a | |
338 | // type assertion converting nil will always succeed. | |
6098d6cb | 339 | first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2, |
340 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 341 | } |
342 | ||
343 | // The second field is simply the object pointer. | |
2c809f8f | 344 | Expression* obj = |
345 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
346 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 347 | } |
348 | ||
2c809f8f | 349 | // Return an expression for the conversion of an interface type to a |
e440a328 | 350 | // non-interface type. |
351 | ||
2c809f8f | 352 | Expression* |
353 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
354 | Location location) | |
e440a328 | 355 | { |
8ba8cc87 | 356 | // We are going to evaluate RHS multiple times. |
357 | go_assert(rhs->is_variable()); | |
358 | ||
e440a328 | 359 | // Call a function to check that the type is valid. The function |
360 | // will panic with an appropriate runtime type error if the type is | |
361 | // not valid. | |
2c809f8f | 362 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
363 | location); | |
364 | Expression* rhs_descriptor = | |
365 | Expression::get_interface_type_descriptor(rhs); | |
366 | ||
367 | Type* rhs_type = rhs->type(); | |
368 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
369 | location); | |
370 | ||
6098d6cb | 371 | Expression* check_iface = Runtime::make_call(Runtime::ASSERTI2T, |
2c809f8f | 372 | location, 3, lhs_type_expr, |
373 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 374 | |
375 | // If the call succeeds, pull out the value. | |
2c809f8f | 376 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
377 | location); | |
e440a328 | 378 | |
379 | // If the value is a pointer, then it is the value we want. | |
380 | // Otherwise it points to the value. | |
381 | if (lhs_type->points_to() == NULL) | |
382 | { | |
2c809f8f | 383 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
384 | location); | |
385 | obj = Expression::make_unary(OPERATOR_MULT, obj, location); | |
e440a328 | 386 | } |
2c809f8f | 387 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 388 | } |
389 | ||
ea664253 | 390 | // Convert an expression to its backend representation. This is implemented by |
391 | // the child class. Not that it is not in general safe to call this multiple | |
e440a328 | 392 | // times for a single expression, but that we don't catch such errors. |
393 | ||
ea664253 | 394 | Bexpression* |
395 | Expression::get_backend(Translate_context* context) | |
e440a328 | 396 | { |
397 | // The child may have marked this expression as having an error. | |
398 | if (this->classification_ == EXPRESSION_ERROR) | |
ea664253 | 399 | return context->backend()->error_expression(); |
e440a328 | 400 | |
ea664253 | 401 | return this->do_get_backend(context); |
e440a328 | 402 | } |
403 | ||
48c2a53a | 404 | // Return a backend expression for VAL. |
405 | Bexpression* | |
406 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
407 | Numeric_constant* val) | |
e440a328 | 408 | { |
48c2a53a | 409 | Gogo* gogo = context->gogo(); |
410 | Type* type = val->type(); | |
411 | if (type == NULL) | |
412 | return gogo->backend()->error_expression(); | |
e440a328 | 413 | |
48c2a53a | 414 | Btype* btype = type->get_backend(gogo); |
415 | Bexpression* ret; | |
416 | if (type->integer_type() != NULL) | |
e440a328 | 417 | { |
418 | mpz_t ival; | |
48c2a53a | 419 | if (!val->to_int(&ival)) |
420 | { | |
421 | go_assert(saw_errors()); | |
422 | return gogo->backend()->error_expression(); | |
423 | } | |
424 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 425 | mpz_clear(ival); |
e440a328 | 426 | } |
48c2a53a | 427 | else if (type->float_type() != NULL) |
e440a328 | 428 | { |
48c2a53a | 429 | mpfr_t fval; |
430 | if (!val->to_float(&fval)) | |
431 | { | |
432 | go_assert(saw_errors()); | |
433 | return gogo->backend()->error_expression(); | |
434 | } | |
435 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
436 | mpfr_clear(fval); | |
e440a328 | 437 | } |
48c2a53a | 438 | else if (type->complex_type() != NULL) |
e440a328 | 439 | { |
fcbea5e4 | 440 | mpc_t cval; |
441 | if (!val->to_complex(&cval)) | |
48c2a53a | 442 | { |
443 | go_assert(saw_errors()); | |
444 | return gogo->backend()->error_expression(); | |
445 | } | |
fcbea5e4 | 446 | ret = gogo->backend()->complex_constant_expression(btype, cval); |
447 | mpc_clear(cval); | |
e440a328 | 448 | } |
449 | else | |
c3e6f413 | 450 | go_unreachable(); |
e440a328 | 451 | |
48c2a53a | 452 | return ret; |
e440a328 | 453 | } |
454 | ||
2c809f8f | 455 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
456 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 457 | |
2c809f8f | 458 | Expression* |
459 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 460 | { |
2c809f8f | 461 | Type* val_type = val->type(); |
462 | Type* bound_type = Type::lookup_integer_type("int"); | |
463 | ||
464 | int val_type_size; | |
465 | bool val_is_unsigned = false; | |
466 | if (val_type->integer_type() != NULL) | |
467 | { | |
468 | val_type_size = val_type->integer_type()->bits(); | |
469 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
470 | } | |
471 | else | |
472 | { | |
473 | if (!val_type->is_numeric_type() | |
474 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
475 | { | |
476 | go_assert(saw_errors()); | |
477 | return Expression::make_boolean(true, loc); | |
478 | } | |
e440a328 | 479 | |
2c809f8f | 480 | if (val_type->complex_type() != NULL) |
481 | val_type_size = val_type->complex_type()->bits(); | |
482 | else | |
483 | val_type_size = val_type->float_type()->bits(); | |
484 | } | |
485 | ||
486 | Expression* negative_index = Expression::make_boolean(false, loc); | |
487 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
488 | if (!val_is_unsigned) | |
e440a328 | 489 | { |
e67508fa | 490 | Expression* zero = Expression::make_integer_ul(0, val_type, loc); |
2c809f8f | 491 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); |
e440a328 | 492 | } |
493 | ||
2c809f8f | 494 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 495 | if (val_type_size > bound_type_size |
496 | || (val_type_size == bound_type_size | |
2c809f8f | 497 | && val_is_unsigned)) |
498 | { | |
499 | mpz_t one; | |
500 | mpz_init_set_ui(one, 1UL); | |
501 | ||
502 | // maxval = 2^(bound_type_size - 1) - 1 | |
503 | mpz_t maxval; | |
504 | mpz_init(maxval); | |
505 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
506 | mpz_sub_ui(maxval, maxval, 1); | |
e67508fa | 507 | Expression* max = Expression::make_integer_z(&maxval, val_type, loc); |
2c809f8f | 508 | mpz_clear(one); |
509 | mpz_clear(maxval); | |
510 | ||
511 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 512 | } |
513 | ||
2c809f8f | 514 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
515 | loc); | |
e440a328 | 516 | } |
517 | ||
d751bb78 | 518 | void |
519 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
520 | { | |
521 | this->do_dump_expression(ast_dump_context); | |
522 | } | |
523 | ||
e440a328 | 524 | // Error expressions. This are used to avoid cascading errors. |
525 | ||
526 | class Error_expression : public Expression | |
527 | { | |
528 | public: | |
b13c66cd | 529 | Error_expression(Location location) |
e440a328 | 530 | : Expression(EXPRESSION_ERROR, location) |
531 | { } | |
532 | ||
533 | protected: | |
534 | bool | |
535 | do_is_constant() const | |
536 | { return true; } | |
537 | ||
0e168074 | 538 | bool |
539 | do_is_immutable() const | |
540 | { return true; } | |
541 | ||
e440a328 | 542 | bool |
0c77715b | 543 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 544 | { |
0c77715b | 545 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 546 | return true; |
547 | } | |
548 | ||
4f2138d7 | 549 | bool |
e440a328 | 550 | do_discarding_value() |
4f2138d7 | 551 | { return true; } |
e440a328 | 552 | |
553 | Type* | |
554 | do_type() | |
555 | { return Type::make_error_type(); } | |
556 | ||
557 | void | |
558 | do_determine_type(const Type_context*) | |
559 | { } | |
560 | ||
561 | Expression* | |
562 | do_copy() | |
563 | { return this; } | |
564 | ||
565 | bool | |
566 | do_is_addressable() const | |
567 | { return true; } | |
568 | ||
ea664253 | 569 | Bexpression* |
570 | do_get_backend(Translate_context* context) | |
571 | { return context->backend()->error_expression(); } | |
d751bb78 | 572 | |
573 | void | |
574 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 575 | }; |
576 | ||
d751bb78 | 577 | // Dump the ast representation for an error expression to a dump context. |
578 | ||
579 | void | |
580 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
581 | { | |
582 | ast_dump_context->ostream() << "_Error_" ; | |
583 | } | |
584 | ||
e440a328 | 585 | Expression* |
b13c66cd | 586 | Expression::make_error(Location location) |
e440a328 | 587 | { |
588 | return new Error_expression(location); | |
589 | } | |
590 | ||
591 | // An expression which is really a type. This is used during parsing. | |
592 | // It is an error if these survive after lowering. | |
593 | ||
594 | class | |
595 | Type_expression : public Expression | |
596 | { | |
597 | public: | |
b13c66cd | 598 | Type_expression(Type* type, Location location) |
e440a328 | 599 | : Expression(EXPRESSION_TYPE, location), |
600 | type_(type) | |
601 | { } | |
602 | ||
603 | protected: | |
604 | int | |
605 | do_traverse(Traverse* traverse) | |
606 | { return Type::traverse(this->type_, traverse); } | |
607 | ||
608 | Type* | |
609 | do_type() | |
610 | { return this->type_; } | |
611 | ||
612 | void | |
613 | do_determine_type(const Type_context*) | |
614 | { } | |
615 | ||
616 | void | |
617 | do_check_types(Gogo*) | |
618 | { this->report_error(_("invalid use of type")); } | |
619 | ||
620 | Expression* | |
621 | do_copy() | |
622 | { return this; } | |
623 | ||
ea664253 | 624 | Bexpression* |
625 | do_get_backend(Translate_context*) | |
c3e6f413 | 626 | { go_unreachable(); } |
e440a328 | 627 | |
d751bb78 | 628 | void do_dump_expression(Ast_dump_context*) const; |
629 | ||
e440a328 | 630 | private: |
631 | // The type which we are representing as an expression. | |
632 | Type* type_; | |
633 | }; | |
634 | ||
d751bb78 | 635 | void |
636 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
637 | { | |
638 | ast_dump_context->dump_type(this->type_); | |
639 | } | |
640 | ||
e440a328 | 641 | Expression* |
b13c66cd | 642 | Expression::make_type(Type* type, Location location) |
e440a328 | 643 | { |
644 | return new Type_expression(type, location); | |
645 | } | |
646 | ||
e03bdf36 | 647 | // Class Parser_expression. |
648 | ||
649 | Type* | |
650 | Parser_expression::do_type() | |
651 | { | |
652 | // We should never really ask for the type of a Parser_expression. | |
653 | // However, it can happen, at least when we have an invalid const | |
654 | // whose initializer refers to the const itself. In that case we | |
655 | // may ask for the type when lowering the const itself. | |
c484d925 | 656 | go_assert(saw_errors()); |
e03bdf36 | 657 | return Type::make_error_type(); |
658 | } | |
659 | ||
e440a328 | 660 | // Class Var_expression. |
661 | ||
662 | // Lower a variable expression. Here we just make sure that the | |
663 | // initialization expression of the variable has been lowered. This | |
664 | // ensures that we will be able to determine the type of the variable | |
665 | // if necessary. | |
666 | ||
667 | Expression* | |
ceeb4318 | 668 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
669 | Statement_inserter* inserter, int) | |
e440a328 | 670 | { |
671 | if (this->variable_->is_variable()) | |
672 | { | |
673 | Variable* var = this->variable_->var_value(); | |
674 | // This is either a local variable or a global variable. A | |
675 | // reference to a variable which is local to an enclosing | |
676 | // function will be a reference to a field in a closure. | |
677 | if (var->is_global()) | |
ceeb4318 | 678 | { |
679 | function = NULL; | |
680 | inserter = NULL; | |
681 | } | |
682 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 683 | } |
684 | return this; | |
685 | } | |
686 | ||
e440a328 | 687 | // Return the type of a reference to a variable. |
688 | ||
689 | Type* | |
690 | Var_expression::do_type() | |
691 | { | |
692 | if (this->variable_->is_variable()) | |
693 | return this->variable_->var_value()->type(); | |
694 | else if (this->variable_->is_result_variable()) | |
695 | return this->variable_->result_var_value()->type(); | |
696 | else | |
c3e6f413 | 697 | go_unreachable(); |
e440a328 | 698 | } |
699 | ||
0ab09e06 | 700 | // Determine the type of a reference to a variable. |
701 | ||
702 | void | |
703 | Var_expression::do_determine_type(const Type_context*) | |
704 | { | |
705 | if (this->variable_->is_variable()) | |
706 | this->variable_->var_value()->determine_type(); | |
707 | } | |
708 | ||
e440a328 | 709 | // Something takes the address of this variable. This means that we |
710 | // may want to move the variable onto the heap. | |
711 | ||
712 | void | |
713 | Var_expression::do_address_taken(bool escapes) | |
714 | { | |
715 | if (!escapes) | |
f325319b | 716 | { |
717 | if (this->variable_->is_variable()) | |
718 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
719 | else if (this->variable_->is_result_variable()) | |
720 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
721 | else | |
722 | go_unreachable(); | |
723 | } | |
e440a328 | 724 | else |
f325319b | 725 | { |
726 | if (this->variable_->is_variable()) | |
727 | this->variable_->var_value()->set_address_taken(); | |
728 | else if (this->variable_->is_result_variable()) | |
729 | this->variable_->result_var_value()->set_address_taken(); | |
730 | else | |
731 | go_unreachable(); | |
732 | } | |
45ff893b | 733 | |
734 | if (this->variable_->is_variable() | |
735 | && this->variable_->var_value()->is_in_heap()) | |
736 | { | |
737 | Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP); | |
738 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
739 | } | |
e440a328 | 740 | } |
741 | ||
ea664253 | 742 | // Get the backend representation for a reference to a variable. |
e440a328 | 743 | |
ea664253 | 744 | Bexpression* |
745 | Var_expression::do_get_backend(Translate_context* context) | |
e440a328 | 746 | { |
fe2f84cf | 747 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
748 | context->function()); | |
fe2f84cf | 749 | bool is_in_heap; |
c6777780 | 750 | Location loc = this->location(); |
9b27b43c | 751 | Btype* btype; |
752 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 753 | if (this->variable_->is_variable()) |
9b27b43c | 754 | { |
755 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
756 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
757 | } | |
fe2f84cf | 758 | else if (this->variable_->is_result_variable()) |
9b27b43c | 759 | { |
760 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
761 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
762 | } | |
fe2f84cf | 763 | else |
c3e6f413 | 764 | go_unreachable(); |
c6777780 | 765 | |
766 | Bexpression* ret = context->backend()->var_expression(bvar, loc); | |
fe2f84cf | 767 | if (is_in_heap) |
9b27b43c | 768 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
ea664253 | 769 | return ret; |
e440a328 | 770 | } |
771 | ||
d751bb78 | 772 | // Ast dump for variable expression. |
773 | ||
774 | void | |
775 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
776 | { | |
777 | ast_dump_context->ostream() << this->variable_->name() ; | |
778 | } | |
779 | ||
e440a328 | 780 | // Make a reference to a variable in an expression. |
781 | ||
782 | Expression* | |
b13c66cd | 783 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 784 | { |
785 | if (var->is_sink()) | |
786 | return Expression::make_sink(location); | |
787 | ||
788 | // FIXME: Creating a new object for each reference to a variable is | |
789 | // wasteful. | |
790 | return new Var_expression(var, location); | |
791 | } | |
792 | ||
b0c09712 | 793 | // Class Enclosed_var_expression. |
794 | ||
795 | int | |
796 | Enclosed_var_expression::do_traverse(Traverse*) | |
797 | { | |
798 | return TRAVERSE_CONTINUE; | |
799 | } | |
800 | ||
801 | // Lower the reference to the enclosed variable. | |
802 | ||
803 | Expression* | |
804 | Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function, | |
805 | Statement_inserter* inserter, int) | |
806 | { | |
807 | gogo->lower_expression(function, inserter, &this->reference_); | |
808 | return this; | |
809 | } | |
810 | ||
811 | // Flatten the reference to the enclosed variable. | |
812 | ||
813 | Expression* | |
814 | Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function, | |
815 | Statement_inserter* inserter) | |
816 | { | |
817 | gogo->flatten_expression(function, inserter, &this->reference_); | |
818 | return this; | |
819 | } | |
820 | ||
821 | void | |
822 | Enclosed_var_expression::do_address_taken(bool escapes) | |
823 | { | |
824 | if (!escapes) | |
825 | { | |
826 | if (this->variable_->is_variable()) | |
827 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
828 | else if (this->variable_->is_result_variable()) | |
829 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
830 | else | |
831 | go_unreachable(); | |
832 | } | |
833 | else | |
834 | { | |
835 | if (this->variable_->is_variable()) | |
836 | this->variable_->var_value()->set_address_taken(); | |
837 | else if (this->variable_->is_result_variable()) | |
838 | this->variable_->result_var_value()->set_address_taken(); | |
839 | else | |
840 | go_unreachable(); | |
841 | } | |
45ff893b | 842 | |
843 | if (this->variable_->is_variable() | |
844 | && this->variable_->var_value()->is_in_heap()) | |
845 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
b0c09712 | 846 | } |
847 | ||
848 | // Ast dump for enclosed variable expression. | |
849 | ||
850 | void | |
851 | Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const | |
852 | { | |
853 | adc->ostream() << this->variable_->name(); | |
854 | } | |
855 | ||
856 | // Make a reference to a variable within an enclosing function. | |
857 | ||
858 | Expression* | |
859 | Expression::make_enclosing_var_reference(Expression* reference, | |
860 | Named_object* var, Location location) | |
861 | { | |
862 | return new Enclosed_var_expression(reference, var, location); | |
863 | } | |
864 | ||
e440a328 | 865 | // Class Temporary_reference_expression. |
866 | ||
867 | // The type. | |
868 | ||
869 | Type* | |
870 | Temporary_reference_expression::do_type() | |
871 | { | |
872 | return this->statement_->type(); | |
873 | } | |
874 | ||
875 | // Called if something takes the address of this temporary variable. | |
876 | // We never have to move temporary variables to the heap, but we do | |
877 | // need to know that they must live in the stack rather than in a | |
878 | // register. | |
879 | ||
880 | void | |
881 | Temporary_reference_expression::do_address_taken(bool) | |
882 | { | |
883 | this->statement_->set_is_address_taken(); | |
884 | } | |
885 | ||
ea664253 | 886 | // Get a backend expression referring to the variable. |
e440a328 | 887 | |
ea664253 | 888 | Bexpression* |
889 | Temporary_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 890 | { |
cd440cff | 891 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 892 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
cd440cff | 893 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 894 | |
cd440cff | 895 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 896 | // that the Go frontend can. In some cases this means that a |
897 | // temporary variable won't have the right backend type. Correct | |
898 | // that here by adding a type cast. We need to use base() to push | |
899 | // the circularity down one level. | |
cd440cff | 900 | Type* stype = this->statement_->type(); |
ceeb4318 | 901 | if (!this->is_lvalue_ |
cd440cff | 902 | && stype->has_pointer() |
903 | && stype->deref()->is_void_type()) | |
eefc1ed3 | 904 | { |
cd440cff | 905 | Btype* btype = this->type()->base()->get_backend(gogo); |
906 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 907 | } |
ea664253 | 908 | return ret; |
e440a328 | 909 | } |
910 | ||
d751bb78 | 911 | // Ast dump for temporary reference. |
912 | ||
913 | void | |
914 | Temporary_reference_expression::do_dump_expression( | |
915 | Ast_dump_context* ast_dump_context) const | |
916 | { | |
917 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
918 | } | |
919 | ||
e440a328 | 920 | // Make a reference to a temporary variable. |
921 | ||
ceeb4318 | 922 | Temporary_reference_expression* |
e440a328 | 923 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 924 | Location location) |
e440a328 | 925 | { |
926 | return new Temporary_reference_expression(statement, location); | |
927 | } | |
928 | ||
e9d3367e | 929 | // Class Set_and_use_temporary_expression. |
930 | ||
931 | // Return the type. | |
932 | ||
933 | Type* | |
934 | Set_and_use_temporary_expression::do_type() | |
935 | { | |
936 | return this->statement_->type(); | |
937 | } | |
938 | ||
0afbb937 | 939 | // Determine the type of the expression. |
940 | ||
941 | void | |
942 | Set_and_use_temporary_expression::do_determine_type( | |
943 | const Type_context* context) | |
944 | { | |
945 | this->expr_->determine_type(context); | |
946 | } | |
947 | ||
e9d3367e | 948 | // Take the address. |
949 | ||
950 | void | |
951 | Set_and_use_temporary_expression::do_address_taken(bool) | |
952 | { | |
953 | this->statement_->set_is_address_taken(); | |
954 | } | |
955 | ||
956 | // Return the backend representation. | |
957 | ||
ea664253 | 958 | Bexpression* |
959 | Set_and_use_temporary_expression::do_get_backend(Translate_context* context) | |
e9d3367e | 960 | { |
e9d3367e | 961 | Location loc = this->location(); |
a302c105 | 962 | Gogo* gogo = context->gogo(); |
963 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
964 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); | |
965 | ||
ea664253 | 966 | Bexpression* bexpr = this->expr_->get_backend(context); |
a302c105 | 967 | Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc); |
968 | var_ref = gogo->backend()->var_expression(bvar, loc); | |
969 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); | |
ea664253 | 970 | return ret; |
e9d3367e | 971 | } |
972 | ||
973 | // Dump. | |
974 | ||
975 | void | |
976 | Set_and_use_temporary_expression::do_dump_expression( | |
977 | Ast_dump_context* ast_dump_context) const | |
978 | { | |
979 | ast_dump_context->ostream() << '('; | |
980 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
981 | ast_dump_context->ostream() << " = "; | |
982 | this->expr_->dump_expression(ast_dump_context); | |
983 | ast_dump_context->ostream() << ')'; | |
984 | } | |
985 | ||
986 | // Make a set-and-use temporary. | |
987 | ||
988 | Set_and_use_temporary_expression* | |
989 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
990 | Expression* expr, Location location) | |
991 | { | |
992 | return new Set_and_use_temporary_expression(statement, expr, location); | |
993 | } | |
994 | ||
e440a328 | 995 | // A sink expression--a use of the blank identifier _. |
996 | ||
997 | class Sink_expression : public Expression | |
998 | { | |
999 | public: | |
b13c66cd | 1000 | Sink_expression(Location location) |
e440a328 | 1001 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 1002 | type_(NULL), bvar_(NULL) |
e440a328 | 1003 | { } |
1004 | ||
1005 | protected: | |
4f2138d7 | 1006 | bool |
e440a328 | 1007 | do_discarding_value() |
4f2138d7 | 1008 | { return true; } |
e440a328 | 1009 | |
1010 | Type* | |
1011 | do_type(); | |
1012 | ||
1013 | void | |
1014 | do_determine_type(const Type_context*); | |
1015 | ||
1016 | Expression* | |
1017 | do_copy() | |
1018 | { return new Sink_expression(this->location()); } | |
1019 | ||
ea664253 | 1020 | Bexpression* |
1021 | do_get_backend(Translate_context*); | |
e440a328 | 1022 | |
d751bb78 | 1023 | void |
1024 | do_dump_expression(Ast_dump_context*) const; | |
1025 | ||
e440a328 | 1026 | private: |
1027 | // The type of this sink variable. | |
1028 | Type* type_; | |
1029 | // The temporary variable we generate. | |
aa93217a | 1030 | Bvariable* bvar_; |
e440a328 | 1031 | }; |
1032 | ||
1033 | // Return the type of a sink expression. | |
1034 | ||
1035 | Type* | |
1036 | Sink_expression::do_type() | |
1037 | { | |
1038 | if (this->type_ == NULL) | |
1039 | return Type::make_sink_type(); | |
1040 | return this->type_; | |
1041 | } | |
1042 | ||
1043 | // Determine the type of a sink expression. | |
1044 | ||
1045 | void | |
1046 | Sink_expression::do_determine_type(const Type_context* context) | |
1047 | { | |
1048 | if (context->type != NULL) | |
1049 | this->type_ = context->type; | |
1050 | } | |
1051 | ||
1052 | // Return a temporary variable for a sink expression. This will | |
1053 | // presumably be a write-only variable which the middle-end will drop. | |
1054 | ||
ea664253 | 1055 | Bexpression* |
1056 | Sink_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1057 | { |
aa93217a | 1058 | Location loc = this->location(); |
1059 | Gogo* gogo = context->gogo(); | |
1060 | if (this->bvar_ == NULL) | |
e440a328 | 1061 | { |
c484d925 | 1062 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 1063 | Named_object* fn = context->function(); |
1064 | go_assert(fn != NULL); | |
1065 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 1066 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 1067 | Bstatement* decl; |
1068 | this->bvar_ = | |
1069 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
1070 | false, loc, &decl); | |
1071 | Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc); | |
1072 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); | |
ea664253 | 1073 | return var_ref; |
e440a328 | 1074 | } |
ea664253 | 1075 | return gogo->backend()->var_expression(this->bvar_, loc); |
e440a328 | 1076 | } |
1077 | ||
d751bb78 | 1078 | // Ast dump for sink expression. |
1079 | ||
1080 | void | |
1081 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1082 | { | |
1083 | ast_dump_context->ostream() << "_" ; | |
1084 | } | |
1085 | ||
e440a328 | 1086 | // Make a sink expression. |
1087 | ||
1088 | Expression* | |
b13c66cd | 1089 | Expression::make_sink(Location location) |
e440a328 | 1090 | { |
1091 | return new Sink_expression(location); | |
1092 | } | |
1093 | ||
1094 | // Class Func_expression. | |
1095 | ||
1096 | // FIXME: Can a function expression appear in a constant expression? | |
1097 | // The value is unchanging. Initializing a constant to the address of | |
1098 | // a function seems like it could work, though there might be little | |
1099 | // point to it. | |
1100 | ||
e440a328 | 1101 | // Traversal. |
1102 | ||
1103 | int | |
1104 | Func_expression::do_traverse(Traverse* traverse) | |
1105 | { | |
1106 | return (this->closure_ == NULL | |
1107 | ? TRAVERSE_CONTINUE | |
1108 | : Expression::traverse(&this->closure_, traverse)); | |
1109 | } | |
1110 | ||
1111 | // Return the type of a function expression. | |
1112 | ||
1113 | Type* | |
1114 | Func_expression::do_type() | |
1115 | { | |
1116 | if (this->function_->is_function()) | |
1117 | return this->function_->func_value()->type(); | |
1118 | else if (this->function_->is_function_declaration()) | |
1119 | return this->function_->func_declaration_value()->type(); | |
1120 | else | |
c3e6f413 | 1121 | go_unreachable(); |
e440a328 | 1122 | } |
1123 | ||
ea664253 | 1124 | // Get the backend representation for the code of a function expression. |
e440a328 | 1125 | |
97267c39 | 1126 | Bexpression* |
8381eda7 | 1127 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1128 | { |
1129 | Function_type* fntype; | |
8381eda7 | 1130 | if (no->is_function()) |
1131 | fntype = no->func_value()->type(); | |
1132 | else if (no->is_function_declaration()) | |
1133 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1134 | else |
c3e6f413 | 1135 | go_unreachable(); |
e440a328 | 1136 | |
1137 | // Builtin functions are handled specially by Call_expression. We | |
1138 | // can't take their address. | |
1139 | if (fntype->is_builtin()) | |
1140 | { | |
631d5788 | 1141 | go_error_at(loc, |
1142 | "invalid use of special builtin function %qs; must be called", | |
1143 | no->message_name().c_str()); | |
97267c39 | 1144 | return gogo->backend()->error_expression(); |
e440a328 | 1145 | } |
1146 | ||
97267c39 | 1147 | Bfunction* fndecl; |
e440a328 | 1148 | if (no->is_function()) |
cf3cae55 | 1149 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1150 | else if (no->is_function_declaration()) |
cf3cae55 | 1151 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1152 | else |
c3e6f413 | 1153 | go_unreachable(); |
e440a328 | 1154 | |
97267c39 | 1155 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1156 | } |
1157 | ||
ea664253 | 1158 | // Get the backend representation for a function expression. This is used when |
1159 | // we take the address of a function rather than simply calling it. A func | |
8381eda7 | 1160 | // value is represented as a pointer to a block of memory. The first |
1161 | // word of that memory is a pointer to the function code. The | |
1162 | // remaining parts of that memory are the addresses of variables that | |
1163 | // the function closes over. | |
e440a328 | 1164 | |
ea664253 | 1165 | Bexpression* |
1166 | Func_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1167 | { |
8381eda7 | 1168 | // If there is no closure, just use the function descriptor. |
2010c17a | 1169 | if (this->closure_ == NULL) |
8381eda7 | 1170 | { |
1171 | Gogo* gogo = context->gogo(); | |
1172 | Named_object* no = this->function_; | |
1173 | Expression* descriptor; | |
1174 | if (no->is_function()) | |
1175 | descriptor = no->func_value()->descriptor(gogo, no); | |
1176 | else if (no->is_function_declaration()) | |
1177 | { | |
1178 | if (no->func_declaration_value()->type()->is_builtin()) | |
1179 | { | |
631d5788 | 1180 | go_error_at(this->location(), |
1181 | ("invalid use of special builtin function %qs; " | |
1182 | "must be called"), | |
1183 | no->message_name().c_str()); | |
ea664253 | 1184 | return gogo->backend()->error_expression(); |
8381eda7 | 1185 | } |
1186 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1187 | } | |
1188 | else | |
1189 | go_unreachable(); | |
2010c17a | 1190 | |
ea664253 | 1191 | Bexpression* bdesc = descriptor->get_backend(context); |
1192 | return gogo->backend()->address_expression(bdesc, this->location()); | |
8381eda7 | 1193 | } |
e440a328 | 1194 | |
8381eda7 | 1195 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1196 | |
8381eda7 | 1197 | // If there is a closure, then the closure is itself the function |
1198 | // expression. It is a pointer to a struct whose first field points | |
1199 | // to the function code and whose remaining fields are the addresses | |
1200 | // of the closed-over variables. | |
ea664253 | 1201 | return this->closure_->get_backend(context); |
e440a328 | 1202 | } |
1203 | ||
d751bb78 | 1204 | // Ast dump for function. |
1205 | ||
1206 | void | |
1207 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1208 | { | |
8b1c301d | 1209 | ast_dump_context->ostream() << this->function_->name(); |
1210 | if (this->closure_ != NULL) | |
1211 | { | |
1212 | ast_dump_context->ostream() << " {closure = "; | |
1213 | this->closure_->dump_expression(ast_dump_context); | |
1214 | ast_dump_context->ostream() << "}"; | |
1215 | } | |
d751bb78 | 1216 | } |
1217 | ||
e440a328 | 1218 | // Make a reference to a function in an expression. |
1219 | ||
1220 | Expression* | |
1221 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1222 | Location location) |
e440a328 | 1223 | { |
b1d7ecfa | 1224 | Func_expression* fe = new Func_expression(function, closure, location); |
1225 | ||
1226 | // Detect references to builtin functions and set the runtime code if | |
1227 | // appropriate. | |
1228 | if (function->is_function_declaration()) | |
1229 | fe->set_runtime_code(Runtime::name_to_code(function->name())); | |
1230 | return fe; | |
e440a328 | 1231 | } |
1232 | ||
c6837989 | 1233 | // Class Func_descriptor_expression. |
8381eda7 | 1234 | |
c6837989 | 1235 | // Constructor. |
8381eda7 | 1236 | |
c6837989 | 1237 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1238 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1239 | fn_(fn), dvar_(NULL) |
c6837989 | 1240 | { |
1241 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1242 | } | |
8381eda7 | 1243 | |
c6837989 | 1244 | // Traversal. |
8381eda7 | 1245 | |
c6837989 | 1246 | int |
1247 | Func_descriptor_expression::do_traverse(Traverse*) | |
1248 | { | |
1249 | return TRAVERSE_CONTINUE; | |
1250 | } | |
8381eda7 | 1251 | |
1252 | // All function descriptors have the same type. | |
1253 | ||
1254 | Type* Func_descriptor_expression::descriptor_type; | |
1255 | ||
1256 | void | |
1257 | Func_descriptor_expression::make_func_descriptor_type() | |
1258 | { | |
1259 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1260 | return; | |
1261 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1262 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1263 | Func_descriptor_expression::descriptor_type = | |
1264 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1265 | } | |
1266 | ||
1267 | Type* | |
1268 | Func_descriptor_expression::do_type() | |
1269 | { | |
1270 | Func_descriptor_expression::make_func_descriptor_type(); | |
1271 | return Func_descriptor_expression::descriptor_type; | |
1272 | } | |
1273 | ||
ea664253 | 1274 | // The backend representation for a function descriptor. |
8381eda7 | 1275 | |
ea664253 | 1276 | Bexpression* |
1277 | Func_descriptor_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1278 | { |
8381eda7 | 1279 | Named_object* no = this->fn_; |
1280 | Location loc = no->location(); | |
ea664253 | 1281 | if (this->dvar_ != NULL) |
1282 | return context->backend()->var_expression(this->dvar_, loc); | |
8381eda7 | 1283 | |
ea664253 | 1284 | Gogo* gogo = context->gogo(); |
8381eda7 | 1285 | std::string var_name; |
09e57698 | 1286 | bool is_descriptor = false; |
1287 | if (no->is_function_declaration() | |
1288 | && !no->func_declaration_value()->asm_name().empty() | |
1289 | && Linemap::is_predeclared_location(no->location())) | |
1290 | { | |
6098d6cb | 1291 | if (no->func_declaration_value()->asm_name().substr(0, 8) != "runtime.") |
1292 | var_name = no->func_declaration_value()->asm_name() + "_descriptor"; | |
1293 | else | |
1294 | var_name = no->func_declaration_value()->asm_name() + "$descriptor"; | |
09e57698 | 1295 | is_descriptor = true; |
1296 | } | |
8381eda7 | 1297 | else |
09e57698 | 1298 | { |
1299 | if (no->package() == NULL) | |
1300 | var_name = gogo->pkgpath_symbol(); | |
1301 | else | |
1302 | var_name = no->package()->pkgpath_symbol(); | |
1303 | var_name.push_back('.'); | |
1304 | var_name.append(Gogo::unpack_hidden_name(no->name())); | |
1305 | var_name.append("$descriptor"); | |
1306 | } | |
8381eda7 | 1307 | |
1308 | Btype* btype = this->type()->get_backend(gogo); | |
1309 | ||
1310 | Bvariable* bvar; | |
09e57698 | 1311 | if (no->package() != NULL || is_descriptor) |
f8bdf81a | 1312 | bvar = context->backend()->immutable_struct_reference(var_name, btype, |
1313 | loc); | |
8381eda7 | 1314 | else |
1315 | { | |
1316 | Location bloc = Linemap::predeclared_location(); | |
1317 | bool is_hidden = ((no->is_function() | |
1318 | && no->func_value()->enclosing() != NULL) | |
1319 | || Gogo::is_thunk(no)); | |
1320 | bvar = context->backend()->immutable_struct(var_name, is_hidden, false, | |
1321 | btype, bloc); | |
1322 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1323 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1324 | Expression* init = |
1325 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1326 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1327 | bcontext.set_is_const(); | |
ea664253 | 1328 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1329 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1330 | false, btype, bloc, binit); | |
1331 | } | |
1332 | ||
1333 | this->dvar_ = bvar; | |
ea664253 | 1334 | return gogo->backend()->var_expression(bvar, loc); |
8381eda7 | 1335 | } |
1336 | ||
c6837989 | 1337 | // Print a function descriptor expression. |
1338 | ||
1339 | void | |
1340 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1341 | { | |
1342 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1343 | } | |
1344 | ||
8381eda7 | 1345 | // Make a function descriptor expression. |
1346 | ||
c6837989 | 1347 | Func_descriptor_expression* |
1348 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1349 | { |
c6837989 | 1350 | return new Func_descriptor_expression(fn); |
8381eda7 | 1351 | } |
1352 | ||
1353 | // Make the function descriptor type, so that it can be converted. | |
1354 | ||
1355 | void | |
1356 | Expression::make_func_descriptor_type() | |
1357 | { | |
1358 | Func_descriptor_expression::make_func_descriptor_type(); | |
1359 | } | |
1360 | ||
1361 | // A reference to just the code of a function. | |
1362 | ||
1363 | class Func_code_reference_expression : public Expression | |
1364 | { | |
1365 | public: | |
1366 | Func_code_reference_expression(Named_object* function, Location location) | |
1367 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1368 | function_(function) | |
1369 | { } | |
1370 | ||
1371 | protected: | |
1372 | int | |
1373 | do_traverse(Traverse*) | |
1374 | { return TRAVERSE_CONTINUE; } | |
1375 | ||
f9ca30f9 | 1376 | bool |
1377 | do_is_immutable() const | |
1378 | { return true; } | |
1379 | ||
8381eda7 | 1380 | Type* |
1381 | do_type() | |
1382 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1383 | ||
1384 | void | |
1385 | do_determine_type(const Type_context*) | |
1386 | { } | |
1387 | ||
1388 | Expression* | |
1389 | do_copy() | |
1390 | { | |
1391 | return Expression::make_func_code_reference(this->function_, | |
1392 | this->location()); | |
1393 | } | |
1394 | ||
ea664253 | 1395 | Bexpression* |
1396 | do_get_backend(Translate_context*); | |
8381eda7 | 1397 | |
1398 | void | |
1399 | do_dump_expression(Ast_dump_context* context) const | |
1400 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1401 | ||
1402 | private: | |
1403 | // The function. | |
1404 | Named_object* function_; | |
1405 | }; | |
1406 | ||
ea664253 | 1407 | // Get the backend representation for a reference to function code. |
8381eda7 | 1408 | |
ea664253 | 1409 | Bexpression* |
1410 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1411 | { |
ea664253 | 1412 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1413 | this->location()); | |
8381eda7 | 1414 | } |
1415 | ||
1416 | // Make a reference to the code of a function. | |
1417 | ||
1418 | Expression* | |
1419 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1420 | { | |
1421 | return new Func_code_reference_expression(function, location); | |
1422 | } | |
1423 | ||
e440a328 | 1424 | // Class Unknown_expression. |
1425 | ||
1426 | // Return the name of an unknown expression. | |
1427 | ||
1428 | const std::string& | |
1429 | Unknown_expression::name() const | |
1430 | { | |
1431 | return this->named_object_->name(); | |
1432 | } | |
1433 | ||
1434 | // Lower a reference to an unknown name. | |
1435 | ||
1436 | Expression* | |
ceeb4318 | 1437 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1438 | { |
b13c66cd | 1439 | Location location = this->location(); |
e440a328 | 1440 | Named_object* no = this->named_object_; |
deded542 | 1441 | Named_object* real; |
1442 | if (!no->is_unknown()) | |
1443 | real = no; | |
1444 | else | |
e440a328 | 1445 | { |
deded542 | 1446 | real = no->unknown_value()->real_named_object(); |
1447 | if (real == NULL) | |
1448 | { | |
1449 | if (this->is_composite_literal_key_) | |
1450 | return this; | |
acf8e158 | 1451 | if (!this->no_error_message_) |
631d5788 | 1452 | go_error_at(location, "reference to undefined name %qs", |
1453 | this->named_object_->message_name().c_str()); | |
deded542 | 1454 | return Expression::make_error(location); |
1455 | } | |
e440a328 | 1456 | } |
1457 | switch (real->classification()) | |
1458 | { | |
1459 | case Named_object::NAMED_OBJECT_CONST: | |
1460 | return Expression::make_const_reference(real, location); | |
1461 | case Named_object::NAMED_OBJECT_TYPE: | |
1462 | return Expression::make_type(real->type_value(), location); | |
1463 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1464 | if (this->is_composite_literal_key_) | |
1465 | return this; | |
acf8e158 | 1466 | if (!this->no_error_message_) |
631d5788 | 1467 | go_error_at(location, "reference to undefined type %qs", |
1468 | real->message_name().c_str()); | |
e440a328 | 1469 | return Expression::make_error(location); |
1470 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1471 | real->var_value()->set_is_used(); |
e440a328 | 1472 | return Expression::make_var_reference(real, location); |
1473 | case Named_object::NAMED_OBJECT_FUNC: | |
1474 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1475 | return Expression::make_func_reference(real, NULL, location); | |
1476 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1477 | if (this->is_composite_literal_key_) | |
1478 | return this; | |
acf8e158 | 1479 | if (!this->no_error_message_) |
631d5788 | 1480 | go_error_at(location, "unexpected reference to package"); |
e440a328 | 1481 | return Expression::make_error(location); |
1482 | default: | |
c3e6f413 | 1483 | go_unreachable(); |
e440a328 | 1484 | } |
1485 | } | |
1486 | ||
d751bb78 | 1487 | // Dump the ast representation for an unknown expression to a dump context. |
1488 | ||
1489 | void | |
1490 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1491 | { | |
1492 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1493 | << ")"; | |
d751bb78 | 1494 | } |
1495 | ||
e440a328 | 1496 | // Make a reference to an unknown name. |
1497 | ||
acf8e158 | 1498 | Unknown_expression* |
b13c66cd | 1499 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1500 | { |
e440a328 | 1501 | return new Unknown_expression(no, location); |
1502 | } | |
1503 | ||
1504 | // A boolean expression. | |
1505 | ||
1506 | class Boolean_expression : public Expression | |
1507 | { | |
1508 | public: | |
b13c66cd | 1509 | Boolean_expression(bool val, Location location) |
e440a328 | 1510 | : Expression(EXPRESSION_BOOLEAN, location), |
1511 | val_(val), type_(NULL) | |
1512 | { } | |
1513 | ||
1514 | static Expression* | |
1515 | do_import(Import*); | |
1516 | ||
1517 | protected: | |
1518 | bool | |
1519 | do_is_constant() const | |
1520 | { return true; } | |
1521 | ||
0e168074 | 1522 | bool |
1523 | do_is_immutable() const | |
1524 | { return true; } | |
1525 | ||
e440a328 | 1526 | Type* |
1527 | do_type(); | |
1528 | ||
1529 | void | |
1530 | do_determine_type(const Type_context*); | |
1531 | ||
1532 | Expression* | |
1533 | do_copy() | |
1534 | { return this; } | |
1535 | ||
ea664253 | 1536 | Bexpression* |
1537 | do_get_backend(Translate_context* context) | |
1538 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1539 | |
1540 | void | |
1541 | do_export(Export* exp) const | |
1542 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1543 | ||
d751bb78 | 1544 | void |
1545 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1546 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1547 | ||
e440a328 | 1548 | private: |
1549 | // The constant. | |
1550 | bool val_; | |
1551 | // The type as determined by context. | |
1552 | Type* type_; | |
1553 | }; | |
1554 | ||
1555 | // Get the type. | |
1556 | ||
1557 | Type* | |
1558 | Boolean_expression::do_type() | |
1559 | { | |
1560 | if (this->type_ == NULL) | |
1561 | this->type_ = Type::make_boolean_type(); | |
1562 | return this->type_; | |
1563 | } | |
1564 | ||
1565 | // Set the type from the context. | |
1566 | ||
1567 | void | |
1568 | Boolean_expression::do_determine_type(const Type_context* context) | |
1569 | { | |
1570 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1571 | ; | |
1572 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1573 | this->type_ = context->type; | |
1574 | else if (!context->may_be_abstract) | |
1575 | this->type_ = Type::lookup_bool_type(); | |
1576 | } | |
1577 | ||
1578 | // Import a boolean constant. | |
1579 | ||
1580 | Expression* | |
1581 | Boolean_expression::do_import(Import* imp) | |
1582 | { | |
1583 | if (imp->peek_char() == 't') | |
1584 | { | |
1585 | imp->require_c_string("true"); | |
1586 | return Expression::make_boolean(true, imp->location()); | |
1587 | } | |
1588 | else | |
1589 | { | |
1590 | imp->require_c_string("false"); | |
1591 | return Expression::make_boolean(false, imp->location()); | |
1592 | } | |
1593 | } | |
1594 | ||
1595 | // Make a boolean expression. | |
1596 | ||
1597 | Expression* | |
b13c66cd | 1598 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1599 | { |
1600 | return new Boolean_expression(val, location); | |
1601 | } | |
1602 | ||
1603 | // Class String_expression. | |
1604 | ||
1605 | // Get the type. | |
1606 | ||
1607 | Type* | |
1608 | String_expression::do_type() | |
1609 | { | |
1610 | if (this->type_ == NULL) | |
1611 | this->type_ = Type::make_string_type(); | |
1612 | return this->type_; | |
1613 | } | |
1614 | ||
1615 | // Set the type from the context. | |
1616 | ||
1617 | void | |
1618 | String_expression::do_determine_type(const Type_context* context) | |
1619 | { | |
1620 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1621 | ; | |
1622 | else if (context->type != NULL && context->type->is_string_type()) | |
1623 | this->type_ = context->type; | |
1624 | else if (!context->may_be_abstract) | |
1625 | this->type_ = Type::lookup_string_type(); | |
1626 | } | |
1627 | ||
1628 | // Build a string constant. | |
1629 | ||
ea664253 | 1630 | Bexpression* |
1631 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1632 | { |
2c809f8f | 1633 | Gogo* gogo = context->gogo(); |
1634 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1635 | ||
1636 | Location loc = this->location(); | |
1637 | std::vector<Bexpression*> init(2); | |
1638 | Bexpression* str_cst = | |
1639 | gogo->backend()->string_constant_expression(this->val_); | |
1640 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1641 | ||
1642 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1643 | mpz_t lenval; | |
1644 | mpz_init_set_ui(lenval, this->val_.length()); | |
1645 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1646 | mpz_clear(lenval); | |
1647 | ||
ea664253 | 1648 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1649 | } |
1650 | ||
8b1c301d | 1651 | // Write string literal to string dump. |
e440a328 | 1652 | |
1653 | void | |
8b1c301d | 1654 | String_expression::export_string(String_dump* exp, |
1655 | const String_expression* str) | |
e440a328 | 1656 | { |
1657 | std::string s; | |
8b1c301d | 1658 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1659 | s += '"'; |
8b1c301d | 1660 | for (std::string::const_iterator p = str->val_.begin(); |
1661 | p != str->val_.end(); | |
e440a328 | 1662 | ++p) |
1663 | { | |
1664 | if (*p == '\\' || *p == '"') | |
1665 | { | |
1666 | s += '\\'; | |
1667 | s += *p; | |
1668 | } | |
1669 | else if (*p >= 0x20 && *p < 0x7f) | |
1670 | s += *p; | |
1671 | else if (*p == '\n') | |
1672 | s += "\\n"; | |
1673 | else if (*p == '\t') | |
1674 | s += "\\t"; | |
1675 | else | |
1676 | { | |
1677 | s += "\\x"; | |
1678 | unsigned char c = *p; | |
1679 | unsigned int dig = c >> 4; | |
1680 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1681 | dig = c & 0xf; | |
1682 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1683 | } | |
1684 | } | |
1685 | s += '"'; | |
1686 | exp->write_string(s); | |
1687 | } | |
1688 | ||
8b1c301d | 1689 | // Export a string expression. |
1690 | ||
1691 | void | |
1692 | String_expression::do_export(Export* exp) const | |
1693 | { | |
1694 | String_expression::export_string(exp, this); | |
1695 | } | |
1696 | ||
e440a328 | 1697 | // Import a string expression. |
1698 | ||
1699 | Expression* | |
1700 | String_expression::do_import(Import* imp) | |
1701 | { | |
1702 | imp->require_c_string("\""); | |
1703 | std::string val; | |
1704 | while (true) | |
1705 | { | |
1706 | int c = imp->get_char(); | |
1707 | if (c == '"' || c == -1) | |
1708 | break; | |
1709 | if (c != '\\') | |
1710 | val += static_cast<char>(c); | |
1711 | else | |
1712 | { | |
1713 | c = imp->get_char(); | |
1714 | if (c == '\\' || c == '"') | |
1715 | val += static_cast<char>(c); | |
1716 | else if (c == 'n') | |
1717 | val += '\n'; | |
1718 | else if (c == 't') | |
1719 | val += '\t'; | |
1720 | else if (c == 'x') | |
1721 | { | |
1722 | c = imp->get_char(); | |
1723 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1724 | c = imp->get_char(); | |
1725 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1726 | char v = (vh << 4) | vl; | |
1727 | val += v; | |
1728 | } | |
1729 | else | |
1730 | { | |
631d5788 | 1731 | go_error_at(imp->location(), "bad string constant"); |
e440a328 | 1732 | return Expression::make_error(imp->location()); |
1733 | } | |
1734 | } | |
1735 | } | |
1736 | return Expression::make_string(val, imp->location()); | |
1737 | } | |
1738 | ||
d751bb78 | 1739 | // Ast dump for string expression. |
1740 | ||
1741 | void | |
1742 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1743 | { | |
8b1c301d | 1744 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1745 | } |
1746 | ||
e440a328 | 1747 | // Make a string expression. |
1748 | ||
1749 | Expression* | |
b13c66cd | 1750 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1751 | { |
1752 | return new String_expression(val, location); | |
1753 | } | |
1754 | ||
2c809f8f | 1755 | // An expression that evaluates to some characteristic of a string. |
1756 | // This is used when indexing, bound-checking, or nil checking a string. | |
1757 | ||
1758 | class String_info_expression : public Expression | |
1759 | { | |
1760 | public: | |
1761 | String_info_expression(Expression* string, String_info string_info, | |
1762 | Location location) | |
1763 | : Expression(EXPRESSION_STRING_INFO, location), | |
1764 | string_(string), string_info_(string_info) | |
1765 | { } | |
1766 | ||
1767 | protected: | |
1768 | Type* | |
1769 | do_type(); | |
1770 | ||
1771 | void | |
1772 | do_determine_type(const Type_context*) | |
1773 | { go_unreachable(); } | |
1774 | ||
1775 | Expression* | |
1776 | do_copy() | |
1777 | { | |
1778 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1779 | this->location()); | |
1780 | } | |
1781 | ||
ea664253 | 1782 | Bexpression* |
1783 | do_get_backend(Translate_context* context); | |
2c809f8f | 1784 | |
1785 | void | |
1786 | do_dump_expression(Ast_dump_context*) const; | |
1787 | ||
1788 | void | |
1789 | do_issue_nil_check() | |
1790 | { this->string_->issue_nil_check(); } | |
1791 | ||
1792 | private: | |
1793 | // The string for which we are getting information. | |
1794 | Expression* string_; | |
1795 | // What information we want. | |
1796 | String_info string_info_; | |
1797 | }; | |
1798 | ||
1799 | // Return the type of the string info. | |
1800 | ||
1801 | Type* | |
1802 | String_info_expression::do_type() | |
1803 | { | |
1804 | switch (this->string_info_) | |
1805 | { | |
1806 | case STRING_INFO_DATA: | |
1807 | { | |
1808 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1809 | return Type::make_pointer_type(byte_type); | |
1810 | } | |
1811 | case STRING_INFO_LENGTH: | |
1812 | return Type::lookup_integer_type("int"); | |
1813 | default: | |
1814 | go_unreachable(); | |
1815 | } | |
1816 | } | |
1817 | ||
1818 | // Return string information in GENERIC. | |
1819 | ||
ea664253 | 1820 | Bexpression* |
1821 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1822 | { |
1823 | Gogo* gogo = context->gogo(); | |
1824 | ||
ea664253 | 1825 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1826 | switch (this->string_info_) |
1827 | { | |
1828 | case STRING_INFO_DATA: | |
1829 | case STRING_INFO_LENGTH: | |
ea664253 | 1830 | return gogo->backend()->struct_field_expression(bstring, |
1831 | this->string_info_, | |
1832 | this->location()); | |
2c809f8f | 1833 | break; |
1834 | default: | |
1835 | go_unreachable(); | |
1836 | } | |
2c809f8f | 1837 | } |
1838 | ||
1839 | // Dump ast representation for a type info expression. | |
1840 | ||
1841 | void | |
1842 | String_info_expression::do_dump_expression( | |
1843 | Ast_dump_context* ast_dump_context) const | |
1844 | { | |
1845 | ast_dump_context->ostream() << "stringinfo("; | |
1846 | this->string_->dump_expression(ast_dump_context); | |
1847 | ast_dump_context->ostream() << ","; | |
1848 | ast_dump_context->ostream() << | |
1849 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1850 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1851 | : "unknown"); | |
1852 | ast_dump_context->ostream() << ")"; | |
1853 | } | |
1854 | ||
1855 | // Make a string info expression. | |
1856 | ||
1857 | Expression* | |
1858 | Expression::make_string_info(Expression* string, String_info string_info, | |
1859 | Location location) | |
1860 | { | |
1861 | return new String_info_expression(string, string_info, location); | |
1862 | } | |
1863 | ||
e440a328 | 1864 | // Make an integer expression. |
1865 | ||
1866 | class Integer_expression : public Expression | |
1867 | { | |
1868 | public: | |
5d4b8566 | 1869 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1870 | Location location) | |
e440a328 | 1871 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1872 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1873 | { mpz_init_set(this->val_, *val); } |
1874 | ||
1875 | static Expression* | |
1876 | do_import(Import*); | |
1877 | ||
8b1c301d | 1878 | // Write VAL to string dump. |
e440a328 | 1879 | static void |
8b1c301d | 1880 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1881 | |
d751bb78 | 1882 | // Write VAL to dump context. |
1883 | static void | |
1884 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1885 | ||
e440a328 | 1886 | protected: |
1887 | bool | |
1888 | do_is_constant() const | |
1889 | { return true; } | |
1890 | ||
0e168074 | 1891 | bool |
1892 | do_is_immutable() const | |
1893 | { return true; } | |
1894 | ||
e440a328 | 1895 | bool |
0c77715b | 1896 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1897 | |
1898 | Type* | |
1899 | do_type(); | |
1900 | ||
1901 | void | |
1902 | do_determine_type(const Type_context* context); | |
1903 | ||
1904 | void | |
1905 | do_check_types(Gogo*); | |
1906 | ||
ea664253 | 1907 | Bexpression* |
1908 | do_get_backend(Translate_context*); | |
e440a328 | 1909 | |
1910 | Expression* | |
1911 | do_copy() | |
5d4b8566 | 1912 | { |
1913 | if (this->is_character_constant_) | |
1914 | return Expression::make_character(&this->val_, this->type_, | |
1915 | this->location()); | |
1916 | else | |
e67508fa | 1917 | return Expression::make_integer_z(&this->val_, this->type_, |
1918 | this->location()); | |
5d4b8566 | 1919 | } |
e440a328 | 1920 | |
1921 | void | |
1922 | do_export(Export*) const; | |
1923 | ||
d751bb78 | 1924 | void |
1925 | do_dump_expression(Ast_dump_context*) const; | |
1926 | ||
e440a328 | 1927 | private: |
1928 | // The integer value. | |
1929 | mpz_t val_; | |
1930 | // The type so far. | |
1931 | Type* type_; | |
5d4b8566 | 1932 | // Whether this is a character constant. |
1933 | bool is_character_constant_; | |
e440a328 | 1934 | }; |
1935 | ||
0c77715b | 1936 | // Return a numeric constant for this expression. We have to mark |
1937 | // this as a character when appropriate. | |
e440a328 | 1938 | |
1939 | bool | |
0c77715b | 1940 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1941 | { |
0c77715b | 1942 | if (this->is_character_constant_) |
1943 | nc->set_rune(this->type_, this->val_); | |
1944 | else | |
1945 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1946 | return true; |
1947 | } | |
1948 | ||
1949 | // Return the current type. If we haven't set the type yet, we return | |
1950 | // an abstract integer type. | |
1951 | ||
1952 | Type* | |
1953 | Integer_expression::do_type() | |
1954 | { | |
1955 | if (this->type_ == NULL) | |
5d4b8566 | 1956 | { |
1957 | if (this->is_character_constant_) | |
1958 | this->type_ = Type::make_abstract_character_type(); | |
1959 | else | |
1960 | this->type_ = Type::make_abstract_integer_type(); | |
1961 | } | |
e440a328 | 1962 | return this->type_; |
1963 | } | |
1964 | ||
1965 | // Set the type of the integer value. Here we may switch from an | |
1966 | // abstract type to a real type. | |
1967 | ||
1968 | void | |
1969 | Integer_expression::do_determine_type(const Type_context* context) | |
1970 | { | |
1971 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1972 | ; | |
0c77715b | 1973 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1974 | this->type_ = context->type; |
1975 | else if (!context->may_be_abstract) | |
5d4b8566 | 1976 | { |
1977 | if (this->is_character_constant_) | |
1978 | this->type_ = Type::lookup_integer_type("int32"); | |
1979 | else | |
1980 | this->type_ = Type::lookup_integer_type("int"); | |
1981 | } | |
e440a328 | 1982 | } |
1983 | ||
e440a328 | 1984 | // Check the type of an integer constant. |
1985 | ||
1986 | void | |
1987 | Integer_expression::do_check_types(Gogo*) | |
1988 | { | |
0c77715b | 1989 | Type* type = this->type_; |
1990 | if (type == NULL) | |
e440a328 | 1991 | return; |
0c77715b | 1992 | Numeric_constant nc; |
1993 | if (this->is_character_constant_) | |
1994 | nc.set_rune(NULL, this->val_); | |
1995 | else | |
1996 | nc.set_int(NULL, this->val_); | |
1997 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1998 | this->set_is_error(); |
1999 | } | |
2000 | ||
ea664253 | 2001 | // Get the backend representation for an integer constant. |
e440a328 | 2002 | |
ea664253 | 2003 | Bexpression* |
2004 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2005 | { |
12373dd5 | 2006 | if (this->is_error_expression() |
2007 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2008 | { | |
2009 | go_assert(saw_errors()); | |
2010 | return context->gogo()->backend()->error_expression(); | |
2011 | } | |
2012 | ||
48c2a53a | 2013 | Type* resolved_type = NULL; |
e440a328 | 2014 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2015 | resolved_type = this->type_; |
e440a328 | 2016 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
2017 | { | |
2018 | // We are converting to an abstract floating point type. | |
48c2a53a | 2019 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2020 | } |
2021 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2022 | { | |
2023 | // We are converting to an abstract complex type. | |
48c2a53a | 2024 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2025 | } |
2026 | else | |
2027 | { | |
2028 | // If we still have an abstract type here, then this is being | |
2029 | // used in a constant expression which didn't get reduced for | |
2030 | // some reason. Use a type which will fit the value. We use <, | |
2031 | // not <=, because we need an extra bit for the sign bit. | |
2032 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 2033 | Type* int_type = Type::lookup_integer_type("int"); |
2034 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 2035 | resolved_type = int_type; |
e440a328 | 2036 | else if (bits < 64) |
48c2a53a | 2037 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 2038 | else |
48c2a53a | 2039 | { |
2040 | if (!saw_errors()) | |
631d5788 | 2041 | go_error_at(this->location(), |
2042 | "unknown type for large integer constant"); | |
ea664253 | 2043 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 2044 | } |
e440a328 | 2045 | } |
48c2a53a | 2046 | Numeric_constant nc; |
2047 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 2048 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2049 | } |
2050 | ||
2051 | // Write VAL to export data. | |
2052 | ||
2053 | void | |
8b1c301d | 2054 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 2055 | { |
2056 | char* s = mpz_get_str(NULL, 10, val); | |
2057 | exp->write_c_string(s); | |
2058 | free(s); | |
2059 | } | |
2060 | ||
2061 | // Export an integer in a constant expression. | |
2062 | ||
2063 | void | |
2064 | Integer_expression::do_export(Export* exp) const | |
2065 | { | |
2066 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 2067 | if (this->is_character_constant_) |
2068 | exp->write_c_string("'"); | |
e440a328 | 2069 | // A trailing space lets us reliably identify the end of the number. |
2070 | exp->write_c_string(" "); | |
2071 | } | |
2072 | ||
2073 | // Import an integer, floating point, or complex value. This handles | |
2074 | // all these types because they all start with digits. | |
2075 | ||
2076 | Expression* | |
2077 | Integer_expression::do_import(Import* imp) | |
2078 | { | |
2079 | std::string num = imp->read_identifier(); | |
2080 | imp->require_c_string(" "); | |
2081 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2082 | { | |
2083 | mpfr_t real; | |
2084 | size_t plus_pos = num.find('+', 1); | |
2085 | size_t minus_pos = num.find('-', 1); | |
2086 | size_t pos; | |
2087 | if (plus_pos == std::string::npos) | |
2088 | pos = minus_pos; | |
2089 | else if (minus_pos == std::string::npos) | |
2090 | pos = plus_pos; | |
2091 | else | |
2092 | { | |
631d5788 | 2093 | go_error_at(imp->location(), "bad number in import data: %qs", |
2094 | num.c_str()); | |
e440a328 | 2095 | return Expression::make_error(imp->location()); |
2096 | } | |
2097 | if (pos == std::string::npos) | |
2098 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2099 | else | |
2100 | { | |
2101 | std::string real_str = num.substr(0, pos); | |
2102 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2103 | { | |
631d5788 | 2104 | go_error_at(imp->location(), "bad number in import data: %qs", |
2105 | real_str.c_str()); | |
e440a328 | 2106 | return Expression::make_error(imp->location()); |
2107 | } | |
2108 | } | |
2109 | ||
2110 | std::string imag_str; | |
2111 | if (pos == std::string::npos) | |
2112 | imag_str = num; | |
2113 | else | |
2114 | imag_str = num.substr(pos); | |
2115 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2116 | mpfr_t imag; | |
2117 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2118 | { | |
631d5788 | 2119 | go_error_at(imp->location(), "bad number in import data: %qs", |
2120 | imag_str.c_str()); | |
e440a328 | 2121 | return Expression::make_error(imp->location()); |
2122 | } | |
fcbea5e4 | 2123 | mpc_t cval; |
2124 | mpc_init2(cval, mpc_precision); | |
2125 | mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); | |
e440a328 | 2126 | mpfr_clear(real); |
2127 | mpfr_clear(imag); | |
fcbea5e4 | 2128 | Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); |
2129 | mpc_clear(cval); | |
e440a328 | 2130 | return ret; |
2131 | } | |
2132 | else if (num.find('.') == std::string::npos | |
2133 | && num.find('E') == std::string::npos) | |
2134 | { | |
5d4b8566 | 2135 | bool is_character_constant = (!num.empty() |
2136 | && num[num.length() - 1] == '\''); | |
2137 | if (is_character_constant) | |
2138 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2139 | mpz_t val; |
2140 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2141 | { | |
631d5788 | 2142 | go_error_at(imp->location(), "bad number in import data: %qs", |
2143 | num.c_str()); | |
e440a328 | 2144 | return Expression::make_error(imp->location()); |
2145 | } | |
5d4b8566 | 2146 | Expression* ret; |
2147 | if (is_character_constant) | |
2148 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2149 | else | |
e67508fa | 2150 | ret = Expression::make_integer_z(&val, NULL, imp->location()); |
e440a328 | 2151 | mpz_clear(val); |
2152 | return ret; | |
2153 | } | |
2154 | else | |
2155 | { | |
2156 | mpfr_t val; | |
2157 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2158 | { | |
631d5788 | 2159 | go_error_at(imp->location(), "bad number in import data: %qs", |
2160 | num.c_str()); | |
e440a328 | 2161 | return Expression::make_error(imp->location()); |
2162 | } | |
2163 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2164 | mpfr_clear(val); | |
2165 | return ret; | |
2166 | } | |
2167 | } | |
d751bb78 | 2168 | // Ast dump for integer expression. |
2169 | ||
2170 | void | |
2171 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2172 | { | |
5d4b8566 | 2173 | if (this->is_character_constant_) |
2174 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2175 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2176 | if (this->is_character_constant_) |
2177 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2178 | } |
2179 | ||
e67508fa | 2180 | // Build a new integer value from a multi-precision integer. |
e440a328 | 2181 | |
2182 | Expression* | |
e67508fa | 2183 | Expression::make_integer_z(const mpz_t* val, Type* type, Location location) |
5d4b8566 | 2184 | { |
2185 | return new Integer_expression(val, type, false, location); | |
2186 | } | |
2187 | ||
e67508fa | 2188 | // Build a new integer value from an unsigned long. |
2189 | ||
2190 | Expression* | |
2191 | Expression::make_integer_ul(unsigned long val, Type *type, Location location) | |
2192 | { | |
2193 | mpz_t zval; | |
2194 | mpz_init_set_ui(zval, val); | |
2195 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2196 | mpz_clear(zval); | |
2197 | return ret; | |
2198 | } | |
2199 | ||
2200 | // Build a new integer value from a signed long. | |
2201 | ||
2202 | Expression* | |
2203 | Expression::make_integer_sl(long val, Type *type, Location location) | |
2204 | { | |
2205 | mpz_t zval; | |
2206 | mpz_init_set_si(zval, val); | |
2207 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2208 | mpz_clear(zval); | |
2209 | return ret; | |
2210 | } | |
2211 | ||
3f378015 | 2212 | // Store an int64_t in an uninitialized mpz_t. |
2213 | ||
2214 | static void | |
2215 | set_mpz_from_int64(mpz_t* zval, int64_t val) | |
2216 | { | |
2217 | if (val >= 0) | |
2218 | { | |
2219 | unsigned long ul = static_cast<unsigned long>(val); | |
2220 | if (static_cast<int64_t>(ul) == val) | |
2221 | { | |
2222 | mpz_init_set_ui(*zval, ul); | |
2223 | return; | |
2224 | } | |
2225 | } | |
2226 | uint64_t uv; | |
2227 | if (val >= 0) | |
2228 | uv = static_cast<uint64_t>(val); | |
2229 | else | |
2230 | uv = static_cast<uint64_t>(- val); | |
2231 | unsigned long ul = uv & 0xffffffffUL; | |
2232 | mpz_init_set_ui(*zval, ul); | |
2233 | mpz_t hval; | |
2234 | mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); | |
2235 | mpz_mul_2exp(hval, hval, 32); | |
2236 | mpz_add(*zval, *zval, hval); | |
2237 | mpz_clear(hval); | |
2238 | if (val < 0) | |
2239 | mpz_neg(*zval, *zval); | |
2240 | } | |
2241 | ||
2242 | // Build a new integer value from an int64_t. | |
2243 | ||
2244 | Expression* | |
2245 | Expression::make_integer_int64(int64_t val, Type* type, Location location) | |
2246 | { | |
2247 | mpz_t zval; | |
2248 | set_mpz_from_int64(&zval, val); | |
2249 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2250 | mpz_clear(zval); | |
2251 | return ret; | |
2252 | } | |
2253 | ||
5d4b8566 | 2254 | // Build a new character constant value. |
2255 | ||
2256 | Expression* | |
2257 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2258 | { |
5d4b8566 | 2259 | return new Integer_expression(val, type, true, location); |
e440a328 | 2260 | } |
2261 | ||
2262 | // Floats. | |
2263 | ||
2264 | class Float_expression : public Expression | |
2265 | { | |
2266 | public: | |
b13c66cd | 2267 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2268 | : Expression(EXPRESSION_FLOAT, location), |
2269 | type_(type) | |
2270 | { | |
2271 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2272 | } | |
2273 | ||
e440a328 | 2274 | // Write VAL to export data. |
2275 | static void | |
8b1c301d | 2276 | export_float(String_dump* exp, const mpfr_t val); |
2277 | ||
d751bb78 | 2278 | // Write VAL to dump file. |
2279 | static void | |
2280 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2281 | |
2282 | protected: | |
2283 | bool | |
2284 | do_is_constant() const | |
2285 | { return true; } | |
2286 | ||
0e168074 | 2287 | bool |
2288 | do_is_immutable() const | |
2289 | { return true; } | |
2290 | ||
e440a328 | 2291 | bool |
0c77715b | 2292 | do_numeric_constant_value(Numeric_constant* nc) const |
2293 | { | |
2294 | nc->set_float(this->type_, this->val_); | |
2295 | return true; | |
2296 | } | |
e440a328 | 2297 | |
2298 | Type* | |
2299 | do_type(); | |
2300 | ||
2301 | void | |
2302 | do_determine_type(const Type_context*); | |
2303 | ||
2304 | void | |
2305 | do_check_types(Gogo*); | |
2306 | ||
2307 | Expression* | |
2308 | do_copy() | |
2309 | { return Expression::make_float(&this->val_, this->type_, | |
2310 | this->location()); } | |
2311 | ||
ea664253 | 2312 | Bexpression* |
2313 | do_get_backend(Translate_context*); | |
e440a328 | 2314 | |
2315 | void | |
2316 | do_export(Export*) const; | |
2317 | ||
d751bb78 | 2318 | void |
2319 | do_dump_expression(Ast_dump_context*) const; | |
2320 | ||
e440a328 | 2321 | private: |
2322 | // The floating point value. | |
2323 | mpfr_t val_; | |
2324 | // The type so far. | |
2325 | Type* type_; | |
2326 | }; | |
2327 | ||
e440a328 | 2328 | // Return the current type. If we haven't set the type yet, we return |
2329 | // an abstract float type. | |
2330 | ||
2331 | Type* | |
2332 | Float_expression::do_type() | |
2333 | { | |
2334 | if (this->type_ == NULL) | |
2335 | this->type_ = Type::make_abstract_float_type(); | |
2336 | return this->type_; | |
2337 | } | |
2338 | ||
2339 | // Set the type of the float value. Here we may switch from an | |
2340 | // abstract type to a real type. | |
2341 | ||
2342 | void | |
2343 | Float_expression::do_determine_type(const Type_context* context) | |
2344 | { | |
2345 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2346 | ; | |
2347 | else if (context->type != NULL | |
2348 | && (context->type->integer_type() != NULL | |
2349 | || context->type->float_type() != NULL | |
2350 | || context->type->complex_type() != NULL)) | |
2351 | this->type_ = context->type; | |
2352 | else if (!context->may_be_abstract) | |
48080209 | 2353 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2354 | } |
2355 | ||
e440a328 | 2356 | // Check the type of a float value. |
2357 | ||
2358 | void | |
2359 | Float_expression::do_check_types(Gogo*) | |
2360 | { | |
0c77715b | 2361 | Type* type = this->type_; |
2362 | if (type == NULL) | |
e440a328 | 2363 | return; |
0c77715b | 2364 | Numeric_constant nc; |
2365 | nc.set_float(NULL, this->val_); | |
2366 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2367 | this->set_is_error(); |
e440a328 | 2368 | } |
2369 | ||
ea664253 | 2370 | // Get the backend representation for a float constant. |
e440a328 | 2371 | |
ea664253 | 2372 | Bexpression* |
2373 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2374 | { |
12373dd5 | 2375 | if (this->is_error_expression() |
2376 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2377 | { | |
2378 | go_assert(saw_errors()); | |
2379 | return context->gogo()->backend()->error_expression(); | |
2380 | } | |
2381 | ||
48c2a53a | 2382 | Type* resolved_type; |
e440a328 | 2383 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2384 | resolved_type = this->type_; |
e440a328 | 2385 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2386 | { | |
2387 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2388 | resolved_type = Type::lookup_integer_type("int"); |
2389 | } | |
2390 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2391 | { | |
2392 | // We are converting to an abstract complex type. | |
2393 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2394 | } |
2395 | else | |
2396 | { | |
2397 | // If we still have an abstract type here, then this is being | |
2398 | // used in a constant expression which didn't get reduced. We | |
2399 | // just use float64 and hope for the best. | |
48c2a53a | 2400 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2401 | } |
48c2a53a | 2402 | |
2403 | Numeric_constant nc; | |
2404 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2405 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2406 | } |
2407 | ||
8b1c301d | 2408 | // Write a floating point number to a string dump. |
e440a328 | 2409 | |
2410 | void | |
8b1c301d | 2411 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2412 | { |
2413 | mp_exp_t exponent; | |
2414 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2415 | if (*s == '-') | |
2416 | exp->write_c_string("-"); | |
2417 | exp->write_c_string("0."); | |
2418 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2419 | mpfr_free_str(s); | |
2420 | char buf[30]; | |
2421 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2422 | exp->write_c_string(buf); | |
2423 | } | |
2424 | ||
2425 | // Export a floating point number in a constant expression. | |
2426 | ||
2427 | void | |
2428 | Float_expression::do_export(Export* exp) const | |
2429 | { | |
2430 | Float_expression::export_float(exp, this->val_); | |
2431 | // A trailing space lets us reliably identify the end of the number. | |
2432 | exp->write_c_string(" "); | |
2433 | } | |
2434 | ||
d751bb78 | 2435 | // Dump a floating point number to the dump file. |
2436 | ||
2437 | void | |
2438 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2439 | { | |
8b1c301d | 2440 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2441 | } |
2442 | ||
e440a328 | 2443 | // Make a float expression. |
2444 | ||
2445 | Expression* | |
b13c66cd | 2446 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2447 | { |
2448 | return new Float_expression(val, type, location); | |
2449 | } | |
2450 | ||
2451 | // Complex numbers. | |
2452 | ||
2453 | class Complex_expression : public Expression | |
2454 | { | |
2455 | public: | |
fcbea5e4 | 2456 | Complex_expression(const mpc_t* val, Type* type, Location location) |
e440a328 | 2457 | : Expression(EXPRESSION_COMPLEX, location), |
2458 | type_(type) | |
2459 | { | |
fcbea5e4 | 2460 | mpc_init2(this->val_, mpc_precision); |
2461 | mpc_set(this->val_, *val, MPC_RNDNN); | |
e440a328 | 2462 | } |
2463 | ||
fcbea5e4 | 2464 | // Write VAL to string dump. |
e440a328 | 2465 | static void |
fcbea5e4 | 2466 | export_complex(String_dump* exp, const mpc_t val); |
e440a328 | 2467 | |
d751bb78 | 2468 | // Write REAL/IMAG to dump context. |
2469 | static void | |
fcbea5e4 | 2470 | dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); |
d751bb78 | 2471 | |
e440a328 | 2472 | protected: |
2473 | bool | |
2474 | do_is_constant() const | |
2475 | { return true; } | |
2476 | ||
0e168074 | 2477 | bool |
2478 | do_is_immutable() const | |
2479 | { return true; } | |
2480 | ||
e440a328 | 2481 | bool |
0c77715b | 2482 | do_numeric_constant_value(Numeric_constant* nc) const |
2483 | { | |
fcbea5e4 | 2484 | nc->set_complex(this->type_, this->val_); |
0c77715b | 2485 | return true; |
2486 | } | |
e440a328 | 2487 | |
2488 | Type* | |
2489 | do_type(); | |
2490 | ||
2491 | void | |
2492 | do_determine_type(const Type_context*); | |
2493 | ||
2494 | void | |
2495 | do_check_types(Gogo*); | |
2496 | ||
2497 | Expression* | |
2498 | do_copy() | |
2499 | { | |
fcbea5e4 | 2500 | return Expression::make_complex(&this->val_, this->type_, |
e440a328 | 2501 | this->location()); |
2502 | } | |
2503 | ||
ea664253 | 2504 | Bexpression* |
2505 | do_get_backend(Translate_context*); | |
e440a328 | 2506 | |
2507 | void | |
2508 | do_export(Export*) const; | |
2509 | ||
d751bb78 | 2510 | void |
2511 | do_dump_expression(Ast_dump_context*) const; | |
abd26de0 | 2512 | |
e440a328 | 2513 | private: |
fcbea5e4 | 2514 | // The complex value. |
2515 | mpc_t val_; | |
e440a328 | 2516 | // The type if known. |
2517 | Type* type_; | |
2518 | }; | |
2519 | ||
e440a328 | 2520 | // Return the current type. If we haven't set the type yet, we return |
2521 | // an abstract complex type. | |
2522 | ||
2523 | Type* | |
2524 | Complex_expression::do_type() | |
2525 | { | |
2526 | if (this->type_ == NULL) | |
2527 | this->type_ = Type::make_abstract_complex_type(); | |
2528 | return this->type_; | |
2529 | } | |
2530 | ||
2531 | // Set the type of the complex value. Here we may switch from an | |
2532 | // abstract type to a real type. | |
2533 | ||
2534 | void | |
2535 | Complex_expression::do_determine_type(const Type_context* context) | |
2536 | { | |
2537 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2538 | ; | |
abd26de0 | 2539 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2540 | this->type_ = context->type; |
2541 | else if (!context->may_be_abstract) | |
48080209 | 2542 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2543 | } |
2544 | ||
e440a328 | 2545 | // Check the type of a complex value. |
2546 | ||
2547 | void | |
2548 | Complex_expression::do_check_types(Gogo*) | |
2549 | { | |
0c77715b | 2550 | Type* type = this->type_; |
2551 | if (type == NULL) | |
e440a328 | 2552 | return; |
0c77715b | 2553 | Numeric_constant nc; |
fcbea5e4 | 2554 | nc.set_complex(NULL, this->val_); |
0c77715b | 2555 | if (!nc.set_type(this->type_, true, this->location())) |
e440a328 | 2556 | this->set_is_error(); |
2557 | } | |
2558 | ||
ea664253 | 2559 | // Get the backend representation for a complex constant. |
e440a328 | 2560 | |
ea664253 | 2561 | Bexpression* |
2562 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2563 | { |
12373dd5 | 2564 | if (this->is_error_expression() |
2565 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2566 | { | |
2567 | go_assert(saw_errors()); | |
2568 | return context->gogo()->backend()->error_expression(); | |
2569 | } | |
2570 | ||
48c2a53a | 2571 | Type* resolved_type; |
e440a328 | 2572 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2573 | resolved_type = this->type_; |
2574 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2575 | { | |
2576 | // We are converting to an abstract integer type. | |
2577 | resolved_type = Type::lookup_integer_type("int"); | |
2578 | } | |
2579 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2580 | { | |
2581 | // We are converting to an abstract float type. | |
2582 | resolved_type = Type::lookup_float_type("float64"); | |
2583 | } | |
e440a328 | 2584 | else |
2585 | { | |
47ae02b7 | 2586 | // If we still have an abstract type here, this is being |
e440a328 | 2587 | // used in a constant expression which didn't get reduced. We |
2588 | // just use complex128 and hope for the best. | |
48c2a53a | 2589 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2590 | } |
48c2a53a | 2591 | |
2592 | Numeric_constant nc; | |
fcbea5e4 | 2593 | nc.set_complex(resolved_type, this->val_); |
ea664253 | 2594 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2595 | } |
2596 | ||
2597 | // Write REAL/IMAG to export data. | |
2598 | ||
2599 | void | |
fcbea5e4 | 2600 | Complex_expression::export_complex(String_dump* exp, const mpc_t val) |
e440a328 | 2601 | { |
fcbea5e4 | 2602 | if (!mpfr_zero_p(mpc_realref(val))) |
e440a328 | 2603 | { |
fcbea5e4 | 2604 | Float_expression::export_float(exp, mpc_realref(val)); |
d1db782d | 2605 | if (mpfr_sgn(mpc_imagref(val)) >= 0) |
e440a328 | 2606 | exp->write_c_string("+"); |
2607 | } | |
fcbea5e4 | 2608 | Float_expression::export_float(exp, mpc_imagref(val)); |
e440a328 | 2609 | exp->write_c_string("i"); |
2610 | } | |
2611 | ||
2612 | // Export a complex number in a constant expression. | |
2613 | ||
2614 | void | |
2615 | Complex_expression::do_export(Export* exp) const | |
2616 | { | |
fcbea5e4 | 2617 | Complex_expression::export_complex(exp, this->val_); |
e440a328 | 2618 | // A trailing space lets us reliably identify the end of the number. |
2619 | exp->write_c_string(" "); | |
2620 | } | |
2621 | ||
d751bb78 | 2622 | // Dump a complex expression to the dump file. |
2623 | ||
2624 | void | |
2625 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2626 | { | |
fcbea5e4 | 2627 | Complex_expression::export_complex(ast_dump_context, this->val_); |
d751bb78 | 2628 | } |
2629 | ||
e440a328 | 2630 | // Make a complex expression. |
2631 | ||
2632 | Expression* | |
fcbea5e4 | 2633 | Expression::make_complex(const mpc_t* val, Type* type, Location location) |
e440a328 | 2634 | { |
fcbea5e4 | 2635 | return new Complex_expression(val, type, location); |
e440a328 | 2636 | } |
2637 | ||
d5b605df | 2638 | // Find a named object in an expression. |
2639 | ||
2640 | class Find_named_object : public Traverse | |
2641 | { | |
2642 | public: | |
2643 | Find_named_object(Named_object* no) | |
2644 | : Traverse(traverse_expressions), | |
2645 | no_(no), found_(false) | |
2646 | { } | |
2647 | ||
2648 | // Whether we found the object. | |
2649 | bool | |
2650 | found() const | |
2651 | { return this->found_; } | |
2652 | ||
2653 | protected: | |
2654 | int | |
2655 | expression(Expression**); | |
2656 | ||
2657 | private: | |
2658 | // The object we are looking for. | |
2659 | Named_object* no_; | |
2660 | // Whether we found it. | |
2661 | bool found_; | |
2662 | }; | |
2663 | ||
e440a328 | 2664 | // A reference to a const in an expression. |
2665 | ||
2666 | class Const_expression : public Expression | |
2667 | { | |
2668 | public: | |
b13c66cd | 2669 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2670 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2671 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2672 | { } |
2673 | ||
d5b605df | 2674 | Named_object* |
2675 | named_object() | |
2676 | { return this->constant_; } | |
2677 | ||
a7f064d5 | 2678 | // Check that the initializer does not refer to the constant itself. |
2679 | void | |
2680 | check_for_init_loop(); | |
2681 | ||
e440a328 | 2682 | protected: |
ba4aedd4 | 2683 | int |
2684 | do_traverse(Traverse*); | |
2685 | ||
e440a328 | 2686 | Expression* |
ceeb4318 | 2687 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2688 | |
2689 | bool | |
2690 | do_is_constant() const | |
2691 | { return true; } | |
2692 | ||
0e168074 | 2693 | bool |
2694 | do_is_immutable() const | |
2695 | { return true; } | |
2696 | ||
e440a328 | 2697 | bool |
0c77715b | 2698 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2699 | |
2700 | bool | |
af6b489a | 2701 | do_string_constant_value(std::string* val) const; |
e440a328 | 2702 | |
2703 | Type* | |
2704 | do_type(); | |
2705 | ||
2706 | // The type of a const is set by the declaration, not the use. | |
2707 | void | |
2708 | do_determine_type(const Type_context*); | |
2709 | ||
2710 | void | |
2711 | do_check_types(Gogo*); | |
2712 | ||
2713 | Expression* | |
2714 | do_copy() | |
2715 | { return this; } | |
2716 | ||
ea664253 | 2717 | Bexpression* |
2718 | do_get_backend(Translate_context* context); | |
e440a328 | 2719 | |
2720 | // When exporting a reference to a const as part of a const | |
2721 | // expression, we export the value. We ignore the fact that it has | |
2722 | // a name. | |
2723 | void | |
2724 | do_export(Export* exp) const | |
2725 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2726 | ||
d751bb78 | 2727 | void |
2728 | do_dump_expression(Ast_dump_context*) const; | |
2729 | ||
e440a328 | 2730 | private: |
2731 | // The constant. | |
2732 | Named_object* constant_; | |
2733 | // The type of this reference. This is used if the constant has an | |
2734 | // abstract type. | |
2735 | Type* type_; | |
13e818f5 | 2736 | // Used to prevent infinite recursion when a constant incorrectly |
2737 | // refers to itself. | |
2738 | mutable bool seen_; | |
e440a328 | 2739 | }; |
2740 | ||
ba4aedd4 | 2741 | // Traversal. |
2742 | ||
2743 | int | |
2744 | Const_expression::do_traverse(Traverse* traverse) | |
2745 | { | |
2746 | if (this->type_ != NULL) | |
2747 | return Type::traverse(this->type_, traverse); | |
2748 | return TRAVERSE_CONTINUE; | |
2749 | } | |
2750 | ||
e440a328 | 2751 | // Lower a constant expression. This is where we convert the |
2752 | // predeclared constant iota into an integer value. | |
2753 | ||
2754 | Expression* | |
ceeb4318 | 2755 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2756 | Statement_inserter*, int iota_value) | |
e440a328 | 2757 | { |
2758 | if (this->constant_->const_value()->expr()->classification() | |
2759 | == EXPRESSION_IOTA) | |
2760 | { | |
2761 | if (iota_value == -1) | |
2762 | { | |
631d5788 | 2763 | go_error_at(this->location(), |
2764 | "iota is only defined in const declarations"); | |
e440a328 | 2765 | iota_value = 0; |
2766 | } | |
e67508fa | 2767 | return Expression::make_integer_ul(iota_value, NULL, this->location()); |
e440a328 | 2768 | } |
2769 | ||
2770 | // Make sure that the constant itself has been lowered. | |
2771 | gogo->lower_constant(this->constant_); | |
2772 | ||
2773 | return this; | |
2774 | } | |
2775 | ||
0c77715b | 2776 | // Return a numeric constant value. |
e440a328 | 2777 | |
2778 | bool | |
0c77715b | 2779 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2780 | { |
13e818f5 | 2781 | if (this->seen_) |
2782 | return false; | |
2783 | ||
e440a328 | 2784 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2785 | |
13e818f5 | 2786 | this->seen_ = true; |
2787 | ||
0c77715b | 2788 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2789 | |
13e818f5 | 2790 | this->seen_ = false; |
2791 | ||
e440a328 | 2792 | Type* ctype; |
2793 | if (this->type_ != NULL) | |
2794 | ctype = this->type_; | |
2795 | else | |
2796 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2797 | if (r && ctype != NULL) |
2798 | { | |
0c77715b | 2799 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2800 | return false; |
e440a328 | 2801 | } |
e440a328 | 2802 | |
e440a328 | 2803 | return r; |
2804 | } | |
2805 | ||
af6b489a | 2806 | bool |
2807 | Const_expression::do_string_constant_value(std::string* val) const | |
2808 | { | |
2809 | if (this->seen_) | |
2810 | return false; | |
2811 | ||
2812 | Expression* e = this->constant_->const_value()->expr(); | |
2813 | ||
2814 | this->seen_ = true; | |
2815 | bool ok = e->string_constant_value(val); | |
2816 | this->seen_ = false; | |
2817 | ||
2818 | return ok; | |
2819 | } | |
2820 | ||
e440a328 | 2821 | // Return the type of the const reference. |
2822 | ||
2823 | Type* | |
2824 | Const_expression::do_type() | |
2825 | { | |
2826 | if (this->type_ != NULL) | |
2827 | return this->type_; | |
13e818f5 | 2828 | |
2f78f012 | 2829 | Named_constant* nc = this->constant_->const_value(); |
2830 | ||
2831 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2832 | { |
2833 | this->report_error(_("constant refers to itself")); | |
2834 | this->type_ = Type::make_error_type(); | |
2835 | return this->type_; | |
2836 | } | |
2837 | ||
2838 | this->seen_ = true; | |
2839 | ||
e440a328 | 2840 | Type* ret = nc->type(); |
13e818f5 | 2841 | |
e440a328 | 2842 | if (ret != NULL) |
13e818f5 | 2843 | { |
2844 | this->seen_ = false; | |
2845 | return ret; | |
2846 | } | |
2847 | ||
e440a328 | 2848 | // During parsing, a named constant may have a NULL type, but we |
2849 | // must not return a NULL type here. | |
13e818f5 | 2850 | ret = nc->expr()->type(); |
2851 | ||
2852 | this->seen_ = false; | |
2853 | ||
2854 | return ret; | |
e440a328 | 2855 | } |
2856 | ||
2857 | // Set the type of the const reference. | |
2858 | ||
2859 | void | |
2860 | Const_expression::do_determine_type(const Type_context* context) | |
2861 | { | |
2862 | Type* ctype = this->constant_->const_value()->type(); | |
2863 | Type* cetype = (ctype != NULL | |
2864 | ? ctype | |
2865 | : this->constant_->const_value()->expr()->type()); | |
2866 | if (ctype != NULL && !ctype->is_abstract()) | |
2867 | ; | |
2868 | else if (context->type != NULL | |
0c77715b | 2869 | && context->type->is_numeric_type() |
2870 | && cetype->is_numeric_type()) | |
e440a328 | 2871 | this->type_ = context->type; |
2872 | else if (context->type != NULL | |
2873 | && context->type->is_string_type() | |
2874 | && cetype->is_string_type()) | |
2875 | this->type_ = context->type; | |
2876 | else if (context->type != NULL | |
2877 | && context->type->is_boolean_type() | |
2878 | && cetype->is_boolean_type()) | |
2879 | this->type_ = context->type; | |
2880 | else if (!context->may_be_abstract) | |
2881 | { | |
2882 | if (cetype->is_abstract()) | |
2883 | cetype = cetype->make_non_abstract_type(); | |
2884 | this->type_ = cetype; | |
2885 | } | |
2886 | } | |
2887 | ||
a7f064d5 | 2888 | // Check for a loop in which the initializer of a constant refers to |
2889 | // the constant itself. | |
e440a328 | 2890 | |
2891 | void | |
a7f064d5 | 2892 | Const_expression::check_for_init_loop() |
e440a328 | 2893 | { |
5c13bd80 | 2894 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2895 | return; |
2896 | ||
a7f064d5 | 2897 | if (this->seen_) |
2898 | { | |
2899 | this->report_error(_("constant refers to itself")); | |
2900 | this->type_ = Type::make_error_type(); | |
2901 | return; | |
2902 | } | |
2903 | ||
d5b605df | 2904 | Expression* init = this->constant_->const_value()->expr(); |
2905 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2906 | |
2907 | this->seen_ = true; | |
d5b605df | 2908 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2909 | this->seen_ = false; |
2910 | ||
d5b605df | 2911 | if (find_named_object.found()) |
2912 | { | |
5c13bd80 | 2913 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2914 | { |
2915 | this->report_error(_("constant refers to itself")); | |
2916 | this->type_ = Type::make_error_type(); | |
2917 | } | |
d5b605df | 2918 | return; |
2919 | } | |
a7f064d5 | 2920 | } |
2921 | ||
2922 | // Check types of a const reference. | |
2923 | ||
2924 | void | |
2925 | Const_expression::do_check_types(Gogo*) | |
2926 | { | |
5c13bd80 | 2927 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2928 | return; |
2929 | ||
2930 | this->check_for_init_loop(); | |
d5b605df | 2931 | |
0c77715b | 2932 | // Check that numeric constant fits in type. |
2933 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2934 | { |
0c77715b | 2935 | Numeric_constant nc; |
2936 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2937 | { |
0c77715b | 2938 | if (!nc.set_type(this->type_, true, this->location())) |
2939 | this->set_is_error(); | |
e440a328 | 2940 | } |
e440a328 | 2941 | } |
2942 | } | |
2943 | ||
ea664253 | 2944 | // Return the backend representation for a const reference. |
e440a328 | 2945 | |
ea664253 | 2946 | Bexpression* |
2947 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2948 | { |
12373dd5 | 2949 | if (this->is_error_expression() |
2950 | || (this->type_ != NULL && this->type_->is_error())) | |
2951 | { | |
2952 | go_assert(saw_errors()); | |
2953 | return context->backend()->error_expression(); | |
2954 | } | |
e440a328 | 2955 | |
2956 | // If the type has been set for this expression, but the underlying | |
2957 | // object is an abstract int or float, we try to get the abstract | |
2958 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2959 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2960 | if (this->type_ != NULL |
0c77715b | 2961 | && this->type_->is_numeric_type() |
a68492b4 | 2962 | && (this->constant_->const_value()->type() == NULL |
2963 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2964 | { |
0c77715b | 2965 | Numeric_constant nc; |
2966 | if (expr->numeric_constant_value(&nc) | |
2967 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2968 | { |
0c77715b | 2969 | Expression* e = nc.expression(this->location()); |
ea664253 | 2970 | return e->get_backend(context); |
e440a328 | 2971 | } |
e440a328 | 2972 | } |
2973 | ||
2c809f8f | 2974 | if (this->type_ != NULL) |
f2de4532 | 2975 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 2976 | return expr->get_backend(context); |
e440a328 | 2977 | } |
2978 | ||
d751bb78 | 2979 | // Dump ast representation for constant expression. |
2980 | ||
2981 | void | |
2982 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2983 | { | |
2984 | ast_dump_context->ostream() << this->constant_->name(); | |
2985 | } | |
2986 | ||
e440a328 | 2987 | // Make a reference to a constant in an expression. |
2988 | ||
2989 | Expression* | |
2990 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2991 | Location location) |
e440a328 | 2992 | { |
2993 | return new Const_expression(constant, location); | |
2994 | } | |
2995 | ||
d5b605df | 2996 | // Find a named object in an expression. |
2997 | ||
2998 | int | |
2999 | Find_named_object::expression(Expression** pexpr) | |
3000 | { | |
3001 | switch ((*pexpr)->classification()) | |
3002 | { | |
3003 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 3004 | { |
3005 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
3006 | if (ce->named_object() == this->no_) | |
3007 | break; | |
3008 | ||
3009 | // We need to check a constant initializer explicitly, as | |
3010 | // loops here will not be caught by the loop checking for | |
3011 | // variable initializers. | |
3012 | ce->check_for_init_loop(); | |
3013 | ||
3014 | return TRAVERSE_CONTINUE; | |
3015 | } | |
3016 | ||
d5b605df | 3017 | case Expression::EXPRESSION_VAR_REFERENCE: |
3018 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
3019 | break; | |
3020 | return TRAVERSE_CONTINUE; | |
3021 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
3022 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
3023 | break; | |
3024 | return TRAVERSE_CONTINUE; | |
3025 | default: | |
3026 | return TRAVERSE_CONTINUE; | |
3027 | } | |
3028 | this->found_ = true; | |
3029 | return TRAVERSE_EXIT; | |
3030 | } | |
3031 | ||
e440a328 | 3032 | // The nil value. |
3033 | ||
3034 | class Nil_expression : public Expression | |
3035 | { | |
3036 | public: | |
b13c66cd | 3037 | Nil_expression(Location location) |
e440a328 | 3038 | : Expression(EXPRESSION_NIL, location) |
3039 | { } | |
3040 | ||
3041 | static Expression* | |
3042 | do_import(Import*); | |
3043 | ||
3044 | protected: | |
3045 | bool | |
3046 | do_is_constant() const | |
3047 | { return true; } | |
3048 | ||
f9ca30f9 | 3049 | bool |
3050 | do_is_immutable() const | |
3051 | { return true; } | |
3052 | ||
e440a328 | 3053 | Type* |
3054 | do_type() | |
3055 | { return Type::make_nil_type(); } | |
3056 | ||
3057 | void | |
3058 | do_determine_type(const Type_context*) | |
3059 | { } | |
3060 | ||
3061 | Expression* | |
3062 | do_copy() | |
3063 | { return this; } | |
3064 | ||
ea664253 | 3065 | Bexpression* |
3066 | do_get_backend(Translate_context* context) | |
3067 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 3068 | |
3069 | void | |
3070 | do_export(Export* exp) const | |
3071 | { exp->write_c_string("nil"); } | |
d751bb78 | 3072 | |
3073 | void | |
3074 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3075 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 3076 | }; |
3077 | ||
3078 | // Import a nil expression. | |
3079 | ||
3080 | Expression* | |
3081 | Nil_expression::do_import(Import* imp) | |
3082 | { | |
3083 | imp->require_c_string("nil"); | |
3084 | return Expression::make_nil(imp->location()); | |
3085 | } | |
3086 | ||
3087 | // Make a nil expression. | |
3088 | ||
3089 | Expression* | |
b13c66cd | 3090 | Expression::make_nil(Location location) |
e440a328 | 3091 | { |
3092 | return new Nil_expression(location); | |
3093 | } | |
3094 | ||
3095 | // The value of the predeclared constant iota. This is little more | |
3096 | // than a marker. This will be lowered to an integer in | |
3097 | // Const_expression::do_lower, which is where we know the value that | |
3098 | // it should have. | |
3099 | ||
3100 | class Iota_expression : public Parser_expression | |
3101 | { | |
3102 | public: | |
b13c66cd | 3103 | Iota_expression(Location location) |
e440a328 | 3104 | : Parser_expression(EXPRESSION_IOTA, location) |
3105 | { } | |
3106 | ||
3107 | protected: | |
3108 | Expression* | |
ceeb4318 | 3109 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 3110 | { go_unreachable(); } |
e440a328 | 3111 | |
3112 | // There should only ever be one of these. | |
3113 | Expression* | |
3114 | do_copy() | |
c3e6f413 | 3115 | { go_unreachable(); } |
d751bb78 | 3116 | |
3117 | void | |
3118 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3119 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3120 | }; |
3121 | ||
3122 | // Make an iota expression. This is only called for one case: the | |
3123 | // value of the predeclared constant iota. | |
3124 | ||
3125 | Expression* | |
3126 | Expression::make_iota() | |
3127 | { | |
b13c66cd | 3128 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3129 | return &iota_expression; |
3130 | } | |
3131 | ||
da244e59 | 3132 | // Class Type_conversion_expression. |
e440a328 | 3133 | |
3134 | // Traversal. | |
3135 | ||
3136 | int | |
3137 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3138 | { | |
3139 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3140 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3141 | return TRAVERSE_EXIT; | |
3142 | return TRAVERSE_CONTINUE; | |
3143 | } | |
3144 | ||
3145 | // Convert to a constant at lowering time. | |
3146 | ||
3147 | Expression* | |
ceeb4318 | 3148 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3149 | Statement_inserter*, int) | |
e440a328 | 3150 | { |
3151 | Type* type = this->type_; | |
3152 | Expression* val = this->expr_; | |
b13c66cd | 3153 | Location location = this->location(); |
e440a328 | 3154 | |
0c77715b | 3155 | if (type->is_numeric_type()) |
e440a328 | 3156 | { |
0c77715b | 3157 | Numeric_constant nc; |
3158 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3159 | { |
0c77715b | 3160 | if (!nc.set_type(type, true, location)) |
3161 | return Expression::make_error(location); | |
3162 | return nc.expression(location); | |
e440a328 | 3163 | } |
e440a328 | 3164 | } |
3165 | ||
d7739c9a | 3166 | // According to the language specification on string conversions |
3167 | // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): | |
3168 | // When converting an integer into a string, the string will be a UTF-8 | |
3169 | // representation of the integer and integers "outside the range of valid | |
3170 | // Unicode code points are converted to '\uFFFD'." | |
3171 | if (type->is_string_type()) | |
3172 | { | |
3173 | Numeric_constant nc; | |
3174 | if (val->numeric_constant_value(&nc) && nc.is_int()) | |
3175 | { | |
3176 | // An integer value doesn't fit in the Unicode code point range if it | |
3177 | // overflows the Go "int" type or is negative. | |
3178 | unsigned long ul; | |
3179 | if (!nc.set_type(Type::lookup_integer_type("int"), false, location) | |
3180 | || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) | |
3181 | return Expression::make_string("\ufffd", location); | |
3182 | } | |
3183 | } | |
3184 | ||
55072f2b | 3185 | if (type->is_slice_type()) |
e440a328 | 3186 | { |
3187 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3188 | bool is_byte = (element_type->integer_type() != NULL |
3189 | && element_type->integer_type()->is_byte()); | |
3190 | bool is_rune = (element_type->integer_type() != NULL | |
3191 | && element_type->integer_type()->is_rune()); | |
3192 | if (is_byte || is_rune) | |
e440a328 | 3193 | { |
3194 | std::string s; | |
3195 | if (val->string_constant_value(&s)) | |
3196 | { | |
3197 | Expression_list* vals = new Expression_list(); | |
3198 | if (is_byte) | |
3199 | { | |
3200 | for (std::string::const_iterator p = s.begin(); | |
3201 | p != s.end(); | |
3202 | p++) | |
3203 | { | |
e67508fa | 3204 | unsigned char c = static_cast<unsigned char>(*p); |
3205 | vals->push_back(Expression::make_integer_ul(c, | |
3206 | element_type, | |
3207 | location)); | |
e440a328 | 3208 | } |
3209 | } | |
3210 | else | |
3211 | { | |
3212 | const char *p = s.data(); | |
3213 | const char *pend = s.data() + s.length(); | |
3214 | while (p < pend) | |
3215 | { | |
3216 | unsigned int c; | |
3217 | int adv = Lex::fetch_char(p, &c); | |
3218 | if (adv == 0) | |
3219 | { | |
631d5788 | 3220 | go_warning_at(this->location(), 0, |
e440a328 | 3221 | "invalid UTF-8 encoding"); |
3222 | adv = 1; | |
3223 | } | |
3224 | p += adv; | |
e67508fa | 3225 | vals->push_back(Expression::make_integer_ul(c, |
3226 | element_type, | |
3227 | location)); | |
e440a328 | 3228 | } |
3229 | } | |
3230 | ||
3231 | return Expression::make_slice_composite_literal(type, vals, | |
3232 | location); | |
3233 | } | |
3234 | } | |
3235 | } | |
3236 | ||
3237 | return this; | |
3238 | } | |
3239 | ||
35a54f17 | 3240 | // Flatten a type conversion by using a temporary variable for the slice |
3241 | // in slice to string conversions. | |
3242 | ||
3243 | Expression* | |
3244 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3245 | Statement_inserter* inserter) | |
3246 | { | |
5bf8be8b | 3247 | if (this->type()->is_error_type() || this->expr_->is_error_expression()) |
3248 | { | |
3249 | go_assert(saw_errors()); | |
3250 | return Expression::make_error(this->location()); | |
3251 | } | |
3252 | ||
2c809f8f | 3253 | if (((this->type()->is_string_type() |
3254 | && this->expr_->type()->is_slice_type()) | |
8ba8cc87 | 3255 | || this->expr_->type()->interface_type() != NULL) |
35a54f17 | 3256 | && !this->expr_->is_variable()) |
3257 | { | |
3258 | Temporary_statement* temp = | |
3259 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3260 | inserter->insert(temp); | |
3261 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3262 | } | |
3263 | return this; | |
3264 | } | |
3265 | ||
1ca01a59 | 3266 | // Return whether a type conversion is a constant. |
3267 | ||
3268 | bool | |
3269 | Type_conversion_expression::do_is_constant() const | |
3270 | { | |
3271 | if (!this->expr_->is_constant()) | |
3272 | return false; | |
3273 | ||
3274 | // A conversion to a type that may not be used as a constant is not | |
3275 | // a constant. For example, []byte(nil). | |
3276 | Type* type = this->type_; | |
3277 | if (type->integer_type() == NULL | |
3278 | && type->float_type() == NULL | |
3279 | && type->complex_type() == NULL | |
3280 | && !type->is_boolean_type() | |
3281 | && !type->is_string_type()) | |
3282 | return false; | |
3283 | ||
3284 | return true; | |
3285 | } | |
3286 | ||
0e168074 | 3287 | // Return whether a type conversion is immutable. |
3288 | ||
3289 | bool | |
3290 | Type_conversion_expression::do_is_immutable() const | |
3291 | { | |
3292 | Type* type = this->type_; | |
3293 | Type* expr_type = this->expr_->type(); | |
3294 | ||
3295 | if (type->interface_type() != NULL | |
3296 | || expr_type->interface_type() != NULL) | |
3297 | return false; | |
3298 | ||
3299 | if (!this->expr_->is_immutable()) | |
3300 | return false; | |
3301 | ||
3302 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3303 | return true; | |
3304 | ||
3305 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3306 | } | |
3307 | ||
0c77715b | 3308 | // Return the constant numeric value if there is one. |
e440a328 | 3309 | |
3310 | bool | |
0c77715b | 3311 | Type_conversion_expression::do_numeric_constant_value( |
3312 | Numeric_constant* nc) const | |
e440a328 | 3313 | { |
0c77715b | 3314 | if (!this->type_->is_numeric_type()) |
e440a328 | 3315 | return false; |
0c77715b | 3316 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3317 | return false; |
0c77715b | 3318 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3319 | } |
3320 | ||
3321 | // Return the constant string value if there is one. | |
3322 | ||
3323 | bool | |
3324 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3325 | { | |
3326 | if (this->type_->is_string_type() | |
3327 | && this->expr_->type()->integer_type() != NULL) | |
3328 | { | |
0c77715b | 3329 | Numeric_constant nc; |
3330 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3331 | { |
0c77715b | 3332 | unsigned long ival; |
3333 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3334 | { |
0c77715b | 3335 | val->clear(); |
3336 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3337 | return true; |
3338 | } | |
3339 | } | |
e440a328 | 3340 | } |
3341 | ||
3342 | // FIXME: Could handle conversion from const []int here. | |
3343 | ||
3344 | return false; | |
3345 | } | |
3346 | ||
da244e59 | 3347 | // Determine the resulting type of the conversion. |
3348 | ||
3349 | void | |
3350 | Type_conversion_expression::do_determine_type(const Type_context*) | |
3351 | { | |
3352 | Type_context subcontext(this->type_, false); | |
3353 | this->expr_->determine_type(&subcontext); | |
3354 | } | |
3355 | ||
e440a328 | 3356 | // Check that types are convertible. |
3357 | ||
3358 | void | |
3359 | Type_conversion_expression::do_check_types(Gogo*) | |
3360 | { | |
3361 | Type* type = this->type_; | |
3362 | Type* expr_type = this->expr_->type(); | |
3363 | std::string reason; | |
3364 | ||
5c13bd80 | 3365 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3366 | { |
842f6425 | 3367 | this->set_is_error(); |
3368 | return; | |
3369 | } | |
3370 | ||
e440a328 | 3371 | if (this->may_convert_function_types_ |
3372 | && type->function_type() != NULL | |
3373 | && expr_type->function_type() != NULL) | |
3374 | return; | |
3375 | ||
3376 | if (Type::are_convertible(type, expr_type, &reason)) | |
3377 | return; | |
3378 | ||
631d5788 | 3379 | go_error_at(this->location(), "%s", reason.c_str()); |
e440a328 | 3380 | this->set_is_error(); |
3381 | } | |
3382 | ||
ea664253 | 3383 | // Get the backend representation for a type conversion. |
e440a328 | 3384 | |
ea664253 | 3385 | Bexpression* |
3386 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3387 | { |
e440a328 | 3388 | Type* type = this->type_; |
3389 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3390 | |
3391 | Gogo* gogo = context->gogo(); | |
3392 | Btype* btype = type->get_backend(gogo); | |
ea664253 | 3393 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3394 | Location loc = this->location(); |
3395 | ||
3396 | if (Type::are_identical(type, expr_type, false, NULL)) | |
ea664253 | 3397 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
2c809f8f | 3398 | else if (type->interface_type() != NULL |
3399 | || expr_type->interface_type() != NULL) | |
e440a328 | 3400 | { |
2c809f8f | 3401 | Expression* conversion = |
3402 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3403 | this->location()); | |
ea664253 | 3404 | return conversion->get_backend(context); |
e440a328 | 3405 | } |
3406 | else if (type->is_string_type() | |
3407 | && expr_type->integer_type() != NULL) | |
3408 | { | |
2c809f8f | 3409 | mpz_t intval; |
3410 | Numeric_constant nc; | |
3411 | if (this->expr_->numeric_constant_value(&nc) | |
3412 | && nc.to_int(&intval) | |
3413 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3414 | { |
e440a328 | 3415 | std::string s; |
2c809f8f | 3416 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3417 | mpz_clear(intval); | |
3418 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3419 | return se->get_backend(context); |
e440a328 | 3420 | } |
3421 | ||
f16ab008 | 3422 | Expression* i2s_expr = |
736a16ba | 3423 | Runtime::make_call(Runtime::INTSTRING, loc, 2, |
3424 | Expression::make_nil(loc), this->expr_); | |
ea664253 | 3425 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3426 | } |
55072f2b | 3427 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3428 | { |
55072f2b | 3429 | Array_type* a = expr_type->array_type(); |
e440a328 | 3430 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3431 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3432 | go_assert(this->expr_->is_variable()); |
3433 | ||
3434 | Runtime::Function code; | |
60963afd | 3435 | if (e->integer_type()->is_byte()) |
736a16ba | 3436 | code = Runtime::SLICEBYTETOSTRING; |
e440a328 | 3437 | else |
35a54f17 | 3438 | { |
3439 | go_assert(e->integer_type()->is_rune()); | |
736a16ba | 3440 | code = Runtime::SLICERUNETOSTRING; |
35a54f17 | 3441 | } |
736a16ba | 3442 | return Runtime::make_call(code, loc, 2, Expression::make_nil(loc), |
3443 | this->expr_)->get_backend(context); | |
e440a328 | 3444 | } |
411eb89e | 3445 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3446 | { |
3447 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3448 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3449 | |
2c809f8f | 3450 | Runtime::Function code; |
60963afd | 3451 | if (e->integer_type()->is_byte()) |
736a16ba | 3452 | code = Runtime::STRINGTOSLICEBYTE; |
e440a328 | 3453 | else |
3454 | { | |
60963afd | 3455 | go_assert(e->integer_type()->is_rune()); |
736a16ba | 3456 | code = Runtime::STRINGTOSLICERUNE; |
e440a328 | 3457 | } |
736a16ba | 3458 | Expression* s2a = Runtime::make_call(code, loc, 2, |
3459 | Expression::make_nil(loc), | |
3460 | this->expr_); | |
ea664253 | 3461 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3462 | } |
3463 | else if (type->is_numeric_type()) | |
3464 | { | |
3465 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
ea664253 | 3466 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3467 | } |
3468 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3469 | && (expr_type->points_to() != NULL |
3470 | || expr_type->integer_type())) | |
3471 | || (expr_type->is_unsafe_pointer_type() | |
3472 | && type->points_to() != NULL) | |
3473 | || (this->may_convert_function_types_ | |
3474 | && type->function_type() != NULL | |
3475 | && expr_type->function_type() != NULL)) | |
ea664253 | 3476 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3477 | else |
2c809f8f | 3478 | { |
3479 | Expression* conversion = | |
3480 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3481 | return conversion->get_backend(context); |
2c809f8f | 3482 | } |
e440a328 | 3483 | } |
3484 | ||
3485 | // Output a type conversion in a constant expression. | |
3486 | ||
3487 | void | |
3488 | Type_conversion_expression::do_export(Export* exp) const | |
3489 | { | |
3490 | exp->write_c_string("convert("); | |
3491 | exp->write_type(this->type_); | |
3492 | exp->write_c_string(", "); | |
3493 | this->expr_->export_expression(exp); | |
3494 | exp->write_c_string(")"); | |
3495 | } | |
3496 | ||
3497 | // Import a type conversion or a struct construction. | |
3498 | ||
3499 | Expression* | |
3500 | Type_conversion_expression::do_import(Import* imp) | |
3501 | { | |
3502 | imp->require_c_string("convert("); | |
3503 | Type* type = imp->read_type(); | |
3504 | imp->require_c_string(", "); | |
3505 | Expression* val = Expression::import_expression(imp); | |
3506 | imp->require_c_string(")"); | |
3507 | return Expression::make_cast(type, val, imp->location()); | |
3508 | } | |
3509 | ||
d751bb78 | 3510 | // Dump ast representation for a type conversion expression. |
3511 | ||
3512 | void | |
3513 | Type_conversion_expression::do_dump_expression( | |
3514 | Ast_dump_context* ast_dump_context) const | |
3515 | { | |
3516 | ast_dump_context->dump_type(this->type_); | |
3517 | ast_dump_context->ostream() << "("; | |
3518 | ast_dump_context->dump_expression(this->expr_); | |
3519 | ast_dump_context->ostream() << ") "; | |
3520 | } | |
3521 | ||
e440a328 | 3522 | // Make a type cast expression. |
3523 | ||
3524 | Expression* | |
b13c66cd | 3525 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3526 | { |
3527 | if (type->is_error_type() || val->is_error_expression()) | |
3528 | return Expression::make_error(location); | |
3529 | return new Type_conversion_expression(type, val, location); | |
3530 | } | |
3531 | ||
98f62f7a | 3532 | // Class Unsafe_type_conversion_expression. |
9581e91d | 3533 | |
3534 | // Traversal. | |
3535 | ||
3536 | int | |
3537 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3538 | { | |
3539 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3540 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3541 | return TRAVERSE_EXIT; | |
3542 | return TRAVERSE_CONTINUE; | |
3543 | } | |
3544 | ||
aa5ae575 | 3545 | // Return whether an unsafe type conversion is immutable. |
3546 | ||
3547 | bool | |
3548 | Unsafe_type_conversion_expression::do_is_immutable() const | |
3549 | { | |
3550 | Type* type = this->type_; | |
3551 | Type* expr_type = this->expr_->type(); | |
3552 | ||
3553 | if (type->interface_type() != NULL | |
3554 | || expr_type->interface_type() != NULL) | |
3555 | return false; | |
3556 | ||
3557 | if (!this->expr_->is_immutable()) | |
3558 | return false; | |
3559 | ||
3560 | if (Type::are_convertible(type, expr_type, NULL)) | |
3561 | return true; | |
3562 | ||
3563 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3564 | } | |
3565 | ||
9581e91d | 3566 | // Convert to backend representation. |
3567 | ||
ea664253 | 3568 | Bexpression* |
3569 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3570 | { |
3571 | // We are only called for a limited number of cases. | |
3572 | ||
3573 | Type* t = this->type_; | |
3574 | Type* et = this->expr_->type(); | |
5c4802f1 | 3575 | |
3576 | if (t->is_error_type() | |
3577 | || this->expr_->is_error_expression() | |
3578 | || et->is_error_type()) | |
3579 | { | |
3580 | go_assert(saw_errors()); | |
3581 | return context->backend()->error_expression(); | |
3582 | } | |
3583 | ||
2c809f8f | 3584 | if (t->array_type() != NULL) |
3585 | go_assert(et->array_type() != NULL | |
3586 | && t->is_slice_type() == et->is_slice_type()); | |
3587 | else if (t->struct_type() != NULL) | |
9581e91d | 3588 | { |
2c809f8f | 3589 | if (t->named_type() != NULL |
3590 | && et->named_type() != NULL | |
3591 | && !Type::are_convertible(t, et, NULL)) | |
3592 | { | |
3593 | go_assert(saw_errors()); | |
ea664253 | 3594 | return context->backend()->error_expression(); |
2c809f8f | 3595 | } |
3596 | ||
3597 | go_assert(et->struct_type() != NULL | |
3598 | && Type::are_convertible(t, et, NULL)); | |
3599 | } | |
3600 | else if (t->map_type() != NULL) | |
c484d925 | 3601 | go_assert(et->map_type() != NULL); |
9581e91d | 3602 | else if (t->channel_type() != NULL) |
c484d925 | 3603 | go_assert(et->channel_type() != NULL); |
09ea332d | 3604 | else if (t->points_to() != NULL) |
2c809f8f | 3605 | go_assert(et->points_to() != NULL |
3606 | || et->channel_type() != NULL | |
3607 | || et->map_type() != NULL | |
3608 | || et->function_type() != NULL | |
132ed071 | 3609 | || et->integer_type() != NULL |
2c809f8f | 3610 | || et->is_nil_type()); |
9581e91d | 3611 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3612 | go_assert(t->points_to() != NULL); |
2c809f8f | 3613 | else if (t->interface_type() != NULL) |
9581e91d | 3614 | { |
2c809f8f | 3615 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3616 | go_assert(et->interface_type() != NULL |
2c809f8f | 3617 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3618 | } |
588e3cf9 | 3619 | else if (t->integer_type() != NULL) |
2c809f8f | 3620 | go_assert(et->is_boolean_type() |
3621 | || et->integer_type() != NULL | |
3622 | || et->function_type() != NULL | |
3623 | || et->points_to() != NULL | |
3624 | || et->map_type() != NULL | |
8ba8cc87 | 3625 | || et->channel_type() != NULL |
3626 | || et->is_nil_type()); | |
cd39797e | 3627 | else if (t->function_type() != NULL) |
3628 | go_assert(et->points_to() != NULL); | |
9581e91d | 3629 | else |
c3e6f413 | 3630 | go_unreachable(); |
9581e91d | 3631 | |
2c809f8f | 3632 | Gogo* gogo = context->gogo(); |
3633 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3634 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3635 | Location loc = this->location(); |
ea664253 | 3636 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3637 | } |
3638 | ||
d751bb78 | 3639 | // Dump ast representation for an unsafe type conversion expression. |
3640 | ||
3641 | void | |
3642 | Unsafe_type_conversion_expression::do_dump_expression( | |
3643 | Ast_dump_context* ast_dump_context) const | |
3644 | { | |
3645 | ast_dump_context->dump_type(this->type_); | |
3646 | ast_dump_context->ostream() << "("; | |
3647 | ast_dump_context->dump_expression(this->expr_); | |
3648 | ast_dump_context->ostream() << ") "; | |
3649 | } | |
3650 | ||
9581e91d | 3651 | // Make an unsafe type conversion expression. |
3652 | ||
3653 | Expression* | |
3654 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3655 | Location location) |
9581e91d | 3656 | { |
3657 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3658 | } | |
3659 | ||
76f85fd6 | 3660 | // Class Unary_expression. |
e440a328 | 3661 | |
3662 | // If we are taking the address of a composite literal, and the | |
2c809f8f | 3663 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3664 | // instead. |
3665 | ||
3666 | Expression* | |
ceeb4318 | 3667 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3668 | { |
b13c66cd | 3669 | Location loc = this->location(); |
e440a328 | 3670 | Operator op = this->op_; |
3671 | Expression* expr = this->expr_; | |
3672 | ||
3673 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3674 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3675 | ||
3676 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3677 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3678 | // analysis here? This case is found in math/unsafe.go and is | |
3679 | // therefore worth special casing. | |
3680 | if (op == OPERATOR_MULT) | |
3681 | { | |
3682 | Expression* e = expr; | |
3683 | while (e->classification() == EXPRESSION_CONVERSION) | |
3684 | { | |
3685 | Type_conversion_expression* te | |
3686 | = static_cast<Type_conversion_expression*>(e); | |
3687 | e = te->expr(); | |
3688 | } | |
3689 | ||
3690 | if (e->classification() == EXPRESSION_UNARY) | |
3691 | { | |
3692 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3693 | if (ue->op_ == OPERATOR_AND) | |
3694 | { | |
3695 | if (e == expr) | |
3696 | { | |
3697 | // *&x == x. | |
f4dea966 | 3698 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3699 | { | |
631d5788 | 3700 | go_error_at(ue->location(), |
3701 | "invalid operand for unary %<&%>"); | |
f4dea966 | 3702 | this->set_is_error(); |
3703 | } | |
e440a328 | 3704 | return ue->expr_; |
3705 | } | |
3706 | ue->set_does_not_escape(); | |
3707 | } | |
3708 | } | |
3709 | } | |
3710 | ||
55661ce9 | 3711 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3712 | // having to deal with TYPE_VOID in other places. | |
3713 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3714 | { | |
631d5788 | 3715 | go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); |
55661ce9 | 3716 | return Expression::make_error(this->location()); |
3717 | } | |
3718 | ||
d9f3743a | 3719 | // Check for an invalid pointer dereference. We need to do this |
3720 | // here because Unary_expression::do_type will return an error type | |
3721 | // in this case. That can cause code to appear erroneous, and | |
3722 | // therefore disappear at lowering time, without any error message. | |
3723 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3724 | { | |
3725 | this->report_error(_("expected pointer")); | |
3726 | return Expression::make_error(this->location()); | |
3727 | } | |
3728 | ||
59a401fe | 3729 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3730 | { |
0c77715b | 3731 | Numeric_constant nc; |
3732 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3733 | { |
0c77715b | 3734 | Numeric_constant result; |
3735 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3736 | return result.expression(loc); | |
e440a328 | 3737 | } |
3738 | } | |
3739 | ||
3740 | return this; | |
3741 | } | |
3742 | ||
f9ca30f9 | 3743 | // Flatten expression if a nil check must be performed and create temporary |
3744 | // variables if necessary. | |
3745 | ||
3746 | Expression* | |
3747 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3748 | Statement_inserter* inserter) | |
3749 | { | |
5bf8be8b | 3750 | if (this->is_error_expression() |
3751 | || this->expr_->is_error_expression() | |
3752 | || this->expr_->type()->is_error_type()) | |
3753 | { | |
3754 | go_assert(saw_errors()); | |
3755 | return Expression::make_error(this->location()); | |
3756 | } | |
f4dea966 | 3757 | |
f9ca30f9 | 3758 | Location location = this->location(); |
3759 | if (this->op_ == OPERATOR_MULT | |
3760 | && !this->expr_->is_variable()) | |
3761 | { | |
3762 | go_assert(this->expr_->type()->points_to() != NULL); | |
3763 | Type* ptype = this->expr_->type()->points_to(); | |
3764 | if (!ptype->is_void_type()) | |
3765 | { | |
2a305b85 | 3766 | int64_t s; |
3767 | bool ok = ptype->backend_type_size(gogo, &s); | |
3768 | if (!ok) | |
3769 | { | |
3770 | go_assert(saw_errors()); | |
3771 | return Expression::make_error(this->location()); | |
3772 | } | |
f9ca30f9 | 3773 | if (s >= 4096 || this->issue_nil_check_) |
3774 | { | |
3775 | Temporary_statement* temp = | |
3776 | Statement::make_temporary(NULL, this->expr_, location); | |
3777 | inserter->insert(temp); | |
3778 | this->expr_ = | |
3779 | Expression::make_temporary_reference(temp, location); | |
3780 | } | |
3781 | } | |
3782 | } | |
3783 | ||
da244e59 | 3784 | if (this->op_ == OPERATOR_AND) |
3785 | { | |
8ff995b5 | 3786 | // If this->escapes_ is false at this point, then it was set to |
3787 | // false by an explicit call to set_does_not_escape, and the | |
3788 | // value does not escape. If this->escapes_ is true, we may be | |
3789 | // able to set it to false if taking the address of a variable | |
3790 | // that does not escape. | |
45ff893b | 3791 | Node* n = Node::make_node(this); |
3792 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
3793 | this->escapes_ = false; | |
3794 | ||
0dad6de4 | 3795 | // When compiling the runtime, the address operator does not |
3796 | // cause local variables to escapes. When escape analysis | |
3797 | // becomes the default, this should be changed to make it an | |
3798 | // error if we have an address operator that escapes. | |
3799 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
3800 | this->escapes_ = false; | |
3801 | ||
45ff893b | 3802 | Named_object* var = NULL; |
3803 | if (this->expr_->var_expression() != NULL) | |
3804 | var = this->expr_->var_expression()->named_object(); | |
3805 | else if (this->expr_->enclosed_var_expression() != NULL) | |
3806 | var = this->expr_->enclosed_var_expression()->variable(); | |
3807 | ||
3808 | if (this->escapes_ && var != NULL) | |
da244e59 | 3809 | { |
da244e59 | 3810 | if (var->is_variable()) |
3811 | this->escapes_ = var->var_value()->escapes(); | |
3812 | if (var->is_result_variable()) | |
3813 | this->escapes_ = var->result_var_value()->escapes(); | |
3814 | } | |
3815 | this->expr_->address_taken(this->escapes_); | |
3816 | } | |
3817 | ||
f9ca30f9 | 3818 | if (this->create_temp_ && !this->expr_->is_variable()) |
3819 | { | |
3820 | Temporary_statement* temp = | |
3821 | Statement::make_temporary(NULL, this->expr_, location); | |
3822 | inserter->insert(temp); | |
3823 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3824 | } | |
3825 | ||
3826 | return this; | |
3827 | } | |
3828 | ||
e440a328 | 3829 | // Return whether a unary expression is a constant. |
3830 | ||
3831 | bool | |
3832 | Unary_expression::do_is_constant() const | |
3833 | { | |
3834 | if (this->op_ == OPERATOR_MULT) | |
3835 | { | |
3836 | // Indirecting through a pointer is only constant if the object | |
3837 | // to which the expression points is constant, but we currently | |
3838 | // have no way to determine that. | |
3839 | return false; | |
3840 | } | |
3841 | else if (this->op_ == OPERATOR_AND) | |
3842 | { | |
3843 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3844 | // global variable, not constant otherwise. In other cases taking the |
3845 | // address is probably not a constant. | |
e440a328 | 3846 | Var_expression* ve = this->expr_->var_expression(); |
3847 | if (ve != NULL) | |
3848 | { | |
3849 | Named_object* no = ve->named_object(); | |
3850 | return no->is_variable() && no->var_value()->is_global(); | |
3851 | } | |
3852 | return false; | |
3853 | } | |
3854 | else | |
3855 | return this->expr_->is_constant(); | |
3856 | } | |
3857 | ||
0c77715b | 3858 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3859 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3860 | |
3861 | bool | |
0c77715b | 3862 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3863 | Location location, Numeric_constant* nc) | |
e440a328 | 3864 | { |
3865 | switch (op) | |
3866 | { | |
3867 | case OPERATOR_PLUS: | |
0c77715b | 3868 | *nc = *unc; |
e440a328 | 3869 | return true; |
0c77715b | 3870 | |
e440a328 | 3871 | case OPERATOR_MINUS: |
0c77715b | 3872 | if (unc->is_int() || unc->is_rune()) |
3873 | break; | |
3874 | else if (unc->is_float()) | |
3875 | { | |
3876 | mpfr_t uval; | |
3877 | unc->get_float(&uval); | |
3878 | mpfr_t val; | |
3879 | mpfr_init(val); | |
3880 | mpfr_neg(val, uval, GMP_RNDN); | |
3881 | nc->set_float(unc->type(), val); | |
3882 | mpfr_clear(uval); | |
3883 | mpfr_clear(val); | |
3884 | return true; | |
3885 | } | |
3886 | else if (unc->is_complex()) | |
3887 | { | |
fcbea5e4 | 3888 | mpc_t uval; |
3889 | unc->get_complex(&uval); | |
3890 | mpc_t val; | |
3891 | mpc_init2(val, mpc_precision); | |
3892 | mpc_neg(val, uval, MPC_RNDNN); | |
3893 | nc->set_complex(unc->type(), val); | |
3894 | mpc_clear(uval); | |
3895 | mpc_clear(val); | |
0c77715b | 3896 | return true; |
3897 | } | |
e440a328 | 3898 | else |
0c77715b | 3899 | go_unreachable(); |
e440a328 | 3900 | |
0c77715b | 3901 | case OPERATOR_XOR: |
3902 | break; | |
68448d53 | 3903 | |
59a401fe | 3904 | case OPERATOR_NOT: |
e440a328 | 3905 | case OPERATOR_AND: |
3906 | case OPERATOR_MULT: | |
3907 | return false; | |
0c77715b | 3908 | |
e440a328 | 3909 | default: |
c3e6f413 | 3910 | go_unreachable(); |
e440a328 | 3911 | } |
e440a328 | 3912 | |
0c77715b | 3913 | if (!unc->is_int() && !unc->is_rune()) |
3914 | return false; | |
3915 | ||
3916 | mpz_t uval; | |
8387e1df | 3917 | if (unc->is_rune()) |
3918 | unc->get_rune(&uval); | |
3919 | else | |
3920 | unc->get_int(&uval); | |
0c77715b | 3921 | mpz_t val; |
3922 | mpz_init(val); | |
e440a328 | 3923 | |
e440a328 | 3924 | switch (op) |
3925 | { | |
e440a328 | 3926 | case OPERATOR_MINUS: |
0c77715b | 3927 | mpz_neg(val, uval); |
3928 | break; | |
3929 | ||
e440a328 | 3930 | case OPERATOR_NOT: |
0c77715b | 3931 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3932 | break; | |
3933 | ||
e440a328 | 3934 | case OPERATOR_XOR: |
0c77715b | 3935 | { |
3936 | Type* utype = unc->type(); | |
3937 | if (utype->integer_type() == NULL | |
3938 | || utype->integer_type()->is_abstract()) | |
3939 | mpz_com(val, uval); | |
3940 | else | |
3941 | { | |
3942 | // The number of HOST_WIDE_INTs that it takes to represent | |
3943 | // UVAL. | |
3944 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3945 | + HOST_BITS_PER_WIDE_INT | |
3946 | - 1) | |
3947 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3948 | |
0c77715b | 3949 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3950 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3951 | ||
3952 | size_t obits = utype->integer_type()->bits(); | |
3953 | ||
3954 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3955 | { | |
3956 | mpz_t adj; | |
3957 | mpz_init_set_ui(adj, 1); | |
3958 | mpz_mul_2exp(adj, adj, obits); | |
3959 | mpz_add(uval, uval, adj); | |
3960 | mpz_clear(adj); | |
3961 | } | |
3962 | ||
3963 | size_t ecount; | |
3964 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3965 | go_assert(ecount <= count); | |
3966 | ||
3967 | // Trim down to the number of words required by the type. | |
3968 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3969 | / HOST_BITS_PER_WIDE_INT); | |
3970 | go_assert(ocount <= count); | |
3971 | ||
3972 | for (size_t i = 0; i < ocount; ++i) | |
3973 | phwi[i] = ~phwi[i]; | |
3974 | ||
3975 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3976 | if (clearbits != 0) | |
3977 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3978 | >> clearbits); | |
3979 | ||
3980 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3981 | ||
3982 | if (!utype->integer_type()->is_unsigned() | |
3983 | && mpz_tstbit(val, obits - 1)) | |
3984 | { | |
3985 | mpz_t adj; | |
3986 | mpz_init_set_ui(adj, 1); | |
3987 | mpz_mul_2exp(adj, adj, obits); | |
3988 | mpz_sub(val, val, adj); | |
3989 | mpz_clear(adj); | |
3990 | } | |
3991 | ||
3992 | delete[] phwi; | |
3993 | } | |
3994 | } | |
3995 | break; | |
e440a328 | 3996 | |
e440a328 | 3997 | default: |
c3e6f413 | 3998 | go_unreachable(); |
e440a328 | 3999 | } |
e440a328 | 4000 | |
0c77715b | 4001 | if (unc->is_rune()) |
4002 | nc->set_rune(NULL, val); | |
e440a328 | 4003 | else |
0c77715b | 4004 | nc->set_int(NULL, val); |
e440a328 | 4005 | |
0c77715b | 4006 | mpz_clear(uval); |
4007 | mpz_clear(val); | |
e440a328 | 4008 | |
0c77715b | 4009 | return nc->set_type(unc->type(), true, location); |
e440a328 | 4010 | } |
4011 | ||
0c77715b | 4012 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 4013 | |
4014 | bool | |
0c77715b | 4015 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 4016 | { |
0c77715b | 4017 | Numeric_constant unc; |
4018 | if (!this->expr_->numeric_constant_value(&unc)) | |
4019 | return false; | |
4020 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
4021 | nc); | |
e440a328 | 4022 | } |
4023 | ||
4024 | // Return the type of a unary expression. | |
4025 | ||
4026 | Type* | |
4027 | Unary_expression::do_type() | |
4028 | { | |
4029 | switch (this->op_) | |
4030 | { | |
4031 | case OPERATOR_PLUS: | |
4032 | case OPERATOR_MINUS: | |
4033 | case OPERATOR_NOT: | |
4034 | case OPERATOR_XOR: | |
4035 | return this->expr_->type(); | |
4036 | ||
4037 | case OPERATOR_AND: | |
4038 | return Type::make_pointer_type(this->expr_->type()); | |
4039 | ||
4040 | case OPERATOR_MULT: | |
4041 | { | |
4042 | Type* subtype = this->expr_->type(); | |
4043 | Type* points_to = subtype->points_to(); | |
4044 | if (points_to == NULL) | |
4045 | return Type::make_error_type(); | |
4046 | return points_to; | |
4047 | } | |
4048 | ||
4049 | default: | |
c3e6f413 | 4050 | go_unreachable(); |
e440a328 | 4051 | } |
4052 | } | |
4053 | ||
4054 | // Determine abstract types for a unary expression. | |
4055 | ||
4056 | void | |
4057 | Unary_expression::do_determine_type(const Type_context* context) | |
4058 | { | |
4059 | switch (this->op_) | |
4060 | { | |
4061 | case OPERATOR_PLUS: | |
4062 | case OPERATOR_MINUS: | |
4063 | case OPERATOR_NOT: | |
4064 | case OPERATOR_XOR: | |
4065 | this->expr_->determine_type(context); | |
4066 | break; | |
4067 | ||
4068 | case OPERATOR_AND: | |
4069 | // Taking the address of something. | |
4070 | { | |
4071 | Type* subtype = (context->type == NULL | |
4072 | ? NULL | |
4073 | : context->type->points_to()); | |
4074 | Type_context subcontext(subtype, false); | |
4075 | this->expr_->determine_type(&subcontext); | |
4076 | } | |
4077 | break; | |
4078 | ||
4079 | case OPERATOR_MULT: | |
4080 | // Indirecting through a pointer. | |
4081 | { | |
4082 | Type* subtype = (context->type == NULL | |
4083 | ? NULL | |
4084 | : Type::make_pointer_type(context->type)); | |
4085 | Type_context subcontext(subtype, false); | |
4086 | this->expr_->determine_type(&subcontext); | |
4087 | } | |
4088 | break; | |
4089 | ||
4090 | default: | |
c3e6f413 | 4091 | go_unreachable(); |
e440a328 | 4092 | } |
4093 | } | |
4094 | ||
4095 | // Check types for a unary expression. | |
4096 | ||
4097 | void | |
4098 | Unary_expression::do_check_types(Gogo*) | |
4099 | { | |
9fe897ef | 4100 | Type* type = this->expr_->type(); |
5c13bd80 | 4101 | if (type->is_error()) |
9fe897ef | 4102 | { |
4103 | this->set_is_error(); | |
4104 | return; | |
4105 | } | |
4106 | ||
e440a328 | 4107 | switch (this->op_) |
4108 | { | |
4109 | case OPERATOR_PLUS: | |
4110 | case OPERATOR_MINUS: | |
9fe897ef | 4111 | if (type->integer_type() == NULL |
4112 | && type->float_type() == NULL | |
4113 | && type->complex_type() == NULL) | |
4114 | this->report_error(_("expected numeric type")); | |
e440a328 | 4115 | break; |
4116 | ||
4117 | case OPERATOR_NOT: | |
59a401fe | 4118 | if (!type->is_boolean_type()) |
4119 | this->report_error(_("expected boolean type")); | |
4120 | break; | |
4121 | ||
e440a328 | 4122 | case OPERATOR_XOR: |
b3b1474e | 4123 | if (type->integer_type() == NULL) |
4124 | this->report_error(_("expected integer")); | |
e440a328 | 4125 | break; |
4126 | ||
4127 | case OPERATOR_AND: | |
4128 | if (!this->expr_->is_addressable()) | |
09ea332d | 4129 | { |
4130 | if (!this->create_temp_) | |
f4dea966 | 4131 | { |
631d5788 | 4132 | go_error_at(this->location(), "invalid operand for unary %<&%>"); |
f4dea966 | 4133 | this->set_is_error(); |
4134 | } | |
09ea332d | 4135 | } |
e440a328 | 4136 | else |
da244e59 | 4137 | this->expr_->issue_nil_check(); |
e440a328 | 4138 | break; |
4139 | ||
4140 | case OPERATOR_MULT: | |
4141 | // Indirecting through a pointer. | |
9fe897ef | 4142 | if (type->points_to() == NULL) |
4143 | this->report_error(_("expected pointer")); | |
7661d702 | 4144 | if (type->points_to()->is_error()) |
4145 | this->set_is_error(); | |
e440a328 | 4146 | break; |
4147 | ||
4148 | default: | |
c3e6f413 | 4149 | go_unreachable(); |
e440a328 | 4150 | } |
4151 | } | |
4152 | ||
ea664253 | 4153 | // Get the backend representation for a unary expression. |
e440a328 | 4154 | |
ea664253 | 4155 | Bexpression* |
4156 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4157 | { |
1b1f2abf | 4158 | Gogo* gogo = context->gogo(); |
e9d3367e | 4159 | Location loc = this->location(); |
4160 | ||
4161 | // Taking the address of a set-and-use-temporary expression requires | |
4162 | // setting the temporary and then taking the address. | |
4163 | if (this->op_ == OPERATOR_AND) | |
4164 | { | |
4165 | Set_and_use_temporary_expression* sut = | |
4166 | this->expr_->set_and_use_temporary_expression(); | |
4167 | if (sut != NULL) | |
4168 | { | |
4169 | Temporary_statement* temp = sut->temporary(); | |
4170 | Bvariable* bvar = temp->get_backend_variable(context); | |
f9ca30f9 | 4171 | Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4172 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4173 | |
4174 | Bstatement* bassign = | |
4175 | gogo->backend()->assignment_statement(bvar_expr, bval, loc); | |
4176 | Bexpression* bvar_addr = | |
4177 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4178 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4179 | } |
4180 | } | |
4181 | ||
f9ca30f9 | 4182 | Bexpression* ret; |
ea664253 | 4183 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4184 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4185 | switch (this->op_) |
4186 | { | |
4187 | case OPERATOR_PLUS: | |
f9ca30f9 | 4188 | ret = bexpr; |
4189 | break; | |
e440a328 | 4190 | |
4191 | case OPERATOR_MINUS: | |
f9ca30f9 | 4192 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4193 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4194 | break; | |
e440a328 | 4195 | |
4196 | case OPERATOR_NOT: | |
e440a328 | 4197 | case OPERATOR_XOR: |
f9ca30f9 | 4198 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4199 | break; | |
e440a328 | 4200 | |
4201 | case OPERATOR_AND: | |
09ea332d | 4202 | if (!this->create_temp_) |
4203 | { | |
4204 | // We should not see a non-constant constructor here; cases | |
4205 | // where we would see one should have been moved onto the | |
4206 | // heap at parse time. Taking the address of a nonconstant | |
4207 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4208 | |
4209 | go_assert(!this->expr_->is_composite_literal() | |
4210 | || this->expr_->is_immutable()); | |
24060bf9 | 4211 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4212 | { | |
4213 | Unary_expression* ue = | |
4214 | static_cast<Unary_expression*>(this->expr_); | |
4215 | go_assert(ue->op() != OPERATOR_AND); | |
4216 | } | |
09ea332d | 4217 | } |
e440a328 | 4218 | |
f23d7786 | 4219 | static unsigned int counter; |
4220 | char buf[100]; | |
4221 | if (this->is_gc_root_ || this->is_slice_init_) | |
76f85fd6 | 4222 | { |
f23d7786 | 4223 | bool copy_to_heap = false; |
4224 | if (this->is_gc_root_) | |
4225 | { | |
4226 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4227 | // they cannot be represented as an immutable_struct in the | |
4228 | // backend. | |
4229 | static unsigned int root_counter; | |
4230 | snprintf(buf, sizeof buf, "gc%u", root_counter); | |
4231 | ++root_counter; | |
4232 | } | |
4233 | else | |
4234 | { | |
4235 | // Build a decl for a slice value initializer. An immutable slice | |
4236 | // value initializer may have to be copied to the heap if it | |
4237 | // contains pointers in a non-constant context. | |
4238 | snprintf(buf, sizeof buf, "C%u", counter); | |
4239 | ++counter; | |
4240 | ||
4241 | Array_type* at = this->expr_->type()->array_type(); | |
4242 | go_assert(at != NULL); | |
4243 | ||
4244 | // If we are not copying the value to the heap, we will only | |
4245 | // initialize the value once, so we can use this directly | |
4246 | // rather than copying it. In that case we can't make it | |
4247 | // read-only, because the program is permitted to change it. | |
4248 | copy_to_heap = (at->element_type()->has_pointer() | |
4249 | && !context->is_const()); | |
4250 | } | |
4251 | Bvariable* implicit = | |
aa5ae575 | 4252 | gogo->backend()->implicit_variable(buf, btype, true, copy_to_heap, |
5892f89f | 4253 | false, 0); |
aa5ae575 | 4254 | gogo->backend()->implicit_variable_set_init(implicit, buf, btype, |
4255 | true, copy_to_heap, false, | |
4256 | bexpr); | |
f23d7786 | 4257 | bexpr = gogo->backend()->var_expression(implicit, loc); |
76f85fd6 | 4258 | } |
4259 | else if ((this->expr_->is_composite_literal() | |
f9ca30f9 | 4260 | || this->expr_->string_expression() != NULL) |
4261 | && this->expr_->is_immutable()) | |
4262 | { | |
76f85fd6 | 4263 | // Build a decl for a constant constructor. |
f9ca30f9 | 4264 | snprintf(buf, sizeof buf, "C%u", counter); |
4265 | ++counter; | |
4266 | ||
4267 | Bvariable* decl = | |
4268 | gogo->backend()->immutable_struct(buf, true, false, btype, loc); | |
4269 | gogo->backend()->immutable_struct_set_init(decl, buf, true, false, | |
4270 | btype, loc, bexpr); | |
4271 | bexpr = gogo->backend()->var_expression(decl, loc); | |
4272 | } | |
09ea332d | 4273 | |
f9ca30f9 | 4274 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4275 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4276 | break; | |
e440a328 | 4277 | |
4278 | case OPERATOR_MULT: | |
4279 | { | |
f9ca30f9 | 4280 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4281 | |
4282 | // If we are dereferencing the pointer to a large struct, we | |
4283 | // need to check for nil. We don't bother to check for small | |
4284 | // structs because we expect the system to crash on a nil | |
56080003 | 4285 | // pointer dereference. However, if we know the address of this |
4286 | // expression is being taken, we must always check for nil. | |
f9ca30f9 | 4287 | |
4288 | Type* ptype = this->expr_->type()->points_to(); | |
4289 | Btype* pbtype = ptype->get_backend(gogo); | |
4290 | if (!ptype->is_void_type()) | |
e440a328 | 4291 | { |
2a305b85 | 4292 | int64_t s; |
4293 | bool ok = ptype->backend_type_size(gogo, &s); | |
4294 | if (!ok) | |
4295 | { | |
4296 | go_assert(saw_errors()); | |
4297 | return gogo->backend()->error_expression(); | |
4298 | } | |
f9ca30f9 | 4299 | if (s >= 4096 || this->issue_nil_check_) |
19b4f09b | 4300 | { |
f9ca30f9 | 4301 | go_assert(this->expr_->is_variable()); |
ea664253 | 4302 | Bexpression* nil = |
4303 | Expression::make_nil(loc)->get_backend(context); | |
f9ca30f9 | 4304 | Bexpression* compare = |
4305 | gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr, | |
4306 | nil, loc); | |
f9ca30f9 | 4307 | Bexpression* crash = |
ea664253 | 4308 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4309 | loc)->get_backend(context); | |
f9ca30f9 | 4310 | bexpr = gogo->backend()->conditional_expression(btype, compare, |
4311 | crash, bexpr, | |
4312 | loc); | |
4313 | ||
19b4f09b | 4314 | } |
e440a328 | 4315 | } |
9b27b43c | 4316 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); |
e440a328 | 4317 | } |
f9ca30f9 | 4318 | break; |
e440a328 | 4319 | |
4320 | default: | |
c3e6f413 | 4321 | go_unreachable(); |
e440a328 | 4322 | } |
f9ca30f9 | 4323 | |
ea664253 | 4324 | return ret; |
e440a328 | 4325 | } |
4326 | ||
4327 | // Export a unary expression. | |
4328 | ||
4329 | void | |
4330 | Unary_expression::do_export(Export* exp) const | |
4331 | { | |
4332 | switch (this->op_) | |
4333 | { | |
4334 | case OPERATOR_PLUS: | |
4335 | exp->write_c_string("+ "); | |
4336 | break; | |
4337 | case OPERATOR_MINUS: | |
4338 | exp->write_c_string("- "); | |
4339 | break; | |
4340 | case OPERATOR_NOT: | |
4341 | exp->write_c_string("! "); | |
4342 | break; | |
4343 | case OPERATOR_XOR: | |
4344 | exp->write_c_string("^ "); | |
4345 | break; | |
4346 | case OPERATOR_AND: | |
4347 | case OPERATOR_MULT: | |
4348 | default: | |
c3e6f413 | 4349 | go_unreachable(); |
e440a328 | 4350 | } |
4351 | this->expr_->export_expression(exp); | |
4352 | } | |
4353 | ||
4354 | // Import a unary expression. | |
4355 | ||
4356 | Expression* | |
4357 | Unary_expression::do_import(Import* imp) | |
4358 | { | |
4359 | Operator op; | |
4360 | switch (imp->get_char()) | |
4361 | { | |
4362 | case '+': | |
4363 | op = OPERATOR_PLUS; | |
4364 | break; | |
4365 | case '-': | |
4366 | op = OPERATOR_MINUS; | |
4367 | break; | |
4368 | case '!': | |
4369 | op = OPERATOR_NOT; | |
4370 | break; | |
4371 | case '^': | |
4372 | op = OPERATOR_XOR; | |
4373 | break; | |
4374 | default: | |
c3e6f413 | 4375 | go_unreachable(); |
e440a328 | 4376 | } |
4377 | imp->require_c_string(" "); | |
4378 | Expression* expr = Expression::import_expression(imp); | |
4379 | return Expression::make_unary(op, expr, imp->location()); | |
4380 | } | |
4381 | ||
d751bb78 | 4382 | // Dump ast representation of an unary expression. |
4383 | ||
4384 | void | |
4385 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4386 | { | |
4387 | ast_dump_context->dump_operator(this->op_); | |
4388 | ast_dump_context->ostream() << "("; | |
4389 | ast_dump_context->dump_expression(this->expr_); | |
4390 | ast_dump_context->ostream() << ") "; | |
4391 | } | |
4392 | ||
e440a328 | 4393 | // Make a unary expression. |
4394 | ||
4395 | Expression* | |
b13c66cd | 4396 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4397 | { |
4398 | return new Unary_expression(op, expr, location); | |
4399 | } | |
4400 | ||
4401 | // If this is an indirection through a pointer, return the expression | |
4402 | // being pointed through. Otherwise return this. | |
4403 | ||
4404 | Expression* | |
4405 | Expression::deref() | |
4406 | { | |
4407 | if (this->classification_ == EXPRESSION_UNARY) | |
4408 | { | |
4409 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4410 | if (ue->op() == OPERATOR_MULT) | |
4411 | return ue->operand(); | |
4412 | } | |
4413 | return this; | |
4414 | } | |
4415 | ||
4416 | // Class Binary_expression. | |
4417 | ||
4418 | // Traversal. | |
4419 | ||
4420 | int | |
4421 | Binary_expression::do_traverse(Traverse* traverse) | |
4422 | { | |
4423 | int t = Expression::traverse(&this->left_, traverse); | |
4424 | if (t == TRAVERSE_EXIT) | |
4425 | return TRAVERSE_EXIT; | |
4426 | return Expression::traverse(&this->right_, traverse); | |
4427 | } | |
4428 | ||
0c77715b | 4429 | // Return the type to use for a binary operation on operands of |
4430 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4431 | // such may be NULL or abstract. | |
4432 | ||
4433 | bool | |
4434 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4435 | Type* right_type, Type** result_type) | |
4436 | { | |
4437 | if (left_type != right_type | |
4438 | && !left_type->is_abstract() | |
4439 | && !right_type->is_abstract() | |
4440 | && left_type->base() != right_type->base() | |
4441 | && op != OPERATOR_LSHIFT | |
4442 | && op != OPERATOR_RSHIFT) | |
4443 | { | |
4444 | // May be a type error--let it be diagnosed elsewhere. | |
4445 | return false; | |
4446 | } | |
4447 | ||
4448 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4449 | { | |
4450 | if (left_type->integer_type() != NULL) | |
4451 | *result_type = left_type; | |
4452 | else | |
4453 | *result_type = Type::make_abstract_integer_type(); | |
4454 | } | |
4455 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4456 | *result_type = left_type; | |
4457 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4458 | *result_type = right_type; | |
4459 | else if (!left_type->is_abstract()) | |
4460 | *result_type = left_type; | |
4461 | else if (!right_type->is_abstract()) | |
4462 | *result_type = right_type; | |
4463 | else if (left_type->complex_type() != NULL) | |
4464 | *result_type = left_type; | |
4465 | else if (right_type->complex_type() != NULL) | |
4466 | *result_type = right_type; | |
4467 | else if (left_type->float_type() != NULL) | |
4468 | *result_type = left_type; | |
4469 | else if (right_type->float_type() != NULL) | |
4470 | *result_type = right_type; | |
4471 | else if (left_type->integer_type() != NULL | |
4472 | && left_type->integer_type()->is_rune()) | |
4473 | *result_type = left_type; | |
4474 | else if (right_type->integer_type() != NULL | |
4475 | && right_type->integer_type()->is_rune()) | |
4476 | *result_type = right_type; | |
4477 | else | |
4478 | *result_type = left_type; | |
4479 | ||
4480 | return true; | |
4481 | } | |
4482 | ||
4483 | // Convert an integer comparison code and an operator to a boolean | |
4484 | // value. | |
e440a328 | 4485 | |
4486 | bool | |
0c77715b | 4487 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4488 | { |
e440a328 | 4489 | switch (op) |
4490 | { | |
4491 | case OPERATOR_EQEQ: | |
0c77715b | 4492 | return cmp == 0; |
4493 | break; | |
e440a328 | 4494 | case OPERATOR_NOTEQ: |
0c77715b | 4495 | return cmp != 0; |
4496 | break; | |
e440a328 | 4497 | case OPERATOR_LT: |
0c77715b | 4498 | return cmp < 0; |
4499 | break; | |
e440a328 | 4500 | case OPERATOR_LE: |
0c77715b | 4501 | return cmp <= 0; |
e440a328 | 4502 | case OPERATOR_GT: |
0c77715b | 4503 | return cmp > 0; |
e440a328 | 4504 | case OPERATOR_GE: |
0c77715b | 4505 | return cmp >= 0; |
e440a328 | 4506 | default: |
c3e6f413 | 4507 | go_unreachable(); |
e440a328 | 4508 | } |
4509 | } | |
4510 | ||
0c77715b | 4511 | // Compare constants according to OP. |
e440a328 | 4512 | |
4513 | bool | |
0c77715b | 4514 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4515 | Numeric_constant* right_nc, | |
4516 | Location location, bool* result) | |
e440a328 | 4517 | { |
0c77715b | 4518 | Type* left_type = left_nc->type(); |
4519 | Type* right_type = right_nc->type(); | |
4520 | ||
4521 | Type* type; | |
4522 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4523 | return false; | |
4524 | ||
4525 | // When comparing an untyped operand to a typed operand, we are | |
4526 | // effectively coercing the untyped operand to the other operand's | |
4527 | // type, so make sure that is valid. | |
4528 | if (!left_nc->set_type(type, true, location) | |
4529 | || !right_nc->set_type(type, true, location)) | |
4530 | return false; | |
4531 | ||
4532 | bool ret; | |
4533 | int cmp; | |
4534 | if (type->complex_type() != NULL) | |
4535 | { | |
4536 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4537 | return false; | |
4538 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4539 | } | |
4540 | else if (type->float_type() != NULL) | |
4541 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4542 | else |
0c77715b | 4543 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4544 | ||
4545 | if (ret) | |
4546 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4547 | ||
4548 | return ret; | |
4549 | } | |
4550 | ||
4551 | // Compare integer constants. | |
4552 | ||
4553 | bool | |
4554 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4555 | const Numeric_constant* right_nc, | |
4556 | int* cmp) | |
4557 | { | |
4558 | mpz_t left_val; | |
4559 | if (!left_nc->to_int(&left_val)) | |
4560 | return false; | |
4561 | mpz_t right_val; | |
4562 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4563 | { |
0c77715b | 4564 | mpz_clear(left_val); |
4565 | return false; | |
e440a328 | 4566 | } |
0c77715b | 4567 | |
4568 | *cmp = mpz_cmp(left_val, right_val); | |
4569 | ||
4570 | mpz_clear(left_val); | |
4571 | mpz_clear(right_val); | |
4572 | ||
4573 | return true; | |
4574 | } | |
4575 | ||
4576 | // Compare floating point constants. | |
4577 | ||
4578 | bool | |
4579 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4580 | const Numeric_constant* right_nc, | |
4581 | int* cmp) | |
4582 | { | |
4583 | mpfr_t left_val; | |
4584 | if (!left_nc->to_float(&left_val)) | |
4585 | return false; | |
4586 | mpfr_t right_val; | |
4587 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4588 | { |
0c77715b | 4589 | mpfr_clear(left_val); |
4590 | return false; | |
4591 | } | |
4592 | ||
4593 | // We already coerced both operands to the same type. If that type | |
4594 | // is not an abstract type, we need to round the values accordingly. | |
4595 | Type* type = left_nc->type(); | |
4596 | if (!type->is_abstract() && type->float_type() != NULL) | |
4597 | { | |
4598 | int bits = type->float_type()->bits(); | |
4599 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4600 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4601 | } |
0c77715b | 4602 | |
4603 | *cmp = mpfr_cmp(left_val, right_val); | |
4604 | ||
4605 | mpfr_clear(left_val); | |
4606 | mpfr_clear(right_val); | |
4607 | ||
4608 | return true; | |
e440a328 | 4609 | } |
4610 | ||
0c77715b | 4611 | // Compare complex constants. Complex numbers may only be compared |
4612 | // for equality. | |
e440a328 | 4613 | |
4614 | bool | |
0c77715b | 4615 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4616 | const Numeric_constant* right_nc, | |
4617 | int* cmp) | |
e440a328 | 4618 | { |
fcbea5e4 | 4619 | mpc_t left_val; |
4620 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4621 | return false; |
fcbea5e4 | 4622 | mpc_t right_val; |
4623 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4624 | { |
fcbea5e4 | 4625 | mpc_clear(left_val); |
0c77715b | 4626 | return false; |
e440a328 | 4627 | } |
0c77715b | 4628 | |
4629 | // We already coerced both operands to the same type. If that type | |
4630 | // is not an abstract type, we need to round the values accordingly. | |
4631 | Type* type = left_nc->type(); | |
4632 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4633 | { |
0c77715b | 4634 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4635 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4636 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4637 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4638 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4639 | } |
0c77715b | 4640 | |
fcbea5e4 | 4641 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4642 | |
fcbea5e4 | 4643 | mpc_clear(left_val); |
4644 | mpc_clear(right_val); | |
0c77715b | 4645 | |
4646 | return true; | |
e440a328 | 4647 | } |
4648 | ||
0c77715b | 4649 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4650 | // true if this could be done, false if not. Issue errors at LOCATION | |
4651 | // as appropriate. | |
e440a328 | 4652 | |
4653 | bool | |
0c77715b | 4654 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4655 | Numeric_constant* right_nc, | |
4656 | Location location, Numeric_constant* nc) | |
e440a328 | 4657 | { |
e440a328 | 4658 | switch (op) |
4659 | { | |
4660 | case OPERATOR_OROR: | |
4661 | case OPERATOR_ANDAND: | |
4662 | case OPERATOR_EQEQ: | |
4663 | case OPERATOR_NOTEQ: | |
4664 | case OPERATOR_LT: | |
4665 | case OPERATOR_LE: | |
4666 | case OPERATOR_GT: | |
4667 | case OPERATOR_GE: | |
9767e2d3 | 4668 | // These return boolean values, not numeric. |
4669 | return false; | |
0c77715b | 4670 | default: |
4671 | break; | |
4672 | } | |
4673 | ||
4674 | Type* left_type = left_nc->type(); | |
4675 | Type* right_type = right_nc->type(); | |
4676 | ||
4677 | Type* type; | |
4678 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4679 | return false; | |
4680 | ||
4681 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4682 | ||
4683 | // When combining an untyped operand with a typed operand, we are | |
4684 | // effectively coercing the untyped operand to the other operand's | |
4685 | // type, so make sure that is valid. | |
4686 | if (!left_nc->set_type(type, true, location)) | |
4687 | return false; | |
4688 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4689 | return false; | |
85334a21 | 4690 | if (is_shift |
4691 | && ((left_type->integer_type() == NULL | |
4692 | && !left_type->is_abstract()) | |
4693 | || (right_type->integer_type() == NULL | |
4694 | && !right_type->is_abstract()))) | |
4695 | return false; | |
0c77715b | 4696 | |
4697 | bool r; | |
4698 | if (type->complex_type() != NULL) | |
4699 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4700 | else if (type->float_type() != NULL) | |
4701 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4702 | else | |
4703 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4704 | ||
4705 | if (r) | |
4706 | r = nc->set_type(type, true, location); | |
4707 | ||
4708 | return r; | |
4709 | } | |
4710 | ||
4711 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4712 | // integer operations. Return true if this could be done, false if | |
4713 | // not. | |
4714 | ||
4715 | bool | |
4716 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4717 | const Numeric_constant* right_nc, | |
4718 | Location location, Numeric_constant* nc) | |
4719 | { | |
4720 | mpz_t left_val; | |
4721 | if (!left_nc->to_int(&left_val)) | |
4722 | return false; | |
4723 | mpz_t right_val; | |
4724 | if (!right_nc->to_int(&right_val)) | |
4725 | { | |
4726 | mpz_clear(left_val); | |
e440a328 | 4727 | return false; |
0c77715b | 4728 | } |
4729 | ||
4730 | mpz_t val; | |
4731 | mpz_init(val); | |
4732 | ||
4733 | switch (op) | |
4734 | { | |
e440a328 | 4735 | case OPERATOR_PLUS: |
4736 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4737 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4738 | { | |
631d5788 | 4739 | go_error_at(location, "constant addition overflow"); |
71a45216 | 4740 | nc->set_invalid(); |
2c809f8f | 4741 | mpz_set_ui(val, 1); |
4742 | } | |
e440a328 | 4743 | break; |
4744 | case OPERATOR_MINUS: | |
4745 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4746 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4747 | { | |
631d5788 | 4748 | go_error_at(location, "constant subtraction overflow"); |
71a45216 | 4749 | nc->set_invalid(); |
2c809f8f | 4750 | mpz_set_ui(val, 1); |
4751 | } | |
e440a328 | 4752 | break; |
4753 | case OPERATOR_OR: | |
4754 | mpz_ior(val, left_val, right_val); | |
4755 | break; | |
4756 | case OPERATOR_XOR: | |
4757 | mpz_xor(val, left_val, right_val); | |
4758 | break; | |
4759 | case OPERATOR_MULT: | |
4760 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4761 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4762 | { | |
631d5788 | 4763 | go_error_at(location, "constant multiplication overflow"); |
71a45216 | 4764 | nc->set_invalid(); |
2c809f8f | 4765 | mpz_set_ui(val, 1); |
4766 | } | |
e440a328 | 4767 | break; |
4768 | case OPERATOR_DIV: | |
4769 | if (mpz_sgn(right_val) != 0) | |
4770 | mpz_tdiv_q(val, left_val, right_val); | |
4771 | else | |
4772 | { | |
631d5788 | 4773 | go_error_at(location, "division by zero"); |
71a45216 | 4774 | nc->set_invalid(); |
e440a328 | 4775 | mpz_set_ui(val, 0); |
e440a328 | 4776 | } |
4777 | break; | |
4778 | case OPERATOR_MOD: | |
4779 | if (mpz_sgn(right_val) != 0) | |
4780 | mpz_tdiv_r(val, left_val, right_val); | |
4781 | else | |
4782 | { | |
631d5788 | 4783 | go_error_at(location, "division by zero"); |
71a45216 | 4784 | nc->set_invalid(); |
e440a328 | 4785 | mpz_set_ui(val, 0); |
e440a328 | 4786 | } |
4787 | break; | |
4788 | case OPERATOR_LSHIFT: | |
4789 | { | |
4790 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4791 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4792 | mpz_mul_2exp(val, left_val, shift); | |
4793 | else | |
e440a328 | 4794 | { |
631d5788 | 4795 | go_error_at(location, "shift count overflow"); |
71a45216 | 4796 | nc->set_invalid(); |
2c809f8f | 4797 | mpz_set_ui(val, 1); |
e440a328 | 4798 | } |
e440a328 | 4799 | break; |
4800 | } | |
4801 | break; | |
4802 | case OPERATOR_RSHIFT: | |
4803 | { | |
4804 | unsigned long shift = mpz_get_ui(right_val); | |
4805 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4806 | { | |
631d5788 | 4807 | go_error_at(location, "shift count overflow"); |
71a45216 | 4808 | nc->set_invalid(); |
2c809f8f | 4809 | mpz_set_ui(val, 1); |
e440a328 | 4810 | } |
e440a328 | 4811 | else |
0c77715b | 4812 | { |
4813 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4814 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4815 | else | |
4816 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4817 | } | |
e440a328 | 4818 | break; |
4819 | } | |
4820 | break; | |
4821 | case OPERATOR_AND: | |
4822 | mpz_and(val, left_val, right_val); | |
4823 | break; | |
4824 | case OPERATOR_BITCLEAR: | |
4825 | { | |
4826 | mpz_t tval; | |
4827 | mpz_init(tval); | |
4828 | mpz_com(tval, right_val); | |
4829 | mpz_and(val, left_val, tval); | |
4830 | mpz_clear(tval); | |
4831 | } | |
4832 | break; | |
4833 | default: | |
c3e6f413 | 4834 | go_unreachable(); |
e440a328 | 4835 | } |
4836 | ||
0c77715b | 4837 | mpz_clear(left_val); |
4838 | mpz_clear(right_val); | |
e440a328 | 4839 | |
0c77715b | 4840 | if (left_nc->is_rune() |
4841 | || (op != OPERATOR_LSHIFT | |
4842 | && op != OPERATOR_RSHIFT | |
4843 | && right_nc->is_rune())) | |
4844 | nc->set_rune(NULL, val); | |
4845 | else | |
4846 | nc->set_int(NULL, val); | |
4847 | ||
4848 | mpz_clear(val); | |
e440a328 | 4849 | |
4850 | return true; | |
4851 | } | |
4852 | ||
0c77715b | 4853 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4854 | // floating point operations. Return true if this could be done, | |
4855 | // false if not. | |
e440a328 | 4856 | |
4857 | bool | |
0c77715b | 4858 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4859 | const Numeric_constant* right_nc, | |
4860 | Location location, Numeric_constant* nc) | |
e440a328 | 4861 | { |
0c77715b | 4862 | mpfr_t left_val; |
4863 | if (!left_nc->to_float(&left_val)) | |
4864 | return false; | |
4865 | mpfr_t right_val; | |
4866 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4867 | { |
0c77715b | 4868 | mpfr_clear(left_val); |
e440a328 | 4869 | return false; |
0c77715b | 4870 | } |
4871 | ||
4872 | mpfr_t val; | |
4873 | mpfr_init(val); | |
4874 | ||
4875 | bool ret = true; | |
4876 | switch (op) | |
4877 | { | |
e440a328 | 4878 | case OPERATOR_PLUS: |
4879 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4880 | break; | |
4881 | case OPERATOR_MINUS: | |
4882 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4883 | break; | |
4884 | case OPERATOR_OR: | |
4885 | case OPERATOR_XOR: | |
4886 | case OPERATOR_AND: | |
4887 | case OPERATOR_BITCLEAR: | |
0c77715b | 4888 | case OPERATOR_MOD: |
4889 | case OPERATOR_LSHIFT: | |
4890 | case OPERATOR_RSHIFT: | |
4891 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4892 | ret = false; | |
4893 | break; | |
e440a328 | 4894 | case OPERATOR_MULT: |
4895 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4896 | break; | |
4897 | case OPERATOR_DIV: | |
0c77715b | 4898 | if (!mpfr_zero_p(right_val)) |
4899 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4900 | else | |
4901 | { | |
631d5788 | 4902 | go_error_at(location, "division by zero"); |
71a45216 | 4903 | nc->set_invalid(); |
0c77715b | 4904 | mpfr_set_ui(val, 0, GMP_RNDN); |
4905 | } | |
e440a328 | 4906 | break; |
e440a328 | 4907 | default: |
c3e6f413 | 4908 | go_unreachable(); |
e440a328 | 4909 | } |
4910 | ||
0c77715b | 4911 | mpfr_clear(left_val); |
4912 | mpfr_clear(right_val); | |
e440a328 | 4913 | |
0c77715b | 4914 | nc->set_float(NULL, val); |
4915 | mpfr_clear(val); | |
e440a328 | 4916 | |
0c77715b | 4917 | return ret; |
e440a328 | 4918 | } |
4919 | ||
0c77715b | 4920 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4921 | // complex operations. Return true if this could be done, false if | |
4922 | // not. | |
e440a328 | 4923 | |
4924 | bool | |
0c77715b | 4925 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4926 | const Numeric_constant* right_nc, | |
4927 | Location location, Numeric_constant* nc) | |
e440a328 | 4928 | { |
fcbea5e4 | 4929 | mpc_t left_val; |
4930 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4931 | return false; |
fcbea5e4 | 4932 | mpc_t right_val; |
4933 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4934 | { |
fcbea5e4 | 4935 | mpc_clear(left_val); |
e440a328 | 4936 | return false; |
0c77715b | 4937 | } |
4938 | ||
fcbea5e4 | 4939 | mpc_t val; |
4940 | mpc_init2(val, mpc_precision); | |
0c77715b | 4941 | |
4942 | bool ret = true; | |
4943 | switch (op) | |
4944 | { | |
e440a328 | 4945 | case OPERATOR_PLUS: |
fcbea5e4 | 4946 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 4947 | break; |
4948 | case OPERATOR_MINUS: | |
fcbea5e4 | 4949 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 4950 | break; |
4951 | case OPERATOR_OR: | |
4952 | case OPERATOR_XOR: | |
4953 | case OPERATOR_AND: | |
4954 | case OPERATOR_BITCLEAR: | |
0c77715b | 4955 | case OPERATOR_MOD: |
4956 | case OPERATOR_LSHIFT: | |
4957 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 4958 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 4959 | ret = false; |
4960 | break; | |
e440a328 | 4961 | case OPERATOR_MULT: |
fcbea5e4 | 4962 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 4963 | break; |
4964 | case OPERATOR_DIV: | |
fcbea5e4 | 4965 | if (mpc_cmp_si(right_val, 0) == 0) |
4966 | { | |
631d5788 | 4967 | go_error_at(location, "division by zero"); |
71a45216 | 4968 | nc->set_invalid(); |
fcbea5e4 | 4969 | mpc_set_ui(val, 0, MPC_RNDNN); |
4970 | break; | |
4971 | } | |
4972 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 4973 | break; |
e440a328 | 4974 | default: |
c3e6f413 | 4975 | go_unreachable(); |
e440a328 | 4976 | } |
4977 | ||
fcbea5e4 | 4978 | mpc_clear(left_val); |
4979 | mpc_clear(right_val); | |
e440a328 | 4980 | |
fcbea5e4 | 4981 | nc->set_complex(NULL, val); |
4982 | mpc_clear(val); | |
e440a328 | 4983 | |
0c77715b | 4984 | return ret; |
e440a328 | 4985 | } |
4986 | ||
4987 | // Lower a binary expression. We have to evaluate constant | |
4988 | // expressions now, in order to implement Go's unlimited precision | |
4989 | // constants. | |
4990 | ||
4991 | Expression* | |
e9d3367e | 4992 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
4993 | Statement_inserter* inserter, int) | |
e440a328 | 4994 | { |
b13c66cd | 4995 | Location location = this->location(); |
e440a328 | 4996 | Operator op = this->op_; |
4997 | Expression* left = this->left_; | |
4998 | Expression* right = this->right_; | |
4999 | ||
5000 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5001 | || op == OPERATOR_NOTEQ | |
5002 | || op == OPERATOR_LT | |
5003 | || op == OPERATOR_LE | |
5004 | || op == OPERATOR_GT | |
5005 | || op == OPERATOR_GE); | |
5006 | ||
0c77715b | 5007 | // Numeric constant expressions. |
e440a328 | 5008 | { |
0c77715b | 5009 | Numeric_constant left_nc; |
5010 | Numeric_constant right_nc; | |
5011 | if (left->numeric_constant_value(&left_nc) | |
5012 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5013 | { |
0c77715b | 5014 | if (is_comparison) |
e440a328 | 5015 | { |
0c77715b | 5016 | bool result; |
5017 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5018 | &right_nc, location, | |
5019 | &result)) | |
5020 | return this; | |
e90c9dfc | 5021 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5022 | Expression::make_boolean(result, |
5023 | location), | |
5024 | location); | |
e440a328 | 5025 | } |
5026 | else | |
5027 | { | |
0c77715b | 5028 | Numeric_constant nc; |
5029 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5030 | location, &nc)) | |
71a45216 | 5031 | return this; |
0c77715b | 5032 | return nc.expression(location); |
e440a328 | 5033 | } |
5034 | } | |
e440a328 | 5035 | } |
5036 | ||
5037 | // String constant expressions. | |
315fa98d | 5038 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5039 | { |
5040 | std::string left_string; | |
5041 | std::string right_string; | |
5042 | if (left->string_constant_value(&left_string) | |
5043 | && right->string_constant_value(&right_string)) | |
315fa98d | 5044 | { |
5045 | if (op == OPERATOR_PLUS) | |
5046 | return Expression::make_string(left_string + right_string, | |
5047 | location); | |
5048 | else if (is_comparison) | |
5049 | { | |
5050 | int cmp = left_string.compare(right_string); | |
0c77715b | 5051 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5052 | return Expression::make_boolean(r, location); |
b40dc774 | 5053 | } |
5054 | } | |
b40dc774 | 5055 | } |
5056 | ||
ceeb12d7 | 5057 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5058 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5059 | { | |
b79832ca | 5060 | if (left->type()->struct_type() != NULL |
5061 | && right->type()->struct_type() != NULL) | |
e9d3367e | 5062 | return this->lower_struct_comparison(gogo, inserter); |
5063 | else if (left->type()->array_type() != NULL | |
b79832ca | 5064 | && !left->type()->is_slice_type() |
5065 | && right->type()->array_type() != NULL | |
5066 | && !right->type()->is_slice_type()) | |
e9d3367e | 5067 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 5068 | else if ((left->type()->interface_type() != NULL |
5069 | && right->type()->interface_type() == NULL) | |
5070 | || (left->type()->interface_type() == NULL | |
5071 | && right->type()->interface_type() != NULL)) | |
5072 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5073 | } |
5074 | ||
736a16ba | 5075 | // Lower string concatenation to String_concat_expression, so that |
5076 | // we can group sequences of string additions. | |
5077 | if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) | |
5078 | { | |
5079 | Expression_list* exprs; | |
5080 | String_concat_expression* left_sce = | |
5081 | this->left_->string_concat_expression(); | |
5082 | if (left_sce != NULL) | |
5083 | exprs = left_sce->exprs(); | |
5084 | else | |
5085 | { | |
5086 | exprs = new Expression_list(); | |
5087 | exprs->push_back(this->left_); | |
5088 | } | |
5089 | ||
5090 | String_concat_expression* right_sce = | |
5091 | this->right_->string_concat_expression(); | |
5092 | if (right_sce != NULL) | |
5093 | exprs->append(right_sce->exprs()); | |
5094 | else | |
5095 | exprs->push_back(this->right_); | |
5096 | ||
5097 | return Expression::make_string_concat(exprs); | |
5098 | } | |
5099 | ||
e440a328 | 5100 | return this; |
5101 | } | |
5102 | ||
e9d3367e | 5103 | // Lower a struct comparison. |
5104 | ||
5105 | Expression* | |
5106 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5107 | Statement_inserter* inserter) | |
5108 | { | |
5109 | Struct_type* st = this->left_->type()->struct_type(); | |
5110 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5111 | if (st2 == NULL) | |
5112 | return this; | |
5113 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5114 | return this; | |
5115 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5116 | this->right_->type(), NULL)) | |
5117 | return this; | |
5118 | ||
5119 | // See if we can compare using memcmp. As a heuristic, we use | |
5120 | // memcmp rather than field references and comparisons if there are | |
5121 | // more than two fields. | |
113ef6a5 | 5122 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5123 | return this->lower_compare_to_memcmp(gogo, inserter); |
5124 | ||
5125 | Location loc = this->location(); | |
5126 | ||
5127 | Expression* left = this->left_; | |
5128 | Temporary_statement* left_temp = NULL; | |
5129 | if (left->var_expression() == NULL | |
5130 | && left->temporary_reference_expression() == NULL) | |
5131 | { | |
5132 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5133 | inserter->insert(left_temp); | |
5134 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5135 | } | |
5136 | ||
5137 | Expression* right = this->right_; | |
5138 | Temporary_statement* right_temp = NULL; | |
5139 | if (right->var_expression() == NULL | |
5140 | && right->temporary_reference_expression() == NULL) | |
5141 | { | |
5142 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5143 | inserter->insert(right_temp); | |
5144 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5145 | } | |
5146 | ||
5147 | Expression* ret = Expression::make_boolean(true, loc); | |
5148 | const Struct_field_list* fields = st->fields(); | |
5149 | unsigned int field_index = 0; | |
5150 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5151 | pf != fields->end(); | |
5152 | ++pf, ++field_index) | |
5153 | { | |
f5165c05 | 5154 | if (Gogo::is_sink_name(pf->field_name())) |
5155 | continue; | |
5156 | ||
e9d3367e | 5157 | if (field_index > 0) |
5158 | { | |
5159 | if (left_temp == NULL) | |
5160 | left = left->copy(); | |
5161 | else | |
5162 | left = Expression::make_temporary_reference(left_temp, loc); | |
5163 | if (right_temp == NULL) | |
5164 | right = right->copy(); | |
5165 | else | |
5166 | right = Expression::make_temporary_reference(right_temp, loc); | |
5167 | } | |
5168 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5169 | loc); | |
5170 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5171 | loc); | |
5172 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5173 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5174 | } | |
5175 | ||
5176 | if (this->op_ == OPERATOR_NOTEQ) | |
5177 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5178 | ||
5179 | return ret; | |
5180 | } | |
5181 | ||
5182 | // Lower an array comparison. | |
5183 | ||
5184 | Expression* | |
5185 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5186 | Statement_inserter* inserter) | |
5187 | { | |
5188 | Array_type* at = this->left_->type()->array_type(); | |
5189 | Array_type* at2 = this->right_->type()->array_type(); | |
5190 | if (at2 == NULL) | |
5191 | return this; | |
5192 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5193 | return this; | |
5194 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5195 | this->right_->type(), NULL)) | |
5196 | return this; | |
5197 | ||
5198 | // Call memcmp directly if possible. This may let the middle-end | |
5199 | // optimize the call. | |
113ef6a5 | 5200 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5201 | return this->lower_compare_to_memcmp(gogo, inserter); |
5202 | ||
5203 | // Call the array comparison function. | |
5204 | Named_object* hash_fn; | |
5205 | Named_object* equal_fn; | |
5206 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5207 | &hash_fn, &equal_fn); | |
5208 | ||
5209 | Location loc = this->location(); | |
5210 | ||
5211 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5212 | ||
5213 | Expression_list* args = new Expression_list(); | |
5214 | args->push_back(this->operand_address(inserter, this->left_)); | |
5215 | args->push_back(this->operand_address(inserter, this->right_)); | |
5216 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5217 | ||
5218 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5219 | ||
5220 | if (this->op_ == OPERATOR_NOTEQ) | |
5221 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5222 | ||
5223 | return ret; | |
5224 | } | |
5225 | ||
ceeb12d7 | 5226 | // Lower an interface to value comparison. |
5227 | ||
5228 | Expression* | |
5229 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5230 | Statement_inserter* inserter) | |
5231 | { | |
5232 | Type* left_type = this->left_->type(); | |
5233 | Type* right_type = this->right_->type(); | |
5234 | Interface_type* ift; | |
5235 | if (left_type->interface_type() != NULL) | |
5236 | { | |
5237 | ift = left_type->interface_type(); | |
5238 | if (!ift->implements_interface(right_type, NULL)) | |
5239 | return this; | |
5240 | } | |
5241 | else | |
5242 | { | |
5243 | ift = right_type->interface_type(); | |
5244 | if (!ift->implements_interface(left_type, NULL)) | |
5245 | return this; | |
5246 | } | |
5247 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5248 | return this; | |
5249 | ||
5250 | Location loc = this->location(); | |
5251 | ||
5252 | if (left_type->interface_type() == NULL | |
5253 | && left_type->points_to() == NULL | |
5254 | && !this->left_->is_addressable()) | |
5255 | { | |
5256 | Temporary_statement* temp = | |
5257 | Statement::make_temporary(left_type, NULL, loc); | |
5258 | inserter->insert(temp); | |
5259 | this->left_ = | |
5260 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5261 | } | |
5262 | ||
5263 | if (right_type->interface_type() == NULL | |
5264 | && right_type->points_to() == NULL | |
5265 | && !this->right_->is_addressable()) | |
5266 | { | |
5267 | Temporary_statement* temp = | |
5268 | Statement::make_temporary(right_type, NULL, loc); | |
5269 | inserter->insert(temp); | |
5270 | this->right_ = | |
5271 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5272 | } | |
5273 | ||
5274 | return this; | |
5275 | } | |
5276 | ||
e9d3367e | 5277 | // Lower a struct or array comparison to a call to memcmp. |
5278 | ||
5279 | Expression* | |
5280 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5281 | { | |
5282 | Location loc = this->location(); | |
5283 | ||
5284 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5285 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5286 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5287 | TYPE_INFO_SIZE); | |
5288 | ||
5289 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5290 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5291 | return Expression::make_binary(this->op_, call, zero, loc); |
5292 | } | |
5293 | ||
a32698ee | 5294 | Expression* |
5c3f3470 | 5295 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5296 | Statement_inserter* inserter) |
5297 | { | |
5298 | Location loc = this->location(); | |
5bf8be8b | 5299 | if (this->left_->type()->is_error_type() |
5300 | || this->right_->type()->is_error_type() | |
5301 | || this->left_->is_error_expression() | |
5302 | || this->right_->is_error_expression()) | |
5303 | { | |
5304 | go_assert(saw_errors()); | |
5305 | return Expression::make_error(loc); | |
5306 | } | |
5307 | ||
a32698ee | 5308 | Temporary_statement* temp; |
a32698ee | 5309 | |
5310 | Type* left_type = this->left_->type(); | |
5311 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5312 | || this->op_ == OPERATOR_RSHIFT); | |
5313 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5314 | left_type->integer_type() != NULL) | |
5315 | || this->op_ == OPERATOR_MOD); | |
5316 | ||
a32698ee | 5317 | if (is_shift_op |
5c3f3470 | 5318 | || (is_idiv_op |
5319 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5320 | { |
545ab43b | 5321 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5322 | { |
5323 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5324 | inserter->insert(temp); | |
5325 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5326 | } | |
545ab43b | 5327 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5328 | { |
5329 | temp = | |
5330 | Statement::make_temporary(NULL, this->right_, loc); | |
5331 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5332 | inserter->insert(temp); | |
5333 | } | |
5334 | } | |
5335 | return this; | |
5336 | } | |
5337 | ||
5338 | ||
e9d3367e | 5339 | // Return the address of EXPR, cast to unsafe.Pointer. |
5340 | ||
5341 | Expression* | |
5342 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5343 | Expression* expr) | |
5344 | { | |
5345 | Location loc = this->location(); | |
5346 | ||
5347 | if (!expr->is_addressable()) | |
5348 | { | |
5349 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5350 | loc); | |
5351 | inserter->insert(temp); | |
5352 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5353 | } | |
5354 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5355 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5356 | Type* void_type = Type::make_void_type(); | |
5357 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5358 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5359 | } | |
5360 | ||
0c77715b | 5361 | // Return the numeric constant value, if it has one. |
e440a328 | 5362 | |
5363 | bool | |
0c77715b | 5364 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5365 | { |
0c77715b | 5366 | Numeric_constant left_nc; |
5367 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5368 | return false; | |
5369 | Numeric_constant right_nc; | |
5370 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5371 | return false; | |
9767e2d3 | 5372 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5373 | this->location(), nc); |
e440a328 | 5374 | } |
5375 | ||
5376 | // Note that the value is being discarded. | |
5377 | ||
4f2138d7 | 5378 | bool |
e440a328 | 5379 | Binary_expression::do_discarding_value() |
5380 | { | |
5381 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5382 | return this->right_->discarding_value(); |
e440a328 | 5383 | else |
4f2138d7 | 5384 | { |
5385 | this->unused_value_error(); | |
5386 | return false; | |
5387 | } | |
e440a328 | 5388 | } |
5389 | ||
5390 | // Get type. | |
5391 | ||
5392 | Type* | |
5393 | Binary_expression::do_type() | |
5394 | { | |
5f5fea79 | 5395 | if (this->classification() == EXPRESSION_ERROR) |
5396 | return Type::make_error_type(); | |
5397 | ||
e440a328 | 5398 | switch (this->op_) |
5399 | { | |
e440a328 | 5400 | case OPERATOR_EQEQ: |
5401 | case OPERATOR_NOTEQ: | |
5402 | case OPERATOR_LT: | |
5403 | case OPERATOR_LE: | |
5404 | case OPERATOR_GT: | |
5405 | case OPERATOR_GE: | |
e90c9dfc | 5406 | if (this->type_ == NULL) |
5407 | this->type_ = Type::make_boolean_type(); | |
5408 | return this->type_; | |
e440a328 | 5409 | |
5410 | case OPERATOR_PLUS: | |
5411 | case OPERATOR_MINUS: | |
5412 | case OPERATOR_OR: | |
5413 | case OPERATOR_XOR: | |
5414 | case OPERATOR_MULT: | |
5415 | case OPERATOR_DIV: | |
5416 | case OPERATOR_MOD: | |
5417 | case OPERATOR_AND: | |
5418 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5419 | case OPERATOR_OROR: |
5420 | case OPERATOR_ANDAND: | |
e440a328 | 5421 | { |
0c77715b | 5422 | Type* type; |
5423 | if (!Binary_expression::operation_type(this->op_, | |
5424 | this->left_->type(), | |
5425 | this->right_->type(), | |
5426 | &type)) | |
5427 | return Type::make_error_type(); | |
5428 | return type; | |
e440a328 | 5429 | } |
5430 | ||
5431 | case OPERATOR_LSHIFT: | |
5432 | case OPERATOR_RSHIFT: | |
5433 | return this->left_->type(); | |
5434 | ||
5435 | default: | |
c3e6f413 | 5436 | go_unreachable(); |
e440a328 | 5437 | } |
5438 | } | |
5439 | ||
5440 | // Set type for a binary expression. | |
5441 | ||
5442 | void | |
5443 | Binary_expression::do_determine_type(const Type_context* context) | |
5444 | { | |
5445 | Type* tleft = this->left_->type(); | |
5446 | Type* tright = this->right_->type(); | |
5447 | ||
5448 | // Both sides should have the same type, except for the shift | |
5449 | // operations. For a comparison, we should ignore the incoming | |
5450 | // type. | |
5451 | ||
5452 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5453 | || this->op_ == OPERATOR_RSHIFT); | |
5454 | ||
5455 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5456 | || this->op_ == OPERATOR_NOTEQ | |
5457 | || this->op_ == OPERATOR_LT | |
5458 | || this->op_ == OPERATOR_LE | |
5459 | || this->op_ == OPERATOR_GT | |
5460 | || this->op_ == OPERATOR_GE); | |
5461 | ||
c999c2a7 | 5462 | // For constant expressions, the context of the result is not useful in |
5463 | // determining the types of the operands. It is only legal to use abstract | |
5464 | // boolean, numeric, and string constants as operands where it is legal to | |
5465 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5466 | // Any issues with the operation will be resolved in the check_types pass. | |
5467 | bool is_constant_expr = (this->left_->is_constant() | |
5468 | && this->right_->is_constant()); | |
5469 | ||
e440a328 | 5470 | Type_context subcontext(*context); |
5471 | ||
5472 | if (is_comparison) | |
5473 | { | |
5474 | // In a comparison, the context does not determine the types of | |
5475 | // the operands. | |
5476 | subcontext.type = NULL; | |
5477 | } | |
5478 | ||
5479 | // Set the context for the left hand operand. | |
5480 | if (is_shift_op) | |
5481 | { | |
b40dc774 | 5482 | // The right hand operand of a shift plays no role in |
5483 | // determining the type of the left hand operand. | |
e440a328 | 5484 | } |
5485 | else if (!tleft->is_abstract()) | |
5486 | subcontext.type = tleft; | |
5487 | else if (!tright->is_abstract()) | |
5488 | subcontext.type = tright; | |
5489 | else if (subcontext.type == NULL) | |
5490 | { | |
5491 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5492 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5493 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5494 | { | |
5495 | // Both sides have an abstract integer, abstract float, or | |
5496 | // abstract complex type. Just let CONTEXT determine | |
5497 | // whether they may remain abstract or not. | |
5498 | } | |
5499 | else if (tleft->complex_type() != NULL) | |
5500 | subcontext.type = tleft; | |
5501 | else if (tright->complex_type() != NULL) | |
5502 | subcontext.type = tright; | |
5503 | else if (tleft->float_type() != NULL) | |
5504 | subcontext.type = tleft; | |
5505 | else if (tright->float_type() != NULL) | |
5506 | subcontext.type = tright; | |
5507 | else | |
5508 | subcontext.type = tleft; | |
f58a23ae | 5509 | |
5510 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5511 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5512 | } |
5513 | ||
c999c2a7 | 5514 | if (!is_constant_expr) |
5515 | this->left_->determine_type(&subcontext); | |
e440a328 | 5516 | |
e440a328 | 5517 | if (is_shift_op) |
5518 | { | |
b40dc774 | 5519 | // We may have inherited an unusable type for the shift operand. |
5520 | // Give a useful error if that happened. | |
5521 | if (tleft->is_abstract() | |
5522 | && subcontext.type != NULL | |
8ab6effb | 5523 | && !subcontext.may_be_abstract |
f6bc81e6 | 5524 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5525 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5526 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5527 | "for left operand of shift")); |
b40dc774 | 5528 | |
5529 | // The context for the right hand operand is the same as for the | |
5530 | // left hand operand, except for a shift operator. | |
e440a328 | 5531 | subcontext.type = Type::lookup_integer_type("uint"); |
5532 | subcontext.may_be_abstract = false; | |
5533 | } | |
5534 | ||
c999c2a7 | 5535 | if (!is_constant_expr) |
5536 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5537 | |
5538 | if (is_comparison) | |
5539 | { | |
5540 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5541 | ; | |
5542 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5543 | this->type_ = context->type; | |
5544 | else if (!context->may_be_abstract) | |
5545 | this->type_ = Type::lookup_bool_type(); | |
5546 | } | |
e440a328 | 5547 | } |
5548 | ||
5549 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5550 | // OTYPE is the type of the other operand. Return whether the |
5551 | // operation is OK. This should not be used for shift. | |
e440a328 | 5552 | |
5553 | bool | |
be8b5eee | 5554 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5555 | Location location) |
e440a328 | 5556 | { |
5557 | switch (op) | |
5558 | { | |
5559 | case OPERATOR_OROR: | |
5560 | case OPERATOR_ANDAND: | |
c999c2a7 | 5561 | if (!type->is_boolean_type() |
5562 | || !otype->is_boolean_type()) | |
e440a328 | 5563 | { |
631d5788 | 5564 | go_error_at(location, "expected boolean type"); |
e440a328 | 5565 | return false; |
5566 | } | |
5567 | break; | |
5568 | ||
5569 | case OPERATOR_EQEQ: | |
5570 | case OPERATOR_NOTEQ: | |
e9d3367e | 5571 | { |
5572 | std::string reason; | |
5573 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5574 | { | |
631d5788 | 5575 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5576 | return false; |
5577 | } | |
5578 | } | |
e440a328 | 5579 | break; |
5580 | ||
5581 | case OPERATOR_LT: | |
5582 | case OPERATOR_LE: | |
5583 | case OPERATOR_GT: | |
5584 | case OPERATOR_GE: | |
e9d3367e | 5585 | { |
5586 | std::string reason; | |
5587 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5588 | { | |
631d5788 | 5589 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5590 | return false; |
5591 | } | |
5592 | } | |
e440a328 | 5593 | break; |
5594 | ||
5595 | case OPERATOR_PLUS: | |
5596 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5597 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5598 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5599 | { |
631d5788 | 5600 | go_error_at(location, |
e440a328 | 5601 | "expected integer, floating, complex, or string type"); |
5602 | return false; | |
5603 | } | |
5604 | break; | |
5605 | ||
5606 | case OPERATOR_MINUS: | |
5607 | case OPERATOR_MINUSEQ: | |
5608 | case OPERATOR_MULT: | |
5609 | case OPERATOR_MULTEQ: | |
5610 | case OPERATOR_DIV: | |
5611 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5612 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5613 | { |
631d5788 | 5614 | go_error_at(location, "expected integer, floating, or complex type"); |
e440a328 | 5615 | return false; |
5616 | } | |
5617 | break; | |
5618 | ||
5619 | case OPERATOR_MOD: | |
5620 | case OPERATOR_MODEQ: | |
5621 | case OPERATOR_OR: | |
5622 | case OPERATOR_OREQ: | |
5623 | case OPERATOR_AND: | |
5624 | case OPERATOR_ANDEQ: | |
5625 | case OPERATOR_XOR: | |
5626 | case OPERATOR_XOREQ: | |
5627 | case OPERATOR_BITCLEAR: | |
5628 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5629 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5630 | { |
631d5788 | 5631 | go_error_at(location, "expected integer type"); |
e440a328 | 5632 | return false; |
5633 | } | |
5634 | break; | |
5635 | ||
5636 | default: | |
c3e6f413 | 5637 | go_unreachable(); |
e440a328 | 5638 | } |
5639 | ||
5640 | return true; | |
5641 | } | |
5642 | ||
5643 | // Check types. | |
5644 | ||
5645 | void | |
5646 | Binary_expression::do_check_types(Gogo*) | |
5647 | { | |
5f5fea79 | 5648 | if (this->classification() == EXPRESSION_ERROR) |
5649 | return; | |
5650 | ||
e440a328 | 5651 | Type* left_type = this->left_->type(); |
5652 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5653 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5654 | { |
5655 | this->set_is_error(); | |
5656 | return; | |
5657 | } | |
e440a328 | 5658 | |
5659 | if (this->op_ == OPERATOR_EQEQ | |
5660 | || this->op_ == OPERATOR_NOTEQ | |
5661 | || this->op_ == OPERATOR_LT | |
5662 | || this->op_ == OPERATOR_LE | |
5663 | || this->op_ == OPERATOR_GT | |
5664 | || this->op_ == OPERATOR_GE) | |
5665 | { | |
907c5ecd | 5666 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5667 | { | |
5668 | this->report_error(_("invalid comparison of nil with nil")); | |
5669 | return; | |
5670 | } | |
e440a328 | 5671 | if (!Type::are_assignable(left_type, right_type, NULL) |
5672 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5673 | { | |
5674 | this->report_error(_("incompatible types in binary expression")); | |
5675 | return; | |
5676 | } | |
5677 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5678 | right_type, |
e440a328 | 5679 | this->location()) |
5680 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5681 | left_type, |
e440a328 | 5682 | this->location())) |
5683 | { | |
5684 | this->set_is_error(); | |
5685 | return; | |
5686 | } | |
5687 | } | |
5688 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5689 | { | |
5690 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5691 | { | |
5692 | this->report_error(_("incompatible types in binary expression")); | |
5693 | return; | |
5694 | } | |
5695 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5696 | right_type, |
e440a328 | 5697 | this->location())) |
5698 | { | |
5699 | this->set_is_error(); | |
5700 | return; | |
5701 | } | |
5c65b19d | 5702 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5703 | { | |
5704 | // Division by a zero integer constant is an error. | |
5705 | Numeric_constant rconst; | |
5706 | unsigned long rval; | |
5707 | if (left_type->integer_type() != NULL | |
5708 | && this->right_->numeric_constant_value(&rconst) | |
5709 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5710 | && rval == 0) | |
5711 | { | |
5712 | this->report_error(_("integer division by zero")); | |
5713 | return; | |
5714 | } | |
5715 | } | |
e440a328 | 5716 | } |
5717 | else | |
5718 | { | |
5719 | if (left_type->integer_type() == NULL) | |
5720 | this->report_error(_("shift of non-integer operand")); | |
5721 | ||
6b5e0fac | 5722 | if (right_type->is_string_type()) |
5723 | this->report_error(_("shift count not unsigned integer")); | |
5724 | else if (!right_type->is_abstract() | |
e440a328 | 5725 | && (right_type->integer_type() == NULL |
5726 | || !right_type->integer_type()->is_unsigned())) | |
5727 | this->report_error(_("shift count not unsigned integer")); | |
5728 | else | |
5729 | { | |
0c77715b | 5730 | Numeric_constant nc; |
5731 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5732 | { |
0c77715b | 5733 | mpz_t val; |
5734 | if (!nc.to_int(&val)) | |
5735 | this->report_error(_("shift count not unsigned integer")); | |
5736 | else | |
a4eba91b | 5737 | { |
0c77715b | 5738 | if (mpz_sgn(val) < 0) |
5739 | { | |
5740 | this->report_error(_("negative shift count")); | |
0c77715b | 5741 | Location rloc = this->right_->location(); |
e67508fa | 5742 | this->right_ = Expression::make_integer_ul(0, right_type, |
5743 | rloc); | |
0c77715b | 5744 | } |
5745 | mpz_clear(val); | |
a4eba91b | 5746 | } |
e440a328 | 5747 | } |
e440a328 | 5748 | } |
5749 | } | |
5750 | } | |
5751 | ||
ea664253 | 5752 | // Get the backend representation for a binary expression. |
e440a328 | 5753 | |
ea664253 | 5754 | Bexpression* |
5755 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 5756 | { |
1b1f2abf | 5757 | Gogo* gogo = context->gogo(); |
a32698ee | 5758 | Location loc = this->location(); |
5759 | Type* left_type = this->left_->type(); | |
5760 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5761 | |
e440a328 | 5762 | bool use_left_type = true; |
5763 | bool is_shift_op = false; | |
29a2d1d8 | 5764 | bool is_idiv_op = false; |
e440a328 | 5765 | switch (this->op_) |
5766 | { | |
5767 | case OPERATOR_EQEQ: | |
5768 | case OPERATOR_NOTEQ: | |
5769 | case OPERATOR_LT: | |
5770 | case OPERATOR_LE: | |
5771 | case OPERATOR_GT: | |
5772 | case OPERATOR_GE: | |
ea664253 | 5773 | return Expression::comparison(context, this->type_, this->op_, |
5774 | this->left_, this->right_, loc); | |
e440a328 | 5775 | |
5776 | case OPERATOR_OROR: | |
e440a328 | 5777 | case OPERATOR_ANDAND: |
e440a328 | 5778 | use_left_type = false; |
5779 | break; | |
5780 | case OPERATOR_PLUS: | |
e440a328 | 5781 | case OPERATOR_MINUS: |
e440a328 | 5782 | case OPERATOR_OR: |
e440a328 | 5783 | case OPERATOR_XOR: |
e440a328 | 5784 | case OPERATOR_MULT: |
e440a328 | 5785 | break; |
5786 | case OPERATOR_DIV: | |
a32698ee | 5787 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
5788 | break; | |
729f8831 | 5789 | // Fall through. |
e440a328 | 5790 | case OPERATOR_MOD: |
29a2d1d8 | 5791 | is_idiv_op = true; |
e440a328 | 5792 | break; |
5793 | case OPERATOR_LSHIFT: | |
e440a328 | 5794 | case OPERATOR_RSHIFT: |
e440a328 | 5795 | is_shift_op = true; |
5796 | break; | |
e440a328 | 5797 | case OPERATOR_BITCLEAR: |
a32698ee | 5798 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
5799 | case OPERATOR_AND: | |
e440a328 | 5800 | break; |
5801 | default: | |
c3e6f413 | 5802 | go_unreachable(); |
e440a328 | 5803 | } |
5804 | ||
736a16ba | 5805 | // The only binary operation for string is +, and that should have |
5806 | // been converted to a String_concat_expression in do_lower. | |
5807 | go_assert(!left_type->is_string_type()); | |
a32698ee | 5808 | |
5809 | // For complex division Go might want slightly different results than the | |
5810 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 5811 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
5812 | { | |
a32698ee | 5813 | Runtime::Function complex_code; |
1850e20c | 5814 | switch (this->left_->type()->complex_type()->bits()) |
5815 | { | |
5816 | case 64: | |
a32698ee | 5817 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 5818 | break; |
5819 | case 128: | |
a32698ee | 5820 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 5821 | break; |
5822 | default: | |
5823 | go_unreachable(); | |
5824 | } | |
a32698ee | 5825 | Expression* complex_div = |
5826 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 5827 | return complex_div->get_backend(context); |
1850e20c | 5828 | } |
5829 | ||
ea664253 | 5830 | Bexpression* left = this->left_->get_backend(context); |
5831 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 5832 | |
a32698ee | 5833 | Type* type = use_left_type ? left_type : right_type; |
5834 | Btype* btype = type->get_backend(gogo); | |
5835 | ||
5836 | Bexpression* ret = | |
5837 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
5838 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 5839 | |
a32698ee | 5840 | // Initialize overflow constants. |
5841 | Bexpression* overflow; | |
5842 | mpz_t zero; | |
5843 | mpz_init_set_ui(zero, 0UL); | |
5844 | mpz_t one; | |
5845 | mpz_init_set_ui(one, 1UL); | |
5846 | mpz_t neg_one; | |
5847 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 5848 | |
a32698ee | 5849 | Btype* left_btype = left_type->get_backend(gogo); |
5850 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 5851 | |
5852 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 5853 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 5854 | if (is_shift_op) |
5855 | { | |
a32698ee | 5856 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 5857 | |
a32698ee | 5858 | mpz_t bitsval; |
5859 | int bits = left_type->integer_type()->bits(); | |
5860 | mpz_init_set_ui(bitsval, bits); | |
5861 | Bexpression* bits_expr = | |
5862 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
5863 | Bexpression* compare = | |
5864 | gogo->backend()->binary_expression(OPERATOR_LT, | |
5865 | right, bits_expr, loc); | |
e440a328 | 5866 | |
a32698ee | 5867 | Bexpression* zero_expr = |
5868 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
5869 | overflow = zero_expr; | |
e440a328 | 5870 | if (this->op_ == OPERATOR_RSHIFT |
a32698ee | 5871 | && !left_type->integer_type()->is_unsigned()) |
e440a328 | 5872 | { |
a32698ee | 5873 | Bexpression* neg_expr = |
5874 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
5875 | zero_expr, loc); | |
5876 | Bexpression* neg_one_expr = | |
5877 | gogo->backend()->integer_constant_expression(left_btype, neg_one); | |
5878 | overflow = gogo->backend()->conditional_expression(btype, neg_expr, | |
5879 | neg_one_expr, | |
5880 | zero_expr, loc); | |
29a2d1d8 | 5881 | } |
a32698ee | 5882 | ret = gogo->backend()->conditional_expression(btype, compare, ret, |
5883 | overflow, loc); | |
5884 | mpz_clear(bitsval); | |
29a2d1d8 | 5885 | } |
5886 | ||
5887 | // Add checks for division by zero and division overflow as needed. | |
5888 | if (is_idiv_op) | |
5889 | { | |
5c3f3470 | 5890 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 5891 | { |
5892 | // right == 0 | |
a32698ee | 5893 | Bexpression* zero_expr = |
5894 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
5895 | Bexpression* check = | |
5896 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5897 | right, zero_expr, loc); | |
29a2d1d8 | 5898 | |
a32698ee | 5899 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 5900 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 5901 | Bexpression* crash = gogo->runtime_error(errcode, |
5902 | loc)->get_backend(context); | |
29a2d1d8 | 5903 | |
5904 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
ea664253 | 5905 | ret = gogo->backend()->conditional_expression(btype, check, crash, |
5906 | ret, loc); | |
b13c66cd | 5907 | } |
5908 | ||
5c3f3470 | 5909 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 5910 | { |
5911 | // right == -1 | |
5912 | // FIXME: It would be nice to say that this test is expected | |
5913 | // to return false. | |
a32698ee | 5914 | |
5915 | Bexpression* neg_one_expr = | |
5916 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
5917 | Bexpression* check = | |
5918 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5919 | right, neg_one_expr, loc); | |
5920 | ||
5921 | Bexpression* zero_expr = | |
5922 | gogo->backend()->integer_constant_expression(btype, zero); | |
5923 | Bexpression* one_expr = | |
5924 | gogo->backend()->integer_constant_expression(btype, one); | |
5925 | ||
5926 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 5927 | { |
5928 | // An unsigned -1 is the largest possible number, so | |
5929 | // dividing is always 1 or 0. | |
a32698ee | 5930 | |
5931 | Bexpression* cmp = | |
5932 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5933 | left, right, loc); | |
29a2d1d8 | 5934 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 5935 | overflow = |
5936 | gogo->backend()->conditional_expression(btype, cmp, | |
5937 | one_expr, zero_expr, | |
5938 | loc); | |
29a2d1d8 | 5939 | else |
a32698ee | 5940 | overflow = |
5941 | gogo->backend()->conditional_expression(btype, cmp, | |
5942 | zero_expr, left, | |
5943 | loc); | |
29a2d1d8 | 5944 | } |
5945 | else | |
5946 | { | |
5947 | // Computing left / -1 is the same as computing - left, | |
5948 | // which does not overflow since Go sets -fwrapv. | |
5949 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 5950 | { |
5951 | Expression* negate_expr = | |
5952 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 5953 | overflow = negate_expr->get_backend(context); |
a32698ee | 5954 | } |
29a2d1d8 | 5955 | else |
a32698ee | 5956 | overflow = zero_expr; |
29a2d1d8 | 5957 | } |
a32698ee | 5958 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 5959 | |
5960 | // right == -1 ? - left : ret | |
a32698ee | 5961 | ret = gogo->backend()->conditional_expression(btype, check, overflow, |
5962 | ret, loc); | |
29a2d1d8 | 5963 | } |
e440a328 | 5964 | } |
5965 | ||
a32698ee | 5966 | mpz_clear(zero); |
5967 | mpz_clear(one); | |
5968 | mpz_clear(neg_one); | |
ea664253 | 5969 | return ret; |
e440a328 | 5970 | } |
5971 | ||
5972 | // Export a binary expression. | |
5973 | ||
5974 | void | |
5975 | Binary_expression::do_export(Export* exp) const | |
5976 | { | |
5977 | exp->write_c_string("("); | |
5978 | this->left_->export_expression(exp); | |
5979 | switch (this->op_) | |
5980 | { | |
5981 | case OPERATOR_OROR: | |
5982 | exp->write_c_string(" || "); | |
5983 | break; | |
5984 | case OPERATOR_ANDAND: | |
5985 | exp->write_c_string(" && "); | |
5986 | break; | |
5987 | case OPERATOR_EQEQ: | |
5988 | exp->write_c_string(" == "); | |
5989 | break; | |
5990 | case OPERATOR_NOTEQ: | |
5991 | exp->write_c_string(" != "); | |
5992 | break; | |
5993 | case OPERATOR_LT: | |
5994 | exp->write_c_string(" < "); | |
5995 | break; | |
5996 | case OPERATOR_LE: | |
5997 | exp->write_c_string(" <= "); | |
5998 | break; | |
5999 | case OPERATOR_GT: | |
6000 | exp->write_c_string(" > "); | |
6001 | break; | |
6002 | case OPERATOR_GE: | |
6003 | exp->write_c_string(" >= "); | |
6004 | break; | |
6005 | case OPERATOR_PLUS: | |
6006 | exp->write_c_string(" + "); | |
6007 | break; | |
6008 | case OPERATOR_MINUS: | |
6009 | exp->write_c_string(" - "); | |
6010 | break; | |
6011 | case OPERATOR_OR: | |
6012 | exp->write_c_string(" | "); | |
6013 | break; | |
6014 | case OPERATOR_XOR: | |
6015 | exp->write_c_string(" ^ "); | |
6016 | break; | |
6017 | case OPERATOR_MULT: | |
6018 | exp->write_c_string(" * "); | |
6019 | break; | |
6020 | case OPERATOR_DIV: | |
6021 | exp->write_c_string(" / "); | |
6022 | break; | |
6023 | case OPERATOR_MOD: | |
6024 | exp->write_c_string(" % "); | |
6025 | break; | |
6026 | case OPERATOR_LSHIFT: | |
6027 | exp->write_c_string(" << "); | |
6028 | break; | |
6029 | case OPERATOR_RSHIFT: | |
6030 | exp->write_c_string(" >> "); | |
6031 | break; | |
6032 | case OPERATOR_AND: | |
6033 | exp->write_c_string(" & "); | |
6034 | break; | |
6035 | case OPERATOR_BITCLEAR: | |
6036 | exp->write_c_string(" &^ "); | |
6037 | break; | |
6038 | default: | |
c3e6f413 | 6039 | go_unreachable(); |
e440a328 | 6040 | } |
6041 | this->right_->export_expression(exp); | |
6042 | exp->write_c_string(")"); | |
6043 | } | |
6044 | ||
6045 | // Import a binary expression. | |
6046 | ||
6047 | Expression* | |
6048 | Binary_expression::do_import(Import* imp) | |
6049 | { | |
6050 | imp->require_c_string("("); | |
6051 | ||
6052 | Expression* left = Expression::import_expression(imp); | |
6053 | ||
6054 | Operator op; | |
6055 | if (imp->match_c_string(" || ")) | |
6056 | { | |
6057 | op = OPERATOR_OROR; | |
6058 | imp->advance(4); | |
6059 | } | |
6060 | else if (imp->match_c_string(" && ")) | |
6061 | { | |
6062 | op = OPERATOR_ANDAND; | |
6063 | imp->advance(4); | |
6064 | } | |
6065 | else if (imp->match_c_string(" == ")) | |
6066 | { | |
6067 | op = OPERATOR_EQEQ; | |
6068 | imp->advance(4); | |
6069 | } | |
6070 | else if (imp->match_c_string(" != ")) | |
6071 | { | |
6072 | op = OPERATOR_NOTEQ; | |
6073 | imp->advance(4); | |
6074 | } | |
6075 | else if (imp->match_c_string(" < ")) | |
6076 | { | |
6077 | op = OPERATOR_LT; | |
6078 | imp->advance(3); | |
6079 | } | |
6080 | else if (imp->match_c_string(" <= ")) | |
6081 | { | |
6082 | op = OPERATOR_LE; | |
6083 | imp->advance(4); | |
6084 | } | |
6085 | else if (imp->match_c_string(" > ")) | |
6086 | { | |
6087 | op = OPERATOR_GT; | |
6088 | imp->advance(3); | |
6089 | } | |
6090 | else if (imp->match_c_string(" >= ")) | |
6091 | { | |
6092 | op = OPERATOR_GE; | |
6093 | imp->advance(4); | |
6094 | } | |
6095 | else if (imp->match_c_string(" + ")) | |
6096 | { | |
6097 | op = OPERATOR_PLUS; | |
6098 | imp->advance(3); | |
6099 | } | |
6100 | else if (imp->match_c_string(" - ")) | |
6101 | { | |
6102 | op = OPERATOR_MINUS; | |
6103 | imp->advance(3); | |
6104 | } | |
6105 | else if (imp->match_c_string(" | ")) | |
6106 | { | |
6107 | op = OPERATOR_OR; | |
6108 | imp->advance(3); | |
6109 | } | |
6110 | else if (imp->match_c_string(" ^ ")) | |
6111 | { | |
6112 | op = OPERATOR_XOR; | |
6113 | imp->advance(3); | |
6114 | } | |
6115 | else if (imp->match_c_string(" * ")) | |
6116 | { | |
6117 | op = OPERATOR_MULT; | |
6118 | imp->advance(3); | |
6119 | } | |
6120 | else if (imp->match_c_string(" / ")) | |
6121 | { | |
6122 | op = OPERATOR_DIV; | |
6123 | imp->advance(3); | |
6124 | } | |
6125 | else if (imp->match_c_string(" % ")) | |
6126 | { | |
6127 | op = OPERATOR_MOD; | |
6128 | imp->advance(3); | |
6129 | } | |
6130 | else if (imp->match_c_string(" << ")) | |
6131 | { | |
6132 | op = OPERATOR_LSHIFT; | |
6133 | imp->advance(4); | |
6134 | } | |
6135 | else if (imp->match_c_string(" >> ")) | |
6136 | { | |
6137 | op = OPERATOR_RSHIFT; | |
6138 | imp->advance(4); | |
6139 | } | |
6140 | else if (imp->match_c_string(" & ")) | |
6141 | { | |
6142 | op = OPERATOR_AND; | |
6143 | imp->advance(3); | |
6144 | } | |
6145 | else if (imp->match_c_string(" &^ ")) | |
6146 | { | |
6147 | op = OPERATOR_BITCLEAR; | |
6148 | imp->advance(4); | |
6149 | } | |
6150 | else | |
6151 | { | |
631d5788 | 6152 | go_error_at(imp->location(), "unrecognized binary operator"); |
e440a328 | 6153 | return Expression::make_error(imp->location()); |
6154 | } | |
6155 | ||
6156 | Expression* right = Expression::import_expression(imp); | |
6157 | ||
6158 | imp->require_c_string(")"); | |
6159 | ||
6160 | return Expression::make_binary(op, left, right, imp->location()); | |
6161 | } | |
6162 | ||
d751bb78 | 6163 | // Dump ast representation of a binary expression. |
6164 | ||
6165 | void | |
6166 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6167 | { | |
6168 | ast_dump_context->ostream() << "("; | |
6169 | ast_dump_context->dump_expression(this->left_); | |
6170 | ast_dump_context->ostream() << " "; | |
6171 | ast_dump_context->dump_operator(this->op_); | |
6172 | ast_dump_context->ostream() << " "; | |
6173 | ast_dump_context->dump_expression(this->right_); | |
6174 | ast_dump_context->ostream() << ") "; | |
6175 | } | |
6176 | ||
e440a328 | 6177 | // Make a binary expression. |
6178 | ||
6179 | Expression* | |
6180 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6181 | Location location) |
e440a328 | 6182 | { |
6183 | return new Binary_expression(op, left, right, location); | |
6184 | } | |
6185 | ||
6186 | // Implement a comparison. | |
6187 | ||
a32698ee | 6188 | Bexpression* |
6189 | Expression::comparison(Translate_context* context, Type* result_type, | |
6190 | Operator op, Expression* left, Expression* right, | |
6191 | Location location) | |
e440a328 | 6192 | { |
2387f644 | 6193 | Type* left_type = left->type(); |
6194 | Type* right_type = right->type(); | |
ceeb12d7 | 6195 | |
e67508fa | 6196 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6197 | |
15c67ee2 | 6198 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6199 | { |
6098d6cb | 6200 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
6201 | { | |
6202 | left = Runtime::make_call(Runtime::EQSTRING, location, 2, | |
6203 | left, right); | |
6204 | right = Expression::make_boolean(true, location); | |
6205 | } | |
6206 | else | |
6207 | { | |
6208 | left = Runtime::make_call(Runtime::CMPSTRING, location, 2, | |
6209 | left, right); | |
6210 | right = zexpr; | |
6211 | } | |
e440a328 | 6212 | } |
15c67ee2 | 6213 | else if ((left_type->interface_type() != NULL |
6214 | && right_type->interface_type() == NULL | |
6215 | && !right_type->is_nil_type()) | |
6216 | || (left_type->interface_type() == NULL | |
6217 | && !left_type->is_nil_type() | |
6218 | && right_type->interface_type() != NULL)) | |
e440a328 | 6219 | { |
6220 | // Comparing an interface value to a non-interface value. | |
6221 | if (left_type->interface_type() == NULL) | |
6222 | { | |
6223 | std::swap(left_type, right_type); | |
2387f644 | 6224 | std::swap(left, right); |
e440a328 | 6225 | } |
6226 | ||
6227 | // The right operand is not an interface. We need to take its | |
6228 | // address if it is not a pointer. | |
ceeb12d7 | 6229 | Expression* pointer_arg = NULL; |
e440a328 | 6230 | if (right_type->points_to() != NULL) |
2387f644 | 6231 | pointer_arg = right; |
e440a328 | 6232 | else |
6233 | { | |
2387f644 | 6234 | go_assert(right->is_addressable()); |
6235 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6236 | location); |
e440a328 | 6237 | } |
e440a328 | 6238 | |
2387f644 | 6239 | Expression* descriptor = |
6240 | Expression::make_type_descriptor(right_type, location); | |
6241 | left = | |
ceeb12d7 | 6242 | Runtime::make_call((left_type->interface_type()->is_empty() |
6098d6cb | 6243 | ? Runtime::EFACEVALEQ |
6244 | : Runtime::IFACEVALEQ), | |
2387f644 | 6245 | location, 3, left, descriptor, |
ceeb12d7 | 6246 | pointer_arg); |
6098d6cb | 6247 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6248 | right = Expression::make_boolean(true, location); | |
e440a328 | 6249 | } |
6250 | else if (left_type->interface_type() != NULL | |
6251 | && right_type->interface_type() != NULL) | |
6252 | { | |
ceeb12d7 | 6253 | Runtime::Function compare_function; |
739bad04 | 6254 | if (left_type->interface_type()->is_empty() |
6255 | && right_type->interface_type()->is_empty()) | |
6098d6cb | 6256 | compare_function = Runtime::EFACEEQ; |
739bad04 | 6257 | else if (!left_type->interface_type()->is_empty() |
6258 | && !right_type->interface_type()->is_empty()) | |
6098d6cb | 6259 | compare_function = Runtime::IFACEEQ; |
739bad04 | 6260 | else |
6261 | { | |
6262 | if (left_type->interface_type()->is_empty()) | |
6263 | { | |
739bad04 | 6264 | std::swap(left_type, right_type); |
2387f644 | 6265 | std::swap(left, right); |
739bad04 | 6266 | } |
c484d925 | 6267 | go_assert(!left_type->interface_type()->is_empty()); |
6268 | go_assert(right_type->interface_type()->is_empty()); | |
6098d6cb | 6269 | compare_function = Runtime::IFACEEFACEEQ; |
739bad04 | 6270 | } |
6271 | ||
2387f644 | 6272 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6098d6cb | 6273 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6274 | right = Expression::make_boolean(true, location); | |
e440a328 | 6275 | } |
6276 | ||
6277 | if (left_type->is_nil_type() | |
6278 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6279 | { | |
6280 | std::swap(left_type, right_type); | |
2387f644 | 6281 | std::swap(left, right); |
e440a328 | 6282 | } |
6283 | ||
6284 | if (right_type->is_nil_type()) | |
6285 | { | |
2387f644 | 6286 | right = Expression::make_nil(location); |
e440a328 | 6287 | if (left_type->array_type() != NULL |
6288 | && left_type->array_type()->length() == NULL) | |
6289 | { | |
6290 | Array_type* at = left_type->array_type(); | |
2387f644 | 6291 | left = at->get_value_pointer(context->gogo(), left); |
e440a328 | 6292 | } |
6293 | else if (left_type->interface_type() != NULL) | |
6294 | { | |
6295 | // An interface is nil if the first field is nil. | |
2387f644 | 6296 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6297 | } |
6298 | } | |
6299 | ||
ea664253 | 6300 | Bexpression* left_bexpr = left->get_backend(context); |
6301 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6302 | |
a32698ee | 6303 | Gogo* gogo = context->gogo(); |
6304 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6305 | right_bexpr, location); | |
6306 | if (result_type != NULL) | |
6307 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6308 | ret, location); | |
e440a328 | 6309 | return ret; |
6310 | } | |
6311 | ||
736a16ba | 6312 | // Class String_concat_expression. |
6313 | ||
6314 | bool | |
6315 | String_concat_expression::do_is_constant() const | |
6316 | { | |
6317 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6318 | pe != this->exprs_->end(); | |
6319 | ++pe) | |
6320 | { | |
6321 | if (!(*pe)->is_constant()) | |
6322 | return false; | |
6323 | } | |
6324 | return true; | |
6325 | } | |
6326 | ||
6327 | bool | |
6328 | String_concat_expression::do_is_immutable() const | |
6329 | { | |
6330 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6331 | pe != this->exprs_->end(); | |
6332 | ++pe) | |
6333 | { | |
6334 | if (!(*pe)->is_immutable()) | |
6335 | return false; | |
6336 | } | |
6337 | return true; | |
6338 | } | |
6339 | ||
6340 | Type* | |
6341 | String_concat_expression::do_type() | |
6342 | { | |
6343 | Type* t = this->exprs_->front()->type(); | |
6344 | Expression_list::iterator pe = this->exprs_->begin(); | |
6345 | ++pe; | |
6346 | for (; pe != this->exprs_->end(); ++pe) | |
6347 | { | |
6348 | Type* t1; | |
6349 | if (!Binary_expression::operation_type(OPERATOR_PLUS, t, | |
6350 | (*pe)->type(), | |
6351 | &t1)) | |
6352 | return Type::make_error_type(); | |
6353 | t = t1; | |
6354 | } | |
6355 | return t; | |
6356 | } | |
6357 | ||
6358 | void | |
6359 | String_concat_expression::do_determine_type(const Type_context* context) | |
6360 | { | |
6361 | Type_context subcontext(*context); | |
6362 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6363 | pe != this->exprs_->end(); | |
6364 | ++pe) | |
6365 | { | |
6366 | Type* t = (*pe)->type(); | |
6367 | if (!t->is_abstract()) | |
6368 | { | |
6369 | subcontext.type = t; | |
6370 | break; | |
6371 | } | |
6372 | } | |
6373 | if (subcontext.type == NULL) | |
6374 | subcontext.type = this->exprs_->front()->type(); | |
6375 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6376 | pe != this->exprs_->end(); | |
6377 | ++pe) | |
6378 | (*pe)->determine_type(&subcontext); | |
6379 | } | |
6380 | ||
6381 | void | |
6382 | String_concat_expression::do_check_types(Gogo*) | |
6383 | { | |
6384 | if (this->is_error_expression()) | |
6385 | return; | |
6386 | Type* t = this->exprs_->front()->type(); | |
6387 | if (t->is_error()) | |
6388 | { | |
6389 | this->set_is_error(); | |
6390 | return; | |
6391 | } | |
6392 | Expression_list::iterator pe = this->exprs_->begin(); | |
6393 | ++pe; | |
6394 | for (; pe != this->exprs_->end(); ++pe) | |
6395 | { | |
6396 | Type* t1 = (*pe)->type(); | |
6397 | if (!Type::are_compatible_for_binop(t, t1)) | |
6398 | { | |
6399 | this->report_error("incompatible types in binary expression"); | |
6400 | return; | |
6401 | } | |
6402 | if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, | |
6403 | this->location())) | |
6404 | { | |
6405 | this->set_is_error(); | |
6406 | return; | |
6407 | } | |
6408 | } | |
6409 | } | |
6410 | ||
6411 | Expression* | |
6412 | String_concat_expression::do_flatten(Gogo*, Named_object*, | |
6413 | Statement_inserter*) | |
6414 | { | |
6415 | if (this->is_error_expression()) | |
6416 | return this; | |
6417 | Location loc = this->location(); | |
6418 | Type* type = this->type(); | |
6419 | Expression* nil_arg = Expression::make_nil(loc); | |
6420 | Expression* call; | |
6421 | switch (this->exprs_->size()) | |
6422 | { | |
6423 | case 0: case 1: | |
6424 | go_unreachable(); | |
6425 | ||
6426 | case 2: case 3: case 4: case 5: | |
6427 | { | |
6428 | Expression* len = Expression::make_integer_ul(this->exprs_->size(), | |
6429 | NULL, loc); | |
6430 | Array_type* arg_type = Type::make_array_type(type, len); | |
6431 | arg_type->set_is_array_incomparable(); | |
6432 | Expression* arg = | |
6433 | Expression::make_array_composite_literal(arg_type, this->exprs_, | |
6434 | loc); | |
6435 | Runtime::Function code; | |
6436 | switch (this->exprs_->size()) | |
6437 | { | |
6438 | default: | |
6439 | go_unreachable(); | |
6440 | case 2: | |
6441 | code = Runtime::CONCATSTRING2; | |
6442 | break; | |
6443 | case 3: | |
6444 | code = Runtime::CONCATSTRING3; | |
6445 | break; | |
6446 | case 4: | |
6447 | code = Runtime::CONCATSTRING4; | |
6448 | break; | |
6449 | case 5: | |
6450 | code = Runtime::CONCATSTRING5; | |
6451 | break; | |
6452 | } | |
6453 | call = Runtime::make_call(code, loc, 2, nil_arg, arg); | |
6454 | } | |
6455 | break; | |
6456 | ||
6457 | default: | |
6458 | { | |
6459 | Type* arg_type = Type::make_array_type(type, NULL); | |
6460 | Slice_construction_expression* sce = | |
6461 | Expression::make_slice_composite_literal(arg_type, this->exprs_, | |
6462 | loc); | |
6463 | sce->set_storage_does_not_escape(); | |
6464 | call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg, | |
6465 | sce); | |
6466 | } | |
6467 | break; | |
6468 | } | |
6469 | ||
6470 | return Expression::make_cast(type, call, loc); | |
6471 | } | |
6472 | ||
6473 | void | |
6474 | String_concat_expression::do_dump_expression( | |
6475 | Ast_dump_context* ast_dump_context) const | |
6476 | { | |
6477 | ast_dump_context->ostream() << "concat("; | |
6478 | ast_dump_context->dump_expression_list(this->exprs_, false); | |
6479 | ast_dump_context->ostream() << ")"; | |
6480 | } | |
6481 | ||
6482 | Expression* | |
6483 | Expression::make_string_concat(Expression_list* exprs) | |
6484 | { | |
6485 | return new String_concat_expression(exprs); | |
6486 | } | |
6487 | ||
e440a328 | 6488 | // Class Bound_method_expression. |
6489 | ||
6490 | // Traversal. | |
6491 | ||
6492 | int | |
6493 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6494 | { | |
e0659c9e | 6495 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6496 | } |
6497 | ||
6498 | // Return the type of a bound method expression. The type of this | |
0afbb937 | 6499 | // object is simply the type of the method with no receiver. |
e440a328 | 6500 | |
6501 | Type* | |
6502 | Bound_method_expression::do_type() | |
6503 | { | |
0afbb937 | 6504 | Named_object* fn = this->method_->named_object(); |
6505 | Function_type* fntype; | |
6506 | if (fn->is_function()) | |
6507 | fntype = fn->func_value()->type(); | |
6508 | else if (fn->is_function_declaration()) | |
6509 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6510 | else |
6511 | return Type::make_error_type(); | |
0afbb937 | 6512 | return fntype->copy_without_receiver(); |
e440a328 | 6513 | } |
6514 | ||
6515 | // Determine the types of a method expression. | |
6516 | ||
6517 | void | |
6518 | Bound_method_expression::do_determine_type(const Type_context*) | |
6519 | { | |
0afbb937 | 6520 | Named_object* fn = this->method_->named_object(); |
6521 | Function_type* fntype; | |
6522 | if (fn->is_function()) | |
6523 | fntype = fn->func_value()->type(); | |
6524 | else if (fn->is_function_declaration()) | |
6525 | fntype = fn->func_declaration_value()->type(); | |
6526 | else | |
6527 | fntype = NULL; | |
e440a328 | 6528 | if (fntype == NULL || !fntype->is_method()) |
6529 | this->expr_->determine_type_no_context(); | |
6530 | else | |
6531 | { | |
6532 | Type_context subcontext(fntype->receiver()->type(), false); | |
6533 | this->expr_->determine_type(&subcontext); | |
6534 | } | |
6535 | } | |
6536 | ||
6537 | // Check the types of a method expression. | |
6538 | ||
6539 | void | |
6540 | Bound_method_expression::do_check_types(Gogo*) | |
6541 | { | |
0afbb937 | 6542 | Named_object* fn = this->method_->named_object(); |
6543 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6544 | { | |
6545 | this->report_error(_("object is not a method")); | |
6546 | return; | |
6547 | } | |
6548 | ||
6549 | Function_type* fntype; | |
6550 | if (fn->is_function()) | |
6551 | fntype = fn->func_value()->type(); | |
6552 | else if (fn->is_function_declaration()) | |
6553 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6554 | else |
0afbb937 | 6555 | go_unreachable(); |
6556 | Type* rtype = fntype->receiver()->type()->deref(); | |
6557 | Type* etype = (this->expr_type_ != NULL | |
6558 | ? this->expr_type_ | |
6559 | : this->expr_->type()); | |
6560 | etype = etype->deref(); | |
6561 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6562 | this->report_error(_("method type does not match object type")); | |
6563 | } | |
6564 | ||
6565 | // If a bound method expression is not simply called, then it is | |
6566 | // represented as a closure. The closure will hold a single variable, | |
6567 | // the receiver to pass to the method. The function will be a simple | |
6568 | // thunk that pulls that value from the closure and calls the method | |
6569 | // with the remaining arguments. | |
6570 | // | |
6571 | // Because method values are not common, we don't build all thunks for | |
6572 | // every methods, but instead only build them as we need them. In | |
6573 | // particular, we even build them on demand for methods defined in | |
6574 | // other packages. | |
6575 | ||
6576 | Bound_method_expression::Method_value_thunks | |
6577 | Bound_method_expression::method_value_thunks; | |
6578 | ||
6579 | // Find or create the thunk for METHOD. | |
6580 | ||
6581 | Named_object* | |
6582 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6583 | Named_object* fn) | |
6584 | { | |
6585 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6586 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6587 | Bound_method_expression::method_value_thunks.insert(val); | |
6588 | if (!ins.second) | |
6589 | { | |
6590 | // We have seen this method before. | |
6591 | go_assert(ins.first->second != NULL); | |
6592 | return ins.first->second; | |
6593 | } | |
6594 | ||
6595 | Location loc = fn->location(); | |
6596 | ||
6597 | Function_type* orig_fntype; | |
6598 | if (fn->is_function()) | |
6599 | orig_fntype = fn->func_value()->type(); | |
6600 | else if (fn->is_function_declaration()) | |
6601 | orig_fntype = fn->func_declaration_value()->type(); | |
6602 | else | |
6603 | orig_fntype = NULL; | |
6604 | ||
6605 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6606 | { |
0afbb937 | 6607 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6608 | return ins.first->second; | |
e440a328 | 6609 | } |
0afbb937 | 6610 | |
6611 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6612 | // The type here is wrong--it should be the C function type. But it |
6613 | // doesn't really matter. | |
0afbb937 | 6614 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6615 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6616 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6617 | orig_fntype->receiver()->type(), | |
6618 | loc))); | |
6619 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
6620 | closure_type = Type::make_pointer_type(closure_type); | |
6621 | ||
f8bdf81a | 6622 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6623 | |
da244e59 | 6624 | std::string thunk_name = Gogo::thunk_name(); |
6625 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 6626 | false, loc); |
6627 | ||
f8bdf81a | 6628 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6629 | cvar->set_is_used(); | |
1ecc6157 | 6630 | cvar->set_is_closure(); |
da244e59 | 6631 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6632 | NULL, cvar); | |
f8bdf81a | 6633 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6634 | |
f8bdf81a | 6635 | gogo->start_block(loc); |
0afbb937 | 6636 | |
6637 | // Field 0 of the closure is the function code pointer, field 1 is | |
6638 | // the value on which to invoke the method. | |
6639 | Expression* arg = Expression::make_var_reference(cp, loc); | |
6640 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
6641 | arg = Expression::make_field_reference(arg, 1, loc); | |
6642 | ||
6643 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6644 | ||
6645 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6646 | Expression_list* args; | |
6647 | if (orig_params == NULL || orig_params->empty()) | |
6648 | args = NULL; | |
6649 | else | |
6650 | { | |
6651 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6652 | args = new Expression_list(); | |
6653 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6654 | p != new_params->end(); |
0afbb937 | 6655 | ++p) |
6656 | { | |
6657 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6658 | go_assert(p_no != NULL | |
6659 | && p_no->is_variable() | |
6660 | && p_no->var_value()->is_parameter()); | |
6661 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6662 | } | |
6663 | } | |
6664 | ||
6665 | Call_expression* call = Expression::make_call(bme, args, | |
6666 | orig_fntype->is_varargs(), | |
6667 | loc); | |
6668 | call->set_varargs_are_lowered(); | |
6669 | ||
6670 | Statement* s = Statement::make_return_from_call(call, loc); | |
6671 | gogo->add_statement(s); | |
6672 | Block* b = gogo->finish_block(loc); | |
6673 | gogo->add_block(b, loc); | |
6674 | gogo->lower_block(new_no, b); | |
a32698ee | 6675 | gogo->flatten_block(new_no, b); |
0afbb937 | 6676 | gogo->finish_function(loc); |
6677 | ||
6678 | ins.first->second = new_no; | |
6679 | return new_no; | |
6680 | } | |
6681 | ||
6682 | // Return an expression to check *REF for nil while dereferencing | |
6683 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6684 | // reference. This is a static function so that we don't have to | |
6685 | // worry about declaring Field_indexes in expressions.h. | |
6686 | ||
6687 | static Expression* | |
6688 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6689 | Expression** ref) | |
6690 | { | |
6691 | if (field_indexes == NULL) | |
6692 | return Expression::make_boolean(false, loc); | |
6693 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6694 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6695 | go_assert(stype != NULL | |
6696 | && field_indexes->field_index < stype->field_count()); | |
6697 | if ((*ref)->type()->struct_type() == NULL) | |
6698 | { | |
6699 | go_assert((*ref)->type()->points_to() != NULL); | |
6700 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6701 | Expression::make_nil(loc), | |
6702 | loc); | |
6703 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
6704 | *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); | |
6705 | go_assert((*ref)->type()->struct_type() == stype); | |
6706 | } | |
6707 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6708 | loc); | |
6709 | return cond; | |
e440a328 | 6710 | } |
6711 | ||
cd39797e | 6712 | // Flatten a method value into a struct with nil checks. We can't do |
6713 | // this in the lowering phase, because if the method value is called | |
6714 | // directly we don't need a thunk. That case will have been handled | |
6715 | // by Call_expression::do_lower, so if we get here then we do need a | |
6716 | // thunk. | |
e440a328 | 6717 | |
cd39797e | 6718 | Expression* |
6719 | Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, | |
6720 | Statement_inserter* inserter) | |
e440a328 | 6721 | { |
cd39797e | 6722 | Location loc = this->location(); |
6723 | ||
6724 | Named_object* thunk = Bound_method_expression::create_thunk(gogo, | |
0afbb937 | 6725 | this->method_, |
6726 | this->function_); | |
6727 | if (thunk->is_erroneous()) | |
6728 | { | |
6729 | go_assert(saw_errors()); | |
cd39797e | 6730 | return Expression::make_error(loc); |
0afbb937 | 6731 | } |
6732 | ||
cd39797e | 6733 | // Force the expression into a variable. This is only necessary if |
6734 | // we are going to do nil checks below, but it's easy enough to | |
6735 | // always do it. | |
6736 | Expression* expr = this->expr_; | |
6737 | if (!expr->is_variable()) | |
6738 | { | |
6739 | Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); | |
6740 | inserter->insert(etemp); | |
6741 | expr = Expression::make_temporary_reference(etemp, loc); | |
6742 | } | |
0afbb937 | 6743 | |
6744 | // If the method expects a value, and we have a pointer, we need to | |
6745 | // dereference the pointer. | |
6746 | ||
6747 | Named_object* fn = this->method_->named_object(); | |
cd39797e | 6748 | Function_type *fntype; |
0afbb937 | 6749 | if (fn->is_function()) |
6750 | fntype = fn->func_value()->type(); | |
6751 | else if (fn->is_function_declaration()) | |
6752 | fntype = fn->func_declaration_value()->type(); | |
6753 | else | |
6754 | go_unreachable(); | |
6755 | ||
cd39797e | 6756 | Expression* val = expr; |
0afbb937 | 6757 | if (fntype->receiver()->type()->points_to() == NULL |
6758 | && val->type()->points_to() != NULL) | |
6759 | val = Expression::make_unary(OPERATOR_MULT, val, loc); | |
6760 | ||
6761 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
6762 | // expect a closure whose second field has type this->expr_type_ (if | |
6763 | // that is not NULL). We are going to pass it a closure whose | |
6764 | // second field has type this->expr_->type(). Since | |
6765 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
6766 | // away with this. | |
6767 | ||
6768 | Struct_field_list* fields = new Struct_field_list(); | |
6769 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
6770 | thunk->func_value()->type(), | |
6771 | loc))); | |
6772 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
6773 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6774 | ||
6775 | Expression_list* vals = new Expression_list(); | |
6776 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
6777 | vals->push_back(val); | |
6778 | ||
6779 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
0afbb937 | 6780 | |
cd39797e | 6781 | if (!gogo->compiling_runtime() || gogo->package_name() != "runtime") |
6782 | ret = Expression::make_heap_expression(ret, loc); | |
6783 | else | |
6784 | { | |
6785 | // When compiling the runtime, method closures do not escape. | |
6786 | // When escape analysis becomes the default, and applies to | |
6787 | // method closures, this should be changed to make it an error | |
6788 | // if a method closure escapes. | |
6789 | Temporary_statement* ctemp = Statement::make_temporary(st, ret, loc); | |
6790 | inserter->insert(ctemp); | |
6791 | ret = Expression::make_temporary_reference(ctemp, loc); | |
6792 | ret = Expression::make_unary(OPERATOR_AND, ret, loc); | |
6793 | ret->unary_expression()->set_does_not_escape(); | |
6794 | } | |
6795 | ||
6796 | // If necessary, check whether the expression or any embedded | |
6797 | // pointers are nil. | |
0afbb937 | 6798 | |
df7ef1fd | 6799 | Expression* nil_check = NULL; |
0afbb937 | 6800 | if (this->method_->field_indexes() != NULL) |
6801 | { | |
0afbb937 | 6802 | Expression* ref = expr; |
6803 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
6804 | expr = ref; | |
6805 | } | |
6806 | ||
6807 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
6808 | { | |
6809 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
6810 | Expression::make_nil(loc), | |
6811 | loc); | |
6812 | if (nil_check == NULL) | |
6813 | nil_check = n; | |
6814 | else | |
6815 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
6816 | } | |
6817 | ||
6818 | if (nil_check != NULL) | |
6819 | { | |
cd39797e | 6820 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
6821 | loc); | |
6822 | // Fix the type of the conditional expression by pretending to | |
6823 | // evaluate to RET either way through the conditional. | |
6824 | crash = Expression::make_compound(crash, ret, loc); | |
6825 | ret = Expression::make_conditional(nil_check, crash, ret, loc); | |
6826 | } | |
6827 | ||
6828 | // RET is a pointer to a struct, but we want a function type. | |
6829 | ret = Expression::make_unsafe_cast(this->type(), ret, loc); | |
6830 | ||
6831 | return ret; | |
e440a328 | 6832 | } |
6833 | ||
d751bb78 | 6834 | // Dump ast representation of a bound method expression. |
6835 | ||
6836 | void | |
6837 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6838 | const | |
6839 | { | |
6840 | if (this->expr_type_ != NULL) | |
6841 | ast_dump_context->ostream() << "("; | |
6842 | ast_dump_context->dump_expression(this->expr_); | |
6843 | if (this->expr_type_ != NULL) | |
6844 | { | |
6845 | ast_dump_context->ostream() << ":"; | |
6846 | ast_dump_context->dump_type(this->expr_type_); | |
6847 | ast_dump_context->ostream() << ")"; | |
6848 | } | |
6849 | ||
0afbb937 | 6850 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 6851 | } |
6852 | ||
e440a328 | 6853 | // Make a method expression. |
6854 | ||
6855 | Bound_method_expression* | |
0afbb937 | 6856 | Expression::make_bound_method(Expression* expr, const Method* method, |
6857 | Named_object* function, Location location) | |
e440a328 | 6858 | { |
0afbb937 | 6859 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 6860 | } |
6861 | ||
6862 | // Class Builtin_call_expression. This is used for a call to a | |
6863 | // builtin function. | |
6864 | ||
6865 | class Builtin_call_expression : public Call_expression | |
6866 | { | |
6867 | public: | |
6868 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6869 | bool is_varargs, Location location); |
e440a328 | 6870 | |
6871 | protected: | |
6872 | // This overrides Call_expression::do_lower. | |
6873 | Expression* | |
ceeb4318 | 6874 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6875 | |
35a54f17 | 6876 | Expression* |
6877 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
6878 | ||
e440a328 | 6879 | bool |
6880 | do_is_constant() const; | |
6881 | ||
6882 | bool | |
0c77715b | 6883 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6884 | |
4f2138d7 | 6885 | bool |
a7549a6a | 6886 | do_discarding_value(); |
6887 | ||
e440a328 | 6888 | Type* |
6889 | do_type(); | |
6890 | ||
6891 | void | |
6892 | do_determine_type(const Type_context*); | |
6893 | ||
6894 | void | |
6895 | do_check_types(Gogo*); | |
6896 | ||
6897 | Expression* | |
72666aed | 6898 | do_copy(); |
e440a328 | 6899 | |
ea664253 | 6900 | Bexpression* |
6901 | do_get_backend(Translate_context*); | |
e440a328 | 6902 | |
6903 | void | |
6904 | do_export(Export*) const; | |
6905 | ||
6906 | virtual bool | |
6907 | do_is_recover_call() const; | |
6908 | ||
6909 | virtual void | |
6910 | do_set_recover_arg(Expression*); | |
6911 | ||
6912 | private: | |
6913 | // The builtin functions. | |
6914 | enum Builtin_function_code | |
6915 | { | |
6916 | BUILTIN_INVALID, | |
6917 | ||
6918 | // Predeclared builtin functions. | |
6919 | BUILTIN_APPEND, | |
6920 | BUILTIN_CAP, | |
6921 | BUILTIN_CLOSE, | |
48080209 | 6922 | BUILTIN_COMPLEX, |
e440a328 | 6923 | BUILTIN_COPY, |
1cce762f | 6924 | BUILTIN_DELETE, |
e440a328 | 6925 | BUILTIN_IMAG, |
6926 | BUILTIN_LEN, | |
6927 | BUILTIN_MAKE, | |
6928 | BUILTIN_NEW, | |
6929 | BUILTIN_PANIC, | |
6930 | BUILTIN_PRINT, | |
6931 | BUILTIN_PRINTLN, | |
6932 | BUILTIN_REAL, | |
6933 | BUILTIN_RECOVER, | |
6934 | ||
6935 | // Builtin functions from the unsafe package. | |
6936 | BUILTIN_ALIGNOF, | |
6937 | BUILTIN_OFFSETOF, | |
6938 | BUILTIN_SIZEOF | |
6939 | }; | |
6940 | ||
6941 | Expression* | |
6942 | one_arg() const; | |
6943 | ||
6944 | bool | |
6945 | check_one_arg(); | |
6946 | ||
6947 | static Type* | |
6948 | real_imag_type(Type*); | |
6949 | ||
6950 | static Type* | |
48080209 | 6951 | complex_type(Type*); |
e440a328 | 6952 | |
a9182619 | 6953 | Expression* |
6954 | lower_make(); | |
6955 | ||
6956 | bool | |
1ad00fd4 | 6957 | check_int_value(Expression*, bool is_length); |
a9182619 | 6958 | |
e440a328 | 6959 | // A pointer back to the general IR structure. This avoids a global |
6960 | // variable, or passing it around everywhere. | |
6961 | Gogo* gogo_; | |
6962 | // The builtin function being called. | |
6963 | Builtin_function_code code_; | |
0f914071 | 6964 | // Used to stop endless loops when the length of an array uses len |
6965 | // or cap of the array itself. | |
6966 | mutable bool seen_; | |
6334270b | 6967 | // Whether the argument is set for calls to BUILTIN_RECOVER. |
6968 | bool recover_arg_is_set_; | |
e440a328 | 6969 | }; |
6970 | ||
6971 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6972 | Expression* fn, | |
6973 | Expression_list* args, | |
6974 | bool is_varargs, | |
b13c66cd | 6975 | Location location) |
e440a328 | 6976 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 6977 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
6978 | recover_arg_is_set_(false) | |
e440a328 | 6979 | { |
6980 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 6981 | if (fnexp == NULL) |
6982 | { | |
6983 | this->code_ = BUILTIN_INVALID; | |
6984 | return; | |
6985 | } | |
e440a328 | 6986 | const std::string& name(fnexp->named_object()->name()); |
6987 | if (name == "append") | |
6988 | this->code_ = BUILTIN_APPEND; | |
6989 | else if (name == "cap") | |
6990 | this->code_ = BUILTIN_CAP; | |
6991 | else if (name == "close") | |
6992 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6993 | else if (name == "complex") |
6994 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6995 | else if (name == "copy") |
6996 | this->code_ = BUILTIN_COPY; | |
1cce762f | 6997 | else if (name == "delete") |
6998 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 6999 | else if (name == "imag") |
7000 | this->code_ = BUILTIN_IMAG; | |
7001 | else if (name == "len") | |
7002 | this->code_ = BUILTIN_LEN; | |
7003 | else if (name == "make") | |
7004 | this->code_ = BUILTIN_MAKE; | |
7005 | else if (name == "new") | |
7006 | this->code_ = BUILTIN_NEW; | |
7007 | else if (name == "panic") | |
7008 | this->code_ = BUILTIN_PANIC; | |
7009 | else if (name == "print") | |
7010 | this->code_ = BUILTIN_PRINT; | |
7011 | else if (name == "println") | |
7012 | this->code_ = BUILTIN_PRINTLN; | |
7013 | else if (name == "real") | |
7014 | this->code_ = BUILTIN_REAL; | |
7015 | else if (name == "recover") | |
7016 | this->code_ = BUILTIN_RECOVER; | |
7017 | else if (name == "Alignof") | |
7018 | this->code_ = BUILTIN_ALIGNOF; | |
7019 | else if (name == "Offsetof") | |
7020 | this->code_ = BUILTIN_OFFSETOF; | |
7021 | else if (name == "Sizeof") | |
7022 | this->code_ = BUILTIN_SIZEOF; | |
7023 | else | |
c3e6f413 | 7024 | go_unreachable(); |
e440a328 | 7025 | } |
7026 | ||
7027 | // Return whether this is a call to recover. This is a virtual | |
7028 | // function called from the parent class. | |
7029 | ||
7030 | bool | |
7031 | Builtin_call_expression::do_is_recover_call() const | |
7032 | { | |
7033 | if (this->classification() == EXPRESSION_ERROR) | |
7034 | return false; | |
7035 | return this->code_ == BUILTIN_RECOVER; | |
7036 | } | |
7037 | ||
7038 | // Set the argument for a call to recover. | |
7039 | ||
7040 | void | |
7041 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7042 | { | |
7043 | const Expression_list* args = this->args(); | |
c484d925 | 7044 | go_assert(args == NULL || args->empty()); |
e440a328 | 7045 | Expression_list* new_args = new Expression_list(); |
7046 | new_args->push_back(arg); | |
7047 | this->set_args(new_args); | |
6334270b | 7048 | this->recover_arg_is_set_ = true; |
e440a328 | 7049 | } |
7050 | ||
e440a328 | 7051 | // Lower a builtin call expression. This turns new and make into |
7052 | // specific expressions. We also convert to a constant if we can. | |
7053 | ||
7054 | Expression* | |
ceeb4318 | 7055 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
7056 | Statement_inserter* inserter, int) | |
e440a328 | 7057 | { |
79651b1f | 7058 | if (this->is_error_expression()) |
a9182619 | 7059 | return this; |
7060 | ||
b13c66cd | 7061 | Location loc = this->location(); |
1cce762f | 7062 | |
a8725655 | 7063 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7064 | { | |
7065 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7066 | return Expression::make_error(loc); |
a8725655 | 7067 | } |
7068 | ||
393ba00b | 7069 | if (this->code_ == BUILTIN_OFFSETOF) |
7070 | { | |
7071 | Expression* arg = this->one_arg(); | |
12e69faa | 7072 | |
7073 | if (arg->bound_method_expression() != NULL | |
7074 | || arg->interface_field_reference_expression() != NULL) | |
7075 | { | |
7076 | this->report_error(_("invalid use of method value as argument " | |
7077 | "of Offsetof")); | |
7078 | return this; | |
7079 | } | |
7080 | ||
393ba00b | 7081 | Field_reference_expression* farg = arg->field_reference_expression(); |
7082 | while (farg != NULL) | |
7083 | { | |
7084 | if (!farg->implicit()) | |
7085 | break; | |
7086 | // When the selector refers to an embedded field, | |
7087 | // it must not be reached through pointer indirections. | |
7088 | if (farg->expr()->deref() != farg->expr()) | |
7089 | { | |
12e69faa | 7090 | this->report_error(_("argument of Offsetof implies " |
7091 | "indirection of an embedded field")); | |
393ba00b | 7092 | return this; |
7093 | } | |
7094 | // Go up until we reach the original base. | |
7095 | farg = farg->expr()->field_reference_expression(); | |
7096 | } | |
7097 | } | |
7098 | ||
1cce762f | 7099 | if (this->is_constant()) |
e440a328 | 7100 | { |
0c77715b | 7101 | Numeric_constant nc; |
7102 | if (this->numeric_constant_value(&nc)) | |
7103 | return nc.expression(loc); | |
e440a328 | 7104 | } |
1cce762f | 7105 | |
7106 | switch (this->code_) | |
e440a328 | 7107 | { |
1cce762f | 7108 | default: |
7109 | break; | |
7110 | ||
7111 | case BUILTIN_NEW: | |
7112 | { | |
7113 | const Expression_list* args = this->args(); | |
7114 | if (args == NULL || args->size() < 1) | |
7115 | this->report_error(_("not enough arguments")); | |
7116 | else if (args->size() > 1) | |
7117 | this->report_error(_("too many arguments")); | |
7118 | else | |
7119 | { | |
7120 | Expression* arg = args->front(); | |
7121 | if (!arg->is_type_expression()) | |
7122 | { | |
631d5788 | 7123 | go_error_at(arg->location(), "expected type"); |
1cce762f | 7124 | this->set_is_error(); |
7125 | } | |
7126 | else | |
7127 | return Expression::make_allocation(arg->type(), loc); | |
7128 | } | |
7129 | } | |
7130 | break; | |
7131 | ||
7132 | case BUILTIN_MAKE: | |
7133 | return this->lower_make(); | |
7134 | ||
7135 | case BUILTIN_RECOVER: | |
e440a328 | 7136 | if (function != NULL) |
7137 | function->func_value()->set_calls_recover(); | |
7138 | else | |
7139 | { | |
7140 | // Calling recover outside of a function always returns the | |
7141 | // nil empty interface. | |
823c7e3d | 7142 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7143 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7144 | } |
1cce762f | 7145 | break; |
7146 | ||
7147 | case BUILTIN_APPEND: | |
7148 | { | |
7149 | // Lower the varargs. | |
7150 | const Expression_list* args = this->args(); | |
7151 | if (args == NULL || args->empty()) | |
e440a328 | 7152 | return this; |
1cce762f | 7153 | Type* slice_type = args->front()->type(); |
7154 | if (!slice_type->is_slice_type()) | |
7155 | { | |
3ff4863b | 7156 | if (slice_type->is_nil_type()) |
631d5788 | 7157 | go_error_at(args->front()->location(), "use of untyped nil"); |
3ff4863b | 7158 | else |
631d5788 | 7159 | go_error_at(args->front()->location(), |
7160 | "argument 1 must be a slice"); | |
1cce762f | 7161 | this->set_is_error(); |
7162 | return this; | |
7163 | } | |
19fd40c3 | 7164 | Type* element_type = slice_type->array_type()->element_type(); |
7165 | this->lower_varargs(gogo, function, inserter, | |
7166 | Type::make_array_type(element_type, NULL), | |
0e9a2e72 | 7167 | 2, SLICE_STORAGE_DOES_NOT_ESCAPE); |
1cce762f | 7168 | } |
7169 | break; | |
7170 | ||
7171 | case BUILTIN_DELETE: | |
7172 | { | |
7173 | // Lower to a runtime function call. | |
7174 | const Expression_list* args = this->args(); | |
7175 | if (args == NULL || args->size() < 2) | |
7176 | this->report_error(_("not enough arguments")); | |
7177 | else if (args->size() > 2) | |
7178 | this->report_error(_("too many arguments")); | |
7179 | else if (args->front()->type()->map_type() == NULL) | |
7180 | this->report_error(_("argument 1 must be a map")); | |
7181 | else | |
7182 | { | |
7183 | // Since this function returns no value it must appear in | |
7184 | // a statement by itself, so we don't have to worry about | |
7185 | // order of evaluation of values around it. Evaluate the | |
7186 | // map first to get order of evaluation right. | |
7187 | Map_type* mt = args->front()->type()->map_type(); | |
7188 | Temporary_statement* map_temp = | |
7189 | Statement::make_temporary(mt, args->front(), loc); | |
7190 | inserter->insert(map_temp); | |
7191 | ||
7192 | Temporary_statement* key_temp = | |
7193 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7194 | inserter->insert(key_temp); | |
7195 | ||
0d5530d9 | 7196 | Expression* e1 = Expression::make_type_descriptor(mt, loc); |
7197 | Expression* e2 = Expression::make_temporary_reference(map_temp, | |
1cce762f | 7198 | loc); |
0d5530d9 | 7199 | Expression* e3 = Expression::make_temporary_reference(key_temp, |
1cce762f | 7200 | loc); |
0d5530d9 | 7201 | e3 = Expression::make_unary(OPERATOR_AND, e3, loc); |
1cce762f | 7202 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), |
0d5530d9 | 7203 | 3, e1, e2, e3); |
1cce762f | 7204 | } |
7205 | } | |
7206 | break; | |
88b03a70 | 7207 | |
7208 | case BUILTIN_PRINT: | |
7209 | case BUILTIN_PRINTLN: | |
7210 | // Force all the arguments into temporary variables, so that we | |
7211 | // don't try to evaluate something while holding the print lock. | |
7212 | if (this->args() == NULL) | |
7213 | break; | |
7214 | for (Expression_list::iterator pa = this->args()->begin(); | |
7215 | pa != this->args()->end(); | |
7216 | ++pa) | |
7217 | { | |
7218 | if (!(*pa)->is_variable()) | |
7219 | { | |
7220 | Temporary_statement* temp = | |
7221 | Statement::make_temporary(NULL, *pa, loc); | |
7222 | inserter->insert(temp); | |
7223 | *pa = Expression::make_temporary_reference(temp, loc); | |
7224 | } | |
7225 | } | |
7226 | break; | |
e440a328 | 7227 | } |
7228 | ||
7229 | return this; | |
7230 | } | |
7231 | ||
35a54f17 | 7232 | // Flatten a builtin call expression. This turns the arguments of copy and |
7233 | // append into temporary expressions. | |
7234 | ||
7235 | Expression* | |
7236 | Builtin_call_expression::do_flatten(Gogo*, Named_object*, | |
7237 | Statement_inserter* inserter) | |
7238 | { | |
16cb7fec | 7239 | Location loc = this->location(); |
7240 | ||
7241 | switch (this->code_) | |
35a54f17 | 7242 | { |
16cb7fec | 7243 | default: |
7244 | break; | |
7245 | ||
7246 | case BUILTIN_APPEND: | |
7247 | case BUILTIN_COPY: | |
7248 | { | |
7249 | Type* at = this->args()->front()->type(); | |
7250 | for (Expression_list::iterator pa = this->args()->begin(); | |
7251 | pa != this->args()->end(); | |
7252 | ++pa) | |
7253 | { | |
7254 | if ((*pa)->is_nil_expression()) | |
7255 | { | |
7256 | Expression* nil = Expression::make_nil(loc); | |
7257 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
7258 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
7259 | } | |
7260 | if (!(*pa)->is_variable()) | |
7261 | { | |
7262 | Temporary_statement* temp = | |
7263 | Statement::make_temporary(NULL, *pa, loc); | |
7264 | inserter->insert(temp); | |
7265 | *pa = Expression::make_temporary_reference(temp, loc); | |
7266 | } | |
7267 | } | |
7268 | } | |
7269 | break; | |
7270 | ||
7271 | case BUILTIN_PANIC: | |
35a54f17 | 7272 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 7273 | pa != this->args()->end(); |
7274 | ++pa) | |
7275 | { | |
7276 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 7277 | { |
16cb7fec | 7278 | Temporary_statement* temp = |
7279 | Statement::make_temporary(NULL, *pa, loc); | |
7280 | inserter->insert(temp); | |
7281 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 7282 | } |
16cb7fec | 7283 | } |
7739537f | 7284 | break; |
0d5530d9 | 7285 | |
7286 | case BUILTIN_LEN: | |
132ed071 | 7287 | case BUILTIN_CAP: |
0d5530d9 | 7288 | Expression_list::iterator pa = this->args()->begin(); |
7289 | if (!(*pa)->is_variable() | |
7290 | && ((*pa)->type()->map_type() != NULL | |
7291 | || (*pa)->type()->channel_type() != NULL)) | |
7292 | { | |
7293 | Temporary_statement* temp = | |
7294 | Statement::make_temporary(NULL, *pa, loc); | |
7295 | inserter->insert(temp); | |
7296 | *pa = Expression::make_temporary_reference(temp, loc); | |
7297 | } | |
35a54f17 | 7298 | } |
16cb7fec | 7299 | |
35a54f17 | 7300 | return this; |
7301 | } | |
7302 | ||
a9182619 | 7303 | // Lower a make expression. |
7304 | ||
7305 | Expression* | |
7306 | Builtin_call_expression::lower_make() | |
7307 | { | |
b13c66cd | 7308 | Location loc = this->location(); |
a9182619 | 7309 | |
7310 | const Expression_list* args = this->args(); | |
7311 | if (args == NULL || args->size() < 1) | |
7312 | { | |
7313 | this->report_error(_("not enough arguments")); | |
7314 | return Expression::make_error(this->location()); | |
7315 | } | |
7316 | ||
7317 | Expression_list::const_iterator parg = args->begin(); | |
7318 | ||
7319 | Expression* first_arg = *parg; | |
7320 | if (!first_arg->is_type_expression()) | |
7321 | { | |
631d5788 | 7322 | go_error_at(first_arg->location(), "expected type"); |
a9182619 | 7323 | this->set_is_error(); |
7324 | return Expression::make_error(this->location()); | |
7325 | } | |
7326 | Type* type = first_arg->type(); | |
7327 | ||
7328 | bool is_slice = false; | |
7329 | bool is_map = false; | |
7330 | bool is_chan = false; | |
411eb89e | 7331 | if (type->is_slice_type()) |
a9182619 | 7332 | is_slice = true; |
7333 | else if (type->map_type() != NULL) | |
7334 | is_map = true; | |
7335 | else if (type->channel_type() != NULL) | |
7336 | is_chan = true; | |
7337 | else | |
7338 | { | |
7339 | this->report_error(_("invalid type for make function")); | |
7340 | return Expression::make_error(this->location()); | |
7341 | } | |
7342 | ||
ac84c822 | 7343 | bool have_big_args = false; |
7344 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
7345 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
7346 | ||
f6bc81e6 | 7347 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7348 | ||
a9182619 | 7349 | ++parg; |
7350 | Expression* len_arg; | |
7351 | if (parg == args->end()) | |
7352 | { | |
7353 | if (is_slice) | |
7354 | { | |
7355 | this->report_error(_("length required when allocating a slice")); | |
7356 | return Expression::make_error(this->location()); | |
7357 | } | |
e67508fa | 7358 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
a9182619 | 7359 | } |
7360 | else | |
7361 | { | |
7362 | len_arg = *parg; | |
f6bc81e6 | 7363 | len_arg->determine_type(&int_context); |
1ad00fd4 | 7364 | if (!this->check_int_value(len_arg, true)) |
7365 | return Expression::make_error(this->location()); | |
ac84c822 | 7366 | if (len_arg->type()->integer_type() != NULL |
7367 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
7368 | have_big_args = true; | |
a9182619 | 7369 | ++parg; |
7370 | } | |
7371 | ||
7372 | Expression* cap_arg = NULL; | |
7373 | if (is_slice && parg != args->end()) | |
7374 | { | |
7375 | cap_arg = *parg; | |
f6bc81e6 | 7376 | cap_arg->determine_type(&int_context); |
1ad00fd4 | 7377 | if (!this->check_int_value(cap_arg, false)) |
7378 | return Expression::make_error(this->location()); | |
7379 | ||
7380 | Numeric_constant nclen; | |
7381 | Numeric_constant nccap; | |
7382 | unsigned long vlen; | |
7383 | unsigned long vcap; | |
7384 | if (len_arg->numeric_constant_value(&nclen) | |
7385 | && cap_arg->numeric_constant_value(&nccap) | |
7386 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7387 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7388 | && vlen > vcap) | |
a9182619 | 7389 | { |
1ad00fd4 | 7390 | this->report_error(_("len larger than cap")); |
a9182619 | 7391 | return Expression::make_error(this->location()); |
7392 | } | |
1ad00fd4 | 7393 | |
ac84c822 | 7394 | if (cap_arg->type()->integer_type() != NULL |
7395 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
7396 | have_big_args = true; | |
a9182619 | 7397 | ++parg; |
7398 | } | |
7399 | ||
7400 | if (parg != args->end()) | |
7401 | { | |
7402 | this->report_error(_("too many arguments to make")); | |
7403 | return Expression::make_error(this->location()); | |
7404 | } | |
7405 | ||
b13c66cd | 7406 | Location type_loc = first_arg->location(); |
0d5530d9 | 7407 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); |
a9182619 | 7408 | |
7409 | Expression* call; | |
7410 | if (is_slice) | |
7411 | { | |
7412 | if (cap_arg == NULL) | |
ac84c822 | 7413 | call = Runtime::make_call((have_big_args |
7414 | ? Runtime::MAKESLICE1BIG | |
7415 | : Runtime::MAKESLICE1), | |
7416 | loc, 2, type_arg, len_arg); | |
a9182619 | 7417 | else |
ac84c822 | 7418 | call = Runtime::make_call((have_big_args |
7419 | ? Runtime::MAKESLICE2BIG | |
7420 | : Runtime::MAKESLICE2), | |
7421 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7422 | } |
7423 | else if (is_map) | |
0d5530d9 | 7424 | call = Runtime::make_call(Runtime::MAKEMAP, loc, 4, type_arg, len_arg, |
7425 | Expression::make_nil(loc), | |
7426 | Expression::make_nil(loc)); | |
a9182619 | 7427 | else if (is_chan) |
132ed071 | 7428 | call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg); |
a9182619 | 7429 | else |
7430 | go_unreachable(); | |
7431 | ||
7432 | return Expression::make_unsafe_cast(type, call, loc); | |
7433 | } | |
7434 | ||
7435 | // Return whether an expression has an integer value. Report an error | |
7436 | // if not. This is used when handling calls to the predeclared make | |
7437 | // function. | |
7438 | ||
7439 | bool | |
1ad00fd4 | 7440 | Builtin_call_expression::check_int_value(Expression* e, bool is_length) |
a9182619 | 7441 | { |
0c77715b | 7442 | Numeric_constant nc; |
1ad00fd4 | 7443 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7444 | { |
1ad00fd4 | 7445 | unsigned long v; |
7446 | switch (nc.to_unsigned_long(&v)) | |
7447 | { | |
7448 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7449 | break; |
1ad00fd4 | 7450 | case Numeric_constant::NC_UL_NOTINT: |
631d5788 | 7451 | go_error_at(e->location(), "non-integer %s argument to make", |
7452 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7453 | return false; |
7454 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 7455 | go_error_at(e->location(), "negative %s argument to make", |
7456 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7457 | return false; |
7458 | case Numeric_constant::NC_UL_BIG: | |
7459 | // We don't want to give a compile-time error for a 64-bit | |
7460 | // value on a 32-bit target. | |
1b10c5e7 | 7461 | break; |
1ad00fd4 | 7462 | } |
1b10c5e7 | 7463 | |
7464 | mpz_t val; | |
7465 | if (!nc.to_int(&val)) | |
7466 | go_unreachable(); | |
7467 | int bits = mpz_sizeinbase(val, 2); | |
7468 | mpz_clear(val); | |
7469 | Type* int_type = Type::lookup_integer_type("int"); | |
7470 | if (bits >= int_type->integer_type()->bits()) | |
7471 | { | |
631d5788 | 7472 | go_error_at(e->location(), "%s argument too large for make", |
7473 | is_length ? "len" : "cap"); | |
1b10c5e7 | 7474 | return false; |
7475 | } | |
7476 | ||
7477 | return true; | |
a9182619 | 7478 | } |
7479 | ||
1ad00fd4 | 7480 | if (e->type()->integer_type() != NULL) |
7481 | return true; | |
7482 | ||
631d5788 | 7483 | go_error_at(e->location(), "non-integer %s argument to make", |
7484 | is_length ? "len" : "cap"); | |
a9182619 | 7485 | return false; |
7486 | } | |
7487 | ||
e440a328 | 7488 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7489 | // the argument. We need to map complex64 to float32 and complex128 |
7490 | // to float64, so it has to be done by name. This returns NULL if it | |
7491 | // can't figure out the type. | |
e440a328 | 7492 | |
7493 | Type* | |
7494 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7495 | { | |
7496 | if (arg_type == NULL || arg_type->is_abstract()) | |
7497 | return NULL; | |
7498 | Named_type* nt = arg_type->named_type(); | |
7499 | if (nt == NULL) | |
7500 | return NULL; | |
7501 | while (nt->real_type()->named_type() != NULL) | |
7502 | nt = nt->real_type()->named_type(); | |
48080209 | 7503 | if (nt->name() == "complex64") |
e440a328 | 7504 | return Type::lookup_float_type("float32"); |
7505 | else if (nt->name() == "complex128") | |
7506 | return Type::lookup_float_type("float64"); | |
7507 | else | |
7508 | return NULL; | |
7509 | } | |
7510 | ||
48080209 | 7511 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7512 | // argments. Like real_imag_type, we have to map by name. |
7513 | ||
7514 | Type* | |
48080209 | 7515 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7516 | { |
7517 | if (arg_type == NULL || arg_type->is_abstract()) | |
7518 | return NULL; | |
7519 | Named_type* nt = arg_type->named_type(); | |
7520 | if (nt == NULL) | |
7521 | return NULL; | |
7522 | while (nt->real_type()->named_type() != NULL) | |
7523 | nt = nt->real_type()->named_type(); | |
48080209 | 7524 | if (nt->name() == "float32") |
e440a328 | 7525 | return Type::lookup_complex_type("complex64"); |
7526 | else if (nt->name() == "float64") | |
7527 | return Type::lookup_complex_type("complex128"); | |
7528 | else | |
7529 | return NULL; | |
7530 | } | |
7531 | ||
7532 | // Return a single argument, or NULL if there isn't one. | |
7533 | ||
7534 | Expression* | |
7535 | Builtin_call_expression::one_arg() const | |
7536 | { | |
7537 | const Expression_list* args = this->args(); | |
aa615cb3 | 7538 | if (args == NULL || args->size() != 1) |
e440a328 | 7539 | return NULL; |
7540 | return args->front(); | |
7541 | } | |
7542 | ||
83921647 | 7543 | // A traversal class which looks for a call or receive expression. |
7544 | ||
7545 | class Find_call_expression : public Traverse | |
7546 | { | |
7547 | public: | |
7548 | Find_call_expression() | |
7549 | : Traverse(traverse_expressions), | |
7550 | found_(false) | |
7551 | { } | |
7552 | ||
7553 | int | |
7554 | expression(Expression**); | |
7555 | ||
7556 | bool | |
7557 | found() | |
7558 | { return this->found_; } | |
7559 | ||
7560 | private: | |
7561 | bool found_; | |
7562 | }; | |
7563 | ||
7564 | int | |
7565 | Find_call_expression::expression(Expression** pexpr) | |
7566 | { | |
7567 | if ((*pexpr)->call_expression() != NULL | |
7568 | || (*pexpr)->receive_expression() != NULL) | |
7569 | { | |
7570 | this->found_ = true; | |
7571 | return TRAVERSE_EXIT; | |
7572 | } | |
7573 | return TRAVERSE_CONTINUE; | |
7574 | } | |
7575 | ||
7576 | // Return whether this is constant: len of a string constant, or len | |
7577 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7578 | // unsafe.Alignof. | |
e440a328 | 7579 | |
7580 | bool | |
7581 | Builtin_call_expression::do_is_constant() const | |
7582 | { | |
12e69faa | 7583 | if (this->is_error_expression()) |
7584 | return true; | |
e440a328 | 7585 | switch (this->code_) |
7586 | { | |
7587 | case BUILTIN_LEN: | |
7588 | case BUILTIN_CAP: | |
7589 | { | |
0f914071 | 7590 | if (this->seen_) |
7591 | return false; | |
7592 | ||
e440a328 | 7593 | Expression* arg = this->one_arg(); |
7594 | if (arg == NULL) | |
7595 | return false; | |
7596 | Type* arg_type = arg->type(); | |
7597 | ||
7598 | if (arg_type->points_to() != NULL | |
7599 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7600 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7601 | arg_type = arg_type->points_to(); |
7602 | ||
83921647 | 7603 | // The len and cap functions are only constant if there are no |
7604 | // function calls or channel operations in the arguments. | |
7605 | // Otherwise we have to make the call. | |
7606 | if (!arg->is_constant()) | |
7607 | { | |
7608 | Find_call_expression find_call; | |
7609 | Expression::traverse(&arg, &find_call); | |
7610 | if (find_call.found()) | |
7611 | return false; | |
7612 | } | |
7613 | ||
e440a328 | 7614 | if (arg_type->array_type() != NULL |
7615 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7616 | return true; |
e440a328 | 7617 | |
7618 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7619 | { |
7620 | this->seen_ = true; | |
7621 | bool ret = arg->is_constant(); | |
7622 | this->seen_ = false; | |
7623 | return ret; | |
7624 | } | |
e440a328 | 7625 | } |
7626 | break; | |
7627 | ||
7628 | case BUILTIN_SIZEOF: | |
7629 | case BUILTIN_ALIGNOF: | |
7630 | return this->one_arg() != NULL; | |
7631 | ||
7632 | case BUILTIN_OFFSETOF: | |
7633 | { | |
7634 | Expression* arg = this->one_arg(); | |
7635 | if (arg == NULL) | |
7636 | return false; | |
7637 | return arg->field_reference_expression() != NULL; | |
7638 | } | |
7639 | ||
48080209 | 7640 | case BUILTIN_COMPLEX: |
e440a328 | 7641 | { |
7642 | const Expression_list* args = this->args(); | |
7643 | if (args != NULL && args->size() == 2) | |
7644 | return args->front()->is_constant() && args->back()->is_constant(); | |
7645 | } | |
7646 | break; | |
7647 | ||
7648 | case BUILTIN_REAL: | |
7649 | case BUILTIN_IMAG: | |
7650 | { | |
7651 | Expression* arg = this->one_arg(); | |
7652 | return arg != NULL && arg->is_constant(); | |
7653 | } | |
7654 | ||
7655 | default: | |
7656 | break; | |
7657 | } | |
7658 | ||
7659 | return false; | |
7660 | } | |
7661 | ||
0c77715b | 7662 | // Return a numeric constant if possible. |
e440a328 | 7663 | |
7664 | bool | |
0c77715b | 7665 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7666 | { |
7667 | if (this->code_ == BUILTIN_LEN | |
7668 | || this->code_ == BUILTIN_CAP) | |
7669 | { | |
7670 | Expression* arg = this->one_arg(); | |
7671 | if (arg == NULL) | |
7672 | return false; | |
7673 | Type* arg_type = arg->type(); | |
7674 | ||
7675 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7676 | { | |
7677 | std::string sval; | |
7678 | if (arg->string_constant_value(&sval)) | |
7679 | { | |
0c77715b | 7680 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7681 | sval.length()); | |
e440a328 | 7682 | return true; |
7683 | } | |
7684 | } | |
7685 | ||
7686 | if (arg_type->points_to() != NULL | |
7687 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7688 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7689 | arg_type = arg_type->points_to(); |
7690 | ||
7691 | if (arg_type->array_type() != NULL | |
7692 | && arg_type->array_type()->length() != NULL) | |
7693 | { | |
0f914071 | 7694 | if (this->seen_) |
7695 | return false; | |
e440a328 | 7696 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7697 | this->seen_ = true; |
0c77715b | 7698 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7699 | this->seen_ = false; |
7700 | if (r) | |
e440a328 | 7701 | { |
0c77715b | 7702 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7703 | this->location())) | |
7704 | r = false; | |
e440a328 | 7705 | } |
0c77715b | 7706 | return r; |
e440a328 | 7707 | } |
7708 | } | |
7709 | else if (this->code_ == BUILTIN_SIZEOF | |
7710 | || this->code_ == BUILTIN_ALIGNOF) | |
7711 | { | |
7712 | Expression* arg = this->one_arg(); | |
7713 | if (arg == NULL) | |
7714 | return false; | |
7715 | Type* arg_type = arg->type(); | |
5c13bd80 | 7716 | if (arg_type->is_error()) |
e440a328 | 7717 | return false; |
7718 | if (arg_type->is_abstract()) | |
7719 | return false; | |
2c809f8f | 7720 | if (this->seen_) |
7721 | return false; | |
927a01eb | 7722 | |
3f378015 | 7723 | int64_t ret; |
e440a328 | 7724 | if (this->code_ == BUILTIN_SIZEOF) |
7725 | { | |
2c809f8f | 7726 | this->seen_ = true; |
7727 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
7728 | this->seen_ = false; | |
7729 | if (!ok) | |
e440a328 | 7730 | return false; |
7731 | } | |
7732 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7733 | { | |
2c809f8f | 7734 | bool ok; |
7735 | this->seen_ = true; | |
637bd3af | 7736 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 7737 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 7738 | else |
e440a328 | 7739 | { |
7740 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7741 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 7742 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 7743 | } |
2c809f8f | 7744 | this->seen_ = false; |
7745 | if (!ok) | |
7746 | return false; | |
e440a328 | 7747 | } |
7748 | else | |
c3e6f413 | 7749 | go_unreachable(); |
927a01eb | 7750 | |
3f378015 | 7751 | mpz_t zval; |
7752 | set_mpz_from_int64(&zval, ret); | |
7753 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
7754 | mpz_clear(zval); | |
e440a328 | 7755 | return true; |
7756 | } | |
7757 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7758 | { | |
7759 | Expression* arg = this->one_arg(); | |
7760 | if (arg == NULL) | |
7761 | return false; | |
7762 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7763 | if (farg == NULL) | |
7764 | return false; | |
2c809f8f | 7765 | if (this->seen_) |
7766 | return false; | |
7767 | ||
3f378015 | 7768 | int64_t total_offset = 0; |
9a4bd570 | 7769 | while (true) |
7770 | { | |
7771 | Expression* struct_expr = farg->expr(); | |
7772 | Type* st = struct_expr->type(); | |
7773 | if (st->struct_type() == NULL) | |
7774 | return false; | |
7775 | if (st->named_type() != NULL) | |
7776 | st->named_type()->convert(this->gogo_); | |
3f378015 | 7777 | int64_t offset; |
2c809f8f | 7778 | this->seen_ = true; |
7779 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
7780 | farg->field_index(), | |
7781 | &offset); | |
7782 | this->seen_ = false; | |
7783 | if (!ok) | |
7784 | return false; | |
9a4bd570 | 7785 | total_offset += offset; |
7786 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
7787 | { | |
7788 | // Go up until we reach the original base. | |
7789 | farg = struct_expr->field_reference_expression(); | |
7790 | continue; | |
7791 | } | |
7792 | break; | |
7793 | } | |
3f378015 | 7794 | mpz_t zval; |
7795 | set_mpz_from_int64(&zval, total_offset); | |
7796 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
7797 | mpz_clear(zval); | |
e440a328 | 7798 | return true; |
7799 | } | |
0c77715b | 7800 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7801 | { |
7802 | Expression* arg = this->one_arg(); | |
7803 | if (arg == NULL) | |
7804 | return false; | |
7805 | ||
0c77715b | 7806 | Numeric_constant argnc; |
7807 | if (!arg->numeric_constant_value(&argnc)) | |
7808 | return false; | |
7809 | ||
fcbea5e4 | 7810 | mpc_t val; |
7811 | if (!argnc.to_complex(&val)) | |
0c77715b | 7812 | return false; |
e440a328 | 7813 | |
0c77715b | 7814 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7815 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 7816 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 7817 | else |
fcbea5e4 | 7818 | nc->set_float(type, mpc_imagref(val)); |
7819 | mpc_clear(val); | |
0c77715b | 7820 | return true; |
e440a328 | 7821 | } |
0c77715b | 7822 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7823 | { |
7824 | const Expression_list* args = this->args(); | |
7825 | if (args == NULL || args->size() != 2) | |
7826 | return false; | |
7827 | ||
0c77715b | 7828 | Numeric_constant rnc; |
7829 | if (!args->front()->numeric_constant_value(&rnc)) | |
7830 | return false; | |
7831 | Numeric_constant inc; | |
7832 | if (!args->back()->numeric_constant_value(&inc)) | |
7833 | return false; | |
7834 | ||
7835 | if (rnc.type() != NULL | |
7836 | && !rnc.type()->is_abstract() | |
7837 | && inc.type() != NULL | |
7838 | && !inc.type()->is_abstract() | |
7839 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7840 | return false; | |
7841 | ||
e440a328 | 7842 | mpfr_t r; |
0c77715b | 7843 | if (!rnc.to_float(&r)) |
7844 | return false; | |
7845 | mpfr_t i; | |
7846 | if (!inc.to_float(&i)) | |
e440a328 | 7847 | { |
7848 | mpfr_clear(r); | |
7849 | return false; | |
7850 | } | |
7851 | ||
0c77715b | 7852 | Type* arg_type = rnc.type(); |
7853 | if (arg_type == NULL || arg_type->is_abstract()) | |
7854 | arg_type = inc.type(); | |
e440a328 | 7855 | |
fcbea5e4 | 7856 | mpc_t val; |
7857 | mpc_init2(val, mpc_precision); | |
7858 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 7859 | mpfr_clear(r); |
7860 | mpfr_clear(i); | |
7861 | ||
fcbea5e4 | 7862 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7863 | nc->set_complex(type, val); | |
7864 | ||
7865 | mpc_clear(val); | |
7866 | ||
0c77715b | 7867 | return true; |
e440a328 | 7868 | } |
7869 | ||
7870 | return false; | |
7871 | } | |
7872 | ||
a7549a6a | 7873 | // Give an error if we are discarding the value of an expression which |
7874 | // should not normally be discarded. We don't give an error for | |
7875 | // discarding the value of an ordinary function call, but we do for | |
7876 | // builtin functions, purely for consistency with the gc compiler. | |
7877 | ||
4f2138d7 | 7878 | bool |
a7549a6a | 7879 | Builtin_call_expression::do_discarding_value() |
7880 | { | |
7881 | switch (this->code_) | |
7882 | { | |
7883 | case BUILTIN_INVALID: | |
7884 | default: | |
7885 | go_unreachable(); | |
7886 | ||
7887 | case BUILTIN_APPEND: | |
7888 | case BUILTIN_CAP: | |
7889 | case BUILTIN_COMPLEX: | |
7890 | case BUILTIN_IMAG: | |
7891 | case BUILTIN_LEN: | |
7892 | case BUILTIN_MAKE: | |
7893 | case BUILTIN_NEW: | |
7894 | case BUILTIN_REAL: | |
7895 | case BUILTIN_ALIGNOF: | |
7896 | case BUILTIN_OFFSETOF: | |
7897 | case BUILTIN_SIZEOF: | |
7898 | this->unused_value_error(); | |
4f2138d7 | 7899 | return false; |
a7549a6a | 7900 | |
7901 | case BUILTIN_CLOSE: | |
7902 | case BUILTIN_COPY: | |
1cce762f | 7903 | case BUILTIN_DELETE: |
a7549a6a | 7904 | case BUILTIN_PANIC: |
7905 | case BUILTIN_PRINT: | |
7906 | case BUILTIN_PRINTLN: | |
7907 | case BUILTIN_RECOVER: | |
4f2138d7 | 7908 | return true; |
a7549a6a | 7909 | } |
7910 | } | |
7911 | ||
e440a328 | 7912 | // Return the type. |
7913 | ||
7914 | Type* | |
7915 | Builtin_call_expression::do_type() | |
7916 | { | |
79651b1f | 7917 | if (this->is_error_expression()) |
7918 | return Type::make_error_type(); | |
e440a328 | 7919 | switch (this->code_) |
7920 | { | |
7921 | case BUILTIN_INVALID: | |
7922 | default: | |
79651b1f | 7923 | return Type::make_error_type(); |
e440a328 | 7924 | |
7925 | case BUILTIN_NEW: | |
7926 | case BUILTIN_MAKE: | |
7927 | { | |
7928 | const Expression_list* args = this->args(); | |
7929 | if (args == NULL || args->empty()) | |
7930 | return Type::make_error_type(); | |
7931 | return Type::make_pointer_type(args->front()->type()); | |
7932 | } | |
7933 | ||
7934 | case BUILTIN_CAP: | |
7935 | case BUILTIN_COPY: | |
7936 | case BUILTIN_LEN: | |
7ba86326 | 7937 | return Type::lookup_integer_type("int"); |
7938 | ||
e440a328 | 7939 | case BUILTIN_ALIGNOF: |
7940 | case BUILTIN_OFFSETOF: | |
7941 | case BUILTIN_SIZEOF: | |
7ba86326 | 7942 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7943 | |
7944 | case BUILTIN_CLOSE: | |
1cce762f | 7945 | case BUILTIN_DELETE: |
e440a328 | 7946 | case BUILTIN_PANIC: |
7947 | case BUILTIN_PRINT: | |
7948 | case BUILTIN_PRINTLN: | |
7949 | return Type::make_void_type(); | |
7950 | ||
e440a328 | 7951 | case BUILTIN_RECOVER: |
823c7e3d | 7952 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7953 | |
7954 | case BUILTIN_APPEND: | |
7955 | { | |
7956 | const Expression_list* args = this->args(); | |
7957 | if (args == NULL || args->empty()) | |
7958 | return Type::make_error_type(); | |
3ff4863b | 7959 | Type *ret = args->front()->type(); |
7960 | if (!ret->is_slice_type()) | |
7961 | return Type::make_error_type(); | |
7962 | return ret; | |
e440a328 | 7963 | } |
7964 | ||
7965 | case BUILTIN_REAL: | |
7966 | case BUILTIN_IMAG: | |
7967 | { | |
7968 | Expression* arg = this->one_arg(); | |
7969 | if (arg == NULL) | |
7970 | return Type::make_error_type(); | |
7971 | Type* t = arg->type(); | |
7972 | if (t->is_abstract()) | |
7973 | t = t->make_non_abstract_type(); | |
7974 | t = Builtin_call_expression::real_imag_type(t); | |
7975 | if (t == NULL) | |
7976 | t = Type::make_error_type(); | |
7977 | return t; | |
7978 | } | |
7979 | ||
48080209 | 7980 | case BUILTIN_COMPLEX: |
e440a328 | 7981 | { |
7982 | const Expression_list* args = this->args(); | |
7983 | if (args == NULL || args->size() != 2) | |
7984 | return Type::make_error_type(); | |
7985 | Type* t = args->front()->type(); | |
7986 | if (t->is_abstract()) | |
7987 | { | |
7988 | t = args->back()->type(); | |
7989 | if (t->is_abstract()) | |
7990 | t = t->make_non_abstract_type(); | |
7991 | } | |
48080209 | 7992 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7993 | if (t == NULL) |
7994 | t = Type::make_error_type(); | |
7995 | return t; | |
7996 | } | |
7997 | } | |
7998 | } | |
7999 | ||
8000 | // Determine the type. | |
8001 | ||
8002 | void | |
8003 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
8004 | { | |
fb94b0ca | 8005 | if (!this->determining_types()) |
8006 | return; | |
8007 | ||
e440a328 | 8008 | this->fn()->determine_type_no_context(); |
8009 | ||
8010 | const Expression_list* args = this->args(); | |
8011 | ||
8012 | bool is_print; | |
8013 | Type* arg_type = NULL; | |
8014 | switch (this->code_) | |
8015 | { | |
8016 | case BUILTIN_PRINT: | |
8017 | case BUILTIN_PRINTLN: | |
8018 | // Do not force a large integer constant to "int". | |
8019 | is_print = true; | |
8020 | break; | |
8021 | ||
8022 | case BUILTIN_REAL: | |
8023 | case BUILTIN_IMAG: | |
48080209 | 8024 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8025 | if (arg_type == NULL) |
8026 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8027 | is_print = false; |
8028 | break; | |
8029 | ||
48080209 | 8030 | case BUILTIN_COMPLEX: |
e440a328 | 8031 | { |
48080209 | 8032 | // For the complex function the type of one operand can |
e440a328 | 8033 | // determine the type of the other, as in a binary expression. |
8034 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8035 | if (arg_type == NULL) |
8036 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8037 | if (args != NULL && args->size() == 2) |
8038 | { | |
8039 | Type* t1 = args->front()->type(); | |
c849bb59 | 8040 | Type* t2 = args->back()->type(); |
e440a328 | 8041 | if (!t1->is_abstract()) |
8042 | arg_type = t1; | |
8043 | else if (!t2->is_abstract()) | |
8044 | arg_type = t2; | |
8045 | } | |
8046 | is_print = false; | |
8047 | } | |
8048 | break; | |
8049 | ||
8050 | default: | |
8051 | is_print = false; | |
8052 | break; | |
8053 | } | |
8054 | ||
8055 | if (args != NULL) | |
8056 | { | |
8057 | for (Expression_list::const_iterator pa = args->begin(); | |
8058 | pa != args->end(); | |
8059 | ++pa) | |
8060 | { | |
8061 | Type_context subcontext; | |
8062 | subcontext.type = arg_type; | |
8063 | ||
8064 | if (is_print) | |
8065 | { | |
8066 | // We want to print large constants, we so can't just | |
8067 | // use the appropriate nonabstract type. Use uint64 for | |
8068 | // an integer if we know it is nonnegative, otherwise | |
8069 | // use int64 for a integer, otherwise use float64 for a | |
8070 | // float or complex128 for a complex. | |
8071 | Type* want_type = NULL; | |
8072 | Type* atype = (*pa)->type(); | |
8073 | if (atype->is_abstract()) | |
8074 | { | |
8075 | if (atype->integer_type() != NULL) | |
8076 | { | |
0c77715b | 8077 | Numeric_constant nc; |
8078 | if (this->numeric_constant_value(&nc)) | |
8079 | { | |
8080 | mpz_t val; | |
8081 | if (nc.to_int(&val)) | |
8082 | { | |
8083 | if (mpz_sgn(val) >= 0) | |
8084 | want_type = Type::lookup_integer_type("uint64"); | |
8085 | mpz_clear(val); | |
8086 | } | |
8087 | } | |
8088 | if (want_type == NULL) | |
e440a328 | 8089 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8090 | } |
8091 | else if (atype->float_type() != NULL) | |
8092 | want_type = Type::lookup_float_type("float64"); | |
8093 | else if (atype->complex_type() != NULL) | |
8094 | want_type = Type::lookup_complex_type("complex128"); | |
8095 | else if (atype->is_abstract_string_type()) | |
8096 | want_type = Type::lookup_string_type(); | |
8097 | else if (atype->is_abstract_boolean_type()) | |
8098 | want_type = Type::lookup_bool_type(); | |
8099 | else | |
c3e6f413 | 8100 | go_unreachable(); |
e440a328 | 8101 | subcontext.type = want_type; |
8102 | } | |
8103 | } | |
8104 | ||
8105 | (*pa)->determine_type(&subcontext); | |
8106 | } | |
8107 | } | |
8108 | } | |
8109 | ||
8110 | // If there is exactly one argument, return true. Otherwise give an | |
8111 | // error message and return false. | |
8112 | ||
8113 | bool | |
8114 | Builtin_call_expression::check_one_arg() | |
8115 | { | |
8116 | const Expression_list* args = this->args(); | |
8117 | if (args == NULL || args->size() < 1) | |
8118 | { | |
8119 | this->report_error(_("not enough arguments")); | |
8120 | return false; | |
8121 | } | |
8122 | else if (args->size() > 1) | |
8123 | { | |
8124 | this->report_error(_("too many arguments")); | |
8125 | return false; | |
8126 | } | |
8127 | if (args->front()->is_error_expression() | |
5c13bd80 | 8128 | || args->front()->type()->is_error()) |
e440a328 | 8129 | { |
8130 | this->set_is_error(); | |
8131 | return false; | |
8132 | } | |
8133 | return true; | |
8134 | } | |
8135 | ||
8136 | // Check argument types for a builtin function. | |
8137 | ||
8138 | void | |
8139 | Builtin_call_expression::do_check_types(Gogo*) | |
8140 | { | |
375646ea | 8141 | if (this->is_error_expression()) |
8142 | return; | |
e440a328 | 8143 | switch (this->code_) |
8144 | { | |
8145 | case BUILTIN_INVALID: | |
8146 | case BUILTIN_NEW: | |
8147 | case BUILTIN_MAKE: | |
cd238b8d | 8148 | case BUILTIN_DELETE: |
e440a328 | 8149 | return; |
8150 | ||
8151 | case BUILTIN_LEN: | |
8152 | case BUILTIN_CAP: | |
8153 | { | |
8154 | // The single argument may be either a string or an array or a | |
8155 | // map or a channel, or a pointer to a closed array. | |
8156 | if (this->check_one_arg()) | |
8157 | { | |
8158 | Type* arg_type = this->one_arg()->type(); | |
8159 | if (arg_type->points_to() != NULL | |
8160 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8161 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8162 | arg_type = arg_type->points_to(); |
8163 | if (this->code_ == BUILTIN_CAP) | |
8164 | { | |
5c13bd80 | 8165 | if (!arg_type->is_error() |
e440a328 | 8166 | && arg_type->array_type() == NULL |
8167 | && arg_type->channel_type() == NULL) | |
8168 | this->report_error(_("argument must be array or slice " | |
8169 | "or channel")); | |
8170 | } | |
8171 | else | |
8172 | { | |
5c13bd80 | 8173 | if (!arg_type->is_error() |
e440a328 | 8174 | && !arg_type->is_string_type() |
8175 | && arg_type->array_type() == NULL | |
8176 | && arg_type->map_type() == NULL | |
8177 | && arg_type->channel_type() == NULL) | |
8178 | this->report_error(_("argument must be string or " | |
8179 | "array or slice or map or channel")); | |
8180 | } | |
8181 | } | |
8182 | } | |
8183 | break; | |
8184 | ||
8185 | case BUILTIN_PRINT: | |
8186 | case BUILTIN_PRINTLN: | |
8187 | { | |
8188 | const Expression_list* args = this->args(); | |
8189 | if (args == NULL) | |
8190 | { | |
8191 | if (this->code_ == BUILTIN_PRINT) | |
631d5788 | 8192 | go_warning_at(this->location(), 0, |
e440a328 | 8193 | "no arguments for builtin function %<%s%>", |
8194 | (this->code_ == BUILTIN_PRINT | |
8195 | ? "print" | |
8196 | : "println")); | |
8197 | } | |
8198 | else | |
8199 | { | |
8200 | for (Expression_list::const_iterator p = args->begin(); | |
8201 | p != args->end(); | |
8202 | ++p) | |
8203 | { | |
8204 | Type* type = (*p)->type(); | |
5c13bd80 | 8205 | if (type->is_error() |
e440a328 | 8206 | || type->is_string_type() |
8207 | || type->integer_type() != NULL | |
8208 | || type->float_type() != NULL | |
8209 | || type->complex_type() != NULL | |
8210 | || type->is_boolean_type() | |
8211 | || type->points_to() != NULL | |
8212 | || type->interface_type() != NULL | |
8213 | || type->channel_type() != NULL | |
8214 | || type->map_type() != NULL | |
8215 | || type->function_type() != NULL | |
411eb89e | 8216 | || type->is_slice_type()) |
e440a328 | 8217 | ; |
acf8e158 | 8218 | else if ((*p)->is_type_expression()) |
8219 | { | |
8220 | // If this is a type expression it's going to give | |
8221 | // an error anyhow, so we don't need one here. | |
8222 | } | |
e440a328 | 8223 | else |
8224 | this->report_error(_("unsupported argument type to " | |
8225 | "builtin function")); | |
8226 | } | |
8227 | } | |
8228 | } | |
8229 | break; | |
8230 | ||
8231 | case BUILTIN_CLOSE: | |
e440a328 | 8232 | if (this->check_one_arg()) |
8233 | { | |
8234 | if (this->one_arg()->type()->channel_type() == NULL) | |
8235 | this->report_error(_("argument must be channel")); | |
5202d986 | 8236 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8237 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8238 | } |
8239 | break; | |
8240 | ||
8241 | case BUILTIN_PANIC: | |
8242 | case BUILTIN_SIZEOF: | |
8243 | case BUILTIN_ALIGNOF: | |
8244 | this->check_one_arg(); | |
8245 | break; | |
8246 | ||
8247 | case BUILTIN_RECOVER: | |
6334270b | 8248 | if (this->args() != NULL |
8249 | && !this->args()->empty() | |
8250 | && !this->recover_arg_is_set_) | |
e440a328 | 8251 | this->report_error(_("too many arguments")); |
8252 | break; | |
8253 | ||
8254 | case BUILTIN_OFFSETOF: | |
8255 | if (this->check_one_arg()) | |
8256 | { | |
8257 | Expression* arg = this->one_arg(); | |
8258 | if (arg->field_reference_expression() == NULL) | |
8259 | this->report_error(_("argument must be a field reference")); | |
8260 | } | |
8261 | break; | |
8262 | ||
8263 | case BUILTIN_COPY: | |
8264 | { | |
8265 | const Expression_list* args = this->args(); | |
8266 | if (args == NULL || args->size() < 2) | |
8267 | { | |
8268 | this->report_error(_("not enough arguments")); | |
8269 | break; | |
8270 | } | |
8271 | else if (args->size() > 2) | |
8272 | { | |
8273 | this->report_error(_("too many arguments")); | |
8274 | break; | |
8275 | } | |
8276 | Type* arg1_type = args->front()->type(); | |
8277 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8278 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 8279 | { |
8280 | this->set_is_error(); | |
8281 | break; | |
8282 | } | |
e440a328 | 8283 | |
8284 | Type* e1; | |
411eb89e | 8285 | if (arg1_type->is_slice_type()) |
e440a328 | 8286 | e1 = arg1_type->array_type()->element_type(); |
8287 | else | |
8288 | { | |
8289 | this->report_error(_("left argument must be a slice")); | |
8290 | break; | |
8291 | } | |
8292 | ||
411eb89e | 8293 | if (arg2_type->is_slice_type()) |
60963afd | 8294 | { |
8295 | Type* e2 = arg2_type->array_type()->element_type(); | |
8296 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8297 | this->report_error(_("element types must be the same")); | |
8298 | } | |
e440a328 | 8299 | else if (arg2_type->is_string_type()) |
e440a328 | 8300 | { |
60963afd | 8301 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8302 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8303 | } |
60963afd | 8304 | else |
8305 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8306 | } |
8307 | break; | |
8308 | ||
8309 | case BUILTIN_APPEND: | |
8310 | { | |
8311 | const Expression_list* args = this->args(); | |
b0d311a1 | 8312 | if (args == NULL || args->size() < 2) |
e440a328 | 8313 | { |
8314 | this->report_error(_("not enough arguments")); | |
8315 | break; | |
8316 | } | |
0b7755ec | 8317 | if (args->size() > 2) |
8318 | { | |
8319 | this->report_error(_("too many arguments")); | |
8320 | break; | |
8321 | } | |
cd238b8d | 8322 | if (args->front()->type()->is_error() |
8323 | || args->back()->type()->is_error()) | |
6bebb39d | 8324 | { |
8325 | this->set_is_error(); | |
8326 | break; | |
8327 | } | |
cd238b8d | 8328 | |
8329 | Array_type* at = args->front()->type()->array_type(); | |
8330 | Type* e = at->element_type(); | |
4fd4fcf4 | 8331 | |
8332 | // The language permits appending a string to a []byte, as a | |
8333 | // special case. | |
8334 | if (args->back()->type()->is_string_type()) | |
8335 | { | |
60963afd | 8336 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 8337 | break; |
8338 | } | |
8339 | ||
19fd40c3 | 8340 | // The language says that the second argument must be |
8341 | // assignable to a slice of the element type of the first | |
8342 | // argument. We already know the first argument is a slice | |
8343 | // type. | |
cd238b8d | 8344 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 8345 | std::string reason; |
19fd40c3 | 8346 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 8347 | { |
8348 | if (reason.empty()) | |
19fd40c3 | 8349 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 8350 | else |
8351 | { | |
631d5788 | 8352 | go_error_at(this->location(), |
8353 | "argument 2 has invalid type (%s)", | |
8354 | reason.c_str()); | |
e440a328 | 8355 | this->set_is_error(); |
8356 | } | |
8357 | } | |
8358 | break; | |
8359 | } | |
8360 | ||
8361 | case BUILTIN_REAL: | |
8362 | case BUILTIN_IMAG: | |
8363 | if (this->check_one_arg()) | |
8364 | { | |
8365 | if (this->one_arg()->type()->complex_type() == NULL) | |
8366 | this->report_error(_("argument must have complex type")); | |
8367 | } | |
8368 | break; | |
8369 | ||
48080209 | 8370 | case BUILTIN_COMPLEX: |
e440a328 | 8371 | { |
8372 | const Expression_list* args = this->args(); | |
8373 | if (args == NULL || args->size() < 2) | |
8374 | this->report_error(_("not enough arguments")); | |
8375 | else if (args->size() > 2) | |
8376 | this->report_error(_("too many arguments")); | |
8377 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8378 | || args->front()->type()->is_error() |
e440a328 | 8379 | || args->back()->is_error_expression() |
5c13bd80 | 8380 | || args->back()->type()->is_error()) |
e440a328 | 8381 | this->set_is_error(); |
8382 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8383 | args->back()->type(), true, NULL)) |
48080209 | 8384 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8385 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8386 | this->report_error(_("complex arguments must have " |
e440a328 | 8387 | "floating-point type")); |
8388 | } | |
8389 | break; | |
8390 | ||
8391 | default: | |
c3e6f413 | 8392 | go_unreachable(); |
e440a328 | 8393 | } |
8394 | } | |
8395 | ||
72666aed | 8396 | Expression* |
8397 | Builtin_call_expression::do_copy() | |
8398 | { | |
8399 | Call_expression* bce = | |
8400 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8401 | (this->args() == NULL |
8402 | ? NULL | |
8403 | : this->args()->copy()), | |
72666aed | 8404 | this->is_varargs(), |
8405 | this->location()); | |
8406 | ||
8407 | if (this->varargs_are_lowered()) | |
8408 | bce->set_varargs_are_lowered(); | |
8409 | return bce; | |
8410 | } | |
8411 | ||
ea664253 | 8412 | // Return the backend representation for a builtin function. |
e440a328 | 8413 | |
ea664253 | 8414 | Bexpression* |
8415 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8416 | { |
8417 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8418 | Location location = this->location(); |
a0d8874e | 8419 | |
8420 | if (this->is_erroneous_call()) | |
8421 | { | |
8422 | go_assert(saw_errors()); | |
8423 | return gogo->backend()->error_expression(); | |
8424 | } | |
8425 | ||
e440a328 | 8426 | switch (this->code_) |
8427 | { | |
8428 | case BUILTIN_INVALID: | |
8429 | case BUILTIN_NEW: | |
8430 | case BUILTIN_MAKE: | |
c3e6f413 | 8431 | go_unreachable(); |
e440a328 | 8432 | |
8433 | case BUILTIN_LEN: | |
8434 | case BUILTIN_CAP: | |
8435 | { | |
8436 | const Expression_list* args = this->args(); | |
c484d925 | 8437 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8438 | Expression* arg = args->front(); |
e440a328 | 8439 | Type* arg_type = arg->type(); |
0f914071 | 8440 | |
8441 | if (this->seen_) | |
8442 | { | |
c484d925 | 8443 | go_assert(saw_errors()); |
ea664253 | 8444 | return context->backend()->error_expression(); |
0f914071 | 8445 | } |
8446 | this->seen_ = true; | |
0f914071 | 8447 | this->seen_ = false; |
e440a328 | 8448 | if (arg_type->points_to() != NULL) |
8449 | { | |
8450 | arg_type = arg_type->points_to(); | |
c484d925 | 8451 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8452 | && !arg_type->is_slice_type()); |
2c809f8f | 8453 | arg = Expression::make_unary(OPERATOR_MULT, arg, location); |
e440a328 | 8454 | } |
8455 | ||
1b1f2abf | 8456 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8457 | Expression* val; |
e440a328 | 8458 | if (this->code_ == BUILTIN_LEN) |
8459 | { | |
8460 | if (arg_type->is_string_type()) | |
2c809f8f | 8461 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8462 | location); | |
e440a328 | 8463 | else if (arg_type->array_type() != NULL) |
0f914071 | 8464 | { |
8465 | if (this->seen_) | |
8466 | { | |
c484d925 | 8467 | go_assert(saw_errors()); |
ea664253 | 8468 | return context->backend()->error_expression(); |
0f914071 | 8469 | } |
8470 | this->seen_ = true; | |
2c809f8f | 8471 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8472 | this->seen_ = false; |
8473 | } | |
0d5530d9 | 8474 | else if (arg_type->map_type() != NULL |
8475 | || arg_type->channel_type() != NULL) | |
8476 | { | |
8477 | // The first field is the length. If the pointer is | |
8478 | // nil, the length is zero. | |
8479 | Type* pint_type = Type::make_pointer_type(int_type); | |
8480 | arg = Expression::make_unsafe_cast(pint_type, arg, location); | |
8481 | Expression* nil = Expression::make_nil(location); | |
8482 | nil = Expression::make_cast(pint_type, nil, location); | |
8483 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8484 | arg, nil, location); | |
8485 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8486 | location); | |
8487 | Expression* indir = Expression::make_unary(OPERATOR_MULT, | |
8488 | arg, location); | |
8489 | val = Expression::make_conditional(cmp, zero, indir, location); | |
8490 | } | |
e440a328 | 8491 | else |
c3e6f413 | 8492 | go_unreachable(); |
e440a328 | 8493 | } |
8494 | else | |
8495 | { | |
8496 | if (arg_type->array_type() != NULL) | |
0f914071 | 8497 | { |
8498 | if (this->seen_) | |
8499 | { | |
c484d925 | 8500 | go_assert(saw_errors()); |
ea664253 | 8501 | return context->backend()->error_expression(); |
0f914071 | 8502 | } |
8503 | this->seen_ = true; | |
2c809f8f | 8504 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8505 | this->seen_ = false; |
8506 | } | |
e440a328 | 8507 | else if (arg_type->channel_type() != NULL) |
132ed071 | 8508 | { |
8509 | // The second field is the capacity. If the pointer | |
8510 | // is nil, the capacity is zero. | |
8511 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
8512 | Type* pint_type = Type::make_pointer_type(int_type); | |
8513 | Expression* parg = Expression::make_unsafe_cast(uintptr_type, | |
8514 | arg, | |
8515 | location); | |
8516 | int off = int_type->integer_type()->bits() / 8; | |
8517 | Expression* eoff = Expression::make_integer_ul(off, | |
8518 | uintptr_type, | |
8519 | location); | |
8520 | parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, | |
8521 | location); | |
8522 | parg = Expression::make_unsafe_cast(pint_type, parg, location); | |
8523 | Expression* nil = Expression::make_nil(location); | |
8524 | nil = Expression::make_cast(pint_type, nil, location); | |
8525 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8526 | arg, nil, location); | |
8527 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8528 | location); | |
8529 | Expression* indir = Expression::make_unary(OPERATOR_MULT, | |
8530 | parg, location); | |
8531 | val = Expression::make_conditional(cmp, zero, indir, location); | |
8532 | } | |
e440a328 | 8533 | else |
c3e6f413 | 8534 | go_unreachable(); |
e440a328 | 8535 | } |
8536 | ||
2c809f8f | 8537 | return Expression::make_cast(int_type, val, |
ea664253 | 8538 | location)->get_backend(context); |
e440a328 | 8539 | } |
8540 | ||
8541 | case BUILTIN_PRINT: | |
8542 | case BUILTIN_PRINTLN: | |
8543 | { | |
8544 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
88b03a70 | 8545 | |
8546 | Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, | |
8547 | location, 0); | |
e440a328 | 8548 | |
8549 | const Expression_list* call_args = this->args(); | |
8550 | if (call_args != NULL) | |
8551 | { | |
8552 | for (Expression_list::const_iterator p = call_args->begin(); | |
8553 | p != call_args->end(); | |
8554 | ++p) | |
8555 | { | |
8556 | if (is_ln && p != call_args->begin()) | |
8557 | { | |
2c809f8f | 8558 | Expression* print_space = |
88b03a70 | 8559 | Runtime::make_call(Runtime::PRINTSP, location, 0); |
e440a328 | 8560 | |
2c809f8f | 8561 | print_stmts = |
8562 | Expression::make_compound(print_stmts, print_space, | |
8563 | location); | |
8564 | } | |
e440a328 | 8565 | |
2c809f8f | 8566 | Expression* arg = *p; |
8567 | Type* type = arg->type(); | |
8568 | Runtime::Function code; | |
e440a328 | 8569 | if (type->is_string_type()) |
88b03a70 | 8570 | code = Runtime::PRINTSTRING; |
e440a328 | 8571 | else if (type->integer_type() != NULL |
8572 | && type->integer_type()->is_unsigned()) | |
8573 | { | |
e440a328 | 8574 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 8575 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 8576 | code = Runtime::PRINTUINT; |
e440a328 | 8577 | } |
8578 | else if (type->integer_type() != NULL) | |
8579 | { | |
e440a328 | 8580 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 8581 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 8582 | code = Runtime::PRINTINT; |
e440a328 | 8583 | } |
8584 | else if (type->float_type() != NULL) | |
8585 | { | |
2c809f8f | 8586 | Type* dtype = Type::lookup_float_type("float64"); |
8587 | arg = Expression::make_cast(dtype, arg, location); | |
88b03a70 | 8588 | code = Runtime::PRINTFLOAT; |
e440a328 | 8589 | } |
8590 | else if (type->complex_type() != NULL) | |
8591 | { | |
2c809f8f | 8592 | Type* ctype = Type::lookup_complex_type("complex128"); |
8593 | arg = Expression::make_cast(ctype, arg, location); | |
88b03a70 | 8594 | code = Runtime::PRINTCOMPLEX; |
e440a328 | 8595 | } |
8596 | else if (type->is_boolean_type()) | |
88b03a70 | 8597 | code = Runtime::PRINTBOOL; |
e440a328 | 8598 | else if (type->points_to() != NULL |
8599 | || type->channel_type() != NULL | |
8600 | || type->map_type() != NULL | |
8601 | || type->function_type() != NULL) | |
8602 | { | |
2c809f8f | 8603 | arg = Expression::make_cast(type, arg, location); |
88b03a70 | 8604 | code = Runtime::PRINTPOINTER; |
e440a328 | 8605 | } |
8606 | else if (type->interface_type() != NULL) | |
8607 | { | |
8608 | if (type->interface_type()->is_empty()) | |
88b03a70 | 8609 | code = Runtime::PRINTEFACE; |
e440a328 | 8610 | else |
88b03a70 | 8611 | code = Runtime::PRINTIFACE; |
e440a328 | 8612 | } |
411eb89e | 8613 | else if (type->is_slice_type()) |
88b03a70 | 8614 | code = Runtime::PRINTSLICE; |
e440a328 | 8615 | else |
cd238b8d | 8616 | { |
8617 | go_assert(saw_errors()); | |
ea664253 | 8618 | return context->backend()->error_expression(); |
cd238b8d | 8619 | } |
e440a328 | 8620 | |
2c809f8f | 8621 | Expression* call = Runtime::make_call(code, location, 1, arg); |
88b03a70 | 8622 | print_stmts = Expression::make_compound(print_stmts, call, |
8623 | location); | |
e440a328 | 8624 | } |
8625 | } | |
8626 | ||
8627 | if (is_ln) | |
8628 | { | |
2c809f8f | 8629 | Expression* print_nl = |
88b03a70 | 8630 | Runtime::make_call(Runtime::PRINTNL, location, 0); |
8631 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
8632 | location); | |
e440a328 | 8633 | } |
8634 | ||
88b03a70 | 8635 | Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, |
8636 | location, 0); | |
8637 | print_stmts = Expression::make_compound(print_stmts, unlock, location); | |
32e3ff69 | 8638 | |
ea664253 | 8639 | return print_stmts->get_backend(context); |
e440a328 | 8640 | } |
8641 | ||
8642 | case BUILTIN_PANIC: | |
8643 | { | |
8644 | const Expression_list* args = this->args(); | |
c484d925 | 8645 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8646 | Expression* arg = args->front(); |
b13c66cd | 8647 | Type *empty = |
823c7e3d | 8648 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 8649 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
8650 | ||
8651 | Expression* panic = | |
8652 | Runtime::make_call(Runtime::PANIC, location, 1, arg); | |
ea664253 | 8653 | return panic->get_backend(context); |
e440a328 | 8654 | } |
8655 | ||
8656 | case BUILTIN_RECOVER: | |
8657 | { | |
8658 | // The argument is set when building recover thunks. It's a | |
8659 | // boolean value which is true if we can recover a value now. | |
8660 | const Expression_list* args = this->args(); | |
c484d925 | 8661 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8662 | Expression* arg = args->front(); |
b13c66cd | 8663 | Type *empty = |
823c7e3d | 8664 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8665 | |
e440a328 | 8666 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 8667 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 8668 | |
8669 | // We need to handle a deferred call to recover specially, | |
8670 | // because it changes whether it can recover a panic or not. | |
8671 | // See test7 in test/recover1.go. | |
2c809f8f | 8672 | Expression* recover = Runtime::make_call((this->is_deferred() |
8673 | ? Runtime::DEFERRED_RECOVER | |
8674 | : Runtime::RECOVER), | |
8675 | location, 0); | |
8676 | Expression* cond = | |
8677 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 8678 | return cond->get_backend(context); |
e440a328 | 8679 | } |
8680 | ||
8681 | case BUILTIN_CLOSE: | |
e440a328 | 8682 | { |
8683 | const Expression_list* args = this->args(); | |
c484d925 | 8684 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8685 | Expression* arg = args->front(); |
2c809f8f | 8686 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
8687 | 1, arg); | |
ea664253 | 8688 | return close->get_backend(context); |
e440a328 | 8689 | } |
8690 | ||
8691 | case BUILTIN_SIZEOF: | |
8692 | case BUILTIN_OFFSETOF: | |
8693 | case BUILTIN_ALIGNOF: | |
8694 | { | |
0c77715b | 8695 | Numeric_constant nc; |
8696 | unsigned long val; | |
8697 | if (!this->numeric_constant_value(&nc) | |
8698 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8699 | { |
c484d925 | 8700 | go_assert(saw_errors()); |
ea664253 | 8701 | return context->backend()->error_expression(); |
7f1d9abd | 8702 | } |
7ba86326 | 8703 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 8704 | mpz_t ival; |
8705 | nc.get_int(&ival); | |
8706 | Expression* int_cst = | |
e67508fa | 8707 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 8708 | mpz_clear(ival); |
ea664253 | 8709 | return int_cst->get_backend(context); |
e440a328 | 8710 | } |
8711 | ||
8712 | case BUILTIN_COPY: | |
8713 | { | |
8714 | const Expression_list* args = this->args(); | |
c484d925 | 8715 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8716 | Expression* arg1 = args->front(); |
8717 | Expression* arg2 = args->back(); | |
8718 | ||
e440a328 | 8719 | Type* arg1_type = arg1->type(); |
8720 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 8721 | go_assert(arg1->is_variable()); |
2c809f8f | 8722 | Expression* arg1_val = at->get_value_pointer(gogo, arg1); |
8723 | Expression* arg1_len = at->get_length(gogo, arg1); | |
e440a328 | 8724 | |
8725 | Type* arg2_type = arg2->type(); | |
2c809f8f | 8726 | go_assert(arg2->is_variable()); |
8727 | Expression* arg2_val; | |
8728 | Expression* arg2_len; | |
411eb89e | 8729 | if (arg2_type->is_slice_type()) |
e440a328 | 8730 | { |
8731 | at = arg2_type->array_type(); | |
2c809f8f | 8732 | arg2_val = at->get_value_pointer(gogo, arg2); |
8733 | arg2_len = at->get_length(gogo, arg2); | |
e440a328 | 8734 | } |
8735 | else | |
8736 | { | |
2c809f8f | 8737 | go_assert(arg2->is_variable()); |
8738 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, | |
8739 | location); | |
8740 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8741 | location); | |
e440a328 | 8742 | } |
2c809f8f | 8743 | Expression* cond = |
8744 | Expression::make_binary(OPERATOR_LT, arg1_len, arg2_len, location); | |
8745 | Expression* length = | |
8746 | Expression::make_conditional(cond, arg1_len, arg2_len, location); | |
e440a328 | 8747 | |
8748 | Type* element_type = at->element_type(); | |
2a305b85 | 8749 | int64_t element_size; |
8750 | bool ok = element_type->backend_type_size(gogo, &element_size); | |
8751 | if (!ok) | |
8752 | { | |
8753 | go_assert(saw_errors()); | |
8754 | return gogo->backend()->error_expression(); | |
8755 | } | |
8756 | ||
3f378015 | 8757 | Expression* size_expr = Expression::make_integer_int64(element_size, |
8758 | length->type(), | |
8759 | location); | |
2c809f8f | 8760 | Expression* bytecount = |
8761 | Expression::make_binary(OPERATOR_MULT, size_expr, length, location); | |
8762 | Expression* copy = Runtime::make_call(Runtime::COPY, location, 3, | |
8763 | arg1_val, arg2_val, bytecount); | |
8764 | ||
8765 | Expression* compound = Expression::make_compound(copy, length, location); | |
ea664253 | 8766 | return compound->get_backend(context); |
e440a328 | 8767 | } |
8768 | ||
8769 | case BUILTIN_APPEND: | |
8770 | { | |
8771 | const Expression_list* args = this->args(); | |
c484d925 | 8772 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8773 | Expression* arg1 = args->front(); |
8774 | Expression* arg2 = args->back(); | |
8775 | ||
9d44fbe3 | 8776 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8777 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8778 | |
2c809f8f | 8779 | go_assert(arg2->is_variable()); |
8780 | Expression* arg2_val; | |
8781 | Expression* arg2_len; | |
3f378015 | 8782 | int64_t size; |
4fd4fcf4 | 8783 | if (arg2->type()->is_string_type() |
60963afd | 8784 | && element_type->integer_type() != NULL |
8785 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8786 | { |
2c809f8f | 8787 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, |
8788 | location); | |
8789 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8790 | location); | |
e67508fa | 8791 | size = 1; |
4fd4fcf4 | 8792 | } |
8793 | else | |
8794 | { | |
2c809f8f | 8795 | arg2_val = at->get_value_pointer(gogo, arg2); |
8796 | arg2_len = at->get_length(gogo, arg2); | |
2a305b85 | 8797 | bool ok = element_type->backend_type_size(gogo, &size); |
8798 | if (!ok) | |
8799 | { | |
8800 | go_assert(saw_errors()); | |
8801 | return gogo->backend()->error_expression(); | |
8802 | } | |
4fd4fcf4 | 8803 | } |
2c809f8f | 8804 | Expression* element_size = |
3f378015 | 8805 | Expression::make_integer_int64(size, NULL, location); |
2c809f8f | 8806 | |
8807 | Expression* append = Runtime::make_call(Runtime::APPEND, location, 4, | |
8808 | arg1, arg2_val, arg2_len, | |
8809 | element_size); | |
8810 | append = Expression::make_unsafe_cast(arg1->type(), append, location); | |
ea664253 | 8811 | return append->get_backend(context); |
e440a328 | 8812 | } |
8813 | ||
8814 | case BUILTIN_REAL: | |
8815 | case BUILTIN_IMAG: | |
8816 | { | |
8817 | const Expression_list* args = this->args(); | |
c484d925 | 8818 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8819 | |
8820 | Bexpression* ret; | |
ea664253 | 8821 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 8822 | if (this->code_ == BUILTIN_REAL) |
8823 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
8824 | else | |
8825 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 8826 | return ret; |
e440a328 | 8827 | } |
8828 | ||
48080209 | 8829 | case BUILTIN_COMPLEX: |
e440a328 | 8830 | { |
8831 | const Expression_list* args = this->args(); | |
c484d925 | 8832 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 8833 | Bexpression* breal = args->front()->get_backend(context); |
8834 | Bexpression* bimag = args->back()->get_backend(context); | |
8835 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 8836 | } |
8837 | ||
8838 | default: | |
c3e6f413 | 8839 | go_unreachable(); |
e440a328 | 8840 | } |
8841 | } | |
8842 | ||
8843 | // We have to support exporting a builtin call expression, because | |
8844 | // code can set a constant to the result of a builtin expression. | |
8845 | ||
8846 | void | |
8847 | Builtin_call_expression::do_export(Export* exp) const | |
8848 | { | |
0c77715b | 8849 | Numeric_constant nc; |
8850 | if (!this->numeric_constant_value(&nc)) | |
8851 | { | |
631d5788 | 8852 | go_error_at(this->location(), "value is not constant"); |
0c77715b | 8853 | return; |
8854 | } | |
e440a328 | 8855 | |
0c77715b | 8856 | if (nc.is_int()) |
e440a328 | 8857 | { |
0c77715b | 8858 | mpz_t val; |
8859 | nc.get_int(&val); | |
e440a328 | 8860 | Integer_expression::export_integer(exp, val); |
0c77715b | 8861 | mpz_clear(val); |
e440a328 | 8862 | } |
0c77715b | 8863 | else if (nc.is_float()) |
e440a328 | 8864 | { |
8865 | mpfr_t fval; | |
0c77715b | 8866 | nc.get_float(&fval); |
8867 | Float_expression::export_float(exp, fval); | |
e440a328 | 8868 | mpfr_clear(fval); |
8869 | } | |
0c77715b | 8870 | else if (nc.is_complex()) |
e440a328 | 8871 | { |
fcbea5e4 | 8872 | mpc_t cval; |
8873 | nc.get_complex(&cval); | |
8874 | Complex_expression::export_complex(exp, cval); | |
8875 | mpc_clear(cval); | |
e440a328 | 8876 | } |
0c77715b | 8877 | else |
8878 | go_unreachable(); | |
e440a328 | 8879 | |
8880 | // A trailing space lets us reliably identify the end of the number. | |
8881 | exp->write_c_string(" "); | |
8882 | } | |
8883 | ||
8884 | // Class Call_expression. | |
8885 | ||
8381eda7 | 8886 | // A Go function can be viewed in a couple of different ways. The |
8887 | // code of a Go function becomes a backend function with parameters | |
8888 | // whose types are simply the backend representation of the Go types. | |
8889 | // If there are multiple results, they are returned as a backend | |
8890 | // struct. | |
8891 | ||
8892 | // However, when Go code refers to a function other than simply | |
8893 | // calling it, the backend type of that function is actually a struct. | |
8894 | // The first field of the struct points to the Go function code | |
8895 | // (sometimes a wrapper as described below). The remaining fields | |
8896 | // hold addresses of closed-over variables. This struct is called a | |
8897 | // closure. | |
8898 | ||
8899 | // There are a few cases to consider. | |
8900 | ||
8901 | // A direct function call of a known function in package scope. In | |
8902 | // this case there are no closed-over variables, and we know the name | |
8903 | // of the function code. We can simply produce a backend call to the | |
8904 | // function directly, and not worry about the closure. | |
8905 | ||
8906 | // A direct function call of a known function literal. In this case | |
8907 | // we know the function code and we know the closure. We generate the | |
8908 | // function code such that it expects an additional final argument of | |
8909 | // the closure type. We pass the closure as the last argument, after | |
8910 | // the other arguments. | |
8911 | ||
8912 | // An indirect function call. In this case we have a closure. We | |
8913 | // load the pointer to the function code from the first field of the | |
8914 | // closure. We pass the address of the closure as the last argument. | |
8915 | ||
8916 | // A call to a method of an interface. Type methods are always at | |
8917 | // package scope, so we call the function directly, and don't worry | |
8918 | // about the closure. | |
8919 | ||
8920 | // This means that for a function at package scope we have two cases. | |
8921 | // One is the direct call, which has no closure. The other is the | |
8922 | // indirect call, which does have a closure. We can't simply ignore | |
8923 | // the closure, even though it is the last argument, because that will | |
8924 | // fail on targets where the function pops its arguments. So when | |
8925 | // generating a closure for a package-scope function we set the | |
8926 | // function code pointer in the closure to point to a wrapper | |
8927 | // function. This wrapper function accepts a final argument that | |
8928 | // points to the closure, ignores it, and calls the real function as a | |
8929 | // direct function call. This wrapper will normally be efficient, and | |
8930 | // can often simply be a tail call to the real function. | |
8931 | ||
8932 | // We don't use GCC's static chain pointer because 1) we don't need | |
8933 | // it; 2) GCC only permits using a static chain to call a known | |
8934 | // function, so we can't use it for an indirect call anyhow. Since we | |
8935 | // can't use it for an indirect call, we may as well not worry about | |
8936 | // using it for a direct call either. | |
8937 | ||
8938 | // We pass the closure last rather than first because it means that | |
8939 | // the function wrapper we put into a closure for a package-scope | |
8940 | // function can normally just be a tail call to the real function. | |
8941 | ||
8942 | // For method expressions we generate a wrapper that loads the | |
8943 | // receiver from the closure and then calls the method. This | |
8944 | // unfortunately forces reshuffling the arguments, since there is a | |
8945 | // new first argument, but we can't avoid reshuffling either for | |
8946 | // method expressions or for indirect calls of package-scope | |
8947 | // functions, and since the latter are more common we reshuffle for | |
8948 | // method expressions. | |
8949 | ||
8950 | // Note that the Go code retains the Go types. The extra final | |
8951 | // argument only appears when we convert to the backend | |
8952 | // representation. | |
8953 | ||
e440a328 | 8954 | // Traversal. |
8955 | ||
8956 | int | |
8957 | Call_expression::do_traverse(Traverse* traverse) | |
8958 | { | |
0c0dacab | 8959 | // If we are calling a function in a different package that returns |
8960 | // an unnamed type, this may be the only chance we get to traverse | |
8961 | // that type. We don't traverse this->type_ because it may be a | |
8962 | // Call_multiple_result_type that will just lead back here. | |
8963 | if (this->type_ != NULL && !this->type_->is_error_type()) | |
8964 | { | |
8965 | Function_type *fntype = this->get_function_type(); | |
8966 | if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) | |
8967 | return TRAVERSE_EXIT; | |
8968 | } | |
e440a328 | 8969 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) |
8970 | return TRAVERSE_EXIT; | |
8971 | if (this->args_ != NULL) | |
8972 | { | |
8973 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8974 | return TRAVERSE_EXIT; | |
8975 | } | |
8976 | return TRAVERSE_CONTINUE; | |
8977 | } | |
8978 | ||
8979 | // Lower a call statement. | |
8980 | ||
8981 | Expression* | |
ceeb4318 | 8982 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
8983 | Statement_inserter* inserter, int) | |
e440a328 | 8984 | { |
b13c66cd | 8985 | Location loc = this->location(); |
09ea332d | 8986 | |
ceeb4318 | 8987 | // A type cast can look like a function call. |
e440a328 | 8988 | if (this->fn_->is_type_expression() |
8989 | && this->args_ != NULL | |
8990 | && this->args_->size() == 1) | |
8991 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 8992 | loc); |
e440a328 | 8993 | |
88f06749 | 8994 | // Because do_type will return an error type and thus prevent future |
8995 | // errors, check for that case now to ensure that the error gets | |
8996 | // reported. | |
37448b10 | 8997 | Function_type* fntype = this->get_function_type(); |
8998 | if (fntype == NULL) | |
88f06749 | 8999 | { |
9000 | if (!this->fn_->type()->is_error()) | |
9001 | this->report_error(_("expected function")); | |
5f1045b5 | 9002 | this->set_is_error(); |
9003 | return this; | |
88f06749 | 9004 | } |
9005 | ||
e440a328 | 9006 | // Handle an argument which is a call to a function which returns |
9007 | // multiple results. | |
9008 | if (this->args_ != NULL | |
9009 | && this->args_->size() == 1 | |
37448b10 | 9010 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9011 | { |
e440a328 | 9012 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9013 | if (rc > 1 | |
37448b10 | 9014 | && ((fntype->parameters() != NULL |
9015 | && (fntype->parameters()->size() == rc | |
9016 | || (fntype->is_varargs() | |
9017 | && fntype->parameters()->size() - 1 <= rc))) | |
9018 | || fntype->is_builtin())) | |
e440a328 | 9019 | { |
9020 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 9021 | call->set_is_multi_value_arg(); |
c33af8e4 | 9022 | if (this->is_varargs_) |
9023 | { | |
9024 | // It is not clear which result of a multiple result call | |
9025 | // the ellipsis operator should be applied to. If we unpack the | |
9026 | // the call into its individual results here, the ellipsis will be | |
9027 | // applied to the last result. | |
631d5788 | 9028 | go_error_at(call->location(), |
9029 | _("multiple-value argument in single-value context")); | |
c33af8e4 | 9030 | return Expression::make_error(call->location()); |
9031 | } | |
9032 | ||
e440a328 | 9033 | Expression_list* args = new Expression_list; |
9034 | for (size_t i = 0; i < rc; ++i) | |
9035 | args->push_back(Expression::make_call_result(call, i)); | |
9036 | // We can't return a new call expression here, because this | |
42535814 | 9037 | // one may be referenced by Call_result expressions. We |
9038 | // also can't delete the old arguments, because we may still | |
9039 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9040 | this->args_ = args; |
9041 | } | |
9042 | } | |
9043 | ||
37448b10 | 9044 | // Recognize a call to a builtin function. |
9045 | if (fntype->is_builtin()) | |
9046 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9047 | this->is_varargs_, loc); | |
9048 | ||
ceeb4318 | 9049 | // If this call returns multiple results, create a temporary |
9050 | // variable for each result. | |
9051 | size_t rc = this->result_count(); | |
9052 | if (rc > 1 && this->results_ == NULL) | |
9053 | { | |
9054 | std::vector<Temporary_statement*>* temps = | |
9055 | new std::vector<Temporary_statement*>; | |
9056 | temps->reserve(rc); | |
37448b10 | 9057 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9058 | for (Typed_identifier_list::const_iterator p = results->begin(); |
9059 | p != results->end(); | |
9060 | ++p) | |
9061 | { | |
9062 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 9063 | NULL, loc); |
ceeb4318 | 9064 | inserter->insert(temp); |
9065 | temps->push_back(temp); | |
9066 | } | |
9067 | this->results_ = temps; | |
9068 | } | |
9069 | ||
e440a328 | 9070 | // Handle a call to a varargs function by packaging up the extra |
9071 | // parameters. | |
37448b10 | 9072 | if (fntype->is_varargs()) |
e440a328 | 9073 | { |
e440a328 | 9074 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9075 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9076 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9077 | this->lower_varargs(gogo, function, inserter, varargs_type, |
0e9a2e72 | 9078 | parameters->size(), SLICE_STORAGE_MAY_ESCAPE); |
09ea332d | 9079 | } |
9080 | ||
9081 | // If this is call to a method, call the method directly passing the | |
9082 | // object as the first parameter. | |
9083 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9084 | if (bme != NULL) | |
9085 | { | |
0afbb937 | 9086 | Named_object* methodfn = bme->function(); |
09ea332d | 9087 | Expression* first_arg = bme->first_argument(); |
9088 | ||
9089 | // We always pass a pointer when calling a method. | |
9090 | if (first_arg->type()->points_to() == NULL | |
9091 | && !first_arg->type()->is_error()) | |
9092 | { | |
9093 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9094 | // We may need to create a temporary variable so that we can | |
9095 | // take the address. We can't do that here because it will | |
9096 | // mess up the order of evaluation. | |
9097 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9098 | ue->set_create_temp(); | |
9099 | } | |
9100 | ||
9101 | // If we are calling a method which was inherited from an | |
9102 | // embedded struct, and the method did not get a stub, then the | |
9103 | // first type may be wrong. | |
9104 | Type* fatype = bme->first_argument_type(); | |
9105 | if (fatype != NULL) | |
9106 | { | |
9107 | if (fatype->points_to() == NULL) | |
9108 | fatype = Type::make_pointer_type(fatype); | |
9109 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9110 | } | |
9111 | ||
9112 | Expression_list* new_args = new Expression_list(); | |
9113 | new_args->push_back(first_arg); | |
9114 | if (this->args_ != NULL) | |
9115 | { | |
9116 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9117 | p != this->args_->end(); | |
9118 | ++p) | |
9119 | new_args->push_back(*p); | |
9120 | } | |
9121 | ||
9122 | // We have to change in place because this structure may be | |
9123 | // referenced by Call_result_expressions. We can't delete the | |
9124 | // old arguments, because we may be traversing them up in some | |
9125 | // caller. FIXME. | |
9126 | this->args_ = new_args; | |
0afbb937 | 9127 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9128 | bme->location()); |
e440a328 | 9129 | } |
9130 | ||
105f9a24 | 9131 | // Handle a couple of special runtime functions. In the runtime |
9132 | // package, getcallerpc returns the PC of the caller, and | |
9133 | // getcallersp returns the frame pointer of the caller. Implement | |
9134 | // these by turning them into calls to GCC builtin functions. We | |
9135 | // could implement them in normal code, but then we would have to | |
9136 | // explicitly unwind the stack. These functions are intended to be | |
9137 | // efficient. Note that this technique obviously only works for | |
9138 | // direct calls, but that is the only way they are used. The actual | |
9139 | // argument to these functions is always the address of a parameter; | |
9140 | // we don't need that for the GCC builtin functions, so we just | |
9141 | // ignore it. | |
9142 | if (gogo->compiling_runtime() | |
9143 | && this->args_ != NULL | |
9144 | && this->args_->size() == 1 | |
9145 | && gogo->package_name() == "runtime") | |
9146 | { | |
9147 | Func_expression* fe = this->fn_->func_expression(); | |
9148 | if (fe != NULL | |
9149 | && fe->named_object()->is_function_declaration() | |
9150 | && fe->named_object()->package() == NULL) | |
9151 | { | |
9152 | std::string n = Gogo::unpack_hidden_name(fe->named_object()->name()); | |
9153 | if (n == "getcallerpc") | |
9154 | { | |
9155 | static Named_object* builtin_return_address; | |
9156 | return this->lower_to_builtin(&builtin_return_address, | |
9157 | "__builtin_return_address", | |
9158 | 0); | |
9159 | } | |
9160 | else if (n == "getcallersp") | |
9161 | { | |
9162 | static Named_object* builtin_frame_address; | |
9163 | return this->lower_to_builtin(&builtin_frame_address, | |
9164 | "__builtin_frame_address", | |
9165 | 1); | |
9166 | } | |
9167 | } | |
9168 | } | |
9169 | ||
e440a328 | 9170 | return this; |
9171 | } | |
9172 | ||
9173 | // Lower a call to a varargs function. FUNCTION is the function in | |
9174 | // which the call occurs--it's not the function we are calling. | |
9175 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9176 | // PARAM_COUNT is the number of parameters of the function we are | |
9177 | // calling; the last of these parameters will be the varargs | |
9178 | // parameter. | |
9179 | ||
09ea332d | 9180 | void |
e440a328 | 9181 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9182 | Statement_inserter* inserter, |
0e9a2e72 | 9183 | Type* varargs_type, size_t param_count, |
9184 | Slice_storage_escape_disp escape_disp) | |
e440a328 | 9185 | { |
9186 | if (this->varargs_are_lowered_) | |
09ea332d | 9187 | return; |
e440a328 | 9188 | |
b13c66cd | 9189 | Location loc = this->location(); |
e440a328 | 9190 | |
c484d925 | 9191 | go_assert(param_count > 0); |
411eb89e | 9192 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9193 | |
9194 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9195 | if (arg_count < param_count - 1) | |
9196 | { | |
9197 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9198 | return; |
e440a328 | 9199 | } |
9200 | ||
9201 | Expression_list* old_args = this->args_; | |
9202 | Expression_list* new_args = new Expression_list(); | |
9203 | bool push_empty_arg = false; | |
9204 | if (old_args == NULL || old_args->empty()) | |
9205 | { | |
c484d925 | 9206 | go_assert(param_count == 1); |
e440a328 | 9207 | push_empty_arg = true; |
9208 | } | |
9209 | else | |
9210 | { | |
9211 | Expression_list::const_iterator pa; | |
9212 | int i = 1; | |
9213 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9214 | { | |
9215 | if (static_cast<size_t>(i) == param_count) | |
9216 | break; | |
9217 | new_args->push_back(*pa); | |
9218 | } | |
9219 | ||
9220 | // We have reached the varargs parameter. | |
9221 | ||
9222 | bool issued_error = false; | |
9223 | if (pa == old_args->end()) | |
9224 | push_empty_arg = true; | |
9225 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9226 | new_args->push_back(*pa); | |
9227 | else if (this->is_varargs_) | |
9228 | { | |
a6645f74 | 9229 | if ((*pa)->type()->is_slice_type()) |
9230 | this->report_error(_("too many arguments")); | |
9231 | else | |
9232 | { | |
631d5788 | 9233 | go_error_at(this->location(), |
9234 | _("invalid use of %<...%> with non-slice")); | |
a6645f74 | 9235 | this->set_is_error(); |
9236 | } | |
09ea332d | 9237 | return; |
e440a328 | 9238 | } |
e440a328 | 9239 | else |
9240 | { | |
9241 | Type* element_type = varargs_type->array_type()->element_type(); | |
9242 | Expression_list* vals = new Expression_list; | |
9243 | for (; pa != old_args->end(); ++pa, ++i) | |
9244 | { | |
9245 | // Check types here so that we get a better message. | |
9246 | Type* patype = (*pa)->type(); | |
b13c66cd | 9247 | Location paloc = (*pa)->location(); |
e440a328 | 9248 | if (!this->check_argument_type(i, element_type, patype, |
9249 | paloc, issued_error)) | |
9250 | continue; | |
9251 | vals->push_back(*pa); | |
9252 | } | |
0e9a2e72 | 9253 | Slice_construction_expression* sce = |
e440a328 | 9254 | Expression::make_slice_composite_literal(varargs_type, vals, loc); |
0e9a2e72 | 9255 | if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) |
9256 | sce->set_storage_does_not_escape(); | |
9257 | Expression* val = sce; | |
09ea332d | 9258 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9259 | new_args->push_back(val); |
9260 | } | |
9261 | } | |
9262 | ||
9263 | if (push_empty_arg) | |
9264 | new_args->push_back(Expression::make_nil(loc)); | |
9265 | ||
9266 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9267 | // be referenced by Call_result expressions. FIXME. We can't |
9268 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9269 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9270 | this->args_ = new_args; |
9271 | this->varargs_are_lowered_ = true; | |
e440a328 | 9272 | } |
9273 | ||
105f9a24 | 9274 | // Return a call to __builtin_return_address or __builtin_frame_address. |
9275 | ||
9276 | Expression* | |
9277 | Call_expression::lower_to_builtin(Named_object** pno, const char* name, | |
9278 | int arg) | |
9279 | { | |
9280 | if (*pno == NULL) | |
9281 | *pno = Gogo::declare_builtin_rf_address(name); | |
9282 | ||
9283 | Location loc = this->location(); | |
9284 | ||
9285 | Expression* fn = Expression::make_func_reference(*pno, NULL, loc); | |
9286 | Expression* a = Expression::make_integer_ul(arg, NULL, loc); | |
9287 | Expression_list *args = new Expression_list(); | |
9288 | args->push_back(a); | |
9289 | Expression* call = Expression::make_call(fn, args, false, loc); | |
9290 | ||
9291 | // The builtin functions return void*, but the Go functions return uintptr. | |
9292 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9293 | return Expression::make_cast(uintptr_type, call, loc); | |
9294 | } | |
9295 | ||
2c809f8f | 9296 | // Flatten a call with multiple results into a temporary. |
9297 | ||
9298 | Expression* | |
b8e86a51 | 9299 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
9300 | Statement_inserter* inserter) | |
2c809f8f | 9301 | { |
5bf8be8b | 9302 | if (this->is_erroneous_call()) |
9303 | { | |
9304 | go_assert(saw_errors()); | |
9305 | return Expression::make_error(this->location()); | |
9306 | } | |
b8e86a51 | 9307 | |
91c0fd76 | 9308 | if (this->is_flattened_) |
9309 | return this; | |
9310 | this->is_flattened_ = true; | |
9311 | ||
b8e86a51 | 9312 | // Add temporary variables for all arguments that require type |
9313 | // conversion. | |
9314 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 9315 | if (fntype == NULL) |
9316 | { | |
9317 | go_assert(saw_errors()); | |
9318 | return this; | |
9319 | } | |
b8e86a51 | 9320 | if (this->args_ != NULL && !this->args_->empty() |
9321 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
9322 | { | |
9323 | bool is_interface_method = | |
9324 | this->fn_->interface_field_reference_expression() != NULL; | |
9325 | ||
9326 | Expression_list *args = new Expression_list(); | |
9327 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
9328 | Expression_list::const_iterator pa = this->args_->begin(); | |
9329 | if (!is_interface_method && fntype->is_method()) | |
9330 | { | |
9331 | // The receiver argument. | |
9332 | args->push_back(*pa); | |
9333 | ++pa; | |
9334 | } | |
9335 | for (; pa != this->args_->end(); ++pa, ++pp) | |
9336 | { | |
9337 | go_assert(pp != fntype->parameters()->end()); | |
9338 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
9339 | args->push_back(*pa); | |
9340 | else | |
9341 | { | |
9342 | Location loc = (*pa)->location(); | |
8ba8cc87 | 9343 | Expression* arg = *pa; |
9344 | if (!arg->is_variable()) | |
9345 | { | |
9346 | Temporary_statement *temp = | |
9347 | Statement::make_temporary(NULL, arg, loc); | |
9348 | inserter->insert(temp); | |
9349 | arg = Expression::make_temporary_reference(temp, loc); | |
9350 | } | |
9351 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
9352 | loc); | |
9353 | args->push_back(arg); | |
b8e86a51 | 9354 | } |
9355 | } | |
9356 | delete this->args_; | |
9357 | this->args_ = args; | |
9358 | } | |
9359 | ||
2c809f8f | 9360 | size_t rc = this->result_count(); |
9361 | if (rc > 1 && this->call_temp_ == NULL) | |
9362 | { | |
9363 | Struct_field_list* sfl = new Struct_field_list(); | |
9364 | Function_type* fntype = this->get_function_type(); | |
9365 | const Typed_identifier_list* results = fntype->results(); | |
9366 | Location loc = this->location(); | |
9367 | ||
9368 | int i = 0; | |
61575e0f | 9369 | char buf[20]; |
2c809f8f | 9370 | for (Typed_identifier_list::const_iterator p = results->begin(); |
9371 | p != results->end(); | |
9372 | ++p, ++i) | |
9373 | { | |
9374 | snprintf(buf, sizeof buf, "res%d", i); | |
9375 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9376 | } | |
9377 | ||
9378 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9379 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9380 | inserter->insert(this->call_temp_); | |
9381 | } | |
9382 | ||
9383 | return this; | |
9384 | } | |
9385 | ||
ceeb4318 | 9386 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9387 | |
9388 | Function_type* | |
9389 | Call_expression::get_function_type() const | |
9390 | { | |
9391 | return this->fn_->type()->function_type(); | |
9392 | } | |
9393 | ||
9394 | // Return the number of values which this call will return. | |
9395 | ||
9396 | size_t | |
9397 | Call_expression::result_count() const | |
9398 | { | |
9399 | const Function_type* fntype = this->get_function_type(); | |
9400 | if (fntype == NULL) | |
9401 | return 0; | |
9402 | if (fntype->results() == NULL) | |
9403 | return 0; | |
9404 | return fntype->results()->size(); | |
9405 | } | |
9406 | ||
ceeb4318 | 9407 | // Return the temporary which holds a result. |
9408 | ||
9409 | Temporary_statement* | |
9410 | Call_expression::result(size_t i) const | |
9411 | { | |
cd238b8d | 9412 | if (this->results_ == NULL || this->results_->size() <= i) |
9413 | { | |
9414 | go_assert(saw_errors()); | |
9415 | return NULL; | |
9416 | } | |
ceeb4318 | 9417 | return (*this->results_)[i]; |
9418 | } | |
9419 | ||
1373401e | 9420 | // Set the number of results expected from a call expression. |
9421 | ||
9422 | void | |
9423 | Call_expression::set_expected_result_count(size_t count) | |
9424 | { | |
9425 | go_assert(this->expected_result_count_ == 0); | |
9426 | this->expected_result_count_ = count; | |
9427 | } | |
9428 | ||
e440a328 | 9429 | // Return whether this is a call to the predeclared function recover. |
9430 | ||
9431 | bool | |
9432 | Call_expression::is_recover_call() const | |
9433 | { | |
9434 | return this->do_is_recover_call(); | |
9435 | } | |
9436 | ||
9437 | // Set the argument to the recover function. | |
9438 | ||
9439 | void | |
9440 | Call_expression::set_recover_arg(Expression* arg) | |
9441 | { | |
9442 | this->do_set_recover_arg(arg); | |
9443 | } | |
9444 | ||
9445 | // Virtual functions also implemented by Builtin_call_expression. | |
9446 | ||
9447 | bool | |
9448 | Call_expression::do_is_recover_call() const | |
9449 | { | |
9450 | return false; | |
9451 | } | |
9452 | ||
9453 | void | |
9454 | Call_expression::do_set_recover_arg(Expression*) | |
9455 | { | |
c3e6f413 | 9456 | go_unreachable(); |
e440a328 | 9457 | } |
9458 | ||
ceeb4318 | 9459 | // We have found an error with this call expression; return true if |
9460 | // we should report it. | |
9461 | ||
9462 | bool | |
9463 | Call_expression::issue_error() | |
9464 | { | |
9465 | if (this->issued_error_) | |
9466 | return false; | |
9467 | else | |
9468 | { | |
9469 | this->issued_error_ = true; | |
9470 | return true; | |
9471 | } | |
9472 | } | |
9473 | ||
5bf8be8b | 9474 | // Whether or not this call contains errors, either in the call or the |
9475 | // arguments to the call. | |
9476 | ||
9477 | bool | |
9478 | Call_expression::is_erroneous_call() | |
9479 | { | |
9480 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9481 | return true; | |
9482 | ||
9483 | if (this->args() == NULL) | |
9484 | return false; | |
9485 | for (Expression_list::iterator pa = this->args()->begin(); | |
9486 | pa != this->args()->end(); | |
9487 | ++pa) | |
9488 | { | |
9489 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9490 | return true; | |
9491 | } | |
9492 | return false; | |
9493 | } | |
9494 | ||
e440a328 | 9495 | // Get the type. |
9496 | ||
9497 | Type* | |
9498 | Call_expression::do_type() | |
9499 | { | |
9500 | if (this->type_ != NULL) | |
9501 | return this->type_; | |
9502 | ||
9503 | Type* ret; | |
9504 | Function_type* fntype = this->get_function_type(); | |
9505 | if (fntype == NULL) | |
9506 | return Type::make_error_type(); | |
9507 | ||
9508 | const Typed_identifier_list* results = fntype->results(); | |
9509 | if (results == NULL) | |
9510 | ret = Type::make_void_type(); | |
9511 | else if (results->size() == 1) | |
9512 | ret = results->begin()->type(); | |
9513 | else | |
9514 | ret = Type::make_call_multiple_result_type(this); | |
9515 | ||
9516 | this->type_ = ret; | |
9517 | ||
9518 | return this->type_; | |
9519 | } | |
9520 | ||
9521 | // Determine types for a call expression. We can use the function | |
9522 | // parameter types to set the types of the arguments. | |
9523 | ||
9524 | void | |
9525 | Call_expression::do_determine_type(const Type_context*) | |
9526 | { | |
fb94b0ca | 9527 | if (!this->determining_types()) |
9528 | return; | |
9529 | ||
e440a328 | 9530 | this->fn_->determine_type_no_context(); |
9531 | Function_type* fntype = this->get_function_type(); | |
9532 | const Typed_identifier_list* parameters = NULL; | |
9533 | if (fntype != NULL) | |
9534 | parameters = fntype->parameters(); | |
9535 | if (this->args_ != NULL) | |
9536 | { | |
9537 | Typed_identifier_list::const_iterator pt; | |
9538 | if (parameters != NULL) | |
9539 | pt = parameters->begin(); | |
09ea332d | 9540 | bool first = true; |
e440a328 | 9541 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9542 | pa != this->args_->end(); | |
9543 | ++pa) | |
9544 | { | |
09ea332d | 9545 | if (first) |
9546 | { | |
9547 | first = false; | |
9548 | // If this is a method, the first argument is the | |
9549 | // receiver. | |
9550 | if (fntype != NULL && fntype->is_method()) | |
9551 | { | |
9552 | Type* rtype = fntype->receiver()->type(); | |
9553 | // The receiver is always passed as a pointer. | |
9554 | if (rtype->points_to() == NULL) | |
9555 | rtype = Type::make_pointer_type(rtype); | |
9556 | Type_context subcontext(rtype, false); | |
9557 | (*pa)->determine_type(&subcontext); | |
9558 | continue; | |
9559 | } | |
9560 | } | |
9561 | ||
e440a328 | 9562 | if (parameters != NULL && pt != parameters->end()) |
9563 | { | |
9564 | Type_context subcontext(pt->type(), false); | |
9565 | (*pa)->determine_type(&subcontext); | |
9566 | ++pt; | |
9567 | } | |
9568 | else | |
9569 | (*pa)->determine_type_no_context(); | |
9570 | } | |
9571 | } | |
9572 | } | |
9573 | ||
fb94b0ca | 9574 | // Called when determining types for a Call_expression. Return true |
9575 | // if we should go ahead, false if they have already been determined. | |
9576 | ||
9577 | bool | |
9578 | Call_expression::determining_types() | |
9579 | { | |
9580 | if (this->types_are_determined_) | |
9581 | return false; | |
9582 | else | |
9583 | { | |
9584 | this->types_are_determined_ = true; | |
9585 | return true; | |
9586 | } | |
9587 | } | |
9588 | ||
e440a328 | 9589 | // Check types for parameter I. |
9590 | ||
9591 | bool | |
9592 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9593 | const Type* argument_type, | |
b13c66cd | 9594 | Location argument_location, |
e440a328 | 9595 | bool issued_error) |
9596 | { | |
9597 | std::string reason; | |
1eae365b | 9598 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 9599 | { |
9600 | if (!issued_error) | |
9601 | { | |
9602 | if (reason.empty()) | |
631d5788 | 9603 | go_error_at(argument_location, "argument %d has incompatible type", i); |
e440a328 | 9604 | else |
631d5788 | 9605 | go_error_at(argument_location, |
9606 | "argument %d has incompatible type (%s)", | |
9607 | i, reason.c_str()); | |
e440a328 | 9608 | } |
9609 | this->set_is_error(); | |
9610 | return false; | |
9611 | } | |
9612 | return true; | |
9613 | } | |
9614 | ||
9615 | // Check types. | |
9616 | ||
9617 | void | |
9618 | Call_expression::do_check_types(Gogo*) | |
9619 | { | |
a6645f74 | 9620 | if (this->classification() == EXPRESSION_ERROR) |
9621 | return; | |
9622 | ||
e440a328 | 9623 | Function_type* fntype = this->get_function_type(); |
9624 | if (fntype == NULL) | |
9625 | { | |
5c13bd80 | 9626 | if (!this->fn_->type()->is_error()) |
e440a328 | 9627 | this->report_error(_("expected function")); |
9628 | return; | |
9629 | } | |
9630 | ||
1373401e | 9631 | if (this->expected_result_count_ != 0 |
9632 | && this->expected_result_count_ != this->result_count()) | |
9633 | { | |
9634 | if (this->issue_error()) | |
9635 | this->report_error(_("function result count mismatch")); | |
9636 | this->set_is_error(); | |
9637 | return; | |
9638 | } | |
9639 | ||
09ea332d | 9640 | bool is_method = fntype->is_method(); |
9641 | if (is_method) | |
e440a328 | 9642 | { |
09ea332d | 9643 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9644 | Type* rtype = fntype->receiver()->type(); | |
9645 | Expression* first_arg = this->args_->front(); | |
1eae365b | 9646 | // We dereference the values since receivers are always passed |
9647 | // as pointers. | |
09ea332d | 9648 | std::string reason; |
1eae365b | 9649 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
9650 | &reason)) | |
e440a328 | 9651 | { |
09ea332d | 9652 | if (reason.empty()) |
9653 | this->report_error(_("incompatible type for receiver")); | |
9654 | else | |
e440a328 | 9655 | { |
631d5788 | 9656 | go_error_at(this->location(), |
9657 | "incompatible type for receiver (%s)", | |
9658 | reason.c_str()); | |
09ea332d | 9659 | this->set_is_error(); |
e440a328 | 9660 | } |
9661 | } | |
9662 | } | |
9663 | ||
9664 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 9665 | // we don't have to worry about it here unless something is wrong. |
9666 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
9667 | { | |
9668 | if (!fntype->is_varargs()) | |
9669 | { | |
631d5788 | 9670 | go_error_at(this->location(), |
9671 | _("invalid use of %<...%> calling non-variadic function")); | |
a6645f74 | 9672 | this->set_is_error(); |
9673 | return; | |
9674 | } | |
9675 | } | |
e440a328 | 9676 | |
9677 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9678 | if (this->args_ == NULL) | |
9679 | { | |
9680 | if (parameters != NULL && !parameters->empty()) | |
9681 | this->report_error(_("not enough arguments")); | |
9682 | } | |
9683 | else if (parameters == NULL) | |
09ea332d | 9684 | { |
9685 | if (!is_method || this->args_->size() > 1) | |
9686 | this->report_error(_("too many arguments")); | |
9687 | } | |
1373401e | 9688 | else if (this->args_->size() == 1 |
9689 | && this->args_->front()->call_expression() != NULL | |
9690 | && this->args_->front()->call_expression()->result_count() > 1) | |
9691 | { | |
9692 | // This is F(G()) when G returns more than one result. If the | |
9693 | // results can be matched to parameters, it would have been | |
9694 | // lowered in do_lower. If we get here we know there is a | |
9695 | // mismatch. | |
9696 | if (this->args_->front()->call_expression()->result_count() | |
9697 | < parameters->size()) | |
9698 | this->report_error(_("not enough arguments")); | |
9699 | else | |
9700 | this->report_error(_("too many arguments")); | |
9701 | } | |
e440a328 | 9702 | else |
9703 | { | |
9704 | int i = 0; | |
09ea332d | 9705 | Expression_list::const_iterator pa = this->args_->begin(); |
9706 | if (is_method) | |
9707 | ++pa; | |
9708 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9709 | pt != parameters->end(); | |
9710 | ++pt, ++pa, ++i) | |
e440a328 | 9711 | { |
09ea332d | 9712 | if (pa == this->args_->end()) |
e440a328 | 9713 | { |
09ea332d | 9714 | this->report_error(_("not enough arguments")); |
e440a328 | 9715 | return; |
9716 | } | |
9717 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9718 | (*pa)->location(), false); | |
9719 | } | |
09ea332d | 9720 | if (pa != this->args_->end()) |
9721 | this->report_error(_("too many arguments")); | |
e440a328 | 9722 | } |
9723 | } | |
9724 | ||
72666aed | 9725 | Expression* |
9726 | Call_expression::do_copy() | |
9727 | { | |
9728 | Call_expression* call = | |
9729 | Expression::make_call(this->fn_->copy(), | |
9730 | (this->args_ == NULL | |
9731 | ? NULL | |
9732 | : this->args_->copy()), | |
9733 | this->is_varargs_, this->location()); | |
9734 | ||
9735 | if (this->varargs_are_lowered_) | |
9736 | call->set_varargs_are_lowered(); | |
9737 | return call; | |
9738 | } | |
9739 | ||
e440a328 | 9740 | // Return whether we have to use a temporary variable to ensure that |
9741 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9742 | // results then it will inevitably be executed last. |
e440a328 | 9743 | |
9744 | bool | |
9745 | Call_expression::do_must_eval_in_order() const | |
9746 | { | |
ceeb4318 | 9747 | return this->result_count() > 0; |
e440a328 | 9748 | } |
9749 | ||
e440a328 | 9750 | // Get the function and the first argument to use when calling an |
9751 | // interface method. | |
9752 | ||
2387f644 | 9753 | Expression* |
e440a328 | 9754 | Call_expression::interface_method_function( |
e440a328 | 9755 | Interface_field_reference_expression* interface_method, |
2387f644 | 9756 | Expression** first_arg_ptr) |
e440a328 | 9757 | { |
2387f644 | 9758 | *first_arg_ptr = interface_method->get_underlying_object(); |
9759 | return interface_method->get_function(); | |
e440a328 | 9760 | } |
9761 | ||
9762 | // Build the call expression. | |
9763 | ||
ea664253 | 9764 | Bexpression* |
9765 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 9766 | { |
2c809f8f | 9767 | if (this->call_ != NULL) |
ea664253 | 9768 | return this->call_; |
e440a328 | 9769 | |
9770 | Function_type* fntype = this->get_function_type(); | |
9771 | if (fntype == NULL) | |
ea664253 | 9772 | return context->backend()->error_expression(); |
e440a328 | 9773 | |
9774 | if (this->fn_->is_error_expression()) | |
ea664253 | 9775 | return context->backend()->error_expression(); |
e440a328 | 9776 | |
9777 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9778 | Location location = this->location(); |
e440a328 | 9779 | |
9780 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9781 | Interface_field_reference_expression* interface_method = |
9782 | this->fn_->interface_field_reference_expression(); | |
9783 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9784 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9785 | |
f8bdf81a | 9786 | bool has_closure_arg; |
8381eda7 | 9787 | if (has_closure) |
f8bdf81a | 9788 | has_closure_arg = true; |
8381eda7 | 9789 | else if (func != NULL) |
f8bdf81a | 9790 | has_closure_arg = false; |
8381eda7 | 9791 | else if (is_interface_method) |
f8bdf81a | 9792 | has_closure_arg = false; |
8381eda7 | 9793 | else |
f8bdf81a | 9794 | has_closure_arg = true; |
8381eda7 | 9795 | |
e440a328 | 9796 | int nargs; |
2c809f8f | 9797 | std::vector<Bexpression*> fn_args; |
e440a328 | 9798 | if (this->args_ == NULL || this->args_->empty()) |
9799 | { | |
f8bdf81a | 9800 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 9801 | if (nargs > 0) |
9802 | fn_args.resize(1); | |
e440a328 | 9803 | } |
09ea332d | 9804 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9805 | { | |
9806 | // Passing a receiver parameter. | |
9807 | go_assert(!is_interface_method | |
9808 | && fntype->is_method() | |
9809 | && this->args_->size() == 1); | |
f8bdf81a | 9810 | nargs = 1; |
2c809f8f | 9811 | fn_args.resize(1); |
ea664253 | 9812 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 9813 | } |
e440a328 | 9814 | else |
9815 | { | |
9816 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9817 | |
9818 | nargs = this->args_->size(); | |
09ea332d | 9819 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9820 | nargs += i; |
2c809f8f | 9821 | fn_args.resize(nargs); |
e440a328 | 9822 | |
9823 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9824 | Expression_list::const_iterator pe = this->args_->begin(); |
9825 | if (!is_interface_method && fntype->is_method()) | |
9826 | { | |
ea664253 | 9827 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 9828 | ++pe; |
9829 | ++i; | |
9830 | } | |
9831 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9832 | { |
c484d925 | 9833 | go_assert(pp != params->end()); |
2c809f8f | 9834 | Expression* arg = |
9835 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
9836 | location); | |
ea664253 | 9837 | fn_args[i] = arg->get_backend(context); |
e440a328 | 9838 | } |
c484d925 | 9839 | go_assert(pp == params->end()); |
f8bdf81a | 9840 | go_assert(i == nargs); |
e440a328 | 9841 | } |
9842 | ||
2c809f8f | 9843 | Expression* fn; |
9844 | Expression* closure = NULL; | |
8381eda7 | 9845 | if (func != NULL) |
9846 | { | |
9847 | Named_object* no = func->named_object(); | |
2c809f8f | 9848 | fn = Expression::make_func_code_reference(no, location); |
9849 | if (has_closure) | |
9850 | closure = func->closure(); | |
8381eda7 | 9851 | } |
09ea332d | 9852 | else if (!is_interface_method) |
8381eda7 | 9853 | { |
2c809f8f | 9854 | closure = this->fn_; |
9855 | ||
9856 | // The backend representation of this function type is a pointer | |
9857 | // to a struct whose first field is the actual function to call. | |
9858 | Type* pfntype = | |
9859 | Type::make_pointer_type( | |
9860 | Type::make_pointer_type(Type::make_void_type())); | |
9861 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
9862 | fn = Expression::make_unary(OPERATOR_MULT, fn, location); | |
9863 | } | |
e440a328 | 9864 | else |
cf609de4 | 9865 | { |
2387f644 | 9866 | Expression* first_arg; |
2c809f8f | 9867 | fn = this->interface_method_function(interface_method, &first_arg); |
ea664253 | 9868 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 9869 | } |
9870 | ||
1ecc6157 | 9871 | Bexpression* bclosure = NULL; |
9872 | if (has_closure_arg) | |
9873 | bclosure = closure->get_backend(context); | |
f8bdf81a | 9874 | else |
1ecc6157 | 9875 | go_assert(closure == NULL); |
f8bdf81a | 9876 | |
ea664253 | 9877 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 9878 | |
9879 | // When not calling a named function directly, use a type conversion | |
9880 | // in case the type of the function is a recursive type which refers | |
9881 | // to itself. We don't do this for an interface method because 1) | |
9882 | // an interface method never refers to itself, so we always have a | |
9883 | // function type here; 2) we pass an extra first argument to an | |
9884 | // interface method, so fntype is not correct. | |
9885 | if (func == NULL && !is_interface_method) | |
9886 | { | |
9887 | Btype* bft = fntype->get_backend_fntype(gogo); | |
9888 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
9889 | } | |
9890 | ||
1ecc6157 | 9891 | Bexpression* call = gogo->backend()->call_expression(bfn, fn_args, |
9892 | bclosure, location); | |
e440a328 | 9893 | |
2c809f8f | 9894 | if (this->results_ != NULL) |
e440a328 | 9895 | { |
2c809f8f | 9896 | go_assert(this->call_temp_ != NULL); |
9897 | Expression* call_ref = | |
9898 | Expression::make_temporary_reference(this->call_temp_, location); | |
ea664253 | 9899 | Bexpression* bcall_ref = call_ref->get_backend(context); |
2c809f8f | 9900 | Bstatement* assn_stmt = |
9901 | gogo->backend()->assignment_statement(bcall_ref, call, location); | |
e440a328 | 9902 | |
2c809f8f | 9903 | this->call_ = this->set_results(context, bcall_ref); |
e440a328 | 9904 | |
2c809f8f | 9905 | Bexpression* set_and_call = |
9906 | gogo->backend()->compound_expression(assn_stmt, this->call_, | |
9907 | location); | |
ea664253 | 9908 | return set_and_call; |
2c809f8f | 9909 | } |
e440a328 | 9910 | |
2c809f8f | 9911 | this->call_ = call; |
ea664253 | 9912 | return this->call_; |
e440a328 | 9913 | } |
9914 | ||
ceeb4318 | 9915 | // Set the result variables if this call returns multiple results. |
9916 | ||
2c809f8f | 9917 | Bexpression* |
9918 | Call_expression::set_results(Translate_context* context, Bexpression* call) | |
ceeb4318 | 9919 | { |
2c809f8f | 9920 | Gogo* gogo = context->gogo(); |
ceeb4318 | 9921 | |
2c809f8f | 9922 | Bexpression* results = NULL; |
b13c66cd | 9923 | Location loc = this->location(); |
2c809f8f | 9924 | |
ceeb4318 | 9925 | size_t rc = this->result_count(); |
2c809f8f | 9926 | for (size_t i = 0; i < rc; ++i) |
ceeb4318 | 9927 | { |
ceeb4318 | 9928 | Temporary_statement* temp = this->result(i); |
cd238b8d | 9929 | if (temp == NULL) |
9930 | { | |
9931 | go_assert(saw_errors()); | |
2c809f8f | 9932 | return gogo->backend()->error_expression(); |
cd238b8d | 9933 | } |
ceeb4318 | 9934 | Temporary_reference_expression* ref = |
9935 | Expression::make_temporary_reference(temp, loc); | |
9936 | ref->set_is_lvalue(); | |
ceeb4318 | 9937 | |
ea664253 | 9938 | Bexpression* result_ref = ref->get_backend(context); |
2c809f8f | 9939 | Bexpression* call_result = |
9940 | gogo->backend()->struct_field_expression(call, i, loc); | |
9941 | Bstatement* assn_stmt = | |
9942 | gogo->backend()->assignment_statement(result_ref, call_result, loc); | |
ceeb4318 | 9943 | |
2c809f8f | 9944 | Bexpression* result = |
9945 | gogo->backend()->compound_expression(assn_stmt, call_result, loc); | |
ceeb4318 | 9946 | |
2c809f8f | 9947 | if (results == NULL) |
9948 | results = result; | |
9949 | else | |
9950 | { | |
9951 | Bstatement* expr_stmt = gogo->backend()->expression_statement(result); | |
9952 | results = | |
9953 | gogo->backend()->compound_expression(expr_stmt, results, loc); | |
9954 | } | |
9955 | } | |
9956 | return results; | |
ceeb4318 | 9957 | } |
9958 | ||
d751bb78 | 9959 | // Dump ast representation for a call expressin. |
9960 | ||
9961 | void | |
9962 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9963 | { | |
9964 | this->fn_->dump_expression(ast_dump_context); | |
9965 | ast_dump_context->ostream() << "("; | |
9966 | if (args_ != NULL) | |
9967 | ast_dump_context->dump_expression_list(this->args_); | |
9968 | ||
9969 | ast_dump_context->ostream() << ") "; | |
9970 | } | |
9971 | ||
e440a328 | 9972 | // Make a call expression. |
9973 | ||
9974 | Call_expression* | |
9975 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9976 | Location location) |
e440a328 | 9977 | { |
9978 | return new Call_expression(fn, args, is_varargs, location); | |
9979 | } | |
9980 | ||
da244e59 | 9981 | // Class Call_result_expression. |
e440a328 | 9982 | |
9983 | // Traverse a call result. | |
9984 | ||
9985 | int | |
9986 | Call_result_expression::do_traverse(Traverse* traverse) | |
9987 | { | |
9988 | if (traverse->remember_expression(this->call_)) | |
9989 | { | |
9990 | // We have already traversed the call expression. | |
9991 | return TRAVERSE_CONTINUE; | |
9992 | } | |
9993 | return Expression::traverse(&this->call_, traverse); | |
9994 | } | |
9995 | ||
9996 | // Get the type. | |
9997 | ||
9998 | Type* | |
9999 | Call_result_expression::do_type() | |
10000 | { | |
425dd051 | 10001 | if (this->classification() == EXPRESSION_ERROR) |
10002 | return Type::make_error_type(); | |
10003 | ||
e440a328 | 10004 | // THIS->CALL_ can be replaced with a temporary reference due to |
10005 | // Call_expression::do_must_eval_in_order when there is an error. | |
10006 | Call_expression* ce = this->call_->call_expression(); | |
10007 | if (ce == NULL) | |
5e85f268 | 10008 | { |
10009 | this->set_is_error(); | |
10010 | return Type::make_error_type(); | |
10011 | } | |
e440a328 | 10012 | Function_type* fntype = ce->get_function_type(); |
10013 | if (fntype == NULL) | |
5e85f268 | 10014 | { |
e37658e2 | 10015 | if (ce->issue_error()) |
99b3f06f | 10016 | { |
10017 | if (!ce->fn()->type()->is_error()) | |
10018 | this->report_error(_("expected function")); | |
10019 | } | |
5e85f268 | 10020 | this->set_is_error(); |
10021 | return Type::make_error_type(); | |
10022 | } | |
e440a328 | 10023 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10024 | if (results == NULL || results->size() < 2) |
7b8d861f | 10025 | { |
ceeb4318 | 10026 | if (ce->issue_error()) |
10027 | this->report_error(_("number of results does not match " | |
10028 | "number of values")); | |
7b8d861f | 10029 | return Type::make_error_type(); |
10030 | } | |
e440a328 | 10031 | Typed_identifier_list::const_iterator pr = results->begin(); |
10032 | for (unsigned int i = 0; i < this->index_; ++i) | |
10033 | { | |
10034 | if (pr == results->end()) | |
425dd051 | 10035 | break; |
e440a328 | 10036 | ++pr; |
10037 | } | |
10038 | if (pr == results->end()) | |
425dd051 | 10039 | { |
ceeb4318 | 10040 | if (ce->issue_error()) |
10041 | this->report_error(_("number of results does not match " | |
10042 | "number of values")); | |
425dd051 | 10043 | return Type::make_error_type(); |
10044 | } | |
e440a328 | 10045 | return pr->type(); |
10046 | } | |
10047 | ||
425dd051 | 10048 | // Check the type. Just make sure that we trigger the warning in |
10049 | // do_type. | |
e440a328 | 10050 | |
10051 | void | |
10052 | Call_result_expression::do_check_types(Gogo*) | |
10053 | { | |
425dd051 | 10054 | this->type(); |
e440a328 | 10055 | } |
10056 | ||
10057 | // Determine the type. We have nothing to do here, but the 0 result | |
10058 | // needs to pass down to the caller. | |
10059 | ||
10060 | void | |
10061 | Call_result_expression::do_determine_type(const Type_context*) | |
10062 | { | |
fb94b0ca | 10063 | this->call_->determine_type_no_context(); |
e440a328 | 10064 | } |
10065 | ||
ea664253 | 10066 | // Return the backend representation. We just refer to the temporary set by the |
10067 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 10068 | // hard to evaluate the call at the right time. |
e440a328 | 10069 | |
ea664253 | 10070 | Bexpression* |
10071 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10072 | { |
ceeb4318 | 10073 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10074 | if (ce == NULL) |
10075 | { | |
10076 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 10077 | return context->backend()->error_expression(); |
cd238b8d | 10078 | } |
ceeb4318 | 10079 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 10080 | if (ts == NULL) |
10081 | { | |
10082 | go_assert(saw_errors()); | |
ea664253 | 10083 | return context->backend()->error_expression(); |
cd238b8d | 10084 | } |
ceeb4318 | 10085 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
ea664253 | 10086 | return ref->get_backend(context); |
e440a328 | 10087 | } |
10088 | ||
d751bb78 | 10089 | // Dump ast representation for a call result expression. |
10090 | ||
10091 | void | |
10092 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10093 | const | |
10094 | { | |
10095 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10096 | // (struct) and the fields are referenced instead. | |
10097 | ast_dump_context->ostream() << this->index_ << "@("; | |
10098 | ast_dump_context->dump_expression(this->call_); | |
10099 | ast_dump_context->ostream() << ")"; | |
10100 | } | |
10101 | ||
e440a328 | 10102 | // Make a reference to a single result of a call which returns |
10103 | // multiple results. | |
10104 | ||
10105 | Expression* | |
10106 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10107 | { | |
10108 | return new Call_result_expression(call, index); | |
10109 | } | |
10110 | ||
10111 | // Class Index_expression. | |
10112 | ||
10113 | // Traversal. | |
10114 | ||
10115 | int | |
10116 | Index_expression::do_traverse(Traverse* traverse) | |
10117 | { | |
10118 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10119 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10120 | || (this->end_ != NULL | |
acf2b673 | 10121 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
10122 | || (this->cap_ != NULL | |
10123 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 10124 | return TRAVERSE_EXIT; |
10125 | return TRAVERSE_CONTINUE; | |
10126 | } | |
10127 | ||
10128 | // Lower an index expression. This converts the generic index | |
10129 | // expression into an array index, a string index, or a map index. | |
10130 | ||
10131 | Expression* | |
ceeb4318 | 10132 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10133 | { |
b13c66cd | 10134 | Location location = this->location(); |
e440a328 | 10135 | Expression* left = this->left_; |
10136 | Expression* start = this->start_; | |
10137 | Expression* end = this->end_; | |
acf2b673 | 10138 | Expression* cap = this->cap_; |
e440a328 | 10139 | |
10140 | Type* type = left->type(); | |
5c13bd80 | 10141 | if (type->is_error()) |
d9f3743a | 10142 | { |
10143 | go_assert(saw_errors()); | |
10144 | return Expression::make_error(location); | |
10145 | } | |
b0cf7ddd | 10146 | else if (left->is_type_expression()) |
10147 | { | |
631d5788 | 10148 | go_error_at(location, "attempt to index type expression"); |
b0cf7ddd | 10149 | return Expression::make_error(location); |
10150 | } | |
e440a328 | 10151 | else if (type->array_type() != NULL) |
acf2b673 | 10152 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 10153 | else if (type->points_to() != NULL |
10154 | && type->points_to()->array_type() != NULL | |
411eb89e | 10155 | && !type->points_to()->is_slice_type()) |
e440a328 | 10156 | { |
10157 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
10158 | location); | |
38092374 | 10159 | |
10160 | // For an ordinary index into the array, the pointer will be | |
10161 | // dereferenced. For a slice it will not--the resulting slice | |
10162 | // will simply reuse the pointer, which is incorrect if that | |
10163 | // pointer is nil. | |
10164 | if (end != NULL || cap != NULL) | |
10165 | deref->issue_nil_check(); | |
10166 | ||
acf2b673 | 10167 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 10168 | } |
10169 | else if (type->is_string_type()) | |
acf2b673 | 10170 | { |
10171 | if (cap != NULL) | |
10172 | { | |
631d5788 | 10173 | go_error_at(location, "invalid 3-index slice of string"); |
acf2b673 | 10174 | return Expression::make_error(location); |
10175 | } | |
10176 | return Expression::make_string_index(left, start, end, location); | |
10177 | } | |
e440a328 | 10178 | else if (type->map_type() != NULL) |
10179 | { | |
acf2b673 | 10180 | if (end != NULL || cap != NULL) |
e440a328 | 10181 | { |
631d5788 | 10182 | go_error_at(location, "invalid slice of map"); |
e440a328 | 10183 | return Expression::make_error(location); |
10184 | } | |
0d5530d9 | 10185 | return Expression::make_map_index(left, start, location); |
e440a328 | 10186 | } |
10187 | else | |
10188 | { | |
631d5788 | 10189 | go_error_at(location, |
10190 | "attempt to index object which is not array, string, or map"); | |
e440a328 | 10191 | return Expression::make_error(location); |
10192 | } | |
10193 | } | |
10194 | ||
acf2b673 | 10195 | // Write an indexed expression |
10196 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 10197 | |
10198 | void | |
10199 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10200 | const Expression* expr, | |
10201 | const Expression* start, | |
acf2b673 | 10202 | const Expression* end, |
10203 | const Expression* cap) | |
d751bb78 | 10204 | { |
10205 | expr->dump_expression(ast_dump_context); | |
10206 | ast_dump_context->ostream() << "["; | |
10207 | start->dump_expression(ast_dump_context); | |
10208 | if (end != NULL) | |
10209 | { | |
10210 | ast_dump_context->ostream() << ":"; | |
10211 | end->dump_expression(ast_dump_context); | |
10212 | } | |
acf2b673 | 10213 | if (cap != NULL) |
10214 | { | |
10215 | ast_dump_context->ostream() << ":"; | |
10216 | cap->dump_expression(ast_dump_context); | |
10217 | } | |
d751bb78 | 10218 | ast_dump_context->ostream() << "]"; |
10219 | } | |
10220 | ||
10221 | // Dump ast representation for an index expression. | |
10222 | ||
10223 | void | |
10224 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10225 | const | |
10226 | { | |
10227 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 10228 | this->start_, this->end_, this->cap_); |
d751bb78 | 10229 | } |
10230 | ||
e440a328 | 10231 | // Make an index expression. |
10232 | ||
10233 | Expression* | |
10234 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 10235 | Expression* cap, Location location) |
e440a328 | 10236 | { |
acf2b673 | 10237 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 10238 | } |
10239 | ||
da244e59 | 10240 | // Class Array_index_expression. |
e440a328 | 10241 | |
10242 | // Array index traversal. | |
10243 | ||
10244 | int | |
10245 | Array_index_expression::do_traverse(Traverse* traverse) | |
10246 | { | |
10247 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10248 | return TRAVERSE_EXIT; | |
10249 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10250 | return TRAVERSE_EXIT; | |
10251 | if (this->end_ != NULL) | |
10252 | { | |
10253 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10254 | return TRAVERSE_EXIT; | |
10255 | } | |
acf2b673 | 10256 | if (this->cap_ != NULL) |
10257 | { | |
10258 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10259 | return TRAVERSE_EXIT; | |
10260 | } | |
e440a328 | 10261 | return TRAVERSE_CONTINUE; |
10262 | } | |
10263 | ||
10264 | // Return the type of an array index. | |
10265 | ||
10266 | Type* | |
10267 | Array_index_expression::do_type() | |
10268 | { | |
10269 | if (this->type_ == NULL) | |
10270 | { | |
10271 | Array_type* type = this->array_->type()->array_type(); | |
10272 | if (type == NULL) | |
10273 | this->type_ = Type::make_error_type(); | |
10274 | else if (this->end_ == NULL) | |
10275 | this->type_ = type->element_type(); | |
411eb89e | 10276 | else if (type->is_slice_type()) |
e440a328 | 10277 | { |
10278 | // A slice of a slice has the same type as the original | |
10279 | // slice. | |
10280 | this->type_ = this->array_->type()->deref(); | |
10281 | } | |
10282 | else | |
10283 | { | |
10284 | // A slice of an array is a slice. | |
10285 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10286 | } | |
10287 | } | |
10288 | return this->type_; | |
10289 | } | |
10290 | ||
10291 | // Set the type of an array index. | |
10292 | ||
10293 | void | |
10294 | Array_index_expression::do_determine_type(const Type_context*) | |
10295 | { | |
10296 | this->array_->determine_type_no_context(); | |
f77aa642 | 10297 | |
10298 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10299 | if (this->start_->is_constant()) | |
10300 | this->start_->determine_type(&index_context); | |
10301 | else | |
10302 | this->start_->determine_type_no_context(); | |
e440a328 | 10303 | if (this->end_ != NULL) |
f77aa642 | 10304 | { |
10305 | if (this->end_->is_constant()) | |
10306 | this->end_->determine_type(&index_context); | |
10307 | else | |
10308 | this->end_->determine_type_no_context(); | |
10309 | } | |
acf2b673 | 10310 | if (this->cap_ != NULL) |
f77aa642 | 10311 | { |
10312 | if (this->cap_->is_constant()) | |
10313 | this->cap_->determine_type(&index_context); | |
10314 | else | |
10315 | this->cap_->determine_type_no_context(); | |
10316 | } | |
e440a328 | 10317 | } |
10318 | ||
10319 | // Check types of an array index. | |
10320 | ||
10321 | void | |
d0a50ed8 | 10322 | Array_index_expression::do_check_types(Gogo* gogo) |
e440a328 | 10323 | { |
f6bc81e6 | 10324 | Numeric_constant nc; |
10325 | unsigned long v; | |
10326 | if (this->start_->type()->integer_type() == NULL | |
10327 | && !this->start_->type()->is_error() | |
10328 | && (!this->start_->numeric_constant_value(&nc) | |
10329 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10330 | this->report_error(_("index must be integer")); |
10331 | if (this->end_ != NULL | |
10332 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10333 | && !this->end_->type()->is_error() |
10334 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10335 | && !this->end_->is_error_expression() |
10336 | && (!this->end_->numeric_constant_value(&nc) | |
10337 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10338 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10339 | if (this->cap_ != NULL |
10340 | && this->cap_->type()->integer_type() == NULL | |
10341 | && !this->cap_->type()->is_error() | |
10342 | && !this->cap_->is_nil_expression() | |
10343 | && !this->cap_->is_error_expression() | |
10344 | && (!this->cap_->numeric_constant_value(&nc) | |
10345 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10346 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10347 | |
10348 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10349 | if (array_type == NULL) |
10350 | { | |
c484d925 | 10351 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10352 | return; |
10353 | } | |
e440a328 | 10354 | |
10355 | unsigned int int_bits = | |
10356 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10357 | ||
0c77715b | 10358 | Numeric_constant lvalnc; |
e440a328 | 10359 | mpz_t lval; |
e440a328 | 10360 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10361 | && array_type->length()->numeric_constant_value(&lvalnc) |
10362 | && lvalnc.to_int(&lval)); | |
10363 | Numeric_constant inc; | |
e440a328 | 10364 | mpz_t ival; |
0bd5d859 | 10365 | bool ival_valid = false; |
0c77715b | 10366 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10367 | { |
0bd5d859 | 10368 | ival_valid = true; |
e440a328 | 10369 | if (mpz_sgn(ival) < 0 |
10370 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10371 | || (lval_valid | |
10372 | && (this->end_ == NULL | |
10373 | ? mpz_cmp(ival, lval) >= 0 | |
10374 | : mpz_cmp(ival, lval) > 0))) | |
10375 | { | |
631d5788 | 10376 | go_error_at(this->start_->location(), "array index out of bounds"); |
e440a328 | 10377 | this->set_is_error(); |
10378 | } | |
10379 | } | |
10380 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10381 | { | |
0c77715b | 10382 | Numeric_constant enc; |
10383 | mpz_t eval; | |
acf2b673 | 10384 | bool eval_valid = false; |
0c77715b | 10385 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10386 | { |
acf2b673 | 10387 | eval_valid = true; |
0c77715b | 10388 | if (mpz_sgn(eval) < 0 |
10389 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10390 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10391 | { |
631d5788 | 10392 | go_error_at(this->end_->location(), "array index out of bounds"); |
e440a328 | 10393 | this->set_is_error(); |
10394 | } | |
0bd5d859 | 10395 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10396 | this->report_error(_("inverted slice range")); | |
e440a328 | 10397 | } |
acf2b673 | 10398 | |
10399 | Numeric_constant cnc; | |
10400 | mpz_t cval; | |
10401 | if (this->cap_ != NULL | |
10402 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10403 | { | |
10404 | if (mpz_sgn(cval) < 0 | |
10405 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10406 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10407 | { | |
631d5788 | 10408 | go_error_at(this->cap_->location(), "array index out of bounds"); |
acf2b673 | 10409 | this->set_is_error(); |
10410 | } | |
10411 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10412 | { | |
631d5788 | 10413 | go_error_at(this->cap_->location(), |
10414 | "invalid slice index: capacity less than start"); | |
acf2b673 | 10415 | this->set_is_error(); |
10416 | } | |
10417 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10418 | { | |
631d5788 | 10419 | go_error_at(this->cap_->location(), |
10420 | "invalid slice index: capacity less than length"); | |
acf2b673 | 10421 | this->set_is_error(); |
10422 | } | |
10423 | mpz_clear(cval); | |
10424 | } | |
10425 | ||
10426 | if (eval_valid) | |
10427 | mpz_clear(eval); | |
e440a328 | 10428 | } |
0bd5d859 | 10429 | if (ival_valid) |
10430 | mpz_clear(ival); | |
0c77715b | 10431 | if (lval_valid) |
10432 | mpz_clear(lval); | |
e440a328 | 10433 | |
10434 | // A slice of an array requires an addressable array. A slice of a | |
10435 | // slice is always possible. | |
411eb89e | 10436 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10437 | { |
10438 | if (!this->array_->is_addressable()) | |
8da39c3b | 10439 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10440 | else |
d0a50ed8 | 10441 | { |
10442 | bool escapes = true; | |
10443 | ||
10444 | // When compiling the runtime, a slice operation does not | |
10445 | // cause local variables to escape. When escape analysis | |
10446 | // becomes the default, this should be changed to make it an | |
10447 | // error if we have a slice operation that escapes. | |
10448 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
10449 | escapes = false; | |
10450 | ||
10451 | this->array_->address_taken(escapes); | |
10452 | } | |
88ec30c8 | 10453 | } |
e440a328 | 10454 | } |
10455 | ||
2c809f8f | 10456 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10457 | |
10458 | Expression* | |
10459 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10460 | Statement_inserter* inserter) | |
10461 | { | |
10462 | Location loc = this->location(); | |
5bf8be8b | 10463 | Expression* array = this->array_; |
10464 | Expression* start = this->start_; | |
10465 | Expression* end = this->end_; | |
10466 | Expression* cap = this->cap_; | |
10467 | if (array->is_error_expression() | |
10468 | || array->type()->is_error_type() | |
10469 | || start->is_error_expression() | |
10470 | || start->type()->is_error_type() | |
10471 | || (end != NULL | |
10472 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10473 | || (cap != NULL | |
10474 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10475 | { | |
10476 | go_assert(saw_errors()); | |
10477 | return Expression::make_error(loc); | |
10478 | } | |
10479 | ||
2c809f8f | 10480 | Temporary_statement* temp; |
5bf8be8b | 10481 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10482 | { |
5bf8be8b | 10483 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10484 | inserter->insert(temp); |
10485 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10486 | } | |
5bf8be8b | 10487 | if (!start->is_variable()) |
2c809f8f | 10488 | { |
5bf8be8b | 10489 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10490 | inserter->insert(temp); |
10491 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10492 | } | |
5bf8be8b | 10493 | if (end != NULL |
10494 | && !end->is_nil_expression() | |
10495 | && !end->is_variable()) | |
2c809f8f | 10496 | { |
5bf8be8b | 10497 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10498 | inserter->insert(temp); |
10499 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10500 | } | |
5bf8be8b | 10501 | if (cap!= NULL && !cap->is_variable()) |
2c809f8f | 10502 | { |
5bf8be8b | 10503 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10504 | inserter->insert(temp); |
10505 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10506 | } | |
10507 | ||
35a54f17 | 10508 | return this; |
10509 | } | |
10510 | ||
e440a328 | 10511 | // Return whether this expression is addressable. |
10512 | ||
10513 | bool | |
10514 | Array_index_expression::do_is_addressable() const | |
10515 | { | |
10516 | // A slice expression is not addressable. | |
10517 | if (this->end_ != NULL) | |
10518 | return false; | |
10519 | ||
10520 | // An index into a slice is addressable. | |
411eb89e | 10521 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10522 | return true; |
10523 | ||
10524 | // An index into an array is addressable if the array is | |
10525 | // addressable. | |
10526 | return this->array_->is_addressable(); | |
10527 | } | |
10528 | ||
ea664253 | 10529 | // Get the backend representation for an array index. |
e440a328 | 10530 | |
ea664253 | 10531 | Bexpression* |
10532 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10533 | { |
e440a328 | 10534 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10535 | if (array_type == NULL) |
10536 | { | |
c484d925 | 10537 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10538 | return context->backend()->error_expression(); |
d8cd8e2d | 10539 | } |
35a54f17 | 10540 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10541 | |
2c809f8f | 10542 | Location loc = this->location(); |
10543 | Gogo* gogo = context->gogo(); | |
10544 | ||
6dfedc16 | 10545 | Type* int_type = Type::lookup_integer_type("int"); |
10546 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10547 | |
2c809f8f | 10548 | // We need to convert the length and capacity to the Go "int" type here |
10549 | // because the length of a fixed-length array could be of type "uintptr" | |
10550 | // and gimple disallows binary operations between "uintptr" and other | |
10551 | // integer types. FIXME. | |
10552 | Bexpression* length = NULL; | |
a04bfdfc | 10553 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10554 | { | |
35a54f17 | 10555 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10556 | length = len->get_backend(context); |
2c809f8f | 10557 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10558 | } |
10559 | ||
2c809f8f | 10560 | Bexpression* capacity = NULL; |
a04bfdfc | 10561 | if (this->end_ != NULL) |
10562 | { | |
35a54f17 | 10563 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10564 | capacity = cap->get_backend(context); |
2c809f8f | 10565 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10566 | } |
10567 | ||
2c809f8f | 10568 | Bexpression* cap_arg = capacity; |
acf2b673 | 10569 | if (this->cap_ != NULL) |
10570 | { | |
ea664253 | 10571 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10572 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10573 | } |
10574 | ||
2c809f8f | 10575 | if (length == NULL) |
10576 | length = cap_arg; | |
e440a328 | 10577 | |
10578 | int code = (array_type->length() != NULL | |
10579 | ? (this->end_ == NULL | |
10580 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10581 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10582 | : (this->end_ == NULL | |
10583 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10584 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10585 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10586 | |
6dfedc16 | 10587 | if (this->start_->type()->integer_type() == NULL |
10588 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10589 | { | |
10590 | go_assert(saw_errors()); | |
10591 | return context->backend()->error_expression(); | |
10592 | } | |
d9f3743a | 10593 | |
ea664253 | 10594 | Bexpression* bad_index = |
d9f3743a | 10595 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 10596 | |
ea664253 | 10597 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10598 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10599 | Bexpression* start_too_large = | |
10600 | gogo->backend()->binary_expression((this->end_ == NULL | |
10601 | ? OPERATOR_GE | |
10602 | : OPERATOR_GT), | |
10603 | start, | |
10604 | (this->end_ == NULL | |
10605 | ? length | |
10606 | : capacity), | |
10607 | loc); | |
10608 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10609 | bad_index, loc); | |
e440a328 | 10610 | |
10611 | if (this->end_ == NULL) | |
10612 | { | |
10613 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10614 | // wrap the index check into START. |
10615 | start = | |
10616 | gogo->backend()->conditional_expression(int_btype, bad_index, | |
10617 | crash, start, loc); | |
e440a328 | 10618 | |
2c809f8f | 10619 | Bexpression* ret; |
e440a328 | 10620 | if (array_type->length() != NULL) |
10621 | { | |
ea664253 | 10622 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10623 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10624 | } |
10625 | else | |
10626 | { | |
2c809f8f | 10627 | // Slice. |
10628 | Expression* valptr = | |
35a54f17 | 10629 | array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10630 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 10631 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 10632 | |
10633 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
10634 | Btype* ele_btype = ele_type->get_backend(gogo); | |
10635 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 10636 | } |
ea664253 | 10637 | return ret; |
e440a328 | 10638 | } |
10639 | ||
10640 | // Array slice. | |
10641 | ||
acf2b673 | 10642 | if (this->cap_ != NULL) |
10643 | { | |
2c809f8f | 10644 | Bexpression* bounds_bcheck = |
ea664253 | 10645 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 10646 | bad_index = |
10647 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10648 | bad_index, loc); | |
10649 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
10650 | ||
10651 | Bexpression* cap_too_small = | |
10652 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
10653 | Bexpression* cap_too_large = | |
10654 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
10655 | Bexpression* bad_cap = | |
10656 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
10657 | cap_too_large, loc); | |
10658 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
10659 | bad_index, loc); | |
10660 | } | |
10661 | ||
10662 | Bexpression* end; | |
e440a328 | 10663 | if (this->end_->is_nil_expression()) |
2c809f8f | 10664 | end = length; |
e440a328 | 10665 | else |
10666 | { | |
2c809f8f | 10667 | Bexpression* bounds_bcheck = |
ea664253 | 10668 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 10669 | |
2c809f8f | 10670 | bad_index = |
10671 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10672 | bad_index, loc); | |
e440a328 | 10673 | |
ea664253 | 10674 | end = this->end_->get_backend(context); |
2c809f8f | 10675 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
10676 | Bexpression* end_too_small = | |
10677 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
10678 | Bexpression* end_too_large = | |
10679 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
10680 | Bexpression* bad_end = | |
10681 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
10682 | end_too_large, loc); | |
10683 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
10684 | bad_index, loc); | |
e440a328 | 10685 | } |
10686 | ||
35a54f17 | 10687 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10688 | Bexpression* val = valptr->get_backend(context); |
2c809f8f | 10689 | val = gogo->backend()->pointer_offset_expression(val, start, loc); |
e440a328 | 10690 | |
2c809f8f | 10691 | Bexpression* result_length = |
10692 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 10693 | |
2c809f8f | 10694 | Bexpression* result_capacity = |
10695 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 10696 | |
2c809f8f | 10697 | Btype* struct_btype = this->type()->get_backend(gogo); |
10698 | std::vector<Bexpression*> init; | |
10699 | init.push_back(val); | |
10700 | init.push_back(result_length); | |
10701 | init.push_back(result_capacity); | |
e440a328 | 10702 | |
2c809f8f | 10703 | Bexpression* ctor = |
10704 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
ea664253 | 10705 | return gogo->backend()->conditional_expression(struct_btype, bad_index, |
10706 | crash, ctor, loc); | |
e440a328 | 10707 | } |
10708 | ||
d751bb78 | 10709 | // Dump ast representation for an array index expression. |
10710 | ||
10711 | void | |
10712 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10713 | const | |
10714 | { | |
10715 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 10716 | this->start_, this->end_, this->cap_); |
d751bb78 | 10717 | } |
10718 | ||
acf2b673 | 10719 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 10720 | |
10721 | Expression* | |
10722 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 10723 | Expression* end, Expression* cap, |
10724 | Location location) | |
e440a328 | 10725 | { |
acf2b673 | 10726 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 10727 | } |
10728 | ||
50075d74 | 10729 | // Class String_index_expression. |
e440a328 | 10730 | |
10731 | // String index traversal. | |
10732 | ||
10733 | int | |
10734 | String_index_expression::do_traverse(Traverse* traverse) | |
10735 | { | |
10736 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10737 | return TRAVERSE_EXIT; | |
10738 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10739 | return TRAVERSE_EXIT; | |
10740 | if (this->end_ != NULL) | |
10741 | { | |
10742 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10743 | return TRAVERSE_EXIT; | |
10744 | } | |
10745 | return TRAVERSE_CONTINUE; | |
10746 | } | |
10747 | ||
2c809f8f | 10748 | Expression* |
10749 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
10750 | Statement_inserter* inserter) | |
e440a328 | 10751 | { |
2c809f8f | 10752 | Location loc = this->location(); |
5bf8be8b | 10753 | Expression* string = this->string_; |
10754 | Expression* start = this->start_; | |
10755 | Expression* end = this->end_; | |
10756 | if (string->is_error_expression() | |
10757 | || string->type()->is_error_type() | |
10758 | || start->is_error_expression() | |
10759 | || start->type()->is_error_type() | |
10760 | || (end != NULL | |
10761 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
10762 | { | |
10763 | go_assert(saw_errors()); | |
10764 | return Expression::make_error(loc); | |
10765 | } | |
10766 | ||
10767 | Temporary_statement* temp; | |
2c809f8f | 10768 | if (!this->string_->is_variable()) |
10769 | { | |
10770 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
10771 | inserter->insert(temp); | |
10772 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
10773 | } | |
10774 | if (!this->start_->is_variable()) | |
10775 | { | |
10776 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
10777 | inserter->insert(temp); | |
10778 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10779 | } | |
10780 | if (this->end_ != NULL | |
10781 | && !this->end_->is_nil_expression() | |
10782 | && !this->end_->is_variable()) | |
10783 | { | |
10784 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
10785 | inserter->insert(temp); | |
10786 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10787 | } | |
10788 | ||
10789 | return this; | |
10790 | } | |
10791 | ||
10792 | // Return the type of a string index. | |
10793 | ||
10794 | Type* | |
10795 | String_index_expression::do_type() | |
10796 | { | |
10797 | if (this->end_ == NULL) | |
10798 | return Type::lookup_integer_type("uint8"); | |
10799 | else | |
10800 | return this->string_->type(); | |
10801 | } | |
10802 | ||
10803 | // Determine the type of a string index. | |
10804 | ||
10805 | void | |
10806 | String_index_expression::do_determine_type(const Type_context*) | |
10807 | { | |
10808 | this->string_->determine_type_no_context(); | |
f77aa642 | 10809 | |
10810 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10811 | if (this->start_->is_constant()) | |
10812 | this->start_->determine_type(&index_context); | |
10813 | else | |
10814 | this->start_->determine_type_no_context(); | |
e440a328 | 10815 | if (this->end_ != NULL) |
f77aa642 | 10816 | { |
10817 | if (this->end_->is_constant()) | |
10818 | this->end_->determine_type(&index_context); | |
10819 | else | |
10820 | this->end_->determine_type_no_context(); | |
10821 | } | |
e440a328 | 10822 | } |
10823 | ||
10824 | // Check types of a string index. | |
10825 | ||
10826 | void | |
10827 | String_index_expression::do_check_types(Gogo*) | |
10828 | { | |
acdc230d | 10829 | Numeric_constant nc; |
10830 | unsigned long v; | |
10831 | if (this->start_->type()->integer_type() == NULL | |
10832 | && !this->start_->type()->is_error() | |
10833 | && (!this->start_->numeric_constant_value(&nc) | |
10834 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10835 | this->report_error(_("index must be integer")); |
10836 | if (this->end_ != NULL | |
10837 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 10838 | && !this->end_->type()->is_error() |
10839 | && !this->end_->is_nil_expression() | |
10840 | && !this->end_->is_error_expression() | |
10841 | && (!this->end_->numeric_constant_value(&nc) | |
10842 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10843 | this->report_error(_("slice end must be integer")); |
10844 | ||
10845 | std::string sval; | |
10846 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10847 | ||
0c77715b | 10848 | Numeric_constant inc; |
e440a328 | 10849 | mpz_t ival; |
0bd5d859 | 10850 | bool ival_valid = false; |
0c77715b | 10851 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10852 | { |
0bd5d859 | 10853 | ival_valid = true; |
e440a328 | 10854 | if (mpz_sgn(ival) < 0 |
b10f32fb | 10855 | || (sval_valid |
10856 | && (this->end_ == NULL | |
10857 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
10858 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 10859 | { |
631d5788 | 10860 | go_error_at(this->start_->location(), "string index out of bounds"); |
e440a328 | 10861 | this->set_is_error(); |
10862 | } | |
10863 | } | |
10864 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10865 | { | |
0c77715b | 10866 | Numeric_constant enc; |
10867 | mpz_t eval; | |
10868 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10869 | { |
0c77715b | 10870 | if (mpz_sgn(eval) < 0 |
10871 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10872 | { |
631d5788 | 10873 | go_error_at(this->end_->location(), "string index out of bounds"); |
e440a328 | 10874 | this->set_is_error(); |
10875 | } | |
0bd5d859 | 10876 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10877 | this->report_error(_("inverted slice range")); | |
0c77715b | 10878 | mpz_clear(eval); |
e440a328 | 10879 | } |
10880 | } | |
0bd5d859 | 10881 | if (ival_valid) |
10882 | mpz_clear(ival); | |
e440a328 | 10883 | } |
10884 | ||
ea664253 | 10885 | // Get the backend representation for a string index. |
e440a328 | 10886 | |
ea664253 | 10887 | Bexpression* |
10888 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10889 | { |
b13c66cd | 10890 | Location loc = this->location(); |
2c809f8f | 10891 | Expression* string_arg = this->string_; |
10892 | if (this->string_->type()->points_to() != NULL) | |
10893 | string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc); | |
e440a328 | 10894 | |
2c809f8f | 10895 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 10896 | |
2c809f8f | 10897 | int code = (this->end_ == NULL |
10898 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10899 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 10900 | |
2c809f8f | 10901 | Gogo* gogo = context->gogo(); |
ea664253 | 10902 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 10903 | |
10904 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 10905 | |
2c809f8f | 10906 | // It is possible that an error occurred earlier because the start index |
10907 | // cannot be represented as an integer type. In this case, we shouldn't | |
10908 | // try casting the starting index into an integer since | |
10909 | // Type_conversion_expression will fail to get the backend representation. | |
10910 | // FIXME. | |
10911 | if (this->start_->type()->integer_type() == NULL | |
10912 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10913 | { | |
10914 | go_assert(saw_errors()); | |
ea664253 | 10915 | return context->backend()->error_expression(); |
2c809f8f | 10916 | } |
e440a328 | 10917 | |
2c809f8f | 10918 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
e440a328 | 10919 | |
2c809f8f | 10920 | if (this->end_ == NULL) |
10921 | { | |
10922 | Expression* length = | |
10923 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 10924 | |
2c809f8f | 10925 | Expression* start_too_large = |
10926 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
10927 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
10928 | bad_index, loc); | |
10929 | Expression* bytes = | |
10930 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 10931 | |
ea664253 | 10932 | Bexpression* bstart = start->get_backend(context); |
10933 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 10934 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 10935 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
10936 | Bexpression* index = | |
10937 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 10938 | |
2c809f8f | 10939 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 10940 | Bexpression* index_error = bad_index->get_backend(context); |
10941 | return gogo->backend()->conditional_expression(byte_btype, index_error, | |
10942 | crash, index, loc); | |
2c809f8f | 10943 | } |
10944 | ||
10945 | Expression* end = NULL; | |
10946 | if (this->end_->is_nil_expression()) | |
e67508fa | 10947 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 10948 | else |
10949 | { | |
2c809f8f | 10950 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
10951 | bad_index = | |
10952 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
10953 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 10954 | } |
2c809f8f | 10955 | |
10956 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
10957 | string_arg, start, end); | |
ea664253 | 10958 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 10959 | |
10960 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 10961 | Bexpression* index_error = bad_index->get_backend(context); |
10962 | return gogo->backend()->conditional_expression(str_btype, index_error, | |
10963 | crash, bstrslice, loc); | |
e440a328 | 10964 | } |
10965 | ||
d751bb78 | 10966 | // Dump ast representation for a string index expression. |
10967 | ||
10968 | void | |
10969 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10970 | const | |
10971 | { | |
acf2b673 | 10972 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
10973 | this->start_, this->end_, NULL); | |
d751bb78 | 10974 | } |
10975 | ||
e440a328 | 10976 | // Make a string index expression. END may be NULL. |
10977 | ||
10978 | Expression* | |
10979 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10980 | Expression* end, Location location) |
e440a328 | 10981 | { |
10982 | return new String_index_expression(string, start, end, location); | |
10983 | } | |
10984 | ||
10985 | // Class Map_index. | |
10986 | ||
10987 | // Get the type of the map. | |
10988 | ||
10989 | Map_type* | |
10990 | Map_index_expression::get_map_type() const | |
10991 | { | |
0d5530d9 | 10992 | Map_type* mt = this->map_->type()->map_type(); |
c7524fae | 10993 | if (mt == NULL) |
c484d925 | 10994 | go_assert(saw_errors()); |
e440a328 | 10995 | return mt; |
10996 | } | |
10997 | ||
10998 | // Map index traversal. | |
10999 | ||
11000 | int | |
11001 | Map_index_expression::do_traverse(Traverse* traverse) | |
11002 | { | |
11003 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11004 | return TRAVERSE_EXIT; | |
11005 | return Expression::traverse(&this->index_, traverse); | |
11006 | } | |
11007 | ||
2c809f8f | 11008 | // We need to pass in a pointer to the key, so flatten the index into a |
11009 | // temporary variable if it isn't already. The value pointer will be | |
11010 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
11011 | // recomputation. | |
11012 | ||
11013 | Expression* | |
91c0fd76 | 11014 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 11015 | Statement_inserter* inserter) |
11016 | { | |
91c0fd76 | 11017 | Location loc = this->location(); |
2c809f8f | 11018 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 11019 | if (this->index()->is_error_expression() |
11020 | || this->index()->type()->is_error_type() | |
11021 | || mt->is_error_type()) | |
11022 | { | |
11023 | go_assert(saw_errors()); | |
11024 | return Expression::make_error(loc); | |
11025 | } | |
11026 | ||
91c0fd76 | 11027 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
11028 | { | |
11029 | if (this->index_->type()->interface_type() != NULL | |
11030 | && !this->index_->is_variable()) | |
11031 | { | |
11032 | Temporary_statement* temp = | |
11033 | Statement::make_temporary(NULL, this->index_, loc); | |
11034 | inserter->insert(temp); | |
11035 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
11036 | } | |
11037 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
11038 | this->index_, loc); | |
11039 | } | |
2c809f8f | 11040 | |
11041 | if (!this->index_->is_variable()) | |
11042 | { | |
11043 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 11044 | loc); |
2c809f8f | 11045 | inserter->insert(temp); |
91c0fd76 | 11046 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11047 | } |
11048 | ||
11049 | if (this->value_pointer_ == NULL) | |
0d5530d9 | 11050 | this->get_value_pointer(gogo); |
5bf8be8b | 11051 | if (this->value_pointer_->is_error_expression() |
11052 | || this->value_pointer_->type()->is_error_type()) | |
11053 | return Expression::make_error(loc); | |
2c809f8f | 11054 | if (!this->value_pointer_->is_variable()) |
11055 | { | |
11056 | Temporary_statement* temp = | |
91c0fd76 | 11057 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 11058 | inserter->insert(temp); |
91c0fd76 | 11059 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11060 | } |
11061 | ||
11062 | return this; | |
11063 | } | |
11064 | ||
e440a328 | 11065 | // Return the type of a map index. |
11066 | ||
11067 | Type* | |
11068 | Map_index_expression::do_type() | |
11069 | { | |
c7524fae | 11070 | Map_type* mt = this->get_map_type(); |
11071 | if (mt == NULL) | |
11072 | return Type::make_error_type(); | |
0d5530d9 | 11073 | return mt->val_type(); |
e440a328 | 11074 | } |
11075 | ||
11076 | // Fix the type of a map index. | |
11077 | ||
11078 | void | |
11079 | Map_index_expression::do_determine_type(const Type_context*) | |
11080 | { | |
11081 | this->map_->determine_type_no_context(); | |
c7524fae | 11082 | Map_type* mt = this->get_map_type(); |
11083 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11084 | Type_context subcontext(key_type, false); | |
e440a328 | 11085 | this->index_->determine_type(&subcontext); |
11086 | } | |
11087 | ||
11088 | // Check types of a map index. | |
11089 | ||
11090 | void | |
11091 | Map_index_expression::do_check_types(Gogo*) | |
11092 | { | |
11093 | std::string reason; | |
c7524fae | 11094 | Map_type* mt = this->get_map_type(); |
11095 | if (mt == NULL) | |
11096 | return; | |
11097 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11098 | { |
11099 | if (reason.empty()) | |
11100 | this->report_error(_("incompatible type for map index")); | |
11101 | else | |
11102 | { | |
631d5788 | 11103 | go_error_at(this->location(), "incompatible type for map index (%s)", |
11104 | reason.c_str()); | |
e440a328 | 11105 | this->set_is_error(); |
11106 | } | |
11107 | } | |
11108 | } | |
11109 | ||
ea664253 | 11110 | // Get the backend representation for a map index. |
e440a328 | 11111 | |
ea664253 | 11112 | Bexpression* |
11113 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11114 | { |
11115 | Map_type* type = this->get_map_type(); | |
c7524fae | 11116 | if (type == NULL) |
2c809f8f | 11117 | { |
11118 | go_assert(saw_errors()); | |
ea664253 | 11119 | return context->backend()->error_expression(); |
2c809f8f | 11120 | } |
e440a328 | 11121 | |
2c809f8f | 11122 | go_assert(this->value_pointer_ != NULL |
11123 | && this->value_pointer_->is_variable()); | |
e440a328 | 11124 | |
0d5530d9 | 11125 | Expression* val = Expression::make_unary(OPERATOR_MULT, this->value_pointer_, |
11126 | this->location()); | |
11127 | return val->get_backend(context); | |
e440a328 | 11128 | } |
11129 | ||
0d5530d9 | 11130 | // Get an expression for the map index. This returns an expression |
11131 | // that evaluates to a pointer to a value. If the key is not in the | |
11132 | // map, the pointer will point to a zero value. | |
e440a328 | 11133 | |
2c809f8f | 11134 | Expression* |
0d5530d9 | 11135 | Map_index_expression::get_value_pointer(Gogo* gogo) |
e440a328 | 11136 | { |
2c809f8f | 11137 | if (this->value_pointer_ == NULL) |
746d2e73 | 11138 | { |
2c809f8f | 11139 | Map_type* type = this->get_map_type(); |
11140 | if (type == NULL) | |
746d2e73 | 11141 | { |
2c809f8f | 11142 | go_assert(saw_errors()); |
11143 | return Expression::make_error(this->location()); | |
746d2e73 | 11144 | } |
e440a328 | 11145 | |
2c809f8f | 11146 | Location loc = this->location(); |
11147 | Expression* map_ref = this->map_; | |
e440a328 | 11148 | |
0d5530d9 | 11149 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, |
11150 | this->index_, | |
2c809f8f | 11151 | loc); |
0d5530d9 | 11152 | |
11153 | Expression* zero = type->fat_zero_value(gogo); | |
11154 | ||
11155 | Expression* map_index; | |
11156 | ||
11157 | if (zero == NULL) | |
11158 | map_index = | |
11159 | Runtime::make_call(Runtime::MAPACCESS1, loc, 3, | |
11160 | Expression::make_type_descriptor(type, loc), | |
11161 | map_ref, index_ptr); | |
11162 | else | |
11163 | map_index = | |
11164 | Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, | |
11165 | Expression::make_type_descriptor(type, loc), | |
11166 | map_ref, index_ptr, zero); | |
2c809f8f | 11167 | |
11168 | Type* val_type = type->val_type(); | |
11169 | this->value_pointer_ = | |
11170 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
11171 | map_index, this->location()); | |
11172 | } | |
0d5530d9 | 11173 | |
2c809f8f | 11174 | return this->value_pointer_; |
e440a328 | 11175 | } |
11176 | ||
d751bb78 | 11177 | // Dump ast representation for a map index expression |
11178 | ||
11179 | void | |
11180 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11181 | const | |
11182 | { | |
acf2b673 | 11183 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
11184 | this->index_, NULL, NULL); | |
d751bb78 | 11185 | } |
11186 | ||
e440a328 | 11187 | // Make a map index expression. |
11188 | ||
11189 | Map_index_expression* | |
11190 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11191 | Location location) |
e440a328 | 11192 | { |
11193 | return new Map_index_expression(map, index, location); | |
11194 | } | |
11195 | ||
11196 | // Class Field_reference_expression. | |
11197 | ||
149eabc5 | 11198 | // Lower a field reference expression. There is nothing to lower, but |
11199 | // this is where we generate the tracking information for fields with | |
11200 | // the magic go:"track" tag. | |
11201 | ||
11202 | Expression* | |
11203 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11204 | Statement_inserter* inserter, int) | |
11205 | { | |
11206 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11207 | if (struct_type == NULL) | |
11208 | { | |
11209 | // Error will be reported elsewhere. | |
11210 | return this; | |
11211 | } | |
11212 | const Struct_field* field = struct_type->field(this->field_index_); | |
11213 | if (field == NULL) | |
11214 | return this; | |
11215 | if (!field->has_tag()) | |
11216 | return this; | |
11217 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11218 | return this; | |
11219 | ||
604e278d | 11220 | // References from functions generated by the compiler don't count. |
c6292d1d | 11221 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 11222 | return this; |
11223 | ||
149eabc5 | 11224 | // We have found a reference to a tracked field. Build a call to |
11225 | // the runtime function __go_fieldtrack with a string that describes | |
11226 | // the field. FIXME: We should only call this once per referenced | |
11227 | // field per function, not once for each reference to the field. | |
11228 | ||
11229 | if (this->called_fieldtrack_) | |
11230 | return this; | |
11231 | this->called_fieldtrack_ = true; | |
11232 | ||
11233 | Location loc = this->location(); | |
11234 | ||
11235 | std::string s = "fieldtrack \""; | |
11236 | Named_type* nt = this->expr_->type()->named_type(); | |
11237 | if (nt == NULL || nt->named_object()->package() == NULL) | |
11238 | s.append(gogo->pkgpath()); | |
11239 | else | |
11240 | s.append(nt->named_object()->package()->pkgpath()); | |
11241 | s.push_back('.'); | |
11242 | if (nt != NULL) | |
5c29ad36 | 11243 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11244 | s.push_back('.'); |
11245 | s.append(field->field_name()); | |
11246 | s.push_back('"'); | |
11247 | ||
11248 | // We can't use a string here, because internally a string holds a | |
11249 | // pointer to the actual bytes; when the linker garbage collects the | |
11250 | // string, it won't garbage collect the bytes. So we use a | |
11251 | // [...]byte. | |
11252 | ||
e67508fa | 11253 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 11254 | |
11255 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
11256 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
11257 | ||
11258 | Expression_list* bytes = new Expression_list(); | |
11259 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11260 | { | |
e67508fa | 11261 | unsigned char c = static_cast<unsigned char>(*p); |
11262 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 11263 | } |
11264 | ||
11265 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11266 | bytes, false, loc); |
149eabc5 | 11267 | |
11268 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11269 | ||
11270 | static int count; | |
11271 | char buf[50]; | |
11272 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11273 | ++count; | |
11274 | ||
11275 | Named_object* no = gogo->add_variable(buf, var); | |
11276 | e = Expression::make_var_reference(no, loc); | |
11277 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11278 | ||
11279 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 11280 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 11281 | inserter->insert(Statement::make_statement(call, false)); |
11282 | ||
11283 | // Put this function, and the global variable we just created, into | |
11284 | // unique sections. This will permit the linker to garbage collect | |
11285 | // them if they are not referenced. The effect is that the only | |
11286 | // strings, indicating field references, that will wind up in the | |
11287 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11288 | if (function != NULL) |
11289 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11290 | var->set_in_unique_section(); |
11291 | ||
11292 | return this; | |
11293 | } | |
11294 | ||
e440a328 | 11295 | // Return the type of a field reference. |
11296 | ||
11297 | Type* | |
11298 | Field_reference_expression::do_type() | |
11299 | { | |
b0e628fb | 11300 | Type* type = this->expr_->type(); |
5c13bd80 | 11301 | if (type->is_error()) |
b0e628fb | 11302 | return type; |
11303 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11304 | go_assert(struct_type != NULL); |
e440a328 | 11305 | return struct_type->field(this->field_index_)->type(); |
11306 | } | |
11307 | ||
11308 | // Check the types for a field reference. | |
11309 | ||
11310 | void | |
11311 | Field_reference_expression::do_check_types(Gogo*) | |
11312 | { | |
b0e628fb | 11313 | Type* type = this->expr_->type(); |
5c13bd80 | 11314 | if (type->is_error()) |
b0e628fb | 11315 | return; |
11316 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11317 | go_assert(struct_type != NULL); |
11318 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11319 | } |
11320 | ||
ea664253 | 11321 | // Get the backend representation for a field reference. |
e440a328 | 11322 | |
ea664253 | 11323 | Bexpression* |
11324 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11325 | { |
ea664253 | 11326 | Bexpression* bstruct = this->expr_->get_backend(context); |
11327 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11328 | this->field_index_, | |
11329 | this->location()); | |
e440a328 | 11330 | } |
11331 | ||
d751bb78 | 11332 | // Dump ast representation for a field reference expression. |
11333 | ||
11334 | void | |
11335 | Field_reference_expression::do_dump_expression( | |
11336 | Ast_dump_context* ast_dump_context) const | |
11337 | { | |
11338 | this->expr_->dump_expression(ast_dump_context); | |
11339 | ast_dump_context->ostream() << "." << this->field_index_; | |
11340 | } | |
11341 | ||
e440a328 | 11342 | // Make a reference to a qualified identifier in an expression. |
11343 | ||
11344 | Field_reference_expression* | |
11345 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11346 | Location location) |
e440a328 | 11347 | { |
11348 | return new Field_reference_expression(expr, field_index, location); | |
11349 | } | |
11350 | ||
11351 | // Class Interface_field_reference_expression. | |
11352 | ||
2387f644 | 11353 | // Return an expression for the pointer to the function to call. |
e440a328 | 11354 | |
2387f644 | 11355 | Expression* |
11356 | Interface_field_reference_expression::get_function() | |
e440a328 | 11357 | { |
2387f644 | 11358 | Expression* ref = this->expr_; |
11359 | Location loc = this->location(); | |
11360 | if (ref->type()->points_to() != NULL) | |
11361 | ref = Expression::make_unary(OPERATOR_MULT, ref, loc); | |
e440a328 | 11362 | |
2387f644 | 11363 | Expression* mtable = |
11364 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11365 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11366 | |
11367 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11368 | unsigned int index; |
11369 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11370 | go_assert(field != NULL); | |
11371 | mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc); | |
11372 | return Expression::make_field_reference(mtable, index, loc); | |
e440a328 | 11373 | } |
11374 | ||
2387f644 | 11375 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11376 | // function. |
11377 | ||
2387f644 | 11378 | Expression* |
11379 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11380 | { |
2387f644 | 11381 | Expression* expr = this->expr_; |
11382 | if (expr->type()->points_to() != NULL) | |
11383 | expr = Expression::make_unary(OPERATOR_MULT, expr, this->location()); | |
11384 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, | |
11385 | this->location()); | |
e440a328 | 11386 | } |
11387 | ||
11388 | // Traversal. | |
11389 | ||
11390 | int | |
11391 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11392 | { | |
11393 | return Expression::traverse(&this->expr_, traverse); | |
11394 | } | |
11395 | ||
0afbb937 | 11396 | // Lower the expression. If this expression is not called, we need to |
11397 | // evaluate the expression twice when converting to the backend | |
11398 | // interface. So introduce a temporary variable if necessary. | |
11399 | ||
11400 | Expression* | |
9782d556 | 11401 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11402 | Statement_inserter* inserter) | |
0afbb937 | 11403 | { |
5bf8be8b | 11404 | if (this->expr_->is_error_expression() |
11405 | || this->expr_->type()->is_error_type()) | |
11406 | { | |
11407 | go_assert(saw_errors()); | |
11408 | return Expression::make_error(this->location()); | |
11409 | } | |
11410 | ||
2387f644 | 11411 | if (!this->expr_->is_variable()) |
0afbb937 | 11412 | { |
11413 | Temporary_statement* temp = | |
11414 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11415 | inserter->insert(temp); | |
11416 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11417 | this->location()); | |
11418 | } | |
11419 | return this; | |
11420 | } | |
11421 | ||
e440a328 | 11422 | // Return the type of an interface field reference. |
11423 | ||
11424 | Type* | |
11425 | Interface_field_reference_expression::do_type() | |
11426 | { | |
11427 | Type* expr_type = this->expr_->type(); | |
11428 | ||
11429 | Type* points_to = expr_type->points_to(); | |
11430 | if (points_to != NULL) | |
11431 | expr_type = points_to; | |
11432 | ||
11433 | Interface_type* interface_type = expr_type->interface_type(); | |
11434 | if (interface_type == NULL) | |
11435 | return Type::make_error_type(); | |
11436 | ||
11437 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11438 | if (method == NULL) | |
11439 | return Type::make_error_type(); | |
11440 | ||
11441 | return method->type(); | |
11442 | } | |
11443 | ||
11444 | // Determine types. | |
11445 | ||
11446 | void | |
11447 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11448 | { | |
11449 | this->expr_->determine_type_no_context(); | |
11450 | } | |
11451 | ||
11452 | // Check the types for an interface field reference. | |
11453 | ||
11454 | void | |
11455 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11456 | { | |
11457 | Type* type = this->expr_->type(); | |
11458 | ||
11459 | Type* points_to = type->points_to(); | |
11460 | if (points_to != NULL) | |
11461 | type = points_to; | |
11462 | ||
11463 | Interface_type* interface_type = type->interface_type(); | |
11464 | if (interface_type == NULL) | |
5c491127 | 11465 | { |
11466 | if (!type->is_error_type()) | |
11467 | this->report_error(_("expected interface or pointer to interface")); | |
11468 | } | |
e440a328 | 11469 | else |
11470 | { | |
11471 | const Typed_identifier* method = | |
11472 | interface_type->find_method(this->name_); | |
11473 | if (method == NULL) | |
11474 | { | |
631d5788 | 11475 | go_error_at(this->location(), "method %qs not in interface", |
11476 | Gogo::message_name(this->name_).c_str()); | |
e440a328 | 11477 | this->set_is_error(); |
11478 | } | |
11479 | } | |
11480 | } | |
11481 | ||
0afbb937 | 11482 | // If an interface field reference is not simply called, then it is |
11483 | // represented as a closure. The closure will hold a single variable, | |
11484 | // the value of the interface on which the method should be called. | |
11485 | // The function will be a simple thunk that pulls the value from the | |
11486 | // closure and calls the method with the remaining arguments. | |
11487 | ||
11488 | // Because method values are not common, we don't build all thunks for | |
11489 | // all possible interface methods, but instead only build them as we | |
11490 | // need them. In particular, we even build them on demand for | |
11491 | // interface methods defined in other packages. | |
11492 | ||
11493 | Interface_field_reference_expression::Interface_method_thunks | |
11494 | Interface_field_reference_expression::interface_method_thunks; | |
11495 | ||
11496 | // Find or create the thunk to call method NAME on TYPE. | |
11497 | ||
11498 | Named_object* | |
11499 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11500 | Interface_type* type, | |
11501 | const std::string& name) | |
11502 | { | |
11503 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11504 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11505 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11506 | if (ins.second) | |
11507 | { | |
11508 | // This is the first time we have seen this interface. | |
11509 | ins.first->second = new Method_thunks(); | |
11510 | } | |
11511 | ||
11512 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11513 | p != ins.first->second->end(); | |
11514 | p++) | |
11515 | if (p->first == name) | |
11516 | return p->second; | |
11517 | ||
11518 | Location loc = type->location(); | |
11519 | ||
11520 | const Typed_identifier* method_id = type->find_method(name); | |
11521 | if (method_id == NULL) | |
11522 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11523 | ||
11524 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11525 | if (orig_fntype == NULL) | |
11526 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11527 | ||
11528 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11529 | // The type here is wrong--it should be the C function type. But it |
11530 | // doesn't really matter. | |
0afbb937 | 11531 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11532 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11533 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
11534 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
11535 | closure_type = Type::make_pointer_type(closure_type); | |
11536 | ||
f8bdf81a | 11537 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11538 | |
da244e59 | 11539 | std::string thunk_name = Gogo::thunk_name(); |
11540 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 11541 | false, loc); |
11542 | ||
f8bdf81a | 11543 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11544 | cvar->set_is_used(); | |
1ecc6157 | 11545 | cvar->set_is_closure(); |
da244e59 | 11546 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
11547 | NULL, cvar); | |
f8bdf81a | 11548 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 11549 | |
f8bdf81a | 11550 | gogo->start_block(loc); |
0afbb937 | 11551 | |
11552 | // Field 0 of the closure is the function code pointer, field 1 is | |
11553 | // the value on which to invoke the method. | |
11554 | Expression* arg = Expression::make_var_reference(cp, loc); | |
11555 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
11556 | arg = Expression::make_field_reference(arg, 1, loc); | |
11557 | ||
11558 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11559 | loc); | |
11560 | ||
11561 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11562 | Expression_list* args; | |
11563 | if (orig_params == NULL || orig_params->empty()) | |
11564 | args = NULL; | |
11565 | else | |
11566 | { | |
11567 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11568 | args = new Expression_list(); | |
11569 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11570 | p != new_params->end(); |
0afbb937 | 11571 | ++p) |
11572 | { | |
11573 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11574 | go_assert(p_no != NULL | |
11575 | && p_no->is_variable() | |
11576 | && p_no->var_value()->is_parameter()); | |
11577 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11578 | } | |
11579 | } | |
11580 | ||
11581 | Call_expression* call = Expression::make_call(ifre, args, | |
11582 | orig_fntype->is_varargs(), | |
11583 | loc); | |
11584 | call->set_varargs_are_lowered(); | |
11585 | ||
11586 | Statement* s = Statement::make_return_from_call(call, loc); | |
11587 | gogo->add_statement(s); | |
11588 | Block* b = gogo->finish_block(loc); | |
11589 | gogo->add_block(b, loc); | |
11590 | gogo->lower_block(new_no, b); | |
a32698ee | 11591 | gogo->flatten_block(new_no, b); |
0afbb937 | 11592 | gogo->finish_function(loc); |
11593 | ||
11594 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11595 | return new_no; | |
11596 | } | |
11597 | ||
ea664253 | 11598 | // Get the backend representation for a method value. |
e440a328 | 11599 | |
ea664253 | 11600 | Bexpression* |
11601 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11602 | { |
0afbb937 | 11603 | Interface_type* type = this->expr_->type()->interface_type(); |
11604 | if (type == NULL) | |
11605 | { | |
11606 | go_assert(saw_errors()); | |
ea664253 | 11607 | return context->backend()->error_expression(); |
0afbb937 | 11608 | } |
11609 | ||
11610 | Named_object* thunk = | |
11611 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11612 | type, this->name_); | |
11613 | if (thunk->is_erroneous()) | |
11614 | { | |
11615 | go_assert(saw_errors()); | |
ea664253 | 11616 | return context->backend()->error_expression(); |
0afbb937 | 11617 | } |
11618 | ||
11619 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11620 | // the lowering pass because at that point we don't know whether we | |
11621 | // need to create the thunk or not. If the expression is called, we | |
11622 | // don't need the thunk. | |
11623 | ||
11624 | Location loc = this->location(); | |
11625 | ||
11626 | Struct_field_list* fields = new Struct_field_list(); | |
11627 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11628 | thunk->func_value()->type(), | |
11629 | loc))); | |
11630 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11631 | this->expr_->type(), | |
11632 | loc))); | |
11633 | Struct_type* st = Type::make_struct_type(fields, loc); | |
11634 | ||
11635 | Expression_list* vals = new Expression_list(); | |
11636 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
11637 | vals->push_back(this->expr_); | |
11638 | ||
11639 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 11640 | Bexpression* bclosure = |
11641 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 11642 | |
2387f644 | 11643 | Expression* nil_check = |
11644 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
11645 | Expression::make_nil(loc), loc); | |
ea664253 | 11646 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 11647 | |
2387f644 | 11648 | Gogo* gogo = context->gogo(); |
ea664253 | 11649 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
11650 | loc)->get_backend(context); | |
2387f644 | 11651 | |
11652 | Bexpression* bcond = | |
a32698ee | 11653 | gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc); |
2387f644 | 11654 | Bstatement* cond_statement = gogo->backend()->expression_statement(bcond); |
ea664253 | 11655 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 11656 | } |
11657 | ||
d751bb78 | 11658 | // Dump ast representation for an interface field reference. |
11659 | ||
11660 | void | |
11661 | Interface_field_reference_expression::do_dump_expression( | |
11662 | Ast_dump_context* ast_dump_context) const | |
11663 | { | |
11664 | this->expr_->dump_expression(ast_dump_context); | |
11665 | ast_dump_context->ostream() << "." << this->name_; | |
11666 | } | |
11667 | ||
e440a328 | 11668 | // Make a reference to a field in an interface. |
11669 | ||
11670 | Expression* | |
11671 | Expression::make_interface_field_reference(Expression* expr, | |
11672 | const std::string& field, | |
b13c66cd | 11673 | Location location) |
e440a328 | 11674 | { |
11675 | return new Interface_field_reference_expression(expr, field, location); | |
11676 | } | |
11677 | ||
11678 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
11679 | // is lowered after we know the type of the left hand side. | |
11680 | ||
11681 | class Selector_expression : public Parser_expression | |
11682 | { | |
11683 | public: | |
11684 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 11685 | Location location) |
e440a328 | 11686 | : Parser_expression(EXPRESSION_SELECTOR, location), |
11687 | left_(left), name_(name) | |
11688 | { } | |
11689 | ||
11690 | protected: | |
11691 | int | |
11692 | do_traverse(Traverse* traverse) | |
11693 | { return Expression::traverse(&this->left_, traverse); } | |
11694 | ||
11695 | Expression* | |
ceeb4318 | 11696 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 11697 | |
11698 | Expression* | |
11699 | do_copy() | |
11700 | { | |
11701 | return new Selector_expression(this->left_->copy(), this->name_, | |
11702 | this->location()); | |
11703 | } | |
11704 | ||
d751bb78 | 11705 | void |
11706 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
11707 | ||
e440a328 | 11708 | private: |
11709 | Expression* | |
11710 | lower_method_expression(Gogo*); | |
11711 | ||
11712 | // The expression on the left hand side. | |
11713 | Expression* left_; | |
11714 | // The name on the right hand side. | |
11715 | std::string name_; | |
11716 | }; | |
11717 | ||
11718 | // Lower a selector expression once we know the real type of the left | |
11719 | // hand side. | |
11720 | ||
11721 | Expression* | |
ceeb4318 | 11722 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
11723 | int) | |
e440a328 | 11724 | { |
11725 | Expression* left = this->left_; | |
11726 | if (left->is_type_expression()) | |
11727 | return this->lower_method_expression(gogo); | |
11728 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
11729 | this->location()); | |
11730 | } | |
11731 | ||
11732 | // Lower a method expression T.M or (*T).M. We turn this into a | |
11733 | // function literal. | |
11734 | ||
11735 | Expression* | |
11736 | Selector_expression::lower_method_expression(Gogo* gogo) | |
11737 | { | |
b13c66cd | 11738 | Location location = this->location(); |
868b439e | 11739 | Type* left_type = this->left_->type(); |
11740 | Type* type = left_type; | |
e440a328 | 11741 | const std::string& name(this->name_); |
11742 | ||
11743 | bool is_pointer; | |
11744 | if (type->points_to() == NULL) | |
11745 | is_pointer = false; | |
11746 | else | |
11747 | { | |
11748 | is_pointer = true; | |
11749 | type = type->points_to(); | |
11750 | } | |
11751 | Named_type* nt = type->named_type(); | |
11752 | if (nt == NULL) | |
11753 | { | |
631d5788 | 11754 | go_error_at(location, |
11755 | ("method expression requires named type or " | |
11756 | "pointer to named type")); | |
e440a328 | 11757 | return Expression::make_error(location); |
11758 | } | |
11759 | ||
11760 | bool is_ambiguous; | |
11761 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 11762 | const Typed_identifier* imethod = NULL; |
dcc8506b | 11763 | if (method == NULL && !is_pointer) |
ab1468c3 | 11764 | { |
11765 | Interface_type* it = nt->interface_type(); | |
11766 | if (it != NULL) | |
11767 | imethod = it->find_method(name); | |
11768 | } | |
11769 | ||
868b439e | 11770 | if ((method == NULL && imethod == NULL) |
11771 | || (left_type->named_type() != NULL && left_type->points_to() != NULL)) | |
e440a328 | 11772 | { |
11773 | if (!is_ambiguous) | |
631d5788 | 11774 | go_error_at(location, "type %<%s%s%> has no method %<%s%>", |
11775 | is_pointer ? "*" : "", | |
11776 | nt->message_name().c_str(), | |
11777 | Gogo::message_name(name).c_str()); | |
e440a328 | 11778 | else |
631d5788 | 11779 | go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
11780 | Gogo::message_name(name).c_str(), | |
11781 | is_pointer ? "*" : "", | |
11782 | nt->message_name().c_str()); | |
e440a328 | 11783 | return Expression::make_error(location); |
11784 | } | |
11785 | ||
ab1468c3 | 11786 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 11787 | { |
631d5788 | 11788 | go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", |
11789 | nt->message_name().c_str(), | |
11790 | Gogo::message_name(name).c_str()); | |
e440a328 | 11791 | return Expression::make_error(location); |
11792 | } | |
11793 | ||
11794 | // Build a new function type in which the receiver becomes the first | |
11795 | // argument. | |
ab1468c3 | 11796 | Function_type* method_type; |
11797 | if (method != NULL) | |
11798 | { | |
11799 | method_type = method->type(); | |
c484d925 | 11800 | go_assert(method_type->is_method()); |
ab1468c3 | 11801 | } |
11802 | else | |
11803 | { | |
11804 | method_type = imethod->type()->function_type(); | |
c484d925 | 11805 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 11806 | } |
e440a328 | 11807 | |
11808 | const char* const receiver_name = "$this"; | |
11809 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
11810 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
11811 | location)); | |
11812 | ||
11813 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
11814 | if (method_parameters != NULL) | |
11815 | { | |
f470da59 | 11816 | int i = 0; |
e440a328 | 11817 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
11818 | p != method_parameters->end(); | |
f470da59 | 11819 | ++p, ++i) |
11820 | { | |
68883531 | 11821 | if (!p->name().empty()) |
f470da59 | 11822 | parameters->push_back(*p); |
11823 | else | |
11824 | { | |
11825 | char buf[20]; | |
11826 | snprintf(buf, sizeof buf, "$param%d", i); | |
11827 | parameters->push_back(Typed_identifier(buf, p->type(), | |
11828 | p->location())); | |
11829 | } | |
11830 | } | |
e440a328 | 11831 | } |
11832 | ||
11833 | const Typed_identifier_list* method_results = method_type->results(); | |
11834 | Typed_identifier_list* results; | |
11835 | if (method_results == NULL) | |
11836 | results = NULL; | |
11837 | else | |
11838 | { | |
11839 | results = new Typed_identifier_list(); | |
11840 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
11841 | p != method_results->end(); | |
11842 | ++p) | |
11843 | results->push_back(*p); | |
11844 | } | |
11845 | ||
11846 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
11847 | location); | |
11848 | if (method_type->is_varargs()) | |
11849 | fntype->set_is_varargs(); | |
11850 | ||
11851 | // We generate methods which always takes a pointer to the receiver | |
11852 | // as their first argument. If this is for a pointer type, we can | |
11853 | // simply reuse the existing function. We use an internal hack to | |
11854 | // get the right type. | |
8381eda7 | 11855 | // FIXME: This optimization is disabled because it doesn't yet work |
11856 | // with function descriptors when the method expression is not | |
11857 | // directly called. | |
11858 | if (method != NULL && is_pointer && false) | |
e440a328 | 11859 | { |
11860 | Named_object* mno = (method->needs_stub_method() | |
11861 | ? method->stub_object() | |
11862 | : method->named_object()); | |
11863 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
11864 | f = Expression::make_cast(fntype, f, location); | |
11865 | Type_conversion_expression* tce = | |
11866 | static_cast<Type_conversion_expression*>(f); | |
11867 | tce->set_may_convert_function_types(); | |
11868 | return f; | |
11869 | } | |
11870 | ||
11871 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
11872 | location); | |
11873 | ||
11874 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 11875 | go_assert(vno != NULL); |
e440a328 | 11876 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 11877 | Expression* bm; |
11878 | if (method != NULL) | |
11879 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
11880 | else | |
11881 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 11882 | |
11883 | // Even though we found the method above, if it has an error type we | |
11884 | // may see an error here. | |
11885 | if (bm->is_error_expression()) | |
463fe805 | 11886 | { |
11887 | gogo->finish_function(location); | |
11888 | return bm; | |
11889 | } | |
e440a328 | 11890 | |
11891 | Expression_list* args; | |
f470da59 | 11892 | if (parameters->size() <= 1) |
e440a328 | 11893 | args = NULL; |
11894 | else | |
11895 | { | |
11896 | args = new Expression_list(); | |
f470da59 | 11897 | Typed_identifier_list::const_iterator p = parameters->begin(); |
11898 | ++p; | |
11899 | for (; p != parameters->end(); ++p) | |
e440a328 | 11900 | { |
11901 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 11902 | go_assert(vno != NULL); |
e440a328 | 11903 | args->push_back(Expression::make_var_reference(vno, location)); |
11904 | } | |
11905 | } | |
11906 | ||
ceeb4318 | 11907 | gogo->start_block(location); |
11908 | ||
e440a328 | 11909 | Call_expression* call = Expression::make_call(bm, args, |
11910 | method_type->is_varargs(), | |
11911 | location); | |
11912 | ||
0afbb937 | 11913 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 11914 | gogo->add_statement(s); |
11915 | ||
ceeb4318 | 11916 | Block* b = gogo->finish_block(location); |
11917 | ||
11918 | gogo->add_block(b, location); | |
11919 | ||
11920 | // Lower the call in case there are multiple results. | |
11921 | gogo->lower_block(no, b); | |
a32698ee | 11922 | gogo->flatten_block(no, b); |
ceeb4318 | 11923 | |
e440a328 | 11924 | gogo->finish_function(location); |
11925 | ||
11926 | return Expression::make_func_reference(no, NULL, location); | |
11927 | } | |
11928 | ||
d751bb78 | 11929 | // Dump the ast for a selector expression. |
11930 | ||
11931 | void | |
11932 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11933 | const | |
11934 | { | |
11935 | ast_dump_context->dump_expression(this->left_); | |
11936 | ast_dump_context->ostream() << "."; | |
11937 | ast_dump_context->ostream() << this->name_; | |
11938 | } | |
11939 | ||
e440a328 | 11940 | // Make a selector expression. |
11941 | ||
11942 | Expression* | |
11943 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 11944 | Location location) |
e440a328 | 11945 | { |
11946 | return new Selector_expression(left, name, location); | |
11947 | } | |
11948 | ||
da244e59 | 11949 | // Class Allocation_expression. |
e440a328 | 11950 | |
da244e59 | 11951 | int |
11952 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 11953 | { |
da244e59 | 11954 | return Type::traverse(this->type_, traverse); |
11955 | } | |
e440a328 | 11956 | |
da244e59 | 11957 | Type* |
11958 | Allocation_expression::do_type() | |
11959 | { | |
11960 | return Type::make_pointer_type(this->type_); | |
11961 | } | |
e440a328 | 11962 | |
da244e59 | 11963 | // Make a copy of an allocation expression. |
e440a328 | 11964 | |
da244e59 | 11965 | Expression* |
11966 | Allocation_expression::do_copy() | |
11967 | { | |
11968 | Allocation_expression* alloc = | |
11969 | new Allocation_expression(this->type_, this->location()); | |
11970 | if (this->allocate_on_stack_) | |
11971 | alloc->set_allocate_on_stack(); | |
11972 | return alloc; | |
11973 | } | |
e440a328 | 11974 | |
ea664253 | 11975 | // Return the backend representation for an allocation expression. |
e440a328 | 11976 | |
ea664253 | 11977 | Bexpression* |
11978 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11979 | { |
2c809f8f | 11980 | Gogo* gogo = context->gogo(); |
11981 | Location loc = this->location(); | |
da244e59 | 11982 | |
45ff893b | 11983 | Node* n = Node::make_node(this); |
11984 | if (this->allocate_on_stack_ | |
11985 | || (n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
da244e59 | 11986 | { |
2a305b85 | 11987 | int64_t size; |
11988 | bool ok = this->type_->backend_type_size(gogo, &size); | |
11989 | if (!ok) | |
11990 | { | |
11991 | go_assert(saw_errors()); | |
11992 | return gogo->backend()->error_expression(); | |
11993 | } | |
d5d1c295 | 11994 | return gogo->backend()->stack_allocation_expression(size, loc); |
da244e59 | 11995 | } |
11996 | ||
2a305b85 | 11997 | Btype* btype = this->type_->get_backend(gogo); |
11998 | Bexpression* space = | |
ea664253 | 11999 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 12000 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 12001 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 12002 | } |
12003 | ||
d751bb78 | 12004 | // Dump ast representation for an allocation expression. |
12005 | ||
12006 | void | |
12007 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12008 | const | |
12009 | { | |
12010 | ast_dump_context->ostream() << "new("; | |
12011 | ast_dump_context->dump_type(this->type_); | |
12012 | ast_dump_context->ostream() << ")"; | |
12013 | } | |
12014 | ||
e440a328 | 12015 | // Make an allocation expression. |
12016 | ||
12017 | Expression* | |
b13c66cd | 12018 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12019 | { |
12020 | return new Allocation_expression(type, location); | |
12021 | } | |
12022 | ||
da244e59 | 12023 | // Class Struct_construction_expression. |
e440a328 | 12024 | |
12025 | // Traversal. | |
12026 | ||
12027 | int | |
12028 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12029 | { | |
0c4f5a19 | 12030 | if (this->vals_ != NULL) |
12031 | { | |
12032 | if (this->traverse_order_ == NULL) | |
12033 | { | |
12034 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12035 | return TRAVERSE_EXIT; | |
12036 | } | |
12037 | else | |
12038 | { | |
12039 | for (std::vector<int>::const_iterator p = | |
12040 | this->traverse_order_->begin(); | |
12041 | p != this->traverse_order_->end(); | |
12042 | ++p) | |
12043 | { | |
12044 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12045 | == TRAVERSE_EXIT) | |
12046 | return TRAVERSE_EXIT; | |
12047 | } | |
12048 | } | |
12049 | } | |
e440a328 | 12050 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12051 | return TRAVERSE_EXIT; | |
12052 | return TRAVERSE_CONTINUE; | |
12053 | } | |
12054 | ||
12055 | // Return whether this is a constant initializer. | |
12056 | ||
12057 | bool | |
12058 | Struct_construction_expression::is_constant_struct() const | |
12059 | { | |
12060 | if (this->vals_ == NULL) | |
12061 | return true; | |
12062 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12063 | pv != this->vals_->end(); | |
12064 | ++pv) | |
12065 | { | |
12066 | if (*pv != NULL | |
12067 | && !(*pv)->is_constant() | |
12068 | && (!(*pv)->is_composite_literal() | |
12069 | || (*pv)->is_nonconstant_composite_literal())) | |
12070 | return false; | |
12071 | } | |
12072 | ||
12073 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12074 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12075 | pf != fields->end(); | |
12076 | ++pf) | |
12077 | { | |
12078 | // There are no constant constructors for interfaces. | |
12079 | if (pf->type()->interface_type() != NULL) | |
12080 | return false; | |
12081 | } | |
12082 | ||
12083 | return true; | |
12084 | } | |
12085 | ||
f9ca30f9 | 12086 | // Return whether this struct is immutable. |
12087 | ||
12088 | bool | |
12089 | Struct_construction_expression::do_is_immutable() const | |
12090 | { | |
12091 | if (this->vals_ == NULL) | |
12092 | return true; | |
12093 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12094 | pv != this->vals_->end(); | |
12095 | ++pv) | |
12096 | { | |
12097 | if (*pv != NULL && !(*pv)->is_immutable()) | |
12098 | return false; | |
12099 | } | |
12100 | return true; | |
12101 | } | |
12102 | ||
e440a328 | 12103 | // Final type determination. |
12104 | ||
12105 | void | |
12106 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12107 | { | |
12108 | if (this->vals_ == NULL) | |
12109 | return; | |
12110 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12111 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12112 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12113 | pf != fields->end(); | |
12114 | ++pf, ++pv) | |
12115 | { | |
12116 | if (pv == this->vals_->end()) | |
12117 | return; | |
12118 | if (*pv != NULL) | |
12119 | { | |
12120 | Type_context subcontext(pf->type(), false); | |
12121 | (*pv)->determine_type(&subcontext); | |
12122 | } | |
12123 | } | |
a6cb4c0e | 12124 | // Extra values are an error we will report elsewhere; we still want |
12125 | // to determine the type to avoid knockon errors. | |
12126 | for (; pv != this->vals_->end(); ++pv) | |
12127 | (*pv)->determine_type_no_context(); | |
e440a328 | 12128 | } |
12129 | ||
12130 | // Check types. | |
12131 | ||
12132 | void | |
12133 | Struct_construction_expression::do_check_types(Gogo*) | |
12134 | { | |
12135 | if (this->vals_ == NULL) | |
12136 | return; | |
12137 | ||
12138 | Struct_type* st = this->type_->struct_type(); | |
12139 | if (this->vals_->size() > st->field_count()) | |
12140 | { | |
12141 | this->report_error(_("too many expressions for struct")); | |
12142 | return; | |
12143 | } | |
12144 | ||
12145 | const Struct_field_list* fields = st->fields(); | |
12146 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12147 | int i = 0; | |
12148 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12149 | pf != fields->end(); | |
12150 | ++pf, ++pv, ++i) | |
12151 | { | |
12152 | if (pv == this->vals_->end()) | |
12153 | { | |
12154 | this->report_error(_("too few expressions for struct")); | |
12155 | break; | |
12156 | } | |
12157 | ||
12158 | if (*pv == NULL) | |
12159 | continue; | |
12160 | ||
12161 | std::string reason; | |
12162 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12163 | { | |
12164 | if (reason.empty()) | |
631d5788 | 12165 | go_error_at((*pv)->location(), |
12166 | "incompatible type for field %d in struct construction", | |
12167 | i + 1); | |
e440a328 | 12168 | else |
631d5788 | 12169 | go_error_at((*pv)->location(), |
12170 | ("incompatible type for field %d in " | |
12171 | "struct construction (%s)"), | |
12172 | i + 1, reason.c_str()); | |
e440a328 | 12173 | this->set_is_error(); |
12174 | } | |
12175 | } | |
c484d925 | 12176 | go_assert(pv == this->vals_->end()); |
e440a328 | 12177 | } |
12178 | ||
8ba8cc87 | 12179 | // Flatten a struct construction expression. Store the values into |
12180 | // temporaries in case they need interface conversion. | |
12181 | ||
12182 | Expression* | |
12183 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
12184 | Statement_inserter* inserter) | |
12185 | { | |
12186 | if (this->vals_ == NULL) | |
12187 | return this; | |
12188 | ||
12189 | // If this is a constant struct, we don't need temporaries. | |
12190 | if (this->is_constant_struct()) | |
12191 | return this; | |
12192 | ||
12193 | Location loc = this->location(); | |
12194 | for (Expression_list::iterator pv = this->vals_->begin(); | |
12195 | pv != this->vals_->end(); | |
12196 | ++pv) | |
12197 | { | |
12198 | if (*pv != NULL) | |
12199 | { | |
5bf8be8b | 12200 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12201 | { | |
12202 | go_assert(saw_errors()); | |
12203 | return Expression::make_error(loc); | |
12204 | } | |
8ba8cc87 | 12205 | if (!(*pv)->is_variable()) |
12206 | { | |
12207 | Temporary_statement* temp = | |
12208 | Statement::make_temporary(NULL, *pv, loc); | |
12209 | inserter->insert(temp); | |
12210 | *pv = Expression::make_temporary_reference(temp, loc); | |
12211 | } | |
12212 | } | |
12213 | } | |
12214 | return this; | |
12215 | } | |
12216 | ||
ea664253 | 12217 | // Return the backend representation for constructing a struct. |
e440a328 | 12218 | |
ea664253 | 12219 | Bexpression* |
12220 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12221 | { |
12222 | Gogo* gogo = context->gogo(); | |
12223 | ||
2c809f8f | 12224 | Btype* btype = this->type_->get_backend(gogo); |
e440a328 | 12225 | if (this->vals_ == NULL) |
ea664253 | 12226 | return gogo->backend()->zero_expression(btype); |
e440a328 | 12227 | |
e440a328 | 12228 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e440a328 | 12229 | Expression_list::const_iterator pv = this->vals_->begin(); |
2c809f8f | 12230 | std::vector<Bexpression*> init; |
12231 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12232 | pf != fields->end(); | |
12233 | ++pf) | |
e440a328 | 12234 | { |
63697958 | 12235 | Btype* fbtype = pf->type()->get_backend(gogo); |
e440a328 | 12236 | if (pv == this->vals_->end()) |
2c809f8f | 12237 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12238 | else if (*pv == NULL) |
12239 | { | |
2c809f8f | 12240 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12241 | ++pv; |
12242 | } | |
12243 | else | |
12244 | { | |
2c809f8f | 12245 | Expression* val = |
12246 | Expression::convert_for_assignment(gogo, pf->type(), | |
12247 | *pv, this->location()); | |
ea664253 | 12248 | init.push_back(val->get_backend(context)); |
e440a328 | 12249 | ++pv; |
12250 | } | |
e440a328 | 12251 | } |
ea664253 | 12252 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12253 | } |
12254 | ||
12255 | // Export a struct construction. | |
12256 | ||
12257 | void | |
12258 | Struct_construction_expression::do_export(Export* exp) const | |
12259 | { | |
12260 | exp->write_c_string("convert("); | |
12261 | exp->write_type(this->type_); | |
12262 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12263 | pv != this->vals_->end(); | |
12264 | ++pv) | |
12265 | { | |
12266 | exp->write_c_string(", "); | |
12267 | if (*pv != NULL) | |
12268 | (*pv)->export_expression(exp); | |
12269 | } | |
12270 | exp->write_c_string(")"); | |
12271 | } | |
12272 | ||
d751bb78 | 12273 | // Dump ast representation of a struct construction expression. |
12274 | ||
12275 | void | |
12276 | Struct_construction_expression::do_dump_expression( | |
12277 | Ast_dump_context* ast_dump_context) const | |
12278 | { | |
d751bb78 | 12279 | ast_dump_context->dump_type(this->type_); |
12280 | ast_dump_context->ostream() << "{"; | |
12281 | ast_dump_context->dump_expression_list(this->vals_); | |
12282 | ast_dump_context->ostream() << "}"; | |
12283 | } | |
12284 | ||
e440a328 | 12285 | // Make a struct composite literal. This used by the thunk code. |
12286 | ||
12287 | Expression* | |
12288 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12289 | Location location) |
e440a328 | 12290 | { |
c484d925 | 12291 | go_assert(type->struct_type() != NULL); |
e440a328 | 12292 | return new Struct_construction_expression(type, vals, location); |
12293 | } | |
12294 | ||
da244e59 | 12295 | // Class Array_construction_expression. |
e440a328 | 12296 | |
12297 | // Traversal. | |
12298 | ||
12299 | int | |
12300 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12301 | { | |
12302 | if (this->vals_ != NULL | |
12303 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12304 | return TRAVERSE_EXIT; | |
12305 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12306 | return TRAVERSE_EXIT; | |
12307 | return TRAVERSE_CONTINUE; | |
12308 | } | |
12309 | ||
12310 | // Return whether this is a constant initializer. | |
12311 | ||
12312 | bool | |
12313 | Array_construction_expression::is_constant_array() const | |
12314 | { | |
12315 | if (this->vals_ == NULL) | |
12316 | return true; | |
12317 | ||
12318 | // There are no constant constructors for interfaces. | |
12319 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12320 | return false; | |
12321 | ||
12322 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12323 | pv != this->vals_->end(); | |
12324 | ++pv) | |
12325 | { | |
12326 | if (*pv != NULL | |
12327 | && !(*pv)->is_constant() | |
12328 | && (!(*pv)->is_composite_literal() | |
12329 | || (*pv)->is_nonconstant_composite_literal())) | |
12330 | return false; | |
12331 | } | |
12332 | return true; | |
12333 | } | |
12334 | ||
f9ca30f9 | 12335 | // Return whether this is an immutable array initializer. |
12336 | ||
12337 | bool | |
12338 | Array_construction_expression::do_is_immutable() const | |
12339 | { | |
12340 | if (this->vals_ == NULL) | |
12341 | return true; | |
12342 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12343 | pv != this->vals_->end(); | |
12344 | ++pv) | |
12345 | { | |
12346 | if (*pv != NULL && !(*pv)->is_immutable()) | |
12347 | return false; | |
12348 | } | |
12349 | return true; | |
12350 | } | |
12351 | ||
e440a328 | 12352 | // Final type determination. |
12353 | ||
12354 | void | |
12355 | Array_construction_expression::do_determine_type(const Type_context*) | |
12356 | { | |
12357 | if (this->vals_ == NULL) | |
12358 | return; | |
12359 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
12360 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12361 | pv != this->vals_->end(); | |
12362 | ++pv) | |
12363 | { | |
12364 | if (*pv != NULL) | |
12365 | (*pv)->determine_type(&subcontext); | |
12366 | } | |
12367 | } | |
12368 | ||
12369 | // Check types. | |
12370 | ||
12371 | void | |
12372 | Array_construction_expression::do_check_types(Gogo*) | |
12373 | { | |
12374 | if (this->vals_ == NULL) | |
12375 | return; | |
12376 | ||
12377 | Array_type* at = this->type_->array_type(); | |
12378 | int i = 0; | |
12379 | Type* element_type = at->element_type(); | |
12380 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12381 | pv != this->vals_->end(); | |
12382 | ++pv, ++i) | |
12383 | { | |
12384 | if (*pv != NULL | |
12385 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12386 | { | |
631d5788 | 12387 | go_error_at((*pv)->location(), |
12388 | "incompatible type for element %d in composite literal", | |
12389 | i + 1); | |
e440a328 | 12390 | this->set_is_error(); |
12391 | } | |
12392 | } | |
e440a328 | 12393 | } |
12394 | ||
8ba8cc87 | 12395 | // Flatten an array construction expression. Store the values into |
12396 | // temporaries in case they need interface conversion. | |
12397 | ||
12398 | Expression* | |
12399 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12400 | Statement_inserter* inserter) | |
12401 | { | |
12402 | if (this->vals_ == NULL) | |
12403 | return this; | |
12404 | ||
12405 | // If this is a constant array, we don't need temporaries. | |
12406 | if (this->is_constant_array()) | |
12407 | return this; | |
12408 | ||
12409 | Location loc = this->location(); | |
12410 | for (Expression_list::iterator pv = this->vals_->begin(); | |
12411 | pv != this->vals_->end(); | |
12412 | ++pv) | |
12413 | { | |
12414 | if (*pv != NULL) | |
12415 | { | |
5bf8be8b | 12416 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12417 | { | |
12418 | go_assert(saw_errors()); | |
12419 | return Expression::make_error(loc); | |
12420 | } | |
8ba8cc87 | 12421 | if (!(*pv)->is_variable()) |
12422 | { | |
12423 | Temporary_statement* temp = | |
12424 | Statement::make_temporary(NULL, *pv, loc); | |
12425 | inserter->insert(temp); | |
12426 | *pv = Expression::make_temporary_reference(temp, loc); | |
12427 | } | |
12428 | } | |
12429 | } | |
12430 | return this; | |
12431 | } | |
12432 | ||
2c809f8f | 12433 | // Get a constructor expression for the array values. |
e440a328 | 12434 | |
2c809f8f | 12435 | Bexpression* |
12436 | Array_construction_expression::get_constructor(Translate_context* context, | |
12437 | Btype* array_btype) | |
e440a328 | 12438 | { |
e440a328 | 12439 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12440 | |
12441 | std::vector<unsigned long> indexes; | |
12442 | std::vector<Bexpression*> vals; | |
12443 | Gogo* gogo = context->gogo(); | |
e440a328 | 12444 | if (this->vals_ != NULL) |
12445 | { | |
12446 | size_t i = 0; | |
ffe743ca | 12447 | std::vector<unsigned long>::const_iterator pi; |
12448 | if (this->indexes_ != NULL) | |
12449 | pi = this->indexes_->begin(); | |
e440a328 | 12450 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12451 | pv != this->vals_->end(); | |
12452 | ++pv, ++i) | |
12453 | { | |
ffe743ca | 12454 | if (this->indexes_ != NULL) |
12455 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12456 | |
12457 | if (this->indexes_ == NULL) | |
2c809f8f | 12458 | indexes.push_back(i); |
ffe743ca | 12459 | else |
2c809f8f | 12460 | indexes.push_back(*pi); |
e440a328 | 12461 | if (*pv == NULL) |
63697958 | 12462 | { |
63697958 | 12463 | Btype* ebtype = element_type->get_backend(gogo); |
12464 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12465 | vals.push_back(zv); |
63697958 | 12466 | } |
e440a328 | 12467 | else |
12468 | { | |
2c809f8f | 12469 | Expression* val_expr = |
12470 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12471 | this->location()); | |
ea664253 | 12472 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12473 | } |
ffe743ca | 12474 | if (this->indexes_ != NULL) |
12475 | ++pi; | |
e440a328 | 12476 | } |
ffe743ca | 12477 | if (this->indexes_ != NULL) |
12478 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12479 | } |
2c809f8f | 12480 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12481 | vals, this->location()); | |
e440a328 | 12482 | } |
12483 | ||
12484 | // Export an array construction. | |
12485 | ||
12486 | void | |
12487 | Array_construction_expression::do_export(Export* exp) const | |
12488 | { | |
12489 | exp->write_c_string("convert("); | |
12490 | exp->write_type(this->type_); | |
12491 | if (this->vals_ != NULL) | |
12492 | { | |
ffe743ca | 12493 | std::vector<unsigned long>::const_iterator pi; |
12494 | if (this->indexes_ != NULL) | |
12495 | pi = this->indexes_->begin(); | |
e440a328 | 12496 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12497 | pv != this->vals_->end(); | |
12498 | ++pv) | |
12499 | { | |
12500 | exp->write_c_string(", "); | |
ffe743ca | 12501 | |
12502 | if (this->indexes_ != NULL) | |
12503 | { | |
12504 | char buf[100]; | |
12505 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12506 | exp->write_c_string(buf); | |
12507 | exp->write_c_string(":"); | |
12508 | } | |
12509 | ||
e440a328 | 12510 | if (*pv != NULL) |
12511 | (*pv)->export_expression(exp); | |
ffe743ca | 12512 | |
12513 | if (this->indexes_ != NULL) | |
12514 | ++pi; | |
e440a328 | 12515 | } |
12516 | } | |
12517 | exp->write_c_string(")"); | |
12518 | } | |
12519 | ||
0e9a2e72 | 12520 | // Dump ast representation of an array construction expression. |
d751bb78 | 12521 | |
12522 | void | |
12523 | Array_construction_expression::do_dump_expression( | |
12524 | Ast_dump_context* ast_dump_context) const | |
12525 | { | |
ffe743ca | 12526 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12527 | |
12528 | ast_dump_context->ostream() << "[" ; | |
12529 | if (length != NULL) | |
12530 | { | |
12531 | ast_dump_context->dump_expression(length); | |
12532 | } | |
12533 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12534 | ast_dump_context->dump_type(this->type_); |
0e9a2e72 | 12535 | this->dump_slice_storage_expression(ast_dump_context); |
d751bb78 | 12536 | ast_dump_context->ostream() << "{" ; |
ffe743ca | 12537 | if (this->indexes_ == NULL) |
12538 | ast_dump_context->dump_expression_list(this->vals_); | |
12539 | else | |
12540 | { | |
12541 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12542 | for (std::vector<unsigned long>::const_iterator pi = | |
12543 | this->indexes_->begin(); | |
12544 | pi != this->indexes_->end(); | |
12545 | ++pi, ++pv) | |
12546 | { | |
12547 | if (pi != this->indexes_->begin()) | |
12548 | ast_dump_context->ostream() << ", "; | |
12549 | ast_dump_context->ostream() << *pi << ':'; | |
12550 | ast_dump_context->dump_expression(*pv); | |
12551 | } | |
12552 | } | |
d751bb78 | 12553 | ast_dump_context->ostream() << "}" ; |
12554 | ||
12555 | } | |
12556 | ||
da244e59 | 12557 | // Class Fixed_array_construction_expression. |
e440a328 | 12558 | |
da244e59 | 12559 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
12560 | Type* type, const std::vector<unsigned long>* indexes, | |
12561 | Expression_list* vals, Location location) | |
12562 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
12563 | type, indexes, vals, location) | |
12564 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12565 | |
ea664253 | 12566 | // Return the backend representation for constructing a fixed array. |
e440a328 | 12567 | |
ea664253 | 12568 | Bexpression* |
12569 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12570 | { |
9f0e0513 | 12571 | Type* type = this->type(); |
12572 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 12573 | return this->get_constructor(context, btype); |
e440a328 | 12574 | } |
12575 | ||
76f85fd6 | 12576 | Expression* |
12577 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
12578 | Location location) | |
12579 | { | |
12580 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
12581 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
12582 | } | |
12583 | ||
da244e59 | 12584 | // Class Slice_construction_expression. |
e440a328 | 12585 | |
da244e59 | 12586 | Slice_construction_expression::Slice_construction_expression( |
12587 | Type* type, const std::vector<unsigned long>* indexes, | |
12588 | Expression_list* vals, Location location) | |
12589 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
12590 | type, indexes, vals, location), | |
0e9a2e72 | 12591 | valtype_(NULL), array_val_(NULL), slice_storage_(NULL), |
12592 | storage_escapes_(true) | |
e440a328 | 12593 | { |
da244e59 | 12594 | go_assert(type->is_slice_type()); |
23d77f91 | 12595 | |
da244e59 | 12596 | unsigned long lenval; |
12597 | Expression* length; | |
12598 | if (vals == NULL || vals->empty()) | |
12599 | lenval = 0; | |
12600 | else | |
12601 | { | |
12602 | if (this->indexes() == NULL) | |
12603 | lenval = vals->size(); | |
12604 | else | |
12605 | lenval = indexes->back() + 1; | |
12606 | } | |
12607 | Type* int_type = Type::lookup_integer_type("int"); | |
12608 | length = Expression::make_integer_ul(lenval, int_type, location); | |
12609 | Type* element_type = type->array_type()->element_type(); | |
12610 | this->valtype_ = Type::make_array_type(element_type, length); | |
12611 | } | |
e440a328 | 12612 | |
23d77f91 | 12613 | // Traversal. |
12614 | ||
12615 | int | |
12616 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
12617 | { | |
12618 | if (this->Array_construction_expression::do_traverse(traverse) | |
12619 | == TRAVERSE_EXIT) | |
12620 | return TRAVERSE_EXIT; | |
12621 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
12622 | return TRAVERSE_EXIT; | |
0e9a2e72 | 12623 | if (this->array_val_ != NULL |
12624 | && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) | |
12625 | return TRAVERSE_EXIT; | |
12626 | if (this->slice_storage_ != NULL | |
12627 | && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) | |
12628 | return TRAVERSE_EXIT; | |
23d77f91 | 12629 | return TRAVERSE_CONTINUE; |
12630 | } | |
12631 | ||
0e9a2e72 | 12632 | // Helper routine to create fixed array value underlying the slice literal. |
12633 | // May be called during flattening, or later during do_get_backend(). | |
e440a328 | 12634 | |
0e9a2e72 | 12635 | Expression* |
12636 | Slice_construction_expression::create_array_val() | |
e440a328 | 12637 | { |
f9c68f17 | 12638 | Array_type* array_type = this->type()->array_type(); |
12639 | if (array_type == NULL) | |
12640 | { | |
c484d925 | 12641 | go_assert(this->type()->is_error()); |
0e9a2e72 | 12642 | return NULL; |
f9c68f17 | 12643 | } |
12644 | ||
f23d7786 | 12645 | Location loc = this->location(); |
23d77f91 | 12646 | go_assert(this->valtype_ != NULL); |
3d60812e | 12647 | |
f23d7786 | 12648 | Expression_list* vals = this->vals(); |
e440a328 | 12649 | if (this->vals() == NULL || this->vals()->empty()) |
12650 | { | |
f23d7786 | 12651 | // We need to create a unique value for the empty array literal. |
12652 | vals = new Expression_list; | |
12653 | vals->push_back(NULL); | |
e440a328 | 12654 | } |
0e9a2e72 | 12655 | return new Fixed_array_construction_expression( |
12656 | this->valtype_, this->indexes(), vals, loc); | |
12657 | } | |
12658 | ||
12659 | // If we're previous established that the slice storage does not | |
12660 | // escape, then create a separate array temp val here for it. We | |
12661 | // need to do this as part of flattening so as to be able to insert | |
12662 | // the new temp statement. | |
12663 | ||
12664 | Expression* | |
12665 | Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no, | |
12666 | Statement_inserter* inserter) | |
12667 | { | |
12668 | if (this->type()->array_type() == NULL) | |
12669 | return NULL; | |
12670 | ||
12671 | // Base class flattening first | |
12672 | this->Array_construction_expression::do_flatten(gogo, no, inserter); | |
12673 | ||
12674 | // Create an stack-allocated storage temp if storage won't escape | |
12675 | if (!this->storage_escapes_) | |
12676 | { | |
12677 | Location loc = this->location(); | |
12678 | this->array_val_ = create_array_val(); | |
12679 | go_assert(this->array_val_); | |
12680 | Temporary_statement* temp = | |
12681 | Statement::make_temporary(this->valtype_, this->array_val_, loc); | |
12682 | inserter->insert(temp); | |
12683 | this->slice_storage_ = Expression::make_temporary_reference(temp, loc); | |
12684 | } | |
12685 | return this; | |
12686 | } | |
12687 | ||
12688 | // When dumping a slice construction expression that has an explicit | |
12689 | // storeage temp, emit the temp here (if we don't do this the storage | |
12690 | // temp appears unused in the AST dump). | |
12691 | ||
12692 | void | |
12693 | Slice_construction_expression:: | |
12694 | dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const | |
12695 | { | |
12696 | if (this->slice_storage_ == NULL) | |
12697 | return; | |
12698 | ast_dump_context->ostream() << "storage=" ; | |
12699 | ast_dump_context->dump_expression(this->slice_storage_); | |
12700 | } | |
12701 | ||
12702 | // Return the backend representation for constructing a slice. | |
12703 | ||
12704 | Bexpression* | |
12705 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
12706 | { | |
12707 | if (this->array_val_ == NULL) | |
12708 | this->array_val_ = create_array_val(); | |
12709 | if (this->array_val_ == NULL) | |
12710 | { | |
12711 | go_assert(this->type()->is_error()); | |
12712 | return context->backend()->error_expression(); | |
12713 | } | |
12714 | ||
12715 | Location loc = this->location(); | |
12716 | Array_type* array_type = this->type()->array_type(); | |
12717 | Type* element_type = array_type->element_type(); | |
e440a328 | 12718 | |
0e9a2e72 | 12719 | bool is_constant_initializer = this->array_val_->is_immutable(); |
d8829beb | 12720 | |
12721 | // We have to copy the initial values into heap memory if we are in | |
12722 | // a function or if the values are not constants. We also have to | |
12723 | // copy them if they may contain pointers in a non-constant context, | |
12724 | // as otherwise the garbage collector won't see them. | |
12725 | bool copy_to_heap = (context->function() != NULL | |
12726 | || !is_constant_initializer | |
12727 | || (element_type->has_pointer() | |
12728 | && !context->is_const())); | |
e440a328 | 12729 | |
f23d7786 | 12730 | Expression* space; |
0e9a2e72 | 12731 | |
12732 | if (this->slice_storage_ != NULL) | |
12733 | { | |
12734 | go_assert(!this->storage_escapes_); | |
12735 | space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); | |
12736 | } | |
12737 | else if (!copy_to_heap) | |
e440a328 | 12738 | { |
f23d7786 | 12739 | // The initializer will only run once. |
0e9a2e72 | 12740 | space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); |
f23d7786 | 12741 | space->unary_expression()->set_is_slice_init(); |
e440a328 | 12742 | } |
12743 | else | |
45ff893b | 12744 | { |
0e9a2e72 | 12745 | space = Expression::make_heap_expression(this->array_val_, loc); |
45ff893b | 12746 | Node* n = Node::make_node(this); |
12747 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
12748 | { | |
12749 | n = Node::make_node(space); | |
12750 | n->set_encoding(Node::ESCAPE_NONE); | |
12751 | } | |
12752 | } | |
e440a328 | 12753 | |
2c809f8f | 12754 | // Build a constructor for the slice. |
f23d7786 | 12755 | Expression* len = this->valtype_->array_type()->length(); |
12756 | Expression* slice_val = | |
12757 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 12758 | return slice_val->get_backend(context); |
e440a328 | 12759 | } |
12760 | ||
12761 | // Make a slice composite literal. This is used by the type | |
12762 | // descriptor code. | |
12763 | ||
0e9a2e72 | 12764 | Slice_construction_expression* |
e440a328 | 12765 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, |
b13c66cd | 12766 | Location location) |
e440a328 | 12767 | { |
411eb89e | 12768 | go_assert(type->is_slice_type()); |
2c809f8f | 12769 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 12770 | } |
12771 | ||
da244e59 | 12772 | // Class Map_construction_expression. |
e440a328 | 12773 | |
12774 | // Traversal. | |
12775 | ||
12776 | int | |
12777 | Map_construction_expression::do_traverse(Traverse* traverse) | |
12778 | { | |
12779 | if (this->vals_ != NULL | |
12780 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12781 | return TRAVERSE_EXIT; | |
12782 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12783 | return TRAVERSE_EXIT; | |
12784 | return TRAVERSE_CONTINUE; | |
12785 | } | |
12786 | ||
2c809f8f | 12787 | // Flatten constructor initializer into a temporary variable since |
12788 | // we need to take its address for __go_construct_map. | |
12789 | ||
12790 | Expression* | |
12791 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
12792 | Statement_inserter* inserter) | |
12793 | { | |
12794 | if (!this->is_error_expression() | |
12795 | && this->vals_ != NULL | |
12796 | && !this->vals_->empty() | |
12797 | && this->constructor_temp_ == NULL) | |
12798 | { | |
12799 | Map_type* mt = this->type_->map_type(); | |
12800 | Type* key_type = mt->key_type(); | |
12801 | Type* val_type = mt->val_type(); | |
12802 | this->element_type_ = Type::make_builtin_struct_type(2, | |
12803 | "__key", key_type, | |
12804 | "__val", val_type); | |
12805 | ||
12806 | Expression_list* value_pairs = new Expression_list(); | |
12807 | Location loc = this->location(); | |
12808 | ||
12809 | size_t i = 0; | |
12810 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12811 | pv != this->vals_->end(); | |
12812 | ++pv, ++i) | |
12813 | { | |
12814 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 12815 | Expression* key = *pv; |
5bf8be8b | 12816 | if (key->is_error_expression() || key->type()->is_error_type()) |
12817 | { | |
12818 | go_assert(saw_errors()); | |
12819 | return Expression::make_error(loc); | |
12820 | } | |
91c0fd76 | 12821 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
12822 | { | |
12823 | Temporary_statement* temp = | |
12824 | Statement::make_temporary(NULL, key, loc); | |
12825 | inserter->insert(temp); | |
12826 | key = Expression::make_temporary_reference(temp, loc); | |
12827 | } | |
12828 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 12829 | |
12830 | ++pv; | |
91c0fd76 | 12831 | Expression* val = *pv; |
5bf8be8b | 12832 | if (val->is_error_expression() || val->type()->is_error_type()) |
12833 | { | |
12834 | go_assert(saw_errors()); | |
12835 | return Expression::make_error(loc); | |
12836 | } | |
91c0fd76 | 12837 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
12838 | { | |
12839 | Temporary_statement* temp = | |
12840 | Statement::make_temporary(NULL, val, loc); | |
12841 | inserter->insert(temp); | |
12842 | val = Expression::make_temporary_reference(temp, loc); | |
12843 | } | |
12844 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 12845 | |
12846 | key_value_pair->push_back(key); | |
12847 | key_value_pair->push_back(val); | |
12848 | value_pairs->push_back( | |
12849 | Expression::make_struct_composite_literal(this->element_type_, | |
12850 | key_value_pair, loc)); | |
12851 | } | |
12852 | ||
e67508fa | 12853 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
2c809f8f | 12854 | Type* ctor_type = |
12855 | Type::make_array_type(this->element_type_, element_count); | |
12856 | Expression* constructor = | |
12857 | new Fixed_array_construction_expression(ctor_type, NULL, | |
12858 | value_pairs, loc); | |
12859 | ||
12860 | this->constructor_temp_ = | |
12861 | Statement::make_temporary(NULL, constructor, loc); | |
12862 | constructor->issue_nil_check(); | |
12863 | this->constructor_temp_->set_is_address_taken(); | |
12864 | inserter->insert(this->constructor_temp_); | |
12865 | } | |
12866 | ||
12867 | return this; | |
12868 | } | |
12869 | ||
e440a328 | 12870 | // Final type determination. |
12871 | ||
12872 | void | |
12873 | Map_construction_expression::do_determine_type(const Type_context*) | |
12874 | { | |
12875 | if (this->vals_ == NULL) | |
12876 | return; | |
12877 | ||
12878 | Map_type* mt = this->type_->map_type(); | |
12879 | Type_context key_context(mt->key_type(), false); | |
12880 | Type_context val_context(mt->val_type(), false); | |
12881 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12882 | pv != this->vals_->end(); | |
12883 | ++pv) | |
12884 | { | |
12885 | (*pv)->determine_type(&key_context); | |
12886 | ++pv; | |
12887 | (*pv)->determine_type(&val_context); | |
12888 | } | |
12889 | } | |
12890 | ||
12891 | // Check types. | |
12892 | ||
12893 | void | |
12894 | Map_construction_expression::do_check_types(Gogo*) | |
12895 | { | |
12896 | if (this->vals_ == NULL) | |
12897 | return; | |
12898 | ||
12899 | Map_type* mt = this->type_->map_type(); | |
12900 | int i = 0; | |
12901 | Type* key_type = mt->key_type(); | |
12902 | Type* val_type = mt->val_type(); | |
12903 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12904 | pv != this->vals_->end(); | |
12905 | ++pv, ++i) | |
12906 | { | |
12907 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
12908 | { | |
631d5788 | 12909 | go_error_at((*pv)->location(), |
12910 | "incompatible type for element %d key in map construction", | |
12911 | i + 1); | |
e440a328 | 12912 | this->set_is_error(); |
12913 | } | |
12914 | ++pv; | |
12915 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
12916 | { | |
631d5788 | 12917 | go_error_at((*pv)->location(), |
12918 | ("incompatible type for element %d value " | |
12919 | "in map construction"), | |
e440a328 | 12920 | i + 1); |
12921 | this->set_is_error(); | |
12922 | } | |
12923 | } | |
12924 | } | |
12925 | ||
ea664253 | 12926 | // Return the backend representation for constructing a map. |
e440a328 | 12927 | |
ea664253 | 12928 | Bexpression* |
12929 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12930 | { |
2c809f8f | 12931 | if (this->is_error_expression()) |
ea664253 | 12932 | return context->backend()->error_expression(); |
2c809f8f | 12933 | Location loc = this->location(); |
e440a328 | 12934 | |
e440a328 | 12935 | size_t i = 0; |
2c809f8f | 12936 | Expression* ventries; |
e440a328 | 12937 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 12938 | ventries = Expression::make_nil(loc); |
e440a328 | 12939 | else |
12940 | { | |
2c809f8f | 12941 | go_assert(this->constructor_temp_ != NULL); |
12942 | i = this->vals_->size() / 2; | |
e440a328 | 12943 | |
2c809f8f | 12944 | Expression* ctor_ref = |
12945 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
12946 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
12947 | } | |
e440a328 | 12948 | |
2c809f8f | 12949 | Map_type* mt = this->type_->map_type(); |
12950 | if (this->element_type_ == NULL) | |
12951 | this->element_type_ = | |
12952 | Type::make_builtin_struct_type(2, | |
12953 | "__key", mt->key_type(), | |
12954 | "__val", mt->val_type()); | |
0d5530d9 | 12955 | Expression* descriptor = Expression::make_type_descriptor(mt, loc); |
2c809f8f | 12956 | |
12957 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 12958 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 12959 | |
12960 | Expression* entry_size = | |
12961 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
12962 | ||
12963 | unsigned int field_index; | |
12964 | const Struct_field* valfield = | |
12965 | this->element_type_->find_local_field("__val", &field_index); | |
12966 | Expression* val_offset = | |
12967 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
2c809f8f | 12968 | |
12969 | Expression* map_ctor = | |
0d5530d9 | 12970 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, |
12971 | entry_size, val_offset, ventries); | |
ea664253 | 12972 | return map_ctor->get_backend(context); |
2c809f8f | 12973 | } |
e440a328 | 12974 | |
2c809f8f | 12975 | // Export an array construction. |
e440a328 | 12976 | |
2c809f8f | 12977 | void |
12978 | Map_construction_expression::do_export(Export* exp) const | |
12979 | { | |
12980 | exp->write_c_string("convert("); | |
12981 | exp->write_type(this->type_); | |
12982 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12983 | pv != this->vals_->end(); | |
12984 | ++pv) | |
12985 | { | |
12986 | exp->write_c_string(", "); | |
12987 | (*pv)->export_expression(exp); | |
12988 | } | |
12989 | exp->write_c_string(")"); | |
12990 | } | |
e440a328 | 12991 | |
2c809f8f | 12992 | // Dump ast representation for a map construction expression. |
d751bb78 | 12993 | |
12994 | void | |
12995 | Map_construction_expression::do_dump_expression( | |
12996 | Ast_dump_context* ast_dump_context) const | |
12997 | { | |
d751bb78 | 12998 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 12999 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13000 | ast_dump_context->ostream() << "}"; |
13001 | } | |
13002 | ||
7795ac51 | 13003 | // Class Composite_literal_expression. |
e440a328 | 13004 | |
13005 | // Traversal. | |
13006 | ||
13007 | int | |
13008 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13009 | { | |
dbffccfc | 13010 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 13011 | return TRAVERSE_EXIT; |
dbffccfc | 13012 | |
13013 | // If this is a struct composite literal with keys, then the keys | |
13014 | // are field names, not expressions. We don't want to traverse them | |
13015 | // in that case. If we do, we can give an erroneous error "variable | |
13016 | // initializer refers to itself." See bug482.go in the testsuite. | |
13017 | if (this->has_keys_ && this->vals_ != NULL) | |
13018 | { | |
13019 | // The type may not be resolvable at this point. | |
13020 | Type* type = this->type_; | |
a01f2481 | 13021 | |
7795ac51 | 13022 | for (int depth = 0; depth < this->depth_; ++depth) |
a01f2481 | 13023 | { |
13024 | if (type->array_type() != NULL) | |
13025 | type = type->array_type()->element_type(); | |
13026 | else if (type->map_type() != NULL) | |
7795ac51 | 13027 | { |
13028 | if (this->key_path_[depth]) | |
13029 | type = type->map_type()->key_type(); | |
13030 | else | |
13031 | type = type->map_type()->val_type(); | |
13032 | } | |
a01f2481 | 13033 | else |
13034 | { | |
13035 | // This error will be reported during lowering. | |
13036 | return TRAVERSE_CONTINUE; | |
13037 | } | |
13038 | } | |
13039 | ||
dbffccfc | 13040 | while (true) |
13041 | { | |
13042 | if (type->classification() == Type::TYPE_NAMED) | |
13043 | type = type->named_type()->real_type(); | |
13044 | else if (type->classification() == Type::TYPE_FORWARD) | |
13045 | { | |
13046 | Type* t = type->forwarded(); | |
13047 | if (t == type) | |
13048 | break; | |
13049 | type = t; | |
13050 | } | |
13051 | else | |
13052 | break; | |
13053 | } | |
13054 | ||
13055 | if (type->classification() == Type::TYPE_STRUCT) | |
13056 | { | |
13057 | Expression_list::iterator p = this->vals_->begin(); | |
13058 | while (p != this->vals_->end()) | |
13059 | { | |
13060 | // Skip key. | |
13061 | ++p; | |
13062 | go_assert(p != this->vals_->end()); | |
13063 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13064 | return TRAVERSE_EXIT; | |
13065 | ++p; | |
13066 | } | |
13067 | return TRAVERSE_CONTINUE; | |
13068 | } | |
13069 | } | |
13070 | ||
13071 | if (this->vals_ != NULL) | |
13072 | return this->vals_->traverse(traverse); | |
13073 | ||
13074 | return TRAVERSE_CONTINUE; | |
e440a328 | 13075 | } |
13076 | ||
13077 | // Lower a generic composite literal into a specific version based on | |
13078 | // the type. | |
13079 | ||
13080 | Expression* | |
ceeb4318 | 13081 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13082 | Statement_inserter* inserter, int) | |
e440a328 | 13083 | { |
13084 | Type* type = this->type_; | |
13085 | ||
7795ac51 | 13086 | for (int depth = 0; depth < this->depth_; ++depth) |
e440a328 | 13087 | { |
13088 | if (type->array_type() != NULL) | |
13089 | type = type->array_type()->element_type(); | |
13090 | else if (type->map_type() != NULL) | |
7795ac51 | 13091 | { |
13092 | if (this->key_path_[depth]) | |
13093 | type = type->map_type()->key_type(); | |
13094 | else | |
13095 | type = type->map_type()->val_type(); | |
13096 | } | |
e440a328 | 13097 | else |
13098 | { | |
5c13bd80 | 13099 | if (!type->is_error()) |
631d5788 | 13100 | go_error_at(this->location(), |
13101 | ("may only omit types within composite literals " | |
13102 | "of slice, array, or map type")); | |
e440a328 | 13103 | return Expression::make_error(this->location()); |
13104 | } | |
13105 | } | |
13106 | ||
e00772b3 | 13107 | Type *pt = type->points_to(); |
13108 | bool is_pointer = false; | |
13109 | if (pt != NULL) | |
13110 | { | |
13111 | is_pointer = true; | |
13112 | type = pt; | |
13113 | } | |
13114 | ||
13115 | Expression* ret; | |
5c13bd80 | 13116 | if (type->is_error()) |
e440a328 | 13117 | return Expression::make_error(this->location()); |
13118 | else if (type->struct_type() != NULL) | |
e00772b3 | 13119 | ret = this->lower_struct(gogo, type); |
e440a328 | 13120 | else if (type->array_type() != NULL) |
113ef6a5 | 13121 | ret = this->lower_array(type); |
e440a328 | 13122 | else if (type->map_type() != NULL) |
e00772b3 | 13123 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13124 | else |
13125 | { | |
631d5788 | 13126 | go_error_at(this->location(), |
13127 | ("expected struct, slice, array, or map type " | |
13128 | "for composite literal")); | |
e440a328 | 13129 | return Expression::make_error(this->location()); |
13130 | } | |
e00772b3 | 13131 | |
13132 | if (is_pointer) | |
2c809f8f | 13133 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13134 | |
13135 | return ret; | |
e440a328 | 13136 | } |
13137 | ||
13138 | // Lower a struct composite literal. | |
13139 | ||
13140 | Expression* | |
81c4b26b | 13141 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13142 | { |
b13c66cd | 13143 | Location location = this->location(); |
e440a328 | 13144 | Struct_type* st = type->struct_type(); |
13145 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13146 | { |
e6013c28 | 13147 | if (this->vals_ != NULL |
13148 | && !this->vals_->empty() | |
13149 | && type->named_type() != NULL | |
13150 | && type->named_type()->named_object()->package() != NULL) | |
13151 | { | |
13152 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13153 | pf != st->fields()->end(); | |
13154 | ++pf) | |
07daa4e7 | 13155 | { |
07ba7f26 | 13156 | if (Gogo::is_hidden_name(pf->field_name()) |
13157 | || pf->is_embedded_builtin(gogo)) | |
631d5788 | 13158 | go_error_at(this->location(), |
13159 | "assignment of unexported field %qs in %qs literal", | |
13160 | Gogo::message_name(pf->field_name()).c_str(), | |
13161 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13162 | } |
13163 | } | |
13164 | ||
13165 | return new Struct_construction_expression(type, this->vals_, location); | |
13166 | } | |
e440a328 | 13167 | |
13168 | size_t field_count = st->field_count(); | |
13169 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 13170 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 13171 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13172 | Expression* external_expr = NULL; |
13173 | const Named_object* external_no = NULL; | |
e440a328 | 13174 | while (p != this->vals_->end()) |
13175 | { | |
13176 | Expression* name_expr = *p; | |
13177 | ||
13178 | ++p; | |
c484d925 | 13179 | go_assert(p != this->vals_->end()); |
e440a328 | 13180 | Expression* val = *p; |
13181 | ||
13182 | ++p; | |
13183 | ||
13184 | if (name_expr == NULL) | |
13185 | { | |
631d5788 | 13186 | go_error_at(val->location(), |
13187 | "mixture of field and value initializers"); | |
e440a328 | 13188 | return Expression::make_error(location); |
13189 | } | |
13190 | ||
13191 | bool bad_key = false; | |
13192 | std::string name; | |
81c4b26b | 13193 | const Named_object* no = NULL; |
e440a328 | 13194 | switch (name_expr->classification()) |
13195 | { | |
13196 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13197 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 13198 | if (type->named_type() != NULL) |
13199 | { | |
13200 | // If the named object found for this field name comes from a | |
13201 | // different package than the struct it is a part of, do not count | |
13202 | // this incorrect lookup as a usage of the object's package. | |
13203 | no = name_expr->unknown_expression()->named_object(); | |
13204 | if (no->package() != NULL | |
13205 | && no->package() != type->named_type()->named_object()->package()) | |
13206 | no->package()->forget_usage(name_expr); | |
13207 | } | |
e440a328 | 13208 | break; |
13209 | ||
13210 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13211 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13212 | break; |
13213 | ||
13214 | case EXPRESSION_TYPE: | |
13215 | { | |
13216 | Type* t = name_expr->type(); | |
13217 | Named_type* nt = t->named_type(); | |
13218 | if (nt == NULL) | |
13219 | bad_key = true; | |
13220 | else | |
81c4b26b | 13221 | no = nt->named_object(); |
e440a328 | 13222 | } |
13223 | break; | |
13224 | ||
13225 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13226 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13227 | break; |
13228 | ||
b0c09712 | 13229 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
13230 | no = name_expr->enclosed_var_expression()->variable(); | |
e440a328 | 13231 | break; |
13232 | ||
b0c09712 | 13233 | case EXPRESSION_FUNC_REFERENCE: |
13234 | no = name_expr->func_expression()->named_object(); | |
e440a328 | 13235 | break; |
13236 | ||
13237 | default: | |
13238 | bad_key = true; | |
13239 | break; | |
13240 | } | |
13241 | if (bad_key) | |
13242 | { | |
631d5788 | 13243 | go_error_at(name_expr->location(), "expected struct field name"); |
e440a328 | 13244 | return Expression::make_error(location); |
13245 | } | |
13246 | ||
81c4b26b | 13247 | if (no != NULL) |
13248 | { | |
62750cd5 | 13249 | if (no->package() != NULL && external_expr == NULL) |
13250 | { | |
13251 | external_expr = name_expr; | |
13252 | external_no = no; | |
13253 | } | |
13254 | ||
81c4b26b | 13255 | name = no->name(); |
13256 | ||
13257 | // A predefined name won't be packed. If it starts with a | |
13258 | // lower case letter we need to check for that case, because | |
2d29d278 | 13259 | // the field name will be packed. FIXME. |
81c4b26b | 13260 | if (!Gogo::is_hidden_name(name) |
13261 | && name[0] >= 'a' | |
13262 | && name[0] <= 'z') | |
13263 | { | |
13264 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13265 | if (gno == no) | |
13266 | name = gogo->pack_hidden_name(name, false); | |
13267 | } | |
13268 | } | |
13269 | ||
e440a328 | 13270 | unsigned int index; |
13271 | const Struct_field* sf = st->find_local_field(name, &index); | |
13272 | if (sf == NULL) | |
13273 | { | |
631d5788 | 13274 | go_error_at(name_expr->location(), "unknown field %qs in %qs", |
13275 | Gogo::message_name(name).c_str(), | |
13276 | (type->named_type() != NULL | |
13277 | ? type->named_type()->message_name().c_str() | |
13278 | : "unnamed struct")); | |
e440a328 | 13279 | return Expression::make_error(location); |
13280 | } | |
13281 | if (vals[index] != NULL) | |
13282 | { | |
631d5788 | 13283 | go_error_at(name_expr->location(), |
13284 | "duplicate value for field %qs in %qs", | |
13285 | Gogo::message_name(name).c_str(), | |
13286 | (type->named_type() != NULL | |
13287 | ? type->named_type()->message_name().c_str() | |
13288 | : "unnamed struct")); | |
e440a328 | 13289 | return Expression::make_error(location); |
13290 | } | |
13291 | ||
07daa4e7 | 13292 | if (type->named_type() != NULL |
13293 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 13294 | && (Gogo::is_hidden_name(sf->field_name()) |
13295 | || sf->is_embedded_builtin(gogo))) | |
631d5788 | 13296 | go_error_at(name_expr->location(), |
13297 | "assignment of unexported field %qs in %qs literal", | |
13298 | Gogo::message_name(sf->field_name()).c_str(), | |
13299 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13300 | |
e440a328 | 13301 | vals[index] = val; |
0c4f5a19 | 13302 | traverse_order->push_back(index); |
e440a328 | 13303 | } |
13304 | ||
62750cd5 | 13305 | if (!this->all_are_names_) |
13306 | { | |
13307 | // This is a weird case like bug462 in the testsuite. | |
13308 | if (external_expr == NULL) | |
631d5788 | 13309 | go_error_at(this->location(), "unknown field in %qs literal", |
13310 | (type->named_type() != NULL | |
13311 | ? type->named_type()->message_name().c_str() | |
13312 | : "unnamed struct")); | |
62750cd5 | 13313 | else |
631d5788 | 13314 | go_error_at(external_expr->location(), "unknown field %qs in %qs", |
13315 | external_no->message_name().c_str(), | |
13316 | (type->named_type() != NULL | |
13317 | ? type->named_type()->message_name().c_str() | |
13318 | : "unnamed struct")); | |
62750cd5 | 13319 | return Expression::make_error(location); |
13320 | } | |
13321 | ||
e440a328 | 13322 | Expression_list* list = new Expression_list; |
13323 | list->reserve(field_count); | |
13324 | for (size_t i = 0; i < field_count; ++i) | |
13325 | list->push_back(vals[i]); | |
13326 | ||
0c4f5a19 | 13327 | Struct_construction_expression* ret = |
13328 | new Struct_construction_expression(type, list, location); | |
13329 | ret->set_traverse_order(traverse_order); | |
13330 | return ret; | |
e440a328 | 13331 | } |
13332 | ||
00773463 | 13333 | // Used to sort an index/value array. |
13334 | ||
13335 | class Index_value_compare | |
13336 | { | |
13337 | public: | |
13338 | bool | |
13339 | operator()(const std::pair<unsigned long, Expression*>& a, | |
13340 | const std::pair<unsigned long, Expression*>& b) | |
13341 | { return a.first < b.first; } | |
13342 | }; | |
13343 | ||
e440a328 | 13344 | // Lower an array composite literal. |
13345 | ||
13346 | Expression* | |
113ef6a5 | 13347 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13348 | { |
b13c66cd | 13349 | Location location = this->location(); |
e440a328 | 13350 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13351 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13352 | |
ffe743ca | 13353 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13354 | indexes->reserve(this->vals_->size()); | |
00773463 | 13355 | bool indexes_out_of_order = false; |
ffe743ca | 13356 | Expression_list* vals = new Expression_list(); |
13357 | vals->reserve(this->vals_->size()); | |
e440a328 | 13358 | unsigned long index = 0; |
13359 | Expression_list::const_iterator p = this->vals_->begin(); | |
13360 | while (p != this->vals_->end()) | |
13361 | { | |
13362 | Expression* index_expr = *p; | |
13363 | ||
13364 | ++p; | |
c484d925 | 13365 | go_assert(p != this->vals_->end()); |
e440a328 | 13366 | Expression* val = *p; |
13367 | ||
13368 | ++p; | |
13369 | ||
ffe743ca | 13370 | if (index_expr == NULL) |
13371 | { | |
13372 | if (!indexes->empty()) | |
13373 | indexes->push_back(index); | |
13374 | } | |
13375 | else | |
e440a328 | 13376 | { |
ffe743ca | 13377 | if (indexes->empty() && !vals->empty()) |
13378 | { | |
13379 | for (size_t i = 0; i < vals->size(); ++i) | |
13380 | indexes->push_back(i); | |
13381 | } | |
13382 | ||
0c77715b | 13383 | Numeric_constant nc; |
13384 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13385 | { |
631d5788 | 13386 | go_error_at(index_expr->location(), |
13387 | "index expression is not integer constant"); | |
e440a328 | 13388 | return Expression::make_error(location); |
13389 | } | |
6f6d9955 | 13390 | |
0c77715b | 13391 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13392 | { |
0c77715b | 13393 | case Numeric_constant::NC_UL_VALID: |
13394 | break; | |
13395 | case Numeric_constant::NC_UL_NOTINT: | |
631d5788 | 13396 | go_error_at(index_expr->location(), |
13397 | "index expression is not integer constant"); | |
0c77715b | 13398 | return Expression::make_error(location); |
13399 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 13400 | go_error_at(index_expr->location(), |
13401 | "index expression is negative"); | |
e440a328 | 13402 | return Expression::make_error(location); |
0c77715b | 13403 | case Numeric_constant::NC_UL_BIG: |
631d5788 | 13404 | go_error_at(index_expr->location(), "index value overflow"); |
e440a328 | 13405 | return Expression::make_error(location); |
0c77715b | 13406 | default: |
13407 | go_unreachable(); | |
e440a328 | 13408 | } |
6f6d9955 | 13409 | |
13410 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13411 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13412 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13413 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13414 | { |
631d5788 | 13415 | go_error_at(index_expr->location(), "index value overflow"); |
6f6d9955 | 13416 | return Expression::make_error(location); |
13417 | } | |
13418 | ||
ffe743ca | 13419 | if (std::find(indexes->begin(), indexes->end(), index) |
13420 | != indexes->end()) | |
e440a328 | 13421 | { |
631d5788 | 13422 | go_error_at(index_expr->location(), |
13423 | "duplicate value for index %lu", | |
13424 | index); | |
e440a328 | 13425 | return Expression::make_error(location); |
13426 | } | |
ffe743ca | 13427 | |
00773463 | 13428 | if (!indexes->empty() && index < indexes->back()) |
13429 | indexes_out_of_order = true; | |
13430 | ||
ffe743ca | 13431 | indexes->push_back(index); |
e440a328 | 13432 | } |
13433 | ||
ffe743ca | 13434 | vals->push_back(val); |
13435 | ||
e440a328 | 13436 | ++index; |
13437 | } | |
13438 | ||
ffe743ca | 13439 | if (indexes->empty()) |
13440 | { | |
13441 | delete indexes; | |
13442 | indexes = NULL; | |
13443 | } | |
e440a328 | 13444 | |
00773463 | 13445 | if (indexes_out_of_order) |
13446 | { | |
13447 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
13448 | ||
13449 | V v; | |
13450 | v.reserve(indexes->size()); | |
13451 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
13452 | for (Expression_list::const_iterator pe = vals->begin(); | |
13453 | pe != vals->end(); | |
13454 | ++pe, ++pi) | |
13455 | v.push_back(std::make_pair(*pi, *pe)); | |
13456 | ||
13457 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
13458 | ||
13459 | delete indexes; | |
13460 | delete vals; | |
13461 | indexes = new std::vector<unsigned long>(); | |
13462 | indexes->reserve(v.size()); | |
13463 | vals = new Expression_list(); | |
13464 | vals->reserve(v.size()); | |
13465 | ||
13466 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13467 | { | |
13468 | indexes->push_back(p->first); | |
13469 | vals->push_back(p->second); | |
13470 | } | |
13471 | } | |
13472 | ||
ffe743ca | 13473 | return this->make_array(type, indexes, vals); |
e440a328 | 13474 | } |
13475 | ||
13476 | // Actually build the array composite literal. This handles | |
13477 | // [...]{...}. | |
13478 | ||
13479 | Expression* | |
ffe743ca | 13480 | Composite_literal_expression::make_array( |
13481 | Type* type, | |
13482 | const std::vector<unsigned long>* indexes, | |
13483 | Expression_list* vals) | |
e440a328 | 13484 | { |
b13c66cd | 13485 | Location location = this->location(); |
e440a328 | 13486 | Array_type* at = type->array_type(); |
ffe743ca | 13487 | |
e440a328 | 13488 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13489 | { | |
ffe743ca | 13490 | size_t size; |
13491 | if (vals == NULL) | |
13492 | size = 0; | |
00773463 | 13493 | else if (indexes != NULL) |
13494 | size = indexes->back() + 1; | |
13495 | else | |
ffe743ca | 13496 | { |
13497 | size = vals->size(); | |
13498 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13499 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13500 | && size >> (it->bits() - 1) != 0) | |
13501 | { | |
631d5788 | 13502 | go_error_at(location, "too many elements in composite literal"); |
ffe743ca | 13503 | return Expression::make_error(location); |
13504 | } | |
13505 | } | |
ffe743ca | 13506 | |
e67508fa | 13507 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 13508 | at = Type::make_array_type(at->element_type(), elen); |
13509 | type = at; | |
13510 | } | |
ffe743ca | 13511 | else if (at->length() != NULL |
13512 | && !at->length()->is_error_expression() | |
13513 | && this->vals_ != NULL) | |
13514 | { | |
13515 | Numeric_constant nc; | |
13516 | unsigned long val; | |
13517 | if (at->length()->numeric_constant_value(&nc) | |
13518 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13519 | { | |
13520 | if (indexes == NULL) | |
13521 | { | |
13522 | if (this->vals_->size() > val) | |
13523 | { | |
631d5788 | 13524 | go_error_at(location, |
13525 | "too many elements in composite literal"); | |
ffe743ca | 13526 | return Expression::make_error(location); |
13527 | } | |
13528 | } | |
13529 | else | |
13530 | { | |
00773463 | 13531 | unsigned long max = indexes->back(); |
ffe743ca | 13532 | if (max >= val) |
13533 | { | |
631d5788 | 13534 | go_error_at(location, |
13535 | ("some element keys in composite literal " | |
13536 | "are out of range")); | |
ffe743ca | 13537 | return Expression::make_error(location); |
13538 | } | |
13539 | } | |
13540 | } | |
13541 | } | |
13542 | ||
e440a328 | 13543 | if (at->length() != NULL) |
ffe743ca | 13544 | return new Fixed_array_construction_expression(type, indexes, vals, |
13545 | location); | |
e440a328 | 13546 | else |
2c809f8f | 13547 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13548 | } |
13549 | ||
13550 | // Lower a map composite literal. | |
13551 | ||
13552 | Expression* | |
a287720d | 13553 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13554 | Statement_inserter* inserter, |
a287720d | 13555 | Type* type) |
e440a328 | 13556 | { |
b13c66cd | 13557 | Location location = this->location(); |
e440a328 | 13558 | if (this->vals_ != NULL) |
13559 | { | |
13560 | if (!this->has_keys_) | |
13561 | { | |
631d5788 | 13562 | go_error_at(location, "map composite literal must have keys"); |
e440a328 | 13563 | return Expression::make_error(location); |
13564 | } | |
13565 | ||
a287720d | 13566 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13567 | p != this->vals_->end(); |
13568 | p += 2) | |
13569 | { | |
13570 | if (*p == NULL) | |
13571 | { | |
13572 | ++p; | |
631d5788 | 13573 | go_error_at((*p)->location(), |
13574 | ("map composite literal must " | |
13575 | "have keys for every value")); | |
e440a328 | 13576 | return Expression::make_error(location); |
13577 | } | |
a287720d | 13578 | // Make sure we have lowered the key; it may not have been |
13579 | // lowered in order to handle keys for struct composite | |
13580 | // literals. Lower it now to get the right error message. | |
13581 | if ((*p)->unknown_expression() != NULL) | |
13582 | { | |
13583 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13584 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 13585 | go_assert((*p)->is_error_expression()); |
a287720d | 13586 | return Expression::make_error(location); |
13587 | } | |
e440a328 | 13588 | } |
13589 | } | |
13590 | ||
13591 | return new Map_construction_expression(type, this->vals_, location); | |
13592 | } | |
13593 | ||
d751bb78 | 13594 | // Dump ast representation for a composite literal expression. |
13595 | ||
13596 | void | |
13597 | Composite_literal_expression::do_dump_expression( | |
13598 | Ast_dump_context* ast_dump_context) const | |
13599 | { | |
8b1c301d | 13600 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13601 | ast_dump_context->dump_type(this->type_); |
13602 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13603 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13604 | ast_dump_context->ostream() << "})"; |
13605 | } | |
13606 | ||
e440a328 | 13607 | // Make a composite literal expression. |
13608 | ||
13609 | Expression* | |
13610 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 13611 | Expression_list* vals, bool all_are_names, |
b13c66cd | 13612 | Location location) |
e440a328 | 13613 | { |
13614 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 13615 | all_are_names, location); |
e440a328 | 13616 | } |
13617 | ||
13618 | // Return whether this expression is a composite literal. | |
13619 | ||
13620 | bool | |
13621 | Expression::is_composite_literal() const | |
13622 | { | |
13623 | switch (this->classification_) | |
13624 | { | |
13625 | case EXPRESSION_COMPOSITE_LITERAL: | |
13626 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13627 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 13628 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13629 | case EXPRESSION_MAP_CONSTRUCTION: |
13630 | return true; | |
13631 | default: | |
13632 | return false; | |
13633 | } | |
13634 | } | |
13635 | ||
13636 | // Return whether this expression is a composite literal which is not | |
13637 | // constant. | |
13638 | ||
13639 | bool | |
13640 | Expression::is_nonconstant_composite_literal() const | |
13641 | { | |
13642 | switch (this->classification_) | |
13643 | { | |
13644 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13645 | { | |
13646 | const Struct_construction_expression *psce = | |
13647 | static_cast<const Struct_construction_expression*>(this); | |
13648 | return !psce->is_constant_struct(); | |
13649 | } | |
13650 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13651 | { | |
13652 | const Fixed_array_construction_expression *pace = | |
13653 | static_cast<const Fixed_array_construction_expression*>(this); | |
13654 | return !pace->is_constant_array(); | |
13655 | } | |
2c809f8f | 13656 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13657 | { |
2c809f8f | 13658 | const Slice_construction_expression *pace = |
13659 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 13660 | return !pace->is_constant_array(); |
13661 | } | |
13662 | case EXPRESSION_MAP_CONSTRUCTION: | |
13663 | return true; | |
13664 | default: | |
13665 | return false; | |
13666 | } | |
13667 | } | |
13668 | ||
35a54f17 | 13669 | // Return true if this is a variable or temporary_variable. |
13670 | ||
13671 | bool | |
13672 | Expression::is_variable() const | |
13673 | { | |
13674 | switch (this->classification_) | |
13675 | { | |
13676 | case EXPRESSION_VAR_REFERENCE: | |
13677 | case EXPRESSION_TEMPORARY_REFERENCE: | |
13678 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
b0c09712 | 13679 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
35a54f17 | 13680 | return true; |
13681 | default: | |
13682 | return false; | |
13683 | } | |
13684 | } | |
13685 | ||
e440a328 | 13686 | // Return true if this is a reference to a local variable. |
13687 | ||
13688 | bool | |
13689 | Expression::is_local_variable() const | |
13690 | { | |
13691 | const Var_expression* ve = this->var_expression(); | |
13692 | if (ve == NULL) | |
13693 | return false; | |
13694 | const Named_object* no = ve->named_object(); | |
13695 | return (no->is_result_variable() | |
13696 | || (no->is_variable() && !no->var_value()->is_global())); | |
13697 | } | |
13698 | ||
13699 | // Class Type_guard_expression. | |
13700 | ||
13701 | // Traversal. | |
13702 | ||
13703 | int | |
13704 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13705 | { | |
13706 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13707 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13708 | return TRAVERSE_EXIT; | |
13709 | return TRAVERSE_CONTINUE; | |
13710 | } | |
13711 | ||
2c809f8f | 13712 | Expression* |
13713 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
13714 | Statement_inserter* inserter) | |
13715 | { | |
5bf8be8b | 13716 | if (this->expr_->is_error_expression() |
13717 | || this->expr_->type()->is_error_type()) | |
13718 | { | |
13719 | go_assert(saw_errors()); | |
13720 | return Expression::make_error(this->location()); | |
13721 | } | |
13722 | ||
2c809f8f | 13723 | if (!this->expr_->is_variable()) |
13724 | { | |
13725 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
13726 | this->location()); | |
13727 | inserter->insert(temp); | |
13728 | this->expr_ = | |
13729 | Expression::make_temporary_reference(temp, this->location()); | |
13730 | } | |
13731 | return this; | |
13732 | } | |
13733 | ||
e440a328 | 13734 | // Check types of a type guard expression. The expression must have |
13735 | // an interface type, but the actual type conversion is checked at run | |
13736 | // time. | |
13737 | ||
13738 | void | |
13739 | Type_guard_expression::do_check_types(Gogo*) | |
13740 | { | |
e440a328 | 13741 | Type* expr_type = this->expr_->type(); |
7e9da23f | 13742 | if (expr_type->interface_type() == NULL) |
f725ade8 | 13743 | { |
5c13bd80 | 13744 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 13745 | this->report_error(_("type assertion only valid for interface types")); |
13746 | this->set_is_error(); | |
13747 | } | |
e440a328 | 13748 | else if (this->type_->interface_type() == NULL) |
13749 | { | |
13750 | std::string reason; | |
13751 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
13752 | &reason)) | |
13753 | { | |
5c13bd80 | 13754 | if (!this->type_->is_error()) |
e440a328 | 13755 | { |
f725ade8 | 13756 | if (reason.empty()) |
13757 | this->report_error(_("impossible type assertion: " | |
13758 | "type does not implement interface")); | |
13759 | else | |
631d5788 | 13760 | go_error_at(this->location(), |
13761 | ("impossible type assertion: " | |
13762 | "type does not implement interface (%s)"), | |
13763 | reason.c_str()); | |
e440a328 | 13764 | } |
f725ade8 | 13765 | this->set_is_error(); |
e440a328 | 13766 | } |
13767 | } | |
13768 | } | |
13769 | ||
ea664253 | 13770 | // Return the backend representation for a type guard expression. |
e440a328 | 13771 | |
ea664253 | 13772 | Bexpression* |
13773 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13774 | { |
2c809f8f | 13775 | Expression* conversion; |
7e9da23f | 13776 | if (this->type_->interface_type() != NULL) |
2c809f8f | 13777 | conversion = |
13778 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
13779 | true, this->location()); | |
e440a328 | 13780 | else |
2c809f8f | 13781 | conversion = |
13782 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
13783 | this->expr_, this->location()); | |
13784 | ||
ea664253 | 13785 | return conversion->get_backend(context); |
e440a328 | 13786 | } |
13787 | ||
d751bb78 | 13788 | // Dump ast representation for a type guard expression. |
13789 | ||
13790 | void | |
2c809f8f | 13791 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 13792 | const |
13793 | { | |
13794 | this->expr_->dump_expression(ast_dump_context); | |
13795 | ast_dump_context->ostream() << "."; | |
13796 | ast_dump_context->dump_type(this->type_); | |
13797 | } | |
13798 | ||
e440a328 | 13799 | // Make a type guard expression. |
13800 | ||
13801 | Expression* | |
13802 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 13803 | Location location) |
e440a328 | 13804 | { |
13805 | return new Type_guard_expression(expr, type, location); | |
13806 | } | |
13807 | ||
2c809f8f | 13808 | // Class Heap_expression. |
e440a328 | 13809 | |
da244e59 | 13810 | // Return the type of the expression stored on the heap. |
e440a328 | 13811 | |
da244e59 | 13812 | Type* |
13813 | Heap_expression::do_type() | |
13814 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 13815 | |
ea664253 | 13816 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 13817 | |
ea664253 | 13818 | Bexpression* |
13819 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13820 | { |
02c19a1a | 13821 | if (this->expr_->is_error_expression() || this->expr_->type()->is_error()) |
ea664253 | 13822 | return context->backend()->error_expression(); |
2c809f8f | 13823 | |
02c19a1a | 13824 | Location loc = this->location(); |
2c809f8f | 13825 | Gogo* gogo = context->gogo(); |
02c19a1a | 13826 | Btype* btype = this->type()->get_backend(gogo); |
45ff893b | 13827 | |
13828 | Expression* alloc = Expression::make_allocation(this->expr_->type(), loc); | |
13829 | Node* n = Node::make_node(this); | |
13830 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
13831 | alloc->allocation_expression()->set_allocate_on_stack(); | |
13832 | Bexpression* space = alloc->get_backend(context); | |
02c19a1a | 13833 | |
13834 | Bstatement* decl; | |
13835 | Named_object* fn = context->function(); | |
13836 | go_assert(fn != NULL); | |
13837 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
13838 | Bvariable* space_temp = | |
13839 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
13840 | space, true, loc, &decl); | |
13841 | space = gogo->backend()->var_expression(space_temp, loc); | |
9b27b43c | 13842 | Btype* expr_btype = this->expr_->type()->get_backend(gogo); |
13843 | Bexpression* ref = | |
13844 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
02c19a1a | 13845 | |
ea664253 | 13846 | Bexpression* bexpr = this->expr_->get_backend(context); |
02c19a1a | 13847 | Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc); |
13848 | decl = gogo->backend()->compound_statement(decl, assn); | |
13849 | space = gogo->backend()->var_expression(space_temp, loc); | |
ea664253 | 13850 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 13851 | } |
13852 | ||
2c809f8f | 13853 | // Dump ast representation for a heap expression. |
d751bb78 | 13854 | |
13855 | void | |
2c809f8f | 13856 | Heap_expression::do_dump_expression( |
d751bb78 | 13857 | Ast_dump_context* ast_dump_context) const |
13858 | { | |
13859 | ast_dump_context->ostream() << "&("; | |
13860 | ast_dump_context->dump_expression(this->expr_); | |
13861 | ast_dump_context->ostream() << ")"; | |
13862 | } | |
13863 | ||
2c809f8f | 13864 | // Allocate an expression on the heap. |
e440a328 | 13865 | |
13866 | Expression* | |
2c809f8f | 13867 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 13868 | { |
2c809f8f | 13869 | return new Heap_expression(expr, location); |
e440a328 | 13870 | } |
13871 | ||
13872 | // Class Receive_expression. | |
13873 | ||
13874 | // Return the type of a receive expression. | |
13875 | ||
13876 | Type* | |
13877 | Receive_expression::do_type() | |
13878 | { | |
e429e3bd | 13879 | if (this->is_error_expression()) |
13880 | return Type::make_error_type(); | |
e440a328 | 13881 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
13882 | if (channel_type == NULL) | |
e429e3bd | 13883 | { |
13884 | this->report_error(_("expected channel")); | |
13885 | return Type::make_error_type(); | |
13886 | } | |
e440a328 | 13887 | return channel_type->element_type(); |
13888 | } | |
13889 | ||
13890 | // Check types for a receive expression. | |
13891 | ||
13892 | void | |
13893 | Receive_expression::do_check_types(Gogo*) | |
13894 | { | |
13895 | Type* type = this->channel_->type(); | |
5c13bd80 | 13896 | if (type->is_error()) |
e440a328 | 13897 | { |
e429e3bd | 13898 | go_assert(saw_errors()); |
e440a328 | 13899 | this->set_is_error(); |
13900 | return; | |
13901 | } | |
13902 | if (type->channel_type() == NULL) | |
13903 | { | |
13904 | this->report_error(_("expected channel")); | |
13905 | return; | |
13906 | } | |
13907 | if (!type->channel_type()->may_receive()) | |
13908 | { | |
13909 | this->report_error(_("invalid receive on send-only channel")); | |
13910 | return; | |
13911 | } | |
13912 | } | |
13913 | ||
2c809f8f | 13914 | // Flattening for receive expressions creates a temporary variable to store |
13915 | // received data in for receives. | |
13916 | ||
13917 | Expression* | |
13918 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
13919 | Statement_inserter* inserter) | |
13920 | { | |
13921 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13922 | if (channel_type == NULL) | |
13923 | { | |
13924 | go_assert(saw_errors()); | |
13925 | return this; | |
13926 | } | |
5bf8be8b | 13927 | else if (this->channel_->is_error_expression()) |
13928 | { | |
13929 | go_assert(saw_errors()); | |
13930 | return Expression::make_error(this->location()); | |
13931 | } | |
2c809f8f | 13932 | |
13933 | Type* element_type = channel_type->element_type(); | |
13934 | if (this->temp_receiver_ == NULL) | |
13935 | { | |
13936 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
13937 | this->location()); | |
13938 | this->temp_receiver_->set_is_address_taken(); | |
13939 | inserter->insert(this->temp_receiver_); | |
13940 | } | |
13941 | ||
13942 | return this; | |
13943 | } | |
13944 | ||
ea664253 | 13945 | // Get the backend representation for a receive expression. |
e440a328 | 13946 | |
ea664253 | 13947 | Bexpression* |
13948 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13949 | { |
f24f10bb | 13950 | Location loc = this->location(); |
13951 | ||
e440a328 | 13952 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 13953 | if (channel_type == NULL) |
13954 | { | |
c484d925 | 13955 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 13956 | return context->backend()->error_expression(); |
5b8368f4 | 13957 | } |
f24f10bb | 13958 | Expression* td = Expression::make_type_descriptor(channel_type, loc); |
e440a328 | 13959 | |
2c809f8f | 13960 | Expression* recv_ref = |
13961 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13962 | Expression* recv_addr = | |
13963 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13964 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
132ed071 | 13965 | Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 3, |
13966 | td, this->channel_, recv_addr); | |
ea664253 | 13967 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 13968 | } |
13969 | ||
d751bb78 | 13970 | // Dump ast representation for a receive expression. |
13971 | ||
13972 | void | |
13973 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13974 | { | |
13975 | ast_dump_context->ostream() << " <- " ; | |
13976 | ast_dump_context->dump_expression(channel_); | |
13977 | } | |
13978 | ||
e440a328 | 13979 | // Make a receive expression. |
13980 | ||
13981 | Receive_expression* | |
b13c66cd | 13982 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 13983 | { |
13984 | return new Receive_expression(channel, location); | |
13985 | } | |
13986 | ||
e440a328 | 13987 | // An expression which evaluates to a pointer to the type descriptor |
13988 | // of a type. | |
13989 | ||
13990 | class Type_descriptor_expression : public Expression | |
13991 | { | |
13992 | public: | |
b13c66cd | 13993 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 13994 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
13995 | type_(type) | |
13996 | { } | |
13997 | ||
13998 | protected: | |
4b686186 | 13999 | int |
14000 | do_traverse(Traverse*); | |
14001 | ||
e440a328 | 14002 | Type* |
14003 | do_type() | |
14004 | { return Type::make_type_descriptor_ptr_type(); } | |
14005 | ||
f9ca30f9 | 14006 | bool |
14007 | do_is_immutable() const | |
14008 | { return true; } | |
14009 | ||
e440a328 | 14010 | void |
14011 | do_determine_type(const Type_context*) | |
14012 | { } | |
14013 | ||
14014 | Expression* | |
14015 | do_copy() | |
14016 | { return this; } | |
14017 | ||
ea664253 | 14018 | Bexpression* |
14019 | do_get_backend(Translate_context* context) | |
a1d23b41 | 14020 | { |
ea664253 | 14021 | return this->type_->type_descriptor_pointer(context->gogo(), |
14022 | this->location()); | |
a1d23b41 | 14023 | } |
e440a328 | 14024 | |
d751bb78 | 14025 | void |
14026 | do_dump_expression(Ast_dump_context*) const; | |
14027 | ||
e440a328 | 14028 | private: |
14029 | // The type for which this is the descriptor. | |
14030 | Type* type_; | |
14031 | }; | |
14032 | ||
4b686186 | 14033 | int |
14034 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
14035 | { | |
14036 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14037 | return TRAVERSE_EXIT; | |
14038 | return TRAVERSE_CONTINUE; | |
14039 | } | |
14040 | ||
d751bb78 | 14041 | // Dump ast representation for a type descriptor expression. |
14042 | ||
14043 | void | |
14044 | Type_descriptor_expression::do_dump_expression( | |
14045 | Ast_dump_context* ast_dump_context) const | |
14046 | { | |
14047 | ast_dump_context->dump_type(this->type_); | |
14048 | } | |
14049 | ||
e440a328 | 14050 | // Make a type descriptor expression. |
14051 | ||
14052 | Expression* | |
b13c66cd | 14053 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14054 | { |
14055 | return new Type_descriptor_expression(type, location); | |
14056 | } | |
14057 | ||
aa5ae575 | 14058 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
14059 | // of a type. | |
14060 | ||
14061 | class GC_symbol_expression : public Expression | |
14062 | { | |
14063 | public: | |
14064 | GC_symbol_expression(Type* type) | |
14065 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
14066 | type_(type) | |
14067 | {} | |
14068 | ||
14069 | protected: | |
14070 | Type* | |
14071 | do_type() | |
23d77f91 | 14072 | { return Type::lookup_integer_type("uintptr"); } |
aa5ae575 | 14073 | |
14074 | bool | |
14075 | do_is_immutable() const | |
14076 | { return true; } | |
14077 | ||
14078 | void | |
14079 | do_determine_type(const Type_context*) | |
14080 | { } | |
14081 | ||
14082 | Expression* | |
14083 | do_copy() | |
14084 | { return this; } | |
14085 | ||
14086 | Bexpression* | |
14087 | do_get_backend(Translate_context* context) | |
14088 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
14089 | ||
14090 | void | |
14091 | do_dump_expression(Ast_dump_context*) const; | |
14092 | ||
14093 | private: | |
14094 | // The type which this gc symbol describes. | |
14095 | Type* type_; | |
14096 | }; | |
14097 | ||
14098 | // Dump ast representation for a gc symbol expression. | |
14099 | ||
14100 | void | |
14101 | GC_symbol_expression::do_dump_expression( | |
14102 | Ast_dump_context* ast_dump_context) const | |
14103 | { | |
14104 | ast_dump_context->ostream() << "gcdata("; | |
14105 | ast_dump_context->dump_type(this->type_); | |
14106 | ast_dump_context->ostream() << ")"; | |
14107 | } | |
14108 | ||
14109 | // Make a gc symbol expression. | |
14110 | ||
14111 | Expression* | |
14112 | Expression::make_gc_symbol(Type* type) | |
14113 | { | |
14114 | return new GC_symbol_expression(type); | |
14115 | } | |
14116 | ||
e440a328 | 14117 | // An expression which evaluates to some characteristic of a type. |
14118 | // This is only used to initialize fields of a type descriptor. Using | |
14119 | // a new expression class is slightly inefficient but gives us a good | |
14120 | // separation between the frontend and the middle-end with regard to | |
14121 | // how types are laid out. | |
14122 | ||
14123 | class Type_info_expression : public Expression | |
14124 | { | |
14125 | public: | |
14126 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14127 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14128 | type_(type), type_info_(type_info) |
14129 | { } | |
14130 | ||
14131 | protected: | |
0e168074 | 14132 | bool |
14133 | do_is_immutable() const | |
14134 | { return true; } | |
14135 | ||
e440a328 | 14136 | Type* |
14137 | do_type(); | |
14138 | ||
14139 | void | |
14140 | do_determine_type(const Type_context*) | |
14141 | { } | |
14142 | ||
14143 | Expression* | |
14144 | do_copy() | |
14145 | { return this; } | |
14146 | ||
ea664253 | 14147 | Bexpression* |
14148 | do_get_backend(Translate_context* context); | |
e440a328 | 14149 | |
d751bb78 | 14150 | void |
14151 | do_dump_expression(Ast_dump_context*) const; | |
14152 | ||
e440a328 | 14153 | private: |
14154 | // The type for which we are getting information. | |
14155 | Type* type_; | |
14156 | // What information we want. | |
14157 | Type_info type_info_; | |
14158 | }; | |
14159 | ||
14160 | // The type is chosen to match what the type descriptor struct | |
14161 | // expects. | |
14162 | ||
14163 | Type* | |
14164 | Type_info_expression::do_type() | |
14165 | { | |
14166 | switch (this->type_info_) | |
14167 | { | |
14168 | case TYPE_INFO_SIZE: | |
14169 | return Type::lookup_integer_type("uintptr"); | |
14170 | case TYPE_INFO_ALIGNMENT: | |
14171 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14172 | return Type::lookup_integer_type("uint8"); | |
14173 | default: | |
c3e6f413 | 14174 | go_unreachable(); |
e440a328 | 14175 | } |
14176 | } | |
14177 | ||
ea664253 | 14178 | // Return the backend representation for type information. |
e440a328 | 14179 | |
ea664253 | 14180 | Bexpression* |
14181 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14182 | { |
927a01eb | 14183 | Gogo* gogo = context->gogo(); |
2a305b85 | 14184 | bool ok = true; |
3f378015 | 14185 | int64_t val; |
927a01eb | 14186 | switch (this->type_info_) |
e440a328 | 14187 | { |
927a01eb | 14188 | case TYPE_INFO_SIZE: |
2a305b85 | 14189 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 14190 | break; |
14191 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 14192 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 14193 | break; |
14194 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 14195 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 14196 | break; |
14197 | default: | |
14198 | go_unreachable(); | |
e440a328 | 14199 | } |
2a305b85 | 14200 | if (!ok) |
14201 | { | |
14202 | go_assert(saw_errors()); | |
14203 | return gogo->backend()->error_expression(); | |
14204 | } | |
3f378015 | 14205 | Expression* e = Expression::make_integer_int64(val, this->type(), |
14206 | this->location()); | |
14207 | return e->get_backend(context); | |
e440a328 | 14208 | } |
14209 | ||
d751bb78 | 14210 | // Dump ast representation for a type info expression. |
14211 | ||
14212 | void | |
14213 | Type_info_expression::do_dump_expression( | |
14214 | Ast_dump_context* ast_dump_context) const | |
14215 | { | |
14216 | ast_dump_context->ostream() << "typeinfo("; | |
14217 | ast_dump_context->dump_type(this->type_); | |
14218 | ast_dump_context->ostream() << ","; | |
14219 | ast_dump_context->ostream() << | |
14220 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14221 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
14222 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
14223 | : "unknown"); | |
14224 | ast_dump_context->ostream() << ")"; | |
14225 | } | |
14226 | ||
e440a328 | 14227 | // Make a type info expression. |
14228 | ||
14229 | Expression* | |
14230 | Expression::make_type_info(Type* type, Type_info type_info) | |
14231 | { | |
14232 | return new Type_info_expression(type, type_info); | |
14233 | } | |
14234 | ||
35a54f17 | 14235 | // An expression that evaluates to some characteristic of a slice. |
14236 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14237 | ||
14238 | class Slice_info_expression : public Expression | |
14239 | { | |
14240 | public: | |
14241 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14242 | Location location) | |
14243 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14244 | slice_(slice), slice_info_(slice_info) | |
14245 | { } | |
14246 | ||
14247 | protected: | |
14248 | Type* | |
14249 | do_type(); | |
14250 | ||
14251 | void | |
14252 | do_determine_type(const Type_context*) | |
14253 | { } | |
14254 | ||
14255 | Expression* | |
14256 | do_copy() | |
14257 | { | |
14258 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14259 | this->location()); | |
14260 | } | |
14261 | ||
ea664253 | 14262 | Bexpression* |
14263 | do_get_backend(Translate_context* context); | |
35a54f17 | 14264 | |
14265 | void | |
14266 | do_dump_expression(Ast_dump_context*) const; | |
14267 | ||
14268 | void | |
14269 | do_issue_nil_check() | |
14270 | { this->slice_->issue_nil_check(); } | |
14271 | ||
14272 | private: | |
14273 | // The slice for which we are getting information. | |
14274 | Expression* slice_; | |
14275 | // What information we want. | |
14276 | Slice_info slice_info_; | |
14277 | }; | |
14278 | ||
14279 | // Return the type of the slice info. | |
14280 | ||
14281 | Type* | |
14282 | Slice_info_expression::do_type() | |
14283 | { | |
14284 | switch (this->slice_info_) | |
14285 | { | |
14286 | case SLICE_INFO_VALUE_POINTER: | |
14287 | return Type::make_pointer_type( | |
14288 | this->slice_->type()->array_type()->element_type()); | |
14289 | case SLICE_INFO_LENGTH: | |
14290 | case SLICE_INFO_CAPACITY: | |
14291 | return Type::lookup_integer_type("int"); | |
14292 | default: | |
14293 | go_unreachable(); | |
14294 | } | |
14295 | } | |
14296 | ||
ea664253 | 14297 | // Return the backend information for slice information. |
35a54f17 | 14298 | |
ea664253 | 14299 | Bexpression* |
14300 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 14301 | { |
14302 | Gogo* gogo = context->gogo(); | |
ea664253 | 14303 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 14304 | switch (this->slice_info_) |
14305 | { | |
14306 | case SLICE_INFO_VALUE_POINTER: | |
14307 | case SLICE_INFO_LENGTH: | |
14308 | case SLICE_INFO_CAPACITY: | |
ea664253 | 14309 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
14310 | this->location()); | |
35a54f17 | 14311 | break; |
14312 | default: | |
14313 | go_unreachable(); | |
14314 | } | |
35a54f17 | 14315 | } |
14316 | ||
14317 | // Dump ast representation for a type info expression. | |
14318 | ||
14319 | void | |
14320 | Slice_info_expression::do_dump_expression( | |
14321 | Ast_dump_context* ast_dump_context) const | |
14322 | { | |
14323 | ast_dump_context->ostream() << "sliceinfo("; | |
14324 | this->slice_->dump_expression(ast_dump_context); | |
14325 | ast_dump_context->ostream() << ","; | |
14326 | ast_dump_context->ostream() << | |
14327 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14328 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14329 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14330 | : "unknown"); | |
14331 | ast_dump_context->ostream() << ")"; | |
14332 | } | |
14333 | ||
14334 | // Make a slice info expression. | |
14335 | ||
14336 | Expression* | |
14337 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14338 | Location location) | |
14339 | { | |
14340 | return new Slice_info_expression(slice, slice_info, location); | |
14341 | } | |
14342 | ||
2c809f8f | 14343 | // An expression that represents a slice value: a struct with value pointer, |
14344 | // length, and capacity fields. | |
14345 | ||
14346 | class Slice_value_expression : public Expression | |
14347 | { | |
14348 | public: | |
14349 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14350 | Expression* cap, Location location) | |
14351 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14352 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14353 | { } | |
14354 | ||
14355 | protected: | |
14356 | int | |
14357 | do_traverse(Traverse*); | |
14358 | ||
14359 | Type* | |
14360 | do_type() | |
14361 | { return this->type_; } | |
14362 | ||
14363 | void | |
14364 | do_determine_type(const Type_context*) | |
14365 | { go_unreachable(); } | |
14366 | ||
14367 | Expression* | |
14368 | do_copy() | |
14369 | { | |
14370 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14371 | this->len_->copy(), this->cap_->copy(), | |
14372 | this->location()); | |
14373 | } | |
14374 | ||
ea664253 | 14375 | Bexpression* |
14376 | do_get_backend(Translate_context* context); | |
2c809f8f | 14377 | |
14378 | void | |
14379 | do_dump_expression(Ast_dump_context*) const; | |
14380 | ||
14381 | private: | |
14382 | // The type of the slice value. | |
14383 | Type* type_; | |
14384 | // The pointer to the values in the slice. | |
14385 | Expression* valptr_; | |
14386 | // The length of the slice. | |
14387 | Expression* len_; | |
14388 | // The capacity of the slice. | |
14389 | Expression* cap_; | |
14390 | }; | |
14391 | ||
14392 | int | |
14393 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14394 | { | |
55e8ba6a | 14395 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
14396 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 14397 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
14398 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14399 | return TRAVERSE_EXIT; | |
14400 | return TRAVERSE_CONTINUE; | |
14401 | } | |
14402 | ||
ea664253 | 14403 | Bexpression* |
14404 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14405 | { |
14406 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14407 | vals[0] = this->valptr_->get_backend(context); |
14408 | vals[1] = this->len_->get_backend(context); | |
14409 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14410 | |
14411 | Gogo* gogo = context->gogo(); | |
14412 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14413 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14414 | } |
14415 | ||
14416 | void | |
14417 | Slice_value_expression::do_dump_expression( | |
14418 | Ast_dump_context* ast_dump_context) const | |
14419 | { | |
14420 | ast_dump_context->ostream() << "slicevalue("; | |
14421 | ast_dump_context->ostream() << "values: "; | |
14422 | this->valptr_->dump_expression(ast_dump_context); | |
14423 | ast_dump_context->ostream() << ", length: "; | |
14424 | this->len_->dump_expression(ast_dump_context); | |
14425 | ast_dump_context->ostream() << ", capacity: "; | |
14426 | this->cap_->dump_expression(ast_dump_context); | |
14427 | ast_dump_context->ostream() << ")"; | |
14428 | } | |
14429 | ||
14430 | Expression* | |
14431 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14432 | Expression* cap, Location location) | |
14433 | { | |
14434 | go_assert(at->is_slice_type()); | |
14435 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14436 | } | |
2387f644 | 14437 | |
14438 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14439 | // This is used to access the method table or underlying object of an interface. | |
14440 | ||
14441 | class Interface_info_expression : public Expression | |
14442 | { | |
14443 | public: | |
14444 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 14445 | Location location) |
2387f644 | 14446 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
14447 | iface_(iface), iface_info_(iface_info) | |
14448 | { } | |
14449 | ||
14450 | protected: | |
14451 | Type* | |
14452 | do_type(); | |
14453 | ||
14454 | void | |
14455 | do_determine_type(const Type_context*) | |
14456 | { } | |
14457 | ||
14458 | Expression* | |
14459 | do_copy() | |
14460 | { | |
14461 | return new Interface_info_expression(this->iface_->copy(), | |
14462 | this->iface_info_, this->location()); | |
14463 | } | |
14464 | ||
ea664253 | 14465 | Bexpression* |
14466 | do_get_backend(Translate_context* context); | |
2387f644 | 14467 | |
14468 | void | |
14469 | do_dump_expression(Ast_dump_context*) const; | |
14470 | ||
14471 | void | |
14472 | do_issue_nil_check() | |
14473 | { this->iface_->issue_nil_check(); } | |
14474 | ||
14475 | private: | |
14476 | // The interface for which we are getting information. | |
14477 | Expression* iface_; | |
14478 | // What information we want. | |
14479 | Interface_info iface_info_; | |
14480 | }; | |
14481 | ||
14482 | // Return the type of the interface info. | |
14483 | ||
14484 | Type* | |
14485 | Interface_info_expression::do_type() | |
14486 | { | |
14487 | switch (this->iface_info_) | |
14488 | { | |
14489 | case INTERFACE_INFO_METHODS: | |
14490 | { | |
625d3118 | 14491 | typedef Unordered_map(Interface_type*, Type*) Hashtable; |
14492 | static Hashtable result_types; | |
14493 | ||
14494 | Interface_type* itype = this->iface_->type()->interface_type(); | |
14495 | ||
14496 | Hashtable::const_iterator p = result_types.find(itype); | |
14497 | if (p != result_types.end()) | |
14498 | return p->second; | |
14499 | ||
2c809f8f | 14500 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
625d3118 | 14501 | if (itype->is_empty()) |
14502 | { | |
14503 | result_types[itype] = pdt; | |
14504 | return pdt; | |
14505 | } | |
2c809f8f | 14506 | |
2387f644 | 14507 | Location loc = this->location(); |
14508 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 14509 | sfl->push_back( |
14510 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
14511 | ||
2387f644 | 14512 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); |
14513 | p != itype->methods()->end(); | |
14514 | ++p) | |
14515 | { | |
14516 | Function_type* ft = p->type()->function_type(); | |
14517 | go_assert(ft->receiver() == NULL); | |
14518 | ||
14519 | const Typed_identifier_list* params = ft->parameters(); | |
14520 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
14521 | if (params != NULL) | |
14522 | mparams->reserve(params->size() + 1); | |
14523 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
14524 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
14525 | if (params != NULL) | |
14526 | { | |
14527 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
14528 | pp != params->end(); | |
14529 | ++pp) | |
14530 | mparams->push_back(*pp); | |
14531 | } | |
14532 | ||
14533 | Typed_identifier_list* mresults = (ft->results() == NULL | |
14534 | ? NULL | |
14535 | : ft->results()->copy()); | |
14536 | Backend_function_type* mft = | |
14537 | Type::make_backend_function_type(NULL, mparams, mresults, | |
14538 | ft->location()); | |
14539 | ||
14540 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
14541 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
14542 | } | |
14543 | ||
625d3118 | 14544 | Pointer_type *pt = Type::make_pointer_type(Type::make_struct_type(sfl, loc)); |
14545 | result_types[itype] = pt; | |
14546 | return pt; | |
2387f644 | 14547 | } |
14548 | case INTERFACE_INFO_OBJECT: | |
14549 | return Type::make_pointer_type(Type::make_void_type()); | |
14550 | default: | |
14551 | go_unreachable(); | |
14552 | } | |
14553 | } | |
14554 | ||
ea664253 | 14555 | // Return the backend representation for interface information. |
2387f644 | 14556 | |
ea664253 | 14557 | Bexpression* |
14558 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 14559 | { |
14560 | Gogo* gogo = context->gogo(); | |
ea664253 | 14561 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 14562 | switch (this->iface_info_) |
14563 | { | |
14564 | case INTERFACE_INFO_METHODS: | |
14565 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 14566 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
14567 | this->location()); | |
2387f644 | 14568 | break; |
14569 | default: | |
14570 | go_unreachable(); | |
14571 | } | |
2387f644 | 14572 | } |
14573 | ||
14574 | // Dump ast representation for an interface info expression. | |
14575 | ||
14576 | void | |
14577 | Interface_info_expression::do_dump_expression( | |
14578 | Ast_dump_context* ast_dump_context) const | |
14579 | { | |
2c809f8f | 14580 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 14581 | ast_dump_context->ostream() << "interfaceinfo("; |
14582 | this->iface_->dump_expression(ast_dump_context); | |
14583 | ast_dump_context->ostream() << ","; | |
14584 | ast_dump_context->ostream() << | |
2c809f8f | 14585 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
14586 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 14587 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
14588 | : "unknown"); | |
14589 | ast_dump_context->ostream() << ")"; | |
14590 | } | |
14591 | ||
14592 | // Make an interface info expression. | |
14593 | ||
14594 | Expression* | |
14595 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
14596 | Location location) | |
14597 | { | |
14598 | return new Interface_info_expression(iface, iface_info, location); | |
14599 | } | |
14600 | ||
2c809f8f | 14601 | // An expression that represents an interface value. The first field is either |
14602 | // a type descriptor for an empty interface or a pointer to the interface method | |
14603 | // table for a non-empty interface. The second field is always the object. | |
14604 | ||
14605 | class Interface_value_expression : public Expression | |
14606 | { | |
14607 | public: | |
14608 | Interface_value_expression(Type* type, Expression* first_field, | |
14609 | Expression* obj, Location location) | |
14610 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
14611 | type_(type), first_field_(first_field), obj_(obj) | |
14612 | { } | |
14613 | ||
14614 | protected: | |
14615 | int | |
14616 | do_traverse(Traverse*); | |
14617 | ||
14618 | Type* | |
14619 | do_type() | |
14620 | { return this->type_; } | |
14621 | ||
14622 | void | |
14623 | do_determine_type(const Type_context*) | |
14624 | { go_unreachable(); } | |
14625 | ||
14626 | Expression* | |
14627 | do_copy() | |
14628 | { | |
14629 | return new Interface_value_expression(this->type_, | |
14630 | this->first_field_->copy(), | |
14631 | this->obj_->copy(), this->location()); | |
14632 | } | |
14633 | ||
ea664253 | 14634 | Bexpression* |
14635 | do_get_backend(Translate_context* context); | |
2c809f8f | 14636 | |
14637 | void | |
14638 | do_dump_expression(Ast_dump_context*) const; | |
14639 | ||
14640 | private: | |
14641 | // The type of the interface value. | |
14642 | Type* type_; | |
14643 | // The first field of the interface (either a type descriptor or a pointer | |
14644 | // to the method table. | |
14645 | Expression* first_field_; | |
14646 | // The underlying object of the interface. | |
14647 | Expression* obj_; | |
14648 | }; | |
14649 | ||
14650 | int | |
14651 | Interface_value_expression::do_traverse(Traverse* traverse) | |
14652 | { | |
14653 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
14654 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
14655 | return TRAVERSE_EXIT; | |
14656 | return TRAVERSE_CONTINUE; | |
14657 | } | |
14658 | ||
ea664253 | 14659 | Bexpression* |
14660 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14661 | { |
14662 | std::vector<Bexpression*> vals(2); | |
ea664253 | 14663 | vals[0] = this->first_field_->get_backend(context); |
14664 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 14665 | |
14666 | Gogo* gogo = context->gogo(); | |
14667 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14668 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14669 | } |
14670 | ||
14671 | void | |
14672 | Interface_value_expression::do_dump_expression( | |
14673 | Ast_dump_context* ast_dump_context) const | |
14674 | { | |
14675 | ast_dump_context->ostream() << "interfacevalue("; | |
14676 | ast_dump_context->ostream() << | |
14677 | (this->type_->interface_type()->is_empty() | |
14678 | ? "type_descriptor: " | |
14679 | : "methods: "); | |
14680 | this->first_field_->dump_expression(ast_dump_context); | |
14681 | ast_dump_context->ostream() << ", object: "; | |
14682 | this->obj_->dump_expression(ast_dump_context); | |
14683 | ast_dump_context->ostream() << ")"; | |
14684 | } | |
14685 | ||
14686 | Expression* | |
14687 | Expression::make_interface_value(Type* type, Expression* first_value, | |
14688 | Expression* object, Location location) | |
14689 | { | |
14690 | return new Interface_value_expression(type, first_value, object, location); | |
14691 | } | |
14692 | ||
14693 | // An interface method table for a pair of types: an interface type and a type | |
14694 | // that implements that interface. | |
14695 | ||
14696 | class Interface_mtable_expression : public Expression | |
14697 | { | |
14698 | public: | |
14699 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
14700 | bool is_pointer, Location location) | |
14701 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
14702 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
14703 | method_table_type_(NULL), bvar_(NULL) | |
14704 | { } | |
14705 | ||
14706 | protected: | |
14707 | int | |
14708 | do_traverse(Traverse*); | |
14709 | ||
14710 | Type* | |
14711 | do_type(); | |
14712 | ||
14713 | bool | |
14714 | is_immutable() const | |
14715 | { return true; } | |
14716 | ||
14717 | void | |
14718 | do_determine_type(const Type_context*) | |
14719 | { go_unreachable(); } | |
14720 | ||
14721 | Expression* | |
14722 | do_copy() | |
14723 | { | |
14724 | return new Interface_mtable_expression(this->itype_, this->type_, | |
14725 | this->is_pointer_, this->location()); | |
14726 | } | |
14727 | ||
14728 | bool | |
14729 | do_is_addressable() const | |
14730 | { return true; } | |
14731 | ||
ea664253 | 14732 | Bexpression* |
14733 | do_get_backend(Translate_context* context); | |
2c809f8f | 14734 | |
14735 | void | |
14736 | do_dump_expression(Ast_dump_context*) const; | |
14737 | ||
14738 | private: | |
14739 | // The interface type for which the methods are defined. | |
14740 | Interface_type* itype_; | |
14741 | // The type to construct the interface method table for. | |
14742 | Type* type_; | |
14743 | // Whether this table contains the method set for the receiver type or the | |
14744 | // pointer receiver type. | |
14745 | bool is_pointer_; | |
14746 | // The type of the method table. | |
14747 | Type* method_table_type_; | |
14748 | // The backend variable that refers to the interface method table. | |
14749 | Bvariable* bvar_; | |
14750 | }; | |
14751 | ||
14752 | int | |
14753 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
14754 | { | |
14755 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
14756 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14757 | return TRAVERSE_EXIT; | |
14758 | return TRAVERSE_CONTINUE; | |
14759 | } | |
14760 | ||
14761 | Type* | |
14762 | Interface_mtable_expression::do_type() | |
14763 | { | |
14764 | if (this->method_table_type_ != NULL) | |
14765 | return this->method_table_type_; | |
14766 | ||
14767 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14768 | go_assert(!interface_methods->empty()); | |
14769 | ||
14770 | Struct_field_list* sfl = new Struct_field_list; | |
14771 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
14772 | this->location()); | |
14773 | sfl->push_back(Struct_field(tid)); | |
14774 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14775 | p != interface_methods->end(); | |
14776 | ++p) | |
14777 | sfl->push_back(Struct_field(*p)); | |
14778 | this->method_table_type_ = Type::make_struct_type(sfl, this->location()); | |
14779 | return this->method_table_type_; | |
14780 | } | |
14781 | ||
ea664253 | 14782 | Bexpression* |
14783 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14784 | { |
14785 | Gogo* gogo = context->gogo(); | |
2c809f8f | 14786 | Location loc = Linemap::predeclared_location(); |
14787 | if (this->bvar_ != NULL) | |
ea664253 | 14788 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 14789 | |
14790 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14791 | go_assert(!interface_methods->empty()); | |
14792 | ||
14793 | std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_") | |
14794 | + this->itype_->mangled_name(gogo) | |
14795 | + "__" | |
14796 | + this->type_->mangled_name(gogo)); | |
14797 | ||
14798 | // See whether this interface has any hidden methods. | |
14799 | bool has_hidden_methods = false; | |
14800 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14801 | p != interface_methods->end(); | |
14802 | ++p) | |
14803 | { | |
14804 | if (Gogo::is_hidden_name(p->name())) | |
14805 | { | |
14806 | has_hidden_methods = true; | |
14807 | break; | |
14808 | } | |
14809 | } | |
14810 | ||
14811 | // We already know that the named type is convertible to the | |
14812 | // interface. If the interface has hidden methods, and the named | |
14813 | // type is defined in a different package, then the interface | |
14814 | // conversion table will be defined by that other package. | |
14815 | if (has_hidden_methods | |
14816 | && this->type_->named_type() != NULL | |
14817 | && this->type_->named_type()->named_object()->package() != NULL) | |
14818 | { | |
14819 | Btype* btype = this->type()->get_backend(gogo); | |
14820 | this->bvar_ = | |
14821 | gogo->backend()->immutable_struct_reference(mangled_name, btype, loc); | |
ea664253 | 14822 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 14823 | } |
14824 | ||
14825 | // The first element is the type descriptor. | |
14826 | Type* td_type; | |
14827 | if (!this->is_pointer_) | |
14828 | td_type = this->type_; | |
14829 | else | |
14830 | td_type = Type::make_pointer_type(this->type_); | |
14831 | ||
14832 | // Build an interface method table for a type: a type descriptor followed by a | |
14833 | // list of function pointers, one for each interface method. This is used for | |
14834 | // interfaces. | |
14835 | Expression_list* svals = new Expression_list(); | |
14836 | svals->push_back(Expression::make_type_descriptor(td_type, loc)); | |
14837 | ||
14838 | Named_type* nt = this->type_->named_type(); | |
14839 | Struct_type* st = this->type_->struct_type(); | |
14840 | go_assert(nt != NULL || st != NULL); | |
14841 | ||
14842 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14843 | p != interface_methods->end(); | |
14844 | ++p) | |
14845 | { | |
14846 | bool is_ambiguous; | |
14847 | Method* m; | |
14848 | if (nt != NULL) | |
14849 | m = nt->method_function(p->name(), &is_ambiguous); | |
14850 | else | |
14851 | m = st->method_function(p->name(), &is_ambiguous); | |
14852 | go_assert(m != NULL); | |
14853 | Named_object* no = m->named_object(); | |
14854 | ||
14855 | go_assert(no->is_function() || no->is_function_declaration()); | |
14856 | svals->push_back(Expression::make_func_code_reference(no, loc)); | |
14857 | } | |
14858 | ||
14859 | Btype* btype = this->type()->get_backend(gogo); | |
14860 | Expression* mtable = Expression::make_struct_composite_literal(this->type(), | |
14861 | svals, loc); | |
ea664253 | 14862 | Bexpression* ctor = mtable->get_backend(context); |
2c809f8f | 14863 | |
14864 | bool is_public = has_hidden_methods && this->type_->named_type() != NULL; | |
14865 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, false, | |
14866 | !is_public, btype, loc); | |
14867 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
14868 | !is_public, btype, loc, ctor); | |
ea664253 | 14869 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 14870 | } |
14871 | ||
14872 | void | |
14873 | Interface_mtable_expression::do_dump_expression( | |
14874 | Ast_dump_context* ast_dump_context) const | |
14875 | { | |
14876 | ast_dump_context->ostream() << "__go_" | |
14877 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
14878 | ast_dump_context->dump_type(this->itype_); | |
14879 | ast_dump_context->ostream() << "__"; | |
14880 | ast_dump_context->dump_type(this->type_); | |
14881 | } | |
14882 | ||
14883 | Expression* | |
14884 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
14885 | bool is_pointer, Location location) | |
14886 | { | |
14887 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
14888 | } | |
14889 | ||
e440a328 | 14890 | // An expression which evaluates to the offset of a field within a |
14891 | // struct. This, like Type_info_expression, q.v., is only used to | |
14892 | // initialize fields of a type descriptor. | |
14893 | ||
14894 | class Struct_field_offset_expression : public Expression | |
14895 | { | |
14896 | public: | |
14897 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 14898 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
14899 | Linemap::predeclared_location()), | |
e440a328 | 14900 | type_(type), field_(field) |
14901 | { } | |
14902 | ||
14903 | protected: | |
f23d7786 | 14904 | bool |
14905 | do_is_immutable() const | |
14906 | { return true; } | |
14907 | ||
e440a328 | 14908 | Type* |
14909 | do_type() | |
14910 | { return Type::lookup_integer_type("uintptr"); } | |
14911 | ||
14912 | void | |
14913 | do_determine_type(const Type_context*) | |
14914 | { } | |
14915 | ||
14916 | Expression* | |
14917 | do_copy() | |
14918 | { return this; } | |
14919 | ||
ea664253 | 14920 | Bexpression* |
14921 | do_get_backend(Translate_context* context); | |
e440a328 | 14922 | |
d751bb78 | 14923 | void |
14924 | do_dump_expression(Ast_dump_context*) const; | |
14925 | ||
e440a328 | 14926 | private: |
14927 | // The type of the struct. | |
14928 | Struct_type* type_; | |
14929 | // The field. | |
14930 | const Struct_field* field_; | |
14931 | }; | |
14932 | ||
ea664253 | 14933 | // Return the backend representation for a struct field offset. |
e440a328 | 14934 | |
ea664253 | 14935 | Bexpression* |
14936 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14937 | { |
e440a328 | 14938 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 14939 | Struct_field_list::const_iterator p; |
2c8bda43 | 14940 | unsigned i = 0; |
e440a328 | 14941 | for (p = fields->begin(); |
14942 | p != fields->end(); | |
2c8bda43 | 14943 | ++p, ++i) |
14944 | if (&*p == this->field_) | |
14945 | break; | |
c484d925 | 14946 | go_assert(&*p == this->field_); |
e440a328 | 14947 | |
2c8bda43 | 14948 | Gogo* gogo = context->gogo(); |
14949 | Btype* btype = this->type_->get_backend(gogo); | |
14950 | ||
3f378015 | 14951 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 14952 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 14953 | Expression* ret = |
3f378015 | 14954 | Expression::make_integer_int64(offset, uptr_type, |
14955 | Linemap::predeclared_location()); | |
ea664253 | 14956 | return ret->get_backend(context); |
e440a328 | 14957 | } |
14958 | ||
d751bb78 | 14959 | // Dump ast representation for a struct field offset expression. |
14960 | ||
14961 | void | |
14962 | Struct_field_offset_expression::do_dump_expression( | |
14963 | Ast_dump_context* ast_dump_context) const | |
14964 | { | |
14965 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 14966 | ast_dump_context->dump_type(this->type_); |
14967 | ast_dump_context->ostream() << '.'; | |
14968 | ast_dump_context->ostream() << | |
14969 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 14970 | ast_dump_context->ostream() << ")"; |
14971 | } | |
14972 | ||
e440a328 | 14973 | // Make an expression for a struct field offset. |
14974 | ||
14975 | Expression* | |
14976 | Expression::make_struct_field_offset(Struct_type* type, | |
14977 | const Struct_field* field) | |
14978 | { | |
14979 | return new Struct_field_offset_expression(type, field); | |
14980 | } | |
14981 | ||
14982 | // An expression which evaluates to the address of an unnamed label. | |
14983 | ||
14984 | class Label_addr_expression : public Expression | |
14985 | { | |
14986 | public: | |
b13c66cd | 14987 | Label_addr_expression(Label* label, Location location) |
e440a328 | 14988 | : Expression(EXPRESSION_LABEL_ADDR, location), |
14989 | label_(label) | |
14990 | { } | |
14991 | ||
14992 | protected: | |
14993 | Type* | |
14994 | do_type() | |
14995 | { return Type::make_pointer_type(Type::make_void_type()); } | |
14996 | ||
14997 | void | |
14998 | do_determine_type(const Type_context*) | |
14999 | { } | |
15000 | ||
15001 | Expression* | |
15002 | do_copy() | |
15003 | { return new Label_addr_expression(this->label_, this->location()); } | |
15004 | ||
ea664253 | 15005 | Bexpression* |
15006 | do_get_backend(Translate_context* context) | |
15007 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 15008 | |
d751bb78 | 15009 | void |
15010 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15011 | { ast_dump_context->ostream() << this->label_->name(); } | |
15012 | ||
e440a328 | 15013 | private: |
15014 | // The label whose address we are taking. | |
15015 | Label* label_; | |
15016 | }; | |
15017 | ||
15018 | // Make an expression for the address of an unnamed label. | |
15019 | ||
15020 | Expression* | |
b13c66cd | 15021 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 15022 | { |
15023 | return new Label_addr_expression(label, location); | |
15024 | } | |
15025 | ||
da244e59 | 15026 | // Class Conditional_expression. |
283a177b | 15027 | |
2c809f8f | 15028 | // Traversal. |
15029 | ||
15030 | int | |
15031 | Conditional_expression::do_traverse(Traverse* traverse) | |
15032 | { | |
15033 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15034 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15035 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15036 | return TRAVERSE_EXIT; | |
15037 | return TRAVERSE_CONTINUE; | |
15038 | } | |
15039 | ||
283a177b | 15040 | // Return the type of the conditional expression. |
15041 | ||
15042 | Type* | |
15043 | Conditional_expression::do_type() | |
15044 | { | |
15045 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15046 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15047 | NULL)) | |
283a177b | 15048 | result_type = this->then_->type(); |
15049 | else if (this->then_->is_nil_expression() | |
15050 | || this->else_->is_nil_expression()) | |
15051 | result_type = (!this->then_->is_nil_expression() | |
15052 | ? this->then_->type() | |
15053 | : this->else_->type()); | |
15054 | return result_type; | |
15055 | } | |
15056 | ||
2c809f8f | 15057 | // Determine type for a conditional expression. |
15058 | ||
15059 | void | |
15060 | Conditional_expression::do_determine_type(const Type_context* context) | |
15061 | { | |
15062 | this->cond_->determine_type_no_context(); | |
15063 | this->then_->determine_type(context); | |
15064 | this->else_->determine_type(context); | |
15065 | } | |
15066 | ||
283a177b | 15067 | // Get the backend representation of a conditional expression. |
15068 | ||
ea664253 | 15069 | Bexpression* |
15070 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 15071 | { |
15072 | Gogo* gogo = context->gogo(); | |
15073 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 15074 | Bexpression* cond = this->cond_->get_backend(context); |
15075 | Bexpression* then = this->then_->get_backend(context); | |
15076 | Bexpression* belse = this->else_->get_backend(context); | |
15077 | return gogo->backend()->conditional_expression(result_btype, cond, then, | |
15078 | belse, this->location()); | |
283a177b | 15079 | } |
15080 | ||
15081 | // Dump ast representation of a conditional expression. | |
15082 | ||
15083 | void | |
15084 | Conditional_expression::do_dump_expression( | |
15085 | Ast_dump_context* ast_dump_context) const | |
15086 | { | |
15087 | ast_dump_context->ostream() << "("; | |
15088 | ast_dump_context->dump_expression(this->cond_); | |
15089 | ast_dump_context->ostream() << " ? "; | |
15090 | ast_dump_context->dump_expression(this->then_); | |
15091 | ast_dump_context->ostream() << " : "; | |
15092 | ast_dump_context->dump_expression(this->else_); | |
15093 | ast_dump_context->ostream() << ") "; | |
15094 | } | |
15095 | ||
15096 | // Make a conditional expression. | |
15097 | ||
15098 | Expression* | |
15099 | Expression::make_conditional(Expression* cond, Expression* then, | |
15100 | Expression* else_expr, Location location) | |
15101 | { | |
15102 | return new Conditional_expression(cond, then, else_expr, location); | |
15103 | } | |
15104 | ||
da244e59 | 15105 | // Class Compound_expression. |
2c809f8f | 15106 | |
15107 | // Traversal. | |
15108 | ||
15109 | int | |
15110 | Compound_expression::do_traverse(Traverse* traverse) | |
15111 | { | |
15112 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15113 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15114 | return TRAVERSE_EXIT; | |
15115 | return TRAVERSE_CONTINUE; | |
15116 | } | |
15117 | ||
15118 | // Return the type of the compound expression. | |
15119 | ||
15120 | Type* | |
15121 | Compound_expression::do_type() | |
15122 | { | |
15123 | return this->expr_->type(); | |
15124 | } | |
15125 | ||
15126 | // Determine type for a compound expression. | |
15127 | ||
15128 | void | |
15129 | Compound_expression::do_determine_type(const Type_context* context) | |
15130 | { | |
15131 | this->init_->determine_type_no_context(); | |
15132 | this->expr_->determine_type(context); | |
15133 | } | |
15134 | ||
15135 | // Get the backend representation of a compound expression. | |
15136 | ||
ea664253 | 15137 | Bexpression* |
15138 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15139 | { |
15140 | Gogo* gogo = context->gogo(); | |
ea664253 | 15141 | Bexpression* binit = this->init_->get_backend(context); |
2c809f8f | 15142 | Bstatement* init_stmt = gogo->backend()->expression_statement(binit); |
ea664253 | 15143 | Bexpression* bexpr = this->expr_->get_backend(context); |
15144 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15145 | this->location()); | |
2c809f8f | 15146 | } |
15147 | ||
15148 | // Dump ast representation of a conditional expression. | |
15149 | ||
15150 | void | |
15151 | Compound_expression::do_dump_expression( | |
15152 | Ast_dump_context* ast_dump_context) const | |
15153 | { | |
15154 | ast_dump_context->ostream() << "("; | |
15155 | ast_dump_context->dump_expression(this->init_); | |
15156 | ast_dump_context->ostream() << ","; | |
15157 | ast_dump_context->dump_expression(this->expr_); | |
15158 | ast_dump_context->ostream() << ") "; | |
15159 | } | |
15160 | ||
15161 | // Make a compound expression. | |
15162 | ||
15163 | Expression* | |
15164 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15165 | { | |
15166 | return new Compound_expression(init, expr, location); | |
15167 | } | |
15168 | ||
e440a328 | 15169 | // Import an expression. This comes at the end in order to see the |
15170 | // various class definitions. | |
15171 | ||
15172 | Expression* | |
15173 | Expression::import_expression(Import* imp) | |
15174 | { | |
15175 | int c = imp->peek_char(); | |
15176 | if (imp->match_c_string("- ") | |
15177 | || imp->match_c_string("! ") | |
15178 | || imp->match_c_string("^ ")) | |
15179 | return Unary_expression::do_import(imp); | |
15180 | else if (c == '(') | |
15181 | return Binary_expression::do_import(imp); | |
15182 | else if (imp->match_c_string("true") | |
15183 | || imp->match_c_string("false")) | |
15184 | return Boolean_expression::do_import(imp); | |
15185 | else if (c == '"') | |
15186 | return String_expression::do_import(imp); | |
15187 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15188 | { | |
15189 | // This handles integers, floats and complex constants. | |
15190 | return Integer_expression::do_import(imp); | |
15191 | } | |
15192 | else if (imp->match_c_string("nil")) | |
15193 | return Nil_expression::do_import(imp); | |
15194 | else if (imp->match_c_string("convert")) | |
15195 | return Type_conversion_expression::do_import(imp); | |
15196 | else | |
15197 | { | |
631d5788 | 15198 | go_error_at(imp->location(), "import error: expected expression"); |
e440a328 | 15199 | return Expression::make_error(imp->location()); |
15200 | } | |
15201 | } | |
15202 | ||
15203 | // Class Expression_list. | |
15204 | ||
15205 | // Traverse the list. | |
15206 | ||
15207 | int | |
15208 | Expression_list::traverse(Traverse* traverse) | |
15209 | { | |
15210 | for (Expression_list::iterator p = this->begin(); | |
15211 | p != this->end(); | |
15212 | ++p) | |
15213 | { | |
15214 | if (*p != NULL) | |
15215 | { | |
15216 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15217 | return TRAVERSE_EXIT; | |
15218 | } | |
15219 | } | |
15220 | return TRAVERSE_CONTINUE; | |
15221 | } | |
15222 | ||
15223 | // Copy the list. | |
15224 | ||
15225 | Expression_list* | |
15226 | Expression_list::copy() | |
15227 | { | |
15228 | Expression_list* ret = new Expression_list(); | |
15229 | for (Expression_list::iterator p = this->begin(); | |
15230 | p != this->end(); | |
15231 | ++p) | |
15232 | { | |
15233 | if (*p == NULL) | |
15234 | ret->push_back(NULL); | |
15235 | else | |
15236 | ret->push_back((*p)->copy()); | |
15237 | } | |
15238 | return ret; | |
15239 | } | |
15240 | ||
15241 | // Return whether an expression list has an error expression. | |
15242 | ||
15243 | bool | |
15244 | Expression_list::contains_error() const | |
15245 | { | |
15246 | for (Expression_list::const_iterator p = this->begin(); | |
15247 | p != this->end(); | |
15248 | ++p) | |
15249 | if (*p != NULL && (*p)->is_error_expression()) | |
15250 | return true; | |
15251 | return false; | |
15252 | } | |
0c77715b | 15253 | |
15254 | // Class Numeric_constant. | |
15255 | ||
15256 | // Destructor. | |
15257 | ||
15258 | Numeric_constant::~Numeric_constant() | |
15259 | { | |
15260 | this->clear(); | |
15261 | } | |
15262 | ||
15263 | // Copy constructor. | |
15264 | ||
15265 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15266 | : classification_(a.classification_), type_(a.type_) | |
15267 | { | |
15268 | switch (a.classification_) | |
15269 | { | |
15270 | case NC_INVALID: | |
15271 | break; | |
15272 | case NC_INT: | |
15273 | case NC_RUNE: | |
15274 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15275 | break; | |
15276 | case NC_FLOAT: | |
15277 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15278 | break; | |
15279 | case NC_COMPLEX: | |
fcbea5e4 | 15280 | mpc_init2(this->u_.complex_val, mpc_precision); |
15281 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15282 | break; |
15283 | default: | |
15284 | go_unreachable(); | |
15285 | } | |
15286 | } | |
15287 | ||
15288 | // Assignment operator. | |
15289 | ||
15290 | Numeric_constant& | |
15291 | Numeric_constant::operator=(const Numeric_constant& a) | |
15292 | { | |
15293 | this->clear(); | |
15294 | this->classification_ = a.classification_; | |
15295 | this->type_ = a.type_; | |
15296 | switch (a.classification_) | |
15297 | { | |
15298 | case NC_INVALID: | |
15299 | break; | |
15300 | case NC_INT: | |
15301 | case NC_RUNE: | |
15302 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15303 | break; | |
15304 | case NC_FLOAT: | |
15305 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15306 | break; | |
15307 | case NC_COMPLEX: | |
fcbea5e4 | 15308 | mpc_init2(this->u_.complex_val, mpc_precision); |
15309 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15310 | break; |
15311 | default: | |
15312 | go_unreachable(); | |
15313 | } | |
15314 | return *this; | |
15315 | } | |
15316 | ||
15317 | // Clear the contents. | |
15318 | ||
15319 | void | |
15320 | Numeric_constant::clear() | |
15321 | { | |
15322 | switch (this->classification_) | |
15323 | { | |
15324 | case NC_INVALID: | |
15325 | break; | |
15326 | case NC_INT: | |
15327 | case NC_RUNE: | |
15328 | mpz_clear(this->u_.int_val); | |
15329 | break; | |
15330 | case NC_FLOAT: | |
15331 | mpfr_clear(this->u_.float_val); | |
15332 | break; | |
15333 | case NC_COMPLEX: | |
fcbea5e4 | 15334 | mpc_clear(this->u_.complex_val); |
0c77715b | 15335 | break; |
15336 | default: | |
15337 | go_unreachable(); | |
15338 | } | |
15339 | this->classification_ = NC_INVALID; | |
15340 | } | |
15341 | ||
15342 | // Set to an unsigned long value. | |
15343 | ||
15344 | void | |
15345 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15346 | { | |
15347 | this->clear(); | |
15348 | this->classification_ = NC_INT; | |
15349 | this->type_ = type; | |
15350 | mpz_init_set_ui(this->u_.int_val, val); | |
15351 | } | |
15352 | ||
15353 | // Set to an integer value. | |
15354 | ||
15355 | void | |
15356 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15357 | { | |
15358 | this->clear(); | |
15359 | this->classification_ = NC_INT; | |
15360 | this->type_ = type; | |
15361 | mpz_init_set(this->u_.int_val, val); | |
15362 | } | |
15363 | ||
15364 | // Set to a rune value. | |
15365 | ||
15366 | void | |
15367 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15368 | { | |
15369 | this->clear(); | |
15370 | this->classification_ = NC_RUNE; | |
15371 | this->type_ = type; | |
15372 | mpz_init_set(this->u_.int_val, val); | |
15373 | } | |
15374 | ||
15375 | // Set to a floating point value. | |
15376 | ||
15377 | void | |
15378 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15379 | { | |
15380 | this->clear(); | |
15381 | this->classification_ = NC_FLOAT; | |
15382 | this->type_ = type; | |
833b523c | 15383 | // Numeric constants do not have negative zero values, so remove |
15384 | // them here. They also don't have infinity or NaN values, but we | |
15385 | // should never see them here. | |
15386 | if (mpfr_zero_p(val)) | |
15387 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
15388 | else | |
15389 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 15390 | } |
15391 | ||
15392 | // Set to a complex value. | |
15393 | ||
15394 | void | |
fcbea5e4 | 15395 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 15396 | { |
15397 | this->clear(); | |
15398 | this->classification_ = NC_COMPLEX; | |
15399 | this->type_ = type; | |
fcbea5e4 | 15400 | mpc_init2(this->u_.complex_val, mpc_precision); |
15401 | mpc_set(this->u_.complex_val, val, MPC_RNDNN); | |
0c77715b | 15402 | } |
15403 | ||
15404 | // Get an int value. | |
15405 | ||
15406 | void | |
15407 | Numeric_constant::get_int(mpz_t* val) const | |
15408 | { | |
15409 | go_assert(this->is_int()); | |
15410 | mpz_init_set(*val, this->u_.int_val); | |
15411 | } | |
15412 | ||
15413 | // Get a rune value. | |
15414 | ||
15415 | void | |
15416 | Numeric_constant::get_rune(mpz_t* val) const | |
15417 | { | |
15418 | go_assert(this->is_rune()); | |
15419 | mpz_init_set(*val, this->u_.int_val); | |
15420 | } | |
15421 | ||
15422 | // Get a floating point value. | |
15423 | ||
15424 | void | |
15425 | Numeric_constant::get_float(mpfr_t* val) const | |
15426 | { | |
15427 | go_assert(this->is_float()); | |
15428 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15429 | } | |
15430 | ||
15431 | // Get a complex value. | |
15432 | ||
15433 | void | |
fcbea5e4 | 15434 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 15435 | { |
15436 | go_assert(this->is_complex()); | |
fcbea5e4 | 15437 | mpc_init2(*val, mpc_precision); |
15438 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 15439 | } |
15440 | ||
15441 | // Express value as unsigned long if possible. | |
15442 | ||
15443 | Numeric_constant::To_unsigned_long | |
15444 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
15445 | { | |
15446 | switch (this->classification_) | |
15447 | { | |
15448 | case NC_INT: | |
15449 | case NC_RUNE: | |
15450 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
15451 | case NC_FLOAT: | |
15452 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
15453 | case NC_COMPLEX: | |
fcbea5e4 | 15454 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15455 | return NC_UL_NOTINT; |
fcbea5e4 | 15456 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
15457 | val); | |
0c77715b | 15458 | default: |
15459 | go_unreachable(); | |
15460 | } | |
15461 | } | |
15462 | ||
15463 | // Express integer value as unsigned long if possible. | |
15464 | ||
15465 | Numeric_constant::To_unsigned_long | |
15466 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
15467 | unsigned long *val) const | |
15468 | { | |
15469 | if (mpz_sgn(ival) < 0) | |
15470 | return NC_UL_NEGATIVE; | |
15471 | unsigned long ui = mpz_get_ui(ival); | |
15472 | if (mpz_cmp_ui(ival, ui) != 0) | |
15473 | return NC_UL_BIG; | |
15474 | *val = ui; | |
15475 | return NC_UL_VALID; | |
15476 | } | |
15477 | ||
15478 | // Express floating point value as unsigned long if possible. | |
15479 | ||
15480 | Numeric_constant::To_unsigned_long | |
15481 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
15482 | unsigned long *val) const | |
15483 | { | |
15484 | if (!mpfr_integer_p(fval)) | |
15485 | return NC_UL_NOTINT; | |
15486 | mpz_t ival; | |
15487 | mpz_init(ival); | |
15488 | mpfr_get_z(ival, fval, GMP_RNDN); | |
15489 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
15490 | mpz_clear(ival); | |
15491 | return ret; | |
15492 | } | |
15493 | ||
15494 | // Convert value to integer if possible. | |
15495 | ||
15496 | bool | |
15497 | Numeric_constant::to_int(mpz_t* val) const | |
15498 | { | |
15499 | switch (this->classification_) | |
15500 | { | |
15501 | case NC_INT: | |
15502 | case NC_RUNE: | |
15503 | mpz_init_set(*val, this->u_.int_val); | |
15504 | return true; | |
15505 | case NC_FLOAT: | |
15506 | if (!mpfr_integer_p(this->u_.float_val)) | |
15507 | return false; | |
15508 | mpz_init(*val); | |
15509 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
15510 | return true; | |
15511 | case NC_COMPLEX: | |
fcbea5e4 | 15512 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
15513 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 15514 | return false; |
15515 | mpz_init(*val); | |
fcbea5e4 | 15516 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15517 | return true; |
15518 | default: | |
15519 | go_unreachable(); | |
15520 | } | |
15521 | } | |
15522 | ||
15523 | // Convert value to floating point if possible. | |
15524 | ||
15525 | bool | |
15526 | Numeric_constant::to_float(mpfr_t* val) const | |
15527 | { | |
15528 | switch (this->classification_) | |
15529 | { | |
15530 | case NC_INT: | |
15531 | case NC_RUNE: | |
15532 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
15533 | return true; | |
15534 | case NC_FLOAT: | |
15535 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15536 | return true; | |
15537 | case NC_COMPLEX: | |
fcbea5e4 | 15538 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15539 | return false; |
fcbea5e4 | 15540 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15541 | return true; |
15542 | default: | |
15543 | go_unreachable(); | |
15544 | } | |
15545 | } | |
15546 | ||
15547 | // Convert value to complex. | |
15548 | ||
15549 | bool | |
fcbea5e4 | 15550 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 15551 | { |
fcbea5e4 | 15552 | mpc_init2(*val, mpc_precision); |
0c77715b | 15553 | switch (this->classification_) |
15554 | { | |
15555 | case NC_INT: | |
15556 | case NC_RUNE: | |
fcbea5e4 | 15557 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 15558 | return true; |
15559 | case NC_FLOAT: | |
fcbea5e4 | 15560 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 15561 | return true; |
15562 | case NC_COMPLEX: | |
fcbea5e4 | 15563 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 15564 | return true; |
15565 | default: | |
15566 | go_unreachable(); | |
15567 | } | |
15568 | } | |
15569 | ||
15570 | // Get the type. | |
15571 | ||
15572 | Type* | |
15573 | Numeric_constant::type() const | |
15574 | { | |
15575 | if (this->type_ != NULL) | |
15576 | return this->type_; | |
15577 | switch (this->classification_) | |
15578 | { | |
15579 | case NC_INT: | |
15580 | return Type::make_abstract_integer_type(); | |
15581 | case NC_RUNE: | |
15582 | return Type::make_abstract_character_type(); | |
15583 | case NC_FLOAT: | |
15584 | return Type::make_abstract_float_type(); | |
15585 | case NC_COMPLEX: | |
15586 | return Type::make_abstract_complex_type(); | |
15587 | default: | |
15588 | go_unreachable(); | |
15589 | } | |
15590 | } | |
15591 | ||
15592 | // If the constant can be expressed in TYPE, then set the type of the | |
15593 | // constant to TYPE and return true. Otherwise return false, and, if | |
15594 | // ISSUE_ERROR is true, report an appropriate error message. | |
15595 | ||
15596 | bool | |
15597 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
15598 | { | |
15599 | bool ret; | |
f11c2155 | 15600 | if (type == NULL || type->is_error()) |
0c77715b | 15601 | ret = true; |
15602 | else if (type->integer_type() != NULL) | |
15603 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
15604 | else if (type->float_type() != NULL) | |
15605 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
15606 | else if (type->complex_type() != NULL) | |
15607 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
15608 | else | |
5706ab68 | 15609 | { |
15610 | ret = false; | |
15611 | if (issue_error) | |
15612 | go_assert(saw_errors()); | |
15613 | } | |
0c77715b | 15614 | if (ret) |
15615 | this->type_ = type; | |
15616 | return ret; | |
15617 | } | |
15618 | ||
15619 | // Check whether the constant can be expressed in an integer type. | |
15620 | ||
15621 | bool | |
15622 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 15623 | Location location) |
0c77715b | 15624 | { |
15625 | mpz_t val; | |
15626 | switch (this->classification_) | |
15627 | { | |
15628 | case NC_INT: | |
15629 | case NC_RUNE: | |
15630 | mpz_init_set(val, this->u_.int_val); | |
15631 | break; | |
15632 | ||
15633 | case NC_FLOAT: | |
15634 | if (!mpfr_integer_p(this->u_.float_val)) | |
15635 | { | |
15636 | if (issue_error) | |
71a45216 | 15637 | { |
631d5788 | 15638 | go_error_at(location, |
15639 | "floating point constant truncated to integer"); | |
71a45216 | 15640 | this->set_invalid(); |
15641 | } | |
0c77715b | 15642 | return false; |
15643 | } | |
15644 | mpz_init(val); | |
15645 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
15646 | break; | |
15647 | ||
15648 | case NC_COMPLEX: | |
fcbea5e4 | 15649 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
15650 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 15651 | { |
15652 | if (issue_error) | |
71a45216 | 15653 | { |
631d5788 | 15654 | go_error_at(location, "complex constant truncated to integer"); |
71a45216 | 15655 | this->set_invalid(); |
15656 | } | |
0c77715b | 15657 | return false; |
15658 | } | |
15659 | mpz_init(val); | |
fcbea5e4 | 15660 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15661 | break; |
15662 | ||
15663 | default: | |
15664 | go_unreachable(); | |
15665 | } | |
15666 | ||
15667 | bool ret; | |
15668 | if (type->is_abstract()) | |
15669 | ret = true; | |
15670 | else | |
15671 | { | |
15672 | int bits = mpz_sizeinbase(val, 2); | |
15673 | if (type->is_unsigned()) | |
15674 | { | |
15675 | // For an unsigned type we can only accept a nonnegative | |
15676 | // number, and we must be able to represents at least BITS. | |
15677 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
15678 | } | |
15679 | else | |
15680 | { | |
15681 | // For a signed type we need an extra bit to indicate the | |
15682 | // sign. We have to handle the most negative integer | |
15683 | // specially. | |
15684 | ret = (bits + 1 <= type->bits() | |
15685 | || (bits <= type->bits() | |
15686 | && mpz_sgn(val) < 0 | |
15687 | && (mpz_scan1(val, 0) | |
15688 | == static_cast<unsigned long>(type->bits() - 1)) | |
15689 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
15690 | } | |
15691 | } | |
15692 | ||
15693 | if (!ret && issue_error) | |
71a45216 | 15694 | { |
631d5788 | 15695 | go_error_at(location, "integer constant overflow"); |
71a45216 | 15696 | this->set_invalid(); |
15697 | } | |
0c77715b | 15698 | |
15699 | return ret; | |
15700 | } | |
15701 | ||
15702 | // Check whether the constant can be expressed in a floating point | |
15703 | // type. | |
15704 | ||
15705 | bool | |
15706 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 15707 | Location location) |
0c77715b | 15708 | { |
15709 | mpfr_t val; | |
15710 | switch (this->classification_) | |
15711 | { | |
15712 | case NC_INT: | |
15713 | case NC_RUNE: | |
15714 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
15715 | break; | |
15716 | ||
15717 | case NC_FLOAT: | |
15718 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
15719 | break; | |
15720 | ||
15721 | case NC_COMPLEX: | |
fcbea5e4 | 15722 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15723 | { |
15724 | if (issue_error) | |
71a45216 | 15725 | { |
15726 | this->set_invalid(); | |
631d5788 | 15727 | go_error_at(location, "complex constant truncated to float"); |
71a45216 | 15728 | } |
0c77715b | 15729 | return false; |
15730 | } | |
fcbea5e4 | 15731 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15732 | break; |
15733 | ||
15734 | default: | |
15735 | go_unreachable(); | |
15736 | } | |
15737 | ||
15738 | bool ret; | |
15739 | if (type->is_abstract()) | |
15740 | ret = true; | |
15741 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
15742 | { | |
15743 | // A NaN or Infinity always fits in the range of the type. | |
15744 | ret = true; | |
15745 | } | |
15746 | else | |
15747 | { | |
15748 | mp_exp_t exp = mpfr_get_exp(val); | |
15749 | mp_exp_t max_exp; | |
15750 | switch (type->bits()) | |
15751 | { | |
15752 | case 32: | |
15753 | max_exp = 128; | |
15754 | break; | |
15755 | case 64: | |
15756 | max_exp = 1024; | |
15757 | break; | |
15758 | default: | |
15759 | go_unreachable(); | |
15760 | } | |
15761 | ||
15762 | ret = exp <= max_exp; | |
d0bcce51 | 15763 | |
15764 | if (ret) | |
15765 | { | |
15766 | // Round the constant to the desired type. | |
15767 | mpfr_t t; | |
15768 | mpfr_init(t); | |
15769 | switch (type->bits()) | |
15770 | { | |
15771 | case 32: | |
15772 | mpfr_set_prec(t, 24); | |
15773 | break; | |
15774 | case 64: | |
15775 | mpfr_set_prec(t, 53); | |
15776 | break; | |
15777 | default: | |
15778 | go_unreachable(); | |
15779 | } | |
15780 | mpfr_set(t, val, GMP_RNDN); | |
15781 | mpfr_set(val, t, GMP_RNDN); | |
15782 | mpfr_clear(t); | |
15783 | ||
15784 | this->set_float(type, val); | |
15785 | } | |
0c77715b | 15786 | } |
15787 | ||
15788 | mpfr_clear(val); | |
15789 | ||
15790 | if (!ret && issue_error) | |
71a45216 | 15791 | { |
631d5788 | 15792 | go_error_at(location, "floating point constant overflow"); |
71a45216 | 15793 | this->set_invalid(); |
15794 | } | |
0c77715b | 15795 | |
15796 | return ret; | |
15797 | } | |
15798 | ||
15799 | // Check whether the constant can be expressed in a complex type. | |
15800 | ||
15801 | bool | |
15802 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 15803 | Location location) |
0c77715b | 15804 | { |
15805 | if (type->is_abstract()) | |
15806 | return true; | |
15807 | ||
15808 | mp_exp_t max_exp; | |
15809 | switch (type->bits()) | |
15810 | { | |
15811 | case 64: | |
15812 | max_exp = 128; | |
15813 | break; | |
15814 | case 128: | |
15815 | max_exp = 1024; | |
15816 | break; | |
15817 | default: | |
15818 | go_unreachable(); | |
15819 | } | |
15820 | ||
fcbea5e4 | 15821 | mpc_t val; |
15822 | mpc_init2(val, mpc_precision); | |
0c77715b | 15823 | switch (this->classification_) |
15824 | { | |
15825 | case NC_INT: | |
15826 | case NC_RUNE: | |
fcbea5e4 | 15827 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 15828 | break; |
15829 | ||
15830 | case NC_FLOAT: | |
fcbea5e4 | 15831 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 15832 | break; |
15833 | ||
15834 | case NC_COMPLEX: | |
fcbea5e4 | 15835 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 15836 | break; |
15837 | ||
15838 | default: | |
15839 | go_unreachable(); | |
15840 | } | |
15841 | ||
d0bcce51 | 15842 | bool ret = true; |
fcbea5e4 | 15843 | if (!mpfr_nan_p(mpc_realref(val)) |
15844 | && !mpfr_inf_p(mpc_realref(val)) | |
15845 | && !mpfr_zero_p(mpc_realref(val)) | |
15846 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 15847 | { |
15848 | if (issue_error) | |
71a45216 | 15849 | { |
631d5788 | 15850 | go_error_at(location, "complex real part overflow"); |
71a45216 | 15851 | this->set_invalid(); |
15852 | } | |
d0bcce51 | 15853 | ret = false; |
15854 | } | |
0c77715b | 15855 | |
fcbea5e4 | 15856 | if (!mpfr_nan_p(mpc_imagref(val)) |
15857 | && !mpfr_inf_p(mpc_imagref(val)) | |
15858 | && !mpfr_zero_p(mpc_imagref(val)) | |
15859 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 15860 | { |
15861 | if (issue_error) | |
71a45216 | 15862 | { |
631d5788 | 15863 | go_error_at(location, "complex imaginary part overflow"); |
71a45216 | 15864 | this->set_invalid(); |
15865 | } | |
d0bcce51 | 15866 | ret = false; |
15867 | } | |
0c77715b | 15868 | |
d0bcce51 | 15869 | if (ret) |
15870 | { | |
15871 | // Round the constant to the desired type. | |
fcbea5e4 | 15872 | mpc_t t; |
d0bcce51 | 15873 | switch (type->bits()) |
15874 | { | |
15875 | case 64: | |
fcbea5e4 | 15876 | mpc_init2(t, 24); |
d0bcce51 | 15877 | break; |
15878 | case 128: | |
fcbea5e4 | 15879 | mpc_init2(t, 53); |
d0bcce51 | 15880 | break; |
15881 | default: | |
15882 | go_unreachable(); | |
15883 | } | |
fcbea5e4 | 15884 | mpc_set(t, val, MPC_RNDNN); |
15885 | mpc_set(val, t, MPC_RNDNN); | |
15886 | mpc_clear(t); | |
d0bcce51 | 15887 | |
fcbea5e4 | 15888 | this->set_complex(type, val); |
d0bcce51 | 15889 | } |
15890 | ||
fcbea5e4 | 15891 | mpc_clear(val); |
0c77715b | 15892 | |
15893 | return ret; | |
15894 | } | |
15895 | ||
15896 | // Return an Expression for this value. | |
15897 | ||
15898 | Expression* | |
15899 | Numeric_constant::expression(Location loc) const | |
15900 | { | |
15901 | switch (this->classification_) | |
15902 | { | |
15903 | case NC_INT: | |
e67508fa | 15904 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 15905 | case NC_RUNE: |
15906 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
15907 | case NC_FLOAT: | |
15908 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
15909 | case NC_COMPLEX: | |
fcbea5e4 | 15910 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 15911 | case NC_INVALID: |
15912 | go_assert(saw_errors()); | |
15913 | return Expression::make_error(loc); | |
0c77715b | 15914 | default: |
15915 | go_unreachable(); | |
15916 | } | |
15917 | } |