]>
Commit | Line | Data |
---|---|---|
e440a328 | 1 | // expressions.cc -- Go frontend expression handling. |
2 | ||
3 | // Copyright 2009 The Go Authors. All rights reserved. | |
4 | // Use of this source code is governed by a BSD-style | |
5 | // license that can be found in the LICENSE file. | |
6 | ||
7 | #include "go-system.h" | |
8 | ||
ffe743ca | 9 | #include <algorithm> |
10 | ||
e440a328 | 11 | #include "go-c.h" |
12 | #include "gogo.h" | |
631d5788 | 13 | #include "go-diagnostics.h" |
438b4bec | 14 | #include "go-encode-id.h" |
e440a328 | 15 | #include "types.h" |
16 | #include "export.h" | |
17 | #include "import.h" | |
18 | #include "statements.h" | |
19 | #include "lex.h" | |
a9182619 | 20 | #include "runtime.h" |
6e193e6f | 21 | #include "backend.h" |
e440a328 | 22 | #include "expressions.h" |
d751bb78 | 23 | #include "ast-dump.h" |
e440a328 | 24 | |
25 | // Class Expression. | |
26 | ||
27 | Expression::Expression(Expression_classification classification, | |
b13c66cd | 28 | Location location) |
e440a328 | 29 | : classification_(classification), location_(location) |
30 | { | |
31 | } | |
32 | ||
33 | Expression::~Expression() | |
34 | { | |
35 | } | |
36 | ||
e440a328 | 37 | // Traverse the expressions. |
38 | ||
39 | int | |
40 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
41 | { | |
42 | Expression* expr = *pexpr; | |
43 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
44 | { | |
45 | int t = traverse->expression(pexpr); | |
46 | if (t == TRAVERSE_EXIT) | |
47 | return TRAVERSE_EXIT; | |
48 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
49 | return TRAVERSE_CONTINUE; | |
50 | } | |
51 | return expr->do_traverse(traverse); | |
52 | } | |
53 | ||
54 | // Traverse subexpressions of this expression. | |
55 | ||
56 | int | |
57 | Expression::traverse_subexpressions(Traverse* traverse) | |
58 | { | |
59 | return this->do_traverse(traverse); | |
60 | } | |
61 | ||
62 | // Default implementation for do_traverse for child classes. | |
63 | ||
64 | int | |
65 | Expression::do_traverse(Traverse*) | |
66 | { | |
67 | return TRAVERSE_CONTINUE; | |
68 | } | |
69 | ||
70 | // This virtual function is called by the parser if the value of this | |
a7549a6a | 71 | // expression is being discarded. By default, we give an error. |
72 | // Expressions with side effects override. | |
e440a328 | 73 | |
4f2138d7 | 74 | bool |
e440a328 | 75 | Expression::do_discarding_value() |
76 | { | |
a7549a6a | 77 | this->unused_value_error(); |
4f2138d7 | 78 | return false; |
e440a328 | 79 | } |
80 | ||
81 | // This virtual function is called to export expressions. This will | |
82 | // only be used by expressions which may be constant. | |
83 | ||
84 | void | |
85 | Expression::do_export(Export*) const | |
86 | { | |
c3e6f413 | 87 | go_unreachable(); |
e440a328 | 88 | } |
89 | ||
a7549a6a | 90 | // Give an error saying that the value of the expression is not used. |
e440a328 | 91 | |
92 | void | |
a7549a6a | 93 | Expression::unused_value_error() |
e440a328 | 94 | { |
4f2138d7 | 95 | this->report_error(_("value computed is not used")); |
e440a328 | 96 | } |
97 | ||
98 | // Note that this expression is an error. This is called by children | |
99 | // when they discover an error. | |
100 | ||
101 | void | |
102 | Expression::set_is_error() | |
103 | { | |
104 | this->classification_ = EXPRESSION_ERROR; | |
105 | } | |
106 | ||
107 | // For children to call to report an error conveniently. | |
108 | ||
109 | void | |
110 | Expression::report_error(const char* msg) | |
111 | { | |
631d5788 | 112 | go_error_at(this->location_, "%s", msg); |
e440a328 | 113 | this->set_is_error(); |
114 | } | |
115 | ||
116 | // Set types of variables and constants. This is implemented by the | |
117 | // child class. | |
118 | ||
119 | void | |
120 | Expression::determine_type(const Type_context* context) | |
121 | { | |
122 | this->do_determine_type(context); | |
123 | } | |
124 | ||
125 | // Set types when there is no context. | |
126 | ||
127 | void | |
128 | Expression::determine_type_no_context() | |
129 | { | |
130 | Type_context context; | |
131 | this->do_determine_type(&context); | |
132 | } | |
133 | ||
2c809f8f | 134 | // Return an expression handling any conversions which must be done during |
e440a328 | 135 | // assignment. |
136 | ||
2c809f8f | 137 | Expression* |
b4a33049 | 138 | Expression::convert_for_assignment(Gogo*, Type* lhs_type, |
2c809f8f | 139 | Expression* rhs, Location location) |
e440a328 | 140 | { |
2c809f8f | 141 | Type* rhs_type = rhs->type(); |
142 | if (lhs_type->is_error() | |
143 | || rhs_type->is_error() | |
144 | || rhs->is_error_expression()) | |
145 | return Expression::make_error(location); | |
e440a328 | 146 | |
22984631 | 147 | bool are_identical = Type::are_identical(lhs_type, rhs_type, false, NULL); |
148 | if (!are_identical && lhs_type->interface_type() != NULL) | |
e440a328 | 149 | { |
150 | if (rhs_type->interface_type() == NULL) | |
2c809f8f | 151 | return Expression::convert_type_to_interface(lhs_type, rhs, location); |
e440a328 | 152 | else |
2c809f8f | 153 | return Expression::convert_interface_to_interface(lhs_type, rhs, false, |
154 | location); | |
e440a328 | 155 | } |
22984631 | 156 | else if (!are_identical && rhs_type->interface_type() != NULL) |
2c809f8f | 157 | return Expression::convert_interface_to_type(lhs_type, rhs, location); |
411eb89e | 158 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 159 | { |
2c809f8f | 160 | // Assigning nil to a slice. |
2c809f8f | 161 | Expression* nil = Expression::make_nil(location); |
e67508fa | 162 | Expression* zero = Expression::make_integer_ul(0, NULL, location); |
2c809f8f | 163 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); |
e440a328 | 164 | } |
165 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 166 | return Expression::make_nil(location); |
22984631 | 167 | else if (are_identical) |
e440a328 | 168 | { |
22984631 | 169 | if (lhs_type->forwarded() != rhs_type->forwarded()) |
170 | { | |
171 | // Different but identical types require an explicit | |
172 | // conversion. This happens with type aliases. | |
173 | return Expression::make_cast(lhs_type, rhs, location); | |
174 | } | |
175 | ||
e440a328 | 176 | // No conversion is needed. |
2c809f8f | 177 | return rhs; |
178 | } | |
179 | else if (lhs_type->points_to() != NULL) | |
180 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
181 | else if (lhs_type->is_numeric_type()) | |
182 | return Expression::make_cast(lhs_type, rhs, location); | |
183 | else if ((lhs_type->struct_type() != NULL | |
184 | && rhs_type->struct_type() != NULL) | |
185 | || (lhs_type->array_type() != NULL | |
186 | && rhs_type->array_type() != NULL)) | |
e440a328 | 187 | { |
188 | // This conversion must be permitted by Go, or we wouldn't have | |
189 | // gotten here. | |
2c809f8f | 190 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 191 | } |
192 | else | |
2c809f8f | 193 | return rhs; |
e440a328 | 194 | } |
195 | ||
2c809f8f | 196 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 197 | // interface type. |
198 | ||
2c809f8f | 199 | Expression* |
200 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
201 | Location location) | |
e440a328 | 202 | { |
e440a328 | 203 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
204 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
205 | ||
206 | // Since RHS_TYPE is a static type, we can create the interface | |
207 | // method table at compile time. | |
208 | ||
209 | // When setting an interface to nil, we just set both fields to | |
210 | // NULL. | |
2c809f8f | 211 | Type* rhs_type = rhs->type(); |
e440a328 | 212 | if (rhs_type->is_nil_type()) |
63697958 | 213 | { |
2c809f8f | 214 | Expression* nil = Expression::make_nil(location); |
215 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 216 | } |
e440a328 | 217 | |
218 | // This should have been checked already. | |
2b8629dd | 219 | if (!lhs_interface_type->implements_interface(rhs_type, NULL)) |
220 | { | |
221 | go_assert(saw_errors()); | |
222 | return Expression::make_error(location); | |
223 | } | |
e440a328 | 224 | |
e440a328 | 225 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
226 | // then the first field is the type descriptor for RHS_TYPE. | |
227 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 228 | Expression* first_field; |
e440a328 | 229 | if (lhs_is_empty) |
2c809f8f | 230 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 231 | else |
232 | { | |
233 | // Build the interface method table for this interface and this | |
234 | // object type: a list of function pointers for each interface | |
235 | // method. | |
236 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 237 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 238 | bool is_pointer = false; |
c0cab2ec | 239 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 240 | { |
241 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 242 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 243 | is_pointer = true; |
244 | } | |
c0cab2ec | 245 | if (rhs_named_type != NULL) |
2c809f8f | 246 | first_field = |
247 | rhs_named_type->interface_method_table(lhs_interface_type, | |
248 | is_pointer); | |
c0cab2ec | 249 | else if (rhs_struct_type != NULL) |
2c809f8f | 250 | first_field = |
251 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
252 | is_pointer); | |
c0cab2ec | 253 | else |
2c809f8f | 254 | first_field = Expression::make_nil(location); |
e440a328 | 255 | } |
e440a328 | 256 | |
2c809f8f | 257 | Expression* obj; |
e440a328 | 258 | if (rhs_type->points_to() != NULL) |
259 | { | |
2c809f8f | 260 | // We are assigning a pointer to the interface; the interface |
e440a328 | 261 | // holds the pointer itself. |
2c809f8f | 262 | obj = rhs; |
263 | } | |
264 | else | |
265 | { | |
266 | // We are assigning a non-pointer value to the interface; the | |
45ff893b | 267 | // interface gets a copy of the value in the heap if it escapes. |
268 | // TODO(cmang): Associate escape state state of RHS with newly | |
269 | // created OBJ. | |
2c809f8f | 270 | obj = Expression::make_heap_expression(rhs, location); |
e440a328 | 271 | } |
272 | ||
2c809f8f | 273 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
274 | } | |
e440a328 | 275 | |
2c809f8f | 276 | // Return an expression for the type descriptor of RHS. |
e440a328 | 277 | |
2c809f8f | 278 | Expression* |
279 | Expression::get_interface_type_descriptor(Expression* rhs) | |
280 | { | |
281 | go_assert(rhs->type()->interface_type() != NULL); | |
282 | Location location = rhs->location(); | |
e440a328 | 283 | |
2c809f8f | 284 | // The type descriptor is the first field of an empty interface. |
285 | if (rhs->type()->interface_type()->is_empty()) | |
286 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
287 | location); | |
288 | ||
289 | Expression* mtable = | |
290 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 291 | |
2c809f8f | 292 | Expression* descriptor = |
f614ea8b | 293 | Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, location); |
2c809f8f | 294 | descriptor = Expression::make_field_reference(descriptor, 0, location); |
295 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 296 | |
2c809f8f | 297 | Expression* eq = |
298 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
299 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 300 | } |
301 | ||
2c809f8f | 302 | // Return an expression for the conversion of an interface type to an |
e440a328 | 303 | // interface type. |
304 | ||
2c809f8f | 305 | Expression* |
306 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
307 | bool for_type_guard, | |
308 | Location location) | |
e440a328 | 309 | { |
8ba8cc87 | 310 | if (Type::are_identical(lhs_type, rhs->type(), false, NULL)) |
311 | return rhs; | |
312 | ||
e440a328 | 313 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
314 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
315 | ||
e440a328 | 316 | // In the general case this requires runtime examination of the type |
317 | // method table to match it up with the interface methods. | |
318 | ||
319 | // FIXME: If all of the methods in the right hand side interface | |
320 | // also appear in the left hand side interface, then we don't need | |
321 | // to do a runtime check, although we still need to build a new | |
322 | // method table. | |
323 | ||
8ba8cc87 | 324 | // We are going to evaluate RHS multiple times. |
325 | go_assert(rhs->is_variable()); | |
326 | ||
e440a328 | 327 | // Get the type descriptor for the right hand side. This will be |
328 | // NULL for a nil interface. | |
2c809f8f | 329 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
330 | Expression* lhs_type_expr = | |
331 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 332 | |
2c809f8f | 333 | Expression* first_field; |
e440a328 | 334 | if (for_type_guard) |
335 | { | |
336 | // A type assertion fails when converting a nil interface. | |
6098d6cb | 337 | first_field = Runtime::make_call(Runtime::ASSERTITAB, location, 2, |
338 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 339 | } |
340 | else if (lhs_is_empty) | |
341 | { | |
2c809f8f | 342 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 343 | // first field is just the type descriptor of the object. |
2c809f8f | 344 | first_field = rhs_type_expr; |
e440a328 | 345 | } |
346 | else | |
347 | { | |
348 | // A conversion to a non-empty interface may fail, but unlike a | |
349 | // type assertion converting nil will always succeed. | |
6098d6cb | 350 | first_field = Runtime::make_call(Runtime::REQUIREITAB, location, 2, |
351 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 352 | } |
353 | ||
354 | // The second field is simply the object pointer. | |
2c809f8f | 355 | Expression* obj = |
356 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
357 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 358 | } |
359 | ||
2c809f8f | 360 | // Return an expression for the conversion of an interface type to a |
e440a328 | 361 | // non-interface type. |
362 | ||
2c809f8f | 363 | Expression* |
364 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
365 | Location location) | |
e440a328 | 366 | { |
8ba8cc87 | 367 | // We are going to evaluate RHS multiple times. |
368 | go_assert(rhs->is_variable()); | |
369 | ||
e440a328 | 370 | // Call a function to check that the type is valid. The function |
371 | // will panic with an appropriate runtime type error if the type is | |
372 | // not valid. | |
2c809f8f | 373 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
374 | location); | |
375 | Expression* rhs_descriptor = | |
376 | Expression::get_interface_type_descriptor(rhs); | |
377 | ||
378 | Type* rhs_type = rhs->type(); | |
379 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
380 | location); | |
381 | ||
6098d6cb | 382 | Expression* check_iface = Runtime::make_call(Runtime::ASSERTI2T, |
2c809f8f | 383 | location, 3, lhs_type_expr, |
384 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 385 | |
386 | // If the call succeeds, pull out the value. | |
2c809f8f | 387 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
388 | location); | |
e440a328 | 389 | |
390 | // If the value is a pointer, then it is the value we want. | |
391 | // Otherwise it points to the value. | |
392 | if (lhs_type->points_to() == NULL) | |
393 | { | |
2c809f8f | 394 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
395 | location); | |
f614ea8b | 396 | obj = Expression::make_dereference(obj, NIL_CHECK_NOT_NEEDED, |
397 | location); | |
e440a328 | 398 | } |
2c809f8f | 399 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 400 | } |
401 | ||
ea664253 | 402 | // Convert an expression to its backend representation. This is implemented by |
403 | // the child class. Not that it is not in general safe to call this multiple | |
e440a328 | 404 | // times for a single expression, but that we don't catch such errors. |
405 | ||
ea664253 | 406 | Bexpression* |
407 | Expression::get_backend(Translate_context* context) | |
e440a328 | 408 | { |
409 | // The child may have marked this expression as having an error. | |
410 | if (this->classification_ == EXPRESSION_ERROR) | |
ea664253 | 411 | return context->backend()->error_expression(); |
e440a328 | 412 | |
ea664253 | 413 | return this->do_get_backend(context); |
e440a328 | 414 | } |
415 | ||
48c2a53a | 416 | // Return a backend expression for VAL. |
417 | Bexpression* | |
418 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
419 | Numeric_constant* val) | |
e440a328 | 420 | { |
48c2a53a | 421 | Gogo* gogo = context->gogo(); |
422 | Type* type = val->type(); | |
423 | if (type == NULL) | |
424 | return gogo->backend()->error_expression(); | |
e440a328 | 425 | |
48c2a53a | 426 | Btype* btype = type->get_backend(gogo); |
427 | Bexpression* ret; | |
428 | if (type->integer_type() != NULL) | |
e440a328 | 429 | { |
430 | mpz_t ival; | |
48c2a53a | 431 | if (!val->to_int(&ival)) |
432 | { | |
433 | go_assert(saw_errors()); | |
434 | return gogo->backend()->error_expression(); | |
435 | } | |
436 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 437 | mpz_clear(ival); |
e440a328 | 438 | } |
48c2a53a | 439 | else if (type->float_type() != NULL) |
e440a328 | 440 | { |
48c2a53a | 441 | mpfr_t fval; |
442 | if (!val->to_float(&fval)) | |
443 | { | |
444 | go_assert(saw_errors()); | |
445 | return gogo->backend()->error_expression(); | |
446 | } | |
447 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
448 | mpfr_clear(fval); | |
e440a328 | 449 | } |
48c2a53a | 450 | else if (type->complex_type() != NULL) |
e440a328 | 451 | { |
fcbea5e4 | 452 | mpc_t cval; |
453 | if (!val->to_complex(&cval)) | |
48c2a53a | 454 | { |
455 | go_assert(saw_errors()); | |
456 | return gogo->backend()->error_expression(); | |
457 | } | |
fcbea5e4 | 458 | ret = gogo->backend()->complex_constant_expression(btype, cval); |
459 | mpc_clear(cval); | |
e440a328 | 460 | } |
461 | else | |
c3e6f413 | 462 | go_unreachable(); |
e440a328 | 463 | |
48c2a53a | 464 | return ret; |
e440a328 | 465 | } |
466 | ||
2c809f8f | 467 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
468 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 469 | |
2c809f8f | 470 | Expression* |
471 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 472 | { |
2c809f8f | 473 | Type* val_type = val->type(); |
474 | Type* bound_type = Type::lookup_integer_type("int"); | |
475 | ||
476 | int val_type_size; | |
477 | bool val_is_unsigned = false; | |
478 | if (val_type->integer_type() != NULL) | |
479 | { | |
480 | val_type_size = val_type->integer_type()->bits(); | |
481 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
482 | } | |
483 | else | |
484 | { | |
485 | if (!val_type->is_numeric_type() | |
486 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
487 | { | |
488 | go_assert(saw_errors()); | |
489 | return Expression::make_boolean(true, loc); | |
490 | } | |
e440a328 | 491 | |
2c809f8f | 492 | if (val_type->complex_type() != NULL) |
493 | val_type_size = val_type->complex_type()->bits(); | |
494 | else | |
495 | val_type_size = val_type->float_type()->bits(); | |
496 | } | |
497 | ||
498 | Expression* negative_index = Expression::make_boolean(false, loc); | |
499 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
500 | if (!val_is_unsigned) | |
e440a328 | 501 | { |
e67508fa | 502 | Expression* zero = Expression::make_integer_ul(0, val_type, loc); |
2c809f8f | 503 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); |
e440a328 | 504 | } |
505 | ||
2c809f8f | 506 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 507 | if (val_type_size > bound_type_size |
508 | || (val_type_size == bound_type_size | |
2c809f8f | 509 | && val_is_unsigned)) |
510 | { | |
511 | mpz_t one; | |
512 | mpz_init_set_ui(one, 1UL); | |
513 | ||
514 | // maxval = 2^(bound_type_size - 1) - 1 | |
515 | mpz_t maxval; | |
516 | mpz_init(maxval); | |
517 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
518 | mpz_sub_ui(maxval, maxval, 1); | |
e67508fa | 519 | Expression* max = Expression::make_integer_z(&maxval, val_type, loc); |
2c809f8f | 520 | mpz_clear(one); |
521 | mpz_clear(maxval); | |
522 | ||
523 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 524 | } |
525 | ||
2c809f8f | 526 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
527 | loc); | |
e440a328 | 528 | } |
529 | ||
d751bb78 | 530 | void |
531 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
532 | { | |
533 | this->do_dump_expression(ast_dump_context); | |
534 | } | |
535 | ||
e440a328 | 536 | // Error expressions. This are used to avoid cascading errors. |
537 | ||
538 | class Error_expression : public Expression | |
539 | { | |
540 | public: | |
b13c66cd | 541 | Error_expression(Location location) |
e440a328 | 542 | : Expression(EXPRESSION_ERROR, location) |
543 | { } | |
544 | ||
545 | protected: | |
546 | bool | |
547 | do_is_constant() const | |
548 | { return true; } | |
549 | ||
550 | bool | |
0c77715b | 551 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 552 | { |
0c77715b | 553 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 554 | return true; |
555 | } | |
556 | ||
4f2138d7 | 557 | bool |
e440a328 | 558 | do_discarding_value() |
4f2138d7 | 559 | { return true; } |
e440a328 | 560 | |
561 | Type* | |
562 | do_type() | |
563 | { return Type::make_error_type(); } | |
564 | ||
565 | void | |
566 | do_determine_type(const Type_context*) | |
567 | { } | |
568 | ||
569 | Expression* | |
570 | do_copy() | |
571 | { return this; } | |
572 | ||
573 | bool | |
574 | do_is_addressable() const | |
575 | { return true; } | |
576 | ||
ea664253 | 577 | Bexpression* |
578 | do_get_backend(Translate_context* context) | |
579 | { return context->backend()->error_expression(); } | |
d751bb78 | 580 | |
581 | void | |
582 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 583 | }; |
584 | ||
d751bb78 | 585 | // Dump the ast representation for an error expression to a dump context. |
586 | ||
587 | void | |
588 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
589 | { | |
590 | ast_dump_context->ostream() << "_Error_" ; | |
591 | } | |
592 | ||
e440a328 | 593 | Expression* |
b13c66cd | 594 | Expression::make_error(Location location) |
e440a328 | 595 | { |
596 | return new Error_expression(location); | |
597 | } | |
598 | ||
599 | // An expression which is really a type. This is used during parsing. | |
600 | // It is an error if these survive after lowering. | |
601 | ||
602 | class | |
603 | Type_expression : public Expression | |
604 | { | |
605 | public: | |
b13c66cd | 606 | Type_expression(Type* type, Location location) |
e440a328 | 607 | : Expression(EXPRESSION_TYPE, location), |
608 | type_(type) | |
609 | { } | |
610 | ||
611 | protected: | |
612 | int | |
613 | do_traverse(Traverse* traverse) | |
614 | { return Type::traverse(this->type_, traverse); } | |
615 | ||
616 | Type* | |
617 | do_type() | |
618 | { return this->type_; } | |
619 | ||
620 | void | |
621 | do_determine_type(const Type_context*) | |
622 | { } | |
623 | ||
624 | void | |
625 | do_check_types(Gogo*) | |
626 | { this->report_error(_("invalid use of type")); } | |
627 | ||
628 | Expression* | |
629 | do_copy() | |
630 | { return this; } | |
631 | ||
ea664253 | 632 | Bexpression* |
633 | do_get_backend(Translate_context*) | |
c3e6f413 | 634 | { go_unreachable(); } |
e440a328 | 635 | |
d751bb78 | 636 | void do_dump_expression(Ast_dump_context*) const; |
637 | ||
e440a328 | 638 | private: |
639 | // The type which we are representing as an expression. | |
640 | Type* type_; | |
641 | }; | |
642 | ||
d751bb78 | 643 | void |
644 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
645 | { | |
646 | ast_dump_context->dump_type(this->type_); | |
647 | } | |
648 | ||
e440a328 | 649 | Expression* |
b13c66cd | 650 | Expression::make_type(Type* type, Location location) |
e440a328 | 651 | { |
652 | return new Type_expression(type, location); | |
653 | } | |
654 | ||
e03bdf36 | 655 | // Class Parser_expression. |
656 | ||
657 | Type* | |
658 | Parser_expression::do_type() | |
659 | { | |
660 | // We should never really ask for the type of a Parser_expression. | |
661 | // However, it can happen, at least when we have an invalid const | |
662 | // whose initializer refers to the const itself. In that case we | |
663 | // may ask for the type when lowering the const itself. | |
c484d925 | 664 | go_assert(saw_errors()); |
e03bdf36 | 665 | return Type::make_error_type(); |
666 | } | |
667 | ||
e440a328 | 668 | // Class Var_expression. |
669 | ||
670 | // Lower a variable expression. Here we just make sure that the | |
671 | // initialization expression of the variable has been lowered. This | |
672 | // ensures that we will be able to determine the type of the variable | |
673 | // if necessary. | |
674 | ||
675 | Expression* | |
ceeb4318 | 676 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
677 | Statement_inserter* inserter, int) | |
e440a328 | 678 | { |
679 | if (this->variable_->is_variable()) | |
680 | { | |
681 | Variable* var = this->variable_->var_value(); | |
682 | // This is either a local variable or a global variable. A | |
683 | // reference to a variable which is local to an enclosing | |
684 | // function will be a reference to a field in a closure. | |
685 | if (var->is_global()) | |
ceeb4318 | 686 | { |
687 | function = NULL; | |
688 | inserter = NULL; | |
689 | } | |
690 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 691 | } |
692 | return this; | |
693 | } | |
694 | ||
e440a328 | 695 | // Return the type of a reference to a variable. |
696 | ||
697 | Type* | |
698 | Var_expression::do_type() | |
699 | { | |
700 | if (this->variable_->is_variable()) | |
701 | return this->variable_->var_value()->type(); | |
702 | else if (this->variable_->is_result_variable()) | |
703 | return this->variable_->result_var_value()->type(); | |
704 | else | |
c3e6f413 | 705 | go_unreachable(); |
e440a328 | 706 | } |
707 | ||
0ab09e06 | 708 | // Determine the type of a reference to a variable. |
709 | ||
710 | void | |
711 | Var_expression::do_determine_type(const Type_context*) | |
712 | { | |
713 | if (this->variable_->is_variable()) | |
714 | this->variable_->var_value()->determine_type(); | |
715 | } | |
716 | ||
e440a328 | 717 | // Something takes the address of this variable. This means that we |
718 | // may want to move the variable onto the heap. | |
719 | ||
720 | void | |
721 | Var_expression::do_address_taken(bool escapes) | |
722 | { | |
723 | if (!escapes) | |
f325319b | 724 | { |
725 | if (this->variable_->is_variable()) | |
726 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
727 | else if (this->variable_->is_result_variable()) | |
728 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
729 | else | |
730 | go_unreachable(); | |
731 | } | |
e440a328 | 732 | else |
f325319b | 733 | { |
734 | if (this->variable_->is_variable()) | |
735 | this->variable_->var_value()->set_address_taken(); | |
736 | else if (this->variable_->is_result_variable()) | |
737 | this->variable_->result_var_value()->set_address_taken(); | |
738 | else | |
739 | go_unreachable(); | |
740 | } | |
45ff893b | 741 | |
742 | if (this->variable_->is_variable() | |
743 | && this->variable_->var_value()->is_in_heap()) | |
744 | { | |
745 | Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP); | |
746 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
747 | } | |
e440a328 | 748 | } |
749 | ||
ea664253 | 750 | // Get the backend representation for a reference to a variable. |
e440a328 | 751 | |
ea664253 | 752 | Bexpression* |
753 | Var_expression::do_get_backend(Translate_context* context) | |
e440a328 | 754 | { |
fe2f84cf | 755 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
756 | context->function()); | |
fe2f84cf | 757 | bool is_in_heap; |
c6777780 | 758 | Location loc = this->location(); |
9b27b43c | 759 | Btype* btype; |
760 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 761 | if (this->variable_->is_variable()) |
9b27b43c | 762 | { |
763 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
764 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
765 | } | |
fe2f84cf | 766 | else if (this->variable_->is_result_variable()) |
9b27b43c | 767 | { |
768 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
769 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
770 | } | |
fe2f84cf | 771 | else |
c3e6f413 | 772 | go_unreachable(); |
c6777780 | 773 | |
d4e6573e | 774 | Bexpression* ret = |
7af8e400 | 775 | context->backend()->var_expression(bvar, loc); |
fe2f84cf | 776 | if (is_in_heap) |
9b27b43c | 777 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
ea664253 | 778 | return ret; |
e440a328 | 779 | } |
780 | ||
d751bb78 | 781 | // Ast dump for variable expression. |
782 | ||
783 | void | |
784 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
785 | { | |
cd5e24c1 | 786 | ast_dump_context->ostream() << this->variable_->message_name() ; |
d751bb78 | 787 | } |
788 | ||
e440a328 | 789 | // Make a reference to a variable in an expression. |
790 | ||
791 | Expression* | |
b13c66cd | 792 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 793 | { |
794 | if (var->is_sink()) | |
795 | return Expression::make_sink(location); | |
796 | ||
797 | // FIXME: Creating a new object for each reference to a variable is | |
798 | // wasteful. | |
799 | return new Var_expression(var, location); | |
800 | } | |
801 | ||
b0c09712 | 802 | // Class Enclosed_var_expression. |
803 | ||
804 | int | |
805 | Enclosed_var_expression::do_traverse(Traverse*) | |
806 | { | |
807 | return TRAVERSE_CONTINUE; | |
808 | } | |
809 | ||
810 | // Lower the reference to the enclosed variable. | |
811 | ||
812 | Expression* | |
813 | Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function, | |
814 | Statement_inserter* inserter, int) | |
815 | { | |
816 | gogo->lower_expression(function, inserter, &this->reference_); | |
817 | return this; | |
818 | } | |
819 | ||
820 | // Flatten the reference to the enclosed variable. | |
821 | ||
822 | Expression* | |
823 | Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function, | |
824 | Statement_inserter* inserter) | |
825 | { | |
826 | gogo->flatten_expression(function, inserter, &this->reference_); | |
827 | return this; | |
828 | } | |
829 | ||
830 | void | |
831 | Enclosed_var_expression::do_address_taken(bool escapes) | |
832 | { | |
833 | if (!escapes) | |
834 | { | |
835 | if (this->variable_->is_variable()) | |
836 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
837 | else if (this->variable_->is_result_variable()) | |
838 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
839 | else | |
840 | go_unreachable(); | |
841 | } | |
842 | else | |
843 | { | |
844 | if (this->variable_->is_variable()) | |
845 | this->variable_->var_value()->set_address_taken(); | |
846 | else if (this->variable_->is_result_variable()) | |
847 | this->variable_->result_var_value()->set_address_taken(); | |
848 | else | |
849 | go_unreachable(); | |
850 | } | |
45ff893b | 851 | |
852 | if (this->variable_->is_variable() | |
853 | && this->variable_->var_value()->is_in_heap()) | |
854 | Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP); | |
b0c09712 | 855 | } |
856 | ||
857 | // Ast dump for enclosed variable expression. | |
858 | ||
859 | void | |
860 | Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const | |
861 | { | |
cd5e24c1 | 862 | adc->ostream() << this->variable_->message_name(); |
b0c09712 | 863 | } |
864 | ||
865 | // Make a reference to a variable within an enclosing function. | |
866 | ||
867 | Expression* | |
868 | Expression::make_enclosing_var_reference(Expression* reference, | |
869 | Named_object* var, Location location) | |
870 | { | |
871 | return new Enclosed_var_expression(reference, var, location); | |
872 | } | |
873 | ||
e440a328 | 874 | // Class Temporary_reference_expression. |
875 | ||
876 | // The type. | |
877 | ||
878 | Type* | |
879 | Temporary_reference_expression::do_type() | |
880 | { | |
881 | return this->statement_->type(); | |
882 | } | |
883 | ||
884 | // Called if something takes the address of this temporary variable. | |
885 | // We never have to move temporary variables to the heap, but we do | |
886 | // need to know that they must live in the stack rather than in a | |
887 | // register. | |
888 | ||
889 | void | |
890 | Temporary_reference_expression::do_address_taken(bool) | |
891 | { | |
892 | this->statement_->set_is_address_taken(); | |
893 | } | |
894 | ||
ea664253 | 895 | // Get a backend expression referring to the variable. |
e440a328 | 896 | |
ea664253 | 897 | Bexpression* |
898 | Temporary_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 899 | { |
cd440cff | 900 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 901 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
7af8e400 | 902 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 903 | |
cd440cff | 904 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 905 | // that the Go frontend can. In some cases this means that a |
906 | // temporary variable won't have the right backend type. Correct | |
907 | // that here by adding a type cast. We need to use base() to push | |
908 | // the circularity down one level. | |
cd440cff | 909 | Type* stype = this->statement_->type(); |
ceeb4318 | 910 | if (!this->is_lvalue_ |
03118c21 | 911 | && stype->points_to() != NULL |
912 | && stype->points_to()->is_void_type()) | |
eefc1ed3 | 913 | { |
cd440cff | 914 | Btype* btype = this->type()->base()->get_backend(gogo); |
915 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 916 | } |
ea664253 | 917 | return ret; |
e440a328 | 918 | } |
919 | ||
d751bb78 | 920 | // Ast dump for temporary reference. |
921 | ||
922 | void | |
923 | Temporary_reference_expression::do_dump_expression( | |
924 | Ast_dump_context* ast_dump_context) const | |
925 | { | |
926 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
927 | } | |
928 | ||
e440a328 | 929 | // Make a reference to a temporary variable. |
930 | ||
ceeb4318 | 931 | Temporary_reference_expression* |
e440a328 | 932 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 933 | Location location) |
e440a328 | 934 | { |
935 | return new Temporary_reference_expression(statement, location); | |
936 | } | |
937 | ||
e9d3367e | 938 | // Class Set_and_use_temporary_expression. |
939 | ||
940 | // Return the type. | |
941 | ||
942 | Type* | |
943 | Set_and_use_temporary_expression::do_type() | |
944 | { | |
945 | return this->statement_->type(); | |
946 | } | |
947 | ||
0afbb937 | 948 | // Determine the type of the expression. |
949 | ||
950 | void | |
951 | Set_and_use_temporary_expression::do_determine_type( | |
952 | const Type_context* context) | |
953 | { | |
954 | this->expr_->determine_type(context); | |
955 | } | |
956 | ||
e9d3367e | 957 | // Take the address. |
958 | ||
959 | void | |
960 | Set_and_use_temporary_expression::do_address_taken(bool) | |
961 | { | |
962 | this->statement_->set_is_address_taken(); | |
963 | } | |
964 | ||
965 | // Return the backend representation. | |
966 | ||
ea664253 | 967 | Bexpression* |
968 | Set_and_use_temporary_expression::do_get_backend(Translate_context* context) | |
e9d3367e | 969 | { |
e9d3367e | 970 | Location loc = this->location(); |
a302c105 | 971 | Gogo* gogo = context->gogo(); |
972 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
7af8e400 | 973 | Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, loc); |
a302c105 | 974 | |
0ab48656 | 975 | Named_object* fn = context->function(); |
976 | go_assert(fn != NULL); | |
977 | Bfunction* bfn = fn->func_value()->get_or_make_decl(gogo, fn); | |
ea664253 | 978 | Bexpression* bexpr = this->expr_->get_backend(context); |
0ab48656 | 979 | Bstatement* set = gogo->backend()->assignment_statement(bfn, lvar_ref, |
980 | bexpr, loc); | |
7af8e400 | 981 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); |
a302c105 | 982 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); |
ea664253 | 983 | return ret; |
e9d3367e | 984 | } |
985 | ||
986 | // Dump. | |
987 | ||
988 | void | |
989 | Set_and_use_temporary_expression::do_dump_expression( | |
990 | Ast_dump_context* ast_dump_context) const | |
991 | { | |
992 | ast_dump_context->ostream() << '('; | |
993 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
994 | ast_dump_context->ostream() << " = "; | |
995 | this->expr_->dump_expression(ast_dump_context); | |
996 | ast_dump_context->ostream() << ')'; | |
997 | } | |
998 | ||
999 | // Make a set-and-use temporary. | |
1000 | ||
1001 | Set_and_use_temporary_expression* | |
1002 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
1003 | Expression* expr, Location location) | |
1004 | { | |
1005 | return new Set_and_use_temporary_expression(statement, expr, location); | |
1006 | } | |
1007 | ||
e440a328 | 1008 | // A sink expression--a use of the blank identifier _. |
1009 | ||
1010 | class Sink_expression : public Expression | |
1011 | { | |
1012 | public: | |
b13c66cd | 1013 | Sink_expression(Location location) |
e440a328 | 1014 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 1015 | type_(NULL), bvar_(NULL) |
e440a328 | 1016 | { } |
1017 | ||
1018 | protected: | |
4f2138d7 | 1019 | bool |
e440a328 | 1020 | do_discarding_value() |
4f2138d7 | 1021 | { return true; } |
e440a328 | 1022 | |
1023 | Type* | |
1024 | do_type(); | |
1025 | ||
1026 | void | |
1027 | do_determine_type(const Type_context*); | |
1028 | ||
1029 | Expression* | |
1030 | do_copy() | |
1031 | { return new Sink_expression(this->location()); } | |
1032 | ||
ea664253 | 1033 | Bexpression* |
1034 | do_get_backend(Translate_context*); | |
e440a328 | 1035 | |
d751bb78 | 1036 | void |
1037 | do_dump_expression(Ast_dump_context*) const; | |
1038 | ||
e440a328 | 1039 | private: |
1040 | // The type of this sink variable. | |
1041 | Type* type_; | |
1042 | // The temporary variable we generate. | |
aa93217a | 1043 | Bvariable* bvar_; |
e440a328 | 1044 | }; |
1045 | ||
1046 | // Return the type of a sink expression. | |
1047 | ||
1048 | Type* | |
1049 | Sink_expression::do_type() | |
1050 | { | |
1051 | if (this->type_ == NULL) | |
1052 | return Type::make_sink_type(); | |
1053 | return this->type_; | |
1054 | } | |
1055 | ||
1056 | // Determine the type of a sink expression. | |
1057 | ||
1058 | void | |
1059 | Sink_expression::do_determine_type(const Type_context* context) | |
1060 | { | |
1061 | if (context->type != NULL) | |
1062 | this->type_ = context->type; | |
1063 | } | |
1064 | ||
1065 | // Return a temporary variable for a sink expression. This will | |
1066 | // presumably be a write-only variable which the middle-end will drop. | |
1067 | ||
ea664253 | 1068 | Bexpression* |
1069 | Sink_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1070 | { |
aa93217a | 1071 | Location loc = this->location(); |
1072 | Gogo* gogo = context->gogo(); | |
1073 | if (this->bvar_ == NULL) | |
e440a328 | 1074 | { |
c484d925 | 1075 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 1076 | Named_object* fn = context->function(); |
1077 | go_assert(fn != NULL); | |
1078 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 1079 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 1080 | Bstatement* decl; |
1081 | this->bvar_ = | |
1082 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
1083 | false, loc, &decl); | |
d4e6573e | 1084 | Bexpression* var_ref = |
7af8e400 | 1085 | gogo->backend()->var_expression(this->bvar_, loc); |
aa93217a | 1086 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); |
ea664253 | 1087 | return var_ref; |
e440a328 | 1088 | } |
7af8e400 | 1089 | return gogo->backend()->var_expression(this->bvar_, loc); |
e440a328 | 1090 | } |
1091 | ||
d751bb78 | 1092 | // Ast dump for sink expression. |
1093 | ||
1094 | void | |
1095 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1096 | { | |
1097 | ast_dump_context->ostream() << "_" ; | |
1098 | } | |
1099 | ||
e440a328 | 1100 | // Make a sink expression. |
1101 | ||
1102 | Expression* | |
b13c66cd | 1103 | Expression::make_sink(Location location) |
e440a328 | 1104 | { |
1105 | return new Sink_expression(location); | |
1106 | } | |
1107 | ||
1108 | // Class Func_expression. | |
1109 | ||
1110 | // FIXME: Can a function expression appear in a constant expression? | |
1111 | // The value is unchanging. Initializing a constant to the address of | |
1112 | // a function seems like it could work, though there might be little | |
1113 | // point to it. | |
1114 | ||
e440a328 | 1115 | // Traversal. |
1116 | ||
1117 | int | |
1118 | Func_expression::do_traverse(Traverse* traverse) | |
1119 | { | |
1120 | return (this->closure_ == NULL | |
1121 | ? TRAVERSE_CONTINUE | |
1122 | : Expression::traverse(&this->closure_, traverse)); | |
1123 | } | |
1124 | ||
1125 | // Return the type of a function expression. | |
1126 | ||
1127 | Type* | |
1128 | Func_expression::do_type() | |
1129 | { | |
1130 | if (this->function_->is_function()) | |
1131 | return this->function_->func_value()->type(); | |
1132 | else if (this->function_->is_function_declaration()) | |
1133 | return this->function_->func_declaration_value()->type(); | |
1134 | else | |
c3e6f413 | 1135 | go_unreachable(); |
e440a328 | 1136 | } |
1137 | ||
ea664253 | 1138 | // Get the backend representation for the code of a function expression. |
e440a328 | 1139 | |
97267c39 | 1140 | Bexpression* |
8381eda7 | 1141 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1142 | { |
1143 | Function_type* fntype; | |
8381eda7 | 1144 | if (no->is_function()) |
1145 | fntype = no->func_value()->type(); | |
1146 | else if (no->is_function_declaration()) | |
1147 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1148 | else |
c3e6f413 | 1149 | go_unreachable(); |
e440a328 | 1150 | |
1151 | // Builtin functions are handled specially by Call_expression. We | |
1152 | // can't take their address. | |
1153 | if (fntype->is_builtin()) | |
1154 | { | |
631d5788 | 1155 | go_error_at(loc, |
1156 | "invalid use of special builtin function %qs; must be called", | |
1157 | no->message_name().c_str()); | |
97267c39 | 1158 | return gogo->backend()->error_expression(); |
e440a328 | 1159 | } |
1160 | ||
97267c39 | 1161 | Bfunction* fndecl; |
e440a328 | 1162 | if (no->is_function()) |
cf3cae55 | 1163 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1164 | else if (no->is_function_declaration()) |
cf3cae55 | 1165 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1166 | else |
c3e6f413 | 1167 | go_unreachable(); |
e440a328 | 1168 | |
97267c39 | 1169 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1170 | } |
1171 | ||
ea664253 | 1172 | // Get the backend representation for a function expression. This is used when |
1173 | // we take the address of a function rather than simply calling it. A func | |
8381eda7 | 1174 | // value is represented as a pointer to a block of memory. The first |
1175 | // word of that memory is a pointer to the function code. The | |
1176 | // remaining parts of that memory are the addresses of variables that | |
1177 | // the function closes over. | |
e440a328 | 1178 | |
ea664253 | 1179 | Bexpression* |
1180 | Func_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1181 | { |
8381eda7 | 1182 | // If there is no closure, just use the function descriptor. |
2010c17a | 1183 | if (this->closure_ == NULL) |
8381eda7 | 1184 | { |
1185 | Gogo* gogo = context->gogo(); | |
1186 | Named_object* no = this->function_; | |
1187 | Expression* descriptor; | |
1188 | if (no->is_function()) | |
1189 | descriptor = no->func_value()->descriptor(gogo, no); | |
1190 | else if (no->is_function_declaration()) | |
1191 | { | |
1192 | if (no->func_declaration_value()->type()->is_builtin()) | |
1193 | { | |
631d5788 | 1194 | go_error_at(this->location(), |
1195 | ("invalid use of special builtin function %qs; " | |
1196 | "must be called"), | |
1197 | no->message_name().c_str()); | |
ea664253 | 1198 | return gogo->backend()->error_expression(); |
8381eda7 | 1199 | } |
1200 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1201 | } | |
1202 | else | |
1203 | go_unreachable(); | |
2010c17a | 1204 | |
ea664253 | 1205 | Bexpression* bdesc = descriptor->get_backend(context); |
1206 | return gogo->backend()->address_expression(bdesc, this->location()); | |
8381eda7 | 1207 | } |
e440a328 | 1208 | |
8381eda7 | 1209 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1210 | |
8381eda7 | 1211 | // If there is a closure, then the closure is itself the function |
1212 | // expression. It is a pointer to a struct whose first field points | |
1213 | // to the function code and whose remaining fields are the addresses | |
1214 | // of the closed-over variables. | |
76818e19 | 1215 | Bexpression *bexpr = this->closure_->get_backend(context); |
1216 | ||
1217 | // Introduce a backend type conversion, to account for any differences | |
1218 | // between the argument type (function descriptor, struct with a | |
1219 | // single field) and the closure (struct with multiple fields). | |
1220 | Gogo* gogo = context->gogo(); | |
1221 | Btype *btype = this->type()->get_backend(gogo); | |
1222 | return gogo->backend()->convert_expression(btype, bexpr, this->location()); | |
e440a328 | 1223 | } |
1224 | ||
d751bb78 | 1225 | // Ast dump for function. |
1226 | ||
1227 | void | |
1228 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1229 | { | |
8b1c301d | 1230 | ast_dump_context->ostream() << this->function_->name(); |
1231 | if (this->closure_ != NULL) | |
1232 | { | |
1233 | ast_dump_context->ostream() << " {closure = "; | |
1234 | this->closure_->dump_expression(ast_dump_context); | |
1235 | ast_dump_context->ostream() << "}"; | |
1236 | } | |
d751bb78 | 1237 | } |
1238 | ||
e440a328 | 1239 | // Make a reference to a function in an expression. |
1240 | ||
1241 | Expression* | |
1242 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1243 | Location location) |
e440a328 | 1244 | { |
b1d7ecfa | 1245 | Func_expression* fe = new Func_expression(function, closure, location); |
1246 | ||
1247 | // Detect references to builtin functions and set the runtime code if | |
1248 | // appropriate. | |
1249 | if (function->is_function_declaration()) | |
1250 | fe->set_runtime_code(Runtime::name_to_code(function->name())); | |
1251 | return fe; | |
e440a328 | 1252 | } |
1253 | ||
c6837989 | 1254 | // Class Func_descriptor_expression. |
8381eda7 | 1255 | |
c6837989 | 1256 | // Constructor. |
8381eda7 | 1257 | |
c6837989 | 1258 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1259 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1260 | fn_(fn), dvar_(NULL) |
c6837989 | 1261 | { |
1262 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1263 | } | |
8381eda7 | 1264 | |
c6837989 | 1265 | // Traversal. |
8381eda7 | 1266 | |
c6837989 | 1267 | int |
1268 | Func_descriptor_expression::do_traverse(Traverse*) | |
1269 | { | |
1270 | return TRAVERSE_CONTINUE; | |
1271 | } | |
8381eda7 | 1272 | |
1273 | // All function descriptors have the same type. | |
1274 | ||
1275 | Type* Func_descriptor_expression::descriptor_type; | |
1276 | ||
1277 | void | |
1278 | Func_descriptor_expression::make_func_descriptor_type() | |
1279 | { | |
1280 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1281 | return; | |
1282 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1283 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1284 | Func_descriptor_expression::descriptor_type = | |
1285 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1286 | } | |
1287 | ||
1288 | Type* | |
1289 | Func_descriptor_expression::do_type() | |
1290 | { | |
1291 | Func_descriptor_expression::make_func_descriptor_type(); | |
1292 | return Func_descriptor_expression::descriptor_type; | |
1293 | } | |
1294 | ||
ea664253 | 1295 | // The backend representation for a function descriptor. |
8381eda7 | 1296 | |
ea664253 | 1297 | Bexpression* |
1298 | Func_descriptor_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1299 | { |
8381eda7 | 1300 | Named_object* no = this->fn_; |
1301 | Location loc = no->location(); | |
ea664253 | 1302 | if (this->dvar_ != NULL) |
7af8e400 | 1303 | return context->backend()->var_expression(this->dvar_, loc); |
8381eda7 | 1304 | |
ea664253 | 1305 | Gogo* gogo = context->gogo(); |
19272321 | 1306 | std::string var_name(gogo->function_descriptor_name(no)); |
09e57698 | 1307 | bool is_descriptor = false; |
1308 | if (no->is_function_declaration() | |
1309 | && !no->func_declaration_value()->asm_name().empty() | |
1310 | && Linemap::is_predeclared_location(no->location())) | |
19272321 | 1311 | is_descriptor = true; |
8381eda7 | 1312 | |
13f2fdb8 | 1313 | // The runtime package implements some functions defined in the |
1314 | // syscall package. Let the syscall package define the descriptor | |
1315 | // in this case. | |
1316 | if (gogo->compiling_runtime() | |
1317 | && gogo->package_name() == "runtime" | |
1318 | && no->is_function() | |
1319 | && !no->func_value()->asm_name().empty() | |
1320 | && no->func_value()->asm_name().compare(0, 8, "syscall.") == 0) | |
1321 | is_descriptor = true; | |
1322 | ||
8381eda7 | 1323 | Btype* btype = this->type()->get_backend(gogo); |
1324 | ||
1325 | Bvariable* bvar; | |
438b4bec | 1326 | std::string asm_name(go_selectively_encode_id(var_name)); |
09e57698 | 1327 | if (no->package() != NULL || is_descriptor) |
438b4bec | 1328 | bvar = context->backend()->immutable_struct_reference(var_name, asm_name, |
1329 | btype, loc); | |
8381eda7 | 1330 | else |
1331 | { | |
1332 | Location bloc = Linemap::predeclared_location(); | |
1333 | bool is_hidden = ((no->is_function() | |
1334 | && no->func_value()->enclosing() != NULL) | |
1335 | || Gogo::is_thunk(no)); | |
438b4bec | 1336 | bvar = context->backend()->immutable_struct(var_name, asm_name, |
1337 | is_hidden, false, | |
8381eda7 | 1338 | btype, bloc); |
1339 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1340 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1341 | Expression* init = |
1342 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1343 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1344 | bcontext.set_is_const(); | |
ea664253 | 1345 | Bexpression* binit = init->get_backend(&bcontext); |
8381eda7 | 1346 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, |
1347 | false, btype, bloc, binit); | |
1348 | } | |
1349 | ||
1350 | this->dvar_ = bvar; | |
7af8e400 | 1351 | return gogo->backend()->var_expression(bvar, loc); |
8381eda7 | 1352 | } |
1353 | ||
c6837989 | 1354 | // Print a function descriptor expression. |
1355 | ||
1356 | void | |
1357 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1358 | { | |
1359 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1360 | } | |
1361 | ||
8381eda7 | 1362 | // Make a function descriptor expression. |
1363 | ||
c6837989 | 1364 | Func_descriptor_expression* |
1365 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1366 | { |
c6837989 | 1367 | return new Func_descriptor_expression(fn); |
8381eda7 | 1368 | } |
1369 | ||
1370 | // Make the function descriptor type, so that it can be converted. | |
1371 | ||
1372 | void | |
1373 | Expression::make_func_descriptor_type() | |
1374 | { | |
1375 | Func_descriptor_expression::make_func_descriptor_type(); | |
1376 | } | |
1377 | ||
1378 | // A reference to just the code of a function. | |
1379 | ||
1380 | class Func_code_reference_expression : public Expression | |
1381 | { | |
1382 | public: | |
1383 | Func_code_reference_expression(Named_object* function, Location location) | |
1384 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1385 | function_(function) | |
1386 | { } | |
1387 | ||
1388 | protected: | |
1389 | int | |
1390 | do_traverse(Traverse*) | |
1391 | { return TRAVERSE_CONTINUE; } | |
1392 | ||
f9ca30f9 | 1393 | bool |
3ae06f68 | 1394 | do_is_static_initializer() const |
f9ca30f9 | 1395 | { return true; } |
1396 | ||
8381eda7 | 1397 | Type* |
1398 | do_type() | |
1399 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1400 | ||
1401 | void | |
1402 | do_determine_type(const Type_context*) | |
1403 | { } | |
1404 | ||
1405 | Expression* | |
1406 | do_copy() | |
1407 | { | |
1408 | return Expression::make_func_code_reference(this->function_, | |
1409 | this->location()); | |
1410 | } | |
1411 | ||
ea664253 | 1412 | Bexpression* |
1413 | do_get_backend(Translate_context*); | |
8381eda7 | 1414 | |
1415 | void | |
1416 | do_dump_expression(Ast_dump_context* context) const | |
1417 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1418 | ||
1419 | private: | |
1420 | // The function. | |
1421 | Named_object* function_; | |
1422 | }; | |
1423 | ||
ea664253 | 1424 | // Get the backend representation for a reference to function code. |
8381eda7 | 1425 | |
ea664253 | 1426 | Bexpression* |
1427 | Func_code_reference_expression::do_get_backend(Translate_context* context) | |
8381eda7 | 1428 | { |
ea664253 | 1429 | return Func_expression::get_code_pointer(context->gogo(), this->function_, |
1430 | this->location()); | |
8381eda7 | 1431 | } |
1432 | ||
1433 | // Make a reference to the code of a function. | |
1434 | ||
1435 | Expression* | |
1436 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1437 | { | |
1438 | return new Func_code_reference_expression(function, location); | |
1439 | } | |
1440 | ||
e440a328 | 1441 | // Class Unknown_expression. |
1442 | ||
1443 | // Return the name of an unknown expression. | |
1444 | ||
1445 | const std::string& | |
1446 | Unknown_expression::name() const | |
1447 | { | |
1448 | return this->named_object_->name(); | |
1449 | } | |
1450 | ||
1451 | // Lower a reference to an unknown name. | |
1452 | ||
1453 | Expression* | |
ceeb4318 | 1454 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1455 | { |
b13c66cd | 1456 | Location location = this->location(); |
e440a328 | 1457 | Named_object* no = this->named_object_; |
deded542 | 1458 | Named_object* real; |
1459 | if (!no->is_unknown()) | |
1460 | real = no; | |
1461 | else | |
e440a328 | 1462 | { |
deded542 | 1463 | real = no->unknown_value()->real_named_object(); |
1464 | if (real == NULL) | |
1465 | { | |
1466 | if (this->is_composite_literal_key_) | |
1467 | return this; | |
acf8e158 | 1468 | if (!this->no_error_message_) |
631d5788 | 1469 | go_error_at(location, "reference to undefined name %qs", |
1470 | this->named_object_->message_name().c_str()); | |
deded542 | 1471 | return Expression::make_error(location); |
1472 | } | |
e440a328 | 1473 | } |
1474 | switch (real->classification()) | |
1475 | { | |
1476 | case Named_object::NAMED_OBJECT_CONST: | |
1477 | return Expression::make_const_reference(real, location); | |
1478 | case Named_object::NAMED_OBJECT_TYPE: | |
1479 | return Expression::make_type(real->type_value(), location); | |
1480 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1481 | if (this->is_composite_literal_key_) | |
1482 | return this; | |
acf8e158 | 1483 | if (!this->no_error_message_) |
631d5788 | 1484 | go_error_at(location, "reference to undefined type %qs", |
1485 | real->message_name().c_str()); | |
e440a328 | 1486 | return Expression::make_error(location); |
1487 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1488 | real->var_value()->set_is_used(); |
e440a328 | 1489 | return Expression::make_var_reference(real, location); |
1490 | case Named_object::NAMED_OBJECT_FUNC: | |
1491 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1492 | return Expression::make_func_reference(real, NULL, location); | |
1493 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1494 | if (this->is_composite_literal_key_) | |
1495 | return this; | |
acf8e158 | 1496 | if (!this->no_error_message_) |
631d5788 | 1497 | go_error_at(location, "unexpected reference to package"); |
e440a328 | 1498 | return Expression::make_error(location); |
1499 | default: | |
c3e6f413 | 1500 | go_unreachable(); |
e440a328 | 1501 | } |
1502 | } | |
1503 | ||
d751bb78 | 1504 | // Dump the ast representation for an unknown expression to a dump context. |
1505 | ||
1506 | void | |
1507 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1508 | { | |
1509 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1510 | << ")"; | |
d751bb78 | 1511 | } |
1512 | ||
e440a328 | 1513 | // Make a reference to an unknown name. |
1514 | ||
acf8e158 | 1515 | Unknown_expression* |
b13c66cd | 1516 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1517 | { |
e440a328 | 1518 | return new Unknown_expression(no, location); |
1519 | } | |
1520 | ||
1521 | // A boolean expression. | |
1522 | ||
1523 | class Boolean_expression : public Expression | |
1524 | { | |
1525 | public: | |
b13c66cd | 1526 | Boolean_expression(bool val, Location location) |
e440a328 | 1527 | : Expression(EXPRESSION_BOOLEAN, location), |
1528 | val_(val), type_(NULL) | |
1529 | { } | |
1530 | ||
1531 | static Expression* | |
1532 | do_import(Import*); | |
1533 | ||
1534 | protected: | |
1535 | bool | |
1536 | do_is_constant() const | |
1537 | { return true; } | |
1538 | ||
0e168074 | 1539 | bool |
3ae06f68 | 1540 | do_is_static_initializer() const |
0e168074 | 1541 | { return true; } |
1542 | ||
e440a328 | 1543 | Type* |
1544 | do_type(); | |
1545 | ||
1546 | void | |
1547 | do_determine_type(const Type_context*); | |
1548 | ||
1549 | Expression* | |
1550 | do_copy() | |
1551 | { return this; } | |
1552 | ||
ea664253 | 1553 | Bexpression* |
1554 | do_get_backend(Translate_context* context) | |
1555 | { return context->backend()->boolean_constant_expression(this->val_); } | |
e440a328 | 1556 | |
1557 | void | |
1558 | do_export(Export* exp) const | |
1559 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1560 | ||
d751bb78 | 1561 | void |
1562 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1563 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1564 | ||
e440a328 | 1565 | private: |
1566 | // The constant. | |
1567 | bool val_; | |
1568 | // The type as determined by context. | |
1569 | Type* type_; | |
1570 | }; | |
1571 | ||
1572 | // Get the type. | |
1573 | ||
1574 | Type* | |
1575 | Boolean_expression::do_type() | |
1576 | { | |
1577 | if (this->type_ == NULL) | |
1578 | this->type_ = Type::make_boolean_type(); | |
1579 | return this->type_; | |
1580 | } | |
1581 | ||
1582 | // Set the type from the context. | |
1583 | ||
1584 | void | |
1585 | Boolean_expression::do_determine_type(const Type_context* context) | |
1586 | { | |
1587 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1588 | ; | |
1589 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1590 | this->type_ = context->type; | |
1591 | else if (!context->may_be_abstract) | |
1592 | this->type_ = Type::lookup_bool_type(); | |
1593 | } | |
1594 | ||
1595 | // Import a boolean constant. | |
1596 | ||
1597 | Expression* | |
1598 | Boolean_expression::do_import(Import* imp) | |
1599 | { | |
1600 | if (imp->peek_char() == 't') | |
1601 | { | |
1602 | imp->require_c_string("true"); | |
1603 | return Expression::make_boolean(true, imp->location()); | |
1604 | } | |
1605 | else | |
1606 | { | |
1607 | imp->require_c_string("false"); | |
1608 | return Expression::make_boolean(false, imp->location()); | |
1609 | } | |
1610 | } | |
1611 | ||
1612 | // Make a boolean expression. | |
1613 | ||
1614 | Expression* | |
b13c66cd | 1615 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1616 | { |
1617 | return new Boolean_expression(val, location); | |
1618 | } | |
1619 | ||
1620 | // Class String_expression. | |
1621 | ||
1622 | // Get the type. | |
1623 | ||
1624 | Type* | |
1625 | String_expression::do_type() | |
1626 | { | |
1627 | if (this->type_ == NULL) | |
1628 | this->type_ = Type::make_string_type(); | |
1629 | return this->type_; | |
1630 | } | |
1631 | ||
1632 | // Set the type from the context. | |
1633 | ||
1634 | void | |
1635 | String_expression::do_determine_type(const Type_context* context) | |
1636 | { | |
1637 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1638 | ; | |
1639 | else if (context->type != NULL && context->type->is_string_type()) | |
1640 | this->type_ = context->type; | |
1641 | else if (!context->may_be_abstract) | |
1642 | this->type_ = Type::lookup_string_type(); | |
1643 | } | |
1644 | ||
1645 | // Build a string constant. | |
1646 | ||
ea664253 | 1647 | Bexpression* |
1648 | String_expression::do_get_backend(Translate_context* context) | |
e440a328 | 1649 | { |
2c809f8f | 1650 | Gogo* gogo = context->gogo(); |
1651 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1652 | ||
1653 | Location loc = this->location(); | |
1654 | std::vector<Bexpression*> init(2); | |
1655 | Bexpression* str_cst = | |
1656 | gogo->backend()->string_constant_expression(this->val_); | |
1657 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1658 | ||
1659 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1660 | mpz_t lenval; | |
1661 | mpz_init_set_ui(lenval, this->val_.length()); | |
1662 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1663 | mpz_clear(lenval); | |
1664 | ||
ea664253 | 1665 | return gogo->backend()->constructor_expression(btype, init, loc); |
e440a328 | 1666 | } |
1667 | ||
8b1c301d | 1668 | // Write string literal to string dump. |
e440a328 | 1669 | |
1670 | void | |
8b1c301d | 1671 | String_expression::export_string(String_dump* exp, |
1672 | const String_expression* str) | |
e440a328 | 1673 | { |
1674 | std::string s; | |
8b1c301d | 1675 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1676 | s += '"'; |
8b1c301d | 1677 | for (std::string::const_iterator p = str->val_.begin(); |
1678 | p != str->val_.end(); | |
e440a328 | 1679 | ++p) |
1680 | { | |
1681 | if (*p == '\\' || *p == '"') | |
1682 | { | |
1683 | s += '\\'; | |
1684 | s += *p; | |
1685 | } | |
1686 | else if (*p >= 0x20 && *p < 0x7f) | |
1687 | s += *p; | |
1688 | else if (*p == '\n') | |
1689 | s += "\\n"; | |
1690 | else if (*p == '\t') | |
1691 | s += "\\t"; | |
1692 | else | |
1693 | { | |
1694 | s += "\\x"; | |
1695 | unsigned char c = *p; | |
1696 | unsigned int dig = c >> 4; | |
1697 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1698 | dig = c & 0xf; | |
1699 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1700 | } | |
1701 | } | |
1702 | s += '"'; | |
1703 | exp->write_string(s); | |
1704 | } | |
1705 | ||
8b1c301d | 1706 | // Export a string expression. |
1707 | ||
1708 | void | |
1709 | String_expression::do_export(Export* exp) const | |
1710 | { | |
1711 | String_expression::export_string(exp, this); | |
1712 | } | |
1713 | ||
e440a328 | 1714 | // Import a string expression. |
1715 | ||
1716 | Expression* | |
1717 | String_expression::do_import(Import* imp) | |
1718 | { | |
1719 | imp->require_c_string("\""); | |
1720 | std::string val; | |
1721 | while (true) | |
1722 | { | |
1723 | int c = imp->get_char(); | |
1724 | if (c == '"' || c == -1) | |
1725 | break; | |
1726 | if (c != '\\') | |
1727 | val += static_cast<char>(c); | |
1728 | else | |
1729 | { | |
1730 | c = imp->get_char(); | |
1731 | if (c == '\\' || c == '"') | |
1732 | val += static_cast<char>(c); | |
1733 | else if (c == 'n') | |
1734 | val += '\n'; | |
1735 | else if (c == 't') | |
1736 | val += '\t'; | |
1737 | else if (c == 'x') | |
1738 | { | |
1739 | c = imp->get_char(); | |
1740 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1741 | c = imp->get_char(); | |
1742 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1743 | char v = (vh << 4) | vl; | |
1744 | val += v; | |
1745 | } | |
1746 | else | |
1747 | { | |
631d5788 | 1748 | go_error_at(imp->location(), "bad string constant"); |
e440a328 | 1749 | return Expression::make_error(imp->location()); |
1750 | } | |
1751 | } | |
1752 | } | |
1753 | return Expression::make_string(val, imp->location()); | |
1754 | } | |
1755 | ||
d751bb78 | 1756 | // Ast dump for string expression. |
1757 | ||
1758 | void | |
1759 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1760 | { | |
8b1c301d | 1761 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1762 | } |
1763 | ||
e440a328 | 1764 | // Make a string expression. |
1765 | ||
1766 | Expression* | |
b13c66cd | 1767 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1768 | { |
1769 | return new String_expression(val, location); | |
1770 | } | |
1771 | ||
2c809f8f | 1772 | // An expression that evaluates to some characteristic of a string. |
1773 | // This is used when indexing, bound-checking, or nil checking a string. | |
1774 | ||
1775 | class String_info_expression : public Expression | |
1776 | { | |
1777 | public: | |
1778 | String_info_expression(Expression* string, String_info string_info, | |
1779 | Location location) | |
1780 | : Expression(EXPRESSION_STRING_INFO, location), | |
1781 | string_(string), string_info_(string_info) | |
1782 | { } | |
1783 | ||
1784 | protected: | |
1785 | Type* | |
1786 | do_type(); | |
1787 | ||
1788 | void | |
1789 | do_determine_type(const Type_context*) | |
1790 | { go_unreachable(); } | |
1791 | ||
1792 | Expression* | |
1793 | do_copy() | |
1794 | { | |
1795 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1796 | this->location()); | |
1797 | } | |
1798 | ||
ea664253 | 1799 | Bexpression* |
1800 | do_get_backend(Translate_context* context); | |
2c809f8f | 1801 | |
1802 | void | |
1803 | do_dump_expression(Ast_dump_context*) const; | |
1804 | ||
1805 | void | |
1806 | do_issue_nil_check() | |
1807 | { this->string_->issue_nil_check(); } | |
1808 | ||
1809 | private: | |
1810 | // The string for which we are getting information. | |
1811 | Expression* string_; | |
1812 | // What information we want. | |
1813 | String_info string_info_; | |
1814 | }; | |
1815 | ||
1816 | // Return the type of the string info. | |
1817 | ||
1818 | Type* | |
1819 | String_info_expression::do_type() | |
1820 | { | |
1821 | switch (this->string_info_) | |
1822 | { | |
1823 | case STRING_INFO_DATA: | |
1824 | { | |
1825 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1826 | return Type::make_pointer_type(byte_type); | |
1827 | } | |
1828 | case STRING_INFO_LENGTH: | |
1829 | return Type::lookup_integer_type("int"); | |
1830 | default: | |
1831 | go_unreachable(); | |
1832 | } | |
1833 | } | |
1834 | ||
1835 | // Return string information in GENERIC. | |
1836 | ||
ea664253 | 1837 | Bexpression* |
1838 | String_info_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 1839 | { |
1840 | Gogo* gogo = context->gogo(); | |
1841 | ||
ea664253 | 1842 | Bexpression* bstring = this->string_->get_backend(context); |
2c809f8f | 1843 | switch (this->string_info_) |
1844 | { | |
1845 | case STRING_INFO_DATA: | |
1846 | case STRING_INFO_LENGTH: | |
ea664253 | 1847 | return gogo->backend()->struct_field_expression(bstring, |
1848 | this->string_info_, | |
1849 | this->location()); | |
2c809f8f | 1850 | break; |
1851 | default: | |
1852 | go_unreachable(); | |
1853 | } | |
2c809f8f | 1854 | } |
1855 | ||
1856 | // Dump ast representation for a type info expression. | |
1857 | ||
1858 | void | |
1859 | String_info_expression::do_dump_expression( | |
1860 | Ast_dump_context* ast_dump_context) const | |
1861 | { | |
1862 | ast_dump_context->ostream() << "stringinfo("; | |
1863 | this->string_->dump_expression(ast_dump_context); | |
1864 | ast_dump_context->ostream() << ","; | |
1865 | ast_dump_context->ostream() << | |
1866 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1867 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1868 | : "unknown"); | |
1869 | ast_dump_context->ostream() << ")"; | |
1870 | } | |
1871 | ||
1872 | // Make a string info expression. | |
1873 | ||
1874 | Expression* | |
1875 | Expression::make_string_info(Expression* string, String_info string_info, | |
1876 | Location location) | |
1877 | { | |
1878 | return new String_info_expression(string, string_info, location); | |
1879 | } | |
1880 | ||
e440a328 | 1881 | // Make an integer expression. |
1882 | ||
1883 | class Integer_expression : public Expression | |
1884 | { | |
1885 | public: | |
5d4b8566 | 1886 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1887 | Location location) | |
e440a328 | 1888 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1889 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1890 | { mpz_init_set(this->val_, *val); } |
1891 | ||
1892 | static Expression* | |
1893 | do_import(Import*); | |
1894 | ||
8b1c301d | 1895 | // Write VAL to string dump. |
e440a328 | 1896 | static void |
8b1c301d | 1897 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1898 | |
d751bb78 | 1899 | // Write VAL to dump context. |
1900 | static void | |
1901 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1902 | ||
e440a328 | 1903 | protected: |
1904 | bool | |
1905 | do_is_constant() const | |
1906 | { return true; } | |
1907 | ||
0e168074 | 1908 | bool |
3ae06f68 | 1909 | do_is_static_initializer() const |
0e168074 | 1910 | { return true; } |
1911 | ||
e440a328 | 1912 | bool |
0c77715b | 1913 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1914 | |
1915 | Type* | |
1916 | do_type(); | |
1917 | ||
1918 | void | |
1919 | do_determine_type(const Type_context* context); | |
1920 | ||
1921 | void | |
1922 | do_check_types(Gogo*); | |
1923 | ||
ea664253 | 1924 | Bexpression* |
1925 | do_get_backend(Translate_context*); | |
e440a328 | 1926 | |
1927 | Expression* | |
1928 | do_copy() | |
5d4b8566 | 1929 | { |
1930 | if (this->is_character_constant_) | |
1931 | return Expression::make_character(&this->val_, this->type_, | |
1932 | this->location()); | |
1933 | else | |
e67508fa | 1934 | return Expression::make_integer_z(&this->val_, this->type_, |
1935 | this->location()); | |
5d4b8566 | 1936 | } |
e440a328 | 1937 | |
1938 | void | |
1939 | do_export(Export*) const; | |
1940 | ||
d751bb78 | 1941 | void |
1942 | do_dump_expression(Ast_dump_context*) const; | |
1943 | ||
e440a328 | 1944 | private: |
1945 | // The integer value. | |
1946 | mpz_t val_; | |
1947 | // The type so far. | |
1948 | Type* type_; | |
5d4b8566 | 1949 | // Whether this is a character constant. |
1950 | bool is_character_constant_; | |
e440a328 | 1951 | }; |
1952 | ||
0c77715b | 1953 | // Return a numeric constant for this expression. We have to mark |
1954 | // this as a character when appropriate. | |
e440a328 | 1955 | |
1956 | bool | |
0c77715b | 1957 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1958 | { |
0c77715b | 1959 | if (this->is_character_constant_) |
1960 | nc->set_rune(this->type_, this->val_); | |
1961 | else | |
1962 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1963 | return true; |
1964 | } | |
1965 | ||
1966 | // Return the current type. If we haven't set the type yet, we return | |
1967 | // an abstract integer type. | |
1968 | ||
1969 | Type* | |
1970 | Integer_expression::do_type() | |
1971 | { | |
1972 | if (this->type_ == NULL) | |
5d4b8566 | 1973 | { |
1974 | if (this->is_character_constant_) | |
1975 | this->type_ = Type::make_abstract_character_type(); | |
1976 | else | |
1977 | this->type_ = Type::make_abstract_integer_type(); | |
1978 | } | |
e440a328 | 1979 | return this->type_; |
1980 | } | |
1981 | ||
1982 | // Set the type of the integer value. Here we may switch from an | |
1983 | // abstract type to a real type. | |
1984 | ||
1985 | void | |
1986 | Integer_expression::do_determine_type(const Type_context* context) | |
1987 | { | |
1988 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1989 | ; | |
0c77715b | 1990 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1991 | this->type_ = context->type; |
1992 | else if (!context->may_be_abstract) | |
5d4b8566 | 1993 | { |
1994 | if (this->is_character_constant_) | |
1995 | this->type_ = Type::lookup_integer_type("int32"); | |
1996 | else | |
1997 | this->type_ = Type::lookup_integer_type("int"); | |
1998 | } | |
e440a328 | 1999 | } |
2000 | ||
e440a328 | 2001 | // Check the type of an integer constant. |
2002 | ||
2003 | void | |
2004 | Integer_expression::do_check_types(Gogo*) | |
2005 | { | |
0c77715b | 2006 | Type* type = this->type_; |
2007 | if (type == NULL) | |
e440a328 | 2008 | return; |
0c77715b | 2009 | Numeric_constant nc; |
2010 | if (this->is_character_constant_) | |
2011 | nc.set_rune(NULL, this->val_); | |
2012 | else | |
2013 | nc.set_int(NULL, this->val_); | |
2014 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 2015 | this->set_is_error(); |
2016 | } | |
2017 | ||
ea664253 | 2018 | // Get the backend representation for an integer constant. |
e440a328 | 2019 | |
ea664253 | 2020 | Bexpression* |
2021 | Integer_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2022 | { |
12373dd5 | 2023 | if (this->is_error_expression() |
2024 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2025 | { | |
2026 | go_assert(saw_errors()); | |
2027 | return context->gogo()->backend()->error_expression(); | |
2028 | } | |
2029 | ||
48c2a53a | 2030 | Type* resolved_type = NULL; |
e440a328 | 2031 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2032 | resolved_type = this->type_; |
e440a328 | 2033 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
2034 | { | |
2035 | // We are converting to an abstract floating point type. | |
48c2a53a | 2036 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2037 | } |
2038 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2039 | { | |
2040 | // We are converting to an abstract complex type. | |
48c2a53a | 2041 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2042 | } |
2043 | else | |
2044 | { | |
2045 | // If we still have an abstract type here, then this is being | |
2046 | // used in a constant expression which didn't get reduced for | |
2047 | // some reason. Use a type which will fit the value. We use <, | |
2048 | // not <=, because we need an extra bit for the sign bit. | |
2049 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 2050 | Type* int_type = Type::lookup_integer_type("int"); |
2051 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 2052 | resolved_type = int_type; |
e440a328 | 2053 | else if (bits < 64) |
48c2a53a | 2054 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 2055 | else |
48c2a53a | 2056 | { |
2057 | if (!saw_errors()) | |
631d5788 | 2058 | go_error_at(this->location(), |
2059 | "unknown type for large integer constant"); | |
ea664253 | 2060 | return context->gogo()->backend()->error_expression(); |
48c2a53a | 2061 | } |
e440a328 | 2062 | } |
48c2a53a | 2063 | Numeric_constant nc; |
2064 | nc.set_int(resolved_type, this->val_); | |
ea664253 | 2065 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2066 | } |
2067 | ||
2068 | // Write VAL to export data. | |
2069 | ||
2070 | void | |
8b1c301d | 2071 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 2072 | { |
2073 | char* s = mpz_get_str(NULL, 10, val); | |
2074 | exp->write_c_string(s); | |
2075 | free(s); | |
2076 | } | |
2077 | ||
2078 | // Export an integer in a constant expression. | |
2079 | ||
2080 | void | |
2081 | Integer_expression::do_export(Export* exp) const | |
2082 | { | |
2083 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 2084 | if (this->is_character_constant_) |
2085 | exp->write_c_string("'"); | |
e440a328 | 2086 | // A trailing space lets us reliably identify the end of the number. |
2087 | exp->write_c_string(" "); | |
2088 | } | |
2089 | ||
2090 | // Import an integer, floating point, or complex value. This handles | |
2091 | // all these types because they all start with digits. | |
2092 | ||
2093 | Expression* | |
2094 | Integer_expression::do_import(Import* imp) | |
2095 | { | |
2096 | std::string num = imp->read_identifier(); | |
2097 | imp->require_c_string(" "); | |
2098 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2099 | { | |
2100 | mpfr_t real; | |
2101 | size_t plus_pos = num.find('+', 1); | |
2102 | size_t minus_pos = num.find('-', 1); | |
2103 | size_t pos; | |
2104 | if (plus_pos == std::string::npos) | |
2105 | pos = minus_pos; | |
2106 | else if (minus_pos == std::string::npos) | |
2107 | pos = plus_pos; | |
2108 | else | |
2109 | { | |
631d5788 | 2110 | go_error_at(imp->location(), "bad number in import data: %qs", |
2111 | num.c_str()); | |
e440a328 | 2112 | return Expression::make_error(imp->location()); |
2113 | } | |
2114 | if (pos == std::string::npos) | |
2115 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2116 | else | |
2117 | { | |
2118 | std::string real_str = num.substr(0, pos); | |
2119 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2120 | { | |
631d5788 | 2121 | go_error_at(imp->location(), "bad number in import data: %qs", |
2122 | real_str.c_str()); | |
e440a328 | 2123 | return Expression::make_error(imp->location()); |
2124 | } | |
2125 | } | |
2126 | ||
2127 | std::string imag_str; | |
2128 | if (pos == std::string::npos) | |
2129 | imag_str = num; | |
2130 | else | |
2131 | imag_str = num.substr(pos); | |
2132 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2133 | mpfr_t imag; | |
2134 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2135 | { | |
631d5788 | 2136 | go_error_at(imp->location(), "bad number in import data: %qs", |
2137 | imag_str.c_str()); | |
e440a328 | 2138 | return Expression::make_error(imp->location()); |
2139 | } | |
fcbea5e4 | 2140 | mpc_t cval; |
2141 | mpc_init2(cval, mpc_precision); | |
2142 | mpc_set_fr_fr(cval, real, imag, MPC_RNDNN); | |
e440a328 | 2143 | mpfr_clear(real); |
2144 | mpfr_clear(imag); | |
fcbea5e4 | 2145 | Expression* ret = Expression::make_complex(&cval, NULL, imp->location()); |
2146 | mpc_clear(cval); | |
e440a328 | 2147 | return ret; |
2148 | } | |
2149 | else if (num.find('.') == std::string::npos | |
2150 | && num.find('E') == std::string::npos) | |
2151 | { | |
5d4b8566 | 2152 | bool is_character_constant = (!num.empty() |
2153 | && num[num.length() - 1] == '\''); | |
2154 | if (is_character_constant) | |
2155 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2156 | mpz_t val; |
2157 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2158 | { | |
631d5788 | 2159 | go_error_at(imp->location(), "bad number in import data: %qs", |
2160 | num.c_str()); | |
e440a328 | 2161 | return Expression::make_error(imp->location()); |
2162 | } | |
5d4b8566 | 2163 | Expression* ret; |
2164 | if (is_character_constant) | |
2165 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2166 | else | |
e67508fa | 2167 | ret = Expression::make_integer_z(&val, NULL, imp->location()); |
e440a328 | 2168 | mpz_clear(val); |
2169 | return ret; | |
2170 | } | |
2171 | else | |
2172 | { | |
2173 | mpfr_t val; | |
2174 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2175 | { | |
631d5788 | 2176 | go_error_at(imp->location(), "bad number in import data: %qs", |
2177 | num.c_str()); | |
e440a328 | 2178 | return Expression::make_error(imp->location()); |
2179 | } | |
2180 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2181 | mpfr_clear(val); | |
2182 | return ret; | |
2183 | } | |
2184 | } | |
d751bb78 | 2185 | // Ast dump for integer expression. |
2186 | ||
2187 | void | |
2188 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2189 | { | |
5d4b8566 | 2190 | if (this->is_character_constant_) |
2191 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2192 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2193 | if (this->is_character_constant_) |
2194 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2195 | } |
2196 | ||
e67508fa | 2197 | // Build a new integer value from a multi-precision integer. |
e440a328 | 2198 | |
2199 | Expression* | |
e67508fa | 2200 | Expression::make_integer_z(const mpz_t* val, Type* type, Location location) |
5d4b8566 | 2201 | { |
2202 | return new Integer_expression(val, type, false, location); | |
2203 | } | |
2204 | ||
e67508fa | 2205 | // Build a new integer value from an unsigned long. |
2206 | ||
2207 | Expression* | |
2208 | Expression::make_integer_ul(unsigned long val, Type *type, Location location) | |
2209 | { | |
2210 | mpz_t zval; | |
2211 | mpz_init_set_ui(zval, val); | |
2212 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2213 | mpz_clear(zval); | |
2214 | return ret; | |
2215 | } | |
2216 | ||
2217 | // Build a new integer value from a signed long. | |
2218 | ||
2219 | Expression* | |
2220 | Expression::make_integer_sl(long val, Type *type, Location location) | |
2221 | { | |
2222 | mpz_t zval; | |
2223 | mpz_init_set_si(zval, val); | |
2224 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2225 | mpz_clear(zval); | |
2226 | return ret; | |
2227 | } | |
2228 | ||
3f378015 | 2229 | // Store an int64_t in an uninitialized mpz_t. |
2230 | ||
2231 | static void | |
2232 | set_mpz_from_int64(mpz_t* zval, int64_t val) | |
2233 | { | |
2234 | if (val >= 0) | |
2235 | { | |
2236 | unsigned long ul = static_cast<unsigned long>(val); | |
2237 | if (static_cast<int64_t>(ul) == val) | |
2238 | { | |
2239 | mpz_init_set_ui(*zval, ul); | |
2240 | return; | |
2241 | } | |
2242 | } | |
2243 | uint64_t uv; | |
2244 | if (val >= 0) | |
2245 | uv = static_cast<uint64_t>(val); | |
2246 | else | |
2247 | uv = static_cast<uint64_t>(- val); | |
2248 | unsigned long ul = uv & 0xffffffffUL; | |
2249 | mpz_init_set_ui(*zval, ul); | |
2250 | mpz_t hval; | |
2251 | mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32)); | |
2252 | mpz_mul_2exp(hval, hval, 32); | |
2253 | mpz_add(*zval, *zval, hval); | |
2254 | mpz_clear(hval); | |
2255 | if (val < 0) | |
2256 | mpz_neg(*zval, *zval); | |
2257 | } | |
2258 | ||
2259 | // Build a new integer value from an int64_t. | |
2260 | ||
2261 | Expression* | |
2262 | Expression::make_integer_int64(int64_t val, Type* type, Location location) | |
2263 | { | |
2264 | mpz_t zval; | |
2265 | set_mpz_from_int64(&zval, val); | |
2266 | Expression* ret = Expression::make_integer_z(&zval, type, location); | |
2267 | mpz_clear(zval); | |
2268 | return ret; | |
2269 | } | |
2270 | ||
5d4b8566 | 2271 | // Build a new character constant value. |
2272 | ||
2273 | Expression* | |
2274 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2275 | { |
5d4b8566 | 2276 | return new Integer_expression(val, type, true, location); |
e440a328 | 2277 | } |
2278 | ||
2279 | // Floats. | |
2280 | ||
2281 | class Float_expression : public Expression | |
2282 | { | |
2283 | public: | |
b13c66cd | 2284 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2285 | : Expression(EXPRESSION_FLOAT, location), |
2286 | type_(type) | |
2287 | { | |
2288 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2289 | } | |
2290 | ||
e440a328 | 2291 | // Write VAL to export data. |
2292 | static void | |
8b1c301d | 2293 | export_float(String_dump* exp, const mpfr_t val); |
2294 | ||
d751bb78 | 2295 | // Write VAL to dump file. |
2296 | static void | |
2297 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2298 | |
2299 | protected: | |
2300 | bool | |
2301 | do_is_constant() const | |
2302 | { return true; } | |
2303 | ||
0e168074 | 2304 | bool |
3ae06f68 | 2305 | do_is_static_initializer() const |
0e168074 | 2306 | { return true; } |
2307 | ||
e440a328 | 2308 | bool |
0c77715b | 2309 | do_numeric_constant_value(Numeric_constant* nc) const |
2310 | { | |
2311 | nc->set_float(this->type_, this->val_); | |
2312 | return true; | |
2313 | } | |
e440a328 | 2314 | |
2315 | Type* | |
2316 | do_type(); | |
2317 | ||
2318 | void | |
2319 | do_determine_type(const Type_context*); | |
2320 | ||
2321 | void | |
2322 | do_check_types(Gogo*); | |
2323 | ||
2324 | Expression* | |
2325 | do_copy() | |
2326 | { return Expression::make_float(&this->val_, this->type_, | |
2327 | this->location()); } | |
2328 | ||
ea664253 | 2329 | Bexpression* |
2330 | do_get_backend(Translate_context*); | |
e440a328 | 2331 | |
2332 | void | |
2333 | do_export(Export*) const; | |
2334 | ||
d751bb78 | 2335 | void |
2336 | do_dump_expression(Ast_dump_context*) const; | |
2337 | ||
e440a328 | 2338 | private: |
2339 | // The floating point value. | |
2340 | mpfr_t val_; | |
2341 | // The type so far. | |
2342 | Type* type_; | |
2343 | }; | |
2344 | ||
e440a328 | 2345 | // Return the current type. If we haven't set the type yet, we return |
2346 | // an abstract float type. | |
2347 | ||
2348 | Type* | |
2349 | Float_expression::do_type() | |
2350 | { | |
2351 | if (this->type_ == NULL) | |
2352 | this->type_ = Type::make_abstract_float_type(); | |
2353 | return this->type_; | |
2354 | } | |
2355 | ||
2356 | // Set the type of the float value. Here we may switch from an | |
2357 | // abstract type to a real type. | |
2358 | ||
2359 | void | |
2360 | Float_expression::do_determine_type(const Type_context* context) | |
2361 | { | |
2362 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2363 | ; | |
2364 | else if (context->type != NULL | |
2365 | && (context->type->integer_type() != NULL | |
2366 | || context->type->float_type() != NULL | |
2367 | || context->type->complex_type() != NULL)) | |
2368 | this->type_ = context->type; | |
2369 | else if (!context->may_be_abstract) | |
48080209 | 2370 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2371 | } |
2372 | ||
e440a328 | 2373 | // Check the type of a float value. |
2374 | ||
2375 | void | |
2376 | Float_expression::do_check_types(Gogo*) | |
2377 | { | |
0c77715b | 2378 | Type* type = this->type_; |
2379 | if (type == NULL) | |
e440a328 | 2380 | return; |
0c77715b | 2381 | Numeric_constant nc; |
2382 | nc.set_float(NULL, this->val_); | |
2383 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2384 | this->set_is_error(); |
e440a328 | 2385 | } |
2386 | ||
ea664253 | 2387 | // Get the backend representation for a float constant. |
e440a328 | 2388 | |
ea664253 | 2389 | Bexpression* |
2390 | Float_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2391 | { |
12373dd5 | 2392 | if (this->is_error_expression() |
2393 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2394 | { | |
2395 | go_assert(saw_errors()); | |
2396 | return context->gogo()->backend()->error_expression(); | |
2397 | } | |
2398 | ||
48c2a53a | 2399 | Type* resolved_type; |
e440a328 | 2400 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2401 | resolved_type = this->type_; |
e440a328 | 2402 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2403 | { | |
2404 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2405 | resolved_type = Type::lookup_integer_type("int"); |
2406 | } | |
2407 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2408 | { | |
2409 | // We are converting to an abstract complex type. | |
2410 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2411 | } |
2412 | else | |
2413 | { | |
2414 | // If we still have an abstract type here, then this is being | |
2415 | // used in a constant expression which didn't get reduced. We | |
2416 | // just use float64 and hope for the best. | |
48c2a53a | 2417 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2418 | } |
48c2a53a | 2419 | |
2420 | Numeric_constant nc; | |
2421 | nc.set_float(resolved_type, this->val_); | |
ea664253 | 2422 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2423 | } |
2424 | ||
8b1c301d | 2425 | // Write a floating point number to a string dump. |
e440a328 | 2426 | |
2427 | void | |
8b1c301d | 2428 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2429 | { |
2430 | mp_exp_t exponent; | |
2431 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2432 | if (*s == '-') | |
2433 | exp->write_c_string("-"); | |
2434 | exp->write_c_string("0."); | |
2435 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2436 | mpfr_free_str(s); | |
2437 | char buf[30]; | |
2438 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2439 | exp->write_c_string(buf); | |
2440 | } | |
2441 | ||
2442 | // Export a floating point number in a constant expression. | |
2443 | ||
2444 | void | |
2445 | Float_expression::do_export(Export* exp) const | |
2446 | { | |
2447 | Float_expression::export_float(exp, this->val_); | |
2448 | // A trailing space lets us reliably identify the end of the number. | |
2449 | exp->write_c_string(" "); | |
2450 | } | |
2451 | ||
d751bb78 | 2452 | // Dump a floating point number to the dump file. |
2453 | ||
2454 | void | |
2455 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2456 | { | |
8b1c301d | 2457 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2458 | } |
2459 | ||
e440a328 | 2460 | // Make a float expression. |
2461 | ||
2462 | Expression* | |
b13c66cd | 2463 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2464 | { |
2465 | return new Float_expression(val, type, location); | |
2466 | } | |
2467 | ||
2468 | // Complex numbers. | |
2469 | ||
2470 | class Complex_expression : public Expression | |
2471 | { | |
2472 | public: | |
fcbea5e4 | 2473 | Complex_expression(const mpc_t* val, Type* type, Location location) |
e440a328 | 2474 | : Expression(EXPRESSION_COMPLEX, location), |
2475 | type_(type) | |
2476 | { | |
fcbea5e4 | 2477 | mpc_init2(this->val_, mpc_precision); |
2478 | mpc_set(this->val_, *val, MPC_RNDNN); | |
e440a328 | 2479 | } |
2480 | ||
fcbea5e4 | 2481 | // Write VAL to string dump. |
e440a328 | 2482 | static void |
fcbea5e4 | 2483 | export_complex(String_dump* exp, const mpc_t val); |
e440a328 | 2484 | |
d751bb78 | 2485 | // Write REAL/IMAG to dump context. |
2486 | static void | |
fcbea5e4 | 2487 | dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val); |
d751bb78 | 2488 | |
e440a328 | 2489 | protected: |
2490 | bool | |
2491 | do_is_constant() const | |
2492 | { return true; } | |
2493 | ||
0e168074 | 2494 | bool |
3ae06f68 | 2495 | do_is_static_initializer() const |
0e168074 | 2496 | { return true; } |
2497 | ||
e440a328 | 2498 | bool |
0c77715b | 2499 | do_numeric_constant_value(Numeric_constant* nc) const |
2500 | { | |
fcbea5e4 | 2501 | nc->set_complex(this->type_, this->val_); |
0c77715b | 2502 | return true; |
2503 | } | |
e440a328 | 2504 | |
2505 | Type* | |
2506 | do_type(); | |
2507 | ||
2508 | void | |
2509 | do_determine_type(const Type_context*); | |
2510 | ||
2511 | void | |
2512 | do_check_types(Gogo*); | |
2513 | ||
2514 | Expression* | |
2515 | do_copy() | |
2516 | { | |
fcbea5e4 | 2517 | return Expression::make_complex(&this->val_, this->type_, |
e440a328 | 2518 | this->location()); |
2519 | } | |
2520 | ||
ea664253 | 2521 | Bexpression* |
2522 | do_get_backend(Translate_context*); | |
e440a328 | 2523 | |
2524 | void | |
2525 | do_export(Export*) const; | |
2526 | ||
d751bb78 | 2527 | void |
2528 | do_dump_expression(Ast_dump_context*) const; | |
abd26de0 | 2529 | |
e440a328 | 2530 | private: |
fcbea5e4 | 2531 | // The complex value. |
2532 | mpc_t val_; | |
e440a328 | 2533 | // The type if known. |
2534 | Type* type_; | |
2535 | }; | |
2536 | ||
e440a328 | 2537 | // Return the current type. If we haven't set the type yet, we return |
2538 | // an abstract complex type. | |
2539 | ||
2540 | Type* | |
2541 | Complex_expression::do_type() | |
2542 | { | |
2543 | if (this->type_ == NULL) | |
2544 | this->type_ = Type::make_abstract_complex_type(); | |
2545 | return this->type_; | |
2546 | } | |
2547 | ||
2548 | // Set the type of the complex value. Here we may switch from an | |
2549 | // abstract type to a real type. | |
2550 | ||
2551 | void | |
2552 | Complex_expression::do_determine_type(const Type_context* context) | |
2553 | { | |
2554 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2555 | ; | |
abd26de0 | 2556 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 2557 | this->type_ = context->type; |
2558 | else if (!context->may_be_abstract) | |
48080209 | 2559 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2560 | } |
2561 | ||
e440a328 | 2562 | // Check the type of a complex value. |
2563 | ||
2564 | void | |
2565 | Complex_expression::do_check_types(Gogo*) | |
2566 | { | |
0c77715b | 2567 | Type* type = this->type_; |
2568 | if (type == NULL) | |
e440a328 | 2569 | return; |
0c77715b | 2570 | Numeric_constant nc; |
fcbea5e4 | 2571 | nc.set_complex(NULL, this->val_); |
0c77715b | 2572 | if (!nc.set_type(this->type_, true, this->location())) |
e440a328 | 2573 | this->set_is_error(); |
2574 | } | |
2575 | ||
ea664253 | 2576 | // Get the backend representation for a complex constant. |
e440a328 | 2577 | |
ea664253 | 2578 | Bexpression* |
2579 | Complex_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2580 | { |
12373dd5 | 2581 | if (this->is_error_expression() |
2582 | || (this->type_ != NULL && this->type_->is_error_type())) | |
2583 | { | |
2584 | go_assert(saw_errors()); | |
2585 | return context->gogo()->backend()->error_expression(); | |
2586 | } | |
2587 | ||
48c2a53a | 2588 | Type* resolved_type; |
e440a328 | 2589 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2590 | resolved_type = this->type_; |
2591 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2592 | { | |
2593 | // We are converting to an abstract integer type. | |
2594 | resolved_type = Type::lookup_integer_type("int"); | |
2595 | } | |
2596 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2597 | { | |
2598 | // We are converting to an abstract float type. | |
2599 | resolved_type = Type::lookup_float_type("float64"); | |
2600 | } | |
e440a328 | 2601 | else |
2602 | { | |
47ae02b7 | 2603 | // If we still have an abstract type here, this is being |
e440a328 | 2604 | // used in a constant expression which didn't get reduced. We |
2605 | // just use complex128 and hope for the best. | |
48c2a53a | 2606 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2607 | } |
48c2a53a | 2608 | |
2609 | Numeric_constant nc; | |
fcbea5e4 | 2610 | nc.set_complex(resolved_type, this->val_); |
ea664253 | 2611 | return Expression::backend_numeric_constant_expression(context, &nc); |
e440a328 | 2612 | } |
2613 | ||
2614 | // Write REAL/IMAG to export data. | |
2615 | ||
2616 | void | |
fcbea5e4 | 2617 | Complex_expression::export_complex(String_dump* exp, const mpc_t val) |
e440a328 | 2618 | { |
fcbea5e4 | 2619 | if (!mpfr_zero_p(mpc_realref(val))) |
e440a328 | 2620 | { |
fcbea5e4 | 2621 | Float_expression::export_float(exp, mpc_realref(val)); |
d1db782d | 2622 | if (mpfr_sgn(mpc_imagref(val)) >= 0) |
e440a328 | 2623 | exp->write_c_string("+"); |
2624 | } | |
fcbea5e4 | 2625 | Float_expression::export_float(exp, mpc_imagref(val)); |
e440a328 | 2626 | exp->write_c_string("i"); |
2627 | } | |
2628 | ||
2629 | // Export a complex number in a constant expression. | |
2630 | ||
2631 | void | |
2632 | Complex_expression::do_export(Export* exp) const | |
2633 | { | |
fcbea5e4 | 2634 | Complex_expression::export_complex(exp, this->val_); |
e440a328 | 2635 | // A trailing space lets us reliably identify the end of the number. |
2636 | exp->write_c_string(" "); | |
2637 | } | |
2638 | ||
d751bb78 | 2639 | // Dump a complex expression to the dump file. |
2640 | ||
2641 | void | |
2642 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2643 | { | |
fcbea5e4 | 2644 | Complex_expression::export_complex(ast_dump_context, this->val_); |
d751bb78 | 2645 | } |
2646 | ||
e440a328 | 2647 | // Make a complex expression. |
2648 | ||
2649 | Expression* | |
fcbea5e4 | 2650 | Expression::make_complex(const mpc_t* val, Type* type, Location location) |
e440a328 | 2651 | { |
fcbea5e4 | 2652 | return new Complex_expression(val, type, location); |
e440a328 | 2653 | } |
2654 | ||
d5b605df | 2655 | // Find a named object in an expression. |
2656 | ||
2657 | class Find_named_object : public Traverse | |
2658 | { | |
2659 | public: | |
2660 | Find_named_object(Named_object* no) | |
2661 | : Traverse(traverse_expressions), | |
2662 | no_(no), found_(false) | |
2663 | { } | |
2664 | ||
2665 | // Whether we found the object. | |
2666 | bool | |
2667 | found() const | |
2668 | { return this->found_; } | |
2669 | ||
2670 | protected: | |
2671 | int | |
2672 | expression(Expression**); | |
2673 | ||
2674 | private: | |
2675 | // The object we are looking for. | |
2676 | Named_object* no_; | |
2677 | // Whether we found it. | |
2678 | bool found_; | |
2679 | }; | |
2680 | ||
e440a328 | 2681 | // A reference to a const in an expression. |
2682 | ||
2683 | class Const_expression : public Expression | |
2684 | { | |
2685 | public: | |
b13c66cd | 2686 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2687 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2688 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2689 | { } |
2690 | ||
d5b605df | 2691 | Named_object* |
2692 | named_object() | |
2693 | { return this->constant_; } | |
2694 | ||
a7f064d5 | 2695 | // Check that the initializer does not refer to the constant itself. |
2696 | void | |
2697 | check_for_init_loop(); | |
2698 | ||
e440a328 | 2699 | protected: |
ba4aedd4 | 2700 | int |
2701 | do_traverse(Traverse*); | |
2702 | ||
e440a328 | 2703 | Expression* |
ceeb4318 | 2704 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2705 | |
2706 | bool | |
2707 | do_is_constant() const | |
2708 | { return true; } | |
2709 | ||
0e168074 | 2710 | bool |
3ae06f68 | 2711 | do_is_static_initializer() const |
0e168074 | 2712 | { return true; } |
2713 | ||
e440a328 | 2714 | bool |
0c77715b | 2715 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2716 | |
2717 | bool | |
af6b489a | 2718 | do_string_constant_value(std::string* val) const; |
e440a328 | 2719 | |
2720 | Type* | |
2721 | do_type(); | |
2722 | ||
2723 | // The type of a const is set by the declaration, not the use. | |
2724 | void | |
2725 | do_determine_type(const Type_context*); | |
2726 | ||
2727 | void | |
2728 | do_check_types(Gogo*); | |
2729 | ||
2730 | Expression* | |
2731 | do_copy() | |
2732 | { return this; } | |
2733 | ||
ea664253 | 2734 | Bexpression* |
2735 | do_get_backend(Translate_context* context); | |
e440a328 | 2736 | |
2737 | // When exporting a reference to a const as part of a const | |
2738 | // expression, we export the value. We ignore the fact that it has | |
2739 | // a name. | |
2740 | void | |
2741 | do_export(Export* exp) const | |
2742 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2743 | ||
d751bb78 | 2744 | void |
2745 | do_dump_expression(Ast_dump_context*) const; | |
2746 | ||
e440a328 | 2747 | private: |
2748 | // The constant. | |
2749 | Named_object* constant_; | |
2750 | // The type of this reference. This is used if the constant has an | |
2751 | // abstract type. | |
2752 | Type* type_; | |
13e818f5 | 2753 | // Used to prevent infinite recursion when a constant incorrectly |
2754 | // refers to itself. | |
2755 | mutable bool seen_; | |
e440a328 | 2756 | }; |
2757 | ||
ba4aedd4 | 2758 | // Traversal. |
2759 | ||
2760 | int | |
2761 | Const_expression::do_traverse(Traverse* traverse) | |
2762 | { | |
2763 | if (this->type_ != NULL) | |
2764 | return Type::traverse(this->type_, traverse); | |
2765 | return TRAVERSE_CONTINUE; | |
2766 | } | |
2767 | ||
e440a328 | 2768 | // Lower a constant expression. This is where we convert the |
2769 | // predeclared constant iota into an integer value. | |
2770 | ||
2771 | Expression* | |
ceeb4318 | 2772 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2773 | Statement_inserter*, int iota_value) | |
e440a328 | 2774 | { |
2775 | if (this->constant_->const_value()->expr()->classification() | |
2776 | == EXPRESSION_IOTA) | |
2777 | { | |
2778 | if (iota_value == -1) | |
2779 | { | |
631d5788 | 2780 | go_error_at(this->location(), |
2781 | "iota is only defined in const declarations"); | |
e440a328 | 2782 | iota_value = 0; |
2783 | } | |
e67508fa | 2784 | return Expression::make_integer_ul(iota_value, NULL, this->location()); |
e440a328 | 2785 | } |
2786 | ||
2787 | // Make sure that the constant itself has been lowered. | |
2788 | gogo->lower_constant(this->constant_); | |
2789 | ||
2790 | return this; | |
2791 | } | |
2792 | ||
0c77715b | 2793 | // Return a numeric constant value. |
e440a328 | 2794 | |
2795 | bool | |
0c77715b | 2796 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2797 | { |
13e818f5 | 2798 | if (this->seen_) |
2799 | return false; | |
2800 | ||
e440a328 | 2801 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2802 | |
13e818f5 | 2803 | this->seen_ = true; |
2804 | ||
0c77715b | 2805 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2806 | |
13e818f5 | 2807 | this->seen_ = false; |
2808 | ||
e440a328 | 2809 | Type* ctype; |
2810 | if (this->type_ != NULL) | |
2811 | ctype = this->type_; | |
2812 | else | |
2813 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2814 | if (r && ctype != NULL) |
2815 | { | |
0c77715b | 2816 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2817 | return false; |
e440a328 | 2818 | } |
e440a328 | 2819 | |
e440a328 | 2820 | return r; |
2821 | } | |
2822 | ||
af6b489a | 2823 | bool |
2824 | Const_expression::do_string_constant_value(std::string* val) const | |
2825 | { | |
2826 | if (this->seen_) | |
2827 | return false; | |
2828 | ||
2829 | Expression* e = this->constant_->const_value()->expr(); | |
2830 | ||
2831 | this->seen_ = true; | |
2832 | bool ok = e->string_constant_value(val); | |
2833 | this->seen_ = false; | |
2834 | ||
2835 | return ok; | |
2836 | } | |
2837 | ||
e440a328 | 2838 | // Return the type of the const reference. |
2839 | ||
2840 | Type* | |
2841 | Const_expression::do_type() | |
2842 | { | |
2843 | if (this->type_ != NULL) | |
2844 | return this->type_; | |
13e818f5 | 2845 | |
2f78f012 | 2846 | Named_constant* nc = this->constant_->const_value(); |
2847 | ||
2848 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2849 | { |
4b2ab536 | 2850 | if (nc->type() == NULL || !nc->type()->is_error_type()) |
2851 | { | |
2852 | Location loc = this->location(); | |
2853 | if (!this->seen_) | |
2854 | loc = nc->location(); | |
2855 | go_error_at(loc, "constant refers to itself"); | |
2856 | } | |
2857 | this->set_is_error(); | |
13e818f5 | 2858 | this->type_ = Type::make_error_type(); |
4b2ab536 | 2859 | nc->set_type(this->type_); |
13e818f5 | 2860 | return this->type_; |
2861 | } | |
2862 | ||
2863 | this->seen_ = true; | |
2864 | ||
e440a328 | 2865 | Type* ret = nc->type(); |
13e818f5 | 2866 | |
e440a328 | 2867 | if (ret != NULL) |
13e818f5 | 2868 | { |
2869 | this->seen_ = false; | |
2870 | return ret; | |
2871 | } | |
2872 | ||
e440a328 | 2873 | // During parsing, a named constant may have a NULL type, but we |
2874 | // must not return a NULL type here. | |
13e818f5 | 2875 | ret = nc->expr()->type(); |
2876 | ||
2877 | this->seen_ = false; | |
2878 | ||
4b2ab536 | 2879 | if (ret->is_error_type()) |
2880 | nc->set_type(ret); | |
2881 | ||
13e818f5 | 2882 | return ret; |
e440a328 | 2883 | } |
2884 | ||
2885 | // Set the type of the const reference. | |
2886 | ||
2887 | void | |
2888 | Const_expression::do_determine_type(const Type_context* context) | |
2889 | { | |
2890 | Type* ctype = this->constant_->const_value()->type(); | |
2891 | Type* cetype = (ctype != NULL | |
2892 | ? ctype | |
2893 | : this->constant_->const_value()->expr()->type()); | |
2894 | if (ctype != NULL && !ctype->is_abstract()) | |
2895 | ; | |
2896 | else if (context->type != NULL | |
0c77715b | 2897 | && context->type->is_numeric_type() |
2898 | && cetype->is_numeric_type()) | |
e440a328 | 2899 | this->type_ = context->type; |
2900 | else if (context->type != NULL | |
2901 | && context->type->is_string_type() | |
2902 | && cetype->is_string_type()) | |
2903 | this->type_ = context->type; | |
2904 | else if (context->type != NULL | |
2905 | && context->type->is_boolean_type() | |
2906 | && cetype->is_boolean_type()) | |
2907 | this->type_ = context->type; | |
2908 | else if (!context->may_be_abstract) | |
2909 | { | |
2910 | if (cetype->is_abstract()) | |
2911 | cetype = cetype->make_non_abstract_type(); | |
2912 | this->type_ = cetype; | |
2913 | } | |
2914 | } | |
2915 | ||
a7f064d5 | 2916 | // Check for a loop in which the initializer of a constant refers to |
2917 | // the constant itself. | |
e440a328 | 2918 | |
2919 | void | |
a7f064d5 | 2920 | Const_expression::check_for_init_loop() |
e440a328 | 2921 | { |
5c13bd80 | 2922 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2923 | return; |
2924 | ||
a7f064d5 | 2925 | if (this->seen_) |
2926 | { | |
2927 | this->report_error(_("constant refers to itself")); | |
2928 | this->type_ = Type::make_error_type(); | |
2929 | return; | |
2930 | } | |
2931 | ||
d5b605df | 2932 | Expression* init = this->constant_->const_value()->expr(); |
2933 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2934 | |
2935 | this->seen_ = true; | |
d5b605df | 2936 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2937 | this->seen_ = false; |
2938 | ||
d5b605df | 2939 | if (find_named_object.found()) |
2940 | { | |
5c13bd80 | 2941 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2942 | { |
2943 | this->report_error(_("constant refers to itself")); | |
2944 | this->type_ = Type::make_error_type(); | |
2945 | } | |
d5b605df | 2946 | return; |
2947 | } | |
a7f064d5 | 2948 | } |
2949 | ||
2950 | // Check types of a const reference. | |
2951 | ||
2952 | void | |
2953 | Const_expression::do_check_types(Gogo*) | |
2954 | { | |
5c13bd80 | 2955 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2956 | return; |
2957 | ||
2958 | this->check_for_init_loop(); | |
d5b605df | 2959 | |
0c77715b | 2960 | // Check that numeric constant fits in type. |
2961 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2962 | { |
0c77715b | 2963 | Numeric_constant nc; |
2964 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2965 | { |
0c77715b | 2966 | if (!nc.set_type(this->type_, true, this->location())) |
2967 | this->set_is_error(); | |
e440a328 | 2968 | } |
e440a328 | 2969 | } |
2970 | } | |
2971 | ||
ea664253 | 2972 | // Return the backend representation for a const reference. |
e440a328 | 2973 | |
ea664253 | 2974 | Bexpression* |
2975 | Const_expression::do_get_backend(Translate_context* context) | |
e440a328 | 2976 | { |
12373dd5 | 2977 | if (this->is_error_expression() |
2978 | || (this->type_ != NULL && this->type_->is_error())) | |
2979 | { | |
2980 | go_assert(saw_errors()); | |
2981 | return context->backend()->error_expression(); | |
2982 | } | |
e440a328 | 2983 | |
2984 | // If the type has been set for this expression, but the underlying | |
2985 | // object is an abstract int or float, we try to get the abstract | |
2986 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2987 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2988 | if (this->type_ != NULL |
0c77715b | 2989 | && this->type_->is_numeric_type() |
a68492b4 | 2990 | && (this->constant_->const_value()->type() == NULL |
2991 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2992 | { |
0c77715b | 2993 | Numeric_constant nc; |
2994 | if (expr->numeric_constant_value(&nc) | |
2995 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2996 | { |
0c77715b | 2997 | Expression* e = nc.expression(this->location()); |
ea664253 | 2998 | return e->get_backend(context); |
e440a328 | 2999 | } |
e440a328 | 3000 | } |
3001 | ||
2c809f8f | 3002 | if (this->type_ != NULL) |
f2de4532 | 3003 | expr = Expression::make_cast(this->type_, expr, this->location()); |
ea664253 | 3004 | return expr->get_backend(context); |
e440a328 | 3005 | } |
3006 | ||
d751bb78 | 3007 | // Dump ast representation for constant expression. |
3008 | ||
3009 | void | |
3010 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3011 | { | |
3012 | ast_dump_context->ostream() << this->constant_->name(); | |
3013 | } | |
3014 | ||
e440a328 | 3015 | // Make a reference to a constant in an expression. |
3016 | ||
3017 | Expression* | |
3018 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 3019 | Location location) |
e440a328 | 3020 | { |
3021 | return new Const_expression(constant, location); | |
3022 | } | |
3023 | ||
d5b605df | 3024 | // Find a named object in an expression. |
3025 | ||
3026 | int | |
3027 | Find_named_object::expression(Expression** pexpr) | |
3028 | { | |
3029 | switch ((*pexpr)->classification()) | |
3030 | { | |
3031 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 3032 | { |
3033 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
3034 | if (ce->named_object() == this->no_) | |
3035 | break; | |
3036 | ||
3037 | // We need to check a constant initializer explicitly, as | |
3038 | // loops here will not be caught by the loop checking for | |
3039 | // variable initializers. | |
3040 | ce->check_for_init_loop(); | |
3041 | ||
3042 | return TRAVERSE_CONTINUE; | |
3043 | } | |
3044 | ||
d5b605df | 3045 | case Expression::EXPRESSION_VAR_REFERENCE: |
3046 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
3047 | break; | |
3048 | return TRAVERSE_CONTINUE; | |
3049 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
3050 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
3051 | break; | |
3052 | return TRAVERSE_CONTINUE; | |
3053 | default: | |
3054 | return TRAVERSE_CONTINUE; | |
3055 | } | |
3056 | this->found_ = true; | |
3057 | return TRAVERSE_EXIT; | |
3058 | } | |
3059 | ||
e440a328 | 3060 | // The nil value. |
3061 | ||
3062 | class Nil_expression : public Expression | |
3063 | { | |
3064 | public: | |
b13c66cd | 3065 | Nil_expression(Location location) |
e440a328 | 3066 | : Expression(EXPRESSION_NIL, location) |
3067 | { } | |
3068 | ||
3069 | static Expression* | |
3070 | do_import(Import*); | |
3071 | ||
3072 | protected: | |
3073 | bool | |
3074 | do_is_constant() const | |
3075 | { return true; } | |
3076 | ||
f9ca30f9 | 3077 | bool |
3ae06f68 | 3078 | do_is_static_initializer() const |
f9ca30f9 | 3079 | { return true; } |
3080 | ||
e440a328 | 3081 | Type* |
3082 | do_type() | |
3083 | { return Type::make_nil_type(); } | |
3084 | ||
3085 | void | |
3086 | do_determine_type(const Type_context*) | |
3087 | { } | |
3088 | ||
3089 | Expression* | |
3090 | do_copy() | |
3091 | { return this; } | |
3092 | ||
ea664253 | 3093 | Bexpression* |
3094 | do_get_backend(Translate_context* context) | |
3095 | { return context->backend()->nil_pointer_expression(); } | |
e440a328 | 3096 | |
3097 | void | |
3098 | do_export(Export* exp) const | |
3099 | { exp->write_c_string("nil"); } | |
d751bb78 | 3100 | |
3101 | void | |
3102 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3103 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 3104 | }; |
3105 | ||
3106 | // Import a nil expression. | |
3107 | ||
3108 | Expression* | |
3109 | Nil_expression::do_import(Import* imp) | |
3110 | { | |
3111 | imp->require_c_string("nil"); | |
3112 | return Expression::make_nil(imp->location()); | |
3113 | } | |
3114 | ||
3115 | // Make a nil expression. | |
3116 | ||
3117 | Expression* | |
b13c66cd | 3118 | Expression::make_nil(Location location) |
e440a328 | 3119 | { |
3120 | return new Nil_expression(location); | |
3121 | } | |
3122 | ||
3123 | // The value of the predeclared constant iota. This is little more | |
3124 | // than a marker. This will be lowered to an integer in | |
3125 | // Const_expression::do_lower, which is where we know the value that | |
3126 | // it should have. | |
3127 | ||
3128 | class Iota_expression : public Parser_expression | |
3129 | { | |
3130 | public: | |
b13c66cd | 3131 | Iota_expression(Location location) |
e440a328 | 3132 | : Parser_expression(EXPRESSION_IOTA, location) |
3133 | { } | |
3134 | ||
3135 | protected: | |
3136 | Expression* | |
ceeb4318 | 3137 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 3138 | { go_unreachable(); } |
e440a328 | 3139 | |
3140 | // There should only ever be one of these. | |
3141 | Expression* | |
3142 | do_copy() | |
c3e6f413 | 3143 | { go_unreachable(); } |
d751bb78 | 3144 | |
3145 | void | |
3146 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3147 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3148 | }; |
3149 | ||
3150 | // Make an iota expression. This is only called for one case: the | |
3151 | // value of the predeclared constant iota. | |
3152 | ||
3153 | Expression* | |
3154 | Expression::make_iota() | |
3155 | { | |
b13c66cd | 3156 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3157 | return &iota_expression; |
3158 | } | |
3159 | ||
da244e59 | 3160 | // Class Type_conversion_expression. |
e440a328 | 3161 | |
3162 | // Traversal. | |
3163 | ||
3164 | int | |
3165 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3166 | { | |
3167 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3168 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3169 | return TRAVERSE_EXIT; | |
3170 | return TRAVERSE_CONTINUE; | |
3171 | } | |
3172 | ||
3173 | // Convert to a constant at lowering time. | |
3174 | ||
3175 | Expression* | |
ceeb4318 | 3176 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3177 | Statement_inserter*, int) | |
e440a328 | 3178 | { |
3179 | Type* type = this->type_; | |
3180 | Expression* val = this->expr_; | |
b13c66cd | 3181 | Location location = this->location(); |
e440a328 | 3182 | |
0c77715b | 3183 | if (type->is_numeric_type()) |
e440a328 | 3184 | { |
0c77715b | 3185 | Numeric_constant nc; |
3186 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3187 | { |
0c77715b | 3188 | if (!nc.set_type(type, true, location)) |
3189 | return Expression::make_error(location); | |
3190 | return nc.expression(location); | |
e440a328 | 3191 | } |
e440a328 | 3192 | } |
3193 | ||
d7739c9a | 3194 | // According to the language specification on string conversions |
3195 | // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type): | |
3196 | // When converting an integer into a string, the string will be a UTF-8 | |
3197 | // representation of the integer and integers "outside the range of valid | |
3198 | // Unicode code points are converted to '\uFFFD'." | |
3199 | if (type->is_string_type()) | |
3200 | { | |
3201 | Numeric_constant nc; | |
3202 | if (val->numeric_constant_value(&nc) && nc.is_int()) | |
3203 | { | |
3204 | // An integer value doesn't fit in the Unicode code point range if it | |
3205 | // overflows the Go "int" type or is negative. | |
3206 | unsigned long ul; | |
3207 | if (!nc.set_type(Type::lookup_integer_type("int"), false, location) | |
3208 | || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE) | |
3209 | return Expression::make_string("\ufffd", location); | |
3210 | } | |
3211 | } | |
3212 | ||
55072f2b | 3213 | if (type->is_slice_type()) |
e440a328 | 3214 | { |
3215 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3216 | bool is_byte = (element_type->integer_type() != NULL |
3217 | && element_type->integer_type()->is_byte()); | |
3218 | bool is_rune = (element_type->integer_type() != NULL | |
3219 | && element_type->integer_type()->is_rune()); | |
3220 | if (is_byte || is_rune) | |
e440a328 | 3221 | { |
3222 | std::string s; | |
3223 | if (val->string_constant_value(&s)) | |
3224 | { | |
3225 | Expression_list* vals = new Expression_list(); | |
3226 | if (is_byte) | |
3227 | { | |
3228 | for (std::string::const_iterator p = s.begin(); | |
3229 | p != s.end(); | |
3230 | p++) | |
3231 | { | |
e67508fa | 3232 | unsigned char c = static_cast<unsigned char>(*p); |
3233 | vals->push_back(Expression::make_integer_ul(c, | |
3234 | element_type, | |
3235 | location)); | |
e440a328 | 3236 | } |
3237 | } | |
3238 | else | |
3239 | { | |
3240 | const char *p = s.data(); | |
3241 | const char *pend = s.data() + s.length(); | |
3242 | while (p < pend) | |
3243 | { | |
3244 | unsigned int c; | |
3245 | int adv = Lex::fetch_char(p, &c); | |
3246 | if (adv == 0) | |
3247 | { | |
631d5788 | 3248 | go_warning_at(this->location(), 0, |
e440a328 | 3249 | "invalid UTF-8 encoding"); |
3250 | adv = 1; | |
3251 | } | |
3252 | p += adv; | |
e67508fa | 3253 | vals->push_back(Expression::make_integer_ul(c, |
3254 | element_type, | |
3255 | location)); | |
e440a328 | 3256 | } |
3257 | } | |
3258 | ||
3259 | return Expression::make_slice_composite_literal(type, vals, | |
3260 | location); | |
3261 | } | |
3262 | } | |
3263 | } | |
3264 | ||
3265 | return this; | |
3266 | } | |
3267 | ||
35a54f17 | 3268 | // Flatten a type conversion by using a temporary variable for the slice |
3269 | // in slice to string conversions. | |
3270 | ||
3271 | Expression* | |
3272 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3273 | Statement_inserter* inserter) | |
3274 | { | |
5bf8be8b | 3275 | if (this->type()->is_error_type() || this->expr_->is_error_expression()) |
3276 | { | |
3277 | go_assert(saw_errors()); | |
3278 | return Expression::make_error(this->location()); | |
3279 | } | |
3280 | ||
2c809f8f | 3281 | if (((this->type()->is_string_type() |
3282 | && this->expr_->type()->is_slice_type()) | |
8ba8cc87 | 3283 | || this->expr_->type()->interface_type() != NULL) |
35a54f17 | 3284 | && !this->expr_->is_variable()) |
3285 | { | |
3286 | Temporary_statement* temp = | |
3287 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3288 | inserter->insert(temp); | |
3289 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3290 | } | |
3291 | return this; | |
3292 | } | |
3293 | ||
1ca01a59 | 3294 | // Return whether a type conversion is a constant. |
3295 | ||
3296 | bool | |
3297 | Type_conversion_expression::do_is_constant() const | |
3298 | { | |
3299 | if (!this->expr_->is_constant()) | |
3300 | return false; | |
3301 | ||
3302 | // A conversion to a type that may not be used as a constant is not | |
3303 | // a constant. For example, []byte(nil). | |
3304 | Type* type = this->type_; | |
3305 | if (type->integer_type() == NULL | |
3306 | && type->float_type() == NULL | |
3307 | && type->complex_type() == NULL | |
3308 | && !type->is_boolean_type() | |
3309 | && !type->is_string_type()) | |
3310 | return false; | |
3311 | ||
3312 | return true; | |
3313 | } | |
3314 | ||
3ae06f68 | 3315 | // Return whether a type conversion can be used in a constant |
3316 | // initializer. | |
0e168074 | 3317 | |
3318 | bool | |
3ae06f68 | 3319 | Type_conversion_expression::do_is_static_initializer() const |
0e168074 | 3320 | { |
3321 | Type* type = this->type_; | |
3322 | Type* expr_type = this->expr_->type(); | |
3323 | ||
3324 | if (type->interface_type() != NULL | |
3325 | || expr_type->interface_type() != NULL) | |
3326 | return false; | |
3327 | ||
3ae06f68 | 3328 | if (!this->expr_->is_static_initializer()) |
0e168074 | 3329 | return false; |
3330 | ||
3331 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3332 | return true; | |
3333 | ||
03118c21 | 3334 | if (type->is_string_type() && expr_type->is_string_type()) |
3335 | return true; | |
3336 | ||
3337 | if ((type->is_numeric_type() | |
3338 | || type->is_boolean_type() | |
3339 | || type->points_to() != NULL) | |
3340 | && (expr_type->is_numeric_type() | |
3341 | || expr_type->is_boolean_type() | |
3342 | || expr_type->points_to() != NULL)) | |
3343 | return true; | |
3344 | ||
3345 | return false; | |
0e168074 | 3346 | } |
3347 | ||
0c77715b | 3348 | // Return the constant numeric value if there is one. |
e440a328 | 3349 | |
3350 | bool | |
0c77715b | 3351 | Type_conversion_expression::do_numeric_constant_value( |
3352 | Numeric_constant* nc) const | |
e440a328 | 3353 | { |
0c77715b | 3354 | if (!this->type_->is_numeric_type()) |
e440a328 | 3355 | return false; |
0c77715b | 3356 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3357 | return false; |
0c77715b | 3358 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3359 | } |
3360 | ||
3361 | // Return the constant string value if there is one. | |
3362 | ||
3363 | bool | |
3364 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3365 | { | |
3366 | if (this->type_->is_string_type() | |
3367 | && this->expr_->type()->integer_type() != NULL) | |
3368 | { | |
0c77715b | 3369 | Numeric_constant nc; |
3370 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3371 | { |
0c77715b | 3372 | unsigned long ival; |
3373 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3374 | { |
0c77715b | 3375 | val->clear(); |
3376 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3377 | return true; |
3378 | } | |
3379 | } | |
e440a328 | 3380 | } |
3381 | ||
3382 | // FIXME: Could handle conversion from const []int here. | |
3383 | ||
3384 | return false; | |
3385 | } | |
3386 | ||
da244e59 | 3387 | // Determine the resulting type of the conversion. |
3388 | ||
3389 | void | |
3390 | Type_conversion_expression::do_determine_type(const Type_context*) | |
3391 | { | |
3392 | Type_context subcontext(this->type_, false); | |
3393 | this->expr_->determine_type(&subcontext); | |
3394 | } | |
3395 | ||
e440a328 | 3396 | // Check that types are convertible. |
3397 | ||
3398 | void | |
3399 | Type_conversion_expression::do_check_types(Gogo*) | |
3400 | { | |
3401 | Type* type = this->type_; | |
3402 | Type* expr_type = this->expr_->type(); | |
3403 | std::string reason; | |
3404 | ||
5c13bd80 | 3405 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3406 | { |
842f6425 | 3407 | this->set_is_error(); |
3408 | return; | |
3409 | } | |
3410 | ||
e440a328 | 3411 | if (this->may_convert_function_types_ |
3412 | && type->function_type() != NULL | |
3413 | && expr_type->function_type() != NULL) | |
3414 | return; | |
3415 | ||
3416 | if (Type::are_convertible(type, expr_type, &reason)) | |
3417 | return; | |
3418 | ||
631d5788 | 3419 | go_error_at(this->location(), "%s", reason.c_str()); |
e440a328 | 3420 | this->set_is_error(); |
3421 | } | |
3422 | ||
ea664253 | 3423 | // Get the backend representation for a type conversion. |
e440a328 | 3424 | |
ea664253 | 3425 | Bexpression* |
3426 | Type_conversion_expression::do_get_backend(Translate_context* context) | |
e440a328 | 3427 | { |
e440a328 | 3428 | Type* type = this->type_; |
3429 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3430 | |
3431 | Gogo* gogo = context->gogo(); | |
3432 | Btype* btype = type->get_backend(gogo); | |
2c809f8f | 3433 | Location loc = this->location(); |
3434 | ||
3435 | if (Type::are_identical(type, expr_type, false, NULL)) | |
859cdc93 | 3436 | { |
3437 | Bexpression* bexpr = this->expr_->get_backend(context); | |
3438 | return gogo->backend()->convert_expression(btype, bexpr, loc); | |
3439 | } | |
2c809f8f | 3440 | else if (type->interface_type() != NULL |
3441 | || expr_type->interface_type() != NULL) | |
e440a328 | 3442 | { |
2c809f8f | 3443 | Expression* conversion = |
3444 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3445 | this->location()); | |
ea664253 | 3446 | return conversion->get_backend(context); |
e440a328 | 3447 | } |
3448 | else if (type->is_string_type() | |
3449 | && expr_type->integer_type() != NULL) | |
3450 | { | |
2c809f8f | 3451 | mpz_t intval; |
3452 | Numeric_constant nc; | |
3453 | if (this->expr_->numeric_constant_value(&nc) | |
3454 | && nc.to_int(&intval) | |
3455 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3456 | { |
e440a328 | 3457 | std::string s; |
2c809f8f | 3458 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3459 | mpz_clear(intval); | |
3460 | Expression* se = Expression::make_string(s, loc); | |
ea664253 | 3461 | return se->get_backend(context); |
e440a328 | 3462 | } |
3463 | ||
f16ab008 | 3464 | Expression* i2s_expr = |
736a16ba | 3465 | Runtime::make_call(Runtime::INTSTRING, loc, 2, |
3466 | Expression::make_nil(loc), this->expr_); | |
ea664253 | 3467 | return Expression::make_cast(type, i2s_expr, loc)->get_backend(context); |
e440a328 | 3468 | } |
55072f2b | 3469 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3470 | { |
55072f2b | 3471 | Array_type* a = expr_type->array_type(); |
e440a328 | 3472 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3473 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3474 | go_assert(this->expr_->is_variable()); |
3475 | ||
3476 | Runtime::Function code; | |
60963afd | 3477 | if (e->integer_type()->is_byte()) |
736a16ba | 3478 | code = Runtime::SLICEBYTETOSTRING; |
e440a328 | 3479 | else |
35a54f17 | 3480 | { |
3481 | go_assert(e->integer_type()->is_rune()); | |
736a16ba | 3482 | code = Runtime::SLICERUNETOSTRING; |
35a54f17 | 3483 | } |
736a16ba | 3484 | return Runtime::make_call(code, loc, 2, Expression::make_nil(loc), |
3485 | this->expr_)->get_backend(context); | |
e440a328 | 3486 | } |
411eb89e | 3487 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3488 | { |
3489 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3490 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3491 | |
2c809f8f | 3492 | Runtime::Function code; |
60963afd | 3493 | if (e->integer_type()->is_byte()) |
736a16ba | 3494 | code = Runtime::STRINGTOSLICEBYTE; |
e440a328 | 3495 | else |
3496 | { | |
60963afd | 3497 | go_assert(e->integer_type()->is_rune()); |
736a16ba | 3498 | code = Runtime::STRINGTOSLICERUNE; |
e440a328 | 3499 | } |
736a16ba | 3500 | Expression* s2a = Runtime::make_call(code, loc, 2, |
3501 | Expression::make_nil(loc), | |
3502 | this->expr_); | |
ea664253 | 3503 | return Expression::make_unsafe_cast(type, s2a, loc)->get_backend(context); |
2c809f8f | 3504 | } |
3505 | else if (type->is_numeric_type()) | |
3506 | { | |
3507 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
859cdc93 | 3508 | Bexpression* bexpr = this->expr_->get_backend(context); |
ea664253 | 3509 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
e440a328 | 3510 | } |
3511 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3512 | && (expr_type->points_to() != NULL |
3513 | || expr_type->integer_type())) | |
3514 | || (expr_type->is_unsafe_pointer_type() | |
3515 | && type->points_to() != NULL) | |
3516 | || (this->may_convert_function_types_ | |
3517 | && type->function_type() != NULL | |
3518 | && expr_type->function_type() != NULL)) | |
859cdc93 | 3519 | { |
3520 | Bexpression* bexpr = this->expr_->get_backend(context); | |
3521 | return gogo->backend()->convert_expression(btype, bexpr, loc); | |
3522 | } | |
e440a328 | 3523 | else |
2c809f8f | 3524 | { |
3525 | Expression* conversion = | |
3526 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
ea664253 | 3527 | return conversion->get_backend(context); |
2c809f8f | 3528 | } |
e440a328 | 3529 | } |
3530 | ||
3531 | // Output a type conversion in a constant expression. | |
3532 | ||
3533 | void | |
3534 | Type_conversion_expression::do_export(Export* exp) const | |
3535 | { | |
3536 | exp->write_c_string("convert("); | |
3537 | exp->write_type(this->type_); | |
3538 | exp->write_c_string(", "); | |
3539 | this->expr_->export_expression(exp); | |
3540 | exp->write_c_string(")"); | |
3541 | } | |
3542 | ||
3543 | // Import a type conversion or a struct construction. | |
3544 | ||
3545 | Expression* | |
3546 | Type_conversion_expression::do_import(Import* imp) | |
3547 | { | |
3548 | imp->require_c_string("convert("); | |
3549 | Type* type = imp->read_type(); | |
3550 | imp->require_c_string(", "); | |
3551 | Expression* val = Expression::import_expression(imp); | |
3552 | imp->require_c_string(")"); | |
3553 | return Expression::make_cast(type, val, imp->location()); | |
3554 | } | |
3555 | ||
d751bb78 | 3556 | // Dump ast representation for a type conversion expression. |
3557 | ||
3558 | void | |
3559 | Type_conversion_expression::do_dump_expression( | |
3560 | Ast_dump_context* ast_dump_context) const | |
3561 | { | |
3562 | ast_dump_context->dump_type(this->type_); | |
3563 | ast_dump_context->ostream() << "("; | |
3564 | ast_dump_context->dump_expression(this->expr_); | |
3565 | ast_dump_context->ostream() << ") "; | |
3566 | } | |
3567 | ||
e440a328 | 3568 | // Make a type cast expression. |
3569 | ||
3570 | Expression* | |
b13c66cd | 3571 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3572 | { |
3573 | if (type->is_error_type() || val->is_error_expression()) | |
3574 | return Expression::make_error(location); | |
3575 | return new Type_conversion_expression(type, val, location); | |
3576 | } | |
3577 | ||
98f62f7a | 3578 | // Class Unsafe_type_conversion_expression. |
9581e91d | 3579 | |
3580 | // Traversal. | |
3581 | ||
3582 | int | |
3583 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3584 | { | |
3585 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3586 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3587 | return TRAVERSE_EXIT; | |
3588 | return TRAVERSE_CONTINUE; | |
3589 | } | |
3590 | ||
3ae06f68 | 3591 | // Return whether an unsafe type conversion can be used as a constant |
3592 | // initializer. | |
aa5ae575 | 3593 | |
3594 | bool | |
3ae06f68 | 3595 | Unsafe_type_conversion_expression::do_is_static_initializer() const |
aa5ae575 | 3596 | { |
3597 | Type* type = this->type_; | |
3598 | Type* expr_type = this->expr_->type(); | |
3599 | ||
3600 | if (type->interface_type() != NULL | |
3601 | || expr_type->interface_type() != NULL) | |
3602 | return false; | |
3603 | ||
3ae06f68 | 3604 | if (!this->expr_->is_static_initializer()) |
aa5ae575 | 3605 | return false; |
3606 | ||
3607 | if (Type::are_convertible(type, expr_type, NULL)) | |
3608 | return true; | |
3609 | ||
03118c21 | 3610 | if (type->is_string_type() && expr_type->is_string_type()) |
3611 | return true; | |
3612 | ||
3613 | if ((type->is_numeric_type() | |
3614 | || type->is_boolean_type() | |
3615 | || type->points_to() != NULL) | |
3616 | && (expr_type->is_numeric_type() | |
3617 | || expr_type->is_boolean_type() | |
3618 | || expr_type->points_to() != NULL)) | |
3619 | return true; | |
3620 | ||
3621 | return false; | |
aa5ae575 | 3622 | } |
3623 | ||
9581e91d | 3624 | // Convert to backend representation. |
3625 | ||
ea664253 | 3626 | Bexpression* |
3627 | Unsafe_type_conversion_expression::do_get_backend(Translate_context* context) | |
9581e91d | 3628 | { |
3629 | // We are only called for a limited number of cases. | |
3630 | ||
3631 | Type* t = this->type_; | |
3632 | Type* et = this->expr_->type(); | |
5c4802f1 | 3633 | |
3634 | if (t->is_error_type() | |
3635 | || this->expr_->is_error_expression() | |
3636 | || et->is_error_type()) | |
3637 | { | |
3638 | go_assert(saw_errors()); | |
3639 | return context->backend()->error_expression(); | |
3640 | } | |
3641 | ||
2c809f8f | 3642 | if (t->array_type() != NULL) |
3643 | go_assert(et->array_type() != NULL | |
3644 | && t->is_slice_type() == et->is_slice_type()); | |
3645 | else if (t->struct_type() != NULL) | |
9581e91d | 3646 | { |
2c809f8f | 3647 | if (t->named_type() != NULL |
3648 | && et->named_type() != NULL | |
3649 | && !Type::are_convertible(t, et, NULL)) | |
3650 | { | |
3651 | go_assert(saw_errors()); | |
ea664253 | 3652 | return context->backend()->error_expression(); |
2c809f8f | 3653 | } |
3654 | ||
3655 | go_assert(et->struct_type() != NULL | |
3656 | && Type::are_convertible(t, et, NULL)); | |
3657 | } | |
3658 | else if (t->map_type() != NULL) | |
c484d925 | 3659 | go_assert(et->map_type() != NULL); |
9581e91d | 3660 | else if (t->channel_type() != NULL) |
c484d925 | 3661 | go_assert(et->channel_type() != NULL); |
09ea332d | 3662 | else if (t->points_to() != NULL) |
2c809f8f | 3663 | go_assert(et->points_to() != NULL |
3664 | || et->channel_type() != NULL | |
3665 | || et->map_type() != NULL | |
3666 | || et->function_type() != NULL | |
132ed071 | 3667 | || et->integer_type() != NULL |
2c809f8f | 3668 | || et->is_nil_type()); |
9581e91d | 3669 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3670 | go_assert(t->points_to() != NULL); |
2c809f8f | 3671 | else if (t->interface_type() != NULL) |
9581e91d | 3672 | { |
2c809f8f | 3673 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3674 | go_assert(et->interface_type() != NULL |
2c809f8f | 3675 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3676 | } |
588e3cf9 | 3677 | else if (t->integer_type() != NULL) |
2c809f8f | 3678 | go_assert(et->is_boolean_type() |
3679 | || et->integer_type() != NULL | |
3680 | || et->function_type() != NULL | |
3681 | || et->points_to() != NULL | |
3682 | || et->map_type() != NULL | |
8ba8cc87 | 3683 | || et->channel_type() != NULL |
3684 | || et->is_nil_type()); | |
cd39797e | 3685 | else if (t->function_type() != NULL) |
3686 | go_assert(et->points_to() != NULL); | |
9581e91d | 3687 | else |
c3e6f413 | 3688 | go_unreachable(); |
9581e91d | 3689 | |
2c809f8f | 3690 | Gogo* gogo = context->gogo(); |
3691 | Btype* btype = t->get_backend(gogo); | |
ea664253 | 3692 | Bexpression* bexpr = this->expr_->get_backend(context); |
2c809f8f | 3693 | Location loc = this->location(); |
ea664253 | 3694 | return gogo->backend()->convert_expression(btype, bexpr, loc); |
9581e91d | 3695 | } |
3696 | ||
d751bb78 | 3697 | // Dump ast representation for an unsafe type conversion expression. |
3698 | ||
3699 | void | |
3700 | Unsafe_type_conversion_expression::do_dump_expression( | |
3701 | Ast_dump_context* ast_dump_context) const | |
3702 | { | |
3703 | ast_dump_context->dump_type(this->type_); | |
3704 | ast_dump_context->ostream() << "("; | |
3705 | ast_dump_context->dump_expression(this->expr_); | |
3706 | ast_dump_context->ostream() << ") "; | |
3707 | } | |
3708 | ||
9581e91d | 3709 | // Make an unsafe type conversion expression. |
3710 | ||
3711 | Expression* | |
3712 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3713 | Location location) |
9581e91d | 3714 | { |
3715 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3716 | } | |
3717 | ||
76f85fd6 | 3718 | // Class Unary_expression. |
e440a328 | 3719 | |
03118c21 | 3720 | // Call the address_taken method of the operand if needed. This is |
3721 | // called after escape analysis but before inserting write barriers. | |
3722 | ||
3723 | void | |
c1177ba4 | 3724 | Unary_expression::check_operand_address_taken(Gogo*) |
03118c21 | 3725 | { |
3726 | if (this->op_ != OPERATOR_AND) | |
3727 | return; | |
3728 | ||
3729 | // If this->escapes_ is false at this point, then it was set to | |
3730 | // false by an explicit call to set_does_not_escape, and the value | |
3731 | // does not escape. If this->escapes_ is true, we may be able to | |
3732 | // set it to false if taking the address of a variable that does not | |
3733 | // escape. | |
3734 | Node* n = Node::make_node(this); | |
3735 | if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE)) | |
3736 | this->escapes_ = false; | |
3737 | ||
03118c21 | 3738 | Named_object* var = NULL; |
3739 | if (this->expr_->var_expression() != NULL) | |
3740 | var = this->expr_->var_expression()->named_object(); | |
3741 | else if (this->expr_->enclosed_var_expression() != NULL) | |
3742 | var = this->expr_->enclosed_var_expression()->variable(); | |
3743 | ||
3744 | if (this->escapes_ && var != NULL) | |
3745 | { | |
3746 | if (var->is_variable()) | |
3747 | this->escapes_ = var->var_value()->escapes(); | |
3748 | if (var->is_result_variable()) | |
3749 | this->escapes_ = var->result_var_value()->escapes(); | |
3750 | } | |
3751 | ||
3752 | this->expr_->address_taken(this->escapes_); | |
3753 | } | |
3754 | ||
e440a328 | 3755 | // If we are taking the address of a composite literal, and the |
2c809f8f | 3756 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3757 | // instead. |
3758 | ||
3759 | Expression* | |
ceeb4318 | 3760 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3761 | { |
b13c66cd | 3762 | Location loc = this->location(); |
e440a328 | 3763 | Operator op = this->op_; |
3764 | Expression* expr = this->expr_; | |
3765 | ||
3766 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3767 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3768 | ||
3769 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3770 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3771 | // analysis here? This case is found in math/unsafe.go and is | |
3772 | // therefore worth special casing. | |
3773 | if (op == OPERATOR_MULT) | |
3774 | { | |
3775 | Expression* e = expr; | |
3776 | while (e->classification() == EXPRESSION_CONVERSION) | |
3777 | { | |
3778 | Type_conversion_expression* te | |
3779 | = static_cast<Type_conversion_expression*>(e); | |
3780 | e = te->expr(); | |
3781 | } | |
3782 | ||
3783 | if (e->classification() == EXPRESSION_UNARY) | |
3784 | { | |
3785 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3786 | if (ue->op_ == OPERATOR_AND) | |
3787 | { | |
3788 | if (e == expr) | |
3789 | { | |
3790 | // *&x == x. | |
f4dea966 | 3791 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3792 | { | |
631d5788 | 3793 | go_error_at(ue->location(), |
3794 | "invalid operand for unary %<&%>"); | |
f4dea966 | 3795 | this->set_is_error(); |
3796 | } | |
e440a328 | 3797 | return ue->expr_; |
3798 | } | |
3799 | ue->set_does_not_escape(); | |
3800 | } | |
3801 | } | |
3802 | } | |
3803 | ||
55661ce9 | 3804 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3805 | // having to deal with TYPE_VOID in other places. | |
3806 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3807 | { | |
631d5788 | 3808 | go_error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); |
55661ce9 | 3809 | return Expression::make_error(this->location()); |
3810 | } | |
3811 | ||
d9f3743a | 3812 | // Check for an invalid pointer dereference. We need to do this |
3813 | // here because Unary_expression::do_type will return an error type | |
3814 | // in this case. That can cause code to appear erroneous, and | |
3815 | // therefore disappear at lowering time, without any error message. | |
3816 | if (op == OPERATOR_MULT && expr->type()->points_to() == NULL) | |
3817 | { | |
3818 | this->report_error(_("expected pointer")); | |
3819 | return Expression::make_error(this->location()); | |
3820 | } | |
3821 | ||
59a401fe | 3822 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3823 | { |
0c77715b | 3824 | Numeric_constant nc; |
3825 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3826 | { |
0c77715b | 3827 | Numeric_constant result; |
af7a5274 | 3828 | bool issued_error; |
3829 | if (Unary_expression::eval_constant(op, &nc, loc, &result, | |
3830 | &issued_error)) | |
0c77715b | 3831 | return result.expression(loc); |
af7a5274 | 3832 | else if (issued_error) |
3833 | return Expression::make_error(this->location()); | |
e440a328 | 3834 | } |
3835 | } | |
3836 | ||
3837 | return this; | |
3838 | } | |
3839 | ||
f9ca30f9 | 3840 | // Flatten expression if a nil check must be performed and create temporary |
3841 | // variables if necessary. | |
3842 | ||
3843 | Expression* | |
3844 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3845 | Statement_inserter* inserter) | |
3846 | { | |
5bf8be8b | 3847 | if (this->is_error_expression() |
3848 | || this->expr_->is_error_expression() | |
3849 | || this->expr_->type()->is_error_type()) | |
3850 | { | |
3851 | go_assert(saw_errors()); | |
3852 | return Expression::make_error(this->location()); | |
3853 | } | |
f4dea966 | 3854 | |
f9ca30f9 | 3855 | Location location = this->location(); |
3856 | if (this->op_ == OPERATOR_MULT | |
3857 | && !this->expr_->is_variable()) | |
3858 | { | |
3859 | go_assert(this->expr_->type()->points_to() != NULL); | |
f614ea8b | 3860 | switch (this->requires_nil_check(gogo)) |
f9ca30f9 | 3861 | { |
f614ea8b | 3862 | case NIL_CHECK_ERROR_ENCOUNTERED: |
2a305b85 | 3863 | { |
3864 | go_assert(saw_errors()); | |
3865 | return Expression::make_error(this->location()); | |
3866 | } | |
f614ea8b | 3867 | case NIL_CHECK_NOT_NEEDED: |
3868 | break; | |
3869 | case NIL_CHECK_NEEDED: | |
3870 | this->create_temp_ = true; | |
3871 | break; | |
3872 | case NIL_CHECK_DEFAULT: | |
3873 | go_unreachable(); | |
f9ca30f9 | 3874 | } |
3875 | } | |
3876 | ||
3877 | if (this->create_temp_ && !this->expr_->is_variable()) | |
3878 | { | |
3879 | Temporary_statement* temp = | |
3880 | Statement::make_temporary(NULL, this->expr_, location); | |
3881 | inserter->insert(temp); | |
3882 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3883 | } | |
3884 | ||
3885 | return this; | |
3886 | } | |
3887 | ||
e440a328 | 3888 | // Return whether a unary expression is a constant. |
3889 | ||
3890 | bool | |
3891 | Unary_expression::do_is_constant() const | |
3892 | { | |
3893 | if (this->op_ == OPERATOR_MULT) | |
3894 | { | |
3895 | // Indirecting through a pointer is only constant if the object | |
3896 | // to which the expression points is constant, but we currently | |
3897 | // have no way to determine that. | |
3898 | return false; | |
3899 | } | |
3900 | else if (this->op_ == OPERATOR_AND) | |
3901 | { | |
3902 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3903 | // global variable, not constant otherwise. In other cases taking the |
3904 | // address is probably not a constant. | |
e440a328 | 3905 | Var_expression* ve = this->expr_->var_expression(); |
3906 | if (ve != NULL) | |
3907 | { | |
3908 | Named_object* no = ve->named_object(); | |
3909 | return no->is_variable() && no->var_value()->is_global(); | |
3910 | } | |
3911 | return false; | |
3912 | } | |
3913 | else | |
3914 | return this->expr_->is_constant(); | |
3915 | } | |
3916 | ||
3ae06f68 | 3917 | // Return whether a unary expression can be used as a constant |
3918 | // initializer. | |
3919 | ||
3920 | bool | |
3921 | Unary_expression::do_is_static_initializer() const | |
3922 | { | |
3923 | if (this->op_ == OPERATOR_MULT) | |
3924 | return false; | |
3925 | else if (this->op_ == OPERATOR_AND) | |
de048538 | 3926 | return Unary_expression::base_is_static_initializer(this->expr_); |
3927 | else | |
3928 | return this->expr_->is_static_initializer(); | |
3929 | } | |
3ae06f68 | 3930 | |
de048538 | 3931 | // Return whether the address of EXPR can be used as a static |
3932 | // initializer. | |
3ae06f68 | 3933 | |
de048538 | 3934 | bool |
3935 | Unary_expression::base_is_static_initializer(Expression* expr) | |
3936 | { | |
3937 | // The address of a field reference can be a static initializer if | |
3938 | // the base can be a static initializer. | |
3939 | Field_reference_expression* fre = expr->field_reference_expression(); | |
3940 | if (fre != NULL) | |
3941 | return Unary_expression::base_is_static_initializer(fre->expr()); | |
3942 | ||
3943 | // The address of an index expression can be a static initializer if | |
3944 | // the base can be a static initializer and the index is constant. | |
3945 | Array_index_expression* aind = expr->array_index_expression(); | |
3946 | if (aind != NULL) | |
3947 | return (aind->end() == NULL | |
3948 | && aind->start()->is_constant() | |
3949 | && Unary_expression::base_is_static_initializer(aind->array())); | |
3950 | ||
3951 | // The address of a global variable can be a static initializer. | |
3952 | Var_expression* ve = expr->var_expression(); | |
3953 | if (ve != NULL) | |
3954 | { | |
3955 | Named_object* no = ve->named_object(); | |
3956 | return no->is_variable() && no->var_value()->is_global(); | |
3957 | } | |
3958 | ||
3959 | // The address of a composite literal can be used as a static | |
3960 | // initializer if the composite literal is itself usable as a | |
3961 | // static initializer. | |
3962 | if (expr->is_composite_literal() && expr->is_static_initializer()) | |
3963 | return true; | |
3ae06f68 | 3964 | |
de048538 | 3965 | // The address of a string constant can be used as a static |
3966 | // initializer. This can not be written in Go itself but this is | |
3967 | // used when building a type descriptor. | |
3968 | if (expr->string_expression() != NULL) | |
3969 | return true; | |
3970 | ||
3971 | return false; | |
3ae06f68 | 3972 | } |
3973 | ||
f614ea8b | 3974 | // Return whether this dereference expression requires an explicit nil |
3975 | // check. If we are dereferencing the pointer to a large struct | |
3976 | // (greater than the specified size threshold), we need to check for | |
3977 | // nil. We don't bother to check for small structs because we expect | |
3978 | // the system to crash on a nil pointer dereference. However, if we | |
3979 | // know the address of this expression is being taken, we must always | |
3980 | // check for nil. | |
3981 | Unary_expression::Nil_check_classification | |
3982 | Unary_expression::requires_nil_check(Gogo* gogo) | |
3983 | { | |
3984 | go_assert(this->op_ == OPERATOR_MULT); | |
3985 | go_assert(this->expr_->type()->points_to() != NULL); | |
3986 | ||
3987 | if (this->issue_nil_check_ == NIL_CHECK_NEEDED) | |
3988 | return NIL_CHECK_NEEDED; | |
3989 | else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED) | |
3990 | return NIL_CHECK_NOT_NEEDED; | |
3991 | ||
3992 | Type* ptype = this->expr_->type()->points_to(); | |
3993 | int64_t type_size = -1; | |
3994 | if (!ptype->is_void_type()) | |
3995 | { | |
3996 | bool ok = ptype->backend_type_size(gogo, &type_size); | |
3997 | if (!ok) | |
3998 | return NIL_CHECK_ERROR_ENCOUNTERED; | |
3999 | } | |
4000 | ||
4001 | int64_t size_cutoff = gogo->nil_check_size_threshold(); | |
4002 | if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff)) | |
4003 | this->issue_nil_check_ = NIL_CHECK_NEEDED; | |
4004 | else | |
4005 | this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED; | |
4006 | return this->issue_nil_check_; | |
4007 | } | |
4008 | ||
0c77715b | 4009 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
af7a5274 | 4010 | // could be done, false if not. On overflow, issues an error and sets |
4011 | // *ISSUED_ERROR. | |
e440a328 | 4012 | |
4013 | bool | |
0c77715b | 4014 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
af7a5274 | 4015 | Location location, Numeric_constant* nc, |
4016 | bool* issued_error) | |
e440a328 | 4017 | { |
af7a5274 | 4018 | *issued_error = false; |
e440a328 | 4019 | switch (op) |
4020 | { | |
4021 | case OPERATOR_PLUS: | |
0c77715b | 4022 | *nc = *unc; |
e440a328 | 4023 | return true; |
0c77715b | 4024 | |
e440a328 | 4025 | case OPERATOR_MINUS: |
0c77715b | 4026 | if (unc->is_int() || unc->is_rune()) |
4027 | break; | |
4028 | else if (unc->is_float()) | |
4029 | { | |
4030 | mpfr_t uval; | |
4031 | unc->get_float(&uval); | |
4032 | mpfr_t val; | |
4033 | mpfr_init(val); | |
4034 | mpfr_neg(val, uval, GMP_RNDN); | |
4035 | nc->set_float(unc->type(), val); | |
4036 | mpfr_clear(uval); | |
4037 | mpfr_clear(val); | |
4038 | return true; | |
4039 | } | |
4040 | else if (unc->is_complex()) | |
4041 | { | |
fcbea5e4 | 4042 | mpc_t uval; |
4043 | unc->get_complex(&uval); | |
4044 | mpc_t val; | |
4045 | mpc_init2(val, mpc_precision); | |
4046 | mpc_neg(val, uval, MPC_RNDNN); | |
4047 | nc->set_complex(unc->type(), val); | |
4048 | mpc_clear(uval); | |
4049 | mpc_clear(val); | |
0c77715b | 4050 | return true; |
4051 | } | |
e440a328 | 4052 | else |
0c77715b | 4053 | go_unreachable(); |
e440a328 | 4054 | |
0c77715b | 4055 | case OPERATOR_XOR: |
4056 | break; | |
68448d53 | 4057 | |
59a401fe | 4058 | case OPERATOR_NOT: |
e440a328 | 4059 | case OPERATOR_AND: |
4060 | case OPERATOR_MULT: | |
4061 | return false; | |
0c77715b | 4062 | |
e440a328 | 4063 | default: |
c3e6f413 | 4064 | go_unreachable(); |
e440a328 | 4065 | } |
e440a328 | 4066 | |
0c77715b | 4067 | if (!unc->is_int() && !unc->is_rune()) |
4068 | return false; | |
4069 | ||
4070 | mpz_t uval; | |
8387e1df | 4071 | if (unc->is_rune()) |
4072 | unc->get_rune(&uval); | |
4073 | else | |
4074 | unc->get_int(&uval); | |
0c77715b | 4075 | mpz_t val; |
4076 | mpz_init(val); | |
e440a328 | 4077 | |
e440a328 | 4078 | switch (op) |
4079 | { | |
e440a328 | 4080 | case OPERATOR_MINUS: |
0c77715b | 4081 | mpz_neg(val, uval); |
4082 | break; | |
4083 | ||
e440a328 | 4084 | case OPERATOR_NOT: |
0c77715b | 4085 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
4086 | break; | |
4087 | ||
e440a328 | 4088 | case OPERATOR_XOR: |
0c77715b | 4089 | { |
4090 | Type* utype = unc->type(); | |
4091 | if (utype->integer_type() == NULL | |
4092 | || utype->integer_type()->is_abstract()) | |
4093 | mpz_com(val, uval); | |
4094 | else | |
4095 | { | |
4096 | // The number of HOST_WIDE_INTs that it takes to represent | |
4097 | // UVAL. | |
4098 | size_t count = ((mpz_sizeinbase(uval, 2) | |
4099 | + HOST_BITS_PER_WIDE_INT | |
4100 | - 1) | |
4101 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 4102 | |
0c77715b | 4103 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
4104 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
4105 | ||
4106 | size_t obits = utype->integer_type()->bits(); | |
4107 | ||
4108 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
4109 | { | |
4110 | mpz_t adj; | |
4111 | mpz_init_set_ui(adj, 1); | |
4112 | mpz_mul_2exp(adj, adj, obits); | |
4113 | mpz_add(uval, uval, adj); | |
4114 | mpz_clear(adj); | |
4115 | } | |
4116 | ||
4117 | size_t ecount; | |
4118 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
4119 | go_assert(ecount <= count); | |
4120 | ||
4121 | // Trim down to the number of words required by the type. | |
4122 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
4123 | / HOST_BITS_PER_WIDE_INT); | |
4124 | go_assert(ocount <= count); | |
4125 | ||
4126 | for (size_t i = 0; i < ocount; ++i) | |
4127 | phwi[i] = ~phwi[i]; | |
4128 | ||
4129 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
4130 | if (clearbits != 0) | |
4131 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
4132 | >> clearbits); | |
4133 | ||
4134 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
4135 | ||
4136 | if (!utype->integer_type()->is_unsigned() | |
4137 | && mpz_tstbit(val, obits - 1)) | |
4138 | { | |
4139 | mpz_t adj; | |
4140 | mpz_init_set_ui(adj, 1); | |
4141 | mpz_mul_2exp(adj, adj, obits); | |
4142 | mpz_sub(val, val, adj); | |
4143 | mpz_clear(adj); | |
4144 | } | |
4145 | ||
4146 | delete[] phwi; | |
4147 | } | |
4148 | } | |
4149 | break; | |
e440a328 | 4150 | |
e440a328 | 4151 | default: |
c3e6f413 | 4152 | go_unreachable(); |
e440a328 | 4153 | } |
e440a328 | 4154 | |
0c77715b | 4155 | if (unc->is_rune()) |
4156 | nc->set_rune(NULL, val); | |
e440a328 | 4157 | else |
0c77715b | 4158 | nc->set_int(NULL, val); |
e440a328 | 4159 | |
0c77715b | 4160 | mpz_clear(uval); |
4161 | mpz_clear(val); | |
e440a328 | 4162 | |
af7a5274 | 4163 | if (!nc->set_type(unc->type(), true, location)) |
4164 | { | |
4165 | *issued_error = true; | |
4166 | return false; | |
4167 | } | |
4168 | return true; | |
e440a328 | 4169 | } |
4170 | ||
0c77715b | 4171 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 4172 | |
4173 | bool | |
0c77715b | 4174 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 4175 | { |
0c77715b | 4176 | Numeric_constant unc; |
4177 | if (!this->expr_->numeric_constant_value(&unc)) | |
4178 | return false; | |
af7a5274 | 4179 | bool issued_error; |
0c77715b | 4180 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), |
af7a5274 | 4181 | nc, &issued_error); |
e440a328 | 4182 | } |
4183 | ||
4184 | // Return the type of a unary expression. | |
4185 | ||
4186 | Type* | |
4187 | Unary_expression::do_type() | |
4188 | { | |
4189 | switch (this->op_) | |
4190 | { | |
4191 | case OPERATOR_PLUS: | |
4192 | case OPERATOR_MINUS: | |
4193 | case OPERATOR_NOT: | |
4194 | case OPERATOR_XOR: | |
4195 | return this->expr_->type(); | |
4196 | ||
4197 | case OPERATOR_AND: | |
4198 | return Type::make_pointer_type(this->expr_->type()); | |
4199 | ||
4200 | case OPERATOR_MULT: | |
4201 | { | |
4202 | Type* subtype = this->expr_->type(); | |
4203 | Type* points_to = subtype->points_to(); | |
4204 | if (points_to == NULL) | |
4205 | return Type::make_error_type(); | |
4206 | return points_to; | |
4207 | } | |
4208 | ||
4209 | default: | |
c3e6f413 | 4210 | go_unreachable(); |
e440a328 | 4211 | } |
4212 | } | |
4213 | ||
4214 | // Determine abstract types for a unary expression. | |
4215 | ||
4216 | void | |
4217 | Unary_expression::do_determine_type(const Type_context* context) | |
4218 | { | |
4219 | switch (this->op_) | |
4220 | { | |
4221 | case OPERATOR_PLUS: | |
4222 | case OPERATOR_MINUS: | |
4223 | case OPERATOR_NOT: | |
4224 | case OPERATOR_XOR: | |
4225 | this->expr_->determine_type(context); | |
4226 | break; | |
4227 | ||
4228 | case OPERATOR_AND: | |
4229 | // Taking the address of something. | |
4230 | { | |
4231 | Type* subtype = (context->type == NULL | |
4232 | ? NULL | |
4233 | : context->type->points_to()); | |
4234 | Type_context subcontext(subtype, false); | |
4235 | this->expr_->determine_type(&subcontext); | |
4236 | } | |
4237 | break; | |
4238 | ||
4239 | case OPERATOR_MULT: | |
4240 | // Indirecting through a pointer. | |
4241 | { | |
4242 | Type* subtype = (context->type == NULL | |
4243 | ? NULL | |
4244 | : Type::make_pointer_type(context->type)); | |
4245 | Type_context subcontext(subtype, false); | |
4246 | this->expr_->determine_type(&subcontext); | |
4247 | } | |
4248 | break; | |
4249 | ||
4250 | default: | |
c3e6f413 | 4251 | go_unreachable(); |
e440a328 | 4252 | } |
4253 | } | |
4254 | ||
4255 | // Check types for a unary expression. | |
4256 | ||
4257 | void | |
4258 | Unary_expression::do_check_types(Gogo*) | |
4259 | { | |
9fe897ef | 4260 | Type* type = this->expr_->type(); |
5c13bd80 | 4261 | if (type->is_error()) |
9fe897ef | 4262 | { |
4263 | this->set_is_error(); | |
4264 | return; | |
4265 | } | |
4266 | ||
e440a328 | 4267 | switch (this->op_) |
4268 | { | |
4269 | case OPERATOR_PLUS: | |
4270 | case OPERATOR_MINUS: | |
9fe897ef | 4271 | if (type->integer_type() == NULL |
4272 | && type->float_type() == NULL | |
4273 | && type->complex_type() == NULL) | |
4274 | this->report_error(_("expected numeric type")); | |
e440a328 | 4275 | break; |
4276 | ||
4277 | case OPERATOR_NOT: | |
59a401fe | 4278 | if (!type->is_boolean_type()) |
4279 | this->report_error(_("expected boolean type")); | |
4280 | break; | |
4281 | ||
e440a328 | 4282 | case OPERATOR_XOR: |
b3b1474e | 4283 | if (type->integer_type() == NULL) |
4284 | this->report_error(_("expected integer")); | |
e440a328 | 4285 | break; |
4286 | ||
4287 | case OPERATOR_AND: | |
4288 | if (!this->expr_->is_addressable()) | |
09ea332d | 4289 | { |
4290 | if (!this->create_temp_) | |
f4dea966 | 4291 | { |
631d5788 | 4292 | go_error_at(this->location(), "invalid operand for unary %<&%>"); |
f4dea966 | 4293 | this->set_is_error(); |
4294 | } | |
09ea332d | 4295 | } |
e440a328 | 4296 | else |
da244e59 | 4297 | this->expr_->issue_nil_check(); |
e440a328 | 4298 | break; |
4299 | ||
4300 | case OPERATOR_MULT: | |
4301 | // Indirecting through a pointer. | |
9fe897ef | 4302 | if (type->points_to() == NULL) |
4303 | this->report_error(_("expected pointer")); | |
7661d702 | 4304 | if (type->points_to()->is_error()) |
4305 | this->set_is_error(); | |
e440a328 | 4306 | break; |
4307 | ||
4308 | default: | |
c3e6f413 | 4309 | go_unreachable(); |
e440a328 | 4310 | } |
4311 | } | |
4312 | ||
ea664253 | 4313 | // Get the backend representation for a unary expression. |
e440a328 | 4314 | |
ea664253 | 4315 | Bexpression* |
4316 | Unary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 4317 | { |
1b1f2abf | 4318 | Gogo* gogo = context->gogo(); |
e9d3367e | 4319 | Location loc = this->location(); |
4320 | ||
4321 | // Taking the address of a set-and-use-temporary expression requires | |
4322 | // setting the temporary and then taking the address. | |
4323 | if (this->op_ == OPERATOR_AND) | |
4324 | { | |
4325 | Set_and_use_temporary_expression* sut = | |
4326 | this->expr_->set_and_use_temporary_expression(); | |
4327 | if (sut != NULL) | |
4328 | { | |
4329 | Temporary_statement* temp = sut->temporary(); | |
4330 | Bvariable* bvar = temp->get_backend_variable(context); | |
d4e6573e | 4331 | Bexpression* bvar_expr = |
7af8e400 | 4332 | gogo->backend()->var_expression(bvar, loc); |
ea664253 | 4333 | Bexpression* bval = sut->expression()->get_backend(context); |
f9ca30f9 | 4334 | |
0ab48656 | 4335 | Named_object* fn = context->function(); |
4336 | go_assert(fn != NULL); | |
4337 | Bfunction* bfn = | |
4338 | fn->func_value()->get_or_make_decl(gogo, fn); | |
f9ca30f9 | 4339 | Bstatement* bassign = |
0ab48656 | 4340 | gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc); |
f9ca30f9 | 4341 | Bexpression* bvar_addr = |
4342 | gogo->backend()->address_expression(bvar_expr, loc); | |
ea664253 | 4343 | return gogo->backend()->compound_expression(bassign, bvar_addr, loc); |
e9d3367e | 4344 | } |
4345 | } | |
4346 | ||
f9ca30f9 | 4347 | Bexpression* ret; |
ea664253 | 4348 | Bexpression* bexpr = this->expr_->get_backend(context); |
f9ca30f9 | 4349 | Btype* btype = this->expr_->type()->get_backend(gogo); |
e440a328 | 4350 | switch (this->op_) |
4351 | { | |
4352 | case OPERATOR_PLUS: | |
f9ca30f9 | 4353 | ret = bexpr; |
4354 | break; | |
e440a328 | 4355 | |
4356 | case OPERATOR_MINUS: | |
f9ca30f9 | 4357 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4358 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4359 | break; | |
e440a328 | 4360 | |
4361 | case OPERATOR_NOT: | |
e440a328 | 4362 | case OPERATOR_XOR: |
f9ca30f9 | 4363 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4364 | break; | |
e440a328 | 4365 | |
4366 | case OPERATOR_AND: | |
09ea332d | 4367 | if (!this->create_temp_) |
4368 | { | |
4369 | // We should not see a non-constant constructor here; cases | |
4370 | // where we would see one should have been moved onto the | |
4371 | // heap at parse time. Taking the address of a nonconstant | |
4372 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4373 | |
4374 | go_assert(!this->expr_->is_composite_literal() | |
3ae06f68 | 4375 | || this->expr_->is_static_initializer()); |
24060bf9 | 4376 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4377 | { | |
4378 | Unary_expression* ue = | |
4379 | static_cast<Unary_expression*>(this->expr_); | |
4380 | go_assert(ue->op() != OPERATOR_AND); | |
4381 | } | |
09ea332d | 4382 | } |
e440a328 | 4383 | |
f23d7786 | 4384 | if (this->is_gc_root_ || this->is_slice_init_) |
76f85fd6 | 4385 | { |
19272321 | 4386 | std::string var_name; |
f23d7786 | 4387 | bool copy_to_heap = false; |
4388 | if (this->is_gc_root_) | |
4389 | { | |
4390 | // Build a decl for a GC root variable. GC roots are mutable, so | |
4391 | // they cannot be represented as an immutable_struct in the | |
4392 | // backend. | |
19272321 | 4393 | var_name = gogo->gc_root_name(); |
f23d7786 | 4394 | } |
4395 | else | |
4396 | { | |
4397 | // Build a decl for a slice value initializer. An immutable slice | |
4398 | // value initializer may have to be copied to the heap if it | |
4399 | // contains pointers in a non-constant context. | |
19272321 | 4400 | var_name = gogo->initializer_name(); |
f23d7786 | 4401 | |
4402 | Array_type* at = this->expr_->type()->array_type(); | |
4403 | go_assert(at != NULL); | |
4404 | ||
4405 | // If we are not copying the value to the heap, we will only | |
4406 | // initialize the value once, so we can use this directly | |
4407 | // rather than copying it. In that case we can't make it | |
4408 | // read-only, because the program is permitted to change it. | |
3ae06f68 | 4409 | copy_to_heap = context->function() != NULL; |
f23d7786 | 4410 | } |
19272321 | 4411 | std::string asm_name(go_selectively_encode_id(var_name)); |
f23d7786 | 4412 | Bvariable* implicit = |
19272321 | 4413 | gogo->backend()->implicit_variable(var_name, asm_name, |
438b4bec | 4414 | btype, true, copy_to_heap, |
4415 | false, 0); | |
19272321 | 4416 | gogo->backend()->implicit_variable_set_init(implicit, var_name, btype, |
aa5ae575 | 4417 | true, copy_to_heap, false, |
4418 | bexpr); | |
7af8e400 | 4419 | bexpr = gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4420 | |
4421 | // If we are not copying a slice initializer to the heap, | |
4422 | // then it can be changed by the program, so if it can | |
4423 | // contain pointers we must register it as a GC root. | |
4424 | if (this->is_slice_init_ | |
4425 | && !copy_to_heap | |
4426 | && this->expr_->type()->has_pointer()) | |
4427 | { | |
4428 | Bexpression* root = | |
7af8e400 | 4429 | gogo->backend()->var_expression(implicit, loc); |
1b4fb1e0 | 4430 | root = gogo->backend()->address_expression(root, loc); |
4431 | Type* type = Type::make_pointer_type(this->expr_->type()); | |
4432 | gogo->add_gc_root(Expression::make_backend(root, type, loc)); | |
4433 | } | |
76f85fd6 | 4434 | } |
4435 | else if ((this->expr_->is_composite_literal() | |
3ae06f68 | 4436 | || this->expr_->string_expression() != NULL) |
4437 | && this->expr_->is_static_initializer()) | |
f9ca30f9 | 4438 | { |
19272321 | 4439 | std::string var_name(gogo->initializer_name()); |
4440 | std::string asm_name(go_selectively_encode_id(var_name)); | |
f9ca30f9 | 4441 | Bvariable* decl = |
19272321 | 4442 | gogo->backend()->immutable_struct(var_name, asm_name, |
438b4bec | 4443 | true, false, btype, loc); |
19272321 | 4444 | gogo->backend()->immutable_struct_set_init(decl, var_name, true, |
4445 | false, btype, loc, bexpr); | |
7af8e400 | 4446 | bexpr = gogo->backend()->var_expression(decl, loc); |
f9ca30f9 | 4447 | } |
09ea332d | 4448 | |
f9ca30f9 | 4449 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4450 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4451 | break; | |
e440a328 | 4452 | |
4453 | case OPERATOR_MULT: | |
4454 | { | |
f9ca30f9 | 4455 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4456 | |
f614ea8b | 4457 | bool known_valid = false; |
f9ca30f9 | 4458 | Type* ptype = this->expr_->type()->points_to(); |
4459 | Btype* pbtype = ptype->get_backend(gogo); | |
f614ea8b | 4460 | switch (this->requires_nil_check(gogo)) |
4461 | { | |
4462 | case NIL_CHECK_NOT_NEEDED: | |
4463 | break; | |
4464 | case NIL_CHECK_ERROR_ENCOUNTERED: | |
2a305b85 | 4465 | { |
4466 | go_assert(saw_errors()); | |
4467 | return gogo->backend()->error_expression(); | |
4468 | } | |
f614ea8b | 4469 | case NIL_CHECK_NEEDED: |
4470 | { | |
f9ca30f9 | 4471 | go_assert(this->expr_->is_variable()); |
2dd89704 | 4472 | |
4473 | // If we're nil-checking the result of a set-and-use-temporary | |
4474 | // expression, then pick out the target temp and use that | |
4475 | // for the final result of the conditional. | |
4476 | Bexpression* tbexpr = bexpr; | |
4477 | Bexpression* ubexpr = bexpr; | |
4478 | Set_and_use_temporary_expression* sut = | |
4479 | this->expr_->set_and_use_temporary_expression(); | |
4480 | if (sut != NULL) { | |
4481 | Temporary_statement* temp = sut->temporary(); | |
4482 | Bvariable* bvar = temp->get_backend_variable(context); | |
4483 | ubexpr = gogo->backend()->var_expression(bvar, loc); | |
4484 | } | |
ea664253 | 4485 | Bexpression* nil = |
f614ea8b | 4486 | Expression::make_nil(loc)->get_backend(context); |
f9ca30f9 | 4487 | Bexpression* compare = |
2dd89704 | 4488 | gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr, |
f9ca30f9 | 4489 | nil, loc); |
f9ca30f9 | 4490 | Bexpression* crash = |
f614ea8b | 4491 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
4492 | loc)->get_backend(context); | |
93715b75 | 4493 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
4494 | bexpr = gogo->backend()->conditional_expression(bfn, btype, | |
4495 | compare, | |
2dd89704 | 4496 | crash, ubexpr, |
f9ca30f9 | 4497 | loc); |
f614ea8b | 4498 | known_valid = true; |
4499 | break; | |
4500 | } | |
4501 | case NIL_CHECK_DEFAULT: | |
4502 | go_unreachable(); | |
4503 | } | |
4504 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, | |
4505 | known_valid, loc); | |
e440a328 | 4506 | } |
f9ca30f9 | 4507 | break; |
e440a328 | 4508 | |
4509 | default: | |
c3e6f413 | 4510 | go_unreachable(); |
e440a328 | 4511 | } |
f9ca30f9 | 4512 | |
ea664253 | 4513 | return ret; |
e440a328 | 4514 | } |
4515 | ||
4516 | // Export a unary expression. | |
4517 | ||
4518 | void | |
4519 | Unary_expression::do_export(Export* exp) const | |
4520 | { | |
4521 | switch (this->op_) | |
4522 | { | |
4523 | case OPERATOR_PLUS: | |
4524 | exp->write_c_string("+ "); | |
4525 | break; | |
4526 | case OPERATOR_MINUS: | |
4527 | exp->write_c_string("- "); | |
4528 | break; | |
4529 | case OPERATOR_NOT: | |
4530 | exp->write_c_string("! "); | |
4531 | break; | |
4532 | case OPERATOR_XOR: | |
4533 | exp->write_c_string("^ "); | |
4534 | break; | |
4535 | case OPERATOR_AND: | |
4536 | case OPERATOR_MULT: | |
4537 | default: | |
c3e6f413 | 4538 | go_unreachable(); |
e440a328 | 4539 | } |
4540 | this->expr_->export_expression(exp); | |
4541 | } | |
4542 | ||
4543 | // Import a unary expression. | |
4544 | ||
4545 | Expression* | |
4546 | Unary_expression::do_import(Import* imp) | |
4547 | { | |
4548 | Operator op; | |
4549 | switch (imp->get_char()) | |
4550 | { | |
4551 | case '+': | |
4552 | op = OPERATOR_PLUS; | |
4553 | break; | |
4554 | case '-': | |
4555 | op = OPERATOR_MINUS; | |
4556 | break; | |
4557 | case '!': | |
4558 | op = OPERATOR_NOT; | |
4559 | break; | |
4560 | case '^': | |
4561 | op = OPERATOR_XOR; | |
4562 | break; | |
4563 | default: | |
c3e6f413 | 4564 | go_unreachable(); |
e440a328 | 4565 | } |
4566 | imp->require_c_string(" "); | |
4567 | Expression* expr = Expression::import_expression(imp); | |
4568 | return Expression::make_unary(op, expr, imp->location()); | |
4569 | } | |
4570 | ||
d751bb78 | 4571 | // Dump ast representation of an unary expression. |
4572 | ||
4573 | void | |
4574 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4575 | { | |
4576 | ast_dump_context->dump_operator(this->op_); | |
4577 | ast_dump_context->ostream() << "("; | |
4578 | ast_dump_context->dump_expression(this->expr_); | |
4579 | ast_dump_context->ostream() << ") "; | |
4580 | } | |
4581 | ||
e440a328 | 4582 | // Make a unary expression. |
4583 | ||
4584 | Expression* | |
b13c66cd | 4585 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4586 | { |
4587 | return new Unary_expression(op, expr, location); | |
4588 | } | |
4589 | ||
f614ea8b | 4590 | Expression* |
4591 | Expression::make_dereference(Expression* ptr, | |
4592 | Nil_check_classification docheck, | |
4593 | Location location) | |
4594 | { | |
4595 | Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location); | |
4596 | if (docheck == NIL_CHECK_NEEDED) | |
4597 | deref->unary_expression()->set_requires_nil_check(true); | |
4598 | else if (docheck == NIL_CHECK_NOT_NEEDED) | |
4599 | deref->unary_expression()->set_requires_nil_check(false); | |
4600 | return deref; | |
4601 | } | |
4602 | ||
e440a328 | 4603 | // If this is an indirection through a pointer, return the expression |
4604 | // being pointed through. Otherwise return this. | |
4605 | ||
4606 | Expression* | |
4607 | Expression::deref() | |
4608 | { | |
4609 | if (this->classification_ == EXPRESSION_UNARY) | |
4610 | { | |
4611 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4612 | if (ue->op() == OPERATOR_MULT) | |
4613 | return ue->operand(); | |
4614 | } | |
4615 | return this; | |
4616 | } | |
4617 | ||
4618 | // Class Binary_expression. | |
4619 | ||
4620 | // Traversal. | |
4621 | ||
4622 | int | |
4623 | Binary_expression::do_traverse(Traverse* traverse) | |
4624 | { | |
4625 | int t = Expression::traverse(&this->left_, traverse); | |
4626 | if (t == TRAVERSE_EXIT) | |
4627 | return TRAVERSE_EXIT; | |
4628 | return Expression::traverse(&this->right_, traverse); | |
4629 | } | |
4630 | ||
3ae06f68 | 4631 | // Return whether this expression may be used as a static initializer. |
4632 | ||
4633 | bool | |
4634 | Binary_expression::do_is_static_initializer() const | |
4635 | { | |
4636 | if (!this->left_->is_static_initializer() | |
4637 | || !this->right_->is_static_initializer()) | |
4638 | return false; | |
4639 | ||
4640 | // Addresses can be static initializers, but we can't implement | |
4641 | // arbitray binary expressions of them. | |
4642 | Unary_expression* lu = this->left_->unary_expression(); | |
4643 | Unary_expression* ru = this->right_->unary_expression(); | |
4644 | if (lu != NULL && lu->op() == OPERATOR_AND) | |
4645 | { | |
4646 | if (ru != NULL && ru->op() == OPERATOR_AND) | |
4647 | return this->op_ == OPERATOR_MINUS; | |
4648 | else | |
4649 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4650 | } | |
4651 | else if (ru != NULL && ru->op() == OPERATOR_AND) | |
4652 | return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS; | |
4653 | ||
4654 | // Other cases should resolve in the backend. | |
4655 | return true; | |
4656 | } | |
4657 | ||
0c77715b | 4658 | // Return the type to use for a binary operation on operands of |
4659 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4660 | // such may be NULL or abstract. | |
4661 | ||
4662 | bool | |
4663 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4664 | Type* right_type, Type** result_type) | |
4665 | { | |
4666 | if (left_type != right_type | |
4667 | && !left_type->is_abstract() | |
4668 | && !right_type->is_abstract() | |
4669 | && left_type->base() != right_type->base() | |
4670 | && op != OPERATOR_LSHIFT | |
4671 | && op != OPERATOR_RSHIFT) | |
4672 | { | |
4673 | // May be a type error--let it be diagnosed elsewhere. | |
4674 | return false; | |
4675 | } | |
4676 | ||
4677 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4678 | { | |
4679 | if (left_type->integer_type() != NULL) | |
4680 | *result_type = left_type; | |
4681 | else | |
4682 | *result_type = Type::make_abstract_integer_type(); | |
4683 | } | |
4684 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4685 | *result_type = left_type; | |
4686 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4687 | *result_type = right_type; | |
4688 | else if (!left_type->is_abstract()) | |
4689 | *result_type = left_type; | |
4690 | else if (!right_type->is_abstract()) | |
4691 | *result_type = right_type; | |
4692 | else if (left_type->complex_type() != NULL) | |
4693 | *result_type = left_type; | |
4694 | else if (right_type->complex_type() != NULL) | |
4695 | *result_type = right_type; | |
4696 | else if (left_type->float_type() != NULL) | |
4697 | *result_type = left_type; | |
4698 | else if (right_type->float_type() != NULL) | |
4699 | *result_type = right_type; | |
4700 | else if (left_type->integer_type() != NULL | |
4701 | && left_type->integer_type()->is_rune()) | |
4702 | *result_type = left_type; | |
4703 | else if (right_type->integer_type() != NULL | |
4704 | && right_type->integer_type()->is_rune()) | |
4705 | *result_type = right_type; | |
4706 | else | |
4707 | *result_type = left_type; | |
4708 | ||
4709 | return true; | |
4710 | } | |
4711 | ||
4712 | // Convert an integer comparison code and an operator to a boolean | |
4713 | // value. | |
e440a328 | 4714 | |
4715 | bool | |
0c77715b | 4716 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4717 | { |
e440a328 | 4718 | switch (op) |
4719 | { | |
4720 | case OPERATOR_EQEQ: | |
0c77715b | 4721 | return cmp == 0; |
4722 | break; | |
e440a328 | 4723 | case OPERATOR_NOTEQ: |
0c77715b | 4724 | return cmp != 0; |
4725 | break; | |
e440a328 | 4726 | case OPERATOR_LT: |
0c77715b | 4727 | return cmp < 0; |
4728 | break; | |
e440a328 | 4729 | case OPERATOR_LE: |
0c77715b | 4730 | return cmp <= 0; |
e440a328 | 4731 | case OPERATOR_GT: |
0c77715b | 4732 | return cmp > 0; |
e440a328 | 4733 | case OPERATOR_GE: |
0c77715b | 4734 | return cmp >= 0; |
e440a328 | 4735 | default: |
c3e6f413 | 4736 | go_unreachable(); |
e440a328 | 4737 | } |
4738 | } | |
4739 | ||
0c77715b | 4740 | // Compare constants according to OP. |
e440a328 | 4741 | |
4742 | bool | |
0c77715b | 4743 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4744 | Numeric_constant* right_nc, | |
4745 | Location location, bool* result) | |
e440a328 | 4746 | { |
0c77715b | 4747 | Type* left_type = left_nc->type(); |
4748 | Type* right_type = right_nc->type(); | |
4749 | ||
4750 | Type* type; | |
4751 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4752 | return false; | |
4753 | ||
4754 | // When comparing an untyped operand to a typed operand, we are | |
4755 | // effectively coercing the untyped operand to the other operand's | |
4756 | // type, so make sure that is valid. | |
4757 | if (!left_nc->set_type(type, true, location) | |
4758 | || !right_nc->set_type(type, true, location)) | |
4759 | return false; | |
4760 | ||
4761 | bool ret; | |
4762 | int cmp; | |
4763 | if (type->complex_type() != NULL) | |
4764 | { | |
4765 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4766 | return false; | |
4767 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4768 | } | |
4769 | else if (type->float_type() != NULL) | |
4770 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4771 | else |
0c77715b | 4772 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4773 | ||
4774 | if (ret) | |
4775 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4776 | ||
4777 | return ret; | |
4778 | } | |
4779 | ||
4780 | // Compare integer constants. | |
4781 | ||
4782 | bool | |
4783 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4784 | const Numeric_constant* right_nc, | |
4785 | int* cmp) | |
4786 | { | |
4787 | mpz_t left_val; | |
4788 | if (!left_nc->to_int(&left_val)) | |
4789 | return false; | |
4790 | mpz_t right_val; | |
4791 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4792 | { |
0c77715b | 4793 | mpz_clear(left_val); |
4794 | return false; | |
e440a328 | 4795 | } |
0c77715b | 4796 | |
4797 | *cmp = mpz_cmp(left_val, right_val); | |
4798 | ||
4799 | mpz_clear(left_val); | |
4800 | mpz_clear(right_val); | |
4801 | ||
4802 | return true; | |
4803 | } | |
4804 | ||
4805 | // Compare floating point constants. | |
4806 | ||
4807 | bool | |
4808 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4809 | const Numeric_constant* right_nc, | |
4810 | int* cmp) | |
4811 | { | |
4812 | mpfr_t left_val; | |
4813 | if (!left_nc->to_float(&left_val)) | |
4814 | return false; | |
4815 | mpfr_t right_val; | |
4816 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4817 | { |
0c77715b | 4818 | mpfr_clear(left_val); |
4819 | return false; | |
4820 | } | |
4821 | ||
4822 | // We already coerced both operands to the same type. If that type | |
4823 | // is not an abstract type, we need to round the values accordingly. | |
4824 | Type* type = left_nc->type(); | |
4825 | if (!type->is_abstract() && type->float_type() != NULL) | |
4826 | { | |
4827 | int bits = type->float_type()->bits(); | |
4828 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4829 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4830 | } |
0c77715b | 4831 | |
4832 | *cmp = mpfr_cmp(left_val, right_val); | |
4833 | ||
4834 | mpfr_clear(left_val); | |
4835 | mpfr_clear(right_val); | |
4836 | ||
4837 | return true; | |
e440a328 | 4838 | } |
4839 | ||
0c77715b | 4840 | // Compare complex constants. Complex numbers may only be compared |
4841 | // for equality. | |
e440a328 | 4842 | |
4843 | bool | |
0c77715b | 4844 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4845 | const Numeric_constant* right_nc, | |
4846 | int* cmp) | |
e440a328 | 4847 | { |
fcbea5e4 | 4848 | mpc_t left_val; |
4849 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 4850 | return false; |
fcbea5e4 | 4851 | mpc_t right_val; |
4852 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 4853 | { |
fcbea5e4 | 4854 | mpc_clear(left_val); |
0c77715b | 4855 | return false; |
e440a328 | 4856 | } |
0c77715b | 4857 | |
4858 | // We already coerced both operands to the same type. If that type | |
4859 | // is not an abstract type, we need to round the values accordingly. | |
4860 | Type* type = left_nc->type(); | |
4861 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4862 | { |
0c77715b | 4863 | int bits = type->complex_type()->bits(); |
fcbea5e4 | 4864 | mpfr_prec_round(mpc_realref(left_val), bits / 2, GMP_RNDN); |
4865 | mpfr_prec_round(mpc_imagref(left_val), bits / 2, GMP_RNDN); | |
4866 | mpfr_prec_round(mpc_realref(right_val), bits / 2, GMP_RNDN); | |
4867 | mpfr_prec_round(mpc_imagref(right_val), bits / 2, GMP_RNDN); | |
e440a328 | 4868 | } |
0c77715b | 4869 | |
fcbea5e4 | 4870 | *cmp = mpc_cmp(left_val, right_val) != 0; |
0c77715b | 4871 | |
fcbea5e4 | 4872 | mpc_clear(left_val); |
4873 | mpc_clear(right_val); | |
0c77715b | 4874 | |
4875 | return true; | |
e440a328 | 4876 | } |
4877 | ||
0c77715b | 4878 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4879 | // true if this could be done, false if not. Issue errors at LOCATION | |
af7a5274 | 4880 | // as appropriate, and sets *ISSUED_ERROR if it did. |
e440a328 | 4881 | |
4882 | bool | |
0c77715b | 4883 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4884 | Numeric_constant* right_nc, | |
af7a5274 | 4885 | Location location, Numeric_constant* nc, |
4886 | bool* issued_error) | |
e440a328 | 4887 | { |
af7a5274 | 4888 | *issued_error = false; |
e440a328 | 4889 | switch (op) |
4890 | { | |
4891 | case OPERATOR_OROR: | |
4892 | case OPERATOR_ANDAND: | |
4893 | case OPERATOR_EQEQ: | |
4894 | case OPERATOR_NOTEQ: | |
4895 | case OPERATOR_LT: | |
4896 | case OPERATOR_LE: | |
4897 | case OPERATOR_GT: | |
4898 | case OPERATOR_GE: | |
9767e2d3 | 4899 | // These return boolean values, not numeric. |
4900 | return false; | |
0c77715b | 4901 | default: |
4902 | break; | |
4903 | } | |
4904 | ||
4905 | Type* left_type = left_nc->type(); | |
4906 | Type* right_type = right_nc->type(); | |
4907 | ||
4908 | Type* type; | |
4909 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4910 | return false; | |
4911 | ||
4912 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4913 | ||
4914 | // When combining an untyped operand with a typed operand, we are | |
4915 | // effectively coercing the untyped operand to the other operand's | |
4916 | // type, so make sure that is valid. | |
4917 | if (!left_nc->set_type(type, true, location)) | |
4918 | return false; | |
4919 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4920 | return false; | |
85334a21 | 4921 | if (is_shift |
4922 | && ((left_type->integer_type() == NULL | |
4923 | && !left_type->is_abstract()) | |
4924 | || (right_type->integer_type() == NULL | |
4925 | && !right_type->is_abstract()))) | |
4926 | return false; | |
0c77715b | 4927 | |
4928 | bool r; | |
4929 | if (type->complex_type() != NULL) | |
4930 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4931 | else if (type->float_type() != NULL) | |
4932 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4933 | else | |
4934 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4935 | ||
4936 | if (r) | |
af7a5274 | 4937 | { |
4938 | r = nc->set_type(type, true, location); | |
4939 | if (!r) | |
4940 | *issued_error = true; | |
4941 | } | |
0c77715b | 4942 | |
4943 | return r; | |
4944 | } | |
4945 | ||
4946 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4947 | // integer operations. Return true if this could be done, false if | |
4948 | // not. | |
4949 | ||
4950 | bool | |
4951 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4952 | const Numeric_constant* right_nc, | |
4953 | Location location, Numeric_constant* nc) | |
4954 | { | |
4955 | mpz_t left_val; | |
4956 | if (!left_nc->to_int(&left_val)) | |
4957 | return false; | |
4958 | mpz_t right_val; | |
4959 | if (!right_nc->to_int(&right_val)) | |
4960 | { | |
4961 | mpz_clear(left_val); | |
e440a328 | 4962 | return false; |
0c77715b | 4963 | } |
4964 | ||
4965 | mpz_t val; | |
4966 | mpz_init(val); | |
4967 | ||
4968 | switch (op) | |
4969 | { | |
e440a328 | 4970 | case OPERATOR_PLUS: |
4971 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4972 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4973 | { | |
631d5788 | 4974 | go_error_at(location, "constant addition overflow"); |
71a45216 | 4975 | nc->set_invalid(); |
2c809f8f | 4976 | mpz_set_ui(val, 1); |
4977 | } | |
e440a328 | 4978 | break; |
4979 | case OPERATOR_MINUS: | |
4980 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4981 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4982 | { | |
631d5788 | 4983 | go_error_at(location, "constant subtraction overflow"); |
71a45216 | 4984 | nc->set_invalid(); |
2c809f8f | 4985 | mpz_set_ui(val, 1); |
4986 | } | |
e440a328 | 4987 | break; |
4988 | case OPERATOR_OR: | |
4989 | mpz_ior(val, left_val, right_val); | |
4990 | break; | |
4991 | case OPERATOR_XOR: | |
4992 | mpz_xor(val, left_val, right_val); | |
4993 | break; | |
4994 | case OPERATOR_MULT: | |
4995 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4996 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4997 | { | |
631d5788 | 4998 | go_error_at(location, "constant multiplication overflow"); |
71a45216 | 4999 | nc->set_invalid(); |
2c809f8f | 5000 | mpz_set_ui(val, 1); |
5001 | } | |
e440a328 | 5002 | break; |
5003 | case OPERATOR_DIV: | |
5004 | if (mpz_sgn(right_val) != 0) | |
5005 | mpz_tdiv_q(val, left_val, right_val); | |
5006 | else | |
5007 | { | |
631d5788 | 5008 | go_error_at(location, "division by zero"); |
71a45216 | 5009 | nc->set_invalid(); |
e440a328 | 5010 | mpz_set_ui(val, 0); |
e440a328 | 5011 | } |
5012 | break; | |
5013 | case OPERATOR_MOD: | |
5014 | if (mpz_sgn(right_val) != 0) | |
5015 | mpz_tdiv_r(val, left_val, right_val); | |
5016 | else | |
5017 | { | |
631d5788 | 5018 | go_error_at(location, "division by zero"); |
71a45216 | 5019 | nc->set_invalid(); |
e440a328 | 5020 | mpz_set_ui(val, 0); |
e440a328 | 5021 | } |
5022 | break; | |
5023 | case OPERATOR_LSHIFT: | |
5024 | { | |
5025 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 5026 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
5027 | mpz_mul_2exp(val, left_val, shift); | |
5028 | else | |
e440a328 | 5029 | { |
631d5788 | 5030 | go_error_at(location, "shift count overflow"); |
71a45216 | 5031 | nc->set_invalid(); |
2c809f8f | 5032 | mpz_set_ui(val, 1); |
e440a328 | 5033 | } |
e440a328 | 5034 | break; |
5035 | } | |
5036 | break; | |
5037 | case OPERATOR_RSHIFT: | |
5038 | { | |
5039 | unsigned long shift = mpz_get_ui(right_val); | |
5040 | if (mpz_cmp_ui(right_val, shift) != 0) | |
5041 | { | |
631d5788 | 5042 | go_error_at(location, "shift count overflow"); |
71a45216 | 5043 | nc->set_invalid(); |
2c809f8f | 5044 | mpz_set_ui(val, 1); |
e440a328 | 5045 | } |
e440a328 | 5046 | else |
0c77715b | 5047 | { |
5048 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
5049 | mpz_tdiv_q_2exp(val, left_val, shift); | |
5050 | else | |
5051 | mpz_fdiv_q_2exp(val, left_val, shift); | |
5052 | } | |
e440a328 | 5053 | break; |
5054 | } | |
5055 | break; | |
5056 | case OPERATOR_AND: | |
5057 | mpz_and(val, left_val, right_val); | |
5058 | break; | |
5059 | case OPERATOR_BITCLEAR: | |
5060 | { | |
5061 | mpz_t tval; | |
5062 | mpz_init(tval); | |
5063 | mpz_com(tval, right_val); | |
5064 | mpz_and(val, left_val, tval); | |
5065 | mpz_clear(tval); | |
5066 | } | |
5067 | break; | |
5068 | default: | |
c3e6f413 | 5069 | go_unreachable(); |
e440a328 | 5070 | } |
5071 | ||
0c77715b | 5072 | mpz_clear(left_val); |
5073 | mpz_clear(right_val); | |
e440a328 | 5074 | |
0c77715b | 5075 | if (left_nc->is_rune() |
5076 | || (op != OPERATOR_LSHIFT | |
5077 | && op != OPERATOR_RSHIFT | |
5078 | && right_nc->is_rune())) | |
5079 | nc->set_rune(NULL, val); | |
5080 | else | |
5081 | nc->set_int(NULL, val); | |
5082 | ||
5083 | mpz_clear(val); | |
e440a328 | 5084 | |
5085 | return true; | |
5086 | } | |
5087 | ||
0c77715b | 5088 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5089 | // floating point operations. Return true if this could be done, | |
5090 | // false if not. | |
e440a328 | 5091 | |
5092 | bool | |
0c77715b | 5093 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
5094 | const Numeric_constant* right_nc, | |
5095 | Location location, Numeric_constant* nc) | |
e440a328 | 5096 | { |
0c77715b | 5097 | mpfr_t left_val; |
5098 | if (!left_nc->to_float(&left_val)) | |
5099 | return false; | |
5100 | mpfr_t right_val; | |
5101 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 5102 | { |
0c77715b | 5103 | mpfr_clear(left_val); |
e440a328 | 5104 | return false; |
0c77715b | 5105 | } |
5106 | ||
5107 | mpfr_t val; | |
5108 | mpfr_init(val); | |
5109 | ||
5110 | bool ret = true; | |
5111 | switch (op) | |
5112 | { | |
e440a328 | 5113 | case OPERATOR_PLUS: |
5114 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
5115 | break; | |
5116 | case OPERATOR_MINUS: | |
5117 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
5118 | break; | |
5119 | case OPERATOR_OR: | |
5120 | case OPERATOR_XOR: | |
5121 | case OPERATOR_AND: | |
5122 | case OPERATOR_BITCLEAR: | |
0c77715b | 5123 | case OPERATOR_MOD: |
5124 | case OPERATOR_LSHIFT: | |
5125 | case OPERATOR_RSHIFT: | |
5126 | mpfr_set_ui(val, 0, GMP_RNDN); | |
5127 | ret = false; | |
5128 | break; | |
e440a328 | 5129 | case OPERATOR_MULT: |
5130 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
5131 | break; | |
5132 | case OPERATOR_DIV: | |
0c77715b | 5133 | if (!mpfr_zero_p(right_val)) |
5134 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
5135 | else | |
5136 | { | |
631d5788 | 5137 | go_error_at(location, "division by zero"); |
71a45216 | 5138 | nc->set_invalid(); |
0c77715b | 5139 | mpfr_set_ui(val, 0, GMP_RNDN); |
5140 | } | |
e440a328 | 5141 | break; |
e440a328 | 5142 | default: |
c3e6f413 | 5143 | go_unreachable(); |
e440a328 | 5144 | } |
5145 | ||
0c77715b | 5146 | mpfr_clear(left_val); |
5147 | mpfr_clear(right_val); | |
e440a328 | 5148 | |
0c77715b | 5149 | nc->set_float(NULL, val); |
5150 | mpfr_clear(val); | |
e440a328 | 5151 | |
0c77715b | 5152 | return ret; |
e440a328 | 5153 | } |
5154 | ||
0c77715b | 5155 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
5156 | // complex operations. Return true if this could be done, false if | |
5157 | // not. | |
e440a328 | 5158 | |
5159 | bool | |
0c77715b | 5160 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
5161 | const Numeric_constant* right_nc, | |
5162 | Location location, Numeric_constant* nc) | |
e440a328 | 5163 | { |
fcbea5e4 | 5164 | mpc_t left_val; |
5165 | if (!left_nc->to_complex(&left_val)) | |
0c77715b | 5166 | return false; |
fcbea5e4 | 5167 | mpc_t right_val; |
5168 | if (!right_nc->to_complex(&right_val)) | |
e440a328 | 5169 | { |
fcbea5e4 | 5170 | mpc_clear(left_val); |
e440a328 | 5171 | return false; |
0c77715b | 5172 | } |
5173 | ||
fcbea5e4 | 5174 | mpc_t val; |
5175 | mpc_init2(val, mpc_precision); | |
0c77715b | 5176 | |
5177 | bool ret = true; | |
5178 | switch (op) | |
5179 | { | |
e440a328 | 5180 | case OPERATOR_PLUS: |
fcbea5e4 | 5181 | mpc_add(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5182 | break; |
5183 | case OPERATOR_MINUS: | |
fcbea5e4 | 5184 | mpc_sub(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5185 | break; |
5186 | case OPERATOR_OR: | |
5187 | case OPERATOR_XOR: | |
5188 | case OPERATOR_AND: | |
5189 | case OPERATOR_BITCLEAR: | |
0c77715b | 5190 | case OPERATOR_MOD: |
5191 | case OPERATOR_LSHIFT: | |
5192 | case OPERATOR_RSHIFT: | |
fcbea5e4 | 5193 | mpc_set_ui(val, 0, MPC_RNDNN); |
0c77715b | 5194 | ret = false; |
5195 | break; | |
e440a328 | 5196 | case OPERATOR_MULT: |
fcbea5e4 | 5197 | mpc_mul(val, left_val, right_val, MPC_RNDNN); |
e440a328 | 5198 | break; |
5199 | case OPERATOR_DIV: | |
fcbea5e4 | 5200 | if (mpc_cmp_si(right_val, 0) == 0) |
5201 | { | |
631d5788 | 5202 | go_error_at(location, "division by zero"); |
71a45216 | 5203 | nc->set_invalid(); |
fcbea5e4 | 5204 | mpc_set_ui(val, 0, MPC_RNDNN); |
5205 | break; | |
5206 | } | |
5207 | mpc_div(val, left_val, right_val, MPC_RNDNN); | |
e440a328 | 5208 | break; |
e440a328 | 5209 | default: |
c3e6f413 | 5210 | go_unreachable(); |
e440a328 | 5211 | } |
5212 | ||
fcbea5e4 | 5213 | mpc_clear(left_val); |
5214 | mpc_clear(right_val); | |
e440a328 | 5215 | |
fcbea5e4 | 5216 | nc->set_complex(NULL, val); |
5217 | mpc_clear(val); | |
e440a328 | 5218 | |
0c77715b | 5219 | return ret; |
e440a328 | 5220 | } |
5221 | ||
5222 | // Lower a binary expression. We have to evaluate constant | |
5223 | // expressions now, in order to implement Go's unlimited precision | |
5224 | // constants. | |
5225 | ||
5226 | Expression* | |
e9d3367e | 5227 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5228 | Statement_inserter* inserter, int) | |
e440a328 | 5229 | { |
b13c66cd | 5230 | Location location = this->location(); |
e440a328 | 5231 | Operator op = this->op_; |
5232 | Expression* left = this->left_; | |
5233 | Expression* right = this->right_; | |
5234 | ||
5235 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5236 | || op == OPERATOR_NOTEQ | |
5237 | || op == OPERATOR_LT | |
5238 | || op == OPERATOR_LE | |
5239 | || op == OPERATOR_GT | |
5240 | || op == OPERATOR_GE); | |
5241 | ||
0c77715b | 5242 | // Numeric constant expressions. |
e440a328 | 5243 | { |
0c77715b | 5244 | Numeric_constant left_nc; |
5245 | Numeric_constant right_nc; | |
5246 | if (left->numeric_constant_value(&left_nc) | |
5247 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5248 | { |
0c77715b | 5249 | if (is_comparison) |
e440a328 | 5250 | { |
0c77715b | 5251 | bool result; |
5252 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5253 | &right_nc, location, | |
5254 | &result)) | |
5255 | return this; | |
e90c9dfc | 5256 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5257 | Expression::make_boolean(result, |
5258 | location), | |
5259 | location); | |
e440a328 | 5260 | } |
5261 | else | |
5262 | { | |
0c77715b | 5263 | Numeric_constant nc; |
af7a5274 | 5264 | bool issued_error; |
0c77715b | 5265 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, |
af7a5274 | 5266 | location, &nc, |
5267 | &issued_error)) | |
5268 | { | |
5269 | if (issued_error) | |
5270 | return Expression::make_error(location); | |
71a45216 | 5271 | return this; |
af7a5274 | 5272 | } |
0c77715b | 5273 | return nc.expression(location); |
e440a328 | 5274 | } |
5275 | } | |
e440a328 | 5276 | } |
5277 | ||
5278 | // String constant expressions. | |
315fa98d | 5279 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5280 | { |
5281 | std::string left_string; | |
5282 | std::string right_string; | |
5283 | if (left->string_constant_value(&left_string) | |
5284 | && right->string_constant_value(&right_string)) | |
315fa98d | 5285 | { |
5286 | if (op == OPERATOR_PLUS) | |
5287 | return Expression::make_string(left_string + right_string, | |
5288 | location); | |
5289 | else if (is_comparison) | |
5290 | { | |
5291 | int cmp = left_string.compare(right_string); | |
0c77715b | 5292 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5293 | return Expression::make_boolean(r, location); |
b40dc774 | 5294 | } |
5295 | } | |
b40dc774 | 5296 | } |
5297 | ||
ceeb12d7 | 5298 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5299 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5300 | { | |
b79832ca | 5301 | if (left->type()->struct_type() != NULL |
5302 | && right->type()->struct_type() != NULL) | |
e9d3367e | 5303 | return this->lower_struct_comparison(gogo, inserter); |
5304 | else if (left->type()->array_type() != NULL | |
b79832ca | 5305 | && !left->type()->is_slice_type() |
5306 | && right->type()->array_type() != NULL | |
5307 | && !right->type()->is_slice_type()) | |
e9d3367e | 5308 | return this->lower_array_comparison(gogo, inserter); |
ceeb12d7 | 5309 | else if ((left->type()->interface_type() != NULL |
5310 | && right->type()->interface_type() == NULL) | |
5311 | || (left->type()->interface_type() == NULL | |
5312 | && right->type()->interface_type() != NULL)) | |
5313 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5314 | } |
5315 | ||
736a16ba | 5316 | // Lower string concatenation to String_concat_expression, so that |
5317 | // we can group sequences of string additions. | |
5318 | if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS) | |
5319 | { | |
5320 | Expression_list* exprs; | |
5321 | String_concat_expression* left_sce = | |
5322 | this->left_->string_concat_expression(); | |
5323 | if (left_sce != NULL) | |
5324 | exprs = left_sce->exprs(); | |
5325 | else | |
5326 | { | |
5327 | exprs = new Expression_list(); | |
5328 | exprs->push_back(this->left_); | |
5329 | } | |
5330 | ||
5331 | String_concat_expression* right_sce = | |
5332 | this->right_->string_concat_expression(); | |
5333 | if (right_sce != NULL) | |
5334 | exprs->append(right_sce->exprs()); | |
5335 | else | |
5336 | exprs->push_back(this->right_); | |
5337 | ||
5338 | return Expression::make_string_concat(exprs); | |
5339 | } | |
5340 | ||
e440a328 | 5341 | return this; |
5342 | } | |
5343 | ||
e9d3367e | 5344 | // Lower a struct comparison. |
5345 | ||
5346 | Expression* | |
5347 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5348 | Statement_inserter* inserter) | |
5349 | { | |
5350 | Struct_type* st = this->left_->type()->struct_type(); | |
5351 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5352 | if (st2 == NULL) | |
5353 | return this; | |
5354 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5355 | return this; | |
5356 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5357 | this->right_->type(), NULL)) | |
5358 | return this; | |
5359 | ||
5360 | // See if we can compare using memcmp. As a heuristic, we use | |
5361 | // memcmp rather than field references and comparisons if there are | |
5362 | // more than two fields. | |
113ef6a5 | 5363 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5364 | return this->lower_compare_to_memcmp(gogo, inserter); |
5365 | ||
5366 | Location loc = this->location(); | |
5367 | ||
5368 | Expression* left = this->left_; | |
5369 | Temporary_statement* left_temp = NULL; | |
5370 | if (left->var_expression() == NULL | |
5371 | && left->temporary_reference_expression() == NULL) | |
5372 | { | |
5373 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5374 | inserter->insert(left_temp); | |
5375 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5376 | } | |
5377 | ||
5378 | Expression* right = this->right_; | |
5379 | Temporary_statement* right_temp = NULL; | |
5380 | if (right->var_expression() == NULL | |
5381 | && right->temporary_reference_expression() == NULL) | |
5382 | { | |
5383 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5384 | inserter->insert(right_temp); | |
5385 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5386 | } | |
5387 | ||
5388 | Expression* ret = Expression::make_boolean(true, loc); | |
5389 | const Struct_field_list* fields = st->fields(); | |
5390 | unsigned int field_index = 0; | |
5391 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5392 | pf != fields->end(); | |
5393 | ++pf, ++field_index) | |
5394 | { | |
f5165c05 | 5395 | if (Gogo::is_sink_name(pf->field_name())) |
5396 | continue; | |
5397 | ||
e9d3367e | 5398 | if (field_index > 0) |
5399 | { | |
5400 | if (left_temp == NULL) | |
5401 | left = left->copy(); | |
5402 | else | |
5403 | left = Expression::make_temporary_reference(left_temp, loc); | |
5404 | if (right_temp == NULL) | |
5405 | right = right->copy(); | |
5406 | else | |
5407 | right = Expression::make_temporary_reference(right_temp, loc); | |
5408 | } | |
5409 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5410 | loc); | |
5411 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5412 | loc); | |
5413 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5414 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5415 | } | |
5416 | ||
5417 | if (this->op_ == OPERATOR_NOTEQ) | |
5418 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5419 | ||
5420 | return ret; | |
5421 | } | |
5422 | ||
5423 | // Lower an array comparison. | |
5424 | ||
5425 | Expression* | |
5426 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5427 | Statement_inserter* inserter) | |
5428 | { | |
5429 | Array_type* at = this->left_->type()->array_type(); | |
5430 | Array_type* at2 = this->right_->type()->array_type(); | |
5431 | if (at2 == NULL) | |
5432 | return this; | |
5433 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5434 | return this; | |
5435 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5436 | this->right_->type(), NULL)) | |
5437 | return this; | |
5438 | ||
5439 | // Call memcmp directly if possible. This may let the middle-end | |
5440 | // optimize the call. | |
113ef6a5 | 5441 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5442 | return this->lower_compare_to_memcmp(gogo, inserter); |
5443 | ||
5444 | // Call the array comparison function. | |
5445 | Named_object* hash_fn; | |
5446 | Named_object* equal_fn; | |
5447 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5448 | &hash_fn, &equal_fn); | |
5449 | ||
5450 | Location loc = this->location(); | |
5451 | ||
5452 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5453 | ||
5454 | Expression_list* args = new Expression_list(); | |
5455 | args->push_back(this->operand_address(inserter, this->left_)); | |
5456 | args->push_back(this->operand_address(inserter, this->right_)); | |
e9d3367e | 5457 | |
5458 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5459 | ||
5460 | if (this->op_ == OPERATOR_NOTEQ) | |
5461 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5462 | ||
5463 | return ret; | |
5464 | } | |
5465 | ||
ceeb12d7 | 5466 | // Lower an interface to value comparison. |
5467 | ||
5468 | Expression* | |
5469 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5470 | Statement_inserter* inserter) | |
5471 | { | |
5472 | Type* left_type = this->left_->type(); | |
5473 | Type* right_type = this->right_->type(); | |
5474 | Interface_type* ift; | |
5475 | if (left_type->interface_type() != NULL) | |
5476 | { | |
5477 | ift = left_type->interface_type(); | |
5478 | if (!ift->implements_interface(right_type, NULL)) | |
5479 | return this; | |
5480 | } | |
5481 | else | |
5482 | { | |
5483 | ift = right_type->interface_type(); | |
5484 | if (!ift->implements_interface(left_type, NULL)) | |
5485 | return this; | |
5486 | } | |
5487 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5488 | return this; | |
5489 | ||
5490 | Location loc = this->location(); | |
5491 | ||
5492 | if (left_type->interface_type() == NULL | |
5493 | && left_type->points_to() == NULL | |
5494 | && !this->left_->is_addressable()) | |
5495 | { | |
5496 | Temporary_statement* temp = | |
5497 | Statement::make_temporary(left_type, NULL, loc); | |
5498 | inserter->insert(temp); | |
5499 | this->left_ = | |
5500 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5501 | } | |
5502 | ||
5503 | if (right_type->interface_type() == NULL | |
5504 | && right_type->points_to() == NULL | |
5505 | && !this->right_->is_addressable()) | |
5506 | { | |
5507 | Temporary_statement* temp = | |
5508 | Statement::make_temporary(right_type, NULL, loc); | |
5509 | inserter->insert(temp); | |
5510 | this->right_ = | |
5511 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5512 | } | |
5513 | ||
5514 | return this; | |
5515 | } | |
5516 | ||
e9d3367e | 5517 | // Lower a struct or array comparison to a call to memcmp. |
5518 | ||
5519 | Expression* | |
5520 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5521 | { | |
5522 | Location loc = this->location(); | |
5523 | ||
5524 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5525 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5526 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5527 | TYPE_INFO_SIZE); | |
5528 | ||
5529 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
e67508fa | 5530 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); |
e9d3367e | 5531 | return Expression::make_binary(this->op_, call, zero, loc); |
5532 | } | |
5533 | ||
a32698ee | 5534 | Expression* |
5c3f3470 | 5535 | Binary_expression::do_flatten(Gogo* gogo, Named_object*, |
a32698ee | 5536 | Statement_inserter* inserter) |
5537 | { | |
5538 | Location loc = this->location(); | |
5bf8be8b | 5539 | if (this->left_->type()->is_error_type() |
5540 | || this->right_->type()->is_error_type() | |
5541 | || this->left_->is_error_expression() | |
5542 | || this->right_->is_error_expression()) | |
5543 | { | |
5544 | go_assert(saw_errors()); | |
5545 | return Expression::make_error(loc); | |
5546 | } | |
5547 | ||
a32698ee | 5548 | Temporary_statement* temp; |
a32698ee | 5549 | |
5550 | Type* left_type = this->left_->type(); | |
5551 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5552 | || this->op_ == OPERATOR_RSHIFT); | |
5553 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5554 | left_type->integer_type() != NULL) | |
5555 | || this->op_ == OPERATOR_MOD); | |
5556 | ||
a32698ee | 5557 | if (is_shift_op |
5c3f3470 | 5558 | || (is_idiv_op |
5559 | && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))) | |
a32698ee | 5560 | { |
545ab43b | 5561 | if (!this->left_->is_variable() && !this->left_->is_constant()) |
a32698ee | 5562 | { |
5563 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5564 | inserter->insert(temp); | |
5565 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5566 | } | |
545ab43b | 5567 | if (!this->right_->is_variable() && !this->right_->is_constant()) |
a32698ee | 5568 | { |
5569 | temp = | |
5570 | Statement::make_temporary(NULL, this->right_, loc); | |
5571 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5572 | inserter->insert(temp); | |
5573 | } | |
5574 | } | |
5575 | return this; | |
5576 | } | |
5577 | ||
5578 | ||
e9d3367e | 5579 | // Return the address of EXPR, cast to unsafe.Pointer. |
5580 | ||
5581 | Expression* | |
5582 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5583 | Expression* expr) | |
5584 | { | |
5585 | Location loc = this->location(); | |
5586 | ||
5587 | if (!expr->is_addressable()) | |
5588 | { | |
5589 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5590 | loc); | |
5591 | inserter->insert(temp); | |
5592 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5593 | } | |
5594 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5595 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5596 | Type* void_type = Type::make_void_type(); | |
5597 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5598 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5599 | } | |
5600 | ||
0c77715b | 5601 | // Return the numeric constant value, if it has one. |
e440a328 | 5602 | |
5603 | bool | |
0c77715b | 5604 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5605 | { |
0c77715b | 5606 | Numeric_constant left_nc; |
5607 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5608 | return false; | |
5609 | Numeric_constant right_nc; | |
5610 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5611 | return false; | |
af7a5274 | 5612 | bool issued_error; |
9767e2d3 | 5613 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
af7a5274 | 5614 | this->location(), nc, &issued_error); |
e440a328 | 5615 | } |
5616 | ||
5617 | // Note that the value is being discarded. | |
5618 | ||
4f2138d7 | 5619 | bool |
e440a328 | 5620 | Binary_expression::do_discarding_value() |
5621 | { | |
5622 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5623 | return this->right_->discarding_value(); |
e440a328 | 5624 | else |
4f2138d7 | 5625 | { |
5626 | this->unused_value_error(); | |
5627 | return false; | |
5628 | } | |
e440a328 | 5629 | } |
5630 | ||
5631 | // Get type. | |
5632 | ||
5633 | Type* | |
5634 | Binary_expression::do_type() | |
5635 | { | |
5f5fea79 | 5636 | if (this->classification() == EXPRESSION_ERROR) |
5637 | return Type::make_error_type(); | |
5638 | ||
e440a328 | 5639 | switch (this->op_) |
5640 | { | |
e440a328 | 5641 | case OPERATOR_EQEQ: |
5642 | case OPERATOR_NOTEQ: | |
5643 | case OPERATOR_LT: | |
5644 | case OPERATOR_LE: | |
5645 | case OPERATOR_GT: | |
5646 | case OPERATOR_GE: | |
e90c9dfc | 5647 | if (this->type_ == NULL) |
5648 | this->type_ = Type::make_boolean_type(); | |
5649 | return this->type_; | |
e440a328 | 5650 | |
5651 | case OPERATOR_PLUS: | |
5652 | case OPERATOR_MINUS: | |
5653 | case OPERATOR_OR: | |
5654 | case OPERATOR_XOR: | |
5655 | case OPERATOR_MULT: | |
5656 | case OPERATOR_DIV: | |
5657 | case OPERATOR_MOD: | |
5658 | case OPERATOR_AND: | |
5659 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5660 | case OPERATOR_OROR: |
5661 | case OPERATOR_ANDAND: | |
e440a328 | 5662 | { |
0c77715b | 5663 | Type* type; |
5664 | if (!Binary_expression::operation_type(this->op_, | |
5665 | this->left_->type(), | |
5666 | this->right_->type(), | |
5667 | &type)) | |
5668 | return Type::make_error_type(); | |
5669 | return type; | |
e440a328 | 5670 | } |
5671 | ||
5672 | case OPERATOR_LSHIFT: | |
5673 | case OPERATOR_RSHIFT: | |
5674 | return this->left_->type(); | |
5675 | ||
5676 | default: | |
c3e6f413 | 5677 | go_unreachable(); |
e440a328 | 5678 | } |
5679 | } | |
5680 | ||
5681 | // Set type for a binary expression. | |
5682 | ||
5683 | void | |
5684 | Binary_expression::do_determine_type(const Type_context* context) | |
5685 | { | |
5686 | Type* tleft = this->left_->type(); | |
5687 | Type* tright = this->right_->type(); | |
5688 | ||
5689 | // Both sides should have the same type, except for the shift | |
5690 | // operations. For a comparison, we should ignore the incoming | |
5691 | // type. | |
5692 | ||
5693 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5694 | || this->op_ == OPERATOR_RSHIFT); | |
5695 | ||
5696 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5697 | || this->op_ == OPERATOR_NOTEQ | |
5698 | || this->op_ == OPERATOR_LT | |
5699 | || this->op_ == OPERATOR_LE | |
5700 | || this->op_ == OPERATOR_GT | |
5701 | || this->op_ == OPERATOR_GE); | |
5702 | ||
c999c2a7 | 5703 | // For constant expressions, the context of the result is not useful in |
5704 | // determining the types of the operands. It is only legal to use abstract | |
5705 | // boolean, numeric, and string constants as operands where it is legal to | |
5706 | // use non-abstract boolean, numeric, and string constants, respectively. | |
5707 | // Any issues with the operation will be resolved in the check_types pass. | |
5708 | bool is_constant_expr = (this->left_->is_constant() | |
5709 | && this->right_->is_constant()); | |
5710 | ||
e440a328 | 5711 | Type_context subcontext(*context); |
5712 | ||
9c4ff2ce | 5713 | if (is_constant_expr && !is_shift_op) |
af7a5274 | 5714 | { |
5715 | subcontext.type = NULL; | |
5716 | subcontext.may_be_abstract = true; | |
5717 | } | |
5718 | else if (is_comparison) | |
e440a328 | 5719 | { |
5720 | // In a comparison, the context does not determine the types of | |
5721 | // the operands. | |
5722 | subcontext.type = NULL; | |
5723 | } | |
5724 | ||
5725 | // Set the context for the left hand operand. | |
5726 | if (is_shift_op) | |
5727 | { | |
b40dc774 | 5728 | // The right hand operand of a shift plays no role in |
5729 | // determining the type of the left hand operand. | |
e440a328 | 5730 | } |
5731 | else if (!tleft->is_abstract()) | |
5732 | subcontext.type = tleft; | |
5733 | else if (!tright->is_abstract()) | |
5734 | subcontext.type = tright; | |
5735 | else if (subcontext.type == NULL) | |
5736 | { | |
5737 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5738 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5739 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5740 | { | |
5741 | // Both sides have an abstract integer, abstract float, or | |
5742 | // abstract complex type. Just let CONTEXT determine | |
5743 | // whether they may remain abstract or not. | |
5744 | } | |
5745 | else if (tleft->complex_type() != NULL) | |
5746 | subcontext.type = tleft; | |
5747 | else if (tright->complex_type() != NULL) | |
5748 | subcontext.type = tright; | |
5749 | else if (tleft->float_type() != NULL) | |
5750 | subcontext.type = tleft; | |
5751 | else if (tright->float_type() != NULL) | |
5752 | subcontext.type = tright; | |
5753 | else | |
5754 | subcontext.type = tleft; | |
f58a23ae | 5755 | |
5756 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5757 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5758 | } |
5759 | ||
af7a5274 | 5760 | this->left_->determine_type(&subcontext); |
e440a328 | 5761 | |
e440a328 | 5762 | if (is_shift_op) |
5763 | { | |
b40dc774 | 5764 | // We may have inherited an unusable type for the shift operand. |
5765 | // Give a useful error if that happened. | |
5766 | if (tleft->is_abstract() | |
5767 | && subcontext.type != NULL | |
8ab6effb | 5768 | && !subcontext.may_be_abstract |
f6bc81e6 | 5769 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5770 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5771 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5772 | "for left operand of shift")); |
b40dc774 | 5773 | |
5774 | // The context for the right hand operand is the same as for the | |
5775 | // left hand operand, except for a shift operator. | |
e440a328 | 5776 | subcontext.type = Type::lookup_integer_type("uint"); |
5777 | subcontext.may_be_abstract = false; | |
5778 | } | |
5779 | ||
af7a5274 | 5780 | this->right_->determine_type(&subcontext); |
e90c9dfc | 5781 | |
5782 | if (is_comparison) | |
5783 | { | |
5784 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5785 | ; | |
5786 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5787 | this->type_ = context->type; | |
5788 | else if (!context->may_be_abstract) | |
5789 | this->type_ = Type::lookup_bool_type(); | |
5790 | } | |
e440a328 | 5791 | } |
5792 | ||
5793 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5794 | // OTYPE is the type of the other operand. Return whether the |
5795 | // operation is OK. This should not be used for shift. | |
e440a328 | 5796 | |
5797 | bool | |
be8b5eee | 5798 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5799 | Location location) |
e440a328 | 5800 | { |
5801 | switch (op) | |
5802 | { | |
5803 | case OPERATOR_OROR: | |
5804 | case OPERATOR_ANDAND: | |
c999c2a7 | 5805 | if (!type->is_boolean_type() |
5806 | || !otype->is_boolean_type()) | |
e440a328 | 5807 | { |
631d5788 | 5808 | go_error_at(location, "expected boolean type"); |
e440a328 | 5809 | return false; |
5810 | } | |
5811 | break; | |
5812 | ||
5813 | case OPERATOR_EQEQ: | |
5814 | case OPERATOR_NOTEQ: | |
e9d3367e | 5815 | { |
5816 | std::string reason; | |
5817 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5818 | { | |
631d5788 | 5819 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5820 | return false; |
5821 | } | |
5822 | } | |
e440a328 | 5823 | break; |
5824 | ||
5825 | case OPERATOR_LT: | |
5826 | case OPERATOR_LE: | |
5827 | case OPERATOR_GT: | |
5828 | case OPERATOR_GE: | |
e9d3367e | 5829 | { |
5830 | std::string reason; | |
5831 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5832 | { | |
631d5788 | 5833 | go_error_at(location, "%s", reason.c_str()); |
e9d3367e | 5834 | return false; |
5835 | } | |
5836 | } | |
e440a328 | 5837 | break; |
5838 | ||
5839 | case OPERATOR_PLUS: | |
5840 | case OPERATOR_PLUSEQ: | |
c999c2a7 | 5841 | if ((!type->is_numeric_type() && !type->is_string_type()) |
5842 | || (!otype->is_numeric_type() && !otype->is_string_type())) | |
e440a328 | 5843 | { |
631d5788 | 5844 | go_error_at(location, |
e440a328 | 5845 | "expected integer, floating, complex, or string type"); |
5846 | return false; | |
5847 | } | |
5848 | break; | |
5849 | ||
5850 | case OPERATOR_MINUS: | |
5851 | case OPERATOR_MINUSEQ: | |
5852 | case OPERATOR_MULT: | |
5853 | case OPERATOR_MULTEQ: | |
5854 | case OPERATOR_DIV: | |
5855 | case OPERATOR_DIVEQ: | |
c999c2a7 | 5856 | if (!type->is_numeric_type() || !otype->is_numeric_type()) |
e440a328 | 5857 | { |
631d5788 | 5858 | go_error_at(location, "expected integer, floating, or complex type"); |
e440a328 | 5859 | return false; |
5860 | } | |
5861 | break; | |
5862 | ||
5863 | case OPERATOR_MOD: | |
5864 | case OPERATOR_MODEQ: | |
5865 | case OPERATOR_OR: | |
5866 | case OPERATOR_OREQ: | |
5867 | case OPERATOR_AND: | |
5868 | case OPERATOR_ANDEQ: | |
5869 | case OPERATOR_XOR: | |
5870 | case OPERATOR_XOREQ: | |
5871 | case OPERATOR_BITCLEAR: | |
5872 | case OPERATOR_BITCLEAREQ: | |
c999c2a7 | 5873 | if (type->integer_type() == NULL || otype->integer_type() == NULL) |
e440a328 | 5874 | { |
631d5788 | 5875 | go_error_at(location, "expected integer type"); |
e440a328 | 5876 | return false; |
5877 | } | |
5878 | break; | |
5879 | ||
5880 | default: | |
c3e6f413 | 5881 | go_unreachable(); |
e440a328 | 5882 | } |
5883 | ||
5884 | return true; | |
5885 | } | |
5886 | ||
5887 | // Check types. | |
5888 | ||
5889 | void | |
5890 | Binary_expression::do_check_types(Gogo*) | |
5891 | { | |
5f5fea79 | 5892 | if (this->classification() == EXPRESSION_ERROR) |
5893 | return; | |
5894 | ||
e440a328 | 5895 | Type* left_type = this->left_->type(); |
5896 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5897 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5898 | { |
5899 | this->set_is_error(); | |
5900 | return; | |
5901 | } | |
e440a328 | 5902 | |
5903 | if (this->op_ == OPERATOR_EQEQ | |
5904 | || this->op_ == OPERATOR_NOTEQ | |
5905 | || this->op_ == OPERATOR_LT | |
5906 | || this->op_ == OPERATOR_LE | |
5907 | || this->op_ == OPERATOR_GT | |
5908 | || this->op_ == OPERATOR_GE) | |
5909 | { | |
907c5ecd | 5910 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5911 | { | |
5912 | this->report_error(_("invalid comparison of nil with nil")); | |
5913 | return; | |
5914 | } | |
e440a328 | 5915 | if (!Type::are_assignable(left_type, right_type, NULL) |
5916 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5917 | { | |
5918 | this->report_error(_("incompatible types in binary expression")); | |
5919 | return; | |
5920 | } | |
5921 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5922 | right_type, |
e440a328 | 5923 | this->location()) |
5924 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5925 | left_type, |
e440a328 | 5926 | this->location())) |
5927 | { | |
5928 | this->set_is_error(); | |
5929 | return; | |
5930 | } | |
5931 | } | |
5932 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5933 | { | |
5934 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5935 | { | |
5936 | this->report_error(_("incompatible types in binary expression")); | |
5937 | return; | |
5938 | } | |
5939 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5940 | right_type, |
e440a328 | 5941 | this->location())) |
5942 | { | |
5943 | this->set_is_error(); | |
5944 | return; | |
5945 | } | |
5c65b19d | 5946 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5947 | { | |
5948 | // Division by a zero integer constant is an error. | |
5949 | Numeric_constant rconst; | |
5950 | unsigned long rval; | |
5951 | if (left_type->integer_type() != NULL | |
5952 | && this->right_->numeric_constant_value(&rconst) | |
5953 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5954 | && rval == 0) | |
5955 | { | |
5956 | this->report_error(_("integer division by zero")); | |
5957 | return; | |
5958 | } | |
5959 | } | |
e440a328 | 5960 | } |
5961 | else | |
5962 | { | |
5963 | if (left_type->integer_type() == NULL) | |
5964 | this->report_error(_("shift of non-integer operand")); | |
5965 | ||
6b5e0fac | 5966 | if (right_type->is_string_type()) |
5967 | this->report_error(_("shift count not unsigned integer")); | |
5968 | else if (!right_type->is_abstract() | |
e440a328 | 5969 | && (right_type->integer_type() == NULL |
5970 | || !right_type->integer_type()->is_unsigned())) | |
5971 | this->report_error(_("shift count not unsigned integer")); | |
5972 | else | |
5973 | { | |
0c77715b | 5974 | Numeric_constant nc; |
5975 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5976 | { |
0c77715b | 5977 | mpz_t val; |
5978 | if (!nc.to_int(&val)) | |
5979 | this->report_error(_("shift count not unsigned integer")); | |
5980 | else | |
a4eba91b | 5981 | { |
0c77715b | 5982 | if (mpz_sgn(val) < 0) |
5983 | { | |
5984 | this->report_error(_("negative shift count")); | |
0c77715b | 5985 | Location rloc = this->right_->location(); |
e67508fa | 5986 | this->right_ = Expression::make_integer_ul(0, right_type, |
5987 | rloc); | |
0c77715b | 5988 | } |
5989 | mpz_clear(val); | |
a4eba91b | 5990 | } |
e440a328 | 5991 | } |
e440a328 | 5992 | } |
5993 | } | |
5994 | } | |
5995 | ||
ea664253 | 5996 | // Get the backend representation for a binary expression. |
e440a328 | 5997 | |
ea664253 | 5998 | Bexpression* |
5999 | Binary_expression::do_get_backend(Translate_context* context) | |
e440a328 | 6000 | { |
1b1f2abf | 6001 | Gogo* gogo = context->gogo(); |
a32698ee | 6002 | Location loc = this->location(); |
6003 | Type* left_type = this->left_->type(); | |
6004 | Type* right_type = this->right_->type(); | |
1b1f2abf | 6005 | |
e440a328 | 6006 | bool use_left_type = true; |
6007 | bool is_shift_op = false; | |
29a2d1d8 | 6008 | bool is_idiv_op = false; |
e440a328 | 6009 | switch (this->op_) |
6010 | { | |
6011 | case OPERATOR_EQEQ: | |
6012 | case OPERATOR_NOTEQ: | |
6013 | case OPERATOR_LT: | |
6014 | case OPERATOR_LE: | |
6015 | case OPERATOR_GT: | |
6016 | case OPERATOR_GE: | |
ea664253 | 6017 | return Expression::comparison(context, this->type_, this->op_, |
6018 | this->left_, this->right_, loc); | |
e440a328 | 6019 | |
6020 | case OPERATOR_OROR: | |
e440a328 | 6021 | case OPERATOR_ANDAND: |
e440a328 | 6022 | use_left_type = false; |
6023 | break; | |
6024 | case OPERATOR_PLUS: | |
e440a328 | 6025 | case OPERATOR_MINUS: |
e440a328 | 6026 | case OPERATOR_OR: |
e440a328 | 6027 | case OPERATOR_XOR: |
e440a328 | 6028 | case OPERATOR_MULT: |
e440a328 | 6029 | break; |
6030 | case OPERATOR_DIV: | |
a32698ee | 6031 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
6032 | break; | |
729f8831 | 6033 | // Fall through. |
e440a328 | 6034 | case OPERATOR_MOD: |
29a2d1d8 | 6035 | is_idiv_op = true; |
e440a328 | 6036 | break; |
6037 | case OPERATOR_LSHIFT: | |
e440a328 | 6038 | case OPERATOR_RSHIFT: |
e440a328 | 6039 | is_shift_op = true; |
6040 | break; | |
e440a328 | 6041 | case OPERATOR_BITCLEAR: |
a32698ee | 6042 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
6043 | case OPERATOR_AND: | |
e440a328 | 6044 | break; |
6045 | default: | |
c3e6f413 | 6046 | go_unreachable(); |
e440a328 | 6047 | } |
6048 | ||
736a16ba | 6049 | // The only binary operation for string is +, and that should have |
6050 | // been converted to a String_concat_expression in do_lower. | |
6051 | go_assert(!left_type->is_string_type()); | |
a32698ee | 6052 | |
6053 | // For complex division Go might want slightly different results than the | |
6054 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 6055 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
6056 | { | |
a32698ee | 6057 | Runtime::Function complex_code; |
1850e20c | 6058 | switch (this->left_->type()->complex_type()->bits()) |
6059 | { | |
6060 | case 64: | |
a32698ee | 6061 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 6062 | break; |
6063 | case 128: | |
a32698ee | 6064 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 6065 | break; |
6066 | default: | |
6067 | go_unreachable(); | |
6068 | } | |
a32698ee | 6069 | Expression* complex_div = |
6070 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
ea664253 | 6071 | return complex_div->get_backend(context); |
1850e20c | 6072 | } |
6073 | ||
ea664253 | 6074 | Bexpression* left = this->left_->get_backend(context); |
6075 | Bexpression* right = this->right_->get_backend(context); | |
e440a328 | 6076 | |
a32698ee | 6077 | Type* type = use_left_type ? left_type : right_type; |
6078 | Btype* btype = type->get_backend(gogo); | |
6079 | ||
6080 | Bexpression* ret = | |
6081 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
6082 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 6083 | |
a32698ee | 6084 | // Initialize overflow constants. |
6085 | Bexpression* overflow; | |
6086 | mpz_t zero; | |
6087 | mpz_init_set_ui(zero, 0UL); | |
6088 | mpz_t one; | |
6089 | mpz_init_set_ui(one, 1UL); | |
6090 | mpz_t neg_one; | |
6091 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 6092 | |
a32698ee | 6093 | Btype* left_btype = left_type->get_backend(gogo); |
6094 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 6095 | |
6096 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 6097 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 6098 | if (is_shift_op) |
6099 | { | |
a32698ee | 6100 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 6101 | |
a32698ee | 6102 | int bits = left_type->integer_type()->bits(); |
a7c5b619 | 6103 | |
6104 | Numeric_constant nc; | |
6105 | unsigned long ul; | |
6106 | if (!this->right_->numeric_constant_value(&nc) | |
6107 | || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID | |
6108 | || ul >= static_cast<unsigned long>(bits)) | |
e440a328 | 6109 | { |
a7c5b619 | 6110 | mpz_t bitsval; |
6111 | mpz_init_set_ui(bitsval, bits); | |
6112 | Bexpression* bits_expr = | |
6113 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
6114 | Bexpression* compare = | |
6115 | gogo->backend()->binary_expression(OPERATOR_LT, | |
6116 | right, bits_expr, loc); | |
6117 | ||
6118 | Bexpression* zero_expr = | |
6119 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
6120 | overflow = zero_expr; | |
6121 | Bfunction* bfn = context->function()->func_value()->get_decl(); | |
6122 | if (this->op_ == OPERATOR_RSHIFT | |
6123 | && !left_type->integer_type()->is_unsigned()) | |
6124 | { | |
6125 | Bexpression* neg_expr = | |
6126 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
6127 | zero_expr, loc); | |
6128 | Bexpression* neg_one_expr = | |
6129 | gogo->backend()->integer_constant_expression(left_btype, | |
6130 | neg_one); | |
6131 | overflow = gogo->backend()->conditional_expression(bfn, | |
6132 | btype, | |
6133 | neg_expr, | |
6134 | neg_one_expr, | |
6135 | zero_expr, | |
6136 | loc); | |
6137 | } | |
6138 | ret = gogo->backend()->conditional_expression(bfn, btype, compare, | |
6139 | ret, overflow, loc); | |
6140 | mpz_clear(bitsval); | |
29a2d1d8 | 6141 | } |
29a2d1d8 | 6142 | } |
6143 | ||
6144 | // Add checks for division by zero and division overflow as needed. | |
6145 | if (is_idiv_op) | |
6146 | { | |
5c3f3470 | 6147 | if (gogo->check_divide_by_zero()) |
29a2d1d8 | 6148 | { |
6149 | // right == 0 | |
a32698ee | 6150 | Bexpression* zero_expr = |
6151 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
6152 | Bexpression* check = | |
6153 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6154 | right, zero_expr, loc); | |
29a2d1d8 | 6155 | |
a32698ee | 6156 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 6157 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
ea664253 | 6158 | Bexpression* crash = gogo->runtime_error(errcode, |
6159 | loc)->get_backend(context); | |
29a2d1d8 | 6160 | |
6161 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
93715b75 | 6162 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
6163 | ret = gogo->backend()->conditional_expression(bfn, btype, | |
6164 | check, crash, | |
ea664253 | 6165 | ret, loc); |
b13c66cd | 6166 | } |
6167 | ||
5c3f3470 | 6168 | if (gogo->check_divide_overflow()) |
29a2d1d8 | 6169 | { |
6170 | // right == -1 | |
6171 | // FIXME: It would be nice to say that this test is expected | |
6172 | // to return false. | |
a32698ee | 6173 | |
6174 | Bexpression* neg_one_expr = | |
6175 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6176 | Bexpression* check = | |
6177 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6178 | right, neg_one_expr, loc); | |
6179 | ||
6180 | Bexpression* zero_expr = | |
6181 | gogo->backend()->integer_constant_expression(btype, zero); | |
6182 | Bexpression* one_expr = | |
6183 | gogo->backend()->integer_constant_expression(btype, one); | |
93715b75 | 6184 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
a32698ee | 6185 | |
6186 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6187 | { |
6188 | // An unsigned -1 is the largest possible number, so | |
6189 | // dividing is always 1 or 0. | |
a32698ee | 6190 | |
6191 | Bexpression* cmp = | |
6192 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6193 | left, right, loc); | |
29a2d1d8 | 6194 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6195 | overflow = |
93715b75 | 6196 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6197 | one_expr, zero_expr, |
6198 | loc); | |
29a2d1d8 | 6199 | else |
a32698ee | 6200 | overflow = |
93715b75 | 6201 | gogo->backend()->conditional_expression(bfn, btype, cmp, |
a32698ee | 6202 | zero_expr, left, |
6203 | loc); | |
29a2d1d8 | 6204 | } |
6205 | else | |
6206 | { | |
6207 | // Computing left / -1 is the same as computing - left, | |
6208 | // which does not overflow since Go sets -fwrapv. | |
6209 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6210 | { |
6211 | Expression* negate_expr = | |
6212 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
ea664253 | 6213 | overflow = negate_expr->get_backend(context); |
a32698ee | 6214 | } |
29a2d1d8 | 6215 | else |
a32698ee | 6216 | overflow = zero_expr; |
29a2d1d8 | 6217 | } |
a32698ee | 6218 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6219 | |
6220 | // right == -1 ? - left : ret | |
93715b75 | 6221 | ret = gogo->backend()->conditional_expression(bfn, btype, |
6222 | check, overflow, | |
a32698ee | 6223 | ret, loc); |
29a2d1d8 | 6224 | } |
e440a328 | 6225 | } |
6226 | ||
a32698ee | 6227 | mpz_clear(zero); |
6228 | mpz_clear(one); | |
6229 | mpz_clear(neg_one); | |
ea664253 | 6230 | return ret; |
e440a328 | 6231 | } |
6232 | ||
6233 | // Export a binary expression. | |
6234 | ||
6235 | void | |
6236 | Binary_expression::do_export(Export* exp) const | |
6237 | { | |
6238 | exp->write_c_string("("); | |
6239 | this->left_->export_expression(exp); | |
6240 | switch (this->op_) | |
6241 | { | |
6242 | case OPERATOR_OROR: | |
6243 | exp->write_c_string(" || "); | |
6244 | break; | |
6245 | case OPERATOR_ANDAND: | |
6246 | exp->write_c_string(" && "); | |
6247 | break; | |
6248 | case OPERATOR_EQEQ: | |
6249 | exp->write_c_string(" == "); | |
6250 | break; | |
6251 | case OPERATOR_NOTEQ: | |
6252 | exp->write_c_string(" != "); | |
6253 | break; | |
6254 | case OPERATOR_LT: | |
6255 | exp->write_c_string(" < "); | |
6256 | break; | |
6257 | case OPERATOR_LE: | |
6258 | exp->write_c_string(" <= "); | |
6259 | break; | |
6260 | case OPERATOR_GT: | |
6261 | exp->write_c_string(" > "); | |
6262 | break; | |
6263 | case OPERATOR_GE: | |
6264 | exp->write_c_string(" >= "); | |
6265 | break; | |
6266 | case OPERATOR_PLUS: | |
6267 | exp->write_c_string(" + "); | |
6268 | break; | |
6269 | case OPERATOR_MINUS: | |
6270 | exp->write_c_string(" - "); | |
6271 | break; | |
6272 | case OPERATOR_OR: | |
6273 | exp->write_c_string(" | "); | |
6274 | break; | |
6275 | case OPERATOR_XOR: | |
6276 | exp->write_c_string(" ^ "); | |
6277 | break; | |
6278 | case OPERATOR_MULT: | |
6279 | exp->write_c_string(" * "); | |
6280 | break; | |
6281 | case OPERATOR_DIV: | |
6282 | exp->write_c_string(" / "); | |
6283 | break; | |
6284 | case OPERATOR_MOD: | |
6285 | exp->write_c_string(" % "); | |
6286 | break; | |
6287 | case OPERATOR_LSHIFT: | |
6288 | exp->write_c_string(" << "); | |
6289 | break; | |
6290 | case OPERATOR_RSHIFT: | |
6291 | exp->write_c_string(" >> "); | |
6292 | break; | |
6293 | case OPERATOR_AND: | |
6294 | exp->write_c_string(" & "); | |
6295 | break; | |
6296 | case OPERATOR_BITCLEAR: | |
6297 | exp->write_c_string(" &^ "); | |
6298 | break; | |
6299 | default: | |
c3e6f413 | 6300 | go_unreachable(); |
e440a328 | 6301 | } |
6302 | this->right_->export_expression(exp); | |
6303 | exp->write_c_string(")"); | |
6304 | } | |
6305 | ||
6306 | // Import a binary expression. | |
6307 | ||
6308 | Expression* | |
6309 | Binary_expression::do_import(Import* imp) | |
6310 | { | |
6311 | imp->require_c_string("("); | |
6312 | ||
6313 | Expression* left = Expression::import_expression(imp); | |
6314 | ||
6315 | Operator op; | |
6316 | if (imp->match_c_string(" || ")) | |
6317 | { | |
6318 | op = OPERATOR_OROR; | |
6319 | imp->advance(4); | |
6320 | } | |
6321 | else if (imp->match_c_string(" && ")) | |
6322 | { | |
6323 | op = OPERATOR_ANDAND; | |
6324 | imp->advance(4); | |
6325 | } | |
6326 | else if (imp->match_c_string(" == ")) | |
6327 | { | |
6328 | op = OPERATOR_EQEQ; | |
6329 | imp->advance(4); | |
6330 | } | |
6331 | else if (imp->match_c_string(" != ")) | |
6332 | { | |
6333 | op = OPERATOR_NOTEQ; | |
6334 | imp->advance(4); | |
6335 | } | |
6336 | else if (imp->match_c_string(" < ")) | |
6337 | { | |
6338 | op = OPERATOR_LT; | |
6339 | imp->advance(3); | |
6340 | } | |
6341 | else if (imp->match_c_string(" <= ")) | |
6342 | { | |
6343 | op = OPERATOR_LE; | |
6344 | imp->advance(4); | |
6345 | } | |
6346 | else if (imp->match_c_string(" > ")) | |
6347 | { | |
6348 | op = OPERATOR_GT; | |
6349 | imp->advance(3); | |
6350 | } | |
6351 | else if (imp->match_c_string(" >= ")) | |
6352 | { | |
6353 | op = OPERATOR_GE; | |
6354 | imp->advance(4); | |
6355 | } | |
6356 | else if (imp->match_c_string(" + ")) | |
6357 | { | |
6358 | op = OPERATOR_PLUS; | |
6359 | imp->advance(3); | |
6360 | } | |
6361 | else if (imp->match_c_string(" - ")) | |
6362 | { | |
6363 | op = OPERATOR_MINUS; | |
6364 | imp->advance(3); | |
6365 | } | |
6366 | else if (imp->match_c_string(" | ")) | |
6367 | { | |
6368 | op = OPERATOR_OR; | |
6369 | imp->advance(3); | |
6370 | } | |
6371 | else if (imp->match_c_string(" ^ ")) | |
6372 | { | |
6373 | op = OPERATOR_XOR; | |
6374 | imp->advance(3); | |
6375 | } | |
6376 | else if (imp->match_c_string(" * ")) | |
6377 | { | |
6378 | op = OPERATOR_MULT; | |
6379 | imp->advance(3); | |
6380 | } | |
6381 | else if (imp->match_c_string(" / ")) | |
6382 | { | |
6383 | op = OPERATOR_DIV; | |
6384 | imp->advance(3); | |
6385 | } | |
6386 | else if (imp->match_c_string(" % ")) | |
6387 | { | |
6388 | op = OPERATOR_MOD; | |
6389 | imp->advance(3); | |
6390 | } | |
6391 | else if (imp->match_c_string(" << ")) | |
6392 | { | |
6393 | op = OPERATOR_LSHIFT; | |
6394 | imp->advance(4); | |
6395 | } | |
6396 | else if (imp->match_c_string(" >> ")) | |
6397 | { | |
6398 | op = OPERATOR_RSHIFT; | |
6399 | imp->advance(4); | |
6400 | } | |
6401 | else if (imp->match_c_string(" & ")) | |
6402 | { | |
6403 | op = OPERATOR_AND; | |
6404 | imp->advance(3); | |
6405 | } | |
6406 | else if (imp->match_c_string(" &^ ")) | |
6407 | { | |
6408 | op = OPERATOR_BITCLEAR; | |
6409 | imp->advance(4); | |
6410 | } | |
6411 | else | |
6412 | { | |
631d5788 | 6413 | go_error_at(imp->location(), "unrecognized binary operator"); |
e440a328 | 6414 | return Expression::make_error(imp->location()); |
6415 | } | |
6416 | ||
6417 | Expression* right = Expression::import_expression(imp); | |
6418 | ||
6419 | imp->require_c_string(")"); | |
6420 | ||
6421 | return Expression::make_binary(op, left, right, imp->location()); | |
6422 | } | |
6423 | ||
d751bb78 | 6424 | // Dump ast representation of a binary expression. |
6425 | ||
6426 | void | |
6427 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6428 | { | |
6429 | ast_dump_context->ostream() << "("; | |
6430 | ast_dump_context->dump_expression(this->left_); | |
6431 | ast_dump_context->ostream() << " "; | |
6432 | ast_dump_context->dump_operator(this->op_); | |
6433 | ast_dump_context->ostream() << " "; | |
6434 | ast_dump_context->dump_expression(this->right_); | |
6435 | ast_dump_context->ostream() << ") "; | |
6436 | } | |
6437 | ||
e440a328 | 6438 | // Make a binary expression. |
6439 | ||
6440 | Expression* | |
6441 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6442 | Location location) |
e440a328 | 6443 | { |
6444 | return new Binary_expression(op, left, right, location); | |
6445 | } | |
6446 | ||
6447 | // Implement a comparison. | |
6448 | ||
a32698ee | 6449 | Bexpression* |
6450 | Expression::comparison(Translate_context* context, Type* result_type, | |
6451 | Operator op, Expression* left, Expression* right, | |
6452 | Location location) | |
e440a328 | 6453 | { |
2387f644 | 6454 | Type* left_type = left->type(); |
6455 | Type* right_type = right->type(); | |
ceeb12d7 | 6456 | |
e67508fa | 6457 | Expression* zexpr = Expression::make_integer_ul(0, NULL, location); |
1b1f2abf | 6458 | |
15c67ee2 | 6459 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6460 | { |
6098d6cb | 6461 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
6462 | { | |
6463 | left = Runtime::make_call(Runtime::EQSTRING, location, 2, | |
6464 | left, right); | |
6465 | right = Expression::make_boolean(true, location); | |
6466 | } | |
6467 | else | |
6468 | { | |
6469 | left = Runtime::make_call(Runtime::CMPSTRING, location, 2, | |
6470 | left, right); | |
6471 | right = zexpr; | |
6472 | } | |
e440a328 | 6473 | } |
15c67ee2 | 6474 | else if ((left_type->interface_type() != NULL |
6475 | && right_type->interface_type() == NULL | |
6476 | && !right_type->is_nil_type()) | |
6477 | || (left_type->interface_type() == NULL | |
6478 | && !left_type->is_nil_type() | |
6479 | && right_type->interface_type() != NULL)) | |
e440a328 | 6480 | { |
6481 | // Comparing an interface value to a non-interface value. | |
6482 | if (left_type->interface_type() == NULL) | |
6483 | { | |
6484 | std::swap(left_type, right_type); | |
2387f644 | 6485 | std::swap(left, right); |
e440a328 | 6486 | } |
6487 | ||
6488 | // The right operand is not an interface. We need to take its | |
6489 | // address if it is not a pointer. | |
ceeb12d7 | 6490 | Expression* pointer_arg = NULL; |
e440a328 | 6491 | if (right_type->points_to() != NULL) |
2387f644 | 6492 | pointer_arg = right; |
e440a328 | 6493 | else |
6494 | { | |
2387f644 | 6495 | go_assert(right->is_addressable()); |
6496 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6497 | location); |
e440a328 | 6498 | } |
e440a328 | 6499 | |
2387f644 | 6500 | Expression* descriptor = |
6501 | Expression::make_type_descriptor(right_type, location); | |
6502 | left = | |
ceeb12d7 | 6503 | Runtime::make_call((left_type->interface_type()->is_empty() |
6098d6cb | 6504 | ? Runtime::EFACEVALEQ |
6505 | : Runtime::IFACEVALEQ), | |
2387f644 | 6506 | location, 3, left, descriptor, |
ceeb12d7 | 6507 | pointer_arg); |
6098d6cb | 6508 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6509 | right = Expression::make_boolean(true, location); | |
e440a328 | 6510 | } |
6511 | else if (left_type->interface_type() != NULL | |
6512 | && right_type->interface_type() != NULL) | |
6513 | { | |
ceeb12d7 | 6514 | Runtime::Function compare_function; |
739bad04 | 6515 | if (left_type->interface_type()->is_empty() |
6516 | && right_type->interface_type()->is_empty()) | |
6098d6cb | 6517 | compare_function = Runtime::EFACEEQ; |
739bad04 | 6518 | else if (!left_type->interface_type()->is_empty() |
6519 | && !right_type->interface_type()->is_empty()) | |
6098d6cb | 6520 | compare_function = Runtime::IFACEEQ; |
739bad04 | 6521 | else |
6522 | { | |
6523 | if (left_type->interface_type()->is_empty()) | |
6524 | { | |
739bad04 | 6525 | std::swap(left_type, right_type); |
2387f644 | 6526 | std::swap(left, right); |
739bad04 | 6527 | } |
c484d925 | 6528 | go_assert(!left_type->interface_type()->is_empty()); |
6529 | go_assert(right_type->interface_type()->is_empty()); | |
6098d6cb | 6530 | compare_function = Runtime::IFACEEFACEEQ; |
739bad04 | 6531 | } |
6532 | ||
2387f644 | 6533 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6098d6cb | 6534 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
6535 | right = Expression::make_boolean(true, location); | |
e440a328 | 6536 | } |
6537 | ||
6538 | if (left_type->is_nil_type() | |
6539 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6540 | { | |
6541 | std::swap(left_type, right_type); | |
2387f644 | 6542 | std::swap(left, right); |
e440a328 | 6543 | } |
6544 | ||
6545 | if (right_type->is_nil_type()) | |
6546 | { | |
2387f644 | 6547 | right = Expression::make_nil(location); |
e440a328 | 6548 | if (left_type->array_type() != NULL |
6549 | && left_type->array_type()->length() == NULL) | |
6550 | { | |
6551 | Array_type* at = left_type->array_type(); | |
44dbe1d7 | 6552 | bool is_lvalue = false; |
6553 | left = at->get_value_pointer(context->gogo(), left, is_lvalue); | |
e440a328 | 6554 | } |
6555 | else if (left_type->interface_type() != NULL) | |
6556 | { | |
6557 | // An interface is nil if the first field is nil. | |
2387f644 | 6558 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6559 | } |
6560 | } | |
6561 | ||
ea664253 | 6562 | Bexpression* left_bexpr = left->get_backend(context); |
6563 | Bexpression* right_bexpr = right->get_backend(context); | |
e90c9dfc | 6564 | |
a32698ee | 6565 | Gogo* gogo = context->gogo(); |
6566 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6567 | right_bexpr, location); | |
6568 | if (result_type != NULL) | |
6569 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6570 | ret, location); | |
e440a328 | 6571 | return ret; |
6572 | } | |
6573 | ||
736a16ba | 6574 | // Class String_concat_expression. |
6575 | ||
6576 | bool | |
6577 | String_concat_expression::do_is_constant() const | |
6578 | { | |
6579 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6580 | pe != this->exprs_->end(); | |
6581 | ++pe) | |
6582 | { | |
6583 | if (!(*pe)->is_constant()) | |
6584 | return false; | |
6585 | } | |
6586 | return true; | |
6587 | } | |
6588 | ||
6589 | bool | |
3ae06f68 | 6590 | String_concat_expression::do_is_static_initializer() const |
736a16ba | 6591 | { |
6592 | for (Expression_list::const_iterator pe = this->exprs_->begin(); | |
6593 | pe != this->exprs_->end(); | |
6594 | ++pe) | |
6595 | { | |
3ae06f68 | 6596 | if (!(*pe)->is_static_initializer()) |
736a16ba | 6597 | return false; |
6598 | } | |
6599 | return true; | |
6600 | } | |
6601 | ||
6602 | Type* | |
6603 | String_concat_expression::do_type() | |
6604 | { | |
6605 | Type* t = this->exprs_->front()->type(); | |
6606 | Expression_list::iterator pe = this->exprs_->begin(); | |
6607 | ++pe; | |
6608 | for (; pe != this->exprs_->end(); ++pe) | |
6609 | { | |
6610 | Type* t1; | |
6611 | if (!Binary_expression::operation_type(OPERATOR_PLUS, t, | |
6612 | (*pe)->type(), | |
6613 | &t1)) | |
6614 | return Type::make_error_type(); | |
6615 | t = t1; | |
6616 | } | |
6617 | return t; | |
6618 | } | |
6619 | ||
6620 | void | |
6621 | String_concat_expression::do_determine_type(const Type_context* context) | |
6622 | { | |
6623 | Type_context subcontext(*context); | |
6624 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6625 | pe != this->exprs_->end(); | |
6626 | ++pe) | |
6627 | { | |
6628 | Type* t = (*pe)->type(); | |
6629 | if (!t->is_abstract()) | |
6630 | { | |
6631 | subcontext.type = t; | |
6632 | break; | |
6633 | } | |
6634 | } | |
6635 | if (subcontext.type == NULL) | |
6636 | subcontext.type = this->exprs_->front()->type(); | |
6637 | for (Expression_list::iterator pe = this->exprs_->begin(); | |
6638 | pe != this->exprs_->end(); | |
6639 | ++pe) | |
6640 | (*pe)->determine_type(&subcontext); | |
6641 | } | |
6642 | ||
6643 | void | |
6644 | String_concat_expression::do_check_types(Gogo*) | |
6645 | { | |
6646 | if (this->is_error_expression()) | |
6647 | return; | |
6648 | Type* t = this->exprs_->front()->type(); | |
6649 | if (t->is_error()) | |
6650 | { | |
6651 | this->set_is_error(); | |
6652 | return; | |
6653 | } | |
6654 | Expression_list::iterator pe = this->exprs_->begin(); | |
6655 | ++pe; | |
6656 | for (; pe != this->exprs_->end(); ++pe) | |
6657 | { | |
6658 | Type* t1 = (*pe)->type(); | |
6659 | if (!Type::are_compatible_for_binop(t, t1)) | |
6660 | { | |
6661 | this->report_error("incompatible types in binary expression"); | |
6662 | return; | |
6663 | } | |
6664 | if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1, | |
6665 | this->location())) | |
6666 | { | |
6667 | this->set_is_error(); | |
6668 | return; | |
6669 | } | |
6670 | } | |
6671 | } | |
6672 | ||
6673 | Expression* | |
6674 | String_concat_expression::do_flatten(Gogo*, Named_object*, | |
6675 | Statement_inserter*) | |
6676 | { | |
6677 | if (this->is_error_expression()) | |
6678 | return this; | |
6679 | Location loc = this->location(); | |
6680 | Type* type = this->type(); | |
6681 | Expression* nil_arg = Expression::make_nil(loc); | |
6682 | Expression* call; | |
6683 | switch (this->exprs_->size()) | |
6684 | { | |
6685 | case 0: case 1: | |
6686 | go_unreachable(); | |
6687 | ||
6688 | case 2: case 3: case 4: case 5: | |
6689 | { | |
6690 | Expression* len = Expression::make_integer_ul(this->exprs_->size(), | |
6691 | NULL, loc); | |
6692 | Array_type* arg_type = Type::make_array_type(type, len); | |
6693 | arg_type->set_is_array_incomparable(); | |
6694 | Expression* arg = | |
6695 | Expression::make_array_composite_literal(arg_type, this->exprs_, | |
6696 | loc); | |
6697 | Runtime::Function code; | |
6698 | switch (this->exprs_->size()) | |
6699 | { | |
6700 | default: | |
6701 | go_unreachable(); | |
6702 | case 2: | |
6703 | code = Runtime::CONCATSTRING2; | |
6704 | break; | |
6705 | case 3: | |
6706 | code = Runtime::CONCATSTRING3; | |
6707 | break; | |
6708 | case 4: | |
6709 | code = Runtime::CONCATSTRING4; | |
6710 | break; | |
6711 | case 5: | |
6712 | code = Runtime::CONCATSTRING5; | |
6713 | break; | |
6714 | } | |
6715 | call = Runtime::make_call(code, loc, 2, nil_arg, arg); | |
6716 | } | |
6717 | break; | |
6718 | ||
6719 | default: | |
6720 | { | |
6721 | Type* arg_type = Type::make_array_type(type, NULL); | |
6722 | Slice_construction_expression* sce = | |
6723 | Expression::make_slice_composite_literal(arg_type, this->exprs_, | |
6724 | loc); | |
6725 | sce->set_storage_does_not_escape(); | |
6726 | call = Runtime::make_call(Runtime::CONCATSTRINGS, loc, 2, nil_arg, | |
6727 | sce); | |
6728 | } | |
6729 | break; | |
6730 | } | |
6731 | ||
6732 | return Expression::make_cast(type, call, loc); | |
6733 | } | |
6734 | ||
6735 | void | |
6736 | String_concat_expression::do_dump_expression( | |
6737 | Ast_dump_context* ast_dump_context) const | |
6738 | { | |
6739 | ast_dump_context->ostream() << "concat("; | |
6740 | ast_dump_context->dump_expression_list(this->exprs_, false); | |
6741 | ast_dump_context->ostream() << ")"; | |
6742 | } | |
6743 | ||
6744 | Expression* | |
6745 | Expression::make_string_concat(Expression_list* exprs) | |
6746 | { | |
6747 | return new String_concat_expression(exprs); | |
6748 | } | |
6749 | ||
e440a328 | 6750 | // Class Bound_method_expression. |
6751 | ||
6752 | // Traversal. | |
6753 | ||
6754 | int | |
6755 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6756 | { | |
e0659c9e | 6757 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6758 | } |
6759 | ||
6760 | // Return the type of a bound method expression. The type of this | |
0afbb937 | 6761 | // object is simply the type of the method with no receiver. |
e440a328 | 6762 | |
6763 | Type* | |
6764 | Bound_method_expression::do_type() | |
6765 | { | |
0afbb937 | 6766 | Named_object* fn = this->method_->named_object(); |
6767 | Function_type* fntype; | |
6768 | if (fn->is_function()) | |
6769 | fntype = fn->func_value()->type(); | |
6770 | else if (fn->is_function_declaration()) | |
6771 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6772 | else |
6773 | return Type::make_error_type(); | |
0afbb937 | 6774 | return fntype->copy_without_receiver(); |
e440a328 | 6775 | } |
6776 | ||
6777 | // Determine the types of a method expression. | |
6778 | ||
6779 | void | |
6780 | Bound_method_expression::do_determine_type(const Type_context*) | |
6781 | { | |
0afbb937 | 6782 | Named_object* fn = this->method_->named_object(); |
6783 | Function_type* fntype; | |
6784 | if (fn->is_function()) | |
6785 | fntype = fn->func_value()->type(); | |
6786 | else if (fn->is_function_declaration()) | |
6787 | fntype = fn->func_declaration_value()->type(); | |
6788 | else | |
6789 | fntype = NULL; | |
e440a328 | 6790 | if (fntype == NULL || !fntype->is_method()) |
6791 | this->expr_->determine_type_no_context(); | |
6792 | else | |
6793 | { | |
6794 | Type_context subcontext(fntype->receiver()->type(), false); | |
6795 | this->expr_->determine_type(&subcontext); | |
6796 | } | |
6797 | } | |
6798 | ||
6799 | // Check the types of a method expression. | |
6800 | ||
6801 | void | |
6802 | Bound_method_expression::do_check_types(Gogo*) | |
6803 | { | |
0afbb937 | 6804 | Named_object* fn = this->method_->named_object(); |
6805 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6806 | { | |
6807 | this->report_error(_("object is not a method")); | |
6808 | return; | |
6809 | } | |
6810 | ||
6811 | Function_type* fntype; | |
6812 | if (fn->is_function()) | |
6813 | fntype = fn->func_value()->type(); | |
6814 | else if (fn->is_function_declaration()) | |
6815 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6816 | else |
0afbb937 | 6817 | go_unreachable(); |
6818 | Type* rtype = fntype->receiver()->type()->deref(); | |
6819 | Type* etype = (this->expr_type_ != NULL | |
6820 | ? this->expr_type_ | |
6821 | : this->expr_->type()); | |
6822 | etype = etype->deref(); | |
6823 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6824 | this->report_error(_("method type does not match object type")); | |
6825 | } | |
6826 | ||
6827 | // If a bound method expression is not simply called, then it is | |
6828 | // represented as a closure. The closure will hold a single variable, | |
6829 | // the receiver to pass to the method. The function will be a simple | |
6830 | // thunk that pulls that value from the closure and calls the method | |
6831 | // with the remaining arguments. | |
6832 | // | |
6833 | // Because method values are not common, we don't build all thunks for | |
6834 | // every methods, but instead only build them as we need them. In | |
6835 | // particular, we even build them on demand for methods defined in | |
6836 | // other packages. | |
6837 | ||
6838 | Bound_method_expression::Method_value_thunks | |
6839 | Bound_method_expression::method_value_thunks; | |
6840 | ||
6841 | // Find or create the thunk for METHOD. | |
6842 | ||
6843 | Named_object* | |
6844 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6845 | Named_object* fn) | |
6846 | { | |
6847 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6848 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6849 | Bound_method_expression::method_value_thunks.insert(val); | |
6850 | if (!ins.second) | |
6851 | { | |
6852 | // We have seen this method before. | |
6853 | go_assert(ins.first->second != NULL); | |
6854 | return ins.first->second; | |
6855 | } | |
6856 | ||
6857 | Location loc = fn->location(); | |
6858 | ||
6859 | Function_type* orig_fntype; | |
6860 | if (fn->is_function()) | |
6861 | orig_fntype = fn->func_value()->type(); | |
6862 | else if (fn->is_function_declaration()) | |
6863 | orig_fntype = fn->func_declaration_value()->type(); | |
6864 | else | |
6865 | orig_fntype = NULL; | |
6866 | ||
6867 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6868 | { |
13f2fdb8 | 6869 | ins.first->second = |
6870 | Named_object::make_erroneous_name(gogo->thunk_name()); | |
0afbb937 | 6871 | return ins.first->second; |
e440a328 | 6872 | } |
0afbb937 | 6873 | |
6874 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6875 | // The type here is wrong--it should be the C function type. But it |
6876 | // doesn't really matter. | |
0afbb937 | 6877 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
13f2fdb8 | 6878 | sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc))); |
6879 | sfl->push_back(Struct_field(Typed_identifier("val", | |
0afbb937 | 6880 | orig_fntype->receiver()->type(), |
6881 | loc))); | |
6bf4793c | 6882 | Struct_type* st = Type::make_struct_type(sfl, loc); |
6883 | st->set_is_struct_incomparable(); | |
6884 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 6885 | |
f8bdf81a | 6886 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6887 | |
13f2fdb8 | 6888 | std::string thunk_name = gogo->thunk_name(); |
da244e59 | 6889 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, |
0afbb937 | 6890 | false, loc); |
6891 | ||
f8bdf81a | 6892 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6893 | cvar->set_is_used(); | |
1ecc6157 | 6894 | cvar->set_is_closure(); |
da244e59 | 6895 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
6896 | NULL, cvar); | |
f8bdf81a | 6897 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 6898 | |
f8bdf81a | 6899 | gogo->start_block(loc); |
0afbb937 | 6900 | |
6901 | // Field 0 of the closure is the function code pointer, field 1 is | |
6902 | // the value on which to invoke the method. | |
6903 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 6904 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 6905 | arg = Expression::make_field_reference(arg, 1, loc); |
6906 | ||
6907 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6908 | ||
6909 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6910 | Expression_list* args; | |
6911 | if (orig_params == NULL || orig_params->empty()) | |
6912 | args = NULL; | |
6913 | else | |
6914 | { | |
6915 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6916 | args = new Expression_list(); | |
6917 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6918 | p != new_params->end(); |
0afbb937 | 6919 | ++p) |
6920 | { | |
6921 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6922 | go_assert(p_no != NULL | |
6923 | && p_no->is_variable() | |
6924 | && p_no->var_value()->is_parameter()); | |
6925 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6926 | } | |
6927 | } | |
6928 | ||
6929 | Call_expression* call = Expression::make_call(bme, args, | |
6930 | orig_fntype->is_varargs(), | |
6931 | loc); | |
6932 | call->set_varargs_are_lowered(); | |
6933 | ||
6934 | Statement* s = Statement::make_return_from_call(call, loc); | |
6935 | gogo->add_statement(s); | |
6936 | Block* b = gogo->finish_block(loc); | |
6937 | gogo->add_block(b, loc); | |
6938 | gogo->lower_block(new_no, b); | |
a32698ee | 6939 | gogo->flatten_block(new_no, b); |
0afbb937 | 6940 | gogo->finish_function(loc); |
6941 | ||
6942 | ins.first->second = new_no; | |
6943 | return new_no; | |
6944 | } | |
6945 | ||
6946 | // Return an expression to check *REF for nil while dereferencing | |
6947 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6948 | // reference. This is a static function so that we don't have to | |
6949 | // worry about declaring Field_indexes in expressions.h. | |
6950 | ||
6951 | static Expression* | |
6952 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6953 | Expression** ref) | |
6954 | { | |
6955 | if (field_indexes == NULL) | |
6956 | return Expression::make_boolean(false, loc); | |
6957 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6958 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6959 | go_assert(stype != NULL | |
6960 | && field_indexes->field_index < stype->field_count()); | |
6961 | if ((*ref)->type()->struct_type() == NULL) | |
6962 | { | |
6963 | go_assert((*ref)->type()->points_to() != NULL); | |
6964 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6965 | Expression::make_nil(loc), | |
6966 | loc); | |
6967 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
f614ea8b | 6968 | *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT, |
6969 | loc); | |
0afbb937 | 6970 | go_assert((*ref)->type()->struct_type() == stype); |
6971 | } | |
6972 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6973 | loc); | |
6974 | return cond; | |
e440a328 | 6975 | } |
6976 | ||
cd39797e | 6977 | // Flatten a method value into a struct with nil checks. We can't do |
6978 | // this in the lowering phase, because if the method value is called | |
6979 | // directly we don't need a thunk. That case will have been handled | |
6980 | // by Call_expression::do_lower, so if we get here then we do need a | |
6981 | // thunk. | |
e440a328 | 6982 | |
cd39797e | 6983 | Expression* |
6984 | Bound_method_expression::do_flatten(Gogo* gogo, Named_object*, | |
6985 | Statement_inserter* inserter) | |
e440a328 | 6986 | { |
cd39797e | 6987 | Location loc = this->location(); |
6988 | ||
6989 | Named_object* thunk = Bound_method_expression::create_thunk(gogo, | |
0afbb937 | 6990 | this->method_, |
6991 | this->function_); | |
6992 | if (thunk->is_erroneous()) | |
6993 | { | |
6994 | go_assert(saw_errors()); | |
cd39797e | 6995 | return Expression::make_error(loc); |
0afbb937 | 6996 | } |
6997 | ||
cd39797e | 6998 | // Force the expression into a variable. This is only necessary if |
6999 | // we are going to do nil checks below, but it's easy enough to | |
7000 | // always do it. | |
7001 | Expression* expr = this->expr_; | |
7002 | if (!expr->is_variable()) | |
7003 | { | |
7004 | Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc); | |
7005 | inserter->insert(etemp); | |
7006 | expr = Expression::make_temporary_reference(etemp, loc); | |
7007 | } | |
0afbb937 | 7008 | |
7009 | // If the method expects a value, and we have a pointer, we need to | |
7010 | // dereference the pointer. | |
7011 | ||
7012 | Named_object* fn = this->method_->named_object(); | |
cd39797e | 7013 | Function_type *fntype; |
0afbb937 | 7014 | if (fn->is_function()) |
7015 | fntype = fn->func_value()->type(); | |
7016 | else if (fn->is_function_declaration()) | |
7017 | fntype = fn->func_declaration_value()->type(); | |
7018 | else | |
7019 | go_unreachable(); | |
7020 | ||
cd39797e | 7021 | Expression* val = expr; |
0afbb937 | 7022 | if (fntype->receiver()->type()->points_to() == NULL |
7023 | && val->type()->points_to() != NULL) | |
f614ea8b | 7024 | val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc); |
0afbb937 | 7025 | |
7026 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
7027 | // expect a closure whose second field has type this->expr_type_ (if | |
7028 | // that is not NULL). We are going to pass it a closure whose | |
7029 | // second field has type this->expr_->type(). Since | |
7030 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
7031 | // away with this. | |
7032 | ||
7033 | Struct_field_list* fields = new Struct_field_list(); | |
13f2fdb8 | 7034 | fields->push_back(Struct_field(Typed_identifier("fn", |
0afbb937 | 7035 | thunk->func_value()->type(), |
7036 | loc))); | |
13f2fdb8 | 7037 | fields->push_back(Struct_field(Typed_identifier("val", val->type(), loc))); |
0afbb937 | 7038 | Struct_type* st = Type::make_struct_type(fields, loc); |
6bf4793c | 7039 | st->set_is_struct_incomparable(); |
0afbb937 | 7040 | |
7041 | Expression_list* vals = new Expression_list(); | |
7042 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
7043 | vals->push_back(val); | |
7044 | ||
7045 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
c1177ba4 | 7046 | ret = Expression::make_heap_expression(ret, loc); |
0afbb937 | 7047 | |
c1177ba4 | 7048 | Node* n = Node::make_node(this); |
7049 | if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE) | |
7050 | ret->heap_expression()->set_allocate_on_stack(); | |
7051 | else if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
7052 | go_error_at(loc, "%s escapes to heap, not allowed in runtime", | |
7053 | n->ast_format(gogo).c_str()); | |
cd39797e | 7054 | |
7055 | // If necessary, check whether the expression or any embedded | |
7056 | // pointers are nil. | |
0afbb937 | 7057 | |
df7ef1fd | 7058 | Expression* nil_check = NULL; |
0afbb937 | 7059 | if (this->method_->field_indexes() != NULL) |
7060 | { | |
0afbb937 | 7061 | Expression* ref = expr; |
7062 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
7063 | expr = ref; | |
7064 | } | |
7065 | ||
7066 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
7067 | { | |
7068 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
7069 | Expression::make_nil(loc), | |
7070 | loc); | |
7071 | if (nil_check == NULL) | |
7072 | nil_check = n; | |
7073 | else | |
7074 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
7075 | } | |
7076 | ||
7077 | if (nil_check != NULL) | |
7078 | { | |
cd39797e | 7079 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
7080 | loc); | |
7081 | // Fix the type of the conditional expression by pretending to | |
7082 | // evaluate to RET either way through the conditional. | |
7083 | crash = Expression::make_compound(crash, ret, loc); | |
7084 | ret = Expression::make_conditional(nil_check, crash, ret, loc); | |
7085 | } | |
7086 | ||
7087 | // RET is a pointer to a struct, but we want a function type. | |
7088 | ret = Expression::make_unsafe_cast(this->type(), ret, loc); | |
7089 | ||
7090 | return ret; | |
e440a328 | 7091 | } |
7092 | ||
d751bb78 | 7093 | // Dump ast representation of a bound method expression. |
7094 | ||
7095 | void | |
7096 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
7097 | const | |
7098 | { | |
7099 | if (this->expr_type_ != NULL) | |
7100 | ast_dump_context->ostream() << "("; | |
7101 | ast_dump_context->dump_expression(this->expr_); | |
7102 | if (this->expr_type_ != NULL) | |
7103 | { | |
7104 | ast_dump_context->ostream() << ":"; | |
7105 | ast_dump_context->dump_type(this->expr_type_); | |
7106 | ast_dump_context->ostream() << ")"; | |
7107 | } | |
7108 | ||
0afbb937 | 7109 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 7110 | } |
7111 | ||
e440a328 | 7112 | // Make a method expression. |
7113 | ||
7114 | Bound_method_expression* | |
0afbb937 | 7115 | Expression::make_bound_method(Expression* expr, const Method* method, |
7116 | Named_object* function, Location location) | |
e440a328 | 7117 | { |
0afbb937 | 7118 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 7119 | } |
7120 | ||
7121 | // Class Builtin_call_expression. This is used for a call to a | |
7122 | // builtin function. | |
7123 | ||
e440a328 | 7124 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, |
7125 | Expression* fn, | |
7126 | Expression_list* args, | |
7127 | bool is_varargs, | |
b13c66cd | 7128 | Location location) |
e440a328 | 7129 | : Call_expression(fn, args, is_varargs, location), |
6334270b | 7130 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false), |
7131 | recover_arg_is_set_(false) | |
e440a328 | 7132 | { |
7133 | Func_expression* fnexp = this->fn()->func_expression(); | |
79651b1f | 7134 | if (fnexp == NULL) |
7135 | { | |
7136 | this->code_ = BUILTIN_INVALID; | |
7137 | return; | |
7138 | } | |
e440a328 | 7139 | const std::string& name(fnexp->named_object()->name()); |
7140 | if (name == "append") | |
7141 | this->code_ = BUILTIN_APPEND; | |
7142 | else if (name == "cap") | |
7143 | this->code_ = BUILTIN_CAP; | |
7144 | else if (name == "close") | |
7145 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 7146 | else if (name == "complex") |
7147 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 7148 | else if (name == "copy") |
7149 | this->code_ = BUILTIN_COPY; | |
1cce762f | 7150 | else if (name == "delete") |
7151 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 7152 | else if (name == "imag") |
7153 | this->code_ = BUILTIN_IMAG; | |
7154 | else if (name == "len") | |
7155 | this->code_ = BUILTIN_LEN; | |
7156 | else if (name == "make") | |
7157 | this->code_ = BUILTIN_MAKE; | |
7158 | else if (name == "new") | |
7159 | this->code_ = BUILTIN_NEW; | |
7160 | else if (name == "panic") | |
7161 | this->code_ = BUILTIN_PANIC; | |
7162 | else if (name == "print") | |
7163 | this->code_ = BUILTIN_PRINT; | |
7164 | else if (name == "println") | |
7165 | this->code_ = BUILTIN_PRINTLN; | |
7166 | else if (name == "real") | |
7167 | this->code_ = BUILTIN_REAL; | |
7168 | else if (name == "recover") | |
7169 | this->code_ = BUILTIN_RECOVER; | |
7170 | else if (name == "Alignof") | |
7171 | this->code_ = BUILTIN_ALIGNOF; | |
7172 | else if (name == "Offsetof") | |
7173 | this->code_ = BUILTIN_OFFSETOF; | |
7174 | else if (name == "Sizeof") | |
7175 | this->code_ = BUILTIN_SIZEOF; | |
7176 | else | |
c3e6f413 | 7177 | go_unreachable(); |
e440a328 | 7178 | } |
7179 | ||
7180 | // Return whether this is a call to recover. This is a virtual | |
7181 | // function called from the parent class. | |
7182 | ||
7183 | bool | |
7184 | Builtin_call_expression::do_is_recover_call() const | |
7185 | { | |
7186 | if (this->classification() == EXPRESSION_ERROR) | |
7187 | return false; | |
7188 | return this->code_ == BUILTIN_RECOVER; | |
7189 | } | |
7190 | ||
7191 | // Set the argument for a call to recover. | |
7192 | ||
7193 | void | |
7194 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7195 | { | |
7196 | const Expression_list* args = this->args(); | |
c484d925 | 7197 | go_assert(args == NULL || args->empty()); |
e440a328 | 7198 | Expression_list* new_args = new Expression_list(); |
7199 | new_args->push_back(arg); | |
7200 | this->set_args(new_args); | |
6334270b | 7201 | this->recover_arg_is_set_ = true; |
e440a328 | 7202 | } |
7203 | ||
e440a328 | 7204 | // Lower a builtin call expression. This turns new and make into |
7205 | // specific expressions. We also convert to a constant if we can. | |
7206 | ||
7207 | Expression* | |
321e5ad2 | 7208 | Builtin_call_expression::do_lower(Gogo*, Named_object* function, |
ceeb4318 | 7209 | Statement_inserter* inserter, int) |
e440a328 | 7210 | { |
79651b1f | 7211 | if (this->is_error_expression()) |
a9182619 | 7212 | return this; |
7213 | ||
b13c66cd | 7214 | Location loc = this->location(); |
1cce762f | 7215 | |
a8725655 | 7216 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7217 | { | |
7218 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7219 | return Expression::make_error(loc); |
a8725655 | 7220 | } |
7221 | ||
393ba00b | 7222 | if (this->code_ == BUILTIN_OFFSETOF) |
7223 | { | |
7224 | Expression* arg = this->one_arg(); | |
12e69faa | 7225 | |
7226 | if (arg->bound_method_expression() != NULL | |
7227 | || arg->interface_field_reference_expression() != NULL) | |
7228 | { | |
7229 | this->report_error(_("invalid use of method value as argument " | |
7230 | "of Offsetof")); | |
7231 | return this; | |
7232 | } | |
7233 | ||
393ba00b | 7234 | Field_reference_expression* farg = arg->field_reference_expression(); |
7235 | while (farg != NULL) | |
7236 | { | |
7237 | if (!farg->implicit()) | |
7238 | break; | |
7239 | // When the selector refers to an embedded field, | |
7240 | // it must not be reached through pointer indirections. | |
7241 | if (farg->expr()->deref() != farg->expr()) | |
7242 | { | |
12e69faa | 7243 | this->report_error(_("argument of Offsetof implies " |
7244 | "indirection of an embedded field")); | |
393ba00b | 7245 | return this; |
7246 | } | |
7247 | // Go up until we reach the original base. | |
7248 | farg = farg->expr()->field_reference_expression(); | |
7249 | } | |
7250 | } | |
7251 | ||
1cce762f | 7252 | if (this->is_constant()) |
e440a328 | 7253 | { |
0c77715b | 7254 | Numeric_constant nc; |
7255 | if (this->numeric_constant_value(&nc)) | |
7256 | return nc.expression(loc); | |
e440a328 | 7257 | } |
1cce762f | 7258 | |
7259 | switch (this->code_) | |
e440a328 | 7260 | { |
1cce762f | 7261 | default: |
7262 | break; | |
7263 | ||
7264 | case BUILTIN_NEW: | |
7265 | { | |
7266 | const Expression_list* args = this->args(); | |
7267 | if (args == NULL || args->size() < 1) | |
7268 | this->report_error(_("not enough arguments")); | |
7269 | else if (args->size() > 1) | |
7270 | this->report_error(_("too many arguments")); | |
7271 | else | |
7272 | { | |
7273 | Expression* arg = args->front(); | |
7274 | if (!arg->is_type_expression()) | |
7275 | { | |
631d5788 | 7276 | go_error_at(arg->location(), "expected type"); |
1cce762f | 7277 | this->set_is_error(); |
7278 | } | |
7279 | else | |
7280 | return Expression::make_allocation(arg->type(), loc); | |
7281 | } | |
7282 | } | |
7283 | break; | |
7284 | ||
7285 | case BUILTIN_MAKE: | |
321e5ad2 | 7286 | return this->lower_make(inserter); |
1cce762f | 7287 | |
7288 | case BUILTIN_RECOVER: | |
e440a328 | 7289 | if (function != NULL) |
7290 | function->func_value()->set_calls_recover(); | |
7291 | else | |
7292 | { | |
7293 | // Calling recover outside of a function always returns the | |
7294 | // nil empty interface. | |
823c7e3d | 7295 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7296 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7297 | } |
1cce762f | 7298 | break; |
7299 | ||
1cce762f | 7300 | case BUILTIN_DELETE: |
7301 | { | |
7302 | // Lower to a runtime function call. | |
7303 | const Expression_list* args = this->args(); | |
7304 | if (args == NULL || args->size() < 2) | |
7305 | this->report_error(_("not enough arguments")); | |
7306 | else if (args->size() > 2) | |
7307 | this->report_error(_("too many arguments")); | |
7308 | else if (args->front()->type()->map_type() == NULL) | |
7309 | this->report_error(_("argument 1 must be a map")); | |
7310 | else | |
7311 | { | |
7312 | // Since this function returns no value it must appear in | |
7313 | // a statement by itself, so we don't have to worry about | |
7314 | // order of evaluation of values around it. Evaluate the | |
7315 | // map first to get order of evaluation right. | |
7316 | Map_type* mt = args->front()->type()->map_type(); | |
7317 | Temporary_statement* map_temp = | |
7318 | Statement::make_temporary(mt, args->front(), loc); | |
7319 | inserter->insert(map_temp); | |
7320 | ||
7321 | Temporary_statement* key_temp = | |
7322 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7323 | inserter->insert(key_temp); | |
7324 | ||
0d5530d9 | 7325 | Expression* e1 = Expression::make_type_descriptor(mt, loc); |
7326 | Expression* e2 = Expression::make_temporary_reference(map_temp, | |
1cce762f | 7327 | loc); |
0d5530d9 | 7328 | Expression* e3 = Expression::make_temporary_reference(key_temp, |
1cce762f | 7329 | loc); |
0d5530d9 | 7330 | e3 = Expression::make_unary(OPERATOR_AND, e3, loc); |
1cce762f | 7331 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), |
0d5530d9 | 7332 | 3, e1, e2, e3); |
1cce762f | 7333 | } |
7334 | } | |
7335 | break; | |
88b03a70 | 7336 | |
7337 | case BUILTIN_PRINT: | |
7338 | case BUILTIN_PRINTLN: | |
7339 | // Force all the arguments into temporary variables, so that we | |
7340 | // don't try to evaluate something while holding the print lock. | |
7341 | if (this->args() == NULL) | |
7342 | break; | |
7343 | for (Expression_list::iterator pa = this->args()->begin(); | |
7344 | pa != this->args()->end(); | |
7345 | ++pa) | |
7346 | { | |
493ce3ee | 7347 | if (!(*pa)->is_variable() && !(*pa)->is_constant()) |
88b03a70 | 7348 | { |
7349 | Temporary_statement* temp = | |
7350 | Statement::make_temporary(NULL, *pa, loc); | |
7351 | inserter->insert(temp); | |
7352 | *pa = Expression::make_temporary_reference(temp, loc); | |
7353 | } | |
7354 | } | |
7355 | break; | |
e440a328 | 7356 | } |
7357 | ||
7358 | return this; | |
7359 | } | |
7360 | ||
35a54f17 | 7361 | // Flatten a builtin call expression. This turns the arguments of copy and |
7362 | // append into temporary expressions. | |
7363 | ||
7364 | Expression* | |
321e5ad2 | 7365 | Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function, |
35a54f17 | 7366 | Statement_inserter* inserter) |
7367 | { | |
16cb7fec | 7368 | Location loc = this->location(); |
7369 | ||
7370 | switch (this->code_) | |
35a54f17 | 7371 | { |
16cb7fec | 7372 | default: |
7373 | break; | |
7374 | ||
7375 | case BUILTIN_APPEND: | |
321e5ad2 | 7376 | return this->flatten_append(gogo, function, inserter); |
7377 | ||
16cb7fec | 7378 | case BUILTIN_COPY: |
7379 | { | |
7380 | Type* at = this->args()->front()->type(); | |
7381 | for (Expression_list::iterator pa = this->args()->begin(); | |
7382 | pa != this->args()->end(); | |
7383 | ++pa) | |
7384 | { | |
7385 | if ((*pa)->is_nil_expression()) | |
7386 | { | |
7387 | Expression* nil = Expression::make_nil(loc); | |
7388 | Expression* zero = Expression::make_integer_ul(0, NULL, loc); | |
7389 | *pa = Expression::make_slice_value(at, nil, zero, zero, loc); | |
7390 | } | |
7391 | if (!(*pa)->is_variable()) | |
7392 | { | |
7393 | Temporary_statement* temp = | |
7394 | Statement::make_temporary(NULL, *pa, loc); | |
7395 | inserter->insert(temp); | |
7396 | *pa = Expression::make_temporary_reference(temp, loc); | |
7397 | } | |
7398 | } | |
7399 | } | |
7400 | break; | |
7401 | ||
7402 | case BUILTIN_PANIC: | |
35a54f17 | 7403 | for (Expression_list::iterator pa = this->args()->begin(); |
16cb7fec | 7404 | pa != this->args()->end(); |
7405 | ++pa) | |
7406 | { | |
7407 | if (!(*pa)->is_variable() && (*pa)->type()->interface_type() != NULL) | |
55e8ba6a | 7408 | { |
16cb7fec | 7409 | Temporary_statement* temp = |
7410 | Statement::make_temporary(NULL, *pa, loc); | |
7411 | inserter->insert(temp); | |
7412 | *pa = Expression::make_temporary_reference(temp, loc); | |
55e8ba6a | 7413 | } |
16cb7fec | 7414 | } |
7739537f | 7415 | break; |
0d5530d9 | 7416 | |
7417 | case BUILTIN_LEN: | |
132ed071 | 7418 | case BUILTIN_CAP: |
321e5ad2 | 7419 | { |
7420 | Expression_list::iterator pa = this->args()->begin(); | |
7421 | if (!(*pa)->is_variable() | |
7422 | && ((*pa)->type()->map_type() != NULL | |
7423 | || (*pa)->type()->channel_type() != NULL)) | |
7424 | { | |
7425 | Temporary_statement* temp = | |
7426 | Statement::make_temporary(NULL, *pa, loc); | |
7427 | inserter->insert(temp); | |
7428 | *pa = Expression::make_temporary_reference(temp, loc); | |
7429 | } | |
7430 | } | |
7431 | break; | |
35a54f17 | 7432 | } |
16cb7fec | 7433 | |
35a54f17 | 7434 | return this; |
7435 | } | |
7436 | ||
a9182619 | 7437 | // Lower a make expression. |
7438 | ||
7439 | Expression* | |
321e5ad2 | 7440 | Builtin_call_expression::lower_make(Statement_inserter* inserter) |
a9182619 | 7441 | { |
b13c66cd | 7442 | Location loc = this->location(); |
a9182619 | 7443 | |
7444 | const Expression_list* args = this->args(); | |
7445 | if (args == NULL || args->size() < 1) | |
7446 | { | |
7447 | this->report_error(_("not enough arguments")); | |
7448 | return Expression::make_error(this->location()); | |
7449 | } | |
7450 | ||
7451 | Expression_list::const_iterator parg = args->begin(); | |
7452 | ||
7453 | Expression* first_arg = *parg; | |
7454 | if (!first_arg->is_type_expression()) | |
7455 | { | |
631d5788 | 7456 | go_error_at(first_arg->location(), "expected type"); |
a9182619 | 7457 | this->set_is_error(); |
7458 | return Expression::make_error(this->location()); | |
7459 | } | |
7460 | Type* type = first_arg->type(); | |
7461 | ||
22deed0d | 7462 | if (!type->in_heap()) |
7463 | go_error_at(first_arg->location(), | |
7464 | "can't make slice of go:notinheap type"); | |
7465 | ||
a9182619 | 7466 | bool is_slice = false; |
7467 | bool is_map = false; | |
7468 | bool is_chan = false; | |
411eb89e | 7469 | if (type->is_slice_type()) |
a9182619 | 7470 | is_slice = true; |
7471 | else if (type->map_type() != NULL) | |
7472 | is_map = true; | |
7473 | else if (type->channel_type() != NULL) | |
7474 | is_chan = true; | |
7475 | else | |
7476 | { | |
7477 | this->report_error(_("invalid type for make function")); | |
7478 | return Expression::make_error(this->location()); | |
7479 | } | |
7480 | ||
f6bc81e6 | 7481 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7482 | ||
a9182619 | 7483 | ++parg; |
7484 | Expression* len_arg; | |
ccea2b36 | 7485 | bool len_small = false; |
a9182619 | 7486 | if (parg == args->end()) |
7487 | { | |
7488 | if (is_slice) | |
7489 | { | |
7490 | this->report_error(_("length required when allocating a slice")); | |
7491 | return Expression::make_error(this->location()); | |
7492 | } | |
e67508fa | 7493 | len_arg = Expression::make_integer_ul(0, NULL, loc); |
33d1d391 | 7494 | len_small = true; |
a9182619 | 7495 | } |
7496 | else | |
7497 | { | |
7498 | len_arg = *parg; | |
f6bc81e6 | 7499 | len_arg->determine_type(&int_context); |
ccea2b36 | 7500 | if (!this->check_int_value(len_arg, true, &len_small)) |
1ad00fd4 | 7501 | return Expression::make_error(this->location()); |
a9182619 | 7502 | ++parg; |
7503 | } | |
7504 | ||
7505 | Expression* cap_arg = NULL; | |
ccea2b36 | 7506 | bool cap_small = false; |
72bf0e6e | 7507 | Numeric_constant nclen; |
7508 | Numeric_constant nccap; | |
7509 | unsigned long vlen; | |
7510 | unsigned long vcap; | |
a9182619 | 7511 | if (is_slice && parg != args->end()) |
7512 | { | |
7513 | cap_arg = *parg; | |
f6bc81e6 | 7514 | cap_arg->determine_type(&int_context); |
ccea2b36 | 7515 | if (!this->check_int_value(cap_arg, false, &cap_small)) |
1ad00fd4 | 7516 | return Expression::make_error(this->location()); |
7517 | ||
1ad00fd4 | 7518 | if (len_arg->numeric_constant_value(&nclen) |
7519 | && cap_arg->numeric_constant_value(&nccap) | |
7520 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7521 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7522 | && vlen > vcap) | |
a9182619 | 7523 | { |
1ad00fd4 | 7524 | this->report_error(_("len larger than cap")); |
a9182619 | 7525 | return Expression::make_error(this->location()); |
7526 | } | |
1ad00fd4 | 7527 | |
a9182619 | 7528 | ++parg; |
7529 | } | |
7530 | ||
7531 | if (parg != args->end()) | |
7532 | { | |
7533 | this->report_error(_("too many arguments to make")); | |
7534 | return Expression::make_error(this->location()); | |
7535 | } | |
7536 | ||
b13c66cd | 7537 | Location type_loc = first_arg->location(); |
a9182619 | 7538 | |
7539 | Expression* call; | |
7540 | if (is_slice) | |
7541 | { | |
7542 | if (cap_arg == NULL) | |
321e5ad2 | 7543 | { |
72bf0e6e | 7544 | cap_small = len_small; |
7545 | if (len_arg->numeric_constant_value(&nclen) | |
7546 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID) | |
7547 | cap_arg = Expression::make_integer_ul(vlen, len_arg->type(), loc); | |
7548 | else | |
7549 | { | |
7550 | Temporary_statement* temp = Statement::make_temporary(NULL, | |
7551 | len_arg, | |
7552 | loc); | |
7553 | inserter->insert(temp); | |
7554 | len_arg = Expression::make_temporary_reference(temp, loc); | |
7555 | cap_arg = Expression::make_temporary_reference(temp, loc); | |
7556 | } | |
321e5ad2 | 7557 | } |
ccea2b36 | 7558 | |
72bf0e6e | 7559 | Type* et = type->array_type()->element_type(); |
7560 | Expression* type_arg = Expression::make_type_descriptor(et, type_loc); | |
ccea2b36 | 7561 | Runtime::Function code = Runtime::MAKESLICE; |
7562 | if (!len_small || !cap_small) | |
7563 | code = Runtime::MAKESLICE64; | |
7564 | call = Runtime::make_call(code, loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7565 | } |
7566 | else if (is_map) | |
321e5ad2 | 7567 | { |
7568 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
33d1d391 | 7569 | if (!len_small) |
7570 | call = Runtime::make_call(Runtime::MAKEMAP64, loc, 3, type_arg, | |
7571 | len_arg, | |
7572 | Expression::make_nil(loc)); | |
7573 | else | |
7574 | { | |
7575 | Numeric_constant nclen; | |
7576 | unsigned long vlen; | |
7577 | if (len_arg->numeric_constant_value(&nclen) | |
7578 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7579 | && vlen <= Map_type::bucket_size) | |
7580 | call = Runtime::make_call(Runtime::MAKEMAP_SMALL, loc, 0); | |
7581 | else | |
7582 | call = Runtime::make_call(Runtime::MAKEMAP, loc, 3, type_arg, | |
7583 | len_arg, | |
7584 | Expression::make_nil(loc)); | |
7585 | } | |
321e5ad2 | 7586 | } |
a9182619 | 7587 | else if (is_chan) |
321e5ad2 | 7588 | { |
7589 | Expression* type_arg = Expression::make_type_descriptor(type, type_loc); | |
1423c90a | 7590 | Runtime::Function code = Runtime::MAKECHAN; |
7591 | if (!len_small) | |
7592 | code = Runtime::MAKECHAN64; | |
7593 | call = Runtime::make_call(code, loc, 2, type_arg, len_arg); | |
321e5ad2 | 7594 | } |
a9182619 | 7595 | else |
7596 | go_unreachable(); | |
7597 | ||
7598 | return Expression::make_unsafe_cast(type, call, loc); | |
7599 | } | |
7600 | ||
321e5ad2 | 7601 | // Flatten a call to the predeclared append function. We do this in |
7602 | // the flatten phase, not the lowering phase, so that we run after | |
7603 | // type checking and after order_evaluations. | |
7604 | ||
7605 | Expression* | |
7606 | Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function, | |
7607 | Statement_inserter* inserter) | |
7608 | { | |
7609 | if (this->is_error_expression()) | |
7610 | return this; | |
7611 | ||
7612 | Location loc = this->location(); | |
7613 | ||
7614 | const Expression_list* args = this->args(); | |
7615 | go_assert(args != NULL && !args->empty()); | |
7616 | ||
7617 | Type* slice_type = args->front()->type(); | |
7618 | go_assert(slice_type->is_slice_type()); | |
7619 | Type* element_type = slice_type->array_type()->element_type(); | |
7620 | ||
7621 | if (args->size() == 1) | |
7622 | { | |
7623 | // append(s) evaluates to s. | |
7624 | return args->front(); | |
7625 | } | |
7626 | ||
7627 | Type* int_type = Type::lookup_integer_type("int"); | |
7628 | Type* uint_type = Type::lookup_integer_type("uint"); | |
7629 | ||
7630 | // Implementing | |
7631 | // append(s1, s2...) | |
7632 | // or | |
7633 | // append(s1, a1, a2, a3, ...) | |
7634 | ||
7635 | // s1tmp := s1 | |
7636 | Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(), | |
7637 | loc); | |
7638 | inserter->insert(s1tmp); | |
7639 | ||
7640 | // l1tmp := len(s1tmp) | |
7641 | Named_object* lenfn = gogo->lookup_global("len"); | |
7642 | Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7643 | Expression_list* call_args = new Expression_list(); | |
7644 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7645 | Expression* len = Expression::make_call(lenref, call_args, false, loc); | |
7646 | gogo->lower_expression(function, inserter, &len); | |
7647 | gogo->flatten_expression(function, inserter, &len); | |
7648 | Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc); | |
7649 | inserter->insert(l1tmp); | |
7650 | ||
7651 | Temporary_statement* s2tmp = NULL; | |
7652 | Temporary_statement* l2tmp = NULL; | |
7653 | Expression_list* add = NULL; | |
7654 | Expression* len2; | |
7655 | if (this->is_varargs()) | |
7656 | { | |
7657 | go_assert(args->size() == 2); | |
7658 | ||
7659 | // s2tmp := s2 | |
7660 | s2tmp = Statement::make_temporary(NULL, args->back(), loc); | |
7661 | inserter->insert(s2tmp); | |
7662 | ||
7663 | // l2tmp := len(s2tmp) | |
7664 | lenref = Expression::make_func_reference(lenfn, NULL, loc); | |
7665 | call_args = new Expression_list(); | |
7666 | call_args->push_back(Expression::make_temporary_reference(s2tmp, loc)); | |
7667 | len = Expression::make_call(lenref, call_args, false, loc); | |
7668 | gogo->lower_expression(function, inserter, &len); | |
7669 | gogo->flatten_expression(function, inserter, &len); | |
7670 | l2tmp = Statement::make_temporary(int_type, len, loc); | |
7671 | inserter->insert(l2tmp); | |
7672 | ||
7673 | // len2 = l2tmp | |
7674 | len2 = Expression::make_temporary_reference(l2tmp, loc); | |
7675 | } | |
7676 | else | |
7677 | { | |
7678 | // We have to ensure that all the arguments are in variables | |
7679 | // now, because otherwise if one of them is an index expression | |
7680 | // into the current slice we could overwrite it before we fetch | |
7681 | // it. | |
7682 | add = new Expression_list(); | |
7683 | Expression_list::const_iterator pa = args->begin(); | |
7684 | for (++pa; pa != args->end(); ++pa) | |
7685 | { | |
7686 | if ((*pa)->is_variable()) | |
7687 | add->push_back(*pa); | |
7688 | else | |
7689 | { | |
7690 | Temporary_statement* tmp = Statement::make_temporary(NULL, *pa, | |
7691 | loc); | |
7692 | inserter->insert(tmp); | |
7693 | add->push_back(Expression::make_temporary_reference(tmp, loc)); | |
7694 | } | |
7695 | } | |
7696 | ||
7697 | // len2 = len(add) | |
7698 | len2 = Expression::make_integer_ul(add->size(), int_type, loc); | |
7699 | } | |
7700 | ||
7701 | // ntmp := l1tmp + len2 | |
7702 | Expression* ref = Expression::make_temporary_reference(l1tmp, loc); | |
7703 | Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc); | |
7704 | gogo->lower_expression(function, inserter, &sum); | |
7705 | gogo->flatten_expression(function, inserter, &sum); | |
7706 | Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc); | |
7707 | inserter->insert(ntmp); | |
7708 | ||
7709 | // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ? | |
7710 | // growslice(type, s1tmp, ntmp) : | |
7711 | // s1tmp[:ntmp] | |
7712 | // Using uint here means that if the computation of ntmp overflowed, | |
7713 | // we will call growslice which will panic. | |
7714 | ||
7715 | Expression* left = Expression::make_temporary_reference(ntmp, loc); | |
7716 | left = Expression::make_cast(uint_type, left, loc); | |
7717 | ||
7718 | Named_object* capfn = gogo->lookup_global("cap"); | |
7719 | Expression* capref = Expression::make_func_reference(capfn, NULL, loc); | |
7720 | call_args = new Expression_list(); | |
7721 | call_args->push_back(Expression::make_temporary_reference(s1tmp, loc)); | |
7722 | Expression* right = Expression::make_call(capref, call_args, false, loc); | |
7723 | right = Expression::make_cast(uint_type, right, loc); | |
7724 | ||
7725 | Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc); | |
7726 | ||
7727 | Expression* a1 = Expression::make_type_descriptor(element_type, loc); | |
7728 | Expression* a2 = Expression::make_temporary_reference(s1tmp, loc); | |
7729 | Expression* a3 = Expression::make_temporary_reference(ntmp, loc); | |
7730 | Expression* call = Runtime::make_call(Runtime::GROWSLICE, loc, 3, | |
7731 | a1, a2, a3); | |
7732 | call = Expression::make_unsafe_cast(slice_type, call, loc); | |
7733 | ||
7734 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7735 | Expression* zero = Expression::make_integer_ul(0, int_type, loc); | |
7736 | Expression* ref2 = Expression::make_temporary_reference(ntmp, loc); | |
7737 | // FIXME: Mark this index as not requiring bounds checks. | |
7738 | ref = Expression::make_index(ref, zero, ref2, NULL, loc); | |
7739 | ||
7740 | Expression* rhs = Expression::make_conditional(cond, call, ref, loc); | |
7741 | ||
7742 | gogo->lower_expression(function, inserter, &rhs); | |
7743 | gogo->flatten_expression(function, inserter, &rhs); | |
7744 | ||
7745 | Expression* lhs = Expression::make_temporary_reference(s1tmp, loc); | |
7746 | Statement* assign = Statement::make_assignment(lhs, rhs, loc); | |
7747 | inserter->insert(assign); | |
7748 | ||
7749 | if (this->is_varargs()) | |
7750 | { | |
7751 | // copy(s1tmp[l1tmp:], s2tmp) | |
7752 | a1 = Expression::make_temporary_reference(s1tmp, loc); | |
7753 | ref = Expression::make_temporary_reference(l1tmp, loc); | |
7754 | Expression* nil = Expression::make_nil(loc); | |
7755 | // FIXME: Mark this index as not requiring bounds checks. | |
7756 | a1 = Expression::make_index(a1, ref, nil, NULL, loc); | |
7757 | ||
7758 | a2 = Expression::make_temporary_reference(s2tmp, loc); | |
7759 | ||
7760 | Named_object* copyfn = gogo->lookup_global("copy"); | |
7761 | Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc); | |
7762 | call_args = new Expression_list(); | |
7763 | call_args->push_back(a1); | |
7764 | call_args->push_back(a2); | |
7765 | call = Expression::make_call(copyref, call_args, false, loc); | |
7766 | gogo->lower_expression(function, inserter, &call); | |
7767 | gogo->flatten_expression(function, inserter, &call); | |
7768 | inserter->insert(Statement::make_statement(call, false)); | |
7769 | } | |
7770 | else | |
7771 | { | |
7772 | // For each argument: | |
7773 | // s1tmp[l1tmp+i] = a | |
7774 | unsigned long i = 0; | |
7775 | for (Expression_list::const_iterator pa = add->begin(); | |
7776 | pa != add->end(); | |
7777 | ++pa, ++i) | |
7778 | { | |
7779 | ref = Expression::make_temporary_reference(s1tmp, loc); | |
7780 | ref2 = Expression::make_temporary_reference(l1tmp, loc); | |
7781 | Expression* off = Expression::make_integer_ul(i, int_type, loc); | |
7782 | ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc); | |
7783 | // FIXME: Mark this index as not requiring bounds checks. | |
7784 | lhs = Expression::make_index(ref, ref2, NULL, NULL, loc); | |
7785 | gogo->lower_expression(function, inserter, &lhs); | |
7786 | gogo->flatten_expression(function, inserter, &lhs); | |
03118c21 | 7787 | // The flatten pass runs after the write barrier pass, so we |
7788 | // need to insert a write barrier here if necessary. | |
7789 | if (!gogo->assign_needs_write_barrier(lhs)) | |
7790 | assign = Statement::make_assignment(lhs, *pa, loc); | |
7791 | else | |
7792 | { | |
7793 | Function* f = function == NULL ? NULL : function->func_value(); | |
7794 | assign = gogo->assign_with_write_barrier(f, NULL, inserter, | |
7795 | lhs, *pa, loc); | |
7796 | } | |
321e5ad2 | 7797 | inserter->insert(assign); |
7798 | } | |
7799 | } | |
7800 | ||
7801 | return Expression::make_temporary_reference(s1tmp, loc); | |
7802 | } | |
7803 | ||
a9182619 | 7804 | // Return whether an expression has an integer value. Report an error |
7805 | // if not. This is used when handling calls to the predeclared make | |
ccea2b36 | 7806 | // function. Set *SMALL if the value is known to fit in type "int". |
a9182619 | 7807 | |
7808 | bool | |
ccea2b36 | 7809 | Builtin_call_expression::check_int_value(Expression* e, bool is_length, |
7810 | bool *small) | |
a9182619 | 7811 | { |
ccea2b36 | 7812 | *small = false; |
7813 | ||
0c77715b | 7814 | Numeric_constant nc; |
1ad00fd4 | 7815 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7816 | { |
1ad00fd4 | 7817 | unsigned long v; |
7818 | switch (nc.to_unsigned_long(&v)) | |
7819 | { | |
7820 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7821 | break; |
1ad00fd4 | 7822 | case Numeric_constant::NC_UL_NOTINT: |
631d5788 | 7823 | go_error_at(e->location(), "non-integer %s argument to make", |
7824 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7825 | return false; |
7826 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 7827 | go_error_at(e->location(), "negative %s argument to make", |
7828 | is_length ? "len" : "cap"); | |
1ad00fd4 | 7829 | return false; |
7830 | case Numeric_constant::NC_UL_BIG: | |
7831 | // We don't want to give a compile-time error for a 64-bit | |
7832 | // value on a 32-bit target. | |
1b10c5e7 | 7833 | break; |
1ad00fd4 | 7834 | } |
1b10c5e7 | 7835 | |
7836 | mpz_t val; | |
7837 | if (!nc.to_int(&val)) | |
7838 | go_unreachable(); | |
7839 | int bits = mpz_sizeinbase(val, 2); | |
7840 | mpz_clear(val); | |
7841 | Type* int_type = Type::lookup_integer_type("int"); | |
7842 | if (bits >= int_type->integer_type()->bits()) | |
7843 | { | |
631d5788 | 7844 | go_error_at(e->location(), "%s argument too large for make", |
7845 | is_length ? "len" : "cap"); | |
1b10c5e7 | 7846 | return false; |
7847 | } | |
7848 | ||
ccea2b36 | 7849 | *small = true; |
1b10c5e7 | 7850 | return true; |
a9182619 | 7851 | } |
7852 | ||
1ad00fd4 | 7853 | if (e->type()->integer_type() != NULL) |
ccea2b36 | 7854 | { |
7855 | int ebits = e->type()->integer_type()->bits(); | |
7856 | int intbits = Type::lookup_integer_type("int")->integer_type()->bits(); | |
7857 | ||
7858 | // We can treat ebits == intbits as small even for an unsigned | |
7859 | // integer type, because we will convert the value to int and | |
7860 | // then reject it in the runtime if it is negative. | |
7861 | *small = ebits <= intbits; | |
7862 | ||
7863 | return true; | |
7864 | } | |
1ad00fd4 | 7865 | |
631d5788 | 7866 | go_error_at(e->location(), "non-integer %s argument to make", |
7867 | is_length ? "len" : "cap"); | |
a9182619 | 7868 | return false; |
7869 | } | |
7870 | ||
e440a328 | 7871 | // Return the type of the real or imag functions, given the type of |
fcbea5e4 | 7872 | // the argument. We need to map complex64 to float32 and complex128 |
7873 | // to float64, so it has to be done by name. This returns NULL if it | |
7874 | // can't figure out the type. | |
e440a328 | 7875 | |
7876 | Type* | |
7877 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7878 | { | |
7879 | if (arg_type == NULL || arg_type->is_abstract()) | |
7880 | return NULL; | |
7881 | Named_type* nt = arg_type->named_type(); | |
7882 | if (nt == NULL) | |
7883 | return NULL; | |
7884 | while (nt->real_type()->named_type() != NULL) | |
7885 | nt = nt->real_type()->named_type(); | |
48080209 | 7886 | if (nt->name() == "complex64") |
e440a328 | 7887 | return Type::lookup_float_type("float32"); |
7888 | else if (nt->name() == "complex128") | |
7889 | return Type::lookup_float_type("float64"); | |
7890 | else | |
7891 | return NULL; | |
7892 | } | |
7893 | ||
48080209 | 7894 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7895 | // argments. Like real_imag_type, we have to map by name. |
7896 | ||
7897 | Type* | |
48080209 | 7898 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7899 | { |
7900 | if (arg_type == NULL || arg_type->is_abstract()) | |
7901 | return NULL; | |
7902 | Named_type* nt = arg_type->named_type(); | |
7903 | if (nt == NULL) | |
7904 | return NULL; | |
7905 | while (nt->real_type()->named_type() != NULL) | |
7906 | nt = nt->real_type()->named_type(); | |
48080209 | 7907 | if (nt->name() == "float32") |
e440a328 | 7908 | return Type::lookup_complex_type("complex64"); |
7909 | else if (nt->name() == "float64") | |
7910 | return Type::lookup_complex_type("complex128"); | |
7911 | else | |
7912 | return NULL; | |
7913 | } | |
7914 | ||
7915 | // Return a single argument, or NULL if there isn't one. | |
7916 | ||
7917 | Expression* | |
7918 | Builtin_call_expression::one_arg() const | |
7919 | { | |
7920 | const Expression_list* args = this->args(); | |
aa615cb3 | 7921 | if (args == NULL || args->size() != 1) |
e440a328 | 7922 | return NULL; |
7923 | return args->front(); | |
7924 | } | |
7925 | ||
83921647 | 7926 | // A traversal class which looks for a call or receive expression. |
7927 | ||
7928 | class Find_call_expression : public Traverse | |
7929 | { | |
7930 | public: | |
7931 | Find_call_expression() | |
7932 | : Traverse(traverse_expressions), | |
7933 | found_(false) | |
7934 | { } | |
7935 | ||
7936 | int | |
7937 | expression(Expression**); | |
7938 | ||
7939 | bool | |
7940 | found() | |
7941 | { return this->found_; } | |
7942 | ||
7943 | private: | |
7944 | bool found_; | |
7945 | }; | |
7946 | ||
7947 | int | |
7948 | Find_call_expression::expression(Expression** pexpr) | |
7949 | { | |
7950 | if ((*pexpr)->call_expression() != NULL | |
7951 | || (*pexpr)->receive_expression() != NULL) | |
7952 | { | |
7953 | this->found_ = true; | |
7954 | return TRAVERSE_EXIT; | |
7955 | } | |
7956 | return TRAVERSE_CONTINUE; | |
7957 | } | |
7958 | ||
7959 | // Return whether this is constant: len of a string constant, or len | |
7960 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7961 | // unsafe.Alignof. | |
e440a328 | 7962 | |
7963 | bool | |
7964 | Builtin_call_expression::do_is_constant() const | |
7965 | { | |
12e69faa | 7966 | if (this->is_error_expression()) |
7967 | return true; | |
e440a328 | 7968 | switch (this->code_) |
7969 | { | |
7970 | case BUILTIN_LEN: | |
7971 | case BUILTIN_CAP: | |
7972 | { | |
0f914071 | 7973 | if (this->seen_) |
7974 | return false; | |
7975 | ||
e440a328 | 7976 | Expression* arg = this->one_arg(); |
7977 | if (arg == NULL) | |
7978 | return false; | |
7979 | Type* arg_type = arg->type(); | |
7980 | ||
7981 | if (arg_type->points_to() != NULL | |
7982 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7983 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7984 | arg_type = arg_type->points_to(); |
7985 | ||
83921647 | 7986 | // The len and cap functions are only constant if there are no |
7987 | // function calls or channel operations in the arguments. | |
7988 | // Otherwise we have to make the call. | |
7989 | if (!arg->is_constant()) | |
7990 | { | |
7991 | Find_call_expression find_call; | |
7992 | Expression::traverse(&arg, &find_call); | |
7993 | if (find_call.found()) | |
7994 | return false; | |
7995 | } | |
7996 | ||
e440a328 | 7997 | if (arg_type->array_type() != NULL |
7998 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7999 | return true; |
e440a328 | 8000 | |
8001 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 8002 | { |
8003 | this->seen_ = true; | |
8004 | bool ret = arg->is_constant(); | |
8005 | this->seen_ = false; | |
8006 | return ret; | |
8007 | } | |
e440a328 | 8008 | } |
8009 | break; | |
8010 | ||
8011 | case BUILTIN_SIZEOF: | |
8012 | case BUILTIN_ALIGNOF: | |
8013 | return this->one_arg() != NULL; | |
8014 | ||
8015 | case BUILTIN_OFFSETOF: | |
8016 | { | |
8017 | Expression* arg = this->one_arg(); | |
8018 | if (arg == NULL) | |
8019 | return false; | |
8020 | return arg->field_reference_expression() != NULL; | |
8021 | } | |
8022 | ||
48080209 | 8023 | case BUILTIN_COMPLEX: |
e440a328 | 8024 | { |
8025 | const Expression_list* args = this->args(); | |
8026 | if (args != NULL && args->size() == 2) | |
8027 | return args->front()->is_constant() && args->back()->is_constant(); | |
8028 | } | |
8029 | break; | |
8030 | ||
8031 | case BUILTIN_REAL: | |
8032 | case BUILTIN_IMAG: | |
8033 | { | |
8034 | Expression* arg = this->one_arg(); | |
8035 | return arg != NULL && arg->is_constant(); | |
8036 | } | |
8037 | ||
8038 | default: | |
8039 | break; | |
8040 | } | |
8041 | ||
8042 | return false; | |
8043 | } | |
8044 | ||
0c77715b | 8045 | // Return a numeric constant if possible. |
e440a328 | 8046 | |
8047 | bool | |
0c77715b | 8048 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 8049 | { |
8050 | if (this->code_ == BUILTIN_LEN | |
8051 | || this->code_ == BUILTIN_CAP) | |
8052 | { | |
8053 | Expression* arg = this->one_arg(); | |
8054 | if (arg == NULL) | |
8055 | return false; | |
8056 | Type* arg_type = arg->type(); | |
8057 | ||
8058 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
8059 | { | |
8060 | std::string sval; | |
8061 | if (arg->string_constant_value(&sval)) | |
8062 | { | |
0c77715b | 8063 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
8064 | sval.length()); | |
e440a328 | 8065 | return true; |
8066 | } | |
8067 | } | |
8068 | ||
8069 | if (arg_type->points_to() != NULL | |
8070 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8071 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8072 | arg_type = arg_type->points_to(); |
8073 | ||
8074 | if (arg_type->array_type() != NULL | |
8075 | && arg_type->array_type()->length() != NULL) | |
8076 | { | |
0f914071 | 8077 | if (this->seen_) |
8078 | return false; | |
e440a328 | 8079 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 8080 | this->seen_ = true; |
0c77715b | 8081 | bool r = e->numeric_constant_value(nc); |
0f914071 | 8082 | this->seen_ = false; |
8083 | if (r) | |
e440a328 | 8084 | { |
0c77715b | 8085 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
8086 | this->location())) | |
8087 | r = false; | |
e440a328 | 8088 | } |
0c77715b | 8089 | return r; |
e440a328 | 8090 | } |
8091 | } | |
8092 | else if (this->code_ == BUILTIN_SIZEOF | |
8093 | || this->code_ == BUILTIN_ALIGNOF) | |
8094 | { | |
8095 | Expression* arg = this->one_arg(); | |
8096 | if (arg == NULL) | |
8097 | return false; | |
8098 | Type* arg_type = arg->type(); | |
5c13bd80 | 8099 | if (arg_type->is_error()) |
e440a328 | 8100 | return false; |
8101 | if (arg_type->is_abstract()) | |
8102 | return false; | |
2c809f8f | 8103 | if (this->seen_) |
8104 | return false; | |
927a01eb | 8105 | |
3f378015 | 8106 | int64_t ret; |
e440a328 | 8107 | if (this->code_ == BUILTIN_SIZEOF) |
8108 | { | |
2c809f8f | 8109 | this->seen_ = true; |
8110 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
8111 | this->seen_ = false; | |
8112 | if (!ok) | |
e440a328 | 8113 | return false; |
8114 | } | |
8115 | else if (this->code_ == BUILTIN_ALIGNOF) | |
8116 | { | |
2c809f8f | 8117 | bool ok; |
8118 | this->seen_ = true; | |
637bd3af | 8119 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 8120 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 8121 | else |
e440a328 | 8122 | { |
8123 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
8124 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 8125 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 8126 | } |
2c809f8f | 8127 | this->seen_ = false; |
8128 | if (!ok) | |
8129 | return false; | |
e440a328 | 8130 | } |
8131 | else | |
c3e6f413 | 8132 | go_unreachable(); |
927a01eb | 8133 | |
3f378015 | 8134 | mpz_t zval; |
8135 | set_mpz_from_int64(&zval, ret); | |
8136 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8137 | mpz_clear(zval); | |
e440a328 | 8138 | return true; |
8139 | } | |
8140 | else if (this->code_ == BUILTIN_OFFSETOF) | |
8141 | { | |
8142 | Expression* arg = this->one_arg(); | |
8143 | if (arg == NULL) | |
8144 | return false; | |
8145 | Field_reference_expression* farg = arg->field_reference_expression(); | |
8146 | if (farg == NULL) | |
8147 | return false; | |
2c809f8f | 8148 | if (this->seen_) |
8149 | return false; | |
8150 | ||
3f378015 | 8151 | int64_t total_offset = 0; |
9a4bd570 | 8152 | while (true) |
8153 | { | |
8154 | Expression* struct_expr = farg->expr(); | |
8155 | Type* st = struct_expr->type(); | |
8156 | if (st->struct_type() == NULL) | |
8157 | return false; | |
8158 | if (st->named_type() != NULL) | |
8159 | st->named_type()->convert(this->gogo_); | |
3f378015 | 8160 | int64_t offset; |
2c809f8f | 8161 | this->seen_ = true; |
8162 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
8163 | farg->field_index(), | |
8164 | &offset); | |
8165 | this->seen_ = false; | |
8166 | if (!ok) | |
8167 | return false; | |
9a4bd570 | 8168 | total_offset += offset; |
8169 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
8170 | { | |
8171 | // Go up until we reach the original base. | |
8172 | farg = struct_expr->field_reference_expression(); | |
8173 | continue; | |
8174 | } | |
8175 | break; | |
8176 | } | |
3f378015 | 8177 | mpz_t zval; |
8178 | set_mpz_from_int64(&zval, total_offset); | |
8179 | nc->set_int(Type::lookup_integer_type("uintptr"), zval); | |
8180 | mpz_clear(zval); | |
e440a328 | 8181 | return true; |
8182 | } | |
0c77715b | 8183 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 8184 | { |
8185 | Expression* arg = this->one_arg(); | |
8186 | if (arg == NULL) | |
8187 | return false; | |
8188 | ||
0c77715b | 8189 | Numeric_constant argnc; |
8190 | if (!arg->numeric_constant_value(&argnc)) | |
8191 | return false; | |
8192 | ||
fcbea5e4 | 8193 | mpc_t val; |
8194 | if (!argnc.to_complex(&val)) | |
0c77715b | 8195 | return false; |
e440a328 | 8196 | |
0c77715b | 8197 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
8198 | if (this->code_ == BUILTIN_REAL) | |
fcbea5e4 | 8199 | nc->set_float(type, mpc_realref(val)); |
0c77715b | 8200 | else |
fcbea5e4 | 8201 | nc->set_float(type, mpc_imagref(val)); |
8202 | mpc_clear(val); | |
0c77715b | 8203 | return true; |
e440a328 | 8204 | } |
0c77715b | 8205 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 8206 | { |
8207 | const Expression_list* args = this->args(); | |
8208 | if (args == NULL || args->size() != 2) | |
8209 | return false; | |
8210 | ||
0c77715b | 8211 | Numeric_constant rnc; |
8212 | if (!args->front()->numeric_constant_value(&rnc)) | |
8213 | return false; | |
8214 | Numeric_constant inc; | |
8215 | if (!args->back()->numeric_constant_value(&inc)) | |
8216 | return false; | |
8217 | ||
8218 | if (rnc.type() != NULL | |
8219 | && !rnc.type()->is_abstract() | |
8220 | && inc.type() != NULL | |
8221 | && !inc.type()->is_abstract() | |
8222 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
8223 | return false; | |
8224 | ||
e440a328 | 8225 | mpfr_t r; |
0c77715b | 8226 | if (!rnc.to_float(&r)) |
8227 | return false; | |
8228 | mpfr_t i; | |
8229 | if (!inc.to_float(&i)) | |
e440a328 | 8230 | { |
8231 | mpfr_clear(r); | |
8232 | return false; | |
8233 | } | |
8234 | ||
0c77715b | 8235 | Type* arg_type = rnc.type(); |
8236 | if (arg_type == NULL || arg_type->is_abstract()) | |
8237 | arg_type = inc.type(); | |
e440a328 | 8238 | |
fcbea5e4 | 8239 | mpc_t val; |
8240 | mpc_init2(val, mpc_precision); | |
8241 | mpc_set_fr_fr(val, r, i, MPC_RNDNN); | |
e440a328 | 8242 | mpfr_clear(r); |
8243 | mpfr_clear(i); | |
8244 | ||
fcbea5e4 | 8245 | Type* type = Builtin_call_expression::complex_type(arg_type); |
8246 | nc->set_complex(type, val); | |
8247 | ||
8248 | mpc_clear(val); | |
8249 | ||
0c77715b | 8250 | return true; |
e440a328 | 8251 | } |
8252 | ||
8253 | return false; | |
8254 | } | |
8255 | ||
a7549a6a | 8256 | // Give an error if we are discarding the value of an expression which |
8257 | // should not normally be discarded. We don't give an error for | |
8258 | // discarding the value of an ordinary function call, but we do for | |
8259 | // builtin functions, purely for consistency with the gc compiler. | |
8260 | ||
4f2138d7 | 8261 | bool |
a7549a6a | 8262 | Builtin_call_expression::do_discarding_value() |
8263 | { | |
8264 | switch (this->code_) | |
8265 | { | |
8266 | case BUILTIN_INVALID: | |
8267 | default: | |
8268 | go_unreachable(); | |
8269 | ||
8270 | case BUILTIN_APPEND: | |
8271 | case BUILTIN_CAP: | |
8272 | case BUILTIN_COMPLEX: | |
8273 | case BUILTIN_IMAG: | |
8274 | case BUILTIN_LEN: | |
8275 | case BUILTIN_MAKE: | |
8276 | case BUILTIN_NEW: | |
8277 | case BUILTIN_REAL: | |
8278 | case BUILTIN_ALIGNOF: | |
8279 | case BUILTIN_OFFSETOF: | |
8280 | case BUILTIN_SIZEOF: | |
8281 | this->unused_value_error(); | |
4f2138d7 | 8282 | return false; |
a7549a6a | 8283 | |
8284 | case BUILTIN_CLOSE: | |
8285 | case BUILTIN_COPY: | |
1cce762f | 8286 | case BUILTIN_DELETE: |
a7549a6a | 8287 | case BUILTIN_PANIC: |
8288 | case BUILTIN_PRINT: | |
8289 | case BUILTIN_PRINTLN: | |
8290 | case BUILTIN_RECOVER: | |
4f2138d7 | 8291 | return true; |
a7549a6a | 8292 | } |
8293 | } | |
8294 | ||
e440a328 | 8295 | // Return the type. |
8296 | ||
8297 | Type* | |
8298 | Builtin_call_expression::do_type() | |
8299 | { | |
79651b1f | 8300 | if (this->is_error_expression()) |
8301 | return Type::make_error_type(); | |
e440a328 | 8302 | switch (this->code_) |
8303 | { | |
8304 | case BUILTIN_INVALID: | |
8305 | default: | |
79651b1f | 8306 | return Type::make_error_type(); |
e440a328 | 8307 | |
8308 | case BUILTIN_NEW: | |
8309 | case BUILTIN_MAKE: | |
8310 | { | |
8311 | const Expression_list* args = this->args(); | |
8312 | if (args == NULL || args->empty()) | |
8313 | return Type::make_error_type(); | |
8314 | return Type::make_pointer_type(args->front()->type()); | |
8315 | } | |
8316 | ||
8317 | case BUILTIN_CAP: | |
8318 | case BUILTIN_COPY: | |
8319 | case BUILTIN_LEN: | |
7ba86326 | 8320 | return Type::lookup_integer_type("int"); |
8321 | ||
e440a328 | 8322 | case BUILTIN_ALIGNOF: |
8323 | case BUILTIN_OFFSETOF: | |
8324 | case BUILTIN_SIZEOF: | |
7ba86326 | 8325 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 8326 | |
8327 | case BUILTIN_CLOSE: | |
1cce762f | 8328 | case BUILTIN_DELETE: |
e440a328 | 8329 | case BUILTIN_PANIC: |
8330 | case BUILTIN_PRINT: | |
8331 | case BUILTIN_PRINTLN: | |
8332 | return Type::make_void_type(); | |
8333 | ||
e440a328 | 8334 | case BUILTIN_RECOVER: |
823c7e3d | 8335 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8336 | |
8337 | case BUILTIN_APPEND: | |
8338 | { | |
8339 | const Expression_list* args = this->args(); | |
8340 | if (args == NULL || args->empty()) | |
8341 | return Type::make_error_type(); | |
3ff4863b | 8342 | Type *ret = args->front()->type(); |
8343 | if (!ret->is_slice_type()) | |
8344 | return Type::make_error_type(); | |
8345 | return ret; | |
e440a328 | 8346 | } |
8347 | ||
8348 | case BUILTIN_REAL: | |
8349 | case BUILTIN_IMAG: | |
8350 | { | |
8351 | Expression* arg = this->one_arg(); | |
8352 | if (arg == NULL) | |
8353 | return Type::make_error_type(); | |
8354 | Type* t = arg->type(); | |
8355 | if (t->is_abstract()) | |
8356 | t = t->make_non_abstract_type(); | |
8357 | t = Builtin_call_expression::real_imag_type(t); | |
8358 | if (t == NULL) | |
8359 | t = Type::make_error_type(); | |
8360 | return t; | |
8361 | } | |
8362 | ||
48080209 | 8363 | case BUILTIN_COMPLEX: |
e440a328 | 8364 | { |
8365 | const Expression_list* args = this->args(); | |
8366 | if (args == NULL || args->size() != 2) | |
8367 | return Type::make_error_type(); | |
8368 | Type* t = args->front()->type(); | |
8369 | if (t->is_abstract()) | |
8370 | { | |
8371 | t = args->back()->type(); | |
8372 | if (t->is_abstract()) | |
8373 | t = t->make_non_abstract_type(); | |
8374 | } | |
48080209 | 8375 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 8376 | if (t == NULL) |
8377 | t = Type::make_error_type(); | |
8378 | return t; | |
8379 | } | |
8380 | } | |
8381 | } | |
8382 | ||
8383 | // Determine the type. | |
8384 | ||
8385 | void | |
8386 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
8387 | { | |
fb94b0ca | 8388 | if (!this->determining_types()) |
8389 | return; | |
8390 | ||
e440a328 | 8391 | this->fn()->determine_type_no_context(); |
8392 | ||
8393 | const Expression_list* args = this->args(); | |
8394 | ||
8395 | bool is_print; | |
8396 | Type* arg_type = NULL; | |
321e5ad2 | 8397 | Type* trailing_arg_types = NULL; |
e440a328 | 8398 | switch (this->code_) |
8399 | { | |
8400 | case BUILTIN_PRINT: | |
8401 | case BUILTIN_PRINTLN: | |
8402 | // Do not force a large integer constant to "int". | |
8403 | is_print = true; | |
8404 | break; | |
8405 | ||
8406 | case BUILTIN_REAL: | |
8407 | case BUILTIN_IMAG: | |
48080209 | 8408 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8409 | if (arg_type == NULL) |
8410 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8411 | is_print = false; |
8412 | break; | |
8413 | ||
48080209 | 8414 | case BUILTIN_COMPLEX: |
e440a328 | 8415 | { |
48080209 | 8416 | // For the complex function the type of one operand can |
e440a328 | 8417 | // determine the type of the other, as in a binary expression. |
8418 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8419 | if (arg_type == NULL) |
8420 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8421 | if (args != NULL && args->size() == 2) |
8422 | { | |
8423 | Type* t1 = args->front()->type(); | |
c849bb59 | 8424 | Type* t2 = args->back()->type(); |
e440a328 | 8425 | if (!t1->is_abstract()) |
8426 | arg_type = t1; | |
8427 | else if (!t2->is_abstract()) | |
8428 | arg_type = t2; | |
8429 | } | |
8430 | is_print = false; | |
8431 | } | |
8432 | break; | |
8433 | ||
321e5ad2 | 8434 | case BUILTIN_APPEND: |
8435 | if (!this->is_varargs() | |
8436 | && args != NULL | |
8437 | && !args->empty() | |
8438 | && args->front()->type()->is_slice_type()) | |
8439 | trailing_arg_types = | |
8440 | args->front()->type()->array_type()->element_type(); | |
8441 | is_print = false; | |
8442 | break; | |
8443 | ||
e440a328 | 8444 | default: |
8445 | is_print = false; | |
8446 | break; | |
8447 | } | |
8448 | ||
8449 | if (args != NULL) | |
8450 | { | |
8451 | for (Expression_list::const_iterator pa = args->begin(); | |
8452 | pa != args->end(); | |
8453 | ++pa) | |
8454 | { | |
8455 | Type_context subcontext; | |
8456 | subcontext.type = arg_type; | |
8457 | ||
8458 | if (is_print) | |
8459 | { | |
8460 | // We want to print large constants, we so can't just | |
8461 | // use the appropriate nonabstract type. Use uint64 for | |
8462 | // an integer if we know it is nonnegative, otherwise | |
8463 | // use int64 for a integer, otherwise use float64 for a | |
8464 | // float or complex128 for a complex. | |
8465 | Type* want_type = NULL; | |
8466 | Type* atype = (*pa)->type(); | |
8467 | if (atype->is_abstract()) | |
8468 | { | |
8469 | if (atype->integer_type() != NULL) | |
8470 | { | |
0c77715b | 8471 | Numeric_constant nc; |
8472 | if (this->numeric_constant_value(&nc)) | |
8473 | { | |
8474 | mpz_t val; | |
8475 | if (nc.to_int(&val)) | |
8476 | { | |
8477 | if (mpz_sgn(val) >= 0) | |
8478 | want_type = Type::lookup_integer_type("uint64"); | |
8479 | mpz_clear(val); | |
8480 | } | |
8481 | } | |
8482 | if (want_type == NULL) | |
e440a328 | 8483 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8484 | } |
8485 | else if (atype->float_type() != NULL) | |
8486 | want_type = Type::lookup_float_type("float64"); | |
8487 | else if (atype->complex_type() != NULL) | |
8488 | want_type = Type::lookup_complex_type("complex128"); | |
8489 | else if (atype->is_abstract_string_type()) | |
8490 | want_type = Type::lookup_string_type(); | |
8491 | else if (atype->is_abstract_boolean_type()) | |
8492 | want_type = Type::lookup_bool_type(); | |
8493 | else | |
c3e6f413 | 8494 | go_unreachable(); |
e440a328 | 8495 | subcontext.type = want_type; |
8496 | } | |
8497 | } | |
8498 | ||
8499 | (*pa)->determine_type(&subcontext); | |
321e5ad2 | 8500 | |
8501 | if (trailing_arg_types != NULL) | |
8502 | { | |
8503 | arg_type = trailing_arg_types; | |
8504 | trailing_arg_types = NULL; | |
8505 | } | |
e440a328 | 8506 | } |
8507 | } | |
8508 | } | |
8509 | ||
8510 | // If there is exactly one argument, return true. Otherwise give an | |
8511 | // error message and return false. | |
8512 | ||
8513 | bool | |
8514 | Builtin_call_expression::check_one_arg() | |
8515 | { | |
8516 | const Expression_list* args = this->args(); | |
8517 | if (args == NULL || args->size() < 1) | |
8518 | { | |
8519 | this->report_error(_("not enough arguments")); | |
8520 | return false; | |
8521 | } | |
8522 | else if (args->size() > 1) | |
8523 | { | |
8524 | this->report_error(_("too many arguments")); | |
8525 | return false; | |
8526 | } | |
8527 | if (args->front()->is_error_expression() | |
5c13bd80 | 8528 | || args->front()->type()->is_error()) |
e440a328 | 8529 | { |
8530 | this->set_is_error(); | |
8531 | return false; | |
8532 | } | |
8533 | return true; | |
8534 | } | |
8535 | ||
8536 | // Check argument types for a builtin function. | |
8537 | ||
8538 | void | |
8539 | Builtin_call_expression::do_check_types(Gogo*) | |
8540 | { | |
375646ea | 8541 | if (this->is_error_expression()) |
8542 | return; | |
e440a328 | 8543 | switch (this->code_) |
8544 | { | |
8545 | case BUILTIN_INVALID: | |
8546 | case BUILTIN_NEW: | |
8547 | case BUILTIN_MAKE: | |
cd238b8d | 8548 | case BUILTIN_DELETE: |
e440a328 | 8549 | return; |
8550 | ||
8551 | case BUILTIN_LEN: | |
8552 | case BUILTIN_CAP: | |
8553 | { | |
8554 | // The single argument may be either a string or an array or a | |
8555 | // map or a channel, or a pointer to a closed array. | |
8556 | if (this->check_one_arg()) | |
8557 | { | |
8558 | Type* arg_type = this->one_arg()->type(); | |
8559 | if (arg_type->points_to() != NULL | |
8560 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8561 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8562 | arg_type = arg_type->points_to(); |
8563 | if (this->code_ == BUILTIN_CAP) | |
8564 | { | |
5c13bd80 | 8565 | if (!arg_type->is_error() |
e440a328 | 8566 | && arg_type->array_type() == NULL |
8567 | && arg_type->channel_type() == NULL) | |
8568 | this->report_error(_("argument must be array or slice " | |
8569 | "or channel")); | |
8570 | } | |
8571 | else | |
8572 | { | |
5c13bd80 | 8573 | if (!arg_type->is_error() |
e440a328 | 8574 | && !arg_type->is_string_type() |
8575 | && arg_type->array_type() == NULL | |
8576 | && arg_type->map_type() == NULL | |
8577 | && arg_type->channel_type() == NULL) | |
8578 | this->report_error(_("argument must be string or " | |
8579 | "array or slice or map or channel")); | |
8580 | } | |
8581 | } | |
8582 | } | |
8583 | break; | |
8584 | ||
8585 | case BUILTIN_PRINT: | |
8586 | case BUILTIN_PRINTLN: | |
8587 | { | |
8588 | const Expression_list* args = this->args(); | |
8589 | if (args == NULL) | |
8590 | { | |
8591 | if (this->code_ == BUILTIN_PRINT) | |
631d5788 | 8592 | go_warning_at(this->location(), 0, |
e440a328 | 8593 | "no arguments for builtin function %<%s%>", |
8594 | (this->code_ == BUILTIN_PRINT | |
8595 | ? "print" | |
8596 | : "println")); | |
8597 | } | |
8598 | else | |
8599 | { | |
8600 | for (Expression_list::const_iterator p = args->begin(); | |
8601 | p != args->end(); | |
8602 | ++p) | |
8603 | { | |
8604 | Type* type = (*p)->type(); | |
5c13bd80 | 8605 | if (type->is_error() |
e440a328 | 8606 | || type->is_string_type() |
8607 | || type->integer_type() != NULL | |
8608 | || type->float_type() != NULL | |
8609 | || type->complex_type() != NULL | |
8610 | || type->is_boolean_type() | |
8611 | || type->points_to() != NULL | |
8612 | || type->interface_type() != NULL | |
8613 | || type->channel_type() != NULL | |
8614 | || type->map_type() != NULL | |
8615 | || type->function_type() != NULL | |
411eb89e | 8616 | || type->is_slice_type()) |
e440a328 | 8617 | ; |
acf8e158 | 8618 | else if ((*p)->is_type_expression()) |
8619 | { | |
8620 | // If this is a type expression it's going to give | |
8621 | // an error anyhow, so we don't need one here. | |
8622 | } | |
e440a328 | 8623 | else |
8624 | this->report_error(_("unsupported argument type to " | |
8625 | "builtin function")); | |
8626 | } | |
8627 | } | |
8628 | } | |
8629 | break; | |
8630 | ||
8631 | case BUILTIN_CLOSE: | |
e440a328 | 8632 | if (this->check_one_arg()) |
8633 | { | |
8634 | if (this->one_arg()->type()->channel_type() == NULL) | |
8635 | this->report_error(_("argument must be channel")); | |
5202d986 | 8636 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8637 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8638 | } |
8639 | break; | |
8640 | ||
8641 | case BUILTIN_PANIC: | |
8642 | case BUILTIN_SIZEOF: | |
8643 | case BUILTIN_ALIGNOF: | |
8644 | this->check_one_arg(); | |
8645 | break; | |
8646 | ||
8647 | case BUILTIN_RECOVER: | |
6334270b | 8648 | if (this->args() != NULL |
8649 | && !this->args()->empty() | |
8650 | && !this->recover_arg_is_set_) | |
e440a328 | 8651 | this->report_error(_("too many arguments")); |
8652 | break; | |
8653 | ||
8654 | case BUILTIN_OFFSETOF: | |
8655 | if (this->check_one_arg()) | |
8656 | { | |
8657 | Expression* arg = this->one_arg(); | |
8658 | if (arg->field_reference_expression() == NULL) | |
8659 | this->report_error(_("argument must be a field reference")); | |
8660 | } | |
8661 | break; | |
8662 | ||
8663 | case BUILTIN_COPY: | |
8664 | { | |
8665 | const Expression_list* args = this->args(); | |
8666 | if (args == NULL || args->size() < 2) | |
8667 | { | |
8668 | this->report_error(_("not enough arguments")); | |
8669 | break; | |
8670 | } | |
8671 | else if (args->size() > 2) | |
8672 | { | |
8673 | this->report_error(_("too many arguments")); | |
8674 | break; | |
8675 | } | |
8676 | Type* arg1_type = args->front()->type(); | |
8677 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8678 | if (arg1_type->is_error() || arg2_type->is_error()) |
6bebb39d | 8679 | { |
8680 | this->set_is_error(); | |
8681 | break; | |
8682 | } | |
e440a328 | 8683 | |
8684 | Type* e1; | |
411eb89e | 8685 | if (arg1_type->is_slice_type()) |
e440a328 | 8686 | e1 = arg1_type->array_type()->element_type(); |
8687 | else | |
8688 | { | |
8689 | this->report_error(_("left argument must be a slice")); | |
8690 | break; | |
8691 | } | |
8692 | ||
411eb89e | 8693 | if (arg2_type->is_slice_type()) |
60963afd | 8694 | { |
8695 | Type* e2 = arg2_type->array_type()->element_type(); | |
8696 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8697 | this->report_error(_("element types must be the same")); | |
8698 | } | |
e440a328 | 8699 | else if (arg2_type->is_string_type()) |
e440a328 | 8700 | { |
60963afd | 8701 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8702 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8703 | } |
60963afd | 8704 | else |
8705 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8706 | } |
8707 | break; | |
8708 | ||
8709 | case BUILTIN_APPEND: | |
8710 | { | |
8711 | const Expression_list* args = this->args(); | |
321e5ad2 | 8712 | if (args == NULL || args->empty()) |
e440a328 | 8713 | { |
8714 | this->report_error(_("not enough arguments")); | |
8715 | break; | |
8716 | } | |
321e5ad2 | 8717 | |
8718 | Type* slice_type = args->front()->type(); | |
8719 | if (!slice_type->is_slice_type()) | |
6bebb39d | 8720 | { |
321e5ad2 | 8721 | if (slice_type->is_error_type()) |
8722 | break; | |
8723 | if (slice_type->is_nil_type()) | |
8724 | go_error_at(args->front()->location(), "use of untyped nil"); | |
8725 | else | |
8726 | go_error_at(args->front()->location(), | |
8727 | "argument 1 must be a slice"); | |
6bebb39d | 8728 | this->set_is_error(); |
8729 | break; | |
8730 | } | |
cd238b8d | 8731 | |
321e5ad2 | 8732 | Type* element_type = slice_type->array_type()->element_type(); |
22deed0d | 8733 | if (!element_type->in_heap()) |
8734 | go_error_at(args->front()->location(), | |
8735 | "can't append to slice of go:notinheap type"); | |
321e5ad2 | 8736 | if (this->is_varargs()) |
4fd4fcf4 | 8737 | { |
321e5ad2 | 8738 | if (!args->back()->type()->is_slice_type() |
8739 | && !args->back()->type()->is_string_type()) | |
8740 | { | |
8741 | go_error_at(args->back()->location(), | |
8742 | "invalid use of %<...%> with non-slice/non-string"); | |
8743 | this->set_is_error(); | |
8744 | break; | |
8745 | } | |
4fd4fcf4 | 8746 | |
321e5ad2 | 8747 | if (args->size() < 2) |
8748 | { | |
8749 | this->report_error(_("not enough arguments")); | |
8750 | break; | |
8751 | } | |
8752 | if (args->size() > 2) | |
8753 | { | |
8754 | this->report_error(_("too many arguments")); | |
8755 | break; | |
8756 | } | |
8757 | ||
8758 | if (args->back()->type()->is_string_type() | |
8759 | && element_type->integer_type() != NULL | |
8760 | && element_type->integer_type()->is_byte()) | |
8761 | { | |
8762 | // Permit append(s1, s2...) when s1 is a slice of | |
8763 | // bytes and s2 is a string type. | |
8764 | } | |
e440a328 | 8765 | else |
8766 | { | |
321e5ad2 | 8767 | // We have to test for assignment compatibility to a |
8768 | // slice of the element type, which is not necessarily | |
8769 | // the same as the type of the first argument: the | |
8770 | // first argument might have a named type. | |
8771 | Type* check_type = Type::make_array_type(element_type, NULL); | |
8772 | std::string reason; | |
8773 | if (!Type::are_assignable(check_type, args->back()->type(), | |
8774 | &reason)) | |
8775 | { | |
8776 | if (reason.empty()) | |
8777 | go_error_at(args->back()->location(), | |
8778 | "argument 2 has invalid type"); | |
8779 | else | |
8780 | go_error_at(args->back()->location(), | |
8781 | "argument 2 has invalid type (%s)", | |
8782 | reason.c_str()); | |
8783 | this->set_is_error(); | |
8784 | break; | |
8785 | } | |
8786 | } | |
8787 | } | |
8788 | else | |
8789 | { | |
8790 | Expression_list::const_iterator pa = args->begin(); | |
8791 | int i = 2; | |
8792 | for (++pa; pa != args->end(); ++pa, ++i) | |
8793 | { | |
8794 | std::string reason; | |
8795 | if (!Type::are_assignable(element_type, (*pa)->type(), | |
8796 | &reason)) | |
8797 | { | |
8798 | if (reason.empty()) | |
8799 | go_error_at((*pa)->location(), | |
8800 | "argument %d has incompatible type", i); | |
8801 | else | |
8802 | go_error_at((*pa)->location(), | |
8803 | "argument %d has incompatible type (%s)", | |
8804 | i, reason.c_str()); | |
8805 | this->set_is_error(); | |
8806 | } | |
e440a328 | 8807 | } |
8808 | } | |
e440a328 | 8809 | } |
321e5ad2 | 8810 | break; |
e440a328 | 8811 | |
8812 | case BUILTIN_REAL: | |
8813 | case BUILTIN_IMAG: | |
8814 | if (this->check_one_arg()) | |
8815 | { | |
8816 | if (this->one_arg()->type()->complex_type() == NULL) | |
8817 | this->report_error(_("argument must have complex type")); | |
8818 | } | |
8819 | break; | |
8820 | ||
48080209 | 8821 | case BUILTIN_COMPLEX: |
e440a328 | 8822 | { |
8823 | const Expression_list* args = this->args(); | |
8824 | if (args == NULL || args->size() < 2) | |
8825 | this->report_error(_("not enough arguments")); | |
8826 | else if (args->size() > 2) | |
8827 | this->report_error(_("too many arguments")); | |
8828 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8829 | || args->front()->type()->is_error() |
e440a328 | 8830 | || args->back()->is_error_expression() |
5c13bd80 | 8831 | || args->back()->type()->is_error()) |
e440a328 | 8832 | this->set_is_error(); |
8833 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8834 | args->back()->type(), true, NULL)) |
48080209 | 8835 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8836 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8837 | this->report_error(_("complex arguments must have " |
e440a328 | 8838 | "floating-point type")); |
8839 | } | |
8840 | break; | |
8841 | ||
8842 | default: | |
c3e6f413 | 8843 | go_unreachable(); |
e440a328 | 8844 | } |
8845 | } | |
8846 | ||
72666aed | 8847 | Expression* |
8848 | Builtin_call_expression::do_copy() | |
8849 | { | |
8850 | Call_expression* bce = | |
8851 | new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
da244e59 | 8852 | (this->args() == NULL |
8853 | ? NULL | |
8854 | : this->args()->copy()), | |
72666aed | 8855 | this->is_varargs(), |
8856 | this->location()); | |
8857 | ||
8858 | if (this->varargs_are_lowered()) | |
8859 | bce->set_varargs_are_lowered(); | |
8860 | return bce; | |
8861 | } | |
8862 | ||
ea664253 | 8863 | // Return the backend representation for a builtin function. |
e440a328 | 8864 | |
ea664253 | 8865 | Bexpression* |
8866 | Builtin_call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 8867 | { |
8868 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8869 | Location location = this->location(); |
a0d8874e | 8870 | |
8871 | if (this->is_erroneous_call()) | |
8872 | { | |
8873 | go_assert(saw_errors()); | |
8874 | return gogo->backend()->error_expression(); | |
8875 | } | |
8876 | ||
e440a328 | 8877 | switch (this->code_) |
8878 | { | |
8879 | case BUILTIN_INVALID: | |
8880 | case BUILTIN_NEW: | |
8881 | case BUILTIN_MAKE: | |
c3e6f413 | 8882 | go_unreachable(); |
e440a328 | 8883 | |
8884 | case BUILTIN_LEN: | |
8885 | case BUILTIN_CAP: | |
8886 | { | |
8887 | const Expression_list* args = this->args(); | |
c484d925 | 8888 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8889 | Expression* arg = args->front(); |
e440a328 | 8890 | Type* arg_type = arg->type(); |
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; | |
0f914071 | 8898 | this->seen_ = false; |
e440a328 | 8899 | if (arg_type->points_to() != NULL) |
8900 | { | |
8901 | arg_type = arg_type->points_to(); | |
c484d925 | 8902 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8903 | && !arg_type->is_slice_type()); |
f614ea8b | 8904 | arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT, |
8905 | location); | |
e440a328 | 8906 | } |
8907 | ||
1b1f2abf | 8908 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8909 | Expression* val; |
e440a328 | 8910 | if (this->code_ == BUILTIN_LEN) |
8911 | { | |
8912 | if (arg_type->is_string_type()) | |
2c809f8f | 8913 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8914 | location); | |
e440a328 | 8915 | else if (arg_type->array_type() != NULL) |
0f914071 | 8916 | { |
8917 | if (this->seen_) | |
8918 | { | |
c484d925 | 8919 | go_assert(saw_errors()); |
ea664253 | 8920 | return context->backend()->error_expression(); |
0f914071 | 8921 | } |
8922 | this->seen_ = true; | |
2c809f8f | 8923 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8924 | this->seen_ = false; |
8925 | } | |
0d5530d9 | 8926 | else if (arg_type->map_type() != NULL |
8927 | || arg_type->channel_type() != NULL) | |
8928 | { | |
8929 | // The first field is the length. If the pointer is | |
8930 | // nil, the length is zero. | |
8931 | Type* pint_type = Type::make_pointer_type(int_type); | |
8932 | arg = Expression::make_unsafe_cast(pint_type, arg, location); | |
8933 | Expression* nil = Expression::make_nil(location); | |
8934 | nil = Expression::make_cast(pint_type, nil, location); | |
8935 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8936 | arg, nil, location); | |
8937 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8938 | location); | |
f614ea8b | 8939 | Expression* indir = |
8940 | Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, | |
8941 | location); | |
0d5530d9 | 8942 | val = Expression::make_conditional(cmp, zero, indir, location); |
8943 | } | |
e440a328 | 8944 | else |
c3e6f413 | 8945 | go_unreachable(); |
e440a328 | 8946 | } |
8947 | else | |
8948 | { | |
8949 | if (arg_type->array_type() != NULL) | |
0f914071 | 8950 | { |
8951 | if (this->seen_) | |
8952 | { | |
c484d925 | 8953 | go_assert(saw_errors()); |
ea664253 | 8954 | return context->backend()->error_expression(); |
0f914071 | 8955 | } |
8956 | this->seen_ = true; | |
2c809f8f | 8957 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8958 | this->seen_ = false; |
8959 | } | |
e440a328 | 8960 | else if (arg_type->channel_type() != NULL) |
132ed071 | 8961 | { |
8962 | // The second field is the capacity. If the pointer | |
8963 | // is nil, the capacity is zero. | |
8964 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
8965 | Type* pint_type = Type::make_pointer_type(int_type); | |
8966 | Expression* parg = Expression::make_unsafe_cast(uintptr_type, | |
8967 | arg, | |
8968 | location); | |
8969 | int off = int_type->integer_type()->bits() / 8; | |
8970 | Expression* eoff = Expression::make_integer_ul(off, | |
8971 | uintptr_type, | |
8972 | location); | |
8973 | parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff, | |
8974 | location); | |
8975 | parg = Expression::make_unsafe_cast(pint_type, parg, location); | |
8976 | Expression* nil = Expression::make_nil(location); | |
8977 | nil = Expression::make_cast(pint_type, nil, location); | |
8978 | Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, | |
8979 | arg, nil, location); | |
8980 | Expression* zero = Expression::make_integer_ul(0, int_type, | |
8981 | location); | |
f614ea8b | 8982 | Expression* indir = |
8983 | Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED, | |
8984 | location); | |
132ed071 | 8985 | val = Expression::make_conditional(cmp, zero, indir, location); |
8986 | } | |
e440a328 | 8987 | else |
c3e6f413 | 8988 | go_unreachable(); |
e440a328 | 8989 | } |
8990 | ||
2c809f8f | 8991 | return Expression::make_cast(int_type, val, |
ea664253 | 8992 | location)->get_backend(context); |
e440a328 | 8993 | } |
8994 | ||
8995 | case BUILTIN_PRINT: | |
8996 | case BUILTIN_PRINTLN: | |
8997 | { | |
8998 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
88b03a70 | 8999 | |
9000 | Expression* print_stmts = Runtime::make_call(Runtime::PRINTLOCK, | |
9001 | location, 0); | |
e440a328 | 9002 | |
9003 | const Expression_list* call_args = this->args(); | |
9004 | if (call_args != NULL) | |
9005 | { | |
9006 | for (Expression_list::const_iterator p = call_args->begin(); | |
9007 | p != call_args->end(); | |
9008 | ++p) | |
9009 | { | |
9010 | if (is_ln && p != call_args->begin()) | |
9011 | { | |
2c809f8f | 9012 | Expression* print_space = |
88b03a70 | 9013 | Runtime::make_call(Runtime::PRINTSP, location, 0); |
e440a328 | 9014 | |
2c809f8f | 9015 | print_stmts = |
9016 | Expression::make_compound(print_stmts, print_space, | |
9017 | location); | |
9018 | } | |
e440a328 | 9019 | |
2c809f8f | 9020 | Expression* arg = *p; |
9021 | Type* type = arg->type(); | |
9022 | Runtime::Function code; | |
e440a328 | 9023 | if (type->is_string_type()) |
88b03a70 | 9024 | code = Runtime::PRINTSTRING; |
e440a328 | 9025 | else if (type->integer_type() != NULL |
9026 | && type->integer_type()->is_unsigned()) | |
9027 | { | |
e440a328 | 9028 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 9029 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9030 | code = Runtime::PRINTUINT; |
e440a328 | 9031 | } |
9032 | else if (type->integer_type() != NULL) | |
9033 | { | |
e440a328 | 9034 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 9035 | arg = Expression::make_cast(itype, arg, location); |
88b03a70 | 9036 | code = Runtime::PRINTINT; |
e440a328 | 9037 | } |
9038 | else if (type->float_type() != NULL) | |
9039 | { | |
2c809f8f | 9040 | Type* dtype = Type::lookup_float_type("float64"); |
9041 | arg = Expression::make_cast(dtype, arg, location); | |
88b03a70 | 9042 | code = Runtime::PRINTFLOAT; |
e440a328 | 9043 | } |
9044 | else if (type->complex_type() != NULL) | |
9045 | { | |
2c809f8f | 9046 | Type* ctype = Type::lookup_complex_type("complex128"); |
9047 | arg = Expression::make_cast(ctype, arg, location); | |
88b03a70 | 9048 | code = Runtime::PRINTCOMPLEX; |
e440a328 | 9049 | } |
9050 | else if (type->is_boolean_type()) | |
88b03a70 | 9051 | code = Runtime::PRINTBOOL; |
e440a328 | 9052 | else if (type->points_to() != NULL |
9053 | || type->channel_type() != NULL | |
9054 | || type->map_type() != NULL | |
9055 | || type->function_type() != NULL) | |
9056 | { | |
2c809f8f | 9057 | arg = Expression::make_cast(type, arg, location); |
88b03a70 | 9058 | code = Runtime::PRINTPOINTER; |
e440a328 | 9059 | } |
9060 | else if (type->interface_type() != NULL) | |
9061 | { | |
9062 | if (type->interface_type()->is_empty()) | |
88b03a70 | 9063 | code = Runtime::PRINTEFACE; |
e440a328 | 9064 | else |
88b03a70 | 9065 | code = Runtime::PRINTIFACE; |
e440a328 | 9066 | } |
411eb89e | 9067 | else if (type->is_slice_type()) |
88b03a70 | 9068 | code = Runtime::PRINTSLICE; |
e440a328 | 9069 | else |
cd238b8d | 9070 | { |
9071 | go_assert(saw_errors()); | |
ea664253 | 9072 | return context->backend()->error_expression(); |
cd238b8d | 9073 | } |
e440a328 | 9074 | |
2c809f8f | 9075 | Expression* call = Runtime::make_call(code, location, 1, arg); |
88b03a70 | 9076 | print_stmts = Expression::make_compound(print_stmts, call, |
9077 | location); | |
e440a328 | 9078 | } |
9079 | } | |
9080 | ||
9081 | if (is_ln) | |
9082 | { | |
2c809f8f | 9083 | Expression* print_nl = |
88b03a70 | 9084 | Runtime::make_call(Runtime::PRINTNL, location, 0); |
9085 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
9086 | location); | |
e440a328 | 9087 | } |
9088 | ||
88b03a70 | 9089 | Expression* unlock = Runtime::make_call(Runtime::PRINTUNLOCK, |
9090 | location, 0); | |
9091 | print_stmts = Expression::make_compound(print_stmts, unlock, location); | |
32e3ff69 | 9092 | |
ea664253 | 9093 | return print_stmts->get_backend(context); |
e440a328 | 9094 | } |
9095 | ||
9096 | case BUILTIN_PANIC: | |
9097 | { | |
9098 | const Expression_list* args = this->args(); | |
c484d925 | 9099 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9100 | Expression* arg = args->front(); |
b13c66cd | 9101 | Type *empty = |
823c7e3d | 9102 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 9103 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
9104 | ||
9105 | Expression* panic = | |
03ac9de4 | 9106 | Runtime::make_call(Runtime::GOPANIC, location, 1, arg); |
ea664253 | 9107 | return panic->get_backend(context); |
e440a328 | 9108 | } |
9109 | ||
9110 | case BUILTIN_RECOVER: | |
9111 | { | |
9112 | // The argument is set when building recover thunks. It's a | |
9113 | // boolean value which is true if we can recover a value now. | |
9114 | const Expression_list* args = this->args(); | |
c484d925 | 9115 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9116 | Expression* arg = args->front(); |
b13c66cd | 9117 | Type *empty = |
823c7e3d | 9118 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 9119 | |
e440a328 | 9120 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 9121 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 9122 | |
9123 | // We need to handle a deferred call to recover specially, | |
9124 | // because it changes whether it can recover a panic or not. | |
9125 | // See test7 in test/recover1.go. | |
2c809f8f | 9126 | Expression* recover = Runtime::make_call((this->is_deferred() |
03ac9de4 | 9127 | ? Runtime::DEFERREDRECOVER |
9128 | : Runtime::GORECOVER), | |
2c809f8f | 9129 | location, 0); |
9130 | Expression* cond = | |
9131 | Expression::make_conditional(arg, recover, nil, location); | |
ea664253 | 9132 | return cond->get_backend(context); |
e440a328 | 9133 | } |
9134 | ||
9135 | case BUILTIN_CLOSE: | |
e440a328 | 9136 | { |
9137 | const Expression_list* args = this->args(); | |
c484d925 | 9138 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 9139 | Expression* arg = args->front(); |
2c809f8f | 9140 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
9141 | 1, arg); | |
ea664253 | 9142 | return close->get_backend(context); |
e440a328 | 9143 | } |
9144 | ||
9145 | case BUILTIN_SIZEOF: | |
9146 | case BUILTIN_OFFSETOF: | |
9147 | case BUILTIN_ALIGNOF: | |
9148 | { | |
0c77715b | 9149 | Numeric_constant nc; |
9150 | unsigned long val; | |
9151 | if (!this->numeric_constant_value(&nc) | |
9152 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 9153 | { |
c484d925 | 9154 | go_assert(saw_errors()); |
ea664253 | 9155 | return context->backend()->error_expression(); |
7f1d9abd | 9156 | } |
7ba86326 | 9157 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 9158 | mpz_t ival; |
9159 | nc.get_int(&ival); | |
9160 | Expression* int_cst = | |
e67508fa | 9161 | Expression::make_integer_z(&ival, uintptr_type, location); |
2c809f8f | 9162 | mpz_clear(ival); |
ea664253 | 9163 | return int_cst->get_backend(context); |
e440a328 | 9164 | } |
9165 | ||
9166 | case BUILTIN_COPY: | |
9167 | { | |
9168 | const Expression_list* args = this->args(); | |
c484d925 | 9169 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 9170 | Expression* arg1 = args->front(); |
9171 | Expression* arg2 = args->back(); | |
9172 | ||
e440a328 | 9173 | Type* arg1_type = arg1->type(); |
9174 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 9175 | go_assert(arg1->is_variable()); |
321e5ad2 | 9176 | |
9177 | Expression* call; | |
e440a328 | 9178 | |
9179 | Type* arg2_type = arg2->type(); | |
2c809f8f | 9180 | go_assert(arg2->is_variable()); |
321e5ad2 | 9181 | if (arg2_type->is_string_type()) |
9182 | call = Runtime::make_call(Runtime::SLICESTRINGCOPY, location, | |
9183 | 2, arg1, arg2); | |
e440a328 | 9184 | else |
9185 | { | |
321e5ad2 | 9186 | Type* et = at->element_type(); |
9187 | if (et->has_pointer()) | |
9188 | { | |
9189 | Expression* td = Expression::make_type_descriptor(et, | |
9190 | location); | |
9191 | call = Runtime::make_call(Runtime::TYPEDSLICECOPY, location, | |
9192 | 3, td, arg1, arg2); | |
9193 | } | |
9194 | else | |
9195 | { | |
9196 | Expression* sz = Expression::make_type_info(et, | |
9197 | TYPE_INFO_SIZE); | |
9198 | call = Runtime::make_call(Runtime::SLICECOPY, location, 3, | |
9199 | arg1, arg2, sz); | |
9200 | } | |
e440a328 | 9201 | } |
2c809f8f | 9202 | |
321e5ad2 | 9203 | return call->get_backend(context); |
e440a328 | 9204 | } |
9205 | ||
9206 | case BUILTIN_APPEND: | |
321e5ad2 | 9207 | // Handled in Builtin_call_expression::flatten_append. |
9208 | go_unreachable(); | |
e440a328 | 9209 | |
9210 | case BUILTIN_REAL: | |
9211 | case BUILTIN_IMAG: | |
9212 | { | |
9213 | const Expression_list* args = this->args(); | |
c484d925 | 9214 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 9215 | |
9216 | Bexpression* ret; | |
ea664253 | 9217 | Bexpression* bcomplex = args->front()->get_backend(context); |
2c809f8f | 9218 | if (this->code_ == BUILTIN_REAL) |
9219 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
9220 | else | |
9221 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
ea664253 | 9222 | return ret; |
e440a328 | 9223 | } |
9224 | ||
48080209 | 9225 | case BUILTIN_COMPLEX: |
e440a328 | 9226 | { |
9227 | const Expression_list* args = this->args(); | |
c484d925 | 9228 | go_assert(args != NULL && args->size() == 2); |
ea664253 | 9229 | Bexpression* breal = args->front()->get_backend(context); |
9230 | Bexpression* bimag = args->back()->get_backend(context); | |
9231 | return gogo->backend()->complex_expression(breal, bimag, location); | |
e440a328 | 9232 | } |
9233 | ||
9234 | default: | |
c3e6f413 | 9235 | go_unreachable(); |
e440a328 | 9236 | } |
9237 | } | |
9238 | ||
9239 | // We have to support exporting a builtin call expression, because | |
9240 | // code can set a constant to the result of a builtin expression. | |
9241 | ||
9242 | void | |
9243 | Builtin_call_expression::do_export(Export* exp) const | |
9244 | { | |
0c77715b | 9245 | Numeric_constant nc; |
9246 | if (!this->numeric_constant_value(&nc)) | |
9247 | { | |
631d5788 | 9248 | go_error_at(this->location(), "value is not constant"); |
0c77715b | 9249 | return; |
9250 | } | |
e440a328 | 9251 | |
0c77715b | 9252 | if (nc.is_int()) |
e440a328 | 9253 | { |
0c77715b | 9254 | mpz_t val; |
9255 | nc.get_int(&val); | |
e440a328 | 9256 | Integer_expression::export_integer(exp, val); |
0c77715b | 9257 | mpz_clear(val); |
e440a328 | 9258 | } |
0c77715b | 9259 | else if (nc.is_float()) |
e440a328 | 9260 | { |
9261 | mpfr_t fval; | |
0c77715b | 9262 | nc.get_float(&fval); |
9263 | Float_expression::export_float(exp, fval); | |
e440a328 | 9264 | mpfr_clear(fval); |
9265 | } | |
0c77715b | 9266 | else if (nc.is_complex()) |
e440a328 | 9267 | { |
fcbea5e4 | 9268 | mpc_t cval; |
9269 | nc.get_complex(&cval); | |
9270 | Complex_expression::export_complex(exp, cval); | |
9271 | mpc_clear(cval); | |
e440a328 | 9272 | } |
0c77715b | 9273 | else |
9274 | go_unreachable(); | |
e440a328 | 9275 | |
9276 | // A trailing space lets us reliably identify the end of the number. | |
9277 | exp->write_c_string(" "); | |
9278 | } | |
9279 | ||
9280 | // Class Call_expression. | |
9281 | ||
8381eda7 | 9282 | // A Go function can be viewed in a couple of different ways. The |
9283 | // code of a Go function becomes a backend function with parameters | |
9284 | // whose types are simply the backend representation of the Go types. | |
9285 | // If there are multiple results, they are returned as a backend | |
9286 | // struct. | |
9287 | ||
9288 | // However, when Go code refers to a function other than simply | |
9289 | // calling it, the backend type of that function is actually a struct. | |
9290 | // The first field of the struct points to the Go function code | |
9291 | // (sometimes a wrapper as described below). The remaining fields | |
9292 | // hold addresses of closed-over variables. This struct is called a | |
9293 | // closure. | |
9294 | ||
9295 | // There are a few cases to consider. | |
9296 | ||
9297 | // A direct function call of a known function in package scope. In | |
9298 | // this case there are no closed-over variables, and we know the name | |
9299 | // of the function code. We can simply produce a backend call to the | |
9300 | // function directly, and not worry about the closure. | |
9301 | ||
9302 | // A direct function call of a known function literal. In this case | |
9303 | // we know the function code and we know the closure. We generate the | |
9304 | // function code such that it expects an additional final argument of | |
9305 | // the closure type. We pass the closure as the last argument, after | |
9306 | // the other arguments. | |
9307 | ||
9308 | // An indirect function call. In this case we have a closure. We | |
9309 | // load the pointer to the function code from the first field of the | |
9310 | // closure. We pass the address of the closure as the last argument. | |
9311 | ||
9312 | // A call to a method of an interface. Type methods are always at | |
9313 | // package scope, so we call the function directly, and don't worry | |
9314 | // about the closure. | |
9315 | ||
9316 | // This means that for a function at package scope we have two cases. | |
9317 | // One is the direct call, which has no closure. The other is the | |
9318 | // indirect call, which does have a closure. We can't simply ignore | |
9319 | // the closure, even though it is the last argument, because that will | |
9320 | // fail on targets where the function pops its arguments. So when | |
9321 | // generating a closure for a package-scope function we set the | |
9322 | // function code pointer in the closure to point to a wrapper | |
9323 | // function. This wrapper function accepts a final argument that | |
9324 | // points to the closure, ignores it, and calls the real function as a | |
9325 | // direct function call. This wrapper will normally be efficient, and | |
9326 | // can often simply be a tail call to the real function. | |
9327 | ||
9328 | // We don't use GCC's static chain pointer because 1) we don't need | |
9329 | // it; 2) GCC only permits using a static chain to call a known | |
9330 | // function, so we can't use it for an indirect call anyhow. Since we | |
9331 | // can't use it for an indirect call, we may as well not worry about | |
9332 | // using it for a direct call either. | |
9333 | ||
9334 | // We pass the closure last rather than first because it means that | |
9335 | // the function wrapper we put into a closure for a package-scope | |
9336 | // function can normally just be a tail call to the real function. | |
9337 | ||
9338 | // For method expressions we generate a wrapper that loads the | |
9339 | // receiver from the closure and then calls the method. This | |
9340 | // unfortunately forces reshuffling the arguments, since there is a | |
9341 | // new first argument, but we can't avoid reshuffling either for | |
9342 | // method expressions or for indirect calls of package-scope | |
9343 | // functions, and since the latter are more common we reshuffle for | |
9344 | // method expressions. | |
9345 | ||
9346 | // Note that the Go code retains the Go types. The extra final | |
9347 | // argument only appears when we convert to the backend | |
9348 | // representation. | |
9349 | ||
e440a328 | 9350 | // Traversal. |
9351 | ||
9352 | int | |
9353 | Call_expression::do_traverse(Traverse* traverse) | |
9354 | { | |
0c0dacab | 9355 | // If we are calling a function in a different package that returns |
9356 | // an unnamed type, this may be the only chance we get to traverse | |
9357 | // that type. We don't traverse this->type_ because it may be a | |
9358 | // Call_multiple_result_type that will just lead back here. | |
9359 | if (this->type_ != NULL && !this->type_->is_error_type()) | |
9360 | { | |
9361 | Function_type *fntype = this->get_function_type(); | |
9362 | if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT) | |
9363 | return TRAVERSE_EXIT; | |
9364 | } | |
e440a328 | 9365 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) |
9366 | return TRAVERSE_EXIT; | |
9367 | if (this->args_ != NULL) | |
9368 | { | |
9369 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
9370 | return TRAVERSE_EXIT; | |
9371 | } | |
9372 | return TRAVERSE_CONTINUE; | |
9373 | } | |
9374 | ||
9375 | // Lower a call statement. | |
9376 | ||
9377 | Expression* | |
ceeb4318 | 9378 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
9379 | Statement_inserter* inserter, int) | |
e440a328 | 9380 | { |
b13c66cd | 9381 | Location loc = this->location(); |
09ea332d | 9382 | |
ceeb4318 | 9383 | // A type cast can look like a function call. |
e440a328 | 9384 | if (this->fn_->is_type_expression() |
9385 | && this->args_ != NULL | |
9386 | && this->args_->size() == 1) | |
9387 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 9388 | loc); |
e440a328 | 9389 | |
88f06749 | 9390 | // Because do_type will return an error type and thus prevent future |
9391 | // errors, check for that case now to ensure that the error gets | |
9392 | // reported. | |
37448b10 | 9393 | Function_type* fntype = this->get_function_type(); |
9394 | if (fntype == NULL) | |
88f06749 | 9395 | { |
9396 | if (!this->fn_->type()->is_error()) | |
9397 | this->report_error(_("expected function")); | |
5f1045b5 | 9398 | this->set_is_error(); |
9399 | return this; | |
88f06749 | 9400 | } |
9401 | ||
e440a328 | 9402 | // Handle an argument which is a call to a function which returns |
9403 | // multiple results. | |
9404 | if (this->args_ != NULL | |
9405 | && this->args_->size() == 1 | |
37448b10 | 9406 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9407 | { |
e440a328 | 9408 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9409 | if (rc > 1 | |
37448b10 | 9410 | && ((fntype->parameters() != NULL |
9411 | && (fntype->parameters()->size() == rc | |
9412 | || (fntype->is_varargs() | |
9413 | && fntype->parameters()->size() - 1 <= rc))) | |
9414 | || fntype->is_builtin())) | |
e440a328 | 9415 | { |
9416 | Call_expression* call = this->args_->front()->call_expression(); | |
e90ecd2d | 9417 | call->set_is_multi_value_arg(); |
c33af8e4 | 9418 | if (this->is_varargs_) |
9419 | { | |
9420 | // It is not clear which result of a multiple result call | |
9421 | // the ellipsis operator should be applied to. If we unpack the | |
9422 | // the call into its individual results here, the ellipsis will be | |
9423 | // applied to the last result. | |
631d5788 | 9424 | go_error_at(call->location(), |
9425 | _("multiple-value argument in single-value context")); | |
c33af8e4 | 9426 | return Expression::make_error(call->location()); |
9427 | } | |
9428 | ||
e440a328 | 9429 | Expression_list* args = new Expression_list; |
9430 | for (size_t i = 0; i < rc; ++i) | |
9431 | args->push_back(Expression::make_call_result(call, i)); | |
9432 | // We can't return a new call expression here, because this | |
42535814 | 9433 | // one may be referenced by Call_result expressions. We |
9434 | // also can't delete the old arguments, because we may still | |
9435 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9436 | this->args_ = args; |
9437 | } | |
9438 | } | |
9439 | ||
37448b10 | 9440 | // Recognize a call to a builtin function. |
9441 | if (fntype->is_builtin()) | |
9442 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9443 | this->is_varargs_, loc); | |
9444 | ||
ceeb4318 | 9445 | // If this call returns multiple results, create a temporary |
5731103c | 9446 | // variable to hold them. |
9447 | if (this->result_count() > 1 && this->call_temp_ == NULL) | |
ceeb4318 | 9448 | { |
5731103c | 9449 | Struct_field_list* sfl = new Struct_field_list(); |
9450 | Function_type* fntype = this->get_function_type(); | |
37448b10 | 9451 | const Typed_identifier_list* results = fntype->results(); |
5731103c | 9452 | Location loc = this->location(); |
9453 | ||
9454 | int i = 0; | |
9455 | char buf[20]; | |
ceeb4318 | 9456 | for (Typed_identifier_list::const_iterator p = results->begin(); |
5731103c | 9457 | p != results->end(); |
9458 | ++p, ++i) | |
9459 | { | |
9460 | snprintf(buf, sizeof buf, "res%d", i); | |
9461 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9462 | } | |
9463 | ||
9464 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9465 | st->set_is_struct_incomparable(); | |
9466 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9467 | inserter->insert(this->call_temp_); | |
ceeb4318 | 9468 | } |
9469 | ||
e440a328 | 9470 | // Handle a call to a varargs function by packaging up the extra |
9471 | // parameters. | |
37448b10 | 9472 | if (fntype->is_varargs()) |
e440a328 | 9473 | { |
e440a328 | 9474 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9475 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9476 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9477 | this->lower_varargs(gogo, function, inserter, varargs_type, |
0e9a2e72 | 9478 | parameters->size(), SLICE_STORAGE_MAY_ESCAPE); |
09ea332d | 9479 | } |
9480 | ||
9481 | // If this is call to a method, call the method directly passing the | |
9482 | // object as the first parameter. | |
9483 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9484 | if (bme != NULL) | |
9485 | { | |
0afbb937 | 9486 | Named_object* methodfn = bme->function(); |
09ea332d | 9487 | Expression* first_arg = bme->first_argument(); |
9488 | ||
9489 | // We always pass a pointer when calling a method. | |
9490 | if (first_arg->type()->points_to() == NULL | |
9491 | && !first_arg->type()->is_error()) | |
9492 | { | |
9493 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9494 | // We may need to create a temporary variable so that we can | |
9495 | // take the address. We can't do that here because it will | |
9496 | // mess up the order of evaluation. | |
9497 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9498 | ue->set_create_temp(); | |
9499 | } | |
9500 | ||
9501 | // If we are calling a method which was inherited from an | |
9502 | // embedded struct, and the method did not get a stub, then the | |
9503 | // first type may be wrong. | |
9504 | Type* fatype = bme->first_argument_type(); | |
9505 | if (fatype != NULL) | |
9506 | { | |
9507 | if (fatype->points_to() == NULL) | |
9508 | fatype = Type::make_pointer_type(fatype); | |
9509 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9510 | } | |
9511 | ||
9512 | Expression_list* new_args = new Expression_list(); | |
9513 | new_args->push_back(first_arg); | |
9514 | if (this->args_ != NULL) | |
9515 | { | |
9516 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9517 | p != this->args_->end(); | |
9518 | ++p) | |
9519 | new_args->push_back(*p); | |
9520 | } | |
9521 | ||
9522 | // We have to change in place because this structure may be | |
9523 | // referenced by Call_result_expressions. We can't delete the | |
9524 | // old arguments, because we may be traversing them up in some | |
9525 | // caller. FIXME. | |
9526 | this->args_ = new_args; | |
0afbb937 | 9527 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9528 | bme->location()); |
e440a328 | 9529 | } |
9530 | ||
105f9a24 | 9531 | // Handle a couple of special runtime functions. In the runtime |
9532 | // package, getcallerpc returns the PC of the caller, and | |
9533 | // getcallersp returns the frame pointer of the caller. Implement | |
9534 | // these by turning them into calls to GCC builtin functions. We | |
9535 | // could implement them in normal code, but then we would have to | |
9536 | // explicitly unwind the stack. These functions are intended to be | |
9537 | // efficient. Note that this technique obviously only works for | |
33d1d391 | 9538 | // direct calls, but that is the only way they are used. |
9539 | if (gogo->compiling_runtime() && gogo->package_name() == "runtime") | |
105f9a24 | 9540 | { |
9541 | Func_expression* fe = this->fn_->func_expression(); | |
9542 | if (fe != NULL | |
9543 | && fe->named_object()->is_function_declaration() | |
9544 | && fe->named_object()->package() == NULL) | |
9545 | { | |
9546 | std::string n = Gogo::unpack_hidden_name(fe->named_object()->name()); | |
33d1d391 | 9547 | if ((this->args_ == NULL || this->args_->size() == 0) |
9548 | && n == "getcallerpc") | |
105f9a24 | 9549 | { |
9550 | static Named_object* builtin_return_address; | |
9551 | return this->lower_to_builtin(&builtin_return_address, | |
9552 | "__builtin_return_address", | |
9553 | 0); | |
9554 | } | |
33d1d391 | 9555 | else if (this->args_ != NULL |
9556 | && this->args_->size() == 1 | |
9557 | && n == "getcallersp") | |
105f9a24 | 9558 | { |
33d1d391 | 9559 | // The actual argument to getcallersp is always the |
9560 | // address of a parameter; we don't need that for the | |
9561 | // GCC builtin function, so we just ignore it. | |
105f9a24 | 9562 | static Named_object* builtin_frame_address; |
9563 | return this->lower_to_builtin(&builtin_frame_address, | |
9564 | "__builtin_frame_address", | |
9565 | 1); | |
9566 | } | |
9567 | } | |
9568 | } | |
9569 | ||
e440a328 | 9570 | return this; |
9571 | } | |
9572 | ||
9573 | // Lower a call to a varargs function. FUNCTION is the function in | |
9574 | // which the call occurs--it's not the function we are calling. | |
9575 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9576 | // PARAM_COUNT is the number of parameters of the function we are | |
9577 | // calling; the last of these parameters will be the varargs | |
9578 | // parameter. | |
9579 | ||
09ea332d | 9580 | void |
e440a328 | 9581 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9582 | Statement_inserter* inserter, |
0e9a2e72 | 9583 | Type* varargs_type, size_t param_count, |
9584 | Slice_storage_escape_disp escape_disp) | |
e440a328 | 9585 | { |
9586 | if (this->varargs_are_lowered_) | |
09ea332d | 9587 | return; |
e440a328 | 9588 | |
b13c66cd | 9589 | Location loc = this->location(); |
e440a328 | 9590 | |
c484d925 | 9591 | go_assert(param_count > 0); |
411eb89e | 9592 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9593 | |
9594 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9595 | if (arg_count < param_count - 1) | |
9596 | { | |
9597 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9598 | return; |
e440a328 | 9599 | } |
9600 | ||
9601 | Expression_list* old_args = this->args_; | |
9602 | Expression_list* new_args = new Expression_list(); | |
9603 | bool push_empty_arg = false; | |
9604 | if (old_args == NULL || old_args->empty()) | |
9605 | { | |
c484d925 | 9606 | go_assert(param_count == 1); |
e440a328 | 9607 | push_empty_arg = true; |
9608 | } | |
9609 | else | |
9610 | { | |
9611 | Expression_list::const_iterator pa; | |
9612 | int i = 1; | |
9613 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9614 | { | |
9615 | if (static_cast<size_t>(i) == param_count) | |
9616 | break; | |
9617 | new_args->push_back(*pa); | |
9618 | } | |
9619 | ||
9620 | // We have reached the varargs parameter. | |
9621 | ||
9622 | bool issued_error = false; | |
9623 | if (pa == old_args->end()) | |
9624 | push_empty_arg = true; | |
9625 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9626 | new_args->push_back(*pa); | |
9627 | else if (this->is_varargs_) | |
9628 | { | |
a6645f74 | 9629 | if ((*pa)->type()->is_slice_type()) |
9630 | this->report_error(_("too many arguments")); | |
9631 | else | |
9632 | { | |
631d5788 | 9633 | go_error_at(this->location(), |
9634 | _("invalid use of %<...%> with non-slice")); | |
a6645f74 | 9635 | this->set_is_error(); |
9636 | } | |
09ea332d | 9637 | return; |
e440a328 | 9638 | } |
e440a328 | 9639 | else |
9640 | { | |
9641 | Type* element_type = varargs_type->array_type()->element_type(); | |
9642 | Expression_list* vals = new Expression_list; | |
9643 | for (; pa != old_args->end(); ++pa, ++i) | |
9644 | { | |
9645 | // Check types here so that we get a better message. | |
9646 | Type* patype = (*pa)->type(); | |
b13c66cd | 9647 | Location paloc = (*pa)->location(); |
e440a328 | 9648 | if (!this->check_argument_type(i, element_type, patype, |
9649 | paloc, issued_error)) | |
9650 | continue; | |
9651 | vals->push_back(*pa); | |
9652 | } | |
0e9a2e72 | 9653 | Slice_construction_expression* sce = |
e440a328 | 9654 | Expression::make_slice_composite_literal(varargs_type, vals, loc); |
0e9a2e72 | 9655 | if (escape_disp == SLICE_STORAGE_DOES_NOT_ESCAPE) |
9656 | sce->set_storage_does_not_escape(); | |
9657 | Expression* val = sce; | |
09ea332d | 9658 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9659 | new_args->push_back(val); |
9660 | } | |
9661 | } | |
9662 | ||
9663 | if (push_empty_arg) | |
9664 | new_args->push_back(Expression::make_nil(loc)); | |
9665 | ||
9666 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9667 | // be referenced by Call_result expressions. FIXME. We can't |
9668 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9669 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9670 | this->args_ = new_args; |
9671 | this->varargs_are_lowered_ = true; | |
e440a328 | 9672 | } |
9673 | ||
105f9a24 | 9674 | // Return a call to __builtin_return_address or __builtin_frame_address. |
9675 | ||
9676 | Expression* | |
9677 | Call_expression::lower_to_builtin(Named_object** pno, const char* name, | |
9678 | int arg) | |
9679 | { | |
9680 | if (*pno == NULL) | |
9681 | *pno = Gogo::declare_builtin_rf_address(name); | |
9682 | ||
9683 | Location loc = this->location(); | |
9684 | ||
9685 | Expression* fn = Expression::make_func_reference(*pno, NULL, loc); | |
9686 | Expression* a = Expression::make_integer_ul(arg, NULL, loc); | |
9687 | Expression_list *args = new Expression_list(); | |
9688 | args->push_back(a); | |
9689 | Expression* call = Expression::make_call(fn, args, false, loc); | |
9690 | ||
9691 | // The builtin functions return void*, but the Go functions return uintptr. | |
9692 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
9693 | return Expression::make_cast(uintptr_type, call, loc); | |
9694 | } | |
9695 | ||
2c809f8f | 9696 | // Flatten a call with multiple results into a temporary. |
9697 | ||
9698 | Expression* | |
b8e86a51 | 9699 | Call_expression::do_flatten(Gogo* gogo, Named_object*, |
9700 | Statement_inserter* inserter) | |
2c809f8f | 9701 | { |
5bf8be8b | 9702 | if (this->is_erroneous_call()) |
9703 | { | |
9704 | go_assert(saw_errors()); | |
9705 | return Expression::make_error(this->location()); | |
9706 | } | |
b8e86a51 | 9707 | |
91c0fd76 | 9708 | if (this->is_flattened_) |
9709 | return this; | |
9710 | this->is_flattened_ = true; | |
9711 | ||
b8e86a51 | 9712 | // Add temporary variables for all arguments that require type |
9713 | // conversion. | |
9714 | Function_type* fntype = this->get_function_type(); | |
9782d556 | 9715 | if (fntype == NULL) |
9716 | { | |
9717 | go_assert(saw_errors()); | |
9718 | return this; | |
9719 | } | |
b8e86a51 | 9720 | if (this->args_ != NULL && !this->args_->empty() |
9721 | && fntype->parameters() != NULL && !fntype->parameters()->empty()) | |
9722 | { | |
9723 | bool is_interface_method = | |
9724 | this->fn_->interface_field_reference_expression() != NULL; | |
9725 | ||
9726 | Expression_list *args = new Expression_list(); | |
9727 | Typed_identifier_list::const_iterator pp = fntype->parameters()->begin(); | |
9728 | Expression_list::const_iterator pa = this->args_->begin(); | |
9729 | if (!is_interface_method && fntype->is_method()) | |
9730 | { | |
9731 | // The receiver argument. | |
9732 | args->push_back(*pa); | |
9733 | ++pa; | |
9734 | } | |
9735 | for (; pa != this->args_->end(); ++pa, ++pp) | |
9736 | { | |
9737 | go_assert(pp != fntype->parameters()->end()); | |
9738 | if (Type::are_identical(pp->type(), (*pa)->type(), true, NULL)) | |
9739 | args->push_back(*pa); | |
9740 | else | |
9741 | { | |
9742 | Location loc = (*pa)->location(); | |
8ba8cc87 | 9743 | Expression* arg = *pa; |
9744 | if (!arg->is_variable()) | |
9745 | { | |
9746 | Temporary_statement *temp = | |
9747 | Statement::make_temporary(NULL, arg, loc); | |
9748 | inserter->insert(temp); | |
9749 | arg = Expression::make_temporary_reference(temp, loc); | |
9750 | } | |
9751 | arg = Expression::convert_for_assignment(gogo, pp->type(), arg, | |
9752 | loc); | |
9753 | args->push_back(arg); | |
b8e86a51 | 9754 | } |
9755 | } | |
9756 | delete this->args_; | |
9757 | this->args_ = args; | |
9758 | } | |
9759 | ||
2c809f8f | 9760 | return this; |
9761 | } | |
9762 | ||
ceeb4318 | 9763 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9764 | |
9765 | Function_type* | |
9766 | Call_expression::get_function_type() const | |
9767 | { | |
9768 | return this->fn_->type()->function_type(); | |
9769 | } | |
9770 | ||
9771 | // Return the number of values which this call will return. | |
9772 | ||
9773 | size_t | |
9774 | Call_expression::result_count() const | |
9775 | { | |
9776 | const Function_type* fntype = this->get_function_type(); | |
9777 | if (fntype == NULL) | |
9778 | return 0; | |
9779 | if (fntype->results() == NULL) | |
9780 | return 0; | |
9781 | return fntype->results()->size(); | |
9782 | } | |
9783 | ||
5731103c | 9784 | // Return the temporary that holds the result for a call with multiple |
9785 | // results. | |
ceeb4318 | 9786 | |
9787 | Temporary_statement* | |
5731103c | 9788 | Call_expression::results() const |
ceeb4318 | 9789 | { |
5731103c | 9790 | if (this->call_temp_ == NULL) |
cd238b8d | 9791 | { |
9792 | go_assert(saw_errors()); | |
9793 | return NULL; | |
9794 | } | |
5731103c | 9795 | return this->call_temp_; |
ceeb4318 | 9796 | } |
9797 | ||
1373401e | 9798 | // Set the number of results expected from a call expression. |
9799 | ||
9800 | void | |
9801 | Call_expression::set_expected_result_count(size_t count) | |
9802 | { | |
9803 | go_assert(this->expected_result_count_ == 0); | |
9804 | this->expected_result_count_ = count; | |
9805 | } | |
9806 | ||
e440a328 | 9807 | // Return whether this is a call to the predeclared function recover. |
9808 | ||
9809 | bool | |
9810 | Call_expression::is_recover_call() const | |
9811 | { | |
9812 | return this->do_is_recover_call(); | |
9813 | } | |
9814 | ||
9815 | // Set the argument to the recover function. | |
9816 | ||
9817 | void | |
9818 | Call_expression::set_recover_arg(Expression* arg) | |
9819 | { | |
9820 | this->do_set_recover_arg(arg); | |
9821 | } | |
9822 | ||
9823 | // Virtual functions also implemented by Builtin_call_expression. | |
9824 | ||
9825 | bool | |
9826 | Call_expression::do_is_recover_call() const | |
9827 | { | |
9828 | return false; | |
9829 | } | |
9830 | ||
9831 | void | |
9832 | Call_expression::do_set_recover_arg(Expression*) | |
9833 | { | |
c3e6f413 | 9834 | go_unreachable(); |
e440a328 | 9835 | } |
9836 | ||
ceeb4318 | 9837 | // We have found an error with this call expression; return true if |
9838 | // we should report it. | |
9839 | ||
9840 | bool | |
9841 | Call_expression::issue_error() | |
9842 | { | |
9843 | if (this->issued_error_) | |
9844 | return false; | |
9845 | else | |
9846 | { | |
9847 | this->issued_error_ = true; | |
9848 | return true; | |
9849 | } | |
9850 | } | |
9851 | ||
5bf8be8b | 9852 | // Whether or not this call contains errors, either in the call or the |
9853 | // arguments to the call. | |
9854 | ||
9855 | bool | |
9856 | Call_expression::is_erroneous_call() | |
9857 | { | |
9858 | if (this->is_error_expression() || this->fn()->is_error_expression()) | |
9859 | return true; | |
9860 | ||
9861 | if (this->args() == NULL) | |
9862 | return false; | |
9863 | for (Expression_list::iterator pa = this->args()->begin(); | |
9864 | pa != this->args()->end(); | |
9865 | ++pa) | |
9866 | { | |
9867 | if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression()) | |
9868 | return true; | |
9869 | } | |
9870 | return false; | |
9871 | } | |
9872 | ||
e440a328 | 9873 | // Get the type. |
9874 | ||
9875 | Type* | |
9876 | Call_expression::do_type() | |
9877 | { | |
9878 | if (this->type_ != NULL) | |
9879 | return this->type_; | |
9880 | ||
9881 | Type* ret; | |
9882 | Function_type* fntype = this->get_function_type(); | |
9883 | if (fntype == NULL) | |
9884 | return Type::make_error_type(); | |
9885 | ||
9886 | const Typed_identifier_list* results = fntype->results(); | |
9887 | if (results == NULL) | |
9888 | ret = Type::make_void_type(); | |
9889 | else if (results->size() == 1) | |
9890 | ret = results->begin()->type(); | |
9891 | else | |
9892 | ret = Type::make_call_multiple_result_type(this); | |
9893 | ||
9894 | this->type_ = ret; | |
9895 | ||
9896 | return this->type_; | |
9897 | } | |
9898 | ||
9899 | // Determine types for a call expression. We can use the function | |
9900 | // parameter types to set the types of the arguments. | |
9901 | ||
9902 | void | |
9903 | Call_expression::do_determine_type(const Type_context*) | |
9904 | { | |
fb94b0ca | 9905 | if (!this->determining_types()) |
9906 | return; | |
9907 | ||
e440a328 | 9908 | this->fn_->determine_type_no_context(); |
9909 | Function_type* fntype = this->get_function_type(); | |
9910 | const Typed_identifier_list* parameters = NULL; | |
9911 | if (fntype != NULL) | |
9912 | parameters = fntype->parameters(); | |
9913 | if (this->args_ != NULL) | |
9914 | { | |
9915 | Typed_identifier_list::const_iterator pt; | |
9916 | if (parameters != NULL) | |
9917 | pt = parameters->begin(); | |
09ea332d | 9918 | bool first = true; |
e440a328 | 9919 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9920 | pa != this->args_->end(); | |
9921 | ++pa) | |
9922 | { | |
09ea332d | 9923 | if (first) |
9924 | { | |
9925 | first = false; | |
9926 | // If this is a method, the first argument is the | |
9927 | // receiver. | |
9928 | if (fntype != NULL && fntype->is_method()) | |
9929 | { | |
9930 | Type* rtype = fntype->receiver()->type(); | |
9931 | // The receiver is always passed as a pointer. | |
9932 | if (rtype->points_to() == NULL) | |
9933 | rtype = Type::make_pointer_type(rtype); | |
9934 | Type_context subcontext(rtype, false); | |
9935 | (*pa)->determine_type(&subcontext); | |
9936 | continue; | |
9937 | } | |
9938 | } | |
9939 | ||
e440a328 | 9940 | if (parameters != NULL && pt != parameters->end()) |
9941 | { | |
9942 | Type_context subcontext(pt->type(), false); | |
9943 | (*pa)->determine_type(&subcontext); | |
9944 | ++pt; | |
9945 | } | |
9946 | else | |
9947 | (*pa)->determine_type_no_context(); | |
9948 | } | |
9949 | } | |
9950 | } | |
9951 | ||
fb94b0ca | 9952 | // Called when determining types for a Call_expression. Return true |
9953 | // if we should go ahead, false if they have already been determined. | |
9954 | ||
9955 | bool | |
9956 | Call_expression::determining_types() | |
9957 | { | |
9958 | if (this->types_are_determined_) | |
9959 | return false; | |
9960 | else | |
9961 | { | |
9962 | this->types_are_determined_ = true; | |
9963 | return true; | |
9964 | } | |
9965 | } | |
9966 | ||
e440a328 | 9967 | // Check types for parameter I. |
9968 | ||
9969 | bool | |
9970 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9971 | const Type* argument_type, | |
b13c66cd | 9972 | Location argument_location, |
e440a328 | 9973 | bool issued_error) |
9974 | { | |
9975 | std::string reason; | |
1eae365b | 9976 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) |
e440a328 | 9977 | { |
9978 | if (!issued_error) | |
9979 | { | |
9980 | if (reason.empty()) | |
631d5788 | 9981 | go_error_at(argument_location, "argument %d has incompatible type", i); |
e440a328 | 9982 | else |
631d5788 | 9983 | go_error_at(argument_location, |
9984 | "argument %d has incompatible type (%s)", | |
9985 | i, reason.c_str()); | |
e440a328 | 9986 | } |
9987 | this->set_is_error(); | |
9988 | return false; | |
9989 | } | |
9990 | return true; | |
9991 | } | |
9992 | ||
9993 | // Check types. | |
9994 | ||
9995 | void | |
9996 | Call_expression::do_check_types(Gogo*) | |
9997 | { | |
a6645f74 | 9998 | if (this->classification() == EXPRESSION_ERROR) |
9999 | return; | |
10000 | ||
e440a328 | 10001 | Function_type* fntype = this->get_function_type(); |
10002 | if (fntype == NULL) | |
10003 | { | |
5c13bd80 | 10004 | if (!this->fn_->type()->is_error()) |
e440a328 | 10005 | this->report_error(_("expected function")); |
10006 | return; | |
10007 | } | |
10008 | ||
1373401e | 10009 | if (this->expected_result_count_ != 0 |
10010 | && this->expected_result_count_ != this->result_count()) | |
10011 | { | |
10012 | if (this->issue_error()) | |
10013 | this->report_error(_("function result count mismatch")); | |
10014 | this->set_is_error(); | |
10015 | return; | |
10016 | } | |
10017 | ||
09ea332d | 10018 | bool is_method = fntype->is_method(); |
10019 | if (is_method) | |
e440a328 | 10020 | { |
09ea332d | 10021 | go_assert(this->args_ != NULL && !this->args_->empty()); |
10022 | Type* rtype = fntype->receiver()->type(); | |
10023 | Expression* first_arg = this->args_->front(); | |
1eae365b | 10024 | // We dereference the values since receivers are always passed |
10025 | // as pointers. | |
09ea332d | 10026 | std::string reason; |
1eae365b | 10027 | if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(), |
10028 | &reason)) | |
e440a328 | 10029 | { |
09ea332d | 10030 | if (reason.empty()) |
10031 | this->report_error(_("incompatible type for receiver")); | |
10032 | else | |
e440a328 | 10033 | { |
631d5788 | 10034 | go_error_at(this->location(), |
10035 | "incompatible type for receiver (%s)", | |
10036 | reason.c_str()); | |
09ea332d | 10037 | this->set_is_error(); |
e440a328 | 10038 | } |
10039 | } | |
10040 | } | |
10041 | ||
10042 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 10043 | // we don't have to worry about it here unless something is wrong. |
10044 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
10045 | { | |
10046 | if (!fntype->is_varargs()) | |
10047 | { | |
631d5788 | 10048 | go_error_at(this->location(), |
10049 | _("invalid use of %<...%> calling non-variadic function")); | |
a6645f74 | 10050 | this->set_is_error(); |
10051 | return; | |
10052 | } | |
10053 | } | |
e440a328 | 10054 | |
10055 | const Typed_identifier_list* parameters = fntype->parameters(); | |
33d1d391 | 10056 | if (this->args_ == NULL || this->args_->size() == 0) |
e440a328 | 10057 | { |
10058 | if (parameters != NULL && !parameters->empty()) | |
10059 | this->report_error(_("not enough arguments")); | |
10060 | } | |
10061 | else if (parameters == NULL) | |
09ea332d | 10062 | { |
10063 | if (!is_method || this->args_->size() > 1) | |
10064 | this->report_error(_("too many arguments")); | |
10065 | } | |
1373401e | 10066 | else if (this->args_->size() == 1 |
10067 | && this->args_->front()->call_expression() != NULL | |
10068 | && this->args_->front()->call_expression()->result_count() > 1) | |
10069 | { | |
10070 | // This is F(G()) when G returns more than one result. If the | |
10071 | // results can be matched to parameters, it would have been | |
10072 | // lowered in do_lower. If we get here we know there is a | |
10073 | // mismatch. | |
10074 | if (this->args_->front()->call_expression()->result_count() | |
10075 | < parameters->size()) | |
10076 | this->report_error(_("not enough arguments")); | |
10077 | else | |
10078 | this->report_error(_("too many arguments")); | |
10079 | } | |
e440a328 | 10080 | else |
10081 | { | |
10082 | int i = 0; | |
09ea332d | 10083 | Expression_list::const_iterator pa = this->args_->begin(); |
10084 | if (is_method) | |
10085 | ++pa; | |
10086 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
10087 | pt != parameters->end(); | |
10088 | ++pt, ++pa, ++i) | |
e440a328 | 10089 | { |
09ea332d | 10090 | if (pa == this->args_->end()) |
e440a328 | 10091 | { |
09ea332d | 10092 | this->report_error(_("not enough arguments")); |
e440a328 | 10093 | return; |
10094 | } | |
10095 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
10096 | (*pa)->location(), false); | |
10097 | } | |
09ea332d | 10098 | if (pa != this->args_->end()) |
10099 | this->report_error(_("too many arguments")); | |
e440a328 | 10100 | } |
10101 | } | |
10102 | ||
72666aed | 10103 | Expression* |
10104 | Call_expression::do_copy() | |
10105 | { | |
10106 | Call_expression* call = | |
10107 | Expression::make_call(this->fn_->copy(), | |
10108 | (this->args_ == NULL | |
10109 | ? NULL | |
10110 | : this->args_->copy()), | |
10111 | this->is_varargs_, this->location()); | |
10112 | ||
10113 | if (this->varargs_are_lowered_) | |
10114 | call->set_varargs_are_lowered(); | |
10115 | return call; | |
10116 | } | |
10117 | ||
e440a328 | 10118 | // Return whether we have to use a temporary variable to ensure that |
10119 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 10120 | // results then it will inevitably be executed last. |
e440a328 | 10121 | |
10122 | bool | |
10123 | Call_expression::do_must_eval_in_order() const | |
10124 | { | |
ceeb4318 | 10125 | return this->result_count() > 0; |
e440a328 | 10126 | } |
10127 | ||
e440a328 | 10128 | // Get the function and the first argument to use when calling an |
10129 | // interface method. | |
10130 | ||
2387f644 | 10131 | Expression* |
e440a328 | 10132 | Call_expression::interface_method_function( |
e440a328 | 10133 | Interface_field_reference_expression* interface_method, |
db122cb9 | 10134 | Expression** first_arg_ptr, |
10135 | Location location) | |
e440a328 | 10136 | { |
db122cb9 | 10137 | Expression* object = interface_method->get_underlying_object(); |
10138 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); | |
10139 | *first_arg_ptr = | |
10140 | Expression::make_unsafe_cast(unsafe_ptr_type, object, location); | |
2387f644 | 10141 | return interface_method->get_function(); |
e440a328 | 10142 | } |
10143 | ||
10144 | // Build the call expression. | |
10145 | ||
ea664253 | 10146 | Bexpression* |
10147 | Call_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10148 | { |
5731103c | 10149 | Location location = this->location(); |
10150 | ||
2c809f8f | 10151 | if (this->call_ != NULL) |
5731103c | 10152 | { |
10153 | // If the call returns multiple results, make a new reference to | |
10154 | // the temporary. | |
10155 | if (this->call_temp_ != NULL) | |
10156 | { | |
10157 | Expression* ref = | |
10158 | Expression::make_temporary_reference(this->call_temp_, location); | |
10159 | return ref->get_backend(context); | |
10160 | } | |
10161 | ||
10162 | return this->call_; | |
10163 | } | |
e440a328 | 10164 | |
10165 | Function_type* fntype = this->get_function_type(); | |
10166 | if (fntype == NULL) | |
ea664253 | 10167 | return context->backend()->error_expression(); |
e440a328 | 10168 | |
10169 | if (this->fn_->is_error_expression()) | |
ea664253 | 10170 | return context->backend()->error_expression(); |
e440a328 | 10171 | |
10172 | Gogo* gogo = context->gogo(); | |
e440a328 | 10173 | |
10174 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 10175 | Interface_field_reference_expression* interface_method = |
10176 | this->fn_->interface_field_reference_expression(); | |
10177 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 10178 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 10179 | |
f8bdf81a | 10180 | bool has_closure_arg; |
8381eda7 | 10181 | if (has_closure) |
f8bdf81a | 10182 | has_closure_arg = true; |
8381eda7 | 10183 | else if (func != NULL) |
f8bdf81a | 10184 | has_closure_arg = false; |
8381eda7 | 10185 | else if (is_interface_method) |
f8bdf81a | 10186 | has_closure_arg = false; |
8381eda7 | 10187 | else |
f8bdf81a | 10188 | has_closure_arg = true; |
8381eda7 | 10189 | |
e440a328 | 10190 | int nargs; |
2c809f8f | 10191 | std::vector<Bexpression*> fn_args; |
e440a328 | 10192 | if (this->args_ == NULL || this->args_->empty()) |
10193 | { | |
f8bdf81a | 10194 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 10195 | if (nargs > 0) |
10196 | fn_args.resize(1); | |
e440a328 | 10197 | } |
09ea332d | 10198 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
10199 | { | |
10200 | // Passing a receiver parameter. | |
10201 | go_assert(!is_interface_method | |
10202 | && fntype->is_method() | |
10203 | && this->args_->size() == 1); | |
f8bdf81a | 10204 | nargs = 1; |
2c809f8f | 10205 | fn_args.resize(1); |
ea664253 | 10206 | fn_args[0] = this->args_->front()->get_backend(context); |
09ea332d | 10207 | } |
e440a328 | 10208 | else |
10209 | { | |
10210 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 10211 | |
10212 | nargs = this->args_->size(); | |
09ea332d | 10213 | int i = is_interface_method ? 1 : 0; |
e440a328 | 10214 | nargs += i; |
2c809f8f | 10215 | fn_args.resize(nargs); |
e440a328 | 10216 | |
10217 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 10218 | Expression_list::const_iterator pe = this->args_->begin(); |
10219 | if (!is_interface_method && fntype->is_method()) | |
10220 | { | |
ea664253 | 10221 | fn_args[i] = (*pe)->get_backend(context); |
09ea332d | 10222 | ++pe; |
10223 | ++i; | |
10224 | } | |
10225 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 10226 | { |
c484d925 | 10227 | go_assert(pp != params->end()); |
2c809f8f | 10228 | Expression* arg = |
10229 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
10230 | location); | |
ea664253 | 10231 | fn_args[i] = arg->get_backend(context); |
e440a328 | 10232 | } |
c484d925 | 10233 | go_assert(pp == params->end()); |
f8bdf81a | 10234 | go_assert(i == nargs); |
e440a328 | 10235 | } |
10236 | ||
2c809f8f | 10237 | Expression* fn; |
10238 | Expression* closure = NULL; | |
8381eda7 | 10239 | if (func != NULL) |
10240 | { | |
10241 | Named_object* no = func->named_object(); | |
2c809f8f | 10242 | fn = Expression::make_func_code_reference(no, location); |
10243 | if (has_closure) | |
10244 | closure = func->closure(); | |
8381eda7 | 10245 | } |
09ea332d | 10246 | else if (!is_interface_method) |
8381eda7 | 10247 | { |
2c809f8f | 10248 | closure = this->fn_; |
10249 | ||
10250 | // The backend representation of this function type is a pointer | |
10251 | // to a struct whose first field is the actual function to call. | |
10252 | Type* pfntype = | |
10253 | Type::make_pointer_type( | |
10254 | Type::make_pointer_type(Type::make_void_type())); | |
10255 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
f614ea8b | 10256 | fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location); |
2c809f8f | 10257 | } |
e440a328 | 10258 | else |
cf609de4 | 10259 | { |
2387f644 | 10260 | Expression* first_arg; |
db122cb9 | 10261 | fn = this->interface_method_function(interface_method, &first_arg, |
10262 | location); | |
ea664253 | 10263 | fn_args[0] = first_arg->get_backend(context); |
e440a328 | 10264 | } |
10265 | ||
1ecc6157 | 10266 | Bexpression* bclosure = NULL; |
10267 | if (has_closure_arg) | |
10268 | bclosure = closure->get_backend(context); | |
f8bdf81a | 10269 | else |
1ecc6157 | 10270 | go_assert(closure == NULL); |
f8bdf81a | 10271 | |
ea664253 | 10272 | Bexpression* bfn = fn->get_backend(context); |
80d1e1a8 | 10273 | |
10274 | // When not calling a named function directly, use a type conversion | |
10275 | // in case the type of the function is a recursive type which refers | |
10276 | // to itself. We don't do this for an interface method because 1) | |
10277 | // an interface method never refers to itself, so we always have a | |
10278 | // function type here; 2) we pass an extra first argument to an | |
10279 | // interface method, so fntype is not correct. | |
10280 | if (func == NULL && !is_interface_method) | |
10281 | { | |
10282 | Btype* bft = fntype->get_backend_fntype(gogo); | |
10283 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
10284 | } | |
10285 | ||
4ced7af9 | 10286 | Bfunction* bfunction = NULL; |
10287 | if (context->function()) | |
10288 | bfunction = context->function()->func_value()->get_decl(); | |
10289 | Bexpression* call = gogo->backend()->call_expression(bfunction, bfn, | |
10290 | fn_args, bclosure, | |
10291 | location); | |
e440a328 | 10292 | |
5731103c | 10293 | if (this->call_temp_ != NULL) |
e440a328 | 10294 | { |
5731103c | 10295 | // This case occurs when the call returns multiple results. |
e440a328 | 10296 | |
5731103c | 10297 | Expression* ref = Expression::make_temporary_reference(this->call_temp_, |
10298 | location); | |
10299 | Bexpression* bref = ref->get_backend(context); | |
10300 | Bstatement* bassn = gogo->backend()->assignment_statement(bfunction, | |
10301 | bref, call, | |
10302 | location); | |
e440a328 | 10303 | |
5731103c | 10304 | ref = Expression::make_temporary_reference(this->call_temp_, location); |
10305 | this->call_ = ref->get_backend(context); | |
10306 | ||
10307 | return gogo->backend()->compound_expression(bassn, this->call_, | |
10308 | location); | |
2c809f8f | 10309 | } |
e440a328 | 10310 | |
2c809f8f | 10311 | this->call_ = call; |
ea664253 | 10312 | return this->call_; |
e440a328 | 10313 | } |
10314 | ||
d751bb78 | 10315 | // Dump ast representation for a call expressin. |
10316 | ||
10317 | void | |
10318 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
10319 | { | |
10320 | this->fn_->dump_expression(ast_dump_context); | |
10321 | ast_dump_context->ostream() << "("; | |
10322 | if (args_ != NULL) | |
10323 | ast_dump_context->dump_expression_list(this->args_); | |
10324 | ||
10325 | ast_dump_context->ostream() << ") "; | |
10326 | } | |
10327 | ||
e440a328 | 10328 | // Make a call expression. |
10329 | ||
10330 | Call_expression* | |
10331 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 10332 | Location location) |
e440a328 | 10333 | { |
10334 | return new Call_expression(fn, args, is_varargs, location); | |
10335 | } | |
10336 | ||
da244e59 | 10337 | // Class Call_result_expression. |
e440a328 | 10338 | |
10339 | // Traverse a call result. | |
10340 | ||
10341 | int | |
10342 | Call_result_expression::do_traverse(Traverse* traverse) | |
10343 | { | |
10344 | if (traverse->remember_expression(this->call_)) | |
10345 | { | |
10346 | // We have already traversed the call expression. | |
10347 | return TRAVERSE_CONTINUE; | |
10348 | } | |
10349 | return Expression::traverse(&this->call_, traverse); | |
10350 | } | |
10351 | ||
10352 | // Get the type. | |
10353 | ||
10354 | Type* | |
10355 | Call_result_expression::do_type() | |
10356 | { | |
425dd051 | 10357 | if (this->classification() == EXPRESSION_ERROR) |
10358 | return Type::make_error_type(); | |
10359 | ||
e440a328 | 10360 | // THIS->CALL_ can be replaced with a temporary reference due to |
10361 | // Call_expression::do_must_eval_in_order when there is an error. | |
10362 | Call_expression* ce = this->call_->call_expression(); | |
10363 | if (ce == NULL) | |
5e85f268 | 10364 | { |
10365 | this->set_is_error(); | |
10366 | return Type::make_error_type(); | |
10367 | } | |
e440a328 | 10368 | Function_type* fntype = ce->get_function_type(); |
10369 | if (fntype == NULL) | |
5e85f268 | 10370 | { |
e37658e2 | 10371 | if (ce->issue_error()) |
99b3f06f | 10372 | { |
10373 | if (!ce->fn()->type()->is_error()) | |
10374 | this->report_error(_("expected function")); | |
10375 | } | |
5e85f268 | 10376 | this->set_is_error(); |
10377 | return Type::make_error_type(); | |
10378 | } | |
e440a328 | 10379 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10380 | if (results == NULL || results->size() < 2) |
7b8d861f | 10381 | { |
ceeb4318 | 10382 | if (ce->issue_error()) |
10383 | this->report_error(_("number of results does not match " | |
10384 | "number of values")); | |
7b8d861f | 10385 | return Type::make_error_type(); |
10386 | } | |
e440a328 | 10387 | Typed_identifier_list::const_iterator pr = results->begin(); |
10388 | for (unsigned int i = 0; i < this->index_; ++i) | |
10389 | { | |
10390 | if (pr == results->end()) | |
425dd051 | 10391 | break; |
e440a328 | 10392 | ++pr; |
10393 | } | |
10394 | if (pr == results->end()) | |
425dd051 | 10395 | { |
ceeb4318 | 10396 | if (ce->issue_error()) |
10397 | this->report_error(_("number of results does not match " | |
10398 | "number of values")); | |
425dd051 | 10399 | return Type::make_error_type(); |
10400 | } | |
e440a328 | 10401 | return pr->type(); |
10402 | } | |
10403 | ||
425dd051 | 10404 | // Check the type. Just make sure that we trigger the warning in |
10405 | // do_type. | |
e440a328 | 10406 | |
10407 | void | |
10408 | Call_result_expression::do_check_types(Gogo*) | |
10409 | { | |
425dd051 | 10410 | this->type(); |
e440a328 | 10411 | } |
10412 | ||
10413 | // Determine the type. We have nothing to do here, but the 0 result | |
10414 | // needs to pass down to the caller. | |
10415 | ||
10416 | void | |
10417 | Call_result_expression::do_determine_type(const Type_context*) | |
10418 | { | |
fb94b0ca | 10419 | this->call_->determine_type_no_context(); |
e440a328 | 10420 | } |
10421 | ||
ea664253 | 10422 | // Return the backend representation. We just refer to the temporary set by the |
10423 | // call expression. We don't do this at lowering time because it makes it | |
ceeb4318 | 10424 | // hard to evaluate the call at the right time. |
e440a328 | 10425 | |
ea664253 | 10426 | Bexpression* |
10427 | Call_result_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10428 | { |
ceeb4318 | 10429 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10430 | if (ce == NULL) |
10431 | { | |
10432 | go_assert(this->call_->is_error_expression()); | |
ea664253 | 10433 | return context->backend()->error_expression(); |
cd238b8d | 10434 | } |
5731103c | 10435 | Temporary_statement* ts = ce->results(); |
cd238b8d | 10436 | if (ts == NULL) |
10437 | { | |
10438 | go_assert(saw_errors()); | |
ea664253 | 10439 | return context->backend()->error_expression(); |
cd238b8d | 10440 | } |
ceeb4318 | 10441 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
5731103c | 10442 | ref = Expression::make_field_reference(ref, this->index_, this->location()); |
ea664253 | 10443 | return ref->get_backend(context); |
e440a328 | 10444 | } |
10445 | ||
d751bb78 | 10446 | // Dump ast representation for a call result expression. |
10447 | ||
10448 | void | |
10449 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10450 | const | |
10451 | { | |
10452 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10453 | // (struct) and the fields are referenced instead. | |
10454 | ast_dump_context->ostream() << this->index_ << "@("; | |
10455 | ast_dump_context->dump_expression(this->call_); | |
10456 | ast_dump_context->ostream() << ")"; | |
10457 | } | |
10458 | ||
e440a328 | 10459 | // Make a reference to a single result of a call which returns |
10460 | // multiple results. | |
10461 | ||
10462 | Expression* | |
10463 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10464 | { | |
10465 | return new Call_result_expression(call, index); | |
10466 | } | |
10467 | ||
10468 | // Class Index_expression. | |
10469 | ||
10470 | // Traversal. | |
10471 | ||
10472 | int | |
10473 | Index_expression::do_traverse(Traverse* traverse) | |
10474 | { | |
10475 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10476 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10477 | || (this->end_ != NULL | |
acf2b673 | 10478 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
10479 | || (this->cap_ != NULL | |
10480 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 10481 | return TRAVERSE_EXIT; |
10482 | return TRAVERSE_CONTINUE; | |
10483 | } | |
10484 | ||
10485 | // Lower an index expression. This converts the generic index | |
10486 | // expression into an array index, a string index, or a map index. | |
10487 | ||
10488 | Expression* | |
ceeb4318 | 10489 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10490 | { |
b13c66cd | 10491 | Location location = this->location(); |
e440a328 | 10492 | Expression* left = this->left_; |
10493 | Expression* start = this->start_; | |
10494 | Expression* end = this->end_; | |
acf2b673 | 10495 | Expression* cap = this->cap_; |
e440a328 | 10496 | |
10497 | Type* type = left->type(); | |
5c13bd80 | 10498 | if (type->is_error()) |
d9f3743a | 10499 | { |
10500 | go_assert(saw_errors()); | |
10501 | return Expression::make_error(location); | |
10502 | } | |
b0cf7ddd | 10503 | else if (left->is_type_expression()) |
10504 | { | |
631d5788 | 10505 | go_error_at(location, "attempt to index type expression"); |
b0cf7ddd | 10506 | return Expression::make_error(location); |
10507 | } | |
e440a328 | 10508 | else if (type->array_type() != NULL) |
acf2b673 | 10509 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 10510 | else if (type->points_to() != NULL |
10511 | && type->points_to()->array_type() != NULL | |
411eb89e | 10512 | && !type->points_to()->is_slice_type()) |
e440a328 | 10513 | { |
f614ea8b | 10514 | Expression* deref = |
10515 | Expression::make_dereference(left, NIL_CHECK_DEFAULT, location); | |
38092374 | 10516 | |
10517 | // For an ordinary index into the array, the pointer will be | |
10518 | // dereferenced. For a slice it will not--the resulting slice | |
10519 | // will simply reuse the pointer, which is incorrect if that | |
10520 | // pointer is nil. | |
10521 | if (end != NULL || cap != NULL) | |
10522 | deref->issue_nil_check(); | |
10523 | ||
acf2b673 | 10524 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 10525 | } |
10526 | else if (type->is_string_type()) | |
acf2b673 | 10527 | { |
10528 | if (cap != NULL) | |
10529 | { | |
631d5788 | 10530 | go_error_at(location, "invalid 3-index slice of string"); |
acf2b673 | 10531 | return Expression::make_error(location); |
10532 | } | |
10533 | return Expression::make_string_index(left, start, end, location); | |
10534 | } | |
e440a328 | 10535 | else if (type->map_type() != NULL) |
10536 | { | |
acf2b673 | 10537 | if (end != NULL || cap != NULL) |
e440a328 | 10538 | { |
631d5788 | 10539 | go_error_at(location, "invalid slice of map"); |
e440a328 | 10540 | return Expression::make_error(location); |
10541 | } | |
0d5530d9 | 10542 | return Expression::make_map_index(left, start, location); |
e440a328 | 10543 | } |
b1aba207 | 10544 | else if (cap != NULL) |
10545 | { | |
10546 | go_error_at(location, | |
10547 | "invalid 3-index slice of object that is not a slice"); | |
10548 | return Expression::make_error(location); | |
10549 | } | |
10550 | else if (end != NULL) | |
10551 | { | |
10552 | go_error_at(location, | |
10553 | ("attempt to slice object that is not " | |
10554 | "array, slice, or string")); | |
10555 | return Expression::make_error(location); | |
10556 | } | |
e440a328 | 10557 | else |
10558 | { | |
631d5788 | 10559 | go_error_at(location, |
b1aba207 | 10560 | ("attempt to index object that is not " |
10561 | "array, slice, string, or map")); | |
e440a328 | 10562 | return Expression::make_error(location); |
10563 | } | |
10564 | } | |
10565 | ||
acf2b673 | 10566 | // Write an indexed expression |
10567 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 10568 | |
10569 | void | |
10570 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10571 | const Expression* expr, | |
10572 | const Expression* start, | |
acf2b673 | 10573 | const Expression* end, |
10574 | const Expression* cap) | |
d751bb78 | 10575 | { |
10576 | expr->dump_expression(ast_dump_context); | |
10577 | ast_dump_context->ostream() << "["; | |
10578 | start->dump_expression(ast_dump_context); | |
10579 | if (end != NULL) | |
10580 | { | |
10581 | ast_dump_context->ostream() << ":"; | |
10582 | end->dump_expression(ast_dump_context); | |
10583 | } | |
acf2b673 | 10584 | if (cap != NULL) |
10585 | { | |
10586 | ast_dump_context->ostream() << ":"; | |
10587 | cap->dump_expression(ast_dump_context); | |
10588 | } | |
d751bb78 | 10589 | ast_dump_context->ostream() << "]"; |
10590 | } | |
10591 | ||
10592 | // Dump ast representation for an index expression. | |
10593 | ||
10594 | void | |
10595 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10596 | const | |
10597 | { | |
10598 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 10599 | this->start_, this->end_, this->cap_); |
d751bb78 | 10600 | } |
10601 | ||
e440a328 | 10602 | // Make an index expression. |
10603 | ||
10604 | Expression* | |
10605 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 10606 | Expression* cap, Location location) |
e440a328 | 10607 | { |
acf2b673 | 10608 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 10609 | } |
10610 | ||
da244e59 | 10611 | // Class Array_index_expression. |
e440a328 | 10612 | |
10613 | // Array index traversal. | |
10614 | ||
10615 | int | |
10616 | Array_index_expression::do_traverse(Traverse* traverse) | |
10617 | { | |
10618 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10619 | return TRAVERSE_EXIT; | |
10620 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10621 | return TRAVERSE_EXIT; | |
10622 | if (this->end_ != NULL) | |
10623 | { | |
10624 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10625 | return TRAVERSE_EXIT; | |
10626 | } | |
acf2b673 | 10627 | if (this->cap_ != NULL) |
10628 | { | |
10629 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10630 | return TRAVERSE_EXIT; | |
10631 | } | |
e440a328 | 10632 | return TRAVERSE_CONTINUE; |
10633 | } | |
10634 | ||
10635 | // Return the type of an array index. | |
10636 | ||
10637 | Type* | |
10638 | Array_index_expression::do_type() | |
10639 | { | |
10640 | if (this->type_ == NULL) | |
10641 | { | |
10642 | Array_type* type = this->array_->type()->array_type(); | |
10643 | if (type == NULL) | |
10644 | this->type_ = Type::make_error_type(); | |
10645 | else if (this->end_ == NULL) | |
10646 | this->type_ = type->element_type(); | |
411eb89e | 10647 | else if (type->is_slice_type()) |
e440a328 | 10648 | { |
10649 | // A slice of a slice has the same type as the original | |
10650 | // slice. | |
10651 | this->type_ = this->array_->type()->deref(); | |
10652 | } | |
10653 | else | |
10654 | { | |
10655 | // A slice of an array is a slice. | |
10656 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10657 | } | |
10658 | } | |
10659 | return this->type_; | |
10660 | } | |
10661 | ||
10662 | // Set the type of an array index. | |
10663 | ||
10664 | void | |
10665 | Array_index_expression::do_determine_type(const Type_context*) | |
10666 | { | |
10667 | this->array_->determine_type_no_context(); | |
f77aa642 | 10668 | |
10669 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
10670 | if (this->start_->is_constant()) | |
10671 | this->start_->determine_type(&index_context); | |
10672 | else | |
10673 | this->start_->determine_type_no_context(); | |
e440a328 | 10674 | if (this->end_ != NULL) |
f77aa642 | 10675 | { |
10676 | if (this->end_->is_constant()) | |
10677 | this->end_->determine_type(&index_context); | |
10678 | else | |
10679 | this->end_->determine_type_no_context(); | |
10680 | } | |
acf2b673 | 10681 | if (this->cap_ != NULL) |
f77aa642 | 10682 | { |
10683 | if (this->cap_->is_constant()) | |
10684 | this->cap_->determine_type(&index_context); | |
10685 | else | |
10686 | this->cap_->determine_type_no_context(); | |
10687 | } | |
e440a328 | 10688 | } |
10689 | ||
10690 | // Check types of an array index. | |
10691 | ||
10692 | void | |
b7327dbf | 10693 | Array_index_expression::do_check_types(Gogo*) |
e440a328 | 10694 | { |
f6bc81e6 | 10695 | Numeric_constant nc; |
10696 | unsigned long v; | |
10697 | if (this->start_->type()->integer_type() == NULL | |
10698 | && !this->start_->type()->is_error() | |
10699 | && (!this->start_->numeric_constant_value(&nc) | |
10700 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10701 | this->report_error(_("index must be integer")); |
10702 | if (this->end_ != NULL | |
10703 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10704 | && !this->end_->type()->is_error() |
10705 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10706 | && !this->end_->is_error_expression() |
10707 | && (!this->end_->numeric_constant_value(&nc) | |
10708 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10709 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10710 | if (this->cap_ != NULL |
10711 | && this->cap_->type()->integer_type() == NULL | |
10712 | && !this->cap_->type()->is_error() | |
10713 | && !this->cap_->is_nil_expression() | |
10714 | && !this->cap_->is_error_expression() | |
10715 | && (!this->cap_->numeric_constant_value(&nc) | |
10716 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10717 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10718 | |
10719 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10720 | if (array_type == NULL) |
10721 | { | |
c484d925 | 10722 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10723 | return; |
10724 | } | |
e440a328 | 10725 | |
10726 | unsigned int int_bits = | |
10727 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10728 | ||
0c77715b | 10729 | Numeric_constant lvalnc; |
e440a328 | 10730 | mpz_t lval; |
e440a328 | 10731 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10732 | && array_type->length()->numeric_constant_value(&lvalnc) |
10733 | && lvalnc.to_int(&lval)); | |
10734 | Numeric_constant inc; | |
e440a328 | 10735 | mpz_t ival; |
0bd5d859 | 10736 | bool ival_valid = false; |
0c77715b | 10737 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10738 | { |
0bd5d859 | 10739 | ival_valid = true; |
e440a328 | 10740 | if (mpz_sgn(ival) < 0 |
10741 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10742 | || (lval_valid | |
10743 | && (this->end_ == NULL | |
10744 | ? mpz_cmp(ival, lval) >= 0 | |
10745 | : mpz_cmp(ival, lval) > 0))) | |
10746 | { | |
631d5788 | 10747 | go_error_at(this->start_->location(), "array index out of bounds"); |
e440a328 | 10748 | this->set_is_error(); |
10749 | } | |
10750 | } | |
10751 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10752 | { | |
0c77715b | 10753 | Numeric_constant enc; |
10754 | mpz_t eval; | |
acf2b673 | 10755 | bool eval_valid = false; |
0c77715b | 10756 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10757 | { |
acf2b673 | 10758 | eval_valid = true; |
0c77715b | 10759 | if (mpz_sgn(eval) < 0 |
10760 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10761 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10762 | { |
631d5788 | 10763 | go_error_at(this->end_->location(), "array index out of bounds"); |
e440a328 | 10764 | this->set_is_error(); |
10765 | } | |
0bd5d859 | 10766 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10767 | this->report_error(_("inverted slice range")); | |
e440a328 | 10768 | } |
acf2b673 | 10769 | |
10770 | Numeric_constant cnc; | |
10771 | mpz_t cval; | |
10772 | if (this->cap_ != NULL | |
10773 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10774 | { | |
10775 | if (mpz_sgn(cval) < 0 | |
10776 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10777 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10778 | { | |
631d5788 | 10779 | go_error_at(this->cap_->location(), "array index out of bounds"); |
acf2b673 | 10780 | this->set_is_error(); |
10781 | } | |
10782 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10783 | { | |
631d5788 | 10784 | go_error_at(this->cap_->location(), |
10785 | "invalid slice index: capacity less than start"); | |
acf2b673 | 10786 | this->set_is_error(); |
10787 | } | |
10788 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10789 | { | |
631d5788 | 10790 | go_error_at(this->cap_->location(), |
10791 | "invalid slice index: capacity less than length"); | |
acf2b673 | 10792 | this->set_is_error(); |
10793 | } | |
10794 | mpz_clear(cval); | |
10795 | } | |
10796 | ||
10797 | if (eval_valid) | |
10798 | mpz_clear(eval); | |
e440a328 | 10799 | } |
0bd5d859 | 10800 | if (ival_valid) |
10801 | mpz_clear(ival); | |
0c77715b | 10802 | if (lval_valid) |
10803 | mpz_clear(lval); | |
e440a328 | 10804 | |
10805 | // A slice of an array requires an addressable array. A slice of a | |
10806 | // slice is always possible. | |
411eb89e | 10807 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10808 | { |
10809 | if (!this->array_->is_addressable()) | |
8da39c3b | 10810 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10811 | else |
b7327dbf | 10812 | // Set the array address taken but not escape. The escape |
10813 | // analysis will make it escape to heap when needed. | |
10814 | this->array_->address_taken(false); | |
88ec30c8 | 10815 | } |
e440a328 | 10816 | } |
10817 | ||
2c809f8f | 10818 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10819 | |
10820 | Expression* | |
10821 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10822 | Statement_inserter* inserter) | |
10823 | { | |
10824 | Location loc = this->location(); | |
5bf8be8b | 10825 | Expression* array = this->array_; |
10826 | Expression* start = this->start_; | |
10827 | Expression* end = this->end_; | |
10828 | Expression* cap = this->cap_; | |
10829 | if (array->is_error_expression() | |
10830 | || array->type()->is_error_type() | |
10831 | || start->is_error_expression() | |
10832 | || start->type()->is_error_type() | |
10833 | || (end != NULL | |
10834 | && (end->is_error_expression() || end->type()->is_error_type())) | |
10835 | || (cap != NULL | |
10836 | && (cap->is_error_expression() || cap->type()->is_error_type()))) | |
10837 | { | |
10838 | go_assert(saw_errors()); | |
10839 | return Expression::make_error(loc); | |
10840 | } | |
10841 | ||
2c809f8f | 10842 | Temporary_statement* temp; |
5bf8be8b | 10843 | if (array->type()->is_slice_type() && !array->is_variable()) |
35a54f17 | 10844 | { |
5bf8be8b | 10845 | temp = Statement::make_temporary(NULL, array, loc); |
35a54f17 | 10846 | inserter->insert(temp); |
10847 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10848 | } | |
5bf8be8b | 10849 | if (!start->is_variable()) |
2c809f8f | 10850 | { |
5bf8be8b | 10851 | temp = Statement::make_temporary(NULL, start, loc); |
2c809f8f | 10852 | inserter->insert(temp); |
10853 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10854 | } | |
5bf8be8b | 10855 | if (end != NULL |
10856 | && !end->is_nil_expression() | |
10857 | && !end->is_variable()) | |
2c809f8f | 10858 | { |
5bf8be8b | 10859 | temp = Statement::make_temporary(NULL, end, loc); |
2c809f8f | 10860 | inserter->insert(temp); |
10861 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10862 | } | |
03118c21 | 10863 | if (cap != NULL && !cap->is_variable()) |
2c809f8f | 10864 | { |
5bf8be8b | 10865 | temp = Statement::make_temporary(NULL, cap, loc); |
2c809f8f | 10866 | inserter->insert(temp); |
10867 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10868 | } | |
10869 | ||
35a54f17 | 10870 | return this; |
10871 | } | |
10872 | ||
e440a328 | 10873 | // Return whether this expression is addressable. |
10874 | ||
10875 | bool | |
10876 | Array_index_expression::do_is_addressable() const | |
10877 | { | |
10878 | // A slice expression is not addressable. | |
10879 | if (this->end_ != NULL) | |
10880 | return false; | |
10881 | ||
10882 | // An index into a slice is addressable. | |
411eb89e | 10883 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10884 | return true; |
10885 | ||
10886 | // An index into an array is addressable if the array is | |
10887 | // addressable. | |
10888 | return this->array_->is_addressable(); | |
10889 | } | |
10890 | ||
bf1323be | 10891 | void |
10892 | Array_index_expression::do_address_taken(bool escapes) | |
10893 | { | |
10894 | // In &x[0], if x is a slice, then x's address is not taken. | |
10895 | if (!this->array_->type()->is_slice_type()) | |
10896 | this->array_->address_taken(escapes); | |
10897 | } | |
10898 | ||
ea664253 | 10899 | // Get the backend representation for an array index. |
e440a328 | 10900 | |
ea664253 | 10901 | Bexpression* |
10902 | Array_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 10903 | { |
e440a328 | 10904 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10905 | if (array_type == NULL) |
10906 | { | |
c484d925 | 10907 | go_assert(this->array_->type()->is_error()); |
ea664253 | 10908 | return context->backend()->error_expression(); |
d8cd8e2d | 10909 | } |
35a54f17 | 10910 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10911 | |
2c809f8f | 10912 | Location loc = this->location(); |
10913 | Gogo* gogo = context->gogo(); | |
10914 | ||
6dfedc16 | 10915 | Type* int_type = Type::lookup_integer_type("int"); |
10916 | Btype* int_btype = int_type->get_backend(gogo); | |
e440a328 | 10917 | |
2c809f8f | 10918 | // We need to convert the length and capacity to the Go "int" type here |
10919 | // because the length of a fixed-length array could be of type "uintptr" | |
10920 | // and gimple disallows binary operations between "uintptr" and other | |
10921 | // integer types. FIXME. | |
10922 | Bexpression* length = NULL; | |
a04bfdfc | 10923 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10924 | { | |
35a54f17 | 10925 | Expression* len = array_type->get_length(gogo, this->array_); |
ea664253 | 10926 | length = len->get_backend(context); |
2c809f8f | 10927 | length = gogo->backend()->convert_expression(int_btype, length, loc); |
a04bfdfc | 10928 | } |
10929 | ||
2c809f8f | 10930 | Bexpression* capacity = NULL; |
a04bfdfc | 10931 | if (this->end_ != NULL) |
10932 | { | |
35a54f17 | 10933 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
ea664253 | 10934 | capacity = cap->get_backend(context); |
2c809f8f | 10935 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); |
a04bfdfc | 10936 | } |
10937 | ||
2c809f8f | 10938 | Bexpression* cap_arg = capacity; |
acf2b673 | 10939 | if (this->cap_ != NULL) |
10940 | { | |
ea664253 | 10941 | cap_arg = this->cap_->get_backend(context); |
2c809f8f | 10942 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); |
acf2b673 | 10943 | } |
10944 | ||
2c809f8f | 10945 | if (length == NULL) |
10946 | length = cap_arg; | |
e440a328 | 10947 | |
10948 | int code = (array_type->length() != NULL | |
10949 | ? (this->end_ == NULL | |
10950 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10951 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10952 | : (this->end_ == NULL | |
10953 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10954 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
ea664253 | 10955 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
2c809f8f | 10956 | |
6dfedc16 | 10957 | if (this->start_->type()->integer_type() == NULL |
10958 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10959 | { | |
10960 | go_assert(saw_errors()); | |
10961 | return context->backend()->error_expression(); | |
10962 | } | |
d9f3743a | 10963 | |
ea664253 | 10964 | Bexpression* bad_index = |
d9f3743a | 10965 | Expression::check_bounds(this->start_, loc)->get_backend(context); |
2c809f8f | 10966 | |
ea664253 | 10967 | Bexpression* start = this->start_->get_backend(context); |
2c809f8f | 10968 | start = gogo->backend()->convert_expression(int_btype, start, loc); |
10969 | Bexpression* start_too_large = | |
10970 | gogo->backend()->binary_expression((this->end_ == NULL | |
10971 | ? OPERATOR_GE | |
10972 | : OPERATOR_GT), | |
10973 | start, | |
10974 | (this->end_ == NULL | |
10975 | ? length | |
10976 | : capacity), | |
10977 | loc); | |
10978 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10979 | bad_index, loc); | |
e440a328 | 10980 | |
93715b75 | 10981 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 10982 | if (this->end_ == NULL) |
10983 | { | |
10984 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10985 | // wrap the index check into START. |
10986 | start = | |
93715b75 | 10987 | gogo->backend()->conditional_expression(bfn, int_btype, bad_index, |
2c809f8f | 10988 | crash, start, loc); |
e440a328 | 10989 | |
2c809f8f | 10990 | Bexpression* ret; |
e440a328 | 10991 | if (array_type->length() != NULL) |
10992 | { | |
ea664253 | 10993 | Bexpression* array = this->array_->get_backend(context); |
2c809f8f | 10994 | ret = gogo->backend()->array_index_expression(array, start, loc); |
e440a328 | 10995 | } |
10996 | else | |
10997 | { | |
2c809f8f | 10998 | // Slice. |
10999 | Expression* valptr = | |
44dbe1d7 | 11000 | array_type->get_value_pointer(gogo, this->array_, |
11001 | this->is_lvalue_); | |
ea664253 | 11002 | Bexpression* ptr = valptr->get_backend(context); |
2c809f8f | 11003 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); |
9b27b43c | 11004 | |
11005 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
11006 | Btype* ele_btype = ele_type->get_backend(gogo); | |
11007 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 11008 | } |
ea664253 | 11009 | return ret; |
e440a328 | 11010 | } |
11011 | ||
11012 | // Array slice. | |
11013 | ||
acf2b673 | 11014 | if (this->cap_ != NULL) |
11015 | { | |
2c809f8f | 11016 | Bexpression* bounds_bcheck = |
ea664253 | 11017 | Expression::check_bounds(this->cap_, loc)->get_backend(context); |
2c809f8f | 11018 | bad_index = |
11019 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11020 | bad_index, loc); | |
11021 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
11022 | ||
11023 | Bexpression* cap_too_small = | |
11024 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
11025 | Bexpression* cap_too_large = | |
11026 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
11027 | Bexpression* bad_cap = | |
11028 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
11029 | cap_too_large, loc); | |
11030 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
11031 | bad_index, loc); | |
11032 | } | |
11033 | ||
11034 | Bexpression* end; | |
e440a328 | 11035 | if (this->end_->is_nil_expression()) |
2c809f8f | 11036 | end = length; |
e440a328 | 11037 | else |
11038 | { | |
2c809f8f | 11039 | Bexpression* bounds_bcheck = |
ea664253 | 11040 | Expression::check_bounds(this->end_, loc)->get_backend(context); |
e440a328 | 11041 | |
2c809f8f | 11042 | bad_index = |
11043 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
11044 | bad_index, loc); | |
e440a328 | 11045 | |
ea664253 | 11046 | end = this->end_->get_backend(context); |
2c809f8f | 11047 | end = gogo->backend()->convert_expression(int_btype, end, loc); |
11048 | Bexpression* end_too_small = | |
11049 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
11050 | Bexpression* end_too_large = | |
11051 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
11052 | Bexpression* bad_end = | |
11053 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
11054 | end_too_large, loc); | |
11055 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
11056 | bad_index, loc); | |
e440a328 | 11057 | } |
11058 | ||
2c809f8f | 11059 | Bexpression* result_length = |
11060 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 11061 | |
2c809f8f | 11062 | Bexpression* result_capacity = |
11063 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 11064 | |
03118c21 | 11065 | // If the new capacity is zero, don't change val. Otherwise we can |
11066 | // get a pointer to the next object in memory, keeping it live | |
11067 | // unnecessarily. When the capacity is zero, the actual pointer | |
11068 | // value doesn't matter. | |
11069 | Bexpression* zero = | |
11070 | Expression::make_integer_ul(0, int_type, loc)->get_backend(context); | |
11071 | Bexpression* cond = | |
11072 | gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero, | |
11073 | loc); | |
11074 | Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype, | |
11075 | cond, zero, | |
11076 | start, loc); | |
44dbe1d7 | 11077 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_, |
11078 | this->is_lvalue_); | |
03118c21 | 11079 | Bexpression* val = valptr->get_backend(context); |
11080 | val = gogo->backend()->pointer_offset_expression(val, offset, loc); | |
11081 | ||
2c809f8f | 11082 | Btype* struct_btype = this->type()->get_backend(gogo); |
11083 | std::vector<Bexpression*> init; | |
11084 | init.push_back(val); | |
11085 | init.push_back(result_length); | |
11086 | init.push_back(result_capacity); | |
e440a328 | 11087 | |
2c809f8f | 11088 | Bexpression* ctor = |
11089 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
93715b75 | 11090 | return gogo->backend()->conditional_expression(bfn, struct_btype, bad_index, |
ea664253 | 11091 | crash, ctor, loc); |
e440a328 | 11092 | } |
11093 | ||
d751bb78 | 11094 | // Dump ast representation for an array index expression. |
11095 | ||
11096 | void | |
11097 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11098 | const | |
11099 | { | |
11100 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 11101 | this->start_, this->end_, this->cap_); |
d751bb78 | 11102 | } |
11103 | ||
acf2b673 | 11104 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 11105 | |
11106 | Expression* | |
11107 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 11108 | Expression* end, Expression* cap, |
11109 | Location location) | |
e440a328 | 11110 | { |
acf2b673 | 11111 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 11112 | } |
11113 | ||
50075d74 | 11114 | // Class String_index_expression. |
e440a328 | 11115 | |
11116 | // String index traversal. | |
11117 | ||
11118 | int | |
11119 | String_index_expression::do_traverse(Traverse* traverse) | |
11120 | { | |
11121 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
11122 | return TRAVERSE_EXIT; | |
11123 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
11124 | return TRAVERSE_EXIT; | |
11125 | if (this->end_ != NULL) | |
11126 | { | |
11127 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
11128 | return TRAVERSE_EXIT; | |
11129 | } | |
11130 | return TRAVERSE_CONTINUE; | |
11131 | } | |
11132 | ||
2c809f8f | 11133 | Expression* |
11134 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
11135 | Statement_inserter* inserter) | |
e440a328 | 11136 | { |
2c809f8f | 11137 | Location loc = this->location(); |
5bf8be8b | 11138 | Expression* string = this->string_; |
11139 | Expression* start = this->start_; | |
11140 | Expression* end = this->end_; | |
11141 | if (string->is_error_expression() | |
11142 | || string->type()->is_error_type() | |
11143 | || start->is_error_expression() | |
11144 | || start->type()->is_error_type() | |
11145 | || (end != NULL | |
11146 | && (end->is_error_expression() || end->type()->is_error_type()))) | |
11147 | { | |
11148 | go_assert(saw_errors()); | |
11149 | return Expression::make_error(loc); | |
11150 | } | |
11151 | ||
11152 | Temporary_statement* temp; | |
2c809f8f | 11153 | if (!this->string_->is_variable()) |
11154 | { | |
11155 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
11156 | inserter->insert(temp); | |
11157 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
11158 | } | |
11159 | if (!this->start_->is_variable()) | |
11160 | { | |
11161 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
11162 | inserter->insert(temp); | |
11163 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
11164 | } | |
11165 | if (this->end_ != NULL | |
11166 | && !this->end_->is_nil_expression() | |
11167 | && !this->end_->is_variable()) | |
11168 | { | |
11169 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
11170 | inserter->insert(temp); | |
11171 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
11172 | } | |
11173 | ||
11174 | return this; | |
11175 | } | |
11176 | ||
11177 | // Return the type of a string index. | |
11178 | ||
11179 | Type* | |
11180 | String_index_expression::do_type() | |
11181 | { | |
11182 | if (this->end_ == NULL) | |
11183 | return Type::lookup_integer_type("uint8"); | |
11184 | else | |
11185 | return this->string_->type(); | |
11186 | } | |
11187 | ||
11188 | // Determine the type of a string index. | |
11189 | ||
11190 | void | |
11191 | String_index_expression::do_determine_type(const Type_context*) | |
11192 | { | |
11193 | this->string_->determine_type_no_context(); | |
f77aa642 | 11194 | |
11195 | Type_context index_context(Type::lookup_integer_type("int"), false); | |
11196 | if (this->start_->is_constant()) | |
11197 | this->start_->determine_type(&index_context); | |
11198 | else | |
11199 | this->start_->determine_type_no_context(); | |
e440a328 | 11200 | if (this->end_ != NULL) |
f77aa642 | 11201 | { |
11202 | if (this->end_->is_constant()) | |
11203 | this->end_->determine_type(&index_context); | |
11204 | else | |
11205 | this->end_->determine_type_no_context(); | |
11206 | } | |
e440a328 | 11207 | } |
11208 | ||
11209 | // Check types of a string index. | |
11210 | ||
11211 | void | |
11212 | String_index_expression::do_check_types(Gogo*) | |
11213 | { | |
acdc230d | 11214 | Numeric_constant nc; |
11215 | unsigned long v; | |
11216 | if (this->start_->type()->integer_type() == NULL | |
11217 | && !this->start_->type()->is_error() | |
11218 | && (!this->start_->numeric_constant_value(&nc) | |
11219 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11220 | this->report_error(_("index must be integer")); |
11221 | if (this->end_ != NULL | |
11222 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 11223 | && !this->end_->type()->is_error() |
11224 | && !this->end_->is_nil_expression() | |
11225 | && !this->end_->is_error_expression() | |
11226 | && (!this->end_->numeric_constant_value(&nc) | |
11227 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 11228 | this->report_error(_("slice end must be integer")); |
11229 | ||
11230 | std::string sval; | |
11231 | bool sval_valid = this->string_->string_constant_value(&sval); | |
11232 | ||
0c77715b | 11233 | Numeric_constant inc; |
e440a328 | 11234 | mpz_t ival; |
0bd5d859 | 11235 | bool ival_valid = false; |
0c77715b | 11236 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 11237 | { |
0bd5d859 | 11238 | ival_valid = true; |
e440a328 | 11239 | if (mpz_sgn(ival) < 0 |
b10f32fb | 11240 | || (sval_valid |
11241 | && (this->end_ == NULL | |
11242 | ? mpz_cmp_ui(ival, sval.length()) >= 0 | |
11243 | : mpz_cmp_ui(ival, sval.length()) > 0))) | |
e440a328 | 11244 | { |
631d5788 | 11245 | go_error_at(this->start_->location(), "string index out of bounds"); |
e440a328 | 11246 | this->set_is_error(); |
11247 | } | |
11248 | } | |
11249 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
11250 | { | |
0c77715b | 11251 | Numeric_constant enc; |
11252 | mpz_t eval; | |
11253 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 11254 | { |
0c77715b | 11255 | if (mpz_sgn(eval) < 0 |
11256 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 11257 | { |
631d5788 | 11258 | go_error_at(this->end_->location(), "string index out of bounds"); |
e440a328 | 11259 | this->set_is_error(); |
11260 | } | |
0bd5d859 | 11261 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
11262 | this->report_error(_("inverted slice range")); | |
0c77715b | 11263 | mpz_clear(eval); |
e440a328 | 11264 | } |
11265 | } | |
0bd5d859 | 11266 | if (ival_valid) |
11267 | mpz_clear(ival); | |
e440a328 | 11268 | } |
11269 | ||
ea664253 | 11270 | // Get the backend representation for a string index. |
e440a328 | 11271 | |
ea664253 | 11272 | Bexpression* |
11273 | String_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11274 | { |
b13c66cd | 11275 | Location loc = this->location(); |
2c809f8f | 11276 | Expression* string_arg = this->string_; |
11277 | if (this->string_->type()->points_to() != NULL) | |
f614ea8b | 11278 | string_arg = Expression::make_dereference(this->string_, |
11279 | NIL_CHECK_NOT_NEEDED, loc); | |
e440a328 | 11280 | |
2c809f8f | 11281 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 11282 | |
2c809f8f | 11283 | int code = (this->end_ == NULL |
11284 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
11285 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 11286 | |
2c809f8f | 11287 | Gogo* gogo = context->gogo(); |
ea664253 | 11288 | Bexpression* crash = gogo->runtime_error(code, loc)->get_backend(context); |
1b1f2abf | 11289 | |
11290 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 11291 | |
2c809f8f | 11292 | // It is possible that an error occurred earlier because the start index |
11293 | // cannot be represented as an integer type. In this case, we shouldn't | |
11294 | // try casting the starting index into an integer since | |
11295 | // Type_conversion_expression will fail to get the backend representation. | |
11296 | // FIXME. | |
11297 | if (this->start_->type()->integer_type() == NULL | |
11298 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
11299 | { | |
11300 | go_assert(saw_errors()); | |
ea664253 | 11301 | return context->backend()->error_expression(); |
2c809f8f | 11302 | } |
e440a328 | 11303 | |
2c809f8f | 11304 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
93715b75 | 11305 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
e440a328 | 11306 | |
2c809f8f | 11307 | if (this->end_ == NULL) |
11308 | { | |
11309 | Expression* length = | |
11310 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 11311 | |
2c809f8f | 11312 | Expression* start_too_large = |
11313 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
11314 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
11315 | bad_index, loc); | |
11316 | Expression* bytes = | |
11317 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 11318 | |
ea664253 | 11319 | Bexpression* bstart = start->get_backend(context); |
11320 | Bexpression* ptr = bytes->get_backend(context); | |
2c809f8f | 11321 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); |
9b27b43c | 11322 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
11323 | Bexpression* index = | |
11324 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 11325 | |
2c809f8f | 11326 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
ea664253 | 11327 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11328 | return gogo->backend()->conditional_expression(bfn, byte_btype, |
11329 | index_error, crash, | |
11330 | index, loc); | |
2c809f8f | 11331 | } |
11332 | ||
11333 | Expression* end = NULL; | |
11334 | if (this->end_->is_nil_expression()) | |
e67508fa | 11335 | end = Expression::make_integer_sl(-1, int_type, loc); |
e440a328 | 11336 | else |
11337 | { | |
2c809f8f | 11338 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
11339 | bad_index = | |
11340 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
11341 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 11342 | } |
2c809f8f | 11343 | |
11344 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
11345 | string_arg, start, end); | |
ea664253 | 11346 | Bexpression* bstrslice = strslice->get_backend(context); |
2c809f8f | 11347 | |
11348 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
ea664253 | 11349 | Bexpression* index_error = bad_index->get_backend(context); |
93715b75 | 11350 | return gogo->backend()->conditional_expression(bfn, str_btype, index_error, |
ea664253 | 11351 | crash, bstrslice, loc); |
e440a328 | 11352 | } |
11353 | ||
d751bb78 | 11354 | // Dump ast representation for a string index expression. |
11355 | ||
11356 | void | |
11357 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11358 | const | |
11359 | { | |
acf2b673 | 11360 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
11361 | this->start_, this->end_, NULL); | |
d751bb78 | 11362 | } |
11363 | ||
e440a328 | 11364 | // Make a string index expression. END may be NULL. |
11365 | ||
11366 | Expression* | |
11367 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 11368 | Expression* end, Location location) |
e440a328 | 11369 | { |
11370 | return new String_index_expression(string, start, end, location); | |
11371 | } | |
11372 | ||
11373 | // Class Map_index. | |
11374 | ||
11375 | // Get the type of the map. | |
11376 | ||
11377 | Map_type* | |
11378 | Map_index_expression::get_map_type() const | |
11379 | { | |
0d5530d9 | 11380 | Map_type* mt = this->map_->type()->map_type(); |
c7524fae | 11381 | if (mt == NULL) |
c484d925 | 11382 | go_assert(saw_errors()); |
e440a328 | 11383 | return mt; |
11384 | } | |
11385 | ||
11386 | // Map index traversal. | |
11387 | ||
11388 | int | |
11389 | Map_index_expression::do_traverse(Traverse* traverse) | |
11390 | { | |
11391 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11392 | return TRAVERSE_EXIT; | |
11393 | return Expression::traverse(&this->index_, traverse); | |
11394 | } | |
11395 | ||
2c809f8f | 11396 | // We need to pass in a pointer to the key, so flatten the index into a |
11397 | // temporary variable if it isn't already. The value pointer will be | |
11398 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
11399 | // recomputation. | |
11400 | ||
11401 | Expression* | |
91c0fd76 | 11402 | Map_index_expression::do_flatten(Gogo* gogo, Named_object*, |
2c809f8f | 11403 | Statement_inserter* inserter) |
11404 | { | |
91c0fd76 | 11405 | Location loc = this->location(); |
2c809f8f | 11406 | Map_type* mt = this->get_map_type(); |
5bf8be8b | 11407 | if (this->index()->is_error_expression() |
11408 | || this->index()->type()->is_error_type() | |
11409 | || mt->is_error_type()) | |
11410 | { | |
11411 | go_assert(saw_errors()); | |
11412 | return Expression::make_error(loc); | |
11413 | } | |
11414 | ||
91c0fd76 | 11415 | if (!Type::are_identical(mt->key_type(), this->index_->type(), false, NULL)) |
11416 | { | |
11417 | if (this->index_->type()->interface_type() != NULL | |
11418 | && !this->index_->is_variable()) | |
11419 | { | |
11420 | Temporary_statement* temp = | |
11421 | Statement::make_temporary(NULL, this->index_, loc); | |
11422 | inserter->insert(temp); | |
11423 | this->index_ = Expression::make_temporary_reference(temp, loc); | |
11424 | } | |
11425 | this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(), | |
11426 | this->index_, loc); | |
11427 | } | |
2c809f8f | 11428 | |
11429 | if (!this->index_->is_variable()) | |
11430 | { | |
11431 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
91c0fd76 | 11432 | loc); |
2c809f8f | 11433 | inserter->insert(temp); |
91c0fd76 | 11434 | this->index_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11435 | } |
11436 | ||
11437 | if (this->value_pointer_ == NULL) | |
0d5530d9 | 11438 | this->get_value_pointer(gogo); |
5bf8be8b | 11439 | if (this->value_pointer_->is_error_expression() |
11440 | || this->value_pointer_->type()->is_error_type()) | |
11441 | return Expression::make_error(loc); | |
2c809f8f | 11442 | if (!this->value_pointer_->is_variable()) |
11443 | { | |
11444 | Temporary_statement* temp = | |
91c0fd76 | 11445 | Statement::make_temporary(NULL, this->value_pointer_, loc); |
2c809f8f | 11446 | inserter->insert(temp); |
91c0fd76 | 11447 | this->value_pointer_ = Expression::make_temporary_reference(temp, loc); |
2c809f8f | 11448 | } |
11449 | ||
11450 | return this; | |
11451 | } | |
11452 | ||
e440a328 | 11453 | // Return the type of a map index. |
11454 | ||
11455 | Type* | |
11456 | Map_index_expression::do_type() | |
11457 | { | |
c7524fae | 11458 | Map_type* mt = this->get_map_type(); |
11459 | if (mt == NULL) | |
11460 | return Type::make_error_type(); | |
0d5530d9 | 11461 | return mt->val_type(); |
e440a328 | 11462 | } |
11463 | ||
11464 | // Fix the type of a map index. | |
11465 | ||
11466 | void | |
11467 | Map_index_expression::do_determine_type(const Type_context*) | |
11468 | { | |
11469 | this->map_->determine_type_no_context(); | |
c7524fae | 11470 | Map_type* mt = this->get_map_type(); |
11471 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11472 | Type_context subcontext(key_type, false); | |
e440a328 | 11473 | this->index_->determine_type(&subcontext); |
11474 | } | |
11475 | ||
11476 | // Check types of a map index. | |
11477 | ||
11478 | void | |
11479 | Map_index_expression::do_check_types(Gogo*) | |
11480 | { | |
11481 | std::string reason; | |
c7524fae | 11482 | Map_type* mt = this->get_map_type(); |
11483 | if (mt == NULL) | |
11484 | return; | |
11485 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11486 | { |
11487 | if (reason.empty()) | |
11488 | this->report_error(_("incompatible type for map index")); | |
11489 | else | |
11490 | { | |
631d5788 | 11491 | go_error_at(this->location(), "incompatible type for map index (%s)", |
11492 | reason.c_str()); | |
e440a328 | 11493 | this->set_is_error(); |
11494 | } | |
11495 | } | |
11496 | } | |
11497 | ||
ea664253 | 11498 | // Get the backend representation for a map index. |
e440a328 | 11499 | |
ea664253 | 11500 | Bexpression* |
11501 | Map_index_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11502 | { |
11503 | Map_type* type = this->get_map_type(); | |
c7524fae | 11504 | if (type == NULL) |
2c809f8f | 11505 | { |
11506 | go_assert(saw_errors()); | |
ea664253 | 11507 | return context->backend()->error_expression(); |
2c809f8f | 11508 | } |
e440a328 | 11509 | |
2c809f8f | 11510 | go_assert(this->value_pointer_ != NULL |
11511 | && this->value_pointer_->is_variable()); | |
e440a328 | 11512 | |
f614ea8b | 11513 | Expression* val = Expression::make_dereference(this->value_pointer_, |
11514 | NIL_CHECK_NOT_NEEDED, | |
11515 | this->location()); | |
0d5530d9 | 11516 | return val->get_backend(context); |
e440a328 | 11517 | } |
11518 | ||
0d5530d9 | 11519 | // Get an expression for the map index. This returns an expression |
11520 | // that evaluates to a pointer to a value. If the key is not in the | |
11521 | // map, the pointer will point to a zero value. | |
e440a328 | 11522 | |
2c809f8f | 11523 | Expression* |
0d5530d9 | 11524 | Map_index_expression::get_value_pointer(Gogo* gogo) |
e440a328 | 11525 | { |
2c809f8f | 11526 | if (this->value_pointer_ == NULL) |
746d2e73 | 11527 | { |
2c809f8f | 11528 | Map_type* type = this->get_map_type(); |
11529 | if (type == NULL) | |
746d2e73 | 11530 | { |
2c809f8f | 11531 | go_assert(saw_errors()); |
11532 | return Expression::make_error(this->location()); | |
746d2e73 | 11533 | } |
e440a328 | 11534 | |
2c809f8f | 11535 | Location loc = this->location(); |
11536 | Expression* map_ref = this->map_; | |
e440a328 | 11537 | |
0d5530d9 | 11538 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, |
11539 | this->index_, | |
2c809f8f | 11540 | loc); |
0d5530d9 | 11541 | |
11542 | Expression* zero = type->fat_zero_value(gogo); | |
11543 | ||
11544 | Expression* map_index; | |
11545 | ||
11546 | if (zero == NULL) | |
11547 | map_index = | |
11548 | Runtime::make_call(Runtime::MAPACCESS1, loc, 3, | |
11549 | Expression::make_type_descriptor(type, loc), | |
11550 | map_ref, index_ptr); | |
11551 | else | |
11552 | map_index = | |
11553 | Runtime::make_call(Runtime::MAPACCESS1_FAT, loc, 4, | |
11554 | Expression::make_type_descriptor(type, loc), | |
11555 | map_ref, index_ptr, zero); | |
2c809f8f | 11556 | |
11557 | Type* val_type = type->val_type(); | |
11558 | this->value_pointer_ = | |
11559 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
11560 | map_index, this->location()); | |
11561 | } | |
0d5530d9 | 11562 | |
2c809f8f | 11563 | return this->value_pointer_; |
e440a328 | 11564 | } |
11565 | ||
d751bb78 | 11566 | // Dump ast representation for a map index expression |
11567 | ||
11568 | void | |
11569 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11570 | const | |
11571 | { | |
acf2b673 | 11572 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
11573 | this->index_, NULL, NULL); | |
d751bb78 | 11574 | } |
11575 | ||
e440a328 | 11576 | // Make a map index expression. |
11577 | ||
11578 | Map_index_expression* | |
11579 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11580 | Location location) |
e440a328 | 11581 | { |
11582 | return new Map_index_expression(map, index, location); | |
11583 | } | |
11584 | ||
11585 | // Class Field_reference_expression. | |
11586 | ||
149eabc5 | 11587 | // Lower a field reference expression. There is nothing to lower, but |
11588 | // this is where we generate the tracking information for fields with | |
11589 | // the magic go:"track" tag. | |
11590 | ||
11591 | Expression* | |
11592 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11593 | Statement_inserter* inserter, int) | |
11594 | { | |
11595 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11596 | if (struct_type == NULL) | |
11597 | { | |
11598 | // Error will be reported elsewhere. | |
11599 | return this; | |
11600 | } | |
11601 | const Struct_field* field = struct_type->field(this->field_index_); | |
11602 | if (field == NULL) | |
11603 | return this; | |
11604 | if (!field->has_tag()) | |
11605 | return this; | |
11606 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11607 | return this; | |
11608 | ||
604e278d | 11609 | // References from functions generated by the compiler don't count. |
c6292d1d | 11610 | if (function != NULL && function->func_value()->is_type_specific_function()) |
604e278d | 11611 | return this; |
11612 | ||
149eabc5 | 11613 | // We have found a reference to a tracked field. Build a call to |
11614 | // the runtime function __go_fieldtrack with a string that describes | |
11615 | // the field. FIXME: We should only call this once per referenced | |
11616 | // field per function, not once for each reference to the field. | |
11617 | ||
11618 | if (this->called_fieldtrack_) | |
11619 | return this; | |
11620 | this->called_fieldtrack_ = true; | |
11621 | ||
11622 | Location loc = this->location(); | |
11623 | ||
11624 | std::string s = "fieldtrack \""; | |
11625 | Named_type* nt = this->expr_->type()->named_type(); | |
11626 | if (nt == NULL || nt->named_object()->package() == NULL) | |
11627 | s.append(gogo->pkgpath()); | |
11628 | else | |
11629 | s.append(nt->named_object()->package()->pkgpath()); | |
11630 | s.push_back('.'); | |
11631 | if (nt != NULL) | |
5c29ad36 | 11632 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11633 | s.push_back('.'); |
11634 | s.append(field->field_name()); | |
11635 | s.push_back('"'); | |
11636 | ||
11637 | // We can't use a string here, because internally a string holds a | |
11638 | // pointer to the actual bytes; when the linker garbage collects the | |
11639 | // string, it won't garbage collect the bytes. So we use a | |
11640 | // [...]byte. | |
11641 | ||
e67508fa | 11642 | Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc); |
149eabc5 | 11643 | |
11644 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
6bf4793c | 11645 | Array_type* array_type = Type::make_array_type(byte_type, length_expr); |
11646 | array_type->set_is_array_incomparable(); | |
149eabc5 | 11647 | |
11648 | Expression_list* bytes = new Expression_list(); | |
11649 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11650 | { | |
e67508fa | 11651 | unsigned char c = static_cast<unsigned char>(*p); |
11652 | bytes->push_back(Expression::make_integer_ul(c, NULL, loc)); | |
149eabc5 | 11653 | } |
11654 | ||
11655 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11656 | bytes, false, loc); |
149eabc5 | 11657 | |
11658 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11659 | ||
11660 | static int count; | |
11661 | char buf[50]; | |
11662 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11663 | ++count; | |
11664 | ||
11665 | Named_object* no = gogo->add_variable(buf, var); | |
11666 | e = Expression::make_var_reference(no, loc); | |
11667 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11668 | ||
11669 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
604e278d | 11670 | gogo->lower_expression(function, inserter, &call); |
149eabc5 | 11671 | inserter->insert(Statement::make_statement(call, false)); |
11672 | ||
11673 | // Put this function, and the global variable we just created, into | |
11674 | // unique sections. This will permit the linker to garbage collect | |
11675 | // them if they are not referenced. The effect is that the only | |
11676 | // strings, indicating field references, that will wind up in the | |
11677 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11678 | if (function != NULL) |
11679 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11680 | var->set_in_unique_section(); |
11681 | ||
11682 | return this; | |
11683 | } | |
11684 | ||
e440a328 | 11685 | // Return the type of a field reference. |
11686 | ||
11687 | Type* | |
11688 | Field_reference_expression::do_type() | |
11689 | { | |
b0e628fb | 11690 | Type* type = this->expr_->type(); |
5c13bd80 | 11691 | if (type->is_error()) |
b0e628fb | 11692 | return type; |
11693 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11694 | go_assert(struct_type != NULL); |
e440a328 | 11695 | return struct_type->field(this->field_index_)->type(); |
11696 | } | |
11697 | ||
11698 | // Check the types for a field reference. | |
11699 | ||
11700 | void | |
11701 | Field_reference_expression::do_check_types(Gogo*) | |
11702 | { | |
b0e628fb | 11703 | Type* type = this->expr_->type(); |
5c13bd80 | 11704 | if (type->is_error()) |
b0e628fb | 11705 | return; |
11706 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11707 | go_assert(struct_type != NULL); |
11708 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11709 | } |
11710 | ||
ea664253 | 11711 | // Get the backend representation for a field reference. |
e440a328 | 11712 | |
ea664253 | 11713 | Bexpression* |
11714 | Field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11715 | { |
ea664253 | 11716 | Bexpression* bstruct = this->expr_->get_backend(context); |
11717 | return context->gogo()->backend()->struct_field_expression(bstruct, | |
11718 | this->field_index_, | |
11719 | this->location()); | |
e440a328 | 11720 | } |
11721 | ||
d751bb78 | 11722 | // Dump ast representation for a field reference expression. |
11723 | ||
11724 | void | |
11725 | Field_reference_expression::do_dump_expression( | |
11726 | Ast_dump_context* ast_dump_context) const | |
11727 | { | |
11728 | this->expr_->dump_expression(ast_dump_context); | |
11729 | ast_dump_context->ostream() << "." << this->field_index_; | |
11730 | } | |
11731 | ||
e440a328 | 11732 | // Make a reference to a qualified identifier in an expression. |
11733 | ||
11734 | Field_reference_expression* | |
11735 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11736 | Location location) |
e440a328 | 11737 | { |
11738 | return new Field_reference_expression(expr, field_index, location); | |
11739 | } | |
11740 | ||
11741 | // Class Interface_field_reference_expression. | |
11742 | ||
2387f644 | 11743 | // Return an expression for the pointer to the function to call. |
e440a328 | 11744 | |
2387f644 | 11745 | Expression* |
11746 | Interface_field_reference_expression::get_function() | |
e440a328 | 11747 | { |
2387f644 | 11748 | Expression* ref = this->expr_; |
11749 | Location loc = this->location(); | |
11750 | if (ref->type()->points_to() != NULL) | |
f614ea8b | 11751 | ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc); |
e440a328 | 11752 | |
2387f644 | 11753 | Expression* mtable = |
11754 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11755 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11756 | |
11757 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11758 | unsigned int index; |
11759 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11760 | go_assert(field != NULL); | |
f614ea8b | 11761 | |
11762 | mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc); | |
2387f644 | 11763 | return Expression::make_field_reference(mtable, index, loc); |
e440a328 | 11764 | } |
11765 | ||
2387f644 | 11766 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11767 | // function. |
11768 | ||
2387f644 | 11769 | Expression* |
11770 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11771 | { |
2387f644 | 11772 | Expression* expr = this->expr_; |
11773 | if (expr->type()->points_to() != NULL) | |
f614ea8b | 11774 | expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT, |
11775 | this->location()); | |
2387f644 | 11776 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, |
11777 | this->location()); | |
e440a328 | 11778 | } |
11779 | ||
11780 | // Traversal. | |
11781 | ||
11782 | int | |
11783 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11784 | { | |
11785 | return Expression::traverse(&this->expr_, traverse); | |
11786 | } | |
11787 | ||
0afbb937 | 11788 | // Lower the expression. If this expression is not called, we need to |
11789 | // evaluate the expression twice when converting to the backend | |
11790 | // interface. So introduce a temporary variable if necessary. | |
11791 | ||
11792 | Expression* | |
9782d556 | 11793 | Interface_field_reference_expression::do_flatten(Gogo*, Named_object*, |
11794 | Statement_inserter* inserter) | |
0afbb937 | 11795 | { |
5bf8be8b | 11796 | if (this->expr_->is_error_expression() |
11797 | || this->expr_->type()->is_error_type()) | |
11798 | { | |
11799 | go_assert(saw_errors()); | |
11800 | return Expression::make_error(this->location()); | |
11801 | } | |
11802 | ||
2387f644 | 11803 | if (!this->expr_->is_variable()) |
0afbb937 | 11804 | { |
11805 | Temporary_statement* temp = | |
11806 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11807 | inserter->insert(temp); | |
11808 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11809 | this->location()); | |
11810 | } | |
11811 | return this; | |
11812 | } | |
11813 | ||
e440a328 | 11814 | // Return the type of an interface field reference. |
11815 | ||
11816 | Type* | |
11817 | Interface_field_reference_expression::do_type() | |
11818 | { | |
11819 | Type* expr_type = this->expr_->type(); | |
11820 | ||
11821 | Type* points_to = expr_type->points_to(); | |
11822 | if (points_to != NULL) | |
11823 | expr_type = points_to; | |
11824 | ||
11825 | Interface_type* interface_type = expr_type->interface_type(); | |
11826 | if (interface_type == NULL) | |
11827 | return Type::make_error_type(); | |
11828 | ||
11829 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11830 | if (method == NULL) | |
11831 | return Type::make_error_type(); | |
11832 | ||
11833 | return method->type(); | |
11834 | } | |
11835 | ||
11836 | // Determine types. | |
11837 | ||
11838 | void | |
11839 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11840 | { | |
11841 | this->expr_->determine_type_no_context(); | |
11842 | } | |
11843 | ||
11844 | // Check the types for an interface field reference. | |
11845 | ||
11846 | void | |
11847 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11848 | { | |
11849 | Type* type = this->expr_->type(); | |
11850 | ||
11851 | Type* points_to = type->points_to(); | |
11852 | if (points_to != NULL) | |
11853 | type = points_to; | |
11854 | ||
11855 | Interface_type* interface_type = type->interface_type(); | |
11856 | if (interface_type == NULL) | |
5c491127 | 11857 | { |
11858 | if (!type->is_error_type()) | |
11859 | this->report_error(_("expected interface or pointer to interface")); | |
11860 | } | |
e440a328 | 11861 | else |
11862 | { | |
11863 | const Typed_identifier* method = | |
11864 | interface_type->find_method(this->name_); | |
11865 | if (method == NULL) | |
11866 | { | |
631d5788 | 11867 | go_error_at(this->location(), "method %qs not in interface", |
11868 | Gogo::message_name(this->name_).c_str()); | |
e440a328 | 11869 | this->set_is_error(); |
11870 | } | |
11871 | } | |
11872 | } | |
11873 | ||
0afbb937 | 11874 | // If an interface field reference is not simply called, then it is |
11875 | // represented as a closure. The closure will hold a single variable, | |
11876 | // the value of the interface on which the method should be called. | |
11877 | // The function will be a simple thunk that pulls the value from the | |
11878 | // closure and calls the method with the remaining arguments. | |
11879 | ||
11880 | // Because method values are not common, we don't build all thunks for | |
11881 | // all possible interface methods, but instead only build them as we | |
11882 | // need them. In particular, we even build them on demand for | |
11883 | // interface methods defined in other packages. | |
11884 | ||
11885 | Interface_field_reference_expression::Interface_method_thunks | |
11886 | Interface_field_reference_expression::interface_method_thunks; | |
11887 | ||
11888 | // Find or create the thunk to call method NAME on TYPE. | |
11889 | ||
11890 | Named_object* | |
11891 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11892 | Interface_type* type, | |
11893 | const std::string& name) | |
11894 | { | |
11895 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11896 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11897 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11898 | if (ins.second) | |
11899 | { | |
11900 | // This is the first time we have seen this interface. | |
11901 | ins.first->second = new Method_thunks(); | |
11902 | } | |
11903 | ||
11904 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11905 | p != ins.first->second->end(); | |
11906 | p++) | |
11907 | if (p->first == name) | |
11908 | return p->second; | |
11909 | ||
11910 | Location loc = type->location(); | |
11911 | ||
11912 | const Typed_identifier* method_id = type->find_method(name); | |
11913 | if (method_id == NULL) | |
13f2fdb8 | 11914 | return Named_object::make_erroneous_name(gogo->thunk_name()); |
0afbb937 | 11915 | |
11916 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11917 | if (orig_fntype == NULL) | |
13f2fdb8 | 11918 | return Named_object::make_erroneous_name(gogo->thunk_name()); |
0afbb937 | 11919 | |
11920 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11921 | // The type here is wrong--it should be the C function type. But it |
11922 | // doesn't really matter. | |
0afbb937 | 11923 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
13f2fdb8 | 11924 | sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc))); |
11925 | sfl->push_back(Struct_field(Typed_identifier("val", type, loc))); | |
6bf4793c | 11926 | Struct_type* st = Type::make_struct_type(sfl, loc); |
11927 | st->set_is_struct_incomparable(); | |
11928 | Type* closure_type = Type::make_pointer_type(st); | |
0afbb937 | 11929 | |
f8bdf81a | 11930 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11931 | |
13f2fdb8 | 11932 | std::string thunk_name = gogo->thunk_name(); |
da244e59 | 11933 | Named_object* new_no = gogo->start_function(thunk_name, new_fntype, |
0afbb937 | 11934 | false, loc); |
11935 | ||
f8bdf81a | 11936 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11937 | cvar->set_is_used(); | |
1ecc6157 | 11938 | cvar->set_is_closure(); |
da244e59 | 11939 | Named_object* cp = Named_object::make_variable("$closure" + thunk_name, |
11940 | NULL, cvar); | |
f8bdf81a | 11941 | new_no->func_value()->set_closure_var(cp); |
0afbb937 | 11942 | |
f8bdf81a | 11943 | gogo->start_block(loc); |
0afbb937 | 11944 | |
11945 | // Field 0 of the closure is the function code pointer, field 1 is | |
11946 | // the value on which to invoke the method. | |
11947 | Expression* arg = Expression::make_var_reference(cp, loc); | |
f614ea8b | 11948 | arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc); |
0afbb937 | 11949 | arg = Expression::make_field_reference(arg, 1, loc); |
11950 | ||
11951 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11952 | loc); | |
11953 | ||
11954 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11955 | Expression_list* args; | |
11956 | if (orig_params == NULL || orig_params->empty()) | |
11957 | args = NULL; | |
11958 | else | |
11959 | { | |
11960 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11961 | args = new Expression_list(); | |
11962 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11963 | p != new_params->end(); |
0afbb937 | 11964 | ++p) |
11965 | { | |
11966 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11967 | go_assert(p_no != NULL | |
11968 | && p_no->is_variable() | |
11969 | && p_no->var_value()->is_parameter()); | |
11970 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11971 | } | |
11972 | } | |
11973 | ||
11974 | Call_expression* call = Expression::make_call(ifre, args, | |
11975 | orig_fntype->is_varargs(), | |
11976 | loc); | |
11977 | call->set_varargs_are_lowered(); | |
11978 | ||
11979 | Statement* s = Statement::make_return_from_call(call, loc); | |
11980 | gogo->add_statement(s); | |
11981 | Block* b = gogo->finish_block(loc); | |
11982 | gogo->add_block(b, loc); | |
11983 | gogo->lower_block(new_no, b); | |
a32698ee | 11984 | gogo->flatten_block(new_no, b); |
0afbb937 | 11985 | gogo->finish_function(loc); |
11986 | ||
11987 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11988 | return new_no; | |
11989 | } | |
11990 | ||
ea664253 | 11991 | // Get the backend representation for a method value. |
e440a328 | 11992 | |
ea664253 | 11993 | Bexpression* |
11994 | Interface_field_reference_expression::do_get_backend(Translate_context* context) | |
e440a328 | 11995 | { |
0afbb937 | 11996 | Interface_type* type = this->expr_->type()->interface_type(); |
11997 | if (type == NULL) | |
11998 | { | |
11999 | go_assert(saw_errors()); | |
ea664253 | 12000 | return context->backend()->error_expression(); |
0afbb937 | 12001 | } |
12002 | ||
12003 | Named_object* thunk = | |
12004 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
12005 | type, this->name_); | |
12006 | if (thunk->is_erroneous()) | |
12007 | { | |
12008 | go_assert(saw_errors()); | |
ea664253 | 12009 | return context->backend()->error_expression(); |
0afbb937 | 12010 | } |
12011 | ||
12012 | // FIXME: We should lower this earlier, but we can't it lower it in | |
12013 | // the lowering pass because at that point we don't know whether we | |
12014 | // need to create the thunk or not. If the expression is called, we | |
12015 | // don't need the thunk. | |
12016 | ||
12017 | Location loc = this->location(); | |
12018 | ||
12019 | Struct_field_list* fields = new Struct_field_list(); | |
13f2fdb8 | 12020 | fields->push_back(Struct_field(Typed_identifier("fn", |
0afbb937 | 12021 | thunk->func_value()->type(), |
12022 | loc))); | |
13f2fdb8 | 12023 | fields->push_back(Struct_field(Typed_identifier("val", |
0afbb937 | 12024 | this->expr_->type(), |
12025 | loc))); | |
12026 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6bf4793c | 12027 | st->set_is_struct_incomparable(); |
0afbb937 | 12028 | |
12029 | Expression_list* vals = new Expression_list(); | |
12030 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
12031 | vals->push_back(this->expr_); | |
12032 | ||
12033 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
ea664253 | 12034 | Bexpression* bclosure = |
12035 | Expression::make_heap_expression(expr, loc)->get_backend(context); | |
0afbb937 | 12036 | |
4df0c2d4 | 12037 | Gogo* gogo = context->gogo(); |
12038 | Btype* btype = this->type()->get_backend(gogo); | |
12039 | bclosure = gogo->backend()->convert_expression(btype, bclosure, loc); | |
12040 | ||
2387f644 | 12041 | Expression* nil_check = |
12042 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
12043 | Expression::make_nil(loc), loc); | |
ea664253 | 12044 | Bexpression* bnil_check = nil_check->get_backend(context); |
0afbb937 | 12045 | |
ea664253 | 12046 | Bexpression* bcrash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
12047 | loc)->get_backend(context); | |
2387f644 | 12048 | |
93715b75 | 12049 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
2387f644 | 12050 | Bexpression* bcond = |
93715b75 | 12051 | gogo->backend()->conditional_expression(bfn, NULL, |
12052 | bnil_check, bcrash, NULL, loc); | |
0ab48656 | 12053 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
12054 | Bstatement* cond_statement = | |
12055 | gogo->backend()->expression_statement(bfunction, bcond); | |
ea664253 | 12056 | return gogo->backend()->compound_expression(cond_statement, bclosure, loc); |
e440a328 | 12057 | } |
12058 | ||
d751bb78 | 12059 | // Dump ast representation for an interface field reference. |
12060 | ||
12061 | void | |
12062 | Interface_field_reference_expression::do_dump_expression( | |
12063 | Ast_dump_context* ast_dump_context) const | |
12064 | { | |
12065 | this->expr_->dump_expression(ast_dump_context); | |
12066 | ast_dump_context->ostream() << "." << this->name_; | |
12067 | } | |
12068 | ||
e440a328 | 12069 | // Make a reference to a field in an interface. |
12070 | ||
12071 | Expression* | |
12072 | Expression::make_interface_field_reference(Expression* expr, | |
12073 | const std::string& field, | |
b13c66cd | 12074 | Location location) |
e440a328 | 12075 | { |
12076 | return new Interface_field_reference_expression(expr, field, location); | |
12077 | } | |
12078 | ||
12079 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
12080 | // is lowered after we know the type of the left hand side. | |
12081 | ||
12082 | class Selector_expression : public Parser_expression | |
12083 | { | |
12084 | public: | |
12085 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 12086 | Location location) |
e440a328 | 12087 | : Parser_expression(EXPRESSION_SELECTOR, location), |
12088 | left_(left), name_(name) | |
12089 | { } | |
12090 | ||
12091 | protected: | |
12092 | int | |
12093 | do_traverse(Traverse* traverse) | |
12094 | { return Expression::traverse(&this->left_, traverse); } | |
12095 | ||
12096 | Expression* | |
ceeb4318 | 12097 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12098 | |
12099 | Expression* | |
12100 | do_copy() | |
12101 | { | |
12102 | return new Selector_expression(this->left_->copy(), this->name_, | |
12103 | this->location()); | |
12104 | } | |
12105 | ||
d751bb78 | 12106 | void |
12107 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
12108 | ||
e440a328 | 12109 | private: |
12110 | Expression* | |
12111 | lower_method_expression(Gogo*); | |
12112 | ||
12113 | // The expression on the left hand side. | |
12114 | Expression* left_; | |
12115 | // The name on the right hand side. | |
12116 | std::string name_; | |
12117 | }; | |
12118 | ||
12119 | // Lower a selector expression once we know the real type of the left | |
12120 | // hand side. | |
12121 | ||
12122 | Expression* | |
ceeb4318 | 12123 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
12124 | int) | |
e440a328 | 12125 | { |
12126 | Expression* left = this->left_; | |
12127 | if (left->is_type_expression()) | |
12128 | return this->lower_method_expression(gogo); | |
12129 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
12130 | this->location()); | |
12131 | } | |
12132 | ||
12133 | // Lower a method expression T.M or (*T).M. We turn this into a | |
12134 | // function literal. | |
12135 | ||
12136 | Expression* | |
12137 | Selector_expression::lower_method_expression(Gogo* gogo) | |
12138 | { | |
b13c66cd | 12139 | Location location = this->location(); |
868b439e | 12140 | Type* left_type = this->left_->type(); |
12141 | Type* type = left_type; | |
e440a328 | 12142 | const std::string& name(this->name_); |
12143 | ||
12144 | bool is_pointer; | |
12145 | if (type->points_to() == NULL) | |
12146 | is_pointer = false; | |
12147 | else | |
12148 | { | |
12149 | is_pointer = true; | |
12150 | type = type->points_to(); | |
12151 | } | |
12152 | Named_type* nt = type->named_type(); | |
12153 | if (nt == NULL) | |
12154 | { | |
631d5788 | 12155 | go_error_at(location, |
12156 | ("method expression requires named type or " | |
12157 | "pointer to named type")); | |
e440a328 | 12158 | return Expression::make_error(location); |
12159 | } | |
12160 | ||
12161 | bool is_ambiguous; | |
12162 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 12163 | const Typed_identifier* imethod = NULL; |
dcc8506b | 12164 | if (method == NULL && !is_pointer) |
ab1468c3 | 12165 | { |
12166 | Interface_type* it = nt->interface_type(); | |
12167 | if (it != NULL) | |
12168 | imethod = it->find_method(name); | |
12169 | } | |
12170 | ||
868b439e | 12171 | if ((method == NULL && imethod == NULL) |
12172 | || (left_type->named_type() != NULL && left_type->points_to() != NULL)) | |
e440a328 | 12173 | { |
12174 | if (!is_ambiguous) | |
631d5788 | 12175 | go_error_at(location, "type %<%s%s%> has no method %<%s%>", |
12176 | is_pointer ? "*" : "", | |
12177 | nt->message_name().c_str(), | |
12178 | Gogo::message_name(name).c_str()); | |
e440a328 | 12179 | else |
631d5788 | 12180 | go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
12181 | Gogo::message_name(name).c_str(), | |
12182 | is_pointer ? "*" : "", | |
12183 | nt->message_name().c_str()); | |
e440a328 | 12184 | return Expression::make_error(location); |
12185 | } | |
12186 | ||
ab1468c3 | 12187 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 12188 | { |
631d5788 | 12189 | go_error_at(location, "method requires pointer (use %<(*%s).%s%>)", |
12190 | nt->message_name().c_str(), | |
12191 | Gogo::message_name(name).c_str()); | |
e440a328 | 12192 | return Expression::make_error(location); |
12193 | } | |
12194 | ||
12195 | // Build a new function type in which the receiver becomes the first | |
12196 | // argument. | |
ab1468c3 | 12197 | Function_type* method_type; |
12198 | if (method != NULL) | |
12199 | { | |
12200 | method_type = method->type(); | |
c484d925 | 12201 | go_assert(method_type->is_method()); |
ab1468c3 | 12202 | } |
12203 | else | |
12204 | { | |
12205 | method_type = imethod->type()->function_type(); | |
c484d925 | 12206 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 12207 | } |
e440a328 | 12208 | |
12209 | const char* const receiver_name = "$this"; | |
12210 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
12211 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
12212 | location)); | |
12213 | ||
12214 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
12215 | if (method_parameters != NULL) | |
12216 | { | |
f470da59 | 12217 | int i = 0; |
e440a328 | 12218 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
12219 | p != method_parameters->end(); | |
f470da59 | 12220 | ++p, ++i) |
12221 | { | |
68883531 | 12222 | if (!p->name().empty()) |
f470da59 | 12223 | parameters->push_back(*p); |
12224 | else | |
12225 | { | |
12226 | char buf[20]; | |
12227 | snprintf(buf, sizeof buf, "$param%d", i); | |
12228 | parameters->push_back(Typed_identifier(buf, p->type(), | |
12229 | p->location())); | |
12230 | } | |
12231 | } | |
e440a328 | 12232 | } |
12233 | ||
12234 | const Typed_identifier_list* method_results = method_type->results(); | |
12235 | Typed_identifier_list* results; | |
12236 | if (method_results == NULL) | |
12237 | results = NULL; | |
12238 | else | |
12239 | { | |
12240 | results = new Typed_identifier_list(); | |
12241 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
12242 | p != method_results->end(); | |
12243 | ++p) | |
12244 | results->push_back(*p); | |
12245 | } | |
12246 | ||
12247 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
12248 | location); | |
12249 | if (method_type->is_varargs()) | |
12250 | fntype->set_is_varargs(); | |
12251 | ||
12252 | // We generate methods which always takes a pointer to the receiver | |
12253 | // as their first argument. If this is for a pointer type, we can | |
12254 | // simply reuse the existing function. We use an internal hack to | |
12255 | // get the right type. | |
8381eda7 | 12256 | // FIXME: This optimization is disabled because it doesn't yet work |
12257 | // with function descriptors when the method expression is not | |
12258 | // directly called. | |
12259 | if (method != NULL && is_pointer && false) | |
e440a328 | 12260 | { |
12261 | Named_object* mno = (method->needs_stub_method() | |
12262 | ? method->stub_object() | |
12263 | : method->named_object()); | |
12264 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
12265 | f = Expression::make_cast(fntype, f, location); | |
12266 | Type_conversion_expression* tce = | |
12267 | static_cast<Type_conversion_expression*>(f); | |
12268 | tce->set_may_convert_function_types(); | |
12269 | return f; | |
12270 | } | |
12271 | ||
13f2fdb8 | 12272 | Named_object* no = gogo->start_function(gogo->thunk_name(), fntype, false, |
e440a328 | 12273 | location); |
12274 | ||
12275 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 12276 | go_assert(vno != NULL); |
e440a328 | 12277 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 12278 | Expression* bm; |
12279 | if (method != NULL) | |
12280 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
12281 | else | |
12282 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 12283 | |
12284 | // Even though we found the method above, if it has an error type we | |
12285 | // may see an error here. | |
12286 | if (bm->is_error_expression()) | |
463fe805 | 12287 | { |
12288 | gogo->finish_function(location); | |
12289 | return bm; | |
12290 | } | |
e440a328 | 12291 | |
12292 | Expression_list* args; | |
f470da59 | 12293 | if (parameters->size() <= 1) |
e440a328 | 12294 | args = NULL; |
12295 | else | |
12296 | { | |
12297 | args = new Expression_list(); | |
f470da59 | 12298 | Typed_identifier_list::const_iterator p = parameters->begin(); |
12299 | ++p; | |
12300 | for (; p != parameters->end(); ++p) | |
e440a328 | 12301 | { |
12302 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 12303 | go_assert(vno != NULL); |
e440a328 | 12304 | args->push_back(Expression::make_var_reference(vno, location)); |
12305 | } | |
12306 | } | |
12307 | ||
ceeb4318 | 12308 | gogo->start_block(location); |
12309 | ||
e440a328 | 12310 | Call_expression* call = Expression::make_call(bm, args, |
12311 | method_type->is_varargs(), | |
12312 | location); | |
12313 | ||
0afbb937 | 12314 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 12315 | gogo->add_statement(s); |
12316 | ||
ceeb4318 | 12317 | Block* b = gogo->finish_block(location); |
12318 | ||
12319 | gogo->add_block(b, location); | |
12320 | ||
12321 | // Lower the call in case there are multiple results. | |
12322 | gogo->lower_block(no, b); | |
a32698ee | 12323 | gogo->flatten_block(no, b); |
ceeb4318 | 12324 | |
e440a328 | 12325 | gogo->finish_function(location); |
12326 | ||
12327 | return Expression::make_func_reference(no, NULL, location); | |
12328 | } | |
12329 | ||
d751bb78 | 12330 | // Dump the ast for a selector expression. |
12331 | ||
12332 | void | |
12333 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12334 | const | |
12335 | { | |
12336 | ast_dump_context->dump_expression(this->left_); | |
12337 | ast_dump_context->ostream() << "."; | |
12338 | ast_dump_context->ostream() << this->name_; | |
12339 | } | |
12340 | ||
e440a328 | 12341 | // Make a selector expression. |
12342 | ||
12343 | Expression* | |
12344 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 12345 | Location location) |
e440a328 | 12346 | { |
12347 | return new Selector_expression(left, name, location); | |
12348 | } | |
12349 | ||
da244e59 | 12350 | // Class Allocation_expression. |
e440a328 | 12351 | |
da244e59 | 12352 | int |
12353 | Allocation_expression::do_traverse(Traverse* traverse) | |
e440a328 | 12354 | { |
da244e59 | 12355 | return Type::traverse(this->type_, traverse); |
12356 | } | |
e440a328 | 12357 | |
da244e59 | 12358 | Type* |
12359 | Allocation_expression::do_type() | |
12360 | { | |
12361 | return Type::make_pointer_type(this->type_); | |
12362 | } | |
e440a328 | 12363 | |
22deed0d | 12364 | void |
12365 | Allocation_expression::do_check_types(Gogo*) | |
12366 | { | |
12367 | if (!this->type_->in_heap()) | |
12368 | go_error_at(this->location(), "can't heap allocate go:notinheap type"); | |
12369 | } | |
12370 | ||
da244e59 | 12371 | // Make a copy of an allocation expression. |
e440a328 | 12372 | |
da244e59 | 12373 | Expression* |
12374 | Allocation_expression::do_copy() | |
12375 | { | |
12376 | Allocation_expression* alloc = | |
12377 | new Allocation_expression(this->type_, this->location()); | |
12378 | if (this->allocate_on_stack_) | |
12379 | alloc->set_allocate_on_stack(); | |
12380 | return alloc; | |
12381 | } | |
e440a328 | 12382 | |
ea664253 | 12383 | // Return the backend representation for an allocation expression. |
e440a328 | 12384 | |
ea664253 | 12385 | Bexpression* |
12386 | Allocation_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12387 | { |
2c809f8f | 12388 | Gogo* gogo = context->gogo(); |
12389 | Location loc = this->location(); | |
06e83d10 | 12390 | Btype* btype = this->type_->get_backend(gogo); |
da244e59 | 12391 | |
5973ede0 | 12392 | if (this->allocate_on_stack_) |
da244e59 | 12393 | { |
2a305b85 | 12394 | int64_t size; |
12395 | bool ok = this->type_->backend_type_size(gogo, &size); | |
12396 | if (!ok) | |
12397 | { | |
12398 | go_assert(saw_errors()); | |
12399 | return gogo->backend()->error_expression(); | |
12400 | } | |
06e83d10 | 12401 | Bstatement* decl; |
12402 | Named_object* fn = context->function(); | |
12403 | go_assert(fn != NULL); | |
12404 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
12405 | Bexpression* zero = gogo->backend()->zero_expression(btype); | |
12406 | Bvariable* temp = | |
12407 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
12408 | zero, true, loc, &decl); | |
12409 | Bexpression* ret = gogo->backend()->var_expression(temp, loc); | |
12410 | ret = gogo->backend()->address_expression(ret, loc); | |
12411 | ret = gogo->backend()->compound_expression(decl, ret, loc); | |
12412 | return ret; | |
da244e59 | 12413 | } |
12414 | ||
2a305b85 | 12415 | Bexpression* space = |
ea664253 | 12416 | gogo->allocate_memory(this->type_, loc)->get_backend(context); |
d5d1c295 | 12417 | Btype* pbtype = gogo->backend()->pointer_type(btype); |
ea664253 | 12418 | return gogo->backend()->convert_expression(pbtype, space, loc); |
e440a328 | 12419 | } |
12420 | ||
d751bb78 | 12421 | // Dump ast representation for an allocation expression. |
12422 | ||
12423 | void | |
12424 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12425 | const | |
12426 | { | |
12427 | ast_dump_context->ostream() << "new("; | |
12428 | ast_dump_context->dump_type(this->type_); | |
12429 | ast_dump_context->ostream() << ")"; | |
12430 | } | |
12431 | ||
e440a328 | 12432 | // Make an allocation expression. |
12433 | ||
12434 | Expression* | |
b13c66cd | 12435 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12436 | { |
12437 | return new Allocation_expression(type, location); | |
12438 | } | |
12439 | ||
e32de7ba | 12440 | // Class Ordered_value_list. |
e440a328 | 12441 | |
12442 | int | |
e32de7ba | 12443 | Ordered_value_list::traverse_vals(Traverse* traverse) |
e440a328 | 12444 | { |
0c4f5a19 | 12445 | if (this->vals_ != NULL) |
12446 | { | |
12447 | if (this->traverse_order_ == NULL) | |
12448 | { | |
12449 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12450 | return TRAVERSE_EXIT; | |
12451 | } | |
12452 | else | |
12453 | { | |
e32de7ba | 12454 | for (std::vector<unsigned long>::const_iterator p = |
12455 | this->traverse_order_->begin(); | |
0c4f5a19 | 12456 | p != this->traverse_order_->end(); |
12457 | ++p) | |
12458 | { | |
12459 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12460 | == TRAVERSE_EXIT) | |
12461 | return TRAVERSE_EXIT; | |
12462 | } | |
12463 | } | |
12464 | } | |
e32de7ba | 12465 | return TRAVERSE_CONTINUE; |
12466 | } | |
12467 | ||
12468 | // Class Struct_construction_expression. | |
12469 | ||
12470 | // Traversal. | |
12471 | ||
12472 | int | |
12473 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12474 | { | |
12475 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) | |
12476 | return TRAVERSE_EXIT; | |
e440a328 | 12477 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12478 | return TRAVERSE_EXIT; | |
12479 | return TRAVERSE_CONTINUE; | |
12480 | } | |
12481 | ||
12482 | // Return whether this is a constant initializer. | |
12483 | ||
12484 | bool | |
12485 | Struct_construction_expression::is_constant_struct() const | |
12486 | { | |
e32de7ba | 12487 | if (this->vals() == NULL) |
e440a328 | 12488 | return true; |
e32de7ba | 12489 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12490 | pv != this->vals()->end(); | |
e440a328 | 12491 | ++pv) |
12492 | { | |
12493 | if (*pv != NULL | |
12494 | && !(*pv)->is_constant() | |
12495 | && (!(*pv)->is_composite_literal() | |
12496 | || (*pv)->is_nonconstant_composite_literal())) | |
12497 | return false; | |
12498 | } | |
12499 | ||
12500 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12501 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12502 | pf != fields->end(); | |
12503 | ++pf) | |
12504 | { | |
12505 | // There are no constant constructors for interfaces. | |
12506 | if (pf->type()->interface_type() != NULL) | |
12507 | return false; | |
12508 | } | |
12509 | ||
12510 | return true; | |
12511 | } | |
12512 | ||
3ae06f68 | 12513 | // Return whether this struct can be used as a constant initializer. |
f9ca30f9 | 12514 | |
12515 | bool | |
3ae06f68 | 12516 | Struct_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12517 | { |
e32de7ba | 12518 | if (this->vals() == NULL) |
f9ca30f9 | 12519 | return true; |
e32de7ba | 12520 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12521 | pv != this->vals()->end(); | |
f9ca30f9 | 12522 | ++pv) |
12523 | { | |
3ae06f68 | 12524 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12525 | return false; |
12526 | } | |
de048538 | 12527 | |
12528 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12529 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12530 | pf != fields->end(); | |
12531 | ++pf) | |
12532 | { | |
12533 | // There are no constant constructors for interfaces. | |
12534 | if (pf->type()->interface_type() != NULL) | |
12535 | return false; | |
12536 | } | |
12537 | ||
f9ca30f9 | 12538 | return true; |
12539 | } | |
12540 | ||
e440a328 | 12541 | // Final type determination. |
12542 | ||
12543 | void | |
12544 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12545 | { | |
e32de7ba | 12546 | if (this->vals() == NULL) |
e440a328 | 12547 | return; |
12548 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
e32de7ba | 12549 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12550 | for (Struct_field_list::const_iterator pf = fields->begin(); |
12551 | pf != fields->end(); | |
12552 | ++pf, ++pv) | |
12553 | { | |
e32de7ba | 12554 | if (pv == this->vals()->end()) |
e440a328 | 12555 | return; |
12556 | if (*pv != NULL) | |
12557 | { | |
12558 | Type_context subcontext(pf->type(), false); | |
12559 | (*pv)->determine_type(&subcontext); | |
12560 | } | |
12561 | } | |
a6cb4c0e | 12562 | // Extra values are an error we will report elsewhere; we still want |
12563 | // to determine the type to avoid knockon errors. | |
e32de7ba | 12564 | for (; pv != this->vals()->end(); ++pv) |
a6cb4c0e | 12565 | (*pv)->determine_type_no_context(); |
e440a328 | 12566 | } |
12567 | ||
12568 | // Check types. | |
12569 | ||
12570 | void | |
12571 | Struct_construction_expression::do_check_types(Gogo*) | |
12572 | { | |
e32de7ba | 12573 | if (this->vals() == NULL) |
e440a328 | 12574 | return; |
12575 | ||
12576 | Struct_type* st = this->type_->struct_type(); | |
e32de7ba | 12577 | if (this->vals()->size() > st->field_count()) |
e440a328 | 12578 | { |
12579 | this->report_error(_("too many expressions for struct")); | |
12580 | return; | |
12581 | } | |
12582 | ||
12583 | const Struct_field_list* fields = st->fields(); | |
e32de7ba | 12584 | Expression_list::const_iterator pv = this->vals()->begin(); |
e440a328 | 12585 | int i = 0; |
12586 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12587 | pf != fields->end(); | |
12588 | ++pf, ++pv, ++i) | |
12589 | { | |
e32de7ba | 12590 | if (pv == this->vals()->end()) |
e440a328 | 12591 | { |
12592 | this->report_error(_("too few expressions for struct")); | |
12593 | break; | |
12594 | } | |
12595 | ||
12596 | if (*pv == NULL) | |
12597 | continue; | |
12598 | ||
12599 | std::string reason; | |
12600 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12601 | { | |
12602 | if (reason.empty()) | |
631d5788 | 12603 | go_error_at((*pv)->location(), |
12604 | "incompatible type for field %d in struct construction", | |
12605 | i + 1); | |
e440a328 | 12606 | else |
631d5788 | 12607 | go_error_at((*pv)->location(), |
12608 | ("incompatible type for field %d in " | |
12609 | "struct construction (%s)"), | |
12610 | i + 1, reason.c_str()); | |
e440a328 | 12611 | this->set_is_error(); |
12612 | } | |
12613 | } | |
e32de7ba | 12614 | go_assert(pv == this->vals()->end()); |
e440a328 | 12615 | } |
12616 | ||
8ba8cc87 | 12617 | // Flatten a struct construction expression. Store the values into |
12618 | // temporaries in case they need interface conversion. | |
12619 | ||
12620 | Expression* | |
12621 | Struct_construction_expression::do_flatten(Gogo*, Named_object*, | |
12622 | Statement_inserter* inserter) | |
12623 | { | |
e32de7ba | 12624 | if (this->vals() == NULL) |
8ba8cc87 | 12625 | return this; |
12626 | ||
12627 | // If this is a constant struct, we don't need temporaries. | |
de048538 | 12628 | if (this->is_constant_struct() || this->is_static_initializer()) |
8ba8cc87 | 12629 | return this; |
12630 | ||
12631 | Location loc = this->location(); | |
e32de7ba | 12632 | for (Expression_list::iterator pv = this->vals()->begin(); |
12633 | pv != this->vals()->end(); | |
8ba8cc87 | 12634 | ++pv) |
12635 | { | |
12636 | if (*pv != NULL) | |
12637 | { | |
5bf8be8b | 12638 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12639 | { | |
12640 | go_assert(saw_errors()); | |
12641 | return Expression::make_error(loc); | |
12642 | } | |
8ba8cc87 | 12643 | if (!(*pv)->is_variable()) |
12644 | { | |
12645 | Temporary_statement* temp = | |
12646 | Statement::make_temporary(NULL, *pv, loc); | |
12647 | inserter->insert(temp); | |
12648 | *pv = Expression::make_temporary_reference(temp, loc); | |
12649 | } | |
12650 | } | |
12651 | } | |
12652 | return this; | |
12653 | } | |
12654 | ||
ea664253 | 12655 | // Return the backend representation for constructing a struct. |
e440a328 | 12656 | |
ea664253 | 12657 | Bexpression* |
12658 | Struct_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 12659 | { |
12660 | Gogo* gogo = context->gogo(); | |
12661 | ||
2c809f8f | 12662 | Btype* btype = this->type_->get_backend(gogo); |
e32de7ba | 12663 | if (this->vals() == NULL) |
ea664253 | 12664 | return gogo->backend()->zero_expression(btype); |
e440a328 | 12665 | |
e440a328 | 12666 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e32de7ba | 12667 | Expression_list::const_iterator pv = this->vals()->begin(); |
2c809f8f | 12668 | std::vector<Bexpression*> init; |
12669 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12670 | pf != fields->end(); | |
12671 | ++pf) | |
e440a328 | 12672 | { |
63697958 | 12673 | Btype* fbtype = pf->type()->get_backend(gogo); |
e32de7ba | 12674 | if (pv == this->vals()->end()) |
2c809f8f | 12675 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12676 | else if (*pv == NULL) |
12677 | { | |
2c809f8f | 12678 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12679 | ++pv; |
12680 | } | |
12681 | else | |
12682 | { | |
2c809f8f | 12683 | Expression* val = |
12684 | Expression::convert_for_assignment(gogo, pf->type(), | |
12685 | *pv, this->location()); | |
ea664253 | 12686 | init.push_back(val->get_backend(context)); |
e440a328 | 12687 | ++pv; |
12688 | } | |
e440a328 | 12689 | } |
ea664253 | 12690 | return gogo->backend()->constructor_expression(btype, init, this->location()); |
e440a328 | 12691 | } |
12692 | ||
12693 | // Export a struct construction. | |
12694 | ||
12695 | void | |
12696 | Struct_construction_expression::do_export(Export* exp) const | |
12697 | { | |
12698 | exp->write_c_string("convert("); | |
12699 | exp->write_type(this->type_); | |
e32de7ba | 12700 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12701 | pv != this->vals()->end(); | |
e440a328 | 12702 | ++pv) |
12703 | { | |
12704 | exp->write_c_string(", "); | |
12705 | if (*pv != NULL) | |
12706 | (*pv)->export_expression(exp); | |
12707 | } | |
12708 | exp->write_c_string(")"); | |
12709 | } | |
12710 | ||
d751bb78 | 12711 | // Dump ast representation of a struct construction expression. |
12712 | ||
12713 | void | |
12714 | Struct_construction_expression::do_dump_expression( | |
12715 | Ast_dump_context* ast_dump_context) const | |
12716 | { | |
d751bb78 | 12717 | ast_dump_context->dump_type(this->type_); |
12718 | ast_dump_context->ostream() << "{"; | |
e32de7ba | 12719 | ast_dump_context->dump_expression_list(this->vals()); |
d751bb78 | 12720 | ast_dump_context->ostream() << "}"; |
12721 | } | |
12722 | ||
e440a328 | 12723 | // Make a struct composite literal. This used by the thunk code. |
12724 | ||
12725 | Expression* | |
12726 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12727 | Location location) |
e440a328 | 12728 | { |
c484d925 | 12729 | go_assert(type->struct_type() != NULL); |
e440a328 | 12730 | return new Struct_construction_expression(type, vals, location); |
12731 | } | |
12732 | ||
da244e59 | 12733 | // Class Array_construction_expression. |
e440a328 | 12734 | |
12735 | // Traversal. | |
12736 | ||
12737 | int | |
12738 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12739 | { | |
e32de7ba | 12740 | if (this->traverse_vals(traverse) == TRAVERSE_EXIT) |
e440a328 | 12741 | return TRAVERSE_EXIT; |
12742 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12743 | return TRAVERSE_EXIT; | |
12744 | return TRAVERSE_CONTINUE; | |
12745 | } | |
12746 | ||
12747 | // Return whether this is a constant initializer. | |
12748 | ||
12749 | bool | |
12750 | Array_construction_expression::is_constant_array() const | |
12751 | { | |
e32de7ba | 12752 | if (this->vals() == NULL) |
e440a328 | 12753 | return true; |
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(); | |
e440a328 | 12761 | ++pv) |
12762 | { | |
12763 | if (*pv != NULL | |
12764 | && !(*pv)->is_constant() | |
12765 | && (!(*pv)->is_composite_literal() | |
12766 | || (*pv)->is_nonconstant_composite_literal())) | |
12767 | return false; | |
12768 | } | |
12769 | return true; | |
12770 | } | |
12771 | ||
3ae06f68 | 12772 | // Return whether this can be used a constant initializer. |
f9ca30f9 | 12773 | |
12774 | bool | |
3ae06f68 | 12775 | Array_construction_expression::do_is_static_initializer() const |
f9ca30f9 | 12776 | { |
e32de7ba | 12777 | if (this->vals() == NULL) |
f9ca30f9 | 12778 | return true; |
de048538 | 12779 | |
12780 | // There are no constant constructors for interfaces. | |
12781 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12782 | return false; | |
12783 | ||
e32de7ba | 12784 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12785 | pv != this->vals()->end(); | |
f9ca30f9 | 12786 | ++pv) |
12787 | { | |
3ae06f68 | 12788 | if (*pv != NULL && !(*pv)->is_static_initializer()) |
f9ca30f9 | 12789 | return false; |
12790 | } | |
12791 | return true; | |
12792 | } | |
12793 | ||
e440a328 | 12794 | // Final type determination. |
12795 | ||
12796 | void | |
12797 | Array_construction_expression::do_determine_type(const Type_context*) | |
12798 | { | |
e32de7ba | 12799 | if (this->vals() == NULL) |
e440a328 | 12800 | return; |
12801 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
e32de7ba | 12802 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12803 | pv != this->vals()->end(); | |
e440a328 | 12804 | ++pv) |
12805 | { | |
12806 | if (*pv != NULL) | |
12807 | (*pv)->determine_type(&subcontext); | |
12808 | } | |
12809 | } | |
12810 | ||
12811 | // Check types. | |
12812 | ||
12813 | void | |
12814 | Array_construction_expression::do_check_types(Gogo*) | |
12815 | { | |
e32de7ba | 12816 | if (this->vals() == NULL) |
e440a328 | 12817 | return; |
12818 | ||
12819 | Array_type* at = this->type_->array_type(); | |
12820 | int i = 0; | |
12821 | Type* element_type = at->element_type(); | |
e32de7ba | 12822 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12823 | pv != this->vals()->end(); | |
e440a328 | 12824 | ++pv, ++i) |
12825 | { | |
12826 | if (*pv != NULL | |
12827 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12828 | { | |
631d5788 | 12829 | go_error_at((*pv)->location(), |
12830 | "incompatible type for element %d in composite literal", | |
12831 | i + 1); | |
e440a328 | 12832 | this->set_is_error(); |
12833 | } | |
12834 | } | |
e440a328 | 12835 | } |
12836 | ||
8ba8cc87 | 12837 | // Flatten an array construction expression. Store the values into |
12838 | // temporaries in case they need interface conversion. | |
12839 | ||
12840 | Expression* | |
12841 | Array_construction_expression::do_flatten(Gogo*, Named_object*, | |
12842 | Statement_inserter* inserter) | |
12843 | { | |
e32de7ba | 12844 | if (this->vals() == NULL) |
8ba8cc87 | 12845 | return this; |
12846 | ||
12847 | // If this is a constant array, we don't need temporaries. | |
de048538 | 12848 | if (this->is_constant_array() || this->is_static_initializer()) |
8ba8cc87 | 12849 | return this; |
12850 | ||
12851 | Location loc = this->location(); | |
e32de7ba | 12852 | for (Expression_list::iterator pv = this->vals()->begin(); |
12853 | pv != this->vals()->end(); | |
8ba8cc87 | 12854 | ++pv) |
12855 | { | |
12856 | if (*pv != NULL) | |
12857 | { | |
5bf8be8b | 12858 | if ((*pv)->is_error_expression() || (*pv)->type()->is_error_type()) |
12859 | { | |
12860 | go_assert(saw_errors()); | |
12861 | return Expression::make_error(loc); | |
12862 | } | |
8ba8cc87 | 12863 | if (!(*pv)->is_variable()) |
12864 | { | |
12865 | Temporary_statement* temp = | |
12866 | Statement::make_temporary(NULL, *pv, loc); | |
12867 | inserter->insert(temp); | |
12868 | *pv = Expression::make_temporary_reference(temp, loc); | |
12869 | } | |
12870 | } | |
12871 | } | |
12872 | return this; | |
12873 | } | |
12874 | ||
2c809f8f | 12875 | // Get a constructor expression for the array values. |
e440a328 | 12876 | |
2c809f8f | 12877 | Bexpression* |
12878 | Array_construction_expression::get_constructor(Translate_context* context, | |
12879 | Btype* array_btype) | |
e440a328 | 12880 | { |
e440a328 | 12881 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12882 | |
12883 | std::vector<unsigned long> indexes; | |
12884 | std::vector<Bexpression*> vals; | |
12885 | Gogo* gogo = context->gogo(); | |
e32de7ba | 12886 | if (this->vals() != NULL) |
e440a328 | 12887 | { |
12888 | size_t i = 0; | |
ffe743ca | 12889 | std::vector<unsigned long>::const_iterator pi; |
12890 | if (this->indexes_ != NULL) | |
12891 | pi = this->indexes_->begin(); | |
e32de7ba | 12892 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12893 | pv != this->vals()->end(); | |
e440a328 | 12894 | ++pv, ++i) |
12895 | { | |
ffe743ca | 12896 | if (this->indexes_ != NULL) |
12897 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12898 | |
12899 | if (this->indexes_ == NULL) | |
2c809f8f | 12900 | indexes.push_back(i); |
ffe743ca | 12901 | else |
2c809f8f | 12902 | indexes.push_back(*pi); |
e440a328 | 12903 | if (*pv == NULL) |
63697958 | 12904 | { |
63697958 | 12905 | Btype* ebtype = element_type->get_backend(gogo); |
12906 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12907 | vals.push_back(zv); |
63697958 | 12908 | } |
e440a328 | 12909 | else |
12910 | { | |
2c809f8f | 12911 | Expression* val_expr = |
12912 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12913 | this->location()); | |
ea664253 | 12914 | vals.push_back(val_expr->get_backend(context)); |
e440a328 | 12915 | } |
ffe743ca | 12916 | if (this->indexes_ != NULL) |
12917 | ++pi; | |
e440a328 | 12918 | } |
ffe743ca | 12919 | if (this->indexes_ != NULL) |
12920 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12921 | } |
2c809f8f | 12922 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12923 | vals, this->location()); | |
e440a328 | 12924 | } |
12925 | ||
12926 | // Export an array construction. | |
12927 | ||
12928 | void | |
12929 | Array_construction_expression::do_export(Export* exp) const | |
12930 | { | |
12931 | exp->write_c_string("convert("); | |
12932 | exp->write_type(this->type_); | |
e32de7ba | 12933 | if (this->vals() != NULL) |
e440a328 | 12934 | { |
ffe743ca | 12935 | std::vector<unsigned long>::const_iterator pi; |
12936 | if (this->indexes_ != NULL) | |
12937 | pi = this->indexes_->begin(); | |
e32de7ba | 12938 | for (Expression_list::const_iterator pv = this->vals()->begin(); |
12939 | pv != this->vals()->end(); | |
e440a328 | 12940 | ++pv) |
12941 | { | |
12942 | exp->write_c_string(", "); | |
ffe743ca | 12943 | |
12944 | if (this->indexes_ != NULL) | |
12945 | { | |
12946 | char buf[100]; | |
12947 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12948 | exp->write_c_string(buf); | |
12949 | exp->write_c_string(":"); | |
12950 | } | |
12951 | ||
e440a328 | 12952 | if (*pv != NULL) |
12953 | (*pv)->export_expression(exp); | |
ffe743ca | 12954 | |
12955 | if (this->indexes_ != NULL) | |
12956 | ++pi; | |
e440a328 | 12957 | } |
12958 | } | |
12959 | exp->write_c_string(")"); | |
12960 | } | |
12961 | ||
0e9a2e72 | 12962 | // Dump ast representation of an array construction expression. |
d751bb78 | 12963 | |
12964 | void | |
12965 | Array_construction_expression::do_dump_expression( | |
12966 | Ast_dump_context* ast_dump_context) const | |
12967 | { | |
ffe743ca | 12968 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12969 | |
12970 | ast_dump_context->ostream() << "[" ; | |
12971 | if (length != NULL) | |
12972 | { | |
12973 | ast_dump_context->dump_expression(length); | |
12974 | } | |
12975 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12976 | ast_dump_context->dump_type(this->type_); |
0e9a2e72 | 12977 | this->dump_slice_storage_expression(ast_dump_context); |
d751bb78 | 12978 | ast_dump_context->ostream() << "{" ; |
ffe743ca | 12979 | if (this->indexes_ == NULL) |
e32de7ba | 12980 | ast_dump_context->dump_expression_list(this->vals()); |
ffe743ca | 12981 | else |
12982 | { | |
e32de7ba | 12983 | Expression_list::const_iterator pv = this->vals()->begin(); |
ffe743ca | 12984 | for (std::vector<unsigned long>::const_iterator pi = |
12985 | this->indexes_->begin(); | |
12986 | pi != this->indexes_->end(); | |
12987 | ++pi, ++pv) | |
12988 | { | |
12989 | if (pi != this->indexes_->begin()) | |
12990 | ast_dump_context->ostream() << ", "; | |
12991 | ast_dump_context->ostream() << *pi << ':'; | |
12992 | ast_dump_context->dump_expression(*pv); | |
12993 | } | |
12994 | } | |
d751bb78 | 12995 | ast_dump_context->ostream() << "}" ; |
12996 | ||
12997 | } | |
12998 | ||
da244e59 | 12999 | // Class Fixed_array_construction_expression. |
e440a328 | 13000 | |
da244e59 | 13001 | Fixed_array_construction_expression::Fixed_array_construction_expression( |
13002 | Type* type, const std::vector<unsigned long>* indexes, | |
13003 | Expression_list* vals, Location location) | |
13004 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
13005 | type, indexes, vals, location) | |
13006 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 13007 | |
ea664253 | 13008 | // Return the backend representation for constructing a fixed array. |
e440a328 | 13009 | |
ea664253 | 13010 | Bexpression* |
13011 | Fixed_array_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13012 | { |
9f0e0513 | 13013 | Type* type = this->type(); |
13014 | Btype* btype = type->get_backend(context->gogo()); | |
ea664253 | 13015 | return this->get_constructor(context, btype); |
e440a328 | 13016 | } |
13017 | ||
76f85fd6 | 13018 | Expression* |
13019 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
13020 | Location location) | |
13021 | { | |
13022 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
13023 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
13024 | } | |
13025 | ||
da244e59 | 13026 | // Class Slice_construction_expression. |
e440a328 | 13027 | |
da244e59 | 13028 | Slice_construction_expression::Slice_construction_expression( |
13029 | Type* type, const std::vector<unsigned long>* indexes, | |
13030 | Expression_list* vals, Location location) | |
13031 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
13032 | type, indexes, vals, location), | |
0e9a2e72 | 13033 | valtype_(NULL), array_val_(NULL), slice_storage_(NULL), |
13034 | storage_escapes_(true) | |
e440a328 | 13035 | { |
da244e59 | 13036 | go_assert(type->is_slice_type()); |
23d77f91 | 13037 | |
da244e59 | 13038 | unsigned long lenval; |
13039 | Expression* length; | |
13040 | if (vals == NULL || vals->empty()) | |
13041 | lenval = 0; | |
13042 | else | |
13043 | { | |
13044 | if (this->indexes() == NULL) | |
13045 | lenval = vals->size(); | |
13046 | else | |
13047 | lenval = indexes->back() + 1; | |
13048 | } | |
13049 | Type* int_type = Type::lookup_integer_type("int"); | |
13050 | length = Expression::make_integer_ul(lenval, int_type, location); | |
13051 | Type* element_type = type->array_type()->element_type(); | |
6bf4793c | 13052 | Array_type* array_type = Type::make_array_type(element_type, length); |
13053 | array_type->set_is_array_incomparable(); | |
13054 | this->valtype_ = array_type; | |
da244e59 | 13055 | } |
e440a328 | 13056 | |
23d77f91 | 13057 | // Traversal. |
13058 | ||
13059 | int | |
13060 | Slice_construction_expression::do_traverse(Traverse* traverse) | |
13061 | { | |
13062 | if (this->Array_construction_expression::do_traverse(traverse) | |
13063 | == TRAVERSE_EXIT) | |
13064 | return TRAVERSE_EXIT; | |
13065 | if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT) | |
13066 | return TRAVERSE_EXIT; | |
0e9a2e72 | 13067 | if (this->array_val_ != NULL |
13068 | && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT) | |
13069 | return TRAVERSE_EXIT; | |
13070 | if (this->slice_storage_ != NULL | |
13071 | && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT) | |
13072 | return TRAVERSE_EXIT; | |
23d77f91 | 13073 | return TRAVERSE_CONTINUE; |
13074 | } | |
13075 | ||
0e9a2e72 | 13076 | // Helper routine to create fixed array value underlying the slice literal. |
13077 | // May be called during flattening, or later during do_get_backend(). | |
e440a328 | 13078 | |
0e9a2e72 | 13079 | Expression* |
13080 | Slice_construction_expression::create_array_val() | |
e440a328 | 13081 | { |
f9c68f17 | 13082 | Array_type* array_type = this->type()->array_type(); |
13083 | if (array_type == NULL) | |
13084 | { | |
c484d925 | 13085 | go_assert(this->type()->is_error()); |
0e9a2e72 | 13086 | return NULL; |
f9c68f17 | 13087 | } |
13088 | ||
f23d7786 | 13089 | Location loc = this->location(); |
23d77f91 | 13090 | go_assert(this->valtype_ != NULL); |
3d60812e | 13091 | |
f23d7786 | 13092 | Expression_list* vals = this->vals(); |
0e9a2e72 | 13093 | return new Fixed_array_construction_expression( |
13094 | this->valtype_, this->indexes(), vals, loc); | |
13095 | } | |
13096 | ||
13097 | // If we're previous established that the slice storage does not | |
13098 | // escape, then create a separate array temp val here for it. We | |
13099 | // need to do this as part of flattening so as to be able to insert | |
13100 | // the new temp statement. | |
13101 | ||
13102 | Expression* | |
13103 | Slice_construction_expression::do_flatten(Gogo* gogo, Named_object* no, | |
13104 | Statement_inserter* inserter) | |
13105 | { | |
13106 | if (this->type()->array_type() == NULL) | |
13107 | return NULL; | |
13108 | ||
13109 | // Base class flattening first | |
13110 | this->Array_construction_expression::do_flatten(gogo, no, inserter); | |
13111 | ||
a1bbc2c3 | 13112 | // Create a stack-allocated storage temp if storage won't escape |
03118c21 | 13113 | if (!this->storage_escapes_ |
13114 | && this->slice_storage_ == NULL | |
13115 | && this->element_count() > 0) | |
0e9a2e72 | 13116 | { |
13117 | Location loc = this->location(); | |
03118c21 | 13118 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13119 | go_assert(this->array_val_); |
13120 | Temporary_statement* temp = | |
13121 | Statement::make_temporary(this->valtype_, this->array_val_, loc); | |
13122 | inserter->insert(temp); | |
13123 | this->slice_storage_ = Expression::make_temporary_reference(temp, loc); | |
13124 | } | |
13125 | return this; | |
13126 | } | |
13127 | ||
13128 | // When dumping a slice construction expression that has an explicit | |
13129 | // storeage temp, emit the temp here (if we don't do this the storage | |
13130 | // temp appears unused in the AST dump). | |
13131 | ||
13132 | void | |
13133 | Slice_construction_expression:: | |
13134 | dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const | |
13135 | { | |
13136 | if (this->slice_storage_ == NULL) | |
13137 | return; | |
13138 | ast_dump_context->ostream() << "storage=" ; | |
13139 | ast_dump_context->dump_expression(this->slice_storage_); | |
13140 | } | |
13141 | ||
13142 | // Return the backend representation for constructing a slice. | |
13143 | ||
13144 | Bexpression* | |
13145 | Slice_construction_expression::do_get_backend(Translate_context* context) | |
13146 | { | |
13147 | if (this->array_val_ == NULL) | |
03118c21 | 13148 | this->array_val_ = this->create_array_val(); |
0e9a2e72 | 13149 | if (this->array_val_ == NULL) |
13150 | { | |
13151 | go_assert(this->type()->is_error()); | |
13152 | return context->backend()->error_expression(); | |
13153 | } | |
13154 | ||
13155 | Location loc = this->location(); | |
e440a328 | 13156 | |
3ae06f68 | 13157 | bool is_static_initializer = this->array_val_->is_static_initializer(); |
d8829beb | 13158 | |
13159 | // We have to copy the initial values into heap memory if we are in | |
3ae06f68 | 13160 | // a function or if the values are not constants. |
13161 | bool copy_to_heap = context->function() != NULL || !is_static_initializer; | |
e440a328 | 13162 | |
f23d7786 | 13163 | Expression* space; |
0e9a2e72 | 13164 | |
13165 | if (this->slice_storage_ != NULL) | |
13166 | { | |
13167 | go_assert(!this->storage_escapes_); | |
13168 | space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc); | |
13169 | } | |
13170 | else if (!copy_to_heap) | |
e440a328 | 13171 | { |
f23d7786 | 13172 | // The initializer will only run once. |
0e9a2e72 | 13173 | space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc); |
f23d7786 | 13174 | space->unary_expression()->set_is_slice_init(); |
e440a328 | 13175 | } |
13176 | else | |
45ff893b | 13177 | { |
5973ede0 | 13178 | go_assert(this->storage_escapes_ || this->element_count() == 0); |
0e9a2e72 | 13179 | space = Expression::make_heap_expression(this->array_val_, loc); |
45ff893b | 13180 | } |
e440a328 | 13181 | |
2c809f8f | 13182 | // Build a constructor for the slice. |
f23d7786 | 13183 | Expression* len = this->valtype_->array_type()->length(); |
13184 | Expression* slice_val = | |
13185 | Expression::make_slice_value(this->type(), space, len, len, loc); | |
ea664253 | 13186 | return slice_val->get_backend(context); |
e440a328 | 13187 | } |
13188 | ||
13189 | // Make a slice composite literal. This is used by the type | |
13190 | // descriptor code. | |
13191 | ||
0e9a2e72 | 13192 | Slice_construction_expression* |
e440a328 | 13193 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, |
b13c66cd | 13194 | Location location) |
e440a328 | 13195 | { |
411eb89e | 13196 | go_assert(type->is_slice_type()); |
2c809f8f | 13197 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 13198 | } |
13199 | ||
da244e59 | 13200 | // Class Map_construction_expression. |
e440a328 | 13201 | |
13202 | // Traversal. | |
13203 | ||
13204 | int | |
13205 | Map_construction_expression::do_traverse(Traverse* traverse) | |
13206 | { | |
13207 | if (this->vals_ != NULL | |
13208 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
13209 | return TRAVERSE_EXIT; | |
13210 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13211 | return TRAVERSE_EXIT; | |
13212 | return TRAVERSE_CONTINUE; | |
13213 | } | |
13214 | ||
2c809f8f | 13215 | // Flatten constructor initializer into a temporary variable since |
13216 | // we need to take its address for __go_construct_map. | |
13217 | ||
13218 | Expression* | |
13219 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
13220 | Statement_inserter* inserter) | |
13221 | { | |
13222 | if (!this->is_error_expression() | |
13223 | && this->vals_ != NULL | |
13224 | && !this->vals_->empty() | |
13225 | && this->constructor_temp_ == NULL) | |
13226 | { | |
13227 | Map_type* mt = this->type_->map_type(); | |
13228 | Type* key_type = mt->key_type(); | |
13229 | Type* val_type = mt->val_type(); | |
13230 | this->element_type_ = Type::make_builtin_struct_type(2, | |
13231 | "__key", key_type, | |
13232 | "__val", val_type); | |
13233 | ||
13234 | Expression_list* value_pairs = new Expression_list(); | |
13235 | Location loc = this->location(); | |
13236 | ||
13237 | size_t i = 0; | |
13238 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13239 | pv != this->vals_->end(); | |
13240 | ++pv, ++i) | |
13241 | { | |
13242 | Expression_list* key_value_pair = new Expression_list(); | |
91c0fd76 | 13243 | Expression* key = *pv; |
5bf8be8b | 13244 | if (key->is_error_expression() || key->type()->is_error_type()) |
13245 | { | |
13246 | go_assert(saw_errors()); | |
13247 | return Expression::make_error(loc); | |
13248 | } | |
91c0fd76 | 13249 | if (key->type()->interface_type() != NULL && !key->is_variable()) |
13250 | { | |
13251 | Temporary_statement* temp = | |
13252 | Statement::make_temporary(NULL, key, loc); | |
13253 | inserter->insert(temp); | |
13254 | key = Expression::make_temporary_reference(temp, loc); | |
13255 | } | |
13256 | key = Expression::convert_for_assignment(gogo, key_type, key, loc); | |
2c809f8f | 13257 | |
13258 | ++pv; | |
91c0fd76 | 13259 | Expression* val = *pv; |
5bf8be8b | 13260 | if (val->is_error_expression() || val->type()->is_error_type()) |
13261 | { | |
13262 | go_assert(saw_errors()); | |
13263 | return Expression::make_error(loc); | |
13264 | } | |
91c0fd76 | 13265 | if (val->type()->interface_type() != NULL && !val->is_variable()) |
13266 | { | |
13267 | Temporary_statement* temp = | |
13268 | Statement::make_temporary(NULL, val, loc); | |
13269 | inserter->insert(temp); | |
13270 | val = Expression::make_temporary_reference(temp, loc); | |
13271 | } | |
13272 | val = Expression::convert_for_assignment(gogo, val_type, val, loc); | |
2c809f8f | 13273 | |
13274 | key_value_pair->push_back(key); | |
13275 | key_value_pair->push_back(val); | |
13276 | value_pairs->push_back( | |
13277 | Expression::make_struct_composite_literal(this->element_type_, | |
13278 | key_value_pair, loc)); | |
13279 | } | |
13280 | ||
e67508fa | 13281 | Expression* element_count = Expression::make_integer_ul(i, NULL, loc); |
6bf4793c | 13282 | Array_type* ctor_type = |
2c809f8f | 13283 | Type::make_array_type(this->element_type_, element_count); |
6bf4793c | 13284 | ctor_type->set_is_array_incomparable(); |
2c809f8f | 13285 | Expression* constructor = |
13286 | new Fixed_array_construction_expression(ctor_type, NULL, | |
13287 | value_pairs, loc); | |
13288 | ||
13289 | this->constructor_temp_ = | |
13290 | Statement::make_temporary(NULL, constructor, loc); | |
13291 | constructor->issue_nil_check(); | |
13292 | this->constructor_temp_->set_is_address_taken(); | |
13293 | inserter->insert(this->constructor_temp_); | |
13294 | } | |
13295 | ||
13296 | return this; | |
13297 | } | |
13298 | ||
e440a328 | 13299 | // Final type determination. |
13300 | ||
13301 | void | |
13302 | Map_construction_expression::do_determine_type(const Type_context*) | |
13303 | { | |
13304 | if (this->vals_ == NULL) | |
13305 | return; | |
13306 | ||
13307 | Map_type* mt = this->type_->map_type(); | |
13308 | Type_context key_context(mt->key_type(), false); | |
13309 | Type_context val_context(mt->val_type(), false); | |
13310 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13311 | pv != this->vals_->end(); | |
13312 | ++pv) | |
13313 | { | |
13314 | (*pv)->determine_type(&key_context); | |
13315 | ++pv; | |
13316 | (*pv)->determine_type(&val_context); | |
13317 | } | |
13318 | } | |
13319 | ||
13320 | // Check types. | |
13321 | ||
13322 | void | |
13323 | Map_construction_expression::do_check_types(Gogo*) | |
13324 | { | |
13325 | if (this->vals_ == NULL) | |
13326 | return; | |
13327 | ||
13328 | Map_type* mt = this->type_->map_type(); | |
13329 | int i = 0; | |
13330 | Type* key_type = mt->key_type(); | |
13331 | Type* val_type = mt->val_type(); | |
13332 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13333 | pv != this->vals_->end(); | |
13334 | ++pv, ++i) | |
13335 | { | |
13336 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
13337 | { | |
631d5788 | 13338 | go_error_at((*pv)->location(), |
13339 | "incompatible type for element %d key in map construction", | |
13340 | i + 1); | |
e440a328 | 13341 | this->set_is_error(); |
13342 | } | |
13343 | ++pv; | |
13344 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
13345 | { | |
631d5788 | 13346 | go_error_at((*pv)->location(), |
13347 | ("incompatible type for element %d value " | |
13348 | "in map construction"), | |
e440a328 | 13349 | i + 1); |
13350 | this->set_is_error(); | |
13351 | } | |
13352 | } | |
13353 | } | |
13354 | ||
ea664253 | 13355 | // Return the backend representation for constructing a map. |
e440a328 | 13356 | |
ea664253 | 13357 | Bexpression* |
13358 | Map_construction_expression::do_get_backend(Translate_context* context) | |
e440a328 | 13359 | { |
2c809f8f | 13360 | if (this->is_error_expression()) |
ea664253 | 13361 | return context->backend()->error_expression(); |
2c809f8f | 13362 | Location loc = this->location(); |
e440a328 | 13363 | |
e440a328 | 13364 | size_t i = 0; |
2c809f8f | 13365 | Expression* ventries; |
e440a328 | 13366 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 13367 | ventries = Expression::make_nil(loc); |
e440a328 | 13368 | else |
13369 | { | |
2c809f8f | 13370 | go_assert(this->constructor_temp_ != NULL); |
13371 | i = this->vals_->size() / 2; | |
e440a328 | 13372 | |
2c809f8f | 13373 | Expression* ctor_ref = |
13374 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
13375 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
13376 | } | |
e440a328 | 13377 | |
2c809f8f | 13378 | Map_type* mt = this->type_->map_type(); |
13379 | if (this->element_type_ == NULL) | |
13380 | this->element_type_ = | |
13381 | Type::make_builtin_struct_type(2, | |
13382 | "__key", mt->key_type(), | |
13383 | "__val", mt->val_type()); | |
0d5530d9 | 13384 | Expression* descriptor = Expression::make_type_descriptor(mt, loc); |
2c809f8f | 13385 | |
13386 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
e67508fa | 13387 | Expression* count = Expression::make_integer_ul(i, uintptr_t, loc); |
2c809f8f | 13388 | |
13389 | Expression* entry_size = | |
13390 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
13391 | ||
13392 | unsigned int field_index; | |
13393 | const Struct_field* valfield = | |
13394 | this->element_type_->find_local_field("__val", &field_index); | |
13395 | Expression* val_offset = | |
13396 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
2c809f8f | 13397 | |
13398 | Expression* map_ctor = | |
0d5530d9 | 13399 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 5, descriptor, count, |
13400 | entry_size, val_offset, ventries); | |
ea664253 | 13401 | return map_ctor->get_backend(context); |
2c809f8f | 13402 | } |
e440a328 | 13403 | |
2c809f8f | 13404 | // Export an array construction. |
e440a328 | 13405 | |
2c809f8f | 13406 | void |
13407 | Map_construction_expression::do_export(Export* exp) const | |
13408 | { | |
13409 | exp->write_c_string("convert("); | |
13410 | exp->write_type(this->type_); | |
13411 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13412 | pv != this->vals_->end(); | |
13413 | ++pv) | |
13414 | { | |
13415 | exp->write_c_string(", "); | |
13416 | (*pv)->export_expression(exp); | |
13417 | } | |
13418 | exp->write_c_string(")"); | |
13419 | } | |
e440a328 | 13420 | |
2c809f8f | 13421 | // Dump ast representation for a map construction expression. |
d751bb78 | 13422 | |
13423 | void | |
13424 | Map_construction_expression::do_dump_expression( | |
13425 | Ast_dump_context* ast_dump_context) const | |
13426 | { | |
d751bb78 | 13427 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 13428 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13429 | ast_dump_context->ostream() << "}"; |
13430 | } | |
13431 | ||
7795ac51 | 13432 | // Class Composite_literal_expression. |
e440a328 | 13433 | |
13434 | // Traversal. | |
13435 | ||
13436 | int | |
13437 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13438 | { | |
dbffccfc | 13439 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 13440 | return TRAVERSE_EXIT; |
dbffccfc | 13441 | |
13442 | // If this is a struct composite literal with keys, then the keys | |
13443 | // are field names, not expressions. We don't want to traverse them | |
13444 | // in that case. If we do, we can give an erroneous error "variable | |
13445 | // initializer refers to itself." See bug482.go in the testsuite. | |
13446 | if (this->has_keys_ && this->vals_ != NULL) | |
13447 | { | |
13448 | // The type may not be resolvable at this point. | |
13449 | Type* type = this->type_; | |
a01f2481 | 13450 | |
7795ac51 | 13451 | for (int depth = 0; depth < this->depth_; ++depth) |
a01f2481 | 13452 | { |
13453 | if (type->array_type() != NULL) | |
13454 | type = type->array_type()->element_type(); | |
13455 | else if (type->map_type() != NULL) | |
7795ac51 | 13456 | { |
13457 | if (this->key_path_[depth]) | |
13458 | type = type->map_type()->key_type(); | |
13459 | else | |
13460 | type = type->map_type()->val_type(); | |
13461 | } | |
a01f2481 | 13462 | else |
13463 | { | |
13464 | // This error will be reported during lowering. | |
13465 | return TRAVERSE_CONTINUE; | |
13466 | } | |
13467 | } | |
13468 | ||
dbffccfc | 13469 | while (true) |
13470 | { | |
13471 | if (type->classification() == Type::TYPE_NAMED) | |
13472 | type = type->named_type()->real_type(); | |
13473 | else if (type->classification() == Type::TYPE_FORWARD) | |
13474 | { | |
13475 | Type* t = type->forwarded(); | |
13476 | if (t == type) | |
13477 | break; | |
13478 | type = t; | |
13479 | } | |
13480 | else | |
13481 | break; | |
13482 | } | |
13483 | ||
13484 | if (type->classification() == Type::TYPE_STRUCT) | |
13485 | { | |
13486 | Expression_list::iterator p = this->vals_->begin(); | |
13487 | while (p != this->vals_->end()) | |
13488 | { | |
13489 | // Skip key. | |
13490 | ++p; | |
13491 | go_assert(p != this->vals_->end()); | |
13492 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13493 | return TRAVERSE_EXIT; | |
13494 | ++p; | |
13495 | } | |
13496 | return TRAVERSE_CONTINUE; | |
13497 | } | |
13498 | } | |
13499 | ||
13500 | if (this->vals_ != NULL) | |
13501 | return this->vals_->traverse(traverse); | |
13502 | ||
13503 | return TRAVERSE_CONTINUE; | |
e440a328 | 13504 | } |
13505 | ||
13506 | // Lower a generic composite literal into a specific version based on | |
13507 | // the type. | |
13508 | ||
13509 | Expression* | |
ceeb4318 | 13510 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13511 | Statement_inserter* inserter, int) | |
e440a328 | 13512 | { |
13513 | Type* type = this->type_; | |
13514 | ||
7795ac51 | 13515 | for (int depth = 0; depth < this->depth_; ++depth) |
e440a328 | 13516 | { |
13517 | if (type->array_type() != NULL) | |
13518 | type = type->array_type()->element_type(); | |
13519 | else if (type->map_type() != NULL) | |
7795ac51 | 13520 | { |
13521 | if (this->key_path_[depth]) | |
13522 | type = type->map_type()->key_type(); | |
13523 | else | |
13524 | type = type->map_type()->val_type(); | |
13525 | } | |
e440a328 | 13526 | else |
13527 | { | |
5c13bd80 | 13528 | if (!type->is_error()) |
631d5788 | 13529 | go_error_at(this->location(), |
13530 | ("may only omit types within composite literals " | |
13531 | "of slice, array, or map type")); | |
e440a328 | 13532 | return Expression::make_error(this->location()); |
13533 | } | |
13534 | } | |
13535 | ||
e00772b3 | 13536 | Type *pt = type->points_to(); |
13537 | bool is_pointer = false; | |
13538 | if (pt != NULL) | |
13539 | { | |
13540 | is_pointer = true; | |
13541 | type = pt; | |
13542 | } | |
13543 | ||
13544 | Expression* ret; | |
5c13bd80 | 13545 | if (type->is_error()) |
e440a328 | 13546 | return Expression::make_error(this->location()); |
13547 | else if (type->struct_type() != NULL) | |
e00772b3 | 13548 | ret = this->lower_struct(gogo, type); |
e440a328 | 13549 | else if (type->array_type() != NULL) |
113ef6a5 | 13550 | ret = this->lower_array(type); |
e440a328 | 13551 | else if (type->map_type() != NULL) |
e00772b3 | 13552 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13553 | else |
13554 | { | |
631d5788 | 13555 | go_error_at(this->location(), |
13556 | ("expected struct, slice, array, or map type " | |
13557 | "for composite literal")); | |
e440a328 | 13558 | return Expression::make_error(this->location()); |
13559 | } | |
e00772b3 | 13560 | |
13561 | if (is_pointer) | |
2c809f8f | 13562 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13563 | |
13564 | return ret; | |
e440a328 | 13565 | } |
13566 | ||
13567 | // Lower a struct composite literal. | |
13568 | ||
13569 | Expression* | |
81c4b26b | 13570 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13571 | { |
b13c66cd | 13572 | Location location = this->location(); |
e440a328 | 13573 | Struct_type* st = type->struct_type(); |
13574 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13575 | { |
e6013c28 | 13576 | if (this->vals_ != NULL |
13577 | && !this->vals_->empty() | |
13578 | && type->named_type() != NULL | |
13579 | && type->named_type()->named_object()->package() != NULL) | |
13580 | { | |
13581 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13582 | pf != st->fields()->end(); | |
13583 | ++pf) | |
07daa4e7 | 13584 | { |
07ba7f26 | 13585 | if (Gogo::is_hidden_name(pf->field_name()) |
13586 | || pf->is_embedded_builtin(gogo)) | |
631d5788 | 13587 | go_error_at(this->location(), |
13588 | "assignment of unexported field %qs in %qs literal", | |
13589 | Gogo::message_name(pf->field_name()).c_str(), | |
13590 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13591 | } |
13592 | } | |
13593 | ||
13594 | return new Struct_construction_expression(type, this->vals_, location); | |
13595 | } | |
e440a328 | 13596 | |
13597 | size_t field_count = st->field_count(); | |
13598 | std::vector<Expression*> vals(field_count); | |
e32de7ba | 13599 | std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>); |
e440a328 | 13600 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13601 | Expression* external_expr = NULL; |
13602 | const Named_object* external_no = NULL; | |
e440a328 | 13603 | while (p != this->vals_->end()) |
13604 | { | |
13605 | Expression* name_expr = *p; | |
13606 | ||
13607 | ++p; | |
c484d925 | 13608 | go_assert(p != this->vals_->end()); |
e440a328 | 13609 | Expression* val = *p; |
13610 | ||
13611 | ++p; | |
13612 | ||
13613 | if (name_expr == NULL) | |
13614 | { | |
631d5788 | 13615 | go_error_at(val->location(), |
13616 | "mixture of field and value initializers"); | |
e440a328 | 13617 | return Expression::make_error(location); |
13618 | } | |
13619 | ||
13620 | bool bad_key = false; | |
13621 | std::string name; | |
81c4b26b | 13622 | const Named_object* no = NULL; |
e440a328 | 13623 | switch (name_expr->classification()) |
13624 | { | |
13625 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13626 | name = name_expr->unknown_expression()->name(); | |
7f7ce694 | 13627 | if (type->named_type() != NULL) |
13628 | { | |
13629 | // If the named object found for this field name comes from a | |
13630 | // different package than the struct it is a part of, do not count | |
13631 | // this incorrect lookup as a usage of the object's package. | |
13632 | no = name_expr->unknown_expression()->named_object(); | |
13633 | if (no->package() != NULL | |
13634 | && no->package() != type->named_type()->named_object()->package()) | |
13635 | no->package()->forget_usage(name_expr); | |
13636 | } | |
e440a328 | 13637 | break; |
13638 | ||
13639 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13640 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13641 | break; |
13642 | ||
13643 | case EXPRESSION_TYPE: | |
13644 | { | |
13645 | Type* t = name_expr->type(); | |
13646 | Named_type* nt = t->named_type(); | |
13647 | if (nt == NULL) | |
13648 | bad_key = true; | |
13649 | else | |
81c4b26b | 13650 | no = nt->named_object(); |
e440a328 | 13651 | } |
13652 | break; | |
13653 | ||
13654 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13655 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13656 | break; |
13657 | ||
b0c09712 | 13658 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
13659 | no = name_expr->enclosed_var_expression()->variable(); | |
e440a328 | 13660 | break; |
13661 | ||
b0c09712 | 13662 | case EXPRESSION_FUNC_REFERENCE: |
13663 | no = name_expr->func_expression()->named_object(); | |
e440a328 | 13664 | break; |
13665 | ||
13666 | default: | |
13667 | bad_key = true; | |
13668 | break; | |
13669 | } | |
13670 | if (bad_key) | |
13671 | { | |
631d5788 | 13672 | go_error_at(name_expr->location(), "expected struct field name"); |
e440a328 | 13673 | return Expression::make_error(location); |
13674 | } | |
13675 | ||
81c4b26b | 13676 | if (no != NULL) |
13677 | { | |
62750cd5 | 13678 | if (no->package() != NULL && external_expr == NULL) |
13679 | { | |
13680 | external_expr = name_expr; | |
13681 | external_no = no; | |
13682 | } | |
13683 | ||
81c4b26b | 13684 | name = no->name(); |
13685 | ||
13686 | // A predefined name won't be packed. If it starts with a | |
13687 | // lower case letter we need to check for that case, because | |
2d29d278 | 13688 | // the field name will be packed. FIXME. |
81c4b26b | 13689 | if (!Gogo::is_hidden_name(name) |
13690 | && name[0] >= 'a' | |
13691 | && name[0] <= 'z') | |
13692 | { | |
13693 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13694 | if (gno == no) | |
13695 | name = gogo->pack_hidden_name(name, false); | |
13696 | } | |
13697 | } | |
13698 | ||
e440a328 | 13699 | unsigned int index; |
13700 | const Struct_field* sf = st->find_local_field(name, &index); | |
13701 | if (sf == NULL) | |
13702 | { | |
631d5788 | 13703 | go_error_at(name_expr->location(), "unknown field %qs in %qs", |
13704 | Gogo::message_name(name).c_str(), | |
13705 | (type->named_type() != NULL | |
13706 | ? type->named_type()->message_name().c_str() | |
13707 | : "unnamed struct")); | |
e440a328 | 13708 | return Expression::make_error(location); |
13709 | } | |
13710 | if (vals[index] != NULL) | |
13711 | { | |
631d5788 | 13712 | go_error_at(name_expr->location(), |
13713 | "duplicate value for field %qs in %qs", | |
13714 | Gogo::message_name(name).c_str(), | |
13715 | (type->named_type() != NULL | |
13716 | ? type->named_type()->message_name().c_str() | |
13717 | : "unnamed struct")); | |
e440a328 | 13718 | return Expression::make_error(location); |
13719 | } | |
13720 | ||
07daa4e7 | 13721 | if (type->named_type() != NULL |
13722 | && type->named_type()->named_object()->package() != NULL | |
07ba7f26 | 13723 | && (Gogo::is_hidden_name(sf->field_name()) |
13724 | || sf->is_embedded_builtin(gogo))) | |
631d5788 | 13725 | go_error_at(name_expr->location(), |
13726 | "assignment of unexported field %qs in %qs literal", | |
13727 | Gogo::message_name(sf->field_name()).c_str(), | |
13728 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13729 | |
e440a328 | 13730 | vals[index] = val; |
e32de7ba | 13731 | traverse_order->push_back(static_cast<unsigned long>(index)); |
e440a328 | 13732 | } |
13733 | ||
62750cd5 | 13734 | if (!this->all_are_names_) |
13735 | { | |
13736 | // This is a weird case like bug462 in the testsuite. | |
13737 | if (external_expr == NULL) | |
631d5788 | 13738 | go_error_at(this->location(), "unknown field in %qs literal", |
13739 | (type->named_type() != NULL | |
13740 | ? type->named_type()->message_name().c_str() | |
13741 | : "unnamed struct")); | |
62750cd5 | 13742 | else |
631d5788 | 13743 | go_error_at(external_expr->location(), "unknown field %qs in %qs", |
13744 | external_no->message_name().c_str(), | |
13745 | (type->named_type() != NULL | |
13746 | ? type->named_type()->message_name().c_str() | |
13747 | : "unnamed struct")); | |
62750cd5 | 13748 | return Expression::make_error(location); |
13749 | } | |
13750 | ||
e440a328 | 13751 | Expression_list* list = new Expression_list; |
13752 | list->reserve(field_count); | |
13753 | for (size_t i = 0; i < field_count; ++i) | |
13754 | list->push_back(vals[i]); | |
13755 | ||
0c4f5a19 | 13756 | Struct_construction_expression* ret = |
13757 | new Struct_construction_expression(type, list, location); | |
13758 | ret->set_traverse_order(traverse_order); | |
13759 | return ret; | |
e440a328 | 13760 | } |
13761 | ||
e32de7ba | 13762 | // Index/value/traversal-order triple. |
00773463 | 13763 | |
e32de7ba | 13764 | struct IVT_triple { |
13765 | unsigned long index; | |
13766 | unsigned long traversal_order; | |
13767 | Expression* expr; | |
13768 | IVT_triple(unsigned long i, unsigned long to, Expression *e) | |
13769 | : index(i), traversal_order(to), expr(e) { } | |
13770 | bool operator<(const IVT_triple& other) const | |
13771 | { return this->index < other.index; } | |
00773463 | 13772 | }; |
13773 | ||
e440a328 | 13774 | // Lower an array composite literal. |
13775 | ||
13776 | Expression* | |
113ef6a5 | 13777 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13778 | { |
b13c66cd | 13779 | Location location = this->location(); |
e440a328 | 13780 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13781 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13782 | |
ffe743ca | 13783 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13784 | indexes->reserve(this->vals_->size()); | |
00773463 | 13785 | bool indexes_out_of_order = false; |
ffe743ca | 13786 | Expression_list* vals = new Expression_list(); |
13787 | vals->reserve(this->vals_->size()); | |
e440a328 | 13788 | unsigned long index = 0; |
13789 | Expression_list::const_iterator p = this->vals_->begin(); | |
13790 | while (p != this->vals_->end()) | |
13791 | { | |
13792 | Expression* index_expr = *p; | |
13793 | ||
13794 | ++p; | |
c484d925 | 13795 | go_assert(p != this->vals_->end()); |
e440a328 | 13796 | Expression* val = *p; |
13797 | ||
13798 | ++p; | |
13799 | ||
ffe743ca | 13800 | if (index_expr == NULL) |
13801 | { | |
13802 | if (!indexes->empty()) | |
13803 | indexes->push_back(index); | |
13804 | } | |
13805 | else | |
e440a328 | 13806 | { |
ffe743ca | 13807 | if (indexes->empty() && !vals->empty()) |
13808 | { | |
13809 | for (size_t i = 0; i < vals->size(); ++i) | |
13810 | indexes->push_back(i); | |
13811 | } | |
13812 | ||
0c77715b | 13813 | Numeric_constant nc; |
13814 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13815 | { |
631d5788 | 13816 | go_error_at(index_expr->location(), |
13817 | "index expression is not integer constant"); | |
e440a328 | 13818 | return Expression::make_error(location); |
13819 | } | |
6f6d9955 | 13820 | |
0c77715b | 13821 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13822 | { |
0c77715b | 13823 | case Numeric_constant::NC_UL_VALID: |
13824 | break; | |
13825 | case Numeric_constant::NC_UL_NOTINT: | |
631d5788 | 13826 | go_error_at(index_expr->location(), |
13827 | "index expression is not integer constant"); | |
0c77715b | 13828 | return Expression::make_error(location); |
13829 | case Numeric_constant::NC_UL_NEGATIVE: | |
631d5788 | 13830 | go_error_at(index_expr->location(), |
13831 | "index expression is negative"); | |
e440a328 | 13832 | return Expression::make_error(location); |
0c77715b | 13833 | case Numeric_constant::NC_UL_BIG: |
631d5788 | 13834 | go_error_at(index_expr->location(), "index value overflow"); |
e440a328 | 13835 | return Expression::make_error(location); |
0c77715b | 13836 | default: |
13837 | go_unreachable(); | |
e440a328 | 13838 | } |
6f6d9955 | 13839 | |
13840 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13841 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13842 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13843 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13844 | { |
631d5788 | 13845 | go_error_at(index_expr->location(), "index value overflow"); |
6f6d9955 | 13846 | return Expression::make_error(location); |
13847 | } | |
13848 | ||
ffe743ca | 13849 | if (std::find(indexes->begin(), indexes->end(), index) |
13850 | != indexes->end()) | |
e440a328 | 13851 | { |
631d5788 | 13852 | go_error_at(index_expr->location(), |
13853 | "duplicate value for index %lu", | |
13854 | index); | |
e440a328 | 13855 | return Expression::make_error(location); |
13856 | } | |
ffe743ca | 13857 | |
00773463 | 13858 | if (!indexes->empty() && index < indexes->back()) |
13859 | indexes_out_of_order = true; | |
13860 | ||
ffe743ca | 13861 | indexes->push_back(index); |
e440a328 | 13862 | } |
13863 | ||
ffe743ca | 13864 | vals->push_back(val); |
13865 | ||
e440a328 | 13866 | ++index; |
13867 | } | |
13868 | ||
ffe743ca | 13869 | if (indexes->empty()) |
13870 | { | |
13871 | delete indexes; | |
13872 | indexes = NULL; | |
13873 | } | |
e440a328 | 13874 | |
e32de7ba | 13875 | std::vector<unsigned long>* traverse_order = NULL; |
00773463 | 13876 | if (indexes_out_of_order) |
13877 | { | |
e32de7ba | 13878 | typedef std::vector<IVT_triple> V; |
00773463 | 13879 | |
13880 | V v; | |
13881 | v.reserve(indexes->size()); | |
13882 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
e32de7ba | 13883 | unsigned long torder = 0; |
00773463 | 13884 | for (Expression_list::const_iterator pe = vals->begin(); |
13885 | pe != vals->end(); | |
e32de7ba | 13886 | ++pe, ++pi, ++torder) |
13887 | v.push_back(IVT_triple(*pi, torder, *pe)); | |
00773463 | 13888 | |
e32de7ba | 13889 | std::sort(v.begin(), v.end()); |
00773463 | 13890 | |
13891 | delete indexes; | |
13892 | delete vals; | |
e32de7ba | 13893 | |
00773463 | 13894 | indexes = new std::vector<unsigned long>(); |
13895 | indexes->reserve(v.size()); | |
13896 | vals = new Expression_list(); | |
13897 | vals->reserve(v.size()); | |
e32de7ba | 13898 | traverse_order = new std::vector<unsigned long>(); |
13899 | traverse_order->reserve(v.size()); | |
00773463 | 13900 | |
13901 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13902 | { | |
e32de7ba | 13903 | indexes->push_back(p->index); |
13904 | vals->push_back(p->expr); | |
13905 | traverse_order->push_back(p->traversal_order); | |
00773463 | 13906 | } |
13907 | } | |
13908 | ||
e32de7ba | 13909 | Expression* ret = this->make_array(type, indexes, vals); |
13910 | Array_construction_expression* ace = ret->array_literal(); | |
13911 | if (ace != NULL && traverse_order != NULL) | |
13912 | ace->set_traverse_order(traverse_order); | |
13913 | return ret; | |
e440a328 | 13914 | } |
13915 | ||
13916 | // Actually build the array composite literal. This handles | |
13917 | // [...]{...}. | |
13918 | ||
13919 | Expression* | |
ffe743ca | 13920 | Composite_literal_expression::make_array( |
13921 | Type* type, | |
13922 | const std::vector<unsigned long>* indexes, | |
13923 | Expression_list* vals) | |
e440a328 | 13924 | { |
b13c66cd | 13925 | Location location = this->location(); |
e440a328 | 13926 | Array_type* at = type->array_type(); |
ffe743ca | 13927 | |
e440a328 | 13928 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13929 | { | |
ffe743ca | 13930 | size_t size; |
13931 | if (vals == NULL) | |
13932 | size = 0; | |
00773463 | 13933 | else if (indexes != NULL) |
13934 | size = indexes->back() + 1; | |
13935 | else | |
ffe743ca | 13936 | { |
13937 | size = vals->size(); | |
13938 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13939 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13940 | && size >> (it->bits() - 1) != 0) | |
13941 | { | |
631d5788 | 13942 | go_error_at(location, "too many elements in composite literal"); |
ffe743ca | 13943 | return Expression::make_error(location); |
13944 | } | |
13945 | } | |
ffe743ca | 13946 | |
e67508fa | 13947 | Expression* elen = Expression::make_integer_ul(size, NULL, location); |
e440a328 | 13948 | at = Type::make_array_type(at->element_type(), elen); |
13949 | type = at; | |
13950 | } | |
ffe743ca | 13951 | else if (at->length() != NULL |
13952 | && !at->length()->is_error_expression() | |
13953 | && this->vals_ != NULL) | |
13954 | { | |
13955 | Numeric_constant nc; | |
13956 | unsigned long val; | |
13957 | if (at->length()->numeric_constant_value(&nc) | |
13958 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13959 | { | |
13960 | if (indexes == NULL) | |
13961 | { | |
13962 | if (this->vals_->size() > val) | |
13963 | { | |
631d5788 | 13964 | go_error_at(location, |
13965 | "too many elements in composite literal"); | |
ffe743ca | 13966 | return Expression::make_error(location); |
13967 | } | |
13968 | } | |
13969 | else | |
13970 | { | |
00773463 | 13971 | unsigned long max = indexes->back(); |
ffe743ca | 13972 | if (max >= val) |
13973 | { | |
631d5788 | 13974 | go_error_at(location, |
13975 | ("some element keys in composite literal " | |
13976 | "are out of range")); | |
ffe743ca | 13977 | return Expression::make_error(location); |
13978 | } | |
13979 | } | |
13980 | } | |
13981 | } | |
13982 | ||
e440a328 | 13983 | if (at->length() != NULL) |
ffe743ca | 13984 | return new Fixed_array_construction_expression(type, indexes, vals, |
13985 | location); | |
e440a328 | 13986 | else |
2c809f8f | 13987 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13988 | } |
13989 | ||
13990 | // Lower a map composite literal. | |
13991 | ||
13992 | Expression* | |
a287720d | 13993 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13994 | Statement_inserter* inserter, |
a287720d | 13995 | Type* type) |
e440a328 | 13996 | { |
b13c66cd | 13997 | Location location = this->location(); |
e440a328 | 13998 | if (this->vals_ != NULL) |
13999 | { | |
14000 | if (!this->has_keys_) | |
14001 | { | |
631d5788 | 14002 | go_error_at(location, "map composite literal must have keys"); |
e440a328 | 14003 | return Expression::make_error(location); |
14004 | } | |
14005 | ||
a287720d | 14006 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 14007 | p != this->vals_->end(); |
14008 | p += 2) | |
14009 | { | |
14010 | if (*p == NULL) | |
14011 | { | |
14012 | ++p; | |
631d5788 | 14013 | go_error_at((*p)->location(), |
14014 | ("map composite literal must " | |
14015 | "have keys for every value")); | |
e440a328 | 14016 | return Expression::make_error(location); |
14017 | } | |
a287720d | 14018 | // Make sure we have lowered the key; it may not have been |
14019 | // lowered in order to handle keys for struct composite | |
14020 | // literals. Lower it now to get the right error message. | |
14021 | if ((*p)->unknown_expression() != NULL) | |
14022 | { | |
14023 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 14024 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 14025 | go_assert((*p)->is_error_expression()); |
a287720d | 14026 | return Expression::make_error(location); |
14027 | } | |
e440a328 | 14028 | } |
14029 | } | |
14030 | ||
14031 | return new Map_construction_expression(type, this->vals_, location); | |
14032 | } | |
14033 | ||
d751bb78 | 14034 | // Dump ast representation for a composite literal expression. |
14035 | ||
14036 | void | |
14037 | Composite_literal_expression::do_dump_expression( | |
14038 | Ast_dump_context* ast_dump_context) const | |
14039 | { | |
8b1c301d | 14040 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 14041 | ast_dump_context->dump_type(this->type_); |
14042 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 14043 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 14044 | ast_dump_context->ostream() << "})"; |
14045 | } | |
14046 | ||
e440a328 | 14047 | // Make a composite literal expression. |
14048 | ||
14049 | Expression* | |
14050 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 14051 | Expression_list* vals, bool all_are_names, |
b13c66cd | 14052 | Location location) |
e440a328 | 14053 | { |
14054 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 14055 | all_are_names, location); |
e440a328 | 14056 | } |
14057 | ||
14058 | // Return whether this expression is a composite literal. | |
14059 | ||
14060 | bool | |
14061 | Expression::is_composite_literal() const | |
14062 | { | |
14063 | switch (this->classification_) | |
14064 | { | |
14065 | case EXPRESSION_COMPOSITE_LITERAL: | |
14066 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14067 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 14068 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14069 | case EXPRESSION_MAP_CONSTRUCTION: |
14070 | return true; | |
14071 | default: | |
14072 | return false; | |
14073 | } | |
14074 | } | |
14075 | ||
14076 | // Return whether this expression is a composite literal which is not | |
14077 | // constant. | |
14078 | ||
14079 | bool | |
14080 | Expression::is_nonconstant_composite_literal() const | |
14081 | { | |
14082 | switch (this->classification_) | |
14083 | { | |
14084 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
14085 | { | |
14086 | const Struct_construction_expression *psce = | |
14087 | static_cast<const Struct_construction_expression*>(this); | |
14088 | return !psce->is_constant_struct(); | |
14089 | } | |
14090 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
14091 | { | |
14092 | const Fixed_array_construction_expression *pace = | |
14093 | static_cast<const Fixed_array_construction_expression*>(this); | |
14094 | return !pace->is_constant_array(); | |
14095 | } | |
2c809f8f | 14096 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 14097 | { |
2c809f8f | 14098 | const Slice_construction_expression *pace = |
14099 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 14100 | return !pace->is_constant_array(); |
14101 | } | |
14102 | case EXPRESSION_MAP_CONSTRUCTION: | |
14103 | return true; | |
14104 | default: | |
14105 | return false; | |
14106 | } | |
14107 | } | |
14108 | ||
35a54f17 | 14109 | // Return true if this is a variable or temporary_variable. |
14110 | ||
14111 | bool | |
14112 | Expression::is_variable() const | |
14113 | { | |
14114 | switch (this->classification_) | |
14115 | { | |
14116 | case EXPRESSION_VAR_REFERENCE: | |
14117 | case EXPRESSION_TEMPORARY_REFERENCE: | |
14118 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
b0c09712 | 14119 | case EXPRESSION_ENCLOSED_VAR_REFERENCE: |
35a54f17 | 14120 | return true; |
14121 | default: | |
14122 | return false; | |
14123 | } | |
14124 | } | |
14125 | ||
e440a328 | 14126 | // Return true if this is a reference to a local variable. |
14127 | ||
14128 | bool | |
14129 | Expression::is_local_variable() const | |
14130 | { | |
14131 | const Var_expression* ve = this->var_expression(); | |
14132 | if (ve == NULL) | |
14133 | return false; | |
14134 | const Named_object* no = ve->named_object(); | |
14135 | return (no->is_result_variable() | |
14136 | || (no->is_variable() && !no->var_value()->is_global())); | |
14137 | } | |
14138 | ||
14139 | // Class Type_guard_expression. | |
14140 | ||
14141 | // Traversal. | |
14142 | ||
14143 | int | |
14144 | Type_guard_expression::do_traverse(Traverse* traverse) | |
14145 | { | |
14146 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
14147 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14148 | return TRAVERSE_EXIT; | |
14149 | return TRAVERSE_CONTINUE; | |
14150 | } | |
14151 | ||
2c809f8f | 14152 | Expression* |
14153 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
14154 | Statement_inserter* inserter) | |
14155 | { | |
5bf8be8b | 14156 | if (this->expr_->is_error_expression() |
14157 | || this->expr_->type()->is_error_type()) | |
14158 | { | |
14159 | go_assert(saw_errors()); | |
14160 | return Expression::make_error(this->location()); | |
14161 | } | |
14162 | ||
2c809f8f | 14163 | if (!this->expr_->is_variable()) |
14164 | { | |
14165 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
14166 | this->location()); | |
14167 | inserter->insert(temp); | |
14168 | this->expr_ = | |
14169 | Expression::make_temporary_reference(temp, this->location()); | |
14170 | } | |
14171 | return this; | |
14172 | } | |
14173 | ||
e440a328 | 14174 | // Check types of a type guard expression. The expression must have |
14175 | // an interface type, but the actual type conversion is checked at run | |
14176 | // time. | |
14177 | ||
14178 | void | |
14179 | Type_guard_expression::do_check_types(Gogo*) | |
14180 | { | |
e440a328 | 14181 | Type* expr_type = this->expr_->type(); |
7e9da23f | 14182 | if (expr_type->interface_type() == NULL) |
f725ade8 | 14183 | { |
5c13bd80 | 14184 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 14185 | this->report_error(_("type assertion only valid for interface types")); |
14186 | this->set_is_error(); | |
14187 | } | |
e440a328 | 14188 | else if (this->type_->interface_type() == NULL) |
14189 | { | |
14190 | std::string reason; | |
14191 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
14192 | &reason)) | |
14193 | { | |
5c13bd80 | 14194 | if (!this->type_->is_error()) |
e440a328 | 14195 | { |
f725ade8 | 14196 | if (reason.empty()) |
14197 | this->report_error(_("impossible type assertion: " | |
14198 | "type does not implement interface")); | |
14199 | else | |
631d5788 | 14200 | go_error_at(this->location(), |
14201 | ("impossible type assertion: " | |
14202 | "type does not implement interface (%s)"), | |
14203 | reason.c_str()); | |
e440a328 | 14204 | } |
f725ade8 | 14205 | this->set_is_error(); |
e440a328 | 14206 | } |
14207 | } | |
14208 | } | |
14209 | ||
ea664253 | 14210 | // Return the backend representation for a type guard expression. |
e440a328 | 14211 | |
ea664253 | 14212 | Bexpression* |
14213 | Type_guard_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14214 | { |
2c809f8f | 14215 | Expression* conversion; |
7e9da23f | 14216 | if (this->type_->interface_type() != NULL) |
2c809f8f | 14217 | conversion = |
14218 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
14219 | true, this->location()); | |
e440a328 | 14220 | else |
2c809f8f | 14221 | conversion = |
14222 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
14223 | this->expr_, this->location()); | |
14224 | ||
21b70e8f | 14225 | Gogo* gogo = context->gogo(); |
14226 | Btype* bt = this->type_->get_backend(gogo); | |
14227 | Bexpression* bexpr = conversion->get_backend(context); | |
14228 | return gogo->backend()->convert_expression(bt, bexpr, this->location()); | |
e440a328 | 14229 | } |
14230 | ||
d751bb78 | 14231 | // Dump ast representation for a type guard expression. |
14232 | ||
14233 | void | |
2c809f8f | 14234 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 14235 | const |
14236 | { | |
14237 | this->expr_->dump_expression(ast_dump_context); | |
14238 | ast_dump_context->ostream() << "."; | |
14239 | ast_dump_context->dump_type(this->type_); | |
14240 | } | |
14241 | ||
e440a328 | 14242 | // Make a type guard expression. |
14243 | ||
14244 | Expression* | |
14245 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 14246 | Location location) |
e440a328 | 14247 | { |
14248 | return new Type_guard_expression(expr, type, location); | |
14249 | } | |
14250 | ||
2c809f8f | 14251 | // Class Heap_expression. |
e440a328 | 14252 | |
da244e59 | 14253 | // Return the type of the expression stored on the heap. |
e440a328 | 14254 | |
da244e59 | 14255 | Type* |
14256 | Heap_expression::do_type() | |
14257 | { return Type::make_pointer_type(this->expr_->type()); } | |
e440a328 | 14258 | |
ea664253 | 14259 | // Return the backend representation for allocating an expression on the heap. |
e440a328 | 14260 | |
ea664253 | 14261 | Bexpression* |
14262 | Heap_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14263 | { |
03118c21 | 14264 | Type* etype = this->expr_->type(); |
14265 | if (this->expr_->is_error_expression() || etype->is_error()) | |
ea664253 | 14266 | return context->backend()->error_expression(); |
2c809f8f | 14267 | |
02c19a1a | 14268 | Location loc = this->location(); |
2c809f8f | 14269 | Gogo* gogo = context->gogo(); |
02c19a1a | 14270 | Btype* btype = this->type()->get_backend(gogo); |
45ff893b | 14271 | |
03118c21 | 14272 | Expression* alloc = Expression::make_allocation(etype, loc); |
5973ede0 | 14273 | if (this->allocate_on_stack_) |
45ff893b | 14274 | alloc->allocation_expression()->set_allocate_on_stack(); |
14275 | Bexpression* space = alloc->get_backend(context); | |
02c19a1a | 14276 | |
14277 | Bstatement* decl; | |
14278 | Named_object* fn = context->function(); | |
14279 | go_assert(fn != NULL); | |
14280 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
14281 | Bvariable* space_temp = | |
14282 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
14283 | space, true, loc, &decl); | |
03118c21 | 14284 | Btype* expr_btype = etype->get_backend(gogo); |
02c19a1a | 14285 | |
ea664253 | 14286 | Bexpression* bexpr = this->expr_->get_backend(context); |
03118c21 | 14287 | |
14288 | // If this assignment needs a write barrier, call typedmemmove. We | |
14289 | // don't do this in the write barrier pass because in some cases | |
14290 | // backend conversion can introduce new Heap_expression values. | |
14291 | Bstatement* assn; | |
06e83d10 | 14292 | if (!etype->has_pointer() || this->allocate_on_stack_) |
03118c21 | 14293 | { |
7af8e400 | 14294 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14295 | Bexpression* ref = |
14296 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
14297 | assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc); | |
14298 | } | |
14299 | else | |
14300 | { | |
14301 | Bstatement* edecl; | |
14302 | Bvariable* btemp = | |
14303 | gogo->backend()->temporary_variable(fndecl, context->bblock(), | |
14304 | expr_btype, bexpr, true, loc, | |
14305 | &edecl); | |
14306 | Bexpression* btempref = gogo->backend()->var_expression(btemp, | |
7af8e400 | 14307 | loc); |
03118c21 | 14308 | Bexpression* addr = gogo->backend()->address_expression(btempref, loc); |
14309 | ||
14310 | Expression* td = Expression::make_type_descriptor(etype, loc); | |
14311 | Type* etype_ptr = Type::make_pointer_type(etype); | |
7af8e400 | 14312 | space = gogo->backend()->var_expression(space_temp, loc); |
03118c21 | 14313 | Expression* elhs = Expression::make_backend(space, etype_ptr, loc); |
14314 | Expression* erhs = Expression::make_backend(addr, etype_ptr, loc); | |
14315 | Expression* call = Runtime::make_call(Runtime::TYPEDMEMMOVE, loc, 3, | |
14316 | td, elhs, erhs); | |
14317 | Bexpression* bcall = call->get_backend(context); | |
14318 | Bstatement* s = gogo->backend()->expression_statement(fndecl, bcall); | |
14319 | assn = gogo->backend()->compound_statement(edecl, s); | |
14320 | } | |
02c19a1a | 14321 | decl = gogo->backend()->compound_statement(decl, assn); |
7af8e400 | 14322 | space = gogo->backend()->var_expression(space_temp, loc); |
ea664253 | 14323 | return gogo->backend()->compound_expression(decl, space, loc); |
e440a328 | 14324 | } |
14325 | ||
2c809f8f | 14326 | // Dump ast representation for a heap expression. |
d751bb78 | 14327 | |
14328 | void | |
2c809f8f | 14329 | Heap_expression::do_dump_expression( |
d751bb78 | 14330 | Ast_dump_context* ast_dump_context) const |
14331 | { | |
14332 | ast_dump_context->ostream() << "&("; | |
14333 | ast_dump_context->dump_expression(this->expr_); | |
14334 | ast_dump_context->ostream() << ")"; | |
14335 | } | |
14336 | ||
2c809f8f | 14337 | // Allocate an expression on the heap. |
e440a328 | 14338 | |
14339 | Expression* | |
2c809f8f | 14340 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 14341 | { |
2c809f8f | 14342 | return new Heap_expression(expr, location); |
e440a328 | 14343 | } |
14344 | ||
14345 | // Class Receive_expression. | |
14346 | ||
14347 | // Return the type of a receive expression. | |
14348 | ||
14349 | Type* | |
14350 | Receive_expression::do_type() | |
14351 | { | |
e429e3bd | 14352 | if (this->is_error_expression()) |
14353 | return Type::make_error_type(); | |
e440a328 | 14354 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
14355 | if (channel_type == NULL) | |
e429e3bd | 14356 | { |
14357 | this->report_error(_("expected channel")); | |
14358 | return Type::make_error_type(); | |
14359 | } | |
e440a328 | 14360 | return channel_type->element_type(); |
14361 | } | |
14362 | ||
14363 | // Check types for a receive expression. | |
14364 | ||
14365 | void | |
14366 | Receive_expression::do_check_types(Gogo*) | |
14367 | { | |
14368 | Type* type = this->channel_->type(); | |
5c13bd80 | 14369 | if (type->is_error()) |
e440a328 | 14370 | { |
e429e3bd | 14371 | go_assert(saw_errors()); |
e440a328 | 14372 | this->set_is_error(); |
14373 | return; | |
14374 | } | |
14375 | if (type->channel_type() == NULL) | |
14376 | { | |
14377 | this->report_error(_("expected channel")); | |
14378 | return; | |
14379 | } | |
14380 | if (!type->channel_type()->may_receive()) | |
14381 | { | |
14382 | this->report_error(_("invalid receive on send-only channel")); | |
14383 | return; | |
14384 | } | |
14385 | } | |
14386 | ||
2c809f8f | 14387 | // Flattening for receive expressions creates a temporary variable to store |
14388 | // received data in for receives. | |
14389 | ||
14390 | Expression* | |
14391 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
14392 | Statement_inserter* inserter) | |
14393 | { | |
14394 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
14395 | if (channel_type == NULL) | |
14396 | { | |
14397 | go_assert(saw_errors()); | |
14398 | return this; | |
14399 | } | |
5bf8be8b | 14400 | else if (this->channel_->is_error_expression()) |
14401 | { | |
14402 | go_assert(saw_errors()); | |
14403 | return Expression::make_error(this->location()); | |
14404 | } | |
2c809f8f | 14405 | |
14406 | Type* element_type = channel_type->element_type(); | |
14407 | if (this->temp_receiver_ == NULL) | |
14408 | { | |
14409 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
14410 | this->location()); | |
14411 | this->temp_receiver_->set_is_address_taken(); | |
14412 | inserter->insert(this->temp_receiver_); | |
14413 | } | |
14414 | ||
14415 | return this; | |
14416 | } | |
14417 | ||
ea664253 | 14418 | // Get the backend representation for a receive expression. |
e440a328 | 14419 | |
ea664253 | 14420 | Bexpression* |
14421 | Receive_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14422 | { |
f24f10bb | 14423 | Location loc = this->location(); |
14424 | ||
e440a328 | 14425 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 14426 | if (channel_type == NULL) |
14427 | { | |
c484d925 | 14428 | go_assert(this->channel_->type()->is_error()); |
ea664253 | 14429 | return context->backend()->error_expression(); |
5b8368f4 | 14430 | } |
e440a328 | 14431 | |
2c809f8f | 14432 | Expression* recv_ref = |
14433 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14434 | Expression* recv_addr = | |
14435 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
14436 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
e36a5ff5 | 14437 | Expression* recv = Runtime::make_call(Runtime::CHANRECV1, loc, 2, |
14438 | this->channel_, recv_addr); | |
ea664253 | 14439 | return Expression::make_compound(recv, recv_ref, loc)->get_backend(context); |
e440a328 | 14440 | } |
14441 | ||
d751bb78 | 14442 | // Dump ast representation for a receive expression. |
14443 | ||
14444 | void | |
14445 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14446 | { | |
14447 | ast_dump_context->ostream() << " <- " ; | |
14448 | ast_dump_context->dump_expression(channel_); | |
14449 | } | |
14450 | ||
e440a328 | 14451 | // Make a receive expression. |
14452 | ||
14453 | Receive_expression* | |
b13c66cd | 14454 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 14455 | { |
14456 | return new Receive_expression(channel, location); | |
14457 | } | |
14458 | ||
e440a328 | 14459 | // An expression which evaluates to a pointer to the type descriptor |
14460 | // of a type. | |
14461 | ||
14462 | class Type_descriptor_expression : public Expression | |
14463 | { | |
14464 | public: | |
b13c66cd | 14465 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 14466 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
14467 | type_(type) | |
14468 | { } | |
14469 | ||
14470 | protected: | |
4b686186 | 14471 | int |
14472 | do_traverse(Traverse*); | |
14473 | ||
e440a328 | 14474 | Type* |
14475 | do_type() | |
14476 | { return Type::make_type_descriptor_ptr_type(); } | |
14477 | ||
f9ca30f9 | 14478 | bool |
3ae06f68 | 14479 | do_is_static_initializer() const |
f9ca30f9 | 14480 | { return true; } |
14481 | ||
e440a328 | 14482 | void |
14483 | do_determine_type(const Type_context*) | |
14484 | { } | |
14485 | ||
14486 | Expression* | |
14487 | do_copy() | |
14488 | { return this; } | |
14489 | ||
ea664253 | 14490 | Bexpression* |
14491 | do_get_backend(Translate_context* context) | |
a1d23b41 | 14492 | { |
ea664253 | 14493 | return this->type_->type_descriptor_pointer(context->gogo(), |
14494 | this->location()); | |
a1d23b41 | 14495 | } |
e440a328 | 14496 | |
d751bb78 | 14497 | void |
14498 | do_dump_expression(Ast_dump_context*) const; | |
14499 | ||
e440a328 | 14500 | private: |
14501 | // The type for which this is the descriptor. | |
14502 | Type* type_; | |
14503 | }; | |
14504 | ||
4b686186 | 14505 | int |
14506 | Type_descriptor_expression::do_traverse(Traverse* traverse) | |
14507 | { | |
14508 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14509 | return TRAVERSE_EXIT; | |
14510 | return TRAVERSE_CONTINUE; | |
14511 | } | |
14512 | ||
d751bb78 | 14513 | // Dump ast representation for a type descriptor expression. |
14514 | ||
14515 | void | |
14516 | Type_descriptor_expression::do_dump_expression( | |
14517 | Ast_dump_context* ast_dump_context) const | |
14518 | { | |
14519 | ast_dump_context->dump_type(this->type_); | |
14520 | } | |
14521 | ||
e440a328 | 14522 | // Make a type descriptor expression. |
14523 | ||
14524 | Expression* | |
b13c66cd | 14525 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14526 | { |
14527 | return new Type_descriptor_expression(type, location); | |
14528 | } | |
14529 | ||
aa5ae575 | 14530 | // An expression which evaluates to a pointer to the Garbage Collection symbol |
14531 | // of a type. | |
14532 | ||
14533 | class GC_symbol_expression : public Expression | |
14534 | { | |
14535 | public: | |
14536 | GC_symbol_expression(Type* type) | |
14537 | : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()), | |
14538 | type_(type) | |
14539 | {} | |
14540 | ||
14541 | protected: | |
14542 | Type* | |
14543 | do_type() | |
03118c21 | 14544 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } |
aa5ae575 | 14545 | |
14546 | bool | |
3ae06f68 | 14547 | do_is_static_initializer() const |
aa5ae575 | 14548 | { return true; } |
14549 | ||
14550 | void | |
14551 | do_determine_type(const Type_context*) | |
14552 | { } | |
14553 | ||
14554 | Expression* | |
14555 | do_copy() | |
14556 | { return this; } | |
14557 | ||
14558 | Bexpression* | |
14559 | do_get_backend(Translate_context* context) | |
14560 | { return this->type_->gc_symbol_pointer(context->gogo()); } | |
14561 | ||
14562 | void | |
14563 | do_dump_expression(Ast_dump_context*) const; | |
14564 | ||
14565 | private: | |
14566 | // The type which this gc symbol describes. | |
14567 | Type* type_; | |
14568 | }; | |
14569 | ||
14570 | // Dump ast representation for a gc symbol expression. | |
14571 | ||
14572 | void | |
14573 | GC_symbol_expression::do_dump_expression( | |
14574 | Ast_dump_context* ast_dump_context) const | |
14575 | { | |
14576 | ast_dump_context->ostream() << "gcdata("; | |
14577 | ast_dump_context->dump_type(this->type_); | |
14578 | ast_dump_context->ostream() << ")"; | |
14579 | } | |
14580 | ||
14581 | // Make a gc symbol expression. | |
14582 | ||
14583 | Expression* | |
14584 | Expression::make_gc_symbol(Type* type) | |
14585 | { | |
14586 | return new GC_symbol_expression(type); | |
14587 | } | |
14588 | ||
03118c21 | 14589 | // An expression that evaluates to a pointer to a symbol holding the |
14590 | // ptrmask data of a type. | |
14591 | ||
14592 | class Ptrmask_symbol_expression : public Expression | |
14593 | { | |
14594 | public: | |
14595 | Ptrmask_symbol_expression(Type* type) | |
14596 | : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()), | |
14597 | type_(type) | |
14598 | {} | |
14599 | ||
14600 | protected: | |
14601 | Type* | |
14602 | do_type() | |
14603 | { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); } | |
14604 | ||
14605 | bool | |
14606 | do_is_static_initializer() const | |
14607 | { return true; } | |
14608 | ||
14609 | void | |
14610 | do_determine_type(const Type_context*) | |
14611 | { } | |
14612 | ||
14613 | Expression* | |
14614 | do_copy() | |
14615 | { return this; } | |
14616 | ||
14617 | Bexpression* | |
14618 | do_get_backend(Translate_context*); | |
14619 | ||
14620 | void | |
14621 | do_dump_expression(Ast_dump_context*) const; | |
14622 | ||
14623 | private: | |
14624 | // The type that this ptrmask symbol describes. | |
14625 | Type* type_; | |
14626 | }; | |
14627 | ||
14628 | // Return the ptrmask variable. | |
14629 | ||
14630 | Bexpression* | |
14631 | Ptrmask_symbol_expression::do_get_backend(Translate_context* context) | |
14632 | { | |
14633 | Gogo* gogo = context->gogo(); | |
14634 | ||
14635 | // If this type does not need a gcprog, then we can use the standard | |
14636 | // GC symbol. | |
14637 | int64_t ptrsize, ptrdata; | |
14638 | if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata)) | |
14639 | return this->type_->gc_symbol_pointer(gogo); | |
14640 | ||
14641 | // Otherwise we have to build a ptrmask variable, and return a | |
14642 | // pointer to it. | |
14643 | ||
14644 | Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata); | |
14645 | Location bloc = Linemap::predeclared_location(); | |
7af8e400 | 14646 | Bexpression* bref = gogo->backend()->var_expression(bvar, bloc); |
03118c21 | 14647 | Bexpression* baddr = gogo->backend()->address_expression(bref, bloc); |
14648 | ||
14649 | Type* uint8_type = Type::lookup_integer_type("uint8"); | |
14650 | Type* pointer_uint8_type = Type::make_pointer_type(uint8_type); | |
14651 | Btype* ubtype = pointer_uint8_type->get_backend(gogo); | |
14652 | return gogo->backend()->convert_expression(ubtype, baddr, bloc); | |
14653 | } | |
14654 | ||
14655 | // Dump AST for a ptrmask symbol expression. | |
14656 | ||
14657 | void | |
14658 | Ptrmask_symbol_expression::do_dump_expression( | |
14659 | Ast_dump_context* ast_dump_context) const | |
14660 | { | |
14661 | ast_dump_context->ostream() << "ptrmask("; | |
14662 | ast_dump_context->dump_type(this->type_); | |
14663 | ast_dump_context->ostream() << ")"; | |
14664 | } | |
14665 | ||
14666 | // Make a ptrmask symbol expression. | |
14667 | ||
14668 | Expression* | |
14669 | Expression::make_ptrmask_symbol(Type* type) | |
14670 | { | |
14671 | return new Ptrmask_symbol_expression(type); | |
14672 | } | |
14673 | ||
e440a328 | 14674 | // An expression which evaluates to some characteristic of a type. |
14675 | // This is only used to initialize fields of a type descriptor. Using | |
14676 | // a new expression class is slightly inefficient but gives us a good | |
14677 | // separation between the frontend and the middle-end with regard to | |
14678 | // how types are laid out. | |
14679 | ||
14680 | class Type_info_expression : public Expression | |
14681 | { | |
14682 | public: | |
14683 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14684 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14685 | type_(type), type_info_(type_info) |
14686 | { } | |
14687 | ||
14688 | protected: | |
0e168074 | 14689 | bool |
3ae06f68 | 14690 | do_is_static_initializer() const |
0e168074 | 14691 | { return true; } |
14692 | ||
e440a328 | 14693 | Type* |
14694 | do_type(); | |
14695 | ||
14696 | void | |
14697 | do_determine_type(const Type_context*) | |
14698 | { } | |
14699 | ||
14700 | Expression* | |
14701 | do_copy() | |
14702 | { return this; } | |
14703 | ||
ea664253 | 14704 | Bexpression* |
14705 | do_get_backend(Translate_context* context); | |
e440a328 | 14706 | |
d751bb78 | 14707 | void |
14708 | do_dump_expression(Ast_dump_context*) const; | |
14709 | ||
e440a328 | 14710 | private: |
14711 | // The type for which we are getting information. | |
14712 | Type* type_; | |
14713 | // What information we want. | |
14714 | Type_info type_info_; | |
14715 | }; | |
14716 | ||
14717 | // The type is chosen to match what the type descriptor struct | |
14718 | // expects. | |
14719 | ||
14720 | Type* | |
14721 | Type_info_expression::do_type() | |
14722 | { | |
14723 | switch (this->type_info_) | |
14724 | { | |
14725 | case TYPE_INFO_SIZE: | |
03118c21 | 14726 | case TYPE_INFO_BACKEND_PTRDATA: |
14727 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
e440a328 | 14728 | return Type::lookup_integer_type("uintptr"); |
14729 | case TYPE_INFO_ALIGNMENT: | |
14730 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14731 | return Type::lookup_integer_type("uint8"); | |
14732 | default: | |
c3e6f413 | 14733 | go_unreachable(); |
e440a328 | 14734 | } |
14735 | } | |
14736 | ||
ea664253 | 14737 | // Return the backend representation for type information. |
e440a328 | 14738 | |
ea664253 | 14739 | Bexpression* |
14740 | Type_info_expression::do_get_backend(Translate_context* context) | |
e440a328 | 14741 | { |
927a01eb | 14742 | Gogo* gogo = context->gogo(); |
2a305b85 | 14743 | bool ok = true; |
3f378015 | 14744 | int64_t val; |
927a01eb | 14745 | switch (this->type_info_) |
e440a328 | 14746 | { |
927a01eb | 14747 | case TYPE_INFO_SIZE: |
2a305b85 | 14748 | ok = this->type_->backend_type_size(gogo, &val); |
927a01eb | 14749 | break; |
14750 | case TYPE_INFO_ALIGNMENT: | |
2a305b85 | 14751 | ok = this->type_->backend_type_align(gogo, &val); |
927a01eb | 14752 | break; |
14753 | case TYPE_INFO_FIELD_ALIGNMENT: | |
2a305b85 | 14754 | ok = this->type_->backend_type_field_align(gogo, &val); |
927a01eb | 14755 | break; |
03118c21 | 14756 | case TYPE_INFO_BACKEND_PTRDATA: |
14757 | ok = this->type_->backend_type_ptrdata(gogo, &val); | |
14758 | break; | |
14759 | case TYPE_INFO_DESCRIPTOR_PTRDATA: | |
14760 | ok = this->type_->descriptor_ptrdata(gogo, &val); | |
14761 | break; | |
927a01eb | 14762 | default: |
14763 | go_unreachable(); | |
e440a328 | 14764 | } |
2a305b85 | 14765 | if (!ok) |
14766 | { | |
14767 | go_assert(saw_errors()); | |
14768 | return gogo->backend()->error_expression(); | |
14769 | } | |
3f378015 | 14770 | Expression* e = Expression::make_integer_int64(val, this->type(), |
14771 | this->location()); | |
14772 | return e->get_backend(context); | |
e440a328 | 14773 | } |
14774 | ||
d751bb78 | 14775 | // Dump ast representation for a type info expression. |
14776 | ||
14777 | void | |
14778 | Type_info_expression::do_dump_expression( | |
14779 | Ast_dump_context* ast_dump_context) const | |
14780 | { | |
14781 | ast_dump_context->ostream() << "typeinfo("; | |
14782 | ast_dump_context->dump_type(this->type_); | |
14783 | ast_dump_context->ostream() << ","; | |
14784 | ast_dump_context->ostream() << | |
14785 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14786 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
03118c21 | 14787 | : this->type_info_ == TYPE_INFO_SIZE ? "size" |
14788 | : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata" | |
14789 | : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata" | |
d751bb78 | 14790 | : "unknown"); |
14791 | ast_dump_context->ostream() << ")"; | |
14792 | } | |
14793 | ||
e440a328 | 14794 | // Make a type info expression. |
14795 | ||
14796 | Expression* | |
14797 | Expression::make_type_info(Type* type, Type_info type_info) | |
14798 | { | |
14799 | return new Type_info_expression(type, type_info); | |
14800 | } | |
14801 | ||
35a54f17 | 14802 | // An expression that evaluates to some characteristic of a slice. |
14803 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14804 | ||
14805 | class Slice_info_expression : public Expression | |
14806 | { | |
14807 | public: | |
14808 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14809 | Location location) | |
14810 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14811 | slice_(slice), slice_info_(slice_info) | |
14812 | { } | |
14813 | ||
14814 | protected: | |
14815 | Type* | |
14816 | do_type(); | |
14817 | ||
14818 | void | |
14819 | do_determine_type(const Type_context*) | |
14820 | { } | |
14821 | ||
14822 | Expression* | |
14823 | do_copy() | |
14824 | { | |
14825 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14826 | this->location()); | |
14827 | } | |
14828 | ||
ea664253 | 14829 | Bexpression* |
14830 | do_get_backend(Translate_context* context); | |
35a54f17 | 14831 | |
14832 | void | |
14833 | do_dump_expression(Ast_dump_context*) const; | |
14834 | ||
14835 | void | |
14836 | do_issue_nil_check() | |
14837 | { this->slice_->issue_nil_check(); } | |
14838 | ||
14839 | private: | |
14840 | // The slice for which we are getting information. | |
14841 | Expression* slice_; | |
14842 | // What information we want. | |
14843 | Slice_info slice_info_; | |
14844 | }; | |
14845 | ||
14846 | // Return the type of the slice info. | |
14847 | ||
14848 | Type* | |
14849 | Slice_info_expression::do_type() | |
14850 | { | |
14851 | switch (this->slice_info_) | |
14852 | { | |
14853 | case SLICE_INFO_VALUE_POINTER: | |
14854 | return Type::make_pointer_type( | |
14855 | this->slice_->type()->array_type()->element_type()); | |
14856 | case SLICE_INFO_LENGTH: | |
14857 | case SLICE_INFO_CAPACITY: | |
14858 | return Type::lookup_integer_type("int"); | |
14859 | default: | |
14860 | go_unreachable(); | |
14861 | } | |
14862 | } | |
14863 | ||
ea664253 | 14864 | // Return the backend information for slice information. |
35a54f17 | 14865 | |
ea664253 | 14866 | Bexpression* |
14867 | Slice_info_expression::do_get_backend(Translate_context* context) | |
35a54f17 | 14868 | { |
14869 | Gogo* gogo = context->gogo(); | |
ea664253 | 14870 | Bexpression* bslice = this->slice_->get_backend(context); |
35a54f17 | 14871 | switch (this->slice_info_) |
14872 | { | |
14873 | case SLICE_INFO_VALUE_POINTER: | |
14874 | case SLICE_INFO_LENGTH: | |
14875 | case SLICE_INFO_CAPACITY: | |
ea664253 | 14876 | return gogo->backend()->struct_field_expression(bslice, this->slice_info_, |
14877 | this->location()); | |
35a54f17 | 14878 | break; |
14879 | default: | |
14880 | go_unreachable(); | |
14881 | } | |
35a54f17 | 14882 | } |
14883 | ||
14884 | // Dump ast representation for a type info expression. | |
14885 | ||
14886 | void | |
14887 | Slice_info_expression::do_dump_expression( | |
14888 | Ast_dump_context* ast_dump_context) const | |
14889 | { | |
14890 | ast_dump_context->ostream() << "sliceinfo("; | |
14891 | this->slice_->dump_expression(ast_dump_context); | |
14892 | ast_dump_context->ostream() << ","; | |
14893 | ast_dump_context->ostream() << | |
14894 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14895 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14896 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14897 | : "unknown"); | |
14898 | ast_dump_context->ostream() << ")"; | |
14899 | } | |
14900 | ||
14901 | // Make a slice info expression. | |
14902 | ||
14903 | Expression* | |
14904 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14905 | Location location) | |
14906 | { | |
14907 | return new Slice_info_expression(slice, slice_info, location); | |
14908 | } | |
14909 | ||
2c809f8f | 14910 | // An expression that represents a slice value: a struct with value pointer, |
14911 | // length, and capacity fields. | |
14912 | ||
14913 | class Slice_value_expression : public Expression | |
14914 | { | |
14915 | public: | |
14916 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14917 | Expression* cap, Location location) | |
14918 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14919 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14920 | { } | |
14921 | ||
14922 | protected: | |
14923 | int | |
14924 | do_traverse(Traverse*); | |
14925 | ||
14926 | Type* | |
14927 | do_type() | |
14928 | { return this->type_; } | |
14929 | ||
14930 | void | |
14931 | do_determine_type(const Type_context*) | |
14932 | { go_unreachable(); } | |
14933 | ||
14934 | Expression* | |
14935 | do_copy() | |
14936 | { | |
14937 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14938 | this->len_->copy(), this->cap_->copy(), | |
14939 | this->location()); | |
14940 | } | |
14941 | ||
ea664253 | 14942 | Bexpression* |
14943 | do_get_backend(Translate_context* context); | |
2c809f8f | 14944 | |
14945 | void | |
14946 | do_dump_expression(Ast_dump_context*) const; | |
14947 | ||
14948 | private: | |
14949 | // The type of the slice value. | |
14950 | Type* type_; | |
14951 | // The pointer to the values in the slice. | |
14952 | Expression* valptr_; | |
14953 | // The length of the slice. | |
14954 | Expression* len_; | |
14955 | // The capacity of the slice. | |
14956 | Expression* cap_; | |
14957 | }; | |
14958 | ||
14959 | int | |
14960 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14961 | { | |
55e8ba6a | 14962 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT |
14963 | || Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
2c809f8f | 14964 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT |
14965 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14966 | return TRAVERSE_EXIT; | |
14967 | return TRAVERSE_CONTINUE; | |
14968 | } | |
14969 | ||
ea664253 | 14970 | Bexpression* |
14971 | Slice_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 14972 | { |
14973 | std::vector<Bexpression*> vals(3); | |
ea664253 | 14974 | vals[0] = this->valptr_->get_backend(context); |
14975 | vals[1] = this->len_->get_backend(context); | |
14976 | vals[2] = this->cap_->get_backend(context); | |
2c809f8f | 14977 | |
14978 | Gogo* gogo = context->gogo(); | |
14979 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 14980 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 14981 | } |
14982 | ||
14983 | void | |
14984 | Slice_value_expression::do_dump_expression( | |
14985 | Ast_dump_context* ast_dump_context) const | |
14986 | { | |
14987 | ast_dump_context->ostream() << "slicevalue("; | |
14988 | ast_dump_context->ostream() << "values: "; | |
14989 | this->valptr_->dump_expression(ast_dump_context); | |
14990 | ast_dump_context->ostream() << ", length: "; | |
14991 | this->len_->dump_expression(ast_dump_context); | |
14992 | ast_dump_context->ostream() << ", capacity: "; | |
14993 | this->cap_->dump_expression(ast_dump_context); | |
14994 | ast_dump_context->ostream() << ")"; | |
14995 | } | |
14996 | ||
14997 | Expression* | |
14998 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14999 | Expression* cap, Location location) | |
15000 | { | |
15001 | go_assert(at->is_slice_type()); | |
15002 | return new Slice_value_expression(at, valptr, len, cap, location); | |
15003 | } | |
2387f644 | 15004 | |
15005 | // An expression that evaluates to some characteristic of a non-empty interface. | |
15006 | // This is used to access the method table or underlying object of an interface. | |
15007 | ||
15008 | class Interface_info_expression : public Expression | |
15009 | { | |
15010 | public: | |
15011 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 15012 | Location location) |
2387f644 | 15013 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
15014 | iface_(iface), iface_info_(iface_info) | |
15015 | { } | |
15016 | ||
15017 | protected: | |
15018 | Type* | |
15019 | do_type(); | |
15020 | ||
15021 | void | |
15022 | do_determine_type(const Type_context*) | |
15023 | { } | |
15024 | ||
15025 | Expression* | |
15026 | do_copy() | |
15027 | { | |
15028 | return new Interface_info_expression(this->iface_->copy(), | |
15029 | this->iface_info_, this->location()); | |
15030 | } | |
15031 | ||
ea664253 | 15032 | Bexpression* |
15033 | do_get_backend(Translate_context* context); | |
2387f644 | 15034 | |
15035 | void | |
15036 | do_dump_expression(Ast_dump_context*) const; | |
15037 | ||
15038 | void | |
15039 | do_issue_nil_check() | |
15040 | { this->iface_->issue_nil_check(); } | |
15041 | ||
15042 | private: | |
15043 | // The interface for which we are getting information. | |
15044 | Expression* iface_; | |
15045 | // What information we want. | |
15046 | Interface_info iface_info_; | |
15047 | }; | |
15048 | ||
15049 | // Return the type of the interface info. | |
15050 | ||
15051 | Type* | |
15052 | Interface_info_expression::do_type() | |
15053 | { | |
15054 | switch (this->iface_info_) | |
15055 | { | |
15056 | case INTERFACE_INFO_METHODS: | |
15057 | { | |
625d3118 | 15058 | typedef Unordered_map(Interface_type*, Type*) Hashtable; |
15059 | static Hashtable result_types; | |
15060 | ||
15061 | Interface_type* itype = this->iface_->type()->interface_type(); | |
15062 | ||
15063 | Hashtable::const_iterator p = result_types.find(itype); | |
15064 | if (p != result_types.end()) | |
15065 | return p->second; | |
15066 | ||
2c809f8f | 15067 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
625d3118 | 15068 | if (itype->is_empty()) |
15069 | { | |
15070 | result_types[itype] = pdt; | |
15071 | return pdt; | |
15072 | } | |
2c809f8f | 15073 | |
2387f644 | 15074 | Location loc = this->location(); |
15075 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 15076 | sfl->push_back( |
15077 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
15078 | ||
2387f644 | 15079 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); |
15080 | p != itype->methods()->end(); | |
15081 | ++p) | |
15082 | { | |
15083 | Function_type* ft = p->type()->function_type(); | |
15084 | go_assert(ft->receiver() == NULL); | |
15085 | ||
15086 | const Typed_identifier_list* params = ft->parameters(); | |
15087 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
15088 | if (params != NULL) | |
15089 | mparams->reserve(params->size() + 1); | |
15090 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
15091 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
15092 | if (params != NULL) | |
15093 | { | |
15094 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
15095 | pp != params->end(); | |
15096 | ++pp) | |
15097 | mparams->push_back(*pp); | |
15098 | } | |
15099 | ||
15100 | Typed_identifier_list* mresults = (ft->results() == NULL | |
15101 | ? NULL | |
15102 | : ft->results()->copy()); | |
15103 | Backend_function_type* mft = | |
15104 | Type::make_backend_function_type(NULL, mparams, mresults, | |
15105 | ft->location()); | |
15106 | ||
15107 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
15108 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
15109 | } | |
15110 | ||
6bf4793c | 15111 | Struct_type* st = Type::make_struct_type(sfl, loc); |
15112 | st->set_is_struct_incomparable(); | |
15113 | Pointer_type *pt = Type::make_pointer_type(st); | |
625d3118 | 15114 | result_types[itype] = pt; |
15115 | return pt; | |
2387f644 | 15116 | } |
15117 | case INTERFACE_INFO_OBJECT: | |
15118 | return Type::make_pointer_type(Type::make_void_type()); | |
15119 | default: | |
15120 | go_unreachable(); | |
15121 | } | |
15122 | } | |
15123 | ||
ea664253 | 15124 | // Return the backend representation for interface information. |
2387f644 | 15125 | |
ea664253 | 15126 | Bexpression* |
15127 | Interface_info_expression::do_get_backend(Translate_context* context) | |
2387f644 | 15128 | { |
15129 | Gogo* gogo = context->gogo(); | |
ea664253 | 15130 | Bexpression* biface = this->iface_->get_backend(context); |
2387f644 | 15131 | switch (this->iface_info_) |
15132 | { | |
15133 | case INTERFACE_INFO_METHODS: | |
15134 | case INTERFACE_INFO_OBJECT: | |
ea664253 | 15135 | return gogo->backend()->struct_field_expression(biface, this->iface_info_, |
15136 | this->location()); | |
2387f644 | 15137 | break; |
15138 | default: | |
15139 | go_unreachable(); | |
15140 | } | |
2387f644 | 15141 | } |
15142 | ||
15143 | // Dump ast representation for an interface info expression. | |
15144 | ||
15145 | void | |
15146 | Interface_info_expression::do_dump_expression( | |
15147 | Ast_dump_context* ast_dump_context) const | |
15148 | { | |
2c809f8f | 15149 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 15150 | ast_dump_context->ostream() << "interfaceinfo("; |
15151 | this->iface_->dump_expression(ast_dump_context); | |
15152 | ast_dump_context->ostream() << ","; | |
15153 | ast_dump_context->ostream() << | |
2c809f8f | 15154 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
15155 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 15156 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
15157 | : "unknown"); | |
15158 | ast_dump_context->ostream() << ")"; | |
15159 | } | |
15160 | ||
15161 | // Make an interface info expression. | |
15162 | ||
15163 | Expression* | |
15164 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
15165 | Location location) | |
15166 | { | |
15167 | return new Interface_info_expression(iface, iface_info, location); | |
15168 | } | |
15169 | ||
2c809f8f | 15170 | // An expression that represents an interface value. The first field is either |
15171 | // a type descriptor for an empty interface or a pointer to the interface method | |
15172 | // table for a non-empty interface. The second field is always the object. | |
15173 | ||
15174 | class Interface_value_expression : public Expression | |
15175 | { | |
15176 | public: | |
15177 | Interface_value_expression(Type* type, Expression* first_field, | |
15178 | Expression* obj, Location location) | |
15179 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
15180 | type_(type), first_field_(first_field), obj_(obj) | |
15181 | { } | |
15182 | ||
15183 | protected: | |
15184 | int | |
15185 | do_traverse(Traverse*); | |
15186 | ||
15187 | Type* | |
15188 | do_type() | |
15189 | { return this->type_; } | |
15190 | ||
15191 | void | |
15192 | do_determine_type(const Type_context*) | |
15193 | { go_unreachable(); } | |
15194 | ||
15195 | Expression* | |
15196 | do_copy() | |
15197 | { | |
15198 | return new Interface_value_expression(this->type_, | |
15199 | this->first_field_->copy(), | |
15200 | this->obj_->copy(), this->location()); | |
15201 | } | |
15202 | ||
ea664253 | 15203 | Bexpression* |
15204 | do_get_backend(Translate_context* context); | |
2c809f8f | 15205 | |
15206 | void | |
15207 | do_dump_expression(Ast_dump_context*) const; | |
15208 | ||
15209 | private: | |
15210 | // The type of the interface value. | |
15211 | Type* type_; | |
15212 | // The first field of the interface (either a type descriptor or a pointer | |
15213 | // to the method table. | |
15214 | Expression* first_field_; | |
15215 | // The underlying object of the interface. | |
15216 | Expression* obj_; | |
15217 | }; | |
15218 | ||
15219 | int | |
15220 | Interface_value_expression::do_traverse(Traverse* traverse) | |
15221 | { | |
15222 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
15223 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
15224 | return TRAVERSE_EXIT; | |
15225 | return TRAVERSE_CONTINUE; | |
15226 | } | |
15227 | ||
ea664253 | 15228 | Bexpression* |
15229 | Interface_value_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15230 | { |
15231 | std::vector<Bexpression*> vals(2); | |
ea664253 | 15232 | vals[0] = this->first_field_->get_backend(context); |
15233 | vals[1] = this->obj_->get_backend(context); | |
2c809f8f | 15234 | |
15235 | Gogo* gogo = context->gogo(); | |
15236 | Btype* btype = this->type_->get_backend(gogo); | |
ea664253 | 15237 | return gogo->backend()->constructor_expression(btype, vals, this->location()); |
2c809f8f | 15238 | } |
15239 | ||
15240 | void | |
15241 | Interface_value_expression::do_dump_expression( | |
15242 | Ast_dump_context* ast_dump_context) const | |
15243 | { | |
15244 | ast_dump_context->ostream() << "interfacevalue("; | |
15245 | ast_dump_context->ostream() << | |
15246 | (this->type_->interface_type()->is_empty() | |
15247 | ? "type_descriptor: " | |
15248 | : "methods: "); | |
15249 | this->first_field_->dump_expression(ast_dump_context); | |
15250 | ast_dump_context->ostream() << ", object: "; | |
15251 | this->obj_->dump_expression(ast_dump_context); | |
15252 | ast_dump_context->ostream() << ")"; | |
15253 | } | |
15254 | ||
15255 | Expression* | |
15256 | Expression::make_interface_value(Type* type, Expression* first_value, | |
15257 | Expression* object, Location location) | |
15258 | { | |
15259 | return new Interface_value_expression(type, first_value, object, location); | |
15260 | } | |
15261 | ||
15262 | // An interface method table for a pair of types: an interface type and a type | |
15263 | // that implements that interface. | |
15264 | ||
15265 | class Interface_mtable_expression : public Expression | |
15266 | { | |
15267 | public: | |
15268 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
15269 | bool is_pointer, Location location) | |
15270 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
15271 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
15272 | method_table_type_(NULL), bvar_(NULL) | |
15273 | { } | |
15274 | ||
15275 | protected: | |
15276 | int | |
15277 | do_traverse(Traverse*); | |
15278 | ||
15279 | Type* | |
15280 | do_type(); | |
15281 | ||
15282 | bool | |
3ae06f68 | 15283 | do_is_static_initializer() const |
2c809f8f | 15284 | { return true; } |
15285 | ||
15286 | void | |
15287 | do_determine_type(const Type_context*) | |
15288 | { go_unreachable(); } | |
15289 | ||
15290 | Expression* | |
15291 | do_copy() | |
15292 | { | |
15293 | return new Interface_mtable_expression(this->itype_, this->type_, | |
15294 | this->is_pointer_, this->location()); | |
15295 | } | |
15296 | ||
15297 | bool | |
15298 | do_is_addressable() const | |
15299 | { return true; } | |
15300 | ||
ea664253 | 15301 | Bexpression* |
15302 | do_get_backend(Translate_context* context); | |
2c809f8f | 15303 | |
15304 | void | |
15305 | do_dump_expression(Ast_dump_context*) const; | |
15306 | ||
15307 | private: | |
15308 | // The interface type for which the methods are defined. | |
15309 | Interface_type* itype_; | |
15310 | // The type to construct the interface method table for. | |
15311 | Type* type_; | |
15312 | // Whether this table contains the method set for the receiver type or the | |
15313 | // pointer receiver type. | |
15314 | bool is_pointer_; | |
15315 | // The type of the method table. | |
15316 | Type* method_table_type_; | |
15317 | // The backend variable that refers to the interface method table. | |
15318 | Bvariable* bvar_; | |
15319 | }; | |
15320 | ||
15321 | int | |
15322 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
15323 | { | |
15324 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
15325 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
15326 | return TRAVERSE_EXIT; | |
15327 | return TRAVERSE_CONTINUE; | |
15328 | } | |
15329 | ||
15330 | Type* | |
15331 | Interface_mtable_expression::do_type() | |
15332 | { | |
15333 | if (this->method_table_type_ != NULL) | |
15334 | return this->method_table_type_; | |
15335 | ||
15336 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15337 | go_assert(!interface_methods->empty()); | |
15338 | ||
15339 | Struct_field_list* sfl = new Struct_field_list; | |
15340 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
15341 | this->location()); | |
15342 | sfl->push_back(Struct_field(tid)); | |
db122cb9 | 15343 | Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type()); |
2c809f8f | 15344 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); |
15345 | p != interface_methods->end(); | |
15346 | ++p) | |
db122cb9 | 15347 | { |
15348 | // We want C function pointers here, not func descriptors; model | |
15349 | // using void* pointers. | |
15350 | Typed_identifier method(p->name(), unsafe_ptr_type, p->location()); | |
15351 | sfl->push_back(Struct_field(method)); | |
15352 | } | |
6bf4793c | 15353 | Struct_type* st = Type::make_struct_type(sfl, this->location()); |
15354 | st->set_is_struct_incomparable(); | |
15355 | this->method_table_type_ = st; | |
2c809f8f | 15356 | return this->method_table_type_; |
15357 | } | |
15358 | ||
ea664253 | 15359 | Bexpression* |
15360 | Interface_mtable_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15361 | { |
15362 | Gogo* gogo = context->gogo(); | |
2c809f8f | 15363 | Location loc = Linemap::predeclared_location(); |
15364 | if (this->bvar_ != NULL) | |
7af8e400 | 15365 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15366 | |
15367 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
15368 | go_assert(!interface_methods->empty()); | |
15369 | ||
19272321 | 15370 | std::string mangled_name = |
15371 | gogo->interface_method_table_name(this->itype_, this->type_, | |
15372 | this->is_pointer_); | |
2c809f8f | 15373 | |
1530c754 | 15374 | // Set is_public if we are converting a named type to an interface |
15375 | // type that is defined in the same package as the named type, and | |
15376 | // the interface has hidden methods. In that case the interface | |
15377 | // method table will be defined by the package that defines the | |
15378 | // types. | |
15379 | bool is_public = false; | |
15380 | if (this->type_->named_type() != NULL | |
15381 | && (this->type_->named_type()->named_object()->package() | |
15382 | == this->itype_->package())) | |
15383 | { | |
15384 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15385 | p != interface_methods->end(); | |
15386 | ++p) | |
2c809f8f | 15387 | { |
1530c754 | 15388 | if (Gogo::is_hidden_name(p->name())) |
15389 | { | |
15390 | is_public = true; | |
15391 | break; | |
15392 | } | |
2c809f8f | 15393 | } |
15394 | } | |
15395 | ||
1530c754 | 15396 | if (is_public |
2c809f8f | 15397 | && this->type_->named_type()->named_object()->package() != NULL) |
15398 | { | |
1530c754 | 15399 | // The interface conversion table is defined elsewhere. |
2c809f8f | 15400 | Btype* btype = this->type()->get_backend(gogo); |
438b4bec | 15401 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
2c809f8f | 15402 | this->bvar_ = |
438b4bec | 15403 | gogo->backend()->immutable_struct_reference(mangled_name, asm_name, |
15404 | btype, loc); | |
7af8e400 | 15405 | return gogo->backend()->var_expression(this->bvar_, this->location()); |
2c809f8f | 15406 | } |
15407 | ||
15408 | // The first element is the type descriptor. | |
15409 | Type* td_type; | |
15410 | if (!this->is_pointer_) | |
15411 | td_type = this->type_; | |
15412 | else | |
15413 | td_type = Type::make_pointer_type(this->type_); | |
15414 | ||
db122cb9 | 15415 | std::vector<Backend::Btyped_identifier> bstructfields; |
15416 | ||
2c809f8f | 15417 | // Build an interface method table for a type: a type descriptor followed by a |
15418 | // list of function pointers, one for each interface method. This is used for | |
15419 | // interfaces. | |
15420 | Expression_list* svals = new Expression_list(); | |
db122cb9 | 15421 | Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc); |
15422 | svals->push_back(tdescriptor); | |
15423 | ||
15424 | Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo); | |
15425 | Backend::Btyped_identifier btd("_type", tdesc_btype, loc); | |
15426 | bstructfields.push_back(btd); | |
2c809f8f | 15427 | |
15428 | Named_type* nt = this->type_->named_type(); | |
15429 | Struct_type* st = this->type_->struct_type(); | |
15430 | go_assert(nt != NULL || st != NULL); | |
15431 | ||
15432 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
15433 | p != interface_methods->end(); | |
15434 | ++p) | |
15435 | { | |
15436 | bool is_ambiguous; | |
15437 | Method* m; | |
15438 | if (nt != NULL) | |
15439 | m = nt->method_function(p->name(), &is_ambiguous); | |
15440 | else | |
15441 | m = st->method_function(p->name(), &is_ambiguous); | |
15442 | go_assert(m != NULL); | |
15443 | Named_object* no = m->named_object(); | |
15444 | ||
15445 | go_assert(no->is_function() || no->is_function_declaration()); | |
db122cb9 | 15446 | |
15447 | Btype* fcn_btype = m->type()->get_backend_fntype(gogo); | |
15448 | Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc); | |
15449 | bstructfields.push_back(bmtype); | |
15450 | ||
2c809f8f | 15451 | svals->push_back(Expression::make_func_code_reference(no, loc)); |
15452 | } | |
15453 | ||
db122cb9 | 15454 | Btype *btype = gogo->backend()->struct_type(bstructfields); |
15455 | std::vector<Bexpression*> ctor_bexprs; | |
15456 | for (Expression_list::const_iterator pe = svals->begin(); | |
15457 | pe != svals->end(); | |
15458 | ++pe) | |
15459 | { | |
15460 | ctor_bexprs.push_back((*pe)->get_backend(context)); | |
15461 | } | |
15462 | Bexpression* ctor = | |
15463 | gogo->backend()->constructor_expression(btype, ctor_bexprs, loc); | |
2c809f8f | 15464 | |
438b4bec | 15465 | std::string asm_name(go_selectively_encode_id(mangled_name)); |
15466 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, asm_name, false, | |
2c809f8f | 15467 | !is_public, btype, loc); |
15468 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
15469 | !is_public, btype, loc, ctor); | |
7af8e400 | 15470 | return gogo->backend()->var_expression(this->bvar_, loc); |
2c809f8f | 15471 | } |
15472 | ||
15473 | void | |
15474 | Interface_mtable_expression::do_dump_expression( | |
15475 | Ast_dump_context* ast_dump_context) const | |
15476 | { | |
15477 | ast_dump_context->ostream() << "__go_" | |
15478 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
15479 | ast_dump_context->dump_type(this->itype_); | |
15480 | ast_dump_context->ostream() << "__"; | |
15481 | ast_dump_context->dump_type(this->type_); | |
15482 | } | |
15483 | ||
15484 | Expression* | |
15485 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
15486 | bool is_pointer, Location location) | |
15487 | { | |
15488 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
15489 | } | |
15490 | ||
e440a328 | 15491 | // An expression which evaluates to the offset of a field within a |
15492 | // struct. This, like Type_info_expression, q.v., is only used to | |
15493 | // initialize fields of a type descriptor. | |
15494 | ||
15495 | class Struct_field_offset_expression : public Expression | |
15496 | { | |
15497 | public: | |
15498 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 15499 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
15500 | Linemap::predeclared_location()), | |
e440a328 | 15501 | type_(type), field_(field) |
15502 | { } | |
15503 | ||
15504 | protected: | |
f23d7786 | 15505 | bool |
3ae06f68 | 15506 | do_is_static_initializer() const |
f23d7786 | 15507 | { return true; } |
15508 | ||
e440a328 | 15509 | Type* |
15510 | do_type() | |
15511 | { return Type::lookup_integer_type("uintptr"); } | |
15512 | ||
15513 | void | |
15514 | do_determine_type(const Type_context*) | |
15515 | { } | |
15516 | ||
15517 | Expression* | |
15518 | do_copy() | |
15519 | { return this; } | |
15520 | ||
ea664253 | 15521 | Bexpression* |
15522 | do_get_backend(Translate_context* context); | |
e440a328 | 15523 | |
d751bb78 | 15524 | void |
15525 | do_dump_expression(Ast_dump_context*) const; | |
15526 | ||
e440a328 | 15527 | private: |
15528 | // The type of the struct. | |
15529 | Struct_type* type_; | |
15530 | // The field. | |
15531 | const Struct_field* field_; | |
15532 | }; | |
15533 | ||
ea664253 | 15534 | // Return the backend representation for a struct field offset. |
e440a328 | 15535 | |
ea664253 | 15536 | Bexpression* |
15537 | Struct_field_offset_expression::do_get_backend(Translate_context* context) | |
e440a328 | 15538 | { |
e440a328 | 15539 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 15540 | Struct_field_list::const_iterator p; |
2c8bda43 | 15541 | unsigned i = 0; |
e440a328 | 15542 | for (p = fields->begin(); |
15543 | p != fields->end(); | |
2c8bda43 | 15544 | ++p, ++i) |
15545 | if (&*p == this->field_) | |
15546 | break; | |
c484d925 | 15547 | go_assert(&*p == this->field_); |
e440a328 | 15548 | |
2c8bda43 | 15549 | Gogo* gogo = context->gogo(); |
15550 | Btype* btype = this->type_->get_backend(gogo); | |
15551 | ||
3f378015 | 15552 | int64_t offset = gogo->backend()->type_field_offset(btype, i); |
2c8bda43 | 15553 | Type* uptr_type = Type::lookup_integer_type("uintptr"); |
e67508fa | 15554 | Expression* ret = |
3f378015 | 15555 | Expression::make_integer_int64(offset, uptr_type, |
15556 | Linemap::predeclared_location()); | |
ea664253 | 15557 | return ret->get_backend(context); |
e440a328 | 15558 | } |
15559 | ||
d751bb78 | 15560 | // Dump ast representation for a struct field offset expression. |
15561 | ||
15562 | void | |
15563 | Struct_field_offset_expression::do_dump_expression( | |
15564 | Ast_dump_context* ast_dump_context) const | |
15565 | { | |
15566 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 15567 | ast_dump_context->dump_type(this->type_); |
15568 | ast_dump_context->ostream() << '.'; | |
15569 | ast_dump_context->ostream() << | |
15570 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 15571 | ast_dump_context->ostream() << ")"; |
15572 | } | |
15573 | ||
e440a328 | 15574 | // Make an expression for a struct field offset. |
15575 | ||
15576 | Expression* | |
15577 | Expression::make_struct_field_offset(Struct_type* type, | |
15578 | const Struct_field* field) | |
15579 | { | |
15580 | return new Struct_field_offset_expression(type, field); | |
15581 | } | |
15582 | ||
15583 | // An expression which evaluates to the address of an unnamed label. | |
15584 | ||
15585 | class Label_addr_expression : public Expression | |
15586 | { | |
15587 | public: | |
b13c66cd | 15588 | Label_addr_expression(Label* label, Location location) |
e440a328 | 15589 | : Expression(EXPRESSION_LABEL_ADDR, location), |
15590 | label_(label) | |
15591 | { } | |
15592 | ||
15593 | protected: | |
15594 | Type* | |
15595 | do_type() | |
15596 | { return Type::make_pointer_type(Type::make_void_type()); } | |
15597 | ||
15598 | void | |
15599 | do_determine_type(const Type_context*) | |
15600 | { } | |
15601 | ||
15602 | Expression* | |
15603 | do_copy() | |
15604 | { return new Label_addr_expression(this->label_, this->location()); } | |
15605 | ||
ea664253 | 15606 | Bexpression* |
15607 | do_get_backend(Translate_context* context) | |
15608 | { return this->label_->get_addr(context, this->location()); } | |
e440a328 | 15609 | |
d751bb78 | 15610 | void |
15611 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15612 | { ast_dump_context->ostream() << this->label_->name(); } | |
15613 | ||
e440a328 | 15614 | private: |
15615 | // The label whose address we are taking. | |
15616 | Label* label_; | |
15617 | }; | |
15618 | ||
15619 | // Make an expression for the address of an unnamed label. | |
15620 | ||
15621 | Expression* | |
b13c66cd | 15622 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 15623 | { |
15624 | return new Label_addr_expression(label, location); | |
15625 | } | |
15626 | ||
da244e59 | 15627 | // Class Conditional_expression. |
283a177b | 15628 | |
2c809f8f | 15629 | // Traversal. |
15630 | ||
15631 | int | |
15632 | Conditional_expression::do_traverse(Traverse* traverse) | |
15633 | { | |
15634 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15635 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15636 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15637 | return TRAVERSE_EXIT; | |
15638 | return TRAVERSE_CONTINUE; | |
15639 | } | |
15640 | ||
283a177b | 15641 | // Return the type of the conditional expression. |
15642 | ||
15643 | Type* | |
15644 | Conditional_expression::do_type() | |
15645 | { | |
15646 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15647 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15648 | NULL)) | |
283a177b | 15649 | result_type = this->then_->type(); |
15650 | else if (this->then_->is_nil_expression() | |
15651 | || this->else_->is_nil_expression()) | |
15652 | result_type = (!this->then_->is_nil_expression() | |
15653 | ? this->then_->type() | |
15654 | : this->else_->type()); | |
15655 | return result_type; | |
15656 | } | |
15657 | ||
2c809f8f | 15658 | // Determine type for a conditional expression. |
15659 | ||
15660 | void | |
15661 | Conditional_expression::do_determine_type(const Type_context* context) | |
15662 | { | |
15663 | this->cond_->determine_type_no_context(); | |
15664 | this->then_->determine_type(context); | |
15665 | this->else_->determine_type(context); | |
15666 | } | |
15667 | ||
283a177b | 15668 | // Get the backend representation of a conditional expression. |
15669 | ||
ea664253 | 15670 | Bexpression* |
15671 | Conditional_expression::do_get_backend(Translate_context* context) | |
283a177b | 15672 | { |
15673 | Gogo* gogo = context->gogo(); | |
15674 | Btype* result_btype = this->type()->get_backend(gogo); | |
ea664253 | 15675 | Bexpression* cond = this->cond_->get_backend(context); |
15676 | Bexpression* then = this->then_->get_backend(context); | |
15677 | Bexpression* belse = this->else_->get_backend(context); | |
93715b75 | 15678 | Bfunction* bfn = context->function()->func_value()->get_decl(); |
15679 | return gogo->backend()->conditional_expression(bfn, result_btype, cond, then, | |
ea664253 | 15680 | belse, this->location()); |
283a177b | 15681 | } |
15682 | ||
15683 | // Dump ast representation of a conditional expression. | |
15684 | ||
15685 | void | |
15686 | Conditional_expression::do_dump_expression( | |
15687 | Ast_dump_context* ast_dump_context) const | |
15688 | { | |
15689 | ast_dump_context->ostream() << "("; | |
15690 | ast_dump_context->dump_expression(this->cond_); | |
15691 | ast_dump_context->ostream() << " ? "; | |
15692 | ast_dump_context->dump_expression(this->then_); | |
15693 | ast_dump_context->ostream() << " : "; | |
15694 | ast_dump_context->dump_expression(this->else_); | |
15695 | ast_dump_context->ostream() << ") "; | |
15696 | } | |
15697 | ||
15698 | // Make a conditional expression. | |
15699 | ||
15700 | Expression* | |
15701 | Expression::make_conditional(Expression* cond, Expression* then, | |
15702 | Expression* else_expr, Location location) | |
15703 | { | |
15704 | return new Conditional_expression(cond, then, else_expr, location); | |
15705 | } | |
15706 | ||
da244e59 | 15707 | // Class Compound_expression. |
2c809f8f | 15708 | |
15709 | // Traversal. | |
15710 | ||
15711 | int | |
15712 | Compound_expression::do_traverse(Traverse* traverse) | |
15713 | { | |
15714 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15715 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15716 | return TRAVERSE_EXIT; | |
15717 | return TRAVERSE_CONTINUE; | |
15718 | } | |
15719 | ||
15720 | // Return the type of the compound expression. | |
15721 | ||
15722 | Type* | |
15723 | Compound_expression::do_type() | |
15724 | { | |
15725 | return this->expr_->type(); | |
15726 | } | |
15727 | ||
15728 | // Determine type for a compound expression. | |
15729 | ||
15730 | void | |
15731 | Compound_expression::do_determine_type(const Type_context* context) | |
15732 | { | |
15733 | this->init_->determine_type_no_context(); | |
15734 | this->expr_->determine_type(context); | |
15735 | } | |
15736 | ||
15737 | // Get the backend representation of a compound expression. | |
15738 | ||
ea664253 | 15739 | Bexpression* |
15740 | Compound_expression::do_get_backend(Translate_context* context) | |
2c809f8f | 15741 | { |
15742 | Gogo* gogo = context->gogo(); | |
ea664253 | 15743 | Bexpression* binit = this->init_->get_backend(context); |
0ab48656 | 15744 | Bfunction* bfunction = context->function()->func_value()->get_decl(); |
15745 | Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction, | |
15746 | binit); | |
ea664253 | 15747 | Bexpression* bexpr = this->expr_->get_backend(context); |
15748 | return gogo->backend()->compound_expression(init_stmt, bexpr, | |
15749 | this->location()); | |
2c809f8f | 15750 | } |
15751 | ||
15752 | // Dump ast representation of a conditional expression. | |
15753 | ||
15754 | void | |
15755 | Compound_expression::do_dump_expression( | |
15756 | Ast_dump_context* ast_dump_context) const | |
15757 | { | |
15758 | ast_dump_context->ostream() << "("; | |
15759 | ast_dump_context->dump_expression(this->init_); | |
15760 | ast_dump_context->ostream() << ","; | |
15761 | ast_dump_context->dump_expression(this->expr_); | |
15762 | ast_dump_context->ostream() << ") "; | |
15763 | } | |
15764 | ||
15765 | // Make a compound expression. | |
15766 | ||
15767 | Expression* | |
15768 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15769 | { | |
15770 | return new Compound_expression(init, expr, location); | |
15771 | } | |
15772 | ||
1b4fb1e0 | 15773 | // Class Backend_expression. |
15774 | ||
15775 | int | |
15776 | Backend_expression::do_traverse(Traverse*) | |
15777 | { | |
15778 | return TRAVERSE_CONTINUE; | |
15779 | } | |
15780 | ||
15781 | void | |
15782 | Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
15783 | { | |
15784 | ast_dump_context->ostream() << "backend_expression<"; | |
15785 | ast_dump_context->dump_type(this->type_); | |
15786 | ast_dump_context->ostream() << ">"; | |
15787 | } | |
15788 | ||
15789 | Expression* | |
15790 | Expression::make_backend(Bexpression* bexpr, Type* type, Location location) | |
15791 | { | |
15792 | return new Backend_expression(bexpr, type, location); | |
15793 | } | |
15794 | ||
e440a328 | 15795 | // Import an expression. This comes at the end in order to see the |
15796 | // various class definitions. | |
15797 | ||
15798 | Expression* | |
15799 | Expression::import_expression(Import* imp) | |
15800 | { | |
15801 | int c = imp->peek_char(); | |
15802 | if (imp->match_c_string("- ") | |
15803 | || imp->match_c_string("! ") | |
15804 | || imp->match_c_string("^ ")) | |
15805 | return Unary_expression::do_import(imp); | |
15806 | else if (c == '(') | |
15807 | return Binary_expression::do_import(imp); | |
15808 | else if (imp->match_c_string("true") | |
15809 | || imp->match_c_string("false")) | |
15810 | return Boolean_expression::do_import(imp); | |
15811 | else if (c == '"') | |
15812 | return String_expression::do_import(imp); | |
15813 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15814 | { | |
15815 | // This handles integers, floats and complex constants. | |
15816 | return Integer_expression::do_import(imp); | |
15817 | } | |
15818 | else if (imp->match_c_string("nil")) | |
15819 | return Nil_expression::do_import(imp); | |
15820 | else if (imp->match_c_string("convert")) | |
15821 | return Type_conversion_expression::do_import(imp); | |
15822 | else | |
15823 | { | |
631d5788 | 15824 | go_error_at(imp->location(), "import error: expected expression"); |
e440a328 | 15825 | return Expression::make_error(imp->location()); |
15826 | } | |
15827 | } | |
15828 | ||
15829 | // Class Expression_list. | |
15830 | ||
15831 | // Traverse the list. | |
15832 | ||
15833 | int | |
15834 | Expression_list::traverse(Traverse* traverse) | |
15835 | { | |
15836 | for (Expression_list::iterator p = this->begin(); | |
15837 | p != this->end(); | |
15838 | ++p) | |
15839 | { | |
15840 | if (*p != NULL) | |
15841 | { | |
15842 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15843 | return TRAVERSE_EXIT; | |
15844 | } | |
15845 | } | |
15846 | return TRAVERSE_CONTINUE; | |
15847 | } | |
15848 | ||
15849 | // Copy the list. | |
15850 | ||
15851 | Expression_list* | |
15852 | Expression_list::copy() | |
15853 | { | |
15854 | Expression_list* ret = new Expression_list(); | |
15855 | for (Expression_list::iterator p = this->begin(); | |
15856 | p != this->end(); | |
15857 | ++p) | |
15858 | { | |
15859 | if (*p == NULL) | |
15860 | ret->push_back(NULL); | |
15861 | else | |
15862 | ret->push_back((*p)->copy()); | |
15863 | } | |
15864 | return ret; | |
15865 | } | |
15866 | ||
15867 | // Return whether an expression list has an error expression. | |
15868 | ||
15869 | bool | |
15870 | Expression_list::contains_error() const | |
15871 | { | |
15872 | for (Expression_list::const_iterator p = this->begin(); | |
15873 | p != this->end(); | |
15874 | ++p) | |
15875 | if (*p != NULL && (*p)->is_error_expression()) | |
15876 | return true; | |
15877 | return false; | |
15878 | } | |
0c77715b | 15879 | |
15880 | // Class Numeric_constant. | |
15881 | ||
15882 | // Destructor. | |
15883 | ||
15884 | Numeric_constant::~Numeric_constant() | |
15885 | { | |
15886 | this->clear(); | |
15887 | } | |
15888 | ||
15889 | // Copy constructor. | |
15890 | ||
15891 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15892 | : classification_(a.classification_), type_(a.type_) | |
15893 | { | |
15894 | switch (a.classification_) | |
15895 | { | |
15896 | case NC_INVALID: | |
15897 | break; | |
15898 | case NC_INT: | |
15899 | case NC_RUNE: | |
15900 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15901 | break; | |
15902 | case NC_FLOAT: | |
15903 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15904 | break; | |
15905 | case NC_COMPLEX: | |
fcbea5e4 | 15906 | mpc_init2(this->u_.complex_val, mpc_precision); |
15907 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15908 | break; |
15909 | default: | |
15910 | go_unreachable(); | |
15911 | } | |
15912 | } | |
15913 | ||
15914 | // Assignment operator. | |
15915 | ||
15916 | Numeric_constant& | |
15917 | Numeric_constant::operator=(const Numeric_constant& a) | |
15918 | { | |
15919 | this->clear(); | |
15920 | this->classification_ = a.classification_; | |
15921 | this->type_ = a.type_; | |
15922 | switch (a.classification_) | |
15923 | { | |
15924 | case NC_INVALID: | |
15925 | break; | |
15926 | case NC_INT: | |
15927 | case NC_RUNE: | |
15928 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15929 | break; | |
15930 | case NC_FLOAT: | |
15931 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15932 | break; | |
15933 | case NC_COMPLEX: | |
fcbea5e4 | 15934 | mpc_init2(this->u_.complex_val, mpc_precision); |
15935 | mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN); | |
0c77715b | 15936 | break; |
15937 | default: | |
15938 | go_unreachable(); | |
15939 | } | |
15940 | return *this; | |
15941 | } | |
15942 | ||
15943 | // Clear the contents. | |
15944 | ||
15945 | void | |
15946 | Numeric_constant::clear() | |
15947 | { | |
15948 | switch (this->classification_) | |
15949 | { | |
15950 | case NC_INVALID: | |
15951 | break; | |
15952 | case NC_INT: | |
15953 | case NC_RUNE: | |
15954 | mpz_clear(this->u_.int_val); | |
15955 | break; | |
15956 | case NC_FLOAT: | |
15957 | mpfr_clear(this->u_.float_val); | |
15958 | break; | |
15959 | case NC_COMPLEX: | |
fcbea5e4 | 15960 | mpc_clear(this->u_.complex_val); |
0c77715b | 15961 | break; |
15962 | default: | |
15963 | go_unreachable(); | |
15964 | } | |
15965 | this->classification_ = NC_INVALID; | |
15966 | } | |
15967 | ||
15968 | // Set to an unsigned long value. | |
15969 | ||
15970 | void | |
15971 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15972 | { | |
15973 | this->clear(); | |
15974 | this->classification_ = NC_INT; | |
15975 | this->type_ = type; | |
15976 | mpz_init_set_ui(this->u_.int_val, val); | |
15977 | } | |
15978 | ||
15979 | // Set to an integer value. | |
15980 | ||
15981 | void | |
15982 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15983 | { | |
15984 | this->clear(); | |
15985 | this->classification_ = NC_INT; | |
15986 | this->type_ = type; | |
15987 | mpz_init_set(this->u_.int_val, val); | |
15988 | } | |
15989 | ||
15990 | // Set to a rune value. | |
15991 | ||
15992 | void | |
15993 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15994 | { | |
15995 | this->clear(); | |
15996 | this->classification_ = NC_RUNE; | |
15997 | this->type_ = type; | |
15998 | mpz_init_set(this->u_.int_val, val); | |
15999 | } | |
16000 | ||
16001 | // Set to a floating point value. | |
16002 | ||
16003 | void | |
16004 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
16005 | { | |
16006 | this->clear(); | |
16007 | this->classification_ = NC_FLOAT; | |
16008 | this->type_ = type; | |
833b523c | 16009 | // Numeric constants do not have negative zero values, so remove |
16010 | // them here. They also don't have infinity or NaN values, but we | |
16011 | // should never see them here. | |
16012 | if (mpfr_zero_p(val)) | |
16013 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
16014 | else | |
16015 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 16016 | } |
16017 | ||
16018 | // Set to a complex value. | |
16019 | ||
16020 | void | |
fcbea5e4 | 16021 | Numeric_constant::set_complex(Type* type, const mpc_t val) |
0c77715b | 16022 | { |
16023 | this->clear(); | |
16024 | this->classification_ = NC_COMPLEX; | |
16025 | this->type_ = type; | |
fcbea5e4 | 16026 | mpc_init2(this->u_.complex_val, mpc_precision); |
16027 | mpc_set(this->u_.complex_val, val, MPC_RNDNN); | |
0c77715b | 16028 | } |
16029 | ||
16030 | // Get an int value. | |
16031 | ||
16032 | void | |
16033 | Numeric_constant::get_int(mpz_t* val) const | |
16034 | { | |
16035 | go_assert(this->is_int()); | |
16036 | mpz_init_set(*val, this->u_.int_val); | |
16037 | } | |
16038 | ||
16039 | // Get a rune value. | |
16040 | ||
16041 | void | |
16042 | Numeric_constant::get_rune(mpz_t* val) const | |
16043 | { | |
16044 | go_assert(this->is_rune()); | |
16045 | mpz_init_set(*val, this->u_.int_val); | |
16046 | } | |
16047 | ||
16048 | // Get a floating point value. | |
16049 | ||
16050 | void | |
16051 | Numeric_constant::get_float(mpfr_t* val) const | |
16052 | { | |
16053 | go_assert(this->is_float()); | |
16054 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16055 | } | |
16056 | ||
16057 | // Get a complex value. | |
16058 | ||
16059 | void | |
fcbea5e4 | 16060 | Numeric_constant::get_complex(mpc_t* val) const |
0c77715b | 16061 | { |
16062 | go_assert(this->is_complex()); | |
fcbea5e4 | 16063 | mpc_init2(*val, mpc_precision); |
16064 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); | |
0c77715b | 16065 | } |
16066 | ||
16067 | // Express value as unsigned long if possible. | |
16068 | ||
16069 | Numeric_constant::To_unsigned_long | |
16070 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
16071 | { | |
16072 | switch (this->classification_) | |
16073 | { | |
16074 | case NC_INT: | |
16075 | case NC_RUNE: | |
16076 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
16077 | case NC_FLOAT: | |
16078 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
16079 | case NC_COMPLEX: | |
fcbea5e4 | 16080 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16081 | return NC_UL_NOTINT; |
fcbea5e4 | 16082 | return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val), |
16083 | val); | |
0c77715b | 16084 | default: |
16085 | go_unreachable(); | |
16086 | } | |
16087 | } | |
16088 | ||
16089 | // Express integer value as unsigned long if possible. | |
16090 | ||
16091 | Numeric_constant::To_unsigned_long | |
16092 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
16093 | unsigned long *val) const | |
16094 | { | |
16095 | if (mpz_sgn(ival) < 0) | |
16096 | return NC_UL_NEGATIVE; | |
16097 | unsigned long ui = mpz_get_ui(ival); | |
16098 | if (mpz_cmp_ui(ival, ui) != 0) | |
16099 | return NC_UL_BIG; | |
16100 | *val = ui; | |
16101 | return NC_UL_VALID; | |
16102 | } | |
16103 | ||
16104 | // Express floating point value as unsigned long if possible. | |
16105 | ||
16106 | Numeric_constant::To_unsigned_long | |
16107 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
16108 | unsigned long *val) const | |
16109 | { | |
16110 | if (!mpfr_integer_p(fval)) | |
16111 | return NC_UL_NOTINT; | |
16112 | mpz_t ival; | |
16113 | mpz_init(ival); | |
16114 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16115 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
16116 | mpz_clear(ival); | |
16117 | return ret; | |
16118 | } | |
16119 | ||
03118c21 | 16120 | // Express value as memory size if possible. |
16121 | ||
16122 | bool | |
16123 | Numeric_constant::to_memory_size(int64_t* val) const | |
16124 | { | |
16125 | switch (this->classification_) | |
16126 | { | |
16127 | case NC_INT: | |
16128 | case NC_RUNE: | |
16129 | return this->mpz_to_memory_size(this->u_.int_val, val); | |
16130 | case NC_FLOAT: | |
16131 | return this->mpfr_to_memory_size(this->u_.float_val, val); | |
16132 | case NC_COMPLEX: | |
16133 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
16134 | return false; | |
16135 | return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val); | |
16136 | default: | |
16137 | go_unreachable(); | |
16138 | } | |
16139 | } | |
16140 | ||
16141 | // Express integer as memory size if possible. | |
16142 | ||
16143 | bool | |
16144 | Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const | |
16145 | { | |
16146 | if (mpz_sgn(ival) < 0) | |
16147 | return false; | |
16148 | if (mpz_fits_slong_p(ival)) | |
16149 | { | |
16150 | *val = static_cast<int64_t>(mpz_get_si(ival)); | |
16151 | return true; | |
16152 | } | |
16153 | ||
16154 | // Test >= 64, not > 64, because an int64_t can hold 63 bits of a | |
16155 | // positive value. | |
16156 | if (mpz_sizeinbase(ival, 2) >= 64) | |
16157 | return false; | |
16158 | ||
16159 | mpz_t q, r; | |
16160 | mpz_init(q); | |
16161 | mpz_init(r); | |
16162 | mpz_tdiv_q_2exp(q, ival, 32); | |
16163 | mpz_tdiv_r_2exp(r, ival, 32); | |
16164 | go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r)); | |
16165 | *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32) | |
16166 | + static_cast<int64_t>(mpz_get_ui(r))); | |
16167 | mpz_clear(r); | |
16168 | mpz_clear(q); | |
16169 | return true; | |
16170 | } | |
16171 | ||
16172 | // Express floating point value as memory size if possible. | |
16173 | ||
16174 | bool | |
16175 | Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const | |
16176 | { | |
16177 | if (!mpfr_integer_p(fval)) | |
16178 | return false; | |
16179 | mpz_t ival; | |
16180 | mpz_init(ival); | |
16181 | mpfr_get_z(ival, fval, GMP_RNDN); | |
16182 | bool ret = this->mpz_to_memory_size(ival, val); | |
16183 | mpz_clear(ival); | |
16184 | return ret; | |
16185 | } | |
16186 | ||
0c77715b | 16187 | // Convert value to integer if possible. |
16188 | ||
16189 | bool | |
16190 | Numeric_constant::to_int(mpz_t* val) const | |
16191 | { | |
16192 | switch (this->classification_) | |
16193 | { | |
16194 | case NC_INT: | |
16195 | case NC_RUNE: | |
16196 | mpz_init_set(*val, this->u_.int_val); | |
16197 | return true; | |
16198 | case NC_FLOAT: | |
16199 | if (!mpfr_integer_p(this->u_.float_val)) | |
16200 | return false; | |
16201 | mpz_init(*val); | |
16202 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
16203 | return true; | |
16204 | case NC_COMPLEX: | |
fcbea5e4 | 16205 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)) |
16206 | || !mpfr_integer_p(mpc_realref(this->u_.complex_val))) | |
0c77715b | 16207 | return false; |
16208 | mpz_init(*val); | |
fcbea5e4 | 16209 | mpfr_get_z(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16210 | return true; |
16211 | default: | |
16212 | go_unreachable(); | |
16213 | } | |
16214 | } | |
16215 | ||
16216 | // Convert value to floating point if possible. | |
16217 | ||
16218 | bool | |
16219 | Numeric_constant::to_float(mpfr_t* val) const | |
16220 | { | |
16221 | switch (this->classification_) | |
16222 | { | |
16223 | case NC_INT: | |
16224 | case NC_RUNE: | |
16225 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
16226 | return true; | |
16227 | case NC_FLOAT: | |
16228 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
16229 | return true; | |
16230 | case NC_COMPLEX: | |
fcbea5e4 | 16231 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16232 | return false; |
fcbea5e4 | 16233 | mpfr_init_set(*val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16234 | return true; |
16235 | default: | |
16236 | go_unreachable(); | |
16237 | } | |
16238 | } | |
16239 | ||
16240 | // Convert value to complex. | |
16241 | ||
16242 | bool | |
fcbea5e4 | 16243 | Numeric_constant::to_complex(mpc_t* val) const |
0c77715b | 16244 | { |
fcbea5e4 | 16245 | mpc_init2(*val, mpc_precision); |
0c77715b | 16246 | switch (this->classification_) |
16247 | { | |
16248 | case NC_INT: | |
16249 | case NC_RUNE: | |
fcbea5e4 | 16250 | mpc_set_z(*val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16251 | return true; |
16252 | case NC_FLOAT: | |
fcbea5e4 | 16253 | mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16254 | return true; |
16255 | case NC_COMPLEX: | |
fcbea5e4 | 16256 | mpc_set(*val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16257 | return true; |
16258 | default: | |
16259 | go_unreachable(); | |
16260 | } | |
16261 | } | |
16262 | ||
16263 | // Get the type. | |
16264 | ||
16265 | Type* | |
16266 | Numeric_constant::type() const | |
16267 | { | |
16268 | if (this->type_ != NULL) | |
16269 | return this->type_; | |
16270 | switch (this->classification_) | |
16271 | { | |
16272 | case NC_INT: | |
16273 | return Type::make_abstract_integer_type(); | |
16274 | case NC_RUNE: | |
16275 | return Type::make_abstract_character_type(); | |
16276 | case NC_FLOAT: | |
16277 | return Type::make_abstract_float_type(); | |
16278 | case NC_COMPLEX: | |
16279 | return Type::make_abstract_complex_type(); | |
16280 | default: | |
16281 | go_unreachable(); | |
16282 | } | |
16283 | } | |
16284 | ||
16285 | // If the constant can be expressed in TYPE, then set the type of the | |
16286 | // constant to TYPE and return true. Otherwise return false, and, if | |
16287 | // ISSUE_ERROR is true, report an appropriate error message. | |
16288 | ||
16289 | bool | |
16290 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
16291 | { | |
16292 | bool ret; | |
f11c2155 | 16293 | if (type == NULL || type->is_error()) |
0c77715b | 16294 | ret = true; |
16295 | else if (type->integer_type() != NULL) | |
16296 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
16297 | else if (type->float_type() != NULL) | |
16298 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
16299 | else if (type->complex_type() != NULL) | |
16300 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
16301 | else | |
5706ab68 | 16302 | { |
16303 | ret = false; | |
16304 | if (issue_error) | |
16305 | go_assert(saw_errors()); | |
16306 | } | |
0c77715b | 16307 | if (ret) |
16308 | this->type_ = type; | |
16309 | return ret; | |
16310 | } | |
16311 | ||
16312 | // Check whether the constant can be expressed in an integer type. | |
16313 | ||
16314 | bool | |
16315 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
71a45216 | 16316 | Location location) |
0c77715b | 16317 | { |
16318 | mpz_t val; | |
16319 | switch (this->classification_) | |
16320 | { | |
16321 | case NC_INT: | |
16322 | case NC_RUNE: | |
16323 | mpz_init_set(val, this->u_.int_val); | |
16324 | break; | |
16325 | ||
16326 | case NC_FLOAT: | |
16327 | if (!mpfr_integer_p(this->u_.float_val)) | |
16328 | { | |
16329 | if (issue_error) | |
71a45216 | 16330 | { |
631d5788 | 16331 | go_error_at(location, |
16332 | "floating point constant truncated to integer"); | |
71a45216 | 16333 | this->set_invalid(); |
16334 | } | |
0c77715b | 16335 | return false; |
16336 | } | |
16337 | mpz_init(val); | |
16338 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
16339 | break; | |
16340 | ||
16341 | case NC_COMPLEX: | |
fcbea5e4 | 16342 | if (!mpfr_integer_p(mpc_realref(this->u_.complex_val)) |
16343 | || !mpfr_zero_p(mpc_imagref(this->u_.complex_val))) | |
0c77715b | 16344 | { |
16345 | if (issue_error) | |
71a45216 | 16346 | { |
631d5788 | 16347 | go_error_at(location, "complex constant truncated to integer"); |
71a45216 | 16348 | this->set_invalid(); |
16349 | } | |
0c77715b | 16350 | return false; |
16351 | } | |
16352 | mpz_init(val); | |
fcbea5e4 | 16353 | mpfr_get_z(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16354 | break; |
16355 | ||
16356 | default: | |
16357 | go_unreachable(); | |
16358 | } | |
16359 | ||
16360 | bool ret; | |
16361 | if (type->is_abstract()) | |
16362 | ret = true; | |
16363 | else | |
16364 | { | |
16365 | int bits = mpz_sizeinbase(val, 2); | |
16366 | if (type->is_unsigned()) | |
16367 | { | |
16368 | // For an unsigned type we can only accept a nonnegative | |
16369 | // number, and we must be able to represents at least BITS. | |
16370 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
16371 | } | |
16372 | else | |
16373 | { | |
16374 | // For a signed type we need an extra bit to indicate the | |
16375 | // sign. We have to handle the most negative integer | |
16376 | // specially. | |
16377 | ret = (bits + 1 <= type->bits() | |
16378 | || (bits <= type->bits() | |
16379 | && mpz_sgn(val) < 0 | |
16380 | && (mpz_scan1(val, 0) | |
16381 | == static_cast<unsigned long>(type->bits() - 1)) | |
16382 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
16383 | } | |
16384 | } | |
16385 | ||
16386 | if (!ret && issue_error) | |
71a45216 | 16387 | { |
631d5788 | 16388 | go_error_at(location, "integer constant overflow"); |
71a45216 | 16389 | this->set_invalid(); |
16390 | } | |
0c77715b | 16391 | |
16392 | return ret; | |
16393 | } | |
16394 | ||
16395 | // Check whether the constant can be expressed in a floating point | |
16396 | // type. | |
16397 | ||
16398 | bool | |
16399 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 16400 | Location location) |
0c77715b | 16401 | { |
16402 | mpfr_t val; | |
16403 | switch (this->classification_) | |
16404 | { | |
16405 | case NC_INT: | |
16406 | case NC_RUNE: | |
16407 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
16408 | break; | |
16409 | ||
16410 | case NC_FLOAT: | |
16411 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
16412 | break; | |
16413 | ||
16414 | case NC_COMPLEX: | |
fcbea5e4 | 16415 | if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))) |
0c77715b | 16416 | { |
16417 | if (issue_error) | |
71a45216 | 16418 | { |
16419 | this->set_invalid(); | |
631d5788 | 16420 | go_error_at(location, "complex constant truncated to float"); |
71a45216 | 16421 | } |
0c77715b | 16422 | return false; |
16423 | } | |
fcbea5e4 | 16424 | mpfr_init_set(val, mpc_realref(this->u_.complex_val), GMP_RNDN); |
0c77715b | 16425 | break; |
16426 | ||
16427 | default: | |
16428 | go_unreachable(); | |
16429 | } | |
16430 | ||
16431 | bool ret; | |
16432 | if (type->is_abstract()) | |
16433 | ret = true; | |
16434 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
16435 | { | |
16436 | // A NaN or Infinity always fits in the range of the type. | |
16437 | ret = true; | |
16438 | } | |
16439 | else | |
16440 | { | |
16441 | mp_exp_t exp = mpfr_get_exp(val); | |
16442 | mp_exp_t max_exp; | |
16443 | switch (type->bits()) | |
16444 | { | |
16445 | case 32: | |
16446 | max_exp = 128; | |
16447 | break; | |
16448 | case 64: | |
16449 | max_exp = 1024; | |
16450 | break; | |
16451 | default: | |
16452 | go_unreachable(); | |
16453 | } | |
16454 | ||
16455 | ret = exp <= max_exp; | |
d0bcce51 | 16456 | |
16457 | if (ret) | |
16458 | { | |
16459 | // Round the constant to the desired type. | |
16460 | mpfr_t t; | |
16461 | mpfr_init(t); | |
16462 | switch (type->bits()) | |
16463 | { | |
16464 | case 32: | |
16465 | mpfr_set_prec(t, 24); | |
16466 | break; | |
16467 | case 64: | |
16468 | mpfr_set_prec(t, 53); | |
16469 | break; | |
16470 | default: | |
16471 | go_unreachable(); | |
16472 | } | |
16473 | mpfr_set(t, val, GMP_RNDN); | |
16474 | mpfr_set(val, t, GMP_RNDN); | |
16475 | mpfr_clear(t); | |
16476 | ||
16477 | this->set_float(type, val); | |
16478 | } | |
0c77715b | 16479 | } |
16480 | ||
16481 | mpfr_clear(val); | |
16482 | ||
16483 | if (!ret && issue_error) | |
71a45216 | 16484 | { |
631d5788 | 16485 | go_error_at(location, "floating point constant overflow"); |
71a45216 | 16486 | this->set_invalid(); |
16487 | } | |
0c77715b | 16488 | |
16489 | return ret; | |
16490 | } | |
16491 | ||
16492 | // Check whether the constant can be expressed in a complex type. | |
16493 | ||
16494 | bool | |
16495 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 16496 | Location location) |
0c77715b | 16497 | { |
16498 | if (type->is_abstract()) | |
16499 | return true; | |
16500 | ||
16501 | mp_exp_t max_exp; | |
16502 | switch (type->bits()) | |
16503 | { | |
16504 | case 64: | |
16505 | max_exp = 128; | |
16506 | break; | |
16507 | case 128: | |
16508 | max_exp = 1024; | |
16509 | break; | |
16510 | default: | |
16511 | go_unreachable(); | |
16512 | } | |
16513 | ||
fcbea5e4 | 16514 | mpc_t val; |
16515 | mpc_init2(val, mpc_precision); | |
0c77715b | 16516 | switch (this->classification_) |
16517 | { | |
16518 | case NC_INT: | |
16519 | case NC_RUNE: | |
fcbea5e4 | 16520 | mpc_set_z(val, this->u_.int_val, MPC_RNDNN); |
0c77715b | 16521 | break; |
16522 | ||
16523 | case NC_FLOAT: | |
fcbea5e4 | 16524 | mpc_set_fr(val, this->u_.float_val, MPC_RNDNN); |
0c77715b | 16525 | break; |
16526 | ||
16527 | case NC_COMPLEX: | |
fcbea5e4 | 16528 | mpc_set(val, this->u_.complex_val, MPC_RNDNN); |
0c77715b | 16529 | break; |
16530 | ||
16531 | default: | |
16532 | go_unreachable(); | |
16533 | } | |
16534 | ||
d0bcce51 | 16535 | bool ret = true; |
fcbea5e4 | 16536 | if (!mpfr_nan_p(mpc_realref(val)) |
16537 | && !mpfr_inf_p(mpc_realref(val)) | |
16538 | && !mpfr_zero_p(mpc_realref(val)) | |
16539 | && mpfr_get_exp(mpc_realref(val)) > max_exp) | |
d0bcce51 | 16540 | { |
16541 | if (issue_error) | |
71a45216 | 16542 | { |
631d5788 | 16543 | go_error_at(location, "complex real part overflow"); |
71a45216 | 16544 | this->set_invalid(); |
16545 | } | |
d0bcce51 | 16546 | ret = false; |
16547 | } | |
0c77715b | 16548 | |
fcbea5e4 | 16549 | if (!mpfr_nan_p(mpc_imagref(val)) |
16550 | && !mpfr_inf_p(mpc_imagref(val)) | |
16551 | && !mpfr_zero_p(mpc_imagref(val)) | |
16552 | && mpfr_get_exp(mpc_imagref(val)) > max_exp) | |
d0bcce51 | 16553 | { |
16554 | if (issue_error) | |
71a45216 | 16555 | { |
631d5788 | 16556 | go_error_at(location, "complex imaginary part overflow"); |
71a45216 | 16557 | this->set_invalid(); |
16558 | } | |
d0bcce51 | 16559 | ret = false; |
16560 | } | |
0c77715b | 16561 | |
d0bcce51 | 16562 | if (ret) |
16563 | { | |
16564 | // Round the constant to the desired type. | |
fcbea5e4 | 16565 | mpc_t t; |
d0bcce51 | 16566 | switch (type->bits()) |
16567 | { | |
16568 | case 64: | |
fcbea5e4 | 16569 | mpc_init2(t, 24); |
d0bcce51 | 16570 | break; |
16571 | case 128: | |
fcbea5e4 | 16572 | mpc_init2(t, 53); |
d0bcce51 | 16573 | break; |
16574 | default: | |
16575 | go_unreachable(); | |
16576 | } | |
fcbea5e4 | 16577 | mpc_set(t, val, MPC_RNDNN); |
16578 | mpc_set(val, t, MPC_RNDNN); | |
16579 | mpc_clear(t); | |
d0bcce51 | 16580 | |
fcbea5e4 | 16581 | this->set_complex(type, val); |
d0bcce51 | 16582 | } |
16583 | ||
fcbea5e4 | 16584 | mpc_clear(val); |
0c77715b | 16585 | |
16586 | return ret; | |
16587 | } | |
16588 | ||
16589 | // Return an Expression for this value. | |
16590 | ||
16591 | Expression* | |
16592 | Numeric_constant::expression(Location loc) const | |
16593 | { | |
16594 | switch (this->classification_) | |
16595 | { | |
16596 | case NC_INT: | |
e67508fa | 16597 | return Expression::make_integer_z(&this->u_.int_val, this->type_, loc); |
0c77715b | 16598 | case NC_RUNE: |
16599 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
16600 | case NC_FLOAT: | |
16601 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
16602 | case NC_COMPLEX: | |
fcbea5e4 | 16603 | return Expression::make_complex(&this->u_.complex_val, this->type_, loc); |
71a45216 | 16604 | case NC_INVALID: |
16605 | go_assert(saw_errors()); | |
16606 | return Expression::make_error(loc); | |
0c77715b | 16607 | default: |
16608 | go_unreachable(); | |
16609 | } | |
16610 | } |