]>
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 | |
22984631 | 147 | bool are_identical = Type::are_identical(lhs_type, rhs_type, false, NULL); |
148 | if (!are_identical && 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 | } |
22984631 | 156 | else if (!are_identical && rhs_type->interface_type() != NULL) |
2c809f8f | 157 | return Expression::convert_interface_to_type(lhs_type, rhs, location); |
411eb89e | 158 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 159 | { |
2c809f8f | 160 | // Assigning nil to a slice. |
2c809f8f | 161 | Expression* nil = Expression::make_nil(location); |
e67508fa | 162 | Expression* zero = Expression::make_integer_ul(0, NULL, location); |
2c809f8f | 163 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); |
e440a328 | 164 | } |
165 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 166 | return Expression::make_nil(location); |
22984631 | 167 | else if (are_identical) |
e440a328 | 168 | { |
22984631 | 169 | if (lhs_type->forwarded() != rhs_type->forwarded()) |
170 | { | |
171 | // Different but identical types require an explicit | |
172 | // conversion. This happens with type aliases. | |
173 | return Expression::make_cast(lhs_type, rhs, location); | |
174 | } | |
175 | ||
e440a328 | 176 | // No conversion is needed. |
2c809f8f | 177 | return rhs; |
178 | } | |
179 | else if (lhs_type->points_to() != NULL) | |
180 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
181 | else if (lhs_type->is_numeric_type()) | |
182 | return Expression::make_cast(lhs_type, rhs, location); | |
183 | else if ((lhs_type->struct_type() != NULL | |
184 | && rhs_type->struct_type() != NULL) | |
185 | || (lhs_type->array_type() != NULL | |
186 | && rhs_type->array_type() != NULL)) | |
e440a328 | 187 | { |
188 | // This conversion must be permitted by Go, or we wouldn't have | |
189 | // gotten here. | |
2c809f8f | 190 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 191 | } |
192 | else | |
2c809f8f | 193 | return rhs; |
e440a328 | 194 | } |
195 | ||
2c809f8f | 196 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 197 | // interface type. |
198 | ||
2c809f8f | 199 | Expression* |
200 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
201 | Location location) | |
e440a328 | 202 | { |
e440a328 | 203 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
204 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
205 | ||
206 | // Since RHS_TYPE is a static type, we can create the interface | |
207 | // method table at compile time. | |
208 | ||
209 | // When setting an interface to nil, we just set both fields to | |
210 | // NULL. | |
2c809f8f | 211 | Type* rhs_type = rhs->type(); |
e440a328 | 212 | if (rhs_type->is_nil_type()) |
63697958 | 213 | { |
2c809f8f | 214 | Expression* nil = Expression::make_nil(location); |
215 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 216 | } |
e440a328 | 217 | |
218 | // This should have been checked already. | |
2b8629dd | 219 | if (!lhs_interface_type->implements_interface(rhs_type, NULL)) |
220 | { | |
221 | go_assert(saw_errors()); | |
222 | return Expression::make_error(location); | |
223 | } | |
e440a328 | 224 | |
e440a328 | 225 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
226 | // then the first field is the type descriptor for RHS_TYPE. | |
227 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 228 | Expression* first_field; |
e440a328 | 229 | if (lhs_is_empty) |
2c809f8f | 230 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 231 | else |
232 | { | |
233 | // Build the interface method table for this interface and this | |
234 | // object type: a list of function pointers for each interface | |
235 | // method. | |
236 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 237 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 238 | bool is_pointer = false; |
c0cab2ec | 239 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 240 | { |
241 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 242 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 243 | is_pointer = true; |
244 | } | |
c0cab2ec | 245 | if (rhs_named_type != NULL) |
2c809f8f | 246 | first_field = |
247 | rhs_named_type->interface_method_table(lhs_interface_type, | |
248 | is_pointer); | |
c0cab2ec | 249 | else if (rhs_struct_type != NULL) |
2c809f8f | 250 | first_field = |
251 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
252 | is_pointer); | |
c0cab2ec | 253 | else |
2c809f8f | 254 | first_field = Expression::make_nil(location); |
e440a328 | 255 | } |
e440a328 | 256 | |
2c809f8f | 257 | Expression* obj; |
e440a328 | 258 | if (rhs_type->points_to() != NULL) |
259 | { | |
2c809f8f | 260 | // We are assigning a pointer to the interface; the interface |
e440a328 | 261 | // holds the pointer itself. |
2c809f8f | 262 | obj = rhs; |
263 | } | |
264 | else | |
265 | { | |
266 | // We are assigning a non-pointer value to the interface; the | |
45ff893b | 267 | // interface gets a copy of the value in the heap if it escapes. |
268 | // TODO(cmang): Associate escape state state of RHS with newly | |
269 | // created OBJ. | |
2c809f8f | 270 | obj = Expression::make_heap_expression(rhs, location); |
e440a328 | 271 | } |
272 | ||
2c809f8f | 273 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
274 | } | |
e440a328 | 275 | |
2c809f8f | 276 | // Return an expression for the type descriptor of RHS. |
e440a328 | 277 | |
2c809f8f | 278 | Expression* |
279 | Expression::get_interface_type_descriptor(Expression* rhs) | |
280 | { | |
281 | go_assert(rhs->type()->interface_type() != NULL); | |
282 | Location location = rhs->location(); | |
e440a328 | 283 | |
2c809f8f | 284 | // The type descriptor is the first field of an empty interface. |
285 | if (rhs->type()->interface_type()->is_empty()) | |
286 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
287 | location); | |
288 | ||
289 | Expression* mtable = | |
290 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 291 | |
2c809f8f | 292 | Expression* descriptor = |
f614ea8b | 293 | Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, location); |
2c809f8f | 294 | descriptor = Expression::make_field_reference(descriptor, 0, location); |
295 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 296 | |
2c809f8f | 297 | Expression* eq = |
298 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
299 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 300 | } |
301 | ||
2c809f8f | 302 | // Return an expression for the conversion of an interface type to an |
e440a328 | 303 | // interface type. |
304 | ||
2c809f8f | 305 | Expression* |
306 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
307 | bool for_type_guard, | |
308 | Location location) | |
e440a328 | 309 | { |
8ba8cc87 | 310 | if (Type::are_identical(lhs_type, rhs->type(), false, NULL)) |
311 | return rhs; | |
312 | ||
e440a328 | 313 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
314 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
315 | ||
e440a328 | 316 | // In the general case this requires runtime examination of the type |
317 | // method table to match it up with the interface methods. | |
318 | ||
319 | // FIXME: If all of the methods in the right hand side interface | |
320 | // also appear in the left hand side interface, then we don't need | |
321 | // to do a runtime check, although we still need to build a new | |
322 | // method table. | |
323 | ||
8ba8cc87 | 324 | // We are going to evaluate RHS multiple times. |
325 | go_assert(rhs->is_variable()); | |
326 | ||
e440a328 | 327 | // Get the type descriptor for the right hand side. This will be |
328 | // NULL for a nil interface. | |
2c809f8f | 329 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
330 | Expression* lhs_type_expr = | |
331 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 332 | |
2c809f8f | 333 | Expression* first_field; |
e440a328 | 334 | if (for_type_guard) |
335 | { | |
336 | // A type assertion fails when converting a nil interface. | |
6098d6cb | 337 | first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2, |
338 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 339 | } |
340 | else if (lhs_is_empty) | |
341 | { | |
2c809f8f | 342 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 343 | // first field is just the type descriptor of the object. |
2c809f8f | 344 | first_field = rhs_type_expr; |
e440a328 | 345 | } |
346 | else | |
347 | { | |
348 | // A conversion to a non-empty interface may fail, but unlike a | |
349 | // type assertion converting nil will always succeed. | |
6098d6cb | 350 | first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2, |
351 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 352 | } |
353 | ||
354 | // The second field is simply the object pointer. | |
2c809f8f | 355 | Expression* obj = |
356 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
357 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 358 | } |
359 | ||
2c809f8f | 360 | // Return an expression for the conversion of an interface type to a |
e440a328 | 361 | // non-interface type. |
362 | ||
2c809f8f | 363 | Expression* |
364 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
365 | Location location) | |
e440a328 | 366 | { |
8ba8cc87 | 367 | // We are going to evaluate RHS multiple times. |
368 | go_assert(rhs->is_variable()); | |
369 | ||
e440a328 | 370 | // Call a function to check that the type is valid. The function |
371 | // will panic with an appropriate runtime type error if the type is | |
372 | // not valid. | |
2c809f8f | 373 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
374 | location); | |
375 | Expression* rhs_descriptor = | |
376 | Expression::get_interface_type_descriptor(rhs); | |
377 | ||
378 | Type* rhs_type = rhs->type(); | |
379 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
380 | location); | |
381 | ||
6098d6cb | 382 | Expression* check_iface = Runtime::make_call(Runtime::ASSERTI2T, |
2c809f8f | 383 | location, 3, lhs_type_expr, |
384 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 385 | |
386 | // If the call succeeds, pull out the value. | |
2c809f8f | 387 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
388 | location); | |
e440a328 | 389 | |
390 | // If the value is a pointer, then it is the value we want. | |
391 | // Otherwise it points to the value. | |
392 | if (lhs_type->points_to() == NULL) | |
393 | { | |
2c809f8f | 394 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
395 | location); | |
f614ea8b | 396 | obj = Expression::make_dereference(obj, NIL_CHECK_NOT_NEEDED, |
397 | location); | |
e440a328 | 398 | } |
2c809f8f | 399 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 400 | } |
401 | ||
ea664253 | 402 | // Convert an expression to its backend representation. This is implemented by |
403 | // the child class. Not that it is not in general safe to call this multiple | |
e440a328 | 404 | // times for a single expression, but that we don't catch such errors. |
405 | ||
ea664253 | 406 | Bexpression* |
407 | Expression::get_backend(Translate_context* context) | |
e440a328 | 408 | { |
409 | // The child may have marked this expression as having an error. | |
410 | if (this->classification_ == EXPRESSION_ERROR) | |
ea664253 | 411 | return context->backend()->error_expression(); |
e440a328 | 412 | |
ea664253 | 413 | return this->do_get_backend(context); |
e440a328 | 414 | } |
415 | ||
48c2a53a | 416 | // Return a backend expression for VAL. |
417 | Bexpression* | |
418 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
419 | Numeric_constant* val) | |
e440a328 | 420 | { |
48c2a53a | 421 | Gogo* gogo = context->gogo(); |
422 | Type* type = val->type(); | |
423 | if (type == NULL) | |
424 | return gogo->backend()->error_expression(); | |
e440a328 | 425 | |
48c2a53a | 426 | Btype* btype = type->get_backend(gogo); |
427 | Bexpression* ret; | |
428 | if (type->integer_type() != NULL) | |
e440a328 | 429 | { |
430 | mpz_t ival; | |
48c2a53a | 431 | if (!val->to_int(&ival)) |
432 | { | |
433 | go_assert(saw_errors()); | |
434 | return gogo->backend()->error_expression(); | |
435 | } | |
436 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 437 | mpz_clear(ival); |
e440a328 | 438 | } |
48c2a53a | 439 | else if (type->float_type() != NULL) |
e440a328 | 440 | { |
48c2a53a | 441 | mpfr_t fval; |
442 | if (!val->to_float(&fval)) | |
443 | { | |
444 | go_assert(saw_errors()); | |
445 | return gogo->backend()->error_expression(); | |
446 | } | |
447 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
448 | mpfr_clear(fval); | |
e440a328 | 449 | } |
48c2a53a | 450 | else if (type->complex_type() != NULL) |
e440a328 | 451 | { |
fcbea5e4 | 452 | mpc_t cval; |
453 | if (!val->to_complex(&cval)) | |
48c2a53a | 454 | { |
455 | go_assert(saw_errors()); | |
456 | return gogo->backend()->error_expression(); | |
457 | } | |
fcbea5e4 | 458 | ret = gogo->backend()->complex_constant_expression(btype, cval); |
459 | mpc_clear(cval); | |
e440a328 | 460 | } |
461 | else | |
c3e6f413 | 462 | go_unreachable(); |
e440a328 | 463 | |
48c2a53a | 464 | return ret; |
e440a328 | 465 | } |
466 | ||
2c809f8f | 467 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
468 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 469 | |
2c809f8f | 470 | Expression* |
471 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 472 | { |
2c809f8f | 473 | Type* val_type = val->type(); |
474 | Type* bound_type = Type::lookup_integer_type("int"); | |
475 | ||
476 | int val_type_size; | |
477 | bool val_is_unsigned = false; | |
478 | if (val_type->integer_type() != NULL) | |
479 | { | |
480 | val_type_size = val_type->integer_type()->bits(); | |
481 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
482 | } | |
483 | else | |
484 | { | |
485 | if (!val_type->is_numeric_type() | |
486 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
487 | { | |
488 | go_assert(saw_errors()); | |
489 | return Expression::make_boolean(true, loc); | |
490 | } | |
e440a328 | 491 | |
2c809f8f | 492 | if (val_type->complex_type() != NULL) |
493 | val_type_size = val_type->complex_type()->bits(); | |
494 | else | |
495 | val_type_size = val_type->float_type()->bits(); | |
496 | } | |
497 | ||
498 | Expression* negative_index = Expression::make_boolean(false, loc); | |
499 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
500 | if (!val_is_unsigned) | |
e440a328 | 501 | { |
e67508fa | 502 | Expression* zero = Expression::make_integer_ul(0, val_type, loc); |
2c809f8f | 503 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); |
e440a328 | 504 | } |
505 | ||
2c809f8f | 506 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 507 | if (val_type_size > bound_type_size |
508 | || (val_type_size == bound_type_size | |
2c809f8f | 509 | && val_is_unsigned)) |
510 | { | |
511 | mpz_t one; | |
512 | mpz_init_set_ui(one, 1UL); | |
513 | ||
514 | // maxval = 2^(bound_type_size - 1) - 1 | |
515 | mpz_t maxval; | |
516 | mpz_init(maxval); | |
517 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
518 | mpz_sub_ui(maxval, maxval, 1); | |
e67508fa | 519 | Expression* max = Expression::make_integer_z(&maxval, val_type, loc); |
2c809f8f | 520 | mpz_clear(one); |
521 | mpz_clear(maxval); | |
522 | ||
523 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 524 | } |
525 | ||
2c809f8f | 526 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
527 | loc); | |
e440a328 | 528 | } |
529 | ||
d751bb78 | 530 | void |
531 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
532 | { | |
533 | this->do_dump_expression(ast_dump_context); | |
534 | } | |
535 | ||
e440a328 | 536 | // Error expressions. This are used to avoid cascading errors. |
537 | ||
538 | class Error_expression : public Expression | |
539 | { | |
540 | public: | |
b13c66cd | 541 | Error_expression(Location location) |
e440a328 | 542 | : Expression(EXPRESSION_ERROR, location) |
543 | { } | |
544 | ||
545 | protected: | |
546 | bool | |
547 | do_is_constant() const | |
548 | { return true; } | |
549 | ||
550 | bool | |
0c77715b | 551 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 552 | { |
0c77715b | 553 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 554 | return true; |
555 | } | |
556 | ||
4f2138d7 | 557 | bool |
e440a328 | 558 | do_discarding_value() |
4f2138d7 | 559 | { return true; } |
e440a328 | 560 | |
561 | Type* | |
562 | do_type() | |
563 | { return Type::make_error_type(); } | |
564 | ||
565 | void | |
566 | do_determine_type(const Type_context*) | |
567 | { } | |
568 | ||
569 | Expression* | |
570 | do_copy() | |
571 | { return this; } | |
572 | ||
573 | bool | |
574 | do_is_addressable() const | |
575 | { return true; } | |
576 | ||
ea664253 | 577 | Bexpression* |
578 | do_get_backend(Translate_context* context) | |
579 | { return context->backend()->error_expression(); } | |
d751bb78 | 580 | |
581 | void | |
582 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 583 | }; |
584 | ||
d751bb78 | 585 | // Dump the ast representation for an error expression to a dump context. |
586 | ||
587 | void | |
588 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
589 | { | |
590 | ast_dump_context->ostream() << "_Error_" ; | |
591 | } | |
592 | ||
e440a328 | 593 | Expression* |
b13c66cd | 594 | Expression::make_error(Location location) |
e440a328 | 595 | { |
596 | return new Error_expression(location); | |
597 | } | |
598 | ||
599 | // An expression which is really a type. This is used during parsing. | |
600 | // It is an error if these survive after lowering. | |
601 | ||
602 | class | |
603 | Type_expression : public Expression | |
604 | { | |
605 | public: | |
b13c66cd | 606 | Type_expression(Type* type, Location location) |
e440a328 | 607 | : Expression(EXPRESSION_TYPE, location), |
608 | type_(type) | |
609 | { } | |
610 | ||
611 | protected: | |
612 | int | |
613 | do_traverse(Traverse* traverse) | |
614 | { return Type::traverse(this->type_, traverse); } | |
615 | ||
616 | Type* | |
617 | do_type() | |
618 | { return this->type_; } | |
619 | ||
620 | void | |
621 | do_determine_type(const Type_context*) | |
622 | { } | |
623 | ||
624 | void | |
625 | do_check_types(Gogo*) | |
626 | { this->report_error(_("invalid use of type")); } | |
627 | ||
628 | Expression* | |
629 | do_copy() | |
630 | { return this; } | |
631 | ||
ea664253 | 632 | Bexpression* |
633 | do_get_backend(Translate_context*) | |
c3e6f413 | 634 | { go_unreachable(); } |
e440a328 | 635 | |
d751bb78 | 636 | void do_dump_expression(Ast_dump_context*) const; |
637 | ||
e440a328 | 638 | private: |
639 | // The type which we are representing as an expression. | |
640 | Type* type_; | |
641 | }; | |
642 | ||
d751bb78 | 643 | void |
644 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
645 | { | |
646 | ast_dump_context->dump_type(this->type_); | |
647 | } | |
648 | ||
e440a328 | 649 | Expression* |
b13c66cd | 650 | Expression::make_type(Type* type, Location location) |
e440a328 | 651 | { |
652 | return new Type_expression(type, location); | |
653 | } | |
654 | ||
e03bdf36 | 655 | // Class Parser_expression. |
656 | ||
657 | Type* | |
658 | Parser_expression::do_type() | |
659 | { | |
660 | // We should never really ask for the type of a Parser_expression. | |
661 | // However, it can happen, at least when we have an invalid const | |
662 | // whose initializer refers to the const itself. In that case we | |
663 | // may ask for the type when lowering the const itself. | |
c484d925 | 664 | go_assert(saw_errors()); |
e03bdf36 | 665 | return Type::make_error_type(); |
666 | } | |
667 | ||
e440a328 | 668 | // Class Var_expression. |
669 | ||
670 | // Lower a variable expression. Here we just make sure that the | |
671 | // initialization expression of the variable has been lowered. This | |
672 | // ensures that we will be able to determine the type of the variable | |
673 | // if necessary. | |
674 | ||
675 | Expression* | |
ceeb4318 | 676 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
677 | Statement_inserter* inserter, int) | |
e440a328 | 678 | { |
679 | if (this->variable_->is_variable()) | |
680 | { | |
681 | Variable* var = this->variable_->var_value(); | |
682 | // This is either a local variable or a global variable. A | |
683 | // reference to a variable which is local to an enclosing | |
684 | // function will be a reference to a field in a closure. | |
685 | if (var->is_global()) | |
ceeb4318 | 686 | { |
687 | function = NULL; | |
688 | inserter = NULL; | |
689 | } | |
690 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 691 | } |
692 | return this; | |
693 | } | |
694 | ||
e440a328 | 695 | // Return the type of a reference to a variable. |
696 | ||
697 | Type* | |
698 | Var_expression::do_type() | |
699 | { | |
700 | if (this->variable_->is_variable()) | |
701 | return this->variable_->var_value()->type(); | |
702 | else if (this->variable_->is_result_variable()) | |
703 | return this->variable_->result_var_value()->type(); | |
704 | else | |
c3e6f413 | 705 | go_unreachable(); |
e440a328 | 706 | } |
707 | ||
0ab09e06 | 708 | // Determine the type of a reference to a variable. |
709 | ||
710 | void | |
711 | Var_expression::do_determine_type(const Type_context*) | |
712 | { | |
713 | if (this->variable_->is_variable()) | |
714 | this->variable_->var_value()->determine_type(); | |
715 | } | |
716 | ||
e440a328 | 717 | // Something takes the address of this variable. This means that we |
718 | // may want to move the variable onto the heap. | |
719 | ||
720 | void | |
721 | Var_expression::do_address_taken(bool escapes) | |
722 | { | |
723 | if (!escapes) | |
f325319b | 724 | { |
725 | if (this->variable_->is_variable()) | |
726 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
727 | else if (this->variable_->is_result_variable()) | |
728 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
729 | else | |
730 | go_unreachable(); | |
731 | } | |
e440a328 | 732 | else |
f325319b | 733 | { |
734 | if (this->variable_->is_variable()) | |
735 | this->variable_->var_value()->set_address_taken(); | |
736 | else if (this->variable_->is_result_variable()) | |
737 | this->variable_->result_var_value()->set_address_taken(); | |
738 | else | |
739 | go_unreachable(); | |
740 | } | |
45ff893b | 741 | |
742 | if (this->variable_->is_variable() | |
743 | && this->variable_->var_value()->is_in_heap()) | |
744 | { | |
745 | Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP); | |
746 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
747 | } | |
e440a328 | 748 | } |
749 | ||
ea664253 | 750 | // Get the backend representation for a reference to a variable. |
e440a328 | 751 | |
ea664253 | 752 | Bexpression* |
753 | Var_expression::do_get_backend(Translate_context* context) | |
e440a328 | 754 | { |
fe2f84cf | 755 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
756 | context->function()); | |
fe2f84cf | 757 | bool is_in_heap; |
c6777780 | 758 | Location loc = this->location(); |
9b27b43c | 759 | Btype* btype; |
760 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 761 | if (this->variable_->is_variable()) |
9b27b43c | 762 | { |
763 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
764 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
765 | } | |
fe2f84cf | 766 | else if (this->variable_->is_result_variable()) |
9b27b43c | 767 | { |
768 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
769 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
770 | } | |
fe2f84cf | 771 | else |
c3e6f413 | 772 | go_unreachable(); |
c6777780 | 773 | |
d4e6573e | 774 | Bexpression* ret = |
7af8e400 | 775 | context->backend()->var_expression(bvar, loc); |
fe2f84cf | 776 | if (is_in_heap) |
9b27b43c | 777 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
ea664253 | 778 | return ret; |
e440a328 | 779 | } |
780 | ||
d751bb78 | 781 | // Ast dump for variable expression. |
782 | ||
783 | void | |
784 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
785 | { | |
cd5e24c1 | 786 | ast_dump_context->ostream() << this->variable_->message_name() ; |
d751bb78 | 787 | } |
788 | ||
e440a328 | 789 | // Make a reference to a variable in an expression. |
790 | ||
791 | Expression* | |
b13c66cd | 792 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 793 | { |
794 | if (var->is_sink()) | |
795 | return Expression::make_sink(location); | |
796 | ||
797 | // FIXME: Creating a new object for each reference to a variable is | |
798 | // wasteful. | |
799 | return new Var_expression(var, location); | |
800 | } | |
801 | ||
b0c09712 | 802 | // Class Enclosed_var_expression. |
803 | ||
804 | int | |
805 | Enclosed_var_expression::do_traverse(Traverse*) | |
806 | { | |
807 | return TRAVERSE_CONTINUE; | |
808 | } | |
809 | ||
810 | // Lower the reference to the enclosed variable. | |
811 | ||
812 | Expression* | |
813 | Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function, | |
814 | Statement_inserter* inserter, int) | |
815 | { | |
816 | gogo->lower_expression(function, inserter, &this->reference_); | |
817 | return this; | |
818 | } | |
819 | ||
820 | // Flatten the reference to the enclosed variable. | |
821 | ||
822 | Expression* | |
823 | Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function, | |
824 | Statement_inserter* inserter) | |
825 | { | |
826 | gogo->flatten_expression(function, inserter, &this->reference_); | |
827 | return this; | |
828 | } | |
829 | ||
830 | void | |
831 | Enclosed_var_expression::do_address_taken(bool escapes) | |
832 | { | |
833 | if (!escapes) | |
834 | { | |
835 | if (this->variable_->is_variable()) | |
836 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
837 | else if (this->variable_->is_result_variable()) | |
838 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
839 | else | |
840 | go_unreachable(); | |
841 | } | |
842 | else | |
843 | { | |
844 | if (this->variable_->is_variable()) | |
845 | this->variable_->var_value()->set_address_taken(); | |
846 | else if (this->variable_->is_result_variable()) | |
847 | this->variable_->result_var_value()->set_address_taken(); | |
848 | else | |
849 | go_unreachable(); | |
850 | } | |
45ff893b | 851 | |
852 | if (this->variable_->is_variable() | |
853 | && this->variable_->var_value()->is_in_heap()) | |
854 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
b0c09712 | 855 | } |
856 | ||
857 | // Ast dump for enclosed variable expression. | |
858 | ||
859 | void | |
860 | Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const | |
861 | { | |
cd5e24c1 | 862 | adc->ostream() << this->variable_->message_name(); |
b0c09712 | 863 | } |
864 | ||
865 | // Make a reference to a variable within an enclosing function. | |
866 | ||
867 | Expression* | |
868 | Expression::make_enclosing_var_reference(Expression* reference, | |
869 | Named_object* var, Location location) | |
870 | { | |
871 | return new Enclosed_var_expression(reference, var, location); | |
872 | } | |
873 | ||
e440a328 | 874 | // Class Temporary_reference_expression. |
875 | ||
876 | // The type. | |
877 | ||
878 | Type* | |
879 | Temporary_reference_expression::do_type() | |
880 | { | |
881 | return this->statement_->type(); | |
882 | } | |
883 | ||
884 | // Called if something takes the address of this temporary variable. | |
885 | // We never have to move temporary variables to the heap, but we do | |
886 | // need to know that they must live in the stack rather than in a | |
887 | // register. | |
888 | ||
889 | void | |
890 | Temporary_reference_expression::do_address_taken(bool) | |
891 | { | |
892 | this->statement_->set_is_address_taken(); | |
893 | } | |
894 | ||
ea664253 | 895 | // Get a backend expression referring to the variable. |
e440a328 | 896 | |
ea664253 | 897 | Bexpression* |
898 | Temporary_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 899 | { |
cd440cff | 900 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 901 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
7af8e400 | 902 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 903 | |
cd440cff | 904 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 905 | // that the Go frontend can. In some cases this means that a |
906 | // temporary variable won't have the right backend type. Correct | |
907 | // that here by adding a type cast. We need to use base() to push | |
908 | // the circularity down one level. | |
cd440cff | 909 | Type* stype = this->statement_->type(); |
ceeb4318 | 910 | if (!this->is_lvalue_ |
03118c21 | 911 | && stype->points_to() != NULL |
912 | && stype->points_to()->is_void_type()) | |
eefc1ed3 | 913 | { |
cd440cff | 914 | Btype* btype = this->type()->base()->get_backend(gogo); |
915 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 916 | } |
ea664253 | 917 | return ret; |
e440a328 | 918 | } |
919 | ||
d751bb78 | 920 | // Ast dump for temporary reference. |
921 | ||
922 | void | |
923 | Temporary_reference_expression::do_dump_expression( | |
924 | Ast_dump_context* ast_dump_context) const | |
925 | { | |
926 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
927 | } | |
928 | ||
e440a328 | 929 | // Make a reference to a temporary variable. |
930 | ||
ceeb4318 | 931 | Temporary_reference_expression* |
e440a328 | 932 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 933 | Location location) |
e440a328 | 934 | { |
935 | return new Temporary_reference_expression(statement, location); | |
936 | } | |
937 | ||
e9d3367e | 938 | // Class Set_and_use_temporary_expression. |
939 | ||
940 | // Return the type. | |
941 | ||
942 | Type* | |
943 | Set_and_use_temporary_expression::do_type() | |
944 | { | |
945 | return this->statement_->type(); | |
946 | } | |
947 | ||
0afbb937 | 948 | // Determine the type of the expression. |
949 | ||
950 | void | |
951 | Set_and_use_temporary_expression::do_determine_type( | |
952 | const Type_context* context) | |
953 | { | |
954 | this->expr_->determine_type(context); | |
955 | } | |
956 | ||
e9d3367e | 957 | // Take the address. |
958 | ||
959 | void | |
960 | Set_and_use_temporary_expression::do_address_taken(bool) | |
961 | { | |
962 | this->statement_->set_is_address_taken(); | |
963 | } | |
964 | ||
965 | // Return the backend representation. | |
966 | ||
ea664253 | 967 | Bexpression* |
968 | Set_and_use_temporary_expression::do_get_backend(Translate_context* context) | |
e9d3367e | 969 | { |
e9d3367e | 970 | Location loc = this->location(); |
a302c105 | 971 | Gogo* gogo = context->gogo(); |
972 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
7af8e400 | 973 | Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, loc); |
a302c105 | 974 | |
0ab48656 | 975 | Named_object* fn = context->function(); |
976 | go_assert(fn != NULL); | |
977 | Bfunction* bfn = fn->func_value()->get_or_make_decl(gogo, fn); | |
ea664253 | 978 | Bexpression* bexpr = this->expr_->get_backend(context); |
0ab48656 | 979 | Bstatement* set = gogo->backend()->assignment_statement(bfn, lvar_ref, |
980 | bexpr, loc); | |
7af8e400 | 981 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); |
a302c105 | 982 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); |
ea664253 | 983 | return ret; |
e9d3367e | 984 | } |
985 | ||
986 | // Dump. | |
987 | ||
988 | void | |
989 | Set_and_use_temporary_expression::do_dump_expression( | |
990 | Ast_dump_context* ast_dump_context) const | |
991 | { | |
992 | ast_dump_context->ostream() << '('; | |
993 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
994 | ast_dump_context->ostream() << " = "; | |
995 | this->expr_->dump_expression(ast_dump_context); | |
996 | ast_dump_context->ostream() << ')'; | |
997 | } | |
998 | ||
999 | // Make a set-and-use temporary. | |
1000 | ||
1001 | Set_and_use_temporary_expression* | |
1002 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
1003 | Expression* expr, Location location) | |
1004 | { | |
1005 | return new Set_and_use_temporary_expression(statement, expr, location); | |
1006 | } | |
1007 | ||
e440a328 | 1008 | // A sink expression--a use of the blank identifier _. |
1009 | ||
1010 | class Sink_expression : public Expression | |
1011 | { | |
1012 | public: | |
b13c66cd | 1013 | Sink_expression(Location location) |
e440a328 | 1014 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 1015 | type_(NULL), bvar_(NULL) |
e440a328 | 1016 | { } |
1017 | ||
1018 | protected: | |
4f2138d7 | 1019 | bool |
e440a328 | 1020 | do_discarding_value() |
4f2138d7 | 1021 | { return true; } |
e440a328 | 1022 | |
1023 | Type* | |
1024 | do_type(); | |
1025 | ||
1026 | void | |
1027 | do_determine_type(const Type_context*); | |
1028 | ||
1029 | Expression* | |
1030 | do_copy() | |
1031 | { return new Sink_expression(this->location()); } | |
1032 | ||
ea664253 | 1033 | Bexpression* |
1034 | do_get_backend(Translate_context*); | |
e440a328 | 1035 | |
d751bb78 | 1036 | void |
1037 | do_dump_expression(Ast_dump_context*) const; | |
1038 | ||
e440a328 | 1039 | private: |
1040 | // The type of this sink variable. | |
1041 | Type* type_; | |
1042 | // The temporary variable we generate. | |
aa93217a | 1043 | Bvariable* bvar_; |
e440a328 | 1044 | }; |
1045 | ||
1046 | // Return the type of a sink expression. | |
1047 | ||
1048 | Type* | |
1049 | Sink_expression::do_type() | |
1050 | { | |
1051 | if (this->type_ == NULL) | |
1052 | return Type::make_sink_type(); | |
1053 | return this->type_; | |
1054 | } | |
1055 | ||
1056 | // Determine the type of a sink expression. | |
1057 | ||
1058 | void | |
1059 | Sink_expression::do_determine_type(const Type_context* context) | |
1060 | { | |
1061 | if (context->type != NULL) | |
1062 | this->type_ = context->type; | |
1063 | } | |
1064 | ||
1065 | // Return a temporary variable for a sink expression. This will | |
1066 | // presumably be a write-only variable which the middle-end will drop. | |
1067 | ||
ea664253 | 1068 | Bexpression* |
1069 | Sink_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1070 | { |
aa93217a | 1071 | Location loc = this->location(); |
1072 | Gogo* gogo = context->gogo(); | |
1073 | if (this->bvar_ == NULL) | |
e440a328 | 1074 | { |
c484d925 | 1075 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 1076 | Named_object* fn = context->function(); |
1077 | go_assert(fn != NULL); | |
1078 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 1079 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 1080 | Bstatement* decl; |
1081 | this->bvar_ = | |
1082 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
1083 | false, loc, &decl); | |
d4e6573e | 1084 | Bexpression* var_ref = |
7af8e400 | 1085 | gogo->backend()->var_expression(this->bvar_, loc); |
aa93217a | 1086 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); |
ea664253 | 1087 | return var_ref; |
e440a328 | 1088 | } |
7af8e400 | 1089 | return gogo->backend()->var_expression(this->bvar_, loc); |
e440a328 | 1090 | } |
1091 | ||
d751bb78 | 1092 | // Ast dump for sink expression. |
1093 | ||
1094 | void | |
1095 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1096 | { | |
1097 | ast_dump_context->ostream() << "_" ; | |
1098 | } | |
1099 | ||
e440a328 | 1100 | // Make a sink expression. |
1101 | ||
1102 | Expression* | |
b13c66cd | 1103 | Expression::make_sink(Location location) |
e440a328 | 1104 | { |
1105 | return new Sink_expression(location); | |
1106 | } | |
1107 | ||
1108 | // Class Func_expression. | |
1109 | ||
1110 | // FIXME: Can a function expression appear in a constant expression? | |
1111 | // The value is unchanging. Initializing a constant to the address of | |
1112 | // a function seems like it could work, though there might be little | |
1113 | // point to it. | |
1114 | ||
e440a328 | 1115 | // Traversal. |
1116 | ||
1117 | int | |
1118 | Func_expression::do_traverse(Traverse* traverse) | |
1119 | { | |
1120 | return (this->closure_ == NULL | |
1121 | ? TRAVERSE_CONTINUE | |
1122 | : Expression::traverse(&this->closure_, traverse)); | |
1123 | } | |
1124 | ||
1125 | // Return the type of a function expression. | |
1126 | ||
1127 | Type* | |
1128 | Func_expression::do_type() | |
1129 | { | |
1130 | if (this->function_->is_function()) | |
1131 | return this->function_->func_value()->type(); | |
1132 | else if (this->function_->is_function_declaration()) | |
1133 | return this->function_->func_declaration_value()->type(); | |
1134 | else | |
c3e6f413 | 1135 | go_unreachable(); |
e440a328 | 1136 | } |
1137 | ||
ea664253 | 1138 | // Get the backend representation for the code of a function expression. |
e440a328 | 1139 | |
97267c39 | 1140 | Bexpression* |
8381eda7 | 1141 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1142 | { |
1143 | Function_type* fntype; | |
8381eda7 | 1144 | if (no->is_function()) |
1145 | fntype = no->func_value()->type(); | |
1146 | else if (no->is_function_declaration()) | |
1147 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1148 | else |
c3e6f413 | 1149 | go_unreachable(); |
e440a328 | 1150 | |
1151 | // Builtin functions are handled specially by Call_expression. We | |
1152 | // can't take their address. | |
1153 | if (fntype->is_builtin()) | |
1154 | { | |
631d5788 | 1155 | go_error_at(loc, |
1156 | "invalid use of special builtin function %qs; must be called", | |
1157 | no->message_name().c_str()); | |
97267c39 | 1158 | return gogo->backend()->error_expression(); |
e440a328 | 1159 | } |
1160 | ||
97267c39 | 1161 | Bfunction* fndecl; |
e440a328 | 1162 | if (no->is_function()) |
cf3cae55 | 1163 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1164 | else if (no->is_function_declaration()) |
cf3cae55 | 1165 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1166 | else |
c3e6f413 | 1167 | go_unreachable(); |
e440a328 | 1168 | |
97267c39 | 1169 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1170 | } |
1171 | ||
ea664253 | 1172 | // Get the backend representation for a function expression. This is used when |
1173 | // we take the address of a function rather than simply calling it. A func | |
8381eda7 | 1174 | // value is represented as a pointer to a block of memory. The first |
1175 | // word of that memory is a pointer to the function code. The | |
1176 | // remaining parts of that memory are the addresses of variables that | |
1177 | // the function closes over. | |
e440a328 | 1178 | |
ea664253 | 1179 | Bexpression* |
1180 | Func_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1181 | { |
8381eda7 | 1182 | // If there is no closure, just use the function descriptor. |
2010c17a | 1183 | if (this->closure_ == NULL) |
8381eda7 | 1184 | { |
1185 | Gogo* gogo = context->gogo(); | |
1186 | Named_object* no = this->function_; | |
1187 | Expression* descriptor; | |
1188 | if (no->is_function()) | |
1189 | descriptor = no->func_value()->descriptor(gogo, no); | |
1190 | else if (no->is_function_declaration()) | |
1191 | { | |
1192 | if (no->func_declaration_value()->type()->is_builtin()) | |
1193 | { | |
631d5788 | 1194 | go_error_at(this->location(), |
1195 | ("invalid use of special builtin function %qs; " | |
1196 | "must be called"), | |
1197 | no->message_name().c_str()); | |
ea664253 | 1198 | return gogo->backend()->error_expression(); |
8381eda7 | 1199 | } |
1200 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1201 | } | |
1202 | else | |
1203 | go_unreachable(); | |
2010c17a | 1204 | |
ea664253 | 1205 | Bexpression* bdesc = descriptor->get_backend(context); |
1206 | return gogo->backend()->address_expression(bdesc, this->location()); | |
8381eda7 | 1207 | } |
e440a328 | 1208 | |
8381eda7 | 1209 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1210 | |
8381eda7 | 1211 | // If there is a closure, then the closure is itself the function |
1212 | // expression. It is a pointer to a struct whose first field points | |
1213 | // to the function code and whose remaining fields are the addresses | |
1214 | // of the closed-over variables. | |
76818e19 | 1215 | Bexpression *bexpr = this->closure_->get_backend(context); |
1216 | ||
1217 | // Introduce a backend type conversion, to account for any differences | |
1218 | // between the argument type (function descriptor, struct with a | |
1219 | // single field) and the closure (struct with multiple fields). | |
1220 | Gogo* gogo = context->gogo(); | |
1221 | Btype *btype = this->type()->get_backend(gogo); | |
1222 | return gogo->backend()->convert_expression(btype, bexpr, this->location()); | |
e440a328 | 1223 | } |
1224 | ||
d751bb78 | 1225 | // Ast dump for function. |
1226 | ||
1227 | void | |
1228 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1229 | { | |
8b1c301d | 1230 | ast_dump_context->ostream() << this->function_->name(); |
1231 | if (this->closure_ != NULL) | |
1232 | { | |
1233 | ast_dump_context->ostream() << " {closure = "; | |
1234 | this->closure_->dump_expression(ast_dump_context); | |
1235 | ast_dump_context->ostream() << "}"; | |
1236 | } | |
d751bb78 | 1237 | } |
1238 | ||
e440a328 | 1239 | // Make a reference to a function in an expression. |
1240 | ||
1241 | Expression* | |
1242 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1243 | Location location) |
e440a328 | 1244 | { |
b1d7ecfa | 1245 | Func_expression* fe = new Func_expression(function, closure, location); |
1246 | ||
1247 | // Detect references to builtin functions and set the runtime code if | |
1248 | // appropriate. | |
1249 | if (function->is_function_declaration()) | |
1250 | fe->set_runtime_code(Runtime::name_to_code(function->name())); | |
1251 | return fe; | |
e440a328 | 1252 | } |
1253 | ||
c6837989 | 1254 | // Class Func_descriptor_expression. |
8381eda7 | 1255 | |
c6837989 | 1256 | // Constructor. |
8381eda7 | 1257 | |
c6837989 | 1258 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1259 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1260 | fn_(fn), dvar_(NULL) |
c6837989 | 1261 | { |
1262 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1263 | } | |
8381eda7 | 1264 | |
c6837989 | 1265 | // Traversal. |
8381eda7 | 1266 | |
c6837989 | 1267 | int |
1268 | Func_descriptor_expression::do_traverse(Traverse*) | |
1269 | { | |
1270 | return TRAVERSE_CONTINUE; | |
1271 | } | |
8381eda7 | 1272 | |
1273 | // All function descriptors have the same type. | |
1274 | ||
1275 | Type* Func_descriptor_expression::descriptor_type; | |
1276 | ||
1277 | void | |
1278 | Func_descriptor_expression::make_func_descriptor_type() | |
1279 | { | |
1280 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1281 | return; | |
1282 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1283 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1284 | Func_descriptor_expression::descriptor_type = | |
1285 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1286 | } | |
1287 | ||
1288 | Type* | |
1289 | Func_descriptor_expression::do_type() | |
1290 | { | |
1291 | Func_descriptor_expression::make_func_descriptor_type(); | |
1292 | return Func_descriptor_expression::descriptor_type; | |
1293 | } | |
1294 | ||
ea664253 | 1295 | // The backend representation for a function descriptor. |
8381eda7 | 1296 | |
ea664253 | 1297 | Bexpression* |
1298 | Func_descriptor_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1299 | { |
8381eda7 | 1300 | Named_object* no = this->fn_; |
1301 | Location loc = no->location(); | |
ea664253 | 1302 | if (this->dvar_ != NULL) |
7af8e400 | 1303 | return context->backend()->var_expression(this->dvar_, loc); |
8381eda7 | 1304 | |
ea664253 | 1305 | Gogo* gogo = context->gogo(); |
19272321 | 1306 | std::string var_name(gogo->function_descriptor_name(no)); |
09e57698 | 1307 | bool is_descriptor = false; |
1308 | if (no->is_function_declaration() | |
1309 | && !no->func_declaration_value()->asm_name().empty() | |
1310 | && Linemap::is_predeclared_location(no->location())) | |
19272321 | 1311 | is_descriptor = true; |
8381eda7 | 1312 | |
13f2fdb8 | 1313 | // The runtime package implements some functions defined in the |
1314 | // syscall package. Let the syscall package define the descriptor | |
1315 | // in this case. | |
1316 | if (gogo->compiling_runtime() | |
1317 | && gogo->package_name() == "runtime" | |
1318 | && no->is_function() | |
1319 | && !no->func_value()->asm_name().empty() | |
1320 | && no->func_value()->asm_name().compare(0, 8, "syscall.") == 0) | |
1321 | is_descriptor = true; | |
1322 | ||
8381eda7 | 1323 | Btype* btype = this->type()->get_backend(gogo); |
1324 | ||
1325 | Bvariable* bvar; | |
438b4bec | 1326 | std::string asm_name(go_selectively_encode_id(var_name)); |
09e57698 | 1327 | if (no->package() != NULL || is_descriptor) |
438b4bec | 1328 | bvar = context->backend()->immutable_struct_reference(var_name, asm_name, |
1329 | btype, loc); | |
8381eda7 | 1330 | else |
1331 | { | |
1332 | Location bloc = Linemap::predeclared_location(); | |
80efb81d | 1333 | |
1334 | // The runtime package has hash/equality functions that are | |
1335 | // referenced by type descriptors outside of the runtime, so the | |
1336 | // function descriptors must be visible even though they are not | |
1337 | // exported. | |
1338 | bool is_exported_runtime = false; | |
1339 | if (gogo->compiling_runtime() | |
1340 | && gogo->package_name() == "runtime" | |
1341 | && (no->name().find("hash") != std::string::npos | |
1342 | || no->name().find("equal") != std::string::npos)) | |
1343 | is_exported_runtime = true; | |
1344 | ||
8381eda7 | 1345 | bool is_hidden = ((no->is_function() |
1346 | && no->func_value()->enclosing() != NULL) | |
80efb81d | 1347 | || (Gogo::is_hidden_name(no->name()) |
1348 | && !is_exported_runtime) | |
8381eda7 | 1349 | || Gogo::is_thunk(no)); |
80efb81d | 1350 | |
438b4bec | 1351 | bvar = context->backend()->immutable_struct(var_name, asm_name, |
1352 | is_hidden, false, | |
8381eda7 | 1353 | btype, bloc); |
1354 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1355 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1356 | Expression* init = |
1357 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1358 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1359 | bcontext.set_is_const(); | |
ea664253 | 1360 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1361 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1362 | false, btype, bloc, binit); | |
1363 | } | |
1364 | ||
1365 | this->dvar_ = bvar; | |
7af8e400 | 1366 | return gogo->backend()->var_expression(bvar, loc); |
8381eda7 | 1367 | } |
1368 | ||
c6837989 | 1369 | // Print a function descriptor expression. |
1370 | ||
1371 | void | |
1372 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1373 | { | |
1374 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1375 | } | |
1376 | ||
8381eda7 | 1377 | // Make a function descriptor expression. |
1378 | ||
c6837989 | 1379 | Func_descriptor_expression* |
1380 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1381 | { |
c6837989 | 1382 | return new Func_descriptor_expression(fn); |
8381eda7 | 1383 | } |
1384 | ||
1385 | // Make the function descriptor type, so that it can be converted. | |
1386 | ||
1387 | void | |
1388 | Expression::make_func_descriptor_type() | |
1389 | { | |
1390 | Func_descriptor_expression::make_func_descriptor_type(); | |
1391 | } | |
1392 | ||
1393 | // A reference to just the code of a function. | |
1394 | ||
1395 | class Func_code_reference_expression : public Expression | |
1396 | { | |
1397 | public: | |
1398 | Func_code_reference_expression(Named_object* function, Location location) | |
1399 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1400 | function_(function) | |
1401 | { } | |
1402 | ||
1403 | protected: | |
1404 | int | |
1405 | do_traverse(Traverse*) | |
1406 | { return TRAVERSE_CONTINUE; } | |
1407 | ||
f9ca30f9 | 1408 | bool |
3ae06f68 | 1409 | do_is_static_initializer() const |
f9ca30f9 | 1410 | { return true; } |
1411 | ||
8381eda7 | 1412 | Type* |
1413 | do_type() | |
1414 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1415 | ||
1416 | void | |
1417 | do_determine_type(const Type_context*) | |
1418 | { } | |
1419 | ||
1420 | Expression* | |
1421 | do_copy() | |
1422 | { | |
1423 | return Expression::make_func_code_reference(this->function_, | |
1424 | this->location()); | |
1425 | } | |
1426 | ||
ea664253 | 1427 | Bexpression* |
1428 | do_get_backend(Translate_context*); | |
8381eda7 | 1429 | |
1430 | void | |
1431 | do_dump_expression(Ast_dump_context* context) const | |
1432 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1433 | ||
1434 | private: | |
1435 | // The function. | |
1436 | Named_object* function_; | |
1437 | }; | |
1438 | ||
ea664253 | 1439 | // Get the backend representation for a reference to function code. |
8381eda7 | 1440 | |
ea664253 | 1441 | Bexpression* |
1442 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1443 | { |
ea664253 | 1444 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1445 | this->location()); | |
8381eda7 | 1446 | } |
1447 | ||
1448 | // Make a reference to the code of a function. | |
1449 | ||
1450 | Expression* | |
1451 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1452 | { | |
1453 | return new Func_code_reference_expression(function, location); | |
1454 | } | |
1455 | ||
e440a328 | 1456 | // Class Unknown_expression. |
1457 | ||
1458 | // Return the name of an unknown expression. | |
1459 | ||
1460 | const std::string& | |
1461 | Unknown_expression::name() const | |
1462 | { | |
1463 | return this->named_object_->name(); | |
1464 | } | |
1465 | ||
1466 | // Lower a reference to an unknown name. | |
1467 | ||
1468 | Expression* | |
ceeb4318 | 1469 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1470 | { |
b13c66cd | 1471 | Location location = this->location(); |
e440a328 | 1472 | Named_object* no = this->named_object_; |
deded542 | 1473 | Named_object* real; |
1474 | if (!no->is_unknown()) | |
1475 | real = no; | |
1476 | else | |
e440a328 | 1477 | { |
deded542 | 1478 | real = no->unknown_value()->real_named_object(); |
1479 | if (real == NULL) | |
1480 | { | |
1481 | if (this->is_composite_literal_key_) | |
1482 | return this; | |
acf8e158 | 1483 | if (!this->no_error_message_) |
631d5788 | 1484 | go_error_at(location, "reference to undefined name %qs", |
1485 | this->named_object_->message_name().c_str()); | |
deded542 | 1486 | return Expression::make_error(location); |
1487 | } | |
e440a328 | 1488 | } |
1489 | switch (real->classification()) | |
1490 | { | |
1491 | case Named_object::NAMED_OBJECT_CONST: | |
1492 | return Expression::make_const_reference(real, location); | |
1493 | case Named_object::NAMED_OBJECT_TYPE: | |
1494 | return Expression::make_type(real->type_value(), location); | |
1495 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1496 | if (this->is_composite_literal_key_) | |
1497 | return this; | |
acf8e158 | 1498 | if (!this->no_error_message_) |
631d5788 | 1499 | go_error_at(location, "reference to undefined type %qs", |
1500 | real->message_name().c_str()); | |
e440a328 | 1501 | return Expression::make_error(location); |
1502 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1503 | real->var_value()->set_is_used(); |
e440a328 | 1504 | return Expression::make_var_reference(real, location); |
1505 | case Named_object::NAMED_OBJECT_FUNC: | |
1506 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1507 | return Expression::make_func_reference(real, NULL, location); | |
1508 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1509 | if (this->is_composite_literal_key_) | |
1510 | return this; | |
acf8e158 | 1511 | if (!this->no_error_message_) |
631d5788 | 1512 | go_error_at(location, "unexpected reference to package"); |
e440a328 | 1513 | return Expression::make_error(location); |
1514 | default: | |
c3e6f413 | 1515 | go_unreachable(); |
e440a328 | 1516 | } |
1517 | } | |
1518 | ||
d751bb78 | 1519 | // Dump the ast representation for an unknown expression to a dump context. |
1520 | ||
1521 | void | |
1522 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1523 | { | |
1524 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1525 | << ")"; | |
d751bb78 | 1526 | } |
1527 | ||
e440a328 | 1528 | // Make a reference to an unknown name. |
1529 | ||
acf8e158 | 1530 | Unknown_expression* |
b13c66cd | 1531 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1532 | { |
e440a328 | 1533 | return new Unknown_expression(no, location); |
1534 | } | |
1535 | ||
1536 | // A boolean expression. | |
1537 | ||
1538 | class Boolean_expression : public Expression | |
1539 | { | |
1540 | public: | |
b13c66cd | 1541 | Boolean_expression(bool val, Location location) |
e440a328 | 1542 | : Expression(EXPRESSION_BOOLEAN, location), |
1543 | val_(val), type_(NULL) | |
1544 | { } | |
1545 | ||
1546 | static Expression* | |
1547 | do_import(Import*); | |
1548 | ||
1549 | protected: | |
1550 | bool | |
1551 | do_is_constant() const | |
1552 | { return true; } | |
1553 | ||
0e168074 | 1554 | bool |
3ae06f68 | 1555 | do_is_static_initializer() const |
0e168074 | 1556 | { return true; } |
1557 | ||
e440a328 | 1558 | Type* |
1559 | do_type(); | |
1560 | ||
1561 | void | |
1562 | do_determine_type(const Type_context*); | |
1563 | ||
1564 | Expression* | |
1565 | do_copy() | |
1566 | { return this; } | |
1567 | ||
ea664253 | 1568 | Bexpression* |
1569 | do_get_backend(Translate_context* context) | |
1570 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1571 | |
1572 | void | |
1573 | do_export(Export* exp) const | |
1574 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1575 | ||
d751bb78 | 1576 | void |
1577 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1578 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1579 | ||
e440a328 | 1580 | private: |
1581 | // The constant. | |
1582 | bool val_; | |
1583 | // The type as determined by context. | |
1584 | Type* type_; | |
1585 | }; | |
1586 | ||
1587 | // Get the type. | |
1588 | ||
1589 | Type* | |
1590 | Boolean_expression::do_type() | |
1591 | { | |
1592 | if (this->type_ == NULL) | |
1593 | this->type_ = Type::make_boolean_type(); | |
1594 | return this->type_; | |
1595 | } | |
1596 | ||
1597 | // Set the type from the context. | |
1598 | ||
1599 | void | |
1600 | Boolean_expression::do_determine_type(const Type_context* context) | |
1601 | { | |
1602 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1603 | ; | |
1604 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1605 | this->type_ = context->type; | |
1606 | else if (!context->may_be_abstract) | |
1607 | this->type_ = Type::lookup_bool_type(); | |
1608 | } | |
1609 | ||
1610 | // Import a boolean constant. | |
1611 | ||
1612 | Expression* | |
1613 | Boolean_expression::do_import(Import* imp) | |
1614 | { | |
1615 | if (imp->peek_char() == 't') | |
1616 | { | |
1617 | imp->require_c_string("true"); | |
1618 | return Expression::make_boolean(true, imp->location()); | |
1619 | } | |
1620 | else | |
1621 | { | |
1622 | imp->require_c_string("false"); | |
1623 | return Expression::make_boolean(false, imp->location()); | |
1624 | } | |
1625 | } | |
1626 | ||
1627 | // Make a boolean expression. | |
1628 | ||
1629 | Expression* | |
b13c66cd | 1630 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1631 | { |
1632 | return new Boolean_expression(val, location); | |
1633 | } | |
1634 | ||
1635 | // Class String_expression. | |
1636 | ||
1637 | // Get the type. | |
1638 | ||
1639 | Type* | |
1640 | String_expression::do_type() | |
1641 | { | |
1642 | if (this->type_ == NULL) | |
1643 | this->type_ = Type::make_string_type(); | |
1644 | return this->type_; | |
1645 | } | |
1646 | ||
1647 | // Set the type from the context. | |
1648 | ||
1649 | void | |
1650 | String_expression::do_determine_type(const Type_context* context) | |
1651 | { | |
1652 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1653 | ; | |
1654 | else if (context->type != NULL && context->type->is_string_type()) | |
1655 | this->type_ = context->type; | |
1656 | else if (!context->may_be_abstract) | |
1657 | this->type_ = Type::lookup_string_type(); | |
1658 | } | |
1659 | ||
1660 | // Build a string constant. | |
1661 | ||
ea664253 | 1662 | Bexpression* |
1663 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1664 | { |
2c809f8f | 1665 | Gogo* gogo = context->gogo(); |
1666 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1667 | ||
1668 | Location loc = this->location(); | |
1669 | std::vector<Bexpression*> init(2); | |
1670 | Bexpression* str_cst = | |
1671 | gogo->backend()->string_constant_expression(this->val_); | |
1672 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1673 | ||
1674 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1675 | mpz_t lenval; | |
1676 | mpz_init_set_ui(lenval, this->val_.length()); | |
1677 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1678 | mpz_clear(lenval); | |
1679 | ||
ea664253 | 1680 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1681 | } |
1682 | ||
8b1c301d | 1683 | // Write string literal to string dump. |
e440a328 | 1684 | |
1685 | void | |
8b1c301d | 1686 | String_expression::export_string(String_dump* exp, |
1687 | const String_expression* str) | |
e440a328 | 1688 | { |
1689 | std::string s; | |
8b1c301d | 1690 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1691 | s += '"'; |
8b1c301d | 1692 | for (std::string::const_iterator p = str->val_.begin(); |
1693 | p != str->val_.end(); | |
e440a328 | 1694 | ++p) |
1695 | { | |
1696 | if (*p == '\\' || *p == '"') | |
1697 | { | |
1698 | s += '\\'; | |
1699 | s += *p; | |
1700 | } | |
1701 | else if (*p >= 0x20 && *p < 0x7f) | |
1702 | s += *p; | |
1703 | else if (*p == '\n') | |
1704 | s += "\\n"; | |
1705 | else if (*p == '\t') | |
1706 | s += "\\t"; | |
1707 | else | |
1708 | { | |
1709 | s += "\\x"; | |
1710 | unsigned char c = *p; | |
1711 | unsigned int dig = c >> 4; | |
1712 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1713 | dig = c & 0xf; | |
1714 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1715 | } | |
1716 | } | |
1717 | s += '"'; | |
1718 | exp->write_string(s); | |
1719 | } | |
1720 | ||
8b1c301d | 1721 | // Export a string expression. |
1722 | ||
1723 | void | |
1724 | String_expression::do_export(Export* exp) const | |
1725 | { | |
1726 | String_expression::export_string(exp, this); | |
1727 | } | |
1728 | ||
e440a328 | 1729 | // Import a string expression. |
1730 | ||
1731 | Expression* | |
1732 | String_expression::do_import(Import* imp) | |
1733 | { | |
1734 | imp->require_c_string("\""); | |
1735 | std::string val; | |
1736 | while (true) | |
1737 | { | |
1738 | int c = imp->get_char(); | |
1739 | if (c == '"' || c == -1) | |
1740 | break; | |
1741 | if (c != '\\') | |
1742 | val += static_cast<char>(c); | |
1743 | else | |
1744 | { | |
1745 | c = imp->get_char(); | |
1746 | if (c == '\\' || c == '"') | |
1747 | val += static_cast<char>(c); | |
1748 | else if (c == 'n') | |
1749 | val += '\n'; | |
1750 | else if (c == 't') | |
1751 | val += '\t'; | |
1752 | else if (c == 'x') | |
1753 | { | |
1754 | c = imp->get_char(); | |
1755 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1756 | c = imp->get_char(); | |
1757 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1758 | char v = (vh << 4) | vl; | |
1759 | val += v; | |
1760 | } | |
1761 | else | |
1762 | { | |
631d5788 | 1763 | go_error_at(imp->location(), "bad string constant"); |
e440a328 | 1764 | return Expression::make_error(imp->location()); |
1765 | } | |
1766 | } | |
1767 | } | |
1768 | return Expression::make_string(val, imp->location()); | |
1769 | } | |
1770 | ||
d751bb78 | 1771 | // Ast dump for string expression. |
1772 | ||
1773 | void | |
1774 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1775 | { | |
8b1c301d | 1776 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1777 | } |
1778 | ||
e440a328 | 1779 | // Make a string expression. |
1780 | ||
1781 | Expression* | |
b13c66cd | 1782 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1783 | { |
1784 | return new String_expression(val, location); | |
1785 | } | |
1786 | ||
2c809f8f | 1787 | // An expression that evaluates to some characteristic of a string. |
1788 | // This is used when indexing, bound-checking, or nil checking a string. | |
1789 | ||
1790 | class String_info_expression : public Expression | |
1791 | { | |
1792 | public: | |
1793 | String_info_expression(Expression* string, String_info string_info, | |
1794 | Location location) | |
1795 | : Expression(EXPRESSION_STRING_INFO, location), | |
1796 | string_(string), string_info_(string_info) | |
1797 | { } | |
1798 | ||
1799 | protected: | |
1800 | Type* | |
1801 | do_type(); | |
1802 | ||
1803 | void | |
1804 | do_determine_type(const Type_context*) | |
1805 | { go_unreachable(); } | |
1806 | ||
1807 | Expression* | |
1808 | do_copy() | |
1809 | { | |
1810 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1811 | this->location()); | |
1812 | } | |
1813 | ||
ea664253 | 1814 | Bexpression* |
1815 | do_get_backend(Translate_context* context); | |
2c809f8f | 1816 | |
1817 | void | |
1818 | do_dump_expression(Ast_dump_context*) const; | |
1819 | ||
1820 | void | |
1821 | do_issue_nil_check() | |
1822 | { this->string_->issue_nil_check(); } | |
1823 | ||
1824 | private: | |
1825 | // The string for which we are getting information. | |
1826 | Expression* string_; | |
1827 | // What information we want. | |
1828 | String_info string_info_; | |
1829 | }; | |
1830 | ||
1831 | // Return the type of the string info. | |
1832 | ||
1833 | Type* | |
1834 | String_info_expression::do_type() | |
1835 | { | |
1836 | switch (this->string_info_) | |
1837 | { | |
1838 | case STRING_INFO_DATA: | |
1839 | { | |
1840 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1841 | return Type::make_pointer_type(byte_type); | |
1842 | } | |
1843 | case STRING_INFO_LENGTH: | |
1844 | return Type::lookup_integer_type("int"); | |
1845 | default: | |
1846 | go_unreachable(); | |
1847 | } | |
1848 | } | |
1849 | ||
1850 | // Return string information in GENERIC. | |
1851 | ||
ea664253 | 1852 | Bexpression* |
1853 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1854 | { |
1855 | Gogo* gogo = context->gogo(); | |
1856 | ||
ea664253 | 1857 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1858 | switch (this->string_info_) |
1859 | { | |
1860 | case STRING_INFO_DATA: | |
1861 | case STRING_INFO_LENGTH: | |
ea664253 | 1862 | return gogo->backend()->struct_field_expression(bstring, |
1863 | this->string_info_, | |
1864 | this->location()); | |
2c809f8f | 1865 | break; |
1866 | default: | |
1867 | go_unreachable(); | |
1868 | } | |
2c809f8f | 1869 | } |
1870 | ||
1871 | // Dump ast representation for a type info expression. | |
1872 | ||
1873 | void | |
1874 | String_info_expression::do_dump_expression( | |
1875 | Ast_dump_context* ast_dump_context) const | |
1876 | { | |
1877 | ast_dump_context->ostream() << "stringinfo("; | |
1878 | this->string_->dump_expression(ast_dump_context); | |
1879 | ast_dump_context->ostream() << ","; | |
1880 | ast_dump_context->ostream() << | |
1881 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1882 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1883 | : "unknown"); | |
1884 | ast_dump_context->ostream() << ")"; | |
1885 | } | |
1886 | ||
1887 | // Make a string info expression. | |
1888 | ||
1889 | Expression* | |
1890 | Expression::make_string_info(Expression* string, String_info string_info, | |
1891 | Location location) | |
1892 | { | |
1893 | return new String_info_expression(string, string_info, location); | |
1894 | } | |
1895 | ||
e440a328 | 1896 | // Make an integer expression. |
1897 | ||
1898 | class Integer_expression : public Expression | |
1899 | { | |
1900 | public: | |
5d4b8566 | 1901 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1902 | Location location) | |
e440a328 | 1903 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1904 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1905 | { mpz_init_set(this->val_, *val); } |
1906 | ||
1907 | static Expression* | |
1908 | do_import(Import*); | |
1909 | ||
8b1c301d | 1910 | // Write VAL to string dump. |
e440a328 | 1911 | static void |
8b1c301d | 1912 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1913 | |
d751bb78 | 1914 | // Write VAL to dump context. |
1915 | static void | |
1916 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1917 | ||
e440a328 | 1918 | protected: |
1919 | bool | |
1920 | do_is_constant() const | |
1921 | { return true; } | |
1922 | ||
0e168074 | 1923 | bool |
3ae06f68 | 1924 | do_is_static_initializer() const |
0e168074 | 1925 | { return true; } |
1926 | ||
e440a328 | 1927 | bool |
0c77715b | 1928 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1929 | |
1930 | Type* | |
1931 | do_type(); | |
1932 | ||
1933 | void | |
1934 | do_determine_type(const Type_context* context); | |
1935 | ||
1936 | void | |
1937 | do_check_types(Gogo*); | |
1938 | ||
ea664253 | 1939 | Bexpression* |
1940 | do_get_backend(Translate_context*); | |
e440a328 | 1941 | |
1942 | Expression* | |
1943 | do_copy() | |
5d4b8566 | 1944 | { |
1945 | if (this->is_character_constant_) | |
de590a61 | 1946 | return Expression::make_character(&this->val_, |
1947 | (this->type_ == NULL | |
1948 | ? NULL | |
1949 | : this->type_->copy_expressions()), | |
5d4b8566 | 1950 | this->location()); |
1951 | else | |
de590a61 | 1952 | return Expression::make_integer_z(&this->val_, |
1953 | (this->type_ == NULL | |
1954 | ? NULL | |
1955 | : this->type_->copy_expressions()), | |
e67508fa | 1956 | this->location()); |
5d4b8566 | 1957 | } |
e440a328 | 1958 | |
1959 | void | |
1960 | do_export(Export*) const; | |
1961 | ||
d751bb78 | 1962 | void |
1963 | do_dump_expression(Ast_dump_context*) const; | |
1964 | ||
e440a328 | 1965 | private: |
1966 | // The integer value. | |
1967 | mpz_t val_; | |
1968 | // The type so far. | |
1969 | Type* type_; | |
5d4b8566 | 1970 | // Whether this is a character constant. |
1971 | bool is_character_constant_; | |
e440a328 | 1972 | }; |
1973 | ||
0c77715b | 1974 | // Return a numeric constant for this expression. We have to mark |
1975 | // this as a character when appropriate. | |
e440a328 | 1976 | |
1977 | bool | |
0c77715b | 1978 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1979 | { |
0c77715b | 1980 | if (this->is_character_constant_) |
1981 | nc->set_rune(this->type_, this->val_); | |
1982 | else | |
1983 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1984 | return true; |
1985 | } | |
1986 | ||
1987 | // Return the current type. If we haven't set the type yet, we return | |
1988 | // an abstract integer type. | |
1989 | ||
1990 | Type* | |
1991 | Integer_expression::do_type() | |
1992 | { | |
1993 | if (this->type_ == NULL) | |
5d4b8566 | 1994 | { |
1995 | if (this->is_character_constant_) | |
1996 | this->type_ = Type::make_abstract_character_type(); | |
1997 | else | |
1998 | this->type_ = Type::make_abstract_integer_type(); | |
1999 | } | |
e440a328 | 2000 | return this->type_; |
2001 | } | |
2002 | ||
2003 | // Set the type of the integer value. Here we may switch from an | |
2004 | // abstract type to a real type. | |
2005 | ||
2006 | void | |
2007 | Integer_expression::do_determine_type(const Type_context* context) | |
2008 | { | |
2009 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2010 | ; | |
0c77715b | 2011 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2012 | this->type_ = context->type; |
2013 | else if (!context->may_be_abstract) | |
5d4b8566 | 2014 | { |
2015 | if (this->is_character_constant_) | |
2016 | this->type_ = Type::lookup_integer_type("int32"); | |
2017 | else | |
2018 | this->type_ = Type::lookup_integer_type("int"); | |
2019 | } | |
e440a328 | 2020 | } |
2021 | ||
e440a328 | 2022 | // Check the type of an integer constant. |
2023 | ||
2024 | void | |
2025 | Integer_expression::do_check_types(Gogo*) | |
2026 | { | |
0c77715b | 2027 | Type* type = this->type_; |
2028 | if (type == NULL) | |
e440a328 | 2029 | return; |
0c77715b | 2030 | Numeric_constant nc; |
2031 | if (this->is_character_constant_) | |
2032 | nc.set_rune(NULL, this->val_); | |
2033 | else | |
2034 | nc.set_int(NULL, this->val_); | |
2035 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 2036 | this->set_is_error(); |
2037 | } | |
2038 | ||
ea664253 | 2039 | // Get the backend representation for an integer constant. |
e440a328 | 2040 | |
ea664253 | 2041 | Bexpression* |
2042 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2043 | { |
12373dd5 | 2044 | if (this->is_error_expression() |
2045 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2046 | { | |
2047 | go_assert(saw_errors()); | |
2048 | return context->gogo()->backend()->error_expression(); | |
2049 | } | |
2050 | ||
48c2a53a | 2051 | Type* resolved_type = NULL; |
e440a328 | 2052 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2053 | resolved_type = this->type_; |
e440a328 | 2054 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
2055 | { | |
2056 | // We are converting to an abstract floating point type. | |
48c2a53a | 2057 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2058 | } |
2059 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2060 | { | |
2061 | // We are converting to an abstract complex type. | |
48c2a53a | 2062 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2063 | } |
2064 | else | |
2065 | { | |
2066 | // If we still have an abstract type here, then this is being | |
2067 | // used in a constant expression which didn't get reduced for | |
2068 | // some reason. Use a type which will fit the value. We use <, | |
2069 | // not <=, because we need an extra bit for the sign bit. | |
2070 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 2071 | Type* int_type = Type::lookup_integer_type("int"); |
2072 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 2073 | resolved_type = int_type; |
e440a328 | 2074 | else if (bits < 64) |
48c2a53a | 2075 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 2076 | else |
48c2a53a | 2077 | { |
2078 | if (!saw_errors()) | |
631d5788 | 2079 | go_error_at(this->location(), |
2080 | "unknown type for large integer constant"); | |
ea664253 | 2081 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 2082 | } |
e440a328 | 2083 | } |
48c2a53a | 2084 | Numeric_constant nc; |
2085 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 2086 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2087 | } |
2088 | ||
2089 | // Write VAL to export data. | |
2090 | ||
2091 | void | |
8b1c301d | 2092 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 2093 | { |
2094 | char* s = mpz_get_str(NULL, 10, val); | |
2095 | exp->write_c_string(s); | |
2096 | free(s); | |
2097 | } | |
2098 | ||
2099 | // Export an integer in a constant expression. | |
2100 | ||
2101 | void | |
2102 | Integer_expression::do_export(Export* exp) const | |
2103 | { | |
2104 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 2105 | if (this->is_character_constant_) |
2106 | exp->write_c_string("'"); | |
e440a328 | 2107 | // A trailing space lets us reliably identify the end of the number. |
2108 | exp->write_c_string(" "); | |
2109 | } | |
2110 | ||
2111 | // Import an integer, floating point, or complex value. This handles | |
2112 | // all these types because they all start with digits. | |
2113 | ||
2114 | Expression* | |
2115 | Integer_expression::do_import(Import* imp) | |
2116 | { | |
2117 | std::string num = imp->read_identifier(); | |
2118 | imp->require_c_string(" "); | |
2119 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2120 | { | |
2121 | mpfr_t real; | |
2122 | size_t plus_pos = num.find('+', 1); | |
2123 | size_t minus_pos = num.find('-', 1); | |
2124 | size_t pos; | |
2125 | if (plus_pos == std::string::npos) | |
2126 | pos = minus_pos; | |
2127 | else if (minus_pos == std::string::npos) | |
2128 | pos = plus_pos; | |
2129 | else | |
2130 | { | |
631d5788 | 2131 | go_error_at(imp->location(), "bad number in import data: %qs", |
2132 | num.c_str()); | |
e440a328 | 2133 | return Expression::make_error(imp->location()); |
2134 | } | |
2135 | if (pos == std::string::npos) | |
2136 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2137 | else | |
2138 | { | |
2139 | std::string real_str = num.substr(0, pos); | |
2140 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2141 | { | |
631d5788 | 2142 | go_error_at(imp->location(), "bad number in import data: %qs", |
2143 | real_str.c_str()); | |
e440a328 | 2144 | return Expression::make_error(imp->location()); |
2145 | } | |
2146 | } | |
2147 | ||
2148 | std::string imag_str; | |
2149 | if (pos == std::string::npos) | |
2150 | imag_str = num; | |
2151 | else | |
2152 | imag_str = num.substr(pos); | |
2153 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2154 | mpfr_t imag; | |
2155 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2156 | { | |
631d5788 | 2157 | go_error_at(imp->location(), "bad number in import data: %qs", |
2158 | imag_str.c_str()); | |
e440a328 | 2159 | return Expression::make_error(imp->location()); |
2160 | } | |
fcbea5e4 | 2161 | mpc_t cval; |
2162 | mpc_init2(cval, mpc_precision); | |
2163 | mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); | |
e440a328 | 2164 | mpfr_clear(real); |
2165 | mpfr_clear(imag); | |
fcbea5e4 | 2166 | Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); |
2167 | mpc_clear(cval); | |
e440a328 | 2168 | return ret; |
2169 | } | |
2170 | else if (num.find('.') == std::string::npos | |
2171 | && num.find('E') == std::string::npos) | |
2172 | { | |
5d4b8566 | 2173 | bool is_character_constant = (!num.empty() |
2174 | && num[num.length() - 1] == '\''); | |
2175 | if (is_character_constant) | |
2176 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2177 | mpz_t val; |
2178 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2179 | { | |
631d5788 | 2180 | go_error_at(imp->location(), "bad number in import data: %qs", |
2181 | num.c_str()); | |
e440a328 | 2182 | return Expression::make_error(imp->location()); |
2183 | } | |
5d4b8566 | 2184 | Expression* ret; |
2185 | if (is_character_constant) | |
2186 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2187 | else | |
e67508fa | 2188 | ret = Expression::make_integer_z(&val, NULL, imp->location()); |
e440a328 | 2189 | mpz_clear(val); |
2190 | return ret; | |
2191 | } | |
2192 | else | |
2193 | { | |
2194 | mpfr_t val; | |
2195 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2196 | { | |
631d5788 | 2197 | go_error_at(imp->location(), "bad number in import data: %qs", |
2198 | num.c_str()); | |
e440a328 | 2199 | return Expression::make_error(imp->location()); |
2200 | } | |
2201 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2202 | mpfr_clear(val); | |
2203 | return ret; | |
2204 | } | |
2205 | } | |
d751bb78 | 2206 | // Ast dump for integer expression. |
2207 | ||
2208 | void | |
2209 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2210 | { | |
5d4b8566 | 2211 | if (this->is_character_constant_) |
2212 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2213 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2214 | if (this->is_character_constant_) |
2215 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2216 | } |
2217 | ||
e67508fa | 2218 | // Build a new integer value from a multi-precision integer. |
e440a328 | 2219 | |
2220 | Expression* | |
e67508fa | 2221 | Expression::make_integer_z(const mpz_t* val, Type* type, Location location) |
5d4b8566 | 2222 | { |
2223 | return new Integer_expression(val, type, false, location); | |
2224 | } | |
2225 | ||
e67508fa | 2226 | // Build a new integer value from an unsigned long. |
2227 | ||
2228 | Expression* | |
2229 | Expression::make_integer_ul(unsigned long val, Type *type, Location location) | |
2230 | { | |
2231 | mpz_t zval; | |
2232 | mpz_init_set_ui(zval, val); | |
2233 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2234 | mpz_clear(zval); | |
2235 | return ret; | |
2236 | } | |
2237 | ||
2238 | // Build a new integer value from a signed long. | |
2239 | ||
2240 | Expression* | |
2241 | Expression::make_integer_sl(long val, Type *type, Location location) | |
2242 | { | |
2243 | mpz_t zval; | |
2244 | mpz_init_set_si(zval, val); | |
2245 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2246 | mpz_clear(zval); | |
2247 | return ret; | |
2248 | } | |
2249 | ||
3f378015 | 2250 | // Store an int64_t in an uninitialized mpz_t. |
2251 | ||
2252 | static void | |
2253 | set_mpz_from_int64(mpz_t* zval, int64_t val) | |
2254 | { | |
2255 | if (val >= 0) | |
2256 | { | |
2257 | unsigned long ul = static_cast<unsigned long>(val); | |
2258 | if (static_cast<int64_t>(ul) == val) | |
2259 | { | |
2260 | mpz_init_set_ui(*zval, ul); | |
2261 | return; | |
2262 | } | |
2263 | } | |
2264 | uint64_t uv; | |
2265 | if (val >= 0) | |
2266 | uv = static_cast<uint64_t>(val); | |
2267 | else | |
2268 | uv = static_cast<uint64_t>(- val); | |
2269 | unsigned long ul = uv & 0xffffffffUL; | |
2270 | mpz_init_set_ui(*zval, ul); | |
2271 | mpz_t hval; | |
2272 | mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); | |
2273 | mpz_mul_2exp(hval, hval, 32); | |
2274 | mpz_add(*zval, *zval, hval); | |
2275 | mpz_clear(hval); | |
2276 | if (val < 0) | |
2277 | mpz_neg(*zval, *zval); | |
2278 | } | |
2279 | ||
2280 | // Build a new integer value from an int64_t. | |
2281 | ||
2282 | Expression* | |
2283 | Expression::make_integer_int64(int64_t val, Type* type, Location location) | |
2284 | { | |
2285 | mpz_t zval; | |
2286 | set_mpz_from_int64(&zval, val); | |
2287 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2288 | mpz_clear(zval); | |
2289 | return ret; | |
2290 | } | |
2291 | ||
5d4b8566 | 2292 | // Build a new character constant value. |
2293 | ||
2294 | Expression* | |
2295 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2296 | { |
5d4b8566 | 2297 | return new Integer_expression(val, type, true, location); |
e440a328 | 2298 | } |
2299 | ||
2300 | // Floats. | |
2301 | ||
2302 | class Float_expression : public Expression | |
2303 | { | |
2304 | public: | |
b13c66cd | 2305 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2306 | : Expression(EXPRESSION_FLOAT, location), |
2307 | type_(type) | |
2308 | { | |
2309 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2310 | } | |
2311 | ||
e440a328 | 2312 | // Write VAL to export data. |
2313 | static void | |
8b1c301d | 2314 | export_float(String_dump* exp, const mpfr_t val); |
2315 | ||
d751bb78 | 2316 | // Write VAL to dump file. |
2317 | static void | |
2318 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2319 | |
2320 | protected: | |
2321 | bool | |
2322 | do_is_constant() const | |
2323 | { return true; } | |
2324 | ||
0e168074 | 2325 | bool |
3ae06f68 | 2326 | do_is_static_initializer() const |
0e168074 | 2327 | { return true; } |
2328 | ||
e440a328 | 2329 | bool |
0c77715b | 2330 | do_numeric_constant_value(Numeric_constant* nc) const |
2331 | { | |
2332 | nc->set_float(this->type_, this->val_); | |
2333 | return true; | |
2334 | } | |
e440a328 | 2335 | |
2336 | Type* | |
2337 | do_type(); | |
2338 | ||
2339 | void | |
2340 | do_determine_type(const Type_context*); | |
2341 | ||
2342 | void | |
2343 | do_check_types(Gogo*); | |
2344 | ||
2345 | Expression* | |
2346 | do_copy() | |
de590a61 | 2347 | { return Expression::make_float(&this->val_, |
2348 | (this->type_ == NULL | |
2349 | ? NULL | |
2350 | : this->type_->copy_expressions()), | |
e440a328 | 2351 | this->location()); } |
2352 | ||
ea664253 | 2353 | Bexpression* |
2354 | do_get_backend(Translate_context*); | |
e440a328 | 2355 | |
2356 | void | |
2357 | do_export(Export*) const; | |
2358 | ||
d751bb78 | 2359 | void |
2360 | do_dump_expression(Ast_dump_context*) const; | |
2361 | ||
e440a328 | 2362 | private: |
2363 | // The floating point value. | |
2364 | mpfr_t val_; | |
2365 | // The type so far. | |
2366 | Type* type_; | |
2367 | }; | |
2368 | ||
e440a328 | 2369 | // Return the current type. If we haven't set the type yet, we return |
2370 | // an abstract float type. | |
2371 | ||
2372 | Type* | |
2373 | Float_expression::do_type() | |
2374 | { | |
2375 | if (this->type_ == NULL) | |
2376 | this->type_ = Type::make_abstract_float_type(); | |
2377 | return this->type_; | |
2378 | } | |
2379 | ||
2380 | // Set the type of the float value. Here we may switch from an | |
2381 | // abstract type to a real type. | |
2382 | ||
2383 | void | |
2384 | Float_expression::do_determine_type(const Type_context* context) | |
2385 | { | |
2386 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2387 | ; | |
2388 | else if (context->type != NULL | |
2389 | && (context->type->integer_type() != NULL | |
2390 | || context->type->float_type() != NULL | |
2391 | || context->type->complex_type() != NULL)) | |
2392 | this->type_ = context->type; | |
2393 | else if (!context->may_be_abstract) | |
48080209 | 2394 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2395 | } |
2396 | ||
e440a328 | 2397 | // Check the type of a float value. |
2398 | ||
2399 | void | |
2400 | Float_expression::do_check_types(Gogo*) | |
2401 | { | |
0c77715b | 2402 | Type* type = this->type_; |
2403 | if (type == NULL) | |
e440a328 | 2404 | return; |
0c77715b | 2405 | Numeric_constant nc; |
2406 | nc.set_float(NULL, this->val_); | |
2407 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2408 | this->set_is_error(); |
e440a328 | 2409 | } |
2410 | ||
ea664253 | 2411 | // Get the backend representation for a float constant. |
e440a328 | 2412 | |
ea664253 | 2413 | Bexpression* |
2414 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2415 | { |
12373dd5 | 2416 | if (this->is_error_expression() |
2417 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2418 | { | |
2419 | go_assert(saw_errors()); | |
2420 | return context->gogo()->backend()->error_expression(); | |
2421 | } | |
2422 | ||
48c2a53a | 2423 | Type* resolved_type; |
e440a328 | 2424 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2425 | resolved_type = this->type_; |
e440a328 | 2426 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2427 | { | |
2428 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2429 | resolved_type = Type::lookup_integer_type("int"); |
2430 | } | |
2431 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2432 | { | |
2433 | // We are converting to an abstract complex type. | |
2434 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2435 | } |
2436 | else | |
2437 | { | |
2438 | // If we still have an abstract type here, then this is being | |
2439 | // used in a constant expression which didn't get reduced. We | |
2440 | // just use float64 and hope for the best. | |
48c2a53a | 2441 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2442 | } |
48c2a53a | 2443 | |
2444 | Numeric_constant nc; | |
2445 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2446 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2447 | } |
2448 | ||
8b1c301d | 2449 | // Write a floating point number to a string dump. |
e440a328 | 2450 | |
2451 | void | |
8b1c301d | 2452 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2453 | { |
2454 | mp_exp_t exponent; | |
2455 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2456 | if (*s == '-') | |
2457 | exp->write_c_string("-"); | |
2458 | exp->write_c_string("0."); | |
2459 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2460 | mpfr_free_str(s); | |
2461 | char buf[30]; | |
2462 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2463 | exp->write_c_string(buf); | |
2464 | } | |
2465 | ||
2466 | // Export a floating point number in a constant expression. | |
2467 | ||
2468 | void | |
2469 | Float_expression::do_export(Export* exp) const | |
2470 | { | |
2471 | Float_expression::export_float(exp, this->val_); | |
2472 | // A trailing space lets us reliably identify the end of the number. | |
2473 | exp->write_c_string(" "); | |
2474 | } | |
2475 | ||
d751bb78 | 2476 | // Dump a floating point number to the dump file. |
2477 | ||
2478 | void | |
2479 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2480 | { | |
8b1c301d | 2481 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2482 | } |
2483 | ||
e440a328 | 2484 | // Make a float expression. |
2485 | ||
2486 | Expression* | |
b13c66cd | 2487 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2488 | { |
2489 | return new Float_expression(val, type, location); | |
2490 | } | |
2491 | ||
2492 | // Complex numbers. | |
2493 | ||
2494 | class Complex_expression : public Expression | |
2495 | { | |
2496 | public: | |
fcbea5e4 | 2497 | Complex_expression(const mpc_t* val, Type* type, Location location) |
e440a328 | 2498 | : Expression(EXPRESSION_COMPLEX, location), |
2499 | type_(type) | |
2500 | { | |
fcbea5e4 | 2501 | mpc_init2(this->val_, mpc_precision); |
2502 | mpc_set(this->val_, *val, MPC_RNDNN); | |
e440a328 | 2503 | } |
2504 | ||
fcbea5e4 | 2505 | // Write VAL to string dump. |
e440a328 | 2506 | static void |
fcbea5e4 | 2507 | export_complex(String_dump* exp, const mpc_t val); |
e440a328 | 2508 | |
d751bb78 | 2509 | // Write REAL/IMAG to dump context. |
2510 | static void | |
fcbea5e4 | 2511 | dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); |
d751bb78 | 2512 | |
e440a328 | 2513 | protected: |
2514 | bool | |
2515 | do_is_constant() const | |
2516 | { return true; } | |
2517 | ||
0e168074 | 2518 | bool |
3ae06f68 | 2519 | do_is_static_initializer() const |
0e168074 | 2520 | { return true; } |
2521 | ||
e440a328 | 2522 | bool |
0c77715b | 2523 | do_numeric_constant_value(Numeric_constant* nc) const |
2524 | { | |
fcbea5e4 | 2525 | nc->set_complex(this->type_, this->val_); |
0c77715b | 2526 | return true; |
2527 | } | |
e440a328 | 2528 | |
2529 | Type* | |
2530 | do_type(); | |
2531 | ||
2532 | void | |
2533 | do_determine_type(const Type_context*); | |
2534 | ||
2535 | void | |
2536 | do_check_types(Gogo*); | |
2537 | ||
2538 | Expression* | |
2539 | do_copy() | |
2540 | { | |
de590a61 | 2541 | return Expression::make_complex(&this->val_, |
2542 | (this->type_ == NULL | |
2543 | ? NULL | |
2544 | : this->type_->copy_expressions()), | |
e440a328 | 2545 | this->location()); |
2546 | } | |
2547 | ||
ea664253 | 2548 | Bexpression* |
2549 | do_get_backend(Translate_context*); | |
e440a328 | 2550 | |
2551 | void | |
2552 | do_export(Export*) const; | |
2553 | ||
d751bb78 | 2554 | void |
2555 | do_dump_expression(Ast_dump_context*) const; | |
abd26de0 | 2556 | |
e440a328 | 2557 | private: |
fcbea5e4 | 2558 | // The complex value. |
2559 | mpc_t val_; | |
e440a328 | 2560 | // The type if known. |
2561 | Type* type_; | |
2562 | }; | |
2563 | ||
e440a328 | 2564 | // Return the current type. If we haven't set the type yet, we return |
2565 | // an abstract complex type. | |
2566 | ||
2567 | Type* | |
2568 | Complex_expression::do_type() | |
2569 | { | |
2570 | if (this->type_ == NULL) | |
2571 | this->type_ = Type::make_abstract_complex_type(); | |
2572 | return this->type_; | |
2573 | } | |
2574 | ||
2575 | // Set the type of the complex value. Here we may switch from an | |
2576 | // abstract type to a real type. | |
2577 | ||
2578 | void | |
2579 | Complex_expression::do_determine_type(const Type_context* context) | |
2580 | { | |
2581 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2582 | ; | |
abd26de0 | 2583 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2584 | this->type_ = context->type; |
2585 | else if (!context->may_be_abstract) | |
48080209 | 2586 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2587 | } |
2588 | ||
e440a328 | 2589 | // Check the type of a complex value. |
2590 | ||
2591 | void | |
2592 | Complex_expression::do_check_types(Gogo*) | |
2593 | { | |
0c77715b | 2594 | Type* type = this->type_; |
2595 | if (type == NULL) | |
e440a328 | 2596 | return; |
0c77715b | 2597 | Numeric_constant nc; |
fcbea5e4 | 2598 | nc.set_complex(NULL, this->val_); |
0c77715b | 2599 | if (!nc.set_type(this->type_, true, this->location())) |
e440a328 | 2600 | this->set_is_error(); |
2601 | } | |
2602 | ||
ea664253 | 2603 | // Get the backend representation for a complex constant. |
e440a328 | 2604 | |
ea664253 | 2605 | Bexpression* |
2606 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2607 | { |
12373dd5 | 2608 | if (this->is_error_expression() |
2609 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2610 | { | |
2611 | go_assert(saw_errors()); | |
2612 | return context->gogo()->backend()->error_expression(); | |
2613 | } | |
2614 | ||
48c2a53a | 2615 | Type* resolved_type; |
e440a328 | 2616 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2617 | resolved_type = this->type_; |
2618 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2619 | { | |
2620 | // We are converting to an abstract integer type. | |
2621 | resolved_type = Type::lookup_integer_type("int"); | |
2622 | } | |
2623 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2624 | { | |
2625 | // We are converting to an abstract float type. | |
2626 | resolved_type = Type::lookup_float_type("float64"); | |
2627 | } | |
e440a328 | 2628 | else |
2629 | { | |
47ae02b7 | 2630 | // If we still have an abstract type here, this is being |
e440a328 | 2631 | // used in a constant expression which didn't get reduced. We |
2632 | // just use complex128 and hope for the best. | |
48c2a53a | 2633 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2634 | } |
48c2a53a | 2635 | |
2636 | Numeric_constant nc; | |
fcbea5e4 | 2637 | nc.set_complex(resolved_type, this->val_); |
ea664253 | 2638 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2639 | } |
2640 | ||
2641 | // Write REAL/IMAG to export data. | |
2642 | ||
2643 | void | |
fcbea5e4 | 2644 | Complex_expression::export_complex(String_dump* exp, const mpc_t val) |
e440a328 | 2645 | { |
fcbea5e4 | 2646 | if (!mpfr_zero_p(mpc_realref(val))) |
e440a328 | 2647 | { |
fcbea5e4 | 2648 | Float_expression::export_float(exp, mpc_realref(val)); |
d1db782d | 2649 | if (mpfr_sgn(mpc_imagref(val)) >= 0) |
e440a328 | 2650 | exp->write_c_string("+"); |
2651 | } | |
fcbea5e4 | 2652 | Float_expression::export_float(exp, mpc_imagref(val)); |
e440a328 | 2653 | exp->write_c_string("i"); |
2654 | } | |
2655 | ||
2656 | // Export a complex number in a constant expression. | |
2657 | ||
2658 | void | |
2659 | Complex_expression::do_export(Export* exp) const | |
2660 | { | |
fcbea5e4 | 2661 | Complex_expression::export_complex(exp, this->val_); |
e440a328 | 2662 | // A trailing space lets us reliably identify the end of the number. |
2663 | exp->write_c_string(" "); | |
2664 | } | |
2665 | ||
d751bb78 | 2666 | // Dump a complex expression to the dump file. |
2667 | ||
2668 | void | |
2669 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2670 | { | |
fcbea5e4 | 2671 | Complex_expression::export_complex(ast_dump_context, this->val_); |
d751bb78 | 2672 | } |
2673 | ||
e440a328 | 2674 | // Make a complex expression. |
2675 | ||
2676 | Expression* | |
fcbea5e4 | 2677 | Expression::make_complex(const mpc_t* val, Type* type, Location location) |
e440a328 | 2678 | { |
fcbea5e4 | 2679 | return new Complex_expression(val, type, location); |
e440a328 | 2680 | } |
2681 | ||
d5b605df | 2682 | // Find a named object in an expression. |
2683 | ||
2684 | class Find_named_object : public Traverse | |
2685 | { | |
2686 | public: | |
2687 | Find_named_object(Named_object* no) | |
2688 | : Traverse(traverse_expressions), | |
2689 | no_(no), found_(false) | |
2690 | { } | |
2691 | ||
2692 | // Whether we found the object. | |
2693 | bool | |
2694 | found() const | |
2695 | { return this->found_; } | |
2696 | ||
2697 | protected: | |
2698 | int | |
2699 | expression(Expression**); | |
2700 | ||
2701 | private: | |
2702 | // The object we are looking for. | |
2703 | Named_object* no_; | |
2704 | // Whether we found it. | |
2705 | bool found_; | |
2706 | }; | |
2707 | ||
e440a328 | 2708 | // A reference to a const in an expression. |
2709 | ||
2710 | class Const_expression : public Expression | |
2711 | { | |
2712 | public: | |
b13c66cd | 2713 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2714 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2715 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2716 | { } |
2717 | ||
d5b605df | 2718 | Named_object* |
2719 | named_object() | |
2720 | { return this->constant_; } | |
2721 | ||
a7f064d5 | 2722 | // Check that the initializer does not refer to the constant itself. |
2723 | void | |
2724 | check_for_init_loop(); | |
2725 | ||
e440a328 | 2726 | protected: |
ba4aedd4 | 2727 | int |
2728 | do_traverse(Traverse*); | |
2729 | ||
e440a328 | 2730 | Expression* |
ceeb4318 | 2731 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2732 | |
2733 | bool | |
2734 | do_is_constant() const | |
2735 | { return true; } | |
2736 | ||
0e168074 | 2737 | bool |
3ae06f68 | 2738 | do_is_static_initializer() const |
0e168074 | 2739 | { return true; } |
2740 | ||
e440a328 | 2741 | bool |
0c77715b | 2742 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2743 | |
2744 | bool | |
af6b489a | 2745 | do_string_constant_value(std::string* val) const; |
e440a328 | 2746 | |
2747 | Type* | |
2748 | do_type(); | |
2749 | ||
2750 | // The type of a const is set by the declaration, not the use. | |
2751 | void | |
2752 | do_determine_type(const Type_context*); | |
2753 | ||
2754 | void | |
2755 | do_check_types(Gogo*); | |
2756 | ||
2757 | Expression* | |
2758 | do_copy() | |
2759 | { return this; } | |
2760 | ||
ea664253 | 2761 | Bexpression* |
2762 | do_get_backend(Translate_context* context); | |
e440a328 | 2763 | |
2764 | // When exporting a reference to a const as part of a const | |
2765 | // expression, we export the value. We ignore the fact that it has | |
2766 | // a name. | |
2767 | void | |
2768 | do_export(Export* exp) const | |
2769 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2770 | ||
d751bb78 | 2771 | void |
2772 | do_dump_expression(Ast_dump_context*) const; | |
2773 | ||
e440a328 | 2774 | private: |
2775 | // The constant. | |
2776 | Named_object* constant_; | |
2777 | // The type of this reference. This is used if the constant has an | |
2778 | // abstract type. | |
2779 | Type* type_; | |
13e818f5 | 2780 | // Used to prevent infinite recursion when a constant incorrectly |
2781 | // refers to itself. | |
2782 | mutable bool seen_; | |
e440a328 | 2783 | }; |
2784 | ||
ba4aedd4 | 2785 | // Traversal. |
2786 | ||
2787 | int | |
2788 | Const_expression::do_traverse(Traverse* traverse) | |
2789 | { | |
2790 | if (this->type_ != NULL) | |
2791 | return Type::traverse(this->type_, traverse); | |
2792 | return TRAVERSE_CONTINUE; | |
2793 | } | |
2794 | ||
e440a328 | 2795 | // Lower a constant expression. This is where we convert the |
2796 | // predeclared constant iota into an integer value. | |
2797 | ||
2798 | Expression* | |
ceeb4318 | 2799 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2800 | Statement_inserter*, int iota_value) | |
e440a328 | 2801 | { |
2802 | if (this->constant_->const_value()->expr()->classification() | |
2803 | == EXPRESSION_IOTA) | |
2804 | { | |
2805 | if (iota_value == -1) | |
2806 | { | |
631d5788 | 2807 | go_error_at(this->location(), |
2808 | "iota is only defined in const declarations"); | |
e440a328 | 2809 | iota_value = 0; |
2810 | } | |
e67508fa | 2811 | return Expression::make_integer_ul(iota_value, NULL, this->location()); |
e440a328 | 2812 | } |
2813 | ||
2814 | // Make sure that the constant itself has been lowered. | |
2815 | gogo->lower_constant(this->constant_); | |
2816 | ||
2817 | return this; | |
2818 | } | |
2819 | ||
0c77715b | 2820 | // Return a numeric constant value. |
e440a328 | 2821 | |
2822 | bool | |
0c77715b | 2823 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2824 | { |
13e818f5 | 2825 | if (this->seen_) |
2826 | return false; | |
2827 | ||
e440a328 | 2828 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2829 | |
13e818f5 | 2830 | this->seen_ = true; |
2831 | ||
0c77715b | 2832 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2833 | |
13e818f5 | 2834 | this->seen_ = false; |
2835 | ||
e440a328 | 2836 | Type* ctype; |
2837 | if (this->type_ != NULL) | |
2838 | ctype = this->type_; | |
2839 | else | |
2840 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2841 | if (r && ctype != NULL) |
2842 | { | |
0c77715b | 2843 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2844 | return false; |
e440a328 | 2845 | } |
e440a328 | 2846 | |
e440a328 | 2847 | return r; |
2848 | } | |
2849 | ||
af6b489a | 2850 | bool |
2851 | Const_expression::do_string_constant_value(std::string* val) const | |
2852 | { | |
2853 | if (this->seen_) | |
2854 | return false; | |
2855 | ||
2856 | Expression* e = this->constant_->const_value()->expr(); | |
2857 | ||
2858 | this->seen_ = true; | |
2859 | bool ok = e->string_constant_value(val); | |
2860 | this->seen_ = false; | |
2861 | ||
2862 | return ok; | |
2863 | } | |
2864 | ||
e440a328 | 2865 | // Return the type of the const reference. |
2866 | ||
2867 | Type* | |
2868 | Const_expression::do_type() | |
2869 | { | |
2870 | if (this->type_ != NULL) | |
2871 | return this->type_; | |
13e818f5 | 2872 | |
2f78f012 | 2873 | Named_constant* nc = this->constant_->const_value(); |
2874 | ||
2875 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2876 | { |
4b2ab536 | 2877 | if (nc->type() == NULL || !nc->type()->is_error_type()) |
2878 | { | |
2879 | Location loc = this->location(); | |
2880 | if (!this->seen_) | |
2881 | loc = nc->location(); | |
2882 | go_error_at(loc, "constant refers to itself"); | |
2883 | } | |
2884 | this->set_is_error(); | |
13e818f5 | 2885 | this->type_ = Type::make_error_type(); |
4b2ab536 | 2886 | nc->set_type(this->type_); |
13e818f5 | 2887 | return this->type_; |
2888 | } | |
2889 | ||
2890 | this->seen_ = true; | |
2891 | ||
e440a328 | 2892 | Type* ret = nc->type(); |
13e818f5 | 2893 | |
e440a328 | 2894 | if (ret != NULL) |
13e818f5 | 2895 | { |
2896 | this->seen_ = false; | |
2897 | return ret; | |
2898 | } | |
2899 | ||
e440a328 | 2900 | // During parsing, a named constant may have a NULL type, but we |
2901 | // must not return a NULL type here. | |
13e818f5 | 2902 | ret = nc->expr()->type(); |
2903 | ||
2904 | this->seen_ = false; | |
2905 | ||
4b2ab536 | 2906 | if (ret->is_error_type()) |
2907 | nc->set_type(ret); | |
2908 | ||
13e818f5 | 2909 | return ret; |
e440a328 | 2910 | } |
2911 | ||
2912 | // Set the type of the const reference. | |
2913 | ||
2914 | void | |
2915 | Const_expression::do_determine_type(const Type_context* context) | |
2916 | { | |
2917 | Type* ctype = this->constant_->const_value()->type(); | |
2918 | Type* cetype = (ctype != NULL | |
2919 | ? ctype | |
2920 | : this->constant_->const_value()->expr()->type()); | |
2921 | if (ctype != NULL && !ctype->is_abstract()) | |
2922 | ; | |
2923 | else if (context->type != NULL | |
0c77715b | 2924 | && context->type->is_numeric_type() |
2925 | && cetype->is_numeric_type()) | |
e440a328 | 2926 | this->type_ = context->type; |
2927 | else if (context->type != NULL | |
2928 | && context->type->is_string_type() | |
2929 | && cetype->is_string_type()) | |
2930 | this->type_ = context->type; | |
2931 | else if (context->type != NULL | |
2932 | && context->type->is_boolean_type() | |
2933 | && cetype->is_boolean_type()) | |
2934 | this->type_ = context->type; | |
2935 | else if (!context->may_be_abstract) | |
2936 | { | |
2937 | if (cetype->is_abstract()) | |
2938 | cetype = cetype->make_non_abstract_type(); | |
2939 | this->type_ = cetype; | |
2940 | } | |
2941 | } | |
2942 | ||
a7f064d5 | 2943 | // Check for a loop in which the initializer of a constant refers to |
2944 | // the constant itself. | |
e440a328 | 2945 | |
2946 | void | |
a7f064d5 | 2947 | Const_expression::check_for_init_loop() |
e440a328 | 2948 | { |
5c13bd80 | 2949 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2950 | return; |
2951 | ||
a7f064d5 | 2952 | if (this->seen_) |
2953 | { | |
2954 | this->report_error(_("constant refers to itself")); | |
2955 | this->type_ = Type::make_error_type(); | |
2956 | return; | |
2957 | } | |
2958 | ||
d5b605df | 2959 | Expression* init = this->constant_->const_value()->expr(); |
2960 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2961 | |
2962 | this->seen_ = true; | |
d5b605df | 2963 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2964 | this->seen_ = false; |
2965 | ||
d5b605df | 2966 | if (find_named_object.found()) |
2967 | { | |
5c13bd80 | 2968 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2969 | { |
2970 | this->report_error(_("constant refers to itself")); | |
2971 | this->type_ = Type::make_error_type(); | |
2972 | } | |
d5b605df | 2973 | return; |
2974 | } | |
a7f064d5 | 2975 | } |
2976 | ||
2977 | // Check types of a const reference. | |
2978 | ||
2979 | void | |
2980 | Const_expression::do_check_types(Gogo*) | |
2981 | { | |
5c13bd80 | 2982 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2983 | return; |
2984 | ||
2985 | this->check_for_init_loop(); | |
d5b605df | 2986 | |
0c77715b | 2987 | // Check that numeric constant fits in type. |
2988 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2989 | { |
0c77715b | 2990 | Numeric_constant nc; |
2991 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2992 | { |
0c77715b | 2993 | if (!nc.set_type(this->type_, true, this->location())) |
2994 | this->set_is_error(); | |
e440a328 | 2995 | } |
e440a328 | 2996 | } |
2997 | } | |
2998 | ||
ea664253 | 2999 | // Return the backend representation for a const reference. |
e440a328 | 3000 | |
ea664253 | 3001 | Bexpression* |
3002 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3003 | { |
12373dd5 | 3004 | if (this->is_error_expression() |
3005 | || (this->type_ != NULL && this->type_->is_error())) | |
3006 | { | |
3007 | go_assert(saw_errors()); | |
3008 | return context->backend()->error_expression(); | |
3009 | } | |
e440a328 | 3010 | |
3011 | // If the type has been set for this expression, but the underlying | |
3012 | // object is an abstract int or float, we try to get the abstract | |
3013 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 3014 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 3015 | if (this->type_ != NULL |
0c77715b | 3016 | && this->type_->is_numeric_type() |
a68492b4 | 3017 | && (this->constant_->const_value()->type() == NULL |
3018 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 3019 | { |
0c77715b | 3020 | Numeric_constant nc; |
3021 | if (expr->numeric_constant_value(&nc) | |
3022 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 3023 | { |
0c77715b | 3024 | Expression* e = nc.expression(this->location()); |
ea664253 | 3025 | return e->get_backend(context); |
e440a328 | 3026 | } |
e440a328 | 3027 | } |
3028 | ||
2c809f8f | 3029 | if (this->type_ != NULL) |
f2de4532 | 3030 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 3031 | return expr->get_backend(context); |
e440a328 | 3032 | } |
3033 | ||
d751bb78 | 3034 | // Dump ast representation for constant expression. |
3035 | ||
3036 | void | |
3037 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3038 | { | |
3039 | ast_dump_context->ostream() << this->constant_->name(); | |
3040 | } | |
3041 | ||
e440a328 | 3042 | // Make a reference to a constant in an expression. |
3043 | ||
3044 | Expression* | |
3045 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 3046 | Location location) |
e440a328 | 3047 | { |
3048 | return new Const_expression(constant, location); | |
3049 | } | |
3050 | ||
d5b605df | 3051 | // Find a named object in an expression. |
3052 | ||
3053 | int | |
3054 | Find_named_object::expression(Expression** pexpr) | |
3055 | { | |
3056 | switch ((*pexpr)->classification()) | |
3057 | { | |
3058 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 3059 | { |
3060 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
3061 | if (ce->named_object() == this->no_) | |
3062 | break; | |
3063 | ||
3064 | // We need to check a constant initializer explicitly, as | |
3065 | // loops here will not be caught by the loop checking for | |
3066 | // variable initializers. | |
3067 | ce->check_for_init_loop(); | |
3068 | ||
3069 | return TRAVERSE_CONTINUE; | |
3070 | } | |
3071 | ||
d5b605df | 3072 | case Expression::EXPRESSION_VAR_REFERENCE: |
3073 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
3074 | break; | |
3075 | return TRAVERSE_CONTINUE; | |
3076 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
3077 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
3078 | break; | |
3079 | return TRAVERSE_CONTINUE; | |
3080 | default: | |
3081 | return TRAVERSE_CONTINUE; | |
3082 | } | |
3083 | this->found_ = true; | |
3084 | return TRAVERSE_EXIT; | |
3085 | } | |
3086 | ||
e440a328 | 3087 | // The nil value. |
3088 | ||
3089 | class Nil_expression : public Expression | |
3090 | { | |
3091 | public: | |
b13c66cd | 3092 | Nil_expression(Location location) |
e440a328 | 3093 | : Expression(EXPRESSION_NIL, location) |
3094 | { } | |
3095 | ||
3096 | static Expression* | |
3097 | do_import(Import*); | |
3098 | ||
3099 | protected: | |
3100 | bool | |
3101 | do_is_constant() const | |
3102 | { return true; } | |
3103 | ||
f9ca30f9 | 3104 | bool |
3ae06f68 | 3105 | do_is_static_initializer() const |
f9ca30f9 | 3106 | { return true; } |
3107 | ||
e440a328 | 3108 | Type* |
3109 | do_type() | |
3110 | { return Type::make_nil_type(); } | |
3111 | ||
3112 | void | |
3113 | do_determine_type(const Type_context*) | |
3114 | { } | |
3115 | ||
3116 | Expression* | |
3117 | do_copy() | |
3118 | { return this; } | |
3119 | ||
ea664253 | 3120 | Bexpression* |
3121 | do_get_backend(Translate_context* context) | |
3122 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 3123 | |
3124 | void | |
3125 | do_export(Export* exp) const | |
3126 | { exp->write_c_string("nil"); } | |
d751bb78 | 3127 | |
3128 | void | |
3129 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3130 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 3131 | }; |
3132 | ||
3133 | // Import a nil expression. | |
3134 | ||
3135 | Expression* | |
3136 | Nil_expression::do_import(Import* imp) | |
3137 | { | |
3138 | imp->require_c_string("nil"); | |
3139 | return Expression::make_nil(imp->location()); | |
3140 | } | |
3141 | ||
3142 | // Make a nil expression. | |
3143 | ||
3144 | Expression* | |
b13c66cd | 3145 | Expression::make_nil(Location location) |
e440a328 | 3146 | { |
3147 | return new Nil_expression(location); | |
3148 | } | |
3149 | ||
3150 | // The value of the predeclared constant iota. This is little more | |
3151 | // than a marker. This will be lowered to an integer in | |
3152 | // Const_expression::do_lower, which is where we know the value that | |
3153 | // it should have. | |
3154 | ||
3155 | class Iota_expression : public Parser_expression | |
3156 | { | |
3157 | public: | |
b13c66cd | 3158 | Iota_expression(Location location) |
e440a328 | 3159 | : Parser_expression(EXPRESSION_IOTA, location) |
3160 | { } | |
3161 | ||
3162 | protected: | |
3163 | Expression* | |
ceeb4318 | 3164 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 3165 | { go_unreachable(); } |
e440a328 | 3166 | |
3167 | // There should only ever be one of these. | |
3168 | Expression* | |
3169 | do_copy() | |
c3e6f413 | 3170 | { go_unreachable(); } |
d751bb78 | 3171 | |
3172 | void | |
3173 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3174 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3175 | }; |
3176 | ||
3177 | // Make an iota expression. This is only called for one case: the | |
3178 | // value of the predeclared constant iota. | |
3179 | ||
3180 | Expression* | |
3181 | Expression::make_iota() | |
3182 | { | |
b13c66cd | 3183 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3184 | return &iota_expression; |
3185 | } | |
3186 | ||
da244e59 | 3187 | // Class Type_conversion_expression. |
e440a328 | 3188 | |
3189 | // Traversal. | |
3190 | ||
3191 | int | |
3192 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3193 | { | |
3194 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3195 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3196 | return TRAVERSE_EXIT; | |
3197 | return TRAVERSE_CONTINUE; | |
3198 | } | |
3199 | ||
3200 | // Convert to a constant at lowering time. | |
3201 | ||
3202 | Expression* | |
ceeb4318 | 3203 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3204 | Statement_inserter*, int) | |
e440a328 | 3205 | { |
3206 | Type* type = this->type_; | |
3207 | Expression* val = this->expr_; | |
b13c66cd | 3208 | Location location = this->location(); |
e440a328 | 3209 | |
0c77715b | 3210 | if (type->is_numeric_type()) |
e440a328 | 3211 | { |
0c77715b | 3212 | Numeric_constant nc; |
3213 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3214 | { |
0c77715b | 3215 | if (!nc.set_type(type, true, location)) |
3216 | return Expression::make_error(location); | |
3217 | return nc.expression(location); | |
e440a328 | 3218 | } |
e440a328 | 3219 | } |
3220 | ||
d7739c9a | 3221 | // According to the language specification on string conversions |
3222 | // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): | |
3223 | // When converting an integer into a string, the string will be a UTF-8 | |
3224 | // representation of the integer and integers "outside the range of valid | |
3225 | // Unicode code points are converted to '\uFFFD'." | |
3226 | if (type->is_string_type()) | |
3227 | { | |
3228 | Numeric_constant nc; | |
3229 | if (val->numeric_constant_value(&nc) && nc.is_int()) | |
3230 | { | |
3231 | // An integer value doesn't fit in the Unicode code point range if it | |
3232 | // overflows the Go "int" type or is negative. | |
3233 | unsigned long ul; | |
3234 | if (!nc.set_type(Type::lookup_integer_type("int"), false, location) | |
3235 | || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) | |
3236 | return Expression::make_string("\ufffd", location); | |
3237 | } | |
3238 | } | |
3239 | ||
55072f2b | 3240 | if (type->is_slice_type()) |
e440a328 | 3241 | { |
3242 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3243 | bool is_byte = (element_type->integer_type() != NULL |
3244 | && element_type->integer_type()->is_byte()); | |
3245 | bool is_rune = (element_type->integer_type() != NULL | |
3246 | && element_type->integer_type()->is_rune()); | |
3247 | if (is_byte || is_rune) | |
e440a328 | 3248 | { |
3249 | std::string s; | |
3250 | if (val->string_constant_value(&s)) | |
3251 | { | |
3252 | Expression_list* vals = new Expression_list(); | |
3253 | if (is_byte) | |
3254 | { | |
3255 | for (std::string::const_iterator p = s.begin(); | |
3256 | p != s.end(); | |
3257 | p++) | |
3258 | { | |
e67508fa | 3259 | unsigned char c = static_cast<unsigned char>(*p); |
3260 | vals->push_back(Expression::make_integer_ul(c, | |
3261 | element_type, | |
3262 | location)); | |
e440a328 | 3263 | } |
3264 | } | |
3265 | else | |
3266 | { | |
3267 | const char *p = s.data(); | |
3268 | const char *pend = s.data() + s.length(); | |
3269 | while (p < pend) | |
3270 | { | |
3271 | unsigned int c; | |
3272 | int adv = Lex::fetch_char(p, &c); | |
3273 | if (adv == 0) | |
3274 | { | |
631d5788 | 3275 | go_warning_at(this->location(), 0, |
e440a328 | 3276 | "invalid UTF-8 encoding"); |
3277 | adv = 1; | |
3278 | } | |
3279 | p += adv; | |
e67508fa | 3280 | vals->push_back(Expression::make_integer_ul(c, |
3281 | element_type, | |
3282 | location)); | |
e440a328 | 3283 | } |
3284 | } | |
3285 | ||
3286 | return Expression::make_slice_composite_literal(type, vals, | |
3287 | location); | |
3288 | } | |
3289 | } | |
3290 | } | |
3291 | ||
3292 | return this; | |
3293 | } | |
3294 | ||
35a54f17 | 3295 | // Flatten a type conversion by using a temporary variable for the slice |
3296 | // in slice to string conversions. | |
3297 | ||
3298 | Expression* | |
3299 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3300 | Statement_inserter* inserter) | |
3301 | { | |
5bf8be8b | 3302 | if (this->type()->is_error_type() || this->expr_->is_error_expression()) |
3303 | { | |
3304 | go_assert(saw_errors()); | |
3305 | return Expression::make_error(this->location()); | |
3306 | } | |
3307 | ||
2c809f8f | 3308 | if (((this->type()->is_string_type() |
3309 | && this->expr_->type()->is_slice_type()) | |
8ba8cc87 | 3310 | || this->expr_->type()->interface_type() != NULL) |
35a54f17 | 3311 | && !this->expr_->is_variable()) |
3312 | { | |
3313 | Temporary_statement* temp = | |
3314 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3315 | inserter->insert(temp); | |
3316 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3317 | } | |
3318 | return this; | |
3319 | } | |
3320 | ||
1ca01a59 | 3321 | // Return whether a type conversion is a constant. |
3322 | ||
3323 | bool | |
3324 | Type_conversion_expression::do_is_constant() const | |
3325 | { | |
3326 | if (!this->expr_->is_constant()) | |
3327 | return false; | |
3328 | ||
3329 | // A conversion to a type that may not be used as a constant is not | |
3330 | // a constant. For example, []byte(nil). | |
3331 | Type* type = this->type_; | |
3332 | if (type->integer_type() == NULL | |
3333 | && type->float_type() == NULL | |
3334 | && type->complex_type() == NULL | |
3335 | && !type->is_boolean_type() | |
3336 | && !type->is_string_type()) | |
3337 | return false; | |
3338 | ||
3339 | return true; | |
3340 | } | |
3341 | ||
3ae06f68 | 3342 | // Return whether a type conversion can be used in a constant |
3343 | // initializer. | |
0e168074 | 3344 | |
3345 | bool | |
3ae06f68 | 3346 | Type_conversion_expression::do_is_static_initializer() const |
0e168074 | 3347 | { |
3348 | Type* type = this->type_; | |
3349 | Type* expr_type = this->expr_->type(); | |
3350 | ||
3351 | if (type->interface_type() != NULL | |
3352 | || expr_type->interface_type() != NULL) | |
3353 | return false; | |
3354 | ||
3ae06f68 | 3355 | if (!this->expr_->is_static_initializer()) |
0e168074 | 3356 | return false; |
3357 | ||
3358 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3359 | return true; | |
3360 | ||
03118c21 | 3361 | if (type->is_string_type() && expr_type->is_string_type()) |
3362 | return true; | |
3363 | ||
3364 | if ((type->is_numeric_type() | |
3365 | || type->is_boolean_type() | |
3366 | || type->points_to() != NULL) | |
3367 | && (expr_type->is_numeric_type() | |
3368 | || expr_type->is_boolean_type() | |
3369 | || expr_type->points_to() != NULL)) | |
3370 | return true; | |
3371 | ||
3372 | return false; | |
0e168074 | 3373 | } |
3374 | ||
0c77715b | 3375 | // Return the constant numeric value if there is one. |
e440a328 | 3376 | |
3377 | bool | |
0c77715b | 3378 | Type_conversion_expression::do_numeric_constant_value( |
3379 | Numeric_constant* nc) const | |
e440a328 | 3380 | { |
0c77715b | 3381 | if (!this->type_->is_numeric_type()) |
e440a328 | 3382 | return false; |
0c77715b | 3383 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3384 | return false; |
0c77715b | 3385 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3386 | } |
3387 | ||
3388 | // Return the constant string value if there is one. | |
3389 | ||
3390 | bool | |
3391 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3392 | { | |
3393 | if (this->type_->is_string_type() | |
3394 | && this->expr_->type()->integer_type() != NULL) | |
3395 | { | |
0c77715b | 3396 | Numeric_constant nc; |
3397 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3398 | { |
0c77715b | 3399 | unsigned long ival; |
3400 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3401 | { |
0c77715b | 3402 | val->clear(); |
3403 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3404 | return true; |
3405 | } | |
3406 | } | |
e440a328 | 3407 | } |
3408 | ||
3409 | // FIXME: Could handle conversion from const []int here. | |
3410 | ||
3411 | return false; | |
3412 | } | |
3413 | ||
da244e59 | 3414 | // Determine the resulting type of the conversion. |
3415 | ||
3416 | void | |
3417 | Type_conversion_expression::do_determine_type(const Type_context*) | |
3418 | { | |
3419 | Type_context subcontext(this->type_, false); | |
3420 | this->expr_->determine_type(&subcontext); | |
3421 | } | |
3422 | ||
e440a328 | 3423 | // Check that types are convertible. |
3424 | ||
3425 | void | |
3426 | Type_conversion_expression::do_check_types(Gogo*) | |
3427 | { | |
3428 | Type* type = this->type_; | |
3429 | Type* expr_type = this->expr_->type(); | |
3430 | std::string reason; | |
3431 | ||
5c13bd80 | 3432 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3433 | { |
842f6425 | 3434 | this->set_is_error(); |
3435 | return; | |
3436 | } | |
3437 | ||
e440a328 | 3438 | if (this->may_convert_function_types_ |
3439 | && type->function_type() != NULL | |
3440 | && expr_type->function_type() != NULL) | |
3441 | return; | |
3442 | ||
3443 | if (Type::are_convertible(type, expr_type, &reason)) | |
3444 | return; | |
3445 | ||
631d5788 | 3446 | go_error_at(this->location(), "%s", reason.c_str()); |
e440a328 | 3447 | this->set_is_error(); |
3448 | } | |
3449 | ||
de590a61 | 3450 | // Copy. |
3451 | ||
3452 | Expression* | |
3453 | Type_conversion_expression::do_copy() | |
3454 | { | |
3455 | return new Type_conversion_expression(this->type_->copy_expressions(), | |
3456 | this->expr_->copy(), | |
3457 | this->location()); | |
3458 | } | |
3459 | ||
ea664253 | 3460 | // Get the backend representation for a type conversion. |
e440a328 | 3461 | |
ea664253 | 3462 | Bexpression* |
3463 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3464 | { |
e440a328 | 3465 | Type* type = this->type_; |
3466 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3467 | |
3468 | Gogo* gogo = context->gogo(); | |
3469 | Btype* btype = type->get_backend(gogo); | |
2c809f8f | 3470 | Location loc = this->location(); |
3471 | ||
3472 | if (Type::are_identical(type, expr_type, false, NULL)) | |
859cdc93 | 3473 | { |
3474 | Bexpression* bexpr = this->expr_->get_backend(context); | |
3475 | return gogo->backend()->convert_expression(btype, bexpr, loc); | |
3476 | } | |
2c809f8f | 3477 | else if (type->interface_type() != NULL |
3478 | || expr_type->interface_type() != NULL) | |
e440a328 | 3479 | { |
2c809f8f | 3480 | Expression* conversion = |
3481 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3482 | this->location()); | |
ea664253 | 3483 | return conversion->get_backend(context); |
e440a328 | 3484 | } |
3485 | else if (type->is_string_type() | |
3486 | && expr_type->integer_type() != NULL) | |
3487 | { | |
2c809f8f | 3488 | mpz_t intval; |
3489 | Numeric_constant nc; | |
3490 | if (this->expr_->numeric_constant_value(&nc) | |
3491 | && nc.to_int(&intval) | |
3492 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3493 | { |
e440a328 | 3494 | std::string s; |
2c809f8f | 3495 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3496 | mpz_clear(intval); | |
3497 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3498 | return se->get_backend(context); |
e440a328 | 3499 | } |
3500 | ||
f16ab008 | 3501 | Expression* i2s_expr = |
736a16ba | 3502 | Runtime::make_call(Runtime::INTSTRING, loc, 2, |
3503 | Expression::make_nil(loc), this->expr_); | |
ea664253 | 3504 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3505 | } |
55072f2b | 3506 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3507 | { |
55072f2b | 3508 | Array_type* a = expr_type->array_type(); |
e440a328 | 3509 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3510 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3511 | go_assert(this->expr_->is_variable()); |
3512 | ||
3513 | Runtime::Function code; | |
60963afd | 3514 | if (e->integer_type()->is_byte()) |
736a16ba | 3515 | code = Runtime::SLICEBYTETOSTRING; |
e440a328 | 3516 | else |
35a54f17 | 3517 | { |
3518 | go_assert(e->integer_type()->is_rune()); | |
736a16ba | 3519 | code = Runtime::SLICERUNETOSTRING; |
35a54f17 | 3520 | } |
736a16ba | 3521 | return Runtime::make_call(code, loc, 2, Expression::make_nil(loc), |
3522 | this->expr_)->get_backend(context); | |
e440a328 | 3523 | } |
411eb89e | 3524 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3525 | { |
3526 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3527 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3528 | |
2c809f8f | 3529 | Runtime::Function code; |
60963afd | 3530 | if (e->integer_type()->is_byte()) |
736a16ba | 3531 | code = Runtime::STRINGTOSLICEBYTE; |
e440a328 | 3532 | else |
3533 | { | |
60963afd | 3534 | go_assert(e->integer_type()->is_rune()); |
736a16ba | 3535 | code = Runtime::STRINGTOSLICERUNE; |
e440a328 | 3536 | } |
736a16ba | 3537 | Expression* s2a = Runtime::make_call(code, loc, 2, |
3538 | Expression::make_nil(loc), | |
3539 | this->expr_); | |
ea664253 | 3540 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3541 | } |
3542 | else if (type->is_numeric_type()) | |
3543 | { | |
3544 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
859cdc93 | 3545 | Bexpression* bexpr = this->expr_->get_backend(context); |
ea664253 | 3546 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3547 | } |
3548 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3549 | && (expr_type->points_to() != NULL |
3550 | || expr_type->integer_type())) | |
3551 | || (expr_type->is_unsafe_pointer_type() | |
3552 | && type->points_to() != NULL) | |
3553 | || (this->may_convert_function_types_ | |
3554 | && type->function_type() != NULL | |
3555 | && expr_type->function_type() != NULL)) | |
859cdc93 | 3556 | { |
3557 | Bexpression* bexpr = this->expr_->get_backend(context); | |
3558 | return gogo->backend()->convert_expression(btype, bexpr, loc); | |
3559 | } | |
e440a328 | 3560 | else |
2c809f8f | 3561 | { |
3562 | Expression* conversion = | |
3563 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3564 | return conversion->get_backend(context); |
2c809f8f | 3565 | } |
e440a328 | 3566 | } |
3567 | ||
3568 | // Output a type conversion in a constant expression. | |
3569 | ||
3570 | void | |
3571 | Type_conversion_expression::do_export(Export* exp) const | |
3572 | { | |
3573 | exp->write_c_string("convert("); | |
3574 | exp->write_type(this->type_); | |
3575 | exp->write_c_string(", "); | |
3576 | this->expr_->export_expression(exp); | |
3577 | exp->write_c_string(")"); | |
3578 | } | |
3579 | ||
3580 | // Import a type conversion or a struct construction. | |
3581 | ||
3582 | Expression* | |
3583 | Type_conversion_expression::do_import(Import* imp) | |
3584 | { | |
3585 | imp->require_c_string("convert("); | |
3586 | Type* type = imp->read_type(); | |
3587 | imp->require_c_string(", "); | |
3588 | Expression* val = Expression::import_expression(imp); | |
3589 | imp->require_c_string(")"); | |
3590 | return Expression::make_cast(type, val, imp->location()); | |
3591 | } | |
3592 | ||
d751bb78 | 3593 | // Dump ast representation for a type conversion expression. |
3594 | ||
3595 | void | |
3596 | Type_conversion_expression::do_dump_expression( | |
3597 | Ast_dump_context* ast_dump_context) const | |
3598 | { | |
3599 | ast_dump_context->dump_type(this->type_); | |
3600 | ast_dump_context->ostream() << "("; | |
3601 | ast_dump_context->dump_expression(this->expr_); | |
3602 | ast_dump_context->ostream() << ") "; | |
3603 | } | |
3604 | ||
e440a328 | 3605 | // Make a type cast expression. |
3606 | ||
3607 | Expression* | |
b13c66cd | 3608 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3609 | { |
3610 | if (type->is_error_type() || val->is_error_expression()) | |
3611 | return Expression::make_error(location); | |
3612 | return new Type_conversion_expression(type, val, location); | |
3613 | } | |
3614 | ||
98f62f7a | 3615 | // Class Unsafe_type_conversion_expression. |
9581e91d | 3616 | |
3617 | // Traversal. | |
3618 | ||
3619 | int | |
3620 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3621 | { | |
3622 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3623 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3624 | return TRAVERSE_EXIT; | |
3625 | return TRAVERSE_CONTINUE; | |
3626 | } | |
3627 | ||
3ae06f68 | 3628 | // Return whether an unsafe type conversion can be used as a constant |
3629 | // initializer. | |
aa5ae575 | 3630 | |
3631 | bool | |
3ae06f68 | 3632 | Unsafe_type_conversion_expression::do_is_static_initializer() const |
aa5ae575 | 3633 | { |
3634 | Type* type = this->type_; | |
3635 | Type* expr_type = this->expr_->type(); | |
3636 | ||
3637 | if (type->interface_type() != NULL | |
3638 | || expr_type->interface_type() != NULL) | |
3639 | return false; | |
3640 | ||
3ae06f68 | 3641 | if (!this->expr_->is_static_initializer()) |
aa5ae575 | 3642 | return false; |
3643 | ||
3644 | if (Type::are_convertible(type, expr_type, NULL)) | |
3645 | return true; | |
3646 | ||
03118c21 | 3647 | if (type->is_string_type() && expr_type->is_string_type()) |
3648 | return true; | |
3649 | ||
3650 | if ((type->is_numeric_type() | |
3651 | || type->is_boolean_type() | |
3652 | || type->points_to() != NULL) | |
3653 | && (expr_type->is_numeric_type() | |
3654 | || expr_type->is_boolean_type() | |
3655 | || expr_type->points_to() != NULL)) | |
3656 | return true; | |
3657 | ||
3658 | return false; | |
aa5ae575 | 3659 | } |
3660 | ||
de590a61 | 3661 | // Copy. |
3662 | ||
3663 | Expression* | |
3664 | Unsafe_type_conversion_expression::do_copy() | |
3665 | { | |
3666 | return new Unsafe_type_conversion_expression(this->type_->copy_expressions(), | |
3667 | this->expr_->copy(), | |
3668 | this->location()); | |
3669 | } | |
3670 | ||
9581e91d | 3671 | // Convert to backend representation. |
3672 | ||
ea664253 | 3673 | Bexpression* |
3674 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3675 | { |
3676 | // We are only called for a limited number of cases. | |
3677 | ||
3678 | Type* t = this->type_; | |
3679 | Type* et = this->expr_->type(); | |
5c4802f1 | 3680 | |
3681 | if (t->is_error_type() | |
3682 | || this->expr_->is_error_expression() | |
3683 | || et->is_error_type()) | |
3684 | { | |
3685 | go_assert(saw_errors()); | |
3686 | return context->backend()->error_expression(); | |
3687 | } | |
3688 | ||
2c809f8f | 3689 | if (t->array_type() != NULL) |
3690 | go_assert(et->array_type() != NULL | |
3691 | && t->is_slice_type() == et->is_slice_type()); | |
3692 | else if (t->struct_type() != NULL) | |
9581e91d | 3693 | { |
2c809f8f | 3694 | if (t->named_type() != NULL |
3695 | && et->named_type() != NULL | |
3696 | && !Type::are_convertible(t, et, NULL)) | |
3697 | { | |
3698 | go_assert(saw_errors()); | |
ea664253 | 3699 | return context->backend()->error_expression(); |
2c809f8f | 3700 | } |
3701 | ||
3702 | go_assert(et->struct_type() != NULL | |
3703 | && Type::are_convertible(t, et, NULL)); | |
3704 | } | |
3705 | else if (t->map_type() != NULL) | |
c484d925 | 3706 | go_assert(et->map_type() != NULL); |
9581e91d | 3707 | else if (t->channel_type() != NULL) |
c484d925 | 3708 | go_assert(et->channel_type() != NULL); |
09ea332d | 3709 | else if (t->points_to() != NULL) |
2c809f8f | 3710 | go_assert(et->points_to() != NULL |
3711 | || et->channel_type() != NULL | |
3712 | || et->map_type() != NULL | |
3713 | || et->function_type() != NULL | |
132ed071 | 3714 | || et->integer_type() != NULL |
2c809f8f | 3715 | || et->is_nil_type()); |
9581e91d | 3716 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3717 | go_assert(t->points_to() != NULL); |
2c809f8f | 3718 | else if (t->interface_type() != NULL) |
9581e91d | 3719 | { |
2c809f8f | 3720 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3721 | go_assert(et->interface_type() != NULL |
2c809f8f | 3722 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3723 | } |
588e3cf9 | 3724 | else if (t->integer_type() != NULL) |
2c809f8f | 3725 | go_assert(et->is_boolean_type() |
3726 | || et->integer_type() != NULL | |
3727 | || et->function_type() != NULL | |
3728 | || et->points_to() != NULL | |
3729 | || et->map_type() != NULL | |
8ba8cc87 | 3730 | || et->channel_type() != NULL |
3731 | || et->is_nil_type()); | |
cd39797e | 3732 | else if (t->function_type() != NULL) |
3733 | go_assert(et->points_to() != NULL); | |
9581e91d | 3734 | else |
c3e6f413 | 3735 | go_unreachable(); |
9581e91d | 3736 | |
2c809f8f | 3737 | Gogo* gogo = context->gogo(); |
3738 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3739 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3740 | Location loc = this->location(); |
ea664253 | 3741 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3742 | } |
3743 | ||
d751bb78 | 3744 | // Dump ast representation for an unsafe type conversion expression. |
3745 | ||
3746 | void | |
3747 | Unsafe_type_conversion_expression::do_dump_expression( | |
3748 | Ast_dump_context* ast_dump_context) const | |
3749 | { | |
3750 | ast_dump_context->dump_type(this->type_); | |
3751 | ast_dump_context->ostream() << "("; | |
3752 | ast_dump_context->dump_expression(this->expr_); | |
3753 | ast_dump_context->ostream() << ") "; | |
3754 | } | |
3755 | ||
9581e91d | 3756 | // Make an unsafe type conversion expression. |
3757 | ||
3758 | Expression* | |
3759 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3760 | Location location) |
9581e91d | 3761 | { |
3762 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3763 | } | |
3764 | ||
76f85fd6 | 3765 | // Class Unary_expression. |
e440a328 | 3766 | |
03118c21 | 3767 | // Call the address_taken method of the operand if needed. This is |
3768 | // called after escape analysis but before inserting write barriers. | |
3769 | ||
3770 | void | |
c1177ba4 | 3771 | Unary_expression::check_operand_address_taken(Gogo*) |
03118c21 | 3772 | { |
3773 | if (this->op_ != OPERATOR_AND) | |
3774 | return; | |
3775 | ||
3776 | // If this->escapes_ is false at this point, then it was set to | |
3777 | // false by an explicit call to set_does_not_escape, and the value | |
3778 | // does not escape. If this->escapes_ is true, we may be able to | |
3779 | // set it to false if taking the address of a variable that does not | |
3780 | // escape. | |
3781 | Node* n = Node::make_node(this); | |
3782 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
3783 | this->escapes_ = false; | |
3784 | ||
03118c21 | 3785 | Named_object* var = NULL; |
3786 | if (this->expr_->var_expression() != NULL) | |
3787 | var = this->expr_->var_expression()->named_object(); | |
3788 | else if (this->expr_->enclosed_var_expression() != NULL) | |
3789 | var = this->expr_->enclosed_var_expression()->variable(); | |
3790 | ||
3791 | if (this->escapes_ && var != NULL) | |
3792 | { | |
3793 | if (var->is_variable()) | |
3794 | this->escapes_ = var->var_value()->escapes(); | |
3795 | if (var->is_result_variable()) | |
3796 | this->escapes_ = var->result_var_value()->escapes(); | |
3797 | } | |
3798 | ||
3799 | this->expr_->address_taken(this->escapes_); | |
3800 | } | |
3801 | ||
e440a328 | 3802 | // If we are taking the address of a composite literal, and the |
2c809f8f | 3803 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3804 | // instead. |
3805 | ||
3806 | Expression* | |
ceeb4318 | 3807 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3808 | { |
b13c66cd | 3809 | Location loc = this->location(); |
e440a328 | 3810 | Operator op = this->op_; |
3811 | Expression* expr = this->expr_; | |
3812 | ||
3813 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3814 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3815 | ||
3816 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3817 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3818 | // analysis here? This case is found in math/unsafe.go and is | |
3819 | // therefore worth special casing. | |
3820 | if (op == OPERATOR_MULT) | |
3821 | { | |
3822 | Expression* e = expr; | |
3823 | while (e->classification() == EXPRESSION_CONVERSION) | |
3824 | { | |
3825 | Type_conversion_expression* te | |
3826 | = static_cast<Type_conversion_expression*>(e); | |
3827 | e = te->expr(); | |
3828 | } | |
3829 | ||
3830 | if (e->classification() == EXPRESSION_UNARY) | |
3831 | { | |
3832 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3833 | if (ue->op_ == OPERATOR_AND) | |
3834 | { | |
3835 | if (e == expr) | |
3836 | { | |
3837 | // *&x == x. | |
f4dea966 | 3838 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3839 | { | |
631d5788 | 3840 | go_error_at(ue->location(), |
3841 | "invalid operand for unary %<&%>"); | |
f4dea966 | 3842 | this->set_is_error(); |
3843 | } | |
e440a328 | 3844 | return ue->expr_; |
3845 | } | |
3846 | ue->set_does_not_escape(); | |
3847 | } | |
3848 | } | |
3849 | } | |
3850 | ||
55661ce9 | 3851 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3852 | // having to deal with TYPE_VOID in other places. | |
3853 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3854 | { | |
631d5788 | 3855 | go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); |
55661ce9 | 3856 | return Expression::make_error(this->location()); |
3857 | } | |
3858 | ||
d9f3743a | 3859 | // Check for an invalid pointer dereference. We need to do this |
3860 | // here because Unary_expression::do_type will return an error type | |
3861 | // in this case. That can cause code to appear erroneous, and | |
3862 | // therefore disappear at lowering time, without any error message. | |
3863 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3864 | { | |
3865 | this->report_error(_("expected pointer")); | |
3866 | return Expression::make_error(this->location()); | |
3867 | } | |
3868 | ||
59a401fe | 3869 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3870 | { |
0c77715b | 3871 | Numeric_constant nc; |
3872 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3873 | { |
0c77715b | 3874 | Numeric_constant result; |
af7a5274 | 3875 | bool issued_error; |
3876 | if (Unary_expression::eval_constant(op, &nc, loc, &result, | |
3877 | &issued_error)) | |
0c77715b | 3878 | return result.expression(loc); |
af7a5274 | 3879 | else if (issued_error) |
3880 | return Expression::make_error(this->location()); | |
e440a328 | 3881 | } |
3882 | } | |
3883 | ||
3884 | return this; | |
3885 | } | |
3886 | ||
f9ca30f9 | 3887 | // Flatten expression if a nil check must be performed and create temporary |
3888 | // variables if necessary. | |
3889 | ||
3890 | Expression* | |
3891 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3892 | Statement_inserter* inserter) | |
3893 | { | |
5bf8be8b | 3894 | if (this->is_error_expression() |
3895 | || this->expr_->is_error_expression() | |
3896 | || this->expr_->type()->is_error_type()) | |
3897 | { | |
3898 | go_assert(saw_errors()); | |
3899 | return Expression::make_error(this->location()); | |
3900 | } | |
f4dea966 | 3901 | |
f9ca30f9 | 3902 | Location location = this->location(); |
3903 | if (this->op_ == OPERATOR_MULT | |
3904 | && !this->expr_->is_variable()) | |
3905 | { | |
3906 | go_assert(this->expr_->type()->points_to() != NULL); | |
f614ea8b | 3907 | switch (this->requires_nil_check(gogo)) |
f9ca30f9 | 3908 | { |
f614ea8b | 3909 | case NIL_CHECK_ERROR_ENCOUNTERED: |
2a305b85 | 3910 | { |
3911 | go_assert(saw_errors()); | |
3912 | return Expression::make_error(this->location()); | |
3913 | } | |
f614ea8b | 3914 | case NIL_CHECK_NOT_NEEDED: |
3915 | break; | |
3916 | case NIL_CHECK_NEEDED: | |
3917 | this->create_temp_ = true; | |
3918 | break; | |
3919 | case NIL_CHECK_DEFAULT: | |
3920 | go_unreachable(); | |
f9ca30f9 | 3921 | } |
3922 | } | |
3923 | ||
3924 | if (this->create_temp_ && !this->expr_->is_variable()) | |
3925 | { | |
3926 | Temporary_statement* temp = | |
3927 | Statement::make_temporary(NULL, this->expr_, location); | |
3928 | inserter->insert(temp); | |
3929 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3930 | } | |
3931 | ||
3932 | return this; | |
3933 | } | |
3934 | ||
e440a328 | 3935 | // Return whether a unary expression is a constant. |
3936 | ||
3937 | bool | |
3938 | Unary_expression::do_is_constant() const | |
3939 | { | |
3940 | if (this->op_ == OPERATOR_MULT) | |
3941 | { | |
3942 | // Indirecting through a pointer is only constant if the object | |
3943 | // to which the expression points is constant, but we currently | |
3944 | // have no way to determine that. | |
3945 | return false; | |
3946 | } | |
3947 | else if (this->op_ == OPERATOR_AND) | |
3948 | { | |
3949 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3950 | // global variable, not constant otherwise. In other cases taking the |
3951 | // address is probably not a constant. | |
e440a328 | 3952 | Var_expression* ve = this->expr_->var_expression(); |
3953 | if (ve != NULL) | |
3954 | { | |
3955 | Named_object* no = ve->named_object(); | |
3956 | return no->is_variable() && no->var_value()->is_global(); | |
3957 | } | |
3958 | return false; | |
3959 | } | |
3960 | else | |
3961 | return this->expr_->is_constant(); | |
3962 | } | |
3963 | ||
3ae06f68 | 3964 | // Return whether a unary expression can be used as a constant |
3965 | // initializer. | |
3966 | ||
3967 | bool | |
3968 | Unary_expression::do_is_static_initializer() const | |
3969 | { | |
3970 | if (this->op_ == OPERATOR_MULT) | |
3971 | return false; | |
3972 | else if (this->op_ == OPERATOR_AND) | |
de048538 | 3973 | return Unary_expression::base_is_static_initializer(this->expr_); |
3974 | else | |
3975 | return this->expr_->is_static_initializer(); | |
3976 | } | |
3ae06f68 | 3977 | |
de048538 | 3978 | // Return whether the address of EXPR can be used as a static |
3979 | // initializer. | |
3ae06f68 | 3980 | |
de048538 | 3981 | bool |
3982 | Unary_expression::base_is_static_initializer(Expression* expr) | |
3983 | { | |
3984 | // The address of a field reference can be a static initializer if | |
3985 | // the base can be a static initializer. | |
3986 | Field_reference_expression* fre = expr->field_reference_expression(); | |
3987 | if (fre != NULL) | |
3988 | return Unary_expression::base_is_static_initializer(fre->expr()); | |
3989 | ||
3990 | // The address of an index expression can be a static initializer if | |
3991 | // the base can be a static initializer and the index is constant. | |
3992 | Array_index_expression* aind = expr->array_index_expression(); | |
3993 | if (aind != NULL) | |
3994 | return (aind->end() == NULL | |
3995 | && aind->start()->is_constant() | |
3996 | && Unary_expression::base_is_static_initializer(aind->array())); | |
3997 | ||
3998 | // The address of a global variable can be a static initializer. | |
3999 | Var_expression* ve = expr->var_expression(); | |
4000 | if (ve != NULL) | |
4001 | { | |
4002 | Named_object* no = ve->named_object(); | |
4003 | return no->is_variable() && no->var_value()->is_global(); | |
4004 | } | |
4005 | ||
4006 | // The address of a composite literal can be used as a static | |
4007 | // initializer if the composite literal is itself usable as a | |
4008 | // static initializer. | |
4009 | if (expr->is_composite_literal() && expr->is_static_initializer()) | |
4010 | return true; | |
3ae06f68 | 4011 | |
de048538 | 4012 | // The address of a string constant can be used as a static |
4013 | // initializer. This can not be written in Go itself but this is | |
4014 | // used when building a type descriptor. | |
4015 | if (expr->string_expression() != NULL) | |
4016 | return true; | |
4017 | ||
4018 | return false; | |
3ae06f68 | 4019 | } |
4020 | ||
f614ea8b | 4021 | // Return whether this dereference expression requires an explicit nil |
4022 | // check. If we are dereferencing the pointer to a large struct | |
4023 | // (greater than the specified size threshold), we need to check for | |
4024 | // nil. We don't bother to check for small structs because we expect | |
4025 | // the system to crash on a nil pointer dereference. However, if we | |
4026 | // know the address of this expression is being taken, we must always | |
4027 | // check for nil. | |
4028 | Unary_expression::Nil_check_classification | |
4029 | Unary_expression::requires_nil_check(Gogo* gogo) | |
4030 | { | |
4031 | go_assert(this->op_ == OPERATOR_MULT); | |
4032 | go_assert(this->expr_->type()->points_to() != NULL); | |
4033 | ||
4034 | if (this->issue_nil_check_ == NIL_CHECK_NEEDED) | |
4035 | return NIL_CHECK_NEEDED; | |
4036 | else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED) | |
4037 | return NIL_CHECK_NOT_NEEDED; | |
4038 | ||
4039 | Type* ptype = this->expr_->type()->points_to(); | |
4040 | int64_t type_size = -1; | |
4041 | if (!ptype->is_void_type()) | |
4042 | { | |
4043 | bool ok = ptype->backend_type_size(gogo, &type_size); | |
4044 | if (!ok) | |
4045 | return NIL_CHECK_ERROR_ENCOUNTERED; | |
4046 | } | |
4047 | ||
4048 | int64_t size_cutoff = gogo->nil_check_size_threshold(); | |
4049 | if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff)) | |
4050 | this->issue_nil_check_ = NIL_CHECK_NEEDED; | |
4051 | else | |
4052 | this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED; | |
4053 | return this->issue_nil_check_; | |
4054 | } | |
4055 | ||
0c77715b | 4056 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
af7a5274 | 4057 | // could be done, false if not. On overflow, issues an error and sets |
4058 | // *ISSUED_ERROR. | |
e440a328 | 4059 | |
4060 | bool | |
0c77715b | 4061 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
af7a5274 | 4062 | Location location, Numeric_constant* nc, |
4063 | bool* issued_error) | |
e440a328 | 4064 | { |
af7a5274 | 4065 | *issued_error = false; |
e440a328 | 4066 | switch (op) |
4067 | { | |
4068 | case OPERATOR_PLUS: | |
0c77715b | 4069 | *nc = *unc; |
e440a328 | 4070 | return true; |
0c77715b | 4071 | |
e440a328 | 4072 | case OPERATOR_MINUS: |
0c77715b | 4073 | if (unc->is_int() || unc->is_rune()) |
4074 | break; | |
4075 | else if (unc->is_float()) | |
4076 | { | |
4077 | mpfr_t uval; | |
4078 | unc->get_float(&uval); | |
4079 | mpfr_t val; | |
4080 | mpfr_init(val); | |
4081 | mpfr_neg(val, uval, GMP_RNDN); | |
4082 | nc->set_float(unc->type(), val); | |
4083 | mpfr_clear(uval); | |
4084 | mpfr_clear(val); | |
4085 | return true; | |
4086 | } | |
4087 | else if (unc->is_complex()) | |
4088 | { | |
fcbea5e4 | 4089 | mpc_t uval; |
4090 | unc->get_complex(&uval); | |
4091 | mpc_t val; | |
4092 | mpc_init2(val, mpc_precision); | |
4093 | mpc_neg(val, uval, MPC_RNDNN); | |
4094 | nc->set_complex(unc->type(), val); | |
4095 | mpc_clear(uval); | |
4096 | mpc_clear(val); | |
0c77715b | 4097 | return true; |
4098 | } | |
e440a328 | 4099 | else |
0c77715b | 4100 | go_unreachable(); |
e440a328 | 4101 | |
0c77715b | 4102 | case OPERATOR_XOR: |
4103 | break; | |
68448d53 | 4104 | |
59a401fe | 4105 | case OPERATOR_NOT: |
e440a328 | 4106 | case OPERATOR_AND: |
4107 | case OPERATOR_MULT: | |
4108 | return false; | |
0c77715b | 4109 | |
e440a328 | 4110 | default: |
c3e6f413 | 4111 | go_unreachable(); |
e440a328 | 4112 | } |
e440a328 | 4113 | |
0c77715b | 4114 | if (!unc->is_int() && !unc->is_rune()) |
4115 | return false; | |
4116 | ||
4117 | mpz_t uval; | |
8387e1df | 4118 | if (unc->is_rune()) |
4119 | unc->get_rune(&uval); | |
4120 | else | |
4121 | unc->get_int(&uval); | |
0c77715b | 4122 | mpz_t val; |
4123 | mpz_init(val); | |
e440a328 | 4124 | |
e440a328 | 4125 | switch (op) |
4126 | { | |
e440a328 | 4127 | case OPERATOR_MINUS: |
0c77715b | 4128 | mpz_neg(val, uval); |
4129 | break; | |
4130 | ||
e440a328 | 4131 | case OPERATOR_NOT: |
0c77715b | 4132 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
4133 | break; | |
4134 | ||
e440a328 | 4135 | case OPERATOR_XOR: |
0c77715b | 4136 | { |
4137 | Type* utype = unc->type(); | |
4138 | if (utype->integer_type() == NULL | |
4139 | || utype->integer_type()->is_abstract()) | |
4140 | mpz_com(val, uval); | |
4141 | else | |
4142 | { | |
4143 | // The number of HOST_WIDE_INTs that it takes to represent | |
4144 | // UVAL. | |
4145 | size_t count = ((mpz_sizeinbase(uval, 2) | |
4146 | + HOST_BITS_PER_WIDE_INT | |
4147 | - 1) | |
4148 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 4149 | |
0c77715b | 4150 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
4151 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
4152 | ||
4153 | size_t obits = utype->integer_type()->bits(); | |
4154 | ||
4155 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
4156 | { | |
4157 | mpz_t adj; | |
4158 | mpz_init_set_ui(adj, 1); | |
4159 | mpz_mul_2exp(adj, adj, obits); | |
4160 | mpz_add(uval, uval, adj); | |
4161 | mpz_clear(adj); | |
4162 | } | |
4163 | ||
4164 | size_t ecount; | |
4165 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
4166 | go_assert(ecount <= count); | |
4167 | ||
4168 | // Trim down to the number of words required by the type. | |
4169 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
4170 | / HOST_BITS_PER_WIDE_INT); | |
4171 | go_assert(ocount <= count); | |
4172 | ||
4173 | for (size_t i = 0; i < ocount; ++i) | |
4174 | phwi[i] = ~phwi[i]; | |
4175 | ||
4176 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
4177 | if (clearbits != 0) | |
4178 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
4179 | >> clearbits); | |
4180 | ||
4181 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
4182 | ||
4183 | if (!utype->integer_type()->is_unsigned() | |
4184 | && mpz_tstbit(val, obits - 1)) | |
4185 | { | |
4186 | mpz_t adj; | |
4187 | mpz_init_set_ui(adj, 1); | |
4188 | mpz_mul_2exp(adj, adj, obits); | |
4189 | mpz_sub(val, val, adj); | |
4190 | mpz_clear(adj); | |
4191 | } | |
4192 | ||
4193 | delete[] phwi; | |
4194 | } | |
4195 | } | |
4196 | break; | |
e440a328 | 4197 | |
e440a328 | 4198 | default: |
c3e6f413 | 4199 | go_unreachable(); |
e440a328 | 4200 | } |
e440a328 | 4201 | |
0c77715b | 4202 | if (unc->is_rune()) |
4203 | nc->set_rune(NULL, val); | |
e440a328 | 4204 | else |
0c77715b | 4205 | nc->set_int(NULL, val); |
e440a328 | 4206 | |
0c77715b | 4207 | mpz_clear(uval); |
4208 | mpz_clear(val); | |
e440a328 | 4209 | |
af7a5274 | 4210 | if (!nc->set_type(unc->type(), true, location)) |
4211 | { | |
4212 | *issued_error = true; | |
4213 | return false; | |
4214 | } | |
4215 | return true; | |
e440a328 | 4216 | } |
4217 | ||
0c77715b | 4218 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 4219 | |
4220 | bool | |
0c77715b | 4221 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 4222 | { |
0c77715b | 4223 | Numeric_constant unc; |
4224 | if (!this->expr_->numeric_constant_value(&unc)) | |
4225 | return false; | |
af7a5274 | 4226 | bool issued_error; |
0c77715b | 4227 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), |
af7a5274 | 4228 | nc, &issued_error); |
e440a328 | 4229 | } |
4230 | ||
4231 | // Return the type of a unary expression. | |
4232 | ||
4233 | Type* | |
4234 | Unary_expression::do_type() | |
4235 | { | |
4236 | switch (this->op_) | |
4237 | { | |
4238 | case OPERATOR_PLUS: | |
4239 | case OPERATOR_MINUS: | |
4240 | case OPERATOR_NOT: | |
4241 | case OPERATOR_XOR: | |
4242 | return this->expr_->type(); | |
4243 | ||
4244 | case OPERATOR_AND: | |
4245 | return Type::make_pointer_type(this->expr_->type()); | |
4246 | ||
4247 | case OPERATOR_MULT: | |
4248 | { | |
4249 | Type* subtype = this->expr_->type(); | |
4250 | Type* points_to = subtype->points_to(); | |
4251 | if (points_to == NULL) | |
4252 | return Type::make_error_type(); | |
4253 | return points_to; | |
4254 | } | |
4255 | ||
4256 | default: | |
c3e6f413 | 4257 | go_unreachable(); |
e440a328 | 4258 | } |
4259 | } | |
4260 | ||
4261 | // Determine abstract types for a unary expression. | |
4262 | ||
4263 | void | |
4264 | Unary_expression::do_determine_type(const Type_context* context) | |
4265 | { | |
4266 | switch (this->op_) | |
4267 | { | |
4268 | case OPERATOR_PLUS: | |
4269 | case OPERATOR_MINUS: | |
4270 | case OPERATOR_NOT: | |
4271 | case OPERATOR_XOR: | |
4272 | this->expr_->determine_type(context); | |
4273 | break; | |
4274 | ||
4275 | case OPERATOR_AND: | |
4276 | // Taking the address of something. | |
4277 | { | |
4278 | Type* subtype = (context->type == NULL | |
4279 | ? NULL | |
4280 | : context->type->points_to()); | |
4281 | Type_context subcontext(subtype, false); | |
4282 | this->expr_->determine_type(&subcontext); | |
4283 | } | |
4284 | break; | |
4285 | ||
4286 | case OPERATOR_MULT: | |
4287 | // Indirecting through a pointer. | |
4288 | { | |
4289 | Type* subtype = (context->type == NULL | |
4290 | ? NULL | |
4291 | : Type::make_pointer_type(context->type)); | |
4292 | Type_context subcontext(subtype, false); | |
4293 | this->expr_->determine_type(&subcontext); | |
4294 | } | |
4295 | break; | |
4296 | ||
4297 | default: | |
c3e6f413 | 4298 | go_unreachable(); |
e440a328 | 4299 | } |
4300 | } | |
4301 | ||
4302 | // Check types for a unary expression. | |
4303 | ||
4304 | void | |
4305 | Unary_expression::do_check_types(Gogo*) | |
4306 | { | |
9fe897ef | 4307 | Type* type = this->expr_->type(); |
5c13bd80 | 4308 | if (type->is_error()) |
9fe897ef | 4309 | { |
4310 | this->set_is_error(); | |
4311 | return; | |
4312 | } | |
4313 | ||
e440a328 | 4314 | switch (this->op_) |
4315 | { | |
4316 | case OPERATOR_PLUS: | |
4317 | case OPERATOR_MINUS: | |
9fe897ef | 4318 | if (type->integer_type() == NULL |
4319 | && type->float_type() == NULL | |
4320 | && type->complex_type() == NULL) | |
4321 | this->report_error(_("expected numeric type")); | |
e440a328 | 4322 | break; |
4323 | ||
4324 | case OPERATOR_NOT: | |
59a401fe | 4325 | if (!type->is_boolean_type()) |
4326 | this->report_error(_("expected boolean type")); | |
4327 | break; | |
4328 | ||
e440a328 | 4329 | case OPERATOR_XOR: |
b3b1474e | 4330 | if (type->integer_type() == NULL) |
4331 | this->report_error(_("expected integer")); | |
e440a328 | 4332 | break; |
4333 | ||
4334 | case OPERATOR_AND: | |
4335 | if (!this->expr_->is_addressable()) | |
09ea332d | 4336 | { |
4337 | if (!this->create_temp_) | |
f4dea966 | 4338 | { |
631d5788 | 4339 | go_error_at(this->location(), "invalid operand for unary %<&%>"); |
f4dea966 | 4340 | this->set_is_error(); |
4341 | } | |
09ea332d | 4342 | } |
e440a328 | 4343 | else |
da244e59 | 4344 | this->expr_->issue_nil_check(); |
e440a328 | 4345 | break; |
4346 | ||
4347 | case OPERATOR_MULT: | |
4348 | // Indirecting through a pointer. | |
9fe897ef | 4349 | if (type->points_to() == NULL) |
4350 | this->report_error(_("expected pointer")); | |
7661d702 | 4351 | if (type->points_to()->is_error()) |
4352 | this->set_is_error(); | |
e440a328 | 4353 | break; |
4354 | ||
4355 | default: | |
c3e6f413 | 4356 | go_unreachable(); |
e440a328 | 4357 | } |
4358 | } | |
4359 | ||
ea664253 | 4360 | // Get the backend representation for a unary expression. |
e440a328 | 4361 | |
ea664253 | 4362 | Bexpression* |
4363 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4364 | { |
1b1f2abf | 4365 | Gogo* gogo = context->gogo(); |
e9d3367e | 4366 | Location loc = this->location(); |
4367 | ||
4368 | // Taking the address of a set-and-use-temporary expression requires | |
4369 | // setting the temporary and then taking the address. | |
4370 | if (this->op_ == OPERATOR_AND) | |
4371 | { | |
4372 | Set_and_use_temporary_expression* sut = | |
4373 | this->expr_->set_and_use_temporary_expression(); | |
4374 | if (sut != NULL) | |
4375 | { | |
4376 | Temporary_statement* temp = sut->temporary(); | |
4377 | Bvariable* bvar = temp->get_backend_variable(context); | |
d4e6573e | 4378 | Bexpression* bvar_expr = |
7af8e400 | 4379 | gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4380 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4381 | |
0ab48656 | 4382 | Named_object* fn = context->function(); |
4383 | go_assert(fn != NULL); | |
4384 | Bfunction* bfn = | |
4385 | fn->func_value()->get_or_make_decl(gogo, fn); | |
f9ca30f9 | 4386 | Bstatement* bassign = |
0ab48656 | 4387 | gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc); |
f9ca30f9 | 4388 | Bexpression* bvar_addr = |
4389 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4390 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4391 | } |
4392 | } | |
4393 | ||
f9ca30f9 | 4394 | Bexpression* ret; |
ea664253 | 4395 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4396 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4397 | switch (this->op_) |
4398 | { | |
4399 | case OPERATOR_PLUS: | |
f9ca30f9 | 4400 | ret = bexpr; |
4401 | break; | |
e440a328 | 4402 | |
4403 | case OPERATOR_MINUS: | |
f9ca30f9 | 4404 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4405 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4406 | break; | |
e440a328 | 4407 | |
4408 | case OPERATOR_NOT: | |
e440a328 | 4409 | case OPERATOR_XOR: |
f9ca30f9 | 4410 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4411 | break; | |
e440a328 | 4412 | |
4413 | case OPERATOR_AND: | |
09ea332d | 4414 | if (!this->create_temp_) |
4415 | { | |
4416 | // We should not see a non-constant constructor here; cases | |
4417 | // where we would see one should have been moved onto the | |
4418 | // heap at parse time. Taking the address of a nonconstant | |
4419 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4420 | |
4421 | go_assert(!this->expr_->is_composite_literal() | |
3ae06f68 | 4422 | || this->expr_->is_static_initializer()); |
24060bf9 | 4423 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4424 | { | |
4425 | Unary_expression* ue = | |
4426 | static_cast<Unary_expression*>(this->expr_); | |
4427 | go_assert(ue->op() != OPERATOR_AND); | |
4428 | } | |
09ea332d | 4429 | } |
e440a328 | 4430 | |
f23d7786 | 4431 | if (this->is_gc_root_ || this->is_slice_init_) |
76f85fd6 | 4432 | { |
19272321 | 4433 | std::string var_name; |
f23d7786 | 4434 | bool copy_to_heap = false; |
4435 | if (this->is_gc_root_) | |
4436 | { | |
4437 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4438 | // they cannot be represented as an immutable_struct in the | |
4439 | // backend. | |
19272321 | 4440 | var_name = gogo->gc_root_name(); |
f23d7786 | 4441 | } |
4442 | else | |
4443 | { | |
4444 | // Build a decl for a slice value initializer. An immutable slice | |
4445 | // value initializer may have to be copied to the heap if it | |
4446 | // contains pointers in a non-constant context. | |
19272321 | 4447 | var_name = gogo->initializer_name(); |
f23d7786 | 4448 | |
4449 | Array_type* at = this->expr_->type()->array_type(); | |
4450 | go_assert(at != NULL); | |
4451 | ||
4452 | // If we are not copying the value to the heap, we will only | |
4453 | // initialize the value once, so we can use this directly | |
4454 | // rather than copying it. In that case we can't make it | |
4455 | // read-only, because the program is permitted to change it. | |
3ae06f68 | 4456 | copy_to_heap = context->function() != NULL; |
f23d7786 | 4457 | } |
19272321 | 4458 | std::string asm_name(go_selectively_encode_id(var_name)); |
f23d7786 | 4459 | Bvariable* implicit = |
19272321 | 4460 | gogo->backend()->implicit_variable(var_name, asm_name, |
438b4bec | 4461 | btype, true, copy_to_heap, |
4462 | false, 0); | |
19272321 | 4463 | gogo->backend()->implicit_variable_set_init(implicit, var_name, btype, |
aa5ae575 | 4464 | true, copy_to_heap, false, |
4465 | bexpr); | |
7af8e400 | 4466 | bexpr = gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4467 | |
4468 | // If we are not copying a slice initializer to the heap, | |
4469 | // then it can be changed by the program, so if it can | |
4470 | // contain pointers we must register it as a GC root. | |
4471 | if (this->is_slice_init_ | |
4472 | && !copy_to_heap | |
4473 | && this->expr_->type()->has_pointer()) | |
4474 | { | |
4475 | Bexpression* root = | |
7af8e400 | 4476 | gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4477 | root = gogo->backend()->address_expression(root, loc); |
4478 | Type* type = Type::make_pointer_type(this->expr_->type()); | |
4479 | gogo->add_gc_root(Expression::make_backend(root, type, loc)); | |
4480 | } | |
76f85fd6 | 4481 | } |
4482 | else if ((this->expr_->is_composite_literal() | |
3ae06f68 | 4483 | || this->expr_->string_expression() != NULL) |
4484 | && this->expr_->is_static_initializer()) | |
f9ca30f9 | 4485 | { |
19272321 | 4486 | std::string var_name(gogo->initializer_name()); |
4487 | std::string asm_name(go_selectively_encode_id(var_name)); | |
f9ca30f9 | 4488 | Bvariable* decl = |
19272321 | 4489 | gogo->backend()->immutable_struct(var_name, asm_name, |
438b4bec | 4490 | true, false, btype, loc); |
19272321 | 4491 | gogo->backend()->immutable_struct_set_init(decl, var_name, true, |
4492 | false, btype, loc, bexpr); | |
7af8e400 | 4493 | bexpr = gogo->backend()->var_expression(decl, loc); |
f9ca30f9 | 4494 | } |
09ea332d | 4495 | |
f9ca30f9 | 4496 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4497 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4498 | break; | |
e440a328 | 4499 | |
4500 | case OPERATOR_MULT: | |
4501 | { | |
f9ca30f9 | 4502 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4503 | |
f614ea8b | 4504 | bool known_valid = false; |
f9ca30f9 | 4505 | Type* ptype = this->expr_->type()->points_to(); |
4506 | Btype* pbtype = ptype->get_backend(gogo); | |
f614ea8b | 4507 | switch (this->requires_nil_check(gogo)) |
4508 | { | |
4509 | case NIL_CHECK_NOT_NEEDED: | |
4510 | break; | |
4511 | case NIL_CHECK_ERROR_ENCOUNTERED: | |
2a305b85 | 4512 | { |
4513 | go_assert(saw_errors()); | |
4514 | return gogo->backend()->error_expression(); | |
4515 | } | |
f614ea8b | 4516 | case NIL_CHECK_NEEDED: |
4517 | { | |
f9ca30f9 | 4518 | go_assert(this->expr_->is_variable()); |
2dd89704 | 4519 | |
4520 | // If we're nil-checking the result of a set-and-use-temporary | |
4521 | // expression, then pick out the target temp and use that | |
4522 | // for the final result of the conditional. | |
4523 | Bexpression* tbexpr = bexpr; | |
4524 | Bexpression* ubexpr = bexpr; | |
4525 | Set_and_use_temporary_expression* sut = | |
4526 | this->expr_->set_and_use_temporary_expression(); | |
4527 | if (sut != NULL) { | |
4528 | Temporary_statement* temp = sut->temporary(); | |
4529 | Bvariable* bvar = temp->get_backend_variable(context); | |
4530 | ubexpr = gogo->backend()->var_expression(bvar, loc); | |
4531 | } | |
ea664253 | 4532 | Bexpression* nil = |
f614ea8b | 4533 | Expression::make_nil(loc)->get_backend(context); |
f9ca30f9 | 4534 | Bexpression* compare = |
2dd89704 | 4535 | gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr, |
f9ca30f9 | 4536 | nil, loc); |
f9ca30f9 | 4537 | Bexpression* crash = |
f614ea8b | 4538 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4539 | loc)->get_backend(context); | |
93715b75 | 4540 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
4541 | bexpr = gogo->backend()->conditional_expression(bfn, btype, | |
4542 | compare, | |
2dd89704 | 4543 | crash, ubexpr, |
f9ca30f9 | 4544 | loc); |
f614ea8b | 4545 | known_valid = true; |
4546 | break; | |
4547 | } | |
4548 | case NIL_CHECK_DEFAULT: | |
4549 | go_unreachable(); | |
4550 | } | |
4551 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, | |
4552 | known_valid, loc); | |
e440a328 | 4553 | } |
f9ca30f9 | 4554 | break; |
e440a328 | 4555 | |
4556 | default: | |
c3e6f413 | 4557 | go_unreachable(); |
e440a328 | 4558 | } |
f9ca30f9 | 4559 | |
ea664253 | 4560 | return ret; |
e440a328 | 4561 | } |
4562 | ||
4563 | // Export a unary expression. | |
4564 | ||
4565 | void | |
4566 | Unary_expression::do_export(Export* exp) const | |
4567 | { | |
4568 | switch (this->op_) | |
4569 | { | |
4570 | case OPERATOR_PLUS: | |
4571 | exp->write_c_string("+ "); | |
4572 | break; | |
4573 | case OPERATOR_MINUS: | |
4574 | exp->write_c_string("- "); | |
4575 | break; | |
4576 | case OPERATOR_NOT: | |
4577 | exp->write_c_string("! "); | |
4578 | break; | |
4579 | case OPERATOR_XOR: | |
4580 | exp->write_c_string("^ "); | |
4581 | break; | |
4582 | case OPERATOR_AND: | |
4583 | case OPERATOR_MULT: | |
4584 | default: | |
c3e6f413 | 4585 | go_unreachable(); |
e440a328 | 4586 | } |
4587 | this->expr_->export_expression(exp); | |
4588 | } | |
4589 | ||
4590 | // Import a unary expression. | |
4591 | ||
4592 | Expression* | |
4593 | Unary_expression::do_import(Import* imp) | |
4594 | { | |
4595 | Operator op; | |
4596 | switch (imp->get_char()) | |
4597 | { | |
4598 | case '+': | |
4599 | op = OPERATOR_PLUS; | |
4600 | break; | |
4601 | case '-': | |
4602 | op = OPERATOR_MINUS; | |
4603 | break; | |
4604 | case '!': | |
4605 | op = OPERATOR_NOT; | |
4606 | break; | |
4607 | case '^': | |
4608 | op = OPERATOR_XOR; | |
4609 | break; | |
4610 | default: | |
c3e6f413 | 4611 | go_unreachable(); |
e440a328 | 4612 | } |
4613 | imp->require_c_string(" "); | |
4614 | Expression* expr = Expression::import_expression(imp); | |
4615 | return Expression::make_unary(op, expr, imp->location()); | |
4616 | } | |
4617 | ||
d751bb78 | 4618 | // Dump ast representation of an unary expression. |
4619 | ||
4620 | void | |
4621 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4622 | { | |
4623 | ast_dump_context->dump_operator(this->op_); | |
4624 | ast_dump_context->ostream() << "("; | |
4625 | ast_dump_context->dump_expression(this->expr_); | |
4626 | ast_dump_context->ostream() << ") "; | |
4627 | } | |
4628 | ||
e440a328 | 4629 | // Make a unary expression. |
4630 | ||
4631 | Expression* | |
b13c66cd | 4632 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4633 | { |
4634 | return new Unary_expression(op, expr, location); | |
4635 | } | |
4636 | ||
f614ea8b | 4637 | Expression* |
4638 | Expression::make_dereference(Expression* ptr, | |
4639 | Nil_check_classification docheck, | |
4640 | Location location) | |
4641 | { | |
4642 | Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location); | |
4643 | if (docheck == NIL_CHECK_NEEDED) | |
4644 | deref->unary_expression()->set_requires_nil_check(true); | |
4645 | else if (docheck == NIL_CHECK_NOT_NEEDED) | |
4646 | deref->unary_expression()->set_requires_nil_check(false); | |
4647 | return deref; | |
4648 | } | |
4649 | ||
e440a328 | 4650 | // If this is an indirection through a pointer, return the expression |
4651 | // being pointed through. Otherwise return this. | |
4652 | ||
4653 | Expression* | |
4654 | Expression::deref() | |
4655 | { | |
4656 | if (this->classification_ == EXPRESSION_UNARY) | |
4657 | { | |
4658 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4659 | if (ue->op() == OPERATOR_MULT) | |
4660 | return ue->operand(); | |
4661 | } | |
4662 | return this; | |
4663 | } | |
4664 | ||
4665 | // Class Binary_expression. | |
4666 | ||
4667 | // Traversal. | |
4668 | ||
4669 | int | |
4670 | Binary_expression::do_traverse(Traverse* traverse) | |
4671 | { | |
4672 | int t = Expression::traverse(&this->left_, traverse); | |
4673 | if (t == TRAVERSE_EXIT) | |
4674 | return TRAVERSE_EXIT; | |
4675 | return Expression::traverse(&this->right_, traverse); | |
4676 | } | |
4677 | ||
3ae06f68 | 4678 | // Return whether this expression may be used as a static initializer. |
4679 | ||
4680 | bool | |
4681 | Binary_expression::do_is_static_initializer() const | |
4682 | { | |
4683 | if (!this->left_->is_static_initializer() | |
4684 | || !this->right_->is_static_initializer()) | |
4685 | return false; | |
4686 | ||
4687 | // Addresses can be static initializers, but we can't implement | |
4688 | // arbitray binary expressions of them. | |
4689 | Unary_expression* lu = this->left_->unary_expression(); | |
4690 | Unary_expression* ru = this->right_->unary_expression(); | |
4691 | if (lu != NULL && lu->op() == OPERATOR_AND) | |
4692 | { | |
4693 | if (ru != NULL && ru->op() == OPERATOR_AND) | |
4694 | return this->op_ == OPERATOR_MINUS; | |
4695 | else | |
4696 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4697 | } | |
4698 | else if (ru != NULL && ru->op() == OPERATOR_AND) | |
4699 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4700 | ||
4701 | // Other cases should resolve in the backend. | |
4702 | return true; | |
4703 | } | |
4704 | ||
0c77715b | 4705 | // Return the type to use for a binary operation on operands of |
4706 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4707 | // such may be NULL or abstract. | |
4708 | ||
4709 | bool | |
4710 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4711 | Type* right_type, Type** result_type) | |
4712 | { | |
4713 | if (left_type != right_type | |
4714 | && !left_type->is_abstract() | |
4715 | && !right_type->is_abstract() | |
4716 | && left_type->base() != right_type->base() | |
4717 | && op != OPERATOR_LSHIFT | |
4718 | && op != OPERATOR_RSHIFT) | |
4719 | { | |
4720 | // May be a type error--let it be diagnosed elsewhere. | |
4721 | return false; | |
4722 | } | |
4723 | ||
4724 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4725 | { | |
4726 | if (left_type->integer_type() != NULL) | |
4727 | *result_type = left_type; | |
4728 | else | |
4729 | *result_type = Type::make_abstract_integer_type(); | |
4730 | } | |
4731 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4732 | *result_type = left_type; | |
4733 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4734 | *result_type = right_type; | |
4735 | else if (!left_type->is_abstract()) | |
4736 | *result_type = left_type; | |
4737 | else if (!right_type->is_abstract()) | |
4738 | *result_type = right_type; | |
4739 | else if (left_type->complex_type() != NULL) | |
4740 | *result_type = left_type; | |
4741 | else if (right_type->complex_type() != NULL) | |
4742 | *result_type = right_type; | |
4743 | else if (left_type->float_type() != NULL) | |
4744 | *result_type = left_type; | |
4745 | else if (right_type->float_type() != NULL) | |
4746 | *result_type = right_type; | |
4747 | else if (left_type->integer_type() != NULL | |
4748 | && left_type->integer_type()->is_rune()) | |
4749 | *result_type = left_type; | |
4750 | else if (right_type->integer_type() != NULL | |
4751 | && right_type->integer_type()->is_rune()) | |
4752 | *result_type = right_type; | |
4753 | else | |
4754 | *result_type = left_type; | |
4755 | ||
4756 | return true; | |
4757 | } | |
4758 | ||
4759 | // Convert an integer comparison code and an operator to a boolean | |
4760 | // value. | |
e440a328 | 4761 | |
4762 | bool | |
0c77715b | 4763 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4764 | { |
e440a328 | 4765 | switch (op) |
4766 | { | |
4767 | case OPERATOR_EQEQ: | |
0c77715b | 4768 | return cmp == 0; |
4769 | break; | |
e440a328 | 4770 | case OPERATOR_NOTEQ: |
0c77715b | 4771 | return cmp != 0; |
4772 | break; | |
e440a328 | 4773 | case OPERATOR_LT: |
0c77715b | 4774 | return cmp < 0; |
4775 | break; | |
e440a328 | 4776 | case OPERATOR_LE: |
0c77715b | 4777 | return cmp <= 0; |
e440a328 | 4778 | case OPERATOR_GT: |
0c77715b | 4779 | return cmp > 0; |
e440a328 | 4780 | case OPERATOR_GE: |
0c77715b | 4781 | return cmp >= 0; |
e440a328 | 4782 | default: |
c3e6f413 | 4783 | go_unreachable(); |
e440a328 | 4784 | } |
4785 | } | |
4786 | ||
0c77715b | 4787 | // Compare constants according to OP. |
e440a328 | 4788 | |
4789 | bool | |
0c77715b | 4790 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4791 | Numeric_constant* right_nc, | |
4792 | Location location, bool* result) | |
e440a328 | 4793 | { |
0c77715b | 4794 | Type* left_type = left_nc->type(); |
4795 | Type* right_type = right_nc->type(); | |
4796 | ||
4797 | Type* type; | |
4798 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4799 | return false; | |
4800 | ||
4801 | // When comparing an untyped operand to a typed operand, we are | |
4802 | // effectively coercing the untyped operand to the other operand's | |
4803 | // type, so make sure that is valid. | |
4804 | if (!left_nc->set_type(type, true, location) | |
4805 | || !right_nc->set_type(type, true, location)) | |
4806 | return false; | |
4807 | ||
4808 | bool ret; | |
4809 | int cmp; | |
4810 | if (type->complex_type() != NULL) | |
4811 | { | |
4812 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4813 | return false; | |
4814 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4815 | } | |
4816 | else if (type->float_type() != NULL) | |
4817 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4818 | else |
0c77715b | 4819 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4820 | ||
4821 | if (ret) | |
4822 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4823 | ||
4824 | return ret; | |
4825 | } | |
4826 | ||
4827 | // Compare integer constants. | |
4828 | ||
4829 | bool | |
4830 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4831 | const Numeric_constant* right_nc, | |
4832 | int* cmp) | |
4833 | { | |
4834 | mpz_t left_val; | |
4835 | if (!left_nc->to_int(&left_val)) | |
4836 | return false; | |
4837 | mpz_t right_val; | |
4838 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4839 | { |
0c77715b | 4840 | mpz_clear(left_val); |
4841 | return false; | |
e440a328 | 4842 | } |
0c77715b | 4843 | |
4844 | *cmp = mpz_cmp(left_val, right_val); | |
4845 | ||
4846 | mpz_clear(left_val); | |
4847 | mpz_clear(right_val); | |
4848 | ||
4849 | return true; | |
4850 | } | |
4851 | ||
4852 | // Compare floating point constants. | |
4853 | ||
4854 | bool | |
4855 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4856 | const Numeric_constant* right_nc, | |
4857 | int* cmp) | |
4858 | { | |
4859 | mpfr_t left_val; | |
4860 | if (!left_nc->to_float(&left_val)) | |
4861 | return false; | |
4862 | mpfr_t right_val; | |
4863 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4864 | { |
0c77715b | 4865 | mpfr_clear(left_val); |
4866 | return false; | |
4867 | } | |
4868 | ||
4869 | // We already coerced both operands to the same type. If that type | |
4870 | // is not an abstract type, we need to round the values accordingly. | |
4871 | Type* type = left_nc->type(); | |
4872 | if (!type->is_abstract() && type->float_type() != NULL) | |
4873 | { | |
4874 | int bits = type->float_type()->bits(); | |
4875 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4876 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4877 | } |
0c77715b | 4878 | |
4879 | *cmp = mpfr_cmp(left_val, right_val); | |
4880 | ||
4881 | mpfr_clear(left_val); | |
4882 | mpfr_clear(right_val); | |
4883 | ||
4884 | return true; | |
e440a328 | 4885 | } |
4886 | ||
0c77715b | 4887 | // Compare complex constants. Complex numbers may only be compared |
4888 | // for equality. | |
e440a328 | 4889 | |
4890 | bool | |
0c77715b | 4891 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4892 | const Numeric_constant* right_nc, | |
4893 | int* cmp) | |
e440a328 | 4894 | { |
fcbea5e4 | 4895 | mpc_t left_val; |
4896 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4897 | return false; |
fcbea5e4 | 4898 | mpc_t right_val; |
4899 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4900 | { |
fcbea5e4 | 4901 | mpc_clear(left_val); |
0c77715b | 4902 | return false; |
e440a328 | 4903 | } |
0c77715b | 4904 | |
4905 | // We already coerced both operands to the same type. If that type | |
4906 | // is not an abstract type, we need to round the values accordingly. | |
4907 | Type* type = left_nc->type(); | |
4908 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4909 | { |
0c77715b | 4910 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4911 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4912 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4913 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4914 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4915 | } |
0c77715b | 4916 | |
fcbea5e4 | 4917 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4918 | |
fcbea5e4 | 4919 | mpc_clear(left_val); |
4920 | mpc_clear(right_val); | |
0c77715b | 4921 | |
4922 | return true; | |
e440a328 | 4923 | } |
4924 | ||
0c77715b | 4925 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4926 | // true if this could be done, false if not. Issue errors at LOCATION | |
af7a5274 | 4927 | // as appropriate, and sets *ISSUED_ERROR if it did. |
e440a328 | 4928 | |
4929 | bool | |
0c77715b | 4930 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4931 | Numeric_constant* right_nc, | |
af7a5274 | 4932 | Location location, Numeric_constant* nc, |
4933 | bool* issued_error) | |
e440a328 | 4934 | { |
af7a5274 | 4935 | *issued_error = false; |
e440a328 | 4936 | switch (op) |
4937 | { | |
4938 | case OPERATOR_OROR: | |
4939 | case OPERATOR_ANDAND: | |
4940 | case OPERATOR_EQEQ: | |
4941 | case OPERATOR_NOTEQ: | |
4942 | case OPERATOR_LT: | |
4943 | case OPERATOR_LE: | |
4944 | case OPERATOR_GT: | |
4945 | case OPERATOR_GE: | |
9767e2d3 | 4946 | // These return boolean values, not numeric. |
4947 | return false; | |
0c77715b | 4948 | default: |
4949 | break; | |
4950 | } | |
4951 | ||
4952 | Type* left_type = left_nc->type(); | |
4953 | Type* right_type = right_nc->type(); | |
4954 | ||
4955 | Type* type; | |
4956 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4957 | return false; | |
4958 | ||
4959 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4960 | ||
4961 | // When combining an untyped operand with a typed operand, we are | |
4962 | // effectively coercing the untyped operand to the other operand's | |
4963 | // type, so make sure that is valid. | |
4964 | if (!left_nc->set_type(type, true, location)) | |
4965 | return false; | |
4966 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4967 | return false; | |
85334a21 | 4968 | if (is_shift |
4969 | && ((left_type->integer_type() == NULL | |
4970 | && !left_type->is_abstract()) | |
4971 | || (right_type->integer_type() == NULL | |
4972 | && !right_type->is_abstract()))) | |
4973 | return false; | |
0c77715b | 4974 | |
4975 | bool r; | |
4976 | if (type->complex_type() != NULL) | |
4977 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4978 | else if (type->float_type() != NULL) | |
4979 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4980 | else | |
4981 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4982 | ||
4983 | if (r) | |
af7a5274 | 4984 | { |
4985 | r = nc->set_type(type, true, location); | |
4986 | if (!r) | |
4987 | *issued_error = true; | |
4988 | } | |
0c77715b | 4989 | |
4990 | return r; | |
4991 | } | |
4992 | ||
4993 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4994 | // integer operations. Return true if this could be done, false if | |
4995 | // not. | |
4996 | ||
4997 | bool | |
4998 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4999 | const Numeric_constant* right_nc, | |
5000 | Location location, Numeric_constant* nc) | |
5001 | { | |
5002 | mpz_t left_val; | |
5003 | if (!left_nc->to_int(&left_val)) | |
5004 | return false; | |
5005 | mpz_t right_val; | |
5006 | if (!right_nc->to_int(&right_val)) | |
5007 | { | |
5008 | mpz_clear(left_val); | |
e440a328 | 5009 | return false; |
0c77715b | 5010 | } |
5011 | ||
5012 | mpz_t val; | |
5013 | mpz_init(val); | |
5014 | ||
5015 | switch (op) | |
5016 | { | |
e440a328 | 5017 | case OPERATOR_PLUS: |
5018 | mpz_add(val, left_val, right_val); | |
2c809f8f | 5019 | if (mpz_sizeinbase(val, 2) > 0x100000) |
5020 | { | |
631d5788 | 5021 | go_error_at(location, "constant addition overflow"); |
71a45216 | 5022 | nc->set_invalid(); |
2c809f8f | 5023 | mpz_set_ui(val, 1); |
5024 | } | |
e440a328 | 5025 | break; |
5026 | case OPERATOR_MINUS: | |
5027 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 5028 | if (mpz_sizeinbase(val, 2) > 0x100000) |
5029 | { | |
631d5788 | 5030 | go_error_at(location, "constant subtraction overflow"); |
71a45216 | 5031 | nc->set_invalid(); |
2c809f8f | 5032 | mpz_set_ui(val, 1); |
5033 | } | |
e440a328 | 5034 | break; |
5035 | case OPERATOR_OR: | |
5036 | mpz_ior(val, left_val, right_val); | |
5037 | break; | |
5038 | case OPERATOR_XOR: | |
5039 | mpz_xor(val, left_val, right_val); | |
5040 | break; | |
5041 | case OPERATOR_MULT: | |
5042 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 5043 | if (mpz_sizeinbase(val, 2) > 0x100000) |
5044 | { | |
631d5788 | 5045 | go_error_at(location, "constant multiplication overflow"); |
71a45216 | 5046 | nc->set_invalid(); |
2c809f8f | 5047 | mpz_set_ui(val, 1); |
5048 | } | |
e440a328 | 5049 | break; |
5050 | case OPERATOR_DIV: | |
5051 | if (mpz_sgn(right_val) != 0) | |
5052 | mpz_tdiv_q(val, left_val, right_val); | |
5053 | else | |
5054 | { | |
631d5788 | 5055 | go_error_at(location, "division by zero"); |
71a45216 | 5056 | nc->set_invalid(); |
e440a328 | 5057 | mpz_set_ui(val, 0); |
e440a328 | 5058 | } |
5059 | break; | |
5060 | case OPERATOR_MOD: | |
5061 | if (mpz_sgn(right_val) != 0) | |
5062 | mpz_tdiv_r(val, left_val, right_val); | |
5063 | else | |
5064 | { | |
631d5788 | 5065 | go_error_at(location, "division by zero"); |
71a45216 | 5066 | nc->set_invalid(); |
e440a328 | 5067 | mpz_set_ui(val, 0); |
e440a328 | 5068 | } |
5069 | break; | |
5070 | case OPERATOR_LSHIFT: | |
5071 | { | |
5072 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 5073 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
5074 | mpz_mul_2exp(val, left_val, shift); | |
5075 | else | |
e440a328 | 5076 | { |
631d5788 | 5077 | go_error_at(location, "shift count overflow"); |
71a45216 | 5078 | nc->set_invalid(); |
2c809f8f | 5079 | mpz_set_ui(val, 1); |
e440a328 | 5080 | } |
e440a328 | 5081 | break; |
5082 | } | |
5083 | break; | |
5084 | case OPERATOR_RSHIFT: | |
5085 | { | |
5086 | unsigned long shift = mpz_get_ui(right_val); | |
5087 | if (mpz_cmp_ui(right_val, shift) != 0) | |
5088 | { | |
631d5788 | 5089 | go_error_at(location, "shift count overflow"); |
71a45216 | 5090 | nc->set_invalid(); |
2c809f8f | 5091 | mpz_set_ui(val, 1); |
e440a328 | 5092 | } |
e440a328 | 5093 | else |
0c77715b | 5094 | { |
5095 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
5096 | mpz_tdiv_q_2exp(val, left_val, shift); | |
5097 | else | |
5098 | mpz_fdiv_q_2exp(val, left_val, shift); | |
5099 | } | |
e440a328 | 5100 | break; |
5101 | } | |
5102 | break; | |
5103 | case OPERATOR_AND: | |
5104 | mpz_and(val, left_val, right_val); | |
5105 | break; | |
5106 | case OPERATOR_BITCLEAR: | |
5107 | { | |
5108 | mpz_t tval; | |
5109 | mpz_init(tval); | |
5110 | mpz_com(tval, right_val); | |
5111 | mpz_and(val, left_val, tval); | |
5112 | mpz_clear(tval); | |
5113 | } | |
5114 | break; | |
5115 | default: | |
c3e6f413 | 5116 | go_unreachable(); |
e440a328 | 5117 | } |
5118 | ||
0c77715b | 5119 | mpz_clear(left_val); |
5120 | mpz_clear(right_val); | |
e440a328 | 5121 | |
0c77715b | 5122 | if (left_nc->is_rune() |
5123 | || (op != OPERATOR_LSHIFT | |
5124 | && op != OPERATOR_RSHIFT | |
5125 | && right_nc->is_rune())) | |
5126 | nc->set_rune(NULL, val); | |
5127 | else | |
5128 | nc->set_int(NULL, val); | |
5129 | ||
5130 | mpz_clear(val); | |
e440a328 | 5131 | |
5132 | return true; | |
5133 | } | |
5134 | ||
0c77715b | 5135 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5136 | // floating point operations. Return true if this could be done, | |
5137 | // false if not. | |
e440a328 | 5138 | |
5139 | bool | |
0c77715b | 5140 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
5141 | const Numeric_constant* right_nc, | |
5142 | Location location, Numeric_constant* nc) | |
e440a328 | 5143 | { |
0c77715b | 5144 | mpfr_t left_val; |
5145 | if (!left_nc->to_float(&left_val)) | |
5146 | return false; | |
5147 | mpfr_t right_val; | |
5148 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 5149 | { |
0c77715b | 5150 | mpfr_clear(left_val); |
e440a328 | 5151 | return false; |
0c77715b | 5152 | } |
5153 | ||
5154 | mpfr_t val; | |
5155 | mpfr_init(val); | |
5156 | ||
5157 | bool ret = true; | |
5158 | switch (op) | |
5159 | { | |
e440a328 | 5160 | case OPERATOR_PLUS: |
5161 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
5162 | break; | |
5163 | case OPERATOR_MINUS: | |
5164 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
5165 | break; | |
5166 | case OPERATOR_OR: | |
5167 | case OPERATOR_XOR: | |
5168 | case OPERATOR_AND: | |
5169 | case OPERATOR_BITCLEAR: | |
0c77715b | 5170 | case OPERATOR_MOD: |
5171 | case OPERATOR_LSHIFT: | |
5172 | case OPERATOR_RSHIFT: | |
5173 | mpfr_set_ui(val, 0, GMP_RNDN); | |
5174 | ret = false; | |
5175 | break; | |
e440a328 | 5176 | case OPERATOR_MULT: |
5177 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
5178 | break; | |
5179 | case OPERATOR_DIV: | |
0c77715b | 5180 | if (!mpfr_zero_p(right_val)) |
5181 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
5182 | else | |
5183 | { | |
631d5788 | 5184 | go_error_at(location, "division by zero"); |
71a45216 | 5185 | nc->set_invalid(); |
0c77715b | 5186 | mpfr_set_ui(val, 0, GMP_RNDN); |
5187 | } | |
e440a328 | 5188 | break; |
e440a328 | 5189 | default: |
c3e6f413 | 5190 | go_unreachable(); |
e440a328 | 5191 | } |
5192 | ||
0c77715b | 5193 | mpfr_clear(left_val); |
5194 | mpfr_clear(right_val); | |
e440a328 | 5195 | |
0c77715b | 5196 | nc->set_float(NULL, val); |
5197 | mpfr_clear(val); | |
e440a328 | 5198 | |
0c77715b | 5199 | return ret; |
e440a328 | 5200 | } |
5201 | ||
0c77715b | 5202 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5203 | // complex operations. Return true if this could be done, false if | |
5204 | // not. | |
e440a328 | 5205 | |
5206 | bool | |
0c77715b | 5207 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
5208 | const Numeric_constant* right_nc, | |
5209 | Location location, Numeric_constant* nc) | |
e440a328 | 5210 | { |
fcbea5e4 | 5211 | mpc_t left_val; |
5212 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 5213 | return false; |
fcbea5e4 | 5214 | mpc_t right_val; |
5215 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 5216 | { |
fcbea5e4 | 5217 | mpc_clear(left_val); |
e440a328 | 5218 | return false; |
0c77715b | 5219 | } |
5220 | ||
fcbea5e4 | 5221 | mpc_t val; |
5222 | mpc_init2(val, mpc_precision); | |
0c77715b | 5223 | |
5224 | bool ret = true; | |
5225 | switch (op) | |
5226 | { | |
e440a328 | 5227 | case OPERATOR_PLUS: |
fcbea5e4 | 5228 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5229 | break; |
5230 | case OPERATOR_MINUS: | |
fcbea5e4 | 5231 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5232 | break; |
5233 | case OPERATOR_OR: | |
5234 | case OPERATOR_XOR: | |
5235 | case OPERATOR_AND: | |
5236 | case OPERATOR_BITCLEAR: | |
0c77715b | 5237 | case OPERATOR_MOD: |
5238 | case OPERATOR_LSHIFT: | |
5239 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 5240 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 5241 | ret = false; |
5242 | break; | |
e440a328 | 5243 | case OPERATOR_MULT: |
fcbea5e4 | 5244 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5245 | break; |
5246 | case OPERATOR_DIV: | |
fcbea5e4 | 5247 | if (mpc_cmp_si(right_val, 0) == 0) |
5248 | { | |
631d5788 | 5249 | go_error_at(location, "division by zero"); |
71a45216 | 5250 | nc->set_invalid(); |
fcbea5e4 | 5251 | mpc_set_ui(val, 0, MPC_RNDNN); |
5252 | break; | |
5253 | } | |
5254 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 5255 | break; |
e440a328 | 5256 | default: |
c3e6f413 | 5257 | go_unreachable(); |
e440a328 | 5258 | } |
5259 | ||
fcbea5e4 | 5260 | mpc_clear(left_val); |
5261 | mpc_clear(right_val); | |
e440a328 | 5262 | |
fcbea5e4 | 5263 | nc->set_complex(NULL, val); |
5264 | mpc_clear(val); | |
e440a328 | 5265 | |
0c77715b | 5266 | return ret; |
e440a328 | 5267 | } |
5268 | ||
5269 | // Lower a binary expression. We have to evaluate constant | |
5270 | // expressions now, in order to implement Go's unlimited precision | |
5271 | // constants. | |
5272 | ||
5273 | Expression* | |
e9d3367e | 5274 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5275 | Statement_inserter* inserter, int) | |
e440a328 | 5276 | { |
b13c66cd | 5277 | Location location = this->location(); |
e440a328 | 5278 | Operator op = this->op_; |
5279 | Expression* left = this->left_; | |
5280 | Expression* right = this->right_; | |
5281 | ||
5282 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5283 | || op == OPERATOR_NOTEQ | |
5284 | || op == OPERATOR_LT | |
5285 | || op == OPERATOR_LE | |
5286 | || op == OPERATOR_GT | |
5287 | || op == OPERATOR_GE); | |
5288 | ||
0c77715b | 5289 | // Numeric constant expressions. |
e440a328 | 5290 | { |
0c77715b | 5291 | Numeric_constant left_nc; |
5292 | Numeric_constant right_nc; | |
5293 | if (left->numeric_constant_value(&left_nc) | |
5294 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5295 | { |
0c77715b | 5296 | if (is_comparison) |
e440a328 | 5297 | { |
0c77715b | 5298 | bool result; |
5299 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5300 | &right_nc, location, | |
5301 | &result)) | |
5302 | return this; | |
e90c9dfc | 5303 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5304 | Expression::make_boolean(result, |
5305 | location), | |
5306 | location); | |
e440a328 | 5307 | } |
5308 | else | |
5309 | { | |
0c77715b | 5310 | Numeric_constant nc; |
af7a5274 | 5311 | bool issued_error; |
0c77715b | 5312 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, |
af7a5274 | 5313 | location, &nc, |
5314 | &issued_error)) | |
5315 | { | |
5316 | if (issued_error) | |
5317 | return Expression::make_error(location); | |
71a45216 | 5318 | return this; |
af7a5274 | 5319 | } |
0c77715b | 5320 | return nc.expression(location); |
e440a328 | 5321 | } |
5322 | } | |
e440a328 | 5323 | } |
5324 | ||
5325 | // String constant expressions. | |
315fa98d | 5326 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5327 | { |
5328 | std::string left_string; | |
5329 | std::string right_string; | |
5330 | if (left->string_constant_value(&left_string) | |
5331 | && right->string_constant_value(&right_string)) | |
315fa98d | 5332 | { |
5333 | if (op == OPERATOR_PLUS) | |
5334 | return Expression::make_string(left_string + right_string, | |
5335 | location); | |
5336 | else if (is_comparison) | |
5337 | { | |
5338 | int cmp = left_string.compare(right_string); | |
0c77715b | 5339 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5340 | return Expression::make_boolean(r, location); |
b40dc774 | 5341 | } |
5342 | } | |
b40dc774 | 5343 | } |
5344 | ||
ceeb12d7 | 5345 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5346 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5347 | { | |
b79832ca | 5348 | if (left->type()->struct_type() != NULL |
5349 | && right->type()->struct_type() != NULL) | |
e9d3367e | 5350 | return this->lower_struct_comparison(gogo, inserter); |
5351 | else if (left->type()->array_type() != NULL | |
b79832ca | 5352 | && !left->type()->is_slice_type() |
5353 | && right->type()->array_type() != NULL | |
5354 | && !right->type()->is_slice_type()) | |
e9d3367e | 5355 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 5356 | else if ((left->type()->interface_type() != NULL |
5357 | && right->type()->interface_type() == NULL) | |
5358 | || (left->type()->interface_type() == NULL | |
5359 | && right->type()->interface_type() != NULL)) | |
5360 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5361 | } |
5362 | ||
736a16ba | 5363 | // Lower string concatenation to String_concat_expression, so that |
5364 | // we can group sequences of string additions. | |
5365 | if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) | |
5366 | { | |
5367 | Expression_list* exprs; | |
5368 | String_concat_expression* left_sce = | |
5369 | this->left_->string_concat_expression(); | |
5370 | if (left_sce != NULL) | |
5371 | exprs = left_sce->exprs(); | |
5372 | else | |
5373 | { | |
5374 | exprs = new Expression_list(); | |
5375 | exprs->push_back(this->left_); | |
5376 | } | |
5377 | ||
5378 | String_concat_expression* right_sce = | |
5379 | this->right_->string_concat_expression(); | |
5380 | if (right_sce != NULL) | |
5381 | exprs->append(right_sce->exprs()); | |
5382 | else | |
5383 | exprs->push_back(this->right_); | |
5384 | ||
5385 | return Expression::make_string_concat(exprs); | |
5386 | } | |
5387 | ||
e440a328 | 5388 | return this; |
5389 | } | |
5390 | ||
e9d3367e | 5391 | // Lower a struct comparison. |
5392 | ||
5393 | Expression* | |
5394 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5395 | Statement_inserter* inserter) | |
5396 | { | |
5397 | Struct_type* st = this->left_->type()->struct_type(); | |
5398 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5399 | if (st2 == NULL) | |
5400 | return this; | |
5401 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5402 | return this; | |
5403 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5404 | this->right_->type(), NULL)) | |
5405 | return this; | |
5406 | ||
5407 | // See if we can compare using memcmp. As a heuristic, we use | |
5408 | // memcmp rather than field references and comparisons if there are | |
5409 | // more than two fields. | |
113ef6a5 | 5410 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5411 | return this->lower_compare_to_memcmp(gogo, inserter); |
5412 | ||
5413 | Location loc = this->location(); | |
5414 | ||
5415 | Expression* left = this->left_; | |
5416 | Temporary_statement* left_temp = NULL; | |
5417 | if (left->var_expression() == NULL | |
5418 | && left->temporary_reference_expression() == NULL) | |
5419 | { | |
5420 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5421 | inserter->insert(left_temp); | |
5422 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5423 | } | |
5424 | ||
5425 | Expression* right = this->right_; | |
5426 | Temporary_statement* right_temp = NULL; | |
5427 | if (right->var_expression() == NULL | |
5428 | && right->temporary_reference_expression() == NULL) | |
5429 | { | |
5430 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5431 | inserter->insert(right_temp); | |
5432 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5433 | } | |
5434 | ||
5435 | Expression* ret = Expression::make_boolean(true, loc); | |
5436 | const Struct_field_list* fields = st->fields(); | |
5437 | unsigned int field_index = 0; | |
5438 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5439 | pf != fields->end(); | |
5440 | ++pf, ++field_index) | |
5441 | { | |
f5165c05 | 5442 | if (Gogo::is_sink_name(pf->field_name())) |
5443 | continue; | |
5444 | ||
e9d3367e | 5445 | if (field_index > 0) |
5446 | { | |
5447 | if (left_temp == NULL) | |
5448 | left = left->copy(); | |
5449 | else | |
5450 | left = Expression::make_temporary_reference(left_temp, loc); | |
5451 | if (right_temp == NULL) | |
5452 | right = right->copy(); | |
5453 | else | |
5454 | right = Expression::make_temporary_reference(right_temp, loc); | |
5455 | } | |
5456 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5457 | loc); | |
5458 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5459 | loc); | |
5460 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5461 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5462 | } | |
5463 | ||
5464 | if (this->op_ == OPERATOR_NOTEQ) | |
5465 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5466 | ||
5467 | return ret; | |
5468 | } | |
5469 | ||
5470 | // Lower an array comparison. | |
5471 | ||
5472 | Expression* | |
5473 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5474 | Statement_inserter* inserter) | |
5475 | { | |
5476 | Array_type* at = this->left_->type()->array_type(); | |
5477 | Array_type* at2 = this->right_->type()->array_type(); | |
5478 | if (at2 == NULL) | |
5479 | return this; | |
5480 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5481 | return this; | |
5482 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5483 | this->right_->type(), NULL)) | |
5484 | return this; | |
5485 | ||
5486 | // Call memcmp directly if possible. This may let the middle-end | |
5487 | // optimize the call. | |
113ef6a5 | 5488 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5489 | return this->lower_compare_to_memcmp(gogo, inserter); |
5490 | ||
5491 | // Call the array comparison function. | |
5492 | Named_object* hash_fn; | |
5493 | Named_object* equal_fn; | |
5494 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5495 | &hash_fn, &equal_fn); | |
5496 | ||
5497 | Location loc = this->location(); | |
5498 | ||
5499 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5500 | ||
5501 | Expression_list* args = new Expression_list(); | |
5502 | args->push_back(this->operand_address(inserter, this->left_)); | |
5503 | args->push_back(this->operand_address(inserter, this->right_)); | |
e9d3367e | 5504 | |
5505 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5506 | ||
5507 | if (this->op_ == OPERATOR_NOTEQ) | |
5508 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5509 | ||
5510 | return ret; | |
5511 | } | |
5512 | ||
ceeb12d7 | 5513 | // Lower an interface to value comparison. |
5514 | ||
5515 | Expression* | |
5516 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5517 | Statement_inserter* inserter) | |
5518 | { | |
5519 | Type* left_type = this->left_->type(); | |
5520 | Type* right_type = this->right_->type(); | |
5521 | Interface_type* ift; | |
5522 | if (left_type->interface_type() != NULL) | |
5523 | { | |
5524 | ift = left_type->interface_type(); | |
5525 | if (!ift->implements_interface(right_type, NULL)) | |
5526 | return this; | |
5527 | } | |
5528 | else | |
5529 | { | |
5530 | ift = right_type->interface_type(); | |
5531 | if (!ift->implements_interface(left_type, NULL)) | |
5532 | return this; | |
5533 | } | |
5534 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5535 | return this; | |
5536 | ||
5537 | Location loc = this->location(); | |
5538 | ||
5539 | if (left_type->interface_type() == NULL | |
5540 | && left_type->points_to() == NULL | |
5541 | && !this->left_->is_addressable()) | |
5542 | { | |
5543 | Temporary_statement* temp = | |
5544 | Statement::make_temporary(left_type, NULL, loc); | |
5545 | inserter->insert(temp); | |
5546 | this->left_ = | |
5547 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5548 | } | |
5549 | ||
5550 | if (right_type->interface_type() == NULL | |
5551 | && right_type->points_to() == NULL | |
5552 | && !this->right_->is_addressable()) | |
5553 | { | |
5554 | Temporary_statement* temp = | |
5555 | Statement::make_temporary(right_type, NULL, loc); | |
5556 | inserter->insert(temp); | |
5557 | this->right_ = | |
5558 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5559 | } | |
5560 | ||
5561 | return this; | |
5562 | } | |
5563 | ||
e9d3367e | 5564 | // Lower a struct or array comparison to a call to memcmp. |
5565 | ||
5566 | Expression* | |
5567 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5568 | { | |
5569 | Location loc = this->location(); | |
5570 | ||
5571 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5572 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5573 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5574 | TYPE_INFO_SIZE); | |
5575 | ||
5576 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5577 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5578 | return Expression::make_binary(this->op_, call, zero, loc); |
5579 | } | |
5580 | ||
a32698ee | 5581 | Expression* |
5c3f3470 | 5582 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5583 | Statement_inserter* inserter) |
5584 | { | |
5585 | Location loc = this->location(); | |
5bf8be8b | 5586 | if (this->left_->type()->is_error_type() |
5587 | || this->right_->type()->is_error_type() | |
5588 | || this->left_->is_error_expression() | |
5589 | || this->right_->is_error_expression()) | |
5590 | { | |
5591 | go_assert(saw_errors()); | |
5592 | return Expression::make_error(loc); | |
5593 | } | |
5594 | ||
a32698ee | 5595 | Temporary_statement* temp; |
a32698ee | 5596 | |
5597 | Type* left_type = this->left_->type(); | |
5598 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5599 | || this->op_ == OPERATOR_RSHIFT); | |
5600 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5601 | left_type->integer_type() != NULL) | |
5602 | || this->op_ == OPERATOR_MOD); | |
5603 | ||
a32698ee | 5604 | if (is_shift_op |
5c3f3470 | 5605 | || (is_idiv_op |
5606 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5607 | { |
545ab43b | 5608 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5609 | { |
5610 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5611 | inserter->insert(temp); | |
5612 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5613 | } | |
545ab43b | 5614 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5615 | { |
5616 | temp = | |
5617 | Statement::make_temporary(NULL, this->right_, loc); | |
5618 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5619 | inserter->insert(temp); | |
5620 | } | |
5621 | } | |
5622 | return this; | |
5623 | } | |
5624 | ||
5625 | ||
e9d3367e | 5626 | // Return the address of EXPR, cast to unsafe.Pointer. |
5627 | ||
5628 | Expression* | |
5629 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5630 | Expression* expr) | |
5631 | { | |
5632 | Location loc = this->location(); | |
5633 | ||
5634 | if (!expr->is_addressable()) | |
5635 | { | |
5636 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5637 | loc); | |
5638 | inserter->insert(temp); | |
5639 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5640 | } | |
5641 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5642 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5643 | Type* void_type = Type::make_void_type(); | |
5644 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5645 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5646 | } | |
5647 | ||
0c77715b | 5648 | // Return the numeric constant value, if it has one. |
e440a328 | 5649 | |
5650 | bool | |
0c77715b | 5651 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5652 | { |
0c77715b | 5653 | Numeric_constant left_nc; |
5654 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5655 | return false; | |
5656 | Numeric_constant right_nc; | |
5657 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5658 | return false; | |
af7a5274 | 5659 | bool issued_error; |
9767e2d3 | 5660 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
af7a5274 | 5661 | this->location(), nc, &issued_error); |
e440a328 | 5662 | } |
5663 | ||
5664 | // Note that the value is being discarded. | |
5665 | ||
4f2138d7 | 5666 | bool |
e440a328 | 5667 | Binary_expression::do_discarding_value() |
5668 | { | |
5669 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5670 | return this->right_->discarding_value(); |
e440a328 | 5671 | else |
4f2138d7 | 5672 | { |
5673 | this->unused_value_error(); | |
5674 | return false; | |
5675 | } | |
e440a328 | 5676 | } |
5677 | ||
5678 | // Get type. | |
5679 | ||
5680 | Type* | |
5681 | Binary_expression::do_type() | |
5682 | { | |
5f5fea79 | 5683 | if (this->classification() == EXPRESSION_ERROR) |
5684 | return Type::make_error_type(); | |
5685 | ||
e440a328 | 5686 | switch (this->op_) |
5687 | { | |
e440a328 | 5688 | case OPERATOR_EQEQ: |
5689 | case OPERATOR_NOTEQ: | |
5690 | case OPERATOR_LT: | |
5691 | case OPERATOR_LE: | |
5692 | case OPERATOR_GT: | |
5693 | case OPERATOR_GE: | |
e90c9dfc | 5694 | if (this->type_ == NULL) |
5695 | this->type_ = Type::make_boolean_type(); | |
5696 | return this->type_; | |
e440a328 | 5697 | |
5698 | case OPERATOR_PLUS: | |
5699 | case OPERATOR_MINUS: | |
5700 | case OPERATOR_OR: | |
5701 | case OPERATOR_XOR: | |
5702 | case OPERATOR_MULT: | |
5703 | case OPERATOR_DIV: | |
5704 | case OPERATOR_MOD: | |
5705 | case OPERATOR_AND: | |
5706 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5707 | case OPERATOR_OROR: |
5708 | case OPERATOR_ANDAND: | |
e440a328 | 5709 | { |
0c77715b | 5710 | Type* type; |
5711 | if (!Binary_expression::operation_type(this->op_, | |
5712 | this->left_->type(), | |
5713 | this->right_->type(), | |
5714 | &type)) | |
5715 | return Type::make_error_type(); | |
5716 | return type; | |
e440a328 | 5717 | } |
5718 | ||
5719 | case OPERATOR_LSHIFT: | |
5720 | case OPERATOR_RSHIFT: | |
5721 | return this->left_->type(); | |
5722 | ||
5723 | default: | |
c3e6f413 | 5724 | go_unreachable(); |
e440a328 | 5725 | } |
5726 | } | |
5727 | ||
5728 | // Set type for a binary expression. | |
5729 | ||
5730 | void | |
5731 | Binary_expression::do_determine_type(const Type_context* context) | |
5732 | { | |
5733 | Type* tleft = this->left_->type(); | |
5734 | Type* tright = this->right_->type(); | |
5735 | ||
5736 | // Both sides should have the same type, except for the shift | |
5737 | // operations. For a comparison, we should ignore the incoming | |
5738 | // type. | |
5739 | ||
5740 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5741 | || this->op_ == OPERATOR_RSHIFT); | |
5742 | ||
5743 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5744 | || this->op_ == OPERATOR_NOTEQ | |
5745 | || this->op_ == OPERATOR_LT | |
5746 | || this->op_ == OPERATOR_LE | |
5747 | || this->op_ == OPERATOR_GT | |
5748 | || this->op_ == OPERATOR_GE); | |
5749 | ||
c999c2a7 | 5750 | // For constant expressions, the context of the result is not useful in |
5751 | // determining the types of the operands. It is only legal to use abstract | |
5752 | // boolean, numeric, and string constants as operands where it is legal to | |
5753 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5754 | // Any issues with the operation will be resolved in the check_types pass. | |
5755 | bool is_constant_expr = (this->left_->is_constant() | |
5756 | && this->right_->is_constant()); | |
5757 | ||
e440a328 | 5758 | Type_context subcontext(*context); |
5759 | ||
9c4ff2ce | 5760 | if (is_constant_expr && !is_shift_op) |
af7a5274 | 5761 | { |
5762 | subcontext.type = NULL; | |
5763 | subcontext.may_be_abstract = true; | |
5764 | } | |
5765 | else if (is_comparison) | |
e440a328 | 5766 | { |
5767 | // In a comparison, the context does not determine the types of | |
5768 | // the operands. | |
5769 | subcontext.type = NULL; | |
5770 | } | |
5771 | ||
5772 | // Set the context for the left hand operand. | |
5773 | if (is_shift_op) | |
5774 | { | |
b40dc774 | 5775 | // The right hand operand of a shift plays no role in |
5776 | // determining the type of the left hand operand. | |
e440a328 | 5777 | } |
5778 | else if (!tleft->is_abstract()) | |
5779 | subcontext.type = tleft; | |
5780 | else if (!tright->is_abstract()) | |
5781 | subcontext.type = tright; | |
5782 | else if (subcontext.type == NULL) | |
5783 | { | |
5784 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5785 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5786 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5787 | { | |
5788 | // Both sides have an abstract integer, abstract float, or | |
5789 | // abstract complex type. Just let CONTEXT determine | |
5790 | // whether they may remain abstract or not. | |
5791 | } | |
5792 | else if (tleft->complex_type() != NULL) | |
5793 | subcontext.type = tleft; | |
5794 | else if (tright->complex_type() != NULL) | |
5795 | subcontext.type = tright; | |
5796 | else if (tleft->float_type() != NULL) | |
5797 | subcontext.type = tleft; | |
5798 | else if (tright->float_type() != NULL) | |
5799 | subcontext.type = tright; | |
5800 | else | |
5801 | subcontext.type = tleft; | |
f58a23ae | 5802 | |
5803 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5804 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5805 | } |
5806 | ||
af7a5274 | 5807 | this->left_->determine_type(&subcontext); |
e440a328 | 5808 | |
e440a328 | 5809 | if (is_shift_op) |
5810 | { | |
b40dc774 | 5811 | // We may have inherited an unusable type for the shift operand. |
5812 | // Give a useful error if that happened. | |
5813 | if (tleft->is_abstract() | |
5814 | && subcontext.type != NULL | |
8ab6effb | 5815 | && !subcontext.may_be_abstract |
f6bc81e6 | 5816 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5817 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5818 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5819 | "for left operand of shift")); |
b40dc774 | 5820 | |
5821 | // The context for the right hand operand is the same as for the | |
5822 | // left hand operand, except for a shift operator. | |
e440a328 | 5823 | subcontext.type = Type::lookup_integer_type("uint"); |
5824 | subcontext.may_be_abstract = false; | |
5825 | } | |
5826 | ||
af7a5274 | 5827 | this->right_->determine_type(&subcontext); |
e90c9dfc | 5828 | |
5829 | if (is_comparison) | |
5830 | { | |
5831 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5832 | ; | |
5833 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5834 | this->type_ = context->type; | |
5835 | else if (!context->may_be_abstract) | |
5836 | this->type_ = Type::lookup_bool_type(); | |
5837 | } | |
e440a328 | 5838 | } |
5839 | ||
5840 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5841 | // OTYPE is the type of the other operand. Return whether the |
5842 | // operation is OK. This should not be used for shift. | |
e440a328 | 5843 | |
5844 | bool | |
be8b5eee | 5845 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5846 | Location location) |
e440a328 | 5847 | { |
5848 | switch (op) | |
5849 | { | |
5850 | case OPERATOR_OROR: | |
5851 | case OPERATOR_ANDAND: | |
c999c2a7 | 5852 | if (!type->is_boolean_type() |
5853 | || !otype->is_boolean_type()) | |
e440a328 | 5854 | { |
631d5788 | 5855 | go_error_at(location, "expected boolean type"); |
e440a328 | 5856 | return false; |
5857 | } | |
5858 | break; | |
5859 | ||
5860 | case OPERATOR_EQEQ: | |
5861 | case OPERATOR_NOTEQ: | |
e9d3367e | 5862 | { |
5863 | std::string reason; | |
5864 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5865 | { | |
631d5788 | 5866 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5867 | return false; |
5868 | } | |
5869 | } | |
e440a328 | 5870 | break; |
5871 | ||
5872 | case OPERATOR_LT: | |
5873 | case OPERATOR_LE: | |
5874 | case OPERATOR_GT: | |
5875 | case OPERATOR_GE: | |
e9d3367e | 5876 | { |
5877 | std::string reason; | |
5878 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5879 | { | |
631d5788 | 5880 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5881 | return false; |
5882 | } | |
5883 | } | |
e440a328 | 5884 | break; |
5885 | ||
5886 | case OPERATOR_PLUS: | |
5887 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5888 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5889 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5890 | { |
631d5788 | 5891 | go_error_at(location, |
e440a328 | 5892 | "expected integer, floating, complex, or string type"); |
5893 | return false; | |
5894 | } | |
5895 | break; | |
5896 | ||
5897 | case OPERATOR_MINUS: | |
5898 | case OPERATOR_MINUSEQ: | |
5899 | case OPERATOR_MULT: | |
5900 | case OPERATOR_MULTEQ: | |
5901 | case OPERATOR_DIV: | |
5902 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5903 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5904 | { |
631d5788 | 5905 | go_error_at(location, "expected integer, floating, or complex type"); |
e440a328 | 5906 | return false; |
5907 | } | |
5908 | break; | |
5909 | ||
5910 | case OPERATOR_MOD: | |
5911 | case OPERATOR_MODEQ: | |
5912 | case OPERATOR_OR: | |
5913 | case OPERATOR_OREQ: | |
5914 | case OPERATOR_AND: | |
5915 | case OPERATOR_ANDEQ: | |
5916 | case OPERATOR_XOR: | |
5917 | case OPERATOR_XOREQ: | |
5918 | case OPERATOR_BITCLEAR: | |
5919 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5920 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5921 | { |
631d5788 | 5922 | go_error_at(location, "expected integer type"); |
e440a328 | 5923 | return false; |
5924 | } | |
5925 | break; | |
5926 | ||
5927 | default: | |
c3e6f413 | 5928 | go_unreachable(); |
e440a328 | 5929 | } |
5930 | ||
5931 | return true; | |
5932 | } | |
5933 | ||
5934 | // Check types. | |
5935 | ||
5936 | void | |
5937 | Binary_expression::do_check_types(Gogo*) | |
5938 | { | |
5f5fea79 | 5939 | if (this->classification() == EXPRESSION_ERROR) |
5940 | return; | |
5941 | ||
e440a328 | 5942 | Type* left_type = this->left_->type(); |
5943 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5944 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5945 | { |
5946 | this->set_is_error(); | |
5947 | return; | |
5948 | } | |
e440a328 | 5949 | |
5950 | if (this->op_ == OPERATOR_EQEQ | |
5951 | || this->op_ == OPERATOR_NOTEQ | |
5952 | || this->op_ == OPERATOR_LT | |
5953 | || this->op_ == OPERATOR_LE | |
5954 | || this->op_ == OPERATOR_GT | |
5955 | || this->op_ == OPERATOR_GE) | |
5956 | { | |
907c5ecd | 5957 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5958 | { | |
5959 | this->report_error(_("invalid comparison of nil with nil")); | |
5960 | return; | |
5961 | } | |
e440a328 | 5962 | if (!Type::are_assignable(left_type, right_type, NULL) |
5963 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5964 | { | |
5965 | this->report_error(_("incompatible types in binary expression")); | |
5966 | return; | |
5967 | } | |
5968 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5969 | right_type, |
e440a328 | 5970 | this->location()) |
5971 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5972 | left_type, |
e440a328 | 5973 | this->location())) |
5974 | { | |
5975 | this->set_is_error(); | |
5976 | return; | |
5977 | } | |
5978 | } | |
5979 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5980 | { | |
5981 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5982 | { | |
5983 | this->report_error(_("incompatible types in binary expression")); | |
5984 | return; | |
5985 | } | |
5986 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5987 | right_type, |
e440a328 | 5988 | this->location())) |
5989 | { | |
5990 | this->set_is_error(); | |
5991 | return; | |
5992 | } | |
5c65b19d | 5993 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5994 | { | |
5995 | // Division by a zero integer constant is an error. | |
5996 | Numeric_constant rconst; | |
5997 | unsigned long rval; | |
5998 | if (left_type->integer_type() != NULL | |
5999 | && this->right_->numeric_constant_value(&rconst) | |
6000 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
6001 | && rval == 0) | |
6002 | { | |
6003 | this->report_error(_("integer division by zero")); | |
6004 | return; | |
6005 | } | |
6006 | } | |
e440a328 | 6007 | } |
6008 | else | |
6009 | { | |
6010 | if (left_type->integer_type() == NULL) | |
6011 | this->report_error(_("shift of non-integer operand")); | |
6012 | ||
6b5e0fac | 6013 | if (right_type->is_string_type()) |
6014 | this->report_error(_("shift count not unsigned integer")); | |
6015 | else if (!right_type->is_abstract() | |
e440a328 | 6016 | && (right_type->integer_type() == NULL |
6017 | || !right_type->integer_type()->is_unsigned())) | |
6018 | this->report_error(_("shift count not unsigned integer")); | |
6019 | else | |
6020 | { | |
0c77715b | 6021 | Numeric_constant nc; |
6022 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 6023 | { |
0c77715b | 6024 | mpz_t val; |
6025 | if (!nc.to_int(&val)) | |
6026 | this->report_error(_("shift count not unsigned integer")); | |
6027 | else | |
a4eba91b | 6028 | { |
0c77715b | 6029 | if (mpz_sgn(val) < 0) |
6030 | { | |
6031 | this->report_error(_("negative shift count")); | |
0c77715b | 6032 | Location rloc = this->right_->location(); |
e67508fa | 6033 | this->right_ = Expression::make_integer_ul(0, right_type, |
6034 | rloc); | |
0c77715b | 6035 | } |
6036 | mpz_clear(val); | |
a4eba91b | 6037 | } |
e440a328 | 6038 | } |
e440a328 | 6039 | } |
6040 | } | |
6041 | } | |
6042 | ||
ea664253 | 6043 | // Get the backend representation for a binary expression. |
e440a328 | 6044 | |
ea664253 | 6045 | Bexpression* |
6046 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 6047 | { |
1b1f2abf | 6048 | Gogo* gogo = context->gogo(); |
a32698ee | 6049 | Location loc = this->location(); |
6050 | Type* left_type = this->left_->type(); | |
6051 | Type* right_type = this->right_->type(); | |
1b1f2abf | 6052 | |
e440a328 | 6053 | bool use_left_type = true; |
6054 | bool is_shift_op = false; | |
29a2d1d8 | 6055 | bool is_idiv_op = false; |
e440a328 | 6056 | switch (this->op_) |
6057 | { | |
6058 | case OPERATOR_EQEQ: | |
6059 | case OPERATOR_NOTEQ: | |
6060 | case OPERATOR_LT: | |
6061 | case OPERATOR_LE: | |
6062 | case OPERATOR_GT: | |
6063 | case OPERATOR_GE: | |
ea664253 | 6064 | return Expression::comparison(context, this->type_, this->op_, |
6065 | this->left_, this->right_, loc); | |
e440a328 | 6066 | |
6067 | case OPERATOR_OROR: | |
e440a328 | 6068 | case OPERATOR_ANDAND: |
e440a328 | 6069 | use_left_type = false; |
6070 | break; | |
6071 | case OPERATOR_PLUS: | |
e440a328 | 6072 | case OPERATOR_MINUS: |
e440a328 | 6073 | case OPERATOR_OR: |
e440a328 | 6074 | case OPERATOR_XOR: |
e440a328 | 6075 | case OPERATOR_MULT: |
e440a328 | 6076 | break; |
6077 | case OPERATOR_DIV: | |
a32698ee | 6078 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
6079 | break; | |
729f8831 | 6080 | // Fall through. |
e440a328 | 6081 | case OPERATOR_MOD: |
29a2d1d8 | 6082 | is_idiv_op = true; |
e440a328 | 6083 | break; |
6084 | case OPERATOR_LSHIFT: | |
e440a328 | 6085 | case OPERATOR_RSHIFT: |
e440a328 | 6086 | is_shift_op = true; |
6087 | break; | |
e440a328 | 6088 | case OPERATOR_BITCLEAR: |
a32698ee | 6089 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
6090 | case OPERATOR_AND: | |
e440a328 | 6091 | break; |
6092 | default: | |
c3e6f413 | 6093 | go_unreachable(); |
e440a328 | 6094 | } |
6095 | ||
736a16ba | 6096 | // The only binary operation for string is +, and that should have |
6097 | // been converted to a String_concat_expression in do_lower. | |
6098 | go_assert(!left_type->is_string_type()); | |
a32698ee | 6099 | |
6100 | // For complex division Go might want slightly different results than the | |
6101 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 6102 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
6103 | { | |
a32698ee | 6104 | Runtime::Function complex_code; |
1850e20c | 6105 | switch (this->left_->type()->complex_type()->bits()) |
6106 | { | |
6107 | case 64: | |
a32698ee | 6108 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 6109 | break; |
6110 | case 128: | |
a32698ee | 6111 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 6112 | break; |
6113 | default: | |
6114 | go_unreachable(); | |
6115 | } | |
a32698ee | 6116 | Expression* complex_div = |
6117 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 6118 | return complex_div->get_backend(context); |
1850e20c | 6119 | } |
6120 | ||
ea664253 | 6121 | Bexpression* left = this->left_->get_backend(context); |
6122 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 6123 | |
a32698ee | 6124 | Type* type = use_left_type ? left_type : right_type; |
6125 | Btype* btype = type->get_backend(gogo); | |
6126 | ||
6127 | Bexpression* ret = | |
6128 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
6129 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 6130 | |
a32698ee | 6131 | // Initialize overflow constants. |
6132 | Bexpression* overflow; | |
6133 | mpz_t zero; | |
6134 | mpz_init_set_ui(zero, 0UL); | |
6135 | mpz_t one; | |
6136 | mpz_init_set_ui(one, 1UL); | |
6137 | mpz_t neg_one; | |
6138 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 6139 | |
a32698ee | 6140 | Btype* left_btype = left_type->get_backend(gogo); |
6141 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 6142 | |
6143 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 6144 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 6145 | if (is_shift_op) |
6146 | { | |
a32698ee | 6147 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 6148 | |
a32698ee | 6149 | int bits = left_type->integer_type()->bits(); |
a7c5b619 | 6150 | |
6151 | Numeric_constant nc; | |
6152 | unsigned long ul; | |
6153 | if (!this->right_->numeric_constant_value(&nc) | |
6154 | || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID | |
6155 | || ul >= static_cast<unsigned long>(bits)) | |
e440a328 | 6156 | { |
a7c5b619 | 6157 | mpz_t bitsval; |
6158 | mpz_init_set_ui(bitsval, bits); | |
6159 | Bexpression* bits_expr = | |
6160 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
6161 | Bexpression* compare = | |
6162 | gogo->backend()->binary_expression(OPERATOR_LT, | |
6163 | right, bits_expr, loc); | |
6164 | ||
6165 | Bexpression* zero_expr = | |
6166 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
6167 | overflow = zero_expr; | |
6168 | Bfunction* bfn = context->function()->func_value()->get_decl(); | |
6169 | if (this->op_ == OPERATOR_RSHIFT | |
6170 | && !left_type->integer_type()->is_unsigned()) | |
6171 | { | |
6172 | Bexpression* neg_expr = | |
6173 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
6174 | zero_expr, loc); | |
6175 | Bexpression* neg_one_expr = | |
6176 | gogo->backend()->integer_constant_expression(left_btype, | |
6177 | neg_one); | |
6178 | overflow = gogo->backend()->conditional_expression(bfn, | |
6179 | btype, | |
6180 | neg_expr, | |
6181 | neg_one_expr, | |
6182 | zero_expr, | |
6183 | loc); | |
6184 | } | |
6185 | ret = gogo->backend()->conditional_expression(bfn, btype, compare, | |
6186 | ret, overflow, loc); | |
6187 | mpz_clear(bitsval); | |
29a2d1d8 | 6188 | } |
29a2d1d8 | 6189 | } |
6190 | ||
6191 | // Add checks for division by zero and division overflow as needed. | |
6192 | if (is_idiv_op) | |
6193 | { | |
5c3f3470 | 6194 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 6195 | { |
6196 | // right == 0 | |
a32698ee | 6197 | Bexpression* zero_expr = |
6198 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
6199 | Bexpression* check = | |
6200 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6201 | right, zero_expr, loc); | |
29a2d1d8 | 6202 | |
a32698ee | 6203 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 6204 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 6205 | Bexpression* crash = gogo->runtime_error(errcode, |
6206 | loc)->get_backend(context); | |
29a2d1d8 | 6207 | |
6208 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
93715b75 | 6209 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
6210 | ret = gogo->backend()->conditional_expression(bfn, btype, | |
6211 | check, crash, | |
ea664253 | 6212 | ret, loc); |
b13c66cd | 6213 | } |
6214 | ||
5c3f3470 | 6215 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 6216 | { |
6217 | // right == -1 | |
6218 | // FIXME: It would be nice to say that this test is expected | |
6219 | // to return false. | |
a32698ee | 6220 | |
6221 | Bexpression* neg_one_expr = | |
6222 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6223 | Bexpression* check = | |
6224 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6225 | right, neg_one_expr, loc); | |
6226 | ||
6227 | Bexpression* zero_expr = | |
6228 | gogo->backend()->integer_constant_expression(btype, zero); | |
6229 | Bexpression* one_expr = | |
6230 | gogo->backend()->integer_constant_expression(btype, one); | |
93715b75 | 6231 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
a32698ee | 6232 | |
6233 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6234 | { |
6235 | // An unsigned -1 is the largest possible number, so | |
6236 | // dividing is always 1 or 0. | |
a32698ee | 6237 | |
6238 | Bexpression* cmp = | |
6239 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6240 | left, right, loc); | |
29a2d1d8 | 6241 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6242 | overflow = |
93715b75 | 6243 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6244 | one_expr, zero_expr, |
6245 | loc); | |
29a2d1d8 | 6246 | else |
a32698ee | 6247 | overflow = |
93715b75 | 6248 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6249 | zero_expr, left, |
6250 | loc); | |
29a2d1d8 | 6251 | } |
6252 | else | |
6253 | { | |
6254 | // Computing left / -1 is the same as computing - left, | |
6255 | // which does not overflow since Go sets -fwrapv. | |
6256 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6257 | { |
6258 | Expression* negate_expr = | |
6259 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 6260 | overflow = negate_expr->get_backend(context); |
a32698ee | 6261 | } |
29a2d1d8 | 6262 | else |
a32698ee | 6263 | overflow = zero_expr; |
29a2d1d8 | 6264 | } |
a32698ee | 6265 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6266 | |
6267 | // right == -1 ? - left : ret | |
93715b75 | 6268 | ret = gogo->backend()->conditional_expression(bfn, btype, |
6269 | check, overflow, | |
a32698ee | 6270 | ret, loc); |
29a2d1d8 | 6271 | } |
e440a328 | 6272 | } |
6273 | ||
a32698ee | 6274 | mpz_clear(zero); |
6275 | mpz_clear(one); | |
6276 | mpz_clear(neg_one); | |
ea664253 | 6277 | return ret; |
e440a328 | 6278 | } |
6279 | ||
6280 | // Export a binary expression. | |
6281 | ||
6282 | void | |
6283 | Binary_expression::do_export(Export* exp) const | |
6284 | { | |
6285 | exp->write_c_string("("); | |
6286 | this->left_->export_expression(exp); | |
6287 | switch (this->op_) | |
6288 | { | |
6289 | case OPERATOR_OROR: | |
6290 | exp->write_c_string(" || "); | |
6291 | break; | |
6292 | case OPERATOR_ANDAND: | |
6293 | exp->write_c_string(" && "); | |
6294 | break; | |
6295 | case OPERATOR_EQEQ: | |
6296 | exp->write_c_string(" == "); | |
6297 | break; | |
6298 | case OPERATOR_NOTEQ: | |
6299 | exp->write_c_string(" != "); | |
6300 | break; | |
6301 | case OPERATOR_LT: | |
6302 | exp->write_c_string(" < "); | |
6303 | break; | |
6304 | case OPERATOR_LE: | |
6305 | exp->write_c_string(" <= "); | |
6306 | break; | |
6307 | case OPERATOR_GT: | |
6308 | exp->write_c_string(" > "); | |
6309 | break; | |
6310 | case OPERATOR_GE: | |
6311 | exp->write_c_string(" >= "); | |
6312 | break; | |
6313 | case OPERATOR_PLUS: | |
6314 | exp->write_c_string(" + "); | |
6315 | break; | |
6316 | case OPERATOR_MINUS: | |
6317 | exp->write_c_string(" - "); | |
6318 | break; | |
6319 | case OPERATOR_OR: | |
6320 | exp->write_c_string(" | "); | |
6321 | break; | |
6322 | case OPERATOR_XOR: | |
6323 | exp->write_c_string(" ^ "); | |
6324 | break; | |
6325 | case OPERATOR_MULT: | |
6326 | exp->write_c_string(" * "); | |
6327 | break; | |
6328 | case OPERATOR_DIV: | |
6329 | exp->write_c_string(" / "); | |
6330 | break; | |
6331 | case OPERATOR_MOD: | |
6332 | exp->write_c_string(" % "); | |
6333 | break; | |
6334 | case OPERATOR_LSHIFT: | |
6335 | exp->write_c_string(" << "); | |
6336 | break; | |
6337 | case OPERATOR_RSHIFT: | |
6338 | exp->write_c_string(" >> "); | |
6339 | break; | |
6340 | case OPERATOR_AND: | |
6341 | exp->write_c_string(" & "); | |
6342 | break; | |
6343 | case OPERATOR_BITCLEAR: | |
6344 | exp->write_c_string(" &^ "); | |
6345 | break; | |
6346 | default: | |
c3e6f413 | 6347 | go_unreachable(); |
e440a328 | 6348 | } |
6349 | this->right_->export_expression(exp); | |
6350 | exp->write_c_string(")"); | |
6351 | } | |
6352 | ||
6353 | // Import a binary expression. | |
6354 | ||
6355 | Expression* | |
6356 | Binary_expression::do_import(Import* imp) | |
6357 | { | |
6358 | imp->require_c_string("("); | |
6359 | ||
6360 | Expression* left = Expression::import_expression(imp); | |
6361 | ||
6362 | Operator op; | |
6363 | if (imp->match_c_string(" || ")) | |
6364 | { | |
6365 | op = OPERATOR_OROR; | |
6366 | imp->advance(4); | |
6367 | } | |
6368 | else if (imp->match_c_string(" && ")) | |
6369 | { | |
6370 | op = OPERATOR_ANDAND; | |
6371 | imp->advance(4); | |
6372 | } | |
6373 | else if (imp->match_c_string(" == ")) | |
6374 | { | |
6375 | op = OPERATOR_EQEQ; | |
6376 | imp->advance(4); | |
6377 | } | |
6378 | else if (imp->match_c_string(" != ")) | |
6379 | { | |
6380 | op = OPERATOR_NOTEQ; | |
6381 | imp->advance(4); | |
6382 | } | |
6383 | else if (imp->match_c_string(" < ")) | |
6384 | { | |
6385 | op = OPERATOR_LT; | |
6386 | imp->advance(3); | |
6387 | } | |
6388 | else if (imp->match_c_string(" <= ")) | |
6389 | { | |
6390 | op = OPERATOR_LE; | |
6391 | imp->advance(4); | |
6392 | } | |
6393 | else if (imp->match_c_string(" > ")) | |
6394 | { | |
6395 | op = OPERATOR_GT; | |
6396 | imp->advance(3); | |
6397 | } | |
6398 | else if (imp->match_c_string(" >= ")) | |
6399 | { | |
6400 | op = OPERATOR_GE; | |
6401 | imp->advance(4); | |
6402 | } | |
6403 | else if (imp->match_c_string(" + ")) | |
6404 | { | |
6405 | op = OPERATOR_PLUS; | |
6406 | imp->advance(3); | |
6407 | } | |
6408 | else if (imp->match_c_string(" - ")) | |
6409 | { | |
6410 | op = OPERATOR_MINUS; | |
6411 | imp->advance(3); | |
6412 | } | |
6413 | else if (imp->match_c_string(" | ")) | |
6414 | { | |
6415 | op = OPERATOR_OR; | |
6416 | imp->advance(3); | |
6417 | } | |
6418 | else if (imp->match_c_string(" ^ ")) | |
6419 | { | |
6420 | op = OPERATOR_XOR; | |
6421 | imp->advance(3); | |
6422 | } | |
6423 | else if (imp->match_c_string(" * ")) | |
6424 | { | |
6425 | op = OPERATOR_MULT; | |
6426 | imp->advance(3); | |
6427 | } | |
6428 | else if (imp->match_c_string(" / ")) | |
6429 | { | |
6430 | op = OPERATOR_DIV; | |
6431 | imp->advance(3); | |
6432 | } | |
6433 | else if (imp->match_c_string(" % ")) | |
6434 | { | |
6435 | op = OPERATOR_MOD; | |
6436 | imp->advance(3); | |
6437 | } | |
6438 | else if (imp->match_c_string(" << ")) | |
6439 | { | |
6440 | op = OPERATOR_LSHIFT; | |
6441 | imp->advance(4); | |
6442 | } | |
6443 | else if (imp->match_c_string(" >> ")) | |
6444 | { | |
6445 | op = OPERATOR_RSHIFT; | |
6446 | imp->advance(4); | |
6447 | } | |
6448 | else if (imp->match_c_string(" & ")) | |
6449 | { | |
6450 | op = OPERATOR_AND; | |
6451 | imp->advance(3); | |
6452 | } | |
6453 | else if (imp->match_c_string(" &^ ")) | |
6454 | { | |
6455 | op = OPERATOR_BITCLEAR; | |
6456 | imp->advance(4); | |
6457 | } | |
6458 | else | |
6459 | { | |
631d5788 | 6460 | go_error_at(imp->location(), "unrecognized binary operator"); |
e440a328 | 6461 | return Expression::make_error(imp->location()); |
6462 | } | |
6463 | ||
6464 | Expression* right = Expression::import_expression(imp); | |
6465 | ||
6466 | imp->require_c_string(")"); | |
6467 | ||
6468 | return Expression::make_binary(op, left, right, imp->location()); | |
6469 | } | |
6470 | ||
d751bb78 | 6471 | // Dump ast representation of a binary expression. |
6472 | ||
6473 | void | |
6474 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6475 | { | |
6476 | ast_dump_context->ostream() << "("; | |
6477 | ast_dump_context->dump_expression(this->left_); | |
6478 | ast_dump_context->ostream() << " "; | |
6479 | ast_dump_context->dump_operator(this->op_); | |
6480 | ast_dump_context->ostream() << " "; | |
6481 | ast_dump_context->dump_expression(this->right_); | |
6482 | ast_dump_context->ostream() << ") "; | |
6483 | } | |
6484 | ||
e440a328 | 6485 | // Make a binary expression. |
6486 | ||
6487 | Expression* | |
6488 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6489 | Location location) |
e440a328 | 6490 | { |
6491 | return new Binary_expression(op, left, right, location); | |
6492 | } | |
6493 | ||
6494 | // Implement a comparison. | |
6495 | ||
a32698ee | 6496 | Bexpression* |
6497 | Expression::comparison(Translate_context* context, Type* result_type, | |
6498 | Operator op, Expression* left, Expression* right, | |
6499 | Location location) | |
e440a328 | 6500 | { |
2387f644 | 6501 | Type* left_type = left->type(); |
6502 | Type* right_type = right->type(); | |
ceeb12d7 | 6503 | |
e67508fa | 6504 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6505 | |
15c67ee2 | 6506 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6507 | { |
6098d6cb | 6508 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
6509 | { | |
6510 | left = Runtime::make_call(Runtime::EQSTRING, location, 2, | |
6511 | left, right); | |
6512 | right = Expression::make_boolean(true, location); | |
6513 | } | |
6514 | else | |
6515 | { | |
6516 | left = Runtime::make_call(Runtime::CMPSTRING, location, 2, | |
6517 | left, right); | |
6518 | right = zexpr; | |
6519 | } | |
e440a328 | 6520 | } |
15c67ee2 | 6521 | else if ((left_type->interface_type() != NULL |
6522 | && right_type->interface_type() == NULL | |
6523 | && !right_type->is_nil_type()) | |
6524 | || (left_type->interface_type() == NULL | |
6525 | && !left_type->is_nil_type() | |
6526 | && right_type->interface_type() != NULL)) | |
e440a328 | 6527 | { |
6528 | // Comparing an interface value to a non-interface value. | |
6529 | if (left_type->interface_type() == NULL) | |
6530 | { | |
6531 | std::swap(left_type, right_type); | |
2387f644 | 6532 | std::swap(left, right); |
e440a328 | 6533 | } |
6534 | ||
6535 | // The right operand is not an interface. We need to take its | |
6536 | // address if it is not a pointer. | |
ceeb12d7 | 6537 | Expression* pointer_arg = NULL; |
e440a328 | 6538 | if (right_type->points_to() != NULL) |
2387f644 | 6539 | pointer_arg = right; |
e440a328 | 6540 | else |
6541 | { | |
2387f644 | 6542 | go_assert(right->is_addressable()); |
6543 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6544 | location); |
e440a328 | 6545 | } |
e440a328 | 6546 | |
2387f644 | 6547 | Expression* descriptor = |
6548 | Expression::make_type_descriptor(right_type, location); | |
6549 | left = | |
ceeb12d7 | 6550 | Runtime::make_call((left_type->interface_type()->is_empty() |
6098d6cb | 6551 | ? Runtime::EFACEVALEQ |
6552 | : Runtime::IFACEVALEQ), | |
2387f644 | 6553 | location, 3, left, descriptor, |
ceeb12d7 | 6554 | pointer_arg); |
6098d6cb | 6555 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6556 | right = Expression::make_boolean(true, location); | |
e440a328 | 6557 | } |
6558 | else if (left_type->interface_type() != NULL | |
6559 | && right_type->interface_type() != NULL) | |
6560 | { | |
ceeb12d7 | 6561 | Runtime::Function compare_function; |
739bad04 | 6562 | if (left_type->interface_type()->is_empty() |
6563 | && right_type->interface_type()->is_empty()) | |
6098d6cb | 6564 | compare_function = Runtime::EFACEEQ; |
739bad04 | 6565 | else if (!left_type->interface_type()->is_empty() |
6566 | && !right_type->interface_type()->is_empty()) | |
6098d6cb | 6567 | compare_function = Runtime::IFACEEQ; |
739bad04 | 6568 | else |
6569 | { | |
6570 | if (left_type->interface_type()->is_empty()) | |
6571 | { | |
739bad04 | 6572 | std::swap(left_type, right_type); |
2387f644 | 6573 | std::swap(left, right); |
739bad04 | 6574 | } |
c484d925 | 6575 | go_assert(!left_type->interface_type()->is_empty()); |
6576 | go_assert(right_type->interface_type()->is_empty()); | |
6098d6cb | 6577 | compare_function = Runtime::IFACEEFACEEQ; |
739bad04 | 6578 | } |
6579 | ||
2387f644 | 6580 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6098d6cb | 6581 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6582 | right = Expression::make_boolean(true, location); | |
e440a328 | 6583 | } |
6584 | ||
6585 | if (left_type->is_nil_type() | |
6586 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6587 | { | |
6588 | std::swap(left_type, right_type); | |
2387f644 | 6589 | std::swap(left, right); |
e440a328 | 6590 | } |
6591 | ||
6592 | if (right_type->is_nil_type()) | |
6593 | { | |
2387f644 | 6594 | right = Expression::make_nil(location); |
e440a328 | 6595 | if (left_type->array_type() != NULL |
6596 | && left_type->array_type()->length() == NULL) | |
6597 | { | |
6598 | Array_type* at = left_type->array_type(); | |
44dbe1d7 | 6599 | bool is_lvalue = false; |
6600 | left = at->get_value_pointer(context->gogo(), left, is_lvalue); | |
e440a328 | 6601 | } |
6602 | else if (left_type->interface_type() != NULL) | |
6603 | { | |
6604 | // An interface is nil if the first field is nil. | |
2387f644 | 6605 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6606 | } |
6607 | } | |
6608 | ||
ea664253 | 6609 | Bexpression* left_bexpr = left->get_backend(context); |
6610 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6611 | |
a32698ee | 6612 | Gogo* gogo = context->gogo(); |
6613 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6614 | right_bexpr, location); | |
6615 | if (result_type != NULL) | |
6616 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6617 | ret, location); | |
e440a328 | 6618 | return ret; |
6619 | } | |
6620 | ||
736a16ba | 6621 | // Class String_concat_expression. |
6622 | ||
6623 | bool | |
6624 | String_concat_expression::do_is_constant() const | |
6625 | { | |
6626 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6627 | pe != this->exprs_->end(); | |
6628 | ++pe) | |
6629 | { | |
6630 | if (!(*pe)->is_constant()) | |
6631 | return false; | |
6632 | } | |
6633 | return true; | |
6634 | } | |
6635 | ||
6636 | bool | |
3ae06f68 | 6637 | String_concat_expression::do_is_static_initializer() const |
736a16ba | 6638 | { |
6639 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6640 | pe != this->exprs_->end(); | |
6641 | ++pe) | |
6642 | { | |
3ae06f68 | 6643 | if (!(*pe)->is_static_initializer()) |
736a16ba | 6644 | return false; |
6645 | } | |
6646 | return true; | |
6647 | } | |
6648 | ||
6649 | Type* | |
6650 | String_concat_expression::do_type() | |
6651 | { | |
6652 | Type* t = this->exprs_->front()->type(); | |
6653 | Expression_list::iterator pe = this->exprs_->begin(); | |
6654 | ++pe; | |
6655 | for (; pe != this->exprs_->end(); ++pe) | |
6656 | { | |
6657 | Type* t1; | |
6658 | if (!Binary_expression::operation_type(OPERATOR_PLUS, t, | |
6659 | (*pe)->type(), | |
6660 | &t1)) | |
6661 | return Type::make_error_type(); | |
6662 | t = t1; | |
6663 | } | |
6664 | return t; | |
6665 | } | |
6666 | ||
6667 | void | |
6668 | String_concat_expression::do_determine_type(const Type_context* context) | |
6669 | { | |
6670 | Type_context subcontext(*context); | |
6671 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6672 | pe != this->exprs_->end(); | |
6673 | ++pe) | |
6674 | { | |
6675 | Type* t = (*pe)->type(); | |
6676 | if (!t->is_abstract()) | |
6677 | { | |
6678 | subcontext.type = t; | |
6679 | break; | |
6680 | } | |
6681 | } | |
6682 | if (subcontext.type == NULL) | |
6683 | subcontext.type = this->exprs_->front()->type(); | |
6684 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6685 | pe != this->exprs_->end(); | |
6686 | ++pe) | |
6687 | (*pe)->determine_type(&subcontext); | |
6688 | } | |
6689 | ||
6690 | void | |
6691 | String_concat_expression::do_check_types(Gogo*) | |
6692 | { | |
6693 | if (this->is_error_expression()) | |
6694 | return; | |
6695 | Type* t = this->exprs_->front()->type(); | |
6696 | if (t->is_error()) | |
6697 | { | |
6698 | this->set_is_error(); | |
6699 | return; | |
6700 | } | |
6701 | Expression_list::iterator pe = this->exprs_->begin(); | |
6702 | ++pe; | |
6703 | for (; pe != this->exprs_->end(); ++pe) | |
6704 | { | |
6705 | Type* t1 = (*pe)->type(); | |
6706 | if (!Type::are_compatible_for_binop(t, t1)) | |
6707 | { | |
6708 | this->report_error("incompatible types in binary expression"); | |
6709 | return; | |
6710 | } | |
6711 | if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, | |
6712 | this->location())) | |
6713 | { | |
6714 | this->set_is_error(); | |
6715 | return; | |
6716 | } | |
6717 | } | |
6718 | } | |
6719 | ||
6720 | Expression* | |
6721 | String_concat_expression::do_flatten(Gogo*, Named_object*, | |
6722 | Statement_inserter*) | |
6723 | { | |
6724 | if (this->is_error_expression()) | |
6725 | return this; | |
6726 | Location loc = this->location(); | |
6727 | Type* type = this->type(); | |
6728 | Expression* nil_arg = Expression::make_nil(loc); | |
6729 | Expression* call; | |
6730 | switch (this->exprs_->size()) | |
6731 | { | |
6732 | case 0: case 1: | |
6733 | go_unreachable(); | |
6734 | ||
6735 | case 2: case 3: case 4: case 5: | |
6736 | { | |
6737 | Expression* len = Expression::make_integer_ul(this->exprs_->size(), | |
6738 | NULL, loc); | |
6739 | Array_type* arg_type = Type::make_array_type(type, len); | |
6740 | arg_type->set_is_array_incomparable(); | |
6741 | Expression* arg = | |
6742 | Expression::make_array_composite_literal(arg_type, this->exprs_, | |
6743 | loc); | |
6744 | Runtime::Function code; | |
6745 | switch (this->exprs_->size()) | |
6746 | { | |
6747 | default: | |
6748 | go_unreachable(); | |
6749 | case 2: | |
6750 | code = Runtime::CONCATSTRING2; | |
6751 | break; | |
6752 | case 3: | |
6753 | code = Runtime::CONCATSTRING3; | |
6754 | break; | |
6755 | case 4: | |
6756 | code = Runtime::CONCATSTRING4; | |
6757 | break; | |
6758 | case 5: | |
6759 | code = Runtime::CONCATSTRING5; | |
6760 | break; | |
6761 | } | |
6762 | call = Runtime::make_call(code, loc, 2, nil_arg, arg); | |
6763 | } | |
6764 | break; | |
6765 | ||
6766 | default: | |
6767 | { | |
6768 | Type* arg_type = Type::make_array_type(type, NULL); | |
6769 | Slice_construction_expression* sce = | |
6770 | Expression::make_slice_composite_literal(arg_type, this->exprs_, | |
6771 | loc); | |
6772 | sce->set_storage_does_not_escape(); | |
6773 | call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg, | |
6774 | sce); | |
6775 | } | |
6776 | break; | |
6777 | } | |
6778 | ||
6779 | return Expression::make_cast(type, call, loc); | |
6780 | } | |
6781 | ||
6782 | void | |
6783 | String_concat_expression::do_dump_expression( | |
6784 | Ast_dump_context* ast_dump_context) const | |
6785 | { | |
6786 | ast_dump_context->ostream() << "concat("; | |
6787 | ast_dump_context->dump_expression_list(this->exprs_, false); | |
6788 | ast_dump_context->ostream() << ")"; | |
6789 | } | |
6790 | ||
6791 | Expression* | |
6792 | Expression::make_string_concat(Expression_list* exprs) | |
6793 | { | |
6794 | return new String_concat_expression(exprs); | |
6795 | } | |
6796 | ||
e440a328 | 6797 | // Class Bound_method_expression. |
6798 | ||
6799 | // Traversal. | |
6800 | ||
6801 | int | |
6802 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6803 | { | |
e0659c9e | 6804 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6805 | } |
6806 | ||
6807 | // Return the type of a bound method expression. The type of this | |
0afbb937 | 6808 | // object is simply the type of the method with no receiver. |
e440a328 | 6809 | |
6810 | Type* | |
6811 | Bound_method_expression::do_type() | |
6812 | { | |
0afbb937 | 6813 | Named_object* fn = this->method_->named_object(); |
6814 | Function_type* fntype; | |
6815 | if (fn->is_function()) | |
6816 | fntype = fn->func_value()->type(); | |
6817 | else if (fn->is_function_declaration()) | |
6818 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6819 | else |
6820 | return Type::make_error_type(); | |
0afbb937 | 6821 | return fntype->copy_without_receiver(); |
e440a328 | 6822 | } |
6823 | ||
6824 | // Determine the types of a method expression. | |
6825 | ||
6826 | void | |
6827 | Bound_method_expression::do_determine_type(const Type_context*) | |
6828 | { | |
0afbb937 | 6829 | Named_object* fn = this->method_->named_object(); |
6830 | Function_type* fntype; | |
6831 | if (fn->is_function()) | |
6832 | fntype = fn->func_value()->type(); | |
6833 | else if (fn->is_function_declaration()) | |
6834 | fntype = fn->func_declaration_value()->type(); | |
6835 | else | |
6836 | fntype = NULL; | |
e440a328 | 6837 | if (fntype == NULL || !fntype->is_method()) |
6838 | this->expr_->determine_type_no_context(); | |
6839 | else | |
6840 | { | |
6841 | Type_context subcontext(fntype->receiver()->type(), false); | |
6842 | this->expr_->determine_type(&subcontext); | |
6843 | } | |
6844 | } | |
6845 | ||
6846 | // Check the types of a method expression. | |
6847 | ||
6848 | void | |
6849 | Bound_method_expression::do_check_types(Gogo*) | |
6850 | { | |
0afbb937 | 6851 | Named_object* fn = this->method_->named_object(); |
6852 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6853 | { | |
6854 | this->report_error(_("object is not a method")); | |
6855 | return; | |
6856 | } | |
6857 | ||
6858 | Function_type* fntype; | |
6859 | if (fn->is_function()) | |
6860 | fntype = fn->func_value()->type(); | |
6861 | else if (fn->is_function_declaration()) | |
6862 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6863 | else |
0afbb937 | 6864 | go_unreachable(); |
6865 | Type* rtype = fntype->receiver()->type()->deref(); | |
6866 | Type* etype = (this->expr_type_ != NULL | |
6867 | ? this->expr_type_ | |
6868 | : this->expr_->type()); | |
6869 | etype = etype->deref(); | |
6870 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6871 | this->report_error(_("method type does not match object type")); | |
6872 | } | |
6873 | ||
6874 | // If a bound method expression is not simply called, then it is | |
6875 | // represented as a closure. The closure will hold a single variable, | |
6876 | // the receiver to pass to the method. The function will be a simple | |
6877 | // thunk that pulls that value from the closure and calls the method | |
6878 | // with the remaining arguments. | |
6879 | // | |
6880 | // Because method values are not common, we don't build all thunks for | |
6881 | // every methods, but instead only build them as we need them. In | |
6882 | // particular, we even build them on demand for methods defined in | |
6883 | // other packages. | |
6884 | ||
6885 | Bound_method_expression::Method_value_thunks | |
6886 | Bound_method_expression::method_value_thunks; | |
6887 | ||
6888 | // Find or create the thunk for METHOD. | |
6889 | ||
6890 | Named_object* | |
6891 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6892 | Named_object* fn) | |
6893 | { | |
6894 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6895 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6896 | Bound_method_expression::method_value_thunks.insert(val); | |
6897 | if (!ins.second) | |
6898 | { | |
6899 | // We have seen this method before. | |
6900 | go_assert(ins.first->second != NULL); | |
6901 | return ins.first->second; | |
6902 | } | |
6903 | ||
6904 | Location loc = fn->location(); | |
6905 | ||
6906 | Function_type* orig_fntype; | |
6907 | if (fn->is_function()) | |
6908 | orig_fntype = fn->func_value()->type(); | |
6909 | else if (fn->is_function_declaration()) | |
6910 | orig_fntype = fn->func_declaration_value()->type(); | |
6911 | else | |
6912 | orig_fntype = NULL; | |
6913 | ||
6914 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6915 | { |
13f2fdb8 | 6916 | ins.first->second = |
6917 | Named_object::make_erroneous_name(gogo->thunk_name()); | |
0afbb937 | 6918 | return ins.first->second; |
e440a328 | 6919 | } |
0afbb937 | 6920 | |
6921 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6922 | // The type here is wrong--it should be the C function type. But it |
6923 | // doesn't really matter. | |
0afbb937 | 6924 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
13f2fdb8 | 6925 | sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc))); |
6926 | sfl->push_back(Struct_field(Typed_identifier("val", | |
0afbb937 | 6927 | orig_fntype->receiver()->type(), |
6928 | loc))); | |
6bf4793c | 6929 | Struct_type* st = Type::make_struct_type(sfl, loc); |
6930 | st->set_is_struct_incomparable(); | |
6931 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 6932 | |
f8bdf81a | 6933 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6934 | |
13f2fdb8 | 6935 | std::string thunk_name = gogo->thunk_name(); |
da244e59 | 6936 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, |
0afbb937 | 6937 | false, loc); |
6938 | ||
f8bdf81a | 6939 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6940 | cvar->set_is_used(); | |
1ecc6157 | 6941 | cvar->set_is_closure(); |
da244e59 | 6942 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6943 | NULL, cvar); | |
f8bdf81a | 6944 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6945 | |
f8bdf81a | 6946 | gogo->start_block(loc); |
0afbb937 | 6947 | |
6948 | // Field 0 of the closure is the function code pointer, field 1 is | |
6949 | // the value on which to invoke the method. | |
6950 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 6951 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 6952 | arg = Expression::make_field_reference(arg, 1, loc); |
6953 | ||
6954 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6955 | ||
6956 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6957 | Expression_list* args; | |
6958 | if (orig_params == NULL || orig_params->empty()) | |
6959 | args = NULL; | |
6960 | else | |
6961 | { | |
6962 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6963 | args = new Expression_list(); | |
6964 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6965 | p != new_params->end(); |
0afbb937 | 6966 | ++p) |
6967 | { | |
6968 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6969 | go_assert(p_no != NULL | |
6970 | && p_no->is_variable() | |
6971 | && p_no->var_value()->is_parameter()); | |
6972 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6973 | } | |
6974 | } | |
6975 | ||
6976 | Call_expression* call = Expression::make_call(bme, args, | |
6977 | orig_fntype->is_varargs(), | |
6978 | loc); | |
6979 | call->set_varargs_are_lowered(); | |
6980 | ||
6981 | Statement* s = Statement::make_return_from_call(call, loc); | |
6982 | gogo->add_statement(s); | |
6983 | Block* b = gogo->finish_block(loc); | |
6984 | gogo->add_block(b, loc); | |
6985 | gogo->lower_block(new_no, b); | |
a32698ee | 6986 | gogo->flatten_block(new_no, b); |
0afbb937 | 6987 | gogo->finish_function(loc); |
6988 | ||
6989 | ins.first->second = new_no; | |
6990 | return new_no; | |
6991 | } | |
6992 | ||
6993 | // Return an expression to check *REF for nil while dereferencing | |
6994 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6995 | // reference. This is a static function so that we don't have to | |
6996 | // worry about declaring Field_indexes in expressions.h. | |
6997 | ||
6998 | static Expression* | |
6999 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
7000 | Expression** ref) | |
7001 | { | |
7002 | if (field_indexes == NULL) | |
7003 | return Expression::make_boolean(false, loc); | |
7004 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
7005 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
7006 | go_assert(stype != NULL | |
7007 | && field_indexes->field_index < stype->field_count()); | |
7008 | if ((*ref)->type()->struct_type() == NULL) | |
7009 | { | |
7010 | go_assert((*ref)->type()->points_to() != NULL); | |
7011 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
7012 | Expression::make_nil(loc), | |
7013 | loc); | |
7014 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
f614ea8b | 7015 | *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT, |
7016 | loc); | |
0afbb937 | 7017 | go_assert((*ref)->type()->struct_type() == stype); |
7018 | } | |
7019 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
7020 | loc); | |
7021 | return cond; | |
e440a328 | 7022 | } |
7023 | ||
cd39797e | 7024 | // Flatten a method value into a struct with nil checks. We can't do |
7025 | // this in the lowering phase, because if the method value is called | |
7026 | // directly we don't need a thunk. That case will have been handled | |
7027 | // by Call_expression::do_lower, so if we get here then we do need a | |
7028 | // thunk. | |
e440a328 | 7029 | |
cd39797e | 7030 | Expression* |
7031 | Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, | |
7032 | Statement_inserter* inserter) | |
e440a328 | 7033 | { |
cd39797e | 7034 | Location loc = this->location(); |
7035 | ||
7036 | Named_object* thunk = Bound_method_expression::create_thunk(gogo, | |
0afbb937 | 7037 | this->method_, |
7038 | this->function_); | |
7039 | if (thunk->is_erroneous()) | |
7040 | { | |
7041 | go_assert(saw_errors()); | |
cd39797e | 7042 | return Expression::make_error(loc); |
0afbb937 | 7043 | } |
7044 | ||
cd39797e | 7045 | // Force the expression into a variable. This is only necessary if |
7046 | // we are going to do nil checks below, but it's easy enough to | |
7047 | // always do it. | |
7048 | Expression* expr = this->expr_; | |
7049 | if (!expr->is_variable()) | |
7050 | { | |
7051 | Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); | |
7052 | inserter->insert(etemp); | |
7053 | expr = Expression::make_temporary_reference(etemp, loc); | |
7054 | } | |
0afbb937 | 7055 | |
7056 | // If the method expects a value, and we have a pointer, we need to | |
7057 | // dereference the pointer. | |
7058 | ||
7059 | Named_object* fn = this->method_->named_object(); | |
cd39797e | 7060 | Function_type *fntype; |
0afbb937 | 7061 | if (fn->is_function()) |
7062 | fntype = fn->func_value()->type(); | |
7063 | else if (fn->is_function_declaration()) | |
7064 | fntype = fn->func_declaration_value()->type(); | |
7065 | else | |
7066 | go_unreachable(); | |
7067 | ||
cd39797e | 7068 | Expression* val = expr; |
0afbb937 | 7069 | if (fntype->receiver()->type()->points_to() == NULL |
7070 | && val->type()->points_to() != NULL) | |
f614ea8b | 7071 | val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc); |
0afbb937 | 7072 | |
7073 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
7074 | // expect a closure whose second field has type this->expr_type_ (if | |
7075 | // that is not NULL). We are going to pass it a closure whose | |
7076 | // second field has type this->expr_->type(). Since | |
7077 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
7078 | // away with this. | |
7079 | ||
7080 | Struct_field_list* fields = new Struct_field_list(); | |
13f2fdb8 | 7081 | fields->push_back(Struct_field(Typed_identifier("fn", |
0afbb937 | 7082 | thunk->func_value()->type(), |
7083 | loc))); | |
13f2fdb8 | 7084 | fields->push_back(Struct_field(Typed_identifier("val", val->type(), loc))); |
0afbb937 | 7085 | Struct_type* st = Type::make_struct_type(fields, loc); |
6bf4793c | 7086 | st->set_is_struct_incomparable(); |
0afbb937 | 7087 | |
7088 | Expression_list* vals = new Expression_list(); | |
7089 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
7090 | vals->push_back(val); | |
7091 | ||
7092 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
c1177ba4 | 7093 | ret = Expression::make_heap_expression(ret, loc); |
0afbb937 | 7094 | |
c1177ba4 | 7095 | Node* n = Node::make_node(this); |
7096 | if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE) | |
7097 | ret->heap_expression()->set_allocate_on_stack(); | |
7098 | else if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
7099 | go_error_at(loc, "%s escapes to heap, not allowed in runtime", | |
7100 | n->ast_format(gogo).c_str()); | |
cd39797e | 7101 | |
7102 | // If necessary, check whether the expression or any embedded | |
7103 | // pointers are nil. | |
0afbb937 | 7104 | |
df7ef1fd | 7105 | Expression* nil_check = NULL; |
0afbb937 | 7106 | if (this->method_->field_indexes() != NULL) |
7107 | { | |
0afbb937 | 7108 | Expression* ref = expr; |
7109 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
7110 | expr = ref; | |
7111 | } | |
7112 | ||
7113 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
7114 | { | |
7115 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
7116 | Expression::make_nil(loc), | |
7117 | loc); | |
7118 | if (nil_check == NULL) | |
7119 | nil_check = n; | |
7120 | else | |
7121 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
7122 | } | |
7123 | ||
7124 | if (nil_check != NULL) | |
7125 | { | |
cd39797e | 7126 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
7127 | loc); | |
7128 | // Fix the type of the conditional expression by pretending to | |
7129 | // evaluate to RET either way through the conditional. | |
7130 | crash = Expression::make_compound(crash, ret, loc); | |
7131 | ret = Expression::make_conditional(nil_check, crash, ret, loc); | |
7132 | } | |
7133 | ||
7134 | // RET is a pointer to a struct, but we want a function type. | |
7135 | ret = Expression::make_unsafe_cast(this->type(), ret, loc); | |
7136 | ||
7137 | return ret; | |
e440a328 | 7138 | } |
7139 | ||
d751bb78 | 7140 | // Dump ast representation of a bound method expression. |
7141 | ||
7142 | void | |
7143 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
7144 | const | |
7145 | { | |
7146 | if (this->expr_type_ != NULL) | |
7147 | ast_dump_context->ostream() << "("; | |
7148 | ast_dump_context->dump_expression(this->expr_); | |
7149 | if (this->expr_type_ != NULL) | |
7150 | { | |
7151 | ast_dump_context->ostream() << ":"; | |
7152 | ast_dump_context->dump_type(this->expr_type_); | |
7153 | ast_dump_context->ostream() << ")"; | |
7154 | } | |
7155 | ||
0afbb937 | 7156 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 7157 | } |
7158 | ||
e440a328 | 7159 | // Make a method expression. |
7160 | ||
7161 | Bound_method_expression* | |
0afbb937 | 7162 | Expression::make_bound_method(Expression* expr, const Method* method, |
7163 | Named_object* function, Location location) | |
e440a328 | 7164 | { |
0afbb937 | 7165 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 7166 | } |
7167 | ||
7168 | // Class Builtin_call_expression. This is used for a call to a | |
7169 | // builtin function. | |
7170 | ||
e440a328 | 7171 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, |
7172 | Expression* fn, | |
7173 | Expression_list* args, | |
7174 | bool is_varargs, | |
b13c66cd | 7175 | Location location) |
e440a328 | 7176 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 7177 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
7178 | recover_arg_is_set_(false) | |
e440a328 | 7179 | { |
7180 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 7181 | if (fnexp == NULL) |
7182 | { | |
7183 | this->code_ = BUILTIN_INVALID; | |
7184 | return; | |
7185 | } | |
e440a328 | 7186 | const std::string& name(fnexp->named_object()->name()); |
7187 | if (name == "append") | |
7188 | this->code_ = BUILTIN_APPEND; | |
7189 | else if (name == "cap") | |
7190 | this->code_ = BUILTIN_CAP; | |
7191 | else if (name == "close") | |
7192 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 7193 | else if (name == "complex") |
7194 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 7195 | else if (name == "copy") |
7196 | this->code_ = BUILTIN_COPY; | |
1cce762f | 7197 | else if (name == "delete") |
7198 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 7199 | else if (name == "imag") |
7200 | this->code_ = BUILTIN_IMAG; | |
7201 | else if (name == "len") | |
7202 | this->code_ = BUILTIN_LEN; | |
7203 | else if (name == "make") | |
7204 | this->code_ = BUILTIN_MAKE; | |
7205 | else if (name == "new") | |
7206 | this->code_ = BUILTIN_NEW; | |
7207 | else if (name == "panic") | |
7208 | this->code_ = BUILTIN_PANIC; | |
7209 | else if (name == "print") | |
7210 | this->code_ = BUILTIN_PRINT; | |
7211 | else if (name == "println") | |
7212 | this->code_ = BUILTIN_PRINTLN; | |
7213 | else if (name == "real") | |
7214 | this->code_ = BUILTIN_REAL; | |
7215 | else if (name == "recover") | |
7216 | this->code_ = BUILTIN_RECOVER; | |
7217 | else if (name == "Alignof") | |
7218 | this->code_ = BUILTIN_ALIGNOF; | |
7219 | else if (name == "Offsetof") | |
7220 | this->code_ = BUILTIN_OFFSETOF; | |
7221 | else if (name == "Sizeof") | |
7222 | this->code_ = BUILTIN_SIZEOF; | |
7223 | else | |
c3e6f413 | 7224 | go_unreachable(); |
e440a328 | 7225 | } |
7226 | ||
7227 | // Return whether this is a call to recover. This is a virtual | |
7228 | // function called from the parent class. | |
7229 | ||
7230 | bool | |
7231 | Builtin_call_expression::do_is_recover_call() const | |
7232 | { | |
7233 | if (this->classification() == EXPRESSION_ERROR) | |
7234 | return false; | |
7235 | return this->code_ == BUILTIN_RECOVER; | |
7236 | } | |
7237 | ||
7238 | // Set the argument for a call to recover. | |
7239 | ||
7240 | void | |
7241 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7242 | { | |
7243 | const Expression_list* args = this->args(); | |
c484d925 | 7244 | go_assert(args == NULL || args->empty()); |
e440a328 | 7245 | Expression_list* new_args = new Expression_list(); |
7246 | new_args->push_back(arg); | |
7247 | this->set_args(new_args); | |
6334270b | 7248 | this->recover_arg_is_set_ = true; |
e440a328 | 7249 | } |
7250 | ||
e440a328 | 7251 | // Lower a builtin call expression. This turns new and make into |
7252 | // specific expressions. We also convert to a constant if we can. | |
7253 | ||
7254 | Expression* | |
321e5ad2 | 7255 | Builtin_call_expression::do_lower(Gogo*, Named_object* function, |
ceeb4318 | 7256 | Statement_inserter* inserter, int) |
e440a328 | 7257 | { |
79651b1f | 7258 | if (this->is_error_expression()) |
a9182619 | 7259 | return this; |
7260 | ||
b13c66cd | 7261 | Location loc = this->location(); |
1cce762f | 7262 | |
a8725655 | 7263 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7264 | { | |
7265 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7266 | return Expression::make_error(loc); |
a8725655 | 7267 | } |
7268 | ||
393ba00b | 7269 | if (this->code_ == BUILTIN_OFFSETOF) |
7270 | { | |
7271 | Expression* arg = this->one_arg(); | |
12e69faa | 7272 | |
7273 | if (arg->bound_method_expression() != NULL | |
7274 | || arg->interface_field_reference_expression() != NULL) | |
7275 | { | |
7276 | this->report_error(_("invalid use of method value as argument " | |
7277 | "of Offsetof")); | |
7278 | return this; | |
7279 | } | |
7280 | ||
393ba00b | 7281 | Field_reference_expression* farg = arg->field_reference_expression(); |
7282 | while (farg != NULL) | |
7283 | { | |
7284 | if (!farg->implicit()) | |
7285 | break; | |
7286 | // When the selector refers to an embedded field, | |
7287 | // it must not be reached through pointer indirections. | |
7288 | if (farg->expr()->deref() != farg->expr()) | |
7289 | { | |
12e69faa | 7290 | this->report_error(_("argument of Offsetof implies " |
7291 | "indirection of an embedded field")); | |
393ba00b | 7292 | return this; |
7293 | } | |
7294 | // Go up until we reach the original base. | |
7295 | farg = farg->expr()->field_reference_expression(); | |
7296 | } | |
7297 | } | |
7298 | ||
1cce762f | 7299 | if (this->is_constant()) |
e440a328 | 7300 | { |
0c77715b | 7301 | Numeric_constant nc; |
7302 | if (this->numeric_constant_value(&nc)) | |
7303 | return nc.expression(loc); | |
e440a328 | 7304 | } |
1cce762f | 7305 | |
7306 | switch (this->code_) | |
e440a328 | 7307 | { |
1cce762f | 7308 | default: |
7309 | break; | |
7310 | ||
7311 | case BUILTIN_NEW: | |
7312 | { | |
7313 | const Expression_list* args = this->args(); | |
7314 | if (args == NULL || args->size() < 1) | |
7315 | this->report_error(_("not enough arguments")); | |
7316 | else if (args->size() > 1) | |
7317 | this->report_error(_("too many arguments")); | |
7318 | else | |
7319 | { | |
7320 | Expression* arg = args->front(); | |
7321 | if (!arg->is_type_expression()) | |
7322 | { | |
631d5788 | 7323 | go_error_at(arg->location(), "expected type"); |
1cce762f | 7324 | this->set_is_error(); |
7325 | } | |
7326 | else | |
7327 | return Expression::make_allocation(arg->type(), loc); | |
7328 | } | |
7329 | } | |
7330 | break; | |
7331 | ||
7332 | case BUILTIN_MAKE: | |
321e5ad2 | 7333 | return this->lower_make(inserter); |
1cce762f | 7334 | |
7335 | case BUILTIN_RECOVER: | |
e440a328 | 7336 | if (function != NULL) |
7337 | function->func_value()->set_calls_recover(); | |
7338 | else | |
7339 | { | |
7340 | // Calling recover outside of a function always returns the | |
7341 | // nil empty interface. | |
823c7e3d | 7342 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7343 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7344 | } |
1cce762f | 7345 | break; |
7346 | ||
1cce762f | 7347 | case BUILTIN_DELETE: |
7348 | { | |
7349 | // Lower to a runtime function call. | |
7350 | const Expression_list* args = this->args(); | |
7351 | if (args == NULL || args->size() < 2) | |
7352 | this->report_error(_("not enough arguments")); | |
7353 | else if (args->size() > 2) | |
7354 | this->report_error(_("too many arguments")); | |
7355 | else if (args->front()->type()->map_type() == NULL) | |
7356 | this->report_error(_("argument 1 must be a map")); | |
7357 | else | |
7358 | { | |
7359 | // Since this function returns no value it must appear in | |
7360 | // a statement by itself, so we don't have to worry about | |
7361 | // order of evaluation of values around it. Evaluate the | |
7362 | // map first to get order of evaluation right. | |
7363 | Map_type* mt = args->front()->type()->map_type(); | |
7364 | Temporary_statement* map_temp = | |
7365 | Statement::make_temporary(mt, args->front(), loc); | |
7366 | inserter->insert(map_temp); | |
7367 | ||
7368 | Temporary_statement* key_temp = | |
7369 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7370 | inserter->insert(key_temp); | |
7371 | ||
0d5530d9 | 7372 | Expression* e1 = Expression::make_type_descriptor(mt, loc); |
7373 | Expression* e2 = Expression::make_temporary_reference(map_temp, | |
1cce762f | 7374 | loc); |
0d5530d9 | 7375 | Expression* e3 = Expression::make_temporary_reference(key_temp, |
1cce762f | 7376 | loc); |
0d5530d9 | 7377 | e3 = Expression::make_unary(OPERATOR_AND, e3, loc); |
1cce762f | 7378 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), |
0d5530d9 | 7379 | 3, e1, e2, e3); |
1cce762f | 7380 | } |
7381 | } | |
7382 | break; | |
88b03a70 | 7383 | |
7384 | case BUILTIN_PRINT: | |
7385 | case BUILTIN_PRINTLN: | |
7386 | // Force all the arguments into temporary variables, so that we | |
7387 | // don't try to evaluate something while holding the print lock. | |
7388 | if (this->args() == NULL) | |
7389 | break; | |
7390 | for (Expression_list::iterator pa = this->args()->begin(); | |
7391 | pa != this->args()->end(); | |
7392 | ++pa) | |
7393 | { | |
493ce3ee | 7394 | if (!(*pa)->is_variable() && !(*pa)->is_constant()) |
88b03a70 | 7395 | { |
7396 | Temporary_statement* temp = | |
7397 | Statement::make_temporary(NULL, *pa, loc); | |
7398 | inserter->insert(temp); | |
7399 | *pa = Expression::make_temporary_reference(temp, loc); | |
7400 | } | |
7401 | } | |
7402 | break; | |
e440a328 | 7403 | } |
7404 | ||
7405 | return this; | |
7406 | } | |
7407 | ||
35a54f17 | 7408 | // Flatten a builtin call expression. This turns the arguments of copy and |
7409 | // append into temporary expressions. | |
7410 | ||
7411 | Expression* | |
321e5ad2 | 7412 | Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function, |
35a54f17 | 7413 | Statement_inserter* inserter) |
7414 | { | |
16cb7fec | 7415 | Location loc = this->location(); |
7416 | ||
7417 | switch (this->code_) | |
35a54f17 | 7418 | { |
16cb7fec | 7419 | default: |
7420 | break; | |
7421 | ||
7422 | case BUILTIN_APPEND: | |
321e5ad2 | 7423 | return this->flatten_append(gogo, function, inserter); |
7424 | ||
16cb7fec | 7425 | case BUILTIN_COPY: |
7426 | { | |
7427 | Type* at = this->args()->front()->type(); | |
7428 | for (Expression_list::iterator pa = this->args()->begin(); | |
7429 | pa != this->args()->end(); | |
7430 | ++pa) | |
7431 | { | |
7432 | if ((*pa)->is_nil_expression()) | |
7433 | { | |
7434 | Expression* nil = Expression::make_nil(loc); | |
7435 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
7436 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
7437 | } | |
7438 | if (!(*pa)->is_variable()) | |
7439 | { | |
7440 | Temporary_statement* temp = | |
7441 | Statement::make_temporary(NULL, *pa, loc); | |
7442 | inserter->insert(temp); | |
7443 | *pa = Expression::make_temporary_reference(temp, loc); | |
7444 | } | |
7445 | } | |
7446 | } | |
7447 | break; | |
7448 | ||
7449 | case BUILTIN_PANIC: | |
35a54f17 | 7450 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 7451 | pa != this->args()->end(); |
7452 | ++pa) | |
7453 | { | |
7454 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 7455 | { |
16cb7fec | 7456 | Temporary_statement* temp = |
7457 | Statement::make_temporary(NULL, *pa, loc); | |
7458 | inserter->insert(temp); | |
7459 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 7460 | } |
16cb7fec | 7461 | } |
7739537f | 7462 | break; |
0d5530d9 | 7463 | |
7464 | case BUILTIN_LEN: | |
132ed071 | 7465 | case BUILTIN_CAP: |
321e5ad2 | 7466 | { |
7467 | Expression_list::iterator pa = this->args()->begin(); | |
7468 | if (!(*pa)->is_variable() | |
7469 | && ((*pa)->type()->map_type() != NULL | |
7470 | || (*pa)->type()->channel_type() != NULL)) | |
7471 | { | |
7472 | Temporary_statement* temp = | |
7473 | Statement::make_temporary(NULL, *pa, loc); | |
7474 | inserter->insert(temp); | |
7475 | *pa = Expression::make_temporary_reference(temp, loc); | |
7476 | } | |
7477 | } | |
7478 | break; | |
35a54f17 | 7479 | } |
16cb7fec | 7480 | |
35a54f17 | 7481 | return this; |
7482 | } | |
7483 | ||
a9182619 | 7484 | // Lower a make expression. |
7485 | ||
7486 | Expression* | |
321e5ad2 | 7487 | Builtin_call_expression::lower_make(Statement_inserter* inserter) |
a9182619 | 7488 | { |
b13c66cd | 7489 | Location loc = this->location(); |
a9182619 | 7490 | |
7491 | const Expression_list* args = this->args(); | |
7492 | if (args == NULL || args->size() < 1) | |
7493 | { | |
7494 | this->report_error(_("not enough arguments")); | |
7495 | return Expression::make_error(this->location()); | |
7496 | } | |
7497 | ||
7498 | Expression_list::const_iterator parg = args->begin(); | |
7499 | ||
7500 | Expression* first_arg = *parg; | |
7501 | if (!first_arg->is_type_expression()) | |
7502 | { | |
631d5788 | 7503 | go_error_at(first_arg->location(), "expected type"); |
a9182619 | 7504 | this->set_is_error(); |
7505 | return Expression::make_error(this->location()); | |
7506 | } | |
7507 | Type* type = first_arg->type(); | |
7508 | ||
22deed0d | 7509 | if (!type->in_heap()) |
7510 | go_error_at(first_arg->location(), | |
7511 | "can't make slice of go:notinheap type"); | |
7512 | ||
a9182619 | 7513 | bool is_slice = false; |
7514 | bool is_map = false; | |
7515 | bool is_chan = false; | |
411eb89e | 7516 | if (type->is_slice_type()) |
a9182619 | 7517 | is_slice = true; |
7518 | else if (type->map_type() != NULL) | |
7519 | is_map = true; | |
7520 | else if (type->channel_type() != NULL) | |
7521 | is_chan = true; | |
7522 | else | |
7523 | { | |
7524 | this->report_error(_("invalid type for make function")); | |
7525 | return Expression::make_error(this->location()); | |
7526 | } | |
7527 | ||
f6bc81e6 | 7528 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7529 | ||
a9182619 | 7530 | ++parg; |
7531 | Expression* len_arg; | |
ccea2b36 | 7532 | bool len_small = false; |
a9182619 | 7533 | if (parg == args->end()) |
7534 | { | |
7535 | if (is_slice) | |
7536 | { | |
7537 | this->report_error(_("length required when allocating a slice")); | |
7538 | return Expression::make_error(this->location()); | |
7539 | } | |
e67508fa | 7540 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
33d1d391 | 7541 | len_small = true; |
a9182619 | 7542 | } |
7543 | else | |
7544 | { | |
7545 | len_arg = *parg; | |
f6bc81e6 | 7546 | len_arg->determine_type(&int_context); |
a065edc5 | 7547 | if (len_arg->type()->integer_type() == NULL) |
7548 | { | |
7549 | go_error_at(len_arg->location(), "non-integer len argument in make"); | |
7550 | return Expression::make_error(this->location()); | |
7551 | } | |
ccea2b36 | 7552 | if (!this->check_int_value(len_arg, true, &len_small)) |
1ad00fd4 | 7553 | return Expression::make_error(this->location()); |
a9182619 | 7554 | ++parg; |
7555 | } | |
7556 | ||
7557 | Expression* cap_arg = NULL; | |
ccea2b36 | 7558 | bool cap_small = false; |
72bf0e6e | 7559 | Numeric_constant nclen; |
7560 | Numeric_constant nccap; | |
7561 | unsigned long vlen; | |
7562 | unsigned long vcap; | |
a9182619 | 7563 | if (is_slice && parg != args->end()) |
7564 | { | |
7565 | cap_arg = *parg; | |
f6bc81e6 | 7566 | cap_arg->determine_type(&int_context); |
a065edc5 | 7567 | if (cap_arg->type()->integer_type() == NULL) |
7568 | { | |
7569 | go_error_at(cap_arg->location(), "non-integer cap argument in make"); | |
7570 | return Expression::make_error(this->location()); | |
7571 | } | |
ccea2b36 | 7572 | if (!this->check_int_value(cap_arg, false, &cap_small)) |
1ad00fd4 | 7573 | return Expression::make_error(this->location()); |
7574 | ||
1ad00fd4 | 7575 | if (len_arg->numeric_constant_value(&nclen) |
7576 | && cap_arg->numeric_constant_value(&nccap) | |
7577 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7578 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7579 | && vlen > vcap) | |
a9182619 | 7580 | { |
1ad00fd4 | 7581 | this->report_error(_("len larger than cap")); |
a9182619 | 7582 | return Expression::make_error(this->location()); |
7583 | } | |
1ad00fd4 | 7584 | |
a9182619 | 7585 | ++parg; |
7586 | } | |
7587 | ||
7588 | if (parg != args->end()) | |
7589 | { | |
7590 | this->report_error(_("too many arguments to make")); | |
7591 | return Expression::make_error(this->location()); | |
7592 | } | |
7593 | ||
b13c66cd | 7594 | Location type_loc = first_arg->location(); |
a9182619 | 7595 | |
7596 | Expression* call; | |
7597 | if (is_slice) | |
7598 | { | |
7599 | if (cap_arg == NULL) | |
321e5ad2 | 7600 | { |
72bf0e6e | 7601 | cap_small = len_small; |
7602 | if (len_arg->numeric_constant_value(&nclen) | |
7603 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID) | |
7604 | cap_arg = Expression::make_integer_ul(vlen, len_arg->type(), loc); | |
7605 | else | |
7606 | { | |
7607 | Temporary_statement* temp = Statement::make_temporary(NULL, | |
7608 | len_arg, | |
7609 | loc); | |
7610 | inserter->insert(temp); | |
7611 | len_arg = Expression::make_temporary_reference(temp, loc); | |
7612 | cap_arg = Expression::make_temporary_reference(temp, loc); | |
7613 | } | |
321e5ad2 | 7614 | } |
ccea2b36 | 7615 | |
72bf0e6e | 7616 | Type* et = type->array_type()->element_type(); |
7617 | Expression* type_arg = Expression::make_type_descriptor(et, type_loc); | |
ccea2b36 | 7618 | Runtime::Function code = Runtime::MAKESLICE; |
7619 | if (!len_small || !cap_small) | |
7620 | code = Runtime::MAKESLICE64; | |
7621 | call = Runtime::make_call(code, loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7622 | } |
7623 | else if (is_map) | |
321e5ad2 | 7624 | { |
7625 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
33d1d391 | 7626 | if (!len_small) |
7627 | call = Runtime::make_call(Runtime::MAKEMAP64, loc, 3, type_arg, | |
7628 | len_arg, | |
7629 | Expression::make_nil(loc)); | |
7630 | else | |
7631 | { | |
7632 | Numeric_constant nclen; | |
7633 | unsigned long vlen; | |
7634 | if (len_arg->numeric_constant_value(&nclen) | |
7635 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7636 | && vlen <= Map_type::bucket_size) | |
7637 | call = Runtime::make_call(Runtime::MAKEMAP_SMALL, loc, 0); | |
7638 | else | |
7639 | call = Runtime::make_call(Runtime::MAKEMAP, loc, 3, type_arg, | |
7640 | len_arg, | |
7641 | Expression::make_nil(loc)); | |
7642 | } | |
321e5ad2 | 7643 | } |
a9182619 | 7644 | else if (is_chan) |
321e5ad2 | 7645 | { |
7646 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
1423c90a | 7647 | Runtime::Function code = Runtime::MAKECHAN; |
7648 | if (!len_small) | |
7649 | code = Runtime::MAKECHAN64; | |
7650 | call = Runtime::make_call(code, loc, 2, type_arg, len_arg); | |
321e5ad2 | 7651 | } |
a9182619 | 7652 | else |
7653 | go_unreachable(); | |
7654 | ||
7655 | return Expression::make_unsafe_cast(type, call, loc); | |
7656 | } | |
7657 | ||
321e5ad2 | 7658 | // Flatten a call to the predeclared append function. We do this in |
7659 | // the flatten phase, not the lowering phase, so that we run after | |
7660 | // type checking and after order_evaluations. | |
7661 | ||
7662 | Expression* | |
7663 | Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function, | |
7664 | Statement_inserter* inserter) | |
7665 | { | |
7666 | if (this->is_error_expression()) | |
7667 | return this; | |
7668 | ||
7669 | Location loc = this->location(); | |
7670 | ||
7671 | const Expression_list* args = this->args(); | |
7672 | go_assert(args != NULL && !args->empty()); | |
7673 | ||
7674 | Type* slice_type = args->front()->type(); | |
7675 | go_assert(slice_type->is_slice_type()); | |
7676 | Type* element_type = slice_type->array_type()->element_type(); | |
7677 | ||
7678 | if (args->size() == 1) | |
7679 | { | |
7680 | // append(s) evaluates to s. | |
7681 | return args->front(); | |
7682 | } | |
7683 | ||
7684 | Type* int_type = Type::lookup_integer_type("int"); | |
7685 | Type* uint_type = Type::lookup_integer_type("uint"); | |
7686 | ||
7687 | // Implementing | |
7688 | // append(s1, s2...) | |
7689 | // or | |
7690 | // append(s1, a1, a2, a3, ...) | |
7691 | ||
7692 | // s1tmp := s1 | |
7693 | Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(), | |
7694 | loc); | |
7695 | inserter->insert(s1tmp); | |
7696 | ||
7697 | // l1tmp := len(s1tmp) | |
7698 | Named_object* lenfn = gogo->lookup_global("len"); | |
7699 | Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7700 | Expression_list* call_args = new Expression_list(); | |
7701 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7702 | Expression* len = Expression::make_call(lenref, call_args, false, loc); | |
7703 | gogo->lower_expression(function, inserter, &len); | |
7704 | gogo->flatten_expression(function, inserter, &len); | |
7705 | Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc); | |
7706 | inserter->insert(l1tmp); | |
7707 | ||
7708 | Temporary_statement* s2tmp = NULL; | |
7709 | Temporary_statement* l2tmp = NULL; | |
7710 | Expression_list* add = NULL; | |
7711 | Expression* len2; | |
7712 | if (this->is_varargs()) | |
7713 | { | |
7714 | go_assert(args->size() == 2); | |
7715 | ||
7716 | // s2tmp := s2 | |
7717 | s2tmp = Statement::make_temporary(NULL, args->back(), loc); | |
7718 | inserter->insert(s2tmp); | |
7719 | ||
7720 | // l2tmp := len(s2tmp) | |
7721 | lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7722 | call_args = new Expression_list(); | |
7723 | call_args->push_back(Expression::make_temporary_reference(s2tmp, loc)); | |
7724 | len = Expression::make_call(lenref, call_args, false, loc); | |
7725 | gogo->lower_expression(function, inserter, &len); | |
7726 | gogo->flatten_expression(function, inserter, &len); | |
7727 | l2tmp = Statement::make_temporary(int_type, len, loc); | |
7728 | inserter->insert(l2tmp); | |
7729 | ||
7730 | // len2 = l2tmp | |
7731 | len2 = Expression::make_temporary_reference(l2tmp, loc); | |
7732 | } | |
7733 | else | |
7734 | { | |
7735 | // We have to ensure that all the arguments are in variables | |
7736 | // now, because otherwise if one of them is an index expression | |
7737 | // into the current slice we could overwrite it before we fetch | |
7738 | // it. | |
7739 | add = new Expression_list(); | |
7740 | Expression_list::const_iterator pa = args->begin(); | |
7741 | for (++pa; pa != args->end(); ++pa) | |
7742 | { | |
7743 | if ((*pa)->is_variable()) | |
7744 | add->push_back(*pa); | |
7745 | else | |
7746 | { | |
7747 | Temporary_statement* tmp = Statement::make_temporary(NULL, *pa, | |
7748 | loc); | |
7749 | inserter->insert(tmp); | |
7750 | add->push_back(Expression::make_temporary_reference(tmp, loc)); | |
7751 | } | |
7752 | } | |
7753 | ||
7754 | // len2 = len(add) | |
7755 | len2 = Expression::make_integer_ul(add->size(), int_type, loc); | |
7756 | } | |
7757 | ||
7758 | // ntmp := l1tmp + len2 | |
7759 | Expression* ref = Expression::make_temporary_reference(l1tmp, loc); | |
7760 | Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc); | |
7761 | gogo->lower_expression(function, inserter, &sum); | |
7762 | gogo->flatten_expression(function, inserter, &sum); | |
7763 | Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc); | |
7764 | inserter->insert(ntmp); | |
7765 | ||
7766 | // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ? | |
7767 | // growslice(type, s1tmp, ntmp) : | |
7768 | // s1tmp[:ntmp] | |
7769 | // Using uint here means that if the computation of ntmp overflowed, | |
7770 | // we will call growslice which will panic. | |
7771 | ||
7772 | Expression* left = Expression::make_temporary_reference(ntmp, loc); | |
7773 | left = Expression::make_cast(uint_type, left, loc); | |
7774 | ||
7775 | Named_object* capfn = gogo->lookup_global("cap"); | |
7776 | Expression* capref = Expression::make_func_reference(capfn, NULL, loc); | |
7777 | call_args = new Expression_list(); | |
7778 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7779 | Expression* right = Expression::make_call(capref, call_args, false, loc); | |
7780 | right = Expression::make_cast(uint_type, right, loc); | |
7781 | ||
7782 | Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc); | |
7783 | ||
7784 | Expression* a1 = Expression::make_type_descriptor(element_type, loc); | |
7785 | Expression* a2 = Expression::make_temporary_reference(s1tmp, loc); | |
7786 | Expression* a3 = Expression::make_temporary_reference(ntmp, loc); | |
7787 | Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3, | |
7788 | a1, a2, a3); | |
7789 | call = Expression::make_unsafe_cast(slice_type, call, loc); | |
7790 | ||
7791 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7792 | Expression* zero = Expression::make_integer_ul(0, int_type, loc); | |
7793 | Expression* ref2 = Expression::make_temporary_reference(ntmp, loc); | |
7794 | // FIXME: Mark this index as not requiring bounds checks. | |
7795 | ref = Expression::make_index(ref, zero, ref2, NULL, loc); | |
7796 | ||
7797 | Expression* rhs = Expression::make_conditional(cond, call, ref, loc); | |
7798 | ||
7799 | gogo->lower_expression(function, inserter, &rhs); | |
7800 | gogo->flatten_expression(function, inserter, &rhs); | |
7801 | ||
7802 | Expression* lhs = Expression::make_temporary_reference(s1tmp, loc); | |
7803 | Statement* assign = Statement::make_assignment(lhs, rhs, loc); | |
7804 | inserter->insert(assign); | |
7805 | ||
7806 | if (this->is_varargs()) | |
7807 | { | |
7808 | // copy(s1tmp[l1tmp:], s2tmp) | |
7809 | a1 = Expression::make_temporary_reference(s1tmp, loc); | |
7810 | ref = Expression::make_temporary_reference(l1tmp, loc); | |
7811 | Expression* nil = Expression::make_nil(loc); | |
7812 | // FIXME: Mark this index as not requiring bounds checks. | |
7813 | a1 = Expression::make_index(a1, ref, nil, NULL, loc); | |
7814 | ||
7815 | a2 = Expression::make_temporary_reference(s2tmp, loc); | |
7816 | ||
7817 | Named_object* copyfn = gogo->lookup_global("copy"); | |
7818 | Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc); | |
7819 | call_args = new Expression_list(); | |
7820 | call_args->push_back(a1); | |
7821 | call_args->push_back(a2); | |
7822 | call = Expression::make_call(copyref, call_args, false, loc); | |
7823 | gogo->lower_expression(function, inserter, &call); | |
7824 | gogo->flatten_expression(function, inserter, &call); | |
7825 | inserter->insert(Statement::make_statement(call, false)); | |
7826 | } | |
7827 | else | |
7828 | { | |
7829 | // For each argument: | |
7830 | // s1tmp[l1tmp+i] = a | |
7831 | unsigned long i = 0; | |
7832 | for (Expression_list::const_iterator pa = add->begin(); | |
7833 | pa != add->end(); | |
7834 | ++pa, ++i) | |
7835 | { | |
7836 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7837 | ref2 = Expression::make_temporary_reference(l1tmp, loc); | |
7838 | Expression* off = Expression::make_integer_ul(i, int_type, loc); | |
7839 | ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc); | |
7840 | // FIXME: Mark this index as not requiring bounds checks. | |
7841 | lhs = Expression::make_index(ref, ref2, NULL, NULL, loc); | |
7842 | gogo->lower_expression(function, inserter, &lhs); | |
7843 | gogo->flatten_expression(function, inserter, &lhs); | |
03118c21 | 7844 | // The flatten pass runs after the write barrier pass, so we |
7845 | // need to insert a write barrier here if necessary. | |
7846 | if (!gogo->assign_needs_write_barrier(lhs)) | |
7847 | assign = Statement::make_assignment(lhs, *pa, loc); | |
7848 | else | |
7849 | { | |
7850 | Function* f = function == NULL ? NULL : function->func_value(); | |
7851 | assign = gogo->assign_with_write_barrier(f, NULL, inserter, | |
7852 | lhs, *pa, loc); | |
7853 | } | |
321e5ad2 | 7854 | inserter->insert(assign); |
7855 | } | |
7856 | } | |
7857 | ||
7858 | return Expression::make_temporary_reference(s1tmp, loc); | |
7859 | } | |
7860 | ||
a9182619 | 7861 | // Return whether an expression has an integer value. Report an error |
7862 | // if not. This is used when handling calls to the predeclared make | |
ccea2b36 | 7863 | // function. Set *SMALL if the value is known to fit in type "int". |
a9182619 | 7864 | |
7865 | bool | |
ccea2b36 | 7866 | Builtin_call_expression::check_int_value(Expression* e, bool is_length, |
7867 | bool *small) | |
a9182619 | 7868 | { |
ccea2b36 | 7869 | *small = false; |
7870 | ||
0c77715b | 7871 | Numeric_constant nc; |
1ad00fd4 | 7872 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7873 | { |
1ad00fd4 | 7874 | unsigned long v; |
7875 | switch (nc.to_unsigned_long(&v)) | |
7876 | { | |
7877 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7878 | break; |
1ad00fd4 | 7879 | case Numeric_constant::NC_UL_NOTINT: |
631d5788 | 7880 | go_error_at(e->location(), "non-integer %s argument to make", |
7881 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7882 | return false; |
7883 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 7884 | go_error_at(e->location(), "negative %s argument to make", |
7885 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7886 | return false; |
7887 | case Numeric_constant::NC_UL_BIG: | |
7888 | // We don't want to give a compile-time error for a 64-bit | |
7889 | // value on a 32-bit target. | |
1b10c5e7 | 7890 | break; |
1ad00fd4 | 7891 | } |
1b10c5e7 | 7892 | |
7893 | mpz_t val; | |
7894 | if (!nc.to_int(&val)) | |
7895 | go_unreachable(); | |
7896 | int bits = mpz_sizeinbase(val, 2); | |
7897 | mpz_clear(val); | |
7898 | Type* int_type = Type::lookup_integer_type("int"); | |
7899 | if (bits >= int_type->integer_type()->bits()) | |
7900 | { | |
631d5788 | 7901 | go_error_at(e->location(), "%s argument too large for make", |
7902 | is_length ? "len" : "cap"); | |
1b10c5e7 | 7903 | return false; |
7904 | } | |
7905 | ||
ccea2b36 | 7906 | *small = true; |
1b10c5e7 | 7907 | return true; |
a9182619 | 7908 | } |
7909 | ||
1ad00fd4 | 7910 | if (e->type()->integer_type() != NULL) |
ccea2b36 | 7911 | { |
7912 | int ebits = e->type()->integer_type()->bits(); | |
7913 | int intbits = Type::lookup_integer_type("int")->integer_type()->bits(); | |
7914 | ||
7915 | // We can treat ebits == intbits as small even for an unsigned | |
7916 | // integer type, because we will convert the value to int and | |
7917 | // then reject it in the runtime if it is negative. | |
7918 | *small = ebits <= intbits; | |
7919 | ||
7920 | return true; | |
7921 | } | |
1ad00fd4 | 7922 | |
631d5788 | 7923 | go_error_at(e->location(), "non-integer %s argument to make", |
7924 | is_length ? "len" : "cap"); | |
a9182619 | 7925 | return false; |
7926 | } | |
7927 | ||
e440a328 | 7928 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7929 | // the argument. We need to map complex64 to float32 and complex128 |
7930 | // to float64, so it has to be done by name. This returns NULL if it | |
7931 | // can't figure out the type. | |
e440a328 | 7932 | |
7933 | Type* | |
7934 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7935 | { | |
7936 | if (arg_type == NULL || arg_type->is_abstract()) | |
7937 | return NULL; | |
7938 | Named_type* nt = arg_type->named_type(); | |
7939 | if (nt == NULL) | |
7940 | return NULL; | |
7941 | while (nt->real_type()->named_type() != NULL) | |
7942 | nt = nt->real_type()->named_type(); | |
48080209 | 7943 | if (nt->name() == "complex64") |
e440a328 | 7944 | return Type::lookup_float_type("float32"); |
7945 | else if (nt->name() == "complex128") | |
7946 | return Type::lookup_float_type("float64"); | |
7947 | else | |
7948 | return NULL; | |
7949 | } | |
7950 | ||
48080209 | 7951 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7952 | // argments. Like real_imag_type, we have to map by name. |
7953 | ||
7954 | Type* | |
48080209 | 7955 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7956 | { |
7957 | if (arg_type == NULL || arg_type->is_abstract()) | |
7958 | return NULL; | |
7959 | Named_type* nt = arg_type->named_type(); | |
7960 | if (nt == NULL) | |
7961 | return NULL; | |
7962 | while (nt->real_type()->named_type() != NULL) | |
7963 | nt = nt->real_type()->named_type(); | |
48080209 | 7964 | if (nt->name() == "float32") |
e440a328 | 7965 | return Type::lookup_complex_type("complex64"); |
7966 | else if (nt->name() == "float64") | |
7967 | return Type::lookup_complex_type("complex128"); | |
7968 | else | |
7969 | return NULL; | |
7970 | } | |
7971 | ||
7972 | // Return a single argument, or NULL if there isn't one. | |
7973 | ||
7974 | Expression* | |
7975 | Builtin_call_expression::one_arg() const | |
7976 | { | |
7977 | const Expression_list* args = this->args(); | |
aa615cb3 | 7978 | if (args == NULL || args->size() != 1) |
e440a328 | 7979 | return NULL; |
7980 | return args->front(); | |
7981 | } | |
7982 | ||
83921647 | 7983 | // A traversal class which looks for a call or receive expression. |
7984 | ||
7985 | class Find_call_expression : public Traverse | |
7986 | { | |
7987 | public: | |
7988 | Find_call_expression() | |
7989 | : Traverse(traverse_expressions), | |
7990 | found_(false) | |
7991 | { } | |
7992 | ||
7993 | int | |
7994 | expression(Expression**); | |
7995 | ||
7996 | bool | |
7997 | found() | |
7998 | { return this->found_; } | |
7999 | ||
8000 | private: | |
8001 | bool found_; | |
8002 | }; | |
8003 | ||
8004 | int | |
8005 | Find_call_expression::expression(Expression** pexpr) | |
8006 | { | |
4714afb2 | 8007 | Expression* expr = *pexpr; |
8008 | if (!expr->is_constant() | |
8009 | && (expr->call_expression() != NULL | |
8010 | || expr->receive_expression() != NULL)) | |
83921647 | 8011 | { |
8012 | this->found_ = true; | |
8013 | return TRAVERSE_EXIT; | |
8014 | } | |
8015 | return TRAVERSE_CONTINUE; | |
8016 | } | |
8017 | ||
4714afb2 | 8018 | // Return whether calling len or cap on EXPR, of array type, is a |
8019 | // constant. The language spec says "the expressions len(s) and | |
8020 | // cap(s) are constants if the type of s is an array or pointer to an | |
8021 | // array and the expression s does not contain channel receives or | |
8022 | // (non-constant) function calls." | |
8023 | ||
8024 | bool | |
8025 | Builtin_call_expression::array_len_is_constant(Expression* expr) | |
8026 | { | |
8027 | go_assert(expr->type()->deref()->array_type() != NULL | |
8028 | && !expr->type()->deref()->is_slice_type()); | |
8029 | if (expr->is_constant()) | |
8030 | return true; | |
8031 | Find_call_expression find_call; | |
8032 | Expression::traverse(&expr, &find_call); | |
8033 | return !find_call.found(); | |
8034 | } | |
8035 | ||
83921647 | 8036 | // Return whether this is constant: len of a string constant, or len |
8037 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
8038 | // unsafe.Alignof. | |
e440a328 | 8039 | |
8040 | bool | |
8041 | Builtin_call_expression::do_is_constant() const | |
8042 | { | |
12e69faa | 8043 | if (this->is_error_expression()) |
8044 | return true; | |
e440a328 | 8045 | switch (this->code_) |
8046 | { | |
8047 | case BUILTIN_LEN: | |
8048 | case BUILTIN_CAP: | |
8049 | { | |
0f914071 | 8050 | if (this->seen_) |
8051 | return false; | |
8052 | ||
e440a328 | 8053 | Expression* arg = this->one_arg(); |
8054 | if (arg == NULL) | |
8055 | return false; | |
8056 | Type* arg_type = arg->type(); | |
8057 | ||
8058 | if (arg_type->points_to() != NULL | |
8059 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8060 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8061 | arg_type = arg_type->points_to(); |
8062 | ||
8063 | if (arg_type->array_type() != NULL | |
54934d77 | 8064 | && arg_type->array_type()->length() != NULL) |
8065 | { | |
8066 | this->seen_ = true; | |
8067 | bool ret = Builtin_call_expression::array_len_is_constant(arg); | |
8068 | this->seen_ = false; | |
8069 | return ret; | |
8070 | } | |
e440a328 | 8071 | |
8072 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 8073 | { |
8074 | this->seen_ = true; | |
8075 | bool ret = arg->is_constant(); | |
8076 | this->seen_ = false; | |
8077 | return ret; | |
8078 | } | |
e440a328 | 8079 | } |
8080 | break; | |
8081 | ||
8082 | case BUILTIN_SIZEOF: | |
8083 | case BUILTIN_ALIGNOF: | |
8084 | return this->one_arg() != NULL; | |
8085 | ||
8086 | case BUILTIN_OFFSETOF: | |
8087 | { | |
8088 | Expression* arg = this->one_arg(); | |
8089 | if (arg == NULL) | |
8090 | return false; | |
8091 | return arg->field_reference_expression() != NULL; | |
8092 | } | |
8093 | ||
48080209 | 8094 | case BUILTIN_COMPLEX: |
e440a328 | 8095 | { |
8096 | const Expression_list* args = this->args(); | |
8097 | if (args != NULL && args->size() == 2) | |
8098 | return args->front()->is_constant() && args->back()->is_constant(); | |
8099 | } | |
8100 | break; | |
8101 | ||
8102 | case BUILTIN_REAL: | |
8103 | case BUILTIN_IMAG: | |
8104 | { | |
8105 | Expression* arg = this->one_arg(); | |
8106 | return arg != NULL && arg->is_constant(); | |
8107 | } | |
8108 | ||
8109 | default: | |
8110 | break; | |
8111 | } | |
8112 | ||
8113 | return false; | |
8114 | } | |
8115 | ||
0c77715b | 8116 | // Return a numeric constant if possible. |
e440a328 | 8117 | |
8118 | bool | |
0c77715b | 8119 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 8120 | { |
8121 | if (this->code_ == BUILTIN_LEN | |
8122 | || this->code_ == BUILTIN_CAP) | |
8123 | { | |
8124 | Expression* arg = this->one_arg(); | |
8125 | if (arg == NULL) | |
8126 | return false; | |
8127 | Type* arg_type = arg->type(); | |
8128 | ||
8129 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
8130 | { | |
8131 | std::string sval; | |
8132 | if (arg->string_constant_value(&sval)) | |
8133 | { | |
0c77715b | 8134 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
8135 | sval.length()); | |
e440a328 | 8136 | return true; |
8137 | } | |
8138 | } | |
8139 | ||
8140 | if (arg_type->points_to() != NULL | |
8141 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8142 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8143 | arg_type = arg_type->points_to(); |
8144 | ||
8145 | if (arg_type->array_type() != NULL | |
8146 | && arg_type->array_type()->length() != NULL) | |
8147 | { | |
0f914071 | 8148 | if (this->seen_) |
8149 | return false; | |
e440a328 | 8150 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 8151 | this->seen_ = true; |
0c77715b | 8152 | bool r = e->numeric_constant_value(nc); |
0f914071 | 8153 | this->seen_ = false; |
8154 | if (r) | |
e440a328 | 8155 | { |
0c77715b | 8156 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
8157 | this->location())) | |
8158 | r = false; | |
e440a328 | 8159 | } |
0c77715b | 8160 | return r; |
e440a328 | 8161 | } |
8162 | } | |
8163 | else if (this->code_ == BUILTIN_SIZEOF | |
8164 | || this->code_ == BUILTIN_ALIGNOF) | |
8165 | { | |
8166 | Expression* arg = this->one_arg(); | |
8167 | if (arg == NULL) | |
8168 | return false; | |
8169 | Type* arg_type = arg->type(); | |
5c13bd80 | 8170 | if (arg_type->is_error()) |
e440a328 | 8171 | return false; |
8172 | if (arg_type->is_abstract()) | |
8173 | return false; | |
2c809f8f | 8174 | if (this->seen_) |
8175 | return false; | |
927a01eb | 8176 | |
3f378015 | 8177 | int64_t ret; |
e440a328 | 8178 | if (this->code_ == BUILTIN_SIZEOF) |
8179 | { | |
2c809f8f | 8180 | this->seen_ = true; |
8181 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
8182 | this->seen_ = false; | |
8183 | if (!ok) | |
e440a328 | 8184 | return false; |
8185 | } | |
8186 | else if (this->code_ == BUILTIN_ALIGNOF) | |
8187 | { | |
2c809f8f | 8188 | bool ok; |
8189 | this->seen_ = true; | |
637bd3af | 8190 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 8191 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 8192 | else |
e440a328 | 8193 | { |
8194 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
8195 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 8196 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 8197 | } |
2c809f8f | 8198 | this->seen_ = false; |
8199 | if (!ok) | |
8200 | return false; | |
e440a328 | 8201 | } |
8202 | else | |
c3e6f413 | 8203 | go_unreachable(); |
927a01eb | 8204 | |
3f378015 | 8205 | mpz_t zval; |
8206 | set_mpz_from_int64(&zval, ret); | |
8207 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8208 | mpz_clear(zval); | |
e440a328 | 8209 | return true; |
8210 | } | |
8211 | else if (this->code_ == BUILTIN_OFFSETOF) | |
8212 | { | |
8213 | Expression* arg = this->one_arg(); | |
8214 | if (arg == NULL) | |
8215 | return false; | |
8216 | Field_reference_expression* farg = arg->field_reference_expression(); | |
8217 | if (farg == NULL) | |
8218 | return false; | |
2c809f8f | 8219 | if (this->seen_) |
8220 | return false; | |
8221 | ||
3f378015 | 8222 | int64_t total_offset = 0; |
9a4bd570 | 8223 | while (true) |
8224 | { | |
8225 | Expression* struct_expr = farg->expr(); | |
8226 | Type* st = struct_expr->type(); | |
8227 | if (st->struct_type() == NULL) | |
8228 | return false; | |
8229 | if (st->named_type() != NULL) | |
8230 | st->named_type()->convert(this->gogo_); | |
fbabafbd | 8231 | if (st->is_error_type()) |
8232 | { | |
8233 | go_assert(saw_errors()); | |
8234 | return false; | |
8235 | } | |
3f378015 | 8236 | int64_t offset; |
2c809f8f | 8237 | this->seen_ = true; |
8238 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
8239 | farg->field_index(), | |
8240 | &offset); | |
8241 | this->seen_ = false; | |
8242 | if (!ok) | |
8243 | return false; | |
9a4bd570 | 8244 | total_offset += offset; |
8245 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
8246 | { | |
8247 | // Go up until we reach the original base. | |
8248 | farg = struct_expr->field_reference_expression(); | |
8249 | continue; | |
8250 | } | |
8251 | break; | |
8252 | } | |
3f378015 | 8253 | mpz_t zval; |
8254 | set_mpz_from_int64(&zval, total_offset); | |
8255 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8256 | mpz_clear(zval); | |
e440a328 | 8257 | return true; |
8258 | } | |
0c77715b | 8259 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 8260 | { |
8261 | Expression* arg = this->one_arg(); | |
8262 | if (arg == NULL) | |
8263 | return false; | |
8264 | ||
0c77715b | 8265 | Numeric_constant argnc; |
8266 | if (!arg->numeric_constant_value(&argnc)) | |
8267 | return false; | |
8268 | ||
fcbea5e4 | 8269 | mpc_t val; |
8270 | if (!argnc.to_complex(&val)) | |
0c77715b | 8271 | return false; |
e440a328 | 8272 | |
0c77715b | 8273 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
8274 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 8275 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 8276 | else |
fcbea5e4 | 8277 | nc->set_float(type, mpc_imagref(val)); |
8278 | mpc_clear(val); | |
0c77715b | 8279 | return true; |
e440a328 | 8280 | } |
0c77715b | 8281 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 8282 | { |
8283 | const Expression_list* args = this->args(); | |
8284 | if (args == NULL || args->size() != 2) | |
8285 | return false; | |
8286 | ||
0c77715b | 8287 | Numeric_constant rnc; |
8288 | if (!args->front()->numeric_constant_value(&rnc)) | |
8289 | return false; | |
8290 | Numeric_constant inc; | |
8291 | if (!args->back()->numeric_constant_value(&inc)) | |
8292 | return false; | |
8293 | ||
8294 | if (rnc.type() != NULL | |
8295 | && !rnc.type()->is_abstract() | |
8296 | && inc.type() != NULL | |
8297 | && !inc.type()->is_abstract() | |
8298 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
8299 | return false; | |
8300 | ||
e440a328 | 8301 | mpfr_t r; |
0c77715b | 8302 | if (!rnc.to_float(&r)) |
8303 | return false; | |
8304 | mpfr_t i; | |
8305 | if (!inc.to_float(&i)) | |
e440a328 | 8306 | { |
8307 | mpfr_clear(r); | |
8308 | return false; | |
8309 | } | |
8310 | ||
0c77715b | 8311 | Type* arg_type = rnc.type(); |
8312 | if (arg_type == NULL || arg_type->is_abstract()) | |
8313 | arg_type = inc.type(); | |
e440a328 | 8314 | |
fcbea5e4 | 8315 | mpc_t val; |
8316 | mpc_init2(val, mpc_precision); | |
8317 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 8318 | mpfr_clear(r); |
8319 | mpfr_clear(i); | |
8320 | ||
fcbea5e4 | 8321 | Type* type = Builtin_call_expression::complex_type(arg_type); |
8322 | nc->set_complex(type, val); | |
8323 | ||
8324 | mpc_clear(val); | |
8325 | ||
0c77715b | 8326 | return true; |
e440a328 | 8327 | } |
8328 | ||
8329 | return false; | |
8330 | } | |
8331 | ||
a7549a6a | 8332 | // Give an error if we are discarding the value of an expression which |
8333 | // should not normally be discarded. We don't give an error for | |
8334 | // discarding the value of an ordinary function call, but we do for | |
8335 | // builtin functions, purely for consistency with the gc compiler. | |
8336 | ||
4f2138d7 | 8337 | bool |
a7549a6a | 8338 | Builtin_call_expression::do_discarding_value() |
8339 | { | |
8340 | switch (this->code_) | |
8341 | { | |
8342 | case BUILTIN_INVALID: | |
8343 | default: | |
8344 | go_unreachable(); | |
8345 | ||
8346 | case BUILTIN_APPEND: | |
8347 | case BUILTIN_CAP: | |
8348 | case BUILTIN_COMPLEX: | |
8349 | case BUILTIN_IMAG: | |
8350 | case BUILTIN_LEN: | |
8351 | case BUILTIN_MAKE: | |
8352 | case BUILTIN_NEW: | |
8353 | case BUILTIN_REAL: | |
8354 | case BUILTIN_ALIGNOF: | |
8355 | case BUILTIN_OFFSETOF: | |
8356 | case BUILTIN_SIZEOF: | |
8357 | this->unused_value_error(); | |
4f2138d7 | 8358 | return false; |
a7549a6a | 8359 | |
8360 | case BUILTIN_CLOSE: | |
8361 | case BUILTIN_COPY: | |
1cce762f | 8362 | case BUILTIN_DELETE: |
a7549a6a | 8363 | case BUILTIN_PANIC: |
8364 | case BUILTIN_PRINT: | |
8365 | case BUILTIN_PRINTLN: | |
8366 | case BUILTIN_RECOVER: | |
4f2138d7 | 8367 | return true; |
a7549a6a | 8368 | } |
8369 | } | |
8370 | ||
e440a328 | 8371 | // Return the type. |
8372 | ||
8373 | Type* | |
8374 | Builtin_call_expression::do_type() | |
8375 | { | |
79651b1f | 8376 | if (this->is_error_expression()) |
8377 | return Type::make_error_type(); | |
e440a328 | 8378 | switch (this->code_) |
8379 | { | |
8380 | case BUILTIN_INVALID: | |
8381 | default: | |
79651b1f | 8382 | return Type::make_error_type(); |
e440a328 | 8383 | |
8384 | case BUILTIN_NEW: | |
e440a328 | 8385 | { |
8386 | const Expression_list* args = this->args(); | |
8387 | if (args == NULL || args->empty()) | |
8388 | return Type::make_error_type(); | |
8389 | return Type::make_pointer_type(args->front()->type()); | |
8390 | } | |
8391 | ||
a065edc5 | 8392 | case BUILTIN_MAKE: |
8393 | { | |
8394 | const Expression_list* args = this->args(); | |
8395 | if (args == NULL || args->empty()) | |
8396 | return Type::make_error_type(); | |
8397 | return args->front()->type(); | |
8398 | } | |
8399 | ||
e440a328 | 8400 | case BUILTIN_CAP: |
8401 | case BUILTIN_COPY: | |
8402 | case BUILTIN_LEN: | |
7ba86326 | 8403 | return Type::lookup_integer_type("int"); |
8404 | ||
e440a328 | 8405 | case BUILTIN_ALIGNOF: |
8406 | case BUILTIN_OFFSETOF: | |
8407 | case BUILTIN_SIZEOF: | |
7ba86326 | 8408 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 8409 | |
8410 | case BUILTIN_CLOSE: | |
1cce762f | 8411 | case BUILTIN_DELETE: |
e440a328 | 8412 | case BUILTIN_PANIC: |
8413 | case BUILTIN_PRINT: | |
8414 | case BUILTIN_PRINTLN: | |
8415 | return Type::make_void_type(); | |
8416 | ||
e440a328 | 8417 | case BUILTIN_RECOVER: |
823c7e3d | 8418 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8419 | |
8420 | case BUILTIN_APPEND: | |
8421 | { | |
8422 | const Expression_list* args = this->args(); | |
8423 | if (args == NULL || args->empty()) | |
8424 | return Type::make_error_type(); | |
3ff4863b | 8425 | Type *ret = args->front()->type(); |
8426 | if (!ret->is_slice_type()) | |
8427 | return Type::make_error_type(); | |
8428 | return ret; | |
e440a328 | 8429 | } |
8430 | ||
8431 | case BUILTIN_REAL: | |
8432 | case BUILTIN_IMAG: | |
8433 | { | |
8434 | Expression* arg = this->one_arg(); | |
8435 | if (arg == NULL) | |
8436 | return Type::make_error_type(); | |
8437 | Type* t = arg->type(); | |
8438 | if (t->is_abstract()) | |
8439 | t = t->make_non_abstract_type(); | |
8440 | t = Builtin_call_expression::real_imag_type(t); | |
8441 | if (t == NULL) | |
8442 | t = Type::make_error_type(); | |
8443 | return t; | |
8444 | } | |
8445 | ||
48080209 | 8446 | case BUILTIN_COMPLEX: |
e440a328 | 8447 | { |
8448 | const Expression_list* args = this->args(); | |
8449 | if (args == NULL || args->size() != 2) | |
8450 | return Type::make_error_type(); | |
8451 | Type* t = args->front()->type(); | |
8452 | if (t->is_abstract()) | |
8453 | { | |
8454 | t = args->back()->type(); | |
8455 | if (t->is_abstract()) | |
8456 | t = t->make_non_abstract_type(); | |
8457 | } | |
48080209 | 8458 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 8459 | if (t == NULL) |
8460 | t = Type::make_error_type(); | |
8461 | return t; | |
8462 | } | |
8463 | } | |
8464 | } | |
8465 | ||
8466 | // Determine the type. | |
8467 | ||
8468 | void | |
8469 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
8470 | { | |
fb94b0ca | 8471 | if (!this->determining_types()) |
8472 | return; | |
8473 | ||
e440a328 | 8474 | this->fn()->determine_type_no_context(); |
8475 | ||
8476 | const Expression_list* args = this->args(); | |
8477 | ||
8478 | bool is_print; | |
8479 | Type* arg_type = NULL; | |
321e5ad2 | 8480 | Type* trailing_arg_types = NULL; |
e440a328 | 8481 | switch (this->code_) |
8482 | { | |
8483 | case BUILTIN_PRINT: | |
8484 | case BUILTIN_PRINTLN: | |
8485 | // Do not force a large integer constant to "int". | |
8486 | is_print = true; | |
8487 | break; | |
8488 | ||
8489 | case BUILTIN_REAL: | |
8490 | case BUILTIN_IMAG: | |
48080209 | 8491 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8492 | if (arg_type == NULL) |
8493 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8494 | is_print = false; |
8495 | break; | |
8496 | ||
48080209 | 8497 | case BUILTIN_COMPLEX: |
e440a328 | 8498 | { |
48080209 | 8499 | // For the complex function the type of one operand can |
e440a328 | 8500 | // determine the type of the other, as in a binary expression. |
8501 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8502 | if (arg_type == NULL) |
8503 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8504 | if (args != NULL && args->size() == 2) |
8505 | { | |
8506 | Type* t1 = args->front()->type(); | |
c849bb59 | 8507 | Type* t2 = args->back()->type(); |
e440a328 | 8508 | if (!t1->is_abstract()) |
8509 | arg_type = t1; | |
8510 | else if (!t2->is_abstract()) | |
8511 | arg_type = t2; | |
8512 | } | |
8513 | is_print = false; | |
8514 | } | |
8515 | break; | |
8516 | ||
321e5ad2 | 8517 | case BUILTIN_APPEND: |
8518 | if (!this->is_varargs() | |
8519 | && args != NULL | |
8520 | && !args->empty() | |
8521 | && args->front()->type()->is_slice_type()) | |
8522 | trailing_arg_types = | |
8523 | args->front()->type()->array_type()->element_type(); | |
8524 | is_print = false; | |
8525 | break; | |
8526 | ||
e440a328 | 8527 | default: |
8528 | is_print = false; | |
8529 | break; | |
8530 | } | |
8531 | ||
8532 | if (args != NULL) | |
8533 | { | |
8534 | for (Expression_list::const_iterator pa = args->begin(); | |
8535 | pa != args->end(); | |
8536 | ++pa) | |
8537 | { | |
8538 | Type_context subcontext; | |
8539 | subcontext.type = arg_type; | |
8540 | ||
8541 | if (is_print) | |
8542 | { | |
8543 | // We want to print large constants, we so can't just | |
8544 | // use the appropriate nonabstract type. Use uint64 for | |
8545 | // an integer if we know it is nonnegative, otherwise | |
8546 | // use int64 for a integer, otherwise use float64 for a | |
8547 | // float or complex128 for a complex. | |
8548 | Type* want_type = NULL; | |
8549 | Type* atype = (*pa)->type(); | |
8550 | if (atype->is_abstract()) | |
8551 | { | |
8552 | if (atype->integer_type() != NULL) | |
8553 | { | |
0c77715b | 8554 | Numeric_constant nc; |
8555 | if (this->numeric_constant_value(&nc)) | |
8556 | { | |
8557 | mpz_t val; | |
8558 | if (nc.to_int(&val)) | |
8559 | { | |
8560 | if (mpz_sgn(val) >= 0) | |
8561 | want_type = Type::lookup_integer_type("uint64"); | |
8562 | mpz_clear(val); | |
8563 | } | |
8564 | } | |
8565 | if (want_type == NULL) | |
e440a328 | 8566 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8567 | } |
8568 | else if (atype->float_type() != NULL) | |
8569 | want_type = Type::lookup_float_type("float64"); | |
8570 | else if (atype->complex_type() != NULL) | |
8571 | want_type = Type::lookup_complex_type("complex128"); | |
8572 | else if (atype->is_abstract_string_type()) | |
8573 | want_type = Type::lookup_string_type(); | |
8574 | else if (atype->is_abstract_boolean_type()) | |
8575 | want_type = Type::lookup_bool_type(); | |
8576 | else | |
c3e6f413 | 8577 | go_unreachable(); |
e440a328 | 8578 | subcontext.type = want_type; |
8579 | } | |
8580 | } | |
8581 | ||
8582 | (*pa)->determine_type(&subcontext); | |
321e5ad2 | 8583 | |
8584 | if (trailing_arg_types != NULL) | |
8585 | { | |
8586 | arg_type = trailing_arg_types; | |
8587 | trailing_arg_types = NULL; | |
8588 | } | |
e440a328 | 8589 | } |
8590 | } | |
8591 | } | |
8592 | ||
8593 | // If there is exactly one argument, return true. Otherwise give an | |
8594 | // error message and return false. | |
8595 | ||
8596 | bool | |
8597 | Builtin_call_expression::check_one_arg() | |
8598 | { | |
8599 | const Expression_list* args = this->args(); | |
8600 | if (args == NULL || args->size() < 1) | |
8601 | { | |
8602 | this->report_error(_("not enough arguments")); | |
8603 | return false; | |
8604 | } | |
8605 | else if (args->size() > 1) | |
8606 | { | |
8607 | this->report_error(_("too many arguments")); | |
8608 | return false; | |
8609 | } | |
8610 | if (args->front()->is_error_expression() | |
5c13bd80 | 8611 | || args->front()->type()->is_error()) |
e440a328 | 8612 | { |
8613 | this->set_is_error(); | |
8614 | return false; | |
8615 | } | |
8616 | return true; | |
8617 | } | |
8618 | ||
8619 | // Check argument types for a builtin function. | |
8620 | ||
8621 | void | |
8622 | Builtin_call_expression::do_check_types(Gogo*) | |
8623 | { | |
375646ea | 8624 | if (this->is_error_expression()) |
8625 | return; | |
e440a328 | 8626 | switch (this->code_) |
8627 | { | |
8628 | case BUILTIN_INVALID: | |
8629 | case BUILTIN_NEW: | |
8630 | case BUILTIN_MAKE: | |
cd238b8d | 8631 | case BUILTIN_DELETE: |
e440a328 | 8632 | return; |
8633 | ||
8634 | case BUILTIN_LEN: | |
8635 | case BUILTIN_CAP: | |
8636 | { | |
8637 | // The single argument may be either a string or an array or a | |
8638 | // map or a channel, or a pointer to a closed array. | |
8639 | if (this->check_one_arg()) | |
8640 | { | |
8641 | Type* arg_type = this->one_arg()->type(); | |
8642 | if (arg_type->points_to() != NULL | |
8643 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8644 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8645 | arg_type = arg_type->points_to(); |
8646 | if (this->code_ == BUILTIN_CAP) | |
8647 | { | |
5c13bd80 | 8648 | if (!arg_type->is_error() |
e440a328 | 8649 | && arg_type->array_type() == NULL |
8650 | && arg_type->channel_type() == NULL) | |
8651 | this->report_error(_("argument must be array or slice " | |
8652 | "or channel")); | |
8653 | } | |
8654 | else | |
8655 | { | |
5c13bd80 | 8656 | if (!arg_type->is_error() |
e440a328 | 8657 | && !arg_type->is_string_type() |
8658 | && arg_type->array_type() == NULL | |
8659 | && arg_type->map_type() == NULL | |
8660 | && arg_type->channel_type() == NULL) | |
8661 | this->report_error(_("argument must be string or " | |
8662 | "array or slice or map or channel")); | |
8663 | } | |
8664 | } | |
8665 | } | |
8666 | break; | |
8667 | ||
8668 | case BUILTIN_PRINT: | |
8669 | case BUILTIN_PRINTLN: | |
8670 | { | |
8671 | const Expression_list* args = this->args(); | |
8672 | if (args == NULL) | |
8673 | { | |
8674 | if (this->code_ == BUILTIN_PRINT) | |
631d5788 | 8675 | go_warning_at(this->location(), 0, |
e440a328 | 8676 | "no arguments for builtin function %<%s%>", |
8677 | (this->code_ == BUILTIN_PRINT | |
8678 | ? "print" | |
8679 | : "println")); | |
8680 | } | |
8681 | else | |
8682 | { | |
8683 | for (Expression_list::const_iterator p = args->begin(); | |
8684 | p != args->end(); | |
8685 | ++p) | |
8686 | { | |
8687 | Type* type = (*p)->type(); | |
5c13bd80 | 8688 | if (type->is_error() |
e440a328 | 8689 | || type->is_string_type() |
8690 | || type->integer_type() != NULL | |
8691 | || type->float_type() != NULL | |
8692 | || type->complex_type() != NULL | |
8693 | || type->is_boolean_type() | |
8694 | || type->points_to() != NULL | |
8695 | || type->interface_type() != NULL | |
8696 | || type->channel_type() != NULL | |
8697 | || type->map_type() != NULL | |
8698 | || type->function_type() != NULL | |
411eb89e | 8699 | || type->is_slice_type()) |
e440a328 | 8700 | ; |
acf8e158 | 8701 | else if ((*p)->is_type_expression()) |
8702 | { | |
8703 | // If this is a type expression it's going to give | |
8704 | // an error anyhow, so we don't need one here. | |
8705 | } | |
e440a328 | 8706 | else |
8707 | this->report_error(_("unsupported argument type to " | |
8708 | "builtin function")); | |
8709 | } | |
8710 | } | |
8711 | } | |
8712 | break; | |
8713 | ||
8714 | case BUILTIN_CLOSE: | |
e440a328 | 8715 | if (this->check_one_arg()) |
8716 | { | |
8717 | if (this->one_arg()->type()->channel_type() == NULL) | |
8718 | this->report_error(_("argument must be channel")); | |
5202d986 | 8719 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8720 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8721 | } |
8722 | break; | |
8723 | ||
8724 | case BUILTIN_PANIC: | |
8725 | case BUILTIN_SIZEOF: | |
8726 | case BUILTIN_ALIGNOF: | |
8727 | this->check_one_arg(); | |
8728 | break; | |
8729 | ||
8730 | case BUILTIN_RECOVER: | |
6334270b | 8731 | if (this->args() != NULL |
8732 | && !this->args()->empty() | |
8733 | && !this->recover_arg_is_set_) | |
e440a328 | 8734 | this->report_error(_("too many arguments")); |
8735 | break; | |
8736 | ||
8737 | case BUILTIN_OFFSETOF: | |
8738 | if (this->check_one_arg()) | |
8739 | { | |
8740 | Expression* arg = this->one_arg(); | |
8741 | if (arg->field_reference_expression() == NULL) | |
8742 | this->report_error(_("argument must be a field reference")); | |
8743 | } | |
8744 | break; | |
8745 | ||
8746 | case BUILTIN_COPY: | |
8747 | { | |
8748 | const Expression_list* args = this->args(); | |
8749 | if (args == NULL || args->size() < 2) | |
8750 | { | |
8751 | this->report_error(_("not enough arguments")); | |
8752 | break; | |
8753 | } | |
8754 | else if (args->size() > 2) | |
8755 | { | |
8756 | this->report_error(_("too many arguments")); | |
8757 | break; | |
8758 | } | |
8759 | Type* arg1_type = args->front()->type(); | |
8760 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8761 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 8762 | { |
8763 | this->set_is_error(); | |
8764 | break; | |
8765 | } | |
e440a328 | 8766 | |
8767 | Type* e1; | |
411eb89e | 8768 | if (arg1_type->is_slice_type()) |
e440a328 | 8769 | e1 = arg1_type->array_type()->element_type(); |
8770 | else | |
8771 | { | |
8772 | this->report_error(_("left argument must be a slice")); | |
8773 | break; | |
8774 | } | |
8775 | ||
411eb89e | 8776 | if (arg2_type->is_slice_type()) |
60963afd | 8777 | { |
8778 | Type* e2 = arg2_type->array_type()->element_type(); | |
8779 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8780 | this->report_error(_("element types must be the same")); | |
8781 | } | |
e440a328 | 8782 | else if (arg2_type->is_string_type()) |
e440a328 | 8783 | { |
60963afd | 8784 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8785 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8786 | } |
60963afd | 8787 | else |
8788 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8789 | } |
8790 | break; | |
8791 | ||
8792 | case BUILTIN_APPEND: | |
8793 | { | |
8794 | const Expression_list* args = this->args(); | |
321e5ad2 | 8795 | if (args == NULL || args->empty()) |
e440a328 | 8796 | { |
8797 | this->report_error(_("not enough arguments")); | |
8798 | break; | |
8799 | } | |
321e5ad2 | 8800 | |
8801 | Type* slice_type = args->front()->type(); | |
8802 | if (!slice_type->is_slice_type()) | |
6bebb39d | 8803 | { |
321e5ad2 | 8804 | if (slice_type->is_error_type()) |
8805 | break; | |
8806 | if (slice_type->is_nil_type()) | |
8807 | go_error_at(args->front()->location(), "use of untyped nil"); | |
8808 | else | |
8809 | go_error_at(args->front()->location(), | |
8810 | "argument 1 must be a slice"); | |
6bebb39d | 8811 | this->set_is_error(); |
8812 | break; | |
8813 | } | |
cd238b8d | 8814 | |
321e5ad2 | 8815 | Type* element_type = slice_type->array_type()->element_type(); |
22deed0d | 8816 | if (!element_type->in_heap()) |
8817 | go_error_at(args->front()->location(), | |
8818 | "can't append to slice of go:notinheap type"); | |
321e5ad2 | 8819 | if (this->is_varargs()) |
4fd4fcf4 | 8820 | { |
321e5ad2 | 8821 | if (!args->back()->type()->is_slice_type() |
8822 | && !args->back()->type()->is_string_type()) | |
8823 | { | |
8824 | go_error_at(args->back()->location(), | |
8825 | "invalid use of %<...%> with non-slice/non-string"); | |
8826 | this->set_is_error(); | |
8827 | break; | |
8828 | } | |
4fd4fcf4 | 8829 | |
321e5ad2 | 8830 | if (args->size() < 2) |
8831 | { | |
8832 | this->report_error(_("not enough arguments")); | |
8833 | break; | |
8834 | } | |
8835 | if (args->size() > 2) | |
8836 | { | |
8837 | this->report_error(_("too many arguments")); | |
8838 | break; | |
8839 | } | |
8840 | ||
8841 | if (args->back()->type()->is_string_type() | |
8842 | && element_type->integer_type() != NULL | |
8843 | && element_type->integer_type()->is_byte()) | |
8844 | { | |
8845 | // Permit append(s1, s2...) when s1 is a slice of | |
8846 | // bytes and s2 is a string type. | |
8847 | } | |
e440a328 | 8848 | else |
8849 | { | |
321e5ad2 | 8850 | // We have to test for assignment compatibility to a |
8851 | // slice of the element type, which is not necessarily | |
8852 | // the same as the type of the first argument: the | |
8853 | // first argument might have a named type. | |
8854 | Type* check_type = Type::make_array_type(element_type, NULL); | |
8855 | std::string reason; | |
8856 | if (!Type::are_assignable(check_type, args->back()->type(), | |
8857 | &reason)) | |
8858 | { | |
8859 | if (reason.empty()) | |
8860 | go_error_at(args->back()->location(), | |
8861 | "argument 2 has invalid type"); | |
8862 | else | |
8863 | go_error_at(args->back()->location(), | |
8864 | "argument 2 has invalid type (%s)", | |
8865 | reason.c_str()); | |
8866 | this->set_is_error(); | |
8867 | break; | |
8868 | } | |
8869 | } | |
8870 | } | |
8871 | else | |
8872 | { | |
8873 | Expression_list::const_iterator pa = args->begin(); | |
8874 | int i = 2; | |
8875 | for (++pa; pa != args->end(); ++pa, ++i) | |
8876 | { | |
8877 | std::string reason; | |
8878 | if (!Type::are_assignable(element_type, (*pa)->type(), | |
8879 | &reason)) | |
8880 | { | |
8881 | if (reason.empty()) | |
8882 | go_error_at((*pa)->location(), | |
8883 | "argument %d has incompatible type", i); | |
8884 | else | |
8885 | go_error_at((*pa)->location(), | |
8886 | "argument %d has incompatible type (%s)", | |
8887 | i, reason.c_str()); | |
8888 | this->set_is_error(); | |
8889 | } | |
e440a328 | 8890 | } |
8891 | } | |
e440a328 | 8892 | } |
321e5ad2 | 8893 | break; |
e440a328 | 8894 | |
8895 | case BUILTIN_REAL: | |
8896 | case BUILTIN_IMAG: | |
8897 | if (this->check_one_arg()) | |
8898 | { | |
8899 | if (this->one_arg()->type()->complex_type() == NULL) | |
8900 | this->report_error(_("argument must have complex type")); | |
8901 | } | |
8902 | break; | |
8903 | ||
48080209 | 8904 | case BUILTIN_COMPLEX: |
e440a328 | 8905 | { |
8906 | const Expression_list* args = this->args(); | |
8907 | if (args == NULL || args->size() < 2) | |
8908 | this->report_error(_("not enough arguments")); | |
8909 | else if (args->size() > 2) | |
8910 | this->report_error(_("too many arguments")); | |
8911 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8912 | || args->front()->type()->is_error() |
e440a328 | 8913 | || args->back()->is_error_expression() |
5c13bd80 | 8914 | || args->back()->type()->is_error()) |
e440a328 | 8915 | this->set_is_error(); |
8916 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8917 | args->back()->type(), true, NULL)) |
48080209 | 8918 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8919 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8920 | this->report_error(_("complex arguments must have " |
e440a328 | 8921 | "floating-point type")); |
8922 | } | |
8923 | break; | |
8924 | ||
8925 | default: | |
c3e6f413 | 8926 | go_unreachable(); |
e440a328 | 8927 | } |
8928 | } | |
8929 | ||
72666aed | 8930 | Expression* |
8931 | Builtin_call_expression::do_copy() | |
8932 | { | |
8933 | Call_expression* bce = | |
8934 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8935 | (this->args() == NULL |
8936 | ? NULL | |
8937 | : this->args()->copy()), | |
72666aed | 8938 | this->is_varargs(), |
8939 | this->location()); | |
8940 | ||
8941 | if (this->varargs_are_lowered()) | |
8942 | bce->set_varargs_are_lowered(); | |
8943 | return bce; | |
8944 | } | |
8945 | ||
ea664253 | 8946 | // Return the backend representation for a builtin function. |
e440a328 | 8947 | |
ea664253 | 8948 | Bexpression* |
8949 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8950 | { |
8951 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8952 | Location location = this->location(); |
a0d8874e | 8953 | |
8954 | if (this->is_erroneous_call()) | |
8955 | { | |
8956 | go_assert(saw_errors()); | |
8957 | return gogo->backend()->error_expression(); | |
8958 | } | |
8959 | ||
e440a328 | 8960 | switch (this->code_) |
8961 | { | |
8962 | case BUILTIN_INVALID: | |
8963 | case BUILTIN_NEW: | |
8964 | case BUILTIN_MAKE: | |
c3e6f413 | 8965 | go_unreachable(); |
e440a328 | 8966 | |
8967 | case BUILTIN_LEN: | |
8968 | case BUILTIN_CAP: | |
8969 | { | |
8970 | const Expression_list* args = this->args(); | |
c484d925 | 8971 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8972 | Expression* arg = args->front(); |
e440a328 | 8973 | Type* arg_type = arg->type(); |
0f914071 | 8974 | |
8975 | if (this->seen_) | |
8976 | { | |
c484d925 | 8977 | go_assert(saw_errors()); |
ea664253 | 8978 | return context->backend()->error_expression(); |
0f914071 | 8979 | } |
8980 | this->seen_ = true; | |
0f914071 | 8981 | this->seen_ = false; |
e440a328 | 8982 | if (arg_type->points_to() != NULL) |
8983 | { | |
8984 | arg_type = arg_type->points_to(); | |
c484d925 | 8985 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8986 | && !arg_type->is_slice_type()); |
f614ea8b | 8987 | arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT, |
8988 | location); | |
e440a328 | 8989 | } |
8990 | ||
1b1f2abf | 8991 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8992 | Expression* val; |
e440a328 | 8993 | if (this->code_ == BUILTIN_LEN) |
8994 | { | |
8995 | if (arg_type->is_string_type()) | |
2c809f8f | 8996 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8997 | location); | |
e440a328 | 8998 | else if (arg_type->array_type() != NULL) |
0f914071 | 8999 | { |
9000 | if (this->seen_) | |
9001 | { | |
c484d925 | 9002 | go_assert(saw_errors()); |
ea664253 | 9003 | return context->backend()->error_expression(); |
0f914071 | 9004 | } |
9005 | this->seen_ = true; | |
2c809f8f | 9006 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 9007 | this->seen_ = false; |
9008 | } | |
0d5530d9 | 9009 | else if (arg_type->map_type() != NULL |
9010 | || arg_type->channel_type() != NULL) | |
9011 | { | |
9012 | // The first field is the length. If the pointer is | |
9013 | // nil, the length is zero. | |
9014 | Type* pint_type = Type::make_pointer_type(int_type); | |
9015 | arg = Expression::make_unsafe_cast(pint_type, arg, location); | |
9016 | Expression* nil = Expression::make_nil(location); | |
9017 | nil = Expression::make_cast(pint_type, nil, location); | |
9018 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
9019 | arg, nil, location); | |
9020 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
9021 | location); | |
f614ea8b | 9022 | Expression* indir = |
9023 | Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, | |
9024 | location); | |
0d5530d9 | 9025 | val = Expression::make_conditional(cmp, zero, indir, location); |
9026 | } | |
e440a328 | 9027 | else |
c3e6f413 | 9028 | go_unreachable(); |
e440a328 | 9029 | } |
9030 | else | |
9031 | { | |
9032 | if (arg_type->array_type() != NULL) | |
0f914071 | 9033 | { |
9034 | if (this->seen_) | |
9035 | { | |
c484d925 | 9036 | go_assert(saw_errors()); |
ea664253 | 9037 | return context->backend()->error_expression(); |
0f914071 | 9038 | } |
9039 | this->seen_ = true; | |
2c809f8f | 9040 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 9041 | this->seen_ = false; |
9042 | } | |
e440a328 | 9043 | else if (arg_type->channel_type() != NULL) |
132ed071 | 9044 | { |
9045 | // The second field is the capacity. If the pointer | |
9046 | // is nil, the capacity is zero. | |
9047 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9048 | Type* pint_type = Type::make_pointer_type(int_type); | |
9049 | Expression* parg = Expression::make_unsafe_cast(uintptr_type, | |
9050 | arg, | |
9051 | location); | |
9052 | int off = int_type->integer_type()->bits() / 8; | |
9053 | Expression* eoff = Expression::make_integer_ul(off, | |
9054 | uintptr_type, | |
9055 | location); | |
9056 | parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, | |
9057 | location); | |
9058 | parg = Expression::make_unsafe_cast(pint_type, parg, location); | |
9059 | Expression* nil = Expression::make_nil(location); | |
9060 | nil = Expression::make_cast(pint_type, nil, location); | |
9061 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
9062 | arg, nil, location); | |
9063 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
9064 | location); | |
f614ea8b | 9065 | Expression* indir = |
9066 | Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED, | |
9067 | location); | |
132ed071 | 9068 | val = Expression::make_conditional(cmp, zero, indir, location); |
9069 | } | |
e440a328 | 9070 | else |
c3e6f413 | 9071 | go_unreachable(); |
e440a328 | 9072 | } |
9073 | ||
2c809f8f | 9074 | return Expression::make_cast(int_type, val, |
ea664253 | 9075 | location)->get_backend(context); |
e440a328 | 9076 | } |
9077 | ||
9078 | case BUILTIN_PRINT: | |
9079 | case BUILTIN_PRINTLN: | |
9080 | { | |
9081 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
88b03a70 | 9082 | |
9083 | Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, | |
9084 | location, 0); | |
e440a328 | 9085 | |
9086 | const Expression_list* call_args = this->args(); | |
9087 | if (call_args != NULL) | |
9088 | { | |
9089 | for (Expression_list::const_iterator p = call_args->begin(); | |
9090 | p != call_args->end(); | |
9091 | ++p) | |
9092 | { | |
9093 | if (is_ln && p != call_args->begin()) | |
9094 | { | |
2c809f8f | 9095 | Expression* print_space = |
88b03a70 | 9096 | Runtime::make_call(Runtime::PRINTSP, location, 0); |
e440a328 | 9097 | |
2c809f8f | 9098 | print_stmts = |
9099 | Expression::make_compound(print_stmts, print_space, | |
9100 | location); | |
9101 | } | |
e440a328 | 9102 | |
2c809f8f | 9103 | Expression* arg = *p; |
9104 | Type* type = arg->type(); | |
9105 | Runtime::Function code; | |
e440a328 | 9106 | if (type->is_string_type()) |
88b03a70 | 9107 | code = Runtime::PRINTSTRING; |
e440a328 | 9108 | else if (type->integer_type() != NULL |
9109 | && type->integer_type()->is_unsigned()) | |
9110 | { | |
e440a328 | 9111 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 9112 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9113 | code = Runtime::PRINTUINT; |
e440a328 | 9114 | } |
9115 | else if (type->integer_type() != NULL) | |
9116 | { | |
e440a328 | 9117 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 9118 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9119 | code = Runtime::PRINTINT; |
e440a328 | 9120 | } |
9121 | else if (type->float_type() != NULL) | |
9122 | { | |
2c809f8f | 9123 | Type* dtype = Type::lookup_float_type("float64"); |
9124 | arg = Expression::make_cast(dtype, arg, location); | |
88b03a70 | 9125 | code = Runtime::PRINTFLOAT; |
e440a328 | 9126 | } |
9127 | else if (type->complex_type() != NULL) | |
9128 | { | |
2c809f8f | 9129 | Type* ctype = Type::lookup_complex_type("complex128"); |
9130 | arg = Expression::make_cast(ctype, arg, location); | |
88b03a70 | 9131 | code = Runtime::PRINTCOMPLEX; |
e440a328 | 9132 | } |
9133 | else if (type->is_boolean_type()) | |
88b03a70 | 9134 | code = Runtime::PRINTBOOL; |
e440a328 | 9135 | else if (type->points_to() != NULL |
9136 | || type->channel_type() != NULL | |
9137 | || type->map_type() != NULL | |
9138 | || type->function_type() != NULL) | |
9139 | { | |
2c809f8f | 9140 | arg = Expression::make_cast(type, arg, location); |
88b03a70 | 9141 | code = Runtime::PRINTPOINTER; |
e440a328 | 9142 | } |
9143 | else if (type->interface_type() != NULL) | |
9144 | { | |
9145 | if (type->interface_type()->is_empty()) | |
88b03a70 | 9146 | code = Runtime::PRINTEFACE; |
e440a328 | 9147 | else |
88b03a70 | 9148 | code = Runtime::PRINTIFACE; |
e440a328 | 9149 | } |
411eb89e | 9150 | else if (type->is_slice_type()) |
88b03a70 | 9151 | code = Runtime::PRINTSLICE; |
e440a328 | 9152 | else |
cd238b8d | 9153 | { |
9154 | go_assert(saw_errors()); | |
ea664253 | 9155 | return context->backend()->error_expression(); |
cd238b8d | 9156 | } |
e440a328 | 9157 | |
2c809f8f | 9158 | Expression* call = Runtime::make_call(code, location, 1, arg); |
88b03a70 | 9159 | print_stmts = Expression::make_compound(print_stmts, call, |
9160 | location); | |
e440a328 | 9161 | } |
9162 | } | |
9163 | ||
9164 | if (is_ln) | |
9165 | { | |
2c809f8f | 9166 | Expression* print_nl = |
88b03a70 | 9167 | Runtime::make_call(Runtime::PRINTNL, location, 0); |
9168 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
9169 | location); | |
e440a328 | 9170 | } |
9171 | ||
88b03a70 | 9172 | Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, |
9173 | location, 0); | |
9174 | print_stmts = Expression::make_compound(print_stmts, unlock, location); | |
32e3ff69 | 9175 | |
ea664253 | 9176 | return print_stmts->get_backend(context); |
e440a328 | 9177 | } |
9178 | ||
9179 | case BUILTIN_PANIC: | |
9180 | { | |
9181 | const Expression_list* args = this->args(); | |
c484d925 | 9182 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9183 | Expression* arg = args->front(); |
b13c66cd | 9184 | Type *empty = |
823c7e3d | 9185 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 9186 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
9187 | ||
9188 | Expression* panic = | |
03ac9de4 | 9189 | Runtime::make_call(Runtime::GOPANIC, location, 1, arg); |
ea664253 | 9190 | return panic->get_backend(context); |
e440a328 | 9191 | } |
9192 | ||
9193 | case BUILTIN_RECOVER: | |
9194 | { | |
9195 | // The argument is set when building recover thunks. It's a | |
9196 | // boolean value which is true if we can recover a value now. | |
9197 | const Expression_list* args = this->args(); | |
c484d925 | 9198 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9199 | Expression* arg = args->front(); |
b13c66cd | 9200 | Type *empty = |
823c7e3d | 9201 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 9202 | |
e440a328 | 9203 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 9204 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 9205 | |
9206 | // We need to handle a deferred call to recover specially, | |
9207 | // because it changes whether it can recover a panic or not. | |
9208 | // See test7 in test/recover1.go. | |
2c809f8f | 9209 | Expression* recover = Runtime::make_call((this->is_deferred() |
03ac9de4 | 9210 | ? Runtime::DEFERREDRECOVER |
9211 | : Runtime::GORECOVER), | |
2c809f8f | 9212 | location, 0); |
9213 | Expression* cond = | |
9214 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 9215 | return cond->get_backend(context); |
e440a328 | 9216 | } |
9217 | ||
9218 | case BUILTIN_CLOSE: | |
e440a328 | 9219 | { |
9220 | const Expression_list* args = this->args(); | |
c484d925 | 9221 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9222 | Expression* arg = args->front(); |
2c809f8f | 9223 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
9224 | 1, arg); | |
ea664253 | 9225 | return close->get_backend(context); |
e440a328 | 9226 | } |
9227 | ||
9228 | case BUILTIN_SIZEOF: | |
9229 | case BUILTIN_OFFSETOF: | |
9230 | case BUILTIN_ALIGNOF: | |
9231 | { | |
0c77715b | 9232 | Numeric_constant nc; |
9233 | unsigned long val; | |
9234 | if (!this->numeric_constant_value(&nc) | |
9235 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 9236 | { |
c484d925 | 9237 | go_assert(saw_errors()); |
ea664253 | 9238 | return context->backend()->error_expression(); |
7f1d9abd | 9239 | } |
7ba86326 | 9240 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 9241 | mpz_t ival; |
9242 | nc.get_int(&ival); | |
9243 | Expression* int_cst = | |
e67508fa | 9244 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 9245 | mpz_clear(ival); |
ea664253 | 9246 | return int_cst->get_backend(context); |
e440a328 | 9247 | } |
9248 | ||
9249 | case BUILTIN_COPY: | |
9250 | { | |
9251 | const Expression_list* args = this->args(); | |
c484d925 | 9252 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 9253 | Expression* arg1 = args->front(); |
9254 | Expression* arg2 = args->back(); | |
9255 | ||
e440a328 | 9256 | Type* arg1_type = arg1->type(); |
9257 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 9258 | go_assert(arg1->is_variable()); |
321e5ad2 | 9259 | |
9260 | Expression* call; | |
e440a328 | 9261 | |
9262 | Type* arg2_type = arg2->type(); | |
2c809f8f | 9263 | go_assert(arg2->is_variable()); |
321e5ad2 | 9264 | if (arg2_type->is_string_type()) |
9265 | call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location, | |
9266 | 2, arg1, arg2); | |
e440a328 | 9267 | else |
9268 | { | |
321e5ad2 | 9269 | Type* et = at->element_type(); |
9270 | if (et->has_pointer()) | |
9271 | { | |
9272 | Expression* td = Expression::make_type_descriptor(et, | |
9273 | location); | |
9274 | call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location, | |
9275 | 3, td, arg1, arg2); | |
9276 | } | |
9277 | else | |
9278 | { | |
9279 | Expression* sz = Expression::make_type_info(et, | |
9280 | TYPE_INFO_SIZE); | |
9281 | call = Runtime::make_call(Runtime::SLICECOPY, location, 3, | |
9282 | arg1, arg2, sz); | |
9283 | } | |
e440a328 | 9284 | } |
2c809f8f | 9285 | |
321e5ad2 | 9286 | return call->get_backend(context); |
e440a328 | 9287 | } |
9288 | ||
9289 | case BUILTIN_APPEND: | |
321e5ad2 | 9290 | // Handled in Builtin_call_expression::flatten_append. |
9291 | go_unreachable(); | |
e440a328 | 9292 | |
9293 | case BUILTIN_REAL: | |
9294 | case BUILTIN_IMAG: | |
9295 | { | |
9296 | const Expression_list* args = this->args(); | |
c484d925 | 9297 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 9298 | |
9299 | Bexpression* ret; | |
ea664253 | 9300 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 9301 | if (this->code_ == BUILTIN_REAL) |
9302 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
9303 | else | |
9304 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 9305 | return ret; |
e440a328 | 9306 | } |
9307 | ||
48080209 | 9308 | case BUILTIN_COMPLEX: |
e440a328 | 9309 | { |
9310 | const Expression_list* args = this->args(); | |
c484d925 | 9311 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 9312 | Bexpression* breal = args->front()->get_backend(context); |
9313 | Bexpression* bimag = args->back()->get_backend(context); | |
9314 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 9315 | } |
9316 | ||
9317 | default: | |
c3e6f413 | 9318 | go_unreachable(); |
e440a328 | 9319 | } |
9320 | } | |
9321 | ||
9322 | // We have to support exporting a builtin call expression, because | |
9323 | // code can set a constant to the result of a builtin expression. | |
9324 | ||
9325 | void | |
9326 | Builtin_call_expression::do_export(Export* exp) const | |
9327 | { | |
0c77715b | 9328 | Numeric_constant nc; |
9329 | if (!this->numeric_constant_value(&nc)) | |
9330 | { | |
631d5788 | 9331 | go_error_at(this->location(), "value is not constant"); |
0c77715b | 9332 | return; |
9333 | } | |
e440a328 | 9334 | |
0c77715b | 9335 | if (nc.is_int()) |
e440a328 | 9336 | { |
0c77715b | 9337 | mpz_t val; |
9338 | nc.get_int(&val); | |
e440a328 | 9339 | Integer_expression::export_integer(exp, val); |
0c77715b | 9340 | mpz_clear(val); |
e440a328 | 9341 | } |
0c77715b | 9342 | else if (nc.is_float()) |
e440a328 | 9343 | { |
9344 | mpfr_t fval; | |
0c77715b | 9345 | nc.get_float(&fval); |
9346 | Float_expression::export_float(exp, fval); | |
e440a328 | 9347 | mpfr_clear(fval); |
9348 | } | |
0c77715b | 9349 | else if (nc.is_complex()) |
e440a328 | 9350 | { |
fcbea5e4 | 9351 | mpc_t cval; |
9352 | nc.get_complex(&cval); | |
9353 | Complex_expression::export_complex(exp, cval); | |
9354 | mpc_clear(cval); | |
e440a328 | 9355 | } |
0c77715b | 9356 | else |
9357 | go_unreachable(); | |
e440a328 | 9358 | |
9359 | // A trailing space lets us reliably identify the end of the number. | |
9360 | exp->write_c_string(" "); | |
9361 | } | |
9362 | ||
9363 | // Class Call_expression. | |
9364 | ||
8381eda7 | 9365 | // A Go function can be viewed in a couple of different ways. The |
9366 | // code of a Go function becomes a backend function with parameters | |
9367 | // whose types are simply the backend representation of the Go types. | |
9368 | // If there are multiple results, they are returned as a backend | |
9369 | // struct. | |
9370 | ||
9371 | // However, when Go code refers to a function other than simply | |
9372 | // calling it, the backend type of that function is actually a struct. | |
9373 | // The first field of the struct points to the Go function code | |
9374 | // (sometimes a wrapper as described below). The remaining fields | |
9375 | // hold addresses of closed-over variables. This struct is called a | |
9376 | // closure. | |
9377 | ||
9378 | // There are a few cases to consider. | |
9379 | ||
9380 | // A direct function call of a known function in package scope. In | |
9381 | // this case there are no closed-over variables, and we know the name | |
9382 | // of the function code. We can simply produce a backend call to the | |
9383 | // function directly, and not worry about the closure. | |
9384 | ||
9385 | // A direct function call of a known function literal. In this case | |
9386 | // we know the function code and we know the closure. We generate the | |
9387 | // function code such that it expects an additional final argument of | |
9388 | // the closure type. We pass the closure as the last argument, after | |
9389 | // the other arguments. | |
9390 | ||
9391 | // An indirect function call. In this case we have a closure. We | |
9392 | // load the pointer to the function code from the first field of the | |
9393 | // closure. We pass the address of the closure as the last argument. | |
9394 | ||
9395 | // A call to a method of an interface. Type methods are always at | |
9396 | // package scope, so we call the function directly, and don't worry | |
9397 | // about the closure. | |
9398 | ||
9399 | // This means that for a function at package scope we have two cases. | |
9400 | // One is the direct call, which has no closure. The other is the | |
9401 | // indirect call, which does have a closure. We can't simply ignore | |
9402 | // the closure, even though it is the last argument, because that will | |
9403 | // fail on targets where the function pops its arguments. So when | |
9404 | // generating a closure for a package-scope function we set the | |
9405 | // function code pointer in the closure to point to a wrapper | |
9406 | // function. This wrapper function accepts a final argument that | |
9407 | // points to the closure, ignores it, and calls the real function as a | |
9408 | // direct function call. This wrapper will normally be efficient, and | |
9409 | // can often simply be a tail call to the real function. | |
9410 | ||
9411 | // We don't use GCC's static chain pointer because 1) we don't need | |
9412 | // it; 2) GCC only permits using a static chain to call a known | |
9413 | // function, so we can't use it for an indirect call anyhow. Since we | |
9414 | // can't use it for an indirect call, we may as well not worry about | |
9415 | // using it for a direct call either. | |
9416 | ||
9417 | // We pass the closure last rather than first because it means that | |
9418 | // the function wrapper we put into a closure for a package-scope | |
9419 | // function can normally just be a tail call to the real function. | |
9420 | ||
9421 | // For method expressions we generate a wrapper that loads the | |
9422 | // receiver from the closure and then calls the method. This | |
9423 | // unfortunately forces reshuffling the arguments, since there is a | |
9424 | // new first argument, but we can't avoid reshuffling either for | |
9425 | // method expressions or for indirect calls of package-scope | |
9426 | // functions, and since the latter are more common we reshuffle for | |
9427 | // method expressions. | |
9428 | ||
9429 | // Note that the Go code retains the Go types. The extra final | |
9430 | // argument only appears when we convert to the backend | |
9431 | // representation. | |
9432 | ||
e440a328 | 9433 | // Traversal. |
9434 | ||
9435 | int | |
9436 | Call_expression::do_traverse(Traverse* traverse) | |
9437 | { | |
0c0dacab | 9438 | // If we are calling a function in a different package that returns |
9439 | // an unnamed type, this may be the only chance we get to traverse | |
9440 | // that type. We don't traverse this->type_ because it may be a | |
9441 | // Call_multiple_result_type that will just lead back here. | |
9442 | if (this->type_ != NULL && !this->type_->is_error_type()) | |
9443 | { | |
9444 | Function_type *fntype = this->get_function_type(); | |
9445 | if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) | |
9446 | return TRAVERSE_EXIT; | |
9447 | } | |
e440a328 | 9448 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) |
9449 | return TRAVERSE_EXIT; | |
9450 | if (this->args_ != NULL) | |
9451 | { | |
9452 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
9453 | return TRAVERSE_EXIT; | |
9454 | } | |
9455 | return TRAVERSE_CONTINUE; | |
9456 | } | |
9457 | ||
9458 | // Lower a call statement. | |
9459 | ||
9460 | Expression* | |
ceeb4318 | 9461 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
9462 | Statement_inserter* inserter, int) | |
e440a328 | 9463 | { |
b13c66cd | 9464 | Location loc = this->location(); |
09ea332d | 9465 | |
ceeb4318 | 9466 | // A type cast can look like a function call. |
e440a328 | 9467 | if (this->fn_->is_type_expression() |
9468 | && this->args_ != NULL | |
9469 | && this->args_->size() == 1) | |
9470 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 9471 | loc); |
e440a328 | 9472 | |
88f06749 | 9473 | // Because do_type will return an error type and thus prevent future |
9474 | // errors, check for that case now to ensure that the error gets | |
9475 | // reported. | |
37448b10 | 9476 | Function_type* fntype = this->get_function_type(); |
9477 | if (fntype == NULL) | |
88f06749 | 9478 | { |
9479 | if (!this->fn_->type()->is_error()) | |
9480 | this->report_error(_("expected function")); | |
5f1045b5 | 9481 | this->set_is_error(); |
9482 | return this; | |
88f06749 | 9483 | } |
9484 | ||
e440a328 | 9485 | // Handle an argument which is a call to a function which returns |
9486 | // multiple results. | |
9487 | if (this->args_ != NULL | |
9488 | && this->args_->size() == 1 | |
37448b10 | 9489 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9490 | { |
e440a328 | 9491 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9492 | if (rc > 1 | |
37448b10 | 9493 | && ((fntype->parameters() != NULL |
9494 | && (fntype->parameters()->size() == rc | |
9495 | || (fntype->is_varargs() | |
9496 | && fntype->parameters()->size() - 1 <= rc))) | |
9497 | || fntype->is_builtin())) | |
e440a328 | 9498 | { |
9499 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 9500 | call->set_is_multi_value_arg(); |
c33af8e4 | 9501 | if (this->is_varargs_) |
9502 | { | |
9503 | // It is not clear which result of a multiple result call | |
9504 | // the ellipsis operator should be applied to. If we unpack the | |
9505 | // the call into its individual results here, the ellipsis will be | |
9506 | // applied to the last result. | |
631d5788 | 9507 | go_error_at(call->location(), |
9508 | _("multiple-value argument in single-value context")); | |
c33af8e4 | 9509 | return Expression::make_error(call->location()); |
9510 | } | |
9511 | ||
e440a328 | 9512 | Expression_list* args = new Expression_list; |
9513 | for (size_t i = 0; i < rc; ++i) | |
9514 | args->push_back(Expression::make_call_result(call, i)); | |
9515 | // We can't return a new call expression here, because this | |
42535814 | 9516 | // one may be referenced by Call_result expressions. We |
9517 | // also can't delete the old arguments, because we may still | |
9518 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9519 | this->args_ = args; |
9520 | } | |
9521 | } | |
9522 | ||
37448b10 | 9523 | // Recognize a call to a builtin function. |
9524 | if (fntype->is_builtin()) | |
9525 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9526 | this->is_varargs_, loc); | |
9527 | ||
ceeb4318 | 9528 | // If this call returns multiple results, create a temporary |
5731103c | 9529 | // variable to hold them. |
9530 | if (this->result_count() > 1 && this->call_temp_ == NULL) | |
ceeb4318 | 9531 | { |
5731103c | 9532 | Struct_field_list* sfl = new Struct_field_list(); |
9533 | Function_type* fntype = this->get_function_type(); | |
37448b10 | 9534 | const Typed_identifier_list* results = fntype->results(); |
5731103c | 9535 | Location loc = this->location(); |
9536 | ||
9537 | int i = 0; | |
9538 | char buf[20]; | |
ceeb4318 | 9539 | for (Typed_identifier_list::const_iterator p = results->begin(); |
5731103c | 9540 | p != results->end(); |
9541 | ++p, ++i) | |
9542 | { | |
9543 | snprintf(buf, sizeof buf, "res%d", i); | |
9544 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9545 | } | |
9546 | ||
9547 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9548 | st->set_is_struct_incomparable(); | |
9549 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9550 | inserter->insert(this->call_temp_); | |
ceeb4318 | 9551 | } |
9552 | ||
e440a328 | 9553 | // Handle a call to a varargs function by packaging up the extra |
9554 | // parameters. | |
37448b10 | 9555 | if (fntype->is_varargs()) |
e440a328 | 9556 | { |
e440a328 | 9557 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9558 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9559 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9560 | this->lower_varargs(gogo, function, inserter, varargs_type, |
0e9a2e72 | 9561 | parameters->size(), SLICE_STORAGE_MAY_ESCAPE); |
09ea332d | 9562 | } |
9563 | ||
9564 | // If this is call to a method, call the method directly passing the | |
9565 | // object as the first parameter. | |
9566 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9567 | if (bme != NULL) | |
9568 | { | |
0afbb937 | 9569 | Named_object* methodfn = bme->function(); |
09ea332d | 9570 | Expression* first_arg = bme->first_argument(); |
9571 | ||
9572 | // We always pass a pointer when calling a method. | |
9573 | if (first_arg->type()->points_to() == NULL | |
9574 | && !first_arg->type()->is_error()) | |
9575 | { | |
9576 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9577 | // We may need to create a temporary variable so that we can | |
9578 | // take the address. We can't do that here because it will | |
9579 | // mess up the order of evaluation. | |
9580 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9581 | ue->set_create_temp(); | |
9582 | } | |
9583 | ||
9584 | // If we are calling a method which was inherited from an | |
9585 | // embedded struct, and the method did not get a stub, then the | |
9586 | // first type may be wrong. | |
9587 | Type* fatype = bme->first_argument_type(); | |
9588 | if (fatype != NULL) | |
9589 | { | |
9590 | if (fatype->points_to() == NULL) | |
9591 | fatype = Type::make_pointer_type(fatype); | |
9592 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9593 | } | |
9594 | ||
9595 | Expression_list* new_args = new Expression_list(); | |
9596 | new_args->push_back(first_arg); | |
9597 | if (this->args_ != NULL) | |
9598 | { | |
9599 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9600 | p != this->args_->end(); | |
9601 | ++p) | |
9602 | new_args->push_back(*p); | |
9603 | } | |
9604 | ||
9605 | // We have to change in place because this structure may be | |
9606 | // referenced by Call_result_expressions. We can't delete the | |
9607 | // old arguments, because we may be traversing them up in some | |
9608 | // caller. FIXME. | |
9609 | this->args_ = new_args; | |
0afbb937 | 9610 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9611 | bme->location()); |
e440a328 | 9612 | } |
9613 | ||
105f9a24 | 9614 | // Handle a couple of special runtime functions. In the runtime |
9615 | // package, getcallerpc returns the PC of the caller, and | |
9616 | // getcallersp returns the frame pointer of the caller. Implement | |
9617 | // these by turning them into calls to GCC builtin functions. We | |
9618 | // could implement them in normal code, but then we would have to | |
9619 | // explicitly unwind the stack. These functions are intended to be | |
9620 | // efficient. Note that this technique obviously only works for | |
33d1d391 | 9621 | // direct calls, but that is the only way they are used. |
9622 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
105f9a24 | 9623 | { |
9624 | Func_expression* fe = this->fn_->func_expression(); | |
9625 | if (fe != NULL | |
9626 | && fe->named_object()->is_function_declaration() | |
9627 | && fe->named_object()->package() == NULL) | |
9628 | { | |
9629 | std::string n = Gogo::unpack_hidden_name(fe->named_object()->name()); | |
33d1d391 | 9630 | if ((this->args_ == NULL || this->args_->size() == 0) |
9631 | && n == "getcallerpc") | |
105f9a24 | 9632 | { |
9633 | static Named_object* builtin_return_address; | |
9634 | return this->lower_to_builtin(&builtin_return_address, | |
9635 | "__builtin_return_address", | |
9636 | 0); | |
9637 | } | |
b5665f52 | 9638 | else if ((this->args_ == NULL || this->args_->size() == 0) |
33d1d391 | 9639 | && n == "getcallersp") |
105f9a24 | 9640 | { |
9641 | static Named_object* builtin_frame_address; | |
9642 | return this->lower_to_builtin(&builtin_frame_address, | |
9643 | "__builtin_frame_address", | |
9644 | 1); | |
9645 | } | |
9646 | } | |
9647 | } | |
9648 | ||
e440a328 | 9649 | return this; |
9650 | } | |
9651 | ||
9652 | // Lower a call to a varargs function. FUNCTION is the function in | |
9653 | // which the call occurs--it's not the function we are calling. | |
9654 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9655 | // PARAM_COUNT is the number of parameters of the function we are | |
9656 | // calling; the last of these parameters will be the varargs | |
9657 | // parameter. | |
9658 | ||
09ea332d | 9659 | void |
e440a328 | 9660 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9661 | Statement_inserter* inserter, |
0e9a2e72 | 9662 | Type* varargs_type, size_t param_count, |
9663 | Slice_storage_escape_disp escape_disp) | |
e440a328 | 9664 | { |
9665 | if (this->varargs_are_lowered_) | |
09ea332d | 9666 | return; |
e440a328 | 9667 | |
b13c66cd | 9668 | Location loc = this->location(); |
e440a328 | 9669 | |
c484d925 | 9670 | go_assert(param_count > 0); |
411eb89e | 9671 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9672 | |
9673 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9674 | if (arg_count < param_count - 1) | |
9675 | { | |
9676 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9677 | return; |
e440a328 | 9678 | } |
9679 | ||
9680 | Expression_list* old_args = this->args_; | |
9681 | Expression_list* new_args = new Expression_list(); | |
9682 | bool push_empty_arg = false; | |
9683 | if (old_args == NULL || old_args->empty()) | |
9684 | { | |
c484d925 | 9685 | go_assert(param_count == 1); |
e440a328 | 9686 | push_empty_arg = true; |
9687 | } | |
9688 | else | |
9689 | { | |
9690 | Expression_list::const_iterator pa; | |
9691 | int i = 1; | |
9692 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9693 | { | |
9694 | if (static_cast<size_t>(i) == param_count) | |
9695 | break; | |
9696 | new_args->push_back(*pa); | |
9697 | } | |
9698 | ||
9699 | // We have reached the varargs parameter. | |
9700 | ||
9701 | bool issued_error = false; | |
9702 | if (pa == old_args->end()) | |
9703 | push_empty_arg = true; | |
9704 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9705 | new_args->push_back(*pa); | |
9706 | else if (this->is_varargs_) | |
9707 | { | |
a6645f74 | 9708 | if ((*pa)->type()->is_slice_type()) |
9709 | this->report_error(_("too many arguments")); | |
9710 | else | |
9711 | { | |
631d5788 | 9712 | go_error_at(this->location(), |
9713 | _("invalid use of %<...%> with non-slice")); | |
a6645f74 | 9714 | this->set_is_error(); |
9715 | } | |
09ea332d | 9716 | return; |
e440a328 | 9717 | } |
e440a328 | 9718 | else |
9719 | { | |
9720 | Type* element_type = varargs_type->array_type()->element_type(); | |
9721 | Expression_list* vals = new Expression_list; | |
9722 | for (; pa != old_args->end(); ++pa, ++i) | |
9723 | { | |
9724 | // Check types here so that we get a better message. | |
9725 | Type* patype = (*pa)->type(); | |
b13c66cd | 9726 | Location paloc = (*pa)->location(); |
e440a328 | 9727 | if (!this->check_argument_type(i, element_type, patype, |
9728 | paloc, issued_error)) | |
9729 | continue; | |
9730 | vals->push_back(*pa); | |
9731 | } | |
0e9a2e72 | 9732 | Slice_construction_expression* sce = |
e440a328 | 9733 | Expression::make_slice_composite_literal(varargs_type, vals, loc); |
0e9a2e72 | 9734 | if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) |
9735 | sce->set_storage_does_not_escape(); | |
9736 | Expression* val = sce; | |
09ea332d | 9737 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9738 | new_args->push_back(val); |
9739 | } | |
9740 | } | |
9741 | ||
9742 | if (push_empty_arg) | |
9743 | new_args->push_back(Expression::make_nil(loc)); | |
9744 | ||
9745 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9746 | // be referenced by Call_result expressions. FIXME. We can't |
9747 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9748 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9749 | this->args_ = new_args; |
9750 | this->varargs_are_lowered_ = true; | |
e440a328 | 9751 | } |
9752 | ||
105f9a24 | 9753 | // Return a call to __builtin_return_address or __builtin_frame_address. |
9754 | ||
9755 | Expression* | |
9756 | Call_expression::lower_to_builtin(Named_object** pno, const char* name, | |
9757 | int arg) | |
9758 | { | |
9759 | if (*pno == NULL) | |
9760 | *pno = Gogo::declare_builtin_rf_address(name); | |
9761 | ||
9762 | Location loc = this->location(); | |
9763 | ||
9764 | Expression* fn = Expression::make_func_reference(*pno, NULL, loc); | |
9765 | Expression* a = Expression::make_integer_ul(arg, NULL, loc); | |
9766 | Expression_list *args = new Expression_list(); | |
9767 | args->push_back(a); | |
9768 | Expression* call = Expression::make_call(fn, args, false, loc); | |
9769 | ||
9770 | // The builtin functions return void*, but the Go functions return uintptr. | |
9771 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9772 | return Expression::make_cast(uintptr_type, call, loc); | |
9773 | } | |
9774 | ||
2c809f8f | 9775 | // Flatten a call with multiple results into a temporary. |
9776 | ||
9777 | Expression* | |
b8e86a51 | 9778 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
9779 | Statement_inserter* inserter) | |
2c809f8f | 9780 | { |
5bf8be8b | 9781 | if (this->is_erroneous_call()) |
9782 | { | |
9783 | go_assert(saw_errors()); | |
9784 | return Expression::make_error(this->location()); | |
9785 | } | |
b8e86a51 | 9786 | |
91c0fd76 | 9787 | if (this->is_flattened_) |
9788 | return this; | |
9789 | this->is_flattened_ = true; | |
9790 | ||
b8e86a51 | 9791 | // Add temporary variables for all arguments that require type |
9792 | // conversion. | |
9793 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 9794 | if (fntype == NULL) |
9795 | { | |
9796 | go_assert(saw_errors()); | |
9797 | return this; | |
9798 | } | |
b8e86a51 | 9799 | if (this->args_ != NULL && !this->args_->empty() |
9800 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
9801 | { | |
9802 | bool is_interface_method = | |
9803 | this->fn_->interface_field_reference_expression() != NULL; | |
9804 | ||
9805 | Expression_list *args = new Expression_list(); | |
9806 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
9807 | Expression_list::const_iterator pa = this->args_->begin(); | |
9808 | if (!is_interface_method && fntype->is_method()) | |
9809 | { | |
9810 | // The receiver argument. | |
9811 | args->push_back(*pa); | |
9812 | ++pa; | |
9813 | } | |
9814 | for (; pa != this->args_->end(); ++pa, ++pp) | |
9815 | { | |
9816 | go_assert(pp != fntype->parameters()->end()); | |
9817 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
9818 | args->push_back(*pa); | |
9819 | else | |
9820 | { | |
9821 | Location loc = (*pa)->location(); | |
8ba8cc87 | 9822 | Expression* arg = *pa; |
9823 | if (!arg->is_variable()) | |
9824 | { | |
9825 | Temporary_statement *temp = | |
9826 | Statement::make_temporary(NULL, arg, loc); | |
9827 | inserter->insert(temp); | |
9828 | arg = Expression::make_temporary_reference(temp, loc); | |
9829 | } | |
9830 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
9831 | loc); | |
9832 | args->push_back(arg); | |
b8e86a51 | 9833 | } |
9834 | } | |
9835 | delete this->args_; | |
9836 | this->args_ = args; | |
9837 | } | |
9838 | ||
2c809f8f | 9839 | return this; |
9840 | } | |
9841 | ||
ceeb4318 | 9842 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9843 | |
9844 | Function_type* | |
9845 | Call_expression::get_function_type() const | |
9846 | { | |
9847 | return this->fn_->type()->function_type(); | |
9848 | } | |
9849 | ||
9850 | // Return the number of values which this call will return. | |
9851 | ||
9852 | size_t | |
9853 | Call_expression::result_count() const | |
9854 | { | |
9855 | const Function_type* fntype = this->get_function_type(); | |
9856 | if (fntype == NULL) | |
9857 | return 0; | |
9858 | if (fntype->results() == NULL) | |
9859 | return 0; | |
9860 | return fntype->results()->size(); | |
9861 | } | |
9862 | ||
5731103c | 9863 | // Return the temporary that holds the result for a call with multiple |
9864 | // results. | |
ceeb4318 | 9865 | |
9866 | Temporary_statement* | |
5731103c | 9867 | Call_expression::results() const |
ceeb4318 | 9868 | { |
5731103c | 9869 | if (this->call_temp_ == NULL) |
cd238b8d | 9870 | { |
9871 | go_assert(saw_errors()); | |
9872 | return NULL; | |
9873 | } | |
5731103c | 9874 | return this->call_temp_; |
ceeb4318 | 9875 | } |
9876 | ||
1373401e | 9877 | // Set the number of results expected from a call expression. |
9878 | ||
9879 | void | |
9880 | Call_expression::set_expected_result_count(size_t count) | |
9881 | { | |
9882 | go_assert(this->expected_result_count_ == 0); | |
9883 | this->expected_result_count_ = count; | |
9884 | } | |
9885 | ||
e440a328 | 9886 | // Return whether this is a call to the predeclared function recover. |
9887 | ||
9888 | bool | |
9889 | Call_expression::is_recover_call() const | |
9890 | { | |
9891 | return this->do_is_recover_call(); | |
9892 | } | |
9893 | ||
9894 | // Set the argument to the recover function. | |
9895 | ||
9896 | void | |
9897 | Call_expression::set_recover_arg(Expression* arg) | |
9898 | { | |
9899 | this->do_set_recover_arg(arg); | |
9900 | } | |
9901 | ||
9902 | // Virtual functions also implemented by Builtin_call_expression. | |
9903 | ||
9904 | bool | |
9905 | Call_expression::do_is_recover_call() const | |
9906 | { | |
9907 | return false; | |
9908 | } | |
9909 | ||
9910 | void | |
9911 | Call_expression::do_set_recover_arg(Expression*) | |
9912 | { | |
c3e6f413 | 9913 | go_unreachable(); |
e440a328 | 9914 | } |
9915 | ||
ceeb4318 | 9916 | // We have found an error with this call expression; return true if |
9917 | // we should report it. | |
9918 | ||
9919 | bool | |
9920 | Call_expression::issue_error() | |
9921 | { | |
9922 | if (this->issued_error_) | |
9923 | return false; | |
9924 | else | |
9925 | { | |
9926 | this->issued_error_ = true; | |
9927 | return true; | |
9928 | } | |
9929 | } | |
9930 | ||
5bf8be8b | 9931 | // Whether or not this call contains errors, either in the call or the |
9932 | // arguments to the call. | |
9933 | ||
9934 | bool | |
9935 | Call_expression::is_erroneous_call() | |
9936 | { | |
9937 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9938 | return true; | |
9939 | ||
9940 | if (this->args() == NULL) | |
9941 | return false; | |
9942 | for (Expression_list::iterator pa = this->args()->begin(); | |
9943 | pa != this->args()->end(); | |
9944 | ++pa) | |
9945 | { | |
9946 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9947 | return true; | |
9948 | } | |
9949 | return false; | |
9950 | } | |
9951 | ||
e440a328 | 9952 | // Get the type. |
9953 | ||
9954 | Type* | |
9955 | Call_expression::do_type() | |
9956 | { | |
9957 | if (this->type_ != NULL) | |
9958 | return this->type_; | |
9959 | ||
9960 | Type* ret; | |
9961 | Function_type* fntype = this->get_function_type(); | |
9962 | if (fntype == NULL) | |
9963 | return Type::make_error_type(); | |
9964 | ||
9965 | const Typed_identifier_list* results = fntype->results(); | |
9966 | if (results == NULL) | |
9967 | ret = Type::make_void_type(); | |
9968 | else if (results->size() == 1) | |
9969 | ret = results->begin()->type(); | |
9970 | else | |
9971 | ret = Type::make_call_multiple_result_type(this); | |
9972 | ||
9973 | this->type_ = ret; | |
9974 | ||
9975 | return this->type_; | |
9976 | } | |
9977 | ||
9978 | // Determine types for a call expression. We can use the function | |
9979 | // parameter types to set the types of the arguments. | |
9980 | ||
9981 | void | |
9982 | Call_expression::do_determine_type(const Type_context*) | |
9983 | { | |
fb94b0ca | 9984 | if (!this->determining_types()) |
9985 | return; | |
9986 | ||
e440a328 | 9987 | this->fn_->determine_type_no_context(); |
9988 | Function_type* fntype = this->get_function_type(); | |
9989 | const Typed_identifier_list* parameters = NULL; | |
9990 | if (fntype != NULL) | |
9991 | parameters = fntype->parameters(); | |
9992 | if (this->args_ != NULL) | |
9993 | { | |
9994 | Typed_identifier_list::const_iterator pt; | |
9995 | if (parameters != NULL) | |
9996 | pt = parameters->begin(); | |
09ea332d | 9997 | bool first = true; |
e440a328 | 9998 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9999 | pa != this->args_->end(); | |
10000 | ++pa) | |
10001 | { | |
09ea332d | 10002 | if (first) |
10003 | { | |
10004 | first = false; | |
10005 | // If this is a method, the first argument is the | |
10006 | // receiver. | |
10007 | if (fntype != NULL && fntype->is_method()) | |
10008 | { | |
10009 | Type* rtype = fntype->receiver()->type(); | |
10010 | // The receiver is always passed as a pointer. | |
10011 | if (rtype->points_to() == NULL) | |
10012 | rtype = Type::make_pointer_type(rtype); | |
10013 | Type_context subcontext(rtype, false); | |
10014 | (*pa)->determine_type(&subcontext); | |
10015 | continue; | |
10016 | } | |
10017 | } | |
10018 | ||
e440a328 | 10019 | if (parameters != NULL && pt != parameters->end()) |
10020 | { | |
10021 | Type_context subcontext(pt->type(), false); | |
10022 | (*pa)->determine_type(&subcontext); | |
10023 | ++pt; | |
10024 | } | |
10025 | else | |
10026 | (*pa)->determine_type_no_context(); | |
10027 | } | |
10028 | } | |
10029 | } | |
10030 | ||
fb94b0ca | 10031 | // Called when determining types for a Call_expression. Return true |
10032 | // if we should go ahead, false if they have already been determined. | |
10033 | ||
10034 | bool | |
10035 | Call_expression::determining_types() | |
10036 | { | |
10037 | if (this->types_are_determined_) | |
10038 | return false; | |
10039 | else | |
10040 | { | |
10041 | this->types_are_determined_ = true; | |
10042 | return true; | |
10043 | } | |
10044 | } | |
10045 | ||
e440a328 | 10046 | // Check types for parameter I. |
10047 | ||
10048 | bool | |
10049 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
10050 | const Type* argument_type, | |
b13c66cd | 10051 | Location argument_location, |
e440a328 | 10052 | bool issued_error) |
10053 | { | |
10054 | std::string reason; | |
1eae365b | 10055 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 10056 | { |
10057 | if (!issued_error) | |
10058 | { | |
10059 | if (reason.empty()) | |
631d5788 | 10060 | go_error_at(argument_location, "argument %d has incompatible type", i); |
e440a328 | 10061 | else |
631d5788 | 10062 | go_error_at(argument_location, |
10063 | "argument %d has incompatible type (%s)", | |
10064 | i, reason.c_str()); | |
e440a328 | 10065 | } |
10066 | this->set_is_error(); | |
10067 | return false; | |
10068 | } | |
10069 | return true; | |
10070 | } | |
10071 | ||
10072 | // Check types. | |
10073 | ||
10074 | void | |
10075 | Call_expression::do_check_types(Gogo*) | |
10076 | { | |
a6645f74 | 10077 | if (this->classification() == EXPRESSION_ERROR) |
10078 | return; | |
10079 | ||
e440a328 | 10080 | Function_type* fntype = this->get_function_type(); |
10081 | if (fntype == NULL) | |
10082 | { | |
5c13bd80 | 10083 | if (!this->fn_->type()->is_error()) |
e440a328 | 10084 | this->report_error(_("expected function")); |
10085 | return; | |
10086 | } | |
10087 | ||
1373401e | 10088 | if (this->expected_result_count_ != 0 |
10089 | && this->expected_result_count_ != this->result_count()) | |
10090 | { | |
10091 | if (this->issue_error()) | |
10092 | this->report_error(_("function result count mismatch")); | |
10093 | this->set_is_error(); | |
10094 | return; | |
10095 | } | |
10096 | ||
09ea332d | 10097 | bool is_method = fntype->is_method(); |
10098 | if (is_method) | |
e440a328 | 10099 | { |
09ea332d | 10100 | go_assert(this->args_ != NULL && !this->args_->empty()); |
10101 | Type* rtype = fntype->receiver()->type(); | |
10102 | Expression* first_arg = this->args_->front(); | |
1eae365b | 10103 | // We dereference the values since receivers are always passed |
10104 | // as pointers. | |
09ea332d | 10105 | std::string reason; |
1eae365b | 10106 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
10107 | &reason)) | |
e440a328 | 10108 | { |
09ea332d | 10109 | if (reason.empty()) |
10110 | this->report_error(_("incompatible type for receiver")); | |
10111 | else | |
e440a328 | 10112 | { |
631d5788 | 10113 | go_error_at(this->location(), |
10114 | "incompatible type for receiver (%s)", | |
10115 | reason.c_str()); | |
09ea332d | 10116 | this->set_is_error(); |
e440a328 | 10117 | } |
10118 | } | |
10119 | } | |
10120 | ||
10121 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 10122 | // we don't have to worry about it here unless something is wrong. |
10123 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
10124 | { | |
10125 | if (!fntype->is_varargs()) | |
10126 | { | |
631d5788 | 10127 | go_error_at(this->location(), |
10128 | _("invalid use of %<...%> calling non-variadic function")); | |
a6645f74 | 10129 | this->set_is_error(); |
10130 | return; | |
10131 | } | |
10132 | } | |
e440a328 | 10133 | |
10134 | const Typed_identifier_list* parameters = fntype->parameters(); | |
33d1d391 | 10135 | if (this->args_ == NULL || this->args_->size() == 0) |
e440a328 | 10136 | { |
10137 | if (parameters != NULL && !parameters->empty()) | |
10138 | this->report_error(_("not enough arguments")); | |
10139 | } | |
10140 | else if (parameters == NULL) | |
09ea332d | 10141 | { |
10142 | if (!is_method || this->args_->size() > 1) | |
10143 | this->report_error(_("too many arguments")); | |
10144 | } | |
1373401e | 10145 | else if (this->args_->size() == 1 |
10146 | && this->args_->front()->call_expression() != NULL | |
10147 | && this->args_->front()->call_expression()->result_count() > 1) | |
10148 | { | |
10149 | // This is F(G()) when G returns more than one result. If the | |
10150 | // results can be matched to parameters, it would have been | |
10151 | // lowered in do_lower. If we get here we know there is a | |
10152 | // mismatch. | |
10153 | if (this->args_->front()->call_expression()->result_count() | |
10154 | < parameters->size()) | |
10155 | this->report_error(_("not enough arguments")); | |
10156 | else | |
10157 | this->report_error(_("too many arguments")); | |
10158 | } | |
e440a328 | 10159 | else |
10160 | { | |
10161 | int i = 0; | |
09ea332d | 10162 | Expression_list::const_iterator pa = this->args_->begin(); |
10163 | if (is_method) | |
10164 | ++pa; | |
10165 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
10166 | pt != parameters->end(); | |
10167 | ++pt, ++pa, ++i) | |
e440a328 | 10168 | { |
09ea332d | 10169 | if (pa == this->args_->end()) |
e440a328 | 10170 | { |
09ea332d | 10171 | this->report_error(_("not enough arguments")); |
e440a328 | 10172 | return; |
10173 | } | |
10174 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
10175 | (*pa)->location(), false); | |
10176 | } | |
09ea332d | 10177 | if (pa != this->args_->end()) |
10178 | this->report_error(_("too many arguments")); | |
e440a328 | 10179 | } |
10180 | } | |
10181 | ||
72666aed | 10182 | Expression* |
10183 | Call_expression::do_copy() | |
10184 | { | |
10185 | Call_expression* call = | |
10186 | Expression::make_call(this->fn_->copy(), | |
10187 | (this->args_ == NULL | |
10188 | ? NULL | |
10189 | : this->args_->copy()), | |
10190 | this->is_varargs_, this->location()); | |
10191 | ||
10192 | if (this->varargs_are_lowered_) | |
10193 | call->set_varargs_are_lowered(); | |
10194 | return call; | |
10195 | } | |
10196 | ||
e440a328 | 10197 | // Return whether we have to use a temporary variable to ensure that |
10198 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 10199 | // results then it will inevitably be executed last. |
e440a328 | 10200 | |
10201 | bool | |
10202 | Call_expression::do_must_eval_in_order() const | |
10203 | { | |
ceeb4318 | 10204 | return this->result_count() > 0; |
e440a328 | 10205 | } |
10206 | ||
e440a328 | 10207 | // Get the function and the first argument to use when calling an |
10208 | // interface method. | |
10209 | ||
2387f644 | 10210 | Expression* |
e440a328 | 10211 | Call_expression::interface_method_function( |
e440a328 | 10212 | Interface_field_reference_expression* interface_method, |
db122cb9 | 10213 | Expression** first_arg_ptr, |
10214 | Location location) | |
e440a328 | 10215 | { |
db122cb9 | 10216 | Expression* object = interface_method->get_underlying_object(); |
10217 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); | |
10218 | *first_arg_ptr = | |
10219 | Expression::make_unsafe_cast(unsafe_ptr_type, object, location); | |
2387f644 | 10220 | return interface_method->get_function(); |
e440a328 | 10221 | } |
10222 | ||
10223 | // Build the call expression. | |
10224 | ||
ea664253 | 10225 | Bexpression* |
10226 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10227 | { |
5731103c | 10228 | Location location = this->location(); |
10229 | ||
2c809f8f | 10230 | if (this->call_ != NULL) |
5731103c | 10231 | { |
10232 | // If the call returns multiple results, make a new reference to | |
10233 | // the temporary. | |
10234 | if (this->call_temp_ != NULL) | |
10235 | { | |
10236 | Expression* ref = | |
10237 | Expression::make_temporary_reference(this->call_temp_, location); | |
10238 | return ref->get_backend(context); | |
10239 | } | |
10240 | ||
10241 | return this->call_; | |
10242 | } | |
e440a328 | 10243 | |
10244 | Function_type* fntype = this->get_function_type(); | |
10245 | if (fntype == NULL) | |
ea664253 | 10246 | return context->backend()->error_expression(); |
e440a328 | 10247 | |
10248 | if (this->fn_->is_error_expression()) | |
ea664253 | 10249 | return context->backend()->error_expression(); |
e440a328 | 10250 | |
10251 | Gogo* gogo = context->gogo(); | |
e440a328 | 10252 | |
10253 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 10254 | Interface_field_reference_expression* interface_method = |
10255 | this->fn_->interface_field_reference_expression(); | |
10256 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 10257 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 10258 | |
f8bdf81a | 10259 | bool has_closure_arg; |
8381eda7 | 10260 | if (has_closure) |
f8bdf81a | 10261 | has_closure_arg = true; |
8381eda7 | 10262 | else if (func != NULL) |
f8bdf81a | 10263 | has_closure_arg = false; |
8381eda7 | 10264 | else if (is_interface_method) |
f8bdf81a | 10265 | has_closure_arg = false; |
8381eda7 | 10266 | else |
f8bdf81a | 10267 | has_closure_arg = true; |
8381eda7 | 10268 | |
e440a328 | 10269 | int nargs; |
2c809f8f | 10270 | std::vector<Bexpression*> fn_args; |
e440a328 | 10271 | if (this->args_ == NULL || this->args_->empty()) |
10272 | { | |
f8bdf81a | 10273 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 10274 | if (nargs > 0) |
10275 | fn_args.resize(1); | |
e440a328 | 10276 | } |
09ea332d | 10277 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
10278 | { | |
10279 | // Passing a receiver parameter. | |
10280 | go_assert(!is_interface_method | |
10281 | && fntype->is_method() | |
10282 | && this->args_->size() == 1); | |
f8bdf81a | 10283 | nargs = 1; |
2c809f8f | 10284 | fn_args.resize(1); |
ea664253 | 10285 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 10286 | } |
e440a328 | 10287 | else |
10288 | { | |
10289 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 10290 | |
10291 | nargs = this->args_->size(); | |
09ea332d | 10292 | int i = is_interface_method ? 1 : 0; |
e440a328 | 10293 | nargs += i; |
2c809f8f | 10294 | fn_args.resize(nargs); |
e440a328 | 10295 | |
10296 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 10297 | Expression_list::const_iterator pe = this->args_->begin(); |
10298 | if (!is_interface_method && fntype->is_method()) | |
10299 | { | |
ea664253 | 10300 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 10301 | ++pe; |
10302 | ++i; | |
10303 | } | |
10304 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 10305 | { |
c484d925 | 10306 | go_assert(pp != params->end()); |
2c809f8f | 10307 | Expression* arg = |
10308 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
10309 | location); | |
ea664253 | 10310 | fn_args[i] = arg->get_backend(context); |
e440a328 | 10311 | } |
c484d925 | 10312 | go_assert(pp == params->end()); |
f8bdf81a | 10313 | go_assert(i == nargs); |
e440a328 | 10314 | } |
10315 | ||
2c809f8f | 10316 | Expression* fn; |
10317 | Expression* closure = NULL; | |
8381eda7 | 10318 | if (func != NULL) |
10319 | { | |
10320 | Named_object* no = func->named_object(); | |
2c809f8f | 10321 | fn = Expression::make_func_code_reference(no, location); |
10322 | if (has_closure) | |
10323 | closure = func->closure(); | |
8381eda7 | 10324 | } |
09ea332d | 10325 | else if (!is_interface_method) |
8381eda7 | 10326 | { |
2c809f8f | 10327 | closure = this->fn_; |
10328 | ||
10329 | // The backend representation of this function type is a pointer | |
10330 | // to a struct whose first field is the actual function to call. | |
10331 | Type* pfntype = | |
10332 | Type::make_pointer_type( | |
10333 | Type::make_pointer_type(Type::make_void_type())); | |
10334 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
f614ea8b | 10335 | fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location); |
2c809f8f | 10336 | } |
e440a328 | 10337 | else |
cf609de4 | 10338 | { |
2387f644 | 10339 | Expression* first_arg; |
db122cb9 | 10340 | fn = this->interface_method_function(interface_method, &first_arg, |
10341 | location); | |
ea664253 | 10342 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 10343 | } |
10344 | ||
1ecc6157 | 10345 | Bexpression* bclosure = NULL; |
10346 | if (has_closure_arg) | |
10347 | bclosure = closure->get_backend(context); | |
f8bdf81a | 10348 | else |
1ecc6157 | 10349 | go_assert(closure == NULL); |
f8bdf81a | 10350 | |
ea664253 | 10351 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 10352 | |
10353 | // When not calling a named function directly, use a type conversion | |
10354 | // in case the type of the function is a recursive type which refers | |
10355 | // to itself. We don't do this for an interface method because 1) | |
10356 | // an interface method never refers to itself, so we always have a | |
10357 | // function type here; 2) we pass an extra first argument to an | |
10358 | // interface method, so fntype is not correct. | |
10359 | if (func == NULL && !is_interface_method) | |
10360 | { | |
10361 | Btype* bft = fntype->get_backend_fntype(gogo); | |
10362 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
10363 | } | |
10364 | ||
4ced7af9 | 10365 | Bfunction* bfunction = NULL; |
10366 | if (context->function()) | |
10367 | bfunction = context->function()->func_value()->get_decl(); | |
10368 | Bexpression* call = gogo->backend()->call_expression(bfunction, bfn, | |
10369 | fn_args, bclosure, | |
10370 | location); | |
e440a328 | 10371 | |
5731103c | 10372 | if (this->call_temp_ != NULL) |
e440a328 | 10373 | { |
5731103c | 10374 | // This case occurs when the call returns multiple results. |
e440a328 | 10375 | |
5731103c | 10376 | Expression* ref = Expression::make_temporary_reference(this->call_temp_, |
10377 | location); | |
10378 | Bexpression* bref = ref->get_backend(context); | |
10379 | Bstatement* bassn = gogo->backend()->assignment_statement(bfunction, | |
10380 | bref, call, | |
10381 | location); | |
e440a328 | 10382 | |
5731103c | 10383 | ref = Expression::make_temporary_reference(this->call_temp_, location); |
10384 | this->call_ = ref->get_backend(context); | |
10385 | ||
10386 | return gogo->backend()->compound_expression(bassn, this->call_, | |
10387 | location); | |
2c809f8f | 10388 | } |
e440a328 | 10389 | |
2c809f8f | 10390 | this->call_ = call; |
ea664253 | 10391 | return this->call_; |
e440a328 | 10392 | } |
10393 | ||
d751bb78 | 10394 | // Dump ast representation for a call expressin. |
10395 | ||
10396 | void | |
10397 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
10398 | { | |
10399 | this->fn_->dump_expression(ast_dump_context); | |
10400 | ast_dump_context->ostream() << "("; | |
10401 | if (args_ != NULL) | |
10402 | ast_dump_context->dump_expression_list(this->args_); | |
10403 | ||
10404 | ast_dump_context->ostream() << ") "; | |
10405 | } | |
10406 | ||
e440a328 | 10407 | // Make a call expression. |
10408 | ||
10409 | Call_expression* | |
10410 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 10411 | Location location) |
e440a328 | 10412 | { |
10413 | return new Call_expression(fn, args, is_varargs, location); | |
10414 | } | |
10415 | ||
da244e59 | 10416 | // Class Call_result_expression. |
e440a328 | 10417 | |
10418 | // Traverse a call result. | |
10419 | ||
10420 | int | |
10421 | Call_result_expression::do_traverse(Traverse* traverse) | |
10422 | { | |
10423 | if (traverse->remember_expression(this->call_)) | |
10424 | { | |
10425 | // We have already traversed the call expression. | |
10426 | return TRAVERSE_CONTINUE; | |
10427 | } | |
10428 | return Expression::traverse(&this->call_, traverse); | |
10429 | } | |
10430 | ||
10431 | // Get the type. | |
10432 | ||
10433 | Type* | |
10434 | Call_result_expression::do_type() | |
10435 | { | |
425dd051 | 10436 | if (this->classification() == EXPRESSION_ERROR) |
10437 | return Type::make_error_type(); | |
10438 | ||
e440a328 | 10439 | // THIS->CALL_ can be replaced with a temporary reference due to |
10440 | // Call_expression::do_must_eval_in_order when there is an error. | |
10441 | Call_expression* ce = this->call_->call_expression(); | |
10442 | if (ce == NULL) | |
5e85f268 | 10443 | { |
10444 | this->set_is_error(); | |
10445 | return Type::make_error_type(); | |
10446 | } | |
e440a328 | 10447 | Function_type* fntype = ce->get_function_type(); |
10448 | if (fntype == NULL) | |
5e85f268 | 10449 | { |
e37658e2 | 10450 | if (ce->issue_error()) |
99b3f06f | 10451 | { |
10452 | if (!ce->fn()->type()->is_error()) | |
10453 | this->report_error(_("expected function")); | |
10454 | } | |
5e85f268 | 10455 | this->set_is_error(); |
10456 | return Type::make_error_type(); | |
10457 | } | |
e440a328 | 10458 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10459 | if (results == NULL || results->size() < 2) |
7b8d861f | 10460 | { |
ceeb4318 | 10461 | if (ce->issue_error()) |
10462 | this->report_error(_("number of results does not match " | |
10463 | "number of values")); | |
7b8d861f | 10464 | return Type::make_error_type(); |
10465 | } | |
e440a328 | 10466 | Typed_identifier_list::const_iterator pr = results->begin(); |
10467 | for (unsigned int i = 0; i < this->index_; ++i) | |
10468 | { | |
10469 | if (pr == results->end()) | |
425dd051 | 10470 | break; |
e440a328 | 10471 | ++pr; |
10472 | } | |
10473 | if (pr == results->end()) | |
425dd051 | 10474 | { |
ceeb4318 | 10475 | if (ce->issue_error()) |
10476 | this->report_error(_("number of results does not match " | |
10477 | "number of values")); | |
425dd051 | 10478 | return Type::make_error_type(); |
10479 | } | |
e440a328 | 10480 | return pr->type(); |
10481 | } | |
10482 | ||
425dd051 | 10483 | // Check the type. Just make sure that we trigger the warning in |
10484 | // do_type. | |
e440a328 | 10485 | |
10486 | void | |
10487 | Call_result_expression::do_check_types(Gogo*) | |
10488 | { | |
425dd051 | 10489 | this->type(); |
e440a328 | 10490 | } |
10491 | ||
10492 | // Determine the type. We have nothing to do here, but the 0 result | |
10493 | // needs to pass down to the caller. | |
10494 | ||
10495 | void | |
10496 | Call_result_expression::do_determine_type(const Type_context*) | |
10497 | { | |
fb94b0ca | 10498 | this->call_->determine_type_no_context(); |
e440a328 | 10499 | } |
10500 | ||
ea664253 | 10501 | // Return the backend representation. We just refer to the temporary set by the |
10502 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 10503 | // hard to evaluate the call at the right time. |
e440a328 | 10504 | |
ea664253 | 10505 | Bexpression* |
10506 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10507 | { |
ceeb4318 | 10508 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10509 | if (ce == NULL) |
10510 | { | |
10511 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 10512 | return context->backend()->error_expression(); |
cd238b8d | 10513 | } |
5731103c | 10514 | Temporary_statement* ts = ce->results(); |
cd238b8d | 10515 | if (ts == NULL) |
10516 | { | |
10517 | go_assert(saw_errors()); | |
ea664253 | 10518 | return context->backend()->error_expression(); |
cd238b8d | 10519 | } |
ceeb4318 | 10520 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
5731103c | 10521 | ref = Expression::make_field_reference(ref, this->index_, this->location()); |
ea664253 | 10522 | return ref->get_backend(context); |
e440a328 | 10523 | } |
10524 | ||
d751bb78 | 10525 | // Dump ast representation for a call result expression. |
10526 | ||
10527 | void | |
10528 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10529 | const | |
10530 | { | |
10531 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10532 | // (struct) and the fields are referenced instead. | |
10533 | ast_dump_context->ostream() << this->index_ << "@("; | |
10534 | ast_dump_context->dump_expression(this->call_); | |
10535 | ast_dump_context->ostream() << ")"; | |
10536 | } | |
10537 | ||
e440a328 | 10538 | // Make a reference to a single result of a call which returns |
10539 | // multiple results. | |
10540 | ||
10541 | Expression* | |
10542 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10543 | { | |
10544 | return new Call_result_expression(call, index); | |
10545 | } | |
10546 | ||
10547 | // Class Index_expression. | |
10548 | ||
10549 | // Traversal. | |
10550 | ||
10551 | int | |
10552 | Index_expression::do_traverse(Traverse* traverse) | |
10553 | { | |
10554 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10555 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10556 | || (this->end_ != NULL | |
acf2b673 | 10557 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
10558 | || (this->cap_ != NULL | |
10559 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 10560 | return TRAVERSE_EXIT; |
10561 | return TRAVERSE_CONTINUE; | |
10562 | } | |
10563 | ||
10564 | // Lower an index expression. This converts the generic index | |
10565 | // expression into an array index, a string index, or a map index. | |
10566 | ||
10567 | Expression* | |
ceeb4318 | 10568 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10569 | { |
b13c66cd | 10570 | Location location = this->location(); |
e440a328 | 10571 | Expression* left = this->left_; |
10572 | Expression* start = this->start_; | |
10573 | Expression* end = this->end_; | |
acf2b673 | 10574 | Expression* cap = this->cap_; |
e440a328 | 10575 | |
10576 | Type* type = left->type(); | |
5c13bd80 | 10577 | if (type->is_error()) |
d9f3743a | 10578 | { |
10579 | go_assert(saw_errors()); | |
10580 | return Expression::make_error(location); | |
10581 | } | |
b0cf7ddd | 10582 | else if (left->is_type_expression()) |
10583 | { | |
631d5788 | 10584 | go_error_at(location, "attempt to index type expression"); |
b0cf7ddd | 10585 | return Expression::make_error(location); |
10586 | } | |
e440a328 | 10587 | else if (type->array_type() != NULL) |
acf2b673 | 10588 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 10589 | else if (type->points_to() != NULL |
10590 | && type->points_to()->array_type() != NULL | |
411eb89e | 10591 | && !type->points_to()->is_slice_type()) |
e440a328 | 10592 | { |
f614ea8b | 10593 | Expression* deref = |
10594 | Expression::make_dereference(left, NIL_CHECK_DEFAULT, location); | |
38092374 | 10595 | |
10596 | // For an ordinary index into the array, the pointer will be | |
10597 | // dereferenced. For a slice it will not--the resulting slice | |
10598 | // will simply reuse the pointer, which is incorrect if that | |
10599 | // pointer is nil. | |
10600 | if (end != NULL || cap != NULL) | |
10601 | deref->issue_nil_check(); | |
10602 | ||
acf2b673 | 10603 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 10604 | } |
10605 | else if (type->is_string_type()) | |
acf2b673 | 10606 | { |
10607 | if (cap != NULL) | |
10608 | { | |
631d5788 | 10609 | go_error_at(location, "invalid 3-index slice of string"); |
acf2b673 | 10610 | return Expression::make_error(location); |
10611 | } | |
10612 | return Expression::make_string_index(left, start, end, location); | |
10613 | } | |
e440a328 | 10614 | else if (type->map_type() != NULL) |
10615 | { | |
acf2b673 | 10616 | if (end != NULL || cap != NULL) |
e440a328 | 10617 | { |
631d5788 | 10618 | go_error_at(location, "invalid slice of map"); |
e440a328 | 10619 | return Expression::make_error(location); |
10620 | } | |
0d5530d9 | 10621 | return Expression::make_map_index(left, start, location); |
e440a328 | 10622 | } |
b1aba207 | 10623 | else if (cap != NULL) |
10624 | { | |
10625 | go_error_at(location, | |
10626 | "invalid 3-index slice of object that is not a slice"); | |
10627 | return Expression::make_error(location); | |
10628 | } | |
10629 | else if (end != NULL) | |
10630 | { | |
10631 | go_error_at(location, | |
10632 | ("attempt to slice object that is not " | |
10633 | "array, slice, or string")); | |
10634 | return Expression::make_error(location); | |
10635 | } | |
e440a328 | 10636 | else |
10637 | { | |
631d5788 | 10638 | go_error_at(location, |
b1aba207 | 10639 | ("attempt to index object that is not " |
10640 | "array, slice, string, or map")); | |
e440a328 | 10641 | return Expression::make_error(location); |
10642 | } | |
10643 | } | |
10644 | ||
acf2b673 | 10645 | // Write an indexed expression |
10646 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 10647 | |
10648 | void | |
10649 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10650 | const Expression* expr, | |
10651 | const Expression* start, | |
acf2b673 | 10652 | const Expression* end, |
10653 | const Expression* cap) | |
d751bb78 | 10654 | { |
10655 | expr->dump_expression(ast_dump_context); | |
10656 | ast_dump_context->ostream() << "["; | |
10657 | start->dump_expression(ast_dump_context); | |
10658 | if (end != NULL) | |
10659 | { | |
10660 | ast_dump_context->ostream() << ":"; | |
10661 | end->dump_expression(ast_dump_context); | |
10662 | } | |
acf2b673 | 10663 | if (cap != NULL) |
10664 | { | |
10665 | ast_dump_context->ostream() << ":"; | |
10666 | cap->dump_expression(ast_dump_context); | |
10667 | } | |
d751bb78 | 10668 | ast_dump_context->ostream() << "]"; |
10669 | } | |
10670 | ||
10671 | // Dump ast representation for an index expression. | |
10672 | ||
10673 | void | |
10674 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10675 | const | |
10676 | { | |
10677 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 10678 | this->start_, this->end_, this->cap_); |
d751bb78 | 10679 | } |
10680 | ||
e440a328 | 10681 | // Make an index expression. |
10682 | ||
10683 | Expression* | |
10684 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 10685 | Expression* cap, Location location) |
e440a328 | 10686 | { |
acf2b673 | 10687 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 10688 | } |
10689 | ||
da244e59 | 10690 | // Class Array_index_expression. |
e440a328 | 10691 | |
10692 | // Array index traversal. | |
10693 | ||
10694 | int | |
10695 | Array_index_expression::do_traverse(Traverse* traverse) | |
10696 | { | |
10697 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10698 | return TRAVERSE_EXIT; | |
10699 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10700 | return TRAVERSE_EXIT; | |
10701 | if (this->end_ != NULL) | |
10702 | { | |
10703 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10704 | return TRAVERSE_EXIT; | |
10705 | } | |
acf2b673 | 10706 | if (this->cap_ != NULL) |
10707 | { | |
10708 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10709 | return TRAVERSE_EXIT; | |
10710 | } | |
e440a328 | 10711 | return TRAVERSE_CONTINUE; |
10712 | } | |
10713 | ||
10714 | // Return the type of an array index. | |
10715 | ||
10716 | Type* | |
10717 | Array_index_expression::do_type() | |
10718 | { | |
10719 | if (this->type_ == NULL) | |
10720 | { | |
10721 | Array_type* type = this->array_->type()->array_type(); | |
10722 | if (type == NULL) | |
10723 | this->type_ = Type::make_error_type(); | |
10724 | else if (this->end_ == NULL) | |
10725 | this->type_ = type->element_type(); | |
411eb89e | 10726 | else if (type->is_slice_type()) |
e440a328 | 10727 | { |
10728 | // A slice of a slice has the same type as the original | |
10729 | // slice. | |
10730 | this->type_ = this->array_->type()->deref(); | |
10731 | } | |
10732 | else | |
10733 | { | |
10734 | // A slice of an array is a slice. | |
10735 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10736 | } | |
10737 | } | |
10738 | return this->type_; | |
10739 | } | |
10740 | ||
10741 | // Set the type of an array index. | |
10742 | ||
10743 | void | |
10744 | Array_index_expression::do_determine_type(const Type_context*) | |
10745 | { | |
10746 | this->array_->determine_type_no_context(); | |
f77aa642 | 10747 | |
10748 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10749 | if (this->start_->is_constant()) | |
10750 | this->start_->determine_type(&index_context); | |
10751 | else | |
10752 | this->start_->determine_type_no_context(); | |
e440a328 | 10753 | if (this->end_ != NULL) |
f77aa642 | 10754 | { |
10755 | if (this->end_->is_constant()) | |
10756 | this->end_->determine_type(&index_context); | |
10757 | else | |
10758 | this->end_->determine_type_no_context(); | |
10759 | } | |
acf2b673 | 10760 | if (this->cap_ != NULL) |
f77aa642 | 10761 | { |
10762 | if (this->cap_->is_constant()) | |
10763 | this->cap_->determine_type(&index_context); | |
10764 | else | |
10765 | this->cap_->determine_type_no_context(); | |
10766 | } | |
e440a328 | 10767 | } |
10768 | ||
10769 | // Check types of an array index. | |
10770 | ||
10771 | void | |
b7327dbf | 10772 | Array_index_expression::do_check_types(Gogo*) |
e440a328 | 10773 | { |
f6bc81e6 | 10774 | Numeric_constant nc; |
10775 | unsigned long v; | |
10776 | if (this->start_->type()->integer_type() == NULL | |
10777 | && !this->start_->type()->is_error() | |
10778 | && (!this->start_->numeric_constant_value(&nc) | |
10779 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10780 | this->report_error(_("index must be integer")); |
10781 | if (this->end_ != NULL | |
10782 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10783 | && !this->end_->type()->is_error() |
10784 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10785 | && !this->end_->is_error_expression() |
10786 | && (!this->end_->numeric_constant_value(&nc) | |
10787 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10788 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10789 | if (this->cap_ != NULL |
10790 | && this->cap_->type()->integer_type() == NULL | |
10791 | && !this->cap_->type()->is_error() | |
10792 | && !this->cap_->is_nil_expression() | |
10793 | && !this->cap_->is_error_expression() | |
10794 | && (!this->cap_->numeric_constant_value(&nc) | |
10795 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10796 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10797 | |
10798 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10799 | if (array_type == NULL) |
10800 | { | |
c484d925 | 10801 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10802 | return; |
10803 | } | |
e440a328 | 10804 | |
10805 | unsigned int int_bits = | |
10806 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10807 | ||
0c77715b | 10808 | Numeric_constant lvalnc; |
e440a328 | 10809 | mpz_t lval; |
e440a328 | 10810 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10811 | && array_type->length()->numeric_constant_value(&lvalnc) |
10812 | && lvalnc.to_int(&lval)); | |
10813 | Numeric_constant inc; | |
e440a328 | 10814 | mpz_t ival; |
0bd5d859 | 10815 | bool ival_valid = false; |
0c77715b | 10816 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10817 | { |
0bd5d859 | 10818 | ival_valid = true; |
e440a328 | 10819 | if (mpz_sgn(ival) < 0 |
10820 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10821 | || (lval_valid | |
10822 | && (this->end_ == NULL | |
10823 | ? mpz_cmp(ival, lval) >= 0 | |
10824 | : mpz_cmp(ival, lval) > 0))) | |
10825 | { | |
631d5788 | 10826 | go_error_at(this->start_->location(), "array index out of bounds"); |
e440a328 | 10827 | this->set_is_error(); |
10828 | } | |
10829 | } | |
10830 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10831 | { | |
0c77715b | 10832 | Numeric_constant enc; |
10833 | mpz_t eval; | |
acf2b673 | 10834 | bool eval_valid = false; |
0c77715b | 10835 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10836 | { |
acf2b673 | 10837 | eval_valid = true; |
0c77715b | 10838 | if (mpz_sgn(eval) < 0 |
10839 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10840 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10841 | { |
631d5788 | 10842 | go_error_at(this->end_->location(), "array index out of bounds"); |
e440a328 | 10843 | this->set_is_error(); |
10844 | } | |
0bd5d859 | 10845 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10846 | this->report_error(_("inverted slice range")); | |
e440a328 | 10847 | } |
acf2b673 | 10848 | |
10849 | Numeric_constant cnc; | |
10850 | mpz_t cval; | |
10851 | if (this->cap_ != NULL | |
10852 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10853 | { | |
10854 | if (mpz_sgn(cval) < 0 | |
10855 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10856 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10857 | { | |
631d5788 | 10858 | go_error_at(this->cap_->location(), "array index out of bounds"); |
acf2b673 | 10859 | this->set_is_error(); |
10860 | } | |
10861 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10862 | { | |
631d5788 | 10863 | go_error_at(this->cap_->location(), |
10864 | "invalid slice index: capacity less than start"); | |
acf2b673 | 10865 | this->set_is_error(); |
10866 | } | |
10867 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10868 | { | |
631d5788 | 10869 | go_error_at(this->cap_->location(), |
10870 | "invalid slice index: capacity less than length"); | |
acf2b673 | 10871 | this->set_is_error(); |
10872 | } | |
10873 | mpz_clear(cval); | |
10874 | } | |
10875 | ||
10876 | if (eval_valid) | |
10877 | mpz_clear(eval); | |
e440a328 | 10878 | } |
0bd5d859 | 10879 | if (ival_valid) |
10880 | mpz_clear(ival); | |
0c77715b | 10881 | if (lval_valid) |
10882 | mpz_clear(lval); | |
e440a328 | 10883 | |
10884 | // A slice of an array requires an addressable array. A slice of a | |
10885 | // slice is always possible. | |
411eb89e | 10886 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10887 | { |
10888 | if (!this->array_->is_addressable()) | |
8da39c3b | 10889 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10890 | else |
b7327dbf | 10891 | // Set the array address taken but not escape. The escape |
10892 | // analysis will make it escape to heap when needed. | |
10893 | this->array_->address_taken(false); | |
88ec30c8 | 10894 | } |
e440a328 | 10895 | } |
10896 | ||
71a38860 | 10897 | // The subexpressions of an array index must be evaluated in order. |
10898 | // If this is indexing into an array, rather than a slice, then only | |
10899 | // the index should be evaluated. Since this is called for values on | |
10900 | // the left hand side of an assigment, evaluating the array, meaning | |
10901 | // copying the array, will cause a different array to be modified. | |
10902 | ||
10903 | bool | |
10904 | Array_index_expression::do_must_eval_subexpressions_in_order( | |
10905 | int* skip) const | |
10906 | { | |
10907 | *skip = this->array_->type()->is_slice_type() ? 0 : 1; | |
10908 | return true; | |
10909 | } | |
10910 | ||
2c809f8f | 10911 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10912 | |
10913 | Expression* | |
10914 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10915 | Statement_inserter* inserter) | |
10916 | { | |
10917 | Location loc = this->location(); | |
5bf8be8b | 10918 | Expression* array = this->array_; |
10919 | Expression* start = this->start_; | |
10920 | Expression* end = this->end_; | |
10921 | Expression* cap = this->cap_; | |
10922 | if (array->is_error_expression() | |
10923 | || array->type()->is_error_type() | |
10924 | || start->is_error_expression() | |
10925 | || start->type()->is_error_type() | |
10926 | || (end != NULL | |
10927 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10928 | || (cap != NULL | |
10929 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10930 | { | |
10931 | go_assert(saw_errors()); | |
10932 | return Expression::make_error(loc); | |
10933 | } | |
10934 | ||
2c809f8f | 10935 | Temporary_statement* temp; |
5bf8be8b | 10936 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10937 | { |
5bf8be8b | 10938 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10939 | inserter->insert(temp); |
10940 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10941 | } | |
5bf8be8b | 10942 | if (!start->is_variable()) |
2c809f8f | 10943 | { |
5bf8be8b | 10944 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10945 | inserter->insert(temp); |
10946 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10947 | } | |
5bf8be8b | 10948 | if (end != NULL |
10949 | && !end->is_nil_expression() | |
10950 | && !end->is_variable()) | |
2c809f8f | 10951 | { |
5bf8be8b | 10952 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10953 | inserter->insert(temp); |
10954 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10955 | } | |
03118c21 | 10956 | if (cap != NULL && !cap->is_variable()) |
2c809f8f | 10957 | { |
5bf8be8b | 10958 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10959 | inserter->insert(temp); |
10960 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10961 | } | |
10962 | ||
35a54f17 | 10963 | return this; |
10964 | } | |
10965 | ||
e440a328 | 10966 | // Return whether this expression is addressable. |
10967 | ||
10968 | bool | |
10969 | Array_index_expression::do_is_addressable() const | |
10970 | { | |
10971 | // A slice expression is not addressable. | |
10972 | if (this->end_ != NULL) | |
10973 | return false; | |
10974 | ||
10975 | // An index into a slice is addressable. | |
411eb89e | 10976 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10977 | return true; |
10978 | ||
10979 | // An index into an array is addressable if the array is | |
10980 | // addressable. | |
10981 | return this->array_->is_addressable(); | |
10982 | } | |
10983 | ||
bf1323be | 10984 | void |
10985 | Array_index_expression::do_address_taken(bool escapes) | |
10986 | { | |
10987 | // In &x[0], if x is a slice, then x's address is not taken. | |
10988 | if (!this->array_->type()->is_slice_type()) | |
10989 | this->array_->address_taken(escapes); | |
10990 | } | |
10991 | ||
ea664253 | 10992 | // Get the backend representation for an array index. |
e440a328 | 10993 | |
ea664253 | 10994 | Bexpression* |
10995 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10996 | { |
e440a328 | 10997 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10998 | if (array_type == NULL) |
10999 | { | |
c484d925 | 11000 | go_assert(this->array_->type()->is_error()); |
ea664253 | 11001 | return context->backend()->error_expression(); |
d8cd8e2d | 11002 | } |
35a54f17 | 11003 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 11004 | |
2c809f8f | 11005 | Location loc = this->location(); |
11006 | Gogo* gogo = context->gogo(); | |
11007 | ||
6dfedc16 | 11008 | Type* int_type = Type::lookup_integer_type("int"); |
11009 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 11010 | |
2c809f8f | 11011 | // We need to convert the length and capacity to the Go "int" type here |
11012 | // because the length of a fixed-length array could be of type "uintptr" | |
11013 | // and gimple disallows binary operations between "uintptr" and other | |
11014 | // integer types. FIXME. | |
11015 | Bexpression* length = NULL; | |
a04bfdfc | 11016 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
11017 | { | |
35a54f17 | 11018 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 11019 | length = len->get_backend(context); |
2c809f8f | 11020 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 11021 | } |
11022 | ||
2c809f8f | 11023 | Bexpression* capacity = NULL; |
a04bfdfc | 11024 | if (this->end_ != NULL) |
11025 | { | |
35a54f17 | 11026 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 11027 | capacity = cap->get_backend(context); |
2c809f8f | 11028 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 11029 | } |
11030 | ||
2c809f8f | 11031 | Bexpression* cap_arg = capacity; |
acf2b673 | 11032 | if (this->cap_ != NULL) |
11033 | { | |
ea664253 | 11034 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 11035 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 11036 | } |
11037 | ||
2c809f8f | 11038 | if (length == NULL) |
11039 | length = cap_arg; | |
e440a328 | 11040 | |
11041 | int code = (array_type->length() != NULL | |
11042 | ? (this->end_ == NULL | |
11043 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
11044 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
11045 | : (this->end_ == NULL | |
11046 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
11047 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 11048 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 11049 | |
6dfedc16 | 11050 | if (this->start_->type()->integer_type() == NULL |
11051 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
11052 | { | |
11053 | go_assert(saw_errors()); | |
11054 | return context->backend()->error_expression(); | |
11055 | } | |
d9f3743a | 11056 | |
ea664253 | 11057 | Bexpression* bad_index = |
d9f3743a | 11058 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 11059 | |
ea664253 | 11060 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 11061 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
11062 | Bexpression* start_too_large = | |
11063 | gogo->backend()->binary_expression((this->end_ == NULL | |
11064 | ? OPERATOR_GE | |
11065 | : OPERATOR_GT), | |
11066 | start, | |
11067 | (this->end_ == NULL | |
11068 | ? length | |
11069 | : capacity), | |
11070 | loc); | |
11071 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
11072 | bad_index, loc); | |
e440a328 | 11073 | |
93715b75 | 11074 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 11075 | if (this->end_ == NULL) |
11076 | { | |
11077 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 11078 | // wrap the index check into START. |
11079 | start = | |
93715b75 | 11080 | gogo->backend()->conditional_expression(bfn, int_btype, bad_index, |
2c809f8f | 11081 | crash, start, loc); |
e440a328 | 11082 | |
2c809f8f | 11083 | Bexpression* ret; |
e440a328 | 11084 | if (array_type->length() != NULL) |
11085 | { | |
ea664253 | 11086 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 11087 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 11088 | } |
11089 | else | |
11090 | { | |
2c809f8f | 11091 | // Slice. |
11092 | Expression* valptr = | |
44dbe1d7 | 11093 | array_type->get_value_pointer(gogo, this->array_, |
11094 | this->is_lvalue_); | |
ea664253 | 11095 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 11096 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 11097 | |
11098 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
11099 | Btype* ele_btype = ele_type->get_backend(gogo); | |
11100 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 11101 | } |
ea664253 | 11102 | return ret; |
e440a328 | 11103 | } |
11104 | ||
11105 | // Array slice. | |
11106 | ||
acf2b673 | 11107 | if (this->cap_ != NULL) |
11108 | { | |
2c809f8f | 11109 | Bexpression* bounds_bcheck = |
ea664253 | 11110 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 11111 | bad_index = |
11112 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11113 | bad_index, loc); | |
11114 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
11115 | ||
11116 | Bexpression* cap_too_small = | |
11117 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
11118 | Bexpression* cap_too_large = | |
11119 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
11120 | Bexpression* bad_cap = | |
11121 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
11122 | cap_too_large, loc); | |
11123 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
11124 | bad_index, loc); | |
11125 | } | |
11126 | ||
11127 | Bexpression* end; | |
e440a328 | 11128 | if (this->end_->is_nil_expression()) |
2c809f8f | 11129 | end = length; |
e440a328 | 11130 | else |
11131 | { | |
2c809f8f | 11132 | Bexpression* bounds_bcheck = |
ea664253 | 11133 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 11134 | |
2c809f8f | 11135 | bad_index = |
11136 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11137 | bad_index, loc); | |
e440a328 | 11138 | |
ea664253 | 11139 | end = this->end_->get_backend(context); |
2c809f8f | 11140 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
11141 | Bexpression* end_too_small = | |
11142 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
11143 | Bexpression* end_too_large = | |
11144 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
11145 | Bexpression* bad_end = | |
11146 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
11147 | end_too_large, loc); | |
11148 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
11149 | bad_index, loc); | |
e440a328 | 11150 | } |
11151 | ||
2c809f8f | 11152 | Bexpression* result_length = |
11153 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 11154 | |
2c809f8f | 11155 | Bexpression* result_capacity = |
11156 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 11157 | |
03118c21 | 11158 | // If the new capacity is zero, don't change val. Otherwise we can |
11159 | // get a pointer to the next object in memory, keeping it live | |
11160 | // unnecessarily. When the capacity is zero, the actual pointer | |
11161 | // value doesn't matter. | |
11162 | Bexpression* zero = | |
11163 | Expression::make_integer_ul(0, int_type, loc)->get_backend(context); | |
11164 | Bexpression* cond = | |
11165 | gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero, | |
11166 | loc); | |
11167 | Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype, | |
11168 | cond, zero, | |
11169 | start, loc); | |
44dbe1d7 | 11170 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_, |
11171 | this->is_lvalue_); | |
03118c21 | 11172 | Bexpression* val = valptr->get_backend(context); |
11173 | val = gogo->backend()->pointer_offset_expression(val, offset, loc); | |
11174 | ||
2c809f8f | 11175 | Btype* struct_btype = this->type()->get_backend(gogo); |
11176 | std::vector<Bexpression*> init; | |
11177 | init.push_back(val); | |
11178 | init.push_back(result_length); | |
11179 | init.push_back(result_capacity); | |
e440a328 | 11180 | |
2c809f8f | 11181 | Bexpression* ctor = |
11182 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
93715b75 | 11183 | return gogo->backend()->conditional_expression(bfn, struct_btype, bad_index, |
ea664253 | 11184 | crash, ctor, loc); |
e440a328 | 11185 | } |
11186 | ||
d751bb78 | 11187 | // Dump ast representation for an array index expression. |
11188 | ||
11189 | void | |
11190 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11191 | const | |
11192 | { | |
11193 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 11194 | this->start_, this->end_, this->cap_); |
d751bb78 | 11195 | } |
11196 | ||
acf2b673 | 11197 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 11198 | |
11199 | Expression* | |
11200 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 11201 | Expression* end, Expression* cap, |
11202 | Location location) | |
e440a328 | 11203 | { |
acf2b673 | 11204 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 11205 | } |
11206 | ||
50075d74 | 11207 | // Class String_index_expression. |
e440a328 | 11208 | |
11209 | // String index traversal. | |
11210 | ||
11211 | int | |
11212 | String_index_expression::do_traverse(Traverse* traverse) | |
11213 | { | |
11214 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
11215 | return TRAVERSE_EXIT; | |
11216 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
11217 | return TRAVERSE_EXIT; | |
11218 | if (this->end_ != NULL) | |
11219 | { | |
11220 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
11221 | return TRAVERSE_EXIT; | |
11222 | } | |
11223 | return TRAVERSE_CONTINUE; | |
11224 | } | |
11225 | ||
2c809f8f | 11226 | Expression* |
11227 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
11228 | Statement_inserter* inserter) | |
e440a328 | 11229 | { |
2c809f8f | 11230 | Location loc = this->location(); |
5bf8be8b | 11231 | Expression* string = this->string_; |
11232 | Expression* start = this->start_; | |
11233 | Expression* end = this->end_; | |
11234 | if (string->is_error_expression() | |
11235 | || string->type()->is_error_type() | |
11236 | || start->is_error_expression() | |
11237 | || start->type()->is_error_type() | |
11238 | || (end != NULL | |
11239 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
11240 | { | |
11241 | go_assert(saw_errors()); | |
11242 | return Expression::make_error(loc); | |
11243 | } | |
11244 | ||
11245 | Temporary_statement* temp; | |
2c809f8f | 11246 | if (!this->string_->is_variable()) |
11247 | { | |
11248 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
11249 | inserter->insert(temp); | |
11250 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
11251 | } | |
11252 | if (!this->start_->is_variable()) | |
11253 | { | |
11254 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
11255 | inserter->insert(temp); | |
11256 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
11257 | } | |
11258 | if (this->end_ != NULL | |
11259 | && !this->end_->is_nil_expression() | |
11260 | && !this->end_->is_variable()) | |
11261 | { | |
11262 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
11263 | inserter->insert(temp); | |
11264 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
11265 | } | |
11266 | ||
11267 | return this; | |
11268 | } | |
11269 | ||
11270 | // Return the type of a string index. | |
11271 | ||
11272 | Type* | |
11273 | String_index_expression::do_type() | |
11274 | { | |
11275 | if (this->end_ == NULL) | |
11276 | return Type::lookup_integer_type("uint8"); | |
11277 | else | |
11278 | return this->string_->type(); | |
11279 | } | |
11280 | ||
11281 | // Determine the type of a string index. | |
11282 | ||
11283 | void | |
11284 | String_index_expression::do_determine_type(const Type_context*) | |
11285 | { | |
11286 | this->string_->determine_type_no_context(); | |
f77aa642 | 11287 | |
11288 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
11289 | if (this->start_->is_constant()) | |
11290 | this->start_->determine_type(&index_context); | |
11291 | else | |
11292 | this->start_->determine_type_no_context(); | |
e440a328 | 11293 | if (this->end_ != NULL) |
f77aa642 | 11294 | { |
11295 | if (this->end_->is_constant()) | |
11296 | this->end_->determine_type(&index_context); | |
11297 | else | |
11298 | this->end_->determine_type_no_context(); | |
11299 | } | |
e440a328 | 11300 | } |
11301 | ||
11302 | // Check types of a string index. | |
11303 | ||
11304 | void | |
11305 | String_index_expression::do_check_types(Gogo*) | |
11306 | { | |
acdc230d | 11307 | Numeric_constant nc; |
11308 | unsigned long v; | |
11309 | if (this->start_->type()->integer_type() == NULL | |
11310 | && !this->start_->type()->is_error() | |
11311 | && (!this->start_->numeric_constant_value(&nc) | |
11312 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11313 | this->report_error(_("index must be integer")); |
11314 | if (this->end_ != NULL | |
11315 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 11316 | && !this->end_->type()->is_error() |
11317 | && !this->end_->is_nil_expression() | |
11318 | && !this->end_->is_error_expression() | |
11319 | && (!this->end_->numeric_constant_value(&nc) | |
11320 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11321 | this->report_error(_("slice end must be integer")); |
11322 | ||
11323 | std::string sval; | |
11324 | bool sval_valid = this->string_->string_constant_value(&sval); | |
11325 | ||
0c77715b | 11326 | Numeric_constant inc; |
e440a328 | 11327 | mpz_t ival; |
0bd5d859 | 11328 | bool ival_valid = false; |
0c77715b | 11329 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 11330 | { |
0bd5d859 | 11331 | ival_valid = true; |
e440a328 | 11332 | if (mpz_sgn(ival) < 0 |
b10f32fb | 11333 | || (sval_valid |
11334 | && (this->end_ == NULL | |
11335 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
11336 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 11337 | { |
631d5788 | 11338 | go_error_at(this->start_->location(), "string index out of bounds"); |
e440a328 | 11339 | this->set_is_error(); |
11340 | } | |
11341 | } | |
11342 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
11343 | { | |
0c77715b | 11344 | Numeric_constant enc; |
11345 | mpz_t eval; | |
11346 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 11347 | { |
0c77715b | 11348 | if (mpz_sgn(eval) < 0 |
11349 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 11350 | { |
631d5788 | 11351 | go_error_at(this->end_->location(), "string index out of bounds"); |
e440a328 | 11352 | this->set_is_error(); |
11353 | } | |
0bd5d859 | 11354 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
11355 | this->report_error(_("inverted slice range")); | |
0c77715b | 11356 | mpz_clear(eval); |
e440a328 | 11357 | } |
11358 | } | |
0bd5d859 | 11359 | if (ival_valid) |
11360 | mpz_clear(ival); | |
e440a328 | 11361 | } |
11362 | ||
ea664253 | 11363 | // Get the backend representation for a string index. |
e440a328 | 11364 | |
ea664253 | 11365 | Bexpression* |
11366 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11367 | { |
b13c66cd | 11368 | Location loc = this->location(); |
2c809f8f | 11369 | Expression* string_arg = this->string_; |
11370 | if (this->string_->type()->points_to() != NULL) | |
f614ea8b | 11371 | string_arg = Expression::make_dereference(this->string_, |
11372 | NIL_CHECK_NOT_NEEDED, loc); | |
e440a328 | 11373 | |
2c809f8f | 11374 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 11375 | |
2c809f8f | 11376 | int code = (this->end_ == NULL |
11377 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
11378 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 11379 | |
2c809f8f | 11380 | Gogo* gogo = context->gogo(); |
ea664253 | 11381 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 11382 | |
11383 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 11384 | |
2c809f8f | 11385 | // It is possible that an error occurred earlier because the start index |
11386 | // cannot be represented as an integer type. In this case, we shouldn't | |
11387 | // try casting the starting index into an integer since | |
11388 | // Type_conversion_expression will fail to get the backend representation. | |
11389 | // FIXME. | |
11390 | if (this->start_->type()->integer_type() == NULL | |
11391 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
11392 | { | |
11393 | go_assert(saw_errors()); | |
ea664253 | 11394 | return context->backend()->error_expression(); |
2c809f8f | 11395 | } |
e440a328 | 11396 | |
2c809f8f | 11397 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
93715b75 | 11398 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 11399 | |
2c809f8f | 11400 | if (this->end_ == NULL) |
11401 | { | |
11402 | Expression* length = | |
11403 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 11404 | |
2c809f8f | 11405 | Expression* start_too_large = |
11406 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
11407 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
11408 | bad_index, loc); | |
11409 | Expression* bytes = | |
11410 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 11411 | |
ea664253 | 11412 | Bexpression* bstart = start->get_backend(context); |
11413 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 11414 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 11415 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
11416 | Bexpression* index = | |
11417 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 11418 | |
2c809f8f | 11419 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 11420 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11421 | return gogo->backend()->conditional_expression(bfn, byte_btype, |
11422 | index_error, crash, | |
11423 | index, loc); | |
2c809f8f | 11424 | } |
11425 | ||
11426 | Expression* end = NULL; | |
11427 | if (this->end_->is_nil_expression()) | |
e67508fa | 11428 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 11429 | else |
11430 | { | |
2c809f8f | 11431 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
11432 | bad_index = | |
11433 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
11434 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 11435 | } |
2c809f8f | 11436 | |
11437 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
11438 | string_arg, start, end); | |
ea664253 | 11439 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 11440 | |
11441 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 11442 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11443 | return gogo->backend()->conditional_expression(bfn, str_btype, index_error, |
ea664253 | 11444 | crash, bstrslice, loc); |
e440a328 | 11445 | } |
11446 | ||
d751bb78 | 11447 | // Dump ast representation for a string index expression. |
11448 | ||
11449 | void | |
11450 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11451 | const | |
11452 | { | |
acf2b673 | 11453 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
11454 | this->start_, this->end_, NULL); | |
d751bb78 | 11455 | } |
11456 | ||
e440a328 | 11457 | // Make a string index expression. END may be NULL. |
11458 | ||
11459 | Expression* | |
11460 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 11461 | Expression* end, Location location) |
e440a328 | 11462 | { |
11463 | return new String_index_expression(string, start, end, location); | |
11464 | } | |
11465 | ||
11466 | // Class Map_index. | |
11467 | ||
11468 | // Get the type of the map. | |
11469 | ||
11470 | Map_type* | |
11471 | Map_index_expression::get_map_type() const | |
11472 | { | |
0d5530d9 | 11473 | Map_type* mt = this->map_->type()->map_type(); |
c7524fae | 11474 | if (mt == NULL) |
c484d925 | 11475 | go_assert(saw_errors()); |
e440a328 | 11476 | return mt; |
11477 | } | |
11478 | ||
11479 | // Map index traversal. | |
11480 | ||
11481 | int | |
11482 | Map_index_expression::do_traverse(Traverse* traverse) | |
11483 | { | |
11484 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11485 | return TRAVERSE_EXIT; | |
11486 | return Expression::traverse(&this->index_, traverse); | |
11487 | } | |
11488 | ||
2c809f8f | 11489 | // We need to pass in a pointer to the key, so flatten the index into a |
11490 | // temporary variable if it isn't already. The value pointer will be | |
11491 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
11492 | // recomputation. | |
11493 | ||
11494 | Expression* | |
91c0fd76 | 11495 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 11496 | Statement_inserter* inserter) |
11497 | { | |
91c0fd76 | 11498 | Location loc = this->location(); |
2c809f8f | 11499 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 11500 | if (this->index()->is_error_expression() |
11501 | || this->index()->type()->is_error_type() | |
11502 | || mt->is_error_type()) | |
11503 | { | |
11504 | go_assert(saw_errors()); | |
11505 | return Expression::make_error(loc); | |
11506 | } | |
11507 | ||
91c0fd76 | 11508 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
11509 | { | |
11510 | if (this->index_->type()->interface_type() != NULL | |
11511 | && !this->index_->is_variable()) | |
11512 | { | |
11513 | Temporary_statement* temp = | |
11514 | Statement::make_temporary(NULL, this->index_, loc); | |
11515 | inserter->insert(temp); | |
11516 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
11517 | } | |
11518 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
11519 | this->index_, loc); | |
11520 | } | |
2c809f8f | 11521 | |
11522 | if (!this->index_->is_variable()) | |
11523 | { | |
11524 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 11525 | loc); |
2c809f8f | 11526 | inserter->insert(temp); |
91c0fd76 | 11527 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11528 | } |
11529 | ||
11530 | if (this->value_pointer_ == NULL) | |
0d5530d9 | 11531 | this->get_value_pointer(gogo); |
5bf8be8b | 11532 | if (this->value_pointer_->is_error_expression() |
11533 | || this->value_pointer_->type()->is_error_type()) | |
11534 | return Expression::make_error(loc); | |
2c809f8f | 11535 | if (!this->value_pointer_->is_variable()) |
11536 | { | |
11537 | Temporary_statement* temp = | |
91c0fd76 | 11538 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 11539 | inserter->insert(temp); |
91c0fd76 | 11540 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11541 | } |
11542 | ||
11543 | return this; | |
11544 | } | |
11545 | ||
e440a328 | 11546 | // Return the type of a map index. |
11547 | ||
11548 | Type* | |
11549 | Map_index_expression::do_type() | |
11550 | { | |
c7524fae | 11551 | Map_type* mt = this->get_map_type(); |
11552 | if (mt == NULL) | |
11553 | return Type::make_error_type(); | |
0d5530d9 | 11554 | return mt->val_type(); |
e440a328 | 11555 | } |
11556 | ||
11557 | // Fix the type of a map index. | |
11558 | ||
11559 | void | |
11560 | Map_index_expression::do_determine_type(const Type_context*) | |
11561 | { | |
11562 | this->map_->determine_type_no_context(); | |
c7524fae | 11563 | Map_type* mt = this->get_map_type(); |
11564 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11565 | Type_context subcontext(key_type, false); | |
e440a328 | 11566 | this->index_->determine_type(&subcontext); |
11567 | } | |
11568 | ||
11569 | // Check types of a map index. | |
11570 | ||
11571 | void | |
11572 | Map_index_expression::do_check_types(Gogo*) | |
11573 | { | |
11574 | std::string reason; | |
c7524fae | 11575 | Map_type* mt = this->get_map_type(); |
11576 | if (mt == NULL) | |
11577 | return; | |
11578 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11579 | { |
11580 | if (reason.empty()) | |
11581 | this->report_error(_("incompatible type for map index")); | |
11582 | else | |
11583 | { | |
631d5788 | 11584 | go_error_at(this->location(), "incompatible type for map index (%s)", |
11585 | reason.c_str()); | |
e440a328 | 11586 | this->set_is_error(); |
11587 | } | |
11588 | } | |
11589 | } | |
11590 | ||
ea664253 | 11591 | // Get the backend representation for a map index. |
e440a328 | 11592 | |
ea664253 | 11593 | Bexpression* |
11594 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11595 | { |
11596 | Map_type* type = this->get_map_type(); | |
c7524fae | 11597 | if (type == NULL) |
2c809f8f | 11598 | { |
11599 | go_assert(saw_errors()); | |
ea664253 | 11600 | return context->backend()->error_expression(); |
2c809f8f | 11601 | } |
e440a328 | 11602 | |
2c809f8f | 11603 | go_assert(this->value_pointer_ != NULL |
11604 | && this->value_pointer_->is_variable()); | |
e440a328 | 11605 | |
f614ea8b | 11606 | Expression* val = Expression::make_dereference(this->value_pointer_, |
11607 | NIL_CHECK_NOT_NEEDED, | |
11608 | this->location()); | |
0d5530d9 | 11609 | return val->get_backend(context); |
e440a328 | 11610 | } |
11611 | ||
0d5530d9 | 11612 | // Get an expression for the map index. This returns an expression |
11613 | // that evaluates to a pointer to a value. If the key is not in the | |
11614 | // map, the pointer will point to a zero value. | |
e440a328 | 11615 | |
2c809f8f | 11616 | Expression* |
0d5530d9 | 11617 | Map_index_expression::get_value_pointer(Gogo* gogo) |
e440a328 | 11618 | { |
2c809f8f | 11619 | if (this->value_pointer_ == NULL) |
746d2e73 | 11620 | { |
2c809f8f | 11621 | Map_type* type = this->get_map_type(); |
11622 | if (type == NULL) | |
746d2e73 | 11623 | { |
2c809f8f | 11624 | go_assert(saw_errors()); |
11625 | return Expression::make_error(this->location()); | |
746d2e73 | 11626 | } |
e440a328 | 11627 | |
2c809f8f | 11628 | Location loc = this->location(); |
11629 | Expression* map_ref = this->map_; | |
e440a328 | 11630 | |
0d5530d9 | 11631 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, |
11632 | this->index_, | |
2c809f8f | 11633 | loc); |
0d5530d9 | 11634 | |
11635 | Expression* zero = type->fat_zero_value(gogo); | |
11636 | ||
11637 | Expression* map_index; | |
11638 | ||
11639 | if (zero == NULL) | |
11640 | map_index = | |
11641 | Runtime::make_call(Runtime::MAPACCESS1, loc, 3, | |
11642 | Expression::make_type_descriptor(type, loc), | |
11643 | map_ref, index_ptr); | |
11644 | else | |
11645 | map_index = | |
11646 | Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, | |
11647 | Expression::make_type_descriptor(type, loc), | |
11648 | map_ref, index_ptr, zero); | |
2c809f8f | 11649 | |
11650 | Type* val_type = type->val_type(); | |
11651 | this->value_pointer_ = | |
11652 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
11653 | map_index, this->location()); | |
11654 | } | |
0d5530d9 | 11655 | |
2c809f8f | 11656 | return this->value_pointer_; |
e440a328 | 11657 | } |
11658 | ||
d751bb78 | 11659 | // Dump ast representation for a map index expression |
11660 | ||
11661 | void | |
11662 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11663 | const | |
11664 | { | |
acf2b673 | 11665 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
11666 | this->index_, NULL, NULL); | |
d751bb78 | 11667 | } |
11668 | ||
e440a328 | 11669 | // Make a map index expression. |
11670 | ||
11671 | Map_index_expression* | |
11672 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11673 | Location location) |
e440a328 | 11674 | { |
11675 | return new Map_index_expression(map, index, location); | |
11676 | } | |
11677 | ||
11678 | // Class Field_reference_expression. | |
11679 | ||
149eabc5 | 11680 | // Lower a field reference expression. There is nothing to lower, but |
11681 | // this is where we generate the tracking information for fields with | |
11682 | // the magic go:"track" tag. | |
11683 | ||
11684 | Expression* | |
11685 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11686 | Statement_inserter* inserter, int) | |
11687 | { | |
11688 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11689 | if (struct_type == NULL) | |
11690 | { | |
11691 | // Error will be reported elsewhere. | |
11692 | return this; | |
11693 | } | |
11694 | const Struct_field* field = struct_type->field(this->field_index_); | |
11695 | if (field == NULL) | |
11696 | return this; | |
11697 | if (!field->has_tag()) | |
11698 | return this; | |
11699 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11700 | return this; | |
11701 | ||
604e278d | 11702 | // References from functions generated by the compiler don't count. |
c6292d1d | 11703 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 11704 | return this; |
11705 | ||
149eabc5 | 11706 | // We have found a reference to a tracked field. Build a call to |
11707 | // the runtime function __go_fieldtrack with a string that describes | |
11708 | // the field. FIXME: We should only call this once per referenced | |
11709 | // field per function, not once for each reference to the field. | |
11710 | ||
11711 | if (this->called_fieldtrack_) | |
11712 | return this; | |
11713 | this->called_fieldtrack_ = true; | |
11714 | ||
11715 | Location loc = this->location(); | |
11716 | ||
11717 | std::string s = "fieldtrack \""; | |
e65055a5 | 11718 | Named_type* nt = this->expr_->type()->unalias()->named_type(); |
149eabc5 | 11719 | if (nt == NULL || nt->named_object()->package() == NULL) |
11720 | s.append(gogo->pkgpath()); | |
11721 | else | |
11722 | s.append(nt->named_object()->package()->pkgpath()); | |
11723 | s.push_back('.'); | |
11724 | if (nt != NULL) | |
5c29ad36 | 11725 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11726 | s.push_back('.'); |
11727 | s.append(field->field_name()); | |
11728 | s.push_back('"'); | |
11729 | ||
11730 | // We can't use a string here, because internally a string holds a | |
11731 | // pointer to the actual bytes; when the linker garbage collects the | |
11732 | // string, it won't garbage collect the bytes. So we use a | |
11733 | // [...]byte. | |
11734 | ||
e67508fa | 11735 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 11736 | |
11737 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
6bf4793c | 11738 | Array_type* array_type = Type::make_array_type(byte_type, length_expr); |
11739 | array_type->set_is_array_incomparable(); | |
149eabc5 | 11740 | |
11741 | Expression_list* bytes = new Expression_list(); | |
11742 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11743 | { | |
e67508fa | 11744 | unsigned char c = static_cast<unsigned char>(*p); |
11745 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 11746 | } |
11747 | ||
11748 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11749 | bytes, false, loc); |
149eabc5 | 11750 | |
11751 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11752 | ||
11753 | static int count; | |
11754 | char buf[50]; | |
11755 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11756 | ++count; | |
11757 | ||
11758 | Named_object* no = gogo->add_variable(buf, var); | |
11759 | e = Expression::make_var_reference(no, loc); | |
11760 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11761 | ||
11762 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 11763 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 11764 | inserter->insert(Statement::make_statement(call, false)); |
11765 | ||
11766 | // Put this function, and the global variable we just created, into | |
11767 | // unique sections. This will permit the linker to garbage collect | |
11768 | // them if they are not referenced. The effect is that the only | |
11769 | // strings, indicating field references, that will wind up in the | |
11770 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11771 | if (function != NULL) |
11772 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11773 | var->set_in_unique_section(); |
11774 | ||
11775 | return this; | |
11776 | } | |
11777 | ||
e440a328 | 11778 | // Return the type of a field reference. |
11779 | ||
11780 | Type* | |
11781 | Field_reference_expression::do_type() | |
11782 | { | |
b0e628fb | 11783 | Type* type = this->expr_->type(); |
5c13bd80 | 11784 | if (type->is_error()) |
b0e628fb | 11785 | return type; |
11786 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11787 | go_assert(struct_type != NULL); |
e440a328 | 11788 | return struct_type->field(this->field_index_)->type(); |
11789 | } | |
11790 | ||
11791 | // Check the types for a field reference. | |
11792 | ||
11793 | void | |
11794 | Field_reference_expression::do_check_types(Gogo*) | |
11795 | { | |
b0e628fb | 11796 | Type* type = this->expr_->type(); |
5c13bd80 | 11797 | if (type->is_error()) |
b0e628fb | 11798 | return; |
11799 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11800 | go_assert(struct_type != NULL); |
11801 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11802 | } |
11803 | ||
ea664253 | 11804 | // Get the backend representation for a field reference. |
e440a328 | 11805 | |
ea664253 | 11806 | Bexpression* |
11807 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11808 | { |
ea664253 | 11809 | Bexpression* bstruct = this->expr_->get_backend(context); |
11810 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11811 | this->field_index_, | |
11812 | this->location()); | |
e440a328 | 11813 | } |
11814 | ||
d751bb78 | 11815 | // Dump ast representation for a field reference expression. |
11816 | ||
11817 | void | |
11818 | Field_reference_expression::do_dump_expression( | |
11819 | Ast_dump_context* ast_dump_context) const | |
11820 | { | |
11821 | this->expr_->dump_expression(ast_dump_context); | |
11822 | ast_dump_context->ostream() << "." << this->field_index_; | |
11823 | } | |
11824 | ||
e440a328 | 11825 | // Make a reference to a qualified identifier in an expression. |
11826 | ||
11827 | Field_reference_expression* | |
11828 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11829 | Location location) |
e440a328 | 11830 | { |
11831 | return new Field_reference_expression(expr, field_index, location); | |
11832 | } | |
11833 | ||
11834 | // Class Interface_field_reference_expression. | |
11835 | ||
2387f644 | 11836 | // Return an expression for the pointer to the function to call. |
e440a328 | 11837 | |
2387f644 | 11838 | Expression* |
11839 | Interface_field_reference_expression::get_function() | |
e440a328 | 11840 | { |
2387f644 | 11841 | Expression* ref = this->expr_; |
11842 | Location loc = this->location(); | |
11843 | if (ref->type()->points_to() != NULL) | |
f614ea8b | 11844 | ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc); |
e440a328 | 11845 | |
2387f644 | 11846 | Expression* mtable = |
11847 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11848 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11849 | |
11850 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11851 | unsigned int index; |
11852 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11853 | go_assert(field != NULL); | |
f614ea8b | 11854 | |
11855 | mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc); | |
2387f644 | 11856 | return Expression::make_field_reference(mtable, index, loc); |
e440a328 | 11857 | } |
11858 | ||
2387f644 | 11859 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11860 | // function. |
11861 | ||
2387f644 | 11862 | Expression* |
11863 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11864 | { |
2387f644 | 11865 | Expression* expr = this->expr_; |
11866 | if (expr->type()->points_to() != NULL) | |
f614ea8b | 11867 | expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT, |
11868 | this->location()); | |
2387f644 | 11869 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, |
11870 | this->location()); | |
e440a328 | 11871 | } |
11872 | ||
11873 | // Traversal. | |
11874 | ||
11875 | int | |
11876 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11877 | { | |
11878 | return Expression::traverse(&this->expr_, traverse); | |
11879 | } | |
11880 | ||
0afbb937 | 11881 | // Lower the expression. If this expression is not called, we need to |
11882 | // evaluate the expression twice when converting to the backend | |
11883 | // interface. So introduce a temporary variable if necessary. | |
11884 | ||
11885 | Expression* | |
9782d556 | 11886 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11887 | Statement_inserter* inserter) | |
0afbb937 | 11888 | { |
5bf8be8b | 11889 | if (this->expr_->is_error_expression() |
11890 | || this->expr_->type()->is_error_type()) | |
11891 | { | |
11892 | go_assert(saw_errors()); | |
11893 | return Expression::make_error(this->location()); | |
11894 | } | |
11895 | ||
2387f644 | 11896 | if (!this->expr_->is_variable()) |
0afbb937 | 11897 | { |
11898 | Temporary_statement* temp = | |
9189e53b | 11899 | Statement::make_temporary(NULL, this->expr_, this->location()); |
0afbb937 | 11900 | inserter->insert(temp); |
9189e53b | 11901 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); |
0afbb937 | 11902 | } |
11903 | return this; | |
11904 | } | |
11905 | ||
e440a328 | 11906 | // Return the type of an interface field reference. |
11907 | ||
11908 | Type* | |
11909 | Interface_field_reference_expression::do_type() | |
11910 | { | |
11911 | Type* expr_type = this->expr_->type(); | |
11912 | ||
11913 | Type* points_to = expr_type->points_to(); | |
11914 | if (points_to != NULL) | |
11915 | expr_type = points_to; | |
11916 | ||
11917 | Interface_type* interface_type = expr_type->interface_type(); | |
11918 | if (interface_type == NULL) | |
11919 | return Type::make_error_type(); | |
11920 | ||
11921 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11922 | if (method == NULL) | |
11923 | return Type::make_error_type(); | |
11924 | ||
11925 | return method->type(); | |
11926 | } | |
11927 | ||
11928 | // Determine types. | |
11929 | ||
11930 | void | |
11931 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11932 | { | |
11933 | this->expr_->determine_type_no_context(); | |
11934 | } | |
11935 | ||
11936 | // Check the types for an interface field reference. | |
11937 | ||
11938 | void | |
11939 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11940 | { | |
11941 | Type* type = this->expr_->type(); | |
11942 | ||
11943 | Type* points_to = type->points_to(); | |
11944 | if (points_to != NULL) | |
11945 | type = points_to; | |
11946 | ||
11947 | Interface_type* interface_type = type->interface_type(); | |
11948 | if (interface_type == NULL) | |
5c491127 | 11949 | { |
11950 | if (!type->is_error_type()) | |
11951 | this->report_error(_("expected interface or pointer to interface")); | |
11952 | } | |
e440a328 | 11953 | else |
11954 | { | |
11955 | const Typed_identifier* method = | |
11956 | interface_type->find_method(this->name_); | |
11957 | if (method == NULL) | |
11958 | { | |
631d5788 | 11959 | go_error_at(this->location(), "method %qs not in interface", |
11960 | Gogo::message_name(this->name_).c_str()); | |
e440a328 | 11961 | this->set_is_error(); |
11962 | } | |
11963 | } | |
11964 | } | |
11965 | ||
0afbb937 | 11966 | // If an interface field reference is not simply called, then it is |
11967 | // represented as a closure. The closure will hold a single variable, | |
11968 | // the value of the interface on which the method should be called. | |
11969 | // The function will be a simple thunk that pulls the value from the | |
11970 | // closure and calls the method with the remaining arguments. | |
11971 | ||
11972 | // Because method values are not common, we don't build all thunks for | |
11973 | // all possible interface methods, but instead only build them as we | |
11974 | // need them. In particular, we even build them on demand for | |
11975 | // interface methods defined in other packages. | |
11976 | ||
11977 | Interface_field_reference_expression::Interface_method_thunks | |
11978 | Interface_field_reference_expression::interface_method_thunks; | |
11979 | ||
11980 | // Find or create the thunk to call method NAME on TYPE. | |
11981 | ||
11982 | Named_object* | |
11983 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11984 | Interface_type* type, | |
11985 | const std::string& name) | |
11986 | { | |
11987 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11988 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11989 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11990 | if (ins.second) | |
11991 | { | |
11992 | // This is the first time we have seen this interface. | |
11993 | ins.first->second = new Method_thunks(); | |
11994 | } | |
11995 | ||
11996 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11997 | p != ins.first->second->end(); | |
11998 | p++) | |
11999 | if (p->first == name) | |
12000 | return p->second; | |
12001 | ||
12002 | Location loc = type->location(); | |
12003 | ||
12004 | const Typed_identifier* method_id = type->find_method(name); | |
12005 | if (method_id == NULL) | |
13f2fdb8 | 12006 | return Named_object::make_erroneous_name(gogo->thunk_name()); |
0afbb937 | 12007 | |
12008 | Function_type* orig_fntype = method_id->type()->function_type(); | |
12009 | if (orig_fntype == NULL) | |
13f2fdb8 | 12010 | return Named_object::make_erroneous_name(gogo->thunk_name()); |
0afbb937 | 12011 | |
12012 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 12013 | // The type here is wrong--it should be the C function type. But it |
12014 | // doesn't really matter. | |
0afbb937 | 12015 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
13f2fdb8 | 12016 | sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc))); |
12017 | sfl->push_back(Struct_field(Typed_identifier("val", type, loc))); | |
6bf4793c | 12018 | Struct_type* st = Type::make_struct_type(sfl, loc); |
12019 | st->set_is_struct_incomparable(); | |
12020 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 12021 | |
f8bdf81a | 12022 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 12023 | |
13f2fdb8 | 12024 | std::string thunk_name = gogo->thunk_name(); |
da244e59 | 12025 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, |
0afbb937 | 12026 | false, loc); |
12027 | ||
f8bdf81a | 12028 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
12029 | cvar->set_is_used(); | |
1ecc6157 | 12030 | cvar->set_is_closure(); |
da244e59 | 12031 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
12032 | NULL, cvar); | |
f8bdf81a | 12033 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 12034 | |
f8bdf81a | 12035 | gogo->start_block(loc); |
0afbb937 | 12036 | |
12037 | // Field 0 of the closure is the function code pointer, field 1 is | |
12038 | // the value on which to invoke the method. | |
12039 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 12040 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 12041 | arg = Expression::make_field_reference(arg, 1, loc); |
12042 | ||
12043 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
12044 | loc); | |
12045 | ||
12046 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
12047 | Expression_list* args; | |
12048 | if (orig_params == NULL || orig_params->empty()) | |
12049 | args = NULL; | |
12050 | else | |
12051 | { | |
12052 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
12053 | args = new Expression_list(); | |
12054 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 12055 | p != new_params->end(); |
0afbb937 | 12056 | ++p) |
12057 | { | |
12058 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
12059 | go_assert(p_no != NULL | |
12060 | && p_no->is_variable() | |
12061 | && p_no->var_value()->is_parameter()); | |
12062 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
12063 | } | |
12064 | } | |
12065 | ||
12066 | Call_expression* call = Expression::make_call(ifre, args, | |
12067 | orig_fntype->is_varargs(), | |
12068 | loc); | |
12069 | call->set_varargs_are_lowered(); | |
12070 | ||
12071 | Statement* s = Statement::make_return_from_call(call, loc); | |
12072 | gogo->add_statement(s); | |
12073 | Block* b = gogo->finish_block(loc); | |
12074 | gogo->add_block(b, loc); | |
12075 | gogo->lower_block(new_no, b); | |
a32698ee | 12076 | gogo->flatten_block(new_no, b); |
0afbb937 | 12077 | gogo->finish_function(loc); |
12078 | ||
12079 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
12080 | return new_no; | |
12081 | } | |
12082 | ||
ea664253 | 12083 | // Get the backend representation for a method value. |
e440a328 | 12084 | |
ea664253 | 12085 | Bexpression* |
12086 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12087 | { |
0afbb937 | 12088 | Interface_type* type = this->expr_->type()->interface_type(); |
12089 | if (type == NULL) | |
12090 | { | |
12091 | go_assert(saw_errors()); | |
ea664253 | 12092 | return context->backend()->error_expression(); |
0afbb937 | 12093 | } |
12094 | ||
12095 | Named_object* thunk = | |
12096 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
12097 | type, this->name_); | |
12098 | if (thunk->is_erroneous()) | |
12099 | { | |
12100 | go_assert(saw_errors()); | |
ea664253 | 12101 | return context->backend()->error_expression(); |
0afbb937 | 12102 | } |
12103 | ||
12104 | // FIXME: We should lower this earlier, but we can't it lower it in | |
12105 | // the lowering pass because at that point we don't know whether we | |
12106 | // need to create the thunk or not. If the expression is called, we | |
12107 | // don't need the thunk. | |
12108 | ||
12109 | Location loc = this->location(); | |
12110 | ||
12111 | Struct_field_list* fields = new Struct_field_list(); | |
13f2fdb8 | 12112 | fields->push_back(Struct_field(Typed_identifier("fn", |
0afbb937 | 12113 | thunk->func_value()->type(), |
12114 | loc))); | |
13f2fdb8 | 12115 | fields->push_back(Struct_field(Typed_identifier("val", |
0afbb937 | 12116 | this->expr_->type(), |
12117 | loc))); | |
12118 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6bf4793c | 12119 | st->set_is_struct_incomparable(); |
0afbb937 | 12120 | |
12121 | Expression_list* vals = new Expression_list(); | |
12122 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
12123 | vals->push_back(this->expr_); | |
12124 | ||
12125 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 12126 | Bexpression* bclosure = |
12127 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 12128 | |
4df0c2d4 | 12129 | Gogo* gogo = context->gogo(); |
12130 | Btype* btype = this->type()->get_backend(gogo); | |
12131 | bclosure = gogo->backend()->convert_expression(btype, bclosure, loc); | |
12132 | ||
2387f644 | 12133 | Expression* nil_check = |
12134 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
12135 | Expression::make_nil(loc), loc); | |
ea664253 | 12136 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 12137 | |
ea664253 | 12138 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
12139 | loc)->get_backend(context); | |
2387f644 | 12140 | |
93715b75 | 12141 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
2387f644 | 12142 | Bexpression* bcond = |
93715b75 | 12143 | gogo->backend()->conditional_expression(bfn, NULL, |
12144 | bnil_check, bcrash, NULL, loc); | |
0ab48656 | 12145 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
12146 | Bstatement* cond_statement = | |
12147 | gogo->backend()->expression_statement(bfunction, bcond); | |
ea664253 | 12148 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 12149 | } |
12150 | ||
d751bb78 | 12151 | // Dump ast representation for an interface field reference. |
12152 | ||
12153 | void | |
12154 | Interface_field_reference_expression::do_dump_expression( | |
12155 | Ast_dump_context* ast_dump_context) const | |
12156 | { | |
12157 | this->expr_->dump_expression(ast_dump_context); | |
12158 | ast_dump_context->ostream() << "." << this->name_; | |
12159 | } | |
12160 | ||
e440a328 | 12161 | // Make a reference to a field in an interface. |
12162 | ||
12163 | Expression* | |
12164 | Expression::make_interface_field_reference(Expression* expr, | |
12165 | const std::string& field, | |
b13c66cd | 12166 | Location location) |
e440a328 | 12167 | { |
12168 | return new Interface_field_reference_expression(expr, field, location); | |
12169 | } | |
12170 | ||
12171 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
12172 | // is lowered after we know the type of the left hand side. | |
12173 | ||
12174 | class Selector_expression : public Parser_expression | |
12175 | { | |
12176 | public: | |
12177 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 12178 | Location location) |
e440a328 | 12179 | : Parser_expression(EXPRESSION_SELECTOR, location), |
12180 | left_(left), name_(name) | |
12181 | { } | |
12182 | ||
12183 | protected: | |
12184 | int | |
12185 | do_traverse(Traverse* traverse) | |
12186 | { return Expression::traverse(&this->left_, traverse); } | |
12187 | ||
12188 | Expression* | |
ceeb4318 | 12189 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12190 | |
12191 | Expression* | |
12192 | do_copy() | |
12193 | { | |
12194 | return new Selector_expression(this->left_->copy(), this->name_, | |
12195 | this->location()); | |
12196 | } | |
12197 | ||
d751bb78 | 12198 | void |
12199 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
12200 | ||
e440a328 | 12201 | private: |
12202 | Expression* | |
12203 | lower_method_expression(Gogo*); | |
12204 | ||
12205 | // The expression on the left hand side. | |
12206 | Expression* left_; | |
12207 | // The name on the right hand side. | |
12208 | std::string name_; | |
12209 | }; | |
12210 | ||
12211 | // Lower a selector expression once we know the real type of the left | |
12212 | // hand side. | |
12213 | ||
12214 | Expression* | |
ceeb4318 | 12215 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
12216 | int) | |
e440a328 | 12217 | { |
12218 | Expression* left = this->left_; | |
12219 | if (left->is_type_expression()) | |
12220 | return this->lower_method_expression(gogo); | |
12221 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
12222 | this->location()); | |
12223 | } | |
12224 | ||
12225 | // Lower a method expression T.M or (*T).M. We turn this into a | |
12226 | // function literal. | |
12227 | ||
12228 | Expression* | |
12229 | Selector_expression::lower_method_expression(Gogo* gogo) | |
12230 | { | |
b13c66cd | 12231 | Location location = this->location(); |
868b439e | 12232 | Type* left_type = this->left_->type(); |
12233 | Type* type = left_type; | |
e440a328 | 12234 | const std::string& name(this->name_); |
12235 | ||
12236 | bool is_pointer; | |
12237 | if (type->points_to() == NULL) | |
12238 | is_pointer = false; | |
12239 | else | |
12240 | { | |
12241 | is_pointer = true; | |
12242 | type = type->points_to(); | |
12243 | } | |
12244 | Named_type* nt = type->named_type(); | |
12245 | if (nt == NULL) | |
12246 | { | |
631d5788 | 12247 | go_error_at(location, |
12248 | ("method expression requires named type or " | |
12249 | "pointer to named type")); | |
e440a328 | 12250 | return Expression::make_error(location); |
12251 | } | |
12252 | ||
12253 | bool is_ambiguous; | |
12254 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 12255 | const Typed_identifier* imethod = NULL; |
dcc8506b | 12256 | if (method == NULL && !is_pointer) |
ab1468c3 | 12257 | { |
12258 | Interface_type* it = nt->interface_type(); | |
12259 | if (it != NULL) | |
12260 | imethod = it->find_method(name); | |
12261 | } | |
12262 | ||
868b439e | 12263 | if ((method == NULL && imethod == NULL) |
12264 | || (left_type->named_type() != NULL && left_type->points_to() != NULL)) | |
e440a328 | 12265 | { |
12266 | if (!is_ambiguous) | |
631d5788 | 12267 | go_error_at(location, "type %<%s%s%> has no method %<%s%>", |
12268 | is_pointer ? "*" : "", | |
12269 | nt->message_name().c_str(), | |
12270 | Gogo::message_name(name).c_str()); | |
e440a328 | 12271 | else |
631d5788 | 12272 | go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
12273 | Gogo::message_name(name).c_str(), | |
12274 | is_pointer ? "*" : "", | |
12275 | nt->message_name().c_str()); | |
e440a328 | 12276 | return Expression::make_error(location); |
12277 | } | |
12278 | ||
ab1468c3 | 12279 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 12280 | { |
631d5788 | 12281 | go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", |
12282 | nt->message_name().c_str(), | |
12283 | Gogo::message_name(name).c_str()); | |
e440a328 | 12284 | return Expression::make_error(location); |
12285 | } | |
12286 | ||
12287 | // Build a new function type in which the receiver becomes the first | |
12288 | // argument. | |
ab1468c3 | 12289 | Function_type* method_type; |
12290 | if (method != NULL) | |
12291 | { | |
12292 | method_type = method->type(); | |
c484d925 | 12293 | go_assert(method_type->is_method()); |
ab1468c3 | 12294 | } |
12295 | else | |
12296 | { | |
12297 | method_type = imethod->type()->function_type(); | |
c484d925 | 12298 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 12299 | } |
e440a328 | 12300 | |
12301 | const char* const receiver_name = "$this"; | |
12302 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
12303 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
12304 | location)); | |
12305 | ||
12306 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
12307 | if (method_parameters != NULL) | |
12308 | { | |
f470da59 | 12309 | int i = 0; |
e440a328 | 12310 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
12311 | p != method_parameters->end(); | |
f470da59 | 12312 | ++p, ++i) |
12313 | { | |
68883531 | 12314 | if (!p->name().empty()) |
f470da59 | 12315 | parameters->push_back(*p); |
12316 | else | |
12317 | { | |
12318 | char buf[20]; | |
12319 | snprintf(buf, sizeof buf, "$param%d", i); | |
12320 | parameters->push_back(Typed_identifier(buf, p->type(), | |
12321 | p->location())); | |
12322 | } | |
12323 | } | |
e440a328 | 12324 | } |
12325 | ||
12326 | const Typed_identifier_list* method_results = method_type->results(); | |
12327 | Typed_identifier_list* results; | |
12328 | if (method_results == NULL) | |
12329 | results = NULL; | |
12330 | else | |
12331 | { | |
12332 | results = new Typed_identifier_list(); | |
12333 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
12334 | p != method_results->end(); | |
12335 | ++p) | |
12336 | results->push_back(*p); | |
12337 | } | |
12338 | ||
12339 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
12340 | location); | |
12341 | if (method_type->is_varargs()) | |
12342 | fntype->set_is_varargs(); | |
12343 | ||
12344 | // We generate methods which always takes a pointer to the receiver | |
12345 | // as their first argument. If this is for a pointer type, we can | |
12346 | // simply reuse the existing function. We use an internal hack to | |
12347 | // get the right type. | |
8381eda7 | 12348 | // FIXME: This optimization is disabled because it doesn't yet work |
12349 | // with function descriptors when the method expression is not | |
12350 | // directly called. | |
12351 | if (method != NULL && is_pointer && false) | |
e440a328 | 12352 | { |
12353 | Named_object* mno = (method->needs_stub_method() | |
12354 | ? method->stub_object() | |
12355 | : method->named_object()); | |
12356 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
12357 | f = Expression::make_cast(fntype, f, location); | |
12358 | Type_conversion_expression* tce = | |
12359 | static_cast<Type_conversion_expression*>(f); | |
12360 | tce->set_may_convert_function_types(); | |
12361 | return f; | |
12362 | } | |
12363 | ||
13f2fdb8 | 12364 | Named_object* no = gogo->start_function(gogo->thunk_name(), fntype, false, |
e440a328 | 12365 | location); |
12366 | ||
12367 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 12368 | go_assert(vno != NULL); |
e440a328 | 12369 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 12370 | Expression* bm; |
12371 | if (method != NULL) | |
12372 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
12373 | else | |
12374 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 12375 | |
12376 | // Even though we found the method above, if it has an error type we | |
12377 | // may see an error here. | |
12378 | if (bm->is_error_expression()) | |
463fe805 | 12379 | { |
12380 | gogo->finish_function(location); | |
12381 | return bm; | |
12382 | } | |
e440a328 | 12383 | |
12384 | Expression_list* args; | |
f470da59 | 12385 | if (parameters->size() <= 1) |
e440a328 | 12386 | args = NULL; |
12387 | else | |
12388 | { | |
12389 | args = new Expression_list(); | |
f470da59 | 12390 | Typed_identifier_list::const_iterator p = parameters->begin(); |
12391 | ++p; | |
12392 | for (; p != parameters->end(); ++p) | |
e440a328 | 12393 | { |
12394 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 12395 | go_assert(vno != NULL); |
e440a328 | 12396 | args->push_back(Expression::make_var_reference(vno, location)); |
12397 | } | |
12398 | } | |
12399 | ||
ceeb4318 | 12400 | gogo->start_block(location); |
12401 | ||
e440a328 | 12402 | Call_expression* call = Expression::make_call(bm, args, |
12403 | method_type->is_varargs(), | |
12404 | location); | |
12405 | ||
0afbb937 | 12406 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 12407 | gogo->add_statement(s); |
12408 | ||
ceeb4318 | 12409 | Block* b = gogo->finish_block(location); |
12410 | ||
12411 | gogo->add_block(b, location); | |
12412 | ||
12413 | // Lower the call in case there are multiple results. | |
12414 | gogo->lower_block(no, b); | |
a32698ee | 12415 | gogo->flatten_block(no, b); |
ceeb4318 | 12416 | |
e440a328 | 12417 | gogo->finish_function(location); |
12418 | ||
12419 | return Expression::make_func_reference(no, NULL, location); | |
12420 | } | |
12421 | ||
d751bb78 | 12422 | // Dump the ast for a selector expression. |
12423 | ||
12424 | void | |
12425 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12426 | const | |
12427 | { | |
12428 | ast_dump_context->dump_expression(this->left_); | |
12429 | ast_dump_context->ostream() << "."; | |
12430 | ast_dump_context->ostream() << this->name_; | |
12431 | } | |
12432 | ||
e440a328 | 12433 | // Make a selector expression. |
12434 | ||
12435 | Expression* | |
12436 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 12437 | Location location) |
e440a328 | 12438 | { |
12439 | return new Selector_expression(left, name, location); | |
12440 | } | |
12441 | ||
da244e59 | 12442 | // Class Allocation_expression. |
e440a328 | 12443 | |
da244e59 | 12444 | int |
12445 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 12446 | { |
da244e59 | 12447 | return Type::traverse(this->type_, traverse); |
12448 | } | |
e440a328 | 12449 | |
da244e59 | 12450 | Type* |
12451 | Allocation_expression::do_type() | |
12452 | { | |
12453 | return Type::make_pointer_type(this->type_); | |
12454 | } | |
e440a328 | 12455 | |
22deed0d | 12456 | void |
12457 | Allocation_expression::do_check_types(Gogo*) | |
12458 | { | |
12459 | if (!this->type_->in_heap()) | |
12460 | go_error_at(this->location(), "can't heap allocate go:notinheap type"); | |
12461 | } | |
12462 | ||
da244e59 | 12463 | // Make a copy of an allocation expression. |
e440a328 | 12464 | |
da244e59 | 12465 | Expression* |
12466 | Allocation_expression::do_copy() | |
12467 | { | |
12468 | Allocation_expression* alloc = | |
de590a61 | 12469 | new Allocation_expression(this->type_->copy_expressions(), |
12470 | this->location()); | |
da244e59 | 12471 | if (this->allocate_on_stack_) |
12472 | alloc->set_allocate_on_stack(); | |
12473 | return alloc; | |
12474 | } | |
e440a328 | 12475 | |
ea664253 | 12476 | // Return the backend representation for an allocation expression. |
e440a328 | 12477 | |
ea664253 | 12478 | Bexpression* |
12479 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12480 | { |
2c809f8f | 12481 | Gogo* gogo = context->gogo(); |
12482 | Location loc = this->location(); | |
06e83d10 | 12483 | Btype* btype = this->type_->get_backend(gogo); |
da244e59 | 12484 | |
5973ede0 | 12485 | if (this->allocate_on_stack_) |
da244e59 | 12486 | { |
2a305b85 | 12487 | int64_t size; |
12488 | bool ok = this->type_->backend_type_size(gogo, &size); | |
12489 | if (!ok) | |
12490 | { | |
12491 | go_assert(saw_errors()); | |
12492 | return gogo->backend()->error_expression(); | |
12493 | } | |
06e83d10 | 12494 | Bstatement* decl; |
12495 | Named_object* fn = context->function(); | |
12496 | go_assert(fn != NULL); | |
12497 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
12498 | Bexpression* zero = gogo->backend()->zero_expression(btype); | |
12499 | Bvariable* temp = | |
12500 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
12501 | zero, true, loc, &decl); | |
12502 | Bexpression* ret = gogo->backend()->var_expression(temp, loc); | |
12503 | ret = gogo->backend()->address_expression(ret, loc); | |
12504 | ret = gogo->backend()->compound_expression(decl, ret, loc); | |
12505 | return ret; | |
da244e59 | 12506 | } |
12507 | ||
2a305b85 | 12508 | Bexpression* space = |
ea664253 | 12509 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 12510 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 12511 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 12512 | } |
12513 | ||
d751bb78 | 12514 | // Dump ast representation for an allocation expression. |
12515 | ||
12516 | void | |
12517 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12518 | const | |
12519 | { | |
12520 | ast_dump_context->ostream() << "new("; | |
12521 | ast_dump_context->dump_type(this->type_); | |
12522 | ast_dump_context->ostream() << ")"; | |
12523 | } | |
12524 | ||
e440a328 | 12525 | // Make an allocation expression. |
12526 | ||
12527 | Expression* | |
b13c66cd | 12528 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12529 | { |
12530 | return new Allocation_expression(type, location); | |
12531 | } | |
12532 | ||
e32de7ba | 12533 | // Class Ordered_value_list. |
e440a328 | 12534 | |
12535 | int | |
e32de7ba | 12536 | Ordered_value_list::traverse_vals(Traverse* traverse) |
e440a328 | 12537 | { |
0c4f5a19 | 12538 | if (this->vals_ != NULL) |
12539 | { | |
12540 | if (this->traverse_order_ == NULL) | |
12541 | { | |
12542 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12543 | return TRAVERSE_EXIT; | |
12544 | } | |
12545 | else | |
12546 | { | |
e32de7ba | 12547 | for (std::vector<unsigned long>::const_iterator p = |
12548 | this->traverse_order_->begin(); | |
0c4f5a19 | 12549 | p != this->traverse_order_->end(); |
12550 | ++p) | |
12551 | { | |
12552 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12553 | == TRAVERSE_EXIT) | |
12554 | return TRAVERSE_EXIT; | |
12555 | } | |
12556 | } | |
12557 | } | |
e32de7ba | 12558 | return TRAVERSE_CONTINUE; |
12559 | } | |
12560 | ||
12561 | // Class Struct_construction_expression. | |
12562 | ||
12563 | // Traversal. | |
12564 | ||
12565 | int | |
12566 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12567 | { | |
12568 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) | |
12569 | return TRAVERSE_EXIT; | |
e440a328 | 12570 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12571 | return TRAVERSE_EXIT; | |
12572 | return TRAVERSE_CONTINUE; | |
12573 | } | |
12574 | ||
12575 | // Return whether this is a constant initializer. | |
12576 | ||
12577 | bool | |
12578 | Struct_construction_expression::is_constant_struct() const | |
12579 | { | |
e32de7ba | 12580 | if (this->vals() == NULL) |
e440a328 | 12581 | return true; |
e32de7ba | 12582 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12583 | pv != this->vals()->end(); | |
e440a328 | 12584 | ++pv) |
12585 | { | |
12586 | if (*pv != NULL | |
12587 | && !(*pv)->is_constant() | |
12588 | && (!(*pv)->is_composite_literal() | |
12589 | || (*pv)->is_nonconstant_composite_literal())) | |
12590 | return false; | |
12591 | } | |
12592 | ||
12593 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12594 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12595 | pf != fields->end(); | |
12596 | ++pf) | |
12597 | { | |
12598 | // There are no constant constructors for interfaces. | |
12599 | if (pf->type()->interface_type() != NULL) | |
12600 | return false; | |
12601 | } | |
12602 | ||
12603 | return true; | |
12604 | } | |
12605 | ||
3ae06f68 | 12606 | // Return whether this struct can be used as a constant initializer. |
f9ca30f9 | 12607 | |
12608 | bool | |
3ae06f68 | 12609 | Struct_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12610 | { |
e32de7ba | 12611 | if (this->vals() == NULL) |
f9ca30f9 | 12612 | return true; |
e32de7ba | 12613 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12614 | pv != this->vals()->end(); | |
f9ca30f9 | 12615 | ++pv) |
12616 | { | |
3ae06f68 | 12617 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12618 | return false; |
12619 | } | |
de048538 | 12620 | |
12621 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12622 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12623 | pf != fields->end(); | |
12624 | ++pf) | |
12625 | { | |
12626 | // There are no constant constructors for interfaces. | |
12627 | if (pf->type()->interface_type() != NULL) | |
12628 | return false; | |
12629 | } | |
12630 | ||
f9ca30f9 | 12631 | return true; |
12632 | } | |
12633 | ||
e440a328 | 12634 | // Final type determination. |
12635 | ||
12636 | void | |
12637 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12638 | { | |
e32de7ba | 12639 | if (this->vals() == NULL) |
e440a328 | 12640 | return; |
12641 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
e32de7ba | 12642 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12643 | for (Struct_field_list::const_iterator pf = fields->begin(); |
12644 | pf != fields->end(); | |
12645 | ++pf, ++pv) | |
12646 | { | |
e32de7ba | 12647 | if (pv == this->vals()->end()) |
e440a328 | 12648 | return; |
12649 | if (*pv != NULL) | |
12650 | { | |
12651 | Type_context subcontext(pf->type(), false); | |
12652 | (*pv)->determine_type(&subcontext); | |
12653 | } | |
12654 | } | |
a6cb4c0e | 12655 | // Extra values are an error we will report elsewhere; we still want |
12656 | // to determine the type to avoid knockon errors. | |
e32de7ba | 12657 | for (; pv != this->vals()->end(); ++pv) |
a6cb4c0e | 12658 | (*pv)->determine_type_no_context(); |
e440a328 | 12659 | } |
12660 | ||
12661 | // Check types. | |
12662 | ||
12663 | void | |
12664 | Struct_construction_expression::do_check_types(Gogo*) | |
12665 | { | |
e32de7ba | 12666 | if (this->vals() == NULL) |
e440a328 | 12667 | return; |
12668 | ||
12669 | Struct_type* st = this->type_->struct_type(); | |
e32de7ba | 12670 | if (this->vals()->size() > st->field_count()) |
e440a328 | 12671 | { |
12672 | this->report_error(_("too many expressions for struct")); | |
12673 | return; | |
12674 | } | |
12675 | ||
12676 | const Struct_field_list* fields = st->fields(); | |
e32de7ba | 12677 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12678 | int i = 0; |
12679 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12680 | pf != fields->end(); | |
12681 | ++pf, ++pv, ++i) | |
12682 | { | |
e32de7ba | 12683 | if (pv == this->vals()->end()) |
e440a328 | 12684 | { |
12685 | this->report_error(_("too few expressions for struct")); | |
12686 | break; | |
12687 | } | |
12688 | ||
12689 | if (*pv == NULL) | |
12690 | continue; | |
12691 | ||
12692 | std::string reason; | |
12693 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12694 | { | |
12695 | if (reason.empty()) | |
631d5788 | 12696 | go_error_at((*pv)->location(), |
12697 | "incompatible type for field %d in struct construction", | |
12698 | i + 1); | |
e440a328 | 12699 | else |
631d5788 | 12700 | go_error_at((*pv)->location(), |
12701 | ("incompatible type for field %d in " | |
12702 | "struct construction (%s)"), | |
12703 | i + 1, reason.c_str()); | |
e440a328 | 12704 | this->set_is_error(); |
12705 | } | |
12706 | } | |
e32de7ba | 12707 | go_assert(pv == this->vals()->end()); |
e440a328 | 12708 | } |
12709 | ||
de590a61 | 12710 | // Copy. |
12711 | ||
12712 | Expression* | |
12713 | Struct_construction_expression::do_copy() | |
12714 | { | |
12715 | Struct_construction_expression* ret = | |
12716 | new Struct_construction_expression(this->type_->copy_expressions(), | |
12717 | (this->vals() == NULL | |
12718 | ? NULL | |
12719 | : this->vals()->copy()), | |
12720 | this->location()); | |
12721 | if (this->traverse_order() != NULL) | |
12722 | ret->set_traverse_order(this->traverse_order()); | |
12723 | return ret; | |
12724 | } | |
12725 | ||
8ba8cc87 | 12726 | // Flatten a struct construction expression. Store the values into |
12727 | // temporaries in case they need interface conversion. | |
12728 | ||
12729 | Expression* | |
12730 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
12731 | Statement_inserter* inserter) | |
12732 | { | |
e32de7ba | 12733 | if (this->vals() == NULL) |
8ba8cc87 | 12734 | return this; |
12735 | ||
12736 | // If this is a constant struct, we don't need temporaries. | |
de048538 | 12737 | if (this->is_constant_struct() || this->is_static_initializer()) |
8ba8cc87 | 12738 | return this; |
12739 | ||
12740 | Location loc = this->location(); | |
e32de7ba | 12741 | for (Expression_list::iterator pv = this->vals()->begin(); |
12742 | pv != this->vals()->end(); | |
8ba8cc87 | 12743 | ++pv) |
12744 | { | |
12745 | if (*pv != NULL) | |
12746 | { | |
5bf8be8b | 12747 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12748 | { | |
12749 | go_assert(saw_errors()); | |
12750 | return Expression::make_error(loc); | |
12751 | } | |
8ba8cc87 | 12752 | if (!(*pv)->is_variable()) |
12753 | { | |
12754 | Temporary_statement* temp = | |
12755 | Statement::make_temporary(NULL, *pv, loc); | |
12756 | inserter->insert(temp); | |
12757 | *pv = Expression::make_temporary_reference(temp, loc); | |
12758 | } | |
12759 | } | |
12760 | } | |
12761 | return this; | |
12762 | } | |
12763 | ||
ea664253 | 12764 | // Return the backend representation for constructing a struct. |
e440a328 | 12765 | |
ea664253 | 12766 | Bexpression* |
12767 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12768 | { |
12769 | Gogo* gogo = context->gogo(); | |
12770 | ||
2c809f8f | 12771 | Btype* btype = this->type_->get_backend(gogo); |
e32de7ba | 12772 | if (this->vals() == NULL) |
ea664253 | 12773 | return gogo->backend()->zero_expression(btype); |
e440a328 | 12774 | |
e440a328 | 12775 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e32de7ba | 12776 | Expression_list::const_iterator pv = this->vals()->begin(); |
2c809f8f | 12777 | std::vector<Bexpression*> init; |
12778 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12779 | pf != fields->end(); | |
12780 | ++pf) | |
e440a328 | 12781 | { |
63697958 | 12782 | Btype* fbtype = pf->type()->get_backend(gogo); |
e32de7ba | 12783 | if (pv == this->vals()->end()) |
2c809f8f | 12784 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12785 | else if (*pv == NULL) |
12786 | { | |
2c809f8f | 12787 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12788 | ++pv; |
12789 | } | |
12790 | else | |
12791 | { | |
2c809f8f | 12792 | Expression* val = |
12793 | Expression::convert_for_assignment(gogo, pf->type(), | |
12794 | *pv, this->location()); | |
ea664253 | 12795 | init.push_back(val->get_backend(context)); |
e440a328 | 12796 | ++pv; |
12797 | } | |
e440a328 | 12798 | } |
ea664253 | 12799 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12800 | } |
12801 | ||
12802 | // Export a struct construction. | |
12803 | ||
12804 | void | |
12805 | Struct_construction_expression::do_export(Export* exp) const | |
12806 | { | |
12807 | exp->write_c_string("convert("); | |
12808 | exp->write_type(this->type_); | |
e32de7ba | 12809 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12810 | pv != this->vals()->end(); | |
e440a328 | 12811 | ++pv) |
12812 | { | |
12813 | exp->write_c_string(", "); | |
12814 | if (*pv != NULL) | |
12815 | (*pv)->export_expression(exp); | |
12816 | } | |
12817 | exp->write_c_string(")"); | |
12818 | } | |
12819 | ||
d751bb78 | 12820 | // Dump ast representation of a struct construction expression. |
12821 | ||
12822 | void | |
12823 | Struct_construction_expression::do_dump_expression( | |
12824 | Ast_dump_context* ast_dump_context) const | |
12825 | { | |
d751bb78 | 12826 | ast_dump_context->dump_type(this->type_); |
12827 | ast_dump_context->ostream() << "{"; | |
e32de7ba | 12828 | ast_dump_context->dump_expression_list(this->vals()); |
d751bb78 | 12829 | ast_dump_context->ostream() << "}"; |
12830 | } | |
12831 | ||
e440a328 | 12832 | // Make a struct composite literal. This used by the thunk code. |
12833 | ||
12834 | Expression* | |
12835 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12836 | Location location) |
e440a328 | 12837 | { |
c484d925 | 12838 | go_assert(type->struct_type() != NULL); |
e440a328 | 12839 | return new Struct_construction_expression(type, vals, location); |
12840 | } | |
12841 | ||
da244e59 | 12842 | // Class Array_construction_expression. |
e440a328 | 12843 | |
12844 | // Traversal. | |
12845 | ||
12846 | int | |
12847 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12848 | { | |
e32de7ba | 12849 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) |
e440a328 | 12850 | return TRAVERSE_EXIT; |
12851 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12852 | return TRAVERSE_EXIT; | |
12853 | return TRAVERSE_CONTINUE; | |
12854 | } | |
12855 | ||
12856 | // Return whether this is a constant initializer. | |
12857 | ||
12858 | bool | |
12859 | Array_construction_expression::is_constant_array() const | |
12860 | { | |
e32de7ba | 12861 | if (this->vals() == NULL) |
e440a328 | 12862 | return true; |
12863 | ||
12864 | // There are no constant constructors for interfaces. | |
12865 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12866 | return false; | |
12867 | ||
e32de7ba | 12868 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12869 | pv != this->vals()->end(); | |
e440a328 | 12870 | ++pv) |
12871 | { | |
12872 | if (*pv != NULL | |
12873 | && !(*pv)->is_constant() | |
12874 | && (!(*pv)->is_composite_literal() | |
12875 | || (*pv)->is_nonconstant_composite_literal())) | |
12876 | return false; | |
12877 | } | |
12878 | return true; | |
12879 | } | |
12880 | ||
3ae06f68 | 12881 | // Return whether this can be used a constant initializer. |
f9ca30f9 | 12882 | |
12883 | bool | |
3ae06f68 | 12884 | Array_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12885 | { |
e32de7ba | 12886 | if (this->vals() == NULL) |
f9ca30f9 | 12887 | return true; |
de048538 | 12888 | |
12889 | // There are no constant constructors for interfaces. | |
12890 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12891 | return false; | |
12892 | ||
e32de7ba | 12893 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12894 | pv != this->vals()->end(); | |
f9ca30f9 | 12895 | ++pv) |
12896 | { | |
3ae06f68 | 12897 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12898 | return false; |
12899 | } | |
12900 | return true; | |
12901 | } | |
12902 | ||
e440a328 | 12903 | // Final type determination. |
12904 | ||
12905 | void | |
12906 | Array_construction_expression::do_determine_type(const Type_context*) | |
12907 | { | |
e32de7ba | 12908 | if (this->vals() == NULL) |
e440a328 | 12909 | return; |
12910 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
e32de7ba | 12911 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12912 | pv != this->vals()->end(); | |
e440a328 | 12913 | ++pv) |
12914 | { | |
12915 | if (*pv != NULL) | |
12916 | (*pv)->determine_type(&subcontext); | |
12917 | } | |
12918 | } | |
12919 | ||
12920 | // Check types. | |
12921 | ||
12922 | void | |
12923 | Array_construction_expression::do_check_types(Gogo*) | |
12924 | { | |
e32de7ba | 12925 | if (this->vals() == NULL) |
e440a328 | 12926 | return; |
12927 | ||
12928 | Array_type* at = this->type_->array_type(); | |
12929 | int i = 0; | |
12930 | Type* element_type = at->element_type(); | |
e32de7ba | 12931 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12932 | pv != this->vals()->end(); | |
e440a328 | 12933 | ++pv, ++i) |
12934 | { | |
12935 | if (*pv != NULL | |
12936 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12937 | { | |
631d5788 | 12938 | go_error_at((*pv)->location(), |
12939 | "incompatible type for element %d in composite literal", | |
12940 | i + 1); | |
e440a328 | 12941 | this->set_is_error(); |
12942 | } | |
12943 | } | |
e440a328 | 12944 | } |
12945 | ||
8ba8cc87 | 12946 | // Flatten an array construction expression. Store the values into |
12947 | // temporaries in case they need interface conversion. | |
12948 | ||
12949 | Expression* | |
12950 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12951 | Statement_inserter* inserter) | |
12952 | { | |
e32de7ba | 12953 | if (this->vals() == NULL) |
8ba8cc87 | 12954 | return this; |
12955 | ||
12956 | // If this is a constant array, we don't need temporaries. | |
de048538 | 12957 | if (this->is_constant_array() || this->is_static_initializer()) |
8ba8cc87 | 12958 | return this; |
12959 | ||
12960 | Location loc = this->location(); | |
e32de7ba | 12961 | for (Expression_list::iterator pv = this->vals()->begin(); |
12962 | pv != this->vals()->end(); | |
8ba8cc87 | 12963 | ++pv) |
12964 | { | |
12965 | if (*pv != NULL) | |
12966 | { | |
5bf8be8b | 12967 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12968 | { | |
12969 | go_assert(saw_errors()); | |
12970 | return Expression::make_error(loc); | |
12971 | } | |
8ba8cc87 | 12972 | if (!(*pv)->is_variable()) |
12973 | { | |
12974 | Temporary_statement* temp = | |
12975 | Statement::make_temporary(NULL, *pv, loc); | |
12976 | inserter->insert(temp); | |
12977 | *pv = Expression::make_temporary_reference(temp, loc); | |
12978 | } | |
12979 | } | |
12980 | } | |
12981 | return this; | |
12982 | } | |
12983 | ||
2c809f8f | 12984 | // Get a constructor expression for the array values. |
e440a328 | 12985 | |
2c809f8f | 12986 | Bexpression* |
12987 | Array_construction_expression::get_constructor(Translate_context* context, | |
12988 | Btype* array_btype) | |
e440a328 | 12989 | { |
e440a328 | 12990 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12991 | |
12992 | std::vector<unsigned long> indexes; | |
12993 | std::vector<Bexpression*> vals; | |
12994 | Gogo* gogo = context->gogo(); | |
e32de7ba | 12995 | if (this->vals() != NULL) |
e440a328 | 12996 | { |
12997 | size_t i = 0; | |
ffe743ca | 12998 | std::vector<unsigned long>::const_iterator pi; |
12999 | if (this->indexes_ != NULL) | |
13000 | pi = this->indexes_->begin(); | |
e32de7ba | 13001 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
13002 | pv != this->vals()->end(); | |
e440a328 | 13003 | ++pv, ++i) |
13004 | { | |
ffe743ca | 13005 | if (this->indexes_ != NULL) |
13006 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 13007 | |
13008 | if (this->indexes_ == NULL) | |
2c809f8f | 13009 | indexes.push_back(i); |
ffe743ca | 13010 | else |
2c809f8f | 13011 | indexes.push_back(*pi); |
e440a328 | 13012 | if (*pv == NULL) |
63697958 | 13013 | { |
63697958 | 13014 | Btype* ebtype = element_type->get_backend(gogo); |
13015 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 13016 | vals.push_back(zv); |
63697958 | 13017 | } |
e440a328 | 13018 | else |
13019 | { | |
2c809f8f | 13020 | Expression* val_expr = |
13021 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
13022 | this->location()); | |
ea664253 | 13023 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 13024 | } |
ffe743ca | 13025 | if (this->indexes_ != NULL) |
13026 | ++pi; | |
e440a328 | 13027 | } |
ffe743ca | 13028 | if (this->indexes_ != NULL) |
13029 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 13030 | } |
2c809f8f | 13031 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
13032 | vals, this->location()); | |
e440a328 | 13033 | } |
13034 | ||
13035 | // Export an array construction. | |
13036 | ||
13037 | void | |
13038 | Array_construction_expression::do_export(Export* exp) const | |
13039 | { | |
13040 | exp->write_c_string("convert("); | |
13041 | exp->write_type(this->type_); | |
e32de7ba | 13042 | if (this->vals() != NULL) |
e440a328 | 13043 | { |
ffe743ca | 13044 | std::vector<unsigned long>::const_iterator pi; |
13045 | if (this->indexes_ != NULL) | |
13046 | pi = this->indexes_->begin(); | |
e32de7ba | 13047 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
13048 | pv != this->vals()->end(); | |
e440a328 | 13049 | ++pv) |
13050 | { | |
13051 | exp->write_c_string(", "); | |
ffe743ca | 13052 | |
13053 | if (this->indexes_ != NULL) | |
13054 | { | |
13055 | char buf[100]; | |
13056 | snprintf(buf, sizeof buf, "%lu", *pi); | |
13057 | exp->write_c_string(buf); | |
13058 | exp->write_c_string(":"); | |
13059 | } | |
13060 | ||
e440a328 | 13061 | if (*pv != NULL) |
13062 | (*pv)->export_expression(exp); | |
ffe743ca | 13063 | |
13064 | if (this->indexes_ != NULL) | |
13065 | ++pi; | |
e440a328 | 13066 | } |
13067 | } | |
13068 | exp->write_c_string(")"); | |
13069 | } | |
13070 | ||
0e9a2e72 | 13071 | // Dump ast representation of an array construction expression. |
d751bb78 | 13072 | |
13073 | void | |
13074 | Array_construction_expression::do_dump_expression( | |
13075 | Ast_dump_context* ast_dump_context) const | |
13076 | { | |
ffe743ca | 13077 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 13078 | |
13079 | ast_dump_context->ostream() << "[" ; | |
13080 | if (length != NULL) | |
13081 | { | |
13082 | ast_dump_context->dump_expression(length); | |
13083 | } | |
13084 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 13085 | ast_dump_context->dump_type(this->type_); |
0e9a2e72 | 13086 | this->dump_slice_storage_expression(ast_dump_context); |
d751bb78 | 13087 | ast_dump_context->ostream() << "{" ; |
ffe743ca | 13088 | if (this->indexes_ == NULL) |
e32de7ba | 13089 | ast_dump_context->dump_expression_list(this->vals()); |
ffe743ca | 13090 | else |
13091 | { | |
e32de7ba | 13092 | Expression_list::const_iterator pv = this->vals()->begin(); |
ffe743ca | 13093 | for (std::vector<unsigned long>::const_iterator pi = |
13094 | this->indexes_->begin(); | |
13095 | pi != this->indexes_->end(); | |
13096 | ++pi, ++pv) | |
13097 | { | |
13098 | if (pi != this->indexes_->begin()) | |
13099 | ast_dump_context->ostream() << ", "; | |
13100 | ast_dump_context->ostream() << *pi << ':'; | |
13101 | ast_dump_context->dump_expression(*pv); | |
13102 | } | |
13103 | } | |
d751bb78 | 13104 | ast_dump_context->ostream() << "}" ; |
13105 | ||
13106 | } | |
13107 | ||
da244e59 | 13108 | // Class Fixed_array_construction_expression. |
e440a328 | 13109 | |
da244e59 | 13110 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
13111 | Type* type, const std::vector<unsigned long>* indexes, | |
13112 | Expression_list* vals, Location location) | |
13113 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
13114 | type, indexes, vals, location) | |
13115 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 13116 | |
de590a61 | 13117 | |
13118 | // Copy. | |
13119 | ||
13120 | Expression* | |
13121 | Fixed_array_construction_expression::do_copy() | |
13122 | { | |
13123 | Type* t = this->type()->copy_expressions(); | |
13124 | return new Fixed_array_construction_expression(t, this->indexes(), | |
13125 | (this->vals() == NULL | |
13126 | ? NULL | |
13127 | : this->vals()->copy()), | |
13128 | this->location()); | |
13129 | } | |
13130 | ||
ea664253 | 13131 | // Return the backend representation for constructing a fixed array. |
e440a328 | 13132 | |
ea664253 | 13133 | Bexpression* |
13134 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13135 | { |
9f0e0513 | 13136 | Type* type = this->type(); |
13137 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 13138 | return this->get_constructor(context, btype); |
e440a328 | 13139 | } |
13140 | ||
76f85fd6 | 13141 | Expression* |
13142 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
13143 | Location location) | |
13144 | { | |
13145 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
13146 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
13147 | } | |
13148 | ||
da244e59 | 13149 | // Class Slice_construction_expression. |
e440a328 | 13150 | |
da244e59 | 13151 | Slice_construction_expression::Slice_construction_expression( |
13152 | Type* type, const std::vector<unsigned long>* indexes, | |
13153 | Expression_list* vals, Location location) | |
13154 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
13155 | type, indexes, vals, location), | |
0e9a2e72 | 13156 | valtype_(NULL), array_val_(NULL), slice_storage_(NULL), |
13157 | storage_escapes_(true) | |
e440a328 | 13158 | { |
da244e59 | 13159 | go_assert(type->is_slice_type()); |
23d77f91 | 13160 | |
da244e59 | 13161 | unsigned long lenval; |
13162 | Expression* length; | |
13163 | if (vals == NULL || vals->empty()) | |
13164 | lenval = 0; | |
13165 | else | |
13166 | { | |
13167 | if (this->indexes() == NULL) | |
13168 | lenval = vals->size(); | |
13169 | else | |
13170 | lenval = indexes->back() + 1; | |
13171 | } | |
13172 | Type* int_type = Type::lookup_integer_type("int"); | |
13173 | length = Expression::make_integer_ul(lenval, int_type, location); | |
13174 | Type* element_type = type->array_type()->element_type(); | |
6bf4793c | 13175 | Array_type* array_type = Type::make_array_type(element_type, length); |
13176 | array_type->set_is_array_incomparable(); | |
13177 | this->valtype_ = array_type; | |
da244e59 | 13178 | } |
e440a328 | 13179 | |
23d77f91 | 13180 | // Traversal. |
13181 | ||
13182 | int | |
13183 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
13184 | { | |
13185 | if (this->Array_construction_expression::do_traverse(traverse) | |
13186 | == TRAVERSE_EXIT) | |
13187 | return TRAVERSE_EXIT; | |
13188 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
13189 | return TRAVERSE_EXIT; | |
0e9a2e72 | 13190 | if (this->array_val_ != NULL |
13191 | && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) | |
13192 | return TRAVERSE_EXIT; | |
13193 | if (this->slice_storage_ != NULL | |
13194 | && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) | |
13195 | return TRAVERSE_EXIT; | |
23d77f91 | 13196 | return TRAVERSE_CONTINUE; |
13197 | } | |
13198 | ||
0e9a2e72 | 13199 | // Helper routine to create fixed array value underlying the slice literal. |
13200 | // May be called during flattening, or later during do_get_backend(). | |
e440a328 | 13201 | |
0e9a2e72 | 13202 | Expression* |
13203 | Slice_construction_expression::create_array_val() | |
e440a328 | 13204 | { |
f9c68f17 | 13205 | Array_type* array_type = this->type()->array_type(); |
13206 | if (array_type == NULL) | |
13207 | { | |
c484d925 | 13208 | go_assert(this->type()->is_error()); |
0e9a2e72 | 13209 | return NULL; |
f9c68f17 | 13210 | } |
13211 | ||
f23d7786 | 13212 | Location loc = this->location(); |
23d77f91 | 13213 | go_assert(this->valtype_ != NULL); |
3d60812e | 13214 | |
f23d7786 | 13215 | Expression_list* vals = this->vals(); |
0e9a2e72 | 13216 | return new Fixed_array_construction_expression( |
13217 | this->valtype_, this->indexes(), vals, loc); | |
13218 | } | |
13219 | ||
13220 | // If we're previous established that the slice storage does not | |
13221 | // escape, then create a separate array temp val here for it. We | |
13222 | // need to do this as part of flattening so as to be able to insert | |
13223 | // the new temp statement. | |
13224 | ||
13225 | Expression* | |
13226 | Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no, | |
13227 | Statement_inserter* inserter) | |
13228 | { | |
13229 | if (this->type()->array_type() == NULL) | |
13230 | return NULL; | |
13231 | ||
13232 | // Base class flattening first | |
13233 | this->Array_construction_expression::do_flatten(gogo, no, inserter); | |
13234 | ||
a1bbc2c3 | 13235 | // Create a stack-allocated storage temp if storage won't escape |
03118c21 | 13236 | if (!this->storage_escapes_ |
13237 | && this->slice_storage_ == NULL | |
13238 | && this->element_count() > 0) | |
0e9a2e72 | 13239 | { |
13240 | Location loc = this->location(); | |
03118c21 | 13241 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13242 | go_assert(this->array_val_); |
13243 | Temporary_statement* temp = | |
13244 | Statement::make_temporary(this->valtype_, this->array_val_, loc); | |
13245 | inserter->insert(temp); | |
13246 | this->slice_storage_ = Expression::make_temporary_reference(temp, loc); | |
13247 | } | |
13248 | return this; | |
13249 | } | |
13250 | ||
13251 | // When dumping a slice construction expression that has an explicit | |
13252 | // storeage temp, emit the temp here (if we don't do this the storage | |
13253 | // temp appears unused in the AST dump). | |
13254 | ||
13255 | void | |
13256 | Slice_construction_expression:: | |
13257 | dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const | |
13258 | { | |
13259 | if (this->slice_storage_ == NULL) | |
13260 | return; | |
13261 | ast_dump_context->ostream() << "storage=" ; | |
13262 | ast_dump_context->dump_expression(this->slice_storage_); | |
13263 | } | |
13264 | ||
de590a61 | 13265 | // Copy. |
13266 | ||
13267 | Expression* | |
13268 | Slice_construction_expression::do_copy() | |
13269 | { | |
13270 | return new Slice_construction_expression(this->type()->copy_expressions(), | |
13271 | this->indexes(), | |
13272 | (this->vals() == NULL | |
13273 | ? NULL | |
13274 | : this->vals()->copy()), | |
13275 | this->location()); | |
13276 | } | |
13277 | ||
0e9a2e72 | 13278 | // Return the backend representation for constructing a slice. |
13279 | ||
13280 | Bexpression* | |
13281 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
13282 | { | |
13283 | if (this->array_val_ == NULL) | |
03118c21 | 13284 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13285 | if (this->array_val_ == NULL) |
13286 | { | |
13287 | go_assert(this->type()->is_error()); | |
13288 | return context->backend()->error_expression(); | |
13289 | } | |
13290 | ||
13291 | Location loc = this->location(); | |
e440a328 | 13292 | |
3ae06f68 | 13293 | bool is_static_initializer = this->array_val_->is_static_initializer(); |
d8829beb | 13294 | |
13295 | // We have to copy the initial values into heap memory if we are in | |
3ae06f68 | 13296 | // a function or if the values are not constants. |
13297 | bool copy_to_heap = context->function() != NULL || !is_static_initializer; | |
e440a328 | 13298 | |
f23d7786 | 13299 | Expression* space; |
0e9a2e72 | 13300 | |
13301 | if (this->slice_storage_ != NULL) | |
13302 | { | |
13303 | go_assert(!this->storage_escapes_); | |
13304 | space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); | |
13305 | } | |
13306 | else if (!copy_to_heap) | |
e440a328 | 13307 | { |
f23d7786 | 13308 | // The initializer will only run once. |
0e9a2e72 | 13309 | space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); |
f23d7786 | 13310 | space->unary_expression()->set_is_slice_init(); |
e440a328 | 13311 | } |
13312 | else | |
45ff893b | 13313 | { |
5973ede0 | 13314 | go_assert(this->storage_escapes_ || this->element_count() == 0); |
0e9a2e72 | 13315 | space = Expression::make_heap_expression(this->array_val_, loc); |
45ff893b | 13316 | } |
e440a328 | 13317 | |
2c809f8f | 13318 | // Build a constructor for the slice. |
f23d7786 | 13319 | Expression* len = this->valtype_->array_type()->length(); |
13320 | Expression* slice_val = | |
13321 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 13322 | return slice_val->get_backend(context); |
e440a328 | 13323 | } |
13324 | ||
13325 | // Make a slice composite literal. This is used by the type | |
13326 | // descriptor code. | |
13327 | ||
0e9a2e72 | 13328 | Slice_construction_expression* |
e440a328 | 13329 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, |
b13c66cd | 13330 | Location location) |
e440a328 | 13331 | { |
411eb89e | 13332 | go_assert(type->is_slice_type()); |
2c809f8f | 13333 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 13334 | } |
13335 | ||
da244e59 | 13336 | // Class Map_construction_expression. |
e440a328 | 13337 | |
13338 | // Traversal. | |
13339 | ||
13340 | int | |
13341 | Map_construction_expression::do_traverse(Traverse* traverse) | |
13342 | { | |
13343 | if (this->vals_ != NULL | |
13344 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
13345 | return TRAVERSE_EXIT; | |
13346 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13347 | return TRAVERSE_EXIT; | |
13348 | return TRAVERSE_CONTINUE; | |
13349 | } | |
13350 | ||
2c809f8f | 13351 | // Flatten constructor initializer into a temporary variable since |
13352 | // we need to take its address for __go_construct_map. | |
13353 | ||
13354 | Expression* | |
13355 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
13356 | Statement_inserter* inserter) | |
13357 | { | |
13358 | if (!this->is_error_expression() | |
13359 | && this->vals_ != NULL | |
13360 | && !this->vals_->empty() | |
13361 | && this->constructor_temp_ == NULL) | |
13362 | { | |
13363 | Map_type* mt = this->type_->map_type(); | |
13364 | Type* key_type = mt->key_type(); | |
13365 | Type* val_type = mt->val_type(); | |
13366 | this->element_type_ = Type::make_builtin_struct_type(2, | |
13367 | "__key", key_type, | |
13368 | "__val", val_type); | |
13369 | ||
13370 | Expression_list* value_pairs = new Expression_list(); | |
13371 | Location loc = this->location(); | |
13372 | ||
13373 | size_t i = 0; | |
13374 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13375 | pv != this->vals_->end(); | |
13376 | ++pv, ++i) | |
13377 | { | |
13378 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 13379 | Expression* key = *pv; |
5bf8be8b | 13380 | if (key->is_error_expression() || key->type()->is_error_type()) |
13381 | { | |
13382 | go_assert(saw_errors()); | |
13383 | return Expression::make_error(loc); | |
13384 | } | |
91c0fd76 | 13385 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
13386 | { | |
13387 | Temporary_statement* temp = | |
13388 | Statement::make_temporary(NULL, key, loc); | |
13389 | inserter->insert(temp); | |
13390 | key = Expression::make_temporary_reference(temp, loc); | |
13391 | } | |
13392 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 13393 | |
13394 | ++pv; | |
91c0fd76 | 13395 | Expression* val = *pv; |
5bf8be8b | 13396 | if (val->is_error_expression() || val->type()->is_error_type()) |
13397 | { | |
13398 | go_assert(saw_errors()); | |
13399 | return Expression::make_error(loc); | |
13400 | } | |
91c0fd76 | 13401 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
13402 | { | |
13403 | Temporary_statement* temp = | |
13404 | Statement::make_temporary(NULL, val, loc); | |
13405 | inserter->insert(temp); | |
13406 | val = Expression::make_temporary_reference(temp, loc); | |
13407 | } | |
13408 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 13409 | |
13410 | key_value_pair->push_back(key); | |
13411 | key_value_pair->push_back(val); | |
13412 | value_pairs->push_back( | |
13413 | Expression::make_struct_composite_literal(this->element_type_, | |
13414 | key_value_pair, loc)); | |
13415 | } | |
13416 | ||
e67508fa | 13417 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
6bf4793c | 13418 | Array_type* ctor_type = |
2c809f8f | 13419 | Type::make_array_type(this->element_type_, element_count); |
6bf4793c | 13420 | ctor_type->set_is_array_incomparable(); |
2c809f8f | 13421 | Expression* constructor = |
13422 | new Fixed_array_construction_expression(ctor_type, NULL, | |
13423 | value_pairs, loc); | |
13424 | ||
13425 | this->constructor_temp_ = | |
13426 | Statement::make_temporary(NULL, constructor, loc); | |
13427 | constructor->issue_nil_check(); | |
13428 | this->constructor_temp_->set_is_address_taken(); | |
13429 | inserter->insert(this->constructor_temp_); | |
13430 | } | |
13431 | ||
13432 | return this; | |
13433 | } | |
13434 | ||
e440a328 | 13435 | // Final type determination. |
13436 | ||
13437 | void | |
13438 | Map_construction_expression::do_determine_type(const Type_context*) | |
13439 | { | |
13440 | if (this->vals_ == NULL) | |
13441 | return; | |
13442 | ||
13443 | Map_type* mt = this->type_->map_type(); | |
13444 | Type_context key_context(mt->key_type(), false); | |
13445 | Type_context val_context(mt->val_type(), false); | |
13446 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13447 | pv != this->vals_->end(); | |
13448 | ++pv) | |
13449 | { | |
13450 | (*pv)->determine_type(&key_context); | |
13451 | ++pv; | |
13452 | (*pv)->determine_type(&val_context); | |
13453 | } | |
13454 | } | |
13455 | ||
13456 | // Check types. | |
13457 | ||
13458 | void | |
13459 | Map_construction_expression::do_check_types(Gogo*) | |
13460 | { | |
13461 | if (this->vals_ == NULL) | |
13462 | return; | |
13463 | ||
13464 | Map_type* mt = this->type_->map_type(); | |
13465 | int i = 0; | |
13466 | Type* key_type = mt->key_type(); | |
13467 | Type* val_type = mt->val_type(); | |
13468 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13469 | pv != this->vals_->end(); | |
13470 | ++pv, ++i) | |
13471 | { | |
13472 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
13473 | { | |
631d5788 | 13474 | go_error_at((*pv)->location(), |
13475 | "incompatible type for element %d key in map construction", | |
13476 | i + 1); | |
e440a328 | 13477 | this->set_is_error(); |
13478 | } | |
13479 | ++pv; | |
13480 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
13481 | { | |
631d5788 | 13482 | go_error_at((*pv)->location(), |
13483 | ("incompatible type for element %d value " | |
13484 | "in map construction"), | |
e440a328 | 13485 | i + 1); |
13486 | this->set_is_error(); | |
13487 | } | |
13488 | } | |
13489 | } | |
13490 | ||
de590a61 | 13491 | // Copy. |
13492 | ||
13493 | Expression* | |
13494 | Map_construction_expression::do_copy() | |
13495 | { | |
13496 | return new Map_construction_expression(this->type_->copy_expressions(), | |
13497 | (this->vals_ == NULL | |
13498 | ? NULL | |
13499 | : this->vals_->copy()), | |
13500 | this->location()); | |
13501 | } | |
13502 | ||
ea664253 | 13503 | // Return the backend representation for constructing a map. |
e440a328 | 13504 | |
ea664253 | 13505 | Bexpression* |
13506 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13507 | { |
2c809f8f | 13508 | if (this->is_error_expression()) |
ea664253 | 13509 | return context->backend()->error_expression(); |
2c809f8f | 13510 | Location loc = this->location(); |
e440a328 | 13511 | |
e440a328 | 13512 | size_t i = 0; |
2c809f8f | 13513 | Expression* ventries; |
e440a328 | 13514 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 13515 | ventries = Expression::make_nil(loc); |
e440a328 | 13516 | else |
13517 | { | |
2c809f8f | 13518 | go_assert(this->constructor_temp_ != NULL); |
13519 | i = this->vals_->size() / 2; | |
e440a328 | 13520 | |
2c809f8f | 13521 | Expression* ctor_ref = |
13522 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
13523 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
13524 | } | |
e440a328 | 13525 | |
2c809f8f | 13526 | Map_type* mt = this->type_->map_type(); |
13527 | if (this->element_type_ == NULL) | |
13528 | this->element_type_ = | |
13529 | Type::make_builtin_struct_type(2, | |
13530 | "__key", mt->key_type(), | |
13531 | "__val", mt->val_type()); | |
0d5530d9 | 13532 | Expression* descriptor = Expression::make_type_descriptor(mt, loc); |
2c809f8f | 13533 | |
13534 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 13535 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 13536 | |
13537 | Expression* entry_size = | |
13538 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
13539 | ||
13540 | unsigned int field_index; | |
13541 | const Struct_field* valfield = | |
13542 | this->element_type_->find_local_field("__val", &field_index); | |
13543 | Expression* val_offset = | |
13544 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
2c809f8f | 13545 | |
13546 | Expression* map_ctor = | |
0d5530d9 | 13547 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, |
13548 | entry_size, val_offset, ventries); | |
ea664253 | 13549 | return map_ctor->get_backend(context); |
2c809f8f | 13550 | } |
e440a328 | 13551 | |
2c809f8f | 13552 | // Export an array construction. |
e440a328 | 13553 | |
2c809f8f | 13554 | void |
13555 | Map_construction_expression::do_export(Export* exp) const | |
13556 | { | |
13557 | exp->write_c_string("convert("); | |
13558 | exp->write_type(this->type_); | |
13559 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13560 | pv != this->vals_->end(); | |
13561 | ++pv) | |
13562 | { | |
13563 | exp->write_c_string(", "); | |
13564 | (*pv)->export_expression(exp); | |
13565 | } | |
13566 | exp->write_c_string(")"); | |
13567 | } | |
e440a328 | 13568 | |
2c809f8f | 13569 | // Dump ast representation for a map construction expression. |
d751bb78 | 13570 | |
13571 | void | |
13572 | Map_construction_expression::do_dump_expression( | |
13573 | Ast_dump_context* ast_dump_context) const | |
13574 | { | |
d751bb78 | 13575 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 13576 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13577 | ast_dump_context->ostream() << "}"; |
13578 | } | |
13579 | ||
7795ac51 | 13580 | // Class Composite_literal_expression. |
e440a328 | 13581 | |
13582 | // Traversal. | |
13583 | ||
13584 | int | |
13585 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13586 | { | |
dbffccfc | 13587 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 13588 | return TRAVERSE_EXIT; |
dbffccfc | 13589 | |
13590 | // If this is a struct composite literal with keys, then the keys | |
13591 | // are field names, not expressions. We don't want to traverse them | |
13592 | // in that case. If we do, we can give an erroneous error "variable | |
13593 | // initializer refers to itself." See bug482.go in the testsuite. | |
13594 | if (this->has_keys_ && this->vals_ != NULL) | |
13595 | { | |
13596 | // The type may not be resolvable at this point. | |
13597 | Type* type = this->type_; | |
a01f2481 | 13598 | |
7795ac51 | 13599 | for (int depth = 0; depth < this->depth_; ++depth) |
a01f2481 | 13600 | { |
13601 | if (type->array_type() != NULL) | |
13602 | type = type->array_type()->element_type(); | |
13603 | else if (type->map_type() != NULL) | |
7795ac51 | 13604 | { |
13605 | if (this->key_path_[depth]) | |
13606 | type = type->map_type()->key_type(); | |
13607 | else | |
13608 | type = type->map_type()->val_type(); | |
13609 | } | |
a01f2481 | 13610 | else |
13611 | { | |
13612 | // This error will be reported during lowering. | |
13613 | return TRAVERSE_CONTINUE; | |
13614 | } | |
13615 | } | |
13616 | ||
dbffccfc | 13617 | while (true) |
13618 | { | |
13619 | if (type->classification() == Type::TYPE_NAMED) | |
13620 | type = type->named_type()->real_type(); | |
13621 | else if (type->classification() == Type::TYPE_FORWARD) | |
13622 | { | |
13623 | Type* t = type->forwarded(); | |
13624 | if (t == type) | |
13625 | break; | |
13626 | type = t; | |
13627 | } | |
13628 | else | |
13629 | break; | |
13630 | } | |
13631 | ||
13632 | if (type->classification() == Type::TYPE_STRUCT) | |
13633 | { | |
13634 | Expression_list::iterator p = this->vals_->begin(); | |
13635 | while (p != this->vals_->end()) | |
13636 | { | |
13637 | // Skip key. | |
13638 | ++p; | |
13639 | go_assert(p != this->vals_->end()); | |
13640 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13641 | return TRAVERSE_EXIT; | |
13642 | ++p; | |
13643 | } | |
13644 | return TRAVERSE_CONTINUE; | |
13645 | } | |
13646 | } | |
13647 | ||
13648 | if (this->vals_ != NULL) | |
13649 | return this->vals_->traverse(traverse); | |
13650 | ||
13651 | return TRAVERSE_CONTINUE; | |
e440a328 | 13652 | } |
13653 | ||
13654 | // Lower a generic composite literal into a specific version based on | |
13655 | // the type. | |
13656 | ||
13657 | Expression* | |
ceeb4318 | 13658 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13659 | Statement_inserter* inserter, int) | |
e440a328 | 13660 | { |
13661 | Type* type = this->type_; | |
13662 | ||
7795ac51 | 13663 | for (int depth = 0; depth < this->depth_; ++depth) |
e440a328 | 13664 | { |
67a2ed75 | 13665 | type = type->deref(); |
e440a328 | 13666 | if (type->array_type() != NULL) |
13667 | type = type->array_type()->element_type(); | |
13668 | else if (type->map_type() != NULL) | |
7795ac51 | 13669 | { |
13670 | if (this->key_path_[depth]) | |
13671 | type = type->map_type()->key_type(); | |
13672 | else | |
13673 | type = type->map_type()->val_type(); | |
13674 | } | |
e440a328 | 13675 | else |
13676 | { | |
5c13bd80 | 13677 | if (!type->is_error()) |
631d5788 | 13678 | go_error_at(this->location(), |
13679 | ("may only omit types within composite literals " | |
13680 | "of slice, array, or map type")); | |
e440a328 | 13681 | return Expression::make_error(this->location()); |
13682 | } | |
13683 | } | |
13684 | ||
e00772b3 | 13685 | Type *pt = type->points_to(); |
13686 | bool is_pointer = false; | |
13687 | if (pt != NULL) | |
13688 | { | |
13689 | is_pointer = true; | |
13690 | type = pt; | |
13691 | } | |
13692 | ||
13693 | Expression* ret; | |
5c13bd80 | 13694 | if (type->is_error()) |
e440a328 | 13695 | return Expression::make_error(this->location()); |
13696 | else if (type->struct_type() != NULL) | |
e00772b3 | 13697 | ret = this->lower_struct(gogo, type); |
e440a328 | 13698 | else if (type->array_type() != NULL) |
113ef6a5 | 13699 | ret = this->lower_array(type); |
e440a328 | 13700 | else if (type->map_type() != NULL) |
e00772b3 | 13701 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13702 | else |
13703 | { | |
631d5788 | 13704 | go_error_at(this->location(), |
13705 | ("expected struct, slice, array, or map type " | |
13706 | "for composite literal")); | |
e440a328 | 13707 | return Expression::make_error(this->location()); |
13708 | } | |
e00772b3 | 13709 | |
13710 | if (is_pointer) | |
2c809f8f | 13711 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13712 | |
13713 | return ret; | |
e440a328 | 13714 | } |
13715 | ||
13716 | // Lower a struct composite literal. | |
13717 | ||
13718 | Expression* | |
81c4b26b | 13719 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13720 | { |
b13c66cd | 13721 | Location location = this->location(); |
e440a328 | 13722 | Struct_type* st = type->struct_type(); |
13723 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13724 | { |
e6013c28 | 13725 | if (this->vals_ != NULL |
13726 | && !this->vals_->empty() | |
13727 | && type->named_type() != NULL | |
13728 | && type->named_type()->named_object()->package() != NULL) | |
13729 | { | |
13730 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13731 | pf != st->fields()->end(); | |
13732 | ++pf) | |
07daa4e7 | 13733 | { |
07ba7f26 | 13734 | if (Gogo::is_hidden_name(pf->field_name()) |
13735 | || pf->is_embedded_builtin(gogo)) | |
631d5788 | 13736 | go_error_at(this->location(), |
13737 | "assignment of unexported field %qs in %qs literal", | |
13738 | Gogo::message_name(pf->field_name()).c_str(), | |
13739 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13740 | } |
13741 | } | |
13742 | ||
13743 | return new Struct_construction_expression(type, this->vals_, location); | |
13744 | } | |
e440a328 | 13745 | |
13746 | size_t field_count = st->field_count(); | |
13747 | std::vector<Expression*> vals(field_count); | |
e32de7ba | 13748 | std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>); |
e440a328 | 13749 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13750 | Expression* external_expr = NULL; |
13751 | const Named_object* external_no = NULL; | |
e440a328 | 13752 | while (p != this->vals_->end()) |
13753 | { | |
13754 | Expression* name_expr = *p; | |
13755 | ||
13756 | ++p; | |
c484d925 | 13757 | go_assert(p != this->vals_->end()); |
e440a328 | 13758 | Expression* val = *p; |
13759 | ||
13760 | ++p; | |
13761 | ||
13762 | if (name_expr == NULL) | |
13763 | { | |
631d5788 | 13764 | go_error_at(val->location(), |
13765 | "mixture of field and value initializers"); | |
e440a328 | 13766 | return Expression::make_error(location); |
13767 | } | |
13768 | ||
13769 | bool bad_key = false; | |
13770 | std::string name; | |
81c4b26b | 13771 | const Named_object* no = NULL; |
e440a328 | 13772 | switch (name_expr->classification()) |
13773 | { | |
13774 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13775 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 13776 | if (type->named_type() != NULL) |
13777 | { | |
13778 | // If the named object found for this field name comes from a | |
13779 | // different package than the struct it is a part of, do not count | |
13780 | // this incorrect lookup as a usage of the object's package. | |
13781 | no = name_expr->unknown_expression()->named_object(); | |
13782 | if (no->package() != NULL | |
13783 | && no->package() != type->named_type()->named_object()->package()) | |
13784 | no->package()->forget_usage(name_expr); | |
13785 | } | |
e440a328 | 13786 | break; |
13787 | ||
13788 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13789 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13790 | break; |
13791 | ||
13792 | case EXPRESSION_TYPE: | |
13793 | { | |
13794 | Type* t = name_expr->type(); | |
13795 | Named_type* nt = t->named_type(); | |
13796 | if (nt == NULL) | |
13797 | bad_key = true; | |
13798 | else | |
81c4b26b | 13799 | no = nt->named_object(); |
e440a328 | 13800 | } |
13801 | break; | |
13802 | ||
13803 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13804 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13805 | break; |
13806 | ||
b0c09712 | 13807 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
13808 | no = name_expr->enclosed_var_expression()->variable(); | |
e440a328 | 13809 | break; |
13810 | ||
b0c09712 | 13811 | case EXPRESSION_FUNC_REFERENCE: |
13812 | no = name_expr->func_expression()->named_object(); | |
e440a328 | 13813 | break; |
13814 | ||
13815 | default: | |
13816 | bad_key = true; | |
13817 | break; | |
13818 | } | |
13819 | if (bad_key) | |
13820 | { | |
631d5788 | 13821 | go_error_at(name_expr->location(), "expected struct field name"); |
e440a328 | 13822 | return Expression::make_error(location); |
13823 | } | |
13824 | ||
81c4b26b | 13825 | if (no != NULL) |
13826 | { | |
62750cd5 | 13827 | if (no->package() != NULL && external_expr == NULL) |
13828 | { | |
13829 | external_expr = name_expr; | |
13830 | external_no = no; | |
13831 | } | |
13832 | ||
81c4b26b | 13833 | name = no->name(); |
13834 | ||
13835 | // A predefined name won't be packed. If it starts with a | |
13836 | // lower case letter we need to check for that case, because | |
2d29d278 | 13837 | // the field name will be packed. FIXME. |
81c4b26b | 13838 | if (!Gogo::is_hidden_name(name) |
13839 | && name[0] >= 'a' | |
13840 | && name[0] <= 'z') | |
13841 | { | |
13842 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13843 | if (gno == no) | |
13844 | name = gogo->pack_hidden_name(name, false); | |
13845 | } | |
13846 | } | |
13847 | ||
e440a328 | 13848 | unsigned int index; |
13849 | const Struct_field* sf = st->find_local_field(name, &index); | |
13850 | if (sf == NULL) | |
13851 | { | |
631d5788 | 13852 | go_error_at(name_expr->location(), "unknown field %qs in %qs", |
13853 | Gogo::message_name(name).c_str(), | |
13854 | (type->named_type() != NULL | |
13855 | ? type->named_type()->message_name().c_str() | |
13856 | : "unnamed struct")); | |
e440a328 | 13857 | return Expression::make_error(location); |
13858 | } | |
13859 | if (vals[index] != NULL) | |
13860 | { | |
631d5788 | 13861 | go_error_at(name_expr->location(), |
13862 | "duplicate value for field %qs in %qs", | |
13863 | Gogo::message_name(name).c_str(), | |
13864 | (type->named_type() != NULL | |
13865 | ? type->named_type()->message_name().c_str() | |
13866 | : "unnamed struct")); | |
e440a328 | 13867 | return Expression::make_error(location); |
13868 | } | |
13869 | ||
07daa4e7 | 13870 | if (type->named_type() != NULL |
13871 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 13872 | && (Gogo::is_hidden_name(sf->field_name()) |
13873 | || sf->is_embedded_builtin(gogo))) | |
631d5788 | 13874 | go_error_at(name_expr->location(), |
13875 | "assignment of unexported field %qs in %qs literal", | |
13876 | Gogo::message_name(sf->field_name()).c_str(), | |
13877 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13878 | |
e440a328 | 13879 | vals[index] = val; |
e32de7ba | 13880 | traverse_order->push_back(static_cast<unsigned long>(index)); |
e440a328 | 13881 | } |
13882 | ||
62750cd5 | 13883 | if (!this->all_are_names_) |
13884 | { | |
13885 | // This is a weird case like bug462 in the testsuite. | |
13886 | if (external_expr == NULL) | |
631d5788 | 13887 | go_error_at(this->location(), "unknown field in %qs literal", |
13888 | (type->named_type() != NULL | |
13889 | ? type->named_type()->message_name().c_str() | |
13890 | : "unnamed struct")); | |
62750cd5 | 13891 | else |
631d5788 | 13892 | go_error_at(external_expr->location(), "unknown field %qs in %qs", |
13893 | external_no->message_name().c_str(), | |
13894 | (type->named_type() != NULL | |
13895 | ? type->named_type()->message_name().c_str() | |
13896 | : "unnamed struct")); | |
62750cd5 | 13897 | return Expression::make_error(location); |
13898 | } | |
13899 | ||
e440a328 | 13900 | Expression_list* list = new Expression_list; |
13901 | list->reserve(field_count); | |
13902 | for (size_t i = 0; i < field_count; ++i) | |
13903 | list->push_back(vals[i]); | |
13904 | ||
0c4f5a19 | 13905 | Struct_construction_expression* ret = |
13906 | new Struct_construction_expression(type, list, location); | |
13907 | ret->set_traverse_order(traverse_order); | |
13908 | return ret; | |
e440a328 | 13909 | } |
13910 | ||
e32de7ba | 13911 | // Index/value/traversal-order triple. |
00773463 | 13912 | |
e32de7ba | 13913 | struct IVT_triple { |
13914 | unsigned long index; | |
13915 | unsigned long traversal_order; | |
13916 | Expression* expr; | |
13917 | IVT_triple(unsigned long i, unsigned long to, Expression *e) | |
13918 | : index(i), traversal_order(to), expr(e) { } | |
13919 | bool operator<(const IVT_triple& other) const | |
13920 | { return this->index < other.index; } | |
00773463 | 13921 | }; |
13922 | ||
e440a328 | 13923 | // Lower an array composite literal. |
13924 | ||
13925 | Expression* | |
113ef6a5 | 13926 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13927 | { |
b13c66cd | 13928 | Location location = this->location(); |
e440a328 | 13929 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13930 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13931 | |
ffe743ca | 13932 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13933 | indexes->reserve(this->vals_->size()); | |
00773463 | 13934 | bool indexes_out_of_order = false; |
ffe743ca | 13935 | Expression_list* vals = new Expression_list(); |
13936 | vals->reserve(this->vals_->size()); | |
e440a328 | 13937 | unsigned long index = 0; |
13938 | Expression_list::const_iterator p = this->vals_->begin(); | |
13939 | while (p != this->vals_->end()) | |
13940 | { | |
13941 | Expression* index_expr = *p; | |
13942 | ||
13943 | ++p; | |
c484d925 | 13944 | go_assert(p != this->vals_->end()); |
e440a328 | 13945 | Expression* val = *p; |
13946 | ||
13947 | ++p; | |
13948 | ||
ffe743ca | 13949 | if (index_expr == NULL) |
13950 | { | |
13951 | if (!indexes->empty()) | |
13952 | indexes->push_back(index); | |
13953 | } | |
13954 | else | |
e440a328 | 13955 | { |
ffe743ca | 13956 | if (indexes->empty() && !vals->empty()) |
13957 | { | |
13958 | for (size_t i = 0; i < vals->size(); ++i) | |
13959 | indexes->push_back(i); | |
13960 | } | |
13961 | ||
0c77715b | 13962 | Numeric_constant nc; |
13963 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13964 | { |
631d5788 | 13965 | go_error_at(index_expr->location(), |
13966 | "index expression is not integer constant"); | |
e440a328 | 13967 | return Expression::make_error(location); |
13968 | } | |
6f6d9955 | 13969 | |
0c77715b | 13970 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13971 | { |
0c77715b | 13972 | case Numeric_constant::NC_UL_VALID: |
13973 | break; | |
13974 | case Numeric_constant::NC_UL_NOTINT: | |
631d5788 | 13975 | go_error_at(index_expr->location(), |
13976 | "index expression is not integer constant"); | |
0c77715b | 13977 | return Expression::make_error(location); |
13978 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 13979 | go_error_at(index_expr->location(), |
13980 | "index expression is negative"); | |
e440a328 | 13981 | return Expression::make_error(location); |
0c77715b | 13982 | case Numeric_constant::NC_UL_BIG: |
631d5788 | 13983 | go_error_at(index_expr->location(), "index value overflow"); |
e440a328 | 13984 | return Expression::make_error(location); |
0c77715b | 13985 | default: |
13986 | go_unreachable(); | |
e440a328 | 13987 | } |
6f6d9955 | 13988 | |
13989 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13990 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13991 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13992 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13993 | { |
631d5788 | 13994 | go_error_at(index_expr->location(), "index value overflow"); |
6f6d9955 | 13995 | return Expression::make_error(location); |
13996 | } | |
13997 | ||
ffe743ca | 13998 | if (std::find(indexes->begin(), indexes->end(), index) |
13999 | != indexes->end()) | |
e440a328 | 14000 | { |
631d5788 | 14001 | go_error_at(index_expr->location(), |
14002 | "duplicate value for index %lu", | |
14003 | index); | |
e440a328 | 14004 | return Expression::make_error(location); |
14005 | } | |
ffe743ca | 14006 | |
00773463 | 14007 | if (!indexes->empty() && index < indexes->back()) |
14008 | indexes_out_of_order = true; | |
14009 | ||
ffe743ca | 14010 | indexes->push_back(index); |
e440a328 | 14011 | } |
14012 | ||
ffe743ca | 14013 | vals->push_back(val); |
14014 | ||
e440a328 | 14015 | ++index; |
14016 | } | |
14017 | ||
ffe743ca | 14018 | if (indexes->empty()) |
14019 | { | |
14020 | delete indexes; | |
14021 | indexes = NULL; | |
14022 | } | |
e440a328 | 14023 | |
e32de7ba | 14024 | std::vector<unsigned long>* traverse_order = NULL; |
00773463 | 14025 | if (indexes_out_of_order) |
14026 | { | |
e32de7ba | 14027 | typedef std::vector<IVT_triple> V; |
00773463 | 14028 | |
14029 | V v; | |
14030 | v.reserve(indexes->size()); | |
14031 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
e32de7ba | 14032 | unsigned long torder = 0; |
00773463 | 14033 | for (Expression_list::const_iterator pe = vals->begin(); |
14034 | pe != vals->end(); | |
e32de7ba | 14035 | ++pe, ++pi, ++torder) |
14036 | v.push_back(IVT_triple(*pi, torder, *pe)); | |
00773463 | 14037 | |
e32de7ba | 14038 | std::sort(v.begin(), v.end()); |
00773463 | 14039 | |
14040 | delete indexes; | |
14041 | delete vals; | |
e32de7ba | 14042 | |
00773463 | 14043 | indexes = new std::vector<unsigned long>(); |
14044 | indexes->reserve(v.size()); | |
14045 | vals = new Expression_list(); | |
14046 | vals->reserve(v.size()); | |
e32de7ba | 14047 | traverse_order = new std::vector<unsigned long>(); |
14048 | traverse_order->reserve(v.size()); | |
00773463 | 14049 | |
14050 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
14051 | { | |
e32de7ba | 14052 | indexes->push_back(p->index); |
14053 | vals->push_back(p->expr); | |
14054 | traverse_order->push_back(p->traversal_order); | |
00773463 | 14055 | } |
14056 | } | |
14057 | ||
e32de7ba | 14058 | Expression* ret = this->make_array(type, indexes, vals); |
14059 | Array_construction_expression* ace = ret->array_literal(); | |
14060 | if (ace != NULL && traverse_order != NULL) | |
14061 | ace->set_traverse_order(traverse_order); | |
14062 | return ret; | |
e440a328 | 14063 | } |
14064 | ||
14065 | // Actually build the array composite literal. This handles | |
14066 | // [...]{...}. | |
14067 | ||
14068 | Expression* | |
ffe743ca | 14069 | Composite_literal_expression::make_array( |
14070 | Type* type, | |
14071 | const std::vector<unsigned long>* indexes, | |
14072 | Expression_list* vals) | |
e440a328 | 14073 | { |
b13c66cd | 14074 | Location location = this->location(); |
e440a328 | 14075 | Array_type* at = type->array_type(); |
ffe743ca | 14076 | |
e440a328 | 14077 | if (at->length() != NULL && at->length()->is_nil_expression()) |
14078 | { | |
ffe743ca | 14079 | size_t size; |
14080 | if (vals == NULL) | |
14081 | size = 0; | |
00773463 | 14082 | else if (indexes != NULL) |
14083 | size = indexes->back() + 1; | |
14084 | else | |
ffe743ca | 14085 | { |
14086 | size = vals->size(); | |
14087 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
14088 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
14089 | && size >> (it->bits() - 1) != 0) | |
14090 | { | |
631d5788 | 14091 | go_error_at(location, "too many elements in composite literal"); |
ffe743ca | 14092 | return Expression::make_error(location); |
14093 | } | |
14094 | } | |
ffe743ca | 14095 | |
e67508fa | 14096 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 14097 | at = Type::make_array_type(at->element_type(), elen); |
14098 | type = at; | |
14099 | } | |
ffe743ca | 14100 | else if (at->length() != NULL |
14101 | && !at->length()->is_error_expression() | |
14102 | && this->vals_ != NULL) | |
14103 | { | |
14104 | Numeric_constant nc; | |
14105 | unsigned long val; | |
14106 | if (at->length()->numeric_constant_value(&nc) | |
14107 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
14108 | { | |
14109 | if (indexes == NULL) | |
14110 | { | |
14111 | if (this->vals_->size() > val) | |
14112 | { | |
631d5788 | 14113 | go_error_at(location, |
14114 | "too many elements in composite literal"); | |
ffe743ca | 14115 | return Expression::make_error(location); |
14116 | } | |
14117 | } | |
14118 | else | |
14119 | { | |
00773463 | 14120 | unsigned long max = indexes->back(); |
ffe743ca | 14121 | if (max >= val) |
14122 | { | |
631d5788 | 14123 | go_error_at(location, |
14124 | ("some element keys in composite literal " | |
14125 | "are out of range")); | |
ffe743ca | 14126 | return Expression::make_error(location); |
14127 | } | |
14128 | } | |
14129 | } | |
14130 | } | |
14131 | ||
e440a328 | 14132 | if (at->length() != NULL) |
ffe743ca | 14133 | return new Fixed_array_construction_expression(type, indexes, vals, |
14134 | location); | |
e440a328 | 14135 | else |
2c809f8f | 14136 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 14137 | } |
14138 | ||
14139 | // Lower a map composite literal. | |
14140 | ||
14141 | Expression* | |
a287720d | 14142 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 14143 | Statement_inserter* inserter, |
a287720d | 14144 | Type* type) |
e440a328 | 14145 | { |
b13c66cd | 14146 | Location location = this->location(); |
e440a328 | 14147 | if (this->vals_ != NULL) |
14148 | { | |
14149 | if (!this->has_keys_) | |
14150 | { | |
631d5788 | 14151 | go_error_at(location, "map composite literal must have keys"); |
e440a328 | 14152 | return Expression::make_error(location); |
14153 | } | |
14154 | ||
a287720d | 14155 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 14156 | p != this->vals_->end(); |
14157 | p += 2) | |
14158 | { | |
14159 | if (*p == NULL) | |
14160 | { | |
14161 | ++p; | |
631d5788 | 14162 | go_error_at((*p)->location(), |
14163 | ("map composite literal must " | |
14164 | "have keys for every value")); | |
e440a328 | 14165 | return Expression::make_error(location); |
14166 | } | |
a287720d | 14167 | // Make sure we have lowered the key; it may not have been |
14168 | // lowered in order to handle keys for struct composite | |
14169 | // literals. Lower it now to get the right error message. | |
14170 | if ((*p)->unknown_expression() != NULL) | |
14171 | { | |
14172 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 14173 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 14174 | go_assert((*p)->is_error_expression()); |
a287720d | 14175 | return Expression::make_error(location); |
14176 | } | |
e440a328 | 14177 | } |
14178 | } | |
14179 | ||
14180 | return new Map_construction_expression(type, this->vals_, location); | |
14181 | } | |
14182 | ||
de590a61 | 14183 | // Copy. |
14184 | ||
14185 | Expression* | |
14186 | Composite_literal_expression::do_copy() | |
14187 | { | |
14188 | Composite_literal_expression* ret = | |
14189 | new Composite_literal_expression(this->type_->copy_expressions(), | |
14190 | this->depth_, this->has_keys_, | |
14191 | (this->vals_ == NULL | |
14192 | ? NULL | |
14193 | : this->vals_->copy()), | |
14194 | this->all_are_names_, | |
14195 | this->location()); | |
14196 | ret->key_path_ = this->key_path_; | |
14197 | return ret; | |
14198 | } | |
14199 | ||
d751bb78 | 14200 | // Dump ast representation for a composite literal expression. |
14201 | ||
14202 | void | |
14203 | Composite_literal_expression::do_dump_expression( | |
14204 | Ast_dump_context* ast_dump_context) const | |
14205 | { | |
8b1c301d | 14206 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 14207 | ast_dump_context->dump_type(this->type_); |
14208 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 14209 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 14210 | ast_dump_context->ostream() << "})"; |
14211 | } | |
14212 | ||
e440a328 | 14213 | // Make a composite literal expression. |
14214 | ||
14215 | Expression* | |
14216 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 14217 | Expression_list* vals, bool all_are_names, |
b13c66cd | 14218 | Location location) |
e440a328 | 14219 | { |
14220 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 14221 | all_are_names, location); |
e440a328 | 14222 | } |
14223 | ||
14224 | // Return whether this expression is a composite literal. | |
14225 | ||
14226 | bool | |
14227 | Expression::is_composite_literal() const | |
14228 | { | |
14229 | switch (this->classification_) | |
14230 | { | |
14231 | case EXPRESSION_COMPOSITE_LITERAL: | |
14232 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14233 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 14234 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14235 | case EXPRESSION_MAP_CONSTRUCTION: |
14236 | return true; | |
14237 | default: | |
14238 | return false; | |
14239 | } | |
14240 | } | |
14241 | ||
14242 | // Return whether this expression is a composite literal which is not | |
14243 | // constant. | |
14244 | ||
14245 | bool | |
14246 | Expression::is_nonconstant_composite_literal() const | |
14247 | { | |
14248 | switch (this->classification_) | |
14249 | { | |
14250 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14251 | { | |
14252 | const Struct_construction_expression *psce = | |
14253 | static_cast<const Struct_construction_expression*>(this); | |
14254 | return !psce->is_constant_struct(); | |
14255 | } | |
14256 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
14257 | { | |
14258 | const Fixed_array_construction_expression *pace = | |
14259 | static_cast<const Fixed_array_construction_expression*>(this); | |
14260 | return !pace->is_constant_array(); | |
14261 | } | |
2c809f8f | 14262 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14263 | { |
2c809f8f | 14264 | const Slice_construction_expression *pace = |
14265 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 14266 | return !pace->is_constant_array(); |
14267 | } | |
14268 | case EXPRESSION_MAP_CONSTRUCTION: | |
14269 | return true; | |
14270 | default: | |
14271 | return false; | |
14272 | } | |
14273 | } | |
14274 | ||
35a54f17 | 14275 | // Return true if this is a variable or temporary_variable. |
14276 | ||
14277 | bool | |
14278 | Expression::is_variable() const | |
14279 | { | |
14280 | switch (this->classification_) | |
14281 | { | |
14282 | case EXPRESSION_VAR_REFERENCE: | |
14283 | case EXPRESSION_TEMPORARY_REFERENCE: | |
14284 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
b0c09712 | 14285 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
35a54f17 | 14286 | return true; |
14287 | default: | |
14288 | return false; | |
14289 | } | |
14290 | } | |
14291 | ||
e440a328 | 14292 | // Return true if this is a reference to a local variable. |
14293 | ||
14294 | bool | |
14295 | Expression::is_local_variable() const | |
14296 | { | |
14297 | const Var_expression* ve = this->var_expression(); | |
14298 | if (ve == NULL) | |
14299 | return false; | |
14300 | const Named_object* no = ve->named_object(); | |
14301 | return (no->is_result_variable() | |
14302 | || (no->is_variable() && !no->var_value()->is_global())); | |
14303 | } | |
14304 | ||
14305 | // Class Type_guard_expression. | |
14306 | ||
14307 | // Traversal. | |
14308 | ||
14309 | int | |
14310 | Type_guard_expression::do_traverse(Traverse* traverse) | |
14311 | { | |
14312 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
14313 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14314 | return TRAVERSE_EXIT; | |
14315 | return TRAVERSE_CONTINUE; | |
14316 | } | |
14317 | ||
2c809f8f | 14318 | Expression* |
14319 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
14320 | Statement_inserter* inserter) | |
14321 | { | |
5bf8be8b | 14322 | if (this->expr_->is_error_expression() |
14323 | || this->expr_->type()->is_error_type()) | |
14324 | { | |
14325 | go_assert(saw_errors()); | |
14326 | return Expression::make_error(this->location()); | |
14327 | } | |
14328 | ||
2c809f8f | 14329 | if (!this->expr_->is_variable()) |
14330 | { | |
14331 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
14332 | this->location()); | |
14333 | inserter->insert(temp); | |
14334 | this->expr_ = | |
14335 | Expression::make_temporary_reference(temp, this->location()); | |
14336 | } | |
14337 | return this; | |
14338 | } | |
14339 | ||
e440a328 | 14340 | // Check types of a type guard expression. The expression must have |
14341 | // an interface type, but the actual type conversion is checked at run | |
14342 | // time. | |
14343 | ||
14344 | void | |
14345 | Type_guard_expression::do_check_types(Gogo*) | |
14346 | { | |
e440a328 | 14347 | Type* expr_type = this->expr_->type(); |
7e9da23f | 14348 | if (expr_type->interface_type() == NULL) |
f725ade8 | 14349 | { |
5c13bd80 | 14350 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 14351 | this->report_error(_("type assertion only valid for interface types")); |
14352 | this->set_is_error(); | |
14353 | } | |
e440a328 | 14354 | else if (this->type_->interface_type() == NULL) |
14355 | { | |
14356 | std::string reason; | |
14357 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
14358 | &reason)) | |
14359 | { | |
5c13bd80 | 14360 | if (!this->type_->is_error()) |
e440a328 | 14361 | { |
f725ade8 | 14362 | if (reason.empty()) |
14363 | this->report_error(_("impossible type assertion: " | |
14364 | "type does not implement interface")); | |
14365 | else | |
631d5788 | 14366 | go_error_at(this->location(), |
14367 | ("impossible type assertion: " | |
14368 | "type does not implement interface (%s)"), | |
14369 | reason.c_str()); | |
e440a328 | 14370 | } |
f725ade8 | 14371 | this->set_is_error(); |
e440a328 | 14372 | } |
14373 | } | |
14374 | } | |
14375 | ||
de590a61 | 14376 | // Copy. |
14377 | ||
14378 | Expression* | |
14379 | Type_guard_expression::do_copy() | |
14380 | { | |
14381 | return new Type_guard_expression(this->expr_->copy(), | |
14382 | this->type_->copy_expressions(), | |
14383 | this->location()); | |
14384 | } | |
14385 | ||
ea664253 | 14386 | // Return the backend representation for a type guard expression. |
e440a328 | 14387 | |
ea664253 | 14388 | Bexpression* |
14389 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14390 | { |
2c809f8f | 14391 | Expression* conversion; |
7e9da23f | 14392 | if (this->type_->interface_type() != NULL) |
2c809f8f | 14393 | conversion = |
14394 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
14395 | true, this->location()); | |
e440a328 | 14396 | else |
2c809f8f | 14397 | conversion = |
14398 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
14399 | this->expr_, this->location()); | |
14400 | ||
21b70e8f | 14401 | Gogo* gogo = context->gogo(); |
14402 | Btype* bt = this->type_->get_backend(gogo); | |
14403 | Bexpression* bexpr = conversion->get_backend(context); | |
14404 | return gogo->backend()->convert_expression(bt, bexpr, this->location()); | |
e440a328 | 14405 | } |
14406 | ||
d751bb78 | 14407 | // Dump ast representation for a type guard expression. |
14408 | ||
14409 | void | |
2c809f8f | 14410 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 14411 | const |
14412 | { | |
14413 | this->expr_->dump_expression(ast_dump_context); | |
14414 | ast_dump_context->ostream() << "."; | |
14415 | ast_dump_context->dump_type(this->type_); | |
14416 | } | |
14417 | ||
e440a328 | 14418 | // Make a type guard expression. |
14419 | ||
14420 | Expression* | |
14421 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 14422 | Location location) |
e440a328 | 14423 | { |
14424 | return new Type_guard_expression(expr, type, location); | |
14425 | } | |
14426 | ||
2c809f8f | 14427 | // Class Heap_expression. |
e440a328 | 14428 | |
da244e59 | 14429 | // Return the type of the expression stored on the heap. |
e440a328 | 14430 | |
da244e59 | 14431 | Type* |
14432 | Heap_expression::do_type() | |
14433 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 14434 | |
ea664253 | 14435 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 14436 | |
ea664253 | 14437 | Bexpression* |
14438 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14439 | { |
03118c21 | 14440 | Type* etype = this->expr_->type(); |
14441 | if (this->expr_->is_error_expression() || etype->is_error()) | |
ea664253 | 14442 | return context->backend()->error_expression(); |
2c809f8f | 14443 | |
02c19a1a | 14444 | Location loc = this->location(); |
2c809f8f | 14445 | Gogo* gogo = context->gogo(); |
02c19a1a | 14446 | Btype* btype = this->type()->get_backend(gogo); |
45ff893b | 14447 | |
03118c21 | 14448 | Expression* alloc = Expression::make_allocation(etype, loc); |
5973ede0 | 14449 | if (this->allocate_on_stack_) |
45ff893b | 14450 | alloc->allocation_expression()->set_allocate_on_stack(); |
14451 | Bexpression* space = alloc->get_backend(context); | |
02c19a1a | 14452 | |
14453 | Bstatement* decl; | |
14454 | Named_object* fn = context->function(); | |
14455 | go_assert(fn != NULL); | |
14456 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
14457 | Bvariable* space_temp = | |
14458 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
14459 | space, true, loc, &decl); | |
03118c21 | 14460 | Btype* expr_btype = etype->get_backend(gogo); |
02c19a1a | 14461 | |
ea664253 | 14462 | Bexpression* bexpr = this->expr_->get_backend(context); |
03118c21 | 14463 | |
14464 | // If this assignment needs a write barrier, call typedmemmove. We | |
14465 | // don't do this in the write barrier pass because in some cases | |
14466 | // backend conversion can introduce new Heap_expression values. | |
14467 | Bstatement* assn; | |
06e83d10 | 14468 | if (!etype->has_pointer() || this->allocate_on_stack_) |
03118c21 | 14469 | { |
7af8e400 | 14470 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14471 | Bexpression* ref = |
14472 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
14473 | assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc); | |
14474 | } | |
14475 | else | |
14476 | { | |
14477 | Bstatement* edecl; | |
14478 | Bvariable* btemp = | |
14479 | gogo->backend()->temporary_variable(fndecl, context->bblock(), | |
14480 | expr_btype, bexpr, true, loc, | |
14481 | &edecl); | |
14482 | Bexpression* btempref = gogo->backend()->var_expression(btemp, | |
7af8e400 | 14483 | loc); |
03118c21 | 14484 | Bexpression* addr = gogo->backend()->address_expression(btempref, loc); |
14485 | ||
14486 | Expression* td = Expression::make_type_descriptor(etype, loc); | |
14487 | Type* etype_ptr = Type::make_pointer_type(etype); | |
7af8e400 | 14488 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14489 | Expression* elhs = Expression::make_backend(space, etype_ptr, loc); |
14490 | Expression* erhs = Expression::make_backend(addr, etype_ptr, loc); | |
14491 | Expression* call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3, | |
14492 | td, elhs, erhs); | |
14493 | Bexpression* bcall = call->get_backend(context); | |
14494 | Bstatement* s = gogo->backend()->expression_statement(fndecl, bcall); | |
14495 | assn = gogo->backend()->compound_statement(edecl, s); | |
14496 | } | |
02c19a1a | 14497 | decl = gogo->backend()->compound_statement(decl, assn); |
7af8e400 | 14498 | space = gogo->backend()->var_expression(space_temp, loc); |
ea664253 | 14499 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 14500 | } |
14501 | ||
2c809f8f | 14502 | // Dump ast representation for a heap expression. |
d751bb78 | 14503 | |
14504 | void | |
2c809f8f | 14505 | Heap_expression::do_dump_expression( |
d751bb78 | 14506 | Ast_dump_context* ast_dump_context) const |
14507 | { | |
14508 | ast_dump_context->ostream() << "&("; | |
14509 | ast_dump_context->dump_expression(this->expr_); | |
14510 | ast_dump_context->ostream() << ")"; | |
14511 | } | |
14512 | ||
2c809f8f | 14513 | // Allocate an expression on the heap. |
e440a328 | 14514 | |
14515 | Expression* | |
2c809f8f | 14516 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 14517 | { |
2c809f8f | 14518 | return new Heap_expression(expr, location); |
e440a328 | 14519 | } |
14520 | ||
14521 | // Class Receive_expression. | |
14522 | ||
14523 | // Return the type of a receive expression. | |
14524 | ||
14525 | Type* | |
14526 | Receive_expression::do_type() | |
14527 | { | |
e429e3bd | 14528 | if (this->is_error_expression()) |
14529 | return Type::make_error_type(); | |
e440a328 | 14530 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
14531 | if (channel_type == NULL) | |
e429e3bd | 14532 | { |
14533 | this->report_error(_("expected channel")); | |
14534 | return Type::make_error_type(); | |
14535 | } | |
e440a328 | 14536 | return channel_type->element_type(); |
14537 | } | |
14538 | ||
14539 | // Check types for a receive expression. | |
14540 | ||
14541 | void | |
14542 | Receive_expression::do_check_types(Gogo*) | |
14543 | { | |
14544 | Type* type = this->channel_->type(); | |
5c13bd80 | 14545 | if (type->is_error()) |
e440a328 | 14546 | { |
e429e3bd | 14547 | go_assert(saw_errors()); |
e440a328 | 14548 | this->set_is_error(); |
14549 | return; | |
14550 | } | |
14551 | if (type->channel_type() == NULL) | |
14552 | { | |
14553 | this->report_error(_("expected channel")); | |
14554 | return; | |
14555 | } | |
14556 | if (!type->channel_type()->may_receive()) | |
14557 | { | |
14558 | this->report_error(_("invalid receive on send-only channel")); | |
14559 | return; | |
14560 | } | |
14561 | } | |
14562 | ||
2c809f8f | 14563 | // Flattening for receive expressions creates a temporary variable to store |
14564 | // received data in for receives. | |
14565 | ||
14566 | Expression* | |
14567 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
14568 | Statement_inserter* inserter) | |
14569 | { | |
14570 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
14571 | if (channel_type == NULL) | |
14572 | { | |
14573 | go_assert(saw_errors()); | |
14574 | return this; | |
14575 | } | |
5bf8be8b | 14576 | else if (this->channel_->is_error_expression()) |
14577 | { | |
14578 | go_assert(saw_errors()); | |
14579 | return Expression::make_error(this->location()); | |
14580 | } | |
2c809f8f | 14581 | |
14582 | Type* element_type = channel_type->element_type(); | |
14583 | if (this->temp_receiver_ == NULL) | |
14584 | { | |
14585 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
14586 | this->location()); | |
14587 | this->temp_receiver_->set_is_address_taken(); | |
14588 | inserter->insert(this->temp_receiver_); | |
14589 | } | |
14590 | ||
14591 | return this; | |
14592 | } | |
14593 | ||
ea664253 | 14594 | // Get the backend representation for a receive expression. |
e440a328 | 14595 | |
ea664253 | 14596 | Bexpression* |
14597 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14598 | { |
f24f10bb | 14599 | Location loc = this->location(); |
14600 | ||
e440a328 | 14601 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 14602 | if (channel_type == NULL) |
14603 | { | |
c484d925 | 14604 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 14605 | return context->backend()->error_expression(); |
5b8368f4 | 14606 | } |
e440a328 | 14607 | |
2c809f8f | 14608 | Expression* recv_ref = |
14609 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14610 | Expression* recv_addr = | |
14611 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14612 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
e36a5ff5 | 14613 | Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 2, |
14614 | this->channel_, recv_addr); | |
ea664253 | 14615 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 14616 | } |
14617 | ||
d751bb78 | 14618 | // Dump ast representation for a receive expression. |
14619 | ||
14620 | void | |
14621 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14622 | { | |
14623 | ast_dump_context->ostream() << " <- " ; | |
14624 | ast_dump_context->dump_expression(channel_); | |
14625 | } | |
14626 | ||
e440a328 | 14627 | // Make a receive expression. |
14628 | ||
14629 | Receive_expression* | |
b13c66cd | 14630 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 14631 | { |
14632 | return new Receive_expression(channel, location); | |
14633 | } | |
14634 | ||
e440a328 | 14635 | // An expression which evaluates to a pointer to the type descriptor |
14636 | // of a type. | |
14637 | ||
14638 | class Type_descriptor_expression : public Expression | |
14639 | { | |
14640 | public: | |
b13c66cd | 14641 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 14642 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
14643 | type_(type) | |
14644 | { } | |
14645 | ||
14646 | protected: | |
4b686186 | 14647 | int |
14648 | do_traverse(Traverse*); | |
14649 | ||
e440a328 | 14650 | Type* |
14651 | do_type() | |
14652 | { return Type::make_type_descriptor_ptr_type(); } | |
14653 | ||
f9ca30f9 | 14654 | bool |
3ae06f68 | 14655 | do_is_static_initializer() const |
f9ca30f9 | 14656 | { return true; } |
14657 | ||
e440a328 | 14658 | void |
14659 | do_determine_type(const Type_context*) | |
14660 | { } | |
14661 | ||
14662 | Expression* | |
14663 | do_copy() | |
14664 | { return this; } | |
14665 | ||
ea664253 | 14666 | Bexpression* |
14667 | do_get_backend(Translate_context* context) | |
a1d23b41 | 14668 | { |
ea664253 | 14669 | return this->type_->type_descriptor_pointer(context->gogo(), |
14670 | this->location()); | |
a1d23b41 | 14671 | } |
e440a328 | 14672 | |
d751bb78 | 14673 | void |
14674 | do_dump_expression(Ast_dump_context*) const; | |
14675 | ||
e440a328 | 14676 | private: |
14677 | // The type for which this is the descriptor. | |
14678 | Type* type_; | |
14679 | }; | |
14680 | ||
4b686186 | 14681 | int |
14682 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
14683 | { | |
14684 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14685 | return TRAVERSE_EXIT; | |
14686 | return TRAVERSE_CONTINUE; | |
14687 | } | |
14688 | ||
d751bb78 | 14689 | // Dump ast representation for a type descriptor expression. |
14690 | ||
14691 | void | |
14692 | Type_descriptor_expression::do_dump_expression( | |
14693 | Ast_dump_context* ast_dump_context) const | |
14694 | { | |
14695 | ast_dump_context->dump_type(this->type_); | |
14696 | } | |
14697 | ||
e440a328 | 14698 | // Make a type descriptor expression. |
14699 | ||
14700 | Expression* | |
b13c66cd | 14701 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14702 | { |
14703 | return new Type_descriptor_expression(type, location); | |
14704 | } | |
14705 | ||
aa5ae575 | 14706 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
14707 | // of a type. | |
14708 | ||
14709 | class GC_symbol_expression : public Expression | |
14710 | { | |
14711 | public: | |
14712 | GC_symbol_expression(Type* type) | |
14713 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
14714 | type_(type) | |
14715 | {} | |
14716 | ||
14717 | protected: | |
14718 | Type* | |
14719 | do_type() | |
03118c21 | 14720 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } |
aa5ae575 | 14721 | |
14722 | bool | |
3ae06f68 | 14723 | do_is_static_initializer() const |
aa5ae575 | 14724 | { return true; } |
14725 | ||
14726 | void | |
14727 | do_determine_type(const Type_context*) | |
14728 | { } | |
14729 | ||
14730 | Expression* | |
14731 | do_copy() | |
14732 | { return this; } | |
14733 | ||
14734 | Bexpression* | |
14735 | do_get_backend(Translate_context* context) | |
14736 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
14737 | ||
14738 | void | |
14739 | do_dump_expression(Ast_dump_context*) const; | |
14740 | ||
14741 | private: | |
14742 | // The type which this gc symbol describes. | |
14743 | Type* type_; | |
14744 | }; | |
14745 | ||
14746 | // Dump ast representation for a gc symbol expression. | |
14747 | ||
14748 | void | |
14749 | GC_symbol_expression::do_dump_expression( | |
14750 | Ast_dump_context* ast_dump_context) const | |
14751 | { | |
14752 | ast_dump_context->ostream() << "gcdata("; | |
14753 | ast_dump_context->dump_type(this->type_); | |
14754 | ast_dump_context->ostream() << ")"; | |
14755 | } | |
14756 | ||
14757 | // Make a gc symbol expression. | |
14758 | ||
14759 | Expression* | |
14760 | Expression::make_gc_symbol(Type* type) | |
14761 | { | |
14762 | return new GC_symbol_expression(type); | |
14763 | } | |
14764 | ||
03118c21 | 14765 | // An expression that evaluates to a pointer to a symbol holding the |
14766 | // ptrmask data of a type. | |
14767 | ||
14768 | class Ptrmask_symbol_expression : public Expression | |
14769 | { | |
14770 | public: | |
14771 | Ptrmask_symbol_expression(Type* type) | |
14772 | : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()), | |
14773 | type_(type) | |
14774 | {} | |
14775 | ||
14776 | protected: | |
14777 | Type* | |
14778 | do_type() | |
14779 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } | |
14780 | ||
14781 | bool | |
14782 | do_is_static_initializer() const | |
14783 | { return true; } | |
14784 | ||
14785 | void | |
14786 | do_determine_type(const Type_context*) | |
14787 | { } | |
14788 | ||
14789 | Expression* | |
14790 | do_copy() | |
14791 | { return this; } | |
14792 | ||
14793 | Bexpression* | |
14794 | do_get_backend(Translate_context*); | |
14795 | ||
14796 | void | |
14797 | do_dump_expression(Ast_dump_context*) const; | |
14798 | ||
14799 | private: | |
14800 | // The type that this ptrmask symbol describes. | |
14801 | Type* type_; | |
14802 | }; | |
14803 | ||
14804 | // Return the ptrmask variable. | |
14805 | ||
14806 | Bexpression* | |
14807 | Ptrmask_symbol_expression::do_get_backend(Translate_context* context) | |
14808 | { | |
14809 | Gogo* gogo = context->gogo(); | |
14810 | ||
14811 | // If this type does not need a gcprog, then we can use the standard | |
14812 | // GC symbol. | |
14813 | int64_t ptrsize, ptrdata; | |
14814 | if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata)) | |
14815 | return this->type_->gc_symbol_pointer(gogo); | |
14816 | ||
14817 | // Otherwise we have to build a ptrmask variable, and return a | |
14818 | // pointer to it. | |
14819 | ||
14820 | Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata); | |
14821 | Location bloc = Linemap::predeclared_location(); | |
7af8e400 | 14822 | Bexpression* bref = gogo->backend()->var_expression(bvar, bloc); |
03118c21 | 14823 | Bexpression* baddr = gogo->backend()->address_expression(bref, bloc); |
14824 | ||
14825 | Type* uint8_type = Type::lookup_integer_type("uint8"); | |
14826 | Type* pointer_uint8_type = Type::make_pointer_type(uint8_type); | |
14827 | Btype* ubtype = pointer_uint8_type->get_backend(gogo); | |
14828 | return gogo->backend()->convert_expression(ubtype, baddr, bloc); | |
14829 | } | |
14830 | ||
14831 | // Dump AST for a ptrmask symbol expression. | |
14832 | ||
14833 | void | |
14834 | Ptrmask_symbol_expression::do_dump_expression( | |
14835 | Ast_dump_context* ast_dump_context) const | |
14836 | { | |
14837 | ast_dump_context->ostream() << "ptrmask("; | |
14838 | ast_dump_context->dump_type(this->type_); | |
14839 | ast_dump_context->ostream() << ")"; | |
14840 | } | |
14841 | ||
14842 | // Make a ptrmask symbol expression. | |
14843 | ||
14844 | Expression* | |
14845 | Expression::make_ptrmask_symbol(Type* type) | |
14846 | { | |
14847 | return new Ptrmask_symbol_expression(type); | |
14848 | } | |
14849 | ||
e440a328 | 14850 | // An expression which evaluates to some characteristic of a type. |
14851 | // This is only used to initialize fields of a type descriptor. Using | |
14852 | // a new expression class is slightly inefficient but gives us a good | |
14853 | // separation between the frontend and the middle-end with regard to | |
14854 | // how types are laid out. | |
14855 | ||
14856 | class Type_info_expression : public Expression | |
14857 | { | |
14858 | public: | |
14859 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14860 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14861 | type_(type), type_info_(type_info) |
14862 | { } | |
14863 | ||
14864 | protected: | |
0e168074 | 14865 | bool |
3ae06f68 | 14866 | do_is_static_initializer() const |
0e168074 | 14867 | { return true; } |
14868 | ||
e440a328 | 14869 | Type* |
14870 | do_type(); | |
14871 | ||
14872 | void | |
14873 | do_determine_type(const Type_context*) | |
14874 | { } | |
14875 | ||
14876 | Expression* | |
14877 | do_copy() | |
14878 | { return this; } | |
14879 | ||
ea664253 | 14880 | Bexpression* |
14881 | do_get_backend(Translate_context* context); | |
e440a328 | 14882 | |
d751bb78 | 14883 | void |
14884 | do_dump_expression(Ast_dump_context*) const; | |
14885 | ||
e440a328 | 14886 | private: |
14887 | // The type for which we are getting information. | |
14888 | Type* type_; | |
14889 | // What information we want. | |
14890 | Type_info type_info_; | |
14891 | }; | |
14892 | ||
14893 | // The type is chosen to match what the type descriptor struct | |
14894 | // expects. | |
14895 | ||
14896 | Type* | |
14897 | Type_info_expression::do_type() | |
14898 | { | |
14899 | switch (this->type_info_) | |
14900 | { | |
14901 | case TYPE_INFO_SIZE: | |
03118c21 | 14902 | case TYPE_INFO_BACKEND_PTRDATA: |
14903 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
e440a328 | 14904 | return Type::lookup_integer_type("uintptr"); |
14905 | case TYPE_INFO_ALIGNMENT: | |
14906 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14907 | return Type::lookup_integer_type("uint8"); | |
14908 | default: | |
c3e6f413 | 14909 | go_unreachable(); |
e440a328 | 14910 | } |
14911 | } | |
14912 | ||
ea664253 | 14913 | // Return the backend representation for type information. |
e440a328 | 14914 | |
ea664253 | 14915 | Bexpression* |
14916 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14917 | { |
927a01eb | 14918 | Gogo* gogo = context->gogo(); |
2a305b85 | 14919 | bool ok = true; |
3f378015 | 14920 | int64_t val; |
927a01eb | 14921 | switch (this->type_info_) |
e440a328 | 14922 | { |
927a01eb | 14923 | case TYPE_INFO_SIZE: |
2a305b85 | 14924 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 14925 | break; |
14926 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 14927 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 14928 | break; |
14929 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 14930 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 14931 | break; |
03118c21 | 14932 | case TYPE_INFO_BACKEND_PTRDATA: |
14933 | ok = this->type_->backend_type_ptrdata(gogo, &val); | |
14934 | break; | |
14935 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
14936 | ok = this->type_->descriptor_ptrdata(gogo, &val); | |
14937 | break; | |
927a01eb | 14938 | default: |
14939 | go_unreachable(); | |
e440a328 | 14940 | } |
2a305b85 | 14941 | if (!ok) |
14942 | { | |
14943 | go_assert(saw_errors()); | |
14944 | return gogo->backend()->error_expression(); | |
14945 | } | |
3f378015 | 14946 | Expression* e = Expression::make_integer_int64(val, this->type(), |
14947 | this->location()); | |
14948 | return e->get_backend(context); | |
e440a328 | 14949 | } |
14950 | ||
d751bb78 | 14951 | // Dump ast representation for a type info expression. |
14952 | ||
14953 | void | |
14954 | Type_info_expression::do_dump_expression( | |
14955 | Ast_dump_context* ast_dump_context) const | |
14956 | { | |
14957 | ast_dump_context->ostream() << "typeinfo("; | |
14958 | ast_dump_context->dump_type(this->type_); | |
14959 | ast_dump_context->ostream() << ","; | |
14960 | ast_dump_context->ostream() << | |
14961 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14962 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
03118c21 | 14963 | : this->type_info_ == TYPE_INFO_SIZE ? "size" |
14964 | : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata" | |
14965 | : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata" | |
d751bb78 | 14966 | : "unknown"); |
14967 | ast_dump_context->ostream() << ")"; | |
14968 | } | |
14969 | ||
e440a328 | 14970 | // Make a type info expression. |
14971 | ||
14972 | Expression* | |
14973 | Expression::make_type_info(Type* type, Type_info type_info) | |
14974 | { | |
14975 | return new Type_info_expression(type, type_info); | |
14976 | } | |
14977 | ||
35a54f17 | 14978 | // An expression that evaluates to some characteristic of a slice. |
14979 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14980 | ||
14981 | class Slice_info_expression : public Expression | |
14982 | { | |
14983 | public: | |
14984 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14985 | Location location) | |
14986 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14987 | slice_(slice), slice_info_(slice_info) | |
14988 | { } | |
14989 | ||
14990 | protected: | |
14991 | Type* | |
14992 | do_type(); | |
14993 | ||
14994 | void | |
14995 | do_determine_type(const Type_context*) | |
14996 | { } | |
14997 | ||
14998 | Expression* | |
14999 | do_copy() | |
15000 | { | |
15001 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
15002 | this->location()); | |
15003 | } | |
15004 | ||
ea664253 | 15005 | Bexpression* |
15006 | do_get_backend(Translate_context* context); | |
35a54f17 | 15007 | |
15008 | void | |
15009 | do_dump_expression(Ast_dump_context*) const; | |
15010 | ||
15011 | void | |
15012 | do_issue_nil_check() | |
15013 | { this->slice_->issue_nil_check(); } | |
15014 | ||
15015 | private: | |
15016 | // The slice for which we are getting information. | |
15017 | Expression* slice_; | |
15018 | // What information we want. | |
15019 | Slice_info slice_info_; | |
15020 | }; | |
15021 | ||
15022 | // Return the type of the slice info. | |
15023 | ||
15024 | Type* | |
15025 | Slice_info_expression::do_type() | |
15026 | { | |
15027 | switch (this->slice_info_) | |
15028 | { | |
15029 | case SLICE_INFO_VALUE_POINTER: | |
15030 | return Type::make_pointer_type( | |
15031 | this->slice_->type()->array_type()->element_type()); | |
15032 | case SLICE_INFO_LENGTH: | |
15033 | case SLICE_INFO_CAPACITY: | |
15034 | return Type::lookup_integer_type("int"); | |
15035 | default: | |
15036 | go_unreachable(); | |
15037 | } | |
15038 | } | |
15039 | ||
ea664253 | 15040 | // Return the backend information for slice information. |
35a54f17 | 15041 | |
ea664253 | 15042 | Bexpression* |
15043 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 15044 | { |
15045 | Gogo* gogo = context->gogo(); | |
ea664253 | 15046 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 15047 | switch (this->slice_info_) |
15048 | { | |
15049 | case SLICE_INFO_VALUE_POINTER: | |
15050 | case SLICE_INFO_LENGTH: | |
15051 | case SLICE_INFO_CAPACITY: | |
ea664253 | 15052 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
15053 | this->location()); | |
35a54f17 | 15054 | break; |
15055 | default: | |
15056 | go_unreachable(); | |
15057 | } | |
35a54f17 | 15058 | } |
15059 | ||
15060 | // Dump ast representation for a type info expression. | |
15061 | ||
15062 | void | |
15063 | Slice_info_expression::do_dump_expression( | |
15064 | Ast_dump_context* ast_dump_context) const | |
15065 | { | |
15066 | ast_dump_context->ostream() << "sliceinfo("; | |
15067 | this->slice_->dump_expression(ast_dump_context); | |
15068 | ast_dump_context->ostream() << ","; | |
15069 | ast_dump_context->ostream() << | |
15070 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
15071 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
15072 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
15073 | : "unknown"); | |
15074 | ast_dump_context->ostream() << ")"; | |
15075 | } | |
15076 | ||
15077 | // Make a slice info expression. | |
15078 | ||
15079 | Expression* | |
15080 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
15081 | Location location) | |
15082 | { | |
15083 | return new Slice_info_expression(slice, slice_info, location); | |
15084 | } | |
15085 | ||
2c809f8f | 15086 | // An expression that represents a slice value: a struct with value pointer, |
15087 | // length, and capacity fields. | |
15088 | ||
15089 | class Slice_value_expression : public Expression | |
15090 | { | |
15091 | public: | |
15092 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
15093 | Expression* cap, Location location) | |
15094 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
15095 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
15096 | { } | |
15097 | ||
15098 | protected: | |
15099 | int | |
15100 | do_traverse(Traverse*); | |
15101 | ||
15102 | Type* | |
15103 | do_type() | |
15104 | { return this->type_; } | |
15105 | ||
15106 | void | |
15107 | do_determine_type(const Type_context*) | |
15108 | { go_unreachable(); } | |
15109 | ||
15110 | Expression* | |
15111 | do_copy() | |
15112 | { | |
de590a61 | 15113 | return new Slice_value_expression(this->type_->copy_expressions(), |
15114 | this->valptr_->copy(), | |
2c809f8f | 15115 | this->len_->copy(), this->cap_->copy(), |
15116 | this->location()); | |
15117 | } | |
15118 | ||
ea664253 | 15119 | Bexpression* |
15120 | do_get_backend(Translate_context* context); | |
2c809f8f | 15121 | |
15122 | void | |
15123 | do_dump_expression(Ast_dump_context*) const; | |
15124 | ||
15125 | private: | |
15126 | // The type of the slice value. | |
15127 | Type* type_; | |
15128 | // The pointer to the values in the slice. | |
15129 | Expression* valptr_; | |
15130 | // The length of the slice. | |
15131 | Expression* len_; | |
15132 | // The capacity of the slice. | |
15133 | Expression* cap_; | |
15134 | }; | |
15135 | ||
15136 | int | |
15137 | Slice_value_expression::do_traverse(Traverse* traverse) | |
15138 | { | |
55e8ba6a | 15139 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
15140 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 15141 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
15142 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
15143 | return TRAVERSE_EXIT; | |
15144 | return TRAVERSE_CONTINUE; | |
15145 | } | |
15146 | ||
ea664253 | 15147 | Bexpression* |
15148 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15149 | { |
15150 | std::vector<Bexpression*> vals(3); | |
ea664253 | 15151 | vals[0] = this->valptr_->get_backend(context); |
15152 | vals[1] = this->len_->get_backend(context); | |
15153 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 15154 | |
15155 | Gogo* gogo = context->gogo(); | |
15156 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 15157 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 15158 | } |
15159 | ||
15160 | void | |
15161 | Slice_value_expression::do_dump_expression( | |
15162 | Ast_dump_context* ast_dump_context) const | |
15163 | { | |
15164 | ast_dump_context->ostream() << "slicevalue("; | |
15165 | ast_dump_context->ostream() << "values: "; | |
15166 | this->valptr_->dump_expression(ast_dump_context); | |
15167 | ast_dump_context->ostream() << ", length: "; | |
15168 | this->len_->dump_expression(ast_dump_context); | |
15169 | ast_dump_context->ostream() << ", capacity: "; | |
15170 | this->cap_->dump_expression(ast_dump_context); | |
15171 | ast_dump_context->ostream() << ")"; | |
15172 | } | |
15173 | ||
15174 | Expression* | |
15175 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
15176 | Expression* cap, Location location) | |
15177 | { | |
15178 | go_assert(at->is_slice_type()); | |
15179 | return new Slice_value_expression(at, valptr, len, cap, location); | |
15180 | } | |
2387f644 | 15181 | |
15182 | // An expression that evaluates to some characteristic of a non-empty interface. | |
15183 | // This is used to access the method table or underlying object of an interface. | |
15184 | ||
15185 | class Interface_info_expression : public Expression | |
15186 | { | |
15187 | public: | |
15188 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 15189 | Location location) |
2387f644 | 15190 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
15191 | iface_(iface), iface_info_(iface_info) | |
15192 | { } | |
15193 | ||
15194 | protected: | |
15195 | Type* | |
15196 | do_type(); | |
15197 | ||
15198 | void | |
15199 | do_determine_type(const Type_context*) | |
15200 | { } | |
15201 | ||
15202 | Expression* | |
15203 | do_copy() | |
15204 | { | |
15205 | return new Interface_info_expression(this->iface_->copy(), | |
15206 | this->iface_info_, this->location()); | |
15207 | } | |
15208 | ||
ea664253 | 15209 | Bexpression* |
15210 | do_get_backend(Translate_context* context); | |
2387f644 | 15211 | |
15212 | void | |
15213 | do_dump_expression(Ast_dump_context*) const; | |
15214 | ||
15215 | void | |
15216 | do_issue_nil_check() | |
15217 | { this->iface_->issue_nil_check(); } | |
15218 | ||
15219 | private: | |
15220 | // The interface for which we are getting information. | |
15221 | Expression* iface_; | |
15222 | // What information we want. | |
15223 | Interface_info iface_info_; | |
15224 | }; | |
15225 | ||
15226 | // Return the type of the interface info. | |
15227 | ||
15228 | Type* | |
15229 | Interface_info_expression::do_type() | |
15230 | { | |
15231 | switch (this->iface_info_) | |
15232 | { | |
15233 | case INTERFACE_INFO_METHODS: | |
15234 | { | |
625d3118 | 15235 | typedef Unordered_map(Interface_type*, Type*) Hashtable; |
15236 | static Hashtable result_types; | |
15237 | ||
15238 | Interface_type* itype = this->iface_->type()->interface_type(); | |
15239 | ||
15240 | Hashtable::const_iterator p = result_types.find(itype); | |
15241 | if (p != result_types.end()) | |
15242 | return p->second; | |
15243 | ||
2c809f8f | 15244 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
625d3118 | 15245 | if (itype->is_empty()) |
15246 | { | |
15247 | result_types[itype] = pdt; | |
15248 | return pdt; | |
15249 | } | |
2c809f8f | 15250 | |
2387f644 | 15251 | Location loc = this->location(); |
15252 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 15253 | sfl->push_back( |
15254 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
15255 | ||
2387f644 | 15256 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); |
15257 | p != itype->methods()->end(); | |
15258 | ++p) | |
15259 | { | |
15260 | Function_type* ft = p->type()->function_type(); | |
15261 | go_assert(ft->receiver() == NULL); | |
15262 | ||
15263 | const Typed_identifier_list* params = ft->parameters(); | |
15264 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
15265 | if (params != NULL) | |
15266 | mparams->reserve(params->size() + 1); | |
15267 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
15268 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
15269 | if (params != NULL) | |
15270 | { | |
15271 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
15272 | pp != params->end(); | |
15273 | ++pp) | |
15274 | mparams->push_back(*pp); | |
15275 | } | |
15276 | ||
15277 | Typed_identifier_list* mresults = (ft->results() == NULL | |
15278 | ? NULL | |
15279 | : ft->results()->copy()); | |
15280 | Backend_function_type* mft = | |
15281 | Type::make_backend_function_type(NULL, mparams, mresults, | |
15282 | ft->location()); | |
15283 | ||
15284 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
15285 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
15286 | } | |
15287 | ||
6bf4793c | 15288 | Struct_type* st = Type::make_struct_type(sfl, loc); |
15289 | st->set_is_struct_incomparable(); | |
15290 | Pointer_type *pt = Type::make_pointer_type(st); | |
625d3118 | 15291 | result_types[itype] = pt; |
15292 | return pt; | |
2387f644 | 15293 | } |
15294 | case INTERFACE_INFO_OBJECT: | |
15295 | return Type::make_pointer_type(Type::make_void_type()); | |
15296 | default: | |
15297 | go_unreachable(); | |
15298 | } | |
15299 | } | |
15300 | ||
ea664253 | 15301 | // Return the backend representation for interface information. |
2387f644 | 15302 | |
ea664253 | 15303 | Bexpression* |
15304 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 15305 | { |
15306 | Gogo* gogo = context->gogo(); | |
ea664253 | 15307 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 15308 | switch (this->iface_info_) |
15309 | { | |
15310 | case INTERFACE_INFO_METHODS: | |
15311 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 15312 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
15313 | this->location()); | |
2387f644 | 15314 | break; |
15315 | default: | |
15316 | go_unreachable(); | |
15317 | } | |
2387f644 | 15318 | } |
15319 | ||
15320 | // Dump ast representation for an interface info expression. | |
15321 | ||
15322 | void | |
15323 | Interface_info_expression::do_dump_expression( | |
15324 | Ast_dump_context* ast_dump_context) const | |
15325 | { | |
2c809f8f | 15326 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 15327 | ast_dump_context->ostream() << "interfaceinfo("; |
15328 | this->iface_->dump_expression(ast_dump_context); | |
15329 | ast_dump_context->ostream() << ","; | |
15330 | ast_dump_context->ostream() << | |
2c809f8f | 15331 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
15332 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 15333 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
15334 | : "unknown"); | |
15335 | ast_dump_context->ostream() << ")"; | |
15336 | } | |
15337 | ||
15338 | // Make an interface info expression. | |
15339 | ||
15340 | Expression* | |
15341 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
15342 | Location location) | |
15343 | { | |
15344 | return new Interface_info_expression(iface, iface_info, location); | |
15345 | } | |
15346 | ||
2c809f8f | 15347 | // An expression that represents an interface value. The first field is either |
15348 | // a type descriptor for an empty interface or a pointer to the interface method | |
15349 | // table for a non-empty interface. The second field is always the object. | |
15350 | ||
15351 | class Interface_value_expression : public Expression | |
15352 | { | |
15353 | public: | |
15354 | Interface_value_expression(Type* type, Expression* first_field, | |
15355 | Expression* obj, Location location) | |
15356 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
15357 | type_(type), first_field_(first_field), obj_(obj) | |
15358 | { } | |
15359 | ||
15360 | protected: | |
15361 | int | |
15362 | do_traverse(Traverse*); | |
15363 | ||
15364 | Type* | |
15365 | do_type() | |
15366 | { return this->type_; } | |
15367 | ||
15368 | void | |
15369 | do_determine_type(const Type_context*) | |
15370 | { go_unreachable(); } | |
15371 | ||
15372 | Expression* | |
15373 | do_copy() | |
15374 | { | |
de590a61 | 15375 | return new Interface_value_expression(this->type_->copy_expressions(), |
2c809f8f | 15376 | this->first_field_->copy(), |
15377 | this->obj_->copy(), this->location()); | |
15378 | } | |
15379 | ||
ea664253 | 15380 | Bexpression* |
15381 | do_get_backend(Translate_context* context); | |
2c809f8f | 15382 | |
15383 | void | |
15384 | do_dump_expression(Ast_dump_context*) const; | |
15385 | ||
15386 | private: | |
15387 | // The type of the interface value. | |
15388 | Type* type_; | |
15389 | // The first field of the interface (either a type descriptor or a pointer | |
15390 | // to the method table. | |
15391 | Expression* first_field_; | |
15392 | // The underlying object of the interface. | |
15393 | Expression* obj_; | |
15394 | }; | |
15395 | ||
15396 | int | |
15397 | Interface_value_expression::do_traverse(Traverse* traverse) | |
15398 | { | |
15399 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
15400 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
15401 | return TRAVERSE_EXIT; | |
15402 | return TRAVERSE_CONTINUE; | |
15403 | } | |
15404 | ||
ea664253 | 15405 | Bexpression* |
15406 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15407 | { |
15408 | std::vector<Bexpression*> vals(2); | |
ea664253 | 15409 | vals[0] = this->first_field_->get_backend(context); |
15410 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 15411 | |
15412 | Gogo* gogo = context->gogo(); | |
15413 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 15414 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 15415 | } |
15416 | ||
15417 | void | |
15418 | Interface_value_expression::do_dump_expression( | |
15419 | Ast_dump_context* ast_dump_context) const | |
15420 | { | |
15421 | ast_dump_context->ostream() << "interfacevalue("; | |
15422 | ast_dump_context->ostream() << | |
15423 | (this->type_->interface_type()->is_empty() | |
15424 | ? "type_descriptor: " | |
15425 | : "methods: "); | |
15426 | this->first_field_->dump_expression(ast_dump_context); | |
15427 | ast_dump_context->ostream() << ", object: "; | |
15428 | this->obj_->dump_expression(ast_dump_context); | |
15429 | ast_dump_context->ostream() << ")"; | |
15430 | } | |
15431 | ||
15432 | Expression* | |
15433 | Expression::make_interface_value(Type* type, Expression* first_value, | |
15434 | Expression* object, Location location) | |
15435 | { | |
15436 | return new Interface_value_expression(type, first_value, object, location); | |
15437 | } | |
15438 | ||
15439 | // An interface method table for a pair of types: an interface type and a type | |
15440 | // that implements that interface. | |
15441 | ||
15442 | class Interface_mtable_expression : public Expression | |
15443 | { | |
15444 | public: | |
15445 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
15446 | bool is_pointer, Location location) | |
15447 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
15448 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
15449 | method_table_type_(NULL), bvar_(NULL) | |
15450 | { } | |
15451 | ||
15452 | protected: | |
15453 | int | |
15454 | do_traverse(Traverse*); | |
15455 | ||
15456 | Type* | |
15457 | do_type(); | |
15458 | ||
15459 | bool | |
3ae06f68 | 15460 | do_is_static_initializer() const |
2c809f8f | 15461 | { return true; } |
15462 | ||
15463 | void | |
15464 | do_determine_type(const Type_context*) | |
15465 | { go_unreachable(); } | |
15466 | ||
15467 | Expression* | |
15468 | do_copy() | |
15469 | { | |
de590a61 | 15470 | Interface_type* itype = this->itype_->copy_expressions()->interface_type(); |
15471 | return new Interface_mtable_expression(itype, | |
15472 | this->type_->copy_expressions(), | |
2c809f8f | 15473 | this->is_pointer_, this->location()); |
15474 | } | |
15475 | ||
15476 | bool | |
15477 | do_is_addressable() const | |
15478 | { return true; } | |
15479 | ||
ea664253 | 15480 | Bexpression* |
15481 | do_get_backend(Translate_context* context); | |
2c809f8f | 15482 | |
15483 | void | |
15484 | do_dump_expression(Ast_dump_context*) const; | |
15485 | ||
15486 | private: | |
15487 | // The interface type for which the methods are defined. | |
15488 | Interface_type* itype_; | |
15489 | // The type to construct the interface method table for. | |
15490 | Type* type_; | |
15491 | // Whether this table contains the method set for the receiver type or the | |
15492 | // pointer receiver type. | |
15493 | bool is_pointer_; | |
15494 | // The type of the method table. | |
15495 | Type* method_table_type_; | |
15496 | // The backend variable that refers to the interface method table. | |
15497 | Bvariable* bvar_; | |
15498 | }; | |
15499 | ||
15500 | int | |
15501 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
15502 | { | |
15503 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
15504 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
15505 | return TRAVERSE_EXIT; | |
15506 | return TRAVERSE_CONTINUE; | |
15507 | } | |
15508 | ||
15509 | Type* | |
15510 | Interface_mtable_expression::do_type() | |
15511 | { | |
15512 | if (this->method_table_type_ != NULL) | |
15513 | return this->method_table_type_; | |
15514 | ||
15515 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15516 | go_assert(!interface_methods->empty()); | |
15517 | ||
15518 | Struct_field_list* sfl = new Struct_field_list; | |
15519 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
15520 | this->location()); | |
15521 | sfl->push_back(Struct_field(tid)); | |
db122cb9 | 15522 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); |
2c809f8f | 15523 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); |
15524 | p != interface_methods->end(); | |
15525 | ++p) | |
db122cb9 | 15526 | { |
15527 | // We want C function pointers here, not func descriptors; model | |
15528 | // using void* pointers. | |
15529 | Typed_identifier method(p->name(), unsafe_ptr_type, p->location()); | |
15530 | sfl->push_back(Struct_field(method)); | |
15531 | } | |
6bf4793c | 15532 | Struct_type* st = Type::make_struct_type(sfl, this->location()); |
15533 | st->set_is_struct_incomparable(); | |
15534 | this->method_table_type_ = st; | |
2c809f8f | 15535 | return this->method_table_type_; |
15536 | } | |
15537 | ||
ea664253 | 15538 | Bexpression* |
15539 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15540 | { |
15541 | Gogo* gogo = context->gogo(); | |
2c809f8f | 15542 | Location loc = Linemap::predeclared_location(); |
15543 | if (this->bvar_ != NULL) | |
7af8e400 | 15544 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15545 | |
15546 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15547 | go_assert(!interface_methods->empty()); | |
15548 | ||
19272321 | 15549 | std::string mangled_name = |
15550 | gogo->interface_method_table_name(this->itype_, this->type_, | |
15551 | this->is_pointer_); | |
2c809f8f | 15552 | |
1530c754 | 15553 | // Set is_public if we are converting a named type to an interface |
15554 | // type that is defined in the same package as the named type, and | |
15555 | // the interface has hidden methods. In that case the interface | |
15556 | // method table will be defined by the package that defines the | |
15557 | // types. | |
15558 | bool is_public = false; | |
15559 | if (this->type_->named_type() != NULL | |
15560 | && (this->type_->named_type()->named_object()->package() | |
15561 | == this->itype_->package())) | |
15562 | { | |
15563 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15564 | p != interface_methods->end(); | |
15565 | ++p) | |
2c809f8f | 15566 | { |
1530c754 | 15567 | if (Gogo::is_hidden_name(p->name())) |
15568 | { | |
15569 | is_public = true; | |
15570 | break; | |
15571 | } | |
2c809f8f | 15572 | } |
15573 | } | |
15574 | ||
1530c754 | 15575 | if (is_public |
2c809f8f | 15576 | && this->type_->named_type()->named_object()->package() != NULL) |
15577 | { | |
1530c754 | 15578 | // The interface conversion table is defined elsewhere. |
2c809f8f | 15579 | Btype* btype = this->type()->get_backend(gogo); |
438b4bec | 15580 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
2c809f8f | 15581 | this->bvar_ = |
438b4bec | 15582 | gogo->backend()->immutable_struct_reference(mangled_name, asm_name, |
15583 | btype, loc); | |
7af8e400 | 15584 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15585 | } |
15586 | ||
15587 | // The first element is the type descriptor. | |
15588 | Type* td_type; | |
15589 | if (!this->is_pointer_) | |
15590 | td_type = this->type_; | |
15591 | else | |
15592 | td_type = Type::make_pointer_type(this->type_); | |
15593 | ||
db122cb9 | 15594 | std::vector<Backend::Btyped_identifier> bstructfields; |
15595 | ||
2c809f8f | 15596 | // Build an interface method table for a type: a type descriptor followed by a |
15597 | // list of function pointers, one for each interface method. This is used for | |
15598 | // interfaces. | |
15599 | Expression_list* svals = new Expression_list(); | |
db122cb9 | 15600 | Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc); |
15601 | svals->push_back(tdescriptor); | |
15602 | ||
15603 | Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo); | |
15604 | Backend::Btyped_identifier btd("_type", tdesc_btype, loc); | |
15605 | bstructfields.push_back(btd); | |
2c809f8f | 15606 | |
15607 | Named_type* nt = this->type_->named_type(); | |
15608 | Struct_type* st = this->type_->struct_type(); | |
15609 | go_assert(nt != NULL || st != NULL); | |
15610 | ||
15611 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15612 | p != interface_methods->end(); | |
15613 | ++p) | |
15614 | { | |
15615 | bool is_ambiguous; | |
15616 | Method* m; | |
15617 | if (nt != NULL) | |
15618 | m = nt->method_function(p->name(), &is_ambiguous); | |
15619 | else | |
15620 | m = st->method_function(p->name(), &is_ambiguous); | |
15621 | go_assert(m != NULL); | |
15622 | Named_object* no = m->named_object(); | |
15623 | ||
15624 | go_assert(no->is_function() || no->is_function_declaration()); | |
db122cb9 | 15625 | |
15626 | Btype* fcn_btype = m->type()->get_backend_fntype(gogo); | |
15627 | Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc); | |
15628 | bstructfields.push_back(bmtype); | |
15629 | ||
2c809f8f | 15630 | svals->push_back(Expression::make_func_code_reference(no, loc)); |
15631 | } | |
15632 | ||
db122cb9 | 15633 | Btype *btype = gogo->backend()->struct_type(bstructfields); |
15634 | std::vector<Bexpression*> ctor_bexprs; | |
15635 | for (Expression_list::const_iterator pe = svals->begin(); | |
15636 | pe != svals->end(); | |
15637 | ++pe) | |
15638 | { | |
15639 | ctor_bexprs.push_back((*pe)->get_backend(context)); | |
15640 | } | |
15641 | Bexpression* ctor = | |
15642 | gogo->backend()->constructor_expression(btype, ctor_bexprs, loc); | |
2c809f8f | 15643 | |
438b4bec | 15644 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
15645 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false, | |
2c809f8f | 15646 | !is_public, btype, loc); |
15647 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
15648 | !is_public, btype, loc, ctor); | |
7af8e400 | 15649 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 15650 | } |
15651 | ||
15652 | void | |
15653 | Interface_mtable_expression::do_dump_expression( | |
15654 | Ast_dump_context* ast_dump_context) const | |
15655 | { | |
15656 | ast_dump_context->ostream() << "__go_" | |
15657 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
15658 | ast_dump_context->dump_type(this->itype_); | |
15659 | ast_dump_context->ostream() << "__"; | |
15660 | ast_dump_context->dump_type(this->type_); | |
15661 | } | |
15662 | ||
15663 | Expression* | |
15664 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
15665 | bool is_pointer, Location location) | |
15666 | { | |
15667 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
15668 | } | |
15669 | ||
e440a328 | 15670 | // An expression which evaluates to the offset of a field within a |
15671 | // struct. This, like Type_info_expression, q.v., is only used to | |
15672 | // initialize fields of a type descriptor. | |
15673 | ||
15674 | class Struct_field_offset_expression : public Expression | |
15675 | { | |
15676 | public: | |
15677 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 15678 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
15679 | Linemap::predeclared_location()), | |
e440a328 | 15680 | type_(type), field_(field) |
15681 | { } | |
15682 | ||
15683 | protected: | |
f23d7786 | 15684 | bool |
3ae06f68 | 15685 | do_is_static_initializer() const |
f23d7786 | 15686 | { return true; } |
15687 | ||
e440a328 | 15688 | Type* |
15689 | do_type() | |
15690 | { return Type::lookup_integer_type("uintptr"); } | |
15691 | ||
15692 | void | |
15693 | do_determine_type(const Type_context*) | |
15694 | { } | |
15695 | ||
15696 | Expression* | |
15697 | do_copy() | |
15698 | { return this; } | |
15699 | ||
ea664253 | 15700 | Bexpression* |
15701 | do_get_backend(Translate_context* context); | |
e440a328 | 15702 | |
d751bb78 | 15703 | void |
15704 | do_dump_expression(Ast_dump_context*) const; | |
15705 | ||
e440a328 | 15706 | private: |
15707 | // The type of the struct. | |
15708 | Struct_type* type_; | |
15709 | // The field. | |
15710 | const Struct_field* field_; | |
15711 | }; | |
15712 | ||
ea664253 | 15713 | // Return the backend representation for a struct field offset. |
e440a328 | 15714 | |
ea664253 | 15715 | Bexpression* |
15716 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 15717 | { |
e440a328 | 15718 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 15719 | Struct_field_list::const_iterator p; |
2c8bda43 | 15720 | unsigned i = 0; |
e440a328 | 15721 | for (p = fields->begin(); |
15722 | p != fields->end(); | |
2c8bda43 | 15723 | ++p, ++i) |
15724 | if (&*p == this->field_) | |
15725 | break; | |
c484d925 | 15726 | go_assert(&*p == this->field_); |
e440a328 | 15727 | |
2c8bda43 | 15728 | Gogo* gogo = context->gogo(); |
15729 | Btype* btype = this->type_->get_backend(gogo); | |
15730 | ||
3f378015 | 15731 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 15732 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 15733 | Expression* ret = |
3f378015 | 15734 | Expression::make_integer_int64(offset, uptr_type, |
15735 | Linemap::predeclared_location()); | |
ea664253 | 15736 | return ret->get_backend(context); |
e440a328 | 15737 | } |
15738 | ||
d751bb78 | 15739 | // Dump ast representation for a struct field offset expression. |
15740 | ||
15741 | void | |
15742 | Struct_field_offset_expression::do_dump_expression( | |
15743 | Ast_dump_context* ast_dump_context) const | |
15744 | { | |
15745 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 15746 | ast_dump_context->dump_type(this->type_); |
15747 | ast_dump_context->ostream() << '.'; | |
15748 | ast_dump_context->ostream() << | |
15749 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 15750 | ast_dump_context->ostream() << ")"; |
15751 | } | |
15752 | ||
e440a328 | 15753 | // Make an expression for a struct field offset. |
15754 | ||
15755 | Expression* | |
15756 | Expression::make_struct_field_offset(Struct_type* type, | |
15757 | const Struct_field* field) | |
15758 | { | |
15759 | return new Struct_field_offset_expression(type, field); | |
15760 | } | |
15761 | ||
15762 | // An expression which evaluates to the address of an unnamed label. | |
15763 | ||
15764 | class Label_addr_expression : public Expression | |
15765 | { | |
15766 | public: | |
b13c66cd | 15767 | Label_addr_expression(Label* label, Location location) |
e440a328 | 15768 | : Expression(EXPRESSION_LABEL_ADDR, location), |
15769 | label_(label) | |
15770 | { } | |
15771 | ||
15772 | protected: | |
15773 | Type* | |
15774 | do_type() | |
15775 | { return Type::make_pointer_type(Type::make_void_type()); } | |
15776 | ||
15777 | void | |
15778 | do_determine_type(const Type_context*) | |
15779 | { } | |
15780 | ||
15781 | Expression* | |
15782 | do_copy() | |
15783 | { return new Label_addr_expression(this->label_, this->location()); } | |
15784 | ||
ea664253 | 15785 | Bexpression* |
15786 | do_get_backend(Translate_context* context) | |
15787 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 15788 | |
d751bb78 | 15789 | void |
15790 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15791 | { ast_dump_context->ostream() << this->label_->name(); } | |
15792 | ||
e440a328 | 15793 | private: |
15794 | // The label whose address we are taking. | |
15795 | Label* label_; | |
15796 | }; | |
15797 | ||
15798 | // Make an expression for the address of an unnamed label. | |
15799 | ||
15800 | Expression* | |
b13c66cd | 15801 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 15802 | { |
15803 | return new Label_addr_expression(label, location); | |
15804 | } | |
15805 | ||
da244e59 | 15806 | // Class Conditional_expression. |
283a177b | 15807 | |
2c809f8f | 15808 | // Traversal. |
15809 | ||
15810 | int | |
15811 | Conditional_expression::do_traverse(Traverse* traverse) | |
15812 | { | |
15813 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15814 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15815 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15816 | return TRAVERSE_EXIT; | |
15817 | return TRAVERSE_CONTINUE; | |
15818 | } | |
15819 | ||
283a177b | 15820 | // Return the type of the conditional expression. |
15821 | ||
15822 | Type* | |
15823 | Conditional_expression::do_type() | |
15824 | { | |
15825 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15826 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15827 | NULL)) | |
283a177b | 15828 | result_type = this->then_->type(); |
15829 | else if (this->then_->is_nil_expression() | |
15830 | || this->else_->is_nil_expression()) | |
15831 | result_type = (!this->then_->is_nil_expression() | |
15832 | ? this->then_->type() | |
15833 | : this->else_->type()); | |
15834 | return result_type; | |
15835 | } | |
15836 | ||
2c809f8f | 15837 | // Determine type for a conditional expression. |
15838 | ||
15839 | void | |
15840 | Conditional_expression::do_determine_type(const Type_context* context) | |
15841 | { | |
15842 | this->cond_->determine_type_no_context(); | |
15843 | this->then_->determine_type(context); | |
15844 | this->else_->determine_type(context); | |
15845 | } | |
15846 | ||
283a177b | 15847 | // Get the backend representation of a conditional expression. |
15848 | ||
ea664253 | 15849 | Bexpression* |
15850 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 15851 | { |
15852 | Gogo* gogo = context->gogo(); | |
15853 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 15854 | Bexpression* cond = this->cond_->get_backend(context); |
15855 | Bexpression* then = this->then_->get_backend(context); | |
15856 | Bexpression* belse = this->else_->get_backend(context); | |
93715b75 | 15857 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
15858 | return gogo->backend()->conditional_expression(bfn, result_btype, cond, then, | |
ea664253 | 15859 | belse, this->location()); |
283a177b | 15860 | } |
15861 | ||
15862 | // Dump ast representation of a conditional expression. | |
15863 | ||
15864 | void | |
15865 | Conditional_expression::do_dump_expression( | |
15866 | Ast_dump_context* ast_dump_context) const | |
15867 | { | |
15868 | ast_dump_context->ostream() << "("; | |
15869 | ast_dump_context->dump_expression(this->cond_); | |
15870 | ast_dump_context->ostream() << " ? "; | |
15871 | ast_dump_context->dump_expression(this->then_); | |
15872 | ast_dump_context->ostream() << " : "; | |
15873 | ast_dump_context->dump_expression(this->else_); | |
15874 | ast_dump_context->ostream() << ") "; | |
15875 | } | |
15876 | ||
15877 | // Make a conditional expression. | |
15878 | ||
15879 | Expression* | |
15880 | Expression::make_conditional(Expression* cond, Expression* then, | |
15881 | Expression* else_expr, Location location) | |
15882 | { | |
15883 | return new Conditional_expression(cond, then, else_expr, location); | |
15884 | } | |
15885 | ||
da244e59 | 15886 | // Class Compound_expression. |
2c809f8f | 15887 | |
15888 | // Traversal. | |
15889 | ||
15890 | int | |
15891 | Compound_expression::do_traverse(Traverse* traverse) | |
15892 | { | |
15893 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15894 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15895 | return TRAVERSE_EXIT; | |
15896 | return TRAVERSE_CONTINUE; | |
15897 | } | |
15898 | ||
15899 | // Return the type of the compound expression. | |
15900 | ||
15901 | Type* | |
15902 | Compound_expression::do_type() | |
15903 | { | |
15904 | return this->expr_->type(); | |
15905 | } | |
15906 | ||
15907 | // Determine type for a compound expression. | |
15908 | ||
15909 | void | |
15910 | Compound_expression::do_determine_type(const Type_context* context) | |
15911 | { | |
15912 | this->init_->determine_type_no_context(); | |
15913 | this->expr_->determine_type(context); | |
15914 | } | |
15915 | ||
15916 | // Get the backend representation of a compound expression. | |
15917 | ||
ea664253 | 15918 | Bexpression* |
15919 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15920 | { |
15921 | Gogo* gogo = context->gogo(); | |
ea664253 | 15922 | Bexpression* binit = this->init_->get_backend(context); |
0ab48656 | 15923 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
15924 | Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction, | |
15925 | binit); | |
ea664253 | 15926 | Bexpression* bexpr = this->expr_->get_backend(context); |
15927 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15928 | this->location()); | |
2c809f8f | 15929 | } |
15930 | ||
15931 | // Dump ast representation of a conditional expression. | |
15932 | ||
15933 | void | |
15934 | Compound_expression::do_dump_expression( | |
15935 | Ast_dump_context* ast_dump_context) const | |
15936 | { | |
15937 | ast_dump_context->ostream() << "("; | |
15938 | ast_dump_context->dump_expression(this->init_); | |
15939 | ast_dump_context->ostream() << ","; | |
15940 | ast_dump_context->dump_expression(this->expr_); | |
15941 | ast_dump_context->ostream() << ") "; | |
15942 | } | |
15943 | ||
15944 | // Make a compound expression. | |
15945 | ||
15946 | Expression* | |
15947 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15948 | { | |
15949 | return new Compound_expression(init, expr, location); | |
15950 | } | |
15951 | ||
1b4fb1e0 | 15952 | // Class Backend_expression. |
15953 | ||
15954 | int | |
15955 | Backend_expression::do_traverse(Traverse*) | |
15956 | { | |
15957 | return TRAVERSE_CONTINUE; | |
15958 | } | |
15959 | ||
de590a61 | 15960 | Expression* |
15961 | Backend_expression::do_copy() | |
15962 | { | |
15963 | return new Backend_expression(this->bexpr_, this->type_->copy_expressions(), | |
15964 | this->location()); | |
15965 | } | |
15966 | ||
1b4fb1e0 | 15967 | void |
15968 | Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15969 | { | |
15970 | ast_dump_context->ostream() << "backend_expression<"; | |
15971 | ast_dump_context->dump_type(this->type_); | |
15972 | ast_dump_context->ostream() << ">"; | |
15973 | } | |
15974 | ||
15975 | Expression* | |
15976 | Expression::make_backend(Bexpression* bexpr, Type* type, Location location) | |
15977 | { | |
15978 | return new Backend_expression(bexpr, type, location); | |
15979 | } | |
15980 | ||
e440a328 | 15981 | // Import an expression. This comes at the end in order to see the |
15982 | // various class definitions. | |
15983 | ||
15984 | Expression* | |
15985 | Expression::import_expression(Import* imp) | |
15986 | { | |
15987 | int c = imp->peek_char(); | |
15988 | if (imp->match_c_string("- ") | |
15989 | || imp->match_c_string("! ") | |
15990 | || imp->match_c_string("^ ")) | |
15991 | return Unary_expression::do_import(imp); | |
15992 | else if (c == '(') | |
15993 | return Binary_expression::do_import(imp); | |
15994 | else if (imp->match_c_string("true") | |
15995 | || imp->match_c_string("false")) | |
15996 | return Boolean_expression::do_import(imp); | |
15997 | else if (c == '"') | |
15998 | return String_expression::do_import(imp); | |
15999 | else if (c == '-' || (c >= '0' && c <= '9')) | |
16000 | { | |
16001 | // This handles integers, floats and complex constants. | |
16002 | return Integer_expression::do_import(imp); | |
16003 | } | |
16004 | else if (imp->match_c_string("nil")) | |
16005 | return Nil_expression::do_import(imp); | |
16006 | else if (imp->match_c_string("convert")) | |
16007 | return Type_conversion_expression::do_import(imp); | |
16008 | else | |
16009 | { | |
631d5788 | 16010 | go_error_at(imp->location(), "import error: expected expression"); |
e440a328 | 16011 | return Expression::make_error(imp->location()); |
16012 | } | |
16013 | } | |
16014 | ||
16015 | // Class Expression_list. | |
16016 | ||
16017 | // Traverse the list. | |
16018 | ||
16019 | int | |
16020 | Expression_list::traverse(Traverse* traverse) | |
16021 | { | |
16022 | for (Expression_list::iterator p = this->begin(); | |
16023 | p != this->end(); | |
16024 | ++p) | |
16025 | { | |
16026 | if (*p != NULL) | |
16027 | { | |
16028 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
16029 | return TRAVERSE_EXIT; | |
16030 | } | |
16031 | } | |
16032 | return TRAVERSE_CONTINUE; | |
16033 | } | |
16034 | ||
16035 | // Copy the list. | |
16036 | ||
16037 | Expression_list* | |
16038 | Expression_list::copy() | |
16039 | { | |
16040 | Expression_list* ret = new Expression_list(); | |
16041 | for (Expression_list::iterator p = this->begin(); | |
16042 | p != this->end(); | |
16043 | ++p) | |
16044 | { | |
16045 | if (*p == NULL) | |
16046 | ret->push_back(NULL); | |
16047 | else | |
16048 | ret->push_back((*p)->copy()); | |
16049 | } | |
16050 | return ret; | |
16051 | } | |
16052 | ||
16053 | // Return whether an expression list has an error expression. | |
16054 | ||
16055 | bool | |
16056 | Expression_list::contains_error() const | |
16057 | { | |
16058 | for (Expression_list::const_iterator p = this->begin(); | |
16059 | p != this->end(); | |
16060 | ++p) | |
16061 | if (*p != NULL && (*p)->is_error_expression()) | |
16062 | return true; | |
16063 | return false; | |
16064 | } | |
0c77715b | 16065 | |
16066 | // Class Numeric_constant. | |
16067 | ||
16068 | // Destructor. | |
16069 | ||
16070 | Numeric_constant::~Numeric_constant() | |
16071 | { | |
16072 | this->clear(); | |
16073 | } | |
16074 | ||
16075 | // Copy constructor. | |
16076 | ||
16077 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
16078 | : classification_(a.classification_), type_(a.type_) | |
16079 | { | |
16080 | switch (a.classification_) | |
16081 | { | |
16082 | case NC_INVALID: | |
16083 | break; | |
16084 | case NC_INT: | |
16085 | case NC_RUNE: | |
16086 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
16087 | break; | |
16088 | case NC_FLOAT: | |
16089 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
16090 | break; | |
16091 | case NC_COMPLEX: | |
fcbea5e4 | 16092 | mpc_init2(this->u_.complex_val, mpc_precision); |
16093 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 16094 | break; |
16095 | default: | |
16096 | go_unreachable(); | |
16097 | } | |
16098 | } | |
16099 | ||
16100 | // Assignment operator. | |
16101 | ||
16102 | Numeric_constant& | |
16103 | Numeric_constant::operator=(const Numeric_constant& a) | |
16104 | { | |
16105 | this->clear(); | |
16106 | this->classification_ = a.classification_; | |
16107 | this->type_ = a.type_; | |
16108 | switch (a.classification_) | |
16109 | { | |
16110 | case NC_INVALID: | |
16111 | break; | |
16112 | case NC_INT: | |
16113 | case NC_RUNE: | |
16114 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
16115 | break; | |
16116 | case NC_FLOAT: | |
16117 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
16118 | break; | |
16119 | case NC_COMPLEX: | |
fcbea5e4 | 16120 | mpc_init2(this->u_.complex_val, mpc_precision); |
16121 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 16122 | break; |
16123 | default: | |
16124 | go_unreachable(); | |
16125 | } | |
16126 | return *this; | |
16127 | } | |
16128 | ||
16129 | // Clear the contents. | |
16130 | ||
16131 | void | |
16132 | Numeric_constant::clear() | |
16133 | { | |
16134 | switch (this->classification_) | |
16135 | { | |
16136 | case NC_INVALID: | |
16137 | break; | |
16138 | case NC_INT: | |
16139 | case NC_RUNE: | |
16140 | mpz_clear(this->u_.int_val); | |
16141 | break; | |
16142 | case NC_FLOAT: | |
16143 | mpfr_clear(this->u_.float_val); | |
16144 | break; | |
16145 | case NC_COMPLEX: | |
fcbea5e4 | 16146 | mpc_clear(this->u_.complex_val); |
0c77715b | 16147 | break; |
16148 | default: | |
16149 | go_unreachable(); | |
16150 | } | |
16151 | this->classification_ = NC_INVALID; | |
16152 | } | |
16153 | ||
16154 | // Set to an unsigned long value. | |
16155 | ||
16156 | void | |
16157 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
16158 | { | |
16159 | this->clear(); | |
16160 | this->classification_ = NC_INT; | |
16161 | this->type_ = type; | |
16162 | mpz_init_set_ui(this->u_.int_val, val); | |
16163 | } | |
16164 | ||
16165 | // Set to an integer value. | |
16166 | ||
16167 | void | |
16168 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
16169 | { | |
16170 | this->clear(); | |
16171 | this->classification_ = NC_INT; | |
16172 | this->type_ = type; | |
16173 | mpz_init_set(this->u_.int_val, val); | |
16174 | } | |
16175 | ||
16176 | // Set to a rune value. | |
16177 | ||
16178 | void | |
16179 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
16180 | { | |
16181 | this->clear(); | |
16182 | this->classification_ = NC_RUNE; | |
16183 | this->type_ = type; | |
16184 | mpz_init_set(this->u_.int_val, val); | |
16185 | } | |
16186 | ||
16187 | // Set to a floating point value. | |
16188 | ||
16189 | void | |
16190 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
16191 | { | |
16192 | this->clear(); | |
16193 | this->classification_ = NC_FLOAT; | |
16194 | this->type_ = type; | |
d1d4ace3 | 16195 | |
833b523c | 16196 | // Numeric constants do not have negative zero values, so remove |
16197 | // them here. They also don't have infinity or NaN values, but we | |
16198 | // should never see them here. | |
d1d4ace3 | 16199 | int bits = 0; |
16200 | if (type != NULL | |
16201 | && type->float_type() != NULL | |
16202 | && !type->float_type()->is_abstract()) | |
16203 | bits = type->float_type()->bits(); | |
16204 | if (Numeric_constant::is_float_neg_zero(val, bits)) | |
833b523c | 16205 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); |
16206 | else | |
16207 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 16208 | } |
16209 | ||
16210 | // Set to a complex value. | |
16211 | ||
16212 | void | |
fcbea5e4 | 16213 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 16214 | { |
16215 | this->clear(); | |
16216 | this->classification_ = NC_COMPLEX; | |
16217 | this->type_ = type; | |
d1d4ace3 | 16218 | |
16219 | // Avoid negative zero as in set_float. | |
16220 | int bits = 0; | |
16221 | if (type != NULL | |
16222 | && type->complex_type() != NULL | |
16223 | && !type->complex_type()->is_abstract()) | |
16224 | bits = type->complex_type()->bits() / 2; | |
16225 | ||
16226 | mpfr_t real; | |
16227 | mpfr_init_set(real, mpc_realref(val), GMP_RNDN); | |
16228 | if (Numeric_constant::is_float_neg_zero(real, bits)) | |
16229 | mpfr_set_ui(real, 0, GMP_RNDN); | |
16230 | ||
16231 | mpfr_t imag; | |
16232 | mpfr_init_set(imag, mpc_imagref(val), GMP_RNDN); | |
16233 | if (Numeric_constant::is_float_neg_zero(imag, bits)) | |
16234 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
16235 | ||
fcbea5e4 | 16236 | mpc_init2(this->u_.complex_val, mpc_precision); |
d1d4ace3 | 16237 | mpc_set_fr_fr(this->u_.complex_val, real, imag, MPC_RNDNN); |
16238 | ||
16239 | mpfr_clear(real); | |
16240 | mpfr_clear(imag); | |
16241 | } | |
16242 | ||
16243 | // Return whether VAL, at a precision of BITS, is a negative zero. | |
16244 | // BITS may be zero in which case it is ignored. | |
16245 | ||
16246 | bool | |
16247 | Numeric_constant::is_float_neg_zero(const mpfr_t val, int bits) | |
16248 | { | |
16249 | if (!mpfr_signbit(val)) | |
16250 | return false; | |
16251 | if (mpfr_zero_p(val)) | |
16252 | return true; | |
16253 | mp_exp_t min_exp; | |
16254 | switch (bits) | |
16255 | { | |
16256 | case 0: | |
16257 | return false; | |
16258 | case 32: | |
16259 | // In a denormalized float32 the exponent is -126, and there are | |
16260 | // 24 bits of which at least the last must be 1, so the smallest | |
16261 | // representable non-zero exponent is -126 - (24 - 1) == -149. | |
16262 | min_exp = -149; | |
16263 | break; | |
16264 | case 64: | |
16265 | // Minimum exponent is -1022, there are 53 bits. | |
16266 | min_exp = -1074; | |
16267 | break; | |
16268 | default: | |
16269 | go_unreachable(); | |
16270 | } | |
16271 | return mpfr_get_exp(val) < min_exp; | |
0c77715b | 16272 | } |
16273 | ||
16274 | // Get an int value. | |
16275 | ||
16276 | void | |
16277 | Numeric_constant::get_int(mpz_t* val) const | |
16278 | { | |
16279 | go_assert(this->is_int()); | |
16280 | mpz_init_set(*val, this->u_.int_val); | |
16281 | } | |
16282 | ||
16283 | // Get a rune value. | |
16284 | ||
16285 | void | |
16286 | Numeric_constant::get_rune(mpz_t* val) const | |
16287 | { | |
16288 | go_assert(this->is_rune()); | |
16289 | mpz_init_set(*val, this->u_.int_val); | |
16290 | } | |
16291 | ||
16292 | // Get a floating point value. | |
16293 | ||
16294 | void | |
16295 | Numeric_constant::get_float(mpfr_t* val) const | |
16296 | { | |
16297 | go_assert(this->is_float()); | |
16298 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16299 | } | |
16300 | ||
16301 | // Get a complex value. | |
16302 | ||
16303 | void | |
fcbea5e4 | 16304 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 16305 | { |
16306 | go_assert(this->is_complex()); | |
fcbea5e4 | 16307 | mpc_init2(*val, mpc_precision); |
16308 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 16309 | } |
16310 | ||
16311 | // Express value as unsigned long if possible. | |
16312 | ||
16313 | Numeric_constant::To_unsigned_long | |
16314 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
16315 | { | |
16316 | switch (this->classification_) | |
16317 | { | |
16318 | case NC_INT: | |
16319 | case NC_RUNE: | |
16320 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
16321 | case NC_FLOAT: | |
16322 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
16323 | case NC_COMPLEX: | |
fcbea5e4 | 16324 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16325 | return NC_UL_NOTINT; |
fcbea5e4 | 16326 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
16327 | val); | |
0c77715b | 16328 | default: |
16329 | go_unreachable(); | |
16330 | } | |
16331 | } | |
16332 | ||
16333 | // Express integer value as unsigned long if possible. | |
16334 | ||
16335 | Numeric_constant::To_unsigned_long | |
16336 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
16337 | unsigned long *val) const | |
16338 | { | |
16339 | if (mpz_sgn(ival) < 0) | |
16340 | return NC_UL_NEGATIVE; | |
16341 | unsigned long ui = mpz_get_ui(ival); | |
16342 | if (mpz_cmp_ui(ival, ui) != 0) | |
16343 | return NC_UL_BIG; | |
16344 | *val = ui; | |
16345 | return NC_UL_VALID; | |
16346 | } | |
16347 | ||
16348 | // Express floating point value as unsigned long if possible. | |
16349 | ||
16350 | Numeric_constant::To_unsigned_long | |
16351 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
16352 | unsigned long *val) const | |
16353 | { | |
16354 | if (!mpfr_integer_p(fval)) | |
16355 | return NC_UL_NOTINT; | |
16356 | mpz_t ival; | |
16357 | mpz_init(ival); | |
16358 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16359 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
16360 | mpz_clear(ival); | |
16361 | return ret; | |
16362 | } | |
16363 | ||
03118c21 | 16364 | // Express value as memory size if possible. |
16365 | ||
16366 | bool | |
16367 | Numeric_constant::to_memory_size(int64_t* val) const | |
16368 | { | |
16369 | switch (this->classification_) | |
16370 | { | |
16371 | case NC_INT: | |
16372 | case NC_RUNE: | |
16373 | return this->mpz_to_memory_size(this->u_.int_val, val); | |
16374 | case NC_FLOAT: | |
16375 | return this->mpfr_to_memory_size(this->u_.float_val, val); | |
16376 | case NC_COMPLEX: | |
16377 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
16378 | return false; | |
16379 | return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val); | |
16380 | default: | |
16381 | go_unreachable(); | |
16382 | } | |
16383 | } | |
16384 | ||
16385 | // Express integer as memory size if possible. | |
16386 | ||
16387 | bool | |
16388 | Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const | |
16389 | { | |
16390 | if (mpz_sgn(ival) < 0) | |
16391 | return false; | |
16392 | if (mpz_fits_slong_p(ival)) | |
16393 | { | |
16394 | *val = static_cast<int64_t>(mpz_get_si(ival)); | |
16395 | return true; | |
16396 | } | |
16397 | ||
16398 | // Test >= 64, not > 64, because an int64_t can hold 63 bits of a | |
16399 | // positive value. | |
16400 | if (mpz_sizeinbase(ival, 2) >= 64) | |
16401 | return false; | |
16402 | ||
16403 | mpz_t q, r; | |
16404 | mpz_init(q); | |
16405 | mpz_init(r); | |
16406 | mpz_tdiv_q_2exp(q, ival, 32); | |
16407 | mpz_tdiv_r_2exp(r, ival, 32); | |
16408 | go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r)); | |
16409 | *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32) | |
16410 | + static_cast<int64_t>(mpz_get_ui(r))); | |
16411 | mpz_clear(r); | |
16412 | mpz_clear(q); | |
16413 | return true; | |
16414 | } | |
16415 | ||
16416 | // Express floating point value as memory size if possible. | |
16417 | ||
16418 | bool | |
16419 | Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const | |
16420 | { | |
16421 | if (!mpfr_integer_p(fval)) | |
16422 | return false; | |
16423 | mpz_t ival; | |
16424 | mpz_init(ival); | |
16425 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16426 | bool ret = this->mpz_to_memory_size(ival, val); | |
16427 | mpz_clear(ival); | |
16428 | return ret; | |
16429 | } | |
16430 | ||
0c77715b | 16431 | // Convert value to integer if possible. |
16432 | ||
16433 | bool | |
16434 | Numeric_constant::to_int(mpz_t* val) const | |
16435 | { | |
16436 | switch (this->classification_) | |
16437 | { | |
16438 | case NC_INT: | |
16439 | case NC_RUNE: | |
16440 | mpz_init_set(*val, this->u_.int_val); | |
16441 | return true; | |
16442 | case NC_FLOAT: | |
16443 | if (!mpfr_integer_p(this->u_.float_val)) | |
16444 | return false; | |
16445 | mpz_init(*val); | |
16446 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
16447 | return true; | |
16448 | case NC_COMPLEX: | |
fcbea5e4 | 16449 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
16450 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 16451 | return false; |
16452 | mpz_init(*val); | |
fcbea5e4 | 16453 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16454 | return true; |
16455 | default: | |
16456 | go_unreachable(); | |
16457 | } | |
16458 | } | |
16459 | ||
16460 | // Convert value to floating point if possible. | |
16461 | ||
16462 | bool | |
16463 | Numeric_constant::to_float(mpfr_t* val) const | |
16464 | { | |
16465 | switch (this->classification_) | |
16466 | { | |
16467 | case NC_INT: | |
16468 | case NC_RUNE: | |
16469 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
16470 | return true; | |
16471 | case NC_FLOAT: | |
16472 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16473 | return true; | |
16474 | case NC_COMPLEX: | |
fcbea5e4 | 16475 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16476 | return false; |
fcbea5e4 | 16477 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16478 | return true; |
16479 | default: | |
16480 | go_unreachable(); | |
16481 | } | |
16482 | } | |
16483 | ||
16484 | // Convert value to complex. | |
16485 | ||
16486 | bool | |
fcbea5e4 | 16487 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 16488 | { |
fcbea5e4 | 16489 | mpc_init2(*val, mpc_precision); |
0c77715b | 16490 | switch (this->classification_) |
16491 | { | |
16492 | case NC_INT: | |
16493 | case NC_RUNE: | |
fcbea5e4 | 16494 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16495 | return true; |
16496 | case NC_FLOAT: | |
fcbea5e4 | 16497 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16498 | return true; |
16499 | case NC_COMPLEX: | |
fcbea5e4 | 16500 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16501 | return true; |
16502 | default: | |
16503 | go_unreachable(); | |
16504 | } | |
16505 | } | |
16506 | ||
16507 | // Get the type. | |
16508 | ||
16509 | Type* | |
16510 | Numeric_constant::type() const | |
16511 | { | |
16512 | if (this->type_ != NULL) | |
16513 | return this->type_; | |
16514 | switch (this->classification_) | |
16515 | { | |
16516 | case NC_INT: | |
16517 | return Type::make_abstract_integer_type(); | |
16518 | case NC_RUNE: | |
16519 | return Type::make_abstract_character_type(); | |
16520 | case NC_FLOAT: | |
16521 | return Type::make_abstract_float_type(); | |
16522 | case NC_COMPLEX: | |
16523 | return Type::make_abstract_complex_type(); | |
16524 | default: | |
16525 | go_unreachable(); | |
16526 | } | |
16527 | } | |
16528 | ||
16529 | // If the constant can be expressed in TYPE, then set the type of the | |
16530 | // constant to TYPE and return true. Otherwise return false, and, if | |
16531 | // ISSUE_ERROR is true, report an appropriate error message. | |
16532 | ||
16533 | bool | |
16534 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
16535 | { | |
16536 | bool ret; | |
f11c2155 | 16537 | if (type == NULL || type->is_error()) |
0c77715b | 16538 | ret = true; |
16539 | else if (type->integer_type() != NULL) | |
16540 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
16541 | else if (type->float_type() != NULL) | |
16542 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
16543 | else if (type->complex_type() != NULL) | |
16544 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
16545 | else | |
5706ab68 | 16546 | { |
16547 | ret = false; | |
16548 | if (issue_error) | |
16549 | go_assert(saw_errors()); | |
16550 | } | |
0c77715b | 16551 | if (ret) |
16552 | this->type_ = type; | |
16553 | return ret; | |
16554 | } | |
16555 | ||
16556 | // Check whether the constant can be expressed in an integer type. | |
16557 | ||
16558 | bool | |
16559 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 16560 | Location location) |
0c77715b | 16561 | { |
16562 | mpz_t val; | |
16563 | switch (this->classification_) | |
16564 | { | |
16565 | case NC_INT: | |
16566 | case NC_RUNE: | |
16567 | mpz_init_set(val, this->u_.int_val); | |
16568 | break; | |
16569 | ||
16570 | case NC_FLOAT: | |
16571 | if (!mpfr_integer_p(this->u_.float_val)) | |
16572 | { | |
16573 | if (issue_error) | |
71a45216 | 16574 | { |
631d5788 | 16575 | go_error_at(location, |
16576 | "floating point constant truncated to integer"); | |
71a45216 | 16577 | this->set_invalid(); |
16578 | } | |
0c77715b | 16579 | return false; |
16580 | } | |
16581 | mpz_init(val); | |
16582 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
16583 | break; | |
16584 | ||
16585 | case NC_COMPLEX: | |
fcbea5e4 | 16586 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
16587 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 16588 | { |
16589 | if (issue_error) | |
71a45216 | 16590 | { |
631d5788 | 16591 | go_error_at(location, "complex constant truncated to integer"); |
71a45216 | 16592 | this->set_invalid(); |
16593 | } | |
0c77715b | 16594 | return false; |
16595 | } | |
16596 | mpz_init(val); | |
fcbea5e4 | 16597 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16598 | break; |
16599 | ||
16600 | default: | |
16601 | go_unreachable(); | |
16602 | } | |
16603 | ||
16604 | bool ret; | |
16605 | if (type->is_abstract()) | |
16606 | ret = true; | |
16607 | else | |
16608 | { | |
16609 | int bits = mpz_sizeinbase(val, 2); | |
16610 | if (type->is_unsigned()) | |
16611 | { | |
16612 | // For an unsigned type we can only accept a nonnegative | |
16613 | // number, and we must be able to represents at least BITS. | |
16614 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
16615 | } | |
16616 | else | |
16617 | { | |
16618 | // For a signed type we need an extra bit to indicate the | |
16619 | // sign. We have to handle the most negative integer | |
16620 | // specially. | |
16621 | ret = (bits + 1 <= type->bits() | |
16622 | || (bits <= type->bits() | |
16623 | && mpz_sgn(val) < 0 | |
16624 | && (mpz_scan1(val, 0) | |
16625 | == static_cast<unsigned long>(type->bits() - 1)) | |
16626 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
16627 | } | |
16628 | } | |
16629 | ||
16630 | if (!ret && issue_error) | |
71a45216 | 16631 | { |
631d5788 | 16632 | go_error_at(location, "integer constant overflow"); |
71a45216 | 16633 | this->set_invalid(); |
16634 | } | |
0c77715b | 16635 | |
16636 | return ret; | |
16637 | } | |
16638 | ||
16639 | // Check whether the constant can be expressed in a floating point | |
16640 | // type. | |
16641 | ||
16642 | bool | |
16643 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 16644 | Location location) |
0c77715b | 16645 | { |
16646 | mpfr_t val; | |
16647 | switch (this->classification_) | |
16648 | { | |
16649 | case NC_INT: | |
16650 | case NC_RUNE: | |
16651 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
16652 | break; | |
16653 | ||
16654 | case NC_FLOAT: | |
16655 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
16656 | break; | |
16657 | ||
16658 | case NC_COMPLEX: | |
fcbea5e4 | 16659 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16660 | { |
16661 | if (issue_error) | |
71a45216 | 16662 | { |
16663 | this->set_invalid(); | |
631d5788 | 16664 | go_error_at(location, "complex constant truncated to float"); |
71a45216 | 16665 | } |
0c77715b | 16666 | return false; |
16667 | } | |
fcbea5e4 | 16668 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16669 | break; |
16670 | ||
16671 | default: | |
16672 | go_unreachable(); | |
16673 | } | |
16674 | ||
16675 | bool ret; | |
16676 | if (type->is_abstract()) | |
16677 | ret = true; | |
16678 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
16679 | { | |
16680 | // A NaN or Infinity always fits in the range of the type. | |
16681 | ret = true; | |
16682 | } | |
16683 | else | |
16684 | { | |
16685 | mp_exp_t exp = mpfr_get_exp(val); | |
16686 | mp_exp_t max_exp; | |
16687 | switch (type->bits()) | |
16688 | { | |
16689 | case 32: | |
16690 | max_exp = 128; | |
16691 | break; | |
16692 | case 64: | |
16693 | max_exp = 1024; | |
16694 | break; | |
16695 | default: | |
16696 | go_unreachable(); | |
16697 | } | |
16698 | ||
16699 | ret = exp <= max_exp; | |
d0bcce51 | 16700 | |
16701 | if (ret) | |
16702 | { | |
16703 | // Round the constant to the desired type. | |
16704 | mpfr_t t; | |
16705 | mpfr_init(t); | |
16706 | switch (type->bits()) | |
16707 | { | |
16708 | case 32: | |
16709 | mpfr_set_prec(t, 24); | |
16710 | break; | |
16711 | case 64: | |
16712 | mpfr_set_prec(t, 53); | |
16713 | break; | |
16714 | default: | |
16715 | go_unreachable(); | |
16716 | } | |
16717 | mpfr_set(t, val, GMP_RNDN); | |
16718 | mpfr_set(val, t, GMP_RNDN); | |
16719 | mpfr_clear(t); | |
16720 | ||
16721 | this->set_float(type, val); | |
16722 | } | |
0c77715b | 16723 | } |
16724 | ||
16725 | mpfr_clear(val); | |
16726 | ||
16727 | if (!ret && issue_error) | |
71a45216 | 16728 | { |
631d5788 | 16729 | go_error_at(location, "floating point constant overflow"); |
71a45216 | 16730 | this->set_invalid(); |
16731 | } | |
0c77715b | 16732 | |
16733 | return ret; | |
16734 | } | |
16735 | ||
16736 | // Check whether the constant can be expressed in a complex type. | |
16737 | ||
16738 | bool | |
16739 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 16740 | Location location) |
0c77715b | 16741 | { |
16742 | if (type->is_abstract()) | |
16743 | return true; | |
16744 | ||
16745 | mp_exp_t max_exp; | |
16746 | switch (type->bits()) | |
16747 | { | |
16748 | case 64: | |
16749 | max_exp = 128; | |
16750 | break; | |
16751 | case 128: | |
16752 | max_exp = 1024; | |
16753 | break; | |
16754 | default: | |
16755 | go_unreachable(); | |
16756 | } | |
16757 | ||
fcbea5e4 | 16758 | mpc_t val; |
16759 | mpc_init2(val, mpc_precision); | |
0c77715b | 16760 | switch (this->classification_) |
16761 | { | |
16762 | case NC_INT: | |
16763 | case NC_RUNE: | |
fcbea5e4 | 16764 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16765 | break; |
16766 | ||
16767 | case NC_FLOAT: | |
fcbea5e4 | 16768 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16769 | break; |
16770 | ||
16771 | case NC_COMPLEX: | |
fcbea5e4 | 16772 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16773 | break; |
16774 | ||
16775 | default: | |
16776 | go_unreachable(); | |
16777 | } | |
16778 | ||
d0bcce51 | 16779 | bool ret = true; |
fcbea5e4 | 16780 | if (!mpfr_nan_p(mpc_realref(val)) |
16781 | && !mpfr_inf_p(mpc_realref(val)) | |
16782 | && !mpfr_zero_p(mpc_realref(val)) | |
16783 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 16784 | { |
16785 | if (issue_error) | |
71a45216 | 16786 | { |
631d5788 | 16787 | go_error_at(location, "complex real part overflow"); |
71a45216 | 16788 | this->set_invalid(); |
16789 | } | |
d0bcce51 | 16790 | ret = false; |
16791 | } | |
0c77715b | 16792 | |
fcbea5e4 | 16793 | if (!mpfr_nan_p(mpc_imagref(val)) |
16794 | && !mpfr_inf_p(mpc_imagref(val)) | |
16795 | && !mpfr_zero_p(mpc_imagref(val)) | |
16796 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 16797 | { |
16798 | if (issue_error) | |
71a45216 | 16799 | { |
631d5788 | 16800 | go_error_at(location, "complex imaginary part overflow"); |
71a45216 | 16801 | this->set_invalid(); |
16802 | } | |
d0bcce51 | 16803 | ret = false; |
16804 | } | |
0c77715b | 16805 | |
d0bcce51 | 16806 | if (ret) |
16807 | { | |
16808 | // Round the constant to the desired type. | |
fcbea5e4 | 16809 | mpc_t t; |
d0bcce51 | 16810 | switch (type->bits()) |
16811 | { | |
16812 | case 64: | |
fcbea5e4 | 16813 | mpc_init2(t, 24); |
d0bcce51 | 16814 | break; |
16815 | case 128: | |
fcbea5e4 | 16816 | mpc_init2(t, 53); |
d0bcce51 | 16817 | break; |
16818 | default: | |
16819 | go_unreachable(); | |
16820 | } | |
fcbea5e4 | 16821 | mpc_set(t, val, MPC_RNDNN); |
16822 | mpc_set(val, t, MPC_RNDNN); | |
16823 | mpc_clear(t); | |
d0bcce51 | 16824 | |
fcbea5e4 | 16825 | this->set_complex(type, val); |
d0bcce51 | 16826 | } |
16827 | ||
fcbea5e4 | 16828 | mpc_clear(val); |
0c77715b | 16829 | |
16830 | return ret; | |
16831 | } | |
16832 | ||
16833 | // Return an Expression for this value. | |
16834 | ||
16835 | Expression* | |
16836 | Numeric_constant::expression(Location loc) const | |
16837 | { | |
16838 | switch (this->classification_) | |
16839 | { | |
16840 | case NC_INT: | |
e67508fa | 16841 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 16842 | case NC_RUNE: |
16843 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
16844 | case NC_FLOAT: | |
16845 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
16846 | case NC_COMPLEX: | |
fcbea5e4 | 16847 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 16848 | case NC_INVALID: |
16849 | go_assert(saw_errors()); | |
16850 | return Expression::make_error(loc); | |
0c77715b | 16851 | default: |
16852 | go_unreachable(); | |
16853 | } | |
16854 | } |