]>
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 | |
c1177ba4 | 3703 | Unary_expression::check_operand_address_taken(Gogo*) |
03118c21 | 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 | ||
03118c21 | 3717 | Named_object* var = NULL; |
3718 | if (this->expr_->var_expression() != NULL) | |
3719 | var = this->expr_->var_expression()->named_object(); | |
3720 | else if (this->expr_->enclosed_var_expression() != NULL) | |
3721 | var = this->expr_->enclosed_var_expression()->variable(); | |
3722 | ||
3723 | if (this->escapes_ && var != NULL) | |
3724 | { | |
3725 | if (var->is_variable()) | |
3726 | this->escapes_ = var->var_value()->escapes(); | |
3727 | if (var->is_result_variable()) | |
3728 | this->escapes_ = var->result_var_value()->escapes(); | |
3729 | } | |
3730 | ||
3731 | this->expr_->address_taken(this->escapes_); | |
3732 | } | |
3733 | ||
e440a328 | 3734 | // If we are taking the address of a composite literal, and the |
2c809f8f | 3735 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3736 | // instead. |
3737 | ||
3738 | Expression* | |
ceeb4318 | 3739 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3740 | { |
b13c66cd | 3741 | Location loc = this->location(); |
e440a328 | 3742 | Operator op = this->op_; |
3743 | Expression* expr = this->expr_; | |
3744 | ||
3745 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3746 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3747 | ||
3748 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3749 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3750 | // analysis here? This case is found in math/unsafe.go and is | |
3751 | // therefore worth special casing. | |
3752 | if (op == OPERATOR_MULT) | |
3753 | { | |
3754 | Expression* e = expr; | |
3755 | while (e->classification() == EXPRESSION_CONVERSION) | |
3756 | { | |
3757 | Type_conversion_expression* te | |
3758 | = static_cast<Type_conversion_expression*>(e); | |
3759 | e = te->expr(); | |
3760 | } | |
3761 | ||
3762 | if (e->classification() == EXPRESSION_UNARY) | |
3763 | { | |
3764 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3765 | if (ue->op_ == OPERATOR_AND) | |
3766 | { | |
3767 | if (e == expr) | |
3768 | { | |
3769 | // *&x == x. | |
f4dea966 | 3770 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3771 | { | |
631d5788 | 3772 | go_error_at(ue->location(), |
3773 | "invalid operand for unary %<&%>"); | |
f4dea966 | 3774 | this->set_is_error(); |
3775 | } | |
e440a328 | 3776 | return ue->expr_; |
3777 | } | |
3778 | ue->set_does_not_escape(); | |
3779 | } | |
3780 | } | |
3781 | } | |
3782 | ||
55661ce9 | 3783 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3784 | // having to deal with TYPE_VOID in other places. | |
3785 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3786 | { | |
631d5788 | 3787 | go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); |
55661ce9 | 3788 | return Expression::make_error(this->location()); |
3789 | } | |
3790 | ||
d9f3743a | 3791 | // Check for an invalid pointer dereference. We need to do this |
3792 | // here because Unary_expression::do_type will return an error type | |
3793 | // in this case. That can cause code to appear erroneous, and | |
3794 | // therefore disappear at lowering time, without any error message. | |
3795 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3796 | { | |
3797 | this->report_error(_("expected pointer")); | |
3798 | return Expression::make_error(this->location()); | |
3799 | } | |
3800 | ||
59a401fe | 3801 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3802 | { |
0c77715b | 3803 | Numeric_constant nc; |
3804 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3805 | { |
0c77715b | 3806 | Numeric_constant result; |
af7a5274 | 3807 | bool issued_error; |
3808 | if (Unary_expression::eval_constant(op, &nc, loc, &result, | |
3809 | &issued_error)) | |
0c77715b | 3810 | return result.expression(loc); |
af7a5274 | 3811 | else if (issued_error) |
3812 | return Expression::make_error(this->location()); | |
e440a328 | 3813 | } |
3814 | } | |
3815 | ||
3816 | return this; | |
3817 | } | |
3818 | ||
f9ca30f9 | 3819 | // Flatten expression if a nil check must be performed and create temporary |
3820 | // variables if necessary. | |
3821 | ||
3822 | Expression* | |
3823 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3824 | Statement_inserter* inserter) | |
3825 | { | |
5bf8be8b | 3826 | if (this->is_error_expression() |
3827 | || this->expr_->is_error_expression() | |
3828 | || this->expr_->type()->is_error_type()) | |
3829 | { | |
3830 | go_assert(saw_errors()); | |
3831 | return Expression::make_error(this->location()); | |
3832 | } | |
f4dea966 | 3833 | |
f9ca30f9 | 3834 | Location location = this->location(); |
3835 | if (this->op_ == OPERATOR_MULT | |
3836 | && !this->expr_->is_variable()) | |
3837 | { | |
3838 | go_assert(this->expr_->type()->points_to() != NULL); | |
f614ea8b | 3839 | switch (this->requires_nil_check(gogo)) |
f9ca30f9 | 3840 | { |
f614ea8b | 3841 | case NIL_CHECK_ERROR_ENCOUNTERED: |
2a305b85 | 3842 | { |
3843 | go_assert(saw_errors()); | |
3844 | return Expression::make_error(this->location()); | |
3845 | } | |
f614ea8b | 3846 | case NIL_CHECK_NOT_NEEDED: |
3847 | break; | |
3848 | case NIL_CHECK_NEEDED: | |
3849 | this->create_temp_ = true; | |
3850 | break; | |
3851 | case NIL_CHECK_DEFAULT: | |
3852 | go_unreachable(); | |
f9ca30f9 | 3853 | } |
3854 | } | |
3855 | ||
3856 | if (this->create_temp_ && !this->expr_->is_variable()) | |
3857 | { | |
3858 | Temporary_statement* temp = | |
3859 | Statement::make_temporary(NULL, this->expr_, location); | |
3860 | inserter->insert(temp); | |
3861 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3862 | } | |
3863 | ||
3864 | return this; | |
3865 | } | |
3866 | ||
e440a328 | 3867 | // Return whether a unary expression is a constant. |
3868 | ||
3869 | bool | |
3870 | Unary_expression::do_is_constant() const | |
3871 | { | |
3872 | if (this->op_ == OPERATOR_MULT) | |
3873 | { | |
3874 | // Indirecting through a pointer is only constant if the object | |
3875 | // to which the expression points is constant, but we currently | |
3876 | // have no way to determine that. | |
3877 | return false; | |
3878 | } | |
3879 | else if (this->op_ == OPERATOR_AND) | |
3880 | { | |
3881 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3882 | // global variable, not constant otherwise. In other cases taking the |
3883 | // address is probably not a constant. | |
e440a328 | 3884 | Var_expression* ve = this->expr_->var_expression(); |
3885 | if (ve != NULL) | |
3886 | { | |
3887 | Named_object* no = ve->named_object(); | |
3888 | return no->is_variable() && no->var_value()->is_global(); | |
3889 | } | |
3890 | return false; | |
3891 | } | |
3892 | else | |
3893 | return this->expr_->is_constant(); | |
3894 | } | |
3895 | ||
3ae06f68 | 3896 | // Return whether a unary expression can be used as a constant |
3897 | // initializer. | |
3898 | ||
3899 | bool | |
3900 | Unary_expression::do_is_static_initializer() const | |
3901 | { | |
3902 | if (this->op_ == OPERATOR_MULT) | |
3903 | return false; | |
3904 | else if (this->op_ == OPERATOR_AND) | |
de048538 | 3905 | return Unary_expression::base_is_static_initializer(this->expr_); |
3906 | else | |
3907 | return this->expr_->is_static_initializer(); | |
3908 | } | |
3ae06f68 | 3909 | |
de048538 | 3910 | // Return whether the address of EXPR can be used as a static |
3911 | // initializer. | |
3ae06f68 | 3912 | |
de048538 | 3913 | bool |
3914 | Unary_expression::base_is_static_initializer(Expression* expr) | |
3915 | { | |
3916 | // The address of a field reference can be a static initializer if | |
3917 | // the base can be a static initializer. | |
3918 | Field_reference_expression* fre = expr->field_reference_expression(); | |
3919 | if (fre != NULL) | |
3920 | return Unary_expression::base_is_static_initializer(fre->expr()); | |
3921 | ||
3922 | // The address of an index expression can be a static initializer if | |
3923 | // the base can be a static initializer and the index is constant. | |
3924 | Array_index_expression* aind = expr->array_index_expression(); | |
3925 | if (aind != NULL) | |
3926 | return (aind->end() == NULL | |
3927 | && aind->start()->is_constant() | |
3928 | && Unary_expression::base_is_static_initializer(aind->array())); | |
3929 | ||
3930 | // The address of a global variable can be a static initializer. | |
3931 | Var_expression* ve = expr->var_expression(); | |
3932 | if (ve != NULL) | |
3933 | { | |
3934 | Named_object* no = ve->named_object(); | |
3935 | return no->is_variable() && no->var_value()->is_global(); | |
3936 | } | |
3937 | ||
3938 | // The address of a composite literal can be used as a static | |
3939 | // initializer if the composite literal is itself usable as a | |
3940 | // static initializer. | |
3941 | if (expr->is_composite_literal() && expr->is_static_initializer()) | |
3942 | return true; | |
3ae06f68 | 3943 | |
de048538 | 3944 | // The address of a string constant can be used as a static |
3945 | // initializer. This can not be written in Go itself but this is | |
3946 | // used when building a type descriptor. | |
3947 | if (expr->string_expression() != NULL) | |
3948 | return true; | |
3949 | ||
3950 | return false; | |
3ae06f68 | 3951 | } |
3952 | ||
f614ea8b | 3953 | // Return whether this dereference expression requires an explicit nil |
3954 | // check. If we are dereferencing the pointer to a large struct | |
3955 | // (greater than the specified size threshold), we need to check for | |
3956 | // nil. We don't bother to check for small structs because we expect | |
3957 | // the system to crash on a nil pointer dereference. However, if we | |
3958 | // know the address of this expression is being taken, we must always | |
3959 | // check for nil. | |
3960 | Unary_expression::Nil_check_classification | |
3961 | Unary_expression::requires_nil_check(Gogo* gogo) | |
3962 | { | |
3963 | go_assert(this->op_ == OPERATOR_MULT); | |
3964 | go_assert(this->expr_->type()->points_to() != NULL); | |
3965 | ||
3966 | if (this->issue_nil_check_ == NIL_CHECK_NEEDED) | |
3967 | return NIL_CHECK_NEEDED; | |
3968 | else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED) | |
3969 | return NIL_CHECK_NOT_NEEDED; | |
3970 | ||
3971 | Type* ptype = this->expr_->type()->points_to(); | |
3972 | int64_t type_size = -1; | |
3973 | if (!ptype->is_void_type()) | |
3974 | { | |
3975 | bool ok = ptype->backend_type_size(gogo, &type_size); | |
3976 | if (!ok) | |
3977 | return NIL_CHECK_ERROR_ENCOUNTERED; | |
3978 | } | |
3979 | ||
3980 | int64_t size_cutoff = gogo->nil_check_size_threshold(); | |
3981 | if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff)) | |
3982 | this->issue_nil_check_ = NIL_CHECK_NEEDED; | |
3983 | else | |
3984 | this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED; | |
3985 | return this->issue_nil_check_; | |
3986 | } | |
3987 | ||
0c77715b | 3988 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
af7a5274 | 3989 | // could be done, false if not. On overflow, issues an error and sets |
3990 | // *ISSUED_ERROR. | |
e440a328 | 3991 | |
3992 | bool | |
0c77715b | 3993 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
af7a5274 | 3994 | Location location, Numeric_constant* nc, |
3995 | bool* issued_error) | |
e440a328 | 3996 | { |
af7a5274 | 3997 | *issued_error = false; |
e440a328 | 3998 | switch (op) |
3999 | { | |
4000 | case OPERATOR_PLUS: | |
0c77715b | 4001 | *nc = *unc; |
e440a328 | 4002 | return true; |
0c77715b | 4003 | |
e440a328 | 4004 | case OPERATOR_MINUS: |
0c77715b | 4005 | if (unc->is_int() || unc->is_rune()) |
4006 | break; | |
4007 | else if (unc->is_float()) | |
4008 | { | |
4009 | mpfr_t uval; | |
4010 | unc->get_float(&uval); | |
4011 | mpfr_t val; | |
4012 | mpfr_init(val); | |
4013 | mpfr_neg(val, uval, GMP_RNDN); | |
4014 | nc->set_float(unc->type(), val); | |
4015 | mpfr_clear(uval); | |
4016 | mpfr_clear(val); | |
4017 | return true; | |
4018 | } | |
4019 | else if (unc->is_complex()) | |
4020 | { | |
fcbea5e4 | 4021 | mpc_t uval; |
4022 | unc->get_complex(&uval); | |
4023 | mpc_t val; | |
4024 | mpc_init2(val, mpc_precision); | |
4025 | mpc_neg(val, uval, MPC_RNDNN); | |
4026 | nc->set_complex(unc->type(), val); | |
4027 | mpc_clear(uval); | |
4028 | mpc_clear(val); | |
0c77715b | 4029 | return true; |
4030 | } | |
e440a328 | 4031 | else |
0c77715b | 4032 | go_unreachable(); |
e440a328 | 4033 | |
0c77715b | 4034 | case OPERATOR_XOR: |
4035 | break; | |
68448d53 | 4036 | |
59a401fe | 4037 | case OPERATOR_NOT: |
e440a328 | 4038 | case OPERATOR_AND: |
4039 | case OPERATOR_MULT: | |
4040 | return false; | |
0c77715b | 4041 | |
e440a328 | 4042 | default: |
c3e6f413 | 4043 | go_unreachable(); |
e440a328 | 4044 | } |
e440a328 | 4045 | |
0c77715b | 4046 | if (!unc->is_int() && !unc->is_rune()) |
4047 | return false; | |
4048 | ||
4049 | mpz_t uval; | |
8387e1df | 4050 | if (unc->is_rune()) |
4051 | unc->get_rune(&uval); | |
4052 | else | |
4053 | unc->get_int(&uval); | |
0c77715b | 4054 | mpz_t val; |
4055 | mpz_init(val); | |
e440a328 | 4056 | |
e440a328 | 4057 | switch (op) |
4058 | { | |
e440a328 | 4059 | case OPERATOR_MINUS: |
0c77715b | 4060 | mpz_neg(val, uval); |
4061 | break; | |
4062 | ||
e440a328 | 4063 | case OPERATOR_NOT: |
0c77715b | 4064 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
4065 | break; | |
4066 | ||
e440a328 | 4067 | case OPERATOR_XOR: |
0c77715b | 4068 | { |
4069 | Type* utype = unc->type(); | |
4070 | if (utype->integer_type() == NULL | |
4071 | || utype->integer_type()->is_abstract()) | |
4072 | mpz_com(val, uval); | |
4073 | else | |
4074 | { | |
4075 | // The number of HOST_WIDE_INTs that it takes to represent | |
4076 | // UVAL. | |
4077 | size_t count = ((mpz_sizeinbase(uval, 2) | |
4078 | + HOST_BITS_PER_WIDE_INT | |
4079 | - 1) | |
4080 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 4081 | |
0c77715b | 4082 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
4083 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
4084 | ||
4085 | size_t obits = utype->integer_type()->bits(); | |
4086 | ||
4087 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
4088 | { | |
4089 | mpz_t adj; | |
4090 | mpz_init_set_ui(adj, 1); | |
4091 | mpz_mul_2exp(adj, adj, obits); | |
4092 | mpz_add(uval, uval, adj); | |
4093 | mpz_clear(adj); | |
4094 | } | |
4095 | ||
4096 | size_t ecount; | |
4097 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
4098 | go_assert(ecount <= count); | |
4099 | ||
4100 | // Trim down to the number of words required by the type. | |
4101 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
4102 | / HOST_BITS_PER_WIDE_INT); | |
4103 | go_assert(ocount <= count); | |
4104 | ||
4105 | for (size_t i = 0; i < ocount; ++i) | |
4106 | phwi[i] = ~phwi[i]; | |
4107 | ||
4108 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
4109 | if (clearbits != 0) | |
4110 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
4111 | >> clearbits); | |
4112 | ||
4113 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
4114 | ||
4115 | if (!utype->integer_type()->is_unsigned() | |
4116 | && mpz_tstbit(val, obits - 1)) | |
4117 | { | |
4118 | mpz_t adj; | |
4119 | mpz_init_set_ui(adj, 1); | |
4120 | mpz_mul_2exp(adj, adj, obits); | |
4121 | mpz_sub(val, val, adj); | |
4122 | mpz_clear(adj); | |
4123 | } | |
4124 | ||
4125 | delete[] phwi; | |
4126 | } | |
4127 | } | |
4128 | break; | |
e440a328 | 4129 | |
e440a328 | 4130 | default: |
c3e6f413 | 4131 | go_unreachable(); |
e440a328 | 4132 | } |
e440a328 | 4133 | |
0c77715b | 4134 | if (unc->is_rune()) |
4135 | nc->set_rune(NULL, val); | |
e440a328 | 4136 | else |
0c77715b | 4137 | nc->set_int(NULL, val); |
e440a328 | 4138 | |
0c77715b | 4139 | mpz_clear(uval); |
4140 | mpz_clear(val); | |
e440a328 | 4141 | |
af7a5274 | 4142 | if (!nc->set_type(unc->type(), true, location)) |
4143 | { | |
4144 | *issued_error = true; | |
4145 | return false; | |
4146 | } | |
4147 | return true; | |
e440a328 | 4148 | } |
4149 | ||
0c77715b | 4150 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 4151 | |
4152 | bool | |
0c77715b | 4153 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 4154 | { |
0c77715b | 4155 | Numeric_constant unc; |
4156 | if (!this->expr_->numeric_constant_value(&unc)) | |
4157 | return false; | |
af7a5274 | 4158 | bool issued_error; |
0c77715b | 4159 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), |
af7a5274 | 4160 | nc, &issued_error); |
e440a328 | 4161 | } |
4162 | ||
4163 | // Return the type of a unary expression. | |
4164 | ||
4165 | Type* | |
4166 | Unary_expression::do_type() | |
4167 | { | |
4168 | switch (this->op_) | |
4169 | { | |
4170 | case OPERATOR_PLUS: | |
4171 | case OPERATOR_MINUS: | |
4172 | case OPERATOR_NOT: | |
4173 | case OPERATOR_XOR: | |
4174 | return this->expr_->type(); | |
4175 | ||
4176 | case OPERATOR_AND: | |
4177 | return Type::make_pointer_type(this->expr_->type()); | |
4178 | ||
4179 | case OPERATOR_MULT: | |
4180 | { | |
4181 | Type* subtype = this->expr_->type(); | |
4182 | Type* points_to = subtype->points_to(); | |
4183 | if (points_to == NULL) | |
4184 | return Type::make_error_type(); | |
4185 | return points_to; | |
4186 | } | |
4187 | ||
4188 | default: | |
c3e6f413 | 4189 | go_unreachable(); |
e440a328 | 4190 | } |
4191 | } | |
4192 | ||
4193 | // Determine abstract types for a unary expression. | |
4194 | ||
4195 | void | |
4196 | Unary_expression::do_determine_type(const Type_context* context) | |
4197 | { | |
4198 | switch (this->op_) | |
4199 | { | |
4200 | case OPERATOR_PLUS: | |
4201 | case OPERATOR_MINUS: | |
4202 | case OPERATOR_NOT: | |
4203 | case OPERATOR_XOR: | |
4204 | this->expr_->determine_type(context); | |
4205 | break; | |
4206 | ||
4207 | case OPERATOR_AND: | |
4208 | // Taking the address of something. | |
4209 | { | |
4210 | Type* subtype = (context->type == NULL | |
4211 | ? NULL | |
4212 | : context->type->points_to()); | |
4213 | Type_context subcontext(subtype, false); | |
4214 | this->expr_->determine_type(&subcontext); | |
4215 | } | |
4216 | break; | |
4217 | ||
4218 | case OPERATOR_MULT: | |
4219 | // Indirecting through a pointer. | |
4220 | { | |
4221 | Type* subtype = (context->type == NULL | |
4222 | ? NULL | |
4223 | : Type::make_pointer_type(context->type)); | |
4224 | Type_context subcontext(subtype, false); | |
4225 | this->expr_->determine_type(&subcontext); | |
4226 | } | |
4227 | break; | |
4228 | ||
4229 | default: | |
c3e6f413 | 4230 | go_unreachable(); |
e440a328 | 4231 | } |
4232 | } | |
4233 | ||
4234 | // Check types for a unary expression. | |
4235 | ||
4236 | void | |
4237 | Unary_expression::do_check_types(Gogo*) | |
4238 | { | |
9fe897ef | 4239 | Type* type = this->expr_->type(); |
5c13bd80 | 4240 | if (type->is_error()) |
9fe897ef | 4241 | { |
4242 | this->set_is_error(); | |
4243 | return; | |
4244 | } | |
4245 | ||
e440a328 | 4246 | switch (this->op_) |
4247 | { | |
4248 | case OPERATOR_PLUS: | |
4249 | case OPERATOR_MINUS: | |
9fe897ef | 4250 | if (type->integer_type() == NULL |
4251 | && type->float_type() == NULL | |
4252 | && type->complex_type() == NULL) | |
4253 | this->report_error(_("expected numeric type")); | |
e440a328 | 4254 | break; |
4255 | ||
4256 | case OPERATOR_NOT: | |
59a401fe | 4257 | if (!type->is_boolean_type()) |
4258 | this->report_error(_("expected boolean type")); | |
4259 | break; | |
4260 | ||
e440a328 | 4261 | case OPERATOR_XOR: |
b3b1474e | 4262 | if (type->integer_type() == NULL) |
4263 | this->report_error(_("expected integer")); | |
e440a328 | 4264 | break; |
4265 | ||
4266 | case OPERATOR_AND: | |
4267 | if (!this->expr_->is_addressable()) | |
09ea332d | 4268 | { |
4269 | if (!this->create_temp_) | |
f4dea966 | 4270 | { |
631d5788 | 4271 | go_error_at(this->location(), "invalid operand for unary %<&%>"); |
f4dea966 | 4272 | this->set_is_error(); |
4273 | } | |
09ea332d | 4274 | } |
e440a328 | 4275 | else |
da244e59 | 4276 | this->expr_->issue_nil_check(); |
e440a328 | 4277 | break; |
4278 | ||
4279 | case OPERATOR_MULT: | |
4280 | // Indirecting through a pointer. | |
9fe897ef | 4281 | if (type->points_to() == NULL) |
4282 | this->report_error(_("expected pointer")); | |
7661d702 | 4283 | if (type->points_to()->is_error()) |
4284 | this->set_is_error(); | |
e440a328 | 4285 | break; |
4286 | ||
4287 | default: | |
c3e6f413 | 4288 | go_unreachable(); |
e440a328 | 4289 | } |
4290 | } | |
4291 | ||
ea664253 | 4292 | // Get the backend representation for a unary expression. |
e440a328 | 4293 | |
ea664253 | 4294 | Bexpression* |
4295 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4296 | { |
1b1f2abf | 4297 | Gogo* gogo = context->gogo(); |
e9d3367e | 4298 | Location loc = this->location(); |
4299 | ||
4300 | // Taking the address of a set-and-use-temporary expression requires | |
4301 | // setting the temporary and then taking the address. | |
4302 | if (this->op_ == OPERATOR_AND) | |
4303 | { | |
4304 | Set_and_use_temporary_expression* sut = | |
4305 | this->expr_->set_and_use_temporary_expression(); | |
4306 | if (sut != NULL) | |
4307 | { | |
4308 | Temporary_statement* temp = sut->temporary(); | |
4309 | Bvariable* bvar = temp->get_backend_variable(context); | |
d4e6573e | 4310 | Bexpression* bvar_expr = |
7af8e400 | 4311 | gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4312 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4313 | |
0ab48656 | 4314 | Named_object* fn = context->function(); |
4315 | go_assert(fn != NULL); | |
4316 | Bfunction* bfn = | |
4317 | fn->func_value()->get_or_make_decl(gogo, fn); | |
f9ca30f9 | 4318 | Bstatement* bassign = |
0ab48656 | 4319 | gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc); |
f9ca30f9 | 4320 | Bexpression* bvar_addr = |
4321 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4322 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4323 | } |
4324 | } | |
4325 | ||
f9ca30f9 | 4326 | Bexpression* ret; |
ea664253 | 4327 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4328 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4329 | switch (this->op_) |
4330 | { | |
4331 | case OPERATOR_PLUS: | |
f9ca30f9 | 4332 | ret = bexpr; |
4333 | break; | |
e440a328 | 4334 | |
4335 | case OPERATOR_MINUS: | |
f9ca30f9 | 4336 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4337 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4338 | break; | |
e440a328 | 4339 | |
4340 | case OPERATOR_NOT: | |
e440a328 | 4341 | case OPERATOR_XOR: |
f9ca30f9 | 4342 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4343 | break; | |
e440a328 | 4344 | |
4345 | case OPERATOR_AND: | |
09ea332d | 4346 | if (!this->create_temp_) |
4347 | { | |
4348 | // We should not see a non-constant constructor here; cases | |
4349 | // where we would see one should have been moved onto the | |
4350 | // heap at parse time. Taking the address of a nonconstant | |
4351 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4352 | |
4353 | go_assert(!this->expr_->is_composite_literal() | |
3ae06f68 | 4354 | || this->expr_->is_static_initializer()); |
24060bf9 | 4355 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4356 | { | |
4357 | Unary_expression* ue = | |
4358 | static_cast<Unary_expression*>(this->expr_); | |
4359 | go_assert(ue->op() != OPERATOR_AND); | |
4360 | } | |
09ea332d | 4361 | } |
e440a328 | 4362 | |
f23d7786 | 4363 | if (this->is_gc_root_ || this->is_slice_init_) |
76f85fd6 | 4364 | { |
19272321 | 4365 | std::string var_name; |
f23d7786 | 4366 | bool copy_to_heap = false; |
4367 | if (this->is_gc_root_) | |
4368 | { | |
4369 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4370 | // they cannot be represented as an immutable_struct in the | |
4371 | // backend. | |
19272321 | 4372 | var_name = gogo->gc_root_name(); |
f23d7786 | 4373 | } |
4374 | else | |
4375 | { | |
4376 | // Build a decl for a slice value initializer. An immutable slice | |
4377 | // value initializer may have to be copied to the heap if it | |
4378 | // contains pointers in a non-constant context. | |
19272321 | 4379 | var_name = gogo->initializer_name(); |
f23d7786 | 4380 | |
4381 | Array_type* at = this->expr_->type()->array_type(); | |
4382 | go_assert(at != NULL); | |
4383 | ||
4384 | // If we are not copying the value to the heap, we will only | |
4385 | // initialize the value once, so we can use this directly | |
4386 | // rather than copying it. In that case we can't make it | |
4387 | // read-only, because the program is permitted to change it. | |
3ae06f68 | 4388 | copy_to_heap = context->function() != NULL; |
f23d7786 | 4389 | } |
19272321 | 4390 | std::string asm_name(go_selectively_encode_id(var_name)); |
f23d7786 | 4391 | Bvariable* implicit = |
19272321 | 4392 | gogo->backend()->implicit_variable(var_name, asm_name, |
438b4bec | 4393 | btype, true, copy_to_heap, |
4394 | false, 0); | |
19272321 | 4395 | gogo->backend()->implicit_variable_set_init(implicit, var_name, btype, |
aa5ae575 | 4396 | true, copy_to_heap, false, |
4397 | bexpr); | |
7af8e400 | 4398 | bexpr = gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4399 | |
4400 | // If we are not copying a slice initializer to the heap, | |
4401 | // then it can be changed by the program, so if it can | |
4402 | // contain pointers we must register it as a GC root. | |
4403 | if (this->is_slice_init_ | |
4404 | && !copy_to_heap | |
4405 | && this->expr_->type()->has_pointer()) | |
4406 | { | |
4407 | Bexpression* root = | |
7af8e400 | 4408 | gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4409 | root = gogo->backend()->address_expression(root, loc); |
4410 | Type* type = Type::make_pointer_type(this->expr_->type()); | |
4411 | gogo->add_gc_root(Expression::make_backend(root, type, loc)); | |
4412 | } | |
76f85fd6 | 4413 | } |
4414 | else if ((this->expr_->is_composite_literal() | |
3ae06f68 | 4415 | || this->expr_->string_expression() != NULL) |
4416 | && this->expr_->is_static_initializer()) | |
f9ca30f9 | 4417 | { |
19272321 | 4418 | std::string var_name(gogo->initializer_name()); |
4419 | std::string asm_name(go_selectively_encode_id(var_name)); | |
f9ca30f9 | 4420 | Bvariable* decl = |
19272321 | 4421 | gogo->backend()->immutable_struct(var_name, asm_name, |
438b4bec | 4422 | true, false, btype, loc); |
19272321 | 4423 | gogo->backend()->immutable_struct_set_init(decl, var_name, true, |
4424 | false, btype, loc, bexpr); | |
7af8e400 | 4425 | bexpr = gogo->backend()->var_expression(decl, loc); |
f9ca30f9 | 4426 | } |
09ea332d | 4427 | |
f9ca30f9 | 4428 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4429 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4430 | break; | |
e440a328 | 4431 | |
4432 | case OPERATOR_MULT: | |
4433 | { | |
f9ca30f9 | 4434 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4435 | |
f614ea8b | 4436 | bool known_valid = false; |
f9ca30f9 | 4437 | Type* ptype = this->expr_->type()->points_to(); |
4438 | Btype* pbtype = ptype->get_backend(gogo); | |
f614ea8b | 4439 | switch (this->requires_nil_check(gogo)) |
4440 | { | |
4441 | case NIL_CHECK_NOT_NEEDED: | |
4442 | break; | |
4443 | case NIL_CHECK_ERROR_ENCOUNTERED: | |
2a305b85 | 4444 | { |
4445 | go_assert(saw_errors()); | |
4446 | return gogo->backend()->error_expression(); | |
4447 | } | |
f614ea8b | 4448 | case NIL_CHECK_NEEDED: |
4449 | { | |
f9ca30f9 | 4450 | go_assert(this->expr_->is_variable()); |
2dd89704 | 4451 | |
4452 | // If we're nil-checking the result of a set-and-use-temporary | |
4453 | // expression, then pick out the target temp and use that | |
4454 | // for the final result of the conditional. | |
4455 | Bexpression* tbexpr = bexpr; | |
4456 | Bexpression* ubexpr = bexpr; | |
4457 | Set_and_use_temporary_expression* sut = | |
4458 | this->expr_->set_and_use_temporary_expression(); | |
4459 | if (sut != NULL) { | |
4460 | Temporary_statement* temp = sut->temporary(); | |
4461 | Bvariable* bvar = temp->get_backend_variable(context); | |
4462 | ubexpr = gogo->backend()->var_expression(bvar, loc); | |
4463 | } | |
ea664253 | 4464 | Bexpression* nil = |
f614ea8b | 4465 | Expression::make_nil(loc)->get_backend(context); |
f9ca30f9 | 4466 | Bexpression* compare = |
2dd89704 | 4467 | gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr, |
f9ca30f9 | 4468 | nil, loc); |
f9ca30f9 | 4469 | Bexpression* crash = |
f614ea8b | 4470 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4471 | loc)->get_backend(context); | |
93715b75 | 4472 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
4473 | bexpr = gogo->backend()->conditional_expression(bfn, btype, | |
4474 | compare, | |
2dd89704 | 4475 | crash, ubexpr, |
f9ca30f9 | 4476 | loc); |
f614ea8b | 4477 | known_valid = true; |
4478 | break; | |
4479 | } | |
4480 | case NIL_CHECK_DEFAULT: | |
4481 | go_unreachable(); | |
4482 | } | |
4483 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, | |
4484 | known_valid, loc); | |
e440a328 | 4485 | } |
f9ca30f9 | 4486 | break; |
e440a328 | 4487 | |
4488 | default: | |
c3e6f413 | 4489 | go_unreachable(); |
e440a328 | 4490 | } |
f9ca30f9 | 4491 | |
ea664253 | 4492 | return ret; |
e440a328 | 4493 | } |
4494 | ||
4495 | // Export a unary expression. | |
4496 | ||
4497 | void | |
4498 | Unary_expression::do_export(Export* exp) const | |
4499 | { | |
4500 | switch (this->op_) | |
4501 | { | |
4502 | case OPERATOR_PLUS: | |
4503 | exp->write_c_string("+ "); | |
4504 | break; | |
4505 | case OPERATOR_MINUS: | |
4506 | exp->write_c_string("- "); | |
4507 | break; | |
4508 | case OPERATOR_NOT: | |
4509 | exp->write_c_string("! "); | |
4510 | break; | |
4511 | case OPERATOR_XOR: | |
4512 | exp->write_c_string("^ "); | |
4513 | break; | |
4514 | case OPERATOR_AND: | |
4515 | case OPERATOR_MULT: | |
4516 | default: | |
c3e6f413 | 4517 | go_unreachable(); |
e440a328 | 4518 | } |
4519 | this->expr_->export_expression(exp); | |
4520 | } | |
4521 | ||
4522 | // Import a unary expression. | |
4523 | ||
4524 | Expression* | |
4525 | Unary_expression::do_import(Import* imp) | |
4526 | { | |
4527 | Operator op; | |
4528 | switch (imp->get_char()) | |
4529 | { | |
4530 | case '+': | |
4531 | op = OPERATOR_PLUS; | |
4532 | break; | |
4533 | case '-': | |
4534 | op = OPERATOR_MINUS; | |
4535 | break; | |
4536 | case '!': | |
4537 | op = OPERATOR_NOT; | |
4538 | break; | |
4539 | case '^': | |
4540 | op = OPERATOR_XOR; | |
4541 | break; | |
4542 | default: | |
c3e6f413 | 4543 | go_unreachable(); |
e440a328 | 4544 | } |
4545 | imp->require_c_string(" "); | |
4546 | Expression* expr = Expression::import_expression(imp); | |
4547 | return Expression::make_unary(op, expr, imp->location()); | |
4548 | } | |
4549 | ||
d751bb78 | 4550 | // Dump ast representation of an unary expression. |
4551 | ||
4552 | void | |
4553 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4554 | { | |
4555 | ast_dump_context->dump_operator(this->op_); | |
4556 | ast_dump_context->ostream() << "("; | |
4557 | ast_dump_context->dump_expression(this->expr_); | |
4558 | ast_dump_context->ostream() << ") "; | |
4559 | } | |
4560 | ||
e440a328 | 4561 | // Make a unary expression. |
4562 | ||
4563 | Expression* | |
b13c66cd | 4564 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4565 | { |
4566 | return new Unary_expression(op, expr, location); | |
4567 | } | |
4568 | ||
f614ea8b | 4569 | Expression* |
4570 | Expression::make_dereference(Expression* ptr, | |
4571 | Nil_check_classification docheck, | |
4572 | Location location) | |
4573 | { | |
4574 | Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location); | |
4575 | if (docheck == NIL_CHECK_NEEDED) | |
4576 | deref->unary_expression()->set_requires_nil_check(true); | |
4577 | else if (docheck == NIL_CHECK_NOT_NEEDED) | |
4578 | deref->unary_expression()->set_requires_nil_check(false); | |
4579 | return deref; | |
4580 | } | |
4581 | ||
e440a328 | 4582 | // If this is an indirection through a pointer, return the expression |
4583 | // being pointed through. Otherwise return this. | |
4584 | ||
4585 | Expression* | |
4586 | Expression::deref() | |
4587 | { | |
4588 | if (this->classification_ == EXPRESSION_UNARY) | |
4589 | { | |
4590 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4591 | if (ue->op() == OPERATOR_MULT) | |
4592 | return ue->operand(); | |
4593 | } | |
4594 | return this; | |
4595 | } | |
4596 | ||
4597 | // Class Binary_expression. | |
4598 | ||
4599 | // Traversal. | |
4600 | ||
4601 | int | |
4602 | Binary_expression::do_traverse(Traverse* traverse) | |
4603 | { | |
4604 | int t = Expression::traverse(&this->left_, traverse); | |
4605 | if (t == TRAVERSE_EXIT) | |
4606 | return TRAVERSE_EXIT; | |
4607 | return Expression::traverse(&this->right_, traverse); | |
4608 | } | |
4609 | ||
3ae06f68 | 4610 | // Return whether this expression may be used as a static initializer. |
4611 | ||
4612 | bool | |
4613 | Binary_expression::do_is_static_initializer() const | |
4614 | { | |
4615 | if (!this->left_->is_static_initializer() | |
4616 | || !this->right_->is_static_initializer()) | |
4617 | return false; | |
4618 | ||
4619 | // Addresses can be static initializers, but we can't implement | |
4620 | // arbitray binary expressions of them. | |
4621 | Unary_expression* lu = this->left_->unary_expression(); | |
4622 | Unary_expression* ru = this->right_->unary_expression(); | |
4623 | if (lu != NULL && lu->op() == OPERATOR_AND) | |
4624 | { | |
4625 | if (ru != NULL && ru->op() == OPERATOR_AND) | |
4626 | return this->op_ == OPERATOR_MINUS; | |
4627 | else | |
4628 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4629 | } | |
4630 | else if (ru != NULL && ru->op() == OPERATOR_AND) | |
4631 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4632 | ||
4633 | // Other cases should resolve in the backend. | |
4634 | return true; | |
4635 | } | |
4636 | ||
0c77715b | 4637 | // Return the type to use for a binary operation on operands of |
4638 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4639 | // such may be NULL or abstract. | |
4640 | ||
4641 | bool | |
4642 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4643 | Type* right_type, Type** result_type) | |
4644 | { | |
4645 | if (left_type != right_type | |
4646 | && !left_type->is_abstract() | |
4647 | && !right_type->is_abstract() | |
4648 | && left_type->base() != right_type->base() | |
4649 | && op != OPERATOR_LSHIFT | |
4650 | && op != OPERATOR_RSHIFT) | |
4651 | { | |
4652 | // May be a type error--let it be diagnosed elsewhere. | |
4653 | return false; | |
4654 | } | |
4655 | ||
4656 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4657 | { | |
4658 | if (left_type->integer_type() != NULL) | |
4659 | *result_type = left_type; | |
4660 | else | |
4661 | *result_type = Type::make_abstract_integer_type(); | |
4662 | } | |
4663 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4664 | *result_type = left_type; | |
4665 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4666 | *result_type = right_type; | |
4667 | else if (!left_type->is_abstract()) | |
4668 | *result_type = left_type; | |
4669 | else if (!right_type->is_abstract()) | |
4670 | *result_type = right_type; | |
4671 | else if (left_type->complex_type() != NULL) | |
4672 | *result_type = left_type; | |
4673 | else if (right_type->complex_type() != NULL) | |
4674 | *result_type = right_type; | |
4675 | else if (left_type->float_type() != NULL) | |
4676 | *result_type = left_type; | |
4677 | else if (right_type->float_type() != NULL) | |
4678 | *result_type = right_type; | |
4679 | else if (left_type->integer_type() != NULL | |
4680 | && left_type->integer_type()->is_rune()) | |
4681 | *result_type = left_type; | |
4682 | else if (right_type->integer_type() != NULL | |
4683 | && right_type->integer_type()->is_rune()) | |
4684 | *result_type = right_type; | |
4685 | else | |
4686 | *result_type = left_type; | |
4687 | ||
4688 | return true; | |
4689 | } | |
4690 | ||
4691 | // Convert an integer comparison code and an operator to a boolean | |
4692 | // value. | |
e440a328 | 4693 | |
4694 | bool | |
0c77715b | 4695 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4696 | { |
e440a328 | 4697 | switch (op) |
4698 | { | |
4699 | case OPERATOR_EQEQ: | |
0c77715b | 4700 | return cmp == 0; |
4701 | break; | |
e440a328 | 4702 | case OPERATOR_NOTEQ: |
0c77715b | 4703 | return cmp != 0; |
4704 | break; | |
e440a328 | 4705 | case OPERATOR_LT: |
0c77715b | 4706 | return cmp < 0; |
4707 | break; | |
e440a328 | 4708 | case OPERATOR_LE: |
0c77715b | 4709 | return cmp <= 0; |
e440a328 | 4710 | case OPERATOR_GT: |
0c77715b | 4711 | return cmp > 0; |
e440a328 | 4712 | case OPERATOR_GE: |
0c77715b | 4713 | return cmp >= 0; |
e440a328 | 4714 | default: |
c3e6f413 | 4715 | go_unreachable(); |
e440a328 | 4716 | } |
4717 | } | |
4718 | ||
0c77715b | 4719 | // Compare constants according to OP. |
e440a328 | 4720 | |
4721 | bool | |
0c77715b | 4722 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4723 | Numeric_constant* right_nc, | |
4724 | Location location, bool* result) | |
e440a328 | 4725 | { |
0c77715b | 4726 | Type* left_type = left_nc->type(); |
4727 | Type* right_type = right_nc->type(); | |
4728 | ||
4729 | Type* type; | |
4730 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4731 | return false; | |
4732 | ||
4733 | // When comparing an untyped operand to a typed operand, we are | |
4734 | // effectively coercing the untyped operand to the other operand's | |
4735 | // type, so make sure that is valid. | |
4736 | if (!left_nc->set_type(type, true, location) | |
4737 | || !right_nc->set_type(type, true, location)) | |
4738 | return false; | |
4739 | ||
4740 | bool ret; | |
4741 | int cmp; | |
4742 | if (type->complex_type() != NULL) | |
4743 | { | |
4744 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4745 | return false; | |
4746 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4747 | } | |
4748 | else if (type->float_type() != NULL) | |
4749 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4750 | else |
0c77715b | 4751 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4752 | ||
4753 | if (ret) | |
4754 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4755 | ||
4756 | return ret; | |
4757 | } | |
4758 | ||
4759 | // Compare integer constants. | |
4760 | ||
4761 | bool | |
4762 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4763 | const Numeric_constant* right_nc, | |
4764 | int* cmp) | |
4765 | { | |
4766 | mpz_t left_val; | |
4767 | if (!left_nc->to_int(&left_val)) | |
4768 | return false; | |
4769 | mpz_t right_val; | |
4770 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4771 | { |
0c77715b | 4772 | mpz_clear(left_val); |
4773 | return false; | |
e440a328 | 4774 | } |
0c77715b | 4775 | |
4776 | *cmp = mpz_cmp(left_val, right_val); | |
4777 | ||
4778 | mpz_clear(left_val); | |
4779 | mpz_clear(right_val); | |
4780 | ||
4781 | return true; | |
4782 | } | |
4783 | ||
4784 | // Compare floating point constants. | |
4785 | ||
4786 | bool | |
4787 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4788 | const Numeric_constant* right_nc, | |
4789 | int* cmp) | |
4790 | { | |
4791 | mpfr_t left_val; | |
4792 | if (!left_nc->to_float(&left_val)) | |
4793 | return false; | |
4794 | mpfr_t right_val; | |
4795 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4796 | { |
0c77715b | 4797 | mpfr_clear(left_val); |
4798 | return false; | |
4799 | } | |
4800 | ||
4801 | // We already coerced both operands to the same type. If that type | |
4802 | // is not an abstract type, we need to round the values accordingly. | |
4803 | Type* type = left_nc->type(); | |
4804 | if (!type->is_abstract() && type->float_type() != NULL) | |
4805 | { | |
4806 | int bits = type->float_type()->bits(); | |
4807 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4808 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4809 | } |
0c77715b | 4810 | |
4811 | *cmp = mpfr_cmp(left_val, right_val); | |
4812 | ||
4813 | mpfr_clear(left_val); | |
4814 | mpfr_clear(right_val); | |
4815 | ||
4816 | return true; | |
e440a328 | 4817 | } |
4818 | ||
0c77715b | 4819 | // Compare complex constants. Complex numbers may only be compared |
4820 | // for equality. | |
e440a328 | 4821 | |
4822 | bool | |
0c77715b | 4823 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4824 | const Numeric_constant* right_nc, | |
4825 | int* cmp) | |
e440a328 | 4826 | { |
fcbea5e4 | 4827 | mpc_t left_val; |
4828 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4829 | return false; |
fcbea5e4 | 4830 | mpc_t right_val; |
4831 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4832 | { |
fcbea5e4 | 4833 | mpc_clear(left_val); |
0c77715b | 4834 | return false; |
e440a328 | 4835 | } |
0c77715b | 4836 | |
4837 | // We already coerced both operands to the same type. If that type | |
4838 | // is not an abstract type, we need to round the values accordingly. | |
4839 | Type* type = left_nc->type(); | |
4840 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4841 | { |
0c77715b | 4842 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4843 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4844 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4845 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4846 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4847 | } |
0c77715b | 4848 | |
fcbea5e4 | 4849 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4850 | |
fcbea5e4 | 4851 | mpc_clear(left_val); |
4852 | mpc_clear(right_val); | |
0c77715b | 4853 | |
4854 | return true; | |
e440a328 | 4855 | } |
4856 | ||
0c77715b | 4857 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4858 | // true if this could be done, false if not. Issue errors at LOCATION | |
af7a5274 | 4859 | // as appropriate, and sets *ISSUED_ERROR if it did. |
e440a328 | 4860 | |
4861 | bool | |
0c77715b | 4862 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4863 | Numeric_constant* right_nc, | |
af7a5274 | 4864 | Location location, Numeric_constant* nc, |
4865 | bool* issued_error) | |
e440a328 | 4866 | { |
af7a5274 | 4867 | *issued_error = false; |
e440a328 | 4868 | switch (op) |
4869 | { | |
4870 | case OPERATOR_OROR: | |
4871 | case OPERATOR_ANDAND: | |
4872 | case OPERATOR_EQEQ: | |
4873 | case OPERATOR_NOTEQ: | |
4874 | case OPERATOR_LT: | |
4875 | case OPERATOR_LE: | |
4876 | case OPERATOR_GT: | |
4877 | case OPERATOR_GE: | |
9767e2d3 | 4878 | // These return boolean values, not numeric. |
4879 | return false; | |
0c77715b | 4880 | default: |
4881 | break; | |
4882 | } | |
4883 | ||
4884 | Type* left_type = left_nc->type(); | |
4885 | Type* right_type = right_nc->type(); | |
4886 | ||
4887 | Type* type; | |
4888 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4889 | return false; | |
4890 | ||
4891 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4892 | ||
4893 | // When combining an untyped operand with a typed operand, we are | |
4894 | // effectively coercing the untyped operand to the other operand's | |
4895 | // type, so make sure that is valid. | |
4896 | if (!left_nc->set_type(type, true, location)) | |
4897 | return false; | |
4898 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4899 | return false; | |
85334a21 | 4900 | if (is_shift |
4901 | && ((left_type->integer_type() == NULL | |
4902 | && !left_type->is_abstract()) | |
4903 | || (right_type->integer_type() == NULL | |
4904 | && !right_type->is_abstract()))) | |
4905 | return false; | |
0c77715b | 4906 | |
4907 | bool r; | |
4908 | if (type->complex_type() != NULL) | |
4909 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4910 | else if (type->float_type() != NULL) | |
4911 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4912 | else | |
4913 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4914 | ||
4915 | if (r) | |
af7a5274 | 4916 | { |
4917 | r = nc->set_type(type, true, location); | |
4918 | if (!r) | |
4919 | *issued_error = true; | |
4920 | } | |
0c77715b | 4921 | |
4922 | return r; | |
4923 | } | |
4924 | ||
4925 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4926 | // integer operations. Return true if this could be done, false if | |
4927 | // not. | |
4928 | ||
4929 | bool | |
4930 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4931 | const Numeric_constant* right_nc, | |
4932 | Location location, Numeric_constant* nc) | |
4933 | { | |
4934 | mpz_t left_val; | |
4935 | if (!left_nc->to_int(&left_val)) | |
4936 | return false; | |
4937 | mpz_t right_val; | |
4938 | if (!right_nc->to_int(&right_val)) | |
4939 | { | |
4940 | mpz_clear(left_val); | |
e440a328 | 4941 | return false; |
0c77715b | 4942 | } |
4943 | ||
4944 | mpz_t val; | |
4945 | mpz_init(val); | |
4946 | ||
4947 | switch (op) | |
4948 | { | |
e440a328 | 4949 | case OPERATOR_PLUS: |
4950 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4951 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4952 | { | |
631d5788 | 4953 | go_error_at(location, "constant addition overflow"); |
71a45216 | 4954 | nc->set_invalid(); |
2c809f8f | 4955 | mpz_set_ui(val, 1); |
4956 | } | |
e440a328 | 4957 | break; |
4958 | case OPERATOR_MINUS: | |
4959 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4960 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4961 | { | |
631d5788 | 4962 | go_error_at(location, "constant subtraction overflow"); |
71a45216 | 4963 | nc->set_invalid(); |
2c809f8f | 4964 | mpz_set_ui(val, 1); |
4965 | } | |
e440a328 | 4966 | break; |
4967 | case OPERATOR_OR: | |
4968 | mpz_ior(val, left_val, right_val); | |
4969 | break; | |
4970 | case OPERATOR_XOR: | |
4971 | mpz_xor(val, left_val, right_val); | |
4972 | break; | |
4973 | case OPERATOR_MULT: | |
4974 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4975 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4976 | { | |
631d5788 | 4977 | go_error_at(location, "constant multiplication overflow"); |
71a45216 | 4978 | nc->set_invalid(); |
2c809f8f | 4979 | mpz_set_ui(val, 1); |
4980 | } | |
e440a328 | 4981 | break; |
4982 | case OPERATOR_DIV: | |
4983 | if (mpz_sgn(right_val) != 0) | |
4984 | mpz_tdiv_q(val, left_val, right_val); | |
4985 | else | |
4986 | { | |
631d5788 | 4987 | go_error_at(location, "division by zero"); |
71a45216 | 4988 | nc->set_invalid(); |
e440a328 | 4989 | mpz_set_ui(val, 0); |
e440a328 | 4990 | } |
4991 | break; | |
4992 | case OPERATOR_MOD: | |
4993 | if (mpz_sgn(right_val) != 0) | |
4994 | mpz_tdiv_r(val, left_val, right_val); | |
4995 | else | |
4996 | { | |
631d5788 | 4997 | go_error_at(location, "division by zero"); |
71a45216 | 4998 | nc->set_invalid(); |
e440a328 | 4999 | mpz_set_ui(val, 0); |
e440a328 | 5000 | } |
5001 | break; | |
5002 | case OPERATOR_LSHIFT: | |
5003 | { | |
5004 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 5005 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
5006 | mpz_mul_2exp(val, left_val, shift); | |
5007 | else | |
e440a328 | 5008 | { |
631d5788 | 5009 | go_error_at(location, "shift count overflow"); |
71a45216 | 5010 | nc->set_invalid(); |
2c809f8f | 5011 | mpz_set_ui(val, 1); |
e440a328 | 5012 | } |
e440a328 | 5013 | break; |
5014 | } | |
5015 | break; | |
5016 | case OPERATOR_RSHIFT: | |
5017 | { | |
5018 | unsigned long shift = mpz_get_ui(right_val); | |
5019 | if (mpz_cmp_ui(right_val, shift) != 0) | |
5020 | { | |
631d5788 | 5021 | go_error_at(location, "shift count overflow"); |
71a45216 | 5022 | nc->set_invalid(); |
2c809f8f | 5023 | mpz_set_ui(val, 1); |
e440a328 | 5024 | } |
e440a328 | 5025 | else |
0c77715b | 5026 | { |
5027 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
5028 | mpz_tdiv_q_2exp(val, left_val, shift); | |
5029 | else | |
5030 | mpz_fdiv_q_2exp(val, left_val, shift); | |
5031 | } | |
e440a328 | 5032 | break; |
5033 | } | |
5034 | break; | |
5035 | case OPERATOR_AND: | |
5036 | mpz_and(val, left_val, right_val); | |
5037 | break; | |
5038 | case OPERATOR_BITCLEAR: | |
5039 | { | |
5040 | mpz_t tval; | |
5041 | mpz_init(tval); | |
5042 | mpz_com(tval, right_val); | |
5043 | mpz_and(val, left_val, tval); | |
5044 | mpz_clear(tval); | |
5045 | } | |
5046 | break; | |
5047 | default: | |
c3e6f413 | 5048 | go_unreachable(); |
e440a328 | 5049 | } |
5050 | ||
0c77715b | 5051 | mpz_clear(left_val); |
5052 | mpz_clear(right_val); | |
e440a328 | 5053 | |
0c77715b | 5054 | if (left_nc->is_rune() |
5055 | || (op != OPERATOR_LSHIFT | |
5056 | && op != OPERATOR_RSHIFT | |
5057 | && right_nc->is_rune())) | |
5058 | nc->set_rune(NULL, val); | |
5059 | else | |
5060 | nc->set_int(NULL, val); | |
5061 | ||
5062 | mpz_clear(val); | |
e440a328 | 5063 | |
5064 | return true; | |
5065 | } | |
5066 | ||
0c77715b | 5067 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5068 | // floating point operations. Return true if this could be done, | |
5069 | // false if not. | |
e440a328 | 5070 | |
5071 | bool | |
0c77715b | 5072 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
5073 | const Numeric_constant* right_nc, | |
5074 | Location location, Numeric_constant* nc) | |
e440a328 | 5075 | { |
0c77715b | 5076 | mpfr_t left_val; |
5077 | if (!left_nc->to_float(&left_val)) | |
5078 | return false; | |
5079 | mpfr_t right_val; | |
5080 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 5081 | { |
0c77715b | 5082 | mpfr_clear(left_val); |
e440a328 | 5083 | return false; |
0c77715b | 5084 | } |
5085 | ||
5086 | mpfr_t val; | |
5087 | mpfr_init(val); | |
5088 | ||
5089 | bool ret = true; | |
5090 | switch (op) | |
5091 | { | |
e440a328 | 5092 | case OPERATOR_PLUS: |
5093 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
5094 | break; | |
5095 | case OPERATOR_MINUS: | |
5096 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
5097 | break; | |
5098 | case OPERATOR_OR: | |
5099 | case OPERATOR_XOR: | |
5100 | case OPERATOR_AND: | |
5101 | case OPERATOR_BITCLEAR: | |
0c77715b | 5102 | case OPERATOR_MOD: |
5103 | case OPERATOR_LSHIFT: | |
5104 | case OPERATOR_RSHIFT: | |
5105 | mpfr_set_ui(val, 0, GMP_RNDN); | |
5106 | ret = false; | |
5107 | break; | |
e440a328 | 5108 | case OPERATOR_MULT: |
5109 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
5110 | break; | |
5111 | case OPERATOR_DIV: | |
0c77715b | 5112 | if (!mpfr_zero_p(right_val)) |
5113 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
5114 | else | |
5115 | { | |
631d5788 | 5116 | go_error_at(location, "division by zero"); |
71a45216 | 5117 | nc->set_invalid(); |
0c77715b | 5118 | mpfr_set_ui(val, 0, GMP_RNDN); |
5119 | } | |
e440a328 | 5120 | break; |
e440a328 | 5121 | default: |
c3e6f413 | 5122 | go_unreachable(); |
e440a328 | 5123 | } |
5124 | ||
0c77715b | 5125 | mpfr_clear(left_val); |
5126 | mpfr_clear(right_val); | |
e440a328 | 5127 | |
0c77715b | 5128 | nc->set_float(NULL, val); |
5129 | mpfr_clear(val); | |
e440a328 | 5130 | |
0c77715b | 5131 | return ret; |
e440a328 | 5132 | } |
5133 | ||
0c77715b | 5134 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5135 | // complex operations. Return true if this could be done, false if | |
5136 | // not. | |
e440a328 | 5137 | |
5138 | bool | |
0c77715b | 5139 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
5140 | const Numeric_constant* right_nc, | |
5141 | Location location, Numeric_constant* nc) | |
e440a328 | 5142 | { |
fcbea5e4 | 5143 | mpc_t left_val; |
5144 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 5145 | return false; |
fcbea5e4 | 5146 | mpc_t right_val; |
5147 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 5148 | { |
fcbea5e4 | 5149 | mpc_clear(left_val); |
e440a328 | 5150 | return false; |
0c77715b | 5151 | } |
5152 | ||
fcbea5e4 | 5153 | mpc_t val; |
5154 | mpc_init2(val, mpc_precision); | |
0c77715b | 5155 | |
5156 | bool ret = true; | |
5157 | switch (op) | |
5158 | { | |
e440a328 | 5159 | case OPERATOR_PLUS: |
fcbea5e4 | 5160 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5161 | break; |
5162 | case OPERATOR_MINUS: | |
fcbea5e4 | 5163 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5164 | break; |
5165 | case OPERATOR_OR: | |
5166 | case OPERATOR_XOR: | |
5167 | case OPERATOR_AND: | |
5168 | case OPERATOR_BITCLEAR: | |
0c77715b | 5169 | case OPERATOR_MOD: |
5170 | case OPERATOR_LSHIFT: | |
5171 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 5172 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 5173 | ret = false; |
5174 | break; | |
e440a328 | 5175 | case OPERATOR_MULT: |
fcbea5e4 | 5176 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5177 | break; |
5178 | case OPERATOR_DIV: | |
fcbea5e4 | 5179 | if (mpc_cmp_si(right_val, 0) == 0) |
5180 | { | |
631d5788 | 5181 | go_error_at(location, "division by zero"); |
71a45216 | 5182 | nc->set_invalid(); |
fcbea5e4 | 5183 | mpc_set_ui(val, 0, MPC_RNDNN); |
5184 | break; | |
5185 | } | |
5186 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 5187 | break; |
e440a328 | 5188 | default: |
c3e6f413 | 5189 | go_unreachable(); |
e440a328 | 5190 | } |
5191 | ||
fcbea5e4 | 5192 | mpc_clear(left_val); |
5193 | mpc_clear(right_val); | |
e440a328 | 5194 | |
fcbea5e4 | 5195 | nc->set_complex(NULL, val); |
5196 | mpc_clear(val); | |
e440a328 | 5197 | |
0c77715b | 5198 | return ret; |
e440a328 | 5199 | } |
5200 | ||
5201 | // Lower a binary expression. We have to evaluate constant | |
5202 | // expressions now, in order to implement Go's unlimited precision | |
5203 | // constants. | |
5204 | ||
5205 | Expression* | |
e9d3367e | 5206 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5207 | Statement_inserter* inserter, int) | |
e440a328 | 5208 | { |
b13c66cd | 5209 | Location location = this->location(); |
e440a328 | 5210 | Operator op = this->op_; |
5211 | Expression* left = this->left_; | |
5212 | Expression* right = this->right_; | |
5213 | ||
5214 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5215 | || op == OPERATOR_NOTEQ | |
5216 | || op == OPERATOR_LT | |
5217 | || op == OPERATOR_LE | |
5218 | || op == OPERATOR_GT | |
5219 | || op == OPERATOR_GE); | |
5220 | ||
0c77715b | 5221 | // Numeric constant expressions. |
e440a328 | 5222 | { |
0c77715b | 5223 | Numeric_constant left_nc; |
5224 | Numeric_constant right_nc; | |
5225 | if (left->numeric_constant_value(&left_nc) | |
5226 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5227 | { |
0c77715b | 5228 | if (is_comparison) |
e440a328 | 5229 | { |
0c77715b | 5230 | bool result; |
5231 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5232 | &right_nc, location, | |
5233 | &result)) | |
5234 | return this; | |
e90c9dfc | 5235 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5236 | Expression::make_boolean(result, |
5237 | location), | |
5238 | location); | |
e440a328 | 5239 | } |
5240 | else | |
5241 | { | |
0c77715b | 5242 | Numeric_constant nc; |
af7a5274 | 5243 | bool issued_error; |
0c77715b | 5244 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, |
af7a5274 | 5245 | location, &nc, |
5246 | &issued_error)) | |
5247 | { | |
5248 | if (issued_error) | |
5249 | return Expression::make_error(location); | |
71a45216 | 5250 | return this; |
af7a5274 | 5251 | } |
0c77715b | 5252 | return nc.expression(location); |
e440a328 | 5253 | } |
5254 | } | |
e440a328 | 5255 | } |
5256 | ||
5257 | // String constant expressions. | |
315fa98d | 5258 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5259 | { |
5260 | std::string left_string; | |
5261 | std::string right_string; | |
5262 | if (left->string_constant_value(&left_string) | |
5263 | && right->string_constant_value(&right_string)) | |
315fa98d | 5264 | { |
5265 | if (op == OPERATOR_PLUS) | |
5266 | return Expression::make_string(left_string + right_string, | |
5267 | location); | |
5268 | else if (is_comparison) | |
5269 | { | |
5270 | int cmp = left_string.compare(right_string); | |
0c77715b | 5271 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5272 | return Expression::make_boolean(r, location); |
b40dc774 | 5273 | } |
5274 | } | |
b40dc774 | 5275 | } |
5276 | ||
ceeb12d7 | 5277 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5278 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5279 | { | |
b79832ca | 5280 | if (left->type()->struct_type() != NULL |
5281 | && right->type()->struct_type() != NULL) | |
e9d3367e | 5282 | return this->lower_struct_comparison(gogo, inserter); |
5283 | else if (left->type()->array_type() != NULL | |
b79832ca | 5284 | && !left->type()->is_slice_type() |
5285 | && right->type()->array_type() != NULL | |
5286 | && !right->type()->is_slice_type()) | |
e9d3367e | 5287 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 5288 | else if ((left->type()->interface_type() != NULL |
5289 | && right->type()->interface_type() == NULL) | |
5290 | || (left->type()->interface_type() == NULL | |
5291 | && right->type()->interface_type() != NULL)) | |
5292 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5293 | } |
5294 | ||
736a16ba | 5295 | // Lower string concatenation to String_concat_expression, so that |
5296 | // we can group sequences of string additions. | |
5297 | if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) | |
5298 | { | |
5299 | Expression_list* exprs; | |
5300 | String_concat_expression* left_sce = | |
5301 | this->left_->string_concat_expression(); | |
5302 | if (left_sce != NULL) | |
5303 | exprs = left_sce->exprs(); | |
5304 | else | |
5305 | { | |
5306 | exprs = new Expression_list(); | |
5307 | exprs->push_back(this->left_); | |
5308 | } | |
5309 | ||
5310 | String_concat_expression* right_sce = | |
5311 | this->right_->string_concat_expression(); | |
5312 | if (right_sce != NULL) | |
5313 | exprs->append(right_sce->exprs()); | |
5314 | else | |
5315 | exprs->push_back(this->right_); | |
5316 | ||
5317 | return Expression::make_string_concat(exprs); | |
5318 | } | |
5319 | ||
e440a328 | 5320 | return this; |
5321 | } | |
5322 | ||
e9d3367e | 5323 | // Lower a struct comparison. |
5324 | ||
5325 | Expression* | |
5326 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5327 | Statement_inserter* inserter) | |
5328 | { | |
5329 | Struct_type* st = this->left_->type()->struct_type(); | |
5330 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5331 | if (st2 == NULL) | |
5332 | return this; | |
5333 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5334 | return this; | |
5335 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5336 | this->right_->type(), NULL)) | |
5337 | return this; | |
5338 | ||
5339 | // See if we can compare using memcmp. As a heuristic, we use | |
5340 | // memcmp rather than field references and comparisons if there are | |
5341 | // more than two fields. | |
113ef6a5 | 5342 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5343 | return this->lower_compare_to_memcmp(gogo, inserter); |
5344 | ||
5345 | Location loc = this->location(); | |
5346 | ||
5347 | Expression* left = this->left_; | |
5348 | Temporary_statement* left_temp = NULL; | |
5349 | if (left->var_expression() == NULL | |
5350 | && left->temporary_reference_expression() == NULL) | |
5351 | { | |
5352 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5353 | inserter->insert(left_temp); | |
5354 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5355 | } | |
5356 | ||
5357 | Expression* right = this->right_; | |
5358 | Temporary_statement* right_temp = NULL; | |
5359 | if (right->var_expression() == NULL | |
5360 | && right->temporary_reference_expression() == NULL) | |
5361 | { | |
5362 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5363 | inserter->insert(right_temp); | |
5364 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5365 | } | |
5366 | ||
5367 | Expression* ret = Expression::make_boolean(true, loc); | |
5368 | const Struct_field_list* fields = st->fields(); | |
5369 | unsigned int field_index = 0; | |
5370 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5371 | pf != fields->end(); | |
5372 | ++pf, ++field_index) | |
5373 | { | |
f5165c05 | 5374 | if (Gogo::is_sink_name(pf->field_name())) |
5375 | continue; | |
5376 | ||
e9d3367e | 5377 | if (field_index > 0) |
5378 | { | |
5379 | if (left_temp == NULL) | |
5380 | left = left->copy(); | |
5381 | else | |
5382 | left = Expression::make_temporary_reference(left_temp, loc); | |
5383 | if (right_temp == NULL) | |
5384 | right = right->copy(); | |
5385 | else | |
5386 | right = Expression::make_temporary_reference(right_temp, loc); | |
5387 | } | |
5388 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5389 | loc); | |
5390 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5391 | loc); | |
5392 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5393 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5394 | } | |
5395 | ||
5396 | if (this->op_ == OPERATOR_NOTEQ) | |
5397 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5398 | ||
5399 | return ret; | |
5400 | } | |
5401 | ||
5402 | // Lower an array comparison. | |
5403 | ||
5404 | Expression* | |
5405 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5406 | Statement_inserter* inserter) | |
5407 | { | |
5408 | Array_type* at = this->left_->type()->array_type(); | |
5409 | Array_type* at2 = this->right_->type()->array_type(); | |
5410 | if (at2 == NULL) | |
5411 | return this; | |
5412 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5413 | return this; | |
5414 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5415 | this->right_->type(), NULL)) | |
5416 | return this; | |
5417 | ||
5418 | // Call memcmp directly if possible. This may let the middle-end | |
5419 | // optimize the call. | |
113ef6a5 | 5420 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5421 | return this->lower_compare_to_memcmp(gogo, inserter); |
5422 | ||
5423 | // Call the array comparison function. | |
5424 | Named_object* hash_fn; | |
5425 | Named_object* equal_fn; | |
5426 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5427 | &hash_fn, &equal_fn); | |
5428 | ||
5429 | Location loc = this->location(); | |
5430 | ||
5431 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5432 | ||
5433 | Expression_list* args = new Expression_list(); | |
5434 | args->push_back(this->operand_address(inserter, this->left_)); | |
5435 | args->push_back(this->operand_address(inserter, this->right_)); | |
e9d3367e | 5436 | |
5437 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5438 | ||
5439 | if (this->op_ == OPERATOR_NOTEQ) | |
5440 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5441 | ||
5442 | return ret; | |
5443 | } | |
5444 | ||
ceeb12d7 | 5445 | // Lower an interface to value comparison. |
5446 | ||
5447 | Expression* | |
5448 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5449 | Statement_inserter* inserter) | |
5450 | { | |
5451 | Type* left_type = this->left_->type(); | |
5452 | Type* right_type = this->right_->type(); | |
5453 | Interface_type* ift; | |
5454 | if (left_type->interface_type() != NULL) | |
5455 | { | |
5456 | ift = left_type->interface_type(); | |
5457 | if (!ift->implements_interface(right_type, NULL)) | |
5458 | return this; | |
5459 | } | |
5460 | else | |
5461 | { | |
5462 | ift = right_type->interface_type(); | |
5463 | if (!ift->implements_interface(left_type, NULL)) | |
5464 | return this; | |
5465 | } | |
5466 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5467 | return this; | |
5468 | ||
5469 | Location loc = this->location(); | |
5470 | ||
5471 | if (left_type->interface_type() == NULL | |
5472 | && left_type->points_to() == NULL | |
5473 | && !this->left_->is_addressable()) | |
5474 | { | |
5475 | Temporary_statement* temp = | |
5476 | Statement::make_temporary(left_type, NULL, loc); | |
5477 | inserter->insert(temp); | |
5478 | this->left_ = | |
5479 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5480 | } | |
5481 | ||
5482 | if (right_type->interface_type() == NULL | |
5483 | && right_type->points_to() == NULL | |
5484 | && !this->right_->is_addressable()) | |
5485 | { | |
5486 | Temporary_statement* temp = | |
5487 | Statement::make_temporary(right_type, NULL, loc); | |
5488 | inserter->insert(temp); | |
5489 | this->right_ = | |
5490 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5491 | } | |
5492 | ||
5493 | return this; | |
5494 | } | |
5495 | ||
e9d3367e | 5496 | // Lower a struct or array comparison to a call to memcmp. |
5497 | ||
5498 | Expression* | |
5499 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5500 | { | |
5501 | Location loc = this->location(); | |
5502 | ||
5503 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5504 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5505 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5506 | TYPE_INFO_SIZE); | |
5507 | ||
5508 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5509 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5510 | return Expression::make_binary(this->op_, call, zero, loc); |
5511 | } | |
5512 | ||
a32698ee | 5513 | Expression* |
5c3f3470 | 5514 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5515 | Statement_inserter* inserter) |
5516 | { | |
5517 | Location loc = this->location(); | |
5bf8be8b | 5518 | if (this->left_->type()->is_error_type() |
5519 | || this->right_->type()->is_error_type() | |
5520 | || this->left_->is_error_expression() | |
5521 | || this->right_->is_error_expression()) | |
5522 | { | |
5523 | go_assert(saw_errors()); | |
5524 | return Expression::make_error(loc); | |
5525 | } | |
5526 | ||
a32698ee | 5527 | Temporary_statement* temp; |
a32698ee | 5528 | |
5529 | Type* left_type = this->left_->type(); | |
5530 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5531 | || this->op_ == OPERATOR_RSHIFT); | |
5532 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5533 | left_type->integer_type() != NULL) | |
5534 | || this->op_ == OPERATOR_MOD); | |
5535 | ||
a32698ee | 5536 | if (is_shift_op |
5c3f3470 | 5537 | || (is_idiv_op |
5538 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5539 | { |
545ab43b | 5540 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5541 | { |
5542 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5543 | inserter->insert(temp); | |
5544 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5545 | } | |
545ab43b | 5546 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5547 | { |
5548 | temp = | |
5549 | Statement::make_temporary(NULL, this->right_, loc); | |
5550 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5551 | inserter->insert(temp); | |
5552 | } | |
5553 | } | |
5554 | return this; | |
5555 | } | |
5556 | ||
5557 | ||
e9d3367e | 5558 | // Return the address of EXPR, cast to unsafe.Pointer. |
5559 | ||
5560 | Expression* | |
5561 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5562 | Expression* expr) | |
5563 | { | |
5564 | Location loc = this->location(); | |
5565 | ||
5566 | if (!expr->is_addressable()) | |
5567 | { | |
5568 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5569 | loc); | |
5570 | inserter->insert(temp); | |
5571 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5572 | } | |
5573 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5574 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5575 | Type* void_type = Type::make_void_type(); | |
5576 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5577 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5578 | } | |
5579 | ||
0c77715b | 5580 | // Return the numeric constant value, if it has one. |
e440a328 | 5581 | |
5582 | bool | |
0c77715b | 5583 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5584 | { |
0c77715b | 5585 | Numeric_constant left_nc; |
5586 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5587 | return false; | |
5588 | Numeric_constant right_nc; | |
5589 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5590 | return false; | |
af7a5274 | 5591 | bool issued_error; |
9767e2d3 | 5592 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
af7a5274 | 5593 | this->location(), nc, &issued_error); |
e440a328 | 5594 | } |
5595 | ||
5596 | // Note that the value is being discarded. | |
5597 | ||
4f2138d7 | 5598 | bool |
e440a328 | 5599 | Binary_expression::do_discarding_value() |
5600 | { | |
5601 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5602 | return this->right_->discarding_value(); |
e440a328 | 5603 | else |
4f2138d7 | 5604 | { |
5605 | this->unused_value_error(); | |
5606 | return false; | |
5607 | } | |
e440a328 | 5608 | } |
5609 | ||
5610 | // Get type. | |
5611 | ||
5612 | Type* | |
5613 | Binary_expression::do_type() | |
5614 | { | |
5f5fea79 | 5615 | if (this->classification() == EXPRESSION_ERROR) |
5616 | return Type::make_error_type(); | |
5617 | ||
e440a328 | 5618 | switch (this->op_) |
5619 | { | |
e440a328 | 5620 | case OPERATOR_EQEQ: |
5621 | case OPERATOR_NOTEQ: | |
5622 | case OPERATOR_LT: | |
5623 | case OPERATOR_LE: | |
5624 | case OPERATOR_GT: | |
5625 | case OPERATOR_GE: | |
e90c9dfc | 5626 | if (this->type_ == NULL) |
5627 | this->type_ = Type::make_boolean_type(); | |
5628 | return this->type_; | |
e440a328 | 5629 | |
5630 | case OPERATOR_PLUS: | |
5631 | case OPERATOR_MINUS: | |
5632 | case OPERATOR_OR: | |
5633 | case OPERATOR_XOR: | |
5634 | case OPERATOR_MULT: | |
5635 | case OPERATOR_DIV: | |
5636 | case OPERATOR_MOD: | |
5637 | case OPERATOR_AND: | |
5638 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5639 | case OPERATOR_OROR: |
5640 | case OPERATOR_ANDAND: | |
e440a328 | 5641 | { |
0c77715b | 5642 | Type* type; |
5643 | if (!Binary_expression::operation_type(this->op_, | |
5644 | this->left_->type(), | |
5645 | this->right_->type(), | |
5646 | &type)) | |
5647 | return Type::make_error_type(); | |
5648 | return type; | |
e440a328 | 5649 | } |
5650 | ||
5651 | case OPERATOR_LSHIFT: | |
5652 | case OPERATOR_RSHIFT: | |
5653 | return this->left_->type(); | |
5654 | ||
5655 | default: | |
c3e6f413 | 5656 | go_unreachable(); |
e440a328 | 5657 | } |
5658 | } | |
5659 | ||
5660 | // Set type for a binary expression. | |
5661 | ||
5662 | void | |
5663 | Binary_expression::do_determine_type(const Type_context* context) | |
5664 | { | |
5665 | Type* tleft = this->left_->type(); | |
5666 | Type* tright = this->right_->type(); | |
5667 | ||
5668 | // Both sides should have the same type, except for the shift | |
5669 | // operations. For a comparison, we should ignore the incoming | |
5670 | // type. | |
5671 | ||
5672 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5673 | || this->op_ == OPERATOR_RSHIFT); | |
5674 | ||
5675 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5676 | || this->op_ == OPERATOR_NOTEQ | |
5677 | || this->op_ == OPERATOR_LT | |
5678 | || this->op_ == OPERATOR_LE | |
5679 | || this->op_ == OPERATOR_GT | |
5680 | || this->op_ == OPERATOR_GE); | |
5681 | ||
c999c2a7 | 5682 | // For constant expressions, the context of the result is not useful in |
5683 | // determining the types of the operands. It is only legal to use abstract | |
5684 | // boolean, numeric, and string constants as operands where it is legal to | |
5685 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5686 | // Any issues with the operation will be resolved in the check_types pass. | |
5687 | bool is_constant_expr = (this->left_->is_constant() | |
5688 | && this->right_->is_constant()); | |
5689 | ||
e440a328 | 5690 | Type_context subcontext(*context); |
5691 | ||
9c4ff2ce | 5692 | if (is_constant_expr && !is_shift_op) |
af7a5274 | 5693 | { |
5694 | subcontext.type = NULL; | |
5695 | subcontext.may_be_abstract = true; | |
5696 | } | |
5697 | else if (is_comparison) | |
e440a328 | 5698 | { |
5699 | // In a comparison, the context does not determine the types of | |
5700 | // the operands. | |
5701 | subcontext.type = NULL; | |
5702 | } | |
5703 | ||
5704 | // Set the context for the left hand operand. | |
5705 | if (is_shift_op) | |
5706 | { | |
b40dc774 | 5707 | // The right hand operand of a shift plays no role in |
5708 | // determining the type of the left hand operand. | |
e440a328 | 5709 | } |
5710 | else if (!tleft->is_abstract()) | |
5711 | subcontext.type = tleft; | |
5712 | else if (!tright->is_abstract()) | |
5713 | subcontext.type = tright; | |
5714 | else if (subcontext.type == NULL) | |
5715 | { | |
5716 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5717 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5718 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5719 | { | |
5720 | // Both sides have an abstract integer, abstract float, or | |
5721 | // abstract complex type. Just let CONTEXT determine | |
5722 | // whether they may remain abstract or not. | |
5723 | } | |
5724 | else if (tleft->complex_type() != NULL) | |
5725 | subcontext.type = tleft; | |
5726 | else if (tright->complex_type() != NULL) | |
5727 | subcontext.type = tright; | |
5728 | else if (tleft->float_type() != NULL) | |
5729 | subcontext.type = tleft; | |
5730 | else if (tright->float_type() != NULL) | |
5731 | subcontext.type = tright; | |
5732 | else | |
5733 | subcontext.type = tleft; | |
f58a23ae | 5734 | |
5735 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5736 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5737 | } |
5738 | ||
af7a5274 | 5739 | this->left_->determine_type(&subcontext); |
e440a328 | 5740 | |
e440a328 | 5741 | if (is_shift_op) |
5742 | { | |
b40dc774 | 5743 | // We may have inherited an unusable type for the shift operand. |
5744 | // Give a useful error if that happened. | |
5745 | if (tleft->is_abstract() | |
5746 | && subcontext.type != NULL | |
8ab6effb | 5747 | && !subcontext.may_be_abstract |
f6bc81e6 | 5748 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5749 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5750 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5751 | "for left operand of shift")); |
b40dc774 | 5752 | |
5753 | // The context for the right hand operand is the same as for the | |
5754 | // left hand operand, except for a shift operator. | |
e440a328 | 5755 | subcontext.type = Type::lookup_integer_type("uint"); |
5756 | subcontext.may_be_abstract = false; | |
5757 | } | |
5758 | ||
af7a5274 | 5759 | this->right_->determine_type(&subcontext); |
e90c9dfc | 5760 | |
5761 | if (is_comparison) | |
5762 | { | |
5763 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5764 | ; | |
5765 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5766 | this->type_ = context->type; | |
5767 | else if (!context->may_be_abstract) | |
5768 | this->type_ = Type::lookup_bool_type(); | |
5769 | } | |
e440a328 | 5770 | } |
5771 | ||
5772 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5773 | // OTYPE is the type of the other operand. Return whether the |
5774 | // operation is OK. This should not be used for shift. | |
e440a328 | 5775 | |
5776 | bool | |
be8b5eee | 5777 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5778 | Location location) |
e440a328 | 5779 | { |
5780 | switch (op) | |
5781 | { | |
5782 | case OPERATOR_OROR: | |
5783 | case OPERATOR_ANDAND: | |
c999c2a7 | 5784 | if (!type->is_boolean_type() |
5785 | || !otype->is_boolean_type()) | |
e440a328 | 5786 | { |
631d5788 | 5787 | go_error_at(location, "expected boolean type"); |
e440a328 | 5788 | return false; |
5789 | } | |
5790 | break; | |
5791 | ||
5792 | case OPERATOR_EQEQ: | |
5793 | case OPERATOR_NOTEQ: | |
e9d3367e | 5794 | { |
5795 | std::string reason; | |
5796 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5797 | { | |
631d5788 | 5798 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5799 | return false; |
5800 | } | |
5801 | } | |
e440a328 | 5802 | break; |
5803 | ||
5804 | case OPERATOR_LT: | |
5805 | case OPERATOR_LE: | |
5806 | case OPERATOR_GT: | |
5807 | case OPERATOR_GE: | |
e9d3367e | 5808 | { |
5809 | std::string reason; | |
5810 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5811 | { | |
631d5788 | 5812 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5813 | return false; |
5814 | } | |
5815 | } | |
e440a328 | 5816 | break; |
5817 | ||
5818 | case OPERATOR_PLUS: | |
5819 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5820 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5821 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5822 | { |
631d5788 | 5823 | go_error_at(location, |
e440a328 | 5824 | "expected integer, floating, complex, or string type"); |
5825 | return false; | |
5826 | } | |
5827 | break; | |
5828 | ||
5829 | case OPERATOR_MINUS: | |
5830 | case OPERATOR_MINUSEQ: | |
5831 | case OPERATOR_MULT: | |
5832 | case OPERATOR_MULTEQ: | |
5833 | case OPERATOR_DIV: | |
5834 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5835 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5836 | { |
631d5788 | 5837 | go_error_at(location, "expected integer, floating, or complex type"); |
e440a328 | 5838 | return false; |
5839 | } | |
5840 | break; | |
5841 | ||
5842 | case OPERATOR_MOD: | |
5843 | case OPERATOR_MODEQ: | |
5844 | case OPERATOR_OR: | |
5845 | case OPERATOR_OREQ: | |
5846 | case OPERATOR_AND: | |
5847 | case OPERATOR_ANDEQ: | |
5848 | case OPERATOR_XOR: | |
5849 | case OPERATOR_XOREQ: | |
5850 | case OPERATOR_BITCLEAR: | |
5851 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5852 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5853 | { |
631d5788 | 5854 | go_error_at(location, "expected integer type"); |
e440a328 | 5855 | return false; |
5856 | } | |
5857 | break; | |
5858 | ||
5859 | default: | |
c3e6f413 | 5860 | go_unreachable(); |
e440a328 | 5861 | } |
5862 | ||
5863 | return true; | |
5864 | } | |
5865 | ||
5866 | // Check types. | |
5867 | ||
5868 | void | |
5869 | Binary_expression::do_check_types(Gogo*) | |
5870 | { | |
5f5fea79 | 5871 | if (this->classification() == EXPRESSION_ERROR) |
5872 | return; | |
5873 | ||
e440a328 | 5874 | Type* left_type = this->left_->type(); |
5875 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5876 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5877 | { |
5878 | this->set_is_error(); | |
5879 | return; | |
5880 | } | |
e440a328 | 5881 | |
5882 | if (this->op_ == OPERATOR_EQEQ | |
5883 | || this->op_ == OPERATOR_NOTEQ | |
5884 | || this->op_ == OPERATOR_LT | |
5885 | || this->op_ == OPERATOR_LE | |
5886 | || this->op_ == OPERATOR_GT | |
5887 | || this->op_ == OPERATOR_GE) | |
5888 | { | |
907c5ecd | 5889 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5890 | { | |
5891 | this->report_error(_("invalid comparison of nil with nil")); | |
5892 | return; | |
5893 | } | |
e440a328 | 5894 | if (!Type::are_assignable(left_type, right_type, NULL) |
5895 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5896 | { | |
5897 | this->report_error(_("incompatible types in binary expression")); | |
5898 | return; | |
5899 | } | |
5900 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5901 | right_type, |
e440a328 | 5902 | this->location()) |
5903 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5904 | left_type, |
e440a328 | 5905 | this->location())) |
5906 | { | |
5907 | this->set_is_error(); | |
5908 | return; | |
5909 | } | |
5910 | } | |
5911 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5912 | { | |
5913 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5914 | { | |
5915 | this->report_error(_("incompatible types in binary expression")); | |
5916 | return; | |
5917 | } | |
5918 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5919 | right_type, |
e440a328 | 5920 | this->location())) |
5921 | { | |
5922 | this->set_is_error(); | |
5923 | return; | |
5924 | } | |
5c65b19d | 5925 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5926 | { | |
5927 | // Division by a zero integer constant is an error. | |
5928 | Numeric_constant rconst; | |
5929 | unsigned long rval; | |
5930 | if (left_type->integer_type() != NULL | |
5931 | && this->right_->numeric_constant_value(&rconst) | |
5932 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5933 | && rval == 0) | |
5934 | { | |
5935 | this->report_error(_("integer division by zero")); | |
5936 | return; | |
5937 | } | |
5938 | } | |
e440a328 | 5939 | } |
5940 | else | |
5941 | { | |
5942 | if (left_type->integer_type() == NULL) | |
5943 | this->report_error(_("shift of non-integer operand")); | |
5944 | ||
6b5e0fac | 5945 | if (right_type->is_string_type()) |
5946 | this->report_error(_("shift count not unsigned integer")); | |
5947 | else if (!right_type->is_abstract() | |
e440a328 | 5948 | && (right_type->integer_type() == NULL |
5949 | || !right_type->integer_type()->is_unsigned())) | |
5950 | this->report_error(_("shift count not unsigned integer")); | |
5951 | else | |
5952 | { | |
0c77715b | 5953 | Numeric_constant nc; |
5954 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5955 | { |
0c77715b | 5956 | mpz_t val; |
5957 | if (!nc.to_int(&val)) | |
5958 | this->report_error(_("shift count not unsigned integer")); | |
5959 | else | |
a4eba91b | 5960 | { |
0c77715b | 5961 | if (mpz_sgn(val) < 0) |
5962 | { | |
5963 | this->report_error(_("negative shift count")); | |
0c77715b | 5964 | Location rloc = this->right_->location(); |
e67508fa | 5965 | this->right_ = Expression::make_integer_ul(0, right_type, |
5966 | rloc); | |
0c77715b | 5967 | } |
5968 | mpz_clear(val); | |
a4eba91b | 5969 | } |
e440a328 | 5970 | } |
e440a328 | 5971 | } |
5972 | } | |
5973 | } | |
5974 | ||
ea664253 | 5975 | // Get the backend representation for a binary expression. |
e440a328 | 5976 | |
ea664253 | 5977 | Bexpression* |
5978 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 5979 | { |
1b1f2abf | 5980 | Gogo* gogo = context->gogo(); |
a32698ee | 5981 | Location loc = this->location(); |
5982 | Type* left_type = this->left_->type(); | |
5983 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5984 | |
e440a328 | 5985 | bool use_left_type = true; |
5986 | bool is_shift_op = false; | |
29a2d1d8 | 5987 | bool is_idiv_op = false; |
e440a328 | 5988 | switch (this->op_) |
5989 | { | |
5990 | case OPERATOR_EQEQ: | |
5991 | case OPERATOR_NOTEQ: | |
5992 | case OPERATOR_LT: | |
5993 | case OPERATOR_LE: | |
5994 | case OPERATOR_GT: | |
5995 | case OPERATOR_GE: | |
ea664253 | 5996 | return Expression::comparison(context, this->type_, this->op_, |
5997 | this->left_, this->right_, loc); | |
e440a328 | 5998 | |
5999 | case OPERATOR_OROR: | |
e440a328 | 6000 | case OPERATOR_ANDAND: |
e440a328 | 6001 | use_left_type = false; |
6002 | break; | |
6003 | case OPERATOR_PLUS: | |
e440a328 | 6004 | case OPERATOR_MINUS: |
e440a328 | 6005 | case OPERATOR_OR: |
e440a328 | 6006 | case OPERATOR_XOR: |
e440a328 | 6007 | case OPERATOR_MULT: |
e440a328 | 6008 | break; |
6009 | case OPERATOR_DIV: | |
a32698ee | 6010 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
6011 | break; | |
729f8831 | 6012 | // Fall through. |
e440a328 | 6013 | case OPERATOR_MOD: |
29a2d1d8 | 6014 | is_idiv_op = true; |
e440a328 | 6015 | break; |
6016 | case OPERATOR_LSHIFT: | |
e440a328 | 6017 | case OPERATOR_RSHIFT: |
e440a328 | 6018 | is_shift_op = true; |
6019 | break; | |
e440a328 | 6020 | case OPERATOR_BITCLEAR: |
a32698ee | 6021 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
6022 | case OPERATOR_AND: | |
e440a328 | 6023 | break; |
6024 | default: | |
c3e6f413 | 6025 | go_unreachable(); |
e440a328 | 6026 | } |
6027 | ||
736a16ba | 6028 | // The only binary operation for string is +, and that should have |
6029 | // been converted to a String_concat_expression in do_lower. | |
6030 | go_assert(!left_type->is_string_type()); | |
a32698ee | 6031 | |
6032 | // For complex division Go might want slightly different results than the | |
6033 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 6034 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
6035 | { | |
a32698ee | 6036 | Runtime::Function complex_code; |
1850e20c | 6037 | switch (this->left_->type()->complex_type()->bits()) |
6038 | { | |
6039 | case 64: | |
a32698ee | 6040 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 6041 | break; |
6042 | case 128: | |
a32698ee | 6043 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 6044 | break; |
6045 | default: | |
6046 | go_unreachable(); | |
6047 | } | |
a32698ee | 6048 | Expression* complex_div = |
6049 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 6050 | return complex_div->get_backend(context); |
1850e20c | 6051 | } |
6052 | ||
ea664253 | 6053 | Bexpression* left = this->left_->get_backend(context); |
6054 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 6055 | |
a32698ee | 6056 | Type* type = use_left_type ? left_type : right_type; |
6057 | Btype* btype = type->get_backend(gogo); | |
6058 | ||
6059 | Bexpression* ret = | |
6060 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
6061 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 6062 | |
a32698ee | 6063 | // Initialize overflow constants. |
6064 | Bexpression* overflow; | |
6065 | mpz_t zero; | |
6066 | mpz_init_set_ui(zero, 0UL); | |
6067 | mpz_t one; | |
6068 | mpz_init_set_ui(one, 1UL); | |
6069 | mpz_t neg_one; | |
6070 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 6071 | |
a32698ee | 6072 | Btype* left_btype = left_type->get_backend(gogo); |
6073 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 6074 | |
6075 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 6076 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 6077 | if (is_shift_op) |
6078 | { | |
a32698ee | 6079 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 6080 | |
a32698ee | 6081 | int bits = left_type->integer_type()->bits(); |
a7c5b619 | 6082 | |
6083 | Numeric_constant nc; | |
6084 | unsigned long ul; | |
6085 | if (!this->right_->numeric_constant_value(&nc) | |
6086 | || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID | |
6087 | || ul >= static_cast<unsigned long>(bits)) | |
e440a328 | 6088 | { |
a7c5b619 | 6089 | mpz_t bitsval; |
6090 | mpz_init_set_ui(bitsval, bits); | |
6091 | Bexpression* bits_expr = | |
6092 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
6093 | Bexpression* compare = | |
6094 | gogo->backend()->binary_expression(OPERATOR_LT, | |
6095 | right, bits_expr, loc); | |
6096 | ||
6097 | Bexpression* zero_expr = | |
6098 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
6099 | overflow = zero_expr; | |
6100 | Bfunction* bfn = context->function()->func_value()->get_decl(); | |
6101 | if (this->op_ == OPERATOR_RSHIFT | |
6102 | && !left_type->integer_type()->is_unsigned()) | |
6103 | { | |
6104 | Bexpression* neg_expr = | |
6105 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
6106 | zero_expr, loc); | |
6107 | Bexpression* neg_one_expr = | |
6108 | gogo->backend()->integer_constant_expression(left_btype, | |
6109 | neg_one); | |
6110 | overflow = gogo->backend()->conditional_expression(bfn, | |
6111 | btype, | |
6112 | neg_expr, | |
6113 | neg_one_expr, | |
6114 | zero_expr, | |
6115 | loc); | |
6116 | } | |
6117 | ret = gogo->backend()->conditional_expression(bfn, btype, compare, | |
6118 | ret, overflow, loc); | |
6119 | mpz_clear(bitsval); | |
29a2d1d8 | 6120 | } |
29a2d1d8 | 6121 | } |
6122 | ||
6123 | // Add checks for division by zero and division overflow as needed. | |
6124 | if (is_idiv_op) | |
6125 | { | |
5c3f3470 | 6126 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 6127 | { |
6128 | // right == 0 | |
a32698ee | 6129 | Bexpression* zero_expr = |
6130 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
6131 | Bexpression* check = | |
6132 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6133 | right, zero_expr, loc); | |
29a2d1d8 | 6134 | |
a32698ee | 6135 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 6136 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 6137 | Bexpression* crash = gogo->runtime_error(errcode, |
6138 | loc)->get_backend(context); | |
29a2d1d8 | 6139 | |
6140 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
93715b75 | 6141 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
6142 | ret = gogo->backend()->conditional_expression(bfn, btype, | |
6143 | check, crash, | |
ea664253 | 6144 | ret, loc); |
b13c66cd | 6145 | } |
6146 | ||
5c3f3470 | 6147 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 6148 | { |
6149 | // right == -1 | |
6150 | // FIXME: It would be nice to say that this test is expected | |
6151 | // to return false. | |
a32698ee | 6152 | |
6153 | Bexpression* neg_one_expr = | |
6154 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6155 | Bexpression* check = | |
6156 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6157 | right, neg_one_expr, loc); | |
6158 | ||
6159 | Bexpression* zero_expr = | |
6160 | gogo->backend()->integer_constant_expression(btype, zero); | |
6161 | Bexpression* one_expr = | |
6162 | gogo->backend()->integer_constant_expression(btype, one); | |
93715b75 | 6163 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
a32698ee | 6164 | |
6165 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6166 | { |
6167 | // An unsigned -1 is the largest possible number, so | |
6168 | // dividing is always 1 or 0. | |
a32698ee | 6169 | |
6170 | Bexpression* cmp = | |
6171 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6172 | left, right, loc); | |
29a2d1d8 | 6173 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6174 | overflow = |
93715b75 | 6175 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6176 | one_expr, zero_expr, |
6177 | loc); | |
29a2d1d8 | 6178 | else |
a32698ee | 6179 | overflow = |
93715b75 | 6180 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6181 | zero_expr, left, |
6182 | loc); | |
29a2d1d8 | 6183 | } |
6184 | else | |
6185 | { | |
6186 | // Computing left / -1 is the same as computing - left, | |
6187 | // which does not overflow since Go sets -fwrapv. | |
6188 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6189 | { |
6190 | Expression* negate_expr = | |
6191 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 6192 | overflow = negate_expr->get_backend(context); |
a32698ee | 6193 | } |
29a2d1d8 | 6194 | else |
a32698ee | 6195 | overflow = zero_expr; |
29a2d1d8 | 6196 | } |
a32698ee | 6197 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6198 | |
6199 | // right == -1 ? - left : ret | |
93715b75 | 6200 | ret = gogo->backend()->conditional_expression(bfn, btype, |
6201 | check, overflow, | |
a32698ee | 6202 | ret, loc); |
29a2d1d8 | 6203 | } |
e440a328 | 6204 | } |
6205 | ||
a32698ee | 6206 | mpz_clear(zero); |
6207 | mpz_clear(one); | |
6208 | mpz_clear(neg_one); | |
ea664253 | 6209 | return ret; |
e440a328 | 6210 | } |
6211 | ||
6212 | // Export a binary expression. | |
6213 | ||
6214 | void | |
6215 | Binary_expression::do_export(Export* exp) const | |
6216 | { | |
6217 | exp->write_c_string("("); | |
6218 | this->left_->export_expression(exp); | |
6219 | switch (this->op_) | |
6220 | { | |
6221 | case OPERATOR_OROR: | |
6222 | exp->write_c_string(" || "); | |
6223 | break; | |
6224 | case OPERATOR_ANDAND: | |
6225 | exp->write_c_string(" && "); | |
6226 | break; | |
6227 | case OPERATOR_EQEQ: | |
6228 | exp->write_c_string(" == "); | |
6229 | break; | |
6230 | case OPERATOR_NOTEQ: | |
6231 | exp->write_c_string(" != "); | |
6232 | break; | |
6233 | case OPERATOR_LT: | |
6234 | exp->write_c_string(" < "); | |
6235 | break; | |
6236 | case OPERATOR_LE: | |
6237 | exp->write_c_string(" <= "); | |
6238 | break; | |
6239 | case OPERATOR_GT: | |
6240 | exp->write_c_string(" > "); | |
6241 | break; | |
6242 | case OPERATOR_GE: | |
6243 | exp->write_c_string(" >= "); | |
6244 | break; | |
6245 | case OPERATOR_PLUS: | |
6246 | exp->write_c_string(" + "); | |
6247 | break; | |
6248 | case OPERATOR_MINUS: | |
6249 | exp->write_c_string(" - "); | |
6250 | break; | |
6251 | case OPERATOR_OR: | |
6252 | exp->write_c_string(" | "); | |
6253 | break; | |
6254 | case OPERATOR_XOR: | |
6255 | exp->write_c_string(" ^ "); | |
6256 | break; | |
6257 | case OPERATOR_MULT: | |
6258 | exp->write_c_string(" * "); | |
6259 | break; | |
6260 | case OPERATOR_DIV: | |
6261 | exp->write_c_string(" / "); | |
6262 | break; | |
6263 | case OPERATOR_MOD: | |
6264 | exp->write_c_string(" % "); | |
6265 | break; | |
6266 | case OPERATOR_LSHIFT: | |
6267 | exp->write_c_string(" << "); | |
6268 | break; | |
6269 | case OPERATOR_RSHIFT: | |
6270 | exp->write_c_string(" >> "); | |
6271 | break; | |
6272 | case OPERATOR_AND: | |
6273 | exp->write_c_string(" & "); | |
6274 | break; | |
6275 | case OPERATOR_BITCLEAR: | |
6276 | exp->write_c_string(" &^ "); | |
6277 | break; | |
6278 | default: | |
c3e6f413 | 6279 | go_unreachable(); |
e440a328 | 6280 | } |
6281 | this->right_->export_expression(exp); | |
6282 | exp->write_c_string(")"); | |
6283 | } | |
6284 | ||
6285 | // Import a binary expression. | |
6286 | ||
6287 | Expression* | |
6288 | Binary_expression::do_import(Import* imp) | |
6289 | { | |
6290 | imp->require_c_string("("); | |
6291 | ||
6292 | Expression* left = Expression::import_expression(imp); | |
6293 | ||
6294 | Operator op; | |
6295 | if (imp->match_c_string(" || ")) | |
6296 | { | |
6297 | op = OPERATOR_OROR; | |
6298 | imp->advance(4); | |
6299 | } | |
6300 | else if (imp->match_c_string(" && ")) | |
6301 | { | |
6302 | op = OPERATOR_ANDAND; | |
6303 | imp->advance(4); | |
6304 | } | |
6305 | else if (imp->match_c_string(" == ")) | |
6306 | { | |
6307 | op = OPERATOR_EQEQ; | |
6308 | imp->advance(4); | |
6309 | } | |
6310 | else if (imp->match_c_string(" != ")) | |
6311 | { | |
6312 | op = OPERATOR_NOTEQ; | |
6313 | imp->advance(4); | |
6314 | } | |
6315 | else if (imp->match_c_string(" < ")) | |
6316 | { | |
6317 | op = OPERATOR_LT; | |
6318 | imp->advance(3); | |
6319 | } | |
6320 | else if (imp->match_c_string(" <= ")) | |
6321 | { | |
6322 | op = OPERATOR_LE; | |
6323 | imp->advance(4); | |
6324 | } | |
6325 | else if (imp->match_c_string(" > ")) | |
6326 | { | |
6327 | op = OPERATOR_GT; | |
6328 | imp->advance(3); | |
6329 | } | |
6330 | else if (imp->match_c_string(" >= ")) | |
6331 | { | |
6332 | op = OPERATOR_GE; | |
6333 | imp->advance(4); | |
6334 | } | |
6335 | else if (imp->match_c_string(" + ")) | |
6336 | { | |
6337 | op = OPERATOR_PLUS; | |
6338 | imp->advance(3); | |
6339 | } | |
6340 | else if (imp->match_c_string(" - ")) | |
6341 | { | |
6342 | op = OPERATOR_MINUS; | |
6343 | imp->advance(3); | |
6344 | } | |
6345 | else if (imp->match_c_string(" | ")) | |
6346 | { | |
6347 | op = OPERATOR_OR; | |
6348 | imp->advance(3); | |
6349 | } | |
6350 | else if (imp->match_c_string(" ^ ")) | |
6351 | { | |
6352 | op = OPERATOR_XOR; | |
6353 | imp->advance(3); | |
6354 | } | |
6355 | else if (imp->match_c_string(" * ")) | |
6356 | { | |
6357 | op = OPERATOR_MULT; | |
6358 | imp->advance(3); | |
6359 | } | |
6360 | else if (imp->match_c_string(" / ")) | |
6361 | { | |
6362 | op = OPERATOR_DIV; | |
6363 | imp->advance(3); | |
6364 | } | |
6365 | else if (imp->match_c_string(" % ")) | |
6366 | { | |
6367 | op = OPERATOR_MOD; | |
6368 | imp->advance(3); | |
6369 | } | |
6370 | else if (imp->match_c_string(" << ")) | |
6371 | { | |
6372 | op = OPERATOR_LSHIFT; | |
6373 | imp->advance(4); | |
6374 | } | |
6375 | else if (imp->match_c_string(" >> ")) | |
6376 | { | |
6377 | op = OPERATOR_RSHIFT; | |
6378 | imp->advance(4); | |
6379 | } | |
6380 | else if (imp->match_c_string(" & ")) | |
6381 | { | |
6382 | op = OPERATOR_AND; | |
6383 | imp->advance(3); | |
6384 | } | |
6385 | else if (imp->match_c_string(" &^ ")) | |
6386 | { | |
6387 | op = OPERATOR_BITCLEAR; | |
6388 | imp->advance(4); | |
6389 | } | |
6390 | else | |
6391 | { | |
631d5788 | 6392 | go_error_at(imp->location(), "unrecognized binary operator"); |
e440a328 | 6393 | return Expression::make_error(imp->location()); |
6394 | } | |
6395 | ||
6396 | Expression* right = Expression::import_expression(imp); | |
6397 | ||
6398 | imp->require_c_string(")"); | |
6399 | ||
6400 | return Expression::make_binary(op, left, right, imp->location()); | |
6401 | } | |
6402 | ||
d751bb78 | 6403 | // Dump ast representation of a binary expression. |
6404 | ||
6405 | void | |
6406 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6407 | { | |
6408 | ast_dump_context->ostream() << "("; | |
6409 | ast_dump_context->dump_expression(this->left_); | |
6410 | ast_dump_context->ostream() << " "; | |
6411 | ast_dump_context->dump_operator(this->op_); | |
6412 | ast_dump_context->ostream() << " "; | |
6413 | ast_dump_context->dump_expression(this->right_); | |
6414 | ast_dump_context->ostream() << ") "; | |
6415 | } | |
6416 | ||
e440a328 | 6417 | // Make a binary expression. |
6418 | ||
6419 | Expression* | |
6420 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6421 | Location location) |
e440a328 | 6422 | { |
6423 | return new Binary_expression(op, left, right, location); | |
6424 | } | |
6425 | ||
6426 | // Implement a comparison. | |
6427 | ||
a32698ee | 6428 | Bexpression* |
6429 | Expression::comparison(Translate_context* context, Type* result_type, | |
6430 | Operator op, Expression* left, Expression* right, | |
6431 | Location location) | |
e440a328 | 6432 | { |
2387f644 | 6433 | Type* left_type = left->type(); |
6434 | Type* right_type = right->type(); | |
ceeb12d7 | 6435 | |
e67508fa | 6436 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6437 | |
15c67ee2 | 6438 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6439 | { |
6098d6cb | 6440 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
6441 | { | |
6442 | left = Runtime::make_call(Runtime::EQSTRING, location, 2, | |
6443 | left, right); | |
6444 | right = Expression::make_boolean(true, location); | |
6445 | } | |
6446 | else | |
6447 | { | |
6448 | left = Runtime::make_call(Runtime::CMPSTRING, location, 2, | |
6449 | left, right); | |
6450 | right = zexpr; | |
6451 | } | |
e440a328 | 6452 | } |
15c67ee2 | 6453 | else if ((left_type->interface_type() != NULL |
6454 | && right_type->interface_type() == NULL | |
6455 | && !right_type->is_nil_type()) | |
6456 | || (left_type->interface_type() == NULL | |
6457 | && !left_type->is_nil_type() | |
6458 | && right_type->interface_type() != NULL)) | |
e440a328 | 6459 | { |
6460 | // Comparing an interface value to a non-interface value. | |
6461 | if (left_type->interface_type() == NULL) | |
6462 | { | |
6463 | std::swap(left_type, right_type); | |
2387f644 | 6464 | std::swap(left, right); |
e440a328 | 6465 | } |
6466 | ||
6467 | // The right operand is not an interface. We need to take its | |
6468 | // address if it is not a pointer. | |
ceeb12d7 | 6469 | Expression* pointer_arg = NULL; |
e440a328 | 6470 | if (right_type->points_to() != NULL) |
2387f644 | 6471 | pointer_arg = right; |
e440a328 | 6472 | else |
6473 | { | |
2387f644 | 6474 | go_assert(right->is_addressable()); |
6475 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6476 | location); |
e440a328 | 6477 | } |
e440a328 | 6478 | |
2387f644 | 6479 | Expression* descriptor = |
6480 | Expression::make_type_descriptor(right_type, location); | |
6481 | left = | |
ceeb12d7 | 6482 | Runtime::make_call((left_type->interface_type()->is_empty() |
6098d6cb | 6483 | ? Runtime::EFACEVALEQ |
6484 | : Runtime::IFACEVALEQ), | |
2387f644 | 6485 | location, 3, left, descriptor, |
ceeb12d7 | 6486 | pointer_arg); |
6098d6cb | 6487 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6488 | right = Expression::make_boolean(true, location); | |
e440a328 | 6489 | } |
6490 | else if (left_type->interface_type() != NULL | |
6491 | && right_type->interface_type() != NULL) | |
6492 | { | |
ceeb12d7 | 6493 | Runtime::Function compare_function; |
739bad04 | 6494 | if (left_type->interface_type()->is_empty() |
6495 | && right_type->interface_type()->is_empty()) | |
6098d6cb | 6496 | compare_function = Runtime::EFACEEQ; |
739bad04 | 6497 | else if (!left_type->interface_type()->is_empty() |
6498 | && !right_type->interface_type()->is_empty()) | |
6098d6cb | 6499 | compare_function = Runtime::IFACEEQ; |
739bad04 | 6500 | else |
6501 | { | |
6502 | if (left_type->interface_type()->is_empty()) | |
6503 | { | |
739bad04 | 6504 | std::swap(left_type, right_type); |
2387f644 | 6505 | std::swap(left, right); |
739bad04 | 6506 | } |
c484d925 | 6507 | go_assert(!left_type->interface_type()->is_empty()); |
6508 | go_assert(right_type->interface_type()->is_empty()); | |
6098d6cb | 6509 | compare_function = Runtime::IFACEEFACEEQ; |
739bad04 | 6510 | } |
6511 | ||
2387f644 | 6512 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6098d6cb | 6513 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6514 | right = Expression::make_boolean(true, location); | |
e440a328 | 6515 | } |
6516 | ||
6517 | if (left_type->is_nil_type() | |
6518 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6519 | { | |
6520 | std::swap(left_type, right_type); | |
2387f644 | 6521 | std::swap(left, right); |
e440a328 | 6522 | } |
6523 | ||
6524 | if (right_type->is_nil_type()) | |
6525 | { | |
2387f644 | 6526 | right = Expression::make_nil(location); |
e440a328 | 6527 | if (left_type->array_type() != NULL |
6528 | && left_type->array_type()->length() == NULL) | |
6529 | { | |
6530 | Array_type* at = left_type->array_type(); | |
44dbe1d7 | 6531 | bool is_lvalue = false; |
6532 | left = at->get_value_pointer(context->gogo(), left, is_lvalue); | |
e440a328 | 6533 | } |
6534 | else if (left_type->interface_type() != NULL) | |
6535 | { | |
6536 | // An interface is nil if the first field is nil. | |
2387f644 | 6537 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6538 | } |
6539 | } | |
6540 | ||
ea664253 | 6541 | Bexpression* left_bexpr = left->get_backend(context); |
6542 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6543 | |
a32698ee | 6544 | Gogo* gogo = context->gogo(); |
6545 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6546 | right_bexpr, location); | |
6547 | if (result_type != NULL) | |
6548 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6549 | ret, location); | |
e440a328 | 6550 | return ret; |
6551 | } | |
6552 | ||
736a16ba | 6553 | // Class String_concat_expression. |
6554 | ||
6555 | bool | |
6556 | String_concat_expression::do_is_constant() const | |
6557 | { | |
6558 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6559 | pe != this->exprs_->end(); | |
6560 | ++pe) | |
6561 | { | |
6562 | if (!(*pe)->is_constant()) | |
6563 | return false; | |
6564 | } | |
6565 | return true; | |
6566 | } | |
6567 | ||
6568 | bool | |
3ae06f68 | 6569 | String_concat_expression::do_is_static_initializer() const |
736a16ba | 6570 | { |
6571 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6572 | pe != this->exprs_->end(); | |
6573 | ++pe) | |
6574 | { | |
3ae06f68 | 6575 | if (!(*pe)->is_static_initializer()) |
736a16ba | 6576 | return false; |
6577 | } | |
6578 | return true; | |
6579 | } | |
6580 | ||
6581 | Type* | |
6582 | String_concat_expression::do_type() | |
6583 | { | |
6584 | Type* t = this->exprs_->front()->type(); | |
6585 | Expression_list::iterator pe = this->exprs_->begin(); | |
6586 | ++pe; | |
6587 | for (; pe != this->exprs_->end(); ++pe) | |
6588 | { | |
6589 | Type* t1; | |
6590 | if (!Binary_expression::operation_type(OPERATOR_PLUS, t, | |
6591 | (*pe)->type(), | |
6592 | &t1)) | |
6593 | return Type::make_error_type(); | |
6594 | t = t1; | |
6595 | } | |
6596 | return t; | |
6597 | } | |
6598 | ||
6599 | void | |
6600 | String_concat_expression::do_determine_type(const Type_context* context) | |
6601 | { | |
6602 | Type_context subcontext(*context); | |
6603 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6604 | pe != this->exprs_->end(); | |
6605 | ++pe) | |
6606 | { | |
6607 | Type* t = (*pe)->type(); | |
6608 | if (!t->is_abstract()) | |
6609 | { | |
6610 | subcontext.type = t; | |
6611 | break; | |
6612 | } | |
6613 | } | |
6614 | if (subcontext.type == NULL) | |
6615 | subcontext.type = this->exprs_->front()->type(); | |
6616 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6617 | pe != this->exprs_->end(); | |
6618 | ++pe) | |
6619 | (*pe)->determine_type(&subcontext); | |
6620 | } | |
6621 | ||
6622 | void | |
6623 | String_concat_expression::do_check_types(Gogo*) | |
6624 | { | |
6625 | if (this->is_error_expression()) | |
6626 | return; | |
6627 | Type* t = this->exprs_->front()->type(); | |
6628 | if (t->is_error()) | |
6629 | { | |
6630 | this->set_is_error(); | |
6631 | return; | |
6632 | } | |
6633 | Expression_list::iterator pe = this->exprs_->begin(); | |
6634 | ++pe; | |
6635 | for (; pe != this->exprs_->end(); ++pe) | |
6636 | { | |
6637 | Type* t1 = (*pe)->type(); | |
6638 | if (!Type::are_compatible_for_binop(t, t1)) | |
6639 | { | |
6640 | this->report_error("incompatible types in binary expression"); | |
6641 | return; | |
6642 | } | |
6643 | if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, | |
6644 | this->location())) | |
6645 | { | |
6646 | this->set_is_error(); | |
6647 | return; | |
6648 | } | |
6649 | } | |
6650 | } | |
6651 | ||
6652 | Expression* | |
6653 | String_concat_expression::do_flatten(Gogo*, Named_object*, | |
6654 | Statement_inserter*) | |
6655 | { | |
6656 | if (this->is_error_expression()) | |
6657 | return this; | |
6658 | Location loc = this->location(); | |
6659 | Type* type = this->type(); | |
6660 | Expression* nil_arg = Expression::make_nil(loc); | |
6661 | Expression* call; | |
6662 | switch (this->exprs_->size()) | |
6663 | { | |
6664 | case 0: case 1: | |
6665 | go_unreachable(); | |
6666 | ||
6667 | case 2: case 3: case 4: case 5: | |
6668 | { | |
6669 | Expression* len = Expression::make_integer_ul(this->exprs_->size(), | |
6670 | NULL, loc); | |
6671 | Array_type* arg_type = Type::make_array_type(type, len); | |
6672 | arg_type->set_is_array_incomparable(); | |
6673 | Expression* arg = | |
6674 | Expression::make_array_composite_literal(arg_type, this->exprs_, | |
6675 | loc); | |
6676 | Runtime::Function code; | |
6677 | switch (this->exprs_->size()) | |
6678 | { | |
6679 | default: | |
6680 | go_unreachable(); | |
6681 | case 2: | |
6682 | code = Runtime::CONCATSTRING2; | |
6683 | break; | |
6684 | case 3: | |
6685 | code = Runtime::CONCATSTRING3; | |
6686 | break; | |
6687 | case 4: | |
6688 | code = Runtime::CONCATSTRING4; | |
6689 | break; | |
6690 | case 5: | |
6691 | code = Runtime::CONCATSTRING5; | |
6692 | break; | |
6693 | } | |
6694 | call = Runtime::make_call(code, loc, 2, nil_arg, arg); | |
6695 | } | |
6696 | break; | |
6697 | ||
6698 | default: | |
6699 | { | |
6700 | Type* arg_type = Type::make_array_type(type, NULL); | |
6701 | Slice_construction_expression* sce = | |
6702 | Expression::make_slice_composite_literal(arg_type, this->exprs_, | |
6703 | loc); | |
6704 | sce->set_storage_does_not_escape(); | |
6705 | call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg, | |
6706 | sce); | |
6707 | } | |
6708 | break; | |
6709 | } | |
6710 | ||
6711 | return Expression::make_cast(type, call, loc); | |
6712 | } | |
6713 | ||
6714 | void | |
6715 | String_concat_expression::do_dump_expression( | |
6716 | Ast_dump_context* ast_dump_context) const | |
6717 | { | |
6718 | ast_dump_context->ostream() << "concat("; | |
6719 | ast_dump_context->dump_expression_list(this->exprs_, false); | |
6720 | ast_dump_context->ostream() << ")"; | |
6721 | } | |
6722 | ||
6723 | Expression* | |
6724 | Expression::make_string_concat(Expression_list* exprs) | |
6725 | { | |
6726 | return new String_concat_expression(exprs); | |
6727 | } | |
6728 | ||
e440a328 | 6729 | // Class Bound_method_expression. |
6730 | ||
6731 | // Traversal. | |
6732 | ||
6733 | int | |
6734 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6735 | { | |
e0659c9e | 6736 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6737 | } |
6738 | ||
6739 | // Return the type of a bound method expression. The type of this | |
0afbb937 | 6740 | // object is simply the type of the method with no receiver. |
e440a328 | 6741 | |
6742 | Type* | |
6743 | Bound_method_expression::do_type() | |
6744 | { | |
0afbb937 | 6745 | Named_object* fn = this->method_->named_object(); |
6746 | Function_type* fntype; | |
6747 | if (fn->is_function()) | |
6748 | fntype = fn->func_value()->type(); | |
6749 | else if (fn->is_function_declaration()) | |
6750 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6751 | else |
6752 | return Type::make_error_type(); | |
0afbb937 | 6753 | return fntype->copy_without_receiver(); |
e440a328 | 6754 | } |
6755 | ||
6756 | // Determine the types of a method expression. | |
6757 | ||
6758 | void | |
6759 | Bound_method_expression::do_determine_type(const Type_context*) | |
6760 | { | |
0afbb937 | 6761 | Named_object* fn = this->method_->named_object(); |
6762 | Function_type* fntype; | |
6763 | if (fn->is_function()) | |
6764 | fntype = fn->func_value()->type(); | |
6765 | else if (fn->is_function_declaration()) | |
6766 | fntype = fn->func_declaration_value()->type(); | |
6767 | else | |
6768 | fntype = NULL; | |
e440a328 | 6769 | if (fntype == NULL || !fntype->is_method()) |
6770 | this->expr_->determine_type_no_context(); | |
6771 | else | |
6772 | { | |
6773 | Type_context subcontext(fntype->receiver()->type(), false); | |
6774 | this->expr_->determine_type(&subcontext); | |
6775 | } | |
6776 | } | |
6777 | ||
6778 | // Check the types of a method expression. | |
6779 | ||
6780 | void | |
6781 | Bound_method_expression::do_check_types(Gogo*) | |
6782 | { | |
0afbb937 | 6783 | Named_object* fn = this->method_->named_object(); |
6784 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6785 | { | |
6786 | this->report_error(_("object is not a method")); | |
6787 | return; | |
6788 | } | |
6789 | ||
6790 | Function_type* fntype; | |
6791 | if (fn->is_function()) | |
6792 | fntype = fn->func_value()->type(); | |
6793 | else if (fn->is_function_declaration()) | |
6794 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6795 | else |
0afbb937 | 6796 | go_unreachable(); |
6797 | Type* rtype = fntype->receiver()->type()->deref(); | |
6798 | Type* etype = (this->expr_type_ != NULL | |
6799 | ? this->expr_type_ | |
6800 | : this->expr_->type()); | |
6801 | etype = etype->deref(); | |
6802 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6803 | this->report_error(_("method type does not match object type")); | |
6804 | } | |
6805 | ||
6806 | // If a bound method expression is not simply called, then it is | |
6807 | // represented as a closure. The closure will hold a single variable, | |
6808 | // the receiver to pass to the method. The function will be a simple | |
6809 | // thunk that pulls that value from the closure and calls the method | |
6810 | // with the remaining arguments. | |
6811 | // | |
6812 | // Because method values are not common, we don't build all thunks for | |
6813 | // every methods, but instead only build them as we need them. In | |
6814 | // particular, we even build them on demand for methods defined in | |
6815 | // other packages. | |
6816 | ||
6817 | Bound_method_expression::Method_value_thunks | |
6818 | Bound_method_expression::method_value_thunks; | |
6819 | ||
6820 | // Find or create the thunk for METHOD. | |
6821 | ||
6822 | Named_object* | |
6823 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6824 | Named_object* fn) | |
6825 | { | |
6826 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6827 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6828 | Bound_method_expression::method_value_thunks.insert(val); | |
6829 | if (!ins.second) | |
6830 | { | |
6831 | // We have seen this method before. | |
6832 | go_assert(ins.first->second != NULL); | |
6833 | return ins.first->second; | |
6834 | } | |
6835 | ||
6836 | Location loc = fn->location(); | |
6837 | ||
6838 | Function_type* orig_fntype; | |
6839 | if (fn->is_function()) | |
6840 | orig_fntype = fn->func_value()->type(); | |
6841 | else if (fn->is_function_declaration()) | |
6842 | orig_fntype = fn->func_declaration_value()->type(); | |
6843 | else | |
6844 | orig_fntype = NULL; | |
6845 | ||
6846 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6847 | { |
0afbb937 | 6848 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6849 | return ins.first->second; | |
e440a328 | 6850 | } |
0afbb937 | 6851 | |
6852 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6853 | // The type here is wrong--it should be the C function type. But it |
6854 | // doesn't really matter. | |
0afbb937 | 6855 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6856 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6857 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6858 | orig_fntype->receiver()->type(), | |
6859 | loc))); | |
6bf4793c | 6860 | Struct_type* st = Type::make_struct_type(sfl, loc); |
6861 | st->set_is_struct_incomparable(); | |
6862 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 6863 | |
f8bdf81a | 6864 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6865 | |
da244e59 | 6866 | std::string thunk_name = Gogo::thunk_name(); |
6867 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 6868 | false, loc); |
6869 | ||
f8bdf81a | 6870 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6871 | cvar->set_is_used(); | |
1ecc6157 | 6872 | cvar->set_is_closure(); |
da244e59 | 6873 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6874 | NULL, cvar); | |
f8bdf81a | 6875 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6876 | |
f8bdf81a | 6877 | gogo->start_block(loc); |
0afbb937 | 6878 | |
6879 | // Field 0 of the closure is the function code pointer, field 1 is | |
6880 | // the value on which to invoke the method. | |
6881 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 6882 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 6883 | arg = Expression::make_field_reference(arg, 1, loc); |
6884 | ||
6885 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6886 | ||
6887 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6888 | Expression_list* args; | |
6889 | if (orig_params == NULL || orig_params->empty()) | |
6890 | args = NULL; | |
6891 | else | |
6892 | { | |
6893 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6894 | args = new Expression_list(); | |
6895 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6896 | p != new_params->end(); |
0afbb937 | 6897 | ++p) |
6898 | { | |
6899 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6900 | go_assert(p_no != NULL | |
6901 | && p_no->is_variable() | |
6902 | && p_no->var_value()->is_parameter()); | |
6903 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6904 | } | |
6905 | } | |
6906 | ||
6907 | Call_expression* call = Expression::make_call(bme, args, | |
6908 | orig_fntype->is_varargs(), | |
6909 | loc); | |
6910 | call->set_varargs_are_lowered(); | |
6911 | ||
6912 | Statement* s = Statement::make_return_from_call(call, loc); | |
6913 | gogo->add_statement(s); | |
6914 | Block* b = gogo->finish_block(loc); | |
6915 | gogo->add_block(b, loc); | |
6916 | gogo->lower_block(new_no, b); | |
a32698ee | 6917 | gogo->flatten_block(new_no, b); |
0afbb937 | 6918 | gogo->finish_function(loc); |
6919 | ||
6920 | ins.first->second = new_no; | |
6921 | return new_no; | |
6922 | } | |
6923 | ||
6924 | // Return an expression to check *REF for nil while dereferencing | |
6925 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6926 | // reference. This is a static function so that we don't have to | |
6927 | // worry about declaring Field_indexes in expressions.h. | |
6928 | ||
6929 | static Expression* | |
6930 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6931 | Expression** ref) | |
6932 | { | |
6933 | if (field_indexes == NULL) | |
6934 | return Expression::make_boolean(false, loc); | |
6935 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6936 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6937 | go_assert(stype != NULL | |
6938 | && field_indexes->field_index < stype->field_count()); | |
6939 | if ((*ref)->type()->struct_type() == NULL) | |
6940 | { | |
6941 | go_assert((*ref)->type()->points_to() != NULL); | |
6942 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6943 | Expression::make_nil(loc), | |
6944 | loc); | |
6945 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
f614ea8b | 6946 | *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT, |
6947 | loc); | |
0afbb937 | 6948 | go_assert((*ref)->type()->struct_type() == stype); |
6949 | } | |
6950 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6951 | loc); | |
6952 | return cond; | |
e440a328 | 6953 | } |
6954 | ||
cd39797e | 6955 | // Flatten a method value into a struct with nil checks. We can't do |
6956 | // this in the lowering phase, because if the method value is called | |
6957 | // directly we don't need a thunk. That case will have been handled | |
6958 | // by Call_expression::do_lower, so if we get here then we do need a | |
6959 | // thunk. | |
e440a328 | 6960 | |
cd39797e | 6961 | Expression* |
6962 | Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, | |
6963 | Statement_inserter* inserter) | |
e440a328 | 6964 | { |
cd39797e | 6965 | Location loc = this->location(); |
6966 | ||
6967 | Named_object* thunk = Bound_method_expression::create_thunk(gogo, | |
0afbb937 | 6968 | this->method_, |
6969 | this->function_); | |
6970 | if (thunk->is_erroneous()) | |
6971 | { | |
6972 | go_assert(saw_errors()); | |
cd39797e | 6973 | return Expression::make_error(loc); |
0afbb937 | 6974 | } |
6975 | ||
cd39797e | 6976 | // Force the expression into a variable. This is only necessary if |
6977 | // we are going to do nil checks below, but it's easy enough to | |
6978 | // always do it. | |
6979 | Expression* expr = this->expr_; | |
6980 | if (!expr->is_variable()) | |
6981 | { | |
6982 | Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); | |
6983 | inserter->insert(etemp); | |
6984 | expr = Expression::make_temporary_reference(etemp, loc); | |
6985 | } | |
0afbb937 | 6986 | |
6987 | // If the method expects a value, and we have a pointer, we need to | |
6988 | // dereference the pointer. | |
6989 | ||
6990 | Named_object* fn = this->method_->named_object(); | |
cd39797e | 6991 | Function_type *fntype; |
0afbb937 | 6992 | if (fn->is_function()) |
6993 | fntype = fn->func_value()->type(); | |
6994 | else if (fn->is_function_declaration()) | |
6995 | fntype = fn->func_declaration_value()->type(); | |
6996 | else | |
6997 | go_unreachable(); | |
6998 | ||
cd39797e | 6999 | Expression* val = expr; |
0afbb937 | 7000 | if (fntype->receiver()->type()->points_to() == NULL |
7001 | && val->type()->points_to() != NULL) | |
f614ea8b | 7002 | val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc); |
0afbb937 | 7003 | |
7004 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
7005 | // expect a closure whose second field has type this->expr_type_ (if | |
7006 | // that is not NULL). We are going to pass it a closure whose | |
7007 | // second field has type this->expr_->type(). Since | |
7008 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
7009 | // away with this. | |
7010 | ||
7011 | Struct_field_list* fields = new Struct_field_list(); | |
7012 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
7013 | thunk->func_value()->type(), | |
7014 | loc))); | |
7015 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
7016 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6bf4793c | 7017 | st->set_is_struct_incomparable(); |
0afbb937 | 7018 | |
7019 | Expression_list* vals = new Expression_list(); | |
7020 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
7021 | vals->push_back(val); | |
7022 | ||
7023 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
c1177ba4 | 7024 | ret = Expression::make_heap_expression(ret, loc); |
0afbb937 | 7025 | |
c1177ba4 | 7026 | Node* n = Node::make_node(this); |
7027 | if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE) | |
7028 | ret->heap_expression()->set_allocate_on_stack(); | |
7029 | else if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
7030 | go_error_at(loc, "%s escapes to heap, not allowed in runtime", | |
7031 | n->ast_format(gogo).c_str()); | |
cd39797e | 7032 | |
7033 | // If necessary, check whether the expression or any embedded | |
7034 | // pointers are nil. | |
0afbb937 | 7035 | |
df7ef1fd | 7036 | Expression* nil_check = NULL; |
0afbb937 | 7037 | if (this->method_->field_indexes() != NULL) |
7038 | { | |
0afbb937 | 7039 | Expression* ref = expr; |
7040 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
7041 | expr = ref; | |
7042 | } | |
7043 | ||
7044 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
7045 | { | |
7046 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
7047 | Expression::make_nil(loc), | |
7048 | loc); | |
7049 | if (nil_check == NULL) | |
7050 | nil_check = n; | |
7051 | else | |
7052 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
7053 | } | |
7054 | ||
7055 | if (nil_check != NULL) | |
7056 | { | |
cd39797e | 7057 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
7058 | loc); | |
7059 | // Fix the type of the conditional expression by pretending to | |
7060 | // evaluate to RET either way through the conditional. | |
7061 | crash = Expression::make_compound(crash, ret, loc); | |
7062 | ret = Expression::make_conditional(nil_check, crash, ret, loc); | |
7063 | } | |
7064 | ||
7065 | // RET is a pointer to a struct, but we want a function type. | |
7066 | ret = Expression::make_unsafe_cast(this->type(), ret, loc); | |
7067 | ||
7068 | return ret; | |
e440a328 | 7069 | } |
7070 | ||
d751bb78 | 7071 | // Dump ast representation of a bound method expression. |
7072 | ||
7073 | void | |
7074 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
7075 | const | |
7076 | { | |
7077 | if (this->expr_type_ != NULL) | |
7078 | ast_dump_context->ostream() << "("; | |
7079 | ast_dump_context->dump_expression(this->expr_); | |
7080 | if (this->expr_type_ != NULL) | |
7081 | { | |
7082 | ast_dump_context->ostream() << ":"; | |
7083 | ast_dump_context->dump_type(this->expr_type_); | |
7084 | ast_dump_context->ostream() << ")"; | |
7085 | } | |
7086 | ||
0afbb937 | 7087 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 7088 | } |
7089 | ||
e440a328 | 7090 | // Make a method expression. |
7091 | ||
7092 | Bound_method_expression* | |
0afbb937 | 7093 | Expression::make_bound_method(Expression* expr, const Method* method, |
7094 | Named_object* function, Location location) | |
e440a328 | 7095 | { |
0afbb937 | 7096 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 7097 | } |
7098 | ||
7099 | // Class Builtin_call_expression. This is used for a call to a | |
7100 | // builtin function. | |
7101 | ||
e440a328 | 7102 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, |
7103 | Expression* fn, | |
7104 | Expression_list* args, | |
7105 | bool is_varargs, | |
b13c66cd | 7106 | Location location) |
e440a328 | 7107 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 7108 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
7109 | recover_arg_is_set_(false) | |
e440a328 | 7110 | { |
7111 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 7112 | if (fnexp == NULL) |
7113 | { | |
7114 | this->code_ = BUILTIN_INVALID; | |
7115 | return; | |
7116 | } | |
e440a328 | 7117 | const std::string& name(fnexp->named_object()->name()); |
7118 | if (name == "append") | |
7119 | this->code_ = BUILTIN_APPEND; | |
7120 | else if (name == "cap") | |
7121 | this->code_ = BUILTIN_CAP; | |
7122 | else if (name == "close") | |
7123 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 7124 | else if (name == "complex") |
7125 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 7126 | else if (name == "copy") |
7127 | this->code_ = BUILTIN_COPY; | |
1cce762f | 7128 | else if (name == "delete") |
7129 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 7130 | else if (name == "imag") |
7131 | this->code_ = BUILTIN_IMAG; | |
7132 | else if (name == "len") | |
7133 | this->code_ = BUILTIN_LEN; | |
7134 | else if (name == "make") | |
7135 | this->code_ = BUILTIN_MAKE; | |
7136 | else if (name == "new") | |
7137 | this->code_ = BUILTIN_NEW; | |
7138 | else if (name == "panic") | |
7139 | this->code_ = BUILTIN_PANIC; | |
7140 | else if (name == "print") | |
7141 | this->code_ = BUILTIN_PRINT; | |
7142 | else if (name == "println") | |
7143 | this->code_ = BUILTIN_PRINTLN; | |
7144 | else if (name == "real") | |
7145 | this->code_ = BUILTIN_REAL; | |
7146 | else if (name == "recover") | |
7147 | this->code_ = BUILTIN_RECOVER; | |
7148 | else if (name == "Alignof") | |
7149 | this->code_ = BUILTIN_ALIGNOF; | |
7150 | else if (name == "Offsetof") | |
7151 | this->code_ = BUILTIN_OFFSETOF; | |
7152 | else if (name == "Sizeof") | |
7153 | this->code_ = BUILTIN_SIZEOF; | |
7154 | else | |
c3e6f413 | 7155 | go_unreachable(); |
e440a328 | 7156 | } |
7157 | ||
7158 | // Return whether this is a call to recover. This is a virtual | |
7159 | // function called from the parent class. | |
7160 | ||
7161 | bool | |
7162 | Builtin_call_expression::do_is_recover_call() const | |
7163 | { | |
7164 | if (this->classification() == EXPRESSION_ERROR) | |
7165 | return false; | |
7166 | return this->code_ == BUILTIN_RECOVER; | |
7167 | } | |
7168 | ||
7169 | // Set the argument for a call to recover. | |
7170 | ||
7171 | void | |
7172 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7173 | { | |
7174 | const Expression_list* args = this->args(); | |
c484d925 | 7175 | go_assert(args == NULL || args->empty()); |
e440a328 | 7176 | Expression_list* new_args = new Expression_list(); |
7177 | new_args->push_back(arg); | |
7178 | this->set_args(new_args); | |
6334270b | 7179 | this->recover_arg_is_set_ = true; |
e440a328 | 7180 | } |
7181 | ||
e440a328 | 7182 | // Lower a builtin call expression. This turns new and make into |
7183 | // specific expressions. We also convert to a constant if we can. | |
7184 | ||
7185 | Expression* | |
321e5ad2 | 7186 | Builtin_call_expression::do_lower(Gogo*, Named_object* function, |
ceeb4318 | 7187 | Statement_inserter* inserter, int) |
e440a328 | 7188 | { |
79651b1f | 7189 | if (this->is_error_expression()) |
a9182619 | 7190 | return this; |
7191 | ||
b13c66cd | 7192 | Location loc = this->location(); |
1cce762f | 7193 | |
a8725655 | 7194 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7195 | { | |
7196 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7197 | return Expression::make_error(loc); |
a8725655 | 7198 | } |
7199 | ||
393ba00b | 7200 | if (this->code_ == BUILTIN_OFFSETOF) |
7201 | { | |
7202 | Expression* arg = this->one_arg(); | |
12e69faa | 7203 | |
7204 | if (arg->bound_method_expression() != NULL | |
7205 | || arg->interface_field_reference_expression() != NULL) | |
7206 | { | |
7207 | this->report_error(_("invalid use of method value as argument " | |
7208 | "of Offsetof")); | |
7209 | return this; | |
7210 | } | |
7211 | ||
393ba00b | 7212 | Field_reference_expression* farg = arg->field_reference_expression(); |
7213 | while (farg != NULL) | |
7214 | { | |
7215 | if (!farg->implicit()) | |
7216 | break; | |
7217 | // When the selector refers to an embedded field, | |
7218 | // it must not be reached through pointer indirections. | |
7219 | if (farg->expr()->deref() != farg->expr()) | |
7220 | { | |
12e69faa | 7221 | this->report_error(_("argument of Offsetof implies " |
7222 | "indirection of an embedded field")); | |
393ba00b | 7223 | return this; |
7224 | } | |
7225 | // Go up until we reach the original base. | |
7226 | farg = farg->expr()->field_reference_expression(); | |
7227 | } | |
7228 | } | |
7229 | ||
1cce762f | 7230 | if (this->is_constant()) |
e440a328 | 7231 | { |
0c77715b | 7232 | Numeric_constant nc; |
7233 | if (this->numeric_constant_value(&nc)) | |
7234 | return nc.expression(loc); | |
e440a328 | 7235 | } |
1cce762f | 7236 | |
7237 | switch (this->code_) | |
e440a328 | 7238 | { |
1cce762f | 7239 | default: |
7240 | break; | |
7241 | ||
7242 | case BUILTIN_NEW: | |
7243 | { | |
7244 | const Expression_list* args = this->args(); | |
7245 | if (args == NULL || args->size() < 1) | |
7246 | this->report_error(_("not enough arguments")); | |
7247 | else if (args->size() > 1) | |
7248 | this->report_error(_("too many arguments")); | |
7249 | else | |
7250 | { | |
7251 | Expression* arg = args->front(); | |
7252 | if (!arg->is_type_expression()) | |
7253 | { | |
631d5788 | 7254 | go_error_at(arg->location(), "expected type"); |
1cce762f | 7255 | this->set_is_error(); |
7256 | } | |
7257 | else | |
7258 | return Expression::make_allocation(arg->type(), loc); | |
7259 | } | |
7260 | } | |
7261 | break; | |
7262 | ||
7263 | case BUILTIN_MAKE: | |
321e5ad2 | 7264 | return this->lower_make(inserter); |
1cce762f | 7265 | |
7266 | case BUILTIN_RECOVER: | |
e440a328 | 7267 | if (function != NULL) |
7268 | function->func_value()->set_calls_recover(); | |
7269 | else | |
7270 | { | |
7271 | // Calling recover outside of a function always returns the | |
7272 | // nil empty interface. | |
823c7e3d | 7273 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7274 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7275 | } |
1cce762f | 7276 | break; |
7277 | ||
1cce762f | 7278 | case BUILTIN_DELETE: |
7279 | { | |
7280 | // Lower to a runtime function call. | |
7281 | const Expression_list* args = this->args(); | |
7282 | if (args == NULL || args->size() < 2) | |
7283 | this->report_error(_("not enough arguments")); | |
7284 | else if (args->size() > 2) | |
7285 | this->report_error(_("too many arguments")); | |
7286 | else if (args->front()->type()->map_type() == NULL) | |
7287 | this->report_error(_("argument 1 must be a map")); | |
7288 | else | |
7289 | { | |
7290 | // Since this function returns no value it must appear in | |
7291 | // a statement by itself, so we don't have to worry about | |
7292 | // order of evaluation of values around it. Evaluate the | |
7293 | // map first to get order of evaluation right. | |
7294 | Map_type* mt = args->front()->type()->map_type(); | |
7295 | Temporary_statement* map_temp = | |
7296 | Statement::make_temporary(mt, args->front(), loc); | |
7297 | inserter->insert(map_temp); | |
7298 | ||
7299 | Temporary_statement* key_temp = | |
7300 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7301 | inserter->insert(key_temp); | |
7302 | ||
0d5530d9 | 7303 | Expression* e1 = Expression::make_type_descriptor(mt, loc); |
7304 | Expression* e2 = Expression::make_temporary_reference(map_temp, | |
1cce762f | 7305 | loc); |
0d5530d9 | 7306 | Expression* e3 = Expression::make_temporary_reference(key_temp, |
1cce762f | 7307 | loc); |
0d5530d9 | 7308 | e3 = Expression::make_unary(OPERATOR_AND, e3, loc); |
1cce762f | 7309 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), |
0d5530d9 | 7310 | 3, e1, e2, e3); |
1cce762f | 7311 | } |
7312 | } | |
7313 | break; | |
88b03a70 | 7314 | |
7315 | case BUILTIN_PRINT: | |
7316 | case BUILTIN_PRINTLN: | |
7317 | // Force all the arguments into temporary variables, so that we | |
7318 | // don't try to evaluate something while holding the print lock. | |
7319 | if (this->args() == NULL) | |
7320 | break; | |
7321 | for (Expression_list::iterator pa = this->args()->begin(); | |
7322 | pa != this->args()->end(); | |
7323 | ++pa) | |
7324 | { | |
493ce3ee | 7325 | if (!(*pa)->is_variable() && !(*pa)->is_constant()) |
88b03a70 | 7326 | { |
7327 | Temporary_statement* temp = | |
7328 | Statement::make_temporary(NULL, *pa, loc); | |
7329 | inserter->insert(temp); | |
7330 | *pa = Expression::make_temporary_reference(temp, loc); | |
7331 | } | |
7332 | } | |
7333 | break; | |
e440a328 | 7334 | } |
7335 | ||
7336 | return this; | |
7337 | } | |
7338 | ||
35a54f17 | 7339 | // Flatten a builtin call expression. This turns the arguments of copy and |
7340 | // append into temporary expressions. | |
7341 | ||
7342 | Expression* | |
321e5ad2 | 7343 | Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function, |
35a54f17 | 7344 | Statement_inserter* inserter) |
7345 | { | |
16cb7fec | 7346 | Location loc = this->location(); |
7347 | ||
7348 | switch (this->code_) | |
35a54f17 | 7349 | { |
16cb7fec | 7350 | default: |
7351 | break; | |
7352 | ||
7353 | case BUILTIN_APPEND: | |
321e5ad2 | 7354 | return this->flatten_append(gogo, function, inserter); |
7355 | ||
16cb7fec | 7356 | case BUILTIN_COPY: |
7357 | { | |
7358 | Type* at = this->args()->front()->type(); | |
7359 | for (Expression_list::iterator pa = this->args()->begin(); | |
7360 | pa != this->args()->end(); | |
7361 | ++pa) | |
7362 | { | |
7363 | if ((*pa)->is_nil_expression()) | |
7364 | { | |
7365 | Expression* nil = Expression::make_nil(loc); | |
7366 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
7367 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
7368 | } | |
7369 | if (!(*pa)->is_variable()) | |
7370 | { | |
7371 | Temporary_statement* temp = | |
7372 | Statement::make_temporary(NULL, *pa, loc); | |
7373 | inserter->insert(temp); | |
7374 | *pa = Expression::make_temporary_reference(temp, loc); | |
7375 | } | |
7376 | } | |
7377 | } | |
7378 | break; | |
7379 | ||
7380 | case BUILTIN_PANIC: | |
35a54f17 | 7381 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 7382 | pa != this->args()->end(); |
7383 | ++pa) | |
7384 | { | |
7385 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 7386 | { |
16cb7fec | 7387 | Temporary_statement* temp = |
7388 | Statement::make_temporary(NULL, *pa, loc); | |
7389 | inserter->insert(temp); | |
7390 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 7391 | } |
16cb7fec | 7392 | } |
7739537f | 7393 | break; |
0d5530d9 | 7394 | |
7395 | case BUILTIN_LEN: | |
132ed071 | 7396 | case BUILTIN_CAP: |
321e5ad2 | 7397 | { |
7398 | Expression_list::iterator pa = this->args()->begin(); | |
7399 | if (!(*pa)->is_variable() | |
7400 | && ((*pa)->type()->map_type() != NULL | |
7401 | || (*pa)->type()->channel_type() != NULL)) | |
7402 | { | |
7403 | Temporary_statement* temp = | |
7404 | Statement::make_temporary(NULL, *pa, loc); | |
7405 | inserter->insert(temp); | |
7406 | *pa = Expression::make_temporary_reference(temp, loc); | |
7407 | } | |
7408 | } | |
7409 | break; | |
35a54f17 | 7410 | } |
16cb7fec | 7411 | |
35a54f17 | 7412 | return this; |
7413 | } | |
7414 | ||
a9182619 | 7415 | // Lower a make expression. |
7416 | ||
7417 | Expression* | |
321e5ad2 | 7418 | Builtin_call_expression::lower_make(Statement_inserter* inserter) |
a9182619 | 7419 | { |
b13c66cd | 7420 | Location loc = this->location(); |
a9182619 | 7421 | |
7422 | const Expression_list* args = this->args(); | |
7423 | if (args == NULL || args->size() < 1) | |
7424 | { | |
7425 | this->report_error(_("not enough arguments")); | |
7426 | return Expression::make_error(this->location()); | |
7427 | } | |
7428 | ||
7429 | Expression_list::const_iterator parg = args->begin(); | |
7430 | ||
7431 | Expression* first_arg = *parg; | |
7432 | if (!first_arg->is_type_expression()) | |
7433 | { | |
631d5788 | 7434 | go_error_at(first_arg->location(), "expected type"); |
a9182619 | 7435 | this->set_is_error(); |
7436 | return Expression::make_error(this->location()); | |
7437 | } | |
7438 | Type* type = first_arg->type(); | |
7439 | ||
22deed0d | 7440 | if (!type->in_heap()) |
7441 | go_error_at(first_arg->location(), | |
7442 | "can't make slice of go:notinheap type"); | |
7443 | ||
a9182619 | 7444 | bool is_slice = false; |
7445 | bool is_map = false; | |
7446 | bool is_chan = false; | |
411eb89e | 7447 | if (type->is_slice_type()) |
a9182619 | 7448 | is_slice = true; |
7449 | else if (type->map_type() != NULL) | |
7450 | is_map = true; | |
7451 | else if (type->channel_type() != NULL) | |
7452 | is_chan = true; | |
7453 | else | |
7454 | { | |
7455 | this->report_error(_("invalid type for make function")); | |
7456 | return Expression::make_error(this->location()); | |
7457 | } | |
7458 | ||
f6bc81e6 | 7459 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7460 | ||
a9182619 | 7461 | ++parg; |
7462 | Expression* len_arg; | |
ccea2b36 | 7463 | bool len_small = false; |
a9182619 | 7464 | if (parg == args->end()) |
7465 | { | |
7466 | if (is_slice) | |
7467 | { | |
7468 | this->report_error(_("length required when allocating a slice")); | |
7469 | return Expression::make_error(this->location()); | |
7470 | } | |
e67508fa | 7471 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
33d1d391 | 7472 | len_small = true; |
a9182619 | 7473 | } |
7474 | else | |
7475 | { | |
7476 | len_arg = *parg; | |
f6bc81e6 | 7477 | len_arg->determine_type(&int_context); |
ccea2b36 | 7478 | if (!this->check_int_value(len_arg, true, &len_small)) |
1ad00fd4 | 7479 | return Expression::make_error(this->location()); |
a9182619 | 7480 | ++parg; |
7481 | } | |
7482 | ||
7483 | Expression* cap_arg = NULL; | |
ccea2b36 | 7484 | bool cap_small = false; |
72bf0e6e | 7485 | Numeric_constant nclen; |
7486 | Numeric_constant nccap; | |
7487 | unsigned long vlen; | |
7488 | unsigned long vcap; | |
a9182619 | 7489 | if (is_slice && parg != args->end()) |
7490 | { | |
7491 | cap_arg = *parg; | |
f6bc81e6 | 7492 | cap_arg->determine_type(&int_context); |
ccea2b36 | 7493 | if (!this->check_int_value(cap_arg, false, &cap_small)) |
1ad00fd4 | 7494 | return Expression::make_error(this->location()); |
7495 | ||
1ad00fd4 | 7496 | if (len_arg->numeric_constant_value(&nclen) |
7497 | && cap_arg->numeric_constant_value(&nccap) | |
7498 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7499 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7500 | && vlen > vcap) | |
a9182619 | 7501 | { |
1ad00fd4 | 7502 | this->report_error(_("len larger than cap")); |
a9182619 | 7503 | return Expression::make_error(this->location()); |
7504 | } | |
1ad00fd4 | 7505 | |
a9182619 | 7506 | ++parg; |
7507 | } | |
7508 | ||
7509 | if (parg != args->end()) | |
7510 | { | |
7511 | this->report_error(_("too many arguments to make")); | |
7512 | return Expression::make_error(this->location()); | |
7513 | } | |
7514 | ||
b13c66cd | 7515 | Location type_loc = first_arg->location(); |
a9182619 | 7516 | |
7517 | Expression* call; | |
7518 | if (is_slice) | |
7519 | { | |
7520 | if (cap_arg == NULL) | |
321e5ad2 | 7521 | { |
72bf0e6e | 7522 | cap_small = len_small; |
7523 | if (len_arg->numeric_constant_value(&nclen) | |
7524 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID) | |
7525 | cap_arg = Expression::make_integer_ul(vlen, len_arg->type(), loc); | |
7526 | else | |
7527 | { | |
7528 | Temporary_statement* temp = Statement::make_temporary(NULL, | |
7529 | len_arg, | |
7530 | loc); | |
7531 | inserter->insert(temp); | |
7532 | len_arg = Expression::make_temporary_reference(temp, loc); | |
7533 | cap_arg = Expression::make_temporary_reference(temp, loc); | |
7534 | } | |
321e5ad2 | 7535 | } |
ccea2b36 | 7536 | |
72bf0e6e | 7537 | Type* et = type->array_type()->element_type(); |
7538 | Expression* type_arg = Expression::make_type_descriptor(et, type_loc); | |
ccea2b36 | 7539 | Runtime::Function code = Runtime::MAKESLICE; |
7540 | if (!len_small || !cap_small) | |
7541 | code = Runtime::MAKESLICE64; | |
7542 | call = Runtime::make_call(code, loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7543 | } |
7544 | else if (is_map) | |
321e5ad2 | 7545 | { |
7546 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
33d1d391 | 7547 | if (!len_small) |
7548 | call = Runtime::make_call(Runtime::MAKEMAP64, loc, 3, type_arg, | |
7549 | len_arg, | |
7550 | Expression::make_nil(loc)); | |
7551 | else | |
7552 | { | |
7553 | Numeric_constant nclen; | |
7554 | unsigned long vlen; | |
7555 | if (len_arg->numeric_constant_value(&nclen) | |
7556 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7557 | && vlen <= Map_type::bucket_size) | |
7558 | call = Runtime::make_call(Runtime::MAKEMAP_SMALL, loc, 0); | |
7559 | else | |
7560 | call = Runtime::make_call(Runtime::MAKEMAP, loc, 3, type_arg, | |
7561 | len_arg, | |
7562 | Expression::make_nil(loc)); | |
7563 | } | |
321e5ad2 | 7564 | } |
a9182619 | 7565 | else if (is_chan) |
321e5ad2 | 7566 | { |
7567 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
7568 | call = Runtime::make_call(Runtime::MAKECHAN, loc, 2, type_arg, len_arg); | |
7569 | } | |
a9182619 | 7570 | else |
7571 | go_unreachable(); | |
7572 | ||
7573 | return Expression::make_unsafe_cast(type, call, loc); | |
7574 | } | |
7575 | ||
321e5ad2 | 7576 | // Flatten a call to the predeclared append function. We do this in |
7577 | // the flatten phase, not the lowering phase, so that we run after | |
7578 | // type checking and after order_evaluations. | |
7579 | ||
7580 | Expression* | |
7581 | Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function, | |
7582 | Statement_inserter* inserter) | |
7583 | { | |
7584 | if (this->is_error_expression()) | |
7585 | return this; | |
7586 | ||
7587 | Location loc = this->location(); | |
7588 | ||
7589 | const Expression_list* args = this->args(); | |
7590 | go_assert(args != NULL && !args->empty()); | |
7591 | ||
7592 | Type* slice_type = args->front()->type(); | |
7593 | go_assert(slice_type->is_slice_type()); | |
7594 | Type* element_type = slice_type->array_type()->element_type(); | |
7595 | ||
7596 | if (args->size() == 1) | |
7597 | { | |
7598 | // append(s) evaluates to s. | |
7599 | return args->front(); | |
7600 | } | |
7601 | ||
7602 | Type* int_type = Type::lookup_integer_type("int"); | |
7603 | Type* uint_type = Type::lookup_integer_type("uint"); | |
7604 | ||
7605 | // Implementing | |
7606 | // append(s1, s2...) | |
7607 | // or | |
7608 | // append(s1, a1, a2, a3, ...) | |
7609 | ||
7610 | // s1tmp := s1 | |
7611 | Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(), | |
7612 | loc); | |
7613 | inserter->insert(s1tmp); | |
7614 | ||
7615 | // l1tmp := len(s1tmp) | |
7616 | Named_object* lenfn = gogo->lookup_global("len"); | |
7617 | Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7618 | Expression_list* call_args = new Expression_list(); | |
7619 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7620 | Expression* len = Expression::make_call(lenref, call_args, false, loc); | |
7621 | gogo->lower_expression(function, inserter, &len); | |
7622 | gogo->flatten_expression(function, inserter, &len); | |
7623 | Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc); | |
7624 | inserter->insert(l1tmp); | |
7625 | ||
7626 | Temporary_statement* s2tmp = NULL; | |
7627 | Temporary_statement* l2tmp = NULL; | |
7628 | Expression_list* add = NULL; | |
7629 | Expression* len2; | |
7630 | if (this->is_varargs()) | |
7631 | { | |
7632 | go_assert(args->size() == 2); | |
7633 | ||
7634 | // s2tmp := s2 | |
7635 | s2tmp = Statement::make_temporary(NULL, args->back(), loc); | |
7636 | inserter->insert(s2tmp); | |
7637 | ||
7638 | // l2tmp := len(s2tmp) | |
7639 | lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7640 | call_args = new Expression_list(); | |
7641 | call_args->push_back(Expression::make_temporary_reference(s2tmp, loc)); | |
7642 | len = Expression::make_call(lenref, call_args, false, loc); | |
7643 | gogo->lower_expression(function, inserter, &len); | |
7644 | gogo->flatten_expression(function, inserter, &len); | |
7645 | l2tmp = Statement::make_temporary(int_type, len, loc); | |
7646 | inserter->insert(l2tmp); | |
7647 | ||
7648 | // len2 = l2tmp | |
7649 | len2 = Expression::make_temporary_reference(l2tmp, loc); | |
7650 | } | |
7651 | else | |
7652 | { | |
7653 | // We have to ensure that all the arguments are in variables | |
7654 | // now, because otherwise if one of them is an index expression | |
7655 | // into the current slice we could overwrite it before we fetch | |
7656 | // it. | |
7657 | add = new Expression_list(); | |
7658 | Expression_list::const_iterator pa = args->begin(); | |
7659 | for (++pa; pa != args->end(); ++pa) | |
7660 | { | |
7661 | if ((*pa)->is_variable()) | |
7662 | add->push_back(*pa); | |
7663 | else | |
7664 | { | |
7665 | Temporary_statement* tmp = Statement::make_temporary(NULL, *pa, | |
7666 | loc); | |
7667 | inserter->insert(tmp); | |
7668 | add->push_back(Expression::make_temporary_reference(tmp, loc)); | |
7669 | } | |
7670 | } | |
7671 | ||
7672 | // len2 = len(add) | |
7673 | len2 = Expression::make_integer_ul(add->size(), int_type, loc); | |
7674 | } | |
7675 | ||
7676 | // ntmp := l1tmp + len2 | |
7677 | Expression* ref = Expression::make_temporary_reference(l1tmp, loc); | |
7678 | Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc); | |
7679 | gogo->lower_expression(function, inserter, &sum); | |
7680 | gogo->flatten_expression(function, inserter, &sum); | |
7681 | Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc); | |
7682 | inserter->insert(ntmp); | |
7683 | ||
7684 | // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ? | |
7685 | // growslice(type, s1tmp, ntmp) : | |
7686 | // s1tmp[:ntmp] | |
7687 | // Using uint here means that if the computation of ntmp overflowed, | |
7688 | // we will call growslice which will panic. | |
7689 | ||
7690 | Expression* left = Expression::make_temporary_reference(ntmp, loc); | |
7691 | left = Expression::make_cast(uint_type, left, loc); | |
7692 | ||
7693 | Named_object* capfn = gogo->lookup_global("cap"); | |
7694 | Expression* capref = Expression::make_func_reference(capfn, NULL, loc); | |
7695 | call_args = new Expression_list(); | |
7696 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7697 | Expression* right = Expression::make_call(capref, call_args, false, loc); | |
7698 | right = Expression::make_cast(uint_type, right, loc); | |
7699 | ||
7700 | Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc); | |
7701 | ||
7702 | Expression* a1 = Expression::make_type_descriptor(element_type, loc); | |
7703 | Expression* a2 = Expression::make_temporary_reference(s1tmp, loc); | |
7704 | Expression* a3 = Expression::make_temporary_reference(ntmp, loc); | |
7705 | Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3, | |
7706 | a1, a2, a3); | |
7707 | call = Expression::make_unsafe_cast(slice_type, call, loc); | |
7708 | ||
7709 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7710 | Expression* zero = Expression::make_integer_ul(0, int_type, loc); | |
7711 | Expression* ref2 = Expression::make_temporary_reference(ntmp, loc); | |
7712 | // FIXME: Mark this index as not requiring bounds checks. | |
7713 | ref = Expression::make_index(ref, zero, ref2, NULL, loc); | |
7714 | ||
7715 | Expression* rhs = Expression::make_conditional(cond, call, ref, loc); | |
7716 | ||
7717 | gogo->lower_expression(function, inserter, &rhs); | |
7718 | gogo->flatten_expression(function, inserter, &rhs); | |
7719 | ||
7720 | Expression* lhs = Expression::make_temporary_reference(s1tmp, loc); | |
7721 | Statement* assign = Statement::make_assignment(lhs, rhs, loc); | |
7722 | inserter->insert(assign); | |
7723 | ||
7724 | if (this->is_varargs()) | |
7725 | { | |
7726 | // copy(s1tmp[l1tmp:], s2tmp) | |
7727 | a1 = Expression::make_temporary_reference(s1tmp, loc); | |
7728 | ref = Expression::make_temporary_reference(l1tmp, loc); | |
7729 | Expression* nil = Expression::make_nil(loc); | |
7730 | // FIXME: Mark this index as not requiring bounds checks. | |
7731 | a1 = Expression::make_index(a1, ref, nil, NULL, loc); | |
7732 | ||
7733 | a2 = Expression::make_temporary_reference(s2tmp, loc); | |
7734 | ||
7735 | Named_object* copyfn = gogo->lookup_global("copy"); | |
7736 | Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc); | |
7737 | call_args = new Expression_list(); | |
7738 | call_args->push_back(a1); | |
7739 | call_args->push_back(a2); | |
7740 | call = Expression::make_call(copyref, call_args, false, loc); | |
7741 | gogo->lower_expression(function, inserter, &call); | |
7742 | gogo->flatten_expression(function, inserter, &call); | |
7743 | inserter->insert(Statement::make_statement(call, false)); | |
7744 | } | |
7745 | else | |
7746 | { | |
7747 | // For each argument: | |
7748 | // s1tmp[l1tmp+i] = a | |
7749 | unsigned long i = 0; | |
7750 | for (Expression_list::const_iterator pa = add->begin(); | |
7751 | pa != add->end(); | |
7752 | ++pa, ++i) | |
7753 | { | |
7754 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7755 | ref2 = Expression::make_temporary_reference(l1tmp, loc); | |
7756 | Expression* off = Expression::make_integer_ul(i, int_type, loc); | |
7757 | ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc); | |
7758 | // FIXME: Mark this index as not requiring bounds checks. | |
7759 | lhs = Expression::make_index(ref, ref2, NULL, NULL, loc); | |
7760 | gogo->lower_expression(function, inserter, &lhs); | |
7761 | gogo->flatten_expression(function, inserter, &lhs); | |
03118c21 | 7762 | // The flatten pass runs after the write barrier pass, so we |
7763 | // need to insert a write barrier here if necessary. | |
7764 | if (!gogo->assign_needs_write_barrier(lhs)) | |
7765 | assign = Statement::make_assignment(lhs, *pa, loc); | |
7766 | else | |
7767 | { | |
7768 | Function* f = function == NULL ? NULL : function->func_value(); | |
7769 | assign = gogo->assign_with_write_barrier(f, NULL, inserter, | |
7770 | lhs, *pa, loc); | |
7771 | } | |
321e5ad2 | 7772 | inserter->insert(assign); |
7773 | } | |
7774 | } | |
7775 | ||
7776 | return Expression::make_temporary_reference(s1tmp, loc); | |
7777 | } | |
7778 | ||
a9182619 | 7779 | // Return whether an expression has an integer value. Report an error |
7780 | // if not. This is used when handling calls to the predeclared make | |
ccea2b36 | 7781 | // function. Set *SMALL if the value is known to fit in type "int". |
a9182619 | 7782 | |
7783 | bool | |
ccea2b36 | 7784 | Builtin_call_expression::check_int_value(Expression* e, bool is_length, |
7785 | bool *small) | |
a9182619 | 7786 | { |
ccea2b36 | 7787 | *small = false; |
7788 | ||
0c77715b | 7789 | Numeric_constant nc; |
1ad00fd4 | 7790 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7791 | { |
1ad00fd4 | 7792 | unsigned long v; |
7793 | switch (nc.to_unsigned_long(&v)) | |
7794 | { | |
7795 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7796 | break; |
1ad00fd4 | 7797 | case Numeric_constant::NC_UL_NOTINT: |
631d5788 | 7798 | go_error_at(e->location(), "non-integer %s argument to make", |
7799 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7800 | return false; |
7801 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 7802 | go_error_at(e->location(), "negative %s argument to make", |
7803 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7804 | return false; |
7805 | case Numeric_constant::NC_UL_BIG: | |
7806 | // We don't want to give a compile-time error for a 64-bit | |
7807 | // value on a 32-bit target. | |
1b10c5e7 | 7808 | break; |
1ad00fd4 | 7809 | } |
1b10c5e7 | 7810 | |
7811 | mpz_t val; | |
7812 | if (!nc.to_int(&val)) | |
7813 | go_unreachable(); | |
7814 | int bits = mpz_sizeinbase(val, 2); | |
7815 | mpz_clear(val); | |
7816 | Type* int_type = Type::lookup_integer_type("int"); | |
7817 | if (bits >= int_type->integer_type()->bits()) | |
7818 | { | |
631d5788 | 7819 | go_error_at(e->location(), "%s argument too large for make", |
7820 | is_length ? "len" : "cap"); | |
1b10c5e7 | 7821 | return false; |
7822 | } | |
7823 | ||
ccea2b36 | 7824 | *small = true; |
1b10c5e7 | 7825 | return true; |
a9182619 | 7826 | } |
7827 | ||
1ad00fd4 | 7828 | if (e->type()->integer_type() != NULL) |
ccea2b36 | 7829 | { |
7830 | int ebits = e->type()->integer_type()->bits(); | |
7831 | int intbits = Type::lookup_integer_type("int")->integer_type()->bits(); | |
7832 | ||
7833 | // We can treat ebits == intbits as small even for an unsigned | |
7834 | // integer type, because we will convert the value to int and | |
7835 | // then reject it in the runtime if it is negative. | |
7836 | *small = ebits <= intbits; | |
7837 | ||
7838 | return true; | |
7839 | } | |
1ad00fd4 | 7840 | |
631d5788 | 7841 | go_error_at(e->location(), "non-integer %s argument to make", |
7842 | is_length ? "len" : "cap"); | |
a9182619 | 7843 | return false; |
7844 | } | |
7845 | ||
e440a328 | 7846 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7847 | // the argument. We need to map complex64 to float32 and complex128 |
7848 | // to float64, so it has to be done by name. This returns NULL if it | |
7849 | // can't figure out the type. | |
e440a328 | 7850 | |
7851 | Type* | |
7852 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7853 | { | |
7854 | if (arg_type == NULL || arg_type->is_abstract()) | |
7855 | return NULL; | |
7856 | Named_type* nt = arg_type->named_type(); | |
7857 | if (nt == NULL) | |
7858 | return NULL; | |
7859 | while (nt->real_type()->named_type() != NULL) | |
7860 | nt = nt->real_type()->named_type(); | |
48080209 | 7861 | if (nt->name() == "complex64") |
e440a328 | 7862 | return Type::lookup_float_type("float32"); |
7863 | else if (nt->name() == "complex128") | |
7864 | return Type::lookup_float_type("float64"); | |
7865 | else | |
7866 | return NULL; | |
7867 | } | |
7868 | ||
48080209 | 7869 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7870 | // argments. Like real_imag_type, we have to map by name. |
7871 | ||
7872 | Type* | |
48080209 | 7873 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7874 | { |
7875 | if (arg_type == NULL || arg_type->is_abstract()) | |
7876 | return NULL; | |
7877 | Named_type* nt = arg_type->named_type(); | |
7878 | if (nt == NULL) | |
7879 | return NULL; | |
7880 | while (nt->real_type()->named_type() != NULL) | |
7881 | nt = nt->real_type()->named_type(); | |
48080209 | 7882 | if (nt->name() == "float32") |
e440a328 | 7883 | return Type::lookup_complex_type("complex64"); |
7884 | else if (nt->name() == "float64") | |
7885 | return Type::lookup_complex_type("complex128"); | |
7886 | else | |
7887 | return NULL; | |
7888 | } | |
7889 | ||
7890 | // Return a single argument, or NULL if there isn't one. | |
7891 | ||
7892 | Expression* | |
7893 | Builtin_call_expression::one_arg() const | |
7894 | { | |
7895 | const Expression_list* args = this->args(); | |
aa615cb3 | 7896 | if (args == NULL || args->size() != 1) |
e440a328 | 7897 | return NULL; |
7898 | return args->front(); | |
7899 | } | |
7900 | ||
83921647 | 7901 | // A traversal class which looks for a call or receive expression. |
7902 | ||
7903 | class Find_call_expression : public Traverse | |
7904 | { | |
7905 | public: | |
7906 | Find_call_expression() | |
7907 | : Traverse(traverse_expressions), | |
7908 | found_(false) | |
7909 | { } | |
7910 | ||
7911 | int | |
7912 | expression(Expression**); | |
7913 | ||
7914 | bool | |
7915 | found() | |
7916 | { return this->found_; } | |
7917 | ||
7918 | private: | |
7919 | bool found_; | |
7920 | }; | |
7921 | ||
7922 | int | |
7923 | Find_call_expression::expression(Expression** pexpr) | |
7924 | { | |
7925 | if ((*pexpr)->call_expression() != NULL | |
7926 | || (*pexpr)->receive_expression() != NULL) | |
7927 | { | |
7928 | this->found_ = true; | |
7929 | return TRAVERSE_EXIT; | |
7930 | } | |
7931 | return TRAVERSE_CONTINUE; | |
7932 | } | |
7933 | ||
7934 | // Return whether this is constant: len of a string constant, or len | |
7935 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7936 | // unsafe.Alignof. | |
e440a328 | 7937 | |
7938 | bool | |
7939 | Builtin_call_expression::do_is_constant() const | |
7940 | { | |
12e69faa | 7941 | if (this->is_error_expression()) |
7942 | return true; | |
e440a328 | 7943 | switch (this->code_) |
7944 | { | |
7945 | case BUILTIN_LEN: | |
7946 | case BUILTIN_CAP: | |
7947 | { | |
0f914071 | 7948 | if (this->seen_) |
7949 | return false; | |
7950 | ||
e440a328 | 7951 | Expression* arg = this->one_arg(); |
7952 | if (arg == NULL) | |
7953 | return false; | |
7954 | Type* arg_type = arg->type(); | |
7955 | ||
7956 | if (arg_type->points_to() != NULL | |
7957 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7958 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7959 | arg_type = arg_type->points_to(); |
7960 | ||
83921647 | 7961 | // The len and cap functions are only constant if there are no |
7962 | // function calls or channel operations in the arguments. | |
7963 | // Otherwise we have to make the call. | |
7964 | if (!arg->is_constant()) | |
7965 | { | |
7966 | Find_call_expression find_call; | |
7967 | Expression::traverse(&arg, &find_call); | |
7968 | if (find_call.found()) | |
7969 | return false; | |
7970 | } | |
7971 | ||
e440a328 | 7972 | if (arg_type->array_type() != NULL |
7973 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7974 | return true; |
e440a328 | 7975 | |
7976 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7977 | { |
7978 | this->seen_ = true; | |
7979 | bool ret = arg->is_constant(); | |
7980 | this->seen_ = false; | |
7981 | return ret; | |
7982 | } | |
e440a328 | 7983 | } |
7984 | break; | |
7985 | ||
7986 | case BUILTIN_SIZEOF: | |
7987 | case BUILTIN_ALIGNOF: | |
7988 | return this->one_arg() != NULL; | |
7989 | ||
7990 | case BUILTIN_OFFSETOF: | |
7991 | { | |
7992 | Expression* arg = this->one_arg(); | |
7993 | if (arg == NULL) | |
7994 | return false; | |
7995 | return arg->field_reference_expression() != NULL; | |
7996 | } | |
7997 | ||
48080209 | 7998 | case BUILTIN_COMPLEX: |
e440a328 | 7999 | { |
8000 | const Expression_list* args = this->args(); | |
8001 | if (args != NULL && args->size() == 2) | |
8002 | return args->front()->is_constant() && args->back()->is_constant(); | |
8003 | } | |
8004 | break; | |
8005 | ||
8006 | case BUILTIN_REAL: | |
8007 | case BUILTIN_IMAG: | |
8008 | { | |
8009 | Expression* arg = this->one_arg(); | |
8010 | return arg != NULL && arg->is_constant(); | |
8011 | } | |
8012 | ||
8013 | default: | |
8014 | break; | |
8015 | } | |
8016 | ||
8017 | return false; | |
8018 | } | |
8019 | ||
0c77715b | 8020 | // Return a numeric constant if possible. |
e440a328 | 8021 | |
8022 | bool | |
0c77715b | 8023 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 8024 | { |
8025 | if (this->code_ == BUILTIN_LEN | |
8026 | || this->code_ == BUILTIN_CAP) | |
8027 | { | |
8028 | Expression* arg = this->one_arg(); | |
8029 | if (arg == NULL) | |
8030 | return false; | |
8031 | Type* arg_type = arg->type(); | |
8032 | ||
8033 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
8034 | { | |
8035 | std::string sval; | |
8036 | if (arg->string_constant_value(&sval)) | |
8037 | { | |
0c77715b | 8038 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
8039 | sval.length()); | |
e440a328 | 8040 | return true; |
8041 | } | |
8042 | } | |
8043 | ||
8044 | if (arg_type->points_to() != NULL | |
8045 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8046 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8047 | arg_type = arg_type->points_to(); |
8048 | ||
8049 | if (arg_type->array_type() != NULL | |
8050 | && arg_type->array_type()->length() != NULL) | |
8051 | { | |
0f914071 | 8052 | if (this->seen_) |
8053 | return false; | |
e440a328 | 8054 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 8055 | this->seen_ = true; |
0c77715b | 8056 | bool r = e->numeric_constant_value(nc); |
0f914071 | 8057 | this->seen_ = false; |
8058 | if (r) | |
e440a328 | 8059 | { |
0c77715b | 8060 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
8061 | this->location())) | |
8062 | r = false; | |
e440a328 | 8063 | } |
0c77715b | 8064 | return r; |
e440a328 | 8065 | } |
8066 | } | |
8067 | else if (this->code_ == BUILTIN_SIZEOF | |
8068 | || this->code_ == BUILTIN_ALIGNOF) | |
8069 | { | |
8070 | Expression* arg = this->one_arg(); | |
8071 | if (arg == NULL) | |
8072 | return false; | |
8073 | Type* arg_type = arg->type(); | |
5c13bd80 | 8074 | if (arg_type->is_error()) |
e440a328 | 8075 | return false; |
8076 | if (arg_type->is_abstract()) | |
8077 | return false; | |
2c809f8f | 8078 | if (this->seen_) |
8079 | return false; | |
927a01eb | 8080 | |
3f378015 | 8081 | int64_t ret; |
e440a328 | 8082 | if (this->code_ == BUILTIN_SIZEOF) |
8083 | { | |
2c809f8f | 8084 | this->seen_ = true; |
8085 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
8086 | this->seen_ = false; | |
8087 | if (!ok) | |
e440a328 | 8088 | return false; |
8089 | } | |
8090 | else if (this->code_ == BUILTIN_ALIGNOF) | |
8091 | { | |
2c809f8f | 8092 | bool ok; |
8093 | this->seen_ = true; | |
637bd3af | 8094 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 8095 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 8096 | else |
e440a328 | 8097 | { |
8098 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
8099 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 8100 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 8101 | } |
2c809f8f | 8102 | this->seen_ = false; |
8103 | if (!ok) | |
8104 | return false; | |
e440a328 | 8105 | } |
8106 | else | |
c3e6f413 | 8107 | go_unreachable(); |
927a01eb | 8108 | |
3f378015 | 8109 | mpz_t zval; |
8110 | set_mpz_from_int64(&zval, ret); | |
8111 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8112 | mpz_clear(zval); | |
e440a328 | 8113 | return true; |
8114 | } | |
8115 | else if (this->code_ == BUILTIN_OFFSETOF) | |
8116 | { | |
8117 | Expression* arg = this->one_arg(); | |
8118 | if (arg == NULL) | |
8119 | return false; | |
8120 | Field_reference_expression* farg = arg->field_reference_expression(); | |
8121 | if (farg == NULL) | |
8122 | return false; | |
2c809f8f | 8123 | if (this->seen_) |
8124 | return false; | |
8125 | ||
3f378015 | 8126 | int64_t total_offset = 0; |
9a4bd570 | 8127 | while (true) |
8128 | { | |
8129 | Expression* struct_expr = farg->expr(); | |
8130 | Type* st = struct_expr->type(); | |
8131 | if (st->struct_type() == NULL) | |
8132 | return false; | |
8133 | if (st->named_type() != NULL) | |
8134 | st->named_type()->convert(this->gogo_); | |
3f378015 | 8135 | int64_t offset; |
2c809f8f | 8136 | this->seen_ = true; |
8137 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
8138 | farg->field_index(), | |
8139 | &offset); | |
8140 | this->seen_ = false; | |
8141 | if (!ok) | |
8142 | return false; | |
9a4bd570 | 8143 | total_offset += offset; |
8144 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
8145 | { | |
8146 | // Go up until we reach the original base. | |
8147 | farg = struct_expr->field_reference_expression(); | |
8148 | continue; | |
8149 | } | |
8150 | break; | |
8151 | } | |
3f378015 | 8152 | mpz_t zval; |
8153 | set_mpz_from_int64(&zval, total_offset); | |
8154 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8155 | mpz_clear(zval); | |
e440a328 | 8156 | return true; |
8157 | } | |
0c77715b | 8158 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 8159 | { |
8160 | Expression* arg = this->one_arg(); | |
8161 | if (arg == NULL) | |
8162 | return false; | |
8163 | ||
0c77715b | 8164 | Numeric_constant argnc; |
8165 | if (!arg->numeric_constant_value(&argnc)) | |
8166 | return false; | |
8167 | ||
fcbea5e4 | 8168 | mpc_t val; |
8169 | if (!argnc.to_complex(&val)) | |
0c77715b | 8170 | return false; |
e440a328 | 8171 | |
0c77715b | 8172 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
8173 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 8174 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 8175 | else |
fcbea5e4 | 8176 | nc->set_float(type, mpc_imagref(val)); |
8177 | mpc_clear(val); | |
0c77715b | 8178 | return true; |
e440a328 | 8179 | } |
0c77715b | 8180 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 8181 | { |
8182 | const Expression_list* args = this->args(); | |
8183 | if (args == NULL || args->size() != 2) | |
8184 | return false; | |
8185 | ||
0c77715b | 8186 | Numeric_constant rnc; |
8187 | if (!args->front()->numeric_constant_value(&rnc)) | |
8188 | return false; | |
8189 | Numeric_constant inc; | |
8190 | if (!args->back()->numeric_constant_value(&inc)) | |
8191 | return false; | |
8192 | ||
8193 | if (rnc.type() != NULL | |
8194 | && !rnc.type()->is_abstract() | |
8195 | && inc.type() != NULL | |
8196 | && !inc.type()->is_abstract() | |
8197 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
8198 | return false; | |
8199 | ||
e440a328 | 8200 | mpfr_t r; |
0c77715b | 8201 | if (!rnc.to_float(&r)) |
8202 | return false; | |
8203 | mpfr_t i; | |
8204 | if (!inc.to_float(&i)) | |
e440a328 | 8205 | { |
8206 | mpfr_clear(r); | |
8207 | return false; | |
8208 | } | |
8209 | ||
0c77715b | 8210 | Type* arg_type = rnc.type(); |
8211 | if (arg_type == NULL || arg_type->is_abstract()) | |
8212 | arg_type = inc.type(); | |
e440a328 | 8213 | |
fcbea5e4 | 8214 | mpc_t val; |
8215 | mpc_init2(val, mpc_precision); | |
8216 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 8217 | mpfr_clear(r); |
8218 | mpfr_clear(i); | |
8219 | ||
fcbea5e4 | 8220 | Type* type = Builtin_call_expression::complex_type(arg_type); |
8221 | nc->set_complex(type, val); | |
8222 | ||
8223 | mpc_clear(val); | |
8224 | ||
0c77715b | 8225 | return true; |
e440a328 | 8226 | } |
8227 | ||
8228 | return false; | |
8229 | } | |
8230 | ||
a7549a6a | 8231 | // Give an error if we are discarding the value of an expression which |
8232 | // should not normally be discarded. We don't give an error for | |
8233 | // discarding the value of an ordinary function call, but we do for | |
8234 | // builtin functions, purely for consistency with the gc compiler. | |
8235 | ||
4f2138d7 | 8236 | bool |
a7549a6a | 8237 | Builtin_call_expression::do_discarding_value() |
8238 | { | |
8239 | switch (this->code_) | |
8240 | { | |
8241 | case BUILTIN_INVALID: | |
8242 | default: | |
8243 | go_unreachable(); | |
8244 | ||
8245 | case BUILTIN_APPEND: | |
8246 | case BUILTIN_CAP: | |
8247 | case BUILTIN_COMPLEX: | |
8248 | case BUILTIN_IMAG: | |
8249 | case BUILTIN_LEN: | |
8250 | case BUILTIN_MAKE: | |
8251 | case BUILTIN_NEW: | |
8252 | case BUILTIN_REAL: | |
8253 | case BUILTIN_ALIGNOF: | |
8254 | case BUILTIN_OFFSETOF: | |
8255 | case BUILTIN_SIZEOF: | |
8256 | this->unused_value_error(); | |
4f2138d7 | 8257 | return false; |
a7549a6a | 8258 | |
8259 | case BUILTIN_CLOSE: | |
8260 | case BUILTIN_COPY: | |
1cce762f | 8261 | case BUILTIN_DELETE: |
a7549a6a | 8262 | case BUILTIN_PANIC: |
8263 | case BUILTIN_PRINT: | |
8264 | case BUILTIN_PRINTLN: | |
8265 | case BUILTIN_RECOVER: | |
4f2138d7 | 8266 | return true; |
a7549a6a | 8267 | } |
8268 | } | |
8269 | ||
e440a328 | 8270 | // Return the type. |
8271 | ||
8272 | Type* | |
8273 | Builtin_call_expression::do_type() | |
8274 | { | |
79651b1f | 8275 | if (this->is_error_expression()) |
8276 | return Type::make_error_type(); | |
e440a328 | 8277 | switch (this->code_) |
8278 | { | |
8279 | case BUILTIN_INVALID: | |
8280 | default: | |
79651b1f | 8281 | return Type::make_error_type(); |
e440a328 | 8282 | |
8283 | case BUILTIN_NEW: | |
8284 | case BUILTIN_MAKE: | |
8285 | { | |
8286 | const Expression_list* args = this->args(); | |
8287 | if (args == NULL || args->empty()) | |
8288 | return Type::make_error_type(); | |
8289 | return Type::make_pointer_type(args->front()->type()); | |
8290 | } | |
8291 | ||
8292 | case BUILTIN_CAP: | |
8293 | case BUILTIN_COPY: | |
8294 | case BUILTIN_LEN: | |
7ba86326 | 8295 | return Type::lookup_integer_type("int"); |
8296 | ||
e440a328 | 8297 | case BUILTIN_ALIGNOF: |
8298 | case BUILTIN_OFFSETOF: | |
8299 | case BUILTIN_SIZEOF: | |
7ba86326 | 8300 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 8301 | |
8302 | case BUILTIN_CLOSE: | |
1cce762f | 8303 | case BUILTIN_DELETE: |
e440a328 | 8304 | case BUILTIN_PANIC: |
8305 | case BUILTIN_PRINT: | |
8306 | case BUILTIN_PRINTLN: | |
8307 | return Type::make_void_type(); | |
8308 | ||
e440a328 | 8309 | case BUILTIN_RECOVER: |
823c7e3d | 8310 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8311 | |
8312 | case BUILTIN_APPEND: | |
8313 | { | |
8314 | const Expression_list* args = this->args(); | |
8315 | if (args == NULL || args->empty()) | |
8316 | return Type::make_error_type(); | |
3ff4863b | 8317 | Type *ret = args->front()->type(); |
8318 | if (!ret->is_slice_type()) | |
8319 | return Type::make_error_type(); | |
8320 | return ret; | |
e440a328 | 8321 | } |
8322 | ||
8323 | case BUILTIN_REAL: | |
8324 | case BUILTIN_IMAG: | |
8325 | { | |
8326 | Expression* arg = this->one_arg(); | |
8327 | if (arg == NULL) | |
8328 | return Type::make_error_type(); | |
8329 | Type* t = arg->type(); | |
8330 | if (t->is_abstract()) | |
8331 | t = t->make_non_abstract_type(); | |
8332 | t = Builtin_call_expression::real_imag_type(t); | |
8333 | if (t == NULL) | |
8334 | t = Type::make_error_type(); | |
8335 | return t; | |
8336 | } | |
8337 | ||
48080209 | 8338 | case BUILTIN_COMPLEX: |
e440a328 | 8339 | { |
8340 | const Expression_list* args = this->args(); | |
8341 | if (args == NULL || args->size() != 2) | |
8342 | return Type::make_error_type(); | |
8343 | Type* t = args->front()->type(); | |
8344 | if (t->is_abstract()) | |
8345 | { | |
8346 | t = args->back()->type(); | |
8347 | if (t->is_abstract()) | |
8348 | t = t->make_non_abstract_type(); | |
8349 | } | |
48080209 | 8350 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 8351 | if (t == NULL) |
8352 | t = Type::make_error_type(); | |
8353 | return t; | |
8354 | } | |
8355 | } | |
8356 | } | |
8357 | ||
8358 | // Determine the type. | |
8359 | ||
8360 | void | |
8361 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
8362 | { | |
fb94b0ca | 8363 | if (!this->determining_types()) |
8364 | return; | |
8365 | ||
e440a328 | 8366 | this->fn()->determine_type_no_context(); |
8367 | ||
8368 | const Expression_list* args = this->args(); | |
8369 | ||
8370 | bool is_print; | |
8371 | Type* arg_type = NULL; | |
321e5ad2 | 8372 | Type* trailing_arg_types = NULL; |
e440a328 | 8373 | switch (this->code_) |
8374 | { | |
8375 | case BUILTIN_PRINT: | |
8376 | case BUILTIN_PRINTLN: | |
8377 | // Do not force a large integer constant to "int". | |
8378 | is_print = true; | |
8379 | break; | |
8380 | ||
8381 | case BUILTIN_REAL: | |
8382 | case BUILTIN_IMAG: | |
48080209 | 8383 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8384 | if (arg_type == NULL) |
8385 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8386 | is_print = false; |
8387 | break; | |
8388 | ||
48080209 | 8389 | case BUILTIN_COMPLEX: |
e440a328 | 8390 | { |
48080209 | 8391 | // For the complex function the type of one operand can |
e440a328 | 8392 | // determine the type of the other, as in a binary expression. |
8393 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8394 | if (arg_type == NULL) |
8395 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8396 | if (args != NULL && args->size() == 2) |
8397 | { | |
8398 | Type* t1 = args->front()->type(); | |
c849bb59 | 8399 | Type* t2 = args->back()->type(); |
e440a328 | 8400 | if (!t1->is_abstract()) |
8401 | arg_type = t1; | |
8402 | else if (!t2->is_abstract()) | |
8403 | arg_type = t2; | |
8404 | } | |
8405 | is_print = false; | |
8406 | } | |
8407 | break; | |
8408 | ||
321e5ad2 | 8409 | case BUILTIN_APPEND: |
8410 | if (!this->is_varargs() | |
8411 | && args != NULL | |
8412 | && !args->empty() | |
8413 | && args->front()->type()->is_slice_type()) | |
8414 | trailing_arg_types = | |
8415 | args->front()->type()->array_type()->element_type(); | |
8416 | is_print = false; | |
8417 | break; | |
8418 | ||
e440a328 | 8419 | default: |
8420 | is_print = false; | |
8421 | break; | |
8422 | } | |
8423 | ||
8424 | if (args != NULL) | |
8425 | { | |
8426 | for (Expression_list::const_iterator pa = args->begin(); | |
8427 | pa != args->end(); | |
8428 | ++pa) | |
8429 | { | |
8430 | Type_context subcontext; | |
8431 | subcontext.type = arg_type; | |
8432 | ||
8433 | if (is_print) | |
8434 | { | |
8435 | // We want to print large constants, we so can't just | |
8436 | // use the appropriate nonabstract type. Use uint64 for | |
8437 | // an integer if we know it is nonnegative, otherwise | |
8438 | // use int64 for a integer, otherwise use float64 for a | |
8439 | // float or complex128 for a complex. | |
8440 | Type* want_type = NULL; | |
8441 | Type* atype = (*pa)->type(); | |
8442 | if (atype->is_abstract()) | |
8443 | { | |
8444 | if (atype->integer_type() != NULL) | |
8445 | { | |
0c77715b | 8446 | Numeric_constant nc; |
8447 | if (this->numeric_constant_value(&nc)) | |
8448 | { | |
8449 | mpz_t val; | |
8450 | if (nc.to_int(&val)) | |
8451 | { | |
8452 | if (mpz_sgn(val) >= 0) | |
8453 | want_type = Type::lookup_integer_type("uint64"); | |
8454 | mpz_clear(val); | |
8455 | } | |
8456 | } | |
8457 | if (want_type == NULL) | |
e440a328 | 8458 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8459 | } |
8460 | else if (atype->float_type() != NULL) | |
8461 | want_type = Type::lookup_float_type("float64"); | |
8462 | else if (atype->complex_type() != NULL) | |
8463 | want_type = Type::lookup_complex_type("complex128"); | |
8464 | else if (atype->is_abstract_string_type()) | |
8465 | want_type = Type::lookup_string_type(); | |
8466 | else if (atype->is_abstract_boolean_type()) | |
8467 | want_type = Type::lookup_bool_type(); | |
8468 | else | |
c3e6f413 | 8469 | go_unreachable(); |
e440a328 | 8470 | subcontext.type = want_type; |
8471 | } | |
8472 | } | |
8473 | ||
8474 | (*pa)->determine_type(&subcontext); | |
321e5ad2 | 8475 | |
8476 | if (trailing_arg_types != NULL) | |
8477 | { | |
8478 | arg_type = trailing_arg_types; | |
8479 | trailing_arg_types = NULL; | |
8480 | } | |
e440a328 | 8481 | } |
8482 | } | |
8483 | } | |
8484 | ||
8485 | // If there is exactly one argument, return true. Otherwise give an | |
8486 | // error message and return false. | |
8487 | ||
8488 | bool | |
8489 | Builtin_call_expression::check_one_arg() | |
8490 | { | |
8491 | const Expression_list* args = this->args(); | |
8492 | if (args == NULL || args->size() < 1) | |
8493 | { | |
8494 | this->report_error(_("not enough arguments")); | |
8495 | return false; | |
8496 | } | |
8497 | else if (args->size() > 1) | |
8498 | { | |
8499 | this->report_error(_("too many arguments")); | |
8500 | return false; | |
8501 | } | |
8502 | if (args->front()->is_error_expression() | |
5c13bd80 | 8503 | || args->front()->type()->is_error()) |
e440a328 | 8504 | { |
8505 | this->set_is_error(); | |
8506 | return false; | |
8507 | } | |
8508 | return true; | |
8509 | } | |
8510 | ||
8511 | // Check argument types for a builtin function. | |
8512 | ||
8513 | void | |
8514 | Builtin_call_expression::do_check_types(Gogo*) | |
8515 | { | |
375646ea | 8516 | if (this->is_error_expression()) |
8517 | return; | |
e440a328 | 8518 | switch (this->code_) |
8519 | { | |
8520 | case BUILTIN_INVALID: | |
8521 | case BUILTIN_NEW: | |
8522 | case BUILTIN_MAKE: | |
cd238b8d | 8523 | case BUILTIN_DELETE: |
e440a328 | 8524 | return; |
8525 | ||
8526 | case BUILTIN_LEN: | |
8527 | case BUILTIN_CAP: | |
8528 | { | |
8529 | // The single argument may be either a string or an array or a | |
8530 | // map or a channel, or a pointer to a closed array. | |
8531 | if (this->check_one_arg()) | |
8532 | { | |
8533 | Type* arg_type = this->one_arg()->type(); | |
8534 | if (arg_type->points_to() != NULL | |
8535 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8536 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8537 | arg_type = arg_type->points_to(); |
8538 | if (this->code_ == BUILTIN_CAP) | |
8539 | { | |
5c13bd80 | 8540 | if (!arg_type->is_error() |
e440a328 | 8541 | && arg_type->array_type() == NULL |
8542 | && arg_type->channel_type() == NULL) | |
8543 | this->report_error(_("argument must be array or slice " | |
8544 | "or channel")); | |
8545 | } | |
8546 | else | |
8547 | { | |
5c13bd80 | 8548 | if (!arg_type->is_error() |
e440a328 | 8549 | && !arg_type->is_string_type() |
8550 | && arg_type->array_type() == NULL | |
8551 | && arg_type->map_type() == NULL | |
8552 | && arg_type->channel_type() == NULL) | |
8553 | this->report_error(_("argument must be string or " | |
8554 | "array or slice or map or channel")); | |
8555 | } | |
8556 | } | |
8557 | } | |
8558 | break; | |
8559 | ||
8560 | case BUILTIN_PRINT: | |
8561 | case BUILTIN_PRINTLN: | |
8562 | { | |
8563 | const Expression_list* args = this->args(); | |
8564 | if (args == NULL) | |
8565 | { | |
8566 | if (this->code_ == BUILTIN_PRINT) | |
631d5788 | 8567 | go_warning_at(this->location(), 0, |
e440a328 | 8568 | "no arguments for builtin function %<%s%>", |
8569 | (this->code_ == BUILTIN_PRINT | |
8570 | ? "print" | |
8571 | : "println")); | |
8572 | } | |
8573 | else | |
8574 | { | |
8575 | for (Expression_list::const_iterator p = args->begin(); | |
8576 | p != args->end(); | |
8577 | ++p) | |
8578 | { | |
8579 | Type* type = (*p)->type(); | |
5c13bd80 | 8580 | if (type->is_error() |
e440a328 | 8581 | || type->is_string_type() |
8582 | || type->integer_type() != NULL | |
8583 | || type->float_type() != NULL | |
8584 | || type->complex_type() != NULL | |
8585 | || type->is_boolean_type() | |
8586 | || type->points_to() != NULL | |
8587 | || type->interface_type() != NULL | |
8588 | || type->channel_type() != NULL | |
8589 | || type->map_type() != NULL | |
8590 | || type->function_type() != NULL | |
411eb89e | 8591 | || type->is_slice_type()) |
e440a328 | 8592 | ; |
acf8e158 | 8593 | else if ((*p)->is_type_expression()) |
8594 | { | |
8595 | // If this is a type expression it's going to give | |
8596 | // an error anyhow, so we don't need one here. | |
8597 | } | |
e440a328 | 8598 | else |
8599 | this->report_error(_("unsupported argument type to " | |
8600 | "builtin function")); | |
8601 | } | |
8602 | } | |
8603 | } | |
8604 | break; | |
8605 | ||
8606 | case BUILTIN_CLOSE: | |
e440a328 | 8607 | if (this->check_one_arg()) |
8608 | { | |
8609 | if (this->one_arg()->type()->channel_type() == NULL) | |
8610 | this->report_error(_("argument must be channel")); | |
5202d986 | 8611 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8612 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8613 | } |
8614 | break; | |
8615 | ||
8616 | case BUILTIN_PANIC: | |
8617 | case BUILTIN_SIZEOF: | |
8618 | case BUILTIN_ALIGNOF: | |
8619 | this->check_one_arg(); | |
8620 | break; | |
8621 | ||
8622 | case BUILTIN_RECOVER: | |
6334270b | 8623 | if (this->args() != NULL |
8624 | && !this->args()->empty() | |
8625 | && !this->recover_arg_is_set_) | |
e440a328 | 8626 | this->report_error(_("too many arguments")); |
8627 | break; | |
8628 | ||
8629 | case BUILTIN_OFFSETOF: | |
8630 | if (this->check_one_arg()) | |
8631 | { | |
8632 | Expression* arg = this->one_arg(); | |
8633 | if (arg->field_reference_expression() == NULL) | |
8634 | this->report_error(_("argument must be a field reference")); | |
8635 | } | |
8636 | break; | |
8637 | ||
8638 | case BUILTIN_COPY: | |
8639 | { | |
8640 | const Expression_list* args = this->args(); | |
8641 | if (args == NULL || args->size() < 2) | |
8642 | { | |
8643 | this->report_error(_("not enough arguments")); | |
8644 | break; | |
8645 | } | |
8646 | else if (args->size() > 2) | |
8647 | { | |
8648 | this->report_error(_("too many arguments")); | |
8649 | break; | |
8650 | } | |
8651 | Type* arg1_type = args->front()->type(); | |
8652 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8653 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 8654 | { |
8655 | this->set_is_error(); | |
8656 | break; | |
8657 | } | |
e440a328 | 8658 | |
8659 | Type* e1; | |
411eb89e | 8660 | if (arg1_type->is_slice_type()) |
e440a328 | 8661 | e1 = arg1_type->array_type()->element_type(); |
8662 | else | |
8663 | { | |
8664 | this->report_error(_("left argument must be a slice")); | |
8665 | break; | |
8666 | } | |
8667 | ||
411eb89e | 8668 | if (arg2_type->is_slice_type()) |
60963afd | 8669 | { |
8670 | Type* e2 = arg2_type->array_type()->element_type(); | |
8671 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8672 | this->report_error(_("element types must be the same")); | |
8673 | } | |
e440a328 | 8674 | else if (arg2_type->is_string_type()) |
e440a328 | 8675 | { |
60963afd | 8676 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8677 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8678 | } |
60963afd | 8679 | else |
8680 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8681 | } |
8682 | break; | |
8683 | ||
8684 | case BUILTIN_APPEND: | |
8685 | { | |
8686 | const Expression_list* args = this->args(); | |
321e5ad2 | 8687 | if (args == NULL || args->empty()) |
e440a328 | 8688 | { |
8689 | this->report_error(_("not enough arguments")); | |
8690 | break; | |
8691 | } | |
321e5ad2 | 8692 | |
8693 | Type* slice_type = args->front()->type(); | |
8694 | if (!slice_type->is_slice_type()) | |
6bebb39d | 8695 | { |
321e5ad2 | 8696 | if (slice_type->is_error_type()) |
8697 | break; | |
8698 | if (slice_type->is_nil_type()) | |
8699 | go_error_at(args->front()->location(), "use of untyped nil"); | |
8700 | else | |
8701 | go_error_at(args->front()->location(), | |
8702 | "argument 1 must be a slice"); | |
6bebb39d | 8703 | this->set_is_error(); |
8704 | break; | |
8705 | } | |
cd238b8d | 8706 | |
321e5ad2 | 8707 | Type* element_type = slice_type->array_type()->element_type(); |
22deed0d | 8708 | if (!element_type->in_heap()) |
8709 | go_error_at(args->front()->location(), | |
8710 | "can't append to slice of go:notinheap type"); | |
321e5ad2 | 8711 | if (this->is_varargs()) |
4fd4fcf4 | 8712 | { |
321e5ad2 | 8713 | if (!args->back()->type()->is_slice_type() |
8714 | && !args->back()->type()->is_string_type()) | |
8715 | { | |
8716 | go_error_at(args->back()->location(), | |
8717 | "invalid use of %<...%> with non-slice/non-string"); | |
8718 | this->set_is_error(); | |
8719 | break; | |
8720 | } | |
4fd4fcf4 | 8721 | |
321e5ad2 | 8722 | if (args->size() < 2) |
8723 | { | |
8724 | this->report_error(_("not enough arguments")); | |
8725 | break; | |
8726 | } | |
8727 | if (args->size() > 2) | |
8728 | { | |
8729 | this->report_error(_("too many arguments")); | |
8730 | break; | |
8731 | } | |
8732 | ||
8733 | if (args->back()->type()->is_string_type() | |
8734 | && element_type->integer_type() != NULL | |
8735 | && element_type->integer_type()->is_byte()) | |
8736 | { | |
8737 | // Permit append(s1, s2...) when s1 is a slice of | |
8738 | // bytes and s2 is a string type. | |
8739 | } | |
e440a328 | 8740 | else |
8741 | { | |
321e5ad2 | 8742 | // We have to test for assignment compatibility to a |
8743 | // slice of the element type, which is not necessarily | |
8744 | // the same as the type of the first argument: the | |
8745 | // first argument might have a named type. | |
8746 | Type* check_type = Type::make_array_type(element_type, NULL); | |
8747 | std::string reason; | |
8748 | if (!Type::are_assignable(check_type, args->back()->type(), | |
8749 | &reason)) | |
8750 | { | |
8751 | if (reason.empty()) | |
8752 | go_error_at(args->back()->location(), | |
8753 | "argument 2 has invalid type"); | |
8754 | else | |
8755 | go_error_at(args->back()->location(), | |
8756 | "argument 2 has invalid type (%s)", | |
8757 | reason.c_str()); | |
8758 | this->set_is_error(); | |
8759 | break; | |
8760 | } | |
8761 | } | |
8762 | } | |
8763 | else | |
8764 | { | |
8765 | Expression_list::const_iterator pa = args->begin(); | |
8766 | int i = 2; | |
8767 | for (++pa; pa != args->end(); ++pa, ++i) | |
8768 | { | |
8769 | std::string reason; | |
8770 | if (!Type::are_assignable(element_type, (*pa)->type(), | |
8771 | &reason)) | |
8772 | { | |
8773 | if (reason.empty()) | |
8774 | go_error_at((*pa)->location(), | |
8775 | "argument %d has incompatible type", i); | |
8776 | else | |
8777 | go_error_at((*pa)->location(), | |
8778 | "argument %d has incompatible type (%s)", | |
8779 | i, reason.c_str()); | |
8780 | this->set_is_error(); | |
8781 | } | |
e440a328 | 8782 | } |
8783 | } | |
e440a328 | 8784 | } |
321e5ad2 | 8785 | break; |
e440a328 | 8786 | |
8787 | case BUILTIN_REAL: | |
8788 | case BUILTIN_IMAG: | |
8789 | if (this->check_one_arg()) | |
8790 | { | |
8791 | if (this->one_arg()->type()->complex_type() == NULL) | |
8792 | this->report_error(_("argument must have complex type")); | |
8793 | } | |
8794 | break; | |
8795 | ||
48080209 | 8796 | case BUILTIN_COMPLEX: |
e440a328 | 8797 | { |
8798 | const Expression_list* args = this->args(); | |
8799 | if (args == NULL || args->size() < 2) | |
8800 | this->report_error(_("not enough arguments")); | |
8801 | else if (args->size() > 2) | |
8802 | this->report_error(_("too many arguments")); | |
8803 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8804 | || args->front()->type()->is_error() |
e440a328 | 8805 | || args->back()->is_error_expression() |
5c13bd80 | 8806 | || args->back()->type()->is_error()) |
e440a328 | 8807 | this->set_is_error(); |
8808 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8809 | args->back()->type(), true, NULL)) |
48080209 | 8810 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8811 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8812 | this->report_error(_("complex arguments must have " |
e440a328 | 8813 | "floating-point type")); |
8814 | } | |
8815 | break; | |
8816 | ||
8817 | default: | |
c3e6f413 | 8818 | go_unreachable(); |
e440a328 | 8819 | } |
8820 | } | |
8821 | ||
72666aed | 8822 | Expression* |
8823 | Builtin_call_expression::do_copy() | |
8824 | { | |
8825 | Call_expression* bce = | |
8826 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8827 | (this->args() == NULL |
8828 | ? NULL | |
8829 | : this->args()->copy()), | |
72666aed | 8830 | this->is_varargs(), |
8831 | this->location()); | |
8832 | ||
8833 | if (this->varargs_are_lowered()) | |
8834 | bce->set_varargs_are_lowered(); | |
8835 | return bce; | |
8836 | } | |
8837 | ||
ea664253 | 8838 | // Return the backend representation for a builtin function. |
e440a328 | 8839 | |
ea664253 | 8840 | Bexpression* |
8841 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8842 | { |
8843 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8844 | Location location = this->location(); |
a0d8874e | 8845 | |
8846 | if (this->is_erroneous_call()) | |
8847 | { | |
8848 | go_assert(saw_errors()); | |
8849 | return gogo->backend()->error_expression(); | |
8850 | } | |
8851 | ||
e440a328 | 8852 | switch (this->code_) |
8853 | { | |
8854 | case BUILTIN_INVALID: | |
8855 | case BUILTIN_NEW: | |
8856 | case BUILTIN_MAKE: | |
c3e6f413 | 8857 | go_unreachable(); |
e440a328 | 8858 | |
8859 | case BUILTIN_LEN: | |
8860 | case BUILTIN_CAP: | |
8861 | { | |
8862 | const Expression_list* args = this->args(); | |
c484d925 | 8863 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8864 | Expression* arg = args->front(); |
e440a328 | 8865 | Type* arg_type = arg->type(); |
0f914071 | 8866 | |
8867 | if (this->seen_) | |
8868 | { | |
c484d925 | 8869 | go_assert(saw_errors()); |
ea664253 | 8870 | return context->backend()->error_expression(); |
0f914071 | 8871 | } |
8872 | this->seen_ = true; | |
0f914071 | 8873 | this->seen_ = false; |
e440a328 | 8874 | if (arg_type->points_to() != NULL) |
8875 | { | |
8876 | arg_type = arg_type->points_to(); | |
c484d925 | 8877 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8878 | && !arg_type->is_slice_type()); |
f614ea8b | 8879 | arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT, |
8880 | location); | |
e440a328 | 8881 | } |
8882 | ||
1b1f2abf | 8883 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8884 | Expression* val; |
e440a328 | 8885 | if (this->code_ == BUILTIN_LEN) |
8886 | { | |
8887 | if (arg_type->is_string_type()) | |
2c809f8f | 8888 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8889 | location); | |
e440a328 | 8890 | else if (arg_type->array_type() != NULL) |
0f914071 | 8891 | { |
8892 | if (this->seen_) | |
8893 | { | |
c484d925 | 8894 | go_assert(saw_errors()); |
ea664253 | 8895 | return context->backend()->error_expression(); |
0f914071 | 8896 | } |
8897 | this->seen_ = true; | |
2c809f8f | 8898 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8899 | this->seen_ = false; |
8900 | } | |
0d5530d9 | 8901 | else if (arg_type->map_type() != NULL |
8902 | || arg_type->channel_type() != NULL) | |
8903 | { | |
8904 | // The first field is the length. If the pointer is | |
8905 | // nil, the length is zero. | |
8906 | Type* pint_type = Type::make_pointer_type(int_type); | |
8907 | arg = Expression::make_unsafe_cast(pint_type, arg, location); | |
8908 | Expression* nil = Expression::make_nil(location); | |
8909 | nil = Expression::make_cast(pint_type, nil, location); | |
8910 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8911 | arg, nil, location); | |
8912 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8913 | location); | |
f614ea8b | 8914 | Expression* indir = |
8915 | Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, | |
8916 | location); | |
0d5530d9 | 8917 | val = Expression::make_conditional(cmp, zero, indir, location); |
8918 | } | |
e440a328 | 8919 | else |
c3e6f413 | 8920 | go_unreachable(); |
e440a328 | 8921 | } |
8922 | else | |
8923 | { | |
8924 | if (arg_type->array_type() != NULL) | |
0f914071 | 8925 | { |
8926 | if (this->seen_) | |
8927 | { | |
c484d925 | 8928 | go_assert(saw_errors()); |
ea664253 | 8929 | return context->backend()->error_expression(); |
0f914071 | 8930 | } |
8931 | this->seen_ = true; | |
2c809f8f | 8932 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8933 | this->seen_ = false; |
8934 | } | |
e440a328 | 8935 | else if (arg_type->channel_type() != NULL) |
132ed071 | 8936 | { |
8937 | // The second field is the capacity. If the pointer | |
8938 | // is nil, the capacity is zero. | |
8939 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
8940 | Type* pint_type = Type::make_pointer_type(int_type); | |
8941 | Expression* parg = Expression::make_unsafe_cast(uintptr_type, | |
8942 | arg, | |
8943 | location); | |
8944 | int off = int_type->integer_type()->bits() / 8; | |
8945 | Expression* eoff = Expression::make_integer_ul(off, | |
8946 | uintptr_type, | |
8947 | location); | |
8948 | parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, | |
8949 | location); | |
8950 | parg = Expression::make_unsafe_cast(pint_type, parg, location); | |
8951 | Expression* nil = Expression::make_nil(location); | |
8952 | nil = Expression::make_cast(pint_type, nil, location); | |
8953 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8954 | arg, nil, location); | |
8955 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8956 | location); | |
f614ea8b | 8957 | Expression* indir = |
8958 | Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED, | |
8959 | location); | |
132ed071 | 8960 | val = Expression::make_conditional(cmp, zero, indir, location); |
8961 | } | |
e440a328 | 8962 | else |
c3e6f413 | 8963 | go_unreachable(); |
e440a328 | 8964 | } |
8965 | ||
2c809f8f | 8966 | return Expression::make_cast(int_type, val, |
ea664253 | 8967 | location)->get_backend(context); |
e440a328 | 8968 | } |
8969 | ||
8970 | case BUILTIN_PRINT: | |
8971 | case BUILTIN_PRINTLN: | |
8972 | { | |
8973 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
88b03a70 | 8974 | |
8975 | Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, | |
8976 | location, 0); | |
e440a328 | 8977 | |
8978 | const Expression_list* call_args = this->args(); | |
8979 | if (call_args != NULL) | |
8980 | { | |
8981 | for (Expression_list::const_iterator p = call_args->begin(); | |
8982 | p != call_args->end(); | |
8983 | ++p) | |
8984 | { | |
8985 | if (is_ln && p != call_args->begin()) | |
8986 | { | |
2c809f8f | 8987 | Expression* print_space = |
88b03a70 | 8988 | Runtime::make_call(Runtime::PRINTSP, location, 0); |
e440a328 | 8989 | |
2c809f8f | 8990 | print_stmts = |
8991 | Expression::make_compound(print_stmts, print_space, | |
8992 | location); | |
8993 | } | |
e440a328 | 8994 | |
2c809f8f | 8995 | Expression* arg = *p; |
8996 | Type* type = arg->type(); | |
8997 | Runtime::Function code; | |
e440a328 | 8998 | if (type->is_string_type()) |
88b03a70 | 8999 | code = Runtime::PRINTSTRING; |
e440a328 | 9000 | else if (type->integer_type() != NULL |
9001 | && type->integer_type()->is_unsigned()) | |
9002 | { | |
e440a328 | 9003 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 9004 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9005 | code = Runtime::PRINTUINT; |
e440a328 | 9006 | } |
9007 | else if (type->integer_type() != NULL) | |
9008 | { | |
e440a328 | 9009 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 9010 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9011 | code = Runtime::PRINTINT; |
e440a328 | 9012 | } |
9013 | else if (type->float_type() != NULL) | |
9014 | { | |
2c809f8f | 9015 | Type* dtype = Type::lookup_float_type("float64"); |
9016 | arg = Expression::make_cast(dtype, arg, location); | |
88b03a70 | 9017 | code = Runtime::PRINTFLOAT; |
e440a328 | 9018 | } |
9019 | else if (type->complex_type() != NULL) | |
9020 | { | |
2c809f8f | 9021 | Type* ctype = Type::lookup_complex_type("complex128"); |
9022 | arg = Expression::make_cast(ctype, arg, location); | |
88b03a70 | 9023 | code = Runtime::PRINTCOMPLEX; |
e440a328 | 9024 | } |
9025 | else if (type->is_boolean_type()) | |
88b03a70 | 9026 | code = Runtime::PRINTBOOL; |
e440a328 | 9027 | else if (type->points_to() != NULL |
9028 | || type->channel_type() != NULL | |
9029 | || type->map_type() != NULL | |
9030 | || type->function_type() != NULL) | |
9031 | { | |
2c809f8f | 9032 | arg = Expression::make_cast(type, arg, location); |
88b03a70 | 9033 | code = Runtime::PRINTPOINTER; |
e440a328 | 9034 | } |
9035 | else if (type->interface_type() != NULL) | |
9036 | { | |
9037 | if (type->interface_type()->is_empty()) | |
88b03a70 | 9038 | code = Runtime::PRINTEFACE; |
e440a328 | 9039 | else |
88b03a70 | 9040 | code = Runtime::PRINTIFACE; |
e440a328 | 9041 | } |
411eb89e | 9042 | else if (type->is_slice_type()) |
88b03a70 | 9043 | code = Runtime::PRINTSLICE; |
e440a328 | 9044 | else |
cd238b8d | 9045 | { |
9046 | go_assert(saw_errors()); | |
ea664253 | 9047 | return context->backend()->error_expression(); |
cd238b8d | 9048 | } |
e440a328 | 9049 | |
2c809f8f | 9050 | Expression* call = Runtime::make_call(code, location, 1, arg); |
88b03a70 | 9051 | print_stmts = Expression::make_compound(print_stmts, call, |
9052 | location); | |
e440a328 | 9053 | } |
9054 | } | |
9055 | ||
9056 | if (is_ln) | |
9057 | { | |
2c809f8f | 9058 | Expression* print_nl = |
88b03a70 | 9059 | Runtime::make_call(Runtime::PRINTNL, location, 0); |
9060 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
9061 | location); | |
e440a328 | 9062 | } |
9063 | ||
88b03a70 | 9064 | Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, |
9065 | location, 0); | |
9066 | print_stmts = Expression::make_compound(print_stmts, unlock, location); | |
32e3ff69 | 9067 | |
ea664253 | 9068 | return print_stmts->get_backend(context); |
e440a328 | 9069 | } |
9070 | ||
9071 | case BUILTIN_PANIC: | |
9072 | { | |
9073 | const Expression_list* args = this->args(); | |
c484d925 | 9074 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9075 | Expression* arg = args->front(); |
b13c66cd | 9076 | Type *empty = |
823c7e3d | 9077 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 9078 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
9079 | ||
9080 | Expression* panic = | |
03ac9de4 | 9081 | Runtime::make_call(Runtime::GOPANIC, location, 1, arg); |
ea664253 | 9082 | return panic->get_backend(context); |
e440a328 | 9083 | } |
9084 | ||
9085 | case BUILTIN_RECOVER: | |
9086 | { | |
9087 | // The argument is set when building recover thunks. It's a | |
9088 | // boolean value which is true if we can recover a value now. | |
9089 | const Expression_list* args = this->args(); | |
c484d925 | 9090 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9091 | Expression* arg = args->front(); |
b13c66cd | 9092 | Type *empty = |
823c7e3d | 9093 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 9094 | |
e440a328 | 9095 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 9096 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 9097 | |
9098 | // We need to handle a deferred call to recover specially, | |
9099 | // because it changes whether it can recover a panic or not. | |
9100 | // See test7 in test/recover1.go. | |
2c809f8f | 9101 | Expression* recover = Runtime::make_call((this->is_deferred() |
03ac9de4 | 9102 | ? Runtime::DEFERREDRECOVER |
9103 | : Runtime::GORECOVER), | |
2c809f8f | 9104 | location, 0); |
9105 | Expression* cond = | |
9106 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 9107 | return cond->get_backend(context); |
e440a328 | 9108 | } |
9109 | ||
9110 | case BUILTIN_CLOSE: | |
e440a328 | 9111 | { |
9112 | const Expression_list* args = this->args(); | |
c484d925 | 9113 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9114 | Expression* arg = args->front(); |
2c809f8f | 9115 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
9116 | 1, arg); | |
ea664253 | 9117 | return close->get_backend(context); |
e440a328 | 9118 | } |
9119 | ||
9120 | case BUILTIN_SIZEOF: | |
9121 | case BUILTIN_OFFSETOF: | |
9122 | case BUILTIN_ALIGNOF: | |
9123 | { | |
0c77715b | 9124 | Numeric_constant nc; |
9125 | unsigned long val; | |
9126 | if (!this->numeric_constant_value(&nc) | |
9127 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 9128 | { |
c484d925 | 9129 | go_assert(saw_errors()); |
ea664253 | 9130 | return context->backend()->error_expression(); |
7f1d9abd | 9131 | } |
7ba86326 | 9132 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 9133 | mpz_t ival; |
9134 | nc.get_int(&ival); | |
9135 | Expression* int_cst = | |
e67508fa | 9136 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 9137 | mpz_clear(ival); |
ea664253 | 9138 | return int_cst->get_backend(context); |
e440a328 | 9139 | } |
9140 | ||
9141 | case BUILTIN_COPY: | |
9142 | { | |
9143 | const Expression_list* args = this->args(); | |
c484d925 | 9144 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 9145 | Expression* arg1 = args->front(); |
9146 | Expression* arg2 = args->back(); | |
9147 | ||
e440a328 | 9148 | Type* arg1_type = arg1->type(); |
9149 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 9150 | go_assert(arg1->is_variable()); |
321e5ad2 | 9151 | |
9152 | Expression* call; | |
e440a328 | 9153 | |
9154 | Type* arg2_type = arg2->type(); | |
2c809f8f | 9155 | go_assert(arg2->is_variable()); |
321e5ad2 | 9156 | if (arg2_type->is_string_type()) |
9157 | call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location, | |
9158 | 2, arg1, arg2); | |
e440a328 | 9159 | else |
9160 | { | |
321e5ad2 | 9161 | Type* et = at->element_type(); |
9162 | if (et->has_pointer()) | |
9163 | { | |
9164 | Expression* td = Expression::make_type_descriptor(et, | |
9165 | location); | |
9166 | call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location, | |
9167 | 3, td, arg1, arg2); | |
9168 | } | |
9169 | else | |
9170 | { | |
9171 | Expression* sz = Expression::make_type_info(et, | |
9172 | TYPE_INFO_SIZE); | |
9173 | call = Runtime::make_call(Runtime::SLICECOPY, location, 3, | |
9174 | arg1, arg2, sz); | |
9175 | } | |
e440a328 | 9176 | } |
2c809f8f | 9177 | |
321e5ad2 | 9178 | return call->get_backend(context); |
e440a328 | 9179 | } |
9180 | ||
9181 | case BUILTIN_APPEND: | |
321e5ad2 | 9182 | // Handled in Builtin_call_expression::flatten_append. |
9183 | go_unreachable(); | |
e440a328 | 9184 | |
9185 | case BUILTIN_REAL: | |
9186 | case BUILTIN_IMAG: | |
9187 | { | |
9188 | const Expression_list* args = this->args(); | |
c484d925 | 9189 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 9190 | |
9191 | Bexpression* ret; | |
ea664253 | 9192 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 9193 | if (this->code_ == BUILTIN_REAL) |
9194 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
9195 | else | |
9196 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 9197 | return ret; |
e440a328 | 9198 | } |
9199 | ||
48080209 | 9200 | case BUILTIN_COMPLEX: |
e440a328 | 9201 | { |
9202 | const Expression_list* args = this->args(); | |
c484d925 | 9203 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 9204 | Bexpression* breal = args->front()->get_backend(context); |
9205 | Bexpression* bimag = args->back()->get_backend(context); | |
9206 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 9207 | } |
9208 | ||
9209 | default: | |
c3e6f413 | 9210 | go_unreachable(); |
e440a328 | 9211 | } |
9212 | } | |
9213 | ||
9214 | // We have to support exporting a builtin call expression, because | |
9215 | // code can set a constant to the result of a builtin expression. | |
9216 | ||
9217 | void | |
9218 | Builtin_call_expression::do_export(Export* exp) const | |
9219 | { | |
0c77715b | 9220 | Numeric_constant nc; |
9221 | if (!this->numeric_constant_value(&nc)) | |
9222 | { | |
631d5788 | 9223 | go_error_at(this->location(), "value is not constant"); |
0c77715b | 9224 | return; |
9225 | } | |
e440a328 | 9226 | |
0c77715b | 9227 | if (nc.is_int()) |
e440a328 | 9228 | { |
0c77715b | 9229 | mpz_t val; |
9230 | nc.get_int(&val); | |
e440a328 | 9231 | Integer_expression::export_integer(exp, val); |
0c77715b | 9232 | mpz_clear(val); |
e440a328 | 9233 | } |
0c77715b | 9234 | else if (nc.is_float()) |
e440a328 | 9235 | { |
9236 | mpfr_t fval; | |
0c77715b | 9237 | nc.get_float(&fval); |
9238 | Float_expression::export_float(exp, fval); | |
e440a328 | 9239 | mpfr_clear(fval); |
9240 | } | |
0c77715b | 9241 | else if (nc.is_complex()) |
e440a328 | 9242 | { |
fcbea5e4 | 9243 | mpc_t cval; |
9244 | nc.get_complex(&cval); | |
9245 | Complex_expression::export_complex(exp, cval); | |
9246 | mpc_clear(cval); | |
e440a328 | 9247 | } |
0c77715b | 9248 | else |
9249 | go_unreachable(); | |
e440a328 | 9250 | |
9251 | // A trailing space lets us reliably identify the end of the number. | |
9252 | exp->write_c_string(" "); | |
9253 | } | |
9254 | ||
9255 | // Class Call_expression. | |
9256 | ||
8381eda7 | 9257 | // A Go function can be viewed in a couple of different ways. The |
9258 | // code of a Go function becomes a backend function with parameters | |
9259 | // whose types are simply the backend representation of the Go types. | |
9260 | // If there are multiple results, they are returned as a backend | |
9261 | // struct. | |
9262 | ||
9263 | // However, when Go code refers to a function other than simply | |
9264 | // calling it, the backend type of that function is actually a struct. | |
9265 | // The first field of the struct points to the Go function code | |
9266 | // (sometimes a wrapper as described below). The remaining fields | |
9267 | // hold addresses of closed-over variables. This struct is called a | |
9268 | // closure. | |
9269 | ||
9270 | // There are a few cases to consider. | |
9271 | ||
9272 | // A direct function call of a known function in package scope. In | |
9273 | // this case there are no closed-over variables, and we know the name | |
9274 | // of the function code. We can simply produce a backend call to the | |
9275 | // function directly, and not worry about the closure. | |
9276 | ||
9277 | // A direct function call of a known function literal. In this case | |
9278 | // we know the function code and we know the closure. We generate the | |
9279 | // function code such that it expects an additional final argument of | |
9280 | // the closure type. We pass the closure as the last argument, after | |
9281 | // the other arguments. | |
9282 | ||
9283 | // An indirect function call. In this case we have a closure. We | |
9284 | // load the pointer to the function code from the first field of the | |
9285 | // closure. We pass the address of the closure as the last argument. | |
9286 | ||
9287 | // A call to a method of an interface. Type methods are always at | |
9288 | // package scope, so we call the function directly, and don't worry | |
9289 | // about the closure. | |
9290 | ||
9291 | // This means that for a function at package scope we have two cases. | |
9292 | // One is the direct call, which has no closure. The other is the | |
9293 | // indirect call, which does have a closure. We can't simply ignore | |
9294 | // the closure, even though it is the last argument, because that will | |
9295 | // fail on targets where the function pops its arguments. So when | |
9296 | // generating a closure for a package-scope function we set the | |
9297 | // function code pointer in the closure to point to a wrapper | |
9298 | // function. This wrapper function accepts a final argument that | |
9299 | // points to the closure, ignores it, and calls the real function as a | |
9300 | // direct function call. This wrapper will normally be efficient, and | |
9301 | // can often simply be a tail call to the real function. | |
9302 | ||
9303 | // We don't use GCC's static chain pointer because 1) we don't need | |
9304 | // it; 2) GCC only permits using a static chain to call a known | |
9305 | // function, so we can't use it for an indirect call anyhow. Since we | |
9306 | // can't use it for an indirect call, we may as well not worry about | |
9307 | // using it for a direct call either. | |
9308 | ||
9309 | // We pass the closure last rather than first because it means that | |
9310 | // the function wrapper we put into a closure for a package-scope | |
9311 | // function can normally just be a tail call to the real function. | |
9312 | ||
9313 | // For method expressions we generate a wrapper that loads the | |
9314 | // receiver from the closure and then calls the method. This | |
9315 | // unfortunately forces reshuffling the arguments, since there is a | |
9316 | // new first argument, but we can't avoid reshuffling either for | |
9317 | // method expressions or for indirect calls of package-scope | |
9318 | // functions, and since the latter are more common we reshuffle for | |
9319 | // method expressions. | |
9320 | ||
9321 | // Note that the Go code retains the Go types. The extra final | |
9322 | // argument only appears when we convert to the backend | |
9323 | // representation. | |
9324 | ||
e440a328 | 9325 | // Traversal. |
9326 | ||
9327 | int | |
9328 | Call_expression::do_traverse(Traverse* traverse) | |
9329 | { | |
0c0dacab | 9330 | // If we are calling a function in a different package that returns |
9331 | // an unnamed type, this may be the only chance we get to traverse | |
9332 | // that type. We don't traverse this->type_ because it may be a | |
9333 | // Call_multiple_result_type that will just lead back here. | |
9334 | if (this->type_ != NULL && !this->type_->is_error_type()) | |
9335 | { | |
9336 | Function_type *fntype = this->get_function_type(); | |
9337 | if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) | |
9338 | return TRAVERSE_EXIT; | |
9339 | } | |
e440a328 | 9340 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) |
9341 | return TRAVERSE_EXIT; | |
9342 | if (this->args_ != NULL) | |
9343 | { | |
9344 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
9345 | return TRAVERSE_EXIT; | |
9346 | } | |
9347 | return TRAVERSE_CONTINUE; | |
9348 | } | |
9349 | ||
9350 | // Lower a call statement. | |
9351 | ||
9352 | Expression* | |
ceeb4318 | 9353 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
9354 | Statement_inserter* inserter, int) | |
e440a328 | 9355 | { |
b13c66cd | 9356 | Location loc = this->location(); |
09ea332d | 9357 | |
ceeb4318 | 9358 | // A type cast can look like a function call. |
e440a328 | 9359 | if (this->fn_->is_type_expression() |
9360 | && this->args_ != NULL | |
9361 | && this->args_->size() == 1) | |
9362 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 9363 | loc); |
e440a328 | 9364 | |
88f06749 | 9365 | // Because do_type will return an error type and thus prevent future |
9366 | // errors, check for that case now to ensure that the error gets | |
9367 | // reported. | |
37448b10 | 9368 | Function_type* fntype = this->get_function_type(); |
9369 | if (fntype == NULL) | |
88f06749 | 9370 | { |
9371 | if (!this->fn_->type()->is_error()) | |
9372 | this->report_error(_("expected function")); | |
5f1045b5 | 9373 | this->set_is_error(); |
9374 | return this; | |
88f06749 | 9375 | } |
9376 | ||
e440a328 | 9377 | // Handle an argument which is a call to a function which returns |
9378 | // multiple results. | |
9379 | if (this->args_ != NULL | |
9380 | && this->args_->size() == 1 | |
37448b10 | 9381 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9382 | { |
e440a328 | 9383 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9384 | if (rc > 1 | |
37448b10 | 9385 | && ((fntype->parameters() != NULL |
9386 | && (fntype->parameters()->size() == rc | |
9387 | || (fntype->is_varargs() | |
9388 | && fntype->parameters()->size() - 1 <= rc))) | |
9389 | || fntype->is_builtin())) | |
e440a328 | 9390 | { |
9391 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 9392 | call->set_is_multi_value_arg(); |
c33af8e4 | 9393 | if (this->is_varargs_) |
9394 | { | |
9395 | // It is not clear which result of a multiple result call | |
9396 | // the ellipsis operator should be applied to. If we unpack the | |
9397 | // the call into its individual results here, the ellipsis will be | |
9398 | // applied to the last result. | |
631d5788 | 9399 | go_error_at(call->location(), |
9400 | _("multiple-value argument in single-value context")); | |
c33af8e4 | 9401 | return Expression::make_error(call->location()); |
9402 | } | |
9403 | ||
e440a328 | 9404 | Expression_list* args = new Expression_list; |
9405 | for (size_t i = 0; i < rc; ++i) | |
9406 | args->push_back(Expression::make_call_result(call, i)); | |
9407 | // We can't return a new call expression here, because this | |
42535814 | 9408 | // one may be referenced by Call_result expressions. We |
9409 | // also can't delete the old arguments, because we may still | |
9410 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9411 | this->args_ = args; |
9412 | } | |
9413 | } | |
9414 | ||
37448b10 | 9415 | // Recognize a call to a builtin function. |
9416 | if (fntype->is_builtin()) | |
9417 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9418 | this->is_varargs_, loc); | |
9419 | ||
ceeb4318 | 9420 | // If this call returns multiple results, create a temporary |
5731103c | 9421 | // variable to hold them. |
9422 | if (this->result_count() > 1 && this->call_temp_ == NULL) | |
ceeb4318 | 9423 | { |
5731103c | 9424 | Struct_field_list* sfl = new Struct_field_list(); |
9425 | Function_type* fntype = this->get_function_type(); | |
37448b10 | 9426 | const Typed_identifier_list* results = fntype->results(); |
5731103c | 9427 | Location loc = this->location(); |
9428 | ||
9429 | int i = 0; | |
9430 | char buf[20]; | |
ceeb4318 | 9431 | for (Typed_identifier_list::const_iterator p = results->begin(); |
5731103c | 9432 | p != results->end(); |
9433 | ++p, ++i) | |
9434 | { | |
9435 | snprintf(buf, sizeof buf, "res%d", i); | |
9436 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9437 | } | |
9438 | ||
9439 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9440 | st->set_is_struct_incomparable(); | |
9441 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9442 | inserter->insert(this->call_temp_); | |
ceeb4318 | 9443 | } |
9444 | ||
e440a328 | 9445 | // Handle a call to a varargs function by packaging up the extra |
9446 | // parameters. | |
37448b10 | 9447 | if (fntype->is_varargs()) |
e440a328 | 9448 | { |
e440a328 | 9449 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9450 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9451 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9452 | this->lower_varargs(gogo, function, inserter, varargs_type, |
0e9a2e72 | 9453 | parameters->size(), SLICE_STORAGE_MAY_ESCAPE); |
09ea332d | 9454 | } |
9455 | ||
9456 | // If this is call to a method, call the method directly passing the | |
9457 | // object as the first parameter. | |
9458 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9459 | if (bme != NULL) | |
9460 | { | |
0afbb937 | 9461 | Named_object* methodfn = bme->function(); |
09ea332d | 9462 | Expression* first_arg = bme->first_argument(); |
9463 | ||
9464 | // We always pass a pointer when calling a method. | |
9465 | if (first_arg->type()->points_to() == NULL | |
9466 | && !first_arg->type()->is_error()) | |
9467 | { | |
9468 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9469 | // We may need to create a temporary variable so that we can | |
9470 | // take the address. We can't do that here because it will | |
9471 | // mess up the order of evaluation. | |
9472 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9473 | ue->set_create_temp(); | |
9474 | } | |
9475 | ||
9476 | // If we are calling a method which was inherited from an | |
9477 | // embedded struct, and the method did not get a stub, then the | |
9478 | // first type may be wrong. | |
9479 | Type* fatype = bme->first_argument_type(); | |
9480 | if (fatype != NULL) | |
9481 | { | |
9482 | if (fatype->points_to() == NULL) | |
9483 | fatype = Type::make_pointer_type(fatype); | |
9484 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9485 | } | |
9486 | ||
9487 | Expression_list* new_args = new Expression_list(); | |
9488 | new_args->push_back(first_arg); | |
9489 | if (this->args_ != NULL) | |
9490 | { | |
9491 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9492 | p != this->args_->end(); | |
9493 | ++p) | |
9494 | new_args->push_back(*p); | |
9495 | } | |
9496 | ||
9497 | // We have to change in place because this structure may be | |
9498 | // referenced by Call_result_expressions. We can't delete the | |
9499 | // old arguments, because we may be traversing them up in some | |
9500 | // caller. FIXME. | |
9501 | this->args_ = new_args; | |
0afbb937 | 9502 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9503 | bme->location()); |
e440a328 | 9504 | } |
9505 | ||
105f9a24 | 9506 | // Handle a couple of special runtime functions. In the runtime |
9507 | // package, getcallerpc returns the PC of the caller, and | |
9508 | // getcallersp returns the frame pointer of the caller. Implement | |
9509 | // these by turning them into calls to GCC builtin functions. We | |
9510 | // could implement them in normal code, but then we would have to | |
9511 | // explicitly unwind the stack. These functions are intended to be | |
9512 | // efficient. Note that this technique obviously only works for | |
33d1d391 | 9513 | // direct calls, but that is the only way they are used. |
9514 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
105f9a24 | 9515 | { |
9516 | Func_expression* fe = this->fn_->func_expression(); | |
9517 | if (fe != NULL | |
9518 | && fe->named_object()->is_function_declaration() | |
9519 | && fe->named_object()->package() == NULL) | |
9520 | { | |
9521 | std::string n = Gogo::unpack_hidden_name(fe->named_object()->name()); | |
33d1d391 | 9522 | if ((this->args_ == NULL || this->args_->size() == 0) |
9523 | && n == "getcallerpc") | |
105f9a24 | 9524 | { |
9525 | static Named_object* builtin_return_address; | |
9526 | return this->lower_to_builtin(&builtin_return_address, | |
9527 | "__builtin_return_address", | |
9528 | 0); | |
9529 | } | |
33d1d391 | 9530 | else if (this->args_ != NULL |
9531 | && this->args_->size() == 1 | |
9532 | && n == "getcallersp") | |
105f9a24 | 9533 | { |
33d1d391 | 9534 | // The actual argument to getcallersp is always the |
9535 | // address of a parameter; we don't need that for the | |
9536 | // GCC builtin function, so we just ignore it. | |
105f9a24 | 9537 | static Named_object* builtin_frame_address; |
9538 | return this->lower_to_builtin(&builtin_frame_address, | |
9539 | "__builtin_frame_address", | |
9540 | 1); | |
9541 | } | |
9542 | } | |
9543 | } | |
9544 | ||
e440a328 | 9545 | return this; |
9546 | } | |
9547 | ||
9548 | // Lower a call to a varargs function. FUNCTION is the function in | |
9549 | // which the call occurs--it's not the function we are calling. | |
9550 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9551 | // PARAM_COUNT is the number of parameters of the function we are | |
9552 | // calling; the last of these parameters will be the varargs | |
9553 | // parameter. | |
9554 | ||
09ea332d | 9555 | void |
e440a328 | 9556 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9557 | Statement_inserter* inserter, |
0e9a2e72 | 9558 | Type* varargs_type, size_t param_count, |
9559 | Slice_storage_escape_disp escape_disp) | |
e440a328 | 9560 | { |
9561 | if (this->varargs_are_lowered_) | |
09ea332d | 9562 | return; |
e440a328 | 9563 | |
b13c66cd | 9564 | Location loc = this->location(); |
e440a328 | 9565 | |
c484d925 | 9566 | go_assert(param_count > 0); |
411eb89e | 9567 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9568 | |
9569 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9570 | if (arg_count < param_count - 1) | |
9571 | { | |
9572 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9573 | return; |
e440a328 | 9574 | } |
9575 | ||
9576 | Expression_list* old_args = this->args_; | |
9577 | Expression_list* new_args = new Expression_list(); | |
9578 | bool push_empty_arg = false; | |
9579 | if (old_args == NULL || old_args->empty()) | |
9580 | { | |
c484d925 | 9581 | go_assert(param_count == 1); |
e440a328 | 9582 | push_empty_arg = true; |
9583 | } | |
9584 | else | |
9585 | { | |
9586 | Expression_list::const_iterator pa; | |
9587 | int i = 1; | |
9588 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9589 | { | |
9590 | if (static_cast<size_t>(i) == param_count) | |
9591 | break; | |
9592 | new_args->push_back(*pa); | |
9593 | } | |
9594 | ||
9595 | // We have reached the varargs parameter. | |
9596 | ||
9597 | bool issued_error = false; | |
9598 | if (pa == old_args->end()) | |
9599 | push_empty_arg = true; | |
9600 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9601 | new_args->push_back(*pa); | |
9602 | else if (this->is_varargs_) | |
9603 | { | |
a6645f74 | 9604 | if ((*pa)->type()->is_slice_type()) |
9605 | this->report_error(_("too many arguments")); | |
9606 | else | |
9607 | { | |
631d5788 | 9608 | go_error_at(this->location(), |
9609 | _("invalid use of %<...%> with non-slice")); | |
a6645f74 | 9610 | this->set_is_error(); |
9611 | } | |
09ea332d | 9612 | return; |
e440a328 | 9613 | } |
e440a328 | 9614 | else |
9615 | { | |
9616 | Type* element_type = varargs_type->array_type()->element_type(); | |
9617 | Expression_list* vals = new Expression_list; | |
9618 | for (; pa != old_args->end(); ++pa, ++i) | |
9619 | { | |
9620 | // Check types here so that we get a better message. | |
9621 | Type* patype = (*pa)->type(); | |
b13c66cd | 9622 | Location paloc = (*pa)->location(); |
e440a328 | 9623 | if (!this->check_argument_type(i, element_type, patype, |
9624 | paloc, issued_error)) | |
9625 | continue; | |
9626 | vals->push_back(*pa); | |
9627 | } | |
0e9a2e72 | 9628 | Slice_construction_expression* sce = |
e440a328 | 9629 | Expression::make_slice_composite_literal(varargs_type, vals, loc); |
0e9a2e72 | 9630 | if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) |
9631 | sce->set_storage_does_not_escape(); | |
9632 | Expression* val = sce; | |
09ea332d | 9633 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9634 | new_args->push_back(val); |
9635 | } | |
9636 | } | |
9637 | ||
9638 | if (push_empty_arg) | |
9639 | new_args->push_back(Expression::make_nil(loc)); | |
9640 | ||
9641 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9642 | // be referenced by Call_result expressions. FIXME. We can't |
9643 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9644 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9645 | this->args_ = new_args; |
9646 | this->varargs_are_lowered_ = true; | |
e440a328 | 9647 | } |
9648 | ||
105f9a24 | 9649 | // Return a call to __builtin_return_address or __builtin_frame_address. |
9650 | ||
9651 | Expression* | |
9652 | Call_expression::lower_to_builtin(Named_object** pno, const char* name, | |
9653 | int arg) | |
9654 | { | |
9655 | if (*pno == NULL) | |
9656 | *pno = Gogo::declare_builtin_rf_address(name); | |
9657 | ||
9658 | Location loc = this->location(); | |
9659 | ||
9660 | Expression* fn = Expression::make_func_reference(*pno, NULL, loc); | |
9661 | Expression* a = Expression::make_integer_ul(arg, NULL, loc); | |
9662 | Expression_list *args = new Expression_list(); | |
9663 | args->push_back(a); | |
9664 | Expression* call = Expression::make_call(fn, args, false, loc); | |
9665 | ||
9666 | // The builtin functions return void*, but the Go functions return uintptr. | |
9667 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9668 | return Expression::make_cast(uintptr_type, call, loc); | |
9669 | } | |
9670 | ||
2c809f8f | 9671 | // Flatten a call with multiple results into a temporary. |
9672 | ||
9673 | Expression* | |
b8e86a51 | 9674 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
9675 | Statement_inserter* inserter) | |
2c809f8f | 9676 | { |
5bf8be8b | 9677 | if (this->is_erroneous_call()) |
9678 | { | |
9679 | go_assert(saw_errors()); | |
9680 | return Expression::make_error(this->location()); | |
9681 | } | |
b8e86a51 | 9682 | |
91c0fd76 | 9683 | if (this->is_flattened_) |
9684 | return this; | |
9685 | this->is_flattened_ = true; | |
9686 | ||
b8e86a51 | 9687 | // Add temporary variables for all arguments that require type |
9688 | // conversion. | |
9689 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 9690 | if (fntype == NULL) |
9691 | { | |
9692 | go_assert(saw_errors()); | |
9693 | return this; | |
9694 | } | |
b8e86a51 | 9695 | if (this->args_ != NULL && !this->args_->empty() |
9696 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
9697 | { | |
9698 | bool is_interface_method = | |
9699 | this->fn_->interface_field_reference_expression() != NULL; | |
9700 | ||
9701 | Expression_list *args = new Expression_list(); | |
9702 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
9703 | Expression_list::const_iterator pa = this->args_->begin(); | |
9704 | if (!is_interface_method && fntype->is_method()) | |
9705 | { | |
9706 | // The receiver argument. | |
9707 | args->push_back(*pa); | |
9708 | ++pa; | |
9709 | } | |
9710 | for (; pa != this->args_->end(); ++pa, ++pp) | |
9711 | { | |
9712 | go_assert(pp != fntype->parameters()->end()); | |
9713 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
9714 | args->push_back(*pa); | |
9715 | else | |
9716 | { | |
9717 | Location loc = (*pa)->location(); | |
8ba8cc87 | 9718 | Expression* arg = *pa; |
9719 | if (!arg->is_variable()) | |
9720 | { | |
9721 | Temporary_statement *temp = | |
9722 | Statement::make_temporary(NULL, arg, loc); | |
9723 | inserter->insert(temp); | |
9724 | arg = Expression::make_temporary_reference(temp, loc); | |
9725 | } | |
9726 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
9727 | loc); | |
9728 | args->push_back(arg); | |
b8e86a51 | 9729 | } |
9730 | } | |
9731 | delete this->args_; | |
9732 | this->args_ = args; | |
9733 | } | |
9734 | ||
2c809f8f | 9735 | return this; |
9736 | } | |
9737 | ||
ceeb4318 | 9738 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9739 | |
9740 | Function_type* | |
9741 | Call_expression::get_function_type() const | |
9742 | { | |
9743 | return this->fn_->type()->function_type(); | |
9744 | } | |
9745 | ||
9746 | // Return the number of values which this call will return. | |
9747 | ||
9748 | size_t | |
9749 | Call_expression::result_count() const | |
9750 | { | |
9751 | const Function_type* fntype = this->get_function_type(); | |
9752 | if (fntype == NULL) | |
9753 | return 0; | |
9754 | if (fntype->results() == NULL) | |
9755 | return 0; | |
9756 | return fntype->results()->size(); | |
9757 | } | |
9758 | ||
5731103c | 9759 | // Return the temporary that holds the result for a call with multiple |
9760 | // results. | |
ceeb4318 | 9761 | |
9762 | Temporary_statement* | |
5731103c | 9763 | Call_expression::results() const |
ceeb4318 | 9764 | { |
5731103c | 9765 | if (this->call_temp_ == NULL) |
cd238b8d | 9766 | { |
9767 | go_assert(saw_errors()); | |
9768 | return NULL; | |
9769 | } | |
5731103c | 9770 | return this->call_temp_; |
ceeb4318 | 9771 | } |
9772 | ||
1373401e | 9773 | // Set the number of results expected from a call expression. |
9774 | ||
9775 | void | |
9776 | Call_expression::set_expected_result_count(size_t count) | |
9777 | { | |
9778 | go_assert(this->expected_result_count_ == 0); | |
9779 | this->expected_result_count_ = count; | |
9780 | } | |
9781 | ||
e440a328 | 9782 | // Return whether this is a call to the predeclared function recover. |
9783 | ||
9784 | bool | |
9785 | Call_expression::is_recover_call() const | |
9786 | { | |
9787 | return this->do_is_recover_call(); | |
9788 | } | |
9789 | ||
9790 | // Set the argument to the recover function. | |
9791 | ||
9792 | void | |
9793 | Call_expression::set_recover_arg(Expression* arg) | |
9794 | { | |
9795 | this->do_set_recover_arg(arg); | |
9796 | } | |
9797 | ||
9798 | // Virtual functions also implemented by Builtin_call_expression. | |
9799 | ||
9800 | bool | |
9801 | Call_expression::do_is_recover_call() const | |
9802 | { | |
9803 | return false; | |
9804 | } | |
9805 | ||
9806 | void | |
9807 | Call_expression::do_set_recover_arg(Expression*) | |
9808 | { | |
c3e6f413 | 9809 | go_unreachable(); |
e440a328 | 9810 | } |
9811 | ||
ceeb4318 | 9812 | // We have found an error with this call expression; return true if |
9813 | // we should report it. | |
9814 | ||
9815 | bool | |
9816 | Call_expression::issue_error() | |
9817 | { | |
9818 | if (this->issued_error_) | |
9819 | return false; | |
9820 | else | |
9821 | { | |
9822 | this->issued_error_ = true; | |
9823 | return true; | |
9824 | } | |
9825 | } | |
9826 | ||
5bf8be8b | 9827 | // Whether or not this call contains errors, either in the call or the |
9828 | // arguments to the call. | |
9829 | ||
9830 | bool | |
9831 | Call_expression::is_erroneous_call() | |
9832 | { | |
9833 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9834 | return true; | |
9835 | ||
9836 | if (this->args() == NULL) | |
9837 | return false; | |
9838 | for (Expression_list::iterator pa = this->args()->begin(); | |
9839 | pa != this->args()->end(); | |
9840 | ++pa) | |
9841 | { | |
9842 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9843 | return true; | |
9844 | } | |
9845 | return false; | |
9846 | } | |
9847 | ||
e440a328 | 9848 | // Get the type. |
9849 | ||
9850 | Type* | |
9851 | Call_expression::do_type() | |
9852 | { | |
9853 | if (this->type_ != NULL) | |
9854 | return this->type_; | |
9855 | ||
9856 | Type* ret; | |
9857 | Function_type* fntype = this->get_function_type(); | |
9858 | if (fntype == NULL) | |
9859 | return Type::make_error_type(); | |
9860 | ||
9861 | const Typed_identifier_list* results = fntype->results(); | |
9862 | if (results == NULL) | |
9863 | ret = Type::make_void_type(); | |
9864 | else if (results->size() == 1) | |
9865 | ret = results->begin()->type(); | |
9866 | else | |
9867 | ret = Type::make_call_multiple_result_type(this); | |
9868 | ||
9869 | this->type_ = ret; | |
9870 | ||
9871 | return this->type_; | |
9872 | } | |
9873 | ||
9874 | // Determine types for a call expression. We can use the function | |
9875 | // parameter types to set the types of the arguments. | |
9876 | ||
9877 | void | |
9878 | Call_expression::do_determine_type(const Type_context*) | |
9879 | { | |
fb94b0ca | 9880 | if (!this->determining_types()) |
9881 | return; | |
9882 | ||
e440a328 | 9883 | this->fn_->determine_type_no_context(); |
9884 | Function_type* fntype = this->get_function_type(); | |
9885 | const Typed_identifier_list* parameters = NULL; | |
9886 | if (fntype != NULL) | |
9887 | parameters = fntype->parameters(); | |
9888 | if (this->args_ != NULL) | |
9889 | { | |
9890 | Typed_identifier_list::const_iterator pt; | |
9891 | if (parameters != NULL) | |
9892 | pt = parameters->begin(); | |
09ea332d | 9893 | bool first = true; |
e440a328 | 9894 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9895 | pa != this->args_->end(); | |
9896 | ++pa) | |
9897 | { | |
09ea332d | 9898 | if (first) |
9899 | { | |
9900 | first = false; | |
9901 | // If this is a method, the first argument is the | |
9902 | // receiver. | |
9903 | if (fntype != NULL && fntype->is_method()) | |
9904 | { | |
9905 | Type* rtype = fntype->receiver()->type(); | |
9906 | // The receiver is always passed as a pointer. | |
9907 | if (rtype->points_to() == NULL) | |
9908 | rtype = Type::make_pointer_type(rtype); | |
9909 | Type_context subcontext(rtype, false); | |
9910 | (*pa)->determine_type(&subcontext); | |
9911 | continue; | |
9912 | } | |
9913 | } | |
9914 | ||
e440a328 | 9915 | if (parameters != NULL && pt != parameters->end()) |
9916 | { | |
9917 | Type_context subcontext(pt->type(), false); | |
9918 | (*pa)->determine_type(&subcontext); | |
9919 | ++pt; | |
9920 | } | |
9921 | else | |
9922 | (*pa)->determine_type_no_context(); | |
9923 | } | |
9924 | } | |
9925 | } | |
9926 | ||
fb94b0ca | 9927 | // Called when determining types for a Call_expression. Return true |
9928 | // if we should go ahead, false if they have already been determined. | |
9929 | ||
9930 | bool | |
9931 | Call_expression::determining_types() | |
9932 | { | |
9933 | if (this->types_are_determined_) | |
9934 | return false; | |
9935 | else | |
9936 | { | |
9937 | this->types_are_determined_ = true; | |
9938 | return true; | |
9939 | } | |
9940 | } | |
9941 | ||
e440a328 | 9942 | // Check types for parameter I. |
9943 | ||
9944 | bool | |
9945 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9946 | const Type* argument_type, | |
b13c66cd | 9947 | Location argument_location, |
e440a328 | 9948 | bool issued_error) |
9949 | { | |
9950 | std::string reason; | |
1eae365b | 9951 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 9952 | { |
9953 | if (!issued_error) | |
9954 | { | |
9955 | if (reason.empty()) | |
631d5788 | 9956 | go_error_at(argument_location, "argument %d has incompatible type", i); |
e440a328 | 9957 | else |
631d5788 | 9958 | go_error_at(argument_location, |
9959 | "argument %d has incompatible type (%s)", | |
9960 | i, reason.c_str()); | |
e440a328 | 9961 | } |
9962 | this->set_is_error(); | |
9963 | return false; | |
9964 | } | |
9965 | return true; | |
9966 | } | |
9967 | ||
9968 | // Check types. | |
9969 | ||
9970 | void | |
9971 | Call_expression::do_check_types(Gogo*) | |
9972 | { | |
a6645f74 | 9973 | if (this->classification() == EXPRESSION_ERROR) |
9974 | return; | |
9975 | ||
e440a328 | 9976 | Function_type* fntype = this->get_function_type(); |
9977 | if (fntype == NULL) | |
9978 | { | |
5c13bd80 | 9979 | if (!this->fn_->type()->is_error()) |
e440a328 | 9980 | this->report_error(_("expected function")); |
9981 | return; | |
9982 | } | |
9983 | ||
1373401e | 9984 | if (this->expected_result_count_ != 0 |
9985 | && this->expected_result_count_ != this->result_count()) | |
9986 | { | |
9987 | if (this->issue_error()) | |
9988 | this->report_error(_("function result count mismatch")); | |
9989 | this->set_is_error(); | |
9990 | return; | |
9991 | } | |
9992 | ||
09ea332d | 9993 | bool is_method = fntype->is_method(); |
9994 | if (is_method) | |
e440a328 | 9995 | { |
09ea332d | 9996 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9997 | Type* rtype = fntype->receiver()->type(); | |
9998 | Expression* first_arg = this->args_->front(); | |
1eae365b | 9999 | // We dereference the values since receivers are always passed |
10000 | // as pointers. | |
09ea332d | 10001 | std::string reason; |
1eae365b | 10002 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
10003 | &reason)) | |
e440a328 | 10004 | { |
09ea332d | 10005 | if (reason.empty()) |
10006 | this->report_error(_("incompatible type for receiver")); | |
10007 | else | |
e440a328 | 10008 | { |
631d5788 | 10009 | go_error_at(this->location(), |
10010 | "incompatible type for receiver (%s)", | |
10011 | reason.c_str()); | |
09ea332d | 10012 | this->set_is_error(); |
e440a328 | 10013 | } |
10014 | } | |
10015 | } | |
10016 | ||
10017 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 10018 | // we don't have to worry about it here unless something is wrong. |
10019 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
10020 | { | |
10021 | if (!fntype->is_varargs()) | |
10022 | { | |
631d5788 | 10023 | go_error_at(this->location(), |
10024 | _("invalid use of %<...%> calling non-variadic function")); | |
a6645f74 | 10025 | this->set_is_error(); |
10026 | return; | |
10027 | } | |
10028 | } | |
e440a328 | 10029 | |
10030 | const Typed_identifier_list* parameters = fntype->parameters(); | |
33d1d391 | 10031 | if (this->args_ == NULL || this->args_->size() == 0) |
e440a328 | 10032 | { |
10033 | if (parameters != NULL && !parameters->empty()) | |
10034 | this->report_error(_("not enough arguments")); | |
10035 | } | |
10036 | else if (parameters == NULL) | |
09ea332d | 10037 | { |
10038 | if (!is_method || this->args_->size() > 1) | |
10039 | this->report_error(_("too many arguments")); | |
10040 | } | |
1373401e | 10041 | else if (this->args_->size() == 1 |
10042 | && this->args_->front()->call_expression() != NULL | |
10043 | && this->args_->front()->call_expression()->result_count() > 1) | |
10044 | { | |
10045 | // This is F(G()) when G returns more than one result. If the | |
10046 | // results can be matched to parameters, it would have been | |
10047 | // lowered in do_lower. If we get here we know there is a | |
10048 | // mismatch. | |
10049 | if (this->args_->front()->call_expression()->result_count() | |
10050 | < parameters->size()) | |
10051 | this->report_error(_("not enough arguments")); | |
10052 | else | |
10053 | this->report_error(_("too many arguments")); | |
10054 | } | |
e440a328 | 10055 | else |
10056 | { | |
10057 | int i = 0; | |
09ea332d | 10058 | Expression_list::const_iterator pa = this->args_->begin(); |
10059 | if (is_method) | |
10060 | ++pa; | |
10061 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
10062 | pt != parameters->end(); | |
10063 | ++pt, ++pa, ++i) | |
e440a328 | 10064 | { |
09ea332d | 10065 | if (pa == this->args_->end()) |
e440a328 | 10066 | { |
09ea332d | 10067 | this->report_error(_("not enough arguments")); |
e440a328 | 10068 | return; |
10069 | } | |
10070 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
10071 | (*pa)->location(), false); | |
10072 | } | |
09ea332d | 10073 | if (pa != this->args_->end()) |
10074 | this->report_error(_("too many arguments")); | |
e440a328 | 10075 | } |
10076 | } | |
10077 | ||
72666aed | 10078 | Expression* |
10079 | Call_expression::do_copy() | |
10080 | { | |
10081 | Call_expression* call = | |
10082 | Expression::make_call(this->fn_->copy(), | |
10083 | (this->args_ == NULL | |
10084 | ? NULL | |
10085 | : this->args_->copy()), | |
10086 | this->is_varargs_, this->location()); | |
10087 | ||
10088 | if (this->varargs_are_lowered_) | |
10089 | call->set_varargs_are_lowered(); | |
10090 | return call; | |
10091 | } | |
10092 | ||
e440a328 | 10093 | // Return whether we have to use a temporary variable to ensure that |
10094 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 10095 | // results then it will inevitably be executed last. |
e440a328 | 10096 | |
10097 | bool | |
10098 | Call_expression::do_must_eval_in_order() const | |
10099 | { | |
ceeb4318 | 10100 | return this->result_count() > 0; |
e440a328 | 10101 | } |
10102 | ||
e440a328 | 10103 | // Get the function and the first argument to use when calling an |
10104 | // interface method. | |
10105 | ||
2387f644 | 10106 | Expression* |
e440a328 | 10107 | Call_expression::interface_method_function( |
e440a328 | 10108 | Interface_field_reference_expression* interface_method, |
db122cb9 | 10109 | Expression** first_arg_ptr, |
10110 | Location location) | |
e440a328 | 10111 | { |
db122cb9 | 10112 | Expression* object = interface_method->get_underlying_object(); |
10113 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); | |
10114 | *first_arg_ptr = | |
10115 | Expression::make_unsafe_cast(unsafe_ptr_type, object, location); | |
2387f644 | 10116 | return interface_method->get_function(); |
e440a328 | 10117 | } |
10118 | ||
10119 | // Build the call expression. | |
10120 | ||
ea664253 | 10121 | Bexpression* |
10122 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10123 | { |
5731103c | 10124 | Location location = this->location(); |
10125 | ||
2c809f8f | 10126 | if (this->call_ != NULL) |
5731103c | 10127 | { |
10128 | // If the call returns multiple results, make a new reference to | |
10129 | // the temporary. | |
10130 | if (this->call_temp_ != NULL) | |
10131 | { | |
10132 | Expression* ref = | |
10133 | Expression::make_temporary_reference(this->call_temp_, location); | |
10134 | return ref->get_backend(context); | |
10135 | } | |
10136 | ||
10137 | return this->call_; | |
10138 | } | |
e440a328 | 10139 | |
10140 | Function_type* fntype = this->get_function_type(); | |
10141 | if (fntype == NULL) | |
ea664253 | 10142 | return context->backend()->error_expression(); |
e440a328 | 10143 | |
10144 | if (this->fn_->is_error_expression()) | |
ea664253 | 10145 | return context->backend()->error_expression(); |
e440a328 | 10146 | |
10147 | Gogo* gogo = context->gogo(); | |
e440a328 | 10148 | |
10149 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 10150 | Interface_field_reference_expression* interface_method = |
10151 | this->fn_->interface_field_reference_expression(); | |
10152 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 10153 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 10154 | |
f8bdf81a | 10155 | bool has_closure_arg; |
8381eda7 | 10156 | if (has_closure) |
f8bdf81a | 10157 | has_closure_arg = true; |
8381eda7 | 10158 | else if (func != NULL) |
f8bdf81a | 10159 | has_closure_arg = false; |
8381eda7 | 10160 | else if (is_interface_method) |
f8bdf81a | 10161 | has_closure_arg = false; |
8381eda7 | 10162 | else |
f8bdf81a | 10163 | has_closure_arg = true; |
8381eda7 | 10164 | |
e440a328 | 10165 | int nargs; |
2c809f8f | 10166 | std::vector<Bexpression*> fn_args; |
e440a328 | 10167 | if (this->args_ == NULL || this->args_->empty()) |
10168 | { | |
f8bdf81a | 10169 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 10170 | if (nargs > 0) |
10171 | fn_args.resize(1); | |
e440a328 | 10172 | } |
09ea332d | 10173 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
10174 | { | |
10175 | // Passing a receiver parameter. | |
10176 | go_assert(!is_interface_method | |
10177 | && fntype->is_method() | |
10178 | && this->args_->size() == 1); | |
f8bdf81a | 10179 | nargs = 1; |
2c809f8f | 10180 | fn_args.resize(1); |
ea664253 | 10181 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 10182 | } |
e440a328 | 10183 | else |
10184 | { | |
10185 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 10186 | |
10187 | nargs = this->args_->size(); | |
09ea332d | 10188 | int i = is_interface_method ? 1 : 0; |
e440a328 | 10189 | nargs += i; |
2c809f8f | 10190 | fn_args.resize(nargs); |
e440a328 | 10191 | |
10192 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 10193 | Expression_list::const_iterator pe = this->args_->begin(); |
10194 | if (!is_interface_method && fntype->is_method()) | |
10195 | { | |
ea664253 | 10196 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 10197 | ++pe; |
10198 | ++i; | |
10199 | } | |
10200 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 10201 | { |
c484d925 | 10202 | go_assert(pp != params->end()); |
2c809f8f | 10203 | Expression* arg = |
10204 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
10205 | location); | |
ea664253 | 10206 | fn_args[i] = arg->get_backend(context); |
e440a328 | 10207 | } |
c484d925 | 10208 | go_assert(pp == params->end()); |
f8bdf81a | 10209 | go_assert(i == nargs); |
e440a328 | 10210 | } |
10211 | ||
2c809f8f | 10212 | Expression* fn; |
10213 | Expression* closure = NULL; | |
8381eda7 | 10214 | if (func != NULL) |
10215 | { | |
10216 | Named_object* no = func->named_object(); | |
2c809f8f | 10217 | fn = Expression::make_func_code_reference(no, location); |
10218 | if (has_closure) | |
10219 | closure = func->closure(); | |
8381eda7 | 10220 | } |
09ea332d | 10221 | else if (!is_interface_method) |
8381eda7 | 10222 | { |
2c809f8f | 10223 | closure = this->fn_; |
10224 | ||
10225 | // The backend representation of this function type is a pointer | |
10226 | // to a struct whose first field is the actual function to call. | |
10227 | Type* pfntype = | |
10228 | Type::make_pointer_type( | |
10229 | Type::make_pointer_type(Type::make_void_type())); | |
10230 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
f614ea8b | 10231 | fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location); |
2c809f8f | 10232 | } |
e440a328 | 10233 | else |
cf609de4 | 10234 | { |
2387f644 | 10235 | Expression* first_arg; |
db122cb9 | 10236 | fn = this->interface_method_function(interface_method, &first_arg, |
10237 | location); | |
ea664253 | 10238 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 10239 | } |
10240 | ||
1ecc6157 | 10241 | Bexpression* bclosure = NULL; |
10242 | if (has_closure_arg) | |
10243 | bclosure = closure->get_backend(context); | |
f8bdf81a | 10244 | else |
1ecc6157 | 10245 | go_assert(closure == NULL); |
f8bdf81a | 10246 | |
ea664253 | 10247 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 10248 | |
10249 | // When not calling a named function directly, use a type conversion | |
10250 | // in case the type of the function is a recursive type which refers | |
10251 | // to itself. We don't do this for an interface method because 1) | |
10252 | // an interface method never refers to itself, so we always have a | |
10253 | // function type here; 2) we pass an extra first argument to an | |
10254 | // interface method, so fntype is not correct. | |
10255 | if (func == NULL && !is_interface_method) | |
10256 | { | |
10257 | Btype* bft = fntype->get_backend_fntype(gogo); | |
10258 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
10259 | } | |
10260 | ||
4ced7af9 | 10261 | Bfunction* bfunction = NULL; |
10262 | if (context->function()) | |
10263 | bfunction = context->function()->func_value()->get_decl(); | |
10264 | Bexpression* call = gogo->backend()->call_expression(bfunction, bfn, | |
10265 | fn_args, bclosure, | |
10266 | location); | |
e440a328 | 10267 | |
5731103c | 10268 | if (this->call_temp_ != NULL) |
e440a328 | 10269 | { |
5731103c | 10270 | // This case occurs when the call returns multiple results. |
e440a328 | 10271 | |
5731103c | 10272 | Expression* ref = Expression::make_temporary_reference(this->call_temp_, |
10273 | location); | |
10274 | Bexpression* bref = ref->get_backend(context); | |
10275 | Bstatement* bassn = gogo->backend()->assignment_statement(bfunction, | |
10276 | bref, call, | |
10277 | location); | |
e440a328 | 10278 | |
5731103c | 10279 | ref = Expression::make_temporary_reference(this->call_temp_, location); |
10280 | this->call_ = ref->get_backend(context); | |
10281 | ||
10282 | return gogo->backend()->compound_expression(bassn, this->call_, | |
10283 | location); | |
2c809f8f | 10284 | } |
e440a328 | 10285 | |
2c809f8f | 10286 | this->call_ = call; |
ea664253 | 10287 | return this->call_; |
e440a328 | 10288 | } |
10289 | ||
d751bb78 | 10290 | // Dump ast representation for a call expressin. |
10291 | ||
10292 | void | |
10293 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
10294 | { | |
10295 | this->fn_->dump_expression(ast_dump_context); | |
10296 | ast_dump_context->ostream() << "("; | |
10297 | if (args_ != NULL) | |
10298 | ast_dump_context->dump_expression_list(this->args_); | |
10299 | ||
10300 | ast_dump_context->ostream() << ") "; | |
10301 | } | |
10302 | ||
e440a328 | 10303 | // Make a call expression. |
10304 | ||
10305 | Call_expression* | |
10306 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 10307 | Location location) |
e440a328 | 10308 | { |
10309 | return new Call_expression(fn, args, is_varargs, location); | |
10310 | } | |
10311 | ||
da244e59 | 10312 | // Class Call_result_expression. |
e440a328 | 10313 | |
10314 | // Traverse a call result. | |
10315 | ||
10316 | int | |
10317 | Call_result_expression::do_traverse(Traverse* traverse) | |
10318 | { | |
10319 | if (traverse->remember_expression(this->call_)) | |
10320 | { | |
10321 | // We have already traversed the call expression. | |
10322 | return TRAVERSE_CONTINUE; | |
10323 | } | |
10324 | return Expression::traverse(&this->call_, traverse); | |
10325 | } | |
10326 | ||
10327 | // Get the type. | |
10328 | ||
10329 | Type* | |
10330 | Call_result_expression::do_type() | |
10331 | { | |
425dd051 | 10332 | if (this->classification() == EXPRESSION_ERROR) |
10333 | return Type::make_error_type(); | |
10334 | ||
e440a328 | 10335 | // THIS->CALL_ can be replaced with a temporary reference due to |
10336 | // Call_expression::do_must_eval_in_order when there is an error. | |
10337 | Call_expression* ce = this->call_->call_expression(); | |
10338 | if (ce == NULL) | |
5e85f268 | 10339 | { |
10340 | this->set_is_error(); | |
10341 | return Type::make_error_type(); | |
10342 | } | |
e440a328 | 10343 | Function_type* fntype = ce->get_function_type(); |
10344 | if (fntype == NULL) | |
5e85f268 | 10345 | { |
e37658e2 | 10346 | if (ce->issue_error()) |
99b3f06f | 10347 | { |
10348 | if (!ce->fn()->type()->is_error()) | |
10349 | this->report_error(_("expected function")); | |
10350 | } | |
5e85f268 | 10351 | this->set_is_error(); |
10352 | return Type::make_error_type(); | |
10353 | } | |
e440a328 | 10354 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10355 | if (results == NULL || results->size() < 2) |
7b8d861f | 10356 | { |
ceeb4318 | 10357 | if (ce->issue_error()) |
10358 | this->report_error(_("number of results does not match " | |
10359 | "number of values")); | |
7b8d861f | 10360 | return Type::make_error_type(); |
10361 | } | |
e440a328 | 10362 | Typed_identifier_list::const_iterator pr = results->begin(); |
10363 | for (unsigned int i = 0; i < this->index_; ++i) | |
10364 | { | |
10365 | if (pr == results->end()) | |
425dd051 | 10366 | break; |
e440a328 | 10367 | ++pr; |
10368 | } | |
10369 | if (pr == results->end()) | |
425dd051 | 10370 | { |
ceeb4318 | 10371 | if (ce->issue_error()) |
10372 | this->report_error(_("number of results does not match " | |
10373 | "number of values")); | |
425dd051 | 10374 | return Type::make_error_type(); |
10375 | } | |
e440a328 | 10376 | return pr->type(); |
10377 | } | |
10378 | ||
425dd051 | 10379 | // Check the type. Just make sure that we trigger the warning in |
10380 | // do_type. | |
e440a328 | 10381 | |
10382 | void | |
10383 | Call_result_expression::do_check_types(Gogo*) | |
10384 | { | |
425dd051 | 10385 | this->type(); |
e440a328 | 10386 | } |
10387 | ||
10388 | // Determine the type. We have nothing to do here, but the 0 result | |
10389 | // needs to pass down to the caller. | |
10390 | ||
10391 | void | |
10392 | Call_result_expression::do_determine_type(const Type_context*) | |
10393 | { | |
fb94b0ca | 10394 | this->call_->determine_type_no_context(); |
e440a328 | 10395 | } |
10396 | ||
ea664253 | 10397 | // Return the backend representation. We just refer to the temporary set by the |
10398 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 10399 | // hard to evaluate the call at the right time. |
e440a328 | 10400 | |
ea664253 | 10401 | Bexpression* |
10402 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10403 | { |
ceeb4318 | 10404 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10405 | if (ce == NULL) |
10406 | { | |
10407 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 10408 | return context->backend()->error_expression(); |
cd238b8d | 10409 | } |
5731103c | 10410 | Temporary_statement* ts = ce->results(); |
cd238b8d | 10411 | if (ts == NULL) |
10412 | { | |
10413 | go_assert(saw_errors()); | |
ea664253 | 10414 | return context->backend()->error_expression(); |
cd238b8d | 10415 | } |
ceeb4318 | 10416 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
5731103c | 10417 | ref = Expression::make_field_reference(ref, this->index_, this->location()); |
ea664253 | 10418 | return ref->get_backend(context); |
e440a328 | 10419 | } |
10420 | ||
d751bb78 | 10421 | // Dump ast representation for a call result expression. |
10422 | ||
10423 | void | |
10424 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10425 | const | |
10426 | { | |
10427 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10428 | // (struct) and the fields are referenced instead. | |
10429 | ast_dump_context->ostream() << this->index_ << "@("; | |
10430 | ast_dump_context->dump_expression(this->call_); | |
10431 | ast_dump_context->ostream() << ")"; | |
10432 | } | |
10433 | ||
e440a328 | 10434 | // Make a reference to a single result of a call which returns |
10435 | // multiple results. | |
10436 | ||
10437 | Expression* | |
10438 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10439 | { | |
10440 | return new Call_result_expression(call, index); | |
10441 | } | |
10442 | ||
10443 | // Class Index_expression. | |
10444 | ||
10445 | // Traversal. | |
10446 | ||
10447 | int | |
10448 | Index_expression::do_traverse(Traverse* traverse) | |
10449 | { | |
10450 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10451 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10452 | || (this->end_ != NULL | |
acf2b673 | 10453 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
10454 | || (this->cap_ != NULL | |
10455 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 10456 | return TRAVERSE_EXIT; |
10457 | return TRAVERSE_CONTINUE; | |
10458 | } | |
10459 | ||
10460 | // Lower an index expression. This converts the generic index | |
10461 | // expression into an array index, a string index, or a map index. | |
10462 | ||
10463 | Expression* | |
ceeb4318 | 10464 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10465 | { |
b13c66cd | 10466 | Location location = this->location(); |
e440a328 | 10467 | Expression* left = this->left_; |
10468 | Expression* start = this->start_; | |
10469 | Expression* end = this->end_; | |
acf2b673 | 10470 | Expression* cap = this->cap_; |
e440a328 | 10471 | |
10472 | Type* type = left->type(); | |
5c13bd80 | 10473 | if (type->is_error()) |
d9f3743a | 10474 | { |
10475 | go_assert(saw_errors()); | |
10476 | return Expression::make_error(location); | |
10477 | } | |
b0cf7ddd | 10478 | else if (left->is_type_expression()) |
10479 | { | |
631d5788 | 10480 | go_error_at(location, "attempt to index type expression"); |
b0cf7ddd | 10481 | return Expression::make_error(location); |
10482 | } | |
e440a328 | 10483 | else if (type->array_type() != NULL) |
acf2b673 | 10484 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 10485 | else if (type->points_to() != NULL |
10486 | && type->points_to()->array_type() != NULL | |
411eb89e | 10487 | && !type->points_to()->is_slice_type()) |
e440a328 | 10488 | { |
f614ea8b | 10489 | Expression* deref = |
10490 | Expression::make_dereference(left, NIL_CHECK_DEFAULT, location); | |
38092374 | 10491 | |
10492 | // For an ordinary index into the array, the pointer will be | |
10493 | // dereferenced. For a slice it will not--the resulting slice | |
10494 | // will simply reuse the pointer, which is incorrect if that | |
10495 | // pointer is nil. | |
10496 | if (end != NULL || cap != NULL) | |
10497 | deref->issue_nil_check(); | |
10498 | ||
acf2b673 | 10499 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 10500 | } |
10501 | else if (type->is_string_type()) | |
acf2b673 | 10502 | { |
10503 | if (cap != NULL) | |
10504 | { | |
631d5788 | 10505 | go_error_at(location, "invalid 3-index slice of string"); |
acf2b673 | 10506 | return Expression::make_error(location); |
10507 | } | |
10508 | return Expression::make_string_index(left, start, end, location); | |
10509 | } | |
e440a328 | 10510 | else if (type->map_type() != NULL) |
10511 | { | |
acf2b673 | 10512 | if (end != NULL || cap != NULL) |
e440a328 | 10513 | { |
631d5788 | 10514 | go_error_at(location, "invalid slice of map"); |
e440a328 | 10515 | return Expression::make_error(location); |
10516 | } | |
0d5530d9 | 10517 | return Expression::make_map_index(left, start, location); |
e440a328 | 10518 | } |
b1aba207 | 10519 | else if (cap != NULL) |
10520 | { | |
10521 | go_error_at(location, | |
10522 | "invalid 3-index slice of object that is not a slice"); | |
10523 | return Expression::make_error(location); | |
10524 | } | |
10525 | else if (end != NULL) | |
10526 | { | |
10527 | go_error_at(location, | |
10528 | ("attempt to slice object that is not " | |
10529 | "array, slice, or string")); | |
10530 | return Expression::make_error(location); | |
10531 | } | |
e440a328 | 10532 | else |
10533 | { | |
631d5788 | 10534 | go_error_at(location, |
b1aba207 | 10535 | ("attempt to index object that is not " |
10536 | "array, slice, string, or map")); | |
e440a328 | 10537 | return Expression::make_error(location); |
10538 | } | |
10539 | } | |
10540 | ||
acf2b673 | 10541 | // Write an indexed expression |
10542 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 10543 | |
10544 | void | |
10545 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10546 | const Expression* expr, | |
10547 | const Expression* start, | |
acf2b673 | 10548 | const Expression* end, |
10549 | const Expression* cap) | |
d751bb78 | 10550 | { |
10551 | expr->dump_expression(ast_dump_context); | |
10552 | ast_dump_context->ostream() << "["; | |
10553 | start->dump_expression(ast_dump_context); | |
10554 | if (end != NULL) | |
10555 | { | |
10556 | ast_dump_context->ostream() << ":"; | |
10557 | end->dump_expression(ast_dump_context); | |
10558 | } | |
acf2b673 | 10559 | if (cap != NULL) |
10560 | { | |
10561 | ast_dump_context->ostream() << ":"; | |
10562 | cap->dump_expression(ast_dump_context); | |
10563 | } | |
d751bb78 | 10564 | ast_dump_context->ostream() << "]"; |
10565 | } | |
10566 | ||
10567 | // Dump ast representation for an index expression. | |
10568 | ||
10569 | void | |
10570 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10571 | const | |
10572 | { | |
10573 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 10574 | this->start_, this->end_, this->cap_); |
d751bb78 | 10575 | } |
10576 | ||
e440a328 | 10577 | // Make an index expression. |
10578 | ||
10579 | Expression* | |
10580 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 10581 | Expression* cap, Location location) |
e440a328 | 10582 | { |
acf2b673 | 10583 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 10584 | } |
10585 | ||
da244e59 | 10586 | // Class Array_index_expression. |
e440a328 | 10587 | |
10588 | // Array index traversal. | |
10589 | ||
10590 | int | |
10591 | Array_index_expression::do_traverse(Traverse* traverse) | |
10592 | { | |
10593 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10594 | return TRAVERSE_EXIT; | |
10595 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10596 | return TRAVERSE_EXIT; | |
10597 | if (this->end_ != NULL) | |
10598 | { | |
10599 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10600 | return TRAVERSE_EXIT; | |
10601 | } | |
acf2b673 | 10602 | if (this->cap_ != NULL) |
10603 | { | |
10604 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10605 | return TRAVERSE_EXIT; | |
10606 | } | |
e440a328 | 10607 | return TRAVERSE_CONTINUE; |
10608 | } | |
10609 | ||
10610 | // Return the type of an array index. | |
10611 | ||
10612 | Type* | |
10613 | Array_index_expression::do_type() | |
10614 | { | |
10615 | if (this->type_ == NULL) | |
10616 | { | |
10617 | Array_type* type = this->array_->type()->array_type(); | |
10618 | if (type == NULL) | |
10619 | this->type_ = Type::make_error_type(); | |
10620 | else if (this->end_ == NULL) | |
10621 | this->type_ = type->element_type(); | |
411eb89e | 10622 | else if (type->is_slice_type()) |
e440a328 | 10623 | { |
10624 | // A slice of a slice has the same type as the original | |
10625 | // slice. | |
10626 | this->type_ = this->array_->type()->deref(); | |
10627 | } | |
10628 | else | |
10629 | { | |
10630 | // A slice of an array is a slice. | |
10631 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10632 | } | |
10633 | } | |
10634 | return this->type_; | |
10635 | } | |
10636 | ||
10637 | // Set the type of an array index. | |
10638 | ||
10639 | void | |
10640 | Array_index_expression::do_determine_type(const Type_context*) | |
10641 | { | |
10642 | this->array_->determine_type_no_context(); | |
f77aa642 | 10643 | |
10644 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10645 | if (this->start_->is_constant()) | |
10646 | this->start_->determine_type(&index_context); | |
10647 | else | |
10648 | this->start_->determine_type_no_context(); | |
e440a328 | 10649 | if (this->end_ != NULL) |
f77aa642 | 10650 | { |
10651 | if (this->end_->is_constant()) | |
10652 | this->end_->determine_type(&index_context); | |
10653 | else | |
10654 | this->end_->determine_type_no_context(); | |
10655 | } | |
acf2b673 | 10656 | if (this->cap_ != NULL) |
f77aa642 | 10657 | { |
10658 | if (this->cap_->is_constant()) | |
10659 | this->cap_->determine_type(&index_context); | |
10660 | else | |
10661 | this->cap_->determine_type_no_context(); | |
10662 | } | |
e440a328 | 10663 | } |
10664 | ||
10665 | // Check types of an array index. | |
10666 | ||
10667 | void | |
b7327dbf | 10668 | Array_index_expression::do_check_types(Gogo*) |
e440a328 | 10669 | { |
f6bc81e6 | 10670 | Numeric_constant nc; |
10671 | unsigned long v; | |
10672 | if (this->start_->type()->integer_type() == NULL | |
10673 | && !this->start_->type()->is_error() | |
10674 | && (!this->start_->numeric_constant_value(&nc) | |
10675 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10676 | this->report_error(_("index must be integer")); |
10677 | if (this->end_ != NULL | |
10678 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10679 | && !this->end_->type()->is_error() |
10680 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10681 | && !this->end_->is_error_expression() |
10682 | && (!this->end_->numeric_constant_value(&nc) | |
10683 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10684 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10685 | if (this->cap_ != NULL |
10686 | && this->cap_->type()->integer_type() == NULL | |
10687 | && !this->cap_->type()->is_error() | |
10688 | && !this->cap_->is_nil_expression() | |
10689 | && !this->cap_->is_error_expression() | |
10690 | && (!this->cap_->numeric_constant_value(&nc) | |
10691 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10692 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10693 | |
10694 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10695 | if (array_type == NULL) |
10696 | { | |
c484d925 | 10697 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10698 | return; |
10699 | } | |
e440a328 | 10700 | |
10701 | unsigned int int_bits = | |
10702 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10703 | ||
0c77715b | 10704 | Numeric_constant lvalnc; |
e440a328 | 10705 | mpz_t lval; |
e440a328 | 10706 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10707 | && array_type->length()->numeric_constant_value(&lvalnc) |
10708 | && lvalnc.to_int(&lval)); | |
10709 | Numeric_constant inc; | |
e440a328 | 10710 | mpz_t ival; |
0bd5d859 | 10711 | bool ival_valid = false; |
0c77715b | 10712 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10713 | { |
0bd5d859 | 10714 | ival_valid = true; |
e440a328 | 10715 | if (mpz_sgn(ival) < 0 |
10716 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10717 | || (lval_valid | |
10718 | && (this->end_ == NULL | |
10719 | ? mpz_cmp(ival, lval) >= 0 | |
10720 | : mpz_cmp(ival, lval) > 0))) | |
10721 | { | |
631d5788 | 10722 | go_error_at(this->start_->location(), "array index out of bounds"); |
e440a328 | 10723 | this->set_is_error(); |
10724 | } | |
10725 | } | |
10726 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10727 | { | |
0c77715b | 10728 | Numeric_constant enc; |
10729 | mpz_t eval; | |
acf2b673 | 10730 | bool eval_valid = false; |
0c77715b | 10731 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10732 | { |
acf2b673 | 10733 | eval_valid = true; |
0c77715b | 10734 | if (mpz_sgn(eval) < 0 |
10735 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10736 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10737 | { |
631d5788 | 10738 | go_error_at(this->end_->location(), "array index out of bounds"); |
e440a328 | 10739 | this->set_is_error(); |
10740 | } | |
0bd5d859 | 10741 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10742 | this->report_error(_("inverted slice range")); | |
e440a328 | 10743 | } |
acf2b673 | 10744 | |
10745 | Numeric_constant cnc; | |
10746 | mpz_t cval; | |
10747 | if (this->cap_ != NULL | |
10748 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10749 | { | |
10750 | if (mpz_sgn(cval) < 0 | |
10751 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10752 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10753 | { | |
631d5788 | 10754 | go_error_at(this->cap_->location(), "array index out of bounds"); |
acf2b673 | 10755 | this->set_is_error(); |
10756 | } | |
10757 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10758 | { | |
631d5788 | 10759 | go_error_at(this->cap_->location(), |
10760 | "invalid slice index: capacity less than start"); | |
acf2b673 | 10761 | this->set_is_error(); |
10762 | } | |
10763 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10764 | { | |
631d5788 | 10765 | go_error_at(this->cap_->location(), |
10766 | "invalid slice index: capacity less than length"); | |
acf2b673 | 10767 | this->set_is_error(); |
10768 | } | |
10769 | mpz_clear(cval); | |
10770 | } | |
10771 | ||
10772 | if (eval_valid) | |
10773 | mpz_clear(eval); | |
e440a328 | 10774 | } |
0bd5d859 | 10775 | if (ival_valid) |
10776 | mpz_clear(ival); | |
0c77715b | 10777 | if (lval_valid) |
10778 | mpz_clear(lval); | |
e440a328 | 10779 | |
10780 | // A slice of an array requires an addressable array. A slice of a | |
10781 | // slice is always possible. | |
411eb89e | 10782 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10783 | { |
10784 | if (!this->array_->is_addressable()) | |
8da39c3b | 10785 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10786 | else |
b7327dbf | 10787 | // Set the array address taken but not escape. The escape |
10788 | // analysis will make it escape to heap when needed. | |
10789 | this->array_->address_taken(false); | |
88ec30c8 | 10790 | } |
e440a328 | 10791 | } |
10792 | ||
2c809f8f | 10793 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10794 | |
10795 | Expression* | |
10796 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10797 | Statement_inserter* inserter) | |
10798 | { | |
10799 | Location loc = this->location(); | |
5bf8be8b | 10800 | Expression* array = this->array_; |
10801 | Expression* start = this->start_; | |
10802 | Expression* end = this->end_; | |
10803 | Expression* cap = this->cap_; | |
10804 | if (array->is_error_expression() | |
10805 | || array->type()->is_error_type() | |
10806 | || start->is_error_expression() | |
10807 | || start->type()->is_error_type() | |
10808 | || (end != NULL | |
10809 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10810 | || (cap != NULL | |
10811 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10812 | { | |
10813 | go_assert(saw_errors()); | |
10814 | return Expression::make_error(loc); | |
10815 | } | |
10816 | ||
2c809f8f | 10817 | Temporary_statement* temp; |
5bf8be8b | 10818 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10819 | { |
5bf8be8b | 10820 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10821 | inserter->insert(temp); |
10822 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10823 | } | |
5bf8be8b | 10824 | if (!start->is_variable()) |
2c809f8f | 10825 | { |
5bf8be8b | 10826 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10827 | inserter->insert(temp); |
10828 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10829 | } | |
5bf8be8b | 10830 | if (end != NULL |
10831 | && !end->is_nil_expression() | |
10832 | && !end->is_variable()) | |
2c809f8f | 10833 | { |
5bf8be8b | 10834 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10835 | inserter->insert(temp); |
10836 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10837 | } | |
03118c21 | 10838 | if (cap != NULL && !cap->is_variable()) |
2c809f8f | 10839 | { |
5bf8be8b | 10840 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10841 | inserter->insert(temp); |
10842 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10843 | } | |
10844 | ||
35a54f17 | 10845 | return this; |
10846 | } | |
10847 | ||
e440a328 | 10848 | // Return whether this expression is addressable. |
10849 | ||
10850 | bool | |
10851 | Array_index_expression::do_is_addressable() const | |
10852 | { | |
10853 | // A slice expression is not addressable. | |
10854 | if (this->end_ != NULL) | |
10855 | return false; | |
10856 | ||
10857 | // An index into a slice is addressable. | |
411eb89e | 10858 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10859 | return true; |
10860 | ||
10861 | // An index into an array is addressable if the array is | |
10862 | // addressable. | |
10863 | return this->array_->is_addressable(); | |
10864 | } | |
10865 | ||
bf1323be | 10866 | void |
10867 | Array_index_expression::do_address_taken(bool escapes) | |
10868 | { | |
10869 | // In &x[0], if x is a slice, then x's address is not taken. | |
10870 | if (!this->array_->type()->is_slice_type()) | |
10871 | this->array_->address_taken(escapes); | |
10872 | } | |
10873 | ||
ea664253 | 10874 | // Get the backend representation for an array index. |
e440a328 | 10875 | |
ea664253 | 10876 | Bexpression* |
10877 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10878 | { |
e440a328 | 10879 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10880 | if (array_type == NULL) |
10881 | { | |
c484d925 | 10882 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10883 | return context->backend()->error_expression(); |
d8cd8e2d | 10884 | } |
35a54f17 | 10885 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10886 | |
2c809f8f | 10887 | Location loc = this->location(); |
10888 | Gogo* gogo = context->gogo(); | |
10889 | ||
6dfedc16 | 10890 | Type* int_type = Type::lookup_integer_type("int"); |
10891 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10892 | |
2c809f8f | 10893 | // We need to convert the length and capacity to the Go "int" type here |
10894 | // because the length of a fixed-length array could be of type "uintptr" | |
10895 | // and gimple disallows binary operations between "uintptr" and other | |
10896 | // integer types. FIXME. | |
10897 | Bexpression* length = NULL; | |
a04bfdfc | 10898 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10899 | { | |
35a54f17 | 10900 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10901 | length = len->get_backend(context); |
2c809f8f | 10902 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10903 | } |
10904 | ||
2c809f8f | 10905 | Bexpression* capacity = NULL; |
a04bfdfc | 10906 | if (this->end_ != NULL) |
10907 | { | |
35a54f17 | 10908 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10909 | capacity = cap->get_backend(context); |
2c809f8f | 10910 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10911 | } |
10912 | ||
2c809f8f | 10913 | Bexpression* cap_arg = capacity; |
acf2b673 | 10914 | if (this->cap_ != NULL) |
10915 | { | |
ea664253 | 10916 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10917 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10918 | } |
10919 | ||
2c809f8f | 10920 | if (length == NULL) |
10921 | length = cap_arg; | |
e440a328 | 10922 | |
10923 | int code = (array_type->length() != NULL | |
10924 | ? (this->end_ == NULL | |
10925 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10926 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10927 | : (this->end_ == NULL | |
10928 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10929 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10930 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10931 | |
6dfedc16 | 10932 | if (this->start_->type()->integer_type() == NULL |
10933 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10934 | { | |
10935 | go_assert(saw_errors()); | |
10936 | return context->backend()->error_expression(); | |
10937 | } | |
d9f3743a | 10938 | |
ea664253 | 10939 | Bexpression* bad_index = |
d9f3743a | 10940 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 10941 | |
ea664253 | 10942 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10943 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10944 | Bexpression* start_too_large = | |
10945 | gogo->backend()->binary_expression((this->end_ == NULL | |
10946 | ? OPERATOR_GE | |
10947 | : OPERATOR_GT), | |
10948 | start, | |
10949 | (this->end_ == NULL | |
10950 | ? length | |
10951 | : capacity), | |
10952 | loc); | |
10953 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10954 | bad_index, loc); | |
e440a328 | 10955 | |
93715b75 | 10956 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 10957 | if (this->end_ == NULL) |
10958 | { | |
10959 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10960 | // wrap the index check into START. |
10961 | start = | |
93715b75 | 10962 | gogo->backend()->conditional_expression(bfn, int_btype, bad_index, |
2c809f8f | 10963 | crash, start, loc); |
e440a328 | 10964 | |
2c809f8f | 10965 | Bexpression* ret; |
e440a328 | 10966 | if (array_type->length() != NULL) |
10967 | { | |
ea664253 | 10968 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10969 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10970 | } |
10971 | else | |
10972 | { | |
2c809f8f | 10973 | // Slice. |
10974 | Expression* valptr = | |
44dbe1d7 | 10975 | array_type->get_value_pointer(gogo, this->array_, |
10976 | this->is_lvalue_); | |
ea664253 | 10977 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 10978 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 10979 | |
10980 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
10981 | Btype* ele_btype = ele_type->get_backend(gogo); | |
10982 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 10983 | } |
ea664253 | 10984 | return ret; |
e440a328 | 10985 | } |
10986 | ||
10987 | // Array slice. | |
10988 | ||
acf2b673 | 10989 | if (this->cap_ != NULL) |
10990 | { | |
2c809f8f | 10991 | Bexpression* bounds_bcheck = |
ea664253 | 10992 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 10993 | bad_index = |
10994 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10995 | bad_index, loc); | |
10996 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
10997 | ||
10998 | Bexpression* cap_too_small = | |
10999 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
11000 | Bexpression* cap_too_large = | |
11001 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
11002 | Bexpression* bad_cap = | |
11003 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
11004 | cap_too_large, loc); | |
11005 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
11006 | bad_index, loc); | |
11007 | } | |
11008 | ||
11009 | Bexpression* end; | |
e440a328 | 11010 | if (this->end_->is_nil_expression()) |
2c809f8f | 11011 | end = length; |
e440a328 | 11012 | else |
11013 | { | |
2c809f8f | 11014 | Bexpression* bounds_bcheck = |
ea664253 | 11015 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 11016 | |
2c809f8f | 11017 | bad_index = |
11018 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11019 | bad_index, loc); | |
e440a328 | 11020 | |
ea664253 | 11021 | end = this->end_->get_backend(context); |
2c809f8f | 11022 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
11023 | Bexpression* end_too_small = | |
11024 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
11025 | Bexpression* end_too_large = | |
11026 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
11027 | Bexpression* bad_end = | |
11028 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
11029 | end_too_large, loc); | |
11030 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
11031 | bad_index, loc); | |
e440a328 | 11032 | } |
11033 | ||
2c809f8f | 11034 | Bexpression* result_length = |
11035 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 11036 | |
2c809f8f | 11037 | Bexpression* result_capacity = |
11038 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 11039 | |
03118c21 | 11040 | // If the new capacity is zero, don't change val. Otherwise we can |
11041 | // get a pointer to the next object in memory, keeping it live | |
11042 | // unnecessarily. When the capacity is zero, the actual pointer | |
11043 | // value doesn't matter. | |
11044 | Bexpression* zero = | |
11045 | Expression::make_integer_ul(0, int_type, loc)->get_backend(context); | |
11046 | Bexpression* cond = | |
11047 | gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero, | |
11048 | loc); | |
11049 | Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype, | |
11050 | cond, zero, | |
11051 | start, loc); | |
44dbe1d7 | 11052 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_, |
11053 | this->is_lvalue_); | |
03118c21 | 11054 | Bexpression* val = valptr->get_backend(context); |
11055 | val = gogo->backend()->pointer_offset_expression(val, offset, loc); | |
11056 | ||
2c809f8f | 11057 | Btype* struct_btype = this->type()->get_backend(gogo); |
11058 | std::vector<Bexpression*> init; | |
11059 | init.push_back(val); | |
11060 | init.push_back(result_length); | |
11061 | init.push_back(result_capacity); | |
e440a328 | 11062 | |
2c809f8f | 11063 | Bexpression* ctor = |
11064 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
93715b75 | 11065 | return gogo->backend()->conditional_expression(bfn, struct_btype, bad_index, |
ea664253 | 11066 | crash, ctor, loc); |
e440a328 | 11067 | } |
11068 | ||
d751bb78 | 11069 | // Dump ast representation for an array index expression. |
11070 | ||
11071 | void | |
11072 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11073 | const | |
11074 | { | |
11075 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 11076 | this->start_, this->end_, this->cap_); |
d751bb78 | 11077 | } |
11078 | ||
acf2b673 | 11079 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 11080 | |
11081 | Expression* | |
11082 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 11083 | Expression* end, Expression* cap, |
11084 | Location location) | |
e440a328 | 11085 | { |
acf2b673 | 11086 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 11087 | } |
11088 | ||
50075d74 | 11089 | // Class String_index_expression. |
e440a328 | 11090 | |
11091 | // String index traversal. | |
11092 | ||
11093 | int | |
11094 | String_index_expression::do_traverse(Traverse* traverse) | |
11095 | { | |
11096 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
11097 | return TRAVERSE_EXIT; | |
11098 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
11099 | return TRAVERSE_EXIT; | |
11100 | if (this->end_ != NULL) | |
11101 | { | |
11102 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
11103 | return TRAVERSE_EXIT; | |
11104 | } | |
11105 | return TRAVERSE_CONTINUE; | |
11106 | } | |
11107 | ||
2c809f8f | 11108 | Expression* |
11109 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
11110 | Statement_inserter* inserter) | |
e440a328 | 11111 | { |
2c809f8f | 11112 | Location loc = this->location(); |
5bf8be8b | 11113 | Expression* string = this->string_; |
11114 | Expression* start = this->start_; | |
11115 | Expression* end = this->end_; | |
11116 | if (string->is_error_expression() | |
11117 | || string->type()->is_error_type() | |
11118 | || start->is_error_expression() | |
11119 | || start->type()->is_error_type() | |
11120 | || (end != NULL | |
11121 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
11122 | { | |
11123 | go_assert(saw_errors()); | |
11124 | return Expression::make_error(loc); | |
11125 | } | |
11126 | ||
11127 | Temporary_statement* temp; | |
2c809f8f | 11128 | if (!this->string_->is_variable()) |
11129 | { | |
11130 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
11131 | inserter->insert(temp); | |
11132 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
11133 | } | |
11134 | if (!this->start_->is_variable()) | |
11135 | { | |
11136 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
11137 | inserter->insert(temp); | |
11138 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
11139 | } | |
11140 | if (this->end_ != NULL | |
11141 | && !this->end_->is_nil_expression() | |
11142 | && !this->end_->is_variable()) | |
11143 | { | |
11144 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
11145 | inserter->insert(temp); | |
11146 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
11147 | } | |
11148 | ||
11149 | return this; | |
11150 | } | |
11151 | ||
11152 | // Return the type of a string index. | |
11153 | ||
11154 | Type* | |
11155 | String_index_expression::do_type() | |
11156 | { | |
11157 | if (this->end_ == NULL) | |
11158 | return Type::lookup_integer_type("uint8"); | |
11159 | else | |
11160 | return this->string_->type(); | |
11161 | } | |
11162 | ||
11163 | // Determine the type of a string index. | |
11164 | ||
11165 | void | |
11166 | String_index_expression::do_determine_type(const Type_context*) | |
11167 | { | |
11168 | this->string_->determine_type_no_context(); | |
f77aa642 | 11169 | |
11170 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
11171 | if (this->start_->is_constant()) | |
11172 | this->start_->determine_type(&index_context); | |
11173 | else | |
11174 | this->start_->determine_type_no_context(); | |
e440a328 | 11175 | if (this->end_ != NULL) |
f77aa642 | 11176 | { |
11177 | if (this->end_->is_constant()) | |
11178 | this->end_->determine_type(&index_context); | |
11179 | else | |
11180 | this->end_->determine_type_no_context(); | |
11181 | } | |
e440a328 | 11182 | } |
11183 | ||
11184 | // Check types of a string index. | |
11185 | ||
11186 | void | |
11187 | String_index_expression::do_check_types(Gogo*) | |
11188 | { | |
acdc230d | 11189 | Numeric_constant nc; |
11190 | unsigned long v; | |
11191 | if (this->start_->type()->integer_type() == NULL | |
11192 | && !this->start_->type()->is_error() | |
11193 | && (!this->start_->numeric_constant_value(&nc) | |
11194 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11195 | this->report_error(_("index must be integer")); |
11196 | if (this->end_ != NULL | |
11197 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 11198 | && !this->end_->type()->is_error() |
11199 | && !this->end_->is_nil_expression() | |
11200 | && !this->end_->is_error_expression() | |
11201 | && (!this->end_->numeric_constant_value(&nc) | |
11202 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11203 | this->report_error(_("slice end must be integer")); |
11204 | ||
11205 | std::string sval; | |
11206 | bool sval_valid = this->string_->string_constant_value(&sval); | |
11207 | ||
0c77715b | 11208 | Numeric_constant inc; |
e440a328 | 11209 | mpz_t ival; |
0bd5d859 | 11210 | bool ival_valid = false; |
0c77715b | 11211 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 11212 | { |
0bd5d859 | 11213 | ival_valid = true; |
e440a328 | 11214 | if (mpz_sgn(ival) < 0 |
b10f32fb | 11215 | || (sval_valid |
11216 | && (this->end_ == NULL | |
11217 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
11218 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 11219 | { |
631d5788 | 11220 | go_error_at(this->start_->location(), "string index out of bounds"); |
e440a328 | 11221 | this->set_is_error(); |
11222 | } | |
11223 | } | |
11224 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
11225 | { | |
0c77715b | 11226 | Numeric_constant enc; |
11227 | mpz_t eval; | |
11228 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 11229 | { |
0c77715b | 11230 | if (mpz_sgn(eval) < 0 |
11231 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 11232 | { |
631d5788 | 11233 | go_error_at(this->end_->location(), "string index out of bounds"); |
e440a328 | 11234 | this->set_is_error(); |
11235 | } | |
0bd5d859 | 11236 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
11237 | this->report_error(_("inverted slice range")); | |
0c77715b | 11238 | mpz_clear(eval); |
e440a328 | 11239 | } |
11240 | } | |
0bd5d859 | 11241 | if (ival_valid) |
11242 | mpz_clear(ival); | |
e440a328 | 11243 | } |
11244 | ||
ea664253 | 11245 | // Get the backend representation for a string index. |
e440a328 | 11246 | |
ea664253 | 11247 | Bexpression* |
11248 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11249 | { |
b13c66cd | 11250 | Location loc = this->location(); |
2c809f8f | 11251 | Expression* string_arg = this->string_; |
11252 | if (this->string_->type()->points_to() != NULL) | |
f614ea8b | 11253 | string_arg = Expression::make_dereference(this->string_, |
11254 | NIL_CHECK_NOT_NEEDED, loc); | |
e440a328 | 11255 | |
2c809f8f | 11256 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 11257 | |
2c809f8f | 11258 | int code = (this->end_ == NULL |
11259 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
11260 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 11261 | |
2c809f8f | 11262 | Gogo* gogo = context->gogo(); |
ea664253 | 11263 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 11264 | |
11265 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 11266 | |
2c809f8f | 11267 | // It is possible that an error occurred earlier because the start index |
11268 | // cannot be represented as an integer type. In this case, we shouldn't | |
11269 | // try casting the starting index into an integer since | |
11270 | // Type_conversion_expression will fail to get the backend representation. | |
11271 | // FIXME. | |
11272 | if (this->start_->type()->integer_type() == NULL | |
11273 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
11274 | { | |
11275 | go_assert(saw_errors()); | |
ea664253 | 11276 | return context->backend()->error_expression(); |
2c809f8f | 11277 | } |
e440a328 | 11278 | |
2c809f8f | 11279 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
93715b75 | 11280 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 11281 | |
2c809f8f | 11282 | if (this->end_ == NULL) |
11283 | { | |
11284 | Expression* length = | |
11285 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 11286 | |
2c809f8f | 11287 | Expression* start_too_large = |
11288 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
11289 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
11290 | bad_index, loc); | |
11291 | Expression* bytes = | |
11292 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 11293 | |
ea664253 | 11294 | Bexpression* bstart = start->get_backend(context); |
11295 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 11296 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 11297 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
11298 | Bexpression* index = | |
11299 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 11300 | |
2c809f8f | 11301 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 11302 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11303 | return gogo->backend()->conditional_expression(bfn, byte_btype, |
11304 | index_error, crash, | |
11305 | index, loc); | |
2c809f8f | 11306 | } |
11307 | ||
11308 | Expression* end = NULL; | |
11309 | if (this->end_->is_nil_expression()) | |
e67508fa | 11310 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 11311 | else |
11312 | { | |
2c809f8f | 11313 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
11314 | bad_index = | |
11315 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
11316 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 11317 | } |
2c809f8f | 11318 | |
11319 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
11320 | string_arg, start, end); | |
ea664253 | 11321 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 11322 | |
11323 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 11324 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11325 | return gogo->backend()->conditional_expression(bfn, str_btype, index_error, |
ea664253 | 11326 | crash, bstrslice, loc); |
e440a328 | 11327 | } |
11328 | ||
d751bb78 | 11329 | // Dump ast representation for a string index expression. |
11330 | ||
11331 | void | |
11332 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11333 | const | |
11334 | { | |
acf2b673 | 11335 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
11336 | this->start_, this->end_, NULL); | |
d751bb78 | 11337 | } |
11338 | ||
e440a328 | 11339 | // Make a string index expression. END may be NULL. |
11340 | ||
11341 | Expression* | |
11342 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 11343 | Expression* end, Location location) |
e440a328 | 11344 | { |
11345 | return new String_index_expression(string, start, end, location); | |
11346 | } | |
11347 | ||
11348 | // Class Map_index. | |
11349 | ||
11350 | // Get the type of the map. | |
11351 | ||
11352 | Map_type* | |
11353 | Map_index_expression::get_map_type() const | |
11354 | { | |
0d5530d9 | 11355 | Map_type* mt = this->map_->type()->map_type(); |
c7524fae | 11356 | if (mt == NULL) |
c484d925 | 11357 | go_assert(saw_errors()); |
e440a328 | 11358 | return mt; |
11359 | } | |
11360 | ||
11361 | // Map index traversal. | |
11362 | ||
11363 | int | |
11364 | Map_index_expression::do_traverse(Traverse* traverse) | |
11365 | { | |
11366 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11367 | return TRAVERSE_EXIT; | |
11368 | return Expression::traverse(&this->index_, traverse); | |
11369 | } | |
11370 | ||
2c809f8f | 11371 | // We need to pass in a pointer to the key, so flatten the index into a |
11372 | // temporary variable if it isn't already. The value pointer will be | |
11373 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
11374 | // recomputation. | |
11375 | ||
11376 | Expression* | |
91c0fd76 | 11377 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 11378 | Statement_inserter* inserter) |
11379 | { | |
91c0fd76 | 11380 | Location loc = this->location(); |
2c809f8f | 11381 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 11382 | if (this->index()->is_error_expression() |
11383 | || this->index()->type()->is_error_type() | |
11384 | || mt->is_error_type()) | |
11385 | { | |
11386 | go_assert(saw_errors()); | |
11387 | return Expression::make_error(loc); | |
11388 | } | |
11389 | ||
91c0fd76 | 11390 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
11391 | { | |
11392 | if (this->index_->type()->interface_type() != NULL | |
11393 | && !this->index_->is_variable()) | |
11394 | { | |
11395 | Temporary_statement* temp = | |
11396 | Statement::make_temporary(NULL, this->index_, loc); | |
11397 | inserter->insert(temp); | |
11398 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
11399 | } | |
11400 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
11401 | this->index_, loc); | |
11402 | } | |
2c809f8f | 11403 | |
11404 | if (!this->index_->is_variable()) | |
11405 | { | |
11406 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 11407 | loc); |
2c809f8f | 11408 | inserter->insert(temp); |
91c0fd76 | 11409 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11410 | } |
11411 | ||
11412 | if (this->value_pointer_ == NULL) | |
0d5530d9 | 11413 | this->get_value_pointer(gogo); |
5bf8be8b | 11414 | if (this->value_pointer_->is_error_expression() |
11415 | || this->value_pointer_->type()->is_error_type()) | |
11416 | return Expression::make_error(loc); | |
2c809f8f | 11417 | if (!this->value_pointer_->is_variable()) |
11418 | { | |
11419 | Temporary_statement* temp = | |
91c0fd76 | 11420 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 11421 | inserter->insert(temp); |
91c0fd76 | 11422 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11423 | } |
11424 | ||
11425 | return this; | |
11426 | } | |
11427 | ||
e440a328 | 11428 | // Return the type of a map index. |
11429 | ||
11430 | Type* | |
11431 | Map_index_expression::do_type() | |
11432 | { | |
c7524fae | 11433 | Map_type* mt = this->get_map_type(); |
11434 | if (mt == NULL) | |
11435 | return Type::make_error_type(); | |
0d5530d9 | 11436 | return mt->val_type(); |
e440a328 | 11437 | } |
11438 | ||
11439 | // Fix the type of a map index. | |
11440 | ||
11441 | void | |
11442 | Map_index_expression::do_determine_type(const Type_context*) | |
11443 | { | |
11444 | this->map_->determine_type_no_context(); | |
c7524fae | 11445 | Map_type* mt = this->get_map_type(); |
11446 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11447 | Type_context subcontext(key_type, false); | |
e440a328 | 11448 | this->index_->determine_type(&subcontext); |
11449 | } | |
11450 | ||
11451 | // Check types of a map index. | |
11452 | ||
11453 | void | |
11454 | Map_index_expression::do_check_types(Gogo*) | |
11455 | { | |
11456 | std::string reason; | |
c7524fae | 11457 | Map_type* mt = this->get_map_type(); |
11458 | if (mt == NULL) | |
11459 | return; | |
11460 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11461 | { |
11462 | if (reason.empty()) | |
11463 | this->report_error(_("incompatible type for map index")); | |
11464 | else | |
11465 | { | |
631d5788 | 11466 | go_error_at(this->location(), "incompatible type for map index (%s)", |
11467 | reason.c_str()); | |
e440a328 | 11468 | this->set_is_error(); |
11469 | } | |
11470 | } | |
11471 | } | |
11472 | ||
ea664253 | 11473 | // Get the backend representation for a map index. |
e440a328 | 11474 | |
ea664253 | 11475 | Bexpression* |
11476 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11477 | { |
11478 | Map_type* type = this->get_map_type(); | |
c7524fae | 11479 | if (type == NULL) |
2c809f8f | 11480 | { |
11481 | go_assert(saw_errors()); | |
ea664253 | 11482 | return context->backend()->error_expression(); |
2c809f8f | 11483 | } |
e440a328 | 11484 | |
2c809f8f | 11485 | go_assert(this->value_pointer_ != NULL |
11486 | && this->value_pointer_->is_variable()); | |
e440a328 | 11487 | |
f614ea8b | 11488 | Expression* val = Expression::make_dereference(this->value_pointer_, |
11489 | NIL_CHECK_NOT_NEEDED, | |
11490 | this->location()); | |
0d5530d9 | 11491 | return val->get_backend(context); |
e440a328 | 11492 | } |
11493 | ||
0d5530d9 | 11494 | // Get an expression for the map index. This returns an expression |
11495 | // that evaluates to a pointer to a value. If the key is not in the | |
11496 | // map, the pointer will point to a zero value. | |
e440a328 | 11497 | |
2c809f8f | 11498 | Expression* |
0d5530d9 | 11499 | Map_index_expression::get_value_pointer(Gogo* gogo) |
e440a328 | 11500 | { |
2c809f8f | 11501 | if (this->value_pointer_ == NULL) |
746d2e73 | 11502 | { |
2c809f8f | 11503 | Map_type* type = this->get_map_type(); |
11504 | if (type == NULL) | |
746d2e73 | 11505 | { |
2c809f8f | 11506 | go_assert(saw_errors()); |
11507 | return Expression::make_error(this->location()); | |
746d2e73 | 11508 | } |
e440a328 | 11509 | |
2c809f8f | 11510 | Location loc = this->location(); |
11511 | Expression* map_ref = this->map_; | |
e440a328 | 11512 | |
0d5530d9 | 11513 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, |
11514 | this->index_, | |
2c809f8f | 11515 | loc); |
0d5530d9 | 11516 | |
11517 | Expression* zero = type->fat_zero_value(gogo); | |
11518 | ||
11519 | Expression* map_index; | |
11520 | ||
11521 | if (zero == NULL) | |
11522 | map_index = | |
11523 | Runtime::make_call(Runtime::MAPACCESS1, loc, 3, | |
11524 | Expression::make_type_descriptor(type, loc), | |
11525 | map_ref, index_ptr); | |
11526 | else | |
11527 | map_index = | |
11528 | Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, | |
11529 | Expression::make_type_descriptor(type, loc), | |
11530 | map_ref, index_ptr, zero); | |
2c809f8f | 11531 | |
11532 | Type* val_type = type->val_type(); | |
11533 | this->value_pointer_ = | |
11534 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
11535 | map_index, this->location()); | |
11536 | } | |
0d5530d9 | 11537 | |
2c809f8f | 11538 | return this->value_pointer_; |
e440a328 | 11539 | } |
11540 | ||
d751bb78 | 11541 | // Dump ast representation for a map index expression |
11542 | ||
11543 | void | |
11544 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11545 | const | |
11546 | { | |
acf2b673 | 11547 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
11548 | this->index_, NULL, NULL); | |
d751bb78 | 11549 | } |
11550 | ||
e440a328 | 11551 | // Make a map index expression. |
11552 | ||
11553 | Map_index_expression* | |
11554 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11555 | Location location) |
e440a328 | 11556 | { |
11557 | return new Map_index_expression(map, index, location); | |
11558 | } | |
11559 | ||
11560 | // Class Field_reference_expression. | |
11561 | ||
149eabc5 | 11562 | // Lower a field reference expression. There is nothing to lower, but |
11563 | // this is where we generate the tracking information for fields with | |
11564 | // the magic go:"track" tag. | |
11565 | ||
11566 | Expression* | |
11567 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11568 | Statement_inserter* inserter, int) | |
11569 | { | |
11570 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11571 | if (struct_type == NULL) | |
11572 | { | |
11573 | // Error will be reported elsewhere. | |
11574 | return this; | |
11575 | } | |
11576 | const Struct_field* field = struct_type->field(this->field_index_); | |
11577 | if (field == NULL) | |
11578 | return this; | |
11579 | if (!field->has_tag()) | |
11580 | return this; | |
11581 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11582 | return this; | |
11583 | ||
604e278d | 11584 | // References from functions generated by the compiler don't count. |
c6292d1d | 11585 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 11586 | return this; |
11587 | ||
149eabc5 | 11588 | // We have found a reference to a tracked field. Build a call to |
11589 | // the runtime function __go_fieldtrack with a string that describes | |
11590 | // the field. FIXME: We should only call this once per referenced | |
11591 | // field per function, not once for each reference to the field. | |
11592 | ||
11593 | if (this->called_fieldtrack_) | |
11594 | return this; | |
11595 | this->called_fieldtrack_ = true; | |
11596 | ||
11597 | Location loc = this->location(); | |
11598 | ||
11599 | std::string s = "fieldtrack \""; | |
11600 | Named_type* nt = this->expr_->type()->named_type(); | |
11601 | if (nt == NULL || nt->named_object()->package() == NULL) | |
11602 | s.append(gogo->pkgpath()); | |
11603 | else | |
11604 | s.append(nt->named_object()->package()->pkgpath()); | |
11605 | s.push_back('.'); | |
11606 | if (nt != NULL) | |
5c29ad36 | 11607 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11608 | s.push_back('.'); |
11609 | s.append(field->field_name()); | |
11610 | s.push_back('"'); | |
11611 | ||
11612 | // We can't use a string here, because internally a string holds a | |
11613 | // pointer to the actual bytes; when the linker garbage collects the | |
11614 | // string, it won't garbage collect the bytes. So we use a | |
11615 | // [...]byte. | |
11616 | ||
e67508fa | 11617 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 11618 | |
11619 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
6bf4793c | 11620 | Array_type* array_type = Type::make_array_type(byte_type, length_expr); |
11621 | array_type->set_is_array_incomparable(); | |
149eabc5 | 11622 | |
11623 | Expression_list* bytes = new Expression_list(); | |
11624 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11625 | { | |
e67508fa | 11626 | unsigned char c = static_cast<unsigned char>(*p); |
11627 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 11628 | } |
11629 | ||
11630 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11631 | bytes, false, loc); |
149eabc5 | 11632 | |
11633 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11634 | ||
11635 | static int count; | |
11636 | char buf[50]; | |
11637 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11638 | ++count; | |
11639 | ||
11640 | Named_object* no = gogo->add_variable(buf, var); | |
11641 | e = Expression::make_var_reference(no, loc); | |
11642 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11643 | ||
11644 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 11645 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 11646 | inserter->insert(Statement::make_statement(call, false)); |
11647 | ||
11648 | // Put this function, and the global variable we just created, into | |
11649 | // unique sections. This will permit the linker to garbage collect | |
11650 | // them if they are not referenced. The effect is that the only | |
11651 | // strings, indicating field references, that will wind up in the | |
11652 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11653 | if (function != NULL) |
11654 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11655 | var->set_in_unique_section(); |
11656 | ||
11657 | return this; | |
11658 | } | |
11659 | ||
e440a328 | 11660 | // Return the type of a field reference. |
11661 | ||
11662 | Type* | |
11663 | Field_reference_expression::do_type() | |
11664 | { | |
b0e628fb | 11665 | Type* type = this->expr_->type(); |
5c13bd80 | 11666 | if (type->is_error()) |
b0e628fb | 11667 | return type; |
11668 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11669 | go_assert(struct_type != NULL); |
e440a328 | 11670 | return struct_type->field(this->field_index_)->type(); |
11671 | } | |
11672 | ||
11673 | // Check the types for a field reference. | |
11674 | ||
11675 | void | |
11676 | Field_reference_expression::do_check_types(Gogo*) | |
11677 | { | |
b0e628fb | 11678 | Type* type = this->expr_->type(); |
5c13bd80 | 11679 | if (type->is_error()) |
b0e628fb | 11680 | return; |
11681 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11682 | go_assert(struct_type != NULL); |
11683 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11684 | } |
11685 | ||
ea664253 | 11686 | // Get the backend representation for a field reference. |
e440a328 | 11687 | |
ea664253 | 11688 | Bexpression* |
11689 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11690 | { |
ea664253 | 11691 | Bexpression* bstruct = this->expr_->get_backend(context); |
11692 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11693 | this->field_index_, | |
11694 | this->location()); | |
e440a328 | 11695 | } |
11696 | ||
d751bb78 | 11697 | // Dump ast representation for a field reference expression. |
11698 | ||
11699 | void | |
11700 | Field_reference_expression::do_dump_expression( | |
11701 | Ast_dump_context* ast_dump_context) const | |
11702 | { | |
11703 | this->expr_->dump_expression(ast_dump_context); | |
11704 | ast_dump_context->ostream() << "." << this->field_index_; | |
11705 | } | |
11706 | ||
e440a328 | 11707 | // Make a reference to a qualified identifier in an expression. |
11708 | ||
11709 | Field_reference_expression* | |
11710 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11711 | Location location) |
e440a328 | 11712 | { |
11713 | return new Field_reference_expression(expr, field_index, location); | |
11714 | } | |
11715 | ||
11716 | // Class Interface_field_reference_expression. | |
11717 | ||
2387f644 | 11718 | // Return an expression for the pointer to the function to call. |
e440a328 | 11719 | |
2387f644 | 11720 | Expression* |
11721 | Interface_field_reference_expression::get_function() | |
e440a328 | 11722 | { |
2387f644 | 11723 | Expression* ref = this->expr_; |
11724 | Location loc = this->location(); | |
11725 | if (ref->type()->points_to() != NULL) | |
f614ea8b | 11726 | ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc); |
e440a328 | 11727 | |
2387f644 | 11728 | Expression* mtable = |
11729 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11730 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11731 | |
11732 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11733 | unsigned int index; |
11734 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11735 | go_assert(field != NULL); | |
f614ea8b | 11736 | |
11737 | mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc); | |
2387f644 | 11738 | return Expression::make_field_reference(mtable, index, loc); |
e440a328 | 11739 | } |
11740 | ||
2387f644 | 11741 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11742 | // function. |
11743 | ||
2387f644 | 11744 | Expression* |
11745 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11746 | { |
2387f644 | 11747 | Expression* expr = this->expr_; |
11748 | if (expr->type()->points_to() != NULL) | |
f614ea8b | 11749 | expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT, |
11750 | this->location()); | |
2387f644 | 11751 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, |
11752 | this->location()); | |
e440a328 | 11753 | } |
11754 | ||
11755 | // Traversal. | |
11756 | ||
11757 | int | |
11758 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11759 | { | |
11760 | return Expression::traverse(&this->expr_, traverse); | |
11761 | } | |
11762 | ||
0afbb937 | 11763 | // Lower the expression. If this expression is not called, we need to |
11764 | // evaluate the expression twice when converting to the backend | |
11765 | // interface. So introduce a temporary variable if necessary. | |
11766 | ||
11767 | Expression* | |
9782d556 | 11768 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11769 | Statement_inserter* inserter) | |
0afbb937 | 11770 | { |
5bf8be8b | 11771 | if (this->expr_->is_error_expression() |
11772 | || this->expr_->type()->is_error_type()) | |
11773 | { | |
11774 | go_assert(saw_errors()); | |
11775 | return Expression::make_error(this->location()); | |
11776 | } | |
11777 | ||
2387f644 | 11778 | if (!this->expr_->is_variable()) |
0afbb937 | 11779 | { |
11780 | Temporary_statement* temp = | |
11781 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11782 | inserter->insert(temp); | |
11783 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11784 | this->location()); | |
11785 | } | |
11786 | return this; | |
11787 | } | |
11788 | ||
e440a328 | 11789 | // Return the type of an interface field reference. |
11790 | ||
11791 | Type* | |
11792 | Interface_field_reference_expression::do_type() | |
11793 | { | |
11794 | Type* expr_type = this->expr_->type(); | |
11795 | ||
11796 | Type* points_to = expr_type->points_to(); | |
11797 | if (points_to != NULL) | |
11798 | expr_type = points_to; | |
11799 | ||
11800 | Interface_type* interface_type = expr_type->interface_type(); | |
11801 | if (interface_type == NULL) | |
11802 | return Type::make_error_type(); | |
11803 | ||
11804 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11805 | if (method == NULL) | |
11806 | return Type::make_error_type(); | |
11807 | ||
11808 | return method->type(); | |
11809 | } | |
11810 | ||
11811 | // Determine types. | |
11812 | ||
11813 | void | |
11814 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11815 | { | |
11816 | this->expr_->determine_type_no_context(); | |
11817 | } | |
11818 | ||
11819 | // Check the types for an interface field reference. | |
11820 | ||
11821 | void | |
11822 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11823 | { | |
11824 | Type* type = this->expr_->type(); | |
11825 | ||
11826 | Type* points_to = type->points_to(); | |
11827 | if (points_to != NULL) | |
11828 | type = points_to; | |
11829 | ||
11830 | Interface_type* interface_type = type->interface_type(); | |
11831 | if (interface_type == NULL) | |
5c491127 | 11832 | { |
11833 | if (!type->is_error_type()) | |
11834 | this->report_error(_("expected interface or pointer to interface")); | |
11835 | } | |
e440a328 | 11836 | else |
11837 | { | |
11838 | const Typed_identifier* method = | |
11839 | interface_type->find_method(this->name_); | |
11840 | if (method == NULL) | |
11841 | { | |
631d5788 | 11842 | go_error_at(this->location(), "method %qs not in interface", |
11843 | Gogo::message_name(this->name_).c_str()); | |
e440a328 | 11844 | this->set_is_error(); |
11845 | } | |
11846 | } | |
11847 | } | |
11848 | ||
0afbb937 | 11849 | // If an interface field reference is not simply called, then it is |
11850 | // represented as a closure. The closure will hold a single variable, | |
11851 | // the value of the interface on which the method should be called. | |
11852 | // The function will be a simple thunk that pulls the value from the | |
11853 | // closure and calls the method with the remaining arguments. | |
11854 | ||
11855 | // Because method values are not common, we don't build all thunks for | |
11856 | // all possible interface methods, but instead only build them as we | |
11857 | // need them. In particular, we even build them on demand for | |
11858 | // interface methods defined in other packages. | |
11859 | ||
11860 | Interface_field_reference_expression::Interface_method_thunks | |
11861 | Interface_field_reference_expression::interface_method_thunks; | |
11862 | ||
11863 | // Find or create the thunk to call method NAME on TYPE. | |
11864 | ||
11865 | Named_object* | |
11866 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11867 | Interface_type* type, | |
11868 | const std::string& name) | |
11869 | { | |
11870 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11871 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11872 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11873 | if (ins.second) | |
11874 | { | |
11875 | // This is the first time we have seen this interface. | |
11876 | ins.first->second = new Method_thunks(); | |
11877 | } | |
11878 | ||
11879 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11880 | p != ins.first->second->end(); | |
11881 | p++) | |
11882 | if (p->first == name) | |
11883 | return p->second; | |
11884 | ||
11885 | Location loc = type->location(); | |
11886 | ||
11887 | const Typed_identifier* method_id = type->find_method(name); | |
11888 | if (method_id == NULL) | |
11889 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11890 | ||
11891 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11892 | if (orig_fntype == NULL) | |
11893 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11894 | ||
11895 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11896 | // The type here is wrong--it should be the C function type. But it |
11897 | // doesn't really matter. | |
0afbb937 | 11898 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11899 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11900 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
6bf4793c | 11901 | Struct_type* st = Type::make_struct_type(sfl, loc); |
11902 | st->set_is_struct_incomparable(); | |
11903 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 11904 | |
f8bdf81a | 11905 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11906 | |
da244e59 | 11907 | std::string thunk_name = Gogo::thunk_name(); |
11908 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, | |
0afbb937 | 11909 | false, loc); |
11910 | ||
f8bdf81a | 11911 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11912 | cvar->set_is_used(); | |
1ecc6157 | 11913 | cvar->set_is_closure(); |
da244e59 | 11914 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
11915 | NULL, cvar); | |
f8bdf81a | 11916 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 11917 | |
f8bdf81a | 11918 | gogo->start_block(loc); |
0afbb937 | 11919 | |
11920 | // Field 0 of the closure is the function code pointer, field 1 is | |
11921 | // the value on which to invoke the method. | |
11922 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 11923 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 11924 | arg = Expression::make_field_reference(arg, 1, loc); |
11925 | ||
11926 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11927 | loc); | |
11928 | ||
11929 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11930 | Expression_list* args; | |
11931 | if (orig_params == NULL || orig_params->empty()) | |
11932 | args = NULL; | |
11933 | else | |
11934 | { | |
11935 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11936 | args = new Expression_list(); | |
11937 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11938 | p != new_params->end(); |
0afbb937 | 11939 | ++p) |
11940 | { | |
11941 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11942 | go_assert(p_no != NULL | |
11943 | && p_no->is_variable() | |
11944 | && p_no->var_value()->is_parameter()); | |
11945 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11946 | } | |
11947 | } | |
11948 | ||
11949 | Call_expression* call = Expression::make_call(ifre, args, | |
11950 | orig_fntype->is_varargs(), | |
11951 | loc); | |
11952 | call->set_varargs_are_lowered(); | |
11953 | ||
11954 | Statement* s = Statement::make_return_from_call(call, loc); | |
11955 | gogo->add_statement(s); | |
11956 | Block* b = gogo->finish_block(loc); | |
11957 | gogo->add_block(b, loc); | |
11958 | gogo->lower_block(new_no, b); | |
a32698ee | 11959 | gogo->flatten_block(new_no, b); |
0afbb937 | 11960 | gogo->finish_function(loc); |
11961 | ||
11962 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11963 | return new_no; | |
11964 | } | |
11965 | ||
ea664253 | 11966 | // Get the backend representation for a method value. |
e440a328 | 11967 | |
ea664253 | 11968 | Bexpression* |
11969 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11970 | { |
0afbb937 | 11971 | Interface_type* type = this->expr_->type()->interface_type(); |
11972 | if (type == NULL) | |
11973 | { | |
11974 | go_assert(saw_errors()); | |
ea664253 | 11975 | return context->backend()->error_expression(); |
0afbb937 | 11976 | } |
11977 | ||
11978 | Named_object* thunk = | |
11979 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11980 | type, this->name_); | |
11981 | if (thunk->is_erroneous()) | |
11982 | { | |
11983 | go_assert(saw_errors()); | |
ea664253 | 11984 | return context->backend()->error_expression(); |
0afbb937 | 11985 | } |
11986 | ||
11987 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11988 | // the lowering pass because at that point we don't know whether we | |
11989 | // need to create the thunk or not. If the expression is called, we | |
11990 | // don't need the thunk. | |
11991 | ||
11992 | Location loc = this->location(); | |
11993 | ||
11994 | Struct_field_list* fields = new Struct_field_list(); | |
11995 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11996 | thunk->func_value()->type(), | |
11997 | loc))); | |
11998 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11999 | this->expr_->type(), | |
12000 | loc))); | |
12001 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6bf4793c | 12002 | st->set_is_struct_incomparable(); |
0afbb937 | 12003 | |
12004 | Expression_list* vals = new Expression_list(); | |
12005 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
12006 | vals->push_back(this->expr_); | |
12007 | ||
12008 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 12009 | Bexpression* bclosure = |
12010 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 12011 | |
4df0c2d4 | 12012 | Gogo* gogo = context->gogo(); |
12013 | Btype* btype = this->type()->get_backend(gogo); | |
12014 | bclosure = gogo->backend()->convert_expression(btype, bclosure, loc); | |
12015 | ||
2387f644 | 12016 | Expression* nil_check = |
12017 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
12018 | Expression::make_nil(loc), loc); | |
ea664253 | 12019 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 12020 | |
ea664253 | 12021 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
12022 | loc)->get_backend(context); | |
2387f644 | 12023 | |
93715b75 | 12024 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
2387f644 | 12025 | Bexpression* bcond = |
93715b75 | 12026 | gogo->backend()->conditional_expression(bfn, NULL, |
12027 | bnil_check, bcrash, NULL, loc); | |
0ab48656 | 12028 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
12029 | Bstatement* cond_statement = | |
12030 | gogo->backend()->expression_statement(bfunction, bcond); | |
ea664253 | 12031 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 12032 | } |
12033 | ||
d751bb78 | 12034 | // Dump ast representation for an interface field reference. |
12035 | ||
12036 | void | |
12037 | Interface_field_reference_expression::do_dump_expression( | |
12038 | Ast_dump_context* ast_dump_context) const | |
12039 | { | |
12040 | this->expr_->dump_expression(ast_dump_context); | |
12041 | ast_dump_context->ostream() << "." << this->name_; | |
12042 | } | |
12043 | ||
e440a328 | 12044 | // Make a reference to a field in an interface. |
12045 | ||
12046 | Expression* | |
12047 | Expression::make_interface_field_reference(Expression* expr, | |
12048 | const std::string& field, | |
b13c66cd | 12049 | Location location) |
e440a328 | 12050 | { |
12051 | return new Interface_field_reference_expression(expr, field, location); | |
12052 | } | |
12053 | ||
12054 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
12055 | // is lowered after we know the type of the left hand side. | |
12056 | ||
12057 | class Selector_expression : public Parser_expression | |
12058 | { | |
12059 | public: | |
12060 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 12061 | Location location) |
e440a328 | 12062 | : Parser_expression(EXPRESSION_SELECTOR, location), |
12063 | left_(left), name_(name) | |
12064 | { } | |
12065 | ||
12066 | protected: | |
12067 | int | |
12068 | do_traverse(Traverse* traverse) | |
12069 | { return Expression::traverse(&this->left_, traverse); } | |
12070 | ||
12071 | Expression* | |
ceeb4318 | 12072 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12073 | |
12074 | Expression* | |
12075 | do_copy() | |
12076 | { | |
12077 | return new Selector_expression(this->left_->copy(), this->name_, | |
12078 | this->location()); | |
12079 | } | |
12080 | ||
d751bb78 | 12081 | void |
12082 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
12083 | ||
e440a328 | 12084 | private: |
12085 | Expression* | |
12086 | lower_method_expression(Gogo*); | |
12087 | ||
12088 | // The expression on the left hand side. | |
12089 | Expression* left_; | |
12090 | // The name on the right hand side. | |
12091 | std::string name_; | |
12092 | }; | |
12093 | ||
12094 | // Lower a selector expression once we know the real type of the left | |
12095 | // hand side. | |
12096 | ||
12097 | Expression* | |
ceeb4318 | 12098 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
12099 | int) | |
e440a328 | 12100 | { |
12101 | Expression* left = this->left_; | |
12102 | if (left->is_type_expression()) | |
12103 | return this->lower_method_expression(gogo); | |
12104 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
12105 | this->location()); | |
12106 | } | |
12107 | ||
12108 | // Lower a method expression T.M or (*T).M. We turn this into a | |
12109 | // function literal. | |
12110 | ||
12111 | Expression* | |
12112 | Selector_expression::lower_method_expression(Gogo* gogo) | |
12113 | { | |
b13c66cd | 12114 | Location location = this->location(); |
868b439e | 12115 | Type* left_type = this->left_->type(); |
12116 | Type* type = left_type; | |
e440a328 | 12117 | const std::string& name(this->name_); |
12118 | ||
12119 | bool is_pointer; | |
12120 | if (type->points_to() == NULL) | |
12121 | is_pointer = false; | |
12122 | else | |
12123 | { | |
12124 | is_pointer = true; | |
12125 | type = type->points_to(); | |
12126 | } | |
12127 | Named_type* nt = type->named_type(); | |
12128 | if (nt == NULL) | |
12129 | { | |
631d5788 | 12130 | go_error_at(location, |
12131 | ("method expression requires named type or " | |
12132 | "pointer to named type")); | |
e440a328 | 12133 | return Expression::make_error(location); |
12134 | } | |
12135 | ||
12136 | bool is_ambiguous; | |
12137 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 12138 | const Typed_identifier* imethod = NULL; |
dcc8506b | 12139 | if (method == NULL && !is_pointer) |
ab1468c3 | 12140 | { |
12141 | Interface_type* it = nt->interface_type(); | |
12142 | if (it != NULL) | |
12143 | imethod = it->find_method(name); | |
12144 | } | |
12145 | ||
868b439e | 12146 | if ((method == NULL && imethod == NULL) |
12147 | || (left_type->named_type() != NULL && left_type->points_to() != NULL)) | |
e440a328 | 12148 | { |
12149 | if (!is_ambiguous) | |
631d5788 | 12150 | go_error_at(location, "type %<%s%s%> has no method %<%s%>", |
12151 | is_pointer ? "*" : "", | |
12152 | nt->message_name().c_str(), | |
12153 | Gogo::message_name(name).c_str()); | |
e440a328 | 12154 | else |
631d5788 | 12155 | go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
12156 | Gogo::message_name(name).c_str(), | |
12157 | is_pointer ? "*" : "", | |
12158 | nt->message_name().c_str()); | |
e440a328 | 12159 | return Expression::make_error(location); |
12160 | } | |
12161 | ||
ab1468c3 | 12162 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 12163 | { |
631d5788 | 12164 | go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", |
12165 | nt->message_name().c_str(), | |
12166 | Gogo::message_name(name).c_str()); | |
e440a328 | 12167 | return Expression::make_error(location); |
12168 | } | |
12169 | ||
12170 | // Build a new function type in which the receiver becomes the first | |
12171 | // argument. | |
ab1468c3 | 12172 | Function_type* method_type; |
12173 | if (method != NULL) | |
12174 | { | |
12175 | method_type = method->type(); | |
c484d925 | 12176 | go_assert(method_type->is_method()); |
ab1468c3 | 12177 | } |
12178 | else | |
12179 | { | |
12180 | method_type = imethod->type()->function_type(); | |
c484d925 | 12181 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 12182 | } |
e440a328 | 12183 | |
12184 | const char* const receiver_name = "$this"; | |
12185 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
12186 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
12187 | location)); | |
12188 | ||
12189 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
12190 | if (method_parameters != NULL) | |
12191 | { | |
f470da59 | 12192 | int i = 0; |
e440a328 | 12193 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
12194 | p != method_parameters->end(); | |
f470da59 | 12195 | ++p, ++i) |
12196 | { | |
68883531 | 12197 | if (!p->name().empty()) |
f470da59 | 12198 | parameters->push_back(*p); |
12199 | else | |
12200 | { | |
12201 | char buf[20]; | |
12202 | snprintf(buf, sizeof buf, "$param%d", i); | |
12203 | parameters->push_back(Typed_identifier(buf, p->type(), | |
12204 | p->location())); | |
12205 | } | |
12206 | } | |
e440a328 | 12207 | } |
12208 | ||
12209 | const Typed_identifier_list* method_results = method_type->results(); | |
12210 | Typed_identifier_list* results; | |
12211 | if (method_results == NULL) | |
12212 | results = NULL; | |
12213 | else | |
12214 | { | |
12215 | results = new Typed_identifier_list(); | |
12216 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
12217 | p != method_results->end(); | |
12218 | ++p) | |
12219 | results->push_back(*p); | |
12220 | } | |
12221 | ||
12222 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
12223 | location); | |
12224 | if (method_type->is_varargs()) | |
12225 | fntype->set_is_varargs(); | |
12226 | ||
12227 | // We generate methods which always takes a pointer to the receiver | |
12228 | // as their first argument. If this is for a pointer type, we can | |
12229 | // simply reuse the existing function. We use an internal hack to | |
12230 | // get the right type. | |
8381eda7 | 12231 | // FIXME: This optimization is disabled because it doesn't yet work |
12232 | // with function descriptors when the method expression is not | |
12233 | // directly called. | |
12234 | if (method != NULL && is_pointer && false) | |
e440a328 | 12235 | { |
12236 | Named_object* mno = (method->needs_stub_method() | |
12237 | ? method->stub_object() | |
12238 | : method->named_object()); | |
12239 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
12240 | f = Expression::make_cast(fntype, f, location); | |
12241 | Type_conversion_expression* tce = | |
12242 | static_cast<Type_conversion_expression*>(f); | |
12243 | tce->set_may_convert_function_types(); | |
12244 | return f; | |
12245 | } | |
12246 | ||
12247 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
12248 | location); | |
12249 | ||
12250 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 12251 | go_assert(vno != NULL); |
e440a328 | 12252 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 12253 | Expression* bm; |
12254 | if (method != NULL) | |
12255 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
12256 | else | |
12257 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 12258 | |
12259 | // Even though we found the method above, if it has an error type we | |
12260 | // may see an error here. | |
12261 | if (bm->is_error_expression()) | |
463fe805 | 12262 | { |
12263 | gogo->finish_function(location); | |
12264 | return bm; | |
12265 | } | |
e440a328 | 12266 | |
12267 | Expression_list* args; | |
f470da59 | 12268 | if (parameters->size() <= 1) |
e440a328 | 12269 | args = NULL; |
12270 | else | |
12271 | { | |
12272 | args = new Expression_list(); | |
f470da59 | 12273 | Typed_identifier_list::const_iterator p = parameters->begin(); |
12274 | ++p; | |
12275 | for (; p != parameters->end(); ++p) | |
e440a328 | 12276 | { |
12277 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 12278 | go_assert(vno != NULL); |
e440a328 | 12279 | args->push_back(Expression::make_var_reference(vno, location)); |
12280 | } | |
12281 | } | |
12282 | ||
ceeb4318 | 12283 | gogo->start_block(location); |
12284 | ||
e440a328 | 12285 | Call_expression* call = Expression::make_call(bm, args, |
12286 | method_type->is_varargs(), | |
12287 | location); | |
12288 | ||
0afbb937 | 12289 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 12290 | gogo->add_statement(s); |
12291 | ||
ceeb4318 | 12292 | Block* b = gogo->finish_block(location); |
12293 | ||
12294 | gogo->add_block(b, location); | |
12295 | ||
12296 | // Lower the call in case there are multiple results. | |
12297 | gogo->lower_block(no, b); | |
a32698ee | 12298 | gogo->flatten_block(no, b); |
ceeb4318 | 12299 | |
e440a328 | 12300 | gogo->finish_function(location); |
12301 | ||
12302 | return Expression::make_func_reference(no, NULL, location); | |
12303 | } | |
12304 | ||
d751bb78 | 12305 | // Dump the ast for a selector expression. |
12306 | ||
12307 | void | |
12308 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12309 | const | |
12310 | { | |
12311 | ast_dump_context->dump_expression(this->left_); | |
12312 | ast_dump_context->ostream() << "."; | |
12313 | ast_dump_context->ostream() << this->name_; | |
12314 | } | |
12315 | ||
e440a328 | 12316 | // Make a selector expression. |
12317 | ||
12318 | Expression* | |
12319 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 12320 | Location location) |
e440a328 | 12321 | { |
12322 | return new Selector_expression(left, name, location); | |
12323 | } | |
12324 | ||
da244e59 | 12325 | // Class Allocation_expression. |
e440a328 | 12326 | |
da244e59 | 12327 | int |
12328 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 12329 | { |
da244e59 | 12330 | return Type::traverse(this->type_, traverse); |
12331 | } | |
e440a328 | 12332 | |
da244e59 | 12333 | Type* |
12334 | Allocation_expression::do_type() | |
12335 | { | |
12336 | return Type::make_pointer_type(this->type_); | |
12337 | } | |
e440a328 | 12338 | |
22deed0d | 12339 | void |
12340 | Allocation_expression::do_check_types(Gogo*) | |
12341 | { | |
12342 | if (!this->type_->in_heap()) | |
12343 | go_error_at(this->location(), "can't heap allocate go:notinheap type"); | |
12344 | } | |
12345 | ||
da244e59 | 12346 | // Make a copy of an allocation expression. |
e440a328 | 12347 | |
da244e59 | 12348 | Expression* |
12349 | Allocation_expression::do_copy() | |
12350 | { | |
12351 | Allocation_expression* alloc = | |
12352 | new Allocation_expression(this->type_, this->location()); | |
12353 | if (this->allocate_on_stack_) | |
12354 | alloc->set_allocate_on_stack(); | |
12355 | return alloc; | |
12356 | } | |
e440a328 | 12357 | |
ea664253 | 12358 | // Return the backend representation for an allocation expression. |
e440a328 | 12359 | |
ea664253 | 12360 | Bexpression* |
12361 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12362 | { |
2c809f8f | 12363 | Gogo* gogo = context->gogo(); |
12364 | Location loc = this->location(); | |
06e83d10 | 12365 | Btype* btype = this->type_->get_backend(gogo); |
da244e59 | 12366 | |
5973ede0 | 12367 | if (this->allocate_on_stack_) |
da244e59 | 12368 | { |
2a305b85 | 12369 | int64_t size; |
12370 | bool ok = this->type_->backend_type_size(gogo, &size); | |
12371 | if (!ok) | |
12372 | { | |
12373 | go_assert(saw_errors()); | |
12374 | return gogo->backend()->error_expression(); | |
12375 | } | |
06e83d10 | 12376 | Bstatement* decl; |
12377 | Named_object* fn = context->function(); | |
12378 | go_assert(fn != NULL); | |
12379 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
12380 | Bexpression* zero = gogo->backend()->zero_expression(btype); | |
12381 | Bvariable* temp = | |
12382 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
12383 | zero, true, loc, &decl); | |
12384 | Bexpression* ret = gogo->backend()->var_expression(temp, loc); | |
12385 | ret = gogo->backend()->address_expression(ret, loc); | |
12386 | ret = gogo->backend()->compound_expression(decl, ret, loc); | |
12387 | return ret; | |
da244e59 | 12388 | } |
12389 | ||
2a305b85 | 12390 | Bexpression* space = |
ea664253 | 12391 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 12392 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 12393 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 12394 | } |
12395 | ||
d751bb78 | 12396 | // Dump ast representation for an allocation expression. |
12397 | ||
12398 | void | |
12399 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12400 | const | |
12401 | { | |
12402 | ast_dump_context->ostream() << "new("; | |
12403 | ast_dump_context->dump_type(this->type_); | |
12404 | ast_dump_context->ostream() << ")"; | |
12405 | } | |
12406 | ||
e440a328 | 12407 | // Make an allocation expression. |
12408 | ||
12409 | Expression* | |
b13c66cd | 12410 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12411 | { |
12412 | return new Allocation_expression(type, location); | |
12413 | } | |
12414 | ||
e32de7ba | 12415 | // Class Ordered_value_list. |
e440a328 | 12416 | |
12417 | int | |
e32de7ba | 12418 | Ordered_value_list::traverse_vals(Traverse* traverse) |
e440a328 | 12419 | { |
0c4f5a19 | 12420 | if (this->vals_ != NULL) |
12421 | { | |
12422 | if (this->traverse_order_ == NULL) | |
12423 | { | |
12424 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12425 | return TRAVERSE_EXIT; | |
12426 | } | |
12427 | else | |
12428 | { | |
e32de7ba | 12429 | for (std::vector<unsigned long>::const_iterator p = |
12430 | this->traverse_order_->begin(); | |
0c4f5a19 | 12431 | p != this->traverse_order_->end(); |
12432 | ++p) | |
12433 | { | |
12434 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12435 | == TRAVERSE_EXIT) | |
12436 | return TRAVERSE_EXIT; | |
12437 | } | |
12438 | } | |
12439 | } | |
e32de7ba | 12440 | return TRAVERSE_CONTINUE; |
12441 | } | |
12442 | ||
12443 | // Class Struct_construction_expression. | |
12444 | ||
12445 | // Traversal. | |
12446 | ||
12447 | int | |
12448 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12449 | { | |
12450 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) | |
12451 | return TRAVERSE_EXIT; | |
e440a328 | 12452 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12453 | return TRAVERSE_EXIT; | |
12454 | return TRAVERSE_CONTINUE; | |
12455 | } | |
12456 | ||
12457 | // Return whether this is a constant initializer. | |
12458 | ||
12459 | bool | |
12460 | Struct_construction_expression::is_constant_struct() const | |
12461 | { | |
e32de7ba | 12462 | if (this->vals() == NULL) |
e440a328 | 12463 | return true; |
e32de7ba | 12464 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12465 | pv != this->vals()->end(); | |
e440a328 | 12466 | ++pv) |
12467 | { | |
12468 | if (*pv != NULL | |
12469 | && !(*pv)->is_constant() | |
12470 | && (!(*pv)->is_composite_literal() | |
12471 | || (*pv)->is_nonconstant_composite_literal())) | |
12472 | return false; | |
12473 | } | |
12474 | ||
12475 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12476 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12477 | pf != fields->end(); | |
12478 | ++pf) | |
12479 | { | |
12480 | // There are no constant constructors for interfaces. | |
12481 | if (pf->type()->interface_type() != NULL) | |
12482 | return false; | |
12483 | } | |
12484 | ||
12485 | return true; | |
12486 | } | |
12487 | ||
3ae06f68 | 12488 | // Return whether this struct can be used as a constant initializer. |
f9ca30f9 | 12489 | |
12490 | bool | |
3ae06f68 | 12491 | Struct_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12492 | { |
e32de7ba | 12493 | if (this->vals() == NULL) |
f9ca30f9 | 12494 | return true; |
e32de7ba | 12495 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12496 | pv != this->vals()->end(); | |
f9ca30f9 | 12497 | ++pv) |
12498 | { | |
3ae06f68 | 12499 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12500 | return false; |
12501 | } | |
de048538 | 12502 | |
12503 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12504 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12505 | pf != fields->end(); | |
12506 | ++pf) | |
12507 | { | |
12508 | // There are no constant constructors for interfaces. | |
12509 | if (pf->type()->interface_type() != NULL) | |
12510 | return false; | |
12511 | } | |
12512 | ||
f9ca30f9 | 12513 | return true; |
12514 | } | |
12515 | ||
e440a328 | 12516 | // Final type determination. |
12517 | ||
12518 | void | |
12519 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12520 | { | |
e32de7ba | 12521 | if (this->vals() == NULL) |
e440a328 | 12522 | return; |
12523 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
e32de7ba | 12524 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12525 | for (Struct_field_list::const_iterator pf = fields->begin(); |
12526 | pf != fields->end(); | |
12527 | ++pf, ++pv) | |
12528 | { | |
e32de7ba | 12529 | if (pv == this->vals()->end()) |
e440a328 | 12530 | return; |
12531 | if (*pv != NULL) | |
12532 | { | |
12533 | Type_context subcontext(pf->type(), false); | |
12534 | (*pv)->determine_type(&subcontext); | |
12535 | } | |
12536 | } | |
a6cb4c0e | 12537 | // Extra values are an error we will report elsewhere; we still want |
12538 | // to determine the type to avoid knockon errors. | |
e32de7ba | 12539 | for (; pv != this->vals()->end(); ++pv) |
a6cb4c0e | 12540 | (*pv)->determine_type_no_context(); |
e440a328 | 12541 | } |
12542 | ||
12543 | // Check types. | |
12544 | ||
12545 | void | |
12546 | Struct_construction_expression::do_check_types(Gogo*) | |
12547 | { | |
e32de7ba | 12548 | if (this->vals() == NULL) |
e440a328 | 12549 | return; |
12550 | ||
12551 | Struct_type* st = this->type_->struct_type(); | |
e32de7ba | 12552 | if (this->vals()->size() > st->field_count()) |
e440a328 | 12553 | { |
12554 | this->report_error(_("too many expressions for struct")); | |
12555 | return; | |
12556 | } | |
12557 | ||
12558 | const Struct_field_list* fields = st->fields(); | |
e32de7ba | 12559 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12560 | int i = 0; |
12561 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12562 | pf != fields->end(); | |
12563 | ++pf, ++pv, ++i) | |
12564 | { | |
e32de7ba | 12565 | if (pv == this->vals()->end()) |
e440a328 | 12566 | { |
12567 | this->report_error(_("too few expressions for struct")); | |
12568 | break; | |
12569 | } | |
12570 | ||
12571 | if (*pv == NULL) | |
12572 | continue; | |
12573 | ||
12574 | std::string reason; | |
12575 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12576 | { | |
12577 | if (reason.empty()) | |
631d5788 | 12578 | go_error_at((*pv)->location(), |
12579 | "incompatible type for field %d in struct construction", | |
12580 | i + 1); | |
e440a328 | 12581 | else |
631d5788 | 12582 | go_error_at((*pv)->location(), |
12583 | ("incompatible type for field %d in " | |
12584 | "struct construction (%s)"), | |
12585 | i + 1, reason.c_str()); | |
e440a328 | 12586 | this->set_is_error(); |
12587 | } | |
12588 | } | |
e32de7ba | 12589 | go_assert(pv == this->vals()->end()); |
e440a328 | 12590 | } |
12591 | ||
8ba8cc87 | 12592 | // Flatten a struct construction expression. Store the values into |
12593 | // temporaries in case they need interface conversion. | |
12594 | ||
12595 | Expression* | |
12596 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
12597 | Statement_inserter* inserter) | |
12598 | { | |
e32de7ba | 12599 | if (this->vals() == NULL) |
8ba8cc87 | 12600 | return this; |
12601 | ||
12602 | // If this is a constant struct, we don't need temporaries. | |
de048538 | 12603 | if (this->is_constant_struct() || this->is_static_initializer()) |
8ba8cc87 | 12604 | return this; |
12605 | ||
12606 | Location loc = this->location(); | |
e32de7ba | 12607 | for (Expression_list::iterator pv = this->vals()->begin(); |
12608 | pv != this->vals()->end(); | |
8ba8cc87 | 12609 | ++pv) |
12610 | { | |
12611 | if (*pv != NULL) | |
12612 | { | |
5bf8be8b | 12613 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12614 | { | |
12615 | go_assert(saw_errors()); | |
12616 | return Expression::make_error(loc); | |
12617 | } | |
8ba8cc87 | 12618 | if (!(*pv)->is_variable()) |
12619 | { | |
12620 | Temporary_statement* temp = | |
12621 | Statement::make_temporary(NULL, *pv, loc); | |
12622 | inserter->insert(temp); | |
12623 | *pv = Expression::make_temporary_reference(temp, loc); | |
12624 | } | |
12625 | } | |
12626 | } | |
12627 | return this; | |
12628 | } | |
12629 | ||
ea664253 | 12630 | // Return the backend representation for constructing a struct. |
e440a328 | 12631 | |
ea664253 | 12632 | Bexpression* |
12633 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12634 | { |
12635 | Gogo* gogo = context->gogo(); | |
12636 | ||
2c809f8f | 12637 | Btype* btype = this->type_->get_backend(gogo); |
e32de7ba | 12638 | if (this->vals() == NULL) |
ea664253 | 12639 | return gogo->backend()->zero_expression(btype); |
e440a328 | 12640 | |
e440a328 | 12641 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e32de7ba | 12642 | Expression_list::const_iterator pv = this->vals()->begin(); |
2c809f8f | 12643 | std::vector<Bexpression*> init; |
12644 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12645 | pf != fields->end(); | |
12646 | ++pf) | |
e440a328 | 12647 | { |
63697958 | 12648 | Btype* fbtype = pf->type()->get_backend(gogo); |
e32de7ba | 12649 | if (pv == this->vals()->end()) |
2c809f8f | 12650 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12651 | else if (*pv == NULL) |
12652 | { | |
2c809f8f | 12653 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12654 | ++pv; |
12655 | } | |
12656 | else | |
12657 | { | |
2c809f8f | 12658 | Expression* val = |
12659 | Expression::convert_for_assignment(gogo, pf->type(), | |
12660 | *pv, this->location()); | |
ea664253 | 12661 | init.push_back(val->get_backend(context)); |
e440a328 | 12662 | ++pv; |
12663 | } | |
e440a328 | 12664 | } |
ea664253 | 12665 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12666 | } |
12667 | ||
12668 | // Export a struct construction. | |
12669 | ||
12670 | void | |
12671 | Struct_construction_expression::do_export(Export* exp) const | |
12672 | { | |
12673 | exp->write_c_string("convert("); | |
12674 | exp->write_type(this->type_); | |
e32de7ba | 12675 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12676 | pv != this->vals()->end(); | |
e440a328 | 12677 | ++pv) |
12678 | { | |
12679 | exp->write_c_string(", "); | |
12680 | if (*pv != NULL) | |
12681 | (*pv)->export_expression(exp); | |
12682 | } | |
12683 | exp->write_c_string(")"); | |
12684 | } | |
12685 | ||
d751bb78 | 12686 | // Dump ast representation of a struct construction expression. |
12687 | ||
12688 | void | |
12689 | Struct_construction_expression::do_dump_expression( | |
12690 | Ast_dump_context* ast_dump_context) const | |
12691 | { | |
d751bb78 | 12692 | ast_dump_context->dump_type(this->type_); |
12693 | ast_dump_context->ostream() << "{"; | |
e32de7ba | 12694 | ast_dump_context->dump_expression_list(this->vals()); |
d751bb78 | 12695 | ast_dump_context->ostream() << "}"; |
12696 | } | |
12697 | ||
e440a328 | 12698 | // Make a struct composite literal. This used by the thunk code. |
12699 | ||
12700 | Expression* | |
12701 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12702 | Location location) |
e440a328 | 12703 | { |
c484d925 | 12704 | go_assert(type->struct_type() != NULL); |
e440a328 | 12705 | return new Struct_construction_expression(type, vals, location); |
12706 | } | |
12707 | ||
da244e59 | 12708 | // Class Array_construction_expression. |
e440a328 | 12709 | |
12710 | // Traversal. | |
12711 | ||
12712 | int | |
12713 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12714 | { | |
e32de7ba | 12715 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) |
e440a328 | 12716 | return TRAVERSE_EXIT; |
12717 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12718 | return TRAVERSE_EXIT; | |
12719 | return TRAVERSE_CONTINUE; | |
12720 | } | |
12721 | ||
12722 | // Return whether this is a constant initializer. | |
12723 | ||
12724 | bool | |
12725 | Array_construction_expression::is_constant_array() const | |
12726 | { | |
e32de7ba | 12727 | if (this->vals() == NULL) |
e440a328 | 12728 | return true; |
12729 | ||
12730 | // There are no constant constructors for interfaces. | |
12731 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12732 | return false; | |
12733 | ||
e32de7ba | 12734 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12735 | pv != this->vals()->end(); | |
e440a328 | 12736 | ++pv) |
12737 | { | |
12738 | if (*pv != NULL | |
12739 | && !(*pv)->is_constant() | |
12740 | && (!(*pv)->is_composite_literal() | |
12741 | || (*pv)->is_nonconstant_composite_literal())) | |
12742 | return false; | |
12743 | } | |
12744 | return true; | |
12745 | } | |
12746 | ||
3ae06f68 | 12747 | // Return whether this can be used a constant initializer. |
f9ca30f9 | 12748 | |
12749 | bool | |
3ae06f68 | 12750 | Array_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12751 | { |
e32de7ba | 12752 | if (this->vals() == NULL) |
f9ca30f9 | 12753 | return true; |
de048538 | 12754 | |
12755 | // There are no constant constructors for interfaces. | |
12756 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12757 | return false; | |
12758 | ||
e32de7ba | 12759 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12760 | pv != this->vals()->end(); | |
f9ca30f9 | 12761 | ++pv) |
12762 | { | |
3ae06f68 | 12763 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12764 | return false; |
12765 | } | |
12766 | return true; | |
12767 | } | |
12768 | ||
e440a328 | 12769 | // Final type determination. |
12770 | ||
12771 | void | |
12772 | Array_construction_expression::do_determine_type(const Type_context*) | |
12773 | { | |
e32de7ba | 12774 | if (this->vals() == NULL) |
e440a328 | 12775 | return; |
12776 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
e32de7ba | 12777 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12778 | pv != this->vals()->end(); | |
e440a328 | 12779 | ++pv) |
12780 | { | |
12781 | if (*pv != NULL) | |
12782 | (*pv)->determine_type(&subcontext); | |
12783 | } | |
12784 | } | |
12785 | ||
12786 | // Check types. | |
12787 | ||
12788 | void | |
12789 | Array_construction_expression::do_check_types(Gogo*) | |
12790 | { | |
e32de7ba | 12791 | if (this->vals() == NULL) |
e440a328 | 12792 | return; |
12793 | ||
12794 | Array_type* at = this->type_->array_type(); | |
12795 | int i = 0; | |
12796 | Type* element_type = at->element_type(); | |
e32de7ba | 12797 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12798 | pv != this->vals()->end(); | |
e440a328 | 12799 | ++pv, ++i) |
12800 | { | |
12801 | if (*pv != NULL | |
12802 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12803 | { | |
631d5788 | 12804 | go_error_at((*pv)->location(), |
12805 | "incompatible type for element %d in composite literal", | |
12806 | i + 1); | |
e440a328 | 12807 | this->set_is_error(); |
12808 | } | |
12809 | } | |
e440a328 | 12810 | } |
12811 | ||
8ba8cc87 | 12812 | // Flatten an array construction expression. Store the values into |
12813 | // temporaries in case they need interface conversion. | |
12814 | ||
12815 | Expression* | |
12816 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12817 | Statement_inserter* inserter) | |
12818 | { | |
e32de7ba | 12819 | if (this->vals() == NULL) |
8ba8cc87 | 12820 | return this; |
12821 | ||
12822 | // If this is a constant array, we don't need temporaries. | |
de048538 | 12823 | if (this->is_constant_array() || this->is_static_initializer()) |
8ba8cc87 | 12824 | return this; |
12825 | ||
12826 | Location loc = this->location(); | |
e32de7ba | 12827 | for (Expression_list::iterator pv = this->vals()->begin(); |
12828 | pv != this->vals()->end(); | |
8ba8cc87 | 12829 | ++pv) |
12830 | { | |
12831 | if (*pv != NULL) | |
12832 | { | |
5bf8be8b | 12833 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12834 | { | |
12835 | go_assert(saw_errors()); | |
12836 | return Expression::make_error(loc); | |
12837 | } | |
8ba8cc87 | 12838 | if (!(*pv)->is_variable()) |
12839 | { | |
12840 | Temporary_statement* temp = | |
12841 | Statement::make_temporary(NULL, *pv, loc); | |
12842 | inserter->insert(temp); | |
12843 | *pv = Expression::make_temporary_reference(temp, loc); | |
12844 | } | |
12845 | } | |
12846 | } | |
12847 | return this; | |
12848 | } | |
12849 | ||
2c809f8f | 12850 | // Get a constructor expression for the array values. |
e440a328 | 12851 | |
2c809f8f | 12852 | Bexpression* |
12853 | Array_construction_expression::get_constructor(Translate_context* context, | |
12854 | Btype* array_btype) | |
e440a328 | 12855 | { |
e440a328 | 12856 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12857 | |
12858 | std::vector<unsigned long> indexes; | |
12859 | std::vector<Bexpression*> vals; | |
12860 | Gogo* gogo = context->gogo(); | |
e32de7ba | 12861 | if (this->vals() != NULL) |
e440a328 | 12862 | { |
12863 | size_t i = 0; | |
ffe743ca | 12864 | std::vector<unsigned long>::const_iterator pi; |
12865 | if (this->indexes_ != NULL) | |
12866 | pi = this->indexes_->begin(); | |
e32de7ba | 12867 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12868 | pv != this->vals()->end(); | |
e440a328 | 12869 | ++pv, ++i) |
12870 | { | |
ffe743ca | 12871 | if (this->indexes_ != NULL) |
12872 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12873 | |
12874 | if (this->indexes_ == NULL) | |
2c809f8f | 12875 | indexes.push_back(i); |
ffe743ca | 12876 | else |
2c809f8f | 12877 | indexes.push_back(*pi); |
e440a328 | 12878 | if (*pv == NULL) |
63697958 | 12879 | { |
63697958 | 12880 | Btype* ebtype = element_type->get_backend(gogo); |
12881 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12882 | vals.push_back(zv); |
63697958 | 12883 | } |
e440a328 | 12884 | else |
12885 | { | |
2c809f8f | 12886 | Expression* val_expr = |
12887 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12888 | this->location()); | |
ea664253 | 12889 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12890 | } |
ffe743ca | 12891 | if (this->indexes_ != NULL) |
12892 | ++pi; | |
e440a328 | 12893 | } |
ffe743ca | 12894 | if (this->indexes_ != NULL) |
12895 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12896 | } |
2c809f8f | 12897 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12898 | vals, this->location()); | |
e440a328 | 12899 | } |
12900 | ||
12901 | // Export an array construction. | |
12902 | ||
12903 | void | |
12904 | Array_construction_expression::do_export(Export* exp) const | |
12905 | { | |
12906 | exp->write_c_string("convert("); | |
12907 | exp->write_type(this->type_); | |
e32de7ba | 12908 | if (this->vals() != NULL) |
e440a328 | 12909 | { |
ffe743ca | 12910 | std::vector<unsigned long>::const_iterator pi; |
12911 | if (this->indexes_ != NULL) | |
12912 | pi = this->indexes_->begin(); | |
e32de7ba | 12913 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12914 | pv != this->vals()->end(); | |
e440a328 | 12915 | ++pv) |
12916 | { | |
12917 | exp->write_c_string(", "); | |
ffe743ca | 12918 | |
12919 | if (this->indexes_ != NULL) | |
12920 | { | |
12921 | char buf[100]; | |
12922 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12923 | exp->write_c_string(buf); | |
12924 | exp->write_c_string(":"); | |
12925 | } | |
12926 | ||
e440a328 | 12927 | if (*pv != NULL) |
12928 | (*pv)->export_expression(exp); | |
ffe743ca | 12929 | |
12930 | if (this->indexes_ != NULL) | |
12931 | ++pi; | |
e440a328 | 12932 | } |
12933 | } | |
12934 | exp->write_c_string(")"); | |
12935 | } | |
12936 | ||
0e9a2e72 | 12937 | // Dump ast representation of an array construction expression. |
d751bb78 | 12938 | |
12939 | void | |
12940 | Array_construction_expression::do_dump_expression( | |
12941 | Ast_dump_context* ast_dump_context) const | |
12942 | { | |
ffe743ca | 12943 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12944 | |
12945 | ast_dump_context->ostream() << "[" ; | |
12946 | if (length != NULL) | |
12947 | { | |
12948 | ast_dump_context->dump_expression(length); | |
12949 | } | |
12950 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12951 | ast_dump_context->dump_type(this->type_); |
0e9a2e72 | 12952 | this->dump_slice_storage_expression(ast_dump_context); |
d751bb78 | 12953 | ast_dump_context->ostream() << "{" ; |
ffe743ca | 12954 | if (this->indexes_ == NULL) |
e32de7ba | 12955 | ast_dump_context->dump_expression_list(this->vals()); |
ffe743ca | 12956 | else |
12957 | { | |
e32de7ba | 12958 | Expression_list::const_iterator pv = this->vals()->begin(); |
ffe743ca | 12959 | for (std::vector<unsigned long>::const_iterator pi = |
12960 | this->indexes_->begin(); | |
12961 | pi != this->indexes_->end(); | |
12962 | ++pi, ++pv) | |
12963 | { | |
12964 | if (pi != this->indexes_->begin()) | |
12965 | ast_dump_context->ostream() << ", "; | |
12966 | ast_dump_context->ostream() << *pi << ':'; | |
12967 | ast_dump_context->dump_expression(*pv); | |
12968 | } | |
12969 | } | |
d751bb78 | 12970 | ast_dump_context->ostream() << "}" ; |
12971 | ||
12972 | } | |
12973 | ||
da244e59 | 12974 | // Class Fixed_array_construction_expression. |
e440a328 | 12975 | |
da244e59 | 12976 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
12977 | Type* type, const std::vector<unsigned long>* indexes, | |
12978 | Expression_list* vals, Location location) | |
12979 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
12980 | type, indexes, vals, location) | |
12981 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12982 | |
ea664253 | 12983 | // Return the backend representation for constructing a fixed array. |
e440a328 | 12984 | |
ea664253 | 12985 | Bexpression* |
12986 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12987 | { |
9f0e0513 | 12988 | Type* type = this->type(); |
12989 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 12990 | return this->get_constructor(context, btype); |
e440a328 | 12991 | } |
12992 | ||
76f85fd6 | 12993 | Expression* |
12994 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
12995 | Location location) | |
12996 | { | |
12997 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
12998 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
12999 | } | |
13000 | ||
da244e59 | 13001 | // Class Slice_construction_expression. |
e440a328 | 13002 | |
da244e59 | 13003 | Slice_construction_expression::Slice_construction_expression( |
13004 | Type* type, const std::vector<unsigned long>* indexes, | |
13005 | Expression_list* vals, Location location) | |
13006 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
13007 | type, indexes, vals, location), | |
0e9a2e72 | 13008 | valtype_(NULL), array_val_(NULL), slice_storage_(NULL), |
13009 | storage_escapes_(true) | |
e440a328 | 13010 | { |
da244e59 | 13011 | go_assert(type->is_slice_type()); |
23d77f91 | 13012 | |
da244e59 | 13013 | unsigned long lenval; |
13014 | Expression* length; | |
13015 | if (vals == NULL || vals->empty()) | |
13016 | lenval = 0; | |
13017 | else | |
13018 | { | |
13019 | if (this->indexes() == NULL) | |
13020 | lenval = vals->size(); | |
13021 | else | |
13022 | lenval = indexes->back() + 1; | |
13023 | } | |
13024 | Type* int_type = Type::lookup_integer_type("int"); | |
13025 | length = Expression::make_integer_ul(lenval, int_type, location); | |
13026 | Type* element_type = type->array_type()->element_type(); | |
6bf4793c | 13027 | Array_type* array_type = Type::make_array_type(element_type, length); |
13028 | array_type->set_is_array_incomparable(); | |
13029 | this->valtype_ = array_type; | |
da244e59 | 13030 | } |
e440a328 | 13031 | |
23d77f91 | 13032 | // Traversal. |
13033 | ||
13034 | int | |
13035 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
13036 | { | |
13037 | if (this->Array_construction_expression::do_traverse(traverse) | |
13038 | == TRAVERSE_EXIT) | |
13039 | return TRAVERSE_EXIT; | |
13040 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
13041 | return TRAVERSE_EXIT; | |
0e9a2e72 | 13042 | if (this->array_val_ != NULL |
13043 | && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) | |
13044 | return TRAVERSE_EXIT; | |
13045 | if (this->slice_storage_ != NULL | |
13046 | && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) | |
13047 | return TRAVERSE_EXIT; | |
23d77f91 | 13048 | return TRAVERSE_CONTINUE; |
13049 | } | |
13050 | ||
0e9a2e72 | 13051 | // Helper routine to create fixed array value underlying the slice literal. |
13052 | // May be called during flattening, or later during do_get_backend(). | |
e440a328 | 13053 | |
0e9a2e72 | 13054 | Expression* |
13055 | Slice_construction_expression::create_array_val() | |
e440a328 | 13056 | { |
f9c68f17 | 13057 | Array_type* array_type = this->type()->array_type(); |
13058 | if (array_type == NULL) | |
13059 | { | |
c484d925 | 13060 | go_assert(this->type()->is_error()); |
0e9a2e72 | 13061 | return NULL; |
f9c68f17 | 13062 | } |
13063 | ||
f23d7786 | 13064 | Location loc = this->location(); |
23d77f91 | 13065 | go_assert(this->valtype_ != NULL); |
3d60812e | 13066 | |
f23d7786 | 13067 | Expression_list* vals = this->vals(); |
0e9a2e72 | 13068 | return new Fixed_array_construction_expression( |
13069 | this->valtype_, this->indexes(), vals, loc); | |
13070 | } | |
13071 | ||
13072 | // If we're previous established that the slice storage does not | |
13073 | // escape, then create a separate array temp val here for it. We | |
13074 | // need to do this as part of flattening so as to be able to insert | |
13075 | // the new temp statement. | |
13076 | ||
13077 | Expression* | |
13078 | Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no, | |
13079 | Statement_inserter* inserter) | |
13080 | { | |
13081 | if (this->type()->array_type() == NULL) | |
13082 | return NULL; | |
13083 | ||
13084 | // Base class flattening first | |
13085 | this->Array_construction_expression::do_flatten(gogo, no, inserter); | |
13086 | ||
a1bbc2c3 | 13087 | // Create a stack-allocated storage temp if storage won't escape |
03118c21 | 13088 | if (!this->storage_escapes_ |
13089 | && this->slice_storage_ == NULL | |
13090 | && this->element_count() > 0) | |
0e9a2e72 | 13091 | { |
13092 | Location loc = this->location(); | |
03118c21 | 13093 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13094 | go_assert(this->array_val_); |
13095 | Temporary_statement* temp = | |
13096 | Statement::make_temporary(this->valtype_, this->array_val_, loc); | |
13097 | inserter->insert(temp); | |
13098 | this->slice_storage_ = Expression::make_temporary_reference(temp, loc); | |
13099 | } | |
13100 | return this; | |
13101 | } | |
13102 | ||
13103 | // When dumping a slice construction expression that has an explicit | |
13104 | // storeage temp, emit the temp here (if we don't do this the storage | |
13105 | // temp appears unused in the AST dump). | |
13106 | ||
13107 | void | |
13108 | Slice_construction_expression:: | |
13109 | dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const | |
13110 | { | |
13111 | if (this->slice_storage_ == NULL) | |
13112 | return; | |
13113 | ast_dump_context->ostream() << "storage=" ; | |
13114 | ast_dump_context->dump_expression(this->slice_storage_); | |
13115 | } | |
13116 | ||
13117 | // Return the backend representation for constructing a slice. | |
13118 | ||
13119 | Bexpression* | |
13120 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
13121 | { | |
13122 | if (this->array_val_ == NULL) | |
03118c21 | 13123 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13124 | if (this->array_val_ == NULL) |
13125 | { | |
13126 | go_assert(this->type()->is_error()); | |
13127 | return context->backend()->error_expression(); | |
13128 | } | |
13129 | ||
13130 | Location loc = this->location(); | |
e440a328 | 13131 | |
3ae06f68 | 13132 | bool is_static_initializer = this->array_val_->is_static_initializer(); |
d8829beb | 13133 | |
13134 | // We have to copy the initial values into heap memory if we are in | |
3ae06f68 | 13135 | // a function or if the values are not constants. |
13136 | bool copy_to_heap = context->function() != NULL || !is_static_initializer; | |
e440a328 | 13137 | |
f23d7786 | 13138 | Expression* space; |
0e9a2e72 | 13139 | |
13140 | if (this->slice_storage_ != NULL) | |
13141 | { | |
13142 | go_assert(!this->storage_escapes_); | |
13143 | space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); | |
13144 | } | |
13145 | else if (!copy_to_heap) | |
e440a328 | 13146 | { |
f23d7786 | 13147 | // The initializer will only run once. |
0e9a2e72 | 13148 | space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); |
f23d7786 | 13149 | space->unary_expression()->set_is_slice_init(); |
e440a328 | 13150 | } |
13151 | else | |
45ff893b | 13152 | { |
5973ede0 | 13153 | go_assert(this->storage_escapes_ || this->element_count() == 0); |
0e9a2e72 | 13154 | space = Expression::make_heap_expression(this->array_val_, loc); |
45ff893b | 13155 | } |
e440a328 | 13156 | |
2c809f8f | 13157 | // Build a constructor for the slice. |
f23d7786 | 13158 | Expression* len = this->valtype_->array_type()->length(); |
13159 | Expression* slice_val = | |
13160 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 13161 | return slice_val->get_backend(context); |
e440a328 | 13162 | } |
13163 | ||
13164 | // Make a slice composite literal. This is used by the type | |
13165 | // descriptor code. | |
13166 | ||
0e9a2e72 | 13167 | Slice_construction_expression* |
e440a328 | 13168 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, |
b13c66cd | 13169 | Location location) |
e440a328 | 13170 | { |
411eb89e | 13171 | go_assert(type->is_slice_type()); |
2c809f8f | 13172 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 13173 | } |
13174 | ||
da244e59 | 13175 | // Class Map_construction_expression. |
e440a328 | 13176 | |
13177 | // Traversal. | |
13178 | ||
13179 | int | |
13180 | Map_construction_expression::do_traverse(Traverse* traverse) | |
13181 | { | |
13182 | if (this->vals_ != NULL | |
13183 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
13184 | return TRAVERSE_EXIT; | |
13185 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13186 | return TRAVERSE_EXIT; | |
13187 | return TRAVERSE_CONTINUE; | |
13188 | } | |
13189 | ||
2c809f8f | 13190 | // Flatten constructor initializer into a temporary variable since |
13191 | // we need to take its address for __go_construct_map. | |
13192 | ||
13193 | Expression* | |
13194 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
13195 | Statement_inserter* inserter) | |
13196 | { | |
13197 | if (!this->is_error_expression() | |
13198 | && this->vals_ != NULL | |
13199 | && !this->vals_->empty() | |
13200 | && this->constructor_temp_ == NULL) | |
13201 | { | |
13202 | Map_type* mt = this->type_->map_type(); | |
13203 | Type* key_type = mt->key_type(); | |
13204 | Type* val_type = mt->val_type(); | |
13205 | this->element_type_ = Type::make_builtin_struct_type(2, | |
13206 | "__key", key_type, | |
13207 | "__val", val_type); | |
13208 | ||
13209 | Expression_list* value_pairs = new Expression_list(); | |
13210 | Location loc = this->location(); | |
13211 | ||
13212 | size_t i = 0; | |
13213 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13214 | pv != this->vals_->end(); | |
13215 | ++pv, ++i) | |
13216 | { | |
13217 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 13218 | Expression* key = *pv; |
5bf8be8b | 13219 | if (key->is_error_expression() || key->type()->is_error_type()) |
13220 | { | |
13221 | go_assert(saw_errors()); | |
13222 | return Expression::make_error(loc); | |
13223 | } | |
91c0fd76 | 13224 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
13225 | { | |
13226 | Temporary_statement* temp = | |
13227 | Statement::make_temporary(NULL, key, loc); | |
13228 | inserter->insert(temp); | |
13229 | key = Expression::make_temporary_reference(temp, loc); | |
13230 | } | |
13231 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 13232 | |
13233 | ++pv; | |
91c0fd76 | 13234 | Expression* val = *pv; |
5bf8be8b | 13235 | if (val->is_error_expression() || val->type()->is_error_type()) |
13236 | { | |
13237 | go_assert(saw_errors()); | |
13238 | return Expression::make_error(loc); | |
13239 | } | |
91c0fd76 | 13240 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
13241 | { | |
13242 | Temporary_statement* temp = | |
13243 | Statement::make_temporary(NULL, val, loc); | |
13244 | inserter->insert(temp); | |
13245 | val = Expression::make_temporary_reference(temp, loc); | |
13246 | } | |
13247 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 13248 | |
13249 | key_value_pair->push_back(key); | |
13250 | key_value_pair->push_back(val); | |
13251 | value_pairs->push_back( | |
13252 | Expression::make_struct_composite_literal(this->element_type_, | |
13253 | key_value_pair, loc)); | |
13254 | } | |
13255 | ||
e67508fa | 13256 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
6bf4793c | 13257 | Array_type* ctor_type = |
2c809f8f | 13258 | Type::make_array_type(this->element_type_, element_count); |
6bf4793c | 13259 | ctor_type->set_is_array_incomparable(); |
2c809f8f | 13260 | Expression* constructor = |
13261 | new Fixed_array_construction_expression(ctor_type, NULL, | |
13262 | value_pairs, loc); | |
13263 | ||
13264 | this->constructor_temp_ = | |
13265 | Statement::make_temporary(NULL, constructor, loc); | |
13266 | constructor->issue_nil_check(); | |
13267 | this->constructor_temp_->set_is_address_taken(); | |
13268 | inserter->insert(this->constructor_temp_); | |
13269 | } | |
13270 | ||
13271 | return this; | |
13272 | } | |
13273 | ||
e440a328 | 13274 | // Final type determination. |
13275 | ||
13276 | void | |
13277 | Map_construction_expression::do_determine_type(const Type_context*) | |
13278 | { | |
13279 | if (this->vals_ == NULL) | |
13280 | return; | |
13281 | ||
13282 | Map_type* mt = this->type_->map_type(); | |
13283 | Type_context key_context(mt->key_type(), false); | |
13284 | Type_context val_context(mt->val_type(), false); | |
13285 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13286 | pv != this->vals_->end(); | |
13287 | ++pv) | |
13288 | { | |
13289 | (*pv)->determine_type(&key_context); | |
13290 | ++pv; | |
13291 | (*pv)->determine_type(&val_context); | |
13292 | } | |
13293 | } | |
13294 | ||
13295 | // Check types. | |
13296 | ||
13297 | void | |
13298 | Map_construction_expression::do_check_types(Gogo*) | |
13299 | { | |
13300 | if (this->vals_ == NULL) | |
13301 | return; | |
13302 | ||
13303 | Map_type* mt = this->type_->map_type(); | |
13304 | int i = 0; | |
13305 | Type* key_type = mt->key_type(); | |
13306 | Type* val_type = mt->val_type(); | |
13307 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13308 | pv != this->vals_->end(); | |
13309 | ++pv, ++i) | |
13310 | { | |
13311 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
13312 | { | |
631d5788 | 13313 | go_error_at((*pv)->location(), |
13314 | "incompatible type for element %d key in map construction", | |
13315 | i + 1); | |
e440a328 | 13316 | this->set_is_error(); |
13317 | } | |
13318 | ++pv; | |
13319 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
13320 | { | |
631d5788 | 13321 | go_error_at((*pv)->location(), |
13322 | ("incompatible type for element %d value " | |
13323 | "in map construction"), | |
e440a328 | 13324 | i + 1); |
13325 | this->set_is_error(); | |
13326 | } | |
13327 | } | |
13328 | } | |
13329 | ||
ea664253 | 13330 | // Return the backend representation for constructing a map. |
e440a328 | 13331 | |
ea664253 | 13332 | Bexpression* |
13333 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13334 | { |
2c809f8f | 13335 | if (this->is_error_expression()) |
ea664253 | 13336 | return context->backend()->error_expression(); |
2c809f8f | 13337 | Location loc = this->location(); |
e440a328 | 13338 | |
e440a328 | 13339 | size_t i = 0; |
2c809f8f | 13340 | Expression* ventries; |
e440a328 | 13341 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 13342 | ventries = Expression::make_nil(loc); |
e440a328 | 13343 | else |
13344 | { | |
2c809f8f | 13345 | go_assert(this->constructor_temp_ != NULL); |
13346 | i = this->vals_->size() / 2; | |
e440a328 | 13347 | |
2c809f8f | 13348 | Expression* ctor_ref = |
13349 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
13350 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
13351 | } | |
e440a328 | 13352 | |
2c809f8f | 13353 | Map_type* mt = this->type_->map_type(); |
13354 | if (this->element_type_ == NULL) | |
13355 | this->element_type_ = | |
13356 | Type::make_builtin_struct_type(2, | |
13357 | "__key", mt->key_type(), | |
13358 | "__val", mt->val_type()); | |
0d5530d9 | 13359 | Expression* descriptor = Expression::make_type_descriptor(mt, loc); |
2c809f8f | 13360 | |
13361 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 13362 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 13363 | |
13364 | Expression* entry_size = | |
13365 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
13366 | ||
13367 | unsigned int field_index; | |
13368 | const Struct_field* valfield = | |
13369 | this->element_type_->find_local_field("__val", &field_index); | |
13370 | Expression* val_offset = | |
13371 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
2c809f8f | 13372 | |
13373 | Expression* map_ctor = | |
0d5530d9 | 13374 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, |
13375 | entry_size, val_offset, ventries); | |
ea664253 | 13376 | return map_ctor->get_backend(context); |
2c809f8f | 13377 | } |
e440a328 | 13378 | |
2c809f8f | 13379 | // Export an array construction. |
e440a328 | 13380 | |
2c809f8f | 13381 | void |
13382 | Map_construction_expression::do_export(Export* exp) const | |
13383 | { | |
13384 | exp->write_c_string("convert("); | |
13385 | exp->write_type(this->type_); | |
13386 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13387 | pv != this->vals_->end(); | |
13388 | ++pv) | |
13389 | { | |
13390 | exp->write_c_string(", "); | |
13391 | (*pv)->export_expression(exp); | |
13392 | } | |
13393 | exp->write_c_string(")"); | |
13394 | } | |
e440a328 | 13395 | |
2c809f8f | 13396 | // Dump ast representation for a map construction expression. |
d751bb78 | 13397 | |
13398 | void | |
13399 | Map_construction_expression::do_dump_expression( | |
13400 | Ast_dump_context* ast_dump_context) const | |
13401 | { | |
d751bb78 | 13402 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 13403 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13404 | ast_dump_context->ostream() << "}"; |
13405 | } | |
13406 | ||
7795ac51 | 13407 | // Class Composite_literal_expression. |
e440a328 | 13408 | |
13409 | // Traversal. | |
13410 | ||
13411 | int | |
13412 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13413 | { | |
dbffccfc | 13414 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 13415 | return TRAVERSE_EXIT; |
dbffccfc | 13416 | |
13417 | // If this is a struct composite literal with keys, then the keys | |
13418 | // are field names, not expressions. We don't want to traverse them | |
13419 | // in that case. If we do, we can give an erroneous error "variable | |
13420 | // initializer refers to itself." See bug482.go in the testsuite. | |
13421 | if (this->has_keys_ && this->vals_ != NULL) | |
13422 | { | |
13423 | // The type may not be resolvable at this point. | |
13424 | Type* type = this->type_; | |
a01f2481 | 13425 | |
7795ac51 | 13426 | for (int depth = 0; depth < this->depth_; ++depth) |
a01f2481 | 13427 | { |
13428 | if (type->array_type() != NULL) | |
13429 | type = type->array_type()->element_type(); | |
13430 | else if (type->map_type() != NULL) | |
7795ac51 | 13431 | { |
13432 | if (this->key_path_[depth]) | |
13433 | type = type->map_type()->key_type(); | |
13434 | else | |
13435 | type = type->map_type()->val_type(); | |
13436 | } | |
a01f2481 | 13437 | else |
13438 | { | |
13439 | // This error will be reported during lowering. | |
13440 | return TRAVERSE_CONTINUE; | |
13441 | } | |
13442 | } | |
13443 | ||
dbffccfc | 13444 | while (true) |
13445 | { | |
13446 | if (type->classification() == Type::TYPE_NAMED) | |
13447 | type = type->named_type()->real_type(); | |
13448 | else if (type->classification() == Type::TYPE_FORWARD) | |
13449 | { | |
13450 | Type* t = type->forwarded(); | |
13451 | if (t == type) | |
13452 | break; | |
13453 | type = t; | |
13454 | } | |
13455 | else | |
13456 | break; | |
13457 | } | |
13458 | ||
13459 | if (type->classification() == Type::TYPE_STRUCT) | |
13460 | { | |
13461 | Expression_list::iterator p = this->vals_->begin(); | |
13462 | while (p != this->vals_->end()) | |
13463 | { | |
13464 | // Skip key. | |
13465 | ++p; | |
13466 | go_assert(p != this->vals_->end()); | |
13467 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13468 | return TRAVERSE_EXIT; | |
13469 | ++p; | |
13470 | } | |
13471 | return TRAVERSE_CONTINUE; | |
13472 | } | |
13473 | } | |
13474 | ||
13475 | if (this->vals_ != NULL) | |
13476 | return this->vals_->traverse(traverse); | |
13477 | ||
13478 | return TRAVERSE_CONTINUE; | |
e440a328 | 13479 | } |
13480 | ||
13481 | // Lower a generic composite literal into a specific version based on | |
13482 | // the type. | |
13483 | ||
13484 | Expression* | |
ceeb4318 | 13485 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13486 | Statement_inserter* inserter, int) | |
e440a328 | 13487 | { |
13488 | Type* type = this->type_; | |
13489 | ||
7795ac51 | 13490 | for (int depth = 0; depth < this->depth_; ++depth) |
e440a328 | 13491 | { |
13492 | if (type->array_type() != NULL) | |
13493 | type = type->array_type()->element_type(); | |
13494 | else if (type->map_type() != NULL) | |
7795ac51 | 13495 | { |
13496 | if (this->key_path_[depth]) | |
13497 | type = type->map_type()->key_type(); | |
13498 | else | |
13499 | type = type->map_type()->val_type(); | |
13500 | } | |
e440a328 | 13501 | else |
13502 | { | |
5c13bd80 | 13503 | if (!type->is_error()) |
631d5788 | 13504 | go_error_at(this->location(), |
13505 | ("may only omit types within composite literals " | |
13506 | "of slice, array, or map type")); | |
e440a328 | 13507 | return Expression::make_error(this->location()); |
13508 | } | |
13509 | } | |
13510 | ||
e00772b3 | 13511 | Type *pt = type->points_to(); |
13512 | bool is_pointer = false; | |
13513 | if (pt != NULL) | |
13514 | { | |
13515 | is_pointer = true; | |
13516 | type = pt; | |
13517 | } | |
13518 | ||
13519 | Expression* ret; | |
5c13bd80 | 13520 | if (type->is_error()) |
e440a328 | 13521 | return Expression::make_error(this->location()); |
13522 | else if (type->struct_type() != NULL) | |
e00772b3 | 13523 | ret = this->lower_struct(gogo, type); |
e440a328 | 13524 | else if (type->array_type() != NULL) |
113ef6a5 | 13525 | ret = this->lower_array(type); |
e440a328 | 13526 | else if (type->map_type() != NULL) |
e00772b3 | 13527 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13528 | else |
13529 | { | |
631d5788 | 13530 | go_error_at(this->location(), |
13531 | ("expected struct, slice, array, or map type " | |
13532 | "for composite literal")); | |
e440a328 | 13533 | return Expression::make_error(this->location()); |
13534 | } | |
e00772b3 | 13535 | |
13536 | if (is_pointer) | |
2c809f8f | 13537 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13538 | |
13539 | return ret; | |
e440a328 | 13540 | } |
13541 | ||
13542 | // Lower a struct composite literal. | |
13543 | ||
13544 | Expression* | |
81c4b26b | 13545 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13546 | { |
b13c66cd | 13547 | Location location = this->location(); |
e440a328 | 13548 | Struct_type* st = type->struct_type(); |
13549 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13550 | { |
e6013c28 | 13551 | if (this->vals_ != NULL |
13552 | && !this->vals_->empty() | |
13553 | && type->named_type() != NULL | |
13554 | && type->named_type()->named_object()->package() != NULL) | |
13555 | { | |
13556 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13557 | pf != st->fields()->end(); | |
13558 | ++pf) | |
07daa4e7 | 13559 | { |
07ba7f26 | 13560 | if (Gogo::is_hidden_name(pf->field_name()) |
13561 | || pf->is_embedded_builtin(gogo)) | |
631d5788 | 13562 | go_error_at(this->location(), |
13563 | "assignment of unexported field %qs in %qs literal", | |
13564 | Gogo::message_name(pf->field_name()).c_str(), | |
13565 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13566 | } |
13567 | } | |
13568 | ||
13569 | return new Struct_construction_expression(type, this->vals_, location); | |
13570 | } | |
e440a328 | 13571 | |
13572 | size_t field_count = st->field_count(); | |
13573 | std::vector<Expression*> vals(field_count); | |
e32de7ba | 13574 | std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>); |
e440a328 | 13575 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13576 | Expression* external_expr = NULL; |
13577 | const Named_object* external_no = NULL; | |
e440a328 | 13578 | while (p != this->vals_->end()) |
13579 | { | |
13580 | Expression* name_expr = *p; | |
13581 | ||
13582 | ++p; | |
c484d925 | 13583 | go_assert(p != this->vals_->end()); |
e440a328 | 13584 | Expression* val = *p; |
13585 | ||
13586 | ++p; | |
13587 | ||
13588 | if (name_expr == NULL) | |
13589 | { | |
631d5788 | 13590 | go_error_at(val->location(), |
13591 | "mixture of field and value initializers"); | |
e440a328 | 13592 | return Expression::make_error(location); |
13593 | } | |
13594 | ||
13595 | bool bad_key = false; | |
13596 | std::string name; | |
81c4b26b | 13597 | const Named_object* no = NULL; |
e440a328 | 13598 | switch (name_expr->classification()) |
13599 | { | |
13600 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13601 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 13602 | if (type->named_type() != NULL) |
13603 | { | |
13604 | // If the named object found for this field name comes from a | |
13605 | // different package than the struct it is a part of, do not count | |
13606 | // this incorrect lookup as a usage of the object's package. | |
13607 | no = name_expr->unknown_expression()->named_object(); | |
13608 | if (no->package() != NULL | |
13609 | && no->package() != type->named_type()->named_object()->package()) | |
13610 | no->package()->forget_usage(name_expr); | |
13611 | } | |
e440a328 | 13612 | break; |
13613 | ||
13614 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13615 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13616 | break; |
13617 | ||
13618 | case EXPRESSION_TYPE: | |
13619 | { | |
13620 | Type* t = name_expr->type(); | |
13621 | Named_type* nt = t->named_type(); | |
13622 | if (nt == NULL) | |
13623 | bad_key = true; | |
13624 | else | |
81c4b26b | 13625 | no = nt->named_object(); |
e440a328 | 13626 | } |
13627 | break; | |
13628 | ||
13629 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13630 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13631 | break; |
13632 | ||
b0c09712 | 13633 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
13634 | no = name_expr->enclosed_var_expression()->variable(); | |
e440a328 | 13635 | break; |
13636 | ||
b0c09712 | 13637 | case EXPRESSION_FUNC_REFERENCE: |
13638 | no = name_expr->func_expression()->named_object(); | |
e440a328 | 13639 | break; |
13640 | ||
13641 | default: | |
13642 | bad_key = true; | |
13643 | break; | |
13644 | } | |
13645 | if (bad_key) | |
13646 | { | |
631d5788 | 13647 | go_error_at(name_expr->location(), "expected struct field name"); |
e440a328 | 13648 | return Expression::make_error(location); |
13649 | } | |
13650 | ||
81c4b26b | 13651 | if (no != NULL) |
13652 | { | |
62750cd5 | 13653 | if (no->package() != NULL && external_expr == NULL) |
13654 | { | |
13655 | external_expr = name_expr; | |
13656 | external_no = no; | |
13657 | } | |
13658 | ||
81c4b26b | 13659 | name = no->name(); |
13660 | ||
13661 | // A predefined name won't be packed. If it starts with a | |
13662 | // lower case letter we need to check for that case, because | |
2d29d278 | 13663 | // the field name will be packed. FIXME. |
81c4b26b | 13664 | if (!Gogo::is_hidden_name(name) |
13665 | && name[0] >= 'a' | |
13666 | && name[0] <= 'z') | |
13667 | { | |
13668 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13669 | if (gno == no) | |
13670 | name = gogo->pack_hidden_name(name, false); | |
13671 | } | |
13672 | } | |
13673 | ||
e440a328 | 13674 | unsigned int index; |
13675 | const Struct_field* sf = st->find_local_field(name, &index); | |
13676 | if (sf == NULL) | |
13677 | { | |
631d5788 | 13678 | go_error_at(name_expr->location(), "unknown field %qs in %qs", |
13679 | Gogo::message_name(name).c_str(), | |
13680 | (type->named_type() != NULL | |
13681 | ? type->named_type()->message_name().c_str() | |
13682 | : "unnamed struct")); | |
e440a328 | 13683 | return Expression::make_error(location); |
13684 | } | |
13685 | if (vals[index] != NULL) | |
13686 | { | |
631d5788 | 13687 | go_error_at(name_expr->location(), |
13688 | "duplicate value for field %qs in %qs", | |
13689 | Gogo::message_name(name).c_str(), | |
13690 | (type->named_type() != NULL | |
13691 | ? type->named_type()->message_name().c_str() | |
13692 | : "unnamed struct")); | |
e440a328 | 13693 | return Expression::make_error(location); |
13694 | } | |
13695 | ||
07daa4e7 | 13696 | if (type->named_type() != NULL |
13697 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 13698 | && (Gogo::is_hidden_name(sf->field_name()) |
13699 | || sf->is_embedded_builtin(gogo))) | |
631d5788 | 13700 | go_error_at(name_expr->location(), |
13701 | "assignment of unexported field %qs in %qs literal", | |
13702 | Gogo::message_name(sf->field_name()).c_str(), | |
13703 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13704 | |
e440a328 | 13705 | vals[index] = val; |
e32de7ba | 13706 | traverse_order->push_back(static_cast<unsigned long>(index)); |
e440a328 | 13707 | } |
13708 | ||
62750cd5 | 13709 | if (!this->all_are_names_) |
13710 | { | |
13711 | // This is a weird case like bug462 in the testsuite. | |
13712 | if (external_expr == NULL) | |
631d5788 | 13713 | go_error_at(this->location(), "unknown field in %qs literal", |
13714 | (type->named_type() != NULL | |
13715 | ? type->named_type()->message_name().c_str() | |
13716 | : "unnamed struct")); | |
62750cd5 | 13717 | else |
631d5788 | 13718 | go_error_at(external_expr->location(), "unknown field %qs in %qs", |
13719 | external_no->message_name().c_str(), | |
13720 | (type->named_type() != NULL | |
13721 | ? type->named_type()->message_name().c_str() | |
13722 | : "unnamed struct")); | |
62750cd5 | 13723 | return Expression::make_error(location); |
13724 | } | |
13725 | ||
e440a328 | 13726 | Expression_list* list = new Expression_list; |
13727 | list->reserve(field_count); | |
13728 | for (size_t i = 0; i < field_count; ++i) | |
13729 | list->push_back(vals[i]); | |
13730 | ||
0c4f5a19 | 13731 | Struct_construction_expression* ret = |
13732 | new Struct_construction_expression(type, list, location); | |
13733 | ret->set_traverse_order(traverse_order); | |
13734 | return ret; | |
e440a328 | 13735 | } |
13736 | ||
e32de7ba | 13737 | // Index/value/traversal-order triple. |
00773463 | 13738 | |
e32de7ba | 13739 | struct IVT_triple { |
13740 | unsigned long index; | |
13741 | unsigned long traversal_order; | |
13742 | Expression* expr; | |
13743 | IVT_triple(unsigned long i, unsigned long to, Expression *e) | |
13744 | : index(i), traversal_order(to), expr(e) { } | |
13745 | bool operator<(const IVT_triple& other) const | |
13746 | { return this->index < other.index; } | |
00773463 | 13747 | }; |
13748 | ||
e440a328 | 13749 | // Lower an array composite literal. |
13750 | ||
13751 | Expression* | |
113ef6a5 | 13752 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13753 | { |
b13c66cd | 13754 | Location location = this->location(); |
e440a328 | 13755 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13756 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13757 | |
ffe743ca | 13758 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13759 | indexes->reserve(this->vals_->size()); | |
00773463 | 13760 | bool indexes_out_of_order = false; |
ffe743ca | 13761 | Expression_list* vals = new Expression_list(); |
13762 | vals->reserve(this->vals_->size()); | |
e440a328 | 13763 | unsigned long index = 0; |
13764 | Expression_list::const_iterator p = this->vals_->begin(); | |
13765 | while (p != this->vals_->end()) | |
13766 | { | |
13767 | Expression* index_expr = *p; | |
13768 | ||
13769 | ++p; | |
c484d925 | 13770 | go_assert(p != this->vals_->end()); |
e440a328 | 13771 | Expression* val = *p; |
13772 | ||
13773 | ++p; | |
13774 | ||
ffe743ca | 13775 | if (index_expr == NULL) |
13776 | { | |
13777 | if (!indexes->empty()) | |
13778 | indexes->push_back(index); | |
13779 | } | |
13780 | else | |
e440a328 | 13781 | { |
ffe743ca | 13782 | if (indexes->empty() && !vals->empty()) |
13783 | { | |
13784 | for (size_t i = 0; i < vals->size(); ++i) | |
13785 | indexes->push_back(i); | |
13786 | } | |
13787 | ||
0c77715b | 13788 | Numeric_constant nc; |
13789 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13790 | { |
631d5788 | 13791 | go_error_at(index_expr->location(), |
13792 | "index expression is not integer constant"); | |
e440a328 | 13793 | return Expression::make_error(location); |
13794 | } | |
6f6d9955 | 13795 | |
0c77715b | 13796 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13797 | { |
0c77715b | 13798 | case Numeric_constant::NC_UL_VALID: |
13799 | break; | |
13800 | case Numeric_constant::NC_UL_NOTINT: | |
631d5788 | 13801 | go_error_at(index_expr->location(), |
13802 | "index expression is not integer constant"); | |
0c77715b | 13803 | return Expression::make_error(location); |
13804 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 13805 | go_error_at(index_expr->location(), |
13806 | "index expression is negative"); | |
e440a328 | 13807 | return Expression::make_error(location); |
0c77715b | 13808 | case Numeric_constant::NC_UL_BIG: |
631d5788 | 13809 | go_error_at(index_expr->location(), "index value overflow"); |
e440a328 | 13810 | return Expression::make_error(location); |
0c77715b | 13811 | default: |
13812 | go_unreachable(); | |
e440a328 | 13813 | } |
6f6d9955 | 13814 | |
13815 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13816 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13817 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13818 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13819 | { |
631d5788 | 13820 | go_error_at(index_expr->location(), "index value overflow"); |
6f6d9955 | 13821 | return Expression::make_error(location); |
13822 | } | |
13823 | ||
ffe743ca | 13824 | if (std::find(indexes->begin(), indexes->end(), index) |
13825 | != indexes->end()) | |
e440a328 | 13826 | { |
631d5788 | 13827 | go_error_at(index_expr->location(), |
13828 | "duplicate value for index %lu", | |
13829 | index); | |
e440a328 | 13830 | return Expression::make_error(location); |
13831 | } | |
ffe743ca | 13832 | |
00773463 | 13833 | if (!indexes->empty() && index < indexes->back()) |
13834 | indexes_out_of_order = true; | |
13835 | ||
ffe743ca | 13836 | indexes->push_back(index); |
e440a328 | 13837 | } |
13838 | ||
ffe743ca | 13839 | vals->push_back(val); |
13840 | ||
e440a328 | 13841 | ++index; |
13842 | } | |
13843 | ||
ffe743ca | 13844 | if (indexes->empty()) |
13845 | { | |
13846 | delete indexes; | |
13847 | indexes = NULL; | |
13848 | } | |
e440a328 | 13849 | |
e32de7ba | 13850 | std::vector<unsigned long>* traverse_order = NULL; |
00773463 | 13851 | if (indexes_out_of_order) |
13852 | { | |
e32de7ba | 13853 | typedef std::vector<IVT_triple> V; |
00773463 | 13854 | |
13855 | V v; | |
13856 | v.reserve(indexes->size()); | |
13857 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
e32de7ba | 13858 | unsigned long torder = 0; |
00773463 | 13859 | for (Expression_list::const_iterator pe = vals->begin(); |
13860 | pe != vals->end(); | |
e32de7ba | 13861 | ++pe, ++pi, ++torder) |
13862 | v.push_back(IVT_triple(*pi, torder, *pe)); | |
00773463 | 13863 | |
e32de7ba | 13864 | std::sort(v.begin(), v.end()); |
00773463 | 13865 | |
13866 | delete indexes; | |
13867 | delete vals; | |
e32de7ba | 13868 | |
00773463 | 13869 | indexes = new std::vector<unsigned long>(); |
13870 | indexes->reserve(v.size()); | |
13871 | vals = new Expression_list(); | |
13872 | vals->reserve(v.size()); | |
e32de7ba | 13873 | traverse_order = new std::vector<unsigned long>(); |
13874 | traverse_order->reserve(v.size()); | |
00773463 | 13875 | |
13876 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13877 | { | |
e32de7ba | 13878 | indexes->push_back(p->index); |
13879 | vals->push_back(p->expr); | |
13880 | traverse_order->push_back(p->traversal_order); | |
00773463 | 13881 | } |
13882 | } | |
13883 | ||
e32de7ba | 13884 | Expression* ret = this->make_array(type, indexes, vals); |
13885 | Array_construction_expression* ace = ret->array_literal(); | |
13886 | if (ace != NULL && traverse_order != NULL) | |
13887 | ace->set_traverse_order(traverse_order); | |
13888 | return ret; | |
e440a328 | 13889 | } |
13890 | ||
13891 | // Actually build the array composite literal. This handles | |
13892 | // [...]{...}. | |
13893 | ||
13894 | Expression* | |
ffe743ca | 13895 | Composite_literal_expression::make_array( |
13896 | Type* type, | |
13897 | const std::vector<unsigned long>* indexes, | |
13898 | Expression_list* vals) | |
e440a328 | 13899 | { |
b13c66cd | 13900 | Location location = this->location(); |
e440a328 | 13901 | Array_type* at = type->array_type(); |
ffe743ca | 13902 | |
e440a328 | 13903 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13904 | { | |
ffe743ca | 13905 | size_t size; |
13906 | if (vals == NULL) | |
13907 | size = 0; | |
00773463 | 13908 | else if (indexes != NULL) |
13909 | size = indexes->back() + 1; | |
13910 | else | |
ffe743ca | 13911 | { |
13912 | size = vals->size(); | |
13913 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13914 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13915 | && size >> (it->bits() - 1) != 0) | |
13916 | { | |
631d5788 | 13917 | go_error_at(location, "too many elements in composite literal"); |
ffe743ca | 13918 | return Expression::make_error(location); |
13919 | } | |
13920 | } | |
ffe743ca | 13921 | |
e67508fa | 13922 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 13923 | at = Type::make_array_type(at->element_type(), elen); |
13924 | type = at; | |
13925 | } | |
ffe743ca | 13926 | else if (at->length() != NULL |
13927 | && !at->length()->is_error_expression() | |
13928 | && this->vals_ != NULL) | |
13929 | { | |
13930 | Numeric_constant nc; | |
13931 | unsigned long val; | |
13932 | if (at->length()->numeric_constant_value(&nc) | |
13933 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13934 | { | |
13935 | if (indexes == NULL) | |
13936 | { | |
13937 | if (this->vals_->size() > val) | |
13938 | { | |
631d5788 | 13939 | go_error_at(location, |
13940 | "too many elements in composite literal"); | |
ffe743ca | 13941 | return Expression::make_error(location); |
13942 | } | |
13943 | } | |
13944 | else | |
13945 | { | |
00773463 | 13946 | unsigned long max = indexes->back(); |
ffe743ca | 13947 | if (max >= val) |
13948 | { | |
631d5788 | 13949 | go_error_at(location, |
13950 | ("some element keys in composite literal " | |
13951 | "are out of range")); | |
ffe743ca | 13952 | return Expression::make_error(location); |
13953 | } | |
13954 | } | |
13955 | } | |
13956 | } | |
13957 | ||
e440a328 | 13958 | if (at->length() != NULL) |
ffe743ca | 13959 | return new Fixed_array_construction_expression(type, indexes, vals, |
13960 | location); | |
e440a328 | 13961 | else |
2c809f8f | 13962 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13963 | } |
13964 | ||
13965 | // Lower a map composite literal. | |
13966 | ||
13967 | Expression* | |
a287720d | 13968 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13969 | Statement_inserter* inserter, |
a287720d | 13970 | Type* type) |
e440a328 | 13971 | { |
b13c66cd | 13972 | Location location = this->location(); |
e440a328 | 13973 | if (this->vals_ != NULL) |
13974 | { | |
13975 | if (!this->has_keys_) | |
13976 | { | |
631d5788 | 13977 | go_error_at(location, "map composite literal must have keys"); |
e440a328 | 13978 | return Expression::make_error(location); |
13979 | } | |
13980 | ||
a287720d | 13981 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13982 | p != this->vals_->end(); |
13983 | p += 2) | |
13984 | { | |
13985 | if (*p == NULL) | |
13986 | { | |
13987 | ++p; | |
631d5788 | 13988 | go_error_at((*p)->location(), |
13989 | ("map composite literal must " | |
13990 | "have keys for every value")); | |
e440a328 | 13991 | return Expression::make_error(location); |
13992 | } | |
a287720d | 13993 | // Make sure we have lowered the key; it may not have been |
13994 | // lowered in order to handle keys for struct composite | |
13995 | // literals. Lower it now to get the right error message. | |
13996 | if ((*p)->unknown_expression() != NULL) | |
13997 | { | |
13998 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13999 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 14000 | go_assert((*p)->is_error_expression()); |
a287720d | 14001 | return Expression::make_error(location); |
14002 | } | |
e440a328 | 14003 | } |
14004 | } | |
14005 | ||
14006 | return new Map_construction_expression(type, this->vals_, location); | |
14007 | } | |
14008 | ||
d751bb78 | 14009 | // Dump ast representation for a composite literal expression. |
14010 | ||
14011 | void | |
14012 | Composite_literal_expression::do_dump_expression( | |
14013 | Ast_dump_context* ast_dump_context) const | |
14014 | { | |
8b1c301d | 14015 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 14016 | ast_dump_context->dump_type(this->type_); |
14017 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 14018 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 14019 | ast_dump_context->ostream() << "})"; |
14020 | } | |
14021 | ||
e440a328 | 14022 | // Make a composite literal expression. |
14023 | ||
14024 | Expression* | |
14025 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 14026 | Expression_list* vals, bool all_are_names, |
b13c66cd | 14027 | Location location) |
e440a328 | 14028 | { |
14029 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 14030 | all_are_names, location); |
e440a328 | 14031 | } |
14032 | ||
14033 | // Return whether this expression is a composite literal. | |
14034 | ||
14035 | bool | |
14036 | Expression::is_composite_literal() const | |
14037 | { | |
14038 | switch (this->classification_) | |
14039 | { | |
14040 | case EXPRESSION_COMPOSITE_LITERAL: | |
14041 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14042 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 14043 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14044 | case EXPRESSION_MAP_CONSTRUCTION: |
14045 | return true; | |
14046 | default: | |
14047 | return false; | |
14048 | } | |
14049 | } | |
14050 | ||
14051 | // Return whether this expression is a composite literal which is not | |
14052 | // constant. | |
14053 | ||
14054 | bool | |
14055 | Expression::is_nonconstant_composite_literal() const | |
14056 | { | |
14057 | switch (this->classification_) | |
14058 | { | |
14059 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14060 | { | |
14061 | const Struct_construction_expression *psce = | |
14062 | static_cast<const Struct_construction_expression*>(this); | |
14063 | return !psce->is_constant_struct(); | |
14064 | } | |
14065 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
14066 | { | |
14067 | const Fixed_array_construction_expression *pace = | |
14068 | static_cast<const Fixed_array_construction_expression*>(this); | |
14069 | return !pace->is_constant_array(); | |
14070 | } | |
2c809f8f | 14071 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14072 | { |
2c809f8f | 14073 | const Slice_construction_expression *pace = |
14074 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 14075 | return !pace->is_constant_array(); |
14076 | } | |
14077 | case EXPRESSION_MAP_CONSTRUCTION: | |
14078 | return true; | |
14079 | default: | |
14080 | return false; | |
14081 | } | |
14082 | } | |
14083 | ||
35a54f17 | 14084 | // Return true if this is a variable or temporary_variable. |
14085 | ||
14086 | bool | |
14087 | Expression::is_variable() const | |
14088 | { | |
14089 | switch (this->classification_) | |
14090 | { | |
14091 | case EXPRESSION_VAR_REFERENCE: | |
14092 | case EXPRESSION_TEMPORARY_REFERENCE: | |
14093 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
b0c09712 | 14094 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
35a54f17 | 14095 | return true; |
14096 | default: | |
14097 | return false; | |
14098 | } | |
14099 | } | |
14100 | ||
e440a328 | 14101 | // Return true if this is a reference to a local variable. |
14102 | ||
14103 | bool | |
14104 | Expression::is_local_variable() const | |
14105 | { | |
14106 | const Var_expression* ve = this->var_expression(); | |
14107 | if (ve == NULL) | |
14108 | return false; | |
14109 | const Named_object* no = ve->named_object(); | |
14110 | return (no->is_result_variable() | |
14111 | || (no->is_variable() && !no->var_value()->is_global())); | |
14112 | } | |
14113 | ||
14114 | // Class Type_guard_expression. | |
14115 | ||
14116 | // Traversal. | |
14117 | ||
14118 | int | |
14119 | Type_guard_expression::do_traverse(Traverse* traverse) | |
14120 | { | |
14121 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
14122 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14123 | return TRAVERSE_EXIT; | |
14124 | return TRAVERSE_CONTINUE; | |
14125 | } | |
14126 | ||
2c809f8f | 14127 | Expression* |
14128 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
14129 | Statement_inserter* inserter) | |
14130 | { | |
5bf8be8b | 14131 | if (this->expr_->is_error_expression() |
14132 | || this->expr_->type()->is_error_type()) | |
14133 | { | |
14134 | go_assert(saw_errors()); | |
14135 | return Expression::make_error(this->location()); | |
14136 | } | |
14137 | ||
2c809f8f | 14138 | if (!this->expr_->is_variable()) |
14139 | { | |
14140 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
14141 | this->location()); | |
14142 | inserter->insert(temp); | |
14143 | this->expr_ = | |
14144 | Expression::make_temporary_reference(temp, this->location()); | |
14145 | } | |
14146 | return this; | |
14147 | } | |
14148 | ||
e440a328 | 14149 | // Check types of a type guard expression. The expression must have |
14150 | // an interface type, but the actual type conversion is checked at run | |
14151 | // time. | |
14152 | ||
14153 | void | |
14154 | Type_guard_expression::do_check_types(Gogo*) | |
14155 | { | |
e440a328 | 14156 | Type* expr_type = this->expr_->type(); |
7e9da23f | 14157 | if (expr_type->interface_type() == NULL) |
f725ade8 | 14158 | { |
5c13bd80 | 14159 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 14160 | this->report_error(_("type assertion only valid for interface types")); |
14161 | this->set_is_error(); | |
14162 | } | |
e440a328 | 14163 | else if (this->type_->interface_type() == NULL) |
14164 | { | |
14165 | std::string reason; | |
14166 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
14167 | &reason)) | |
14168 | { | |
5c13bd80 | 14169 | if (!this->type_->is_error()) |
e440a328 | 14170 | { |
f725ade8 | 14171 | if (reason.empty()) |
14172 | this->report_error(_("impossible type assertion: " | |
14173 | "type does not implement interface")); | |
14174 | else | |
631d5788 | 14175 | go_error_at(this->location(), |
14176 | ("impossible type assertion: " | |
14177 | "type does not implement interface (%s)"), | |
14178 | reason.c_str()); | |
e440a328 | 14179 | } |
f725ade8 | 14180 | this->set_is_error(); |
e440a328 | 14181 | } |
14182 | } | |
14183 | } | |
14184 | ||
ea664253 | 14185 | // Return the backend representation for a type guard expression. |
e440a328 | 14186 | |
ea664253 | 14187 | Bexpression* |
14188 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14189 | { |
2c809f8f | 14190 | Expression* conversion; |
7e9da23f | 14191 | if (this->type_->interface_type() != NULL) |
2c809f8f | 14192 | conversion = |
14193 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
14194 | true, this->location()); | |
e440a328 | 14195 | else |
2c809f8f | 14196 | conversion = |
14197 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
14198 | this->expr_, this->location()); | |
14199 | ||
21b70e8f | 14200 | Gogo* gogo = context->gogo(); |
14201 | Btype* bt = this->type_->get_backend(gogo); | |
14202 | Bexpression* bexpr = conversion->get_backend(context); | |
14203 | return gogo->backend()->convert_expression(bt, bexpr, this->location()); | |
e440a328 | 14204 | } |
14205 | ||
d751bb78 | 14206 | // Dump ast representation for a type guard expression. |
14207 | ||
14208 | void | |
2c809f8f | 14209 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 14210 | const |
14211 | { | |
14212 | this->expr_->dump_expression(ast_dump_context); | |
14213 | ast_dump_context->ostream() << "."; | |
14214 | ast_dump_context->dump_type(this->type_); | |
14215 | } | |
14216 | ||
e440a328 | 14217 | // Make a type guard expression. |
14218 | ||
14219 | Expression* | |
14220 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 14221 | Location location) |
e440a328 | 14222 | { |
14223 | return new Type_guard_expression(expr, type, location); | |
14224 | } | |
14225 | ||
2c809f8f | 14226 | // Class Heap_expression. |
e440a328 | 14227 | |
da244e59 | 14228 | // Return the type of the expression stored on the heap. |
e440a328 | 14229 | |
da244e59 | 14230 | Type* |
14231 | Heap_expression::do_type() | |
14232 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 14233 | |
ea664253 | 14234 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 14235 | |
ea664253 | 14236 | Bexpression* |
14237 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14238 | { |
03118c21 | 14239 | Type* etype = this->expr_->type(); |
14240 | if (this->expr_->is_error_expression() || etype->is_error()) | |
ea664253 | 14241 | return context->backend()->error_expression(); |
2c809f8f | 14242 | |
02c19a1a | 14243 | Location loc = this->location(); |
2c809f8f | 14244 | Gogo* gogo = context->gogo(); |
02c19a1a | 14245 | Btype* btype = this->type()->get_backend(gogo); |
45ff893b | 14246 | |
03118c21 | 14247 | Expression* alloc = Expression::make_allocation(etype, loc); |
5973ede0 | 14248 | if (this->allocate_on_stack_) |
45ff893b | 14249 | alloc->allocation_expression()->set_allocate_on_stack(); |
14250 | Bexpression* space = alloc->get_backend(context); | |
02c19a1a | 14251 | |
14252 | Bstatement* decl; | |
14253 | Named_object* fn = context->function(); | |
14254 | go_assert(fn != NULL); | |
14255 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
14256 | Bvariable* space_temp = | |
14257 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
14258 | space, true, loc, &decl); | |
03118c21 | 14259 | Btype* expr_btype = etype->get_backend(gogo); |
02c19a1a | 14260 | |
ea664253 | 14261 | Bexpression* bexpr = this->expr_->get_backend(context); |
03118c21 | 14262 | |
14263 | // If this assignment needs a write barrier, call typedmemmove. We | |
14264 | // don't do this in the write barrier pass because in some cases | |
14265 | // backend conversion can introduce new Heap_expression values. | |
14266 | Bstatement* assn; | |
06e83d10 | 14267 | if (!etype->has_pointer() || this->allocate_on_stack_) |
03118c21 | 14268 | { |
7af8e400 | 14269 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14270 | Bexpression* ref = |
14271 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
14272 | assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc); | |
14273 | } | |
14274 | else | |
14275 | { | |
14276 | Bstatement* edecl; | |
14277 | Bvariable* btemp = | |
14278 | gogo->backend()->temporary_variable(fndecl, context->bblock(), | |
14279 | expr_btype, bexpr, true, loc, | |
14280 | &edecl); | |
14281 | Bexpression* btempref = gogo->backend()->var_expression(btemp, | |
7af8e400 | 14282 | loc); |
03118c21 | 14283 | Bexpression* addr = gogo->backend()->address_expression(btempref, loc); |
14284 | ||
14285 | Expression* td = Expression::make_type_descriptor(etype, loc); | |
14286 | Type* etype_ptr = Type::make_pointer_type(etype); | |
7af8e400 | 14287 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14288 | Expression* elhs = Expression::make_backend(space, etype_ptr, loc); |
14289 | Expression* erhs = Expression::make_backend(addr, etype_ptr, loc); | |
14290 | Expression* call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3, | |
14291 | td, elhs, erhs); | |
14292 | Bexpression* bcall = call->get_backend(context); | |
14293 | Bstatement* s = gogo->backend()->expression_statement(fndecl, bcall); | |
14294 | assn = gogo->backend()->compound_statement(edecl, s); | |
14295 | } | |
02c19a1a | 14296 | decl = gogo->backend()->compound_statement(decl, assn); |
7af8e400 | 14297 | space = gogo->backend()->var_expression(space_temp, loc); |
ea664253 | 14298 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 14299 | } |
14300 | ||
2c809f8f | 14301 | // Dump ast representation for a heap expression. |
d751bb78 | 14302 | |
14303 | void | |
2c809f8f | 14304 | Heap_expression::do_dump_expression( |
d751bb78 | 14305 | Ast_dump_context* ast_dump_context) const |
14306 | { | |
14307 | ast_dump_context->ostream() << "&("; | |
14308 | ast_dump_context->dump_expression(this->expr_); | |
14309 | ast_dump_context->ostream() << ")"; | |
14310 | } | |
14311 | ||
2c809f8f | 14312 | // Allocate an expression on the heap. |
e440a328 | 14313 | |
14314 | Expression* | |
2c809f8f | 14315 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 14316 | { |
2c809f8f | 14317 | return new Heap_expression(expr, location); |
e440a328 | 14318 | } |
14319 | ||
14320 | // Class Receive_expression. | |
14321 | ||
14322 | // Return the type of a receive expression. | |
14323 | ||
14324 | Type* | |
14325 | Receive_expression::do_type() | |
14326 | { | |
e429e3bd | 14327 | if (this->is_error_expression()) |
14328 | return Type::make_error_type(); | |
e440a328 | 14329 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
14330 | if (channel_type == NULL) | |
e429e3bd | 14331 | { |
14332 | this->report_error(_("expected channel")); | |
14333 | return Type::make_error_type(); | |
14334 | } | |
e440a328 | 14335 | return channel_type->element_type(); |
14336 | } | |
14337 | ||
14338 | // Check types for a receive expression. | |
14339 | ||
14340 | void | |
14341 | Receive_expression::do_check_types(Gogo*) | |
14342 | { | |
14343 | Type* type = this->channel_->type(); | |
5c13bd80 | 14344 | if (type->is_error()) |
e440a328 | 14345 | { |
e429e3bd | 14346 | go_assert(saw_errors()); |
e440a328 | 14347 | this->set_is_error(); |
14348 | return; | |
14349 | } | |
14350 | if (type->channel_type() == NULL) | |
14351 | { | |
14352 | this->report_error(_("expected channel")); | |
14353 | return; | |
14354 | } | |
14355 | if (!type->channel_type()->may_receive()) | |
14356 | { | |
14357 | this->report_error(_("invalid receive on send-only channel")); | |
14358 | return; | |
14359 | } | |
14360 | } | |
14361 | ||
2c809f8f | 14362 | // Flattening for receive expressions creates a temporary variable to store |
14363 | // received data in for receives. | |
14364 | ||
14365 | Expression* | |
14366 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
14367 | Statement_inserter* inserter) | |
14368 | { | |
14369 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
14370 | if (channel_type == NULL) | |
14371 | { | |
14372 | go_assert(saw_errors()); | |
14373 | return this; | |
14374 | } | |
5bf8be8b | 14375 | else if (this->channel_->is_error_expression()) |
14376 | { | |
14377 | go_assert(saw_errors()); | |
14378 | return Expression::make_error(this->location()); | |
14379 | } | |
2c809f8f | 14380 | |
14381 | Type* element_type = channel_type->element_type(); | |
14382 | if (this->temp_receiver_ == NULL) | |
14383 | { | |
14384 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
14385 | this->location()); | |
14386 | this->temp_receiver_->set_is_address_taken(); | |
14387 | inserter->insert(this->temp_receiver_); | |
14388 | } | |
14389 | ||
14390 | return this; | |
14391 | } | |
14392 | ||
ea664253 | 14393 | // Get the backend representation for a receive expression. |
e440a328 | 14394 | |
ea664253 | 14395 | Bexpression* |
14396 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14397 | { |
f24f10bb | 14398 | Location loc = this->location(); |
14399 | ||
e440a328 | 14400 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 14401 | if (channel_type == NULL) |
14402 | { | |
c484d925 | 14403 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 14404 | return context->backend()->error_expression(); |
5b8368f4 | 14405 | } |
e440a328 | 14406 | |
2c809f8f | 14407 | Expression* recv_ref = |
14408 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14409 | Expression* recv_addr = | |
14410 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14411 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
e36a5ff5 | 14412 | Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 2, |
14413 | this->channel_, recv_addr); | |
ea664253 | 14414 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 14415 | } |
14416 | ||
d751bb78 | 14417 | // Dump ast representation for a receive expression. |
14418 | ||
14419 | void | |
14420 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14421 | { | |
14422 | ast_dump_context->ostream() << " <- " ; | |
14423 | ast_dump_context->dump_expression(channel_); | |
14424 | } | |
14425 | ||
e440a328 | 14426 | // Make a receive expression. |
14427 | ||
14428 | Receive_expression* | |
b13c66cd | 14429 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 14430 | { |
14431 | return new Receive_expression(channel, location); | |
14432 | } | |
14433 | ||
e440a328 | 14434 | // An expression which evaluates to a pointer to the type descriptor |
14435 | // of a type. | |
14436 | ||
14437 | class Type_descriptor_expression : public Expression | |
14438 | { | |
14439 | public: | |
b13c66cd | 14440 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 14441 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
14442 | type_(type) | |
14443 | { } | |
14444 | ||
14445 | protected: | |
4b686186 | 14446 | int |
14447 | do_traverse(Traverse*); | |
14448 | ||
e440a328 | 14449 | Type* |
14450 | do_type() | |
14451 | { return Type::make_type_descriptor_ptr_type(); } | |
14452 | ||
f9ca30f9 | 14453 | bool |
3ae06f68 | 14454 | do_is_static_initializer() const |
f9ca30f9 | 14455 | { return true; } |
14456 | ||
e440a328 | 14457 | void |
14458 | do_determine_type(const Type_context*) | |
14459 | { } | |
14460 | ||
14461 | Expression* | |
14462 | do_copy() | |
14463 | { return this; } | |
14464 | ||
ea664253 | 14465 | Bexpression* |
14466 | do_get_backend(Translate_context* context) | |
a1d23b41 | 14467 | { |
ea664253 | 14468 | return this->type_->type_descriptor_pointer(context->gogo(), |
14469 | this->location()); | |
a1d23b41 | 14470 | } |
e440a328 | 14471 | |
d751bb78 | 14472 | void |
14473 | do_dump_expression(Ast_dump_context*) const; | |
14474 | ||
e440a328 | 14475 | private: |
14476 | // The type for which this is the descriptor. | |
14477 | Type* type_; | |
14478 | }; | |
14479 | ||
4b686186 | 14480 | int |
14481 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
14482 | { | |
14483 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14484 | return TRAVERSE_EXIT; | |
14485 | return TRAVERSE_CONTINUE; | |
14486 | } | |
14487 | ||
d751bb78 | 14488 | // Dump ast representation for a type descriptor expression. |
14489 | ||
14490 | void | |
14491 | Type_descriptor_expression::do_dump_expression( | |
14492 | Ast_dump_context* ast_dump_context) const | |
14493 | { | |
14494 | ast_dump_context->dump_type(this->type_); | |
14495 | } | |
14496 | ||
e440a328 | 14497 | // Make a type descriptor expression. |
14498 | ||
14499 | Expression* | |
b13c66cd | 14500 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14501 | { |
14502 | return new Type_descriptor_expression(type, location); | |
14503 | } | |
14504 | ||
aa5ae575 | 14505 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
14506 | // of a type. | |
14507 | ||
14508 | class GC_symbol_expression : public Expression | |
14509 | { | |
14510 | public: | |
14511 | GC_symbol_expression(Type* type) | |
14512 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
14513 | type_(type) | |
14514 | {} | |
14515 | ||
14516 | protected: | |
14517 | Type* | |
14518 | do_type() | |
03118c21 | 14519 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } |
aa5ae575 | 14520 | |
14521 | bool | |
3ae06f68 | 14522 | do_is_static_initializer() const |
aa5ae575 | 14523 | { return true; } |
14524 | ||
14525 | void | |
14526 | do_determine_type(const Type_context*) | |
14527 | { } | |
14528 | ||
14529 | Expression* | |
14530 | do_copy() | |
14531 | { return this; } | |
14532 | ||
14533 | Bexpression* | |
14534 | do_get_backend(Translate_context* context) | |
14535 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
14536 | ||
14537 | void | |
14538 | do_dump_expression(Ast_dump_context*) const; | |
14539 | ||
14540 | private: | |
14541 | // The type which this gc symbol describes. | |
14542 | Type* type_; | |
14543 | }; | |
14544 | ||
14545 | // Dump ast representation for a gc symbol expression. | |
14546 | ||
14547 | void | |
14548 | GC_symbol_expression::do_dump_expression( | |
14549 | Ast_dump_context* ast_dump_context) const | |
14550 | { | |
14551 | ast_dump_context->ostream() << "gcdata("; | |
14552 | ast_dump_context->dump_type(this->type_); | |
14553 | ast_dump_context->ostream() << ")"; | |
14554 | } | |
14555 | ||
14556 | // Make a gc symbol expression. | |
14557 | ||
14558 | Expression* | |
14559 | Expression::make_gc_symbol(Type* type) | |
14560 | { | |
14561 | return new GC_symbol_expression(type); | |
14562 | } | |
14563 | ||
03118c21 | 14564 | // An expression that evaluates to a pointer to a symbol holding the |
14565 | // ptrmask data of a type. | |
14566 | ||
14567 | class Ptrmask_symbol_expression : public Expression | |
14568 | { | |
14569 | public: | |
14570 | Ptrmask_symbol_expression(Type* type) | |
14571 | : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()), | |
14572 | type_(type) | |
14573 | {} | |
14574 | ||
14575 | protected: | |
14576 | Type* | |
14577 | do_type() | |
14578 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } | |
14579 | ||
14580 | bool | |
14581 | do_is_static_initializer() const | |
14582 | { return true; } | |
14583 | ||
14584 | void | |
14585 | do_determine_type(const Type_context*) | |
14586 | { } | |
14587 | ||
14588 | Expression* | |
14589 | do_copy() | |
14590 | { return this; } | |
14591 | ||
14592 | Bexpression* | |
14593 | do_get_backend(Translate_context*); | |
14594 | ||
14595 | void | |
14596 | do_dump_expression(Ast_dump_context*) const; | |
14597 | ||
14598 | private: | |
14599 | // The type that this ptrmask symbol describes. | |
14600 | Type* type_; | |
14601 | }; | |
14602 | ||
14603 | // Return the ptrmask variable. | |
14604 | ||
14605 | Bexpression* | |
14606 | Ptrmask_symbol_expression::do_get_backend(Translate_context* context) | |
14607 | { | |
14608 | Gogo* gogo = context->gogo(); | |
14609 | ||
14610 | // If this type does not need a gcprog, then we can use the standard | |
14611 | // GC symbol. | |
14612 | int64_t ptrsize, ptrdata; | |
14613 | if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata)) | |
14614 | return this->type_->gc_symbol_pointer(gogo); | |
14615 | ||
14616 | // Otherwise we have to build a ptrmask variable, and return a | |
14617 | // pointer to it. | |
14618 | ||
14619 | Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata); | |
14620 | Location bloc = Linemap::predeclared_location(); | |
7af8e400 | 14621 | Bexpression* bref = gogo->backend()->var_expression(bvar, bloc); |
03118c21 | 14622 | Bexpression* baddr = gogo->backend()->address_expression(bref, bloc); |
14623 | ||
14624 | Type* uint8_type = Type::lookup_integer_type("uint8"); | |
14625 | Type* pointer_uint8_type = Type::make_pointer_type(uint8_type); | |
14626 | Btype* ubtype = pointer_uint8_type->get_backend(gogo); | |
14627 | return gogo->backend()->convert_expression(ubtype, baddr, bloc); | |
14628 | } | |
14629 | ||
14630 | // Dump AST for a ptrmask symbol expression. | |
14631 | ||
14632 | void | |
14633 | Ptrmask_symbol_expression::do_dump_expression( | |
14634 | Ast_dump_context* ast_dump_context) const | |
14635 | { | |
14636 | ast_dump_context->ostream() << "ptrmask("; | |
14637 | ast_dump_context->dump_type(this->type_); | |
14638 | ast_dump_context->ostream() << ")"; | |
14639 | } | |
14640 | ||
14641 | // Make a ptrmask symbol expression. | |
14642 | ||
14643 | Expression* | |
14644 | Expression::make_ptrmask_symbol(Type* type) | |
14645 | { | |
14646 | return new Ptrmask_symbol_expression(type); | |
14647 | } | |
14648 | ||
e440a328 | 14649 | // An expression which evaluates to some characteristic of a type. |
14650 | // This is only used to initialize fields of a type descriptor. Using | |
14651 | // a new expression class is slightly inefficient but gives us a good | |
14652 | // separation between the frontend and the middle-end with regard to | |
14653 | // how types are laid out. | |
14654 | ||
14655 | class Type_info_expression : public Expression | |
14656 | { | |
14657 | public: | |
14658 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14659 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14660 | type_(type), type_info_(type_info) |
14661 | { } | |
14662 | ||
14663 | protected: | |
0e168074 | 14664 | bool |
3ae06f68 | 14665 | do_is_static_initializer() const |
0e168074 | 14666 | { return true; } |
14667 | ||
e440a328 | 14668 | Type* |
14669 | do_type(); | |
14670 | ||
14671 | void | |
14672 | do_determine_type(const Type_context*) | |
14673 | { } | |
14674 | ||
14675 | Expression* | |
14676 | do_copy() | |
14677 | { return this; } | |
14678 | ||
ea664253 | 14679 | Bexpression* |
14680 | do_get_backend(Translate_context* context); | |
e440a328 | 14681 | |
d751bb78 | 14682 | void |
14683 | do_dump_expression(Ast_dump_context*) const; | |
14684 | ||
e440a328 | 14685 | private: |
14686 | // The type for which we are getting information. | |
14687 | Type* type_; | |
14688 | // What information we want. | |
14689 | Type_info type_info_; | |
14690 | }; | |
14691 | ||
14692 | // The type is chosen to match what the type descriptor struct | |
14693 | // expects. | |
14694 | ||
14695 | Type* | |
14696 | Type_info_expression::do_type() | |
14697 | { | |
14698 | switch (this->type_info_) | |
14699 | { | |
14700 | case TYPE_INFO_SIZE: | |
03118c21 | 14701 | case TYPE_INFO_BACKEND_PTRDATA: |
14702 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
e440a328 | 14703 | return Type::lookup_integer_type("uintptr"); |
14704 | case TYPE_INFO_ALIGNMENT: | |
14705 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14706 | return Type::lookup_integer_type("uint8"); | |
14707 | default: | |
c3e6f413 | 14708 | go_unreachable(); |
e440a328 | 14709 | } |
14710 | } | |
14711 | ||
ea664253 | 14712 | // Return the backend representation for type information. |
e440a328 | 14713 | |
ea664253 | 14714 | Bexpression* |
14715 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14716 | { |
927a01eb | 14717 | Gogo* gogo = context->gogo(); |
2a305b85 | 14718 | bool ok = true; |
3f378015 | 14719 | int64_t val; |
927a01eb | 14720 | switch (this->type_info_) |
e440a328 | 14721 | { |
927a01eb | 14722 | case TYPE_INFO_SIZE: |
2a305b85 | 14723 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 14724 | break; |
14725 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 14726 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 14727 | break; |
14728 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 14729 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 14730 | break; |
03118c21 | 14731 | case TYPE_INFO_BACKEND_PTRDATA: |
14732 | ok = this->type_->backend_type_ptrdata(gogo, &val); | |
14733 | break; | |
14734 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
14735 | ok = this->type_->descriptor_ptrdata(gogo, &val); | |
14736 | break; | |
927a01eb | 14737 | default: |
14738 | go_unreachable(); | |
e440a328 | 14739 | } |
2a305b85 | 14740 | if (!ok) |
14741 | { | |
14742 | go_assert(saw_errors()); | |
14743 | return gogo->backend()->error_expression(); | |
14744 | } | |
3f378015 | 14745 | Expression* e = Expression::make_integer_int64(val, this->type(), |
14746 | this->location()); | |
14747 | return e->get_backend(context); | |
e440a328 | 14748 | } |
14749 | ||
d751bb78 | 14750 | // Dump ast representation for a type info expression. |
14751 | ||
14752 | void | |
14753 | Type_info_expression::do_dump_expression( | |
14754 | Ast_dump_context* ast_dump_context) const | |
14755 | { | |
14756 | ast_dump_context->ostream() << "typeinfo("; | |
14757 | ast_dump_context->dump_type(this->type_); | |
14758 | ast_dump_context->ostream() << ","; | |
14759 | ast_dump_context->ostream() << | |
14760 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14761 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
03118c21 | 14762 | : this->type_info_ == TYPE_INFO_SIZE ? "size" |
14763 | : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata" | |
14764 | : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata" | |
d751bb78 | 14765 | : "unknown"); |
14766 | ast_dump_context->ostream() << ")"; | |
14767 | } | |
14768 | ||
e440a328 | 14769 | // Make a type info expression. |
14770 | ||
14771 | Expression* | |
14772 | Expression::make_type_info(Type* type, Type_info type_info) | |
14773 | { | |
14774 | return new Type_info_expression(type, type_info); | |
14775 | } | |
14776 | ||
35a54f17 | 14777 | // An expression that evaluates to some characteristic of a slice. |
14778 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14779 | ||
14780 | class Slice_info_expression : public Expression | |
14781 | { | |
14782 | public: | |
14783 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14784 | Location location) | |
14785 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14786 | slice_(slice), slice_info_(slice_info) | |
14787 | { } | |
14788 | ||
14789 | protected: | |
14790 | Type* | |
14791 | do_type(); | |
14792 | ||
14793 | void | |
14794 | do_determine_type(const Type_context*) | |
14795 | { } | |
14796 | ||
14797 | Expression* | |
14798 | do_copy() | |
14799 | { | |
14800 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14801 | this->location()); | |
14802 | } | |
14803 | ||
ea664253 | 14804 | Bexpression* |
14805 | do_get_backend(Translate_context* context); | |
35a54f17 | 14806 | |
14807 | void | |
14808 | do_dump_expression(Ast_dump_context*) const; | |
14809 | ||
14810 | void | |
14811 | do_issue_nil_check() | |
14812 | { this->slice_->issue_nil_check(); } | |
14813 | ||
14814 | private: | |
14815 | // The slice for which we are getting information. | |
14816 | Expression* slice_; | |
14817 | // What information we want. | |
14818 | Slice_info slice_info_; | |
14819 | }; | |
14820 | ||
14821 | // Return the type of the slice info. | |
14822 | ||
14823 | Type* | |
14824 | Slice_info_expression::do_type() | |
14825 | { | |
14826 | switch (this->slice_info_) | |
14827 | { | |
14828 | case SLICE_INFO_VALUE_POINTER: | |
14829 | return Type::make_pointer_type( | |
14830 | this->slice_->type()->array_type()->element_type()); | |
14831 | case SLICE_INFO_LENGTH: | |
14832 | case SLICE_INFO_CAPACITY: | |
14833 | return Type::lookup_integer_type("int"); | |
14834 | default: | |
14835 | go_unreachable(); | |
14836 | } | |
14837 | } | |
14838 | ||
ea664253 | 14839 | // Return the backend information for slice information. |
35a54f17 | 14840 | |
ea664253 | 14841 | Bexpression* |
14842 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 14843 | { |
14844 | Gogo* gogo = context->gogo(); | |
ea664253 | 14845 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 14846 | switch (this->slice_info_) |
14847 | { | |
14848 | case SLICE_INFO_VALUE_POINTER: | |
14849 | case SLICE_INFO_LENGTH: | |
14850 | case SLICE_INFO_CAPACITY: | |
ea664253 | 14851 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
14852 | this->location()); | |
35a54f17 | 14853 | break; |
14854 | default: | |
14855 | go_unreachable(); | |
14856 | } | |
35a54f17 | 14857 | } |
14858 | ||
14859 | // Dump ast representation for a type info expression. | |
14860 | ||
14861 | void | |
14862 | Slice_info_expression::do_dump_expression( | |
14863 | Ast_dump_context* ast_dump_context) const | |
14864 | { | |
14865 | ast_dump_context->ostream() << "sliceinfo("; | |
14866 | this->slice_->dump_expression(ast_dump_context); | |
14867 | ast_dump_context->ostream() << ","; | |
14868 | ast_dump_context->ostream() << | |
14869 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14870 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14871 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14872 | : "unknown"); | |
14873 | ast_dump_context->ostream() << ")"; | |
14874 | } | |
14875 | ||
14876 | // Make a slice info expression. | |
14877 | ||
14878 | Expression* | |
14879 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14880 | Location location) | |
14881 | { | |
14882 | return new Slice_info_expression(slice, slice_info, location); | |
14883 | } | |
14884 | ||
2c809f8f | 14885 | // An expression that represents a slice value: a struct with value pointer, |
14886 | // length, and capacity fields. | |
14887 | ||
14888 | class Slice_value_expression : public Expression | |
14889 | { | |
14890 | public: | |
14891 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14892 | Expression* cap, Location location) | |
14893 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14894 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14895 | { } | |
14896 | ||
14897 | protected: | |
14898 | int | |
14899 | do_traverse(Traverse*); | |
14900 | ||
14901 | Type* | |
14902 | do_type() | |
14903 | { return this->type_; } | |
14904 | ||
14905 | void | |
14906 | do_determine_type(const Type_context*) | |
14907 | { go_unreachable(); } | |
14908 | ||
14909 | Expression* | |
14910 | do_copy() | |
14911 | { | |
14912 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14913 | this->len_->copy(), this->cap_->copy(), | |
14914 | this->location()); | |
14915 | } | |
14916 | ||
ea664253 | 14917 | Bexpression* |
14918 | do_get_backend(Translate_context* context); | |
2c809f8f | 14919 | |
14920 | void | |
14921 | do_dump_expression(Ast_dump_context*) const; | |
14922 | ||
14923 | private: | |
14924 | // The type of the slice value. | |
14925 | Type* type_; | |
14926 | // The pointer to the values in the slice. | |
14927 | Expression* valptr_; | |
14928 | // The length of the slice. | |
14929 | Expression* len_; | |
14930 | // The capacity of the slice. | |
14931 | Expression* cap_; | |
14932 | }; | |
14933 | ||
14934 | int | |
14935 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14936 | { | |
55e8ba6a | 14937 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
14938 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 14939 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
14940 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14941 | return TRAVERSE_EXIT; | |
14942 | return TRAVERSE_CONTINUE; | |
14943 | } | |
14944 | ||
ea664253 | 14945 | Bexpression* |
14946 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14947 | { |
14948 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14949 | vals[0] = this->valptr_->get_backend(context); |
14950 | vals[1] = this->len_->get_backend(context); | |
14951 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14952 | |
14953 | Gogo* gogo = context->gogo(); | |
14954 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14955 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14956 | } |
14957 | ||
14958 | void | |
14959 | Slice_value_expression::do_dump_expression( | |
14960 | Ast_dump_context* ast_dump_context) const | |
14961 | { | |
14962 | ast_dump_context->ostream() << "slicevalue("; | |
14963 | ast_dump_context->ostream() << "values: "; | |
14964 | this->valptr_->dump_expression(ast_dump_context); | |
14965 | ast_dump_context->ostream() << ", length: "; | |
14966 | this->len_->dump_expression(ast_dump_context); | |
14967 | ast_dump_context->ostream() << ", capacity: "; | |
14968 | this->cap_->dump_expression(ast_dump_context); | |
14969 | ast_dump_context->ostream() << ")"; | |
14970 | } | |
14971 | ||
14972 | Expression* | |
14973 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14974 | Expression* cap, Location location) | |
14975 | { | |
14976 | go_assert(at->is_slice_type()); | |
14977 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14978 | } | |
2387f644 | 14979 | |
14980 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14981 | // This is used to access the method table or underlying object of an interface. | |
14982 | ||
14983 | class Interface_info_expression : public Expression | |
14984 | { | |
14985 | public: | |
14986 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 14987 | Location location) |
2387f644 | 14988 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
14989 | iface_(iface), iface_info_(iface_info) | |
14990 | { } | |
14991 | ||
14992 | protected: | |
14993 | Type* | |
14994 | do_type(); | |
14995 | ||
14996 | void | |
14997 | do_determine_type(const Type_context*) | |
14998 | { } | |
14999 | ||
15000 | Expression* | |
15001 | do_copy() | |
15002 | { | |
15003 | return new Interface_info_expression(this->iface_->copy(), | |
15004 | this->iface_info_, this->location()); | |
15005 | } | |
15006 | ||
ea664253 | 15007 | Bexpression* |
15008 | do_get_backend(Translate_context* context); | |
2387f644 | 15009 | |
15010 | void | |
15011 | do_dump_expression(Ast_dump_context*) const; | |
15012 | ||
15013 | void | |
15014 | do_issue_nil_check() | |
15015 | { this->iface_->issue_nil_check(); } | |
15016 | ||
15017 | private: | |
15018 | // The interface for which we are getting information. | |
15019 | Expression* iface_; | |
15020 | // What information we want. | |
15021 | Interface_info iface_info_; | |
15022 | }; | |
15023 | ||
15024 | // Return the type of the interface info. | |
15025 | ||
15026 | Type* | |
15027 | Interface_info_expression::do_type() | |
15028 | { | |
15029 | switch (this->iface_info_) | |
15030 | { | |
15031 | case INTERFACE_INFO_METHODS: | |
15032 | { | |
625d3118 | 15033 | typedef Unordered_map(Interface_type*, Type*) Hashtable; |
15034 | static Hashtable result_types; | |
15035 | ||
15036 | Interface_type* itype = this->iface_->type()->interface_type(); | |
15037 | ||
15038 | Hashtable::const_iterator p = result_types.find(itype); | |
15039 | if (p != result_types.end()) | |
15040 | return p->second; | |
15041 | ||
2c809f8f | 15042 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
625d3118 | 15043 | if (itype->is_empty()) |
15044 | { | |
15045 | result_types[itype] = pdt; | |
15046 | return pdt; | |
15047 | } | |
2c809f8f | 15048 | |
2387f644 | 15049 | Location loc = this->location(); |
15050 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 15051 | sfl->push_back( |
15052 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
15053 | ||
2387f644 | 15054 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); |
15055 | p != itype->methods()->end(); | |
15056 | ++p) | |
15057 | { | |
15058 | Function_type* ft = p->type()->function_type(); | |
15059 | go_assert(ft->receiver() == NULL); | |
15060 | ||
15061 | const Typed_identifier_list* params = ft->parameters(); | |
15062 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
15063 | if (params != NULL) | |
15064 | mparams->reserve(params->size() + 1); | |
15065 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
15066 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
15067 | if (params != NULL) | |
15068 | { | |
15069 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
15070 | pp != params->end(); | |
15071 | ++pp) | |
15072 | mparams->push_back(*pp); | |
15073 | } | |
15074 | ||
15075 | Typed_identifier_list* mresults = (ft->results() == NULL | |
15076 | ? NULL | |
15077 | : ft->results()->copy()); | |
15078 | Backend_function_type* mft = | |
15079 | Type::make_backend_function_type(NULL, mparams, mresults, | |
15080 | ft->location()); | |
15081 | ||
15082 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
15083 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
15084 | } | |
15085 | ||
6bf4793c | 15086 | Struct_type* st = Type::make_struct_type(sfl, loc); |
15087 | st->set_is_struct_incomparable(); | |
15088 | Pointer_type *pt = Type::make_pointer_type(st); | |
625d3118 | 15089 | result_types[itype] = pt; |
15090 | return pt; | |
2387f644 | 15091 | } |
15092 | case INTERFACE_INFO_OBJECT: | |
15093 | return Type::make_pointer_type(Type::make_void_type()); | |
15094 | default: | |
15095 | go_unreachable(); | |
15096 | } | |
15097 | } | |
15098 | ||
ea664253 | 15099 | // Return the backend representation for interface information. |
2387f644 | 15100 | |
ea664253 | 15101 | Bexpression* |
15102 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 15103 | { |
15104 | Gogo* gogo = context->gogo(); | |
ea664253 | 15105 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 15106 | switch (this->iface_info_) |
15107 | { | |
15108 | case INTERFACE_INFO_METHODS: | |
15109 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 15110 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
15111 | this->location()); | |
2387f644 | 15112 | break; |
15113 | default: | |
15114 | go_unreachable(); | |
15115 | } | |
2387f644 | 15116 | } |
15117 | ||
15118 | // Dump ast representation for an interface info expression. | |
15119 | ||
15120 | void | |
15121 | Interface_info_expression::do_dump_expression( | |
15122 | Ast_dump_context* ast_dump_context) const | |
15123 | { | |
2c809f8f | 15124 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 15125 | ast_dump_context->ostream() << "interfaceinfo("; |
15126 | this->iface_->dump_expression(ast_dump_context); | |
15127 | ast_dump_context->ostream() << ","; | |
15128 | ast_dump_context->ostream() << | |
2c809f8f | 15129 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
15130 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 15131 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
15132 | : "unknown"); | |
15133 | ast_dump_context->ostream() << ")"; | |
15134 | } | |
15135 | ||
15136 | // Make an interface info expression. | |
15137 | ||
15138 | Expression* | |
15139 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
15140 | Location location) | |
15141 | { | |
15142 | return new Interface_info_expression(iface, iface_info, location); | |
15143 | } | |
15144 | ||
2c809f8f | 15145 | // An expression that represents an interface value. The first field is either |
15146 | // a type descriptor for an empty interface or a pointer to the interface method | |
15147 | // table for a non-empty interface. The second field is always the object. | |
15148 | ||
15149 | class Interface_value_expression : public Expression | |
15150 | { | |
15151 | public: | |
15152 | Interface_value_expression(Type* type, Expression* first_field, | |
15153 | Expression* obj, Location location) | |
15154 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
15155 | type_(type), first_field_(first_field), obj_(obj) | |
15156 | { } | |
15157 | ||
15158 | protected: | |
15159 | int | |
15160 | do_traverse(Traverse*); | |
15161 | ||
15162 | Type* | |
15163 | do_type() | |
15164 | { return this->type_; } | |
15165 | ||
15166 | void | |
15167 | do_determine_type(const Type_context*) | |
15168 | { go_unreachable(); } | |
15169 | ||
15170 | Expression* | |
15171 | do_copy() | |
15172 | { | |
15173 | return new Interface_value_expression(this->type_, | |
15174 | this->first_field_->copy(), | |
15175 | this->obj_->copy(), this->location()); | |
15176 | } | |
15177 | ||
ea664253 | 15178 | Bexpression* |
15179 | do_get_backend(Translate_context* context); | |
2c809f8f | 15180 | |
15181 | void | |
15182 | do_dump_expression(Ast_dump_context*) const; | |
15183 | ||
15184 | private: | |
15185 | // The type of the interface value. | |
15186 | Type* type_; | |
15187 | // The first field of the interface (either a type descriptor or a pointer | |
15188 | // to the method table. | |
15189 | Expression* first_field_; | |
15190 | // The underlying object of the interface. | |
15191 | Expression* obj_; | |
15192 | }; | |
15193 | ||
15194 | int | |
15195 | Interface_value_expression::do_traverse(Traverse* traverse) | |
15196 | { | |
15197 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
15198 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
15199 | return TRAVERSE_EXIT; | |
15200 | return TRAVERSE_CONTINUE; | |
15201 | } | |
15202 | ||
ea664253 | 15203 | Bexpression* |
15204 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15205 | { |
15206 | std::vector<Bexpression*> vals(2); | |
ea664253 | 15207 | vals[0] = this->first_field_->get_backend(context); |
15208 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 15209 | |
15210 | Gogo* gogo = context->gogo(); | |
15211 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 15212 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 15213 | } |
15214 | ||
15215 | void | |
15216 | Interface_value_expression::do_dump_expression( | |
15217 | Ast_dump_context* ast_dump_context) const | |
15218 | { | |
15219 | ast_dump_context->ostream() << "interfacevalue("; | |
15220 | ast_dump_context->ostream() << | |
15221 | (this->type_->interface_type()->is_empty() | |
15222 | ? "type_descriptor: " | |
15223 | : "methods: "); | |
15224 | this->first_field_->dump_expression(ast_dump_context); | |
15225 | ast_dump_context->ostream() << ", object: "; | |
15226 | this->obj_->dump_expression(ast_dump_context); | |
15227 | ast_dump_context->ostream() << ")"; | |
15228 | } | |
15229 | ||
15230 | Expression* | |
15231 | Expression::make_interface_value(Type* type, Expression* first_value, | |
15232 | Expression* object, Location location) | |
15233 | { | |
15234 | return new Interface_value_expression(type, first_value, object, location); | |
15235 | } | |
15236 | ||
15237 | // An interface method table for a pair of types: an interface type and a type | |
15238 | // that implements that interface. | |
15239 | ||
15240 | class Interface_mtable_expression : public Expression | |
15241 | { | |
15242 | public: | |
15243 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
15244 | bool is_pointer, Location location) | |
15245 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
15246 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
15247 | method_table_type_(NULL), bvar_(NULL) | |
15248 | { } | |
15249 | ||
15250 | protected: | |
15251 | int | |
15252 | do_traverse(Traverse*); | |
15253 | ||
15254 | Type* | |
15255 | do_type(); | |
15256 | ||
15257 | bool | |
3ae06f68 | 15258 | do_is_static_initializer() const |
2c809f8f | 15259 | { return true; } |
15260 | ||
15261 | void | |
15262 | do_determine_type(const Type_context*) | |
15263 | { go_unreachable(); } | |
15264 | ||
15265 | Expression* | |
15266 | do_copy() | |
15267 | { | |
15268 | return new Interface_mtable_expression(this->itype_, this->type_, | |
15269 | this->is_pointer_, this->location()); | |
15270 | } | |
15271 | ||
15272 | bool | |
15273 | do_is_addressable() const | |
15274 | { return true; } | |
15275 | ||
ea664253 | 15276 | Bexpression* |
15277 | do_get_backend(Translate_context* context); | |
2c809f8f | 15278 | |
15279 | void | |
15280 | do_dump_expression(Ast_dump_context*) const; | |
15281 | ||
15282 | private: | |
15283 | // The interface type for which the methods are defined. | |
15284 | Interface_type* itype_; | |
15285 | // The type to construct the interface method table for. | |
15286 | Type* type_; | |
15287 | // Whether this table contains the method set for the receiver type or the | |
15288 | // pointer receiver type. | |
15289 | bool is_pointer_; | |
15290 | // The type of the method table. | |
15291 | Type* method_table_type_; | |
15292 | // The backend variable that refers to the interface method table. | |
15293 | Bvariable* bvar_; | |
15294 | }; | |
15295 | ||
15296 | int | |
15297 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
15298 | { | |
15299 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
15300 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
15301 | return TRAVERSE_EXIT; | |
15302 | return TRAVERSE_CONTINUE; | |
15303 | } | |
15304 | ||
15305 | Type* | |
15306 | Interface_mtable_expression::do_type() | |
15307 | { | |
15308 | if (this->method_table_type_ != NULL) | |
15309 | return this->method_table_type_; | |
15310 | ||
15311 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15312 | go_assert(!interface_methods->empty()); | |
15313 | ||
15314 | Struct_field_list* sfl = new Struct_field_list; | |
15315 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
15316 | this->location()); | |
15317 | sfl->push_back(Struct_field(tid)); | |
db122cb9 | 15318 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); |
2c809f8f | 15319 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); |
15320 | p != interface_methods->end(); | |
15321 | ++p) | |
db122cb9 | 15322 | { |
15323 | // We want C function pointers here, not func descriptors; model | |
15324 | // using void* pointers. | |
15325 | Typed_identifier method(p->name(), unsafe_ptr_type, p->location()); | |
15326 | sfl->push_back(Struct_field(method)); | |
15327 | } | |
6bf4793c | 15328 | Struct_type* st = Type::make_struct_type(sfl, this->location()); |
15329 | st->set_is_struct_incomparable(); | |
15330 | this->method_table_type_ = st; | |
2c809f8f | 15331 | return this->method_table_type_; |
15332 | } | |
15333 | ||
ea664253 | 15334 | Bexpression* |
15335 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15336 | { |
15337 | Gogo* gogo = context->gogo(); | |
2c809f8f | 15338 | Location loc = Linemap::predeclared_location(); |
15339 | if (this->bvar_ != NULL) | |
7af8e400 | 15340 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15341 | |
15342 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15343 | go_assert(!interface_methods->empty()); | |
15344 | ||
19272321 | 15345 | std::string mangled_name = |
15346 | gogo->interface_method_table_name(this->itype_, this->type_, | |
15347 | this->is_pointer_); | |
2c809f8f | 15348 | |
1530c754 | 15349 | // Set is_public if we are converting a named type to an interface |
15350 | // type that is defined in the same package as the named type, and | |
15351 | // the interface has hidden methods. In that case the interface | |
15352 | // method table will be defined by the package that defines the | |
15353 | // types. | |
15354 | bool is_public = false; | |
15355 | if (this->type_->named_type() != NULL | |
15356 | && (this->type_->named_type()->named_object()->package() | |
15357 | == this->itype_->package())) | |
15358 | { | |
15359 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15360 | p != interface_methods->end(); | |
15361 | ++p) | |
2c809f8f | 15362 | { |
1530c754 | 15363 | if (Gogo::is_hidden_name(p->name())) |
15364 | { | |
15365 | is_public = true; | |
15366 | break; | |
15367 | } | |
2c809f8f | 15368 | } |
15369 | } | |
15370 | ||
1530c754 | 15371 | if (is_public |
2c809f8f | 15372 | && this->type_->named_type()->named_object()->package() != NULL) |
15373 | { | |
1530c754 | 15374 | // The interface conversion table is defined elsewhere. |
2c809f8f | 15375 | Btype* btype = this->type()->get_backend(gogo); |
438b4bec | 15376 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
2c809f8f | 15377 | this->bvar_ = |
438b4bec | 15378 | gogo->backend()->immutable_struct_reference(mangled_name, asm_name, |
15379 | btype, loc); | |
7af8e400 | 15380 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15381 | } |
15382 | ||
15383 | // The first element is the type descriptor. | |
15384 | Type* td_type; | |
15385 | if (!this->is_pointer_) | |
15386 | td_type = this->type_; | |
15387 | else | |
15388 | td_type = Type::make_pointer_type(this->type_); | |
15389 | ||
db122cb9 | 15390 | std::vector<Backend::Btyped_identifier> bstructfields; |
15391 | ||
2c809f8f | 15392 | // Build an interface method table for a type: a type descriptor followed by a |
15393 | // list of function pointers, one for each interface method. This is used for | |
15394 | // interfaces. | |
15395 | Expression_list* svals = new Expression_list(); | |
db122cb9 | 15396 | Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc); |
15397 | svals->push_back(tdescriptor); | |
15398 | ||
15399 | Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo); | |
15400 | Backend::Btyped_identifier btd("_type", tdesc_btype, loc); | |
15401 | bstructfields.push_back(btd); | |
2c809f8f | 15402 | |
15403 | Named_type* nt = this->type_->named_type(); | |
15404 | Struct_type* st = this->type_->struct_type(); | |
15405 | go_assert(nt != NULL || st != NULL); | |
15406 | ||
15407 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15408 | p != interface_methods->end(); | |
15409 | ++p) | |
15410 | { | |
15411 | bool is_ambiguous; | |
15412 | Method* m; | |
15413 | if (nt != NULL) | |
15414 | m = nt->method_function(p->name(), &is_ambiguous); | |
15415 | else | |
15416 | m = st->method_function(p->name(), &is_ambiguous); | |
15417 | go_assert(m != NULL); | |
15418 | Named_object* no = m->named_object(); | |
15419 | ||
15420 | go_assert(no->is_function() || no->is_function_declaration()); | |
db122cb9 | 15421 | |
15422 | Btype* fcn_btype = m->type()->get_backend_fntype(gogo); | |
15423 | Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc); | |
15424 | bstructfields.push_back(bmtype); | |
15425 | ||
2c809f8f | 15426 | svals->push_back(Expression::make_func_code_reference(no, loc)); |
15427 | } | |
15428 | ||
db122cb9 | 15429 | Btype *btype = gogo->backend()->struct_type(bstructfields); |
15430 | std::vector<Bexpression*> ctor_bexprs; | |
15431 | for (Expression_list::const_iterator pe = svals->begin(); | |
15432 | pe != svals->end(); | |
15433 | ++pe) | |
15434 | { | |
15435 | ctor_bexprs.push_back((*pe)->get_backend(context)); | |
15436 | } | |
15437 | Bexpression* ctor = | |
15438 | gogo->backend()->constructor_expression(btype, ctor_bexprs, loc); | |
2c809f8f | 15439 | |
438b4bec | 15440 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
15441 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false, | |
2c809f8f | 15442 | !is_public, btype, loc); |
15443 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
15444 | !is_public, btype, loc, ctor); | |
7af8e400 | 15445 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 15446 | } |
15447 | ||
15448 | void | |
15449 | Interface_mtable_expression::do_dump_expression( | |
15450 | Ast_dump_context* ast_dump_context) const | |
15451 | { | |
15452 | ast_dump_context->ostream() << "__go_" | |
15453 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
15454 | ast_dump_context->dump_type(this->itype_); | |
15455 | ast_dump_context->ostream() << "__"; | |
15456 | ast_dump_context->dump_type(this->type_); | |
15457 | } | |
15458 | ||
15459 | Expression* | |
15460 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
15461 | bool is_pointer, Location location) | |
15462 | { | |
15463 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
15464 | } | |
15465 | ||
e440a328 | 15466 | // An expression which evaluates to the offset of a field within a |
15467 | // struct. This, like Type_info_expression, q.v., is only used to | |
15468 | // initialize fields of a type descriptor. | |
15469 | ||
15470 | class Struct_field_offset_expression : public Expression | |
15471 | { | |
15472 | public: | |
15473 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 15474 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
15475 | Linemap::predeclared_location()), | |
e440a328 | 15476 | type_(type), field_(field) |
15477 | { } | |
15478 | ||
15479 | protected: | |
f23d7786 | 15480 | bool |
3ae06f68 | 15481 | do_is_static_initializer() const |
f23d7786 | 15482 | { return true; } |
15483 | ||
e440a328 | 15484 | Type* |
15485 | do_type() | |
15486 | { return Type::lookup_integer_type("uintptr"); } | |
15487 | ||
15488 | void | |
15489 | do_determine_type(const Type_context*) | |
15490 | { } | |
15491 | ||
15492 | Expression* | |
15493 | do_copy() | |
15494 | { return this; } | |
15495 | ||
ea664253 | 15496 | Bexpression* |
15497 | do_get_backend(Translate_context* context); | |
e440a328 | 15498 | |
d751bb78 | 15499 | void |
15500 | do_dump_expression(Ast_dump_context*) const; | |
15501 | ||
e440a328 | 15502 | private: |
15503 | // The type of the struct. | |
15504 | Struct_type* type_; | |
15505 | // The field. | |
15506 | const Struct_field* field_; | |
15507 | }; | |
15508 | ||
ea664253 | 15509 | // Return the backend representation for a struct field offset. |
e440a328 | 15510 | |
ea664253 | 15511 | Bexpression* |
15512 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 15513 | { |
e440a328 | 15514 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 15515 | Struct_field_list::const_iterator p; |
2c8bda43 | 15516 | unsigned i = 0; |
e440a328 | 15517 | for (p = fields->begin(); |
15518 | p != fields->end(); | |
2c8bda43 | 15519 | ++p, ++i) |
15520 | if (&*p == this->field_) | |
15521 | break; | |
c484d925 | 15522 | go_assert(&*p == this->field_); |
e440a328 | 15523 | |
2c8bda43 | 15524 | Gogo* gogo = context->gogo(); |
15525 | Btype* btype = this->type_->get_backend(gogo); | |
15526 | ||
3f378015 | 15527 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 15528 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 15529 | Expression* ret = |
3f378015 | 15530 | Expression::make_integer_int64(offset, uptr_type, |
15531 | Linemap::predeclared_location()); | |
ea664253 | 15532 | return ret->get_backend(context); |
e440a328 | 15533 | } |
15534 | ||
d751bb78 | 15535 | // Dump ast representation for a struct field offset expression. |
15536 | ||
15537 | void | |
15538 | Struct_field_offset_expression::do_dump_expression( | |
15539 | Ast_dump_context* ast_dump_context) const | |
15540 | { | |
15541 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 15542 | ast_dump_context->dump_type(this->type_); |
15543 | ast_dump_context->ostream() << '.'; | |
15544 | ast_dump_context->ostream() << | |
15545 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 15546 | ast_dump_context->ostream() << ")"; |
15547 | } | |
15548 | ||
e440a328 | 15549 | // Make an expression for a struct field offset. |
15550 | ||
15551 | Expression* | |
15552 | Expression::make_struct_field_offset(Struct_type* type, | |
15553 | const Struct_field* field) | |
15554 | { | |
15555 | return new Struct_field_offset_expression(type, field); | |
15556 | } | |
15557 | ||
15558 | // An expression which evaluates to the address of an unnamed label. | |
15559 | ||
15560 | class Label_addr_expression : public Expression | |
15561 | { | |
15562 | public: | |
b13c66cd | 15563 | Label_addr_expression(Label* label, Location location) |
e440a328 | 15564 | : Expression(EXPRESSION_LABEL_ADDR, location), |
15565 | label_(label) | |
15566 | { } | |
15567 | ||
15568 | protected: | |
15569 | Type* | |
15570 | do_type() | |
15571 | { return Type::make_pointer_type(Type::make_void_type()); } | |
15572 | ||
15573 | void | |
15574 | do_determine_type(const Type_context*) | |
15575 | { } | |
15576 | ||
15577 | Expression* | |
15578 | do_copy() | |
15579 | { return new Label_addr_expression(this->label_, this->location()); } | |
15580 | ||
ea664253 | 15581 | Bexpression* |
15582 | do_get_backend(Translate_context* context) | |
15583 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 15584 | |
d751bb78 | 15585 | void |
15586 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15587 | { ast_dump_context->ostream() << this->label_->name(); } | |
15588 | ||
e440a328 | 15589 | private: |
15590 | // The label whose address we are taking. | |
15591 | Label* label_; | |
15592 | }; | |
15593 | ||
15594 | // Make an expression for the address of an unnamed label. | |
15595 | ||
15596 | Expression* | |
b13c66cd | 15597 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 15598 | { |
15599 | return new Label_addr_expression(label, location); | |
15600 | } | |
15601 | ||
da244e59 | 15602 | // Class Conditional_expression. |
283a177b | 15603 | |
2c809f8f | 15604 | // Traversal. |
15605 | ||
15606 | int | |
15607 | Conditional_expression::do_traverse(Traverse* traverse) | |
15608 | { | |
15609 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15610 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15611 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15612 | return TRAVERSE_EXIT; | |
15613 | return TRAVERSE_CONTINUE; | |
15614 | } | |
15615 | ||
283a177b | 15616 | // Return the type of the conditional expression. |
15617 | ||
15618 | Type* | |
15619 | Conditional_expression::do_type() | |
15620 | { | |
15621 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15622 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15623 | NULL)) | |
283a177b | 15624 | result_type = this->then_->type(); |
15625 | else if (this->then_->is_nil_expression() | |
15626 | || this->else_->is_nil_expression()) | |
15627 | result_type = (!this->then_->is_nil_expression() | |
15628 | ? this->then_->type() | |
15629 | : this->else_->type()); | |
15630 | return result_type; | |
15631 | } | |
15632 | ||
2c809f8f | 15633 | // Determine type for a conditional expression. |
15634 | ||
15635 | void | |
15636 | Conditional_expression::do_determine_type(const Type_context* context) | |
15637 | { | |
15638 | this->cond_->determine_type_no_context(); | |
15639 | this->then_->determine_type(context); | |
15640 | this->else_->determine_type(context); | |
15641 | } | |
15642 | ||
283a177b | 15643 | // Get the backend representation of a conditional expression. |
15644 | ||
ea664253 | 15645 | Bexpression* |
15646 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 15647 | { |
15648 | Gogo* gogo = context->gogo(); | |
15649 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 15650 | Bexpression* cond = this->cond_->get_backend(context); |
15651 | Bexpression* then = this->then_->get_backend(context); | |
15652 | Bexpression* belse = this->else_->get_backend(context); | |
93715b75 | 15653 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
15654 | return gogo->backend()->conditional_expression(bfn, result_btype, cond, then, | |
ea664253 | 15655 | belse, this->location()); |
283a177b | 15656 | } |
15657 | ||
15658 | // Dump ast representation of a conditional expression. | |
15659 | ||
15660 | void | |
15661 | Conditional_expression::do_dump_expression( | |
15662 | Ast_dump_context* ast_dump_context) const | |
15663 | { | |
15664 | ast_dump_context->ostream() << "("; | |
15665 | ast_dump_context->dump_expression(this->cond_); | |
15666 | ast_dump_context->ostream() << " ? "; | |
15667 | ast_dump_context->dump_expression(this->then_); | |
15668 | ast_dump_context->ostream() << " : "; | |
15669 | ast_dump_context->dump_expression(this->else_); | |
15670 | ast_dump_context->ostream() << ") "; | |
15671 | } | |
15672 | ||
15673 | // Make a conditional expression. | |
15674 | ||
15675 | Expression* | |
15676 | Expression::make_conditional(Expression* cond, Expression* then, | |
15677 | Expression* else_expr, Location location) | |
15678 | { | |
15679 | return new Conditional_expression(cond, then, else_expr, location); | |
15680 | } | |
15681 | ||
da244e59 | 15682 | // Class Compound_expression. |
2c809f8f | 15683 | |
15684 | // Traversal. | |
15685 | ||
15686 | int | |
15687 | Compound_expression::do_traverse(Traverse* traverse) | |
15688 | { | |
15689 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15690 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15691 | return TRAVERSE_EXIT; | |
15692 | return TRAVERSE_CONTINUE; | |
15693 | } | |
15694 | ||
15695 | // Return the type of the compound expression. | |
15696 | ||
15697 | Type* | |
15698 | Compound_expression::do_type() | |
15699 | { | |
15700 | return this->expr_->type(); | |
15701 | } | |
15702 | ||
15703 | // Determine type for a compound expression. | |
15704 | ||
15705 | void | |
15706 | Compound_expression::do_determine_type(const Type_context* context) | |
15707 | { | |
15708 | this->init_->determine_type_no_context(); | |
15709 | this->expr_->determine_type(context); | |
15710 | } | |
15711 | ||
15712 | // Get the backend representation of a compound expression. | |
15713 | ||
ea664253 | 15714 | Bexpression* |
15715 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15716 | { |
15717 | Gogo* gogo = context->gogo(); | |
ea664253 | 15718 | Bexpression* binit = this->init_->get_backend(context); |
0ab48656 | 15719 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
15720 | Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction, | |
15721 | binit); | |
ea664253 | 15722 | Bexpression* bexpr = this->expr_->get_backend(context); |
15723 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15724 | this->location()); | |
2c809f8f | 15725 | } |
15726 | ||
15727 | // Dump ast representation of a conditional expression. | |
15728 | ||
15729 | void | |
15730 | Compound_expression::do_dump_expression( | |
15731 | Ast_dump_context* ast_dump_context) const | |
15732 | { | |
15733 | ast_dump_context->ostream() << "("; | |
15734 | ast_dump_context->dump_expression(this->init_); | |
15735 | ast_dump_context->ostream() << ","; | |
15736 | ast_dump_context->dump_expression(this->expr_); | |
15737 | ast_dump_context->ostream() << ") "; | |
15738 | } | |
15739 | ||
15740 | // Make a compound expression. | |
15741 | ||
15742 | Expression* | |
15743 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15744 | { | |
15745 | return new Compound_expression(init, expr, location); | |
15746 | } | |
15747 | ||
1b4fb1e0 | 15748 | // Class Backend_expression. |
15749 | ||
15750 | int | |
15751 | Backend_expression::do_traverse(Traverse*) | |
15752 | { | |
15753 | return TRAVERSE_CONTINUE; | |
15754 | } | |
15755 | ||
15756 | void | |
15757 | Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15758 | { | |
15759 | ast_dump_context->ostream() << "backend_expression<"; | |
15760 | ast_dump_context->dump_type(this->type_); | |
15761 | ast_dump_context->ostream() << ">"; | |
15762 | } | |
15763 | ||
15764 | Expression* | |
15765 | Expression::make_backend(Bexpression* bexpr, Type* type, Location location) | |
15766 | { | |
15767 | return new Backend_expression(bexpr, type, location); | |
15768 | } | |
15769 | ||
e440a328 | 15770 | // Import an expression. This comes at the end in order to see the |
15771 | // various class definitions. | |
15772 | ||
15773 | Expression* | |
15774 | Expression::import_expression(Import* imp) | |
15775 | { | |
15776 | int c = imp->peek_char(); | |
15777 | if (imp->match_c_string("- ") | |
15778 | || imp->match_c_string("! ") | |
15779 | || imp->match_c_string("^ ")) | |
15780 | return Unary_expression::do_import(imp); | |
15781 | else if (c == '(') | |
15782 | return Binary_expression::do_import(imp); | |
15783 | else if (imp->match_c_string("true") | |
15784 | || imp->match_c_string("false")) | |
15785 | return Boolean_expression::do_import(imp); | |
15786 | else if (c == '"') | |
15787 | return String_expression::do_import(imp); | |
15788 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15789 | { | |
15790 | // This handles integers, floats and complex constants. | |
15791 | return Integer_expression::do_import(imp); | |
15792 | } | |
15793 | else if (imp->match_c_string("nil")) | |
15794 | return Nil_expression::do_import(imp); | |
15795 | else if (imp->match_c_string("convert")) | |
15796 | return Type_conversion_expression::do_import(imp); | |
15797 | else | |
15798 | { | |
631d5788 | 15799 | go_error_at(imp->location(), "import error: expected expression"); |
e440a328 | 15800 | return Expression::make_error(imp->location()); |
15801 | } | |
15802 | } | |
15803 | ||
15804 | // Class Expression_list. | |
15805 | ||
15806 | // Traverse the list. | |
15807 | ||
15808 | int | |
15809 | Expression_list::traverse(Traverse* traverse) | |
15810 | { | |
15811 | for (Expression_list::iterator p = this->begin(); | |
15812 | p != this->end(); | |
15813 | ++p) | |
15814 | { | |
15815 | if (*p != NULL) | |
15816 | { | |
15817 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15818 | return TRAVERSE_EXIT; | |
15819 | } | |
15820 | } | |
15821 | return TRAVERSE_CONTINUE; | |
15822 | } | |
15823 | ||
15824 | // Copy the list. | |
15825 | ||
15826 | Expression_list* | |
15827 | Expression_list::copy() | |
15828 | { | |
15829 | Expression_list* ret = new Expression_list(); | |
15830 | for (Expression_list::iterator p = this->begin(); | |
15831 | p != this->end(); | |
15832 | ++p) | |
15833 | { | |
15834 | if (*p == NULL) | |
15835 | ret->push_back(NULL); | |
15836 | else | |
15837 | ret->push_back((*p)->copy()); | |
15838 | } | |
15839 | return ret; | |
15840 | } | |
15841 | ||
15842 | // Return whether an expression list has an error expression. | |
15843 | ||
15844 | bool | |
15845 | Expression_list::contains_error() const | |
15846 | { | |
15847 | for (Expression_list::const_iterator p = this->begin(); | |
15848 | p != this->end(); | |
15849 | ++p) | |
15850 | if (*p != NULL && (*p)->is_error_expression()) | |
15851 | return true; | |
15852 | return false; | |
15853 | } | |
0c77715b | 15854 | |
15855 | // Class Numeric_constant. | |
15856 | ||
15857 | // Destructor. | |
15858 | ||
15859 | Numeric_constant::~Numeric_constant() | |
15860 | { | |
15861 | this->clear(); | |
15862 | } | |
15863 | ||
15864 | // Copy constructor. | |
15865 | ||
15866 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15867 | : classification_(a.classification_), type_(a.type_) | |
15868 | { | |
15869 | switch (a.classification_) | |
15870 | { | |
15871 | case NC_INVALID: | |
15872 | break; | |
15873 | case NC_INT: | |
15874 | case NC_RUNE: | |
15875 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15876 | break; | |
15877 | case NC_FLOAT: | |
15878 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15879 | break; | |
15880 | case NC_COMPLEX: | |
fcbea5e4 | 15881 | mpc_init2(this->u_.complex_val, mpc_precision); |
15882 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15883 | break; |
15884 | default: | |
15885 | go_unreachable(); | |
15886 | } | |
15887 | } | |
15888 | ||
15889 | // Assignment operator. | |
15890 | ||
15891 | Numeric_constant& | |
15892 | Numeric_constant::operator=(const Numeric_constant& a) | |
15893 | { | |
15894 | this->clear(); | |
15895 | this->classification_ = a.classification_; | |
15896 | this->type_ = a.type_; | |
15897 | switch (a.classification_) | |
15898 | { | |
15899 | case NC_INVALID: | |
15900 | break; | |
15901 | case NC_INT: | |
15902 | case NC_RUNE: | |
15903 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15904 | break; | |
15905 | case NC_FLOAT: | |
15906 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15907 | break; | |
15908 | case NC_COMPLEX: | |
fcbea5e4 | 15909 | mpc_init2(this->u_.complex_val, mpc_precision); |
15910 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15911 | break; |
15912 | default: | |
15913 | go_unreachable(); | |
15914 | } | |
15915 | return *this; | |
15916 | } | |
15917 | ||
15918 | // Clear the contents. | |
15919 | ||
15920 | void | |
15921 | Numeric_constant::clear() | |
15922 | { | |
15923 | switch (this->classification_) | |
15924 | { | |
15925 | case NC_INVALID: | |
15926 | break; | |
15927 | case NC_INT: | |
15928 | case NC_RUNE: | |
15929 | mpz_clear(this->u_.int_val); | |
15930 | break; | |
15931 | case NC_FLOAT: | |
15932 | mpfr_clear(this->u_.float_val); | |
15933 | break; | |
15934 | case NC_COMPLEX: | |
fcbea5e4 | 15935 | mpc_clear(this->u_.complex_val); |
0c77715b | 15936 | break; |
15937 | default: | |
15938 | go_unreachable(); | |
15939 | } | |
15940 | this->classification_ = NC_INVALID; | |
15941 | } | |
15942 | ||
15943 | // Set to an unsigned long value. | |
15944 | ||
15945 | void | |
15946 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15947 | { | |
15948 | this->clear(); | |
15949 | this->classification_ = NC_INT; | |
15950 | this->type_ = type; | |
15951 | mpz_init_set_ui(this->u_.int_val, val); | |
15952 | } | |
15953 | ||
15954 | // Set to an integer value. | |
15955 | ||
15956 | void | |
15957 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15958 | { | |
15959 | this->clear(); | |
15960 | this->classification_ = NC_INT; | |
15961 | this->type_ = type; | |
15962 | mpz_init_set(this->u_.int_val, val); | |
15963 | } | |
15964 | ||
15965 | // Set to a rune value. | |
15966 | ||
15967 | void | |
15968 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15969 | { | |
15970 | this->clear(); | |
15971 | this->classification_ = NC_RUNE; | |
15972 | this->type_ = type; | |
15973 | mpz_init_set(this->u_.int_val, val); | |
15974 | } | |
15975 | ||
15976 | // Set to a floating point value. | |
15977 | ||
15978 | void | |
15979 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15980 | { | |
15981 | this->clear(); | |
15982 | this->classification_ = NC_FLOAT; | |
15983 | this->type_ = type; | |
833b523c | 15984 | // Numeric constants do not have negative zero values, so remove |
15985 | // them here. They also don't have infinity or NaN values, but we | |
15986 | // should never see them here. | |
15987 | if (mpfr_zero_p(val)) | |
15988 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
15989 | else | |
15990 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 15991 | } |
15992 | ||
15993 | // Set to a complex value. | |
15994 | ||
15995 | void | |
fcbea5e4 | 15996 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 15997 | { |
15998 | this->clear(); | |
15999 | this->classification_ = NC_COMPLEX; | |
16000 | this->type_ = type; | |
fcbea5e4 | 16001 | mpc_init2(this->u_.complex_val, mpc_precision); |
16002 | mpc_set(this->u_.complex_val, val, MPC_RNDNN); | |
0c77715b | 16003 | } |
16004 | ||
16005 | // Get an int value. | |
16006 | ||
16007 | void | |
16008 | Numeric_constant::get_int(mpz_t* val) const | |
16009 | { | |
16010 | go_assert(this->is_int()); | |
16011 | mpz_init_set(*val, this->u_.int_val); | |
16012 | } | |
16013 | ||
16014 | // Get a rune value. | |
16015 | ||
16016 | void | |
16017 | Numeric_constant::get_rune(mpz_t* val) const | |
16018 | { | |
16019 | go_assert(this->is_rune()); | |
16020 | mpz_init_set(*val, this->u_.int_val); | |
16021 | } | |
16022 | ||
16023 | // Get a floating point value. | |
16024 | ||
16025 | void | |
16026 | Numeric_constant::get_float(mpfr_t* val) const | |
16027 | { | |
16028 | go_assert(this->is_float()); | |
16029 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16030 | } | |
16031 | ||
16032 | // Get a complex value. | |
16033 | ||
16034 | void | |
fcbea5e4 | 16035 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 16036 | { |
16037 | go_assert(this->is_complex()); | |
fcbea5e4 | 16038 | mpc_init2(*val, mpc_precision); |
16039 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 16040 | } |
16041 | ||
16042 | // Express value as unsigned long if possible. | |
16043 | ||
16044 | Numeric_constant::To_unsigned_long | |
16045 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
16046 | { | |
16047 | switch (this->classification_) | |
16048 | { | |
16049 | case NC_INT: | |
16050 | case NC_RUNE: | |
16051 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
16052 | case NC_FLOAT: | |
16053 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
16054 | case NC_COMPLEX: | |
fcbea5e4 | 16055 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16056 | return NC_UL_NOTINT; |
fcbea5e4 | 16057 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
16058 | val); | |
0c77715b | 16059 | default: |
16060 | go_unreachable(); | |
16061 | } | |
16062 | } | |
16063 | ||
16064 | // Express integer value as unsigned long if possible. | |
16065 | ||
16066 | Numeric_constant::To_unsigned_long | |
16067 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
16068 | unsigned long *val) const | |
16069 | { | |
16070 | if (mpz_sgn(ival) < 0) | |
16071 | return NC_UL_NEGATIVE; | |
16072 | unsigned long ui = mpz_get_ui(ival); | |
16073 | if (mpz_cmp_ui(ival, ui) != 0) | |
16074 | return NC_UL_BIG; | |
16075 | *val = ui; | |
16076 | return NC_UL_VALID; | |
16077 | } | |
16078 | ||
16079 | // Express floating point value as unsigned long if possible. | |
16080 | ||
16081 | Numeric_constant::To_unsigned_long | |
16082 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
16083 | unsigned long *val) const | |
16084 | { | |
16085 | if (!mpfr_integer_p(fval)) | |
16086 | return NC_UL_NOTINT; | |
16087 | mpz_t ival; | |
16088 | mpz_init(ival); | |
16089 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16090 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
16091 | mpz_clear(ival); | |
16092 | return ret; | |
16093 | } | |
16094 | ||
03118c21 | 16095 | // Express value as memory size if possible. |
16096 | ||
16097 | bool | |
16098 | Numeric_constant::to_memory_size(int64_t* val) const | |
16099 | { | |
16100 | switch (this->classification_) | |
16101 | { | |
16102 | case NC_INT: | |
16103 | case NC_RUNE: | |
16104 | return this->mpz_to_memory_size(this->u_.int_val, val); | |
16105 | case NC_FLOAT: | |
16106 | return this->mpfr_to_memory_size(this->u_.float_val, val); | |
16107 | case NC_COMPLEX: | |
16108 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
16109 | return false; | |
16110 | return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val); | |
16111 | default: | |
16112 | go_unreachable(); | |
16113 | } | |
16114 | } | |
16115 | ||
16116 | // Express integer as memory size if possible. | |
16117 | ||
16118 | bool | |
16119 | Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const | |
16120 | { | |
16121 | if (mpz_sgn(ival) < 0) | |
16122 | return false; | |
16123 | if (mpz_fits_slong_p(ival)) | |
16124 | { | |
16125 | *val = static_cast<int64_t>(mpz_get_si(ival)); | |
16126 | return true; | |
16127 | } | |
16128 | ||
16129 | // Test >= 64, not > 64, because an int64_t can hold 63 bits of a | |
16130 | // positive value. | |
16131 | if (mpz_sizeinbase(ival, 2) >= 64) | |
16132 | return false; | |
16133 | ||
16134 | mpz_t q, r; | |
16135 | mpz_init(q); | |
16136 | mpz_init(r); | |
16137 | mpz_tdiv_q_2exp(q, ival, 32); | |
16138 | mpz_tdiv_r_2exp(r, ival, 32); | |
16139 | go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r)); | |
16140 | *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32) | |
16141 | + static_cast<int64_t>(mpz_get_ui(r))); | |
16142 | mpz_clear(r); | |
16143 | mpz_clear(q); | |
16144 | return true; | |
16145 | } | |
16146 | ||
16147 | // Express floating point value as memory size if possible. | |
16148 | ||
16149 | bool | |
16150 | Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const | |
16151 | { | |
16152 | if (!mpfr_integer_p(fval)) | |
16153 | return false; | |
16154 | mpz_t ival; | |
16155 | mpz_init(ival); | |
16156 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16157 | bool ret = this->mpz_to_memory_size(ival, val); | |
16158 | mpz_clear(ival); | |
16159 | return ret; | |
16160 | } | |
16161 | ||
0c77715b | 16162 | // Convert value to integer if possible. |
16163 | ||
16164 | bool | |
16165 | Numeric_constant::to_int(mpz_t* val) const | |
16166 | { | |
16167 | switch (this->classification_) | |
16168 | { | |
16169 | case NC_INT: | |
16170 | case NC_RUNE: | |
16171 | mpz_init_set(*val, this->u_.int_val); | |
16172 | return true; | |
16173 | case NC_FLOAT: | |
16174 | if (!mpfr_integer_p(this->u_.float_val)) | |
16175 | return false; | |
16176 | mpz_init(*val); | |
16177 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
16178 | return true; | |
16179 | case NC_COMPLEX: | |
fcbea5e4 | 16180 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
16181 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 16182 | return false; |
16183 | mpz_init(*val); | |
fcbea5e4 | 16184 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16185 | return true; |
16186 | default: | |
16187 | go_unreachable(); | |
16188 | } | |
16189 | } | |
16190 | ||
16191 | // Convert value to floating point if possible. | |
16192 | ||
16193 | bool | |
16194 | Numeric_constant::to_float(mpfr_t* val) const | |
16195 | { | |
16196 | switch (this->classification_) | |
16197 | { | |
16198 | case NC_INT: | |
16199 | case NC_RUNE: | |
16200 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
16201 | return true; | |
16202 | case NC_FLOAT: | |
16203 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16204 | return true; | |
16205 | case NC_COMPLEX: | |
fcbea5e4 | 16206 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16207 | return false; |
fcbea5e4 | 16208 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16209 | return true; |
16210 | default: | |
16211 | go_unreachable(); | |
16212 | } | |
16213 | } | |
16214 | ||
16215 | // Convert value to complex. | |
16216 | ||
16217 | bool | |
fcbea5e4 | 16218 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 16219 | { |
fcbea5e4 | 16220 | mpc_init2(*val, mpc_precision); |
0c77715b | 16221 | switch (this->classification_) |
16222 | { | |
16223 | case NC_INT: | |
16224 | case NC_RUNE: | |
fcbea5e4 | 16225 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16226 | return true; |
16227 | case NC_FLOAT: | |
fcbea5e4 | 16228 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16229 | return true; |
16230 | case NC_COMPLEX: | |
fcbea5e4 | 16231 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16232 | return true; |
16233 | default: | |
16234 | go_unreachable(); | |
16235 | } | |
16236 | } | |
16237 | ||
16238 | // Get the type. | |
16239 | ||
16240 | Type* | |
16241 | Numeric_constant::type() const | |
16242 | { | |
16243 | if (this->type_ != NULL) | |
16244 | return this->type_; | |
16245 | switch (this->classification_) | |
16246 | { | |
16247 | case NC_INT: | |
16248 | return Type::make_abstract_integer_type(); | |
16249 | case NC_RUNE: | |
16250 | return Type::make_abstract_character_type(); | |
16251 | case NC_FLOAT: | |
16252 | return Type::make_abstract_float_type(); | |
16253 | case NC_COMPLEX: | |
16254 | return Type::make_abstract_complex_type(); | |
16255 | default: | |
16256 | go_unreachable(); | |
16257 | } | |
16258 | } | |
16259 | ||
16260 | // If the constant can be expressed in TYPE, then set the type of the | |
16261 | // constant to TYPE and return true. Otherwise return false, and, if | |
16262 | // ISSUE_ERROR is true, report an appropriate error message. | |
16263 | ||
16264 | bool | |
16265 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
16266 | { | |
16267 | bool ret; | |
f11c2155 | 16268 | if (type == NULL || type->is_error()) |
0c77715b | 16269 | ret = true; |
16270 | else if (type->integer_type() != NULL) | |
16271 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
16272 | else if (type->float_type() != NULL) | |
16273 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
16274 | else if (type->complex_type() != NULL) | |
16275 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
16276 | else | |
5706ab68 | 16277 | { |
16278 | ret = false; | |
16279 | if (issue_error) | |
16280 | go_assert(saw_errors()); | |
16281 | } | |
0c77715b | 16282 | if (ret) |
16283 | this->type_ = type; | |
16284 | return ret; | |
16285 | } | |
16286 | ||
16287 | // Check whether the constant can be expressed in an integer type. | |
16288 | ||
16289 | bool | |
16290 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 16291 | Location location) |
0c77715b | 16292 | { |
16293 | mpz_t val; | |
16294 | switch (this->classification_) | |
16295 | { | |
16296 | case NC_INT: | |
16297 | case NC_RUNE: | |
16298 | mpz_init_set(val, this->u_.int_val); | |
16299 | break; | |
16300 | ||
16301 | case NC_FLOAT: | |
16302 | if (!mpfr_integer_p(this->u_.float_val)) | |
16303 | { | |
16304 | if (issue_error) | |
71a45216 | 16305 | { |
631d5788 | 16306 | go_error_at(location, |
16307 | "floating point constant truncated to integer"); | |
71a45216 | 16308 | this->set_invalid(); |
16309 | } | |
0c77715b | 16310 | return false; |
16311 | } | |
16312 | mpz_init(val); | |
16313 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
16314 | break; | |
16315 | ||
16316 | case NC_COMPLEX: | |
fcbea5e4 | 16317 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
16318 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 16319 | { |
16320 | if (issue_error) | |
71a45216 | 16321 | { |
631d5788 | 16322 | go_error_at(location, "complex constant truncated to integer"); |
71a45216 | 16323 | this->set_invalid(); |
16324 | } | |
0c77715b | 16325 | return false; |
16326 | } | |
16327 | mpz_init(val); | |
fcbea5e4 | 16328 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16329 | break; |
16330 | ||
16331 | default: | |
16332 | go_unreachable(); | |
16333 | } | |
16334 | ||
16335 | bool ret; | |
16336 | if (type->is_abstract()) | |
16337 | ret = true; | |
16338 | else | |
16339 | { | |
16340 | int bits = mpz_sizeinbase(val, 2); | |
16341 | if (type->is_unsigned()) | |
16342 | { | |
16343 | // For an unsigned type we can only accept a nonnegative | |
16344 | // number, and we must be able to represents at least BITS. | |
16345 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
16346 | } | |
16347 | else | |
16348 | { | |
16349 | // For a signed type we need an extra bit to indicate the | |
16350 | // sign. We have to handle the most negative integer | |
16351 | // specially. | |
16352 | ret = (bits + 1 <= type->bits() | |
16353 | || (bits <= type->bits() | |
16354 | && mpz_sgn(val) < 0 | |
16355 | && (mpz_scan1(val, 0) | |
16356 | == static_cast<unsigned long>(type->bits() - 1)) | |
16357 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
16358 | } | |
16359 | } | |
16360 | ||
16361 | if (!ret && issue_error) | |
71a45216 | 16362 | { |
631d5788 | 16363 | go_error_at(location, "integer constant overflow"); |
71a45216 | 16364 | this->set_invalid(); |
16365 | } | |
0c77715b | 16366 | |
16367 | return ret; | |
16368 | } | |
16369 | ||
16370 | // Check whether the constant can be expressed in a floating point | |
16371 | // type. | |
16372 | ||
16373 | bool | |
16374 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 16375 | Location location) |
0c77715b | 16376 | { |
16377 | mpfr_t val; | |
16378 | switch (this->classification_) | |
16379 | { | |
16380 | case NC_INT: | |
16381 | case NC_RUNE: | |
16382 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
16383 | break; | |
16384 | ||
16385 | case NC_FLOAT: | |
16386 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
16387 | break; | |
16388 | ||
16389 | case NC_COMPLEX: | |
fcbea5e4 | 16390 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16391 | { |
16392 | if (issue_error) | |
71a45216 | 16393 | { |
16394 | this->set_invalid(); | |
631d5788 | 16395 | go_error_at(location, "complex constant truncated to float"); |
71a45216 | 16396 | } |
0c77715b | 16397 | return false; |
16398 | } | |
fcbea5e4 | 16399 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16400 | break; |
16401 | ||
16402 | default: | |
16403 | go_unreachable(); | |
16404 | } | |
16405 | ||
16406 | bool ret; | |
16407 | if (type->is_abstract()) | |
16408 | ret = true; | |
16409 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
16410 | { | |
16411 | // A NaN or Infinity always fits in the range of the type. | |
16412 | ret = true; | |
16413 | } | |
16414 | else | |
16415 | { | |
16416 | mp_exp_t exp = mpfr_get_exp(val); | |
16417 | mp_exp_t max_exp; | |
16418 | switch (type->bits()) | |
16419 | { | |
16420 | case 32: | |
16421 | max_exp = 128; | |
16422 | break; | |
16423 | case 64: | |
16424 | max_exp = 1024; | |
16425 | break; | |
16426 | default: | |
16427 | go_unreachable(); | |
16428 | } | |
16429 | ||
16430 | ret = exp <= max_exp; | |
d0bcce51 | 16431 | |
16432 | if (ret) | |
16433 | { | |
16434 | // Round the constant to the desired type. | |
16435 | mpfr_t t; | |
16436 | mpfr_init(t); | |
16437 | switch (type->bits()) | |
16438 | { | |
16439 | case 32: | |
16440 | mpfr_set_prec(t, 24); | |
16441 | break; | |
16442 | case 64: | |
16443 | mpfr_set_prec(t, 53); | |
16444 | break; | |
16445 | default: | |
16446 | go_unreachable(); | |
16447 | } | |
16448 | mpfr_set(t, val, GMP_RNDN); | |
16449 | mpfr_set(val, t, GMP_RNDN); | |
16450 | mpfr_clear(t); | |
16451 | ||
16452 | this->set_float(type, val); | |
16453 | } | |
0c77715b | 16454 | } |
16455 | ||
16456 | mpfr_clear(val); | |
16457 | ||
16458 | if (!ret && issue_error) | |
71a45216 | 16459 | { |
631d5788 | 16460 | go_error_at(location, "floating point constant overflow"); |
71a45216 | 16461 | this->set_invalid(); |
16462 | } | |
0c77715b | 16463 | |
16464 | return ret; | |
16465 | } | |
16466 | ||
16467 | // Check whether the constant can be expressed in a complex type. | |
16468 | ||
16469 | bool | |
16470 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 16471 | Location location) |
0c77715b | 16472 | { |
16473 | if (type->is_abstract()) | |
16474 | return true; | |
16475 | ||
16476 | mp_exp_t max_exp; | |
16477 | switch (type->bits()) | |
16478 | { | |
16479 | case 64: | |
16480 | max_exp = 128; | |
16481 | break; | |
16482 | case 128: | |
16483 | max_exp = 1024; | |
16484 | break; | |
16485 | default: | |
16486 | go_unreachable(); | |
16487 | } | |
16488 | ||
fcbea5e4 | 16489 | mpc_t val; |
16490 | mpc_init2(val, mpc_precision); | |
0c77715b | 16491 | switch (this->classification_) |
16492 | { | |
16493 | case NC_INT: | |
16494 | case NC_RUNE: | |
fcbea5e4 | 16495 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16496 | break; |
16497 | ||
16498 | case NC_FLOAT: | |
fcbea5e4 | 16499 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16500 | break; |
16501 | ||
16502 | case NC_COMPLEX: | |
fcbea5e4 | 16503 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16504 | break; |
16505 | ||
16506 | default: | |
16507 | go_unreachable(); | |
16508 | } | |
16509 | ||
d0bcce51 | 16510 | bool ret = true; |
fcbea5e4 | 16511 | if (!mpfr_nan_p(mpc_realref(val)) |
16512 | && !mpfr_inf_p(mpc_realref(val)) | |
16513 | && !mpfr_zero_p(mpc_realref(val)) | |
16514 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 16515 | { |
16516 | if (issue_error) | |
71a45216 | 16517 | { |
631d5788 | 16518 | go_error_at(location, "complex real part overflow"); |
71a45216 | 16519 | this->set_invalid(); |
16520 | } | |
d0bcce51 | 16521 | ret = false; |
16522 | } | |
0c77715b | 16523 | |
fcbea5e4 | 16524 | if (!mpfr_nan_p(mpc_imagref(val)) |
16525 | && !mpfr_inf_p(mpc_imagref(val)) | |
16526 | && !mpfr_zero_p(mpc_imagref(val)) | |
16527 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 16528 | { |
16529 | if (issue_error) | |
71a45216 | 16530 | { |
631d5788 | 16531 | go_error_at(location, "complex imaginary part overflow"); |
71a45216 | 16532 | this->set_invalid(); |
16533 | } | |
d0bcce51 | 16534 | ret = false; |
16535 | } | |
0c77715b | 16536 | |
d0bcce51 | 16537 | if (ret) |
16538 | { | |
16539 | // Round the constant to the desired type. | |
fcbea5e4 | 16540 | mpc_t t; |
d0bcce51 | 16541 | switch (type->bits()) |
16542 | { | |
16543 | case 64: | |
fcbea5e4 | 16544 | mpc_init2(t, 24); |
d0bcce51 | 16545 | break; |
16546 | case 128: | |
fcbea5e4 | 16547 | mpc_init2(t, 53); |
d0bcce51 | 16548 | break; |
16549 | default: | |
16550 | go_unreachable(); | |
16551 | } | |
fcbea5e4 | 16552 | mpc_set(t, val, MPC_RNDNN); |
16553 | mpc_set(val, t, MPC_RNDNN); | |
16554 | mpc_clear(t); | |
d0bcce51 | 16555 | |
fcbea5e4 | 16556 | this->set_complex(type, val); |
d0bcce51 | 16557 | } |
16558 | ||
fcbea5e4 | 16559 | mpc_clear(val); |
0c77715b | 16560 | |
16561 | return ret; | |
16562 | } | |
16563 | ||
16564 | // Return an Expression for this value. | |
16565 | ||
16566 | Expression* | |
16567 | Numeric_constant::expression(Location loc) const | |
16568 | { | |
16569 | switch (this->classification_) | |
16570 | { | |
16571 | case NC_INT: | |
e67508fa | 16572 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 16573 | case NC_RUNE: |
16574 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
16575 | case NC_FLOAT: | |
16576 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
16577 | case NC_COMPLEX: | |
fcbea5e4 | 16578 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 16579 | case NC_INVALID: |
16580 | go_assert(saw_errors()); | |
16581 | return Expression::make_error(loc); | |
0c77715b | 16582 | default: |
16583 | go_unreachable(); | |
16584 | } | |
16585 | } |