]>
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" |
438b4bec | 14 | #include "go-encode-id.h" |
e440a328 | 15 | #include "types.h" |
16 | #include "export.h" | |
17 | #include "import.h" | |
18 | #include "statements.h" | |
19 | #include "lex.h" | |
a9182619 | 20 | #include "runtime.h" |
6e193e6f | 21 | #include "backend.h" |
e440a328 | 22 | #include "expressions.h" |
d751bb78 | 23 | #include "ast-dump.h" |
e440a328 | 24 | |
25 | // Class Expression. | |
26 | ||
27 | Expression::Expression(Expression_classification classification, | |
b13c66cd | 28 | Location location) |
e440a328 | 29 | : classification_(classification), location_(location) |
30 | { | |
31 | } | |
32 | ||
33 | Expression::~Expression() | |
34 | { | |
35 | } | |
36 | ||
e440a328 | 37 | // Traverse the expressions. |
38 | ||
39 | int | |
40 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
41 | { | |
42 | Expression* expr = *pexpr; | |
43 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
44 | { | |
45 | int t = traverse->expression(pexpr); | |
46 | if (t == TRAVERSE_EXIT) | |
47 | return TRAVERSE_EXIT; | |
48 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
49 | return TRAVERSE_CONTINUE; | |
50 | } | |
51 | return expr->do_traverse(traverse); | |
52 | } | |
53 | ||
54 | // Traverse subexpressions of this expression. | |
55 | ||
56 | int | |
57 | Expression::traverse_subexpressions(Traverse* traverse) | |
58 | { | |
59 | return this->do_traverse(traverse); | |
60 | } | |
61 | ||
62 | // Default implementation for do_traverse for child classes. | |
63 | ||
64 | int | |
65 | Expression::do_traverse(Traverse*) | |
66 | { | |
67 | return TRAVERSE_CONTINUE; | |
68 | } | |
69 | ||
70 | // This virtual function is called by the parser if the value of this | |
a7549a6a | 71 | // expression is being discarded. By default, we give an error. |
72 | // Expressions with side effects override. | |
e440a328 | 73 | |
4f2138d7 | 74 | bool |
e440a328 | 75 | Expression::do_discarding_value() |
76 | { | |
a7549a6a | 77 | this->unused_value_error(); |
4f2138d7 | 78 | return false; |
e440a328 | 79 | } |
80 | ||
81 | // This virtual function is called to export expressions. This will | |
82 | // only be used by expressions which may be constant. | |
83 | ||
84 | void | |
85 | Expression::do_export(Export*) const | |
86 | { | |
c3e6f413 | 87 | go_unreachable(); |
e440a328 | 88 | } |
89 | ||
a7549a6a | 90 | // Give an error saying that the value of the expression is not used. |
e440a328 | 91 | |
92 | void | |
a7549a6a | 93 | Expression::unused_value_error() |
e440a328 | 94 | { |
4f2138d7 | 95 | this->report_error(_("value computed is not used")); |
e440a328 | 96 | } |
97 | ||
98 | // Note that this expression is an error. This is called by children | |
99 | // when they discover an error. | |
100 | ||
101 | void | |
102 | Expression::set_is_error() | |
103 | { | |
104 | this->classification_ = EXPRESSION_ERROR; | |
105 | } | |
106 | ||
107 | // For children to call to report an error conveniently. | |
108 | ||
109 | void | |
110 | Expression::report_error(const char* msg) | |
111 | { | |
631d5788 | 112 | go_error_at(this->location_, "%s", msg); |
e440a328 | 113 | this->set_is_error(); |
114 | } | |
115 | ||
116 | // Set types of variables and constants. This is implemented by the | |
117 | // child class. | |
118 | ||
119 | void | |
120 | Expression::determine_type(const Type_context* context) | |
121 | { | |
122 | this->do_determine_type(context); | |
123 | } | |
124 | ||
125 | // Set types when there is no context. | |
126 | ||
127 | void | |
128 | Expression::determine_type_no_context() | |
129 | { | |
130 | Type_context context; | |
131 | this->do_determine_type(&context); | |
132 | } | |
133 | ||
2c809f8f | 134 | // Return an expression handling any conversions which must be done during |
e440a328 | 135 | // assignment. |
136 | ||
2c809f8f | 137 | Expression* |
b4a33049 | 138 | Expression::convert_for_assignment(Gogo*, Type* lhs_type, |
2c809f8f | 139 | Expression* rhs, Location location) |
e440a328 | 140 | { |
2c809f8f | 141 | Type* rhs_type = rhs->type(); |
142 | if (lhs_type->is_error() | |
143 | || rhs_type->is_error() | |
144 | || rhs->is_error_expression()) | |
145 | return Expression::make_error(location); | |
e440a328 | 146 | |
54211955 | 147 | if (lhs_type->forwarded() != rhs_type->forwarded() |
148 | && lhs_type->interface_type() != NULL) | |
e440a328 | 149 | { |
150 | if (rhs_type->interface_type() == NULL) | |
2c809f8f | 151 | return Expression::convert_type_to_interface(lhs_type, rhs, location); |
e440a328 | 152 | else |
2c809f8f | 153 | return Expression::convert_interface_to_interface(lhs_type, rhs, false, |
154 | location); | |
e440a328 | 155 | } |
54211955 | 156 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
157 | && rhs_type->interface_type() != NULL) | |
2c809f8f | 158 | return Expression::convert_interface_to_type(lhs_type, rhs, location); |
411eb89e | 159 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 160 | { |
2c809f8f | 161 | // Assigning nil to a slice. |
2c809f8f | 162 | Expression* nil = Expression::make_nil(location); |
e67508fa | 163 | Expression* zero = Expression::make_integer_ul(0, NULL, location); |
2c809f8f | 164 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); |
e440a328 | 165 | } |
166 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 167 | return Expression::make_nil(location); |
168 | else if (Type::are_identical(lhs_type, rhs_type, false, NULL)) | |
e440a328 | 169 | { |
170 | // No conversion is needed. | |
2c809f8f | 171 | return rhs; |
172 | } | |
173 | else if (lhs_type->points_to() != NULL) | |
174 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
175 | else if (lhs_type->is_numeric_type()) | |
176 | return Expression::make_cast(lhs_type, rhs, location); | |
177 | else if ((lhs_type->struct_type() != NULL | |
178 | && rhs_type->struct_type() != NULL) | |
179 | || (lhs_type->array_type() != NULL | |
180 | && rhs_type->array_type() != NULL)) | |
e440a328 | 181 | { |
182 | // This conversion must be permitted by Go, or we wouldn't have | |
183 | // gotten here. | |
2c809f8f | 184 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 185 | } |
186 | else | |
2c809f8f | 187 | return rhs; |
e440a328 | 188 | } |
189 | ||
2c809f8f | 190 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 191 | // interface type. |
192 | ||
2c809f8f | 193 | Expression* |
194 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
195 | Location location) | |
e440a328 | 196 | { |
e440a328 | 197 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
198 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
199 | ||
200 | // Since RHS_TYPE is a static type, we can create the interface | |
201 | // method table at compile time. | |
202 | ||
203 | // When setting an interface to nil, we just set both fields to | |
204 | // NULL. | |
2c809f8f | 205 | Type* rhs_type = rhs->type(); |
e440a328 | 206 | if (rhs_type->is_nil_type()) |
63697958 | 207 | { |
2c809f8f | 208 | Expression* nil = Expression::make_nil(location); |
209 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 210 | } |
e440a328 | 211 | |
212 | // This should have been checked already. | |
c484d925 | 213 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 214 | |
e440a328 | 215 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
216 | // then the first field is the type descriptor for RHS_TYPE. | |
217 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 218 | Expression* first_field; |
e440a328 | 219 | if (lhs_is_empty) |
2c809f8f | 220 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 221 | else |
222 | { | |
223 | // Build the interface method table for this interface and this | |
224 | // object type: a list of function pointers for each interface | |
225 | // method. | |
226 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 227 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 228 | bool is_pointer = false; |
c0cab2ec | 229 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 230 | { |
231 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 232 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 233 | is_pointer = true; |
234 | } | |
c0cab2ec | 235 | if (rhs_named_type != NULL) |
2c809f8f | 236 | first_field = |
237 | rhs_named_type->interface_method_table(lhs_interface_type, | |
238 | is_pointer); | |
c0cab2ec | 239 | else if (rhs_struct_type != NULL) |
2c809f8f | 240 | first_field = |
241 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
242 | is_pointer); | |
c0cab2ec | 243 | else |
2c809f8f | 244 | first_field = Expression::make_nil(location); |
e440a328 | 245 | } |
e440a328 | 246 | |
2c809f8f | 247 | Expression* obj; |
e440a328 | 248 | if (rhs_type->points_to() != NULL) |
249 | { | |
2c809f8f | 250 | // We are assigning a pointer to the interface; the interface |
e440a328 | 251 | // holds the pointer itself. |
2c809f8f | 252 | obj = rhs; |
253 | } | |
254 | else | |
255 | { | |
256 | // We are assigning a non-pointer value to the interface; the | |
45ff893b | 257 | // interface gets a copy of the value in the heap if it escapes. |
258 | // TODO(cmang): Associate escape state state of RHS with newly | |
259 | // created OBJ. | |
2c809f8f | 260 | obj = Expression::make_heap_expression(rhs, location); |
e440a328 | 261 | } |
262 | ||
2c809f8f | 263 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
264 | } | |
e440a328 | 265 | |
2c809f8f | 266 | // Return an expression for the type descriptor of RHS. |
e440a328 | 267 | |
2c809f8f | 268 | Expression* |
269 | Expression::get_interface_type_descriptor(Expression* rhs) | |
270 | { | |
271 | go_assert(rhs->type()->interface_type() != NULL); | |
272 | Location location = rhs->location(); | |
e440a328 | 273 | |
2c809f8f | 274 | // The type descriptor is the first field of an empty interface. |
275 | if (rhs->type()->interface_type()->is_empty()) | |
276 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
277 | location); | |
278 | ||
279 | Expression* mtable = | |
280 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 281 | |
2c809f8f | 282 | Expression* descriptor = |
283 | Expression::make_unary(OPERATOR_MULT, mtable, location); | |
284 | descriptor = Expression::make_field_reference(descriptor, 0, location); | |
285 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 286 | |
2c809f8f | 287 | Expression* eq = |
288 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
289 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 290 | } |
291 | ||
2c809f8f | 292 | // Return an expression for the conversion of an interface type to an |
e440a328 | 293 | // interface type. |
294 | ||
2c809f8f | 295 | Expression* |
296 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
297 | bool for_type_guard, | |
298 | Location location) | |
e440a328 | 299 | { |
8ba8cc87 | 300 | if (Type::are_identical(lhs_type, rhs->type(), false, NULL)) |
301 | return rhs; | |
302 | ||
e440a328 | 303 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
304 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
305 | ||
e440a328 | 306 | // In the general case this requires runtime examination of the type |
307 | // method table to match it up with the interface methods. | |
308 | ||
309 | // FIXME: If all of the methods in the right hand side interface | |
310 | // also appear in the left hand side interface, then we don't need | |
311 | // to do a runtime check, although we still need to build a new | |
312 | // method table. | |
313 | ||
8ba8cc87 | 314 | // We are going to evaluate RHS multiple times. |
315 | go_assert(rhs->is_variable()); | |
316 | ||
e440a328 | 317 | // Get the type descriptor for the right hand side. This will be |
318 | // NULL for a nil interface. | |
2c809f8f | 319 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
320 | Expression* lhs_type_expr = | |
321 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 322 | |
2c809f8f | 323 | Expression* first_field; |
e440a328 | 324 | if (for_type_guard) |
325 | { | |
326 | // A type assertion fails when converting a nil interface. | |
6098d6cb | 327 | first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2, |
328 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 329 | } |
330 | else if (lhs_is_empty) | |
331 | { | |
2c809f8f | 332 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 333 | // first field is just the type descriptor of the object. |
2c809f8f | 334 | first_field = rhs_type_expr; |
e440a328 | 335 | } |
336 | else | |
337 | { | |
338 | // A conversion to a non-empty interface may fail, but unlike a | |
339 | // type assertion converting nil will always succeed. | |
6098d6cb | 340 | first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2, |
341 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 342 | } |
343 | ||
344 | // The second field is simply the object pointer. | |
2c809f8f | 345 | Expression* obj = |
346 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
347 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 348 | } |
349 | ||
2c809f8f | 350 | // Return an expression for the conversion of an interface type to a |
e440a328 | 351 | // non-interface type. |
352 | ||
2c809f8f | 353 | Expression* |
354 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
355 | Location location) | |
e440a328 | 356 | { |
8ba8cc87 | 357 | // We are going to evaluate RHS multiple times. |
358 | go_assert(rhs->is_variable()); | |
359 | ||
e440a328 | 360 | // Call a function to check that the type is valid. The function |
361 | // will panic with an appropriate runtime type error if the type is | |
362 | // not valid. | |
2c809f8f | 363 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
364 | location); | |
365 | Expression* rhs_descriptor = | |
366 | Expression::get_interface_type_descriptor(rhs); | |
367 | ||
368 | Type* rhs_type = rhs->type(); | |
369 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
370 | location); | |
371 | ||
6098d6cb | 372 | Expression* check_iface = Runtime::make_call(Runtime::ASSERTI2T, |
2c809f8f | 373 | location, 3, lhs_type_expr, |
374 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 375 | |
376 | // If the call succeeds, pull out the value. | |
2c809f8f | 377 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
378 | location); | |
e440a328 | 379 | |
380 | // If the value is a pointer, then it is the value we want. | |
381 | // Otherwise it points to the value. | |
382 | if (lhs_type->points_to() == NULL) | |
383 | { | |
2c809f8f | 384 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
385 | location); | |
386 | obj = Expression::make_unary(OPERATOR_MULT, obj, location); | |
e440a328 | 387 | } |
2c809f8f | 388 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 389 | } |
390 | ||
ea664253 | 391 | // Convert an expression to its backend representation. This is implemented by |
392 | // the child class. Not that it is not in general safe to call this multiple | |
e440a328 | 393 | // times for a single expression, but that we don't catch such errors. |
394 | ||
ea664253 | 395 | Bexpression* |
396 | Expression::get_backend(Translate_context* context) | |
e440a328 | 397 | { |
398 | // The child may have marked this expression as having an error. | |
399 | if (this->classification_ == EXPRESSION_ERROR) | |
ea664253 | 400 | return context->backend()->error_expression(); |
e440a328 | 401 | |
ea664253 | 402 | return this->do_get_backend(context); |
e440a328 | 403 | } |
404 | ||
48c2a53a | 405 | // Return a backend expression for VAL. |
406 | Bexpression* | |
407 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
408 | Numeric_constant* val) | |
e440a328 | 409 | { |
48c2a53a | 410 | Gogo* gogo = context->gogo(); |
411 | Type* type = val->type(); | |
412 | if (type == NULL) | |
413 | return gogo->backend()->error_expression(); | |
e440a328 | 414 | |
48c2a53a | 415 | Btype* btype = type->get_backend(gogo); |
416 | Bexpression* ret; | |
417 | if (type->integer_type() != NULL) | |
e440a328 | 418 | { |
419 | mpz_t ival; | |
48c2a53a | 420 | if (!val->to_int(&ival)) |
421 | { | |
422 | go_assert(saw_errors()); | |
423 | return gogo->backend()->error_expression(); | |
424 | } | |
425 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 426 | mpz_clear(ival); |
e440a328 | 427 | } |
48c2a53a | 428 | else if (type->float_type() != NULL) |
e440a328 | 429 | { |
48c2a53a | 430 | mpfr_t fval; |
431 | if (!val->to_float(&fval)) | |
432 | { | |
433 | go_assert(saw_errors()); | |
434 | return gogo->backend()->error_expression(); | |
435 | } | |
436 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
437 | mpfr_clear(fval); | |
e440a328 | 438 | } |
48c2a53a | 439 | else if (type->complex_type() != NULL) |
e440a328 | 440 | { |
fcbea5e4 | 441 | mpc_t cval; |
442 | if (!val->to_complex(&cval)) | |
48c2a53a | 443 | { |
444 | go_assert(saw_errors()); | |
445 | return gogo->backend()->error_expression(); | |
446 | } | |
fcbea5e4 | 447 | ret = gogo->backend()->complex_constant_expression(btype, cval); |
448 | mpc_clear(cval); | |
e440a328 | 449 | } |
450 | else | |
c3e6f413 | 451 | go_unreachable(); |
e440a328 | 452 | |
48c2a53a | 453 | return ret; |
e440a328 | 454 | } |
455 | ||
2c809f8f | 456 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
457 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 458 | |
2c809f8f | 459 | Expression* |
460 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 461 | { |
2c809f8f | 462 | Type* val_type = val->type(); |
463 | Type* bound_type = Type::lookup_integer_type("int"); | |
464 | ||
465 | int val_type_size; | |
466 | bool val_is_unsigned = false; | |
467 | if (val_type->integer_type() != NULL) | |
468 | { | |
469 | val_type_size = val_type->integer_type()->bits(); | |
470 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
471 | } | |
472 | else | |
473 | { | |
474 | if (!val_type->is_numeric_type() | |
475 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
476 | { | |
477 | go_assert(saw_errors()); | |
478 | return Expression::make_boolean(true, loc); | |
479 | } | |
e440a328 | 480 | |
2c809f8f | 481 | if (val_type->complex_type() != NULL) |
482 | val_type_size = val_type->complex_type()->bits(); | |
483 | else | |
484 | val_type_size = val_type->float_type()->bits(); | |
485 | } | |
486 | ||
487 | Expression* negative_index = Expression::make_boolean(false, loc); | |
488 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
489 | if (!val_is_unsigned) | |
e440a328 | 490 | { |
e67508fa | 491 | Expression* zero = Expression::make_integer_ul(0, val_type, loc); |
2c809f8f | 492 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); |
e440a328 | 493 | } |
494 | ||
2c809f8f | 495 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 496 | if (val_type_size > bound_type_size |
497 | || (val_type_size == bound_type_size | |
2c809f8f | 498 | && val_is_unsigned)) |
499 | { | |
500 | mpz_t one; | |
501 | mpz_init_set_ui(one, 1UL); | |
502 | ||
503 | // maxval = 2^(bound_type_size - 1) - 1 | |
504 | mpz_t maxval; | |
505 | mpz_init(maxval); | |
506 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
507 | mpz_sub_ui(maxval, maxval, 1); | |
e67508fa | 508 | Expression* max = Expression::make_integer_z(&maxval, val_type, loc); |
2c809f8f | 509 | mpz_clear(one); |
510 | mpz_clear(maxval); | |
511 | ||
512 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 513 | } |
514 | ||
2c809f8f | 515 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
516 | loc); | |
e440a328 | 517 | } |
518 | ||
d751bb78 | 519 | void |
520 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
521 | { | |
522 | this->do_dump_expression(ast_dump_context); | |
523 | } | |
524 | ||
e440a328 | 525 | // Error expressions. This are used to avoid cascading errors. |
526 | ||
527 | class Error_expression : public Expression | |
528 | { | |
529 | public: | |
b13c66cd | 530 | Error_expression(Location location) |
e440a328 | 531 | : Expression(EXPRESSION_ERROR, location) |
532 | { } | |
533 | ||
534 | protected: | |
535 | bool | |
536 | do_is_constant() const | |
537 | { return true; } | |
538 | ||
539 | bool | |
0c77715b | 540 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 541 | { |
0c77715b | 542 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 543 | return true; |
544 | } | |
545 | ||
4f2138d7 | 546 | bool |
e440a328 | 547 | do_discarding_value() |
4f2138d7 | 548 | { return true; } |
e440a328 | 549 | |
550 | Type* | |
551 | do_type() | |
552 | { return Type::make_error_type(); } | |
553 | ||
554 | void | |
555 | do_determine_type(const Type_context*) | |
556 | { } | |
557 | ||
558 | Expression* | |
559 | do_copy() | |
560 | { return this; } | |
561 | ||
562 | bool | |
563 | do_is_addressable() const | |
564 | { return true; } | |
565 | ||
ea664253 | 566 | Bexpression* |
567 | do_get_backend(Translate_context* context) | |
568 | { return context->backend()->error_expression(); } | |
d751bb78 | 569 | |
570 | void | |
571 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 572 | }; |
573 | ||
d751bb78 | 574 | // Dump the ast representation for an error expression to a dump context. |
575 | ||
576 | void | |
577 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
578 | { | |
579 | ast_dump_context->ostream() << "_Error_" ; | |
580 | } | |
581 | ||
e440a328 | 582 | Expression* |
b13c66cd | 583 | Expression::make_error(Location location) |
e440a328 | 584 | { |
585 | return new Error_expression(location); | |
586 | } | |
587 | ||
588 | // An expression which is really a type. This is used during parsing. | |
589 | // It is an error if these survive after lowering. | |
590 | ||
591 | class | |
592 | Type_expression : public Expression | |
593 | { | |
594 | public: | |
b13c66cd | 595 | Type_expression(Type* type, Location location) |
e440a328 | 596 | : Expression(EXPRESSION_TYPE, location), |
597 | type_(type) | |
598 | { } | |
599 | ||
600 | protected: | |
601 | int | |
602 | do_traverse(Traverse* traverse) | |
603 | { return Type::traverse(this->type_, traverse); } | |
604 | ||
605 | Type* | |
606 | do_type() | |
607 | { return this->type_; } | |
608 | ||
609 | void | |
610 | do_determine_type(const Type_context*) | |
611 | { } | |
612 | ||
613 | void | |
614 | do_check_types(Gogo*) | |
615 | { this->report_error(_("invalid use of type")); } | |
616 | ||
617 | Expression* | |
618 | do_copy() | |
619 | { return this; } | |
620 | ||
ea664253 | 621 | Bexpression* |
622 | do_get_backend(Translate_context*) | |
c3e6f413 | 623 | { go_unreachable(); } |
e440a328 | 624 | |
d751bb78 | 625 | void do_dump_expression(Ast_dump_context*) const; |
626 | ||
e440a328 | 627 | private: |
628 | // The type which we are representing as an expression. | |
629 | Type* type_; | |
630 | }; | |
631 | ||
d751bb78 | 632 | void |
633 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
634 | { | |
635 | ast_dump_context->dump_type(this->type_); | |
636 | } | |
637 | ||
e440a328 | 638 | Expression* |
b13c66cd | 639 | Expression::make_type(Type* type, Location location) |
e440a328 | 640 | { |
641 | return new Type_expression(type, location); | |
642 | } | |
643 | ||
e03bdf36 | 644 | // Class Parser_expression. |
645 | ||
646 | Type* | |
647 | Parser_expression::do_type() | |
648 | { | |
649 | // We should never really ask for the type of a Parser_expression. | |
650 | // However, it can happen, at least when we have an invalid const | |
651 | // whose initializer refers to the const itself. In that case we | |
652 | // may ask for the type when lowering the const itself. | |
c484d925 | 653 | go_assert(saw_errors()); |
e03bdf36 | 654 | return Type::make_error_type(); |
655 | } | |
656 | ||
e440a328 | 657 | // Class Var_expression. |
658 | ||
659 | // Lower a variable expression. Here we just make sure that the | |
660 | // initialization expression of the variable has been lowered. This | |
661 | // ensures that we will be able to determine the type of the variable | |
662 | // if necessary. | |
663 | ||
664 | Expression* | |
ceeb4318 | 665 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
666 | Statement_inserter* inserter, int) | |
e440a328 | 667 | { |
668 | if (this->variable_->is_variable()) | |
669 | { | |
670 | Variable* var = this->variable_->var_value(); | |
671 | // This is either a local variable or a global variable. A | |
672 | // reference to a variable which is local to an enclosing | |
673 | // function will be a reference to a field in a closure. | |
674 | if (var->is_global()) | |
ceeb4318 | 675 | { |
676 | function = NULL; | |
677 | inserter = NULL; | |
678 | } | |
679 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 680 | } |
681 | return this; | |
682 | } | |
683 | ||
e440a328 | 684 | // Return the type of a reference to a variable. |
685 | ||
686 | Type* | |
687 | Var_expression::do_type() | |
688 | { | |
689 | if (this->variable_->is_variable()) | |
690 | return this->variable_->var_value()->type(); | |
691 | else if (this->variable_->is_result_variable()) | |
692 | return this->variable_->result_var_value()->type(); | |
693 | else | |
c3e6f413 | 694 | go_unreachable(); |
e440a328 | 695 | } |
696 | ||
0ab09e06 | 697 | // Determine the type of a reference to a variable. |
698 | ||
699 | void | |
700 | Var_expression::do_determine_type(const Type_context*) | |
701 | { | |
702 | if (this->variable_->is_variable()) | |
703 | this->variable_->var_value()->determine_type(); | |
704 | } | |
705 | ||
e440a328 | 706 | // Something takes the address of this variable. This means that we |
707 | // may want to move the variable onto the heap. | |
708 | ||
709 | void | |
710 | Var_expression::do_address_taken(bool escapes) | |
711 | { | |
712 | if (!escapes) | |
f325319b | 713 | { |
714 | if (this->variable_->is_variable()) | |
715 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
716 | else if (this->variable_->is_result_variable()) | |
717 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
718 | else | |
719 | go_unreachable(); | |
720 | } | |
e440a328 | 721 | else |
f325319b | 722 | { |
723 | if (this->variable_->is_variable()) | |
724 | this->variable_->var_value()->set_address_taken(); | |
725 | else if (this->variable_->is_result_variable()) | |
726 | this->variable_->result_var_value()->set_address_taken(); | |
727 | else | |
728 | go_unreachable(); | |
729 | } | |
45ff893b | 730 | |
731 | if (this->variable_->is_variable() | |
732 | && this->variable_->var_value()->is_in_heap()) | |
733 | { | |
734 | Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP); | |
735 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
736 | } | |
e440a328 | 737 | } |
738 | ||
ea664253 | 739 | // Get the backend representation for a reference to a variable. |
e440a328 | 740 | |
ea664253 | 741 | Bexpression* |
742 | Var_expression::do_get_backend(Translate_context* context) | |
e440a328 | 743 | { |
fe2f84cf | 744 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
745 | context->function()); | |
fe2f84cf | 746 | bool is_in_heap; |
c6777780 | 747 | Location loc = this->location(); |
9b27b43c | 748 | Btype* btype; |
749 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 750 | if (this->variable_->is_variable()) |
9b27b43c | 751 | { |
752 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
753 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
754 | } | |
fe2f84cf | 755 | else if (this->variable_->is_result_variable()) |
9b27b43c | 756 | { |
757 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
758 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
759 | } | |
fe2f84cf | 760 | else |
c3e6f413 | 761 | go_unreachable(); |
c6777780 | 762 | |
d4e6573e | 763 | Bexpression* ret = |
764 | context->backend()->var_expression(bvar, this->in_lvalue_pos_, loc); | |
fe2f84cf | 765 | if (is_in_heap) |
9b27b43c | 766 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
ea664253 | 767 | return ret; |
e440a328 | 768 | } |
769 | ||
d751bb78 | 770 | // Ast dump for variable expression. |
771 | ||
772 | void | |
773 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
774 | { | |
775 | ast_dump_context->ostream() << this->variable_->name() ; | |
776 | } | |
777 | ||
e440a328 | 778 | // Make a reference to a variable in an expression. |
779 | ||
780 | Expression* | |
b13c66cd | 781 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 782 | { |
783 | if (var->is_sink()) | |
784 | return Expression::make_sink(location); | |
785 | ||
786 | // FIXME: Creating a new object for each reference to a variable is | |
787 | // wasteful. | |
788 | return new Var_expression(var, location); | |
789 | } | |
790 | ||
b0c09712 | 791 | // Class Enclosed_var_expression. |
792 | ||
793 | int | |
794 | Enclosed_var_expression::do_traverse(Traverse*) | |
795 | { | |
796 | return TRAVERSE_CONTINUE; | |
797 | } | |
798 | ||
799 | // Lower the reference to the enclosed variable. | |
800 | ||
801 | Expression* | |
802 | Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function, | |
803 | Statement_inserter* inserter, int) | |
804 | { | |
805 | gogo->lower_expression(function, inserter, &this->reference_); | |
806 | return this; | |
807 | } | |
808 | ||
809 | // Flatten the reference to the enclosed variable. | |
810 | ||
811 | Expression* | |
812 | Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function, | |
813 | Statement_inserter* inserter) | |
814 | { | |
815 | gogo->flatten_expression(function, inserter, &this->reference_); | |
816 | return this; | |
817 | } | |
818 | ||
819 | void | |
820 | Enclosed_var_expression::do_address_taken(bool escapes) | |
821 | { | |
822 | if (!escapes) | |
823 | { | |
824 | if (this->variable_->is_variable()) | |
825 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
826 | else if (this->variable_->is_result_variable()) | |
827 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
828 | else | |
829 | go_unreachable(); | |
830 | } | |
831 | else | |
832 | { | |
833 | if (this->variable_->is_variable()) | |
834 | this->variable_->var_value()->set_address_taken(); | |
835 | else if (this->variable_->is_result_variable()) | |
836 | this->variable_->result_var_value()->set_address_taken(); | |
837 | else | |
838 | go_unreachable(); | |
839 | } | |
45ff893b | 840 | |
841 | if (this->variable_->is_variable() | |
842 | && this->variable_->var_value()->is_in_heap()) | |
843 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
b0c09712 | 844 | } |
845 | ||
846 | // Ast dump for enclosed variable expression. | |
847 | ||
848 | void | |
849 | Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const | |
850 | { | |
851 | adc->ostream() << this->variable_->name(); | |
852 | } | |
853 | ||
854 | // Make a reference to a variable within an enclosing function. | |
855 | ||
856 | Expression* | |
857 | Expression::make_enclosing_var_reference(Expression* reference, | |
858 | Named_object* var, Location location) | |
859 | { | |
860 | return new Enclosed_var_expression(reference, var, location); | |
861 | } | |
862 | ||
e440a328 | 863 | // Class Temporary_reference_expression. |
864 | ||
865 | // The type. | |
866 | ||
867 | Type* | |
868 | Temporary_reference_expression::do_type() | |
869 | { | |
870 | return this->statement_->type(); | |
871 | } | |
872 | ||
873 | // Called if something takes the address of this temporary variable. | |
874 | // We never have to move temporary variables to the heap, but we do | |
875 | // need to know that they must live in the stack rather than in a | |
876 | // register. | |
877 | ||
878 | void | |
879 | Temporary_reference_expression::do_address_taken(bool) | |
880 | { | |
881 | this->statement_->set_is_address_taken(); | |
882 | } | |
883 | ||
ea664253 | 884 | // Get a backend expression referring to the variable. |
e440a328 | 885 | |
ea664253 | 886 | Bexpression* |
887 | Temporary_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 888 | { |
cd440cff | 889 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 890 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
d4e6573e | 891 | Varexpr_context ve_ctxt = (this->is_lvalue_ ? VE_lvalue : VE_rvalue); |
892 | ||
893 | Bexpression* ret = gogo->backend()->var_expression(bvar, ve_ctxt, | |
894 | this->location()); | |
eefc1ed3 | 895 | |
cd440cff | 896 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 897 | // that the Go frontend can. In some cases this means that a |
898 | // temporary variable won't have the right backend type. Correct | |
899 | // that here by adding a type cast. We need to use base() to push | |
900 | // the circularity down one level. | |
cd440cff | 901 | Type* stype = this->statement_->type(); |
ceeb4318 | 902 | if (!this->is_lvalue_ |
cd440cff | 903 | && stype->has_pointer() |
904 | && stype->deref()->is_void_type()) | |
eefc1ed3 | 905 | { |
cd440cff | 906 | Btype* btype = this->type()->base()->get_backend(gogo); |
907 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 908 | } |
ea664253 | 909 | return ret; |
e440a328 | 910 | } |
911 | ||
d751bb78 | 912 | // Ast dump for temporary reference. |
913 | ||
914 | void | |
915 | Temporary_reference_expression::do_dump_expression( | |
916 | Ast_dump_context* ast_dump_context) const | |
917 | { | |
918 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
919 | } | |
920 | ||
e440a328 | 921 | // Make a reference to a temporary variable. |
922 | ||
ceeb4318 | 923 | Temporary_reference_expression* |
e440a328 | 924 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 925 | Location location) |
e440a328 | 926 | { |
927 | return new Temporary_reference_expression(statement, location); | |
928 | } | |
929 | ||
e9d3367e | 930 | // Class Set_and_use_temporary_expression. |
931 | ||
932 | // Return the type. | |
933 | ||
934 | Type* | |
935 | Set_and_use_temporary_expression::do_type() | |
936 | { | |
937 | return this->statement_->type(); | |
938 | } | |
939 | ||
0afbb937 | 940 | // Determine the type of the expression. |
941 | ||
942 | void | |
943 | Set_and_use_temporary_expression::do_determine_type( | |
944 | const Type_context* context) | |
945 | { | |
946 | this->expr_->determine_type(context); | |
947 | } | |
948 | ||
e9d3367e | 949 | // Take the address. |
950 | ||
951 | void | |
952 | Set_and_use_temporary_expression::do_address_taken(bool) | |
953 | { | |
954 | this->statement_->set_is_address_taken(); | |
955 | } | |
956 | ||
957 | // Return the backend representation. | |
958 | ||
ea664253 | 959 | Bexpression* |
960 | Set_and_use_temporary_expression::do_get_backend(Translate_context* context) | |
e9d3367e | 961 | { |
e9d3367e | 962 | Location loc = this->location(); |
a302c105 | 963 | Gogo* gogo = context->gogo(); |
964 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
d4e6573e | 965 | Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, VE_rvalue, loc); |
a302c105 | 966 | |
ea664253 | 967 | Bexpression* bexpr = this->expr_->get_backend(context); |
d4e6573e | 968 | Bstatement* set = gogo->backend()->assignment_statement(lvar_ref, bexpr, loc); |
969 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, VE_lvalue, loc); | |
a302c105 | 970 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); |
ea664253 | 971 | return ret; |
e9d3367e | 972 | } |
973 | ||
974 | // Dump. | |
975 | ||
976 | void | |
977 | Set_and_use_temporary_expression::do_dump_expression( | |
978 | Ast_dump_context* ast_dump_context) const | |
979 | { | |
980 | ast_dump_context->ostream() << '('; | |
981 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
982 | ast_dump_context->ostream() << " = "; | |
983 | this->expr_->dump_expression(ast_dump_context); | |
984 | ast_dump_context->ostream() << ')'; | |
985 | } | |
986 | ||
987 | // Make a set-and-use temporary. | |
988 | ||
989 | Set_and_use_temporary_expression* | |
990 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
991 | Expression* expr, Location location) | |
992 | { | |
993 | return new Set_and_use_temporary_expression(statement, expr, location); | |
994 | } | |
995 | ||
e440a328 | 996 | // A sink expression--a use of the blank identifier _. |
997 | ||
998 | class Sink_expression : public Expression | |
999 | { | |
1000 | public: | |
b13c66cd | 1001 | Sink_expression(Location location) |
e440a328 | 1002 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 1003 | type_(NULL), bvar_(NULL) |
e440a328 | 1004 | { } |
1005 | ||
1006 | protected: | |
4f2138d7 | 1007 | bool |
e440a328 | 1008 | do_discarding_value() |
4f2138d7 | 1009 | { return true; } |
e440a328 | 1010 | |
1011 | Type* | |
1012 | do_type(); | |
1013 | ||
1014 | void | |
1015 | do_determine_type(const Type_context*); | |
1016 | ||
1017 | Expression* | |
1018 | do_copy() | |
1019 | { return new Sink_expression(this->location()); } | |
1020 | ||
ea664253 | 1021 | Bexpression* |
1022 | do_get_backend(Translate_context*); | |
e440a328 | 1023 | |
d751bb78 | 1024 | void |
1025 | do_dump_expression(Ast_dump_context*) const; | |
1026 | ||
e440a328 | 1027 | private: |
1028 | // The type of this sink variable. | |
1029 | Type* type_; | |
1030 | // The temporary variable we generate. | |
aa93217a | 1031 | Bvariable* bvar_; |
e440a328 | 1032 | }; |
1033 | ||
1034 | // Return the type of a sink expression. | |
1035 | ||
1036 | Type* | |
1037 | Sink_expression::do_type() | |
1038 | { | |
1039 | if (this->type_ == NULL) | |
1040 | return Type::make_sink_type(); | |
1041 | return this->type_; | |
1042 | } | |
1043 | ||
1044 | // Determine the type of a sink expression. | |
1045 | ||
1046 | void | |
1047 | Sink_expression::do_determine_type(const Type_context* context) | |
1048 | { | |
1049 | if (context->type != NULL) | |
1050 | this->type_ = context->type; | |
1051 | } | |
1052 | ||
1053 | // Return a temporary variable for a sink expression. This will | |
1054 | // presumably be a write-only variable which the middle-end will drop. | |
1055 | ||
ea664253 | 1056 | Bexpression* |
1057 | Sink_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1058 | { |
aa93217a | 1059 | Location loc = this->location(); |
1060 | Gogo* gogo = context->gogo(); | |
1061 | if (this->bvar_ == NULL) | |
e440a328 | 1062 | { |
c484d925 | 1063 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 1064 | Named_object* fn = context->function(); |
1065 | go_assert(fn != NULL); | |
1066 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 1067 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 1068 | Bstatement* decl; |
1069 | this->bvar_ = | |
1070 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
1071 | false, loc, &decl); | |
d4e6573e | 1072 | Bexpression* var_ref = |
1073 | gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc); | |
aa93217a | 1074 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); |
ea664253 | 1075 | return var_ref; |
e440a328 | 1076 | } |
d4e6573e | 1077 | return gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc); |
e440a328 | 1078 | } |
1079 | ||
d751bb78 | 1080 | // Ast dump for sink expression. |
1081 | ||
1082 | void | |
1083 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1084 | { | |
1085 | ast_dump_context->ostream() << "_" ; | |
1086 | } | |
1087 | ||
e440a328 | 1088 | // Make a sink expression. |
1089 | ||
1090 | Expression* | |
b13c66cd | 1091 | Expression::make_sink(Location location) |
e440a328 | 1092 | { |
1093 | return new Sink_expression(location); | |
1094 | } | |
1095 | ||
1096 | // Class Func_expression. | |
1097 | ||
1098 | // FIXME: Can a function expression appear in a constant expression? | |
1099 | // The value is unchanging. Initializing a constant to the address of | |
1100 | // a function seems like it could work, though there might be little | |
1101 | // point to it. | |
1102 | ||
e440a328 | 1103 | // Traversal. |
1104 | ||
1105 | int | |
1106 | Func_expression::do_traverse(Traverse* traverse) | |
1107 | { | |
1108 | return (this->closure_ == NULL | |
1109 | ? TRAVERSE_CONTINUE | |
1110 | : Expression::traverse(&this->closure_, traverse)); | |
1111 | } | |
1112 | ||
1113 | // Return the type of a function expression. | |
1114 | ||
1115 | Type* | |
1116 | Func_expression::do_type() | |
1117 | { | |
1118 | if (this->function_->is_function()) | |
1119 | return this->function_->func_value()->type(); | |
1120 | else if (this->function_->is_function_declaration()) | |
1121 | return this->function_->func_declaration_value()->type(); | |
1122 | else | |
c3e6f413 | 1123 | go_unreachable(); |
e440a328 | 1124 | } |
1125 | ||
ea664253 | 1126 | // Get the backend representation for the code of a function expression. |
e440a328 | 1127 | |
97267c39 | 1128 | Bexpression* |
8381eda7 | 1129 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1130 | { |
1131 | Function_type* fntype; | |
8381eda7 | 1132 | if (no->is_function()) |
1133 | fntype = no->func_value()->type(); | |
1134 | else if (no->is_function_declaration()) | |
1135 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1136 | else |
c3e6f413 | 1137 | go_unreachable(); |
e440a328 | 1138 | |
1139 | // Builtin functions are handled specially by Call_expression. We | |
1140 | // can't take their address. | |
1141 | if (fntype->is_builtin()) | |
1142 | { | |
631d5788 | 1143 | go_error_at(loc, |
1144 | "invalid use of special builtin function %qs; must be called", | |
1145 | no->message_name().c_str()); | |
97267c39 | 1146 | return gogo->backend()->error_expression(); |
e440a328 | 1147 | } |
1148 | ||
97267c39 | 1149 | Bfunction* fndecl; |
e440a328 | 1150 | if (no->is_function()) |
cf3cae55 | 1151 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1152 | else if (no->is_function_declaration()) |
cf3cae55 | 1153 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1154 | else |
c3e6f413 | 1155 | go_unreachable(); |
e440a328 | 1156 | |
97267c39 | 1157 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1158 | } |
1159 | ||
ea664253 | 1160 | // Get the backend representation for a function expression. This is used when |
1161 | // we take the address of a function rather than simply calling it. A func | |
8381eda7 | 1162 | // value is represented as a pointer to a block of memory. The first |
1163 | // word of that memory is a pointer to the function code. The | |
1164 | // remaining parts of that memory are the addresses of variables that | |
1165 | // the function closes over. | |
e440a328 | 1166 | |
ea664253 | 1167 | Bexpression* |
1168 | Func_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1169 | { |
8381eda7 | 1170 | // If there is no closure, just use the function descriptor. |
2010c17a | 1171 | if (this->closure_ == NULL) |
8381eda7 | 1172 | { |
1173 | Gogo* gogo = context->gogo(); | |
1174 | Named_object* no = this->function_; | |
1175 | Expression* descriptor; | |
1176 | if (no->is_function()) | |
1177 | descriptor = no->func_value()->descriptor(gogo, no); | |
1178 | else if (no->is_function_declaration()) | |
1179 | { | |
1180 | if (no->func_declaration_value()->type()->is_builtin()) | |
1181 | { | |
631d5788 | 1182 | go_error_at(this->location(), |
1183 | ("invalid use of special builtin function %qs; " | |
1184 | "must be called"), | |
1185 | no->message_name().c_str()); | |
ea664253 | 1186 | return gogo->backend()->error_expression(); |
8381eda7 | 1187 | } |
1188 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1189 | } | |
1190 | else | |
1191 | go_unreachable(); | |
2010c17a | 1192 | |
ea664253 | 1193 | Bexpression* bdesc = descriptor->get_backend(context); |
1194 | return gogo->backend()->address_expression(bdesc, this->location()); | |
8381eda7 | 1195 | } |
e440a328 | 1196 | |
8381eda7 | 1197 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1198 | |
8381eda7 | 1199 | // If there is a closure, then the closure is itself the function |
1200 | // expression. It is a pointer to a struct whose first field points | |
1201 | // to the function code and whose remaining fields are the addresses | |
1202 | // of the closed-over variables. | |
ea664253 | 1203 | return this->closure_->get_backend(context); |
e440a328 | 1204 | } |
1205 | ||
d751bb78 | 1206 | // Ast dump for function. |
1207 | ||
1208 | void | |
1209 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1210 | { | |
8b1c301d | 1211 | ast_dump_context->ostream() << this->function_->name(); |
1212 | if (this->closure_ != NULL) | |
1213 | { | |
1214 | ast_dump_context->ostream() << " {closure = "; | |
1215 | this->closure_->dump_expression(ast_dump_context); | |
1216 | ast_dump_context->ostream() << "}"; | |
1217 | } | |
d751bb78 | 1218 | } |
1219 | ||
e440a328 | 1220 | // Make a reference to a function in an expression. |
1221 | ||
1222 | Expression* | |
1223 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1224 | Location location) |
e440a328 | 1225 | { |
b1d7ecfa | 1226 | Func_expression* fe = new Func_expression(function, closure, location); |
1227 | ||
1228 | // Detect references to builtin functions and set the runtime code if | |
1229 | // appropriate. | |
1230 | if (function->is_function_declaration()) | |
1231 | fe->set_runtime_code(Runtime::name_to_code(function->name())); | |
1232 | return fe; | |
e440a328 | 1233 | } |
1234 | ||
c6837989 | 1235 | // Class Func_descriptor_expression. |
8381eda7 | 1236 | |
c6837989 | 1237 | // Constructor. |
8381eda7 | 1238 | |
c6837989 | 1239 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1240 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1241 | fn_(fn), dvar_(NULL) |
c6837989 | 1242 | { |
1243 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1244 | } | |
8381eda7 | 1245 | |
c6837989 | 1246 | // Traversal. |
8381eda7 | 1247 | |
c6837989 | 1248 | int |
1249 | Func_descriptor_expression::do_traverse(Traverse*) | |
1250 | { | |
1251 | return TRAVERSE_CONTINUE; | |
1252 | } | |
8381eda7 | 1253 | |
1254 | // All function descriptors have the same type. | |
1255 | ||
1256 | Type* Func_descriptor_expression::descriptor_type; | |
1257 | ||
1258 | void | |
1259 | Func_descriptor_expression::make_func_descriptor_type() | |
1260 | { | |
1261 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1262 | return; | |
1263 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1264 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1265 | Func_descriptor_expression::descriptor_type = | |
1266 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1267 | } | |
1268 | ||
1269 | Type* | |
1270 | Func_descriptor_expression::do_type() | |
1271 | { | |
1272 | Func_descriptor_expression::make_func_descriptor_type(); | |
1273 | return Func_descriptor_expression::descriptor_type; | |
1274 | } | |
1275 | ||
ea664253 | 1276 | // The backend representation for a function descriptor. |
8381eda7 | 1277 | |
ea664253 | 1278 | Bexpression* |
1279 | Func_descriptor_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1280 | { |
8381eda7 | 1281 | Named_object* no = this->fn_; |
1282 | Location loc = no->location(); | |
ea664253 | 1283 | if (this->dvar_ != NULL) |
d4e6573e | 1284 | return context->backend()->var_expression(this->dvar_, VE_rvalue, loc); |
8381eda7 | 1285 | |
ea664253 | 1286 | Gogo* gogo = context->gogo(); |
8381eda7 | 1287 | std::string var_name; |
09e57698 | 1288 | bool is_descriptor = false; |
1289 | if (no->is_function_declaration() | |
1290 | && !no->func_declaration_value()->asm_name().empty() | |
1291 | && Linemap::is_predeclared_location(no->location())) | |
1292 | { | |
6098d6cb | 1293 | if (no->func_declaration_value()->asm_name().substr(0, 8) != "runtime.") |
1294 | var_name = no->func_declaration_value()->asm_name() + "_descriptor"; | |
1295 | else | |
1296 | var_name = no->func_declaration_value()->asm_name() + "$descriptor"; | |
09e57698 | 1297 | is_descriptor = true; |
1298 | } | |
8381eda7 | 1299 | else |
09e57698 | 1300 | { |
1301 | if (no->package() == NULL) | |
1302 | var_name = gogo->pkgpath_symbol(); | |
1303 | else | |
1304 | var_name = no->package()->pkgpath_symbol(); | |
1305 | var_name.push_back('.'); | |
1306 | var_name.append(Gogo::unpack_hidden_name(no->name())); | |
1307 | var_name.append("$descriptor"); | |
1308 | } | |
8381eda7 | 1309 | |
1310 | Btype* btype = this->type()->get_backend(gogo); | |
1311 | ||
1312 | Bvariable* bvar; | |
438b4bec | 1313 | std::string asm_name(go_selectively_encode_id(var_name)); |
09e57698 | 1314 | if (no->package() != NULL || is_descriptor) |
438b4bec | 1315 | bvar = context->backend()->immutable_struct_reference(var_name, asm_name, |
1316 | btype, loc); | |
8381eda7 | 1317 | else |
1318 | { | |
1319 | Location bloc = Linemap::predeclared_location(); | |
1320 | bool is_hidden = ((no->is_function() | |
1321 | && no->func_value()->enclosing() != NULL) | |
1322 | || Gogo::is_thunk(no)); | |
438b4bec | 1323 | bvar = context->backend()->immutable_struct(var_name, asm_name, |
1324 | is_hidden, false, | |
8381eda7 | 1325 | btype, bloc); |
1326 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1327 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1328 | Expression* init = |
1329 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1330 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1331 | bcontext.set_is_const(); | |
ea664253 | 1332 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1333 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1334 | false, btype, bloc, binit); | |
1335 | } | |
1336 | ||
1337 | this->dvar_ = bvar; | |
d4e6573e | 1338 | return gogo->backend()->var_expression(bvar, VE_rvalue, loc); |
8381eda7 | 1339 | } |
1340 | ||
c6837989 | 1341 | // Print a function descriptor expression. |
1342 | ||
1343 | void | |
1344 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1345 | { | |
1346 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1347 | } | |
1348 | ||
8381eda7 | 1349 | // Make a function descriptor expression. |
1350 | ||
c6837989 | 1351 | Func_descriptor_expression* |
1352 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1353 | { |
c6837989 | 1354 | return new Func_descriptor_expression(fn); |
8381eda7 | 1355 | } |
1356 | ||
1357 | // Make the function descriptor type, so that it can be converted. | |
1358 | ||
1359 | void | |
1360 | Expression::make_func_descriptor_type() | |
1361 | { | |
1362 | Func_descriptor_expression::make_func_descriptor_type(); | |
1363 | } | |
1364 | ||
1365 | // A reference to just the code of a function. | |
1366 | ||
1367 | class Func_code_reference_expression : public Expression | |
1368 | { | |
1369 | public: | |
1370 | Func_code_reference_expression(Named_object* function, Location location) | |
1371 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1372 | function_(function) | |
1373 | { } | |
1374 | ||
1375 | protected: | |
1376 | int | |
1377 | do_traverse(Traverse*) | |
1378 | { return TRAVERSE_CONTINUE; } | |
1379 | ||
f9ca30f9 | 1380 | bool |
3ae06f68 | 1381 | do_is_static_initializer() const |
f9ca30f9 | 1382 | { return true; } |
1383 | ||
8381eda7 | 1384 | Type* |
1385 | do_type() | |
1386 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1387 | ||
1388 | void | |
1389 | do_determine_type(const Type_context*) | |
1390 | { } | |
1391 | ||
1392 | Expression* | |
1393 | do_copy() | |
1394 | { | |
1395 | return Expression::make_func_code_reference(this->function_, | |
1396 | this->location()); | |
1397 | } | |
1398 | ||
ea664253 | 1399 | Bexpression* |
1400 | do_get_backend(Translate_context*); | |
8381eda7 | 1401 | |
1402 | void | |
1403 | do_dump_expression(Ast_dump_context* context) const | |
1404 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1405 | ||
1406 | private: | |
1407 | // The function. | |
1408 | Named_object* function_; | |
1409 | }; | |
1410 | ||
ea664253 | 1411 | // Get the backend representation for a reference to function code. |
8381eda7 | 1412 | |
ea664253 | 1413 | Bexpression* |
1414 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1415 | { |
ea664253 | 1416 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1417 | this->location()); | |
8381eda7 | 1418 | } |
1419 | ||
1420 | // Make a reference to the code of a function. | |
1421 | ||
1422 | Expression* | |
1423 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1424 | { | |
1425 | return new Func_code_reference_expression(function, location); | |
1426 | } | |
1427 | ||
e440a328 | 1428 | // Class Unknown_expression. |
1429 | ||
1430 | // Return the name of an unknown expression. | |
1431 | ||
1432 | const std::string& | |
1433 | Unknown_expression::name() const | |
1434 | { | |
1435 | return this->named_object_->name(); | |
1436 | } | |
1437 | ||
1438 | // Lower a reference to an unknown name. | |
1439 | ||
1440 | Expression* | |
ceeb4318 | 1441 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1442 | { |
b13c66cd | 1443 | Location location = this->location(); |
e440a328 | 1444 | Named_object* no = this->named_object_; |
deded542 | 1445 | Named_object* real; |
1446 | if (!no->is_unknown()) | |
1447 | real = no; | |
1448 | else | |
e440a328 | 1449 | { |
deded542 | 1450 | real = no->unknown_value()->real_named_object(); |
1451 | if (real == NULL) | |
1452 | { | |
1453 | if (this->is_composite_literal_key_) | |
1454 | return this; | |
acf8e158 | 1455 | if (!this->no_error_message_) |
631d5788 | 1456 | go_error_at(location, "reference to undefined name %qs", |
1457 | this->named_object_->message_name().c_str()); | |
deded542 | 1458 | return Expression::make_error(location); |
1459 | } | |
e440a328 | 1460 | } |
1461 | switch (real->classification()) | |
1462 | { | |
1463 | case Named_object::NAMED_OBJECT_CONST: | |
1464 | return Expression::make_const_reference(real, location); | |
1465 | case Named_object::NAMED_OBJECT_TYPE: | |
1466 | return Expression::make_type(real->type_value(), location); | |
1467 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1468 | if (this->is_composite_literal_key_) | |
1469 | return this; | |
acf8e158 | 1470 | if (!this->no_error_message_) |
631d5788 | 1471 | go_error_at(location, "reference to undefined type %qs", |
1472 | real->message_name().c_str()); | |
e440a328 | 1473 | return Expression::make_error(location); |
1474 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1475 | real->var_value()->set_is_used(); |
e440a328 | 1476 | return Expression::make_var_reference(real, location); |
1477 | case Named_object::NAMED_OBJECT_FUNC: | |
1478 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1479 | return Expression::make_func_reference(real, NULL, location); | |
1480 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1481 | if (this->is_composite_literal_key_) | |
1482 | return this; | |
acf8e158 | 1483 | if (!this->no_error_message_) |
631d5788 | 1484 | go_error_at(location, "unexpected reference to package"); |
e440a328 | 1485 | return Expression::make_error(location); |
1486 | default: | |
c3e6f413 | 1487 | go_unreachable(); |
e440a328 | 1488 | } |
1489 | } | |
1490 | ||
d751bb78 | 1491 | // Dump the ast representation for an unknown expression to a dump context. |
1492 | ||
1493 | void | |
1494 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1495 | { | |
1496 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1497 | << ")"; | |
d751bb78 | 1498 | } |
1499 | ||
e440a328 | 1500 | // Make a reference to an unknown name. |
1501 | ||
acf8e158 | 1502 | Unknown_expression* |
b13c66cd | 1503 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1504 | { |
e440a328 | 1505 | return new Unknown_expression(no, location); |
1506 | } | |
1507 | ||
1508 | // A boolean expression. | |
1509 | ||
1510 | class Boolean_expression : public Expression | |
1511 | { | |
1512 | public: | |
b13c66cd | 1513 | Boolean_expression(bool val, Location location) |
e440a328 | 1514 | : Expression(EXPRESSION_BOOLEAN, location), |
1515 | val_(val), type_(NULL) | |
1516 | { } | |
1517 | ||
1518 | static Expression* | |
1519 | do_import(Import*); | |
1520 | ||
1521 | protected: | |
1522 | bool | |
1523 | do_is_constant() const | |
1524 | { return true; } | |
1525 | ||
0e168074 | 1526 | bool |
3ae06f68 | 1527 | do_is_static_initializer() const |
0e168074 | 1528 | { return true; } |
1529 | ||
e440a328 | 1530 | Type* |
1531 | do_type(); | |
1532 | ||
1533 | void | |
1534 | do_determine_type(const Type_context*); | |
1535 | ||
1536 | Expression* | |
1537 | do_copy() | |
1538 | { return this; } | |
1539 | ||
ea664253 | 1540 | Bexpression* |
1541 | do_get_backend(Translate_context* context) | |
1542 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1543 | |
1544 | void | |
1545 | do_export(Export* exp) const | |
1546 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1547 | ||
d751bb78 | 1548 | void |
1549 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1550 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1551 | ||
e440a328 | 1552 | private: |
1553 | // The constant. | |
1554 | bool val_; | |
1555 | // The type as determined by context. | |
1556 | Type* type_; | |
1557 | }; | |
1558 | ||
1559 | // Get the type. | |
1560 | ||
1561 | Type* | |
1562 | Boolean_expression::do_type() | |
1563 | { | |
1564 | if (this->type_ == NULL) | |
1565 | this->type_ = Type::make_boolean_type(); | |
1566 | return this->type_; | |
1567 | } | |
1568 | ||
1569 | // Set the type from the context. | |
1570 | ||
1571 | void | |
1572 | Boolean_expression::do_determine_type(const Type_context* context) | |
1573 | { | |
1574 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1575 | ; | |
1576 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1577 | this->type_ = context->type; | |
1578 | else if (!context->may_be_abstract) | |
1579 | this->type_ = Type::lookup_bool_type(); | |
1580 | } | |
1581 | ||
1582 | // Import a boolean constant. | |
1583 | ||
1584 | Expression* | |
1585 | Boolean_expression::do_import(Import* imp) | |
1586 | { | |
1587 | if (imp->peek_char() == 't') | |
1588 | { | |
1589 | imp->require_c_string("true"); | |
1590 | return Expression::make_boolean(true, imp->location()); | |
1591 | } | |
1592 | else | |
1593 | { | |
1594 | imp->require_c_string("false"); | |
1595 | return Expression::make_boolean(false, imp->location()); | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | // Make a boolean expression. | |
1600 | ||
1601 | Expression* | |
b13c66cd | 1602 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1603 | { |
1604 | return new Boolean_expression(val, location); | |
1605 | } | |
1606 | ||
1607 | // Class String_expression. | |
1608 | ||
1609 | // Get the type. | |
1610 | ||
1611 | Type* | |
1612 | String_expression::do_type() | |
1613 | { | |
1614 | if (this->type_ == NULL) | |
1615 | this->type_ = Type::make_string_type(); | |
1616 | return this->type_; | |
1617 | } | |
1618 | ||
1619 | // Set the type from the context. | |
1620 | ||
1621 | void | |
1622 | String_expression::do_determine_type(const Type_context* context) | |
1623 | { | |
1624 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1625 | ; | |
1626 | else if (context->type != NULL && context->type->is_string_type()) | |
1627 | this->type_ = context->type; | |
1628 | else if (!context->may_be_abstract) | |
1629 | this->type_ = Type::lookup_string_type(); | |
1630 | } | |
1631 | ||
1632 | // Build a string constant. | |
1633 | ||
ea664253 | 1634 | Bexpression* |
1635 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1636 | { |
2c809f8f | 1637 | Gogo* gogo = context->gogo(); |
1638 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1639 | ||
1640 | Location loc = this->location(); | |
1641 | std::vector<Bexpression*> init(2); | |
1642 | Bexpression* str_cst = | |
1643 | gogo->backend()->string_constant_expression(this->val_); | |
1644 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1645 | ||
1646 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1647 | mpz_t lenval; | |
1648 | mpz_init_set_ui(lenval, this->val_.length()); | |
1649 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1650 | mpz_clear(lenval); | |
1651 | ||
ea664253 | 1652 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1653 | } |
1654 | ||
8b1c301d | 1655 | // Write string literal to string dump. |
e440a328 | 1656 | |
1657 | void | |
8b1c301d | 1658 | String_expression::export_string(String_dump* exp, |
1659 | const String_expression* str) | |
e440a328 | 1660 | { |
1661 | std::string s; | |
8b1c301d | 1662 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1663 | s += '"'; |
8b1c301d | 1664 | for (std::string::const_iterator p = str->val_.begin(); |
1665 | p != str->val_.end(); | |
e440a328 | 1666 | ++p) |
1667 | { | |
1668 | if (*p == '\\' || *p == '"') | |
1669 | { | |
1670 | s += '\\'; | |
1671 | s += *p; | |
1672 | } | |
1673 | else if (*p >= 0x20 && *p < 0x7f) | |
1674 | s += *p; | |
1675 | else if (*p == '\n') | |
1676 | s += "\\n"; | |
1677 | else if (*p == '\t') | |
1678 | s += "\\t"; | |
1679 | else | |
1680 | { | |
1681 | s += "\\x"; | |
1682 | unsigned char c = *p; | |
1683 | unsigned int dig = c >> 4; | |
1684 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1685 | dig = c & 0xf; | |
1686 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1687 | } | |
1688 | } | |
1689 | s += '"'; | |
1690 | exp->write_string(s); | |
1691 | } | |
1692 | ||
8b1c301d | 1693 | // Export a string expression. |
1694 | ||
1695 | void | |
1696 | String_expression::do_export(Export* exp) const | |
1697 | { | |
1698 | String_expression::export_string(exp, this); | |
1699 | } | |
1700 | ||
e440a328 | 1701 | // Import a string expression. |
1702 | ||
1703 | Expression* | |
1704 | String_expression::do_import(Import* imp) | |
1705 | { | |
1706 | imp->require_c_string("\""); | |
1707 | std::string val; | |
1708 | while (true) | |
1709 | { | |
1710 | int c = imp->get_char(); | |
1711 | if (c == '"' || c == -1) | |
1712 | break; | |
1713 | if (c != '\\') | |
1714 | val += static_cast<char>(c); | |
1715 | else | |
1716 | { | |
1717 | c = imp->get_char(); | |
1718 | if (c == '\\' || c == '"') | |
1719 | val += static_cast<char>(c); | |
1720 | else if (c == 'n') | |
1721 | val += '\n'; | |
1722 | else if (c == 't') | |
1723 | val += '\t'; | |
1724 | else if (c == 'x') | |
1725 | { | |
1726 | c = imp->get_char(); | |
1727 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1728 | c = imp->get_char(); | |
1729 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1730 | char v = (vh << 4) | vl; | |
1731 | val += v; | |
1732 | } | |
1733 | else | |
1734 | { | |
631d5788 | 1735 | go_error_at(imp->location(), "bad string constant"); |
e440a328 | 1736 | return Expression::make_error(imp->location()); |
1737 | } | |
1738 | } | |
1739 | } | |
1740 | return Expression::make_string(val, imp->location()); | |
1741 | } | |
1742 | ||
d751bb78 | 1743 | // Ast dump for string expression. |
1744 | ||
1745 | void | |
1746 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1747 | { | |
8b1c301d | 1748 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1749 | } |
1750 | ||
e440a328 | 1751 | // Make a string expression. |
1752 | ||
1753 | Expression* | |
b13c66cd | 1754 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1755 | { |
1756 | return new String_expression(val, location); | |
1757 | } | |
1758 | ||
2c809f8f | 1759 | // An expression that evaluates to some characteristic of a string. |
1760 | // This is used when indexing, bound-checking, or nil checking a string. | |
1761 | ||
1762 | class String_info_expression : public Expression | |
1763 | { | |
1764 | public: | |
1765 | String_info_expression(Expression* string, String_info string_info, | |
1766 | Location location) | |
1767 | : Expression(EXPRESSION_STRING_INFO, location), | |
1768 | string_(string), string_info_(string_info) | |
1769 | { } | |
1770 | ||
1771 | protected: | |
1772 | Type* | |
1773 | do_type(); | |
1774 | ||
1775 | void | |
1776 | do_determine_type(const Type_context*) | |
1777 | { go_unreachable(); } | |
1778 | ||
1779 | Expression* | |
1780 | do_copy() | |
1781 | { | |
1782 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1783 | this->location()); | |
1784 | } | |
1785 | ||
ea664253 | 1786 | Bexpression* |
1787 | do_get_backend(Translate_context* context); | |
2c809f8f | 1788 | |
1789 | void | |
1790 | do_dump_expression(Ast_dump_context*) const; | |
1791 | ||
1792 | void | |
1793 | do_issue_nil_check() | |
1794 | { this->string_->issue_nil_check(); } | |
1795 | ||
1796 | private: | |
1797 | // The string for which we are getting information. | |
1798 | Expression* string_; | |
1799 | // What information we want. | |
1800 | String_info string_info_; | |
1801 | }; | |
1802 | ||
1803 | // Return the type of the string info. | |
1804 | ||
1805 | Type* | |
1806 | String_info_expression::do_type() | |
1807 | { | |
1808 | switch (this->string_info_) | |
1809 | { | |
1810 | case STRING_INFO_DATA: | |
1811 | { | |
1812 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1813 | return Type::make_pointer_type(byte_type); | |
1814 | } | |
1815 | case STRING_INFO_LENGTH: | |
1816 | return Type::lookup_integer_type("int"); | |
1817 | default: | |
1818 | go_unreachable(); | |
1819 | } | |
1820 | } | |
1821 | ||
1822 | // Return string information in GENERIC. | |
1823 | ||
ea664253 | 1824 | Bexpression* |
1825 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1826 | { |
1827 | Gogo* gogo = context->gogo(); | |
1828 | ||
ea664253 | 1829 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1830 | switch (this->string_info_) |
1831 | { | |
1832 | case STRING_INFO_DATA: | |
1833 | case STRING_INFO_LENGTH: | |
ea664253 | 1834 | return gogo->backend()->struct_field_expression(bstring, |
1835 | this->string_info_, | |
1836 | this->location()); | |
2c809f8f | 1837 | break; |
1838 | default: | |
1839 | go_unreachable(); | |
1840 | } | |
2c809f8f | 1841 | } |
1842 | ||
1843 | // Dump ast representation for a type info expression. | |
1844 | ||
1845 | void | |
1846 | String_info_expression::do_dump_expression( | |
1847 | Ast_dump_context* ast_dump_context) const | |
1848 | { | |
1849 | ast_dump_context->ostream() << "stringinfo("; | |
1850 | this->string_->dump_expression(ast_dump_context); | |
1851 | ast_dump_context->ostream() << ","; | |
1852 | ast_dump_context->ostream() << | |
1853 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1854 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1855 | : "unknown"); | |
1856 | ast_dump_context->ostream() << ")"; | |
1857 | } | |
1858 | ||
1859 | // Make a string info expression. | |
1860 | ||
1861 | Expression* | |
1862 | Expression::make_string_info(Expression* string, String_info string_info, | |
1863 | Location location) | |
1864 | { | |
1865 | return new String_info_expression(string, string_info, location); | |
1866 | } | |
1867 | ||
e440a328 | 1868 | // Make an integer expression. |
1869 | ||
1870 | class Integer_expression : public Expression | |
1871 | { | |
1872 | public: | |
5d4b8566 | 1873 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1874 | Location location) | |
e440a328 | 1875 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1876 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1877 | { mpz_init_set(this->val_, *val); } |
1878 | ||
1879 | static Expression* | |
1880 | do_import(Import*); | |
1881 | ||
8b1c301d | 1882 | // Write VAL to string dump. |
e440a328 | 1883 | static void |
8b1c301d | 1884 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1885 | |
d751bb78 | 1886 | // Write VAL to dump context. |
1887 | static void | |
1888 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1889 | ||
e440a328 | 1890 | protected: |
1891 | bool | |
1892 | do_is_constant() const | |
1893 | { return true; } | |
1894 | ||
0e168074 | 1895 | bool |
3ae06f68 | 1896 | do_is_static_initializer() const |
0e168074 | 1897 | { return true; } |
1898 | ||
e440a328 | 1899 | bool |
0c77715b | 1900 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1901 | |
1902 | Type* | |
1903 | do_type(); | |
1904 | ||
1905 | void | |
1906 | do_determine_type(const Type_context* context); | |
1907 | ||
1908 | void | |
1909 | do_check_types(Gogo*); | |
1910 | ||
ea664253 | 1911 | Bexpression* |
1912 | do_get_backend(Translate_context*); | |
e440a328 | 1913 | |
1914 | Expression* | |
1915 | do_copy() | |
5d4b8566 | 1916 | { |
1917 | if (this->is_character_constant_) | |
1918 | return Expression::make_character(&this->val_, this->type_, | |
1919 | this->location()); | |
1920 | else | |
e67508fa | 1921 | return Expression::make_integer_z(&this->val_, this->type_, |
1922 | this->location()); | |
5d4b8566 | 1923 | } |
e440a328 | 1924 | |
1925 | void | |
1926 | do_export(Export*) const; | |
1927 | ||
d751bb78 | 1928 | void |
1929 | do_dump_expression(Ast_dump_context*) const; | |
1930 | ||
e440a328 | 1931 | private: |
1932 | // The integer value. | |
1933 | mpz_t val_; | |
1934 | // The type so far. | |
1935 | Type* type_; | |
5d4b8566 | 1936 | // Whether this is a character constant. |
1937 | bool is_character_constant_; | |
e440a328 | 1938 | }; |
1939 | ||
0c77715b | 1940 | // Return a numeric constant for this expression. We have to mark |
1941 | // this as a character when appropriate. | |
e440a328 | 1942 | |
1943 | bool | |
0c77715b | 1944 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1945 | { |
0c77715b | 1946 | if (this->is_character_constant_) |
1947 | nc->set_rune(this->type_, this->val_); | |
1948 | else | |
1949 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1950 | return true; |
1951 | } | |
1952 | ||
1953 | // Return the current type. If we haven't set the type yet, we return | |
1954 | // an abstract integer type. | |
1955 | ||
1956 | Type* | |
1957 | Integer_expression::do_type() | |
1958 | { | |
1959 | if (this->type_ == NULL) | |
5d4b8566 | 1960 | { |
1961 | if (this->is_character_constant_) | |
1962 | this->type_ = Type::make_abstract_character_type(); | |
1963 | else | |
1964 | this->type_ = Type::make_abstract_integer_type(); | |
1965 | } | |
e440a328 | 1966 | return this->type_; |
1967 | } | |
1968 | ||
1969 | // Set the type of the integer value. Here we may switch from an | |
1970 | // abstract type to a real type. | |
1971 | ||
1972 | void | |
1973 | Integer_expression::do_determine_type(const Type_context* context) | |
1974 | { | |
1975 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1976 | ; | |
0c77715b | 1977 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1978 | this->type_ = context->type; |
1979 | else if (!context->may_be_abstract) | |
5d4b8566 | 1980 | { |
1981 | if (this->is_character_constant_) | |
1982 | this->type_ = Type::lookup_integer_type("int32"); | |
1983 | else | |
1984 | this->type_ = Type::lookup_integer_type("int"); | |
1985 | } | |
e440a328 | 1986 | } |
1987 | ||
e440a328 | 1988 | // Check the type of an integer constant. |
1989 | ||
1990 | void | |
1991 | Integer_expression::do_check_types(Gogo*) | |
1992 | { | |
0c77715b | 1993 | Type* type = this->type_; |
1994 | if (type == NULL) | |
e440a328 | 1995 | return; |
0c77715b | 1996 | Numeric_constant nc; |
1997 | if (this->is_character_constant_) | |
1998 | nc.set_rune(NULL, this->val_); | |
1999 | else | |
2000 | nc.set_int(NULL, this->val_); | |
2001 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 2002 | this->set_is_error(); |
2003 | } | |
2004 | ||
ea664253 | 2005 | // Get the backend representation for an integer constant. |
e440a328 | 2006 | |
ea664253 | 2007 | Bexpression* |
2008 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2009 | { |
12373dd5 | 2010 | if (this->is_error_expression() |
2011 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2012 | { | |
2013 | go_assert(saw_errors()); | |
2014 | return context->gogo()->backend()->error_expression(); | |
2015 | } | |
2016 | ||
48c2a53a | 2017 | Type* resolved_type = NULL; |
e440a328 | 2018 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2019 | resolved_type = this->type_; |
e440a328 | 2020 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
2021 | { | |
2022 | // We are converting to an abstract floating point type. | |
48c2a53a | 2023 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2024 | } |
2025 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2026 | { | |
2027 | // We are converting to an abstract complex type. | |
48c2a53a | 2028 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2029 | } |
2030 | else | |
2031 | { | |
2032 | // If we still have an abstract type here, then this is being | |
2033 | // used in a constant expression which didn't get reduced for | |
2034 | // some reason. Use a type which will fit the value. We use <, | |
2035 | // not <=, because we need an extra bit for the sign bit. | |
2036 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 2037 | Type* int_type = Type::lookup_integer_type("int"); |
2038 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 2039 | resolved_type = int_type; |
e440a328 | 2040 | else if (bits < 64) |
48c2a53a | 2041 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 2042 | else |
48c2a53a | 2043 | { |
2044 | if (!saw_errors()) | |
631d5788 | 2045 | go_error_at(this->location(), |
2046 | "unknown type for large integer constant"); | |
ea664253 | 2047 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 2048 | } |
e440a328 | 2049 | } |
48c2a53a | 2050 | Numeric_constant nc; |
2051 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 2052 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2053 | } |
2054 | ||
2055 | // Write VAL to export data. | |
2056 | ||
2057 | void | |
8b1c301d | 2058 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 2059 | { |
2060 | char* s = mpz_get_str(NULL, 10, val); | |
2061 | exp->write_c_string(s); | |
2062 | free(s); | |
2063 | } | |
2064 | ||
2065 | // Export an integer in a constant expression. | |
2066 | ||
2067 | void | |
2068 | Integer_expression::do_export(Export* exp) const | |
2069 | { | |
2070 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 2071 | if (this->is_character_constant_) |
2072 | exp->write_c_string("'"); | |
e440a328 | 2073 | // A trailing space lets us reliably identify the end of the number. |
2074 | exp->write_c_string(" "); | |
2075 | } | |
2076 | ||
2077 | // Import an integer, floating point, or complex value. This handles | |
2078 | // all these types because they all start with digits. | |
2079 | ||
2080 | Expression* | |
2081 | Integer_expression::do_import(Import* imp) | |
2082 | { | |
2083 | std::string num = imp->read_identifier(); | |
2084 | imp->require_c_string(" "); | |
2085 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2086 | { | |
2087 | mpfr_t real; | |
2088 | size_t plus_pos = num.find('+', 1); | |
2089 | size_t minus_pos = num.find('-', 1); | |
2090 | size_t pos; | |
2091 | if (plus_pos == std::string::npos) | |
2092 | pos = minus_pos; | |
2093 | else if (minus_pos == std::string::npos) | |
2094 | pos = plus_pos; | |
2095 | else | |
2096 | { | |
631d5788 | 2097 | go_error_at(imp->location(), "bad number in import data: %qs", |
2098 | num.c_str()); | |
e440a328 | 2099 | return Expression::make_error(imp->location()); |
2100 | } | |
2101 | if (pos == std::string::npos) | |
2102 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2103 | else | |
2104 | { | |
2105 | std::string real_str = num.substr(0, pos); | |
2106 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2107 | { | |
631d5788 | 2108 | go_error_at(imp->location(), "bad number in import data: %qs", |
2109 | real_str.c_str()); | |
e440a328 | 2110 | return Expression::make_error(imp->location()); |
2111 | } | |
2112 | } | |
2113 | ||
2114 | std::string imag_str; | |
2115 | if (pos == std::string::npos) | |
2116 | imag_str = num; | |
2117 | else | |
2118 | imag_str = num.substr(pos); | |
2119 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2120 | mpfr_t imag; | |
2121 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2122 | { | |
631d5788 | 2123 | go_error_at(imp->location(), "bad number in import data: %qs", |
2124 | imag_str.c_str()); | |
e440a328 | 2125 | return Expression::make_error(imp->location()); |
2126 | } | |
fcbea5e4 | 2127 | mpc_t cval; |
2128 | mpc_init2(cval, mpc_precision); | |
2129 | mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); | |
e440a328 | 2130 | mpfr_clear(real); |
2131 | mpfr_clear(imag); | |
fcbea5e4 | 2132 | Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); |
2133 | mpc_clear(cval); | |
e440a328 | 2134 | return ret; |
2135 | } | |
2136 | else if (num.find('.') == std::string::npos | |
2137 | && num.find('E') == std::string::npos) | |
2138 | { | |
5d4b8566 | 2139 | bool is_character_constant = (!num.empty() |
2140 | && num[num.length() - 1] == '\''); | |
2141 | if (is_character_constant) | |
2142 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2143 | mpz_t val; |
2144 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2145 | { | |
631d5788 | 2146 | go_error_at(imp->location(), "bad number in import data: %qs", |
2147 | num.c_str()); | |
e440a328 | 2148 | return Expression::make_error(imp->location()); |
2149 | } | |
5d4b8566 | 2150 | Expression* ret; |
2151 | if (is_character_constant) | |
2152 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2153 | else | |
e67508fa | 2154 | ret = Expression::make_integer_z(&val, NULL, imp->location()); |
e440a328 | 2155 | mpz_clear(val); |
2156 | return ret; | |
2157 | } | |
2158 | else | |
2159 | { | |
2160 | mpfr_t val; | |
2161 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2162 | { | |
631d5788 | 2163 | go_error_at(imp->location(), "bad number in import data: %qs", |
2164 | num.c_str()); | |
e440a328 | 2165 | return Expression::make_error(imp->location()); |
2166 | } | |
2167 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2168 | mpfr_clear(val); | |
2169 | return ret; | |
2170 | } | |
2171 | } | |
d751bb78 | 2172 | // Ast dump for integer expression. |
2173 | ||
2174 | void | |
2175 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2176 | { | |
5d4b8566 | 2177 | if (this->is_character_constant_) |
2178 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2179 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2180 | if (this->is_character_constant_) |
2181 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2182 | } |
2183 | ||
e67508fa | 2184 | // Build a new integer value from a multi-precision integer. |
e440a328 | 2185 | |
2186 | Expression* | |
e67508fa | 2187 | Expression::make_integer_z(const mpz_t* val, Type* type, Location location) |
5d4b8566 | 2188 | { |
2189 | return new Integer_expression(val, type, false, location); | |
2190 | } | |
2191 | ||
e67508fa | 2192 | // Build a new integer value from an unsigned long. |
2193 | ||
2194 | Expression* | |
2195 | Expression::make_integer_ul(unsigned long val, Type *type, Location location) | |
2196 | { | |
2197 | mpz_t zval; | |
2198 | mpz_init_set_ui(zval, val); | |
2199 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2200 | mpz_clear(zval); | |
2201 | return ret; | |
2202 | } | |
2203 | ||
2204 | // Build a new integer value from a signed long. | |
2205 | ||
2206 | Expression* | |
2207 | Expression::make_integer_sl(long val, Type *type, Location location) | |
2208 | { | |
2209 | mpz_t zval; | |
2210 | mpz_init_set_si(zval, val); | |
2211 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2212 | mpz_clear(zval); | |
2213 | return ret; | |
2214 | } | |
2215 | ||
3f378015 | 2216 | // Store an int64_t in an uninitialized mpz_t. |
2217 | ||
2218 | static void | |
2219 | set_mpz_from_int64(mpz_t* zval, int64_t val) | |
2220 | { | |
2221 | if (val >= 0) | |
2222 | { | |
2223 | unsigned long ul = static_cast<unsigned long>(val); | |
2224 | if (static_cast<int64_t>(ul) == val) | |
2225 | { | |
2226 | mpz_init_set_ui(*zval, ul); | |
2227 | return; | |
2228 | } | |
2229 | } | |
2230 | uint64_t uv; | |
2231 | if (val >= 0) | |
2232 | uv = static_cast<uint64_t>(val); | |
2233 | else | |
2234 | uv = static_cast<uint64_t>(- val); | |
2235 | unsigned long ul = uv & 0xffffffffUL; | |
2236 | mpz_init_set_ui(*zval, ul); | |
2237 | mpz_t hval; | |
2238 | mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); | |
2239 | mpz_mul_2exp(hval, hval, 32); | |
2240 | mpz_add(*zval, *zval, hval); | |
2241 | mpz_clear(hval); | |
2242 | if (val < 0) | |
2243 | mpz_neg(*zval, *zval); | |
2244 | } | |
2245 | ||
2246 | // Build a new integer value from an int64_t. | |
2247 | ||
2248 | Expression* | |
2249 | Expression::make_integer_int64(int64_t val, Type* type, Location location) | |
2250 | { | |
2251 | mpz_t zval; | |
2252 | set_mpz_from_int64(&zval, val); | |
2253 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2254 | mpz_clear(zval); | |
2255 | return ret; | |
2256 | } | |
2257 | ||
5d4b8566 | 2258 | // Build a new character constant value. |
2259 | ||
2260 | Expression* | |
2261 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2262 | { |
5d4b8566 | 2263 | return new Integer_expression(val, type, true, location); |
e440a328 | 2264 | } |
2265 | ||
2266 | // Floats. | |
2267 | ||
2268 | class Float_expression : public Expression | |
2269 | { | |
2270 | public: | |
b13c66cd | 2271 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2272 | : Expression(EXPRESSION_FLOAT, location), |
2273 | type_(type) | |
2274 | { | |
2275 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2276 | } | |
2277 | ||
e440a328 | 2278 | // Write VAL to export data. |
2279 | static void | |
8b1c301d | 2280 | export_float(String_dump* exp, const mpfr_t val); |
2281 | ||
d751bb78 | 2282 | // Write VAL to dump file. |
2283 | static void | |
2284 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2285 | |
2286 | protected: | |
2287 | bool | |
2288 | do_is_constant() const | |
2289 | { return true; } | |
2290 | ||
0e168074 | 2291 | bool |
3ae06f68 | 2292 | do_is_static_initializer() const |
0e168074 | 2293 | { return true; } |
2294 | ||
e440a328 | 2295 | bool |
0c77715b | 2296 | do_numeric_constant_value(Numeric_constant* nc) const |
2297 | { | |
2298 | nc->set_float(this->type_, this->val_); | |
2299 | return true; | |
2300 | } | |
e440a328 | 2301 | |
2302 | Type* | |
2303 | do_type(); | |
2304 | ||
2305 | void | |
2306 | do_determine_type(const Type_context*); | |
2307 | ||
2308 | void | |
2309 | do_check_types(Gogo*); | |
2310 | ||
2311 | Expression* | |
2312 | do_copy() | |
2313 | { return Expression::make_float(&this->val_, this->type_, | |
2314 | this->location()); } | |
2315 | ||
ea664253 | 2316 | Bexpression* |
2317 | do_get_backend(Translate_context*); | |
e440a328 | 2318 | |
2319 | void | |
2320 | do_export(Export*) const; | |
2321 | ||
d751bb78 | 2322 | void |
2323 | do_dump_expression(Ast_dump_context*) const; | |
2324 | ||
e440a328 | 2325 | private: |
2326 | // The floating point value. | |
2327 | mpfr_t val_; | |
2328 | // The type so far. | |
2329 | Type* type_; | |
2330 | }; | |
2331 | ||
e440a328 | 2332 | // Return the current type. If we haven't set the type yet, we return |
2333 | // an abstract float type. | |
2334 | ||
2335 | Type* | |
2336 | Float_expression::do_type() | |
2337 | { | |
2338 | if (this->type_ == NULL) | |
2339 | this->type_ = Type::make_abstract_float_type(); | |
2340 | return this->type_; | |
2341 | } | |
2342 | ||
2343 | // Set the type of the float value. Here we may switch from an | |
2344 | // abstract type to a real type. | |
2345 | ||
2346 | void | |
2347 | Float_expression::do_determine_type(const Type_context* context) | |
2348 | { | |
2349 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2350 | ; | |
2351 | else if (context->type != NULL | |
2352 | && (context->type->integer_type() != NULL | |
2353 | || context->type->float_type() != NULL | |
2354 | || context->type->complex_type() != NULL)) | |
2355 | this->type_ = context->type; | |
2356 | else if (!context->may_be_abstract) | |
48080209 | 2357 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2358 | } |
2359 | ||
e440a328 | 2360 | // Check the type of a float value. |
2361 | ||
2362 | void | |
2363 | Float_expression::do_check_types(Gogo*) | |
2364 | { | |
0c77715b | 2365 | Type* type = this->type_; |
2366 | if (type == NULL) | |
e440a328 | 2367 | return; |
0c77715b | 2368 | Numeric_constant nc; |
2369 | nc.set_float(NULL, this->val_); | |
2370 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2371 | this->set_is_error(); |
e440a328 | 2372 | } |
2373 | ||
ea664253 | 2374 | // Get the backend representation for a float constant. |
e440a328 | 2375 | |
ea664253 | 2376 | Bexpression* |
2377 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2378 | { |
12373dd5 | 2379 | if (this->is_error_expression() |
2380 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2381 | { | |
2382 | go_assert(saw_errors()); | |
2383 | return context->gogo()->backend()->error_expression(); | |
2384 | } | |
2385 | ||
48c2a53a | 2386 | Type* resolved_type; |
e440a328 | 2387 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2388 | resolved_type = this->type_; |
e440a328 | 2389 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2390 | { | |
2391 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2392 | resolved_type = Type::lookup_integer_type("int"); |
2393 | } | |
2394 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2395 | { | |
2396 | // We are converting to an abstract complex type. | |
2397 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2398 | } |
2399 | else | |
2400 | { | |
2401 | // If we still have an abstract type here, then this is being | |
2402 | // used in a constant expression which didn't get reduced. We | |
2403 | // just use float64 and hope for the best. | |
48c2a53a | 2404 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2405 | } |
48c2a53a | 2406 | |
2407 | Numeric_constant nc; | |
2408 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2409 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2410 | } |
2411 | ||
8b1c301d | 2412 | // Write a floating point number to a string dump. |
e440a328 | 2413 | |
2414 | void | |
8b1c301d | 2415 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2416 | { |
2417 | mp_exp_t exponent; | |
2418 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2419 | if (*s == '-') | |
2420 | exp->write_c_string("-"); | |
2421 | exp->write_c_string("0."); | |
2422 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2423 | mpfr_free_str(s); | |
2424 | char buf[30]; | |
2425 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2426 | exp->write_c_string(buf); | |
2427 | } | |
2428 | ||
2429 | // Export a floating point number in a constant expression. | |
2430 | ||
2431 | void | |
2432 | Float_expression::do_export(Export* exp) const | |
2433 | { | |
2434 | Float_expression::export_float(exp, this->val_); | |
2435 | // A trailing space lets us reliably identify the end of the number. | |
2436 | exp->write_c_string(" "); | |
2437 | } | |
2438 | ||
d751bb78 | 2439 | // Dump a floating point number to the dump file. |
2440 | ||
2441 | void | |
2442 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2443 | { | |
8b1c301d | 2444 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2445 | } |
2446 | ||
e440a328 | 2447 | // Make a float expression. |
2448 | ||
2449 | Expression* | |
b13c66cd | 2450 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2451 | { |
2452 | return new Float_expression(val, type, location); | |
2453 | } | |
2454 | ||
2455 | // Complex numbers. | |
2456 | ||
2457 | class Complex_expression : public Expression | |
2458 | { | |
2459 | public: | |
fcbea5e4 | 2460 | Complex_expression(const mpc_t* val, Type* type, Location location) |
e440a328 | 2461 | : Expression(EXPRESSION_COMPLEX, location), |
2462 | type_(type) | |
2463 | { | |
fcbea5e4 | 2464 | mpc_init2(this->val_, mpc_precision); |
2465 | mpc_set(this->val_, *val, MPC_RNDNN); | |
e440a328 | 2466 | } |
2467 | ||
fcbea5e4 | 2468 | // Write VAL to string dump. |
e440a328 | 2469 | static void |
fcbea5e4 | 2470 | export_complex(String_dump* exp, const mpc_t val); |
e440a328 | 2471 | |
d751bb78 | 2472 | // Write REAL/IMAG to dump context. |
2473 | static void | |
fcbea5e4 | 2474 | dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); |
d751bb78 | 2475 | |
e440a328 | 2476 | protected: |
2477 | bool | |
2478 | do_is_constant() const | |
2479 | { return true; } | |
2480 | ||
0e168074 | 2481 | bool |
3ae06f68 | 2482 | do_is_static_initializer() const |
0e168074 | 2483 | { return true; } |
2484 | ||
e440a328 | 2485 | bool |
0c77715b | 2486 | do_numeric_constant_value(Numeric_constant* nc) const |
2487 | { | |
fcbea5e4 | 2488 | nc->set_complex(this->type_, this->val_); |
0c77715b | 2489 | return true; |
2490 | } | |
e440a328 | 2491 | |
2492 | Type* | |
2493 | do_type(); | |
2494 | ||
2495 | void | |
2496 | do_determine_type(const Type_context*); | |
2497 | ||
2498 | void | |
2499 | do_check_types(Gogo*); | |
2500 | ||
2501 | Expression* | |
2502 | do_copy() | |
2503 | { | |
fcbea5e4 | 2504 | return Expression::make_complex(&this->val_, this->type_, |
e440a328 | 2505 | this->location()); |
2506 | } | |
2507 | ||
ea664253 | 2508 | Bexpression* |
2509 | do_get_backend(Translate_context*); | |
e440a328 | 2510 | |
2511 | void | |
2512 | do_export(Export*) const; | |
2513 | ||
d751bb78 | 2514 | void |
2515 | do_dump_expression(Ast_dump_context*) const; | |
abd26de0 | 2516 | |
e440a328 | 2517 | private: |
fcbea5e4 | 2518 | // The complex value. |
2519 | mpc_t val_; | |
e440a328 | 2520 | // The type if known. |
2521 | Type* type_; | |
2522 | }; | |
2523 | ||
e440a328 | 2524 | // Return the current type. If we haven't set the type yet, we return |
2525 | // an abstract complex type. | |
2526 | ||
2527 | Type* | |
2528 | Complex_expression::do_type() | |
2529 | { | |
2530 | if (this->type_ == NULL) | |
2531 | this->type_ = Type::make_abstract_complex_type(); | |
2532 | return this->type_; | |
2533 | } | |
2534 | ||
2535 | // Set the type of the complex value. Here we may switch from an | |
2536 | // abstract type to a real type. | |
2537 | ||
2538 | void | |
2539 | Complex_expression::do_determine_type(const Type_context* context) | |
2540 | { | |
2541 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2542 | ; | |
abd26de0 | 2543 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2544 | this->type_ = context->type; |
2545 | else if (!context->may_be_abstract) | |
48080209 | 2546 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2547 | } |
2548 | ||
e440a328 | 2549 | // Check the type of a complex value. |
2550 | ||
2551 | void | |
2552 | Complex_expression::do_check_types(Gogo*) | |
2553 | { | |
0c77715b | 2554 | Type* type = this->type_; |
2555 | if (type == NULL) | |
e440a328 | 2556 | return; |
0c77715b | 2557 | Numeric_constant nc; |
fcbea5e4 | 2558 | nc.set_complex(NULL, this->val_); |
0c77715b | 2559 | if (!nc.set_type(this->type_, true, this->location())) |
e440a328 | 2560 | this->set_is_error(); |
2561 | } | |
2562 | ||
ea664253 | 2563 | // Get the backend representation for a complex constant. |
e440a328 | 2564 | |
ea664253 | 2565 | Bexpression* |
2566 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2567 | { |
12373dd5 | 2568 | if (this->is_error_expression() |
2569 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2570 | { | |
2571 | go_assert(saw_errors()); | |
2572 | return context->gogo()->backend()->error_expression(); | |
2573 | } | |
2574 | ||
48c2a53a | 2575 | Type* resolved_type; |
e440a328 | 2576 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2577 | resolved_type = this->type_; |
2578 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2579 | { | |
2580 | // We are converting to an abstract integer type. | |
2581 | resolved_type = Type::lookup_integer_type("int"); | |
2582 | } | |
2583 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2584 | { | |
2585 | // We are converting to an abstract float type. | |
2586 | resolved_type = Type::lookup_float_type("float64"); | |
2587 | } | |
e440a328 | 2588 | else |
2589 | { | |
47ae02b7 | 2590 | // If we still have an abstract type here, this is being |
e440a328 | 2591 | // used in a constant expression which didn't get reduced. We |
2592 | // just use complex128 and hope for the best. | |
48c2a53a | 2593 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2594 | } |
48c2a53a | 2595 | |
2596 | Numeric_constant nc; | |
fcbea5e4 | 2597 | nc.set_complex(resolved_type, this->val_); |
ea664253 | 2598 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2599 | } |
2600 | ||
2601 | // Write REAL/IMAG to export data. | |
2602 | ||
2603 | void | |
fcbea5e4 | 2604 | Complex_expression::export_complex(String_dump* exp, const mpc_t val) |
e440a328 | 2605 | { |
fcbea5e4 | 2606 | if (!mpfr_zero_p(mpc_realref(val))) |
e440a328 | 2607 | { |
fcbea5e4 | 2608 | Float_expression::export_float(exp, mpc_realref(val)); |
d1db782d | 2609 | if (mpfr_sgn(mpc_imagref(val)) >= 0) |
e440a328 | 2610 | exp->write_c_string("+"); |
2611 | } | |
fcbea5e4 | 2612 | Float_expression::export_float(exp, mpc_imagref(val)); |
e440a328 | 2613 | exp->write_c_string("i"); |
2614 | } | |
2615 | ||
2616 | // Export a complex number in a constant expression. | |
2617 | ||
2618 | void | |
2619 | Complex_expression::do_export(Export* exp) const | |
2620 | { | |
fcbea5e4 | 2621 | Complex_expression::export_complex(exp, this->val_); |
e440a328 | 2622 | // A trailing space lets us reliably identify the end of the number. |
2623 | exp->write_c_string(" "); | |
2624 | } | |
2625 | ||
d751bb78 | 2626 | // Dump a complex expression to the dump file. |
2627 | ||
2628 | void | |
2629 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2630 | { | |
fcbea5e4 | 2631 | Complex_expression::export_complex(ast_dump_context, this->val_); |
d751bb78 | 2632 | } |
2633 | ||
e440a328 | 2634 | // Make a complex expression. |
2635 | ||
2636 | Expression* | |
fcbea5e4 | 2637 | Expression::make_complex(const mpc_t* val, Type* type, Location location) |
e440a328 | 2638 | { |
fcbea5e4 | 2639 | return new Complex_expression(val, type, location); |
e440a328 | 2640 | } |
2641 | ||
d5b605df | 2642 | // Find a named object in an expression. |
2643 | ||
2644 | class Find_named_object : public Traverse | |
2645 | { | |
2646 | public: | |
2647 | Find_named_object(Named_object* no) | |
2648 | : Traverse(traverse_expressions), | |
2649 | no_(no), found_(false) | |
2650 | { } | |
2651 | ||
2652 | // Whether we found the object. | |
2653 | bool | |
2654 | found() const | |
2655 | { return this->found_; } | |
2656 | ||
2657 | protected: | |
2658 | int | |
2659 | expression(Expression**); | |
2660 | ||
2661 | private: | |
2662 | // The object we are looking for. | |
2663 | Named_object* no_; | |
2664 | // Whether we found it. | |
2665 | bool found_; | |
2666 | }; | |
2667 | ||
e440a328 | 2668 | // A reference to a const in an expression. |
2669 | ||
2670 | class Const_expression : public Expression | |
2671 | { | |
2672 | public: | |
b13c66cd | 2673 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2674 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2675 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2676 | { } |
2677 | ||
d5b605df | 2678 | Named_object* |
2679 | named_object() | |
2680 | { return this->constant_; } | |
2681 | ||
a7f064d5 | 2682 | // Check that the initializer does not refer to the constant itself. |
2683 | void | |
2684 | check_for_init_loop(); | |
2685 | ||
e440a328 | 2686 | protected: |
ba4aedd4 | 2687 | int |
2688 | do_traverse(Traverse*); | |
2689 | ||
e440a328 | 2690 | Expression* |
ceeb4318 | 2691 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2692 | |
2693 | bool | |
2694 | do_is_constant() const | |
2695 | { return true; } | |
2696 | ||
0e168074 | 2697 | bool |
3ae06f68 | 2698 | do_is_static_initializer() const |
0e168074 | 2699 | { return true; } |
2700 | ||
e440a328 | 2701 | bool |
0c77715b | 2702 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2703 | |
2704 | bool | |
af6b489a | 2705 | do_string_constant_value(std::string* val) const; |
e440a328 | 2706 | |
2707 | Type* | |
2708 | do_type(); | |
2709 | ||
2710 | // The type of a const is set by the declaration, not the use. | |
2711 | void | |
2712 | do_determine_type(const Type_context*); | |
2713 | ||
2714 | void | |
2715 | do_check_types(Gogo*); | |
2716 | ||
2717 | Expression* | |
2718 | do_copy() | |
2719 | { return this; } | |
2720 | ||
ea664253 | 2721 | Bexpression* |
2722 | do_get_backend(Translate_context* context); | |
e440a328 | 2723 | |
2724 | // When exporting a reference to a const as part of a const | |
2725 | // expression, we export the value. We ignore the fact that it has | |
2726 | // a name. | |
2727 | void | |
2728 | do_export(Export* exp) const | |
2729 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2730 | ||
d751bb78 | 2731 | void |
2732 | do_dump_expression(Ast_dump_context*) const; | |
2733 | ||
e440a328 | 2734 | private: |
2735 | // The constant. | |
2736 | Named_object* constant_; | |
2737 | // The type of this reference. This is used if the constant has an | |
2738 | // abstract type. | |
2739 | Type* type_; | |
13e818f5 | 2740 | // Used to prevent infinite recursion when a constant incorrectly |
2741 | // refers to itself. | |
2742 | mutable bool seen_; | |
e440a328 | 2743 | }; |
2744 | ||
ba4aedd4 | 2745 | // Traversal. |
2746 | ||
2747 | int | |
2748 | Const_expression::do_traverse(Traverse* traverse) | |
2749 | { | |
2750 | if (this->type_ != NULL) | |
2751 | return Type::traverse(this->type_, traverse); | |
2752 | return TRAVERSE_CONTINUE; | |
2753 | } | |
2754 | ||
e440a328 | 2755 | // Lower a constant expression. This is where we convert the |
2756 | // predeclared constant iota into an integer value. | |
2757 | ||
2758 | Expression* | |
ceeb4318 | 2759 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2760 | Statement_inserter*, int iota_value) | |
e440a328 | 2761 | { |
2762 | if (this->constant_->const_value()->expr()->classification() | |
2763 | == EXPRESSION_IOTA) | |
2764 | { | |
2765 | if (iota_value == -1) | |
2766 | { | |
631d5788 | 2767 | go_error_at(this->location(), |
2768 | "iota is only defined in const declarations"); | |
e440a328 | 2769 | iota_value = 0; |
2770 | } | |
e67508fa | 2771 | return Expression::make_integer_ul(iota_value, NULL, this->location()); |
e440a328 | 2772 | } |
2773 | ||
2774 | // Make sure that the constant itself has been lowered. | |
2775 | gogo->lower_constant(this->constant_); | |
2776 | ||
2777 | return this; | |
2778 | } | |
2779 | ||
0c77715b | 2780 | // Return a numeric constant value. |
e440a328 | 2781 | |
2782 | bool | |
0c77715b | 2783 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2784 | { |
13e818f5 | 2785 | if (this->seen_) |
2786 | return false; | |
2787 | ||
e440a328 | 2788 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2789 | |
13e818f5 | 2790 | this->seen_ = true; |
2791 | ||
0c77715b | 2792 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2793 | |
13e818f5 | 2794 | this->seen_ = false; |
2795 | ||
e440a328 | 2796 | Type* ctype; |
2797 | if (this->type_ != NULL) | |
2798 | ctype = this->type_; | |
2799 | else | |
2800 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2801 | if (r && ctype != NULL) |
2802 | { | |
0c77715b | 2803 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2804 | return false; |
e440a328 | 2805 | } |
e440a328 | 2806 | |
e440a328 | 2807 | return r; |
2808 | } | |
2809 | ||
af6b489a | 2810 | bool |
2811 | Const_expression::do_string_constant_value(std::string* val) const | |
2812 | { | |
2813 | if (this->seen_) | |
2814 | return false; | |
2815 | ||
2816 | Expression* e = this->constant_->const_value()->expr(); | |
2817 | ||
2818 | this->seen_ = true; | |
2819 | bool ok = e->string_constant_value(val); | |
2820 | this->seen_ = false; | |
2821 | ||
2822 | return ok; | |
2823 | } | |
2824 | ||
e440a328 | 2825 | // Return the type of the const reference. |
2826 | ||
2827 | Type* | |
2828 | Const_expression::do_type() | |
2829 | { | |
2830 | if (this->type_ != NULL) | |
2831 | return this->type_; | |
13e818f5 | 2832 | |
2f78f012 | 2833 | Named_constant* nc = this->constant_->const_value(); |
2834 | ||
2835 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2836 | { |
2837 | this->report_error(_("constant refers to itself")); | |
2838 | this->type_ = Type::make_error_type(); | |
2839 | return this->type_; | |
2840 | } | |
2841 | ||
2842 | this->seen_ = true; | |
2843 | ||
e440a328 | 2844 | Type* ret = nc->type(); |
13e818f5 | 2845 | |
e440a328 | 2846 | if (ret != NULL) |
13e818f5 | 2847 | { |
2848 | this->seen_ = false; | |
2849 | return ret; | |
2850 | } | |
2851 | ||
e440a328 | 2852 | // During parsing, a named constant may have a NULL type, but we |
2853 | // must not return a NULL type here. | |
13e818f5 | 2854 | ret = nc->expr()->type(); |
2855 | ||
2856 | this->seen_ = false; | |
2857 | ||
2858 | return ret; | |
e440a328 | 2859 | } |
2860 | ||
2861 | // Set the type of the const reference. | |
2862 | ||
2863 | void | |
2864 | Const_expression::do_determine_type(const Type_context* context) | |
2865 | { | |
2866 | Type* ctype = this->constant_->const_value()->type(); | |
2867 | Type* cetype = (ctype != NULL | |
2868 | ? ctype | |
2869 | : this->constant_->const_value()->expr()->type()); | |
2870 | if (ctype != NULL && !ctype->is_abstract()) | |
2871 | ; | |
2872 | else if (context->type != NULL | |
0c77715b | 2873 | && context->type->is_numeric_type() |
2874 | && cetype->is_numeric_type()) | |
e440a328 | 2875 | this->type_ = context->type; |
2876 | else if (context->type != NULL | |
2877 | && context->type->is_string_type() | |
2878 | && cetype->is_string_type()) | |
2879 | this->type_ = context->type; | |
2880 | else if (context->type != NULL | |
2881 | && context->type->is_boolean_type() | |
2882 | && cetype->is_boolean_type()) | |
2883 | this->type_ = context->type; | |
2884 | else if (!context->may_be_abstract) | |
2885 | { | |
2886 | if (cetype->is_abstract()) | |
2887 | cetype = cetype->make_non_abstract_type(); | |
2888 | this->type_ = cetype; | |
2889 | } | |
2890 | } | |
2891 | ||
a7f064d5 | 2892 | // Check for a loop in which the initializer of a constant refers to |
2893 | // the constant itself. | |
e440a328 | 2894 | |
2895 | void | |
a7f064d5 | 2896 | Const_expression::check_for_init_loop() |
e440a328 | 2897 | { |
5c13bd80 | 2898 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2899 | return; |
2900 | ||
a7f064d5 | 2901 | if (this->seen_) |
2902 | { | |
2903 | this->report_error(_("constant refers to itself")); | |
2904 | this->type_ = Type::make_error_type(); | |
2905 | return; | |
2906 | } | |
2907 | ||
d5b605df | 2908 | Expression* init = this->constant_->const_value()->expr(); |
2909 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2910 | |
2911 | this->seen_ = true; | |
d5b605df | 2912 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2913 | this->seen_ = false; |
2914 | ||
d5b605df | 2915 | if (find_named_object.found()) |
2916 | { | |
5c13bd80 | 2917 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2918 | { |
2919 | this->report_error(_("constant refers to itself")); | |
2920 | this->type_ = Type::make_error_type(); | |
2921 | } | |
d5b605df | 2922 | return; |
2923 | } | |
a7f064d5 | 2924 | } |
2925 | ||
2926 | // Check types of a const reference. | |
2927 | ||
2928 | void | |
2929 | Const_expression::do_check_types(Gogo*) | |
2930 | { | |
5c13bd80 | 2931 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2932 | return; |
2933 | ||
2934 | this->check_for_init_loop(); | |
d5b605df | 2935 | |
0c77715b | 2936 | // Check that numeric constant fits in type. |
2937 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2938 | { |
0c77715b | 2939 | Numeric_constant nc; |
2940 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2941 | { |
0c77715b | 2942 | if (!nc.set_type(this->type_, true, this->location())) |
2943 | this->set_is_error(); | |
e440a328 | 2944 | } |
e440a328 | 2945 | } |
2946 | } | |
2947 | ||
ea664253 | 2948 | // Return the backend representation for a const reference. |
e440a328 | 2949 | |
ea664253 | 2950 | Bexpression* |
2951 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2952 | { |
12373dd5 | 2953 | if (this->is_error_expression() |
2954 | || (this->type_ != NULL && this->type_->is_error())) | |
2955 | { | |
2956 | go_assert(saw_errors()); | |
2957 | return context->backend()->error_expression(); | |
2958 | } | |
e440a328 | 2959 | |
2960 | // If the type has been set for this expression, but the underlying | |
2961 | // object is an abstract int or float, we try to get the abstract | |
2962 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2963 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2964 | if (this->type_ != NULL |
0c77715b | 2965 | && this->type_->is_numeric_type() |
a68492b4 | 2966 | && (this->constant_->const_value()->type() == NULL |
2967 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2968 | { |
0c77715b | 2969 | Numeric_constant nc; |
2970 | if (expr->numeric_constant_value(&nc) | |
2971 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2972 | { |
0c77715b | 2973 | Expression* e = nc.expression(this->location()); |
ea664253 | 2974 | return e->get_backend(context); |
e440a328 | 2975 | } |
e440a328 | 2976 | } |
2977 | ||
2c809f8f | 2978 | if (this->type_ != NULL) |
f2de4532 | 2979 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 2980 | return expr->get_backend(context); |
e440a328 | 2981 | } |
2982 | ||
d751bb78 | 2983 | // Dump ast representation for constant expression. |
2984 | ||
2985 | void | |
2986 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2987 | { | |
2988 | ast_dump_context->ostream() << this->constant_->name(); | |
2989 | } | |
2990 | ||
e440a328 | 2991 | // Make a reference to a constant in an expression. |
2992 | ||
2993 | Expression* | |
2994 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2995 | Location location) |
e440a328 | 2996 | { |
2997 | return new Const_expression(constant, location); | |
2998 | } | |
2999 | ||
d5b605df | 3000 | // Find a named object in an expression. |
3001 | ||
3002 | int | |
3003 | Find_named_object::expression(Expression** pexpr) | |
3004 | { | |
3005 | switch ((*pexpr)->classification()) | |
3006 | { | |
3007 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 3008 | { |
3009 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
3010 | if (ce->named_object() == this->no_) | |
3011 | break; | |
3012 | ||
3013 | // We need to check a constant initializer explicitly, as | |
3014 | // loops here will not be caught by the loop checking for | |
3015 | // variable initializers. | |
3016 | ce->check_for_init_loop(); | |
3017 | ||
3018 | return TRAVERSE_CONTINUE; | |
3019 | } | |
3020 | ||
d5b605df | 3021 | case Expression::EXPRESSION_VAR_REFERENCE: |
3022 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
3023 | break; | |
3024 | return TRAVERSE_CONTINUE; | |
3025 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
3026 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
3027 | break; | |
3028 | return TRAVERSE_CONTINUE; | |
3029 | default: | |
3030 | return TRAVERSE_CONTINUE; | |
3031 | } | |
3032 | this->found_ = true; | |
3033 | return TRAVERSE_EXIT; | |
3034 | } | |
3035 | ||
e440a328 | 3036 | // The nil value. |
3037 | ||
3038 | class Nil_expression : public Expression | |
3039 | { | |
3040 | public: | |
b13c66cd | 3041 | Nil_expression(Location location) |
e440a328 | 3042 | : Expression(EXPRESSION_NIL, location) |
3043 | { } | |
3044 | ||
3045 | static Expression* | |
3046 | do_import(Import*); | |
3047 | ||
3048 | protected: | |
3049 | bool | |
3050 | do_is_constant() const | |
3051 | { return true; } | |
3052 | ||
f9ca30f9 | 3053 | bool |
3ae06f68 | 3054 | do_is_static_initializer() const |
f9ca30f9 | 3055 | { return true; } |
3056 | ||
e440a328 | 3057 | Type* |
3058 | do_type() | |
3059 | { return Type::make_nil_type(); } | |
3060 | ||
3061 | void | |
3062 | do_determine_type(const Type_context*) | |
3063 | { } | |
3064 | ||
3065 | Expression* | |
3066 | do_copy() | |
3067 | { return this; } | |
3068 | ||
ea664253 | 3069 | Bexpression* |
3070 | do_get_backend(Translate_context* context) | |
3071 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 3072 | |
3073 | void | |
3074 | do_export(Export* exp) const | |
3075 | { exp->write_c_string("nil"); } | |
d751bb78 | 3076 | |
3077 | void | |
3078 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3079 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 3080 | }; |
3081 | ||
3082 | // Import a nil expression. | |
3083 | ||
3084 | Expression* | |
3085 | Nil_expression::do_import(Import* imp) | |
3086 | { | |
3087 | imp->require_c_string("nil"); | |
3088 | return Expression::make_nil(imp->location()); | |
3089 | } | |
3090 | ||
3091 | // Make a nil expression. | |
3092 | ||
3093 | Expression* | |
b13c66cd | 3094 | Expression::make_nil(Location location) |
e440a328 | 3095 | { |
3096 | return new Nil_expression(location); | |
3097 | } | |
3098 | ||
3099 | // The value of the predeclared constant iota. This is little more | |
3100 | // than a marker. This will be lowered to an integer in | |
3101 | // Const_expression::do_lower, which is where we know the value that | |
3102 | // it should have. | |
3103 | ||
3104 | class Iota_expression : public Parser_expression | |
3105 | { | |
3106 | public: | |
b13c66cd | 3107 | Iota_expression(Location location) |
e440a328 | 3108 | : Parser_expression(EXPRESSION_IOTA, location) |
3109 | { } | |
3110 | ||
3111 | protected: | |
3112 | Expression* | |
ceeb4318 | 3113 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 3114 | { go_unreachable(); } |
e440a328 | 3115 | |
3116 | // There should only ever be one of these. | |
3117 | Expression* | |
3118 | do_copy() | |
c3e6f413 | 3119 | { go_unreachable(); } |
d751bb78 | 3120 | |
3121 | void | |
3122 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3123 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3124 | }; |
3125 | ||
3126 | // Make an iota expression. This is only called for one case: the | |
3127 | // value of the predeclared constant iota. | |
3128 | ||
3129 | Expression* | |
3130 | Expression::make_iota() | |
3131 | { | |
b13c66cd | 3132 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3133 | return &iota_expression; |
3134 | } | |
3135 | ||
da244e59 | 3136 | // Class Type_conversion_expression. |
e440a328 | 3137 | |
3138 | // Traversal. | |
3139 | ||
3140 | int | |
3141 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3142 | { | |
3143 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3144 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3145 | return TRAVERSE_EXIT; | |
3146 | return TRAVERSE_CONTINUE; | |
3147 | } | |
3148 | ||
3149 | // Convert to a constant at lowering time. | |
3150 | ||
3151 | Expression* | |
ceeb4318 | 3152 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3153 | Statement_inserter*, int) | |
e440a328 | 3154 | { |
3155 | Type* type = this->type_; | |
3156 | Expression* val = this->expr_; | |
b13c66cd | 3157 | Location location = this->location(); |
e440a328 | 3158 | |
0c77715b | 3159 | if (type->is_numeric_type()) |
e440a328 | 3160 | { |
0c77715b | 3161 | Numeric_constant nc; |
3162 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3163 | { |
0c77715b | 3164 | if (!nc.set_type(type, true, location)) |
3165 | return Expression::make_error(location); | |
3166 | return nc.expression(location); | |
e440a328 | 3167 | } |
e440a328 | 3168 | } |
3169 | ||
d7739c9a | 3170 | // According to the language specification on string conversions |
3171 | // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): | |
3172 | // When converting an integer into a string, the string will be a UTF-8 | |
3173 | // representation of the integer and integers "outside the range of valid | |
3174 | // Unicode code points are converted to '\uFFFD'." | |
3175 | if (type->is_string_type()) | |
3176 | { | |
3177 | Numeric_constant nc; | |
3178 | if (val->numeric_constant_value(&nc) && nc.is_int()) | |
3179 | { | |
3180 | // An integer value doesn't fit in the Unicode code point range if it | |
3181 | // overflows the Go "int" type or is negative. | |
3182 | unsigned long ul; | |
3183 | if (!nc.set_type(Type::lookup_integer_type("int"), false, location) | |
3184 | || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) | |
3185 | return Expression::make_string("\ufffd", location); | |
3186 | } | |
3187 | } | |
3188 | ||
55072f2b | 3189 | if (type->is_slice_type()) |
e440a328 | 3190 | { |
3191 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3192 | bool is_byte = (element_type->integer_type() != NULL |
3193 | && element_type->integer_type()->is_byte()); | |
3194 | bool is_rune = (element_type->integer_type() != NULL | |
3195 | && element_type->integer_type()->is_rune()); | |
3196 | if (is_byte || is_rune) | |
e440a328 | 3197 | { |
3198 | std::string s; | |
3199 | if (val->string_constant_value(&s)) | |
3200 | { | |
3201 | Expression_list* vals = new Expression_list(); | |
3202 | if (is_byte) | |
3203 | { | |
3204 | for (std::string::const_iterator p = s.begin(); | |
3205 | p != s.end(); | |
3206 | p++) | |
3207 | { | |
e67508fa | 3208 | unsigned char c = static_cast<unsigned char>(*p); |
3209 | vals->push_back(Expression::make_integer_ul(c, | |
3210 | element_type, | |
3211 | location)); | |
e440a328 | 3212 | } |
3213 | } | |
3214 | else | |
3215 | { | |
3216 | const char *p = s.data(); | |
3217 | const char *pend = s.data() + s.length(); | |
3218 | while (p < pend) | |
3219 | { | |
3220 | unsigned int c; | |
3221 | int adv = Lex::fetch_char(p, &c); | |
3222 | if (adv == 0) | |
3223 | { | |
631d5788 | 3224 | go_warning_at(this->location(), 0, |
e440a328 | 3225 | "invalid UTF-8 encoding"); |
3226 | adv = 1; | |
3227 | } | |
3228 | p += adv; | |
e67508fa | 3229 | vals->push_back(Expression::make_integer_ul(c, |
3230 | element_type, | |
3231 | location)); | |
e440a328 | 3232 | } |
3233 | } | |
3234 | ||
3235 | return Expression::make_slice_composite_literal(type, vals, | |
3236 | location); | |
3237 | } | |
3238 | } | |
3239 | } | |
3240 | ||
3241 | return this; | |
3242 | } | |
3243 | ||
35a54f17 | 3244 | // Flatten a type conversion by using a temporary variable for the slice |
3245 | // in slice to string conversions. | |
3246 | ||
3247 | Expression* | |
3248 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3249 | Statement_inserter* inserter) | |
3250 | { | |
5bf8be8b | 3251 | if (this->type()->is_error_type() || this->expr_->is_error_expression()) |
3252 | { | |
3253 | go_assert(saw_errors()); | |
3254 | return Expression::make_error(this->location()); | |
3255 | } | |
3256 | ||
2c809f8f | 3257 | if (((this->type()->is_string_type() |
3258 | && this->expr_->type()->is_slice_type()) | |
8ba8cc87 | 3259 | || this->expr_->type()->interface_type() != NULL) |
35a54f17 | 3260 | && !this->expr_->is_variable()) |
3261 | { | |
3262 | Temporary_statement* temp = | |
3263 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3264 | inserter->insert(temp); | |
3265 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3266 | } | |
3267 | return this; | |
3268 | } | |
3269 | ||
1ca01a59 | 3270 | // Return whether a type conversion is a constant. |
3271 | ||
3272 | bool | |
3273 | Type_conversion_expression::do_is_constant() const | |
3274 | { | |
3275 | if (!this->expr_->is_constant()) | |
3276 | return false; | |
3277 | ||
3278 | // A conversion to a type that may not be used as a constant is not | |
3279 | // a constant. For example, []byte(nil). | |
3280 | Type* type = this->type_; | |
3281 | if (type->integer_type() == NULL | |
3282 | && type->float_type() == NULL | |
3283 | && type->complex_type() == NULL | |
3284 | && !type->is_boolean_type() | |
3285 | && !type->is_string_type()) | |
3286 | return false; | |
3287 | ||
3288 | return true; | |
3289 | } | |
3290 | ||
3ae06f68 | 3291 | // Return whether a type conversion can be used in a constant |
3292 | // initializer. | |
0e168074 | 3293 | |
3294 | bool | |
3ae06f68 | 3295 | Type_conversion_expression::do_is_static_initializer() const |
0e168074 | 3296 | { |
3297 | Type* type = this->type_; | |
3298 | Type* expr_type = this->expr_->type(); | |
3299 | ||
3300 | if (type->interface_type() != NULL | |
3301 | || expr_type->interface_type() != NULL) | |
3302 | return false; | |
3303 | ||
3ae06f68 | 3304 | if (!this->expr_->is_static_initializer()) |
0e168074 | 3305 | return false; |
3306 | ||
3307 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3308 | return true; | |
3309 | ||
3310 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3311 | } | |
3312 | ||
0c77715b | 3313 | // Return the constant numeric value if there is one. |
e440a328 | 3314 | |
3315 | bool | |
0c77715b | 3316 | Type_conversion_expression::do_numeric_constant_value( |
3317 | Numeric_constant* nc) const | |
e440a328 | 3318 | { |
0c77715b | 3319 | if (!this->type_->is_numeric_type()) |
e440a328 | 3320 | return false; |
0c77715b | 3321 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3322 | return false; |
0c77715b | 3323 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3324 | } |
3325 | ||
3326 | // Return the constant string value if there is one. | |
3327 | ||
3328 | bool | |
3329 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3330 | { | |
3331 | if (this->type_->is_string_type() | |
3332 | && this->expr_->type()->integer_type() != NULL) | |
3333 | { | |
0c77715b | 3334 | Numeric_constant nc; |
3335 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3336 | { |
0c77715b | 3337 | unsigned long ival; |
3338 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3339 | { |
0c77715b | 3340 | val->clear(); |
3341 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3342 | return true; |
3343 | } | |
3344 | } | |
e440a328 | 3345 | } |
3346 | ||
3347 | // FIXME: Could handle conversion from const []int here. | |
3348 | ||
3349 | return false; | |
3350 | } | |
3351 | ||
da244e59 | 3352 | // Determine the resulting type of the conversion. |
3353 | ||
3354 | void | |
3355 | Type_conversion_expression::do_determine_type(const Type_context*) | |
3356 | { | |
3357 | Type_context subcontext(this->type_, false); | |
3358 | this->expr_->determine_type(&subcontext); | |
3359 | } | |
3360 | ||
e440a328 | 3361 | // Check that types are convertible. |
3362 | ||
3363 | void | |
3364 | Type_conversion_expression::do_check_types(Gogo*) | |
3365 | { | |
3366 | Type* type = this->type_; | |
3367 | Type* expr_type = this->expr_->type(); | |
3368 | std::string reason; | |
3369 | ||
5c13bd80 | 3370 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3371 | { |
842f6425 | 3372 | this->set_is_error(); |
3373 | return; | |
3374 | } | |
3375 | ||
e440a328 | 3376 | if (this->may_convert_function_types_ |
3377 | && type->function_type() != NULL | |
3378 | && expr_type->function_type() != NULL) | |
3379 | return; | |
3380 | ||
3381 | if (Type::are_convertible(type, expr_type, &reason)) | |
3382 | return; | |
3383 | ||
631d5788 | 3384 | go_error_at(this->location(), "%s", reason.c_str()); |
e440a328 | 3385 | this->set_is_error(); |
3386 | } | |
3387 | ||
ea664253 | 3388 | // Get the backend representation for a type conversion. |
e440a328 | 3389 | |
ea664253 | 3390 | Bexpression* |
3391 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3392 | { |
e440a328 | 3393 | Type* type = this->type_; |
3394 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3395 | |
3396 | Gogo* gogo = context->gogo(); | |
3397 | Btype* btype = type->get_backend(gogo); | |
ea664253 | 3398 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3399 | Location loc = this->location(); |
3400 | ||
3401 | if (Type::are_identical(type, expr_type, false, NULL)) | |
ea664253 | 3402 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
2c809f8f | 3403 | else if (type->interface_type() != NULL |
3404 | || expr_type->interface_type() != NULL) | |
e440a328 | 3405 | { |
2c809f8f | 3406 | Expression* conversion = |
3407 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3408 | this->location()); | |
ea664253 | 3409 | return conversion->get_backend(context); |
e440a328 | 3410 | } |
3411 | else if (type->is_string_type() | |
3412 | && expr_type->integer_type() != NULL) | |
3413 | { | |
2c809f8f | 3414 | mpz_t intval; |
3415 | Numeric_constant nc; | |
3416 | if (this->expr_->numeric_constant_value(&nc) | |
3417 | && nc.to_int(&intval) | |
3418 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3419 | { |
e440a328 | 3420 | std::string s; |
2c809f8f | 3421 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3422 | mpz_clear(intval); | |
3423 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3424 | return se->get_backend(context); |
e440a328 | 3425 | } |
3426 | ||
f16ab008 | 3427 | Expression* i2s_expr = |
736a16ba | 3428 | Runtime::make_call(Runtime::INTSTRING, loc, 2, |
3429 | Expression::make_nil(loc), this->expr_); | |
ea664253 | 3430 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3431 | } |
55072f2b | 3432 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3433 | { |
55072f2b | 3434 | Array_type* a = expr_type->array_type(); |
e440a328 | 3435 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3436 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3437 | go_assert(this->expr_->is_variable()); |
3438 | ||
3439 | Runtime::Function code; | |
60963afd | 3440 | if (e->integer_type()->is_byte()) |
736a16ba | 3441 | code = Runtime::SLICEBYTETOSTRING; |
e440a328 | 3442 | else |
35a54f17 | 3443 | { |
3444 | go_assert(e->integer_type()->is_rune()); | |
736a16ba | 3445 | code = Runtime::SLICERUNETOSTRING; |
35a54f17 | 3446 | } |
736a16ba | 3447 | return Runtime::make_call(code, loc, 2, Expression::make_nil(loc), |
3448 | this->expr_)->get_backend(context); | |
e440a328 | 3449 | } |
411eb89e | 3450 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3451 | { |
3452 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3453 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3454 | |
2c809f8f | 3455 | Runtime::Function code; |
60963afd | 3456 | if (e->integer_type()->is_byte()) |
736a16ba | 3457 | code = Runtime::STRINGTOSLICEBYTE; |
e440a328 | 3458 | else |
3459 | { | |
60963afd | 3460 | go_assert(e->integer_type()->is_rune()); |
736a16ba | 3461 | code = Runtime::STRINGTOSLICERUNE; |
e440a328 | 3462 | } |
736a16ba | 3463 | Expression* s2a = Runtime::make_call(code, loc, 2, |
3464 | Expression::make_nil(loc), | |
3465 | this->expr_); | |
ea664253 | 3466 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3467 | } |
3468 | else if (type->is_numeric_type()) | |
3469 | { | |
3470 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
ea664253 | 3471 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3472 | } |
3473 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3474 | && (expr_type->points_to() != NULL |
3475 | || expr_type->integer_type())) | |
3476 | || (expr_type->is_unsafe_pointer_type() | |
3477 | && type->points_to() != NULL) | |
3478 | || (this->may_convert_function_types_ | |
3479 | && type->function_type() != NULL | |
3480 | && expr_type->function_type() != NULL)) | |
ea664253 | 3481 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3482 | else |
2c809f8f | 3483 | { |
3484 | Expression* conversion = | |
3485 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3486 | return conversion->get_backend(context); |
2c809f8f | 3487 | } |
e440a328 | 3488 | } |
3489 | ||
3490 | // Output a type conversion in a constant expression. | |
3491 | ||
3492 | void | |
3493 | Type_conversion_expression::do_export(Export* exp) const | |
3494 | { | |
3495 | exp->write_c_string("convert("); | |
3496 | exp->write_type(this->type_); | |
3497 | exp->write_c_string(", "); | |
3498 | this->expr_->export_expression(exp); | |
3499 | exp->write_c_string(")"); | |
3500 | } | |
3501 | ||
3502 | // Import a type conversion or a struct construction. | |
3503 | ||
3504 | Expression* | |
3505 | Type_conversion_expression::do_import(Import* imp) | |
3506 | { | |
3507 | imp->require_c_string("convert("); | |
3508 | Type* type = imp->read_type(); | |
3509 | imp->require_c_string(", "); | |
3510 | Expression* val = Expression::import_expression(imp); | |
3511 | imp->require_c_string(")"); | |
3512 | return Expression::make_cast(type, val, imp->location()); | |
3513 | } | |
3514 | ||
d751bb78 | 3515 | // Dump ast representation for a type conversion expression. |
3516 | ||
3517 | void | |
3518 | Type_conversion_expression::do_dump_expression( | |
3519 | Ast_dump_context* ast_dump_context) const | |
3520 | { | |
3521 | ast_dump_context->dump_type(this->type_); | |
3522 | ast_dump_context->ostream() << "("; | |
3523 | ast_dump_context->dump_expression(this->expr_); | |
3524 | ast_dump_context->ostream() << ") "; | |
3525 | } | |
3526 | ||
e440a328 | 3527 | // Make a type cast expression. |
3528 | ||
3529 | Expression* | |
b13c66cd | 3530 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3531 | { |
3532 | if (type->is_error_type() || val->is_error_expression()) | |
3533 | return Expression::make_error(location); | |
3534 | return new Type_conversion_expression(type, val, location); | |
3535 | } | |
3536 | ||
98f62f7a | 3537 | // Class Unsafe_type_conversion_expression. |
9581e91d | 3538 | |
3539 | // Traversal. | |
3540 | ||
3541 | int | |
3542 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3543 | { | |
3544 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3545 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3546 | return TRAVERSE_EXIT; | |
3547 | return TRAVERSE_CONTINUE; | |
3548 | } | |
3549 | ||
3ae06f68 | 3550 | // Return whether an unsafe type conversion can be used as a constant |
3551 | // initializer. | |
aa5ae575 | 3552 | |
3553 | bool | |
3ae06f68 | 3554 | Unsafe_type_conversion_expression::do_is_static_initializer() const |
aa5ae575 | 3555 | { |
3556 | Type* type = this->type_; | |
3557 | Type* expr_type = this->expr_->type(); | |
3558 | ||
3559 | if (type->interface_type() != NULL | |
3560 | || expr_type->interface_type() != NULL) | |
3561 | return false; | |
3562 | ||
3ae06f68 | 3563 | if (!this->expr_->is_static_initializer()) |
aa5ae575 | 3564 | return false; |
3565 | ||
3566 | if (Type::are_convertible(type, expr_type, NULL)) | |
3567 | return true; | |
3568 | ||
3569 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3570 | } | |
3571 | ||
9581e91d | 3572 | // Convert to backend representation. |
3573 | ||
ea664253 | 3574 | Bexpression* |
3575 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3576 | { |
3577 | // We are only called for a limited number of cases. | |
3578 | ||
3579 | Type* t = this->type_; | |
3580 | Type* et = this->expr_->type(); | |
5c4802f1 | 3581 | |
3582 | if (t->is_error_type() | |
3583 | || this->expr_->is_error_expression() | |
3584 | || et->is_error_type()) | |
3585 | { | |
3586 | go_assert(saw_errors()); | |
3587 | return context->backend()->error_expression(); | |
3588 | } | |
3589 | ||
2c809f8f | 3590 | if (t->array_type() != NULL) |
3591 | go_assert(et->array_type() != NULL | |
3592 | && t->is_slice_type() == et->is_slice_type()); | |
3593 | else if (t->struct_type() != NULL) | |
9581e91d | 3594 | { |
2c809f8f | 3595 | if (t->named_type() != NULL |
3596 | && et->named_type() != NULL | |
3597 | && !Type::are_convertible(t, et, NULL)) | |
3598 | { | |
3599 | go_assert(saw_errors()); | |
ea664253 | 3600 | return context->backend()->error_expression(); |
2c809f8f | 3601 | } |
3602 | ||
3603 | go_assert(et->struct_type() != NULL | |
3604 | && Type::are_convertible(t, et, NULL)); | |
3605 | } | |
3606 | else if (t->map_type() != NULL) | |
c484d925 | 3607 | go_assert(et->map_type() != NULL); |
9581e91d | 3608 | else if (t->channel_type() != NULL) |
c484d925 | 3609 | go_assert(et->channel_type() != NULL); |
09ea332d | 3610 | else if (t->points_to() != NULL) |
2c809f8f | 3611 | go_assert(et->points_to() != NULL |
3612 | || et->channel_type() != NULL | |
3613 | || et->map_type() != NULL | |
3614 | || et->function_type() != NULL | |
132ed071 | 3615 | || et->integer_type() != NULL |
2c809f8f | 3616 | || et->is_nil_type()); |
9581e91d | 3617 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3618 | go_assert(t->points_to() != NULL); |
2c809f8f | 3619 | else if (t->interface_type() != NULL) |
9581e91d | 3620 | { |
2c809f8f | 3621 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3622 | go_assert(et->interface_type() != NULL |
2c809f8f | 3623 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3624 | } |
588e3cf9 | 3625 | else if (t->integer_type() != NULL) |
2c809f8f | 3626 | go_assert(et->is_boolean_type() |
3627 | || et->integer_type() != NULL | |
3628 | || et->function_type() != NULL | |
3629 | || et->points_to() != NULL | |
3630 | || et->map_type() != NULL | |
8ba8cc87 | 3631 | || et->channel_type() != NULL |
3632 | || et->is_nil_type()); | |
cd39797e | 3633 | else if (t->function_type() != NULL) |
3634 | go_assert(et->points_to() != NULL); | |
9581e91d | 3635 | else |
c3e6f413 | 3636 | go_unreachable(); |
9581e91d | 3637 | |
2c809f8f | 3638 | Gogo* gogo = context->gogo(); |
3639 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3640 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3641 | Location loc = this->location(); |
ea664253 | 3642 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3643 | } |
3644 | ||
d751bb78 | 3645 | // Dump ast representation for an unsafe type conversion expression. |
3646 | ||
3647 | void | |
3648 | Unsafe_type_conversion_expression::do_dump_expression( | |
3649 | Ast_dump_context* ast_dump_context) const | |
3650 | { | |
3651 | ast_dump_context->dump_type(this->type_); | |
3652 | ast_dump_context->ostream() << "("; | |
3653 | ast_dump_context->dump_expression(this->expr_); | |
3654 | ast_dump_context->ostream() << ") "; | |
3655 | } | |
3656 | ||
9581e91d | 3657 | // Make an unsafe type conversion expression. |
3658 | ||
3659 | Expression* | |
3660 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3661 | Location location) |
9581e91d | 3662 | { |
3663 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3664 | } | |
3665 | ||
76f85fd6 | 3666 | // Class Unary_expression. |
e440a328 | 3667 | |
3668 | // If we are taking the address of a composite literal, and the | |
2c809f8f | 3669 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3670 | // instead. |
3671 | ||
3672 | Expression* | |
ceeb4318 | 3673 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3674 | { |
b13c66cd | 3675 | Location loc = this->location(); |
e440a328 | 3676 | Operator op = this->op_; |
3677 | Expression* expr = this->expr_; | |
3678 | ||
3679 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3680 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3681 | ||
3682 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3683 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3684 | // analysis here? This case is found in math/unsafe.go and is | |
3685 | // therefore worth special casing. | |
3686 | if (op == OPERATOR_MULT) | |
3687 | { | |
3688 | Expression* e = expr; | |
3689 | while (e->classification() == EXPRESSION_CONVERSION) | |
3690 | { | |
3691 | Type_conversion_expression* te | |
3692 | = static_cast<Type_conversion_expression*>(e); | |
3693 | e = te->expr(); | |
3694 | } | |
3695 | ||
3696 | if (e->classification() == EXPRESSION_UNARY) | |
3697 | { | |
3698 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3699 | if (ue->op_ == OPERATOR_AND) | |
3700 | { | |
3701 | if (e == expr) | |
3702 | { | |
3703 | // *&x == x. | |
f4dea966 | 3704 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3705 | { | |
631d5788 | 3706 | go_error_at(ue->location(), |
3707 | "invalid operand for unary %<&%>"); | |
f4dea966 | 3708 | this->set_is_error(); |
3709 | } | |
e440a328 | 3710 | return ue->expr_; |
3711 | } | |
3712 | ue->set_does_not_escape(); | |
3713 | } | |
3714 | } | |
3715 | } | |
3716 | ||
55661ce9 | 3717 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3718 | // having to deal with TYPE_VOID in other places. | |
3719 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3720 | { | |
631d5788 | 3721 | go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); |
55661ce9 | 3722 | return Expression::make_error(this->location()); |
3723 | } | |
3724 | ||
d9f3743a | 3725 | // Check for an invalid pointer dereference. We need to do this |
3726 | // here because Unary_expression::do_type will return an error type | |
3727 | // in this case. That can cause code to appear erroneous, and | |
3728 | // therefore disappear at lowering time, without any error message. | |
3729 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3730 | { | |
3731 | this->report_error(_("expected pointer")); | |
3732 | return Expression::make_error(this->location()); | |
3733 | } | |
3734 | ||
59a401fe | 3735 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3736 | { |
0c77715b | 3737 | Numeric_constant nc; |
3738 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3739 | { |
0c77715b | 3740 | Numeric_constant result; |
3741 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3742 | return result.expression(loc); | |
e440a328 | 3743 | } |
3744 | } | |
3745 | ||
3746 | return this; | |
3747 | } | |
3748 | ||
f9ca30f9 | 3749 | // Flatten expression if a nil check must be performed and create temporary |
3750 | // variables if necessary. | |
3751 | ||
3752 | Expression* | |
3753 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3754 | Statement_inserter* inserter) | |
3755 | { | |
5bf8be8b | 3756 | if (this->is_error_expression() |
3757 | || this->expr_->is_error_expression() | |
3758 | || this->expr_->type()->is_error_type()) | |
3759 | { | |
3760 | go_assert(saw_errors()); | |
3761 | return Expression::make_error(this->location()); | |
3762 | } | |
f4dea966 | 3763 | |
f9ca30f9 | 3764 | Location location = this->location(); |
3765 | if (this->op_ == OPERATOR_MULT | |
3766 | && !this->expr_->is_variable()) | |
3767 | { | |
3768 | go_assert(this->expr_->type()->points_to() != NULL); | |
3769 | Type* ptype = this->expr_->type()->points_to(); | |
3770 | if (!ptype->is_void_type()) | |
3771 | { | |
2a305b85 | 3772 | int64_t s; |
3773 | bool ok = ptype->backend_type_size(gogo, &s); | |
3774 | if (!ok) | |
3775 | { | |
3776 | go_assert(saw_errors()); | |
3777 | return Expression::make_error(this->location()); | |
3778 | } | |
f9ca30f9 | 3779 | if (s >= 4096 || this->issue_nil_check_) |
3780 | { | |
3781 | Temporary_statement* temp = | |
3782 | Statement::make_temporary(NULL, this->expr_, location); | |
3783 | inserter->insert(temp); | |
3784 | this->expr_ = | |
3785 | Expression::make_temporary_reference(temp, location); | |
3786 | } | |
3787 | } | |
3788 | } | |
3789 | ||
da244e59 | 3790 | if (this->op_ == OPERATOR_AND) |
3791 | { | |
8ff995b5 | 3792 | // If this->escapes_ is false at this point, then it was set to |
3793 | // false by an explicit call to set_does_not_escape, and the | |
3794 | // value does not escape. If this->escapes_ is true, we may be | |
3795 | // able to set it to false if taking the address of a variable | |
3796 | // that does not escape. | |
45ff893b | 3797 | Node* n = Node::make_node(this); |
3798 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
3799 | this->escapes_ = false; | |
3800 | ||
0dad6de4 | 3801 | // When compiling the runtime, the address operator does not |
87211034 | 3802 | // cause local variables to escape. When escape analysis |
0dad6de4 | 3803 | // becomes the default, this should be changed to make it an |
3804 | // error if we have an address operator that escapes. | |
3805 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
3806 | this->escapes_ = false; | |
3807 | ||
45ff893b | 3808 | Named_object* var = NULL; |
3809 | if (this->expr_->var_expression() != NULL) | |
3810 | var = this->expr_->var_expression()->named_object(); | |
3811 | else if (this->expr_->enclosed_var_expression() != NULL) | |
3812 | var = this->expr_->enclosed_var_expression()->variable(); | |
3813 | ||
3814 | if (this->escapes_ && var != NULL) | |
da244e59 | 3815 | { |
da244e59 | 3816 | if (var->is_variable()) |
3817 | this->escapes_ = var->var_value()->escapes(); | |
3818 | if (var->is_result_variable()) | |
3819 | this->escapes_ = var->result_var_value()->escapes(); | |
3820 | } | |
3821 | this->expr_->address_taken(this->escapes_); | |
3822 | } | |
3823 | ||
f9ca30f9 | 3824 | if (this->create_temp_ && !this->expr_->is_variable()) |
3825 | { | |
3826 | Temporary_statement* temp = | |
3827 | Statement::make_temporary(NULL, this->expr_, location); | |
3828 | inserter->insert(temp); | |
3829 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3830 | } | |
3831 | ||
3832 | return this; | |
3833 | } | |
3834 | ||
e440a328 | 3835 | // Return whether a unary expression is a constant. |
3836 | ||
3837 | bool | |
3838 | Unary_expression::do_is_constant() const | |
3839 | { | |
3840 | if (this->op_ == OPERATOR_MULT) | |
3841 | { | |
3842 | // Indirecting through a pointer is only constant if the object | |
3843 | // to which the expression points is constant, but we currently | |
3844 | // have no way to determine that. | |
3845 | return false; | |
3846 | } | |
3847 | else if (this->op_ == OPERATOR_AND) | |
3848 | { | |
3849 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3850 | // global variable, not constant otherwise. In other cases taking the |
3851 | // address is probably not a constant. | |
e440a328 | 3852 | Var_expression* ve = this->expr_->var_expression(); |
3853 | if (ve != NULL) | |
3854 | { | |
3855 | Named_object* no = ve->named_object(); | |
3856 | return no->is_variable() && no->var_value()->is_global(); | |
3857 | } | |
3858 | return false; | |
3859 | } | |
3860 | else | |
3861 | return this->expr_->is_constant(); | |
3862 | } | |
3863 | ||
3ae06f68 | 3864 | // Return whether a unary expression can be used as a constant |
3865 | // initializer. | |
3866 | ||
3867 | bool | |
3868 | Unary_expression::do_is_static_initializer() const | |
3869 | { | |
3870 | if (this->op_ == OPERATOR_MULT) | |
3871 | return false; | |
3872 | else if (this->op_ == OPERATOR_AND) | |
3873 | { | |
3874 | // The address of a global variable can used as a static | |
3875 | // initializer. | |
3876 | Var_expression* ve = this->expr_->var_expression(); | |
3877 | if (ve != NULL) | |
3878 | { | |
3879 | Named_object* no = ve->named_object(); | |
3880 | return no->is_variable() && no->var_value()->is_global(); | |
3881 | } | |
3882 | ||
3883 | // The address of a composite literal can be used as a static | |
3884 | // initializer if the composite literal is itself usable as a | |
3885 | // static initializer. | |
3886 | if (this->expr_->is_composite_literal() | |
3887 | && this->expr_->is_static_initializer()) | |
3888 | return true; | |
3889 | ||
3890 | // The address of a string constant can be used as a static | |
3891 | // initializer. This can not be written in Go itself but this | |
3892 | // is used when building a type descriptor. | |
3893 | if (this->expr_->string_expression() != NULL) | |
3894 | return true; | |
3895 | ||
3896 | return false; | |
3897 | } | |
3898 | else | |
3899 | return this->expr_->is_static_initializer(); | |
3900 | } | |
3901 | ||
0c77715b | 3902 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3903 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3904 | |
3905 | bool | |
0c77715b | 3906 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3907 | Location location, Numeric_constant* nc) | |
e440a328 | 3908 | { |
3909 | switch (op) | |
3910 | { | |
3911 | case OPERATOR_PLUS: | |
0c77715b | 3912 | *nc = *unc; |
e440a328 | 3913 | return true; |
0c77715b | 3914 | |
e440a328 | 3915 | case OPERATOR_MINUS: |
0c77715b | 3916 | if (unc->is_int() || unc->is_rune()) |
3917 | break; | |
3918 | else if (unc->is_float()) | |
3919 | { | |
3920 | mpfr_t uval; | |
3921 | unc->get_float(&uval); | |
3922 | mpfr_t val; | |
3923 | mpfr_init(val); | |
3924 | mpfr_neg(val, uval, GMP_RNDN); | |
3925 | nc->set_float(unc->type(), val); | |
3926 | mpfr_clear(uval); | |
3927 | mpfr_clear(val); | |
3928 | return true; | |
3929 | } | |
3930 | else if (unc->is_complex()) | |
3931 | { | |
fcbea5e4 | 3932 | mpc_t uval; |
3933 | unc->get_complex(&uval); | |
3934 | mpc_t val; | |
3935 | mpc_init2(val, mpc_precision); | |
3936 | mpc_neg(val, uval, MPC_RNDNN); | |
3937 | nc->set_complex(unc->type(), val); | |
3938 | mpc_clear(uval); | |
3939 | mpc_clear(val); | |
0c77715b | 3940 | return true; |
3941 | } | |
e440a328 | 3942 | else |
0c77715b | 3943 | go_unreachable(); |
e440a328 | 3944 | |
0c77715b | 3945 | case OPERATOR_XOR: |
3946 | break; | |
68448d53 | 3947 | |
59a401fe | 3948 | case OPERATOR_NOT: |
e440a328 | 3949 | case OPERATOR_AND: |
3950 | case OPERATOR_MULT: | |
3951 | return false; | |
0c77715b | 3952 | |
e440a328 | 3953 | default: |
c3e6f413 | 3954 | go_unreachable(); |
e440a328 | 3955 | } |
e440a328 | 3956 | |
0c77715b | 3957 | if (!unc->is_int() && !unc->is_rune()) |
3958 | return false; | |
3959 | ||
3960 | mpz_t uval; | |
8387e1df | 3961 | if (unc->is_rune()) |
3962 | unc->get_rune(&uval); | |
3963 | else | |
3964 | unc->get_int(&uval); | |
0c77715b | 3965 | mpz_t val; |
3966 | mpz_init(val); | |
e440a328 | 3967 | |
e440a328 | 3968 | switch (op) |
3969 | { | |
e440a328 | 3970 | case OPERATOR_MINUS: |
0c77715b | 3971 | mpz_neg(val, uval); |
3972 | break; | |
3973 | ||
e440a328 | 3974 | case OPERATOR_NOT: |
0c77715b | 3975 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3976 | break; | |
3977 | ||
e440a328 | 3978 | case OPERATOR_XOR: |
0c77715b | 3979 | { |
3980 | Type* utype = unc->type(); | |
3981 | if (utype->integer_type() == NULL | |
3982 | || utype->integer_type()->is_abstract()) | |
3983 | mpz_com(val, uval); | |
3984 | else | |
3985 | { | |
3986 | // The number of HOST_WIDE_INTs that it takes to represent | |
3987 | // UVAL. | |
3988 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3989 | + HOST_BITS_PER_WIDE_INT | |
3990 | - 1) | |
3991 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3992 | |
0c77715b | 3993 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3994 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3995 | ||
3996 | size_t obits = utype->integer_type()->bits(); | |
3997 | ||
3998 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3999 | { | |
4000 | mpz_t adj; | |
4001 | mpz_init_set_ui(adj, 1); | |
4002 | mpz_mul_2exp(adj, adj, obits); | |
4003 | mpz_add(uval, uval, adj); | |
4004 | mpz_clear(adj); | |
4005 | } | |
4006 | ||
4007 | size_t ecount; | |
4008 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
4009 | go_assert(ecount <= count); | |
4010 | ||
4011 | // Trim down to the number of words required by the type. | |
4012 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
4013 | / HOST_BITS_PER_WIDE_INT); | |
4014 | go_assert(ocount <= count); | |
4015 | ||
4016 | for (size_t i = 0; i < ocount; ++i) | |
4017 | phwi[i] = ~phwi[i]; | |
4018 | ||
4019 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
4020 | if (clearbits != 0) | |
4021 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
4022 | >> clearbits); | |
4023 | ||
4024 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
4025 | ||
4026 | if (!utype->integer_type()->is_unsigned() | |
4027 | && mpz_tstbit(val, obits - 1)) | |
4028 | { | |
4029 | mpz_t adj; | |
4030 | mpz_init_set_ui(adj, 1); | |
4031 | mpz_mul_2exp(adj, adj, obits); | |
4032 | mpz_sub(val, val, adj); | |
4033 | mpz_clear(adj); | |
4034 | } | |
4035 | ||
4036 | delete[] phwi; | |
4037 | } | |
4038 | } | |
4039 | break; | |
e440a328 | 4040 | |
e440a328 | 4041 | default: |
c3e6f413 | 4042 | go_unreachable(); |
e440a328 | 4043 | } |
e440a328 | 4044 | |
0c77715b | 4045 | if (unc->is_rune()) |
4046 | nc->set_rune(NULL, val); | |
e440a328 | 4047 | else |
0c77715b | 4048 | nc->set_int(NULL, val); |
e440a328 | 4049 | |
0c77715b | 4050 | mpz_clear(uval); |
4051 | mpz_clear(val); | |
e440a328 | 4052 | |
0c77715b | 4053 | return nc->set_type(unc->type(), true, location); |
e440a328 | 4054 | } |
4055 | ||
0c77715b | 4056 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 4057 | |
4058 | bool | |
0c77715b | 4059 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 4060 | { |
0c77715b | 4061 | Numeric_constant unc; |
4062 | if (!this->expr_->numeric_constant_value(&unc)) | |
4063 | return false; | |
4064 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
4065 | nc); | |
e440a328 | 4066 | } |
4067 | ||
4068 | // Return the type of a unary expression. | |
4069 | ||
4070 | Type* | |
4071 | Unary_expression::do_type() | |
4072 | { | |
4073 | switch (this->op_) | |
4074 | { | |
4075 | case OPERATOR_PLUS: | |
4076 | case OPERATOR_MINUS: | |
4077 | case OPERATOR_NOT: | |
4078 | case OPERATOR_XOR: | |
4079 | return this->expr_->type(); | |
4080 | ||
4081 | case OPERATOR_AND: | |
4082 | return Type::make_pointer_type(this->expr_->type()); | |
4083 | ||
4084 | case OPERATOR_MULT: | |
4085 | { | |
4086 | Type* subtype = this->expr_->type(); | |
4087 | Type* points_to = subtype->points_to(); | |
4088 | if (points_to == NULL) | |
4089 | return Type::make_error_type(); | |
4090 | return points_to; | |
4091 | } | |
4092 | ||
4093 | default: | |
c3e6f413 | 4094 | go_unreachable(); |
e440a328 | 4095 | } |
4096 | } | |
4097 | ||
4098 | // Determine abstract types for a unary expression. | |
4099 | ||
4100 | void | |
4101 | Unary_expression::do_determine_type(const Type_context* context) | |
4102 | { | |
4103 | switch (this->op_) | |
4104 | { | |
4105 | case OPERATOR_PLUS: | |
4106 | case OPERATOR_MINUS: | |
4107 | case OPERATOR_NOT: | |
4108 | case OPERATOR_XOR: | |
4109 | this->expr_->determine_type(context); | |
4110 | break; | |
4111 | ||
4112 | case OPERATOR_AND: | |
4113 | // Taking the address of something. | |
4114 | { | |
4115 | Type* subtype = (context->type == NULL | |
4116 | ? NULL | |
4117 | : context->type->points_to()); | |
4118 | Type_context subcontext(subtype, false); | |
4119 | this->expr_->determine_type(&subcontext); | |
4120 | } | |
4121 | break; | |
4122 | ||
4123 | case OPERATOR_MULT: | |
4124 | // Indirecting through a pointer. | |
4125 | { | |
4126 | Type* subtype = (context->type == NULL | |
4127 | ? NULL | |
4128 | : Type::make_pointer_type(context->type)); | |
4129 | Type_context subcontext(subtype, false); | |
4130 | this->expr_->determine_type(&subcontext); | |
4131 | } | |
4132 | break; | |
4133 | ||
4134 | default: | |
c3e6f413 | 4135 | go_unreachable(); |
e440a328 | 4136 | } |
4137 | } | |
4138 | ||
4139 | // Check types for a unary expression. | |
4140 | ||
4141 | void | |
4142 | Unary_expression::do_check_types(Gogo*) | |
4143 | { | |
9fe897ef | 4144 | Type* type = this->expr_->type(); |
5c13bd80 | 4145 | if (type->is_error()) |
9fe897ef | 4146 | { |
4147 | this->set_is_error(); | |
4148 | return; | |
4149 | } | |
4150 | ||
e440a328 | 4151 | switch (this->op_) |
4152 | { | |
4153 | case OPERATOR_PLUS: | |
4154 | case OPERATOR_MINUS: | |
9fe897ef | 4155 | if (type->integer_type() == NULL |
4156 | && type->float_type() == NULL | |
4157 | && type->complex_type() == NULL) | |
4158 | this->report_error(_("expected numeric type")); | |
e440a328 | 4159 | break; |
4160 | ||
4161 | case OPERATOR_NOT: | |
59a401fe | 4162 | if (!type->is_boolean_type()) |
4163 | this->report_error(_("expected boolean type")); | |
4164 | break; | |
4165 | ||
e440a328 | 4166 | case OPERATOR_XOR: |
b3b1474e | 4167 | if (type->integer_type() == NULL) |
4168 | this->report_error(_("expected integer")); | |
e440a328 | 4169 | break; |
4170 | ||
4171 | case OPERATOR_AND: | |
4172 | if (!this->expr_->is_addressable()) | |
09ea332d | 4173 | { |
4174 | if (!this->create_temp_) | |
f4dea966 | 4175 | { |
631d5788 | 4176 | go_error_at(this->location(), "invalid operand for unary %<&%>"); |
f4dea966 | 4177 | this->set_is_error(); |
4178 | } | |
09ea332d | 4179 | } |
e440a328 | 4180 | else |
da244e59 | 4181 | this->expr_->issue_nil_check(); |
e440a328 | 4182 | break; |
4183 | ||
4184 | case OPERATOR_MULT: | |
4185 | // Indirecting through a pointer. | |
9fe897ef | 4186 | if (type->points_to() == NULL) |
4187 | this->report_error(_("expected pointer")); | |
7661d702 | 4188 | if (type->points_to()->is_error()) |
4189 | this->set_is_error(); | |
e440a328 | 4190 | break; |
4191 | ||
4192 | default: | |
c3e6f413 | 4193 | go_unreachable(); |
e440a328 | 4194 | } |
4195 | } | |
4196 | ||
ea664253 | 4197 | // Get the backend representation for a unary expression. |
e440a328 | 4198 | |
ea664253 | 4199 | Bexpression* |
4200 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4201 | { |
1b1f2abf | 4202 | Gogo* gogo = context->gogo(); |
e9d3367e | 4203 | Location loc = this->location(); |
4204 | ||
4205 | // Taking the address of a set-and-use-temporary expression requires | |
4206 | // setting the temporary and then taking the address. | |
4207 | if (this->op_ == OPERATOR_AND) | |
4208 | { | |
4209 | Set_and_use_temporary_expression* sut = | |
4210 | this->expr_->set_and_use_temporary_expression(); | |
4211 | if (sut != NULL) | |
4212 | { | |
4213 | Temporary_statement* temp = sut->temporary(); | |
4214 | Bvariable* bvar = temp->get_backend_variable(context); | |
d4e6573e | 4215 | Bexpression* bvar_expr = |
4216 | gogo->backend()->var_expression(bvar, VE_lvalue, loc); | |
ea664253 | 4217 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4218 | |
4219 | Bstatement* bassign = | |
4220 | gogo->backend()->assignment_statement(bvar_expr, bval, loc); | |
4221 | Bexpression* bvar_addr = | |
4222 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4223 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4224 | } |
4225 | } | |
4226 | ||
f9ca30f9 | 4227 | Bexpression* ret; |
ea664253 | 4228 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4229 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4230 | switch (this->op_) |
4231 | { | |
4232 | case OPERATOR_PLUS: | |
f9ca30f9 | 4233 | ret = bexpr; |
4234 | break; | |
e440a328 | 4235 | |
4236 | case OPERATOR_MINUS: | |
f9ca30f9 | 4237 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4238 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4239 | break; | |
e440a328 | 4240 | |
4241 | case OPERATOR_NOT: | |
e440a328 | 4242 | case OPERATOR_XOR: |
f9ca30f9 | 4243 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4244 | break; | |
e440a328 | 4245 | |
4246 | case OPERATOR_AND: | |
09ea332d | 4247 | if (!this->create_temp_) |
4248 | { | |
4249 | // We should not see a non-constant constructor here; cases | |
4250 | // where we would see one should have been moved onto the | |
4251 | // heap at parse time. Taking the address of a nonconstant | |
4252 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4253 | |
4254 | go_assert(!this->expr_->is_composite_literal() | |
3ae06f68 | 4255 | || this->expr_->is_static_initializer()); |
24060bf9 | 4256 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4257 | { | |
4258 | Unary_expression* ue = | |
4259 | static_cast<Unary_expression*>(this->expr_); | |
4260 | go_assert(ue->op() != OPERATOR_AND); | |
4261 | } | |
09ea332d | 4262 | } |
e440a328 | 4263 | |
f23d7786 | 4264 | static unsigned int counter; |
4265 | char buf[100]; | |
4266 | if (this->is_gc_root_ || this->is_slice_init_) | |
76f85fd6 | 4267 | { |
f23d7786 | 4268 | bool copy_to_heap = false; |
4269 | if (this->is_gc_root_) | |
4270 | { | |
4271 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4272 | // they cannot be represented as an immutable_struct in the | |
4273 | // backend. | |
4274 | static unsigned int root_counter; | |
4275 | snprintf(buf, sizeof buf, "gc%u", root_counter); | |
4276 | ++root_counter; | |
4277 | } | |
4278 | else | |
4279 | { | |
4280 | // Build a decl for a slice value initializer. An immutable slice | |
4281 | // value initializer may have to be copied to the heap if it | |
4282 | // contains pointers in a non-constant context. | |
4283 | snprintf(buf, sizeof buf, "C%u", counter); | |
4284 | ++counter; | |
4285 | ||
4286 | Array_type* at = this->expr_->type()->array_type(); | |
4287 | go_assert(at != NULL); | |
4288 | ||
4289 | // If we are not copying the value to the heap, we will only | |
4290 | // initialize the value once, so we can use this directly | |
4291 | // rather than copying it. In that case we can't make it | |
4292 | // read-only, because the program is permitted to change it. | |
3ae06f68 | 4293 | copy_to_heap = context->function() != NULL; |
f23d7786 | 4294 | } |
438b4bec | 4295 | std::string asm_name(go_selectively_encode_id(buf)); |
f23d7786 | 4296 | Bvariable* implicit = |
438b4bec | 4297 | gogo->backend()->implicit_variable(buf, asm_name, |
4298 | btype, true, copy_to_heap, | |
4299 | false, 0); | |
aa5ae575 | 4300 | gogo->backend()->implicit_variable_set_init(implicit, buf, btype, |
4301 | true, copy_to_heap, false, | |
4302 | bexpr); | |
d4e6573e | 4303 | bexpr = gogo->backend()->var_expression(implicit, VE_lvalue, loc); |
1b4fb1e0 | 4304 | |
4305 | // If we are not copying a slice initializer to the heap, | |
4306 | // then it can be changed by the program, so if it can | |
4307 | // contain pointers we must register it as a GC root. | |
4308 | if (this->is_slice_init_ | |
4309 | && !copy_to_heap | |
4310 | && this->expr_->type()->has_pointer()) | |
4311 | { | |
4312 | Bexpression* root = | |
d4e6573e | 4313 | gogo->backend()->var_expression(implicit, VE_lvalue, loc); |
1b4fb1e0 | 4314 | root = gogo->backend()->address_expression(root, loc); |
4315 | Type* type = Type::make_pointer_type(this->expr_->type()); | |
4316 | gogo->add_gc_root(Expression::make_backend(root, type, loc)); | |
4317 | } | |
76f85fd6 | 4318 | } |
4319 | else if ((this->expr_->is_composite_literal() | |
3ae06f68 | 4320 | || this->expr_->string_expression() != NULL) |
4321 | && this->expr_->is_static_initializer()) | |
f9ca30f9 | 4322 | { |
76f85fd6 | 4323 | // Build a decl for a constant constructor. |
f9ca30f9 | 4324 | snprintf(buf, sizeof buf, "C%u", counter); |
4325 | ++counter; | |
4326 | ||
438b4bec | 4327 | std::string asm_name(go_selectively_encode_id(buf)); |
f9ca30f9 | 4328 | Bvariable* decl = |
438b4bec | 4329 | gogo->backend()->immutable_struct(buf, asm_name, |
4330 | true, false, btype, loc); | |
f9ca30f9 | 4331 | gogo->backend()->immutable_struct_set_init(decl, buf, true, false, |
4332 | btype, loc, bexpr); | |
d4e6573e | 4333 | bexpr = gogo->backend()->var_expression(decl, VE_lvalue, loc); |
f9ca30f9 | 4334 | } |
09ea332d | 4335 | |
f9ca30f9 | 4336 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4337 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4338 | break; | |
e440a328 | 4339 | |
4340 | case OPERATOR_MULT: | |
4341 | { | |
f9ca30f9 | 4342 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4343 | |
4344 | // If we are dereferencing the pointer to a large struct, we | |
4345 | // need to check for nil. We don't bother to check for small | |
4346 | // structs because we expect the system to crash on a nil | |
56080003 | 4347 | // pointer dereference. However, if we know the address of this |
4348 | // expression is being taken, we must always check for nil. | |
f9ca30f9 | 4349 | |
4350 | Type* ptype = this->expr_->type()->points_to(); | |
4351 | Btype* pbtype = ptype->get_backend(gogo); | |
4352 | if (!ptype->is_void_type()) | |
e440a328 | 4353 | { |
2a305b85 | 4354 | int64_t s; |
4355 | bool ok = ptype->backend_type_size(gogo, &s); | |
4356 | if (!ok) | |
4357 | { | |
4358 | go_assert(saw_errors()); | |
4359 | return gogo->backend()->error_expression(); | |
4360 | } | |
f9ca30f9 | 4361 | if (s >= 4096 || this->issue_nil_check_) |
19b4f09b | 4362 | { |
f9ca30f9 | 4363 | go_assert(this->expr_->is_variable()); |
ea664253 | 4364 | Bexpression* nil = |
4365 | Expression::make_nil(loc)->get_backend(context); | |
f9ca30f9 | 4366 | Bexpression* compare = |
4367 | gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr, | |
4368 | nil, loc); | |
f9ca30f9 | 4369 | Bexpression* crash = |
ea664253 | 4370 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4371 | loc)->get_backend(context); | |
f9ca30f9 | 4372 | bexpr = gogo->backend()->conditional_expression(btype, compare, |
4373 | crash, bexpr, | |
4374 | loc); | |
4375 | ||
19b4f09b | 4376 | } |
e440a328 | 4377 | } |
9b27b43c | 4378 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); |
e440a328 | 4379 | } |
f9ca30f9 | 4380 | break; |
e440a328 | 4381 | |
4382 | default: | |
c3e6f413 | 4383 | go_unreachable(); |
e440a328 | 4384 | } |
f9ca30f9 | 4385 | |
ea664253 | 4386 | return ret; |
e440a328 | 4387 | } |
4388 | ||
4389 | // Export a unary expression. | |
4390 | ||
4391 | void | |
4392 | Unary_expression::do_export(Export* exp) const | |
4393 | { | |
4394 | switch (this->op_) | |
4395 | { | |
4396 | case OPERATOR_PLUS: | |
4397 | exp->write_c_string("+ "); | |
4398 | break; | |
4399 | case OPERATOR_MINUS: | |
4400 | exp->write_c_string("- "); | |
4401 | break; | |
4402 | case OPERATOR_NOT: | |
4403 | exp->write_c_string("! "); | |
4404 | break; | |
4405 | case OPERATOR_XOR: | |
4406 | exp->write_c_string("^ "); | |
4407 | break; | |
4408 | case OPERATOR_AND: | |
4409 | case OPERATOR_MULT: | |
4410 | default: | |
c3e6f413 | 4411 | go_unreachable(); |
e440a328 | 4412 | } |
4413 | this->expr_->export_expression(exp); | |
4414 | } | |
4415 | ||
4416 | // Import a unary expression. | |
4417 | ||
4418 | Expression* | |
4419 | Unary_expression::do_import(Import* imp) | |
4420 | { | |
4421 | Operator op; | |
4422 | switch (imp->get_char()) | |
4423 | { | |
4424 | case '+': | |
4425 | op = OPERATOR_PLUS; | |
4426 | break; | |
4427 | case '-': | |
4428 | op = OPERATOR_MINUS; | |
4429 | break; | |
4430 | case '!': | |
4431 | op = OPERATOR_NOT; | |
4432 | break; | |
4433 | case '^': | |
4434 | op = OPERATOR_XOR; | |
4435 | break; | |
4436 | default: | |
c3e6f413 | 4437 | go_unreachable(); |
e440a328 | 4438 | } |
4439 | imp->require_c_string(" "); | |
4440 | Expression* expr = Expression::import_expression(imp); | |
4441 | return Expression::make_unary(op, expr, imp->location()); | |
4442 | } | |
4443 | ||
d751bb78 | 4444 | // Dump ast representation of an unary expression. |
4445 | ||
4446 | void | |
4447 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4448 | { | |
4449 | ast_dump_context->dump_operator(this->op_); | |
4450 | ast_dump_context->ostream() << "("; | |
4451 | ast_dump_context->dump_expression(this->expr_); | |
4452 | ast_dump_context->ostream() << ") "; | |
4453 | } | |
4454 | ||
e440a328 | 4455 | // Make a unary expression. |
4456 | ||
4457 | Expression* | |
b13c66cd | 4458 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4459 | { |
4460 | return new Unary_expression(op, expr, location); | |
4461 | } | |
4462 | ||
4463 | // If this is an indirection through a pointer, return the expression | |
4464 | // being pointed through. Otherwise return this. | |
4465 | ||
4466 | Expression* | |
4467 | Expression::deref() | |
4468 | { | |
4469 | if (this->classification_ == EXPRESSION_UNARY) | |
4470 | { | |
4471 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4472 | if (ue->op() == OPERATOR_MULT) | |
4473 | return ue->operand(); | |
4474 | } | |
4475 | return this; | |
4476 | } | |
4477 | ||
4478 | // Class Binary_expression. | |
4479 | ||
4480 | // Traversal. | |
4481 | ||
4482 | int | |
4483 | Binary_expression::do_traverse(Traverse* traverse) | |
4484 | { | |
4485 | int t = Expression::traverse(&this->left_, traverse); | |
4486 | if (t == TRAVERSE_EXIT) | |
4487 | return TRAVERSE_EXIT; | |
4488 | return Expression::traverse(&this->right_, traverse); | |
4489 | } | |
4490 | ||
3ae06f68 | 4491 | // Return whether this expression may be used as a static initializer. |
4492 | ||
4493 | bool | |
4494 | Binary_expression::do_is_static_initializer() const | |
4495 | { | |
4496 | if (!this->left_->is_static_initializer() | |
4497 | || !this->right_->is_static_initializer()) | |
4498 | return false; | |
4499 | ||
4500 | // Addresses can be static initializers, but we can't implement | |
4501 | // arbitray binary expressions of them. | |
4502 | Unary_expression* lu = this->left_->unary_expression(); | |
4503 | Unary_expression* ru = this->right_->unary_expression(); | |
4504 | if (lu != NULL && lu->op() == OPERATOR_AND) | |
4505 | { | |
4506 | if (ru != NULL && ru->op() == OPERATOR_AND) | |
4507 | return this->op_ == OPERATOR_MINUS; | |
4508 | else | |
4509 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4510 | } | |
4511 | else if (ru != NULL && ru->op() == OPERATOR_AND) | |
4512 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4513 | ||
4514 | // Other cases should resolve in the backend. | |
4515 | return true; | |
4516 | } | |
4517 | ||
0c77715b | 4518 | // Return the type to use for a binary operation on operands of |
4519 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4520 | // such may be NULL or abstract. | |
4521 | ||
4522 | bool | |
4523 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4524 | Type* right_type, Type** result_type) | |
4525 | { | |
4526 | if (left_type != right_type | |
4527 | && !left_type->is_abstract() | |
4528 | && !right_type->is_abstract() | |
4529 | && left_type->base() != right_type->base() | |
4530 | && op != OPERATOR_LSHIFT | |
4531 | && op != OPERATOR_RSHIFT) | |
4532 | { | |
4533 | // May be a type error--let it be diagnosed elsewhere. | |
4534 | return false; | |
4535 | } | |
4536 | ||
4537 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4538 | { | |
4539 | if (left_type->integer_type() != NULL) | |
4540 | *result_type = left_type; | |
4541 | else | |
4542 | *result_type = Type::make_abstract_integer_type(); | |
4543 | } | |
4544 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4545 | *result_type = left_type; | |
4546 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4547 | *result_type = right_type; | |
4548 | else if (!left_type->is_abstract()) | |
4549 | *result_type = left_type; | |
4550 | else if (!right_type->is_abstract()) | |
4551 | *result_type = right_type; | |
4552 | else if (left_type->complex_type() != NULL) | |
4553 | *result_type = left_type; | |
4554 | else if (right_type->complex_type() != NULL) | |
4555 | *result_type = right_type; | |
4556 | else if (left_type->float_type() != NULL) | |
4557 | *result_type = left_type; | |
4558 | else if (right_type->float_type() != NULL) | |
4559 | *result_type = right_type; | |
4560 | else if (left_type->integer_type() != NULL | |
4561 | && left_type->integer_type()->is_rune()) | |
4562 | *result_type = left_type; | |
4563 | else if (right_type->integer_type() != NULL | |
4564 | && right_type->integer_type()->is_rune()) | |
4565 | *result_type = right_type; | |
4566 | else | |
4567 | *result_type = left_type; | |
4568 | ||
4569 | return true; | |
4570 | } | |
4571 | ||
4572 | // Convert an integer comparison code and an operator to a boolean | |
4573 | // value. | |
e440a328 | 4574 | |
4575 | bool | |
0c77715b | 4576 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4577 | { |
e440a328 | 4578 | switch (op) |
4579 | { | |
4580 | case OPERATOR_EQEQ: | |
0c77715b | 4581 | return cmp == 0; |
4582 | break; | |
e440a328 | 4583 | case OPERATOR_NOTEQ: |
0c77715b | 4584 | return cmp != 0; |
4585 | break; | |
e440a328 | 4586 | case OPERATOR_LT: |
0c77715b | 4587 | return cmp < 0; |
4588 | break; | |
e440a328 | 4589 | case OPERATOR_LE: |
0c77715b | 4590 | return cmp <= 0; |
e440a328 | 4591 | case OPERATOR_GT: |
0c77715b | 4592 | return cmp > 0; |
e440a328 | 4593 | case OPERATOR_GE: |
0c77715b | 4594 | return cmp >= 0; |
e440a328 | 4595 | default: |
c3e6f413 | 4596 | go_unreachable(); |
e440a328 | 4597 | } |
4598 | } | |
4599 | ||
0c77715b | 4600 | // Compare constants according to OP. |
e440a328 | 4601 | |
4602 | bool | |
0c77715b | 4603 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4604 | Numeric_constant* right_nc, | |
4605 | Location location, bool* result) | |
e440a328 | 4606 | { |
0c77715b | 4607 | Type* left_type = left_nc->type(); |
4608 | Type* right_type = right_nc->type(); | |
4609 | ||
4610 | Type* type; | |
4611 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4612 | return false; | |
4613 | ||
4614 | // When comparing an untyped operand to a typed operand, we are | |
4615 | // effectively coercing the untyped operand to the other operand's | |
4616 | // type, so make sure that is valid. | |
4617 | if (!left_nc->set_type(type, true, location) | |
4618 | || !right_nc->set_type(type, true, location)) | |
4619 | return false; | |
4620 | ||
4621 | bool ret; | |
4622 | int cmp; | |
4623 | if (type->complex_type() != NULL) | |
4624 | { | |
4625 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4626 | return false; | |
4627 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4628 | } | |
4629 | else if (type->float_type() != NULL) | |
4630 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4631 | else |
0c77715b | 4632 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4633 | ||
4634 | if (ret) | |
4635 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4636 | ||
4637 | return ret; | |
4638 | } | |
4639 | ||
4640 | // Compare integer constants. | |
4641 | ||
4642 | bool | |
4643 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4644 | const Numeric_constant* right_nc, | |
4645 | int* cmp) | |
4646 | { | |
4647 | mpz_t left_val; | |
4648 | if (!left_nc->to_int(&left_val)) | |
4649 | return false; | |
4650 | mpz_t right_val; | |
4651 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4652 | { |
0c77715b | 4653 | mpz_clear(left_val); |
4654 | return false; | |
e440a328 | 4655 | } |
0c77715b | 4656 | |
4657 | *cmp = mpz_cmp(left_val, right_val); | |
4658 | ||
4659 | mpz_clear(left_val); | |
4660 | mpz_clear(right_val); | |
4661 | ||
4662 | return true; | |
4663 | } | |
4664 | ||
4665 | // Compare floating point constants. | |
4666 | ||
4667 | bool | |
4668 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4669 | const Numeric_constant* right_nc, | |
4670 | int* cmp) | |
4671 | { | |
4672 | mpfr_t left_val; | |
4673 | if (!left_nc->to_float(&left_val)) | |
4674 | return false; | |
4675 | mpfr_t right_val; | |
4676 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4677 | { |
0c77715b | 4678 | mpfr_clear(left_val); |
4679 | return false; | |
4680 | } | |
4681 | ||
4682 | // We already coerced both operands to the same type. If that type | |
4683 | // is not an abstract type, we need to round the values accordingly. | |
4684 | Type* type = left_nc->type(); | |
4685 | if (!type->is_abstract() && type->float_type() != NULL) | |
4686 | { | |
4687 | int bits = type->float_type()->bits(); | |
4688 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4689 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4690 | } |
0c77715b | 4691 | |
4692 | *cmp = mpfr_cmp(left_val, right_val); | |
4693 | ||
4694 | mpfr_clear(left_val); | |
4695 | mpfr_clear(right_val); | |
4696 | ||
4697 | return true; | |
e440a328 | 4698 | } |
4699 | ||
0c77715b | 4700 | // Compare complex constants. Complex numbers may only be compared |
4701 | // for equality. | |
e440a328 | 4702 | |
4703 | bool | |
0c77715b | 4704 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4705 | const Numeric_constant* right_nc, | |
4706 | int* cmp) | |
e440a328 | 4707 | { |
fcbea5e4 | 4708 | mpc_t left_val; |
4709 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4710 | return false; |
fcbea5e4 | 4711 | mpc_t right_val; |
4712 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4713 | { |
fcbea5e4 | 4714 | mpc_clear(left_val); |
0c77715b | 4715 | return false; |
e440a328 | 4716 | } |
0c77715b | 4717 | |
4718 | // We already coerced both operands to the same type. If that type | |
4719 | // is not an abstract type, we need to round the values accordingly. | |
4720 | Type* type = left_nc->type(); | |
4721 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4722 | { |
0c77715b | 4723 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4724 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4725 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4726 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4727 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4728 | } |
0c77715b | 4729 | |
fcbea5e4 | 4730 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4731 | |
fcbea5e4 | 4732 | mpc_clear(left_val); |
4733 | mpc_clear(right_val); | |
0c77715b | 4734 | |
4735 | return true; | |
e440a328 | 4736 | } |
4737 | ||
0c77715b | 4738 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4739 | // true if this could be done, false if not. Issue errors at LOCATION | |
4740 | // as appropriate. | |
e440a328 | 4741 | |
4742 | bool | |
0c77715b | 4743 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4744 | Numeric_constant* right_nc, | |
4745 | Location location, Numeric_constant* nc) | |
e440a328 | 4746 | { |
e440a328 | 4747 | switch (op) |
4748 | { | |
4749 | case OPERATOR_OROR: | |
4750 | case OPERATOR_ANDAND: | |
4751 | case OPERATOR_EQEQ: | |
4752 | case OPERATOR_NOTEQ: | |
4753 | case OPERATOR_LT: | |
4754 | case OPERATOR_LE: | |
4755 | case OPERATOR_GT: | |
4756 | case OPERATOR_GE: | |
9767e2d3 | 4757 | // These return boolean values, not numeric. |
4758 | return false; | |
0c77715b | 4759 | default: |
4760 | break; | |
4761 | } | |
4762 | ||
4763 | Type* left_type = left_nc->type(); | |
4764 | Type* right_type = right_nc->type(); | |
4765 | ||
4766 | Type* type; | |
4767 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4768 | return false; | |
4769 | ||
4770 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4771 | ||
4772 | // When combining an untyped operand with a typed operand, we are | |
4773 | // effectively coercing the untyped operand to the other operand's | |
4774 | // type, so make sure that is valid. | |
4775 | if (!left_nc->set_type(type, true, location)) | |
4776 | return false; | |
4777 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4778 | return false; | |
85334a21 | 4779 | if (is_shift |
4780 | && ((left_type->integer_type() == NULL | |
4781 | && !left_type->is_abstract()) | |
4782 | || (right_type->integer_type() == NULL | |
4783 | && !right_type->is_abstract()))) | |
4784 | return false; | |
0c77715b | 4785 | |
4786 | bool r; | |
4787 | if (type->complex_type() != NULL) | |
4788 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4789 | else if (type->float_type() != NULL) | |
4790 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4791 | else | |
4792 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4793 | ||
4794 | if (r) | |
4795 | r = nc->set_type(type, true, location); | |
4796 | ||
4797 | return r; | |
4798 | } | |
4799 | ||
4800 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4801 | // integer operations. Return true if this could be done, false if | |
4802 | // not. | |
4803 | ||
4804 | bool | |
4805 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4806 | const Numeric_constant* right_nc, | |
4807 | Location location, Numeric_constant* nc) | |
4808 | { | |
4809 | mpz_t left_val; | |
4810 | if (!left_nc->to_int(&left_val)) | |
4811 | return false; | |
4812 | mpz_t right_val; | |
4813 | if (!right_nc->to_int(&right_val)) | |
4814 | { | |
4815 | mpz_clear(left_val); | |
e440a328 | 4816 | return false; |
0c77715b | 4817 | } |
4818 | ||
4819 | mpz_t val; | |
4820 | mpz_init(val); | |
4821 | ||
4822 | switch (op) | |
4823 | { | |
e440a328 | 4824 | case OPERATOR_PLUS: |
4825 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4826 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4827 | { | |
631d5788 | 4828 | go_error_at(location, "constant addition overflow"); |
71a45216 | 4829 | nc->set_invalid(); |
2c809f8f | 4830 | mpz_set_ui(val, 1); |
4831 | } | |
e440a328 | 4832 | break; |
4833 | case OPERATOR_MINUS: | |
4834 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4835 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4836 | { | |
631d5788 | 4837 | go_error_at(location, "constant subtraction overflow"); |
71a45216 | 4838 | nc->set_invalid(); |
2c809f8f | 4839 | mpz_set_ui(val, 1); |
4840 | } | |
e440a328 | 4841 | break; |
4842 | case OPERATOR_OR: | |
4843 | mpz_ior(val, left_val, right_val); | |
4844 | break; | |
4845 | case OPERATOR_XOR: | |
4846 | mpz_xor(val, left_val, right_val); | |
4847 | break; | |
4848 | case OPERATOR_MULT: | |
4849 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4850 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4851 | { | |
631d5788 | 4852 | go_error_at(location, "constant multiplication overflow"); |
71a45216 | 4853 | nc->set_invalid(); |
2c809f8f | 4854 | mpz_set_ui(val, 1); |
4855 | } | |
e440a328 | 4856 | break; |
4857 | case OPERATOR_DIV: | |
4858 | if (mpz_sgn(right_val) != 0) | |
4859 | mpz_tdiv_q(val, left_val, right_val); | |
4860 | else | |
4861 | { | |
631d5788 | 4862 | go_error_at(location, "division by zero"); |
71a45216 | 4863 | nc->set_invalid(); |
e440a328 | 4864 | mpz_set_ui(val, 0); |
e440a328 | 4865 | } |
4866 | break; | |
4867 | case OPERATOR_MOD: | |
4868 | if (mpz_sgn(right_val) != 0) | |
4869 | mpz_tdiv_r(val, left_val, right_val); | |
4870 | else | |
4871 | { | |
631d5788 | 4872 | go_error_at(location, "division by zero"); |
71a45216 | 4873 | nc->set_invalid(); |
e440a328 | 4874 | mpz_set_ui(val, 0); |
e440a328 | 4875 | } |
4876 | break; | |
4877 | case OPERATOR_LSHIFT: | |
4878 | { | |
4879 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4880 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4881 | mpz_mul_2exp(val, left_val, shift); | |
4882 | else | |
e440a328 | 4883 | { |
631d5788 | 4884 | go_error_at(location, "shift count overflow"); |
71a45216 | 4885 | nc->set_invalid(); |
2c809f8f | 4886 | mpz_set_ui(val, 1); |
e440a328 | 4887 | } |
e440a328 | 4888 | break; |
4889 | } | |
4890 | break; | |
4891 | case OPERATOR_RSHIFT: | |
4892 | { | |
4893 | unsigned long shift = mpz_get_ui(right_val); | |
4894 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4895 | { | |
631d5788 | 4896 | go_error_at(location, "shift count overflow"); |
71a45216 | 4897 | nc->set_invalid(); |
2c809f8f | 4898 | mpz_set_ui(val, 1); |
e440a328 | 4899 | } |
e440a328 | 4900 | else |
0c77715b | 4901 | { |
4902 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4903 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4904 | else | |
4905 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4906 | } | |
e440a328 | 4907 | break; |
4908 | } | |
4909 | break; | |
4910 | case OPERATOR_AND: | |
4911 | mpz_and(val, left_val, right_val); | |
4912 | break; | |
4913 | case OPERATOR_BITCLEAR: | |
4914 | { | |
4915 | mpz_t tval; | |
4916 | mpz_init(tval); | |
4917 | mpz_com(tval, right_val); | |
4918 | mpz_and(val, left_val, tval); | |
4919 | mpz_clear(tval); | |
4920 | } | |
4921 | break; | |
4922 | default: | |
c3e6f413 | 4923 | go_unreachable(); |
e440a328 | 4924 | } |
4925 | ||
0c77715b | 4926 | mpz_clear(left_val); |
4927 | mpz_clear(right_val); | |
e440a328 | 4928 | |
0c77715b | 4929 | if (left_nc->is_rune() |
4930 | || (op != OPERATOR_LSHIFT | |
4931 | && op != OPERATOR_RSHIFT | |
4932 | && right_nc->is_rune())) | |
4933 | nc->set_rune(NULL, val); | |
4934 | else | |
4935 | nc->set_int(NULL, val); | |
4936 | ||
4937 | mpz_clear(val); | |
e440a328 | 4938 | |
4939 | return true; | |
4940 | } | |
4941 | ||
0c77715b | 4942 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4943 | // floating point operations. Return true if this could be done, | |
4944 | // false if not. | |
e440a328 | 4945 | |
4946 | bool | |
0c77715b | 4947 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4948 | const Numeric_constant* right_nc, | |
4949 | Location location, Numeric_constant* nc) | |
e440a328 | 4950 | { |
0c77715b | 4951 | mpfr_t left_val; |
4952 | if (!left_nc->to_float(&left_val)) | |
4953 | return false; | |
4954 | mpfr_t right_val; | |
4955 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4956 | { |
0c77715b | 4957 | mpfr_clear(left_val); |
e440a328 | 4958 | return false; |
0c77715b | 4959 | } |
4960 | ||
4961 | mpfr_t val; | |
4962 | mpfr_init(val); | |
4963 | ||
4964 | bool ret = true; | |
4965 | switch (op) | |
4966 | { | |
e440a328 | 4967 | case OPERATOR_PLUS: |
4968 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4969 | break; | |
4970 | case OPERATOR_MINUS: | |
4971 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4972 | break; | |
4973 | case OPERATOR_OR: | |
4974 | case OPERATOR_XOR: | |
4975 | case OPERATOR_AND: | |
4976 | case OPERATOR_BITCLEAR: | |
0c77715b | 4977 | case OPERATOR_MOD: |
4978 | case OPERATOR_LSHIFT: | |
4979 | case OPERATOR_RSHIFT: | |
4980 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4981 | ret = false; | |
4982 | break; | |
e440a328 | 4983 | case OPERATOR_MULT: |
4984 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4985 | break; | |
4986 | case OPERATOR_DIV: | |
0c77715b | 4987 | if (!mpfr_zero_p(right_val)) |
4988 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4989 | else | |
4990 | { | |
631d5788 | 4991 | go_error_at(location, "division by zero"); |
71a45216 | 4992 | nc->set_invalid(); |
0c77715b | 4993 | mpfr_set_ui(val, 0, GMP_RNDN); |
4994 | } | |
e440a328 | 4995 | break; |
e440a328 | 4996 | default: |
c3e6f413 | 4997 | go_unreachable(); |
e440a328 | 4998 | } |
4999 | ||
0c77715b | 5000 | mpfr_clear(left_val); |
5001 | mpfr_clear(right_val); | |
e440a328 | 5002 | |
0c77715b | 5003 | nc->set_float(NULL, val); |
5004 | mpfr_clear(val); | |
e440a328 | 5005 | |
0c77715b | 5006 | return ret; |
e440a328 | 5007 | } |
5008 | ||
0c77715b | 5009 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5010 | // complex operations. Return true if this could be done, false if | |
5011 | // not. | |
e440a328 | 5012 | |
5013 | bool | |
0c77715b | 5014 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
5015 | const Numeric_constant* right_nc, | |
5016 | Location location, Numeric_constant* nc) | |
e440a328 | 5017 | { |
fcbea5e4 | 5018 | mpc_t left_val; |
5019 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 5020 | return false; |
fcbea5e4 | 5021 | mpc_t right_val; |
5022 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 5023 | { |
fcbea5e4 | 5024 | mpc_clear(left_val); |
e440a328 | 5025 | return false; |
0c77715b | 5026 | } |
5027 | ||
fcbea5e4 | 5028 | mpc_t val; |
5029 | mpc_init2(val, mpc_precision); | |
0c77715b | 5030 | |
5031 | bool ret = true; | |
5032 | switch (op) | |
5033 | { | |
e440a328 | 5034 | case OPERATOR_PLUS: |
fcbea5e4 | 5035 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5036 | break; |
5037 | case OPERATOR_MINUS: | |
fcbea5e4 | 5038 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5039 | break; |
5040 | case OPERATOR_OR: | |
5041 | case OPERATOR_XOR: | |
5042 | case OPERATOR_AND: | |
5043 | case OPERATOR_BITCLEAR: | |
0c77715b | 5044 | case OPERATOR_MOD: |
5045 | case OPERATOR_LSHIFT: | |
5046 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 5047 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 5048 | ret = false; |
5049 | break; | |
e440a328 | 5050 | case OPERATOR_MULT: |
fcbea5e4 | 5051 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5052 | break; |
5053 | case OPERATOR_DIV: | |
fcbea5e4 | 5054 | if (mpc_cmp_si(right_val, 0) == 0) |
5055 | { | |
631d5788 | 5056 | go_error_at(location, "division by zero"); |
71a45216 | 5057 | nc->set_invalid(); |
fcbea5e4 | 5058 | mpc_set_ui(val, 0, MPC_RNDNN); |
5059 | break; | |
5060 | } | |
5061 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 5062 | break; |
e440a328 | 5063 | default: |
c3e6f413 | 5064 | go_unreachable(); |
e440a328 | 5065 | } |
5066 | ||
fcbea5e4 | 5067 | mpc_clear(left_val); |
5068 | mpc_clear(right_val); | |
e440a328 | 5069 | |
fcbea5e4 | 5070 | nc->set_complex(NULL, val); |
5071 | mpc_clear(val); | |
e440a328 | 5072 | |
0c77715b | 5073 | return ret; |
e440a328 | 5074 | } |
5075 | ||
5076 | // Lower a binary expression. We have to evaluate constant | |
5077 | // expressions now, in order to implement Go's unlimited precision | |
5078 | // constants. | |
5079 | ||
5080 | Expression* | |
e9d3367e | 5081 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5082 | Statement_inserter* inserter, int) | |
e440a328 | 5083 | { |
b13c66cd | 5084 | Location location = this->location(); |
e440a328 | 5085 | Operator op = this->op_; |
5086 | Expression* left = this->left_; | |
5087 | Expression* right = this->right_; | |
5088 | ||
5089 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5090 | || op == OPERATOR_NOTEQ | |
5091 | || op == OPERATOR_LT | |
5092 | || op == OPERATOR_LE | |
5093 | || op == OPERATOR_GT | |
5094 | || op == OPERATOR_GE); | |
5095 | ||
0c77715b | 5096 | // Numeric constant expressions. |
e440a328 | 5097 | { |
0c77715b | 5098 | Numeric_constant left_nc; |
5099 | Numeric_constant right_nc; | |
5100 | if (left->numeric_constant_value(&left_nc) | |
5101 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5102 | { |
0c77715b | 5103 | if (is_comparison) |
e440a328 | 5104 | { |
0c77715b | 5105 | bool result; |
5106 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5107 | &right_nc, location, | |
5108 | &result)) | |
5109 | return this; | |
e90c9dfc | 5110 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5111 | Expression::make_boolean(result, |
5112 | location), | |
5113 | location); | |
e440a328 | 5114 | } |
5115 | else | |
5116 | { | |
0c77715b | 5117 | Numeric_constant nc; |
5118 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5119 | location, &nc)) | |
71a45216 | 5120 | return this; |
0c77715b | 5121 | return nc.expression(location); |
e440a328 | 5122 | } |
5123 | } | |
e440a328 | 5124 | } |
5125 | ||
5126 | // String constant expressions. | |
315fa98d | 5127 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5128 | { |
5129 | std::string left_string; | |
5130 | std::string right_string; | |
5131 | if (left->string_constant_value(&left_string) | |
5132 | && right->string_constant_value(&right_string)) | |
315fa98d | 5133 | { |
5134 | if (op == OPERATOR_PLUS) | |
5135 | return Expression::make_string(left_string + right_string, | |
5136 | location); | |
5137 | else if (is_comparison) | |
5138 | { | |
5139 | int cmp = left_string.compare(right_string); | |
0c77715b | 5140 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5141 | return Expression::make_boolean(r, location); |
b40dc774 | 5142 | } |
5143 | } | |
b40dc774 | 5144 | } |
5145 | ||
ceeb12d7 | 5146 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5147 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5148 | { | |
b79832ca | 5149 | if (left->type()->struct_type() != NULL |
5150 | && right->type()->struct_type() != NULL) | |
e9d3367e | 5151 | return this->lower_struct_comparison(gogo, inserter); |
5152 | else if (left->type()->array_type() != NULL | |
b79832ca | 5153 | && !left->type()->is_slice_type() |
5154 | && right->type()->array_type() != NULL | |
5155 | && !right->type()->is_slice_type()) | |
e9d3367e | 5156 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 5157 | else if ((left->type()->interface_type() != NULL |
5158 | && right->type()->interface_type() == NULL) | |
5159 | || (left->type()->interface_type() == NULL | |
5160 | && right->type()->interface_type() != NULL)) | |
5161 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5162 | } |
5163 | ||
736a16ba | 5164 | // Lower string concatenation to String_concat_expression, so that |
5165 | // we can group sequences of string additions. | |
5166 | if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) | |
5167 | { | |
5168 | Expression_list* exprs; | |
5169 | String_concat_expression* left_sce = | |
5170 | this->left_->string_concat_expression(); | |
5171 | if (left_sce != NULL) | |
5172 | exprs = left_sce->exprs(); | |
5173 | else | |
5174 | { | |
5175 | exprs = new Expression_list(); | |
5176 | exprs->push_back(this->left_); | |
5177 | } | |
5178 | ||
5179 | String_concat_expression* right_sce = | |
5180 | this->right_->string_concat_expression(); | |
5181 | if (right_sce != NULL) | |
5182 | exprs->append(right_sce->exprs()); | |
5183 | else | |
5184 | exprs->push_back(this->right_); | |
5185 | ||
5186 | return Expression::make_string_concat(exprs); | |
5187 | } | |
5188 | ||
e440a328 | 5189 | return this; |
5190 | } | |
5191 | ||
e9d3367e | 5192 | // Lower a struct comparison. |
5193 | ||
5194 | Expression* | |
5195 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5196 | Statement_inserter* inserter) | |
5197 | { | |
5198 | Struct_type* st = this->left_->type()->struct_type(); | |
5199 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5200 | if (st2 == NULL) | |
5201 | return this; | |
5202 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5203 | return this; | |
5204 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5205 | this->right_->type(), NULL)) | |
5206 | return this; | |
5207 | ||
5208 | // See if we can compare using memcmp. As a heuristic, we use | |
5209 | // memcmp rather than field references and comparisons if there are | |
5210 | // more than two fields. | |
113ef6a5 | 5211 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5212 | return this->lower_compare_to_memcmp(gogo, inserter); |
5213 | ||
5214 | Location loc = this->location(); | |
5215 | ||
5216 | Expression* left = this->left_; | |
5217 | Temporary_statement* left_temp = NULL; | |
5218 | if (left->var_expression() == NULL | |
5219 | && left->temporary_reference_expression() == NULL) | |
5220 | { | |
5221 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5222 | inserter->insert(left_temp); | |
5223 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5224 | } | |
5225 | ||
5226 | Expression* right = this->right_; | |
5227 | Temporary_statement* right_temp = NULL; | |
5228 | if (right->var_expression() == NULL | |
5229 | && right->temporary_reference_expression() == NULL) | |
5230 | { | |
5231 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5232 | inserter->insert(right_temp); | |
5233 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5234 | } | |
5235 | ||
5236 | Expression* ret = Expression::make_boolean(true, loc); | |
5237 | const Struct_field_list* fields = st->fields(); | |
5238 | unsigned int field_index = 0; | |
5239 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5240 | pf != fields->end(); | |
5241 | ++pf, ++field_index) | |
5242 | { | |
f5165c05 | 5243 | if (Gogo::is_sink_name(pf->field_name())) |
5244 | continue; | |
5245 | ||
e9d3367e | 5246 | if (field_index > 0) |
5247 | { | |
5248 | if (left_temp == NULL) | |
5249 | left = left->copy(); | |
5250 | else | |
5251 | left = Expression::make_temporary_reference(left_temp, loc); | |
5252 | if (right_temp == NULL) | |
5253 | right = right->copy(); | |
5254 | else | |
5255 | right = Expression::make_temporary_reference(right_temp, loc); | |
5256 | } | |
5257 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5258 | loc); | |
5259 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5260 | loc); | |
5261 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5262 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5263 | } | |
5264 | ||
5265 | if (this->op_ == OPERATOR_NOTEQ) | |
5266 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5267 | ||
5268 | return ret; | |
5269 | } | |
5270 | ||
5271 | // Lower an array comparison. | |
5272 | ||
5273 | Expression* | |
5274 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5275 | Statement_inserter* inserter) | |
5276 | { | |
5277 | Array_type* at = this->left_->type()->array_type(); | |
5278 | Array_type* at2 = this->right_->type()->array_type(); | |
5279 | if (at2 == NULL) | |
5280 | return this; | |
5281 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5282 | return this; | |
5283 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5284 | this->right_->type(), NULL)) | |
5285 | return this; | |
5286 | ||
5287 | // Call memcmp directly if possible. This may let the middle-end | |
5288 | // optimize the call. | |
113ef6a5 | 5289 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5290 | return this->lower_compare_to_memcmp(gogo, inserter); |
5291 | ||
5292 | // Call the array comparison function. | |
5293 | Named_object* hash_fn; | |
5294 | Named_object* equal_fn; | |
5295 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5296 | &hash_fn, &equal_fn); | |
5297 | ||
5298 | Location loc = this->location(); | |
5299 | ||
5300 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5301 | ||
5302 | Expression_list* args = new Expression_list(); | |
5303 | args->push_back(this->operand_address(inserter, this->left_)); | |
5304 | args->push_back(this->operand_address(inserter, this->right_)); | |
5305 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5306 | ||
5307 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5308 | ||
5309 | if (this->op_ == OPERATOR_NOTEQ) | |
5310 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5311 | ||
5312 | return ret; | |
5313 | } | |
5314 | ||
ceeb12d7 | 5315 | // Lower an interface to value comparison. |
5316 | ||
5317 | Expression* | |
5318 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5319 | Statement_inserter* inserter) | |
5320 | { | |
5321 | Type* left_type = this->left_->type(); | |
5322 | Type* right_type = this->right_->type(); | |
5323 | Interface_type* ift; | |
5324 | if (left_type->interface_type() != NULL) | |
5325 | { | |
5326 | ift = left_type->interface_type(); | |
5327 | if (!ift->implements_interface(right_type, NULL)) | |
5328 | return this; | |
5329 | } | |
5330 | else | |
5331 | { | |
5332 | ift = right_type->interface_type(); | |
5333 | if (!ift->implements_interface(left_type, NULL)) | |
5334 | return this; | |
5335 | } | |
5336 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5337 | return this; | |
5338 | ||
5339 | Location loc = this->location(); | |
5340 | ||
5341 | if (left_type->interface_type() == NULL | |
5342 | && left_type->points_to() == NULL | |
5343 | && !this->left_->is_addressable()) | |
5344 | { | |
5345 | Temporary_statement* temp = | |
5346 | Statement::make_temporary(left_type, NULL, loc); | |
5347 | inserter->insert(temp); | |
5348 | this->left_ = | |
5349 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5350 | } | |
5351 | ||
5352 | if (right_type->interface_type() == NULL | |
5353 | && right_type->points_to() == NULL | |
5354 | && !this->right_->is_addressable()) | |
5355 | { | |
5356 | Temporary_statement* temp = | |
5357 | Statement::make_temporary(right_type, NULL, loc); | |
5358 | inserter->insert(temp); | |
5359 | this->right_ = | |
5360 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5361 | } | |
5362 | ||
5363 | return this; | |
5364 | } | |
5365 | ||
e9d3367e | 5366 | // Lower a struct or array comparison to a call to memcmp. |
5367 | ||
5368 | Expression* | |
5369 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5370 | { | |
5371 | Location loc = this->location(); | |
5372 | ||
5373 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5374 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5375 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5376 | TYPE_INFO_SIZE); | |
5377 | ||
5378 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5379 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5380 | return Expression::make_binary(this->op_, call, zero, loc); |
5381 | } | |
5382 | ||
a32698ee | 5383 | Expression* |
5c3f3470 | 5384 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5385 | Statement_inserter* inserter) |
5386 | { | |
5387 | Location loc = this->location(); | |
5bf8be8b | 5388 | if (this->left_->type()->is_error_type() |
5389 | || this->right_->type()->is_error_type() | |
5390 | || this->left_->is_error_expression() | |
5391 | || this->right_->is_error_expression()) | |
5392 | { | |
5393 | go_assert(saw_errors()); | |
5394 | return Expression::make_error(loc); | |
5395 | } | |
5396 | ||
a32698ee | 5397 | Temporary_statement* temp; |
a32698ee | 5398 | |
5399 | Type* left_type = this->left_->type(); | |
5400 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5401 | || this->op_ == OPERATOR_RSHIFT); | |
5402 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5403 | left_type->integer_type() != NULL) | |
5404 | || this->op_ == OPERATOR_MOD); | |
5405 | ||
a32698ee | 5406 | if (is_shift_op |
5c3f3470 | 5407 | || (is_idiv_op |
5408 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5409 | { |
545ab43b | 5410 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5411 | { |
5412 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5413 | inserter->insert(temp); | |
5414 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5415 | } | |
545ab43b | 5416 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5417 | { |
5418 | temp = | |
5419 | Statement::make_temporary(NULL, this->right_, loc); | |
5420 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5421 | inserter->insert(temp); | |
5422 | } | |
5423 | } | |
5424 | return this; | |
5425 | } | |
5426 | ||
5427 | ||
e9d3367e | 5428 | // Return the address of EXPR, cast to unsafe.Pointer. |
5429 | ||
5430 | Expression* | |
5431 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5432 | Expression* expr) | |
5433 | { | |
5434 | Location loc = this->location(); | |
5435 | ||
5436 | if (!expr->is_addressable()) | |
5437 | { | |
5438 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5439 | loc); | |
5440 | inserter->insert(temp); | |
5441 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5442 | } | |
5443 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5444 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5445 | Type* void_type = Type::make_void_type(); | |
5446 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5447 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5448 | } | |
5449 | ||
0c77715b | 5450 | // Return the numeric constant value, if it has one. |
e440a328 | 5451 | |
5452 | bool | |
0c77715b | 5453 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5454 | { |
0c77715b | 5455 | Numeric_constant left_nc; |
5456 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5457 | return false; | |
5458 | Numeric_constant right_nc; | |
5459 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5460 | return false; | |
9767e2d3 | 5461 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5462 | this->location(), nc); |
e440a328 | 5463 | } |
5464 | ||
5465 | // Note that the value is being discarded. | |
5466 | ||
4f2138d7 | 5467 | bool |
e440a328 | 5468 | Binary_expression::do_discarding_value() |
5469 | { | |
5470 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5471 | return this->right_->discarding_value(); |
e440a328 | 5472 | else |
4f2138d7 | 5473 | { |
5474 | this->unused_value_error(); | |
5475 | return false; | |
5476 | } | |
e440a328 | 5477 | } |
5478 | ||
5479 | // Get type. | |
5480 | ||
5481 | Type* | |
5482 | Binary_expression::do_type() | |
5483 | { | |
5f5fea79 | 5484 | if (this->classification() == EXPRESSION_ERROR) |
5485 | return Type::make_error_type(); | |
5486 | ||
e440a328 | 5487 | switch (this->op_) |
5488 | { | |
e440a328 | 5489 | case OPERATOR_EQEQ: |
5490 | case OPERATOR_NOTEQ: | |
5491 | case OPERATOR_LT: | |
5492 | case OPERATOR_LE: | |
5493 | case OPERATOR_GT: | |
5494 | case OPERATOR_GE: | |
e90c9dfc | 5495 | if (this->type_ == NULL) |
5496 | this->type_ = Type::make_boolean_type(); | |
5497 | return this->type_; | |
e440a328 | 5498 | |
5499 | case OPERATOR_PLUS: | |
5500 | case OPERATOR_MINUS: | |
5501 | case OPERATOR_OR: | |
5502 | case OPERATOR_XOR: | |
5503 | case OPERATOR_MULT: | |
5504 | case OPERATOR_DIV: | |
5505 | case OPERATOR_MOD: | |
5506 | case OPERATOR_AND: | |
5507 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5508 | case OPERATOR_OROR: |
5509 | case OPERATOR_ANDAND: | |
e440a328 | 5510 | { |
0c77715b | 5511 | Type* type; |
5512 | if (!Binary_expression::operation_type(this->op_, | |
5513 | this->left_->type(), | |
5514 | this->right_->type(), | |
5515 | &type)) | |
5516 | return Type::make_error_type(); | |
5517 | return type; | |
e440a328 | 5518 | } |
5519 | ||
5520 | case OPERATOR_LSHIFT: | |
5521 | case OPERATOR_RSHIFT: | |
5522 | return this->left_->type(); | |
5523 | ||
5524 | default: | |
c3e6f413 | 5525 | go_unreachable(); |
e440a328 | 5526 | } |
5527 | } | |
5528 | ||
5529 | // Set type for a binary expression. | |
5530 | ||
5531 | void | |
5532 | Binary_expression::do_determine_type(const Type_context* context) | |
5533 | { | |
5534 | Type* tleft = this->left_->type(); | |
5535 | Type* tright = this->right_->type(); | |
5536 | ||
5537 | // Both sides should have the same type, except for the shift | |
5538 | // operations. For a comparison, we should ignore the incoming | |
5539 | // type. | |
5540 | ||
5541 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5542 | || this->op_ == OPERATOR_RSHIFT); | |
5543 | ||
5544 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5545 | || this->op_ == OPERATOR_NOTEQ | |
5546 | || this->op_ == OPERATOR_LT | |
5547 | || this->op_ == OPERATOR_LE | |
5548 | || this->op_ == OPERATOR_GT | |
5549 | || this->op_ == OPERATOR_GE); | |
5550 | ||
c999c2a7 | 5551 | // For constant expressions, the context of the result is not useful in |
5552 | // determining the types of the operands. It is only legal to use abstract | |
5553 | // boolean, numeric, and string constants as operands where it is legal to | |
5554 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5555 | // Any issues with the operation will be resolved in the check_types pass. | |
5556 | bool is_constant_expr = (this->left_->is_constant() | |
5557 | && this->right_->is_constant()); | |
5558 | ||
e440a328 | 5559 | Type_context subcontext(*context); |
5560 | ||
5561 | if (is_comparison) | |
5562 | { | |
5563 | // In a comparison, the context does not determine the types of | |
5564 | // the operands. | |
5565 | subcontext.type = NULL; | |
5566 | } | |
5567 | ||
5568 | // Set the context for the left hand operand. | |
5569 | if (is_shift_op) | |
5570 | { | |
b40dc774 | 5571 | // The right hand operand of a shift plays no role in |
5572 | // determining the type of the left hand operand. | |
e440a328 | 5573 | } |
5574 | else if (!tleft->is_abstract()) | |
5575 | subcontext.type = tleft; | |
5576 | else if (!tright->is_abstract()) | |
5577 | subcontext.type = tright; | |
5578 | else if (subcontext.type == NULL) | |
5579 | { | |
5580 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5581 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5582 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5583 | { | |
5584 | // Both sides have an abstract integer, abstract float, or | |
5585 | // abstract complex type. Just let CONTEXT determine | |
5586 | // whether they may remain abstract or not. | |
5587 | } | |
5588 | else if (tleft->complex_type() != NULL) | |
5589 | subcontext.type = tleft; | |
5590 | else if (tright->complex_type() != NULL) | |
5591 | subcontext.type = tright; | |
5592 | else if (tleft->float_type() != NULL) | |
5593 | subcontext.type = tleft; | |
5594 | else if (tright->float_type() != NULL) | |
5595 | subcontext.type = tright; | |
5596 | else | |
5597 | subcontext.type = tleft; | |
f58a23ae | 5598 | |
5599 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5600 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5601 | } |
5602 | ||
c999c2a7 | 5603 | if (!is_constant_expr) |
5604 | this->left_->determine_type(&subcontext); | |
e440a328 | 5605 | |
e440a328 | 5606 | if (is_shift_op) |
5607 | { | |
b40dc774 | 5608 | // We may have inherited an unusable type for the shift operand. |
5609 | // Give a useful error if that happened. | |
5610 | if (tleft->is_abstract() | |
5611 | && subcontext.type != NULL | |
8ab6effb | 5612 | && !subcontext.may_be_abstract |
f6bc81e6 | 5613 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5614 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5615 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5616 | "for left operand of shift")); |
b40dc774 | 5617 | |
5618 | // The context for the right hand operand is the same as for the | |
5619 | // left hand operand, except for a shift operator. | |
e440a328 | 5620 | subcontext.type = Type::lookup_integer_type("uint"); |
5621 | subcontext.may_be_abstract = false; | |
5622 | } | |
5623 | ||
c999c2a7 | 5624 | if (!is_constant_expr) |
5625 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5626 | |
5627 | if (is_comparison) | |
5628 | { | |
5629 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5630 | ; | |
5631 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5632 | this->type_ = context->type; | |
5633 | else if (!context->may_be_abstract) | |
5634 | this->type_ = Type::lookup_bool_type(); | |
5635 | } | |
e440a328 | 5636 | } |
5637 | ||
5638 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5639 | // OTYPE is the type of the other operand. Return whether the |
5640 | // operation is OK. This should not be used for shift. | |
e440a328 | 5641 | |
5642 | bool | |
be8b5eee | 5643 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5644 | Location location) |
e440a328 | 5645 | { |
5646 | switch (op) | |
5647 | { | |
5648 | case OPERATOR_OROR: | |
5649 | case OPERATOR_ANDAND: | |
c999c2a7 | 5650 | if (!type->is_boolean_type() |
5651 | || !otype->is_boolean_type()) | |
e440a328 | 5652 | { |
631d5788 | 5653 | go_error_at(location, "expected boolean type"); |
e440a328 | 5654 | return false; |
5655 | } | |
5656 | break; | |
5657 | ||
5658 | case OPERATOR_EQEQ: | |
5659 | case OPERATOR_NOTEQ: | |
e9d3367e | 5660 | { |
5661 | std::string reason; | |
5662 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5663 | { | |
631d5788 | 5664 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5665 | return false; |
5666 | } | |
5667 | } | |
e440a328 | 5668 | break; |
5669 | ||
5670 | case OPERATOR_LT: | |
5671 | case OPERATOR_LE: | |
5672 | case OPERATOR_GT: | |
5673 | case OPERATOR_GE: | |
e9d3367e | 5674 | { |
5675 | std::string reason; | |
5676 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5677 | { | |
631d5788 | 5678 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5679 | return false; |
5680 | } | |
5681 | } | |
e440a328 | 5682 | break; |
5683 | ||
5684 | case OPERATOR_PLUS: | |
5685 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5686 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5687 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5688 | { |
631d5788 | 5689 | go_error_at(location, |
e440a328 | 5690 | "expected integer, floating, complex, or string type"); |
5691 | return false; | |
5692 | } | |
5693 | break; | |
5694 | ||
5695 | case OPERATOR_MINUS: | |
5696 | case OPERATOR_MINUSEQ: | |
5697 | case OPERATOR_MULT: | |
5698 | case OPERATOR_MULTEQ: | |
5699 | case OPERATOR_DIV: | |
5700 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5701 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5702 | { |
631d5788 | 5703 | go_error_at(location, "expected integer, floating, or complex type"); |
e440a328 | 5704 | return false; |
5705 | } | |
5706 | break; | |
5707 | ||
5708 | case OPERATOR_MOD: | |
5709 | case OPERATOR_MODEQ: | |
5710 | case OPERATOR_OR: | |
5711 | case OPERATOR_OREQ: | |
5712 | case OPERATOR_AND: | |
5713 | case OPERATOR_ANDEQ: | |
5714 | case OPERATOR_XOR: | |
5715 | case OPERATOR_XOREQ: | |
5716 | case OPERATOR_BITCLEAR: | |
5717 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5718 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5719 | { |
631d5788 | 5720 | go_error_at(location, "expected integer type"); |
e440a328 | 5721 | return false; |
5722 | } | |
5723 | break; | |
5724 | ||
5725 | default: | |
c3e6f413 | 5726 | go_unreachable(); |
e440a328 | 5727 | } |
5728 | ||
5729 | return true; | |
5730 | } | |
5731 | ||
5732 | // Check types. | |
5733 | ||
5734 | void | |
5735 | Binary_expression::do_check_types(Gogo*) | |
5736 | { | |
5f5fea79 | 5737 | if (this->classification() == EXPRESSION_ERROR) |
5738 | return; | |
5739 | ||
e440a328 | 5740 | Type* left_type = this->left_->type(); |
5741 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5742 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5743 | { |
5744 | this->set_is_error(); | |
5745 | return; | |
5746 | } | |
e440a328 | 5747 | |
5748 | if (this->op_ == OPERATOR_EQEQ | |
5749 | || this->op_ == OPERATOR_NOTEQ | |
5750 | || this->op_ == OPERATOR_LT | |
5751 | || this->op_ == OPERATOR_LE | |
5752 | || this->op_ == OPERATOR_GT | |
5753 | || this->op_ == OPERATOR_GE) | |
5754 | { | |
907c5ecd | 5755 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5756 | { | |
5757 | this->report_error(_("invalid comparison of nil with nil")); | |
5758 | return; | |
5759 | } | |
e440a328 | 5760 | if (!Type::are_assignable(left_type, right_type, NULL) |
5761 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5762 | { | |
5763 | this->report_error(_("incompatible types in binary expression")); | |
5764 | return; | |
5765 | } | |
5766 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5767 | right_type, |
e440a328 | 5768 | this->location()) |
5769 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5770 | left_type, |
e440a328 | 5771 | this->location())) |
5772 | { | |
5773 | this->set_is_error(); | |
5774 | return; | |
5775 | } | |
5776 | } | |
5777 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5778 | { | |
5779 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5780 | { | |
5781 | this->report_error(_("incompatible types in binary expression")); | |
5782 | return; | |
5783 | } | |
5784 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5785 | right_type, |
e440a328 | 5786 | this->location())) |
5787 | { | |
5788 | this->set_is_error(); | |
5789 | return; | |
5790 | } | |
5c65b19d | 5791 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5792 | { | |
5793 | // Division by a zero integer constant is an error. | |
5794 | Numeric_constant rconst; | |
5795 | unsigned long rval; | |
5796 | if (left_type->integer_type() != NULL | |
5797 | && this->right_->numeric_constant_value(&rconst) | |
5798 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5799 | && rval == 0) | |
5800 | { | |
5801 | this->report_error(_("integer division by zero")); | |
5802 | return; | |
5803 | } | |
5804 | } | |
e440a328 | 5805 | } |
5806 | else | |
5807 | { | |
5808 | if (left_type->integer_type() == NULL) | |
5809 | this->report_error(_("shift of non-integer operand")); | |
5810 | ||
6b5e0fac | 5811 | if (right_type->is_string_type()) |
5812 | this->report_error(_("shift count not unsigned integer")); | |
5813 | else if (!right_type->is_abstract() | |
e440a328 | 5814 | && (right_type->integer_type() == NULL |
5815 | || !right_type->integer_type()->is_unsigned())) | |
5816 | this->report_error(_("shift count not unsigned integer")); | |
5817 | else | |
5818 | { | |
0c77715b | 5819 | Numeric_constant nc; |
5820 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5821 | { |
0c77715b | 5822 | mpz_t val; |
5823 | if (!nc.to_int(&val)) | |
5824 | this->report_error(_("shift count not unsigned integer")); | |
5825 | else | |
a4eba91b | 5826 | { |
0c77715b | 5827 | if (mpz_sgn(val) < 0) |
5828 | { | |
5829 | this->report_error(_("negative shift count")); | |
0c77715b | 5830 | Location rloc = this->right_->location(); |
e67508fa | 5831 | this->right_ = Expression::make_integer_ul(0, right_type, |
5832 | rloc); | |
0c77715b | 5833 | } |
5834 | mpz_clear(val); | |
a4eba91b | 5835 | } |
e440a328 | 5836 | } |
e440a328 | 5837 | } |
5838 | } | |
5839 | } | |
5840 | ||
ea664253 | 5841 | // Get the backend representation for a binary expression. |
e440a328 | 5842 | |
ea664253 | 5843 | Bexpression* |
5844 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 5845 | { |
1b1f2abf | 5846 | Gogo* gogo = context->gogo(); |
a32698ee | 5847 | Location loc = this->location(); |
5848 | Type* left_type = this->left_->type(); | |
5849 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5850 | |
e440a328 | 5851 | bool use_left_type = true; |
5852 | bool is_shift_op = false; | |
29a2d1d8 | 5853 | bool is_idiv_op = false; |
e440a328 | 5854 | switch (this->op_) |
5855 | { | |
5856 | case OPERATOR_EQEQ: | |
5857 | case OPERATOR_NOTEQ: | |
5858 | case OPERATOR_LT: | |
5859 | case OPERATOR_LE: | |
5860 | case OPERATOR_GT: | |
5861 | case OPERATOR_GE: | |
ea664253 | 5862 | return Expression::comparison(context, this->type_, this->op_, |
5863 | this->left_, this->right_, loc); | |
e440a328 | 5864 | |
5865 | case OPERATOR_OROR: | |
e440a328 | 5866 | case OPERATOR_ANDAND: |
e440a328 | 5867 | use_left_type = false; |
5868 | break; | |
5869 | case OPERATOR_PLUS: | |
e440a328 | 5870 | case OPERATOR_MINUS: |
e440a328 | 5871 | case OPERATOR_OR: |
e440a328 | 5872 | case OPERATOR_XOR: |
e440a328 | 5873 | case OPERATOR_MULT: |
e440a328 | 5874 | break; |
5875 | case OPERATOR_DIV: | |
a32698ee | 5876 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
5877 | break; | |
729f8831 | 5878 | // Fall through. |
e440a328 | 5879 | case OPERATOR_MOD: |
29a2d1d8 | 5880 | is_idiv_op = true; |
e440a328 | 5881 | break; |
5882 | case OPERATOR_LSHIFT: | |
e440a328 | 5883 | case OPERATOR_RSHIFT: |
e440a328 | 5884 | is_shift_op = true; |
5885 | break; | |
e440a328 | 5886 | case OPERATOR_BITCLEAR: |
a32698ee | 5887 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
5888 | case OPERATOR_AND: | |
e440a328 | 5889 | break; |
5890 | default: | |
c3e6f413 | 5891 | go_unreachable(); |
e440a328 | 5892 | } |
5893 | ||
736a16ba | 5894 | // The only binary operation for string is +, and that should have |
5895 | // been converted to a String_concat_expression in do_lower. | |
5896 | go_assert(!left_type->is_string_type()); | |
a32698ee | 5897 | |
5898 | // For complex division Go might want slightly different results than the | |
5899 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 5900 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
5901 | { | |
a32698ee | 5902 | Runtime::Function complex_code; |
1850e20c | 5903 | switch (this->left_->type()->complex_type()->bits()) |
5904 | { | |
5905 | case 64: | |
a32698ee | 5906 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 5907 | break; |
5908 | case 128: | |
a32698ee | 5909 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 5910 | break; |
5911 | default: | |
5912 | go_unreachable(); | |
5913 | } | |
a32698ee | 5914 | Expression* complex_div = |
5915 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 5916 | return complex_div->get_backend(context); |
1850e20c | 5917 | } |
5918 | ||
ea664253 | 5919 | Bexpression* left = this->left_->get_backend(context); |
5920 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 5921 | |
a32698ee | 5922 | Type* type = use_left_type ? left_type : right_type; |
5923 | Btype* btype = type->get_backend(gogo); | |
5924 | ||
5925 | Bexpression* ret = | |
5926 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
5927 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 5928 | |
a32698ee | 5929 | // Initialize overflow constants. |
5930 | Bexpression* overflow; | |
5931 | mpz_t zero; | |
5932 | mpz_init_set_ui(zero, 0UL); | |
5933 | mpz_t one; | |
5934 | mpz_init_set_ui(one, 1UL); | |
5935 | mpz_t neg_one; | |
5936 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 5937 | |
a32698ee | 5938 | Btype* left_btype = left_type->get_backend(gogo); |
5939 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 5940 | |
5941 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 5942 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 5943 | if (is_shift_op) |
5944 | { | |
a32698ee | 5945 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 5946 | |
a32698ee | 5947 | mpz_t bitsval; |
5948 | int bits = left_type->integer_type()->bits(); | |
5949 | mpz_init_set_ui(bitsval, bits); | |
5950 | Bexpression* bits_expr = | |
5951 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
5952 | Bexpression* compare = | |
5953 | gogo->backend()->binary_expression(OPERATOR_LT, | |
5954 | right, bits_expr, loc); | |
e440a328 | 5955 | |
a32698ee | 5956 | Bexpression* zero_expr = |
5957 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
5958 | overflow = zero_expr; | |
e440a328 | 5959 | if (this->op_ == OPERATOR_RSHIFT |
a32698ee | 5960 | && !left_type->integer_type()->is_unsigned()) |
e440a328 | 5961 | { |
a32698ee | 5962 | Bexpression* neg_expr = |
5963 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
5964 | zero_expr, loc); | |
5965 | Bexpression* neg_one_expr = | |
5966 | gogo->backend()->integer_constant_expression(left_btype, neg_one); | |
5967 | overflow = gogo->backend()->conditional_expression(btype, neg_expr, | |
5968 | neg_one_expr, | |
5969 | zero_expr, loc); | |
29a2d1d8 | 5970 | } |
a32698ee | 5971 | ret = gogo->backend()->conditional_expression(btype, compare, ret, |
5972 | overflow, loc); | |
5973 | mpz_clear(bitsval); | |
29a2d1d8 | 5974 | } |
5975 | ||
5976 | // Add checks for division by zero and division overflow as needed. | |
5977 | if (is_idiv_op) | |
5978 | { | |
5c3f3470 | 5979 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 5980 | { |
5981 | // right == 0 | |
a32698ee | 5982 | Bexpression* zero_expr = |
5983 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
5984 | Bexpression* check = | |
5985 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
5986 | right, zero_expr, loc); | |
29a2d1d8 | 5987 | |
a32698ee | 5988 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 5989 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 5990 | Bexpression* crash = gogo->runtime_error(errcode, |
5991 | loc)->get_backend(context); | |
29a2d1d8 | 5992 | |
5993 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
ea664253 | 5994 | ret = gogo->backend()->conditional_expression(btype, check, crash, |
5995 | ret, loc); | |
b13c66cd | 5996 | } |
5997 | ||
5c3f3470 | 5998 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 5999 | { |
6000 | // right == -1 | |
6001 | // FIXME: It would be nice to say that this test is expected | |
6002 | // to return false. | |
a32698ee | 6003 | |
6004 | Bexpression* neg_one_expr = | |
6005 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6006 | Bexpression* check = | |
6007 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6008 | right, neg_one_expr, loc); | |
6009 | ||
6010 | Bexpression* zero_expr = | |
6011 | gogo->backend()->integer_constant_expression(btype, zero); | |
6012 | Bexpression* one_expr = | |
6013 | gogo->backend()->integer_constant_expression(btype, one); | |
6014 | ||
6015 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6016 | { |
6017 | // An unsigned -1 is the largest possible number, so | |
6018 | // dividing is always 1 or 0. | |
a32698ee | 6019 | |
6020 | Bexpression* cmp = | |
6021 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6022 | left, right, loc); | |
29a2d1d8 | 6023 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6024 | overflow = |
6025 | gogo->backend()->conditional_expression(btype, cmp, | |
6026 | one_expr, zero_expr, | |
6027 | loc); | |
29a2d1d8 | 6028 | else |
a32698ee | 6029 | overflow = |
6030 | gogo->backend()->conditional_expression(btype, cmp, | |
6031 | zero_expr, left, | |
6032 | loc); | |
29a2d1d8 | 6033 | } |
6034 | else | |
6035 | { | |
6036 | // Computing left / -1 is the same as computing - left, | |
6037 | // which does not overflow since Go sets -fwrapv. | |
6038 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6039 | { |
6040 | Expression* negate_expr = | |
6041 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 6042 | overflow = negate_expr->get_backend(context); |
a32698ee | 6043 | } |
29a2d1d8 | 6044 | else |
a32698ee | 6045 | overflow = zero_expr; |
29a2d1d8 | 6046 | } |
a32698ee | 6047 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6048 | |
6049 | // right == -1 ? - left : ret | |
a32698ee | 6050 | ret = gogo->backend()->conditional_expression(btype, check, overflow, |
6051 | ret, loc); | |
29a2d1d8 | 6052 | } |
e440a328 | 6053 | } |
6054 | ||
a32698ee | 6055 | mpz_clear(zero); |
6056 | mpz_clear(one); | |
6057 | mpz_clear(neg_one); | |
ea664253 | 6058 | return ret; |
e440a328 | 6059 | } |
6060 | ||
6061 | // Export a binary expression. | |
6062 | ||
6063 | void | |
6064 | Binary_expression::do_export(Export* exp) const | |
6065 | { | |
6066 | exp->write_c_string("("); | |
6067 | this->left_->export_expression(exp); | |
6068 | switch (this->op_) | |
6069 | { | |
6070 | case OPERATOR_OROR: | |
6071 | exp->write_c_string(" || "); | |
6072 | break; | |
6073 | case OPERATOR_ANDAND: | |
6074 | exp->write_c_string(" && "); | |
6075 | break; | |
6076 | case OPERATOR_EQEQ: | |
6077 | exp->write_c_string(" == "); | |
6078 | break; | |
6079 | case OPERATOR_NOTEQ: | |
6080 | exp->write_c_string(" != "); | |
6081 | break; | |
6082 | case OPERATOR_LT: | |
6083 | exp->write_c_string(" < "); | |
6084 | break; | |
6085 | case OPERATOR_LE: | |
6086 | exp->write_c_string(" <= "); | |
6087 | break; | |
6088 | case OPERATOR_GT: | |
6089 | exp->write_c_string(" > "); | |
6090 | break; | |
6091 | case OPERATOR_GE: | |
6092 | exp->write_c_string(" >= "); | |
6093 | break; | |
6094 | case OPERATOR_PLUS: | |
6095 | exp->write_c_string(" + "); | |
6096 | break; | |
6097 | case OPERATOR_MINUS: | |
6098 | exp->write_c_string(" - "); | |
6099 | break; | |
6100 | case OPERATOR_OR: | |
6101 | exp->write_c_string(" | "); | |
6102 | break; | |
6103 | case OPERATOR_XOR: | |
6104 | exp->write_c_string(" ^ "); | |
6105 | break; | |
6106 | case OPERATOR_MULT: | |
6107 | exp->write_c_string(" * "); | |
6108 | break; | |
6109 | case OPERATOR_DIV: | |
6110 | exp->write_c_string(" / "); | |
6111 | break; | |
6112 | case OPERATOR_MOD: | |
6113 | exp->write_c_string(" % "); | |
6114 | break; | |
6115 | case OPERATOR_LSHIFT: | |
6116 | exp->write_c_string(" << "); | |
6117 | break; | |
6118 | case OPERATOR_RSHIFT: | |
6119 | exp->write_c_string(" >> "); | |
6120 | break; | |
6121 | case OPERATOR_AND: | |
6122 | exp->write_c_string(" & "); | |
6123 | break; | |
6124 | case OPERATOR_BITCLEAR: | |
6125 | exp->write_c_string(" &^ "); | |
6126 | break; | |
6127 | default: | |
c3e6f413 | 6128 | go_unreachable(); |
e440a328 | 6129 | } |
6130 | this->right_->export_expression(exp); | |
6131 | exp->write_c_string(")"); | |
6132 | } | |
6133 | ||
6134 | // Import a binary expression. | |
6135 | ||
6136 | Expression* | |
6137 | Binary_expression::do_import(Import* imp) | |
6138 | { | |
6139 | imp->require_c_string("("); | |
6140 | ||
6141 | Expression* left = Expression::import_expression(imp); | |
6142 | ||
6143 | Operator op; | |
6144 | if (imp->match_c_string(" || ")) | |
6145 | { | |
6146 | op = OPERATOR_OROR; | |
6147 | imp->advance(4); | |
6148 | } | |
6149 | else if (imp->match_c_string(" && ")) | |
6150 | { | |
6151 | op = OPERATOR_ANDAND; | |
6152 | imp->advance(4); | |
6153 | } | |
6154 | else if (imp->match_c_string(" == ")) | |
6155 | { | |
6156 | op = OPERATOR_EQEQ; | |
6157 | imp->advance(4); | |
6158 | } | |
6159 | else if (imp->match_c_string(" != ")) | |
6160 | { | |
6161 | op = OPERATOR_NOTEQ; | |
6162 | imp->advance(4); | |
6163 | } | |
6164 | else if (imp->match_c_string(" < ")) | |
6165 | { | |
6166 | op = OPERATOR_LT; | |
6167 | imp->advance(3); | |
6168 | } | |
6169 | else if (imp->match_c_string(" <= ")) | |
6170 | { | |
6171 | op = OPERATOR_LE; | |
6172 | imp->advance(4); | |
6173 | } | |
6174 | else if (imp->match_c_string(" > ")) | |
6175 | { | |
6176 | op = OPERATOR_GT; | |
6177 | imp->advance(3); | |
6178 | } | |
6179 | else if (imp->match_c_string(" >= ")) | |
6180 | { | |
6181 | op = OPERATOR_GE; | |
6182 | imp->advance(4); | |
6183 | } | |
6184 | else if (imp->match_c_string(" + ")) | |
6185 | { | |
6186 | op = OPERATOR_PLUS; | |
6187 | imp->advance(3); | |
6188 | } | |
6189 | else if (imp->match_c_string(" - ")) | |
6190 | { | |
6191 | op = OPERATOR_MINUS; | |
6192 | imp->advance(3); | |
6193 | } | |
6194 | else if (imp->match_c_string(" | ")) | |
6195 | { | |
6196 | op = OPERATOR_OR; | |
6197 | imp->advance(3); | |
6198 | } | |
6199 | else if (imp->match_c_string(" ^ ")) | |
6200 | { | |
6201 | op = OPERATOR_XOR; | |
6202 | imp->advance(3); | |
6203 | } | |
6204 | else if (imp->match_c_string(" * ")) | |
6205 | { | |
6206 | op = OPERATOR_MULT; | |
6207 | imp->advance(3); | |
6208 | } | |
6209 | else if (imp->match_c_string(" / ")) | |
6210 | { | |
6211 | op = OPERATOR_DIV; | |
6212 | imp->advance(3); | |
6213 | } | |
6214 | else if (imp->match_c_string(" % ")) | |
6215 | { | |
6216 | op = OPERATOR_MOD; | |
6217 | imp->advance(3); | |
6218 | } | |
6219 | else if (imp->match_c_string(" << ")) | |
6220 | { | |
6221 | op = OPERATOR_LSHIFT; | |
6222 | imp->advance(4); | |
6223 | } | |
6224 | else if (imp->match_c_string(" >> ")) | |
6225 | { | |
6226 | op = OPERATOR_RSHIFT; | |
6227 | imp->advance(4); | |
6228 | } | |
6229 | else if (imp->match_c_string(" & ")) | |
6230 | { | |
6231 | op = OPERATOR_AND; | |
6232 | imp->advance(3); | |
6233 | } | |
6234 | else if (imp->match_c_string(" &^ ")) | |
6235 | { | |
6236 | op = OPERATOR_BITCLEAR; | |
6237 | imp->advance(4); | |
6238 | } | |
6239 | else | |
6240 | { | |
631d5788 | 6241 | go_error_at(imp->location(), "unrecognized binary operator"); |
e440a328 | 6242 | return Expression::make_error(imp->location()); |
6243 | } | |
6244 | ||
6245 | Expression* right = Expression::import_expression(imp); | |
6246 | ||
6247 | imp->require_c_string(")"); | |
6248 | ||
6249 | return Expression::make_binary(op, left, right, imp->location()); | |
6250 | } | |
6251 | ||
d751bb78 | 6252 | // Dump ast representation of a binary expression. |
6253 | ||
6254 | void | |
6255 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6256 | { | |
6257 | ast_dump_context->ostream() << "("; | |
6258 | ast_dump_context->dump_expression(this->left_); | |
6259 | ast_dump_context->ostream() << " "; | |
6260 | ast_dump_context->dump_operator(this->op_); | |
6261 | ast_dump_context->ostream() << " "; | |
6262 | ast_dump_context->dump_expression(this->right_); | |
6263 | ast_dump_context->ostream() << ") "; | |
6264 | } | |
6265 | ||
e440a328 | 6266 | // Make a binary expression. |
6267 | ||
6268 | Expression* | |
6269 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6270 | Location location) |
e440a328 | 6271 | { |
6272 | return new Binary_expression(op, left, right, location); | |
6273 | } | |
6274 | ||
6275 | // Implement a comparison. | |
6276 | ||
a32698ee | 6277 | Bexpression* |
6278 | Expression::comparison(Translate_context* context, Type* result_type, | |
6279 | Operator op, Expression* left, Expression* right, | |
6280 | Location location) | |
e440a328 | 6281 | { |
2387f644 | 6282 | Type* left_type = left->type(); |
6283 | Type* right_type = right->type(); | |
ceeb12d7 | 6284 | |
e67508fa | 6285 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6286 | |
15c67ee2 | 6287 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6288 | { |
6098d6cb | 6289 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
6290 | { | |
6291 | left = Runtime::make_call(Runtime::EQSTRING, location, 2, | |
6292 | left, right); | |
6293 | right = Expression::make_boolean(true, location); | |
6294 | } | |
6295 | else | |
6296 | { | |
6297 | left = Runtime::make_call(Runtime::CMPSTRING, location, 2, | |
6298 | left, right); | |
6299 | right = zexpr; | |
6300 | } | |
e440a328 | 6301 | } |
15c67ee2 | 6302 | else if ((left_type->interface_type() != NULL |
6303 | && right_type->interface_type() == NULL | |
6304 | && !right_type->is_nil_type()) | |
6305 | || (left_type->interface_type() == NULL | |
6306 | && !left_type->is_nil_type() | |
6307 | && right_type->interface_type() != NULL)) | |
e440a328 | 6308 | { |
6309 | // Comparing an interface value to a non-interface value. | |
6310 | if (left_type->interface_type() == NULL) | |
6311 | { | |
6312 | std::swap(left_type, right_type); | |
2387f644 | 6313 | std::swap(left, right); |
e440a328 | 6314 | } |
6315 | ||
6316 | // The right operand is not an interface. We need to take its | |
6317 | // address if it is not a pointer. | |
ceeb12d7 | 6318 | Expression* pointer_arg = NULL; |
e440a328 | 6319 | if (right_type->points_to() != NULL) |
2387f644 | 6320 | pointer_arg = right; |
e440a328 | 6321 | else |
6322 | { | |
2387f644 | 6323 | go_assert(right->is_addressable()); |
6324 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6325 | location); |
e440a328 | 6326 | } |
e440a328 | 6327 | |
2387f644 | 6328 | Expression* descriptor = |
6329 | Expression::make_type_descriptor(right_type, location); | |
6330 | left = | |
ceeb12d7 | 6331 | Runtime::make_call((left_type->interface_type()->is_empty() |
6098d6cb | 6332 | ? Runtime::EFACEVALEQ |
6333 | : Runtime::IFACEVALEQ), | |
2387f644 | 6334 | location, 3, left, descriptor, |
ceeb12d7 | 6335 | pointer_arg); |
6098d6cb | 6336 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6337 | right = Expression::make_boolean(true, location); | |
e440a328 | 6338 | } |
6339 | else if (left_type->interface_type() != NULL | |
6340 | && right_type->interface_type() != NULL) | |
6341 | { | |
ceeb12d7 | 6342 | Runtime::Function compare_function; |
739bad04 | 6343 | if (left_type->interface_type()->is_empty() |
6344 | && right_type->interface_type()->is_empty()) | |
6098d6cb | 6345 | compare_function = Runtime::EFACEEQ; |
739bad04 | 6346 | else if (!left_type->interface_type()->is_empty() |
6347 | && !right_type->interface_type()->is_empty()) | |
6098d6cb | 6348 | compare_function = Runtime::IFACEEQ; |
739bad04 | 6349 | else |
6350 | { | |
6351 | if (left_type->interface_type()->is_empty()) | |
6352 | { | |
739bad04 | 6353 | std::swap(left_type, right_type); |
2387f644 | 6354 | std::swap(left, right); |
739bad04 | 6355 | } |
c484d925 | 6356 | go_assert(!left_type->interface_type()->is_empty()); |
6357 | go_assert(right_type->interface_type()->is_empty()); | |
6098d6cb | 6358 | compare_function = Runtime::IFACEEFACEEQ; |
739bad04 | 6359 | } |
6360 | ||
2387f644 | 6361 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6098d6cb | 6362 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6363 | right = Expression::make_boolean(true, location); | |
e440a328 | 6364 | } |
6365 | ||
6366 | if (left_type->is_nil_type() | |
6367 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6368 | { | |
6369 | std::swap(left_type, right_type); | |
2387f644 | 6370 | std::swap(left, right); |
e440a328 | 6371 | } |
6372 | ||
6373 | if (right_type->is_nil_type()) | |
6374 | { | |
2387f644 | 6375 | right = Expression::make_nil(location); |
e440a328 | 6376 | if (left_type->array_type() != NULL |
6377 | && left_type->array_type()->length() == NULL) | |
6378 | { | |
6379 | Array_type* at = left_type->array_type(); | |
2387f644 | 6380 | left = at->get_value_pointer(context->gogo(), left); |
e440a328 | 6381 | } |
6382 | else if (left_type->interface_type() != NULL) | |
6383 | { | |
6384 | // An interface is nil if the first field is nil. | |
2387f644 | 6385 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6386 | } |
6387 | } | |
6388 | ||
ea664253 | 6389 | Bexpression* left_bexpr = left->get_backend(context); |
6390 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6391 | |
a32698ee | 6392 | Gogo* gogo = context->gogo(); |
6393 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6394 | right_bexpr, location); | |
6395 | if (result_type != NULL) | |
6396 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6397 | ret, location); | |
e440a328 | 6398 | return ret; |
6399 | } | |
6400 | ||
736a16ba | 6401 | // Class String_concat_expression. |
6402 | ||
6403 | bool | |
6404 | String_concat_expression::do_is_constant() const | |
6405 | { | |
6406 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6407 | pe != this->exprs_->end(); | |
6408 | ++pe) | |
6409 | { | |
6410 | if (!(*pe)->is_constant()) | |
6411 | return false; | |
6412 | } | |
6413 | return true; | |
6414 | } | |
6415 | ||
6416 | bool | |
3ae06f68 | 6417 | String_concat_expression::do_is_static_initializer() const |
736a16ba | 6418 | { |
6419 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6420 | pe != this->exprs_->end(); | |
6421 | ++pe) | |
6422 | { | |
3ae06f68 | 6423 | if (!(*pe)->is_static_initializer()) |
736a16ba | 6424 | return false; |
6425 | } | |
6426 | return true; | |
6427 | } | |
6428 | ||
6429 | Type* | |
6430 | String_concat_expression::do_type() | |
6431 | { | |
6432 | Type* t = this->exprs_->front()->type(); | |
6433 | Expression_list::iterator pe = this->exprs_->begin(); | |
6434 | ++pe; | |
6435 | for (; pe != this->exprs_->end(); ++pe) | |
6436 | { | |
6437 | Type* t1; | |
6438 | if (!Binary_expression::operation_type(OPERATOR_PLUS, t, | |
6439 | (*pe)->type(), | |
6440 | &t1)) | |
6441 | return Type::make_error_type(); | |
6442 | t = t1; | |
6443 | } | |
6444 | return t; | |
6445 | } | |
6446 | ||
6447 | void | |
6448 | String_concat_expression::do_determine_type(const Type_context* context) | |
6449 | { | |
6450 | Type_context subcontext(*context); | |
6451 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6452 | pe != this->exprs_->end(); | |
6453 | ++pe) | |
6454 | { | |
6455 | Type* t = (*pe)->type(); | |
6456 | if (!t->is_abstract()) | |
6457 | { | |
6458 | subcontext.type = t; | |
6459 | break; | |
6460 | } | |
6461 | } | |
6462 | if (subcontext.type == NULL) | |
6463 | subcontext.type = this->exprs_->front()->type(); | |
6464 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6465 | pe != this->exprs_->end(); | |
6466 | ++pe) | |
6467 | (*pe)->determine_type(&subcontext); | |
6468 | } | |
6469 | ||
6470 | void | |
6471 | String_concat_expression::do_check_types(Gogo*) | |
6472 | { | |
6473 | if (this->is_error_expression()) | |
6474 | return; | |
6475 | Type* t = this->exprs_->front()->type(); | |
6476 | if (t->is_error()) | |
6477 | { | |
6478 | this->set_is_error(); | |
6479 | return; | |
6480 | } | |
6481 | Expression_list::iterator pe = this->exprs_->begin(); | |
6482 | ++pe; | |
6483 | for (; pe != this->exprs_->end(); ++pe) | |
6484 | { | |
6485 | Type* t1 = (*pe)->type(); | |
6486 | if (!Type::are_compatible_for_binop(t, t1)) | |
6487 | { | |
6488 | this->report_error("incompatible types in binary expression"); | |
6489 | return; | |
6490 | } | |
6491 | if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, | |
6492 | this->location())) | |
6493 | { | |
6494 | this->set_is_error(); | |
6495 | return; | |
6496 | } | |
6497 | } | |
6498 | } | |
6499 | ||
6500 | Expression* | |
6501 | String_concat_expression::do_flatten(Gogo*, Named_object*, | |
6502 | Statement_inserter*) | |
6503 | { | |
6504 | if (this->is_error_expression()) | |
6505 | return this; | |
6506 | Location loc = this->location(); | |
6507 | Type* type = this->type(); | |
6508 | Expression* nil_arg = Expression::make_nil(loc); | |
6509 | Expression* call; | |
6510 | switch (this->exprs_->size()) | |
6511 | { | |
6512 | case 0: case 1: | |
6513 | go_unreachable(); | |
6514 | ||
6515 | case 2: case 3: case 4: case 5: | |
6516 | { | |
6517 | Expression* len = Expression::make_integer_ul(this->exprs_->size(), | |
6518 | NULL, loc); | |
6519 | Array_type* arg_type = Type::make_array_type(type, len); | |
6520 | arg_type->set_is_array_incomparable(); | |
6521 | Expression* arg = | |
6522 | Expression::make_array_composite_literal(arg_type, this->exprs_, | |
6523 | loc); | |
6524 | Runtime::Function code; | |
6525 | switch (this->exprs_->size()) | |
6526 | { | |
6527 | default: | |
6528 | go_unreachable(); | |
6529 | case 2: | |
6530 | code = Runtime::CONCATSTRING2; | |
6531 | break; | |
6532 | case 3: | |
6533 | code = Runtime::CONCATSTRING3; | |
6534 | break; | |
6535 | case 4: | |
6536 | code = Runtime::CONCATSTRING4; | |
6537 | break; | |
6538 | case 5: | |
6539 | code = Runtime::CONCATSTRING5; | |
6540 | break; | |
6541 | } | |
6542 | call = Runtime::make_call(code, loc, 2, nil_arg, arg); | |
6543 | } | |
6544 | break; | |
6545 | ||
6546 | default: | |
6547 | { | |
6548 | Type* arg_type = Type::make_array_type(type, NULL); | |
6549 | Slice_construction_expression* sce = | |
6550 | Expression::make_slice_composite_literal(arg_type, this->exprs_, | |
6551 | loc); | |
6552 | sce->set_storage_does_not_escape(); | |
6553 | call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg, | |
6554 | sce); | |
6555 | } | |
6556 | break; | |
6557 | } | |
6558 | ||
6559 | return Expression::make_cast(type, call, loc); | |
6560 | } | |
6561 | ||
6562 | void | |
6563 | String_concat_expression::do_dump_expression( | |
6564 | Ast_dump_context* ast_dump_context) const | |
6565 | { | |
6566 | ast_dump_context->ostream() << "concat("; | |
6567 | ast_dump_context->dump_expression_list(this->exprs_, false); | |
6568 | ast_dump_context->ostream() << ")"; | |
6569 | } | |
6570 | ||
6571 | Expression* | |
6572 | Expression::make_string_concat(Expression_list* exprs) | |
6573 | { | |
6574 | return new String_concat_expression(exprs); | |
6575 | } | |
6576 | ||
e440a328 | 6577 | // Class Bound_method_expression. |
6578 | ||
6579 | // Traversal. | |
6580 | ||
6581 | int | |
6582 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6583 | { | |
e0659c9e | 6584 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6585 | } |
6586 | ||
6587 | // Return the type of a bound method expression. The type of this | |
0afbb937 | 6588 | // object is simply the type of the method with no receiver. |
e440a328 | 6589 | |
6590 | Type* | |
6591 | Bound_method_expression::do_type() | |
6592 | { | |
0afbb937 | 6593 | Named_object* fn = this->method_->named_object(); |
6594 | Function_type* fntype; | |
6595 | if (fn->is_function()) | |
6596 | fntype = fn->func_value()->type(); | |
6597 | else if (fn->is_function_declaration()) | |
6598 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6599 | else |
6600 | return Type::make_error_type(); | |
0afbb937 | 6601 | return fntype->copy_without_receiver(); |
e440a328 | 6602 | } |
6603 | ||
6604 | // Determine the types of a method expression. | |
6605 | ||
6606 | void | |
6607 | Bound_method_expression::do_determine_type(const Type_context*) | |
6608 | { | |
0afbb937 | 6609 | Named_object* fn = this->method_->named_object(); |
6610 | Function_type* fntype; | |
6611 | if (fn->is_function()) | |
6612 | fntype = fn->func_value()->type(); | |
6613 | else if (fn->is_function_declaration()) | |
6614 | fntype = fn->func_declaration_value()->type(); | |
6615 | else | |
6616 | fntype = NULL; | |
e440a328 | 6617 | if (fntype == NULL || !fntype->is_method()) |
6618 | this->expr_->determine_type_no_context(); | |
6619 | else | |
6620 | { | |
6621 | Type_context subcontext(fntype->receiver()->type(), false); | |
6622 | this->expr_->determine_type(&subcontext); | |
6623 | } | |
6624 | } | |
6625 | ||
6626 | // Check the types of a method expression. | |
6627 | ||
6628 | void | |
6629 | Bound_method_expression::do_check_types(Gogo*) | |
6630 | { | |
0afbb937 | 6631 | Named_object* fn = this->method_->named_object(); |
6632 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6633 | { | |
6634 | this->report_error(_("object is not a method")); | |
6635 | return; | |
6636 | } | |
6637 | ||
6638 | Function_type* fntype; | |
6639 | if (fn->is_function()) | |
6640 | fntype = fn->func_value()->type(); | |
6641 | else if (fn->is_function_declaration()) | |
6642 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6643 | else |
0afbb937 | 6644 | go_unreachable(); |
6645 | Type* rtype = fntype->receiver()->type()->deref(); | |
6646 | Type* etype = (this->expr_type_ != NULL | |
6647 | ? this->expr_type_ | |
6648 | : this->expr_->type()); | |
6649 | etype = etype->deref(); | |
6650 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6651 | this->report_error(_("method type does not match object type")); | |
6652 | } | |
6653 | ||
6654 | // If a bound method expression is not simply called, then it is | |
6655 | // represented as a closure. The closure will hold a single variable, | |
6656 | // the receiver to pass to the method. The function will be a simple | |
6657 | // thunk that pulls that value from the closure and calls the method | |
6658 | // with the remaining arguments. | |
6659 | // | |
6660 | // Because method values are not common, we don't build all thunks for | |
6661 | // every methods, but instead only build them as we need them. In | |
6662 | // particular, we even build them on demand for methods defined in | |
6663 | // other packages. | |
6664 | ||
6665 | Bound_method_expression::Method_value_thunks | |
6666 | Bound_method_expression::method_value_thunks; | |
6667 | ||
6668 | // Find or create the thunk for METHOD. | |
6669 | ||
6670 | Named_object* | |
6671 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6672 | Named_object* fn) | |
6673 | { | |
6674 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6675 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6676 | Bound_method_expression::method_value_thunks.insert(val); | |
6677 | if (!ins.second) | |
6678 | { | |
6679 | // We have seen this method before. | |
6680 | go_assert(ins.first->second != NULL); | |
6681 | return ins.first->second; | |
6682 | } | |
6683 | ||
6684 | Location loc = fn->location(); | |
6685 | ||
6686 | Function_type* orig_fntype; | |
6687 | if (fn->is_function()) | |
6688 | orig_fntype = fn->func_value()->type(); | |
6689 | else if (fn->is_function_declaration()) | |
6690 | orig_fntype = fn->func_declaration_value()->type(); | |
6691 | else | |
6692 | orig_fntype = NULL; | |
6693 | ||
6694 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6695 | { |
0afbb937 | 6696 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6697 | return ins.first->second; | |
e440a328 | 6698 | } |
0afbb937 | 6699 | |
6700 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6701 | // The type here is wrong--it should be the C function type. But it |
6702 | // doesn't really matter. | |
0afbb937 | 6703 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6704 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6705 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6706 | orig_fntype->receiver()->type(), | |
6707 | loc))); | |
6708 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
6709 | closure_type = Type::make_pointer_type(closure_type); | |
6710 | ||
f8bdf81a | 6711 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6712 | |
da244e59 | 6713 | std::string thunk_name = Gogo::thunk_name(); |
6714 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 6715 | false, loc); |
6716 | ||
f8bdf81a | 6717 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6718 | cvar->set_is_used(); | |
1ecc6157 | 6719 | cvar->set_is_closure(); |
da244e59 | 6720 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6721 | NULL, cvar); | |
f8bdf81a | 6722 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6723 | |
f8bdf81a | 6724 | gogo->start_block(loc); |
0afbb937 | 6725 | |
6726 | // Field 0 of the closure is the function code pointer, field 1 is | |
6727 | // the value on which to invoke the method. | |
6728 | Expression* arg = Expression::make_var_reference(cp, loc); | |
6729 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
6730 | arg = Expression::make_field_reference(arg, 1, loc); | |
6731 | ||
6732 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6733 | ||
6734 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6735 | Expression_list* args; | |
6736 | if (orig_params == NULL || orig_params->empty()) | |
6737 | args = NULL; | |
6738 | else | |
6739 | { | |
6740 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6741 | args = new Expression_list(); | |
6742 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6743 | p != new_params->end(); |
0afbb937 | 6744 | ++p) |
6745 | { | |
6746 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6747 | go_assert(p_no != NULL | |
6748 | && p_no->is_variable() | |
6749 | && p_no->var_value()->is_parameter()); | |
6750 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6751 | } | |
6752 | } | |
6753 | ||
6754 | Call_expression* call = Expression::make_call(bme, args, | |
6755 | orig_fntype->is_varargs(), | |
6756 | loc); | |
6757 | call->set_varargs_are_lowered(); | |
6758 | ||
6759 | Statement* s = Statement::make_return_from_call(call, loc); | |
6760 | gogo->add_statement(s); | |
6761 | Block* b = gogo->finish_block(loc); | |
6762 | gogo->add_block(b, loc); | |
6763 | gogo->lower_block(new_no, b); | |
a32698ee | 6764 | gogo->flatten_block(new_no, b); |
0afbb937 | 6765 | gogo->finish_function(loc); |
6766 | ||
6767 | ins.first->second = new_no; | |
6768 | return new_no; | |
6769 | } | |
6770 | ||
6771 | // Return an expression to check *REF for nil while dereferencing | |
6772 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6773 | // reference. This is a static function so that we don't have to | |
6774 | // worry about declaring Field_indexes in expressions.h. | |
6775 | ||
6776 | static Expression* | |
6777 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6778 | Expression** ref) | |
6779 | { | |
6780 | if (field_indexes == NULL) | |
6781 | return Expression::make_boolean(false, loc); | |
6782 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6783 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6784 | go_assert(stype != NULL | |
6785 | && field_indexes->field_index < stype->field_count()); | |
6786 | if ((*ref)->type()->struct_type() == NULL) | |
6787 | { | |
6788 | go_assert((*ref)->type()->points_to() != NULL); | |
6789 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6790 | Expression::make_nil(loc), | |
6791 | loc); | |
6792 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
6793 | *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); | |
6794 | go_assert((*ref)->type()->struct_type() == stype); | |
6795 | } | |
6796 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6797 | loc); | |
6798 | return cond; | |
e440a328 | 6799 | } |
6800 | ||
cd39797e | 6801 | // Flatten a method value into a struct with nil checks. We can't do |
6802 | // this in the lowering phase, because if the method value is called | |
6803 | // directly we don't need a thunk. That case will have been handled | |
6804 | // by Call_expression::do_lower, so if we get here then we do need a | |
6805 | // thunk. | |
e440a328 | 6806 | |
cd39797e | 6807 | Expression* |
6808 | Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, | |
6809 | Statement_inserter* inserter) | |
e440a328 | 6810 | { |
cd39797e | 6811 | Location loc = this->location(); |
6812 | ||
6813 | Named_object* thunk = Bound_method_expression::create_thunk(gogo, | |
0afbb937 | 6814 | this->method_, |
6815 | this->function_); | |
6816 | if (thunk->is_erroneous()) | |
6817 | { | |
6818 | go_assert(saw_errors()); | |
cd39797e | 6819 | return Expression::make_error(loc); |
0afbb937 | 6820 | } |
6821 | ||
cd39797e | 6822 | // Force the expression into a variable. This is only necessary if |
6823 | // we are going to do nil checks below, but it's easy enough to | |
6824 | // always do it. | |
6825 | Expression* expr = this->expr_; | |
6826 | if (!expr->is_variable()) | |
6827 | { | |
6828 | Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); | |
6829 | inserter->insert(etemp); | |
6830 | expr = Expression::make_temporary_reference(etemp, loc); | |
6831 | } | |
0afbb937 | 6832 | |
6833 | // If the method expects a value, and we have a pointer, we need to | |
6834 | // dereference the pointer. | |
6835 | ||
6836 | Named_object* fn = this->method_->named_object(); | |
cd39797e | 6837 | Function_type *fntype; |
0afbb937 | 6838 | if (fn->is_function()) |
6839 | fntype = fn->func_value()->type(); | |
6840 | else if (fn->is_function_declaration()) | |
6841 | fntype = fn->func_declaration_value()->type(); | |
6842 | else | |
6843 | go_unreachable(); | |
6844 | ||
cd39797e | 6845 | Expression* val = expr; |
0afbb937 | 6846 | if (fntype->receiver()->type()->points_to() == NULL |
6847 | && val->type()->points_to() != NULL) | |
6848 | val = Expression::make_unary(OPERATOR_MULT, val, loc); | |
6849 | ||
6850 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
6851 | // expect a closure whose second field has type this->expr_type_ (if | |
6852 | // that is not NULL). We are going to pass it a closure whose | |
6853 | // second field has type this->expr_->type(). Since | |
6854 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
6855 | // away with this. | |
6856 | ||
6857 | Struct_field_list* fields = new Struct_field_list(); | |
6858 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
6859 | thunk->func_value()->type(), | |
6860 | loc))); | |
6861 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
6862 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6863 | ||
6864 | Expression_list* vals = new Expression_list(); | |
6865 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
6866 | vals->push_back(val); | |
6867 | ||
6868 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
0afbb937 | 6869 | |
cd39797e | 6870 | if (!gogo->compiling_runtime() || gogo->package_name() != "runtime") |
6871 | ret = Expression::make_heap_expression(ret, loc); | |
6872 | else | |
6873 | { | |
6874 | // When compiling the runtime, method closures do not escape. | |
6875 | // When escape analysis becomes the default, and applies to | |
6876 | // method closures, this should be changed to make it an error | |
6877 | // if a method closure escapes. | |
6878 | Temporary_statement* ctemp = Statement::make_temporary(st, ret, loc); | |
6879 | inserter->insert(ctemp); | |
6880 | ret = Expression::make_temporary_reference(ctemp, loc); | |
6881 | ret = Expression::make_unary(OPERATOR_AND, ret, loc); | |
6882 | ret->unary_expression()->set_does_not_escape(); | |
6883 | } | |
6884 | ||
6885 | // If necessary, check whether the expression or any embedded | |
6886 | // pointers are nil. | |
0afbb937 | 6887 | |
df7ef1fd | 6888 | Expression* nil_check = NULL; |
0afbb937 | 6889 | if (this->method_->field_indexes() != NULL) |
6890 | { | |
0afbb937 | 6891 | Expression* ref = expr; |
6892 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
6893 | expr = ref; | |
6894 | } | |
6895 | ||
6896 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
6897 | { | |
6898 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
6899 | Expression::make_nil(loc), | |
6900 | loc); | |
6901 | if (nil_check == NULL) | |
6902 | nil_check = n; | |
6903 | else | |
6904 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
6905 | } | |
6906 | ||
6907 | if (nil_check != NULL) | |
6908 | { | |
cd39797e | 6909 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
6910 | loc); | |
6911 | // Fix the type of the conditional expression by pretending to | |
6912 | // evaluate to RET either way through the conditional. | |
6913 | crash = Expression::make_compound(crash, ret, loc); | |
6914 | ret = Expression::make_conditional(nil_check, crash, ret, loc); | |
6915 | } | |
6916 | ||
6917 | // RET is a pointer to a struct, but we want a function type. | |
6918 | ret = Expression::make_unsafe_cast(this->type(), ret, loc); | |
6919 | ||
6920 | return ret; | |
e440a328 | 6921 | } |
6922 | ||
d751bb78 | 6923 | // Dump ast representation of a bound method expression. |
6924 | ||
6925 | void | |
6926 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6927 | const | |
6928 | { | |
6929 | if (this->expr_type_ != NULL) | |
6930 | ast_dump_context->ostream() << "("; | |
6931 | ast_dump_context->dump_expression(this->expr_); | |
6932 | if (this->expr_type_ != NULL) | |
6933 | { | |
6934 | ast_dump_context->ostream() << ":"; | |
6935 | ast_dump_context->dump_type(this->expr_type_); | |
6936 | ast_dump_context->ostream() << ")"; | |
6937 | } | |
6938 | ||
0afbb937 | 6939 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 6940 | } |
6941 | ||
e440a328 | 6942 | // Make a method expression. |
6943 | ||
6944 | Bound_method_expression* | |
0afbb937 | 6945 | Expression::make_bound_method(Expression* expr, const Method* method, |
6946 | Named_object* function, Location location) | |
e440a328 | 6947 | { |
0afbb937 | 6948 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 6949 | } |
6950 | ||
6951 | // Class Builtin_call_expression. This is used for a call to a | |
6952 | // builtin function. | |
6953 | ||
6954 | class Builtin_call_expression : public Call_expression | |
6955 | { | |
6956 | public: | |
6957 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6958 | bool is_varargs, Location location); |
e440a328 | 6959 | |
6960 | protected: | |
6961 | // This overrides Call_expression::do_lower. | |
6962 | Expression* | |
ceeb4318 | 6963 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6964 | |
35a54f17 | 6965 | Expression* |
6966 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
6967 | ||
e440a328 | 6968 | bool |
6969 | do_is_constant() const; | |
6970 | ||
6971 | bool | |
0c77715b | 6972 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6973 | |
4f2138d7 | 6974 | bool |
a7549a6a | 6975 | do_discarding_value(); |
6976 | ||
e440a328 | 6977 | Type* |
6978 | do_type(); | |
6979 | ||
6980 | void | |
6981 | do_determine_type(const Type_context*); | |
6982 | ||
6983 | void | |
6984 | do_check_types(Gogo*); | |
6985 | ||
6986 | Expression* | |
72666aed | 6987 | do_copy(); |
e440a328 | 6988 | |
ea664253 | 6989 | Bexpression* |
6990 | do_get_backend(Translate_context*); | |
e440a328 | 6991 | |
6992 | void | |
6993 | do_export(Export*) const; | |
6994 | ||
6995 | virtual bool | |
6996 | do_is_recover_call() const; | |
6997 | ||
6998 | virtual void | |
6999 | do_set_recover_arg(Expression*); | |
7000 | ||
7001 | private: | |
7002 | // The builtin functions. | |
7003 | enum Builtin_function_code | |
7004 | { | |
7005 | BUILTIN_INVALID, | |
7006 | ||
7007 | // Predeclared builtin functions. | |
7008 | BUILTIN_APPEND, | |
7009 | BUILTIN_CAP, | |
7010 | BUILTIN_CLOSE, | |
48080209 | 7011 | BUILTIN_COMPLEX, |
e440a328 | 7012 | BUILTIN_COPY, |
1cce762f | 7013 | BUILTIN_DELETE, |
e440a328 | 7014 | BUILTIN_IMAG, |
7015 | BUILTIN_LEN, | |
7016 | BUILTIN_MAKE, | |
7017 | BUILTIN_NEW, | |
7018 | BUILTIN_PANIC, | |
7019 | BUILTIN_PRINT, | |
7020 | BUILTIN_PRINTLN, | |
7021 | BUILTIN_REAL, | |
7022 | BUILTIN_RECOVER, | |
7023 | ||
7024 | // Builtin functions from the unsafe package. | |
7025 | BUILTIN_ALIGNOF, | |
7026 | BUILTIN_OFFSETOF, | |
7027 | BUILTIN_SIZEOF | |
7028 | }; | |
7029 | ||
7030 | Expression* | |
7031 | one_arg() const; | |
7032 | ||
7033 | bool | |
7034 | check_one_arg(); | |
7035 | ||
7036 | static Type* | |
7037 | real_imag_type(Type*); | |
7038 | ||
7039 | static Type* | |
48080209 | 7040 | complex_type(Type*); |
e440a328 | 7041 | |
a9182619 | 7042 | Expression* |
321e5ad2 | 7043 | lower_make(Statement_inserter*); |
7044 | ||
7045 | Expression* flatten_append(Gogo*, Named_object*, Statement_inserter*); | |
a9182619 | 7046 | |
7047 | bool | |
1ad00fd4 | 7048 | check_int_value(Expression*, bool is_length); |
a9182619 | 7049 | |
e440a328 | 7050 | // A pointer back to the general IR structure. This avoids a global |
7051 | // variable, or passing it around everywhere. | |
7052 | Gogo* gogo_; | |
7053 | // The builtin function being called. | |
7054 | Builtin_function_code code_; | |
0f914071 | 7055 | // Used to stop endless loops when the length of an array uses len |
7056 | // or cap of the array itself. | |
7057 | mutable bool seen_; | |
6334270b | 7058 | // Whether the argument is set for calls to BUILTIN_RECOVER. |
7059 | bool recover_arg_is_set_; | |
e440a328 | 7060 | }; |
7061 | ||
7062 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
7063 | Expression* fn, | |
7064 | Expression_list* args, | |
7065 | bool is_varargs, | |
b13c66cd | 7066 | Location location) |
e440a328 | 7067 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 7068 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
7069 | recover_arg_is_set_(false) | |
e440a328 | 7070 | { |
7071 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 7072 | if (fnexp == NULL) |
7073 | { | |
7074 | this->code_ = BUILTIN_INVALID; | |
7075 | return; | |
7076 | } | |
e440a328 | 7077 | const std::string& name(fnexp->named_object()->name()); |
7078 | if (name == "append") | |
7079 | this->code_ = BUILTIN_APPEND; | |
7080 | else if (name == "cap") | |
7081 | this->code_ = BUILTIN_CAP; | |
7082 | else if (name == "close") | |
7083 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 7084 | else if (name == "complex") |
7085 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 7086 | else if (name == "copy") |
7087 | this->code_ = BUILTIN_COPY; | |
1cce762f | 7088 | else if (name == "delete") |
7089 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 7090 | else if (name == "imag") |
7091 | this->code_ = BUILTIN_IMAG; | |
7092 | else if (name == "len") | |
7093 | this->code_ = BUILTIN_LEN; | |
7094 | else if (name == "make") | |
7095 | this->code_ = BUILTIN_MAKE; | |
7096 | else if (name == "new") | |
7097 | this->code_ = BUILTIN_NEW; | |
7098 | else if (name == "panic") | |
7099 | this->code_ = BUILTIN_PANIC; | |
7100 | else if (name == "print") | |
7101 | this->code_ = BUILTIN_PRINT; | |
7102 | else if (name == "println") | |
7103 | this->code_ = BUILTIN_PRINTLN; | |
7104 | else if (name == "real") | |
7105 | this->code_ = BUILTIN_REAL; | |
7106 | else if (name == "recover") | |
7107 | this->code_ = BUILTIN_RECOVER; | |
7108 | else if (name == "Alignof") | |
7109 | this->code_ = BUILTIN_ALIGNOF; | |
7110 | else if (name == "Offsetof") | |
7111 | this->code_ = BUILTIN_OFFSETOF; | |
7112 | else if (name == "Sizeof") | |
7113 | this->code_ = BUILTIN_SIZEOF; | |
7114 | else | |
c3e6f413 | 7115 | go_unreachable(); |
e440a328 | 7116 | } |
7117 | ||
7118 | // Return whether this is a call to recover. This is a virtual | |
7119 | // function called from the parent class. | |
7120 | ||
7121 | bool | |
7122 | Builtin_call_expression::do_is_recover_call() const | |
7123 | { | |
7124 | if (this->classification() == EXPRESSION_ERROR) | |
7125 | return false; | |
7126 | return this->code_ == BUILTIN_RECOVER; | |
7127 | } | |
7128 | ||
7129 | // Set the argument for a call to recover. | |
7130 | ||
7131 | void | |
7132 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7133 | { | |
7134 | const Expression_list* args = this->args(); | |
c484d925 | 7135 | go_assert(args == NULL || args->empty()); |
e440a328 | 7136 | Expression_list* new_args = new Expression_list(); |
7137 | new_args->push_back(arg); | |
7138 | this->set_args(new_args); | |
6334270b | 7139 | this->recover_arg_is_set_ = true; |
e440a328 | 7140 | } |
7141 | ||
e440a328 | 7142 | // Lower a builtin call expression. This turns new and make into |
7143 | // specific expressions. We also convert to a constant if we can. | |
7144 | ||
7145 | Expression* | |
321e5ad2 | 7146 | Builtin_call_expression::do_lower(Gogo*, Named_object* function, |
ceeb4318 | 7147 | Statement_inserter* inserter, int) |
e440a328 | 7148 | { |
79651b1f | 7149 | if (this->is_error_expression()) |
a9182619 | 7150 | return this; |
7151 | ||
b13c66cd | 7152 | Location loc = this->location(); |
1cce762f | 7153 | |
a8725655 | 7154 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7155 | { | |
7156 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7157 | return Expression::make_error(loc); |
a8725655 | 7158 | } |
7159 | ||
393ba00b | 7160 | if (this->code_ == BUILTIN_OFFSETOF) |
7161 | { | |
7162 | Expression* arg = this->one_arg(); | |
12e69faa | 7163 | |
7164 | if (arg->bound_method_expression() != NULL | |
7165 | || arg->interface_field_reference_expression() != NULL) | |
7166 | { | |
7167 | this->report_error(_("invalid use of method value as argument " | |
7168 | "of Offsetof")); | |
7169 | return this; | |
7170 | } | |
7171 | ||
393ba00b | 7172 | Field_reference_expression* farg = arg->field_reference_expression(); |
7173 | while (farg != NULL) | |
7174 | { | |
7175 | if (!farg->implicit()) | |
7176 | break; | |
7177 | // When the selector refers to an embedded field, | |
7178 | // it must not be reached through pointer indirections. | |
7179 | if (farg->expr()->deref() != farg->expr()) | |
7180 | { | |
12e69faa | 7181 | this->report_error(_("argument of Offsetof implies " |
7182 | "indirection of an embedded field")); | |
393ba00b | 7183 | return this; |
7184 | } | |
7185 | // Go up until we reach the original base. | |
7186 | farg = farg->expr()->field_reference_expression(); | |
7187 | } | |
7188 | } | |
7189 | ||
1cce762f | 7190 | if (this->is_constant()) |
e440a328 | 7191 | { |
0c77715b | 7192 | Numeric_constant nc; |
7193 | if (this->numeric_constant_value(&nc)) | |
7194 | return nc.expression(loc); | |
e440a328 | 7195 | } |
1cce762f | 7196 | |
7197 | switch (this->code_) | |
e440a328 | 7198 | { |
1cce762f | 7199 | default: |
7200 | break; | |
7201 | ||
7202 | case BUILTIN_NEW: | |
7203 | { | |
7204 | const Expression_list* args = this->args(); | |
7205 | if (args == NULL || args->size() < 1) | |
7206 | this->report_error(_("not enough arguments")); | |
7207 | else if (args->size() > 1) | |
7208 | this->report_error(_("too many arguments")); | |
7209 | else | |
7210 | { | |
7211 | Expression* arg = args->front(); | |
7212 | if (!arg->is_type_expression()) | |
7213 | { | |
631d5788 | 7214 | go_error_at(arg->location(), "expected type"); |
1cce762f | 7215 | this->set_is_error(); |
7216 | } | |
7217 | else | |
7218 | return Expression::make_allocation(arg->type(), loc); | |
7219 | } | |
7220 | } | |
7221 | break; | |
7222 | ||
7223 | case BUILTIN_MAKE: | |
321e5ad2 | 7224 | return this->lower_make(inserter); |
1cce762f | 7225 | |
7226 | case BUILTIN_RECOVER: | |
e440a328 | 7227 | if (function != NULL) |
7228 | function->func_value()->set_calls_recover(); | |
7229 | else | |
7230 | { | |
7231 | // Calling recover outside of a function always returns the | |
7232 | // nil empty interface. | |
823c7e3d | 7233 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7234 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7235 | } |
1cce762f | 7236 | break; |
7237 | ||
1cce762f | 7238 | case BUILTIN_DELETE: |
7239 | { | |
7240 | // Lower to a runtime function call. | |
7241 | const Expression_list* args = this->args(); | |
7242 | if (args == NULL || args->size() < 2) | |
7243 | this->report_error(_("not enough arguments")); | |
7244 | else if (args->size() > 2) | |
7245 | this->report_error(_("too many arguments")); | |
7246 | else if (args->front()->type()->map_type() == NULL) | |
7247 | this->report_error(_("argument 1 must be a map")); | |
7248 | else | |
7249 | { | |
7250 | // Since this function returns no value it must appear in | |
7251 | // a statement by itself, so we don't have to worry about | |
7252 | // order of evaluation of values around it. Evaluate the | |
7253 | // map first to get order of evaluation right. | |
7254 | Map_type* mt = args->front()->type()->map_type(); | |
7255 | Temporary_statement* map_temp = | |
7256 | Statement::make_temporary(mt, args->front(), loc); | |
7257 | inserter->insert(map_temp); | |
7258 | ||
7259 | Temporary_statement* key_temp = | |
7260 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7261 | inserter->insert(key_temp); | |
7262 | ||
0d5530d9 | 7263 | Expression* e1 = Expression::make_type_descriptor(mt, loc); |
7264 | Expression* e2 = Expression::make_temporary_reference(map_temp, | |
1cce762f | 7265 | loc); |
0d5530d9 | 7266 | Expression* e3 = Expression::make_temporary_reference(key_temp, |
1cce762f | 7267 | loc); |
0d5530d9 | 7268 | e3 = Expression::make_unary(OPERATOR_AND, e3, loc); |
1cce762f | 7269 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), |
0d5530d9 | 7270 | 3, e1, e2, e3); |
1cce762f | 7271 | } |
7272 | } | |
7273 | break; | |
88b03a70 | 7274 | |
7275 | case BUILTIN_PRINT: | |
7276 | case BUILTIN_PRINTLN: | |
7277 | // Force all the arguments into temporary variables, so that we | |
7278 | // don't try to evaluate something while holding the print lock. | |
7279 | if (this->args() == NULL) | |
7280 | break; | |
7281 | for (Expression_list::iterator pa = this->args()->begin(); | |
7282 | pa != this->args()->end(); | |
7283 | ++pa) | |
7284 | { | |
493ce3ee | 7285 | if (!(*pa)->is_variable() && !(*pa)->is_constant()) |
88b03a70 | 7286 | { |
7287 | Temporary_statement* temp = | |
7288 | Statement::make_temporary(NULL, *pa, loc); | |
7289 | inserter->insert(temp); | |
7290 | *pa = Expression::make_temporary_reference(temp, loc); | |
7291 | } | |
7292 | } | |
7293 | break; | |
e440a328 | 7294 | } |
7295 | ||
7296 | return this; | |
7297 | } | |
7298 | ||
35a54f17 | 7299 | // Flatten a builtin call expression. This turns the arguments of copy and |
7300 | // append into temporary expressions. | |
7301 | ||
7302 | Expression* | |
321e5ad2 | 7303 | Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function, |
35a54f17 | 7304 | Statement_inserter* inserter) |
7305 | { | |
16cb7fec | 7306 | Location loc = this->location(); |
7307 | ||
7308 | switch (this->code_) | |
35a54f17 | 7309 | { |
16cb7fec | 7310 | default: |
7311 | break; | |
7312 | ||
7313 | case BUILTIN_APPEND: | |
321e5ad2 | 7314 | return this->flatten_append(gogo, function, inserter); |
7315 | ||
16cb7fec | 7316 | case BUILTIN_COPY: |
7317 | { | |
7318 | Type* at = this->args()->front()->type(); | |
7319 | for (Expression_list::iterator pa = this->args()->begin(); | |
7320 | pa != this->args()->end(); | |
7321 | ++pa) | |
7322 | { | |
7323 | if ((*pa)->is_nil_expression()) | |
7324 | { | |
7325 | Expression* nil = Expression::make_nil(loc); | |
7326 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
7327 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
7328 | } | |
7329 | if (!(*pa)->is_variable()) | |
7330 | { | |
7331 | Temporary_statement* temp = | |
7332 | Statement::make_temporary(NULL, *pa, loc); | |
7333 | inserter->insert(temp); | |
7334 | *pa = Expression::make_temporary_reference(temp, loc); | |
7335 | } | |
7336 | } | |
7337 | } | |
7338 | break; | |
7339 | ||
7340 | case BUILTIN_PANIC: | |
35a54f17 | 7341 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 7342 | pa != this->args()->end(); |
7343 | ++pa) | |
7344 | { | |
7345 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 7346 | { |
16cb7fec | 7347 | Temporary_statement* temp = |
7348 | Statement::make_temporary(NULL, *pa, loc); | |
7349 | inserter->insert(temp); | |
7350 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 7351 | } |
16cb7fec | 7352 | } |
7739537f | 7353 | break; |
0d5530d9 | 7354 | |
7355 | case BUILTIN_LEN: | |
132ed071 | 7356 | case BUILTIN_CAP: |
321e5ad2 | 7357 | { |
7358 | Expression_list::iterator pa = this->args()->begin(); | |
7359 | if (!(*pa)->is_variable() | |
7360 | && ((*pa)->type()->map_type() != NULL | |
7361 | || (*pa)->type()->channel_type() != NULL)) | |
7362 | { | |
7363 | Temporary_statement* temp = | |
7364 | Statement::make_temporary(NULL, *pa, loc); | |
7365 | inserter->insert(temp); | |
7366 | *pa = Expression::make_temporary_reference(temp, loc); | |
7367 | } | |
7368 | } | |
7369 | break; | |
35a54f17 | 7370 | } |
16cb7fec | 7371 | |
35a54f17 | 7372 | return this; |
7373 | } | |
7374 | ||
a9182619 | 7375 | // Lower a make expression. |
7376 | ||
7377 | Expression* | |
321e5ad2 | 7378 | Builtin_call_expression::lower_make(Statement_inserter* inserter) |
a9182619 | 7379 | { |
b13c66cd | 7380 | Location loc = this->location(); |
a9182619 | 7381 | |
7382 | const Expression_list* args = this->args(); | |
7383 | if (args == NULL || args->size() < 1) | |
7384 | { | |
7385 | this->report_error(_("not enough arguments")); | |
7386 | return Expression::make_error(this->location()); | |
7387 | } | |
7388 | ||
7389 | Expression_list::const_iterator parg = args->begin(); | |
7390 | ||
7391 | Expression* first_arg = *parg; | |
7392 | if (!first_arg->is_type_expression()) | |
7393 | { | |
631d5788 | 7394 | go_error_at(first_arg->location(), "expected type"); |
a9182619 | 7395 | this->set_is_error(); |
7396 | return Expression::make_error(this->location()); | |
7397 | } | |
7398 | Type* type = first_arg->type(); | |
7399 | ||
7400 | bool is_slice = false; | |
7401 | bool is_map = false; | |
7402 | bool is_chan = false; | |
411eb89e | 7403 | if (type->is_slice_type()) |
a9182619 | 7404 | is_slice = true; |
7405 | else if (type->map_type() != NULL) | |
7406 | is_map = true; | |
7407 | else if (type->channel_type() != NULL) | |
7408 | is_chan = true; | |
7409 | else | |
7410 | { | |
7411 | this->report_error(_("invalid type for make function")); | |
7412 | return Expression::make_error(this->location()); | |
7413 | } | |
7414 | ||
f6bc81e6 | 7415 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7416 | ||
a9182619 | 7417 | ++parg; |
7418 | Expression* len_arg; | |
7419 | if (parg == args->end()) | |
7420 | { | |
7421 | if (is_slice) | |
7422 | { | |
7423 | this->report_error(_("length required when allocating a slice")); | |
7424 | return Expression::make_error(this->location()); | |
7425 | } | |
e67508fa | 7426 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
a9182619 | 7427 | } |
7428 | else | |
7429 | { | |
7430 | len_arg = *parg; | |
f6bc81e6 | 7431 | len_arg->determine_type(&int_context); |
1ad00fd4 | 7432 | if (!this->check_int_value(len_arg, true)) |
7433 | return Expression::make_error(this->location()); | |
a9182619 | 7434 | ++parg; |
7435 | } | |
7436 | ||
7437 | Expression* cap_arg = NULL; | |
7438 | if (is_slice && parg != args->end()) | |
7439 | { | |
7440 | cap_arg = *parg; | |
f6bc81e6 | 7441 | cap_arg->determine_type(&int_context); |
1ad00fd4 | 7442 | if (!this->check_int_value(cap_arg, false)) |
7443 | return Expression::make_error(this->location()); | |
7444 | ||
7445 | Numeric_constant nclen; | |
7446 | Numeric_constant nccap; | |
7447 | unsigned long vlen; | |
7448 | unsigned long vcap; | |
7449 | if (len_arg->numeric_constant_value(&nclen) | |
7450 | && cap_arg->numeric_constant_value(&nccap) | |
7451 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7452 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7453 | && vlen > vcap) | |
a9182619 | 7454 | { |
1ad00fd4 | 7455 | this->report_error(_("len larger than cap")); |
a9182619 | 7456 | return Expression::make_error(this->location()); |
7457 | } | |
1ad00fd4 | 7458 | |
a9182619 | 7459 | ++parg; |
7460 | } | |
7461 | ||
7462 | if (parg != args->end()) | |
7463 | { | |
7464 | this->report_error(_("too many arguments to make")); | |
7465 | return Expression::make_error(this->location()); | |
7466 | } | |
7467 | ||
b13c66cd | 7468 | Location type_loc = first_arg->location(); |
a9182619 | 7469 | |
7470 | Expression* call; | |
7471 | if (is_slice) | |
7472 | { | |
321e5ad2 | 7473 | Type* et = type->array_type()->element_type(); |
7474 | Expression* type_arg = Expression::make_type_descriptor(et, type_loc); | |
a9182619 | 7475 | if (cap_arg == NULL) |
321e5ad2 | 7476 | { |
7477 | Temporary_statement* temp = Statement::make_temporary(NULL, | |
7478 | len_arg, | |
7479 | loc); | |
7480 | inserter->insert(temp); | |
7481 | len_arg = Expression::make_temporary_reference(temp, loc); | |
7482 | cap_arg = Expression::make_temporary_reference(temp, loc); | |
7483 | } | |
7484 | call = Runtime::make_call(Runtime::MAKESLICE, loc, 3, type_arg, | |
7485 | len_arg, cap_arg); | |
a9182619 | 7486 | } |
7487 | else if (is_map) | |
321e5ad2 | 7488 | { |
7489 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
7490 | call = Runtime::make_call(Runtime::MAKEMAP, loc, 4, type_arg, len_arg, | |
7491 | Expression::make_nil(loc), | |
7492 | Expression::make_nil(loc)); | |
7493 | } | |
a9182619 | 7494 | else if (is_chan) |
321e5ad2 | 7495 | { |
7496 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
7497 | call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg); | |
7498 | } | |
a9182619 | 7499 | else |
7500 | go_unreachable(); | |
7501 | ||
7502 | return Expression::make_unsafe_cast(type, call, loc); | |
7503 | } | |
7504 | ||
321e5ad2 | 7505 | // Flatten a call to the predeclared append function. We do this in |
7506 | // the flatten phase, not the lowering phase, so that we run after | |
7507 | // type checking and after order_evaluations. | |
7508 | ||
7509 | Expression* | |
7510 | Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function, | |
7511 | Statement_inserter* inserter) | |
7512 | { | |
7513 | if (this->is_error_expression()) | |
7514 | return this; | |
7515 | ||
7516 | Location loc = this->location(); | |
7517 | ||
7518 | const Expression_list* args = this->args(); | |
7519 | go_assert(args != NULL && !args->empty()); | |
7520 | ||
7521 | Type* slice_type = args->front()->type(); | |
7522 | go_assert(slice_type->is_slice_type()); | |
7523 | Type* element_type = slice_type->array_type()->element_type(); | |
7524 | ||
7525 | if (args->size() == 1) | |
7526 | { | |
7527 | // append(s) evaluates to s. | |
7528 | return args->front(); | |
7529 | } | |
7530 | ||
7531 | Type* int_type = Type::lookup_integer_type("int"); | |
7532 | Type* uint_type = Type::lookup_integer_type("uint"); | |
7533 | ||
7534 | // Implementing | |
7535 | // append(s1, s2...) | |
7536 | // or | |
7537 | // append(s1, a1, a2, a3, ...) | |
7538 | ||
7539 | // s1tmp := s1 | |
7540 | Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(), | |
7541 | loc); | |
7542 | inserter->insert(s1tmp); | |
7543 | ||
7544 | // l1tmp := len(s1tmp) | |
7545 | Named_object* lenfn = gogo->lookup_global("len"); | |
7546 | Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7547 | Expression_list* call_args = new Expression_list(); | |
7548 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7549 | Expression* len = Expression::make_call(lenref, call_args, false, loc); | |
7550 | gogo->lower_expression(function, inserter, &len); | |
7551 | gogo->flatten_expression(function, inserter, &len); | |
7552 | Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc); | |
7553 | inserter->insert(l1tmp); | |
7554 | ||
7555 | Temporary_statement* s2tmp = NULL; | |
7556 | Temporary_statement* l2tmp = NULL; | |
7557 | Expression_list* add = NULL; | |
7558 | Expression* len2; | |
7559 | if (this->is_varargs()) | |
7560 | { | |
7561 | go_assert(args->size() == 2); | |
7562 | ||
7563 | // s2tmp := s2 | |
7564 | s2tmp = Statement::make_temporary(NULL, args->back(), loc); | |
7565 | inserter->insert(s2tmp); | |
7566 | ||
7567 | // l2tmp := len(s2tmp) | |
7568 | lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7569 | call_args = new Expression_list(); | |
7570 | call_args->push_back(Expression::make_temporary_reference(s2tmp, loc)); | |
7571 | len = Expression::make_call(lenref, call_args, false, loc); | |
7572 | gogo->lower_expression(function, inserter, &len); | |
7573 | gogo->flatten_expression(function, inserter, &len); | |
7574 | l2tmp = Statement::make_temporary(int_type, len, loc); | |
7575 | inserter->insert(l2tmp); | |
7576 | ||
7577 | // len2 = l2tmp | |
7578 | len2 = Expression::make_temporary_reference(l2tmp, loc); | |
7579 | } | |
7580 | else | |
7581 | { | |
7582 | // We have to ensure that all the arguments are in variables | |
7583 | // now, because otherwise if one of them is an index expression | |
7584 | // into the current slice we could overwrite it before we fetch | |
7585 | // it. | |
7586 | add = new Expression_list(); | |
7587 | Expression_list::const_iterator pa = args->begin(); | |
7588 | for (++pa; pa != args->end(); ++pa) | |
7589 | { | |
7590 | if ((*pa)->is_variable()) | |
7591 | add->push_back(*pa); | |
7592 | else | |
7593 | { | |
7594 | Temporary_statement* tmp = Statement::make_temporary(NULL, *pa, | |
7595 | loc); | |
7596 | inserter->insert(tmp); | |
7597 | add->push_back(Expression::make_temporary_reference(tmp, loc)); | |
7598 | } | |
7599 | } | |
7600 | ||
7601 | // len2 = len(add) | |
7602 | len2 = Expression::make_integer_ul(add->size(), int_type, loc); | |
7603 | } | |
7604 | ||
7605 | // ntmp := l1tmp + len2 | |
7606 | Expression* ref = Expression::make_temporary_reference(l1tmp, loc); | |
7607 | Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc); | |
7608 | gogo->lower_expression(function, inserter, &sum); | |
7609 | gogo->flatten_expression(function, inserter, &sum); | |
7610 | Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc); | |
7611 | inserter->insert(ntmp); | |
7612 | ||
7613 | // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ? | |
7614 | // growslice(type, s1tmp, ntmp) : | |
7615 | // s1tmp[:ntmp] | |
7616 | // Using uint here means that if the computation of ntmp overflowed, | |
7617 | // we will call growslice which will panic. | |
7618 | ||
7619 | Expression* left = Expression::make_temporary_reference(ntmp, loc); | |
7620 | left = Expression::make_cast(uint_type, left, loc); | |
7621 | ||
7622 | Named_object* capfn = gogo->lookup_global("cap"); | |
7623 | Expression* capref = Expression::make_func_reference(capfn, NULL, loc); | |
7624 | call_args = new Expression_list(); | |
7625 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7626 | Expression* right = Expression::make_call(capref, call_args, false, loc); | |
7627 | right = Expression::make_cast(uint_type, right, loc); | |
7628 | ||
7629 | Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc); | |
7630 | ||
7631 | Expression* a1 = Expression::make_type_descriptor(element_type, loc); | |
7632 | Expression* a2 = Expression::make_temporary_reference(s1tmp, loc); | |
7633 | Expression* a3 = Expression::make_temporary_reference(ntmp, loc); | |
7634 | Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3, | |
7635 | a1, a2, a3); | |
7636 | call = Expression::make_unsafe_cast(slice_type, call, loc); | |
7637 | ||
7638 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7639 | Expression* zero = Expression::make_integer_ul(0, int_type, loc); | |
7640 | Expression* ref2 = Expression::make_temporary_reference(ntmp, loc); | |
7641 | // FIXME: Mark this index as not requiring bounds checks. | |
7642 | ref = Expression::make_index(ref, zero, ref2, NULL, loc); | |
7643 | ||
7644 | Expression* rhs = Expression::make_conditional(cond, call, ref, loc); | |
7645 | ||
7646 | gogo->lower_expression(function, inserter, &rhs); | |
7647 | gogo->flatten_expression(function, inserter, &rhs); | |
7648 | ||
7649 | Expression* lhs = Expression::make_temporary_reference(s1tmp, loc); | |
7650 | Statement* assign = Statement::make_assignment(lhs, rhs, loc); | |
7651 | inserter->insert(assign); | |
7652 | ||
7653 | if (this->is_varargs()) | |
7654 | { | |
7655 | // copy(s1tmp[l1tmp:], s2tmp) | |
7656 | a1 = Expression::make_temporary_reference(s1tmp, loc); | |
7657 | ref = Expression::make_temporary_reference(l1tmp, loc); | |
7658 | Expression* nil = Expression::make_nil(loc); | |
7659 | // FIXME: Mark this index as not requiring bounds checks. | |
7660 | a1 = Expression::make_index(a1, ref, nil, NULL, loc); | |
7661 | ||
7662 | a2 = Expression::make_temporary_reference(s2tmp, loc); | |
7663 | ||
7664 | Named_object* copyfn = gogo->lookup_global("copy"); | |
7665 | Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc); | |
7666 | call_args = new Expression_list(); | |
7667 | call_args->push_back(a1); | |
7668 | call_args->push_back(a2); | |
7669 | call = Expression::make_call(copyref, call_args, false, loc); | |
7670 | gogo->lower_expression(function, inserter, &call); | |
7671 | gogo->flatten_expression(function, inserter, &call); | |
7672 | inserter->insert(Statement::make_statement(call, false)); | |
7673 | } | |
7674 | else | |
7675 | { | |
7676 | // For each argument: | |
7677 | // s1tmp[l1tmp+i] = a | |
7678 | unsigned long i = 0; | |
7679 | for (Expression_list::const_iterator pa = add->begin(); | |
7680 | pa != add->end(); | |
7681 | ++pa, ++i) | |
7682 | { | |
7683 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7684 | ref2 = Expression::make_temporary_reference(l1tmp, loc); | |
7685 | Expression* off = Expression::make_integer_ul(i, int_type, loc); | |
7686 | ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc); | |
7687 | // FIXME: Mark this index as not requiring bounds checks. | |
7688 | lhs = Expression::make_index(ref, ref2, NULL, NULL, loc); | |
7689 | gogo->lower_expression(function, inserter, &lhs); | |
7690 | gogo->flatten_expression(function, inserter, &lhs); | |
7691 | assign = Statement::make_assignment(lhs, *pa, loc); | |
7692 | inserter->insert(assign); | |
7693 | } | |
7694 | } | |
7695 | ||
7696 | return Expression::make_temporary_reference(s1tmp, loc); | |
7697 | } | |
7698 | ||
a9182619 | 7699 | // Return whether an expression has an integer value. Report an error |
7700 | // if not. This is used when handling calls to the predeclared make | |
7701 | // function. | |
7702 | ||
7703 | bool | |
1ad00fd4 | 7704 | Builtin_call_expression::check_int_value(Expression* e, bool is_length) |
a9182619 | 7705 | { |
0c77715b | 7706 | Numeric_constant nc; |
1ad00fd4 | 7707 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7708 | { |
1ad00fd4 | 7709 | unsigned long v; |
7710 | switch (nc.to_unsigned_long(&v)) | |
7711 | { | |
7712 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7713 | break; |
1ad00fd4 | 7714 | case Numeric_constant::NC_UL_NOTINT: |
631d5788 | 7715 | go_error_at(e->location(), "non-integer %s argument to make", |
7716 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7717 | return false; |
7718 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 7719 | go_error_at(e->location(), "negative %s argument to make", |
7720 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7721 | return false; |
7722 | case Numeric_constant::NC_UL_BIG: | |
7723 | // We don't want to give a compile-time error for a 64-bit | |
7724 | // value on a 32-bit target. | |
1b10c5e7 | 7725 | break; |
1ad00fd4 | 7726 | } |
1b10c5e7 | 7727 | |
7728 | mpz_t val; | |
7729 | if (!nc.to_int(&val)) | |
7730 | go_unreachable(); | |
7731 | int bits = mpz_sizeinbase(val, 2); | |
7732 | mpz_clear(val); | |
7733 | Type* int_type = Type::lookup_integer_type("int"); | |
7734 | if (bits >= int_type->integer_type()->bits()) | |
7735 | { | |
631d5788 | 7736 | go_error_at(e->location(), "%s argument too large for make", |
7737 | is_length ? "len" : "cap"); | |
1b10c5e7 | 7738 | return false; |
7739 | } | |
7740 | ||
7741 | return true; | |
a9182619 | 7742 | } |
7743 | ||
1ad00fd4 | 7744 | if (e->type()->integer_type() != NULL) |
7745 | return true; | |
7746 | ||
631d5788 | 7747 | go_error_at(e->location(), "non-integer %s argument to make", |
7748 | is_length ? "len" : "cap"); | |
a9182619 | 7749 | return false; |
7750 | } | |
7751 | ||
e440a328 | 7752 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7753 | // the argument. We need to map complex64 to float32 and complex128 |
7754 | // to float64, so it has to be done by name. This returns NULL if it | |
7755 | // can't figure out the type. | |
e440a328 | 7756 | |
7757 | Type* | |
7758 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7759 | { | |
7760 | if (arg_type == NULL || arg_type->is_abstract()) | |
7761 | return NULL; | |
7762 | Named_type* nt = arg_type->named_type(); | |
7763 | if (nt == NULL) | |
7764 | return NULL; | |
7765 | while (nt->real_type()->named_type() != NULL) | |
7766 | nt = nt->real_type()->named_type(); | |
48080209 | 7767 | if (nt->name() == "complex64") |
e440a328 | 7768 | return Type::lookup_float_type("float32"); |
7769 | else if (nt->name() == "complex128") | |
7770 | return Type::lookup_float_type("float64"); | |
7771 | else | |
7772 | return NULL; | |
7773 | } | |
7774 | ||
48080209 | 7775 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7776 | // argments. Like real_imag_type, we have to map by name. |
7777 | ||
7778 | Type* | |
48080209 | 7779 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7780 | { |
7781 | if (arg_type == NULL || arg_type->is_abstract()) | |
7782 | return NULL; | |
7783 | Named_type* nt = arg_type->named_type(); | |
7784 | if (nt == NULL) | |
7785 | return NULL; | |
7786 | while (nt->real_type()->named_type() != NULL) | |
7787 | nt = nt->real_type()->named_type(); | |
48080209 | 7788 | if (nt->name() == "float32") |
e440a328 | 7789 | return Type::lookup_complex_type("complex64"); |
7790 | else if (nt->name() == "float64") | |
7791 | return Type::lookup_complex_type("complex128"); | |
7792 | else | |
7793 | return NULL; | |
7794 | } | |
7795 | ||
7796 | // Return a single argument, or NULL if there isn't one. | |
7797 | ||
7798 | Expression* | |
7799 | Builtin_call_expression::one_arg() const | |
7800 | { | |
7801 | const Expression_list* args = this->args(); | |
aa615cb3 | 7802 | if (args == NULL || args->size() != 1) |
e440a328 | 7803 | return NULL; |
7804 | return args->front(); | |
7805 | } | |
7806 | ||
83921647 | 7807 | // A traversal class which looks for a call or receive expression. |
7808 | ||
7809 | class Find_call_expression : public Traverse | |
7810 | { | |
7811 | public: | |
7812 | Find_call_expression() | |
7813 | : Traverse(traverse_expressions), | |
7814 | found_(false) | |
7815 | { } | |
7816 | ||
7817 | int | |
7818 | expression(Expression**); | |
7819 | ||
7820 | bool | |
7821 | found() | |
7822 | { return this->found_; } | |
7823 | ||
7824 | private: | |
7825 | bool found_; | |
7826 | }; | |
7827 | ||
7828 | int | |
7829 | Find_call_expression::expression(Expression** pexpr) | |
7830 | { | |
7831 | if ((*pexpr)->call_expression() != NULL | |
7832 | || (*pexpr)->receive_expression() != NULL) | |
7833 | { | |
7834 | this->found_ = true; | |
7835 | return TRAVERSE_EXIT; | |
7836 | } | |
7837 | return TRAVERSE_CONTINUE; | |
7838 | } | |
7839 | ||
7840 | // Return whether this is constant: len of a string constant, or len | |
7841 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7842 | // unsafe.Alignof. | |
e440a328 | 7843 | |
7844 | bool | |
7845 | Builtin_call_expression::do_is_constant() const | |
7846 | { | |
12e69faa | 7847 | if (this->is_error_expression()) |
7848 | return true; | |
e440a328 | 7849 | switch (this->code_) |
7850 | { | |
7851 | case BUILTIN_LEN: | |
7852 | case BUILTIN_CAP: | |
7853 | { | |
0f914071 | 7854 | if (this->seen_) |
7855 | return false; | |
7856 | ||
e440a328 | 7857 | Expression* arg = this->one_arg(); |
7858 | if (arg == NULL) | |
7859 | return false; | |
7860 | Type* arg_type = arg->type(); | |
7861 | ||
7862 | if (arg_type->points_to() != NULL | |
7863 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7864 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7865 | arg_type = arg_type->points_to(); |
7866 | ||
83921647 | 7867 | // The len and cap functions are only constant if there are no |
7868 | // function calls or channel operations in the arguments. | |
7869 | // Otherwise we have to make the call. | |
7870 | if (!arg->is_constant()) | |
7871 | { | |
7872 | Find_call_expression find_call; | |
7873 | Expression::traverse(&arg, &find_call); | |
7874 | if (find_call.found()) | |
7875 | return false; | |
7876 | } | |
7877 | ||
e440a328 | 7878 | if (arg_type->array_type() != NULL |
7879 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7880 | return true; |
e440a328 | 7881 | |
7882 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7883 | { |
7884 | this->seen_ = true; | |
7885 | bool ret = arg->is_constant(); | |
7886 | this->seen_ = false; | |
7887 | return ret; | |
7888 | } | |
e440a328 | 7889 | } |
7890 | break; | |
7891 | ||
7892 | case BUILTIN_SIZEOF: | |
7893 | case BUILTIN_ALIGNOF: | |
7894 | return this->one_arg() != NULL; | |
7895 | ||
7896 | case BUILTIN_OFFSETOF: | |
7897 | { | |
7898 | Expression* arg = this->one_arg(); | |
7899 | if (arg == NULL) | |
7900 | return false; | |
7901 | return arg->field_reference_expression() != NULL; | |
7902 | } | |
7903 | ||
48080209 | 7904 | case BUILTIN_COMPLEX: |
e440a328 | 7905 | { |
7906 | const Expression_list* args = this->args(); | |
7907 | if (args != NULL && args->size() == 2) | |
7908 | return args->front()->is_constant() && args->back()->is_constant(); | |
7909 | } | |
7910 | break; | |
7911 | ||
7912 | case BUILTIN_REAL: | |
7913 | case BUILTIN_IMAG: | |
7914 | { | |
7915 | Expression* arg = this->one_arg(); | |
7916 | return arg != NULL && arg->is_constant(); | |
7917 | } | |
7918 | ||
7919 | default: | |
7920 | break; | |
7921 | } | |
7922 | ||
7923 | return false; | |
7924 | } | |
7925 | ||
0c77715b | 7926 | // Return a numeric constant if possible. |
e440a328 | 7927 | |
7928 | bool | |
0c77715b | 7929 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7930 | { |
7931 | if (this->code_ == BUILTIN_LEN | |
7932 | || this->code_ == BUILTIN_CAP) | |
7933 | { | |
7934 | Expression* arg = this->one_arg(); | |
7935 | if (arg == NULL) | |
7936 | return false; | |
7937 | Type* arg_type = arg->type(); | |
7938 | ||
7939 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7940 | { | |
7941 | std::string sval; | |
7942 | if (arg->string_constant_value(&sval)) | |
7943 | { | |
0c77715b | 7944 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7945 | sval.length()); | |
e440a328 | 7946 | return true; |
7947 | } | |
7948 | } | |
7949 | ||
7950 | if (arg_type->points_to() != NULL | |
7951 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7952 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7953 | arg_type = arg_type->points_to(); |
7954 | ||
7955 | if (arg_type->array_type() != NULL | |
7956 | && arg_type->array_type()->length() != NULL) | |
7957 | { | |
0f914071 | 7958 | if (this->seen_) |
7959 | return false; | |
e440a328 | 7960 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7961 | this->seen_ = true; |
0c77715b | 7962 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7963 | this->seen_ = false; |
7964 | if (r) | |
e440a328 | 7965 | { |
0c77715b | 7966 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7967 | this->location())) | |
7968 | r = false; | |
e440a328 | 7969 | } |
0c77715b | 7970 | return r; |
e440a328 | 7971 | } |
7972 | } | |
7973 | else if (this->code_ == BUILTIN_SIZEOF | |
7974 | || this->code_ == BUILTIN_ALIGNOF) | |
7975 | { | |
7976 | Expression* arg = this->one_arg(); | |
7977 | if (arg == NULL) | |
7978 | return false; | |
7979 | Type* arg_type = arg->type(); | |
5c13bd80 | 7980 | if (arg_type->is_error()) |
e440a328 | 7981 | return false; |
7982 | if (arg_type->is_abstract()) | |
7983 | return false; | |
2c809f8f | 7984 | if (this->seen_) |
7985 | return false; | |
927a01eb | 7986 | |
3f378015 | 7987 | int64_t ret; |
e440a328 | 7988 | if (this->code_ == BUILTIN_SIZEOF) |
7989 | { | |
2c809f8f | 7990 | this->seen_ = true; |
7991 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
7992 | this->seen_ = false; | |
7993 | if (!ok) | |
e440a328 | 7994 | return false; |
7995 | } | |
7996 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7997 | { | |
2c809f8f | 7998 | bool ok; |
7999 | this->seen_ = true; | |
637bd3af | 8000 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 8001 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 8002 | else |
e440a328 | 8003 | { |
8004 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
8005 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 8006 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 8007 | } |
2c809f8f | 8008 | this->seen_ = false; |
8009 | if (!ok) | |
8010 | return false; | |
e440a328 | 8011 | } |
8012 | else | |
c3e6f413 | 8013 | go_unreachable(); |
927a01eb | 8014 | |
3f378015 | 8015 | mpz_t zval; |
8016 | set_mpz_from_int64(&zval, ret); | |
8017 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8018 | mpz_clear(zval); | |
e440a328 | 8019 | return true; |
8020 | } | |
8021 | else if (this->code_ == BUILTIN_OFFSETOF) | |
8022 | { | |
8023 | Expression* arg = this->one_arg(); | |
8024 | if (arg == NULL) | |
8025 | return false; | |
8026 | Field_reference_expression* farg = arg->field_reference_expression(); | |
8027 | if (farg == NULL) | |
8028 | return false; | |
2c809f8f | 8029 | if (this->seen_) |
8030 | return false; | |
8031 | ||
3f378015 | 8032 | int64_t total_offset = 0; |
9a4bd570 | 8033 | while (true) |
8034 | { | |
8035 | Expression* struct_expr = farg->expr(); | |
8036 | Type* st = struct_expr->type(); | |
8037 | if (st->struct_type() == NULL) | |
8038 | return false; | |
8039 | if (st->named_type() != NULL) | |
8040 | st->named_type()->convert(this->gogo_); | |
3f378015 | 8041 | int64_t offset; |
2c809f8f | 8042 | this->seen_ = true; |
8043 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
8044 | farg->field_index(), | |
8045 | &offset); | |
8046 | this->seen_ = false; | |
8047 | if (!ok) | |
8048 | return false; | |
9a4bd570 | 8049 | total_offset += offset; |
8050 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
8051 | { | |
8052 | // Go up until we reach the original base. | |
8053 | farg = struct_expr->field_reference_expression(); | |
8054 | continue; | |
8055 | } | |
8056 | break; | |
8057 | } | |
3f378015 | 8058 | mpz_t zval; |
8059 | set_mpz_from_int64(&zval, total_offset); | |
8060 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8061 | mpz_clear(zval); | |
e440a328 | 8062 | return true; |
8063 | } | |
0c77715b | 8064 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 8065 | { |
8066 | Expression* arg = this->one_arg(); | |
8067 | if (arg == NULL) | |
8068 | return false; | |
8069 | ||
0c77715b | 8070 | Numeric_constant argnc; |
8071 | if (!arg->numeric_constant_value(&argnc)) | |
8072 | return false; | |
8073 | ||
fcbea5e4 | 8074 | mpc_t val; |
8075 | if (!argnc.to_complex(&val)) | |
0c77715b | 8076 | return false; |
e440a328 | 8077 | |
0c77715b | 8078 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
8079 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 8080 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 8081 | else |
fcbea5e4 | 8082 | nc->set_float(type, mpc_imagref(val)); |
8083 | mpc_clear(val); | |
0c77715b | 8084 | return true; |
e440a328 | 8085 | } |
0c77715b | 8086 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 8087 | { |
8088 | const Expression_list* args = this->args(); | |
8089 | if (args == NULL || args->size() != 2) | |
8090 | return false; | |
8091 | ||
0c77715b | 8092 | Numeric_constant rnc; |
8093 | if (!args->front()->numeric_constant_value(&rnc)) | |
8094 | return false; | |
8095 | Numeric_constant inc; | |
8096 | if (!args->back()->numeric_constant_value(&inc)) | |
8097 | return false; | |
8098 | ||
8099 | if (rnc.type() != NULL | |
8100 | && !rnc.type()->is_abstract() | |
8101 | && inc.type() != NULL | |
8102 | && !inc.type()->is_abstract() | |
8103 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
8104 | return false; | |
8105 | ||
e440a328 | 8106 | mpfr_t r; |
0c77715b | 8107 | if (!rnc.to_float(&r)) |
8108 | return false; | |
8109 | mpfr_t i; | |
8110 | if (!inc.to_float(&i)) | |
e440a328 | 8111 | { |
8112 | mpfr_clear(r); | |
8113 | return false; | |
8114 | } | |
8115 | ||
0c77715b | 8116 | Type* arg_type = rnc.type(); |
8117 | if (arg_type == NULL || arg_type->is_abstract()) | |
8118 | arg_type = inc.type(); | |
e440a328 | 8119 | |
fcbea5e4 | 8120 | mpc_t val; |
8121 | mpc_init2(val, mpc_precision); | |
8122 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 8123 | mpfr_clear(r); |
8124 | mpfr_clear(i); | |
8125 | ||
fcbea5e4 | 8126 | Type* type = Builtin_call_expression::complex_type(arg_type); |
8127 | nc->set_complex(type, val); | |
8128 | ||
8129 | mpc_clear(val); | |
8130 | ||
0c77715b | 8131 | return true; |
e440a328 | 8132 | } |
8133 | ||
8134 | return false; | |
8135 | } | |
8136 | ||
a7549a6a | 8137 | // Give an error if we are discarding the value of an expression which |
8138 | // should not normally be discarded. We don't give an error for | |
8139 | // discarding the value of an ordinary function call, but we do for | |
8140 | // builtin functions, purely for consistency with the gc compiler. | |
8141 | ||
4f2138d7 | 8142 | bool |
a7549a6a | 8143 | Builtin_call_expression::do_discarding_value() |
8144 | { | |
8145 | switch (this->code_) | |
8146 | { | |
8147 | case BUILTIN_INVALID: | |
8148 | default: | |
8149 | go_unreachable(); | |
8150 | ||
8151 | case BUILTIN_APPEND: | |
8152 | case BUILTIN_CAP: | |
8153 | case BUILTIN_COMPLEX: | |
8154 | case BUILTIN_IMAG: | |
8155 | case BUILTIN_LEN: | |
8156 | case BUILTIN_MAKE: | |
8157 | case BUILTIN_NEW: | |
8158 | case BUILTIN_REAL: | |
8159 | case BUILTIN_ALIGNOF: | |
8160 | case BUILTIN_OFFSETOF: | |
8161 | case BUILTIN_SIZEOF: | |
8162 | this->unused_value_error(); | |
4f2138d7 | 8163 | return false; |
a7549a6a | 8164 | |
8165 | case BUILTIN_CLOSE: | |
8166 | case BUILTIN_COPY: | |
1cce762f | 8167 | case BUILTIN_DELETE: |
a7549a6a | 8168 | case BUILTIN_PANIC: |
8169 | case BUILTIN_PRINT: | |
8170 | case BUILTIN_PRINTLN: | |
8171 | case BUILTIN_RECOVER: | |
4f2138d7 | 8172 | return true; |
a7549a6a | 8173 | } |
8174 | } | |
8175 | ||
e440a328 | 8176 | // Return the type. |
8177 | ||
8178 | Type* | |
8179 | Builtin_call_expression::do_type() | |
8180 | { | |
79651b1f | 8181 | if (this->is_error_expression()) |
8182 | return Type::make_error_type(); | |
e440a328 | 8183 | switch (this->code_) |
8184 | { | |
8185 | case BUILTIN_INVALID: | |
8186 | default: | |
79651b1f | 8187 | return Type::make_error_type(); |
e440a328 | 8188 | |
8189 | case BUILTIN_NEW: | |
8190 | case BUILTIN_MAKE: | |
8191 | { | |
8192 | const Expression_list* args = this->args(); | |
8193 | if (args == NULL || args->empty()) | |
8194 | return Type::make_error_type(); | |
8195 | return Type::make_pointer_type(args->front()->type()); | |
8196 | } | |
8197 | ||
8198 | case BUILTIN_CAP: | |
8199 | case BUILTIN_COPY: | |
8200 | case BUILTIN_LEN: | |
7ba86326 | 8201 | return Type::lookup_integer_type("int"); |
8202 | ||
e440a328 | 8203 | case BUILTIN_ALIGNOF: |
8204 | case BUILTIN_OFFSETOF: | |
8205 | case BUILTIN_SIZEOF: | |
7ba86326 | 8206 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 8207 | |
8208 | case BUILTIN_CLOSE: | |
1cce762f | 8209 | case BUILTIN_DELETE: |
e440a328 | 8210 | case BUILTIN_PANIC: |
8211 | case BUILTIN_PRINT: | |
8212 | case BUILTIN_PRINTLN: | |
8213 | return Type::make_void_type(); | |
8214 | ||
e440a328 | 8215 | case BUILTIN_RECOVER: |
823c7e3d | 8216 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8217 | |
8218 | case BUILTIN_APPEND: | |
8219 | { | |
8220 | const Expression_list* args = this->args(); | |
8221 | if (args == NULL || args->empty()) | |
8222 | return Type::make_error_type(); | |
3ff4863b | 8223 | Type *ret = args->front()->type(); |
8224 | if (!ret->is_slice_type()) | |
8225 | return Type::make_error_type(); | |
8226 | return ret; | |
e440a328 | 8227 | } |
8228 | ||
8229 | case BUILTIN_REAL: | |
8230 | case BUILTIN_IMAG: | |
8231 | { | |
8232 | Expression* arg = this->one_arg(); | |
8233 | if (arg == NULL) | |
8234 | return Type::make_error_type(); | |
8235 | Type* t = arg->type(); | |
8236 | if (t->is_abstract()) | |
8237 | t = t->make_non_abstract_type(); | |
8238 | t = Builtin_call_expression::real_imag_type(t); | |
8239 | if (t == NULL) | |
8240 | t = Type::make_error_type(); | |
8241 | return t; | |
8242 | } | |
8243 | ||
48080209 | 8244 | case BUILTIN_COMPLEX: |
e440a328 | 8245 | { |
8246 | const Expression_list* args = this->args(); | |
8247 | if (args == NULL || args->size() != 2) | |
8248 | return Type::make_error_type(); | |
8249 | Type* t = args->front()->type(); | |
8250 | if (t->is_abstract()) | |
8251 | { | |
8252 | t = args->back()->type(); | |
8253 | if (t->is_abstract()) | |
8254 | t = t->make_non_abstract_type(); | |
8255 | } | |
48080209 | 8256 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 8257 | if (t == NULL) |
8258 | t = Type::make_error_type(); | |
8259 | return t; | |
8260 | } | |
8261 | } | |
8262 | } | |
8263 | ||
8264 | // Determine the type. | |
8265 | ||
8266 | void | |
8267 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
8268 | { | |
fb94b0ca | 8269 | if (!this->determining_types()) |
8270 | return; | |
8271 | ||
e440a328 | 8272 | this->fn()->determine_type_no_context(); |
8273 | ||
8274 | const Expression_list* args = this->args(); | |
8275 | ||
8276 | bool is_print; | |
8277 | Type* arg_type = NULL; | |
321e5ad2 | 8278 | Type* trailing_arg_types = NULL; |
e440a328 | 8279 | switch (this->code_) |
8280 | { | |
8281 | case BUILTIN_PRINT: | |
8282 | case BUILTIN_PRINTLN: | |
8283 | // Do not force a large integer constant to "int". | |
8284 | is_print = true; | |
8285 | break; | |
8286 | ||
8287 | case BUILTIN_REAL: | |
8288 | case BUILTIN_IMAG: | |
48080209 | 8289 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8290 | if (arg_type == NULL) |
8291 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8292 | is_print = false; |
8293 | break; | |
8294 | ||
48080209 | 8295 | case BUILTIN_COMPLEX: |
e440a328 | 8296 | { |
48080209 | 8297 | // For the complex function the type of one operand can |
e440a328 | 8298 | // determine the type of the other, as in a binary expression. |
8299 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8300 | if (arg_type == NULL) |
8301 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8302 | if (args != NULL && args->size() == 2) |
8303 | { | |
8304 | Type* t1 = args->front()->type(); | |
c849bb59 | 8305 | Type* t2 = args->back()->type(); |
e440a328 | 8306 | if (!t1->is_abstract()) |
8307 | arg_type = t1; | |
8308 | else if (!t2->is_abstract()) | |
8309 | arg_type = t2; | |
8310 | } | |
8311 | is_print = false; | |
8312 | } | |
8313 | break; | |
8314 | ||
321e5ad2 | 8315 | case BUILTIN_APPEND: |
8316 | if (!this->is_varargs() | |
8317 | && args != NULL | |
8318 | && !args->empty() | |
8319 | && args->front()->type()->is_slice_type()) | |
8320 | trailing_arg_types = | |
8321 | args->front()->type()->array_type()->element_type(); | |
8322 | is_print = false; | |
8323 | break; | |
8324 | ||
e440a328 | 8325 | default: |
8326 | is_print = false; | |
8327 | break; | |
8328 | } | |
8329 | ||
8330 | if (args != NULL) | |
8331 | { | |
8332 | for (Expression_list::const_iterator pa = args->begin(); | |
8333 | pa != args->end(); | |
8334 | ++pa) | |
8335 | { | |
8336 | Type_context subcontext; | |
8337 | subcontext.type = arg_type; | |
8338 | ||
8339 | if (is_print) | |
8340 | { | |
8341 | // We want to print large constants, we so can't just | |
8342 | // use the appropriate nonabstract type. Use uint64 for | |
8343 | // an integer if we know it is nonnegative, otherwise | |
8344 | // use int64 for a integer, otherwise use float64 for a | |
8345 | // float or complex128 for a complex. | |
8346 | Type* want_type = NULL; | |
8347 | Type* atype = (*pa)->type(); | |
8348 | if (atype->is_abstract()) | |
8349 | { | |
8350 | if (atype->integer_type() != NULL) | |
8351 | { | |
0c77715b | 8352 | Numeric_constant nc; |
8353 | if (this->numeric_constant_value(&nc)) | |
8354 | { | |
8355 | mpz_t val; | |
8356 | if (nc.to_int(&val)) | |
8357 | { | |
8358 | if (mpz_sgn(val) >= 0) | |
8359 | want_type = Type::lookup_integer_type("uint64"); | |
8360 | mpz_clear(val); | |
8361 | } | |
8362 | } | |
8363 | if (want_type == NULL) | |
e440a328 | 8364 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8365 | } |
8366 | else if (atype->float_type() != NULL) | |
8367 | want_type = Type::lookup_float_type("float64"); | |
8368 | else if (atype->complex_type() != NULL) | |
8369 | want_type = Type::lookup_complex_type("complex128"); | |
8370 | else if (atype->is_abstract_string_type()) | |
8371 | want_type = Type::lookup_string_type(); | |
8372 | else if (atype->is_abstract_boolean_type()) | |
8373 | want_type = Type::lookup_bool_type(); | |
8374 | else | |
c3e6f413 | 8375 | go_unreachable(); |
e440a328 | 8376 | subcontext.type = want_type; |
8377 | } | |
8378 | } | |
8379 | ||
8380 | (*pa)->determine_type(&subcontext); | |
321e5ad2 | 8381 | |
8382 | if (trailing_arg_types != NULL) | |
8383 | { | |
8384 | arg_type = trailing_arg_types; | |
8385 | trailing_arg_types = NULL; | |
8386 | } | |
e440a328 | 8387 | } |
8388 | } | |
8389 | } | |
8390 | ||
8391 | // If there is exactly one argument, return true. Otherwise give an | |
8392 | // error message and return false. | |
8393 | ||
8394 | bool | |
8395 | Builtin_call_expression::check_one_arg() | |
8396 | { | |
8397 | const Expression_list* args = this->args(); | |
8398 | if (args == NULL || args->size() < 1) | |
8399 | { | |
8400 | this->report_error(_("not enough arguments")); | |
8401 | return false; | |
8402 | } | |
8403 | else if (args->size() > 1) | |
8404 | { | |
8405 | this->report_error(_("too many arguments")); | |
8406 | return false; | |
8407 | } | |
8408 | if (args->front()->is_error_expression() | |
5c13bd80 | 8409 | || args->front()->type()->is_error()) |
e440a328 | 8410 | { |
8411 | this->set_is_error(); | |
8412 | return false; | |
8413 | } | |
8414 | return true; | |
8415 | } | |
8416 | ||
8417 | // Check argument types for a builtin function. | |
8418 | ||
8419 | void | |
8420 | Builtin_call_expression::do_check_types(Gogo*) | |
8421 | { | |
375646ea | 8422 | if (this->is_error_expression()) |
8423 | return; | |
e440a328 | 8424 | switch (this->code_) |
8425 | { | |
8426 | case BUILTIN_INVALID: | |
8427 | case BUILTIN_NEW: | |
8428 | case BUILTIN_MAKE: | |
cd238b8d | 8429 | case BUILTIN_DELETE: |
e440a328 | 8430 | return; |
8431 | ||
8432 | case BUILTIN_LEN: | |
8433 | case BUILTIN_CAP: | |
8434 | { | |
8435 | // The single argument may be either a string or an array or a | |
8436 | // map or a channel, or a pointer to a closed array. | |
8437 | if (this->check_one_arg()) | |
8438 | { | |
8439 | Type* arg_type = this->one_arg()->type(); | |
8440 | if (arg_type->points_to() != NULL | |
8441 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8442 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8443 | arg_type = arg_type->points_to(); |
8444 | if (this->code_ == BUILTIN_CAP) | |
8445 | { | |
5c13bd80 | 8446 | if (!arg_type->is_error() |
e440a328 | 8447 | && arg_type->array_type() == NULL |
8448 | && arg_type->channel_type() == NULL) | |
8449 | this->report_error(_("argument must be array or slice " | |
8450 | "or channel")); | |
8451 | } | |
8452 | else | |
8453 | { | |
5c13bd80 | 8454 | if (!arg_type->is_error() |
e440a328 | 8455 | && !arg_type->is_string_type() |
8456 | && arg_type->array_type() == NULL | |
8457 | && arg_type->map_type() == NULL | |
8458 | && arg_type->channel_type() == NULL) | |
8459 | this->report_error(_("argument must be string or " | |
8460 | "array or slice or map or channel")); | |
8461 | } | |
8462 | } | |
8463 | } | |
8464 | break; | |
8465 | ||
8466 | case BUILTIN_PRINT: | |
8467 | case BUILTIN_PRINTLN: | |
8468 | { | |
8469 | const Expression_list* args = this->args(); | |
8470 | if (args == NULL) | |
8471 | { | |
8472 | if (this->code_ == BUILTIN_PRINT) | |
631d5788 | 8473 | go_warning_at(this->location(), 0, |
e440a328 | 8474 | "no arguments for builtin function %<%s%>", |
8475 | (this->code_ == BUILTIN_PRINT | |
8476 | ? "print" | |
8477 | : "println")); | |
8478 | } | |
8479 | else | |
8480 | { | |
8481 | for (Expression_list::const_iterator p = args->begin(); | |
8482 | p != args->end(); | |
8483 | ++p) | |
8484 | { | |
8485 | Type* type = (*p)->type(); | |
5c13bd80 | 8486 | if (type->is_error() |
e440a328 | 8487 | || type->is_string_type() |
8488 | || type->integer_type() != NULL | |
8489 | || type->float_type() != NULL | |
8490 | || type->complex_type() != NULL | |
8491 | || type->is_boolean_type() | |
8492 | || type->points_to() != NULL | |
8493 | || type->interface_type() != NULL | |
8494 | || type->channel_type() != NULL | |
8495 | || type->map_type() != NULL | |
8496 | || type->function_type() != NULL | |
411eb89e | 8497 | || type->is_slice_type()) |
e440a328 | 8498 | ; |
acf8e158 | 8499 | else if ((*p)->is_type_expression()) |
8500 | { | |
8501 | // If this is a type expression it's going to give | |
8502 | // an error anyhow, so we don't need one here. | |
8503 | } | |
e440a328 | 8504 | else |
8505 | this->report_error(_("unsupported argument type to " | |
8506 | "builtin function")); | |
8507 | } | |
8508 | } | |
8509 | } | |
8510 | break; | |
8511 | ||
8512 | case BUILTIN_CLOSE: | |
e440a328 | 8513 | if (this->check_one_arg()) |
8514 | { | |
8515 | if (this->one_arg()->type()->channel_type() == NULL) | |
8516 | this->report_error(_("argument must be channel")); | |
5202d986 | 8517 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8518 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8519 | } |
8520 | break; | |
8521 | ||
8522 | case BUILTIN_PANIC: | |
8523 | case BUILTIN_SIZEOF: | |
8524 | case BUILTIN_ALIGNOF: | |
8525 | this->check_one_arg(); | |
8526 | break; | |
8527 | ||
8528 | case BUILTIN_RECOVER: | |
6334270b | 8529 | if (this->args() != NULL |
8530 | && !this->args()->empty() | |
8531 | && !this->recover_arg_is_set_) | |
e440a328 | 8532 | this->report_error(_("too many arguments")); |
8533 | break; | |
8534 | ||
8535 | case BUILTIN_OFFSETOF: | |
8536 | if (this->check_one_arg()) | |
8537 | { | |
8538 | Expression* arg = this->one_arg(); | |
8539 | if (arg->field_reference_expression() == NULL) | |
8540 | this->report_error(_("argument must be a field reference")); | |
8541 | } | |
8542 | break; | |
8543 | ||
8544 | case BUILTIN_COPY: | |
8545 | { | |
8546 | const Expression_list* args = this->args(); | |
8547 | if (args == NULL || args->size() < 2) | |
8548 | { | |
8549 | this->report_error(_("not enough arguments")); | |
8550 | break; | |
8551 | } | |
8552 | else if (args->size() > 2) | |
8553 | { | |
8554 | this->report_error(_("too many arguments")); | |
8555 | break; | |
8556 | } | |
8557 | Type* arg1_type = args->front()->type(); | |
8558 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8559 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 8560 | { |
8561 | this->set_is_error(); | |
8562 | break; | |
8563 | } | |
e440a328 | 8564 | |
8565 | Type* e1; | |
411eb89e | 8566 | if (arg1_type->is_slice_type()) |
e440a328 | 8567 | e1 = arg1_type->array_type()->element_type(); |
8568 | else | |
8569 | { | |
8570 | this->report_error(_("left argument must be a slice")); | |
8571 | break; | |
8572 | } | |
8573 | ||
411eb89e | 8574 | if (arg2_type->is_slice_type()) |
60963afd | 8575 | { |
8576 | Type* e2 = arg2_type->array_type()->element_type(); | |
8577 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8578 | this->report_error(_("element types must be the same")); | |
8579 | } | |
e440a328 | 8580 | else if (arg2_type->is_string_type()) |
e440a328 | 8581 | { |
60963afd | 8582 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8583 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8584 | } |
60963afd | 8585 | else |
8586 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8587 | } |
8588 | break; | |
8589 | ||
8590 | case BUILTIN_APPEND: | |
8591 | { | |
8592 | const Expression_list* args = this->args(); | |
321e5ad2 | 8593 | if (args == NULL || args->empty()) |
e440a328 | 8594 | { |
8595 | this->report_error(_("not enough arguments")); | |
8596 | break; | |
8597 | } | |
321e5ad2 | 8598 | |
8599 | Type* slice_type = args->front()->type(); | |
8600 | if (!slice_type->is_slice_type()) | |
6bebb39d | 8601 | { |
321e5ad2 | 8602 | if (slice_type->is_error_type()) |
8603 | break; | |
8604 | if (slice_type->is_nil_type()) | |
8605 | go_error_at(args->front()->location(), "use of untyped nil"); | |
8606 | else | |
8607 | go_error_at(args->front()->location(), | |
8608 | "argument 1 must be a slice"); | |
6bebb39d | 8609 | this->set_is_error(); |
8610 | break; | |
8611 | } | |
cd238b8d | 8612 | |
321e5ad2 | 8613 | Type* element_type = slice_type->array_type()->element_type(); |
8614 | if (this->is_varargs()) | |
4fd4fcf4 | 8615 | { |
321e5ad2 | 8616 | if (!args->back()->type()->is_slice_type() |
8617 | && !args->back()->type()->is_string_type()) | |
8618 | { | |
8619 | go_error_at(args->back()->location(), | |
8620 | "invalid use of %<...%> with non-slice/non-string"); | |
8621 | this->set_is_error(); | |
8622 | break; | |
8623 | } | |
4fd4fcf4 | 8624 | |
321e5ad2 | 8625 | if (args->size() < 2) |
8626 | { | |
8627 | this->report_error(_("not enough arguments")); | |
8628 | break; | |
8629 | } | |
8630 | if (args->size() > 2) | |
8631 | { | |
8632 | this->report_error(_("too many arguments")); | |
8633 | break; | |
8634 | } | |
8635 | ||
8636 | if (args->back()->type()->is_string_type() | |
8637 | && element_type->integer_type() != NULL | |
8638 | && element_type->integer_type()->is_byte()) | |
8639 | { | |
8640 | // Permit append(s1, s2...) when s1 is a slice of | |
8641 | // bytes and s2 is a string type. | |
8642 | } | |
e440a328 | 8643 | else |
8644 | { | |
321e5ad2 | 8645 | // We have to test for assignment compatibility to a |
8646 | // slice of the element type, which is not necessarily | |
8647 | // the same as the type of the first argument: the | |
8648 | // first argument might have a named type. | |
8649 | Type* check_type = Type::make_array_type(element_type, NULL); | |
8650 | std::string reason; | |
8651 | if (!Type::are_assignable(check_type, args->back()->type(), | |
8652 | &reason)) | |
8653 | { | |
8654 | if (reason.empty()) | |
8655 | go_error_at(args->back()->location(), | |
8656 | "argument 2 has invalid type"); | |
8657 | else | |
8658 | go_error_at(args->back()->location(), | |
8659 | "argument 2 has invalid type (%s)", | |
8660 | reason.c_str()); | |
8661 | this->set_is_error(); | |
8662 | break; | |
8663 | } | |
8664 | } | |
8665 | } | |
8666 | else | |
8667 | { | |
8668 | Expression_list::const_iterator pa = args->begin(); | |
8669 | int i = 2; | |
8670 | for (++pa; pa != args->end(); ++pa, ++i) | |
8671 | { | |
8672 | std::string reason; | |
8673 | if (!Type::are_assignable(element_type, (*pa)->type(), | |
8674 | &reason)) | |
8675 | { | |
8676 | if (reason.empty()) | |
8677 | go_error_at((*pa)->location(), | |
8678 | "argument %d has incompatible type", i); | |
8679 | else | |
8680 | go_error_at((*pa)->location(), | |
8681 | "argument %d has incompatible type (%s)", | |
8682 | i, reason.c_str()); | |
8683 | this->set_is_error(); | |
8684 | } | |
e440a328 | 8685 | } |
8686 | } | |
e440a328 | 8687 | } |
321e5ad2 | 8688 | break; |
e440a328 | 8689 | |
8690 | case BUILTIN_REAL: | |
8691 | case BUILTIN_IMAG: | |
8692 | if (this->check_one_arg()) | |
8693 | { | |
8694 | if (this->one_arg()->type()->complex_type() == NULL) | |
8695 | this->report_error(_("argument must have complex type")); | |
8696 | } | |
8697 | break; | |
8698 | ||
48080209 | 8699 | case BUILTIN_COMPLEX: |
e440a328 | 8700 | { |
8701 | const Expression_list* args = this->args(); | |
8702 | if (args == NULL || args->size() < 2) | |
8703 | this->report_error(_("not enough arguments")); | |
8704 | else if (args->size() > 2) | |
8705 | this->report_error(_("too many arguments")); | |
8706 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8707 | || args->front()->type()->is_error() |
e440a328 | 8708 | || args->back()->is_error_expression() |
5c13bd80 | 8709 | || args->back()->type()->is_error()) |
e440a328 | 8710 | this->set_is_error(); |
8711 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8712 | args->back()->type(), true, NULL)) |
48080209 | 8713 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8714 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8715 | this->report_error(_("complex arguments must have " |
e440a328 | 8716 | "floating-point type")); |
8717 | } | |
8718 | break; | |
8719 | ||
8720 | default: | |
c3e6f413 | 8721 | go_unreachable(); |
e440a328 | 8722 | } |
8723 | } | |
8724 | ||
72666aed | 8725 | Expression* |
8726 | Builtin_call_expression::do_copy() | |
8727 | { | |
8728 | Call_expression* bce = | |
8729 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8730 | (this->args() == NULL |
8731 | ? NULL | |
8732 | : this->args()->copy()), | |
72666aed | 8733 | this->is_varargs(), |
8734 | this->location()); | |
8735 | ||
8736 | if (this->varargs_are_lowered()) | |
8737 | bce->set_varargs_are_lowered(); | |
8738 | return bce; | |
8739 | } | |
8740 | ||
ea664253 | 8741 | // Return the backend representation for a builtin function. |
e440a328 | 8742 | |
ea664253 | 8743 | Bexpression* |
8744 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8745 | { |
8746 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8747 | Location location = this->location(); |
a0d8874e | 8748 | |
8749 | if (this->is_erroneous_call()) | |
8750 | { | |
8751 | go_assert(saw_errors()); | |
8752 | return gogo->backend()->error_expression(); | |
8753 | } | |
8754 | ||
e440a328 | 8755 | switch (this->code_) |
8756 | { | |
8757 | case BUILTIN_INVALID: | |
8758 | case BUILTIN_NEW: | |
8759 | case BUILTIN_MAKE: | |
c3e6f413 | 8760 | go_unreachable(); |
e440a328 | 8761 | |
8762 | case BUILTIN_LEN: | |
8763 | case BUILTIN_CAP: | |
8764 | { | |
8765 | const Expression_list* args = this->args(); | |
c484d925 | 8766 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8767 | Expression* arg = args->front(); |
e440a328 | 8768 | Type* arg_type = arg->type(); |
0f914071 | 8769 | |
8770 | if (this->seen_) | |
8771 | { | |
c484d925 | 8772 | go_assert(saw_errors()); |
ea664253 | 8773 | return context->backend()->error_expression(); |
0f914071 | 8774 | } |
8775 | this->seen_ = true; | |
0f914071 | 8776 | this->seen_ = false; |
e440a328 | 8777 | if (arg_type->points_to() != NULL) |
8778 | { | |
8779 | arg_type = arg_type->points_to(); | |
c484d925 | 8780 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8781 | && !arg_type->is_slice_type()); |
2c809f8f | 8782 | arg = Expression::make_unary(OPERATOR_MULT, arg, location); |
e440a328 | 8783 | } |
8784 | ||
1b1f2abf | 8785 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8786 | Expression* val; |
e440a328 | 8787 | if (this->code_ == BUILTIN_LEN) |
8788 | { | |
8789 | if (arg_type->is_string_type()) | |
2c809f8f | 8790 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8791 | location); | |
e440a328 | 8792 | else if (arg_type->array_type() != NULL) |
0f914071 | 8793 | { |
8794 | if (this->seen_) | |
8795 | { | |
c484d925 | 8796 | go_assert(saw_errors()); |
ea664253 | 8797 | return context->backend()->error_expression(); |
0f914071 | 8798 | } |
8799 | this->seen_ = true; | |
2c809f8f | 8800 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8801 | this->seen_ = false; |
8802 | } | |
0d5530d9 | 8803 | else if (arg_type->map_type() != NULL |
8804 | || arg_type->channel_type() != NULL) | |
8805 | { | |
8806 | // The first field is the length. If the pointer is | |
8807 | // nil, the length is zero. | |
8808 | Type* pint_type = Type::make_pointer_type(int_type); | |
8809 | arg = Expression::make_unsafe_cast(pint_type, arg, location); | |
8810 | Expression* nil = Expression::make_nil(location); | |
8811 | nil = Expression::make_cast(pint_type, nil, location); | |
8812 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8813 | arg, nil, location); | |
8814 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8815 | location); | |
8816 | Expression* indir = Expression::make_unary(OPERATOR_MULT, | |
8817 | arg, location); | |
8818 | val = Expression::make_conditional(cmp, zero, indir, location); | |
8819 | } | |
e440a328 | 8820 | else |
c3e6f413 | 8821 | go_unreachable(); |
e440a328 | 8822 | } |
8823 | else | |
8824 | { | |
8825 | if (arg_type->array_type() != NULL) | |
0f914071 | 8826 | { |
8827 | if (this->seen_) | |
8828 | { | |
c484d925 | 8829 | go_assert(saw_errors()); |
ea664253 | 8830 | return context->backend()->error_expression(); |
0f914071 | 8831 | } |
8832 | this->seen_ = true; | |
2c809f8f | 8833 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8834 | this->seen_ = false; |
8835 | } | |
e440a328 | 8836 | else if (arg_type->channel_type() != NULL) |
132ed071 | 8837 | { |
8838 | // The second field is the capacity. If the pointer | |
8839 | // is nil, the capacity is zero. | |
8840 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
8841 | Type* pint_type = Type::make_pointer_type(int_type); | |
8842 | Expression* parg = Expression::make_unsafe_cast(uintptr_type, | |
8843 | arg, | |
8844 | location); | |
8845 | int off = int_type->integer_type()->bits() / 8; | |
8846 | Expression* eoff = Expression::make_integer_ul(off, | |
8847 | uintptr_type, | |
8848 | location); | |
8849 | parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, | |
8850 | location); | |
8851 | parg = Expression::make_unsafe_cast(pint_type, parg, location); | |
8852 | Expression* nil = Expression::make_nil(location); | |
8853 | nil = Expression::make_cast(pint_type, nil, location); | |
8854 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8855 | arg, nil, location); | |
8856 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8857 | location); | |
8858 | Expression* indir = Expression::make_unary(OPERATOR_MULT, | |
8859 | parg, location); | |
8860 | val = Expression::make_conditional(cmp, zero, indir, location); | |
8861 | } | |
e440a328 | 8862 | else |
c3e6f413 | 8863 | go_unreachable(); |
e440a328 | 8864 | } |
8865 | ||
2c809f8f | 8866 | return Expression::make_cast(int_type, val, |
ea664253 | 8867 | location)->get_backend(context); |
e440a328 | 8868 | } |
8869 | ||
8870 | case BUILTIN_PRINT: | |
8871 | case BUILTIN_PRINTLN: | |
8872 | { | |
8873 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
88b03a70 | 8874 | |
8875 | Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, | |
8876 | location, 0); | |
e440a328 | 8877 | |
8878 | const Expression_list* call_args = this->args(); | |
8879 | if (call_args != NULL) | |
8880 | { | |
8881 | for (Expression_list::const_iterator p = call_args->begin(); | |
8882 | p != call_args->end(); | |
8883 | ++p) | |
8884 | { | |
8885 | if (is_ln && p != call_args->begin()) | |
8886 | { | |
2c809f8f | 8887 | Expression* print_space = |
88b03a70 | 8888 | Runtime::make_call(Runtime::PRINTSP, location, 0); |
e440a328 | 8889 | |
2c809f8f | 8890 | print_stmts = |
8891 | Expression::make_compound(print_stmts, print_space, | |
8892 | location); | |
8893 | } | |
e440a328 | 8894 | |
2c809f8f | 8895 | Expression* arg = *p; |
8896 | Type* type = arg->type(); | |
8897 | Runtime::Function code; | |
e440a328 | 8898 | if (type->is_string_type()) |
88b03a70 | 8899 | code = Runtime::PRINTSTRING; |
e440a328 | 8900 | else if (type->integer_type() != NULL |
8901 | && type->integer_type()->is_unsigned()) | |
8902 | { | |
e440a328 | 8903 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 8904 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 8905 | code = Runtime::PRINTUINT; |
e440a328 | 8906 | } |
8907 | else if (type->integer_type() != NULL) | |
8908 | { | |
e440a328 | 8909 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 8910 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 8911 | code = Runtime::PRINTINT; |
e440a328 | 8912 | } |
8913 | else if (type->float_type() != NULL) | |
8914 | { | |
2c809f8f | 8915 | Type* dtype = Type::lookup_float_type("float64"); |
8916 | arg = Expression::make_cast(dtype, arg, location); | |
88b03a70 | 8917 | code = Runtime::PRINTFLOAT; |
e440a328 | 8918 | } |
8919 | else if (type->complex_type() != NULL) | |
8920 | { | |
2c809f8f | 8921 | Type* ctype = Type::lookup_complex_type("complex128"); |
8922 | arg = Expression::make_cast(ctype, arg, location); | |
88b03a70 | 8923 | code = Runtime::PRINTCOMPLEX; |
e440a328 | 8924 | } |
8925 | else if (type->is_boolean_type()) | |
88b03a70 | 8926 | code = Runtime::PRINTBOOL; |
e440a328 | 8927 | else if (type->points_to() != NULL |
8928 | || type->channel_type() != NULL | |
8929 | || type->map_type() != NULL | |
8930 | || type->function_type() != NULL) | |
8931 | { | |
2c809f8f | 8932 | arg = Expression::make_cast(type, arg, location); |
88b03a70 | 8933 | code = Runtime::PRINTPOINTER; |
e440a328 | 8934 | } |
8935 | else if (type->interface_type() != NULL) | |
8936 | { | |
8937 | if (type->interface_type()->is_empty()) | |
88b03a70 | 8938 | code = Runtime::PRINTEFACE; |
e440a328 | 8939 | else |
88b03a70 | 8940 | code = Runtime::PRINTIFACE; |
e440a328 | 8941 | } |
411eb89e | 8942 | else if (type->is_slice_type()) |
88b03a70 | 8943 | code = Runtime::PRINTSLICE; |
e440a328 | 8944 | else |
cd238b8d | 8945 | { |
8946 | go_assert(saw_errors()); | |
ea664253 | 8947 | return context->backend()->error_expression(); |
cd238b8d | 8948 | } |
e440a328 | 8949 | |
2c809f8f | 8950 | Expression* call = Runtime::make_call(code, location, 1, arg); |
88b03a70 | 8951 | print_stmts = Expression::make_compound(print_stmts, call, |
8952 | location); | |
e440a328 | 8953 | } |
8954 | } | |
8955 | ||
8956 | if (is_ln) | |
8957 | { | |
2c809f8f | 8958 | Expression* print_nl = |
88b03a70 | 8959 | Runtime::make_call(Runtime::PRINTNL, location, 0); |
8960 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
8961 | location); | |
e440a328 | 8962 | } |
8963 | ||
88b03a70 | 8964 | Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, |
8965 | location, 0); | |
8966 | print_stmts = Expression::make_compound(print_stmts, unlock, location); | |
32e3ff69 | 8967 | |
ea664253 | 8968 | return print_stmts->get_backend(context); |
e440a328 | 8969 | } |
8970 | ||
8971 | case BUILTIN_PANIC: | |
8972 | { | |
8973 | const Expression_list* args = this->args(); | |
c484d925 | 8974 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8975 | Expression* arg = args->front(); |
b13c66cd | 8976 | Type *empty = |
823c7e3d | 8977 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 8978 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
8979 | ||
8980 | Expression* panic = | |
03ac9de4 | 8981 | Runtime::make_call(Runtime::GOPANIC, location, 1, arg); |
ea664253 | 8982 | return panic->get_backend(context); |
e440a328 | 8983 | } |
8984 | ||
8985 | case BUILTIN_RECOVER: | |
8986 | { | |
8987 | // The argument is set when building recover thunks. It's a | |
8988 | // boolean value which is true if we can recover a value now. | |
8989 | const Expression_list* args = this->args(); | |
c484d925 | 8990 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8991 | Expression* arg = args->front(); |
b13c66cd | 8992 | Type *empty = |
823c7e3d | 8993 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8994 | |
e440a328 | 8995 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 8996 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 8997 | |
8998 | // We need to handle a deferred call to recover specially, | |
8999 | // because it changes whether it can recover a panic or not. | |
9000 | // See test7 in test/recover1.go. | |
2c809f8f | 9001 | Expression* recover = Runtime::make_call((this->is_deferred() |
03ac9de4 | 9002 | ? Runtime::DEFERREDRECOVER |
9003 | : Runtime::GORECOVER), | |
2c809f8f | 9004 | location, 0); |
9005 | Expression* cond = | |
9006 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 9007 | return cond->get_backend(context); |
e440a328 | 9008 | } |
9009 | ||
9010 | case BUILTIN_CLOSE: | |
e440a328 | 9011 | { |
9012 | const Expression_list* args = this->args(); | |
c484d925 | 9013 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9014 | Expression* arg = args->front(); |
2c809f8f | 9015 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
9016 | 1, arg); | |
ea664253 | 9017 | return close->get_backend(context); |
e440a328 | 9018 | } |
9019 | ||
9020 | case BUILTIN_SIZEOF: | |
9021 | case BUILTIN_OFFSETOF: | |
9022 | case BUILTIN_ALIGNOF: | |
9023 | { | |
0c77715b | 9024 | Numeric_constant nc; |
9025 | unsigned long val; | |
9026 | if (!this->numeric_constant_value(&nc) | |
9027 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 9028 | { |
c484d925 | 9029 | go_assert(saw_errors()); |
ea664253 | 9030 | return context->backend()->error_expression(); |
7f1d9abd | 9031 | } |
7ba86326 | 9032 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 9033 | mpz_t ival; |
9034 | nc.get_int(&ival); | |
9035 | Expression* int_cst = | |
e67508fa | 9036 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 9037 | mpz_clear(ival); |
ea664253 | 9038 | return int_cst->get_backend(context); |
e440a328 | 9039 | } |
9040 | ||
9041 | case BUILTIN_COPY: | |
9042 | { | |
9043 | const Expression_list* args = this->args(); | |
c484d925 | 9044 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 9045 | Expression* arg1 = args->front(); |
9046 | Expression* arg2 = args->back(); | |
9047 | ||
e440a328 | 9048 | Type* arg1_type = arg1->type(); |
9049 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 9050 | go_assert(arg1->is_variable()); |
321e5ad2 | 9051 | |
9052 | Expression* call; | |
e440a328 | 9053 | |
9054 | Type* arg2_type = arg2->type(); | |
2c809f8f | 9055 | go_assert(arg2->is_variable()); |
321e5ad2 | 9056 | if (arg2_type->is_string_type()) |
9057 | call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location, | |
9058 | 2, arg1, arg2); | |
e440a328 | 9059 | else |
9060 | { | |
321e5ad2 | 9061 | Type* et = at->element_type(); |
9062 | if (et->has_pointer()) | |
9063 | { | |
9064 | Expression* td = Expression::make_type_descriptor(et, | |
9065 | location); | |
9066 | call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location, | |
9067 | 3, td, arg1, arg2); | |
9068 | } | |
9069 | else | |
9070 | { | |
9071 | Expression* sz = Expression::make_type_info(et, | |
9072 | TYPE_INFO_SIZE); | |
9073 | call = Runtime::make_call(Runtime::SLICECOPY, location, 3, | |
9074 | arg1, arg2, sz); | |
9075 | } | |
e440a328 | 9076 | } |
2c809f8f | 9077 | |
321e5ad2 | 9078 | return call->get_backend(context); |
e440a328 | 9079 | } |
9080 | ||
9081 | case BUILTIN_APPEND: | |
321e5ad2 | 9082 | // Handled in Builtin_call_expression::flatten_append. |
9083 | go_unreachable(); | |
e440a328 | 9084 | |
9085 | case BUILTIN_REAL: | |
9086 | case BUILTIN_IMAG: | |
9087 | { | |
9088 | const Expression_list* args = this->args(); | |
c484d925 | 9089 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 9090 | |
9091 | Bexpression* ret; | |
ea664253 | 9092 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 9093 | if (this->code_ == BUILTIN_REAL) |
9094 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
9095 | else | |
9096 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 9097 | return ret; |
e440a328 | 9098 | } |
9099 | ||
48080209 | 9100 | case BUILTIN_COMPLEX: |
e440a328 | 9101 | { |
9102 | const Expression_list* args = this->args(); | |
c484d925 | 9103 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 9104 | Bexpression* breal = args->front()->get_backend(context); |
9105 | Bexpression* bimag = args->back()->get_backend(context); | |
9106 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 9107 | } |
9108 | ||
9109 | default: | |
c3e6f413 | 9110 | go_unreachable(); |
e440a328 | 9111 | } |
9112 | } | |
9113 | ||
9114 | // We have to support exporting a builtin call expression, because | |
9115 | // code can set a constant to the result of a builtin expression. | |
9116 | ||
9117 | void | |
9118 | Builtin_call_expression::do_export(Export* exp) const | |
9119 | { | |
0c77715b | 9120 | Numeric_constant nc; |
9121 | if (!this->numeric_constant_value(&nc)) | |
9122 | { | |
631d5788 | 9123 | go_error_at(this->location(), "value is not constant"); |
0c77715b | 9124 | return; |
9125 | } | |
e440a328 | 9126 | |
0c77715b | 9127 | if (nc.is_int()) |
e440a328 | 9128 | { |
0c77715b | 9129 | mpz_t val; |
9130 | nc.get_int(&val); | |
e440a328 | 9131 | Integer_expression::export_integer(exp, val); |
0c77715b | 9132 | mpz_clear(val); |
e440a328 | 9133 | } |
0c77715b | 9134 | else if (nc.is_float()) |
e440a328 | 9135 | { |
9136 | mpfr_t fval; | |
0c77715b | 9137 | nc.get_float(&fval); |
9138 | Float_expression::export_float(exp, fval); | |
e440a328 | 9139 | mpfr_clear(fval); |
9140 | } | |
0c77715b | 9141 | else if (nc.is_complex()) |
e440a328 | 9142 | { |
fcbea5e4 | 9143 | mpc_t cval; |
9144 | nc.get_complex(&cval); | |
9145 | Complex_expression::export_complex(exp, cval); | |
9146 | mpc_clear(cval); | |
e440a328 | 9147 | } |
0c77715b | 9148 | else |
9149 | go_unreachable(); | |
e440a328 | 9150 | |
9151 | // A trailing space lets us reliably identify the end of the number. | |
9152 | exp->write_c_string(" "); | |
9153 | } | |
9154 | ||
9155 | // Class Call_expression. | |
9156 | ||
8381eda7 | 9157 | // A Go function can be viewed in a couple of different ways. The |
9158 | // code of a Go function becomes a backend function with parameters | |
9159 | // whose types are simply the backend representation of the Go types. | |
9160 | // If there are multiple results, they are returned as a backend | |
9161 | // struct. | |
9162 | ||
9163 | // However, when Go code refers to a function other than simply | |
9164 | // calling it, the backend type of that function is actually a struct. | |
9165 | // The first field of the struct points to the Go function code | |
9166 | // (sometimes a wrapper as described below). The remaining fields | |
9167 | // hold addresses of closed-over variables. This struct is called a | |
9168 | // closure. | |
9169 | ||
9170 | // There are a few cases to consider. | |
9171 | ||
9172 | // A direct function call of a known function in package scope. In | |
9173 | // this case there are no closed-over variables, and we know the name | |
9174 | // of the function code. We can simply produce a backend call to the | |
9175 | // function directly, and not worry about the closure. | |
9176 | ||
9177 | // A direct function call of a known function literal. In this case | |
9178 | // we know the function code and we know the closure. We generate the | |
9179 | // function code such that it expects an additional final argument of | |
9180 | // the closure type. We pass the closure as the last argument, after | |
9181 | // the other arguments. | |
9182 | ||
9183 | // An indirect function call. In this case we have a closure. We | |
9184 | // load the pointer to the function code from the first field of the | |
9185 | // closure. We pass the address of the closure as the last argument. | |
9186 | ||
9187 | // A call to a method of an interface. Type methods are always at | |
9188 | // package scope, so we call the function directly, and don't worry | |
9189 | // about the closure. | |
9190 | ||
9191 | // This means that for a function at package scope we have two cases. | |
9192 | // One is the direct call, which has no closure. The other is the | |
9193 | // indirect call, which does have a closure. We can't simply ignore | |
9194 | // the closure, even though it is the last argument, because that will | |
9195 | // fail on targets where the function pops its arguments. So when | |
9196 | // generating a closure for a package-scope function we set the | |
9197 | // function code pointer in the closure to point to a wrapper | |
9198 | // function. This wrapper function accepts a final argument that | |
9199 | // points to the closure, ignores it, and calls the real function as a | |
9200 | // direct function call. This wrapper will normally be efficient, and | |
9201 | // can often simply be a tail call to the real function. | |
9202 | ||
9203 | // We don't use GCC's static chain pointer because 1) we don't need | |
9204 | // it; 2) GCC only permits using a static chain to call a known | |
9205 | // function, so we can't use it for an indirect call anyhow. Since we | |
9206 | // can't use it for an indirect call, we may as well not worry about | |
9207 | // using it for a direct call either. | |
9208 | ||
9209 | // We pass the closure last rather than first because it means that | |
9210 | // the function wrapper we put into a closure for a package-scope | |
9211 | // function can normally just be a tail call to the real function. | |
9212 | ||
9213 | // For method expressions we generate a wrapper that loads the | |
9214 | // receiver from the closure and then calls the method. This | |
9215 | // unfortunately forces reshuffling the arguments, since there is a | |
9216 | // new first argument, but we can't avoid reshuffling either for | |
9217 | // method expressions or for indirect calls of package-scope | |
9218 | // functions, and since the latter are more common we reshuffle for | |
9219 | // method expressions. | |
9220 | ||
9221 | // Note that the Go code retains the Go types. The extra final | |
9222 | // argument only appears when we convert to the backend | |
9223 | // representation. | |
9224 | ||
e440a328 | 9225 | // Traversal. |
9226 | ||
9227 | int | |
9228 | Call_expression::do_traverse(Traverse* traverse) | |
9229 | { | |
0c0dacab | 9230 | // If we are calling a function in a different package that returns |
9231 | // an unnamed type, this may be the only chance we get to traverse | |
9232 | // that type. We don't traverse this->type_ because it may be a | |
9233 | // Call_multiple_result_type that will just lead back here. | |
9234 | if (this->type_ != NULL && !this->type_->is_error_type()) | |
9235 | { | |
9236 | Function_type *fntype = this->get_function_type(); | |
9237 | if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) | |
9238 | return TRAVERSE_EXIT; | |
9239 | } | |
e440a328 | 9240 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) |
9241 | return TRAVERSE_EXIT; | |
9242 | if (this->args_ != NULL) | |
9243 | { | |
9244 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
9245 | return TRAVERSE_EXIT; | |
9246 | } | |
9247 | return TRAVERSE_CONTINUE; | |
9248 | } | |
9249 | ||
9250 | // Lower a call statement. | |
9251 | ||
9252 | Expression* | |
ceeb4318 | 9253 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
9254 | Statement_inserter* inserter, int) | |
e440a328 | 9255 | { |
b13c66cd | 9256 | Location loc = this->location(); |
09ea332d | 9257 | |
ceeb4318 | 9258 | // A type cast can look like a function call. |
e440a328 | 9259 | if (this->fn_->is_type_expression() |
9260 | && this->args_ != NULL | |
9261 | && this->args_->size() == 1) | |
9262 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 9263 | loc); |
e440a328 | 9264 | |
88f06749 | 9265 | // Because do_type will return an error type and thus prevent future |
9266 | // errors, check for that case now to ensure that the error gets | |
9267 | // reported. | |
37448b10 | 9268 | Function_type* fntype = this->get_function_type(); |
9269 | if (fntype == NULL) | |
88f06749 | 9270 | { |
9271 | if (!this->fn_->type()->is_error()) | |
9272 | this->report_error(_("expected function")); | |
5f1045b5 | 9273 | this->set_is_error(); |
9274 | return this; | |
88f06749 | 9275 | } |
9276 | ||
e440a328 | 9277 | // Handle an argument which is a call to a function which returns |
9278 | // multiple results. | |
9279 | if (this->args_ != NULL | |
9280 | && this->args_->size() == 1 | |
37448b10 | 9281 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9282 | { |
e440a328 | 9283 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9284 | if (rc > 1 | |
37448b10 | 9285 | && ((fntype->parameters() != NULL |
9286 | && (fntype->parameters()->size() == rc | |
9287 | || (fntype->is_varargs() | |
9288 | && fntype->parameters()->size() - 1 <= rc))) | |
9289 | || fntype->is_builtin())) | |
e440a328 | 9290 | { |
9291 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 9292 | call->set_is_multi_value_arg(); |
c33af8e4 | 9293 | if (this->is_varargs_) |
9294 | { | |
9295 | // It is not clear which result of a multiple result call | |
9296 | // the ellipsis operator should be applied to. If we unpack the | |
9297 | // the call into its individual results here, the ellipsis will be | |
9298 | // applied to the last result. | |
631d5788 | 9299 | go_error_at(call->location(), |
9300 | _("multiple-value argument in single-value context")); | |
c33af8e4 | 9301 | return Expression::make_error(call->location()); |
9302 | } | |
9303 | ||
e440a328 | 9304 | Expression_list* args = new Expression_list; |
9305 | for (size_t i = 0; i < rc; ++i) | |
9306 | args->push_back(Expression::make_call_result(call, i)); | |
9307 | // We can't return a new call expression here, because this | |
42535814 | 9308 | // one may be referenced by Call_result expressions. We |
9309 | // also can't delete the old arguments, because we may still | |
9310 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9311 | this->args_ = args; |
9312 | } | |
9313 | } | |
9314 | ||
37448b10 | 9315 | // Recognize a call to a builtin function. |
9316 | if (fntype->is_builtin()) | |
9317 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9318 | this->is_varargs_, loc); | |
9319 | ||
ceeb4318 | 9320 | // If this call returns multiple results, create a temporary |
9321 | // variable for each result. | |
9322 | size_t rc = this->result_count(); | |
9323 | if (rc > 1 && this->results_ == NULL) | |
9324 | { | |
9325 | std::vector<Temporary_statement*>* temps = | |
9326 | new std::vector<Temporary_statement*>; | |
9327 | temps->reserve(rc); | |
37448b10 | 9328 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9329 | for (Typed_identifier_list::const_iterator p = results->begin(); |
9330 | p != results->end(); | |
9331 | ++p) | |
9332 | { | |
9333 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 9334 | NULL, loc); |
ceeb4318 | 9335 | inserter->insert(temp); |
9336 | temps->push_back(temp); | |
9337 | } | |
9338 | this->results_ = temps; | |
9339 | } | |
9340 | ||
e440a328 | 9341 | // Handle a call to a varargs function by packaging up the extra |
9342 | // parameters. | |
37448b10 | 9343 | if (fntype->is_varargs()) |
e440a328 | 9344 | { |
e440a328 | 9345 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9346 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9347 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9348 | this->lower_varargs(gogo, function, inserter, varargs_type, |
0e9a2e72 | 9349 | parameters->size(), SLICE_STORAGE_MAY_ESCAPE); |
09ea332d | 9350 | } |
9351 | ||
9352 | // If this is call to a method, call the method directly passing the | |
9353 | // object as the first parameter. | |
9354 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9355 | if (bme != NULL) | |
9356 | { | |
0afbb937 | 9357 | Named_object* methodfn = bme->function(); |
09ea332d | 9358 | Expression* first_arg = bme->first_argument(); |
9359 | ||
9360 | // We always pass a pointer when calling a method. | |
9361 | if (first_arg->type()->points_to() == NULL | |
9362 | && !first_arg->type()->is_error()) | |
9363 | { | |
9364 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9365 | // We may need to create a temporary variable so that we can | |
9366 | // take the address. We can't do that here because it will | |
9367 | // mess up the order of evaluation. | |
9368 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9369 | ue->set_create_temp(); | |
9370 | } | |
9371 | ||
9372 | // If we are calling a method which was inherited from an | |
9373 | // embedded struct, and the method did not get a stub, then the | |
9374 | // first type may be wrong. | |
9375 | Type* fatype = bme->first_argument_type(); | |
9376 | if (fatype != NULL) | |
9377 | { | |
9378 | if (fatype->points_to() == NULL) | |
9379 | fatype = Type::make_pointer_type(fatype); | |
9380 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9381 | } | |
9382 | ||
9383 | Expression_list* new_args = new Expression_list(); | |
9384 | new_args->push_back(first_arg); | |
9385 | if (this->args_ != NULL) | |
9386 | { | |
9387 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9388 | p != this->args_->end(); | |
9389 | ++p) | |
9390 | new_args->push_back(*p); | |
9391 | } | |
9392 | ||
9393 | // We have to change in place because this structure may be | |
9394 | // referenced by Call_result_expressions. We can't delete the | |
9395 | // old arguments, because we may be traversing them up in some | |
9396 | // caller. FIXME. | |
9397 | this->args_ = new_args; | |
0afbb937 | 9398 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9399 | bme->location()); |
e440a328 | 9400 | } |
9401 | ||
105f9a24 | 9402 | // Handle a couple of special runtime functions. In the runtime |
9403 | // package, getcallerpc returns the PC of the caller, and | |
9404 | // getcallersp returns the frame pointer of the caller. Implement | |
9405 | // these by turning them into calls to GCC builtin functions. We | |
9406 | // could implement them in normal code, but then we would have to | |
9407 | // explicitly unwind the stack. These functions are intended to be | |
9408 | // efficient. Note that this technique obviously only works for | |
9409 | // direct calls, but that is the only way they are used. The actual | |
9410 | // argument to these functions is always the address of a parameter; | |
9411 | // we don't need that for the GCC builtin functions, so we just | |
9412 | // ignore it. | |
9413 | if (gogo->compiling_runtime() | |
9414 | && this->args_ != NULL | |
9415 | && this->args_->size() == 1 | |
9416 | && gogo->package_name() == "runtime") | |
9417 | { | |
9418 | Func_expression* fe = this->fn_->func_expression(); | |
9419 | if (fe != NULL | |
9420 | && fe->named_object()->is_function_declaration() | |
9421 | && fe->named_object()->package() == NULL) | |
9422 | { | |
9423 | std::string n = Gogo::unpack_hidden_name(fe->named_object()->name()); | |
9424 | if (n == "getcallerpc") | |
9425 | { | |
9426 | static Named_object* builtin_return_address; | |
9427 | return this->lower_to_builtin(&builtin_return_address, | |
9428 | "__builtin_return_address", | |
9429 | 0); | |
9430 | } | |
9431 | else if (n == "getcallersp") | |
9432 | { | |
9433 | static Named_object* builtin_frame_address; | |
9434 | return this->lower_to_builtin(&builtin_frame_address, | |
9435 | "__builtin_frame_address", | |
9436 | 1); | |
9437 | } | |
9438 | } | |
9439 | } | |
9440 | ||
e440a328 | 9441 | return this; |
9442 | } | |
9443 | ||
9444 | // Lower a call to a varargs function. FUNCTION is the function in | |
9445 | // which the call occurs--it's not the function we are calling. | |
9446 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9447 | // PARAM_COUNT is the number of parameters of the function we are | |
9448 | // calling; the last of these parameters will be the varargs | |
9449 | // parameter. | |
9450 | ||
09ea332d | 9451 | void |
e440a328 | 9452 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9453 | Statement_inserter* inserter, |
0e9a2e72 | 9454 | Type* varargs_type, size_t param_count, |
9455 | Slice_storage_escape_disp escape_disp) | |
e440a328 | 9456 | { |
9457 | if (this->varargs_are_lowered_) | |
09ea332d | 9458 | return; |
e440a328 | 9459 | |
b13c66cd | 9460 | Location loc = this->location(); |
e440a328 | 9461 | |
c484d925 | 9462 | go_assert(param_count > 0); |
411eb89e | 9463 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9464 | |
9465 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9466 | if (arg_count < param_count - 1) | |
9467 | { | |
9468 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9469 | return; |
e440a328 | 9470 | } |
9471 | ||
9472 | Expression_list* old_args = this->args_; | |
9473 | Expression_list* new_args = new Expression_list(); | |
9474 | bool push_empty_arg = false; | |
9475 | if (old_args == NULL || old_args->empty()) | |
9476 | { | |
c484d925 | 9477 | go_assert(param_count == 1); |
e440a328 | 9478 | push_empty_arg = true; |
9479 | } | |
9480 | else | |
9481 | { | |
9482 | Expression_list::const_iterator pa; | |
9483 | int i = 1; | |
9484 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9485 | { | |
9486 | if (static_cast<size_t>(i) == param_count) | |
9487 | break; | |
9488 | new_args->push_back(*pa); | |
9489 | } | |
9490 | ||
9491 | // We have reached the varargs parameter. | |
9492 | ||
9493 | bool issued_error = false; | |
9494 | if (pa == old_args->end()) | |
9495 | push_empty_arg = true; | |
9496 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9497 | new_args->push_back(*pa); | |
9498 | else if (this->is_varargs_) | |
9499 | { | |
a6645f74 | 9500 | if ((*pa)->type()->is_slice_type()) |
9501 | this->report_error(_("too many arguments")); | |
9502 | else | |
9503 | { | |
631d5788 | 9504 | go_error_at(this->location(), |
9505 | _("invalid use of %<...%> with non-slice")); | |
a6645f74 | 9506 | this->set_is_error(); |
9507 | } | |
09ea332d | 9508 | return; |
e440a328 | 9509 | } |
e440a328 | 9510 | else |
9511 | { | |
9512 | Type* element_type = varargs_type->array_type()->element_type(); | |
9513 | Expression_list* vals = new Expression_list; | |
9514 | for (; pa != old_args->end(); ++pa, ++i) | |
9515 | { | |
9516 | // Check types here so that we get a better message. | |
9517 | Type* patype = (*pa)->type(); | |
b13c66cd | 9518 | Location paloc = (*pa)->location(); |
e440a328 | 9519 | if (!this->check_argument_type(i, element_type, patype, |
9520 | paloc, issued_error)) | |
9521 | continue; | |
9522 | vals->push_back(*pa); | |
9523 | } | |
0e9a2e72 | 9524 | Slice_construction_expression* sce = |
e440a328 | 9525 | Expression::make_slice_composite_literal(varargs_type, vals, loc); |
0e9a2e72 | 9526 | if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) |
9527 | sce->set_storage_does_not_escape(); | |
9528 | Expression* val = sce; | |
09ea332d | 9529 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9530 | new_args->push_back(val); |
9531 | } | |
9532 | } | |
9533 | ||
9534 | if (push_empty_arg) | |
9535 | new_args->push_back(Expression::make_nil(loc)); | |
9536 | ||
9537 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9538 | // be referenced by Call_result expressions. FIXME. We can't |
9539 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9540 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9541 | this->args_ = new_args; |
9542 | this->varargs_are_lowered_ = true; | |
e440a328 | 9543 | } |
9544 | ||
105f9a24 | 9545 | // Return a call to __builtin_return_address or __builtin_frame_address. |
9546 | ||
9547 | Expression* | |
9548 | Call_expression::lower_to_builtin(Named_object** pno, const char* name, | |
9549 | int arg) | |
9550 | { | |
9551 | if (*pno == NULL) | |
9552 | *pno = Gogo::declare_builtin_rf_address(name); | |
9553 | ||
9554 | Location loc = this->location(); | |
9555 | ||
9556 | Expression* fn = Expression::make_func_reference(*pno, NULL, loc); | |
9557 | Expression* a = Expression::make_integer_ul(arg, NULL, loc); | |
9558 | Expression_list *args = new Expression_list(); | |
9559 | args->push_back(a); | |
9560 | Expression* call = Expression::make_call(fn, args, false, loc); | |
9561 | ||
9562 | // The builtin functions return void*, but the Go functions return uintptr. | |
9563 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9564 | return Expression::make_cast(uintptr_type, call, loc); | |
9565 | } | |
9566 | ||
2c809f8f | 9567 | // Flatten a call with multiple results into a temporary. |
9568 | ||
9569 | Expression* | |
b8e86a51 | 9570 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
9571 | Statement_inserter* inserter) | |
2c809f8f | 9572 | { |
5bf8be8b | 9573 | if (this->is_erroneous_call()) |
9574 | { | |
9575 | go_assert(saw_errors()); | |
9576 | return Expression::make_error(this->location()); | |
9577 | } | |
b8e86a51 | 9578 | |
91c0fd76 | 9579 | if (this->is_flattened_) |
9580 | return this; | |
9581 | this->is_flattened_ = true; | |
9582 | ||
b8e86a51 | 9583 | // Add temporary variables for all arguments that require type |
9584 | // conversion. | |
9585 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 9586 | if (fntype == NULL) |
9587 | { | |
9588 | go_assert(saw_errors()); | |
9589 | return this; | |
9590 | } | |
b8e86a51 | 9591 | if (this->args_ != NULL && !this->args_->empty() |
9592 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
9593 | { | |
9594 | bool is_interface_method = | |
9595 | this->fn_->interface_field_reference_expression() != NULL; | |
9596 | ||
9597 | Expression_list *args = new Expression_list(); | |
9598 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
9599 | Expression_list::const_iterator pa = this->args_->begin(); | |
9600 | if (!is_interface_method && fntype->is_method()) | |
9601 | { | |
9602 | // The receiver argument. | |
9603 | args->push_back(*pa); | |
9604 | ++pa; | |
9605 | } | |
9606 | for (; pa != this->args_->end(); ++pa, ++pp) | |
9607 | { | |
9608 | go_assert(pp != fntype->parameters()->end()); | |
9609 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
9610 | args->push_back(*pa); | |
9611 | else | |
9612 | { | |
9613 | Location loc = (*pa)->location(); | |
8ba8cc87 | 9614 | Expression* arg = *pa; |
9615 | if (!arg->is_variable()) | |
9616 | { | |
9617 | Temporary_statement *temp = | |
9618 | Statement::make_temporary(NULL, arg, loc); | |
9619 | inserter->insert(temp); | |
9620 | arg = Expression::make_temporary_reference(temp, loc); | |
9621 | } | |
9622 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
9623 | loc); | |
9624 | args->push_back(arg); | |
b8e86a51 | 9625 | } |
9626 | } | |
9627 | delete this->args_; | |
9628 | this->args_ = args; | |
9629 | } | |
9630 | ||
2c809f8f | 9631 | size_t rc = this->result_count(); |
9632 | if (rc > 1 && this->call_temp_ == NULL) | |
9633 | { | |
9634 | Struct_field_list* sfl = new Struct_field_list(); | |
9635 | Function_type* fntype = this->get_function_type(); | |
9636 | const Typed_identifier_list* results = fntype->results(); | |
9637 | Location loc = this->location(); | |
9638 | ||
9639 | int i = 0; | |
61575e0f | 9640 | char buf[20]; |
2c809f8f | 9641 | for (Typed_identifier_list::const_iterator p = results->begin(); |
9642 | p != results->end(); | |
9643 | ++p, ++i) | |
9644 | { | |
9645 | snprintf(buf, sizeof buf, "res%d", i); | |
9646 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9647 | } | |
9648 | ||
9649 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9650 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9651 | inserter->insert(this->call_temp_); | |
9652 | } | |
9653 | ||
9654 | return this; | |
9655 | } | |
9656 | ||
ceeb4318 | 9657 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9658 | |
9659 | Function_type* | |
9660 | Call_expression::get_function_type() const | |
9661 | { | |
9662 | return this->fn_->type()->function_type(); | |
9663 | } | |
9664 | ||
9665 | // Return the number of values which this call will return. | |
9666 | ||
9667 | size_t | |
9668 | Call_expression::result_count() const | |
9669 | { | |
9670 | const Function_type* fntype = this->get_function_type(); | |
9671 | if (fntype == NULL) | |
9672 | return 0; | |
9673 | if (fntype->results() == NULL) | |
9674 | return 0; | |
9675 | return fntype->results()->size(); | |
9676 | } | |
9677 | ||
ceeb4318 | 9678 | // Return the temporary which holds a result. |
9679 | ||
9680 | Temporary_statement* | |
9681 | Call_expression::result(size_t i) const | |
9682 | { | |
cd238b8d | 9683 | if (this->results_ == NULL || this->results_->size() <= i) |
9684 | { | |
9685 | go_assert(saw_errors()); | |
9686 | return NULL; | |
9687 | } | |
ceeb4318 | 9688 | return (*this->results_)[i]; |
9689 | } | |
9690 | ||
1373401e | 9691 | // Set the number of results expected from a call expression. |
9692 | ||
9693 | void | |
9694 | Call_expression::set_expected_result_count(size_t count) | |
9695 | { | |
9696 | go_assert(this->expected_result_count_ == 0); | |
9697 | this->expected_result_count_ = count; | |
9698 | } | |
9699 | ||
e440a328 | 9700 | // Return whether this is a call to the predeclared function recover. |
9701 | ||
9702 | bool | |
9703 | Call_expression::is_recover_call() const | |
9704 | { | |
9705 | return this->do_is_recover_call(); | |
9706 | } | |
9707 | ||
9708 | // Set the argument to the recover function. | |
9709 | ||
9710 | void | |
9711 | Call_expression::set_recover_arg(Expression* arg) | |
9712 | { | |
9713 | this->do_set_recover_arg(arg); | |
9714 | } | |
9715 | ||
9716 | // Virtual functions also implemented by Builtin_call_expression. | |
9717 | ||
9718 | bool | |
9719 | Call_expression::do_is_recover_call() const | |
9720 | { | |
9721 | return false; | |
9722 | } | |
9723 | ||
9724 | void | |
9725 | Call_expression::do_set_recover_arg(Expression*) | |
9726 | { | |
c3e6f413 | 9727 | go_unreachable(); |
e440a328 | 9728 | } |
9729 | ||
ceeb4318 | 9730 | // We have found an error with this call expression; return true if |
9731 | // we should report it. | |
9732 | ||
9733 | bool | |
9734 | Call_expression::issue_error() | |
9735 | { | |
9736 | if (this->issued_error_) | |
9737 | return false; | |
9738 | else | |
9739 | { | |
9740 | this->issued_error_ = true; | |
9741 | return true; | |
9742 | } | |
9743 | } | |
9744 | ||
5bf8be8b | 9745 | // Whether or not this call contains errors, either in the call or the |
9746 | // arguments to the call. | |
9747 | ||
9748 | bool | |
9749 | Call_expression::is_erroneous_call() | |
9750 | { | |
9751 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9752 | return true; | |
9753 | ||
9754 | if (this->args() == NULL) | |
9755 | return false; | |
9756 | for (Expression_list::iterator pa = this->args()->begin(); | |
9757 | pa != this->args()->end(); | |
9758 | ++pa) | |
9759 | { | |
9760 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9761 | return true; | |
9762 | } | |
9763 | return false; | |
9764 | } | |
9765 | ||
e440a328 | 9766 | // Get the type. |
9767 | ||
9768 | Type* | |
9769 | Call_expression::do_type() | |
9770 | { | |
9771 | if (this->type_ != NULL) | |
9772 | return this->type_; | |
9773 | ||
9774 | Type* ret; | |
9775 | Function_type* fntype = this->get_function_type(); | |
9776 | if (fntype == NULL) | |
9777 | return Type::make_error_type(); | |
9778 | ||
9779 | const Typed_identifier_list* results = fntype->results(); | |
9780 | if (results == NULL) | |
9781 | ret = Type::make_void_type(); | |
9782 | else if (results->size() == 1) | |
9783 | ret = results->begin()->type(); | |
9784 | else | |
9785 | ret = Type::make_call_multiple_result_type(this); | |
9786 | ||
9787 | this->type_ = ret; | |
9788 | ||
9789 | return this->type_; | |
9790 | } | |
9791 | ||
9792 | // Determine types for a call expression. We can use the function | |
9793 | // parameter types to set the types of the arguments. | |
9794 | ||
9795 | void | |
9796 | Call_expression::do_determine_type(const Type_context*) | |
9797 | { | |
fb94b0ca | 9798 | if (!this->determining_types()) |
9799 | return; | |
9800 | ||
e440a328 | 9801 | this->fn_->determine_type_no_context(); |
9802 | Function_type* fntype = this->get_function_type(); | |
9803 | const Typed_identifier_list* parameters = NULL; | |
9804 | if (fntype != NULL) | |
9805 | parameters = fntype->parameters(); | |
9806 | if (this->args_ != NULL) | |
9807 | { | |
9808 | Typed_identifier_list::const_iterator pt; | |
9809 | if (parameters != NULL) | |
9810 | pt = parameters->begin(); | |
09ea332d | 9811 | bool first = true; |
e440a328 | 9812 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9813 | pa != this->args_->end(); | |
9814 | ++pa) | |
9815 | { | |
09ea332d | 9816 | if (first) |
9817 | { | |
9818 | first = false; | |
9819 | // If this is a method, the first argument is the | |
9820 | // receiver. | |
9821 | if (fntype != NULL && fntype->is_method()) | |
9822 | { | |
9823 | Type* rtype = fntype->receiver()->type(); | |
9824 | // The receiver is always passed as a pointer. | |
9825 | if (rtype->points_to() == NULL) | |
9826 | rtype = Type::make_pointer_type(rtype); | |
9827 | Type_context subcontext(rtype, false); | |
9828 | (*pa)->determine_type(&subcontext); | |
9829 | continue; | |
9830 | } | |
9831 | } | |
9832 | ||
e440a328 | 9833 | if (parameters != NULL && pt != parameters->end()) |
9834 | { | |
9835 | Type_context subcontext(pt->type(), false); | |
9836 | (*pa)->determine_type(&subcontext); | |
9837 | ++pt; | |
9838 | } | |
9839 | else | |
9840 | (*pa)->determine_type_no_context(); | |
9841 | } | |
9842 | } | |
9843 | } | |
9844 | ||
fb94b0ca | 9845 | // Called when determining types for a Call_expression. Return true |
9846 | // if we should go ahead, false if they have already been determined. | |
9847 | ||
9848 | bool | |
9849 | Call_expression::determining_types() | |
9850 | { | |
9851 | if (this->types_are_determined_) | |
9852 | return false; | |
9853 | else | |
9854 | { | |
9855 | this->types_are_determined_ = true; | |
9856 | return true; | |
9857 | } | |
9858 | } | |
9859 | ||
e440a328 | 9860 | // Check types for parameter I. |
9861 | ||
9862 | bool | |
9863 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9864 | const Type* argument_type, | |
b13c66cd | 9865 | Location argument_location, |
e440a328 | 9866 | bool issued_error) |
9867 | { | |
9868 | std::string reason; | |
1eae365b | 9869 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 9870 | { |
9871 | if (!issued_error) | |
9872 | { | |
9873 | if (reason.empty()) | |
631d5788 | 9874 | go_error_at(argument_location, "argument %d has incompatible type", i); |
e440a328 | 9875 | else |
631d5788 | 9876 | go_error_at(argument_location, |
9877 | "argument %d has incompatible type (%s)", | |
9878 | i, reason.c_str()); | |
e440a328 | 9879 | } |
9880 | this->set_is_error(); | |
9881 | return false; | |
9882 | } | |
9883 | return true; | |
9884 | } | |
9885 | ||
9886 | // Check types. | |
9887 | ||
9888 | void | |
9889 | Call_expression::do_check_types(Gogo*) | |
9890 | { | |
a6645f74 | 9891 | if (this->classification() == EXPRESSION_ERROR) |
9892 | return; | |
9893 | ||
e440a328 | 9894 | Function_type* fntype = this->get_function_type(); |
9895 | if (fntype == NULL) | |
9896 | { | |
5c13bd80 | 9897 | if (!this->fn_->type()->is_error()) |
e440a328 | 9898 | this->report_error(_("expected function")); |
9899 | return; | |
9900 | } | |
9901 | ||
1373401e | 9902 | if (this->expected_result_count_ != 0 |
9903 | && this->expected_result_count_ != this->result_count()) | |
9904 | { | |
9905 | if (this->issue_error()) | |
9906 | this->report_error(_("function result count mismatch")); | |
9907 | this->set_is_error(); | |
9908 | return; | |
9909 | } | |
9910 | ||
09ea332d | 9911 | bool is_method = fntype->is_method(); |
9912 | if (is_method) | |
e440a328 | 9913 | { |
09ea332d | 9914 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9915 | Type* rtype = fntype->receiver()->type(); | |
9916 | Expression* first_arg = this->args_->front(); | |
1eae365b | 9917 | // We dereference the values since receivers are always passed |
9918 | // as pointers. | |
09ea332d | 9919 | std::string reason; |
1eae365b | 9920 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
9921 | &reason)) | |
e440a328 | 9922 | { |
09ea332d | 9923 | if (reason.empty()) |
9924 | this->report_error(_("incompatible type for receiver")); | |
9925 | else | |
e440a328 | 9926 | { |
631d5788 | 9927 | go_error_at(this->location(), |
9928 | "incompatible type for receiver (%s)", | |
9929 | reason.c_str()); | |
09ea332d | 9930 | this->set_is_error(); |
e440a328 | 9931 | } |
9932 | } | |
9933 | } | |
9934 | ||
9935 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 9936 | // we don't have to worry about it here unless something is wrong. |
9937 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
9938 | { | |
9939 | if (!fntype->is_varargs()) | |
9940 | { | |
631d5788 | 9941 | go_error_at(this->location(), |
9942 | _("invalid use of %<...%> calling non-variadic function")); | |
a6645f74 | 9943 | this->set_is_error(); |
9944 | return; | |
9945 | } | |
9946 | } | |
e440a328 | 9947 | |
9948 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9949 | if (this->args_ == NULL) | |
9950 | { | |
9951 | if (parameters != NULL && !parameters->empty()) | |
9952 | this->report_error(_("not enough arguments")); | |
9953 | } | |
9954 | else if (parameters == NULL) | |
09ea332d | 9955 | { |
9956 | if (!is_method || this->args_->size() > 1) | |
9957 | this->report_error(_("too many arguments")); | |
9958 | } | |
1373401e | 9959 | else if (this->args_->size() == 1 |
9960 | && this->args_->front()->call_expression() != NULL | |
9961 | && this->args_->front()->call_expression()->result_count() > 1) | |
9962 | { | |
9963 | // This is F(G()) when G returns more than one result. If the | |
9964 | // results can be matched to parameters, it would have been | |
9965 | // lowered in do_lower. If we get here we know there is a | |
9966 | // mismatch. | |
9967 | if (this->args_->front()->call_expression()->result_count() | |
9968 | < parameters->size()) | |
9969 | this->report_error(_("not enough arguments")); | |
9970 | else | |
9971 | this->report_error(_("too many arguments")); | |
9972 | } | |
e440a328 | 9973 | else |
9974 | { | |
9975 | int i = 0; | |
09ea332d | 9976 | Expression_list::const_iterator pa = this->args_->begin(); |
9977 | if (is_method) | |
9978 | ++pa; | |
9979 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9980 | pt != parameters->end(); | |
9981 | ++pt, ++pa, ++i) | |
e440a328 | 9982 | { |
09ea332d | 9983 | if (pa == this->args_->end()) |
e440a328 | 9984 | { |
09ea332d | 9985 | this->report_error(_("not enough arguments")); |
e440a328 | 9986 | return; |
9987 | } | |
9988 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9989 | (*pa)->location(), false); | |
9990 | } | |
09ea332d | 9991 | if (pa != this->args_->end()) |
9992 | this->report_error(_("too many arguments")); | |
e440a328 | 9993 | } |
9994 | } | |
9995 | ||
72666aed | 9996 | Expression* |
9997 | Call_expression::do_copy() | |
9998 | { | |
9999 | Call_expression* call = | |
10000 | Expression::make_call(this->fn_->copy(), | |
10001 | (this->args_ == NULL | |
10002 | ? NULL | |
10003 | : this->args_->copy()), | |
10004 | this->is_varargs_, this->location()); | |
10005 | ||
10006 | if (this->varargs_are_lowered_) | |
10007 | call->set_varargs_are_lowered(); | |
10008 | return call; | |
10009 | } | |
10010 | ||
e440a328 | 10011 | // Return whether we have to use a temporary variable to ensure that |
10012 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 10013 | // results then it will inevitably be executed last. |
e440a328 | 10014 | |
10015 | bool | |
10016 | Call_expression::do_must_eval_in_order() const | |
10017 | { | |
ceeb4318 | 10018 | return this->result_count() > 0; |
e440a328 | 10019 | } |
10020 | ||
e440a328 | 10021 | // Get the function and the first argument to use when calling an |
10022 | // interface method. | |
10023 | ||
2387f644 | 10024 | Expression* |
e440a328 | 10025 | Call_expression::interface_method_function( |
e440a328 | 10026 | Interface_field_reference_expression* interface_method, |
2387f644 | 10027 | Expression** first_arg_ptr) |
e440a328 | 10028 | { |
2387f644 | 10029 | *first_arg_ptr = interface_method->get_underlying_object(); |
10030 | return interface_method->get_function(); | |
e440a328 | 10031 | } |
10032 | ||
10033 | // Build the call expression. | |
10034 | ||
ea664253 | 10035 | Bexpression* |
10036 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10037 | { |
2c809f8f | 10038 | if (this->call_ != NULL) |
ea664253 | 10039 | return this->call_; |
e440a328 | 10040 | |
10041 | Function_type* fntype = this->get_function_type(); | |
10042 | if (fntype == NULL) | |
ea664253 | 10043 | return context->backend()->error_expression(); |
e440a328 | 10044 | |
10045 | if (this->fn_->is_error_expression()) | |
ea664253 | 10046 | return context->backend()->error_expression(); |
e440a328 | 10047 | |
10048 | Gogo* gogo = context->gogo(); | |
b13c66cd | 10049 | Location location = this->location(); |
e440a328 | 10050 | |
10051 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 10052 | Interface_field_reference_expression* interface_method = |
10053 | this->fn_->interface_field_reference_expression(); | |
10054 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 10055 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 10056 | |
f8bdf81a | 10057 | bool has_closure_arg; |
8381eda7 | 10058 | if (has_closure) |
f8bdf81a | 10059 | has_closure_arg = true; |
8381eda7 | 10060 | else if (func != NULL) |
f8bdf81a | 10061 | has_closure_arg = false; |
8381eda7 | 10062 | else if (is_interface_method) |
f8bdf81a | 10063 | has_closure_arg = false; |
8381eda7 | 10064 | else |
f8bdf81a | 10065 | has_closure_arg = true; |
8381eda7 | 10066 | |
e440a328 | 10067 | int nargs; |
2c809f8f | 10068 | std::vector<Bexpression*> fn_args; |
e440a328 | 10069 | if (this->args_ == NULL || this->args_->empty()) |
10070 | { | |
f8bdf81a | 10071 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 10072 | if (nargs > 0) |
10073 | fn_args.resize(1); | |
e440a328 | 10074 | } |
09ea332d | 10075 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
10076 | { | |
10077 | // Passing a receiver parameter. | |
10078 | go_assert(!is_interface_method | |
10079 | && fntype->is_method() | |
10080 | && this->args_->size() == 1); | |
f8bdf81a | 10081 | nargs = 1; |
2c809f8f | 10082 | fn_args.resize(1); |
ea664253 | 10083 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 10084 | } |
e440a328 | 10085 | else |
10086 | { | |
10087 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 10088 | |
10089 | nargs = this->args_->size(); | |
09ea332d | 10090 | int i = is_interface_method ? 1 : 0; |
e440a328 | 10091 | nargs += i; |
2c809f8f | 10092 | fn_args.resize(nargs); |
e440a328 | 10093 | |
10094 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 10095 | Expression_list::const_iterator pe = this->args_->begin(); |
10096 | if (!is_interface_method && fntype->is_method()) | |
10097 | { | |
ea664253 | 10098 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 10099 | ++pe; |
10100 | ++i; | |
10101 | } | |
10102 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 10103 | { |
c484d925 | 10104 | go_assert(pp != params->end()); |
2c809f8f | 10105 | Expression* arg = |
10106 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
10107 | location); | |
ea664253 | 10108 | fn_args[i] = arg->get_backend(context); |
e440a328 | 10109 | } |
c484d925 | 10110 | go_assert(pp == params->end()); |
f8bdf81a | 10111 | go_assert(i == nargs); |
e440a328 | 10112 | } |
10113 | ||
2c809f8f | 10114 | Expression* fn; |
10115 | Expression* closure = NULL; | |
8381eda7 | 10116 | if (func != NULL) |
10117 | { | |
10118 | Named_object* no = func->named_object(); | |
2c809f8f | 10119 | fn = Expression::make_func_code_reference(no, location); |
10120 | if (has_closure) | |
10121 | closure = func->closure(); | |
8381eda7 | 10122 | } |
09ea332d | 10123 | else if (!is_interface_method) |
8381eda7 | 10124 | { |
2c809f8f | 10125 | closure = this->fn_; |
10126 | ||
10127 | // The backend representation of this function type is a pointer | |
10128 | // to a struct whose first field is the actual function to call. | |
10129 | Type* pfntype = | |
10130 | Type::make_pointer_type( | |
10131 | Type::make_pointer_type(Type::make_void_type())); | |
10132 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
10133 | fn = Expression::make_unary(OPERATOR_MULT, fn, location); | |
10134 | } | |
e440a328 | 10135 | else |
cf609de4 | 10136 | { |
2387f644 | 10137 | Expression* first_arg; |
2c809f8f | 10138 | fn = this->interface_method_function(interface_method, &first_arg); |
ea664253 | 10139 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 10140 | } |
10141 | ||
1ecc6157 | 10142 | Bexpression* bclosure = NULL; |
10143 | if (has_closure_arg) | |
10144 | bclosure = closure->get_backend(context); | |
f8bdf81a | 10145 | else |
1ecc6157 | 10146 | go_assert(closure == NULL); |
f8bdf81a | 10147 | |
ea664253 | 10148 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 10149 | |
10150 | // When not calling a named function directly, use a type conversion | |
10151 | // in case the type of the function is a recursive type which refers | |
10152 | // to itself. We don't do this for an interface method because 1) | |
10153 | // an interface method never refers to itself, so we always have a | |
10154 | // function type here; 2) we pass an extra first argument to an | |
10155 | // interface method, so fntype is not correct. | |
10156 | if (func == NULL && !is_interface_method) | |
10157 | { | |
10158 | Btype* bft = fntype->get_backend_fntype(gogo); | |
10159 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
10160 | } | |
10161 | ||
1ecc6157 | 10162 | Bexpression* call = gogo->backend()->call_expression(bfn, fn_args, |
10163 | bclosure, location); | |
e440a328 | 10164 | |
2c809f8f | 10165 | if (this->results_ != NULL) |
e440a328 | 10166 | { |
2c809f8f | 10167 | go_assert(this->call_temp_ != NULL); |
10168 | Expression* call_ref = | |
10169 | Expression::make_temporary_reference(this->call_temp_, location); | |
ea664253 | 10170 | Bexpression* bcall_ref = call_ref->get_backend(context); |
2c809f8f | 10171 | Bstatement* assn_stmt = |
10172 | gogo->backend()->assignment_statement(bcall_ref, call, location); | |
e440a328 | 10173 | |
2c809f8f | 10174 | this->call_ = this->set_results(context, bcall_ref); |
e440a328 | 10175 | |
2c809f8f | 10176 | Bexpression* set_and_call = |
10177 | gogo->backend()->compound_expression(assn_stmt, this->call_, | |
10178 | location); | |
ea664253 | 10179 | return set_and_call; |
2c809f8f | 10180 | } |
e440a328 | 10181 | |
2c809f8f | 10182 | this->call_ = call; |
ea664253 | 10183 | return this->call_; |
e440a328 | 10184 | } |
10185 | ||
ceeb4318 | 10186 | // Set the result variables if this call returns multiple results. |
10187 | ||
2c809f8f | 10188 | Bexpression* |
10189 | Call_expression::set_results(Translate_context* context, Bexpression* call) | |
ceeb4318 | 10190 | { |
2c809f8f | 10191 | Gogo* gogo = context->gogo(); |
ceeb4318 | 10192 | |
2c809f8f | 10193 | Bexpression* results = NULL; |
b13c66cd | 10194 | Location loc = this->location(); |
2c809f8f | 10195 | |
ceeb4318 | 10196 | size_t rc = this->result_count(); |
2c809f8f | 10197 | for (size_t i = 0; i < rc; ++i) |
ceeb4318 | 10198 | { |
ceeb4318 | 10199 | Temporary_statement* temp = this->result(i); |
cd238b8d | 10200 | if (temp == NULL) |
10201 | { | |
10202 | go_assert(saw_errors()); | |
2c809f8f | 10203 | return gogo->backend()->error_expression(); |
cd238b8d | 10204 | } |
ceeb4318 | 10205 | Temporary_reference_expression* ref = |
10206 | Expression::make_temporary_reference(temp, loc); | |
10207 | ref->set_is_lvalue(); | |
ceeb4318 | 10208 | |
ea664253 | 10209 | Bexpression* result_ref = ref->get_backend(context); |
2c809f8f | 10210 | Bexpression* call_result = |
10211 | gogo->backend()->struct_field_expression(call, i, loc); | |
10212 | Bstatement* assn_stmt = | |
10213 | gogo->backend()->assignment_statement(result_ref, call_result, loc); | |
ceeb4318 | 10214 | |
2c809f8f | 10215 | Bexpression* result = |
10216 | gogo->backend()->compound_expression(assn_stmt, call_result, loc); | |
ceeb4318 | 10217 | |
2c809f8f | 10218 | if (results == NULL) |
10219 | results = result; | |
10220 | else | |
10221 | { | |
10222 | Bstatement* expr_stmt = gogo->backend()->expression_statement(result); | |
10223 | results = | |
10224 | gogo->backend()->compound_expression(expr_stmt, results, loc); | |
10225 | } | |
10226 | } | |
10227 | return results; | |
ceeb4318 | 10228 | } |
10229 | ||
d751bb78 | 10230 | // Dump ast representation for a call expressin. |
10231 | ||
10232 | void | |
10233 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
10234 | { | |
10235 | this->fn_->dump_expression(ast_dump_context); | |
10236 | ast_dump_context->ostream() << "("; | |
10237 | if (args_ != NULL) | |
10238 | ast_dump_context->dump_expression_list(this->args_); | |
10239 | ||
10240 | ast_dump_context->ostream() << ") "; | |
10241 | } | |
10242 | ||
e440a328 | 10243 | // Make a call expression. |
10244 | ||
10245 | Call_expression* | |
10246 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 10247 | Location location) |
e440a328 | 10248 | { |
10249 | return new Call_expression(fn, args, is_varargs, location); | |
10250 | } | |
10251 | ||
da244e59 | 10252 | // Class Call_result_expression. |
e440a328 | 10253 | |
10254 | // Traverse a call result. | |
10255 | ||
10256 | int | |
10257 | Call_result_expression::do_traverse(Traverse* traverse) | |
10258 | { | |
10259 | if (traverse->remember_expression(this->call_)) | |
10260 | { | |
10261 | // We have already traversed the call expression. | |
10262 | return TRAVERSE_CONTINUE; | |
10263 | } | |
10264 | return Expression::traverse(&this->call_, traverse); | |
10265 | } | |
10266 | ||
10267 | // Get the type. | |
10268 | ||
10269 | Type* | |
10270 | Call_result_expression::do_type() | |
10271 | { | |
425dd051 | 10272 | if (this->classification() == EXPRESSION_ERROR) |
10273 | return Type::make_error_type(); | |
10274 | ||
e440a328 | 10275 | // THIS->CALL_ can be replaced with a temporary reference due to |
10276 | // Call_expression::do_must_eval_in_order when there is an error. | |
10277 | Call_expression* ce = this->call_->call_expression(); | |
10278 | if (ce == NULL) | |
5e85f268 | 10279 | { |
10280 | this->set_is_error(); | |
10281 | return Type::make_error_type(); | |
10282 | } | |
e440a328 | 10283 | Function_type* fntype = ce->get_function_type(); |
10284 | if (fntype == NULL) | |
5e85f268 | 10285 | { |
e37658e2 | 10286 | if (ce->issue_error()) |
99b3f06f | 10287 | { |
10288 | if (!ce->fn()->type()->is_error()) | |
10289 | this->report_error(_("expected function")); | |
10290 | } | |
5e85f268 | 10291 | this->set_is_error(); |
10292 | return Type::make_error_type(); | |
10293 | } | |
e440a328 | 10294 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10295 | if (results == NULL || results->size() < 2) |
7b8d861f | 10296 | { |
ceeb4318 | 10297 | if (ce->issue_error()) |
10298 | this->report_error(_("number of results does not match " | |
10299 | "number of values")); | |
7b8d861f | 10300 | return Type::make_error_type(); |
10301 | } | |
e440a328 | 10302 | Typed_identifier_list::const_iterator pr = results->begin(); |
10303 | for (unsigned int i = 0; i < this->index_; ++i) | |
10304 | { | |
10305 | if (pr == results->end()) | |
425dd051 | 10306 | break; |
e440a328 | 10307 | ++pr; |
10308 | } | |
10309 | if (pr == results->end()) | |
425dd051 | 10310 | { |
ceeb4318 | 10311 | if (ce->issue_error()) |
10312 | this->report_error(_("number of results does not match " | |
10313 | "number of values")); | |
425dd051 | 10314 | return Type::make_error_type(); |
10315 | } | |
e440a328 | 10316 | return pr->type(); |
10317 | } | |
10318 | ||
425dd051 | 10319 | // Check the type. Just make sure that we trigger the warning in |
10320 | // do_type. | |
e440a328 | 10321 | |
10322 | void | |
10323 | Call_result_expression::do_check_types(Gogo*) | |
10324 | { | |
425dd051 | 10325 | this->type(); |
e440a328 | 10326 | } |
10327 | ||
10328 | // Determine the type. We have nothing to do here, but the 0 result | |
10329 | // needs to pass down to the caller. | |
10330 | ||
10331 | void | |
10332 | Call_result_expression::do_determine_type(const Type_context*) | |
10333 | { | |
fb94b0ca | 10334 | this->call_->determine_type_no_context(); |
e440a328 | 10335 | } |
10336 | ||
ea664253 | 10337 | // Return the backend representation. We just refer to the temporary set by the |
10338 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 10339 | // hard to evaluate the call at the right time. |
e440a328 | 10340 | |
ea664253 | 10341 | Bexpression* |
10342 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10343 | { |
ceeb4318 | 10344 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10345 | if (ce == NULL) |
10346 | { | |
10347 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 10348 | return context->backend()->error_expression(); |
cd238b8d | 10349 | } |
ceeb4318 | 10350 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 10351 | if (ts == NULL) |
10352 | { | |
10353 | go_assert(saw_errors()); | |
ea664253 | 10354 | return context->backend()->error_expression(); |
cd238b8d | 10355 | } |
ceeb4318 | 10356 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
ea664253 | 10357 | return ref->get_backend(context); |
e440a328 | 10358 | } |
10359 | ||
d751bb78 | 10360 | // Dump ast representation for a call result expression. |
10361 | ||
10362 | void | |
10363 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10364 | const | |
10365 | { | |
10366 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10367 | // (struct) and the fields are referenced instead. | |
10368 | ast_dump_context->ostream() << this->index_ << "@("; | |
10369 | ast_dump_context->dump_expression(this->call_); | |
10370 | ast_dump_context->ostream() << ")"; | |
10371 | } | |
10372 | ||
e440a328 | 10373 | // Make a reference to a single result of a call which returns |
10374 | // multiple results. | |
10375 | ||
10376 | Expression* | |
10377 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10378 | { | |
10379 | return new Call_result_expression(call, index); | |
10380 | } | |
10381 | ||
10382 | // Class Index_expression. | |
10383 | ||
10384 | // Traversal. | |
10385 | ||
10386 | int | |
10387 | Index_expression::do_traverse(Traverse* traverse) | |
10388 | { | |
10389 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10390 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10391 | || (this->end_ != NULL | |
acf2b673 | 10392 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
10393 | || (this->cap_ != NULL | |
10394 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 10395 | return TRAVERSE_EXIT; |
10396 | return TRAVERSE_CONTINUE; | |
10397 | } | |
10398 | ||
10399 | // Lower an index expression. This converts the generic index | |
10400 | // expression into an array index, a string index, or a map index. | |
10401 | ||
10402 | Expression* | |
ceeb4318 | 10403 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10404 | { |
b13c66cd | 10405 | Location location = this->location(); |
e440a328 | 10406 | Expression* left = this->left_; |
10407 | Expression* start = this->start_; | |
10408 | Expression* end = this->end_; | |
acf2b673 | 10409 | Expression* cap = this->cap_; |
e440a328 | 10410 | |
10411 | Type* type = left->type(); | |
5c13bd80 | 10412 | if (type->is_error()) |
d9f3743a | 10413 | { |
10414 | go_assert(saw_errors()); | |
10415 | return Expression::make_error(location); | |
10416 | } | |
b0cf7ddd | 10417 | else if (left->is_type_expression()) |
10418 | { | |
631d5788 | 10419 | go_error_at(location, "attempt to index type expression"); |
b0cf7ddd | 10420 | return Expression::make_error(location); |
10421 | } | |
e440a328 | 10422 | else if (type->array_type() != NULL) |
acf2b673 | 10423 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 10424 | else if (type->points_to() != NULL |
10425 | && type->points_to()->array_type() != NULL | |
411eb89e | 10426 | && !type->points_to()->is_slice_type()) |
e440a328 | 10427 | { |
10428 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
10429 | location); | |
38092374 | 10430 | |
10431 | // For an ordinary index into the array, the pointer will be | |
10432 | // dereferenced. For a slice it will not--the resulting slice | |
10433 | // will simply reuse the pointer, which is incorrect if that | |
10434 | // pointer is nil. | |
10435 | if (end != NULL || cap != NULL) | |
10436 | deref->issue_nil_check(); | |
10437 | ||
acf2b673 | 10438 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 10439 | } |
10440 | else if (type->is_string_type()) | |
acf2b673 | 10441 | { |
10442 | if (cap != NULL) | |
10443 | { | |
631d5788 | 10444 | go_error_at(location, "invalid 3-index slice of string"); |
acf2b673 | 10445 | return Expression::make_error(location); |
10446 | } | |
10447 | return Expression::make_string_index(left, start, end, location); | |
10448 | } | |
e440a328 | 10449 | else if (type->map_type() != NULL) |
10450 | { | |
acf2b673 | 10451 | if (end != NULL || cap != NULL) |
e440a328 | 10452 | { |
631d5788 | 10453 | go_error_at(location, "invalid slice of map"); |
e440a328 | 10454 | return Expression::make_error(location); |
10455 | } | |
0d5530d9 | 10456 | return Expression::make_map_index(left, start, location); |
e440a328 | 10457 | } |
10458 | else | |
10459 | { | |
631d5788 | 10460 | go_error_at(location, |
10461 | "attempt to index object which is not array, string, or map"); | |
e440a328 | 10462 | return Expression::make_error(location); |
10463 | } | |
10464 | } | |
10465 | ||
acf2b673 | 10466 | // Write an indexed expression |
10467 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 10468 | |
10469 | void | |
10470 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10471 | const Expression* expr, | |
10472 | const Expression* start, | |
acf2b673 | 10473 | const Expression* end, |
10474 | const Expression* cap) | |
d751bb78 | 10475 | { |
10476 | expr->dump_expression(ast_dump_context); | |
10477 | ast_dump_context->ostream() << "["; | |
10478 | start->dump_expression(ast_dump_context); | |
10479 | if (end != NULL) | |
10480 | { | |
10481 | ast_dump_context->ostream() << ":"; | |
10482 | end->dump_expression(ast_dump_context); | |
10483 | } | |
acf2b673 | 10484 | if (cap != NULL) |
10485 | { | |
10486 | ast_dump_context->ostream() << ":"; | |
10487 | cap->dump_expression(ast_dump_context); | |
10488 | } | |
d751bb78 | 10489 | ast_dump_context->ostream() << "]"; |
10490 | } | |
10491 | ||
10492 | // Dump ast representation for an index expression. | |
10493 | ||
10494 | void | |
10495 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10496 | const | |
10497 | { | |
10498 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 10499 | this->start_, this->end_, this->cap_); |
d751bb78 | 10500 | } |
10501 | ||
e440a328 | 10502 | // Make an index expression. |
10503 | ||
10504 | Expression* | |
10505 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 10506 | Expression* cap, Location location) |
e440a328 | 10507 | { |
acf2b673 | 10508 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 10509 | } |
10510 | ||
da244e59 | 10511 | // Class Array_index_expression. |
e440a328 | 10512 | |
10513 | // Array index traversal. | |
10514 | ||
10515 | int | |
10516 | Array_index_expression::do_traverse(Traverse* traverse) | |
10517 | { | |
10518 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10519 | return TRAVERSE_EXIT; | |
10520 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10521 | return TRAVERSE_EXIT; | |
10522 | if (this->end_ != NULL) | |
10523 | { | |
10524 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10525 | return TRAVERSE_EXIT; | |
10526 | } | |
acf2b673 | 10527 | if (this->cap_ != NULL) |
10528 | { | |
10529 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10530 | return TRAVERSE_EXIT; | |
10531 | } | |
e440a328 | 10532 | return TRAVERSE_CONTINUE; |
10533 | } | |
10534 | ||
10535 | // Return the type of an array index. | |
10536 | ||
10537 | Type* | |
10538 | Array_index_expression::do_type() | |
10539 | { | |
10540 | if (this->type_ == NULL) | |
10541 | { | |
10542 | Array_type* type = this->array_->type()->array_type(); | |
10543 | if (type == NULL) | |
10544 | this->type_ = Type::make_error_type(); | |
10545 | else if (this->end_ == NULL) | |
10546 | this->type_ = type->element_type(); | |
411eb89e | 10547 | else if (type->is_slice_type()) |
e440a328 | 10548 | { |
10549 | // A slice of a slice has the same type as the original | |
10550 | // slice. | |
10551 | this->type_ = this->array_->type()->deref(); | |
10552 | } | |
10553 | else | |
10554 | { | |
10555 | // A slice of an array is a slice. | |
10556 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10557 | } | |
10558 | } | |
10559 | return this->type_; | |
10560 | } | |
10561 | ||
10562 | // Set the type of an array index. | |
10563 | ||
10564 | void | |
10565 | Array_index_expression::do_determine_type(const Type_context*) | |
10566 | { | |
10567 | this->array_->determine_type_no_context(); | |
f77aa642 | 10568 | |
10569 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10570 | if (this->start_->is_constant()) | |
10571 | this->start_->determine_type(&index_context); | |
10572 | else | |
10573 | this->start_->determine_type_no_context(); | |
e440a328 | 10574 | if (this->end_ != NULL) |
f77aa642 | 10575 | { |
10576 | if (this->end_->is_constant()) | |
10577 | this->end_->determine_type(&index_context); | |
10578 | else | |
10579 | this->end_->determine_type_no_context(); | |
10580 | } | |
acf2b673 | 10581 | if (this->cap_ != NULL) |
f77aa642 | 10582 | { |
10583 | if (this->cap_->is_constant()) | |
10584 | this->cap_->determine_type(&index_context); | |
10585 | else | |
10586 | this->cap_->determine_type_no_context(); | |
10587 | } | |
e440a328 | 10588 | } |
10589 | ||
10590 | // Check types of an array index. | |
10591 | ||
10592 | void | |
d0a50ed8 | 10593 | Array_index_expression::do_check_types(Gogo* gogo) |
e440a328 | 10594 | { |
f6bc81e6 | 10595 | Numeric_constant nc; |
10596 | unsigned long v; | |
10597 | if (this->start_->type()->integer_type() == NULL | |
10598 | && !this->start_->type()->is_error() | |
10599 | && (!this->start_->numeric_constant_value(&nc) | |
10600 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10601 | this->report_error(_("index must be integer")); |
10602 | if (this->end_ != NULL | |
10603 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10604 | && !this->end_->type()->is_error() |
10605 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10606 | && !this->end_->is_error_expression() |
10607 | && (!this->end_->numeric_constant_value(&nc) | |
10608 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10609 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10610 | if (this->cap_ != NULL |
10611 | && this->cap_->type()->integer_type() == NULL | |
10612 | && !this->cap_->type()->is_error() | |
10613 | && !this->cap_->is_nil_expression() | |
10614 | && !this->cap_->is_error_expression() | |
10615 | && (!this->cap_->numeric_constant_value(&nc) | |
10616 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10617 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10618 | |
10619 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10620 | if (array_type == NULL) |
10621 | { | |
c484d925 | 10622 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10623 | return; |
10624 | } | |
e440a328 | 10625 | |
10626 | unsigned int int_bits = | |
10627 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10628 | ||
0c77715b | 10629 | Numeric_constant lvalnc; |
e440a328 | 10630 | mpz_t lval; |
e440a328 | 10631 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10632 | && array_type->length()->numeric_constant_value(&lvalnc) |
10633 | && lvalnc.to_int(&lval)); | |
10634 | Numeric_constant inc; | |
e440a328 | 10635 | mpz_t ival; |
0bd5d859 | 10636 | bool ival_valid = false; |
0c77715b | 10637 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10638 | { |
0bd5d859 | 10639 | ival_valid = true; |
e440a328 | 10640 | if (mpz_sgn(ival) < 0 |
10641 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10642 | || (lval_valid | |
10643 | && (this->end_ == NULL | |
10644 | ? mpz_cmp(ival, lval) >= 0 | |
10645 | : mpz_cmp(ival, lval) > 0))) | |
10646 | { | |
631d5788 | 10647 | go_error_at(this->start_->location(), "array index out of bounds"); |
e440a328 | 10648 | this->set_is_error(); |
10649 | } | |
10650 | } | |
10651 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10652 | { | |
0c77715b | 10653 | Numeric_constant enc; |
10654 | mpz_t eval; | |
acf2b673 | 10655 | bool eval_valid = false; |
0c77715b | 10656 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10657 | { |
acf2b673 | 10658 | eval_valid = true; |
0c77715b | 10659 | if (mpz_sgn(eval) < 0 |
10660 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10661 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10662 | { |
631d5788 | 10663 | go_error_at(this->end_->location(), "array index out of bounds"); |
e440a328 | 10664 | this->set_is_error(); |
10665 | } | |
0bd5d859 | 10666 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10667 | this->report_error(_("inverted slice range")); | |
e440a328 | 10668 | } |
acf2b673 | 10669 | |
10670 | Numeric_constant cnc; | |
10671 | mpz_t cval; | |
10672 | if (this->cap_ != NULL | |
10673 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10674 | { | |
10675 | if (mpz_sgn(cval) < 0 | |
10676 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10677 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10678 | { | |
631d5788 | 10679 | go_error_at(this->cap_->location(), "array index out of bounds"); |
acf2b673 | 10680 | this->set_is_error(); |
10681 | } | |
10682 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10683 | { | |
631d5788 | 10684 | go_error_at(this->cap_->location(), |
10685 | "invalid slice index: capacity less than start"); | |
acf2b673 | 10686 | this->set_is_error(); |
10687 | } | |
10688 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10689 | { | |
631d5788 | 10690 | go_error_at(this->cap_->location(), |
10691 | "invalid slice index: capacity less than length"); | |
acf2b673 | 10692 | this->set_is_error(); |
10693 | } | |
10694 | mpz_clear(cval); | |
10695 | } | |
10696 | ||
10697 | if (eval_valid) | |
10698 | mpz_clear(eval); | |
e440a328 | 10699 | } |
0bd5d859 | 10700 | if (ival_valid) |
10701 | mpz_clear(ival); | |
0c77715b | 10702 | if (lval_valid) |
10703 | mpz_clear(lval); | |
e440a328 | 10704 | |
10705 | // A slice of an array requires an addressable array. A slice of a | |
10706 | // slice is always possible. | |
411eb89e | 10707 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10708 | { |
10709 | if (!this->array_->is_addressable()) | |
8da39c3b | 10710 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10711 | else |
d0a50ed8 | 10712 | { |
10713 | bool escapes = true; | |
10714 | ||
10715 | // When compiling the runtime, a slice operation does not | |
10716 | // cause local variables to escape. When escape analysis | |
10717 | // becomes the default, this should be changed to make it an | |
10718 | // error if we have a slice operation that escapes. | |
10719 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
10720 | escapes = false; | |
10721 | ||
10722 | this->array_->address_taken(escapes); | |
10723 | } | |
88ec30c8 | 10724 | } |
e440a328 | 10725 | } |
10726 | ||
2c809f8f | 10727 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10728 | |
10729 | Expression* | |
10730 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10731 | Statement_inserter* inserter) | |
10732 | { | |
10733 | Location loc = this->location(); | |
5bf8be8b | 10734 | Expression* array = this->array_; |
10735 | Expression* start = this->start_; | |
10736 | Expression* end = this->end_; | |
10737 | Expression* cap = this->cap_; | |
10738 | if (array->is_error_expression() | |
10739 | || array->type()->is_error_type() | |
10740 | || start->is_error_expression() | |
10741 | || start->type()->is_error_type() | |
10742 | || (end != NULL | |
10743 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10744 | || (cap != NULL | |
10745 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10746 | { | |
10747 | go_assert(saw_errors()); | |
10748 | return Expression::make_error(loc); | |
10749 | } | |
10750 | ||
2c809f8f | 10751 | Temporary_statement* temp; |
5bf8be8b | 10752 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10753 | { |
5bf8be8b | 10754 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10755 | inserter->insert(temp); |
10756 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10757 | } | |
5bf8be8b | 10758 | if (!start->is_variable()) |
2c809f8f | 10759 | { |
5bf8be8b | 10760 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10761 | inserter->insert(temp); |
10762 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10763 | } | |
5bf8be8b | 10764 | if (end != NULL |
10765 | && !end->is_nil_expression() | |
10766 | && !end->is_variable()) | |
2c809f8f | 10767 | { |
5bf8be8b | 10768 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10769 | inserter->insert(temp); |
10770 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10771 | } | |
5bf8be8b | 10772 | if (cap!= NULL && !cap->is_variable()) |
2c809f8f | 10773 | { |
5bf8be8b | 10774 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10775 | inserter->insert(temp); |
10776 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10777 | } | |
10778 | ||
35a54f17 | 10779 | return this; |
10780 | } | |
10781 | ||
e440a328 | 10782 | // Return whether this expression is addressable. |
10783 | ||
10784 | bool | |
10785 | Array_index_expression::do_is_addressable() const | |
10786 | { | |
10787 | // A slice expression is not addressable. | |
10788 | if (this->end_ != NULL) | |
10789 | return false; | |
10790 | ||
10791 | // An index into a slice is addressable. | |
411eb89e | 10792 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10793 | return true; |
10794 | ||
10795 | // An index into an array is addressable if the array is | |
10796 | // addressable. | |
10797 | return this->array_->is_addressable(); | |
10798 | } | |
10799 | ||
ea664253 | 10800 | // Get the backend representation for an array index. |
e440a328 | 10801 | |
ea664253 | 10802 | Bexpression* |
10803 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10804 | { |
e440a328 | 10805 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10806 | if (array_type == NULL) |
10807 | { | |
c484d925 | 10808 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10809 | return context->backend()->error_expression(); |
d8cd8e2d | 10810 | } |
35a54f17 | 10811 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10812 | |
2c809f8f | 10813 | Location loc = this->location(); |
10814 | Gogo* gogo = context->gogo(); | |
10815 | ||
6dfedc16 | 10816 | Type* int_type = Type::lookup_integer_type("int"); |
10817 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10818 | |
2c809f8f | 10819 | // We need to convert the length and capacity to the Go "int" type here |
10820 | // because the length of a fixed-length array could be of type "uintptr" | |
10821 | // and gimple disallows binary operations between "uintptr" and other | |
10822 | // integer types. FIXME. | |
10823 | Bexpression* length = NULL; | |
a04bfdfc | 10824 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10825 | { | |
35a54f17 | 10826 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10827 | length = len->get_backend(context); |
2c809f8f | 10828 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10829 | } |
10830 | ||
2c809f8f | 10831 | Bexpression* capacity = NULL; |
a04bfdfc | 10832 | if (this->end_ != NULL) |
10833 | { | |
35a54f17 | 10834 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10835 | capacity = cap->get_backend(context); |
2c809f8f | 10836 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10837 | } |
10838 | ||
2c809f8f | 10839 | Bexpression* cap_arg = capacity; |
acf2b673 | 10840 | if (this->cap_ != NULL) |
10841 | { | |
ea664253 | 10842 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10843 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10844 | } |
10845 | ||
2c809f8f | 10846 | if (length == NULL) |
10847 | length = cap_arg; | |
e440a328 | 10848 | |
10849 | int code = (array_type->length() != NULL | |
10850 | ? (this->end_ == NULL | |
10851 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10852 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10853 | : (this->end_ == NULL | |
10854 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10855 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10856 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10857 | |
6dfedc16 | 10858 | if (this->start_->type()->integer_type() == NULL |
10859 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10860 | { | |
10861 | go_assert(saw_errors()); | |
10862 | return context->backend()->error_expression(); | |
10863 | } | |
d9f3743a | 10864 | |
ea664253 | 10865 | Bexpression* bad_index = |
d9f3743a | 10866 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 10867 | |
ea664253 | 10868 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10869 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10870 | Bexpression* start_too_large = | |
10871 | gogo->backend()->binary_expression((this->end_ == NULL | |
10872 | ? OPERATOR_GE | |
10873 | : OPERATOR_GT), | |
10874 | start, | |
10875 | (this->end_ == NULL | |
10876 | ? length | |
10877 | : capacity), | |
10878 | loc); | |
10879 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10880 | bad_index, loc); | |
e440a328 | 10881 | |
10882 | if (this->end_ == NULL) | |
10883 | { | |
10884 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10885 | // wrap the index check into START. |
10886 | start = | |
10887 | gogo->backend()->conditional_expression(int_btype, bad_index, | |
10888 | crash, start, loc); | |
e440a328 | 10889 | |
2c809f8f | 10890 | Bexpression* ret; |
e440a328 | 10891 | if (array_type->length() != NULL) |
10892 | { | |
ea664253 | 10893 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10894 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10895 | } |
10896 | else | |
10897 | { | |
2c809f8f | 10898 | // Slice. |
10899 | Expression* valptr = | |
35a54f17 | 10900 | array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10901 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 10902 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 10903 | |
10904 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
10905 | Btype* ele_btype = ele_type->get_backend(gogo); | |
10906 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 10907 | } |
ea664253 | 10908 | return ret; |
e440a328 | 10909 | } |
10910 | ||
10911 | // Array slice. | |
10912 | ||
acf2b673 | 10913 | if (this->cap_ != NULL) |
10914 | { | |
2c809f8f | 10915 | Bexpression* bounds_bcheck = |
ea664253 | 10916 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 10917 | bad_index = |
10918 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10919 | bad_index, loc); | |
10920 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
10921 | ||
10922 | Bexpression* cap_too_small = | |
10923 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
10924 | Bexpression* cap_too_large = | |
10925 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
10926 | Bexpression* bad_cap = | |
10927 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
10928 | cap_too_large, loc); | |
10929 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
10930 | bad_index, loc); | |
10931 | } | |
10932 | ||
10933 | Bexpression* end; | |
e440a328 | 10934 | if (this->end_->is_nil_expression()) |
2c809f8f | 10935 | end = length; |
e440a328 | 10936 | else |
10937 | { | |
2c809f8f | 10938 | Bexpression* bounds_bcheck = |
ea664253 | 10939 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 10940 | |
2c809f8f | 10941 | bad_index = |
10942 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10943 | bad_index, loc); | |
e440a328 | 10944 | |
ea664253 | 10945 | end = this->end_->get_backend(context); |
2c809f8f | 10946 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
10947 | Bexpression* end_too_small = | |
10948 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
10949 | Bexpression* end_too_large = | |
10950 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
10951 | Bexpression* bad_end = | |
10952 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
10953 | end_too_large, loc); | |
10954 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
10955 | bad_index, loc); | |
e440a328 | 10956 | } |
10957 | ||
35a54f17 | 10958 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_); |
ea664253 | 10959 | Bexpression* val = valptr->get_backend(context); |
2c809f8f | 10960 | val = gogo->backend()->pointer_offset_expression(val, start, loc); |
e440a328 | 10961 | |
2c809f8f | 10962 | Bexpression* result_length = |
10963 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 10964 | |
2c809f8f | 10965 | Bexpression* result_capacity = |
10966 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 10967 | |
2c809f8f | 10968 | Btype* struct_btype = this->type()->get_backend(gogo); |
10969 | std::vector<Bexpression*> init; | |
10970 | init.push_back(val); | |
10971 | init.push_back(result_length); | |
10972 | init.push_back(result_capacity); | |
e440a328 | 10973 | |
2c809f8f | 10974 | Bexpression* ctor = |
10975 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
ea664253 | 10976 | return gogo->backend()->conditional_expression(struct_btype, bad_index, |
10977 | crash, ctor, loc); | |
e440a328 | 10978 | } |
10979 | ||
d751bb78 | 10980 | // Dump ast representation for an array index expression. |
10981 | ||
10982 | void | |
10983 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10984 | const | |
10985 | { | |
10986 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 10987 | this->start_, this->end_, this->cap_); |
d751bb78 | 10988 | } |
10989 | ||
acf2b673 | 10990 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 10991 | |
10992 | Expression* | |
10993 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 10994 | Expression* end, Expression* cap, |
10995 | Location location) | |
e440a328 | 10996 | { |
acf2b673 | 10997 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 10998 | } |
10999 | ||
50075d74 | 11000 | // Class String_index_expression. |
e440a328 | 11001 | |
11002 | // String index traversal. | |
11003 | ||
11004 | int | |
11005 | String_index_expression::do_traverse(Traverse* traverse) | |
11006 | { | |
11007 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
11008 | return TRAVERSE_EXIT; | |
11009 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
11010 | return TRAVERSE_EXIT; | |
11011 | if (this->end_ != NULL) | |
11012 | { | |
11013 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
11014 | return TRAVERSE_EXIT; | |
11015 | } | |
11016 | return TRAVERSE_CONTINUE; | |
11017 | } | |
11018 | ||
2c809f8f | 11019 | Expression* |
11020 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
11021 | Statement_inserter* inserter) | |
e440a328 | 11022 | { |
2c809f8f | 11023 | Location loc = this->location(); |
5bf8be8b | 11024 | Expression* string = this->string_; |
11025 | Expression* start = this->start_; | |
11026 | Expression* end = this->end_; | |
11027 | if (string->is_error_expression() | |
11028 | || string->type()->is_error_type() | |
11029 | || start->is_error_expression() | |
11030 | || start->type()->is_error_type() | |
11031 | || (end != NULL | |
11032 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
11033 | { | |
11034 | go_assert(saw_errors()); | |
11035 | return Expression::make_error(loc); | |
11036 | } | |
11037 | ||
11038 | Temporary_statement* temp; | |
2c809f8f | 11039 | if (!this->string_->is_variable()) |
11040 | { | |
11041 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
11042 | inserter->insert(temp); | |
11043 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
11044 | } | |
11045 | if (!this->start_->is_variable()) | |
11046 | { | |
11047 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
11048 | inserter->insert(temp); | |
11049 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
11050 | } | |
11051 | if (this->end_ != NULL | |
11052 | && !this->end_->is_nil_expression() | |
11053 | && !this->end_->is_variable()) | |
11054 | { | |
11055 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
11056 | inserter->insert(temp); | |
11057 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
11058 | } | |
11059 | ||
11060 | return this; | |
11061 | } | |
11062 | ||
11063 | // Return the type of a string index. | |
11064 | ||
11065 | Type* | |
11066 | String_index_expression::do_type() | |
11067 | { | |
11068 | if (this->end_ == NULL) | |
11069 | return Type::lookup_integer_type("uint8"); | |
11070 | else | |
11071 | return this->string_->type(); | |
11072 | } | |
11073 | ||
11074 | // Determine the type of a string index. | |
11075 | ||
11076 | void | |
11077 | String_index_expression::do_determine_type(const Type_context*) | |
11078 | { | |
11079 | this->string_->determine_type_no_context(); | |
f77aa642 | 11080 | |
11081 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
11082 | if (this->start_->is_constant()) | |
11083 | this->start_->determine_type(&index_context); | |
11084 | else | |
11085 | this->start_->determine_type_no_context(); | |
e440a328 | 11086 | if (this->end_ != NULL) |
f77aa642 | 11087 | { |
11088 | if (this->end_->is_constant()) | |
11089 | this->end_->determine_type(&index_context); | |
11090 | else | |
11091 | this->end_->determine_type_no_context(); | |
11092 | } | |
e440a328 | 11093 | } |
11094 | ||
11095 | // Check types of a string index. | |
11096 | ||
11097 | void | |
11098 | String_index_expression::do_check_types(Gogo*) | |
11099 | { | |
acdc230d | 11100 | Numeric_constant nc; |
11101 | unsigned long v; | |
11102 | if (this->start_->type()->integer_type() == NULL | |
11103 | && !this->start_->type()->is_error() | |
11104 | && (!this->start_->numeric_constant_value(&nc) | |
11105 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11106 | this->report_error(_("index must be integer")); |
11107 | if (this->end_ != NULL | |
11108 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 11109 | && !this->end_->type()->is_error() |
11110 | && !this->end_->is_nil_expression() | |
11111 | && !this->end_->is_error_expression() | |
11112 | && (!this->end_->numeric_constant_value(&nc) | |
11113 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11114 | this->report_error(_("slice end must be integer")); |
11115 | ||
11116 | std::string sval; | |
11117 | bool sval_valid = this->string_->string_constant_value(&sval); | |
11118 | ||
0c77715b | 11119 | Numeric_constant inc; |
e440a328 | 11120 | mpz_t ival; |
0bd5d859 | 11121 | bool ival_valid = false; |
0c77715b | 11122 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 11123 | { |
0bd5d859 | 11124 | ival_valid = true; |
e440a328 | 11125 | if (mpz_sgn(ival) < 0 |
b10f32fb | 11126 | || (sval_valid |
11127 | && (this->end_ == NULL | |
11128 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
11129 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 11130 | { |
631d5788 | 11131 | go_error_at(this->start_->location(), "string index out of bounds"); |
e440a328 | 11132 | this->set_is_error(); |
11133 | } | |
11134 | } | |
11135 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
11136 | { | |
0c77715b | 11137 | Numeric_constant enc; |
11138 | mpz_t eval; | |
11139 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 11140 | { |
0c77715b | 11141 | if (mpz_sgn(eval) < 0 |
11142 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 11143 | { |
631d5788 | 11144 | go_error_at(this->end_->location(), "string index out of bounds"); |
e440a328 | 11145 | this->set_is_error(); |
11146 | } | |
0bd5d859 | 11147 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
11148 | this->report_error(_("inverted slice range")); | |
0c77715b | 11149 | mpz_clear(eval); |
e440a328 | 11150 | } |
11151 | } | |
0bd5d859 | 11152 | if (ival_valid) |
11153 | mpz_clear(ival); | |
e440a328 | 11154 | } |
11155 | ||
ea664253 | 11156 | // Get the backend representation for a string index. |
e440a328 | 11157 | |
ea664253 | 11158 | Bexpression* |
11159 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11160 | { |
b13c66cd | 11161 | Location loc = this->location(); |
2c809f8f | 11162 | Expression* string_arg = this->string_; |
11163 | if (this->string_->type()->points_to() != NULL) | |
11164 | string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc); | |
e440a328 | 11165 | |
2c809f8f | 11166 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 11167 | |
2c809f8f | 11168 | int code = (this->end_ == NULL |
11169 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
11170 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 11171 | |
2c809f8f | 11172 | Gogo* gogo = context->gogo(); |
ea664253 | 11173 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 11174 | |
11175 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 11176 | |
2c809f8f | 11177 | // It is possible that an error occurred earlier because the start index |
11178 | // cannot be represented as an integer type. In this case, we shouldn't | |
11179 | // try casting the starting index into an integer since | |
11180 | // Type_conversion_expression will fail to get the backend representation. | |
11181 | // FIXME. | |
11182 | if (this->start_->type()->integer_type() == NULL | |
11183 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
11184 | { | |
11185 | go_assert(saw_errors()); | |
ea664253 | 11186 | return context->backend()->error_expression(); |
2c809f8f | 11187 | } |
e440a328 | 11188 | |
2c809f8f | 11189 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
e440a328 | 11190 | |
2c809f8f | 11191 | if (this->end_ == NULL) |
11192 | { | |
11193 | Expression* length = | |
11194 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 11195 | |
2c809f8f | 11196 | Expression* start_too_large = |
11197 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
11198 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
11199 | bad_index, loc); | |
11200 | Expression* bytes = | |
11201 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 11202 | |
ea664253 | 11203 | Bexpression* bstart = start->get_backend(context); |
11204 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 11205 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 11206 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
11207 | Bexpression* index = | |
11208 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 11209 | |
2c809f8f | 11210 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 11211 | Bexpression* index_error = bad_index->get_backend(context); |
11212 | return gogo->backend()->conditional_expression(byte_btype, index_error, | |
11213 | crash, index, loc); | |
2c809f8f | 11214 | } |
11215 | ||
11216 | Expression* end = NULL; | |
11217 | if (this->end_->is_nil_expression()) | |
e67508fa | 11218 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 11219 | else |
11220 | { | |
2c809f8f | 11221 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
11222 | bad_index = | |
11223 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
11224 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 11225 | } |
2c809f8f | 11226 | |
11227 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
11228 | string_arg, start, end); | |
ea664253 | 11229 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 11230 | |
11231 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 11232 | Bexpression* index_error = bad_index->get_backend(context); |
11233 | return gogo->backend()->conditional_expression(str_btype, index_error, | |
11234 | crash, bstrslice, loc); | |
e440a328 | 11235 | } |
11236 | ||
d751bb78 | 11237 | // Dump ast representation for a string index expression. |
11238 | ||
11239 | void | |
11240 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11241 | const | |
11242 | { | |
acf2b673 | 11243 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
11244 | this->start_, this->end_, NULL); | |
d751bb78 | 11245 | } |
11246 | ||
e440a328 | 11247 | // Make a string index expression. END may be NULL. |
11248 | ||
11249 | Expression* | |
11250 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 11251 | Expression* end, Location location) |
e440a328 | 11252 | { |
11253 | return new String_index_expression(string, start, end, location); | |
11254 | } | |
11255 | ||
11256 | // Class Map_index. | |
11257 | ||
11258 | // Get the type of the map. | |
11259 | ||
11260 | Map_type* | |
11261 | Map_index_expression::get_map_type() const | |
11262 | { | |
0d5530d9 | 11263 | Map_type* mt = this->map_->type()->map_type(); |
c7524fae | 11264 | if (mt == NULL) |
c484d925 | 11265 | go_assert(saw_errors()); |
e440a328 | 11266 | return mt; |
11267 | } | |
11268 | ||
11269 | // Map index traversal. | |
11270 | ||
11271 | int | |
11272 | Map_index_expression::do_traverse(Traverse* traverse) | |
11273 | { | |
11274 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11275 | return TRAVERSE_EXIT; | |
11276 | return Expression::traverse(&this->index_, traverse); | |
11277 | } | |
11278 | ||
2c809f8f | 11279 | // We need to pass in a pointer to the key, so flatten the index into a |
11280 | // temporary variable if it isn't already. The value pointer will be | |
11281 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
11282 | // recomputation. | |
11283 | ||
11284 | Expression* | |
91c0fd76 | 11285 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 11286 | Statement_inserter* inserter) |
11287 | { | |
91c0fd76 | 11288 | Location loc = this->location(); |
2c809f8f | 11289 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 11290 | if (this->index()->is_error_expression() |
11291 | || this->index()->type()->is_error_type() | |
11292 | || mt->is_error_type()) | |
11293 | { | |
11294 | go_assert(saw_errors()); | |
11295 | return Expression::make_error(loc); | |
11296 | } | |
11297 | ||
91c0fd76 | 11298 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
11299 | { | |
11300 | if (this->index_->type()->interface_type() != NULL | |
11301 | && !this->index_->is_variable()) | |
11302 | { | |
11303 | Temporary_statement* temp = | |
11304 | Statement::make_temporary(NULL, this->index_, loc); | |
11305 | inserter->insert(temp); | |
11306 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
11307 | } | |
11308 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
11309 | this->index_, loc); | |
11310 | } | |
2c809f8f | 11311 | |
11312 | if (!this->index_->is_variable()) | |
11313 | { | |
11314 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 11315 | loc); |
2c809f8f | 11316 | inserter->insert(temp); |
91c0fd76 | 11317 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11318 | } |
11319 | ||
11320 | if (this->value_pointer_ == NULL) | |
0d5530d9 | 11321 | this->get_value_pointer(gogo); |
5bf8be8b | 11322 | if (this->value_pointer_->is_error_expression() |
11323 | || this->value_pointer_->type()->is_error_type()) | |
11324 | return Expression::make_error(loc); | |
2c809f8f | 11325 | if (!this->value_pointer_->is_variable()) |
11326 | { | |
11327 | Temporary_statement* temp = | |
91c0fd76 | 11328 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 11329 | inserter->insert(temp); |
91c0fd76 | 11330 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11331 | } |
11332 | ||
11333 | return this; | |
11334 | } | |
11335 | ||
e440a328 | 11336 | // Return the type of a map index. |
11337 | ||
11338 | Type* | |
11339 | Map_index_expression::do_type() | |
11340 | { | |
c7524fae | 11341 | Map_type* mt = this->get_map_type(); |
11342 | if (mt == NULL) | |
11343 | return Type::make_error_type(); | |
0d5530d9 | 11344 | return mt->val_type(); |
e440a328 | 11345 | } |
11346 | ||
11347 | // Fix the type of a map index. | |
11348 | ||
11349 | void | |
11350 | Map_index_expression::do_determine_type(const Type_context*) | |
11351 | { | |
11352 | this->map_->determine_type_no_context(); | |
c7524fae | 11353 | Map_type* mt = this->get_map_type(); |
11354 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11355 | Type_context subcontext(key_type, false); | |
e440a328 | 11356 | this->index_->determine_type(&subcontext); |
11357 | } | |
11358 | ||
11359 | // Check types of a map index. | |
11360 | ||
11361 | void | |
11362 | Map_index_expression::do_check_types(Gogo*) | |
11363 | { | |
11364 | std::string reason; | |
c7524fae | 11365 | Map_type* mt = this->get_map_type(); |
11366 | if (mt == NULL) | |
11367 | return; | |
11368 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11369 | { |
11370 | if (reason.empty()) | |
11371 | this->report_error(_("incompatible type for map index")); | |
11372 | else | |
11373 | { | |
631d5788 | 11374 | go_error_at(this->location(), "incompatible type for map index (%s)", |
11375 | reason.c_str()); | |
e440a328 | 11376 | this->set_is_error(); |
11377 | } | |
11378 | } | |
11379 | } | |
11380 | ||
ea664253 | 11381 | // Get the backend representation for a map index. |
e440a328 | 11382 | |
ea664253 | 11383 | Bexpression* |
11384 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11385 | { |
11386 | Map_type* type = this->get_map_type(); | |
c7524fae | 11387 | if (type == NULL) |
2c809f8f | 11388 | { |
11389 | go_assert(saw_errors()); | |
ea664253 | 11390 | return context->backend()->error_expression(); |
2c809f8f | 11391 | } |
e440a328 | 11392 | |
2c809f8f | 11393 | go_assert(this->value_pointer_ != NULL |
11394 | && this->value_pointer_->is_variable()); | |
e440a328 | 11395 | |
0d5530d9 | 11396 | Expression* val = Expression::make_unary(OPERATOR_MULT, this->value_pointer_, |
11397 | this->location()); | |
11398 | return val->get_backend(context); | |
e440a328 | 11399 | } |
11400 | ||
0d5530d9 | 11401 | // Get an expression for the map index. This returns an expression |
11402 | // that evaluates to a pointer to a value. If the key is not in the | |
11403 | // map, the pointer will point to a zero value. | |
e440a328 | 11404 | |
2c809f8f | 11405 | Expression* |
0d5530d9 | 11406 | Map_index_expression::get_value_pointer(Gogo* gogo) |
e440a328 | 11407 | { |
2c809f8f | 11408 | if (this->value_pointer_ == NULL) |
746d2e73 | 11409 | { |
2c809f8f | 11410 | Map_type* type = this->get_map_type(); |
11411 | if (type == NULL) | |
746d2e73 | 11412 | { |
2c809f8f | 11413 | go_assert(saw_errors()); |
11414 | return Expression::make_error(this->location()); | |
746d2e73 | 11415 | } |
e440a328 | 11416 | |
2c809f8f | 11417 | Location loc = this->location(); |
11418 | Expression* map_ref = this->map_; | |
e440a328 | 11419 | |
0d5530d9 | 11420 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, |
11421 | this->index_, | |
2c809f8f | 11422 | loc); |
0d5530d9 | 11423 | |
11424 | Expression* zero = type->fat_zero_value(gogo); | |
11425 | ||
11426 | Expression* map_index; | |
11427 | ||
11428 | if (zero == NULL) | |
11429 | map_index = | |
11430 | Runtime::make_call(Runtime::MAPACCESS1, loc, 3, | |
11431 | Expression::make_type_descriptor(type, loc), | |
11432 | map_ref, index_ptr); | |
11433 | else | |
11434 | map_index = | |
11435 | Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, | |
11436 | Expression::make_type_descriptor(type, loc), | |
11437 | map_ref, index_ptr, zero); | |
2c809f8f | 11438 | |
11439 | Type* val_type = type->val_type(); | |
11440 | this->value_pointer_ = | |
11441 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
11442 | map_index, this->location()); | |
11443 | } | |
0d5530d9 | 11444 | |
2c809f8f | 11445 | return this->value_pointer_; |
e440a328 | 11446 | } |
11447 | ||
d751bb78 | 11448 | // Dump ast representation for a map index expression |
11449 | ||
11450 | void | |
11451 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11452 | const | |
11453 | { | |
acf2b673 | 11454 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
11455 | this->index_, NULL, NULL); | |
d751bb78 | 11456 | } |
11457 | ||
e440a328 | 11458 | // Make a map index expression. |
11459 | ||
11460 | Map_index_expression* | |
11461 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11462 | Location location) |
e440a328 | 11463 | { |
11464 | return new Map_index_expression(map, index, location); | |
11465 | } | |
11466 | ||
11467 | // Class Field_reference_expression. | |
11468 | ||
149eabc5 | 11469 | // Lower a field reference expression. There is nothing to lower, but |
11470 | // this is where we generate the tracking information for fields with | |
11471 | // the magic go:"track" tag. | |
11472 | ||
11473 | Expression* | |
11474 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11475 | Statement_inserter* inserter, int) | |
11476 | { | |
11477 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11478 | if (struct_type == NULL) | |
11479 | { | |
11480 | // Error will be reported elsewhere. | |
11481 | return this; | |
11482 | } | |
11483 | const Struct_field* field = struct_type->field(this->field_index_); | |
11484 | if (field == NULL) | |
11485 | return this; | |
11486 | if (!field->has_tag()) | |
11487 | return this; | |
11488 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11489 | return this; | |
11490 | ||
604e278d | 11491 | // References from functions generated by the compiler don't count. |
c6292d1d | 11492 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 11493 | return this; |
11494 | ||
149eabc5 | 11495 | // We have found a reference to a tracked field. Build a call to |
11496 | // the runtime function __go_fieldtrack with a string that describes | |
11497 | // the field. FIXME: We should only call this once per referenced | |
11498 | // field per function, not once for each reference to the field. | |
11499 | ||
11500 | if (this->called_fieldtrack_) | |
11501 | return this; | |
11502 | this->called_fieldtrack_ = true; | |
11503 | ||
11504 | Location loc = this->location(); | |
11505 | ||
11506 | std::string s = "fieldtrack \""; | |
11507 | Named_type* nt = this->expr_->type()->named_type(); | |
11508 | if (nt == NULL || nt->named_object()->package() == NULL) | |
11509 | s.append(gogo->pkgpath()); | |
11510 | else | |
11511 | s.append(nt->named_object()->package()->pkgpath()); | |
11512 | s.push_back('.'); | |
11513 | if (nt != NULL) | |
5c29ad36 | 11514 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11515 | s.push_back('.'); |
11516 | s.append(field->field_name()); | |
11517 | s.push_back('"'); | |
11518 | ||
11519 | // We can't use a string here, because internally a string holds a | |
11520 | // pointer to the actual bytes; when the linker garbage collects the | |
11521 | // string, it won't garbage collect the bytes. So we use a | |
11522 | // [...]byte. | |
11523 | ||
e67508fa | 11524 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 11525 | |
11526 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
11527 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
11528 | ||
11529 | Expression_list* bytes = new Expression_list(); | |
11530 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11531 | { | |
e67508fa | 11532 | unsigned char c = static_cast<unsigned char>(*p); |
11533 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 11534 | } |
11535 | ||
11536 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11537 | bytes, false, loc); |
149eabc5 | 11538 | |
11539 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11540 | ||
11541 | static int count; | |
11542 | char buf[50]; | |
11543 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11544 | ++count; | |
11545 | ||
11546 | Named_object* no = gogo->add_variable(buf, var); | |
11547 | e = Expression::make_var_reference(no, loc); | |
11548 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11549 | ||
11550 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 11551 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 11552 | inserter->insert(Statement::make_statement(call, false)); |
11553 | ||
11554 | // Put this function, and the global variable we just created, into | |
11555 | // unique sections. This will permit the linker to garbage collect | |
11556 | // them if they are not referenced. The effect is that the only | |
11557 | // strings, indicating field references, that will wind up in the | |
11558 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11559 | if (function != NULL) |
11560 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11561 | var->set_in_unique_section(); |
11562 | ||
11563 | return this; | |
11564 | } | |
11565 | ||
e440a328 | 11566 | // Return the type of a field reference. |
11567 | ||
11568 | Type* | |
11569 | Field_reference_expression::do_type() | |
11570 | { | |
b0e628fb | 11571 | Type* type = this->expr_->type(); |
5c13bd80 | 11572 | if (type->is_error()) |
b0e628fb | 11573 | return type; |
11574 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11575 | go_assert(struct_type != NULL); |
e440a328 | 11576 | return struct_type->field(this->field_index_)->type(); |
11577 | } | |
11578 | ||
11579 | // Check the types for a field reference. | |
11580 | ||
11581 | void | |
11582 | Field_reference_expression::do_check_types(Gogo*) | |
11583 | { | |
b0e628fb | 11584 | Type* type = this->expr_->type(); |
5c13bd80 | 11585 | if (type->is_error()) |
b0e628fb | 11586 | return; |
11587 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11588 | go_assert(struct_type != NULL); |
11589 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11590 | } |
11591 | ||
ea664253 | 11592 | // Get the backend representation for a field reference. |
e440a328 | 11593 | |
ea664253 | 11594 | Bexpression* |
11595 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11596 | { |
ea664253 | 11597 | Bexpression* bstruct = this->expr_->get_backend(context); |
11598 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11599 | this->field_index_, | |
11600 | this->location()); | |
e440a328 | 11601 | } |
11602 | ||
d751bb78 | 11603 | // Dump ast representation for a field reference expression. |
11604 | ||
11605 | void | |
11606 | Field_reference_expression::do_dump_expression( | |
11607 | Ast_dump_context* ast_dump_context) const | |
11608 | { | |
11609 | this->expr_->dump_expression(ast_dump_context); | |
11610 | ast_dump_context->ostream() << "." << this->field_index_; | |
11611 | } | |
11612 | ||
e440a328 | 11613 | // Make a reference to a qualified identifier in an expression. |
11614 | ||
11615 | Field_reference_expression* | |
11616 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11617 | Location location) |
e440a328 | 11618 | { |
11619 | return new Field_reference_expression(expr, field_index, location); | |
11620 | } | |
11621 | ||
11622 | // Class Interface_field_reference_expression. | |
11623 | ||
2387f644 | 11624 | // Return an expression for the pointer to the function to call. |
e440a328 | 11625 | |
2387f644 | 11626 | Expression* |
11627 | Interface_field_reference_expression::get_function() | |
e440a328 | 11628 | { |
2387f644 | 11629 | Expression* ref = this->expr_; |
11630 | Location loc = this->location(); | |
11631 | if (ref->type()->points_to() != NULL) | |
11632 | ref = Expression::make_unary(OPERATOR_MULT, ref, loc); | |
e440a328 | 11633 | |
2387f644 | 11634 | Expression* mtable = |
11635 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11636 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11637 | |
11638 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11639 | unsigned int index; |
11640 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11641 | go_assert(field != NULL); | |
11642 | mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc); | |
11643 | return Expression::make_field_reference(mtable, index, loc); | |
e440a328 | 11644 | } |
11645 | ||
2387f644 | 11646 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11647 | // function. |
11648 | ||
2387f644 | 11649 | Expression* |
11650 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11651 | { |
2387f644 | 11652 | Expression* expr = this->expr_; |
11653 | if (expr->type()->points_to() != NULL) | |
11654 | expr = Expression::make_unary(OPERATOR_MULT, expr, this->location()); | |
11655 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, | |
11656 | this->location()); | |
e440a328 | 11657 | } |
11658 | ||
11659 | // Traversal. | |
11660 | ||
11661 | int | |
11662 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11663 | { | |
11664 | return Expression::traverse(&this->expr_, traverse); | |
11665 | } | |
11666 | ||
0afbb937 | 11667 | // Lower the expression. If this expression is not called, we need to |
11668 | // evaluate the expression twice when converting to the backend | |
11669 | // interface. So introduce a temporary variable if necessary. | |
11670 | ||
11671 | Expression* | |
9782d556 | 11672 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11673 | Statement_inserter* inserter) | |
0afbb937 | 11674 | { |
5bf8be8b | 11675 | if (this->expr_->is_error_expression() |
11676 | || this->expr_->type()->is_error_type()) | |
11677 | { | |
11678 | go_assert(saw_errors()); | |
11679 | return Expression::make_error(this->location()); | |
11680 | } | |
11681 | ||
2387f644 | 11682 | if (!this->expr_->is_variable()) |
0afbb937 | 11683 | { |
11684 | Temporary_statement* temp = | |
11685 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11686 | inserter->insert(temp); | |
11687 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11688 | this->location()); | |
11689 | } | |
11690 | return this; | |
11691 | } | |
11692 | ||
e440a328 | 11693 | // Return the type of an interface field reference. |
11694 | ||
11695 | Type* | |
11696 | Interface_field_reference_expression::do_type() | |
11697 | { | |
11698 | Type* expr_type = this->expr_->type(); | |
11699 | ||
11700 | Type* points_to = expr_type->points_to(); | |
11701 | if (points_to != NULL) | |
11702 | expr_type = points_to; | |
11703 | ||
11704 | Interface_type* interface_type = expr_type->interface_type(); | |
11705 | if (interface_type == NULL) | |
11706 | return Type::make_error_type(); | |
11707 | ||
11708 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11709 | if (method == NULL) | |
11710 | return Type::make_error_type(); | |
11711 | ||
11712 | return method->type(); | |
11713 | } | |
11714 | ||
11715 | // Determine types. | |
11716 | ||
11717 | void | |
11718 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11719 | { | |
11720 | this->expr_->determine_type_no_context(); | |
11721 | } | |
11722 | ||
11723 | // Check the types for an interface field reference. | |
11724 | ||
11725 | void | |
11726 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11727 | { | |
11728 | Type* type = this->expr_->type(); | |
11729 | ||
11730 | Type* points_to = type->points_to(); | |
11731 | if (points_to != NULL) | |
11732 | type = points_to; | |
11733 | ||
11734 | Interface_type* interface_type = type->interface_type(); | |
11735 | if (interface_type == NULL) | |
5c491127 | 11736 | { |
11737 | if (!type->is_error_type()) | |
11738 | this->report_error(_("expected interface or pointer to interface")); | |
11739 | } | |
e440a328 | 11740 | else |
11741 | { | |
11742 | const Typed_identifier* method = | |
11743 | interface_type->find_method(this->name_); | |
11744 | if (method == NULL) | |
11745 | { | |
631d5788 | 11746 | go_error_at(this->location(), "method %qs not in interface", |
11747 | Gogo::message_name(this->name_).c_str()); | |
e440a328 | 11748 | this->set_is_error(); |
11749 | } | |
11750 | } | |
11751 | } | |
11752 | ||
0afbb937 | 11753 | // If an interface field reference is not simply called, then it is |
11754 | // represented as a closure. The closure will hold a single variable, | |
11755 | // the value of the interface on which the method should be called. | |
11756 | // The function will be a simple thunk that pulls the value from the | |
11757 | // closure and calls the method with the remaining arguments. | |
11758 | ||
11759 | // Because method values are not common, we don't build all thunks for | |
11760 | // all possible interface methods, but instead only build them as we | |
11761 | // need them. In particular, we even build them on demand for | |
11762 | // interface methods defined in other packages. | |
11763 | ||
11764 | Interface_field_reference_expression::Interface_method_thunks | |
11765 | Interface_field_reference_expression::interface_method_thunks; | |
11766 | ||
11767 | // Find or create the thunk to call method NAME on TYPE. | |
11768 | ||
11769 | Named_object* | |
11770 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11771 | Interface_type* type, | |
11772 | const std::string& name) | |
11773 | { | |
11774 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11775 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11776 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11777 | if (ins.second) | |
11778 | { | |
11779 | // This is the first time we have seen this interface. | |
11780 | ins.first->second = new Method_thunks(); | |
11781 | } | |
11782 | ||
11783 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11784 | p != ins.first->second->end(); | |
11785 | p++) | |
11786 | if (p->first == name) | |
11787 | return p->second; | |
11788 | ||
11789 | Location loc = type->location(); | |
11790 | ||
11791 | const Typed_identifier* method_id = type->find_method(name); | |
11792 | if (method_id == NULL) | |
11793 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11794 | ||
11795 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11796 | if (orig_fntype == NULL) | |
11797 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11798 | ||
11799 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11800 | // The type here is wrong--it should be the C function type. But it |
11801 | // doesn't really matter. | |
0afbb937 | 11802 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11803 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11804 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
11805 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
11806 | closure_type = Type::make_pointer_type(closure_type); | |
11807 | ||
f8bdf81a | 11808 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11809 | |
da244e59 | 11810 | std::string thunk_name = Gogo::thunk_name(); |
11811 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 11812 | false, loc); |
11813 | ||
f8bdf81a | 11814 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11815 | cvar->set_is_used(); | |
1ecc6157 | 11816 | cvar->set_is_closure(); |
da244e59 | 11817 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
11818 | NULL, cvar); | |
f8bdf81a | 11819 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 11820 | |
f8bdf81a | 11821 | gogo->start_block(loc); |
0afbb937 | 11822 | |
11823 | // Field 0 of the closure is the function code pointer, field 1 is | |
11824 | // the value on which to invoke the method. | |
11825 | Expression* arg = Expression::make_var_reference(cp, loc); | |
11826 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
11827 | arg = Expression::make_field_reference(arg, 1, loc); | |
11828 | ||
11829 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11830 | loc); | |
11831 | ||
11832 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11833 | Expression_list* args; | |
11834 | if (orig_params == NULL || orig_params->empty()) | |
11835 | args = NULL; | |
11836 | else | |
11837 | { | |
11838 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11839 | args = new Expression_list(); | |
11840 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11841 | p != new_params->end(); |
0afbb937 | 11842 | ++p) |
11843 | { | |
11844 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11845 | go_assert(p_no != NULL | |
11846 | && p_no->is_variable() | |
11847 | && p_no->var_value()->is_parameter()); | |
11848 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11849 | } | |
11850 | } | |
11851 | ||
11852 | Call_expression* call = Expression::make_call(ifre, args, | |
11853 | orig_fntype->is_varargs(), | |
11854 | loc); | |
11855 | call->set_varargs_are_lowered(); | |
11856 | ||
11857 | Statement* s = Statement::make_return_from_call(call, loc); | |
11858 | gogo->add_statement(s); | |
11859 | Block* b = gogo->finish_block(loc); | |
11860 | gogo->add_block(b, loc); | |
11861 | gogo->lower_block(new_no, b); | |
a32698ee | 11862 | gogo->flatten_block(new_no, b); |
0afbb937 | 11863 | gogo->finish_function(loc); |
11864 | ||
11865 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11866 | return new_no; | |
11867 | } | |
11868 | ||
ea664253 | 11869 | // Get the backend representation for a method value. |
e440a328 | 11870 | |
ea664253 | 11871 | Bexpression* |
11872 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11873 | { |
0afbb937 | 11874 | Interface_type* type = this->expr_->type()->interface_type(); |
11875 | if (type == NULL) | |
11876 | { | |
11877 | go_assert(saw_errors()); | |
ea664253 | 11878 | return context->backend()->error_expression(); |
0afbb937 | 11879 | } |
11880 | ||
11881 | Named_object* thunk = | |
11882 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11883 | type, this->name_); | |
11884 | if (thunk->is_erroneous()) | |
11885 | { | |
11886 | go_assert(saw_errors()); | |
ea664253 | 11887 | return context->backend()->error_expression(); |
0afbb937 | 11888 | } |
11889 | ||
11890 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11891 | // the lowering pass because at that point we don't know whether we | |
11892 | // need to create the thunk or not. If the expression is called, we | |
11893 | // don't need the thunk. | |
11894 | ||
11895 | Location loc = this->location(); | |
11896 | ||
11897 | Struct_field_list* fields = new Struct_field_list(); | |
11898 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11899 | thunk->func_value()->type(), | |
11900 | loc))); | |
11901 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11902 | this->expr_->type(), | |
11903 | loc))); | |
11904 | Struct_type* st = Type::make_struct_type(fields, loc); | |
11905 | ||
11906 | Expression_list* vals = new Expression_list(); | |
11907 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
11908 | vals->push_back(this->expr_); | |
11909 | ||
11910 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 11911 | Bexpression* bclosure = |
11912 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 11913 | |
2387f644 | 11914 | Expression* nil_check = |
11915 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
11916 | Expression::make_nil(loc), loc); | |
ea664253 | 11917 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 11918 | |
2387f644 | 11919 | Gogo* gogo = context->gogo(); |
ea664253 | 11920 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
11921 | loc)->get_backend(context); | |
2387f644 | 11922 | |
11923 | Bexpression* bcond = | |
a32698ee | 11924 | gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc); |
2387f644 | 11925 | Bstatement* cond_statement = gogo->backend()->expression_statement(bcond); |
ea664253 | 11926 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 11927 | } |
11928 | ||
d751bb78 | 11929 | // Dump ast representation for an interface field reference. |
11930 | ||
11931 | void | |
11932 | Interface_field_reference_expression::do_dump_expression( | |
11933 | Ast_dump_context* ast_dump_context) const | |
11934 | { | |
11935 | this->expr_->dump_expression(ast_dump_context); | |
11936 | ast_dump_context->ostream() << "." << this->name_; | |
11937 | } | |
11938 | ||
e440a328 | 11939 | // Make a reference to a field in an interface. |
11940 | ||
11941 | Expression* | |
11942 | Expression::make_interface_field_reference(Expression* expr, | |
11943 | const std::string& field, | |
b13c66cd | 11944 | Location location) |
e440a328 | 11945 | { |
11946 | return new Interface_field_reference_expression(expr, field, location); | |
11947 | } | |
11948 | ||
11949 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
11950 | // is lowered after we know the type of the left hand side. | |
11951 | ||
11952 | class Selector_expression : public Parser_expression | |
11953 | { | |
11954 | public: | |
11955 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 11956 | Location location) |
e440a328 | 11957 | : Parser_expression(EXPRESSION_SELECTOR, location), |
11958 | left_(left), name_(name) | |
11959 | { } | |
11960 | ||
11961 | protected: | |
11962 | int | |
11963 | do_traverse(Traverse* traverse) | |
11964 | { return Expression::traverse(&this->left_, traverse); } | |
11965 | ||
11966 | Expression* | |
ceeb4318 | 11967 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 11968 | |
11969 | Expression* | |
11970 | do_copy() | |
11971 | { | |
11972 | return new Selector_expression(this->left_->copy(), this->name_, | |
11973 | this->location()); | |
11974 | } | |
11975 | ||
d751bb78 | 11976 | void |
11977 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
11978 | ||
e440a328 | 11979 | private: |
11980 | Expression* | |
11981 | lower_method_expression(Gogo*); | |
11982 | ||
11983 | // The expression on the left hand side. | |
11984 | Expression* left_; | |
11985 | // The name on the right hand side. | |
11986 | std::string name_; | |
11987 | }; | |
11988 | ||
11989 | // Lower a selector expression once we know the real type of the left | |
11990 | // hand side. | |
11991 | ||
11992 | Expression* | |
ceeb4318 | 11993 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
11994 | int) | |
e440a328 | 11995 | { |
11996 | Expression* left = this->left_; | |
11997 | if (left->is_type_expression()) | |
11998 | return this->lower_method_expression(gogo); | |
11999 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
12000 | this->location()); | |
12001 | } | |
12002 | ||
12003 | // Lower a method expression T.M or (*T).M. We turn this into a | |
12004 | // function literal. | |
12005 | ||
12006 | Expression* | |
12007 | Selector_expression::lower_method_expression(Gogo* gogo) | |
12008 | { | |
b13c66cd | 12009 | Location location = this->location(); |
868b439e | 12010 | Type* left_type = this->left_->type(); |
12011 | Type* type = left_type; | |
e440a328 | 12012 | const std::string& name(this->name_); |
12013 | ||
12014 | bool is_pointer; | |
12015 | if (type->points_to() == NULL) | |
12016 | is_pointer = false; | |
12017 | else | |
12018 | { | |
12019 | is_pointer = true; | |
12020 | type = type->points_to(); | |
12021 | } | |
12022 | Named_type* nt = type->named_type(); | |
12023 | if (nt == NULL) | |
12024 | { | |
631d5788 | 12025 | go_error_at(location, |
12026 | ("method expression requires named type or " | |
12027 | "pointer to named type")); | |
e440a328 | 12028 | return Expression::make_error(location); |
12029 | } | |
12030 | ||
12031 | bool is_ambiguous; | |
12032 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 12033 | const Typed_identifier* imethod = NULL; |
dcc8506b | 12034 | if (method == NULL && !is_pointer) |
ab1468c3 | 12035 | { |
12036 | Interface_type* it = nt->interface_type(); | |
12037 | if (it != NULL) | |
12038 | imethod = it->find_method(name); | |
12039 | } | |
12040 | ||
868b439e | 12041 | if ((method == NULL && imethod == NULL) |
12042 | || (left_type->named_type() != NULL && left_type->points_to() != NULL)) | |
e440a328 | 12043 | { |
12044 | if (!is_ambiguous) | |
631d5788 | 12045 | go_error_at(location, "type %<%s%s%> has no method %<%s%>", |
12046 | is_pointer ? "*" : "", | |
12047 | nt->message_name().c_str(), | |
12048 | Gogo::message_name(name).c_str()); | |
e440a328 | 12049 | else |
631d5788 | 12050 | go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
12051 | Gogo::message_name(name).c_str(), | |
12052 | is_pointer ? "*" : "", | |
12053 | nt->message_name().c_str()); | |
e440a328 | 12054 | return Expression::make_error(location); |
12055 | } | |
12056 | ||
ab1468c3 | 12057 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 12058 | { |
631d5788 | 12059 | go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", |
12060 | nt->message_name().c_str(), | |
12061 | Gogo::message_name(name).c_str()); | |
e440a328 | 12062 | return Expression::make_error(location); |
12063 | } | |
12064 | ||
12065 | // Build a new function type in which the receiver becomes the first | |
12066 | // argument. | |
ab1468c3 | 12067 | Function_type* method_type; |
12068 | if (method != NULL) | |
12069 | { | |
12070 | method_type = method->type(); | |
c484d925 | 12071 | go_assert(method_type->is_method()); |
ab1468c3 | 12072 | } |
12073 | else | |
12074 | { | |
12075 | method_type = imethod->type()->function_type(); | |
c484d925 | 12076 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 12077 | } |
e440a328 | 12078 | |
12079 | const char* const receiver_name = "$this"; | |
12080 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
12081 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
12082 | location)); | |
12083 | ||
12084 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
12085 | if (method_parameters != NULL) | |
12086 | { | |
f470da59 | 12087 | int i = 0; |
e440a328 | 12088 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
12089 | p != method_parameters->end(); | |
f470da59 | 12090 | ++p, ++i) |
12091 | { | |
68883531 | 12092 | if (!p->name().empty()) |
f470da59 | 12093 | parameters->push_back(*p); |
12094 | else | |
12095 | { | |
12096 | char buf[20]; | |
12097 | snprintf(buf, sizeof buf, "$param%d", i); | |
12098 | parameters->push_back(Typed_identifier(buf, p->type(), | |
12099 | p->location())); | |
12100 | } | |
12101 | } | |
e440a328 | 12102 | } |
12103 | ||
12104 | const Typed_identifier_list* method_results = method_type->results(); | |
12105 | Typed_identifier_list* results; | |
12106 | if (method_results == NULL) | |
12107 | results = NULL; | |
12108 | else | |
12109 | { | |
12110 | results = new Typed_identifier_list(); | |
12111 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
12112 | p != method_results->end(); | |
12113 | ++p) | |
12114 | results->push_back(*p); | |
12115 | } | |
12116 | ||
12117 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
12118 | location); | |
12119 | if (method_type->is_varargs()) | |
12120 | fntype->set_is_varargs(); | |
12121 | ||
12122 | // We generate methods which always takes a pointer to the receiver | |
12123 | // as their first argument. If this is for a pointer type, we can | |
12124 | // simply reuse the existing function. We use an internal hack to | |
12125 | // get the right type. | |
8381eda7 | 12126 | // FIXME: This optimization is disabled because it doesn't yet work |
12127 | // with function descriptors when the method expression is not | |
12128 | // directly called. | |
12129 | if (method != NULL && is_pointer && false) | |
e440a328 | 12130 | { |
12131 | Named_object* mno = (method->needs_stub_method() | |
12132 | ? method->stub_object() | |
12133 | : method->named_object()); | |
12134 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
12135 | f = Expression::make_cast(fntype, f, location); | |
12136 | Type_conversion_expression* tce = | |
12137 | static_cast<Type_conversion_expression*>(f); | |
12138 | tce->set_may_convert_function_types(); | |
12139 | return f; | |
12140 | } | |
12141 | ||
12142 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
12143 | location); | |
12144 | ||
12145 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 12146 | go_assert(vno != NULL); |
e440a328 | 12147 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 12148 | Expression* bm; |
12149 | if (method != NULL) | |
12150 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
12151 | else | |
12152 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 12153 | |
12154 | // Even though we found the method above, if it has an error type we | |
12155 | // may see an error here. | |
12156 | if (bm->is_error_expression()) | |
463fe805 | 12157 | { |
12158 | gogo->finish_function(location); | |
12159 | return bm; | |
12160 | } | |
e440a328 | 12161 | |
12162 | Expression_list* args; | |
f470da59 | 12163 | if (parameters->size() <= 1) |
e440a328 | 12164 | args = NULL; |
12165 | else | |
12166 | { | |
12167 | args = new Expression_list(); | |
f470da59 | 12168 | Typed_identifier_list::const_iterator p = parameters->begin(); |
12169 | ++p; | |
12170 | for (; p != parameters->end(); ++p) | |
e440a328 | 12171 | { |
12172 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 12173 | go_assert(vno != NULL); |
e440a328 | 12174 | args->push_back(Expression::make_var_reference(vno, location)); |
12175 | } | |
12176 | } | |
12177 | ||
ceeb4318 | 12178 | gogo->start_block(location); |
12179 | ||
e440a328 | 12180 | Call_expression* call = Expression::make_call(bm, args, |
12181 | method_type->is_varargs(), | |
12182 | location); | |
12183 | ||
0afbb937 | 12184 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 12185 | gogo->add_statement(s); |
12186 | ||
ceeb4318 | 12187 | Block* b = gogo->finish_block(location); |
12188 | ||
12189 | gogo->add_block(b, location); | |
12190 | ||
12191 | // Lower the call in case there are multiple results. | |
12192 | gogo->lower_block(no, b); | |
a32698ee | 12193 | gogo->flatten_block(no, b); |
ceeb4318 | 12194 | |
e440a328 | 12195 | gogo->finish_function(location); |
12196 | ||
12197 | return Expression::make_func_reference(no, NULL, location); | |
12198 | } | |
12199 | ||
d751bb78 | 12200 | // Dump the ast for a selector expression. |
12201 | ||
12202 | void | |
12203 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12204 | const | |
12205 | { | |
12206 | ast_dump_context->dump_expression(this->left_); | |
12207 | ast_dump_context->ostream() << "."; | |
12208 | ast_dump_context->ostream() << this->name_; | |
12209 | } | |
12210 | ||
e440a328 | 12211 | // Make a selector expression. |
12212 | ||
12213 | Expression* | |
12214 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 12215 | Location location) |
e440a328 | 12216 | { |
12217 | return new Selector_expression(left, name, location); | |
12218 | } | |
12219 | ||
da244e59 | 12220 | // Class Allocation_expression. |
e440a328 | 12221 | |
da244e59 | 12222 | int |
12223 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 12224 | { |
da244e59 | 12225 | return Type::traverse(this->type_, traverse); |
12226 | } | |
e440a328 | 12227 | |
da244e59 | 12228 | Type* |
12229 | Allocation_expression::do_type() | |
12230 | { | |
12231 | return Type::make_pointer_type(this->type_); | |
12232 | } | |
e440a328 | 12233 | |
da244e59 | 12234 | // Make a copy of an allocation expression. |
e440a328 | 12235 | |
da244e59 | 12236 | Expression* |
12237 | Allocation_expression::do_copy() | |
12238 | { | |
12239 | Allocation_expression* alloc = | |
12240 | new Allocation_expression(this->type_, this->location()); | |
12241 | if (this->allocate_on_stack_) | |
12242 | alloc->set_allocate_on_stack(); | |
12243 | return alloc; | |
12244 | } | |
e440a328 | 12245 | |
ea664253 | 12246 | // Return the backend representation for an allocation expression. |
e440a328 | 12247 | |
ea664253 | 12248 | Bexpression* |
12249 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12250 | { |
2c809f8f | 12251 | Gogo* gogo = context->gogo(); |
12252 | Location loc = this->location(); | |
da244e59 | 12253 | |
45ff893b | 12254 | Node* n = Node::make_node(this); |
12255 | if (this->allocate_on_stack_ | |
12256 | || (n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
da244e59 | 12257 | { |
2a305b85 | 12258 | int64_t size; |
12259 | bool ok = this->type_->backend_type_size(gogo, &size); | |
12260 | if (!ok) | |
12261 | { | |
12262 | go_assert(saw_errors()); | |
12263 | return gogo->backend()->error_expression(); | |
12264 | } | |
d5d1c295 | 12265 | return gogo->backend()->stack_allocation_expression(size, loc); |
da244e59 | 12266 | } |
12267 | ||
2a305b85 | 12268 | Btype* btype = this->type_->get_backend(gogo); |
12269 | Bexpression* space = | |
ea664253 | 12270 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 12271 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 12272 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 12273 | } |
12274 | ||
d751bb78 | 12275 | // Dump ast representation for an allocation expression. |
12276 | ||
12277 | void | |
12278 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12279 | const | |
12280 | { | |
12281 | ast_dump_context->ostream() << "new("; | |
12282 | ast_dump_context->dump_type(this->type_); | |
12283 | ast_dump_context->ostream() << ")"; | |
12284 | } | |
12285 | ||
e440a328 | 12286 | // Make an allocation expression. |
12287 | ||
12288 | Expression* | |
b13c66cd | 12289 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12290 | { |
12291 | return new Allocation_expression(type, location); | |
12292 | } | |
12293 | ||
e32de7ba | 12294 | // Class Ordered_value_list. |
e440a328 | 12295 | |
12296 | int | |
e32de7ba | 12297 | Ordered_value_list::traverse_vals(Traverse* traverse) |
e440a328 | 12298 | { |
0c4f5a19 | 12299 | if (this->vals_ != NULL) |
12300 | { | |
12301 | if (this->traverse_order_ == NULL) | |
12302 | { | |
12303 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12304 | return TRAVERSE_EXIT; | |
12305 | } | |
12306 | else | |
12307 | { | |
e32de7ba | 12308 | for (std::vector<unsigned long>::const_iterator p = |
12309 | this->traverse_order_->begin(); | |
0c4f5a19 | 12310 | p != this->traverse_order_->end(); |
12311 | ++p) | |
12312 | { | |
12313 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12314 | == TRAVERSE_EXIT) | |
12315 | return TRAVERSE_EXIT; | |
12316 | } | |
12317 | } | |
12318 | } | |
e32de7ba | 12319 | return TRAVERSE_CONTINUE; |
12320 | } | |
12321 | ||
12322 | // Class Struct_construction_expression. | |
12323 | ||
12324 | // Traversal. | |
12325 | ||
12326 | int | |
12327 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12328 | { | |
12329 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) | |
12330 | return TRAVERSE_EXIT; | |
e440a328 | 12331 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12332 | return TRAVERSE_EXIT; | |
12333 | return TRAVERSE_CONTINUE; | |
12334 | } | |
12335 | ||
12336 | // Return whether this is a constant initializer. | |
12337 | ||
12338 | bool | |
12339 | Struct_construction_expression::is_constant_struct() const | |
12340 | { | |
e32de7ba | 12341 | if (this->vals() == NULL) |
e440a328 | 12342 | return true; |
e32de7ba | 12343 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12344 | pv != this->vals()->end(); | |
e440a328 | 12345 | ++pv) |
12346 | { | |
12347 | if (*pv != NULL | |
12348 | && !(*pv)->is_constant() | |
12349 | && (!(*pv)->is_composite_literal() | |
12350 | || (*pv)->is_nonconstant_composite_literal())) | |
12351 | return false; | |
12352 | } | |
12353 | ||
12354 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12355 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12356 | pf != fields->end(); | |
12357 | ++pf) | |
12358 | { | |
12359 | // There are no constant constructors for interfaces. | |
12360 | if (pf->type()->interface_type() != NULL) | |
12361 | return false; | |
12362 | } | |
12363 | ||
12364 | return true; | |
12365 | } | |
12366 | ||
3ae06f68 | 12367 | // Return whether this struct can be used as a constant initializer. |
f9ca30f9 | 12368 | |
12369 | bool | |
3ae06f68 | 12370 | Struct_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12371 | { |
e32de7ba | 12372 | if (this->vals() == NULL) |
f9ca30f9 | 12373 | return true; |
e32de7ba | 12374 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12375 | pv != this->vals()->end(); | |
f9ca30f9 | 12376 | ++pv) |
12377 | { | |
3ae06f68 | 12378 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12379 | return false; |
12380 | } | |
12381 | return true; | |
12382 | } | |
12383 | ||
e440a328 | 12384 | // Final type determination. |
12385 | ||
12386 | void | |
12387 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12388 | { | |
e32de7ba | 12389 | if (this->vals() == NULL) |
e440a328 | 12390 | return; |
12391 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
e32de7ba | 12392 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12393 | for (Struct_field_list::const_iterator pf = fields->begin(); |
12394 | pf != fields->end(); | |
12395 | ++pf, ++pv) | |
12396 | { | |
e32de7ba | 12397 | if (pv == this->vals()->end()) |
e440a328 | 12398 | return; |
12399 | if (*pv != NULL) | |
12400 | { | |
12401 | Type_context subcontext(pf->type(), false); | |
12402 | (*pv)->determine_type(&subcontext); | |
12403 | } | |
12404 | } | |
a6cb4c0e | 12405 | // Extra values are an error we will report elsewhere; we still want |
12406 | // to determine the type to avoid knockon errors. | |
e32de7ba | 12407 | for (; pv != this->vals()->end(); ++pv) |
a6cb4c0e | 12408 | (*pv)->determine_type_no_context(); |
e440a328 | 12409 | } |
12410 | ||
12411 | // Check types. | |
12412 | ||
12413 | void | |
12414 | Struct_construction_expression::do_check_types(Gogo*) | |
12415 | { | |
e32de7ba | 12416 | if (this->vals() == NULL) |
e440a328 | 12417 | return; |
12418 | ||
12419 | Struct_type* st = this->type_->struct_type(); | |
e32de7ba | 12420 | if (this->vals()->size() > st->field_count()) |
e440a328 | 12421 | { |
12422 | this->report_error(_("too many expressions for struct")); | |
12423 | return; | |
12424 | } | |
12425 | ||
12426 | const Struct_field_list* fields = st->fields(); | |
e32de7ba | 12427 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12428 | int i = 0; |
12429 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12430 | pf != fields->end(); | |
12431 | ++pf, ++pv, ++i) | |
12432 | { | |
e32de7ba | 12433 | if (pv == this->vals()->end()) |
e440a328 | 12434 | { |
12435 | this->report_error(_("too few expressions for struct")); | |
12436 | break; | |
12437 | } | |
12438 | ||
12439 | if (*pv == NULL) | |
12440 | continue; | |
12441 | ||
12442 | std::string reason; | |
12443 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12444 | { | |
12445 | if (reason.empty()) | |
631d5788 | 12446 | go_error_at((*pv)->location(), |
12447 | "incompatible type for field %d in struct construction", | |
12448 | i + 1); | |
e440a328 | 12449 | else |
631d5788 | 12450 | go_error_at((*pv)->location(), |
12451 | ("incompatible type for field %d in " | |
12452 | "struct construction (%s)"), | |
12453 | i + 1, reason.c_str()); | |
e440a328 | 12454 | this->set_is_error(); |
12455 | } | |
12456 | } | |
e32de7ba | 12457 | go_assert(pv == this->vals()->end()); |
e440a328 | 12458 | } |
12459 | ||
8ba8cc87 | 12460 | // Flatten a struct construction expression. Store the values into |
12461 | // temporaries in case they need interface conversion. | |
12462 | ||
12463 | Expression* | |
12464 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
12465 | Statement_inserter* inserter) | |
12466 | { | |
e32de7ba | 12467 | if (this->vals() == NULL) |
8ba8cc87 | 12468 | return this; |
12469 | ||
12470 | // If this is a constant struct, we don't need temporaries. | |
12471 | if (this->is_constant_struct()) | |
12472 | return this; | |
12473 | ||
12474 | Location loc = this->location(); | |
e32de7ba | 12475 | for (Expression_list::iterator pv = this->vals()->begin(); |
12476 | pv != this->vals()->end(); | |
8ba8cc87 | 12477 | ++pv) |
12478 | { | |
12479 | if (*pv != NULL) | |
12480 | { | |
5bf8be8b | 12481 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12482 | { | |
12483 | go_assert(saw_errors()); | |
12484 | return Expression::make_error(loc); | |
12485 | } | |
8ba8cc87 | 12486 | if (!(*pv)->is_variable()) |
12487 | { | |
12488 | Temporary_statement* temp = | |
12489 | Statement::make_temporary(NULL, *pv, loc); | |
12490 | inserter->insert(temp); | |
12491 | *pv = Expression::make_temporary_reference(temp, loc); | |
12492 | } | |
12493 | } | |
12494 | } | |
12495 | return this; | |
12496 | } | |
12497 | ||
ea664253 | 12498 | // Return the backend representation for constructing a struct. |
e440a328 | 12499 | |
ea664253 | 12500 | Bexpression* |
12501 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12502 | { |
12503 | Gogo* gogo = context->gogo(); | |
12504 | ||
2c809f8f | 12505 | Btype* btype = this->type_->get_backend(gogo); |
e32de7ba | 12506 | if (this->vals() == NULL) |
ea664253 | 12507 | return gogo->backend()->zero_expression(btype); |
e440a328 | 12508 | |
e440a328 | 12509 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e32de7ba | 12510 | Expression_list::const_iterator pv = this->vals()->begin(); |
2c809f8f | 12511 | std::vector<Bexpression*> init; |
12512 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12513 | pf != fields->end(); | |
12514 | ++pf) | |
e440a328 | 12515 | { |
63697958 | 12516 | Btype* fbtype = pf->type()->get_backend(gogo); |
e32de7ba | 12517 | if (pv == this->vals()->end()) |
2c809f8f | 12518 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12519 | else if (*pv == NULL) |
12520 | { | |
2c809f8f | 12521 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12522 | ++pv; |
12523 | } | |
12524 | else | |
12525 | { | |
2c809f8f | 12526 | Expression* val = |
12527 | Expression::convert_for_assignment(gogo, pf->type(), | |
12528 | *pv, this->location()); | |
ea664253 | 12529 | init.push_back(val->get_backend(context)); |
e440a328 | 12530 | ++pv; |
12531 | } | |
e440a328 | 12532 | } |
ea664253 | 12533 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12534 | } |
12535 | ||
12536 | // Export a struct construction. | |
12537 | ||
12538 | void | |
12539 | Struct_construction_expression::do_export(Export* exp) const | |
12540 | { | |
12541 | exp->write_c_string("convert("); | |
12542 | exp->write_type(this->type_); | |
e32de7ba | 12543 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12544 | pv != this->vals()->end(); | |
e440a328 | 12545 | ++pv) |
12546 | { | |
12547 | exp->write_c_string(", "); | |
12548 | if (*pv != NULL) | |
12549 | (*pv)->export_expression(exp); | |
12550 | } | |
12551 | exp->write_c_string(")"); | |
12552 | } | |
12553 | ||
d751bb78 | 12554 | // Dump ast representation of a struct construction expression. |
12555 | ||
12556 | void | |
12557 | Struct_construction_expression::do_dump_expression( | |
12558 | Ast_dump_context* ast_dump_context) const | |
12559 | { | |
d751bb78 | 12560 | ast_dump_context->dump_type(this->type_); |
12561 | ast_dump_context->ostream() << "{"; | |
e32de7ba | 12562 | ast_dump_context->dump_expression_list(this->vals()); |
d751bb78 | 12563 | ast_dump_context->ostream() << "}"; |
12564 | } | |
12565 | ||
e440a328 | 12566 | // Make a struct composite literal. This used by the thunk code. |
12567 | ||
12568 | Expression* | |
12569 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12570 | Location location) |
e440a328 | 12571 | { |
c484d925 | 12572 | go_assert(type->struct_type() != NULL); |
e440a328 | 12573 | return new Struct_construction_expression(type, vals, location); |
12574 | } | |
12575 | ||
da244e59 | 12576 | // Class Array_construction_expression. |
e440a328 | 12577 | |
12578 | // Traversal. | |
12579 | ||
12580 | int | |
12581 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12582 | { | |
e32de7ba | 12583 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) |
e440a328 | 12584 | return TRAVERSE_EXIT; |
12585 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12586 | return TRAVERSE_EXIT; | |
12587 | return TRAVERSE_CONTINUE; | |
12588 | } | |
12589 | ||
12590 | // Return whether this is a constant initializer. | |
12591 | ||
12592 | bool | |
12593 | Array_construction_expression::is_constant_array() const | |
12594 | { | |
e32de7ba | 12595 | if (this->vals() == NULL) |
e440a328 | 12596 | return true; |
12597 | ||
12598 | // There are no constant constructors for interfaces. | |
12599 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12600 | return false; | |
12601 | ||
e32de7ba | 12602 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12603 | pv != this->vals()->end(); | |
e440a328 | 12604 | ++pv) |
12605 | { | |
12606 | if (*pv != NULL | |
12607 | && !(*pv)->is_constant() | |
12608 | && (!(*pv)->is_composite_literal() | |
12609 | || (*pv)->is_nonconstant_composite_literal())) | |
12610 | return false; | |
12611 | } | |
12612 | return true; | |
12613 | } | |
12614 | ||
3ae06f68 | 12615 | // Return whether this can be used a constant initializer. |
f9ca30f9 | 12616 | |
12617 | bool | |
3ae06f68 | 12618 | Array_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12619 | { |
e32de7ba | 12620 | if (this->vals() == NULL) |
f9ca30f9 | 12621 | return true; |
e32de7ba | 12622 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12623 | pv != this->vals()->end(); | |
f9ca30f9 | 12624 | ++pv) |
12625 | { | |
3ae06f68 | 12626 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12627 | return false; |
12628 | } | |
12629 | return true; | |
12630 | } | |
12631 | ||
e440a328 | 12632 | // Final type determination. |
12633 | ||
12634 | void | |
12635 | Array_construction_expression::do_determine_type(const Type_context*) | |
12636 | { | |
e32de7ba | 12637 | if (this->vals() == NULL) |
e440a328 | 12638 | return; |
12639 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
e32de7ba | 12640 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12641 | pv != this->vals()->end(); | |
e440a328 | 12642 | ++pv) |
12643 | { | |
12644 | if (*pv != NULL) | |
12645 | (*pv)->determine_type(&subcontext); | |
12646 | } | |
12647 | } | |
12648 | ||
12649 | // Check types. | |
12650 | ||
12651 | void | |
12652 | Array_construction_expression::do_check_types(Gogo*) | |
12653 | { | |
e32de7ba | 12654 | if (this->vals() == NULL) |
e440a328 | 12655 | return; |
12656 | ||
12657 | Array_type* at = this->type_->array_type(); | |
12658 | int i = 0; | |
12659 | Type* element_type = at->element_type(); | |
e32de7ba | 12660 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12661 | pv != this->vals()->end(); | |
e440a328 | 12662 | ++pv, ++i) |
12663 | { | |
12664 | if (*pv != NULL | |
12665 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12666 | { | |
631d5788 | 12667 | go_error_at((*pv)->location(), |
12668 | "incompatible type for element %d in composite literal", | |
12669 | i + 1); | |
e440a328 | 12670 | this->set_is_error(); |
12671 | } | |
12672 | } | |
e440a328 | 12673 | } |
12674 | ||
8ba8cc87 | 12675 | // Flatten an array construction expression. Store the values into |
12676 | // temporaries in case they need interface conversion. | |
12677 | ||
12678 | Expression* | |
12679 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12680 | Statement_inserter* inserter) | |
12681 | { | |
e32de7ba | 12682 | if (this->vals() == NULL) |
8ba8cc87 | 12683 | return this; |
12684 | ||
12685 | // If this is a constant array, we don't need temporaries. | |
12686 | if (this->is_constant_array()) | |
12687 | return this; | |
12688 | ||
12689 | Location loc = this->location(); | |
e32de7ba | 12690 | for (Expression_list::iterator pv = this->vals()->begin(); |
12691 | pv != this->vals()->end(); | |
8ba8cc87 | 12692 | ++pv) |
12693 | { | |
12694 | if (*pv != NULL) | |
12695 | { | |
5bf8be8b | 12696 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12697 | { | |
12698 | go_assert(saw_errors()); | |
12699 | return Expression::make_error(loc); | |
12700 | } | |
8ba8cc87 | 12701 | if (!(*pv)->is_variable()) |
12702 | { | |
12703 | Temporary_statement* temp = | |
12704 | Statement::make_temporary(NULL, *pv, loc); | |
12705 | inserter->insert(temp); | |
12706 | *pv = Expression::make_temporary_reference(temp, loc); | |
12707 | } | |
12708 | } | |
12709 | } | |
12710 | return this; | |
12711 | } | |
12712 | ||
2c809f8f | 12713 | // Get a constructor expression for the array values. |
e440a328 | 12714 | |
2c809f8f | 12715 | Bexpression* |
12716 | Array_construction_expression::get_constructor(Translate_context* context, | |
12717 | Btype* array_btype) | |
e440a328 | 12718 | { |
e440a328 | 12719 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12720 | |
12721 | std::vector<unsigned long> indexes; | |
12722 | std::vector<Bexpression*> vals; | |
12723 | Gogo* gogo = context->gogo(); | |
e32de7ba | 12724 | if (this->vals() != NULL) |
e440a328 | 12725 | { |
12726 | size_t i = 0; | |
ffe743ca | 12727 | std::vector<unsigned long>::const_iterator pi; |
12728 | if (this->indexes_ != NULL) | |
12729 | pi = this->indexes_->begin(); | |
e32de7ba | 12730 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12731 | pv != this->vals()->end(); | |
e440a328 | 12732 | ++pv, ++i) |
12733 | { | |
ffe743ca | 12734 | if (this->indexes_ != NULL) |
12735 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12736 | |
12737 | if (this->indexes_ == NULL) | |
2c809f8f | 12738 | indexes.push_back(i); |
ffe743ca | 12739 | else |
2c809f8f | 12740 | indexes.push_back(*pi); |
e440a328 | 12741 | if (*pv == NULL) |
63697958 | 12742 | { |
63697958 | 12743 | Btype* ebtype = element_type->get_backend(gogo); |
12744 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12745 | vals.push_back(zv); |
63697958 | 12746 | } |
e440a328 | 12747 | else |
12748 | { | |
2c809f8f | 12749 | Expression* val_expr = |
12750 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12751 | this->location()); | |
ea664253 | 12752 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12753 | } |
ffe743ca | 12754 | if (this->indexes_ != NULL) |
12755 | ++pi; | |
e440a328 | 12756 | } |
ffe743ca | 12757 | if (this->indexes_ != NULL) |
12758 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12759 | } |
2c809f8f | 12760 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12761 | vals, this->location()); | |
e440a328 | 12762 | } |
12763 | ||
12764 | // Export an array construction. | |
12765 | ||
12766 | void | |
12767 | Array_construction_expression::do_export(Export* exp) const | |
12768 | { | |
12769 | exp->write_c_string("convert("); | |
12770 | exp->write_type(this->type_); | |
e32de7ba | 12771 | if (this->vals() != NULL) |
e440a328 | 12772 | { |
ffe743ca | 12773 | std::vector<unsigned long>::const_iterator pi; |
12774 | if (this->indexes_ != NULL) | |
12775 | pi = this->indexes_->begin(); | |
e32de7ba | 12776 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12777 | pv != this->vals()->end(); | |
e440a328 | 12778 | ++pv) |
12779 | { | |
12780 | exp->write_c_string(", "); | |
ffe743ca | 12781 | |
12782 | if (this->indexes_ != NULL) | |
12783 | { | |
12784 | char buf[100]; | |
12785 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12786 | exp->write_c_string(buf); | |
12787 | exp->write_c_string(":"); | |
12788 | } | |
12789 | ||
e440a328 | 12790 | if (*pv != NULL) |
12791 | (*pv)->export_expression(exp); | |
ffe743ca | 12792 | |
12793 | if (this->indexes_ != NULL) | |
12794 | ++pi; | |
e440a328 | 12795 | } |
12796 | } | |
12797 | exp->write_c_string(")"); | |
12798 | } | |
12799 | ||
0e9a2e72 | 12800 | // Dump ast representation of an array construction expression. |
d751bb78 | 12801 | |
12802 | void | |
12803 | Array_construction_expression::do_dump_expression( | |
12804 | Ast_dump_context* ast_dump_context) const | |
12805 | { | |
ffe743ca | 12806 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12807 | |
12808 | ast_dump_context->ostream() << "[" ; | |
12809 | if (length != NULL) | |
12810 | { | |
12811 | ast_dump_context->dump_expression(length); | |
12812 | } | |
12813 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12814 | ast_dump_context->dump_type(this->type_); |
0e9a2e72 | 12815 | this->dump_slice_storage_expression(ast_dump_context); |
d751bb78 | 12816 | ast_dump_context->ostream() << "{" ; |
ffe743ca | 12817 | if (this->indexes_ == NULL) |
e32de7ba | 12818 | ast_dump_context->dump_expression_list(this->vals()); |
ffe743ca | 12819 | else |
12820 | { | |
e32de7ba | 12821 | Expression_list::const_iterator pv = this->vals()->begin(); |
ffe743ca | 12822 | for (std::vector<unsigned long>::const_iterator pi = |
12823 | this->indexes_->begin(); | |
12824 | pi != this->indexes_->end(); | |
12825 | ++pi, ++pv) | |
12826 | { | |
12827 | if (pi != this->indexes_->begin()) | |
12828 | ast_dump_context->ostream() << ", "; | |
12829 | ast_dump_context->ostream() << *pi << ':'; | |
12830 | ast_dump_context->dump_expression(*pv); | |
12831 | } | |
12832 | } | |
d751bb78 | 12833 | ast_dump_context->ostream() << "}" ; |
12834 | ||
12835 | } | |
12836 | ||
da244e59 | 12837 | // Class Fixed_array_construction_expression. |
e440a328 | 12838 | |
da244e59 | 12839 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
12840 | Type* type, const std::vector<unsigned long>* indexes, | |
12841 | Expression_list* vals, Location location) | |
12842 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
12843 | type, indexes, vals, location) | |
12844 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12845 | |
ea664253 | 12846 | // Return the backend representation for constructing a fixed array. |
e440a328 | 12847 | |
ea664253 | 12848 | Bexpression* |
12849 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12850 | { |
9f0e0513 | 12851 | Type* type = this->type(); |
12852 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 12853 | return this->get_constructor(context, btype); |
e440a328 | 12854 | } |
12855 | ||
76f85fd6 | 12856 | Expression* |
12857 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
12858 | Location location) | |
12859 | { | |
12860 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
12861 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
12862 | } | |
12863 | ||
da244e59 | 12864 | // Class Slice_construction_expression. |
e440a328 | 12865 | |
da244e59 | 12866 | Slice_construction_expression::Slice_construction_expression( |
12867 | Type* type, const std::vector<unsigned long>* indexes, | |
12868 | Expression_list* vals, Location location) | |
12869 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
12870 | type, indexes, vals, location), | |
0e9a2e72 | 12871 | valtype_(NULL), array_val_(NULL), slice_storage_(NULL), |
12872 | storage_escapes_(true) | |
e440a328 | 12873 | { |
da244e59 | 12874 | go_assert(type->is_slice_type()); |
23d77f91 | 12875 | |
da244e59 | 12876 | unsigned long lenval; |
12877 | Expression* length; | |
12878 | if (vals == NULL || vals->empty()) | |
12879 | lenval = 0; | |
12880 | else | |
12881 | { | |
12882 | if (this->indexes() == NULL) | |
12883 | lenval = vals->size(); | |
12884 | else | |
12885 | lenval = indexes->back() + 1; | |
12886 | } | |
12887 | Type* int_type = Type::lookup_integer_type("int"); | |
12888 | length = Expression::make_integer_ul(lenval, int_type, location); | |
12889 | Type* element_type = type->array_type()->element_type(); | |
12890 | this->valtype_ = Type::make_array_type(element_type, length); | |
12891 | } | |
e440a328 | 12892 | |
23d77f91 | 12893 | // Traversal. |
12894 | ||
12895 | int | |
12896 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
12897 | { | |
12898 | if (this->Array_construction_expression::do_traverse(traverse) | |
12899 | == TRAVERSE_EXIT) | |
12900 | return TRAVERSE_EXIT; | |
12901 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
12902 | return TRAVERSE_EXIT; | |
0e9a2e72 | 12903 | if (this->array_val_ != NULL |
12904 | && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) | |
12905 | return TRAVERSE_EXIT; | |
12906 | if (this->slice_storage_ != NULL | |
12907 | && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) | |
12908 | return TRAVERSE_EXIT; | |
23d77f91 | 12909 | return TRAVERSE_CONTINUE; |
12910 | } | |
12911 | ||
0e9a2e72 | 12912 | // Helper routine to create fixed array value underlying the slice literal. |
12913 | // May be called during flattening, or later during do_get_backend(). | |
e440a328 | 12914 | |
0e9a2e72 | 12915 | Expression* |
12916 | Slice_construction_expression::create_array_val() | |
e440a328 | 12917 | { |
f9c68f17 | 12918 | Array_type* array_type = this->type()->array_type(); |
12919 | if (array_type == NULL) | |
12920 | { | |
c484d925 | 12921 | go_assert(this->type()->is_error()); |
0e9a2e72 | 12922 | return NULL; |
f9c68f17 | 12923 | } |
12924 | ||
f23d7786 | 12925 | Location loc = this->location(); |
23d77f91 | 12926 | go_assert(this->valtype_ != NULL); |
3d60812e | 12927 | |
f23d7786 | 12928 | Expression_list* vals = this->vals(); |
0e9a2e72 | 12929 | return new Fixed_array_construction_expression( |
12930 | this->valtype_, this->indexes(), vals, loc); | |
12931 | } | |
12932 | ||
12933 | // If we're previous established that the slice storage does not | |
12934 | // escape, then create a separate array temp val here for it. We | |
12935 | // need to do this as part of flattening so as to be able to insert | |
12936 | // the new temp statement. | |
12937 | ||
12938 | Expression* | |
12939 | Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no, | |
12940 | Statement_inserter* inserter) | |
12941 | { | |
12942 | if (this->type()->array_type() == NULL) | |
12943 | return NULL; | |
12944 | ||
12945 | // Base class flattening first | |
12946 | this->Array_construction_expression::do_flatten(gogo, no, inserter); | |
12947 | ||
a1bbc2c3 | 12948 | // Create a stack-allocated storage temp if storage won't escape |
12949 | if (!this->storage_escapes_ && this->slice_storage_ == NULL) | |
0e9a2e72 | 12950 | { |
12951 | Location loc = this->location(); | |
12952 | this->array_val_ = create_array_val(); | |
12953 | go_assert(this->array_val_); | |
12954 | Temporary_statement* temp = | |
12955 | Statement::make_temporary(this->valtype_, this->array_val_, loc); | |
12956 | inserter->insert(temp); | |
12957 | this->slice_storage_ = Expression::make_temporary_reference(temp, loc); | |
12958 | } | |
12959 | return this; | |
12960 | } | |
12961 | ||
12962 | // When dumping a slice construction expression that has an explicit | |
12963 | // storeage temp, emit the temp here (if we don't do this the storage | |
12964 | // temp appears unused in the AST dump). | |
12965 | ||
12966 | void | |
12967 | Slice_construction_expression:: | |
12968 | dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const | |
12969 | { | |
12970 | if (this->slice_storage_ == NULL) | |
12971 | return; | |
12972 | ast_dump_context->ostream() << "storage=" ; | |
12973 | ast_dump_context->dump_expression(this->slice_storage_); | |
12974 | } | |
12975 | ||
12976 | // Return the backend representation for constructing a slice. | |
12977 | ||
12978 | Bexpression* | |
12979 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
12980 | { | |
12981 | if (this->array_val_ == NULL) | |
12982 | this->array_val_ = create_array_val(); | |
12983 | if (this->array_val_ == NULL) | |
12984 | { | |
12985 | go_assert(this->type()->is_error()); | |
12986 | return context->backend()->error_expression(); | |
12987 | } | |
12988 | ||
12989 | Location loc = this->location(); | |
e440a328 | 12990 | |
3ae06f68 | 12991 | bool is_static_initializer = this->array_val_->is_static_initializer(); |
d8829beb | 12992 | |
12993 | // We have to copy the initial values into heap memory if we are in | |
3ae06f68 | 12994 | // a function or if the values are not constants. |
12995 | bool copy_to_heap = context->function() != NULL || !is_static_initializer; | |
e440a328 | 12996 | |
f23d7786 | 12997 | Expression* space; |
0e9a2e72 | 12998 | |
12999 | if (this->slice_storage_ != NULL) | |
13000 | { | |
13001 | go_assert(!this->storage_escapes_); | |
13002 | space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); | |
13003 | } | |
13004 | else if (!copy_to_heap) | |
e440a328 | 13005 | { |
f23d7786 | 13006 | // The initializer will only run once. |
0e9a2e72 | 13007 | space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); |
f23d7786 | 13008 | space->unary_expression()->set_is_slice_init(); |
e440a328 | 13009 | } |
13010 | else | |
45ff893b | 13011 | { |
0e9a2e72 | 13012 | space = Expression::make_heap_expression(this->array_val_, loc); |
45ff893b | 13013 | Node* n = Node::make_node(this); |
13014 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
13015 | { | |
13016 | n = Node::make_node(space); | |
13017 | n->set_encoding(Node::ESCAPE_NONE); | |
13018 | } | |
13019 | } | |
e440a328 | 13020 | |
2c809f8f | 13021 | // Build a constructor for the slice. |
f23d7786 | 13022 | Expression* len = this->valtype_->array_type()->length(); |
13023 | Expression* slice_val = | |
13024 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 13025 | return slice_val->get_backend(context); |
e440a328 | 13026 | } |
13027 | ||
13028 | // Make a slice composite literal. This is used by the type | |
13029 | // descriptor code. | |
13030 | ||
0e9a2e72 | 13031 | Slice_construction_expression* |
e440a328 | 13032 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, |
b13c66cd | 13033 | Location location) |
e440a328 | 13034 | { |
411eb89e | 13035 | go_assert(type->is_slice_type()); |
2c809f8f | 13036 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 13037 | } |
13038 | ||
da244e59 | 13039 | // Class Map_construction_expression. |
e440a328 | 13040 | |
13041 | // Traversal. | |
13042 | ||
13043 | int | |
13044 | Map_construction_expression::do_traverse(Traverse* traverse) | |
13045 | { | |
13046 | if (this->vals_ != NULL | |
13047 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
13048 | return TRAVERSE_EXIT; | |
13049 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13050 | return TRAVERSE_EXIT; | |
13051 | return TRAVERSE_CONTINUE; | |
13052 | } | |
13053 | ||
2c809f8f | 13054 | // Flatten constructor initializer into a temporary variable since |
13055 | // we need to take its address for __go_construct_map. | |
13056 | ||
13057 | Expression* | |
13058 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
13059 | Statement_inserter* inserter) | |
13060 | { | |
13061 | if (!this->is_error_expression() | |
13062 | && this->vals_ != NULL | |
13063 | && !this->vals_->empty() | |
13064 | && this->constructor_temp_ == NULL) | |
13065 | { | |
13066 | Map_type* mt = this->type_->map_type(); | |
13067 | Type* key_type = mt->key_type(); | |
13068 | Type* val_type = mt->val_type(); | |
13069 | this->element_type_ = Type::make_builtin_struct_type(2, | |
13070 | "__key", key_type, | |
13071 | "__val", val_type); | |
13072 | ||
13073 | Expression_list* value_pairs = new Expression_list(); | |
13074 | Location loc = this->location(); | |
13075 | ||
13076 | size_t i = 0; | |
13077 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13078 | pv != this->vals_->end(); | |
13079 | ++pv, ++i) | |
13080 | { | |
13081 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 13082 | Expression* key = *pv; |
5bf8be8b | 13083 | if (key->is_error_expression() || key->type()->is_error_type()) |
13084 | { | |
13085 | go_assert(saw_errors()); | |
13086 | return Expression::make_error(loc); | |
13087 | } | |
91c0fd76 | 13088 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
13089 | { | |
13090 | Temporary_statement* temp = | |
13091 | Statement::make_temporary(NULL, key, loc); | |
13092 | inserter->insert(temp); | |
13093 | key = Expression::make_temporary_reference(temp, loc); | |
13094 | } | |
13095 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 13096 | |
13097 | ++pv; | |
91c0fd76 | 13098 | Expression* val = *pv; |
5bf8be8b | 13099 | if (val->is_error_expression() || val->type()->is_error_type()) |
13100 | { | |
13101 | go_assert(saw_errors()); | |
13102 | return Expression::make_error(loc); | |
13103 | } | |
91c0fd76 | 13104 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
13105 | { | |
13106 | Temporary_statement* temp = | |
13107 | Statement::make_temporary(NULL, val, loc); | |
13108 | inserter->insert(temp); | |
13109 | val = Expression::make_temporary_reference(temp, loc); | |
13110 | } | |
13111 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 13112 | |
13113 | key_value_pair->push_back(key); | |
13114 | key_value_pair->push_back(val); | |
13115 | value_pairs->push_back( | |
13116 | Expression::make_struct_composite_literal(this->element_type_, | |
13117 | key_value_pair, loc)); | |
13118 | } | |
13119 | ||
e67508fa | 13120 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
2c809f8f | 13121 | Type* ctor_type = |
13122 | Type::make_array_type(this->element_type_, element_count); | |
13123 | Expression* constructor = | |
13124 | new Fixed_array_construction_expression(ctor_type, NULL, | |
13125 | value_pairs, loc); | |
13126 | ||
13127 | this->constructor_temp_ = | |
13128 | Statement::make_temporary(NULL, constructor, loc); | |
13129 | constructor->issue_nil_check(); | |
13130 | this->constructor_temp_->set_is_address_taken(); | |
13131 | inserter->insert(this->constructor_temp_); | |
13132 | } | |
13133 | ||
13134 | return this; | |
13135 | } | |
13136 | ||
e440a328 | 13137 | // Final type determination. |
13138 | ||
13139 | void | |
13140 | Map_construction_expression::do_determine_type(const Type_context*) | |
13141 | { | |
13142 | if (this->vals_ == NULL) | |
13143 | return; | |
13144 | ||
13145 | Map_type* mt = this->type_->map_type(); | |
13146 | Type_context key_context(mt->key_type(), false); | |
13147 | Type_context val_context(mt->val_type(), false); | |
13148 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13149 | pv != this->vals_->end(); | |
13150 | ++pv) | |
13151 | { | |
13152 | (*pv)->determine_type(&key_context); | |
13153 | ++pv; | |
13154 | (*pv)->determine_type(&val_context); | |
13155 | } | |
13156 | } | |
13157 | ||
13158 | // Check types. | |
13159 | ||
13160 | void | |
13161 | Map_construction_expression::do_check_types(Gogo*) | |
13162 | { | |
13163 | if (this->vals_ == NULL) | |
13164 | return; | |
13165 | ||
13166 | Map_type* mt = this->type_->map_type(); | |
13167 | int i = 0; | |
13168 | Type* key_type = mt->key_type(); | |
13169 | Type* val_type = mt->val_type(); | |
13170 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13171 | pv != this->vals_->end(); | |
13172 | ++pv, ++i) | |
13173 | { | |
13174 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
13175 | { | |
631d5788 | 13176 | go_error_at((*pv)->location(), |
13177 | "incompatible type for element %d key in map construction", | |
13178 | i + 1); | |
e440a328 | 13179 | this->set_is_error(); |
13180 | } | |
13181 | ++pv; | |
13182 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
13183 | { | |
631d5788 | 13184 | go_error_at((*pv)->location(), |
13185 | ("incompatible type for element %d value " | |
13186 | "in map construction"), | |
e440a328 | 13187 | i + 1); |
13188 | this->set_is_error(); | |
13189 | } | |
13190 | } | |
13191 | } | |
13192 | ||
ea664253 | 13193 | // Return the backend representation for constructing a map. |
e440a328 | 13194 | |
ea664253 | 13195 | Bexpression* |
13196 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13197 | { |
2c809f8f | 13198 | if (this->is_error_expression()) |
ea664253 | 13199 | return context->backend()->error_expression(); |
2c809f8f | 13200 | Location loc = this->location(); |
e440a328 | 13201 | |
e440a328 | 13202 | size_t i = 0; |
2c809f8f | 13203 | Expression* ventries; |
e440a328 | 13204 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 13205 | ventries = Expression::make_nil(loc); |
e440a328 | 13206 | else |
13207 | { | |
2c809f8f | 13208 | go_assert(this->constructor_temp_ != NULL); |
13209 | i = this->vals_->size() / 2; | |
e440a328 | 13210 | |
2c809f8f | 13211 | Expression* ctor_ref = |
13212 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
13213 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
13214 | } | |
e440a328 | 13215 | |
2c809f8f | 13216 | Map_type* mt = this->type_->map_type(); |
13217 | if (this->element_type_ == NULL) | |
13218 | this->element_type_ = | |
13219 | Type::make_builtin_struct_type(2, | |
13220 | "__key", mt->key_type(), | |
13221 | "__val", mt->val_type()); | |
0d5530d9 | 13222 | Expression* descriptor = Expression::make_type_descriptor(mt, loc); |
2c809f8f | 13223 | |
13224 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 13225 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 13226 | |
13227 | Expression* entry_size = | |
13228 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
13229 | ||
13230 | unsigned int field_index; | |
13231 | const Struct_field* valfield = | |
13232 | this->element_type_->find_local_field("__val", &field_index); | |
13233 | Expression* val_offset = | |
13234 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
2c809f8f | 13235 | |
13236 | Expression* map_ctor = | |
0d5530d9 | 13237 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, |
13238 | entry_size, val_offset, ventries); | |
ea664253 | 13239 | return map_ctor->get_backend(context); |
2c809f8f | 13240 | } |
e440a328 | 13241 | |
2c809f8f | 13242 | // Export an array construction. |
e440a328 | 13243 | |
2c809f8f | 13244 | void |
13245 | Map_construction_expression::do_export(Export* exp) const | |
13246 | { | |
13247 | exp->write_c_string("convert("); | |
13248 | exp->write_type(this->type_); | |
13249 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13250 | pv != this->vals_->end(); | |
13251 | ++pv) | |
13252 | { | |
13253 | exp->write_c_string(", "); | |
13254 | (*pv)->export_expression(exp); | |
13255 | } | |
13256 | exp->write_c_string(")"); | |
13257 | } | |
e440a328 | 13258 | |
2c809f8f | 13259 | // Dump ast representation for a map construction expression. |
d751bb78 | 13260 | |
13261 | void | |
13262 | Map_construction_expression::do_dump_expression( | |
13263 | Ast_dump_context* ast_dump_context) const | |
13264 | { | |
d751bb78 | 13265 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 13266 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13267 | ast_dump_context->ostream() << "}"; |
13268 | } | |
13269 | ||
7795ac51 | 13270 | // Class Composite_literal_expression. |
e440a328 | 13271 | |
13272 | // Traversal. | |
13273 | ||
13274 | int | |
13275 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13276 | { | |
dbffccfc | 13277 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 13278 | return TRAVERSE_EXIT; |
dbffccfc | 13279 | |
13280 | // If this is a struct composite literal with keys, then the keys | |
13281 | // are field names, not expressions. We don't want to traverse them | |
13282 | // in that case. If we do, we can give an erroneous error "variable | |
13283 | // initializer refers to itself." See bug482.go in the testsuite. | |
13284 | if (this->has_keys_ && this->vals_ != NULL) | |
13285 | { | |
13286 | // The type may not be resolvable at this point. | |
13287 | Type* type = this->type_; | |
a01f2481 | 13288 | |
7795ac51 | 13289 | for (int depth = 0; depth < this->depth_; ++depth) |
a01f2481 | 13290 | { |
13291 | if (type->array_type() != NULL) | |
13292 | type = type->array_type()->element_type(); | |
13293 | else if (type->map_type() != NULL) | |
7795ac51 | 13294 | { |
13295 | if (this->key_path_[depth]) | |
13296 | type = type->map_type()->key_type(); | |
13297 | else | |
13298 | type = type->map_type()->val_type(); | |
13299 | } | |
a01f2481 | 13300 | else |
13301 | { | |
13302 | // This error will be reported during lowering. | |
13303 | return TRAVERSE_CONTINUE; | |
13304 | } | |
13305 | } | |
13306 | ||
dbffccfc | 13307 | while (true) |
13308 | { | |
13309 | if (type->classification() == Type::TYPE_NAMED) | |
13310 | type = type->named_type()->real_type(); | |
13311 | else if (type->classification() == Type::TYPE_FORWARD) | |
13312 | { | |
13313 | Type* t = type->forwarded(); | |
13314 | if (t == type) | |
13315 | break; | |
13316 | type = t; | |
13317 | } | |
13318 | else | |
13319 | break; | |
13320 | } | |
13321 | ||
13322 | if (type->classification() == Type::TYPE_STRUCT) | |
13323 | { | |
13324 | Expression_list::iterator p = this->vals_->begin(); | |
13325 | while (p != this->vals_->end()) | |
13326 | { | |
13327 | // Skip key. | |
13328 | ++p; | |
13329 | go_assert(p != this->vals_->end()); | |
13330 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13331 | return TRAVERSE_EXIT; | |
13332 | ++p; | |
13333 | } | |
13334 | return TRAVERSE_CONTINUE; | |
13335 | } | |
13336 | } | |
13337 | ||
13338 | if (this->vals_ != NULL) | |
13339 | return this->vals_->traverse(traverse); | |
13340 | ||
13341 | return TRAVERSE_CONTINUE; | |
e440a328 | 13342 | } |
13343 | ||
13344 | // Lower a generic composite literal into a specific version based on | |
13345 | // the type. | |
13346 | ||
13347 | Expression* | |
ceeb4318 | 13348 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13349 | Statement_inserter* inserter, int) | |
e440a328 | 13350 | { |
13351 | Type* type = this->type_; | |
13352 | ||
7795ac51 | 13353 | for (int depth = 0; depth < this->depth_; ++depth) |
e440a328 | 13354 | { |
13355 | if (type->array_type() != NULL) | |
13356 | type = type->array_type()->element_type(); | |
13357 | else if (type->map_type() != NULL) | |
7795ac51 | 13358 | { |
13359 | if (this->key_path_[depth]) | |
13360 | type = type->map_type()->key_type(); | |
13361 | else | |
13362 | type = type->map_type()->val_type(); | |
13363 | } | |
e440a328 | 13364 | else |
13365 | { | |
5c13bd80 | 13366 | if (!type->is_error()) |
631d5788 | 13367 | go_error_at(this->location(), |
13368 | ("may only omit types within composite literals " | |
13369 | "of slice, array, or map type")); | |
e440a328 | 13370 | return Expression::make_error(this->location()); |
13371 | } | |
13372 | } | |
13373 | ||
e00772b3 | 13374 | Type *pt = type->points_to(); |
13375 | bool is_pointer = false; | |
13376 | if (pt != NULL) | |
13377 | { | |
13378 | is_pointer = true; | |
13379 | type = pt; | |
13380 | } | |
13381 | ||
13382 | Expression* ret; | |
5c13bd80 | 13383 | if (type->is_error()) |
e440a328 | 13384 | return Expression::make_error(this->location()); |
13385 | else if (type->struct_type() != NULL) | |
e00772b3 | 13386 | ret = this->lower_struct(gogo, type); |
e440a328 | 13387 | else if (type->array_type() != NULL) |
113ef6a5 | 13388 | ret = this->lower_array(type); |
e440a328 | 13389 | else if (type->map_type() != NULL) |
e00772b3 | 13390 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13391 | else |
13392 | { | |
631d5788 | 13393 | go_error_at(this->location(), |
13394 | ("expected struct, slice, array, or map type " | |
13395 | "for composite literal")); | |
e440a328 | 13396 | return Expression::make_error(this->location()); |
13397 | } | |
e00772b3 | 13398 | |
13399 | if (is_pointer) | |
2c809f8f | 13400 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13401 | |
13402 | return ret; | |
e440a328 | 13403 | } |
13404 | ||
13405 | // Lower a struct composite literal. | |
13406 | ||
13407 | Expression* | |
81c4b26b | 13408 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13409 | { |
b13c66cd | 13410 | Location location = this->location(); |
e440a328 | 13411 | Struct_type* st = type->struct_type(); |
13412 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13413 | { |
e6013c28 | 13414 | if (this->vals_ != NULL |
13415 | && !this->vals_->empty() | |
13416 | && type->named_type() != NULL | |
13417 | && type->named_type()->named_object()->package() != NULL) | |
13418 | { | |
13419 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13420 | pf != st->fields()->end(); | |
13421 | ++pf) | |
07daa4e7 | 13422 | { |
07ba7f26 | 13423 | if (Gogo::is_hidden_name(pf->field_name()) |
13424 | || pf->is_embedded_builtin(gogo)) | |
631d5788 | 13425 | go_error_at(this->location(), |
13426 | "assignment of unexported field %qs in %qs literal", | |
13427 | Gogo::message_name(pf->field_name()).c_str(), | |
13428 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13429 | } |
13430 | } | |
13431 | ||
13432 | return new Struct_construction_expression(type, this->vals_, location); | |
13433 | } | |
e440a328 | 13434 | |
13435 | size_t field_count = st->field_count(); | |
13436 | std::vector<Expression*> vals(field_count); | |
e32de7ba | 13437 | std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>); |
e440a328 | 13438 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13439 | Expression* external_expr = NULL; |
13440 | const Named_object* external_no = NULL; | |
e440a328 | 13441 | while (p != this->vals_->end()) |
13442 | { | |
13443 | Expression* name_expr = *p; | |
13444 | ||
13445 | ++p; | |
c484d925 | 13446 | go_assert(p != this->vals_->end()); |
e440a328 | 13447 | Expression* val = *p; |
13448 | ||
13449 | ++p; | |
13450 | ||
13451 | if (name_expr == NULL) | |
13452 | { | |
631d5788 | 13453 | go_error_at(val->location(), |
13454 | "mixture of field and value initializers"); | |
e440a328 | 13455 | return Expression::make_error(location); |
13456 | } | |
13457 | ||
13458 | bool bad_key = false; | |
13459 | std::string name; | |
81c4b26b | 13460 | const Named_object* no = NULL; |
e440a328 | 13461 | switch (name_expr->classification()) |
13462 | { | |
13463 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13464 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 13465 | if (type->named_type() != NULL) |
13466 | { | |
13467 | // If the named object found for this field name comes from a | |
13468 | // different package than the struct it is a part of, do not count | |
13469 | // this incorrect lookup as a usage of the object's package. | |
13470 | no = name_expr->unknown_expression()->named_object(); | |
13471 | if (no->package() != NULL | |
13472 | && no->package() != type->named_type()->named_object()->package()) | |
13473 | no->package()->forget_usage(name_expr); | |
13474 | } | |
e440a328 | 13475 | break; |
13476 | ||
13477 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13478 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13479 | break; |
13480 | ||
13481 | case EXPRESSION_TYPE: | |
13482 | { | |
13483 | Type* t = name_expr->type(); | |
13484 | Named_type* nt = t->named_type(); | |
13485 | if (nt == NULL) | |
13486 | bad_key = true; | |
13487 | else | |
81c4b26b | 13488 | no = nt->named_object(); |
e440a328 | 13489 | } |
13490 | break; | |
13491 | ||
13492 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13493 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13494 | break; |
13495 | ||
b0c09712 | 13496 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
13497 | no = name_expr->enclosed_var_expression()->variable(); | |
e440a328 | 13498 | break; |
13499 | ||
b0c09712 | 13500 | case EXPRESSION_FUNC_REFERENCE: |
13501 | no = name_expr->func_expression()->named_object(); | |
e440a328 | 13502 | break; |
13503 | ||
13504 | default: | |
13505 | bad_key = true; | |
13506 | break; | |
13507 | } | |
13508 | if (bad_key) | |
13509 | { | |
631d5788 | 13510 | go_error_at(name_expr->location(), "expected struct field name"); |
e440a328 | 13511 | return Expression::make_error(location); |
13512 | } | |
13513 | ||
81c4b26b | 13514 | if (no != NULL) |
13515 | { | |
62750cd5 | 13516 | if (no->package() != NULL && external_expr == NULL) |
13517 | { | |
13518 | external_expr = name_expr; | |
13519 | external_no = no; | |
13520 | } | |
13521 | ||
81c4b26b | 13522 | name = no->name(); |
13523 | ||
13524 | // A predefined name won't be packed. If it starts with a | |
13525 | // lower case letter we need to check for that case, because | |
2d29d278 | 13526 | // the field name will be packed. FIXME. |
81c4b26b | 13527 | if (!Gogo::is_hidden_name(name) |
13528 | && name[0] >= 'a' | |
13529 | && name[0] <= 'z') | |
13530 | { | |
13531 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13532 | if (gno == no) | |
13533 | name = gogo->pack_hidden_name(name, false); | |
13534 | } | |
13535 | } | |
13536 | ||
e440a328 | 13537 | unsigned int index; |
13538 | const Struct_field* sf = st->find_local_field(name, &index); | |
13539 | if (sf == NULL) | |
13540 | { | |
631d5788 | 13541 | go_error_at(name_expr->location(), "unknown field %qs in %qs", |
13542 | Gogo::message_name(name).c_str(), | |
13543 | (type->named_type() != NULL | |
13544 | ? type->named_type()->message_name().c_str() | |
13545 | : "unnamed struct")); | |
e440a328 | 13546 | return Expression::make_error(location); |
13547 | } | |
13548 | if (vals[index] != NULL) | |
13549 | { | |
631d5788 | 13550 | go_error_at(name_expr->location(), |
13551 | "duplicate value for field %qs in %qs", | |
13552 | Gogo::message_name(name).c_str(), | |
13553 | (type->named_type() != NULL | |
13554 | ? type->named_type()->message_name().c_str() | |
13555 | : "unnamed struct")); | |
e440a328 | 13556 | return Expression::make_error(location); |
13557 | } | |
13558 | ||
07daa4e7 | 13559 | if (type->named_type() != NULL |
13560 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 13561 | && (Gogo::is_hidden_name(sf->field_name()) |
13562 | || sf->is_embedded_builtin(gogo))) | |
631d5788 | 13563 | go_error_at(name_expr->location(), |
13564 | "assignment of unexported field %qs in %qs literal", | |
13565 | Gogo::message_name(sf->field_name()).c_str(), | |
13566 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13567 | |
e440a328 | 13568 | vals[index] = val; |
e32de7ba | 13569 | traverse_order->push_back(static_cast<unsigned long>(index)); |
e440a328 | 13570 | } |
13571 | ||
62750cd5 | 13572 | if (!this->all_are_names_) |
13573 | { | |
13574 | // This is a weird case like bug462 in the testsuite. | |
13575 | if (external_expr == NULL) | |
631d5788 | 13576 | go_error_at(this->location(), "unknown field in %qs literal", |
13577 | (type->named_type() != NULL | |
13578 | ? type->named_type()->message_name().c_str() | |
13579 | : "unnamed struct")); | |
62750cd5 | 13580 | else |
631d5788 | 13581 | go_error_at(external_expr->location(), "unknown field %qs in %qs", |
13582 | external_no->message_name().c_str(), | |
13583 | (type->named_type() != NULL | |
13584 | ? type->named_type()->message_name().c_str() | |
13585 | : "unnamed struct")); | |
62750cd5 | 13586 | return Expression::make_error(location); |
13587 | } | |
13588 | ||
e440a328 | 13589 | Expression_list* list = new Expression_list; |
13590 | list->reserve(field_count); | |
13591 | for (size_t i = 0; i < field_count; ++i) | |
13592 | list->push_back(vals[i]); | |
13593 | ||
0c4f5a19 | 13594 | Struct_construction_expression* ret = |
13595 | new Struct_construction_expression(type, list, location); | |
13596 | ret->set_traverse_order(traverse_order); | |
13597 | return ret; | |
e440a328 | 13598 | } |
13599 | ||
e32de7ba | 13600 | // Index/value/traversal-order triple. |
00773463 | 13601 | |
e32de7ba | 13602 | struct IVT_triple { |
13603 | unsigned long index; | |
13604 | unsigned long traversal_order; | |
13605 | Expression* expr; | |
13606 | IVT_triple(unsigned long i, unsigned long to, Expression *e) | |
13607 | : index(i), traversal_order(to), expr(e) { } | |
13608 | bool operator<(const IVT_triple& other) const | |
13609 | { return this->index < other.index; } | |
00773463 | 13610 | }; |
13611 | ||
e440a328 | 13612 | // Lower an array composite literal. |
13613 | ||
13614 | Expression* | |
113ef6a5 | 13615 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13616 | { |
b13c66cd | 13617 | Location location = this->location(); |
e440a328 | 13618 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13619 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13620 | |
ffe743ca | 13621 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13622 | indexes->reserve(this->vals_->size()); | |
00773463 | 13623 | bool indexes_out_of_order = false; |
ffe743ca | 13624 | Expression_list* vals = new Expression_list(); |
13625 | vals->reserve(this->vals_->size()); | |
e440a328 | 13626 | unsigned long index = 0; |
13627 | Expression_list::const_iterator p = this->vals_->begin(); | |
13628 | while (p != this->vals_->end()) | |
13629 | { | |
13630 | Expression* index_expr = *p; | |
13631 | ||
13632 | ++p; | |
c484d925 | 13633 | go_assert(p != this->vals_->end()); |
e440a328 | 13634 | Expression* val = *p; |
13635 | ||
13636 | ++p; | |
13637 | ||
ffe743ca | 13638 | if (index_expr == NULL) |
13639 | { | |
13640 | if (!indexes->empty()) | |
13641 | indexes->push_back(index); | |
13642 | } | |
13643 | else | |
e440a328 | 13644 | { |
ffe743ca | 13645 | if (indexes->empty() && !vals->empty()) |
13646 | { | |
13647 | for (size_t i = 0; i < vals->size(); ++i) | |
13648 | indexes->push_back(i); | |
13649 | } | |
13650 | ||
0c77715b | 13651 | Numeric_constant nc; |
13652 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13653 | { |
631d5788 | 13654 | go_error_at(index_expr->location(), |
13655 | "index expression is not integer constant"); | |
e440a328 | 13656 | return Expression::make_error(location); |
13657 | } | |
6f6d9955 | 13658 | |
0c77715b | 13659 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13660 | { |
0c77715b | 13661 | case Numeric_constant::NC_UL_VALID: |
13662 | break; | |
13663 | case Numeric_constant::NC_UL_NOTINT: | |
631d5788 | 13664 | go_error_at(index_expr->location(), |
13665 | "index expression is not integer constant"); | |
0c77715b | 13666 | return Expression::make_error(location); |
13667 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 13668 | go_error_at(index_expr->location(), |
13669 | "index expression is negative"); | |
e440a328 | 13670 | return Expression::make_error(location); |
0c77715b | 13671 | case Numeric_constant::NC_UL_BIG: |
631d5788 | 13672 | go_error_at(index_expr->location(), "index value overflow"); |
e440a328 | 13673 | return Expression::make_error(location); |
0c77715b | 13674 | default: |
13675 | go_unreachable(); | |
e440a328 | 13676 | } |
6f6d9955 | 13677 | |
13678 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13679 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13680 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13681 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13682 | { |
631d5788 | 13683 | go_error_at(index_expr->location(), "index value overflow"); |
6f6d9955 | 13684 | return Expression::make_error(location); |
13685 | } | |
13686 | ||
ffe743ca | 13687 | if (std::find(indexes->begin(), indexes->end(), index) |
13688 | != indexes->end()) | |
e440a328 | 13689 | { |
631d5788 | 13690 | go_error_at(index_expr->location(), |
13691 | "duplicate value for index %lu", | |
13692 | index); | |
e440a328 | 13693 | return Expression::make_error(location); |
13694 | } | |
ffe743ca | 13695 | |
00773463 | 13696 | if (!indexes->empty() && index < indexes->back()) |
13697 | indexes_out_of_order = true; | |
13698 | ||
ffe743ca | 13699 | indexes->push_back(index); |
e440a328 | 13700 | } |
13701 | ||
ffe743ca | 13702 | vals->push_back(val); |
13703 | ||
e440a328 | 13704 | ++index; |
13705 | } | |
13706 | ||
ffe743ca | 13707 | if (indexes->empty()) |
13708 | { | |
13709 | delete indexes; | |
13710 | indexes = NULL; | |
13711 | } | |
e440a328 | 13712 | |
e32de7ba | 13713 | std::vector<unsigned long>* traverse_order = NULL; |
00773463 | 13714 | if (indexes_out_of_order) |
13715 | { | |
e32de7ba | 13716 | typedef std::vector<IVT_triple> V; |
00773463 | 13717 | |
13718 | V v; | |
13719 | v.reserve(indexes->size()); | |
13720 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
e32de7ba | 13721 | unsigned long torder = 0; |
00773463 | 13722 | for (Expression_list::const_iterator pe = vals->begin(); |
13723 | pe != vals->end(); | |
e32de7ba | 13724 | ++pe, ++pi, ++torder) |
13725 | v.push_back(IVT_triple(*pi, torder, *pe)); | |
00773463 | 13726 | |
e32de7ba | 13727 | std::sort(v.begin(), v.end()); |
00773463 | 13728 | |
13729 | delete indexes; | |
13730 | delete vals; | |
e32de7ba | 13731 | |
00773463 | 13732 | indexes = new std::vector<unsigned long>(); |
13733 | indexes->reserve(v.size()); | |
13734 | vals = new Expression_list(); | |
13735 | vals->reserve(v.size()); | |
e32de7ba | 13736 | traverse_order = new std::vector<unsigned long>(); |
13737 | traverse_order->reserve(v.size()); | |
00773463 | 13738 | |
13739 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13740 | { | |
e32de7ba | 13741 | indexes->push_back(p->index); |
13742 | vals->push_back(p->expr); | |
13743 | traverse_order->push_back(p->traversal_order); | |
00773463 | 13744 | } |
13745 | } | |
13746 | ||
e32de7ba | 13747 | Expression* ret = this->make_array(type, indexes, vals); |
13748 | Array_construction_expression* ace = ret->array_literal(); | |
13749 | if (ace != NULL && traverse_order != NULL) | |
13750 | ace->set_traverse_order(traverse_order); | |
13751 | return ret; | |
e440a328 | 13752 | } |
13753 | ||
13754 | // Actually build the array composite literal. This handles | |
13755 | // [...]{...}. | |
13756 | ||
13757 | Expression* | |
ffe743ca | 13758 | Composite_literal_expression::make_array( |
13759 | Type* type, | |
13760 | const std::vector<unsigned long>* indexes, | |
13761 | Expression_list* vals) | |
e440a328 | 13762 | { |
b13c66cd | 13763 | Location location = this->location(); |
e440a328 | 13764 | Array_type* at = type->array_type(); |
ffe743ca | 13765 | |
e440a328 | 13766 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13767 | { | |
ffe743ca | 13768 | size_t size; |
13769 | if (vals == NULL) | |
13770 | size = 0; | |
00773463 | 13771 | else if (indexes != NULL) |
13772 | size = indexes->back() + 1; | |
13773 | else | |
ffe743ca | 13774 | { |
13775 | size = vals->size(); | |
13776 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13777 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13778 | && size >> (it->bits() - 1) != 0) | |
13779 | { | |
631d5788 | 13780 | go_error_at(location, "too many elements in composite literal"); |
ffe743ca | 13781 | return Expression::make_error(location); |
13782 | } | |
13783 | } | |
ffe743ca | 13784 | |
e67508fa | 13785 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 13786 | at = Type::make_array_type(at->element_type(), elen); |
13787 | type = at; | |
13788 | } | |
ffe743ca | 13789 | else if (at->length() != NULL |
13790 | && !at->length()->is_error_expression() | |
13791 | && this->vals_ != NULL) | |
13792 | { | |
13793 | Numeric_constant nc; | |
13794 | unsigned long val; | |
13795 | if (at->length()->numeric_constant_value(&nc) | |
13796 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13797 | { | |
13798 | if (indexes == NULL) | |
13799 | { | |
13800 | if (this->vals_->size() > val) | |
13801 | { | |
631d5788 | 13802 | go_error_at(location, |
13803 | "too many elements in composite literal"); | |
ffe743ca | 13804 | return Expression::make_error(location); |
13805 | } | |
13806 | } | |
13807 | else | |
13808 | { | |
00773463 | 13809 | unsigned long max = indexes->back(); |
ffe743ca | 13810 | if (max >= val) |
13811 | { | |
631d5788 | 13812 | go_error_at(location, |
13813 | ("some element keys in composite literal " | |
13814 | "are out of range")); | |
ffe743ca | 13815 | return Expression::make_error(location); |
13816 | } | |
13817 | } | |
13818 | } | |
13819 | } | |
13820 | ||
e440a328 | 13821 | if (at->length() != NULL) |
ffe743ca | 13822 | return new Fixed_array_construction_expression(type, indexes, vals, |
13823 | location); | |
e440a328 | 13824 | else |
2c809f8f | 13825 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13826 | } |
13827 | ||
13828 | // Lower a map composite literal. | |
13829 | ||
13830 | Expression* | |
a287720d | 13831 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13832 | Statement_inserter* inserter, |
a287720d | 13833 | Type* type) |
e440a328 | 13834 | { |
b13c66cd | 13835 | Location location = this->location(); |
e440a328 | 13836 | if (this->vals_ != NULL) |
13837 | { | |
13838 | if (!this->has_keys_) | |
13839 | { | |
631d5788 | 13840 | go_error_at(location, "map composite literal must have keys"); |
e440a328 | 13841 | return Expression::make_error(location); |
13842 | } | |
13843 | ||
a287720d | 13844 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13845 | p != this->vals_->end(); |
13846 | p += 2) | |
13847 | { | |
13848 | if (*p == NULL) | |
13849 | { | |
13850 | ++p; | |
631d5788 | 13851 | go_error_at((*p)->location(), |
13852 | ("map composite literal must " | |
13853 | "have keys for every value")); | |
e440a328 | 13854 | return Expression::make_error(location); |
13855 | } | |
a287720d | 13856 | // Make sure we have lowered the key; it may not have been |
13857 | // lowered in order to handle keys for struct composite | |
13858 | // literals. Lower it now to get the right error message. | |
13859 | if ((*p)->unknown_expression() != NULL) | |
13860 | { | |
13861 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13862 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 13863 | go_assert((*p)->is_error_expression()); |
a287720d | 13864 | return Expression::make_error(location); |
13865 | } | |
e440a328 | 13866 | } |
13867 | } | |
13868 | ||
13869 | return new Map_construction_expression(type, this->vals_, location); | |
13870 | } | |
13871 | ||
d751bb78 | 13872 | // Dump ast representation for a composite literal expression. |
13873 | ||
13874 | void | |
13875 | Composite_literal_expression::do_dump_expression( | |
13876 | Ast_dump_context* ast_dump_context) const | |
13877 | { | |
8b1c301d | 13878 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13879 | ast_dump_context->dump_type(this->type_); |
13880 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13881 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13882 | ast_dump_context->ostream() << "})"; |
13883 | } | |
13884 | ||
e440a328 | 13885 | // Make a composite literal expression. |
13886 | ||
13887 | Expression* | |
13888 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 13889 | Expression_list* vals, bool all_are_names, |
b13c66cd | 13890 | Location location) |
e440a328 | 13891 | { |
13892 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 13893 | all_are_names, location); |
e440a328 | 13894 | } |
13895 | ||
13896 | // Return whether this expression is a composite literal. | |
13897 | ||
13898 | bool | |
13899 | Expression::is_composite_literal() const | |
13900 | { | |
13901 | switch (this->classification_) | |
13902 | { | |
13903 | case EXPRESSION_COMPOSITE_LITERAL: | |
13904 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13905 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 13906 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13907 | case EXPRESSION_MAP_CONSTRUCTION: |
13908 | return true; | |
13909 | default: | |
13910 | return false; | |
13911 | } | |
13912 | } | |
13913 | ||
13914 | // Return whether this expression is a composite literal which is not | |
13915 | // constant. | |
13916 | ||
13917 | bool | |
13918 | Expression::is_nonconstant_composite_literal() const | |
13919 | { | |
13920 | switch (this->classification_) | |
13921 | { | |
13922 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13923 | { | |
13924 | const Struct_construction_expression *psce = | |
13925 | static_cast<const Struct_construction_expression*>(this); | |
13926 | return !psce->is_constant_struct(); | |
13927 | } | |
13928 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13929 | { | |
13930 | const Fixed_array_construction_expression *pace = | |
13931 | static_cast<const Fixed_array_construction_expression*>(this); | |
13932 | return !pace->is_constant_array(); | |
13933 | } | |
2c809f8f | 13934 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13935 | { |
2c809f8f | 13936 | const Slice_construction_expression *pace = |
13937 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 13938 | return !pace->is_constant_array(); |
13939 | } | |
13940 | case EXPRESSION_MAP_CONSTRUCTION: | |
13941 | return true; | |
13942 | default: | |
13943 | return false; | |
13944 | } | |
13945 | } | |
13946 | ||
35a54f17 | 13947 | // Return true if this is a variable or temporary_variable. |
13948 | ||
13949 | bool | |
13950 | Expression::is_variable() const | |
13951 | { | |
13952 | switch (this->classification_) | |
13953 | { | |
13954 | case EXPRESSION_VAR_REFERENCE: | |
13955 | case EXPRESSION_TEMPORARY_REFERENCE: | |
13956 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
b0c09712 | 13957 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
35a54f17 | 13958 | return true; |
13959 | default: | |
13960 | return false; | |
13961 | } | |
13962 | } | |
13963 | ||
e440a328 | 13964 | // Return true if this is a reference to a local variable. |
13965 | ||
13966 | bool | |
13967 | Expression::is_local_variable() const | |
13968 | { | |
13969 | const Var_expression* ve = this->var_expression(); | |
13970 | if (ve == NULL) | |
13971 | return false; | |
13972 | const Named_object* no = ve->named_object(); | |
13973 | return (no->is_result_variable() | |
13974 | || (no->is_variable() && !no->var_value()->is_global())); | |
13975 | } | |
13976 | ||
13977 | // Class Type_guard_expression. | |
13978 | ||
13979 | // Traversal. | |
13980 | ||
13981 | int | |
13982 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13983 | { | |
13984 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13985 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13986 | return TRAVERSE_EXIT; | |
13987 | return TRAVERSE_CONTINUE; | |
13988 | } | |
13989 | ||
2c809f8f | 13990 | Expression* |
13991 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
13992 | Statement_inserter* inserter) | |
13993 | { | |
5bf8be8b | 13994 | if (this->expr_->is_error_expression() |
13995 | || this->expr_->type()->is_error_type()) | |
13996 | { | |
13997 | go_assert(saw_errors()); | |
13998 | return Expression::make_error(this->location()); | |
13999 | } | |
14000 | ||
2c809f8f | 14001 | if (!this->expr_->is_variable()) |
14002 | { | |
14003 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
14004 | this->location()); | |
14005 | inserter->insert(temp); | |
14006 | this->expr_ = | |
14007 | Expression::make_temporary_reference(temp, this->location()); | |
14008 | } | |
14009 | return this; | |
14010 | } | |
14011 | ||
e440a328 | 14012 | // Check types of a type guard expression. The expression must have |
14013 | // an interface type, but the actual type conversion is checked at run | |
14014 | // time. | |
14015 | ||
14016 | void | |
14017 | Type_guard_expression::do_check_types(Gogo*) | |
14018 | { | |
e440a328 | 14019 | Type* expr_type = this->expr_->type(); |
7e9da23f | 14020 | if (expr_type->interface_type() == NULL) |
f725ade8 | 14021 | { |
5c13bd80 | 14022 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 14023 | this->report_error(_("type assertion only valid for interface types")); |
14024 | this->set_is_error(); | |
14025 | } | |
e440a328 | 14026 | else if (this->type_->interface_type() == NULL) |
14027 | { | |
14028 | std::string reason; | |
14029 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
14030 | &reason)) | |
14031 | { | |
5c13bd80 | 14032 | if (!this->type_->is_error()) |
e440a328 | 14033 | { |
f725ade8 | 14034 | if (reason.empty()) |
14035 | this->report_error(_("impossible type assertion: " | |
14036 | "type does not implement interface")); | |
14037 | else | |
631d5788 | 14038 | go_error_at(this->location(), |
14039 | ("impossible type assertion: " | |
14040 | "type does not implement interface (%s)"), | |
14041 | reason.c_str()); | |
e440a328 | 14042 | } |
f725ade8 | 14043 | this->set_is_error(); |
e440a328 | 14044 | } |
14045 | } | |
14046 | } | |
14047 | ||
ea664253 | 14048 | // Return the backend representation for a type guard expression. |
e440a328 | 14049 | |
ea664253 | 14050 | Bexpression* |
14051 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14052 | { |
2c809f8f | 14053 | Expression* conversion; |
7e9da23f | 14054 | if (this->type_->interface_type() != NULL) |
2c809f8f | 14055 | conversion = |
14056 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
14057 | true, this->location()); | |
e440a328 | 14058 | else |
2c809f8f | 14059 | conversion = |
14060 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
14061 | this->expr_, this->location()); | |
14062 | ||
ea664253 | 14063 | return conversion->get_backend(context); |
e440a328 | 14064 | } |
14065 | ||
d751bb78 | 14066 | // Dump ast representation for a type guard expression. |
14067 | ||
14068 | void | |
2c809f8f | 14069 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 14070 | const |
14071 | { | |
14072 | this->expr_->dump_expression(ast_dump_context); | |
14073 | ast_dump_context->ostream() << "."; | |
14074 | ast_dump_context->dump_type(this->type_); | |
14075 | } | |
14076 | ||
e440a328 | 14077 | // Make a type guard expression. |
14078 | ||
14079 | Expression* | |
14080 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 14081 | Location location) |
e440a328 | 14082 | { |
14083 | return new Type_guard_expression(expr, type, location); | |
14084 | } | |
14085 | ||
2c809f8f | 14086 | // Class Heap_expression. |
e440a328 | 14087 | |
da244e59 | 14088 | // Return the type of the expression stored on the heap. |
e440a328 | 14089 | |
da244e59 | 14090 | Type* |
14091 | Heap_expression::do_type() | |
14092 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 14093 | |
ea664253 | 14094 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 14095 | |
ea664253 | 14096 | Bexpression* |
14097 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14098 | { |
02c19a1a | 14099 | if (this->expr_->is_error_expression() || this->expr_->type()->is_error()) |
ea664253 | 14100 | return context->backend()->error_expression(); |
2c809f8f | 14101 | |
02c19a1a | 14102 | Location loc = this->location(); |
2c809f8f | 14103 | Gogo* gogo = context->gogo(); |
02c19a1a | 14104 | Btype* btype = this->type()->get_backend(gogo); |
45ff893b | 14105 | |
14106 | Expression* alloc = Expression::make_allocation(this->expr_->type(), loc); | |
14107 | Node* n = Node::make_node(this); | |
14108 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
14109 | alloc->allocation_expression()->set_allocate_on_stack(); | |
14110 | Bexpression* space = alloc->get_backend(context); | |
02c19a1a | 14111 | |
14112 | Bstatement* decl; | |
14113 | Named_object* fn = context->function(); | |
14114 | go_assert(fn != NULL); | |
14115 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
14116 | Bvariable* space_temp = | |
14117 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
14118 | space, true, loc, &decl); | |
d4e6573e | 14119 | space = gogo->backend()->var_expression(space_temp, VE_lvalue, loc); |
9b27b43c | 14120 | Btype* expr_btype = this->expr_->type()->get_backend(gogo); |
14121 | Bexpression* ref = | |
14122 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
02c19a1a | 14123 | |
ea664253 | 14124 | Bexpression* bexpr = this->expr_->get_backend(context); |
02c19a1a | 14125 | Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc); |
14126 | decl = gogo->backend()->compound_statement(decl, assn); | |
d4e6573e | 14127 | space = gogo->backend()->var_expression(space_temp, VE_rvalue, loc); |
ea664253 | 14128 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 14129 | } |
14130 | ||
2c809f8f | 14131 | // Dump ast representation for a heap expression. |
d751bb78 | 14132 | |
14133 | void | |
2c809f8f | 14134 | Heap_expression::do_dump_expression( |
d751bb78 | 14135 | Ast_dump_context* ast_dump_context) const |
14136 | { | |
14137 | ast_dump_context->ostream() << "&("; | |
14138 | ast_dump_context->dump_expression(this->expr_); | |
14139 | ast_dump_context->ostream() << ")"; | |
14140 | } | |
14141 | ||
2c809f8f | 14142 | // Allocate an expression on the heap. |
e440a328 | 14143 | |
14144 | Expression* | |
2c809f8f | 14145 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 14146 | { |
2c809f8f | 14147 | return new Heap_expression(expr, location); |
e440a328 | 14148 | } |
14149 | ||
14150 | // Class Receive_expression. | |
14151 | ||
14152 | // Return the type of a receive expression. | |
14153 | ||
14154 | Type* | |
14155 | Receive_expression::do_type() | |
14156 | { | |
e429e3bd | 14157 | if (this->is_error_expression()) |
14158 | return Type::make_error_type(); | |
e440a328 | 14159 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
14160 | if (channel_type == NULL) | |
e429e3bd | 14161 | { |
14162 | this->report_error(_("expected channel")); | |
14163 | return Type::make_error_type(); | |
14164 | } | |
e440a328 | 14165 | return channel_type->element_type(); |
14166 | } | |
14167 | ||
14168 | // Check types for a receive expression. | |
14169 | ||
14170 | void | |
14171 | Receive_expression::do_check_types(Gogo*) | |
14172 | { | |
14173 | Type* type = this->channel_->type(); | |
5c13bd80 | 14174 | if (type->is_error()) |
e440a328 | 14175 | { |
e429e3bd | 14176 | go_assert(saw_errors()); |
e440a328 | 14177 | this->set_is_error(); |
14178 | return; | |
14179 | } | |
14180 | if (type->channel_type() == NULL) | |
14181 | { | |
14182 | this->report_error(_("expected channel")); | |
14183 | return; | |
14184 | } | |
14185 | if (!type->channel_type()->may_receive()) | |
14186 | { | |
14187 | this->report_error(_("invalid receive on send-only channel")); | |
14188 | return; | |
14189 | } | |
14190 | } | |
14191 | ||
2c809f8f | 14192 | // Flattening for receive expressions creates a temporary variable to store |
14193 | // received data in for receives. | |
14194 | ||
14195 | Expression* | |
14196 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
14197 | Statement_inserter* inserter) | |
14198 | { | |
14199 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
14200 | if (channel_type == NULL) | |
14201 | { | |
14202 | go_assert(saw_errors()); | |
14203 | return this; | |
14204 | } | |
5bf8be8b | 14205 | else if (this->channel_->is_error_expression()) |
14206 | { | |
14207 | go_assert(saw_errors()); | |
14208 | return Expression::make_error(this->location()); | |
14209 | } | |
2c809f8f | 14210 | |
14211 | Type* element_type = channel_type->element_type(); | |
14212 | if (this->temp_receiver_ == NULL) | |
14213 | { | |
14214 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
14215 | this->location()); | |
14216 | this->temp_receiver_->set_is_address_taken(); | |
14217 | inserter->insert(this->temp_receiver_); | |
14218 | } | |
14219 | ||
14220 | return this; | |
14221 | } | |
14222 | ||
ea664253 | 14223 | // Get the backend representation for a receive expression. |
e440a328 | 14224 | |
ea664253 | 14225 | Bexpression* |
14226 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14227 | { |
f24f10bb | 14228 | Location loc = this->location(); |
14229 | ||
e440a328 | 14230 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 14231 | if (channel_type == NULL) |
14232 | { | |
c484d925 | 14233 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 14234 | return context->backend()->error_expression(); |
5b8368f4 | 14235 | } |
f24f10bb | 14236 | Expression* td = Expression::make_type_descriptor(channel_type, loc); |
e440a328 | 14237 | |
2c809f8f | 14238 | Expression* recv_ref = |
14239 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14240 | Expression* recv_addr = | |
14241 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14242 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
132ed071 | 14243 | Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 3, |
14244 | td, this->channel_, recv_addr); | |
ea664253 | 14245 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 14246 | } |
14247 | ||
d751bb78 | 14248 | // Dump ast representation for a receive expression. |
14249 | ||
14250 | void | |
14251 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14252 | { | |
14253 | ast_dump_context->ostream() << " <- " ; | |
14254 | ast_dump_context->dump_expression(channel_); | |
14255 | } | |
14256 | ||
e440a328 | 14257 | // Make a receive expression. |
14258 | ||
14259 | Receive_expression* | |
b13c66cd | 14260 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 14261 | { |
14262 | return new Receive_expression(channel, location); | |
14263 | } | |
14264 | ||
e440a328 | 14265 | // An expression which evaluates to a pointer to the type descriptor |
14266 | // of a type. | |
14267 | ||
14268 | class Type_descriptor_expression : public Expression | |
14269 | { | |
14270 | public: | |
b13c66cd | 14271 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 14272 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
14273 | type_(type) | |
14274 | { } | |
14275 | ||
14276 | protected: | |
4b686186 | 14277 | int |
14278 | do_traverse(Traverse*); | |
14279 | ||
e440a328 | 14280 | Type* |
14281 | do_type() | |
14282 | { return Type::make_type_descriptor_ptr_type(); } | |
14283 | ||
f9ca30f9 | 14284 | bool |
3ae06f68 | 14285 | do_is_static_initializer() const |
f9ca30f9 | 14286 | { return true; } |
14287 | ||
e440a328 | 14288 | void |
14289 | do_determine_type(const Type_context*) | |
14290 | { } | |
14291 | ||
14292 | Expression* | |
14293 | do_copy() | |
14294 | { return this; } | |
14295 | ||
ea664253 | 14296 | Bexpression* |
14297 | do_get_backend(Translate_context* context) | |
a1d23b41 | 14298 | { |
ea664253 | 14299 | return this->type_->type_descriptor_pointer(context->gogo(), |
14300 | this->location()); | |
a1d23b41 | 14301 | } |
e440a328 | 14302 | |
d751bb78 | 14303 | void |
14304 | do_dump_expression(Ast_dump_context*) const; | |
14305 | ||
e440a328 | 14306 | private: |
14307 | // The type for which this is the descriptor. | |
14308 | Type* type_; | |
14309 | }; | |
14310 | ||
4b686186 | 14311 | int |
14312 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
14313 | { | |
14314 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14315 | return TRAVERSE_EXIT; | |
14316 | return TRAVERSE_CONTINUE; | |
14317 | } | |
14318 | ||
d751bb78 | 14319 | // Dump ast representation for a type descriptor expression. |
14320 | ||
14321 | void | |
14322 | Type_descriptor_expression::do_dump_expression( | |
14323 | Ast_dump_context* ast_dump_context) const | |
14324 | { | |
14325 | ast_dump_context->dump_type(this->type_); | |
14326 | } | |
14327 | ||
e440a328 | 14328 | // Make a type descriptor expression. |
14329 | ||
14330 | Expression* | |
b13c66cd | 14331 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14332 | { |
14333 | return new Type_descriptor_expression(type, location); | |
14334 | } | |
14335 | ||
aa5ae575 | 14336 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
14337 | // of a type. | |
14338 | ||
14339 | class GC_symbol_expression : public Expression | |
14340 | { | |
14341 | public: | |
14342 | GC_symbol_expression(Type* type) | |
14343 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
14344 | type_(type) | |
14345 | {} | |
14346 | ||
14347 | protected: | |
14348 | Type* | |
14349 | do_type() | |
23d77f91 | 14350 | { return Type::lookup_integer_type("uintptr"); } |
aa5ae575 | 14351 | |
14352 | bool | |
3ae06f68 | 14353 | do_is_static_initializer() const |
aa5ae575 | 14354 | { return true; } |
14355 | ||
14356 | void | |
14357 | do_determine_type(const Type_context*) | |
14358 | { } | |
14359 | ||
14360 | Expression* | |
14361 | do_copy() | |
14362 | { return this; } | |
14363 | ||
14364 | Bexpression* | |
14365 | do_get_backend(Translate_context* context) | |
14366 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
14367 | ||
14368 | void | |
14369 | do_dump_expression(Ast_dump_context*) const; | |
14370 | ||
14371 | private: | |
14372 | // The type which this gc symbol describes. | |
14373 | Type* type_; | |
14374 | }; | |
14375 | ||
14376 | // Dump ast representation for a gc symbol expression. | |
14377 | ||
14378 | void | |
14379 | GC_symbol_expression::do_dump_expression( | |
14380 | Ast_dump_context* ast_dump_context) const | |
14381 | { | |
14382 | ast_dump_context->ostream() << "gcdata("; | |
14383 | ast_dump_context->dump_type(this->type_); | |
14384 | ast_dump_context->ostream() << ")"; | |
14385 | } | |
14386 | ||
14387 | // Make a gc symbol expression. | |
14388 | ||
14389 | Expression* | |
14390 | Expression::make_gc_symbol(Type* type) | |
14391 | { | |
14392 | return new GC_symbol_expression(type); | |
14393 | } | |
14394 | ||
e440a328 | 14395 | // An expression which evaluates to some characteristic of a type. |
14396 | // This is only used to initialize fields of a type descriptor. Using | |
14397 | // a new expression class is slightly inefficient but gives us a good | |
14398 | // separation between the frontend and the middle-end with regard to | |
14399 | // how types are laid out. | |
14400 | ||
14401 | class Type_info_expression : public Expression | |
14402 | { | |
14403 | public: | |
14404 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14405 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14406 | type_(type), type_info_(type_info) |
14407 | { } | |
14408 | ||
14409 | protected: | |
0e168074 | 14410 | bool |
3ae06f68 | 14411 | do_is_static_initializer() const |
0e168074 | 14412 | { return true; } |
14413 | ||
e440a328 | 14414 | Type* |
14415 | do_type(); | |
14416 | ||
14417 | void | |
14418 | do_determine_type(const Type_context*) | |
14419 | { } | |
14420 | ||
14421 | Expression* | |
14422 | do_copy() | |
14423 | { return this; } | |
14424 | ||
ea664253 | 14425 | Bexpression* |
14426 | do_get_backend(Translate_context* context); | |
e440a328 | 14427 | |
d751bb78 | 14428 | void |
14429 | do_dump_expression(Ast_dump_context*) const; | |
14430 | ||
e440a328 | 14431 | private: |
14432 | // The type for which we are getting information. | |
14433 | Type* type_; | |
14434 | // What information we want. | |
14435 | Type_info type_info_; | |
14436 | }; | |
14437 | ||
14438 | // The type is chosen to match what the type descriptor struct | |
14439 | // expects. | |
14440 | ||
14441 | Type* | |
14442 | Type_info_expression::do_type() | |
14443 | { | |
14444 | switch (this->type_info_) | |
14445 | { | |
14446 | case TYPE_INFO_SIZE: | |
14447 | return Type::lookup_integer_type("uintptr"); | |
14448 | case TYPE_INFO_ALIGNMENT: | |
14449 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14450 | return Type::lookup_integer_type("uint8"); | |
14451 | default: | |
c3e6f413 | 14452 | go_unreachable(); |
e440a328 | 14453 | } |
14454 | } | |
14455 | ||
ea664253 | 14456 | // Return the backend representation for type information. |
e440a328 | 14457 | |
ea664253 | 14458 | Bexpression* |
14459 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14460 | { |
927a01eb | 14461 | Gogo* gogo = context->gogo(); |
2a305b85 | 14462 | bool ok = true; |
3f378015 | 14463 | int64_t val; |
927a01eb | 14464 | switch (this->type_info_) |
e440a328 | 14465 | { |
927a01eb | 14466 | case TYPE_INFO_SIZE: |
2a305b85 | 14467 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 14468 | break; |
14469 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 14470 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 14471 | break; |
14472 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 14473 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 14474 | break; |
14475 | default: | |
14476 | go_unreachable(); | |
e440a328 | 14477 | } |
2a305b85 | 14478 | if (!ok) |
14479 | { | |
14480 | go_assert(saw_errors()); | |
14481 | return gogo->backend()->error_expression(); | |
14482 | } | |
3f378015 | 14483 | Expression* e = Expression::make_integer_int64(val, this->type(), |
14484 | this->location()); | |
14485 | return e->get_backend(context); | |
e440a328 | 14486 | } |
14487 | ||
d751bb78 | 14488 | // Dump ast representation for a type info expression. |
14489 | ||
14490 | void | |
14491 | Type_info_expression::do_dump_expression( | |
14492 | Ast_dump_context* ast_dump_context) const | |
14493 | { | |
14494 | ast_dump_context->ostream() << "typeinfo("; | |
14495 | ast_dump_context->dump_type(this->type_); | |
14496 | ast_dump_context->ostream() << ","; | |
14497 | ast_dump_context->ostream() << | |
14498 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14499 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
14500 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
14501 | : "unknown"); | |
14502 | ast_dump_context->ostream() << ")"; | |
14503 | } | |
14504 | ||
e440a328 | 14505 | // Make a type info expression. |
14506 | ||
14507 | Expression* | |
14508 | Expression::make_type_info(Type* type, Type_info type_info) | |
14509 | { | |
14510 | return new Type_info_expression(type, type_info); | |
14511 | } | |
14512 | ||
35a54f17 | 14513 | // An expression that evaluates to some characteristic of a slice. |
14514 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14515 | ||
14516 | class Slice_info_expression : public Expression | |
14517 | { | |
14518 | public: | |
14519 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14520 | Location location) | |
14521 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14522 | slice_(slice), slice_info_(slice_info) | |
14523 | { } | |
14524 | ||
14525 | protected: | |
14526 | Type* | |
14527 | do_type(); | |
14528 | ||
14529 | void | |
14530 | do_determine_type(const Type_context*) | |
14531 | { } | |
14532 | ||
14533 | Expression* | |
14534 | do_copy() | |
14535 | { | |
14536 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14537 | this->location()); | |
14538 | } | |
14539 | ||
ea664253 | 14540 | Bexpression* |
14541 | do_get_backend(Translate_context* context); | |
35a54f17 | 14542 | |
14543 | void | |
14544 | do_dump_expression(Ast_dump_context*) const; | |
14545 | ||
14546 | void | |
14547 | do_issue_nil_check() | |
14548 | { this->slice_->issue_nil_check(); } | |
14549 | ||
14550 | private: | |
14551 | // The slice for which we are getting information. | |
14552 | Expression* slice_; | |
14553 | // What information we want. | |
14554 | Slice_info slice_info_; | |
14555 | }; | |
14556 | ||
14557 | // Return the type of the slice info. | |
14558 | ||
14559 | Type* | |
14560 | Slice_info_expression::do_type() | |
14561 | { | |
14562 | switch (this->slice_info_) | |
14563 | { | |
14564 | case SLICE_INFO_VALUE_POINTER: | |
14565 | return Type::make_pointer_type( | |
14566 | this->slice_->type()->array_type()->element_type()); | |
14567 | case SLICE_INFO_LENGTH: | |
14568 | case SLICE_INFO_CAPACITY: | |
14569 | return Type::lookup_integer_type("int"); | |
14570 | default: | |
14571 | go_unreachable(); | |
14572 | } | |
14573 | } | |
14574 | ||
ea664253 | 14575 | // Return the backend information for slice information. |
35a54f17 | 14576 | |
ea664253 | 14577 | Bexpression* |
14578 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 14579 | { |
14580 | Gogo* gogo = context->gogo(); | |
ea664253 | 14581 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 14582 | switch (this->slice_info_) |
14583 | { | |
14584 | case SLICE_INFO_VALUE_POINTER: | |
14585 | case SLICE_INFO_LENGTH: | |
14586 | case SLICE_INFO_CAPACITY: | |
ea664253 | 14587 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
14588 | this->location()); | |
35a54f17 | 14589 | break; |
14590 | default: | |
14591 | go_unreachable(); | |
14592 | } | |
35a54f17 | 14593 | } |
14594 | ||
14595 | // Dump ast representation for a type info expression. | |
14596 | ||
14597 | void | |
14598 | Slice_info_expression::do_dump_expression( | |
14599 | Ast_dump_context* ast_dump_context) const | |
14600 | { | |
14601 | ast_dump_context->ostream() << "sliceinfo("; | |
14602 | this->slice_->dump_expression(ast_dump_context); | |
14603 | ast_dump_context->ostream() << ","; | |
14604 | ast_dump_context->ostream() << | |
14605 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14606 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14607 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14608 | : "unknown"); | |
14609 | ast_dump_context->ostream() << ")"; | |
14610 | } | |
14611 | ||
14612 | // Make a slice info expression. | |
14613 | ||
14614 | Expression* | |
14615 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14616 | Location location) | |
14617 | { | |
14618 | return new Slice_info_expression(slice, slice_info, location); | |
14619 | } | |
14620 | ||
2c809f8f | 14621 | // An expression that represents a slice value: a struct with value pointer, |
14622 | // length, and capacity fields. | |
14623 | ||
14624 | class Slice_value_expression : public Expression | |
14625 | { | |
14626 | public: | |
14627 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14628 | Expression* cap, Location location) | |
14629 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14630 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14631 | { } | |
14632 | ||
14633 | protected: | |
14634 | int | |
14635 | do_traverse(Traverse*); | |
14636 | ||
14637 | Type* | |
14638 | do_type() | |
14639 | { return this->type_; } | |
14640 | ||
14641 | void | |
14642 | do_determine_type(const Type_context*) | |
14643 | { go_unreachable(); } | |
14644 | ||
14645 | Expression* | |
14646 | do_copy() | |
14647 | { | |
14648 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14649 | this->len_->copy(), this->cap_->copy(), | |
14650 | this->location()); | |
14651 | } | |
14652 | ||
ea664253 | 14653 | Bexpression* |
14654 | do_get_backend(Translate_context* context); | |
2c809f8f | 14655 | |
14656 | void | |
14657 | do_dump_expression(Ast_dump_context*) const; | |
14658 | ||
14659 | private: | |
14660 | // The type of the slice value. | |
14661 | Type* type_; | |
14662 | // The pointer to the values in the slice. | |
14663 | Expression* valptr_; | |
14664 | // The length of the slice. | |
14665 | Expression* len_; | |
14666 | // The capacity of the slice. | |
14667 | Expression* cap_; | |
14668 | }; | |
14669 | ||
14670 | int | |
14671 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14672 | { | |
55e8ba6a | 14673 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
14674 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 14675 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
14676 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14677 | return TRAVERSE_EXIT; | |
14678 | return TRAVERSE_CONTINUE; | |
14679 | } | |
14680 | ||
ea664253 | 14681 | Bexpression* |
14682 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14683 | { |
14684 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14685 | vals[0] = this->valptr_->get_backend(context); |
14686 | vals[1] = this->len_->get_backend(context); | |
14687 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14688 | |
14689 | Gogo* gogo = context->gogo(); | |
14690 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14691 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14692 | } |
14693 | ||
14694 | void | |
14695 | Slice_value_expression::do_dump_expression( | |
14696 | Ast_dump_context* ast_dump_context) const | |
14697 | { | |
14698 | ast_dump_context->ostream() << "slicevalue("; | |
14699 | ast_dump_context->ostream() << "values: "; | |
14700 | this->valptr_->dump_expression(ast_dump_context); | |
14701 | ast_dump_context->ostream() << ", length: "; | |
14702 | this->len_->dump_expression(ast_dump_context); | |
14703 | ast_dump_context->ostream() << ", capacity: "; | |
14704 | this->cap_->dump_expression(ast_dump_context); | |
14705 | ast_dump_context->ostream() << ")"; | |
14706 | } | |
14707 | ||
14708 | Expression* | |
14709 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14710 | Expression* cap, Location location) | |
14711 | { | |
14712 | go_assert(at->is_slice_type()); | |
14713 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14714 | } | |
2387f644 | 14715 | |
14716 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14717 | // This is used to access the method table or underlying object of an interface. | |
14718 | ||
14719 | class Interface_info_expression : public Expression | |
14720 | { | |
14721 | public: | |
14722 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 14723 | Location location) |
2387f644 | 14724 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
14725 | iface_(iface), iface_info_(iface_info) | |
14726 | { } | |
14727 | ||
14728 | protected: | |
14729 | Type* | |
14730 | do_type(); | |
14731 | ||
14732 | void | |
14733 | do_determine_type(const Type_context*) | |
14734 | { } | |
14735 | ||
14736 | Expression* | |
14737 | do_copy() | |
14738 | { | |
14739 | return new Interface_info_expression(this->iface_->copy(), | |
14740 | this->iface_info_, this->location()); | |
14741 | } | |
14742 | ||
ea664253 | 14743 | Bexpression* |
14744 | do_get_backend(Translate_context* context); | |
2387f644 | 14745 | |
14746 | void | |
14747 | do_dump_expression(Ast_dump_context*) const; | |
14748 | ||
14749 | void | |
14750 | do_issue_nil_check() | |
14751 | { this->iface_->issue_nil_check(); } | |
14752 | ||
14753 | private: | |
14754 | // The interface for which we are getting information. | |
14755 | Expression* iface_; | |
14756 | // What information we want. | |
14757 | Interface_info iface_info_; | |
14758 | }; | |
14759 | ||
14760 | // Return the type of the interface info. | |
14761 | ||
14762 | Type* | |
14763 | Interface_info_expression::do_type() | |
14764 | { | |
14765 | switch (this->iface_info_) | |
14766 | { | |
14767 | case INTERFACE_INFO_METHODS: | |
14768 | { | |
625d3118 | 14769 | typedef Unordered_map(Interface_type*, Type*) Hashtable; |
14770 | static Hashtable result_types; | |
14771 | ||
14772 | Interface_type* itype = this->iface_->type()->interface_type(); | |
14773 | ||
14774 | Hashtable::const_iterator p = result_types.find(itype); | |
14775 | if (p != result_types.end()) | |
14776 | return p->second; | |
14777 | ||
2c809f8f | 14778 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
625d3118 | 14779 | if (itype->is_empty()) |
14780 | { | |
14781 | result_types[itype] = pdt; | |
14782 | return pdt; | |
14783 | } | |
2c809f8f | 14784 | |
2387f644 | 14785 | Location loc = this->location(); |
14786 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 14787 | sfl->push_back( |
14788 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
14789 | ||
2387f644 | 14790 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); |
14791 | p != itype->methods()->end(); | |
14792 | ++p) | |
14793 | { | |
14794 | Function_type* ft = p->type()->function_type(); | |
14795 | go_assert(ft->receiver() == NULL); | |
14796 | ||
14797 | const Typed_identifier_list* params = ft->parameters(); | |
14798 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
14799 | if (params != NULL) | |
14800 | mparams->reserve(params->size() + 1); | |
14801 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
14802 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
14803 | if (params != NULL) | |
14804 | { | |
14805 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
14806 | pp != params->end(); | |
14807 | ++pp) | |
14808 | mparams->push_back(*pp); | |
14809 | } | |
14810 | ||
14811 | Typed_identifier_list* mresults = (ft->results() == NULL | |
14812 | ? NULL | |
14813 | : ft->results()->copy()); | |
14814 | Backend_function_type* mft = | |
14815 | Type::make_backend_function_type(NULL, mparams, mresults, | |
14816 | ft->location()); | |
14817 | ||
14818 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
14819 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
14820 | } | |
14821 | ||
625d3118 | 14822 | Pointer_type *pt = Type::make_pointer_type(Type::make_struct_type(sfl, loc)); |
14823 | result_types[itype] = pt; | |
14824 | return pt; | |
2387f644 | 14825 | } |
14826 | case INTERFACE_INFO_OBJECT: | |
14827 | return Type::make_pointer_type(Type::make_void_type()); | |
14828 | default: | |
14829 | go_unreachable(); | |
14830 | } | |
14831 | } | |
14832 | ||
ea664253 | 14833 | // Return the backend representation for interface information. |
2387f644 | 14834 | |
ea664253 | 14835 | Bexpression* |
14836 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 14837 | { |
14838 | Gogo* gogo = context->gogo(); | |
ea664253 | 14839 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 14840 | switch (this->iface_info_) |
14841 | { | |
14842 | case INTERFACE_INFO_METHODS: | |
14843 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 14844 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
14845 | this->location()); | |
2387f644 | 14846 | break; |
14847 | default: | |
14848 | go_unreachable(); | |
14849 | } | |
2387f644 | 14850 | } |
14851 | ||
14852 | // Dump ast representation for an interface info expression. | |
14853 | ||
14854 | void | |
14855 | Interface_info_expression::do_dump_expression( | |
14856 | Ast_dump_context* ast_dump_context) const | |
14857 | { | |
2c809f8f | 14858 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 14859 | ast_dump_context->ostream() << "interfaceinfo("; |
14860 | this->iface_->dump_expression(ast_dump_context); | |
14861 | ast_dump_context->ostream() << ","; | |
14862 | ast_dump_context->ostream() << | |
2c809f8f | 14863 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
14864 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 14865 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
14866 | : "unknown"); | |
14867 | ast_dump_context->ostream() << ")"; | |
14868 | } | |
14869 | ||
14870 | // Make an interface info expression. | |
14871 | ||
14872 | Expression* | |
14873 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
14874 | Location location) | |
14875 | { | |
14876 | return new Interface_info_expression(iface, iface_info, location); | |
14877 | } | |
14878 | ||
2c809f8f | 14879 | // An expression that represents an interface value. The first field is either |
14880 | // a type descriptor for an empty interface or a pointer to the interface method | |
14881 | // table for a non-empty interface. The second field is always the object. | |
14882 | ||
14883 | class Interface_value_expression : public Expression | |
14884 | { | |
14885 | public: | |
14886 | Interface_value_expression(Type* type, Expression* first_field, | |
14887 | Expression* obj, Location location) | |
14888 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
14889 | type_(type), first_field_(first_field), obj_(obj) | |
14890 | { } | |
14891 | ||
14892 | protected: | |
14893 | int | |
14894 | do_traverse(Traverse*); | |
14895 | ||
14896 | Type* | |
14897 | do_type() | |
14898 | { return this->type_; } | |
14899 | ||
14900 | void | |
14901 | do_determine_type(const Type_context*) | |
14902 | { go_unreachable(); } | |
14903 | ||
14904 | Expression* | |
14905 | do_copy() | |
14906 | { | |
14907 | return new Interface_value_expression(this->type_, | |
14908 | this->first_field_->copy(), | |
14909 | this->obj_->copy(), this->location()); | |
14910 | } | |
14911 | ||
ea664253 | 14912 | Bexpression* |
14913 | do_get_backend(Translate_context* context); | |
2c809f8f | 14914 | |
14915 | void | |
14916 | do_dump_expression(Ast_dump_context*) const; | |
14917 | ||
14918 | private: | |
14919 | // The type of the interface value. | |
14920 | Type* type_; | |
14921 | // The first field of the interface (either a type descriptor or a pointer | |
14922 | // to the method table. | |
14923 | Expression* first_field_; | |
14924 | // The underlying object of the interface. | |
14925 | Expression* obj_; | |
14926 | }; | |
14927 | ||
14928 | int | |
14929 | Interface_value_expression::do_traverse(Traverse* traverse) | |
14930 | { | |
14931 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
14932 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
14933 | return TRAVERSE_EXIT; | |
14934 | return TRAVERSE_CONTINUE; | |
14935 | } | |
14936 | ||
ea664253 | 14937 | Bexpression* |
14938 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14939 | { |
14940 | std::vector<Bexpression*> vals(2); | |
ea664253 | 14941 | vals[0] = this->first_field_->get_backend(context); |
14942 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 14943 | |
14944 | Gogo* gogo = context->gogo(); | |
14945 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14946 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14947 | } |
14948 | ||
14949 | void | |
14950 | Interface_value_expression::do_dump_expression( | |
14951 | Ast_dump_context* ast_dump_context) const | |
14952 | { | |
14953 | ast_dump_context->ostream() << "interfacevalue("; | |
14954 | ast_dump_context->ostream() << | |
14955 | (this->type_->interface_type()->is_empty() | |
14956 | ? "type_descriptor: " | |
14957 | : "methods: "); | |
14958 | this->first_field_->dump_expression(ast_dump_context); | |
14959 | ast_dump_context->ostream() << ", object: "; | |
14960 | this->obj_->dump_expression(ast_dump_context); | |
14961 | ast_dump_context->ostream() << ")"; | |
14962 | } | |
14963 | ||
14964 | Expression* | |
14965 | Expression::make_interface_value(Type* type, Expression* first_value, | |
14966 | Expression* object, Location location) | |
14967 | { | |
14968 | return new Interface_value_expression(type, first_value, object, location); | |
14969 | } | |
14970 | ||
14971 | // An interface method table for a pair of types: an interface type and a type | |
14972 | // that implements that interface. | |
14973 | ||
14974 | class Interface_mtable_expression : public Expression | |
14975 | { | |
14976 | public: | |
14977 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
14978 | bool is_pointer, Location location) | |
14979 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
14980 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
14981 | method_table_type_(NULL), bvar_(NULL) | |
14982 | { } | |
14983 | ||
14984 | protected: | |
14985 | int | |
14986 | do_traverse(Traverse*); | |
14987 | ||
14988 | Type* | |
14989 | do_type(); | |
14990 | ||
14991 | bool | |
3ae06f68 | 14992 | do_is_static_initializer() const |
2c809f8f | 14993 | { return true; } |
14994 | ||
14995 | void | |
14996 | do_determine_type(const Type_context*) | |
14997 | { go_unreachable(); } | |
14998 | ||
14999 | Expression* | |
15000 | do_copy() | |
15001 | { | |
15002 | return new Interface_mtable_expression(this->itype_, this->type_, | |
15003 | this->is_pointer_, this->location()); | |
15004 | } | |
15005 | ||
15006 | bool | |
15007 | do_is_addressable() const | |
15008 | { return true; } | |
15009 | ||
ea664253 | 15010 | Bexpression* |
15011 | do_get_backend(Translate_context* context); | |
2c809f8f | 15012 | |
15013 | void | |
15014 | do_dump_expression(Ast_dump_context*) const; | |
15015 | ||
15016 | private: | |
15017 | // The interface type for which the methods are defined. | |
15018 | Interface_type* itype_; | |
15019 | // The type to construct the interface method table for. | |
15020 | Type* type_; | |
15021 | // Whether this table contains the method set for the receiver type or the | |
15022 | // pointer receiver type. | |
15023 | bool is_pointer_; | |
15024 | // The type of the method table. | |
15025 | Type* method_table_type_; | |
15026 | // The backend variable that refers to the interface method table. | |
15027 | Bvariable* bvar_; | |
15028 | }; | |
15029 | ||
15030 | int | |
15031 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
15032 | { | |
15033 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
15034 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
15035 | return TRAVERSE_EXIT; | |
15036 | return TRAVERSE_CONTINUE; | |
15037 | } | |
15038 | ||
15039 | Type* | |
15040 | Interface_mtable_expression::do_type() | |
15041 | { | |
15042 | if (this->method_table_type_ != NULL) | |
15043 | return this->method_table_type_; | |
15044 | ||
15045 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15046 | go_assert(!interface_methods->empty()); | |
15047 | ||
15048 | Struct_field_list* sfl = new Struct_field_list; | |
15049 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
15050 | this->location()); | |
15051 | sfl->push_back(Struct_field(tid)); | |
15052 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15053 | p != interface_methods->end(); | |
15054 | ++p) | |
15055 | sfl->push_back(Struct_field(*p)); | |
15056 | this->method_table_type_ = Type::make_struct_type(sfl, this->location()); | |
15057 | return this->method_table_type_; | |
15058 | } | |
15059 | ||
ea664253 | 15060 | Bexpression* |
15061 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15062 | { |
15063 | Gogo* gogo = context->gogo(); | |
2c809f8f | 15064 | Location loc = Linemap::predeclared_location(); |
15065 | if (this->bvar_ != NULL) | |
d4e6573e | 15066 | return gogo->backend()->var_expression(this->bvar_, VE_rvalue, |
15067 | this->location()); | |
2c809f8f | 15068 | |
15069 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15070 | go_assert(!interface_methods->empty()); | |
15071 | ||
15072 | std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_") | |
15073 | + this->itype_->mangled_name(gogo) | |
15074 | + "__" | |
15075 | + this->type_->mangled_name(gogo)); | |
15076 | ||
15077 | // See whether this interface has any hidden methods. | |
15078 | bool has_hidden_methods = false; | |
15079 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15080 | p != interface_methods->end(); | |
15081 | ++p) | |
15082 | { | |
15083 | if (Gogo::is_hidden_name(p->name())) | |
15084 | { | |
15085 | has_hidden_methods = true; | |
15086 | break; | |
15087 | } | |
15088 | } | |
15089 | ||
15090 | // We already know that the named type is convertible to the | |
15091 | // interface. If the interface has hidden methods, and the named | |
15092 | // type is defined in a different package, then the interface | |
15093 | // conversion table will be defined by that other package. | |
15094 | if (has_hidden_methods | |
15095 | && this->type_->named_type() != NULL | |
15096 | && this->type_->named_type()->named_object()->package() != NULL) | |
15097 | { | |
15098 | Btype* btype = this->type()->get_backend(gogo); | |
438b4bec | 15099 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
2c809f8f | 15100 | this->bvar_ = |
438b4bec | 15101 | gogo->backend()->immutable_struct_reference(mangled_name, asm_name, |
15102 | btype, loc); | |
d4e6573e | 15103 | return gogo->backend()->var_expression(this->bvar_, VE_rvalue, |
15104 | this->location()); | |
2c809f8f | 15105 | } |
15106 | ||
15107 | // The first element is the type descriptor. | |
15108 | Type* td_type; | |
15109 | if (!this->is_pointer_) | |
15110 | td_type = this->type_; | |
15111 | else | |
15112 | td_type = Type::make_pointer_type(this->type_); | |
15113 | ||
15114 | // Build an interface method table for a type: a type descriptor followed by a | |
15115 | // list of function pointers, one for each interface method. This is used for | |
15116 | // interfaces. | |
15117 | Expression_list* svals = new Expression_list(); | |
15118 | svals->push_back(Expression::make_type_descriptor(td_type, loc)); | |
15119 | ||
15120 | Named_type* nt = this->type_->named_type(); | |
15121 | Struct_type* st = this->type_->struct_type(); | |
15122 | go_assert(nt != NULL || st != NULL); | |
15123 | ||
15124 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15125 | p != interface_methods->end(); | |
15126 | ++p) | |
15127 | { | |
15128 | bool is_ambiguous; | |
15129 | Method* m; | |
15130 | if (nt != NULL) | |
15131 | m = nt->method_function(p->name(), &is_ambiguous); | |
15132 | else | |
15133 | m = st->method_function(p->name(), &is_ambiguous); | |
15134 | go_assert(m != NULL); | |
15135 | Named_object* no = m->named_object(); | |
15136 | ||
15137 | go_assert(no->is_function() || no->is_function_declaration()); | |
15138 | svals->push_back(Expression::make_func_code_reference(no, loc)); | |
15139 | } | |
15140 | ||
15141 | Btype* btype = this->type()->get_backend(gogo); | |
15142 | Expression* mtable = Expression::make_struct_composite_literal(this->type(), | |
15143 | svals, loc); | |
ea664253 | 15144 | Bexpression* ctor = mtable->get_backend(context); |
2c809f8f | 15145 | |
15146 | bool is_public = has_hidden_methods && this->type_->named_type() != NULL; | |
438b4bec | 15147 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
15148 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false, | |
2c809f8f | 15149 | !is_public, btype, loc); |
15150 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
15151 | !is_public, btype, loc, ctor); | |
d4e6573e | 15152 | return gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc); |
2c809f8f | 15153 | } |
15154 | ||
15155 | void | |
15156 | Interface_mtable_expression::do_dump_expression( | |
15157 | Ast_dump_context* ast_dump_context) const | |
15158 | { | |
15159 | ast_dump_context->ostream() << "__go_" | |
15160 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
15161 | ast_dump_context->dump_type(this->itype_); | |
15162 | ast_dump_context->ostream() << "__"; | |
15163 | ast_dump_context->dump_type(this->type_); | |
15164 | } | |
15165 | ||
15166 | Expression* | |
15167 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
15168 | bool is_pointer, Location location) | |
15169 | { | |
15170 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
15171 | } | |
15172 | ||
e440a328 | 15173 | // An expression which evaluates to the offset of a field within a |
15174 | // struct. This, like Type_info_expression, q.v., is only used to | |
15175 | // initialize fields of a type descriptor. | |
15176 | ||
15177 | class Struct_field_offset_expression : public Expression | |
15178 | { | |
15179 | public: | |
15180 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 15181 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
15182 | Linemap::predeclared_location()), | |
e440a328 | 15183 | type_(type), field_(field) |
15184 | { } | |
15185 | ||
15186 | protected: | |
f23d7786 | 15187 | bool |
3ae06f68 | 15188 | do_is_static_initializer() const |
f23d7786 | 15189 | { return true; } |
15190 | ||
e440a328 | 15191 | Type* |
15192 | do_type() | |
15193 | { return Type::lookup_integer_type("uintptr"); } | |
15194 | ||
15195 | void | |
15196 | do_determine_type(const Type_context*) | |
15197 | { } | |
15198 | ||
15199 | Expression* | |
15200 | do_copy() | |
15201 | { return this; } | |
15202 | ||
ea664253 | 15203 | Bexpression* |
15204 | do_get_backend(Translate_context* context); | |
e440a328 | 15205 | |
d751bb78 | 15206 | void |
15207 | do_dump_expression(Ast_dump_context*) const; | |
15208 | ||
e440a328 | 15209 | private: |
15210 | // The type of the struct. | |
15211 | Struct_type* type_; | |
15212 | // The field. | |
15213 | const Struct_field* field_; | |
15214 | }; | |
15215 | ||
ea664253 | 15216 | // Return the backend representation for a struct field offset. |
e440a328 | 15217 | |
ea664253 | 15218 | Bexpression* |
15219 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 15220 | { |
e440a328 | 15221 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 15222 | Struct_field_list::const_iterator p; |
2c8bda43 | 15223 | unsigned i = 0; |
e440a328 | 15224 | for (p = fields->begin(); |
15225 | p != fields->end(); | |
2c8bda43 | 15226 | ++p, ++i) |
15227 | if (&*p == this->field_) | |
15228 | break; | |
c484d925 | 15229 | go_assert(&*p == this->field_); |
e440a328 | 15230 | |
2c8bda43 | 15231 | Gogo* gogo = context->gogo(); |
15232 | Btype* btype = this->type_->get_backend(gogo); | |
15233 | ||
3f378015 | 15234 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 15235 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 15236 | Expression* ret = |
3f378015 | 15237 | Expression::make_integer_int64(offset, uptr_type, |
15238 | Linemap::predeclared_location()); | |
ea664253 | 15239 | return ret->get_backend(context); |
e440a328 | 15240 | } |
15241 | ||
d751bb78 | 15242 | // Dump ast representation for a struct field offset expression. |
15243 | ||
15244 | void | |
15245 | Struct_field_offset_expression::do_dump_expression( | |
15246 | Ast_dump_context* ast_dump_context) const | |
15247 | { | |
15248 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 15249 | ast_dump_context->dump_type(this->type_); |
15250 | ast_dump_context->ostream() << '.'; | |
15251 | ast_dump_context->ostream() << | |
15252 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 15253 | ast_dump_context->ostream() << ")"; |
15254 | } | |
15255 | ||
e440a328 | 15256 | // Make an expression for a struct field offset. |
15257 | ||
15258 | Expression* | |
15259 | Expression::make_struct_field_offset(Struct_type* type, | |
15260 | const Struct_field* field) | |
15261 | { | |
15262 | return new Struct_field_offset_expression(type, field); | |
15263 | } | |
15264 | ||
15265 | // An expression which evaluates to the address of an unnamed label. | |
15266 | ||
15267 | class Label_addr_expression : public Expression | |
15268 | { | |
15269 | public: | |
b13c66cd | 15270 | Label_addr_expression(Label* label, Location location) |
e440a328 | 15271 | : Expression(EXPRESSION_LABEL_ADDR, location), |
15272 | label_(label) | |
15273 | { } | |
15274 | ||
15275 | protected: | |
15276 | Type* | |
15277 | do_type() | |
15278 | { return Type::make_pointer_type(Type::make_void_type()); } | |
15279 | ||
15280 | void | |
15281 | do_determine_type(const Type_context*) | |
15282 | { } | |
15283 | ||
15284 | Expression* | |
15285 | do_copy() | |
15286 | { return new Label_addr_expression(this->label_, this->location()); } | |
15287 | ||
ea664253 | 15288 | Bexpression* |
15289 | do_get_backend(Translate_context* context) | |
15290 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 15291 | |
d751bb78 | 15292 | void |
15293 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15294 | { ast_dump_context->ostream() << this->label_->name(); } | |
15295 | ||
e440a328 | 15296 | private: |
15297 | // The label whose address we are taking. | |
15298 | Label* label_; | |
15299 | }; | |
15300 | ||
15301 | // Make an expression for the address of an unnamed label. | |
15302 | ||
15303 | Expression* | |
b13c66cd | 15304 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 15305 | { |
15306 | return new Label_addr_expression(label, location); | |
15307 | } | |
15308 | ||
da244e59 | 15309 | // Class Conditional_expression. |
283a177b | 15310 | |
2c809f8f | 15311 | // Traversal. |
15312 | ||
15313 | int | |
15314 | Conditional_expression::do_traverse(Traverse* traverse) | |
15315 | { | |
15316 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15317 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15318 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15319 | return TRAVERSE_EXIT; | |
15320 | return TRAVERSE_CONTINUE; | |
15321 | } | |
15322 | ||
283a177b | 15323 | // Return the type of the conditional expression. |
15324 | ||
15325 | Type* | |
15326 | Conditional_expression::do_type() | |
15327 | { | |
15328 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15329 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15330 | NULL)) | |
283a177b | 15331 | result_type = this->then_->type(); |
15332 | else if (this->then_->is_nil_expression() | |
15333 | || this->else_->is_nil_expression()) | |
15334 | result_type = (!this->then_->is_nil_expression() | |
15335 | ? this->then_->type() | |
15336 | : this->else_->type()); | |
15337 | return result_type; | |
15338 | } | |
15339 | ||
2c809f8f | 15340 | // Determine type for a conditional expression. |
15341 | ||
15342 | void | |
15343 | Conditional_expression::do_determine_type(const Type_context* context) | |
15344 | { | |
15345 | this->cond_->determine_type_no_context(); | |
15346 | this->then_->determine_type(context); | |
15347 | this->else_->determine_type(context); | |
15348 | } | |
15349 | ||
283a177b | 15350 | // Get the backend representation of a conditional expression. |
15351 | ||
ea664253 | 15352 | Bexpression* |
15353 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 15354 | { |
15355 | Gogo* gogo = context->gogo(); | |
15356 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 15357 | Bexpression* cond = this->cond_->get_backend(context); |
15358 | Bexpression* then = this->then_->get_backend(context); | |
15359 | Bexpression* belse = this->else_->get_backend(context); | |
15360 | return gogo->backend()->conditional_expression(result_btype, cond, then, | |
15361 | belse, this->location()); | |
283a177b | 15362 | } |
15363 | ||
15364 | // Dump ast representation of a conditional expression. | |
15365 | ||
15366 | void | |
15367 | Conditional_expression::do_dump_expression( | |
15368 | Ast_dump_context* ast_dump_context) const | |
15369 | { | |
15370 | ast_dump_context->ostream() << "("; | |
15371 | ast_dump_context->dump_expression(this->cond_); | |
15372 | ast_dump_context->ostream() << " ? "; | |
15373 | ast_dump_context->dump_expression(this->then_); | |
15374 | ast_dump_context->ostream() << " : "; | |
15375 | ast_dump_context->dump_expression(this->else_); | |
15376 | ast_dump_context->ostream() << ") "; | |
15377 | } | |
15378 | ||
15379 | // Make a conditional expression. | |
15380 | ||
15381 | Expression* | |
15382 | Expression::make_conditional(Expression* cond, Expression* then, | |
15383 | Expression* else_expr, Location location) | |
15384 | { | |
15385 | return new Conditional_expression(cond, then, else_expr, location); | |
15386 | } | |
15387 | ||
da244e59 | 15388 | // Class Compound_expression. |
2c809f8f | 15389 | |
15390 | // Traversal. | |
15391 | ||
15392 | int | |
15393 | Compound_expression::do_traverse(Traverse* traverse) | |
15394 | { | |
15395 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15396 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15397 | return TRAVERSE_EXIT; | |
15398 | return TRAVERSE_CONTINUE; | |
15399 | } | |
15400 | ||
15401 | // Return the type of the compound expression. | |
15402 | ||
15403 | Type* | |
15404 | Compound_expression::do_type() | |
15405 | { | |
15406 | return this->expr_->type(); | |
15407 | } | |
15408 | ||
15409 | // Determine type for a compound expression. | |
15410 | ||
15411 | void | |
15412 | Compound_expression::do_determine_type(const Type_context* context) | |
15413 | { | |
15414 | this->init_->determine_type_no_context(); | |
15415 | this->expr_->determine_type(context); | |
15416 | } | |
15417 | ||
15418 | // Get the backend representation of a compound expression. | |
15419 | ||
ea664253 | 15420 | Bexpression* |
15421 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15422 | { |
15423 | Gogo* gogo = context->gogo(); | |
ea664253 | 15424 | Bexpression* binit = this->init_->get_backend(context); |
2c809f8f | 15425 | Bstatement* init_stmt = gogo->backend()->expression_statement(binit); |
ea664253 | 15426 | Bexpression* bexpr = this->expr_->get_backend(context); |
15427 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15428 | this->location()); | |
2c809f8f | 15429 | } |
15430 | ||
15431 | // Dump ast representation of a conditional expression. | |
15432 | ||
15433 | void | |
15434 | Compound_expression::do_dump_expression( | |
15435 | Ast_dump_context* ast_dump_context) const | |
15436 | { | |
15437 | ast_dump_context->ostream() << "("; | |
15438 | ast_dump_context->dump_expression(this->init_); | |
15439 | ast_dump_context->ostream() << ","; | |
15440 | ast_dump_context->dump_expression(this->expr_); | |
15441 | ast_dump_context->ostream() << ") "; | |
15442 | } | |
15443 | ||
15444 | // Make a compound expression. | |
15445 | ||
15446 | Expression* | |
15447 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15448 | { | |
15449 | return new Compound_expression(init, expr, location); | |
15450 | } | |
15451 | ||
1b4fb1e0 | 15452 | // Class Backend_expression. |
15453 | ||
15454 | int | |
15455 | Backend_expression::do_traverse(Traverse*) | |
15456 | { | |
15457 | return TRAVERSE_CONTINUE; | |
15458 | } | |
15459 | ||
15460 | void | |
15461 | Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15462 | { | |
15463 | ast_dump_context->ostream() << "backend_expression<"; | |
15464 | ast_dump_context->dump_type(this->type_); | |
15465 | ast_dump_context->ostream() << ">"; | |
15466 | } | |
15467 | ||
15468 | Expression* | |
15469 | Expression::make_backend(Bexpression* bexpr, Type* type, Location location) | |
15470 | { | |
15471 | return new Backend_expression(bexpr, type, location); | |
15472 | } | |
15473 | ||
e440a328 | 15474 | // Import an expression. This comes at the end in order to see the |
15475 | // various class definitions. | |
15476 | ||
15477 | Expression* | |
15478 | Expression::import_expression(Import* imp) | |
15479 | { | |
15480 | int c = imp->peek_char(); | |
15481 | if (imp->match_c_string("- ") | |
15482 | || imp->match_c_string("! ") | |
15483 | || imp->match_c_string("^ ")) | |
15484 | return Unary_expression::do_import(imp); | |
15485 | else if (c == '(') | |
15486 | return Binary_expression::do_import(imp); | |
15487 | else if (imp->match_c_string("true") | |
15488 | || imp->match_c_string("false")) | |
15489 | return Boolean_expression::do_import(imp); | |
15490 | else if (c == '"') | |
15491 | return String_expression::do_import(imp); | |
15492 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15493 | { | |
15494 | // This handles integers, floats and complex constants. | |
15495 | return Integer_expression::do_import(imp); | |
15496 | } | |
15497 | else if (imp->match_c_string("nil")) | |
15498 | return Nil_expression::do_import(imp); | |
15499 | else if (imp->match_c_string("convert")) | |
15500 | return Type_conversion_expression::do_import(imp); | |
15501 | else | |
15502 | { | |
631d5788 | 15503 | go_error_at(imp->location(), "import error: expected expression"); |
e440a328 | 15504 | return Expression::make_error(imp->location()); |
15505 | } | |
15506 | } | |
15507 | ||
15508 | // Class Expression_list. | |
15509 | ||
15510 | // Traverse the list. | |
15511 | ||
15512 | int | |
15513 | Expression_list::traverse(Traverse* traverse) | |
15514 | { | |
15515 | for (Expression_list::iterator p = this->begin(); | |
15516 | p != this->end(); | |
15517 | ++p) | |
15518 | { | |
15519 | if (*p != NULL) | |
15520 | { | |
15521 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15522 | return TRAVERSE_EXIT; | |
15523 | } | |
15524 | } | |
15525 | return TRAVERSE_CONTINUE; | |
15526 | } | |
15527 | ||
15528 | // Copy the list. | |
15529 | ||
15530 | Expression_list* | |
15531 | Expression_list::copy() | |
15532 | { | |
15533 | Expression_list* ret = new Expression_list(); | |
15534 | for (Expression_list::iterator p = this->begin(); | |
15535 | p != this->end(); | |
15536 | ++p) | |
15537 | { | |
15538 | if (*p == NULL) | |
15539 | ret->push_back(NULL); | |
15540 | else | |
15541 | ret->push_back((*p)->copy()); | |
15542 | } | |
15543 | return ret; | |
15544 | } | |
15545 | ||
15546 | // Return whether an expression list has an error expression. | |
15547 | ||
15548 | bool | |
15549 | Expression_list::contains_error() const | |
15550 | { | |
15551 | for (Expression_list::const_iterator p = this->begin(); | |
15552 | p != this->end(); | |
15553 | ++p) | |
15554 | if (*p != NULL && (*p)->is_error_expression()) | |
15555 | return true; | |
15556 | return false; | |
15557 | } | |
0c77715b | 15558 | |
15559 | // Class Numeric_constant. | |
15560 | ||
15561 | // Destructor. | |
15562 | ||
15563 | Numeric_constant::~Numeric_constant() | |
15564 | { | |
15565 | this->clear(); | |
15566 | } | |
15567 | ||
15568 | // Copy constructor. | |
15569 | ||
15570 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15571 | : classification_(a.classification_), type_(a.type_) | |
15572 | { | |
15573 | switch (a.classification_) | |
15574 | { | |
15575 | case NC_INVALID: | |
15576 | break; | |
15577 | case NC_INT: | |
15578 | case NC_RUNE: | |
15579 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15580 | break; | |
15581 | case NC_FLOAT: | |
15582 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15583 | break; | |
15584 | case NC_COMPLEX: | |
fcbea5e4 | 15585 | mpc_init2(this->u_.complex_val, mpc_precision); |
15586 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15587 | break; |
15588 | default: | |
15589 | go_unreachable(); | |
15590 | } | |
15591 | } | |
15592 | ||
15593 | // Assignment operator. | |
15594 | ||
15595 | Numeric_constant& | |
15596 | Numeric_constant::operator=(const Numeric_constant& a) | |
15597 | { | |
15598 | this->clear(); | |
15599 | this->classification_ = a.classification_; | |
15600 | this->type_ = a.type_; | |
15601 | switch (a.classification_) | |
15602 | { | |
15603 | case NC_INVALID: | |
15604 | break; | |
15605 | case NC_INT: | |
15606 | case NC_RUNE: | |
15607 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15608 | break; | |
15609 | case NC_FLOAT: | |
15610 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15611 | break; | |
15612 | case NC_COMPLEX: | |
fcbea5e4 | 15613 | mpc_init2(this->u_.complex_val, mpc_precision); |
15614 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15615 | break; |
15616 | default: | |
15617 | go_unreachable(); | |
15618 | } | |
15619 | return *this; | |
15620 | } | |
15621 | ||
15622 | // Clear the contents. | |
15623 | ||
15624 | void | |
15625 | Numeric_constant::clear() | |
15626 | { | |
15627 | switch (this->classification_) | |
15628 | { | |
15629 | case NC_INVALID: | |
15630 | break; | |
15631 | case NC_INT: | |
15632 | case NC_RUNE: | |
15633 | mpz_clear(this->u_.int_val); | |
15634 | break; | |
15635 | case NC_FLOAT: | |
15636 | mpfr_clear(this->u_.float_val); | |
15637 | break; | |
15638 | case NC_COMPLEX: | |
fcbea5e4 | 15639 | mpc_clear(this->u_.complex_val); |
0c77715b | 15640 | break; |
15641 | default: | |
15642 | go_unreachable(); | |
15643 | } | |
15644 | this->classification_ = NC_INVALID; | |
15645 | } | |
15646 | ||
15647 | // Set to an unsigned long value. | |
15648 | ||
15649 | void | |
15650 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15651 | { | |
15652 | this->clear(); | |
15653 | this->classification_ = NC_INT; | |
15654 | this->type_ = type; | |
15655 | mpz_init_set_ui(this->u_.int_val, val); | |
15656 | } | |
15657 | ||
15658 | // Set to an integer value. | |
15659 | ||
15660 | void | |
15661 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15662 | { | |
15663 | this->clear(); | |
15664 | this->classification_ = NC_INT; | |
15665 | this->type_ = type; | |
15666 | mpz_init_set(this->u_.int_val, val); | |
15667 | } | |
15668 | ||
15669 | // Set to a rune value. | |
15670 | ||
15671 | void | |
15672 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15673 | { | |
15674 | this->clear(); | |
15675 | this->classification_ = NC_RUNE; | |
15676 | this->type_ = type; | |
15677 | mpz_init_set(this->u_.int_val, val); | |
15678 | } | |
15679 | ||
15680 | // Set to a floating point value. | |
15681 | ||
15682 | void | |
15683 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15684 | { | |
15685 | this->clear(); | |
15686 | this->classification_ = NC_FLOAT; | |
15687 | this->type_ = type; | |
833b523c | 15688 | // Numeric constants do not have negative zero values, so remove |
15689 | // them here. They also don't have infinity or NaN values, but we | |
15690 | // should never see them here. | |
15691 | if (mpfr_zero_p(val)) | |
15692 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
15693 | else | |
15694 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 15695 | } |
15696 | ||
15697 | // Set to a complex value. | |
15698 | ||
15699 | void | |
fcbea5e4 | 15700 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 15701 | { |
15702 | this->clear(); | |
15703 | this->classification_ = NC_COMPLEX; | |
15704 | this->type_ = type; | |
fcbea5e4 | 15705 | mpc_init2(this->u_.complex_val, mpc_precision); |
15706 | mpc_set(this->u_.complex_val, val, MPC_RNDNN); | |
0c77715b | 15707 | } |
15708 | ||
15709 | // Get an int value. | |
15710 | ||
15711 | void | |
15712 | Numeric_constant::get_int(mpz_t* val) const | |
15713 | { | |
15714 | go_assert(this->is_int()); | |
15715 | mpz_init_set(*val, this->u_.int_val); | |
15716 | } | |
15717 | ||
15718 | // Get a rune value. | |
15719 | ||
15720 | void | |
15721 | Numeric_constant::get_rune(mpz_t* val) const | |
15722 | { | |
15723 | go_assert(this->is_rune()); | |
15724 | mpz_init_set(*val, this->u_.int_val); | |
15725 | } | |
15726 | ||
15727 | // Get a floating point value. | |
15728 | ||
15729 | void | |
15730 | Numeric_constant::get_float(mpfr_t* val) const | |
15731 | { | |
15732 | go_assert(this->is_float()); | |
15733 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15734 | } | |
15735 | ||
15736 | // Get a complex value. | |
15737 | ||
15738 | void | |
fcbea5e4 | 15739 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 15740 | { |
15741 | go_assert(this->is_complex()); | |
fcbea5e4 | 15742 | mpc_init2(*val, mpc_precision); |
15743 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 15744 | } |
15745 | ||
15746 | // Express value as unsigned long if possible. | |
15747 | ||
15748 | Numeric_constant::To_unsigned_long | |
15749 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
15750 | { | |
15751 | switch (this->classification_) | |
15752 | { | |
15753 | case NC_INT: | |
15754 | case NC_RUNE: | |
15755 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
15756 | case NC_FLOAT: | |
15757 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
15758 | case NC_COMPLEX: | |
fcbea5e4 | 15759 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15760 | return NC_UL_NOTINT; |
fcbea5e4 | 15761 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
15762 | val); | |
0c77715b | 15763 | default: |
15764 | go_unreachable(); | |
15765 | } | |
15766 | } | |
15767 | ||
15768 | // Express integer value as unsigned long if possible. | |
15769 | ||
15770 | Numeric_constant::To_unsigned_long | |
15771 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
15772 | unsigned long *val) const | |
15773 | { | |
15774 | if (mpz_sgn(ival) < 0) | |
15775 | return NC_UL_NEGATIVE; | |
15776 | unsigned long ui = mpz_get_ui(ival); | |
15777 | if (mpz_cmp_ui(ival, ui) != 0) | |
15778 | return NC_UL_BIG; | |
15779 | *val = ui; | |
15780 | return NC_UL_VALID; | |
15781 | } | |
15782 | ||
15783 | // Express floating point value as unsigned long if possible. | |
15784 | ||
15785 | Numeric_constant::To_unsigned_long | |
15786 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
15787 | unsigned long *val) const | |
15788 | { | |
15789 | if (!mpfr_integer_p(fval)) | |
15790 | return NC_UL_NOTINT; | |
15791 | mpz_t ival; | |
15792 | mpz_init(ival); | |
15793 | mpfr_get_z(ival, fval, GMP_RNDN); | |
15794 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
15795 | mpz_clear(ival); | |
15796 | return ret; | |
15797 | } | |
15798 | ||
15799 | // Convert value to integer if possible. | |
15800 | ||
15801 | bool | |
15802 | Numeric_constant::to_int(mpz_t* val) const | |
15803 | { | |
15804 | switch (this->classification_) | |
15805 | { | |
15806 | case NC_INT: | |
15807 | case NC_RUNE: | |
15808 | mpz_init_set(*val, this->u_.int_val); | |
15809 | return true; | |
15810 | case NC_FLOAT: | |
15811 | if (!mpfr_integer_p(this->u_.float_val)) | |
15812 | return false; | |
15813 | mpz_init(*val); | |
15814 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
15815 | return true; | |
15816 | case NC_COMPLEX: | |
fcbea5e4 | 15817 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
15818 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 15819 | return false; |
15820 | mpz_init(*val); | |
fcbea5e4 | 15821 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15822 | return true; |
15823 | default: | |
15824 | go_unreachable(); | |
15825 | } | |
15826 | } | |
15827 | ||
15828 | // Convert value to floating point if possible. | |
15829 | ||
15830 | bool | |
15831 | Numeric_constant::to_float(mpfr_t* val) const | |
15832 | { | |
15833 | switch (this->classification_) | |
15834 | { | |
15835 | case NC_INT: | |
15836 | case NC_RUNE: | |
15837 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
15838 | return true; | |
15839 | case NC_FLOAT: | |
15840 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15841 | return true; | |
15842 | case NC_COMPLEX: | |
fcbea5e4 | 15843 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 15844 | return false; |
fcbea5e4 | 15845 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15846 | return true; |
15847 | default: | |
15848 | go_unreachable(); | |
15849 | } | |
15850 | } | |
15851 | ||
15852 | // Convert value to complex. | |
15853 | ||
15854 | bool | |
fcbea5e4 | 15855 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 15856 | { |
fcbea5e4 | 15857 | mpc_init2(*val, mpc_precision); |
0c77715b | 15858 | switch (this->classification_) |
15859 | { | |
15860 | case NC_INT: | |
15861 | case NC_RUNE: | |
fcbea5e4 | 15862 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 15863 | return true; |
15864 | case NC_FLOAT: | |
fcbea5e4 | 15865 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 15866 | return true; |
15867 | case NC_COMPLEX: | |
fcbea5e4 | 15868 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 15869 | return true; |
15870 | default: | |
15871 | go_unreachable(); | |
15872 | } | |
15873 | } | |
15874 | ||
15875 | // Get the type. | |
15876 | ||
15877 | Type* | |
15878 | Numeric_constant::type() const | |
15879 | { | |
15880 | if (this->type_ != NULL) | |
15881 | return this->type_; | |
15882 | switch (this->classification_) | |
15883 | { | |
15884 | case NC_INT: | |
15885 | return Type::make_abstract_integer_type(); | |
15886 | case NC_RUNE: | |
15887 | return Type::make_abstract_character_type(); | |
15888 | case NC_FLOAT: | |
15889 | return Type::make_abstract_float_type(); | |
15890 | case NC_COMPLEX: | |
15891 | return Type::make_abstract_complex_type(); | |
15892 | default: | |
15893 | go_unreachable(); | |
15894 | } | |
15895 | } | |
15896 | ||
15897 | // If the constant can be expressed in TYPE, then set the type of the | |
15898 | // constant to TYPE and return true. Otherwise return false, and, if | |
15899 | // ISSUE_ERROR is true, report an appropriate error message. | |
15900 | ||
15901 | bool | |
15902 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
15903 | { | |
15904 | bool ret; | |
f11c2155 | 15905 | if (type == NULL || type->is_error()) |
0c77715b | 15906 | ret = true; |
15907 | else if (type->integer_type() != NULL) | |
15908 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
15909 | else if (type->float_type() != NULL) | |
15910 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
15911 | else if (type->complex_type() != NULL) | |
15912 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
15913 | else | |
5706ab68 | 15914 | { |
15915 | ret = false; | |
15916 | if (issue_error) | |
15917 | go_assert(saw_errors()); | |
15918 | } | |
0c77715b | 15919 | if (ret) |
15920 | this->type_ = type; | |
15921 | return ret; | |
15922 | } | |
15923 | ||
15924 | // Check whether the constant can be expressed in an integer type. | |
15925 | ||
15926 | bool | |
15927 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 15928 | Location location) |
0c77715b | 15929 | { |
15930 | mpz_t val; | |
15931 | switch (this->classification_) | |
15932 | { | |
15933 | case NC_INT: | |
15934 | case NC_RUNE: | |
15935 | mpz_init_set(val, this->u_.int_val); | |
15936 | break; | |
15937 | ||
15938 | case NC_FLOAT: | |
15939 | if (!mpfr_integer_p(this->u_.float_val)) | |
15940 | { | |
15941 | if (issue_error) | |
71a45216 | 15942 | { |
631d5788 | 15943 | go_error_at(location, |
15944 | "floating point constant truncated to integer"); | |
71a45216 | 15945 | this->set_invalid(); |
15946 | } | |
0c77715b | 15947 | return false; |
15948 | } | |
15949 | mpz_init(val); | |
15950 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
15951 | break; | |
15952 | ||
15953 | case NC_COMPLEX: | |
fcbea5e4 | 15954 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
15955 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 15956 | { |
15957 | if (issue_error) | |
71a45216 | 15958 | { |
631d5788 | 15959 | go_error_at(location, "complex constant truncated to integer"); |
71a45216 | 15960 | this->set_invalid(); |
15961 | } | |
0c77715b | 15962 | return false; |
15963 | } | |
15964 | mpz_init(val); | |
fcbea5e4 | 15965 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 15966 | break; |
15967 | ||
15968 | default: | |
15969 | go_unreachable(); | |
15970 | } | |
15971 | ||
15972 | bool ret; | |
15973 | if (type->is_abstract()) | |
15974 | ret = true; | |
15975 | else | |
15976 | { | |
15977 | int bits = mpz_sizeinbase(val, 2); | |
15978 | if (type->is_unsigned()) | |
15979 | { | |
15980 | // For an unsigned type we can only accept a nonnegative | |
15981 | // number, and we must be able to represents at least BITS. | |
15982 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
15983 | } | |
15984 | else | |
15985 | { | |
15986 | // For a signed type we need an extra bit to indicate the | |
15987 | // sign. We have to handle the most negative integer | |
15988 | // specially. | |
15989 | ret = (bits + 1 <= type->bits() | |
15990 | || (bits <= type->bits() | |
15991 | && mpz_sgn(val) < 0 | |
15992 | && (mpz_scan1(val, 0) | |
15993 | == static_cast<unsigned long>(type->bits() - 1)) | |
15994 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
15995 | } | |
15996 | } | |
15997 | ||
15998 | if (!ret && issue_error) | |
71a45216 | 15999 | { |
631d5788 | 16000 | go_error_at(location, "integer constant overflow"); |
71a45216 | 16001 | this->set_invalid(); |
16002 | } | |
0c77715b | 16003 | |
16004 | return ret; | |
16005 | } | |
16006 | ||
16007 | // Check whether the constant can be expressed in a floating point | |
16008 | // type. | |
16009 | ||
16010 | bool | |
16011 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 16012 | Location location) |
0c77715b | 16013 | { |
16014 | mpfr_t val; | |
16015 | switch (this->classification_) | |
16016 | { | |
16017 | case NC_INT: | |
16018 | case NC_RUNE: | |
16019 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
16020 | break; | |
16021 | ||
16022 | case NC_FLOAT: | |
16023 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
16024 | break; | |
16025 | ||
16026 | case NC_COMPLEX: | |
fcbea5e4 | 16027 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16028 | { |
16029 | if (issue_error) | |
71a45216 | 16030 | { |
16031 | this->set_invalid(); | |
631d5788 | 16032 | go_error_at(location, "complex constant truncated to float"); |
71a45216 | 16033 | } |
0c77715b | 16034 | return false; |
16035 | } | |
fcbea5e4 | 16036 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16037 | break; |
16038 | ||
16039 | default: | |
16040 | go_unreachable(); | |
16041 | } | |
16042 | ||
16043 | bool ret; | |
16044 | if (type->is_abstract()) | |
16045 | ret = true; | |
16046 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
16047 | { | |
16048 | // A NaN or Infinity always fits in the range of the type. | |
16049 | ret = true; | |
16050 | } | |
16051 | else | |
16052 | { | |
16053 | mp_exp_t exp = mpfr_get_exp(val); | |
16054 | mp_exp_t max_exp; | |
16055 | switch (type->bits()) | |
16056 | { | |
16057 | case 32: | |
16058 | max_exp = 128; | |
16059 | break; | |
16060 | case 64: | |
16061 | max_exp = 1024; | |
16062 | break; | |
16063 | default: | |
16064 | go_unreachable(); | |
16065 | } | |
16066 | ||
16067 | ret = exp <= max_exp; | |
d0bcce51 | 16068 | |
16069 | if (ret) | |
16070 | { | |
16071 | // Round the constant to the desired type. | |
16072 | mpfr_t t; | |
16073 | mpfr_init(t); | |
16074 | switch (type->bits()) | |
16075 | { | |
16076 | case 32: | |
16077 | mpfr_set_prec(t, 24); | |
16078 | break; | |
16079 | case 64: | |
16080 | mpfr_set_prec(t, 53); | |
16081 | break; | |
16082 | default: | |
16083 | go_unreachable(); | |
16084 | } | |
16085 | mpfr_set(t, val, GMP_RNDN); | |
16086 | mpfr_set(val, t, GMP_RNDN); | |
16087 | mpfr_clear(t); | |
16088 | ||
16089 | this->set_float(type, val); | |
16090 | } | |
0c77715b | 16091 | } |
16092 | ||
16093 | mpfr_clear(val); | |
16094 | ||
16095 | if (!ret && issue_error) | |
71a45216 | 16096 | { |
631d5788 | 16097 | go_error_at(location, "floating point constant overflow"); |
71a45216 | 16098 | this->set_invalid(); |
16099 | } | |
0c77715b | 16100 | |
16101 | return ret; | |
16102 | } | |
16103 | ||
16104 | // Check whether the constant can be expressed in a complex type. | |
16105 | ||
16106 | bool | |
16107 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 16108 | Location location) |
0c77715b | 16109 | { |
16110 | if (type->is_abstract()) | |
16111 | return true; | |
16112 | ||
16113 | mp_exp_t max_exp; | |
16114 | switch (type->bits()) | |
16115 | { | |
16116 | case 64: | |
16117 | max_exp = 128; | |
16118 | break; | |
16119 | case 128: | |
16120 | max_exp = 1024; | |
16121 | break; | |
16122 | default: | |
16123 | go_unreachable(); | |
16124 | } | |
16125 | ||
fcbea5e4 | 16126 | mpc_t val; |
16127 | mpc_init2(val, mpc_precision); | |
0c77715b | 16128 | switch (this->classification_) |
16129 | { | |
16130 | case NC_INT: | |
16131 | case NC_RUNE: | |
fcbea5e4 | 16132 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16133 | break; |
16134 | ||
16135 | case NC_FLOAT: | |
fcbea5e4 | 16136 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16137 | break; |
16138 | ||
16139 | case NC_COMPLEX: | |
fcbea5e4 | 16140 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16141 | break; |
16142 | ||
16143 | default: | |
16144 | go_unreachable(); | |
16145 | } | |
16146 | ||
d0bcce51 | 16147 | bool ret = true; |
fcbea5e4 | 16148 | if (!mpfr_nan_p(mpc_realref(val)) |
16149 | && !mpfr_inf_p(mpc_realref(val)) | |
16150 | && !mpfr_zero_p(mpc_realref(val)) | |
16151 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 16152 | { |
16153 | if (issue_error) | |
71a45216 | 16154 | { |
631d5788 | 16155 | go_error_at(location, "complex real part overflow"); |
71a45216 | 16156 | this->set_invalid(); |
16157 | } | |
d0bcce51 | 16158 | ret = false; |
16159 | } | |
0c77715b | 16160 | |
fcbea5e4 | 16161 | if (!mpfr_nan_p(mpc_imagref(val)) |
16162 | && !mpfr_inf_p(mpc_imagref(val)) | |
16163 | && !mpfr_zero_p(mpc_imagref(val)) | |
16164 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 16165 | { |
16166 | if (issue_error) | |
71a45216 | 16167 | { |
631d5788 | 16168 | go_error_at(location, "complex imaginary part overflow"); |
71a45216 | 16169 | this->set_invalid(); |
16170 | } | |
d0bcce51 | 16171 | ret = false; |
16172 | } | |
0c77715b | 16173 | |
d0bcce51 | 16174 | if (ret) |
16175 | { | |
16176 | // Round the constant to the desired type. | |
fcbea5e4 | 16177 | mpc_t t; |
d0bcce51 | 16178 | switch (type->bits()) |
16179 | { | |
16180 | case 64: | |
fcbea5e4 | 16181 | mpc_init2(t, 24); |
d0bcce51 | 16182 | break; |
16183 | case 128: | |
fcbea5e4 | 16184 | mpc_init2(t, 53); |
d0bcce51 | 16185 | break; |
16186 | default: | |
16187 | go_unreachable(); | |
16188 | } | |
fcbea5e4 | 16189 | mpc_set(t, val, MPC_RNDNN); |
16190 | mpc_set(val, t, MPC_RNDNN); | |
16191 | mpc_clear(t); | |
d0bcce51 | 16192 | |
fcbea5e4 | 16193 | this->set_complex(type, val); |
d0bcce51 | 16194 | } |
16195 | ||
fcbea5e4 | 16196 | mpc_clear(val); |
0c77715b | 16197 | |
16198 | return ret; | |
16199 | } | |
16200 | ||
16201 | // Return an Expression for this value. | |
16202 | ||
16203 | Expression* | |
16204 | Numeric_constant::expression(Location loc) const | |
16205 | { | |
16206 | switch (this->classification_) | |
16207 | { | |
16208 | case NC_INT: | |
e67508fa | 16209 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 16210 | case NC_RUNE: |
16211 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
16212 | case NC_FLOAT: | |
16213 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
16214 | case NC_COMPLEX: | |
fcbea5e4 | 16215 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 16216 | case NC_INVALID: |
16217 | go_assert(saw_errors()); | |
16218 | return Expression::make_error(loc); | |
0c77715b | 16219 | default: |
16220 | go_unreachable(); | |
16221 | } | |
16222 | } |