]>
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 | { | |
786 | ast_dump_context->ostream() << this->variable_->name() ; | |
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 | { | |
862 | adc->ostream() << this->variable_->name(); | |
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 | |
1313 | Btype* btype = this->type()->get_backend(gogo); | |
1314 | ||
1315 | Bvariable* bvar; | |
438b4bec | 1316 | std::string asm_name(go_selectively_encode_id(var_name)); |
09e57698 | 1317 | if (no->package() != NULL || is_descriptor) |
438b4bec | 1318 | bvar = context->backend()->immutable_struct_reference(var_name, asm_name, |
1319 | btype, loc); | |
8381eda7 | 1320 | else |
1321 | { | |
1322 | Location bloc = Linemap::predeclared_location(); | |
1323 | bool is_hidden = ((no->is_function() | |
1324 | && no->func_value()->enclosing() != NULL) | |
1325 | || Gogo::is_thunk(no)); | |
438b4bec | 1326 | bvar = context->backend()->immutable_struct(var_name, asm_name, |
1327 | is_hidden, false, | |
8381eda7 | 1328 | btype, bloc); |
1329 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1330 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1331 | Expression* init = |
1332 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1333 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1334 | bcontext.set_is_const(); | |
ea664253 | 1335 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1336 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1337 | false, btype, bloc, binit); | |
1338 | } | |
1339 | ||
1340 | this->dvar_ = bvar; | |
7af8e400 | 1341 | return gogo->backend()->var_expression(bvar, loc); |
8381eda7 | 1342 | } |
1343 | ||
c6837989 | 1344 | // Print a function descriptor expression. |
1345 | ||
1346 | void | |
1347 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1348 | { | |
1349 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1350 | } | |
1351 | ||
8381eda7 | 1352 | // Make a function descriptor expression. |
1353 | ||
c6837989 | 1354 | Func_descriptor_expression* |
1355 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1356 | { |
c6837989 | 1357 | return new Func_descriptor_expression(fn); |
8381eda7 | 1358 | } |
1359 | ||
1360 | // Make the function descriptor type, so that it can be converted. | |
1361 | ||
1362 | void | |
1363 | Expression::make_func_descriptor_type() | |
1364 | { | |
1365 | Func_descriptor_expression::make_func_descriptor_type(); | |
1366 | } | |
1367 | ||
1368 | // A reference to just the code of a function. | |
1369 | ||
1370 | class Func_code_reference_expression : public Expression | |
1371 | { | |
1372 | public: | |
1373 | Func_code_reference_expression(Named_object* function, Location location) | |
1374 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1375 | function_(function) | |
1376 | { } | |
1377 | ||
1378 | protected: | |
1379 | int | |
1380 | do_traverse(Traverse*) | |
1381 | { return TRAVERSE_CONTINUE; } | |
1382 | ||
f9ca30f9 | 1383 | bool |
3ae06f68 | 1384 | do_is_static_initializer() const |
f9ca30f9 | 1385 | { return true; } |
1386 | ||
8381eda7 | 1387 | Type* |
1388 | do_type() | |
1389 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1390 | ||
1391 | void | |
1392 | do_determine_type(const Type_context*) | |
1393 | { } | |
1394 | ||
1395 | Expression* | |
1396 | do_copy() | |
1397 | { | |
1398 | return Expression::make_func_code_reference(this->function_, | |
1399 | this->location()); | |
1400 | } | |
1401 | ||
ea664253 | 1402 | Bexpression* |
1403 | do_get_backend(Translate_context*); | |
8381eda7 | 1404 | |
1405 | void | |
1406 | do_dump_expression(Ast_dump_context* context) const | |
1407 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1408 | ||
1409 | private: | |
1410 | // The function. | |
1411 | Named_object* function_; | |
1412 | }; | |
1413 | ||
ea664253 | 1414 | // Get the backend representation for a reference to function code. |
8381eda7 | 1415 | |
ea664253 | 1416 | Bexpression* |
1417 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1418 | { |
ea664253 | 1419 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1420 | this->location()); | |
8381eda7 | 1421 | } |
1422 | ||
1423 | // Make a reference to the code of a function. | |
1424 | ||
1425 | Expression* | |
1426 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1427 | { | |
1428 | return new Func_code_reference_expression(function, location); | |
1429 | } | |
1430 | ||
e440a328 | 1431 | // Class Unknown_expression. |
1432 | ||
1433 | // Return the name of an unknown expression. | |
1434 | ||
1435 | const std::string& | |
1436 | Unknown_expression::name() const | |
1437 | { | |
1438 | return this->named_object_->name(); | |
1439 | } | |
1440 | ||
1441 | // Lower a reference to an unknown name. | |
1442 | ||
1443 | Expression* | |
ceeb4318 | 1444 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1445 | { |
b13c66cd | 1446 | Location location = this->location(); |
e440a328 | 1447 | Named_object* no = this->named_object_; |
deded542 | 1448 | Named_object* real; |
1449 | if (!no->is_unknown()) | |
1450 | real = no; | |
1451 | else | |
e440a328 | 1452 | { |
deded542 | 1453 | real = no->unknown_value()->real_named_object(); |
1454 | if (real == NULL) | |
1455 | { | |
1456 | if (this->is_composite_literal_key_) | |
1457 | return this; | |
acf8e158 | 1458 | if (!this->no_error_message_) |
631d5788 | 1459 | go_error_at(location, "reference to undefined name %qs", |
1460 | this->named_object_->message_name().c_str()); | |
deded542 | 1461 | return Expression::make_error(location); |
1462 | } | |
e440a328 | 1463 | } |
1464 | switch (real->classification()) | |
1465 | { | |
1466 | case Named_object::NAMED_OBJECT_CONST: | |
1467 | return Expression::make_const_reference(real, location); | |
1468 | case Named_object::NAMED_OBJECT_TYPE: | |
1469 | return Expression::make_type(real->type_value(), location); | |
1470 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1471 | if (this->is_composite_literal_key_) | |
1472 | return this; | |
acf8e158 | 1473 | if (!this->no_error_message_) |
631d5788 | 1474 | go_error_at(location, "reference to undefined type %qs", |
1475 | real->message_name().c_str()); | |
e440a328 | 1476 | return Expression::make_error(location); |
1477 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1478 | real->var_value()->set_is_used(); |
e440a328 | 1479 | return Expression::make_var_reference(real, location); |
1480 | case Named_object::NAMED_OBJECT_FUNC: | |
1481 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1482 | return Expression::make_func_reference(real, NULL, location); | |
1483 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1484 | if (this->is_composite_literal_key_) | |
1485 | return this; | |
acf8e158 | 1486 | if (!this->no_error_message_) |
631d5788 | 1487 | go_error_at(location, "unexpected reference to package"); |
e440a328 | 1488 | return Expression::make_error(location); |
1489 | default: | |
c3e6f413 | 1490 | go_unreachable(); |
e440a328 | 1491 | } |
1492 | } | |
1493 | ||
d751bb78 | 1494 | // Dump the ast representation for an unknown expression to a dump context. |
1495 | ||
1496 | void | |
1497 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1498 | { | |
1499 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1500 | << ")"; | |
d751bb78 | 1501 | } |
1502 | ||
e440a328 | 1503 | // Make a reference to an unknown name. |
1504 | ||
acf8e158 | 1505 | Unknown_expression* |
b13c66cd | 1506 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1507 | { |
e440a328 | 1508 | return new Unknown_expression(no, location); |
1509 | } | |
1510 | ||
1511 | // A boolean expression. | |
1512 | ||
1513 | class Boolean_expression : public Expression | |
1514 | { | |
1515 | public: | |
b13c66cd | 1516 | Boolean_expression(bool val, Location location) |
e440a328 | 1517 | : Expression(EXPRESSION_BOOLEAN, location), |
1518 | val_(val), type_(NULL) | |
1519 | { } | |
1520 | ||
1521 | static Expression* | |
1522 | do_import(Import*); | |
1523 | ||
1524 | protected: | |
1525 | bool | |
1526 | do_is_constant() const | |
1527 | { return true; } | |
1528 | ||
0e168074 | 1529 | bool |
3ae06f68 | 1530 | do_is_static_initializer() const |
0e168074 | 1531 | { return true; } |
1532 | ||
e440a328 | 1533 | Type* |
1534 | do_type(); | |
1535 | ||
1536 | void | |
1537 | do_determine_type(const Type_context*); | |
1538 | ||
1539 | Expression* | |
1540 | do_copy() | |
1541 | { return this; } | |
1542 | ||
ea664253 | 1543 | Bexpression* |
1544 | do_get_backend(Translate_context* context) | |
1545 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1546 | |
1547 | void | |
1548 | do_export(Export* exp) const | |
1549 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1550 | ||
d751bb78 | 1551 | void |
1552 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1553 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1554 | ||
e440a328 | 1555 | private: |
1556 | // The constant. | |
1557 | bool val_; | |
1558 | // The type as determined by context. | |
1559 | Type* type_; | |
1560 | }; | |
1561 | ||
1562 | // Get the type. | |
1563 | ||
1564 | Type* | |
1565 | Boolean_expression::do_type() | |
1566 | { | |
1567 | if (this->type_ == NULL) | |
1568 | this->type_ = Type::make_boolean_type(); | |
1569 | return this->type_; | |
1570 | } | |
1571 | ||
1572 | // Set the type from the context. | |
1573 | ||
1574 | void | |
1575 | Boolean_expression::do_determine_type(const Type_context* context) | |
1576 | { | |
1577 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1578 | ; | |
1579 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1580 | this->type_ = context->type; | |
1581 | else if (!context->may_be_abstract) | |
1582 | this->type_ = Type::lookup_bool_type(); | |
1583 | } | |
1584 | ||
1585 | // Import a boolean constant. | |
1586 | ||
1587 | Expression* | |
1588 | Boolean_expression::do_import(Import* imp) | |
1589 | { | |
1590 | if (imp->peek_char() == 't') | |
1591 | { | |
1592 | imp->require_c_string("true"); | |
1593 | return Expression::make_boolean(true, imp->location()); | |
1594 | } | |
1595 | else | |
1596 | { | |
1597 | imp->require_c_string("false"); | |
1598 | return Expression::make_boolean(false, imp->location()); | |
1599 | } | |
1600 | } | |
1601 | ||
1602 | // Make a boolean expression. | |
1603 | ||
1604 | Expression* | |
b13c66cd | 1605 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1606 | { |
1607 | return new Boolean_expression(val, location); | |
1608 | } | |
1609 | ||
1610 | // Class String_expression. | |
1611 | ||
1612 | // Get the type. | |
1613 | ||
1614 | Type* | |
1615 | String_expression::do_type() | |
1616 | { | |
1617 | if (this->type_ == NULL) | |
1618 | this->type_ = Type::make_string_type(); | |
1619 | return this->type_; | |
1620 | } | |
1621 | ||
1622 | // Set the type from the context. | |
1623 | ||
1624 | void | |
1625 | String_expression::do_determine_type(const Type_context* context) | |
1626 | { | |
1627 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1628 | ; | |
1629 | else if (context->type != NULL && context->type->is_string_type()) | |
1630 | this->type_ = context->type; | |
1631 | else if (!context->may_be_abstract) | |
1632 | this->type_ = Type::lookup_string_type(); | |
1633 | } | |
1634 | ||
1635 | // Build a string constant. | |
1636 | ||
ea664253 | 1637 | Bexpression* |
1638 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1639 | { |
2c809f8f | 1640 | Gogo* gogo = context->gogo(); |
1641 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1642 | ||
1643 | Location loc = this->location(); | |
1644 | std::vector<Bexpression*> init(2); | |
1645 | Bexpression* str_cst = | |
1646 | gogo->backend()->string_constant_expression(this->val_); | |
1647 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1648 | ||
1649 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1650 | mpz_t lenval; | |
1651 | mpz_init_set_ui(lenval, this->val_.length()); | |
1652 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1653 | mpz_clear(lenval); | |
1654 | ||
ea664253 | 1655 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1656 | } |
1657 | ||
8b1c301d | 1658 | // Write string literal to string dump. |
e440a328 | 1659 | |
1660 | void | |
8b1c301d | 1661 | String_expression::export_string(String_dump* exp, |
1662 | const String_expression* str) | |
e440a328 | 1663 | { |
1664 | std::string s; | |
8b1c301d | 1665 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1666 | s += '"'; |
8b1c301d | 1667 | for (std::string::const_iterator p = str->val_.begin(); |
1668 | p != str->val_.end(); | |
e440a328 | 1669 | ++p) |
1670 | { | |
1671 | if (*p == '\\' || *p == '"') | |
1672 | { | |
1673 | s += '\\'; | |
1674 | s += *p; | |
1675 | } | |
1676 | else if (*p >= 0x20 && *p < 0x7f) | |
1677 | s += *p; | |
1678 | else if (*p == '\n') | |
1679 | s += "\\n"; | |
1680 | else if (*p == '\t') | |
1681 | s += "\\t"; | |
1682 | else | |
1683 | { | |
1684 | s += "\\x"; | |
1685 | unsigned char c = *p; | |
1686 | unsigned int dig = c >> 4; | |
1687 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1688 | dig = c & 0xf; | |
1689 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1690 | } | |
1691 | } | |
1692 | s += '"'; | |
1693 | exp->write_string(s); | |
1694 | } | |
1695 | ||
8b1c301d | 1696 | // Export a string expression. |
1697 | ||
1698 | void | |
1699 | String_expression::do_export(Export* exp) const | |
1700 | { | |
1701 | String_expression::export_string(exp, this); | |
1702 | } | |
1703 | ||
e440a328 | 1704 | // Import a string expression. |
1705 | ||
1706 | Expression* | |
1707 | String_expression::do_import(Import* imp) | |
1708 | { | |
1709 | imp->require_c_string("\""); | |
1710 | std::string val; | |
1711 | while (true) | |
1712 | { | |
1713 | int c = imp->get_char(); | |
1714 | if (c == '"' || c == -1) | |
1715 | break; | |
1716 | if (c != '\\') | |
1717 | val += static_cast<char>(c); | |
1718 | else | |
1719 | { | |
1720 | c = imp->get_char(); | |
1721 | if (c == '\\' || c == '"') | |
1722 | val += static_cast<char>(c); | |
1723 | else if (c == 'n') | |
1724 | val += '\n'; | |
1725 | else if (c == 't') | |
1726 | val += '\t'; | |
1727 | else if (c == 'x') | |
1728 | { | |
1729 | c = imp->get_char(); | |
1730 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1731 | c = imp->get_char(); | |
1732 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1733 | char v = (vh << 4) | vl; | |
1734 | val += v; | |
1735 | } | |
1736 | else | |
1737 | { | |
631d5788 | 1738 | go_error_at(imp->location(), "bad string constant"); |
e440a328 | 1739 | return Expression::make_error(imp->location()); |
1740 | } | |
1741 | } | |
1742 | } | |
1743 | return Expression::make_string(val, imp->location()); | |
1744 | } | |
1745 | ||
d751bb78 | 1746 | // Ast dump for string expression. |
1747 | ||
1748 | void | |
1749 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1750 | { | |
8b1c301d | 1751 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1752 | } |
1753 | ||
e440a328 | 1754 | // Make a string expression. |
1755 | ||
1756 | Expression* | |
b13c66cd | 1757 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1758 | { |
1759 | return new String_expression(val, location); | |
1760 | } | |
1761 | ||
2c809f8f | 1762 | // An expression that evaluates to some characteristic of a string. |
1763 | // This is used when indexing, bound-checking, or nil checking a string. | |
1764 | ||
1765 | class String_info_expression : public Expression | |
1766 | { | |
1767 | public: | |
1768 | String_info_expression(Expression* string, String_info string_info, | |
1769 | Location location) | |
1770 | : Expression(EXPRESSION_STRING_INFO, location), | |
1771 | string_(string), string_info_(string_info) | |
1772 | { } | |
1773 | ||
1774 | protected: | |
1775 | Type* | |
1776 | do_type(); | |
1777 | ||
1778 | void | |
1779 | do_determine_type(const Type_context*) | |
1780 | { go_unreachable(); } | |
1781 | ||
1782 | Expression* | |
1783 | do_copy() | |
1784 | { | |
1785 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1786 | this->location()); | |
1787 | } | |
1788 | ||
ea664253 | 1789 | Bexpression* |
1790 | do_get_backend(Translate_context* context); | |
2c809f8f | 1791 | |
1792 | void | |
1793 | do_dump_expression(Ast_dump_context*) const; | |
1794 | ||
1795 | void | |
1796 | do_issue_nil_check() | |
1797 | { this->string_->issue_nil_check(); } | |
1798 | ||
1799 | private: | |
1800 | // The string for which we are getting information. | |
1801 | Expression* string_; | |
1802 | // What information we want. | |
1803 | String_info string_info_; | |
1804 | }; | |
1805 | ||
1806 | // Return the type of the string info. | |
1807 | ||
1808 | Type* | |
1809 | String_info_expression::do_type() | |
1810 | { | |
1811 | switch (this->string_info_) | |
1812 | { | |
1813 | case STRING_INFO_DATA: | |
1814 | { | |
1815 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1816 | return Type::make_pointer_type(byte_type); | |
1817 | } | |
1818 | case STRING_INFO_LENGTH: | |
1819 | return Type::lookup_integer_type("int"); | |
1820 | default: | |
1821 | go_unreachable(); | |
1822 | } | |
1823 | } | |
1824 | ||
1825 | // Return string information in GENERIC. | |
1826 | ||
ea664253 | 1827 | Bexpression* |
1828 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1829 | { |
1830 | Gogo* gogo = context->gogo(); | |
1831 | ||
ea664253 | 1832 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1833 | switch (this->string_info_) |
1834 | { | |
1835 | case STRING_INFO_DATA: | |
1836 | case STRING_INFO_LENGTH: | |
ea664253 | 1837 | return gogo->backend()->struct_field_expression(bstring, |
1838 | this->string_info_, | |
1839 | this->location()); | |
2c809f8f | 1840 | break; |
1841 | default: | |
1842 | go_unreachable(); | |
1843 | } | |
2c809f8f | 1844 | } |
1845 | ||
1846 | // Dump ast representation for a type info expression. | |
1847 | ||
1848 | void | |
1849 | String_info_expression::do_dump_expression( | |
1850 | Ast_dump_context* ast_dump_context) const | |
1851 | { | |
1852 | ast_dump_context->ostream() << "stringinfo("; | |
1853 | this->string_->dump_expression(ast_dump_context); | |
1854 | ast_dump_context->ostream() << ","; | |
1855 | ast_dump_context->ostream() << | |
1856 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1857 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1858 | : "unknown"); | |
1859 | ast_dump_context->ostream() << ")"; | |
1860 | } | |
1861 | ||
1862 | // Make a string info expression. | |
1863 | ||
1864 | Expression* | |
1865 | Expression::make_string_info(Expression* string, String_info string_info, | |
1866 | Location location) | |
1867 | { | |
1868 | return new String_info_expression(string, string_info, location); | |
1869 | } | |
1870 | ||
e440a328 | 1871 | // Make an integer expression. |
1872 | ||
1873 | class Integer_expression : public Expression | |
1874 | { | |
1875 | public: | |
5d4b8566 | 1876 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1877 | Location location) | |
e440a328 | 1878 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1879 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1880 | { mpz_init_set(this->val_, *val); } |
1881 | ||
1882 | static Expression* | |
1883 | do_import(Import*); | |
1884 | ||
8b1c301d | 1885 | // Write VAL to string dump. |
e440a328 | 1886 | static void |
8b1c301d | 1887 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1888 | |
d751bb78 | 1889 | // Write VAL to dump context. |
1890 | static void | |
1891 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1892 | ||
e440a328 | 1893 | protected: |
1894 | bool | |
1895 | do_is_constant() const | |
1896 | { return true; } | |
1897 | ||
0e168074 | 1898 | bool |
3ae06f68 | 1899 | do_is_static_initializer() const |
0e168074 | 1900 | { return true; } |
1901 | ||
e440a328 | 1902 | bool |
0c77715b | 1903 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1904 | |
1905 | Type* | |
1906 | do_type(); | |
1907 | ||
1908 | void | |
1909 | do_determine_type(const Type_context* context); | |
1910 | ||
1911 | void | |
1912 | do_check_types(Gogo*); | |
1913 | ||
ea664253 | 1914 | Bexpression* |
1915 | do_get_backend(Translate_context*); | |
e440a328 | 1916 | |
1917 | Expression* | |
1918 | do_copy() | |
5d4b8566 | 1919 | { |
1920 | if (this->is_character_constant_) | |
1921 | return Expression::make_character(&this->val_, this->type_, | |
1922 | this->location()); | |
1923 | else | |
e67508fa | 1924 | return Expression::make_integer_z(&this->val_, this->type_, |
1925 | this->location()); | |
5d4b8566 | 1926 | } |
e440a328 | 1927 | |
1928 | void | |
1929 | do_export(Export*) const; | |
1930 | ||
d751bb78 | 1931 | void |
1932 | do_dump_expression(Ast_dump_context*) const; | |
1933 | ||
e440a328 | 1934 | private: |
1935 | // The integer value. | |
1936 | mpz_t val_; | |
1937 | // The type so far. | |
1938 | Type* type_; | |
5d4b8566 | 1939 | // Whether this is a character constant. |
1940 | bool is_character_constant_; | |
e440a328 | 1941 | }; |
1942 | ||
0c77715b | 1943 | // Return a numeric constant for this expression. We have to mark |
1944 | // this as a character when appropriate. | |
e440a328 | 1945 | |
1946 | bool | |
0c77715b | 1947 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1948 | { |
0c77715b | 1949 | if (this->is_character_constant_) |
1950 | nc->set_rune(this->type_, this->val_); | |
1951 | else | |
1952 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1953 | return true; |
1954 | } | |
1955 | ||
1956 | // Return the current type. If we haven't set the type yet, we return | |
1957 | // an abstract integer type. | |
1958 | ||
1959 | Type* | |
1960 | Integer_expression::do_type() | |
1961 | { | |
1962 | if (this->type_ == NULL) | |
5d4b8566 | 1963 | { |
1964 | if (this->is_character_constant_) | |
1965 | this->type_ = Type::make_abstract_character_type(); | |
1966 | else | |
1967 | this->type_ = Type::make_abstract_integer_type(); | |
1968 | } | |
e440a328 | 1969 | return this->type_; |
1970 | } | |
1971 | ||
1972 | // Set the type of the integer value. Here we may switch from an | |
1973 | // abstract type to a real type. | |
1974 | ||
1975 | void | |
1976 | Integer_expression::do_determine_type(const Type_context* context) | |
1977 | { | |
1978 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1979 | ; | |
0c77715b | 1980 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1981 | this->type_ = context->type; |
1982 | else if (!context->may_be_abstract) | |
5d4b8566 | 1983 | { |
1984 | if (this->is_character_constant_) | |
1985 | this->type_ = Type::lookup_integer_type("int32"); | |
1986 | else | |
1987 | this->type_ = Type::lookup_integer_type("int"); | |
1988 | } | |
e440a328 | 1989 | } |
1990 | ||
e440a328 | 1991 | // Check the type of an integer constant. |
1992 | ||
1993 | void | |
1994 | Integer_expression::do_check_types(Gogo*) | |
1995 | { | |
0c77715b | 1996 | Type* type = this->type_; |
1997 | if (type == NULL) | |
e440a328 | 1998 | return; |
0c77715b | 1999 | Numeric_constant nc; |
2000 | if (this->is_character_constant_) | |
2001 | nc.set_rune(NULL, this->val_); | |
2002 | else | |
2003 | nc.set_int(NULL, this->val_); | |
2004 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 2005 | this->set_is_error(); |
2006 | } | |
2007 | ||
ea664253 | 2008 | // Get the backend representation for an integer constant. |
e440a328 | 2009 | |
ea664253 | 2010 | Bexpression* |
2011 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2012 | { |
12373dd5 | 2013 | if (this->is_error_expression() |
2014 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2015 | { | |
2016 | go_assert(saw_errors()); | |
2017 | return context->gogo()->backend()->error_expression(); | |
2018 | } | |
2019 | ||
48c2a53a | 2020 | Type* resolved_type = NULL; |
e440a328 | 2021 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2022 | resolved_type = this->type_; |
e440a328 | 2023 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
2024 | { | |
2025 | // We are converting to an abstract floating point type. | |
48c2a53a | 2026 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2027 | } |
2028 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2029 | { | |
2030 | // We are converting to an abstract complex type. | |
48c2a53a | 2031 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2032 | } |
2033 | else | |
2034 | { | |
2035 | // If we still have an abstract type here, then this is being | |
2036 | // used in a constant expression which didn't get reduced for | |
2037 | // some reason. Use a type which will fit the value. We use <, | |
2038 | // not <=, because we need an extra bit for the sign bit. | |
2039 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 2040 | Type* int_type = Type::lookup_integer_type("int"); |
2041 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 2042 | resolved_type = int_type; |
e440a328 | 2043 | else if (bits < 64) |
48c2a53a | 2044 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 2045 | else |
48c2a53a | 2046 | { |
2047 | if (!saw_errors()) | |
631d5788 | 2048 | go_error_at(this->location(), |
2049 | "unknown type for large integer constant"); | |
ea664253 | 2050 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 2051 | } |
e440a328 | 2052 | } |
48c2a53a | 2053 | Numeric_constant nc; |
2054 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 2055 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2056 | } |
2057 | ||
2058 | // Write VAL to export data. | |
2059 | ||
2060 | void | |
8b1c301d | 2061 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 2062 | { |
2063 | char* s = mpz_get_str(NULL, 10, val); | |
2064 | exp->write_c_string(s); | |
2065 | free(s); | |
2066 | } | |
2067 | ||
2068 | // Export an integer in a constant expression. | |
2069 | ||
2070 | void | |
2071 | Integer_expression::do_export(Export* exp) const | |
2072 | { | |
2073 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 2074 | if (this->is_character_constant_) |
2075 | exp->write_c_string("'"); | |
e440a328 | 2076 | // A trailing space lets us reliably identify the end of the number. |
2077 | exp->write_c_string(" "); | |
2078 | } | |
2079 | ||
2080 | // Import an integer, floating point, or complex value. This handles | |
2081 | // all these types because they all start with digits. | |
2082 | ||
2083 | Expression* | |
2084 | Integer_expression::do_import(Import* imp) | |
2085 | { | |
2086 | std::string num = imp->read_identifier(); | |
2087 | imp->require_c_string(" "); | |
2088 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2089 | { | |
2090 | mpfr_t real; | |
2091 | size_t plus_pos = num.find('+', 1); | |
2092 | size_t minus_pos = num.find('-', 1); | |
2093 | size_t pos; | |
2094 | if (plus_pos == std::string::npos) | |
2095 | pos = minus_pos; | |
2096 | else if (minus_pos == std::string::npos) | |
2097 | pos = plus_pos; | |
2098 | else | |
2099 | { | |
631d5788 | 2100 | go_error_at(imp->location(), "bad number in import data: %qs", |
2101 | num.c_str()); | |
e440a328 | 2102 | return Expression::make_error(imp->location()); |
2103 | } | |
2104 | if (pos == std::string::npos) | |
2105 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2106 | else | |
2107 | { | |
2108 | std::string real_str = num.substr(0, pos); | |
2109 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2110 | { | |
631d5788 | 2111 | go_error_at(imp->location(), "bad number in import data: %qs", |
2112 | real_str.c_str()); | |
e440a328 | 2113 | return Expression::make_error(imp->location()); |
2114 | } | |
2115 | } | |
2116 | ||
2117 | std::string imag_str; | |
2118 | if (pos == std::string::npos) | |
2119 | imag_str = num; | |
2120 | else | |
2121 | imag_str = num.substr(pos); | |
2122 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2123 | mpfr_t imag; | |
2124 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2125 | { | |
631d5788 | 2126 | go_error_at(imp->location(), "bad number in import data: %qs", |
2127 | imag_str.c_str()); | |
e440a328 | 2128 | return Expression::make_error(imp->location()); |
2129 | } | |
fcbea5e4 | 2130 | mpc_t cval; |
2131 | mpc_init2(cval, mpc_precision); | |
2132 | mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); | |
e440a328 | 2133 | mpfr_clear(real); |
2134 | mpfr_clear(imag); | |
fcbea5e4 | 2135 | Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); |
2136 | mpc_clear(cval); | |
e440a328 | 2137 | return ret; |
2138 | } | |
2139 | else if (num.find('.') == std::string::npos | |
2140 | && num.find('E') == std::string::npos) | |
2141 | { | |
5d4b8566 | 2142 | bool is_character_constant = (!num.empty() |
2143 | && num[num.length() - 1] == '\''); | |
2144 | if (is_character_constant) | |
2145 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2146 | mpz_t val; |
2147 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2148 | { | |
631d5788 | 2149 | go_error_at(imp->location(), "bad number in import data: %qs", |
2150 | num.c_str()); | |
e440a328 | 2151 | return Expression::make_error(imp->location()); |
2152 | } | |
5d4b8566 | 2153 | Expression* ret; |
2154 | if (is_character_constant) | |
2155 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2156 | else | |
e67508fa | 2157 | ret = Expression::make_integer_z(&val, NULL, imp->location()); |
e440a328 | 2158 | mpz_clear(val); |
2159 | return ret; | |
2160 | } | |
2161 | else | |
2162 | { | |
2163 | mpfr_t val; | |
2164 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2165 | { | |
631d5788 | 2166 | go_error_at(imp->location(), "bad number in import data: %qs", |
2167 | num.c_str()); | |
e440a328 | 2168 | return Expression::make_error(imp->location()); |
2169 | } | |
2170 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2171 | mpfr_clear(val); | |
2172 | return ret; | |
2173 | } | |
2174 | } | |
d751bb78 | 2175 | // Ast dump for integer expression. |
2176 | ||
2177 | void | |
2178 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2179 | { | |
5d4b8566 | 2180 | if (this->is_character_constant_) |
2181 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2182 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2183 | if (this->is_character_constant_) |
2184 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2185 | } |
2186 | ||
e67508fa | 2187 | // Build a new integer value from a multi-precision integer. |
e440a328 | 2188 | |
2189 | Expression* | |
e67508fa | 2190 | Expression::make_integer_z(const mpz_t* val, Type* type, Location location) |
5d4b8566 | 2191 | { |
2192 | return new Integer_expression(val, type, false, location); | |
2193 | } | |
2194 | ||
e67508fa | 2195 | // Build a new integer value from an unsigned long. |
2196 | ||
2197 | Expression* | |
2198 | Expression::make_integer_ul(unsigned long val, Type *type, Location location) | |
2199 | { | |
2200 | mpz_t zval; | |
2201 | mpz_init_set_ui(zval, val); | |
2202 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2203 | mpz_clear(zval); | |
2204 | return ret; | |
2205 | } | |
2206 | ||
2207 | // Build a new integer value from a signed long. | |
2208 | ||
2209 | Expression* | |
2210 | Expression::make_integer_sl(long val, Type *type, Location location) | |
2211 | { | |
2212 | mpz_t zval; | |
2213 | mpz_init_set_si(zval, val); | |
2214 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2215 | mpz_clear(zval); | |
2216 | return ret; | |
2217 | } | |
2218 | ||
3f378015 | 2219 | // Store an int64_t in an uninitialized mpz_t. |
2220 | ||
2221 | static void | |
2222 | set_mpz_from_int64(mpz_t* zval, int64_t val) | |
2223 | { | |
2224 | if (val >= 0) | |
2225 | { | |
2226 | unsigned long ul = static_cast<unsigned long>(val); | |
2227 | if (static_cast<int64_t>(ul) == val) | |
2228 | { | |
2229 | mpz_init_set_ui(*zval, ul); | |
2230 | return; | |
2231 | } | |
2232 | } | |
2233 | uint64_t uv; | |
2234 | if (val >= 0) | |
2235 | uv = static_cast<uint64_t>(val); | |
2236 | else | |
2237 | uv = static_cast<uint64_t>(- val); | |
2238 | unsigned long ul = uv & 0xffffffffUL; | |
2239 | mpz_init_set_ui(*zval, ul); | |
2240 | mpz_t hval; | |
2241 | mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); | |
2242 | mpz_mul_2exp(hval, hval, 32); | |
2243 | mpz_add(*zval, *zval, hval); | |
2244 | mpz_clear(hval); | |
2245 | if (val < 0) | |
2246 | mpz_neg(*zval, *zval); | |
2247 | } | |
2248 | ||
2249 | // Build a new integer value from an int64_t. | |
2250 | ||
2251 | Expression* | |
2252 | Expression::make_integer_int64(int64_t val, Type* type, Location location) | |
2253 | { | |
2254 | mpz_t zval; | |
2255 | set_mpz_from_int64(&zval, val); | |
2256 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2257 | mpz_clear(zval); | |
2258 | return ret; | |
2259 | } | |
2260 | ||
5d4b8566 | 2261 | // Build a new character constant value. |
2262 | ||
2263 | Expression* | |
2264 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2265 | { |
5d4b8566 | 2266 | return new Integer_expression(val, type, true, location); |
e440a328 | 2267 | } |
2268 | ||
2269 | // Floats. | |
2270 | ||
2271 | class Float_expression : public Expression | |
2272 | { | |
2273 | public: | |
b13c66cd | 2274 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2275 | : Expression(EXPRESSION_FLOAT, location), |
2276 | type_(type) | |
2277 | { | |
2278 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2279 | } | |
2280 | ||
e440a328 | 2281 | // Write VAL to export data. |
2282 | static void | |
8b1c301d | 2283 | export_float(String_dump* exp, const mpfr_t val); |
2284 | ||
d751bb78 | 2285 | // Write VAL to dump file. |
2286 | static void | |
2287 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2288 | |
2289 | protected: | |
2290 | bool | |
2291 | do_is_constant() const | |
2292 | { return true; } | |
2293 | ||
0e168074 | 2294 | bool |
3ae06f68 | 2295 | do_is_static_initializer() const |
0e168074 | 2296 | { return true; } |
2297 | ||
e440a328 | 2298 | bool |
0c77715b | 2299 | do_numeric_constant_value(Numeric_constant* nc) const |
2300 | { | |
2301 | nc->set_float(this->type_, this->val_); | |
2302 | return true; | |
2303 | } | |
e440a328 | 2304 | |
2305 | Type* | |
2306 | do_type(); | |
2307 | ||
2308 | void | |
2309 | do_determine_type(const Type_context*); | |
2310 | ||
2311 | void | |
2312 | do_check_types(Gogo*); | |
2313 | ||
2314 | Expression* | |
2315 | do_copy() | |
2316 | { return Expression::make_float(&this->val_, this->type_, | |
2317 | this->location()); } | |
2318 | ||
ea664253 | 2319 | Bexpression* |
2320 | do_get_backend(Translate_context*); | |
e440a328 | 2321 | |
2322 | void | |
2323 | do_export(Export*) const; | |
2324 | ||
d751bb78 | 2325 | void |
2326 | do_dump_expression(Ast_dump_context*) const; | |
2327 | ||
e440a328 | 2328 | private: |
2329 | // The floating point value. | |
2330 | mpfr_t val_; | |
2331 | // The type so far. | |
2332 | Type* type_; | |
2333 | }; | |
2334 | ||
e440a328 | 2335 | // Return the current type. If we haven't set the type yet, we return |
2336 | // an abstract float type. | |
2337 | ||
2338 | Type* | |
2339 | Float_expression::do_type() | |
2340 | { | |
2341 | if (this->type_ == NULL) | |
2342 | this->type_ = Type::make_abstract_float_type(); | |
2343 | return this->type_; | |
2344 | } | |
2345 | ||
2346 | // Set the type of the float value. Here we may switch from an | |
2347 | // abstract type to a real type. | |
2348 | ||
2349 | void | |
2350 | Float_expression::do_determine_type(const Type_context* context) | |
2351 | { | |
2352 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2353 | ; | |
2354 | else if (context->type != NULL | |
2355 | && (context->type->integer_type() != NULL | |
2356 | || context->type->float_type() != NULL | |
2357 | || context->type->complex_type() != NULL)) | |
2358 | this->type_ = context->type; | |
2359 | else if (!context->may_be_abstract) | |
48080209 | 2360 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2361 | } |
2362 | ||
e440a328 | 2363 | // Check the type of a float value. |
2364 | ||
2365 | void | |
2366 | Float_expression::do_check_types(Gogo*) | |
2367 | { | |
0c77715b | 2368 | Type* type = this->type_; |
2369 | if (type == NULL) | |
e440a328 | 2370 | return; |
0c77715b | 2371 | Numeric_constant nc; |
2372 | nc.set_float(NULL, this->val_); | |
2373 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2374 | this->set_is_error(); |
e440a328 | 2375 | } |
2376 | ||
ea664253 | 2377 | // Get the backend representation for a float constant. |
e440a328 | 2378 | |
ea664253 | 2379 | Bexpression* |
2380 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2381 | { |
12373dd5 | 2382 | if (this->is_error_expression() |
2383 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2384 | { | |
2385 | go_assert(saw_errors()); | |
2386 | return context->gogo()->backend()->error_expression(); | |
2387 | } | |
2388 | ||
48c2a53a | 2389 | Type* resolved_type; |
e440a328 | 2390 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2391 | resolved_type = this->type_; |
e440a328 | 2392 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2393 | { | |
2394 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2395 | resolved_type = Type::lookup_integer_type("int"); |
2396 | } | |
2397 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2398 | { | |
2399 | // We are converting to an abstract complex type. | |
2400 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2401 | } |
2402 | else | |
2403 | { | |
2404 | // If we still have an abstract type here, then this is being | |
2405 | // used in a constant expression which didn't get reduced. We | |
2406 | // just use float64 and hope for the best. | |
48c2a53a | 2407 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2408 | } |
48c2a53a | 2409 | |
2410 | Numeric_constant nc; | |
2411 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2412 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2413 | } |
2414 | ||
8b1c301d | 2415 | // Write a floating point number to a string dump. |
e440a328 | 2416 | |
2417 | void | |
8b1c301d | 2418 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2419 | { |
2420 | mp_exp_t exponent; | |
2421 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2422 | if (*s == '-') | |
2423 | exp->write_c_string("-"); | |
2424 | exp->write_c_string("0."); | |
2425 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2426 | mpfr_free_str(s); | |
2427 | char buf[30]; | |
2428 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2429 | exp->write_c_string(buf); | |
2430 | } | |
2431 | ||
2432 | // Export a floating point number in a constant expression. | |
2433 | ||
2434 | void | |
2435 | Float_expression::do_export(Export* exp) const | |
2436 | { | |
2437 | Float_expression::export_float(exp, this->val_); | |
2438 | // A trailing space lets us reliably identify the end of the number. | |
2439 | exp->write_c_string(" "); | |
2440 | } | |
2441 | ||
d751bb78 | 2442 | // Dump a floating point number to the dump file. |
2443 | ||
2444 | void | |
2445 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2446 | { | |
8b1c301d | 2447 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2448 | } |
2449 | ||
e440a328 | 2450 | // Make a float expression. |
2451 | ||
2452 | Expression* | |
b13c66cd | 2453 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2454 | { |
2455 | return new Float_expression(val, type, location); | |
2456 | } | |
2457 | ||
2458 | // Complex numbers. | |
2459 | ||
2460 | class Complex_expression : public Expression | |
2461 | { | |
2462 | public: | |
fcbea5e4 | 2463 | Complex_expression(const mpc_t* val, Type* type, Location location) |
e440a328 | 2464 | : Expression(EXPRESSION_COMPLEX, location), |
2465 | type_(type) | |
2466 | { | |
fcbea5e4 | 2467 | mpc_init2(this->val_, mpc_precision); |
2468 | mpc_set(this->val_, *val, MPC_RNDNN); | |
e440a328 | 2469 | } |
2470 | ||
fcbea5e4 | 2471 | // Write VAL to string dump. |
e440a328 | 2472 | static void |
fcbea5e4 | 2473 | export_complex(String_dump* exp, const mpc_t val); |
e440a328 | 2474 | |
d751bb78 | 2475 | // Write REAL/IMAG to dump context. |
2476 | static void | |
fcbea5e4 | 2477 | dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); |
d751bb78 | 2478 | |
e440a328 | 2479 | protected: |
2480 | bool | |
2481 | do_is_constant() const | |
2482 | { return true; } | |
2483 | ||
0e168074 | 2484 | bool |
3ae06f68 | 2485 | do_is_static_initializer() const |
0e168074 | 2486 | { return true; } |
2487 | ||
e440a328 | 2488 | bool |
0c77715b | 2489 | do_numeric_constant_value(Numeric_constant* nc) const |
2490 | { | |
fcbea5e4 | 2491 | nc->set_complex(this->type_, this->val_); |
0c77715b | 2492 | return true; |
2493 | } | |
e440a328 | 2494 | |
2495 | Type* | |
2496 | do_type(); | |
2497 | ||
2498 | void | |
2499 | do_determine_type(const Type_context*); | |
2500 | ||
2501 | void | |
2502 | do_check_types(Gogo*); | |
2503 | ||
2504 | Expression* | |
2505 | do_copy() | |
2506 | { | |
fcbea5e4 | 2507 | return Expression::make_complex(&this->val_, this->type_, |
e440a328 | 2508 | this->location()); |
2509 | } | |
2510 | ||
ea664253 | 2511 | Bexpression* |
2512 | do_get_backend(Translate_context*); | |
e440a328 | 2513 | |
2514 | void | |
2515 | do_export(Export*) const; | |
2516 | ||
d751bb78 | 2517 | void |
2518 | do_dump_expression(Ast_dump_context*) const; | |
abd26de0 | 2519 | |
e440a328 | 2520 | private: |
fcbea5e4 | 2521 | // The complex value. |
2522 | mpc_t val_; | |
e440a328 | 2523 | // The type if known. |
2524 | Type* type_; | |
2525 | }; | |
2526 | ||
e440a328 | 2527 | // Return the current type. If we haven't set the type yet, we return |
2528 | // an abstract complex type. | |
2529 | ||
2530 | Type* | |
2531 | Complex_expression::do_type() | |
2532 | { | |
2533 | if (this->type_ == NULL) | |
2534 | this->type_ = Type::make_abstract_complex_type(); | |
2535 | return this->type_; | |
2536 | } | |
2537 | ||
2538 | // Set the type of the complex value. Here we may switch from an | |
2539 | // abstract type to a real type. | |
2540 | ||
2541 | void | |
2542 | Complex_expression::do_determine_type(const Type_context* context) | |
2543 | { | |
2544 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2545 | ; | |
abd26de0 | 2546 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2547 | this->type_ = context->type; |
2548 | else if (!context->may_be_abstract) | |
48080209 | 2549 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2550 | } |
2551 | ||
e440a328 | 2552 | // Check the type of a complex value. |
2553 | ||
2554 | void | |
2555 | Complex_expression::do_check_types(Gogo*) | |
2556 | { | |
0c77715b | 2557 | Type* type = this->type_; |
2558 | if (type == NULL) | |
e440a328 | 2559 | return; |
0c77715b | 2560 | Numeric_constant nc; |
fcbea5e4 | 2561 | nc.set_complex(NULL, this->val_); |
0c77715b | 2562 | if (!nc.set_type(this->type_, true, this->location())) |
e440a328 | 2563 | this->set_is_error(); |
2564 | } | |
2565 | ||
ea664253 | 2566 | // Get the backend representation for a complex constant. |
e440a328 | 2567 | |
ea664253 | 2568 | Bexpression* |
2569 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2570 | { |
12373dd5 | 2571 | if (this->is_error_expression() |
2572 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2573 | { | |
2574 | go_assert(saw_errors()); | |
2575 | return context->gogo()->backend()->error_expression(); | |
2576 | } | |
2577 | ||
48c2a53a | 2578 | Type* resolved_type; |
e440a328 | 2579 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2580 | resolved_type = this->type_; |
2581 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2582 | { | |
2583 | // We are converting to an abstract integer type. | |
2584 | resolved_type = Type::lookup_integer_type("int"); | |
2585 | } | |
2586 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2587 | { | |
2588 | // We are converting to an abstract float type. | |
2589 | resolved_type = Type::lookup_float_type("float64"); | |
2590 | } | |
e440a328 | 2591 | else |
2592 | { | |
47ae02b7 | 2593 | // If we still have an abstract type here, this is being |
e440a328 | 2594 | // used in a constant expression which didn't get reduced. We |
2595 | // just use complex128 and hope for the best. | |
48c2a53a | 2596 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2597 | } |
48c2a53a | 2598 | |
2599 | Numeric_constant nc; | |
fcbea5e4 | 2600 | nc.set_complex(resolved_type, this->val_); |
ea664253 | 2601 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2602 | } |
2603 | ||
2604 | // Write REAL/IMAG to export data. | |
2605 | ||
2606 | void | |
fcbea5e4 | 2607 | Complex_expression::export_complex(String_dump* exp, const mpc_t val) |
e440a328 | 2608 | { |
fcbea5e4 | 2609 | if (!mpfr_zero_p(mpc_realref(val))) |
e440a328 | 2610 | { |
fcbea5e4 | 2611 | Float_expression::export_float(exp, mpc_realref(val)); |
d1db782d | 2612 | if (mpfr_sgn(mpc_imagref(val)) >= 0) |
e440a328 | 2613 | exp->write_c_string("+"); |
2614 | } | |
fcbea5e4 | 2615 | Float_expression::export_float(exp, mpc_imagref(val)); |
e440a328 | 2616 | exp->write_c_string("i"); |
2617 | } | |
2618 | ||
2619 | // Export a complex number in a constant expression. | |
2620 | ||
2621 | void | |
2622 | Complex_expression::do_export(Export* exp) const | |
2623 | { | |
fcbea5e4 | 2624 | Complex_expression::export_complex(exp, this->val_); |
e440a328 | 2625 | // A trailing space lets us reliably identify the end of the number. |
2626 | exp->write_c_string(" "); | |
2627 | } | |
2628 | ||
d751bb78 | 2629 | // Dump a complex expression to the dump file. |
2630 | ||
2631 | void | |
2632 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2633 | { | |
fcbea5e4 | 2634 | Complex_expression::export_complex(ast_dump_context, this->val_); |
d751bb78 | 2635 | } |
2636 | ||
e440a328 | 2637 | // Make a complex expression. |
2638 | ||
2639 | Expression* | |
fcbea5e4 | 2640 | Expression::make_complex(const mpc_t* val, Type* type, Location location) |
e440a328 | 2641 | { |
fcbea5e4 | 2642 | return new Complex_expression(val, type, location); |
e440a328 | 2643 | } |
2644 | ||
d5b605df | 2645 | // Find a named object in an expression. |
2646 | ||
2647 | class Find_named_object : public Traverse | |
2648 | { | |
2649 | public: | |
2650 | Find_named_object(Named_object* no) | |
2651 | : Traverse(traverse_expressions), | |
2652 | no_(no), found_(false) | |
2653 | { } | |
2654 | ||
2655 | // Whether we found the object. | |
2656 | bool | |
2657 | found() const | |
2658 | { return this->found_; } | |
2659 | ||
2660 | protected: | |
2661 | int | |
2662 | expression(Expression**); | |
2663 | ||
2664 | private: | |
2665 | // The object we are looking for. | |
2666 | Named_object* no_; | |
2667 | // Whether we found it. | |
2668 | bool found_; | |
2669 | }; | |
2670 | ||
e440a328 | 2671 | // A reference to a const in an expression. |
2672 | ||
2673 | class Const_expression : public Expression | |
2674 | { | |
2675 | public: | |
b13c66cd | 2676 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2677 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2678 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2679 | { } |
2680 | ||
d5b605df | 2681 | Named_object* |
2682 | named_object() | |
2683 | { return this->constant_; } | |
2684 | ||
a7f064d5 | 2685 | // Check that the initializer does not refer to the constant itself. |
2686 | void | |
2687 | check_for_init_loop(); | |
2688 | ||
e440a328 | 2689 | protected: |
ba4aedd4 | 2690 | int |
2691 | do_traverse(Traverse*); | |
2692 | ||
e440a328 | 2693 | Expression* |
ceeb4318 | 2694 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2695 | |
2696 | bool | |
2697 | do_is_constant() const | |
2698 | { return true; } | |
2699 | ||
0e168074 | 2700 | bool |
3ae06f68 | 2701 | do_is_static_initializer() const |
0e168074 | 2702 | { return true; } |
2703 | ||
e440a328 | 2704 | bool |
0c77715b | 2705 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2706 | |
2707 | bool | |
af6b489a | 2708 | do_string_constant_value(std::string* val) const; |
e440a328 | 2709 | |
2710 | Type* | |
2711 | do_type(); | |
2712 | ||
2713 | // The type of a const is set by the declaration, not the use. | |
2714 | void | |
2715 | do_determine_type(const Type_context*); | |
2716 | ||
2717 | void | |
2718 | do_check_types(Gogo*); | |
2719 | ||
2720 | Expression* | |
2721 | do_copy() | |
2722 | { return this; } | |
2723 | ||
ea664253 | 2724 | Bexpression* |
2725 | do_get_backend(Translate_context* context); | |
e440a328 | 2726 | |
2727 | // When exporting a reference to a const as part of a const | |
2728 | // expression, we export the value. We ignore the fact that it has | |
2729 | // a name. | |
2730 | void | |
2731 | do_export(Export* exp) const | |
2732 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2733 | ||
d751bb78 | 2734 | void |
2735 | do_dump_expression(Ast_dump_context*) const; | |
2736 | ||
e440a328 | 2737 | private: |
2738 | // The constant. | |
2739 | Named_object* constant_; | |
2740 | // The type of this reference. This is used if the constant has an | |
2741 | // abstract type. | |
2742 | Type* type_; | |
13e818f5 | 2743 | // Used to prevent infinite recursion when a constant incorrectly |
2744 | // refers to itself. | |
2745 | mutable bool seen_; | |
e440a328 | 2746 | }; |
2747 | ||
ba4aedd4 | 2748 | // Traversal. |
2749 | ||
2750 | int | |
2751 | Const_expression::do_traverse(Traverse* traverse) | |
2752 | { | |
2753 | if (this->type_ != NULL) | |
2754 | return Type::traverse(this->type_, traverse); | |
2755 | return TRAVERSE_CONTINUE; | |
2756 | } | |
2757 | ||
e440a328 | 2758 | // Lower a constant expression. This is where we convert the |
2759 | // predeclared constant iota into an integer value. | |
2760 | ||
2761 | Expression* | |
ceeb4318 | 2762 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2763 | Statement_inserter*, int iota_value) | |
e440a328 | 2764 | { |
2765 | if (this->constant_->const_value()->expr()->classification() | |
2766 | == EXPRESSION_IOTA) | |
2767 | { | |
2768 | if (iota_value == -1) | |
2769 | { | |
631d5788 | 2770 | go_error_at(this->location(), |
2771 | "iota is only defined in const declarations"); | |
e440a328 | 2772 | iota_value = 0; |
2773 | } | |
e67508fa | 2774 | return Expression::make_integer_ul(iota_value, NULL, this->location()); |
e440a328 | 2775 | } |
2776 | ||
2777 | // Make sure that the constant itself has been lowered. | |
2778 | gogo->lower_constant(this->constant_); | |
2779 | ||
2780 | return this; | |
2781 | } | |
2782 | ||
0c77715b | 2783 | // Return a numeric constant value. |
e440a328 | 2784 | |
2785 | bool | |
0c77715b | 2786 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2787 | { |
13e818f5 | 2788 | if (this->seen_) |
2789 | return false; | |
2790 | ||
e440a328 | 2791 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2792 | |
13e818f5 | 2793 | this->seen_ = true; |
2794 | ||
0c77715b | 2795 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2796 | |
13e818f5 | 2797 | this->seen_ = false; |
2798 | ||
e440a328 | 2799 | Type* ctype; |
2800 | if (this->type_ != NULL) | |
2801 | ctype = this->type_; | |
2802 | else | |
2803 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2804 | if (r && ctype != NULL) |
2805 | { | |
0c77715b | 2806 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2807 | return false; |
e440a328 | 2808 | } |
e440a328 | 2809 | |
e440a328 | 2810 | return r; |
2811 | } | |
2812 | ||
af6b489a | 2813 | bool |
2814 | Const_expression::do_string_constant_value(std::string* val) const | |
2815 | { | |
2816 | if (this->seen_) | |
2817 | return false; | |
2818 | ||
2819 | Expression* e = this->constant_->const_value()->expr(); | |
2820 | ||
2821 | this->seen_ = true; | |
2822 | bool ok = e->string_constant_value(val); | |
2823 | this->seen_ = false; | |
2824 | ||
2825 | return ok; | |
2826 | } | |
2827 | ||
e440a328 | 2828 | // Return the type of the const reference. |
2829 | ||
2830 | Type* | |
2831 | Const_expression::do_type() | |
2832 | { | |
2833 | if (this->type_ != NULL) | |
2834 | return this->type_; | |
13e818f5 | 2835 | |
2f78f012 | 2836 | Named_constant* nc = this->constant_->const_value(); |
2837 | ||
2838 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2839 | { |
2840 | this->report_error(_("constant refers to itself")); | |
2841 | this->type_ = Type::make_error_type(); | |
2842 | return this->type_; | |
2843 | } | |
2844 | ||
2845 | this->seen_ = true; | |
2846 | ||
e440a328 | 2847 | Type* ret = nc->type(); |
13e818f5 | 2848 | |
e440a328 | 2849 | if (ret != NULL) |
13e818f5 | 2850 | { |
2851 | this->seen_ = false; | |
2852 | return ret; | |
2853 | } | |
2854 | ||
e440a328 | 2855 | // During parsing, a named constant may have a NULL type, but we |
2856 | // must not return a NULL type here. | |
13e818f5 | 2857 | ret = nc->expr()->type(); |
2858 | ||
2859 | this->seen_ = false; | |
2860 | ||
2861 | return ret; | |
e440a328 | 2862 | } |
2863 | ||
2864 | // Set the type of the const reference. | |
2865 | ||
2866 | void | |
2867 | Const_expression::do_determine_type(const Type_context* context) | |
2868 | { | |
2869 | Type* ctype = this->constant_->const_value()->type(); | |
2870 | Type* cetype = (ctype != NULL | |
2871 | ? ctype | |
2872 | : this->constant_->const_value()->expr()->type()); | |
2873 | if (ctype != NULL && !ctype->is_abstract()) | |
2874 | ; | |
2875 | else if (context->type != NULL | |
0c77715b | 2876 | && context->type->is_numeric_type() |
2877 | && cetype->is_numeric_type()) | |
e440a328 | 2878 | this->type_ = context->type; |
2879 | else if (context->type != NULL | |
2880 | && context->type->is_string_type() | |
2881 | && cetype->is_string_type()) | |
2882 | this->type_ = context->type; | |
2883 | else if (context->type != NULL | |
2884 | && context->type->is_boolean_type() | |
2885 | && cetype->is_boolean_type()) | |
2886 | this->type_ = context->type; | |
2887 | else if (!context->may_be_abstract) | |
2888 | { | |
2889 | if (cetype->is_abstract()) | |
2890 | cetype = cetype->make_non_abstract_type(); | |
2891 | this->type_ = cetype; | |
2892 | } | |
2893 | } | |
2894 | ||
a7f064d5 | 2895 | // Check for a loop in which the initializer of a constant refers to |
2896 | // the constant itself. | |
e440a328 | 2897 | |
2898 | void | |
a7f064d5 | 2899 | Const_expression::check_for_init_loop() |
e440a328 | 2900 | { |
5c13bd80 | 2901 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2902 | return; |
2903 | ||
a7f064d5 | 2904 | if (this->seen_) |
2905 | { | |
2906 | this->report_error(_("constant refers to itself")); | |
2907 | this->type_ = Type::make_error_type(); | |
2908 | return; | |
2909 | } | |
2910 | ||
d5b605df | 2911 | Expression* init = this->constant_->const_value()->expr(); |
2912 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2913 | |
2914 | this->seen_ = true; | |
d5b605df | 2915 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2916 | this->seen_ = false; |
2917 | ||
d5b605df | 2918 | if (find_named_object.found()) |
2919 | { | |
5c13bd80 | 2920 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2921 | { |
2922 | this->report_error(_("constant refers to itself")); | |
2923 | this->type_ = Type::make_error_type(); | |
2924 | } | |
d5b605df | 2925 | return; |
2926 | } | |
a7f064d5 | 2927 | } |
2928 | ||
2929 | // Check types of a const reference. | |
2930 | ||
2931 | void | |
2932 | Const_expression::do_check_types(Gogo*) | |
2933 | { | |
5c13bd80 | 2934 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2935 | return; |
2936 | ||
2937 | this->check_for_init_loop(); | |
d5b605df | 2938 | |
0c77715b | 2939 | // Check that numeric constant fits in type. |
2940 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2941 | { |
0c77715b | 2942 | Numeric_constant nc; |
2943 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2944 | { |
0c77715b | 2945 | if (!nc.set_type(this->type_, true, this->location())) |
2946 | this->set_is_error(); | |
e440a328 | 2947 | } |
e440a328 | 2948 | } |
2949 | } | |
2950 | ||
ea664253 | 2951 | // Return the backend representation for a const reference. |
e440a328 | 2952 | |
ea664253 | 2953 | Bexpression* |
2954 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2955 | { |
12373dd5 | 2956 | if (this->is_error_expression() |
2957 | || (this->type_ != NULL && this->type_->is_error())) | |
2958 | { | |
2959 | go_assert(saw_errors()); | |
2960 | return context->backend()->error_expression(); | |
2961 | } | |
e440a328 | 2962 | |
2963 | // If the type has been set for this expression, but the underlying | |
2964 | // object is an abstract int or float, we try to get the abstract | |
2965 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2966 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2967 | if (this->type_ != NULL |
0c77715b | 2968 | && this->type_->is_numeric_type() |
a68492b4 | 2969 | && (this->constant_->const_value()->type() == NULL |
2970 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2971 | { |
0c77715b | 2972 | Numeric_constant nc; |
2973 | if (expr->numeric_constant_value(&nc) | |
2974 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2975 | { |
0c77715b | 2976 | Expression* e = nc.expression(this->location()); |
ea664253 | 2977 | return e->get_backend(context); |
e440a328 | 2978 | } |
e440a328 | 2979 | } |
2980 | ||
2c809f8f | 2981 | if (this->type_ != NULL) |
f2de4532 | 2982 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 2983 | return expr->get_backend(context); |
e440a328 | 2984 | } |
2985 | ||
d751bb78 | 2986 | // Dump ast representation for constant expression. |
2987 | ||
2988 | void | |
2989 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2990 | { | |
2991 | ast_dump_context->ostream() << this->constant_->name(); | |
2992 | } | |
2993 | ||
e440a328 | 2994 | // Make a reference to a constant in an expression. |
2995 | ||
2996 | Expression* | |
2997 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2998 | Location location) |
e440a328 | 2999 | { |
3000 | return new Const_expression(constant, location); | |
3001 | } | |
3002 | ||
d5b605df | 3003 | // Find a named object in an expression. |
3004 | ||
3005 | int | |
3006 | Find_named_object::expression(Expression** pexpr) | |
3007 | { | |
3008 | switch ((*pexpr)->classification()) | |
3009 | { | |
3010 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 3011 | { |
3012 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
3013 | if (ce->named_object() == this->no_) | |
3014 | break; | |
3015 | ||
3016 | // We need to check a constant initializer explicitly, as | |
3017 | // loops here will not be caught by the loop checking for | |
3018 | // variable initializers. | |
3019 | ce->check_for_init_loop(); | |
3020 | ||
3021 | return TRAVERSE_CONTINUE; | |
3022 | } | |
3023 | ||
d5b605df | 3024 | case Expression::EXPRESSION_VAR_REFERENCE: |
3025 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
3026 | break; | |
3027 | return TRAVERSE_CONTINUE; | |
3028 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
3029 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
3030 | break; | |
3031 | return TRAVERSE_CONTINUE; | |
3032 | default: | |
3033 | return TRAVERSE_CONTINUE; | |
3034 | } | |
3035 | this->found_ = true; | |
3036 | return TRAVERSE_EXIT; | |
3037 | } | |
3038 | ||
e440a328 | 3039 | // The nil value. |
3040 | ||
3041 | class Nil_expression : public Expression | |
3042 | { | |
3043 | public: | |
b13c66cd | 3044 | Nil_expression(Location location) |
e440a328 | 3045 | : Expression(EXPRESSION_NIL, location) |
3046 | { } | |
3047 | ||
3048 | static Expression* | |
3049 | do_import(Import*); | |
3050 | ||
3051 | protected: | |
3052 | bool | |
3053 | do_is_constant() const | |
3054 | { return true; } | |
3055 | ||
f9ca30f9 | 3056 | bool |
3ae06f68 | 3057 | do_is_static_initializer() const |
f9ca30f9 | 3058 | { return true; } |
3059 | ||
e440a328 | 3060 | Type* |
3061 | do_type() | |
3062 | { return Type::make_nil_type(); } | |
3063 | ||
3064 | void | |
3065 | do_determine_type(const Type_context*) | |
3066 | { } | |
3067 | ||
3068 | Expression* | |
3069 | do_copy() | |
3070 | { return this; } | |
3071 | ||
ea664253 | 3072 | Bexpression* |
3073 | do_get_backend(Translate_context* context) | |
3074 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 3075 | |
3076 | void | |
3077 | do_export(Export* exp) const | |
3078 | { exp->write_c_string("nil"); } | |
d751bb78 | 3079 | |
3080 | void | |
3081 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3082 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 3083 | }; |
3084 | ||
3085 | // Import a nil expression. | |
3086 | ||
3087 | Expression* | |
3088 | Nil_expression::do_import(Import* imp) | |
3089 | { | |
3090 | imp->require_c_string("nil"); | |
3091 | return Expression::make_nil(imp->location()); | |
3092 | } | |
3093 | ||
3094 | // Make a nil expression. | |
3095 | ||
3096 | Expression* | |
b13c66cd | 3097 | Expression::make_nil(Location location) |
e440a328 | 3098 | { |
3099 | return new Nil_expression(location); | |
3100 | } | |
3101 | ||
3102 | // The value of the predeclared constant iota. This is little more | |
3103 | // than a marker. This will be lowered to an integer in | |
3104 | // Const_expression::do_lower, which is where we know the value that | |
3105 | // it should have. | |
3106 | ||
3107 | class Iota_expression : public Parser_expression | |
3108 | { | |
3109 | public: | |
b13c66cd | 3110 | Iota_expression(Location location) |
e440a328 | 3111 | : Parser_expression(EXPRESSION_IOTA, location) |
3112 | { } | |
3113 | ||
3114 | protected: | |
3115 | Expression* | |
ceeb4318 | 3116 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 3117 | { go_unreachable(); } |
e440a328 | 3118 | |
3119 | // There should only ever be one of these. | |
3120 | Expression* | |
3121 | do_copy() | |
c3e6f413 | 3122 | { go_unreachable(); } |
d751bb78 | 3123 | |
3124 | void | |
3125 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3126 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3127 | }; |
3128 | ||
3129 | // Make an iota expression. This is only called for one case: the | |
3130 | // value of the predeclared constant iota. | |
3131 | ||
3132 | Expression* | |
3133 | Expression::make_iota() | |
3134 | { | |
b13c66cd | 3135 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3136 | return &iota_expression; |
3137 | } | |
3138 | ||
da244e59 | 3139 | // Class Type_conversion_expression. |
e440a328 | 3140 | |
3141 | // Traversal. | |
3142 | ||
3143 | int | |
3144 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3145 | { | |
3146 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3147 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3148 | return TRAVERSE_EXIT; | |
3149 | return TRAVERSE_CONTINUE; | |
3150 | } | |
3151 | ||
3152 | // Convert to a constant at lowering time. | |
3153 | ||
3154 | Expression* | |
ceeb4318 | 3155 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3156 | Statement_inserter*, int) | |
e440a328 | 3157 | { |
3158 | Type* type = this->type_; | |
3159 | Expression* val = this->expr_; | |
b13c66cd | 3160 | Location location = this->location(); |
e440a328 | 3161 | |
0c77715b | 3162 | if (type->is_numeric_type()) |
e440a328 | 3163 | { |
0c77715b | 3164 | Numeric_constant nc; |
3165 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3166 | { |
0c77715b | 3167 | if (!nc.set_type(type, true, location)) |
3168 | return Expression::make_error(location); | |
3169 | return nc.expression(location); | |
e440a328 | 3170 | } |
e440a328 | 3171 | } |
3172 | ||
d7739c9a | 3173 | // According to the language specification on string conversions |
3174 | // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): | |
3175 | // When converting an integer into a string, the string will be a UTF-8 | |
3176 | // representation of the integer and integers "outside the range of valid | |
3177 | // Unicode code points are converted to '\uFFFD'." | |
3178 | if (type->is_string_type()) | |
3179 | { | |
3180 | Numeric_constant nc; | |
3181 | if (val->numeric_constant_value(&nc) && nc.is_int()) | |
3182 | { | |
3183 | // An integer value doesn't fit in the Unicode code point range if it | |
3184 | // overflows the Go "int" type or is negative. | |
3185 | unsigned long ul; | |
3186 | if (!nc.set_type(Type::lookup_integer_type("int"), false, location) | |
3187 | || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) | |
3188 | return Expression::make_string("\ufffd", location); | |
3189 | } | |
3190 | } | |
3191 | ||
55072f2b | 3192 | if (type->is_slice_type()) |
e440a328 | 3193 | { |
3194 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3195 | bool is_byte = (element_type->integer_type() != NULL |
3196 | && element_type->integer_type()->is_byte()); | |
3197 | bool is_rune = (element_type->integer_type() != NULL | |
3198 | && element_type->integer_type()->is_rune()); | |
3199 | if (is_byte || is_rune) | |
e440a328 | 3200 | { |
3201 | std::string s; | |
3202 | if (val->string_constant_value(&s)) | |
3203 | { | |
3204 | Expression_list* vals = new Expression_list(); | |
3205 | if (is_byte) | |
3206 | { | |
3207 | for (std::string::const_iterator p = s.begin(); | |
3208 | p != s.end(); | |
3209 | p++) | |
3210 | { | |
e67508fa | 3211 | unsigned char c = static_cast<unsigned char>(*p); |
3212 | vals->push_back(Expression::make_integer_ul(c, | |
3213 | element_type, | |
3214 | location)); | |
e440a328 | 3215 | } |
3216 | } | |
3217 | else | |
3218 | { | |
3219 | const char *p = s.data(); | |
3220 | const char *pend = s.data() + s.length(); | |
3221 | while (p < pend) | |
3222 | { | |
3223 | unsigned int c; | |
3224 | int adv = Lex::fetch_char(p, &c); | |
3225 | if (adv == 0) | |
3226 | { | |
631d5788 | 3227 | go_warning_at(this->location(), 0, |
e440a328 | 3228 | "invalid UTF-8 encoding"); |
3229 | adv = 1; | |
3230 | } | |
3231 | p += adv; | |
e67508fa | 3232 | vals->push_back(Expression::make_integer_ul(c, |
3233 | element_type, | |
3234 | location)); | |
e440a328 | 3235 | } |
3236 | } | |
3237 | ||
3238 | return Expression::make_slice_composite_literal(type, vals, | |
3239 | location); | |
3240 | } | |
3241 | } | |
3242 | } | |
3243 | ||
3244 | return this; | |
3245 | } | |
3246 | ||
35a54f17 | 3247 | // Flatten a type conversion by using a temporary variable for the slice |
3248 | // in slice to string conversions. | |
3249 | ||
3250 | Expression* | |
3251 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3252 | Statement_inserter* inserter) | |
3253 | { | |
5bf8be8b | 3254 | if (this->type()->is_error_type() || this->expr_->is_error_expression()) |
3255 | { | |
3256 | go_assert(saw_errors()); | |
3257 | return Expression::make_error(this->location()); | |
3258 | } | |
3259 | ||
2c809f8f | 3260 | if (((this->type()->is_string_type() |
3261 | && this->expr_->type()->is_slice_type()) | |
8ba8cc87 | 3262 | || this->expr_->type()->interface_type() != NULL) |
35a54f17 | 3263 | && !this->expr_->is_variable()) |
3264 | { | |
3265 | Temporary_statement* temp = | |
3266 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3267 | inserter->insert(temp); | |
3268 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3269 | } | |
3270 | return this; | |
3271 | } | |
3272 | ||
1ca01a59 | 3273 | // Return whether a type conversion is a constant. |
3274 | ||
3275 | bool | |
3276 | Type_conversion_expression::do_is_constant() const | |
3277 | { | |
3278 | if (!this->expr_->is_constant()) | |
3279 | return false; | |
3280 | ||
3281 | // A conversion to a type that may not be used as a constant is not | |
3282 | // a constant. For example, []byte(nil). | |
3283 | Type* type = this->type_; | |
3284 | if (type->integer_type() == NULL | |
3285 | && type->float_type() == NULL | |
3286 | && type->complex_type() == NULL | |
3287 | && !type->is_boolean_type() | |
3288 | && !type->is_string_type()) | |
3289 | return false; | |
3290 | ||
3291 | return true; | |
3292 | } | |
3293 | ||
3ae06f68 | 3294 | // Return whether a type conversion can be used in a constant |
3295 | // initializer. | |
0e168074 | 3296 | |
3297 | bool | |
3ae06f68 | 3298 | Type_conversion_expression::do_is_static_initializer() const |
0e168074 | 3299 | { |
3300 | Type* type = this->type_; | |
3301 | Type* expr_type = this->expr_->type(); | |
3302 | ||
3303 | if (type->interface_type() != NULL | |
3304 | || expr_type->interface_type() != NULL) | |
3305 | return false; | |
3306 | ||
3ae06f68 | 3307 | if (!this->expr_->is_static_initializer()) |
0e168074 | 3308 | return false; |
3309 | ||
3310 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3311 | return true; | |
3312 | ||
03118c21 | 3313 | if (type->is_string_type() && expr_type->is_string_type()) |
3314 | return true; | |
3315 | ||
3316 | if ((type->is_numeric_type() | |
3317 | || type->is_boolean_type() | |
3318 | || type->points_to() != NULL) | |
3319 | && (expr_type->is_numeric_type() | |
3320 | || expr_type->is_boolean_type() | |
3321 | || expr_type->points_to() != NULL)) | |
3322 | return true; | |
3323 | ||
3324 | return false; | |
0e168074 | 3325 | } |
3326 | ||
0c77715b | 3327 | // Return the constant numeric value if there is one. |
e440a328 | 3328 | |
3329 | bool | |
0c77715b | 3330 | Type_conversion_expression::do_numeric_constant_value( |
3331 | Numeric_constant* nc) const | |
e440a328 | 3332 | { |
0c77715b | 3333 | if (!this->type_->is_numeric_type()) |
e440a328 | 3334 | return false; |
0c77715b | 3335 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3336 | return false; |
0c77715b | 3337 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3338 | } |
3339 | ||
3340 | // Return the constant string value if there is one. | |
3341 | ||
3342 | bool | |
3343 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3344 | { | |
3345 | if (this->type_->is_string_type() | |
3346 | && this->expr_->type()->integer_type() != NULL) | |
3347 | { | |
0c77715b | 3348 | Numeric_constant nc; |
3349 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3350 | { |
0c77715b | 3351 | unsigned long ival; |
3352 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3353 | { |
0c77715b | 3354 | val->clear(); |
3355 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3356 | return true; |
3357 | } | |
3358 | } | |
e440a328 | 3359 | } |
3360 | ||
3361 | // FIXME: Could handle conversion from const []int here. | |
3362 | ||
3363 | return false; | |
3364 | } | |
3365 | ||
da244e59 | 3366 | // Determine the resulting type of the conversion. |
3367 | ||
3368 | void | |
3369 | Type_conversion_expression::do_determine_type(const Type_context*) | |
3370 | { | |
3371 | Type_context subcontext(this->type_, false); | |
3372 | this->expr_->determine_type(&subcontext); | |
3373 | } | |
3374 | ||
e440a328 | 3375 | // Check that types are convertible. |
3376 | ||
3377 | void | |
3378 | Type_conversion_expression::do_check_types(Gogo*) | |
3379 | { | |
3380 | Type* type = this->type_; | |
3381 | Type* expr_type = this->expr_->type(); | |
3382 | std::string reason; | |
3383 | ||
5c13bd80 | 3384 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3385 | { |
842f6425 | 3386 | this->set_is_error(); |
3387 | return; | |
3388 | } | |
3389 | ||
e440a328 | 3390 | if (this->may_convert_function_types_ |
3391 | && type->function_type() != NULL | |
3392 | && expr_type->function_type() != NULL) | |
3393 | return; | |
3394 | ||
3395 | if (Type::are_convertible(type, expr_type, &reason)) | |
3396 | return; | |
3397 | ||
631d5788 | 3398 | go_error_at(this->location(), "%s", reason.c_str()); |
e440a328 | 3399 | this->set_is_error(); |
3400 | } | |
3401 | ||
ea664253 | 3402 | // Get the backend representation for a type conversion. |
e440a328 | 3403 | |
ea664253 | 3404 | Bexpression* |
3405 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3406 | { |
e440a328 | 3407 | Type* type = this->type_; |
3408 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3409 | |
3410 | Gogo* gogo = context->gogo(); | |
3411 | Btype* btype = type->get_backend(gogo); | |
2c809f8f | 3412 | Location loc = this->location(); |
3413 | ||
3414 | if (Type::are_identical(type, expr_type, false, NULL)) | |
859cdc93 | 3415 | { |
3416 | Bexpression* bexpr = this->expr_->get_backend(context); | |
3417 | return gogo->backend()->convert_expression(btype, bexpr, loc); | |
3418 | } | |
2c809f8f | 3419 | else if (type->interface_type() != NULL |
3420 | || expr_type->interface_type() != NULL) | |
e440a328 | 3421 | { |
2c809f8f | 3422 | Expression* conversion = |
3423 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3424 | this->location()); | |
ea664253 | 3425 | return conversion->get_backend(context); |
e440a328 | 3426 | } |
3427 | else if (type->is_string_type() | |
3428 | && expr_type->integer_type() != NULL) | |
3429 | { | |
2c809f8f | 3430 | mpz_t intval; |
3431 | Numeric_constant nc; | |
3432 | if (this->expr_->numeric_constant_value(&nc) | |
3433 | && nc.to_int(&intval) | |
3434 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3435 | { |
e440a328 | 3436 | std::string s; |
2c809f8f | 3437 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3438 | mpz_clear(intval); | |
3439 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3440 | return se->get_backend(context); |
e440a328 | 3441 | } |
3442 | ||
f16ab008 | 3443 | Expression* i2s_expr = |
736a16ba | 3444 | Runtime::make_call(Runtime::INTSTRING, loc, 2, |
3445 | Expression::make_nil(loc), this->expr_); | |
ea664253 | 3446 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3447 | } |
55072f2b | 3448 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3449 | { |
55072f2b | 3450 | Array_type* a = expr_type->array_type(); |
e440a328 | 3451 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3452 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3453 | go_assert(this->expr_->is_variable()); |
3454 | ||
3455 | Runtime::Function code; | |
60963afd | 3456 | if (e->integer_type()->is_byte()) |
736a16ba | 3457 | code = Runtime::SLICEBYTETOSTRING; |
e440a328 | 3458 | else |
35a54f17 | 3459 | { |
3460 | go_assert(e->integer_type()->is_rune()); | |
736a16ba | 3461 | code = Runtime::SLICERUNETOSTRING; |
35a54f17 | 3462 | } |
736a16ba | 3463 | return Runtime::make_call(code, loc, 2, Expression::make_nil(loc), |
3464 | this->expr_)->get_backend(context); | |
e440a328 | 3465 | } |
411eb89e | 3466 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3467 | { |
3468 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3469 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3470 | |
2c809f8f | 3471 | Runtime::Function code; |
60963afd | 3472 | if (e->integer_type()->is_byte()) |
736a16ba | 3473 | code = Runtime::STRINGTOSLICEBYTE; |
e440a328 | 3474 | else |
3475 | { | |
60963afd | 3476 | go_assert(e->integer_type()->is_rune()); |
736a16ba | 3477 | code = Runtime::STRINGTOSLICERUNE; |
e440a328 | 3478 | } |
736a16ba | 3479 | Expression* s2a = Runtime::make_call(code, loc, 2, |
3480 | Expression::make_nil(loc), | |
3481 | this->expr_); | |
ea664253 | 3482 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3483 | } |
3484 | else if (type->is_numeric_type()) | |
3485 | { | |
3486 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
859cdc93 | 3487 | Bexpression* bexpr = this->expr_->get_backend(context); |
ea664253 | 3488 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3489 | } |
3490 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3491 | && (expr_type->points_to() != NULL |
3492 | || expr_type->integer_type())) | |
3493 | || (expr_type->is_unsafe_pointer_type() | |
3494 | && type->points_to() != NULL) | |
3495 | || (this->may_convert_function_types_ | |
3496 | && type->function_type() != NULL | |
3497 | && expr_type->function_type() != NULL)) | |
859cdc93 | 3498 | { |
3499 | Bexpression* bexpr = this->expr_->get_backend(context); | |
3500 | return gogo->backend()->convert_expression(btype, bexpr, loc); | |
3501 | } | |
e440a328 | 3502 | else |
2c809f8f | 3503 | { |
3504 | Expression* conversion = | |
3505 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3506 | return conversion->get_backend(context); |
2c809f8f | 3507 | } |
e440a328 | 3508 | } |
3509 | ||
3510 | // Output a type conversion in a constant expression. | |
3511 | ||
3512 | void | |
3513 | Type_conversion_expression::do_export(Export* exp) const | |
3514 | { | |
3515 | exp->write_c_string("convert("); | |
3516 | exp->write_type(this->type_); | |
3517 | exp->write_c_string(", "); | |
3518 | this->expr_->export_expression(exp); | |
3519 | exp->write_c_string(")"); | |
3520 | } | |
3521 | ||
3522 | // Import a type conversion or a struct construction. | |
3523 | ||
3524 | Expression* | |
3525 | Type_conversion_expression::do_import(Import* imp) | |
3526 | { | |
3527 | imp->require_c_string("convert("); | |
3528 | Type* type = imp->read_type(); | |
3529 | imp->require_c_string(", "); | |
3530 | Expression* val = Expression::import_expression(imp); | |
3531 | imp->require_c_string(")"); | |
3532 | return Expression::make_cast(type, val, imp->location()); | |
3533 | } | |
3534 | ||
d751bb78 | 3535 | // Dump ast representation for a type conversion expression. |
3536 | ||
3537 | void | |
3538 | Type_conversion_expression::do_dump_expression( | |
3539 | Ast_dump_context* ast_dump_context) const | |
3540 | { | |
3541 | ast_dump_context->dump_type(this->type_); | |
3542 | ast_dump_context->ostream() << "("; | |
3543 | ast_dump_context->dump_expression(this->expr_); | |
3544 | ast_dump_context->ostream() << ") "; | |
3545 | } | |
3546 | ||
e440a328 | 3547 | // Make a type cast expression. |
3548 | ||
3549 | Expression* | |
b13c66cd | 3550 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3551 | { |
3552 | if (type->is_error_type() || val->is_error_expression()) | |
3553 | return Expression::make_error(location); | |
3554 | return new Type_conversion_expression(type, val, location); | |
3555 | } | |
3556 | ||
98f62f7a | 3557 | // Class Unsafe_type_conversion_expression. |
9581e91d | 3558 | |
3559 | // Traversal. | |
3560 | ||
3561 | int | |
3562 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3563 | { | |
3564 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3565 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3566 | return TRAVERSE_EXIT; | |
3567 | return TRAVERSE_CONTINUE; | |
3568 | } | |
3569 | ||
3ae06f68 | 3570 | // Return whether an unsafe type conversion can be used as a constant |
3571 | // initializer. | |
aa5ae575 | 3572 | |
3573 | bool | |
3ae06f68 | 3574 | Unsafe_type_conversion_expression::do_is_static_initializer() const |
aa5ae575 | 3575 | { |
3576 | Type* type = this->type_; | |
3577 | Type* expr_type = this->expr_->type(); | |
3578 | ||
3579 | if (type->interface_type() != NULL | |
3580 | || expr_type->interface_type() != NULL) | |
3581 | return false; | |
3582 | ||
3ae06f68 | 3583 | if (!this->expr_->is_static_initializer()) |
aa5ae575 | 3584 | return false; |
3585 | ||
3586 | if (Type::are_convertible(type, expr_type, NULL)) | |
3587 | return true; | |
3588 | ||
03118c21 | 3589 | if (type->is_string_type() && expr_type->is_string_type()) |
3590 | return true; | |
3591 | ||
3592 | if ((type->is_numeric_type() | |
3593 | || type->is_boolean_type() | |
3594 | || type->points_to() != NULL) | |
3595 | && (expr_type->is_numeric_type() | |
3596 | || expr_type->is_boolean_type() | |
3597 | || expr_type->points_to() != NULL)) | |
3598 | return true; | |
3599 | ||
3600 | return false; | |
aa5ae575 | 3601 | } |
3602 | ||
9581e91d | 3603 | // Convert to backend representation. |
3604 | ||
ea664253 | 3605 | Bexpression* |
3606 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3607 | { |
3608 | // We are only called for a limited number of cases. | |
3609 | ||
3610 | Type* t = this->type_; | |
3611 | Type* et = this->expr_->type(); | |
5c4802f1 | 3612 | |
3613 | if (t->is_error_type() | |
3614 | || this->expr_->is_error_expression() | |
3615 | || et->is_error_type()) | |
3616 | { | |
3617 | go_assert(saw_errors()); | |
3618 | return context->backend()->error_expression(); | |
3619 | } | |
3620 | ||
2c809f8f | 3621 | if (t->array_type() != NULL) |
3622 | go_assert(et->array_type() != NULL | |
3623 | && t->is_slice_type() == et->is_slice_type()); | |
3624 | else if (t->struct_type() != NULL) | |
9581e91d | 3625 | { |
2c809f8f | 3626 | if (t->named_type() != NULL |
3627 | && et->named_type() != NULL | |
3628 | && !Type::are_convertible(t, et, NULL)) | |
3629 | { | |
3630 | go_assert(saw_errors()); | |
ea664253 | 3631 | return context->backend()->error_expression(); |
2c809f8f | 3632 | } |
3633 | ||
3634 | go_assert(et->struct_type() != NULL | |
3635 | && Type::are_convertible(t, et, NULL)); | |
3636 | } | |
3637 | else if (t->map_type() != NULL) | |
c484d925 | 3638 | go_assert(et->map_type() != NULL); |
9581e91d | 3639 | else if (t->channel_type() != NULL) |
c484d925 | 3640 | go_assert(et->channel_type() != NULL); |
09ea332d | 3641 | else if (t->points_to() != NULL) |
2c809f8f | 3642 | go_assert(et->points_to() != NULL |
3643 | || et->channel_type() != NULL | |
3644 | || et->map_type() != NULL | |
3645 | || et->function_type() != NULL | |
132ed071 | 3646 | || et->integer_type() != NULL |
2c809f8f | 3647 | || et->is_nil_type()); |
9581e91d | 3648 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3649 | go_assert(t->points_to() != NULL); |
2c809f8f | 3650 | else if (t->interface_type() != NULL) |
9581e91d | 3651 | { |
2c809f8f | 3652 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3653 | go_assert(et->interface_type() != NULL |
2c809f8f | 3654 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3655 | } |
588e3cf9 | 3656 | else if (t->integer_type() != NULL) |
2c809f8f | 3657 | go_assert(et->is_boolean_type() |
3658 | || et->integer_type() != NULL | |
3659 | || et->function_type() != NULL | |
3660 | || et->points_to() != NULL | |
3661 | || et->map_type() != NULL | |
8ba8cc87 | 3662 | || et->channel_type() != NULL |
3663 | || et->is_nil_type()); | |
cd39797e | 3664 | else if (t->function_type() != NULL) |
3665 | go_assert(et->points_to() != NULL); | |
9581e91d | 3666 | else |
c3e6f413 | 3667 | go_unreachable(); |
9581e91d | 3668 | |
2c809f8f | 3669 | Gogo* gogo = context->gogo(); |
3670 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3671 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3672 | Location loc = this->location(); |
ea664253 | 3673 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3674 | } |
3675 | ||
d751bb78 | 3676 | // Dump ast representation for an unsafe type conversion expression. |
3677 | ||
3678 | void | |
3679 | Unsafe_type_conversion_expression::do_dump_expression( | |
3680 | Ast_dump_context* ast_dump_context) const | |
3681 | { | |
3682 | ast_dump_context->dump_type(this->type_); | |
3683 | ast_dump_context->ostream() << "("; | |
3684 | ast_dump_context->dump_expression(this->expr_); | |
3685 | ast_dump_context->ostream() << ") "; | |
3686 | } | |
3687 | ||
9581e91d | 3688 | // Make an unsafe type conversion expression. |
3689 | ||
3690 | Expression* | |
3691 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3692 | Location location) |
9581e91d | 3693 | { |
3694 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3695 | } | |
3696 | ||
76f85fd6 | 3697 | // Class Unary_expression. |
e440a328 | 3698 | |
03118c21 | 3699 | // Call the address_taken method of the operand if needed. This is |
3700 | // called after escape analysis but before inserting write barriers. | |
3701 | ||
3702 | void | |
3703 | Unary_expression::check_operand_address_taken(Gogo* gogo) | |
3704 | { | |
3705 | if (this->op_ != OPERATOR_AND) | |
3706 | return; | |
3707 | ||
3708 | // If this->escapes_ is false at this point, then it was set to | |
3709 | // false by an explicit call to set_does_not_escape, and the value | |
3710 | // does not escape. If this->escapes_ is true, we may be able to | |
3711 | // set it to false if taking the address of a variable that does not | |
3712 | // escape. | |
3713 | Node* n = Node::make_node(this); | |
3714 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
3715 | this->escapes_ = false; | |
3716 | ||
3717 | // When compiling the runtime, the address operator does not cause | |
3718 | // local variables to escape. When escape analysis becomes the | |
3719 | // default, this should be changed to make it an error if we have an | |
3720 | // address operator that escapes. | |
3721 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
3722 | this->escapes_ = false; | |
3723 | ||
3724 | Named_object* var = NULL; | |
3725 | if (this->expr_->var_expression() != NULL) | |
3726 | var = this->expr_->var_expression()->named_object(); | |
3727 | else if (this->expr_->enclosed_var_expression() != NULL) | |
3728 | var = this->expr_->enclosed_var_expression()->variable(); | |
3729 | ||
3730 | if (this->escapes_ && var != NULL) | |
3731 | { | |
3732 | if (var->is_variable()) | |
3733 | this->escapes_ = var->var_value()->escapes(); | |
3734 | if (var->is_result_variable()) | |
3735 | this->escapes_ = var->result_var_value()->escapes(); | |
3736 | } | |
3737 | ||
3738 | this->expr_->address_taken(this->escapes_); | |
3739 | } | |
3740 | ||
e440a328 | 3741 | // If we are taking the address of a composite literal, and the |
2c809f8f | 3742 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3743 | // instead. |
3744 | ||
3745 | Expression* | |
ceeb4318 | 3746 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3747 | { |
b13c66cd | 3748 | Location loc = this->location(); |
e440a328 | 3749 | Operator op = this->op_; |
3750 | Expression* expr = this->expr_; | |
3751 | ||
3752 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3753 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3754 | ||
3755 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3756 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3757 | // analysis here? This case is found in math/unsafe.go and is | |
3758 | // therefore worth special casing. | |
3759 | if (op == OPERATOR_MULT) | |
3760 | { | |
3761 | Expression* e = expr; | |
3762 | while (e->classification() == EXPRESSION_CONVERSION) | |
3763 | { | |
3764 | Type_conversion_expression* te | |
3765 | = static_cast<Type_conversion_expression*>(e); | |
3766 | e = te->expr(); | |
3767 | } | |
3768 | ||
3769 | if (e->classification() == EXPRESSION_UNARY) | |
3770 | { | |
3771 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3772 | if (ue->op_ == OPERATOR_AND) | |
3773 | { | |
3774 | if (e == expr) | |
3775 | { | |
3776 | // *&x == x. | |
f4dea966 | 3777 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3778 | { | |
631d5788 | 3779 | go_error_at(ue->location(), |
3780 | "invalid operand for unary %<&%>"); | |
f4dea966 | 3781 | this->set_is_error(); |
3782 | } | |
e440a328 | 3783 | return ue->expr_; |
3784 | } | |
3785 | ue->set_does_not_escape(); | |
3786 | } | |
3787 | } | |
3788 | } | |
3789 | ||
55661ce9 | 3790 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3791 | // having to deal with TYPE_VOID in other places. | |
3792 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3793 | { | |
631d5788 | 3794 | go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); |
55661ce9 | 3795 | return Expression::make_error(this->location()); |
3796 | } | |
3797 | ||
d9f3743a | 3798 | // Check for an invalid pointer dereference. We need to do this |
3799 | // here because Unary_expression::do_type will return an error type | |
3800 | // in this case. That can cause code to appear erroneous, and | |
3801 | // therefore disappear at lowering time, without any error message. | |
3802 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3803 | { | |
3804 | this->report_error(_("expected pointer")); | |
3805 | return Expression::make_error(this->location()); | |
3806 | } | |
3807 | ||
59a401fe | 3808 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3809 | { |
0c77715b | 3810 | Numeric_constant nc; |
3811 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3812 | { |
0c77715b | 3813 | Numeric_constant result; |
af7a5274 | 3814 | bool issued_error; |
3815 | if (Unary_expression::eval_constant(op, &nc, loc, &result, | |
3816 | &issued_error)) | |
0c77715b | 3817 | return result.expression(loc); |
af7a5274 | 3818 | else if (issued_error) |
3819 | return Expression::make_error(this->location()); | |
e440a328 | 3820 | } |
3821 | } | |
3822 | ||
3823 | return this; | |
3824 | } | |
3825 | ||
f9ca30f9 | 3826 | // Flatten expression if a nil check must be performed and create temporary |
3827 | // variables if necessary. | |
3828 | ||
3829 | Expression* | |
3830 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3831 | Statement_inserter* inserter) | |
3832 | { | |
5bf8be8b | 3833 | if (this->is_error_expression() |
3834 | || this->expr_->is_error_expression() | |
3835 | || this->expr_->type()->is_error_type()) | |
3836 | { | |
3837 | go_assert(saw_errors()); | |
3838 | return Expression::make_error(this->location()); | |
3839 | } | |
f4dea966 | 3840 | |
f9ca30f9 | 3841 | Location location = this->location(); |
3842 | if (this->op_ == OPERATOR_MULT | |
3843 | && !this->expr_->is_variable()) | |
3844 | { | |
3845 | go_assert(this->expr_->type()->points_to() != NULL); | |
f614ea8b | 3846 | switch (this->requires_nil_check(gogo)) |
f9ca30f9 | 3847 | { |
f614ea8b | 3848 | case NIL_CHECK_ERROR_ENCOUNTERED: |
2a305b85 | 3849 | { |
3850 | go_assert(saw_errors()); | |
3851 | return Expression::make_error(this->location()); | |
3852 | } | |
f614ea8b | 3853 | case NIL_CHECK_NOT_NEEDED: |
3854 | break; | |
3855 | case NIL_CHECK_NEEDED: | |
3856 | this->create_temp_ = true; | |
3857 | break; | |
3858 | case NIL_CHECK_DEFAULT: | |
3859 | go_unreachable(); | |
f9ca30f9 | 3860 | } |
3861 | } | |
3862 | ||
3863 | if (this->create_temp_ && !this->expr_->is_variable()) | |
3864 | { | |
3865 | Temporary_statement* temp = | |
3866 | Statement::make_temporary(NULL, this->expr_, location); | |
3867 | inserter->insert(temp); | |
3868 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3869 | } | |
3870 | ||
3871 | return this; | |
3872 | } | |
3873 | ||
e440a328 | 3874 | // Return whether a unary expression is a constant. |
3875 | ||
3876 | bool | |
3877 | Unary_expression::do_is_constant() const | |
3878 | { | |
3879 | if (this->op_ == OPERATOR_MULT) | |
3880 | { | |
3881 | // Indirecting through a pointer is only constant if the object | |
3882 | // to which the expression points is constant, but we currently | |
3883 | // have no way to determine that. | |
3884 | return false; | |
3885 | } | |
3886 | else if (this->op_ == OPERATOR_AND) | |
3887 | { | |
3888 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3889 | // global variable, not constant otherwise. In other cases taking the |
3890 | // address is probably not a constant. | |
e440a328 | 3891 | Var_expression* ve = this->expr_->var_expression(); |
3892 | if (ve != NULL) | |
3893 | { | |
3894 | Named_object* no = ve->named_object(); | |
3895 | return no->is_variable() && no->var_value()->is_global(); | |
3896 | } | |
3897 | return false; | |
3898 | } | |
3899 | else | |
3900 | return this->expr_->is_constant(); | |
3901 | } | |
3902 | ||
3ae06f68 | 3903 | // Return whether a unary expression can be used as a constant |
3904 | // initializer. | |
3905 | ||
3906 | bool | |
3907 | Unary_expression::do_is_static_initializer() const | |
3908 | { | |
3909 | if (this->op_ == OPERATOR_MULT) | |
3910 | return false; | |
3911 | else if (this->op_ == OPERATOR_AND) | |
de048538 | 3912 | return Unary_expression::base_is_static_initializer(this->expr_); |
3913 | else | |
3914 | return this->expr_->is_static_initializer(); | |
3915 | } | |
3ae06f68 | 3916 | |
de048538 | 3917 | // Return whether the address of EXPR can be used as a static |
3918 | // initializer. | |
3ae06f68 | 3919 | |
de048538 | 3920 | bool |
3921 | Unary_expression::base_is_static_initializer(Expression* expr) | |
3922 | { | |
3923 | // The address of a field reference can be a static initializer if | |
3924 | // the base can be a static initializer. | |
3925 | Field_reference_expression* fre = expr->field_reference_expression(); | |
3926 | if (fre != NULL) | |
3927 | return Unary_expression::base_is_static_initializer(fre->expr()); | |
3928 | ||
3929 | // The address of an index expression can be a static initializer if | |
3930 | // the base can be a static initializer and the index is constant. | |
3931 | Array_index_expression* aind = expr->array_index_expression(); | |
3932 | if (aind != NULL) | |
3933 | return (aind->end() == NULL | |
3934 | && aind->start()->is_constant() | |
3935 | && Unary_expression::base_is_static_initializer(aind->array())); | |
3936 | ||
3937 | // The address of a global variable can be a static initializer. | |
3938 | Var_expression* ve = expr->var_expression(); | |
3939 | if (ve != NULL) | |
3940 | { | |
3941 | Named_object* no = ve->named_object(); | |
3942 | return no->is_variable() && no->var_value()->is_global(); | |
3943 | } | |
3944 | ||
3945 | // The address of a composite literal can be used as a static | |
3946 | // initializer if the composite literal is itself usable as a | |
3947 | // static initializer. | |
3948 | if (expr->is_composite_literal() && expr->is_static_initializer()) | |
3949 | return true; | |
3ae06f68 | 3950 | |
de048538 | 3951 | // The address of a string constant can be used as a static |
3952 | // initializer. This can not be written in Go itself but this is | |
3953 | // used when building a type descriptor. | |
3954 | if (expr->string_expression() != NULL) | |
3955 | return true; | |
3956 | ||
3957 | return false; | |
3ae06f68 | 3958 | } |
3959 | ||
f614ea8b | 3960 | // Return whether this dereference expression requires an explicit nil |
3961 | // check. If we are dereferencing the pointer to a large struct | |
3962 | // (greater than the specified size threshold), we need to check for | |
3963 | // nil. We don't bother to check for small structs because we expect | |
3964 | // the system to crash on a nil pointer dereference. However, if we | |
3965 | // know the address of this expression is being taken, we must always | |
3966 | // check for nil. | |
3967 | Unary_expression::Nil_check_classification | |
3968 | Unary_expression::requires_nil_check(Gogo* gogo) | |
3969 | { | |
3970 | go_assert(this->op_ == OPERATOR_MULT); | |
3971 | go_assert(this->expr_->type()->points_to() != NULL); | |
3972 | ||
3973 | if (this->issue_nil_check_ == NIL_CHECK_NEEDED) | |
3974 | return NIL_CHECK_NEEDED; | |
3975 | else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED) | |
3976 | return NIL_CHECK_NOT_NEEDED; | |
3977 | ||
3978 | Type* ptype = this->expr_->type()->points_to(); | |
3979 | int64_t type_size = -1; | |
3980 | if (!ptype->is_void_type()) | |
3981 | { | |
3982 | bool ok = ptype->backend_type_size(gogo, &type_size); | |
3983 | if (!ok) | |
3984 | return NIL_CHECK_ERROR_ENCOUNTERED; | |
3985 | } | |
3986 | ||
3987 | int64_t size_cutoff = gogo->nil_check_size_threshold(); | |
3988 | if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff)) | |
3989 | this->issue_nil_check_ = NIL_CHECK_NEEDED; | |
3990 | else | |
3991 | this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED; | |
3992 | return this->issue_nil_check_; | |
3993 | } | |
3994 | ||
0c77715b | 3995 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
af7a5274 | 3996 | // could be done, false if not. On overflow, issues an error and sets |
3997 | // *ISSUED_ERROR. | |
e440a328 | 3998 | |
3999 | bool | |
0c77715b | 4000 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
af7a5274 | 4001 | Location location, Numeric_constant* nc, |
4002 | bool* issued_error) | |
e440a328 | 4003 | { |
af7a5274 | 4004 | *issued_error = false; |
e440a328 | 4005 | switch (op) |
4006 | { | |
4007 | case OPERATOR_PLUS: | |
0c77715b | 4008 | *nc = *unc; |
e440a328 | 4009 | return true; |
0c77715b | 4010 | |
e440a328 | 4011 | case OPERATOR_MINUS: |
0c77715b | 4012 | if (unc->is_int() || unc->is_rune()) |
4013 | break; | |
4014 | else if (unc->is_float()) | |
4015 | { | |
4016 | mpfr_t uval; | |
4017 | unc->get_float(&uval); | |
4018 | mpfr_t val; | |
4019 | mpfr_init(val); | |
4020 | mpfr_neg(val, uval, GMP_RNDN); | |
4021 | nc->set_float(unc->type(), val); | |
4022 | mpfr_clear(uval); | |
4023 | mpfr_clear(val); | |
4024 | return true; | |
4025 | } | |
4026 | else if (unc->is_complex()) | |
4027 | { | |
fcbea5e4 | 4028 | mpc_t uval; |
4029 | unc->get_complex(&uval); | |
4030 | mpc_t val; | |
4031 | mpc_init2(val, mpc_precision); | |
4032 | mpc_neg(val, uval, MPC_RNDNN); | |
4033 | nc->set_complex(unc->type(), val); | |
4034 | mpc_clear(uval); | |
4035 | mpc_clear(val); | |
0c77715b | 4036 | return true; |
4037 | } | |
e440a328 | 4038 | else |
0c77715b | 4039 | go_unreachable(); |
e440a328 | 4040 | |
0c77715b | 4041 | case OPERATOR_XOR: |
4042 | break; | |
68448d53 | 4043 | |
59a401fe | 4044 | case OPERATOR_NOT: |
e440a328 | 4045 | case OPERATOR_AND: |
4046 | case OPERATOR_MULT: | |
4047 | return false; | |
0c77715b | 4048 | |
e440a328 | 4049 | default: |
c3e6f413 | 4050 | go_unreachable(); |
e440a328 | 4051 | } |
e440a328 | 4052 | |
0c77715b | 4053 | if (!unc->is_int() && !unc->is_rune()) |
4054 | return false; | |
4055 | ||
4056 | mpz_t uval; | |
8387e1df | 4057 | if (unc->is_rune()) |
4058 | unc->get_rune(&uval); | |
4059 | else | |
4060 | unc->get_int(&uval); | |
0c77715b | 4061 | mpz_t val; |
4062 | mpz_init(val); | |
e440a328 | 4063 | |
e440a328 | 4064 | switch (op) |
4065 | { | |
e440a328 | 4066 | case OPERATOR_MINUS: |
0c77715b | 4067 | mpz_neg(val, uval); |
4068 | break; | |
4069 | ||
e440a328 | 4070 | case OPERATOR_NOT: |
0c77715b | 4071 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
4072 | break; | |
4073 | ||
e440a328 | 4074 | case OPERATOR_XOR: |
0c77715b | 4075 | { |
4076 | Type* utype = unc->type(); | |
4077 | if (utype->integer_type() == NULL | |
4078 | || utype->integer_type()->is_abstract()) | |
4079 | mpz_com(val, uval); | |
4080 | else | |
4081 | { | |
4082 | // The number of HOST_WIDE_INTs that it takes to represent | |
4083 | // UVAL. | |
4084 | size_t count = ((mpz_sizeinbase(uval, 2) | |
4085 | + HOST_BITS_PER_WIDE_INT | |
4086 | - 1) | |
4087 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 4088 | |
0c77715b | 4089 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
4090 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
4091 | ||
4092 | size_t obits = utype->integer_type()->bits(); | |
4093 | ||
4094 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
4095 | { | |
4096 | mpz_t adj; | |
4097 | mpz_init_set_ui(adj, 1); | |
4098 | mpz_mul_2exp(adj, adj, obits); | |
4099 | mpz_add(uval, uval, adj); | |
4100 | mpz_clear(adj); | |
4101 | } | |
4102 | ||
4103 | size_t ecount; | |
4104 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
4105 | go_assert(ecount <= count); | |
4106 | ||
4107 | // Trim down to the number of words required by the type. | |
4108 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
4109 | / HOST_BITS_PER_WIDE_INT); | |
4110 | go_assert(ocount <= count); | |
4111 | ||
4112 | for (size_t i = 0; i < ocount; ++i) | |
4113 | phwi[i] = ~phwi[i]; | |
4114 | ||
4115 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
4116 | if (clearbits != 0) | |
4117 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
4118 | >> clearbits); | |
4119 | ||
4120 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
4121 | ||
4122 | if (!utype->integer_type()->is_unsigned() | |
4123 | && mpz_tstbit(val, obits - 1)) | |
4124 | { | |
4125 | mpz_t adj; | |
4126 | mpz_init_set_ui(adj, 1); | |
4127 | mpz_mul_2exp(adj, adj, obits); | |
4128 | mpz_sub(val, val, adj); | |
4129 | mpz_clear(adj); | |
4130 | } | |
4131 | ||
4132 | delete[] phwi; | |
4133 | } | |
4134 | } | |
4135 | break; | |
e440a328 | 4136 | |
e440a328 | 4137 | default: |
c3e6f413 | 4138 | go_unreachable(); |
e440a328 | 4139 | } |
e440a328 | 4140 | |
0c77715b | 4141 | if (unc->is_rune()) |
4142 | nc->set_rune(NULL, val); | |
e440a328 | 4143 | else |
0c77715b | 4144 | nc->set_int(NULL, val); |
e440a328 | 4145 | |
0c77715b | 4146 | mpz_clear(uval); |
4147 | mpz_clear(val); | |
e440a328 | 4148 | |
af7a5274 | 4149 | if (!nc->set_type(unc->type(), true, location)) |
4150 | { | |
4151 | *issued_error = true; | |
4152 | return false; | |
4153 | } | |
4154 | return true; | |
e440a328 | 4155 | } |
4156 | ||
0c77715b | 4157 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 4158 | |
4159 | bool | |
0c77715b | 4160 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 4161 | { |
0c77715b | 4162 | Numeric_constant unc; |
4163 | if (!this->expr_->numeric_constant_value(&unc)) | |
4164 | return false; | |
af7a5274 | 4165 | bool issued_error; |
0c77715b | 4166 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), |
af7a5274 | 4167 | nc, &issued_error); |
e440a328 | 4168 | } |
4169 | ||
4170 | // Return the type of a unary expression. | |
4171 | ||
4172 | Type* | |
4173 | Unary_expression::do_type() | |
4174 | { | |
4175 | switch (this->op_) | |
4176 | { | |
4177 | case OPERATOR_PLUS: | |
4178 | case OPERATOR_MINUS: | |
4179 | case OPERATOR_NOT: | |
4180 | case OPERATOR_XOR: | |
4181 | return this->expr_->type(); | |
4182 | ||
4183 | case OPERATOR_AND: | |
4184 | return Type::make_pointer_type(this->expr_->type()); | |
4185 | ||
4186 | case OPERATOR_MULT: | |
4187 | { | |
4188 | Type* subtype = this->expr_->type(); | |
4189 | Type* points_to = subtype->points_to(); | |
4190 | if (points_to == NULL) | |
4191 | return Type::make_error_type(); | |
4192 | return points_to; | |
4193 | } | |
4194 | ||
4195 | default: | |
c3e6f413 | 4196 | go_unreachable(); |
e440a328 | 4197 | } |
4198 | } | |
4199 | ||
4200 | // Determine abstract types for a unary expression. | |
4201 | ||
4202 | void | |
4203 | Unary_expression::do_determine_type(const Type_context* context) | |
4204 | { | |
4205 | switch (this->op_) | |
4206 | { | |
4207 | case OPERATOR_PLUS: | |
4208 | case OPERATOR_MINUS: | |
4209 | case OPERATOR_NOT: | |
4210 | case OPERATOR_XOR: | |
4211 | this->expr_->determine_type(context); | |
4212 | break; | |
4213 | ||
4214 | case OPERATOR_AND: | |
4215 | // Taking the address of something. | |
4216 | { | |
4217 | Type* subtype = (context->type == NULL | |
4218 | ? NULL | |
4219 | : context->type->points_to()); | |
4220 | Type_context subcontext(subtype, false); | |
4221 | this->expr_->determine_type(&subcontext); | |
4222 | } | |
4223 | break; | |
4224 | ||
4225 | case OPERATOR_MULT: | |
4226 | // Indirecting through a pointer. | |
4227 | { | |
4228 | Type* subtype = (context->type == NULL | |
4229 | ? NULL | |
4230 | : Type::make_pointer_type(context->type)); | |
4231 | Type_context subcontext(subtype, false); | |
4232 | this->expr_->determine_type(&subcontext); | |
4233 | } | |
4234 | break; | |
4235 | ||
4236 | default: | |
c3e6f413 | 4237 | go_unreachable(); |
e440a328 | 4238 | } |
4239 | } | |
4240 | ||
4241 | // Check types for a unary expression. | |
4242 | ||
4243 | void | |
4244 | Unary_expression::do_check_types(Gogo*) | |
4245 | { | |
9fe897ef | 4246 | Type* type = this->expr_->type(); |
5c13bd80 | 4247 | if (type->is_error()) |
9fe897ef | 4248 | { |
4249 | this->set_is_error(); | |
4250 | return; | |
4251 | } | |
4252 | ||
e440a328 | 4253 | switch (this->op_) |
4254 | { | |
4255 | case OPERATOR_PLUS: | |
4256 | case OPERATOR_MINUS: | |
9fe897ef | 4257 | if (type->integer_type() == NULL |
4258 | && type->float_type() == NULL | |
4259 | && type->complex_type() == NULL) | |
4260 | this->report_error(_("expected numeric type")); | |
e440a328 | 4261 | break; |
4262 | ||
4263 | case OPERATOR_NOT: | |
59a401fe | 4264 | if (!type->is_boolean_type()) |
4265 | this->report_error(_("expected boolean type")); | |
4266 | break; | |
4267 | ||
e440a328 | 4268 | case OPERATOR_XOR: |
b3b1474e | 4269 | if (type->integer_type() == NULL) |
4270 | this->report_error(_("expected integer")); | |
e440a328 | 4271 | break; |
4272 | ||
4273 | case OPERATOR_AND: | |
4274 | if (!this->expr_->is_addressable()) | |
09ea332d | 4275 | { |
4276 | if (!this->create_temp_) | |
f4dea966 | 4277 | { |
631d5788 | 4278 | go_error_at(this->location(), "invalid operand for unary %<&%>"); |
f4dea966 | 4279 | this->set_is_error(); |
4280 | } | |
09ea332d | 4281 | } |
e440a328 | 4282 | else |
da244e59 | 4283 | this->expr_->issue_nil_check(); |
e440a328 | 4284 | break; |
4285 | ||
4286 | case OPERATOR_MULT: | |
4287 | // Indirecting through a pointer. | |
9fe897ef | 4288 | if (type->points_to() == NULL) |
4289 | this->report_error(_("expected pointer")); | |
7661d702 | 4290 | if (type->points_to()->is_error()) |
4291 | this->set_is_error(); | |
e440a328 | 4292 | break; |
4293 | ||
4294 | default: | |
c3e6f413 | 4295 | go_unreachable(); |
e440a328 | 4296 | } |
4297 | } | |
4298 | ||
ea664253 | 4299 | // Get the backend representation for a unary expression. |
e440a328 | 4300 | |
ea664253 | 4301 | Bexpression* |
4302 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4303 | { |
1b1f2abf | 4304 | Gogo* gogo = context->gogo(); |
e9d3367e | 4305 | Location loc = this->location(); |
4306 | ||
4307 | // Taking the address of a set-and-use-temporary expression requires | |
4308 | // setting the temporary and then taking the address. | |
4309 | if (this->op_ == OPERATOR_AND) | |
4310 | { | |
4311 | Set_and_use_temporary_expression* sut = | |
4312 | this->expr_->set_and_use_temporary_expression(); | |
4313 | if (sut != NULL) | |
4314 | { | |
4315 | Temporary_statement* temp = sut->temporary(); | |
4316 | Bvariable* bvar = temp->get_backend_variable(context); | |
d4e6573e | 4317 | Bexpression* bvar_expr = |
7af8e400 | 4318 | gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4319 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4320 | |
0ab48656 | 4321 | Named_object* fn = context->function(); |
4322 | go_assert(fn != NULL); | |
4323 | Bfunction* bfn = | |
4324 | fn->func_value()->get_or_make_decl(gogo, fn); | |
f9ca30f9 | 4325 | Bstatement* bassign = |
0ab48656 | 4326 | gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc); |
f9ca30f9 | 4327 | Bexpression* bvar_addr = |
4328 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4329 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4330 | } |
4331 | } | |
4332 | ||
f9ca30f9 | 4333 | Bexpression* ret; |
ea664253 | 4334 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4335 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4336 | switch (this->op_) |
4337 | { | |
4338 | case OPERATOR_PLUS: | |
f9ca30f9 | 4339 | ret = bexpr; |
4340 | break; | |
e440a328 | 4341 | |
4342 | case OPERATOR_MINUS: | |
f9ca30f9 | 4343 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4344 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4345 | break; | |
e440a328 | 4346 | |
4347 | case OPERATOR_NOT: | |
e440a328 | 4348 | case OPERATOR_XOR: |
f9ca30f9 | 4349 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4350 | break; | |
e440a328 | 4351 | |
4352 | case OPERATOR_AND: | |
09ea332d | 4353 | if (!this->create_temp_) |
4354 | { | |
4355 | // We should not see a non-constant constructor here; cases | |
4356 | // where we would see one should have been moved onto the | |
4357 | // heap at parse time. Taking the address of a nonconstant | |
4358 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4359 | |
4360 | go_assert(!this->expr_->is_composite_literal() | |
3ae06f68 | 4361 | || this->expr_->is_static_initializer()); |
24060bf9 | 4362 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4363 | { | |
4364 | Unary_expression* ue = | |
4365 | static_cast<Unary_expression*>(this->expr_); | |
4366 | go_assert(ue->op() != OPERATOR_AND); | |
4367 | } | |
09ea332d | 4368 | } |
e440a328 | 4369 | |
f23d7786 | 4370 | if (this->is_gc_root_ || this->is_slice_init_) |
76f85fd6 | 4371 | { |
19272321 | 4372 | std::string var_name; |
f23d7786 | 4373 | bool copy_to_heap = false; |
4374 | if (this->is_gc_root_) | |
4375 | { | |
4376 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4377 | // they cannot be represented as an immutable_struct in the | |
4378 | // backend. | |
19272321 | 4379 | var_name = gogo->gc_root_name(); |
f23d7786 | 4380 | } |
4381 | else | |
4382 | { | |
4383 | // Build a decl for a slice value initializer. An immutable slice | |
4384 | // value initializer may have to be copied to the heap if it | |
4385 | // contains pointers in a non-constant context. | |
19272321 | 4386 | var_name = gogo->initializer_name(); |
f23d7786 | 4387 | |
4388 | Array_type* at = this->expr_->type()->array_type(); | |
4389 | go_assert(at != NULL); | |
4390 | ||
4391 | // If we are not copying the value to the heap, we will only | |
4392 | // initialize the value once, so we can use this directly | |
4393 | // rather than copying it. In that case we can't make it | |
4394 | // read-only, because the program is permitted to change it. | |
3ae06f68 | 4395 | copy_to_heap = context->function() != NULL; |
f23d7786 | 4396 | } |
19272321 | 4397 | std::string asm_name(go_selectively_encode_id(var_name)); |
f23d7786 | 4398 | Bvariable* implicit = |
19272321 | 4399 | gogo->backend()->implicit_variable(var_name, asm_name, |
438b4bec | 4400 | btype, true, copy_to_heap, |
4401 | false, 0); | |
19272321 | 4402 | gogo->backend()->implicit_variable_set_init(implicit, var_name, btype, |
aa5ae575 | 4403 | true, copy_to_heap, false, |
4404 | bexpr); | |
7af8e400 | 4405 | bexpr = gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4406 | |
4407 | // If we are not copying a slice initializer to the heap, | |
4408 | // then it can be changed by the program, so if it can | |
4409 | // contain pointers we must register it as a GC root. | |
4410 | if (this->is_slice_init_ | |
4411 | && !copy_to_heap | |
4412 | && this->expr_->type()->has_pointer()) | |
4413 | { | |
4414 | Bexpression* root = | |
7af8e400 | 4415 | gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4416 | root = gogo->backend()->address_expression(root, loc); |
4417 | Type* type = Type::make_pointer_type(this->expr_->type()); | |
4418 | gogo->add_gc_root(Expression::make_backend(root, type, loc)); | |
4419 | } | |
76f85fd6 | 4420 | } |
4421 | else if ((this->expr_->is_composite_literal() | |
3ae06f68 | 4422 | || this->expr_->string_expression() != NULL) |
4423 | && this->expr_->is_static_initializer()) | |
f9ca30f9 | 4424 | { |
19272321 | 4425 | std::string var_name(gogo->initializer_name()); |
4426 | std::string asm_name(go_selectively_encode_id(var_name)); | |
f9ca30f9 | 4427 | Bvariable* decl = |
19272321 | 4428 | gogo->backend()->immutable_struct(var_name, asm_name, |
438b4bec | 4429 | true, false, btype, loc); |
19272321 | 4430 | gogo->backend()->immutable_struct_set_init(decl, var_name, true, |
4431 | false, btype, loc, bexpr); | |
7af8e400 | 4432 | bexpr = gogo->backend()->var_expression(decl, loc); |
f9ca30f9 | 4433 | } |
09ea332d | 4434 | |
f9ca30f9 | 4435 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4436 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4437 | break; | |
e440a328 | 4438 | |
4439 | case OPERATOR_MULT: | |
4440 | { | |
f9ca30f9 | 4441 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4442 | |
f614ea8b | 4443 | bool known_valid = false; |
f9ca30f9 | 4444 | Type* ptype = this->expr_->type()->points_to(); |
4445 | Btype* pbtype = ptype->get_backend(gogo); | |
f614ea8b | 4446 | switch (this->requires_nil_check(gogo)) |
4447 | { | |
4448 | case NIL_CHECK_NOT_NEEDED: | |
4449 | break; | |
4450 | case NIL_CHECK_ERROR_ENCOUNTERED: | |
2a305b85 | 4451 | { |
4452 | go_assert(saw_errors()); | |
4453 | return gogo->backend()->error_expression(); | |
4454 | } | |
f614ea8b | 4455 | case NIL_CHECK_NEEDED: |
4456 | { | |
f9ca30f9 | 4457 | go_assert(this->expr_->is_variable()); |
2dd89704 | 4458 | |
4459 | // If we're nil-checking the result of a set-and-use-temporary | |
4460 | // expression, then pick out the target temp and use that | |
4461 | // for the final result of the conditional. | |
4462 | Bexpression* tbexpr = bexpr; | |
4463 | Bexpression* ubexpr = bexpr; | |
4464 | Set_and_use_temporary_expression* sut = | |
4465 | this->expr_->set_and_use_temporary_expression(); | |
4466 | if (sut != NULL) { | |
4467 | Temporary_statement* temp = sut->temporary(); | |
4468 | Bvariable* bvar = temp->get_backend_variable(context); | |
4469 | ubexpr = gogo->backend()->var_expression(bvar, loc); | |
4470 | } | |
ea664253 | 4471 | Bexpression* nil = |
f614ea8b | 4472 | Expression::make_nil(loc)->get_backend(context); |
f9ca30f9 | 4473 | Bexpression* compare = |
2dd89704 | 4474 | gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr, |
f9ca30f9 | 4475 | nil, loc); |
f9ca30f9 | 4476 | Bexpression* crash = |
f614ea8b | 4477 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4478 | loc)->get_backend(context); | |
93715b75 | 4479 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
4480 | bexpr = gogo->backend()->conditional_expression(bfn, btype, | |
4481 | compare, | |
2dd89704 | 4482 | crash, ubexpr, |
f9ca30f9 | 4483 | loc); |
f614ea8b | 4484 | known_valid = true; |
4485 | break; | |
4486 | } | |
4487 | case NIL_CHECK_DEFAULT: | |
4488 | go_unreachable(); | |
4489 | } | |
4490 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, | |
4491 | known_valid, loc); | |
e440a328 | 4492 | } |
f9ca30f9 | 4493 | break; |
e440a328 | 4494 | |
4495 | default: | |
c3e6f413 | 4496 | go_unreachable(); |
e440a328 | 4497 | } |
f9ca30f9 | 4498 | |
ea664253 | 4499 | return ret; |
e440a328 | 4500 | } |
4501 | ||
4502 | // Export a unary expression. | |
4503 | ||
4504 | void | |
4505 | Unary_expression::do_export(Export* exp) const | |
4506 | { | |
4507 | switch (this->op_) | |
4508 | { | |
4509 | case OPERATOR_PLUS: | |
4510 | exp->write_c_string("+ "); | |
4511 | break; | |
4512 | case OPERATOR_MINUS: | |
4513 | exp->write_c_string("- "); | |
4514 | break; | |
4515 | case OPERATOR_NOT: | |
4516 | exp->write_c_string("! "); | |
4517 | break; | |
4518 | case OPERATOR_XOR: | |
4519 | exp->write_c_string("^ "); | |
4520 | break; | |
4521 | case OPERATOR_AND: | |
4522 | case OPERATOR_MULT: | |
4523 | default: | |
c3e6f413 | 4524 | go_unreachable(); |
e440a328 | 4525 | } |
4526 | this->expr_->export_expression(exp); | |
4527 | } | |
4528 | ||
4529 | // Import a unary expression. | |
4530 | ||
4531 | Expression* | |
4532 | Unary_expression::do_import(Import* imp) | |
4533 | { | |
4534 | Operator op; | |
4535 | switch (imp->get_char()) | |
4536 | { | |
4537 | case '+': | |
4538 | op = OPERATOR_PLUS; | |
4539 | break; | |
4540 | case '-': | |
4541 | op = OPERATOR_MINUS; | |
4542 | break; | |
4543 | case '!': | |
4544 | op = OPERATOR_NOT; | |
4545 | break; | |
4546 | case '^': | |
4547 | op = OPERATOR_XOR; | |
4548 | break; | |
4549 | default: | |
c3e6f413 | 4550 | go_unreachable(); |
e440a328 | 4551 | } |
4552 | imp->require_c_string(" "); | |
4553 | Expression* expr = Expression::import_expression(imp); | |
4554 | return Expression::make_unary(op, expr, imp->location()); | |
4555 | } | |
4556 | ||
d751bb78 | 4557 | // Dump ast representation of an unary expression. |
4558 | ||
4559 | void | |
4560 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4561 | { | |
4562 | ast_dump_context->dump_operator(this->op_); | |
4563 | ast_dump_context->ostream() << "("; | |
4564 | ast_dump_context->dump_expression(this->expr_); | |
4565 | ast_dump_context->ostream() << ") "; | |
4566 | } | |
4567 | ||
e440a328 | 4568 | // Make a unary expression. |
4569 | ||
4570 | Expression* | |
b13c66cd | 4571 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4572 | { |
4573 | return new Unary_expression(op, expr, location); | |
4574 | } | |
4575 | ||
f614ea8b | 4576 | Expression* |
4577 | Expression::make_dereference(Expression* ptr, | |
4578 | Nil_check_classification docheck, | |
4579 | Location location) | |
4580 | { | |
4581 | Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location); | |
4582 | if (docheck == NIL_CHECK_NEEDED) | |
4583 | deref->unary_expression()->set_requires_nil_check(true); | |
4584 | else if (docheck == NIL_CHECK_NOT_NEEDED) | |
4585 | deref->unary_expression()->set_requires_nil_check(false); | |
4586 | return deref; | |
4587 | } | |
4588 | ||
e440a328 | 4589 | // If this is an indirection through a pointer, return the expression |
4590 | // being pointed through. Otherwise return this. | |
4591 | ||
4592 | Expression* | |
4593 | Expression::deref() | |
4594 | { | |
4595 | if (this->classification_ == EXPRESSION_UNARY) | |
4596 | { | |
4597 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4598 | if (ue->op() == OPERATOR_MULT) | |
4599 | return ue->operand(); | |
4600 | } | |
4601 | return this; | |
4602 | } | |
4603 | ||
4604 | // Class Binary_expression. | |
4605 | ||
4606 | // Traversal. | |
4607 | ||
4608 | int | |
4609 | Binary_expression::do_traverse(Traverse* traverse) | |
4610 | { | |
4611 | int t = Expression::traverse(&this->left_, traverse); | |
4612 | if (t == TRAVERSE_EXIT) | |
4613 | return TRAVERSE_EXIT; | |
4614 | return Expression::traverse(&this->right_, traverse); | |
4615 | } | |
4616 | ||
3ae06f68 | 4617 | // Return whether this expression may be used as a static initializer. |
4618 | ||
4619 | bool | |
4620 | Binary_expression::do_is_static_initializer() const | |
4621 | { | |
4622 | if (!this->left_->is_static_initializer() | |
4623 | || !this->right_->is_static_initializer()) | |
4624 | return false; | |
4625 | ||
4626 | // Addresses can be static initializers, but we can't implement | |
4627 | // arbitray binary expressions of them. | |
4628 | Unary_expression* lu = this->left_->unary_expression(); | |
4629 | Unary_expression* ru = this->right_->unary_expression(); | |
4630 | if (lu != NULL && lu->op() == OPERATOR_AND) | |
4631 | { | |
4632 | if (ru != NULL && ru->op() == OPERATOR_AND) | |
4633 | return this->op_ == OPERATOR_MINUS; | |
4634 | else | |
4635 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4636 | } | |
4637 | else if (ru != NULL && ru->op() == OPERATOR_AND) | |
4638 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4639 | ||
4640 | // Other cases should resolve in the backend. | |
4641 | return true; | |
4642 | } | |
4643 | ||
0c77715b | 4644 | // Return the type to use for a binary operation on operands of |
4645 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4646 | // such may be NULL or abstract. | |
4647 | ||
4648 | bool | |
4649 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4650 | Type* right_type, Type** result_type) | |
4651 | { | |
4652 | if (left_type != right_type | |
4653 | && !left_type->is_abstract() | |
4654 | && !right_type->is_abstract() | |
4655 | && left_type->base() != right_type->base() | |
4656 | && op != OPERATOR_LSHIFT | |
4657 | && op != OPERATOR_RSHIFT) | |
4658 | { | |
4659 | // May be a type error--let it be diagnosed elsewhere. | |
4660 | return false; | |
4661 | } | |
4662 | ||
4663 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4664 | { | |
4665 | if (left_type->integer_type() != NULL) | |
4666 | *result_type = left_type; | |
4667 | else | |
4668 | *result_type = Type::make_abstract_integer_type(); | |
4669 | } | |
4670 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4671 | *result_type = left_type; | |
4672 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4673 | *result_type = right_type; | |
4674 | else if (!left_type->is_abstract()) | |
4675 | *result_type = left_type; | |
4676 | else if (!right_type->is_abstract()) | |
4677 | *result_type = right_type; | |
4678 | else if (left_type->complex_type() != NULL) | |
4679 | *result_type = left_type; | |
4680 | else if (right_type->complex_type() != NULL) | |
4681 | *result_type = right_type; | |
4682 | else if (left_type->float_type() != NULL) | |
4683 | *result_type = left_type; | |
4684 | else if (right_type->float_type() != NULL) | |
4685 | *result_type = right_type; | |
4686 | else if (left_type->integer_type() != NULL | |
4687 | && left_type->integer_type()->is_rune()) | |
4688 | *result_type = left_type; | |
4689 | else if (right_type->integer_type() != NULL | |
4690 | && right_type->integer_type()->is_rune()) | |
4691 | *result_type = right_type; | |
4692 | else | |
4693 | *result_type = left_type; | |
4694 | ||
4695 | return true; | |
4696 | } | |
4697 | ||
4698 | // Convert an integer comparison code and an operator to a boolean | |
4699 | // value. | |
e440a328 | 4700 | |
4701 | bool | |
0c77715b | 4702 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4703 | { |
e440a328 | 4704 | switch (op) |
4705 | { | |
4706 | case OPERATOR_EQEQ: | |
0c77715b | 4707 | return cmp == 0; |
4708 | break; | |
e440a328 | 4709 | case OPERATOR_NOTEQ: |
0c77715b | 4710 | return cmp != 0; |
4711 | break; | |
e440a328 | 4712 | case OPERATOR_LT: |
0c77715b | 4713 | return cmp < 0; |
4714 | break; | |
e440a328 | 4715 | case OPERATOR_LE: |
0c77715b | 4716 | return cmp <= 0; |
e440a328 | 4717 | case OPERATOR_GT: |
0c77715b | 4718 | return cmp > 0; |
e440a328 | 4719 | case OPERATOR_GE: |
0c77715b | 4720 | return cmp >= 0; |
e440a328 | 4721 | default: |
c3e6f413 | 4722 | go_unreachable(); |
e440a328 | 4723 | } |
4724 | } | |
4725 | ||
0c77715b | 4726 | // Compare constants according to OP. |
e440a328 | 4727 | |
4728 | bool | |
0c77715b | 4729 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4730 | Numeric_constant* right_nc, | |
4731 | Location location, bool* result) | |
e440a328 | 4732 | { |
0c77715b | 4733 | Type* left_type = left_nc->type(); |
4734 | Type* right_type = right_nc->type(); | |
4735 | ||
4736 | Type* type; | |
4737 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4738 | return false; | |
4739 | ||
4740 | // When comparing an untyped operand to a typed operand, we are | |
4741 | // effectively coercing the untyped operand to the other operand's | |
4742 | // type, so make sure that is valid. | |
4743 | if (!left_nc->set_type(type, true, location) | |
4744 | || !right_nc->set_type(type, true, location)) | |
4745 | return false; | |
4746 | ||
4747 | bool ret; | |
4748 | int cmp; | |
4749 | if (type->complex_type() != NULL) | |
4750 | { | |
4751 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4752 | return false; | |
4753 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4754 | } | |
4755 | else if (type->float_type() != NULL) | |
4756 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4757 | else |
0c77715b | 4758 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4759 | ||
4760 | if (ret) | |
4761 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4762 | ||
4763 | return ret; | |
4764 | } | |
4765 | ||
4766 | // Compare integer constants. | |
4767 | ||
4768 | bool | |
4769 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4770 | const Numeric_constant* right_nc, | |
4771 | int* cmp) | |
4772 | { | |
4773 | mpz_t left_val; | |
4774 | if (!left_nc->to_int(&left_val)) | |
4775 | return false; | |
4776 | mpz_t right_val; | |
4777 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4778 | { |
0c77715b | 4779 | mpz_clear(left_val); |
4780 | return false; | |
e440a328 | 4781 | } |
0c77715b | 4782 | |
4783 | *cmp = mpz_cmp(left_val, right_val); | |
4784 | ||
4785 | mpz_clear(left_val); | |
4786 | mpz_clear(right_val); | |
4787 | ||
4788 | return true; | |
4789 | } | |
4790 | ||
4791 | // Compare floating point constants. | |
4792 | ||
4793 | bool | |
4794 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4795 | const Numeric_constant* right_nc, | |
4796 | int* cmp) | |
4797 | { | |
4798 | mpfr_t left_val; | |
4799 | if (!left_nc->to_float(&left_val)) | |
4800 | return false; | |
4801 | mpfr_t right_val; | |
4802 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4803 | { |
0c77715b | 4804 | mpfr_clear(left_val); |
4805 | return false; | |
4806 | } | |
4807 | ||
4808 | // We already coerced both operands to the same type. If that type | |
4809 | // is not an abstract type, we need to round the values accordingly. | |
4810 | Type* type = left_nc->type(); | |
4811 | if (!type->is_abstract() && type->float_type() != NULL) | |
4812 | { | |
4813 | int bits = type->float_type()->bits(); | |
4814 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4815 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4816 | } |
0c77715b | 4817 | |
4818 | *cmp = mpfr_cmp(left_val, right_val); | |
4819 | ||
4820 | mpfr_clear(left_val); | |
4821 | mpfr_clear(right_val); | |
4822 | ||
4823 | return true; | |
e440a328 | 4824 | } |
4825 | ||
0c77715b | 4826 | // Compare complex constants. Complex numbers may only be compared |
4827 | // for equality. | |
e440a328 | 4828 | |
4829 | bool | |
0c77715b | 4830 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4831 | const Numeric_constant* right_nc, | |
4832 | int* cmp) | |
e440a328 | 4833 | { |
fcbea5e4 | 4834 | mpc_t left_val; |
4835 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4836 | return false; |
fcbea5e4 | 4837 | mpc_t right_val; |
4838 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4839 | { |
fcbea5e4 | 4840 | mpc_clear(left_val); |
0c77715b | 4841 | return false; |
e440a328 | 4842 | } |
0c77715b | 4843 | |
4844 | // We already coerced both operands to the same type. If that type | |
4845 | // is not an abstract type, we need to round the values accordingly. | |
4846 | Type* type = left_nc->type(); | |
4847 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4848 | { |
0c77715b | 4849 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4850 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4851 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4852 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4853 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4854 | } |
0c77715b | 4855 | |
fcbea5e4 | 4856 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4857 | |
fcbea5e4 | 4858 | mpc_clear(left_val); |
4859 | mpc_clear(right_val); | |
0c77715b | 4860 | |
4861 | return true; | |
e440a328 | 4862 | } |
4863 | ||
0c77715b | 4864 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4865 | // true if this could be done, false if not. Issue errors at LOCATION | |
af7a5274 | 4866 | // as appropriate, and sets *ISSUED_ERROR if it did. |
e440a328 | 4867 | |
4868 | bool | |
0c77715b | 4869 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4870 | Numeric_constant* right_nc, | |
af7a5274 | 4871 | Location location, Numeric_constant* nc, |
4872 | bool* issued_error) | |
e440a328 | 4873 | { |
af7a5274 | 4874 | *issued_error = false; |
e440a328 | 4875 | switch (op) |
4876 | { | |
4877 | case OPERATOR_OROR: | |
4878 | case OPERATOR_ANDAND: | |
4879 | case OPERATOR_EQEQ: | |
4880 | case OPERATOR_NOTEQ: | |
4881 | case OPERATOR_LT: | |
4882 | case OPERATOR_LE: | |
4883 | case OPERATOR_GT: | |
4884 | case OPERATOR_GE: | |
9767e2d3 | 4885 | // These return boolean values, not numeric. |
4886 | return false; | |
0c77715b | 4887 | default: |
4888 | break; | |
4889 | } | |
4890 | ||
4891 | Type* left_type = left_nc->type(); | |
4892 | Type* right_type = right_nc->type(); | |
4893 | ||
4894 | Type* type; | |
4895 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4896 | return false; | |
4897 | ||
4898 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4899 | ||
4900 | // When combining an untyped operand with a typed operand, we are | |
4901 | // effectively coercing the untyped operand to the other operand's | |
4902 | // type, so make sure that is valid. | |
4903 | if (!left_nc->set_type(type, true, location)) | |
4904 | return false; | |
4905 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4906 | return false; | |
85334a21 | 4907 | if (is_shift |
4908 | && ((left_type->integer_type() == NULL | |
4909 | && !left_type->is_abstract()) | |
4910 | || (right_type->integer_type() == NULL | |
4911 | && !right_type->is_abstract()))) | |
4912 | return false; | |
0c77715b | 4913 | |
4914 | bool r; | |
4915 | if (type->complex_type() != NULL) | |
4916 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4917 | else if (type->float_type() != NULL) | |
4918 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4919 | else | |
4920 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4921 | ||
4922 | if (r) | |
af7a5274 | 4923 | { |
4924 | r = nc->set_type(type, true, location); | |
4925 | if (!r) | |
4926 | *issued_error = true; | |
4927 | } | |
0c77715b | 4928 | |
4929 | return r; | |
4930 | } | |
4931 | ||
4932 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4933 | // integer operations. Return true if this could be done, false if | |
4934 | // not. | |
4935 | ||
4936 | bool | |
4937 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4938 | const Numeric_constant* right_nc, | |
4939 | Location location, Numeric_constant* nc) | |
4940 | { | |
4941 | mpz_t left_val; | |
4942 | if (!left_nc->to_int(&left_val)) | |
4943 | return false; | |
4944 | mpz_t right_val; | |
4945 | if (!right_nc->to_int(&right_val)) | |
4946 | { | |
4947 | mpz_clear(left_val); | |
e440a328 | 4948 | return false; |
0c77715b | 4949 | } |
4950 | ||
4951 | mpz_t val; | |
4952 | mpz_init(val); | |
4953 | ||
4954 | switch (op) | |
4955 | { | |
e440a328 | 4956 | case OPERATOR_PLUS: |
4957 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4958 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4959 | { | |
631d5788 | 4960 | go_error_at(location, "constant addition overflow"); |
71a45216 | 4961 | nc->set_invalid(); |
2c809f8f | 4962 | mpz_set_ui(val, 1); |
4963 | } | |
e440a328 | 4964 | break; |
4965 | case OPERATOR_MINUS: | |
4966 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4967 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4968 | { | |
631d5788 | 4969 | go_error_at(location, "constant subtraction overflow"); |
71a45216 | 4970 | nc->set_invalid(); |
2c809f8f | 4971 | mpz_set_ui(val, 1); |
4972 | } | |
e440a328 | 4973 | break; |
4974 | case OPERATOR_OR: | |
4975 | mpz_ior(val, left_val, right_val); | |
4976 | break; | |
4977 | case OPERATOR_XOR: | |
4978 | mpz_xor(val, left_val, right_val); | |
4979 | break; | |
4980 | case OPERATOR_MULT: | |
4981 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4982 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4983 | { | |
631d5788 | 4984 | go_error_at(location, "constant multiplication overflow"); |
71a45216 | 4985 | nc->set_invalid(); |
2c809f8f | 4986 | mpz_set_ui(val, 1); |
4987 | } | |
e440a328 | 4988 | break; |
4989 | case OPERATOR_DIV: | |
4990 | if (mpz_sgn(right_val) != 0) | |
4991 | mpz_tdiv_q(val, left_val, right_val); | |
4992 | else | |
4993 | { | |
631d5788 | 4994 | go_error_at(location, "division by zero"); |
71a45216 | 4995 | nc->set_invalid(); |
e440a328 | 4996 | mpz_set_ui(val, 0); |
e440a328 | 4997 | } |
4998 | break; | |
4999 | case OPERATOR_MOD: | |
5000 | if (mpz_sgn(right_val) != 0) | |
5001 | mpz_tdiv_r(val, left_val, right_val); | |
5002 | else | |
5003 | { | |
631d5788 | 5004 | go_error_at(location, "division by zero"); |
71a45216 | 5005 | nc->set_invalid(); |
e440a328 | 5006 | mpz_set_ui(val, 0); |
e440a328 | 5007 | } |
5008 | break; | |
5009 | case OPERATOR_LSHIFT: | |
5010 | { | |
5011 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 5012 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
5013 | mpz_mul_2exp(val, left_val, shift); | |
5014 | else | |
e440a328 | 5015 | { |
631d5788 | 5016 | go_error_at(location, "shift count overflow"); |
71a45216 | 5017 | nc->set_invalid(); |
2c809f8f | 5018 | mpz_set_ui(val, 1); |
e440a328 | 5019 | } |
e440a328 | 5020 | break; |
5021 | } | |
5022 | break; | |
5023 | case OPERATOR_RSHIFT: | |
5024 | { | |
5025 | unsigned long shift = mpz_get_ui(right_val); | |
5026 | if (mpz_cmp_ui(right_val, shift) != 0) | |
5027 | { | |
631d5788 | 5028 | go_error_at(location, "shift count overflow"); |
71a45216 | 5029 | nc->set_invalid(); |
2c809f8f | 5030 | mpz_set_ui(val, 1); |
e440a328 | 5031 | } |
e440a328 | 5032 | else |
0c77715b | 5033 | { |
5034 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
5035 | mpz_tdiv_q_2exp(val, left_val, shift); | |
5036 | else | |
5037 | mpz_fdiv_q_2exp(val, left_val, shift); | |
5038 | } | |
e440a328 | 5039 | break; |
5040 | } | |
5041 | break; | |
5042 | case OPERATOR_AND: | |
5043 | mpz_and(val, left_val, right_val); | |
5044 | break; | |
5045 | case OPERATOR_BITCLEAR: | |
5046 | { | |
5047 | mpz_t tval; | |
5048 | mpz_init(tval); | |
5049 | mpz_com(tval, right_val); | |
5050 | mpz_and(val, left_val, tval); | |
5051 | mpz_clear(tval); | |
5052 | } | |
5053 | break; | |
5054 | default: | |
c3e6f413 | 5055 | go_unreachable(); |
e440a328 | 5056 | } |
5057 | ||
0c77715b | 5058 | mpz_clear(left_val); |
5059 | mpz_clear(right_val); | |
e440a328 | 5060 | |
0c77715b | 5061 | if (left_nc->is_rune() |
5062 | || (op != OPERATOR_LSHIFT | |
5063 | && op != OPERATOR_RSHIFT | |
5064 | && right_nc->is_rune())) | |
5065 | nc->set_rune(NULL, val); | |
5066 | else | |
5067 | nc->set_int(NULL, val); | |
5068 | ||
5069 | mpz_clear(val); | |
e440a328 | 5070 | |
5071 | return true; | |
5072 | } | |
5073 | ||
0c77715b | 5074 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5075 | // floating point operations. Return true if this could be done, | |
5076 | // false if not. | |
e440a328 | 5077 | |
5078 | bool | |
0c77715b | 5079 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
5080 | const Numeric_constant* right_nc, | |
5081 | Location location, Numeric_constant* nc) | |
e440a328 | 5082 | { |
0c77715b | 5083 | mpfr_t left_val; |
5084 | if (!left_nc->to_float(&left_val)) | |
5085 | return false; | |
5086 | mpfr_t right_val; | |
5087 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 5088 | { |
0c77715b | 5089 | mpfr_clear(left_val); |
e440a328 | 5090 | return false; |
0c77715b | 5091 | } |
5092 | ||
5093 | mpfr_t val; | |
5094 | mpfr_init(val); | |
5095 | ||
5096 | bool ret = true; | |
5097 | switch (op) | |
5098 | { | |
e440a328 | 5099 | case OPERATOR_PLUS: |
5100 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
5101 | break; | |
5102 | case OPERATOR_MINUS: | |
5103 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
5104 | break; | |
5105 | case OPERATOR_OR: | |
5106 | case OPERATOR_XOR: | |
5107 | case OPERATOR_AND: | |
5108 | case OPERATOR_BITCLEAR: | |
0c77715b | 5109 | case OPERATOR_MOD: |
5110 | case OPERATOR_LSHIFT: | |
5111 | case OPERATOR_RSHIFT: | |
5112 | mpfr_set_ui(val, 0, GMP_RNDN); | |
5113 | ret = false; | |
5114 | break; | |
e440a328 | 5115 | case OPERATOR_MULT: |
5116 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
5117 | break; | |
5118 | case OPERATOR_DIV: | |
0c77715b | 5119 | if (!mpfr_zero_p(right_val)) |
5120 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
5121 | else | |
5122 | { | |
631d5788 | 5123 | go_error_at(location, "division by zero"); |
71a45216 | 5124 | nc->set_invalid(); |
0c77715b | 5125 | mpfr_set_ui(val, 0, GMP_RNDN); |
5126 | } | |
e440a328 | 5127 | break; |
e440a328 | 5128 | default: |
c3e6f413 | 5129 | go_unreachable(); |
e440a328 | 5130 | } |
5131 | ||
0c77715b | 5132 | mpfr_clear(left_val); |
5133 | mpfr_clear(right_val); | |
e440a328 | 5134 | |
0c77715b | 5135 | nc->set_float(NULL, val); |
5136 | mpfr_clear(val); | |
e440a328 | 5137 | |
0c77715b | 5138 | return ret; |
e440a328 | 5139 | } |
5140 | ||
0c77715b | 5141 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5142 | // complex operations. Return true if this could be done, false if | |
5143 | // not. | |
e440a328 | 5144 | |
5145 | bool | |
0c77715b | 5146 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
5147 | const Numeric_constant* right_nc, | |
5148 | Location location, Numeric_constant* nc) | |
e440a328 | 5149 | { |
fcbea5e4 | 5150 | mpc_t left_val; |
5151 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 5152 | return false; |
fcbea5e4 | 5153 | mpc_t right_val; |
5154 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 5155 | { |
fcbea5e4 | 5156 | mpc_clear(left_val); |
e440a328 | 5157 | return false; |
0c77715b | 5158 | } |
5159 | ||
fcbea5e4 | 5160 | mpc_t val; |
5161 | mpc_init2(val, mpc_precision); | |
0c77715b | 5162 | |
5163 | bool ret = true; | |
5164 | switch (op) | |
5165 | { | |
e440a328 | 5166 | case OPERATOR_PLUS: |
fcbea5e4 | 5167 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5168 | break; |
5169 | case OPERATOR_MINUS: | |
fcbea5e4 | 5170 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5171 | break; |
5172 | case OPERATOR_OR: | |
5173 | case OPERATOR_XOR: | |
5174 | case OPERATOR_AND: | |
5175 | case OPERATOR_BITCLEAR: | |
0c77715b | 5176 | case OPERATOR_MOD: |
5177 | case OPERATOR_LSHIFT: | |
5178 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 5179 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 5180 | ret = false; |
5181 | break; | |
e440a328 | 5182 | case OPERATOR_MULT: |
fcbea5e4 | 5183 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5184 | break; |
5185 | case OPERATOR_DIV: | |
fcbea5e4 | 5186 | if (mpc_cmp_si(right_val, 0) == 0) |
5187 | { | |
631d5788 | 5188 | go_error_at(location, "division by zero"); |
71a45216 | 5189 | nc->set_invalid(); |
fcbea5e4 | 5190 | mpc_set_ui(val, 0, MPC_RNDNN); |
5191 | break; | |
5192 | } | |
5193 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 5194 | break; |
e440a328 | 5195 | default: |
c3e6f413 | 5196 | go_unreachable(); |
e440a328 | 5197 | } |
5198 | ||
fcbea5e4 | 5199 | mpc_clear(left_val); |
5200 | mpc_clear(right_val); | |
e440a328 | 5201 | |
fcbea5e4 | 5202 | nc->set_complex(NULL, val); |
5203 | mpc_clear(val); | |
e440a328 | 5204 | |
0c77715b | 5205 | return ret; |
e440a328 | 5206 | } |
5207 | ||
5208 | // Lower a binary expression. We have to evaluate constant | |
5209 | // expressions now, in order to implement Go's unlimited precision | |
5210 | // constants. | |
5211 | ||
5212 | Expression* | |
e9d3367e | 5213 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5214 | Statement_inserter* inserter, int) | |
e440a328 | 5215 | { |
b13c66cd | 5216 | Location location = this->location(); |
e440a328 | 5217 | Operator op = this->op_; |
5218 | Expression* left = this->left_; | |
5219 | Expression* right = this->right_; | |
5220 | ||
5221 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5222 | || op == OPERATOR_NOTEQ | |
5223 | || op == OPERATOR_LT | |
5224 | || op == OPERATOR_LE | |
5225 | || op == OPERATOR_GT | |
5226 | || op == OPERATOR_GE); | |
5227 | ||
0c77715b | 5228 | // Numeric constant expressions. |
e440a328 | 5229 | { |
0c77715b | 5230 | Numeric_constant left_nc; |
5231 | Numeric_constant right_nc; | |
5232 | if (left->numeric_constant_value(&left_nc) | |
5233 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5234 | { |
0c77715b | 5235 | if (is_comparison) |
e440a328 | 5236 | { |
0c77715b | 5237 | bool result; |
5238 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5239 | &right_nc, location, | |
5240 | &result)) | |
5241 | return this; | |
e90c9dfc | 5242 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5243 | Expression::make_boolean(result, |
5244 | location), | |
5245 | location); | |
e440a328 | 5246 | } |
5247 | else | |
5248 | { | |
0c77715b | 5249 | Numeric_constant nc; |
af7a5274 | 5250 | bool issued_error; |
0c77715b | 5251 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, |
af7a5274 | 5252 | location, &nc, |
5253 | &issued_error)) | |
5254 | { | |
5255 | if (issued_error) | |
5256 | return Expression::make_error(location); | |
71a45216 | 5257 | return this; |
af7a5274 | 5258 | } |
0c77715b | 5259 | return nc.expression(location); |
e440a328 | 5260 | } |
5261 | } | |
e440a328 | 5262 | } |
5263 | ||
5264 | // String constant expressions. | |
315fa98d | 5265 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5266 | { |
5267 | std::string left_string; | |
5268 | std::string right_string; | |
5269 | if (left->string_constant_value(&left_string) | |
5270 | && right->string_constant_value(&right_string)) | |
315fa98d | 5271 | { |
5272 | if (op == OPERATOR_PLUS) | |
5273 | return Expression::make_string(left_string + right_string, | |
5274 | location); | |
5275 | else if (is_comparison) | |
5276 | { | |
5277 | int cmp = left_string.compare(right_string); | |
0c77715b | 5278 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5279 | return Expression::make_boolean(r, location); |
b40dc774 | 5280 | } |
5281 | } | |
b40dc774 | 5282 | } |
5283 | ||
ceeb12d7 | 5284 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5285 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5286 | { | |
b79832ca | 5287 | if (left->type()->struct_type() != NULL |
5288 | && right->type()->struct_type() != NULL) | |
e9d3367e | 5289 | return this->lower_struct_comparison(gogo, inserter); |
5290 | else if (left->type()->array_type() != NULL | |
b79832ca | 5291 | && !left->type()->is_slice_type() |
5292 | && right->type()->array_type() != NULL | |
5293 | && !right->type()->is_slice_type()) | |
e9d3367e | 5294 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 5295 | else if ((left->type()->interface_type() != NULL |
5296 | && right->type()->interface_type() == NULL) | |
5297 | || (left->type()->interface_type() == NULL | |
5298 | && right->type()->interface_type() != NULL)) | |
5299 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5300 | } |
5301 | ||
736a16ba | 5302 | // Lower string concatenation to String_concat_expression, so that |
5303 | // we can group sequences of string additions. | |
5304 | if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) | |
5305 | { | |
5306 | Expression_list* exprs; | |
5307 | String_concat_expression* left_sce = | |
5308 | this->left_->string_concat_expression(); | |
5309 | if (left_sce != NULL) | |
5310 | exprs = left_sce->exprs(); | |
5311 | else | |
5312 | { | |
5313 | exprs = new Expression_list(); | |
5314 | exprs->push_back(this->left_); | |
5315 | } | |
5316 | ||
5317 | String_concat_expression* right_sce = | |
5318 | this->right_->string_concat_expression(); | |
5319 | if (right_sce != NULL) | |
5320 | exprs->append(right_sce->exprs()); | |
5321 | else | |
5322 | exprs->push_back(this->right_); | |
5323 | ||
5324 | return Expression::make_string_concat(exprs); | |
5325 | } | |
5326 | ||
e440a328 | 5327 | return this; |
5328 | } | |
5329 | ||
e9d3367e | 5330 | // Lower a struct comparison. |
5331 | ||
5332 | Expression* | |
5333 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5334 | Statement_inserter* inserter) | |
5335 | { | |
5336 | Struct_type* st = this->left_->type()->struct_type(); | |
5337 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5338 | if (st2 == NULL) | |
5339 | return this; | |
5340 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5341 | return this; | |
5342 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5343 | this->right_->type(), NULL)) | |
5344 | return this; | |
5345 | ||
5346 | // See if we can compare using memcmp. As a heuristic, we use | |
5347 | // memcmp rather than field references and comparisons if there are | |
5348 | // more than two fields. | |
113ef6a5 | 5349 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5350 | return this->lower_compare_to_memcmp(gogo, inserter); |
5351 | ||
5352 | Location loc = this->location(); | |
5353 | ||
5354 | Expression* left = this->left_; | |
5355 | Temporary_statement* left_temp = NULL; | |
5356 | if (left->var_expression() == NULL | |
5357 | && left->temporary_reference_expression() == NULL) | |
5358 | { | |
5359 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5360 | inserter->insert(left_temp); | |
5361 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5362 | } | |
5363 | ||
5364 | Expression* right = this->right_; | |
5365 | Temporary_statement* right_temp = NULL; | |
5366 | if (right->var_expression() == NULL | |
5367 | && right->temporary_reference_expression() == NULL) | |
5368 | { | |
5369 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5370 | inserter->insert(right_temp); | |
5371 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5372 | } | |
5373 | ||
5374 | Expression* ret = Expression::make_boolean(true, loc); | |
5375 | const Struct_field_list* fields = st->fields(); | |
5376 | unsigned int field_index = 0; | |
5377 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5378 | pf != fields->end(); | |
5379 | ++pf, ++field_index) | |
5380 | { | |
f5165c05 | 5381 | if (Gogo::is_sink_name(pf->field_name())) |
5382 | continue; | |
5383 | ||
e9d3367e | 5384 | if (field_index > 0) |
5385 | { | |
5386 | if (left_temp == NULL) | |
5387 | left = left->copy(); | |
5388 | else | |
5389 | left = Expression::make_temporary_reference(left_temp, loc); | |
5390 | if (right_temp == NULL) | |
5391 | right = right->copy(); | |
5392 | else | |
5393 | right = Expression::make_temporary_reference(right_temp, loc); | |
5394 | } | |
5395 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5396 | loc); | |
5397 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5398 | loc); | |
5399 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5400 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5401 | } | |
5402 | ||
5403 | if (this->op_ == OPERATOR_NOTEQ) | |
5404 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5405 | ||
5406 | return ret; | |
5407 | } | |
5408 | ||
5409 | // Lower an array comparison. | |
5410 | ||
5411 | Expression* | |
5412 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5413 | Statement_inserter* inserter) | |
5414 | { | |
5415 | Array_type* at = this->left_->type()->array_type(); | |
5416 | Array_type* at2 = this->right_->type()->array_type(); | |
5417 | if (at2 == NULL) | |
5418 | return this; | |
5419 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5420 | return this; | |
5421 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5422 | this->right_->type(), NULL)) | |
5423 | return this; | |
5424 | ||
5425 | // Call memcmp directly if possible. This may let the middle-end | |
5426 | // optimize the call. | |
113ef6a5 | 5427 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5428 | return this->lower_compare_to_memcmp(gogo, inserter); |
5429 | ||
5430 | // Call the array comparison function. | |
5431 | Named_object* hash_fn; | |
5432 | Named_object* equal_fn; | |
5433 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5434 | &hash_fn, &equal_fn); | |
5435 | ||
5436 | Location loc = this->location(); | |
5437 | ||
5438 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5439 | ||
5440 | Expression_list* args = new Expression_list(); | |
5441 | args->push_back(this->operand_address(inserter, this->left_)); | |
5442 | args->push_back(this->operand_address(inserter, this->right_)); | |
e9d3367e | 5443 | |
5444 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5445 | ||
5446 | if (this->op_ == OPERATOR_NOTEQ) | |
5447 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5448 | ||
5449 | return ret; | |
5450 | } | |
5451 | ||
ceeb12d7 | 5452 | // Lower an interface to value comparison. |
5453 | ||
5454 | Expression* | |
5455 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5456 | Statement_inserter* inserter) | |
5457 | { | |
5458 | Type* left_type = this->left_->type(); | |
5459 | Type* right_type = this->right_->type(); | |
5460 | Interface_type* ift; | |
5461 | if (left_type->interface_type() != NULL) | |
5462 | { | |
5463 | ift = left_type->interface_type(); | |
5464 | if (!ift->implements_interface(right_type, NULL)) | |
5465 | return this; | |
5466 | } | |
5467 | else | |
5468 | { | |
5469 | ift = right_type->interface_type(); | |
5470 | if (!ift->implements_interface(left_type, NULL)) | |
5471 | return this; | |
5472 | } | |
5473 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5474 | return this; | |
5475 | ||
5476 | Location loc = this->location(); | |
5477 | ||
5478 | if (left_type->interface_type() == NULL | |
5479 | && left_type->points_to() == NULL | |
5480 | && !this->left_->is_addressable()) | |
5481 | { | |
5482 | Temporary_statement* temp = | |
5483 | Statement::make_temporary(left_type, NULL, loc); | |
5484 | inserter->insert(temp); | |
5485 | this->left_ = | |
5486 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5487 | } | |
5488 | ||
5489 | if (right_type->interface_type() == NULL | |
5490 | && right_type->points_to() == NULL | |
5491 | && !this->right_->is_addressable()) | |
5492 | { | |
5493 | Temporary_statement* temp = | |
5494 | Statement::make_temporary(right_type, NULL, loc); | |
5495 | inserter->insert(temp); | |
5496 | this->right_ = | |
5497 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5498 | } | |
5499 | ||
5500 | return this; | |
5501 | } | |
5502 | ||
e9d3367e | 5503 | // Lower a struct or array comparison to a call to memcmp. |
5504 | ||
5505 | Expression* | |
5506 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5507 | { | |
5508 | Location loc = this->location(); | |
5509 | ||
5510 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5511 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5512 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5513 | TYPE_INFO_SIZE); | |
5514 | ||
5515 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5516 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5517 | return Expression::make_binary(this->op_, call, zero, loc); |
5518 | } | |
5519 | ||
a32698ee | 5520 | Expression* |
5c3f3470 | 5521 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5522 | Statement_inserter* inserter) |
5523 | { | |
5524 | Location loc = this->location(); | |
5bf8be8b | 5525 | if (this->left_->type()->is_error_type() |
5526 | || this->right_->type()->is_error_type() | |
5527 | || this->left_->is_error_expression() | |
5528 | || this->right_->is_error_expression()) | |
5529 | { | |
5530 | go_assert(saw_errors()); | |
5531 | return Expression::make_error(loc); | |
5532 | } | |
5533 | ||
a32698ee | 5534 | Temporary_statement* temp; |
a32698ee | 5535 | |
5536 | Type* left_type = this->left_->type(); | |
5537 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5538 | || this->op_ == OPERATOR_RSHIFT); | |
5539 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5540 | left_type->integer_type() != NULL) | |
5541 | || this->op_ == OPERATOR_MOD); | |
5542 | ||
a32698ee | 5543 | if (is_shift_op |
5c3f3470 | 5544 | || (is_idiv_op |
5545 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5546 | { |
545ab43b | 5547 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5548 | { |
5549 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5550 | inserter->insert(temp); | |
5551 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5552 | } | |
545ab43b | 5553 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5554 | { |
5555 | temp = | |
5556 | Statement::make_temporary(NULL, this->right_, loc); | |
5557 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5558 | inserter->insert(temp); | |
5559 | } | |
5560 | } | |
5561 | return this; | |
5562 | } | |
5563 | ||
5564 | ||
e9d3367e | 5565 | // Return the address of EXPR, cast to unsafe.Pointer. |
5566 | ||
5567 | Expression* | |
5568 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5569 | Expression* expr) | |
5570 | { | |
5571 | Location loc = this->location(); | |
5572 | ||
5573 | if (!expr->is_addressable()) | |
5574 | { | |
5575 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5576 | loc); | |
5577 | inserter->insert(temp); | |
5578 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5579 | } | |
5580 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5581 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5582 | Type* void_type = Type::make_void_type(); | |
5583 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5584 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5585 | } | |
5586 | ||
0c77715b | 5587 | // Return the numeric constant value, if it has one. |
e440a328 | 5588 | |
5589 | bool | |
0c77715b | 5590 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5591 | { |
0c77715b | 5592 | Numeric_constant left_nc; |
5593 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5594 | return false; | |
5595 | Numeric_constant right_nc; | |
5596 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5597 | return false; | |
af7a5274 | 5598 | bool issued_error; |
9767e2d3 | 5599 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
af7a5274 | 5600 | this->location(), nc, &issued_error); |
e440a328 | 5601 | } |
5602 | ||
5603 | // Note that the value is being discarded. | |
5604 | ||
4f2138d7 | 5605 | bool |
e440a328 | 5606 | Binary_expression::do_discarding_value() |
5607 | { | |
5608 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5609 | return this->right_->discarding_value(); |
e440a328 | 5610 | else |
4f2138d7 | 5611 | { |
5612 | this->unused_value_error(); | |
5613 | return false; | |
5614 | } | |
e440a328 | 5615 | } |
5616 | ||
5617 | // Get type. | |
5618 | ||
5619 | Type* | |
5620 | Binary_expression::do_type() | |
5621 | { | |
5f5fea79 | 5622 | if (this->classification() == EXPRESSION_ERROR) |
5623 | return Type::make_error_type(); | |
5624 | ||
e440a328 | 5625 | switch (this->op_) |
5626 | { | |
e440a328 | 5627 | case OPERATOR_EQEQ: |
5628 | case OPERATOR_NOTEQ: | |
5629 | case OPERATOR_LT: | |
5630 | case OPERATOR_LE: | |
5631 | case OPERATOR_GT: | |
5632 | case OPERATOR_GE: | |
e90c9dfc | 5633 | if (this->type_ == NULL) |
5634 | this->type_ = Type::make_boolean_type(); | |
5635 | return this->type_; | |
e440a328 | 5636 | |
5637 | case OPERATOR_PLUS: | |
5638 | case OPERATOR_MINUS: | |
5639 | case OPERATOR_OR: | |
5640 | case OPERATOR_XOR: | |
5641 | case OPERATOR_MULT: | |
5642 | case OPERATOR_DIV: | |
5643 | case OPERATOR_MOD: | |
5644 | case OPERATOR_AND: | |
5645 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5646 | case OPERATOR_OROR: |
5647 | case OPERATOR_ANDAND: | |
e440a328 | 5648 | { |
0c77715b | 5649 | Type* type; |
5650 | if (!Binary_expression::operation_type(this->op_, | |
5651 | this->left_->type(), | |
5652 | this->right_->type(), | |
5653 | &type)) | |
5654 | return Type::make_error_type(); | |
5655 | return type; | |
e440a328 | 5656 | } |
5657 | ||
5658 | case OPERATOR_LSHIFT: | |
5659 | case OPERATOR_RSHIFT: | |
5660 | return this->left_->type(); | |
5661 | ||
5662 | default: | |
c3e6f413 | 5663 | go_unreachable(); |
e440a328 | 5664 | } |
5665 | } | |
5666 | ||
5667 | // Set type for a binary expression. | |
5668 | ||
5669 | void | |
5670 | Binary_expression::do_determine_type(const Type_context* context) | |
5671 | { | |
5672 | Type* tleft = this->left_->type(); | |
5673 | Type* tright = this->right_->type(); | |
5674 | ||
5675 | // Both sides should have the same type, except for the shift | |
5676 | // operations. For a comparison, we should ignore the incoming | |
5677 | // type. | |
5678 | ||
5679 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5680 | || this->op_ == OPERATOR_RSHIFT); | |
5681 | ||
5682 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5683 | || this->op_ == OPERATOR_NOTEQ | |
5684 | || this->op_ == OPERATOR_LT | |
5685 | || this->op_ == OPERATOR_LE | |
5686 | || this->op_ == OPERATOR_GT | |
5687 | || this->op_ == OPERATOR_GE); | |
5688 | ||
c999c2a7 | 5689 | // For constant expressions, the context of the result is not useful in |
5690 | // determining the types of the operands. It is only legal to use abstract | |
5691 | // boolean, numeric, and string constants as operands where it is legal to | |
5692 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5693 | // Any issues with the operation will be resolved in the check_types pass. | |
5694 | bool is_constant_expr = (this->left_->is_constant() | |
5695 | && this->right_->is_constant()); | |
5696 | ||
e440a328 | 5697 | Type_context subcontext(*context); |
5698 | ||
9c4ff2ce | 5699 | if (is_constant_expr && !is_shift_op) |
af7a5274 | 5700 | { |
5701 | subcontext.type = NULL; | |
5702 | subcontext.may_be_abstract = true; | |
5703 | } | |
5704 | else if (is_comparison) | |
e440a328 | 5705 | { |
5706 | // In a comparison, the context does not determine the types of | |
5707 | // the operands. | |
5708 | subcontext.type = NULL; | |
5709 | } | |
5710 | ||
5711 | // Set the context for the left hand operand. | |
5712 | if (is_shift_op) | |
5713 | { | |
b40dc774 | 5714 | // The right hand operand of a shift plays no role in |
5715 | // determining the type of the left hand operand. | |
e440a328 | 5716 | } |
5717 | else if (!tleft->is_abstract()) | |
5718 | subcontext.type = tleft; | |
5719 | else if (!tright->is_abstract()) | |
5720 | subcontext.type = tright; | |
5721 | else if (subcontext.type == NULL) | |
5722 | { | |
5723 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5724 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5725 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5726 | { | |
5727 | // Both sides have an abstract integer, abstract float, or | |
5728 | // abstract complex type. Just let CONTEXT determine | |
5729 | // whether they may remain abstract or not. | |
5730 | } | |
5731 | else if (tleft->complex_type() != NULL) | |
5732 | subcontext.type = tleft; | |
5733 | else if (tright->complex_type() != NULL) | |
5734 | subcontext.type = tright; | |
5735 | else if (tleft->float_type() != NULL) | |
5736 | subcontext.type = tleft; | |
5737 | else if (tright->float_type() != NULL) | |
5738 | subcontext.type = tright; | |
5739 | else | |
5740 | subcontext.type = tleft; | |
f58a23ae | 5741 | |
5742 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5743 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5744 | } |
5745 | ||
af7a5274 | 5746 | this->left_->determine_type(&subcontext); |
e440a328 | 5747 | |
e440a328 | 5748 | if (is_shift_op) |
5749 | { | |
b40dc774 | 5750 | // We may have inherited an unusable type for the shift operand. |
5751 | // Give a useful error if that happened. | |
5752 | if (tleft->is_abstract() | |
5753 | && subcontext.type != NULL | |
8ab6effb | 5754 | && !subcontext.may_be_abstract |
f6bc81e6 | 5755 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5756 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5757 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5758 | "for left operand of shift")); |
b40dc774 | 5759 | |
5760 | // The context for the right hand operand is the same as for the | |
5761 | // left hand operand, except for a shift operator. | |
e440a328 | 5762 | subcontext.type = Type::lookup_integer_type("uint"); |
5763 | subcontext.may_be_abstract = false; | |
5764 | } | |
5765 | ||
af7a5274 | 5766 | this->right_->determine_type(&subcontext); |
e90c9dfc | 5767 | |
5768 | if (is_comparison) | |
5769 | { | |
5770 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5771 | ; | |
5772 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5773 | this->type_ = context->type; | |
5774 | else if (!context->may_be_abstract) | |
5775 | this->type_ = Type::lookup_bool_type(); | |
5776 | } | |
e440a328 | 5777 | } |
5778 | ||
5779 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5780 | // OTYPE is the type of the other operand. Return whether the |
5781 | // operation is OK. This should not be used for shift. | |
e440a328 | 5782 | |
5783 | bool | |
be8b5eee | 5784 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5785 | Location location) |
e440a328 | 5786 | { |
5787 | switch (op) | |
5788 | { | |
5789 | case OPERATOR_OROR: | |
5790 | case OPERATOR_ANDAND: | |
c999c2a7 | 5791 | if (!type->is_boolean_type() |
5792 | || !otype->is_boolean_type()) | |
e440a328 | 5793 | { |
631d5788 | 5794 | go_error_at(location, "expected boolean type"); |
e440a328 | 5795 | return false; |
5796 | } | |
5797 | break; | |
5798 | ||
5799 | case OPERATOR_EQEQ: | |
5800 | case OPERATOR_NOTEQ: | |
e9d3367e | 5801 | { |
5802 | std::string reason; | |
5803 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5804 | { | |
631d5788 | 5805 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5806 | return false; |
5807 | } | |
5808 | } | |
e440a328 | 5809 | break; |
5810 | ||
5811 | case OPERATOR_LT: | |
5812 | case OPERATOR_LE: | |
5813 | case OPERATOR_GT: | |
5814 | case OPERATOR_GE: | |
e9d3367e | 5815 | { |
5816 | std::string reason; | |
5817 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5818 | { | |
631d5788 | 5819 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5820 | return false; |
5821 | } | |
5822 | } | |
e440a328 | 5823 | break; |
5824 | ||
5825 | case OPERATOR_PLUS: | |
5826 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5827 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5828 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5829 | { |
631d5788 | 5830 | go_error_at(location, |
e440a328 | 5831 | "expected integer, floating, complex, or string type"); |
5832 | return false; | |
5833 | } | |
5834 | break; | |
5835 | ||
5836 | case OPERATOR_MINUS: | |
5837 | case OPERATOR_MINUSEQ: | |
5838 | case OPERATOR_MULT: | |
5839 | case OPERATOR_MULTEQ: | |
5840 | case OPERATOR_DIV: | |
5841 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5842 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5843 | { |
631d5788 | 5844 | go_error_at(location, "expected integer, floating, or complex type"); |
e440a328 | 5845 | return false; |
5846 | } | |
5847 | break; | |
5848 | ||
5849 | case OPERATOR_MOD: | |
5850 | case OPERATOR_MODEQ: | |
5851 | case OPERATOR_OR: | |
5852 | case OPERATOR_OREQ: | |
5853 | case OPERATOR_AND: | |
5854 | case OPERATOR_ANDEQ: | |
5855 | case OPERATOR_XOR: | |
5856 | case OPERATOR_XOREQ: | |
5857 | case OPERATOR_BITCLEAR: | |
5858 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5859 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5860 | { |
631d5788 | 5861 | go_error_at(location, "expected integer type"); |
e440a328 | 5862 | return false; |
5863 | } | |
5864 | break; | |
5865 | ||
5866 | default: | |
c3e6f413 | 5867 | go_unreachable(); |
e440a328 | 5868 | } |
5869 | ||
5870 | return true; | |
5871 | } | |
5872 | ||
5873 | // Check types. | |
5874 | ||
5875 | void | |
5876 | Binary_expression::do_check_types(Gogo*) | |
5877 | { | |
5f5fea79 | 5878 | if (this->classification() == EXPRESSION_ERROR) |
5879 | return; | |
5880 | ||
e440a328 | 5881 | Type* left_type = this->left_->type(); |
5882 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5883 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5884 | { |
5885 | this->set_is_error(); | |
5886 | return; | |
5887 | } | |
e440a328 | 5888 | |
5889 | if (this->op_ == OPERATOR_EQEQ | |
5890 | || this->op_ == OPERATOR_NOTEQ | |
5891 | || this->op_ == OPERATOR_LT | |
5892 | || this->op_ == OPERATOR_LE | |
5893 | || this->op_ == OPERATOR_GT | |
5894 | || this->op_ == OPERATOR_GE) | |
5895 | { | |
907c5ecd | 5896 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5897 | { | |
5898 | this->report_error(_("invalid comparison of nil with nil")); | |
5899 | return; | |
5900 | } | |
e440a328 | 5901 | if (!Type::are_assignable(left_type, right_type, NULL) |
5902 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5903 | { | |
5904 | this->report_error(_("incompatible types in binary expression")); | |
5905 | return; | |
5906 | } | |
5907 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5908 | right_type, |
e440a328 | 5909 | this->location()) |
5910 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5911 | left_type, |
e440a328 | 5912 | this->location())) |
5913 | { | |
5914 | this->set_is_error(); | |
5915 | return; | |
5916 | } | |
5917 | } | |
5918 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5919 | { | |
5920 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5921 | { | |
5922 | this->report_error(_("incompatible types in binary expression")); | |
5923 | return; | |
5924 | } | |
5925 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5926 | right_type, |
e440a328 | 5927 | this->location())) |
5928 | { | |
5929 | this->set_is_error(); | |
5930 | return; | |
5931 | } | |
5c65b19d | 5932 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5933 | { | |
5934 | // Division by a zero integer constant is an error. | |
5935 | Numeric_constant rconst; | |
5936 | unsigned long rval; | |
5937 | if (left_type->integer_type() != NULL | |
5938 | && this->right_->numeric_constant_value(&rconst) | |
5939 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5940 | && rval == 0) | |
5941 | { | |
5942 | this->report_error(_("integer division by zero")); | |
5943 | return; | |
5944 | } | |
5945 | } | |
e440a328 | 5946 | } |
5947 | else | |
5948 | { | |
5949 | if (left_type->integer_type() == NULL) | |
5950 | this->report_error(_("shift of non-integer operand")); | |
5951 | ||
6b5e0fac | 5952 | if (right_type->is_string_type()) |
5953 | this->report_error(_("shift count not unsigned integer")); | |
5954 | else if (!right_type->is_abstract() | |
e440a328 | 5955 | && (right_type->integer_type() == NULL |
5956 | || !right_type->integer_type()->is_unsigned())) | |
5957 | this->report_error(_("shift count not unsigned integer")); | |
5958 | else | |
5959 | { | |
0c77715b | 5960 | Numeric_constant nc; |
5961 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5962 | { |
0c77715b | 5963 | mpz_t val; |
5964 | if (!nc.to_int(&val)) | |
5965 | this->report_error(_("shift count not unsigned integer")); | |
5966 | else | |
a4eba91b | 5967 | { |
0c77715b | 5968 | if (mpz_sgn(val) < 0) |
5969 | { | |
5970 | this->report_error(_("negative shift count")); | |
0c77715b | 5971 | Location rloc = this->right_->location(); |
e67508fa | 5972 | this->right_ = Expression::make_integer_ul(0, right_type, |
5973 | rloc); | |
0c77715b | 5974 | } |
5975 | mpz_clear(val); | |
a4eba91b | 5976 | } |
e440a328 | 5977 | } |
e440a328 | 5978 | } |
5979 | } | |
5980 | } | |
5981 | ||
ea664253 | 5982 | // Get the backend representation for a binary expression. |
e440a328 | 5983 | |
ea664253 | 5984 | Bexpression* |
5985 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 5986 | { |
1b1f2abf | 5987 | Gogo* gogo = context->gogo(); |
a32698ee | 5988 | Location loc = this->location(); |
5989 | Type* left_type = this->left_->type(); | |
5990 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5991 | |
e440a328 | 5992 | bool use_left_type = true; |
5993 | bool is_shift_op = false; | |
29a2d1d8 | 5994 | bool is_idiv_op = false; |
e440a328 | 5995 | switch (this->op_) |
5996 | { | |
5997 | case OPERATOR_EQEQ: | |
5998 | case OPERATOR_NOTEQ: | |
5999 | case OPERATOR_LT: | |
6000 | case OPERATOR_LE: | |
6001 | case OPERATOR_GT: | |
6002 | case OPERATOR_GE: | |
ea664253 | 6003 | return Expression::comparison(context, this->type_, this->op_, |
6004 | this->left_, this->right_, loc); | |
e440a328 | 6005 | |
6006 | case OPERATOR_OROR: | |
e440a328 | 6007 | case OPERATOR_ANDAND: |
e440a328 | 6008 | use_left_type = false; |
6009 | break; | |
6010 | case OPERATOR_PLUS: | |
e440a328 | 6011 | case OPERATOR_MINUS: |
e440a328 | 6012 | case OPERATOR_OR: |
e440a328 | 6013 | case OPERATOR_XOR: |
e440a328 | 6014 | case OPERATOR_MULT: |
e440a328 | 6015 | break; |
6016 | case OPERATOR_DIV: | |
a32698ee | 6017 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
6018 | break; | |
729f8831 | 6019 | // Fall through. |
e440a328 | 6020 | case OPERATOR_MOD: |
29a2d1d8 | 6021 | is_idiv_op = true; |
e440a328 | 6022 | break; |
6023 | case OPERATOR_LSHIFT: | |
e440a328 | 6024 | case OPERATOR_RSHIFT: |
e440a328 | 6025 | is_shift_op = true; |
6026 | break; | |
e440a328 | 6027 | case OPERATOR_BITCLEAR: |
a32698ee | 6028 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
6029 | case OPERATOR_AND: | |
e440a328 | 6030 | break; |
6031 | default: | |
c3e6f413 | 6032 | go_unreachable(); |
e440a328 | 6033 | } |
6034 | ||
736a16ba | 6035 | // The only binary operation for string is +, and that should have |
6036 | // been converted to a String_concat_expression in do_lower. | |
6037 | go_assert(!left_type->is_string_type()); | |
a32698ee | 6038 | |
6039 | // For complex division Go might want slightly different results than the | |
6040 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 6041 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
6042 | { | |
a32698ee | 6043 | Runtime::Function complex_code; |
1850e20c | 6044 | switch (this->left_->type()->complex_type()->bits()) |
6045 | { | |
6046 | case 64: | |
a32698ee | 6047 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 6048 | break; |
6049 | case 128: | |
a32698ee | 6050 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 6051 | break; |
6052 | default: | |
6053 | go_unreachable(); | |
6054 | } | |
a32698ee | 6055 | Expression* complex_div = |
6056 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 6057 | return complex_div->get_backend(context); |
1850e20c | 6058 | } |
6059 | ||
ea664253 | 6060 | Bexpression* left = this->left_->get_backend(context); |
6061 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 6062 | |
a32698ee | 6063 | Type* type = use_left_type ? left_type : right_type; |
6064 | Btype* btype = type->get_backend(gogo); | |
6065 | ||
6066 | Bexpression* ret = | |
6067 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
6068 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 6069 | |
a32698ee | 6070 | // Initialize overflow constants. |
6071 | Bexpression* overflow; | |
6072 | mpz_t zero; | |
6073 | mpz_init_set_ui(zero, 0UL); | |
6074 | mpz_t one; | |
6075 | mpz_init_set_ui(one, 1UL); | |
6076 | mpz_t neg_one; | |
6077 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 6078 | |
a32698ee | 6079 | Btype* left_btype = left_type->get_backend(gogo); |
6080 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 6081 | |
6082 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 6083 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 6084 | if (is_shift_op) |
6085 | { | |
a32698ee | 6086 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 6087 | |
a32698ee | 6088 | int bits = left_type->integer_type()->bits(); |
a7c5b619 | 6089 | |
6090 | Numeric_constant nc; | |
6091 | unsigned long ul; | |
6092 | if (!this->right_->numeric_constant_value(&nc) | |
6093 | || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID | |
6094 | || ul >= static_cast<unsigned long>(bits)) | |
e440a328 | 6095 | { |
a7c5b619 | 6096 | mpz_t bitsval; |
6097 | mpz_init_set_ui(bitsval, bits); | |
6098 | Bexpression* bits_expr = | |
6099 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
6100 | Bexpression* compare = | |
6101 | gogo->backend()->binary_expression(OPERATOR_LT, | |
6102 | right, bits_expr, loc); | |
6103 | ||
6104 | Bexpression* zero_expr = | |
6105 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
6106 | overflow = zero_expr; | |
6107 | Bfunction* bfn = context->function()->func_value()->get_decl(); | |
6108 | if (this->op_ == OPERATOR_RSHIFT | |
6109 | && !left_type->integer_type()->is_unsigned()) | |
6110 | { | |
6111 | Bexpression* neg_expr = | |
6112 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
6113 | zero_expr, loc); | |
6114 | Bexpression* neg_one_expr = | |
6115 | gogo->backend()->integer_constant_expression(left_btype, | |
6116 | neg_one); | |
6117 | overflow = gogo->backend()->conditional_expression(bfn, | |
6118 | btype, | |
6119 | neg_expr, | |
6120 | neg_one_expr, | |
6121 | zero_expr, | |
6122 | loc); | |
6123 | } | |
6124 | ret = gogo->backend()->conditional_expression(bfn, btype, compare, | |
6125 | ret, overflow, loc); | |
6126 | mpz_clear(bitsval); | |
29a2d1d8 | 6127 | } |
29a2d1d8 | 6128 | } |
6129 | ||
6130 | // Add checks for division by zero and division overflow as needed. | |
6131 | if (is_idiv_op) | |
6132 | { | |
5c3f3470 | 6133 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 6134 | { |
6135 | // right == 0 | |
a32698ee | 6136 | Bexpression* zero_expr = |
6137 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
6138 | Bexpression* check = | |
6139 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6140 | right, zero_expr, loc); | |
29a2d1d8 | 6141 | |
a32698ee | 6142 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 6143 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 6144 | Bexpression* crash = gogo->runtime_error(errcode, |
6145 | loc)->get_backend(context); | |
29a2d1d8 | 6146 | |
6147 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
93715b75 | 6148 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
6149 | ret = gogo->backend()->conditional_expression(bfn, btype, | |
6150 | check, crash, | |
ea664253 | 6151 | ret, loc); |
b13c66cd | 6152 | } |
6153 | ||
5c3f3470 | 6154 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 6155 | { |
6156 | // right == -1 | |
6157 | // FIXME: It would be nice to say that this test is expected | |
6158 | // to return false. | |
a32698ee | 6159 | |
6160 | Bexpression* neg_one_expr = | |
6161 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6162 | Bexpression* check = | |
6163 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6164 | right, neg_one_expr, loc); | |
6165 | ||
6166 | Bexpression* zero_expr = | |
6167 | gogo->backend()->integer_constant_expression(btype, zero); | |
6168 | Bexpression* one_expr = | |
6169 | gogo->backend()->integer_constant_expression(btype, one); | |
93715b75 | 6170 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
a32698ee | 6171 | |
6172 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6173 | { |
6174 | // An unsigned -1 is the largest possible number, so | |
6175 | // dividing is always 1 or 0. | |
a32698ee | 6176 | |
6177 | Bexpression* cmp = | |
6178 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6179 | left, right, loc); | |
29a2d1d8 | 6180 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6181 | overflow = |
93715b75 | 6182 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6183 | one_expr, zero_expr, |
6184 | loc); | |
29a2d1d8 | 6185 | else |
a32698ee | 6186 | overflow = |
93715b75 | 6187 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6188 | zero_expr, left, |
6189 | loc); | |
29a2d1d8 | 6190 | } |
6191 | else | |
6192 | { | |
6193 | // Computing left / -1 is the same as computing - left, | |
6194 | // which does not overflow since Go sets -fwrapv. | |
6195 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6196 | { |
6197 | Expression* negate_expr = | |
6198 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 6199 | overflow = negate_expr->get_backend(context); |
a32698ee | 6200 | } |
29a2d1d8 | 6201 | else |
a32698ee | 6202 | overflow = zero_expr; |
29a2d1d8 | 6203 | } |
a32698ee | 6204 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6205 | |
6206 | // right == -1 ? - left : ret | |
93715b75 | 6207 | ret = gogo->backend()->conditional_expression(bfn, btype, |
6208 | check, overflow, | |
a32698ee | 6209 | ret, loc); |
29a2d1d8 | 6210 | } |
e440a328 | 6211 | } |
6212 | ||
a32698ee | 6213 | mpz_clear(zero); |
6214 | mpz_clear(one); | |
6215 | mpz_clear(neg_one); | |
ea664253 | 6216 | return ret; |
e440a328 | 6217 | } |
6218 | ||
6219 | // Export a binary expression. | |
6220 | ||
6221 | void | |
6222 | Binary_expression::do_export(Export* exp) const | |
6223 | { | |
6224 | exp->write_c_string("("); | |
6225 | this->left_->export_expression(exp); | |
6226 | switch (this->op_) | |
6227 | { | |
6228 | case OPERATOR_OROR: | |
6229 | exp->write_c_string(" || "); | |
6230 | break; | |
6231 | case OPERATOR_ANDAND: | |
6232 | exp->write_c_string(" && "); | |
6233 | break; | |
6234 | case OPERATOR_EQEQ: | |
6235 | exp->write_c_string(" == "); | |
6236 | break; | |
6237 | case OPERATOR_NOTEQ: | |
6238 | exp->write_c_string(" != "); | |
6239 | break; | |
6240 | case OPERATOR_LT: | |
6241 | exp->write_c_string(" < "); | |
6242 | break; | |
6243 | case OPERATOR_LE: | |
6244 | exp->write_c_string(" <= "); | |
6245 | break; | |
6246 | case OPERATOR_GT: | |
6247 | exp->write_c_string(" > "); | |
6248 | break; | |
6249 | case OPERATOR_GE: | |
6250 | exp->write_c_string(" >= "); | |
6251 | break; | |
6252 | case OPERATOR_PLUS: | |
6253 | exp->write_c_string(" + "); | |
6254 | break; | |
6255 | case OPERATOR_MINUS: | |
6256 | exp->write_c_string(" - "); | |
6257 | break; | |
6258 | case OPERATOR_OR: | |
6259 | exp->write_c_string(" | "); | |
6260 | break; | |
6261 | case OPERATOR_XOR: | |
6262 | exp->write_c_string(" ^ "); | |
6263 | break; | |
6264 | case OPERATOR_MULT: | |
6265 | exp->write_c_string(" * "); | |
6266 | break; | |
6267 | case OPERATOR_DIV: | |
6268 | exp->write_c_string(" / "); | |
6269 | break; | |
6270 | case OPERATOR_MOD: | |
6271 | exp->write_c_string(" % "); | |
6272 | break; | |
6273 | case OPERATOR_LSHIFT: | |
6274 | exp->write_c_string(" << "); | |
6275 | break; | |
6276 | case OPERATOR_RSHIFT: | |
6277 | exp->write_c_string(" >> "); | |
6278 | break; | |
6279 | case OPERATOR_AND: | |
6280 | exp->write_c_string(" & "); | |
6281 | break; | |
6282 | case OPERATOR_BITCLEAR: | |
6283 | exp->write_c_string(" &^ "); | |
6284 | break; | |
6285 | default: | |
c3e6f413 | 6286 | go_unreachable(); |
e440a328 | 6287 | } |
6288 | this->right_->export_expression(exp); | |
6289 | exp->write_c_string(")"); | |
6290 | } | |
6291 | ||
6292 | // Import a binary expression. | |
6293 | ||
6294 | Expression* | |
6295 | Binary_expression::do_import(Import* imp) | |
6296 | { | |
6297 | imp->require_c_string("("); | |
6298 | ||
6299 | Expression* left = Expression::import_expression(imp); | |
6300 | ||
6301 | Operator op; | |
6302 | if (imp->match_c_string(" || ")) | |
6303 | { | |
6304 | op = OPERATOR_OROR; | |
6305 | imp->advance(4); | |
6306 | } | |
6307 | else if (imp->match_c_string(" && ")) | |
6308 | { | |
6309 | op = OPERATOR_ANDAND; | |
6310 | imp->advance(4); | |
6311 | } | |
6312 | else if (imp->match_c_string(" == ")) | |
6313 | { | |
6314 | op = OPERATOR_EQEQ; | |
6315 | imp->advance(4); | |
6316 | } | |
6317 | else if (imp->match_c_string(" != ")) | |
6318 | { | |
6319 | op = OPERATOR_NOTEQ; | |
6320 | imp->advance(4); | |
6321 | } | |
6322 | else if (imp->match_c_string(" < ")) | |
6323 | { | |
6324 | op = OPERATOR_LT; | |
6325 | imp->advance(3); | |
6326 | } | |
6327 | else if (imp->match_c_string(" <= ")) | |
6328 | { | |
6329 | op = OPERATOR_LE; | |
6330 | imp->advance(4); | |
6331 | } | |
6332 | else if (imp->match_c_string(" > ")) | |
6333 | { | |
6334 | op = OPERATOR_GT; | |
6335 | imp->advance(3); | |
6336 | } | |
6337 | else if (imp->match_c_string(" >= ")) | |
6338 | { | |
6339 | op = OPERATOR_GE; | |
6340 | imp->advance(4); | |
6341 | } | |
6342 | else if (imp->match_c_string(" + ")) | |
6343 | { | |
6344 | op = OPERATOR_PLUS; | |
6345 | imp->advance(3); | |
6346 | } | |
6347 | else if (imp->match_c_string(" - ")) | |
6348 | { | |
6349 | op = OPERATOR_MINUS; | |
6350 | imp->advance(3); | |
6351 | } | |
6352 | else if (imp->match_c_string(" | ")) | |
6353 | { | |
6354 | op = OPERATOR_OR; | |
6355 | imp->advance(3); | |
6356 | } | |
6357 | else if (imp->match_c_string(" ^ ")) | |
6358 | { | |
6359 | op = OPERATOR_XOR; | |
6360 | imp->advance(3); | |
6361 | } | |
6362 | else if (imp->match_c_string(" * ")) | |
6363 | { | |
6364 | op = OPERATOR_MULT; | |
6365 | imp->advance(3); | |
6366 | } | |
6367 | else if (imp->match_c_string(" / ")) | |
6368 | { | |
6369 | op = OPERATOR_DIV; | |
6370 | imp->advance(3); | |
6371 | } | |
6372 | else if (imp->match_c_string(" % ")) | |
6373 | { | |
6374 | op = OPERATOR_MOD; | |
6375 | imp->advance(3); | |
6376 | } | |
6377 | else if (imp->match_c_string(" << ")) | |
6378 | { | |
6379 | op = OPERATOR_LSHIFT; | |
6380 | imp->advance(4); | |
6381 | } | |
6382 | else if (imp->match_c_string(" >> ")) | |
6383 | { | |
6384 | op = OPERATOR_RSHIFT; | |
6385 | imp->advance(4); | |
6386 | } | |
6387 | else if (imp->match_c_string(" & ")) | |
6388 | { | |
6389 | op = OPERATOR_AND; | |
6390 | imp->advance(3); | |
6391 | } | |
6392 | else if (imp->match_c_string(" &^ ")) | |
6393 | { | |
6394 | op = OPERATOR_BITCLEAR; | |
6395 | imp->advance(4); | |
6396 | } | |
6397 | else | |
6398 | { | |
631d5788 | 6399 | go_error_at(imp->location(), "unrecognized binary operator"); |
e440a328 | 6400 | return Expression::make_error(imp->location()); |
6401 | } | |
6402 | ||
6403 | Expression* right = Expression::import_expression(imp); | |
6404 | ||
6405 | imp->require_c_string(")"); | |
6406 | ||
6407 | return Expression::make_binary(op, left, right, imp->location()); | |
6408 | } | |
6409 | ||
d751bb78 | 6410 | // Dump ast representation of a binary expression. |
6411 | ||
6412 | void | |
6413 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6414 | { | |
6415 | ast_dump_context->ostream() << "("; | |
6416 | ast_dump_context->dump_expression(this->left_); | |
6417 | ast_dump_context->ostream() << " "; | |
6418 | ast_dump_context->dump_operator(this->op_); | |
6419 | ast_dump_context->ostream() << " "; | |
6420 | ast_dump_context->dump_expression(this->right_); | |
6421 | ast_dump_context->ostream() << ") "; | |
6422 | } | |
6423 | ||
e440a328 | 6424 | // Make a binary expression. |
6425 | ||
6426 | Expression* | |
6427 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6428 | Location location) |
e440a328 | 6429 | { |
6430 | return new Binary_expression(op, left, right, location); | |
6431 | } | |
6432 | ||
6433 | // Implement a comparison. | |
6434 | ||
a32698ee | 6435 | Bexpression* |
6436 | Expression::comparison(Translate_context* context, Type* result_type, | |
6437 | Operator op, Expression* left, Expression* right, | |
6438 | Location location) | |
e440a328 | 6439 | { |
2387f644 | 6440 | Type* left_type = left->type(); |
6441 | Type* right_type = right->type(); | |
ceeb12d7 | 6442 | |
e67508fa | 6443 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6444 | |
15c67ee2 | 6445 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6446 | { |
6098d6cb | 6447 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
6448 | { | |
6449 | left = Runtime::make_call(Runtime::EQSTRING, location, 2, | |
6450 | left, right); | |
6451 | right = Expression::make_boolean(true, location); | |
6452 | } | |
6453 | else | |
6454 | { | |
6455 | left = Runtime::make_call(Runtime::CMPSTRING, location, 2, | |
6456 | left, right); | |
6457 | right = zexpr; | |
6458 | } | |
e440a328 | 6459 | } |
15c67ee2 | 6460 | else if ((left_type->interface_type() != NULL |
6461 | && right_type->interface_type() == NULL | |
6462 | && !right_type->is_nil_type()) | |
6463 | || (left_type->interface_type() == NULL | |
6464 | && !left_type->is_nil_type() | |
6465 | && right_type->interface_type() != NULL)) | |
e440a328 | 6466 | { |
6467 | // Comparing an interface value to a non-interface value. | |
6468 | if (left_type->interface_type() == NULL) | |
6469 | { | |
6470 | std::swap(left_type, right_type); | |
2387f644 | 6471 | std::swap(left, right); |
e440a328 | 6472 | } |
6473 | ||
6474 | // The right operand is not an interface. We need to take its | |
6475 | // address if it is not a pointer. | |
ceeb12d7 | 6476 | Expression* pointer_arg = NULL; |
e440a328 | 6477 | if (right_type->points_to() != NULL) |
2387f644 | 6478 | pointer_arg = right; |
e440a328 | 6479 | else |
6480 | { | |
2387f644 | 6481 | go_assert(right->is_addressable()); |
6482 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6483 | location); |
e440a328 | 6484 | } |
e440a328 | 6485 | |
2387f644 | 6486 | Expression* descriptor = |
6487 | Expression::make_type_descriptor(right_type, location); | |
6488 | left = | |
ceeb12d7 | 6489 | Runtime::make_call((left_type->interface_type()->is_empty() |
6098d6cb | 6490 | ? Runtime::EFACEVALEQ |
6491 | : Runtime::IFACEVALEQ), | |
2387f644 | 6492 | location, 3, left, descriptor, |
ceeb12d7 | 6493 | pointer_arg); |
6098d6cb | 6494 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6495 | right = Expression::make_boolean(true, location); | |
e440a328 | 6496 | } |
6497 | else if (left_type->interface_type() != NULL | |
6498 | && right_type->interface_type() != NULL) | |
6499 | { | |
ceeb12d7 | 6500 | Runtime::Function compare_function; |
739bad04 | 6501 | if (left_type->interface_type()->is_empty() |
6502 | && right_type->interface_type()->is_empty()) | |
6098d6cb | 6503 | compare_function = Runtime::EFACEEQ; |
739bad04 | 6504 | else if (!left_type->interface_type()->is_empty() |
6505 | && !right_type->interface_type()->is_empty()) | |
6098d6cb | 6506 | compare_function = Runtime::IFACEEQ; |
739bad04 | 6507 | else |
6508 | { | |
6509 | if (left_type->interface_type()->is_empty()) | |
6510 | { | |
739bad04 | 6511 | std::swap(left_type, right_type); |
2387f644 | 6512 | std::swap(left, right); |
739bad04 | 6513 | } |
c484d925 | 6514 | go_assert(!left_type->interface_type()->is_empty()); |
6515 | go_assert(right_type->interface_type()->is_empty()); | |
6098d6cb | 6516 | compare_function = Runtime::IFACEEFACEEQ; |
739bad04 | 6517 | } |
6518 | ||
2387f644 | 6519 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6098d6cb | 6520 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6521 | right = Expression::make_boolean(true, location); | |
e440a328 | 6522 | } |
6523 | ||
6524 | if (left_type->is_nil_type() | |
6525 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6526 | { | |
6527 | std::swap(left_type, right_type); | |
2387f644 | 6528 | std::swap(left, right); |
e440a328 | 6529 | } |
6530 | ||
6531 | if (right_type->is_nil_type()) | |
6532 | { | |
2387f644 | 6533 | right = Expression::make_nil(location); |
e440a328 | 6534 | if (left_type->array_type() != NULL |
6535 | && left_type->array_type()->length() == NULL) | |
6536 | { | |
6537 | Array_type* at = left_type->array_type(); | |
44dbe1d7 | 6538 | bool is_lvalue = false; |
6539 | left = at->get_value_pointer(context->gogo(), left, is_lvalue); | |
e440a328 | 6540 | } |
6541 | else if (left_type->interface_type() != NULL) | |
6542 | { | |
6543 | // An interface is nil if the first field is nil. | |
2387f644 | 6544 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6545 | } |
6546 | } | |
6547 | ||
ea664253 | 6548 | Bexpression* left_bexpr = left->get_backend(context); |
6549 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6550 | |
a32698ee | 6551 | Gogo* gogo = context->gogo(); |
6552 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6553 | right_bexpr, location); | |
6554 | if (result_type != NULL) | |
6555 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6556 | ret, location); | |
e440a328 | 6557 | return ret; |
6558 | } | |
6559 | ||
736a16ba | 6560 | // Class String_concat_expression. |
6561 | ||
6562 | bool | |
6563 | String_concat_expression::do_is_constant() const | |
6564 | { | |
6565 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6566 | pe != this->exprs_->end(); | |
6567 | ++pe) | |
6568 | { | |
6569 | if (!(*pe)->is_constant()) | |
6570 | return false; | |
6571 | } | |
6572 | return true; | |
6573 | } | |
6574 | ||
6575 | bool | |
3ae06f68 | 6576 | String_concat_expression::do_is_static_initializer() const |
736a16ba | 6577 | { |
6578 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6579 | pe != this->exprs_->end(); | |
6580 | ++pe) | |
6581 | { | |
3ae06f68 | 6582 | if (!(*pe)->is_static_initializer()) |
736a16ba | 6583 | return false; |
6584 | } | |
6585 | return true; | |
6586 | } | |
6587 | ||
6588 | Type* | |
6589 | String_concat_expression::do_type() | |
6590 | { | |
6591 | Type* t = this->exprs_->front()->type(); | |
6592 | Expression_list::iterator pe = this->exprs_->begin(); | |
6593 | ++pe; | |
6594 | for (; pe != this->exprs_->end(); ++pe) | |
6595 | { | |
6596 | Type* t1; | |
6597 | if (!Binary_expression::operation_type(OPERATOR_PLUS, t, | |
6598 | (*pe)->type(), | |
6599 | &t1)) | |
6600 | return Type::make_error_type(); | |
6601 | t = t1; | |
6602 | } | |
6603 | return t; | |
6604 | } | |
6605 | ||
6606 | void | |
6607 | String_concat_expression::do_determine_type(const Type_context* context) | |
6608 | { | |
6609 | Type_context subcontext(*context); | |
6610 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6611 | pe != this->exprs_->end(); | |
6612 | ++pe) | |
6613 | { | |
6614 | Type* t = (*pe)->type(); | |
6615 | if (!t->is_abstract()) | |
6616 | { | |
6617 | subcontext.type = t; | |
6618 | break; | |
6619 | } | |
6620 | } | |
6621 | if (subcontext.type == NULL) | |
6622 | subcontext.type = this->exprs_->front()->type(); | |
6623 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6624 | pe != this->exprs_->end(); | |
6625 | ++pe) | |
6626 | (*pe)->determine_type(&subcontext); | |
6627 | } | |
6628 | ||
6629 | void | |
6630 | String_concat_expression::do_check_types(Gogo*) | |
6631 | { | |
6632 | if (this->is_error_expression()) | |
6633 | return; | |
6634 | Type* t = this->exprs_->front()->type(); | |
6635 | if (t->is_error()) | |
6636 | { | |
6637 | this->set_is_error(); | |
6638 | return; | |
6639 | } | |
6640 | Expression_list::iterator pe = this->exprs_->begin(); | |
6641 | ++pe; | |
6642 | for (; pe != this->exprs_->end(); ++pe) | |
6643 | { | |
6644 | Type* t1 = (*pe)->type(); | |
6645 | if (!Type::are_compatible_for_binop(t, t1)) | |
6646 | { | |
6647 | this->report_error("incompatible types in binary expression"); | |
6648 | return; | |
6649 | } | |
6650 | if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, | |
6651 | this->location())) | |
6652 | { | |
6653 | this->set_is_error(); | |
6654 | return; | |
6655 | } | |
6656 | } | |
6657 | } | |
6658 | ||
6659 | Expression* | |
6660 | String_concat_expression::do_flatten(Gogo*, Named_object*, | |
6661 | Statement_inserter*) | |
6662 | { | |
6663 | if (this->is_error_expression()) | |
6664 | return this; | |
6665 | Location loc = this->location(); | |
6666 | Type* type = this->type(); | |
6667 | Expression* nil_arg = Expression::make_nil(loc); | |
6668 | Expression* call; | |
6669 | switch (this->exprs_->size()) | |
6670 | { | |
6671 | case 0: case 1: | |
6672 | go_unreachable(); | |
6673 | ||
6674 | case 2: case 3: case 4: case 5: | |
6675 | { | |
6676 | Expression* len = Expression::make_integer_ul(this->exprs_->size(), | |
6677 | NULL, loc); | |
6678 | Array_type* arg_type = Type::make_array_type(type, len); | |
6679 | arg_type->set_is_array_incomparable(); | |
6680 | Expression* arg = | |
6681 | Expression::make_array_composite_literal(arg_type, this->exprs_, | |
6682 | loc); | |
6683 | Runtime::Function code; | |
6684 | switch (this->exprs_->size()) | |
6685 | { | |
6686 | default: | |
6687 | go_unreachable(); | |
6688 | case 2: | |
6689 | code = Runtime::CONCATSTRING2; | |
6690 | break; | |
6691 | case 3: | |
6692 | code = Runtime::CONCATSTRING3; | |
6693 | break; | |
6694 | case 4: | |
6695 | code = Runtime::CONCATSTRING4; | |
6696 | break; | |
6697 | case 5: | |
6698 | code = Runtime::CONCATSTRING5; | |
6699 | break; | |
6700 | } | |
6701 | call = Runtime::make_call(code, loc, 2, nil_arg, arg); | |
6702 | } | |
6703 | break; | |
6704 | ||
6705 | default: | |
6706 | { | |
6707 | Type* arg_type = Type::make_array_type(type, NULL); | |
6708 | Slice_construction_expression* sce = | |
6709 | Expression::make_slice_composite_literal(arg_type, this->exprs_, | |
6710 | loc); | |
6711 | sce->set_storage_does_not_escape(); | |
6712 | call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg, | |
6713 | sce); | |
6714 | } | |
6715 | break; | |
6716 | } | |
6717 | ||
6718 | return Expression::make_cast(type, call, loc); | |
6719 | } | |
6720 | ||
6721 | void | |
6722 | String_concat_expression::do_dump_expression( | |
6723 | Ast_dump_context* ast_dump_context) const | |
6724 | { | |
6725 | ast_dump_context->ostream() << "concat("; | |
6726 | ast_dump_context->dump_expression_list(this->exprs_, false); | |
6727 | ast_dump_context->ostream() << ")"; | |
6728 | } | |
6729 | ||
6730 | Expression* | |
6731 | Expression::make_string_concat(Expression_list* exprs) | |
6732 | { | |
6733 | return new String_concat_expression(exprs); | |
6734 | } | |
6735 | ||
e440a328 | 6736 | // Class Bound_method_expression. |
6737 | ||
6738 | // Traversal. | |
6739 | ||
6740 | int | |
6741 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6742 | { | |
e0659c9e | 6743 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6744 | } |
6745 | ||
6746 | // Return the type of a bound method expression. The type of this | |
0afbb937 | 6747 | // object is simply the type of the method with no receiver. |
e440a328 | 6748 | |
6749 | Type* | |
6750 | Bound_method_expression::do_type() | |
6751 | { | |
0afbb937 | 6752 | Named_object* fn = this->method_->named_object(); |
6753 | Function_type* fntype; | |
6754 | if (fn->is_function()) | |
6755 | fntype = fn->func_value()->type(); | |
6756 | else if (fn->is_function_declaration()) | |
6757 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6758 | else |
6759 | return Type::make_error_type(); | |
0afbb937 | 6760 | return fntype->copy_without_receiver(); |
e440a328 | 6761 | } |
6762 | ||
6763 | // Determine the types of a method expression. | |
6764 | ||
6765 | void | |
6766 | Bound_method_expression::do_determine_type(const Type_context*) | |
6767 | { | |
0afbb937 | 6768 | Named_object* fn = this->method_->named_object(); |
6769 | Function_type* fntype; | |
6770 | if (fn->is_function()) | |
6771 | fntype = fn->func_value()->type(); | |
6772 | else if (fn->is_function_declaration()) | |
6773 | fntype = fn->func_declaration_value()->type(); | |
6774 | else | |
6775 | fntype = NULL; | |
e440a328 | 6776 | if (fntype == NULL || !fntype->is_method()) |
6777 | this->expr_->determine_type_no_context(); | |
6778 | else | |
6779 | { | |
6780 | Type_context subcontext(fntype->receiver()->type(), false); | |
6781 | this->expr_->determine_type(&subcontext); | |
6782 | } | |
6783 | } | |
6784 | ||
6785 | // Check the types of a method expression. | |
6786 | ||
6787 | void | |
6788 | Bound_method_expression::do_check_types(Gogo*) | |
6789 | { | |
0afbb937 | 6790 | Named_object* fn = this->method_->named_object(); |
6791 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6792 | { | |
6793 | this->report_error(_("object is not a method")); | |
6794 | return; | |
6795 | } | |
6796 | ||
6797 | Function_type* fntype; | |
6798 | if (fn->is_function()) | |
6799 | fntype = fn->func_value()->type(); | |
6800 | else if (fn->is_function_declaration()) | |
6801 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6802 | else |
0afbb937 | 6803 | go_unreachable(); |
6804 | Type* rtype = fntype->receiver()->type()->deref(); | |
6805 | Type* etype = (this->expr_type_ != NULL | |
6806 | ? this->expr_type_ | |
6807 | : this->expr_->type()); | |
6808 | etype = etype->deref(); | |
6809 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6810 | this->report_error(_("method type does not match object type")); | |
6811 | } | |
6812 | ||
6813 | // If a bound method expression is not simply called, then it is | |
6814 | // represented as a closure. The closure will hold a single variable, | |
6815 | // the receiver to pass to the method. The function will be a simple | |
6816 | // thunk that pulls that value from the closure and calls the method | |
6817 | // with the remaining arguments. | |
6818 | // | |
6819 | // Because method values are not common, we don't build all thunks for | |
6820 | // every methods, but instead only build them as we need them. In | |
6821 | // particular, we even build them on demand for methods defined in | |
6822 | // other packages. | |
6823 | ||
6824 | Bound_method_expression::Method_value_thunks | |
6825 | Bound_method_expression::method_value_thunks; | |
6826 | ||
6827 | // Find or create the thunk for METHOD. | |
6828 | ||
6829 | Named_object* | |
6830 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6831 | Named_object* fn) | |
6832 | { | |
6833 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6834 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6835 | Bound_method_expression::method_value_thunks.insert(val); | |
6836 | if (!ins.second) | |
6837 | { | |
6838 | // We have seen this method before. | |
6839 | go_assert(ins.first->second != NULL); | |
6840 | return ins.first->second; | |
6841 | } | |
6842 | ||
6843 | Location loc = fn->location(); | |
6844 | ||
6845 | Function_type* orig_fntype; | |
6846 | if (fn->is_function()) | |
6847 | orig_fntype = fn->func_value()->type(); | |
6848 | else if (fn->is_function_declaration()) | |
6849 | orig_fntype = fn->func_declaration_value()->type(); | |
6850 | else | |
6851 | orig_fntype = NULL; | |
6852 | ||
6853 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6854 | { |
0afbb937 | 6855 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6856 | return ins.first->second; | |
e440a328 | 6857 | } |
0afbb937 | 6858 | |
6859 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6860 | // The type here is wrong--it should be the C function type. But it |
6861 | // doesn't really matter. | |
0afbb937 | 6862 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6863 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6864 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6865 | orig_fntype->receiver()->type(), | |
6866 | loc))); | |
6bf4793c | 6867 | Struct_type* st = Type::make_struct_type(sfl, loc); |
6868 | st->set_is_struct_incomparable(); | |
6869 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 6870 | |
f8bdf81a | 6871 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6872 | |
da244e59 | 6873 | std::string thunk_name = Gogo::thunk_name(); |
6874 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 6875 | false, loc); |
6876 | ||
f8bdf81a | 6877 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6878 | cvar->set_is_used(); | |
1ecc6157 | 6879 | cvar->set_is_closure(); |
da244e59 | 6880 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6881 | NULL, cvar); | |
f8bdf81a | 6882 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6883 | |
f8bdf81a | 6884 | gogo->start_block(loc); |
0afbb937 | 6885 | |
6886 | // Field 0 of the closure is the function code pointer, field 1 is | |
6887 | // the value on which to invoke the method. | |
6888 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 6889 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 6890 | arg = Expression::make_field_reference(arg, 1, loc); |
6891 | ||
6892 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6893 | ||
6894 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6895 | Expression_list* args; | |
6896 | if (orig_params == NULL || orig_params->empty()) | |
6897 | args = NULL; | |
6898 | else | |
6899 | { | |
6900 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6901 | args = new Expression_list(); | |
6902 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6903 | p != new_params->end(); |
0afbb937 | 6904 | ++p) |
6905 | { | |
6906 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6907 | go_assert(p_no != NULL | |
6908 | && p_no->is_variable() | |
6909 | && p_no->var_value()->is_parameter()); | |
6910 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6911 | } | |
6912 | } | |
6913 | ||
6914 | Call_expression* call = Expression::make_call(bme, args, | |
6915 | orig_fntype->is_varargs(), | |
6916 | loc); | |
6917 | call->set_varargs_are_lowered(); | |
6918 | ||
6919 | Statement* s = Statement::make_return_from_call(call, loc); | |
6920 | gogo->add_statement(s); | |
6921 | Block* b = gogo->finish_block(loc); | |
6922 | gogo->add_block(b, loc); | |
6923 | gogo->lower_block(new_no, b); | |
a32698ee | 6924 | gogo->flatten_block(new_no, b); |
0afbb937 | 6925 | gogo->finish_function(loc); |
6926 | ||
6927 | ins.first->second = new_no; | |
6928 | return new_no; | |
6929 | } | |
6930 | ||
6931 | // Return an expression to check *REF for nil while dereferencing | |
6932 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6933 | // reference. This is a static function so that we don't have to | |
6934 | // worry about declaring Field_indexes in expressions.h. | |
6935 | ||
6936 | static Expression* | |
6937 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6938 | Expression** ref) | |
6939 | { | |
6940 | if (field_indexes == NULL) | |
6941 | return Expression::make_boolean(false, loc); | |
6942 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6943 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6944 | go_assert(stype != NULL | |
6945 | && field_indexes->field_index < stype->field_count()); | |
6946 | if ((*ref)->type()->struct_type() == NULL) | |
6947 | { | |
6948 | go_assert((*ref)->type()->points_to() != NULL); | |
6949 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6950 | Expression::make_nil(loc), | |
6951 | loc); | |
6952 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
f614ea8b | 6953 | *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT, |
6954 | loc); | |
0afbb937 | 6955 | go_assert((*ref)->type()->struct_type() == stype); |
6956 | } | |
6957 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6958 | loc); | |
6959 | return cond; | |
e440a328 | 6960 | } |
6961 | ||
cd39797e | 6962 | // Flatten a method value into a struct with nil checks. We can't do |
6963 | // this in the lowering phase, because if the method value is called | |
6964 | // directly we don't need a thunk. That case will have been handled | |
6965 | // by Call_expression::do_lower, so if we get here then we do need a | |
6966 | // thunk. | |
e440a328 | 6967 | |
cd39797e | 6968 | Expression* |
6969 | Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, | |
6970 | Statement_inserter* inserter) | |
e440a328 | 6971 | { |
cd39797e | 6972 | Location loc = this->location(); |
6973 | ||
6974 | Named_object* thunk = Bound_method_expression::create_thunk(gogo, | |
0afbb937 | 6975 | this->method_, |
6976 | this->function_); | |
6977 | if (thunk->is_erroneous()) | |
6978 | { | |
6979 | go_assert(saw_errors()); | |
cd39797e | 6980 | return Expression::make_error(loc); |
0afbb937 | 6981 | } |
6982 | ||
cd39797e | 6983 | // Force the expression into a variable. This is only necessary if |
6984 | // we are going to do nil checks below, but it's easy enough to | |
6985 | // always do it. | |
6986 | Expression* expr = this->expr_; | |
6987 | if (!expr->is_variable()) | |
6988 | { | |
6989 | Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); | |
6990 | inserter->insert(etemp); | |
6991 | expr = Expression::make_temporary_reference(etemp, loc); | |
6992 | } | |
0afbb937 | 6993 | |
6994 | // If the method expects a value, and we have a pointer, we need to | |
6995 | // dereference the pointer. | |
6996 | ||
6997 | Named_object* fn = this->method_->named_object(); | |
cd39797e | 6998 | Function_type *fntype; |
0afbb937 | 6999 | if (fn->is_function()) |
7000 | fntype = fn->func_value()->type(); | |
7001 | else if (fn->is_function_declaration()) | |
7002 | fntype = fn->func_declaration_value()->type(); | |
7003 | else | |
7004 | go_unreachable(); | |
7005 | ||
cd39797e | 7006 | Expression* val = expr; |
0afbb937 | 7007 | if (fntype->receiver()->type()->points_to() == NULL |
7008 | && val->type()->points_to() != NULL) | |
f614ea8b | 7009 | val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc); |
0afbb937 | 7010 | |
7011 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
7012 | // expect a closure whose second field has type this->expr_type_ (if | |
7013 | // that is not NULL). We are going to pass it a closure whose | |
7014 | // second field has type this->expr_->type(). Since | |
7015 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
7016 | // away with this. | |
7017 | ||
7018 | Struct_field_list* fields = new Struct_field_list(); | |
7019 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
7020 | thunk->func_value()->type(), | |
7021 | loc))); | |
7022 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
7023 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6bf4793c | 7024 | st->set_is_struct_incomparable(); |
0afbb937 | 7025 | |
7026 | Expression_list* vals = new Expression_list(); | |
7027 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
7028 | vals->push_back(val); | |
7029 | ||
7030 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
0afbb937 | 7031 | |
cd39797e | 7032 | if (!gogo->compiling_runtime() || gogo->package_name() != "runtime") |
7033 | ret = Expression::make_heap_expression(ret, loc); | |
7034 | else | |
7035 | { | |
7036 | // When compiling the runtime, method closures do not escape. | |
7037 | // When escape analysis becomes the default, and applies to | |
7038 | // method closures, this should be changed to make it an error | |
7039 | // if a method closure escapes. | |
7040 | Temporary_statement* ctemp = Statement::make_temporary(st, ret, loc); | |
7041 | inserter->insert(ctemp); | |
7042 | ret = Expression::make_temporary_reference(ctemp, loc); | |
7043 | ret = Expression::make_unary(OPERATOR_AND, ret, loc); | |
7044 | ret->unary_expression()->set_does_not_escape(); | |
7045 | } | |
7046 | ||
7047 | // If necessary, check whether the expression or any embedded | |
7048 | // pointers are nil. | |
0afbb937 | 7049 | |
df7ef1fd | 7050 | Expression* nil_check = NULL; |
0afbb937 | 7051 | if (this->method_->field_indexes() != NULL) |
7052 | { | |
0afbb937 | 7053 | Expression* ref = expr; |
7054 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
7055 | expr = ref; | |
7056 | } | |
7057 | ||
7058 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
7059 | { | |
7060 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
7061 | Expression::make_nil(loc), | |
7062 | loc); | |
7063 | if (nil_check == NULL) | |
7064 | nil_check = n; | |
7065 | else | |
7066 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
7067 | } | |
7068 | ||
7069 | if (nil_check != NULL) | |
7070 | { | |
cd39797e | 7071 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
7072 | loc); | |
7073 | // Fix the type of the conditional expression by pretending to | |
7074 | // evaluate to RET either way through the conditional. | |
7075 | crash = Expression::make_compound(crash, ret, loc); | |
7076 | ret = Expression::make_conditional(nil_check, crash, ret, loc); | |
7077 | } | |
7078 | ||
7079 | // RET is a pointer to a struct, but we want a function type. | |
7080 | ret = Expression::make_unsafe_cast(this->type(), ret, loc); | |
7081 | ||
7082 | return ret; | |
e440a328 | 7083 | } |
7084 | ||
d751bb78 | 7085 | // Dump ast representation of a bound method expression. |
7086 | ||
7087 | void | |
7088 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
7089 | const | |
7090 | { | |
7091 | if (this->expr_type_ != NULL) | |
7092 | ast_dump_context->ostream() << "("; | |
7093 | ast_dump_context->dump_expression(this->expr_); | |
7094 | if (this->expr_type_ != NULL) | |
7095 | { | |
7096 | ast_dump_context->ostream() << ":"; | |
7097 | ast_dump_context->dump_type(this->expr_type_); | |
7098 | ast_dump_context->ostream() << ")"; | |
7099 | } | |
7100 | ||
0afbb937 | 7101 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 7102 | } |
7103 | ||
e440a328 | 7104 | // Make a method expression. |
7105 | ||
7106 | Bound_method_expression* | |
0afbb937 | 7107 | Expression::make_bound_method(Expression* expr, const Method* method, |
7108 | Named_object* function, Location location) | |
e440a328 | 7109 | { |
0afbb937 | 7110 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 7111 | } |
7112 | ||
7113 | // Class Builtin_call_expression. This is used for a call to a | |
7114 | // builtin function. | |
7115 | ||
e440a328 | 7116 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, |
7117 | Expression* fn, | |
7118 | Expression_list* args, | |
7119 | bool is_varargs, | |
b13c66cd | 7120 | Location location) |
e440a328 | 7121 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 7122 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
7123 | recover_arg_is_set_(false) | |
e440a328 | 7124 | { |
7125 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 7126 | if (fnexp == NULL) |
7127 | { | |
7128 | this->code_ = BUILTIN_INVALID; | |
7129 | return; | |
7130 | } | |
e440a328 | 7131 | const std::string& name(fnexp->named_object()->name()); |
7132 | if (name == "append") | |
7133 | this->code_ = BUILTIN_APPEND; | |
7134 | else if (name == "cap") | |
7135 | this->code_ = BUILTIN_CAP; | |
7136 | else if (name == "close") | |
7137 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 7138 | else if (name == "complex") |
7139 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 7140 | else if (name == "copy") |
7141 | this->code_ = BUILTIN_COPY; | |
1cce762f | 7142 | else if (name == "delete") |
7143 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 7144 | else if (name == "imag") |
7145 | this->code_ = BUILTIN_IMAG; | |
7146 | else if (name == "len") | |
7147 | this->code_ = BUILTIN_LEN; | |
7148 | else if (name == "make") | |
7149 | this->code_ = BUILTIN_MAKE; | |
7150 | else if (name == "new") | |
7151 | this->code_ = BUILTIN_NEW; | |
7152 | else if (name == "panic") | |
7153 | this->code_ = BUILTIN_PANIC; | |
7154 | else if (name == "print") | |
7155 | this->code_ = BUILTIN_PRINT; | |
7156 | else if (name == "println") | |
7157 | this->code_ = BUILTIN_PRINTLN; | |
7158 | else if (name == "real") | |
7159 | this->code_ = BUILTIN_REAL; | |
7160 | else if (name == "recover") | |
7161 | this->code_ = BUILTIN_RECOVER; | |
7162 | else if (name == "Alignof") | |
7163 | this->code_ = BUILTIN_ALIGNOF; | |
7164 | else if (name == "Offsetof") | |
7165 | this->code_ = BUILTIN_OFFSETOF; | |
7166 | else if (name == "Sizeof") | |
7167 | this->code_ = BUILTIN_SIZEOF; | |
7168 | else | |
c3e6f413 | 7169 | go_unreachable(); |
e440a328 | 7170 | } |
7171 | ||
7172 | // Return whether this is a call to recover. This is a virtual | |
7173 | // function called from the parent class. | |
7174 | ||
7175 | bool | |
7176 | Builtin_call_expression::do_is_recover_call() const | |
7177 | { | |
7178 | if (this->classification() == EXPRESSION_ERROR) | |
7179 | return false; | |
7180 | return this->code_ == BUILTIN_RECOVER; | |
7181 | } | |
7182 | ||
7183 | // Set the argument for a call to recover. | |
7184 | ||
7185 | void | |
7186 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7187 | { | |
7188 | const Expression_list* args = this->args(); | |
c484d925 | 7189 | go_assert(args == NULL || args->empty()); |
e440a328 | 7190 | Expression_list* new_args = new Expression_list(); |
7191 | new_args->push_back(arg); | |
7192 | this->set_args(new_args); | |
6334270b | 7193 | this->recover_arg_is_set_ = true; |
e440a328 | 7194 | } |
7195 | ||
e440a328 | 7196 | // Lower a builtin call expression. This turns new and make into |
7197 | // specific expressions. We also convert to a constant if we can. | |
7198 | ||
7199 | Expression* | |
321e5ad2 | 7200 | Builtin_call_expression::do_lower(Gogo*, Named_object* function, |
ceeb4318 | 7201 | Statement_inserter* inserter, int) |
e440a328 | 7202 | { |
79651b1f | 7203 | if (this->is_error_expression()) |
a9182619 | 7204 | return this; |
7205 | ||
b13c66cd | 7206 | Location loc = this->location(); |
1cce762f | 7207 | |
a8725655 | 7208 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7209 | { | |
7210 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7211 | return Expression::make_error(loc); |
a8725655 | 7212 | } |
7213 | ||
393ba00b | 7214 | if (this->code_ == BUILTIN_OFFSETOF) |
7215 | { | |
7216 | Expression* arg = this->one_arg(); | |
12e69faa | 7217 | |
7218 | if (arg->bound_method_expression() != NULL | |
7219 | || arg->interface_field_reference_expression() != NULL) | |
7220 | { | |
7221 | this->report_error(_("invalid use of method value as argument " | |
7222 | "of Offsetof")); | |
7223 | return this; | |
7224 | } | |
7225 | ||
393ba00b | 7226 | Field_reference_expression* farg = arg->field_reference_expression(); |
7227 | while (farg != NULL) | |
7228 | { | |
7229 | if (!farg->implicit()) | |
7230 | break; | |
7231 | // When the selector refers to an embedded field, | |
7232 | // it must not be reached through pointer indirections. | |
7233 | if (farg->expr()->deref() != farg->expr()) | |
7234 | { | |
12e69faa | 7235 | this->report_error(_("argument of Offsetof implies " |
7236 | "indirection of an embedded field")); | |
393ba00b | 7237 | return this; |
7238 | } | |
7239 | // Go up until we reach the original base. | |
7240 | farg = farg->expr()->field_reference_expression(); | |
7241 | } | |
7242 | } | |
7243 | ||
1cce762f | 7244 | if (this->is_constant()) |
e440a328 | 7245 | { |
0c77715b | 7246 | Numeric_constant nc; |
7247 | if (this->numeric_constant_value(&nc)) | |
7248 | return nc.expression(loc); | |
e440a328 | 7249 | } |
1cce762f | 7250 | |
7251 | switch (this->code_) | |
e440a328 | 7252 | { |
1cce762f | 7253 | default: |
7254 | break; | |
7255 | ||
7256 | case BUILTIN_NEW: | |
7257 | { | |
7258 | const Expression_list* args = this->args(); | |
7259 | if (args == NULL || args->size() < 1) | |
7260 | this->report_error(_("not enough arguments")); | |
7261 | else if (args->size() > 1) | |
7262 | this->report_error(_("too many arguments")); | |
7263 | else | |
7264 | { | |
7265 | Expression* arg = args->front(); | |
7266 | if (!arg->is_type_expression()) | |
7267 | { | |
631d5788 | 7268 | go_error_at(arg->location(), "expected type"); |
1cce762f | 7269 | this->set_is_error(); |
7270 | } | |
7271 | else | |
7272 | return Expression::make_allocation(arg->type(), loc); | |
7273 | } | |
7274 | } | |
7275 | break; | |
7276 | ||
7277 | case BUILTIN_MAKE: | |
321e5ad2 | 7278 | return this->lower_make(inserter); |
1cce762f | 7279 | |
7280 | case BUILTIN_RECOVER: | |
e440a328 | 7281 | if (function != NULL) |
7282 | function->func_value()->set_calls_recover(); | |
7283 | else | |
7284 | { | |
7285 | // Calling recover outside of a function always returns the | |
7286 | // nil empty interface. | |
823c7e3d | 7287 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7288 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7289 | } |
1cce762f | 7290 | break; |
7291 | ||
1cce762f | 7292 | case BUILTIN_DELETE: |
7293 | { | |
7294 | // Lower to a runtime function call. | |
7295 | const Expression_list* args = this->args(); | |
7296 | if (args == NULL || args->size() < 2) | |
7297 | this->report_error(_("not enough arguments")); | |
7298 | else if (args->size() > 2) | |
7299 | this->report_error(_("too many arguments")); | |
7300 | else if (args->front()->type()->map_type() == NULL) | |
7301 | this->report_error(_("argument 1 must be a map")); | |
7302 | else | |
7303 | { | |
7304 | // Since this function returns no value it must appear in | |
7305 | // a statement by itself, so we don't have to worry about | |
7306 | // order of evaluation of values around it. Evaluate the | |
7307 | // map first to get order of evaluation right. | |
7308 | Map_type* mt = args->front()->type()->map_type(); | |
7309 | Temporary_statement* map_temp = | |
7310 | Statement::make_temporary(mt, args->front(), loc); | |
7311 | inserter->insert(map_temp); | |
7312 | ||
7313 | Temporary_statement* key_temp = | |
7314 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7315 | inserter->insert(key_temp); | |
7316 | ||
0d5530d9 | 7317 | Expression* e1 = Expression::make_type_descriptor(mt, loc); |
7318 | Expression* e2 = Expression::make_temporary_reference(map_temp, | |
1cce762f | 7319 | loc); |
0d5530d9 | 7320 | Expression* e3 = Expression::make_temporary_reference(key_temp, |
1cce762f | 7321 | loc); |
0d5530d9 | 7322 | e3 = Expression::make_unary(OPERATOR_AND, e3, loc); |
1cce762f | 7323 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), |
0d5530d9 | 7324 | 3, e1, e2, e3); |
1cce762f | 7325 | } |
7326 | } | |
7327 | break; | |
88b03a70 | 7328 | |
7329 | case BUILTIN_PRINT: | |
7330 | case BUILTIN_PRINTLN: | |
7331 | // Force all the arguments into temporary variables, so that we | |
7332 | // don't try to evaluate something while holding the print lock. | |
7333 | if (this->args() == NULL) | |
7334 | break; | |
7335 | for (Expression_list::iterator pa = this->args()->begin(); | |
7336 | pa != this->args()->end(); | |
7337 | ++pa) | |
7338 | { | |
493ce3ee | 7339 | if (!(*pa)->is_variable() && !(*pa)->is_constant()) |
88b03a70 | 7340 | { |
7341 | Temporary_statement* temp = | |
7342 | Statement::make_temporary(NULL, *pa, loc); | |
7343 | inserter->insert(temp); | |
7344 | *pa = Expression::make_temporary_reference(temp, loc); | |
7345 | } | |
7346 | } | |
7347 | break; | |
e440a328 | 7348 | } |
7349 | ||
7350 | return this; | |
7351 | } | |
7352 | ||
35a54f17 | 7353 | // Flatten a builtin call expression. This turns the arguments of copy and |
7354 | // append into temporary expressions. | |
7355 | ||
7356 | Expression* | |
321e5ad2 | 7357 | Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function, |
35a54f17 | 7358 | Statement_inserter* inserter) |
7359 | { | |
16cb7fec | 7360 | Location loc = this->location(); |
7361 | ||
7362 | switch (this->code_) | |
35a54f17 | 7363 | { |
16cb7fec | 7364 | default: |
7365 | break; | |
7366 | ||
7367 | case BUILTIN_APPEND: | |
321e5ad2 | 7368 | return this->flatten_append(gogo, function, inserter); |
7369 | ||
16cb7fec | 7370 | case BUILTIN_COPY: |
7371 | { | |
7372 | Type* at = this->args()->front()->type(); | |
7373 | for (Expression_list::iterator pa = this->args()->begin(); | |
7374 | pa != this->args()->end(); | |
7375 | ++pa) | |
7376 | { | |
7377 | if ((*pa)->is_nil_expression()) | |
7378 | { | |
7379 | Expression* nil = Expression::make_nil(loc); | |
7380 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
7381 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
7382 | } | |
7383 | if (!(*pa)->is_variable()) | |
7384 | { | |
7385 | Temporary_statement* temp = | |
7386 | Statement::make_temporary(NULL, *pa, loc); | |
7387 | inserter->insert(temp); | |
7388 | *pa = Expression::make_temporary_reference(temp, loc); | |
7389 | } | |
7390 | } | |
7391 | } | |
7392 | break; | |
7393 | ||
7394 | case BUILTIN_PANIC: | |
35a54f17 | 7395 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 7396 | pa != this->args()->end(); |
7397 | ++pa) | |
7398 | { | |
7399 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 7400 | { |
16cb7fec | 7401 | Temporary_statement* temp = |
7402 | Statement::make_temporary(NULL, *pa, loc); | |
7403 | inserter->insert(temp); | |
7404 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 7405 | } |
16cb7fec | 7406 | } |
7739537f | 7407 | break; |
0d5530d9 | 7408 | |
7409 | case BUILTIN_LEN: | |
132ed071 | 7410 | case BUILTIN_CAP: |
321e5ad2 | 7411 | { |
7412 | Expression_list::iterator pa = this->args()->begin(); | |
7413 | if (!(*pa)->is_variable() | |
7414 | && ((*pa)->type()->map_type() != NULL | |
7415 | || (*pa)->type()->channel_type() != NULL)) | |
7416 | { | |
7417 | Temporary_statement* temp = | |
7418 | Statement::make_temporary(NULL, *pa, loc); | |
7419 | inserter->insert(temp); | |
7420 | *pa = Expression::make_temporary_reference(temp, loc); | |
7421 | } | |
7422 | } | |
7423 | break; | |
35a54f17 | 7424 | } |
16cb7fec | 7425 | |
35a54f17 | 7426 | return this; |
7427 | } | |
7428 | ||
a9182619 | 7429 | // Lower a make expression. |
7430 | ||
7431 | Expression* | |
321e5ad2 | 7432 | Builtin_call_expression::lower_make(Statement_inserter* inserter) |
a9182619 | 7433 | { |
b13c66cd | 7434 | Location loc = this->location(); |
a9182619 | 7435 | |
7436 | const Expression_list* args = this->args(); | |
7437 | if (args == NULL || args->size() < 1) | |
7438 | { | |
7439 | this->report_error(_("not enough arguments")); | |
7440 | return Expression::make_error(this->location()); | |
7441 | } | |
7442 | ||
7443 | Expression_list::const_iterator parg = args->begin(); | |
7444 | ||
7445 | Expression* first_arg = *parg; | |
7446 | if (!first_arg->is_type_expression()) | |
7447 | { | |
631d5788 | 7448 | go_error_at(first_arg->location(), "expected type"); |
a9182619 | 7449 | this->set_is_error(); |
7450 | return Expression::make_error(this->location()); | |
7451 | } | |
7452 | Type* type = first_arg->type(); | |
7453 | ||
22deed0d | 7454 | if (!type->in_heap()) |
7455 | go_error_at(first_arg->location(), | |
7456 | "can't make slice of go:notinheap type"); | |
7457 | ||
a9182619 | 7458 | bool is_slice = false; |
7459 | bool is_map = false; | |
7460 | bool is_chan = false; | |
411eb89e | 7461 | if (type->is_slice_type()) |
a9182619 | 7462 | is_slice = true; |
7463 | else if (type->map_type() != NULL) | |
7464 | is_map = true; | |
7465 | else if (type->channel_type() != NULL) | |
7466 | is_chan = true; | |
7467 | else | |
7468 | { | |
7469 | this->report_error(_("invalid type for make function")); | |
7470 | return Expression::make_error(this->location()); | |
7471 | } | |
7472 | ||
f6bc81e6 | 7473 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7474 | ||
a9182619 | 7475 | ++parg; |
7476 | Expression* len_arg; | |
ccea2b36 | 7477 | bool len_small = false; |
a9182619 | 7478 | if (parg == args->end()) |
7479 | { | |
7480 | if (is_slice) | |
7481 | { | |
7482 | this->report_error(_("length required when allocating a slice")); | |
7483 | return Expression::make_error(this->location()); | |
7484 | } | |
e67508fa | 7485 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
33d1d391 | 7486 | len_small = true; |
a9182619 | 7487 | } |
7488 | else | |
7489 | { | |
7490 | len_arg = *parg; | |
f6bc81e6 | 7491 | len_arg->determine_type(&int_context); |
ccea2b36 | 7492 | if (!this->check_int_value(len_arg, true, &len_small)) |
1ad00fd4 | 7493 | return Expression::make_error(this->location()); |
a9182619 | 7494 | ++parg; |
7495 | } | |
7496 | ||
7497 | Expression* cap_arg = NULL; | |
ccea2b36 | 7498 | bool cap_small = false; |
72bf0e6e | 7499 | Numeric_constant nclen; |
7500 | Numeric_constant nccap; | |
7501 | unsigned long vlen; | |
7502 | unsigned long vcap; | |
a9182619 | 7503 | if (is_slice && parg != args->end()) |
7504 | { | |
7505 | cap_arg = *parg; | |
f6bc81e6 | 7506 | cap_arg->determine_type(&int_context); |
ccea2b36 | 7507 | if (!this->check_int_value(cap_arg, false, &cap_small)) |
1ad00fd4 | 7508 | return Expression::make_error(this->location()); |
7509 | ||
1ad00fd4 | 7510 | if (len_arg->numeric_constant_value(&nclen) |
7511 | && cap_arg->numeric_constant_value(&nccap) | |
7512 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7513 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7514 | && vlen > vcap) | |
a9182619 | 7515 | { |
1ad00fd4 | 7516 | this->report_error(_("len larger than cap")); |
a9182619 | 7517 | return Expression::make_error(this->location()); |
7518 | } | |
1ad00fd4 | 7519 | |
a9182619 | 7520 | ++parg; |
7521 | } | |
7522 | ||
7523 | if (parg != args->end()) | |
7524 | { | |
7525 | this->report_error(_("too many arguments to make")); | |
7526 | return Expression::make_error(this->location()); | |
7527 | } | |
7528 | ||
b13c66cd | 7529 | Location type_loc = first_arg->location(); |
a9182619 | 7530 | |
7531 | Expression* call; | |
7532 | if (is_slice) | |
7533 | { | |
7534 | if (cap_arg == NULL) | |
321e5ad2 | 7535 | { |
72bf0e6e | 7536 | cap_small = len_small; |
7537 | if (len_arg->numeric_constant_value(&nclen) | |
7538 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID) | |
7539 | cap_arg = Expression::make_integer_ul(vlen, len_arg->type(), loc); | |
7540 | else | |
7541 | { | |
7542 | Temporary_statement* temp = Statement::make_temporary(NULL, | |
7543 | len_arg, | |
7544 | loc); | |
7545 | inserter->insert(temp); | |
7546 | len_arg = Expression::make_temporary_reference(temp, loc); | |
7547 | cap_arg = Expression::make_temporary_reference(temp, loc); | |
7548 | } | |
321e5ad2 | 7549 | } |
ccea2b36 | 7550 | |
72bf0e6e | 7551 | Type* et = type->array_type()->element_type(); |
7552 | Expression* type_arg = Expression::make_type_descriptor(et, type_loc); | |
ccea2b36 | 7553 | Runtime::Function code = Runtime::MAKESLICE; |
7554 | if (!len_small || !cap_small) | |
7555 | code = Runtime::MAKESLICE64; | |
7556 | call = Runtime::make_call(code, loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7557 | } |
7558 | else if (is_map) | |
321e5ad2 | 7559 | { |
7560 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
33d1d391 | 7561 | if (!len_small) |
7562 | call = Runtime::make_call(Runtime::MAKEMAP64, loc, 3, type_arg, | |
7563 | len_arg, | |
7564 | Expression::make_nil(loc)); | |
7565 | else | |
7566 | { | |
7567 | Numeric_constant nclen; | |
7568 | unsigned long vlen; | |
7569 | if (len_arg->numeric_constant_value(&nclen) | |
7570 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7571 | && vlen <= Map_type::bucket_size) | |
7572 | call = Runtime::make_call(Runtime::MAKEMAP_SMALL, loc, 0); | |
7573 | else | |
7574 | call = Runtime::make_call(Runtime::MAKEMAP, loc, 3, type_arg, | |
7575 | len_arg, | |
7576 | Expression::make_nil(loc)); | |
7577 | } | |
321e5ad2 | 7578 | } |
a9182619 | 7579 | else if (is_chan) |
321e5ad2 | 7580 | { |
7581 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
7582 | call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg); | |
7583 | } | |
a9182619 | 7584 | else |
7585 | go_unreachable(); | |
7586 | ||
7587 | return Expression::make_unsafe_cast(type, call, loc); | |
7588 | } | |
7589 | ||
321e5ad2 | 7590 | // Flatten a call to the predeclared append function. We do this in |
7591 | // the flatten phase, not the lowering phase, so that we run after | |
7592 | // type checking and after order_evaluations. | |
7593 | ||
7594 | Expression* | |
7595 | Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function, | |
7596 | Statement_inserter* inserter) | |
7597 | { | |
7598 | if (this->is_error_expression()) | |
7599 | return this; | |
7600 | ||
7601 | Location loc = this->location(); | |
7602 | ||
7603 | const Expression_list* args = this->args(); | |
7604 | go_assert(args != NULL && !args->empty()); | |
7605 | ||
7606 | Type* slice_type = args->front()->type(); | |
7607 | go_assert(slice_type->is_slice_type()); | |
7608 | Type* element_type = slice_type->array_type()->element_type(); | |
7609 | ||
7610 | if (args->size() == 1) | |
7611 | { | |
7612 | // append(s) evaluates to s. | |
7613 | return args->front(); | |
7614 | } | |
7615 | ||
7616 | Type* int_type = Type::lookup_integer_type("int"); | |
7617 | Type* uint_type = Type::lookup_integer_type("uint"); | |
7618 | ||
7619 | // Implementing | |
7620 | // append(s1, s2...) | |
7621 | // or | |
7622 | // append(s1, a1, a2, a3, ...) | |
7623 | ||
7624 | // s1tmp := s1 | |
7625 | Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(), | |
7626 | loc); | |
7627 | inserter->insert(s1tmp); | |
7628 | ||
7629 | // l1tmp := len(s1tmp) | |
7630 | Named_object* lenfn = gogo->lookup_global("len"); | |
7631 | Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7632 | Expression_list* call_args = new Expression_list(); | |
7633 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7634 | Expression* len = Expression::make_call(lenref, call_args, false, loc); | |
7635 | gogo->lower_expression(function, inserter, &len); | |
7636 | gogo->flatten_expression(function, inserter, &len); | |
7637 | Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc); | |
7638 | inserter->insert(l1tmp); | |
7639 | ||
7640 | Temporary_statement* s2tmp = NULL; | |
7641 | Temporary_statement* l2tmp = NULL; | |
7642 | Expression_list* add = NULL; | |
7643 | Expression* len2; | |
7644 | if (this->is_varargs()) | |
7645 | { | |
7646 | go_assert(args->size() == 2); | |
7647 | ||
7648 | // s2tmp := s2 | |
7649 | s2tmp = Statement::make_temporary(NULL, args->back(), loc); | |
7650 | inserter->insert(s2tmp); | |
7651 | ||
7652 | // l2tmp := len(s2tmp) | |
7653 | lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7654 | call_args = new Expression_list(); | |
7655 | call_args->push_back(Expression::make_temporary_reference(s2tmp, loc)); | |
7656 | len = Expression::make_call(lenref, call_args, false, loc); | |
7657 | gogo->lower_expression(function, inserter, &len); | |
7658 | gogo->flatten_expression(function, inserter, &len); | |
7659 | l2tmp = Statement::make_temporary(int_type, len, loc); | |
7660 | inserter->insert(l2tmp); | |
7661 | ||
7662 | // len2 = l2tmp | |
7663 | len2 = Expression::make_temporary_reference(l2tmp, loc); | |
7664 | } | |
7665 | else | |
7666 | { | |
7667 | // We have to ensure that all the arguments are in variables | |
7668 | // now, because otherwise if one of them is an index expression | |
7669 | // into the current slice we could overwrite it before we fetch | |
7670 | // it. | |
7671 | add = new Expression_list(); | |
7672 | Expression_list::const_iterator pa = args->begin(); | |
7673 | for (++pa; pa != args->end(); ++pa) | |
7674 | { | |
7675 | if ((*pa)->is_variable()) | |
7676 | add->push_back(*pa); | |
7677 | else | |
7678 | { | |
7679 | Temporary_statement* tmp = Statement::make_temporary(NULL, *pa, | |
7680 | loc); | |
7681 | inserter->insert(tmp); | |
7682 | add->push_back(Expression::make_temporary_reference(tmp, loc)); | |
7683 | } | |
7684 | } | |
7685 | ||
7686 | // len2 = len(add) | |
7687 | len2 = Expression::make_integer_ul(add->size(), int_type, loc); | |
7688 | } | |
7689 | ||
7690 | // ntmp := l1tmp + len2 | |
7691 | Expression* ref = Expression::make_temporary_reference(l1tmp, loc); | |
7692 | Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc); | |
7693 | gogo->lower_expression(function, inserter, &sum); | |
7694 | gogo->flatten_expression(function, inserter, &sum); | |
7695 | Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc); | |
7696 | inserter->insert(ntmp); | |
7697 | ||
7698 | // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ? | |
7699 | // growslice(type, s1tmp, ntmp) : | |
7700 | // s1tmp[:ntmp] | |
7701 | // Using uint here means that if the computation of ntmp overflowed, | |
7702 | // we will call growslice which will panic. | |
7703 | ||
7704 | Expression* left = Expression::make_temporary_reference(ntmp, loc); | |
7705 | left = Expression::make_cast(uint_type, left, loc); | |
7706 | ||
7707 | Named_object* capfn = gogo->lookup_global("cap"); | |
7708 | Expression* capref = Expression::make_func_reference(capfn, NULL, loc); | |
7709 | call_args = new Expression_list(); | |
7710 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7711 | Expression* right = Expression::make_call(capref, call_args, false, loc); | |
7712 | right = Expression::make_cast(uint_type, right, loc); | |
7713 | ||
7714 | Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc); | |
7715 | ||
7716 | Expression* a1 = Expression::make_type_descriptor(element_type, loc); | |
7717 | Expression* a2 = Expression::make_temporary_reference(s1tmp, loc); | |
7718 | Expression* a3 = Expression::make_temporary_reference(ntmp, loc); | |
7719 | Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3, | |
7720 | a1, a2, a3); | |
7721 | call = Expression::make_unsafe_cast(slice_type, call, loc); | |
7722 | ||
7723 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7724 | Expression* zero = Expression::make_integer_ul(0, int_type, loc); | |
7725 | Expression* ref2 = Expression::make_temporary_reference(ntmp, loc); | |
7726 | // FIXME: Mark this index as not requiring bounds checks. | |
7727 | ref = Expression::make_index(ref, zero, ref2, NULL, loc); | |
7728 | ||
7729 | Expression* rhs = Expression::make_conditional(cond, call, ref, loc); | |
7730 | ||
7731 | gogo->lower_expression(function, inserter, &rhs); | |
7732 | gogo->flatten_expression(function, inserter, &rhs); | |
7733 | ||
7734 | Expression* lhs = Expression::make_temporary_reference(s1tmp, loc); | |
7735 | Statement* assign = Statement::make_assignment(lhs, rhs, loc); | |
7736 | inserter->insert(assign); | |
7737 | ||
7738 | if (this->is_varargs()) | |
7739 | { | |
7740 | // copy(s1tmp[l1tmp:], s2tmp) | |
7741 | a1 = Expression::make_temporary_reference(s1tmp, loc); | |
7742 | ref = Expression::make_temporary_reference(l1tmp, loc); | |
7743 | Expression* nil = Expression::make_nil(loc); | |
7744 | // FIXME: Mark this index as not requiring bounds checks. | |
7745 | a1 = Expression::make_index(a1, ref, nil, NULL, loc); | |
7746 | ||
7747 | a2 = Expression::make_temporary_reference(s2tmp, loc); | |
7748 | ||
7749 | Named_object* copyfn = gogo->lookup_global("copy"); | |
7750 | Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc); | |
7751 | call_args = new Expression_list(); | |
7752 | call_args->push_back(a1); | |
7753 | call_args->push_back(a2); | |
7754 | call = Expression::make_call(copyref, call_args, false, loc); | |
7755 | gogo->lower_expression(function, inserter, &call); | |
7756 | gogo->flatten_expression(function, inserter, &call); | |
7757 | inserter->insert(Statement::make_statement(call, false)); | |
7758 | } | |
7759 | else | |
7760 | { | |
7761 | // For each argument: | |
7762 | // s1tmp[l1tmp+i] = a | |
7763 | unsigned long i = 0; | |
7764 | for (Expression_list::const_iterator pa = add->begin(); | |
7765 | pa != add->end(); | |
7766 | ++pa, ++i) | |
7767 | { | |
7768 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7769 | ref2 = Expression::make_temporary_reference(l1tmp, loc); | |
7770 | Expression* off = Expression::make_integer_ul(i, int_type, loc); | |
7771 | ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc); | |
7772 | // FIXME: Mark this index as not requiring bounds checks. | |
7773 | lhs = Expression::make_index(ref, ref2, NULL, NULL, loc); | |
7774 | gogo->lower_expression(function, inserter, &lhs); | |
7775 | gogo->flatten_expression(function, inserter, &lhs); | |
03118c21 | 7776 | // The flatten pass runs after the write barrier pass, so we |
7777 | // need to insert a write barrier here if necessary. | |
7778 | if (!gogo->assign_needs_write_barrier(lhs)) | |
7779 | assign = Statement::make_assignment(lhs, *pa, loc); | |
7780 | else | |
7781 | { | |
7782 | Function* f = function == NULL ? NULL : function->func_value(); | |
7783 | assign = gogo->assign_with_write_barrier(f, NULL, inserter, | |
7784 | lhs, *pa, loc); | |
7785 | } | |
321e5ad2 | 7786 | inserter->insert(assign); |
7787 | } | |
7788 | } | |
7789 | ||
7790 | return Expression::make_temporary_reference(s1tmp, loc); | |
7791 | } | |
7792 | ||
a9182619 | 7793 | // Return whether an expression has an integer value. Report an error |
7794 | // if not. This is used when handling calls to the predeclared make | |
ccea2b36 | 7795 | // function. Set *SMALL if the value is known to fit in type "int". |
a9182619 | 7796 | |
7797 | bool | |
ccea2b36 | 7798 | Builtin_call_expression::check_int_value(Expression* e, bool is_length, |
7799 | bool *small) | |
a9182619 | 7800 | { |
ccea2b36 | 7801 | *small = false; |
7802 | ||
0c77715b | 7803 | Numeric_constant nc; |
1ad00fd4 | 7804 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7805 | { |
1ad00fd4 | 7806 | unsigned long v; |
7807 | switch (nc.to_unsigned_long(&v)) | |
7808 | { | |
7809 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7810 | break; |
1ad00fd4 | 7811 | case Numeric_constant::NC_UL_NOTINT: |
631d5788 | 7812 | go_error_at(e->location(), "non-integer %s argument to make", |
7813 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7814 | return false; |
7815 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 7816 | go_error_at(e->location(), "negative %s argument to make", |
7817 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7818 | return false; |
7819 | case Numeric_constant::NC_UL_BIG: | |
7820 | // We don't want to give a compile-time error for a 64-bit | |
7821 | // value on a 32-bit target. | |
1b10c5e7 | 7822 | break; |
1ad00fd4 | 7823 | } |
1b10c5e7 | 7824 | |
7825 | mpz_t val; | |
7826 | if (!nc.to_int(&val)) | |
7827 | go_unreachable(); | |
7828 | int bits = mpz_sizeinbase(val, 2); | |
7829 | mpz_clear(val); | |
7830 | Type* int_type = Type::lookup_integer_type("int"); | |
7831 | if (bits >= int_type->integer_type()->bits()) | |
7832 | { | |
631d5788 | 7833 | go_error_at(e->location(), "%s argument too large for make", |
7834 | is_length ? "len" : "cap"); | |
1b10c5e7 | 7835 | return false; |
7836 | } | |
7837 | ||
ccea2b36 | 7838 | *small = true; |
1b10c5e7 | 7839 | return true; |
a9182619 | 7840 | } |
7841 | ||
1ad00fd4 | 7842 | if (e->type()->integer_type() != NULL) |
ccea2b36 | 7843 | { |
7844 | int ebits = e->type()->integer_type()->bits(); | |
7845 | int intbits = Type::lookup_integer_type("int")->integer_type()->bits(); | |
7846 | ||
7847 | // We can treat ebits == intbits as small even for an unsigned | |
7848 | // integer type, because we will convert the value to int and | |
7849 | // then reject it in the runtime if it is negative. | |
7850 | *small = ebits <= intbits; | |
7851 | ||
7852 | return true; | |
7853 | } | |
1ad00fd4 | 7854 | |
631d5788 | 7855 | go_error_at(e->location(), "non-integer %s argument to make", |
7856 | is_length ? "len" : "cap"); | |
a9182619 | 7857 | return false; |
7858 | } | |
7859 | ||
e440a328 | 7860 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7861 | // the argument. We need to map complex64 to float32 and complex128 |
7862 | // to float64, so it has to be done by name. This returns NULL if it | |
7863 | // can't figure out the type. | |
e440a328 | 7864 | |
7865 | Type* | |
7866 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7867 | { | |
7868 | if (arg_type == NULL || arg_type->is_abstract()) | |
7869 | return NULL; | |
7870 | Named_type* nt = arg_type->named_type(); | |
7871 | if (nt == NULL) | |
7872 | return NULL; | |
7873 | while (nt->real_type()->named_type() != NULL) | |
7874 | nt = nt->real_type()->named_type(); | |
48080209 | 7875 | if (nt->name() == "complex64") |
e440a328 | 7876 | return Type::lookup_float_type("float32"); |
7877 | else if (nt->name() == "complex128") | |
7878 | return Type::lookup_float_type("float64"); | |
7879 | else | |
7880 | return NULL; | |
7881 | } | |
7882 | ||
48080209 | 7883 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7884 | // argments. Like real_imag_type, we have to map by name. |
7885 | ||
7886 | Type* | |
48080209 | 7887 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7888 | { |
7889 | if (arg_type == NULL || arg_type->is_abstract()) | |
7890 | return NULL; | |
7891 | Named_type* nt = arg_type->named_type(); | |
7892 | if (nt == NULL) | |
7893 | return NULL; | |
7894 | while (nt->real_type()->named_type() != NULL) | |
7895 | nt = nt->real_type()->named_type(); | |
48080209 | 7896 | if (nt->name() == "float32") |
e440a328 | 7897 | return Type::lookup_complex_type("complex64"); |
7898 | else if (nt->name() == "float64") | |
7899 | return Type::lookup_complex_type("complex128"); | |
7900 | else | |
7901 | return NULL; | |
7902 | } | |
7903 | ||
7904 | // Return a single argument, or NULL if there isn't one. | |
7905 | ||
7906 | Expression* | |
7907 | Builtin_call_expression::one_arg() const | |
7908 | { | |
7909 | const Expression_list* args = this->args(); | |
aa615cb3 | 7910 | if (args == NULL || args->size() != 1) |
e440a328 | 7911 | return NULL; |
7912 | return args->front(); | |
7913 | } | |
7914 | ||
83921647 | 7915 | // A traversal class which looks for a call or receive expression. |
7916 | ||
7917 | class Find_call_expression : public Traverse | |
7918 | { | |
7919 | public: | |
7920 | Find_call_expression() | |
7921 | : Traverse(traverse_expressions), | |
7922 | found_(false) | |
7923 | { } | |
7924 | ||
7925 | int | |
7926 | expression(Expression**); | |
7927 | ||
7928 | bool | |
7929 | found() | |
7930 | { return this->found_; } | |
7931 | ||
7932 | private: | |
7933 | bool found_; | |
7934 | }; | |
7935 | ||
7936 | int | |
7937 | Find_call_expression::expression(Expression** pexpr) | |
7938 | { | |
7939 | if ((*pexpr)->call_expression() != NULL | |
7940 | || (*pexpr)->receive_expression() != NULL) | |
7941 | { | |
7942 | this->found_ = true; | |
7943 | return TRAVERSE_EXIT; | |
7944 | } | |
7945 | return TRAVERSE_CONTINUE; | |
7946 | } | |
7947 | ||
7948 | // Return whether this is constant: len of a string constant, or len | |
7949 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7950 | // unsafe.Alignof. | |
e440a328 | 7951 | |
7952 | bool | |
7953 | Builtin_call_expression::do_is_constant() const | |
7954 | { | |
12e69faa | 7955 | if (this->is_error_expression()) |
7956 | return true; | |
e440a328 | 7957 | switch (this->code_) |
7958 | { | |
7959 | case BUILTIN_LEN: | |
7960 | case BUILTIN_CAP: | |
7961 | { | |
0f914071 | 7962 | if (this->seen_) |
7963 | return false; | |
7964 | ||
e440a328 | 7965 | Expression* arg = this->one_arg(); |
7966 | if (arg == NULL) | |
7967 | return false; | |
7968 | Type* arg_type = arg->type(); | |
7969 | ||
7970 | if (arg_type->points_to() != NULL | |
7971 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7972 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7973 | arg_type = arg_type->points_to(); |
7974 | ||
83921647 | 7975 | // The len and cap functions are only constant if there are no |
7976 | // function calls or channel operations in the arguments. | |
7977 | // Otherwise we have to make the call. | |
7978 | if (!arg->is_constant()) | |
7979 | { | |
7980 | Find_call_expression find_call; | |
7981 | Expression::traverse(&arg, &find_call); | |
7982 | if (find_call.found()) | |
7983 | return false; | |
7984 | } | |
7985 | ||
e440a328 | 7986 | if (arg_type->array_type() != NULL |
7987 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7988 | return true; |
e440a328 | 7989 | |
7990 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7991 | { |
7992 | this->seen_ = true; | |
7993 | bool ret = arg->is_constant(); | |
7994 | this->seen_ = false; | |
7995 | return ret; | |
7996 | } | |
e440a328 | 7997 | } |
7998 | break; | |
7999 | ||
8000 | case BUILTIN_SIZEOF: | |
8001 | case BUILTIN_ALIGNOF: | |
8002 | return this->one_arg() != NULL; | |
8003 | ||
8004 | case BUILTIN_OFFSETOF: | |
8005 | { | |
8006 | Expression* arg = this->one_arg(); | |
8007 | if (arg == NULL) | |
8008 | return false; | |
8009 | return arg->field_reference_expression() != NULL; | |
8010 | } | |
8011 | ||
48080209 | 8012 | case BUILTIN_COMPLEX: |
e440a328 | 8013 | { |
8014 | const Expression_list* args = this->args(); | |
8015 | if (args != NULL && args->size() == 2) | |
8016 | return args->front()->is_constant() && args->back()->is_constant(); | |
8017 | } | |
8018 | break; | |
8019 | ||
8020 | case BUILTIN_REAL: | |
8021 | case BUILTIN_IMAG: | |
8022 | { | |
8023 | Expression* arg = this->one_arg(); | |
8024 | return arg != NULL && arg->is_constant(); | |
8025 | } | |
8026 | ||
8027 | default: | |
8028 | break; | |
8029 | } | |
8030 | ||
8031 | return false; | |
8032 | } | |
8033 | ||
0c77715b | 8034 | // Return a numeric constant if possible. |
e440a328 | 8035 | |
8036 | bool | |
0c77715b | 8037 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 8038 | { |
8039 | if (this->code_ == BUILTIN_LEN | |
8040 | || this->code_ == BUILTIN_CAP) | |
8041 | { | |
8042 | Expression* arg = this->one_arg(); | |
8043 | if (arg == NULL) | |
8044 | return false; | |
8045 | Type* arg_type = arg->type(); | |
8046 | ||
8047 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
8048 | { | |
8049 | std::string sval; | |
8050 | if (arg->string_constant_value(&sval)) | |
8051 | { | |
0c77715b | 8052 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
8053 | sval.length()); | |
e440a328 | 8054 | return true; |
8055 | } | |
8056 | } | |
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 | |
8064 | && arg_type->array_type()->length() != NULL) | |
8065 | { | |
0f914071 | 8066 | if (this->seen_) |
8067 | return false; | |
e440a328 | 8068 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 8069 | this->seen_ = true; |
0c77715b | 8070 | bool r = e->numeric_constant_value(nc); |
0f914071 | 8071 | this->seen_ = false; |
8072 | if (r) | |
e440a328 | 8073 | { |
0c77715b | 8074 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
8075 | this->location())) | |
8076 | r = false; | |
e440a328 | 8077 | } |
0c77715b | 8078 | return r; |
e440a328 | 8079 | } |
8080 | } | |
8081 | else if (this->code_ == BUILTIN_SIZEOF | |
8082 | || this->code_ == BUILTIN_ALIGNOF) | |
8083 | { | |
8084 | Expression* arg = this->one_arg(); | |
8085 | if (arg == NULL) | |
8086 | return false; | |
8087 | Type* arg_type = arg->type(); | |
5c13bd80 | 8088 | if (arg_type->is_error()) |
e440a328 | 8089 | return false; |
8090 | if (arg_type->is_abstract()) | |
8091 | return false; | |
2c809f8f | 8092 | if (this->seen_) |
8093 | return false; | |
927a01eb | 8094 | |
3f378015 | 8095 | int64_t ret; |
e440a328 | 8096 | if (this->code_ == BUILTIN_SIZEOF) |
8097 | { | |
2c809f8f | 8098 | this->seen_ = true; |
8099 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
8100 | this->seen_ = false; | |
8101 | if (!ok) | |
e440a328 | 8102 | return false; |
8103 | } | |
8104 | else if (this->code_ == BUILTIN_ALIGNOF) | |
8105 | { | |
2c809f8f | 8106 | bool ok; |
8107 | this->seen_ = true; | |
637bd3af | 8108 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 8109 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 8110 | else |
e440a328 | 8111 | { |
8112 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
8113 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 8114 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 8115 | } |
2c809f8f | 8116 | this->seen_ = false; |
8117 | if (!ok) | |
8118 | return false; | |
e440a328 | 8119 | } |
8120 | else | |
c3e6f413 | 8121 | go_unreachable(); |
927a01eb | 8122 | |
3f378015 | 8123 | mpz_t zval; |
8124 | set_mpz_from_int64(&zval, ret); | |
8125 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8126 | mpz_clear(zval); | |
e440a328 | 8127 | return true; |
8128 | } | |
8129 | else if (this->code_ == BUILTIN_OFFSETOF) | |
8130 | { | |
8131 | Expression* arg = this->one_arg(); | |
8132 | if (arg == NULL) | |
8133 | return false; | |
8134 | Field_reference_expression* farg = arg->field_reference_expression(); | |
8135 | if (farg == NULL) | |
8136 | return false; | |
2c809f8f | 8137 | if (this->seen_) |
8138 | return false; | |
8139 | ||
3f378015 | 8140 | int64_t total_offset = 0; |
9a4bd570 | 8141 | while (true) |
8142 | { | |
8143 | Expression* struct_expr = farg->expr(); | |
8144 | Type* st = struct_expr->type(); | |
8145 | if (st->struct_type() == NULL) | |
8146 | return false; | |
8147 | if (st->named_type() != NULL) | |
8148 | st->named_type()->convert(this->gogo_); | |
3f378015 | 8149 | int64_t offset; |
2c809f8f | 8150 | this->seen_ = true; |
8151 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
8152 | farg->field_index(), | |
8153 | &offset); | |
8154 | this->seen_ = false; | |
8155 | if (!ok) | |
8156 | return false; | |
9a4bd570 | 8157 | total_offset += offset; |
8158 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
8159 | { | |
8160 | // Go up until we reach the original base. | |
8161 | farg = struct_expr->field_reference_expression(); | |
8162 | continue; | |
8163 | } | |
8164 | break; | |
8165 | } | |
3f378015 | 8166 | mpz_t zval; |
8167 | set_mpz_from_int64(&zval, total_offset); | |
8168 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8169 | mpz_clear(zval); | |
e440a328 | 8170 | return true; |
8171 | } | |
0c77715b | 8172 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 8173 | { |
8174 | Expression* arg = this->one_arg(); | |
8175 | if (arg == NULL) | |
8176 | return false; | |
8177 | ||
0c77715b | 8178 | Numeric_constant argnc; |
8179 | if (!arg->numeric_constant_value(&argnc)) | |
8180 | return false; | |
8181 | ||
fcbea5e4 | 8182 | mpc_t val; |
8183 | if (!argnc.to_complex(&val)) | |
0c77715b | 8184 | return false; |
e440a328 | 8185 | |
0c77715b | 8186 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
8187 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 8188 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 8189 | else |
fcbea5e4 | 8190 | nc->set_float(type, mpc_imagref(val)); |
8191 | mpc_clear(val); | |
0c77715b | 8192 | return true; |
e440a328 | 8193 | } |
0c77715b | 8194 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 8195 | { |
8196 | const Expression_list* args = this->args(); | |
8197 | if (args == NULL || args->size() != 2) | |
8198 | return false; | |
8199 | ||
0c77715b | 8200 | Numeric_constant rnc; |
8201 | if (!args->front()->numeric_constant_value(&rnc)) | |
8202 | return false; | |
8203 | Numeric_constant inc; | |
8204 | if (!args->back()->numeric_constant_value(&inc)) | |
8205 | return false; | |
8206 | ||
8207 | if (rnc.type() != NULL | |
8208 | && !rnc.type()->is_abstract() | |
8209 | && inc.type() != NULL | |
8210 | && !inc.type()->is_abstract() | |
8211 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
8212 | return false; | |
8213 | ||
e440a328 | 8214 | mpfr_t r; |
0c77715b | 8215 | if (!rnc.to_float(&r)) |
8216 | return false; | |
8217 | mpfr_t i; | |
8218 | if (!inc.to_float(&i)) | |
e440a328 | 8219 | { |
8220 | mpfr_clear(r); | |
8221 | return false; | |
8222 | } | |
8223 | ||
0c77715b | 8224 | Type* arg_type = rnc.type(); |
8225 | if (arg_type == NULL || arg_type->is_abstract()) | |
8226 | arg_type = inc.type(); | |
e440a328 | 8227 | |
fcbea5e4 | 8228 | mpc_t val; |
8229 | mpc_init2(val, mpc_precision); | |
8230 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 8231 | mpfr_clear(r); |
8232 | mpfr_clear(i); | |
8233 | ||
fcbea5e4 | 8234 | Type* type = Builtin_call_expression::complex_type(arg_type); |
8235 | nc->set_complex(type, val); | |
8236 | ||
8237 | mpc_clear(val); | |
8238 | ||
0c77715b | 8239 | return true; |
e440a328 | 8240 | } |
8241 | ||
8242 | return false; | |
8243 | } | |
8244 | ||
a7549a6a | 8245 | // Give an error if we are discarding the value of an expression which |
8246 | // should not normally be discarded. We don't give an error for | |
8247 | // discarding the value of an ordinary function call, but we do for | |
8248 | // builtin functions, purely for consistency with the gc compiler. | |
8249 | ||
4f2138d7 | 8250 | bool |
a7549a6a | 8251 | Builtin_call_expression::do_discarding_value() |
8252 | { | |
8253 | switch (this->code_) | |
8254 | { | |
8255 | case BUILTIN_INVALID: | |
8256 | default: | |
8257 | go_unreachable(); | |
8258 | ||
8259 | case BUILTIN_APPEND: | |
8260 | case BUILTIN_CAP: | |
8261 | case BUILTIN_COMPLEX: | |
8262 | case BUILTIN_IMAG: | |
8263 | case BUILTIN_LEN: | |
8264 | case BUILTIN_MAKE: | |
8265 | case BUILTIN_NEW: | |
8266 | case BUILTIN_REAL: | |
8267 | case BUILTIN_ALIGNOF: | |
8268 | case BUILTIN_OFFSETOF: | |
8269 | case BUILTIN_SIZEOF: | |
8270 | this->unused_value_error(); | |
4f2138d7 | 8271 | return false; |
a7549a6a | 8272 | |
8273 | case BUILTIN_CLOSE: | |
8274 | case BUILTIN_COPY: | |
1cce762f | 8275 | case BUILTIN_DELETE: |
a7549a6a | 8276 | case BUILTIN_PANIC: |
8277 | case BUILTIN_PRINT: | |
8278 | case BUILTIN_PRINTLN: | |
8279 | case BUILTIN_RECOVER: | |
4f2138d7 | 8280 | return true; |
a7549a6a | 8281 | } |
8282 | } | |
8283 | ||
e440a328 | 8284 | // Return the type. |
8285 | ||
8286 | Type* | |
8287 | Builtin_call_expression::do_type() | |
8288 | { | |
79651b1f | 8289 | if (this->is_error_expression()) |
8290 | return Type::make_error_type(); | |
e440a328 | 8291 | switch (this->code_) |
8292 | { | |
8293 | case BUILTIN_INVALID: | |
8294 | default: | |
79651b1f | 8295 | return Type::make_error_type(); |
e440a328 | 8296 | |
8297 | case BUILTIN_NEW: | |
8298 | case BUILTIN_MAKE: | |
8299 | { | |
8300 | const Expression_list* args = this->args(); | |
8301 | if (args == NULL || args->empty()) | |
8302 | return Type::make_error_type(); | |
8303 | return Type::make_pointer_type(args->front()->type()); | |
8304 | } | |
8305 | ||
8306 | case BUILTIN_CAP: | |
8307 | case BUILTIN_COPY: | |
8308 | case BUILTIN_LEN: | |
7ba86326 | 8309 | return Type::lookup_integer_type("int"); |
8310 | ||
e440a328 | 8311 | case BUILTIN_ALIGNOF: |
8312 | case BUILTIN_OFFSETOF: | |
8313 | case BUILTIN_SIZEOF: | |
7ba86326 | 8314 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 8315 | |
8316 | case BUILTIN_CLOSE: | |
1cce762f | 8317 | case BUILTIN_DELETE: |
e440a328 | 8318 | case BUILTIN_PANIC: |
8319 | case BUILTIN_PRINT: | |
8320 | case BUILTIN_PRINTLN: | |
8321 | return Type::make_void_type(); | |
8322 | ||
e440a328 | 8323 | case BUILTIN_RECOVER: |
823c7e3d | 8324 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8325 | |
8326 | case BUILTIN_APPEND: | |
8327 | { | |
8328 | const Expression_list* args = this->args(); | |
8329 | if (args == NULL || args->empty()) | |
8330 | return Type::make_error_type(); | |
3ff4863b | 8331 | Type *ret = args->front()->type(); |
8332 | if (!ret->is_slice_type()) | |
8333 | return Type::make_error_type(); | |
8334 | return ret; | |
e440a328 | 8335 | } |
8336 | ||
8337 | case BUILTIN_REAL: | |
8338 | case BUILTIN_IMAG: | |
8339 | { | |
8340 | Expression* arg = this->one_arg(); | |
8341 | if (arg == NULL) | |
8342 | return Type::make_error_type(); | |
8343 | Type* t = arg->type(); | |
8344 | if (t->is_abstract()) | |
8345 | t = t->make_non_abstract_type(); | |
8346 | t = Builtin_call_expression::real_imag_type(t); | |
8347 | if (t == NULL) | |
8348 | t = Type::make_error_type(); | |
8349 | return t; | |
8350 | } | |
8351 | ||
48080209 | 8352 | case BUILTIN_COMPLEX: |
e440a328 | 8353 | { |
8354 | const Expression_list* args = this->args(); | |
8355 | if (args == NULL || args->size() != 2) | |
8356 | return Type::make_error_type(); | |
8357 | Type* t = args->front()->type(); | |
8358 | if (t->is_abstract()) | |
8359 | { | |
8360 | t = args->back()->type(); | |
8361 | if (t->is_abstract()) | |
8362 | t = t->make_non_abstract_type(); | |
8363 | } | |
48080209 | 8364 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 8365 | if (t == NULL) |
8366 | t = Type::make_error_type(); | |
8367 | return t; | |
8368 | } | |
8369 | } | |
8370 | } | |
8371 | ||
8372 | // Determine the type. | |
8373 | ||
8374 | void | |
8375 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
8376 | { | |
fb94b0ca | 8377 | if (!this->determining_types()) |
8378 | return; | |
8379 | ||
e440a328 | 8380 | this->fn()->determine_type_no_context(); |
8381 | ||
8382 | const Expression_list* args = this->args(); | |
8383 | ||
8384 | bool is_print; | |
8385 | Type* arg_type = NULL; | |
321e5ad2 | 8386 | Type* trailing_arg_types = NULL; |
e440a328 | 8387 | switch (this->code_) |
8388 | { | |
8389 | case BUILTIN_PRINT: | |
8390 | case BUILTIN_PRINTLN: | |
8391 | // Do not force a large integer constant to "int". | |
8392 | is_print = true; | |
8393 | break; | |
8394 | ||
8395 | case BUILTIN_REAL: | |
8396 | case BUILTIN_IMAG: | |
48080209 | 8397 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8398 | if (arg_type == NULL) |
8399 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8400 | is_print = false; |
8401 | break; | |
8402 | ||
48080209 | 8403 | case BUILTIN_COMPLEX: |
e440a328 | 8404 | { |
48080209 | 8405 | // For the complex function the type of one operand can |
e440a328 | 8406 | // determine the type of the other, as in a binary expression. |
8407 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8408 | if (arg_type == NULL) |
8409 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8410 | if (args != NULL && args->size() == 2) |
8411 | { | |
8412 | Type* t1 = args->front()->type(); | |
c849bb59 | 8413 | Type* t2 = args->back()->type(); |
e440a328 | 8414 | if (!t1->is_abstract()) |
8415 | arg_type = t1; | |
8416 | else if (!t2->is_abstract()) | |
8417 | arg_type = t2; | |
8418 | } | |
8419 | is_print = false; | |
8420 | } | |
8421 | break; | |
8422 | ||
321e5ad2 | 8423 | case BUILTIN_APPEND: |
8424 | if (!this->is_varargs() | |
8425 | && args != NULL | |
8426 | && !args->empty() | |
8427 | && args->front()->type()->is_slice_type()) | |
8428 | trailing_arg_types = | |
8429 | args->front()->type()->array_type()->element_type(); | |
8430 | is_print = false; | |
8431 | break; | |
8432 | ||
e440a328 | 8433 | default: |
8434 | is_print = false; | |
8435 | break; | |
8436 | } | |
8437 | ||
8438 | if (args != NULL) | |
8439 | { | |
8440 | for (Expression_list::const_iterator pa = args->begin(); | |
8441 | pa != args->end(); | |
8442 | ++pa) | |
8443 | { | |
8444 | Type_context subcontext; | |
8445 | subcontext.type = arg_type; | |
8446 | ||
8447 | if (is_print) | |
8448 | { | |
8449 | // We want to print large constants, we so can't just | |
8450 | // use the appropriate nonabstract type. Use uint64 for | |
8451 | // an integer if we know it is nonnegative, otherwise | |
8452 | // use int64 for a integer, otherwise use float64 for a | |
8453 | // float or complex128 for a complex. | |
8454 | Type* want_type = NULL; | |
8455 | Type* atype = (*pa)->type(); | |
8456 | if (atype->is_abstract()) | |
8457 | { | |
8458 | if (atype->integer_type() != NULL) | |
8459 | { | |
0c77715b | 8460 | Numeric_constant nc; |
8461 | if (this->numeric_constant_value(&nc)) | |
8462 | { | |
8463 | mpz_t val; | |
8464 | if (nc.to_int(&val)) | |
8465 | { | |
8466 | if (mpz_sgn(val) >= 0) | |
8467 | want_type = Type::lookup_integer_type("uint64"); | |
8468 | mpz_clear(val); | |
8469 | } | |
8470 | } | |
8471 | if (want_type == NULL) | |
e440a328 | 8472 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8473 | } |
8474 | else if (atype->float_type() != NULL) | |
8475 | want_type = Type::lookup_float_type("float64"); | |
8476 | else if (atype->complex_type() != NULL) | |
8477 | want_type = Type::lookup_complex_type("complex128"); | |
8478 | else if (atype->is_abstract_string_type()) | |
8479 | want_type = Type::lookup_string_type(); | |
8480 | else if (atype->is_abstract_boolean_type()) | |
8481 | want_type = Type::lookup_bool_type(); | |
8482 | else | |
c3e6f413 | 8483 | go_unreachable(); |
e440a328 | 8484 | subcontext.type = want_type; |
8485 | } | |
8486 | } | |
8487 | ||
8488 | (*pa)->determine_type(&subcontext); | |
321e5ad2 | 8489 | |
8490 | if (trailing_arg_types != NULL) | |
8491 | { | |
8492 | arg_type = trailing_arg_types; | |
8493 | trailing_arg_types = NULL; | |
8494 | } | |
e440a328 | 8495 | } |
8496 | } | |
8497 | } | |
8498 | ||
8499 | // If there is exactly one argument, return true. Otherwise give an | |
8500 | // error message and return false. | |
8501 | ||
8502 | bool | |
8503 | Builtin_call_expression::check_one_arg() | |
8504 | { | |
8505 | const Expression_list* args = this->args(); | |
8506 | if (args == NULL || args->size() < 1) | |
8507 | { | |
8508 | this->report_error(_("not enough arguments")); | |
8509 | return false; | |
8510 | } | |
8511 | else if (args->size() > 1) | |
8512 | { | |
8513 | this->report_error(_("too many arguments")); | |
8514 | return false; | |
8515 | } | |
8516 | if (args->front()->is_error_expression() | |
5c13bd80 | 8517 | || args->front()->type()->is_error()) |
e440a328 | 8518 | { |
8519 | this->set_is_error(); | |
8520 | return false; | |
8521 | } | |
8522 | return true; | |
8523 | } | |
8524 | ||
8525 | // Check argument types for a builtin function. | |
8526 | ||
8527 | void | |
8528 | Builtin_call_expression::do_check_types(Gogo*) | |
8529 | { | |
375646ea | 8530 | if (this->is_error_expression()) |
8531 | return; | |
e440a328 | 8532 | switch (this->code_) |
8533 | { | |
8534 | case BUILTIN_INVALID: | |
8535 | case BUILTIN_NEW: | |
8536 | case BUILTIN_MAKE: | |
cd238b8d | 8537 | case BUILTIN_DELETE: |
e440a328 | 8538 | return; |
8539 | ||
8540 | case BUILTIN_LEN: | |
8541 | case BUILTIN_CAP: | |
8542 | { | |
8543 | // The single argument may be either a string or an array or a | |
8544 | // map or a channel, or a pointer to a closed array. | |
8545 | if (this->check_one_arg()) | |
8546 | { | |
8547 | Type* arg_type = this->one_arg()->type(); | |
8548 | if (arg_type->points_to() != NULL | |
8549 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8550 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8551 | arg_type = arg_type->points_to(); |
8552 | if (this->code_ == BUILTIN_CAP) | |
8553 | { | |
5c13bd80 | 8554 | if (!arg_type->is_error() |
e440a328 | 8555 | && arg_type->array_type() == NULL |
8556 | && arg_type->channel_type() == NULL) | |
8557 | this->report_error(_("argument must be array or slice " | |
8558 | "or channel")); | |
8559 | } | |
8560 | else | |
8561 | { | |
5c13bd80 | 8562 | if (!arg_type->is_error() |
e440a328 | 8563 | && !arg_type->is_string_type() |
8564 | && arg_type->array_type() == NULL | |
8565 | && arg_type->map_type() == NULL | |
8566 | && arg_type->channel_type() == NULL) | |
8567 | this->report_error(_("argument must be string or " | |
8568 | "array or slice or map or channel")); | |
8569 | } | |
8570 | } | |
8571 | } | |
8572 | break; | |
8573 | ||
8574 | case BUILTIN_PRINT: | |
8575 | case BUILTIN_PRINTLN: | |
8576 | { | |
8577 | const Expression_list* args = this->args(); | |
8578 | if (args == NULL) | |
8579 | { | |
8580 | if (this->code_ == BUILTIN_PRINT) | |
631d5788 | 8581 | go_warning_at(this->location(), 0, |
e440a328 | 8582 | "no arguments for builtin function %<%s%>", |
8583 | (this->code_ == BUILTIN_PRINT | |
8584 | ? "print" | |
8585 | : "println")); | |
8586 | } | |
8587 | else | |
8588 | { | |
8589 | for (Expression_list::const_iterator p = args->begin(); | |
8590 | p != args->end(); | |
8591 | ++p) | |
8592 | { | |
8593 | Type* type = (*p)->type(); | |
5c13bd80 | 8594 | if (type->is_error() |
e440a328 | 8595 | || type->is_string_type() |
8596 | || type->integer_type() != NULL | |
8597 | || type->float_type() != NULL | |
8598 | || type->complex_type() != NULL | |
8599 | || type->is_boolean_type() | |
8600 | || type->points_to() != NULL | |
8601 | || type->interface_type() != NULL | |
8602 | || type->channel_type() != NULL | |
8603 | || type->map_type() != NULL | |
8604 | || type->function_type() != NULL | |
411eb89e | 8605 | || type->is_slice_type()) |
e440a328 | 8606 | ; |
acf8e158 | 8607 | else if ((*p)->is_type_expression()) |
8608 | { | |
8609 | // If this is a type expression it's going to give | |
8610 | // an error anyhow, so we don't need one here. | |
8611 | } | |
e440a328 | 8612 | else |
8613 | this->report_error(_("unsupported argument type to " | |
8614 | "builtin function")); | |
8615 | } | |
8616 | } | |
8617 | } | |
8618 | break; | |
8619 | ||
8620 | case BUILTIN_CLOSE: | |
e440a328 | 8621 | if (this->check_one_arg()) |
8622 | { | |
8623 | if (this->one_arg()->type()->channel_type() == NULL) | |
8624 | this->report_error(_("argument must be channel")); | |
5202d986 | 8625 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8626 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8627 | } |
8628 | break; | |
8629 | ||
8630 | case BUILTIN_PANIC: | |
8631 | case BUILTIN_SIZEOF: | |
8632 | case BUILTIN_ALIGNOF: | |
8633 | this->check_one_arg(); | |
8634 | break; | |
8635 | ||
8636 | case BUILTIN_RECOVER: | |
6334270b | 8637 | if (this->args() != NULL |
8638 | && !this->args()->empty() | |
8639 | && !this->recover_arg_is_set_) | |
e440a328 | 8640 | this->report_error(_("too many arguments")); |
8641 | break; | |
8642 | ||
8643 | case BUILTIN_OFFSETOF: | |
8644 | if (this->check_one_arg()) | |
8645 | { | |
8646 | Expression* arg = this->one_arg(); | |
8647 | if (arg->field_reference_expression() == NULL) | |
8648 | this->report_error(_("argument must be a field reference")); | |
8649 | } | |
8650 | break; | |
8651 | ||
8652 | case BUILTIN_COPY: | |
8653 | { | |
8654 | const Expression_list* args = this->args(); | |
8655 | if (args == NULL || args->size() < 2) | |
8656 | { | |
8657 | this->report_error(_("not enough arguments")); | |
8658 | break; | |
8659 | } | |
8660 | else if (args->size() > 2) | |
8661 | { | |
8662 | this->report_error(_("too many arguments")); | |
8663 | break; | |
8664 | } | |
8665 | Type* arg1_type = args->front()->type(); | |
8666 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8667 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 8668 | { |
8669 | this->set_is_error(); | |
8670 | break; | |
8671 | } | |
e440a328 | 8672 | |
8673 | Type* e1; | |
411eb89e | 8674 | if (arg1_type->is_slice_type()) |
e440a328 | 8675 | e1 = arg1_type->array_type()->element_type(); |
8676 | else | |
8677 | { | |
8678 | this->report_error(_("left argument must be a slice")); | |
8679 | break; | |
8680 | } | |
8681 | ||
411eb89e | 8682 | if (arg2_type->is_slice_type()) |
60963afd | 8683 | { |
8684 | Type* e2 = arg2_type->array_type()->element_type(); | |
8685 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8686 | this->report_error(_("element types must be the same")); | |
8687 | } | |
e440a328 | 8688 | else if (arg2_type->is_string_type()) |
e440a328 | 8689 | { |
60963afd | 8690 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8691 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8692 | } |
60963afd | 8693 | else |
8694 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8695 | } |
8696 | break; | |
8697 | ||
8698 | case BUILTIN_APPEND: | |
8699 | { | |
8700 | const Expression_list* args = this->args(); | |
321e5ad2 | 8701 | if (args == NULL || args->empty()) |
e440a328 | 8702 | { |
8703 | this->report_error(_("not enough arguments")); | |
8704 | break; | |
8705 | } | |
321e5ad2 | 8706 | |
8707 | Type* slice_type = args->front()->type(); | |
8708 | if (!slice_type->is_slice_type()) | |
6bebb39d | 8709 | { |
321e5ad2 | 8710 | if (slice_type->is_error_type()) |
8711 | break; | |
8712 | if (slice_type->is_nil_type()) | |
8713 | go_error_at(args->front()->location(), "use of untyped nil"); | |
8714 | else | |
8715 | go_error_at(args->front()->location(), | |
8716 | "argument 1 must be a slice"); | |
6bebb39d | 8717 | this->set_is_error(); |
8718 | break; | |
8719 | } | |
cd238b8d | 8720 | |
321e5ad2 | 8721 | Type* element_type = slice_type->array_type()->element_type(); |
22deed0d | 8722 | if (!element_type->in_heap()) |
8723 | go_error_at(args->front()->location(), | |
8724 | "can't append to slice of go:notinheap type"); | |
321e5ad2 | 8725 | if (this->is_varargs()) |
4fd4fcf4 | 8726 | { |
321e5ad2 | 8727 | if (!args->back()->type()->is_slice_type() |
8728 | && !args->back()->type()->is_string_type()) | |
8729 | { | |
8730 | go_error_at(args->back()->location(), | |
8731 | "invalid use of %<...%> with non-slice/non-string"); | |
8732 | this->set_is_error(); | |
8733 | break; | |
8734 | } | |
4fd4fcf4 | 8735 | |
321e5ad2 | 8736 | if (args->size() < 2) |
8737 | { | |
8738 | this->report_error(_("not enough arguments")); | |
8739 | break; | |
8740 | } | |
8741 | if (args->size() > 2) | |
8742 | { | |
8743 | this->report_error(_("too many arguments")); | |
8744 | break; | |
8745 | } | |
8746 | ||
8747 | if (args->back()->type()->is_string_type() | |
8748 | && element_type->integer_type() != NULL | |
8749 | && element_type->integer_type()->is_byte()) | |
8750 | { | |
8751 | // Permit append(s1, s2...) when s1 is a slice of | |
8752 | // bytes and s2 is a string type. | |
8753 | } | |
e440a328 | 8754 | else |
8755 | { | |
321e5ad2 | 8756 | // We have to test for assignment compatibility to a |
8757 | // slice of the element type, which is not necessarily | |
8758 | // the same as the type of the first argument: the | |
8759 | // first argument might have a named type. | |
8760 | Type* check_type = Type::make_array_type(element_type, NULL); | |
8761 | std::string reason; | |
8762 | if (!Type::are_assignable(check_type, args->back()->type(), | |
8763 | &reason)) | |
8764 | { | |
8765 | if (reason.empty()) | |
8766 | go_error_at(args->back()->location(), | |
8767 | "argument 2 has invalid type"); | |
8768 | else | |
8769 | go_error_at(args->back()->location(), | |
8770 | "argument 2 has invalid type (%s)", | |
8771 | reason.c_str()); | |
8772 | this->set_is_error(); | |
8773 | break; | |
8774 | } | |
8775 | } | |
8776 | } | |
8777 | else | |
8778 | { | |
8779 | Expression_list::const_iterator pa = args->begin(); | |
8780 | int i = 2; | |
8781 | for (++pa; pa != args->end(); ++pa, ++i) | |
8782 | { | |
8783 | std::string reason; | |
8784 | if (!Type::are_assignable(element_type, (*pa)->type(), | |
8785 | &reason)) | |
8786 | { | |
8787 | if (reason.empty()) | |
8788 | go_error_at((*pa)->location(), | |
8789 | "argument %d has incompatible type", i); | |
8790 | else | |
8791 | go_error_at((*pa)->location(), | |
8792 | "argument %d has incompatible type (%s)", | |
8793 | i, reason.c_str()); | |
8794 | this->set_is_error(); | |
8795 | } | |
e440a328 | 8796 | } |
8797 | } | |
e440a328 | 8798 | } |
321e5ad2 | 8799 | break; |
e440a328 | 8800 | |
8801 | case BUILTIN_REAL: | |
8802 | case BUILTIN_IMAG: | |
8803 | if (this->check_one_arg()) | |
8804 | { | |
8805 | if (this->one_arg()->type()->complex_type() == NULL) | |
8806 | this->report_error(_("argument must have complex type")); | |
8807 | } | |
8808 | break; | |
8809 | ||
48080209 | 8810 | case BUILTIN_COMPLEX: |
e440a328 | 8811 | { |
8812 | const Expression_list* args = this->args(); | |
8813 | if (args == NULL || args->size() < 2) | |
8814 | this->report_error(_("not enough arguments")); | |
8815 | else if (args->size() > 2) | |
8816 | this->report_error(_("too many arguments")); | |
8817 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8818 | || args->front()->type()->is_error() |
e440a328 | 8819 | || args->back()->is_error_expression() |
5c13bd80 | 8820 | || args->back()->type()->is_error()) |
e440a328 | 8821 | this->set_is_error(); |
8822 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8823 | args->back()->type(), true, NULL)) |
48080209 | 8824 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8825 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8826 | this->report_error(_("complex arguments must have " |
e440a328 | 8827 | "floating-point type")); |
8828 | } | |
8829 | break; | |
8830 | ||
8831 | default: | |
c3e6f413 | 8832 | go_unreachable(); |
e440a328 | 8833 | } |
8834 | } | |
8835 | ||
72666aed | 8836 | Expression* |
8837 | Builtin_call_expression::do_copy() | |
8838 | { | |
8839 | Call_expression* bce = | |
8840 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8841 | (this->args() == NULL |
8842 | ? NULL | |
8843 | : this->args()->copy()), | |
72666aed | 8844 | this->is_varargs(), |
8845 | this->location()); | |
8846 | ||
8847 | if (this->varargs_are_lowered()) | |
8848 | bce->set_varargs_are_lowered(); | |
8849 | return bce; | |
8850 | } | |
8851 | ||
ea664253 | 8852 | // Return the backend representation for a builtin function. |
e440a328 | 8853 | |
ea664253 | 8854 | Bexpression* |
8855 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8856 | { |
8857 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8858 | Location location = this->location(); |
a0d8874e | 8859 | |
8860 | if (this->is_erroneous_call()) | |
8861 | { | |
8862 | go_assert(saw_errors()); | |
8863 | return gogo->backend()->error_expression(); | |
8864 | } | |
8865 | ||
e440a328 | 8866 | switch (this->code_) |
8867 | { | |
8868 | case BUILTIN_INVALID: | |
8869 | case BUILTIN_NEW: | |
8870 | case BUILTIN_MAKE: | |
c3e6f413 | 8871 | go_unreachable(); |
e440a328 | 8872 | |
8873 | case BUILTIN_LEN: | |
8874 | case BUILTIN_CAP: | |
8875 | { | |
8876 | const Expression_list* args = this->args(); | |
c484d925 | 8877 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8878 | Expression* arg = args->front(); |
e440a328 | 8879 | Type* arg_type = arg->type(); |
0f914071 | 8880 | |
8881 | if (this->seen_) | |
8882 | { | |
c484d925 | 8883 | go_assert(saw_errors()); |
ea664253 | 8884 | return context->backend()->error_expression(); |
0f914071 | 8885 | } |
8886 | this->seen_ = true; | |
0f914071 | 8887 | this->seen_ = false; |
e440a328 | 8888 | if (arg_type->points_to() != NULL) |
8889 | { | |
8890 | arg_type = arg_type->points_to(); | |
c484d925 | 8891 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8892 | && !arg_type->is_slice_type()); |
f614ea8b | 8893 | arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT, |
8894 | location); | |
e440a328 | 8895 | } |
8896 | ||
1b1f2abf | 8897 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8898 | Expression* val; |
e440a328 | 8899 | if (this->code_ == BUILTIN_LEN) |
8900 | { | |
8901 | if (arg_type->is_string_type()) | |
2c809f8f | 8902 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8903 | location); | |
e440a328 | 8904 | else if (arg_type->array_type() != NULL) |
0f914071 | 8905 | { |
8906 | if (this->seen_) | |
8907 | { | |
c484d925 | 8908 | go_assert(saw_errors()); |
ea664253 | 8909 | return context->backend()->error_expression(); |
0f914071 | 8910 | } |
8911 | this->seen_ = true; | |
2c809f8f | 8912 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8913 | this->seen_ = false; |
8914 | } | |
0d5530d9 | 8915 | else if (arg_type->map_type() != NULL |
8916 | || arg_type->channel_type() != NULL) | |
8917 | { | |
8918 | // The first field is the length. If the pointer is | |
8919 | // nil, the length is zero. | |
8920 | Type* pint_type = Type::make_pointer_type(int_type); | |
8921 | arg = Expression::make_unsafe_cast(pint_type, arg, location); | |
8922 | Expression* nil = Expression::make_nil(location); | |
8923 | nil = Expression::make_cast(pint_type, nil, location); | |
8924 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8925 | arg, nil, location); | |
8926 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8927 | location); | |
f614ea8b | 8928 | Expression* indir = |
8929 | Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, | |
8930 | location); | |
0d5530d9 | 8931 | val = Expression::make_conditional(cmp, zero, indir, location); |
8932 | } | |
e440a328 | 8933 | else |
c3e6f413 | 8934 | go_unreachable(); |
e440a328 | 8935 | } |
8936 | else | |
8937 | { | |
8938 | if (arg_type->array_type() != NULL) | |
0f914071 | 8939 | { |
8940 | if (this->seen_) | |
8941 | { | |
c484d925 | 8942 | go_assert(saw_errors()); |
ea664253 | 8943 | return context->backend()->error_expression(); |
0f914071 | 8944 | } |
8945 | this->seen_ = true; | |
2c809f8f | 8946 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8947 | this->seen_ = false; |
8948 | } | |
e440a328 | 8949 | else if (arg_type->channel_type() != NULL) |
132ed071 | 8950 | { |
8951 | // The second field is the capacity. If the pointer | |
8952 | // is nil, the capacity is zero. | |
8953 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
8954 | Type* pint_type = Type::make_pointer_type(int_type); | |
8955 | Expression* parg = Expression::make_unsafe_cast(uintptr_type, | |
8956 | arg, | |
8957 | location); | |
8958 | int off = int_type->integer_type()->bits() / 8; | |
8959 | Expression* eoff = Expression::make_integer_ul(off, | |
8960 | uintptr_type, | |
8961 | location); | |
8962 | parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, | |
8963 | location); | |
8964 | parg = Expression::make_unsafe_cast(pint_type, parg, location); | |
8965 | Expression* nil = Expression::make_nil(location); | |
8966 | nil = Expression::make_cast(pint_type, nil, location); | |
8967 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8968 | arg, nil, location); | |
8969 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8970 | location); | |
f614ea8b | 8971 | Expression* indir = |
8972 | Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED, | |
8973 | location); | |
132ed071 | 8974 | val = Expression::make_conditional(cmp, zero, indir, location); |
8975 | } | |
e440a328 | 8976 | else |
c3e6f413 | 8977 | go_unreachable(); |
e440a328 | 8978 | } |
8979 | ||
2c809f8f | 8980 | return Expression::make_cast(int_type, val, |
ea664253 | 8981 | location)->get_backend(context); |
e440a328 | 8982 | } |
8983 | ||
8984 | case BUILTIN_PRINT: | |
8985 | case BUILTIN_PRINTLN: | |
8986 | { | |
8987 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
88b03a70 | 8988 | |
8989 | Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, | |
8990 | location, 0); | |
e440a328 | 8991 | |
8992 | const Expression_list* call_args = this->args(); | |
8993 | if (call_args != NULL) | |
8994 | { | |
8995 | for (Expression_list::const_iterator p = call_args->begin(); | |
8996 | p != call_args->end(); | |
8997 | ++p) | |
8998 | { | |
8999 | if (is_ln && p != call_args->begin()) | |
9000 | { | |
2c809f8f | 9001 | Expression* print_space = |
88b03a70 | 9002 | Runtime::make_call(Runtime::PRINTSP, location, 0); |
e440a328 | 9003 | |
2c809f8f | 9004 | print_stmts = |
9005 | Expression::make_compound(print_stmts, print_space, | |
9006 | location); | |
9007 | } | |
e440a328 | 9008 | |
2c809f8f | 9009 | Expression* arg = *p; |
9010 | Type* type = arg->type(); | |
9011 | Runtime::Function code; | |
e440a328 | 9012 | if (type->is_string_type()) |
88b03a70 | 9013 | code = Runtime::PRINTSTRING; |
e440a328 | 9014 | else if (type->integer_type() != NULL |
9015 | && type->integer_type()->is_unsigned()) | |
9016 | { | |
e440a328 | 9017 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 9018 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9019 | code = Runtime::PRINTUINT; |
e440a328 | 9020 | } |
9021 | else if (type->integer_type() != NULL) | |
9022 | { | |
e440a328 | 9023 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 9024 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9025 | code = Runtime::PRINTINT; |
e440a328 | 9026 | } |
9027 | else if (type->float_type() != NULL) | |
9028 | { | |
2c809f8f | 9029 | Type* dtype = Type::lookup_float_type("float64"); |
9030 | arg = Expression::make_cast(dtype, arg, location); | |
88b03a70 | 9031 | code = Runtime::PRINTFLOAT; |
e440a328 | 9032 | } |
9033 | else if (type->complex_type() != NULL) | |
9034 | { | |
2c809f8f | 9035 | Type* ctype = Type::lookup_complex_type("complex128"); |
9036 | arg = Expression::make_cast(ctype, arg, location); | |
88b03a70 | 9037 | code = Runtime::PRINTCOMPLEX; |
e440a328 | 9038 | } |
9039 | else if (type->is_boolean_type()) | |
88b03a70 | 9040 | code = Runtime::PRINTBOOL; |
e440a328 | 9041 | else if (type->points_to() != NULL |
9042 | || type->channel_type() != NULL | |
9043 | || type->map_type() != NULL | |
9044 | || type->function_type() != NULL) | |
9045 | { | |
2c809f8f | 9046 | arg = Expression::make_cast(type, arg, location); |
88b03a70 | 9047 | code = Runtime::PRINTPOINTER; |
e440a328 | 9048 | } |
9049 | else if (type->interface_type() != NULL) | |
9050 | { | |
9051 | if (type->interface_type()->is_empty()) | |
88b03a70 | 9052 | code = Runtime::PRINTEFACE; |
e440a328 | 9053 | else |
88b03a70 | 9054 | code = Runtime::PRINTIFACE; |
e440a328 | 9055 | } |
411eb89e | 9056 | else if (type->is_slice_type()) |
88b03a70 | 9057 | code = Runtime::PRINTSLICE; |
e440a328 | 9058 | else |
cd238b8d | 9059 | { |
9060 | go_assert(saw_errors()); | |
ea664253 | 9061 | return context->backend()->error_expression(); |
cd238b8d | 9062 | } |
e440a328 | 9063 | |
2c809f8f | 9064 | Expression* call = Runtime::make_call(code, location, 1, arg); |
88b03a70 | 9065 | print_stmts = Expression::make_compound(print_stmts, call, |
9066 | location); | |
e440a328 | 9067 | } |
9068 | } | |
9069 | ||
9070 | if (is_ln) | |
9071 | { | |
2c809f8f | 9072 | Expression* print_nl = |
88b03a70 | 9073 | Runtime::make_call(Runtime::PRINTNL, location, 0); |
9074 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
9075 | location); | |
e440a328 | 9076 | } |
9077 | ||
88b03a70 | 9078 | Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, |
9079 | location, 0); | |
9080 | print_stmts = Expression::make_compound(print_stmts, unlock, location); | |
32e3ff69 | 9081 | |
ea664253 | 9082 | return print_stmts->get_backend(context); |
e440a328 | 9083 | } |
9084 | ||
9085 | case BUILTIN_PANIC: | |
9086 | { | |
9087 | const Expression_list* args = this->args(); | |
c484d925 | 9088 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9089 | Expression* arg = args->front(); |
b13c66cd | 9090 | Type *empty = |
823c7e3d | 9091 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 9092 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
9093 | ||
9094 | Expression* panic = | |
03ac9de4 | 9095 | Runtime::make_call(Runtime::GOPANIC, location, 1, arg); |
ea664253 | 9096 | return panic->get_backend(context); |
e440a328 | 9097 | } |
9098 | ||
9099 | case BUILTIN_RECOVER: | |
9100 | { | |
9101 | // The argument is set when building recover thunks. It's a | |
9102 | // boolean value which is true if we can recover a value now. | |
9103 | const Expression_list* args = this->args(); | |
c484d925 | 9104 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9105 | Expression* arg = args->front(); |
b13c66cd | 9106 | Type *empty = |
823c7e3d | 9107 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 9108 | |
e440a328 | 9109 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 9110 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 9111 | |
9112 | // We need to handle a deferred call to recover specially, | |
9113 | // because it changes whether it can recover a panic or not. | |
9114 | // See test7 in test/recover1.go. | |
2c809f8f | 9115 | Expression* recover = Runtime::make_call((this->is_deferred() |
03ac9de4 | 9116 | ? Runtime::DEFERREDRECOVER |
9117 | : Runtime::GORECOVER), | |
2c809f8f | 9118 | location, 0); |
9119 | Expression* cond = | |
9120 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 9121 | return cond->get_backend(context); |
e440a328 | 9122 | } |
9123 | ||
9124 | case BUILTIN_CLOSE: | |
e440a328 | 9125 | { |
9126 | const Expression_list* args = this->args(); | |
c484d925 | 9127 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9128 | Expression* arg = args->front(); |
2c809f8f | 9129 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
9130 | 1, arg); | |
ea664253 | 9131 | return close->get_backend(context); |
e440a328 | 9132 | } |
9133 | ||
9134 | case BUILTIN_SIZEOF: | |
9135 | case BUILTIN_OFFSETOF: | |
9136 | case BUILTIN_ALIGNOF: | |
9137 | { | |
0c77715b | 9138 | Numeric_constant nc; |
9139 | unsigned long val; | |
9140 | if (!this->numeric_constant_value(&nc) | |
9141 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 9142 | { |
c484d925 | 9143 | go_assert(saw_errors()); |
ea664253 | 9144 | return context->backend()->error_expression(); |
7f1d9abd | 9145 | } |
7ba86326 | 9146 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 9147 | mpz_t ival; |
9148 | nc.get_int(&ival); | |
9149 | Expression* int_cst = | |
e67508fa | 9150 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 9151 | mpz_clear(ival); |
ea664253 | 9152 | return int_cst->get_backend(context); |
e440a328 | 9153 | } |
9154 | ||
9155 | case BUILTIN_COPY: | |
9156 | { | |
9157 | const Expression_list* args = this->args(); | |
c484d925 | 9158 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 9159 | Expression* arg1 = args->front(); |
9160 | Expression* arg2 = args->back(); | |
9161 | ||
e440a328 | 9162 | Type* arg1_type = arg1->type(); |
9163 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 9164 | go_assert(arg1->is_variable()); |
321e5ad2 | 9165 | |
9166 | Expression* call; | |
e440a328 | 9167 | |
9168 | Type* arg2_type = arg2->type(); | |
2c809f8f | 9169 | go_assert(arg2->is_variable()); |
321e5ad2 | 9170 | if (arg2_type->is_string_type()) |
9171 | call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location, | |
9172 | 2, arg1, arg2); | |
e440a328 | 9173 | else |
9174 | { | |
321e5ad2 | 9175 | Type* et = at->element_type(); |
9176 | if (et->has_pointer()) | |
9177 | { | |
9178 | Expression* td = Expression::make_type_descriptor(et, | |
9179 | location); | |
9180 | call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location, | |
9181 | 3, td, arg1, arg2); | |
9182 | } | |
9183 | else | |
9184 | { | |
9185 | Expression* sz = Expression::make_type_info(et, | |
9186 | TYPE_INFO_SIZE); | |
9187 | call = Runtime::make_call(Runtime::SLICECOPY, location, 3, | |
9188 | arg1, arg2, sz); | |
9189 | } | |
e440a328 | 9190 | } |
2c809f8f | 9191 | |
321e5ad2 | 9192 | return call->get_backend(context); |
e440a328 | 9193 | } |
9194 | ||
9195 | case BUILTIN_APPEND: | |
321e5ad2 | 9196 | // Handled in Builtin_call_expression::flatten_append. |
9197 | go_unreachable(); | |
e440a328 | 9198 | |
9199 | case BUILTIN_REAL: | |
9200 | case BUILTIN_IMAG: | |
9201 | { | |
9202 | const Expression_list* args = this->args(); | |
c484d925 | 9203 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 9204 | |
9205 | Bexpression* ret; | |
ea664253 | 9206 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 9207 | if (this->code_ == BUILTIN_REAL) |
9208 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
9209 | else | |
9210 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 9211 | return ret; |
e440a328 | 9212 | } |
9213 | ||
48080209 | 9214 | case BUILTIN_COMPLEX: |
e440a328 | 9215 | { |
9216 | const Expression_list* args = this->args(); | |
c484d925 | 9217 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 9218 | Bexpression* breal = args->front()->get_backend(context); |
9219 | Bexpression* bimag = args->back()->get_backend(context); | |
9220 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 9221 | } |
9222 | ||
9223 | default: | |
c3e6f413 | 9224 | go_unreachable(); |
e440a328 | 9225 | } |
9226 | } | |
9227 | ||
9228 | // We have to support exporting a builtin call expression, because | |
9229 | // code can set a constant to the result of a builtin expression. | |
9230 | ||
9231 | void | |
9232 | Builtin_call_expression::do_export(Export* exp) const | |
9233 | { | |
0c77715b | 9234 | Numeric_constant nc; |
9235 | if (!this->numeric_constant_value(&nc)) | |
9236 | { | |
631d5788 | 9237 | go_error_at(this->location(), "value is not constant"); |
0c77715b | 9238 | return; |
9239 | } | |
e440a328 | 9240 | |
0c77715b | 9241 | if (nc.is_int()) |
e440a328 | 9242 | { |
0c77715b | 9243 | mpz_t val; |
9244 | nc.get_int(&val); | |
e440a328 | 9245 | Integer_expression::export_integer(exp, val); |
0c77715b | 9246 | mpz_clear(val); |
e440a328 | 9247 | } |
0c77715b | 9248 | else if (nc.is_float()) |
e440a328 | 9249 | { |
9250 | mpfr_t fval; | |
0c77715b | 9251 | nc.get_float(&fval); |
9252 | Float_expression::export_float(exp, fval); | |
e440a328 | 9253 | mpfr_clear(fval); |
9254 | } | |
0c77715b | 9255 | else if (nc.is_complex()) |
e440a328 | 9256 | { |
fcbea5e4 | 9257 | mpc_t cval; |
9258 | nc.get_complex(&cval); | |
9259 | Complex_expression::export_complex(exp, cval); | |
9260 | mpc_clear(cval); | |
e440a328 | 9261 | } |
0c77715b | 9262 | else |
9263 | go_unreachable(); | |
e440a328 | 9264 | |
9265 | // A trailing space lets us reliably identify the end of the number. | |
9266 | exp->write_c_string(" "); | |
9267 | } | |
9268 | ||
9269 | // Class Call_expression. | |
9270 | ||
8381eda7 | 9271 | // A Go function can be viewed in a couple of different ways. The |
9272 | // code of a Go function becomes a backend function with parameters | |
9273 | // whose types are simply the backend representation of the Go types. | |
9274 | // If there are multiple results, they are returned as a backend | |
9275 | // struct. | |
9276 | ||
9277 | // However, when Go code refers to a function other than simply | |
9278 | // calling it, the backend type of that function is actually a struct. | |
9279 | // The first field of the struct points to the Go function code | |
9280 | // (sometimes a wrapper as described below). The remaining fields | |
9281 | // hold addresses of closed-over variables. This struct is called a | |
9282 | // closure. | |
9283 | ||
9284 | // There are a few cases to consider. | |
9285 | ||
9286 | // A direct function call of a known function in package scope. In | |
9287 | // this case there are no closed-over variables, and we know the name | |
9288 | // of the function code. We can simply produce a backend call to the | |
9289 | // function directly, and not worry about the closure. | |
9290 | ||
9291 | // A direct function call of a known function literal. In this case | |
9292 | // we know the function code and we know the closure. We generate the | |
9293 | // function code such that it expects an additional final argument of | |
9294 | // the closure type. We pass the closure as the last argument, after | |
9295 | // the other arguments. | |
9296 | ||
9297 | // An indirect function call. In this case we have a closure. We | |
9298 | // load the pointer to the function code from the first field of the | |
9299 | // closure. We pass the address of the closure as the last argument. | |
9300 | ||
9301 | // A call to a method of an interface. Type methods are always at | |
9302 | // package scope, so we call the function directly, and don't worry | |
9303 | // about the closure. | |
9304 | ||
9305 | // This means that for a function at package scope we have two cases. | |
9306 | // One is the direct call, which has no closure. The other is the | |
9307 | // indirect call, which does have a closure. We can't simply ignore | |
9308 | // the closure, even though it is the last argument, because that will | |
9309 | // fail on targets where the function pops its arguments. So when | |
9310 | // generating a closure for a package-scope function we set the | |
9311 | // function code pointer in the closure to point to a wrapper | |
9312 | // function. This wrapper function accepts a final argument that | |
9313 | // points to the closure, ignores it, and calls the real function as a | |
9314 | // direct function call. This wrapper will normally be efficient, and | |
9315 | // can often simply be a tail call to the real function. | |
9316 | ||
9317 | // We don't use GCC's static chain pointer because 1) we don't need | |
9318 | // it; 2) GCC only permits using a static chain to call a known | |
9319 | // function, so we can't use it for an indirect call anyhow. Since we | |
9320 | // can't use it for an indirect call, we may as well not worry about | |
9321 | // using it for a direct call either. | |
9322 | ||
9323 | // We pass the closure last rather than first because it means that | |
9324 | // the function wrapper we put into a closure for a package-scope | |
9325 | // function can normally just be a tail call to the real function. | |
9326 | ||
9327 | // For method expressions we generate a wrapper that loads the | |
9328 | // receiver from the closure and then calls the method. This | |
9329 | // unfortunately forces reshuffling the arguments, since there is a | |
9330 | // new first argument, but we can't avoid reshuffling either for | |
9331 | // method expressions or for indirect calls of package-scope | |
9332 | // functions, and since the latter are more common we reshuffle for | |
9333 | // method expressions. | |
9334 | ||
9335 | // Note that the Go code retains the Go types. The extra final | |
9336 | // argument only appears when we convert to the backend | |
9337 | // representation. | |
9338 | ||
e440a328 | 9339 | // Traversal. |
9340 | ||
9341 | int | |
9342 | Call_expression::do_traverse(Traverse* traverse) | |
9343 | { | |
0c0dacab | 9344 | // If we are calling a function in a different package that returns |
9345 | // an unnamed type, this may be the only chance we get to traverse | |
9346 | // that type. We don't traverse this->type_ because it may be a | |
9347 | // Call_multiple_result_type that will just lead back here. | |
9348 | if (this->type_ != NULL && !this->type_->is_error_type()) | |
9349 | { | |
9350 | Function_type *fntype = this->get_function_type(); | |
9351 | if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) | |
9352 | return TRAVERSE_EXIT; | |
9353 | } | |
e440a328 | 9354 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) |
9355 | return TRAVERSE_EXIT; | |
9356 | if (this->args_ != NULL) | |
9357 | { | |
9358 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
9359 | return TRAVERSE_EXIT; | |
9360 | } | |
9361 | return TRAVERSE_CONTINUE; | |
9362 | } | |
9363 | ||
9364 | // Lower a call statement. | |
9365 | ||
9366 | Expression* | |
ceeb4318 | 9367 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
9368 | Statement_inserter* inserter, int) | |
e440a328 | 9369 | { |
b13c66cd | 9370 | Location loc = this->location(); |
09ea332d | 9371 | |
ceeb4318 | 9372 | // A type cast can look like a function call. |
e440a328 | 9373 | if (this->fn_->is_type_expression() |
9374 | && this->args_ != NULL | |
9375 | && this->args_->size() == 1) | |
9376 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 9377 | loc); |
e440a328 | 9378 | |
88f06749 | 9379 | // Because do_type will return an error type and thus prevent future |
9380 | // errors, check for that case now to ensure that the error gets | |
9381 | // reported. | |
37448b10 | 9382 | Function_type* fntype = this->get_function_type(); |
9383 | if (fntype == NULL) | |
88f06749 | 9384 | { |
9385 | if (!this->fn_->type()->is_error()) | |
9386 | this->report_error(_("expected function")); | |
5f1045b5 | 9387 | this->set_is_error(); |
9388 | return this; | |
88f06749 | 9389 | } |
9390 | ||
e440a328 | 9391 | // Handle an argument which is a call to a function which returns |
9392 | // multiple results. | |
9393 | if (this->args_ != NULL | |
9394 | && this->args_->size() == 1 | |
37448b10 | 9395 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9396 | { |
e440a328 | 9397 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9398 | if (rc > 1 | |
37448b10 | 9399 | && ((fntype->parameters() != NULL |
9400 | && (fntype->parameters()->size() == rc | |
9401 | || (fntype->is_varargs() | |
9402 | && fntype->parameters()->size() - 1 <= rc))) | |
9403 | || fntype->is_builtin())) | |
e440a328 | 9404 | { |
9405 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 9406 | call->set_is_multi_value_arg(); |
c33af8e4 | 9407 | if (this->is_varargs_) |
9408 | { | |
9409 | // It is not clear which result of a multiple result call | |
9410 | // the ellipsis operator should be applied to. If we unpack the | |
9411 | // the call into its individual results here, the ellipsis will be | |
9412 | // applied to the last result. | |
631d5788 | 9413 | go_error_at(call->location(), |
9414 | _("multiple-value argument in single-value context")); | |
c33af8e4 | 9415 | return Expression::make_error(call->location()); |
9416 | } | |
9417 | ||
e440a328 | 9418 | Expression_list* args = new Expression_list; |
9419 | for (size_t i = 0; i < rc; ++i) | |
9420 | args->push_back(Expression::make_call_result(call, i)); | |
9421 | // We can't return a new call expression here, because this | |
42535814 | 9422 | // one may be referenced by Call_result expressions. We |
9423 | // also can't delete the old arguments, because we may still | |
9424 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9425 | this->args_ = args; |
9426 | } | |
9427 | } | |
9428 | ||
37448b10 | 9429 | // Recognize a call to a builtin function. |
9430 | if (fntype->is_builtin()) | |
9431 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9432 | this->is_varargs_, loc); | |
9433 | ||
ceeb4318 | 9434 | // If this call returns multiple results, create a temporary |
5731103c | 9435 | // variable to hold them. |
9436 | if (this->result_count() > 1 && this->call_temp_ == NULL) | |
ceeb4318 | 9437 | { |
5731103c | 9438 | Struct_field_list* sfl = new Struct_field_list(); |
9439 | Function_type* fntype = this->get_function_type(); | |
37448b10 | 9440 | const Typed_identifier_list* results = fntype->results(); |
5731103c | 9441 | Location loc = this->location(); |
9442 | ||
9443 | int i = 0; | |
9444 | char buf[20]; | |
ceeb4318 | 9445 | for (Typed_identifier_list::const_iterator p = results->begin(); |
5731103c | 9446 | p != results->end(); |
9447 | ++p, ++i) | |
9448 | { | |
9449 | snprintf(buf, sizeof buf, "res%d", i); | |
9450 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9451 | } | |
9452 | ||
9453 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9454 | st->set_is_struct_incomparable(); | |
9455 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9456 | inserter->insert(this->call_temp_); | |
ceeb4318 | 9457 | } |
9458 | ||
e440a328 | 9459 | // Handle a call to a varargs function by packaging up the extra |
9460 | // parameters. | |
37448b10 | 9461 | if (fntype->is_varargs()) |
e440a328 | 9462 | { |
e440a328 | 9463 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9464 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9465 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9466 | this->lower_varargs(gogo, function, inserter, varargs_type, |
0e9a2e72 | 9467 | parameters->size(), SLICE_STORAGE_MAY_ESCAPE); |
09ea332d | 9468 | } |
9469 | ||
9470 | // If this is call to a method, call the method directly passing the | |
9471 | // object as the first parameter. | |
9472 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9473 | if (bme != NULL) | |
9474 | { | |
0afbb937 | 9475 | Named_object* methodfn = bme->function(); |
09ea332d | 9476 | Expression* first_arg = bme->first_argument(); |
9477 | ||
9478 | // We always pass a pointer when calling a method. | |
9479 | if (first_arg->type()->points_to() == NULL | |
9480 | && !first_arg->type()->is_error()) | |
9481 | { | |
9482 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9483 | // We may need to create a temporary variable so that we can | |
9484 | // take the address. We can't do that here because it will | |
9485 | // mess up the order of evaluation. | |
9486 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9487 | ue->set_create_temp(); | |
9488 | } | |
9489 | ||
9490 | // If we are calling a method which was inherited from an | |
9491 | // embedded struct, and the method did not get a stub, then the | |
9492 | // first type may be wrong. | |
9493 | Type* fatype = bme->first_argument_type(); | |
9494 | if (fatype != NULL) | |
9495 | { | |
9496 | if (fatype->points_to() == NULL) | |
9497 | fatype = Type::make_pointer_type(fatype); | |
9498 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9499 | } | |
9500 | ||
9501 | Expression_list* new_args = new Expression_list(); | |
9502 | new_args->push_back(first_arg); | |
9503 | if (this->args_ != NULL) | |
9504 | { | |
9505 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9506 | p != this->args_->end(); | |
9507 | ++p) | |
9508 | new_args->push_back(*p); | |
9509 | } | |
9510 | ||
9511 | // We have to change in place because this structure may be | |
9512 | // referenced by Call_result_expressions. We can't delete the | |
9513 | // old arguments, because we may be traversing them up in some | |
9514 | // caller. FIXME. | |
9515 | this->args_ = new_args; | |
0afbb937 | 9516 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9517 | bme->location()); |
e440a328 | 9518 | } |
9519 | ||
105f9a24 | 9520 | // Handle a couple of special runtime functions. In the runtime |
9521 | // package, getcallerpc returns the PC of the caller, and | |
9522 | // getcallersp returns the frame pointer of the caller. Implement | |
9523 | // these by turning them into calls to GCC builtin functions. We | |
9524 | // could implement them in normal code, but then we would have to | |
9525 | // explicitly unwind the stack. These functions are intended to be | |
9526 | // efficient. Note that this technique obviously only works for | |
33d1d391 | 9527 | // direct calls, but that is the only way they are used. |
9528 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
105f9a24 | 9529 | { |
9530 | Func_expression* fe = this->fn_->func_expression(); | |
9531 | if (fe != NULL | |
9532 | && fe->named_object()->is_function_declaration() | |
9533 | && fe->named_object()->package() == NULL) | |
9534 | { | |
9535 | std::string n = Gogo::unpack_hidden_name(fe->named_object()->name()); | |
33d1d391 | 9536 | if ((this->args_ == NULL || this->args_->size() == 0) |
9537 | && n == "getcallerpc") | |
105f9a24 | 9538 | { |
9539 | static Named_object* builtin_return_address; | |
9540 | return this->lower_to_builtin(&builtin_return_address, | |
9541 | "__builtin_return_address", | |
9542 | 0); | |
9543 | } | |
33d1d391 | 9544 | else if (this->args_ != NULL |
9545 | && this->args_->size() == 1 | |
9546 | && n == "getcallersp") | |
105f9a24 | 9547 | { |
33d1d391 | 9548 | // The actual argument to getcallersp is always the |
9549 | // address of a parameter; we don't need that for the | |
9550 | // GCC builtin function, so we just ignore it. | |
105f9a24 | 9551 | static Named_object* builtin_frame_address; |
9552 | return this->lower_to_builtin(&builtin_frame_address, | |
9553 | "__builtin_frame_address", | |
9554 | 1); | |
9555 | } | |
9556 | } | |
9557 | } | |
9558 | ||
e440a328 | 9559 | return this; |
9560 | } | |
9561 | ||
9562 | // Lower a call to a varargs function. FUNCTION is the function in | |
9563 | // which the call occurs--it's not the function we are calling. | |
9564 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9565 | // PARAM_COUNT is the number of parameters of the function we are | |
9566 | // calling; the last of these parameters will be the varargs | |
9567 | // parameter. | |
9568 | ||
09ea332d | 9569 | void |
e440a328 | 9570 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9571 | Statement_inserter* inserter, |
0e9a2e72 | 9572 | Type* varargs_type, size_t param_count, |
9573 | Slice_storage_escape_disp escape_disp) | |
e440a328 | 9574 | { |
03118c21 | 9575 | // When compiling the runtime, varargs slices do not escape. When |
9576 | // escape analysis becomes the default, this should be changed to | |
9577 | // make it an error if we have a varargs slice that escapes. | |
9578 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
9579 | escape_disp = SLICE_STORAGE_DOES_NOT_ESCAPE; | |
9580 | ||
e440a328 | 9581 | if (this->varargs_are_lowered_) |
09ea332d | 9582 | return; |
e440a328 | 9583 | |
b13c66cd | 9584 | Location loc = this->location(); |
e440a328 | 9585 | |
c484d925 | 9586 | go_assert(param_count > 0); |
411eb89e | 9587 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9588 | |
9589 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9590 | if (arg_count < param_count - 1) | |
9591 | { | |
9592 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9593 | return; |
e440a328 | 9594 | } |
9595 | ||
9596 | Expression_list* old_args = this->args_; | |
9597 | Expression_list* new_args = new Expression_list(); | |
9598 | bool push_empty_arg = false; | |
9599 | if (old_args == NULL || old_args->empty()) | |
9600 | { | |
c484d925 | 9601 | go_assert(param_count == 1); |
e440a328 | 9602 | push_empty_arg = true; |
9603 | } | |
9604 | else | |
9605 | { | |
9606 | Expression_list::const_iterator pa; | |
9607 | int i = 1; | |
9608 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9609 | { | |
9610 | if (static_cast<size_t>(i) == param_count) | |
9611 | break; | |
9612 | new_args->push_back(*pa); | |
9613 | } | |
9614 | ||
9615 | // We have reached the varargs parameter. | |
9616 | ||
9617 | bool issued_error = false; | |
9618 | if (pa == old_args->end()) | |
9619 | push_empty_arg = true; | |
9620 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9621 | new_args->push_back(*pa); | |
9622 | else if (this->is_varargs_) | |
9623 | { | |
a6645f74 | 9624 | if ((*pa)->type()->is_slice_type()) |
9625 | this->report_error(_("too many arguments")); | |
9626 | else | |
9627 | { | |
631d5788 | 9628 | go_error_at(this->location(), |
9629 | _("invalid use of %<...%> with non-slice")); | |
a6645f74 | 9630 | this->set_is_error(); |
9631 | } | |
09ea332d | 9632 | return; |
e440a328 | 9633 | } |
e440a328 | 9634 | else |
9635 | { | |
9636 | Type* element_type = varargs_type->array_type()->element_type(); | |
9637 | Expression_list* vals = new Expression_list; | |
9638 | for (; pa != old_args->end(); ++pa, ++i) | |
9639 | { | |
9640 | // Check types here so that we get a better message. | |
9641 | Type* patype = (*pa)->type(); | |
b13c66cd | 9642 | Location paloc = (*pa)->location(); |
e440a328 | 9643 | if (!this->check_argument_type(i, element_type, patype, |
9644 | paloc, issued_error)) | |
9645 | continue; | |
9646 | vals->push_back(*pa); | |
9647 | } | |
0e9a2e72 | 9648 | Slice_construction_expression* sce = |
e440a328 | 9649 | Expression::make_slice_composite_literal(varargs_type, vals, loc); |
0e9a2e72 | 9650 | if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) |
9651 | sce->set_storage_does_not_escape(); | |
9652 | Expression* val = sce; | |
09ea332d | 9653 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9654 | new_args->push_back(val); |
9655 | } | |
9656 | } | |
9657 | ||
9658 | if (push_empty_arg) | |
9659 | new_args->push_back(Expression::make_nil(loc)); | |
9660 | ||
9661 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9662 | // be referenced by Call_result expressions. FIXME. We can't |
9663 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9664 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9665 | this->args_ = new_args; |
9666 | this->varargs_are_lowered_ = true; | |
e440a328 | 9667 | } |
9668 | ||
105f9a24 | 9669 | // Return a call to __builtin_return_address or __builtin_frame_address. |
9670 | ||
9671 | Expression* | |
9672 | Call_expression::lower_to_builtin(Named_object** pno, const char* name, | |
9673 | int arg) | |
9674 | { | |
9675 | if (*pno == NULL) | |
9676 | *pno = Gogo::declare_builtin_rf_address(name); | |
9677 | ||
9678 | Location loc = this->location(); | |
9679 | ||
9680 | Expression* fn = Expression::make_func_reference(*pno, NULL, loc); | |
9681 | Expression* a = Expression::make_integer_ul(arg, NULL, loc); | |
9682 | Expression_list *args = new Expression_list(); | |
9683 | args->push_back(a); | |
9684 | Expression* call = Expression::make_call(fn, args, false, loc); | |
9685 | ||
9686 | // The builtin functions return void*, but the Go functions return uintptr. | |
9687 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9688 | return Expression::make_cast(uintptr_type, call, loc); | |
9689 | } | |
9690 | ||
2c809f8f | 9691 | // Flatten a call with multiple results into a temporary. |
9692 | ||
9693 | Expression* | |
b8e86a51 | 9694 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
9695 | Statement_inserter* inserter) | |
2c809f8f | 9696 | { |
5bf8be8b | 9697 | if (this->is_erroneous_call()) |
9698 | { | |
9699 | go_assert(saw_errors()); | |
9700 | return Expression::make_error(this->location()); | |
9701 | } | |
b8e86a51 | 9702 | |
91c0fd76 | 9703 | if (this->is_flattened_) |
9704 | return this; | |
9705 | this->is_flattened_ = true; | |
9706 | ||
b8e86a51 | 9707 | // Add temporary variables for all arguments that require type |
9708 | // conversion. | |
9709 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 9710 | if (fntype == NULL) |
9711 | { | |
9712 | go_assert(saw_errors()); | |
9713 | return this; | |
9714 | } | |
b8e86a51 | 9715 | if (this->args_ != NULL && !this->args_->empty() |
9716 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
9717 | { | |
9718 | bool is_interface_method = | |
9719 | this->fn_->interface_field_reference_expression() != NULL; | |
9720 | ||
9721 | Expression_list *args = new Expression_list(); | |
9722 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
9723 | Expression_list::const_iterator pa = this->args_->begin(); | |
9724 | if (!is_interface_method && fntype->is_method()) | |
9725 | { | |
9726 | // The receiver argument. | |
9727 | args->push_back(*pa); | |
9728 | ++pa; | |
9729 | } | |
9730 | for (; pa != this->args_->end(); ++pa, ++pp) | |
9731 | { | |
9732 | go_assert(pp != fntype->parameters()->end()); | |
9733 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
9734 | args->push_back(*pa); | |
9735 | else | |
9736 | { | |
9737 | Location loc = (*pa)->location(); | |
8ba8cc87 | 9738 | Expression* arg = *pa; |
9739 | if (!arg->is_variable()) | |
9740 | { | |
9741 | Temporary_statement *temp = | |
9742 | Statement::make_temporary(NULL, arg, loc); | |
9743 | inserter->insert(temp); | |
9744 | arg = Expression::make_temporary_reference(temp, loc); | |
9745 | } | |
9746 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
9747 | loc); | |
9748 | args->push_back(arg); | |
b8e86a51 | 9749 | } |
9750 | } | |
9751 | delete this->args_; | |
9752 | this->args_ = args; | |
9753 | } | |
9754 | ||
2c809f8f | 9755 | return this; |
9756 | } | |
9757 | ||
ceeb4318 | 9758 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9759 | |
9760 | Function_type* | |
9761 | Call_expression::get_function_type() const | |
9762 | { | |
9763 | return this->fn_->type()->function_type(); | |
9764 | } | |
9765 | ||
9766 | // Return the number of values which this call will return. | |
9767 | ||
9768 | size_t | |
9769 | Call_expression::result_count() const | |
9770 | { | |
9771 | const Function_type* fntype = this->get_function_type(); | |
9772 | if (fntype == NULL) | |
9773 | return 0; | |
9774 | if (fntype->results() == NULL) | |
9775 | return 0; | |
9776 | return fntype->results()->size(); | |
9777 | } | |
9778 | ||
5731103c | 9779 | // Return the temporary that holds the result for a call with multiple |
9780 | // results. | |
ceeb4318 | 9781 | |
9782 | Temporary_statement* | |
5731103c | 9783 | Call_expression::results() const |
ceeb4318 | 9784 | { |
5731103c | 9785 | if (this->call_temp_ == NULL) |
cd238b8d | 9786 | { |
9787 | go_assert(saw_errors()); | |
9788 | return NULL; | |
9789 | } | |
5731103c | 9790 | return this->call_temp_; |
ceeb4318 | 9791 | } |
9792 | ||
1373401e | 9793 | // Set the number of results expected from a call expression. |
9794 | ||
9795 | void | |
9796 | Call_expression::set_expected_result_count(size_t count) | |
9797 | { | |
9798 | go_assert(this->expected_result_count_ == 0); | |
9799 | this->expected_result_count_ = count; | |
9800 | } | |
9801 | ||
e440a328 | 9802 | // Return whether this is a call to the predeclared function recover. |
9803 | ||
9804 | bool | |
9805 | Call_expression::is_recover_call() const | |
9806 | { | |
9807 | return this->do_is_recover_call(); | |
9808 | } | |
9809 | ||
9810 | // Set the argument to the recover function. | |
9811 | ||
9812 | void | |
9813 | Call_expression::set_recover_arg(Expression* arg) | |
9814 | { | |
9815 | this->do_set_recover_arg(arg); | |
9816 | } | |
9817 | ||
9818 | // Virtual functions also implemented by Builtin_call_expression. | |
9819 | ||
9820 | bool | |
9821 | Call_expression::do_is_recover_call() const | |
9822 | { | |
9823 | return false; | |
9824 | } | |
9825 | ||
9826 | void | |
9827 | Call_expression::do_set_recover_arg(Expression*) | |
9828 | { | |
c3e6f413 | 9829 | go_unreachable(); |
e440a328 | 9830 | } |
9831 | ||
ceeb4318 | 9832 | // We have found an error with this call expression; return true if |
9833 | // we should report it. | |
9834 | ||
9835 | bool | |
9836 | Call_expression::issue_error() | |
9837 | { | |
9838 | if (this->issued_error_) | |
9839 | return false; | |
9840 | else | |
9841 | { | |
9842 | this->issued_error_ = true; | |
9843 | return true; | |
9844 | } | |
9845 | } | |
9846 | ||
5bf8be8b | 9847 | // Whether or not this call contains errors, either in the call or the |
9848 | // arguments to the call. | |
9849 | ||
9850 | bool | |
9851 | Call_expression::is_erroneous_call() | |
9852 | { | |
9853 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9854 | return true; | |
9855 | ||
9856 | if (this->args() == NULL) | |
9857 | return false; | |
9858 | for (Expression_list::iterator pa = this->args()->begin(); | |
9859 | pa != this->args()->end(); | |
9860 | ++pa) | |
9861 | { | |
9862 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9863 | return true; | |
9864 | } | |
9865 | return false; | |
9866 | } | |
9867 | ||
e440a328 | 9868 | // Get the type. |
9869 | ||
9870 | Type* | |
9871 | Call_expression::do_type() | |
9872 | { | |
9873 | if (this->type_ != NULL) | |
9874 | return this->type_; | |
9875 | ||
9876 | Type* ret; | |
9877 | Function_type* fntype = this->get_function_type(); | |
9878 | if (fntype == NULL) | |
9879 | return Type::make_error_type(); | |
9880 | ||
9881 | const Typed_identifier_list* results = fntype->results(); | |
9882 | if (results == NULL) | |
9883 | ret = Type::make_void_type(); | |
9884 | else if (results->size() == 1) | |
9885 | ret = results->begin()->type(); | |
9886 | else | |
9887 | ret = Type::make_call_multiple_result_type(this); | |
9888 | ||
9889 | this->type_ = ret; | |
9890 | ||
9891 | return this->type_; | |
9892 | } | |
9893 | ||
9894 | // Determine types for a call expression. We can use the function | |
9895 | // parameter types to set the types of the arguments. | |
9896 | ||
9897 | void | |
9898 | Call_expression::do_determine_type(const Type_context*) | |
9899 | { | |
fb94b0ca | 9900 | if (!this->determining_types()) |
9901 | return; | |
9902 | ||
e440a328 | 9903 | this->fn_->determine_type_no_context(); |
9904 | Function_type* fntype = this->get_function_type(); | |
9905 | const Typed_identifier_list* parameters = NULL; | |
9906 | if (fntype != NULL) | |
9907 | parameters = fntype->parameters(); | |
9908 | if (this->args_ != NULL) | |
9909 | { | |
9910 | Typed_identifier_list::const_iterator pt; | |
9911 | if (parameters != NULL) | |
9912 | pt = parameters->begin(); | |
09ea332d | 9913 | bool first = true; |
e440a328 | 9914 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9915 | pa != this->args_->end(); | |
9916 | ++pa) | |
9917 | { | |
09ea332d | 9918 | if (first) |
9919 | { | |
9920 | first = false; | |
9921 | // If this is a method, the first argument is the | |
9922 | // receiver. | |
9923 | if (fntype != NULL && fntype->is_method()) | |
9924 | { | |
9925 | Type* rtype = fntype->receiver()->type(); | |
9926 | // The receiver is always passed as a pointer. | |
9927 | if (rtype->points_to() == NULL) | |
9928 | rtype = Type::make_pointer_type(rtype); | |
9929 | Type_context subcontext(rtype, false); | |
9930 | (*pa)->determine_type(&subcontext); | |
9931 | continue; | |
9932 | } | |
9933 | } | |
9934 | ||
e440a328 | 9935 | if (parameters != NULL && pt != parameters->end()) |
9936 | { | |
9937 | Type_context subcontext(pt->type(), false); | |
9938 | (*pa)->determine_type(&subcontext); | |
9939 | ++pt; | |
9940 | } | |
9941 | else | |
9942 | (*pa)->determine_type_no_context(); | |
9943 | } | |
9944 | } | |
9945 | } | |
9946 | ||
fb94b0ca | 9947 | // Called when determining types for a Call_expression. Return true |
9948 | // if we should go ahead, false if they have already been determined. | |
9949 | ||
9950 | bool | |
9951 | Call_expression::determining_types() | |
9952 | { | |
9953 | if (this->types_are_determined_) | |
9954 | return false; | |
9955 | else | |
9956 | { | |
9957 | this->types_are_determined_ = true; | |
9958 | return true; | |
9959 | } | |
9960 | } | |
9961 | ||
e440a328 | 9962 | // Check types for parameter I. |
9963 | ||
9964 | bool | |
9965 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9966 | const Type* argument_type, | |
b13c66cd | 9967 | Location argument_location, |
e440a328 | 9968 | bool issued_error) |
9969 | { | |
9970 | std::string reason; | |
1eae365b | 9971 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 9972 | { |
9973 | if (!issued_error) | |
9974 | { | |
9975 | if (reason.empty()) | |
631d5788 | 9976 | go_error_at(argument_location, "argument %d has incompatible type", i); |
e440a328 | 9977 | else |
631d5788 | 9978 | go_error_at(argument_location, |
9979 | "argument %d has incompatible type (%s)", | |
9980 | i, reason.c_str()); | |
e440a328 | 9981 | } |
9982 | this->set_is_error(); | |
9983 | return false; | |
9984 | } | |
9985 | return true; | |
9986 | } | |
9987 | ||
9988 | // Check types. | |
9989 | ||
9990 | void | |
9991 | Call_expression::do_check_types(Gogo*) | |
9992 | { | |
a6645f74 | 9993 | if (this->classification() == EXPRESSION_ERROR) |
9994 | return; | |
9995 | ||
e440a328 | 9996 | Function_type* fntype = this->get_function_type(); |
9997 | if (fntype == NULL) | |
9998 | { | |
5c13bd80 | 9999 | if (!this->fn_->type()->is_error()) |
e440a328 | 10000 | this->report_error(_("expected function")); |
10001 | return; | |
10002 | } | |
10003 | ||
1373401e | 10004 | if (this->expected_result_count_ != 0 |
10005 | && this->expected_result_count_ != this->result_count()) | |
10006 | { | |
10007 | if (this->issue_error()) | |
10008 | this->report_error(_("function result count mismatch")); | |
10009 | this->set_is_error(); | |
10010 | return; | |
10011 | } | |
10012 | ||
09ea332d | 10013 | bool is_method = fntype->is_method(); |
10014 | if (is_method) | |
e440a328 | 10015 | { |
09ea332d | 10016 | go_assert(this->args_ != NULL && !this->args_->empty()); |
10017 | Type* rtype = fntype->receiver()->type(); | |
10018 | Expression* first_arg = this->args_->front(); | |
1eae365b | 10019 | // We dereference the values since receivers are always passed |
10020 | // as pointers. | |
09ea332d | 10021 | std::string reason; |
1eae365b | 10022 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
10023 | &reason)) | |
e440a328 | 10024 | { |
09ea332d | 10025 | if (reason.empty()) |
10026 | this->report_error(_("incompatible type for receiver")); | |
10027 | else | |
e440a328 | 10028 | { |
631d5788 | 10029 | go_error_at(this->location(), |
10030 | "incompatible type for receiver (%s)", | |
10031 | reason.c_str()); | |
09ea332d | 10032 | this->set_is_error(); |
e440a328 | 10033 | } |
10034 | } | |
10035 | } | |
10036 | ||
10037 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 10038 | // we don't have to worry about it here unless something is wrong. |
10039 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
10040 | { | |
10041 | if (!fntype->is_varargs()) | |
10042 | { | |
631d5788 | 10043 | go_error_at(this->location(), |
10044 | _("invalid use of %<...%> calling non-variadic function")); | |
a6645f74 | 10045 | this->set_is_error(); |
10046 | return; | |
10047 | } | |
10048 | } | |
e440a328 | 10049 | |
10050 | const Typed_identifier_list* parameters = fntype->parameters(); | |
33d1d391 | 10051 | if (this->args_ == NULL || this->args_->size() == 0) |
e440a328 | 10052 | { |
10053 | if (parameters != NULL && !parameters->empty()) | |
10054 | this->report_error(_("not enough arguments")); | |
10055 | } | |
10056 | else if (parameters == NULL) | |
09ea332d | 10057 | { |
10058 | if (!is_method || this->args_->size() > 1) | |
10059 | this->report_error(_("too many arguments")); | |
10060 | } | |
1373401e | 10061 | else if (this->args_->size() == 1 |
10062 | && this->args_->front()->call_expression() != NULL | |
10063 | && this->args_->front()->call_expression()->result_count() > 1) | |
10064 | { | |
10065 | // This is F(G()) when G returns more than one result. If the | |
10066 | // results can be matched to parameters, it would have been | |
10067 | // lowered in do_lower. If we get here we know there is a | |
10068 | // mismatch. | |
10069 | if (this->args_->front()->call_expression()->result_count() | |
10070 | < parameters->size()) | |
10071 | this->report_error(_("not enough arguments")); | |
10072 | else | |
10073 | this->report_error(_("too many arguments")); | |
10074 | } | |
e440a328 | 10075 | else |
10076 | { | |
10077 | int i = 0; | |
09ea332d | 10078 | Expression_list::const_iterator pa = this->args_->begin(); |
10079 | if (is_method) | |
10080 | ++pa; | |
10081 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
10082 | pt != parameters->end(); | |
10083 | ++pt, ++pa, ++i) | |
e440a328 | 10084 | { |
09ea332d | 10085 | if (pa == this->args_->end()) |
e440a328 | 10086 | { |
09ea332d | 10087 | this->report_error(_("not enough arguments")); |
e440a328 | 10088 | return; |
10089 | } | |
10090 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
10091 | (*pa)->location(), false); | |
10092 | } | |
09ea332d | 10093 | if (pa != this->args_->end()) |
10094 | this->report_error(_("too many arguments")); | |
e440a328 | 10095 | } |
10096 | } | |
10097 | ||
72666aed | 10098 | Expression* |
10099 | Call_expression::do_copy() | |
10100 | { | |
10101 | Call_expression* call = | |
10102 | Expression::make_call(this->fn_->copy(), | |
10103 | (this->args_ == NULL | |
10104 | ? NULL | |
10105 | : this->args_->copy()), | |
10106 | this->is_varargs_, this->location()); | |
10107 | ||
10108 | if (this->varargs_are_lowered_) | |
10109 | call->set_varargs_are_lowered(); | |
10110 | return call; | |
10111 | } | |
10112 | ||
e440a328 | 10113 | // Return whether we have to use a temporary variable to ensure that |
10114 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 10115 | // results then it will inevitably be executed last. |
e440a328 | 10116 | |
10117 | bool | |
10118 | Call_expression::do_must_eval_in_order() const | |
10119 | { | |
ceeb4318 | 10120 | return this->result_count() > 0; |
e440a328 | 10121 | } |
10122 | ||
e440a328 | 10123 | // Get the function and the first argument to use when calling an |
10124 | // interface method. | |
10125 | ||
2387f644 | 10126 | Expression* |
e440a328 | 10127 | Call_expression::interface_method_function( |
e440a328 | 10128 | Interface_field_reference_expression* interface_method, |
db122cb9 | 10129 | Expression** first_arg_ptr, |
10130 | Location location) | |
e440a328 | 10131 | { |
db122cb9 | 10132 | Expression* object = interface_method->get_underlying_object(); |
10133 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); | |
10134 | *first_arg_ptr = | |
10135 | Expression::make_unsafe_cast(unsafe_ptr_type, object, location); | |
2387f644 | 10136 | return interface_method->get_function(); |
e440a328 | 10137 | } |
10138 | ||
10139 | // Build the call expression. | |
10140 | ||
ea664253 | 10141 | Bexpression* |
10142 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10143 | { |
5731103c | 10144 | Location location = this->location(); |
10145 | ||
2c809f8f | 10146 | if (this->call_ != NULL) |
5731103c | 10147 | { |
10148 | // If the call returns multiple results, make a new reference to | |
10149 | // the temporary. | |
10150 | if (this->call_temp_ != NULL) | |
10151 | { | |
10152 | Expression* ref = | |
10153 | Expression::make_temporary_reference(this->call_temp_, location); | |
10154 | return ref->get_backend(context); | |
10155 | } | |
10156 | ||
10157 | return this->call_; | |
10158 | } | |
e440a328 | 10159 | |
10160 | Function_type* fntype = this->get_function_type(); | |
10161 | if (fntype == NULL) | |
ea664253 | 10162 | return context->backend()->error_expression(); |
e440a328 | 10163 | |
10164 | if (this->fn_->is_error_expression()) | |
ea664253 | 10165 | return context->backend()->error_expression(); |
e440a328 | 10166 | |
10167 | Gogo* gogo = context->gogo(); | |
e440a328 | 10168 | |
10169 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 10170 | Interface_field_reference_expression* interface_method = |
10171 | this->fn_->interface_field_reference_expression(); | |
10172 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 10173 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 10174 | |
f8bdf81a | 10175 | bool has_closure_arg; |
8381eda7 | 10176 | if (has_closure) |
f8bdf81a | 10177 | has_closure_arg = true; |
8381eda7 | 10178 | else if (func != NULL) |
f8bdf81a | 10179 | has_closure_arg = false; |
8381eda7 | 10180 | else if (is_interface_method) |
f8bdf81a | 10181 | has_closure_arg = false; |
8381eda7 | 10182 | else |
f8bdf81a | 10183 | has_closure_arg = true; |
8381eda7 | 10184 | |
e440a328 | 10185 | int nargs; |
2c809f8f | 10186 | std::vector<Bexpression*> fn_args; |
e440a328 | 10187 | if (this->args_ == NULL || this->args_->empty()) |
10188 | { | |
f8bdf81a | 10189 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 10190 | if (nargs > 0) |
10191 | fn_args.resize(1); | |
e440a328 | 10192 | } |
09ea332d | 10193 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
10194 | { | |
10195 | // Passing a receiver parameter. | |
10196 | go_assert(!is_interface_method | |
10197 | && fntype->is_method() | |
10198 | && this->args_->size() == 1); | |
f8bdf81a | 10199 | nargs = 1; |
2c809f8f | 10200 | fn_args.resize(1); |
ea664253 | 10201 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 10202 | } |
e440a328 | 10203 | else |
10204 | { | |
10205 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 10206 | |
10207 | nargs = this->args_->size(); | |
09ea332d | 10208 | int i = is_interface_method ? 1 : 0; |
e440a328 | 10209 | nargs += i; |
2c809f8f | 10210 | fn_args.resize(nargs); |
e440a328 | 10211 | |
10212 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 10213 | Expression_list::const_iterator pe = this->args_->begin(); |
10214 | if (!is_interface_method && fntype->is_method()) | |
10215 | { | |
ea664253 | 10216 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 10217 | ++pe; |
10218 | ++i; | |
10219 | } | |
10220 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 10221 | { |
c484d925 | 10222 | go_assert(pp != params->end()); |
2c809f8f | 10223 | Expression* arg = |
10224 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
10225 | location); | |
ea664253 | 10226 | fn_args[i] = arg->get_backend(context); |
e440a328 | 10227 | } |
c484d925 | 10228 | go_assert(pp == params->end()); |
f8bdf81a | 10229 | go_assert(i == nargs); |
e440a328 | 10230 | } |
10231 | ||
2c809f8f | 10232 | Expression* fn; |
10233 | Expression* closure = NULL; | |
8381eda7 | 10234 | if (func != NULL) |
10235 | { | |
10236 | Named_object* no = func->named_object(); | |
2c809f8f | 10237 | fn = Expression::make_func_code_reference(no, location); |
10238 | if (has_closure) | |
10239 | closure = func->closure(); | |
8381eda7 | 10240 | } |
09ea332d | 10241 | else if (!is_interface_method) |
8381eda7 | 10242 | { |
2c809f8f | 10243 | closure = this->fn_; |
10244 | ||
10245 | // The backend representation of this function type is a pointer | |
10246 | // to a struct whose first field is the actual function to call. | |
10247 | Type* pfntype = | |
10248 | Type::make_pointer_type( | |
10249 | Type::make_pointer_type(Type::make_void_type())); | |
10250 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
f614ea8b | 10251 | fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location); |
2c809f8f | 10252 | } |
e440a328 | 10253 | else |
cf609de4 | 10254 | { |
2387f644 | 10255 | Expression* first_arg; |
db122cb9 | 10256 | fn = this->interface_method_function(interface_method, &first_arg, |
10257 | location); | |
ea664253 | 10258 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 10259 | } |
10260 | ||
1ecc6157 | 10261 | Bexpression* bclosure = NULL; |
10262 | if (has_closure_arg) | |
10263 | bclosure = closure->get_backend(context); | |
f8bdf81a | 10264 | else |
1ecc6157 | 10265 | go_assert(closure == NULL); |
f8bdf81a | 10266 | |
ea664253 | 10267 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 10268 | |
10269 | // When not calling a named function directly, use a type conversion | |
10270 | // in case the type of the function is a recursive type which refers | |
10271 | // to itself. We don't do this for an interface method because 1) | |
10272 | // an interface method never refers to itself, so we always have a | |
10273 | // function type here; 2) we pass an extra first argument to an | |
10274 | // interface method, so fntype is not correct. | |
10275 | if (func == NULL && !is_interface_method) | |
10276 | { | |
10277 | Btype* bft = fntype->get_backend_fntype(gogo); | |
10278 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
10279 | } | |
10280 | ||
4ced7af9 | 10281 | Bfunction* bfunction = NULL; |
10282 | if (context->function()) | |
10283 | bfunction = context->function()->func_value()->get_decl(); | |
10284 | Bexpression* call = gogo->backend()->call_expression(bfunction, bfn, | |
10285 | fn_args, bclosure, | |
10286 | location); | |
e440a328 | 10287 | |
5731103c | 10288 | if (this->call_temp_ != NULL) |
e440a328 | 10289 | { |
5731103c | 10290 | // This case occurs when the call returns multiple results. |
e440a328 | 10291 | |
5731103c | 10292 | Expression* ref = Expression::make_temporary_reference(this->call_temp_, |
10293 | location); | |
10294 | Bexpression* bref = ref->get_backend(context); | |
10295 | Bstatement* bassn = gogo->backend()->assignment_statement(bfunction, | |
10296 | bref, call, | |
10297 | location); | |
e440a328 | 10298 | |
5731103c | 10299 | ref = Expression::make_temporary_reference(this->call_temp_, location); |
10300 | this->call_ = ref->get_backend(context); | |
10301 | ||
10302 | return gogo->backend()->compound_expression(bassn, this->call_, | |
10303 | location); | |
2c809f8f | 10304 | } |
e440a328 | 10305 | |
2c809f8f | 10306 | this->call_ = call; |
ea664253 | 10307 | return this->call_; |
e440a328 | 10308 | } |
10309 | ||
d751bb78 | 10310 | // Dump ast representation for a call expressin. |
10311 | ||
10312 | void | |
10313 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
10314 | { | |
10315 | this->fn_->dump_expression(ast_dump_context); | |
10316 | ast_dump_context->ostream() << "("; | |
10317 | if (args_ != NULL) | |
10318 | ast_dump_context->dump_expression_list(this->args_); | |
10319 | ||
10320 | ast_dump_context->ostream() << ") "; | |
10321 | } | |
10322 | ||
e440a328 | 10323 | // Make a call expression. |
10324 | ||
10325 | Call_expression* | |
10326 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 10327 | Location location) |
e440a328 | 10328 | { |
10329 | return new Call_expression(fn, args, is_varargs, location); | |
10330 | } | |
10331 | ||
da244e59 | 10332 | // Class Call_result_expression. |
e440a328 | 10333 | |
10334 | // Traverse a call result. | |
10335 | ||
10336 | int | |
10337 | Call_result_expression::do_traverse(Traverse* traverse) | |
10338 | { | |
10339 | if (traverse->remember_expression(this->call_)) | |
10340 | { | |
10341 | // We have already traversed the call expression. | |
10342 | return TRAVERSE_CONTINUE; | |
10343 | } | |
10344 | return Expression::traverse(&this->call_, traverse); | |
10345 | } | |
10346 | ||
10347 | // Get the type. | |
10348 | ||
10349 | Type* | |
10350 | Call_result_expression::do_type() | |
10351 | { | |
425dd051 | 10352 | if (this->classification() == EXPRESSION_ERROR) |
10353 | return Type::make_error_type(); | |
10354 | ||
e440a328 | 10355 | // THIS->CALL_ can be replaced with a temporary reference due to |
10356 | // Call_expression::do_must_eval_in_order when there is an error. | |
10357 | Call_expression* ce = this->call_->call_expression(); | |
10358 | if (ce == NULL) | |
5e85f268 | 10359 | { |
10360 | this->set_is_error(); | |
10361 | return Type::make_error_type(); | |
10362 | } | |
e440a328 | 10363 | Function_type* fntype = ce->get_function_type(); |
10364 | if (fntype == NULL) | |
5e85f268 | 10365 | { |
e37658e2 | 10366 | if (ce->issue_error()) |
99b3f06f | 10367 | { |
10368 | if (!ce->fn()->type()->is_error()) | |
10369 | this->report_error(_("expected function")); | |
10370 | } | |
5e85f268 | 10371 | this->set_is_error(); |
10372 | return Type::make_error_type(); | |
10373 | } | |
e440a328 | 10374 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10375 | if (results == NULL || results->size() < 2) |
7b8d861f | 10376 | { |
ceeb4318 | 10377 | if (ce->issue_error()) |
10378 | this->report_error(_("number of results does not match " | |
10379 | "number of values")); | |
7b8d861f | 10380 | return Type::make_error_type(); |
10381 | } | |
e440a328 | 10382 | Typed_identifier_list::const_iterator pr = results->begin(); |
10383 | for (unsigned int i = 0; i < this->index_; ++i) | |
10384 | { | |
10385 | if (pr == results->end()) | |
425dd051 | 10386 | break; |
e440a328 | 10387 | ++pr; |
10388 | } | |
10389 | if (pr == results->end()) | |
425dd051 | 10390 | { |
ceeb4318 | 10391 | if (ce->issue_error()) |
10392 | this->report_error(_("number of results does not match " | |
10393 | "number of values")); | |
425dd051 | 10394 | return Type::make_error_type(); |
10395 | } | |
e440a328 | 10396 | return pr->type(); |
10397 | } | |
10398 | ||
425dd051 | 10399 | // Check the type. Just make sure that we trigger the warning in |
10400 | // do_type. | |
e440a328 | 10401 | |
10402 | void | |
10403 | Call_result_expression::do_check_types(Gogo*) | |
10404 | { | |
425dd051 | 10405 | this->type(); |
e440a328 | 10406 | } |
10407 | ||
10408 | // Determine the type. We have nothing to do here, but the 0 result | |
10409 | // needs to pass down to the caller. | |
10410 | ||
10411 | void | |
10412 | Call_result_expression::do_determine_type(const Type_context*) | |
10413 | { | |
fb94b0ca | 10414 | this->call_->determine_type_no_context(); |
e440a328 | 10415 | } |
10416 | ||
ea664253 | 10417 | // Return the backend representation. We just refer to the temporary set by the |
10418 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 10419 | // hard to evaluate the call at the right time. |
e440a328 | 10420 | |
ea664253 | 10421 | Bexpression* |
10422 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10423 | { |
ceeb4318 | 10424 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10425 | if (ce == NULL) |
10426 | { | |
10427 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 10428 | return context->backend()->error_expression(); |
cd238b8d | 10429 | } |
5731103c | 10430 | Temporary_statement* ts = ce->results(); |
cd238b8d | 10431 | if (ts == NULL) |
10432 | { | |
10433 | go_assert(saw_errors()); | |
ea664253 | 10434 | return context->backend()->error_expression(); |
cd238b8d | 10435 | } |
ceeb4318 | 10436 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
5731103c | 10437 | ref = Expression::make_field_reference(ref, this->index_, this->location()); |
ea664253 | 10438 | return ref->get_backend(context); |
e440a328 | 10439 | } |
10440 | ||
d751bb78 | 10441 | // Dump ast representation for a call result expression. |
10442 | ||
10443 | void | |
10444 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10445 | const | |
10446 | { | |
10447 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10448 | // (struct) and the fields are referenced instead. | |
10449 | ast_dump_context->ostream() << this->index_ << "@("; | |
10450 | ast_dump_context->dump_expression(this->call_); | |
10451 | ast_dump_context->ostream() << ")"; | |
10452 | } | |
10453 | ||
e440a328 | 10454 | // Make a reference to a single result of a call which returns |
10455 | // multiple results. | |
10456 | ||
10457 | Expression* | |
10458 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10459 | { | |
10460 | return new Call_result_expression(call, index); | |
10461 | } | |
10462 | ||
10463 | // Class Index_expression. | |
10464 | ||
10465 | // Traversal. | |
10466 | ||
10467 | int | |
10468 | Index_expression::do_traverse(Traverse* traverse) | |
10469 | { | |
10470 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10471 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10472 | || (this->end_ != NULL | |
acf2b673 | 10473 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
10474 | || (this->cap_ != NULL | |
10475 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 10476 | return TRAVERSE_EXIT; |
10477 | return TRAVERSE_CONTINUE; | |
10478 | } | |
10479 | ||
10480 | // Lower an index expression. This converts the generic index | |
10481 | // expression into an array index, a string index, or a map index. | |
10482 | ||
10483 | Expression* | |
ceeb4318 | 10484 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10485 | { |
b13c66cd | 10486 | Location location = this->location(); |
e440a328 | 10487 | Expression* left = this->left_; |
10488 | Expression* start = this->start_; | |
10489 | Expression* end = this->end_; | |
acf2b673 | 10490 | Expression* cap = this->cap_; |
e440a328 | 10491 | |
10492 | Type* type = left->type(); | |
5c13bd80 | 10493 | if (type->is_error()) |
d9f3743a | 10494 | { |
10495 | go_assert(saw_errors()); | |
10496 | return Expression::make_error(location); | |
10497 | } | |
b0cf7ddd | 10498 | else if (left->is_type_expression()) |
10499 | { | |
631d5788 | 10500 | go_error_at(location, "attempt to index type expression"); |
b0cf7ddd | 10501 | return Expression::make_error(location); |
10502 | } | |
e440a328 | 10503 | else if (type->array_type() != NULL) |
acf2b673 | 10504 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 10505 | else if (type->points_to() != NULL |
10506 | && type->points_to()->array_type() != NULL | |
411eb89e | 10507 | && !type->points_to()->is_slice_type()) |
e440a328 | 10508 | { |
f614ea8b | 10509 | Expression* deref = |
10510 | Expression::make_dereference(left, NIL_CHECK_DEFAULT, location); | |
38092374 | 10511 | |
10512 | // For an ordinary index into the array, the pointer will be | |
10513 | // dereferenced. For a slice it will not--the resulting slice | |
10514 | // will simply reuse the pointer, which is incorrect if that | |
10515 | // pointer is nil. | |
10516 | if (end != NULL || cap != NULL) | |
10517 | deref->issue_nil_check(); | |
10518 | ||
acf2b673 | 10519 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 10520 | } |
10521 | else if (type->is_string_type()) | |
acf2b673 | 10522 | { |
10523 | if (cap != NULL) | |
10524 | { | |
631d5788 | 10525 | go_error_at(location, "invalid 3-index slice of string"); |
acf2b673 | 10526 | return Expression::make_error(location); |
10527 | } | |
10528 | return Expression::make_string_index(left, start, end, location); | |
10529 | } | |
e440a328 | 10530 | else if (type->map_type() != NULL) |
10531 | { | |
acf2b673 | 10532 | if (end != NULL || cap != NULL) |
e440a328 | 10533 | { |
631d5788 | 10534 | go_error_at(location, "invalid slice of map"); |
e440a328 | 10535 | return Expression::make_error(location); |
10536 | } | |
0d5530d9 | 10537 | return Expression::make_map_index(left, start, location); |
e440a328 | 10538 | } |
b1aba207 | 10539 | else if (cap != NULL) |
10540 | { | |
10541 | go_error_at(location, | |
10542 | "invalid 3-index slice of object that is not a slice"); | |
10543 | return Expression::make_error(location); | |
10544 | } | |
10545 | else if (end != NULL) | |
10546 | { | |
10547 | go_error_at(location, | |
10548 | ("attempt to slice object that is not " | |
10549 | "array, slice, or string")); | |
10550 | return Expression::make_error(location); | |
10551 | } | |
e440a328 | 10552 | else |
10553 | { | |
631d5788 | 10554 | go_error_at(location, |
b1aba207 | 10555 | ("attempt to index object that is not " |
10556 | "array, slice, string, or map")); | |
e440a328 | 10557 | return Expression::make_error(location); |
10558 | } | |
10559 | } | |
10560 | ||
acf2b673 | 10561 | // Write an indexed expression |
10562 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 10563 | |
10564 | void | |
10565 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10566 | const Expression* expr, | |
10567 | const Expression* start, | |
acf2b673 | 10568 | const Expression* end, |
10569 | const Expression* cap) | |
d751bb78 | 10570 | { |
10571 | expr->dump_expression(ast_dump_context); | |
10572 | ast_dump_context->ostream() << "["; | |
10573 | start->dump_expression(ast_dump_context); | |
10574 | if (end != NULL) | |
10575 | { | |
10576 | ast_dump_context->ostream() << ":"; | |
10577 | end->dump_expression(ast_dump_context); | |
10578 | } | |
acf2b673 | 10579 | if (cap != NULL) |
10580 | { | |
10581 | ast_dump_context->ostream() << ":"; | |
10582 | cap->dump_expression(ast_dump_context); | |
10583 | } | |
d751bb78 | 10584 | ast_dump_context->ostream() << "]"; |
10585 | } | |
10586 | ||
10587 | // Dump ast representation for an index expression. | |
10588 | ||
10589 | void | |
10590 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10591 | const | |
10592 | { | |
10593 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 10594 | this->start_, this->end_, this->cap_); |
d751bb78 | 10595 | } |
10596 | ||
e440a328 | 10597 | // Make an index expression. |
10598 | ||
10599 | Expression* | |
10600 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 10601 | Expression* cap, Location location) |
e440a328 | 10602 | { |
acf2b673 | 10603 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 10604 | } |
10605 | ||
da244e59 | 10606 | // Class Array_index_expression. |
e440a328 | 10607 | |
10608 | // Array index traversal. | |
10609 | ||
10610 | int | |
10611 | Array_index_expression::do_traverse(Traverse* traverse) | |
10612 | { | |
10613 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10614 | return TRAVERSE_EXIT; | |
10615 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10616 | return TRAVERSE_EXIT; | |
10617 | if (this->end_ != NULL) | |
10618 | { | |
10619 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10620 | return TRAVERSE_EXIT; | |
10621 | } | |
acf2b673 | 10622 | if (this->cap_ != NULL) |
10623 | { | |
10624 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10625 | return TRAVERSE_EXIT; | |
10626 | } | |
e440a328 | 10627 | return TRAVERSE_CONTINUE; |
10628 | } | |
10629 | ||
10630 | // Return the type of an array index. | |
10631 | ||
10632 | Type* | |
10633 | Array_index_expression::do_type() | |
10634 | { | |
10635 | if (this->type_ == NULL) | |
10636 | { | |
10637 | Array_type* type = this->array_->type()->array_type(); | |
10638 | if (type == NULL) | |
10639 | this->type_ = Type::make_error_type(); | |
10640 | else if (this->end_ == NULL) | |
10641 | this->type_ = type->element_type(); | |
411eb89e | 10642 | else if (type->is_slice_type()) |
e440a328 | 10643 | { |
10644 | // A slice of a slice has the same type as the original | |
10645 | // slice. | |
10646 | this->type_ = this->array_->type()->deref(); | |
10647 | } | |
10648 | else | |
10649 | { | |
10650 | // A slice of an array is a slice. | |
10651 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10652 | } | |
10653 | } | |
10654 | return this->type_; | |
10655 | } | |
10656 | ||
10657 | // Set the type of an array index. | |
10658 | ||
10659 | void | |
10660 | Array_index_expression::do_determine_type(const Type_context*) | |
10661 | { | |
10662 | this->array_->determine_type_no_context(); | |
f77aa642 | 10663 | |
10664 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10665 | if (this->start_->is_constant()) | |
10666 | this->start_->determine_type(&index_context); | |
10667 | else | |
10668 | this->start_->determine_type_no_context(); | |
e440a328 | 10669 | if (this->end_ != NULL) |
f77aa642 | 10670 | { |
10671 | if (this->end_->is_constant()) | |
10672 | this->end_->determine_type(&index_context); | |
10673 | else | |
10674 | this->end_->determine_type_no_context(); | |
10675 | } | |
acf2b673 | 10676 | if (this->cap_ != NULL) |
f77aa642 | 10677 | { |
10678 | if (this->cap_->is_constant()) | |
10679 | this->cap_->determine_type(&index_context); | |
10680 | else | |
10681 | this->cap_->determine_type_no_context(); | |
10682 | } | |
e440a328 | 10683 | } |
10684 | ||
10685 | // Check types of an array index. | |
10686 | ||
10687 | void | |
b7327dbf | 10688 | Array_index_expression::do_check_types(Gogo*) |
e440a328 | 10689 | { |
f6bc81e6 | 10690 | Numeric_constant nc; |
10691 | unsigned long v; | |
10692 | if (this->start_->type()->integer_type() == NULL | |
10693 | && !this->start_->type()->is_error() | |
10694 | && (!this->start_->numeric_constant_value(&nc) | |
10695 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10696 | this->report_error(_("index must be integer")); |
10697 | if (this->end_ != NULL | |
10698 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10699 | && !this->end_->type()->is_error() |
10700 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10701 | && !this->end_->is_error_expression() |
10702 | && (!this->end_->numeric_constant_value(&nc) | |
10703 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10704 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10705 | if (this->cap_ != NULL |
10706 | && this->cap_->type()->integer_type() == NULL | |
10707 | && !this->cap_->type()->is_error() | |
10708 | && !this->cap_->is_nil_expression() | |
10709 | && !this->cap_->is_error_expression() | |
10710 | && (!this->cap_->numeric_constant_value(&nc) | |
10711 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10712 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10713 | |
10714 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10715 | if (array_type == NULL) |
10716 | { | |
c484d925 | 10717 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10718 | return; |
10719 | } | |
e440a328 | 10720 | |
10721 | unsigned int int_bits = | |
10722 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10723 | ||
0c77715b | 10724 | Numeric_constant lvalnc; |
e440a328 | 10725 | mpz_t lval; |
e440a328 | 10726 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10727 | && array_type->length()->numeric_constant_value(&lvalnc) |
10728 | && lvalnc.to_int(&lval)); | |
10729 | Numeric_constant inc; | |
e440a328 | 10730 | mpz_t ival; |
0bd5d859 | 10731 | bool ival_valid = false; |
0c77715b | 10732 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10733 | { |
0bd5d859 | 10734 | ival_valid = true; |
e440a328 | 10735 | if (mpz_sgn(ival) < 0 |
10736 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10737 | || (lval_valid | |
10738 | && (this->end_ == NULL | |
10739 | ? mpz_cmp(ival, lval) >= 0 | |
10740 | : mpz_cmp(ival, lval) > 0))) | |
10741 | { | |
631d5788 | 10742 | go_error_at(this->start_->location(), "array index out of bounds"); |
e440a328 | 10743 | this->set_is_error(); |
10744 | } | |
10745 | } | |
10746 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10747 | { | |
0c77715b | 10748 | Numeric_constant enc; |
10749 | mpz_t eval; | |
acf2b673 | 10750 | bool eval_valid = false; |
0c77715b | 10751 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10752 | { |
acf2b673 | 10753 | eval_valid = true; |
0c77715b | 10754 | if (mpz_sgn(eval) < 0 |
10755 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10756 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10757 | { |
631d5788 | 10758 | go_error_at(this->end_->location(), "array index out of bounds"); |
e440a328 | 10759 | this->set_is_error(); |
10760 | } | |
0bd5d859 | 10761 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10762 | this->report_error(_("inverted slice range")); | |
e440a328 | 10763 | } |
acf2b673 | 10764 | |
10765 | Numeric_constant cnc; | |
10766 | mpz_t cval; | |
10767 | if (this->cap_ != NULL | |
10768 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10769 | { | |
10770 | if (mpz_sgn(cval) < 0 | |
10771 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10772 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10773 | { | |
631d5788 | 10774 | go_error_at(this->cap_->location(), "array index out of bounds"); |
acf2b673 | 10775 | this->set_is_error(); |
10776 | } | |
10777 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10778 | { | |
631d5788 | 10779 | go_error_at(this->cap_->location(), |
10780 | "invalid slice index: capacity less than start"); | |
acf2b673 | 10781 | this->set_is_error(); |
10782 | } | |
10783 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10784 | { | |
631d5788 | 10785 | go_error_at(this->cap_->location(), |
10786 | "invalid slice index: capacity less than length"); | |
acf2b673 | 10787 | this->set_is_error(); |
10788 | } | |
10789 | mpz_clear(cval); | |
10790 | } | |
10791 | ||
10792 | if (eval_valid) | |
10793 | mpz_clear(eval); | |
e440a328 | 10794 | } |
0bd5d859 | 10795 | if (ival_valid) |
10796 | mpz_clear(ival); | |
0c77715b | 10797 | if (lval_valid) |
10798 | mpz_clear(lval); | |
e440a328 | 10799 | |
10800 | // A slice of an array requires an addressable array. A slice of a | |
10801 | // slice is always possible. | |
411eb89e | 10802 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10803 | { |
10804 | if (!this->array_->is_addressable()) | |
8da39c3b | 10805 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10806 | else |
b7327dbf | 10807 | // Set the array address taken but not escape. The escape |
10808 | // analysis will make it escape to heap when needed. | |
10809 | this->array_->address_taken(false); | |
88ec30c8 | 10810 | } |
e440a328 | 10811 | } |
10812 | ||
2c809f8f | 10813 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10814 | |
10815 | Expression* | |
10816 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10817 | Statement_inserter* inserter) | |
10818 | { | |
10819 | Location loc = this->location(); | |
5bf8be8b | 10820 | Expression* array = this->array_; |
10821 | Expression* start = this->start_; | |
10822 | Expression* end = this->end_; | |
10823 | Expression* cap = this->cap_; | |
10824 | if (array->is_error_expression() | |
10825 | || array->type()->is_error_type() | |
10826 | || start->is_error_expression() | |
10827 | || start->type()->is_error_type() | |
10828 | || (end != NULL | |
10829 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10830 | || (cap != NULL | |
10831 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10832 | { | |
10833 | go_assert(saw_errors()); | |
10834 | return Expression::make_error(loc); | |
10835 | } | |
10836 | ||
2c809f8f | 10837 | Temporary_statement* temp; |
5bf8be8b | 10838 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10839 | { |
5bf8be8b | 10840 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10841 | inserter->insert(temp); |
10842 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10843 | } | |
5bf8be8b | 10844 | if (!start->is_variable()) |
2c809f8f | 10845 | { |
5bf8be8b | 10846 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10847 | inserter->insert(temp); |
10848 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10849 | } | |
5bf8be8b | 10850 | if (end != NULL |
10851 | && !end->is_nil_expression() | |
10852 | && !end->is_variable()) | |
2c809f8f | 10853 | { |
5bf8be8b | 10854 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10855 | inserter->insert(temp); |
10856 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10857 | } | |
03118c21 | 10858 | if (cap != NULL && !cap->is_variable()) |
2c809f8f | 10859 | { |
5bf8be8b | 10860 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10861 | inserter->insert(temp); |
10862 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10863 | } | |
10864 | ||
35a54f17 | 10865 | return this; |
10866 | } | |
10867 | ||
e440a328 | 10868 | // Return whether this expression is addressable. |
10869 | ||
10870 | bool | |
10871 | Array_index_expression::do_is_addressable() const | |
10872 | { | |
10873 | // A slice expression is not addressable. | |
10874 | if (this->end_ != NULL) | |
10875 | return false; | |
10876 | ||
10877 | // An index into a slice is addressable. | |
411eb89e | 10878 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10879 | return true; |
10880 | ||
10881 | // An index into an array is addressable if the array is | |
10882 | // addressable. | |
10883 | return this->array_->is_addressable(); | |
10884 | } | |
10885 | ||
bf1323be | 10886 | void |
10887 | Array_index_expression::do_address_taken(bool escapes) | |
10888 | { | |
10889 | // In &x[0], if x is a slice, then x's address is not taken. | |
10890 | if (!this->array_->type()->is_slice_type()) | |
10891 | this->array_->address_taken(escapes); | |
10892 | } | |
10893 | ||
ea664253 | 10894 | // Get the backend representation for an array index. |
e440a328 | 10895 | |
ea664253 | 10896 | Bexpression* |
10897 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10898 | { |
e440a328 | 10899 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10900 | if (array_type == NULL) |
10901 | { | |
c484d925 | 10902 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10903 | return context->backend()->error_expression(); |
d8cd8e2d | 10904 | } |
35a54f17 | 10905 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10906 | |
2c809f8f | 10907 | Location loc = this->location(); |
10908 | Gogo* gogo = context->gogo(); | |
10909 | ||
6dfedc16 | 10910 | Type* int_type = Type::lookup_integer_type("int"); |
10911 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10912 | |
2c809f8f | 10913 | // We need to convert the length and capacity to the Go "int" type here |
10914 | // because the length of a fixed-length array could be of type "uintptr" | |
10915 | // and gimple disallows binary operations between "uintptr" and other | |
10916 | // integer types. FIXME. | |
10917 | Bexpression* length = NULL; | |
a04bfdfc | 10918 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10919 | { | |
35a54f17 | 10920 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10921 | length = len->get_backend(context); |
2c809f8f | 10922 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10923 | } |
10924 | ||
2c809f8f | 10925 | Bexpression* capacity = NULL; |
a04bfdfc | 10926 | if (this->end_ != NULL) |
10927 | { | |
35a54f17 | 10928 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10929 | capacity = cap->get_backend(context); |
2c809f8f | 10930 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10931 | } |
10932 | ||
2c809f8f | 10933 | Bexpression* cap_arg = capacity; |
acf2b673 | 10934 | if (this->cap_ != NULL) |
10935 | { | |
ea664253 | 10936 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10937 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10938 | } |
10939 | ||
2c809f8f | 10940 | if (length == NULL) |
10941 | length = cap_arg; | |
e440a328 | 10942 | |
10943 | int code = (array_type->length() != NULL | |
10944 | ? (this->end_ == NULL | |
10945 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10946 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10947 | : (this->end_ == NULL | |
10948 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10949 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10950 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10951 | |
6dfedc16 | 10952 | if (this->start_->type()->integer_type() == NULL |
10953 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10954 | { | |
10955 | go_assert(saw_errors()); | |
10956 | return context->backend()->error_expression(); | |
10957 | } | |
d9f3743a | 10958 | |
ea664253 | 10959 | Bexpression* bad_index = |
d9f3743a | 10960 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 10961 | |
ea664253 | 10962 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10963 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10964 | Bexpression* start_too_large = | |
10965 | gogo->backend()->binary_expression((this->end_ == NULL | |
10966 | ? OPERATOR_GE | |
10967 | : OPERATOR_GT), | |
10968 | start, | |
10969 | (this->end_ == NULL | |
10970 | ? length | |
10971 | : capacity), | |
10972 | loc); | |
10973 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10974 | bad_index, loc); | |
e440a328 | 10975 | |
93715b75 | 10976 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 10977 | if (this->end_ == NULL) |
10978 | { | |
10979 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10980 | // wrap the index check into START. |
10981 | start = | |
93715b75 | 10982 | gogo->backend()->conditional_expression(bfn, int_btype, bad_index, |
2c809f8f | 10983 | crash, start, loc); |
e440a328 | 10984 | |
2c809f8f | 10985 | Bexpression* ret; |
e440a328 | 10986 | if (array_type->length() != NULL) |
10987 | { | |
ea664253 | 10988 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10989 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10990 | } |
10991 | else | |
10992 | { | |
2c809f8f | 10993 | // Slice. |
10994 | Expression* valptr = | |
44dbe1d7 | 10995 | array_type->get_value_pointer(gogo, this->array_, |
10996 | this->is_lvalue_); | |
ea664253 | 10997 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 10998 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 10999 | |
11000 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
11001 | Btype* ele_btype = ele_type->get_backend(gogo); | |
11002 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 11003 | } |
ea664253 | 11004 | return ret; |
e440a328 | 11005 | } |
11006 | ||
11007 | // Array slice. | |
11008 | ||
acf2b673 | 11009 | if (this->cap_ != NULL) |
11010 | { | |
2c809f8f | 11011 | Bexpression* bounds_bcheck = |
ea664253 | 11012 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 11013 | bad_index = |
11014 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11015 | bad_index, loc); | |
11016 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
11017 | ||
11018 | Bexpression* cap_too_small = | |
11019 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
11020 | Bexpression* cap_too_large = | |
11021 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
11022 | Bexpression* bad_cap = | |
11023 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
11024 | cap_too_large, loc); | |
11025 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
11026 | bad_index, loc); | |
11027 | } | |
11028 | ||
11029 | Bexpression* end; | |
e440a328 | 11030 | if (this->end_->is_nil_expression()) |
2c809f8f | 11031 | end = length; |
e440a328 | 11032 | else |
11033 | { | |
2c809f8f | 11034 | Bexpression* bounds_bcheck = |
ea664253 | 11035 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 11036 | |
2c809f8f | 11037 | bad_index = |
11038 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11039 | bad_index, loc); | |
e440a328 | 11040 | |
ea664253 | 11041 | end = this->end_->get_backend(context); |
2c809f8f | 11042 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
11043 | Bexpression* end_too_small = | |
11044 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
11045 | Bexpression* end_too_large = | |
11046 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
11047 | Bexpression* bad_end = | |
11048 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
11049 | end_too_large, loc); | |
11050 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
11051 | bad_index, loc); | |
e440a328 | 11052 | } |
11053 | ||
2c809f8f | 11054 | Bexpression* result_length = |
11055 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 11056 | |
2c809f8f | 11057 | Bexpression* result_capacity = |
11058 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 11059 | |
03118c21 | 11060 | // If the new capacity is zero, don't change val. Otherwise we can |
11061 | // get a pointer to the next object in memory, keeping it live | |
11062 | // unnecessarily. When the capacity is zero, the actual pointer | |
11063 | // value doesn't matter. | |
11064 | Bexpression* zero = | |
11065 | Expression::make_integer_ul(0, int_type, loc)->get_backend(context); | |
11066 | Bexpression* cond = | |
11067 | gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero, | |
11068 | loc); | |
11069 | Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype, | |
11070 | cond, zero, | |
11071 | start, loc); | |
44dbe1d7 | 11072 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_, |
11073 | this->is_lvalue_); | |
03118c21 | 11074 | Bexpression* val = valptr->get_backend(context); |
11075 | val = gogo->backend()->pointer_offset_expression(val, offset, loc); | |
11076 | ||
2c809f8f | 11077 | Btype* struct_btype = this->type()->get_backend(gogo); |
11078 | std::vector<Bexpression*> init; | |
11079 | init.push_back(val); | |
11080 | init.push_back(result_length); | |
11081 | init.push_back(result_capacity); | |
e440a328 | 11082 | |
2c809f8f | 11083 | Bexpression* ctor = |
11084 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
93715b75 | 11085 | return gogo->backend()->conditional_expression(bfn, struct_btype, bad_index, |
ea664253 | 11086 | crash, ctor, loc); |
e440a328 | 11087 | } |
11088 | ||
d751bb78 | 11089 | // Dump ast representation for an array index expression. |
11090 | ||
11091 | void | |
11092 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11093 | const | |
11094 | { | |
11095 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 11096 | this->start_, this->end_, this->cap_); |
d751bb78 | 11097 | } |
11098 | ||
acf2b673 | 11099 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 11100 | |
11101 | Expression* | |
11102 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 11103 | Expression* end, Expression* cap, |
11104 | Location location) | |
e440a328 | 11105 | { |
acf2b673 | 11106 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 11107 | } |
11108 | ||
50075d74 | 11109 | // Class String_index_expression. |
e440a328 | 11110 | |
11111 | // String index traversal. | |
11112 | ||
11113 | int | |
11114 | String_index_expression::do_traverse(Traverse* traverse) | |
11115 | { | |
11116 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
11117 | return TRAVERSE_EXIT; | |
11118 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
11119 | return TRAVERSE_EXIT; | |
11120 | if (this->end_ != NULL) | |
11121 | { | |
11122 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
11123 | return TRAVERSE_EXIT; | |
11124 | } | |
11125 | return TRAVERSE_CONTINUE; | |
11126 | } | |
11127 | ||
2c809f8f | 11128 | Expression* |
11129 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
11130 | Statement_inserter* inserter) | |
e440a328 | 11131 | { |
2c809f8f | 11132 | Location loc = this->location(); |
5bf8be8b | 11133 | Expression* string = this->string_; |
11134 | Expression* start = this->start_; | |
11135 | Expression* end = this->end_; | |
11136 | if (string->is_error_expression() | |
11137 | || string->type()->is_error_type() | |
11138 | || start->is_error_expression() | |
11139 | || start->type()->is_error_type() | |
11140 | || (end != NULL | |
11141 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
11142 | { | |
11143 | go_assert(saw_errors()); | |
11144 | return Expression::make_error(loc); | |
11145 | } | |
11146 | ||
11147 | Temporary_statement* temp; | |
2c809f8f | 11148 | if (!this->string_->is_variable()) |
11149 | { | |
11150 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
11151 | inserter->insert(temp); | |
11152 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
11153 | } | |
11154 | if (!this->start_->is_variable()) | |
11155 | { | |
11156 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
11157 | inserter->insert(temp); | |
11158 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
11159 | } | |
11160 | if (this->end_ != NULL | |
11161 | && !this->end_->is_nil_expression() | |
11162 | && !this->end_->is_variable()) | |
11163 | { | |
11164 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
11165 | inserter->insert(temp); | |
11166 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
11167 | } | |
11168 | ||
11169 | return this; | |
11170 | } | |
11171 | ||
11172 | // Return the type of a string index. | |
11173 | ||
11174 | Type* | |
11175 | String_index_expression::do_type() | |
11176 | { | |
11177 | if (this->end_ == NULL) | |
11178 | return Type::lookup_integer_type("uint8"); | |
11179 | else | |
11180 | return this->string_->type(); | |
11181 | } | |
11182 | ||
11183 | // Determine the type of a string index. | |
11184 | ||
11185 | void | |
11186 | String_index_expression::do_determine_type(const Type_context*) | |
11187 | { | |
11188 | this->string_->determine_type_no_context(); | |
f77aa642 | 11189 | |
11190 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
11191 | if (this->start_->is_constant()) | |
11192 | this->start_->determine_type(&index_context); | |
11193 | else | |
11194 | this->start_->determine_type_no_context(); | |
e440a328 | 11195 | if (this->end_ != NULL) |
f77aa642 | 11196 | { |
11197 | if (this->end_->is_constant()) | |
11198 | this->end_->determine_type(&index_context); | |
11199 | else | |
11200 | this->end_->determine_type_no_context(); | |
11201 | } | |
e440a328 | 11202 | } |
11203 | ||
11204 | // Check types of a string index. | |
11205 | ||
11206 | void | |
11207 | String_index_expression::do_check_types(Gogo*) | |
11208 | { | |
acdc230d | 11209 | Numeric_constant nc; |
11210 | unsigned long v; | |
11211 | if (this->start_->type()->integer_type() == NULL | |
11212 | && !this->start_->type()->is_error() | |
11213 | && (!this->start_->numeric_constant_value(&nc) | |
11214 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11215 | this->report_error(_("index must be integer")); |
11216 | if (this->end_ != NULL | |
11217 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 11218 | && !this->end_->type()->is_error() |
11219 | && !this->end_->is_nil_expression() | |
11220 | && !this->end_->is_error_expression() | |
11221 | && (!this->end_->numeric_constant_value(&nc) | |
11222 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11223 | this->report_error(_("slice end must be integer")); |
11224 | ||
11225 | std::string sval; | |
11226 | bool sval_valid = this->string_->string_constant_value(&sval); | |
11227 | ||
0c77715b | 11228 | Numeric_constant inc; |
e440a328 | 11229 | mpz_t ival; |
0bd5d859 | 11230 | bool ival_valid = false; |
0c77715b | 11231 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 11232 | { |
0bd5d859 | 11233 | ival_valid = true; |
e440a328 | 11234 | if (mpz_sgn(ival) < 0 |
b10f32fb | 11235 | || (sval_valid |
11236 | && (this->end_ == NULL | |
11237 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
11238 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 11239 | { |
631d5788 | 11240 | go_error_at(this->start_->location(), "string index out of bounds"); |
e440a328 | 11241 | this->set_is_error(); |
11242 | } | |
11243 | } | |
11244 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
11245 | { | |
0c77715b | 11246 | Numeric_constant enc; |
11247 | mpz_t eval; | |
11248 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 11249 | { |
0c77715b | 11250 | if (mpz_sgn(eval) < 0 |
11251 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 11252 | { |
631d5788 | 11253 | go_error_at(this->end_->location(), "string index out of bounds"); |
e440a328 | 11254 | this->set_is_error(); |
11255 | } | |
0bd5d859 | 11256 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
11257 | this->report_error(_("inverted slice range")); | |
0c77715b | 11258 | mpz_clear(eval); |
e440a328 | 11259 | } |
11260 | } | |
0bd5d859 | 11261 | if (ival_valid) |
11262 | mpz_clear(ival); | |
e440a328 | 11263 | } |
11264 | ||
ea664253 | 11265 | // Get the backend representation for a string index. |
e440a328 | 11266 | |
ea664253 | 11267 | Bexpression* |
11268 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11269 | { |
b13c66cd | 11270 | Location loc = this->location(); |
2c809f8f | 11271 | Expression* string_arg = this->string_; |
11272 | if (this->string_->type()->points_to() != NULL) | |
f614ea8b | 11273 | string_arg = Expression::make_dereference(this->string_, |
11274 | NIL_CHECK_NOT_NEEDED, loc); | |
e440a328 | 11275 | |
2c809f8f | 11276 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 11277 | |
2c809f8f | 11278 | int code = (this->end_ == NULL |
11279 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
11280 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 11281 | |
2c809f8f | 11282 | Gogo* gogo = context->gogo(); |
ea664253 | 11283 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 11284 | |
11285 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 11286 | |
2c809f8f | 11287 | // It is possible that an error occurred earlier because the start index |
11288 | // cannot be represented as an integer type. In this case, we shouldn't | |
11289 | // try casting the starting index into an integer since | |
11290 | // Type_conversion_expression will fail to get the backend representation. | |
11291 | // FIXME. | |
11292 | if (this->start_->type()->integer_type() == NULL | |
11293 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
11294 | { | |
11295 | go_assert(saw_errors()); | |
ea664253 | 11296 | return context->backend()->error_expression(); |
2c809f8f | 11297 | } |
e440a328 | 11298 | |
2c809f8f | 11299 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
93715b75 | 11300 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 11301 | |
2c809f8f | 11302 | if (this->end_ == NULL) |
11303 | { | |
11304 | Expression* length = | |
11305 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 11306 | |
2c809f8f | 11307 | Expression* start_too_large = |
11308 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
11309 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
11310 | bad_index, loc); | |
11311 | Expression* bytes = | |
11312 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 11313 | |
ea664253 | 11314 | Bexpression* bstart = start->get_backend(context); |
11315 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 11316 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 11317 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
11318 | Bexpression* index = | |
11319 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 11320 | |
2c809f8f | 11321 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 11322 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11323 | return gogo->backend()->conditional_expression(bfn, byte_btype, |
11324 | index_error, crash, | |
11325 | index, loc); | |
2c809f8f | 11326 | } |
11327 | ||
11328 | Expression* end = NULL; | |
11329 | if (this->end_->is_nil_expression()) | |
e67508fa | 11330 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 11331 | else |
11332 | { | |
2c809f8f | 11333 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
11334 | bad_index = | |
11335 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
11336 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 11337 | } |
2c809f8f | 11338 | |
11339 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
11340 | string_arg, start, end); | |
ea664253 | 11341 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 11342 | |
11343 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 11344 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11345 | return gogo->backend()->conditional_expression(bfn, str_btype, index_error, |
ea664253 | 11346 | crash, bstrslice, loc); |
e440a328 | 11347 | } |
11348 | ||
d751bb78 | 11349 | // Dump ast representation for a string index expression. |
11350 | ||
11351 | void | |
11352 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11353 | const | |
11354 | { | |
acf2b673 | 11355 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
11356 | this->start_, this->end_, NULL); | |
d751bb78 | 11357 | } |
11358 | ||
e440a328 | 11359 | // Make a string index expression. END may be NULL. |
11360 | ||
11361 | Expression* | |
11362 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 11363 | Expression* end, Location location) |
e440a328 | 11364 | { |
11365 | return new String_index_expression(string, start, end, location); | |
11366 | } | |
11367 | ||
11368 | // Class Map_index. | |
11369 | ||
11370 | // Get the type of the map. | |
11371 | ||
11372 | Map_type* | |
11373 | Map_index_expression::get_map_type() const | |
11374 | { | |
0d5530d9 | 11375 | Map_type* mt = this->map_->type()->map_type(); |
c7524fae | 11376 | if (mt == NULL) |
c484d925 | 11377 | go_assert(saw_errors()); |
e440a328 | 11378 | return mt; |
11379 | } | |
11380 | ||
11381 | // Map index traversal. | |
11382 | ||
11383 | int | |
11384 | Map_index_expression::do_traverse(Traverse* traverse) | |
11385 | { | |
11386 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11387 | return TRAVERSE_EXIT; | |
11388 | return Expression::traverse(&this->index_, traverse); | |
11389 | } | |
11390 | ||
2c809f8f | 11391 | // We need to pass in a pointer to the key, so flatten the index into a |
11392 | // temporary variable if it isn't already. The value pointer will be | |
11393 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
11394 | // recomputation. | |
11395 | ||
11396 | Expression* | |
91c0fd76 | 11397 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 11398 | Statement_inserter* inserter) |
11399 | { | |
91c0fd76 | 11400 | Location loc = this->location(); |
2c809f8f | 11401 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 11402 | if (this->index()->is_error_expression() |
11403 | || this->index()->type()->is_error_type() | |
11404 | || mt->is_error_type()) | |
11405 | { | |
11406 | go_assert(saw_errors()); | |
11407 | return Expression::make_error(loc); | |
11408 | } | |
11409 | ||
91c0fd76 | 11410 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
11411 | { | |
11412 | if (this->index_->type()->interface_type() != NULL | |
11413 | && !this->index_->is_variable()) | |
11414 | { | |
11415 | Temporary_statement* temp = | |
11416 | Statement::make_temporary(NULL, this->index_, loc); | |
11417 | inserter->insert(temp); | |
11418 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
11419 | } | |
11420 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
11421 | this->index_, loc); | |
11422 | } | |
2c809f8f | 11423 | |
11424 | if (!this->index_->is_variable()) | |
11425 | { | |
11426 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 11427 | loc); |
2c809f8f | 11428 | inserter->insert(temp); |
91c0fd76 | 11429 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11430 | } |
11431 | ||
11432 | if (this->value_pointer_ == NULL) | |
0d5530d9 | 11433 | this->get_value_pointer(gogo); |
5bf8be8b | 11434 | if (this->value_pointer_->is_error_expression() |
11435 | || this->value_pointer_->type()->is_error_type()) | |
11436 | return Expression::make_error(loc); | |
2c809f8f | 11437 | if (!this->value_pointer_->is_variable()) |
11438 | { | |
11439 | Temporary_statement* temp = | |
91c0fd76 | 11440 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 11441 | inserter->insert(temp); |
91c0fd76 | 11442 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11443 | } |
11444 | ||
11445 | return this; | |
11446 | } | |
11447 | ||
e440a328 | 11448 | // Return the type of a map index. |
11449 | ||
11450 | Type* | |
11451 | Map_index_expression::do_type() | |
11452 | { | |
c7524fae | 11453 | Map_type* mt = this->get_map_type(); |
11454 | if (mt == NULL) | |
11455 | return Type::make_error_type(); | |
0d5530d9 | 11456 | return mt->val_type(); |
e440a328 | 11457 | } |
11458 | ||
11459 | // Fix the type of a map index. | |
11460 | ||
11461 | void | |
11462 | Map_index_expression::do_determine_type(const Type_context*) | |
11463 | { | |
11464 | this->map_->determine_type_no_context(); | |
c7524fae | 11465 | Map_type* mt = this->get_map_type(); |
11466 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11467 | Type_context subcontext(key_type, false); | |
e440a328 | 11468 | this->index_->determine_type(&subcontext); |
11469 | } | |
11470 | ||
11471 | // Check types of a map index. | |
11472 | ||
11473 | void | |
11474 | Map_index_expression::do_check_types(Gogo*) | |
11475 | { | |
11476 | std::string reason; | |
c7524fae | 11477 | Map_type* mt = this->get_map_type(); |
11478 | if (mt == NULL) | |
11479 | return; | |
11480 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11481 | { |
11482 | if (reason.empty()) | |
11483 | this->report_error(_("incompatible type for map index")); | |
11484 | else | |
11485 | { | |
631d5788 | 11486 | go_error_at(this->location(), "incompatible type for map index (%s)", |
11487 | reason.c_str()); | |
e440a328 | 11488 | this->set_is_error(); |
11489 | } | |
11490 | } | |
11491 | } | |
11492 | ||
ea664253 | 11493 | // Get the backend representation for a map index. |
e440a328 | 11494 | |
ea664253 | 11495 | Bexpression* |
11496 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11497 | { |
11498 | Map_type* type = this->get_map_type(); | |
c7524fae | 11499 | if (type == NULL) |
2c809f8f | 11500 | { |
11501 | go_assert(saw_errors()); | |
ea664253 | 11502 | return context->backend()->error_expression(); |
2c809f8f | 11503 | } |
e440a328 | 11504 | |
2c809f8f | 11505 | go_assert(this->value_pointer_ != NULL |
11506 | && this->value_pointer_->is_variable()); | |
e440a328 | 11507 | |
f614ea8b | 11508 | Expression* val = Expression::make_dereference(this->value_pointer_, |
11509 | NIL_CHECK_NOT_NEEDED, | |
11510 | this->location()); | |
0d5530d9 | 11511 | return val->get_backend(context); |
e440a328 | 11512 | } |
11513 | ||
0d5530d9 | 11514 | // Get an expression for the map index. This returns an expression |
11515 | // that evaluates to a pointer to a value. If the key is not in the | |
11516 | // map, the pointer will point to a zero value. | |
e440a328 | 11517 | |
2c809f8f | 11518 | Expression* |
0d5530d9 | 11519 | Map_index_expression::get_value_pointer(Gogo* gogo) |
e440a328 | 11520 | { |
2c809f8f | 11521 | if (this->value_pointer_ == NULL) |
746d2e73 | 11522 | { |
2c809f8f | 11523 | Map_type* type = this->get_map_type(); |
11524 | if (type == NULL) | |
746d2e73 | 11525 | { |
2c809f8f | 11526 | go_assert(saw_errors()); |
11527 | return Expression::make_error(this->location()); | |
746d2e73 | 11528 | } |
e440a328 | 11529 | |
2c809f8f | 11530 | Location loc = this->location(); |
11531 | Expression* map_ref = this->map_; | |
e440a328 | 11532 | |
0d5530d9 | 11533 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, |
11534 | this->index_, | |
2c809f8f | 11535 | loc); |
0d5530d9 | 11536 | |
11537 | Expression* zero = type->fat_zero_value(gogo); | |
11538 | ||
11539 | Expression* map_index; | |
11540 | ||
11541 | if (zero == NULL) | |
11542 | map_index = | |
11543 | Runtime::make_call(Runtime::MAPACCESS1, loc, 3, | |
11544 | Expression::make_type_descriptor(type, loc), | |
11545 | map_ref, index_ptr); | |
11546 | else | |
11547 | map_index = | |
11548 | Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, | |
11549 | Expression::make_type_descriptor(type, loc), | |
11550 | map_ref, index_ptr, zero); | |
2c809f8f | 11551 | |
11552 | Type* val_type = type->val_type(); | |
11553 | this->value_pointer_ = | |
11554 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
11555 | map_index, this->location()); | |
11556 | } | |
0d5530d9 | 11557 | |
2c809f8f | 11558 | return this->value_pointer_; |
e440a328 | 11559 | } |
11560 | ||
d751bb78 | 11561 | // Dump ast representation for a map index expression |
11562 | ||
11563 | void | |
11564 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11565 | const | |
11566 | { | |
acf2b673 | 11567 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
11568 | this->index_, NULL, NULL); | |
d751bb78 | 11569 | } |
11570 | ||
e440a328 | 11571 | // Make a map index expression. |
11572 | ||
11573 | Map_index_expression* | |
11574 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11575 | Location location) |
e440a328 | 11576 | { |
11577 | return new Map_index_expression(map, index, location); | |
11578 | } | |
11579 | ||
11580 | // Class Field_reference_expression. | |
11581 | ||
149eabc5 | 11582 | // Lower a field reference expression. There is nothing to lower, but |
11583 | // this is where we generate the tracking information for fields with | |
11584 | // the magic go:"track" tag. | |
11585 | ||
11586 | Expression* | |
11587 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11588 | Statement_inserter* inserter, int) | |
11589 | { | |
11590 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11591 | if (struct_type == NULL) | |
11592 | { | |
11593 | // Error will be reported elsewhere. | |
11594 | return this; | |
11595 | } | |
11596 | const Struct_field* field = struct_type->field(this->field_index_); | |
11597 | if (field == NULL) | |
11598 | return this; | |
11599 | if (!field->has_tag()) | |
11600 | return this; | |
11601 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11602 | return this; | |
11603 | ||
604e278d | 11604 | // References from functions generated by the compiler don't count. |
c6292d1d | 11605 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 11606 | return this; |
11607 | ||
149eabc5 | 11608 | // We have found a reference to a tracked field. Build a call to |
11609 | // the runtime function __go_fieldtrack with a string that describes | |
11610 | // the field. FIXME: We should only call this once per referenced | |
11611 | // field per function, not once for each reference to the field. | |
11612 | ||
11613 | if (this->called_fieldtrack_) | |
11614 | return this; | |
11615 | this->called_fieldtrack_ = true; | |
11616 | ||
11617 | Location loc = this->location(); | |
11618 | ||
11619 | std::string s = "fieldtrack \""; | |
11620 | Named_type* nt = this->expr_->type()->named_type(); | |
11621 | if (nt == NULL || nt->named_object()->package() == NULL) | |
11622 | s.append(gogo->pkgpath()); | |
11623 | else | |
11624 | s.append(nt->named_object()->package()->pkgpath()); | |
11625 | s.push_back('.'); | |
11626 | if (nt != NULL) | |
5c29ad36 | 11627 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11628 | s.push_back('.'); |
11629 | s.append(field->field_name()); | |
11630 | s.push_back('"'); | |
11631 | ||
11632 | // We can't use a string here, because internally a string holds a | |
11633 | // pointer to the actual bytes; when the linker garbage collects the | |
11634 | // string, it won't garbage collect the bytes. So we use a | |
11635 | // [...]byte. | |
11636 | ||
e67508fa | 11637 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 11638 | |
11639 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
6bf4793c | 11640 | Array_type* array_type = Type::make_array_type(byte_type, length_expr); |
11641 | array_type->set_is_array_incomparable(); | |
149eabc5 | 11642 | |
11643 | Expression_list* bytes = new Expression_list(); | |
11644 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11645 | { | |
e67508fa | 11646 | unsigned char c = static_cast<unsigned char>(*p); |
11647 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 11648 | } |
11649 | ||
11650 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11651 | bytes, false, loc); |
149eabc5 | 11652 | |
11653 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11654 | ||
11655 | static int count; | |
11656 | char buf[50]; | |
11657 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11658 | ++count; | |
11659 | ||
11660 | Named_object* no = gogo->add_variable(buf, var); | |
11661 | e = Expression::make_var_reference(no, loc); | |
11662 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11663 | ||
11664 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 11665 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 11666 | inserter->insert(Statement::make_statement(call, false)); |
11667 | ||
11668 | // Put this function, and the global variable we just created, into | |
11669 | // unique sections. This will permit the linker to garbage collect | |
11670 | // them if they are not referenced. The effect is that the only | |
11671 | // strings, indicating field references, that will wind up in the | |
11672 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11673 | if (function != NULL) |
11674 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11675 | var->set_in_unique_section(); |
11676 | ||
11677 | return this; | |
11678 | } | |
11679 | ||
e440a328 | 11680 | // Return the type of a field reference. |
11681 | ||
11682 | Type* | |
11683 | Field_reference_expression::do_type() | |
11684 | { | |
b0e628fb | 11685 | Type* type = this->expr_->type(); |
5c13bd80 | 11686 | if (type->is_error()) |
b0e628fb | 11687 | return type; |
11688 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11689 | go_assert(struct_type != NULL); |
e440a328 | 11690 | return struct_type->field(this->field_index_)->type(); |
11691 | } | |
11692 | ||
11693 | // Check the types for a field reference. | |
11694 | ||
11695 | void | |
11696 | Field_reference_expression::do_check_types(Gogo*) | |
11697 | { | |
b0e628fb | 11698 | Type* type = this->expr_->type(); |
5c13bd80 | 11699 | if (type->is_error()) |
b0e628fb | 11700 | return; |
11701 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11702 | go_assert(struct_type != NULL); |
11703 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11704 | } |
11705 | ||
ea664253 | 11706 | // Get the backend representation for a field reference. |
e440a328 | 11707 | |
ea664253 | 11708 | Bexpression* |
11709 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11710 | { |
ea664253 | 11711 | Bexpression* bstruct = this->expr_->get_backend(context); |
11712 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11713 | this->field_index_, | |
11714 | this->location()); | |
e440a328 | 11715 | } |
11716 | ||
d751bb78 | 11717 | // Dump ast representation for a field reference expression. |
11718 | ||
11719 | void | |
11720 | Field_reference_expression::do_dump_expression( | |
11721 | Ast_dump_context* ast_dump_context) const | |
11722 | { | |
11723 | this->expr_->dump_expression(ast_dump_context); | |
11724 | ast_dump_context->ostream() << "." << this->field_index_; | |
11725 | } | |
11726 | ||
e440a328 | 11727 | // Make a reference to a qualified identifier in an expression. |
11728 | ||
11729 | Field_reference_expression* | |
11730 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11731 | Location location) |
e440a328 | 11732 | { |
11733 | return new Field_reference_expression(expr, field_index, location); | |
11734 | } | |
11735 | ||
11736 | // Class Interface_field_reference_expression. | |
11737 | ||
2387f644 | 11738 | // Return an expression for the pointer to the function to call. |
e440a328 | 11739 | |
2387f644 | 11740 | Expression* |
11741 | Interface_field_reference_expression::get_function() | |
e440a328 | 11742 | { |
2387f644 | 11743 | Expression* ref = this->expr_; |
11744 | Location loc = this->location(); | |
11745 | if (ref->type()->points_to() != NULL) | |
f614ea8b | 11746 | ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc); |
e440a328 | 11747 | |
2387f644 | 11748 | Expression* mtable = |
11749 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11750 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11751 | |
11752 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11753 | unsigned int index; |
11754 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11755 | go_assert(field != NULL); | |
f614ea8b | 11756 | |
11757 | mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc); | |
2387f644 | 11758 | return Expression::make_field_reference(mtable, index, loc); |
e440a328 | 11759 | } |
11760 | ||
2387f644 | 11761 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11762 | // function. |
11763 | ||
2387f644 | 11764 | Expression* |
11765 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11766 | { |
2387f644 | 11767 | Expression* expr = this->expr_; |
11768 | if (expr->type()->points_to() != NULL) | |
f614ea8b | 11769 | expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT, |
11770 | this->location()); | |
2387f644 | 11771 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, |
11772 | this->location()); | |
e440a328 | 11773 | } |
11774 | ||
11775 | // Traversal. | |
11776 | ||
11777 | int | |
11778 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11779 | { | |
11780 | return Expression::traverse(&this->expr_, traverse); | |
11781 | } | |
11782 | ||
0afbb937 | 11783 | // Lower the expression. If this expression is not called, we need to |
11784 | // evaluate the expression twice when converting to the backend | |
11785 | // interface. So introduce a temporary variable if necessary. | |
11786 | ||
11787 | Expression* | |
9782d556 | 11788 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11789 | Statement_inserter* inserter) | |
0afbb937 | 11790 | { |
5bf8be8b | 11791 | if (this->expr_->is_error_expression() |
11792 | || this->expr_->type()->is_error_type()) | |
11793 | { | |
11794 | go_assert(saw_errors()); | |
11795 | return Expression::make_error(this->location()); | |
11796 | } | |
11797 | ||
2387f644 | 11798 | if (!this->expr_->is_variable()) |
0afbb937 | 11799 | { |
11800 | Temporary_statement* temp = | |
11801 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11802 | inserter->insert(temp); | |
11803 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11804 | this->location()); | |
11805 | } | |
11806 | return this; | |
11807 | } | |
11808 | ||
e440a328 | 11809 | // Return the type of an interface field reference. |
11810 | ||
11811 | Type* | |
11812 | Interface_field_reference_expression::do_type() | |
11813 | { | |
11814 | Type* expr_type = this->expr_->type(); | |
11815 | ||
11816 | Type* points_to = expr_type->points_to(); | |
11817 | if (points_to != NULL) | |
11818 | expr_type = points_to; | |
11819 | ||
11820 | Interface_type* interface_type = expr_type->interface_type(); | |
11821 | if (interface_type == NULL) | |
11822 | return Type::make_error_type(); | |
11823 | ||
11824 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11825 | if (method == NULL) | |
11826 | return Type::make_error_type(); | |
11827 | ||
11828 | return method->type(); | |
11829 | } | |
11830 | ||
11831 | // Determine types. | |
11832 | ||
11833 | void | |
11834 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11835 | { | |
11836 | this->expr_->determine_type_no_context(); | |
11837 | } | |
11838 | ||
11839 | // Check the types for an interface field reference. | |
11840 | ||
11841 | void | |
11842 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11843 | { | |
11844 | Type* type = this->expr_->type(); | |
11845 | ||
11846 | Type* points_to = type->points_to(); | |
11847 | if (points_to != NULL) | |
11848 | type = points_to; | |
11849 | ||
11850 | Interface_type* interface_type = type->interface_type(); | |
11851 | if (interface_type == NULL) | |
5c491127 | 11852 | { |
11853 | if (!type->is_error_type()) | |
11854 | this->report_error(_("expected interface or pointer to interface")); | |
11855 | } | |
e440a328 | 11856 | else |
11857 | { | |
11858 | const Typed_identifier* method = | |
11859 | interface_type->find_method(this->name_); | |
11860 | if (method == NULL) | |
11861 | { | |
631d5788 | 11862 | go_error_at(this->location(), "method %qs not in interface", |
11863 | Gogo::message_name(this->name_).c_str()); | |
e440a328 | 11864 | this->set_is_error(); |
11865 | } | |
11866 | } | |
11867 | } | |
11868 | ||
0afbb937 | 11869 | // If an interface field reference is not simply called, then it is |
11870 | // represented as a closure. The closure will hold a single variable, | |
11871 | // the value of the interface on which the method should be called. | |
11872 | // The function will be a simple thunk that pulls the value from the | |
11873 | // closure and calls the method with the remaining arguments. | |
11874 | ||
11875 | // Because method values are not common, we don't build all thunks for | |
11876 | // all possible interface methods, but instead only build them as we | |
11877 | // need them. In particular, we even build them on demand for | |
11878 | // interface methods defined in other packages. | |
11879 | ||
11880 | Interface_field_reference_expression::Interface_method_thunks | |
11881 | Interface_field_reference_expression::interface_method_thunks; | |
11882 | ||
11883 | // Find or create the thunk to call method NAME on TYPE. | |
11884 | ||
11885 | Named_object* | |
11886 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11887 | Interface_type* type, | |
11888 | const std::string& name) | |
11889 | { | |
11890 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11891 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11892 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11893 | if (ins.second) | |
11894 | { | |
11895 | // This is the first time we have seen this interface. | |
11896 | ins.first->second = new Method_thunks(); | |
11897 | } | |
11898 | ||
11899 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11900 | p != ins.first->second->end(); | |
11901 | p++) | |
11902 | if (p->first == name) | |
11903 | return p->second; | |
11904 | ||
11905 | Location loc = type->location(); | |
11906 | ||
11907 | const Typed_identifier* method_id = type->find_method(name); | |
11908 | if (method_id == NULL) | |
11909 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11910 | ||
11911 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11912 | if (orig_fntype == NULL) | |
11913 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11914 | ||
11915 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11916 | // The type here is wrong--it should be the C function type. But it |
11917 | // doesn't really matter. | |
0afbb937 | 11918 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11919 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11920 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
6bf4793c | 11921 | Struct_type* st = Type::make_struct_type(sfl, loc); |
11922 | st->set_is_struct_incomparable(); | |
11923 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 11924 | |
f8bdf81a | 11925 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11926 | |
da244e59 | 11927 | std::string thunk_name = Gogo::thunk_name(); |
11928 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 11929 | false, loc); |
11930 | ||
f8bdf81a | 11931 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11932 | cvar->set_is_used(); | |
1ecc6157 | 11933 | cvar->set_is_closure(); |
da244e59 | 11934 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
11935 | NULL, cvar); | |
f8bdf81a | 11936 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 11937 | |
f8bdf81a | 11938 | gogo->start_block(loc); |
0afbb937 | 11939 | |
11940 | // Field 0 of the closure is the function code pointer, field 1 is | |
11941 | // the value on which to invoke the method. | |
11942 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 11943 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 11944 | arg = Expression::make_field_reference(arg, 1, loc); |
11945 | ||
11946 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11947 | loc); | |
11948 | ||
11949 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11950 | Expression_list* args; | |
11951 | if (orig_params == NULL || orig_params->empty()) | |
11952 | args = NULL; | |
11953 | else | |
11954 | { | |
11955 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11956 | args = new Expression_list(); | |
11957 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11958 | p != new_params->end(); |
0afbb937 | 11959 | ++p) |
11960 | { | |
11961 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11962 | go_assert(p_no != NULL | |
11963 | && p_no->is_variable() | |
11964 | && p_no->var_value()->is_parameter()); | |
11965 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11966 | } | |
11967 | } | |
11968 | ||
11969 | Call_expression* call = Expression::make_call(ifre, args, | |
11970 | orig_fntype->is_varargs(), | |
11971 | loc); | |
11972 | call->set_varargs_are_lowered(); | |
11973 | ||
11974 | Statement* s = Statement::make_return_from_call(call, loc); | |
11975 | gogo->add_statement(s); | |
11976 | Block* b = gogo->finish_block(loc); | |
11977 | gogo->add_block(b, loc); | |
11978 | gogo->lower_block(new_no, b); | |
a32698ee | 11979 | gogo->flatten_block(new_no, b); |
0afbb937 | 11980 | gogo->finish_function(loc); |
11981 | ||
11982 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11983 | return new_no; | |
11984 | } | |
11985 | ||
ea664253 | 11986 | // Get the backend representation for a method value. |
e440a328 | 11987 | |
ea664253 | 11988 | Bexpression* |
11989 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11990 | { |
0afbb937 | 11991 | Interface_type* type = this->expr_->type()->interface_type(); |
11992 | if (type == NULL) | |
11993 | { | |
11994 | go_assert(saw_errors()); | |
ea664253 | 11995 | return context->backend()->error_expression(); |
0afbb937 | 11996 | } |
11997 | ||
11998 | Named_object* thunk = | |
11999 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
12000 | type, this->name_); | |
12001 | if (thunk->is_erroneous()) | |
12002 | { | |
12003 | go_assert(saw_errors()); | |
ea664253 | 12004 | return context->backend()->error_expression(); |
0afbb937 | 12005 | } |
12006 | ||
12007 | // FIXME: We should lower this earlier, but we can't it lower it in | |
12008 | // the lowering pass because at that point we don't know whether we | |
12009 | // need to create the thunk or not. If the expression is called, we | |
12010 | // don't need the thunk. | |
12011 | ||
12012 | Location loc = this->location(); | |
12013 | ||
12014 | Struct_field_list* fields = new Struct_field_list(); | |
12015 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
12016 | thunk->func_value()->type(), | |
12017 | loc))); | |
12018 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
12019 | this->expr_->type(), | |
12020 | loc))); | |
12021 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6bf4793c | 12022 | st->set_is_struct_incomparable(); |
0afbb937 | 12023 | |
12024 | Expression_list* vals = new Expression_list(); | |
12025 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
12026 | vals->push_back(this->expr_); | |
12027 | ||
12028 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 12029 | Bexpression* bclosure = |
12030 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 12031 | |
4df0c2d4 | 12032 | Gogo* gogo = context->gogo(); |
12033 | Btype* btype = this->type()->get_backend(gogo); | |
12034 | bclosure = gogo->backend()->convert_expression(btype, bclosure, loc); | |
12035 | ||
2387f644 | 12036 | Expression* nil_check = |
12037 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
12038 | Expression::make_nil(loc), loc); | |
ea664253 | 12039 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 12040 | |
ea664253 | 12041 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
12042 | loc)->get_backend(context); | |
2387f644 | 12043 | |
93715b75 | 12044 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
2387f644 | 12045 | Bexpression* bcond = |
93715b75 | 12046 | gogo->backend()->conditional_expression(bfn, NULL, |
12047 | bnil_check, bcrash, NULL, loc); | |
0ab48656 | 12048 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
12049 | Bstatement* cond_statement = | |
12050 | gogo->backend()->expression_statement(bfunction, bcond); | |
ea664253 | 12051 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 12052 | } |
12053 | ||
d751bb78 | 12054 | // Dump ast representation for an interface field reference. |
12055 | ||
12056 | void | |
12057 | Interface_field_reference_expression::do_dump_expression( | |
12058 | Ast_dump_context* ast_dump_context) const | |
12059 | { | |
12060 | this->expr_->dump_expression(ast_dump_context); | |
12061 | ast_dump_context->ostream() << "." << this->name_; | |
12062 | } | |
12063 | ||
e440a328 | 12064 | // Make a reference to a field in an interface. |
12065 | ||
12066 | Expression* | |
12067 | Expression::make_interface_field_reference(Expression* expr, | |
12068 | const std::string& field, | |
b13c66cd | 12069 | Location location) |
e440a328 | 12070 | { |
12071 | return new Interface_field_reference_expression(expr, field, location); | |
12072 | } | |
12073 | ||
12074 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
12075 | // is lowered after we know the type of the left hand side. | |
12076 | ||
12077 | class Selector_expression : public Parser_expression | |
12078 | { | |
12079 | public: | |
12080 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 12081 | Location location) |
e440a328 | 12082 | : Parser_expression(EXPRESSION_SELECTOR, location), |
12083 | left_(left), name_(name) | |
12084 | { } | |
12085 | ||
12086 | protected: | |
12087 | int | |
12088 | do_traverse(Traverse* traverse) | |
12089 | { return Expression::traverse(&this->left_, traverse); } | |
12090 | ||
12091 | Expression* | |
ceeb4318 | 12092 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12093 | |
12094 | Expression* | |
12095 | do_copy() | |
12096 | { | |
12097 | return new Selector_expression(this->left_->copy(), this->name_, | |
12098 | this->location()); | |
12099 | } | |
12100 | ||
d751bb78 | 12101 | void |
12102 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
12103 | ||
e440a328 | 12104 | private: |
12105 | Expression* | |
12106 | lower_method_expression(Gogo*); | |
12107 | ||
12108 | // The expression on the left hand side. | |
12109 | Expression* left_; | |
12110 | // The name on the right hand side. | |
12111 | std::string name_; | |
12112 | }; | |
12113 | ||
12114 | // Lower a selector expression once we know the real type of the left | |
12115 | // hand side. | |
12116 | ||
12117 | Expression* | |
ceeb4318 | 12118 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
12119 | int) | |
e440a328 | 12120 | { |
12121 | Expression* left = this->left_; | |
12122 | if (left->is_type_expression()) | |
12123 | return this->lower_method_expression(gogo); | |
12124 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
12125 | this->location()); | |
12126 | } | |
12127 | ||
12128 | // Lower a method expression T.M or (*T).M. We turn this into a | |
12129 | // function literal. | |
12130 | ||
12131 | Expression* | |
12132 | Selector_expression::lower_method_expression(Gogo* gogo) | |
12133 | { | |
b13c66cd | 12134 | Location location = this->location(); |
868b439e | 12135 | Type* left_type = this->left_->type(); |
12136 | Type* type = left_type; | |
e440a328 | 12137 | const std::string& name(this->name_); |
12138 | ||
12139 | bool is_pointer; | |
12140 | if (type->points_to() == NULL) | |
12141 | is_pointer = false; | |
12142 | else | |
12143 | { | |
12144 | is_pointer = true; | |
12145 | type = type->points_to(); | |
12146 | } | |
12147 | Named_type* nt = type->named_type(); | |
12148 | if (nt == NULL) | |
12149 | { | |
631d5788 | 12150 | go_error_at(location, |
12151 | ("method expression requires named type or " | |
12152 | "pointer to named type")); | |
e440a328 | 12153 | return Expression::make_error(location); |
12154 | } | |
12155 | ||
12156 | bool is_ambiguous; | |
12157 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 12158 | const Typed_identifier* imethod = NULL; |
dcc8506b | 12159 | if (method == NULL && !is_pointer) |
ab1468c3 | 12160 | { |
12161 | Interface_type* it = nt->interface_type(); | |
12162 | if (it != NULL) | |
12163 | imethod = it->find_method(name); | |
12164 | } | |
12165 | ||
868b439e | 12166 | if ((method == NULL && imethod == NULL) |
12167 | || (left_type->named_type() != NULL && left_type->points_to() != NULL)) | |
e440a328 | 12168 | { |
12169 | if (!is_ambiguous) | |
631d5788 | 12170 | go_error_at(location, "type %<%s%s%> has no method %<%s%>", |
12171 | is_pointer ? "*" : "", | |
12172 | nt->message_name().c_str(), | |
12173 | Gogo::message_name(name).c_str()); | |
e440a328 | 12174 | else |
631d5788 | 12175 | go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
12176 | Gogo::message_name(name).c_str(), | |
12177 | is_pointer ? "*" : "", | |
12178 | nt->message_name().c_str()); | |
e440a328 | 12179 | return Expression::make_error(location); |
12180 | } | |
12181 | ||
ab1468c3 | 12182 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 12183 | { |
631d5788 | 12184 | go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", |
12185 | nt->message_name().c_str(), | |
12186 | Gogo::message_name(name).c_str()); | |
e440a328 | 12187 | return Expression::make_error(location); |
12188 | } | |
12189 | ||
12190 | // Build a new function type in which the receiver becomes the first | |
12191 | // argument. | |
ab1468c3 | 12192 | Function_type* method_type; |
12193 | if (method != NULL) | |
12194 | { | |
12195 | method_type = method->type(); | |
c484d925 | 12196 | go_assert(method_type->is_method()); |
ab1468c3 | 12197 | } |
12198 | else | |
12199 | { | |
12200 | method_type = imethod->type()->function_type(); | |
c484d925 | 12201 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 12202 | } |
e440a328 | 12203 | |
12204 | const char* const receiver_name = "$this"; | |
12205 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
12206 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
12207 | location)); | |
12208 | ||
12209 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
12210 | if (method_parameters != NULL) | |
12211 | { | |
f470da59 | 12212 | int i = 0; |
e440a328 | 12213 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
12214 | p != method_parameters->end(); | |
f470da59 | 12215 | ++p, ++i) |
12216 | { | |
68883531 | 12217 | if (!p->name().empty()) |
f470da59 | 12218 | parameters->push_back(*p); |
12219 | else | |
12220 | { | |
12221 | char buf[20]; | |
12222 | snprintf(buf, sizeof buf, "$param%d", i); | |
12223 | parameters->push_back(Typed_identifier(buf, p->type(), | |
12224 | p->location())); | |
12225 | } | |
12226 | } | |
e440a328 | 12227 | } |
12228 | ||
12229 | const Typed_identifier_list* method_results = method_type->results(); | |
12230 | Typed_identifier_list* results; | |
12231 | if (method_results == NULL) | |
12232 | results = NULL; | |
12233 | else | |
12234 | { | |
12235 | results = new Typed_identifier_list(); | |
12236 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
12237 | p != method_results->end(); | |
12238 | ++p) | |
12239 | results->push_back(*p); | |
12240 | } | |
12241 | ||
12242 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
12243 | location); | |
12244 | if (method_type->is_varargs()) | |
12245 | fntype->set_is_varargs(); | |
12246 | ||
12247 | // We generate methods which always takes a pointer to the receiver | |
12248 | // as their first argument. If this is for a pointer type, we can | |
12249 | // simply reuse the existing function. We use an internal hack to | |
12250 | // get the right type. | |
8381eda7 | 12251 | // FIXME: This optimization is disabled because it doesn't yet work |
12252 | // with function descriptors when the method expression is not | |
12253 | // directly called. | |
12254 | if (method != NULL && is_pointer && false) | |
e440a328 | 12255 | { |
12256 | Named_object* mno = (method->needs_stub_method() | |
12257 | ? method->stub_object() | |
12258 | : method->named_object()); | |
12259 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
12260 | f = Expression::make_cast(fntype, f, location); | |
12261 | Type_conversion_expression* tce = | |
12262 | static_cast<Type_conversion_expression*>(f); | |
12263 | tce->set_may_convert_function_types(); | |
12264 | return f; | |
12265 | } | |
12266 | ||
12267 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
12268 | location); | |
12269 | ||
12270 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 12271 | go_assert(vno != NULL); |
e440a328 | 12272 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 12273 | Expression* bm; |
12274 | if (method != NULL) | |
12275 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
12276 | else | |
12277 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 12278 | |
12279 | // Even though we found the method above, if it has an error type we | |
12280 | // may see an error here. | |
12281 | if (bm->is_error_expression()) | |
463fe805 | 12282 | { |
12283 | gogo->finish_function(location); | |
12284 | return bm; | |
12285 | } | |
e440a328 | 12286 | |
12287 | Expression_list* args; | |
f470da59 | 12288 | if (parameters->size() <= 1) |
e440a328 | 12289 | args = NULL; |
12290 | else | |
12291 | { | |
12292 | args = new Expression_list(); | |
f470da59 | 12293 | Typed_identifier_list::const_iterator p = parameters->begin(); |
12294 | ++p; | |
12295 | for (; p != parameters->end(); ++p) | |
e440a328 | 12296 | { |
12297 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 12298 | go_assert(vno != NULL); |
e440a328 | 12299 | args->push_back(Expression::make_var_reference(vno, location)); |
12300 | } | |
12301 | } | |
12302 | ||
ceeb4318 | 12303 | gogo->start_block(location); |
12304 | ||
e440a328 | 12305 | Call_expression* call = Expression::make_call(bm, args, |
12306 | method_type->is_varargs(), | |
12307 | location); | |
12308 | ||
0afbb937 | 12309 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 12310 | gogo->add_statement(s); |
12311 | ||
ceeb4318 | 12312 | Block* b = gogo->finish_block(location); |
12313 | ||
12314 | gogo->add_block(b, location); | |
12315 | ||
12316 | // Lower the call in case there are multiple results. | |
12317 | gogo->lower_block(no, b); | |
a32698ee | 12318 | gogo->flatten_block(no, b); |
ceeb4318 | 12319 | |
e440a328 | 12320 | gogo->finish_function(location); |
12321 | ||
12322 | return Expression::make_func_reference(no, NULL, location); | |
12323 | } | |
12324 | ||
d751bb78 | 12325 | // Dump the ast for a selector expression. |
12326 | ||
12327 | void | |
12328 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12329 | const | |
12330 | { | |
12331 | ast_dump_context->dump_expression(this->left_); | |
12332 | ast_dump_context->ostream() << "."; | |
12333 | ast_dump_context->ostream() << this->name_; | |
12334 | } | |
12335 | ||
e440a328 | 12336 | // Make a selector expression. |
12337 | ||
12338 | Expression* | |
12339 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 12340 | Location location) |
e440a328 | 12341 | { |
12342 | return new Selector_expression(left, name, location); | |
12343 | } | |
12344 | ||
da244e59 | 12345 | // Class Allocation_expression. |
e440a328 | 12346 | |
da244e59 | 12347 | int |
12348 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 12349 | { |
da244e59 | 12350 | return Type::traverse(this->type_, traverse); |
12351 | } | |
e440a328 | 12352 | |
da244e59 | 12353 | Type* |
12354 | Allocation_expression::do_type() | |
12355 | { | |
12356 | return Type::make_pointer_type(this->type_); | |
12357 | } | |
e440a328 | 12358 | |
22deed0d | 12359 | void |
12360 | Allocation_expression::do_check_types(Gogo*) | |
12361 | { | |
12362 | if (!this->type_->in_heap()) | |
12363 | go_error_at(this->location(), "can't heap allocate go:notinheap type"); | |
12364 | } | |
12365 | ||
da244e59 | 12366 | // Make a copy of an allocation expression. |
e440a328 | 12367 | |
da244e59 | 12368 | Expression* |
12369 | Allocation_expression::do_copy() | |
12370 | { | |
12371 | Allocation_expression* alloc = | |
12372 | new Allocation_expression(this->type_, this->location()); | |
12373 | if (this->allocate_on_stack_) | |
12374 | alloc->set_allocate_on_stack(); | |
12375 | return alloc; | |
12376 | } | |
e440a328 | 12377 | |
ea664253 | 12378 | // Return the backend representation for an allocation expression. |
e440a328 | 12379 | |
ea664253 | 12380 | Bexpression* |
12381 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12382 | { |
2c809f8f | 12383 | Gogo* gogo = context->gogo(); |
12384 | Location loc = this->location(); | |
da244e59 | 12385 | |
45ff893b | 12386 | Node* n = Node::make_node(this); |
12387 | if (this->allocate_on_stack_ | |
12388 | || (n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
da244e59 | 12389 | { |
2a305b85 | 12390 | int64_t size; |
12391 | bool ok = this->type_->backend_type_size(gogo, &size); | |
12392 | if (!ok) | |
12393 | { | |
12394 | go_assert(saw_errors()); | |
12395 | return gogo->backend()->error_expression(); | |
12396 | } | |
d5d1c295 | 12397 | return gogo->backend()->stack_allocation_expression(size, loc); |
da244e59 | 12398 | } |
12399 | ||
2a305b85 | 12400 | Btype* btype = this->type_->get_backend(gogo); |
12401 | Bexpression* space = | |
ea664253 | 12402 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 12403 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 12404 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 12405 | } |
12406 | ||
d751bb78 | 12407 | // Dump ast representation for an allocation expression. |
12408 | ||
12409 | void | |
12410 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12411 | const | |
12412 | { | |
12413 | ast_dump_context->ostream() << "new("; | |
12414 | ast_dump_context->dump_type(this->type_); | |
12415 | ast_dump_context->ostream() << ")"; | |
12416 | } | |
12417 | ||
e440a328 | 12418 | // Make an allocation expression. |
12419 | ||
12420 | Expression* | |
b13c66cd | 12421 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12422 | { |
12423 | return new Allocation_expression(type, location); | |
12424 | } | |
12425 | ||
e32de7ba | 12426 | // Class Ordered_value_list. |
e440a328 | 12427 | |
12428 | int | |
e32de7ba | 12429 | Ordered_value_list::traverse_vals(Traverse* traverse) |
e440a328 | 12430 | { |
0c4f5a19 | 12431 | if (this->vals_ != NULL) |
12432 | { | |
12433 | if (this->traverse_order_ == NULL) | |
12434 | { | |
12435 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12436 | return TRAVERSE_EXIT; | |
12437 | } | |
12438 | else | |
12439 | { | |
e32de7ba | 12440 | for (std::vector<unsigned long>::const_iterator p = |
12441 | this->traverse_order_->begin(); | |
0c4f5a19 | 12442 | p != this->traverse_order_->end(); |
12443 | ++p) | |
12444 | { | |
12445 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12446 | == TRAVERSE_EXIT) | |
12447 | return TRAVERSE_EXIT; | |
12448 | } | |
12449 | } | |
12450 | } | |
e32de7ba | 12451 | return TRAVERSE_CONTINUE; |
12452 | } | |
12453 | ||
12454 | // Class Struct_construction_expression. | |
12455 | ||
12456 | // Traversal. | |
12457 | ||
12458 | int | |
12459 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12460 | { | |
12461 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) | |
12462 | return TRAVERSE_EXIT; | |
e440a328 | 12463 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12464 | return TRAVERSE_EXIT; | |
12465 | return TRAVERSE_CONTINUE; | |
12466 | } | |
12467 | ||
12468 | // Return whether this is a constant initializer. | |
12469 | ||
12470 | bool | |
12471 | Struct_construction_expression::is_constant_struct() const | |
12472 | { | |
e32de7ba | 12473 | if (this->vals() == NULL) |
e440a328 | 12474 | return true; |
e32de7ba | 12475 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12476 | pv != this->vals()->end(); | |
e440a328 | 12477 | ++pv) |
12478 | { | |
12479 | if (*pv != NULL | |
12480 | && !(*pv)->is_constant() | |
12481 | && (!(*pv)->is_composite_literal() | |
12482 | || (*pv)->is_nonconstant_composite_literal())) | |
12483 | return false; | |
12484 | } | |
12485 | ||
12486 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12487 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12488 | pf != fields->end(); | |
12489 | ++pf) | |
12490 | { | |
12491 | // There are no constant constructors for interfaces. | |
12492 | if (pf->type()->interface_type() != NULL) | |
12493 | return false; | |
12494 | } | |
12495 | ||
12496 | return true; | |
12497 | } | |
12498 | ||
3ae06f68 | 12499 | // Return whether this struct can be used as a constant initializer. |
f9ca30f9 | 12500 | |
12501 | bool | |
3ae06f68 | 12502 | Struct_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12503 | { |
e32de7ba | 12504 | if (this->vals() == NULL) |
f9ca30f9 | 12505 | return true; |
e32de7ba | 12506 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12507 | pv != this->vals()->end(); | |
f9ca30f9 | 12508 | ++pv) |
12509 | { | |
3ae06f68 | 12510 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12511 | return false; |
12512 | } | |
de048538 | 12513 | |
12514 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12515 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12516 | pf != fields->end(); | |
12517 | ++pf) | |
12518 | { | |
12519 | // There are no constant constructors for interfaces. | |
12520 | if (pf->type()->interface_type() != NULL) | |
12521 | return false; | |
12522 | } | |
12523 | ||
f9ca30f9 | 12524 | return true; |
12525 | } | |
12526 | ||
e440a328 | 12527 | // Final type determination. |
12528 | ||
12529 | void | |
12530 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12531 | { | |
e32de7ba | 12532 | if (this->vals() == NULL) |
e440a328 | 12533 | return; |
12534 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
e32de7ba | 12535 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12536 | for (Struct_field_list::const_iterator pf = fields->begin(); |
12537 | pf != fields->end(); | |
12538 | ++pf, ++pv) | |
12539 | { | |
e32de7ba | 12540 | if (pv == this->vals()->end()) |
e440a328 | 12541 | return; |
12542 | if (*pv != NULL) | |
12543 | { | |
12544 | Type_context subcontext(pf->type(), false); | |
12545 | (*pv)->determine_type(&subcontext); | |
12546 | } | |
12547 | } | |
a6cb4c0e | 12548 | // Extra values are an error we will report elsewhere; we still want |
12549 | // to determine the type to avoid knockon errors. | |
e32de7ba | 12550 | for (; pv != this->vals()->end(); ++pv) |
a6cb4c0e | 12551 | (*pv)->determine_type_no_context(); |
e440a328 | 12552 | } |
12553 | ||
12554 | // Check types. | |
12555 | ||
12556 | void | |
12557 | Struct_construction_expression::do_check_types(Gogo*) | |
12558 | { | |
e32de7ba | 12559 | if (this->vals() == NULL) |
e440a328 | 12560 | return; |
12561 | ||
12562 | Struct_type* st = this->type_->struct_type(); | |
e32de7ba | 12563 | if (this->vals()->size() > st->field_count()) |
e440a328 | 12564 | { |
12565 | this->report_error(_("too many expressions for struct")); | |
12566 | return; | |
12567 | } | |
12568 | ||
12569 | const Struct_field_list* fields = st->fields(); | |
e32de7ba | 12570 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12571 | int i = 0; |
12572 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12573 | pf != fields->end(); | |
12574 | ++pf, ++pv, ++i) | |
12575 | { | |
e32de7ba | 12576 | if (pv == this->vals()->end()) |
e440a328 | 12577 | { |
12578 | this->report_error(_("too few expressions for struct")); | |
12579 | break; | |
12580 | } | |
12581 | ||
12582 | if (*pv == NULL) | |
12583 | continue; | |
12584 | ||
12585 | std::string reason; | |
12586 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12587 | { | |
12588 | if (reason.empty()) | |
631d5788 | 12589 | go_error_at((*pv)->location(), |
12590 | "incompatible type for field %d in struct construction", | |
12591 | i + 1); | |
e440a328 | 12592 | else |
631d5788 | 12593 | go_error_at((*pv)->location(), |
12594 | ("incompatible type for field %d in " | |
12595 | "struct construction (%s)"), | |
12596 | i + 1, reason.c_str()); | |
e440a328 | 12597 | this->set_is_error(); |
12598 | } | |
12599 | } | |
e32de7ba | 12600 | go_assert(pv == this->vals()->end()); |
e440a328 | 12601 | } |
12602 | ||
8ba8cc87 | 12603 | // Flatten a struct construction expression. Store the values into |
12604 | // temporaries in case they need interface conversion. | |
12605 | ||
12606 | Expression* | |
12607 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
12608 | Statement_inserter* inserter) | |
12609 | { | |
e32de7ba | 12610 | if (this->vals() == NULL) |
8ba8cc87 | 12611 | return this; |
12612 | ||
12613 | // If this is a constant struct, we don't need temporaries. | |
de048538 | 12614 | if (this->is_constant_struct() || this->is_static_initializer()) |
8ba8cc87 | 12615 | return this; |
12616 | ||
12617 | Location loc = this->location(); | |
e32de7ba | 12618 | for (Expression_list::iterator pv = this->vals()->begin(); |
12619 | pv != this->vals()->end(); | |
8ba8cc87 | 12620 | ++pv) |
12621 | { | |
12622 | if (*pv != NULL) | |
12623 | { | |
5bf8be8b | 12624 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12625 | { | |
12626 | go_assert(saw_errors()); | |
12627 | return Expression::make_error(loc); | |
12628 | } | |
8ba8cc87 | 12629 | if (!(*pv)->is_variable()) |
12630 | { | |
12631 | Temporary_statement* temp = | |
12632 | Statement::make_temporary(NULL, *pv, loc); | |
12633 | inserter->insert(temp); | |
12634 | *pv = Expression::make_temporary_reference(temp, loc); | |
12635 | } | |
12636 | } | |
12637 | } | |
12638 | return this; | |
12639 | } | |
12640 | ||
ea664253 | 12641 | // Return the backend representation for constructing a struct. |
e440a328 | 12642 | |
ea664253 | 12643 | Bexpression* |
12644 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12645 | { |
12646 | Gogo* gogo = context->gogo(); | |
12647 | ||
2c809f8f | 12648 | Btype* btype = this->type_->get_backend(gogo); |
e32de7ba | 12649 | if (this->vals() == NULL) |
ea664253 | 12650 | return gogo->backend()->zero_expression(btype); |
e440a328 | 12651 | |
e440a328 | 12652 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e32de7ba | 12653 | Expression_list::const_iterator pv = this->vals()->begin(); |
2c809f8f | 12654 | std::vector<Bexpression*> init; |
12655 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12656 | pf != fields->end(); | |
12657 | ++pf) | |
e440a328 | 12658 | { |
63697958 | 12659 | Btype* fbtype = pf->type()->get_backend(gogo); |
e32de7ba | 12660 | if (pv == this->vals()->end()) |
2c809f8f | 12661 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12662 | else if (*pv == NULL) |
12663 | { | |
2c809f8f | 12664 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12665 | ++pv; |
12666 | } | |
12667 | else | |
12668 | { | |
2c809f8f | 12669 | Expression* val = |
12670 | Expression::convert_for_assignment(gogo, pf->type(), | |
12671 | *pv, this->location()); | |
ea664253 | 12672 | init.push_back(val->get_backend(context)); |
e440a328 | 12673 | ++pv; |
12674 | } | |
e440a328 | 12675 | } |
ea664253 | 12676 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12677 | } |
12678 | ||
12679 | // Export a struct construction. | |
12680 | ||
12681 | void | |
12682 | Struct_construction_expression::do_export(Export* exp) const | |
12683 | { | |
12684 | exp->write_c_string("convert("); | |
12685 | exp->write_type(this->type_); | |
e32de7ba | 12686 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12687 | pv != this->vals()->end(); | |
e440a328 | 12688 | ++pv) |
12689 | { | |
12690 | exp->write_c_string(", "); | |
12691 | if (*pv != NULL) | |
12692 | (*pv)->export_expression(exp); | |
12693 | } | |
12694 | exp->write_c_string(")"); | |
12695 | } | |
12696 | ||
d751bb78 | 12697 | // Dump ast representation of a struct construction expression. |
12698 | ||
12699 | void | |
12700 | Struct_construction_expression::do_dump_expression( | |
12701 | Ast_dump_context* ast_dump_context) const | |
12702 | { | |
d751bb78 | 12703 | ast_dump_context->dump_type(this->type_); |
12704 | ast_dump_context->ostream() << "{"; | |
e32de7ba | 12705 | ast_dump_context->dump_expression_list(this->vals()); |
d751bb78 | 12706 | ast_dump_context->ostream() << "}"; |
12707 | } | |
12708 | ||
e440a328 | 12709 | // Make a struct composite literal. This used by the thunk code. |
12710 | ||
12711 | Expression* | |
12712 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12713 | Location location) |
e440a328 | 12714 | { |
c484d925 | 12715 | go_assert(type->struct_type() != NULL); |
e440a328 | 12716 | return new Struct_construction_expression(type, vals, location); |
12717 | } | |
12718 | ||
da244e59 | 12719 | // Class Array_construction_expression. |
e440a328 | 12720 | |
12721 | // Traversal. | |
12722 | ||
12723 | int | |
12724 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12725 | { | |
e32de7ba | 12726 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) |
e440a328 | 12727 | return TRAVERSE_EXIT; |
12728 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12729 | return TRAVERSE_EXIT; | |
12730 | return TRAVERSE_CONTINUE; | |
12731 | } | |
12732 | ||
12733 | // Return whether this is a constant initializer. | |
12734 | ||
12735 | bool | |
12736 | Array_construction_expression::is_constant_array() const | |
12737 | { | |
e32de7ba | 12738 | if (this->vals() == NULL) |
e440a328 | 12739 | return true; |
12740 | ||
12741 | // There are no constant constructors for interfaces. | |
12742 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12743 | return false; | |
12744 | ||
e32de7ba | 12745 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12746 | pv != this->vals()->end(); | |
e440a328 | 12747 | ++pv) |
12748 | { | |
12749 | if (*pv != NULL | |
12750 | && !(*pv)->is_constant() | |
12751 | && (!(*pv)->is_composite_literal() | |
12752 | || (*pv)->is_nonconstant_composite_literal())) | |
12753 | return false; | |
12754 | } | |
12755 | return true; | |
12756 | } | |
12757 | ||
3ae06f68 | 12758 | // Return whether this can be used a constant initializer. |
f9ca30f9 | 12759 | |
12760 | bool | |
3ae06f68 | 12761 | Array_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12762 | { |
e32de7ba | 12763 | if (this->vals() == NULL) |
f9ca30f9 | 12764 | return true; |
de048538 | 12765 | |
12766 | // There are no constant constructors for interfaces. | |
12767 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12768 | return false; | |
12769 | ||
e32de7ba | 12770 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12771 | pv != this->vals()->end(); | |
f9ca30f9 | 12772 | ++pv) |
12773 | { | |
3ae06f68 | 12774 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12775 | return false; |
12776 | } | |
12777 | return true; | |
12778 | } | |
12779 | ||
e440a328 | 12780 | // Final type determination. |
12781 | ||
12782 | void | |
12783 | Array_construction_expression::do_determine_type(const Type_context*) | |
12784 | { | |
e32de7ba | 12785 | if (this->vals() == NULL) |
e440a328 | 12786 | return; |
12787 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
e32de7ba | 12788 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12789 | pv != this->vals()->end(); | |
e440a328 | 12790 | ++pv) |
12791 | { | |
12792 | if (*pv != NULL) | |
12793 | (*pv)->determine_type(&subcontext); | |
12794 | } | |
12795 | } | |
12796 | ||
12797 | // Check types. | |
12798 | ||
12799 | void | |
12800 | Array_construction_expression::do_check_types(Gogo*) | |
12801 | { | |
e32de7ba | 12802 | if (this->vals() == NULL) |
e440a328 | 12803 | return; |
12804 | ||
12805 | Array_type* at = this->type_->array_type(); | |
12806 | int i = 0; | |
12807 | Type* element_type = at->element_type(); | |
e32de7ba | 12808 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12809 | pv != this->vals()->end(); | |
e440a328 | 12810 | ++pv, ++i) |
12811 | { | |
12812 | if (*pv != NULL | |
12813 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12814 | { | |
631d5788 | 12815 | go_error_at((*pv)->location(), |
12816 | "incompatible type for element %d in composite literal", | |
12817 | i + 1); | |
e440a328 | 12818 | this->set_is_error(); |
12819 | } | |
12820 | } | |
e440a328 | 12821 | } |
12822 | ||
8ba8cc87 | 12823 | // Flatten an array construction expression. Store the values into |
12824 | // temporaries in case they need interface conversion. | |
12825 | ||
12826 | Expression* | |
12827 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12828 | Statement_inserter* inserter) | |
12829 | { | |
e32de7ba | 12830 | if (this->vals() == NULL) |
8ba8cc87 | 12831 | return this; |
12832 | ||
12833 | // If this is a constant array, we don't need temporaries. | |
de048538 | 12834 | if (this->is_constant_array() || this->is_static_initializer()) |
8ba8cc87 | 12835 | return this; |
12836 | ||
12837 | Location loc = this->location(); | |
e32de7ba | 12838 | for (Expression_list::iterator pv = this->vals()->begin(); |
12839 | pv != this->vals()->end(); | |
8ba8cc87 | 12840 | ++pv) |
12841 | { | |
12842 | if (*pv != NULL) | |
12843 | { | |
5bf8be8b | 12844 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12845 | { | |
12846 | go_assert(saw_errors()); | |
12847 | return Expression::make_error(loc); | |
12848 | } | |
8ba8cc87 | 12849 | if (!(*pv)->is_variable()) |
12850 | { | |
12851 | Temporary_statement* temp = | |
12852 | Statement::make_temporary(NULL, *pv, loc); | |
12853 | inserter->insert(temp); | |
12854 | *pv = Expression::make_temporary_reference(temp, loc); | |
12855 | } | |
12856 | } | |
12857 | } | |
12858 | return this; | |
12859 | } | |
12860 | ||
2c809f8f | 12861 | // Get a constructor expression for the array values. |
e440a328 | 12862 | |
2c809f8f | 12863 | Bexpression* |
12864 | Array_construction_expression::get_constructor(Translate_context* context, | |
12865 | Btype* array_btype) | |
e440a328 | 12866 | { |
e440a328 | 12867 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12868 | |
12869 | std::vector<unsigned long> indexes; | |
12870 | std::vector<Bexpression*> vals; | |
12871 | Gogo* gogo = context->gogo(); | |
e32de7ba | 12872 | if (this->vals() != NULL) |
e440a328 | 12873 | { |
12874 | size_t i = 0; | |
ffe743ca | 12875 | std::vector<unsigned long>::const_iterator pi; |
12876 | if (this->indexes_ != NULL) | |
12877 | pi = this->indexes_->begin(); | |
e32de7ba | 12878 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12879 | pv != this->vals()->end(); | |
e440a328 | 12880 | ++pv, ++i) |
12881 | { | |
ffe743ca | 12882 | if (this->indexes_ != NULL) |
12883 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12884 | |
12885 | if (this->indexes_ == NULL) | |
2c809f8f | 12886 | indexes.push_back(i); |
ffe743ca | 12887 | else |
2c809f8f | 12888 | indexes.push_back(*pi); |
e440a328 | 12889 | if (*pv == NULL) |
63697958 | 12890 | { |
63697958 | 12891 | Btype* ebtype = element_type->get_backend(gogo); |
12892 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12893 | vals.push_back(zv); |
63697958 | 12894 | } |
e440a328 | 12895 | else |
12896 | { | |
2c809f8f | 12897 | Expression* val_expr = |
12898 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12899 | this->location()); | |
ea664253 | 12900 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12901 | } |
ffe743ca | 12902 | if (this->indexes_ != NULL) |
12903 | ++pi; | |
e440a328 | 12904 | } |
ffe743ca | 12905 | if (this->indexes_ != NULL) |
12906 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12907 | } |
2c809f8f | 12908 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12909 | vals, this->location()); | |
e440a328 | 12910 | } |
12911 | ||
12912 | // Export an array construction. | |
12913 | ||
12914 | void | |
12915 | Array_construction_expression::do_export(Export* exp) const | |
12916 | { | |
12917 | exp->write_c_string("convert("); | |
12918 | exp->write_type(this->type_); | |
e32de7ba | 12919 | if (this->vals() != NULL) |
e440a328 | 12920 | { |
ffe743ca | 12921 | std::vector<unsigned long>::const_iterator pi; |
12922 | if (this->indexes_ != NULL) | |
12923 | pi = this->indexes_->begin(); | |
e32de7ba | 12924 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12925 | pv != this->vals()->end(); | |
e440a328 | 12926 | ++pv) |
12927 | { | |
12928 | exp->write_c_string(", "); | |
ffe743ca | 12929 | |
12930 | if (this->indexes_ != NULL) | |
12931 | { | |
12932 | char buf[100]; | |
12933 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12934 | exp->write_c_string(buf); | |
12935 | exp->write_c_string(":"); | |
12936 | } | |
12937 | ||
e440a328 | 12938 | if (*pv != NULL) |
12939 | (*pv)->export_expression(exp); | |
ffe743ca | 12940 | |
12941 | if (this->indexes_ != NULL) | |
12942 | ++pi; | |
e440a328 | 12943 | } |
12944 | } | |
12945 | exp->write_c_string(")"); | |
12946 | } | |
12947 | ||
0e9a2e72 | 12948 | // Dump ast representation of an array construction expression. |
d751bb78 | 12949 | |
12950 | void | |
12951 | Array_construction_expression::do_dump_expression( | |
12952 | Ast_dump_context* ast_dump_context) const | |
12953 | { | |
ffe743ca | 12954 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12955 | |
12956 | ast_dump_context->ostream() << "[" ; | |
12957 | if (length != NULL) | |
12958 | { | |
12959 | ast_dump_context->dump_expression(length); | |
12960 | } | |
12961 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12962 | ast_dump_context->dump_type(this->type_); |
0e9a2e72 | 12963 | this->dump_slice_storage_expression(ast_dump_context); |
d751bb78 | 12964 | ast_dump_context->ostream() << "{" ; |
ffe743ca | 12965 | if (this->indexes_ == NULL) |
e32de7ba | 12966 | ast_dump_context->dump_expression_list(this->vals()); |
ffe743ca | 12967 | else |
12968 | { | |
e32de7ba | 12969 | Expression_list::const_iterator pv = this->vals()->begin(); |
ffe743ca | 12970 | for (std::vector<unsigned long>::const_iterator pi = |
12971 | this->indexes_->begin(); | |
12972 | pi != this->indexes_->end(); | |
12973 | ++pi, ++pv) | |
12974 | { | |
12975 | if (pi != this->indexes_->begin()) | |
12976 | ast_dump_context->ostream() << ", "; | |
12977 | ast_dump_context->ostream() << *pi << ':'; | |
12978 | ast_dump_context->dump_expression(*pv); | |
12979 | } | |
12980 | } | |
d751bb78 | 12981 | ast_dump_context->ostream() << "}" ; |
12982 | ||
12983 | } | |
12984 | ||
da244e59 | 12985 | // Class Fixed_array_construction_expression. |
e440a328 | 12986 | |
da244e59 | 12987 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
12988 | Type* type, const std::vector<unsigned long>* indexes, | |
12989 | Expression_list* vals, Location location) | |
12990 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
12991 | type, indexes, vals, location) | |
12992 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12993 | |
ea664253 | 12994 | // Return the backend representation for constructing a fixed array. |
e440a328 | 12995 | |
ea664253 | 12996 | Bexpression* |
12997 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12998 | { |
9f0e0513 | 12999 | Type* type = this->type(); |
13000 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 13001 | return this->get_constructor(context, btype); |
e440a328 | 13002 | } |
13003 | ||
76f85fd6 | 13004 | Expression* |
13005 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
13006 | Location location) | |
13007 | { | |
13008 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
13009 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
13010 | } | |
13011 | ||
da244e59 | 13012 | // Class Slice_construction_expression. |
e440a328 | 13013 | |
da244e59 | 13014 | Slice_construction_expression::Slice_construction_expression( |
13015 | Type* type, const std::vector<unsigned long>* indexes, | |
13016 | Expression_list* vals, Location location) | |
13017 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
13018 | type, indexes, vals, location), | |
0e9a2e72 | 13019 | valtype_(NULL), array_val_(NULL), slice_storage_(NULL), |
13020 | storage_escapes_(true) | |
e440a328 | 13021 | { |
da244e59 | 13022 | go_assert(type->is_slice_type()); |
23d77f91 | 13023 | |
da244e59 | 13024 | unsigned long lenval; |
13025 | Expression* length; | |
13026 | if (vals == NULL || vals->empty()) | |
13027 | lenval = 0; | |
13028 | else | |
13029 | { | |
13030 | if (this->indexes() == NULL) | |
13031 | lenval = vals->size(); | |
13032 | else | |
13033 | lenval = indexes->back() + 1; | |
13034 | } | |
13035 | Type* int_type = Type::lookup_integer_type("int"); | |
13036 | length = Expression::make_integer_ul(lenval, int_type, location); | |
13037 | Type* element_type = type->array_type()->element_type(); | |
6bf4793c | 13038 | Array_type* array_type = Type::make_array_type(element_type, length); |
13039 | array_type->set_is_array_incomparable(); | |
13040 | this->valtype_ = array_type; | |
da244e59 | 13041 | } |
e440a328 | 13042 | |
23d77f91 | 13043 | // Traversal. |
13044 | ||
13045 | int | |
13046 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
13047 | { | |
13048 | if (this->Array_construction_expression::do_traverse(traverse) | |
13049 | == TRAVERSE_EXIT) | |
13050 | return TRAVERSE_EXIT; | |
13051 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
13052 | return TRAVERSE_EXIT; | |
0e9a2e72 | 13053 | if (this->array_val_ != NULL |
13054 | && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) | |
13055 | return TRAVERSE_EXIT; | |
13056 | if (this->slice_storage_ != NULL | |
13057 | && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) | |
13058 | return TRAVERSE_EXIT; | |
23d77f91 | 13059 | return TRAVERSE_CONTINUE; |
13060 | } | |
13061 | ||
0e9a2e72 | 13062 | // Helper routine to create fixed array value underlying the slice literal. |
13063 | // May be called during flattening, or later during do_get_backend(). | |
e440a328 | 13064 | |
0e9a2e72 | 13065 | Expression* |
13066 | Slice_construction_expression::create_array_val() | |
e440a328 | 13067 | { |
f9c68f17 | 13068 | Array_type* array_type = this->type()->array_type(); |
13069 | if (array_type == NULL) | |
13070 | { | |
c484d925 | 13071 | go_assert(this->type()->is_error()); |
0e9a2e72 | 13072 | return NULL; |
f9c68f17 | 13073 | } |
13074 | ||
f23d7786 | 13075 | Location loc = this->location(); |
23d77f91 | 13076 | go_assert(this->valtype_ != NULL); |
3d60812e | 13077 | |
f23d7786 | 13078 | Expression_list* vals = this->vals(); |
0e9a2e72 | 13079 | return new Fixed_array_construction_expression( |
13080 | this->valtype_, this->indexes(), vals, loc); | |
13081 | } | |
13082 | ||
13083 | // If we're previous established that the slice storage does not | |
13084 | // escape, then create a separate array temp val here for it. We | |
13085 | // need to do this as part of flattening so as to be able to insert | |
13086 | // the new temp statement. | |
13087 | ||
13088 | Expression* | |
13089 | Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no, | |
13090 | Statement_inserter* inserter) | |
13091 | { | |
13092 | if (this->type()->array_type() == NULL) | |
13093 | return NULL; | |
13094 | ||
13095 | // Base class flattening first | |
13096 | this->Array_construction_expression::do_flatten(gogo, no, inserter); | |
13097 | ||
a1bbc2c3 | 13098 | // Create a stack-allocated storage temp if storage won't escape |
03118c21 | 13099 | if (!this->storage_escapes_ |
13100 | && this->slice_storage_ == NULL | |
13101 | && this->element_count() > 0) | |
0e9a2e72 | 13102 | { |
13103 | Location loc = this->location(); | |
03118c21 | 13104 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13105 | go_assert(this->array_val_); |
13106 | Temporary_statement* temp = | |
13107 | Statement::make_temporary(this->valtype_, this->array_val_, loc); | |
13108 | inserter->insert(temp); | |
13109 | this->slice_storage_ = Expression::make_temporary_reference(temp, loc); | |
13110 | } | |
13111 | return this; | |
13112 | } | |
13113 | ||
13114 | // When dumping a slice construction expression that has an explicit | |
13115 | // storeage temp, emit the temp here (if we don't do this the storage | |
13116 | // temp appears unused in the AST dump). | |
13117 | ||
13118 | void | |
13119 | Slice_construction_expression:: | |
13120 | dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const | |
13121 | { | |
13122 | if (this->slice_storage_ == NULL) | |
13123 | return; | |
13124 | ast_dump_context->ostream() << "storage=" ; | |
13125 | ast_dump_context->dump_expression(this->slice_storage_); | |
13126 | } | |
13127 | ||
13128 | // Return the backend representation for constructing a slice. | |
13129 | ||
13130 | Bexpression* | |
13131 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
13132 | { | |
13133 | if (this->array_val_ == NULL) | |
03118c21 | 13134 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13135 | if (this->array_val_ == NULL) |
13136 | { | |
13137 | go_assert(this->type()->is_error()); | |
13138 | return context->backend()->error_expression(); | |
13139 | } | |
13140 | ||
13141 | Location loc = this->location(); | |
e440a328 | 13142 | |
3ae06f68 | 13143 | bool is_static_initializer = this->array_val_->is_static_initializer(); |
d8829beb | 13144 | |
13145 | // We have to copy the initial values into heap memory if we are in | |
3ae06f68 | 13146 | // a function or if the values are not constants. |
13147 | bool copy_to_heap = context->function() != NULL || !is_static_initializer; | |
e440a328 | 13148 | |
f23d7786 | 13149 | Expression* space; |
0e9a2e72 | 13150 | |
13151 | if (this->slice_storage_ != NULL) | |
13152 | { | |
13153 | go_assert(!this->storage_escapes_); | |
13154 | space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); | |
13155 | } | |
13156 | else if (!copy_to_heap) | |
e440a328 | 13157 | { |
f23d7786 | 13158 | // The initializer will only run once. |
0e9a2e72 | 13159 | space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); |
f23d7786 | 13160 | space->unary_expression()->set_is_slice_init(); |
e440a328 | 13161 | } |
13162 | else | |
45ff893b | 13163 | { |
0e9a2e72 | 13164 | space = Expression::make_heap_expression(this->array_val_, loc); |
45ff893b | 13165 | Node* n = Node::make_node(this); |
13166 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
13167 | { | |
13168 | n = Node::make_node(space); | |
13169 | n->set_encoding(Node::ESCAPE_NONE); | |
13170 | } | |
13171 | } | |
e440a328 | 13172 | |
2c809f8f | 13173 | // Build a constructor for the slice. |
f23d7786 | 13174 | Expression* len = this->valtype_->array_type()->length(); |
13175 | Expression* slice_val = | |
13176 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 13177 | return slice_val->get_backend(context); |
e440a328 | 13178 | } |
13179 | ||
13180 | // Make a slice composite literal. This is used by the type | |
13181 | // descriptor code. | |
13182 | ||
0e9a2e72 | 13183 | Slice_construction_expression* |
e440a328 | 13184 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, |
b13c66cd | 13185 | Location location) |
e440a328 | 13186 | { |
411eb89e | 13187 | go_assert(type->is_slice_type()); |
2c809f8f | 13188 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 13189 | } |
13190 | ||
da244e59 | 13191 | // Class Map_construction_expression. |
e440a328 | 13192 | |
13193 | // Traversal. | |
13194 | ||
13195 | int | |
13196 | Map_construction_expression::do_traverse(Traverse* traverse) | |
13197 | { | |
13198 | if (this->vals_ != NULL | |
13199 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
13200 | return TRAVERSE_EXIT; | |
13201 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13202 | return TRAVERSE_EXIT; | |
13203 | return TRAVERSE_CONTINUE; | |
13204 | } | |
13205 | ||
2c809f8f | 13206 | // Flatten constructor initializer into a temporary variable since |
13207 | // we need to take its address for __go_construct_map. | |
13208 | ||
13209 | Expression* | |
13210 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
13211 | Statement_inserter* inserter) | |
13212 | { | |
13213 | if (!this->is_error_expression() | |
13214 | && this->vals_ != NULL | |
13215 | && !this->vals_->empty() | |
13216 | && this->constructor_temp_ == NULL) | |
13217 | { | |
13218 | Map_type* mt = this->type_->map_type(); | |
13219 | Type* key_type = mt->key_type(); | |
13220 | Type* val_type = mt->val_type(); | |
13221 | this->element_type_ = Type::make_builtin_struct_type(2, | |
13222 | "__key", key_type, | |
13223 | "__val", val_type); | |
13224 | ||
13225 | Expression_list* value_pairs = new Expression_list(); | |
13226 | Location loc = this->location(); | |
13227 | ||
13228 | size_t i = 0; | |
13229 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13230 | pv != this->vals_->end(); | |
13231 | ++pv, ++i) | |
13232 | { | |
13233 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 13234 | Expression* key = *pv; |
5bf8be8b | 13235 | if (key->is_error_expression() || key->type()->is_error_type()) |
13236 | { | |
13237 | go_assert(saw_errors()); | |
13238 | return Expression::make_error(loc); | |
13239 | } | |
91c0fd76 | 13240 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
13241 | { | |
13242 | Temporary_statement* temp = | |
13243 | Statement::make_temporary(NULL, key, loc); | |
13244 | inserter->insert(temp); | |
13245 | key = Expression::make_temporary_reference(temp, loc); | |
13246 | } | |
13247 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 13248 | |
13249 | ++pv; | |
91c0fd76 | 13250 | Expression* val = *pv; |
5bf8be8b | 13251 | if (val->is_error_expression() || val->type()->is_error_type()) |
13252 | { | |
13253 | go_assert(saw_errors()); | |
13254 | return Expression::make_error(loc); | |
13255 | } | |
91c0fd76 | 13256 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
13257 | { | |
13258 | Temporary_statement* temp = | |
13259 | Statement::make_temporary(NULL, val, loc); | |
13260 | inserter->insert(temp); | |
13261 | val = Expression::make_temporary_reference(temp, loc); | |
13262 | } | |
13263 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 13264 | |
13265 | key_value_pair->push_back(key); | |
13266 | key_value_pair->push_back(val); | |
13267 | value_pairs->push_back( | |
13268 | Expression::make_struct_composite_literal(this->element_type_, | |
13269 | key_value_pair, loc)); | |
13270 | } | |
13271 | ||
e67508fa | 13272 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
6bf4793c | 13273 | Array_type* ctor_type = |
2c809f8f | 13274 | Type::make_array_type(this->element_type_, element_count); |
6bf4793c | 13275 | ctor_type->set_is_array_incomparable(); |
2c809f8f | 13276 | Expression* constructor = |
13277 | new Fixed_array_construction_expression(ctor_type, NULL, | |
13278 | value_pairs, loc); | |
13279 | ||
13280 | this->constructor_temp_ = | |
13281 | Statement::make_temporary(NULL, constructor, loc); | |
13282 | constructor->issue_nil_check(); | |
13283 | this->constructor_temp_->set_is_address_taken(); | |
13284 | inserter->insert(this->constructor_temp_); | |
13285 | } | |
13286 | ||
13287 | return this; | |
13288 | } | |
13289 | ||
e440a328 | 13290 | // Final type determination. |
13291 | ||
13292 | void | |
13293 | Map_construction_expression::do_determine_type(const Type_context*) | |
13294 | { | |
13295 | if (this->vals_ == NULL) | |
13296 | return; | |
13297 | ||
13298 | Map_type* mt = this->type_->map_type(); | |
13299 | Type_context key_context(mt->key_type(), false); | |
13300 | Type_context val_context(mt->val_type(), false); | |
13301 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13302 | pv != this->vals_->end(); | |
13303 | ++pv) | |
13304 | { | |
13305 | (*pv)->determine_type(&key_context); | |
13306 | ++pv; | |
13307 | (*pv)->determine_type(&val_context); | |
13308 | } | |
13309 | } | |
13310 | ||
13311 | // Check types. | |
13312 | ||
13313 | void | |
13314 | Map_construction_expression::do_check_types(Gogo*) | |
13315 | { | |
13316 | if (this->vals_ == NULL) | |
13317 | return; | |
13318 | ||
13319 | Map_type* mt = this->type_->map_type(); | |
13320 | int i = 0; | |
13321 | Type* key_type = mt->key_type(); | |
13322 | Type* val_type = mt->val_type(); | |
13323 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13324 | pv != this->vals_->end(); | |
13325 | ++pv, ++i) | |
13326 | { | |
13327 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
13328 | { | |
631d5788 | 13329 | go_error_at((*pv)->location(), |
13330 | "incompatible type for element %d key in map construction", | |
13331 | i + 1); | |
e440a328 | 13332 | this->set_is_error(); |
13333 | } | |
13334 | ++pv; | |
13335 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
13336 | { | |
631d5788 | 13337 | go_error_at((*pv)->location(), |
13338 | ("incompatible type for element %d value " | |
13339 | "in map construction"), | |
e440a328 | 13340 | i + 1); |
13341 | this->set_is_error(); | |
13342 | } | |
13343 | } | |
13344 | } | |
13345 | ||
ea664253 | 13346 | // Return the backend representation for constructing a map. |
e440a328 | 13347 | |
ea664253 | 13348 | Bexpression* |
13349 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13350 | { |
2c809f8f | 13351 | if (this->is_error_expression()) |
ea664253 | 13352 | return context->backend()->error_expression(); |
2c809f8f | 13353 | Location loc = this->location(); |
e440a328 | 13354 | |
e440a328 | 13355 | size_t i = 0; |
2c809f8f | 13356 | Expression* ventries; |
e440a328 | 13357 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 13358 | ventries = Expression::make_nil(loc); |
e440a328 | 13359 | else |
13360 | { | |
2c809f8f | 13361 | go_assert(this->constructor_temp_ != NULL); |
13362 | i = this->vals_->size() / 2; | |
e440a328 | 13363 | |
2c809f8f | 13364 | Expression* ctor_ref = |
13365 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
13366 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
13367 | } | |
e440a328 | 13368 | |
2c809f8f | 13369 | Map_type* mt = this->type_->map_type(); |
13370 | if (this->element_type_ == NULL) | |
13371 | this->element_type_ = | |
13372 | Type::make_builtin_struct_type(2, | |
13373 | "__key", mt->key_type(), | |
13374 | "__val", mt->val_type()); | |
0d5530d9 | 13375 | Expression* descriptor = Expression::make_type_descriptor(mt, loc); |
2c809f8f | 13376 | |
13377 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 13378 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 13379 | |
13380 | Expression* entry_size = | |
13381 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
13382 | ||
13383 | unsigned int field_index; | |
13384 | const Struct_field* valfield = | |
13385 | this->element_type_->find_local_field("__val", &field_index); | |
13386 | Expression* val_offset = | |
13387 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
2c809f8f | 13388 | |
13389 | Expression* map_ctor = | |
0d5530d9 | 13390 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, |
13391 | entry_size, val_offset, ventries); | |
ea664253 | 13392 | return map_ctor->get_backend(context); |
2c809f8f | 13393 | } |
e440a328 | 13394 | |
2c809f8f | 13395 | // Export an array construction. |
e440a328 | 13396 | |
2c809f8f | 13397 | void |
13398 | Map_construction_expression::do_export(Export* exp) const | |
13399 | { | |
13400 | exp->write_c_string("convert("); | |
13401 | exp->write_type(this->type_); | |
13402 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13403 | pv != this->vals_->end(); | |
13404 | ++pv) | |
13405 | { | |
13406 | exp->write_c_string(", "); | |
13407 | (*pv)->export_expression(exp); | |
13408 | } | |
13409 | exp->write_c_string(")"); | |
13410 | } | |
e440a328 | 13411 | |
2c809f8f | 13412 | // Dump ast representation for a map construction expression. |
d751bb78 | 13413 | |
13414 | void | |
13415 | Map_construction_expression::do_dump_expression( | |
13416 | Ast_dump_context* ast_dump_context) const | |
13417 | { | |
d751bb78 | 13418 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 13419 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13420 | ast_dump_context->ostream() << "}"; |
13421 | } | |
13422 | ||
7795ac51 | 13423 | // Class Composite_literal_expression. |
e440a328 | 13424 | |
13425 | // Traversal. | |
13426 | ||
13427 | int | |
13428 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13429 | { | |
dbffccfc | 13430 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 13431 | return TRAVERSE_EXIT; |
dbffccfc | 13432 | |
13433 | // If this is a struct composite literal with keys, then the keys | |
13434 | // are field names, not expressions. We don't want to traverse them | |
13435 | // in that case. If we do, we can give an erroneous error "variable | |
13436 | // initializer refers to itself." See bug482.go in the testsuite. | |
13437 | if (this->has_keys_ && this->vals_ != NULL) | |
13438 | { | |
13439 | // The type may not be resolvable at this point. | |
13440 | Type* type = this->type_; | |
a01f2481 | 13441 | |
7795ac51 | 13442 | for (int depth = 0; depth < this->depth_; ++depth) |
a01f2481 | 13443 | { |
13444 | if (type->array_type() != NULL) | |
13445 | type = type->array_type()->element_type(); | |
13446 | else if (type->map_type() != NULL) | |
7795ac51 | 13447 | { |
13448 | if (this->key_path_[depth]) | |
13449 | type = type->map_type()->key_type(); | |
13450 | else | |
13451 | type = type->map_type()->val_type(); | |
13452 | } | |
a01f2481 | 13453 | else |
13454 | { | |
13455 | // This error will be reported during lowering. | |
13456 | return TRAVERSE_CONTINUE; | |
13457 | } | |
13458 | } | |
13459 | ||
dbffccfc | 13460 | while (true) |
13461 | { | |
13462 | if (type->classification() == Type::TYPE_NAMED) | |
13463 | type = type->named_type()->real_type(); | |
13464 | else if (type->classification() == Type::TYPE_FORWARD) | |
13465 | { | |
13466 | Type* t = type->forwarded(); | |
13467 | if (t == type) | |
13468 | break; | |
13469 | type = t; | |
13470 | } | |
13471 | else | |
13472 | break; | |
13473 | } | |
13474 | ||
13475 | if (type->classification() == Type::TYPE_STRUCT) | |
13476 | { | |
13477 | Expression_list::iterator p = this->vals_->begin(); | |
13478 | while (p != this->vals_->end()) | |
13479 | { | |
13480 | // Skip key. | |
13481 | ++p; | |
13482 | go_assert(p != this->vals_->end()); | |
13483 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13484 | return TRAVERSE_EXIT; | |
13485 | ++p; | |
13486 | } | |
13487 | return TRAVERSE_CONTINUE; | |
13488 | } | |
13489 | } | |
13490 | ||
13491 | if (this->vals_ != NULL) | |
13492 | return this->vals_->traverse(traverse); | |
13493 | ||
13494 | return TRAVERSE_CONTINUE; | |
e440a328 | 13495 | } |
13496 | ||
13497 | // Lower a generic composite literal into a specific version based on | |
13498 | // the type. | |
13499 | ||
13500 | Expression* | |
ceeb4318 | 13501 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13502 | Statement_inserter* inserter, int) | |
e440a328 | 13503 | { |
13504 | Type* type = this->type_; | |
13505 | ||
7795ac51 | 13506 | for (int depth = 0; depth < this->depth_; ++depth) |
e440a328 | 13507 | { |
13508 | if (type->array_type() != NULL) | |
13509 | type = type->array_type()->element_type(); | |
13510 | else if (type->map_type() != NULL) | |
7795ac51 | 13511 | { |
13512 | if (this->key_path_[depth]) | |
13513 | type = type->map_type()->key_type(); | |
13514 | else | |
13515 | type = type->map_type()->val_type(); | |
13516 | } | |
e440a328 | 13517 | else |
13518 | { | |
5c13bd80 | 13519 | if (!type->is_error()) |
631d5788 | 13520 | go_error_at(this->location(), |
13521 | ("may only omit types within composite literals " | |
13522 | "of slice, array, or map type")); | |
e440a328 | 13523 | return Expression::make_error(this->location()); |
13524 | } | |
13525 | } | |
13526 | ||
e00772b3 | 13527 | Type *pt = type->points_to(); |
13528 | bool is_pointer = false; | |
13529 | if (pt != NULL) | |
13530 | { | |
13531 | is_pointer = true; | |
13532 | type = pt; | |
13533 | } | |
13534 | ||
13535 | Expression* ret; | |
5c13bd80 | 13536 | if (type->is_error()) |
e440a328 | 13537 | return Expression::make_error(this->location()); |
13538 | else if (type->struct_type() != NULL) | |
e00772b3 | 13539 | ret = this->lower_struct(gogo, type); |
e440a328 | 13540 | else if (type->array_type() != NULL) |
113ef6a5 | 13541 | ret = this->lower_array(type); |
e440a328 | 13542 | else if (type->map_type() != NULL) |
e00772b3 | 13543 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13544 | else |
13545 | { | |
631d5788 | 13546 | go_error_at(this->location(), |
13547 | ("expected struct, slice, array, or map type " | |
13548 | "for composite literal")); | |
e440a328 | 13549 | return Expression::make_error(this->location()); |
13550 | } | |
e00772b3 | 13551 | |
13552 | if (is_pointer) | |
2c809f8f | 13553 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13554 | |
13555 | return ret; | |
e440a328 | 13556 | } |
13557 | ||
13558 | // Lower a struct composite literal. | |
13559 | ||
13560 | Expression* | |
81c4b26b | 13561 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13562 | { |
b13c66cd | 13563 | Location location = this->location(); |
e440a328 | 13564 | Struct_type* st = type->struct_type(); |
13565 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13566 | { |
e6013c28 | 13567 | if (this->vals_ != NULL |
13568 | && !this->vals_->empty() | |
13569 | && type->named_type() != NULL | |
13570 | && type->named_type()->named_object()->package() != NULL) | |
13571 | { | |
13572 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13573 | pf != st->fields()->end(); | |
13574 | ++pf) | |
07daa4e7 | 13575 | { |
07ba7f26 | 13576 | if (Gogo::is_hidden_name(pf->field_name()) |
13577 | || pf->is_embedded_builtin(gogo)) | |
631d5788 | 13578 | go_error_at(this->location(), |
13579 | "assignment of unexported field %qs in %qs literal", | |
13580 | Gogo::message_name(pf->field_name()).c_str(), | |
13581 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13582 | } |
13583 | } | |
13584 | ||
13585 | return new Struct_construction_expression(type, this->vals_, location); | |
13586 | } | |
e440a328 | 13587 | |
13588 | size_t field_count = st->field_count(); | |
13589 | std::vector<Expression*> vals(field_count); | |
e32de7ba | 13590 | std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>); |
e440a328 | 13591 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13592 | Expression* external_expr = NULL; |
13593 | const Named_object* external_no = NULL; | |
e440a328 | 13594 | while (p != this->vals_->end()) |
13595 | { | |
13596 | Expression* name_expr = *p; | |
13597 | ||
13598 | ++p; | |
c484d925 | 13599 | go_assert(p != this->vals_->end()); |
e440a328 | 13600 | Expression* val = *p; |
13601 | ||
13602 | ++p; | |
13603 | ||
13604 | if (name_expr == NULL) | |
13605 | { | |
631d5788 | 13606 | go_error_at(val->location(), |
13607 | "mixture of field and value initializers"); | |
e440a328 | 13608 | return Expression::make_error(location); |
13609 | } | |
13610 | ||
13611 | bool bad_key = false; | |
13612 | std::string name; | |
81c4b26b | 13613 | const Named_object* no = NULL; |
e440a328 | 13614 | switch (name_expr->classification()) |
13615 | { | |
13616 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13617 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 13618 | if (type->named_type() != NULL) |
13619 | { | |
13620 | // If the named object found for this field name comes from a | |
13621 | // different package than the struct it is a part of, do not count | |
13622 | // this incorrect lookup as a usage of the object's package. | |
13623 | no = name_expr->unknown_expression()->named_object(); | |
13624 | if (no->package() != NULL | |
13625 | && no->package() != type->named_type()->named_object()->package()) | |
13626 | no->package()->forget_usage(name_expr); | |
13627 | } | |
e440a328 | 13628 | break; |
13629 | ||
13630 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13631 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13632 | break; |
13633 | ||
13634 | case EXPRESSION_TYPE: | |
13635 | { | |
13636 | Type* t = name_expr->type(); | |
13637 | Named_type* nt = t->named_type(); | |
13638 | if (nt == NULL) | |
13639 | bad_key = true; | |
13640 | else | |
81c4b26b | 13641 | no = nt->named_object(); |
e440a328 | 13642 | } |
13643 | break; | |
13644 | ||
13645 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13646 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13647 | break; |
13648 | ||
b0c09712 | 13649 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
13650 | no = name_expr->enclosed_var_expression()->variable(); | |
e440a328 | 13651 | break; |
13652 | ||
b0c09712 | 13653 | case EXPRESSION_FUNC_REFERENCE: |
13654 | no = name_expr->func_expression()->named_object(); | |
e440a328 | 13655 | break; |
13656 | ||
13657 | default: | |
13658 | bad_key = true; | |
13659 | break; | |
13660 | } | |
13661 | if (bad_key) | |
13662 | { | |
631d5788 | 13663 | go_error_at(name_expr->location(), "expected struct field name"); |
e440a328 | 13664 | return Expression::make_error(location); |
13665 | } | |
13666 | ||
81c4b26b | 13667 | if (no != NULL) |
13668 | { | |
62750cd5 | 13669 | if (no->package() != NULL && external_expr == NULL) |
13670 | { | |
13671 | external_expr = name_expr; | |
13672 | external_no = no; | |
13673 | } | |
13674 | ||
81c4b26b | 13675 | name = no->name(); |
13676 | ||
13677 | // A predefined name won't be packed. If it starts with a | |
13678 | // lower case letter we need to check for that case, because | |
2d29d278 | 13679 | // the field name will be packed. FIXME. |
81c4b26b | 13680 | if (!Gogo::is_hidden_name(name) |
13681 | && name[0] >= 'a' | |
13682 | && name[0] <= 'z') | |
13683 | { | |
13684 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13685 | if (gno == no) | |
13686 | name = gogo->pack_hidden_name(name, false); | |
13687 | } | |
13688 | } | |
13689 | ||
e440a328 | 13690 | unsigned int index; |
13691 | const Struct_field* sf = st->find_local_field(name, &index); | |
13692 | if (sf == NULL) | |
13693 | { | |
631d5788 | 13694 | go_error_at(name_expr->location(), "unknown field %qs in %qs", |
13695 | Gogo::message_name(name).c_str(), | |
13696 | (type->named_type() != NULL | |
13697 | ? type->named_type()->message_name().c_str() | |
13698 | : "unnamed struct")); | |
e440a328 | 13699 | return Expression::make_error(location); |
13700 | } | |
13701 | if (vals[index] != NULL) | |
13702 | { | |
631d5788 | 13703 | go_error_at(name_expr->location(), |
13704 | "duplicate value for field %qs in %qs", | |
13705 | Gogo::message_name(name).c_str(), | |
13706 | (type->named_type() != NULL | |
13707 | ? type->named_type()->message_name().c_str() | |
13708 | : "unnamed struct")); | |
e440a328 | 13709 | return Expression::make_error(location); |
13710 | } | |
13711 | ||
07daa4e7 | 13712 | if (type->named_type() != NULL |
13713 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 13714 | && (Gogo::is_hidden_name(sf->field_name()) |
13715 | || sf->is_embedded_builtin(gogo))) | |
631d5788 | 13716 | go_error_at(name_expr->location(), |
13717 | "assignment of unexported field %qs in %qs literal", | |
13718 | Gogo::message_name(sf->field_name()).c_str(), | |
13719 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13720 | |
e440a328 | 13721 | vals[index] = val; |
e32de7ba | 13722 | traverse_order->push_back(static_cast<unsigned long>(index)); |
e440a328 | 13723 | } |
13724 | ||
62750cd5 | 13725 | if (!this->all_are_names_) |
13726 | { | |
13727 | // This is a weird case like bug462 in the testsuite. | |
13728 | if (external_expr == NULL) | |
631d5788 | 13729 | go_error_at(this->location(), "unknown field in %qs literal", |
13730 | (type->named_type() != NULL | |
13731 | ? type->named_type()->message_name().c_str() | |
13732 | : "unnamed struct")); | |
62750cd5 | 13733 | else |
631d5788 | 13734 | go_error_at(external_expr->location(), "unknown field %qs in %qs", |
13735 | external_no->message_name().c_str(), | |
13736 | (type->named_type() != NULL | |
13737 | ? type->named_type()->message_name().c_str() | |
13738 | : "unnamed struct")); | |
62750cd5 | 13739 | return Expression::make_error(location); |
13740 | } | |
13741 | ||
e440a328 | 13742 | Expression_list* list = new Expression_list; |
13743 | list->reserve(field_count); | |
13744 | for (size_t i = 0; i < field_count; ++i) | |
13745 | list->push_back(vals[i]); | |
13746 | ||
0c4f5a19 | 13747 | Struct_construction_expression* ret = |
13748 | new Struct_construction_expression(type, list, location); | |
13749 | ret->set_traverse_order(traverse_order); | |
13750 | return ret; | |
e440a328 | 13751 | } |
13752 | ||
e32de7ba | 13753 | // Index/value/traversal-order triple. |
00773463 | 13754 | |
e32de7ba | 13755 | struct IVT_triple { |
13756 | unsigned long index; | |
13757 | unsigned long traversal_order; | |
13758 | Expression* expr; | |
13759 | IVT_triple(unsigned long i, unsigned long to, Expression *e) | |
13760 | : index(i), traversal_order(to), expr(e) { } | |
13761 | bool operator<(const IVT_triple& other) const | |
13762 | { return this->index < other.index; } | |
00773463 | 13763 | }; |
13764 | ||
e440a328 | 13765 | // Lower an array composite literal. |
13766 | ||
13767 | Expression* | |
113ef6a5 | 13768 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13769 | { |
b13c66cd | 13770 | Location location = this->location(); |
e440a328 | 13771 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13772 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13773 | |
ffe743ca | 13774 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13775 | indexes->reserve(this->vals_->size()); | |
00773463 | 13776 | bool indexes_out_of_order = false; |
ffe743ca | 13777 | Expression_list* vals = new Expression_list(); |
13778 | vals->reserve(this->vals_->size()); | |
e440a328 | 13779 | unsigned long index = 0; |
13780 | Expression_list::const_iterator p = this->vals_->begin(); | |
13781 | while (p != this->vals_->end()) | |
13782 | { | |
13783 | Expression* index_expr = *p; | |
13784 | ||
13785 | ++p; | |
c484d925 | 13786 | go_assert(p != this->vals_->end()); |
e440a328 | 13787 | Expression* val = *p; |
13788 | ||
13789 | ++p; | |
13790 | ||
ffe743ca | 13791 | if (index_expr == NULL) |
13792 | { | |
13793 | if (!indexes->empty()) | |
13794 | indexes->push_back(index); | |
13795 | } | |
13796 | else | |
e440a328 | 13797 | { |
ffe743ca | 13798 | if (indexes->empty() && !vals->empty()) |
13799 | { | |
13800 | for (size_t i = 0; i < vals->size(); ++i) | |
13801 | indexes->push_back(i); | |
13802 | } | |
13803 | ||
0c77715b | 13804 | Numeric_constant nc; |
13805 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13806 | { |
631d5788 | 13807 | go_error_at(index_expr->location(), |
13808 | "index expression is not integer constant"); | |
e440a328 | 13809 | return Expression::make_error(location); |
13810 | } | |
6f6d9955 | 13811 | |
0c77715b | 13812 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13813 | { |
0c77715b | 13814 | case Numeric_constant::NC_UL_VALID: |
13815 | break; | |
13816 | case Numeric_constant::NC_UL_NOTINT: | |
631d5788 | 13817 | go_error_at(index_expr->location(), |
13818 | "index expression is not integer constant"); | |
0c77715b | 13819 | return Expression::make_error(location); |
13820 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 13821 | go_error_at(index_expr->location(), |
13822 | "index expression is negative"); | |
e440a328 | 13823 | return Expression::make_error(location); |
0c77715b | 13824 | case Numeric_constant::NC_UL_BIG: |
631d5788 | 13825 | go_error_at(index_expr->location(), "index value overflow"); |
e440a328 | 13826 | return Expression::make_error(location); |
0c77715b | 13827 | default: |
13828 | go_unreachable(); | |
e440a328 | 13829 | } |
6f6d9955 | 13830 | |
13831 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13832 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13833 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13834 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13835 | { |
631d5788 | 13836 | go_error_at(index_expr->location(), "index value overflow"); |
6f6d9955 | 13837 | return Expression::make_error(location); |
13838 | } | |
13839 | ||
ffe743ca | 13840 | if (std::find(indexes->begin(), indexes->end(), index) |
13841 | != indexes->end()) | |
e440a328 | 13842 | { |
631d5788 | 13843 | go_error_at(index_expr->location(), |
13844 | "duplicate value for index %lu", | |
13845 | index); | |
e440a328 | 13846 | return Expression::make_error(location); |
13847 | } | |
ffe743ca | 13848 | |
00773463 | 13849 | if (!indexes->empty() && index < indexes->back()) |
13850 | indexes_out_of_order = true; | |
13851 | ||
ffe743ca | 13852 | indexes->push_back(index); |
e440a328 | 13853 | } |
13854 | ||
ffe743ca | 13855 | vals->push_back(val); |
13856 | ||
e440a328 | 13857 | ++index; |
13858 | } | |
13859 | ||
ffe743ca | 13860 | if (indexes->empty()) |
13861 | { | |
13862 | delete indexes; | |
13863 | indexes = NULL; | |
13864 | } | |
e440a328 | 13865 | |
e32de7ba | 13866 | std::vector<unsigned long>* traverse_order = NULL; |
00773463 | 13867 | if (indexes_out_of_order) |
13868 | { | |
e32de7ba | 13869 | typedef std::vector<IVT_triple> V; |
00773463 | 13870 | |
13871 | V v; | |
13872 | v.reserve(indexes->size()); | |
13873 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
e32de7ba | 13874 | unsigned long torder = 0; |
00773463 | 13875 | for (Expression_list::const_iterator pe = vals->begin(); |
13876 | pe != vals->end(); | |
e32de7ba | 13877 | ++pe, ++pi, ++torder) |
13878 | v.push_back(IVT_triple(*pi, torder, *pe)); | |
00773463 | 13879 | |
e32de7ba | 13880 | std::sort(v.begin(), v.end()); |
00773463 | 13881 | |
13882 | delete indexes; | |
13883 | delete vals; | |
e32de7ba | 13884 | |
00773463 | 13885 | indexes = new std::vector<unsigned long>(); |
13886 | indexes->reserve(v.size()); | |
13887 | vals = new Expression_list(); | |
13888 | vals->reserve(v.size()); | |
e32de7ba | 13889 | traverse_order = new std::vector<unsigned long>(); |
13890 | traverse_order->reserve(v.size()); | |
00773463 | 13891 | |
13892 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13893 | { | |
e32de7ba | 13894 | indexes->push_back(p->index); |
13895 | vals->push_back(p->expr); | |
13896 | traverse_order->push_back(p->traversal_order); | |
00773463 | 13897 | } |
13898 | } | |
13899 | ||
e32de7ba | 13900 | Expression* ret = this->make_array(type, indexes, vals); |
13901 | Array_construction_expression* ace = ret->array_literal(); | |
13902 | if (ace != NULL && traverse_order != NULL) | |
13903 | ace->set_traverse_order(traverse_order); | |
13904 | return ret; | |
e440a328 | 13905 | } |
13906 | ||
13907 | // Actually build the array composite literal. This handles | |
13908 | // [...]{...}. | |
13909 | ||
13910 | Expression* | |
ffe743ca | 13911 | Composite_literal_expression::make_array( |
13912 | Type* type, | |
13913 | const std::vector<unsigned long>* indexes, | |
13914 | Expression_list* vals) | |
e440a328 | 13915 | { |
b13c66cd | 13916 | Location location = this->location(); |
e440a328 | 13917 | Array_type* at = type->array_type(); |
ffe743ca | 13918 | |
e440a328 | 13919 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13920 | { | |
ffe743ca | 13921 | size_t size; |
13922 | if (vals == NULL) | |
13923 | size = 0; | |
00773463 | 13924 | else if (indexes != NULL) |
13925 | size = indexes->back() + 1; | |
13926 | else | |
ffe743ca | 13927 | { |
13928 | size = vals->size(); | |
13929 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13930 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13931 | && size >> (it->bits() - 1) != 0) | |
13932 | { | |
631d5788 | 13933 | go_error_at(location, "too many elements in composite literal"); |
ffe743ca | 13934 | return Expression::make_error(location); |
13935 | } | |
13936 | } | |
ffe743ca | 13937 | |
e67508fa | 13938 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 13939 | at = Type::make_array_type(at->element_type(), elen); |
13940 | type = at; | |
13941 | } | |
ffe743ca | 13942 | else if (at->length() != NULL |
13943 | && !at->length()->is_error_expression() | |
13944 | && this->vals_ != NULL) | |
13945 | { | |
13946 | Numeric_constant nc; | |
13947 | unsigned long val; | |
13948 | if (at->length()->numeric_constant_value(&nc) | |
13949 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13950 | { | |
13951 | if (indexes == NULL) | |
13952 | { | |
13953 | if (this->vals_->size() > val) | |
13954 | { | |
631d5788 | 13955 | go_error_at(location, |
13956 | "too many elements in composite literal"); | |
ffe743ca | 13957 | return Expression::make_error(location); |
13958 | } | |
13959 | } | |
13960 | else | |
13961 | { | |
00773463 | 13962 | unsigned long max = indexes->back(); |
ffe743ca | 13963 | if (max >= val) |
13964 | { | |
631d5788 | 13965 | go_error_at(location, |
13966 | ("some element keys in composite literal " | |
13967 | "are out of range")); | |
ffe743ca | 13968 | return Expression::make_error(location); |
13969 | } | |
13970 | } | |
13971 | } | |
13972 | } | |
13973 | ||
e440a328 | 13974 | if (at->length() != NULL) |
ffe743ca | 13975 | return new Fixed_array_construction_expression(type, indexes, vals, |
13976 | location); | |
e440a328 | 13977 | else |
2c809f8f | 13978 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13979 | } |
13980 | ||
13981 | // Lower a map composite literal. | |
13982 | ||
13983 | Expression* | |
a287720d | 13984 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13985 | Statement_inserter* inserter, |
a287720d | 13986 | Type* type) |
e440a328 | 13987 | { |
b13c66cd | 13988 | Location location = this->location(); |
e440a328 | 13989 | if (this->vals_ != NULL) |
13990 | { | |
13991 | if (!this->has_keys_) | |
13992 | { | |
631d5788 | 13993 | go_error_at(location, "map composite literal must have keys"); |
e440a328 | 13994 | return Expression::make_error(location); |
13995 | } | |
13996 | ||
a287720d | 13997 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13998 | p != this->vals_->end(); |
13999 | p += 2) | |
14000 | { | |
14001 | if (*p == NULL) | |
14002 | { | |
14003 | ++p; | |
631d5788 | 14004 | go_error_at((*p)->location(), |
14005 | ("map composite literal must " | |
14006 | "have keys for every value")); | |
e440a328 | 14007 | return Expression::make_error(location); |
14008 | } | |
a287720d | 14009 | // Make sure we have lowered the key; it may not have been |
14010 | // lowered in order to handle keys for struct composite | |
14011 | // literals. Lower it now to get the right error message. | |
14012 | if ((*p)->unknown_expression() != NULL) | |
14013 | { | |
14014 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 14015 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 14016 | go_assert((*p)->is_error_expression()); |
a287720d | 14017 | return Expression::make_error(location); |
14018 | } | |
e440a328 | 14019 | } |
14020 | } | |
14021 | ||
14022 | return new Map_construction_expression(type, this->vals_, location); | |
14023 | } | |
14024 | ||
d751bb78 | 14025 | // Dump ast representation for a composite literal expression. |
14026 | ||
14027 | void | |
14028 | Composite_literal_expression::do_dump_expression( | |
14029 | Ast_dump_context* ast_dump_context) const | |
14030 | { | |
8b1c301d | 14031 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 14032 | ast_dump_context->dump_type(this->type_); |
14033 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 14034 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 14035 | ast_dump_context->ostream() << "})"; |
14036 | } | |
14037 | ||
e440a328 | 14038 | // Make a composite literal expression. |
14039 | ||
14040 | Expression* | |
14041 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 14042 | Expression_list* vals, bool all_are_names, |
b13c66cd | 14043 | Location location) |
e440a328 | 14044 | { |
14045 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 14046 | all_are_names, location); |
e440a328 | 14047 | } |
14048 | ||
14049 | // Return whether this expression is a composite literal. | |
14050 | ||
14051 | bool | |
14052 | Expression::is_composite_literal() const | |
14053 | { | |
14054 | switch (this->classification_) | |
14055 | { | |
14056 | case EXPRESSION_COMPOSITE_LITERAL: | |
14057 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14058 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 14059 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14060 | case EXPRESSION_MAP_CONSTRUCTION: |
14061 | return true; | |
14062 | default: | |
14063 | return false; | |
14064 | } | |
14065 | } | |
14066 | ||
14067 | // Return whether this expression is a composite literal which is not | |
14068 | // constant. | |
14069 | ||
14070 | bool | |
14071 | Expression::is_nonconstant_composite_literal() const | |
14072 | { | |
14073 | switch (this->classification_) | |
14074 | { | |
14075 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14076 | { | |
14077 | const Struct_construction_expression *psce = | |
14078 | static_cast<const Struct_construction_expression*>(this); | |
14079 | return !psce->is_constant_struct(); | |
14080 | } | |
14081 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
14082 | { | |
14083 | const Fixed_array_construction_expression *pace = | |
14084 | static_cast<const Fixed_array_construction_expression*>(this); | |
14085 | return !pace->is_constant_array(); | |
14086 | } | |
2c809f8f | 14087 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14088 | { |
2c809f8f | 14089 | const Slice_construction_expression *pace = |
14090 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 14091 | return !pace->is_constant_array(); |
14092 | } | |
14093 | case EXPRESSION_MAP_CONSTRUCTION: | |
14094 | return true; | |
14095 | default: | |
14096 | return false; | |
14097 | } | |
14098 | } | |
14099 | ||
35a54f17 | 14100 | // Return true if this is a variable or temporary_variable. |
14101 | ||
14102 | bool | |
14103 | Expression::is_variable() const | |
14104 | { | |
14105 | switch (this->classification_) | |
14106 | { | |
14107 | case EXPRESSION_VAR_REFERENCE: | |
14108 | case EXPRESSION_TEMPORARY_REFERENCE: | |
14109 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
b0c09712 | 14110 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
35a54f17 | 14111 | return true; |
14112 | default: | |
14113 | return false; | |
14114 | } | |
14115 | } | |
14116 | ||
e440a328 | 14117 | // Return true if this is a reference to a local variable. |
14118 | ||
14119 | bool | |
14120 | Expression::is_local_variable() const | |
14121 | { | |
14122 | const Var_expression* ve = this->var_expression(); | |
14123 | if (ve == NULL) | |
14124 | return false; | |
14125 | const Named_object* no = ve->named_object(); | |
14126 | return (no->is_result_variable() | |
14127 | || (no->is_variable() && !no->var_value()->is_global())); | |
14128 | } | |
14129 | ||
14130 | // Class Type_guard_expression. | |
14131 | ||
14132 | // Traversal. | |
14133 | ||
14134 | int | |
14135 | Type_guard_expression::do_traverse(Traverse* traverse) | |
14136 | { | |
14137 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
14138 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14139 | return TRAVERSE_EXIT; | |
14140 | return TRAVERSE_CONTINUE; | |
14141 | } | |
14142 | ||
2c809f8f | 14143 | Expression* |
14144 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
14145 | Statement_inserter* inserter) | |
14146 | { | |
5bf8be8b | 14147 | if (this->expr_->is_error_expression() |
14148 | || this->expr_->type()->is_error_type()) | |
14149 | { | |
14150 | go_assert(saw_errors()); | |
14151 | return Expression::make_error(this->location()); | |
14152 | } | |
14153 | ||
2c809f8f | 14154 | if (!this->expr_->is_variable()) |
14155 | { | |
14156 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
14157 | this->location()); | |
14158 | inserter->insert(temp); | |
14159 | this->expr_ = | |
14160 | Expression::make_temporary_reference(temp, this->location()); | |
14161 | } | |
14162 | return this; | |
14163 | } | |
14164 | ||
e440a328 | 14165 | // Check types of a type guard expression. The expression must have |
14166 | // an interface type, but the actual type conversion is checked at run | |
14167 | // time. | |
14168 | ||
14169 | void | |
14170 | Type_guard_expression::do_check_types(Gogo*) | |
14171 | { | |
e440a328 | 14172 | Type* expr_type = this->expr_->type(); |
7e9da23f | 14173 | if (expr_type->interface_type() == NULL) |
f725ade8 | 14174 | { |
5c13bd80 | 14175 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 14176 | this->report_error(_("type assertion only valid for interface types")); |
14177 | this->set_is_error(); | |
14178 | } | |
e440a328 | 14179 | else if (this->type_->interface_type() == NULL) |
14180 | { | |
14181 | std::string reason; | |
14182 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
14183 | &reason)) | |
14184 | { | |
5c13bd80 | 14185 | if (!this->type_->is_error()) |
e440a328 | 14186 | { |
f725ade8 | 14187 | if (reason.empty()) |
14188 | this->report_error(_("impossible type assertion: " | |
14189 | "type does not implement interface")); | |
14190 | else | |
631d5788 | 14191 | go_error_at(this->location(), |
14192 | ("impossible type assertion: " | |
14193 | "type does not implement interface (%s)"), | |
14194 | reason.c_str()); | |
e440a328 | 14195 | } |
f725ade8 | 14196 | this->set_is_error(); |
e440a328 | 14197 | } |
14198 | } | |
14199 | } | |
14200 | ||
ea664253 | 14201 | // Return the backend representation for a type guard expression. |
e440a328 | 14202 | |
ea664253 | 14203 | Bexpression* |
14204 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14205 | { |
2c809f8f | 14206 | Expression* conversion; |
7e9da23f | 14207 | if (this->type_->interface_type() != NULL) |
2c809f8f | 14208 | conversion = |
14209 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
14210 | true, this->location()); | |
e440a328 | 14211 | else |
2c809f8f | 14212 | conversion = |
14213 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
14214 | this->expr_, this->location()); | |
14215 | ||
21b70e8f | 14216 | Gogo* gogo = context->gogo(); |
14217 | Btype* bt = this->type_->get_backend(gogo); | |
14218 | Bexpression* bexpr = conversion->get_backend(context); | |
14219 | return gogo->backend()->convert_expression(bt, bexpr, this->location()); | |
e440a328 | 14220 | } |
14221 | ||
d751bb78 | 14222 | // Dump ast representation for a type guard expression. |
14223 | ||
14224 | void | |
2c809f8f | 14225 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 14226 | const |
14227 | { | |
14228 | this->expr_->dump_expression(ast_dump_context); | |
14229 | ast_dump_context->ostream() << "."; | |
14230 | ast_dump_context->dump_type(this->type_); | |
14231 | } | |
14232 | ||
e440a328 | 14233 | // Make a type guard expression. |
14234 | ||
14235 | Expression* | |
14236 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 14237 | Location location) |
e440a328 | 14238 | { |
14239 | return new Type_guard_expression(expr, type, location); | |
14240 | } | |
14241 | ||
2c809f8f | 14242 | // Class Heap_expression. |
e440a328 | 14243 | |
da244e59 | 14244 | // Return the type of the expression stored on the heap. |
e440a328 | 14245 | |
da244e59 | 14246 | Type* |
14247 | Heap_expression::do_type() | |
14248 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 14249 | |
ea664253 | 14250 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 14251 | |
ea664253 | 14252 | Bexpression* |
14253 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14254 | { |
03118c21 | 14255 | Type* etype = this->expr_->type(); |
14256 | if (this->expr_->is_error_expression() || etype->is_error()) | |
ea664253 | 14257 | return context->backend()->error_expression(); |
2c809f8f | 14258 | |
02c19a1a | 14259 | Location loc = this->location(); |
2c809f8f | 14260 | Gogo* gogo = context->gogo(); |
02c19a1a | 14261 | Btype* btype = this->type()->get_backend(gogo); |
45ff893b | 14262 | |
03118c21 | 14263 | Expression* alloc = Expression::make_allocation(etype, loc); |
45ff893b | 14264 | Node* n = Node::make_node(this); |
14265 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
14266 | alloc->allocation_expression()->set_allocate_on_stack(); | |
14267 | Bexpression* space = alloc->get_backend(context); | |
02c19a1a | 14268 | |
14269 | Bstatement* decl; | |
14270 | Named_object* fn = context->function(); | |
14271 | go_assert(fn != NULL); | |
14272 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
14273 | Bvariable* space_temp = | |
14274 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
14275 | space, true, loc, &decl); | |
03118c21 | 14276 | Btype* expr_btype = etype->get_backend(gogo); |
02c19a1a | 14277 | |
ea664253 | 14278 | Bexpression* bexpr = this->expr_->get_backend(context); |
03118c21 | 14279 | |
14280 | // If this assignment needs a write barrier, call typedmemmove. We | |
14281 | // don't do this in the write barrier pass because in some cases | |
14282 | // backend conversion can introduce new Heap_expression values. | |
14283 | Bstatement* assn; | |
14284 | if (!etype->has_pointer()) | |
14285 | { | |
7af8e400 | 14286 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14287 | Bexpression* ref = |
14288 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
14289 | assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc); | |
14290 | } | |
14291 | else | |
14292 | { | |
14293 | Bstatement* edecl; | |
14294 | Bvariable* btemp = | |
14295 | gogo->backend()->temporary_variable(fndecl, context->bblock(), | |
14296 | expr_btype, bexpr, true, loc, | |
14297 | &edecl); | |
14298 | Bexpression* btempref = gogo->backend()->var_expression(btemp, | |
7af8e400 | 14299 | loc); |
03118c21 | 14300 | Bexpression* addr = gogo->backend()->address_expression(btempref, loc); |
14301 | ||
14302 | Expression* td = Expression::make_type_descriptor(etype, loc); | |
14303 | Type* etype_ptr = Type::make_pointer_type(etype); | |
7af8e400 | 14304 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14305 | Expression* elhs = Expression::make_backend(space, etype_ptr, loc); |
14306 | Expression* erhs = Expression::make_backend(addr, etype_ptr, loc); | |
14307 | Expression* call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3, | |
14308 | td, elhs, erhs); | |
14309 | Bexpression* bcall = call->get_backend(context); | |
14310 | Bstatement* s = gogo->backend()->expression_statement(fndecl, bcall); | |
14311 | assn = gogo->backend()->compound_statement(edecl, s); | |
14312 | } | |
02c19a1a | 14313 | decl = gogo->backend()->compound_statement(decl, assn); |
7af8e400 | 14314 | space = gogo->backend()->var_expression(space_temp, loc); |
ea664253 | 14315 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 14316 | } |
14317 | ||
2c809f8f | 14318 | // Dump ast representation for a heap expression. |
d751bb78 | 14319 | |
14320 | void | |
2c809f8f | 14321 | Heap_expression::do_dump_expression( |
d751bb78 | 14322 | Ast_dump_context* ast_dump_context) const |
14323 | { | |
14324 | ast_dump_context->ostream() << "&("; | |
14325 | ast_dump_context->dump_expression(this->expr_); | |
14326 | ast_dump_context->ostream() << ")"; | |
14327 | } | |
14328 | ||
2c809f8f | 14329 | // Allocate an expression on the heap. |
e440a328 | 14330 | |
14331 | Expression* | |
2c809f8f | 14332 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 14333 | { |
2c809f8f | 14334 | return new Heap_expression(expr, location); |
e440a328 | 14335 | } |
14336 | ||
14337 | // Class Receive_expression. | |
14338 | ||
14339 | // Return the type of a receive expression. | |
14340 | ||
14341 | Type* | |
14342 | Receive_expression::do_type() | |
14343 | { | |
e429e3bd | 14344 | if (this->is_error_expression()) |
14345 | return Type::make_error_type(); | |
e440a328 | 14346 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
14347 | if (channel_type == NULL) | |
e429e3bd | 14348 | { |
14349 | this->report_error(_("expected channel")); | |
14350 | return Type::make_error_type(); | |
14351 | } | |
e440a328 | 14352 | return channel_type->element_type(); |
14353 | } | |
14354 | ||
14355 | // Check types for a receive expression. | |
14356 | ||
14357 | void | |
14358 | Receive_expression::do_check_types(Gogo*) | |
14359 | { | |
14360 | Type* type = this->channel_->type(); | |
5c13bd80 | 14361 | if (type->is_error()) |
e440a328 | 14362 | { |
e429e3bd | 14363 | go_assert(saw_errors()); |
e440a328 | 14364 | this->set_is_error(); |
14365 | return; | |
14366 | } | |
14367 | if (type->channel_type() == NULL) | |
14368 | { | |
14369 | this->report_error(_("expected channel")); | |
14370 | return; | |
14371 | } | |
14372 | if (!type->channel_type()->may_receive()) | |
14373 | { | |
14374 | this->report_error(_("invalid receive on send-only channel")); | |
14375 | return; | |
14376 | } | |
14377 | } | |
14378 | ||
2c809f8f | 14379 | // Flattening for receive expressions creates a temporary variable to store |
14380 | // received data in for receives. | |
14381 | ||
14382 | Expression* | |
14383 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
14384 | Statement_inserter* inserter) | |
14385 | { | |
14386 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
14387 | if (channel_type == NULL) | |
14388 | { | |
14389 | go_assert(saw_errors()); | |
14390 | return this; | |
14391 | } | |
5bf8be8b | 14392 | else if (this->channel_->is_error_expression()) |
14393 | { | |
14394 | go_assert(saw_errors()); | |
14395 | return Expression::make_error(this->location()); | |
14396 | } | |
2c809f8f | 14397 | |
14398 | Type* element_type = channel_type->element_type(); | |
14399 | if (this->temp_receiver_ == NULL) | |
14400 | { | |
14401 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
14402 | this->location()); | |
14403 | this->temp_receiver_->set_is_address_taken(); | |
14404 | inserter->insert(this->temp_receiver_); | |
14405 | } | |
14406 | ||
14407 | return this; | |
14408 | } | |
14409 | ||
ea664253 | 14410 | // Get the backend representation for a receive expression. |
e440a328 | 14411 | |
ea664253 | 14412 | Bexpression* |
14413 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14414 | { |
f24f10bb | 14415 | Location loc = this->location(); |
14416 | ||
e440a328 | 14417 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 14418 | if (channel_type == NULL) |
14419 | { | |
c484d925 | 14420 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 14421 | return context->backend()->error_expression(); |
5b8368f4 | 14422 | } |
e440a328 | 14423 | |
2c809f8f | 14424 | Expression* recv_ref = |
14425 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14426 | Expression* recv_addr = | |
14427 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14428 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
e36a5ff5 | 14429 | Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 2, |
14430 | this->channel_, recv_addr); | |
ea664253 | 14431 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 14432 | } |
14433 | ||
d751bb78 | 14434 | // Dump ast representation for a receive expression. |
14435 | ||
14436 | void | |
14437 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14438 | { | |
14439 | ast_dump_context->ostream() << " <- " ; | |
14440 | ast_dump_context->dump_expression(channel_); | |
14441 | } | |
14442 | ||
e440a328 | 14443 | // Make a receive expression. |
14444 | ||
14445 | Receive_expression* | |
b13c66cd | 14446 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 14447 | { |
14448 | return new Receive_expression(channel, location); | |
14449 | } | |
14450 | ||
e440a328 | 14451 | // An expression which evaluates to a pointer to the type descriptor |
14452 | // of a type. | |
14453 | ||
14454 | class Type_descriptor_expression : public Expression | |
14455 | { | |
14456 | public: | |
b13c66cd | 14457 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 14458 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
14459 | type_(type) | |
14460 | { } | |
14461 | ||
14462 | protected: | |
4b686186 | 14463 | int |
14464 | do_traverse(Traverse*); | |
14465 | ||
e440a328 | 14466 | Type* |
14467 | do_type() | |
14468 | { return Type::make_type_descriptor_ptr_type(); } | |
14469 | ||
f9ca30f9 | 14470 | bool |
3ae06f68 | 14471 | do_is_static_initializer() const |
f9ca30f9 | 14472 | { return true; } |
14473 | ||
e440a328 | 14474 | void |
14475 | do_determine_type(const Type_context*) | |
14476 | { } | |
14477 | ||
14478 | Expression* | |
14479 | do_copy() | |
14480 | { return this; } | |
14481 | ||
ea664253 | 14482 | Bexpression* |
14483 | do_get_backend(Translate_context* context) | |
a1d23b41 | 14484 | { |
ea664253 | 14485 | return this->type_->type_descriptor_pointer(context->gogo(), |
14486 | this->location()); | |
a1d23b41 | 14487 | } |
e440a328 | 14488 | |
d751bb78 | 14489 | void |
14490 | do_dump_expression(Ast_dump_context*) const; | |
14491 | ||
e440a328 | 14492 | private: |
14493 | // The type for which this is the descriptor. | |
14494 | Type* type_; | |
14495 | }; | |
14496 | ||
4b686186 | 14497 | int |
14498 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
14499 | { | |
14500 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14501 | return TRAVERSE_EXIT; | |
14502 | return TRAVERSE_CONTINUE; | |
14503 | } | |
14504 | ||
d751bb78 | 14505 | // Dump ast representation for a type descriptor expression. |
14506 | ||
14507 | void | |
14508 | Type_descriptor_expression::do_dump_expression( | |
14509 | Ast_dump_context* ast_dump_context) const | |
14510 | { | |
14511 | ast_dump_context->dump_type(this->type_); | |
14512 | } | |
14513 | ||
e440a328 | 14514 | // Make a type descriptor expression. |
14515 | ||
14516 | Expression* | |
b13c66cd | 14517 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14518 | { |
14519 | return new Type_descriptor_expression(type, location); | |
14520 | } | |
14521 | ||
aa5ae575 | 14522 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
14523 | // of a type. | |
14524 | ||
14525 | class GC_symbol_expression : public Expression | |
14526 | { | |
14527 | public: | |
14528 | GC_symbol_expression(Type* type) | |
14529 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
14530 | type_(type) | |
14531 | {} | |
14532 | ||
14533 | protected: | |
14534 | Type* | |
14535 | do_type() | |
03118c21 | 14536 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } |
aa5ae575 | 14537 | |
14538 | bool | |
3ae06f68 | 14539 | do_is_static_initializer() const |
aa5ae575 | 14540 | { return true; } |
14541 | ||
14542 | void | |
14543 | do_determine_type(const Type_context*) | |
14544 | { } | |
14545 | ||
14546 | Expression* | |
14547 | do_copy() | |
14548 | { return this; } | |
14549 | ||
14550 | Bexpression* | |
14551 | do_get_backend(Translate_context* context) | |
14552 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
14553 | ||
14554 | void | |
14555 | do_dump_expression(Ast_dump_context*) const; | |
14556 | ||
14557 | private: | |
14558 | // The type which this gc symbol describes. | |
14559 | Type* type_; | |
14560 | }; | |
14561 | ||
14562 | // Dump ast representation for a gc symbol expression. | |
14563 | ||
14564 | void | |
14565 | GC_symbol_expression::do_dump_expression( | |
14566 | Ast_dump_context* ast_dump_context) const | |
14567 | { | |
14568 | ast_dump_context->ostream() << "gcdata("; | |
14569 | ast_dump_context->dump_type(this->type_); | |
14570 | ast_dump_context->ostream() << ")"; | |
14571 | } | |
14572 | ||
14573 | // Make a gc symbol expression. | |
14574 | ||
14575 | Expression* | |
14576 | Expression::make_gc_symbol(Type* type) | |
14577 | { | |
14578 | return new GC_symbol_expression(type); | |
14579 | } | |
14580 | ||
03118c21 | 14581 | // An expression that evaluates to a pointer to a symbol holding the |
14582 | // ptrmask data of a type. | |
14583 | ||
14584 | class Ptrmask_symbol_expression : public Expression | |
14585 | { | |
14586 | public: | |
14587 | Ptrmask_symbol_expression(Type* type) | |
14588 | : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()), | |
14589 | type_(type) | |
14590 | {} | |
14591 | ||
14592 | protected: | |
14593 | Type* | |
14594 | do_type() | |
14595 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } | |
14596 | ||
14597 | bool | |
14598 | do_is_static_initializer() const | |
14599 | { return true; } | |
14600 | ||
14601 | void | |
14602 | do_determine_type(const Type_context*) | |
14603 | { } | |
14604 | ||
14605 | Expression* | |
14606 | do_copy() | |
14607 | { return this; } | |
14608 | ||
14609 | Bexpression* | |
14610 | do_get_backend(Translate_context*); | |
14611 | ||
14612 | void | |
14613 | do_dump_expression(Ast_dump_context*) const; | |
14614 | ||
14615 | private: | |
14616 | // The type that this ptrmask symbol describes. | |
14617 | Type* type_; | |
14618 | }; | |
14619 | ||
14620 | // Return the ptrmask variable. | |
14621 | ||
14622 | Bexpression* | |
14623 | Ptrmask_symbol_expression::do_get_backend(Translate_context* context) | |
14624 | { | |
14625 | Gogo* gogo = context->gogo(); | |
14626 | ||
14627 | // If this type does not need a gcprog, then we can use the standard | |
14628 | // GC symbol. | |
14629 | int64_t ptrsize, ptrdata; | |
14630 | if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata)) | |
14631 | return this->type_->gc_symbol_pointer(gogo); | |
14632 | ||
14633 | // Otherwise we have to build a ptrmask variable, and return a | |
14634 | // pointer to it. | |
14635 | ||
14636 | Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata); | |
14637 | Location bloc = Linemap::predeclared_location(); | |
7af8e400 | 14638 | Bexpression* bref = gogo->backend()->var_expression(bvar, bloc); |
03118c21 | 14639 | Bexpression* baddr = gogo->backend()->address_expression(bref, bloc); |
14640 | ||
14641 | Type* uint8_type = Type::lookup_integer_type("uint8"); | |
14642 | Type* pointer_uint8_type = Type::make_pointer_type(uint8_type); | |
14643 | Btype* ubtype = pointer_uint8_type->get_backend(gogo); | |
14644 | return gogo->backend()->convert_expression(ubtype, baddr, bloc); | |
14645 | } | |
14646 | ||
14647 | // Dump AST for a ptrmask symbol expression. | |
14648 | ||
14649 | void | |
14650 | Ptrmask_symbol_expression::do_dump_expression( | |
14651 | Ast_dump_context* ast_dump_context) const | |
14652 | { | |
14653 | ast_dump_context->ostream() << "ptrmask("; | |
14654 | ast_dump_context->dump_type(this->type_); | |
14655 | ast_dump_context->ostream() << ")"; | |
14656 | } | |
14657 | ||
14658 | // Make a ptrmask symbol expression. | |
14659 | ||
14660 | Expression* | |
14661 | Expression::make_ptrmask_symbol(Type* type) | |
14662 | { | |
14663 | return new Ptrmask_symbol_expression(type); | |
14664 | } | |
14665 | ||
e440a328 | 14666 | // An expression which evaluates to some characteristic of a type. |
14667 | // This is only used to initialize fields of a type descriptor. Using | |
14668 | // a new expression class is slightly inefficient but gives us a good | |
14669 | // separation between the frontend and the middle-end with regard to | |
14670 | // how types are laid out. | |
14671 | ||
14672 | class Type_info_expression : public Expression | |
14673 | { | |
14674 | public: | |
14675 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14676 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14677 | type_(type), type_info_(type_info) |
14678 | { } | |
14679 | ||
14680 | protected: | |
0e168074 | 14681 | bool |
3ae06f68 | 14682 | do_is_static_initializer() const |
0e168074 | 14683 | { return true; } |
14684 | ||
e440a328 | 14685 | Type* |
14686 | do_type(); | |
14687 | ||
14688 | void | |
14689 | do_determine_type(const Type_context*) | |
14690 | { } | |
14691 | ||
14692 | Expression* | |
14693 | do_copy() | |
14694 | { return this; } | |
14695 | ||
ea664253 | 14696 | Bexpression* |
14697 | do_get_backend(Translate_context* context); | |
e440a328 | 14698 | |
d751bb78 | 14699 | void |
14700 | do_dump_expression(Ast_dump_context*) const; | |
14701 | ||
e440a328 | 14702 | private: |
14703 | // The type for which we are getting information. | |
14704 | Type* type_; | |
14705 | // What information we want. | |
14706 | Type_info type_info_; | |
14707 | }; | |
14708 | ||
14709 | // The type is chosen to match what the type descriptor struct | |
14710 | // expects. | |
14711 | ||
14712 | Type* | |
14713 | Type_info_expression::do_type() | |
14714 | { | |
14715 | switch (this->type_info_) | |
14716 | { | |
14717 | case TYPE_INFO_SIZE: | |
03118c21 | 14718 | case TYPE_INFO_BACKEND_PTRDATA: |
14719 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
e440a328 | 14720 | return Type::lookup_integer_type("uintptr"); |
14721 | case TYPE_INFO_ALIGNMENT: | |
14722 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14723 | return Type::lookup_integer_type("uint8"); | |
14724 | default: | |
c3e6f413 | 14725 | go_unreachable(); |
e440a328 | 14726 | } |
14727 | } | |
14728 | ||
ea664253 | 14729 | // Return the backend representation for type information. |
e440a328 | 14730 | |
ea664253 | 14731 | Bexpression* |
14732 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14733 | { |
927a01eb | 14734 | Gogo* gogo = context->gogo(); |
2a305b85 | 14735 | bool ok = true; |
3f378015 | 14736 | int64_t val; |
927a01eb | 14737 | switch (this->type_info_) |
e440a328 | 14738 | { |
927a01eb | 14739 | case TYPE_INFO_SIZE: |
2a305b85 | 14740 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 14741 | break; |
14742 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 14743 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 14744 | break; |
14745 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 14746 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 14747 | break; |
03118c21 | 14748 | case TYPE_INFO_BACKEND_PTRDATA: |
14749 | ok = this->type_->backend_type_ptrdata(gogo, &val); | |
14750 | break; | |
14751 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
14752 | ok = this->type_->descriptor_ptrdata(gogo, &val); | |
14753 | break; | |
927a01eb | 14754 | default: |
14755 | go_unreachable(); | |
e440a328 | 14756 | } |
2a305b85 | 14757 | if (!ok) |
14758 | { | |
14759 | go_assert(saw_errors()); | |
14760 | return gogo->backend()->error_expression(); | |
14761 | } | |
3f378015 | 14762 | Expression* e = Expression::make_integer_int64(val, this->type(), |
14763 | this->location()); | |
14764 | return e->get_backend(context); | |
e440a328 | 14765 | } |
14766 | ||
d751bb78 | 14767 | // Dump ast representation for a type info expression. |
14768 | ||
14769 | void | |
14770 | Type_info_expression::do_dump_expression( | |
14771 | Ast_dump_context* ast_dump_context) const | |
14772 | { | |
14773 | ast_dump_context->ostream() << "typeinfo("; | |
14774 | ast_dump_context->dump_type(this->type_); | |
14775 | ast_dump_context->ostream() << ","; | |
14776 | ast_dump_context->ostream() << | |
14777 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14778 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
03118c21 | 14779 | : this->type_info_ == TYPE_INFO_SIZE ? "size" |
14780 | : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata" | |
14781 | : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata" | |
d751bb78 | 14782 | : "unknown"); |
14783 | ast_dump_context->ostream() << ")"; | |
14784 | } | |
14785 | ||
e440a328 | 14786 | // Make a type info expression. |
14787 | ||
14788 | Expression* | |
14789 | Expression::make_type_info(Type* type, Type_info type_info) | |
14790 | { | |
14791 | return new Type_info_expression(type, type_info); | |
14792 | } | |
14793 | ||
35a54f17 | 14794 | // An expression that evaluates to some characteristic of a slice. |
14795 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14796 | ||
14797 | class Slice_info_expression : public Expression | |
14798 | { | |
14799 | public: | |
14800 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14801 | Location location) | |
14802 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14803 | slice_(slice), slice_info_(slice_info) | |
14804 | { } | |
14805 | ||
14806 | protected: | |
14807 | Type* | |
14808 | do_type(); | |
14809 | ||
14810 | void | |
14811 | do_determine_type(const Type_context*) | |
14812 | { } | |
14813 | ||
14814 | Expression* | |
14815 | do_copy() | |
14816 | { | |
14817 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14818 | this->location()); | |
14819 | } | |
14820 | ||
ea664253 | 14821 | Bexpression* |
14822 | do_get_backend(Translate_context* context); | |
35a54f17 | 14823 | |
14824 | void | |
14825 | do_dump_expression(Ast_dump_context*) const; | |
14826 | ||
14827 | void | |
14828 | do_issue_nil_check() | |
14829 | { this->slice_->issue_nil_check(); } | |
14830 | ||
14831 | private: | |
14832 | // The slice for which we are getting information. | |
14833 | Expression* slice_; | |
14834 | // What information we want. | |
14835 | Slice_info slice_info_; | |
14836 | }; | |
14837 | ||
14838 | // Return the type of the slice info. | |
14839 | ||
14840 | Type* | |
14841 | Slice_info_expression::do_type() | |
14842 | { | |
14843 | switch (this->slice_info_) | |
14844 | { | |
14845 | case SLICE_INFO_VALUE_POINTER: | |
14846 | return Type::make_pointer_type( | |
14847 | this->slice_->type()->array_type()->element_type()); | |
14848 | case SLICE_INFO_LENGTH: | |
14849 | case SLICE_INFO_CAPACITY: | |
14850 | return Type::lookup_integer_type("int"); | |
14851 | default: | |
14852 | go_unreachable(); | |
14853 | } | |
14854 | } | |
14855 | ||
ea664253 | 14856 | // Return the backend information for slice information. |
35a54f17 | 14857 | |
ea664253 | 14858 | Bexpression* |
14859 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 14860 | { |
14861 | Gogo* gogo = context->gogo(); | |
ea664253 | 14862 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 14863 | switch (this->slice_info_) |
14864 | { | |
14865 | case SLICE_INFO_VALUE_POINTER: | |
14866 | case SLICE_INFO_LENGTH: | |
14867 | case SLICE_INFO_CAPACITY: | |
ea664253 | 14868 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
14869 | this->location()); | |
35a54f17 | 14870 | break; |
14871 | default: | |
14872 | go_unreachable(); | |
14873 | } | |
35a54f17 | 14874 | } |
14875 | ||
14876 | // Dump ast representation for a type info expression. | |
14877 | ||
14878 | void | |
14879 | Slice_info_expression::do_dump_expression( | |
14880 | Ast_dump_context* ast_dump_context) const | |
14881 | { | |
14882 | ast_dump_context->ostream() << "sliceinfo("; | |
14883 | this->slice_->dump_expression(ast_dump_context); | |
14884 | ast_dump_context->ostream() << ","; | |
14885 | ast_dump_context->ostream() << | |
14886 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14887 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14888 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14889 | : "unknown"); | |
14890 | ast_dump_context->ostream() << ")"; | |
14891 | } | |
14892 | ||
14893 | // Make a slice info expression. | |
14894 | ||
14895 | Expression* | |
14896 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14897 | Location location) | |
14898 | { | |
14899 | return new Slice_info_expression(slice, slice_info, location); | |
14900 | } | |
14901 | ||
2c809f8f | 14902 | // An expression that represents a slice value: a struct with value pointer, |
14903 | // length, and capacity fields. | |
14904 | ||
14905 | class Slice_value_expression : public Expression | |
14906 | { | |
14907 | public: | |
14908 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14909 | Expression* cap, Location location) | |
14910 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14911 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14912 | { } | |
14913 | ||
14914 | protected: | |
14915 | int | |
14916 | do_traverse(Traverse*); | |
14917 | ||
14918 | Type* | |
14919 | do_type() | |
14920 | { return this->type_; } | |
14921 | ||
14922 | void | |
14923 | do_determine_type(const Type_context*) | |
14924 | { go_unreachable(); } | |
14925 | ||
14926 | Expression* | |
14927 | do_copy() | |
14928 | { | |
14929 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14930 | this->len_->copy(), this->cap_->copy(), | |
14931 | this->location()); | |
14932 | } | |
14933 | ||
ea664253 | 14934 | Bexpression* |
14935 | do_get_backend(Translate_context* context); | |
2c809f8f | 14936 | |
14937 | void | |
14938 | do_dump_expression(Ast_dump_context*) const; | |
14939 | ||
14940 | private: | |
14941 | // The type of the slice value. | |
14942 | Type* type_; | |
14943 | // The pointer to the values in the slice. | |
14944 | Expression* valptr_; | |
14945 | // The length of the slice. | |
14946 | Expression* len_; | |
14947 | // The capacity of the slice. | |
14948 | Expression* cap_; | |
14949 | }; | |
14950 | ||
14951 | int | |
14952 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14953 | { | |
55e8ba6a | 14954 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
14955 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 14956 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
14957 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14958 | return TRAVERSE_EXIT; | |
14959 | return TRAVERSE_CONTINUE; | |
14960 | } | |
14961 | ||
ea664253 | 14962 | Bexpression* |
14963 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14964 | { |
14965 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14966 | vals[0] = this->valptr_->get_backend(context); |
14967 | vals[1] = this->len_->get_backend(context); | |
14968 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14969 | |
14970 | Gogo* gogo = context->gogo(); | |
14971 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14972 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14973 | } |
14974 | ||
14975 | void | |
14976 | Slice_value_expression::do_dump_expression( | |
14977 | Ast_dump_context* ast_dump_context) const | |
14978 | { | |
14979 | ast_dump_context->ostream() << "slicevalue("; | |
14980 | ast_dump_context->ostream() << "values: "; | |
14981 | this->valptr_->dump_expression(ast_dump_context); | |
14982 | ast_dump_context->ostream() << ", length: "; | |
14983 | this->len_->dump_expression(ast_dump_context); | |
14984 | ast_dump_context->ostream() << ", capacity: "; | |
14985 | this->cap_->dump_expression(ast_dump_context); | |
14986 | ast_dump_context->ostream() << ")"; | |
14987 | } | |
14988 | ||
14989 | Expression* | |
14990 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14991 | Expression* cap, Location location) | |
14992 | { | |
14993 | go_assert(at->is_slice_type()); | |
14994 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14995 | } | |
2387f644 | 14996 | |
14997 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14998 | // This is used to access the method table or underlying object of an interface. | |
14999 | ||
15000 | class Interface_info_expression : public Expression | |
15001 | { | |
15002 | public: | |
15003 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 15004 | Location location) |
2387f644 | 15005 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
15006 | iface_(iface), iface_info_(iface_info) | |
15007 | { } | |
15008 | ||
15009 | protected: | |
15010 | Type* | |
15011 | do_type(); | |
15012 | ||
15013 | void | |
15014 | do_determine_type(const Type_context*) | |
15015 | { } | |
15016 | ||
15017 | Expression* | |
15018 | do_copy() | |
15019 | { | |
15020 | return new Interface_info_expression(this->iface_->copy(), | |
15021 | this->iface_info_, this->location()); | |
15022 | } | |
15023 | ||
ea664253 | 15024 | Bexpression* |
15025 | do_get_backend(Translate_context* context); | |
2387f644 | 15026 | |
15027 | void | |
15028 | do_dump_expression(Ast_dump_context*) const; | |
15029 | ||
15030 | void | |
15031 | do_issue_nil_check() | |
15032 | { this->iface_->issue_nil_check(); } | |
15033 | ||
15034 | private: | |
15035 | // The interface for which we are getting information. | |
15036 | Expression* iface_; | |
15037 | // What information we want. | |
15038 | Interface_info iface_info_; | |
15039 | }; | |
15040 | ||
15041 | // Return the type of the interface info. | |
15042 | ||
15043 | Type* | |
15044 | Interface_info_expression::do_type() | |
15045 | { | |
15046 | switch (this->iface_info_) | |
15047 | { | |
15048 | case INTERFACE_INFO_METHODS: | |
15049 | { | |
625d3118 | 15050 | typedef Unordered_map(Interface_type*, Type*) Hashtable; |
15051 | static Hashtable result_types; | |
15052 | ||
15053 | Interface_type* itype = this->iface_->type()->interface_type(); | |
15054 | ||
15055 | Hashtable::const_iterator p = result_types.find(itype); | |
15056 | if (p != result_types.end()) | |
15057 | return p->second; | |
15058 | ||
2c809f8f | 15059 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
625d3118 | 15060 | if (itype->is_empty()) |
15061 | { | |
15062 | result_types[itype] = pdt; | |
15063 | return pdt; | |
15064 | } | |
2c809f8f | 15065 | |
2387f644 | 15066 | Location loc = this->location(); |
15067 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 15068 | sfl->push_back( |
15069 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
15070 | ||
2387f644 | 15071 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); |
15072 | p != itype->methods()->end(); | |
15073 | ++p) | |
15074 | { | |
15075 | Function_type* ft = p->type()->function_type(); | |
15076 | go_assert(ft->receiver() == NULL); | |
15077 | ||
15078 | const Typed_identifier_list* params = ft->parameters(); | |
15079 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
15080 | if (params != NULL) | |
15081 | mparams->reserve(params->size() + 1); | |
15082 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
15083 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
15084 | if (params != NULL) | |
15085 | { | |
15086 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
15087 | pp != params->end(); | |
15088 | ++pp) | |
15089 | mparams->push_back(*pp); | |
15090 | } | |
15091 | ||
15092 | Typed_identifier_list* mresults = (ft->results() == NULL | |
15093 | ? NULL | |
15094 | : ft->results()->copy()); | |
15095 | Backend_function_type* mft = | |
15096 | Type::make_backend_function_type(NULL, mparams, mresults, | |
15097 | ft->location()); | |
15098 | ||
15099 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
15100 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
15101 | } | |
15102 | ||
6bf4793c | 15103 | Struct_type* st = Type::make_struct_type(sfl, loc); |
15104 | st->set_is_struct_incomparable(); | |
15105 | Pointer_type *pt = Type::make_pointer_type(st); | |
625d3118 | 15106 | result_types[itype] = pt; |
15107 | return pt; | |
2387f644 | 15108 | } |
15109 | case INTERFACE_INFO_OBJECT: | |
15110 | return Type::make_pointer_type(Type::make_void_type()); | |
15111 | default: | |
15112 | go_unreachable(); | |
15113 | } | |
15114 | } | |
15115 | ||
ea664253 | 15116 | // Return the backend representation for interface information. |
2387f644 | 15117 | |
ea664253 | 15118 | Bexpression* |
15119 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 15120 | { |
15121 | Gogo* gogo = context->gogo(); | |
ea664253 | 15122 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 15123 | switch (this->iface_info_) |
15124 | { | |
15125 | case INTERFACE_INFO_METHODS: | |
15126 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 15127 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
15128 | this->location()); | |
2387f644 | 15129 | break; |
15130 | default: | |
15131 | go_unreachable(); | |
15132 | } | |
2387f644 | 15133 | } |
15134 | ||
15135 | // Dump ast representation for an interface info expression. | |
15136 | ||
15137 | void | |
15138 | Interface_info_expression::do_dump_expression( | |
15139 | Ast_dump_context* ast_dump_context) const | |
15140 | { | |
2c809f8f | 15141 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 15142 | ast_dump_context->ostream() << "interfaceinfo("; |
15143 | this->iface_->dump_expression(ast_dump_context); | |
15144 | ast_dump_context->ostream() << ","; | |
15145 | ast_dump_context->ostream() << | |
2c809f8f | 15146 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
15147 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 15148 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
15149 | : "unknown"); | |
15150 | ast_dump_context->ostream() << ")"; | |
15151 | } | |
15152 | ||
15153 | // Make an interface info expression. | |
15154 | ||
15155 | Expression* | |
15156 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
15157 | Location location) | |
15158 | { | |
15159 | return new Interface_info_expression(iface, iface_info, location); | |
15160 | } | |
15161 | ||
2c809f8f | 15162 | // An expression that represents an interface value. The first field is either |
15163 | // a type descriptor for an empty interface or a pointer to the interface method | |
15164 | // table for a non-empty interface. The second field is always the object. | |
15165 | ||
15166 | class Interface_value_expression : public Expression | |
15167 | { | |
15168 | public: | |
15169 | Interface_value_expression(Type* type, Expression* first_field, | |
15170 | Expression* obj, Location location) | |
15171 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
15172 | type_(type), first_field_(first_field), obj_(obj) | |
15173 | { } | |
15174 | ||
15175 | protected: | |
15176 | int | |
15177 | do_traverse(Traverse*); | |
15178 | ||
15179 | Type* | |
15180 | do_type() | |
15181 | { return this->type_; } | |
15182 | ||
15183 | void | |
15184 | do_determine_type(const Type_context*) | |
15185 | { go_unreachable(); } | |
15186 | ||
15187 | Expression* | |
15188 | do_copy() | |
15189 | { | |
15190 | return new Interface_value_expression(this->type_, | |
15191 | this->first_field_->copy(), | |
15192 | this->obj_->copy(), this->location()); | |
15193 | } | |
15194 | ||
ea664253 | 15195 | Bexpression* |
15196 | do_get_backend(Translate_context* context); | |
2c809f8f | 15197 | |
15198 | void | |
15199 | do_dump_expression(Ast_dump_context*) const; | |
15200 | ||
15201 | private: | |
15202 | // The type of the interface value. | |
15203 | Type* type_; | |
15204 | // The first field of the interface (either a type descriptor or a pointer | |
15205 | // to the method table. | |
15206 | Expression* first_field_; | |
15207 | // The underlying object of the interface. | |
15208 | Expression* obj_; | |
15209 | }; | |
15210 | ||
15211 | int | |
15212 | Interface_value_expression::do_traverse(Traverse* traverse) | |
15213 | { | |
15214 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
15215 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
15216 | return TRAVERSE_EXIT; | |
15217 | return TRAVERSE_CONTINUE; | |
15218 | } | |
15219 | ||
ea664253 | 15220 | Bexpression* |
15221 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15222 | { |
15223 | std::vector<Bexpression*> vals(2); | |
ea664253 | 15224 | vals[0] = this->first_field_->get_backend(context); |
15225 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 15226 | |
15227 | Gogo* gogo = context->gogo(); | |
15228 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 15229 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 15230 | } |
15231 | ||
15232 | void | |
15233 | Interface_value_expression::do_dump_expression( | |
15234 | Ast_dump_context* ast_dump_context) const | |
15235 | { | |
15236 | ast_dump_context->ostream() << "interfacevalue("; | |
15237 | ast_dump_context->ostream() << | |
15238 | (this->type_->interface_type()->is_empty() | |
15239 | ? "type_descriptor: " | |
15240 | : "methods: "); | |
15241 | this->first_field_->dump_expression(ast_dump_context); | |
15242 | ast_dump_context->ostream() << ", object: "; | |
15243 | this->obj_->dump_expression(ast_dump_context); | |
15244 | ast_dump_context->ostream() << ")"; | |
15245 | } | |
15246 | ||
15247 | Expression* | |
15248 | Expression::make_interface_value(Type* type, Expression* first_value, | |
15249 | Expression* object, Location location) | |
15250 | { | |
15251 | return new Interface_value_expression(type, first_value, object, location); | |
15252 | } | |
15253 | ||
15254 | // An interface method table for a pair of types: an interface type and a type | |
15255 | // that implements that interface. | |
15256 | ||
15257 | class Interface_mtable_expression : public Expression | |
15258 | { | |
15259 | public: | |
15260 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
15261 | bool is_pointer, Location location) | |
15262 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
15263 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
15264 | method_table_type_(NULL), bvar_(NULL) | |
15265 | { } | |
15266 | ||
15267 | protected: | |
15268 | int | |
15269 | do_traverse(Traverse*); | |
15270 | ||
15271 | Type* | |
15272 | do_type(); | |
15273 | ||
15274 | bool | |
3ae06f68 | 15275 | do_is_static_initializer() const |
2c809f8f | 15276 | { return true; } |
15277 | ||
15278 | void | |
15279 | do_determine_type(const Type_context*) | |
15280 | { go_unreachable(); } | |
15281 | ||
15282 | Expression* | |
15283 | do_copy() | |
15284 | { | |
15285 | return new Interface_mtable_expression(this->itype_, this->type_, | |
15286 | this->is_pointer_, this->location()); | |
15287 | } | |
15288 | ||
15289 | bool | |
15290 | do_is_addressable() const | |
15291 | { return true; } | |
15292 | ||
ea664253 | 15293 | Bexpression* |
15294 | do_get_backend(Translate_context* context); | |
2c809f8f | 15295 | |
15296 | void | |
15297 | do_dump_expression(Ast_dump_context*) const; | |
15298 | ||
15299 | private: | |
15300 | // The interface type for which the methods are defined. | |
15301 | Interface_type* itype_; | |
15302 | // The type to construct the interface method table for. | |
15303 | Type* type_; | |
15304 | // Whether this table contains the method set for the receiver type or the | |
15305 | // pointer receiver type. | |
15306 | bool is_pointer_; | |
15307 | // The type of the method table. | |
15308 | Type* method_table_type_; | |
15309 | // The backend variable that refers to the interface method table. | |
15310 | Bvariable* bvar_; | |
15311 | }; | |
15312 | ||
15313 | int | |
15314 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
15315 | { | |
15316 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
15317 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
15318 | return TRAVERSE_EXIT; | |
15319 | return TRAVERSE_CONTINUE; | |
15320 | } | |
15321 | ||
15322 | Type* | |
15323 | Interface_mtable_expression::do_type() | |
15324 | { | |
15325 | if (this->method_table_type_ != NULL) | |
15326 | return this->method_table_type_; | |
15327 | ||
15328 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15329 | go_assert(!interface_methods->empty()); | |
15330 | ||
15331 | Struct_field_list* sfl = new Struct_field_list; | |
15332 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
15333 | this->location()); | |
15334 | sfl->push_back(Struct_field(tid)); | |
db122cb9 | 15335 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); |
2c809f8f | 15336 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); |
15337 | p != interface_methods->end(); | |
15338 | ++p) | |
db122cb9 | 15339 | { |
15340 | // We want C function pointers here, not func descriptors; model | |
15341 | // using void* pointers. | |
15342 | Typed_identifier method(p->name(), unsafe_ptr_type, p->location()); | |
15343 | sfl->push_back(Struct_field(method)); | |
15344 | } | |
6bf4793c | 15345 | Struct_type* st = Type::make_struct_type(sfl, this->location()); |
15346 | st->set_is_struct_incomparable(); | |
15347 | this->method_table_type_ = st; | |
2c809f8f | 15348 | return this->method_table_type_; |
15349 | } | |
15350 | ||
ea664253 | 15351 | Bexpression* |
15352 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15353 | { |
15354 | Gogo* gogo = context->gogo(); | |
2c809f8f | 15355 | Location loc = Linemap::predeclared_location(); |
15356 | if (this->bvar_ != NULL) | |
7af8e400 | 15357 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15358 | |
15359 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15360 | go_assert(!interface_methods->empty()); | |
15361 | ||
19272321 | 15362 | std::string mangled_name = |
15363 | gogo->interface_method_table_name(this->itype_, this->type_, | |
15364 | this->is_pointer_); | |
2c809f8f | 15365 | |
1530c754 | 15366 | // Set is_public if we are converting a named type to an interface |
15367 | // type that is defined in the same package as the named type, and | |
15368 | // the interface has hidden methods. In that case the interface | |
15369 | // method table will be defined by the package that defines the | |
15370 | // types. | |
15371 | bool is_public = false; | |
15372 | if (this->type_->named_type() != NULL | |
15373 | && (this->type_->named_type()->named_object()->package() | |
15374 | == this->itype_->package())) | |
15375 | { | |
15376 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15377 | p != interface_methods->end(); | |
15378 | ++p) | |
2c809f8f | 15379 | { |
1530c754 | 15380 | if (Gogo::is_hidden_name(p->name())) |
15381 | { | |
15382 | is_public = true; | |
15383 | break; | |
15384 | } | |
2c809f8f | 15385 | } |
15386 | } | |
15387 | ||
1530c754 | 15388 | if (is_public |
2c809f8f | 15389 | && this->type_->named_type()->named_object()->package() != NULL) |
15390 | { | |
1530c754 | 15391 | // The interface conversion table is defined elsewhere. |
2c809f8f | 15392 | Btype* btype = this->type()->get_backend(gogo); |
438b4bec | 15393 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
2c809f8f | 15394 | this->bvar_ = |
438b4bec | 15395 | gogo->backend()->immutable_struct_reference(mangled_name, asm_name, |
15396 | btype, loc); | |
7af8e400 | 15397 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15398 | } |
15399 | ||
15400 | // The first element is the type descriptor. | |
15401 | Type* td_type; | |
15402 | if (!this->is_pointer_) | |
15403 | td_type = this->type_; | |
15404 | else | |
15405 | td_type = Type::make_pointer_type(this->type_); | |
15406 | ||
db122cb9 | 15407 | std::vector<Backend::Btyped_identifier> bstructfields; |
15408 | ||
2c809f8f | 15409 | // Build an interface method table for a type: a type descriptor followed by a |
15410 | // list of function pointers, one for each interface method. This is used for | |
15411 | // interfaces. | |
15412 | Expression_list* svals = new Expression_list(); | |
db122cb9 | 15413 | Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc); |
15414 | svals->push_back(tdescriptor); | |
15415 | ||
15416 | Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo); | |
15417 | Backend::Btyped_identifier btd("_type", tdesc_btype, loc); | |
15418 | bstructfields.push_back(btd); | |
2c809f8f | 15419 | |
15420 | Named_type* nt = this->type_->named_type(); | |
15421 | Struct_type* st = this->type_->struct_type(); | |
15422 | go_assert(nt != NULL || st != NULL); | |
15423 | ||
15424 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15425 | p != interface_methods->end(); | |
15426 | ++p) | |
15427 | { | |
15428 | bool is_ambiguous; | |
15429 | Method* m; | |
15430 | if (nt != NULL) | |
15431 | m = nt->method_function(p->name(), &is_ambiguous); | |
15432 | else | |
15433 | m = st->method_function(p->name(), &is_ambiguous); | |
15434 | go_assert(m != NULL); | |
15435 | Named_object* no = m->named_object(); | |
15436 | ||
15437 | go_assert(no->is_function() || no->is_function_declaration()); | |
db122cb9 | 15438 | |
15439 | Btype* fcn_btype = m->type()->get_backend_fntype(gogo); | |
15440 | Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc); | |
15441 | bstructfields.push_back(bmtype); | |
15442 | ||
2c809f8f | 15443 | svals->push_back(Expression::make_func_code_reference(no, loc)); |
15444 | } | |
15445 | ||
db122cb9 | 15446 | Btype *btype = gogo->backend()->struct_type(bstructfields); |
15447 | std::vector<Bexpression*> ctor_bexprs; | |
15448 | for (Expression_list::const_iterator pe = svals->begin(); | |
15449 | pe != svals->end(); | |
15450 | ++pe) | |
15451 | { | |
15452 | ctor_bexprs.push_back((*pe)->get_backend(context)); | |
15453 | } | |
15454 | Bexpression* ctor = | |
15455 | gogo->backend()->constructor_expression(btype, ctor_bexprs, loc); | |
2c809f8f | 15456 | |
438b4bec | 15457 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
15458 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false, | |
2c809f8f | 15459 | !is_public, btype, loc); |
15460 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
15461 | !is_public, btype, loc, ctor); | |
7af8e400 | 15462 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 15463 | } |
15464 | ||
15465 | void | |
15466 | Interface_mtable_expression::do_dump_expression( | |
15467 | Ast_dump_context* ast_dump_context) const | |
15468 | { | |
15469 | ast_dump_context->ostream() << "__go_" | |
15470 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
15471 | ast_dump_context->dump_type(this->itype_); | |
15472 | ast_dump_context->ostream() << "__"; | |
15473 | ast_dump_context->dump_type(this->type_); | |
15474 | } | |
15475 | ||
15476 | Expression* | |
15477 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
15478 | bool is_pointer, Location location) | |
15479 | { | |
15480 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
15481 | } | |
15482 | ||
e440a328 | 15483 | // An expression which evaluates to the offset of a field within a |
15484 | // struct. This, like Type_info_expression, q.v., is only used to | |
15485 | // initialize fields of a type descriptor. | |
15486 | ||
15487 | class Struct_field_offset_expression : public Expression | |
15488 | { | |
15489 | public: | |
15490 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 15491 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
15492 | Linemap::predeclared_location()), | |
e440a328 | 15493 | type_(type), field_(field) |
15494 | { } | |
15495 | ||
15496 | protected: | |
f23d7786 | 15497 | bool |
3ae06f68 | 15498 | do_is_static_initializer() const |
f23d7786 | 15499 | { return true; } |
15500 | ||
e440a328 | 15501 | Type* |
15502 | do_type() | |
15503 | { return Type::lookup_integer_type("uintptr"); } | |
15504 | ||
15505 | void | |
15506 | do_determine_type(const Type_context*) | |
15507 | { } | |
15508 | ||
15509 | Expression* | |
15510 | do_copy() | |
15511 | { return this; } | |
15512 | ||
ea664253 | 15513 | Bexpression* |
15514 | do_get_backend(Translate_context* context); | |
e440a328 | 15515 | |
d751bb78 | 15516 | void |
15517 | do_dump_expression(Ast_dump_context*) const; | |
15518 | ||
e440a328 | 15519 | private: |
15520 | // The type of the struct. | |
15521 | Struct_type* type_; | |
15522 | // The field. | |
15523 | const Struct_field* field_; | |
15524 | }; | |
15525 | ||
ea664253 | 15526 | // Return the backend representation for a struct field offset. |
e440a328 | 15527 | |
ea664253 | 15528 | Bexpression* |
15529 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 15530 | { |
e440a328 | 15531 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 15532 | Struct_field_list::const_iterator p; |
2c8bda43 | 15533 | unsigned i = 0; |
e440a328 | 15534 | for (p = fields->begin(); |
15535 | p != fields->end(); | |
2c8bda43 | 15536 | ++p, ++i) |
15537 | if (&*p == this->field_) | |
15538 | break; | |
c484d925 | 15539 | go_assert(&*p == this->field_); |
e440a328 | 15540 | |
2c8bda43 | 15541 | Gogo* gogo = context->gogo(); |
15542 | Btype* btype = this->type_->get_backend(gogo); | |
15543 | ||
3f378015 | 15544 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 15545 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 15546 | Expression* ret = |
3f378015 | 15547 | Expression::make_integer_int64(offset, uptr_type, |
15548 | Linemap::predeclared_location()); | |
ea664253 | 15549 | return ret->get_backend(context); |
e440a328 | 15550 | } |
15551 | ||
d751bb78 | 15552 | // Dump ast representation for a struct field offset expression. |
15553 | ||
15554 | void | |
15555 | Struct_field_offset_expression::do_dump_expression( | |
15556 | Ast_dump_context* ast_dump_context) const | |
15557 | { | |
15558 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 15559 | ast_dump_context->dump_type(this->type_); |
15560 | ast_dump_context->ostream() << '.'; | |
15561 | ast_dump_context->ostream() << | |
15562 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 15563 | ast_dump_context->ostream() << ")"; |
15564 | } | |
15565 | ||
e440a328 | 15566 | // Make an expression for a struct field offset. |
15567 | ||
15568 | Expression* | |
15569 | Expression::make_struct_field_offset(Struct_type* type, | |
15570 | const Struct_field* field) | |
15571 | { | |
15572 | return new Struct_field_offset_expression(type, field); | |
15573 | } | |
15574 | ||
15575 | // An expression which evaluates to the address of an unnamed label. | |
15576 | ||
15577 | class Label_addr_expression : public Expression | |
15578 | { | |
15579 | public: | |
b13c66cd | 15580 | Label_addr_expression(Label* label, Location location) |
e440a328 | 15581 | : Expression(EXPRESSION_LABEL_ADDR, location), |
15582 | label_(label) | |
15583 | { } | |
15584 | ||
15585 | protected: | |
15586 | Type* | |
15587 | do_type() | |
15588 | { return Type::make_pointer_type(Type::make_void_type()); } | |
15589 | ||
15590 | void | |
15591 | do_determine_type(const Type_context*) | |
15592 | { } | |
15593 | ||
15594 | Expression* | |
15595 | do_copy() | |
15596 | { return new Label_addr_expression(this->label_, this->location()); } | |
15597 | ||
ea664253 | 15598 | Bexpression* |
15599 | do_get_backend(Translate_context* context) | |
15600 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 15601 | |
d751bb78 | 15602 | void |
15603 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15604 | { ast_dump_context->ostream() << this->label_->name(); } | |
15605 | ||
e440a328 | 15606 | private: |
15607 | // The label whose address we are taking. | |
15608 | Label* label_; | |
15609 | }; | |
15610 | ||
15611 | // Make an expression for the address of an unnamed label. | |
15612 | ||
15613 | Expression* | |
b13c66cd | 15614 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 15615 | { |
15616 | return new Label_addr_expression(label, location); | |
15617 | } | |
15618 | ||
da244e59 | 15619 | // Class Conditional_expression. |
283a177b | 15620 | |
2c809f8f | 15621 | // Traversal. |
15622 | ||
15623 | int | |
15624 | Conditional_expression::do_traverse(Traverse* traverse) | |
15625 | { | |
15626 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15627 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15628 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15629 | return TRAVERSE_EXIT; | |
15630 | return TRAVERSE_CONTINUE; | |
15631 | } | |
15632 | ||
283a177b | 15633 | // Return the type of the conditional expression. |
15634 | ||
15635 | Type* | |
15636 | Conditional_expression::do_type() | |
15637 | { | |
15638 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15639 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15640 | NULL)) | |
283a177b | 15641 | result_type = this->then_->type(); |
15642 | else if (this->then_->is_nil_expression() | |
15643 | || this->else_->is_nil_expression()) | |
15644 | result_type = (!this->then_->is_nil_expression() | |
15645 | ? this->then_->type() | |
15646 | : this->else_->type()); | |
15647 | return result_type; | |
15648 | } | |
15649 | ||
2c809f8f | 15650 | // Determine type for a conditional expression. |
15651 | ||
15652 | void | |
15653 | Conditional_expression::do_determine_type(const Type_context* context) | |
15654 | { | |
15655 | this->cond_->determine_type_no_context(); | |
15656 | this->then_->determine_type(context); | |
15657 | this->else_->determine_type(context); | |
15658 | } | |
15659 | ||
283a177b | 15660 | // Get the backend representation of a conditional expression. |
15661 | ||
ea664253 | 15662 | Bexpression* |
15663 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 15664 | { |
15665 | Gogo* gogo = context->gogo(); | |
15666 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 15667 | Bexpression* cond = this->cond_->get_backend(context); |
15668 | Bexpression* then = this->then_->get_backend(context); | |
15669 | Bexpression* belse = this->else_->get_backend(context); | |
93715b75 | 15670 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
15671 | return gogo->backend()->conditional_expression(bfn, result_btype, cond, then, | |
ea664253 | 15672 | belse, this->location()); |
283a177b | 15673 | } |
15674 | ||
15675 | // Dump ast representation of a conditional expression. | |
15676 | ||
15677 | void | |
15678 | Conditional_expression::do_dump_expression( | |
15679 | Ast_dump_context* ast_dump_context) const | |
15680 | { | |
15681 | ast_dump_context->ostream() << "("; | |
15682 | ast_dump_context->dump_expression(this->cond_); | |
15683 | ast_dump_context->ostream() << " ? "; | |
15684 | ast_dump_context->dump_expression(this->then_); | |
15685 | ast_dump_context->ostream() << " : "; | |
15686 | ast_dump_context->dump_expression(this->else_); | |
15687 | ast_dump_context->ostream() << ") "; | |
15688 | } | |
15689 | ||
15690 | // Make a conditional expression. | |
15691 | ||
15692 | Expression* | |
15693 | Expression::make_conditional(Expression* cond, Expression* then, | |
15694 | Expression* else_expr, Location location) | |
15695 | { | |
15696 | return new Conditional_expression(cond, then, else_expr, location); | |
15697 | } | |
15698 | ||
da244e59 | 15699 | // Class Compound_expression. |
2c809f8f | 15700 | |
15701 | // Traversal. | |
15702 | ||
15703 | int | |
15704 | Compound_expression::do_traverse(Traverse* traverse) | |
15705 | { | |
15706 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15707 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15708 | return TRAVERSE_EXIT; | |
15709 | return TRAVERSE_CONTINUE; | |
15710 | } | |
15711 | ||
15712 | // Return the type of the compound expression. | |
15713 | ||
15714 | Type* | |
15715 | Compound_expression::do_type() | |
15716 | { | |
15717 | return this->expr_->type(); | |
15718 | } | |
15719 | ||
15720 | // Determine type for a compound expression. | |
15721 | ||
15722 | void | |
15723 | Compound_expression::do_determine_type(const Type_context* context) | |
15724 | { | |
15725 | this->init_->determine_type_no_context(); | |
15726 | this->expr_->determine_type(context); | |
15727 | } | |
15728 | ||
15729 | // Get the backend representation of a compound expression. | |
15730 | ||
ea664253 | 15731 | Bexpression* |
15732 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15733 | { |
15734 | Gogo* gogo = context->gogo(); | |
ea664253 | 15735 | Bexpression* binit = this->init_->get_backend(context); |
0ab48656 | 15736 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
15737 | Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction, | |
15738 | binit); | |
ea664253 | 15739 | Bexpression* bexpr = this->expr_->get_backend(context); |
15740 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15741 | this->location()); | |
2c809f8f | 15742 | } |
15743 | ||
15744 | // Dump ast representation of a conditional expression. | |
15745 | ||
15746 | void | |
15747 | Compound_expression::do_dump_expression( | |
15748 | Ast_dump_context* ast_dump_context) const | |
15749 | { | |
15750 | ast_dump_context->ostream() << "("; | |
15751 | ast_dump_context->dump_expression(this->init_); | |
15752 | ast_dump_context->ostream() << ","; | |
15753 | ast_dump_context->dump_expression(this->expr_); | |
15754 | ast_dump_context->ostream() << ") "; | |
15755 | } | |
15756 | ||
15757 | // Make a compound expression. | |
15758 | ||
15759 | Expression* | |
15760 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15761 | { | |
15762 | return new Compound_expression(init, expr, location); | |
15763 | } | |
15764 | ||
1b4fb1e0 | 15765 | // Class Backend_expression. |
15766 | ||
15767 | int | |
15768 | Backend_expression::do_traverse(Traverse*) | |
15769 | { | |
15770 | return TRAVERSE_CONTINUE; | |
15771 | } | |
15772 | ||
15773 | void | |
15774 | Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15775 | { | |
15776 | ast_dump_context->ostream() << "backend_expression<"; | |
15777 | ast_dump_context->dump_type(this->type_); | |
15778 | ast_dump_context->ostream() << ">"; | |
15779 | } | |
15780 | ||
15781 | Expression* | |
15782 | Expression::make_backend(Bexpression* bexpr, Type* type, Location location) | |
15783 | { | |
15784 | return new Backend_expression(bexpr, type, location); | |
15785 | } | |
15786 | ||
e440a328 | 15787 | // Import an expression. This comes at the end in order to see the |
15788 | // various class definitions. | |
15789 | ||
15790 | Expression* | |
15791 | Expression::import_expression(Import* imp) | |
15792 | { | |
15793 | int c = imp->peek_char(); | |
15794 | if (imp->match_c_string("- ") | |
15795 | || imp->match_c_string("! ") | |
15796 | || imp->match_c_string("^ ")) | |
15797 | return Unary_expression::do_import(imp); | |
15798 | else if (c == '(') | |
15799 | return Binary_expression::do_import(imp); | |
15800 | else if (imp->match_c_string("true") | |
15801 | || imp->match_c_string("false")) | |
15802 | return Boolean_expression::do_import(imp); | |
15803 | else if (c == '"') | |
15804 | return String_expression::do_import(imp); | |
15805 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15806 | { | |
15807 | // This handles integers, floats and complex constants. | |
15808 | return Integer_expression::do_import(imp); | |
15809 | } | |
15810 | else if (imp->match_c_string("nil")) | |
15811 | return Nil_expression::do_import(imp); | |
15812 | else if (imp->match_c_string("convert")) | |
15813 | return Type_conversion_expression::do_import(imp); | |
15814 | else | |
15815 | { | |
631d5788 | 15816 | go_error_at(imp->location(), "import error: expected expression"); |
e440a328 | 15817 | return Expression::make_error(imp->location()); |
15818 | } | |
15819 | } | |
15820 | ||
15821 | // Class Expression_list. | |
15822 | ||
15823 | // Traverse the list. | |
15824 | ||
15825 | int | |
15826 | Expression_list::traverse(Traverse* traverse) | |
15827 | { | |
15828 | for (Expression_list::iterator p = this->begin(); | |
15829 | p != this->end(); | |
15830 | ++p) | |
15831 | { | |
15832 | if (*p != NULL) | |
15833 | { | |
15834 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15835 | return TRAVERSE_EXIT; | |
15836 | } | |
15837 | } | |
15838 | return TRAVERSE_CONTINUE; | |
15839 | } | |
15840 | ||
15841 | // Copy the list. | |
15842 | ||
15843 | Expression_list* | |
15844 | Expression_list::copy() | |
15845 | { | |
15846 | Expression_list* ret = new Expression_list(); | |
15847 | for (Expression_list::iterator p = this->begin(); | |
15848 | p != this->end(); | |
15849 | ++p) | |
15850 | { | |
15851 | if (*p == NULL) | |
15852 | ret->push_back(NULL); | |
15853 | else | |
15854 | ret->push_back((*p)->copy()); | |
15855 | } | |
15856 | return ret; | |
15857 | } | |
15858 | ||
15859 | // Return whether an expression list has an error expression. | |
15860 | ||
15861 | bool | |
15862 | Expression_list::contains_error() const | |
15863 | { | |
15864 | for (Expression_list::const_iterator p = this->begin(); | |
15865 | p != this->end(); | |
15866 | ++p) | |
15867 | if (*p != NULL && (*p)->is_error_expression()) | |
15868 | return true; | |
15869 | return false; | |
15870 | } | |
0c77715b | 15871 | |
15872 | // Class Numeric_constant. | |
15873 | ||
15874 | // Destructor. | |
15875 | ||
15876 | Numeric_constant::~Numeric_constant() | |
15877 | { | |
15878 | this->clear(); | |
15879 | } | |
15880 | ||
15881 | // Copy constructor. | |
15882 | ||
15883 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15884 | : classification_(a.classification_), type_(a.type_) | |
15885 | { | |
15886 | switch (a.classification_) | |
15887 | { | |
15888 | case NC_INVALID: | |
15889 | break; | |
15890 | case NC_INT: | |
15891 | case NC_RUNE: | |
15892 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15893 | break; | |
15894 | case NC_FLOAT: | |
15895 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15896 | break; | |
15897 | case NC_COMPLEX: | |
fcbea5e4 | 15898 | mpc_init2(this->u_.complex_val, mpc_precision); |
15899 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15900 | break; |
15901 | default: | |
15902 | go_unreachable(); | |
15903 | } | |
15904 | } | |
15905 | ||
15906 | // Assignment operator. | |
15907 | ||
15908 | Numeric_constant& | |
15909 | Numeric_constant::operator=(const Numeric_constant& a) | |
15910 | { | |
15911 | this->clear(); | |
15912 | this->classification_ = a.classification_; | |
15913 | this->type_ = a.type_; | |
15914 | switch (a.classification_) | |
15915 | { | |
15916 | case NC_INVALID: | |
15917 | break; | |
15918 | case NC_INT: | |
15919 | case NC_RUNE: | |
15920 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15921 | break; | |
15922 | case NC_FLOAT: | |
15923 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15924 | break; | |
15925 | case NC_COMPLEX: | |
fcbea5e4 | 15926 | mpc_init2(this->u_.complex_val, mpc_precision); |
15927 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15928 | break; |
15929 | default: | |
15930 | go_unreachable(); | |
15931 | } | |
15932 | return *this; | |
15933 | } | |
15934 | ||
15935 | // Clear the contents. | |
15936 | ||
15937 | void | |
15938 | Numeric_constant::clear() | |
15939 | { | |
15940 | switch (this->classification_) | |
15941 | { | |
15942 | case NC_INVALID: | |
15943 | break; | |
15944 | case NC_INT: | |
15945 | case NC_RUNE: | |
15946 | mpz_clear(this->u_.int_val); | |
15947 | break; | |
15948 | case NC_FLOAT: | |
15949 | mpfr_clear(this->u_.float_val); | |
15950 | break; | |
15951 | case NC_COMPLEX: | |
fcbea5e4 | 15952 | mpc_clear(this->u_.complex_val); |
0c77715b | 15953 | break; |
15954 | default: | |
15955 | go_unreachable(); | |
15956 | } | |
15957 | this->classification_ = NC_INVALID; | |
15958 | } | |
15959 | ||
15960 | // Set to an unsigned long value. | |
15961 | ||
15962 | void | |
15963 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15964 | { | |
15965 | this->clear(); | |
15966 | this->classification_ = NC_INT; | |
15967 | this->type_ = type; | |
15968 | mpz_init_set_ui(this->u_.int_val, val); | |
15969 | } | |
15970 | ||
15971 | // Set to an integer value. | |
15972 | ||
15973 | void | |
15974 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15975 | { | |
15976 | this->clear(); | |
15977 | this->classification_ = NC_INT; | |
15978 | this->type_ = type; | |
15979 | mpz_init_set(this->u_.int_val, val); | |
15980 | } | |
15981 | ||
15982 | // Set to a rune value. | |
15983 | ||
15984 | void | |
15985 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15986 | { | |
15987 | this->clear(); | |
15988 | this->classification_ = NC_RUNE; | |
15989 | this->type_ = type; | |
15990 | mpz_init_set(this->u_.int_val, val); | |
15991 | } | |
15992 | ||
15993 | // Set to a floating point value. | |
15994 | ||
15995 | void | |
15996 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15997 | { | |
15998 | this->clear(); | |
15999 | this->classification_ = NC_FLOAT; | |
16000 | this->type_ = type; | |
833b523c | 16001 | // Numeric constants do not have negative zero values, so remove |
16002 | // them here. They also don't have infinity or NaN values, but we | |
16003 | // should never see them here. | |
16004 | if (mpfr_zero_p(val)) | |
16005 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
16006 | else | |
16007 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 16008 | } |
16009 | ||
16010 | // Set to a complex value. | |
16011 | ||
16012 | void | |
fcbea5e4 | 16013 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 16014 | { |
16015 | this->clear(); | |
16016 | this->classification_ = NC_COMPLEX; | |
16017 | this->type_ = type; | |
fcbea5e4 | 16018 | mpc_init2(this->u_.complex_val, mpc_precision); |
16019 | mpc_set(this->u_.complex_val, val, MPC_RNDNN); | |
0c77715b | 16020 | } |
16021 | ||
16022 | // Get an int value. | |
16023 | ||
16024 | void | |
16025 | Numeric_constant::get_int(mpz_t* val) const | |
16026 | { | |
16027 | go_assert(this->is_int()); | |
16028 | mpz_init_set(*val, this->u_.int_val); | |
16029 | } | |
16030 | ||
16031 | // Get a rune value. | |
16032 | ||
16033 | void | |
16034 | Numeric_constant::get_rune(mpz_t* val) const | |
16035 | { | |
16036 | go_assert(this->is_rune()); | |
16037 | mpz_init_set(*val, this->u_.int_val); | |
16038 | } | |
16039 | ||
16040 | // Get a floating point value. | |
16041 | ||
16042 | void | |
16043 | Numeric_constant::get_float(mpfr_t* val) const | |
16044 | { | |
16045 | go_assert(this->is_float()); | |
16046 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16047 | } | |
16048 | ||
16049 | // Get a complex value. | |
16050 | ||
16051 | void | |
fcbea5e4 | 16052 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 16053 | { |
16054 | go_assert(this->is_complex()); | |
fcbea5e4 | 16055 | mpc_init2(*val, mpc_precision); |
16056 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 16057 | } |
16058 | ||
16059 | // Express value as unsigned long if possible. | |
16060 | ||
16061 | Numeric_constant::To_unsigned_long | |
16062 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
16063 | { | |
16064 | switch (this->classification_) | |
16065 | { | |
16066 | case NC_INT: | |
16067 | case NC_RUNE: | |
16068 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
16069 | case NC_FLOAT: | |
16070 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
16071 | case NC_COMPLEX: | |
fcbea5e4 | 16072 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16073 | return NC_UL_NOTINT; |
fcbea5e4 | 16074 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
16075 | val); | |
0c77715b | 16076 | default: |
16077 | go_unreachable(); | |
16078 | } | |
16079 | } | |
16080 | ||
16081 | // Express integer value as unsigned long if possible. | |
16082 | ||
16083 | Numeric_constant::To_unsigned_long | |
16084 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
16085 | unsigned long *val) const | |
16086 | { | |
16087 | if (mpz_sgn(ival) < 0) | |
16088 | return NC_UL_NEGATIVE; | |
16089 | unsigned long ui = mpz_get_ui(ival); | |
16090 | if (mpz_cmp_ui(ival, ui) != 0) | |
16091 | return NC_UL_BIG; | |
16092 | *val = ui; | |
16093 | return NC_UL_VALID; | |
16094 | } | |
16095 | ||
16096 | // Express floating point value as unsigned long if possible. | |
16097 | ||
16098 | Numeric_constant::To_unsigned_long | |
16099 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
16100 | unsigned long *val) const | |
16101 | { | |
16102 | if (!mpfr_integer_p(fval)) | |
16103 | return NC_UL_NOTINT; | |
16104 | mpz_t ival; | |
16105 | mpz_init(ival); | |
16106 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16107 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
16108 | mpz_clear(ival); | |
16109 | return ret; | |
16110 | } | |
16111 | ||
03118c21 | 16112 | // Express value as memory size if possible. |
16113 | ||
16114 | bool | |
16115 | Numeric_constant::to_memory_size(int64_t* val) const | |
16116 | { | |
16117 | switch (this->classification_) | |
16118 | { | |
16119 | case NC_INT: | |
16120 | case NC_RUNE: | |
16121 | return this->mpz_to_memory_size(this->u_.int_val, val); | |
16122 | case NC_FLOAT: | |
16123 | return this->mpfr_to_memory_size(this->u_.float_val, val); | |
16124 | case NC_COMPLEX: | |
16125 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
16126 | return false; | |
16127 | return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val); | |
16128 | default: | |
16129 | go_unreachable(); | |
16130 | } | |
16131 | } | |
16132 | ||
16133 | // Express integer as memory size if possible. | |
16134 | ||
16135 | bool | |
16136 | Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const | |
16137 | { | |
16138 | if (mpz_sgn(ival) < 0) | |
16139 | return false; | |
16140 | if (mpz_fits_slong_p(ival)) | |
16141 | { | |
16142 | *val = static_cast<int64_t>(mpz_get_si(ival)); | |
16143 | return true; | |
16144 | } | |
16145 | ||
16146 | // Test >= 64, not > 64, because an int64_t can hold 63 bits of a | |
16147 | // positive value. | |
16148 | if (mpz_sizeinbase(ival, 2) >= 64) | |
16149 | return false; | |
16150 | ||
16151 | mpz_t q, r; | |
16152 | mpz_init(q); | |
16153 | mpz_init(r); | |
16154 | mpz_tdiv_q_2exp(q, ival, 32); | |
16155 | mpz_tdiv_r_2exp(r, ival, 32); | |
16156 | go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r)); | |
16157 | *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32) | |
16158 | + static_cast<int64_t>(mpz_get_ui(r))); | |
16159 | mpz_clear(r); | |
16160 | mpz_clear(q); | |
16161 | return true; | |
16162 | } | |
16163 | ||
16164 | // Express floating point value as memory size if possible. | |
16165 | ||
16166 | bool | |
16167 | Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const | |
16168 | { | |
16169 | if (!mpfr_integer_p(fval)) | |
16170 | return false; | |
16171 | mpz_t ival; | |
16172 | mpz_init(ival); | |
16173 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16174 | bool ret = this->mpz_to_memory_size(ival, val); | |
16175 | mpz_clear(ival); | |
16176 | return ret; | |
16177 | } | |
16178 | ||
0c77715b | 16179 | // Convert value to integer if possible. |
16180 | ||
16181 | bool | |
16182 | Numeric_constant::to_int(mpz_t* val) const | |
16183 | { | |
16184 | switch (this->classification_) | |
16185 | { | |
16186 | case NC_INT: | |
16187 | case NC_RUNE: | |
16188 | mpz_init_set(*val, this->u_.int_val); | |
16189 | return true; | |
16190 | case NC_FLOAT: | |
16191 | if (!mpfr_integer_p(this->u_.float_val)) | |
16192 | return false; | |
16193 | mpz_init(*val); | |
16194 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
16195 | return true; | |
16196 | case NC_COMPLEX: | |
fcbea5e4 | 16197 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
16198 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 16199 | return false; |
16200 | mpz_init(*val); | |
fcbea5e4 | 16201 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16202 | return true; |
16203 | default: | |
16204 | go_unreachable(); | |
16205 | } | |
16206 | } | |
16207 | ||
16208 | // Convert value to floating point if possible. | |
16209 | ||
16210 | bool | |
16211 | Numeric_constant::to_float(mpfr_t* val) const | |
16212 | { | |
16213 | switch (this->classification_) | |
16214 | { | |
16215 | case NC_INT: | |
16216 | case NC_RUNE: | |
16217 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
16218 | return true; | |
16219 | case NC_FLOAT: | |
16220 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16221 | return true; | |
16222 | case NC_COMPLEX: | |
fcbea5e4 | 16223 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16224 | return false; |
fcbea5e4 | 16225 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16226 | return true; |
16227 | default: | |
16228 | go_unreachable(); | |
16229 | } | |
16230 | } | |
16231 | ||
16232 | // Convert value to complex. | |
16233 | ||
16234 | bool | |
fcbea5e4 | 16235 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 16236 | { |
fcbea5e4 | 16237 | mpc_init2(*val, mpc_precision); |
0c77715b | 16238 | switch (this->classification_) |
16239 | { | |
16240 | case NC_INT: | |
16241 | case NC_RUNE: | |
fcbea5e4 | 16242 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16243 | return true; |
16244 | case NC_FLOAT: | |
fcbea5e4 | 16245 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16246 | return true; |
16247 | case NC_COMPLEX: | |
fcbea5e4 | 16248 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16249 | return true; |
16250 | default: | |
16251 | go_unreachable(); | |
16252 | } | |
16253 | } | |
16254 | ||
16255 | // Get the type. | |
16256 | ||
16257 | Type* | |
16258 | Numeric_constant::type() const | |
16259 | { | |
16260 | if (this->type_ != NULL) | |
16261 | return this->type_; | |
16262 | switch (this->classification_) | |
16263 | { | |
16264 | case NC_INT: | |
16265 | return Type::make_abstract_integer_type(); | |
16266 | case NC_RUNE: | |
16267 | return Type::make_abstract_character_type(); | |
16268 | case NC_FLOAT: | |
16269 | return Type::make_abstract_float_type(); | |
16270 | case NC_COMPLEX: | |
16271 | return Type::make_abstract_complex_type(); | |
16272 | default: | |
16273 | go_unreachable(); | |
16274 | } | |
16275 | } | |
16276 | ||
16277 | // If the constant can be expressed in TYPE, then set the type of the | |
16278 | // constant to TYPE and return true. Otherwise return false, and, if | |
16279 | // ISSUE_ERROR is true, report an appropriate error message. | |
16280 | ||
16281 | bool | |
16282 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
16283 | { | |
16284 | bool ret; | |
f11c2155 | 16285 | if (type == NULL || type->is_error()) |
0c77715b | 16286 | ret = true; |
16287 | else if (type->integer_type() != NULL) | |
16288 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
16289 | else if (type->float_type() != NULL) | |
16290 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
16291 | else if (type->complex_type() != NULL) | |
16292 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
16293 | else | |
5706ab68 | 16294 | { |
16295 | ret = false; | |
16296 | if (issue_error) | |
16297 | go_assert(saw_errors()); | |
16298 | } | |
0c77715b | 16299 | if (ret) |
16300 | this->type_ = type; | |
16301 | return ret; | |
16302 | } | |
16303 | ||
16304 | // Check whether the constant can be expressed in an integer type. | |
16305 | ||
16306 | bool | |
16307 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 16308 | Location location) |
0c77715b | 16309 | { |
16310 | mpz_t val; | |
16311 | switch (this->classification_) | |
16312 | { | |
16313 | case NC_INT: | |
16314 | case NC_RUNE: | |
16315 | mpz_init_set(val, this->u_.int_val); | |
16316 | break; | |
16317 | ||
16318 | case NC_FLOAT: | |
16319 | if (!mpfr_integer_p(this->u_.float_val)) | |
16320 | { | |
16321 | if (issue_error) | |
71a45216 | 16322 | { |
631d5788 | 16323 | go_error_at(location, |
16324 | "floating point constant truncated to integer"); | |
71a45216 | 16325 | this->set_invalid(); |
16326 | } | |
0c77715b | 16327 | return false; |
16328 | } | |
16329 | mpz_init(val); | |
16330 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
16331 | break; | |
16332 | ||
16333 | case NC_COMPLEX: | |
fcbea5e4 | 16334 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
16335 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 16336 | { |
16337 | if (issue_error) | |
71a45216 | 16338 | { |
631d5788 | 16339 | go_error_at(location, "complex constant truncated to integer"); |
71a45216 | 16340 | this->set_invalid(); |
16341 | } | |
0c77715b | 16342 | return false; |
16343 | } | |
16344 | mpz_init(val); | |
fcbea5e4 | 16345 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16346 | break; |
16347 | ||
16348 | default: | |
16349 | go_unreachable(); | |
16350 | } | |
16351 | ||
16352 | bool ret; | |
16353 | if (type->is_abstract()) | |
16354 | ret = true; | |
16355 | else | |
16356 | { | |
16357 | int bits = mpz_sizeinbase(val, 2); | |
16358 | if (type->is_unsigned()) | |
16359 | { | |
16360 | // For an unsigned type we can only accept a nonnegative | |
16361 | // number, and we must be able to represents at least BITS. | |
16362 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
16363 | } | |
16364 | else | |
16365 | { | |
16366 | // For a signed type we need an extra bit to indicate the | |
16367 | // sign. We have to handle the most negative integer | |
16368 | // specially. | |
16369 | ret = (bits + 1 <= type->bits() | |
16370 | || (bits <= type->bits() | |
16371 | && mpz_sgn(val) < 0 | |
16372 | && (mpz_scan1(val, 0) | |
16373 | == static_cast<unsigned long>(type->bits() - 1)) | |
16374 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
16375 | } | |
16376 | } | |
16377 | ||
16378 | if (!ret && issue_error) | |
71a45216 | 16379 | { |
631d5788 | 16380 | go_error_at(location, "integer constant overflow"); |
71a45216 | 16381 | this->set_invalid(); |
16382 | } | |
0c77715b | 16383 | |
16384 | return ret; | |
16385 | } | |
16386 | ||
16387 | // Check whether the constant can be expressed in a floating point | |
16388 | // type. | |
16389 | ||
16390 | bool | |
16391 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 16392 | Location location) |
0c77715b | 16393 | { |
16394 | mpfr_t val; | |
16395 | switch (this->classification_) | |
16396 | { | |
16397 | case NC_INT: | |
16398 | case NC_RUNE: | |
16399 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
16400 | break; | |
16401 | ||
16402 | case NC_FLOAT: | |
16403 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
16404 | break; | |
16405 | ||
16406 | case NC_COMPLEX: | |
fcbea5e4 | 16407 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16408 | { |
16409 | if (issue_error) | |
71a45216 | 16410 | { |
16411 | this->set_invalid(); | |
631d5788 | 16412 | go_error_at(location, "complex constant truncated to float"); |
71a45216 | 16413 | } |
0c77715b | 16414 | return false; |
16415 | } | |
fcbea5e4 | 16416 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16417 | break; |
16418 | ||
16419 | default: | |
16420 | go_unreachable(); | |
16421 | } | |
16422 | ||
16423 | bool ret; | |
16424 | if (type->is_abstract()) | |
16425 | ret = true; | |
16426 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
16427 | { | |
16428 | // A NaN or Infinity always fits in the range of the type. | |
16429 | ret = true; | |
16430 | } | |
16431 | else | |
16432 | { | |
16433 | mp_exp_t exp = mpfr_get_exp(val); | |
16434 | mp_exp_t max_exp; | |
16435 | switch (type->bits()) | |
16436 | { | |
16437 | case 32: | |
16438 | max_exp = 128; | |
16439 | break; | |
16440 | case 64: | |
16441 | max_exp = 1024; | |
16442 | break; | |
16443 | default: | |
16444 | go_unreachable(); | |
16445 | } | |
16446 | ||
16447 | ret = exp <= max_exp; | |
d0bcce51 | 16448 | |
16449 | if (ret) | |
16450 | { | |
16451 | // Round the constant to the desired type. | |
16452 | mpfr_t t; | |
16453 | mpfr_init(t); | |
16454 | switch (type->bits()) | |
16455 | { | |
16456 | case 32: | |
16457 | mpfr_set_prec(t, 24); | |
16458 | break; | |
16459 | case 64: | |
16460 | mpfr_set_prec(t, 53); | |
16461 | break; | |
16462 | default: | |
16463 | go_unreachable(); | |
16464 | } | |
16465 | mpfr_set(t, val, GMP_RNDN); | |
16466 | mpfr_set(val, t, GMP_RNDN); | |
16467 | mpfr_clear(t); | |
16468 | ||
16469 | this->set_float(type, val); | |
16470 | } | |
0c77715b | 16471 | } |
16472 | ||
16473 | mpfr_clear(val); | |
16474 | ||
16475 | if (!ret && issue_error) | |
71a45216 | 16476 | { |
631d5788 | 16477 | go_error_at(location, "floating point constant overflow"); |
71a45216 | 16478 | this->set_invalid(); |
16479 | } | |
0c77715b | 16480 | |
16481 | return ret; | |
16482 | } | |
16483 | ||
16484 | // Check whether the constant can be expressed in a complex type. | |
16485 | ||
16486 | bool | |
16487 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 16488 | Location location) |
0c77715b | 16489 | { |
16490 | if (type->is_abstract()) | |
16491 | return true; | |
16492 | ||
16493 | mp_exp_t max_exp; | |
16494 | switch (type->bits()) | |
16495 | { | |
16496 | case 64: | |
16497 | max_exp = 128; | |
16498 | break; | |
16499 | case 128: | |
16500 | max_exp = 1024; | |
16501 | break; | |
16502 | default: | |
16503 | go_unreachable(); | |
16504 | } | |
16505 | ||
fcbea5e4 | 16506 | mpc_t val; |
16507 | mpc_init2(val, mpc_precision); | |
0c77715b | 16508 | switch (this->classification_) |
16509 | { | |
16510 | case NC_INT: | |
16511 | case NC_RUNE: | |
fcbea5e4 | 16512 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16513 | break; |
16514 | ||
16515 | case NC_FLOAT: | |
fcbea5e4 | 16516 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16517 | break; |
16518 | ||
16519 | case NC_COMPLEX: | |
fcbea5e4 | 16520 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16521 | break; |
16522 | ||
16523 | default: | |
16524 | go_unreachable(); | |
16525 | } | |
16526 | ||
d0bcce51 | 16527 | bool ret = true; |
fcbea5e4 | 16528 | if (!mpfr_nan_p(mpc_realref(val)) |
16529 | && !mpfr_inf_p(mpc_realref(val)) | |
16530 | && !mpfr_zero_p(mpc_realref(val)) | |
16531 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 16532 | { |
16533 | if (issue_error) | |
71a45216 | 16534 | { |
631d5788 | 16535 | go_error_at(location, "complex real part overflow"); |
71a45216 | 16536 | this->set_invalid(); |
16537 | } | |
d0bcce51 | 16538 | ret = false; |
16539 | } | |
0c77715b | 16540 | |
fcbea5e4 | 16541 | if (!mpfr_nan_p(mpc_imagref(val)) |
16542 | && !mpfr_inf_p(mpc_imagref(val)) | |
16543 | && !mpfr_zero_p(mpc_imagref(val)) | |
16544 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 16545 | { |
16546 | if (issue_error) | |
71a45216 | 16547 | { |
631d5788 | 16548 | go_error_at(location, "complex imaginary part overflow"); |
71a45216 | 16549 | this->set_invalid(); |
16550 | } | |
d0bcce51 | 16551 | ret = false; |
16552 | } | |
0c77715b | 16553 | |
d0bcce51 | 16554 | if (ret) |
16555 | { | |
16556 | // Round the constant to the desired type. | |
fcbea5e4 | 16557 | mpc_t t; |
d0bcce51 | 16558 | switch (type->bits()) |
16559 | { | |
16560 | case 64: | |
fcbea5e4 | 16561 | mpc_init2(t, 24); |
d0bcce51 | 16562 | break; |
16563 | case 128: | |
fcbea5e4 | 16564 | mpc_init2(t, 53); |
d0bcce51 | 16565 | break; |
16566 | default: | |
16567 | go_unreachable(); | |
16568 | } | |
fcbea5e4 | 16569 | mpc_set(t, val, MPC_RNDNN); |
16570 | mpc_set(val, t, MPC_RNDNN); | |
16571 | mpc_clear(t); | |
d0bcce51 | 16572 | |
fcbea5e4 | 16573 | this->set_complex(type, val); |
d0bcce51 | 16574 | } |
16575 | ||
fcbea5e4 | 16576 | mpc_clear(val); |
0c77715b | 16577 | |
16578 | return ret; | |
16579 | } | |
16580 | ||
16581 | // Return an Expression for this value. | |
16582 | ||
16583 | Expression* | |
16584 | Numeric_constant::expression(Location loc) const | |
16585 | { | |
16586 | switch (this->classification_) | |
16587 | { | |
16588 | case NC_INT: | |
e67508fa | 16589 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 16590 | case NC_RUNE: |
16591 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
16592 | case NC_FLOAT: | |
16593 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
16594 | case NC_COMPLEX: | |
fcbea5e4 | 16595 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 16596 | case NC_INVALID: |
16597 | go_assert(saw_errors()); | |
16598 | return Expression::make_error(loc); | |
0c77715b | 16599 | default: |
16600 | go_unreachable(); | |
16601 | } | |
16602 | } |