]>
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 "toplev.h" |
12 | #include "intl.h" | |
13 | #include "tree.h" | |
9ed99284 | 14 | #include "stringpool.h" |
15 | #include "stor-layout.h" | |
764505e6 | 16 | #include "gimple-expr.h" |
e440a328 | 17 | #include "tree-iterator.h" |
18 | #include "convert.h" | |
19 | #include "real.h" | |
20 | #include "realmpfr.h" | |
e440a328 | 21 | |
e440a328 | 22 | #include "go-c.h" |
23 | #include "gogo.h" | |
24 | #include "types.h" | |
25 | #include "export.h" | |
26 | #include "import.h" | |
27 | #include "statements.h" | |
28 | #include "lex.h" | |
a9182619 | 29 | #include "runtime.h" |
6e193e6f | 30 | #include "backend.h" |
e440a328 | 31 | #include "expressions.h" |
d751bb78 | 32 | #include "ast-dump.h" |
e440a328 | 33 | |
34 | // Class Expression. | |
35 | ||
36 | Expression::Expression(Expression_classification classification, | |
b13c66cd | 37 | Location location) |
e440a328 | 38 | : classification_(classification), location_(location) |
39 | { | |
40 | } | |
41 | ||
42 | Expression::~Expression() | |
43 | { | |
44 | } | |
45 | ||
e440a328 | 46 | // Traverse the expressions. |
47 | ||
48 | int | |
49 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
50 | { | |
51 | Expression* expr = *pexpr; | |
52 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
53 | { | |
54 | int t = traverse->expression(pexpr); | |
55 | if (t == TRAVERSE_EXIT) | |
56 | return TRAVERSE_EXIT; | |
57 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
58 | return TRAVERSE_CONTINUE; | |
59 | } | |
60 | return expr->do_traverse(traverse); | |
61 | } | |
62 | ||
63 | // Traverse subexpressions of this expression. | |
64 | ||
65 | int | |
66 | Expression::traverse_subexpressions(Traverse* traverse) | |
67 | { | |
68 | return this->do_traverse(traverse); | |
69 | } | |
70 | ||
71 | // Default implementation for do_traverse for child classes. | |
72 | ||
73 | int | |
74 | Expression::do_traverse(Traverse*) | |
75 | { | |
76 | return TRAVERSE_CONTINUE; | |
77 | } | |
78 | ||
79 | // This virtual function is called by the parser if the value of this | |
a7549a6a | 80 | // expression is being discarded. By default, we give an error. |
81 | // Expressions with side effects override. | |
e440a328 | 82 | |
4f2138d7 | 83 | bool |
e440a328 | 84 | Expression::do_discarding_value() |
85 | { | |
a7549a6a | 86 | this->unused_value_error(); |
4f2138d7 | 87 | return false; |
e440a328 | 88 | } |
89 | ||
90 | // This virtual function is called to export expressions. This will | |
91 | // only be used by expressions which may be constant. | |
92 | ||
93 | void | |
94 | Expression::do_export(Export*) const | |
95 | { | |
c3e6f413 | 96 | go_unreachable(); |
e440a328 | 97 | } |
98 | ||
a7549a6a | 99 | // Give an error saying that the value of the expression is not used. |
e440a328 | 100 | |
101 | void | |
a7549a6a | 102 | Expression::unused_value_error() |
e440a328 | 103 | { |
4f2138d7 | 104 | this->report_error(_("value computed is not used")); |
e440a328 | 105 | } |
106 | ||
107 | // Note that this expression is an error. This is called by children | |
108 | // when they discover an error. | |
109 | ||
110 | void | |
111 | Expression::set_is_error() | |
112 | { | |
113 | this->classification_ = EXPRESSION_ERROR; | |
114 | } | |
115 | ||
116 | // For children to call to report an error conveniently. | |
117 | ||
118 | void | |
119 | Expression::report_error(const char* msg) | |
120 | { | |
121 | error_at(this->location_, "%s", msg); | |
122 | this->set_is_error(); | |
123 | } | |
124 | ||
125 | // Set types of variables and constants. This is implemented by the | |
126 | // child class. | |
127 | ||
128 | void | |
129 | Expression::determine_type(const Type_context* context) | |
130 | { | |
131 | this->do_determine_type(context); | |
132 | } | |
133 | ||
134 | // Set types when there is no context. | |
135 | ||
136 | void | |
137 | Expression::determine_type_no_context() | |
138 | { | |
139 | Type_context context; | |
140 | this->do_determine_type(&context); | |
141 | } | |
142 | ||
2c809f8f | 143 | // Return an expression handling any conversions which must be done during |
e440a328 | 144 | // assignment. |
145 | ||
2c809f8f | 146 | Expression* |
147 | Expression::convert_for_assignment(Gogo* gogo, Type* lhs_type, | |
148 | Expression* rhs, Location location) | |
e440a328 | 149 | { |
2c809f8f | 150 | Type* rhs_type = rhs->type(); |
151 | if (lhs_type->is_error() | |
152 | || rhs_type->is_error() | |
153 | || rhs->is_error_expression()) | |
154 | return Expression::make_error(location); | |
e440a328 | 155 | |
54211955 | 156 | if (lhs_type->forwarded() != rhs_type->forwarded() |
157 | && lhs_type->interface_type() != NULL) | |
e440a328 | 158 | { |
159 | if (rhs_type->interface_type() == NULL) | |
2c809f8f | 160 | return Expression::convert_type_to_interface(lhs_type, rhs, location); |
e440a328 | 161 | else |
2c809f8f | 162 | return Expression::convert_interface_to_interface(lhs_type, rhs, false, |
163 | location); | |
e440a328 | 164 | } |
54211955 | 165 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
166 | && rhs_type->interface_type() != NULL) | |
2c809f8f | 167 | return Expression::convert_interface_to_type(lhs_type, rhs, location); |
411eb89e | 168 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 169 | { |
2c809f8f | 170 | // Assigning nil to a slice. |
171 | mpz_t zval; | |
172 | mpz_init_set_ui(zval, 0UL); | |
173 | Expression* zero = Expression::make_integer(&zval, NULL, location); | |
174 | mpz_clear(zval); | |
175 | Expression* nil = Expression::make_nil(location); | |
176 | return Expression::make_slice_value(lhs_type, nil, zero, zero, location); | |
e440a328 | 177 | } |
178 | else if (rhs_type->is_nil_type()) | |
2c809f8f | 179 | return Expression::make_nil(location); |
180 | else if (Type::are_identical(lhs_type, rhs_type, false, NULL)) | |
e440a328 | 181 | { |
182 | // No conversion is needed. | |
2c809f8f | 183 | return rhs; |
184 | } | |
185 | else if (lhs_type->points_to() != NULL) | |
186 | return Expression::make_unsafe_cast(lhs_type, rhs, location); | |
187 | else if (lhs_type->is_numeric_type()) | |
188 | return Expression::make_cast(lhs_type, rhs, location); | |
189 | else if ((lhs_type->struct_type() != NULL | |
190 | && rhs_type->struct_type() != NULL) | |
191 | || (lhs_type->array_type() != NULL | |
192 | && rhs_type->array_type() != NULL)) | |
e440a328 | 193 | { |
bb92f513 | 194 | // Avoid confusion from zero sized variables which may be |
195 | // represented as non-zero-sized. | |
2c809f8f | 196 | // TODO(cmang): This check is for a GCC-specific issue, and should be |
197 | // removed from the frontend. FIXME. | |
198 | size_t lhs_size = gogo->backend()->type_size(lhs_type->get_backend(gogo)); | |
199 | size_t rhs_size = gogo->backend()->type_size(rhs_type->get_backend(gogo)); | |
200 | if (rhs_size == 0 || lhs_size == 0) | |
201 | return rhs; | |
bb92f513 | 202 | |
e440a328 | 203 | // This conversion must be permitted by Go, or we wouldn't have |
204 | // gotten here. | |
2c809f8f | 205 | return Expression::make_unsafe_cast(lhs_type, rhs, location); |
e440a328 | 206 | } |
207 | else | |
2c809f8f | 208 | return rhs; |
e440a328 | 209 | } |
210 | ||
2c809f8f | 211 | // Return an expression for a conversion from a non-interface type to an |
e440a328 | 212 | // interface type. |
213 | ||
2c809f8f | 214 | Expression* |
215 | Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs, | |
216 | Location location) | |
e440a328 | 217 | { |
e440a328 | 218 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
219 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
220 | ||
221 | // Since RHS_TYPE is a static type, we can create the interface | |
222 | // method table at compile time. | |
223 | ||
224 | // When setting an interface to nil, we just set both fields to | |
225 | // NULL. | |
2c809f8f | 226 | Type* rhs_type = rhs->type(); |
e440a328 | 227 | if (rhs_type->is_nil_type()) |
63697958 | 228 | { |
2c809f8f | 229 | Expression* nil = Expression::make_nil(location); |
230 | return Expression::make_interface_value(lhs_type, nil, nil, location); | |
63697958 | 231 | } |
e440a328 | 232 | |
233 | // This should have been checked already. | |
c484d925 | 234 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 235 | |
e440a328 | 236 | // An interface is a tuple. If LHS_TYPE is an empty interface type, |
237 | // then the first field is the type descriptor for RHS_TYPE. | |
238 | // Otherwise it is the interface method table for RHS_TYPE. | |
2c809f8f | 239 | Expression* first_field; |
e440a328 | 240 | if (lhs_is_empty) |
2c809f8f | 241 | first_field = Expression::make_type_descriptor(rhs_type, location); |
e440a328 | 242 | else |
243 | { | |
244 | // Build the interface method table for this interface and this | |
245 | // object type: a list of function pointers for each interface | |
246 | // method. | |
247 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 248 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 249 | bool is_pointer = false; |
c0cab2ec | 250 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 251 | { |
252 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 253 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 254 | is_pointer = true; |
255 | } | |
c0cab2ec | 256 | if (rhs_named_type != NULL) |
2c809f8f | 257 | first_field = |
258 | rhs_named_type->interface_method_table(lhs_interface_type, | |
259 | is_pointer); | |
c0cab2ec | 260 | else if (rhs_struct_type != NULL) |
2c809f8f | 261 | first_field = |
262 | rhs_struct_type->interface_method_table(lhs_interface_type, | |
263 | is_pointer); | |
c0cab2ec | 264 | else |
2c809f8f | 265 | first_field = Expression::make_nil(location); |
e440a328 | 266 | } |
e440a328 | 267 | |
2c809f8f | 268 | Expression* obj; |
e440a328 | 269 | if (rhs_type->points_to() != NULL) |
270 | { | |
2c809f8f | 271 | // We are assigning a pointer to the interface; the interface |
e440a328 | 272 | // holds the pointer itself. |
2c809f8f | 273 | obj = rhs; |
274 | } | |
275 | else | |
276 | { | |
277 | // We are assigning a non-pointer value to the interface; the | |
278 | // interface gets a copy of the value in the heap. | |
279 | obj = Expression::make_heap_expression(rhs, location); | |
e440a328 | 280 | } |
281 | ||
2c809f8f | 282 | return Expression::make_interface_value(lhs_type, first_field, obj, location); |
283 | } | |
e440a328 | 284 | |
2c809f8f | 285 | // Return an expression for the type descriptor of RHS. |
e440a328 | 286 | |
2c809f8f | 287 | Expression* |
288 | Expression::get_interface_type_descriptor(Expression* rhs) | |
289 | { | |
290 | go_assert(rhs->type()->interface_type() != NULL); | |
291 | Location location = rhs->location(); | |
e440a328 | 292 | |
2c809f8f | 293 | // The type descriptor is the first field of an empty interface. |
294 | if (rhs->type()->interface_type()->is_empty()) | |
295 | return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR, | |
296 | location); | |
297 | ||
298 | Expression* mtable = | |
299 | Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location); | |
e440a328 | 300 | |
2c809f8f | 301 | Expression* descriptor = |
302 | Expression::make_unary(OPERATOR_MULT, mtable, location); | |
303 | descriptor = Expression::make_field_reference(descriptor, 0, location); | |
304 | Expression* nil = Expression::make_nil(location); | |
e440a328 | 305 | |
2c809f8f | 306 | Expression* eq = |
307 | Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location); | |
308 | return Expression::make_conditional(eq, nil, descriptor, location); | |
e440a328 | 309 | } |
310 | ||
2c809f8f | 311 | // Return an expression for the conversion of an interface type to an |
e440a328 | 312 | // interface type. |
313 | ||
2c809f8f | 314 | Expression* |
315 | Expression::convert_interface_to_interface(Type *lhs_type, Expression* rhs, | |
316 | bool for_type_guard, | |
317 | Location location) | |
e440a328 | 318 | { |
e440a328 | 319 | Interface_type* lhs_interface_type = lhs_type->interface_type(); |
320 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
321 | ||
e440a328 | 322 | // In the general case this requires runtime examination of the type |
323 | // method table to match it up with the interface methods. | |
324 | ||
325 | // FIXME: If all of the methods in the right hand side interface | |
326 | // also appear in the left hand side interface, then we don't need | |
327 | // to do a runtime check, although we still need to build a new | |
328 | // method table. | |
329 | ||
330 | // Get the type descriptor for the right hand side. This will be | |
331 | // NULL for a nil interface. | |
2c809f8f | 332 | Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs); |
333 | Expression* lhs_type_expr = | |
334 | Expression::make_type_descriptor(lhs_type, location); | |
e440a328 | 335 | |
2c809f8f | 336 | Expression* first_field; |
e440a328 | 337 | if (for_type_guard) |
338 | { | |
339 | // A type assertion fails when converting a nil interface. | |
2c809f8f | 340 | first_field = |
341 | Runtime::make_call(Runtime::ASSERT_INTERFACE, location, 2, | |
342 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 343 | } |
344 | else if (lhs_is_empty) | |
345 | { | |
2c809f8f | 346 | // A conversion to an empty interface always succeeds, and the |
e440a328 | 347 | // first field is just the type descriptor of the object. |
2c809f8f | 348 | first_field = rhs_type_expr; |
e440a328 | 349 | } |
350 | else | |
351 | { | |
352 | // A conversion to a non-empty interface may fail, but unlike a | |
353 | // type assertion converting nil will always succeed. | |
2c809f8f | 354 | first_field = |
355 | Runtime::make_call(Runtime::CONVERT_INTERFACE, location, 2, | |
356 | lhs_type_expr, rhs_type_expr); | |
e440a328 | 357 | } |
358 | ||
359 | // The second field is simply the object pointer. | |
2c809f8f | 360 | Expression* obj = |
361 | Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location); | |
362 | return Expression::make_interface_value(lhs_type, first_field, obj, location); | |
e440a328 | 363 | } |
364 | ||
2c809f8f | 365 | // Return an expression for the conversion of an interface type to a |
e440a328 | 366 | // non-interface type. |
367 | ||
2c809f8f | 368 | Expression* |
369 | Expression::convert_interface_to_type(Type *lhs_type, Expression* rhs, | |
370 | Location location) | |
e440a328 | 371 | { |
e440a328 | 372 | // Call a function to check that the type is valid. The function |
373 | // will panic with an appropriate runtime type error if the type is | |
374 | // not valid. | |
2c809f8f | 375 | Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type, |
376 | location); | |
377 | Expression* rhs_descriptor = | |
378 | Expression::get_interface_type_descriptor(rhs); | |
379 | ||
380 | Type* rhs_type = rhs->type(); | |
381 | Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type, | |
382 | location); | |
383 | ||
384 | Expression* check_iface = Runtime::make_call(Runtime::CHECK_INTERFACE_TYPE, | |
385 | location, 3, lhs_type_expr, | |
386 | rhs_descriptor, rhs_inter_expr); | |
e440a328 | 387 | |
388 | // If the call succeeds, pull out the value. | |
2c809f8f | 389 | Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, |
390 | location); | |
e440a328 | 391 | |
392 | // If the value is a pointer, then it is the value we want. | |
393 | // Otherwise it points to the value. | |
394 | if (lhs_type->points_to() == NULL) | |
395 | { | |
2c809f8f | 396 | obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj, |
397 | location); | |
398 | obj = Expression::make_unary(OPERATOR_MULT, obj, location); | |
e440a328 | 399 | } |
2c809f8f | 400 | return Expression::make_compound(check_iface, obj, location); |
e440a328 | 401 | } |
402 | ||
403 | // Convert an expression to a tree. This is implemented by the child | |
404 | // class. Not that it is not in general safe to call this multiple | |
405 | // times for a single expression, but that we don't catch such errors. | |
406 | ||
407 | tree | |
408 | Expression::get_tree(Translate_context* context) | |
409 | { | |
410 | // The child may have marked this expression as having an error. | |
411 | if (this->classification_ == EXPRESSION_ERROR) | |
412 | return error_mark_node; | |
413 | ||
414 | return this->do_get_tree(context); | |
415 | } | |
416 | ||
48c2a53a | 417 | // Return a backend expression for VAL. |
418 | Bexpression* | |
419 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
420 | Numeric_constant* val) | |
e440a328 | 421 | { |
48c2a53a | 422 | Gogo* gogo = context->gogo(); |
423 | Type* type = val->type(); | |
424 | if (type == NULL) | |
425 | return gogo->backend()->error_expression(); | |
e440a328 | 426 | |
48c2a53a | 427 | Btype* btype = type->get_backend(gogo); |
428 | Bexpression* ret; | |
429 | if (type->integer_type() != NULL) | |
e440a328 | 430 | { |
431 | mpz_t ival; | |
48c2a53a | 432 | if (!val->to_int(&ival)) |
433 | { | |
434 | go_assert(saw_errors()); | |
435 | return gogo->backend()->error_expression(); | |
436 | } | |
437 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 438 | mpz_clear(ival); |
e440a328 | 439 | } |
48c2a53a | 440 | else if (type->float_type() != NULL) |
e440a328 | 441 | { |
48c2a53a | 442 | mpfr_t fval; |
443 | if (!val->to_float(&fval)) | |
444 | { | |
445 | go_assert(saw_errors()); | |
446 | return gogo->backend()->error_expression(); | |
447 | } | |
448 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
449 | mpfr_clear(fval); | |
e440a328 | 450 | } |
48c2a53a | 451 | else if (type->complex_type() != NULL) |
e440a328 | 452 | { |
48c2a53a | 453 | mpfr_t real; |
454 | mpfr_t imag; | |
455 | if (!val->to_complex(&real, &imag)) | |
456 | { | |
457 | go_assert(saw_errors()); | |
458 | return gogo->backend()->error_expression(); | |
459 | } | |
460 | ret = gogo->backend()->complex_constant_expression(btype, real, imag); | |
461 | mpfr_clear(real); | |
462 | mpfr_clear(imag); | |
e440a328 | 463 | } |
464 | else | |
c3e6f413 | 465 | go_unreachable(); |
e440a328 | 466 | |
48c2a53a | 467 | return ret; |
e440a328 | 468 | } |
469 | ||
2c809f8f | 470 | // Return an expression which evaluates to true if VAL, of arbitrary integer |
471 | // type, is negative or is more than the maximum value of the Go type "int". | |
e440a328 | 472 | |
2c809f8f | 473 | Expression* |
474 | Expression::check_bounds(Expression* val, Location loc) | |
e440a328 | 475 | { |
2c809f8f | 476 | Type* val_type = val->type(); |
477 | Type* bound_type = Type::lookup_integer_type("int"); | |
478 | ||
479 | int val_type_size; | |
480 | bool val_is_unsigned = false; | |
481 | if (val_type->integer_type() != NULL) | |
482 | { | |
483 | val_type_size = val_type->integer_type()->bits(); | |
484 | val_is_unsigned = val_type->integer_type()->is_unsigned(); | |
485 | } | |
486 | else | |
487 | { | |
488 | if (!val_type->is_numeric_type() | |
489 | || !Type::are_convertible(bound_type, val_type, NULL)) | |
490 | { | |
491 | go_assert(saw_errors()); | |
492 | return Expression::make_boolean(true, loc); | |
493 | } | |
e440a328 | 494 | |
2c809f8f | 495 | if (val_type->complex_type() != NULL) |
496 | val_type_size = val_type->complex_type()->bits(); | |
497 | else | |
498 | val_type_size = val_type->float_type()->bits(); | |
499 | } | |
500 | ||
501 | Expression* negative_index = Expression::make_boolean(false, loc); | |
502 | Expression* index_overflows = Expression::make_boolean(false, loc); | |
503 | if (!val_is_unsigned) | |
e440a328 | 504 | { |
2c809f8f | 505 | mpz_t zval; |
506 | mpz_init_set_ui(zval, 0UL); | |
507 | Expression* zero = Expression::make_integer(&zval, val_type, loc); | |
508 | mpz_clear(zval); | |
509 | ||
510 | negative_index = Expression::make_binary(OPERATOR_LT, val, zero, loc); | |
e440a328 | 511 | } |
512 | ||
2c809f8f | 513 | int bound_type_size = bound_type->integer_type()->bits(); |
c3068ac0 | 514 | if (val_type_size > bound_type_size |
515 | || (val_type_size == bound_type_size | |
2c809f8f | 516 | && val_is_unsigned)) |
517 | { | |
518 | mpz_t one; | |
519 | mpz_init_set_ui(one, 1UL); | |
520 | ||
521 | // maxval = 2^(bound_type_size - 1) - 1 | |
522 | mpz_t maxval; | |
523 | mpz_init(maxval); | |
524 | mpz_mul_2exp(maxval, one, bound_type_size - 1); | |
525 | mpz_sub_ui(maxval, maxval, 1); | |
526 | Expression* max = Expression::make_integer(&maxval, val_type, loc); | |
527 | mpz_clear(one); | |
528 | mpz_clear(maxval); | |
529 | ||
530 | index_overflows = Expression::make_binary(OPERATOR_GT, val, max, loc); | |
e440a328 | 531 | } |
532 | ||
2c809f8f | 533 | return Expression::make_binary(OPERATOR_OROR, negative_index, index_overflows, |
534 | loc); | |
e440a328 | 535 | } |
536 | ||
d751bb78 | 537 | void |
538 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
539 | { | |
540 | this->do_dump_expression(ast_dump_context); | |
541 | } | |
542 | ||
e440a328 | 543 | // Error expressions. This are used to avoid cascading errors. |
544 | ||
545 | class Error_expression : public Expression | |
546 | { | |
547 | public: | |
b13c66cd | 548 | Error_expression(Location location) |
e440a328 | 549 | : Expression(EXPRESSION_ERROR, location) |
550 | { } | |
551 | ||
552 | protected: | |
553 | bool | |
554 | do_is_constant() const | |
555 | { return true; } | |
556 | ||
0e168074 | 557 | bool |
558 | do_is_immutable() const | |
559 | { return true; } | |
560 | ||
e440a328 | 561 | bool |
0c77715b | 562 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 563 | { |
0c77715b | 564 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 565 | return true; |
566 | } | |
567 | ||
4f2138d7 | 568 | bool |
e440a328 | 569 | do_discarding_value() |
4f2138d7 | 570 | { return true; } |
e440a328 | 571 | |
572 | Type* | |
573 | do_type() | |
574 | { return Type::make_error_type(); } | |
575 | ||
576 | void | |
577 | do_determine_type(const Type_context*) | |
578 | { } | |
579 | ||
580 | Expression* | |
581 | do_copy() | |
582 | { return this; } | |
583 | ||
584 | bool | |
585 | do_is_addressable() const | |
586 | { return true; } | |
587 | ||
588 | tree | |
589 | do_get_tree(Translate_context*) | |
590 | { return error_mark_node; } | |
d751bb78 | 591 | |
592 | void | |
593 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 594 | }; |
595 | ||
d751bb78 | 596 | // Dump the ast representation for an error expression to a dump context. |
597 | ||
598 | void | |
599 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
600 | { | |
601 | ast_dump_context->ostream() << "_Error_" ; | |
602 | } | |
603 | ||
e440a328 | 604 | Expression* |
b13c66cd | 605 | Expression::make_error(Location location) |
e440a328 | 606 | { |
607 | return new Error_expression(location); | |
608 | } | |
609 | ||
610 | // An expression which is really a type. This is used during parsing. | |
611 | // It is an error if these survive after lowering. | |
612 | ||
613 | class | |
614 | Type_expression : public Expression | |
615 | { | |
616 | public: | |
b13c66cd | 617 | Type_expression(Type* type, Location location) |
e440a328 | 618 | : Expression(EXPRESSION_TYPE, location), |
619 | type_(type) | |
620 | { } | |
621 | ||
622 | protected: | |
623 | int | |
624 | do_traverse(Traverse* traverse) | |
625 | { return Type::traverse(this->type_, traverse); } | |
626 | ||
627 | Type* | |
628 | do_type() | |
629 | { return this->type_; } | |
630 | ||
631 | void | |
632 | do_determine_type(const Type_context*) | |
633 | { } | |
634 | ||
635 | void | |
636 | do_check_types(Gogo*) | |
637 | { this->report_error(_("invalid use of type")); } | |
638 | ||
639 | Expression* | |
640 | do_copy() | |
641 | { return this; } | |
642 | ||
643 | tree | |
644 | do_get_tree(Translate_context*) | |
c3e6f413 | 645 | { go_unreachable(); } |
e440a328 | 646 | |
d751bb78 | 647 | void do_dump_expression(Ast_dump_context*) const; |
648 | ||
e440a328 | 649 | private: |
650 | // The type which we are representing as an expression. | |
651 | Type* type_; | |
652 | }; | |
653 | ||
d751bb78 | 654 | void |
655 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
656 | { | |
657 | ast_dump_context->dump_type(this->type_); | |
658 | } | |
659 | ||
e440a328 | 660 | Expression* |
b13c66cd | 661 | Expression::make_type(Type* type, Location location) |
e440a328 | 662 | { |
663 | return new Type_expression(type, location); | |
664 | } | |
665 | ||
e03bdf36 | 666 | // Class Parser_expression. |
667 | ||
668 | Type* | |
669 | Parser_expression::do_type() | |
670 | { | |
671 | // We should never really ask for the type of a Parser_expression. | |
672 | // However, it can happen, at least when we have an invalid const | |
673 | // whose initializer refers to the const itself. In that case we | |
674 | // may ask for the type when lowering the const itself. | |
c484d925 | 675 | go_assert(saw_errors()); |
e03bdf36 | 676 | return Type::make_error_type(); |
677 | } | |
678 | ||
e440a328 | 679 | // Class Var_expression. |
680 | ||
681 | // Lower a variable expression. Here we just make sure that the | |
682 | // initialization expression of the variable has been lowered. This | |
683 | // ensures that we will be able to determine the type of the variable | |
684 | // if necessary. | |
685 | ||
686 | Expression* | |
ceeb4318 | 687 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
688 | Statement_inserter* inserter, int) | |
e440a328 | 689 | { |
690 | if (this->variable_->is_variable()) | |
691 | { | |
692 | Variable* var = this->variable_->var_value(); | |
693 | // This is either a local variable or a global variable. A | |
694 | // reference to a variable which is local to an enclosing | |
695 | // function will be a reference to a field in a closure. | |
696 | if (var->is_global()) | |
ceeb4318 | 697 | { |
698 | function = NULL; | |
699 | inserter = NULL; | |
700 | } | |
701 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 702 | } |
703 | return this; | |
704 | } | |
705 | ||
e440a328 | 706 | // Return the type of a reference to a variable. |
707 | ||
708 | Type* | |
709 | Var_expression::do_type() | |
710 | { | |
711 | if (this->variable_->is_variable()) | |
712 | return this->variable_->var_value()->type(); | |
713 | else if (this->variable_->is_result_variable()) | |
714 | return this->variable_->result_var_value()->type(); | |
715 | else | |
c3e6f413 | 716 | go_unreachable(); |
e440a328 | 717 | } |
718 | ||
0ab09e06 | 719 | // Determine the type of a reference to a variable. |
720 | ||
721 | void | |
722 | Var_expression::do_determine_type(const Type_context*) | |
723 | { | |
724 | if (this->variable_->is_variable()) | |
725 | this->variable_->var_value()->determine_type(); | |
726 | } | |
727 | ||
e440a328 | 728 | // Something takes the address of this variable. This means that we |
729 | // may want to move the variable onto the heap. | |
730 | ||
731 | void | |
732 | Var_expression::do_address_taken(bool escapes) | |
733 | { | |
734 | if (!escapes) | |
f325319b | 735 | { |
736 | if (this->variable_->is_variable()) | |
737 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
738 | else if (this->variable_->is_result_variable()) | |
739 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
740 | else | |
741 | go_unreachable(); | |
742 | } | |
e440a328 | 743 | else |
f325319b | 744 | { |
745 | if (this->variable_->is_variable()) | |
746 | this->variable_->var_value()->set_address_taken(); | |
747 | else if (this->variable_->is_result_variable()) | |
748 | this->variable_->result_var_value()->set_address_taken(); | |
749 | else | |
750 | go_unreachable(); | |
751 | } | |
e440a328 | 752 | } |
753 | ||
754 | // Get the tree for a reference to a variable. | |
755 | ||
756 | tree | |
757 | Var_expression::do_get_tree(Translate_context* context) | |
758 | { | |
fe2f84cf | 759 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
760 | context->function()); | |
fe2f84cf | 761 | bool is_in_heap; |
c6777780 | 762 | Location loc = this->location(); |
9b27b43c | 763 | Btype* btype; |
764 | Gogo* gogo = context->gogo(); | |
fe2f84cf | 765 | if (this->variable_->is_variable()) |
9b27b43c | 766 | { |
767 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
768 | btype = this->variable_->var_value()->type()->get_backend(gogo); | |
769 | } | |
fe2f84cf | 770 | else if (this->variable_->is_result_variable()) |
9b27b43c | 771 | { |
772 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
773 | btype = this->variable_->result_var_value()->type()->get_backend(gogo); | |
774 | } | |
fe2f84cf | 775 | else |
c3e6f413 | 776 | go_unreachable(); |
c6777780 | 777 | |
778 | Bexpression* ret = context->backend()->var_expression(bvar, loc); | |
fe2f84cf | 779 | if (is_in_heap) |
9b27b43c | 780 | ret = context->backend()->indirect_expression(btype, ret, true, loc); |
c6777780 | 781 | return expr_to_tree(ret); |
e440a328 | 782 | } |
783 | ||
d751bb78 | 784 | // Ast dump for variable expression. |
785 | ||
786 | void | |
787 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
788 | { | |
789 | ast_dump_context->ostream() << this->variable_->name() ; | |
790 | } | |
791 | ||
e440a328 | 792 | // Make a reference to a variable in an expression. |
793 | ||
794 | Expression* | |
b13c66cd | 795 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 796 | { |
797 | if (var->is_sink()) | |
798 | return Expression::make_sink(location); | |
799 | ||
800 | // FIXME: Creating a new object for each reference to a variable is | |
801 | // wasteful. | |
802 | return new Var_expression(var, location); | |
803 | } | |
804 | ||
805 | // Class Temporary_reference_expression. | |
806 | ||
807 | // The type. | |
808 | ||
809 | Type* | |
810 | Temporary_reference_expression::do_type() | |
811 | { | |
812 | return this->statement_->type(); | |
813 | } | |
814 | ||
815 | // Called if something takes the address of this temporary variable. | |
816 | // We never have to move temporary variables to the heap, but we do | |
817 | // need to know that they must live in the stack rather than in a | |
818 | // register. | |
819 | ||
820 | void | |
821 | Temporary_reference_expression::do_address_taken(bool) | |
822 | { | |
823 | this->statement_->set_is_address_taken(); | |
824 | } | |
825 | ||
826 | // Get a tree referring to the variable. | |
827 | ||
828 | tree | |
eefc1ed3 | 829 | Temporary_reference_expression::do_get_tree(Translate_context* context) |
e440a328 | 830 | { |
cd440cff | 831 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 832 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
cd440cff | 833 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 834 | |
cd440cff | 835 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 836 | // that the Go frontend can. In some cases this means that a |
837 | // temporary variable won't have the right backend type. Correct | |
838 | // that here by adding a type cast. We need to use base() to push | |
839 | // the circularity down one level. | |
cd440cff | 840 | Type* stype = this->statement_->type(); |
ceeb4318 | 841 | if (!this->is_lvalue_ |
cd440cff | 842 | && stype->has_pointer() |
843 | && stype->deref()->is_void_type()) | |
eefc1ed3 | 844 | { |
cd440cff | 845 | Btype* btype = this->type()->base()->get_backend(gogo); |
846 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 847 | } |
cd440cff | 848 | return expr_to_tree(ret); |
e440a328 | 849 | } |
850 | ||
d751bb78 | 851 | // Ast dump for temporary reference. |
852 | ||
853 | void | |
854 | Temporary_reference_expression::do_dump_expression( | |
855 | Ast_dump_context* ast_dump_context) const | |
856 | { | |
857 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
858 | } | |
859 | ||
e440a328 | 860 | // Make a reference to a temporary variable. |
861 | ||
ceeb4318 | 862 | Temporary_reference_expression* |
e440a328 | 863 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 864 | Location location) |
e440a328 | 865 | { |
866 | return new Temporary_reference_expression(statement, location); | |
867 | } | |
868 | ||
e9d3367e | 869 | // Class Set_and_use_temporary_expression. |
870 | ||
871 | // Return the type. | |
872 | ||
873 | Type* | |
874 | Set_and_use_temporary_expression::do_type() | |
875 | { | |
876 | return this->statement_->type(); | |
877 | } | |
878 | ||
0afbb937 | 879 | // Determine the type of the expression. |
880 | ||
881 | void | |
882 | Set_and_use_temporary_expression::do_determine_type( | |
883 | const Type_context* context) | |
884 | { | |
885 | this->expr_->determine_type(context); | |
886 | } | |
887 | ||
e9d3367e | 888 | // Take the address. |
889 | ||
890 | void | |
891 | Set_and_use_temporary_expression::do_address_taken(bool) | |
892 | { | |
893 | this->statement_->set_is_address_taken(); | |
894 | } | |
895 | ||
896 | // Return the backend representation. | |
897 | ||
898 | tree | |
899 | Set_and_use_temporary_expression::do_get_tree(Translate_context* context) | |
900 | { | |
e9d3367e | 901 | Location loc = this->location(); |
a302c105 | 902 | Gogo* gogo = context->gogo(); |
903 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
904 | Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc); | |
905 | ||
906 | Bexpression* bexpr = tree_to_expr(this->expr_->get_tree(context)); | |
907 | Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc); | |
908 | var_ref = gogo->backend()->var_expression(bvar, loc); | |
909 | Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc); | |
910 | return expr_to_tree(ret); | |
e9d3367e | 911 | } |
912 | ||
913 | // Dump. | |
914 | ||
915 | void | |
916 | Set_and_use_temporary_expression::do_dump_expression( | |
917 | Ast_dump_context* ast_dump_context) const | |
918 | { | |
919 | ast_dump_context->ostream() << '('; | |
920 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
921 | ast_dump_context->ostream() << " = "; | |
922 | this->expr_->dump_expression(ast_dump_context); | |
923 | ast_dump_context->ostream() << ')'; | |
924 | } | |
925 | ||
926 | // Make a set-and-use temporary. | |
927 | ||
928 | Set_and_use_temporary_expression* | |
929 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
930 | Expression* expr, Location location) | |
931 | { | |
932 | return new Set_and_use_temporary_expression(statement, expr, location); | |
933 | } | |
934 | ||
e440a328 | 935 | // A sink expression--a use of the blank identifier _. |
936 | ||
937 | class Sink_expression : public Expression | |
938 | { | |
939 | public: | |
b13c66cd | 940 | Sink_expression(Location location) |
e440a328 | 941 | : Expression(EXPRESSION_SINK, location), |
aa93217a | 942 | type_(NULL), bvar_(NULL) |
e440a328 | 943 | { } |
944 | ||
945 | protected: | |
4f2138d7 | 946 | bool |
e440a328 | 947 | do_discarding_value() |
4f2138d7 | 948 | { return true; } |
e440a328 | 949 | |
950 | Type* | |
951 | do_type(); | |
952 | ||
953 | void | |
954 | do_determine_type(const Type_context*); | |
955 | ||
956 | Expression* | |
957 | do_copy() | |
958 | { return new Sink_expression(this->location()); } | |
959 | ||
960 | tree | |
961 | do_get_tree(Translate_context*); | |
962 | ||
d751bb78 | 963 | void |
964 | do_dump_expression(Ast_dump_context*) const; | |
965 | ||
e440a328 | 966 | private: |
967 | // The type of this sink variable. | |
968 | Type* type_; | |
969 | // The temporary variable we generate. | |
aa93217a | 970 | Bvariable* bvar_; |
e440a328 | 971 | }; |
972 | ||
973 | // Return the type of a sink expression. | |
974 | ||
975 | Type* | |
976 | Sink_expression::do_type() | |
977 | { | |
978 | if (this->type_ == NULL) | |
979 | return Type::make_sink_type(); | |
980 | return this->type_; | |
981 | } | |
982 | ||
983 | // Determine the type of a sink expression. | |
984 | ||
985 | void | |
986 | Sink_expression::do_determine_type(const Type_context* context) | |
987 | { | |
988 | if (context->type != NULL) | |
989 | this->type_ = context->type; | |
990 | } | |
991 | ||
992 | // Return a temporary variable for a sink expression. This will | |
993 | // presumably be a write-only variable which the middle-end will drop. | |
994 | ||
995 | tree | |
996 | Sink_expression::do_get_tree(Translate_context* context) | |
997 | { | |
aa93217a | 998 | Location loc = this->location(); |
999 | Gogo* gogo = context->gogo(); | |
1000 | if (this->bvar_ == NULL) | |
e440a328 | 1001 | { |
c484d925 | 1002 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
aa93217a | 1003 | Named_object* fn = context->function(); |
1004 | go_assert(fn != NULL); | |
1005 | Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn); | |
9f0e0513 | 1006 | Btype* bt = this->type_->get_backend(context->gogo()); |
aa93217a | 1007 | Bstatement* decl; |
1008 | this->bvar_ = | |
1009 | gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL, | |
1010 | false, loc, &decl); | |
1011 | Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc); | |
1012 | var_ref = gogo->backend()->compound_expression(decl, var_ref, loc); | |
1013 | return expr_to_tree(var_ref); | |
e440a328 | 1014 | } |
aa93217a | 1015 | return expr_to_tree(gogo->backend()->var_expression(this->bvar_, loc)); |
e440a328 | 1016 | } |
1017 | ||
d751bb78 | 1018 | // Ast dump for sink expression. |
1019 | ||
1020 | void | |
1021 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1022 | { | |
1023 | ast_dump_context->ostream() << "_" ; | |
1024 | } | |
1025 | ||
e440a328 | 1026 | // Make a sink expression. |
1027 | ||
1028 | Expression* | |
b13c66cd | 1029 | Expression::make_sink(Location location) |
e440a328 | 1030 | { |
1031 | return new Sink_expression(location); | |
1032 | } | |
1033 | ||
1034 | // Class Func_expression. | |
1035 | ||
1036 | // FIXME: Can a function expression appear in a constant expression? | |
1037 | // The value is unchanging. Initializing a constant to the address of | |
1038 | // a function seems like it could work, though there might be little | |
1039 | // point to it. | |
1040 | ||
e440a328 | 1041 | // Traversal. |
1042 | ||
1043 | int | |
1044 | Func_expression::do_traverse(Traverse* traverse) | |
1045 | { | |
1046 | return (this->closure_ == NULL | |
1047 | ? TRAVERSE_CONTINUE | |
1048 | : Expression::traverse(&this->closure_, traverse)); | |
1049 | } | |
1050 | ||
1051 | // Return the type of a function expression. | |
1052 | ||
1053 | Type* | |
1054 | Func_expression::do_type() | |
1055 | { | |
1056 | if (this->function_->is_function()) | |
1057 | return this->function_->func_value()->type(); | |
1058 | else if (this->function_->is_function_declaration()) | |
1059 | return this->function_->func_declaration_value()->type(); | |
1060 | else | |
c3e6f413 | 1061 | go_unreachable(); |
e440a328 | 1062 | } |
1063 | ||
8381eda7 | 1064 | // Get the tree for the code of a function expression. |
e440a328 | 1065 | |
97267c39 | 1066 | Bexpression* |
8381eda7 | 1067 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1068 | { |
1069 | Function_type* fntype; | |
8381eda7 | 1070 | if (no->is_function()) |
1071 | fntype = no->func_value()->type(); | |
1072 | else if (no->is_function_declaration()) | |
1073 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1074 | else |
c3e6f413 | 1075 | go_unreachable(); |
e440a328 | 1076 | |
1077 | // Builtin functions are handled specially by Call_expression. We | |
1078 | // can't take their address. | |
1079 | if (fntype->is_builtin()) | |
1080 | { | |
8381eda7 | 1081 | error_at(loc, |
cb0e02f3 | 1082 | "invalid use of special builtin function %qs; must be called", |
8381eda7 | 1083 | no->message_name().c_str()); |
97267c39 | 1084 | return gogo->backend()->error_expression(); |
e440a328 | 1085 | } |
1086 | ||
97267c39 | 1087 | Bfunction* fndecl; |
e440a328 | 1088 | if (no->is_function()) |
cf3cae55 | 1089 | fndecl = no->func_value()->get_or_make_decl(gogo, no); |
e440a328 | 1090 | else if (no->is_function_declaration()) |
cf3cae55 | 1091 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no); |
e440a328 | 1092 | else |
c3e6f413 | 1093 | go_unreachable(); |
e440a328 | 1094 | |
97267c39 | 1095 | return gogo->backend()->function_code_expression(fndecl, loc); |
e440a328 | 1096 | } |
1097 | ||
1098 | // Get the tree for a function expression. This is used when we take | |
8381eda7 | 1099 | // the address of a function rather than simply calling it. A func |
1100 | // value is represented as a pointer to a block of memory. The first | |
1101 | // word of that memory is a pointer to the function code. The | |
1102 | // remaining parts of that memory are the addresses of variables that | |
1103 | // the function closes over. | |
e440a328 | 1104 | |
1105 | tree | |
1106 | Func_expression::do_get_tree(Translate_context* context) | |
1107 | { | |
8381eda7 | 1108 | // If there is no closure, just use the function descriptor. |
2010c17a | 1109 | if (this->closure_ == NULL) |
8381eda7 | 1110 | { |
1111 | Gogo* gogo = context->gogo(); | |
1112 | Named_object* no = this->function_; | |
1113 | Expression* descriptor; | |
1114 | if (no->is_function()) | |
1115 | descriptor = no->func_value()->descriptor(gogo, no); | |
1116 | else if (no->is_function_declaration()) | |
1117 | { | |
1118 | if (no->func_declaration_value()->type()->is_builtin()) | |
1119 | { | |
1120 | error_at(this->location(), | |
1121 | ("invalid use of special builtin function %qs; " | |
1122 | "must be called"), | |
1123 | no->message_name().c_str()); | |
1124 | return error_mark_node; | |
1125 | } | |
1126 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1127 | } | |
1128 | else | |
1129 | go_unreachable(); | |
2010c17a | 1130 | |
8381eda7 | 1131 | tree dtree = descriptor->get_tree(context); |
1132 | if (dtree == error_mark_node) | |
1133 | return error_mark_node; | |
1134 | return build_fold_addr_expr_loc(this->location().gcc_location(), dtree); | |
1135 | } | |
e440a328 | 1136 | |
8381eda7 | 1137 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1138 | |
8381eda7 | 1139 | // If there is a closure, then the closure is itself the function |
1140 | // expression. It is a pointer to a struct whose first field points | |
1141 | // to the function code and whose remaining fields are the addresses | |
1142 | // of the closed-over variables. | |
1143 | return this->closure_->get_tree(context); | |
e440a328 | 1144 | } |
1145 | ||
d751bb78 | 1146 | // Ast dump for function. |
1147 | ||
1148 | void | |
1149 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1150 | { | |
8b1c301d | 1151 | ast_dump_context->ostream() << this->function_->name(); |
1152 | if (this->closure_ != NULL) | |
1153 | { | |
1154 | ast_dump_context->ostream() << " {closure = "; | |
1155 | this->closure_->dump_expression(ast_dump_context); | |
1156 | ast_dump_context->ostream() << "}"; | |
1157 | } | |
d751bb78 | 1158 | } |
1159 | ||
e440a328 | 1160 | // Make a reference to a function in an expression. |
1161 | ||
1162 | Expression* | |
1163 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1164 | Location location) |
e440a328 | 1165 | { |
1166 | return new Func_expression(function, closure, location); | |
1167 | } | |
1168 | ||
c6837989 | 1169 | // Class Func_descriptor_expression. |
8381eda7 | 1170 | |
c6837989 | 1171 | // Constructor. |
8381eda7 | 1172 | |
c6837989 | 1173 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1174 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1175 | fn_(fn), dvar_(NULL) |
c6837989 | 1176 | { |
1177 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1178 | } | |
8381eda7 | 1179 | |
c6837989 | 1180 | // Traversal. |
8381eda7 | 1181 | |
c6837989 | 1182 | int |
1183 | Func_descriptor_expression::do_traverse(Traverse*) | |
1184 | { | |
1185 | return TRAVERSE_CONTINUE; | |
1186 | } | |
8381eda7 | 1187 | |
1188 | // All function descriptors have the same type. | |
1189 | ||
1190 | Type* Func_descriptor_expression::descriptor_type; | |
1191 | ||
1192 | void | |
1193 | Func_descriptor_expression::make_func_descriptor_type() | |
1194 | { | |
1195 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1196 | return; | |
1197 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1198 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1199 | Func_descriptor_expression::descriptor_type = | |
1200 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1201 | } | |
1202 | ||
1203 | Type* | |
1204 | Func_descriptor_expression::do_type() | |
1205 | { | |
1206 | Func_descriptor_expression::make_func_descriptor_type(); | |
1207 | return Func_descriptor_expression::descriptor_type; | |
1208 | } | |
1209 | ||
1210 | // The tree for a function descriptor. | |
1211 | ||
1212 | tree | |
1213 | Func_descriptor_expression::do_get_tree(Translate_context* context) | |
1214 | { | |
1215 | if (this->dvar_ != NULL) | |
1216 | return var_to_tree(this->dvar_); | |
1217 | ||
1218 | Gogo* gogo = context->gogo(); | |
1219 | Named_object* no = this->fn_; | |
1220 | Location loc = no->location(); | |
1221 | ||
1222 | std::string var_name; | |
1223 | if (no->package() == NULL) | |
1224 | var_name = gogo->pkgpath_symbol(); | |
1225 | else | |
1226 | var_name = no->package()->pkgpath_symbol(); | |
1227 | var_name.push_back('.'); | |
1228 | var_name.append(Gogo::unpack_hidden_name(no->name())); | |
1229 | var_name.append("$descriptor"); | |
1230 | ||
1231 | Btype* btype = this->type()->get_backend(gogo); | |
1232 | ||
1233 | Bvariable* bvar; | |
1234 | if (no->package() != NULL | |
1235 | || Linemap::is_predeclared_location(no->location())) | |
f8bdf81a | 1236 | bvar = context->backend()->immutable_struct_reference(var_name, btype, |
1237 | loc); | |
8381eda7 | 1238 | else |
1239 | { | |
1240 | Location bloc = Linemap::predeclared_location(); | |
1241 | bool is_hidden = ((no->is_function() | |
1242 | && no->func_value()->enclosing() != NULL) | |
1243 | || Gogo::is_thunk(no)); | |
1244 | bvar = context->backend()->immutable_struct(var_name, is_hidden, false, | |
1245 | btype, bloc); | |
1246 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1247 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1248 | Expression* init = |
1249 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1250 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1251 | bcontext.set_is_const(); | |
1252 | Bexpression* binit = tree_to_expr(init->get_tree(&bcontext)); | |
1253 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, | |
1254 | false, btype, bloc, binit); | |
1255 | } | |
1256 | ||
1257 | this->dvar_ = bvar; | |
1258 | return var_to_tree(bvar); | |
1259 | } | |
1260 | ||
c6837989 | 1261 | // Print a function descriptor expression. |
1262 | ||
1263 | void | |
1264 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1265 | { | |
1266 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1267 | } | |
1268 | ||
8381eda7 | 1269 | // Make a function descriptor expression. |
1270 | ||
c6837989 | 1271 | Func_descriptor_expression* |
1272 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1273 | { |
c6837989 | 1274 | return new Func_descriptor_expression(fn); |
8381eda7 | 1275 | } |
1276 | ||
1277 | // Make the function descriptor type, so that it can be converted. | |
1278 | ||
1279 | void | |
1280 | Expression::make_func_descriptor_type() | |
1281 | { | |
1282 | Func_descriptor_expression::make_func_descriptor_type(); | |
1283 | } | |
1284 | ||
1285 | // A reference to just the code of a function. | |
1286 | ||
1287 | class Func_code_reference_expression : public Expression | |
1288 | { | |
1289 | public: | |
1290 | Func_code_reference_expression(Named_object* function, Location location) | |
1291 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1292 | function_(function) | |
1293 | { } | |
1294 | ||
1295 | protected: | |
1296 | int | |
1297 | do_traverse(Traverse*) | |
1298 | { return TRAVERSE_CONTINUE; } | |
1299 | ||
f9ca30f9 | 1300 | bool |
1301 | do_is_immutable() const | |
1302 | { return true; } | |
1303 | ||
8381eda7 | 1304 | Type* |
1305 | do_type() | |
1306 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1307 | ||
1308 | void | |
1309 | do_determine_type(const Type_context*) | |
1310 | { } | |
1311 | ||
1312 | Expression* | |
1313 | do_copy() | |
1314 | { | |
1315 | return Expression::make_func_code_reference(this->function_, | |
1316 | this->location()); | |
1317 | } | |
1318 | ||
1319 | tree | |
1320 | do_get_tree(Translate_context*); | |
1321 | ||
1322 | void | |
1323 | do_dump_expression(Ast_dump_context* context) const | |
1324 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1325 | ||
1326 | private: | |
1327 | // The function. | |
1328 | Named_object* function_; | |
1329 | }; | |
1330 | ||
1331 | // Get the tree for a reference to function code. | |
1332 | ||
1333 | tree | |
1334 | Func_code_reference_expression::do_get_tree(Translate_context* context) | |
1335 | { | |
97267c39 | 1336 | Bexpression* ret = |
1337 | Func_expression::get_code_pointer(context->gogo(), this->function_, | |
1338 | this->location()); | |
1339 | return expr_to_tree(ret); | |
8381eda7 | 1340 | } |
1341 | ||
1342 | // Make a reference to the code of a function. | |
1343 | ||
1344 | Expression* | |
1345 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1346 | { | |
1347 | return new Func_code_reference_expression(function, location); | |
1348 | } | |
1349 | ||
e440a328 | 1350 | // Class Unknown_expression. |
1351 | ||
1352 | // Return the name of an unknown expression. | |
1353 | ||
1354 | const std::string& | |
1355 | Unknown_expression::name() const | |
1356 | { | |
1357 | return this->named_object_->name(); | |
1358 | } | |
1359 | ||
1360 | // Lower a reference to an unknown name. | |
1361 | ||
1362 | Expression* | |
ceeb4318 | 1363 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1364 | { |
b13c66cd | 1365 | Location location = this->location(); |
e440a328 | 1366 | Named_object* no = this->named_object_; |
deded542 | 1367 | Named_object* real; |
1368 | if (!no->is_unknown()) | |
1369 | real = no; | |
1370 | else | |
e440a328 | 1371 | { |
deded542 | 1372 | real = no->unknown_value()->real_named_object(); |
1373 | if (real == NULL) | |
1374 | { | |
1375 | if (this->is_composite_literal_key_) | |
1376 | return this; | |
acf8e158 | 1377 | if (!this->no_error_message_) |
1378 | error_at(location, "reference to undefined name %qs", | |
1379 | this->named_object_->message_name().c_str()); | |
deded542 | 1380 | return Expression::make_error(location); |
1381 | } | |
e440a328 | 1382 | } |
1383 | switch (real->classification()) | |
1384 | { | |
1385 | case Named_object::NAMED_OBJECT_CONST: | |
1386 | return Expression::make_const_reference(real, location); | |
1387 | case Named_object::NAMED_OBJECT_TYPE: | |
1388 | return Expression::make_type(real->type_value(), location); | |
1389 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1390 | if (this->is_composite_literal_key_) | |
1391 | return this; | |
acf8e158 | 1392 | if (!this->no_error_message_) |
1393 | error_at(location, "reference to undefined type %qs", | |
1394 | real->message_name().c_str()); | |
e440a328 | 1395 | return Expression::make_error(location); |
1396 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1397 | real->var_value()->set_is_used(); |
e440a328 | 1398 | return Expression::make_var_reference(real, location); |
1399 | case Named_object::NAMED_OBJECT_FUNC: | |
1400 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1401 | return Expression::make_func_reference(real, NULL, location); | |
1402 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1403 | if (this->is_composite_literal_key_) | |
1404 | return this; | |
acf8e158 | 1405 | if (!this->no_error_message_) |
1406 | error_at(location, "unexpected reference to package"); | |
e440a328 | 1407 | return Expression::make_error(location); |
1408 | default: | |
c3e6f413 | 1409 | go_unreachable(); |
e440a328 | 1410 | } |
1411 | } | |
1412 | ||
d751bb78 | 1413 | // Dump the ast representation for an unknown expression to a dump context. |
1414 | ||
1415 | void | |
1416 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1417 | { | |
1418 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1419 | << ")"; | |
d751bb78 | 1420 | } |
1421 | ||
e440a328 | 1422 | // Make a reference to an unknown name. |
1423 | ||
acf8e158 | 1424 | Unknown_expression* |
b13c66cd | 1425 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1426 | { |
e440a328 | 1427 | return new Unknown_expression(no, location); |
1428 | } | |
1429 | ||
1430 | // A boolean expression. | |
1431 | ||
1432 | class Boolean_expression : public Expression | |
1433 | { | |
1434 | public: | |
b13c66cd | 1435 | Boolean_expression(bool val, Location location) |
e440a328 | 1436 | : Expression(EXPRESSION_BOOLEAN, location), |
1437 | val_(val), type_(NULL) | |
1438 | { } | |
1439 | ||
1440 | static Expression* | |
1441 | do_import(Import*); | |
1442 | ||
1443 | protected: | |
1444 | bool | |
1445 | do_is_constant() const | |
1446 | { return true; } | |
1447 | ||
0e168074 | 1448 | bool |
1449 | do_is_immutable() const | |
1450 | { return true; } | |
1451 | ||
e440a328 | 1452 | Type* |
1453 | do_type(); | |
1454 | ||
1455 | void | |
1456 | do_determine_type(const Type_context*); | |
1457 | ||
1458 | Expression* | |
1459 | do_copy() | |
1460 | { return this; } | |
1461 | ||
1462 | tree | |
1463 | do_get_tree(Translate_context*) | |
1464 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1465 | ||
1466 | void | |
1467 | do_export(Export* exp) const | |
1468 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1469 | ||
d751bb78 | 1470 | void |
1471 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1472 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1473 | ||
e440a328 | 1474 | private: |
1475 | // The constant. | |
1476 | bool val_; | |
1477 | // The type as determined by context. | |
1478 | Type* type_; | |
1479 | }; | |
1480 | ||
1481 | // Get the type. | |
1482 | ||
1483 | Type* | |
1484 | Boolean_expression::do_type() | |
1485 | { | |
1486 | if (this->type_ == NULL) | |
1487 | this->type_ = Type::make_boolean_type(); | |
1488 | return this->type_; | |
1489 | } | |
1490 | ||
1491 | // Set the type from the context. | |
1492 | ||
1493 | void | |
1494 | Boolean_expression::do_determine_type(const Type_context* context) | |
1495 | { | |
1496 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1497 | ; | |
1498 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1499 | this->type_ = context->type; | |
1500 | else if (!context->may_be_abstract) | |
1501 | this->type_ = Type::lookup_bool_type(); | |
1502 | } | |
1503 | ||
1504 | // Import a boolean constant. | |
1505 | ||
1506 | Expression* | |
1507 | Boolean_expression::do_import(Import* imp) | |
1508 | { | |
1509 | if (imp->peek_char() == 't') | |
1510 | { | |
1511 | imp->require_c_string("true"); | |
1512 | return Expression::make_boolean(true, imp->location()); | |
1513 | } | |
1514 | else | |
1515 | { | |
1516 | imp->require_c_string("false"); | |
1517 | return Expression::make_boolean(false, imp->location()); | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | // Make a boolean expression. | |
1522 | ||
1523 | Expression* | |
b13c66cd | 1524 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1525 | { |
1526 | return new Boolean_expression(val, location); | |
1527 | } | |
1528 | ||
1529 | // Class String_expression. | |
1530 | ||
1531 | // Get the type. | |
1532 | ||
1533 | Type* | |
1534 | String_expression::do_type() | |
1535 | { | |
1536 | if (this->type_ == NULL) | |
1537 | this->type_ = Type::make_string_type(); | |
1538 | return this->type_; | |
1539 | } | |
1540 | ||
1541 | // Set the type from the context. | |
1542 | ||
1543 | void | |
1544 | String_expression::do_determine_type(const Type_context* context) | |
1545 | { | |
1546 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1547 | ; | |
1548 | else if (context->type != NULL && context->type->is_string_type()) | |
1549 | this->type_ = context->type; | |
1550 | else if (!context->may_be_abstract) | |
1551 | this->type_ = Type::lookup_string_type(); | |
1552 | } | |
1553 | ||
1554 | // Build a string constant. | |
1555 | ||
1556 | tree | |
1557 | String_expression::do_get_tree(Translate_context* context) | |
1558 | { | |
2c809f8f | 1559 | Gogo* gogo = context->gogo(); |
1560 | Btype* btype = Type::make_string_type()->get_backend(gogo); | |
1561 | ||
1562 | Location loc = this->location(); | |
1563 | std::vector<Bexpression*> init(2); | |
1564 | Bexpression* str_cst = | |
1565 | gogo->backend()->string_constant_expression(this->val_); | |
1566 | init[0] = gogo->backend()->address_expression(str_cst, loc); | |
1567 | ||
1568 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
1569 | mpz_t lenval; | |
1570 | mpz_init_set_ui(lenval, this->val_.length()); | |
1571 | init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval); | |
1572 | mpz_clear(lenval); | |
1573 | ||
1574 | Bexpression* ret = gogo->backend()->constructor_expression(btype, init, loc); | |
1575 | return expr_to_tree(ret); | |
e440a328 | 1576 | } |
1577 | ||
8b1c301d | 1578 | // Write string literal to string dump. |
e440a328 | 1579 | |
1580 | void | |
8b1c301d | 1581 | String_expression::export_string(String_dump* exp, |
1582 | const String_expression* str) | |
e440a328 | 1583 | { |
1584 | std::string s; | |
8b1c301d | 1585 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1586 | s += '"'; |
8b1c301d | 1587 | for (std::string::const_iterator p = str->val_.begin(); |
1588 | p != str->val_.end(); | |
e440a328 | 1589 | ++p) |
1590 | { | |
1591 | if (*p == '\\' || *p == '"') | |
1592 | { | |
1593 | s += '\\'; | |
1594 | s += *p; | |
1595 | } | |
1596 | else if (*p >= 0x20 && *p < 0x7f) | |
1597 | s += *p; | |
1598 | else if (*p == '\n') | |
1599 | s += "\\n"; | |
1600 | else if (*p == '\t') | |
1601 | s += "\\t"; | |
1602 | else | |
1603 | { | |
1604 | s += "\\x"; | |
1605 | unsigned char c = *p; | |
1606 | unsigned int dig = c >> 4; | |
1607 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1608 | dig = c & 0xf; | |
1609 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1610 | } | |
1611 | } | |
1612 | s += '"'; | |
1613 | exp->write_string(s); | |
1614 | } | |
1615 | ||
8b1c301d | 1616 | // Export a string expression. |
1617 | ||
1618 | void | |
1619 | String_expression::do_export(Export* exp) const | |
1620 | { | |
1621 | String_expression::export_string(exp, this); | |
1622 | } | |
1623 | ||
e440a328 | 1624 | // Import a string expression. |
1625 | ||
1626 | Expression* | |
1627 | String_expression::do_import(Import* imp) | |
1628 | { | |
1629 | imp->require_c_string("\""); | |
1630 | std::string val; | |
1631 | while (true) | |
1632 | { | |
1633 | int c = imp->get_char(); | |
1634 | if (c == '"' || c == -1) | |
1635 | break; | |
1636 | if (c != '\\') | |
1637 | val += static_cast<char>(c); | |
1638 | else | |
1639 | { | |
1640 | c = imp->get_char(); | |
1641 | if (c == '\\' || c == '"') | |
1642 | val += static_cast<char>(c); | |
1643 | else if (c == 'n') | |
1644 | val += '\n'; | |
1645 | else if (c == 't') | |
1646 | val += '\t'; | |
1647 | else if (c == 'x') | |
1648 | { | |
1649 | c = imp->get_char(); | |
1650 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1651 | c = imp->get_char(); | |
1652 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1653 | char v = (vh << 4) | vl; | |
1654 | val += v; | |
1655 | } | |
1656 | else | |
1657 | { | |
1658 | error_at(imp->location(), "bad string constant"); | |
1659 | return Expression::make_error(imp->location()); | |
1660 | } | |
1661 | } | |
1662 | } | |
1663 | return Expression::make_string(val, imp->location()); | |
1664 | } | |
1665 | ||
d751bb78 | 1666 | // Ast dump for string expression. |
1667 | ||
1668 | void | |
1669 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1670 | { | |
8b1c301d | 1671 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1672 | } |
1673 | ||
e440a328 | 1674 | // Make a string expression. |
1675 | ||
1676 | Expression* | |
b13c66cd | 1677 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1678 | { |
1679 | return new String_expression(val, location); | |
1680 | } | |
1681 | ||
2c809f8f | 1682 | // An expression that evaluates to some characteristic of a string. |
1683 | // This is used when indexing, bound-checking, or nil checking a string. | |
1684 | ||
1685 | class String_info_expression : public Expression | |
1686 | { | |
1687 | public: | |
1688 | String_info_expression(Expression* string, String_info string_info, | |
1689 | Location location) | |
1690 | : Expression(EXPRESSION_STRING_INFO, location), | |
1691 | string_(string), string_info_(string_info) | |
1692 | { } | |
1693 | ||
1694 | protected: | |
1695 | Type* | |
1696 | do_type(); | |
1697 | ||
1698 | void | |
1699 | do_determine_type(const Type_context*) | |
1700 | { go_unreachable(); } | |
1701 | ||
1702 | Expression* | |
1703 | do_copy() | |
1704 | { | |
1705 | return new String_info_expression(this->string_->copy(), this->string_info_, | |
1706 | this->location()); | |
1707 | } | |
1708 | ||
1709 | tree | |
1710 | do_get_tree(Translate_context* context); | |
1711 | ||
1712 | void | |
1713 | do_dump_expression(Ast_dump_context*) const; | |
1714 | ||
1715 | void | |
1716 | do_issue_nil_check() | |
1717 | { this->string_->issue_nil_check(); } | |
1718 | ||
1719 | private: | |
1720 | // The string for which we are getting information. | |
1721 | Expression* string_; | |
1722 | // What information we want. | |
1723 | String_info string_info_; | |
1724 | }; | |
1725 | ||
1726 | // Return the type of the string info. | |
1727 | ||
1728 | Type* | |
1729 | String_info_expression::do_type() | |
1730 | { | |
1731 | switch (this->string_info_) | |
1732 | { | |
1733 | case STRING_INFO_DATA: | |
1734 | { | |
1735 | Type* byte_type = Type::lookup_integer_type("uint8"); | |
1736 | return Type::make_pointer_type(byte_type); | |
1737 | } | |
1738 | case STRING_INFO_LENGTH: | |
1739 | return Type::lookup_integer_type("int"); | |
1740 | default: | |
1741 | go_unreachable(); | |
1742 | } | |
1743 | } | |
1744 | ||
1745 | // Return string information in GENERIC. | |
1746 | ||
1747 | tree | |
1748 | String_info_expression::do_get_tree(Translate_context* context) | |
1749 | { | |
1750 | Gogo* gogo = context->gogo(); | |
1751 | ||
1752 | Bexpression* bstring = tree_to_expr(this->string_->get_tree(context)); | |
1753 | Bexpression* ret; | |
1754 | switch (this->string_info_) | |
1755 | { | |
1756 | case STRING_INFO_DATA: | |
1757 | case STRING_INFO_LENGTH: | |
1758 | ret = gogo->backend()->struct_field_expression(bstring, this->string_info_, | |
1759 | this->location()); | |
1760 | break; | |
1761 | default: | |
1762 | go_unreachable(); | |
1763 | } | |
1764 | return expr_to_tree(ret); | |
1765 | } | |
1766 | ||
1767 | // Dump ast representation for a type info expression. | |
1768 | ||
1769 | void | |
1770 | String_info_expression::do_dump_expression( | |
1771 | Ast_dump_context* ast_dump_context) const | |
1772 | { | |
1773 | ast_dump_context->ostream() << "stringinfo("; | |
1774 | this->string_->dump_expression(ast_dump_context); | |
1775 | ast_dump_context->ostream() << ","; | |
1776 | ast_dump_context->ostream() << | |
1777 | (this->string_info_ == STRING_INFO_DATA ? "data" | |
1778 | : this->string_info_ == STRING_INFO_LENGTH ? "length" | |
1779 | : "unknown"); | |
1780 | ast_dump_context->ostream() << ")"; | |
1781 | } | |
1782 | ||
1783 | // Make a string info expression. | |
1784 | ||
1785 | Expression* | |
1786 | Expression::make_string_info(Expression* string, String_info string_info, | |
1787 | Location location) | |
1788 | { | |
1789 | return new String_info_expression(string, string_info, location); | |
1790 | } | |
1791 | ||
e440a328 | 1792 | // Make an integer expression. |
1793 | ||
1794 | class Integer_expression : public Expression | |
1795 | { | |
1796 | public: | |
5d4b8566 | 1797 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1798 | Location location) | |
e440a328 | 1799 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1800 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1801 | { mpz_init_set(this->val_, *val); } |
1802 | ||
1803 | static Expression* | |
1804 | do_import(Import*); | |
1805 | ||
8b1c301d | 1806 | // Write VAL to string dump. |
e440a328 | 1807 | static void |
8b1c301d | 1808 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1809 | |
d751bb78 | 1810 | // Write VAL to dump context. |
1811 | static void | |
1812 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1813 | ||
e440a328 | 1814 | protected: |
1815 | bool | |
1816 | do_is_constant() const | |
1817 | { return true; } | |
1818 | ||
0e168074 | 1819 | bool |
1820 | do_is_immutable() const | |
1821 | { return true; } | |
1822 | ||
e440a328 | 1823 | bool |
0c77715b | 1824 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1825 | |
1826 | Type* | |
1827 | do_type(); | |
1828 | ||
1829 | void | |
1830 | do_determine_type(const Type_context* context); | |
1831 | ||
1832 | void | |
1833 | do_check_types(Gogo*); | |
1834 | ||
1835 | tree | |
1836 | do_get_tree(Translate_context*); | |
1837 | ||
1838 | Expression* | |
1839 | do_copy() | |
5d4b8566 | 1840 | { |
1841 | if (this->is_character_constant_) | |
1842 | return Expression::make_character(&this->val_, this->type_, | |
1843 | this->location()); | |
1844 | else | |
1845 | return Expression::make_integer(&this->val_, this->type_, | |
1846 | this->location()); | |
1847 | } | |
e440a328 | 1848 | |
1849 | void | |
1850 | do_export(Export*) const; | |
1851 | ||
d751bb78 | 1852 | void |
1853 | do_dump_expression(Ast_dump_context*) const; | |
1854 | ||
e440a328 | 1855 | private: |
1856 | // The integer value. | |
1857 | mpz_t val_; | |
1858 | // The type so far. | |
1859 | Type* type_; | |
5d4b8566 | 1860 | // Whether this is a character constant. |
1861 | bool is_character_constant_; | |
e440a328 | 1862 | }; |
1863 | ||
0c77715b | 1864 | // Return a numeric constant for this expression. We have to mark |
1865 | // this as a character when appropriate. | |
e440a328 | 1866 | |
1867 | bool | |
0c77715b | 1868 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1869 | { |
0c77715b | 1870 | if (this->is_character_constant_) |
1871 | nc->set_rune(this->type_, this->val_); | |
1872 | else | |
1873 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1874 | return true; |
1875 | } | |
1876 | ||
1877 | // Return the current type. If we haven't set the type yet, we return | |
1878 | // an abstract integer type. | |
1879 | ||
1880 | Type* | |
1881 | Integer_expression::do_type() | |
1882 | { | |
1883 | if (this->type_ == NULL) | |
5d4b8566 | 1884 | { |
1885 | if (this->is_character_constant_) | |
1886 | this->type_ = Type::make_abstract_character_type(); | |
1887 | else | |
1888 | this->type_ = Type::make_abstract_integer_type(); | |
1889 | } | |
e440a328 | 1890 | return this->type_; |
1891 | } | |
1892 | ||
1893 | // Set the type of the integer value. Here we may switch from an | |
1894 | // abstract type to a real type. | |
1895 | ||
1896 | void | |
1897 | Integer_expression::do_determine_type(const Type_context* context) | |
1898 | { | |
1899 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1900 | ; | |
0c77715b | 1901 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1902 | this->type_ = context->type; |
1903 | else if (!context->may_be_abstract) | |
5d4b8566 | 1904 | { |
1905 | if (this->is_character_constant_) | |
1906 | this->type_ = Type::lookup_integer_type("int32"); | |
1907 | else | |
1908 | this->type_ = Type::lookup_integer_type("int"); | |
1909 | } | |
e440a328 | 1910 | } |
1911 | ||
e440a328 | 1912 | // Check the type of an integer constant. |
1913 | ||
1914 | void | |
1915 | Integer_expression::do_check_types(Gogo*) | |
1916 | { | |
0c77715b | 1917 | Type* type = this->type_; |
1918 | if (type == NULL) | |
e440a328 | 1919 | return; |
0c77715b | 1920 | Numeric_constant nc; |
1921 | if (this->is_character_constant_) | |
1922 | nc.set_rune(NULL, this->val_); | |
1923 | else | |
1924 | nc.set_int(NULL, this->val_); | |
1925 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1926 | this->set_is_error(); |
1927 | } | |
1928 | ||
1929 | // Get a tree for an integer constant. | |
1930 | ||
1931 | tree | |
1932 | Integer_expression::do_get_tree(Translate_context* context) | |
1933 | { | |
48c2a53a | 1934 | Type* resolved_type = NULL; |
e440a328 | 1935 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 1936 | resolved_type = this->type_; |
e440a328 | 1937 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
1938 | { | |
1939 | // We are converting to an abstract floating point type. | |
48c2a53a | 1940 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 1941 | } |
1942 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1943 | { | |
1944 | // We are converting to an abstract complex type. | |
48c2a53a | 1945 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 1946 | } |
1947 | else | |
1948 | { | |
1949 | // If we still have an abstract type here, then this is being | |
1950 | // used in a constant expression which didn't get reduced for | |
1951 | // some reason. Use a type which will fit the value. We use <, | |
1952 | // not <=, because we need an extra bit for the sign bit. | |
1953 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 1954 | Type* int_type = Type::lookup_integer_type("int"); |
1955 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 1956 | resolved_type = int_type; |
e440a328 | 1957 | else if (bits < 64) |
48c2a53a | 1958 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 1959 | else |
48c2a53a | 1960 | { |
1961 | if (!saw_errors()) | |
1962 | error_at(this->location(), | |
1963 | "unknown type for large integer constant"); | |
1964 | Bexpression* ret = context->gogo()->backend()->error_expression(); | |
1965 | return expr_to_tree(ret); | |
1966 | } | |
e440a328 | 1967 | } |
48c2a53a | 1968 | Numeric_constant nc; |
1969 | nc.set_int(resolved_type, this->val_); | |
1970 | Bexpression* ret = | |
1971 | Expression::backend_numeric_constant_expression(context, &nc); | |
1972 | return expr_to_tree(ret); | |
e440a328 | 1973 | } |
1974 | ||
1975 | // Write VAL to export data. | |
1976 | ||
1977 | void | |
8b1c301d | 1978 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 1979 | { |
1980 | char* s = mpz_get_str(NULL, 10, val); | |
1981 | exp->write_c_string(s); | |
1982 | free(s); | |
1983 | } | |
1984 | ||
1985 | // Export an integer in a constant expression. | |
1986 | ||
1987 | void | |
1988 | Integer_expression::do_export(Export* exp) const | |
1989 | { | |
1990 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 1991 | if (this->is_character_constant_) |
1992 | exp->write_c_string("'"); | |
e440a328 | 1993 | // A trailing space lets us reliably identify the end of the number. |
1994 | exp->write_c_string(" "); | |
1995 | } | |
1996 | ||
1997 | // Import an integer, floating point, or complex value. This handles | |
1998 | // all these types because they all start with digits. | |
1999 | ||
2000 | Expression* | |
2001 | Integer_expression::do_import(Import* imp) | |
2002 | { | |
2003 | std::string num = imp->read_identifier(); | |
2004 | imp->require_c_string(" "); | |
2005 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2006 | { | |
2007 | mpfr_t real; | |
2008 | size_t plus_pos = num.find('+', 1); | |
2009 | size_t minus_pos = num.find('-', 1); | |
2010 | size_t pos; | |
2011 | if (plus_pos == std::string::npos) | |
2012 | pos = minus_pos; | |
2013 | else if (minus_pos == std::string::npos) | |
2014 | pos = plus_pos; | |
2015 | else | |
2016 | { | |
2017 | error_at(imp->location(), "bad number in import data: %qs", | |
2018 | num.c_str()); | |
2019 | return Expression::make_error(imp->location()); | |
2020 | } | |
2021 | if (pos == std::string::npos) | |
2022 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2023 | else | |
2024 | { | |
2025 | std::string real_str = num.substr(0, pos); | |
2026 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2027 | { | |
2028 | error_at(imp->location(), "bad number in import data: %qs", | |
2029 | real_str.c_str()); | |
2030 | return Expression::make_error(imp->location()); | |
2031 | } | |
2032 | } | |
2033 | ||
2034 | std::string imag_str; | |
2035 | if (pos == std::string::npos) | |
2036 | imag_str = num; | |
2037 | else | |
2038 | imag_str = num.substr(pos); | |
2039 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2040 | mpfr_t imag; | |
2041 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2042 | { | |
2043 | error_at(imp->location(), "bad number in import data: %qs", | |
2044 | imag_str.c_str()); | |
2045 | return Expression::make_error(imp->location()); | |
2046 | } | |
2047 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
2048 | imp->location()); | |
2049 | mpfr_clear(real); | |
2050 | mpfr_clear(imag); | |
2051 | return ret; | |
2052 | } | |
2053 | else if (num.find('.') == std::string::npos | |
2054 | && num.find('E') == std::string::npos) | |
2055 | { | |
5d4b8566 | 2056 | bool is_character_constant = (!num.empty() |
2057 | && num[num.length() - 1] == '\''); | |
2058 | if (is_character_constant) | |
2059 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2060 | mpz_t val; |
2061 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2062 | { | |
2063 | error_at(imp->location(), "bad number in import data: %qs", | |
2064 | num.c_str()); | |
2065 | return Expression::make_error(imp->location()); | |
2066 | } | |
5d4b8566 | 2067 | Expression* ret; |
2068 | if (is_character_constant) | |
2069 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2070 | else | |
2071 | ret = Expression::make_integer(&val, NULL, imp->location()); | |
e440a328 | 2072 | mpz_clear(val); |
2073 | return ret; | |
2074 | } | |
2075 | else | |
2076 | { | |
2077 | mpfr_t val; | |
2078 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2079 | { | |
2080 | error_at(imp->location(), "bad number in import data: %qs", | |
2081 | num.c_str()); | |
2082 | return Expression::make_error(imp->location()); | |
2083 | } | |
2084 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2085 | mpfr_clear(val); | |
2086 | return ret; | |
2087 | } | |
2088 | } | |
d751bb78 | 2089 | // Ast dump for integer expression. |
2090 | ||
2091 | void | |
2092 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2093 | { | |
5d4b8566 | 2094 | if (this->is_character_constant_) |
2095 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2096 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2097 | if (this->is_character_constant_) |
2098 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2099 | } |
2100 | ||
e440a328 | 2101 | // Build a new integer value. |
2102 | ||
2103 | Expression* | |
5d4b8566 | 2104 | Expression::make_integer(const mpz_t* val, Type* type, Location location) |
2105 | { | |
2106 | return new Integer_expression(val, type, false, location); | |
2107 | } | |
2108 | ||
2109 | // Build a new character constant value. | |
2110 | ||
2111 | Expression* | |
2112 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2113 | { |
5d4b8566 | 2114 | return new Integer_expression(val, type, true, location); |
e440a328 | 2115 | } |
2116 | ||
2117 | // Floats. | |
2118 | ||
2119 | class Float_expression : public Expression | |
2120 | { | |
2121 | public: | |
b13c66cd | 2122 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2123 | : Expression(EXPRESSION_FLOAT, location), |
2124 | type_(type) | |
2125 | { | |
2126 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2127 | } | |
2128 | ||
e440a328 | 2129 | // Write VAL to export data. |
2130 | static void | |
8b1c301d | 2131 | export_float(String_dump* exp, const mpfr_t val); |
2132 | ||
d751bb78 | 2133 | // Write VAL to dump file. |
2134 | static void | |
2135 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2136 | |
2137 | protected: | |
2138 | bool | |
2139 | do_is_constant() const | |
2140 | { return true; } | |
2141 | ||
0e168074 | 2142 | bool |
2143 | do_is_immutable() const | |
2144 | { return true; } | |
2145 | ||
e440a328 | 2146 | bool |
0c77715b | 2147 | do_numeric_constant_value(Numeric_constant* nc) const |
2148 | { | |
2149 | nc->set_float(this->type_, this->val_); | |
2150 | return true; | |
2151 | } | |
e440a328 | 2152 | |
2153 | Type* | |
2154 | do_type(); | |
2155 | ||
2156 | void | |
2157 | do_determine_type(const Type_context*); | |
2158 | ||
2159 | void | |
2160 | do_check_types(Gogo*); | |
2161 | ||
2162 | Expression* | |
2163 | do_copy() | |
2164 | { return Expression::make_float(&this->val_, this->type_, | |
2165 | this->location()); } | |
2166 | ||
2167 | tree | |
2168 | do_get_tree(Translate_context*); | |
2169 | ||
2170 | void | |
2171 | do_export(Export*) const; | |
2172 | ||
d751bb78 | 2173 | void |
2174 | do_dump_expression(Ast_dump_context*) const; | |
2175 | ||
e440a328 | 2176 | private: |
2177 | // The floating point value. | |
2178 | mpfr_t val_; | |
2179 | // The type so far. | |
2180 | Type* type_; | |
2181 | }; | |
2182 | ||
e440a328 | 2183 | // Return the current type. If we haven't set the type yet, we return |
2184 | // an abstract float type. | |
2185 | ||
2186 | Type* | |
2187 | Float_expression::do_type() | |
2188 | { | |
2189 | if (this->type_ == NULL) | |
2190 | this->type_ = Type::make_abstract_float_type(); | |
2191 | return this->type_; | |
2192 | } | |
2193 | ||
2194 | // Set the type of the float value. Here we may switch from an | |
2195 | // abstract type to a real type. | |
2196 | ||
2197 | void | |
2198 | Float_expression::do_determine_type(const Type_context* context) | |
2199 | { | |
2200 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2201 | ; | |
2202 | else if (context->type != NULL | |
2203 | && (context->type->integer_type() != NULL | |
2204 | || context->type->float_type() != NULL | |
2205 | || context->type->complex_type() != NULL)) | |
2206 | this->type_ = context->type; | |
2207 | else if (!context->may_be_abstract) | |
48080209 | 2208 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2209 | } |
2210 | ||
e440a328 | 2211 | // Check the type of a float value. |
2212 | ||
2213 | void | |
2214 | Float_expression::do_check_types(Gogo*) | |
2215 | { | |
0c77715b | 2216 | Type* type = this->type_; |
2217 | if (type == NULL) | |
e440a328 | 2218 | return; |
0c77715b | 2219 | Numeric_constant nc; |
2220 | nc.set_float(NULL, this->val_); | |
2221 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2222 | this->set_is_error(); |
e440a328 | 2223 | } |
2224 | ||
2225 | // Get a tree for a float constant. | |
2226 | ||
2227 | tree | |
2228 | Float_expression::do_get_tree(Translate_context* context) | |
2229 | { | |
48c2a53a | 2230 | Type* resolved_type; |
e440a328 | 2231 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2232 | resolved_type = this->type_; |
e440a328 | 2233 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2234 | { | |
2235 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2236 | resolved_type = Type::lookup_integer_type("int"); |
2237 | } | |
2238 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2239 | { | |
2240 | // We are converting to an abstract complex type. | |
2241 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2242 | } |
2243 | else | |
2244 | { | |
2245 | // If we still have an abstract type here, then this is being | |
2246 | // used in a constant expression which didn't get reduced. We | |
2247 | // just use float64 and hope for the best. | |
48c2a53a | 2248 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2249 | } |
48c2a53a | 2250 | |
2251 | Numeric_constant nc; | |
2252 | nc.set_float(resolved_type, this->val_); | |
2253 | Bexpression* ret = | |
2254 | Expression::backend_numeric_constant_expression(context, &nc); | |
2255 | return expr_to_tree(ret); | |
e440a328 | 2256 | } |
2257 | ||
8b1c301d | 2258 | // Write a floating point number to a string dump. |
e440a328 | 2259 | |
2260 | void | |
8b1c301d | 2261 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2262 | { |
2263 | mp_exp_t exponent; | |
2264 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2265 | if (*s == '-') | |
2266 | exp->write_c_string("-"); | |
2267 | exp->write_c_string("0."); | |
2268 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2269 | mpfr_free_str(s); | |
2270 | char buf[30]; | |
2271 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2272 | exp->write_c_string(buf); | |
2273 | } | |
2274 | ||
2275 | // Export a floating point number in a constant expression. | |
2276 | ||
2277 | void | |
2278 | Float_expression::do_export(Export* exp) const | |
2279 | { | |
2280 | Float_expression::export_float(exp, this->val_); | |
2281 | // A trailing space lets us reliably identify the end of the number. | |
2282 | exp->write_c_string(" "); | |
2283 | } | |
2284 | ||
d751bb78 | 2285 | // Dump a floating point number to the dump file. |
2286 | ||
2287 | void | |
2288 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2289 | { | |
8b1c301d | 2290 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2291 | } |
2292 | ||
e440a328 | 2293 | // Make a float expression. |
2294 | ||
2295 | Expression* | |
b13c66cd | 2296 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2297 | { |
2298 | return new Float_expression(val, type, location); | |
2299 | } | |
2300 | ||
2301 | // Complex numbers. | |
2302 | ||
2303 | class Complex_expression : public Expression | |
2304 | { | |
2305 | public: | |
2306 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2307 | Location location) |
e440a328 | 2308 | : Expression(EXPRESSION_COMPLEX, location), |
2309 | type_(type) | |
2310 | { | |
2311 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2312 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2313 | } | |
2314 | ||
8b1c301d | 2315 | // Write REAL/IMAG to string dump. |
e440a328 | 2316 | static void |
8b1c301d | 2317 | export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val); |
e440a328 | 2318 | |
d751bb78 | 2319 | // Write REAL/IMAG to dump context. |
2320 | static void | |
2321 | dump_complex(Ast_dump_context* ast_dump_context, | |
2322 | const mpfr_t real, const mpfr_t val); | |
2323 | ||
e440a328 | 2324 | protected: |
2325 | bool | |
2326 | do_is_constant() const | |
2327 | { return true; } | |
2328 | ||
0e168074 | 2329 | bool |
2330 | do_is_immutable() const | |
2331 | { return true; } | |
2332 | ||
e440a328 | 2333 | bool |
0c77715b | 2334 | do_numeric_constant_value(Numeric_constant* nc) const |
2335 | { | |
2336 | nc->set_complex(this->type_, this->real_, this->imag_); | |
2337 | return true; | |
2338 | } | |
e440a328 | 2339 | |
2340 | Type* | |
2341 | do_type(); | |
2342 | ||
2343 | void | |
2344 | do_determine_type(const Type_context*); | |
2345 | ||
2346 | void | |
2347 | do_check_types(Gogo*); | |
2348 | ||
2349 | Expression* | |
2350 | do_copy() | |
2351 | { | |
2352 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2353 | this->location()); | |
2354 | } | |
2355 | ||
2356 | tree | |
2357 | do_get_tree(Translate_context*); | |
2358 | ||
2359 | void | |
2360 | do_export(Export*) const; | |
2361 | ||
d751bb78 | 2362 | void |
2363 | do_dump_expression(Ast_dump_context*) const; | |
2364 | ||
e440a328 | 2365 | private: |
2366 | // The real part. | |
2367 | mpfr_t real_; | |
2368 | // The imaginary part; | |
2369 | mpfr_t imag_; | |
2370 | // The type if known. | |
2371 | Type* type_; | |
2372 | }; | |
2373 | ||
e440a328 | 2374 | // Return the current type. If we haven't set the type yet, we return |
2375 | // an abstract complex type. | |
2376 | ||
2377 | Type* | |
2378 | Complex_expression::do_type() | |
2379 | { | |
2380 | if (this->type_ == NULL) | |
2381 | this->type_ = Type::make_abstract_complex_type(); | |
2382 | return this->type_; | |
2383 | } | |
2384 | ||
2385 | // Set the type of the complex value. Here we may switch from an | |
2386 | // abstract type to a real type. | |
2387 | ||
2388 | void | |
2389 | Complex_expression::do_determine_type(const Type_context* context) | |
2390 | { | |
2391 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2392 | ; | |
2393 | else if (context->type != NULL | |
2394 | && context->type->complex_type() != NULL) | |
2395 | this->type_ = context->type; | |
2396 | else if (!context->may_be_abstract) | |
48080209 | 2397 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2398 | } |
2399 | ||
e440a328 | 2400 | // Check the type of a complex value. |
2401 | ||
2402 | void | |
2403 | Complex_expression::do_check_types(Gogo*) | |
2404 | { | |
0c77715b | 2405 | Type* type = this->type_; |
2406 | if (type == NULL) | |
e440a328 | 2407 | return; |
0c77715b | 2408 | Numeric_constant nc; |
2409 | nc.set_complex(NULL, this->real_, this->imag_); | |
2410 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2411 | this->set_is_error(); |
2412 | } | |
2413 | ||
2414 | // Get a tree for a complex constant. | |
2415 | ||
2416 | tree | |
2417 | Complex_expression::do_get_tree(Translate_context* context) | |
2418 | { | |
48c2a53a | 2419 | Type* resolved_type; |
e440a328 | 2420 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2421 | resolved_type = this->type_; |
2422 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2423 | { | |
2424 | // We are converting to an abstract integer type. | |
2425 | resolved_type = Type::lookup_integer_type("int"); | |
2426 | } | |
2427 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2428 | { | |
2429 | // We are converting to an abstract float type. | |
2430 | resolved_type = Type::lookup_float_type("float64"); | |
2431 | } | |
e440a328 | 2432 | else |
2433 | { | |
2434 | // If we still have an abstract type here, this this is being | |
2435 | // used in a constant expression which didn't get reduced. We | |
2436 | // just use complex128 and hope for the best. | |
48c2a53a | 2437 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2438 | } |
48c2a53a | 2439 | |
2440 | Numeric_constant nc; | |
2441 | nc.set_complex(resolved_type, this->real_, this->imag_); | |
2442 | Bexpression* ret = | |
2443 | Expression::backend_numeric_constant_expression(context, &nc); | |
2444 | return expr_to_tree(ret); | |
e440a328 | 2445 | } |
2446 | ||
2447 | // Write REAL/IMAG to export data. | |
2448 | ||
2449 | void | |
8b1c301d | 2450 | Complex_expression::export_complex(String_dump* exp, const mpfr_t real, |
e440a328 | 2451 | const mpfr_t imag) |
2452 | { | |
2453 | if (!mpfr_zero_p(real)) | |
2454 | { | |
2455 | Float_expression::export_float(exp, real); | |
2456 | if (mpfr_sgn(imag) > 0) | |
2457 | exp->write_c_string("+"); | |
2458 | } | |
2459 | Float_expression::export_float(exp, imag); | |
2460 | exp->write_c_string("i"); | |
2461 | } | |
2462 | ||
2463 | // Export a complex number in a constant expression. | |
2464 | ||
2465 | void | |
2466 | Complex_expression::do_export(Export* exp) const | |
2467 | { | |
2468 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2469 | // A trailing space lets us reliably identify the end of the number. | |
2470 | exp->write_c_string(" "); | |
2471 | } | |
2472 | ||
d751bb78 | 2473 | // Dump a complex expression to the dump file. |
2474 | ||
2475 | void | |
2476 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2477 | { | |
8b1c301d | 2478 | Complex_expression::export_complex(ast_dump_context, |
d751bb78 | 2479 | this->real_, |
2480 | this->imag_); | |
2481 | } | |
2482 | ||
e440a328 | 2483 | // Make a complex expression. |
2484 | ||
2485 | Expression* | |
2486 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2487 | Location location) |
e440a328 | 2488 | { |
2489 | return new Complex_expression(real, imag, type, location); | |
2490 | } | |
2491 | ||
d5b605df | 2492 | // Find a named object in an expression. |
2493 | ||
2494 | class Find_named_object : public Traverse | |
2495 | { | |
2496 | public: | |
2497 | Find_named_object(Named_object* no) | |
2498 | : Traverse(traverse_expressions), | |
2499 | no_(no), found_(false) | |
2500 | { } | |
2501 | ||
2502 | // Whether we found the object. | |
2503 | bool | |
2504 | found() const | |
2505 | { return this->found_; } | |
2506 | ||
2507 | protected: | |
2508 | int | |
2509 | expression(Expression**); | |
2510 | ||
2511 | private: | |
2512 | // The object we are looking for. | |
2513 | Named_object* no_; | |
2514 | // Whether we found it. | |
2515 | bool found_; | |
2516 | }; | |
2517 | ||
e440a328 | 2518 | // A reference to a const in an expression. |
2519 | ||
2520 | class Const_expression : public Expression | |
2521 | { | |
2522 | public: | |
b13c66cd | 2523 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2524 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2525 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2526 | { } |
2527 | ||
d5b605df | 2528 | Named_object* |
2529 | named_object() | |
2530 | { return this->constant_; } | |
2531 | ||
a7f064d5 | 2532 | // Check that the initializer does not refer to the constant itself. |
2533 | void | |
2534 | check_for_init_loop(); | |
2535 | ||
e440a328 | 2536 | protected: |
ba4aedd4 | 2537 | int |
2538 | do_traverse(Traverse*); | |
2539 | ||
e440a328 | 2540 | Expression* |
ceeb4318 | 2541 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2542 | |
2543 | bool | |
2544 | do_is_constant() const | |
2545 | { return true; } | |
2546 | ||
0e168074 | 2547 | bool |
2548 | do_is_immutable() const | |
2549 | { return true; } | |
2550 | ||
e440a328 | 2551 | bool |
0c77715b | 2552 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2553 | |
2554 | bool | |
af6b489a | 2555 | do_string_constant_value(std::string* val) const; |
e440a328 | 2556 | |
2557 | Type* | |
2558 | do_type(); | |
2559 | ||
2560 | // The type of a const is set by the declaration, not the use. | |
2561 | void | |
2562 | do_determine_type(const Type_context*); | |
2563 | ||
2564 | void | |
2565 | do_check_types(Gogo*); | |
2566 | ||
2567 | Expression* | |
2568 | do_copy() | |
2569 | { return this; } | |
2570 | ||
2571 | tree | |
2572 | do_get_tree(Translate_context* context); | |
2573 | ||
2574 | // When exporting a reference to a const as part of a const | |
2575 | // expression, we export the value. We ignore the fact that it has | |
2576 | // a name. | |
2577 | void | |
2578 | do_export(Export* exp) const | |
2579 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2580 | ||
d751bb78 | 2581 | void |
2582 | do_dump_expression(Ast_dump_context*) const; | |
2583 | ||
e440a328 | 2584 | private: |
2585 | // The constant. | |
2586 | Named_object* constant_; | |
2587 | // The type of this reference. This is used if the constant has an | |
2588 | // abstract type. | |
2589 | Type* type_; | |
13e818f5 | 2590 | // Used to prevent infinite recursion when a constant incorrectly |
2591 | // refers to itself. | |
2592 | mutable bool seen_; | |
e440a328 | 2593 | }; |
2594 | ||
ba4aedd4 | 2595 | // Traversal. |
2596 | ||
2597 | int | |
2598 | Const_expression::do_traverse(Traverse* traverse) | |
2599 | { | |
2600 | if (this->type_ != NULL) | |
2601 | return Type::traverse(this->type_, traverse); | |
2602 | return TRAVERSE_CONTINUE; | |
2603 | } | |
2604 | ||
e440a328 | 2605 | // Lower a constant expression. This is where we convert the |
2606 | // predeclared constant iota into an integer value. | |
2607 | ||
2608 | Expression* | |
ceeb4318 | 2609 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2610 | Statement_inserter*, int iota_value) | |
e440a328 | 2611 | { |
2612 | if (this->constant_->const_value()->expr()->classification() | |
2613 | == EXPRESSION_IOTA) | |
2614 | { | |
2615 | if (iota_value == -1) | |
2616 | { | |
2617 | error_at(this->location(), | |
2618 | "iota is only defined in const declarations"); | |
2619 | iota_value = 0; | |
2620 | } | |
2621 | mpz_t val; | |
2622 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2623 | Expression* ret = Expression::make_integer(&val, NULL, | |
2624 | this->location()); | |
2625 | mpz_clear(val); | |
2626 | return ret; | |
2627 | } | |
2628 | ||
2629 | // Make sure that the constant itself has been lowered. | |
2630 | gogo->lower_constant(this->constant_); | |
2631 | ||
2632 | return this; | |
2633 | } | |
2634 | ||
0c77715b | 2635 | // Return a numeric constant value. |
e440a328 | 2636 | |
2637 | bool | |
0c77715b | 2638 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2639 | { |
13e818f5 | 2640 | if (this->seen_) |
2641 | return false; | |
2642 | ||
e440a328 | 2643 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2644 | |
13e818f5 | 2645 | this->seen_ = true; |
2646 | ||
0c77715b | 2647 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2648 | |
13e818f5 | 2649 | this->seen_ = false; |
2650 | ||
e440a328 | 2651 | Type* ctype; |
2652 | if (this->type_ != NULL) | |
2653 | ctype = this->type_; | |
2654 | else | |
2655 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2656 | if (r && ctype != NULL) |
2657 | { | |
0c77715b | 2658 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2659 | return false; |
e440a328 | 2660 | } |
e440a328 | 2661 | |
e440a328 | 2662 | return r; |
2663 | } | |
2664 | ||
af6b489a | 2665 | bool |
2666 | Const_expression::do_string_constant_value(std::string* val) const | |
2667 | { | |
2668 | if (this->seen_) | |
2669 | return false; | |
2670 | ||
2671 | Expression* e = this->constant_->const_value()->expr(); | |
2672 | ||
2673 | this->seen_ = true; | |
2674 | bool ok = e->string_constant_value(val); | |
2675 | this->seen_ = false; | |
2676 | ||
2677 | return ok; | |
2678 | } | |
2679 | ||
e440a328 | 2680 | // Return the type of the const reference. |
2681 | ||
2682 | Type* | |
2683 | Const_expression::do_type() | |
2684 | { | |
2685 | if (this->type_ != NULL) | |
2686 | return this->type_; | |
13e818f5 | 2687 | |
2f78f012 | 2688 | Named_constant* nc = this->constant_->const_value(); |
2689 | ||
2690 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2691 | { |
2692 | this->report_error(_("constant refers to itself")); | |
2693 | this->type_ = Type::make_error_type(); | |
2694 | return this->type_; | |
2695 | } | |
2696 | ||
2697 | this->seen_ = true; | |
2698 | ||
e440a328 | 2699 | Type* ret = nc->type(); |
13e818f5 | 2700 | |
e440a328 | 2701 | if (ret != NULL) |
13e818f5 | 2702 | { |
2703 | this->seen_ = false; | |
2704 | return ret; | |
2705 | } | |
2706 | ||
e440a328 | 2707 | // During parsing, a named constant may have a NULL type, but we |
2708 | // must not return a NULL type here. | |
13e818f5 | 2709 | ret = nc->expr()->type(); |
2710 | ||
2711 | this->seen_ = false; | |
2712 | ||
2713 | return ret; | |
e440a328 | 2714 | } |
2715 | ||
2716 | // Set the type of the const reference. | |
2717 | ||
2718 | void | |
2719 | Const_expression::do_determine_type(const Type_context* context) | |
2720 | { | |
2721 | Type* ctype = this->constant_->const_value()->type(); | |
2722 | Type* cetype = (ctype != NULL | |
2723 | ? ctype | |
2724 | : this->constant_->const_value()->expr()->type()); | |
2725 | if (ctype != NULL && !ctype->is_abstract()) | |
2726 | ; | |
2727 | else if (context->type != NULL | |
0c77715b | 2728 | && context->type->is_numeric_type() |
2729 | && cetype->is_numeric_type()) | |
e440a328 | 2730 | this->type_ = context->type; |
2731 | else if (context->type != NULL | |
2732 | && context->type->is_string_type() | |
2733 | && cetype->is_string_type()) | |
2734 | this->type_ = context->type; | |
2735 | else if (context->type != NULL | |
2736 | && context->type->is_boolean_type() | |
2737 | && cetype->is_boolean_type()) | |
2738 | this->type_ = context->type; | |
2739 | else if (!context->may_be_abstract) | |
2740 | { | |
2741 | if (cetype->is_abstract()) | |
2742 | cetype = cetype->make_non_abstract_type(); | |
2743 | this->type_ = cetype; | |
2744 | } | |
2745 | } | |
2746 | ||
a7f064d5 | 2747 | // Check for a loop in which the initializer of a constant refers to |
2748 | // the constant itself. | |
e440a328 | 2749 | |
2750 | void | |
a7f064d5 | 2751 | Const_expression::check_for_init_loop() |
e440a328 | 2752 | { |
5c13bd80 | 2753 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2754 | return; |
2755 | ||
a7f064d5 | 2756 | if (this->seen_) |
2757 | { | |
2758 | this->report_error(_("constant refers to itself")); | |
2759 | this->type_ = Type::make_error_type(); | |
2760 | return; | |
2761 | } | |
2762 | ||
d5b605df | 2763 | Expression* init = this->constant_->const_value()->expr(); |
2764 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2765 | |
2766 | this->seen_ = true; | |
d5b605df | 2767 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2768 | this->seen_ = false; |
2769 | ||
d5b605df | 2770 | if (find_named_object.found()) |
2771 | { | |
5c13bd80 | 2772 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2773 | { |
2774 | this->report_error(_("constant refers to itself")); | |
2775 | this->type_ = Type::make_error_type(); | |
2776 | } | |
d5b605df | 2777 | return; |
2778 | } | |
a7f064d5 | 2779 | } |
2780 | ||
2781 | // Check types of a const reference. | |
2782 | ||
2783 | void | |
2784 | Const_expression::do_check_types(Gogo*) | |
2785 | { | |
5c13bd80 | 2786 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2787 | return; |
2788 | ||
2789 | this->check_for_init_loop(); | |
d5b605df | 2790 | |
0c77715b | 2791 | // Check that numeric constant fits in type. |
2792 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2793 | { |
0c77715b | 2794 | Numeric_constant nc; |
2795 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2796 | { |
0c77715b | 2797 | if (!nc.set_type(this->type_, true, this->location())) |
2798 | this->set_is_error(); | |
e440a328 | 2799 | } |
e440a328 | 2800 | } |
2801 | } | |
2802 | ||
2803 | // Return a tree for the const reference. | |
2804 | ||
2805 | tree | |
2806 | Const_expression::do_get_tree(Translate_context* context) | |
2807 | { | |
2c809f8f | 2808 | if (this->type_ != NULL && this->type_->is_error()) |
2809 | return error_mark_node; | |
e440a328 | 2810 | |
2811 | // If the type has been set for this expression, but the underlying | |
2812 | // object is an abstract int or float, we try to get the abstract | |
2813 | // value. Otherwise we may lose something in the conversion. | |
f2de4532 | 2814 | Expression* expr = this->constant_->const_value()->expr(); |
e440a328 | 2815 | if (this->type_ != NULL |
0c77715b | 2816 | && this->type_->is_numeric_type() |
a68492b4 | 2817 | && (this->constant_->const_value()->type() == NULL |
2818 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2819 | { |
0c77715b | 2820 | Numeric_constant nc; |
2821 | if (expr->numeric_constant_value(&nc) | |
2822 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2823 | { |
0c77715b | 2824 | Expression* e = nc.expression(this->location()); |
2825 | return e->get_tree(context); | |
e440a328 | 2826 | } |
e440a328 | 2827 | } |
2828 | ||
2c809f8f | 2829 | if (this->type_ != NULL) |
f2de4532 | 2830 | expr = Expression::make_cast(this->type_, expr, this->location()); |
2831 | return expr->get_tree(context); | |
e440a328 | 2832 | } |
2833 | ||
d751bb78 | 2834 | // Dump ast representation for constant expression. |
2835 | ||
2836 | void | |
2837 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2838 | { | |
2839 | ast_dump_context->ostream() << this->constant_->name(); | |
2840 | } | |
2841 | ||
e440a328 | 2842 | // Make a reference to a constant in an expression. |
2843 | ||
2844 | Expression* | |
2845 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2846 | Location location) |
e440a328 | 2847 | { |
2848 | return new Const_expression(constant, location); | |
2849 | } | |
2850 | ||
d5b605df | 2851 | // Find a named object in an expression. |
2852 | ||
2853 | int | |
2854 | Find_named_object::expression(Expression** pexpr) | |
2855 | { | |
2856 | switch ((*pexpr)->classification()) | |
2857 | { | |
2858 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2859 | { |
2860 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2861 | if (ce->named_object() == this->no_) | |
2862 | break; | |
2863 | ||
2864 | // We need to check a constant initializer explicitly, as | |
2865 | // loops here will not be caught by the loop checking for | |
2866 | // variable initializers. | |
2867 | ce->check_for_init_loop(); | |
2868 | ||
2869 | return TRAVERSE_CONTINUE; | |
2870 | } | |
2871 | ||
d5b605df | 2872 | case Expression::EXPRESSION_VAR_REFERENCE: |
2873 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2874 | break; | |
2875 | return TRAVERSE_CONTINUE; | |
2876 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2877 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2878 | break; | |
2879 | return TRAVERSE_CONTINUE; | |
2880 | default: | |
2881 | return TRAVERSE_CONTINUE; | |
2882 | } | |
2883 | this->found_ = true; | |
2884 | return TRAVERSE_EXIT; | |
2885 | } | |
2886 | ||
e440a328 | 2887 | // The nil value. |
2888 | ||
2889 | class Nil_expression : public Expression | |
2890 | { | |
2891 | public: | |
b13c66cd | 2892 | Nil_expression(Location location) |
e440a328 | 2893 | : Expression(EXPRESSION_NIL, location) |
2894 | { } | |
2895 | ||
2896 | static Expression* | |
2897 | do_import(Import*); | |
2898 | ||
2899 | protected: | |
2900 | bool | |
2901 | do_is_constant() const | |
2902 | { return true; } | |
2903 | ||
f9ca30f9 | 2904 | bool |
2905 | do_is_immutable() const | |
2906 | { return true; } | |
2907 | ||
e440a328 | 2908 | Type* |
2909 | do_type() | |
2910 | { return Type::make_nil_type(); } | |
2911 | ||
2912 | void | |
2913 | do_determine_type(const Type_context*) | |
2914 | { } | |
2915 | ||
2916 | Expression* | |
2917 | do_copy() | |
2918 | { return this; } | |
2919 | ||
2920 | tree | |
2921 | do_get_tree(Translate_context*) | |
2922 | { return null_pointer_node; } | |
2923 | ||
2924 | void | |
2925 | do_export(Export* exp) const | |
2926 | { exp->write_c_string("nil"); } | |
d751bb78 | 2927 | |
2928 | void | |
2929 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2930 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 2931 | }; |
2932 | ||
2933 | // Import a nil expression. | |
2934 | ||
2935 | Expression* | |
2936 | Nil_expression::do_import(Import* imp) | |
2937 | { | |
2938 | imp->require_c_string("nil"); | |
2939 | return Expression::make_nil(imp->location()); | |
2940 | } | |
2941 | ||
2942 | // Make a nil expression. | |
2943 | ||
2944 | Expression* | |
b13c66cd | 2945 | Expression::make_nil(Location location) |
e440a328 | 2946 | { |
2947 | return new Nil_expression(location); | |
2948 | } | |
2949 | ||
2950 | // The value of the predeclared constant iota. This is little more | |
2951 | // than a marker. This will be lowered to an integer in | |
2952 | // Const_expression::do_lower, which is where we know the value that | |
2953 | // it should have. | |
2954 | ||
2955 | class Iota_expression : public Parser_expression | |
2956 | { | |
2957 | public: | |
b13c66cd | 2958 | Iota_expression(Location location) |
e440a328 | 2959 | : Parser_expression(EXPRESSION_IOTA, location) |
2960 | { } | |
2961 | ||
2962 | protected: | |
2963 | Expression* | |
ceeb4318 | 2964 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 2965 | { go_unreachable(); } |
e440a328 | 2966 | |
2967 | // There should only ever be one of these. | |
2968 | Expression* | |
2969 | do_copy() | |
c3e6f413 | 2970 | { go_unreachable(); } |
d751bb78 | 2971 | |
2972 | void | |
2973 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2974 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 2975 | }; |
2976 | ||
2977 | // Make an iota expression. This is only called for one case: the | |
2978 | // value of the predeclared constant iota. | |
2979 | ||
2980 | Expression* | |
2981 | Expression::make_iota() | |
2982 | { | |
b13c66cd | 2983 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 2984 | return &iota_expression; |
2985 | } | |
2986 | ||
2987 | // A type conversion expression. | |
2988 | ||
2989 | class Type_conversion_expression : public Expression | |
2990 | { | |
2991 | public: | |
2992 | Type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 2993 | Location location) |
e440a328 | 2994 | : Expression(EXPRESSION_CONVERSION, location), |
2995 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2996 | { } | |
2997 | ||
2998 | // Return the type to which we are converting. | |
2999 | Type* | |
3000 | type() const | |
3001 | { return this->type_; } | |
3002 | ||
3003 | // Return the expression which we are converting. | |
3004 | Expression* | |
3005 | expr() const | |
3006 | { return this->expr_; } | |
3007 | ||
3008 | // Permit converting from one function type to another. This is | |
3009 | // used internally for method expressions. | |
3010 | void | |
3011 | set_may_convert_function_types() | |
3012 | { | |
3013 | this->may_convert_function_types_ = true; | |
3014 | } | |
3015 | ||
3016 | // Import a type conversion expression. | |
3017 | static Expression* | |
3018 | do_import(Import*); | |
3019 | ||
3020 | protected: | |
3021 | int | |
3022 | do_traverse(Traverse* traverse); | |
3023 | ||
3024 | Expression* | |
ceeb4318 | 3025 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 3026 | |
35a54f17 | 3027 | Expression* |
3028 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
3029 | ||
e440a328 | 3030 | bool |
1ca01a59 | 3031 | do_is_constant() const; |
e440a328 | 3032 | |
0e168074 | 3033 | bool |
3034 | do_is_immutable() const; | |
3035 | ||
e440a328 | 3036 | bool |
0c77715b | 3037 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 3038 | |
3039 | bool | |
3040 | do_string_constant_value(std::string*) const; | |
3041 | ||
3042 | Type* | |
3043 | do_type() | |
3044 | { return this->type_; } | |
3045 | ||
3046 | void | |
3047 | do_determine_type(const Type_context*) | |
3048 | { | |
3049 | Type_context subcontext(this->type_, false); | |
3050 | this->expr_->determine_type(&subcontext); | |
3051 | } | |
3052 | ||
3053 | void | |
3054 | do_check_types(Gogo*); | |
3055 | ||
3056 | Expression* | |
3057 | do_copy() | |
3058 | { | |
3059 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
3060 | this->location()); | |
3061 | } | |
3062 | ||
3063 | tree | |
3064 | do_get_tree(Translate_context* context); | |
3065 | ||
3066 | void | |
3067 | do_export(Export*) const; | |
3068 | ||
d751bb78 | 3069 | void |
3070 | do_dump_expression(Ast_dump_context*) const; | |
3071 | ||
e440a328 | 3072 | private: |
3073 | // The type to convert to. | |
3074 | Type* type_; | |
3075 | // The expression to convert. | |
3076 | Expression* expr_; | |
3077 | // True if this is permitted to convert function types. This is | |
3078 | // used internally for method expressions. | |
3079 | bool may_convert_function_types_; | |
3080 | }; | |
3081 | ||
3082 | // Traversal. | |
3083 | ||
3084 | int | |
3085 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3086 | { | |
3087 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3088 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3089 | return TRAVERSE_EXIT; | |
3090 | return TRAVERSE_CONTINUE; | |
3091 | } | |
3092 | ||
3093 | // Convert to a constant at lowering time. | |
3094 | ||
3095 | Expression* | |
ceeb4318 | 3096 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3097 | Statement_inserter*, int) | |
e440a328 | 3098 | { |
3099 | Type* type = this->type_; | |
3100 | Expression* val = this->expr_; | |
b13c66cd | 3101 | Location location = this->location(); |
e440a328 | 3102 | |
0c77715b | 3103 | if (type->is_numeric_type()) |
e440a328 | 3104 | { |
0c77715b | 3105 | Numeric_constant nc; |
3106 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3107 | { |
0c77715b | 3108 | if (!nc.set_type(type, true, location)) |
3109 | return Expression::make_error(location); | |
3110 | return nc.expression(location); | |
e440a328 | 3111 | } |
e440a328 | 3112 | } |
3113 | ||
55072f2b | 3114 | if (type->is_slice_type()) |
e440a328 | 3115 | { |
3116 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3117 | bool is_byte = (element_type->integer_type() != NULL |
3118 | && element_type->integer_type()->is_byte()); | |
3119 | bool is_rune = (element_type->integer_type() != NULL | |
3120 | && element_type->integer_type()->is_rune()); | |
3121 | if (is_byte || is_rune) | |
e440a328 | 3122 | { |
3123 | std::string s; | |
3124 | if (val->string_constant_value(&s)) | |
3125 | { | |
3126 | Expression_list* vals = new Expression_list(); | |
3127 | if (is_byte) | |
3128 | { | |
3129 | for (std::string::const_iterator p = s.begin(); | |
3130 | p != s.end(); | |
3131 | p++) | |
3132 | { | |
3133 | mpz_t val; | |
3134 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
3135 | Expression* v = Expression::make_integer(&val, | |
3136 | element_type, | |
3137 | location); | |
3138 | vals->push_back(v); | |
3139 | mpz_clear(val); | |
3140 | } | |
3141 | } | |
3142 | else | |
3143 | { | |
3144 | const char *p = s.data(); | |
3145 | const char *pend = s.data() + s.length(); | |
3146 | while (p < pend) | |
3147 | { | |
3148 | unsigned int c; | |
3149 | int adv = Lex::fetch_char(p, &c); | |
3150 | if (adv == 0) | |
3151 | { | |
3152 | warning_at(this->location(), 0, | |
3153 | "invalid UTF-8 encoding"); | |
3154 | adv = 1; | |
3155 | } | |
3156 | p += adv; | |
3157 | mpz_t val; | |
3158 | mpz_init_set_ui(val, c); | |
3159 | Expression* v = Expression::make_integer(&val, | |
3160 | element_type, | |
3161 | location); | |
3162 | vals->push_back(v); | |
3163 | mpz_clear(val); | |
3164 | } | |
3165 | } | |
3166 | ||
3167 | return Expression::make_slice_composite_literal(type, vals, | |
3168 | location); | |
3169 | } | |
3170 | } | |
3171 | } | |
3172 | ||
3173 | return this; | |
3174 | } | |
3175 | ||
35a54f17 | 3176 | // Flatten a type conversion by using a temporary variable for the slice |
3177 | // in slice to string conversions. | |
3178 | ||
3179 | Expression* | |
3180 | Type_conversion_expression::do_flatten(Gogo*, Named_object*, | |
3181 | Statement_inserter* inserter) | |
3182 | { | |
2c809f8f | 3183 | if (((this->type()->is_string_type() |
3184 | && this->expr_->type()->is_slice_type()) | |
3185 | || (this->type()->interface_type() != NULL | |
3186 | && this->expr_->type()->interface_type() != NULL)) | |
35a54f17 | 3187 | && !this->expr_->is_variable()) |
3188 | { | |
3189 | Temporary_statement* temp = | |
3190 | Statement::make_temporary(NULL, this->expr_, this->location()); | |
3191 | inserter->insert(temp); | |
3192 | this->expr_ = Expression::make_temporary_reference(temp, this->location()); | |
3193 | } | |
3194 | return this; | |
3195 | } | |
3196 | ||
1ca01a59 | 3197 | // Return whether a type conversion is a constant. |
3198 | ||
3199 | bool | |
3200 | Type_conversion_expression::do_is_constant() const | |
3201 | { | |
3202 | if (!this->expr_->is_constant()) | |
3203 | return false; | |
3204 | ||
3205 | // A conversion to a type that may not be used as a constant is not | |
3206 | // a constant. For example, []byte(nil). | |
3207 | Type* type = this->type_; | |
3208 | if (type->integer_type() == NULL | |
3209 | && type->float_type() == NULL | |
3210 | && type->complex_type() == NULL | |
3211 | && !type->is_boolean_type() | |
3212 | && !type->is_string_type()) | |
3213 | return false; | |
3214 | ||
3215 | return true; | |
3216 | } | |
3217 | ||
0e168074 | 3218 | // Return whether a type conversion is immutable. |
3219 | ||
3220 | bool | |
3221 | Type_conversion_expression::do_is_immutable() const | |
3222 | { | |
3223 | Type* type = this->type_; | |
3224 | Type* expr_type = this->expr_->type(); | |
3225 | ||
3226 | if (type->interface_type() != NULL | |
3227 | || expr_type->interface_type() != NULL) | |
3228 | return false; | |
3229 | ||
3230 | if (!this->expr_->is_immutable()) | |
3231 | return false; | |
3232 | ||
3233 | if (Type::are_identical(type, expr_type, false, NULL)) | |
3234 | return true; | |
3235 | ||
3236 | return type->is_basic_type() && expr_type->is_basic_type(); | |
3237 | } | |
3238 | ||
0c77715b | 3239 | // Return the constant numeric value if there is one. |
e440a328 | 3240 | |
3241 | bool | |
0c77715b | 3242 | Type_conversion_expression::do_numeric_constant_value( |
3243 | Numeric_constant* nc) const | |
e440a328 | 3244 | { |
0c77715b | 3245 | if (!this->type_->is_numeric_type()) |
e440a328 | 3246 | return false; |
0c77715b | 3247 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3248 | return false; |
0c77715b | 3249 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3250 | } |
3251 | ||
3252 | // Return the constant string value if there is one. | |
3253 | ||
3254 | bool | |
3255 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3256 | { | |
3257 | if (this->type_->is_string_type() | |
3258 | && this->expr_->type()->integer_type() != NULL) | |
3259 | { | |
0c77715b | 3260 | Numeric_constant nc; |
3261 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3262 | { |
0c77715b | 3263 | unsigned long ival; |
3264 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3265 | { |
0c77715b | 3266 | val->clear(); |
3267 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3268 | return true; |
3269 | } | |
3270 | } | |
e440a328 | 3271 | } |
3272 | ||
3273 | // FIXME: Could handle conversion from const []int here. | |
3274 | ||
3275 | return false; | |
3276 | } | |
3277 | ||
3278 | // Check that types are convertible. | |
3279 | ||
3280 | void | |
3281 | Type_conversion_expression::do_check_types(Gogo*) | |
3282 | { | |
3283 | Type* type = this->type_; | |
3284 | Type* expr_type = this->expr_->type(); | |
3285 | std::string reason; | |
3286 | ||
5c13bd80 | 3287 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3288 | { |
842f6425 | 3289 | this->set_is_error(); |
3290 | return; | |
3291 | } | |
3292 | ||
e440a328 | 3293 | if (this->may_convert_function_types_ |
3294 | && type->function_type() != NULL | |
3295 | && expr_type->function_type() != NULL) | |
3296 | return; | |
3297 | ||
3298 | if (Type::are_convertible(type, expr_type, &reason)) | |
3299 | return; | |
3300 | ||
3301 | error_at(this->location(), "%s", reason.c_str()); | |
3302 | this->set_is_error(); | |
3303 | } | |
3304 | ||
3305 | // Get a tree for a type conversion. | |
3306 | ||
3307 | tree | |
3308 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3309 | { | |
e440a328 | 3310 | Type* type = this->type_; |
3311 | Type* expr_type = this->expr_->type(); | |
2c809f8f | 3312 | |
3313 | Gogo* gogo = context->gogo(); | |
3314 | Btype* btype = type->get_backend(gogo); | |
3315 | Bexpression* bexpr = tree_to_expr(this->expr_->get_tree(context)); | |
3316 | Location loc = this->location(); | |
3317 | ||
3318 | if (Type::are_identical(type, expr_type, false, NULL)) | |
e440a328 | 3319 | { |
2c809f8f | 3320 | Bexpression* bconvert = |
3321 | gogo->backend()->convert_expression(btype, bexpr, loc); | |
3322 | return expr_to_tree(bconvert); | |
e440a328 | 3323 | } |
2c809f8f | 3324 | else if (type->interface_type() != NULL |
3325 | || expr_type->interface_type() != NULL) | |
e440a328 | 3326 | { |
2c809f8f | 3327 | Expression* conversion = |
3328 | Expression::convert_for_assignment(gogo, type, this->expr_, | |
3329 | this->location()); | |
3330 | return conversion->get_tree(context); | |
e440a328 | 3331 | } |
3332 | else if (type->is_string_type() | |
3333 | && expr_type->integer_type() != NULL) | |
3334 | { | |
2c809f8f | 3335 | mpz_t intval; |
3336 | Numeric_constant nc; | |
3337 | if (this->expr_->numeric_constant_value(&nc) | |
3338 | && nc.to_int(&intval) | |
3339 | && mpz_fits_ushort_p(intval)) | |
e440a328 | 3340 | { |
e440a328 | 3341 | std::string s; |
2c809f8f | 3342 | Lex::append_char(mpz_get_ui(intval), true, &s, loc); |
3343 | mpz_clear(intval); | |
3344 | Expression* se = Expression::make_string(s, loc); | |
e440a328 | 3345 | return se->get_tree(context); |
3346 | } | |
3347 | ||
f16ab008 | 3348 | Expression* i2s_expr = |
2c809f8f | 3349 | Runtime::make_call(Runtime::INT_TO_STRING, loc, 1, this->expr_); |
3350 | return Expression::make_cast(type, i2s_expr, loc)->get_tree(context); | |
e440a328 | 3351 | } |
55072f2b | 3352 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3353 | { |
55072f2b | 3354 | Array_type* a = expr_type->array_type(); |
e440a328 | 3355 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3356 | go_assert(e->integer_type() != NULL); |
35a54f17 | 3357 | go_assert(this->expr_->is_variable()); |
3358 | ||
3359 | Runtime::Function code; | |
60963afd | 3360 | if (e->integer_type()->is_byte()) |
35a54f17 | 3361 | code = Runtime::BYTE_ARRAY_TO_STRING; |
e440a328 | 3362 | else |
35a54f17 | 3363 | { |
3364 | go_assert(e->integer_type()->is_rune()); | |
3365 | code = Runtime::INT_ARRAY_TO_STRING; | |
3366 | } | |
3367 | Expression* valptr = a->get_value_pointer(gogo, this->expr_); | |
3368 | Expression* len = a->get_length(gogo, this->expr_); | |
2c809f8f | 3369 | return Runtime::make_call(code, loc, 2, valptr, len)->get_tree(context); |
e440a328 | 3370 | } |
411eb89e | 3371 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3372 | { |
3373 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3374 | go_assert(e->integer_type() != NULL); |
6c252e42 | 3375 | |
2c809f8f | 3376 | Runtime::Function code; |
60963afd | 3377 | if (e->integer_type()->is_byte()) |
2c809f8f | 3378 | code = Runtime::STRING_TO_BYTE_ARRAY; |
e440a328 | 3379 | else |
3380 | { | |
60963afd | 3381 | go_assert(e->integer_type()->is_rune()); |
2c809f8f | 3382 | code = Runtime::STRING_TO_INT_ARRAY; |
e440a328 | 3383 | } |
2c809f8f | 3384 | Expression* s2a = Runtime::make_call(code, loc, 1, this->expr_); |
3385 | return Expression::make_unsafe_cast(type, s2a, loc)->get_tree(context); | |
3386 | } | |
3387 | else if (type->is_numeric_type()) | |
3388 | { | |
3389 | go_assert(Type::are_convertible(type, expr_type, NULL)); | |
3390 | Bexpression* bconvert = | |
3391 | gogo->backend()->convert_expression(btype, bexpr, loc); | |
3392 | return expr_to_tree(bconvert); | |
e440a328 | 3393 | } |
3394 | else if ((type->is_unsafe_pointer_type() | |
2c809f8f | 3395 | && (expr_type->points_to() != NULL |
3396 | || expr_type->integer_type())) | |
3397 | || (expr_type->is_unsafe_pointer_type() | |
3398 | && type->points_to() != NULL) | |
3399 | || (this->may_convert_function_types_ | |
3400 | && type->function_type() != NULL | |
3401 | && expr_type->function_type() != NULL)) | |
3402 | { | |
3403 | Bexpression* bconvert = | |
3404 | gogo->backend()->convert_expression(btype, bexpr, loc); | |
3405 | return expr_to_tree(bconvert); | |
3406 | } | |
e440a328 | 3407 | else |
2c809f8f | 3408 | { |
3409 | Expression* conversion = | |
3410 | Expression::convert_for_assignment(gogo, type, this->expr_, loc); | |
3411 | return conversion->get_tree(context); | |
3412 | } | |
e440a328 | 3413 | } |
3414 | ||
3415 | // Output a type conversion in a constant expression. | |
3416 | ||
3417 | void | |
3418 | Type_conversion_expression::do_export(Export* exp) const | |
3419 | { | |
3420 | exp->write_c_string("convert("); | |
3421 | exp->write_type(this->type_); | |
3422 | exp->write_c_string(", "); | |
3423 | this->expr_->export_expression(exp); | |
3424 | exp->write_c_string(")"); | |
3425 | } | |
3426 | ||
3427 | // Import a type conversion or a struct construction. | |
3428 | ||
3429 | Expression* | |
3430 | Type_conversion_expression::do_import(Import* imp) | |
3431 | { | |
3432 | imp->require_c_string("convert("); | |
3433 | Type* type = imp->read_type(); | |
3434 | imp->require_c_string(", "); | |
3435 | Expression* val = Expression::import_expression(imp); | |
3436 | imp->require_c_string(")"); | |
3437 | return Expression::make_cast(type, val, imp->location()); | |
3438 | } | |
3439 | ||
d751bb78 | 3440 | // Dump ast representation for a type conversion expression. |
3441 | ||
3442 | void | |
3443 | Type_conversion_expression::do_dump_expression( | |
3444 | Ast_dump_context* ast_dump_context) const | |
3445 | { | |
3446 | ast_dump_context->dump_type(this->type_); | |
3447 | ast_dump_context->ostream() << "("; | |
3448 | ast_dump_context->dump_expression(this->expr_); | |
3449 | ast_dump_context->ostream() << ") "; | |
3450 | } | |
3451 | ||
e440a328 | 3452 | // Make a type cast expression. |
3453 | ||
3454 | Expression* | |
b13c66cd | 3455 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3456 | { |
3457 | if (type->is_error_type() || val->is_error_expression()) | |
3458 | return Expression::make_error(location); | |
3459 | return new Type_conversion_expression(type, val, location); | |
3460 | } | |
3461 | ||
9581e91d | 3462 | // An unsafe type conversion, used to pass values to builtin functions. |
3463 | ||
3464 | class Unsafe_type_conversion_expression : public Expression | |
3465 | { | |
3466 | public: | |
3467 | Unsafe_type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 3468 | Location location) |
9581e91d | 3469 | : Expression(EXPRESSION_UNSAFE_CONVERSION, location), |
3470 | type_(type), expr_(expr) | |
3471 | { } | |
3472 | ||
3473 | protected: | |
3474 | int | |
3475 | do_traverse(Traverse* traverse); | |
3476 | ||
3477 | Type* | |
3478 | do_type() | |
3479 | { return this->type_; } | |
3480 | ||
3481 | void | |
3482 | do_determine_type(const Type_context*) | |
a9182619 | 3483 | { this->expr_->determine_type_no_context(); } |
9581e91d | 3484 | |
3485 | Expression* | |
3486 | do_copy() | |
3487 | { | |
3488 | return new Unsafe_type_conversion_expression(this->type_, | |
3489 | this->expr_->copy(), | |
3490 | this->location()); | |
3491 | } | |
3492 | ||
3493 | tree | |
3494 | do_get_tree(Translate_context*); | |
3495 | ||
d751bb78 | 3496 | void |
3497 | do_dump_expression(Ast_dump_context*) const; | |
3498 | ||
9581e91d | 3499 | private: |
3500 | // The type to convert to. | |
3501 | Type* type_; | |
3502 | // The expression to convert. | |
3503 | Expression* expr_; | |
3504 | }; | |
3505 | ||
3506 | // Traversal. | |
3507 | ||
3508 | int | |
3509 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3510 | { | |
3511 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3512 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3513 | return TRAVERSE_EXIT; | |
3514 | return TRAVERSE_CONTINUE; | |
3515 | } | |
3516 | ||
3517 | // Convert to backend representation. | |
3518 | ||
3519 | tree | |
3520 | Unsafe_type_conversion_expression::do_get_tree(Translate_context* context) | |
3521 | { | |
3522 | // We are only called for a limited number of cases. | |
3523 | ||
3524 | Type* t = this->type_; | |
3525 | Type* et = this->expr_->type(); | |
2c809f8f | 3526 | if (t->array_type() != NULL) |
3527 | go_assert(et->array_type() != NULL | |
3528 | && t->is_slice_type() == et->is_slice_type()); | |
3529 | else if (t->struct_type() != NULL) | |
9581e91d | 3530 | { |
2c809f8f | 3531 | if (t->named_type() != NULL |
3532 | && et->named_type() != NULL | |
3533 | && !Type::are_convertible(t, et, NULL)) | |
3534 | { | |
3535 | go_assert(saw_errors()); | |
3536 | return error_mark_node; | |
3537 | } | |
3538 | ||
3539 | go_assert(et->struct_type() != NULL | |
3540 | && Type::are_convertible(t, et, NULL)); | |
3541 | } | |
3542 | else if (t->map_type() != NULL) | |
c484d925 | 3543 | go_assert(et->map_type() != NULL); |
9581e91d | 3544 | else if (t->channel_type() != NULL) |
c484d925 | 3545 | go_assert(et->channel_type() != NULL); |
09ea332d | 3546 | else if (t->points_to() != NULL) |
2c809f8f | 3547 | go_assert(et->points_to() != NULL |
3548 | || et->channel_type() != NULL | |
3549 | || et->map_type() != NULL | |
3550 | || et->function_type() != NULL | |
3551 | || et->is_nil_type()); | |
9581e91d | 3552 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3553 | go_assert(t->points_to() != NULL); |
2c809f8f | 3554 | else if (t->interface_type() != NULL) |
9581e91d | 3555 | { |
2c809f8f | 3556 | bool empty_iface = t->interface_type()->is_empty(); |
c484d925 | 3557 | go_assert(et->interface_type() != NULL |
2c809f8f | 3558 | && et->interface_type()->is_empty() == empty_iface); |
9581e91d | 3559 | } |
588e3cf9 | 3560 | else if (t->integer_type() != NULL) |
2c809f8f | 3561 | go_assert(et->is_boolean_type() |
3562 | || et->integer_type() != NULL | |
3563 | || et->function_type() != NULL | |
3564 | || et->points_to() != NULL | |
3565 | || et->map_type() != NULL | |
3566 | || et->channel_type() != NULL); | |
9581e91d | 3567 | else |
c3e6f413 | 3568 | go_unreachable(); |
9581e91d | 3569 | |
2c809f8f | 3570 | Gogo* gogo = context->gogo(); |
3571 | Btype* btype = t->get_backend(gogo); | |
3572 | Bexpression* bexpr = tree_to_expr(this->expr_->get_tree(context)); | |
3573 | Location loc = this->location(); | |
3574 | Bexpression* ret = | |
3575 | gogo->backend()->convert_expression(btype, bexpr, loc); | |
3576 | return expr_to_tree(ret); | |
9581e91d | 3577 | } |
3578 | ||
d751bb78 | 3579 | // Dump ast representation for an unsafe type conversion expression. |
3580 | ||
3581 | void | |
3582 | Unsafe_type_conversion_expression::do_dump_expression( | |
3583 | Ast_dump_context* ast_dump_context) const | |
3584 | { | |
3585 | ast_dump_context->dump_type(this->type_); | |
3586 | ast_dump_context->ostream() << "("; | |
3587 | ast_dump_context->dump_expression(this->expr_); | |
3588 | ast_dump_context->ostream() << ") "; | |
3589 | } | |
3590 | ||
9581e91d | 3591 | // Make an unsafe type conversion expression. |
3592 | ||
3593 | Expression* | |
3594 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3595 | Location location) |
9581e91d | 3596 | { |
3597 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3598 | } | |
3599 | ||
76f85fd6 | 3600 | // Class Unary_expression. |
e440a328 | 3601 | |
3602 | // If we are taking the address of a composite literal, and the | |
2c809f8f | 3603 | // contents are not constant, then we want to make a heap expression |
e440a328 | 3604 | // instead. |
3605 | ||
3606 | Expression* | |
ceeb4318 | 3607 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3608 | { |
b13c66cd | 3609 | Location loc = this->location(); |
e440a328 | 3610 | Operator op = this->op_; |
3611 | Expression* expr = this->expr_; | |
3612 | ||
3613 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3614 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3615 | ||
3616 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3617 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3618 | // analysis here? This case is found in math/unsafe.go and is | |
3619 | // therefore worth special casing. | |
3620 | if (op == OPERATOR_MULT) | |
3621 | { | |
3622 | Expression* e = expr; | |
3623 | while (e->classification() == EXPRESSION_CONVERSION) | |
3624 | { | |
3625 | Type_conversion_expression* te | |
3626 | = static_cast<Type_conversion_expression*>(e); | |
3627 | e = te->expr(); | |
3628 | } | |
3629 | ||
3630 | if (e->classification() == EXPRESSION_UNARY) | |
3631 | { | |
3632 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3633 | if (ue->op_ == OPERATOR_AND) | |
3634 | { | |
3635 | if (e == expr) | |
3636 | { | |
3637 | // *&x == x. | |
f4dea966 | 3638 | if (!ue->expr_->is_addressable() && !ue->create_temp_) |
3639 | { | |
3640 | error_at(ue->location(), | |
3641 | "invalid operand for unary %<&%>"); | |
3642 | this->set_is_error(); | |
3643 | } | |
e440a328 | 3644 | return ue->expr_; |
3645 | } | |
3646 | ue->set_does_not_escape(); | |
3647 | } | |
3648 | } | |
3649 | } | |
3650 | ||
55661ce9 | 3651 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3652 | // having to deal with TYPE_VOID in other places. | |
3653 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3654 | { | |
3655 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3656 | return Expression::make_error(this->location()); | |
3657 | } | |
3658 | ||
59a401fe | 3659 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3660 | { |
0c77715b | 3661 | Numeric_constant nc; |
3662 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3663 | { |
0c77715b | 3664 | Numeric_constant result; |
3665 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3666 | return result.expression(loc); | |
e440a328 | 3667 | } |
3668 | } | |
3669 | ||
3670 | return this; | |
3671 | } | |
3672 | ||
f9ca30f9 | 3673 | // Flatten expression if a nil check must be performed and create temporary |
3674 | // variables if necessary. | |
3675 | ||
3676 | Expression* | |
3677 | Unary_expression::do_flatten(Gogo* gogo, Named_object*, | |
3678 | Statement_inserter* inserter) | |
3679 | { | |
f4dea966 | 3680 | if (this->is_error_expression() || this->expr_->is_error_expression()) |
3681 | return Expression::make_error(this->location()); | |
3682 | ||
f9ca30f9 | 3683 | Location location = this->location(); |
3684 | if (this->op_ == OPERATOR_MULT | |
3685 | && !this->expr_->is_variable()) | |
3686 | { | |
3687 | go_assert(this->expr_->type()->points_to() != NULL); | |
3688 | Type* ptype = this->expr_->type()->points_to(); | |
3689 | if (!ptype->is_void_type()) | |
3690 | { | |
3691 | Btype* pbtype = ptype->get_backend(gogo); | |
3692 | size_t s = gogo->backend()->type_size(pbtype); | |
3693 | if (s >= 4096 || this->issue_nil_check_) | |
3694 | { | |
3695 | Temporary_statement* temp = | |
3696 | Statement::make_temporary(NULL, this->expr_, location); | |
3697 | inserter->insert(temp); | |
3698 | this->expr_ = | |
3699 | Expression::make_temporary_reference(temp, location); | |
3700 | } | |
3701 | } | |
3702 | } | |
3703 | ||
3704 | if (this->create_temp_ && !this->expr_->is_variable()) | |
3705 | { | |
3706 | Temporary_statement* temp = | |
3707 | Statement::make_temporary(NULL, this->expr_, location); | |
3708 | inserter->insert(temp); | |
3709 | this->expr_ = Expression::make_temporary_reference(temp, location); | |
3710 | } | |
3711 | ||
3712 | return this; | |
3713 | } | |
3714 | ||
e440a328 | 3715 | // Return whether a unary expression is a constant. |
3716 | ||
3717 | bool | |
3718 | Unary_expression::do_is_constant() const | |
3719 | { | |
3720 | if (this->op_ == OPERATOR_MULT) | |
3721 | { | |
3722 | // Indirecting through a pointer is only constant if the object | |
3723 | // to which the expression points is constant, but we currently | |
3724 | // have no way to determine that. | |
3725 | return false; | |
3726 | } | |
3727 | else if (this->op_ == OPERATOR_AND) | |
3728 | { | |
3729 | // Taking the address of a variable is constant if it is a | |
f9ca30f9 | 3730 | // global variable, not constant otherwise. In other cases taking the |
3731 | // address is probably not a constant. | |
e440a328 | 3732 | Var_expression* ve = this->expr_->var_expression(); |
3733 | if (ve != NULL) | |
3734 | { | |
3735 | Named_object* no = ve->named_object(); | |
3736 | return no->is_variable() && no->var_value()->is_global(); | |
3737 | } | |
3738 | return false; | |
3739 | } | |
3740 | else | |
3741 | return this->expr_->is_constant(); | |
3742 | } | |
3743 | ||
0c77715b | 3744 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3745 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3746 | |
3747 | bool | |
0c77715b | 3748 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3749 | Location location, Numeric_constant* nc) | |
e440a328 | 3750 | { |
3751 | switch (op) | |
3752 | { | |
3753 | case OPERATOR_PLUS: | |
0c77715b | 3754 | *nc = *unc; |
e440a328 | 3755 | return true; |
0c77715b | 3756 | |
e440a328 | 3757 | case OPERATOR_MINUS: |
0c77715b | 3758 | if (unc->is_int() || unc->is_rune()) |
3759 | break; | |
3760 | else if (unc->is_float()) | |
3761 | { | |
3762 | mpfr_t uval; | |
3763 | unc->get_float(&uval); | |
3764 | mpfr_t val; | |
3765 | mpfr_init(val); | |
3766 | mpfr_neg(val, uval, GMP_RNDN); | |
3767 | nc->set_float(unc->type(), val); | |
3768 | mpfr_clear(uval); | |
3769 | mpfr_clear(val); | |
3770 | return true; | |
3771 | } | |
3772 | else if (unc->is_complex()) | |
3773 | { | |
3774 | mpfr_t ureal, uimag; | |
3775 | unc->get_complex(&ureal, &uimag); | |
3776 | mpfr_t real, imag; | |
3777 | mpfr_init(real); | |
3778 | mpfr_init(imag); | |
3779 | mpfr_neg(real, ureal, GMP_RNDN); | |
3780 | mpfr_neg(imag, uimag, GMP_RNDN); | |
3781 | nc->set_complex(unc->type(), real, imag); | |
3782 | mpfr_clear(ureal); | |
3783 | mpfr_clear(uimag); | |
3784 | mpfr_clear(real); | |
3785 | mpfr_clear(imag); | |
3786 | return true; | |
3787 | } | |
e440a328 | 3788 | else |
0c77715b | 3789 | go_unreachable(); |
e440a328 | 3790 | |
0c77715b | 3791 | case OPERATOR_XOR: |
3792 | break; | |
68448d53 | 3793 | |
59a401fe | 3794 | case OPERATOR_NOT: |
e440a328 | 3795 | case OPERATOR_AND: |
3796 | case OPERATOR_MULT: | |
3797 | return false; | |
0c77715b | 3798 | |
e440a328 | 3799 | default: |
c3e6f413 | 3800 | go_unreachable(); |
e440a328 | 3801 | } |
e440a328 | 3802 | |
0c77715b | 3803 | if (!unc->is_int() && !unc->is_rune()) |
3804 | return false; | |
3805 | ||
3806 | mpz_t uval; | |
8387e1df | 3807 | if (unc->is_rune()) |
3808 | unc->get_rune(&uval); | |
3809 | else | |
3810 | unc->get_int(&uval); | |
0c77715b | 3811 | mpz_t val; |
3812 | mpz_init(val); | |
e440a328 | 3813 | |
e440a328 | 3814 | switch (op) |
3815 | { | |
e440a328 | 3816 | case OPERATOR_MINUS: |
0c77715b | 3817 | mpz_neg(val, uval); |
3818 | break; | |
3819 | ||
e440a328 | 3820 | case OPERATOR_NOT: |
0c77715b | 3821 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3822 | break; | |
3823 | ||
e440a328 | 3824 | case OPERATOR_XOR: |
0c77715b | 3825 | { |
3826 | Type* utype = unc->type(); | |
3827 | if (utype->integer_type() == NULL | |
3828 | || utype->integer_type()->is_abstract()) | |
3829 | mpz_com(val, uval); | |
3830 | else | |
3831 | { | |
3832 | // The number of HOST_WIDE_INTs that it takes to represent | |
3833 | // UVAL. | |
3834 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3835 | + HOST_BITS_PER_WIDE_INT | |
3836 | - 1) | |
3837 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3838 | |
0c77715b | 3839 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3840 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3841 | ||
3842 | size_t obits = utype->integer_type()->bits(); | |
3843 | ||
3844 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3845 | { | |
3846 | mpz_t adj; | |
3847 | mpz_init_set_ui(adj, 1); | |
3848 | mpz_mul_2exp(adj, adj, obits); | |
3849 | mpz_add(uval, uval, adj); | |
3850 | mpz_clear(adj); | |
3851 | } | |
3852 | ||
3853 | size_t ecount; | |
3854 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3855 | go_assert(ecount <= count); | |
3856 | ||
3857 | // Trim down to the number of words required by the type. | |
3858 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3859 | / HOST_BITS_PER_WIDE_INT); | |
3860 | go_assert(ocount <= count); | |
3861 | ||
3862 | for (size_t i = 0; i < ocount; ++i) | |
3863 | phwi[i] = ~phwi[i]; | |
3864 | ||
3865 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3866 | if (clearbits != 0) | |
3867 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3868 | >> clearbits); | |
3869 | ||
3870 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3871 | ||
3872 | if (!utype->integer_type()->is_unsigned() | |
3873 | && mpz_tstbit(val, obits - 1)) | |
3874 | { | |
3875 | mpz_t adj; | |
3876 | mpz_init_set_ui(adj, 1); | |
3877 | mpz_mul_2exp(adj, adj, obits); | |
3878 | mpz_sub(val, val, adj); | |
3879 | mpz_clear(adj); | |
3880 | } | |
3881 | ||
3882 | delete[] phwi; | |
3883 | } | |
3884 | } | |
3885 | break; | |
e440a328 | 3886 | |
e440a328 | 3887 | default: |
c3e6f413 | 3888 | go_unreachable(); |
e440a328 | 3889 | } |
e440a328 | 3890 | |
0c77715b | 3891 | if (unc->is_rune()) |
3892 | nc->set_rune(NULL, val); | |
e440a328 | 3893 | else |
0c77715b | 3894 | nc->set_int(NULL, val); |
e440a328 | 3895 | |
0c77715b | 3896 | mpz_clear(uval); |
3897 | mpz_clear(val); | |
e440a328 | 3898 | |
0c77715b | 3899 | return nc->set_type(unc->type(), true, location); |
e440a328 | 3900 | } |
3901 | ||
0c77715b | 3902 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 3903 | |
3904 | bool | |
0c77715b | 3905 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 3906 | { |
0c77715b | 3907 | Numeric_constant unc; |
3908 | if (!this->expr_->numeric_constant_value(&unc)) | |
3909 | return false; | |
3910 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
3911 | nc); | |
e440a328 | 3912 | } |
3913 | ||
3914 | // Return the type of a unary expression. | |
3915 | ||
3916 | Type* | |
3917 | Unary_expression::do_type() | |
3918 | { | |
3919 | switch (this->op_) | |
3920 | { | |
3921 | case OPERATOR_PLUS: | |
3922 | case OPERATOR_MINUS: | |
3923 | case OPERATOR_NOT: | |
3924 | case OPERATOR_XOR: | |
3925 | return this->expr_->type(); | |
3926 | ||
3927 | case OPERATOR_AND: | |
3928 | return Type::make_pointer_type(this->expr_->type()); | |
3929 | ||
3930 | case OPERATOR_MULT: | |
3931 | { | |
3932 | Type* subtype = this->expr_->type(); | |
3933 | Type* points_to = subtype->points_to(); | |
3934 | if (points_to == NULL) | |
3935 | return Type::make_error_type(); | |
3936 | return points_to; | |
3937 | } | |
3938 | ||
3939 | default: | |
c3e6f413 | 3940 | go_unreachable(); |
e440a328 | 3941 | } |
3942 | } | |
3943 | ||
3944 | // Determine abstract types for a unary expression. | |
3945 | ||
3946 | void | |
3947 | Unary_expression::do_determine_type(const Type_context* context) | |
3948 | { | |
3949 | switch (this->op_) | |
3950 | { | |
3951 | case OPERATOR_PLUS: | |
3952 | case OPERATOR_MINUS: | |
3953 | case OPERATOR_NOT: | |
3954 | case OPERATOR_XOR: | |
3955 | this->expr_->determine_type(context); | |
3956 | break; | |
3957 | ||
3958 | case OPERATOR_AND: | |
3959 | // Taking the address of something. | |
3960 | { | |
3961 | Type* subtype = (context->type == NULL | |
3962 | ? NULL | |
3963 | : context->type->points_to()); | |
3964 | Type_context subcontext(subtype, false); | |
3965 | this->expr_->determine_type(&subcontext); | |
3966 | } | |
3967 | break; | |
3968 | ||
3969 | case OPERATOR_MULT: | |
3970 | // Indirecting through a pointer. | |
3971 | { | |
3972 | Type* subtype = (context->type == NULL | |
3973 | ? NULL | |
3974 | : Type::make_pointer_type(context->type)); | |
3975 | Type_context subcontext(subtype, false); | |
3976 | this->expr_->determine_type(&subcontext); | |
3977 | } | |
3978 | break; | |
3979 | ||
3980 | default: | |
c3e6f413 | 3981 | go_unreachable(); |
e440a328 | 3982 | } |
3983 | } | |
3984 | ||
3985 | // Check types for a unary expression. | |
3986 | ||
3987 | void | |
3988 | Unary_expression::do_check_types(Gogo*) | |
3989 | { | |
9fe897ef | 3990 | Type* type = this->expr_->type(); |
5c13bd80 | 3991 | if (type->is_error()) |
9fe897ef | 3992 | { |
3993 | this->set_is_error(); | |
3994 | return; | |
3995 | } | |
3996 | ||
e440a328 | 3997 | switch (this->op_) |
3998 | { | |
3999 | case OPERATOR_PLUS: | |
4000 | case OPERATOR_MINUS: | |
9fe897ef | 4001 | if (type->integer_type() == NULL |
4002 | && type->float_type() == NULL | |
4003 | && type->complex_type() == NULL) | |
4004 | this->report_error(_("expected numeric type")); | |
e440a328 | 4005 | break; |
4006 | ||
4007 | case OPERATOR_NOT: | |
59a401fe | 4008 | if (!type->is_boolean_type()) |
4009 | this->report_error(_("expected boolean type")); | |
4010 | break; | |
4011 | ||
e440a328 | 4012 | case OPERATOR_XOR: |
9fe897ef | 4013 | if (type->integer_type() == NULL |
4014 | && !type->is_boolean_type()) | |
4015 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 4016 | break; |
4017 | ||
4018 | case OPERATOR_AND: | |
4019 | if (!this->expr_->is_addressable()) | |
09ea332d | 4020 | { |
4021 | if (!this->create_temp_) | |
f4dea966 | 4022 | { |
4023 | error_at(this->location(), "invalid operand for unary %<&%>"); | |
4024 | this->set_is_error(); | |
4025 | } | |
09ea332d | 4026 | } |
e440a328 | 4027 | else |
56080003 | 4028 | { |
4029 | this->expr_->address_taken(this->escapes_); | |
4030 | this->expr_->issue_nil_check(); | |
4031 | } | |
e440a328 | 4032 | break; |
4033 | ||
4034 | case OPERATOR_MULT: | |
4035 | // Indirecting through a pointer. | |
9fe897ef | 4036 | if (type->points_to() == NULL) |
4037 | this->report_error(_("expected pointer")); | |
e440a328 | 4038 | break; |
4039 | ||
4040 | default: | |
c3e6f413 | 4041 | go_unreachable(); |
e440a328 | 4042 | } |
4043 | } | |
4044 | ||
4045 | // Get a tree for a unary expression. | |
4046 | ||
4047 | tree | |
4048 | Unary_expression::do_get_tree(Translate_context* context) | |
4049 | { | |
1b1f2abf | 4050 | Gogo* gogo = context->gogo(); |
e9d3367e | 4051 | Location loc = this->location(); |
4052 | ||
4053 | // Taking the address of a set-and-use-temporary expression requires | |
4054 | // setting the temporary and then taking the address. | |
4055 | if (this->op_ == OPERATOR_AND) | |
4056 | { | |
4057 | Set_and_use_temporary_expression* sut = | |
4058 | this->expr_->set_and_use_temporary_expression(); | |
4059 | if (sut != NULL) | |
4060 | { | |
4061 | Temporary_statement* temp = sut->temporary(); | |
4062 | Bvariable* bvar = temp->get_backend_variable(context); | |
f9ca30f9 | 4063 | Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc); |
4064 | ||
4065 | Expression* val = sut->expression(); | |
4066 | Bexpression* bval = tree_to_expr(val->get_tree(context)); | |
4067 | ||
4068 | Bstatement* bassign = | |
4069 | gogo->backend()->assignment_statement(bvar_expr, bval, loc); | |
4070 | Bexpression* bvar_addr = | |
4071 | gogo->backend()->address_expression(bvar_expr, loc); | |
4072 | Bexpression* ret = | |
4073 | gogo->backend()->compound_expression(bassign, bvar_addr, loc); | |
4074 | return expr_to_tree(ret); | |
e9d3367e | 4075 | } |
4076 | } | |
4077 | ||
f9ca30f9 | 4078 | Bexpression* ret; |
e440a328 | 4079 | tree expr = this->expr_->get_tree(context); |
f9ca30f9 | 4080 | Bexpression* bexpr = tree_to_expr(expr); |
4081 | Btype* btype = this->expr_->type()->get_backend(gogo); | |
e440a328 | 4082 | switch (this->op_) |
4083 | { | |
4084 | case OPERATOR_PLUS: | |
f9ca30f9 | 4085 | ret = bexpr; |
4086 | break; | |
e440a328 | 4087 | |
4088 | case OPERATOR_MINUS: | |
f9ca30f9 | 4089 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4090 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
4091 | break; | |
e440a328 | 4092 | |
4093 | case OPERATOR_NOT: | |
e440a328 | 4094 | case OPERATOR_XOR: |
f9ca30f9 | 4095 | ret = gogo->backend()->unary_expression(this->op_, bexpr, loc); |
4096 | break; | |
e440a328 | 4097 | |
4098 | case OPERATOR_AND: | |
09ea332d | 4099 | if (!this->create_temp_) |
4100 | { | |
4101 | // We should not see a non-constant constructor here; cases | |
4102 | // where we would see one should have been moved onto the | |
4103 | // heap at parse time. Taking the address of a nonconstant | |
4104 | // constructor will not do what the programmer expects. | |
f9ca30f9 | 4105 | |
4106 | go_assert(!this->expr_->is_composite_literal() | |
4107 | || this->expr_->is_immutable()); | |
24060bf9 | 4108 | if (this->expr_->classification() == EXPRESSION_UNARY) |
4109 | { | |
4110 | Unary_expression* ue = | |
4111 | static_cast<Unary_expression*>(this->expr_); | |
4112 | go_assert(ue->op() != OPERATOR_AND); | |
4113 | } | |
09ea332d | 4114 | } |
e440a328 | 4115 | |
76f85fd6 | 4116 | if (this->is_gc_root_) |
4117 | { | |
4118 | // Build a decl for a GC root variable. GC roots are mutable, so they | |
4119 | // cannot be represented as an immutable_struct in the backend. | |
4120 | Bvariable* gc_root = gogo->backend()->gc_root_variable(btype, bexpr); | |
4121 | bexpr = gogo->backend()->var_expression(gc_root, loc); | |
4122 | } | |
4123 | else if ((this->expr_->is_composite_literal() | |
f9ca30f9 | 4124 | || this->expr_->string_expression() != NULL) |
4125 | && this->expr_->is_immutable()) | |
4126 | { | |
76f85fd6 | 4127 | // Build a decl for a constant constructor. |
f9ca30f9 | 4128 | static unsigned int counter; |
4129 | char buf[100]; | |
4130 | snprintf(buf, sizeof buf, "C%u", counter); | |
4131 | ++counter; | |
4132 | ||
4133 | Bvariable* decl = | |
4134 | gogo->backend()->immutable_struct(buf, true, false, btype, loc); | |
4135 | gogo->backend()->immutable_struct_set_init(decl, buf, true, false, | |
4136 | btype, loc, bexpr); | |
4137 | bexpr = gogo->backend()->var_expression(decl, loc); | |
4138 | } | |
09ea332d | 4139 | |
f9ca30f9 | 4140 | go_assert(!this->create_temp_ || this->expr_->is_variable()); |
4141 | ret = gogo->backend()->address_expression(bexpr, loc); | |
4142 | break; | |
e440a328 | 4143 | |
4144 | case OPERATOR_MULT: | |
4145 | { | |
f9ca30f9 | 4146 | go_assert(this->expr_->type()->points_to() != NULL); |
e440a328 | 4147 | |
4148 | // If we are dereferencing the pointer to a large struct, we | |
4149 | // need to check for nil. We don't bother to check for small | |
4150 | // structs because we expect the system to crash on a nil | |
56080003 | 4151 | // pointer dereference. However, if we know the address of this |
4152 | // expression is being taken, we must always check for nil. | |
f9ca30f9 | 4153 | |
4154 | Type* ptype = this->expr_->type()->points_to(); | |
4155 | Btype* pbtype = ptype->get_backend(gogo); | |
4156 | if (!ptype->is_void_type()) | |
e440a328 | 4157 | { |
f9ca30f9 | 4158 | size_t s = gogo->backend()->type_size(pbtype); |
4159 | if (s >= 4096 || this->issue_nil_check_) | |
19b4f09b | 4160 | { |
f9ca30f9 | 4161 | go_assert(this->expr_->is_variable()); |
4162 | ||
4163 | Expression* nil_expr = Expression::make_nil(loc); | |
4164 | Bexpression* nil = tree_to_expr(nil_expr->get_tree(context)); | |
4165 | Bexpression* compare = | |
4166 | gogo->backend()->binary_expression(OPERATOR_EQEQ, bexpr, | |
4167 | nil, loc); | |
4168 | ||
aff1f085 | 4169 | Expression* crash_expr = |
4170 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, loc); | |
f9ca30f9 | 4171 | Bexpression* crash = |
4172 | tree_to_expr(crash_expr->get_tree(context)); | |
4173 | bexpr = gogo->backend()->conditional_expression(btype, compare, | |
4174 | crash, bexpr, | |
4175 | loc); | |
4176 | ||
19b4f09b | 4177 | } |
e440a328 | 4178 | } |
9b27b43c | 4179 | ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc); |
e440a328 | 4180 | } |
f9ca30f9 | 4181 | break; |
e440a328 | 4182 | |
4183 | default: | |
c3e6f413 | 4184 | go_unreachable(); |
e440a328 | 4185 | } |
f9ca30f9 | 4186 | |
4187 | return expr_to_tree(ret); | |
e440a328 | 4188 | } |
4189 | ||
4190 | // Export a unary expression. | |
4191 | ||
4192 | void | |
4193 | Unary_expression::do_export(Export* exp) const | |
4194 | { | |
4195 | switch (this->op_) | |
4196 | { | |
4197 | case OPERATOR_PLUS: | |
4198 | exp->write_c_string("+ "); | |
4199 | break; | |
4200 | case OPERATOR_MINUS: | |
4201 | exp->write_c_string("- "); | |
4202 | break; | |
4203 | case OPERATOR_NOT: | |
4204 | exp->write_c_string("! "); | |
4205 | break; | |
4206 | case OPERATOR_XOR: | |
4207 | exp->write_c_string("^ "); | |
4208 | break; | |
4209 | case OPERATOR_AND: | |
4210 | case OPERATOR_MULT: | |
4211 | default: | |
c3e6f413 | 4212 | go_unreachable(); |
e440a328 | 4213 | } |
4214 | this->expr_->export_expression(exp); | |
4215 | } | |
4216 | ||
4217 | // Import a unary expression. | |
4218 | ||
4219 | Expression* | |
4220 | Unary_expression::do_import(Import* imp) | |
4221 | { | |
4222 | Operator op; | |
4223 | switch (imp->get_char()) | |
4224 | { | |
4225 | case '+': | |
4226 | op = OPERATOR_PLUS; | |
4227 | break; | |
4228 | case '-': | |
4229 | op = OPERATOR_MINUS; | |
4230 | break; | |
4231 | case '!': | |
4232 | op = OPERATOR_NOT; | |
4233 | break; | |
4234 | case '^': | |
4235 | op = OPERATOR_XOR; | |
4236 | break; | |
4237 | default: | |
c3e6f413 | 4238 | go_unreachable(); |
e440a328 | 4239 | } |
4240 | imp->require_c_string(" "); | |
4241 | Expression* expr = Expression::import_expression(imp); | |
4242 | return Expression::make_unary(op, expr, imp->location()); | |
4243 | } | |
4244 | ||
d751bb78 | 4245 | // Dump ast representation of an unary expression. |
4246 | ||
4247 | void | |
4248 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4249 | { | |
4250 | ast_dump_context->dump_operator(this->op_); | |
4251 | ast_dump_context->ostream() << "("; | |
4252 | ast_dump_context->dump_expression(this->expr_); | |
4253 | ast_dump_context->ostream() << ") "; | |
4254 | } | |
4255 | ||
e440a328 | 4256 | // Make a unary expression. |
4257 | ||
4258 | Expression* | |
b13c66cd | 4259 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4260 | { |
4261 | return new Unary_expression(op, expr, location); | |
4262 | } | |
4263 | ||
4264 | // If this is an indirection through a pointer, return the expression | |
4265 | // being pointed through. Otherwise return this. | |
4266 | ||
4267 | Expression* | |
4268 | Expression::deref() | |
4269 | { | |
4270 | if (this->classification_ == EXPRESSION_UNARY) | |
4271 | { | |
4272 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4273 | if (ue->op() == OPERATOR_MULT) | |
4274 | return ue->operand(); | |
4275 | } | |
4276 | return this; | |
4277 | } | |
4278 | ||
4279 | // Class Binary_expression. | |
4280 | ||
4281 | // Traversal. | |
4282 | ||
4283 | int | |
4284 | Binary_expression::do_traverse(Traverse* traverse) | |
4285 | { | |
4286 | int t = Expression::traverse(&this->left_, traverse); | |
4287 | if (t == TRAVERSE_EXIT) | |
4288 | return TRAVERSE_EXIT; | |
4289 | return Expression::traverse(&this->right_, traverse); | |
4290 | } | |
4291 | ||
0c77715b | 4292 | // Return the type to use for a binary operation on operands of |
4293 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4294 | // such may be NULL or abstract. | |
4295 | ||
4296 | bool | |
4297 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4298 | Type* right_type, Type** result_type) | |
4299 | { | |
4300 | if (left_type != right_type | |
4301 | && !left_type->is_abstract() | |
4302 | && !right_type->is_abstract() | |
4303 | && left_type->base() != right_type->base() | |
4304 | && op != OPERATOR_LSHIFT | |
4305 | && op != OPERATOR_RSHIFT) | |
4306 | { | |
4307 | // May be a type error--let it be diagnosed elsewhere. | |
4308 | return false; | |
4309 | } | |
4310 | ||
4311 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4312 | { | |
4313 | if (left_type->integer_type() != NULL) | |
4314 | *result_type = left_type; | |
4315 | else | |
4316 | *result_type = Type::make_abstract_integer_type(); | |
4317 | } | |
4318 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4319 | *result_type = left_type; | |
4320 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4321 | *result_type = right_type; | |
4322 | else if (!left_type->is_abstract()) | |
4323 | *result_type = left_type; | |
4324 | else if (!right_type->is_abstract()) | |
4325 | *result_type = right_type; | |
4326 | else if (left_type->complex_type() != NULL) | |
4327 | *result_type = left_type; | |
4328 | else if (right_type->complex_type() != NULL) | |
4329 | *result_type = right_type; | |
4330 | else if (left_type->float_type() != NULL) | |
4331 | *result_type = left_type; | |
4332 | else if (right_type->float_type() != NULL) | |
4333 | *result_type = right_type; | |
4334 | else if (left_type->integer_type() != NULL | |
4335 | && left_type->integer_type()->is_rune()) | |
4336 | *result_type = left_type; | |
4337 | else if (right_type->integer_type() != NULL | |
4338 | && right_type->integer_type()->is_rune()) | |
4339 | *result_type = right_type; | |
4340 | else | |
4341 | *result_type = left_type; | |
4342 | ||
4343 | return true; | |
4344 | } | |
4345 | ||
4346 | // Convert an integer comparison code and an operator to a boolean | |
4347 | // value. | |
e440a328 | 4348 | |
4349 | bool | |
0c77715b | 4350 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4351 | { |
e440a328 | 4352 | switch (op) |
4353 | { | |
4354 | case OPERATOR_EQEQ: | |
0c77715b | 4355 | return cmp == 0; |
4356 | break; | |
e440a328 | 4357 | case OPERATOR_NOTEQ: |
0c77715b | 4358 | return cmp != 0; |
4359 | break; | |
e440a328 | 4360 | case OPERATOR_LT: |
0c77715b | 4361 | return cmp < 0; |
4362 | break; | |
e440a328 | 4363 | case OPERATOR_LE: |
0c77715b | 4364 | return cmp <= 0; |
e440a328 | 4365 | case OPERATOR_GT: |
0c77715b | 4366 | return cmp > 0; |
e440a328 | 4367 | case OPERATOR_GE: |
0c77715b | 4368 | return cmp >= 0; |
e440a328 | 4369 | default: |
c3e6f413 | 4370 | go_unreachable(); |
e440a328 | 4371 | } |
4372 | } | |
4373 | ||
0c77715b | 4374 | // Compare constants according to OP. |
e440a328 | 4375 | |
4376 | bool | |
0c77715b | 4377 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4378 | Numeric_constant* right_nc, | |
4379 | Location location, bool* result) | |
e440a328 | 4380 | { |
0c77715b | 4381 | Type* left_type = left_nc->type(); |
4382 | Type* right_type = right_nc->type(); | |
4383 | ||
4384 | Type* type; | |
4385 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4386 | return false; | |
4387 | ||
4388 | // When comparing an untyped operand to a typed operand, we are | |
4389 | // effectively coercing the untyped operand to the other operand's | |
4390 | // type, so make sure that is valid. | |
4391 | if (!left_nc->set_type(type, true, location) | |
4392 | || !right_nc->set_type(type, true, location)) | |
4393 | return false; | |
4394 | ||
4395 | bool ret; | |
4396 | int cmp; | |
4397 | if (type->complex_type() != NULL) | |
4398 | { | |
4399 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4400 | return false; | |
4401 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4402 | } | |
4403 | else if (type->float_type() != NULL) | |
4404 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4405 | else |
0c77715b | 4406 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4407 | ||
4408 | if (ret) | |
4409 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4410 | ||
4411 | return ret; | |
4412 | } | |
4413 | ||
4414 | // Compare integer constants. | |
4415 | ||
4416 | bool | |
4417 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4418 | const Numeric_constant* right_nc, | |
4419 | int* cmp) | |
4420 | { | |
4421 | mpz_t left_val; | |
4422 | if (!left_nc->to_int(&left_val)) | |
4423 | return false; | |
4424 | mpz_t right_val; | |
4425 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4426 | { |
0c77715b | 4427 | mpz_clear(left_val); |
4428 | return false; | |
e440a328 | 4429 | } |
0c77715b | 4430 | |
4431 | *cmp = mpz_cmp(left_val, right_val); | |
4432 | ||
4433 | mpz_clear(left_val); | |
4434 | mpz_clear(right_val); | |
4435 | ||
4436 | return true; | |
4437 | } | |
4438 | ||
4439 | // Compare floating point constants. | |
4440 | ||
4441 | bool | |
4442 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4443 | const Numeric_constant* right_nc, | |
4444 | int* cmp) | |
4445 | { | |
4446 | mpfr_t left_val; | |
4447 | if (!left_nc->to_float(&left_val)) | |
4448 | return false; | |
4449 | mpfr_t right_val; | |
4450 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4451 | { |
0c77715b | 4452 | mpfr_clear(left_val); |
4453 | return false; | |
4454 | } | |
4455 | ||
4456 | // We already coerced both operands to the same type. If that type | |
4457 | // is not an abstract type, we need to round the values accordingly. | |
4458 | Type* type = left_nc->type(); | |
4459 | if (!type->is_abstract() && type->float_type() != NULL) | |
4460 | { | |
4461 | int bits = type->float_type()->bits(); | |
4462 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4463 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4464 | } |
0c77715b | 4465 | |
4466 | *cmp = mpfr_cmp(left_val, right_val); | |
4467 | ||
4468 | mpfr_clear(left_val); | |
4469 | mpfr_clear(right_val); | |
4470 | ||
4471 | return true; | |
e440a328 | 4472 | } |
4473 | ||
0c77715b | 4474 | // Compare complex constants. Complex numbers may only be compared |
4475 | // for equality. | |
e440a328 | 4476 | |
4477 | bool | |
0c77715b | 4478 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4479 | const Numeric_constant* right_nc, | |
4480 | int* cmp) | |
e440a328 | 4481 | { |
0c77715b | 4482 | mpfr_t left_real, left_imag; |
4483 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4484 | return false; | |
4485 | mpfr_t right_real, right_imag; | |
4486 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4487 | { |
0c77715b | 4488 | mpfr_clear(left_real); |
4489 | mpfr_clear(left_imag); | |
4490 | return false; | |
e440a328 | 4491 | } |
0c77715b | 4492 | |
4493 | // We already coerced both operands to the same type. If that type | |
4494 | // is not an abstract type, we need to round the values accordingly. | |
4495 | Type* type = left_nc->type(); | |
4496 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4497 | { |
0c77715b | 4498 | int bits = type->complex_type()->bits(); |
4499 | mpfr_prec_round(left_real, bits / 2, GMP_RNDN); | |
4500 | mpfr_prec_round(left_imag, bits / 2, GMP_RNDN); | |
4501 | mpfr_prec_round(right_real, bits / 2, GMP_RNDN); | |
4502 | mpfr_prec_round(right_imag, bits / 2, GMP_RNDN); | |
e440a328 | 4503 | } |
0c77715b | 4504 | |
4505 | *cmp = (mpfr_cmp(left_real, right_real) != 0 | |
4506 | || mpfr_cmp(left_imag, right_imag) != 0); | |
4507 | ||
4508 | mpfr_clear(left_real); | |
4509 | mpfr_clear(left_imag); | |
4510 | mpfr_clear(right_real); | |
4511 | mpfr_clear(right_imag); | |
4512 | ||
4513 | return true; | |
e440a328 | 4514 | } |
4515 | ||
0c77715b | 4516 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4517 | // true if this could be done, false if not. Issue errors at LOCATION | |
4518 | // as appropriate. | |
e440a328 | 4519 | |
4520 | bool | |
0c77715b | 4521 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4522 | Numeric_constant* right_nc, | |
4523 | Location location, Numeric_constant* nc) | |
e440a328 | 4524 | { |
e440a328 | 4525 | switch (op) |
4526 | { | |
4527 | case OPERATOR_OROR: | |
4528 | case OPERATOR_ANDAND: | |
4529 | case OPERATOR_EQEQ: | |
4530 | case OPERATOR_NOTEQ: | |
4531 | case OPERATOR_LT: | |
4532 | case OPERATOR_LE: | |
4533 | case OPERATOR_GT: | |
4534 | case OPERATOR_GE: | |
9767e2d3 | 4535 | // These return boolean values, not numeric. |
4536 | return false; | |
0c77715b | 4537 | default: |
4538 | break; | |
4539 | } | |
4540 | ||
4541 | Type* left_type = left_nc->type(); | |
4542 | Type* right_type = right_nc->type(); | |
4543 | ||
4544 | Type* type; | |
4545 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4546 | return false; | |
4547 | ||
4548 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4549 | ||
4550 | // When combining an untyped operand with a typed operand, we are | |
4551 | // effectively coercing the untyped operand to the other operand's | |
4552 | // type, so make sure that is valid. | |
4553 | if (!left_nc->set_type(type, true, location)) | |
4554 | return false; | |
4555 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4556 | return false; | |
4557 | ||
4558 | bool r; | |
4559 | if (type->complex_type() != NULL) | |
4560 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4561 | else if (type->float_type() != NULL) | |
4562 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4563 | else | |
4564 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4565 | ||
4566 | if (r) | |
4567 | r = nc->set_type(type, true, location); | |
4568 | ||
4569 | return r; | |
4570 | } | |
4571 | ||
4572 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4573 | // integer operations. Return true if this could be done, false if | |
4574 | // not. | |
4575 | ||
4576 | bool | |
4577 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4578 | const Numeric_constant* right_nc, | |
4579 | Location location, Numeric_constant* nc) | |
4580 | { | |
4581 | mpz_t left_val; | |
4582 | if (!left_nc->to_int(&left_val)) | |
4583 | return false; | |
4584 | mpz_t right_val; | |
4585 | if (!right_nc->to_int(&right_val)) | |
4586 | { | |
4587 | mpz_clear(left_val); | |
e440a328 | 4588 | return false; |
0c77715b | 4589 | } |
4590 | ||
4591 | mpz_t val; | |
4592 | mpz_init(val); | |
4593 | ||
4594 | switch (op) | |
4595 | { | |
e440a328 | 4596 | case OPERATOR_PLUS: |
4597 | mpz_add(val, left_val, right_val); | |
2c809f8f | 4598 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4599 | { | |
4600 | error_at(location, "constant addition overflow"); | |
4601 | mpz_set_ui(val, 1); | |
4602 | } | |
e440a328 | 4603 | break; |
4604 | case OPERATOR_MINUS: | |
4605 | mpz_sub(val, left_val, right_val); | |
2c809f8f | 4606 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4607 | { | |
4608 | error_at(location, "constant subtraction overflow"); | |
4609 | mpz_set_ui(val, 1); | |
4610 | } | |
e440a328 | 4611 | break; |
4612 | case OPERATOR_OR: | |
4613 | mpz_ior(val, left_val, right_val); | |
4614 | break; | |
4615 | case OPERATOR_XOR: | |
4616 | mpz_xor(val, left_val, right_val); | |
4617 | break; | |
4618 | case OPERATOR_MULT: | |
4619 | mpz_mul(val, left_val, right_val); | |
2c809f8f | 4620 | if (mpz_sizeinbase(val, 2) > 0x100000) |
4621 | { | |
4622 | error_at(location, "constant multiplication overflow"); | |
4623 | mpz_set_ui(val, 1); | |
4624 | } | |
e440a328 | 4625 | break; |
4626 | case OPERATOR_DIV: | |
4627 | if (mpz_sgn(right_val) != 0) | |
4628 | mpz_tdiv_q(val, left_val, right_val); | |
4629 | else | |
4630 | { | |
4631 | error_at(location, "division by zero"); | |
4632 | mpz_set_ui(val, 0); | |
e440a328 | 4633 | } |
4634 | break; | |
4635 | case OPERATOR_MOD: | |
4636 | if (mpz_sgn(right_val) != 0) | |
4637 | mpz_tdiv_r(val, left_val, right_val); | |
4638 | else | |
4639 | { | |
4640 | error_at(location, "division by zero"); | |
4641 | mpz_set_ui(val, 0); | |
e440a328 | 4642 | } |
4643 | break; | |
4644 | case OPERATOR_LSHIFT: | |
4645 | { | |
4646 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4647 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4648 | mpz_mul_2exp(val, left_val, shift); | |
4649 | else | |
e440a328 | 4650 | { |
4651 | error_at(location, "shift count overflow"); | |
2c809f8f | 4652 | mpz_set_ui(val, 1); |
e440a328 | 4653 | } |
e440a328 | 4654 | break; |
4655 | } | |
4656 | break; | |
4657 | case OPERATOR_RSHIFT: | |
4658 | { | |
4659 | unsigned long shift = mpz_get_ui(right_val); | |
4660 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4661 | { | |
4662 | error_at(location, "shift count overflow"); | |
2c809f8f | 4663 | mpz_set_ui(val, 1); |
e440a328 | 4664 | } |
e440a328 | 4665 | else |
0c77715b | 4666 | { |
4667 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4668 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4669 | else | |
4670 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4671 | } | |
e440a328 | 4672 | break; |
4673 | } | |
4674 | break; | |
4675 | case OPERATOR_AND: | |
4676 | mpz_and(val, left_val, right_val); | |
4677 | break; | |
4678 | case OPERATOR_BITCLEAR: | |
4679 | { | |
4680 | mpz_t tval; | |
4681 | mpz_init(tval); | |
4682 | mpz_com(tval, right_val); | |
4683 | mpz_and(val, left_val, tval); | |
4684 | mpz_clear(tval); | |
4685 | } | |
4686 | break; | |
4687 | default: | |
c3e6f413 | 4688 | go_unreachable(); |
e440a328 | 4689 | } |
4690 | ||
0c77715b | 4691 | mpz_clear(left_val); |
4692 | mpz_clear(right_val); | |
e440a328 | 4693 | |
0c77715b | 4694 | if (left_nc->is_rune() |
4695 | || (op != OPERATOR_LSHIFT | |
4696 | && op != OPERATOR_RSHIFT | |
4697 | && right_nc->is_rune())) | |
4698 | nc->set_rune(NULL, val); | |
4699 | else | |
4700 | nc->set_int(NULL, val); | |
4701 | ||
4702 | mpz_clear(val); | |
e440a328 | 4703 | |
4704 | return true; | |
4705 | } | |
4706 | ||
0c77715b | 4707 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4708 | // floating point operations. Return true if this could be done, | |
4709 | // false if not. | |
e440a328 | 4710 | |
4711 | bool | |
0c77715b | 4712 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4713 | const Numeric_constant* right_nc, | |
4714 | Location location, Numeric_constant* nc) | |
e440a328 | 4715 | { |
0c77715b | 4716 | mpfr_t left_val; |
4717 | if (!left_nc->to_float(&left_val)) | |
4718 | return false; | |
4719 | mpfr_t right_val; | |
4720 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4721 | { |
0c77715b | 4722 | mpfr_clear(left_val); |
e440a328 | 4723 | return false; |
0c77715b | 4724 | } |
4725 | ||
4726 | mpfr_t val; | |
4727 | mpfr_init(val); | |
4728 | ||
4729 | bool ret = true; | |
4730 | switch (op) | |
4731 | { | |
e440a328 | 4732 | case OPERATOR_PLUS: |
4733 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4734 | break; | |
4735 | case OPERATOR_MINUS: | |
4736 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4737 | break; | |
4738 | case OPERATOR_OR: | |
4739 | case OPERATOR_XOR: | |
4740 | case OPERATOR_AND: | |
4741 | case OPERATOR_BITCLEAR: | |
0c77715b | 4742 | case OPERATOR_MOD: |
4743 | case OPERATOR_LSHIFT: | |
4744 | case OPERATOR_RSHIFT: | |
4745 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4746 | ret = false; | |
4747 | break; | |
e440a328 | 4748 | case OPERATOR_MULT: |
4749 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4750 | break; | |
4751 | case OPERATOR_DIV: | |
0c77715b | 4752 | if (!mpfr_zero_p(right_val)) |
4753 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4754 | else | |
4755 | { | |
4756 | error_at(location, "division by zero"); | |
4757 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4758 | } | |
e440a328 | 4759 | break; |
e440a328 | 4760 | default: |
c3e6f413 | 4761 | go_unreachable(); |
e440a328 | 4762 | } |
4763 | ||
0c77715b | 4764 | mpfr_clear(left_val); |
4765 | mpfr_clear(right_val); | |
e440a328 | 4766 | |
0c77715b | 4767 | nc->set_float(NULL, val); |
4768 | mpfr_clear(val); | |
e440a328 | 4769 | |
0c77715b | 4770 | return ret; |
e440a328 | 4771 | } |
4772 | ||
0c77715b | 4773 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4774 | // complex operations. Return true if this could be done, false if | |
4775 | // not. | |
e440a328 | 4776 | |
4777 | bool | |
0c77715b | 4778 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4779 | const Numeric_constant* right_nc, | |
4780 | Location location, Numeric_constant* nc) | |
e440a328 | 4781 | { |
0c77715b | 4782 | mpfr_t left_real, left_imag; |
4783 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4784 | return false; | |
4785 | mpfr_t right_real, right_imag; | |
4786 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4787 | { |
0c77715b | 4788 | mpfr_clear(left_real); |
4789 | mpfr_clear(left_imag); | |
e440a328 | 4790 | return false; |
0c77715b | 4791 | } |
4792 | ||
4793 | mpfr_t real, imag; | |
4794 | mpfr_init(real); | |
4795 | mpfr_init(imag); | |
4796 | ||
4797 | bool ret = true; | |
4798 | switch (op) | |
4799 | { | |
e440a328 | 4800 | case OPERATOR_PLUS: |
4801 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4802 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4803 | break; | |
4804 | case OPERATOR_MINUS: | |
4805 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4806 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4807 | break; | |
4808 | case OPERATOR_OR: | |
4809 | case OPERATOR_XOR: | |
4810 | case OPERATOR_AND: | |
4811 | case OPERATOR_BITCLEAR: | |
0c77715b | 4812 | case OPERATOR_MOD: |
4813 | case OPERATOR_LSHIFT: | |
4814 | case OPERATOR_RSHIFT: | |
4815 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4816 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4817 | ret = false; | |
4818 | break; | |
e440a328 | 4819 | case OPERATOR_MULT: |
4820 | { | |
4821 | // You might think that multiplying two complex numbers would | |
4822 | // be simple, and you would be right, until you start to think | |
4823 | // about getting the right answer for infinity. If one | |
4824 | // operand here is infinity and the other is anything other | |
4825 | // than zero or NaN, then we are going to wind up subtracting | |
4826 | // two infinity values. That will give us a NaN, but the | |
4827 | // correct answer is infinity. | |
4828 | ||
4829 | mpfr_t lrrr; | |
4830 | mpfr_init(lrrr); | |
4831 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4832 | ||
4833 | mpfr_t lrri; | |
4834 | mpfr_init(lrri); | |
4835 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4836 | ||
4837 | mpfr_t lirr; | |
4838 | mpfr_init(lirr); | |
4839 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4840 | ||
4841 | mpfr_t liri; | |
4842 | mpfr_init(liri); | |
4843 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4844 | ||
4845 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4846 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4847 | ||
4848 | // If we get NaN on both sides, check whether it should really | |
4849 | // be infinity. The rule is that if either side of the | |
4850 | // complex number is infinity, then the whole value is | |
4851 | // infinity, even if the other side is NaN. So the only case | |
4852 | // we have to fix is the one in which both sides are NaN. | |
4853 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4854 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4855 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4856 | { | |
4857 | bool is_infinity = false; | |
4858 | ||
4859 | mpfr_t lr; | |
4860 | mpfr_t li; | |
4861 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4862 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4863 | ||
4864 | mpfr_t rr; | |
4865 | mpfr_t ri; | |
4866 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4867 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4868 | ||
4869 | // If the left side is infinity, then the result is | |
4870 | // infinity. | |
4871 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4872 | { | |
4873 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4874 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4875 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4876 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4877 | if (mpfr_nan_p(rr)) | |
4878 | { | |
4879 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4880 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4881 | } | |
4882 | if (mpfr_nan_p(ri)) | |
4883 | { | |
4884 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4885 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4886 | } | |
4887 | is_infinity = true; | |
4888 | } | |
4889 | ||
4890 | // If the right side is infinity, then the result is | |
4891 | // infinity. | |
4892 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4893 | { | |
4894 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4895 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4896 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4897 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4898 | if (mpfr_nan_p(lr)) | |
4899 | { | |
4900 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4901 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4902 | } | |
4903 | if (mpfr_nan_p(li)) | |
4904 | { | |
4905 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4906 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4907 | } | |
4908 | is_infinity = true; | |
4909 | } | |
4910 | ||
4911 | // If we got an overflow in the intermediate computations, | |
4912 | // then the result is infinity. | |
4913 | if (!is_infinity | |
4914 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4915 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4916 | { | |
4917 | if (mpfr_nan_p(lr)) | |
4918 | { | |
4919 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4920 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4921 | } | |
4922 | if (mpfr_nan_p(li)) | |
4923 | { | |
4924 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4925 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4926 | } | |
4927 | if (mpfr_nan_p(rr)) | |
4928 | { | |
4929 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4930 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4931 | } | |
4932 | if (mpfr_nan_p(ri)) | |
4933 | { | |
4934 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4935 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4936 | } | |
4937 | is_infinity = true; | |
4938 | } | |
4939 | ||
4940 | if (is_infinity) | |
4941 | { | |
4942 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4943 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4944 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4945 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4946 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4947 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4948 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4949 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4950 | } | |
4951 | ||
4952 | mpfr_clear(lr); | |
4953 | mpfr_clear(li); | |
4954 | mpfr_clear(rr); | |
4955 | mpfr_clear(ri); | |
4956 | } | |
4957 | ||
4958 | mpfr_clear(lrrr); | |
4959 | mpfr_clear(lrri); | |
4960 | mpfr_clear(lirr); | |
4961 | mpfr_clear(liri); | |
4962 | } | |
4963 | break; | |
4964 | case OPERATOR_DIV: | |
4965 | { | |
4966 | // For complex division we want to avoid having an | |
4967 | // intermediate overflow turn the whole result in a NaN. We | |
4968 | // scale the values to try to avoid this. | |
4969 | ||
4970 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
0c77715b | 4971 | { |
4972 | error_at(location, "division by zero"); | |
4973 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4974 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4975 | break; | |
4976 | } | |
e440a328 | 4977 | |
4978 | mpfr_t rra; | |
4979 | mpfr_t ria; | |
4980 | mpfr_init(rra); | |
4981 | mpfr_init(ria); | |
4982 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4983 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4984 | mpfr_t t; | |
4985 | mpfr_init(t); | |
4986 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4987 | ||
4988 | mpfr_t rr; | |
4989 | mpfr_t ri; | |
4990 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4991 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4992 | long ilogbw = 0; | |
4993 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4994 | { | |
4995 | ilogbw = mpfr_get_exp(t); | |
4996 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4997 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4998 | } | |
4999 | ||
5000 | mpfr_t denom; | |
5001 | mpfr_init(denom); | |
5002 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
5003 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
5004 | mpfr_add(denom, denom, t, GMP_RNDN); | |
5005 | ||
5006 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
5007 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
5008 | mpfr_add(real, real, t, GMP_RNDN); | |
5009 | mpfr_div(real, real, denom, GMP_RNDN); | |
5010 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
5011 | ||
5012 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
5013 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
5014 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
5015 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
5016 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
5017 | ||
5018 | // If we wind up with NaN on both sides, check whether we | |
5019 | // should really have infinity. The rule is that if either | |
5020 | // side of the complex number is infinity, then the whole | |
5021 | // value is infinity, even if the other side is NaN. So the | |
5022 | // only case we have to fix is the one in which both sides are | |
5023 | // NaN. | |
5024 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
5025 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
5026 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
5027 | { | |
5028 | if (mpfr_zero_p(denom)) | |
5029 | { | |
5030 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
5031 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
5032 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
5033 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
5034 | } | |
5035 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
5036 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
5037 | { | |
5038 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
5039 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
5040 | ||
5041 | mpfr_t t2; | |
5042 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
5043 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
5044 | ||
5045 | mpfr_t t3; | |
5046 | mpfr_init(t3); | |
5047 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
5048 | ||
5049 | mpfr_t t4; | |
5050 | mpfr_init(t4); | |
5051 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
5052 | ||
5053 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5054 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
5055 | ||
5056 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
5057 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
5058 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5059 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
5060 | ||
5061 | mpfr_clear(t2); | |
5062 | mpfr_clear(t3); | |
5063 | mpfr_clear(t4); | |
5064 | } | |
5065 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
5066 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
5067 | { | |
5068 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
5069 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
5070 | ||
5071 | mpfr_t t2; | |
5072 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5073 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
5074 | ||
5075 | mpfr_t t3; | |
5076 | mpfr_init(t3); | |
5077 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
5078 | ||
5079 | mpfr_t t4; | |
5080 | mpfr_init(t4); | |
5081 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
5082 | ||
5083 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5084 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5085 | mpfr_mul(real, real, t3, GMP_RNDN); | |
5086 | ||
5087 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
5088 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
5089 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5090 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5091 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
5092 | ||
5093 | mpfr_clear(t2); | |
5094 | mpfr_clear(t3); | |
5095 | mpfr_clear(t4); | |
5096 | } | |
5097 | } | |
5098 | ||
5099 | mpfr_clear(denom); | |
5100 | mpfr_clear(rr); | |
5101 | mpfr_clear(ri); | |
5102 | mpfr_clear(t); | |
5103 | mpfr_clear(rra); | |
5104 | mpfr_clear(ria); | |
5105 | } | |
5106 | break; | |
e440a328 | 5107 | default: |
c3e6f413 | 5108 | go_unreachable(); |
e440a328 | 5109 | } |
5110 | ||
0c77715b | 5111 | mpfr_clear(left_real); |
5112 | mpfr_clear(left_imag); | |
5113 | mpfr_clear(right_real); | |
5114 | mpfr_clear(right_imag); | |
e440a328 | 5115 | |
0c77715b | 5116 | nc->set_complex(NULL, real, imag); |
5117 | mpfr_clear(real); | |
5118 | mpfr_clear(imag); | |
e440a328 | 5119 | |
0c77715b | 5120 | return ret; |
e440a328 | 5121 | } |
5122 | ||
5123 | // Lower a binary expression. We have to evaluate constant | |
5124 | // expressions now, in order to implement Go's unlimited precision | |
5125 | // constants. | |
5126 | ||
5127 | Expression* | |
e9d3367e | 5128 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5129 | Statement_inserter* inserter, int) | |
e440a328 | 5130 | { |
b13c66cd | 5131 | Location location = this->location(); |
e440a328 | 5132 | Operator op = this->op_; |
5133 | Expression* left = this->left_; | |
5134 | Expression* right = this->right_; | |
5135 | ||
5136 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5137 | || op == OPERATOR_NOTEQ | |
5138 | || op == OPERATOR_LT | |
5139 | || op == OPERATOR_LE | |
5140 | || op == OPERATOR_GT | |
5141 | || op == OPERATOR_GE); | |
5142 | ||
0c77715b | 5143 | // Numeric constant expressions. |
e440a328 | 5144 | { |
0c77715b | 5145 | Numeric_constant left_nc; |
5146 | Numeric_constant right_nc; | |
5147 | if (left->numeric_constant_value(&left_nc) | |
5148 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5149 | { |
0c77715b | 5150 | if (is_comparison) |
e440a328 | 5151 | { |
0c77715b | 5152 | bool result; |
5153 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5154 | &right_nc, location, | |
5155 | &result)) | |
5156 | return this; | |
e90c9dfc | 5157 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5158 | Expression::make_boolean(result, |
5159 | location), | |
5160 | location); | |
e440a328 | 5161 | } |
5162 | else | |
5163 | { | |
0c77715b | 5164 | Numeric_constant nc; |
5165 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5166 | location, &nc)) | |
5167 | return this; | |
5168 | return nc.expression(location); | |
e440a328 | 5169 | } |
5170 | } | |
e440a328 | 5171 | } |
5172 | ||
5173 | // String constant expressions. | |
315fa98d | 5174 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5175 | { |
5176 | std::string left_string; | |
5177 | std::string right_string; | |
5178 | if (left->string_constant_value(&left_string) | |
5179 | && right->string_constant_value(&right_string)) | |
315fa98d | 5180 | { |
5181 | if (op == OPERATOR_PLUS) | |
5182 | return Expression::make_string(left_string + right_string, | |
5183 | location); | |
5184 | else if (is_comparison) | |
5185 | { | |
5186 | int cmp = left_string.compare(right_string); | |
0c77715b | 5187 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5188 | return Expression::make_boolean(r, location); |
b40dc774 | 5189 | } |
5190 | } | |
b40dc774 | 5191 | } |
5192 | ||
ceeb12d7 | 5193 | // Lower struct, array, and some interface comparisons. |
e9d3367e | 5194 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
5195 | { | |
5196 | if (left->type()->struct_type() != NULL) | |
5197 | return this->lower_struct_comparison(gogo, inserter); | |
5198 | else if (left->type()->array_type() != NULL | |
5199 | && !left->type()->is_slice_type()) | |
5200 | return this->lower_array_comparison(gogo, inserter); | |
ceeb12d7 | 5201 | else if ((left->type()->interface_type() != NULL |
5202 | && right->type()->interface_type() == NULL) | |
5203 | || (left->type()->interface_type() == NULL | |
5204 | && right->type()->interface_type() != NULL)) | |
5205 | return this->lower_interface_value_comparison(gogo, inserter); | |
e9d3367e | 5206 | } |
5207 | ||
e440a328 | 5208 | return this; |
5209 | } | |
5210 | ||
e9d3367e | 5211 | // Lower a struct comparison. |
5212 | ||
5213 | Expression* | |
5214 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5215 | Statement_inserter* inserter) | |
5216 | { | |
5217 | Struct_type* st = this->left_->type()->struct_type(); | |
5218 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5219 | if (st2 == NULL) | |
5220 | return this; | |
5221 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5222 | return this; | |
5223 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5224 | this->right_->type(), NULL)) | |
5225 | return this; | |
5226 | ||
5227 | // See if we can compare using memcmp. As a heuristic, we use | |
5228 | // memcmp rather than field references and comparisons if there are | |
5229 | // more than two fields. | |
113ef6a5 | 5230 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5231 | return this->lower_compare_to_memcmp(gogo, inserter); |
5232 | ||
5233 | Location loc = this->location(); | |
5234 | ||
5235 | Expression* left = this->left_; | |
5236 | Temporary_statement* left_temp = NULL; | |
5237 | if (left->var_expression() == NULL | |
5238 | && left->temporary_reference_expression() == NULL) | |
5239 | { | |
5240 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5241 | inserter->insert(left_temp); | |
5242 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5243 | } | |
5244 | ||
5245 | Expression* right = this->right_; | |
5246 | Temporary_statement* right_temp = NULL; | |
5247 | if (right->var_expression() == NULL | |
5248 | && right->temporary_reference_expression() == NULL) | |
5249 | { | |
5250 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5251 | inserter->insert(right_temp); | |
5252 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5253 | } | |
5254 | ||
5255 | Expression* ret = Expression::make_boolean(true, loc); | |
5256 | const Struct_field_list* fields = st->fields(); | |
5257 | unsigned int field_index = 0; | |
5258 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5259 | pf != fields->end(); | |
5260 | ++pf, ++field_index) | |
5261 | { | |
f5165c05 | 5262 | if (Gogo::is_sink_name(pf->field_name())) |
5263 | continue; | |
5264 | ||
e9d3367e | 5265 | if (field_index > 0) |
5266 | { | |
5267 | if (left_temp == NULL) | |
5268 | left = left->copy(); | |
5269 | else | |
5270 | left = Expression::make_temporary_reference(left_temp, loc); | |
5271 | if (right_temp == NULL) | |
5272 | right = right->copy(); | |
5273 | else | |
5274 | right = Expression::make_temporary_reference(right_temp, loc); | |
5275 | } | |
5276 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5277 | loc); | |
5278 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5279 | loc); | |
5280 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5281 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5282 | } | |
5283 | ||
5284 | if (this->op_ == OPERATOR_NOTEQ) | |
5285 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5286 | ||
5287 | return ret; | |
5288 | } | |
5289 | ||
5290 | // Lower an array comparison. | |
5291 | ||
5292 | Expression* | |
5293 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5294 | Statement_inserter* inserter) | |
5295 | { | |
5296 | Array_type* at = this->left_->type()->array_type(); | |
5297 | Array_type* at2 = this->right_->type()->array_type(); | |
5298 | if (at2 == NULL) | |
5299 | return this; | |
5300 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5301 | return this; | |
5302 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5303 | this->right_->type(), NULL)) | |
5304 | return this; | |
5305 | ||
5306 | // Call memcmp directly if possible. This may let the middle-end | |
5307 | // optimize the call. | |
113ef6a5 | 5308 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5309 | return this->lower_compare_to_memcmp(gogo, inserter); |
5310 | ||
5311 | // Call the array comparison function. | |
5312 | Named_object* hash_fn; | |
5313 | Named_object* equal_fn; | |
5314 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5315 | &hash_fn, &equal_fn); | |
5316 | ||
5317 | Location loc = this->location(); | |
5318 | ||
5319 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5320 | ||
5321 | Expression_list* args = new Expression_list(); | |
5322 | args->push_back(this->operand_address(inserter, this->left_)); | |
5323 | args->push_back(this->operand_address(inserter, this->right_)); | |
5324 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5325 | ||
5326 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5327 | ||
5328 | if (this->op_ == OPERATOR_NOTEQ) | |
5329 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5330 | ||
5331 | return ret; | |
5332 | } | |
5333 | ||
ceeb12d7 | 5334 | // Lower an interface to value comparison. |
5335 | ||
5336 | Expression* | |
5337 | Binary_expression::lower_interface_value_comparison(Gogo*, | |
5338 | Statement_inserter* inserter) | |
5339 | { | |
5340 | Type* left_type = this->left_->type(); | |
5341 | Type* right_type = this->right_->type(); | |
5342 | Interface_type* ift; | |
5343 | if (left_type->interface_type() != NULL) | |
5344 | { | |
5345 | ift = left_type->interface_type(); | |
5346 | if (!ift->implements_interface(right_type, NULL)) | |
5347 | return this; | |
5348 | } | |
5349 | else | |
5350 | { | |
5351 | ift = right_type->interface_type(); | |
5352 | if (!ift->implements_interface(left_type, NULL)) | |
5353 | return this; | |
5354 | } | |
5355 | if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL)) | |
5356 | return this; | |
5357 | ||
5358 | Location loc = this->location(); | |
5359 | ||
5360 | if (left_type->interface_type() == NULL | |
5361 | && left_type->points_to() == NULL | |
5362 | && !this->left_->is_addressable()) | |
5363 | { | |
5364 | Temporary_statement* temp = | |
5365 | Statement::make_temporary(left_type, NULL, loc); | |
5366 | inserter->insert(temp); | |
5367 | this->left_ = | |
5368 | Expression::make_set_and_use_temporary(temp, this->left_, loc); | |
5369 | } | |
5370 | ||
5371 | if (right_type->interface_type() == NULL | |
5372 | && right_type->points_to() == NULL | |
5373 | && !this->right_->is_addressable()) | |
5374 | { | |
5375 | Temporary_statement* temp = | |
5376 | Statement::make_temporary(right_type, NULL, loc); | |
5377 | inserter->insert(temp); | |
5378 | this->right_ = | |
5379 | Expression::make_set_and_use_temporary(temp, this->right_, loc); | |
5380 | } | |
5381 | ||
5382 | return this; | |
5383 | } | |
5384 | ||
e9d3367e | 5385 | // Lower a struct or array comparison to a call to memcmp. |
5386 | ||
5387 | Expression* | |
5388 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5389 | { | |
5390 | Location loc = this->location(); | |
5391 | ||
5392 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5393 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5394 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5395 | TYPE_INFO_SIZE); | |
5396 | ||
5397 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
5398 | ||
5399 | mpz_t zval; | |
5400 | mpz_init_set_ui(zval, 0); | |
5401 | Expression* zero = Expression::make_integer(&zval, NULL, loc); | |
5402 | mpz_clear(zval); | |
5403 | ||
5404 | return Expression::make_binary(this->op_, call, zero, loc); | |
5405 | } | |
5406 | ||
a32698ee | 5407 | Expression* |
5408 | Binary_expression::do_flatten(Gogo*, Named_object*, | |
5409 | Statement_inserter* inserter) | |
5410 | { | |
5411 | Location loc = this->location(); | |
5412 | Temporary_statement* temp; | |
5413 | if (this->left_->type()->is_string_type() | |
5414 | && this->op_ == OPERATOR_PLUS) | |
5415 | { | |
5416 | if (!this->left_->is_variable()) | |
5417 | { | |
5418 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5419 | inserter->insert(temp); | |
5420 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5421 | } | |
5422 | if (!this->right_->is_variable()) | |
5423 | { | |
5424 | temp = | |
5425 | Statement::make_temporary(this->left_->type(), this->right_, loc); | |
5426 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5427 | inserter->insert(temp); | |
5428 | } | |
5429 | } | |
5430 | ||
5431 | Type* left_type = this->left_->type(); | |
5432 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5433 | || this->op_ == OPERATOR_RSHIFT); | |
5434 | bool is_idiv_op = ((this->op_ == OPERATOR_DIV && | |
5435 | left_type->integer_type() != NULL) | |
5436 | || this->op_ == OPERATOR_MOD); | |
5437 | ||
5438 | // FIXME: go_check_divide_zero and go_check_divide_overflow are globals | |
5439 | // defined in gcc/go/lang.opt. These should be defined in go_create_gogo | |
5440 | // and accessed from the Gogo* passed to do_flatten. | |
5441 | if (is_shift_op | |
5442 | || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow))) | |
5443 | { | |
5444 | if (!this->left_->is_variable()) | |
5445 | { | |
5446 | temp = Statement::make_temporary(NULL, this->left_, loc); | |
5447 | inserter->insert(temp); | |
5448 | this->left_ = Expression::make_temporary_reference(temp, loc); | |
5449 | } | |
5450 | if (!this->right_->is_variable()) | |
5451 | { | |
5452 | temp = | |
5453 | Statement::make_temporary(NULL, this->right_, loc); | |
5454 | this->right_ = Expression::make_temporary_reference(temp, loc); | |
5455 | inserter->insert(temp); | |
5456 | } | |
5457 | } | |
5458 | return this; | |
5459 | } | |
5460 | ||
5461 | ||
e9d3367e | 5462 | // Return the address of EXPR, cast to unsafe.Pointer. |
5463 | ||
5464 | Expression* | |
5465 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5466 | Expression* expr) | |
5467 | { | |
5468 | Location loc = this->location(); | |
5469 | ||
5470 | if (!expr->is_addressable()) | |
5471 | { | |
5472 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5473 | loc); | |
5474 | inserter->insert(temp); | |
5475 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5476 | } | |
5477 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5478 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5479 | Type* void_type = Type::make_void_type(); | |
5480 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5481 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5482 | } | |
5483 | ||
0c77715b | 5484 | // Return the numeric constant value, if it has one. |
e440a328 | 5485 | |
5486 | bool | |
0c77715b | 5487 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5488 | { |
0c77715b | 5489 | Numeric_constant left_nc; |
5490 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5491 | return false; | |
5492 | Numeric_constant right_nc; | |
5493 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5494 | return false; | |
9767e2d3 | 5495 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5496 | this->location(), nc); |
e440a328 | 5497 | } |
5498 | ||
5499 | // Note that the value is being discarded. | |
5500 | ||
4f2138d7 | 5501 | bool |
e440a328 | 5502 | Binary_expression::do_discarding_value() |
5503 | { | |
5504 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5505 | return this->right_->discarding_value(); |
e440a328 | 5506 | else |
4f2138d7 | 5507 | { |
5508 | this->unused_value_error(); | |
5509 | return false; | |
5510 | } | |
e440a328 | 5511 | } |
5512 | ||
5513 | // Get type. | |
5514 | ||
5515 | Type* | |
5516 | Binary_expression::do_type() | |
5517 | { | |
5f5fea79 | 5518 | if (this->classification() == EXPRESSION_ERROR) |
5519 | return Type::make_error_type(); | |
5520 | ||
e440a328 | 5521 | switch (this->op_) |
5522 | { | |
e440a328 | 5523 | case OPERATOR_EQEQ: |
5524 | case OPERATOR_NOTEQ: | |
5525 | case OPERATOR_LT: | |
5526 | case OPERATOR_LE: | |
5527 | case OPERATOR_GT: | |
5528 | case OPERATOR_GE: | |
e90c9dfc | 5529 | if (this->type_ == NULL) |
5530 | this->type_ = Type::make_boolean_type(); | |
5531 | return this->type_; | |
e440a328 | 5532 | |
5533 | case OPERATOR_PLUS: | |
5534 | case OPERATOR_MINUS: | |
5535 | case OPERATOR_OR: | |
5536 | case OPERATOR_XOR: | |
5537 | case OPERATOR_MULT: | |
5538 | case OPERATOR_DIV: | |
5539 | case OPERATOR_MOD: | |
5540 | case OPERATOR_AND: | |
5541 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5542 | case OPERATOR_OROR: |
5543 | case OPERATOR_ANDAND: | |
e440a328 | 5544 | { |
0c77715b | 5545 | Type* type; |
5546 | if (!Binary_expression::operation_type(this->op_, | |
5547 | this->left_->type(), | |
5548 | this->right_->type(), | |
5549 | &type)) | |
5550 | return Type::make_error_type(); | |
5551 | return type; | |
e440a328 | 5552 | } |
5553 | ||
5554 | case OPERATOR_LSHIFT: | |
5555 | case OPERATOR_RSHIFT: | |
5556 | return this->left_->type(); | |
5557 | ||
5558 | default: | |
c3e6f413 | 5559 | go_unreachable(); |
e440a328 | 5560 | } |
5561 | } | |
5562 | ||
5563 | // Set type for a binary expression. | |
5564 | ||
5565 | void | |
5566 | Binary_expression::do_determine_type(const Type_context* context) | |
5567 | { | |
5568 | Type* tleft = this->left_->type(); | |
5569 | Type* tright = this->right_->type(); | |
5570 | ||
5571 | // Both sides should have the same type, except for the shift | |
5572 | // operations. For a comparison, we should ignore the incoming | |
5573 | // type. | |
5574 | ||
5575 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5576 | || this->op_ == OPERATOR_RSHIFT); | |
5577 | ||
5578 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5579 | || this->op_ == OPERATOR_NOTEQ | |
5580 | || this->op_ == OPERATOR_LT | |
5581 | || this->op_ == OPERATOR_LE | |
5582 | || this->op_ == OPERATOR_GT | |
5583 | || this->op_ == OPERATOR_GE); | |
5584 | ||
5585 | Type_context subcontext(*context); | |
5586 | ||
5587 | if (is_comparison) | |
5588 | { | |
5589 | // In a comparison, the context does not determine the types of | |
5590 | // the operands. | |
5591 | subcontext.type = NULL; | |
5592 | } | |
5593 | ||
02ffd97f | 5594 | if (this->op_ == OPERATOR_ANDAND || this->op_ == OPERATOR_OROR) |
5595 | { | |
5596 | // For a logical operation, the context does not determine the | |
5597 | // types of the operands. The operands must be some boolean | |
5598 | // type but if the context has a boolean type they do not | |
5599 | // inherit it. See http://golang.org/issue/3924. | |
5600 | subcontext.type = NULL; | |
5601 | } | |
5602 | ||
e440a328 | 5603 | // Set the context for the left hand operand. |
5604 | if (is_shift_op) | |
5605 | { | |
b40dc774 | 5606 | // The right hand operand of a shift plays no role in |
5607 | // determining the type of the left hand operand. | |
e440a328 | 5608 | } |
5609 | else if (!tleft->is_abstract()) | |
5610 | subcontext.type = tleft; | |
5611 | else if (!tright->is_abstract()) | |
5612 | subcontext.type = tright; | |
5613 | else if (subcontext.type == NULL) | |
5614 | { | |
5615 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5616 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5617 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5618 | { | |
5619 | // Both sides have an abstract integer, abstract float, or | |
5620 | // abstract complex type. Just let CONTEXT determine | |
5621 | // whether they may remain abstract or not. | |
5622 | } | |
5623 | else if (tleft->complex_type() != NULL) | |
5624 | subcontext.type = tleft; | |
5625 | else if (tright->complex_type() != NULL) | |
5626 | subcontext.type = tright; | |
5627 | else if (tleft->float_type() != NULL) | |
5628 | subcontext.type = tleft; | |
5629 | else if (tright->float_type() != NULL) | |
5630 | subcontext.type = tright; | |
5631 | else | |
5632 | subcontext.type = tleft; | |
f58a23ae | 5633 | |
5634 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5635 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5636 | } |
5637 | ||
5638 | this->left_->determine_type(&subcontext); | |
5639 | ||
e440a328 | 5640 | if (is_shift_op) |
5641 | { | |
b40dc774 | 5642 | // We may have inherited an unusable type for the shift operand. |
5643 | // Give a useful error if that happened. | |
5644 | if (tleft->is_abstract() | |
5645 | && subcontext.type != NULL | |
8ab6effb | 5646 | && !subcontext.may_be_abstract |
f6bc81e6 | 5647 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5648 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5649 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5650 | "for left operand of shift")); |
b40dc774 | 5651 | |
5652 | // The context for the right hand operand is the same as for the | |
5653 | // left hand operand, except for a shift operator. | |
e440a328 | 5654 | subcontext.type = Type::lookup_integer_type("uint"); |
5655 | subcontext.may_be_abstract = false; | |
5656 | } | |
5657 | ||
5658 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5659 | |
5660 | if (is_comparison) | |
5661 | { | |
5662 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5663 | ; | |
5664 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5665 | this->type_ = context->type; | |
5666 | else if (!context->may_be_abstract) | |
5667 | this->type_ = Type::lookup_bool_type(); | |
5668 | } | |
e440a328 | 5669 | } |
5670 | ||
5671 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5672 | // OTYPE is the type of the other operand. Return whether the |
5673 | // operation is OK. This should not be used for shift. | |
e440a328 | 5674 | |
5675 | bool | |
be8b5eee | 5676 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5677 | Location location) |
e440a328 | 5678 | { |
5679 | switch (op) | |
5680 | { | |
5681 | case OPERATOR_OROR: | |
5682 | case OPERATOR_ANDAND: | |
5683 | if (!type->is_boolean_type()) | |
5684 | { | |
5685 | error_at(location, "expected boolean type"); | |
5686 | return false; | |
5687 | } | |
5688 | break; | |
5689 | ||
5690 | case OPERATOR_EQEQ: | |
5691 | case OPERATOR_NOTEQ: | |
e9d3367e | 5692 | { |
5693 | std::string reason; | |
5694 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5695 | { | |
5696 | error_at(location, "%s", reason.c_str()); | |
5697 | return false; | |
5698 | } | |
5699 | } | |
e440a328 | 5700 | break; |
5701 | ||
5702 | case OPERATOR_LT: | |
5703 | case OPERATOR_LE: | |
5704 | case OPERATOR_GT: | |
5705 | case OPERATOR_GE: | |
e9d3367e | 5706 | { |
5707 | std::string reason; | |
5708 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5709 | { | |
5710 | error_at(location, "%s", reason.c_str()); | |
5711 | return false; | |
5712 | } | |
5713 | } | |
e440a328 | 5714 | break; |
5715 | ||
5716 | case OPERATOR_PLUS: | |
5717 | case OPERATOR_PLUSEQ: | |
5718 | if (type->integer_type() == NULL | |
5719 | && type->float_type() == NULL | |
5720 | && type->complex_type() == NULL | |
5721 | && !type->is_string_type()) | |
5722 | { | |
5723 | error_at(location, | |
5724 | "expected integer, floating, complex, or string type"); | |
5725 | return false; | |
5726 | } | |
5727 | break; | |
5728 | ||
5729 | case OPERATOR_MINUS: | |
5730 | case OPERATOR_MINUSEQ: | |
5731 | case OPERATOR_MULT: | |
5732 | case OPERATOR_MULTEQ: | |
5733 | case OPERATOR_DIV: | |
5734 | case OPERATOR_DIVEQ: | |
5735 | if (type->integer_type() == NULL | |
5736 | && type->float_type() == NULL | |
5737 | && type->complex_type() == NULL) | |
5738 | { | |
5739 | error_at(location, "expected integer, floating, or complex type"); | |
5740 | return false; | |
5741 | } | |
5742 | break; | |
5743 | ||
5744 | case OPERATOR_MOD: | |
5745 | case OPERATOR_MODEQ: | |
5746 | case OPERATOR_OR: | |
5747 | case OPERATOR_OREQ: | |
5748 | case OPERATOR_AND: | |
5749 | case OPERATOR_ANDEQ: | |
5750 | case OPERATOR_XOR: | |
5751 | case OPERATOR_XOREQ: | |
5752 | case OPERATOR_BITCLEAR: | |
5753 | case OPERATOR_BITCLEAREQ: | |
5754 | if (type->integer_type() == NULL) | |
5755 | { | |
5756 | error_at(location, "expected integer type"); | |
5757 | return false; | |
5758 | } | |
5759 | break; | |
5760 | ||
5761 | default: | |
c3e6f413 | 5762 | go_unreachable(); |
e440a328 | 5763 | } |
5764 | ||
5765 | return true; | |
5766 | } | |
5767 | ||
5768 | // Check types. | |
5769 | ||
5770 | void | |
5771 | Binary_expression::do_check_types(Gogo*) | |
5772 | { | |
5f5fea79 | 5773 | if (this->classification() == EXPRESSION_ERROR) |
5774 | return; | |
5775 | ||
e440a328 | 5776 | Type* left_type = this->left_->type(); |
5777 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5778 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5779 | { |
5780 | this->set_is_error(); | |
5781 | return; | |
5782 | } | |
e440a328 | 5783 | |
5784 | if (this->op_ == OPERATOR_EQEQ | |
5785 | || this->op_ == OPERATOR_NOTEQ | |
5786 | || this->op_ == OPERATOR_LT | |
5787 | || this->op_ == OPERATOR_LE | |
5788 | || this->op_ == OPERATOR_GT | |
5789 | || this->op_ == OPERATOR_GE) | |
5790 | { | |
907c5ecd | 5791 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5792 | { | |
5793 | this->report_error(_("invalid comparison of nil with nil")); | |
5794 | return; | |
5795 | } | |
e440a328 | 5796 | if (!Type::are_assignable(left_type, right_type, NULL) |
5797 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5798 | { | |
5799 | this->report_error(_("incompatible types in binary expression")); | |
5800 | return; | |
5801 | } | |
5802 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5803 | right_type, |
e440a328 | 5804 | this->location()) |
5805 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5806 | left_type, |
e440a328 | 5807 | this->location())) |
5808 | { | |
5809 | this->set_is_error(); | |
5810 | return; | |
5811 | } | |
5812 | } | |
5813 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5814 | { | |
5815 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5816 | { | |
5817 | this->report_error(_("incompatible types in binary expression")); | |
5818 | return; | |
5819 | } | |
5820 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5821 | right_type, |
e440a328 | 5822 | this->location())) |
5823 | { | |
5824 | this->set_is_error(); | |
5825 | return; | |
5826 | } | |
5c65b19d | 5827 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5828 | { | |
5829 | // Division by a zero integer constant is an error. | |
5830 | Numeric_constant rconst; | |
5831 | unsigned long rval; | |
5832 | if (left_type->integer_type() != NULL | |
5833 | && this->right_->numeric_constant_value(&rconst) | |
5834 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5835 | && rval == 0) | |
5836 | { | |
5837 | this->report_error(_("integer division by zero")); | |
5838 | return; | |
5839 | } | |
5840 | } | |
e440a328 | 5841 | } |
5842 | else | |
5843 | { | |
5844 | if (left_type->integer_type() == NULL) | |
5845 | this->report_error(_("shift of non-integer operand")); | |
5846 | ||
5847 | if (!right_type->is_abstract() | |
5848 | && (right_type->integer_type() == NULL | |
5849 | || !right_type->integer_type()->is_unsigned())) | |
5850 | this->report_error(_("shift count not unsigned integer")); | |
5851 | else | |
5852 | { | |
0c77715b | 5853 | Numeric_constant nc; |
5854 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5855 | { |
0c77715b | 5856 | mpz_t val; |
5857 | if (!nc.to_int(&val)) | |
5858 | this->report_error(_("shift count not unsigned integer")); | |
5859 | else | |
a4eba91b | 5860 | { |
0c77715b | 5861 | if (mpz_sgn(val) < 0) |
5862 | { | |
5863 | this->report_error(_("negative shift count")); | |
5864 | mpz_set_ui(val, 0); | |
5865 | Location rloc = this->right_->location(); | |
5866 | this->right_ = Expression::make_integer(&val, right_type, | |
5867 | rloc); | |
5868 | } | |
5869 | mpz_clear(val); | |
a4eba91b | 5870 | } |
e440a328 | 5871 | } |
e440a328 | 5872 | } |
5873 | } | |
5874 | } | |
5875 | ||
5876 | // Get a tree for a binary expression. | |
5877 | ||
5878 | tree | |
5879 | Binary_expression::do_get_tree(Translate_context* context) | |
5880 | { | |
1b1f2abf | 5881 | Gogo* gogo = context->gogo(); |
a32698ee | 5882 | Location loc = this->location(); |
5883 | Type* left_type = this->left_->type(); | |
5884 | Type* right_type = this->right_->type(); | |
1b1f2abf | 5885 | |
e440a328 | 5886 | bool use_left_type = true; |
5887 | bool is_shift_op = false; | |
29a2d1d8 | 5888 | bool is_idiv_op = false; |
e440a328 | 5889 | switch (this->op_) |
5890 | { | |
5891 | case OPERATOR_EQEQ: | |
5892 | case OPERATOR_NOTEQ: | |
5893 | case OPERATOR_LT: | |
5894 | case OPERATOR_LE: | |
5895 | case OPERATOR_GT: | |
5896 | case OPERATOR_GE: | |
a32698ee | 5897 | { |
5898 | Bexpression* ret = | |
5899 | Expression::comparison(context, this->type_, this->op_, | |
5900 | this->left_, this->right_, loc); | |
5901 | return expr_to_tree(ret); | |
5902 | } | |
e440a328 | 5903 | |
5904 | case OPERATOR_OROR: | |
e440a328 | 5905 | case OPERATOR_ANDAND: |
e440a328 | 5906 | use_left_type = false; |
5907 | break; | |
5908 | case OPERATOR_PLUS: | |
e440a328 | 5909 | case OPERATOR_MINUS: |
e440a328 | 5910 | case OPERATOR_OR: |
e440a328 | 5911 | case OPERATOR_XOR: |
e440a328 | 5912 | case OPERATOR_MULT: |
e440a328 | 5913 | break; |
5914 | case OPERATOR_DIV: | |
a32698ee | 5915 | if (left_type->float_type() != NULL || left_type->complex_type() != NULL) |
5916 | break; | |
e440a328 | 5917 | case OPERATOR_MOD: |
29a2d1d8 | 5918 | is_idiv_op = true; |
e440a328 | 5919 | break; |
5920 | case OPERATOR_LSHIFT: | |
e440a328 | 5921 | case OPERATOR_RSHIFT: |
e440a328 | 5922 | is_shift_op = true; |
5923 | break; | |
e440a328 | 5924 | case OPERATOR_BITCLEAR: |
a32698ee | 5925 | this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc); |
5926 | case OPERATOR_AND: | |
e440a328 | 5927 | break; |
5928 | default: | |
c3e6f413 | 5929 | go_unreachable(); |
e440a328 | 5930 | } |
5931 | ||
a32698ee | 5932 | if (left_type->is_string_type()) |
e440a328 | 5933 | { |
c484d925 | 5934 | go_assert(this->op_ == OPERATOR_PLUS); |
a32698ee | 5935 | Expression* string_plus = |
5936 | Runtime::make_call(Runtime::STRING_PLUS, loc, 2, | |
5937 | this->left_, this->right_); | |
5938 | return string_plus->get_tree(context); | |
5939 | } | |
5940 | ||
5941 | // For complex division Go might want slightly different results than the | |
5942 | // backend implementation provides, so we have our own runtime routine. | |
1850e20c | 5943 | if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL) |
5944 | { | |
a32698ee | 5945 | Runtime::Function complex_code; |
1850e20c | 5946 | switch (this->left_->type()->complex_type()->bits()) |
5947 | { | |
5948 | case 64: | |
a32698ee | 5949 | complex_code = Runtime::COMPLEX64_DIV; |
1850e20c | 5950 | break; |
5951 | case 128: | |
a32698ee | 5952 | complex_code = Runtime::COMPLEX128_DIV; |
1850e20c | 5953 | break; |
5954 | default: | |
5955 | go_unreachable(); | |
5956 | } | |
a32698ee | 5957 | Expression* complex_div = |
5958 | Runtime::make_call(complex_code, loc, 2, this->left_, this->right_); | |
5959 | return complex_div->get_tree(context); | |
1850e20c | 5960 | } |
5961 | ||
a32698ee | 5962 | Bexpression* left = tree_to_expr(this->left_->get_tree(context)); |
5963 | Bexpression* right = tree_to_expr(this->right_->get_tree(context)); | |
e440a328 | 5964 | |
a32698ee | 5965 | Type* type = use_left_type ? left_type : right_type; |
5966 | Btype* btype = type->get_backend(gogo); | |
5967 | ||
5968 | Bexpression* ret = | |
5969 | gogo->backend()->binary_expression(this->op_, left, right, loc); | |
5970 | ret = gogo->backend()->convert_expression(btype, ret, loc); | |
e440a328 | 5971 | |
a32698ee | 5972 | // Initialize overflow constants. |
5973 | Bexpression* overflow; | |
5974 | mpz_t zero; | |
5975 | mpz_init_set_ui(zero, 0UL); | |
5976 | mpz_t one; | |
5977 | mpz_init_set_ui(one, 1UL); | |
5978 | mpz_t neg_one; | |
5979 | mpz_init_set_si(neg_one, -1); | |
e440a328 | 5980 | |
a32698ee | 5981 | Btype* left_btype = left_type->get_backend(gogo); |
5982 | Btype* right_btype = right_type->get_backend(gogo); | |
e440a328 | 5983 | |
5984 | // In Go, a shift larger than the size of the type is well-defined. | |
a32698ee | 5985 | // This is not true in C, so we need to insert a conditional. |
e440a328 | 5986 | if (is_shift_op) |
5987 | { | |
a32698ee | 5988 | go_assert(left_type->integer_type() != NULL); |
e440a328 | 5989 | |
a32698ee | 5990 | mpz_t bitsval; |
5991 | int bits = left_type->integer_type()->bits(); | |
5992 | mpz_init_set_ui(bitsval, bits); | |
5993 | Bexpression* bits_expr = | |
5994 | gogo->backend()->integer_constant_expression(right_btype, bitsval); | |
5995 | Bexpression* compare = | |
5996 | gogo->backend()->binary_expression(OPERATOR_LT, | |
5997 | right, bits_expr, loc); | |
e440a328 | 5998 | |
a32698ee | 5999 | Bexpression* zero_expr = |
6000 | gogo->backend()->integer_constant_expression(left_btype, zero); | |
6001 | overflow = zero_expr; | |
e440a328 | 6002 | if (this->op_ == OPERATOR_RSHIFT |
a32698ee | 6003 | && !left_type->integer_type()->is_unsigned()) |
e440a328 | 6004 | { |
a32698ee | 6005 | Bexpression* neg_expr = |
6006 | gogo->backend()->binary_expression(OPERATOR_LT, left, | |
6007 | zero_expr, loc); | |
6008 | Bexpression* neg_one_expr = | |
6009 | gogo->backend()->integer_constant_expression(left_btype, neg_one); | |
6010 | overflow = gogo->backend()->conditional_expression(btype, neg_expr, | |
6011 | neg_one_expr, | |
6012 | zero_expr, loc); | |
29a2d1d8 | 6013 | } |
a32698ee | 6014 | ret = gogo->backend()->conditional_expression(btype, compare, ret, |
6015 | overflow, loc); | |
6016 | mpz_clear(bitsval); | |
29a2d1d8 | 6017 | } |
6018 | ||
6019 | // Add checks for division by zero and division overflow as needed. | |
6020 | if (is_idiv_op) | |
6021 | { | |
6022 | if (go_check_divide_zero) | |
6023 | { | |
6024 | // right == 0 | |
a32698ee | 6025 | Bexpression* zero_expr = |
6026 | gogo->backend()->integer_constant_expression(right_btype, zero); | |
6027 | Bexpression* check = | |
6028 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6029 | right, zero_expr, loc); | |
29a2d1d8 | 6030 | |
a32698ee | 6031 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO) |
29a2d1d8 | 6032 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; |
a32698ee | 6033 | Expression* crash = gogo->runtime_error(errcode, loc); |
6034 | Bexpression* crash_expr = tree_to_expr(crash->get_tree(context)); | |
29a2d1d8 | 6035 | |
6036 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
a32698ee | 6037 | ret = gogo->backend()->conditional_expression(btype, check, |
6038 | crash_expr, ret, loc); | |
b13c66cd | 6039 | } |
6040 | ||
29a2d1d8 | 6041 | if (go_check_divide_overflow) |
6042 | { | |
6043 | // right == -1 | |
6044 | // FIXME: It would be nice to say that this test is expected | |
6045 | // to return false. | |
a32698ee | 6046 | |
6047 | Bexpression* neg_one_expr = | |
6048 | gogo->backend()->integer_constant_expression(right_btype, neg_one); | |
6049 | Bexpression* check = | |
6050 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6051 | right, neg_one_expr, loc); | |
6052 | ||
6053 | Bexpression* zero_expr = | |
6054 | gogo->backend()->integer_constant_expression(btype, zero); | |
6055 | Bexpression* one_expr = | |
6056 | gogo->backend()->integer_constant_expression(btype, one); | |
6057 | ||
6058 | if (type->integer_type()->is_unsigned()) | |
29a2d1d8 | 6059 | { |
6060 | // An unsigned -1 is the largest possible number, so | |
6061 | // dividing is always 1 or 0. | |
a32698ee | 6062 | |
6063 | Bexpression* cmp = | |
6064 | gogo->backend()->binary_expression(OPERATOR_EQEQ, | |
6065 | left, right, loc); | |
29a2d1d8 | 6066 | if (this->op_ == OPERATOR_DIV) |
a32698ee | 6067 | overflow = |
6068 | gogo->backend()->conditional_expression(btype, cmp, | |
6069 | one_expr, zero_expr, | |
6070 | loc); | |
29a2d1d8 | 6071 | else |
a32698ee | 6072 | overflow = |
6073 | gogo->backend()->conditional_expression(btype, cmp, | |
6074 | zero_expr, left, | |
6075 | loc); | |
29a2d1d8 | 6076 | } |
6077 | else | |
6078 | { | |
6079 | // Computing left / -1 is the same as computing - left, | |
6080 | // which does not overflow since Go sets -fwrapv. | |
6081 | if (this->op_ == OPERATOR_DIV) | |
a32698ee | 6082 | { |
6083 | Expression* negate_expr = | |
6084 | Expression::make_unary(OPERATOR_MINUS, this->left_, loc); | |
6085 | overflow = tree_to_expr(negate_expr->get_tree(context)); | |
6086 | } | |
29a2d1d8 | 6087 | else |
a32698ee | 6088 | overflow = zero_expr; |
29a2d1d8 | 6089 | } |
a32698ee | 6090 | overflow = gogo->backend()->convert_expression(btype, overflow, loc); |
29a2d1d8 | 6091 | |
6092 | // right == -1 ? - left : ret | |
a32698ee | 6093 | ret = gogo->backend()->conditional_expression(btype, check, overflow, |
6094 | ret, loc); | |
29a2d1d8 | 6095 | } |
e440a328 | 6096 | } |
6097 | ||
a32698ee | 6098 | mpz_clear(zero); |
6099 | mpz_clear(one); | |
6100 | mpz_clear(neg_one); | |
6101 | return expr_to_tree(ret); | |
e440a328 | 6102 | } |
6103 | ||
6104 | // Export a binary expression. | |
6105 | ||
6106 | void | |
6107 | Binary_expression::do_export(Export* exp) const | |
6108 | { | |
6109 | exp->write_c_string("("); | |
6110 | this->left_->export_expression(exp); | |
6111 | switch (this->op_) | |
6112 | { | |
6113 | case OPERATOR_OROR: | |
6114 | exp->write_c_string(" || "); | |
6115 | break; | |
6116 | case OPERATOR_ANDAND: | |
6117 | exp->write_c_string(" && "); | |
6118 | break; | |
6119 | case OPERATOR_EQEQ: | |
6120 | exp->write_c_string(" == "); | |
6121 | break; | |
6122 | case OPERATOR_NOTEQ: | |
6123 | exp->write_c_string(" != "); | |
6124 | break; | |
6125 | case OPERATOR_LT: | |
6126 | exp->write_c_string(" < "); | |
6127 | break; | |
6128 | case OPERATOR_LE: | |
6129 | exp->write_c_string(" <= "); | |
6130 | break; | |
6131 | case OPERATOR_GT: | |
6132 | exp->write_c_string(" > "); | |
6133 | break; | |
6134 | case OPERATOR_GE: | |
6135 | exp->write_c_string(" >= "); | |
6136 | break; | |
6137 | case OPERATOR_PLUS: | |
6138 | exp->write_c_string(" + "); | |
6139 | break; | |
6140 | case OPERATOR_MINUS: | |
6141 | exp->write_c_string(" - "); | |
6142 | break; | |
6143 | case OPERATOR_OR: | |
6144 | exp->write_c_string(" | "); | |
6145 | break; | |
6146 | case OPERATOR_XOR: | |
6147 | exp->write_c_string(" ^ "); | |
6148 | break; | |
6149 | case OPERATOR_MULT: | |
6150 | exp->write_c_string(" * "); | |
6151 | break; | |
6152 | case OPERATOR_DIV: | |
6153 | exp->write_c_string(" / "); | |
6154 | break; | |
6155 | case OPERATOR_MOD: | |
6156 | exp->write_c_string(" % "); | |
6157 | break; | |
6158 | case OPERATOR_LSHIFT: | |
6159 | exp->write_c_string(" << "); | |
6160 | break; | |
6161 | case OPERATOR_RSHIFT: | |
6162 | exp->write_c_string(" >> "); | |
6163 | break; | |
6164 | case OPERATOR_AND: | |
6165 | exp->write_c_string(" & "); | |
6166 | break; | |
6167 | case OPERATOR_BITCLEAR: | |
6168 | exp->write_c_string(" &^ "); | |
6169 | break; | |
6170 | default: | |
c3e6f413 | 6171 | go_unreachable(); |
e440a328 | 6172 | } |
6173 | this->right_->export_expression(exp); | |
6174 | exp->write_c_string(")"); | |
6175 | } | |
6176 | ||
6177 | // Import a binary expression. | |
6178 | ||
6179 | Expression* | |
6180 | Binary_expression::do_import(Import* imp) | |
6181 | { | |
6182 | imp->require_c_string("("); | |
6183 | ||
6184 | Expression* left = Expression::import_expression(imp); | |
6185 | ||
6186 | Operator op; | |
6187 | if (imp->match_c_string(" || ")) | |
6188 | { | |
6189 | op = OPERATOR_OROR; | |
6190 | imp->advance(4); | |
6191 | } | |
6192 | else if (imp->match_c_string(" && ")) | |
6193 | { | |
6194 | op = OPERATOR_ANDAND; | |
6195 | imp->advance(4); | |
6196 | } | |
6197 | else if (imp->match_c_string(" == ")) | |
6198 | { | |
6199 | op = OPERATOR_EQEQ; | |
6200 | imp->advance(4); | |
6201 | } | |
6202 | else if (imp->match_c_string(" != ")) | |
6203 | { | |
6204 | op = OPERATOR_NOTEQ; | |
6205 | imp->advance(4); | |
6206 | } | |
6207 | else if (imp->match_c_string(" < ")) | |
6208 | { | |
6209 | op = OPERATOR_LT; | |
6210 | imp->advance(3); | |
6211 | } | |
6212 | else if (imp->match_c_string(" <= ")) | |
6213 | { | |
6214 | op = OPERATOR_LE; | |
6215 | imp->advance(4); | |
6216 | } | |
6217 | else if (imp->match_c_string(" > ")) | |
6218 | { | |
6219 | op = OPERATOR_GT; | |
6220 | imp->advance(3); | |
6221 | } | |
6222 | else if (imp->match_c_string(" >= ")) | |
6223 | { | |
6224 | op = OPERATOR_GE; | |
6225 | imp->advance(4); | |
6226 | } | |
6227 | else if (imp->match_c_string(" + ")) | |
6228 | { | |
6229 | op = OPERATOR_PLUS; | |
6230 | imp->advance(3); | |
6231 | } | |
6232 | else if (imp->match_c_string(" - ")) | |
6233 | { | |
6234 | op = OPERATOR_MINUS; | |
6235 | imp->advance(3); | |
6236 | } | |
6237 | else if (imp->match_c_string(" | ")) | |
6238 | { | |
6239 | op = OPERATOR_OR; | |
6240 | imp->advance(3); | |
6241 | } | |
6242 | else if (imp->match_c_string(" ^ ")) | |
6243 | { | |
6244 | op = OPERATOR_XOR; | |
6245 | imp->advance(3); | |
6246 | } | |
6247 | else if (imp->match_c_string(" * ")) | |
6248 | { | |
6249 | op = OPERATOR_MULT; | |
6250 | imp->advance(3); | |
6251 | } | |
6252 | else if (imp->match_c_string(" / ")) | |
6253 | { | |
6254 | op = OPERATOR_DIV; | |
6255 | imp->advance(3); | |
6256 | } | |
6257 | else if (imp->match_c_string(" % ")) | |
6258 | { | |
6259 | op = OPERATOR_MOD; | |
6260 | imp->advance(3); | |
6261 | } | |
6262 | else if (imp->match_c_string(" << ")) | |
6263 | { | |
6264 | op = OPERATOR_LSHIFT; | |
6265 | imp->advance(4); | |
6266 | } | |
6267 | else if (imp->match_c_string(" >> ")) | |
6268 | { | |
6269 | op = OPERATOR_RSHIFT; | |
6270 | imp->advance(4); | |
6271 | } | |
6272 | else if (imp->match_c_string(" & ")) | |
6273 | { | |
6274 | op = OPERATOR_AND; | |
6275 | imp->advance(3); | |
6276 | } | |
6277 | else if (imp->match_c_string(" &^ ")) | |
6278 | { | |
6279 | op = OPERATOR_BITCLEAR; | |
6280 | imp->advance(4); | |
6281 | } | |
6282 | else | |
6283 | { | |
6284 | error_at(imp->location(), "unrecognized binary operator"); | |
6285 | return Expression::make_error(imp->location()); | |
6286 | } | |
6287 | ||
6288 | Expression* right = Expression::import_expression(imp); | |
6289 | ||
6290 | imp->require_c_string(")"); | |
6291 | ||
6292 | return Expression::make_binary(op, left, right, imp->location()); | |
6293 | } | |
6294 | ||
d751bb78 | 6295 | // Dump ast representation of a binary expression. |
6296 | ||
6297 | void | |
6298 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6299 | { | |
6300 | ast_dump_context->ostream() << "("; | |
6301 | ast_dump_context->dump_expression(this->left_); | |
6302 | ast_dump_context->ostream() << " "; | |
6303 | ast_dump_context->dump_operator(this->op_); | |
6304 | ast_dump_context->ostream() << " "; | |
6305 | ast_dump_context->dump_expression(this->right_); | |
6306 | ast_dump_context->ostream() << ") "; | |
6307 | } | |
6308 | ||
e440a328 | 6309 | // Make a binary expression. |
6310 | ||
6311 | Expression* | |
6312 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6313 | Location location) |
e440a328 | 6314 | { |
6315 | return new Binary_expression(op, left, right, location); | |
6316 | } | |
6317 | ||
6318 | // Implement a comparison. | |
6319 | ||
a32698ee | 6320 | Bexpression* |
6321 | Expression::comparison(Translate_context* context, Type* result_type, | |
6322 | Operator op, Expression* left, Expression* right, | |
6323 | Location location) | |
e440a328 | 6324 | { |
2387f644 | 6325 | Type* left_type = left->type(); |
6326 | Type* right_type = right->type(); | |
ceeb12d7 | 6327 | |
6328 | mpz_t zval; | |
6329 | mpz_init_set_ui(zval, 0UL); | |
6330 | Expression* zexpr = Expression::make_integer(&zval, NULL, location); | |
6331 | mpz_clear(zval); | |
1b1f2abf | 6332 | |
15c67ee2 | 6333 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6334 | { |
2387f644 | 6335 | left = Runtime::make_call(Runtime::STRCMP, location, 2, |
6336 | left, right); | |
6337 | right = zexpr; | |
e440a328 | 6338 | } |
15c67ee2 | 6339 | else if ((left_type->interface_type() != NULL |
6340 | && right_type->interface_type() == NULL | |
6341 | && !right_type->is_nil_type()) | |
6342 | || (left_type->interface_type() == NULL | |
6343 | && !left_type->is_nil_type() | |
6344 | && right_type->interface_type() != NULL)) | |
e440a328 | 6345 | { |
6346 | // Comparing an interface value to a non-interface value. | |
6347 | if (left_type->interface_type() == NULL) | |
6348 | { | |
6349 | std::swap(left_type, right_type); | |
2387f644 | 6350 | std::swap(left, right); |
e440a328 | 6351 | } |
6352 | ||
6353 | // The right operand is not an interface. We need to take its | |
6354 | // address if it is not a pointer. | |
ceeb12d7 | 6355 | Expression* pointer_arg = NULL; |
e440a328 | 6356 | if (right_type->points_to() != NULL) |
2387f644 | 6357 | pointer_arg = right; |
e440a328 | 6358 | else |
6359 | { | |
2387f644 | 6360 | go_assert(right->is_addressable()); |
6361 | pointer_arg = Expression::make_unary(OPERATOR_AND, right, | |
ceeb12d7 | 6362 | location); |
e440a328 | 6363 | } |
e440a328 | 6364 | |
2387f644 | 6365 | Expression* descriptor = |
6366 | Expression::make_type_descriptor(right_type, location); | |
6367 | left = | |
ceeb12d7 | 6368 | Runtime::make_call((left_type->interface_type()->is_empty() |
6369 | ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE | |
6370 | : Runtime::INTERFACE_VALUE_COMPARE), | |
2387f644 | 6371 | location, 3, left, descriptor, |
ceeb12d7 | 6372 | pointer_arg); |
2387f644 | 6373 | right = zexpr; |
e440a328 | 6374 | } |
6375 | else if (left_type->interface_type() != NULL | |
6376 | && right_type->interface_type() != NULL) | |
6377 | { | |
ceeb12d7 | 6378 | Runtime::Function compare_function; |
739bad04 | 6379 | if (left_type->interface_type()->is_empty() |
6380 | && right_type->interface_type()->is_empty()) | |
ceeb12d7 | 6381 | compare_function = Runtime::EMPTY_INTERFACE_COMPARE; |
739bad04 | 6382 | else if (!left_type->interface_type()->is_empty() |
6383 | && !right_type->interface_type()->is_empty()) | |
ceeb12d7 | 6384 | compare_function = Runtime::INTERFACE_COMPARE; |
739bad04 | 6385 | else |
6386 | { | |
6387 | if (left_type->interface_type()->is_empty()) | |
6388 | { | |
c484d925 | 6389 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
739bad04 | 6390 | std::swap(left_type, right_type); |
2387f644 | 6391 | std::swap(left, right); |
739bad04 | 6392 | } |
c484d925 | 6393 | go_assert(!left_type->interface_type()->is_empty()); |
6394 | go_assert(right_type->interface_type()->is_empty()); | |
ceeb12d7 | 6395 | compare_function = Runtime::INTERFACE_EMPTY_COMPARE; |
739bad04 | 6396 | } |
6397 | ||
2387f644 | 6398 | left = Runtime::make_call(compare_function, location, 2, left, right); |
6399 | right = zexpr; | |
e440a328 | 6400 | } |
6401 | ||
6402 | if (left_type->is_nil_type() | |
6403 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6404 | { | |
6405 | std::swap(left_type, right_type); | |
2387f644 | 6406 | std::swap(left, right); |
e440a328 | 6407 | } |
6408 | ||
6409 | if (right_type->is_nil_type()) | |
6410 | { | |
2387f644 | 6411 | right = Expression::make_nil(location); |
e440a328 | 6412 | if (left_type->array_type() != NULL |
6413 | && left_type->array_type()->length() == NULL) | |
6414 | { | |
6415 | Array_type* at = left_type->array_type(); | |
2387f644 | 6416 | left = at->get_value_pointer(context->gogo(), left); |
e440a328 | 6417 | } |
6418 | else if (left_type->interface_type() != NULL) | |
6419 | { | |
6420 | // An interface is nil if the first field is nil. | |
2387f644 | 6421 | left = Expression::make_field_reference(left, 0, location); |
e440a328 | 6422 | } |
6423 | } | |
6424 | ||
a32698ee | 6425 | Bexpression* left_bexpr = tree_to_expr(left->get_tree(context)); |
6426 | Bexpression* right_bexpr = tree_to_expr(right->get_tree(context)); | |
e90c9dfc | 6427 | |
a32698ee | 6428 | Gogo* gogo = context->gogo(); |
6429 | Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr, | |
6430 | right_bexpr, location); | |
6431 | if (result_type != NULL) | |
6432 | ret = gogo->backend()->convert_expression(result_type->get_backend(gogo), | |
6433 | ret, location); | |
e440a328 | 6434 | return ret; |
6435 | } | |
6436 | ||
6437 | // Class Bound_method_expression. | |
6438 | ||
6439 | // Traversal. | |
6440 | ||
6441 | int | |
6442 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6443 | { | |
e0659c9e | 6444 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6445 | } |
6446 | ||
0afbb937 | 6447 | // Lower the expression. If this is a method value rather than being |
6448 | // called, and the method is accessed via a pointer, we may need to | |
6449 | // add nil checks. Introduce a temporary variable so that those nil | |
6450 | // checks do not cause multiple evaluation. | |
6451 | ||
6452 | Expression* | |
6453 | Bound_method_expression::do_lower(Gogo*, Named_object*, | |
6454 | Statement_inserter* inserter, int) | |
6455 | { | |
6456 | // For simplicity we use a temporary for every call to an embedded | |
6457 | // method, even though some of them might be pure value methods and | |
6458 | // not require a temporary. | |
6459 | if (this->expr_->var_expression() == NULL | |
6460 | && this->expr_->temporary_reference_expression() == NULL | |
6461 | && this->expr_->set_and_use_temporary_expression() == NULL | |
6462 | && (this->method_->field_indexes() != NULL | |
6463 | || (this->method_->is_value_method() | |
6464 | && this->expr_->type()->points_to() != NULL))) | |
6465 | { | |
6466 | Temporary_statement* temp = | |
6467 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
6468 | inserter->insert(temp); | |
6469 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
6470 | this->location()); | |
6471 | } | |
6472 | return this; | |
6473 | } | |
6474 | ||
e440a328 | 6475 | // Return the type of a bound method expression. The type of this |
0afbb937 | 6476 | // object is simply the type of the method with no receiver. |
e440a328 | 6477 | |
6478 | Type* | |
6479 | Bound_method_expression::do_type() | |
6480 | { | |
0afbb937 | 6481 | Named_object* fn = this->method_->named_object(); |
6482 | Function_type* fntype; | |
6483 | if (fn->is_function()) | |
6484 | fntype = fn->func_value()->type(); | |
6485 | else if (fn->is_function_declaration()) | |
6486 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6487 | else |
6488 | return Type::make_error_type(); | |
0afbb937 | 6489 | return fntype->copy_without_receiver(); |
e440a328 | 6490 | } |
6491 | ||
6492 | // Determine the types of a method expression. | |
6493 | ||
6494 | void | |
6495 | Bound_method_expression::do_determine_type(const Type_context*) | |
6496 | { | |
0afbb937 | 6497 | Named_object* fn = this->method_->named_object(); |
6498 | Function_type* fntype; | |
6499 | if (fn->is_function()) | |
6500 | fntype = fn->func_value()->type(); | |
6501 | else if (fn->is_function_declaration()) | |
6502 | fntype = fn->func_declaration_value()->type(); | |
6503 | else | |
6504 | fntype = NULL; | |
e440a328 | 6505 | if (fntype == NULL || !fntype->is_method()) |
6506 | this->expr_->determine_type_no_context(); | |
6507 | else | |
6508 | { | |
6509 | Type_context subcontext(fntype->receiver()->type(), false); | |
6510 | this->expr_->determine_type(&subcontext); | |
6511 | } | |
6512 | } | |
6513 | ||
6514 | // Check the types of a method expression. | |
6515 | ||
6516 | void | |
6517 | Bound_method_expression::do_check_types(Gogo*) | |
6518 | { | |
0afbb937 | 6519 | Named_object* fn = this->method_->named_object(); |
6520 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6521 | { | |
6522 | this->report_error(_("object is not a method")); | |
6523 | return; | |
6524 | } | |
6525 | ||
6526 | Function_type* fntype; | |
6527 | if (fn->is_function()) | |
6528 | fntype = fn->func_value()->type(); | |
6529 | else if (fn->is_function_declaration()) | |
6530 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6531 | else |
0afbb937 | 6532 | go_unreachable(); |
6533 | Type* rtype = fntype->receiver()->type()->deref(); | |
6534 | Type* etype = (this->expr_type_ != NULL | |
6535 | ? this->expr_type_ | |
6536 | : this->expr_->type()); | |
6537 | etype = etype->deref(); | |
6538 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6539 | this->report_error(_("method type does not match object type")); | |
6540 | } | |
6541 | ||
6542 | // If a bound method expression is not simply called, then it is | |
6543 | // represented as a closure. The closure will hold a single variable, | |
6544 | // the receiver to pass to the method. The function will be a simple | |
6545 | // thunk that pulls that value from the closure and calls the method | |
6546 | // with the remaining arguments. | |
6547 | // | |
6548 | // Because method values are not common, we don't build all thunks for | |
6549 | // every methods, but instead only build them as we need them. In | |
6550 | // particular, we even build them on demand for methods defined in | |
6551 | // other packages. | |
6552 | ||
6553 | Bound_method_expression::Method_value_thunks | |
6554 | Bound_method_expression::method_value_thunks; | |
6555 | ||
6556 | // Find or create the thunk for METHOD. | |
6557 | ||
6558 | Named_object* | |
6559 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6560 | Named_object* fn) | |
6561 | { | |
6562 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6563 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6564 | Bound_method_expression::method_value_thunks.insert(val); | |
6565 | if (!ins.second) | |
6566 | { | |
6567 | // We have seen this method before. | |
6568 | go_assert(ins.first->second != NULL); | |
6569 | return ins.first->second; | |
6570 | } | |
6571 | ||
6572 | Location loc = fn->location(); | |
6573 | ||
6574 | Function_type* orig_fntype; | |
6575 | if (fn->is_function()) | |
6576 | orig_fntype = fn->func_value()->type(); | |
6577 | else if (fn->is_function_declaration()) | |
6578 | orig_fntype = fn->func_declaration_value()->type(); | |
6579 | else | |
6580 | orig_fntype = NULL; | |
6581 | ||
6582 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6583 | { |
0afbb937 | 6584 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6585 | return ins.first->second; | |
e440a328 | 6586 | } |
0afbb937 | 6587 | |
6588 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6589 | // The type here is wrong--it should be the C function type. But it |
6590 | // doesn't really matter. | |
0afbb937 | 6591 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6592 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6593 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6594 | orig_fntype->receiver()->type(), | |
6595 | loc))); | |
6596 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
6597 | closure_type = Type::make_pointer_type(closure_type); | |
6598 | ||
f8bdf81a | 6599 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6600 | |
6601 | Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, | |
6602 | false, loc); | |
6603 | ||
f8bdf81a | 6604 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6605 | cvar->set_is_used(); | |
6606 | Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); | |
6607 | new_no->func_value()->set_closure_var(cp); | |
0afbb937 | 6608 | |
f8bdf81a | 6609 | gogo->start_block(loc); |
0afbb937 | 6610 | |
6611 | // Field 0 of the closure is the function code pointer, field 1 is | |
6612 | // the value on which to invoke the method. | |
6613 | Expression* arg = Expression::make_var_reference(cp, loc); | |
6614 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
6615 | arg = Expression::make_field_reference(arg, 1, loc); | |
6616 | ||
6617 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6618 | ||
6619 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6620 | Expression_list* args; | |
6621 | if (orig_params == NULL || orig_params->empty()) | |
6622 | args = NULL; | |
6623 | else | |
6624 | { | |
6625 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6626 | args = new Expression_list(); | |
6627 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6628 | p != new_params->end(); |
0afbb937 | 6629 | ++p) |
6630 | { | |
6631 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6632 | go_assert(p_no != NULL | |
6633 | && p_no->is_variable() | |
6634 | && p_no->var_value()->is_parameter()); | |
6635 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6636 | } | |
6637 | } | |
6638 | ||
6639 | Call_expression* call = Expression::make_call(bme, args, | |
6640 | orig_fntype->is_varargs(), | |
6641 | loc); | |
6642 | call->set_varargs_are_lowered(); | |
6643 | ||
6644 | Statement* s = Statement::make_return_from_call(call, loc); | |
6645 | gogo->add_statement(s); | |
6646 | Block* b = gogo->finish_block(loc); | |
6647 | gogo->add_block(b, loc); | |
6648 | gogo->lower_block(new_no, b); | |
a32698ee | 6649 | gogo->flatten_block(new_no, b); |
0afbb937 | 6650 | gogo->finish_function(loc); |
6651 | ||
6652 | ins.first->second = new_no; | |
6653 | return new_no; | |
6654 | } | |
6655 | ||
6656 | // Return an expression to check *REF for nil while dereferencing | |
6657 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6658 | // reference. This is a static function so that we don't have to | |
6659 | // worry about declaring Field_indexes in expressions.h. | |
6660 | ||
6661 | static Expression* | |
6662 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6663 | Expression** ref) | |
6664 | { | |
6665 | if (field_indexes == NULL) | |
6666 | return Expression::make_boolean(false, loc); | |
6667 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6668 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6669 | go_assert(stype != NULL | |
6670 | && field_indexes->field_index < stype->field_count()); | |
6671 | if ((*ref)->type()->struct_type() == NULL) | |
6672 | { | |
6673 | go_assert((*ref)->type()->points_to() != NULL); | |
6674 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6675 | Expression::make_nil(loc), | |
6676 | loc); | |
6677 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
6678 | *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); | |
6679 | go_assert((*ref)->type()->struct_type() == stype); | |
6680 | } | |
6681 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6682 | loc); | |
6683 | return cond; | |
e440a328 | 6684 | } |
6685 | ||
0afbb937 | 6686 | // Get the tree for a method value. |
e440a328 | 6687 | |
6688 | tree | |
0afbb937 | 6689 | Bound_method_expression::do_get_tree(Translate_context* context) |
e440a328 | 6690 | { |
0afbb937 | 6691 | Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(), |
6692 | this->method_, | |
6693 | this->function_); | |
6694 | if (thunk->is_erroneous()) | |
6695 | { | |
6696 | go_assert(saw_errors()); | |
6697 | return error_mark_node; | |
6698 | } | |
6699 | ||
6700 | // FIXME: We should lower this earlier, but we can't lower it in the | |
6701 | // lowering pass because at that point we don't know whether we need | |
6702 | // to create the thunk or not. If the expression is called, we | |
6703 | // don't need the thunk. | |
6704 | ||
6705 | Location loc = this->location(); | |
6706 | ||
6707 | // If the method expects a value, and we have a pointer, we need to | |
6708 | // dereference the pointer. | |
6709 | ||
6710 | Named_object* fn = this->method_->named_object(); | |
6711 | Function_type* fntype; | |
6712 | if (fn->is_function()) | |
6713 | fntype = fn->func_value()->type(); | |
6714 | else if (fn->is_function_declaration()) | |
6715 | fntype = fn->func_declaration_value()->type(); | |
6716 | else | |
6717 | go_unreachable(); | |
6718 | ||
6719 | Expression* val = this->expr_; | |
6720 | if (fntype->receiver()->type()->points_to() == NULL | |
6721 | && val->type()->points_to() != NULL) | |
6722 | val = Expression::make_unary(OPERATOR_MULT, val, loc); | |
6723 | ||
6724 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
6725 | // expect a closure whose second field has type this->expr_type_ (if | |
6726 | // that is not NULL). We are going to pass it a closure whose | |
6727 | // second field has type this->expr_->type(). Since | |
6728 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
6729 | // away with this. | |
6730 | ||
6731 | Struct_field_list* fields = new Struct_field_list(); | |
6732 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
6733 | thunk->func_value()->type(), | |
6734 | loc))); | |
6735 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
6736 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6737 | ||
6738 | Expression_list* vals = new Expression_list(); | |
6739 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
6740 | vals->push_back(val); | |
6741 | ||
6742 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
2c809f8f | 6743 | ret = Expression::make_heap_expression(ret, loc); |
0afbb937 | 6744 | |
0afbb937 | 6745 | // See whether the expression or any embedded pointers are nil. |
6746 | ||
df7ef1fd | 6747 | Expression* nil_check = NULL; |
0afbb937 | 6748 | Expression* expr = this->expr_; |
6749 | if (this->method_->field_indexes() != NULL) | |
6750 | { | |
6751 | // Note that we are evaluating this->expr_ twice, but that is OK | |
6752 | // because in the lowering pass we forced it into a temporary | |
6753 | // variable. | |
6754 | Expression* ref = expr; | |
6755 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
6756 | expr = ref; | |
6757 | } | |
6758 | ||
6759 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
6760 | { | |
6761 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
6762 | Expression::make_nil(loc), | |
6763 | loc); | |
6764 | if (nil_check == NULL) | |
6765 | nil_check = n; | |
6766 | else | |
6767 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
6768 | } | |
6769 | ||
df7ef1fd | 6770 | Bexpression* bme = tree_to_expr(ret->get_tree(context)); |
0afbb937 | 6771 | if (nil_check != NULL) |
6772 | { | |
df7ef1fd | 6773 | Gogo* gogo = context->gogo(); |
6774 | Expression* crash = | |
6775 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, loc); | |
6776 | Bexpression* bcrash = tree_to_expr(crash->get_tree(context)); | |
6777 | Btype* btype = ret->type()->get_backend(gogo); | |
6778 | Bexpression* bcheck = tree_to_expr(nil_check->get_tree(context)); | |
6779 | bme = gogo->backend()->conditional_expression(btype, bcheck, bcrash, | |
6780 | bme, loc); | |
6781 | } | |
6782 | return expr_to_tree(bme); | |
e440a328 | 6783 | } |
6784 | ||
d751bb78 | 6785 | // Dump ast representation of a bound method expression. |
6786 | ||
6787 | void | |
6788 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6789 | const | |
6790 | { | |
6791 | if (this->expr_type_ != NULL) | |
6792 | ast_dump_context->ostream() << "("; | |
6793 | ast_dump_context->dump_expression(this->expr_); | |
6794 | if (this->expr_type_ != NULL) | |
6795 | { | |
6796 | ast_dump_context->ostream() << ":"; | |
6797 | ast_dump_context->dump_type(this->expr_type_); | |
6798 | ast_dump_context->ostream() << ")"; | |
6799 | } | |
6800 | ||
0afbb937 | 6801 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 6802 | } |
6803 | ||
e440a328 | 6804 | // Make a method expression. |
6805 | ||
6806 | Bound_method_expression* | |
0afbb937 | 6807 | Expression::make_bound_method(Expression* expr, const Method* method, |
6808 | Named_object* function, Location location) | |
e440a328 | 6809 | { |
0afbb937 | 6810 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 6811 | } |
6812 | ||
6813 | // Class Builtin_call_expression. This is used for a call to a | |
6814 | // builtin function. | |
6815 | ||
6816 | class Builtin_call_expression : public Call_expression | |
6817 | { | |
6818 | public: | |
6819 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6820 | bool is_varargs, Location location); |
e440a328 | 6821 | |
6822 | protected: | |
6823 | // This overrides Call_expression::do_lower. | |
6824 | Expression* | |
ceeb4318 | 6825 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6826 | |
35a54f17 | 6827 | Expression* |
6828 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
6829 | ||
e440a328 | 6830 | bool |
6831 | do_is_constant() const; | |
6832 | ||
6833 | bool | |
0c77715b | 6834 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6835 | |
4f2138d7 | 6836 | bool |
a7549a6a | 6837 | do_discarding_value(); |
6838 | ||
e440a328 | 6839 | Type* |
6840 | do_type(); | |
6841 | ||
6842 | void | |
6843 | do_determine_type(const Type_context*); | |
6844 | ||
6845 | void | |
6846 | do_check_types(Gogo*); | |
6847 | ||
6848 | Expression* | |
6849 | do_copy() | |
6850 | { | |
6851 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6852 | this->args()->copy(), | |
6853 | this->is_varargs(), | |
6854 | this->location()); | |
6855 | } | |
6856 | ||
6857 | tree | |
6858 | do_get_tree(Translate_context*); | |
6859 | ||
6860 | void | |
6861 | do_export(Export*) const; | |
6862 | ||
6863 | virtual bool | |
6864 | do_is_recover_call() const; | |
6865 | ||
6866 | virtual void | |
6867 | do_set_recover_arg(Expression*); | |
6868 | ||
6869 | private: | |
6870 | // The builtin functions. | |
6871 | enum Builtin_function_code | |
6872 | { | |
6873 | BUILTIN_INVALID, | |
6874 | ||
6875 | // Predeclared builtin functions. | |
6876 | BUILTIN_APPEND, | |
6877 | BUILTIN_CAP, | |
6878 | BUILTIN_CLOSE, | |
48080209 | 6879 | BUILTIN_COMPLEX, |
e440a328 | 6880 | BUILTIN_COPY, |
1cce762f | 6881 | BUILTIN_DELETE, |
e440a328 | 6882 | BUILTIN_IMAG, |
6883 | BUILTIN_LEN, | |
6884 | BUILTIN_MAKE, | |
6885 | BUILTIN_NEW, | |
6886 | BUILTIN_PANIC, | |
6887 | BUILTIN_PRINT, | |
6888 | BUILTIN_PRINTLN, | |
6889 | BUILTIN_REAL, | |
6890 | BUILTIN_RECOVER, | |
6891 | ||
6892 | // Builtin functions from the unsafe package. | |
6893 | BUILTIN_ALIGNOF, | |
6894 | BUILTIN_OFFSETOF, | |
6895 | BUILTIN_SIZEOF | |
6896 | }; | |
6897 | ||
6898 | Expression* | |
6899 | one_arg() const; | |
6900 | ||
6901 | bool | |
6902 | check_one_arg(); | |
6903 | ||
6904 | static Type* | |
6905 | real_imag_type(Type*); | |
6906 | ||
6907 | static Type* | |
48080209 | 6908 | complex_type(Type*); |
e440a328 | 6909 | |
a9182619 | 6910 | Expression* |
6911 | lower_make(); | |
6912 | ||
6913 | bool | |
1ad00fd4 | 6914 | check_int_value(Expression*, bool is_length); |
a9182619 | 6915 | |
e440a328 | 6916 | // A pointer back to the general IR structure. This avoids a global |
6917 | // variable, or passing it around everywhere. | |
6918 | Gogo* gogo_; | |
6919 | // The builtin function being called. | |
6920 | Builtin_function_code code_; | |
0f914071 | 6921 | // Used to stop endless loops when the length of an array uses len |
6922 | // or cap of the array itself. | |
6923 | mutable bool seen_; | |
e440a328 | 6924 | }; |
6925 | ||
6926 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6927 | Expression* fn, | |
6928 | Expression_list* args, | |
6929 | bool is_varargs, | |
b13c66cd | 6930 | Location location) |
e440a328 | 6931 | : Call_expression(fn, args, is_varargs, location), |
0f914071 | 6932 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 6933 | { |
6934 | Func_expression* fnexp = this->fn()->func_expression(); | |
c484d925 | 6935 | go_assert(fnexp != NULL); |
e440a328 | 6936 | const std::string& name(fnexp->named_object()->name()); |
6937 | if (name == "append") | |
6938 | this->code_ = BUILTIN_APPEND; | |
6939 | else if (name == "cap") | |
6940 | this->code_ = BUILTIN_CAP; | |
6941 | else if (name == "close") | |
6942 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6943 | else if (name == "complex") |
6944 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6945 | else if (name == "copy") |
6946 | this->code_ = BUILTIN_COPY; | |
1cce762f | 6947 | else if (name == "delete") |
6948 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 6949 | else if (name == "imag") |
6950 | this->code_ = BUILTIN_IMAG; | |
6951 | else if (name == "len") | |
6952 | this->code_ = BUILTIN_LEN; | |
6953 | else if (name == "make") | |
6954 | this->code_ = BUILTIN_MAKE; | |
6955 | else if (name == "new") | |
6956 | this->code_ = BUILTIN_NEW; | |
6957 | else if (name == "panic") | |
6958 | this->code_ = BUILTIN_PANIC; | |
6959 | else if (name == "print") | |
6960 | this->code_ = BUILTIN_PRINT; | |
6961 | else if (name == "println") | |
6962 | this->code_ = BUILTIN_PRINTLN; | |
6963 | else if (name == "real") | |
6964 | this->code_ = BUILTIN_REAL; | |
6965 | else if (name == "recover") | |
6966 | this->code_ = BUILTIN_RECOVER; | |
6967 | else if (name == "Alignof") | |
6968 | this->code_ = BUILTIN_ALIGNOF; | |
6969 | else if (name == "Offsetof") | |
6970 | this->code_ = BUILTIN_OFFSETOF; | |
6971 | else if (name == "Sizeof") | |
6972 | this->code_ = BUILTIN_SIZEOF; | |
6973 | else | |
c3e6f413 | 6974 | go_unreachable(); |
e440a328 | 6975 | } |
6976 | ||
6977 | // Return whether this is a call to recover. This is a virtual | |
6978 | // function called from the parent class. | |
6979 | ||
6980 | bool | |
6981 | Builtin_call_expression::do_is_recover_call() const | |
6982 | { | |
6983 | if (this->classification() == EXPRESSION_ERROR) | |
6984 | return false; | |
6985 | return this->code_ == BUILTIN_RECOVER; | |
6986 | } | |
6987 | ||
6988 | // Set the argument for a call to recover. | |
6989 | ||
6990 | void | |
6991 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6992 | { | |
6993 | const Expression_list* args = this->args(); | |
c484d925 | 6994 | go_assert(args == NULL || args->empty()); |
e440a328 | 6995 | Expression_list* new_args = new Expression_list(); |
6996 | new_args->push_back(arg); | |
6997 | this->set_args(new_args); | |
6998 | } | |
6999 | ||
e440a328 | 7000 | // Lower a builtin call expression. This turns new and make into |
7001 | // specific expressions. We also convert to a constant if we can. | |
7002 | ||
7003 | Expression* | |
ceeb4318 | 7004 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
7005 | Statement_inserter* inserter, int) | |
e440a328 | 7006 | { |
a9182619 | 7007 | if (this->classification() == EXPRESSION_ERROR) |
7008 | return this; | |
7009 | ||
b13c66cd | 7010 | Location loc = this->location(); |
1cce762f | 7011 | |
a8725655 | 7012 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7013 | { | |
7014 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7015 | return Expression::make_error(loc); |
a8725655 | 7016 | } |
7017 | ||
393ba00b | 7018 | if (this->code_ == BUILTIN_OFFSETOF) |
7019 | { | |
7020 | Expression* arg = this->one_arg(); | |
12e69faa | 7021 | |
7022 | if (arg->bound_method_expression() != NULL | |
7023 | || arg->interface_field_reference_expression() != NULL) | |
7024 | { | |
7025 | this->report_error(_("invalid use of method value as argument " | |
7026 | "of Offsetof")); | |
7027 | return this; | |
7028 | } | |
7029 | ||
393ba00b | 7030 | Field_reference_expression* farg = arg->field_reference_expression(); |
7031 | while (farg != NULL) | |
7032 | { | |
7033 | if (!farg->implicit()) | |
7034 | break; | |
7035 | // When the selector refers to an embedded field, | |
7036 | // it must not be reached through pointer indirections. | |
7037 | if (farg->expr()->deref() != farg->expr()) | |
7038 | { | |
12e69faa | 7039 | this->report_error(_("argument of Offsetof implies " |
7040 | "indirection of an embedded field")); | |
393ba00b | 7041 | return this; |
7042 | } | |
7043 | // Go up until we reach the original base. | |
7044 | farg = farg->expr()->field_reference_expression(); | |
7045 | } | |
7046 | } | |
7047 | ||
1cce762f | 7048 | if (this->is_constant()) |
e440a328 | 7049 | { |
0c77715b | 7050 | Numeric_constant nc; |
7051 | if (this->numeric_constant_value(&nc)) | |
7052 | return nc.expression(loc); | |
e440a328 | 7053 | } |
1cce762f | 7054 | |
7055 | switch (this->code_) | |
e440a328 | 7056 | { |
1cce762f | 7057 | default: |
7058 | break; | |
7059 | ||
7060 | case BUILTIN_NEW: | |
7061 | { | |
7062 | const Expression_list* args = this->args(); | |
7063 | if (args == NULL || args->size() < 1) | |
7064 | this->report_error(_("not enough arguments")); | |
7065 | else if (args->size() > 1) | |
7066 | this->report_error(_("too many arguments")); | |
7067 | else | |
7068 | { | |
7069 | Expression* arg = args->front(); | |
7070 | if (!arg->is_type_expression()) | |
7071 | { | |
7072 | error_at(arg->location(), "expected type"); | |
7073 | this->set_is_error(); | |
7074 | } | |
7075 | else | |
7076 | return Expression::make_allocation(arg->type(), loc); | |
7077 | } | |
7078 | } | |
7079 | break; | |
7080 | ||
7081 | case BUILTIN_MAKE: | |
7082 | return this->lower_make(); | |
7083 | ||
7084 | case BUILTIN_RECOVER: | |
e440a328 | 7085 | if (function != NULL) |
7086 | function->func_value()->set_calls_recover(); | |
7087 | else | |
7088 | { | |
7089 | // Calling recover outside of a function always returns the | |
7090 | // nil empty interface. | |
823c7e3d | 7091 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7092 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7093 | } |
1cce762f | 7094 | break; |
7095 | ||
7096 | case BUILTIN_APPEND: | |
7097 | { | |
7098 | // Lower the varargs. | |
7099 | const Expression_list* args = this->args(); | |
7100 | if (args == NULL || args->empty()) | |
e440a328 | 7101 | return this; |
1cce762f | 7102 | Type* slice_type = args->front()->type(); |
7103 | if (!slice_type->is_slice_type()) | |
7104 | { | |
3ff4863b | 7105 | if (slice_type->is_nil_type()) |
7106 | error_at(args->front()->location(), "use of untyped nil"); | |
7107 | else | |
7108 | error_at(args->front()->location(), | |
7109 | "argument 1 must be a slice"); | |
1cce762f | 7110 | this->set_is_error(); |
7111 | return this; | |
7112 | } | |
19fd40c3 | 7113 | Type* element_type = slice_type->array_type()->element_type(); |
7114 | this->lower_varargs(gogo, function, inserter, | |
7115 | Type::make_array_type(element_type, NULL), | |
7116 | 2); | |
1cce762f | 7117 | } |
7118 | break; | |
7119 | ||
7120 | case BUILTIN_DELETE: | |
7121 | { | |
7122 | // Lower to a runtime function call. | |
7123 | const Expression_list* args = this->args(); | |
7124 | if (args == NULL || args->size() < 2) | |
7125 | this->report_error(_("not enough arguments")); | |
7126 | else if (args->size() > 2) | |
7127 | this->report_error(_("too many arguments")); | |
7128 | else if (args->front()->type()->map_type() == NULL) | |
7129 | this->report_error(_("argument 1 must be a map")); | |
7130 | else | |
7131 | { | |
7132 | // Since this function returns no value it must appear in | |
7133 | // a statement by itself, so we don't have to worry about | |
7134 | // order of evaluation of values around it. Evaluate the | |
7135 | // map first to get order of evaluation right. | |
7136 | Map_type* mt = args->front()->type()->map_type(); | |
7137 | Temporary_statement* map_temp = | |
7138 | Statement::make_temporary(mt, args->front(), loc); | |
7139 | inserter->insert(map_temp); | |
7140 | ||
7141 | Temporary_statement* key_temp = | |
7142 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7143 | inserter->insert(key_temp); | |
7144 | ||
7145 | Expression* e1 = Expression::make_temporary_reference(map_temp, | |
7146 | loc); | |
7147 | Expression* e2 = Expression::make_temporary_reference(key_temp, | |
7148 | loc); | |
7149 | e2 = Expression::make_unary(OPERATOR_AND, e2, loc); | |
7150 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), | |
7151 | 2, e1, e2); | |
7152 | } | |
7153 | } | |
7154 | break; | |
e440a328 | 7155 | } |
7156 | ||
7157 | return this; | |
7158 | } | |
7159 | ||
35a54f17 | 7160 | // Flatten a builtin call expression. This turns the arguments of copy and |
7161 | // append into temporary expressions. | |
7162 | ||
7163 | Expression* | |
7164 | Builtin_call_expression::do_flatten(Gogo*, Named_object*, | |
7165 | Statement_inserter* inserter) | |
7166 | { | |
7167 | if (this->code_ == BUILTIN_APPEND | |
7168 | || this->code_ == BUILTIN_COPY) | |
7169 | { | |
7170 | Location loc = this->location(); | |
7171 | Type* at = this->args()->front()->type(); | |
7172 | for (Expression_list::iterator pa = this->args()->begin(); | |
7173 | pa != this->args()->end(); | |
7174 | ++pa) | |
7175 | { | |
7176 | if ((*pa)->is_nil_expression()) | |
7177 | *pa = Expression::make_slice_composite_literal(at, NULL, loc); | |
7178 | if (!(*pa)->is_variable()) | |
7179 | { | |
7180 | Temporary_statement* temp = | |
7181 | Statement::make_temporary(NULL, *pa, loc); | |
7182 | inserter->insert(temp); | |
7183 | *pa = Expression::make_temporary_reference(temp, loc); | |
7184 | } | |
7185 | } | |
7186 | } | |
7187 | return this; | |
7188 | } | |
7189 | ||
a9182619 | 7190 | // Lower a make expression. |
7191 | ||
7192 | Expression* | |
7193 | Builtin_call_expression::lower_make() | |
7194 | { | |
b13c66cd | 7195 | Location loc = this->location(); |
a9182619 | 7196 | |
7197 | const Expression_list* args = this->args(); | |
7198 | if (args == NULL || args->size() < 1) | |
7199 | { | |
7200 | this->report_error(_("not enough arguments")); | |
7201 | return Expression::make_error(this->location()); | |
7202 | } | |
7203 | ||
7204 | Expression_list::const_iterator parg = args->begin(); | |
7205 | ||
7206 | Expression* first_arg = *parg; | |
7207 | if (!first_arg->is_type_expression()) | |
7208 | { | |
7209 | error_at(first_arg->location(), "expected type"); | |
7210 | this->set_is_error(); | |
7211 | return Expression::make_error(this->location()); | |
7212 | } | |
7213 | Type* type = first_arg->type(); | |
7214 | ||
7215 | bool is_slice = false; | |
7216 | bool is_map = false; | |
7217 | bool is_chan = false; | |
411eb89e | 7218 | if (type->is_slice_type()) |
a9182619 | 7219 | is_slice = true; |
7220 | else if (type->map_type() != NULL) | |
7221 | is_map = true; | |
7222 | else if (type->channel_type() != NULL) | |
7223 | is_chan = true; | |
7224 | else | |
7225 | { | |
7226 | this->report_error(_("invalid type for make function")); | |
7227 | return Expression::make_error(this->location()); | |
7228 | } | |
7229 | ||
ac84c822 | 7230 | bool have_big_args = false; |
7231 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
7232 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
7233 | ||
f6bc81e6 | 7234 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7235 | ||
a9182619 | 7236 | ++parg; |
7237 | Expression* len_arg; | |
7238 | if (parg == args->end()) | |
7239 | { | |
7240 | if (is_slice) | |
7241 | { | |
7242 | this->report_error(_("length required when allocating a slice")); | |
7243 | return Expression::make_error(this->location()); | |
7244 | } | |
7245 | ||
7246 | mpz_t zval; | |
7247 | mpz_init_set_ui(zval, 0); | |
7248 | len_arg = Expression::make_integer(&zval, NULL, loc); | |
7249 | mpz_clear(zval); | |
7250 | } | |
7251 | else | |
7252 | { | |
7253 | len_arg = *parg; | |
f6bc81e6 | 7254 | len_arg->determine_type(&int_context); |
1ad00fd4 | 7255 | if (!this->check_int_value(len_arg, true)) |
7256 | return Expression::make_error(this->location()); | |
ac84c822 | 7257 | if (len_arg->type()->integer_type() != NULL |
7258 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
7259 | have_big_args = true; | |
a9182619 | 7260 | ++parg; |
7261 | } | |
7262 | ||
7263 | Expression* cap_arg = NULL; | |
7264 | if (is_slice && parg != args->end()) | |
7265 | { | |
7266 | cap_arg = *parg; | |
f6bc81e6 | 7267 | cap_arg->determine_type(&int_context); |
1ad00fd4 | 7268 | if (!this->check_int_value(cap_arg, false)) |
7269 | return Expression::make_error(this->location()); | |
7270 | ||
7271 | Numeric_constant nclen; | |
7272 | Numeric_constant nccap; | |
7273 | unsigned long vlen; | |
7274 | unsigned long vcap; | |
7275 | if (len_arg->numeric_constant_value(&nclen) | |
7276 | && cap_arg->numeric_constant_value(&nccap) | |
7277 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7278 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7279 | && vlen > vcap) | |
a9182619 | 7280 | { |
1ad00fd4 | 7281 | this->report_error(_("len larger than cap")); |
a9182619 | 7282 | return Expression::make_error(this->location()); |
7283 | } | |
1ad00fd4 | 7284 | |
ac84c822 | 7285 | if (cap_arg->type()->integer_type() != NULL |
7286 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
7287 | have_big_args = true; | |
a9182619 | 7288 | ++parg; |
7289 | } | |
7290 | ||
7291 | if (parg != args->end()) | |
7292 | { | |
7293 | this->report_error(_("too many arguments to make")); | |
7294 | return Expression::make_error(this->location()); | |
7295 | } | |
7296 | ||
b13c66cd | 7297 | Location type_loc = first_arg->location(); |
a9182619 | 7298 | Expression* type_arg; |
7299 | if (is_slice || is_chan) | |
7300 | type_arg = Expression::make_type_descriptor(type, type_loc); | |
7301 | else if (is_map) | |
7302 | type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); | |
7303 | else | |
7304 | go_unreachable(); | |
7305 | ||
7306 | Expression* call; | |
7307 | if (is_slice) | |
7308 | { | |
7309 | if (cap_arg == NULL) | |
ac84c822 | 7310 | call = Runtime::make_call((have_big_args |
7311 | ? Runtime::MAKESLICE1BIG | |
7312 | : Runtime::MAKESLICE1), | |
7313 | loc, 2, type_arg, len_arg); | |
a9182619 | 7314 | else |
ac84c822 | 7315 | call = Runtime::make_call((have_big_args |
7316 | ? Runtime::MAKESLICE2BIG | |
7317 | : Runtime::MAKESLICE2), | |
7318 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7319 | } |
7320 | else if (is_map) | |
ac84c822 | 7321 | call = Runtime::make_call((have_big_args |
7322 | ? Runtime::MAKEMAPBIG | |
7323 | : Runtime::MAKEMAP), | |
7324 | loc, 2, type_arg, len_arg); | |
a9182619 | 7325 | else if (is_chan) |
ac84c822 | 7326 | call = Runtime::make_call((have_big_args |
7327 | ? Runtime::MAKECHANBIG | |
7328 | : Runtime::MAKECHAN), | |
7329 | loc, 2, type_arg, len_arg); | |
a9182619 | 7330 | else |
7331 | go_unreachable(); | |
7332 | ||
7333 | return Expression::make_unsafe_cast(type, call, loc); | |
7334 | } | |
7335 | ||
7336 | // Return whether an expression has an integer value. Report an error | |
7337 | // if not. This is used when handling calls to the predeclared make | |
7338 | // function. | |
7339 | ||
7340 | bool | |
1ad00fd4 | 7341 | Builtin_call_expression::check_int_value(Expression* e, bool is_length) |
a9182619 | 7342 | { |
0c77715b | 7343 | Numeric_constant nc; |
1ad00fd4 | 7344 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7345 | { |
1ad00fd4 | 7346 | unsigned long v; |
7347 | switch (nc.to_unsigned_long(&v)) | |
7348 | { | |
7349 | case Numeric_constant::NC_UL_VALID: | |
1b10c5e7 | 7350 | break; |
1ad00fd4 | 7351 | case Numeric_constant::NC_UL_NOTINT: |
7352 | error_at(e->location(), "non-integer %s argument to make", | |
7353 | is_length ? "len" : "cap"); | |
7354 | return false; | |
7355 | case Numeric_constant::NC_UL_NEGATIVE: | |
7356 | error_at(e->location(), "negative %s argument to make", | |
7357 | is_length ? "len" : "cap"); | |
7358 | return false; | |
7359 | case Numeric_constant::NC_UL_BIG: | |
7360 | // We don't want to give a compile-time error for a 64-bit | |
7361 | // value on a 32-bit target. | |
1b10c5e7 | 7362 | break; |
1ad00fd4 | 7363 | } |
1b10c5e7 | 7364 | |
7365 | mpz_t val; | |
7366 | if (!nc.to_int(&val)) | |
7367 | go_unreachable(); | |
7368 | int bits = mpz_sizeinbase(val, 2); | |
7369 | mpz_clear(val); | |
7370 | Type* int_type = Type::lookup_integer_type("int"); | |
7371 | if (bits >= int_type->integer_type()->bits()) | |
7372 | { | |
7373 | error_at(e->location(), "%s argument too large for make", | |
7374 | is_length ? "len" : "cap"); | |
7375 | return false; | |
7376 | } | |
7377 | ||
7378 | return true; | |
a9182619 | 7379 | } |
7380 | ||
1ad00fd4 | 7381 | if (e->type()->integer_type() != NULL) |
7382 | return true; | |
7383 | ||
7384 | error_at(e->location(), "non-integer %s argument to make", | |
7385 | is_length ? "len" : "cap"); | |
a9182619 | 7386 | return false; |
7387 | } | |
7388 | ||
e440a328 | 7389 | // Return the type of the real or imag functions, given the type of |
7390 | // the argument. We need to map complex to float, complex64 to | |
7391 | // float32, and complex128 to float64, so it has to be done by name. | |
7392 | // This returns NULL if it can't figure out the type. | |
7393 | ||
7394 | Type* | |
7395 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7396 | { | |
7397 | if (arg_type == NULL || arg_type->is_abstract()) | |
7398 | return NULL; | |
7399 | Named_type* nt = arg_type->named_type(); | |
7400 | if (nt == NULL) | |
7401 | return NULL; | |
7402 | while (nt->real_type()->named_type() != NULL) | |
7403 | nt = nt->real_type()->named_type(); | |
48080209 | 7404 | if (nt->name() == "complex64") |
e440a328 | 7405 | return Type::lookup_float_type("float32"); |
7406 | else if (nt->name() == "complex128") | |
7407 | return Type::lookup_float_type("float64"); | |
7408 | else | |
7409 | return NULL; | |
7410 | } | |
7411 | ||
48080209 | 7412 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7413 | // argments. Like real_imag_type, we have to map by name. |
7414 | ||
7415 | Type* | |
48080209 | 7416 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7417 | { |
7418 | if (arg_type == NULL || arg_type->is_abstract()) | |
7419 | return NULL; | |
7420 | Named_type* nt = arg_type->named_type(); | |
7421 | if (nt == NULL) | |
7422 | return NULL; | |
7423 | while (nt->real_type()->named_type() != NULL) | |
7424 | nt = nt->real_type()->named_type(); | |
48080209 | 7425 | if (nt->name() == "float32") |
e440a328 | 7426 | return Type::lookup_complex_type("complex64"); |
7427 | else if (nt->name() == "float64") | |
7428 | return Type::lookup_complex_type("complex128"); | |
7429 | else | |
7430 | return NULL; | |
7431 | } | |
7432 | ||
7433 | // Return a single argument, or NULL if there isn't one. | |
7434 | ||
7435 | Expression* | |
7436 | Builtin_call_expression::one_arg() const | |
7437 | { | |
7438 | const Expression_list* args = this->args(); | |
aa615cb3 | 7439 | if (args == NULL || args->size() != 1) |
e440a328 | 7440 | return NULL; |
7441 | return args->front(); | |
7442 | } | |
7443 | ||
83921647 | 7444 | // A traversal class which looks for a call or receive expression. |
7445 | ||
7446 | class Find_call_expression : public Traverse | |
7447 | { | |
7448 | public: | |
7449 | Find_call_expression() | |
7450 | : Traverse(traverse_expressions), | |
7451 | found_(false) | |
7452 | { } | |
7453 | ||
7454 | int | |
7455 | expression(Expression**); | |
7456 | ||
7457 | bool | |
7458 | found() | |
7459 | { return this->found_; } | |
7460 | ||
7461 | private: | |
7462 | bool found_; | |
7463 | }; | |
7464 | ||
7465 | int | |
7466 | Find_call_expression::expression(Expression** pexpr) | |
7467 | { | |
7468 | if ((*pexpr)->call_expression() != NULL | |
7469 | || (*pexpr)->receive_expression() != NULL) | |
7470 | { | |
7471 | this->found_ = true; | |
7472 | return TRAVERSE_EXIT; | |
7473 | } | |
7474 | return TRAVERSE_CONTINUE; | |
7475 | } | |
7476 | ||
7477 | // Return whether this is constant: len of a string constant, or len | |
7478 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7479 | // unsafe.Alignof. | |
e440a328 | 7480 | |
7481 | bool | |
7482 | Builtin_call_expression::do_is_constant() const | |
7483 | { | |
12e69faa | 7484 | if (this->is_error_expression()) |
7485 | return true; | |
e440a328 | 7486 | switch (this->code_) |
7487 | { | |
7488 | case BUILTIN_LEN: | |
7489 | case BUILTIN_CAP: | |
7490 | { | |
0f914071 | 7491 | if (this->seen_) |
7492 | return false; | |
7493 | ||
e440a328 | 7494 | Expression* arg = this->one_arg(); |
7495 | if (arg == NULL) | |
7496 | return false; | |
7497 | Type* arg_type = arg->type(); | |
7498 | ||
7499 | if (arg_type->points_to() != NULL | |
7500 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7501 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7502 | arg_type = arg_type->points_to(); |
7503 | ||
83921647 | 7504 | // The len and cap functions are only constant if there are no |
7505 | // function calls or channel operations in the arguments. | |
7506 | // Otherwise we have to make the call. | |
7507 | if (!arg->is_constant()) | |
7508 | { | |
7509 | Find_call_expression find_call; | |
7510 | Expression::traverse(&arg, &find_call); | |
7511 | if (find_call.found()) | |
7512 | return false; | |
7513 | } | |
7514 | ||
e440a328 | 7515 | if (arg_type->array_type() != NULL |
7516 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7517 | return true; |
e440a328 | 7518 | |
7519 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7520 | { |
7521 | this->seen_ = true; | |
7522 | bool ret = arg->is_constant(); | |
7523 | this->seen_ = false; | |
7524 | return ret; | |
7525 | } | |
e440a328 | 7526 | } |
7527 | break; | |
7528 | ||
7529 | case BUILTIN_SIZEOF: | |
7530 | case BUILTIN_ALIGNOF: | |
7531 | return this->one_arg() != NULL; | |
7532 | ||
7533 | case BUILTIN_OFFSETOF: | |
7534 | { | |
7535 | Expression* arg = this->one_arg(); | |
7536 | if (arg == NULL) | |
7537 | return false; | |
7538 | return arg->field_reference_expression() != NULL; | |
7539 | } | |
7540 | ||
48080209 | 7541 | case BUILTIN_COMPLEX: |
e440a328 | 7542 | { |
7543 | const Expression_list* args = this->args(); | |
7544 | if (args != NULL && args->size() == 2) | |
7545 | return args->front()->is_constant() && args->back()->is_constant(); | |
7546 | } | |
7547 | break; | |
7548 | ||
7549 | case BUILTIN_REAL: | |
7550 | case BUILTIN_IMAG: | |
7551 | { | |
7552 | Expression* arg = this->one_arg(); | |
7553 | return arg != NULL && arg->is_constant(); | |
7554 | } | |
7555 | ||
7556 | default: | |
7557 | break; | |
7558 | } | |
7559 | ||
7560 | return false; | |
7561 | } | |
7562 | ||
0c77715b | 7563 | // Return a numeric constant if possible. |
e440a328 | 7564 | |
7565 | bool | |
0c77715b | 7566 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7567 | { |
7568 | if (this->code_ == BUILTIN_LEN | |
7569 | || this->code_ == BUILTIN_CAP) | |
7570 | { | |
7571 | Expression* arg = this->one_arg(); | |
7572 | if (arg == NULL) | |
7573 | return false; | |
7574 | Type* arg_type = arg->type(); | |
7575 | ||
7576 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7577 | { | |
7578 | std::string sval; | |
7579 | if (arg->string_constant_value(&sval)) | |
7580 | { | |
0c77715b | 7581 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7582 | sval.length()); | |
e440a328 | 7583 | return true; |
7584 | } | |
7585 | } | |
7586 | ||
7587 | if (arg_type->points_to() != NULL | |
7588 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7589 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7590 | arg_type = arg_type->points_to(); |
7591 | ||
7592 | if (arg_type->array_type() != NULL | |
7593 | && arg_type->array_type()->length() != NULL) | |
7594 | { | |
0f914071 | 7595 | if (this->seen_) |
7596 | return false; | |
e440a328 | 7597 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7598 | this->seen_ = true; |
0c77715b | 7599 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7600 | this->seen_ = false; |
7601 | if (r) | |
e440a328 | 7602 | { |
0c77715b | 7603 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7604 | this->location())) | |
7605 | r = false; | |
e440a328 | 7606 | } |
0c77715b | 7607 | return r; |
e440a328 | 7608 | } |
7609 | } | |
7610 | else if (this->code_ == BUILTIN_SIZEOF | |
7611 | || this->code_ == BUILTIN_ALIGNOF) | |
7612 | { | |
7613 | Expression* arg = this->one_arg(); | |
7614 | if (arg == NULL) | |
7615 | return false; | |
7616 | Type* arg_type = arg->type(); | |
5c13bd80 | 7617 | if (arg_type->is_error()) |
e440a328 | 7618 | return false; |
7619 | if (arg_type->is_abstract()) | |
7620 | return false; | |
2c809f8f | 7621 | if (this->seen_) |
7622 | return false; | |
927a01eb | 7623 | |
7624 | unsigned int ret; | |
e440a328 | 7625 | if (this->code_ == BUILTIN_SIZEOF) |
7626 | { | |
2c809f8f | 7627 | this->seen_ = true; |
7628 | bool ok = arg_type->backend_type_size(this->gogo_, &ret); | |
7629 | this->seen_ = false; | |
7630 | if (!ok) | |
e440a328 | 7631 | return false; |
7632 | } | |
7633 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7634 | { | |
2c809f8f | 7635 | bool ok; |
7636 | this->seen_ = true; | |
637bd3af | 7637 | if (arg->field_reference_expression() == NULL) |
2c809f8f | 7638 | ok = arg_type->backend_type_align(this->gogo_, &ret); |
637bd3af | 7639 | else |
e440a328 | 7640 | { |
7641 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7642 | // the type of f when it is used as a field in a struct. | |
2c809f8f | 7643 | ok = arg_type->backend_type_field_align(this->gogo_, &ret); |
e440a328 | 7644 | } |
2c809f8f | 7645 | this->seen_ = false; |
7646 | if (!ok) | |
7647 | return false; | |
e440a328 | 7648 | } |
7649 | else | |
c3e6f413 | 7650 | go_unreachable(); |
927a01eb | 7651 | |
7ba86326 | 7652 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
7653 | static_cast<unsigned long>(ret)); | |
e440a328 | 7654 | return true; |
7655 | } | |
7656 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7657 | { | |
7658 | Expression* arg = this->one_arg(); | |
7659 | if (arg == NULL) | |
7660 | return false; | |
7661 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7662 | if (farg == NULL) | |
7663 | return false; | |
2c809f8f | 7664 | if (this->seen_) |
7665 | return false; | |
7666 | ||
9a4bd570 | 7667 | unsigned int total_offset = 0; |
7668 | while (true) | |
7669 | { | |
7670 | Expression* struct_expr = farg->expr(); | |
7671 | Type* st = struct_expr->type(); | |
7672 | if (st->struct_type() == NULL) | |
7673 | return false; | |
7674 | if (st->named_type() != NULL) | |
7675 | st->named_type()->convert(this->gogo_); | |
7676 | unsigned int offset; | |
2c809f8f | 7677 | this->seen_ = true; |
7678 | bool ok = st->struct_type()->backend_field_offset(this->gogo_, | |
7679 | farg->field_index(), | |
7680 | &offset); | |
7681 | this->seen_ = false; | |
7682 | if (!ok) | |
7683 | return false; | |
9a4bd570 | 7684 | total_offset += offset; |
7685 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
7686 | { | |
7687 | // Go up until we reach the original base. | |
7688 | farg = struct_expr->field_reference_expression(); | |
7689 | continue; | |
7690 | } | |
7691 | break; | |
7692 | } | |
7ba86326 | 7693 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
9a4bd570 | 7694 | static_cast<unsigned long>(total_offset)); |
e440a328 | 7695 | return true; |
7696 | } | |
0c77715b | 7697 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7698 | { |
7699 | Expression* arg = this->one_arg(); | |
7700 | if (arg == NULL) | |
7701 | return false; | |
7702 | ||
0c77715b | 7703 | Numeric_constant argnc; |
7704 | if (!arg->numeric_constant_value(&argnc)) | |
7705 | return false; | |
7706 | ||
e440a328 | 7707 | mpfr_t real; |
7708 | mpfr_t imag; | |
0c77715b | 7709 | if (!argnc.to_complex(&real, &imag)) |
7710 | return false; | |
e440a328 | 7711 | |
0c77715b | 7712 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7713 | if (this->code_ == BUILTIN_REAL) | |
7714 | nc->set_float(type, real); | |
7715 | else | |
7716 | nc->set_float(type, imag); | |
7717 | return true; | |
e440a328 | 7718 | } |
0c77715b | 7719 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7720 | { |
7721 | const Expression_list* args = this->args(); | |
7722 | if (args == NULL || args->size() != 2) | |
7723 | return false; | |
7724 | ||
0c77715b | 7725 | Numeric_constant rnc; |
7726 | if (!args->front()->numeric_constant_value(&rnc)) | |
7727 | return false; | |
7728 | Numeric_constant inc; | |
7729 | if (!args->back()->numeric_constant_value(&inc)) | |
7730 | return false; | |
7731 | ||
7732 | if (rnc.type() != NULL | |
7733 | && !rnc.type()->is_abstract() | |
7734 | && inc.type() != NULL | |
7735 | && !inc.type()->is_abstract() | |
7736 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7737 | return false; | |
7738 | ||
e440a328 | 7739 | mpfr_t r; |
0c77715b | 7740 | if (!rnc.to_float(&r)) |
7741 | return false; | |
7742 | mpfr_t i; | |
7743 | if (!inc.to_float(&i)) | |
e440a328 | 7744 | { |
7745 | mpfr_clear(r); | |
7746 | return false; | |
7747 | } | |
7748 | ||
0c77715b | 7749 | Type* arg_type = rnc.type(); |
7750 | if (arg_type == NULL || arg_type->is_abstract()) | |
7751 | arg_type = inc.type(); | |
e440a328 | 7752 | |
0c77715b | 7753 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7754 | nc->set_complex(type, r, i); | |
e440a328 | 7755 | |
7756 | mpfr_clear(r); | |
7757 | mpfr_clear(i); | |
7758 | ||
0c77715b | 7759 | return true; |
e440a328 | 7760 | } |
7761 | ||
7762 | return false; | |
7763 | } | |
7764 | ||
a7549a6a | 7765 | // Give an error if we are discarding the value of an expression which |
7766 | // should not normally be discarded. We don't give an error for | |
7767 | // discarding the value of an ordinary function call, but we do for | |
7768 | // builtin functions, purely for consistency with the gc compiler. | |
7769 | ||
4f2138d7 | 7770 | bool |
a7549a6a | 7771 | Builtin_call_expression::do_discarding_value() |
7772 | { | |
7773 | switch (this->code_) | |
7774 | { | |
7775 | case BUILTIN_INVALID: | |
7776 | default: | |
7777 | go_unreachable(); | |
7778 | ||
7779 | case BUILTIN_APPEND: | |
7780 | case BUILTIN_CAP: | |
7781 | case BUILTIN_COMPLEX: | |
7782 | case BUILTIN_IMAG: | |
7783 | case BUILTIN_LEN: | |
7784 | case BUILTIN_MAKE: | |
7785 | case BUILTIN_NEW: | |
7786 | case BUILTIN_REAL: | |
7787 | case BUILTIN_ALIGNOF: | |
7788 | case BUILTIN_OFFSETOF: | |
7789 | case BUILTIN_SIZEOF: | |
7790 | this->unused_value_error(); | |
4f2138d7 | 7791 | return false; |
a7549a6a | 7792 | |
7793 | case BUILTIN_CLOSE: | |
7794 | case BUILTIN_COPY: | |
1cce762f | 7795 | case BUILTIN_DELETE: |
a7549a6a | 7796 | case BUILTIN_PANIC: |
7797 | case BUILTIN_PRINT: | |
7798 | case BUILTIN_PRINTLN: | |
7799 | case BUILTIN_RECOVER: | |
4f2138d7 | 7800 | return true; |
a7549a6a | 7801 | } |
7802 | } | |
7803 | ||
e440a328 | 7804 | // Return the type. |
7805 | ||
7806 | Type* | |
7807 | Builtin_call_expression::do_type() | |
7808 | { | |
7809 | switch (this->code_) | |
7810 | { | |
7811 | case BUILTIN_INVALID: | |
7812 | default: | |
c3e6f413 | 7813 | go_unreachable(); |
e440a328 | 7814 | |
7815 | case BUILTIN_NEW: | |
7816 | case BUILTIN_MAKE: | |
7817 | { | |
7818 | const Expression_list* args = this->args(); | |
7819 | if (args == NULL || args->empty()) | |
7820 | return Type::make_error_type(); | |
7821 | return Type::make_pointer_type(args->front()->type()); | |
7822 | } | |
7823 | ||
7824 | case BUILTIN_CAP: | |
7825 | case BUILTIN_COPY: | |
7826 | case BUILTIN_LEN: | |
7ba86326 | 7827 | return Type::lookup_integer_type("int"); |
7828 | ||
e440a328 | 7829 | case BUILTIN_ALIGNOF: |
7830 | case BUILTIN_OFFSETOF: | |
7831 | case BUILTIN_SIZEOF: | |
7ba86326 | 7832 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7833 | |
7834 | case BUILTIN_CLOSE: | |
1cce762f | 7835 | case BUILTIN_DELETE: |
e440a328 | 7836 | case BUILTIN_PANIC: |
7837 | case BUILTIN_PRINT: | |
7838 | case BUILTIN_PRINTLN: | |
7839 | return Type::make_void_type(); | |
7840 | ||
e440a328 | 7841 | case BUILTIN_RECOVER: |
823c7e3d | 7842 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7843 | |
7844 | case BUILTIN_APPEND: | |
7845 | { | |
7846 | const Expression_list* args = this->args(); | |
7847 | if (args == NULL || args->empty()) | |
7848 | return Type::make_error_type(); | |
3ff4863b | 7849 | Type *ret = args->front()->type(); |
7850 | if (!ret->is_slice_type()) | |
7851 | return Type::make_error_type(); | |
7852 | return ret; | |
e440a328 | 7853 | } |
7854 | ||
7855 | case BUILTIN_REAL: | |
7856 | case BUILTIN_IMAG: | |
7857 | { | |
7858 | Expression* arg = this->one_arg(); | |
7859 | if (arg == NULL) | |
7860 | return Type::make_error_type(); | |
7861 | Type* t = arg->type(); | |
7862 | if (t->is_abstract()) | |
7863 | t = t->make_non_abstract_type(); | |
7864 | t = Builtin_call_expression::real_imag_type(t); | |
7865 | if (t == NULL) | |
7866 | t = Type::make_error_type(); | |
7867 | return t; | |
7868 | } | |
7869 | ||
48080209 | 7870 | case BUILTIN_COMPLEX: |
e440a328 | 7871 | { |
7872 | const Expression_list* args = this->args(); | |
7873 | if (args == NULL || args->size() != 2) | |
7874 | return Type::make_error_type(); | |
7875 | Type* t = args->front()->type(); | |
7876 | if (t->is_abstract()) | |
7877 | { | |
7878 | t = args->back()->type(); | |
7879 | if (t->is_abstract()) | |
7880 | t = t->make_non_abstract_type(); | |
7881 | } | |
48080209 | 7882 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7883 | if (t == NULL) |
7884 | t = Type::make_error_type(); | |
7885 | return t; | |
7886 | } | |
7887 | } | |
7888 | } | |
7889 | ||
7890 | // Determine the type. | |
7891 | ||
7892 | void | |
7893 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7894 | { | |
fb94b0ca | 7895 | if (!this->determining_types()) |
7896 | return; | |
7897 | ||
e440a328 | 7898 | this->fn()->determine_type_no_context(); |
7899 | ||
7900 | const Expression_list* args = this->args(); | |
7901 | ||
7902 | bool is_print; | |
7903 | Type* arg_type = NULL; | |
7904 | switch (this->code_) | |
7905 | { | |
7906 | case BUILTIN_PRINT: | |
7907 | case BUILTIN_PRINTLN: | |
7908 | // Do not force a large integer constant to "int". | |
7909 | is_print = true; | |
7910 | break; | |
7911 | ||
7912 | case BUILTIN_REAL: | |
7913 | case BUILTIN_IMAG: | |
48080209 | 7914 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 7915 | if (arg_type == NULL) |
7916 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 7917 | is_print = false; |
7918 | break; | |
7919 | ||
48080209 | 7920 | case BUILTIN_COMPLEX: |
e440a328 | 7921 | { |
48080209 | 7922 | // For the complex function the type of one operand can |
e440a328 | 7923 | // determine the type of the other, as in a binary expression. |
7924 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 7925 | if (arg_type == NULL) |
7926 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 7927 | if (args != NULL && args->size() == 2) |
7928 | { | |
7929 | Type* t1 = args->front()->type(); | |
c849bb59 | 7930 | Type* t2 = args->back()->type(); |
e440a328 | 7931 | if (!t1->is_abstract()) |
7932 | arg_type = t1; | |
7933 | else if (!t2->is_abstract()) | |
7934 | arg_type = t2; | |
7935 | } | |
7936 | is_print = false; | |
7937 | } | |
7938 | break; | |
7939 | ||
7940 | default: | |
7941 | is_print = false; | |
7942 | break; | |
7943 | } | |
7944 | ||
7945 | if (args != NULL) | |
7946 | { | |
7947 | for (Expression_list::const_iterator pa = args->begin(); | |
7948 | pa != args->end(); | |
7949 | ++pa) | |
7950 | { | |
7951 | Type_context subcontext; | |
7952 | subcontext.type = arg_type; | |
7953 | ||
7954 | if (is_print) | |
7955 | { | |
7956 | // We want to print large constants, we so can't just | |
7957 | // use the appropriate nonabstract type. Use uint64 for | |
7958 | // an integer if we know it is nonnegative, otherwise | |
7959 | // use int64 for a integer, otherwise use float64 for a | |
7960 | // float or complex128 for a complex. | |
7961 | Type* want_type = NULL; | |
7962 | Type* atype = (*pa)->type(); | |
7963 | if (atype->is_abstract()) | |
7964 | { | |
7965 | if (atype->integer_type() != NULL) | |
7966 | { | |
0c77715b | 7967 | Numeric_constant nc; |
7968 | if (this->numeric_constant_value(&nc)) | |
7969 | { | |
7970 | mpz_t val; | |
7971 | if (nc.to_int(&val)) | |
7972 | { | |
7973 | if (mpz_sgn(val) >= 0) | |
7974 | want_type = Type::lookup_integer_type("uint64"); | |
7975 | mpz_clear(val); | |
7976 | } | |
7977 | } | |
7978 | if (want_type == NULL) | |
e440a328 | 7979 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 7980 | } |
7981 | else if (atype->float_type() != NULL) | |
7982 | want_type = Type::lookup_float_type("float64"); | |
7983 | else if (atype->complex_type() != NULL) | |
7984 | want_type = Type::lookup_complex_type("complex128"); | |
7985 | else if (atype->is_abstract_string_type()) | |
7986 | want_type = Type::lookup_string_type(); | |
7987 | else if (atype->is_abstract_boolean_type()) | |
7988 | want_type = Type::lookup_bool_type(); | |
7989 | else | |
c3e6f413 | 7990 | go_unreachable(); |
e440a328 | 7991 | subcontext.type = want_type; |
7992 | } | |
7993 | } | |
7994 | ||
7995 | (*pa)->determine_type(&subcontext); | |
7996 | } | |
7997 | } | |
7998 | } | |
7999 | ||
8000 | // If there is exactly one argument, return true. Otherwise give an | |
8001 | // error message and return false. | |
8002 | ||
8003 | bool | |
8004 | Builtin_call_expression::check_one_arg() | |
8005 | { | |
8006 | const Expression_list* args = this->args(); | |
8007 | if (args == NULL || args->size() < 1) | |
8008 | { | |
8009 | this->report_error(_("not enough arguments")); | |
8010 | return false; | |
8011 | } | |
8012 | else if (args->size() > 1) | |
8013 | { | |
8014 | this->report_error(_("too many arguments")); | |
8015 | return false; | |
8016 | } | |
8017 | if (args->front()->is_error_expression() | |
5c13bd80 | 8018 | || args->front()->type()->is_error()) |
e440a328 | 8019 | { |
8020 | this->set_is_error(); | |
8021 | return false; | |
8022 | } | |
8023 | return true; | |
8024 | } | |
8025 | ||
8026 | // Check argument types for a builtin function. | |
8027 | ||
8028 | void | |
8029 | Builtin_call_expression::do_check_types(Gogo*) | |
8030 | { | |
375646ea | 8031 | if (this->is_error_expression()) |
8032 | return; | |
e440a328 | 8033 | switch (this->code_) |
8034 | { | |
8035 | case BUILTIN_INVALID: | |
8036 | case BUILTIN_NEW: | |
8037 | case BUILTIN_MAKE: | |
cd238b8d | 8038 | case BUILTIN_DELETE: |
e440a328 | 8039 | return; |
8040 | ||
8041 | case BUILTIN_LEN: | |
8042 | case BUILTIN_CAP: | |
8043 | { | |
8044 | // The single argument may be either a string or an array or a | |
8045 | // map or a channel, or a pointer to a closed array. | |
8046 | if (this->check_one_arg()) | |
8047 | { | |
8048 | Type* arg_type = this->one_arg()->type(); | |
8049 | if (arg_type->points_to() != NULL | |
8050 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8051 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8052 | arg_type = arg_type->points_to(); |
8053 | if (this->code_ == BUILTIN_CAP) | |
8054 | { | |
5c13bd80 | 8055 | if (!arg_type->is_error() |
e440a328 | 8056 | && arg_type->array_type() == NULL |
8057 | && arg_type->channel_type() == NULL) | |
8058 | this->report_error(_("argument must be array or slice " | |
8059 | "or channel")); | |
8060 | } | |
8061 | else | |
8062 | { | |
5c13bd80 | 8063 | if (!arg_type->is_error() |
e440a328 | 8064 | && !arg_type->is_string_type() |
8065 | && arg_type->array_type() == NULL | |
8066 | && arg_type->map_type() == NULL | |
8067 | && arg_type->channel_type() == NULL) | |
8068 | this->report_error(_("argument must be string or " | |
8069 | "array or slice or map or channel")); | |
8070 | } | |
8071 | } | |
8072 | } | |
8073 | break; | |
8074 | ||
8075 | case BUILTIN_PRINT: | |
8076 | case BUILTIN_PRINTLN: | |
8077 | { | |
8078 | const Expression_list* args = this->args(); | |
8079 | if (args == NULL) | |
8080 | { | |
8081 | if (this->code_ == BUILTIN_PRINT) | |
8082 | warning_at(this->location(), 0, | |
8083 | "no arguments for builtin function %<%s%>", | |
8084 | (this->code_ == BUILTIN_PRINT | |
8085 | ? "print" | |
8086 | : "println")); | |
8087 | } | |
8088 | else | |
8089 | { | |
8090 | for (Expression_list::const_iterator p = args->begin(); | |
8091 | p != args->end(); | |
8092 | ++p) | |
8093 | { | |
8094 | Type* type = (*p)->type(); | |
5c13bd80 | 8095 | if (type->is_error() |
e440a328 | 8096 | || type->is_string_type() |
8097 | || type->integer_type() != NULL | |
8098 | || type->float_type() != NULL | |
8099 | || type->complex_type() != NULL | |
8100 | || type->is_boolean_type() | |
8101 | || type->points_to() != NULL | |
8102 | || type->interface_type() != NULL | |
8103 | || type->channel_type() != NULL | |
8104 | || type->map_type() != NULL | |
8105 | || type->function_type() != NULL | |
411eb89e | 8106 | || type->is_slice_type()) |
e440a328 | 8107 | ; |
acf8e158 | 8108 | else if ((*p)->is_type_expression()) |
8109 | { | |
8110 | // If this is a type expression it's going to give | |
8111 | // an error anyhow, so we don't need one here. | |
8112 | } | |
e440a328 | 8113 | else |
8114 | this->report_error(_("unsupported argument type to " | |
8115 | "builtin function")); | |
8116 | } | |
8117 | } | |
8118 | } | |
8119 | break; | |
8120 | ||
8121 | case BUILTIN_CLOSE: | |
e440a328 | 8122 | if (this->check_one_arg()) |
8123 | { | |
8124 | if (this->one_arg()->type()->channel_type() == NULL) | |
8125 | this->report_error(_("argument must be channel")); | |
5202d986 | 8126 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8127 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8128 | } |
8129 | break; | |
8130 | ||
8131 | case BUILTIN_PANIC: | |
8132 | case BUILTIN_SIZEOF: | |
8133 | case BUILTIN_ALIGNOF: | |
8134 | this->check_one_arg(); | |
8135 | break; | |
8136 | ||
8137 | case BUILTIN_RECOVER: | |
8138 | if (this->args() != NULL && !this->args()->empty()) | |
8139 | this->report_error(_("too many arguments")); | |
8140 | break; | |
8141 | ||
8142 | case BUILTIN_OFFSETOF: | |
8143 | if (this->check_one_arg()) | |
8144 | { | |
8145 | Expression* arg = this->one_arg(); | |
8146 | if (arg->field_reference_expression() == NULL) | |
8147 | this->report_error(_("argument must be a field reference")); | |
8148 | } | |
8149 | break; | |
8150 | ||
8151 | case BUILTIN_COPY: | |
8152 | { | |
8153 | const Expression_list* args = this->args(); | |
8154 | if (args == NULL || args->size() < 2) | |
8155 | { | |
8156 | this->report_error(_("not enough arguments")); | |
8157 | break; | |
8158 | } | |
8159 | else if (args->size() > 2) | |
8160 | { | |
8161 | this->report_error(_("too many arguments")); | |
8162 | break; | |
8163 | } | |
8164 | Type* arg1_type = args->front()->type(); | |
8165 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8166 | if (arg1_type->is_error() || arg2_type->is_error()) |
e440a328 | 8167 | break; |
8168 | ||
8169 | Type* e1; | |
411eb89e | 8170 | if (arg1_type->is_slice_type()) |
e440a328 | 8171 | e1 = arg1_type->array_type()->element_type(); |
8172 | else | |
8173 | { | |
8174 | this->report_error(_("left argument must be a slice")); | |
8175 | break; | |
8176 | } | |
8177 | ||
411eb89e | 8178 | if (arg2_type->is_slice_type()) |
60963afd | 8179 | { |
8180 | Type* e2 = arg2_type->array_type()->element_type(); | |
8181 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8182 | this->report_error(_("element types must be the same")); | |
8183 | } | |
e440a328 | 8184 | else if (arg2_type->is_string_type()) |
e440a328 | 8185 | { |
60963afd | 8186 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8187 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8188 | } |
60963afd | 8189 | else |
8190 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8191 | } |
8192 | break; | |
8193 | ||
8194 | case BUILTIN_APPEND: | |
8195 | { | |
8196 | const Expression_list* args = this->args(); | |
b0d311a1 | 8197 | if (args == NULL || args->size() < 2) |
e440a328 | 8198 | { |
8199 | this->report_error(_("not enough arguments")); | |
8200 | break; | |
8201 | } | |
0b7755ec | 8202 | if (args->size() > 2) |
8203 | { | |
8204 | this->report_error(_("too many arguments")); | |
8205 | break; | |
8206 | } | |
cd238b8d | 8207 | if (args->front()->type()->is_error() |
8208 | || args->back()->type()->is_error()) | |
8209 | break; | |
8210 | ||
8211 | Array_type* at = args->front()->type()->array_type(); | |
8212 | Type* e = at->element_type(); | |
4fd4fcf4 | 8213 | |
8214 | // The language permits appending a string to a []byte, as a | |
8215 | // special case. | |
8216 | if (args->back()->type()->is_string_type()) | |
8217 | { | |
60963afd | 8218 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 8219 | break; |
8220 | } | |
8221 | ||
19fd40c3 | 8222 | // The language says that the second argument must be |
8223 | // assignable to a slice of the element type of the first | |
8224 | // argument. We already know the first argument is a slice | |
8225 | // type. | |
cd238b8d | 8226 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 8227 | std::string reason; |
19fd40c3 | 8228 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 8229 | { |
8230 | if (reason.empty()) | |
19fd40c3 | 8231 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 8232 | else |
8233 | { | |
19fd40c3 | 8234 | error_at(this->location(), "argument 2 has invalid type (%s)", |
e440a328 | 8235 | reason.c_str()); |
8236 | this->set_is_error(); | |
8237 | } | |
8238 | } | |
8239 | break; | |
8240 | } | |
8241 | ||
8242 | case BUILTIN_REAL: | |
8243 | case BUILTIN_IMAG: | |
8244 | if (this->check_one_arg()) | |
8245 | { | |
8246 | if (this->one_arg()->type()->complex_type() == NULL) | |
8247 | this->report_error(_("argument must have complex type")); | |
8248 | } | |
8249 | break; | |
8250 | ||
48080209 | 8251 | case BUILTIN_COMPLEX: |
e440a328 | 8252 | { |
8253 | const Expression_list* args = this->args(); | |
8254 | if (args == NULL || args->size() < 2) | |
8255 | this->report_error(_("not enough arguments")); | |
8256 | else if (args->size() > 2) | |
8257 | this->report_error(_("too many arguments")); | |
8258 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8259 | || args->front()->type()->is_error() |
e440a328 | 8260 | || args->back()->is_error_expression() |
5c13bd80 | 8261 | || args->back()->type()->is_error()) |
e440a328 | 8262 | this->set_is_error(); |
8263 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8264 | args->back()->type(), true, NULL)) |
48080209 | 8265 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8266 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8267 | this->report_error(_("complex arguments must have " |
e440a328 | 8268 | "floating-point type")); |
8269 | } | |
8270 | break; | |
8271 | ||
8272 | default: | |
c3e6f413 | 8273 | go_unreachable(); |
e440a328 | 8274 | } |
8275 | } | |
8276 | ||
8277 | // Return the tree for a builtin function. | |
8278 | ||
8279 | tree | |
8280 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
8281 | { | |
8282 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8283 | Location location = this->location(); |
e440a328 | 8284 | switch (this->code_) |
8285 | { | |
8286 | case BUILTIN_INVALID: | |
8287 | case BUILTIN_NEW: | |
8288 | case BUILTIN_MAKE: | |
c3e6f413 | 8289 | go_unreachable(); |
e440a328 | 8290 | |
8291 | case BUILTIN_LEN: | |
8292 | case BUILTIN_CAP: | |
8293 | { | |
8294 | const Expression_list* args = this->args(); | |
c484d925 | 8295 | go_assert(args != NULL && args->size() == 1); |
2c809f8f | 8296 | Expression* arg = args->front(); |
e440a328 | 8297 | Type* arg_type = arg->type(); |
0f914071 | 8298 | |
8299 | if (this->seen_) | |
8300 | { | |
c484d925 | 8301 | go_assert(saw_errors()); |
0f914071 | 8302 | return error_mark_node; |
8303 | } | |
8304 | this->seen_ = true; | |
0f914071 | 8305 | this->seen_ = false; |
e440a328 | 8306 | if (arg_type->points_to() != NULL) |
8307 | { | |
8308 | arg_type = arg_type->points_to(); | |
c484d925 | 8309 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8310 | && !arg_type->is_slice_type()); |
2c809f8f | 8311 | arg = Expression::make_unary(OPERATOR_MULT, arg, location); |
e440a328 | 8312 | } |
8313 | ||
1b1f2abf | 8314 | Type* int_type = Type::lookup_integer_type("int"); |
2c809f8f | 8315 | Expression* val; |
e440a328 | 8316 | if (this->code_ == BUILTIN_LEN) |
8317 | { | |
8318 | if (arg_type->is_string_type()) | |
2c809f8f | 8319 | val = Expression::make_string_info(arg, STRING_INFO_LENGTH, |
8320 | location); | |
e440a328 | 8321 | else if (arg_type->array_type() != NULL) |
0f914071 | 8322 | { |
8323 | if (this->seen_) | |
8324 | { | |
c484d925 | 8325 | go_assert(saw_errors()); |
0f914071 | 8326 | return error_mark_node; |
8327 | } | |
8328 | this->seen_ = true; | |
2c809f8f | 8329 | val = arg_type->array_type()->get_length(gogo, arg); |
0f914071 | 8330 | this->seen_ = false; |
8331 | } | |
e440a328 | 8332 | else if (arg_type->map_type() != NULL) |
2c809f8f | 8333 | val = Runtime::make_call(Runtime::MAP_LEN, location, 1, arg); |
e440a328 | 8334 | else if (arg_type->channel_type() != NULL) |
2c809f8f | 8335 | val = Runtime::make_call(Runtime::CHAN_LEN, location, 1, arg); |
e440a328 | 8336 | else |
c3e6f413 | 8337 | go_unreachable(); |
e440a328 | 8338 | } |
8339 | else | |
8340 | { | |
8341 | if (arg_type->array_type() != NULL) | |
0f914071 | 8342 | { |
8343 | if (this->seen_) | |
8344 | { | |
c484d925 | 8345 | go_assert(saw_errors()); |
0f914071 | 8346 | return error_mark_node; |
8347 | } | |
8348 | this->seen_ = true; | |
2c809f8f | 8349 | val = arg_type->array_type()->get_capacity(gogo, arg); |
0f914071 | 8350 | this->seen_ = false; |
8351 | } | |
e440a328 | 8352 | else if (arg_type->channel_type() != NULL) |
2c809f8f | 8353 | val = Runtime::make_call(Runtime::CHAN_CAP, location, 1, arg); |
e440a328 | 8354 | else |
c3e6f413 | 8355 | go_unreachable(); |
e440a328 | 8356 | } |
8357 | ||
2c809f8f | 8358 | return Expression::make_cast(int_type, val, |
8359 | location)->get_tree(context); | |
e440a328 | 8360 | } |
8361 | ||
8362 | case BUILTIN_PRINT: | |
8363 | case BUILTIN_PRINTLN: | |
8364 | { | |
8365 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
2c809f8f | 8366 | Expression* print_stmts = NULL; |
e440a328 | 8367 | |
8368 | const Expression_list* call_args = this->args(); | |
8369 | if (call_args != NULL) | |
8370 | { | |
8371 | for (Expression_list::const_iterator p = call_args->begin(); | |
8372 | p != call_args->end(); | |
8373 | ++p) | |
8374 | { | |
8375 | if (is_ln && p != call_args->begin()) | |
8376 | { | |
2c809f8f | 8377 | Expression* print_space = |
8378 | Runtime::make_call(Runtime::PRINT_SPACE, | |
8379 | this->location(), 0); | |
e440a328 | 8380 | |
2c809f8f | 8381 | print_stmts = |
8382 | Expression::make_compound(print_stmts, print_space, | |
8383 | location); | |
8384 | } | |
e440a328 | 8385 | |
2c809f8f | 8386 | Expression* arg = *p; |
8387 | Type* type = arg->type(); | |
8388 | Runtime::Function code; | |
e440a328 | 8389 | if (type->is_string_type()) |
2c809f8f | 8390 | code = Runtime::PRINT_STRING; |
e440a328 | 8391 | else if (type->integer_type() != NULL |
8392 | && type->integer_type()->is_unsigned()) | |
8393 | { | |
e440a328 | 8394 | Type* itype = Type::lookup_integer_type("uint64"); |
2c809f8f | 8395 | arg = Expression::make_cast(itype, arg, location); |
8396 | code = Runtime::PRINT_UINT64; | |
e440a328 | 8397 | } |
8398 | else if (type->integer_type() != NULL) | |
8399 | { | |
e440a328 | 8400 | Type* itype = Type::lookup_integer_type("int64"); |
2c809f8f | 8401 | arg = Expression::make_cast(itype, arg, location); |
8402 | code = Runtime::PRINT_INT64; | |
e440a328 | 8403 | } |
8404 | else if (type->float_type() != NULL) | |
8405 | { | |
2c809f8f | 8406 | Type* dtype = Type::lookup_float_type("float64"); |
8407 | arg = Expression::make_cast(dtype, arg, location); | |
8408 | code = Runtime::PRINT_DOUBLE; | |
e440a328 | 8409 | } |
8410 | else if (type->complex_type() != NULL) | |
8411 | { | |
2c809f8f | 8412 | Type* ctype = Type::lookup_complex_type("complex128"); |
8413 | arg = Expression::make_cast(ctype, arg, location); | |
8414 | code = Runtime::PRINT_COMPLEX; | |
e440a328 | 8415 | } |
8416 | else if (type->is_boolean_type()) | |
2c809f8f | 8417 | code = Runtime::PRINT_BOOL; |
e440a328 | 8418 | else if (type->points_to() != NULL |
8419 | || type->channel_type() != NULL | |
8420 | || type->map_type() != NULL | |
8421 | || type->function_type() != NULL) | |
8422 | { | |
2c809f8f | 8423 | arg = Expression::make_cast(type, arg, location); |
8424 | code = Runtime::PRINT_POINTER; | |
e440a328 | 8425 | } |
8426 | else if (type->interface_type() != NULL) | |
8427 | { | |
8428 | if (type->interface_type()->is_empty()) | |
2c809f8f | 8429 | code = Runtime::PRINT_EMPTY_INTERFACE; |
e440a328 | 8430 | else |
2c809f8f | 8431 | code = Runtime::PRINT_INTERFACE; |
e440a328 | 8432 | } |
411eb89e | 8433 | else if (type->is_slice_type()) |
2c809f8f | 8434 | code = Runtime::PRINT_SLICE; |
e440a328 | 8435 | else |
cd238b8d | 8436 | { |
8437 | go_assert(saw_errors()); | |
8438 | return error_mark_node; | |
8439 | } | |
e440a328 | 8440 | |
2c809f8f | 8441 | Expression* call = Runtime::make_call(code, location, 1, arg); |
8442 | if (print_stmts == NULL) | |
8443 | print_stmts = call; | |
8444 | else | |
8445 | print_stmts = Expression::make_compound(print_stmts, call, | |
8446 | location); | |
e440a328 | 8447 | } |
8448 | } | |
8449 | ||
8450 | if (is_ln) | |
8451 | { | |
2c809f8f | 8452 | Expression* print_nl = |
8453 | Runtime::make_call(Runtime::PRINT_NL, location, 0); | |
8454 | if (print_stmts == NULL) | |
8455 | print_stmts = print_nl; | |
8456 | else | |
8457 | print_stmts = Expression::make_compound(print_stmts, print_nl, | |
8458 | location); | |
e440a328 | 8459 | } |
8460 | ||
2c809f8f | 8461 | return print_stmts->get_tree(context); |
e440a328 | 8462 | } |
8463 | ||
8464 | case BUILTIN_PANIC: | |
8465 | { | |
8466 | const Expression_list* args = this->args(); | |
c484d925 | 8467 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8468 | Expression* arg = args->front(); |
b13c66cd | 8469 | Type *empty = |
823c7e3d | 8470 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
2c809f8f | 8471 | arg = Expression::convert_for_assignment(gogo, empty, arg, location); |
8472 | ||
8473 | Expression* panic = | |
8474 | Runtime::make_call(Runtime::PANIC, location, 1, arg); | |
8475 | return panic->get_tree(context); | |
e440a328 | 8476 | } |
8477 | ||
8478 | case BUILTIN_RECOVER: | |
8479 | { | |
8480 | // The argument is set when building recover thunks. It's a | |
8481 | // boolean value which is true if we can recover a value now. | |
8482 | const Expression_list* args = this->args(); | |
c484d925 | 8483 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8484 | Expression* arg = args->front(); |
b13c66cd | 8485 | Type *empty = |
823c7e3d | 8486 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8487 | |
e440a328 | 8488 | Expression* nil = Expression::make_nil(location); |
2c809f8f | 8489 | nil = Expression::convert_for_assignment(gogo, empty, nil, location); |
e440a328 | 8490 | |
8491 | // We need to handle a deferred call to recover specially, | |
8492 | // because it changes whether it can recover a panic or not. | |
8493 | // See test7 in test/recover1.go. | |
2c809f8f | 8494 | Expression* recover = Runtime::make_call((this->is_deferred() |
8495 | ? Runtime::DEFERRED_RECOVER | |
8496 | : Runtime::RECOVER), | |
8497 | location, 0); | |
8498 | Expression* cond = | |
8499 | Expression::make_conditional(arg, recover, nil, location); | |
8500 | return cond->get_tree(context); | |
e440a328 | 8501 | } |
8502 | ||
8503 | case BUILTIN_CLOSE: | |
e440a328 | 8504 | { |
8505 | const Expression_list* args = this->args(); | |
c484d925 | 8506 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8507 | Expression* arg = args->front(); |
2c809f8f | 8508 | Expression* close = Runtime::make_call(Runtime::CLOSE, location, |
8509 | 1, arg); | |
8510 | return close->get_tree(context); | |
e440a328 | 8511 | } |
8512 | ||
8513 | case BUILTIN_SIZEOF: | |
8514 | case BUILTIN_OFFSETOF: | |
8515 | case BUILTIN_ALIGNOF: | |
8516 | { | |
0c77715b | 8517 | Numeric_constant nc; |
8518 | unsigned long val; | |
8519 | if (!this->numeric_constant_value(&nc) | |
8520 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8521 | { |
c484d925 | 8522 | go_assert(saw_errors()); |
7f1d9abd | 8523 | return error_mark_node; |
8524 | } | |
7ba86326 | 8525 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
2c809f8f | 8526 | mpz_t ival; |
8527 | nc.get_int(&ival); | |
8528 | Expression* int_cst = | |
8529 | Expression::make_integer(&ival, uintptr_type, location); | |
8530 | mpz_clear(ival); | |
8531 | return int_cst->get_tree(context); | |
e440a328 | 8532 | } |
8533 | ||
8534 | case BUILTIN_COPY: | |
8535 | { | |
8536 | const Expression_list* args = this->args(); | |
c484d925 | 8537 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8538 | Expression* arg1 = args->front(); |
8539 | Expression* arg2 = args->back(); | |
8540 | ||
e440a328 | 8541 | Type* arg1_type = arg1->type(); |
8542 | Array_type* at = arg1_type->array_type(); | |
35a54f17 | 8543 | go_assert(arg1->is_variable()); |
2c809f8f | 8544 | Expression* arg1_val = at->get_value_pointer(gogo, arg1); |
8545 | Expression* arg1_len = at->get_length(gogo, arg1); | |
e440a328 | 8546 | |
8547 | Type* arg2_type = arg2->type(); | |
2c809f8f | 8548 | go_assert(arg2->is_variable()); |
8549 | Expression* arg2_val; | |
8550 | Expression* arg2_len; | |
411eb89e | 8551 | if (arg2_type->is_slice_type()) |
e440a328 | 8552 | { |
8553 | at = arg2_type->array_type(); | |
2c809f8f | 8554 | arg2_val = at->get_value_pointer(gogo, arg2); |
8555 | arg2_len = at->get_length(gogo, arg2); | |
e440a328 | 8556 | } |
8557 | else | |
8558 | { | |
2c809f8f | 8559 | go_assert(arg2->is_variable()); |
8560 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, | |
8561 | location); | |
8562 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8563 | location); | |
e440a328 | 8564 | } |
2c809f8f | 8565 | Expression* cond = |
8566 | Expression::make_binary(OPERATOR_LT, arg1_len, arg2_len, location); | |
8567 | Expression* length = | |
8568 | Expression::make_conditional(cond, arg1_len, arg2_len, location); | |
e440a328 | 8569 | |
8570 | Type* element_type = at->element_type(); | |
9f0e0513 | 8571 | Btype* element_btype = element_type->get_backend(gogo); |
e440a328 | 8572 | |
2c809f8f | 8573 | mpz_t size; |
8574 | size_t element_size = gogo->backend()->type_size(element_btype); | |
8575 | mpz_init_set_ui(size, element_size); | |
8576 | Expression* size_expr = Expression::make_integer(&size, length->type(), location); | |
8577 | mpz_clear(size); | |
8578 | ||
8579 | Expression* bytecount = | |
8580 | Expression::make_binary(OPERATOR_MULT, size_expr, length, location); | |
8581 | Expression* copy = Runtime::make_call(Runtime::COPY, location, 3, | |
8582 | arg1_val, arg2_val, bytecount); | |
8583 | ||
8584 | Expression* compound = Expression::make_compound(copy, length, location); | |
8585 | return compound->get_tree(context); | |
e440a328 | 8586 | } |
8587 | ||
8588 | case BUILTIN_APPEND: | |
8589 | { | |
8590 | const Expression_list* args = this->args(); | |
c484d925 | 8591 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8592 | Expression* arg1 = args->front(); |
8593 | Expression* arg2 = args->back(); | |
8594 | ||
9d44fbe3 | 8595 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8596 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8597 | |
2c809f8f | 8598 | go_assert(arg2->is_variable()); |
8599 | Expression* arg2_val; | |
8600 | Expression* arg2_len; | |
8601 | mpz_t size; | |
4fd4fcf4 | 8602 | if (arg2->type()->is_string_type() |
60963afd | 8603 | && element_type->integer_type() != NULL |
8604 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8605 | { |
2c809f8f | 8606 | arg2_val = Expression::make_string_info(arg2, STRING_INFO_DATA, |
8607 | location); | |
8608 | arg2_len = Expression::make_string_info(arg2, STRING_INFO_LENGTH, | |
8609 | location); | |
8610 | mpz_init_set_ui(size, 1UL); | |
4fd4fcf4 | 8611 | } |
8612 | else | |
8613 | { | |
2c809f8f | 8614 | arg2_val = at->get_value_pointer(gogo, arg2); |
8615 | arg2_len = at->get_length(gogo, arg2); | |
35a54f17 | 8616 | Btype* element_btype = element_type->get_backend(gogo); |
2c809f8f | 8617 | size_t element_size = gogo->backend()->type_size(element_btype); |
8618 | mpz_init_set_ui(size, element_size); | |
4fd4fcf4 | 8619 | } |
2c809f8f | 8620 | Expression* element_size = |
8621 | Expression::make_integer(&size, NULL, location); | |
8622 | mpz_clear(size); | |
8623 | ||
8624 | Expression* append = Runtime::make_call(Runtime::APPEND, location, 4, | |
8625 | arg1, arg2_val, arg2_len, | |
8626 | element_size); | |
8627 | append = Expression::make_unsafe_cast(arg1->type(), append, location); | |
8628 | return append->get_tree(context); | |
e440a328 | 8629 | } |
8630 | ||
8631 | case BUILTIN_REAL: | |
8632 | case BUILTIN_IMAG: | |
8633 | { | |
8634 | const Expression_list* args = this->args(); | |
c484d925 | 8635 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8636 | Expression* arg = args->front(); |
2c809f8f | 8637 | |
8638 | Bexpression* ret; | |
8639 | Bexpression* bcomplex = tree_to_expr(arg->get_tree(context)); | |
8640 | if (this->code_ == BUILTIN_REAL) | |
8641 | ret = gogo->backend()->real_part_expression(bcomplex, location); | |
8642 | else | |
8643 | ret = gogo->backend()->imag_part_expression(bcomplex, location); | |
8644 | return expr_to_tree(ret); | |
e440a328 | 8645 | } |
8646 | ||
48080209 | 8647 | case BUILTIN_COMPLEX: |
e440a328 | 8648 | { |
8649 | const Expression_list* args = this->args(); | |
c484d925 | 8650 | go_assert(args != NULL && args->size() == 2); |
2c809f8f | 8651 | Bexpression* breal = tree_to_expr(args->front()->get_tree(context)); |
8652 | Bexpression* bimag = tree_to_expr(args->back()->get_tree(context)); | |
8653 | Bexpression* ret = | |
8654 | gogo->backend()->complex_expression(breal, bimag, location); | |
8655 | return expr_to_tree(ret); | |
e440a328 | 8656 | } |
8657 | ||
8658 | default: | |
c3e6f413 | 8659 | go_unreachable(); |
e440a328 | 8660 | } |
8661 | } | |
8662 | ||
8663 | // We have to support exporting a builtin call expression, because | |
8664 | // code can set a constant to the result of a builtin expression. | |
8665 | ||
8666 | void | |
8667 | Builtin_call_expression::do_export(Export* exp) const | |
8668 | { | |
0c77715b | 8669 | Numeric_constant nc; |
8670 | if (!this->numeric_constant_value(&nc)) | |
8671 | { | |
8672 | error_at(this->location(), "value is not constant"); | |
8673 | return; | |
8674 | } | |
e440a328 | 8675 | |
0c77715b | 8676 | if (nc.is_int()) |
e440a328 | 8677 | { |
0c77715b | 8678 | mpz_t val; |
8679 | nc.get_int(&val); | |
e440a328 | 8680 | Integer_expression::export_integer(exp, val); |
0c77715b | 8681 | mpz_clear(val); |
e440a328 | 8682 | } |
0c77715b | 8683 | else if (nc.is_float()) |
e440a328 | 8684 | { |
8685 | mpfr_t fval; | |
0c77715b | 8686 | nc.get_float(&fval); |
8687 | Float_expression::export_float(exp, fval); | |
e440a328 | 8688 | mpfr_clear(fval); |
8689 | } | |
0c77715b | 8690 | else if (nc.is_complex()) |
e440a328 | 8691 | { |
8692 | mpfr_t real; | |
8693 | mpfr_t imag; | |
0c77715b | 8694 | Complex_expression::export_complex(exp, real, imag); |
e440a328 | 8695 | mpfr_clear(real); |
8696 | mpfr_clear(imag); | |
8697 | } | |
0c77715b | 8698 | else |
8699 | go_unreachable(); | |
e440a328 | 8700 | |
8701 | // A trailing space lets us reliably identify the end of the number. | |
8702 | exp->write_c_string(" "); | |
8703 | } | |
8704 | ||
8705 | // Class Call_expression. | |
8706 | ||
8381eda7 | 8707 | // A Go function can be viewed in a couple of different ways. The |
8708 | // code of a Go function becomes a backend function with parameters | |
8709 | // whose types are simply the backend representation of the Go types. | |
8710 | // If there are multiple results, they are returned as a backend | |
8711 | // struct. | |
8712 | ||
8713 | // However, when Go code refers to a function other than simply | |
8714 | // calling it, the backend type of that function is actually a struct. | |
8715 | // The first field of the struct points to the Go function code | |
8716 | // (sometimes a wrapper as described below). The remaining fields | |
8717 | // hold addresses of closed-over variables. This struct is called a | |
8718 | // closure. | |
8719 | ||
8720 | // There are a few cases to consider. | |
8721 | ||
8722 | // A direct function call of a known function in package scope. In | |
8723 | // this case there are no closed-over variables, and we know the name | |
8724 | // of the function code. We can simply produce a backend call to the | |
8725 | // function directly, and not worry about the closure. | |
8726 | ||
8727 | // A direct function call of a known function literal. In this case | |
8728 | // we know the function code and we know the closure. We generate the | |
8729 | // function code such that it expects an additional final argument of | |
8730 | // the closure type. We pass the closure as the last argument, after | |
8731 | // the other arguments. | |
8732 | ||
8733 | // An indirect function call. In this case we have a closure. We | |
8734 | // load the pointer to the function code from the first field of the | |
8735 | // closure. We pass the address of the closure as the last argument. | |
8736 | ||
8737 | // A call to a method of an interface. Type methods are always at | |
8738 | // package scope, so we call the function directly, and don't worry | |
8739 | // about the closure. | |
8740 | ||
8741 | // This means that for a function at package scope we have two cases. | |
8742 | // One is the direct call, which has no closure. The other is the | |
8743 | // indirect call, which does have a closure. We can't simply ignore | |
8744 | // the closure, even though it is the last argument, because that will | |
8745 | // fail on targets where the function pops its arguments. So when | |
8746 | // generating a closure for a package-scope function we set the | |
8747 | // function code pointer in the closure to point to a wrapper | |
8748 | // function. This wrapper function accepts a final argument that | |
8749 | // points to the closure, ignores it, and calls the real function as a | |
8750 | // direct function call. This wrapper will normally be efficient, and | |
8751 | // can often simply be a tail call to the real function. | |
8752 | ||
8753 | // We don't use GCC's static chain pointer because 1) we don't need | |
8754 | // it; 2) GCC only permits using a static chain to call a known | |
8755 | // function, so we can't use it for an indirect call anyhow. Since we | |
8756 | // can't use it for an indirect call, we may as well not worry about | |
8757 | // using it for a direct call either. | |
8758 | ||
8759 | // We pass the closure last rather than first because it means that | |
8760 | // the function wrapper we put into a closure for a package-scope | |
8761 | // function can normally just be a tail call to the real function. | |
8762 | ||
8763 | // For method expressions we generate a wrapper that loads the | |
8764 | // receiver from the closure and then calls the method. This | |
8765 | // unfortunately forces reshuffling the arguments, since there is a | |
8766 | // new first argument, but we can't avoid reshuffling either for | |
8767 | // method expressions or for indirect calls of package-scope | |
8768 | // functions, and since the latter are more common we reshuffle for | |
8769 | // method expressions. | |
8770 | ||
8771 | // Note that the Go code retains the Go types. The extra final | |
8772 | // argument only appears when we convert to the backend | |
8773 | // representation. | |
8774 | ||
e440a328 | 8775 | // Traversal. |
8776 | ||
8777 | int | |
8778 | Call_expression::do_traverse(Traverse* traverse) | |
8779 | { | |
8780 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8781 | return TRAVERSE_EXIT; | |
8782 | if (this->args_ != NULL) | |
8783 | { | |
8784 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8785 | return TRAVERSE_EXIT; | |
8786 | } | |
8787 | return TRAVERSE_CONTINUE; | |
8788 | } | |
8789 | ||
8790 | // Lower a call statement. | |
8791 | ||
8792 | Expression* | |
ceeb4318 | 8793 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
8794 | Statement_inserter* inserter, int) | |
e440a328 | 8795 | { |
b13c66cd | 8796 | Location loc = this->location(); |
09ea332d | 8797 | |
ceeb4318 | 8798 | // A type cast can look like a function call. |
e440a328 | 8799 | if (this->fn_->is_type_expression() |
8800 | && this->args_ != NULL | |
8801 | && this->args_->size() == 1) | |
8802 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 8803 | loc); |
e440a328 | 8804 | |
88f06749 | 8805 | // Because do_type will return an error type and thus prevent future |
8806 | // errors, check for that case now to ensure that the error gets | |
8807 | // reported. | |
37448b10 | 8808 | Function_type* fntype = this->get_function_type(); |
8809 | if (fntype == NULL) | |
88f06749 | 8810 | { |
8811 | if (!this->fn_->type()->is_error()) | |
8812 | this->report_error(_("expected function")); | |
8813 | return Expression::make_error(loc); | |
8814 | } | |
8815 | ||
e440a328 | 8816 | // Handle an argument which is a call to a function which returns |
8817 | // multiple results. | |
8818 | if (this->args_ != NULL | |
8819 | && this->args_->size() == 1 | |
37448b10 | 8820 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 8821 | { |
e440a328 | 8822 | size_t rc = this->args_->front()->call_expression()->result_count(); |
8823 | if (rc > 1 | |
37448b10 | 8824 | && ((fntype->parameters() != NULL |
8825 | && (fntype->parameters()->size() == rc | |
8826 | || (fntype->is_varargs() | |
8827 | && fntype->parameters()->size() - 1 <= rc))) | |
8828 | || fntype->is_builtin())) | |
e440a328 | 8829 | { |
8830 | Call_expression* call = this->args_->front()->call_expression(); | |
8831 | Expression_list* args = new Expression_list; | |
8832 | for (size_t i = 0; i < rc; ++i) | |
8833 | args->push_back(Expression::make_call_result(call, i)); | |
8834 | // We can't return a new call expression here, because this | |
42535814 | 8835 | // one may be referenced by Call_result expressions. We |
8836 | // also can't delete the old arguments, because we may still | |
8837 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8838 | this->args_ = args; |
8839 | } | |
8840 | } | |
8841 | ||
37448b10 | 8842 | // Recognize a call to a builtin function. |
8843 | if (fntype->is_builtin()) | |
8844 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
8845 | this->is_varargs_, loc); | |
8846 | ||
ceeb4318 | 8847 | // If this call returns multiple results, create a temporary |
8848 | // variable for each result. | |
8849 | size_t rc = this->result_count(); | |
8850 | if (rc > 1 && this->results_ == NULL) | |
8851 | { | |
8852 | std::vector<Temporary_statement*>* temps = | |
8853 | new std::vector<Temporary_statement*>; | |
8854 | temps->reserve(rc); | |
37448b10 | 8855 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 8856 | for (Typed_identifier_list::const_iterator p = results->begin(); |
8857 | p != results->end(); | |
8858 | ++p) | |
8859 | { | |
8860 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 8861 | NULL, loc); |
ceeb4318 | 8862 | inserter->insert(temp); |
8863 | temps->push_back(temp); | |
8864 | } | |
8865 | this->results_ = temps; | |
8866 | } | |
8867 | ||
e440a328 | 8868 | // Handle a call to a varargs function by packaging up the extra |
8869 | // parameters. | |
37448b10 | 8870 | if (fntype->is_varargs()) |
e440a328 | 8871 | { |
e440a328 | 8872 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 8873 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 8874 | Type* varargs_type = parameters->back().type(); |
09ea332d | 8875 | this->lower_varargs(gogo, function, inserter, varargs_type, |
8876 | parameters->size()); | |
8877 | } | |
8878 | ||
8879 | // If this is call to a method, call the method directly passing the | |
8880 | // object as the first parameter. | |
8881 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8882 | if (bme != NULL) | |
8883 | { | |
0afbb937 | 8884 | Named_object* methodfn = bme->function(); |
09ea332d | 8885 | Expression* first_arg = bme->first_argument(); |
8886 | ||
8887 | // We always pass a pointer when calling a method. | |
8888 | if (first_arg->type()->points_to() == NULL | |
8889 | && !first_arg->type()->is_error()) | |
8890 | { | |
8891 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
8892 | // We may need to create a temporary variable so that we can | |
8893 | // take the address. We can't do that here because it will | |
8894 | // mess up the order of evaluation. | |
8895 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
8896 | ue->set_create_temp(); | |
8897 | } | |
8898 | ||
8899 | // If we are calling a method which was inherited from an | |
8900 | // embedded struct, and the method did not get a stub, then the | |
8901 | // first type may be wrong. | |
8902 | Type* fatype = bme->first_argument_type(); | |
8903 | if (fatype != NULL) | |
8904 | { | |
8905 | if (fatype->points_to() == NULL) | |
8906 | fatype = Type::make_pointer_type(fatype); | |
8907 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
8908 | } | |
8909 | ||
8910 | Expression_list* new_args = new Expression_list(); | |
8911 | new_args->push_back(first_arg); | |
8912 | if (this->args_ != NULL) | |
8913 | { | |
8914 | for (Expression_list::const_iterator p = this->args_->begin(); | |
8915 | p != this->args_->end(); | |
8916 | ++p) | |
8917 | new_args->push_back(*p); | |
8918 | } | |
8919 | ||
8920 | // We have to change in place because this structure may be | |
8921 | // referenced by Call_result_expressions. We can't delete the | |
8922 | // old arguments, because we may be traversing them up in some | |
8923 | // caller. FIXME. | |
8924 | this->args_ = new_args; | |
0afbb937 | 8925 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 8926 | bme->location()); |
e440a328 | 8927 | } |
8928 | ||
8929 | return this; | |
8930 | } | |
8931 | ||
8932 | // Lower a call to a varargs function. FUNCTION is the function in | |
8933 | // which the call occurs--it's not the function we are calling. | |
8934 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8935 | // PARAM_COUNT is the number of parameters of the function we are | |
8936 | // calling; the last of these parameters will be the varargs | |
8937 | // parameter. | |
8938 | ||
09ea332d | 8939 | void |
e440a328 | 8940 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 8941 | Statement_inserter* inserter, |
e440a328 | 8942 | Type* varargs_type, size_t param_count) |
8943 | { | |
8944 | if (this->varargs_are_lowered_) | |
09ea332d | 8945 | return; |
e440a328 | 8946 | |
b13c66cd | 8947 | Location loc = this->location(); |
e440a328 | 8948 | |
c484d925 | 8949 | go_assert(param_count > 0); |
411eb89e | 8950 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 8951 | |
8952 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8953 | if (arg_count < param_count - 1) | |
8954 | { | |
8955 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 8956 | return; |
e440a328 | 8957 | } |
8958 | ||
8959 | Expression_list* old_args = this->args_; | |
8960 | Expression_list* new_args = new Expression_list(); | |
8961 | bool push_empty_arg = false; | |
8962 | if (old_args == NULL || old_args->empty()) | |
8963 | { | |
c484d925 | 8964 | go_assert(param_count == 1); |
e440a328 | 8965 | push_empty_arg = true; |
8966 | } | |
8967 | else | |
8968 | { | |
8969 | Expression_list::const_iterator pa; | |
8970 | int i = 1; | |
8971 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8972 | { | |
8973 | if (static_cast<size_t>(i) == param_count) | |
8974 | break; | |
8975 | new_args->push_back(*pa); | |
8976 | } | |
8977 | ||
8978 | // We have reached the varargs parameter. | |
8979 | ||
8980 | bool issued_error = false; | |
8981 | if (pa == old_args->end()) | |
8982 | push_empty_arg = true; | |
8983 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8984 | new_args->push_back(*pa); | |
8985 | else if (this->is_varargs_) | |
8986 | { | |
a6645f74 | 8987 | if ((*pa)->type()->is_slice_type()) |
8988 | this->report_error(_("too many arguments")); | |
8989 | else | |
8990 | { | |
8991 | error_at(this->location(), | |
8992 | _("invalid use of %<...%> with non-slice")); | |
8993 | this->set_is_error(); | |
8994 | } | |
09ea332d | 8995 | return; |
e440a328 | 8996 | } |
e440a328 | 8997 | else |
8998 | { | |
8999 | Type* element_type = varargs_type->array_type()->element_type(); | |
9000 | Expression_list* vals = new Expression_list; | |
9001 | for (; pa != old_args->end(); ++pa, ++i) | |
9002 | { | |
9003 | // Check types here so that we get a better message. | |
9004 | Type* patype = (*pa)->type(); | |
b13c66cd | 9005 | Location paloc = (*pa)->location(); |
e440a328 | 9006 | if (!this->check_argument_type(i, element_type, patype, |
9007 | paloc, issued_error)) | |
9008 | continue; | |
9009 | vals->push_back(*pa); | |
9010 | } | |
9011 | Expression* val = | |
9012 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
09ea332d | 9013 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9014 | new_args->push_back(val); |
9015 | } | |
9016 | } | |
9017 | ||
9018 | if (push_empty_arg) | |
9019 | new_args->push_back(Expression::make_nil(loc)); | |
9020 | ||
9021 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9022 | // be referenced by Call_result expressions. FIXME. We can't |
9023 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9024 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9025 | this->args_ = new_args; |
9026 | this->varargs_are_lowered_ = true; | |
e440a328 | 9027 | } |
9028 | ||
2c809f8f | 9029 | // Flatten a call with multiple results into a temporary. |
9030 | ||
9031 | Expression* | |
9032 | Call_expression::do_flatten(Gogo*, Named_object*, Statement_inserter* inserter) | |
9033 | { | |
9034 | size_t rc = this->result_count(); | |
9035 | if (rc > 1 && this->call_temp_ == NULL) | |
9036 | { | |
9037 | Struct_field_list* sfl = new Struct_field_list(); | |
9038 | Function_type* fntype = this->get_function_type(); | |
9039 | const Typed_identifier_list* results = fntype->results(); | |
9040 | Location loc = this->location(); | |
9041 | ||
9042 | int i = 0; | |
9043 | char buf[10]; | |
9044 | for (Typed_identifier_list::const_iterator p = results->begin(); | |
9045 | p != results->end(); | |
9046 | ++p, ++i) | |
9047 | { | |
9048 | snprintf(buf, sizeof buf, "res%d", i); | |
9049 | sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc))); | |
9050 | } | |
9051 | ||
9052 | Struct_type* st = Type::make_struct_type(sfl, loc); | |
9053 | this->call_temp_ = Statement::make_temporary(st, NULL, loc); | |
9054 | inserter->insert(this->call_temp_); | |
9055 | } | |
9056 | ||
9057 | return this; | |
9058 | } | |
9059 | ||
ceeb4318 | 9060 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9061 | |
9062 | Function_type* | |
9063 | Call_expression::get_function_type() const | |
9064 | { | |
9065 | return this->fn_->type()->function_type(); | |
9066 | } | |
9067 | ||
9068 | // Return the number of values which this call will return. | |
9069 | ||
9070 | size_t | |
9071 | Call_expression::result_count() const | |
9072 | { | |
9073 | const Function_type* fntype = this->get_function_type(); | |
9074 | if (fntype == NULL) | |
9075 | return 0; | |
9076 | if (fntype->results() == NULL) | |
9077 | return 0; | |
9078 | return fntype->results()->size(); | |
9079 | } | |
9080 | ||
ceeb4318 | 9081 | // Return the temporary which holds a result. |
9082 | ||
9083 | Temporary_statement* | |
9084 | Call_expression::result(size_t i) const | |
9085 | { | |
cd238b8d | 9086 | if (this->results_ == NULL || this->results_->size() <= i) |
9087 | { | |
9088 | go_assert(saw_errors()); | |
9089 | return NULL; | |
9090 | } | |
ceeb4318 | 9091 | return (*this->results_)[i]; |
9092 | } | |
9093 | ||
e440a328 | 9094 | // Return whether this is a call to the predeclared function recover. |
9095 | ||
9096 | bool | |
9097 | Call_expression::is_recover_call() const | |
9098 | { | |
9099 | return this->do_is_recover_call(); | |
9100 | } | |
9101 | ||
9102 | // Set the argument to the recover function. | |
9103 | ||
9104 | void | |
9105 | Call_expression::set_recover_arg(Expression* arg) | |
9106 | { | |
9107 | this->do_set_recover_arg(arg); | |
9108 | } | |
9109 | ||
9110 | // Virtual functions also implemented by Builtin_call_expression. | |
9111 | ||
9112 | bool | |
9113 | Call_expression::do_is_recover_call() const | |
9114 | { | |
9115 | return false; | |
9116 | } | |
9117 | ||
9118 | void | |
9119 | Call_expression::do_set_recover_arg(Expression*) | |
9120 | { | |
c3e6f413 | 9121 | go_unreachable(); |
e440a328 | 9122 | } |
9123 | ||
ceeb4318 | 9124 | // We have found an error with this call expression; return true if |
9125 | // we should report it. | |
9126 | ||
9127 | bool | |
9128 | Call_expression::issue_error() | |
9129 | { | |
9130 | if (this->issued_error_) | |
9131 | return false; | |
9132 | else | |
9133 | { | |
9134 | this->issued_error_ = true; | |
9135 | return true; | |
9136 | } | |
9137 | } | |
9138 | ||
e440a328 | 9139 | // Get the type. |
9140 | ||
9141 | Type* | |
9142 | Call_expression::do_type() | |
9143 | { | |
9144 | if (this->type_ != NULL) | |
9145 | return this->type_; | |
9146 | ||
9147 | Type* ret; | |
9148 | Function_type* fntype = this->get_function_type(); | |
9149 | if (fntype == NULL) | |
9150 | return Type::make_error_type(); | |
9151 | ||
9152 | const Typed_identifier_list* results = fntype->results(); | |
9153 | if (results == NULL) | |
9154 | ret = Type::make_void_type(); | |
9155 | else if (results->size() == 1) | |
9156 | ret = results->begin()->type(); | |
9157 | else | |
9158 | ret = Type::make_call_multiple_result_type(this); | |
9159 | ||
9160 | this->type_ = ret; | |
9161 | ||
9162 | return this->type_; | |
9163 | } | |
9164 | ||
9165 | // Determine types for a call expression. We can use the function | |
9166 | // parameter types to set the types of the arguments. | |
9167 | ||
9168 | void | |
9169 | Call_expression::do_determine_type(const Type_context*) | |
9170 | { | |
fb94b0ca | 9171 | if (!this->determining_types()) |
9172 | return; | |
9173 | ||
e440a328 | 9174 | this->fn_->determine_type_no_context(); |
9175 | Function_type* fntype = this->get_function_type(); | |
9176 | const Typed_identifier_list* parameters = NULL; | |
9177 | if (fntype != NULL) | |
9178 | parameters = fntype->parameters(); | |
9179 | if (this->args_ != NULL) | |
9180 | { | |
9181 | Typed_identifier_list::const_iterator pt; | |
9182 | if (parameters != NULL) | |
9183 | pt = parameters->begin(); | |
09ea332d | 9184 | bool first = true; |
e440a328 | 9185 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9186 | pa != this->args_->end(); | |
9187 | ++pa) | |
9188 | { | |
09ea332d | 9189 | if (first) |
9190 | { | |
9191 | first = false; | |
9192 | // If this is a method, the first argument is the | |
9193 | // receiver. | |
9194 | if (fntype != NULL && fntype->is_method()) | |
9195 | { | |
9196 | Type* rtype = fntype->receiver()->type(); | |
9197 | // The receiver is always passed as a pointer. | |
9198 | if (rtype->points_to() == NULL) | |
9199 | rtype = Type::make_pointer_type(rtype); | |
9200 | Type_context subcontext(rtype, false); | |
9201 | (*pa)->determine_type(&subcontext); | |
9202 | continue; | |
9203 | } | |
9204 | } | |
9205 | ||
e440a328 | 9206 | if (parameters != NULL && pt != parameters->end()) |
9207 | { | |
9208 | Type_context subcontext(pt->type(), false); | |
9209 | (*pa)->determine_type(&subcontext); | |
9210 | ++pt; | |
9211 | } | |
9212 | else | |
9213 | (*pa)->determine_type_no_context(); | |
9214 | } | |
9215 | } | |
9216 | } | |
9217 | ||
fb94b0ca | 9218 | // Called when determining types for a Call_expression. Return true |
9219 | // if we should go ahead, false if they have already been determined. | |
9220 | ||
9221 | bool | |
9222 | Call_expression::determining_types() | |
9223 | { | |
9224 | if (this->types_are_determined_) | |
9225 | return false; | |
9226 | else | |
9227 | { | |
9228 | this->types_are_determined_ = true; | |
9229 | return true; | |
9230 | } | |
9231 | } | |
9232 | ||
e440a328 | 9233 | // Check types for parameter I. |
9234 | ||
9235 | bool | |
9236 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9237 | const Type* argument_type, | |
b13c66cd | 9238 | Location argument_location, |
e440a328 | 9239 | bool issued_error) |
9240 | { | |
9241 | std::string reason; | |
053ee6ca | 9242 | bool ok; |
9243 | if (this->are_hidden_fields_ok_) | |
9244 | ok = Type::are_assignable_hidden_ok(parameter_type, argument_type, | |
9245 | &reason); | |
9246 | else | |
9247 | ok = Type::are_assignable(parameter_type, argument_type, &reason); | |
9248 | if (!ok) | |
e440a328 | 9249 | { |
9250 | if (!issued_error) | |
9251 | { | |
9252 | if (reason.empty()) | |
9253 | error_at(argument_location, "argument %d has incompatible type", i); | |
9254 | else | |
9255 | error_at(argument_location, | |
9256 | "argument %d has incompatible type (%s)", | |
9257 | i, reason.c_str()); | |
9258 | } | |
9259 | this->set_is_error(); | |
9260 | return false; | |
9261 | } | |
9262 | return true; | |
9263 | } | |
9264 | ||
9265 | // Check types. | |
9266 | ||
9267 | void | |
9268 | Call_expression::do_check_types(Gogo*) | |
9269 | { | |
a6645f74 | 9270 | if (this->classification() == EXPRESSION_ERROR) |
9271 | return; | |
9272 | ||
e440a328 | 9273 | Function_type* fntype = this->get_function_type(); |
9274 | if (fntype == NULL) | |
9275 | { | |
5c13bd80 | 9276 | if (!this->fn_->type()->is_error()) |
e440a328 | 9277 | this->report_error(_("expected function")); |
9278 | return; | |
9279 | } | |
9280 | ||
09ea332d | 9281 | bool is_method = fntype->is_method(); |
9282 | if (is_method) | |
e440a328 | 9283 | { |
09ea332d | 9284 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9285 | Type* rtype = fntype->receiver()->type(); | |
9286 | Expression* first_arg = this->args_->front(); | |
9287 | // The language permits copying hidden fields for a method | |
9288 | // receiver. We dereference the values since receivers are | |
9289 | // always passed as pointers. | |
9290 | std::string reason; | |
9291 | if (!Type::are_assignable_hidden_ok(rtype->deref(), | |
9292 | first_arg->type()->deref(), | |
9293 | &reason)) | |
e440a328 | 9294 | { |
09ea332d | 9295 | if (reason.empty()) |
9296 | this->report_error(_("incompatible type for receiver")); | |
9297 | else | |
e440a328 | 9298 | { |
09ea332d | 9299 | error_at(this->location(), |
9300 | "incompatible type for receiver (%s)", | |
9301 | reason.c_str()); | |
9302 | this->set_is_error(); | |
e440a328 | 9303 | } |
9304 | } | |
9305 | } | |
9306 | ||
9307 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 9308 | // we don't have to worry about it here unless something is wrong. |
9309 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
9310 | { | |
9311 | if (!fntype->is_varargs()) | |
9312 | { | |
9313 | error_at(this->location(), | |
9314 | _("invalid use of %<...%> calling non-variadic function")); | |
9315 | this->set_is_error(); | |
9316 | return; | |
9317 | } | |
9318 | } | |
e440a328 | 9319 | |
9320 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9321 | if (this->args_ == NULL) | |
9322 | { | |
9323 | if (parameters != NULL && !parameters->empty()) | |
9324 | this->report_error(_("not enough arguments")); | |
9325 | } | |
9326 | else if (parameters == NULL) | |
09ea332d | 9327 | { |
9328 | if (!is_method || this->args_->size() > 1) | |
9329 | this->report_error(_("too many arguments")); | |
9330 | } | |
e440a328 | 9331 | else |
9332 | { | |
9333 | int i = 0; | |
09ea332d | 9334 | Expression_list::const_iterator pa = this->args_->begin(); |
9335 | if (is_method) | |
9336 | ++pa; | |
9337 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9338 | pt != parameters->end(); | |
9339 | ++pt, ++pa, ++i) | |
e440a328 | 9340 | { |
09ea332d | 9341 | if (pa == this->args_->end()) |
e440a328 | 9342 | { |
09ea332d | 9343 | this->report_error(_("not enough arguments")); |
e440a328 | 9344 | return; |
9345 | } | |
9346 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9347 | (*pa)->location(), false); | |
9348 | } | |
09ea332d | 9349 | if (pa != this->args_->end()) |
9350 | this->report_error(_("too many arguments")); | |
e440a328 | 9351 | } |
9352 | } | |
9353 | ||
9354 | // Return whether we have to use a temporary variable to ensure that | |
9355 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9356 | // results then it will inevitably be executed last. |
e440a328 | 9357 | |
9358 | bool | |
9359 | Call_expression::do_must_eval_in_order() const | |
9360 | { | |
ceeb4318 | 9361 | return this->result_count() > 0; |
e440a328 | 9362 | } |
9363 | ||
e440a328 | 9364 | // Get the function and the first argument to use when calling an |
9365 | // interface method. | |
9366 | ||
2387f644 | 9367 | Expression* |
e440a328 | 9368 | Call_expression::interface_method_function( |
e440a328 | 9369 | Interface_field_reference_expression* interface_method, |
2387f644 | 9370 | Expression** first_arg_ptr) |
e440a328 | 9371 | { |
2387f644 | 9372 | *first_arg_ptr = interface_method->get_underlying_object(); |
9373 | return interface_method->get_function(); | |
e440a328 | 9374 | } |
9375 | ||
9376 | // Build the call expression. | |
9377 | ||
9378 | tree | |
9379 | Call_expression::do_get_tree(Translate_context* context) | |
9380 | { | |
2c809f8f | 9381 | if (this->call_ != NULL) |
9382 | return expr_to_tree(this->call_); | |
e440a328 | 9383 | |
9384 | Function_type* fntype = this->get_function_type(); | |
9385 | if (fntype == NULL) | |
9386 | return error_mark_node; | |
9387 | ||
9388 | if (this->fn_->is_error_expression()) | |
9389 | return error_mark_node; | |
9390 | ||
9391 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9392 | Location location = this->location(); |
e440a328 | 9393 | |
9394 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9395 | Interface_field_reference_expression* interface_method = |
9396 | this->fn_->interface_field_reference_expression(); | |
9397 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9398 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9399 | |
f8bdf81a | 9400 | bool has_closure_arg; |
8381eda7 | 9401 | if (has_closure) |
f8bdf81a | 9402 | has_closure_arg = true; |
8381eda7 | 9403 | else if (func != NULL) |
f8bdf81a | 9404 | has_closure_arg = false; |
8381eda7 | 9405 | else if (is_interface_method) |
f8bdf81a | 9406 | has_closure_arg = false; |
8381eda7 | 9407 | else |
f8bdf81a | 9408 | has_closure_arg = true; |
8381eda7 | 9409 | |
e440a328 | 9410 | int nargs; |
2c809f8f | 9411 | std::vector<Bexpression*> fn_args; |
e440a328 | 9412 | if (this->args_ == NULL || this->args_->empty()) |
9413 | { | |
f8bdf81a | 9414 | nargs = is_interface_method ? 1 : 0; |
2c809f8f | 9415 | if (nargs > 0) |
9416 | fn_args.resize(1); | |
e440a328 | 9417 | } |
09ea332d | 9418 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9419 | { | |
9420 | // Passing a receiver parameter. | |
9421 | go_assert(!is_interface_method | |
9422 | && fntype->is_method() | |
9423 | && this->args_->size() == 1); | |
f8bdf81a | 9424 | nargs = 1; |
2c809f8f | 9425 | fn_args.resize(1); |
9426 | fn_args[0] = tree_to_expr(this->args_->front()->get_tree(context)); | |
09ea332d | 9427 | } |
e440a328 | 9428 | else |
9429 | { | |
9430 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9431 | |
9432 | nargs = this->args_->size(); | |
09ea332d | 9433 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9434 | nargs += i; |
2c809f8f | 9435 | fn_args.resize(nargs); |
e440a328 | 9436 | |
9437 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9438 | Expression_list::const_iterator pe = this->args_->begin(); |
9439 | if (!is_interface_method && fntype->is_method()) | |
9440 | { | |
2c809f8f | 9441 | fn_args[i] = tree_to_expr((*pe)->get_tree(context)); |
09ea332d | 9442 | ++pe; |
9443 | ++i; | |
9444 | } | |
9445 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9446 | { |
c484d925 | 9447 | go_assert(pp != params->end()); |
2c809f8f | 9448 | Expression* arg = |
9449 | Expression::convert_for_assignment(gogo, pp->type(), *pe, | |
9450 | location); | |
9451 | fn_args[i] = tree_to_expr(arg->get_tree(context)); | |
e440a328 | 9452 | } |
c484d925 | 9453 | go_assert(pp == params->end()); |
f8bdf81a | 9454 | go_assert(i == nargs); |
e440a328 | 9455 | } |
9456 | ||
2c809f8f | 9457 | Expression* fn; |
9458 | Expression* closure = NULL; | |
8381eda7 | 9459 | if (func != NULL) |
9460 | { | |
9461 | Named_object* no = func->named_object(); | |
2c809f8f | 9462 | fn = Expression::make_func_code_reference(no, location); |
9463 | if (has_closure) | |
9464 | closure = func->closure(); | |
8381eda7 | 9465 | } |
09ea332d | 9466 | else if (!is_interface_method) |
8381eda7 | 9467 | { |
2c809f8f | 9468 | closure = this->fn_; |
9469 | ||
9470 | // The backend representation of this function type is a pointer | |
9471 | // to a struct whose first field is the actual function to call. | |
9472 | Type* pfntype = | |
9473 | Type::make_pointer_type( | |
9474 | Type::make_pointer_type(Type::make_void_type())); | |
9475 | fn = Expression::make_unsafe_cast(pfntype, this->fn_, location); | |
9476 | fn = Expression::make_unary(OPERATOR_MULT, fn, location); | |
9477 | } | |
e440a328 | 9478 | else |
cf609de4 | 9479 | { |
2387f644 | 9480 | Expression* first_arg; |
2c809f8f | 9481 | fn = this->interface_method_function(interface_method, &first_arg); |
9482 | fn_args[0] = tree_to_expr(first_arg->get_tree(context)); | |
e440a328 | 9483 | } |
9484 | ||
f8bdf81a | 9485 | if (!has_closure_arg) |
2c809f8f | 9486 | go_assert(closure == NULL); |
f8bdf81a | 9487 | else |
9488 | { | |
9489 | // Pass the closure argument by calling the function function | |
9490 | // __go_set_closure. In the order_evaluations pass we have | |
9491 | // ensured that if any parameters contain call expressions, they | |
9492 | // will have been moved out to temporary variables. | |
2c809f8f | 9493 | go_assert(closure != NULL); |
9494 | Expression* set_closure = | |
9495 | Runtime::make_call(Runtime::SET_CLOSURE, location, 1, closure); | |
9496 | fn = Expression::make_compound(set_closure, fn, location); | |
f8bdf81a | 9497 | } |
9498 | ||
2c809f8f | 9499 | Bexpression* bfn = tree_to_expr(fn->get_tree(context)); |
80d1e1a8 | 9500 | |
9501 | // When not calling a named function directly, use a type conversion | |
9502 | // in case the type of the function is a recursive type which refers | |
9503 | // to itself. We don't do this for an interface method because 1) | |
9504 | // an interface method never refers to itself, so we always have a | |
9505 | // function type here; 2) we pass an extra first argument to an | |
9506 | // interface method, so fntype is not correct. | |
9507 | if (func == NULL && !is_interface_method) | |
9508 | { | |
9509 | Btype* bft = fntype->get_backend_fntype(gogo); | |
9510 | bfn = gogo->backend()->convert_expression(bft, bfn, location); | |
9511 | } | |
9512 | ||
2c809f8f | 9513 | Bexpression* call = gogo->backend()->call_expression(bfn, fn_args, location); |
e440a328 | 9514 | |
2c809f8f | 9515 | if (this->results_ != NULL) |
e440a328 | 9516 | { |
2c809f8f | 9517 | go_assert(this->call_temp_ != NULL); |
9518 | Expression* call_ref = | |
9519 | Expression::make_temporary_reference(this->call_temp_, location); | |
9520 | Bexpression* bcall_ref = tree_to_expr(call_ref->get_tree(context)); | |
9521 | Bstatement* assn_stmt = | |
9522 | gogo->backend()->assignment_statement(bcall_ref, call, location); | |
e440a328 | 9523 | |
2c809f8f | 9524 | this->call_ = this->set_results(context, bcall_ref); |
e440a328 | 9525 | |
2c809f8f | 9526 | Bexpression* set_and_call = |
9527 | gogo->backend()->compound_expression(assn_stmt, this->call_, | |
9528 | location); | |
9529 | return expr_to_tree(set_and_call); | |
9530 | } | |
e440a328 | 9531 | |
2c809f8f | 9532 | this->call_ = call; |
9533 | return expr_to_tree(this->call_); | |
e440a328 | 9534 | } |
9535 | ||
ceeb4318 | 9536 | // Set the result variables if this call returns multiple results. |
9537 | ||
2c809f8f | 9538 | Bexpression* |
9539 | Call_expression::set_results(Translate_context* context, Bexpression* call) | |
ceeb4318 | 9540 | { |
2c809f8f | 9541 | Gogo* gogo = context->gogo(); |
ceeb4318 | 9542 | |
2c809f8f | 9543 | Bexpression* results = NULL; |
b13c66cd | 9544 | Location loc = this->location(); |
2c809f8f | 9545 | |
ceeb4318 | 9546 | size_t rc = this->result_count(); |
2c809f8f | 9547 | for (size_t i = 0; i < rc; ++i) |
ceeb4318 | 9548 | { |
ceeb4318 | 9549 | Temporary_statement* temp = this->result(i); |
cd238b8d | 9550 | if (temp == NULL) |
9551 | { | |
9552 | go_assert(saw_errors()); | |
2c809f8f | 9553 | return gogo->backend()->error_expression(); |
cd238b8d | 9554 | } |
ceeb4318 | 9555 | Temporary_reference_expression* ref = |
9556 | Expression::make_temporary_reference(temp, loc); | |
9557 | ref->set_is_lvalue(); | |
ceeb4318 | 9558 | |
2c809f8f | 9559 | Bexpression* result_ref = tree_to_expr(ref->get_tree(context)); |
9560 | Bexpression* call_result = | |
9561 | gogo->backend()->struct_field_expression(call, i, loc); | |
9562 | Bstatement* assn_stmt = | |
9563 | gogo->backend()->assignment_statement(result_ref, call_result, loc); | |
ceeb4318 | 9564 | |
2c809f8f | 9565 | Bexpression* result = |
9566 | gogo->backend()->compound_expression(assn_stmt, call_result, loc); | |
ceeb4318 | 9567 | |
2c809f8f | 9568 | if (results == NULL) |
9569 | results = result; | |
9570 | else | |
9571 | { | |
9572 | Bstatement* expr_stmt = gogo->backend()->expression_statement(result); | |
9573 | results = | |
9574 | gogo->backend()->compound_expression(expr_stmt, results, loc); | |
9575 | } | |
9576 | } | |
9577 | return results; | |
ceeb4318 | 9578 | } |
9579 | ||
d751bb78 | 9580 | // Dump ast representation for a call expressin. |
9581 | ||
9582 | void | |
9583 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9584 | { | |
9585 | this->fn_->dump_expression(ast_dump_context); | |
9586 | ast_dump_context->ostream() << "("; | |
9587 | if (args_ != NULL) | |
9588 | ast_dump_context->dump_expression_list(this->args_); | |
9589 | ||
9590 | ast_dump_context->ostream() << ") "; | |
9591 | } | |
9592 | ||
e440a328 | 9593 | // Make a call expression. |
9594 | ||
9595 | Call_expression* | |
9596 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9597 | Location location) |
e440a328 | 9598 | { |
9599 | return new Call_expression(fn, args, is_varargs, location); | |
9600 | } | |
9601 | ||
9602 | // A single result from a call which returns multiple results. | |
9603 | ||
9604 | class Call_result_expression : public Expression | |
9605 | { | |
9606 | public: | |
9607 | Call_result_expression(Call_expression* call, unsigned int index) | |
9608 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
9609 | call_(call), index_(index) | |
9610 | { } | |
9611 | ||
9612 | protected: | |
9613 | int | |
9614 | do_traverse(Traverse*); | |
9615 | ||
9616 | Type* | |
9617 | do_type(); | |
9618 | ||
9619 | void | |
9620 | do_determine_type(const Type_context*); | |
9621 | ||
9622 | void | |
9623 | do_check_types(Gogo*); | |
9624 | ||
9625 | Expression* | |
9626 | do_copy() | |
9627 | { | |
9628 | return new Call_result_expression(this->call_->call_expression(), | |
9629 | this->index_); | |
9630 | } | |
9631 | ||
9632 | bool | |
9633 | do_must_eval_in_order() const | |
9634 | { return true; } | |
9635 | ||
9636 | tree | |
9637 | do_get_tree(Translate_context*); | |
9638 | ||
d751bb78 | 9639 | void |
9640 | do_dump_expression(Ast_dump_context*) const; | |
9641 | ||
e440a328 | 9642 | private: |
9643 | // The underlying call expression. | |
9644 | Expression* call_; | |
9645 | // Which result we want. | |
9646 | unsigned int index_; | |
9647 | }; | |
9648 | ||
9649 | // Traverse a call result. | |
9650 | ||
9651 | int | |
9652 | Call_result_expression::do_traverse(Traverse* traverse) | |
9653 | { | |
9654 | if (traverse->remember_expression(this->call_)) | |
9655 | { | |
9656 | // We have already traversed the call expression. | |
9657 | return TRAVERSE_CONTINUE; | |
9658 | } | |
9659 | return Expression::traverse(&this->call_, traverse); | |
9660 | } | |
9661 | ||
9662 | // Get the type. | |
9663 | ||
9664 | Type* | |
9665 | Call_result_expression::do_type() | |
9666 | { | |
425dd051 | 9667 | if (this->classification() == EXPRESSION_ERROR) |
9668 | return Type::make_error_type(); | |
9669 | ||
e440a328 | 9670 | // THIS->CALL_ can be replaced with a temporary reference due to |
9671 | // Call_expression::do_must_eval_in_order when there is an error. | |
9672 | Call_expression* ce = this->call_->call_expression(); | |
9673 | if (ce == NULL) | |
5e85f268 | 9674 | { |
9675 | this->set_is_error(); | |
9676 | return Type::make_error_type(); | |
9677 | } | |
e440a328 | 9678 | Function_type* fntype = ce->get_function_type(); |
9679 | if (fntype == NULL) | |
5e85f268 | 9680 | { |
e37658e2 | 9681 | if (ce->issue_error()) |
99b3f06f | 9682 | { |
9683 | if (!ce->fn()->type()->is_error()) | |
9684 | this->report_error(_("expected function")); | |
9685 | } | |
5e85f268 | 9686 | this->set_is_error(); |
9687 | return Type::make_error_type(); | |
9688 | } | |
e440a328 | 9689 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9690 | if (results == NULL || results->size() < 2) |
7b8d861f | 9691 | { |
ceeb4318 | 9692 | if (ce->issue_error()) |
9693 | this->report_error(_("number of results does not match " | |
9694 | "number of values")); | |
7b8d861f | 9695 | return Type::make_error_type(); |
9696 | } | |
e440a328 | 9697 | Typed_identifier_list::const_iterator pr = results->begin(); |
9698 | for (unsigned int i = 0; i < this->index_; ++i) | |
9699 | { | |
9700 | if (pr == results->end()) | |
425dd051 | 9701 | break; |
e440a328 | 9702 | ++pr; |
9703 | } | |
9704 | if (pr == results->end()) | |
425dd051 | 9705 | { |
ceeb4318 | 9706 | if (ce->issue_error()) |
9707 | this->report_error(_("number of results does not match " | |
9708 | "number of values")); | |
425dd051 | 9709 | return Type::make_error_type(); |
9710 | } | |
e440a328 | 9711 | return pr->type(); |
9712 | } | |
9713 | ||
425dd051 | 9714 | // Check the type. Just make sure that we trigger the warning in |
9715 | // do_type. | |
e440a328 | 9716 | |
9717 | void | |
9718 | Call_result_expression::do_check_types(Gogo*) | |
9719 | { | |
425dd051 | 9720 | this->type(); |
e440a328 | 9721 | } |
9722 | ||
9723 | // Determine the type. We have nothing to do here, but the 0 result | |
9724 | // needs to pass down to the caller. | |
9725 | ||
9726 | void | |
9727 | Call_result_expression::do_determine_type(const Type_context*) | |
9728 | { | |
fb94b0ca | 9729 | this->call_->determine_type_no_context(); |
e440a328 | 9730 | } |
9731 | ||
ceeb4318 | 9732 | // Return the tree. We just refer to the temporary set by the call |
9733 | // expression. We don't do this at lowering time because it makes it | |
9734 | // hard to evaluate the call at the right time. | |
e440a328 | 9735 | |
9736 | tree | |
9737 | Call_result_expression::do_get_tree(Translate_context* context) | |
9738 | { | |
ceeb4318 | 9739 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 9740 | if (ce == NULL) |
9741 | { | |
9742 | go_assert(this->call_->is_error_expression()); | |
9743 | return error_mark_node; | |
9744 | } | |
ceeb4318 | 9745 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 9746 | if (ts == NULL) |
9747 | { | |
9748 | go_assert(saw_errors()); | |
9749 | return error_mark_node; | |
9750 | } | |
ceeb4318 | 9751 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
9752 | return ref->get_tree(context); | |
e440a328 | 9753 | } |
9754 | ||
d751bb78 | 9755 | // Dump ast representation for a call result expression. |
9756 | ||
9757 | void | |
9758 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9759 | const | |
9760 | { | |
9761 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
9762 | // (struct) and the fields are referenced instead. | |
9763 | ast_dump_context->ostream() << this->index_ << "@("; | |
9764 | ast_dump_context->dump_expression(this->call_); | |
9765 | ast_dump_context->ostream() << ")"; | |
9766 | } | |
9767 | ||
e440a328 | 9768 | // Make a reference to a single result of a call which returns |
9769 | // multiple results. | |
9770 | ||
9771 | Expression* | |
9772 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9773 | { | |
9774 | return new Call_result_expression(call, index); | |
9775 | } | |
9776 | ||
9777 | // Class Index_expression. | |
9778 | ||
9779 | // Traversal. | |
9780 | ||
9781 | int | |
9782 | Index_expression::do_traverse(Traverse* traverse) | |
9783 | { | |
9784 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9785 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9786 | || (this->end_ != NULL | |
acf2b673 | 9787 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) |
9788 | || (this->cap_ != NULL | |
9789 | && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)) | |
e440a328 | 9790 | return TRAVERSE_EXIT; |
9791 | return TRAVERSE_CONTINUE; | |
9792 | } | |
9793 | ||
9794 | // Lower an index expression. This converts the generic index | |
9795 | // expression into an array index, a string index, or a map index. | |
9796 | ||
9797 | Expression* | |
ceeb4318 | 9798 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 9799 | { |
b13c66cd | 9800 | Location location = this->location(); |
e440a328 | 9801 | Expression* left = this->left_; |
9802 | Expression* start = this->start_; | |
9803 | Expression* end = this->end_; | |
acf2b673 | 9804 | Expression* cap = this->cap_; |
e440a328 | 9805 | |
9806 | Type* type = left->type(); | |
5c13bd80 | 9807 | if (type->is_error()) |
e440a328 | 9808 | return Expression::make_error(location); |
b0cf7ddd | 9809 | else if (left->is_type_expression()) |
9810 | { | |
9811 | error_at(location, "attempt to index type expression"); | |
9812 | return Expression::make_error(location); | |
9813 | } | |
e440a328 | 9814 | else if (type->array_type() != NULL) |
acf2b673 | 9815 | return Expression::make_array_index(left, start, end, cap, location); |
e440a328 | 9816 | else if (type->points_to() != NULL |
9817 | && type->points_to()->array_type() != NULL | |
411eb89e | 9818 | && !type->points_to()->is_slice_type()) |
e440a328 | 9819 | { |
9820 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9821 | location); | |
38092374 | 9822 | |
9823 | // For an ordinary index into the array, the pointer will be | |
9824 | // dereferenced. For a slice it will not--the resulting slice | |
9825 | // will simply reuse the pointer, which is incorrect if that | |
9826 | // pointer is nil. | |
9827 | if (end != NULL || cap != NULL) | |
9828 | deref->issue_nil_check(); | |
9829 | ||
acf2b673 | 9830 | return Expression::make_array_index(deref, start, end, cap, location); |
e440a328 | 9831 | } |
9832 | else if (type->is_string_type()) | |
acf2b673 | 9833 | { |
9834 | if (cap != NULL) | |
9835 | { | |
9836 | error_at(location, "invalid 3-index slice of string"); | |
9837 | return Expression::make_error(location); | |
9838 | } | |
9839 | return Expression::make_string_index(left, start, end, location); | |
9840 | } | |
e440a328 | 9841 | else if (type->map_type() != NULL) |
9842 | { | |
acf2b673 | 9843 | if (end != NULL || cap != NULL) |
e440a328 | 9844 | { |
9845 | error_at(location, "invalid slice of map"); | |
9846 | return Expression::make_error(location); | |
9847 | } | |
6d4c2432 | 9848 | Map_index_expression* ret = Expression::make_map_index(left, start, |
9849 | location); | |
e440a328 | 9850 | if (this->is_lvalue_) |
9851 | ret->set_is_lvalue(); | |
9852 | return ret; | |
9853 | } | |
9854 | else | |
9855 | { | |
9856 | error_at(location, | |
9857 | "attempt to index object which is not array, string, or map"); | |
9858 | return Expression::make_error(location); | |
9859 | } | |
9860 | } | |
9861 | ||
acf2b673 | 9862 | // Write an indexed expression |
9863 | // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context. | |
d751bb78 | 9864 | |
9865 | void | |
9866 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
9867 | const Expression* expr, | |
9868 | const Expression* start, | |
acf2b673 | 9869 | const Expression* end, |
9870 | const Expression* cap) | |
d751bb78 | 9871 | { |
9872 | expr->dump_expression(ast_dump_context); | |
9873 | ast_dump_context->ostream() << "["; | |
9874 | start->dump_expression(ast_dump_context); | |
9875 | if (end != NULL) | |
9876 | { | |
9877 | ast_dump_context->ostream() << ":"; | |
9878 | end->dump_expression(ast_dump_context); | |
9879 | } | |
acf2b673 | 9880 | if (cap != NULL) |
9881 | { | |
9882 | ast_dump_context->ostream() << ":"; | |
9883 | cap->dump_expression(ast_dump_context); | |
9884 | } | |
d751bb78 | 9885 | ast_dump_context->ostream() << "]"; |
9886 | } | |
9887 | ||
9888 | // Dump ast representation for an index expression. | |
9889 | ||
9890 | void | |
9891 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9892 | const | |
9893 | { | |
9894 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
acf2b673 | 9895 | this->start_, this->end_, this->cap_); |
d751bb78 | 9896 | } |
9897 | ||
e440a328 | 9898 | // Make an index expression. |
9899 | ||
9900 | Expression* | |
9901 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
acf2b673 | 9902 | Expression* cap, Location location) |
e440a328 | 9903 | { |
acf2b673 | 9904 | return new Index_expression(left, start, end, cap, location); |
e440a328 | 9905 | } |
9906 | ||
9907 | // An array index. This is used for both indexing and slicing. | |
9908 | ||
9909 | class Array_index_expression : public Expression | |
9910 | { | |
9911 | public: | |
9912 | Array_index_expression(Expression* array, Expression* start, | |
acf2b673 | 9913 | Expression* end, Expression* cap, Location location) |
e440a328 | 9914 | : Expression(EXPRESSION_ARRAY_INDEX, location), |
acf2b673 | 9915 | array_(array), start_(start), end_(end), cap_(cap), type_(NULL) |
e440a328 | 9916 | { } |
9917 | ||
9918 | protected: | |
9919 | int | |
9920 | do_traverse(Traverse*); | |
9921 | ||
2c809f8f | 9922 | Expression* |
9923 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
9924 | ||
e440a328 | 9925 | Type* |
9926 | do_type(); | |
9927 | ||
9928 | void | |
9929 | do_determine_type(const Type_context*); | |
9930 | ||
9931 | void | |
9932 | do_check_types(Gogo*); | |
9933 | ||
9934 | Expression* | |
9935 | do_copy() | |
9936 | { | |
9937 | return Expression::make_array_index(this->array_->copy(), | |
9938 | this->start_->copy(), | |
9939 | (this->end_ == NULL | |
9940 | ? NULL | |
9941 | : this->end_->copy()), | |
acf2b673 | 9942 | (this->cap_ == NULL |
9943 | ? NULL | |
9944 | : this->cap_->copy()), | |
e440a328 | 9945 | this->location()); |
9946 | } | |
9947 | ||
baef9f7a | 9948 | bool |
9949 | do_must_eval_subexpressions_in_order(int* skip) const | |
9950 | { | |
9951 | *skip = 1; | |
9952 | return true; | |
9953 | } | |
9954 | ||
e440a328 | 9955 | bool |
9956 | do_is_addressable() const; | |
9957 | ||
9958 | void | |
9959 | do_address_taken(bool escapes) | |
9960 | { this->array_->address_taken(escapes); } | |
9961 | ||
56080003 | 9962 | void |
9963 | do_issue_nil_check() | |
9964 | { this->array_->issue_nil_check(); } | |
9965 | ||
e440a328 | 9966 | tree |
9967 | do_get_tree(Translate_context*); | |
9968 | ||
d751bb78 | 9969 | void |
9970 | do_dump_expression(Ast_dump_context*) const; | |
9971 | ||
e440a328 | 9972 | private: |
9973 | // The array we are getting a value from. | |
9974 | Expression* array_; | |
9975 | // The start or only index. | |
9976 | Expression* start_; | |
9977 | // The end index of a slice. This may be NULL for a simple array | |
9978 | // index, or it may be a nil expression for the length of the array. | |
9979 | Expression* end_; | |
acf2b673 | 9980 | // The capacity argument of a slice. This may be NULL for an array index or |
9981 | // slice. | |
9982 | Expression* cap_; | |
e440a328 | 9983 | // The type of the expression. |
9984 | Type* type_; | |
9985 | }; | |
9986 | ||
9987 | // Array index traversal. | |
9988 | ||
9989 | int | |
9990 | Array_index_expression::do_traverse(Traverse* traverse) | |
9991 | { | |
9992 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9993 | return TRAVERSE_EXIT; | |
9994 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9995 | return TRAVERSE_EXIT; | |
9996 | if (this->end_ != NULL) | |
9997 | { | |
9998 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9999 | return TRAVERSE_EXIT; | |
10000 | } | |
acf2b673 | 10001 | if (this->cap_ != NULL) |
10002 | { | |
10003 | if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
10004 | return TRAVERSE_EXIT; | |
10005 | } | |
e440a328 | 10006 | return TRAVERSE_CONTINUE; |
10007 | } | |
10008 | ||
10009 | // Return the type of an array index. | |
10010 | ||
10011 | Type* | |
10012 | Array_index_expression::do_type() | |
10013 | { | |
10014 | if (this->type_ == NULL) | |
10015 | { | |
10016 | Array_type* type = this->array_->type()->array_type(); | |
10017 | if (type == NULL) | |
10018 | this->type_ = Type::make_error_type(); | |
10019 | else if (this->end_ == NULL) | |
10020 | this->type_ = type->element_type(); | |
411eb89e | 10021 | else if (type->is_slice_type()) |
e440a328 | 10022 | { |
10023 | // A slice of a slice has the same type as the original | |
10024 | // slice. | |
10025 | this->type_ = this->array_->type()->deref(); | |
10026 | } | |
10027 | else | |
10028 | { | |
10029 | // A slice of an array is a slice. | |
10030 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10031 | } | |
10032 | } | |
10033 | return this->type_; | |
10034 | } | |
10035 | ||
10036 | // Set the type of an array index. | |
10037 | ||
10038 | void | |
10039 | Array_index_expression::do_determine_type(const Type_context*) | |
10040 | { | |
10041 | this->array_->determine_type_no_context(); | |
7917ad68 | 10042 | this->start_->determine_type_no_context(); |
e440a328 | 10043 | if (this->end_ != NULL) |
7917ad68 | 10044 | this->end_->determine_type_no_context(); |
acf2b673 | 10045 | if (this->cap_ != NULL) |
10046 | this->cap_->determine_type_no_context(); | |
e440a328 | 10047 | } |
10048 | ||
10049 | // Check types of an array index. | |
10050 | ||
10051 | void | |
10052 | Array_index_expression::do_check_types(Gogo*) | |
10053 | { | |
f6bc81e6 | 10054 | Numeric_constant nc; |
10055 | unsigned long v; | |
10056 | if (this->start_->type()->integer_type() == NULL | |
10057 | && !this->start_->type()->is_error() | |
10058 | && (!this->start_->numeric_constant_value(&nc) | |
10059 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10060 | this->report_error(_("index must be integer")); |
10061 | if (this->end_ != NULL | |
10062 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10063 | && !this->end_->type()->is_error() |
10064 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10065 | && !this->end_->is_error_expression() |
10066 | && (!this->end_->numeric_constant_value(&nc) | |
10067 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10068 | this->report_error(_("slice end must be integer")); |
acf2b673 | 10069 | if (this->cap_ != NULL |
10070 | && this->cap_->type()->integer_type() == NULL | |
10071 | && !this->cap_->type()->is_error() | |
10072 | && !this->cap_->is_nil_expression() | |
10073 | && !this->cap_->is_error_expression() | |
10074 | && (!this->cap_->numeric_constant_value(&nc) | |
10075 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
10076 | this->report_error(_("slice capacity must be integer")); | |
e440a328 | 10077 | |
10078 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10079 | if (array_type == NULL) |
10080 | { | |
c484d925 | 10081 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10082 | return; |
10083 | } | |
e440a328 | 10084 | |
10085 | unsigned int int_bits = | |
10086 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10087 | ||
0c77715b | 10088 | Numeric_constant lvalnc; |
e440a328 | 10089 | mpz_t lval; |
e440a328 | 10090 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10091 | && array_type->length()->numeric_constant_value(&lvalnc) |
10092 | && lvalnc.to_int(&lval)); | |
10093 | Numeric_constant inc; | |
e440a328 | 10094 | mpz_t ival; |
0bd5d859 | 10095 | bool ival_valid = false; |
0c77715b | 10096 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10097 | { |
0bd5d859 | 10098 | ival_valid = true; |
e440a328 | 10099 | if (mpz_sgn(ival) < 0 |
10100 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10101 | || (lval_valid | |
10102 | && (this->end_ == NULL | |
10103 | ? mpz_cmp(ival, lval) >= 0 | |
10104 | : mpz_cmp(ival, lval) > 0))) | |
10105 | { | |
10106 | error_at(this->start_->location(), "array index out of bounds"); | |
10107 | this->set_is_error(); | |
10108 | } | |
10109 | } | |
10110 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10111 | { | |
0c77715b | 10112 | Numeric_constant enc; |
10113 | mpz_t eval; | |
acf2b673 | 10114 | bool eval_valid = false; |
0c77715b | 10115 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) |
e440a328 | 10116 | { |
acf2b673 | 10117 | eval_valid = true; |
0c77715b | 10118 | if (mpz_sgn(eval) < 0 |
10119 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10120 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10121 | { |
10122 | error_at(this->end_->location(), "array index out of bounds"); | |
10123 | this->set_is_error(); | |
10124 | } | |
0bd5d859 | 10125 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10126 | this->report_error(_("inverted slice range")); | |
e440a328 | 10127 | } |
acf2b673 | 10128 | |
10129 | Numeric_constant cnc; | |
10130 | mpz_t cval; | |
10131 | if (this->cap_ != NULL | |
10132 | && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval)) | |
10133 | { | |
10134 | if (mpz_sgn(cval) < 0 | |
10135 | || mpz_sizeinbase(cval, 2) >= int_bits | |
10136 | || (lval_valid && mpz_cmp(cval, lval) > 0)) | |
10137 | { | |
10138 | error_at(this->cap_->location(), "array index out of bounds"); | |
10139 | this->set_is_error(); | |
10140 | } | |
10141 | else if (ival_valid && mpz_cmp(ival, cval) > 0) | |
10142 | { | |
10143 | error_at(this->cap_->location(), | |
10144 | "invalid slice index: capacity less than start"); | |
10145 | this->set_is_error(); | |
10146 | } | |
10147 | else if (eval_valid && mpz_cmp(eval, cval) > 0) | |
10148 | { | |
10149 | error_at(this->cap_->location(), | |
10150 | "invalid slice index: capacity less than length"); | |
10151 | this->set_is_error(); | |
10152 | } | |
10153 | mpz_clear(cval); | |
10154 | } | |
10155 | ||
10156 | if (eval_valid) | |
10157 | mpz_clear(eval); | |
e440a328 | 10158 | } |
0bd5d859 | 10159 | if (ival_valid) |
10160 | mpz_clear(ival); | |
0c77715b | 10161 | if (lval_valid) |
10162 | mpz_clear(lval); | |
e440a328 | 10163 | |
10164 | // A slice of an array requires an addressable array. A slice of a | |
10165 | // slice is always possible. | |
411eb89e | 10166 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10167 | { |
10168 | if (!this->array_->is_addressable()) | |
8da39c3b | 10169 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10170 | else |
10171 | this->array_->address_taken(true); | |
10172 | } | |
e440a328 | 10173 | } |
10174 | ||
2c809f8f | 10175 | // Flatten array indexing by using temporary variables for slices and indexes. |
35a54f17 | 10176 | |
10177 | Expression* | |
10178 | Array_index_expression::do_flatten(Gogo*, Named_object*, | |
10179 | Statement_inserter* inserter) | |
10180 | { | |
10181 | Location loc = this->location(); | |
2c809f8f | 10182 | Temporary_statement* temp; |
35a54f17 | 10183 | if (this->array_->type()->is_slice_type() && !this->array_->is_variable()) |
10184 | { | |
2c809f8f | 10185 | temp = Statement::make_temporary(NULL, this->array_, loc); |
35a54f17 | 10186 | inserter->insert(temp); |
10187 | this->array_ = Expression::make_temporary_reference(temp, loc); | |
10188 | } | |
2c809f8f | 10189 | if (!this->start_->is_variable()) |
10190 | { | |
10191 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
10192 | inserter->insert(temp); | |
10193 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10194 | } | |
10195 | if (this->end_ != NULL | |
10196 | && !this->end_->is_nil_expression() | |
10197 | && !this->end_->is_variable()) | |
10198 | { | |
10199 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
10200 | inserter->insert(temp); | |
10201 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10202 | } | |
10203 | if (this->cap_ != NULL && !this->cap_->is_variable()) | |
10204 | { | |
10205 | temp = Statement::make_temporary(NULL, this->cap_, loc); | |
10206 | inserter->insert(temp); | |
10207 | this->cap_ = Expression::make_temporary_reference(temp, loc); | |
10208 | } | |
10209 | ||
35a54f17 | 10210 | return this; |
10211 | } | |
10212 | ||
e440a328 | 10213 | // Return whether this expression is addressable. |
10214 | ||
10215 | bool | |
10216 | Array_index_expression::do_is_addressable() const | |
10217 | { | |
10218 | // A slice expression is not addressable. | |
10219 | if (this->end_ != NULL) | |
10220 | return false; | |
10221 | ||
10222 | // An index into a slice is addressable. | |
411eb89e | 10223 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10224 | return true; |
10225 | ||
10226 | // An index into an array is addressable if the array is | |
10227 | // addressable. | |
10228 | return this->array_->is_addressable(); | |
10229 | } | |
10230 | ||
10231 | // Get a tree for an array index. | |
10232 | ||
10233 | tree | |
10234 | Array_index_expression::do_get_tree(Translate_context* context) | |
10235 | { | |
e440a328 | 10236 | Array_type* array_type = this->array_->type()->array_type(); |
d8cd8e2d | 10237 | if (array_type == NULL) |
10238 | { | |
c484d925 | 10239 | go_assert(this->array_->type()->is_error()); |
d8cd8e2d | 10240 | return error_mark_node; |
10241 | } | |
35a54f17 | 10242 | go_assert(!array_type->is_slice_type() || this->array_->is_variable()); |
e440a328 | 10243 | |
2c809f8f | 10244 | Location loc = this->location(); |
10245 | Gogo* gogo = context->gogo(); | |
10246 | ||
10247 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); | |
e440a328 | 10248 | |
2c809f8f | 10249 | // We need to convert the length and capacity to the Go "int" type here |
10250 | // because the length of a fixed-length array could be of type "uintptr" | |
10251 | // and gimple disallows binary operations between "uintptr" and other | |
10252 | // integer types. FIXME. | |
10253 | Bexpression* length = NULL; | |
a04bfdfc | 10254 | if (this->end_ == NULL || this->end_->is_nil_expression()) |
10255 | { | |
35a54f17 | 10256 | Expression* len = array_type->get_length(gogo, this->array_); |
2c809f8f | 10257 | length = tree_to_expr(len->get_tree(context)); |
10258 | length = gogo->backend()->convert_expression(int_btype, length, loc); | |
a04bfdfc | 10259 | } |
10260 | ||
2c809f8f | 10261 | Bexpression* capacity = NULL; |
a04bfdfc | 10262 | if (this->end_ != NULL) |
10263 | { | |
35a54f17 | 10264 | Expression* cap = array_type->get_capacity(gogo, this->array_); |
2c809f8f | 10265 | capacity = tree_to_expr(cap->get_tree(context)); |
10266 | capacity = gogo->backend()->convert_expression(int_btype, capacity, loc); | |
a04bfdfc | 10267 | } |
10268 | ||
2c809f8f | 10269 | Bexpression* cap_arg = capacity; |
acf2b673 | 10270 | if (this->cap_ != NULL) |
10271 | { | |
2c809f8f | 10272 | cap_arg = tree_to_expr(this->cap_->get_tree(context)); |
10273 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
acf2b673 | 10274 | } |
10275 | ||
2c809f8f | 10276 | if (length == NULL) |
10277 | length = cap_arg; | |
e440a328 | 10278 | |
10279 | int code = (array_type->length() != NULL | |
10280 | ? (this->end_ == NULL | |
10281 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10282 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10283 | : (this->end_ == NULL | |
10284 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10285 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
2c809f8f | 10286 | Bexpression* crash = |
10287 | tree_to_expr(gogo->runtime_error(code, loc)->get_tree(context)); | |
10288 | ||
10289 | Expression* bounds_check = Expression::check_bounds(this->start_, loc); | |
10290 | Bexpression* bad_index = tree_to_expr(bounds_check->get_tree(context)); | |
10291 | ||
10292 | Bexpression* start = tree_to_expr(this->start_->get_tree(context)); | |
10293 | start = gogo->backend()->convert_expression(int_btype, start, loc); | |
10294 | Bexpression* start_too_large = | |
10295 | gogo->backend()->binary_expression((this->end_ == NULL | |
10296 | ? OPERATOR_GE | |
10297 | : OPERATOR_GT), | |
10298 | start, | |
10299 | (this->end_ == NULL | |
10300 | ? length | |
10301 | : capacity), | |
10302 | loc); | |
10303 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, start_too_large, | |
10304 | bad_index, loc); | |
e440a328 | 10305 | |
10306 | if (this->end_ == NULL) | |
10307 | { | |
10308 | // Simple array indexing. This has to return an l-value, so | |
2c809f8f | 10309 | // wrap the index check into START. |
10310 | start = | |
10311 | gogo->backend()->conditional_expression(int_btype, bad_index, | |
10312 | crash, start, loc); | |
e440a328 | 10313 | |
2c809f8f | 10314 | Bexpression* ret; |
e440a328 | 10315 | if (array_type->length() != NULL) |
10316 | { | |
2c809f8f | 10317 | Bexpression* array = tree_to_expr(this->array_->get_tree(context)); |
10318 | ret = gogo->backend()->array_index_expression(array, start, loc); | |
e440a328 | 10319 | } |
10320 | else | |
10321 | { | |
2c809f8f | 10322 | // Slice. |
10323 | Expression* valptr = | |
35a54f17 | 10324 | array_type->get_value_pointer(gogo, this->array_); |
2c809f8f | 10325 | Bexpression* ptr = tree_to_expr(valptr->get_tree(context)); |
10326 | ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc); | |
9b27b43c | 10327 | |
10328 | Type* ele_type = this->array_->type()->array_type()->element_type(); | |
10329 | Btype* ele_btype = ele_type->get_backend(gogo); | |
10330 | ret = gogo->backend()->indirect_expression(ele_btype, ptr, true, loc); | |
e440a328 | 10331 | } |
2c809f8f | 10332 | return expr_to_tree(ret); |
e440a328 | 10333 | } |
10334 | ||
10335 | // Array slice. | |
10336 | ||
acf2b673 | 10337 | if (this->cap_ != NULL) |
10338 | { | |
2c809f8f | 10339 | bounds_check = Expression::check_bounds(this->cap_, loc); |
10340 | Bexpression* bounds_bcheck = | |
10341 | tree_to_expr(bounds_check->get_tree(context)); | |
10342 | bad_index = | |
10343 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10344 | bad_index, loc); | |
10345 | cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc); | |
10346 | ||
10347 | Bexpression* cap_too_small = | |
10348 | gogo->backend()->binary_expression(OPERATOR_LT, cap_arg, start, loc); | |
10349 | Bexpression* cap_too_large = | |
10350 | gogo->backend()->binary_expression(OPERATOR_GT, cap_arg, capacity, loc); | |
10351 | Bexpression* bad_cap = | |
10352 | gogo->backend()->binary_expression(OPERATOR_OROR, cap_too_small, | |
10353 | cap_too_large, loc); | |
10354 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_cap, | |
10355 | bad_index, loc); | |
10356 | } | |
10357 | ||
10358 | Bexpression* end; | |
e440a328 | 10359 | if (this->end_->is_nil_expression()) |
2c809f8f | 10360 | end = length; |
e440a328 | 10361 | else |
10362 | { | |
2c809f8f | 10363 | bounds_check = Expression::check_bounds(this->end_, loc); |
10364 | Bexpression* bounds_bcheck = | |
10365 | tree_to_expr(bounds_check->get_tree(context)); | |
e440a328 | 10366 | |
2c809f8f | 10367 | bad_index = |
10368 | gogo->backend()->binary_expression(OPERATOR_OROR, bounds_bcheck, | |
10369 | bad_index, loc); | |
e440a328 | 10370 | |
2c809f8f | 10371 | end = tree_to_expr(this->end_->get_tree(context)); |
10372 | end = gogo->backend()->convert_expression(int_btype, end, loc); | |
10373 | Bexpression* end_too_small = | |
10374 | gogo->backend()->binary_expression(OPERATOR_LT, end, start, loc); | |
10375 | Bexpression* end_too_large = | |
10376 | gogo->backend()->binary_expression(OPERATOR_GT, end, cap_arg, loc); | |
10377 | Bexpression* bad_end = | |
10378 | gogo->backend()->binary_expression(OPERATOR_OROR, end_too_small, | |
10379 | end_too_large, loc); | |
10380 | bad_index = gogo->backend()->binary_expression(OPERATOR_OROR, bad_end, | |
10381 | bad_index, loc); | |
e440a328 | 10382 | } |
10383 | ||
35a54f17 | 10384 | Expression* valptr = array_type->get_value_pointer(gogo, this->array_); |
2c809f8f | 10385 | Bexpression* val = tree_to_expr(valptr->get_tree(context)); |
10386 | val = gogo->backend()->pointer_offset_expression(val, start, loc); | |
e440a328 | 10387 | |
2c809f8f | 10388 | Bexpression* result_length = |
10389 | gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc); | |
e440a328 | 10390 | |
2c809f8f | 10391 | Bexpression* result_capacity = |
10392 | gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc); | |
e440a328 | 10393 | |
2c809f8f | 10394 | Btype* struct_btype = this->type()->get_backend(gogo); |
10395 | std::vector<Bexpression*> init; | |
10396 | init.push_back(val); | |
10397 | init.push_back(result_length); | |
10398 | init.push_back(result_capacity); | |
e440a328 | 10399 | |
2c809f8f | 10400 | Bexpression* ctor = |
10401 | gogo->backend()->constructor_expression(struct_btype, init, loc); | |
10402 | Bexpression* ret = | |
10403 | gogo->backend()->conditional_expression(struct_btype, bad_index, | |
10404 | crash, ctor, loc); | |
e440a328 | 10405 | |
2c809f8f | 10406 | return expr_to_tree(ret); |
e440a328 | 10407 | } |
10408 | ||
d751bb78 | 10409 | // Dump ast representation for an array index expression. |
10410 | ||
10411 | void | |
10412 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10413 | const | |
10414 | { | |
10415 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
acf2b673 | 10416 | this->start_, this->end_, this->cap_); |
d751bb78 | 10417 | } |
10418 | ||
acf2b673 | 10419 | // Make an array index expression. END and CAP may be NULL. |
e440a328 | 10420 | |
10421 | Expression* | |
10422 | Expression::make_array_index(Expression* array, Expression* start, | |
acf2b673 | 10423 | Expression* end, Expression* cap, |
10424 | Location location) | |
e440a328 | 10425 | { |
acf2b673 | 10426 | return new Array_index_expression(array, start, end, cap, location); |
e440a328 | 10427 | } |
10428 | ||
10429 | // A string index. This is used for both indexing and slicing. | |
10430 | ||
10431 | class String_index_expression : public Expression | |
10432 | { | |
10433 | public: | |
10434 | String_index_expression(Expression* string, Expression* start, | |
b13c66cd | 10435 | Expression* end, Location location) |
e440a328 | 10436 | : Expression(EXPRESSION_STRING_INDEX, location), |
10437 | string_(string), start_(start), end_(end) | |
10438 | { } | |
10439 | ||
10440 | protected: | |
10441 | int | |
10442 | do_traverse(Traverse*); | |
10443 | ||
2c809f8f | 10444 | Expression* |
10445 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
10446 | ||
e440a328 | 10447 | Type* |
10448 | do_type(); | |
10449 | ||
10450 | void | |
10451 | do_determine_type(const Type_context*); | |
10452 | ||
10453 | void | |
10454 | do_check_types(Gogo*); | |
10455 | ||
10456 | Expression* | |
10457 | do_copy() | |
10458 | { | |
10459 | return Expression::make_string_index(this->string_->copy(), | |
10460 | this->start_->copy(), | |
10461 | (this->end_ == NULL | |
10462 | ? NULL | |
10463 | : this->end_->copy()), | |
10464 | this->location()); | |
10465 | } | |
10466 | ||
baef9f7a | 10467 | bool |
10468 | do_must_eval_subexpressions_in_order(int* skip) const | |
10469 | { | |
10470 | *skip = 1; | |
10471 | return true; | |
10472 | } | |
10473 | ||
e440a328 | 10474 | tree |
10475 | do_get_tree(Translate_context*); | |
10476 | ||
d751bb78 | 10477 | void |
10478 | do_dump_expression(Ast_dump_context*) const; | |
10479 | ||
e440a328 | 10480 | private: |
10481 | // The string we are getting a value from. | |
10482 | Expression* string_; | |
10483 | // The start or only index. | |
10484 | Expression* start_; | |
10485 | // The end index of a slice. This may be NULL for a single index, | |
10486 | // or it may be a nil expression for the length of the string. | |
10487 | Expression* end_; | |
10488 | }; | |
10489 | ||
10490 | // String index traversal. | |
10491 | ||
10492 | int | |
10493 | String_index_expression::do_traverse(Traverse* traverse) | |
10494 | { | |
10495 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10496 | return TRAVERSE_EXIT; | |
10497 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10498 | return TRAVERSE_EXIT; | |
10499 | if (this->end_ != NULL) | |
10500 | { | |
10501 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10502 | return TRAVERSE_EXIT; | |
10503 | } | |
10504 | return TRAVERSE_CONTINUE; | |
10505 | } | |
10506 | ||
2c809f8f | 10507 | Expression* |
10508 | String_index_expression::do_flatten(Gogo*, Named_object*, | |
10509 | Statement_inserter* inserter) | |
e440a328 | 10510 | { |
2c809f8f | 10511 | Temporary_statement* temp; |
10512 | Location loc = this->location(); | |
10513 | if (!this->string_->is_variable()) | |
10514 | { | |
10515 | temp = Statement::make_temporary(NULL, this->string_, loc); | |
10516 | inserter->insert(temp); | |
10517 | this->string_ = Expression::make_temporary_reference(temp, loc); | |
10518 | } | |
10519 | if (!this->start_->is_variable()) | |
10520 | { | |
10521 | temp = Statement::make_temporary(NULL, this->start_, loc); | |
10522 | inserter->insert(temp); | |
10523 | this->start_ = Expression::make_temporary_reference(temp, loc); | |
10524 | } | |
10525 | if (this->end_ != NULL | |
10526 | && !this->end_->is_nil_expression() | |
10527 | && !this->end_->is_variable()) | |
10528 | { | |
10529 | temp = Statement::make_temporary(NULL, this->end_, loc); | |
10530 | inserter->insert(temp); | |
10531 | this->end_ = Expression::make_temporary_reference(temp, loc); | |
10532 | } | |
10533 | ||
10534 | return this; | |
10535 | } | |
10536 | ||
10537 | // Return the type of a string index. | |
10538 | ||
10539 | Type* | |
10540 | String_index_expression::do_type() | |
10541 | { | |
10542 | if (this->end_ == NULL) | |
10543 | return Type::lookup_integer_type("uint8"); | |
10544 | else | |
10545 | return this->string_->type(); | |
10546 | } | |
10547 | ||
10548 | // Determine the type of a string index. | |
10549 | ||
10550 | void | |
10551 | String_index_expression::do_determine_type(const Type_context*) | |
10552 | { | |
10553 | this->string_->determine_type_no_context(); | |
10554 | this->start_->determine_type_no_context(); | |
e440a328 | 10555 | if (this->end_ != NULL) |
93000773 | 10556 | this->end_->determine_type_no_context(); |
e440a328 | 10557 | } |
10558 | ||
10559 | // Check types of a string index. | |
10560 | ||
10561 | void | |
10562 | String_index_expression::do_check_types(Gogo*) | |
10563 | { | |
acdc230d | 10564 | Numeric_constant nc; |
10565 | unsigned long v; | |
10566 | if (this->start_->type()->integer_type() == NULL | |
10567 | && !this->start_->type()->is_error() | |
10568 | && (!this->start_->numeric_constant_value(&nc) | |
10569 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10570 | this->report_error(_("index must be integer")); |
10571 | if (this->end_ != NULL | |
10572 | && this->end_->type()->integer_type() == NULL | |
acdc230d | 10573 | && !this->end_->type()->is_error() |
10574 | && !this->end_->is_nil_expression() | |
10575 | && !this->end_->is_error_expression() | |
10576 | && (!this->end_->numeric_constant_value(&nc) | |
10577 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10578 | this->report_error(_("slice end must be integer")); |
10579 | ||
10580 | std::string sval; | |
10581 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10582 | ||
0c77715b | 10583 | Numeric_constant inc; |
e440a328 | 10584 | mpz_t ival; |
0bd5d859 | 10585 | bool ival_valid = false; |
0c77715b | 10586 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10587 | { |
0bd5d859 | 10588 | ival_valid = true; |
e440a328 | 10589 | if (mpz_sgn(ival) < 0 |
10590 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
10591 | { | |
10592 | error_at(this->start_->location(), "string index out of bounds"); | |
10593 | this->set_is_error(); | |
10594 | } | |
10595 | } | |
10596 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10597 | { | |
0c77715b | 10598 | Numeric_constant enc; |
10599 | mpz_t eval; | |
10600 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10601 | { |
0c77715b | 10602 | if (mpz_sgn(eval) < 0 |
10603 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10604 | { |
10605 | error_at(this->end_->location(), "string index out of bounds"); | |
10606 | this->set_is_error(); | |
10607 | } | |
0bd5d859 | 10608 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10609 | this->report_error(_("inverted slice range")); | |
0c77715b | 10610 | mpz_clear(eval); |
e440a328 | 10611 | } |
10612 | } | |
0bd5d859 | 10613 | if (ival_valid) |
10614 | mpz_clear(ival); | |
e440a328 | 10615 | } |
10616 | ||
10617 | // Get a tree for a string index. | |
10618 | ||
10619 | tree | |
10620 | String_index_expression::do_get_tree(Translate_context* context) | |
10621 | { | |
b13c66cd | 10622 | Location loc = this->location(); |
2c809f8f | 10623 | Expression* string_arg = this->string_; |
10624 | if (this->string_->type()->points_to() != NULL) | |
10625 | string_arg = Expression::make_unary(OPERATOR_MULT, this->string_, loc); | |
e440a328 | 10626 | |
2c809f8f | 10627 | Expression* bad_index = Expression::check_bounds(this->start_, loc); |
e440a328 | 10628 | |
2c809f8f | 10629 | int code = (this->end_ == NULL |
10630 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10631 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
e440a328 | 10632 | |
2c809f8f | 10633 | Gogo* gogo = context->gogo(); |
10634 | Bexpression* crash = | |
10635 | tree_to_expr(gogo->runtime_error(code, loc)->get_tree(context)); | |
1b1f2abf | 10636 | |
10637 | Type* int_type = Type::lookup_integer_type("int"); | |
e440a328 | 10638 | |
2c809f8f | 10639 | // It is possible that an error occurred earlier because the start index |
10640 | // cannot be represented as an integer type. In this case, we shouldn't | |
10641 | // try casting the starting index into an integer since | |
10642 | // Type_conversion_expression will fail to get the backend representation. | |
10643 | // FIXME. | |
10644 | if (this->start_->type()->integer_type() == NULL | |
10645 | && !Type::are_convertible(int_type, this->start_->type(), NULL)) | |
10646 | { | |
10647 | go_assert(saw_errors()); | |
10648 | return error_mark_node; | |
10649 | } | |
e440a328 | 10650 | |
2c809f8f | 10651 | Expression* start = Expression::make_cast(int_type, this->start_, loc); |
e440a328 | 10652 | |
2c809f8f | 10653 | if (this->end_ == NULL) |
10654 | { | |
10655 | Expression* length = | |
10656 | Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc); | |
e440a328 | 10657 | |
2c809f8f | 10658 | Expression* start_too_large = |
10659 | Expression::make_binary(OPERATOR_GE, start, length, loc); | |
10660 | bad_index = Expression::make_binary(OPERATOR_OROR, start_too_large, | |
10661 | bad_index, loc); | |
10662 | Expression* bytes = | |
10663 | Expression::make_string_info(this->string_, STRING_INFO_DATA, loc); | |
e440a328 | 10664 | |
2c809f8f | 10665 | Bexpression* bstart = tree_to_expr(start->get_tree(context)); |
10666 | Bexpression* ptr = tree_to_expr(bytes->get_tree(context)); | |
10667 | ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc); | |
9b27b43c | 10668 | Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo); |
10669 | Bexpression* index = | |
10670 | gogo->backend()->indirect_expression(ubtype, ptr, true, loc); | |
e440a328 | 10671 | |
2c809f8f | 10672 | Btype* byte_btype = bytes->type()->points_to()->get_backend(gogo); |
10673 | Bexpression* index_error = tree_to_expr(bad_index->get_tree(context)); | |
10674 | Bexpression* ret = | |
10675 | gogo->backend()->conditional_expression(byte_btype, index_error, | |
10676 | crash, index, loc); | |
10677 | return expr_to_tree(ret); | |
10678 | } | |
10679 | ||
10680 | Expression* end = NULL; | |
10681 | if (this->end_->is_nil_expression()) | |
e440a328 | 10682 | { |
2c809f8f | 10683 | mpz_t neg_one; |
10684 | mpz_init_set_si(neg_one, -1); | |
10685 | end = Expression::make_integer(&neg_one, int_type, loc); | |
10686 | mpz_clear(neg_one); | |
e440a328 | 10687 | } |
10688 | else | |
10689 | { | |
2c809f8f | 10690 | Expression* bounds_check = Expression::check_bounds(this->end_, loc); |
10691 | bad_index = | |
10692 | Expression::make_binary(OPERATOR_OROR, bounds_check, bad_index, loc); | |
10693 | end = Expression::make_cast(int_type, this->end_, loc); | |
e440a328 | 10694 | } |
2c809f8f | 10695 | |
10696 | Expression* strslice = Runtime::make_call(Runtime::STRING_SLICE, loc, 3, | |
10697 | string_arg, start, end); | |
10698 | Bexpression* bstrslice = tree_to_expr(strslice->get_tree(context)); | |
10699 | ||
10700 | Btype* str_btype = strslice->type()->get_backend(gogo); | |
10701 | Bexpression* index_error = tree_to_expr(bad_index->get_tree(context)); | |
10702 | Bexpression* ret = | |
10703 | gogo->backend()->conditional_expression(str_btype, index_error, | |
10704 | crash, bstrslice, loc); | |
10705 | return expr_to_tree(ret); | |
e440a328 | 10706 | } |
10707 | ||
d751bb78 | 10708 | // Dump ast representation for a string index expression. |
10709 | ||
10710 | void | |
10711 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10712 | const | |
10713 | { | |
acf2b673 | 10714 | Index_expression::dump_index_expression(ast_dump_context, this->string_, |
10715 | this->start_, this->end_, NULL); | |
d751bb78 | 10716 | } |
10717 | ||
e440a328 | 10718 | // Make a string index expression. END may be NULL. |
10719 | ||
10720 | Expression* | |
10721 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10722 | Expression* end, Location location) |
e440a328 | 10723 | { |
10724 | return new String_index_expression(string, start, end, location); | |
10725 | } | |
10726 | ||
10727 | // Class Map_index. | |
10728 | ||
10729 | // Get the type of the map. | |
10730 | ||
10731 | Map_type* | |
10732 | Map_index_expression::get_map_type() const | |
10733 | { | |
10734 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 10735 | if (mt == NULL) |
c484d925 | 10736 | go_assert(saw_errors()); |
e440a328 | 10737 | return mt; |
10738 | } | |
10739 | ||
10740 | // Map index traversal. | |
10741 | ||
10742 | int | |
10743 | Map_index_expression::do_traverse(Traverse* traverse) | |
10744 | { | |
10745 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
10746 | return TRAVERSE_EXIT; | |
10747 | return Expression::traverse(&this->index_, traverse); | |
10748 | } | |
10749 | ||
2c809f8f | 10750 | // We need to pass in a pointer to the key, so flatten the index into a |
10751 | // temporary variable if it isn't already. The value pointer will be | |
10752 | // dereferenced and checked for nil, so flatten into a temporary to avoid | |
10753 | // recomputation. | |
10754 | ||
10755 | Expression* | |
10756 | Map_index_expression::do_flatten(Gogo*, Named_object*, | |
10757 | Statement_inserter* inserter) | |
10758 | { | |
10759 | Map_type* mt = this->get_map_type(); | |
10760 | if (this->index_->type() != mt->key_type()) | |
10761 | this->index_ = Expression::make_cast(mt->key_type(), this->index_, | |
10762 | this->location()); | |
10763 | ||
10764 | if (!this->index_->is_variable()) | |
10765 | { | |
10766 | Temporary_statement* temp = Statement::make_temporary(NULL, this->index_, | |
10767 | this->location()); | |
10768 | inserter->insert(temp); | |
10769 | this->index_ = Expression::make_temporary_reference(temp, | |
10770 | this->location()); | |
10771 | } | |
10772 | ||
10773 | if (this->value_pointer_ == NULL) | |
10774 | this->get_value_pointer(this->is_lvalue_); | |
10775 | if (!this->value_pointer_->is_variable()) | |
10776 | { | |
10777 | Temporary_statement* temp = | |
10778 | Statement::make_temporary(NULL, this->value_pointer_, | |
10779 | this->location()); | |
10780 | inserter->insert(temp); | |
10781 | this->value_pointer_ = | |
10782 | Expression::make_temporary_reference(temp, this->location()); | |
10783 | } | |
10784 | ||
10785 | return this; | |
10786 | } | |
10787 | ||
e440a328 | 10788 | // Return the type of a map index. |
10789 | ||
10790 | Type* | |
10791 | Map_index_expression::do_type() | |
10792 | { | |
c7524fae | 10793 | Map_type* mt = this->get_map_type(); |
10794 | if (mt == NULL) | |
10795 | return Type::make_error_type(); | |
10796 | Type* type = mt->val_type(); | |
e440a328 | 10797 | // If this map index is in a tuple assignment, we actually return a |
10798 | // pointer to the value type. Tuple_map_assignment_statement is | |
10799 | // responsible for handling this correctly. We need to get the type | |
10800 | // right in case this gets assigned to a temporary variable. | |
10801 | if (this->is_in_tuple_assignment_) | |
10802 | type = Type::make_pointer_type(type); | |
10803 | return type; | |
10804 | } | |
10805 | ||
10806 | // Fix the type of a map index. | |
10807 | ||
10808 | void | |
10809 | Map_index_expression::do_determine_type(const Type_context*) | |
10810 | { | |
10811 | this->map_->determine_type_no_context(); | |
c7524fae | 10812 | Map_type* mt = this->get_map_type(); |
10813 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
10814 | Type_context subcontext(key_type, false); | |
e440a328 | 10815 | this->index_->determine_type(&subcontext); |
10816 | } | |
10817 | ||
10818 | // Check types of a map index. | |
10819 | ||
10820 | void | |
10821 | Map_index_expression::do_check_types(Gogo*) | |
10822 | { | |
10823 | std::string reason; | |
c7524fae | 10824 | Map_type* mt = this->get_map_type(); |
10825 | if (mt == NULL) | |
10826 | return; | |
10827 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 10828 | { |
10829 | if (reason.empty()) | |
10830 | this->report_error(_("incompatible type for map index")); | |
10831 | else | |
10832 | { | |
10833 | error_at(this->location(), "incompatible type for map index (%s)", | |
10834 | reason.c_str()); | |
10835 | this->set_is_error(); | |
10836 | } | |
10837 | } | |
10838 | } | |
10839 | ||
10840 | // Get a tree for a map index. | |
10841 | ||
10842 | tree | |
10843 | Map_index_expression::do_get_tree(Translate_context* context) | |
10844 | { | |
10845 | Map_type* type = this->get_map_type(); | |
c7524fae | 10846 | if (type == NULL) |
2c809f8f | 10847 | { |
10848 | go_assert(saw_errors()); | |
10849 | return error_mark_node; | |
10850 | } | |
e440a328 | 10851 | |
2c809f8f | 10852 | go_assert(this->value_pointer_ != NULL |
10853 | && this->value_pointer_->is_variable()); | |
e440a328 | 10854 | |
2c809f8f | 10855 | Bexpression* ret; |
e440a328 | 10856 | if (this->is_lvalue_) |
2c809f8f | 10857 | { |
10858 | Expression* val = | |
10859 | Expression::make_unary(OPERATOR_MULT, this->value_pointer_, | |
10860 | this->location()); | |
10861 | ret = tree_to_expr(val->get_tree(context)); | |
10862 | } | |
e440a328 | 10863 | else if (this->is_in_tuple_assignment_) |
10864 | { | |
10865 | // Tuple_map_assignment_statement is responsible for using this | |
10866 | // appropriately. | |
2c809f8f | 10867 | ret = tree_to_expr(this->value_pointer_->get_tree(context)); |
e440a328 | 10868 | } |
10869 | else | |
10870 | { | |
2c809f8f | 10871 | Location loc = this->location(); |
10872 | ||
10873 | Expression* nil_check = | |
10874 | Expression::make_binary(OPERATOR_EQEQ, this->value_pointer_, | |
10875 | Expression::make_nil(loc), loc); | |
10876 | Bexpression* bnil_check = tree_to_expr(nil_check->get_tree(context)); | |
10877 | Expression* val = | |
10878 | Expression::make_unary(OPERATOR_MULT, this->value_pointer_, loc); | |
10879 | Bexpression* bval = tree_to_expr(val->get_tree(context)); | |
10880 | ||
63697958 | 10881 | Gogo* gogo = context->gogo(); |
10882 | Btype* val_btype = type->val_type()->get_backend(gogo); | |
10883 | Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); | |
2c809f8f | 10884 | ret = gogo->backend()->conditional_expression(val_btype, bnil_check, |
10885 | val_zero, bval, loc); | |
e440a328 | 10886 | } |
2c809f8f | 10887 | |
10888 | return expr_to_tree(ret); | |
e440a328 | 10889 | } |
10890 | ||
2c809f8f | 10891 | // Get an expression for the map index. This returns an expression which |
10892 | // evaluates to a pointer to a value. The pointer will be NULL if the key is | |
e440a328 | 10893 | // not in the map. |
10894 | ||
2c809f8f | 10895 | Expression* |
10896 | Map_index_expression::get_value_pointer(bool insert) | |
e440a328 | 10897 | { |
2c809f8f | 10898 | if (this->value_pointer_ == NULL) |
746d2e73 | 10899 | { |
2c809f8f | 10900 | Map_type* type = this->get_map_type(); |
10901 | if (type == NULL) | |
746d2e73 | 10902 | { |
2c809f8f | 10903 | go_assert(saw_errors()); |
10904 | return Expression::make_error(this->location()); | |
746d2e73 | 10905 | } |
e440a328 | 10906 | |
2c809f8f | 10907 | Location loc = this->location(); |
10908 | Expression* map_ref = this->map_; | |
10909 | if (this->map_->type()->points_to() != NULL) | |
10910 | map_ref = Expression::make_unary(OPERATOR_MULT, map_ref, loc); | |
e440a328 | 10911 | |
2c809f8f | 10912 | Expression* index_ptr = Expression::make_unary(OPERATOR_AND, this->index_, |
10913 | loc); | |
10914 | Expression* map_index = | |
10915 | Runtime::make_call(Runtime::MAP_INDEX, loc, 3, | |
10916 | map_ref, index_ptr, | |
10917 | Expression::make_boolean(insert, loc)); | |
10918 | ||
10919 | Type* val_type = type->val_type(); | |
10920 | this->value_pointer_ = | |
10921 | Expression::make_unsafe_cast(Type::make_pointer_type(val_type), | |
10922 | map_index, this->location()); | |
10923 | } | |
10924 | return this->value_pointer_; | |
e440a328 | 10925 | } |
10926 | ||
d751bb78 | 10927 | // Dump ast representation for a map index expression |
10928 | ||
10929 | void | |
10930 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10931 | const | |
10932 | { | |
acf2b673 | 10933 | Index_expression::dump_index_expression(ast_dump_context, this->map_, |
10934 | this->index_, NULL, NULL); | |
d751bb78 | 10935 | } |
10936 | ||
e440a328 | 10937 | // Make a map index expression. |
10938 | ||
10939 | Map_index_expression* | |
10940 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 10941 | Location location) |
e440a328 | 10942 | { |
10943 | return new Map_index_expression(map, index, location); | |
10944 | } | |
10945 | ||
10946 | // Class Field_reference_expression. | |
10947 | ||
149eabc5 | 10948 | // Lower a field reference expression. There is nothing to lower, but |
10949 | // this is where we generate the tracking information for fields with | |
10950 | // the magic go:"track" tag. | |
10951 | ||
10952 | Expression* | |
10953 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
10954 | Statement_inserter* inserter, int) | |
10955 | { | |
10956 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
10957 | if (struct_type == NULL) | |
10958 | { | |
10959 | // Error will be reported elsewhere. | |
10960 | return this; | |
10961 | } | |
10962 | const Struct_field* field = struct_type->field(this->field_index_); | |
10963 | if (field == NULL) | |
10964 | return this; | |
10965 | if (!field->has_tag()) | |
10966 | return this; | |
10967 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
10968 | return this; | |
10969 | ||
10970 | // We have found a reference to a tracked field. Build a call to | |
10971 | // the runtime function __go_fieldtrack with a string that describes | |
10972 | // the field. FIXME: We should only call this once per referenced | |
10973 | // field per function, not once for each reference to the field. | |
10974 | ||
10975 | if (this->called_fieldtrack_) | |
10976 | return this; | |
10977 | this->called_fieldtrack_ = true; | |
10978 | ||
10979 | Location loc = this->location(); | |
10980 | ||
10981 | std::string s = "fieldtrack \""; | |
10982 | Named_type* nt = this->expr_->type()->named_type(); | |
10983 | if (nt == NULL || nt->named_object()->package() == NULL) | |
10984 | s.append(gogo->pkgpath()); | |
10985 | else | |
10986 | s.append(nt->named_object()->package()->pkgpath()); | |
10987 | s.push_back('.'); | |
10988 | if (nt != NULL) | |
5c29ad36 | 10989 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 10990 | s.push_back('.'); |
10991 | s.append(field->field_name()); | |
10992 | s.push_back('"'); | |
10993 | ||
10994 | // We can't use a string here, because internally a string holds a | |
10995 | // pointer to the actual bytes; when the linker garbage collects the | |
10996 | // string, it won't garbage collect the bytes. So we use a | |
10997 | // [...]byte. | |
10998 | ||
10999 | mpz_t val; | |
11000 | mpz_init_set_ui(val, s.length()); | |
11001 | Expression* length_expr = Expression::make_integer(&val, NULL, loc); | |
11002 | mpz_clear(val); | |
11003 | ||
11004 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
11005 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
11006 | ||
11007 | Expression_list* bytes = new Expression_list(); | |
11008 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11009 | { | |
11010 | mpz_init_set_ui(val, *p); | |
11011 | Expression* byte = Expression::make_integer(&val, NULL, loc); | |
11012 | mpz_clear(val); | |
11013 | bytes->push_back(byte); | |
11014 | } | |
11015 | ||
11016 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11017 | bytes, false, loc); |
149eabc5 | 11018 | |
11019 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11020 | ||
11021 | static int count; | |
11022 | char buf[50]; | |
11023 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11024 | ++count; | |
11025 | ||
11026 | Named_object* no = gogo->add_variable(buf, var); | |
11027 | e = Expression::make_var_reference(no, loc); | |
11028 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11029 | ||
11030 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
11031 | inserter->insert(Statement::make_statement(call, false)); | |
11032 | ||
11033 | // Put this function, and the global variable we just created, into | |
11034 | // unique sections. This will permit the linker to garbage collect | |
11035 | // them if they are not referenced. The effect is that the only | |
11036 | // strings, indicating field references, that will wind up in the | |
11037 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11038 | if (function != NULL) |
11039 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11040 | var->set_in_unique_section(); |
11041 | ||
11042 | return this; | |
11043 | } | |
11044 | ||
e440a328 | 11045 | // Return the type of a field reference. |
11046 | ||
11047 | Type* | |
11048 | Field_reference_expression::do_type() | |
11049 | { | |
b0e628fb | 11050 | Type* type = this->expr_->type(); |
5c13bd80 | 11051 | if (type->is_error()) |
b0e628fb | 11052 | return type; |
11053 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11054 | go_assert(struct_type != NULL); |
e440a328 | 11055 | return struct_type->field(this->field_index_)->type(); |
11056 | } | |
11057 | ||
11058 | // Check the types for a field reference. | |
11059 | ||
11060 | void | |
11061 | Field_reference_expression::do_check_types(Gogo*) | |
11062 | { | |
b0e628fb | 11063 | Type* type = this->expr_->type(); |
5c13bd80 | 11064 | if (type->is_error()) |
b0e628fb | 11065 | return; |
11066 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11067 | go_assert(struct_type != NULL); |
11068 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11069 | } |
11070 | ||
11071 | // Get a tree for a field reference. | |
11072 | ||
11073 | tree | |
11074 | Field_reference_expression::do_get_tree(Translate_context* context) | |
11075 | { | |
fbb851c5 | 11076 | Bexpression* bstruct = tree_to_expr(this->expr_->get_tree(context)); |
11077 | Bexpression* ret = | |
11078 | context->gogo()->backend()->struct_field_expression(bstruct, | |
11079 | this->field_index_, | |
11080 | this->location()); | |
11081 | return expr_to_tree(ret); | |
e440a328 | 11082 | } |
11083 | ||
d751bb78 | 11084 | // Dump ast representation for a field reference expression. |
11085 | ||
11086 | void | |
11087 | Field_reference_expression::do_dump_expression( | |
11088 | Ast_dump_context* ast_dump_context) const | |
11089 | { | |
11090 | this->expr_->dump_expression(ast_dump_context); | |
11091 | ast_dump_context->ostream() << "." << this->field_index_; | |
11092 | } | |
11093 | ||
e440a328 | 11094 | // Make a reference to a qualified identifier in an expression. |
11095 | ||
11096 | Field_reference_expression* | |
11097 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11098 | Location location) |
e440a328 | 11099 | { |
11100 | return new Field_reference_expression(expr, field_index, location); | |
11101 | } | |
11102 | ||
11103 | // Class Interface_field_reference_expression. | |
11104 | ||
2387f644 | 11105 | // Return an expression for the pointer to the function to call. |
e440a328 | 11106 | |
2387f644 | 11107 | Expression* |
11108 | Interface_field_reference_expression::get_function() | |
e440a328 | 11109 | { |
2387f644 | 11110 | Expression* ref = this->expr_; |
11111 | Location loc = this->location(); | |
11112 | if (ref->type()->points_to() != NULL) | |
11113 | ref = Expression::make_unary(OPERATOR_MULT, ref, loc); | |
e440a328 | 11114 | |
2387f644 | 11115 | Expression* mtable = |
11116 | Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc); | |
11117 | Struct_type* mtable_type = mtable->type()->points_to()->struct_type(); | |
e440a328 | 11118 | |
11119 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
2387f644 | 11120 | unsigned int index; |
11121 | const Struct_field* field = mtable_type->find_local_field(name, &index); | |
11122 | go_assert(field != NULL); | |
11123 | mtable = Expression::make_unary(OPERATOR_MULT, mtable, loc); | |
11124 | return Expression::make_field_reference(mtable, index, loc); | |
e440a328 | 11125 | } |
11126 | ||
2387f644 | 11127 | // Return an expression for the first argument to pass to the interface |
e440a328 | 11128 | // function. |
11129 | ||
2387f644 | 11130 | Expression* |
11131 | Interface_field_reference_expression::get_underlying_object() | |
e440a328 | 11132 | { |
2387f644 | 11133 | Expression* expr = this->expr_; |
11134 | if (expr->type()->points_to() != NULL) | |
11135 | expr = Expression::make_unary(OPERATOR_MULT, expr, this->location()); | |
11136 | return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT, | |
11137 | this->location()); | |
e440a328 | 11138 | } |
11139 | ||
11140 | // Traversal. | |
11141 | ||
11142 | int | |
11143 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11144 | { | |
11145 | return Expression::traverse(&this->expr_, traverse); | |
11146 | } | |
11147 | ||
0afbb937 | 11148 | // Lower the expression. If this expression is not called, we need to |
11149 | // evaluate the expression twice when converting to the backend | |
11150 | // interface. So introduce a temporary variable if necessary. | |
11151 | ||
11152 | Expression* | |
11153 | Interface_field_reference_expression::do_lower(Gogo*, Named_object*, | |
11154 | Statement_inserter* inserter, | |
11155 | int) | |
11156 | { | |
2387f644 | 11157 | if (!this->expr_->is_variable()) |
0afbb937 | 11158 | { |
11159 | Temporary_statement* temp = | |
11160 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11161 | inserter->insert(temp); | |
11162 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11163 | this->location()); | |
11164 | } | |
11165 | return this; | |
11166 | } | |
11167 | ||
e440a328 | 11168 | // Return the type of an interface field reference. |
11169 | ||
11170 | Type* | |
11171 | Interface_field_reference_expression::do_type() | |
11172 | { | |
11173 | Type* expr_type = this->expr_->type(); | |
11174 | ||
11175 | Type* points_to = expr_type->points_to(); | |
11176 | if (points_to != NULL) | |
11177 | expr_type = points_to; | |
11178 | ||
11179 | Interface_type* interface_type = expr_type->interface_type(); | |
11180 | if (interface_type == NULL) | |
11181 | return Type::make_error_type(); | |
11182 | ||
11183 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11184 | if (method == NULL) | |
11185 | return Type::make_error_type(); | |
11186 | ||
11187 | return method->type(); | |
11188 | } | |
11189 | ||
11190 | // Determine types. | |
11191 | ||
11192 | void | |
11193 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11194 | { | |
11195 | this->expr_->determine_type_no_context(); | |
11196 | } | |
11197 | ||
11198 | // Check the types for an interface field reference. | |
11199 | ||
11200 | void | |
11201 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11202 | { | |
11203 | Type* type = this->expr_->type(); | |
11204 | ||
11205 | Type* points_to = type->points_to(); | |
11206 | if (points_to != NULL) | |
11207 | type = points_to; | |
11208 | ||
11209 | Interface_type* interface_type = type->interface_type(); | |
11210 | if (interface_type == NULL) | |
5c491127 | 11211 | { |
11212 | if (!type->is_error_type()) | |
11213 | this->report_error(_("expected interface or pointer to interface")); | |
11214 | } | |
e440a328 | 11215 | else |
11216 | { | |
11217 | const Typed_identifier* method = | |
11218 | interface_type->find_method(this->name_); | |
11219 | if (method == NULL) | |
11220 | { | |
11221 | error_at(this->location(), "method %qs not in interface", | |
11222 | Gogo::message_name(this->name_).c_str()); | |
11223 | this->set_is_error(); | |
11224 | } | |
11225 | } | |
11226 | } | |
11227 | ||
0afbb937 | 11228 | // If an interface field reference is not simply called, then it is |
11229 | // represented as a closure. The closure will hold a single variable, | |
11230 | // the value of the interface on which the method should be called. | |
11231 | // The function will be a simple thunk that pulls the value from the | |
11232 | // closure and calls the method with the remaining arguments. | |
11233 | ||
11234 | // Because method values are not common, we don't build all thunks for | |
11235 | // all possible interface methods, but instead only build them as we | |
11236 | // need them. In particular, we even build them on demand for | |
11237 | // interface methods defined in other packages. | |
11238 | ||
11239 | Interface_field_reference_expression::Interface_method_thunks | |
11240 | Interface_field_reference_expression::interface_method_thunks; | |
11241 | ||
11242 | // Find or create the thunk to call method NAME on TYPE. | |
11243 | ||
11244 | Named_object* | |
11245 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11246 | Interface_type* type, | |
11247 | const std::string& name) | |
11248 | { | |
11249 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11250 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11251 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11252 | if (ins.second) | |
11253 | { | |
11254 | // This is the first time we have seen this interface. | |
11255 | ins.first->second = new Method_thunks(); | |
11256 | } | |
11257 | ||
11258 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11259 | p != ins.first->second->end(); | |
11260 | p++) | |
11261 | if (p->first == name) | |
11262 | return p->second; | |
11263 | ||
11264 | Location loc = type->location(); | |
11265 | ||
11266 | const Typed_identifier* method_id = type->find_method(name); | |
11267 | if (method_id == NULL) | |
11268 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11269 | ||
11270 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11271 | if (orig_fntype == NULL) | |
11272 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11273 | ||
11274 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11275 | // The type here is wrong--it should be the C function type. But it |
11276 | // doesn't really matter. | |
0afbb937 | 11277 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11278 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11279 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
11280 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
11281 | closure_type = Type::make_pointer_type(closure_type); | |
11282 | ||
f8bdf81a | 11283 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11284 | |
11285 | Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, | |
11286 | false, loc); | |
11287 | ||
f8bdf81a | 11288 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11289 | cvar->set_is_used(); | |
11290 | Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); | |
11291 | new_no->func_value()->set_closure_var(cp); | |
0afbb937 | 11292 | |
f8bdf81a | 11293 | gogo->start_block(loc); |
0afbb937 | 11294 | |
11295 | // Field 0 of the closure is the function code pointer, field 1 is | |
11296 | // the value on which to invoke the method. | |
11297 | Expression* arg = Expression::make_var_reference(cp, loc); | |
11298 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
11299 | arg = Expression::make_field_reference(arg, 1, loc); | |
11300 | ||
11301 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11302 | loc); | |
11303 | ||
11304 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11305 | Expression_list* args; | |
11306 | if (orig_params == NULL || orig_params->empty()) | |
11307 | args = NULL; | |
11308 | else | |
11309 | { | |
11310 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11311 | args = new Expression_list(); | |
11312 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11313 | p != new_params->end(); |
0afbb937 | 11314 | ++p) |
11315 | { | |
11316 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11317 | go_assert(p_no != NULL | |
11318 | && p_no->is_variable() | |
11319 | && p_no->var_value()->is_parameter()); | |
11320 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11321 | } | |
11322 | } | |
11323 | ||
11324 | Call_expression* call = Expression::make_call(ifre, args, | |
11325 | orig_fntype->is_varargs(), | |
11326 | loc); | |
11327 | call->set_varargs_are_lowered(); | |
11328 | ||
11329 | Statement* s = Statement::make_return_from_call(call, loc); | |
11330 | gogo->add_statement(s); | |
11331 | Block* b = gogo->finish_block(loc); | |
11332 | gogo->add_block(b, loc); | |
11333 | gogo->lower_block(new_no, b); | |
a32698ee | 11334 | gogo->flatten_block(new_no, b); |
0afbb937 | 11335 | gogo->finish_function(loc); |
11336 | ||
11337 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11338 | return new_no; | |
11339 | } | |
11340 | ||
11341 | // Get a tree for a method value. | |
e440a328 | 11342 | |
11343 | tree | |
0afbb937 | 11344 | Interface_field_reference_expression::do_get_tree(Translate_context* context) |
e440a328 | 11345 | { |
0afbb937 | 11346 | Interface_type* type = this->expr_->type()->interface_type(); |
11347 | if (type == NULL) | |
11348 | { | |
11349 | go_assert(saw_errors()); | |
11350 | return error_mark_node; | |
11351 | } | |
11352 | ||
11353 | Named_object* thunk = | |
11354 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11355 | type, this->name_); | |
11356 | if (thunk->is_erroneous()) | |
11357 | { | |
11358 | go_assert(saw_errors()); | |
11359 | return error_mark_node; | |
11360 | } | |
11361 | ||
11362 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11363 | // the lowering pass because at that point we don't know whether we | |
11364 | // need to create the thunk or not. If the expression is called, we | |
11365 | // don't need the thunk. | |
11366 | ||
11367 | Location loc = this->location(); | |
11368 | ||
11369 | Struct_field_list* fields = new Struct_field_list(); | |
11370 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11371 | thunk->func_value()->type(), | |
11372 | loc))); | |
11373 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11374 | this->expr_->type(), | |
11375 | loc))); | |
11376 | Struct_type* st = Type::make_struct_type(fields, loc); | |
11377 | ||
11378 | Expression_list* vals = new Expression_list(); | |
11379 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
11380 | vals->push_back(this->expr_); | |
11381 | ||
11382 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
2c809f8f | 11383 | expr = Expression::make_heap_expression(expr, loc); |
0afbb937 | 11384 | |
2387f644 | 11385 | Bexpression* bclosure = tree_to_expr(expr->get_tree(context)); |
11386 | Expression* nil_check = | |
11387 | Expression::make_binary(OPERATOR_EQEQ, this->expr_, | |
11388 | Expression::make_nil(loc), loc); | |
11389 | Bexpression* bnil_check = tree_to_expr(nil_check->get_tree(context)); | |
0afbb937 | 11390 | |
2387f644 | 11391 | Gogo* gogo = context->gogo(); |
11392 | Expression* crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, loc); | |
11393 | Bexpression* bcrash = tree_to_expr(crash->get_tree(context)); | |
11394 | ||
11395 | Bexpression* bcond = | |
a32698ee | 11396 | gogo->backend()->conditional_expression(NULL, bnil_check, bcrash, NULL, loc); |
2387f644 | 11397 | Bstatement* cond_statement = gogo->backend()->expression_statement(bcond); |
11398 | Bexpression* ret = | |
11399 | gogo->backend()->compound_expression(cond_statement, bclosure, loc); | |
11400 | return expr_to_tree(ret); | |
e440a328 | 11401 | } |
11402 | ||
d751bb78 | 11403 | // Dump ast representation for an interface field reference. |
11404 | ||
11405 | void | |
11406 | Interface_field_reference_expression::do_dump_expression( | |
11407 | Ast_dump_context* ast_dump_context) const | |
11408 | { | |
11409 | this->expr_->dump_expression(ast_dump_context); | |
11410 | ast_dump_context->ostream() << "." << this->name_; | |
11411 | } | |
11412 | ||
e440a328 | 11413 | // Make a reference to a field in an interface. |
11414 | ||
11415 | Expression* | |
11416 | Expression::make_interface_field_reference(Expression* expr, | |
11417 | const std::string& field, | |
b13c66cd | 11418 | Location location) |
e440a328 | 11419 | { |
11420 | return new Interface_field_reference_expression(expr, field, location); | |
11421 | } | |
11422 | ||
11423 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
11424 | // is lowered after we know the type of the left hand side. | |
11425 | ||
11426 | class Selector_expression : public Parser_expression | |
11427 | { | |
11428 | public: | |
11429 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 11430 | Location location) |
e440a328 | 11431 | : Parser_expression(EXPRESSION_SELECTOR, location), |
11432 | left_(left), name_(name) | |
11433 | { } | |
11434 | ||
11435 | protected: | |
11436 | int | |
11437 | do_traverse(Traverse* traverse) | |
11438 | { return Expression::traverse(&this->left_, traverse); } | |
11439 | ||
11440 | Expression* | |
ceeb4318 | 11441 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 11442 | |
11443 | Expression* | |
11444 | do_copy() | |
11445 | { | |
11446 | return new Selector_expression(this->left_->copy(), this->name_, | |
11447 | this->location()); | |
11448 | } | |
11449 | ||
d751bb78 | 11450 | void |
11451 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
11452 | ||
e440a328 | 11453 | private: |
11454 | Expression* | |
11455 | lower_method_expression(Gogo*); | |
11456 | ||
11457 | // The expression on the left hand side. | |
11458 | Expression* left_; | |
11459 | // The name on the right hand side. | |
11460 | std::string name_; | |
11461 | }; | |
11462 | ||
11463 | // Lower a selector expression once we know the real type of the left | |
11464 | // hand side. | |
11465 | ||
11466 | Expression* | |
ceeb4318 | 11467 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
11468 | int) | |
e440a328 | 11469 | { |
11470 | Expression* left = this->left_; | |
11471 | if (left->is_type_expression()) | |
11472 | return this->lower_method_expression(gogo); | |
11473 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
11474 | this->location()); | |
11475 | } | |
11476 | ||
11477 | // Lower a method expression T.M or (*T).M. We turn this into a | |
11478 | // function literal. | |
11479 | ||
11480 | Expression* | |
11481 | Selector_expression::lower_method_expression(Gogo* gogo) | |
11482 | { | |
b13c66cd | 11483 | Location location = this->location(); |
e440a328 | 11484 | Type* type = this->left_->type(); |
11485 | const std::string& name(this->name_); | |
11486 | ||
11487 | bool is_pointer; | |
11488 | if (type->points_to() == NULL) | |
11489 | is_pointer = false; | |
11490 | else | |
11491 | { | |
11492 | is_pointer = true; | |
11493 | type = type->points_to(); | |
11494 | } | |
11495 | Named_type* nt = type->named_type(); | |
11496 | if (nt == NULL) | |
11497 | { | |
11498 | error_at(location, | |
11499 | ("method expression requires named type or " | |
11500 | "pointer to named type")); | |
11501 | return Expression::make_error(location); | |
11502 | } | |
11503 | ||
11504 | bool is_ambiguous; | |
11505 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 11506 | const Typed_identifier* imethod = NULL; |
dcc8506b | 11507 | if (method == NULL && !is_pointer) |
ab1468c3 | 11508 | { |
11509 | Interface_type* it = nt->interface_type(); | |
11510 | if (it != NULL) | |
11511 | imethod = it->find_method(name); | |
11512 | } | |
11513 | ||
11514 | if (method == NULL && imethod == NULL) | |
e440a328 | 11515 | { |
11516 | if (!is_ambiguous) | |
dcc8506b | 11517 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
11518 | is_pointer ? "*" : "", | |
e440a328 | 11519 | nt->message_name().c_str(), |
11520 | Gogo::message_name(name).c_str()); | |
11521 | else | |
dcc8506b | 11522 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 11523 | Gogo::message_name(name).c_str(), |
dcc8506b | 11524 | is_pointer ? "*" : "", |
e440a328 | 11525 | nt->message_name().c_str()); |
11526 | return Expression::make_error(location); | |
11527 | } | |
11528 | ||
ab1468c3 | 11529 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 11530 | { |
11531 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
11532 | nt->message_name().c_str(), | |
11533 | Gogo::message_name(name).c_str()); | |
11534 | return Expression::make_error(location); | |
11535 | } | |
11536 | ||
11537 | // Build a new function type in which the receiver becomes the first | |
11538 | // argument. | |
ab1468c3 | 11539 | Function_type* method_type; |
11540 | if (method != NULL) | |
11541 | { | |
11542 | method_type = method->type(); | |
c484d925 | 11543 | go_assert(method_type->is_method()); |
ab1468c3 | 11544 | } |
11545 | else | |
11546 | { | |
11547 | method_type = imethod->type()->function_type(); | |
c484d925 | 11548 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 11549 | } |
e440a328 | 11550 | |
11551 | const char* const receiver_name = "$this"; | |
11552 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
11553 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
11554 | location)); | |
11555 | ||
11556 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
11557 | if (method_parameters != NULL) | |
11558 | { | |
f470da59 | 11559 | int i = 0; |
e440a328 | 11560 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
11561 | p != method_parameters->end(); | |
f470da59 | 11562 | ++p, ++i) |
11563 | { | |
68883531 | 11564 | if (!p->name().empty()) |
f470da59 | 11565 | parameters->push_back(*p); |
11566 | else | |
11567 | { | |
11568 | char buf[20]; | |
11569 | snprintf(buf, sizeof buf, "$param%d", i); | |
11570 | parameters->push_back(Typed_identifier(buf, p->type(), | |
11571 | p->location())); | |
11572 | } | |
11573 | } | |
e440a328 | 11574 | } |
11575 | ||
11576 | const Typed_identifier_list* method_results = method_type->results(); | |
11577 | Typed_identifier_list* results; | |
11578 | if (method_results == NULL) | |
11579 | results = NULL; | |
11580 | else | |
11581 | { | |
11582 | results = new Typed_identifier_list(); | |
11583 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
11584 | p != method_results->end(); | |
11585 | ++p) | |
11586 | results->push_back(*p); | |
11587 | } | |
11588 | ||
11589 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
11590 | location); | |
11591 | if (method_type->is_varargs()) | |
11592 | fntype->set_is_varargs(); | |
11593 | ||
11594 | // We generate methods which always takes a pointer to the receiver | |
11595 | // as their first argument. If this is for a pointer type, we can | |
11596 | // simply reuse the existing function. We use an internal hack to | |
11597 | // get the right type. | |
8381eda7 | 11598 | // FIXME: This optimization is disabled because it doesn't yet work |
11599 | // with function descriptors when the method expression is not | |
11600 | // directly called. | |
11601 | if (method != NULL && is_pointer && false) | |
e440a328 | 11602 | { |
11603 | Named_object* mno = (method->needs_stub_method() | |
11604 | ? method->stub_object() | |
11605 | : method->named_object()); | |
11606 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
11607 | f = Expression::make_cast(fntype, f, location); | |
11608 | Type_conversion_expression* tce = | |
11609 | static_cast<Type_conversion_expression*>(f); | |
11610 | tce->set_may_convert_function_types(); | |
11611 | return f; | |
11612 | } | |
11613 | ||
11614 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
11615 | location); | |
11616 | ||
11617 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 11618 | go_assert(vno != NULL); |
e440a328 | 11619 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 11620 | Expression* bm; |
11621 | if (method != NULL) | |
11622 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
11623 | else | |
11624 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 11625 | |
11626 | // Even though we found the method above, if it has an error type we | |
11627 | // may see an error here. | |
11628 | if (bm->is_error_expression()) | |
463fe805 | 11629 | { |
11630 | gogo->finish_function(location); | |
11631 | return bm; | |
11632 | } | |
e440a328 | 11633 | |
11634 | Expression_list* args; | |
f470da59 | 11635 | if (parameters->size() <= 1) |
e440a328 | 11636 | args = NULL; |
11637 | else | |
11638 | { | |
11639 | args = new Expression_list(); | |
f470da59 | 11640 | Typed_identifier_list::const_iterator p = parameters->begin(); |
11641 | ++p; | |
11642 | for (; p != parameters->end(); ++p) | |
e440a328 | 11643 | { |
11644 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 11645 | go_assert(vno != NULL); |
e440a328 | 11646 | args->push_back(Expression::make_var_reference(vno, location)); |
11647 | } | |
11648 | } | |
11649 | ||
ceeb4318 | 11650 | gogo->start_block(location); |
11651 | ||
e440a328 | 11652 | Call_expression* call = Expression::make_call(bm, args, |
11653 | method_type->is_varargs(), | |
11654 | location); | |
11655 | ||
0afbb937 | 11656 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 11657 | gogo->add_statement(s); |
11658 | ||
ceeb4318 | 11659 | Block* b = gogo->finish_block(location); |
11660 | ||
11661 | gogo->add_block(b, location); | |
11662 | ||
11663 | // Lower the call in case there are multiple results. | |
11664 | gogo->lower_block(no, b); | |
a32698ee | 11665 | gogo->flatten_block(no, b); |
ceeb4318 | 11666 | |
e440a328 | 11667 | gogo->finish_function(location); |
11668 | ||
11669 | return Expression::make_func_reference(no, NULL, location); | |
11670 | } | |
11671 | ||
d751bb78 | 11672 | // Dump the ast for a selector expression. |
11673 | ||
11674 | void | |
11675 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11676 | const | |
11677 | { | |
11678 | ast_dump_context->dump_expression(this->left_); | |
11679 | ast_dump_context->ostream() << "."; | |
11680 | ast_dump_context->ostream() << this->name_; | |
11681 | } | |
11682 | ||
e440a328 | 11683 | // Make a selector expression. |
11684 | ||
11685 | Expression* | |
11686 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 11687 | Location location) |
e440a328 | 11688 | { |
11689 | return new Selector_expression(left, name, location); | |
11690 | } | |
11691 | ||
11692 | // Implement the builtin function new. | |
11693 | ||
11694 | class Allocation_expression : public Expression | |
11695 | { | |
11696 | public: | |
b13c66cd | 11697 | Allocation_expression(Type* type, Location location) |
e440a328 | 11698 | : Expression(EXPRESSION_ALLOCATION, location), |
11699 | type_(type) | |
11700 | { } | |
11701 | ||
11702 | protected: | |
11703 | int | |
11704 | do_traverse(Traverse* traverse) | |
11705 | { return Type::traverse(this->type_, traverse); } | |
11706 | ||
11707 | Type* | |
11708 | do_type() | |
11709 | { return Type::make_pointer_type(this->type_); } | |
11710 | ||
11711 | void | |
11712 | do_determine_type(const Type_context*) | |
11713 | { } | |
11714 | ||
e440a328 | 11715 | Expression* |
11716 | do_copy() | |
11717 | { return new Allocation_expression(this->type_, this->location()); } | |
11718 | ||
11719 | tree | |
11720 | do_get_tree(Translate_context*); | |
11721 | ||
d751bb78 | 11722 | void |
11723 | do_dump_expression(Ast_dump_context*) const; | |
11724 | ||
e440a328 | 11725 | private: |
11726 | // The type we are allocating. | |
11727 | Type* type_; | |
11728 | }; | |
11729 | ||
e440a328 | 11730 | // Return a tree for an allocation expression. |
11731 | ||
11732 | tree | |
11733 | Allocation_expression::do_get_tree(Translate_context* context) | |
11734 | { | |
2c809f8f | 11735 | Gogo* gogo = context->gogo(); |
11736 | Location loc = this->location(); | |
11737 | Expression* space = gogo->allocate_memory(this->type_, loc); | |
11738 | Bexpression* bspace = tree_to_expr(space->get_tree(context)); | |
11739 | Btype* pbtype = gogo->backend()->pointer_type(this->type_->get_backend(gogo)); | |
11740 | Bexpression* ret = gogo->backend()->convert_expression(pbtype, bspace, loc); | |
11741 | return expr_to_tree(ret); | |
e440a328 | 11742 | } |
11743 | ||
d751bb78 | 11744 | // Dump ast representation for an allocation expression. |
11745 | ||
11746 | void | |
11747 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11748 | const | |
11749 | { | |
11750 | ast_dump_context->ostream() << "new("; | |
11751 | ast_dump_context->dump_type(this->type_); | |
11752 | ast_dump_context->ostream() << ")"; | |
11753 | } | |
11754 | ||
e440a328 | 11755 | // Make an allocation expression. |
11756 | ||
11757 | Expression* | |
b13c66cd | 11758 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 11759 | { |
11760 | return new Allocation_expression(type, location); | |
11761 | } | |
11762 | ||
e440a328 | 11763 | // Construct a struct. |
11764 | ||
11765 | class Struct_construction_expression : public Expression | |
11766 | { | |
11767 | public: | |
11768 | Struct_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 11769 | Location location) |
e440a328 | 11770 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), |
0c4f5a19 | 11771 | type_(type), vals_(vals), traverse_order_(NULL) |
e440a328 | 11772 | { } |
11773 | ||
0c4f5a19 | 11774 | // Set the traversal order, used to ensure that we implement the |
11775 | // order of evaluation rules. Takes ownership of the argument. | |
11776 | void | |
11777 | set_traverse_order(std::vector<int>* traverse_order) | |
11778 | { this->traverse_order_ = traverse_order; } | |
11779 | ||
e440a328 | 11780 | // Return whether this is a constant initializer. |
11781 | bool | |
11782 | is_constant_struct() const; | |
11783 | ||
11784 | protected: | |
11785 | int | |
11786 | do_traverse(Traverse* traverse); | |
11787 | ||
f9ca30f9 | 11788 | bool |
11789 | do_is_immutable() const; | |
11790 | ||
e440a328 | 11791 | Type* |
11792 | do_type() | |
11793 | { return this->type_; } | |
11794 | ||
11795 | void | |
11796 | do_determine_type(const Type_context*); | |
11797 | ||
11798 | void | |
11799 | do_check_types(Gogo*); | |
11800 | ||
11801 | Expression* | |
11802 | do_copy() | |
11803 | { | |
0c4f5a19 | 11804 | Struct_construction_expression* ret = |
11805 | new Struct_construction_expression(this->type_, this->vals_->copy(), | |
11806 | this->location()); | |
11807 | if (this->traverse_order_ != NULL) | |
11808 | ret->set_traverse_order(this->traverse_order_); | |
11809 | return ret; | |
e440a328 | 11810 | } |
11811 | ||
e440a328 | 11812 | tree |
11813 | do_get_tree(Translate_context*); | |
11814 | ||
11815 | void | |
11816 | do_export(Export*) const; | |
11817 | ||
d751bb78 | 11818 | void |
11819 | do_dump_expression(Ast_dump_context*) const; | |
11820 | ||
e440a328 | 11821 | private: |
11822 | // The type of the struct to construct. | |
11823 | Type* type_; | |
11824 | // The list of values, in order of the fields in the struct. A NULL | |
11825 | // entry means that the field should be zero-initialized. | |
11826 | Expression_list* vals_; | |
0c4f5a19 | 11827 | // If not NULL, the order in which to traverse vals_. This is used |
11828 | // so that we implement the order of evaluation rules correctly. | |
11829 | std::vector<int>* traverse_order_; | |
e440a328 | 11830 | }; |
11831 | ||
11832 | // Traversal. | |
11833 | ||
11834 | int | |
11835 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
11836 | { | |
0c4f5a19 | 11837 | if (this->vals_ != NULL) |
11838 | { | |
11839 | if (this->traverse_order_ == NULL) | |
11840 | { | |
11841 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11842 | return TRAVERSE_EXIT; | |
11843 | } | |
11844 | else | |
11845 | { | |
11846 | for (std::vector<int>::const_iterator p = | |
11847 | this->traverse_order_->begin(); | |
11848 | p != this->traverse_order_->end(); | |
11849 | ++p) | |
11850 | { | |
11851 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
11852 | == TRAVERSE_EXIT) | |
11853 | return TRAVERSE_EXIT; | |
11854 | } | |
11855 | } | |
11856 | } | |
e440a328 | 11857 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
11858 | return TRAVERSE_EXIT; | |
11859 | return TRAVERSE_CONTINUE; | |
11860 | } | |
11861 | ||
11862 | // Return whether this is a constant initializer. | |
11863 | ||
11864 | bool | |
11865 | Struct_construction_expression::is_constant_struct() const | |
11866 | { | |
11867 | if (this->vals_ == NULL) | |
11868 | return true; | |
11869 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11870 | pv != this->vals_->end(); | |
11871 | ++pv) | |
11872 | { | |
11873 | if (*pv != NULL | |
11874 | && !(*pv)->is_constant() | |
11875 | && (!(*pv)->is_composite_literal() | |
11876 | || (*pv)->is_nonconstant_composite_literal())) | |
11877 | return false; | |
11878 | } | |
11879 | ||
11880 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11881 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11882 | pf != fields->end(); | |
11883 | ++pf) | |
11884 | { | |
11885 | // There are no constant constructors for interfaces. | |
11886 | if (pf->type()->interface_type() != NULL) | |
11887 | return false; | |
11888 | } | |
11889 | ||
11890 | return true; | |
11891 | } | |
11892 | ||
f9ca30f9 | 11893 | // Return whether this struct is immutable. |
11894 | ||
11895 | bool | |
11896 | Struct_construction_expression::do_is_immutable() const | |
11897 | { | |
11898 | if (this->vals_ == NULL) | |
11899 | return true; | |
11900 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11901 | pv != this->vals_->end(); | |
11902 | ++pv) | |
11903 | { | |
11904 | if (*pv != NULL && !(*pv)->is_immutable()) | |
11905 | return false; | |
11906 | } | |
11907 | return true; | |
11908 | } | |
11909 | ||
e440a328 | 11910 | // Final type determination. |
11911 | ||
11912 | void | |
11913 | Struct_construction_expression::do_determine_type(const Type_context*) | |
11914 | { | |
11915 | if (this->vals_ == NULL) | |
11916 | return; | |
11917 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11918 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11919 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11920 | pf != fields->end(); | |
11921 | ++pf, ++pv) | |
11922 | { | |
11923 | if (pv == this->vals_->end()) | |
11924 | return; | |
11925 | if (*pv != NULL) | |
11926 | { | |
11927 | Type_context subcontext(pf->type(), false); | |
11928 | (*pv)->determine_type(&subcontext); | |
11929 | } | |
11930 | } | |
a6cb4c0e | 11931 | // Extra values are an error we will report elsewhere; we still want |
11932 | // to determine the type to avoid knockon errors. | |
11933 | for (; pv != this->vals_->end(); ++pv) | |
11934 | (*pv)->determine_type_no_context(); | |
e440a328 | 11935 | } |
11936 | ||
11937 | // Check types. | |
11938 | ||
11939 | void | |
11940 | Struct_construction_expression::do_check_types(Gogo*) | |
11941 | { | |
11942 | if (this->vals_ == NULL) | |
11943 | return; | |
11944 | ||
11945 | Struct_type* st = this->type_->struct_type(); | |
11946 | if (this->vals_->size() > st->field_count()) | |
11947 | { | |
11948 | this->report_error(_("too many expressions for struct")); | |
11949 | return; | |
11950 | } | |
11951 | ||
11952 | const Struct_field_list* fields = st->fields(); | |
11953 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11954 | int i = 0; | |
11955 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11956 | pf != fields->end(); | |
11957 | ++pf, ++pv, ++i) | |
11958 | { | |
11959 | if (pv == this->vals_->end()) | |
11960 | { | |
11961 | this->report_error(_("too few expressions for struct")); | |
11962 | break; | |
11963 | } | |
11964 | ||
11965 | if (*pv == NULL) | |
11966 | continue; | |
11967 | ||
11968 | std::string reason; | |
11969 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
11970 | { | |
11971 | if (reason.empty()) | |
11972 | error_at((*pv)->location(), | |
11973 | "incompatible type for field %d in struct construction", | |
11974 | i + 1); | |
11975 | else | |
11976 | error_at((*pv)->location(), | |
11977 | ("incompatible type for field %d in " | |
11978 | "struct construction (%s)"), | |
11979 | i + 1, reason.c_str()); | |
11980 | this->set_is_error(); | |
11981 | } | |
11982 | } | |
c484d925 | 11983 | go_assert(pv == this->vals_->end()); |
e440a328 | 11984 | } |
11985 | ||
11986 | // Return a tree for constructing a struct. | |
11987 | ||
11988 | tree | |
11989 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
11990 | { | |
11991 | Gogo* gogo = context->gogo(); | |
11992 | ||
2c809f8f | 11993 | Btype* btype = this->type_->get_backend(gogo); |
e440a328 | 11994 | if (this->vals_ == NULL) |
2c809f8f | 11995 | return expr_to_tree(gogo->backend()->zero_expression(btype)); |
e440a328 | 11996 | |
e440a328 | 11997 | const Struct_field_list* fields = this->type_->struct_type()->fields(); |
e440a328 | 11998 | Expression_list::const_iterator pv = this->vals_->begin(); |
2c809f8f | 11999 | std::vector<Bexpression*> init; |
12000 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12001 | pf != fields->end(); | |
12002 | ++pf) | |
e440a328 | 12003 | { |
63697958 | 12004 | Btype* fbtype = pf->type()->get_backend(gogo); |
e440a328 | 12005 | if (pv == this->vals_->end()) |
2c809f8f | 12006 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12007 | else if (*pv == NULL) |
12008 | { | |
2c809f8f | 12009 | init.push_back(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12010 | ++pv; |
12011 | } | |
12012 | else | |
12013 | { | |
2c809f8f | 12014 | Expression* val = |
12015 | Expression::convert_for_assignment(gogo, pf->type(), | |
12016 | *pv, this->location()); | |
12017 | init.push_back(tree_to_expr(val->get_tree(context))); | |
e440a328 | 12018 | ++pv; |
12019 | } | |
e440a328 | 12020 | } |
e440a328 | 12021 | |
2c809f8f | 12022 | Bexpression* ret = |
12023 | gogo->backend()->constructor_expression(btype, init, this->location()); | |
12024 | return expr_to_tree(ret); | |
e440a328 | 12025 | } |
12026 | ||
12027 | // Export a struct construction. | |
12028 | ||
12029 | void | |
12030 | Struct_construction_expression::do_export(Export* exp) const | |
12031 | { | |
12032 | exp->write_c_string("convert("); | |
12033 | exp->write_type(this->type_); | |
12034 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12035 | pv != this->vals_->end(); | |
12036 | ++pv) | |
12037 | { | |
12038 | exp->write_c_string(", "); | |
12039 | if (*pv != NULL) | |
12040 | (*pv)->export_expression(exp); | |
12041 | } | |
12042 | exp->write_c_string(")"); | |
12043 | } | |
12044 | ||
d751bb78 | 12045 | // Dump ast representation of a struct construction expression. |
12046 | ||
12047 | void | |
12048 | Struct_construction_expression::do_dump_expression( | |
12049 | Ast_dump_context* ast_dump_context) const | |
12050 | { | |
d751bb78 | 12051 | ast_dump_context->dump_type(this->type_); |
12052 | ast_dump_context->ostream() << "{"; | |
12053 | ast_dump_context->dump_expression_list(this->vals_); | |
12054 | ast_dump_context->ostream() << "}"; | |
12055 | } | |
12056 | ||
e440a328 | 12057 | // Make a struct composite literal. This used by the thunk code. |
12058 | ||
12059 | Expression* | |
12060 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12061 | Location location) |
e440a328 | 12062 | { |
c484d925 | 12063 | go_assert(type->struct_type() != NULL); |
e440a328 | 12064 | return new Struct_construction_expression(type, vals, location); |
12065 | } | |
12066 | ||
12067 | // Construct an array. This class is not used directly; instead we | |
12068 | // use the child classes, Fixed_array_construction_expression and | |
2c809f8f | 12069 | // Slice_construction_expression. |
e440a328 | 12070 | |
12071 | class Array_construction_expression : public Expression | |
12072 | { | |
12073 | protected: | |
12074 | Array_construction_expression(Expression_classification classification, | |
ffe743ca | 12075 | Type* type, |
12076 | const std::vector<unsigned long>* indexes, | |
12077 | Expression_list* vals, Location location) | |
e440a328 | 12078 | : Expression(classification, location), |
ffe743ca | 12079 | type_(type), indexes_(indexes), vals_(vals) |
12080 | { go_assert(indexes == NULL || indexes->size() == vals->size()); } | |
e440a328 | 12081 | |
12082 | public: | |
12083 | // Return whether this is a constant initializer. | |
12084 | bool | |
12085 | is_constant_array() const; | |
12086 | ||
12087 | // Return the number of elements. | |
12088 | size_t | |
12089 | element_count() const | |
12090 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
12091 | ||
12092 | protected: | |
12093 | int | |
12094 | do_traverse(Traverse* traverse); | |
12095 | ||
f9ca30f9 | 12096 | bool |
12097 | do_is_immutable() const; | |
12098 | ||
e440a328 | 12099 | Type* |
12100 | do_type() | |
12101 | { return this->type_; } | |
12102 | ||
12103 | void | |
12104 | do_determine_type(const Type_context*); | |
12105 | ||
12106 | void | |
12107 | do_check_types(Gogo*); | |
12108 | ||
e440a328 | 12109 | void |
12110 | do_export(Export*) const; | |
12111 | ||
ffe743ca | 12112 | // The indexes. |
12113 | const std::vector<unsigned long>* | |
12114 | indexes() | |
12115 | { return this->indexes_; } | |
12116 | ||
e440a328 | 12117 | // The list of values. |
12118 | Expression_list* | |
12119 | vals() | |
12120 | { return this->vals_; } | |
12121 | ||
2c809f8f | 12122 | // Get the backend constructor for the array values. |
12123 | Bexpression* | |
12124 | get_constructor(Translate_context* context, Btype* btype); | |
e440a328 | 12125 | |
d751bb78 | 12126 | void |
12127 | do_dump_expression(Ast_dump_context*) const; | |
12128 | ||
e440a328 | 12129 | private: |
12130 | // The type of the array to construct. | |
12131 | Type* type_; | |
ffe743ca | 12132 | // The list of indexes into the array, one for each value. This may |
12133 | // be NULL, in which case the indexes start at zero and increment. | |
12134 | const std::vector<unsigned long>* indexes_; | |
12135 | // The list of values. This may be NULL if there are no values. | |
e440a328 | 12136 | Expression_list* vals_; |
12137 | }; | |
12138 | ||
12139 | // Traversal. | |
12140 | ||
12141 | int | |
12142 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12143 | { | |
12144 | if (this->vals_ != NULL | |
12145 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12146 | return TRAVERSE_EXIT; | |
12147 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12148 | return TRAVERSE_EXIT; | |
12149 | return TRAVERSE_CONTINUE; | |
12150 | } | |
12151 | ||
12152 | // Return whether this is a constant initializer. | |
12153 | ||
12154 | bool | |
12155 | Array_construction_expression::is_constant_array() const | |
12156 | { | |
12157 | if (this->vals_ == NULL) | |
12158 | return true; | |
12159 | ||
12160 | // There are no constant constructors for interfaces. | |
12161 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12162 | return false; | |
12163 | ||
12164 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12165 | pv != this->vals_->end(); | |
12166 | ++pv) | |
12167 | { | |
12168 | if (*pv != NULL | |
12169 | && !(*pv)->is_constant() | |
12170 | && (!(*pv)->is_composite_literal() | |
12171 | || (*pv)->is_nonconstant_composite_literal())) | |
12172 | return false; | |
12173 | } | |
12174 | return true; | |
12175 | } | |
12176 | ||
f9ca30f9 | 12177 | // Return whether this is an immutable array initializer. |
12178 | ||
12179 | bool | |
12180 | Array_construction_expression::do_is_immutable() const | |
12181 | { | |
12182 | if (this->vals_ == NULL) | |
12183 | return true; | |
12184 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12185 | pv != this->vals_->end(); | |
12186 | ++pv) | |
12187 | { | |
12188 | if (*pv != NULL && !(*pv)->is_immutable()) | |
12189 | return false; | |
12190 | } | |
12191 | return true; | |
12192 | } | |
12193 | ||
e440a328 | 12194 | // Final type determination. |
12195 | ||
12196 | void | |
12197 | Array_construction_expression::do_determine_type(const Type_context*) | |
12198 | { | |
12199 | if (this->vals_ == NULL) | |
12200 | return; | |
12201 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
12202 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12203 | pv != this->vals_->end(); | |
12204 | ++pv) | |
12205 | { | |
12206 | if (*pv != NULL) | |
12207 | (*pv)->determine_type(&subcontext); | |
12208 | } | |
12209 | } | |
12210 | ||
12211 | // Check types. | |
12212 | ||
12213 | void | |
12214 | Array_construction_expression::do_check_types(Gogo*) | |
12215 | { | |
12216 | if (this->vals_ == NULL) | |
12217 | return; | |
12218 | ||
12219 | Array_type* at = this->type_->array_type(); | |
12220 | int i = 0; | |
12221 | Type* element_type = at->element_type(); | |
12222 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12223 | pv != this->vals_->end(); | |
12224 | ++pv, ++i) | |
12225 | { | |
12226 | if (*pv != NULL | |
12227 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12228 | { | |
12229 | error_at((*pv)->location(), | |
12230 | "incompatible type for element %d in composite literal", | |
12231 | i + 1); | |
12232 | this->set_is_error(); | |
12233 | } | |
12234 | } | |
e440a328 | 12235 | } |
12236 | ||
2c809f8f | 12237 | // Get a constructor expression for the array values. |
e440a328 | 12238 | |
2c809f8f | 12239 | Bexpression* |
12240 | Array_construction_expression::get_constructor(Translate_context* context, | |
12241 | Btype* array_btype) | |
e440a328 | 12242 | { |
e440a328 | 12243 | Type* element_type = this->type_->array_type()->element_type(); |
2c809f8f | 12244 | |
12245 | std::vector<unsigned long> indexes; | |
12246 | std::vector<Bexpression*> vals; | |
12247 | Gogo* gogo = context->gogo(); | |
e440a328 | 12248 | if (this->vals_ != NULL) |
12249 | { | |
12250 | size_t i = 0; | |
ffe743ca | 12251 | std::vector<unsigned long>::const_iterator pi; |
12252 | if (this->indexes_ != NULL) | |
12253 | pi = this->indexes_->begin(); | |
e440a328 | 12254 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12255 | pv != this->vals_->end(); | |
12256 | ++pv, ++i) | |
12257 | { | |
ffe743ca | 12258 | if (this->indexes_ != NULL) |
12259 | go_assert(pi != this->indexes_->end()); | |
ffe743ca | 12260 | |
12261 | if (this->indexes_ == NULL) | |
2c809f8f | 12262 | indexes.push_back(i); |
ffe743ca | 12263 | else |
2c809f8f | 12264 | indexes.push_back(*pi); |
e440a328 | 12265 | if (*pv == NULL) |
63697958 | 12266 | { |
63697958 | 12267 | Btype* ebtype = element_type->get_backend(gogo); |
12268 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
2c809f8f | 12269 | vals.push_back(zv); |
63697958 | 12270 | } |
e440a328 | 12271 | else |
12272 | { | |
2c809f8f | 12273 | Expression* val_expr = |
12274 | Expression::convert_for_assignment(gogo, element_type, *pv, | |
12275 | this->location()); | |
12276 | vals.push_back(tree_to_expr(val_expr->get_tree(context))); | |
e440a328 | 12277 | } |
ffe743ca | 12278 | if (this->indexes_ != NULL) |
12279 | ++pi; | |
e440a328 | 12280 | } |
ffe743ca | 12281 | if (this->indexes_ != NULL) |
12282 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12283 | } |
2c809f8f | 12284 | return gogo->backend()->array_constructor_expression(array_btype, indexes, |
12285 | vals, this->location()); | |
e440a328 | 12286 | } |
12287 | ||
12288 | // Export an array construction. | |
12289 | ||
12290 | void | |
12291 | Array_construction_expression::do_export(Export* exp) const | |
12292 | { | |
12293 | exp->write_c_string("convert("); | |
12294 | exp->write_type(this->type_); | |
12295 | if (this->vals_ != NULL) | |
12296 | { | |
ffe743ca | 12297 | std::vector<unsigned long>::const_iterator pi; |
12298 | if (this->indexes_ != NULL) | |
12299 | pi = this->indexes_->begin(); | |
e440a328 | 12300 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12301 | pv != this->vals_->end(); | |
12302 | ++pv) | |
12303 | { | |
12304 | exp->write_c_string(", "); | |
ffe743ca | 12305 | |
12306 | if (this->indexes_ != NULL) | |
12307 | { | |
12308 | char buf[100]; | |
12309 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12310 | exp->write_c_string(buf); | |
12311 | exp->write_c_string(":"); | |
12312 | } | |
12313 | ||
e440a328 | 12314 | if (*pv != NULL) |
12315 | (*pv)->export_expression(exp); | |
ffe743ca | 12316 | |
12317 | if (this->indexes_ != NULL) | |
12318 | ++pi; | |
e440a328 | 12319 | } |
12320 | } | |
12321 | exp->write_c_string(")"); | |
12322 | } | |
12323 | ||
d751bb78 | 12324 | // Dump ast representation of an array construction expressin. |
12325 | ||
12326 | void | |
12327 | Array_construction_expression::do_dump_expression( | |
12328 | Ast_dump_context* ast_dump_context) const | |
12329 | { | |
ffe743ca | 12330 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12331 | |
12332 | ast_dump_context->ostream() << "[" ; | |
12333 | if (length != NULL) | |
12334 | { | |
12335 | ast_dump_context->dump_expression(length); | |
12336 | } | |
12337 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12338 | ast_dump_context->dump_type(this->type_); |
12339 | ast_dump_context->ostream() << "{" ; | |
ffe743ca | 12340 | if (this->indexes_ == NULL) |
12341 | ast_dump_context->dump_expression_list(this->vals_); | |
12342 | else | |
12343 | { | |
12344 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12345 | for (std::vector<unsigned long>::const_iterator pi = | |
12346 | this->indexes_->begin(); | |
12347 | pi != this->indexes_->end(); | |
12348 | ++pi, ++pv) | |
12349 | { | |
12350 | if (pi != this->indexes_->begin()) | |
12351 | ast_dump_context->ostream() << ", "; | |
12352 | ast_dump_context->ostream() << *pi << ':'; | |
12353 | ast_dump_context->dump_expression(*pv); | |
12354 | } | |
12355 | } | |
d751bb78 | 12356 | ast_dump_context->ostream() << "}" ; |
12357 | ||
12358 | } | |
12359 | ||
e440a328 | 12360 | // Construct a fixed array. |
12361 | ||
12362 | class Fixed_array_construction_expression : | |
12363 | public Array_construction_expression | |
12364 | { | |
12365 | public: | |
ffe743ca | 12366 | Fixed_array_construction_expression(Type* type, |
12367 | const std::vector<unsigned long>* indexes, | |
12368 | Expression_list* vals, Location location) | |
e440a328 | 12369 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, |
ffe743ca | 12370 | type, indexes, vals, location) |
12371 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12372 | |
12373 | protected: | |
12374 | Expression* | |
12375 | do_copy() | |
12376 | { | |
12377 | return new Fixed_array_construction_expression(this->type(), | |
ffe743ca | 12378 | this->indexes(), |
e440a328 | 12379 | (this->vals() == NULL |
12380 | ? NULL | |
12381 | : this->vals()->copy()), | |
12382 | this->location()); | |
12383 | } | |
12384 | ||
12385 | tree | |
12386 | do_get_tree(Translate_context*); | |
12387 | }; | |
12388 | ||
12389 | // Return a tree for constructing a fixed array. | |
12390 | ||
12391 | tree | |
12392 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
12393 | { | |
9f0e0513 | 12394 | Type* type = this->type(); |
12395 | Btype* btype = type->get_backend(context->gogo()); | |
2c809f8f | 12396 | return expr_to_tree(this->get_constructor(context, btype)); |
e440a328 | 12397 | } |
12398 | ||
76f85fd6 | 12399 | Expression* |
12400 | Expression::make_array_composite_literal(Type* type, Expression_list* vals, | |
12401 | Location location) | |
12402 | { | |
12403 | go_assert(type->array_type() != NULL && !type->is_slice_type()); | |
12404 | return new Fixed_array_construction_expression(type, NULL, vals, location); | |
12405 | } | |
12406 | ||
2c809f8f | 12407 | // Construct a slice. |
e440a328 | 12408 | |
2c809f8f | 12409 | class Slice_construction_expression : public Array_construction_expression |
e440a328 | 12410 | { |
12411 | public: | |
2c809f8f | 12412 | Slice_construction_expression(Type* type, |
12413 | const std::vector<unsigned long>* indexes, | |
12414 | Expression_list* vals, Location location) | |
12415 | : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION, | |
12416 | type, indexes, vals, location), | |
12417 | valtype_(NULL) | |
ffe743ca | 12418 | { go_assert(type->is_slice_type()); } |
e440a328 | 12419 | |
12420 | protected: | |
2c809f8f | 12421 | // Note that taking the address of a slice literal is invalid. |
e440a328 | 12422 | |
12423 | Expression* | |
12424 | do_copy() | |
12425 | { | |
2c809f8f | 12426 | return new Slice_construction_expression(this->type(), this->indexes(), |
12427 | (this->vals() == NULL | |
12428 | ? NULL | |
12429 | : this->vals()->copy()), | |
12430 | this->location()); | |
e440a328 | 12431 | } |
12432 | ||
12433 | tree | |
12434 | do_get_tree(Translate_context*); | |
2c809f8f | 12435 | |
12436 | private: | |
12437 | // The type of the values in this slice. | |
12438 | Type* valtype_; | |
e440a328 | 12439 | }; |
12440 | ||
2c809f8f | 12441 | // Return a tree for constructing a slice. |
e440a328 | 12442 | |
12443 | tree | |
2c809f8f | 12444 | Slice_construction_expression::do_get_tree(Translate_context* context) |
e440a328 | 12445 | { |
f9c68f17 | 12446 | Array_type* array_type = this->type()->array_type(); |
12447 | if (array_type == NULL) | |
12448 | { | |
c484d925 | 12449 | go_assert(this->type()->is_error()); |
f9c68f17 | 12450 | return error_mark_node; |
12451 | } | |
12452 | ||
12453 | Type* element_type = array_type->element_type(); | |
2c809f8f | 12454 | if (this->valtype_ == NULL) |
12455 | { | |
12456 | mpz_t lenval; | |
12457 | Expression* length; | |
12458 | if (this->vals() == NULL || this->vals()->empty()) | |
12459 | mpz_init_set_ui(lenval, 0); | |
12460 | else | |
12461 | { | |
12462 | if (this->indexes() == NULL) | |
12463 | mpz_init_set_ui(lenval, this->vals()->size()); | |
12464 | else | |
12465 | mpz_init_set_ui(lenval, this->indexes()->back() + 1); | |
12466 | } | |
12467 | Location loc = this->location(); | |
12468 | Type* int_type = Type::lookup_integer_type("int"); | |
12469 | length = Expression::make_integer(&lenval, int_type, loc); | |
12470 | mpz_clear(lenval); | |
12471 | this->valtype_ = Type::make_array_type(element_type, length); | |
12472 | } | |
3d60812e | 12473 | |
e440a328 | 12474 | tree values; |
2c809f8f | 12475 | Gogo* gogo = context->gogo(); |
12476 | Btype* val_btype = this->valtype_->get_backend(gogo); | |
e440a328 | 12477 | if (this->vals() == NULL || this->vals()->empty()) |
12478 | { | |
12479 | // We need to create a unique value. | |
2c809f8f | 12480 | Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo); |
12481 | Bexpression* zero = gogo->backend()->zero_expression(int_btype); | |
12482 | std::vector<unsigned long> index(1, 0); | |
12483 | std::vector<Bexpression*> val(1, zero); | |
12484 | Bexpression* ctor = | |
12485 | gogo->backend()->array_constructor_expression(val_btype, index, val, | |
12486 | this->location()); | |
12487 | values = expr_to_tree(ctor); | |
e440a328 | 12488 | } |
12489 | else | |
2c809f8f | 12490 | values = expr_to_tree(this->get_constructor(context, val_btype)); |
e440a328 | 12491 | |
12492 | if (values == error_mark_node) | |
12493 | return error_mark_node; | |
12494 | ||
12495 | bool is_constant_initializer = TREE_CONSTANT(values); | |
d8829beb | 12496 | |
12497 | // We have to copy the initial values into heap memory if we are in | |
12498 | // a function or if the values are not constants. We also have to | |
12499 | // copy them if they may contain pointers in a non-constant context, | |
12500 | // as otherwise the garbage collector won't see them. | |
12501 | bool copy_to_heap = (context->function() != NULL | |
12502 | || !is_constant_initializer | |
12503 | || (element_type->has_pointer() | |
12504 | && !context->is_const())); | |
e440a328 | 12505 | |
12506 | if (is_constant_initializer) | |
12507 | { | |
b13c66cd | 12508 | tree tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 12509 | create_tmp_var_name("C"), TREE_TYPE(values)); |
12510 | DECL_EXTERNAL(tmp) = 0; | |
12511 | TREE_PUBLIC(tmp) = 0; | |
12512 | TREE_STATIC(tmp) = 1; | |
12513 | DECL_ARTIFICIAL(tmp) = 1; | |
d8829beb | 12514 | if (copy_to_heap) |
e440a328 | 12515 | { |
d8829beb | 12516 | // If we are not copying the value to the heap, we will only |
12517 | // initialize the value once, so we can use this directly | |
12518 | // rather than copying it. In that case we can't make it | |
12519 | // read-only, because the program is permitted to change it. | |
e440a328 | 12520 | TREE_READONLY(tmp) = 1; |
12521 | TREE_CONSTANT(tmp) = 1; | |
12522 | } | |
12523 | DECL_INITIAL(tmp) = values; | |
12524 | rest_of_decl_compilation(tmp, 1, 0); | |
12525 | values = tmp; | |
12526 | } | |
12527 | ||
12528 | tree space; | |
12529 | tree set; | |
d8829beb | 12530 | if (!copy_to_heap) |
e440a328 | 12531 | { |
d8829beb | 12532 | // the initializer will only run once. |
e440a328 | 12533 | space = build_fold_addr_expr(values); |
12534 | set = NULL_TREE; | |
12535 | } | |
12536 | else | |
12537 | { | |
2c809f8f | 12538 | Expression* alloc = |
12539 | context->gogo()->allocate_memory(this->valtype_, this->location()); | |
12540 | space = save_expr(alloc->get_tree(context)); | |
e440a328 | 12541 | |
12542 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
b13c66cd | 12543 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
12544 | s); | |
e440a328 | 12545 | TREE_THIS_NOTRAP(ref) = 1; |
12546 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
12547 | } | |
12548 | ||
2c809f8f | 12549 | // Build a constructor for the slice. |
e440a328 | 12550 | |
9f0e0513 | 12551 | tree type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
3d60812e | 12552 | if (type_tree == error_mark_node) |
12553 | return error_mark_node; | |
c484d925 | 12554 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 12555 | |
95f84544 | 12556 | vec<constructor_elt, va_gc> *init; |
12557 | vec_alloc(init, 3); | |
e440a328 | 12558 | |
e82e4eb5 | 12559 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12560 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 12561 | tree field = TYPE_FIELDS(type_tree); |
c484d925 | 12562 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 12563 | elt->index = field; |
12564 | elt->value = fold_convert(TREE_TYPE(field), space); | |
12565 | ||
2c809f8f | 12566 | tree length_tree = this->valtype_->array_type()->length()->get_tree(context); |
95f84544 | 12567 | elt = init->quick_push(empty); |
e440a328 | 12568 | field = DECL_CHAIN(field); |
c484d925 | 12569 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 12570 | elt->index = field; |
12571 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
12572 | ||
95f84544 | 12573 | elt = init->quick_push(empty); |
e440a328 | 12574 | field = DECL_CHAIN(field); |
c484d925 | 12575 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); |
e440a328 | 12576 | elt->index = field; |
12577 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
12578 | ||
12579 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 12580 | if (constructor == error_mark_node) |
12581 | return error_mark_node; | |
d8829beb | 12582 | if (!copy_to_heap) |
e440a328 | 12583 | TREE_CONSTANT(constructor) = 1; |
12584 | ||
12585 | if (set == NULL_TREE) | |
12586 | return constructor; | |
12587 | else | |
12588 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
12589 | } | |
12590 | ||
12591 | // Make a slice composite literal. This is used by the type | |
12592 | // descriptor code. | |
12593 | ||
12594 | Expression* | |
12595 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12596 | Location location) |
e440a328 | 12597 | { |
411eb89e | 12598 | go_assert(type->is_slice_type()); |
2c809f8f | 12599 | return new Slice_construction_expression(type, NULL, vals, location); |
e440a328 | 12600 | } |
12601 | ||
12602 | // Construct a map. | |
12603 | ||
12604 | class Map_construction_expression : public Expression | |
12605 | { | |
12606 | public: | |
12607 | Map_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 12608 | Location location) |
e440a328 | 12609 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), |
2c809f8f | 12610 | type_(type), vals_(vals), element_type_(NULL), constructor_temp_(NULL) |
c484d925 | 12611 | { go_assert(vals == NULL || vals->size() % 2 == 0); } |
e440a328 | 12612 | |
12613 | protected: | |
12614 | int | |
12615 | do_traverse(Traverse* traverse); | |
12616 | ||
2c809f8f | 12617 | Expression* |
12618 | do_flatten(Gogo*, Named_object*, Statement_inserter*); | |
12619 | ||
e440a328 | 12620 | Type* |
12621 | do_type() | |
12622 | { return this->type_; } | |
12623 | ||
12624 | void | |
12625 | do_determine_type(const Type_context*); | |
12626 | ||
12627 | void | |
12628 | do_check_types(Gogo*); | |
12629 | ||
12630 | Expression* | |
12631 | do_copy() | |
12632 | { | |
12633 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
12634 | this->location()); | |
12635 | } | |
12636 | ||
12637 | tree | |
12638 | do_get_tree(Translate_context*); | |
12639 | ||
12640 | void | |
12641 | do_export(Export*) const; | |
12642 | ||
d751bb78 | 12643 | void |
12644 | do_dump_expression(Ast_dump_context*) const; | |
12645 | ||
e440a328 | 12646 | private: |
12647 | // The type of the map to construct. | |
12648 | Type* type_; | |
12649 | // The list of values. | |
12650 | Expression_list* vals_; | |
2c809f8f | 12651 | // The type of the key-value pair struct for each map element. |
12652 | Struct_type* element_type_; | |
12653 | // A temporary reference to the variable storing the constructor initializer. | |
12654 | Temporary_statement* constructor_temp_; | |
e440a328 | 12655 | }; |
12656 | ||
12657 | // Traversal. | |
12658 | ||
12659 | int | |
12660 | Map_construction_expression::do_traverse(Traverse* traverse) | |
12661 | { | |
12662 | if (this->vals_ != NULL | |
12663 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12664 | return TRAVERSE_EXIT; | |
12665 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12666 | return TRAVERSE_EXIT; | |
12667 | return TRAVERSE_CONTINUE; | |
12668 | } | |
12669 | ||
2c809f8f | 12670 | // Flatten constructor initializer into a temporary variable since |
12671 | // we need to take its address for __go_construct_map. | |
12672 | ||
12673 | Expression* | |
12674 | Map_construction_expression::do_flatten(Gogo* gogo, Named_object*, | |
12675 | Statement_inserter* inserter) | |
12676 | { | |
12677 | if (!this->is_error_expression() | |
12678 | && this->vals_ != NULL | |
12679 | && !this->vals_->empty() | |
12680 | && this->constructor_temp_ == NULL) | |
12681 | { | |
12682 | Map_type* mt = this->type_->map_type(); | |
12683 | Type* key_type = mt->key_type(); | |
12684 | Type* val_type = mt->val_type(); | |
12685 | this->element_type_ = Type::make_builtin_struct_type(2, | |
12686 | "__key", key_type, | |
12687 | "__val", val_type); | |
12688 | ||
12689 | Expression_list* value_pairs = new Expression_list(); | |
12690 | Location loc = this->location(); | |
12691 | ||
12692 | size_t i = 0; | |
12693 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12694 | pv != this->vals_->end(); | |
12695 | ++pv, ++i) | |
12696 | { | |
12697 | Expression_list* key_value_pair = new Expression_list(); | |
12698 | Expression* key = | |
12699 | Expression::convert_for_assignment(gogo, key_type, *pv, loc); | |
12700 | ||
12701 | ++pv; | |
12702 | Expression* val = | |
12703 | Expression::convert_for_assignment(gogo, val_type, *pv, loc); | |
12704 | ||
12705 | key_value_pair->push_back(key); | |
12706 | key_value_pair->push_back(val); | |
12707 | value_pairs->push_back( | |
12708 | Expression::make_struct_composite_literal(this->element_type_, | |
12709 | key_value_pair, loc)); | |
12710 | } | |
12711 | ||
12712 | mpz_t lenval; | |
12713 | mpz_init_set_ui(lenval, i); | |
12714 | Expression* element_count = Expression::make_integer(&lenval, NULL, loc); | |
12715 | mpz_clear(lenval); | |
12716 | ||
12717 | Type* ctor_type = | |
12718 | Type::make_array_type(this->element_type_, element_count); | |
12719 | Expression* constructor = | |
12720 | new Fixed_array_construction_expression(ctor_type, NULL, | |
12721 | value_pairs, loc); | |
12722 | ||
12723 | this->constructor_temp_ = | |
12724 | Statement::make_temporary(NULL, constructor, loc); | |
12725 | constructor->issue_nil_check(); | |
12726 | this->constructor_temp_->set_is_address_taken(); | |
12727 | inserter->insert(this->constructor_temp_); | |
12728 | } | |
12729 | ||
12730 | return this; | |
12731 | } | |
12732 | ||
e440a328 | 12733 | // Final type determination. |
12734 | ||
12735 | void | |
12736 | Map_construction_expression::do_determine_type(const Type_context*) | |
12737 | { | |
12738 | if (this->vals_ == NULL) | |
12739 | return; | |
12740 | ||
12741 | Map_type* mt = this->type_->map_type(); | |
12742 | Type_context key_context(mt->key_type(), false); | |
12743 | Type_context val_context(mt->val_type(), false); | |
12744 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12745 | pv != this->vals_->end(); | |
12746 | ++pv) | |
12747 | { | |
12748 | (*pv)->determine_type(&key_context); | |
12749 | ++pv; | |
12750 | (*pv)->determine_type(&val_context); | |
12751 | } | |
12752 | } | |
12753 | ||
12754 | // Check types. | |
12755 | ||
12756 | void | |
12757 | Map_construction_expression::do_check_types(Gogo*) | |
12758 | { | |
12759 | if (this->vals_ == NULL) | |
12760 | return; | |
12761 | ||
12762 | Map_type* mt = this->type_->map_type(); | |
12763 | int i = 0; | |
12764 | Type* key_type = mt->key_type(); | |
12765 | Type* val_type = mt->val_type(); | |
12766 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12767 | pv != this->vals_->end(); | |
12768 | ++pv, ++i) | |
12769 | { | |
12770 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
12771 | { | |
12772 | error_at((*pv)->location(), | |
12773 | "incompatible type for element %d key in map construction", | |
12774 | i + 1); | |
12775 | this->set_is_error(); | |
12776 | } | |
12777 | ++pv; | |
12778 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
12779 | { | |
12780 | error_at((*pv)->location(), | |
12781 | ("incompatible type for element %d value " | |
12782 | "in map construction"), | |
12783 | i + 1); | |
12784 | this->set_is_error(); | |
12785 | } | |
12786 | } | |
12787 | } | |
12788 | ||
12789 | // Return a tree for constructing a map. | |
12790 | ||
12791 | tree | |
12792 | Map_construction_expression::do_get_tree(Translate_context* context) | |
12793 | { | |
2c809f8f | 12794 | if (this->is_error_expression()) |
5845bde6 | 12795 | return error_mark_node; |
2c809f8f | 12796 | Location loc = this->location(); |
e440a328 | 12797 | |
e440a328 | 12798 | size_t i = 0; |
2c809f8f | 12799 | Expression* ventries; |
e440a328 | 12800 | if (this->vals_ == NULL || this->vals_->empty()) |
2c809f8f | 12801 | ventries = Expression::make_nil(loc); |
e440a328 | 12802 | else |
12803 | { | |
2c809f8f | 12804 | go_assert(this->constructor_temp_ != NULL); |
12805 | i = this->vals_->size() / 2; | |
e440a328 | 12806 | |
2c809f8f | 12807 | Expression* ctor_ref = |
12808 | Expression::make_temporary_reference(this->constructor_temp_, loc); | |
12809 | ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc); | |
12810 | } | |
e440a328 | 12811 | |
2c809f8f | 12812 | Map_type* mt = this->type_->map_type(); |
12813 | if (this->element_type_ == NULL) | |
12814 | this->element_type_ = | |
12815 | Type::make_builtin_struct_type(2, | |
12816 | "__key", mt->key_type(), | |
12817 | "__val", mt->val_type()); | |
12818 | Expression* descriptor = Expression::make_map_descriptor(mt, loc); | |
12819 | ||
12820 | Type* uintptr_t = Type::lookup_integer_type("uintptr"); | |
12821 | mpz_t countval; | |
12822 | mpz_init_set_ui(countval, i); | |
12823 | Expression* count = Expression::make_integer(&countval, uintptr_t, loc); | |
12824 | mpz_clear(countval); | |
12825 | ||
12826 | Expression* entry_size = | |
12827 | Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE); | |
12828 | ||
12829 | unsigned int field_index; | |
12830 | const Struct_field* valfield = | |
12831 | this->element_type_->find_local_field("__val", &field_index); | |
12832 | Expression* val_offset = | |
12833 | Expression::make_struct_field_offset(this->element_type_, valfield); | |
12834 | Expression* val_size = | |
12835 | Expression::make_type_info(mt->val_type(), TYPE_INFO_SIZE); | |
12836 | ||
12837 | Expression* map_ctor = | |
12838 | Runtime::make_call(Runtime::CONSTRUCT_MAP, loc, 6, descriptor, count, | |
12839 | entry_size, val_offset, val_size, ventries); | |
12840 | return map_ctor->get_tree(context); | |
12841 | } | |
e440a328 | 12842 | |
2c809f8f | 12843 | // Export an array construction. |
e440a328 | 12844 | |
2c809f8f | 12845 | void |
12846 | Map_construction_expression::do_export(Export* exp) const | |
12847 | { | |
12848 | exp->write_c_string("convert("); | |
12849 | exp->write_type(this->type_); | |
12850 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12851 | pv != this->vals_->end(); | |
12852 | ++pv) | |
12853 | { | |
12854 | exp->write_c_string(", "); | |
12855 | (*pv)->export_expression(exp); | |
12856 | } | |
12857 | exp->write_c_string(")"); | |
12858 | } | |
e440a328 | 12859 | |
2c809f8f | 12860 | // Dump ast representation for a map construction expression. |
d751bb78 | 12861 | |
12862 | void | |
12863 | Map_construction_expression::do_dump_expression( | |
12864 | Ast_dump_context* ast_dump_context) const | |
12865 | { | |
d751bb78 | 12866 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 12867 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 12868 | ast_dump_context->ostream() << "}"; |
12869 | } | |
12870 | ||
e440a328 | 12871 | // A general composite literal. This is lowered to a type specific |
12872 | // version. | |
12873 | ||
12874 | class Composite_literal_expression : public Parser_expression | |
12875 | { | |
12876 | public: | |
12877 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
62750cd5 | 12878 | Expression_list* vals, bool all_are_names, |
12879 | Location location) | |
e440a328 | 12880 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), |
62750cd5 | 12881 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys), |
12882 | all_are_names_(all_are_names) | |
e440a328 | 12883 | { } |
12884 | ||
12885 | protected: | |
12886 | int | |
12887 | do_traverse(Traverse* traverse); | |
12888 | ||
12889 | Expression* | |
ceeb4318 | 12890 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12891 | |
12892 | Expression* | |
12893 | do_copy() | |
12894 | { | |
12895 | return new Composite_literal_expression(this->type_, this->depth_, | |
12896 | this->has_keys_, | |
12897 | (this->vals_ == NULL | |
12898 | ? NULL | |
12899 | : this->vals_->copy()), | |
62750cd5 | 12900 | this->all_are_names_, |
e440a328 | 12901 | this->location()); |
12902 | } | |
12903 | ||
d751bb78 | 12904 | void |
12905 | do_dump_expression(Ast_dump_context*) const; | |
12906 | ||
e440a328 | 12907 | private: |
12908 | Expression* | |
81c4b26b | 12909 | lower_struct(Gogo*, Type*); |
e440a328 | 12910 | |
12911 | Expression* | |
113ef6a5 | 12912 | lower_array(Type*); |
e440a328 | 12913 | |
12914 | Expression* | |
ffe743ca | 12915 | make_array(Type*, const std::vector<unsigned long>*, Expression_list*); |
e440a328 | 12916 | |
12917 | Expression* | |
ceeb4318 | 12918 | lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); |
e440a328 | 12919 | |
12920 | // The type of the composite literal. | |
12921 | Type* type_; | |
12922 | // The depth within a list of composite literals within a composite | |
12923 | // literal, when the type is omitted. | |
12924 | int depth_; | |
12925 | // The values to put in the composite literal. | |
12926 | Expression_list* vals_; | |
12927 | // If this is true, then VALS_ is a list of pairs: a key and a | |
12928 | // value. In an array initializer, a missing key will be NULL. | |
12929 | bool has_keys_; | |
62750cd5 | 12930 | // If this is true, then HAS_KEYS_ is true, and every key is a |
12931 | // simple identifier. | |
12932 | bool all_are_names_; | |
e440a328 | 12933 | }; |
12934 | ||
12935 | // Traversal. | |
12936 | ||
12937 | int | |
12938 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
12939 | { | |
dbffccfc | 12940 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
e440a328 | 12941 | return TRAVERSE_EXIT; |
dbffccfc | 12942 | |
12943 | // If this is a struct composite literal with keys, then the keys | |
12944 | // are field names, not expressions. We don't want to traverse them | |
12945 | // in that case. If we do, we can give an erroneous error "variable | |
12946 | // initializer refers to itself." See bug482.go in the testsuite. | |
12947 | if (this->has_keys_ && this->vals_ != NULL) | |
12948 | { | |
12949 | // The type may not be resolvable at this point. | |
12950 | Type* type = this->type_; | |
a01f2481 | 12951 | |
12952 | for (int depth = this->depth_; depth > 0; --depth) | |
12953 | { | |
12954 | if (type->array_type() != NULL) | |
12955 | type = type->array_type()->element_type(); | |
12956 | else if (type->map_type() != NULL) | |
12957 | type = type->map_type()->val_type(); | |
12958 | else | |
12959 | { | |
12960 | // This error will be reported during lowering. | |
12961 | return TRAVERSE_CONTINUE; | |
12962 | } | |
12963 | } | |
12964 | ||
dbffccfc | 12965 | while (true) |
12966 | { | |
12967 | if (type->classification() == Type::TYPE_NAMED) | |
12968 | type = type->named_type()->real_type(); | |
12969 | else if (type->classification() == Type::TYPE_FORWARD) | |
12970 | { | |
12971 | Type* t = type->forwarded(); | |
12972 | if (t == type) | |
12973 | break; | |
12974 | type = t; | |
12975 | } | |
12976 | else | |
12977 | break; | |
12978 | } | |
12979 | ||
12980 | if (type->classification() == Type::TYPE_STRUCT) | |
12981 | { | |
12982 | Expression_list::iterator p = this->vals_->begin(); | |
12983 | while (p != this->vals_->end()) | |
12984 | { | |
12985 | // Skip key. | |
12986 | ++p; | |
12987 | go_assert(p != this->vals_->end()); | |
12988 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12989 | return TRAVERSE_EXIT; | |
12990 | ++p; | |
12991 | } | |
12992 | return TRAVERSE_CONTINUE; | |
12993 | } | |
12994 | } | |
12995 | ||
12996 | if (this->vals_ != NULL) | |
12997 | return this->vals_->traverse(traverse); | |
12998 | ||
12999 | return TRAVERSE_CONTINUE; | |
e440a328 | 13000 | } |
13001 | ||
13002 | // Lower a generic composite literal into a specific version based on | |
13003 | // the type. | |
13004 | ||
13005 | Expression* | |
ceeb4318 | 13006 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13007 | Statement_inserter* inserter, int) | |
e440a328 | 13008 | { |
13009 | Type* type = this->type_; | |
13010 | ||
13011 | for (int depth = this->depth_; depth > 0; --depth) | |
13012 | { | |
13013 | if (type->array_type() != NULL) | |
13014 | type = type->array_type()->element_type(); | |
13015 | else if (type->map_type() != NULL) | |
13016 | type = type->map_type()->val_type(); | |
13017 | else | |
13018 | { | |
5c13bd80 | 13019 | if (!type->is_error()) |
e440a328 | 13020 | error_at(this->location(), |
13021 | ("may only omit types within composite literals " | |
13022 | "of slice, array, or map type")); | |
13023 | return Expression::make_error(this->location()); | |
13024 | } | |
13025 | } | |
13026 | ||
e00772b3 | 13027 | Type *pt = type->points_to(); |
13028 | bool is_pointer = false; | |
13029 | if (pt != NULL) | |
13030 | { | |
13031 | is_pointer = true; | |
13032 | type = pt; | |
13033 | } | |
13034 | ||
13035 | Expression* ret; | |
5c13bd80 | 13036 | if (type->is_error()) |
e440a328 | 13037 | return Expression::make_error(this->location()); |
13038 | else if (type->struct_type() != NULL) | |
e00772b3 | 13039 | ret = this->lower_struct(gogo, type); |
e440a328 | 13040 | else if (type->array_type() != NULL) |
113ef6a5 | 13041 | ret = this->lower_array(type); |
e440a328 | 13042 | else if (type->map_type() != NULL) |
e00772b3 | 13043 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13044 | else |
13045 | { | |
13046 | error_at(this->location(), | |
13047 | ("expected struct, slice, array, or map type " | |
13048 | "for composite literal")); | |
13049 | return Expression::make_error(this->location()); | |
13050 | } | |
e00772b3 | 13051 | |
13052 | if (is_pointer) | |
2c809f8f | 13053 | ret = Expression::make_heap_expression(ret, this->location()); |
e00772b3 | 13054 | |
13055 | return ret; | |
e440a328 | 13056 | } |
13057 | ||
13058 | // Lower a struct composite literal. | |
13059 | ||
13060 | Expression* | |
81c4b26b | 13061 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13062 | { |
b13c66cd | 13063 | Location location = this->location(); |
e440a328 | 13064 | Struct_type* st = type->struct_type(); |
13065 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13066 | { |
e6013c28 | 13067 | if (this->vals_ != NULL |
13068 | && !this->vals_->empty() | |
13069 | && type->named_type() != NULL | |
13070 | && type->named_type()->named_object()->package() != NULL) | |
13071 | { | |
13072 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13073 | pf != st->fields()->end(); | |
13074 | ++pf) | |
07daa4e7 | 13075 | { |
e6013c28 | 13076 | if (Gogo::is_hidden_name(pf->field_name())) |
07daa4e7 | 13077 | error_at(this->location(), |
e6013c28 | 13078 | "assignment of unexported field %qs in %qs literal", |
13079 | Gogo::message_name(pf->field_name()).c_str(), | |
13080 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13081 | } |
13082 | } | |
13083 | ||
13084 | return new Struct_construction_expression(type, this->vals_, location); | |
13085 | } | |
e440a328 | 13086 | |
13087 | size_t field_count = st->field_count(); | |
13088 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 13089 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 13090 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13091 | Expression* external_expr = NULL; |
13092 | const Named_object* external_no = NULL; | |
e440a328 | 13093 | while (p != this->vals_->end()) |
13094 | { | |
13095 | Expression* name_expr = *p; | |
13096 | ||
13097 | ++p; | |
c484d925 | 13098 | go_assert(p != this->vals_->end()); |
e440a328 | 13099 | Expression* val = *p; |
13100 | ||
13101 | ++p; | |
13102 | ||
13103 | if (name_expr == NULL) | |
13104 | { | |
13105 | error_at(val->location(), "mixture of field and value initializers"); | |
13106 | return Expression::make_error(location); | |
13107 | } | |
13108 | ||
13109 | bool bad_key = false; | |
13110 | std::string name; | |
81c4b26b | 13111 | const Named_object* no = NULL; |
e440a328 | 13112 | switch (name_expr->classification()) |
13113 | { | |
13114 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13115 | name = name_expr->unknown_expression()->name(); | |
13116 | break; | |
13117 | ||
13118 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13119 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13120 | break; |
13121 | ||
13122 | case EXPRESSION_TYPE: | |
13123 | { | |
13124 | Type* t = name_expr->type(); | |
13125 | Named_type* nt = t->named_type(); | |
13126 | if (nt == NULL) | |
13127 | bad_key = true; | |
13128 | else | |
81c4b26b | 13129 | no = nt->named_object(); |
e440a328 | 13130 | } |
13131 | break; | |
13132 | ||
13133 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13134 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13135 | break; |
13136 | ||
13137 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 13138 | no = name_expr->func_expression()->named_object(); |
e440a328 | 13139 | break; |
13140 | ||
13141 | case EXPRESSION_UNARY: | |
13142 | // If there is a local variable around with the same name as | |
13143 | // the field, and this occurs in the closure, then the | |
13144 | // parser may turn the field reference into an indirection | |
13145 | // through the closure. FIXME: This is a mess. | |
13146 | { | |
13147 | bad_key = true; | |
13148 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
13149 | if (ue->op() == OPERATOR_MULT) | |
13150 | { | |
13151 | Field_reference_expression* fre = | |
13152 | ue->operand()->field_reference_expression(); | |
13153 | if (fre != NULL) | |
13154 | { | |
13155 | Struct_type* st = | |
13156 | fre->expr()->type()->deref()->struct_type(); | |
13157 | if (st != NULL) | |
13158 | { | |
13159 | const Struct_field* sf = st->field(fre->field_index()); | |
13160 | name = sf->field_name(); | |
2d29d278 | 13161 | |
13162 | // See below. FIXME. | |
13163 | if (!Gogo::is_hidden_name(name) | |
13164 | && name[0] >= 'a' | |
13165 | && name[0] <= 'z') | |
13166 | { | |
13167 | if (gogo->lookup_global(name.c_str()) != NULL) | |
13168 | name = gogo->pack_hidden_name(name, false); | |
13169 | } | |
13170 | ||
e440a328 | 13171 | char buf[20]; |
13172 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
13173 | size_t buflen = strlen(buf); | |
13174 | if (name.compare(name.length() - buflen, buflen, buf) | |
13175 | == 0) | |
13176 | { | |
13177 | name = name.substr(0, name.length() - buflen); | |
13178 | bad_key = false; | |
13179 | } | |
13180 | } | |
13181 | } | |
13182 | } | |
13183 | } | |
13184 | break; | |
13185 | ||
13186 | default: | |
13187 | bad_key = true; | |
13188 | break; | |
13189 | } | |
13190 | if (bad_key) | |
13191 | { | |
13192 | error_at(name_expr->location(), "expected struct field name"); | |
13193 | return Expression::make_error(location); | |
13194 | } | |
13195 | ||
81c4b26b | 13196 | if (no != NULL) |
13197 | { | |
62750cd5 | 13198 | if (no->package() != NULL && external_expr == NULL) |
13199 | { | |
13200 | external_expr = name_expr; | |
13201 | external_no = no; | |
13202 | } | |
13203 | ||
81c4b26b | 13204 | name = no->name(); |
13205 | ||
13206 | // A predefined name won't be packed. If it starts with a | |
13207 | // lower case letter we need to check for that case, because | |
2d29d278 | 13208 | // the field name will be packed. FIXME. |
81c4b26b | 13209 | if (!Gogo::is_hidden_name(name) |
13210 | && name[0] >= 'a' | |
13211 | && name[0] <= 'z') | |
13212 | { | |
13213 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13214 | if (gno == no) | |
13215 | name = gogo->pack_hidden_name(name, false); | |
13216 | } | |
13217 | } | |
13218 | ||
e440a328 | 13219 | unsigned int index; |
13220 | const Struct_field* sf = st->find_local_field(name, &index); | |
13221 | if (sf == NULL) | |
13222 | { | |
13223 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
13224 | Gogo::message_name(name).c_str(), | |
13225 | (type->named_type() != NULL | |
13226 | ? type->named_type()->message_name().c_str() | |
13227 | : "unnamed struct")); | |
13228 | return Expression::make_error(location); | |
13229 | } | |
13230 | if (vals[index] != NULL) | |
13231 | { | |
13232 | error_at(name_expr->location(), | |
13233 | "duplicate value for field %qs in %qs", | |
13234 | Gogo::message_name(name).c_str(), | |
13235 | (type->named_type() != NULL | |
13236 | ? type->named_type()->message_name().c_str() | |
13237 | : "unnamed struct")); | |
13238 | return Expression::make_error(location); | |
13239 | } | |
13240 | ||
07daa4e7 | 13241 | if (type->named_type() != NULL |
13242 | && type->named_type()->named_object()->package() != NULL | |
13243 | && Gogo::is_hidden_name(sf->field_name())) | |
13244 | error_at(name_expr->location(), | |
13245 | "assignment of unexported field %qs in %qs literal", | |
13246 | Gogo::message_name(sf->field_name()).c_str(), | |
13247 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13248 | |
e440a328 | 13249 | vals[index] = val; |
0c4f5a19 | 13250 | traverse_order->push_back(index); |
e440a328 | 13251 | } |
13252 | ||
62750cd5 | 13253 | if (!this->all_are_names_) |
13254 | { | |
13255 | // This is a weird case like bug462 in the testsuite. | |
13256 | if (external_expr == NULL) | |
13257 | error_at(this->location(), "unknown field in %qs literal", | |
13258 | (type->named_type() != NULL | |
13259 | ? type->named_type()->message_name().c_str() | |
13260 | : "unnamed struct")); | |
13261 | else | |
13262 | error_at(external_expr->location(), "unknown field %qs in %qs", | |
13263 | external_no->message_name().c_str(), | |
13264 | (type->named_type() != NULL | |
13265 | ? type->named_type()->message_name().c_str() | |
13266 | : "unnamed struct")); | |
13267 | return Expression::make_error(location); | |
13268 | } | |
13269 | ||
e440a328 | 13270 | Expression_list* list = new Expression_list; |
13271 | list->reserve(field_count); | |
13272 | for (size_t i = 0; i < field_count; ++i) | |
13273 | list->push_back(vals[i]); | |
13274 | ||
0c4f5a19 | 13275 | Struct_construction_expression* ret = |
13276 | new Struct_construction_expression(type, list, location); | |
13277 | ret->set_traverse_order(traverse_order); | |
13278 | return ret; | |
e440a328 | 13279 | } |
13280 | ||
00773463 | 13281 | // Used to sort an index/value array. |
13282 | ||
13283 | class Index_value_compare | |
13284 | { | |
13285 | public: | |
13286 | bool | |
13287 | operator()(const std::pair<unsigned long, Expression*>& a, | |
13288 | const std::pair<unsigned long, Expression*>& b) | |
13289 | { return a.first < b.first; } | |
13290 | }; | |
13291 | ||
e440a328 | 13292 | // Lower an array composite literal. |
13293 | ||
13294 | Expression* | |
113ef6a5 | 13295 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13296 | { |
b13c66cd | 13297 | Location location = this->location(); |
e440a328 | 13298 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13299 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13300 | |
ffe743ca | 13301 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13302 | indexes->reserve(this->vals_->size()); | |
00773463 | 13303 | bool indexes_out_of_order = false; |
ffe743ca | 13304 | Expression_list* vals = new Expression_list(); |
13305 | vals->reserve(this->vals_->size()); | |
e440a328 | 13306 | unsigned long index = 0; |
13307 | Expression_list::const_iterator p = this->vals_->begin(); | |
13308 | while (p != this->vals_->end()) | |
13309 | { | |
13310 | Expression* index_expr = *p; | |
13311 | ||
13312 | ++p; | |
c484d925 | 13313 | go_assert(p != this->vals_->end()); |
e440a328 | 13314 | Expression* val = *p; |
13315 | ||
13316 | ++p; | |
13317 | ||
ffe743ca | 13318 | if (index_expr == NULL) |
13319 | { | |
13320 | if (!indexes->empty()) | |
13321 | indexes->push_back(index); | |
13322 | } | |
13323 | else | |
e440a328 | 13324 | { |
ffe743ca | 13325 | if (indexes->empty() && !vals->empty()) |
13326 | { | |
13327 | for (size_t i = 0; i < vals->size(); ++i) | |
13328 | indexes->push_back(i); | |
13329 | } | |
13330 | ||
0c77715b | 13331 | Numeric_constant nc; |
13332 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13333 | { |
e440a328 | 13334 | error_at(index_expr->location(), |
13335 | "index expression is not integer constant"); | |
13336 | return Expression::make_error(location); | |
13337 | } | |
6f6d9955 | 13338 | |
0c77715b | 13339 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13340 | { |
0c77715b | 13341 | case Numeric_constant::NC_UL_VALID: |
13342 | break; | |
13343 | case Numeric_constant::NC_UL_NOTINT: | |
13344 | error_at(index_expr->location(), | |
13345 | "index expression is not integer constant"); | |
13346 | return Expression::make_error(location); | |
13347 | case Numeric_constant::NC_UL_NEGATIVE: | |
e440a328 | 13348 | error_at(index_expr->location(), "index expression is negative"); |
13349 | return Expression::make_error(location); | |
0c77715b | 13350 | case Numeric_constant::NC_UL_BIG: |
e440a328 | 13351 | error_at(index_expr->location(), "index value overflow"); |
13352 | return Expression::make_error(location); | |
0c77715b | 13353 | default: |
13354 | go_unreachable(); | |
e440a328 | 13355 | } |
6f6d9955 | 13356 | |
13357 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13358 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13359 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13360 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13361 | { |
6f6d9955 | 13362 | error_at(index_expr->location(), "index value overflow"); |
13363 | return Expression::make_error(location); | |
13364 | } | |
13365 | ||
ffe743ca | 13366 | if (std::find(indexes->begin(), indexes->end(), index) |
13367 | != indexes->end()) | |
e440a328 | 13368 | { |
ffe743ca | 13369 | error_at(index_expr->location(), "duplicate value for index %lu", |
e440a328 | 13370 | index); |
13371 | return Expression::make_error(location); | |
13372 | } | |
ffe743ca | 13373 | |
00773463 | 13374 | if (!indexes->empty() && index < indexes->back()) |
13375 | indexes_out_of_order = true; | |
13376 | ||
ffe743ca | 13377 | indexes->push_back(index); |
e440a328 | 13378 | } |
13379 | ||
ffe743ca | 13380 | vals->push_back(val); |
13381 | ||
e440a328 | 13382 | ++index; |
13383 | } | |
13384 | ||
ffe743ca | 13385 | if (indexes->empty()) |
13386 | { | |
13387 | delete indexes; | |
13388 | indexes = NULL; | |
13389 | } | |
e440a328 | 13390 | |
00773463 | 13391 | if (indexes_out_of_order) |
13392 | { | |
13393 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
13394 | ||
13395 | V v; | |
13396 | v.reserve(indexes->size()); | |
13397 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
13398 | for (Expression_list::const_iterator pe = vals->begin(); | |
13399 | pe != vals->end(); | |
13400 | ++pe, ++pi) | |
13401 | v.push_back(std::make_pair(*pi, *pe)); | |
13402 | ||
13403 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
13404 | ||
13405 | delete indexes; | |
13406 | delete vals; | |
13407 | indexes = new std::vector<unsigned long>(); | |
13408 | indexes->reserve(v.size()); | |
13409 | vals = new Expression_list(); | |
13410 | vals->reserve(v.size()); | |
13411 | ||
13412 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13413 | { | |
13414 | indexes->push_back(p->first); | |
13415 | vals->push_back(p->second); | |
13416 | } | |
13417 | } | |
13418 | ||
ffe743ca | 13419 | return this->make_array(type, indexes, vals); |
e440a328 | 13420 | } |
13421 | ||
13422 | // Actually build the array composite literal. This handles | |
13423 | // [...]{...}. | |
13424 | ||
13425 | Expression* | |
ffe743ca | 13426 | Composite_literal_expression::make_array( |
13427 | Type* type, | |
13428 | const std::vector<unsigned long>* indexes, | |
13429 | Expression_list* vals) | |
e440a328 | 13430 | { |
b13c66cd | 13431 | Location location = this->location(); |
e440a328 | 13432 | Array_type* at = type->array_type(); |
ffe743ca | 13433 | |
e440a328 | 13434 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13435 | { | |
ffe743ca | 13436 | size_t size; |
13437 | if (vals == NULL) | |
13438 | size = 0; | |
00773463 | 13439 | else if (indexes != NULL) |
13440 | size = indexes->back() + 1; | |
13441 | else | |
ffe743ca | 13442 | { |
13443 | size = vals->size(); | |
13444 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13445 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13446 | && size >> (it->bits() - 1) != 0) | |
13447 | { | |
13448 | error_at(location, "too many elements in composite literal"); | |
13449 | return Expression::make_error(location); | |
13450 | } | |
13451 | } | |
ffe743ca | 13452 | |
e440a328 | 13453 | mpz_t vlen; |
13454 | mpz_init_set_ui(vlen, size); | |
13455 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
13456 | mpz_clear(vlen); | |
13457 | at = Type::make_array_type(at->element_type(), elen); | |
13458 | type = at; | |
13459 | } | |
ffe743ca | 13460 | else if (at->length() != NULL |
13461 | && !at->length()->is_error_expression() | |
13462 | && this->vals_ != NULL) | |
13463 | { | |
13464 | Numeric_constant nc; | |
13465 | unsigned long val; | |
13466 | if (at->length()->numeric_constant_value(&nc) | |
13467 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13468 | { | |
13469 | if (indexes == NULL) | |
13470 | { | |
13471 | if (this->vals_->size() > val) | |
13472 | { | |
13473 | error_at(location, "too many elements in composite literal"); | |
13474 | return Expression::make_error(location); | |
13475 | } | |
13476 | } | |
13477 | else | |
13478 | { | |
00773463 | 13479 | unsigned long max = indexes->back(); |
ffe743ca | 13480 | if (max >= val) |
13481 | { | |
13482 | error_at(location, | |
13483 | ("some element keys in composite literal " | |
13484 | "are out of range")); | |
13485 | return Expression::make_error(location); | |
13486 | } | |
13487 | } | |
13488 | } | |
13489 | } | |
13490 | ||
e440a328 | 13491 | if (at->length() != NULL) |
ffe743ca | 13492 | return new Fixed_array_construction_expression(type, indexes, vals, |
13493 | location); | |
e440a328 | 13494 | else |
2c809f8f | 13495 | return new Slice_construction_expression(type, indexes, vals, location); |
e440a328 | 13496 | } |
13497 | ||
13498 | // Lower a map composite literal. | |
13499 | ||
13500 | Expression* | |
a287720d | 13501 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13502 | Statement_inserter* inserter, |
a287720d | 13503 | Type* type) |
e440a328 | 13504 | { |
b13c66cd | 13505 | Location location = this->location(); |
e440a328 | 13506 | if (this->vals_ != NULL) |
13507 | { | |
13508 | if (!this->has_keys_) | |
13509 | { | |
13510 | error_at(location, "map composite literal must have keys"); | |
13511 | return Expression::make_error(location); | |
13512 | } | |
13513 | ||
a287720d | 13514 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13515 | p != this->vals_->end(); |
13516 | p += 2) | |
13517 | { | |
13518 | if (*p == NULL) | |
13519 | { | |
13520 | ++p; | |
13521 | error_at((*p)->location(), | |
13522 | "map composite literal must have keys for every value"); | |
13523 | return Expression::make_error(location); | |
13524 | } | |
a287720d | 13525 | // Make sure we have lowered the key; it may not have been |
13526 | // lowered in order to handle keys for struct composite | |
13527 | // literals. Lower it now to get the right error message. | |
13528 | if ((*p)->unknown_expression() != NULL) | |
13529 | { | |
13530 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13531 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 13532 | go_assert((*p)->is_error_expression()); |
a287720d | 13533 | return Expression::make_error(location); |
13534 | } | |
e440a328 | 13535 | } |
13536 | } | |
13537 | ||
13538 | return new Map_construction_expression(type, this->vals_, location); | |
13539 | } | |
13540 | ||
d751bb78 | 13541 | // Dump ast representation for a composite literal expression. |
13542 | ||
13543 | void | |
13544 | Composite_literal_expression::do_dump_expression( | |
13545 | Ast_dump_context* ast_dump_context) const | |
13546 | { | |
8b1c301d | 13547 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13548 | ast_dump_context->dump_type(this->type_); |
13549 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13550 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13551 | ast_dump_context->ostream() << "})"; |
13552 | } | |
13553 | ||
e440a328 | 13554 | // Make a composite literal expression. |
13555 | ||
13556 | Expression* | |
13557 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 13558 | Expression_list* vals, bool all_are_names, |
b13c66cd | 13559 | Location location) |
e440a328 | 13560 | { |
13561 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 13562 | all_are_names, location); |
e440a328 | 13563 | } |
13564 | ||
13565 | // Return whether this expression is a composite literal. | |
13566 | ||
13567 | bool | |
13568 | Expression::is_composite_literal() const | |
13569 | { | |
13570 | switch (this->classification_) | |
13571 | { | |
13572 | case EXPRESSION_COMPOSITE_LITERAL: | |
13573 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13574 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
2c809f8f | 13575 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13576 | case EXPRESSION_MAP_CONSTRUCTION: |
13577 | return true; | |
13578 | default: | |
13579 | return false; | |
13580 | } | |
13581 | } | |
13582 | ||
13583 | // Return whether this expression is a composite literal which is not | |
13584 | // constant. | |
13585 | ||
13586 | bool | |
13587 | Expression::is_nonconstant_composite_literal() const | |
13588 | { | |
13589 | switch (this->classification_) | |
13590 | { | |
13591 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13592 | { | |
13593 | const Struct_construction_expression *psce = | |
13594 | static_cast<const Struct_construction_expression*>(this); | |
13595 | return !psce->is_constant_struct(); | |
13596 | } | |
13597 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13598 | { | |
13599 | const Fixed_array_construction_expression *pace = | |
13600 | static_cast<const Fixed_array_construction_expression*>(this); | |
13601 | return !pace->is_constant_array(); | |
13602 | } | |
2c809f8f | 13603 | case EXPRESSION_SLICE_CONSTRUCTION: |
e440a328 | 13604 | { |
2c809f8f | 13605 | const Slice_construction_expression *pace = |
13606 | static_cast<const Slice_construction_expression*>(this); | |
e440a328 | 13607 | return !pace->is_constant_array(); |
13608 | } | |
13609 | case EXPRESSION_MAP_CONSTRUCTION: | |
13610 | return true; | |
13611 | default: | |
13612 | return false; | |
13613 | } | |
13614 | } | |
13615 | ||
35a54f17 | 13616 | // Return true if this is a variable or temporary_variable. |
13617 | ||
13618 | bool | |
13619 | Expression::is_variable() const | |
13620 | { | |
13621 | switch (this->classification_) | |
13622 | { | |
13623 | case EXPRESSION_VAR_REFERENCE: | |
13624 | case EXPRESSION_TEMPORARY_REFERENCE: | |
13625 | case EXPRESSION_SET_AND_USE_TEMPORARY: | |
13626 | return true; | |
13627 | default: | |
13628 | return false; | |
13629 | } | |
13630 | } | |
13631 | ||
e440a328 | 13632 | // Return true if this is a reference to a local variable. |
13633 | ||
13634 | bool | |
13635 | Expression::is_local_variable() const | |
13636 | { | |
13637 | const Var_expression* ve = this->var_expression(); | |
13638 | if (ve == NULL) | |
13639 | return false; | |
13640 | const Named_object* no = ve->named_object(); | |
13641 | return (no->is_result_variable() | |
13642 | || (no->is_variable() && !no->var_value()->is_global())); | |
13643 | } | |
13644 | ||
13645 | // Class Type_guard_expression. | |
13646 | ||
13647 | // Traversal. | |
13648 | ||
13649 | int | |
13650 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13651 | { | |
13652 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13653 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13654 | return TRAVERSE_EXIT; | |
13655 | return TRAVERSE_CONTINUE; | |
13656 | } | |
13657 | ||
2c809f8f | 13658 | Expression* |
13659 | Type_guard_expression::do_flatten(Gogo*, Named_object*, | |
13660 | Statement_inserter* inserter) | |
13661 | { | |
13662 | if (!this->expr_->is_variable()) | |
13663 | { | |
13664 | Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_, | |
13665 | this->location()); | |
13666 | inserter->insert(temp); | |
13667 | this->expr_ = | |
13668 | Expression::make_temporary_reference(temp, this->location()); | |
13669 | } | |
13670 | return this; | |
13671 | } | |
13672 | ||
e440a328 | 13673 | // Check types of a type guard expression. The expression must have |
13674 | // an interface type, but the actual type conversion is checked at run | |
13675 | // time. | |
13676 | ||
13677 | void | |
13678 | Type_guard_expression::do_check_types(Gogo*) | |
13679 | { | |
e440a328 | 13680 | Type* expr_type = this->expr_->type(); |
7e9da23f | 13681 | if (expr_type->interface_type() == NULL) |
f725ade8 | 13682 | { |
5c13bd80 | 13683 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 13684 | this->report_error(_("type assertion only valid for interface types")); |
13685 | this->set_is_error(); | |
13686 | } | |
e440a328 | 13687 | else if (this->type_->interface_type() == NULL) |
13688 | { | |
13689 | std::string reason; | |
13690 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
13691 | &reason)) | |
13692 | { | |
5c13bd80 | 13693 | if (!this->type_->is_error()) |
e440a328 | 13694 | { |
f725ade8 | 13695 | if (reason.empty()) |
13696 | this->report_error(_("impossible type assertion: " | |
13697 | "type does not implement interface")); | |
13698 | else | |
13699 | error_at(this->location(), | |
13700 | ("impossible type assertion: " | |
13701 | "type does not implement interface (%s)"), | |
13702 | reason.c_str()); | |
e440a328 | 13703 | } |
f725ade8 | 13704 | this->set_is_error(); |
e440a328 | 13705 | } |
13706 | } | |
13707 | } | |
13708 | ||
13709 | // Return a tree for a type guard expression. | |
13710 | ||
13711 | tree | |
13712 | Type_guard_expression::do_get_tree(Translate_context* context) | |
13713 | { | |
2c809f8f | 13714 | Expression* conversion; |
7e9da23f | 13715 | if (this->type_->interface_type() != NULL) |
2c809f8f | 13716 | conversion = |
13717 | Expression::convert_interface_to_interface(this->type_, this->expr_, | |
13718 | true, this->location()); | |
e440a328 | 13719 | else |
2c809f8f | 13720 | conversion = |
13721 | Expression::convert_for_assignment(context->gogo(), this->type_, | |
13722 | this->expr_, this->location()); | |
13723 | ||
13724 | return conversion->get_tree(context); | |
e440a328 | 13725 | } |
13726 | ||
d751bb78 | 13727 | // Dump ast representation for a type guard expression. |
13728 | ||
13729 | void | |
2c809f8f | 13730 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) |
d751bb78 | 13731 | const |
13732 | { | |
13733 | this->expr_->dump_expression(ast_dump_context); | |
13734 | ast_dump_context->ostream() << "."; | |
13735 | ast_dump_context->dump_type(this->type_); | |
13736 | } | |
13737 | ||
e440a328 | 13738 | // Make a type guard expression. |
13739 | ||
13740 | Expression* | |
13741 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 13742 | Location location) |
e440a328 | 13743 | { |
13744 | return new Type_guard_expression(expr, type, location); | |
13745 | } | |
13746 | ||
2c809f8f | 13747 | // Class Heap_expression. |
e440a328 | 13748 | |
2c809f8f | 13749 | // When you take the address of an escaping expression, it is allocated |
e440a328 | 13750 | // on the heap. This class implements that. |
13751 | ||
2c809f8f | 13752 | class Heap_expression : public Expression |
e440a328 | 13753 | { |
13754 | public: | |
2c809f8f | 13755 | Heap_expression(Expression* expr, Location location) |
13756 | : Expression(EXPRESSION_HEAP, location), | |
e440a328 | 13757 | expr_(expr) |
13758 | { } | |
13759 | ||
13760 | protected: | |
13761 | int | |
13762 | do_traverse(Traverse* traverse) | |
13763 | { return Expression::traverse(&this->expr_, traverse); } | |
13764 | ||
13765 | Type* | |
13766 | do_type() | |
13767 | { return Type::make_pointer_type(this->expr_->type()); } | |
13768 | ||
13769 | void | |
13770 | do_determine_type(const Type_context*) | |
13771 | { this->expr_->determine_type_no_context(); } | |
13772 | ||
13773 | Expression* | |
13774 | do_copy() | |
13775 | { | |
2c809f8f | 13776 | return Expression::make_heap_expression(this->expr_->copy(), |
13777 | this->location()); | |
e440a328 | 13778 | } |
13779 | ||
13780 | tree | |
13781 | do_get_tree(Translate_context*); | |
13782 | ||
13783 | // We only export global objects, and the parser does not generate | |
13784 | // this in global scope. | |
13785 | void | |
13786 | do_export(Export*) const | |
c3e6f413 | 13787 | { go_unreachable(); } |
e440a328 | 13788 | |
d751bb78 | 13789 | void |
13790 | do_dump_expression(Ast_dump_context*) const; | |
13791 | ||
e440a328 | 13792 | private: |
2c809f8f | 13793 | // The expression which is being put on the heap. |
e440a328 | 13794 | Expression* expr_; |
13795 | }; | |
13796 | ||
2c809f8f | 13797 | // Return a tree which allocates an expression on the heap. |
e440a328 | 13798 | |
13799 | tree | |
2c809f8f | 13800 | Heap_expression::do_get_tree(Translate_context* context) |
e440a328 | 13801 | { |
02c19a1a | 13802 | if (this->expr_->is_error_expression() || this->expr_->type()->is_error()) |
e440a328 | 13803 | return error_mark_node; |
2c809f8f | 13804 | |
02c19a1a | 13805 | Location loc = this->location(); |
2c809f8f | 13806 | Gogo* gogo = context->gogo(); |
02c19a1a | 13807 | Btype* btype = this->type()->get_backend(gogo); |
13808 | Expression* alloc = Expression::make_allocation(this->expr_->type(), loc); | |
13809 | Bexpression* space = tree_to_expr(alloc->get_tree(context)); | |
13810 | ||
13811 | Bstatement* decl; | |
13812 | Named_object* fn = context->function(); | |
13813 | go_assert(fn != NULL); | |
13814 | Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn); | |
13815 | Bvariable* space_temp = | |
13816 | gogo->backend()->temporary_variable(fndecl, context->bblock(), btype, | |
13817 | space, true, loc, &decl); | |
13818 | space = gogo->backend()->var_expression(space_temp, loc); | |
9b27b43c | 13819 | Btype* expr_btype = this->expr_->type()->get_backend(gogo); |
13820 | Bexpression* ref = | |
13821 | gogo->backend()->indirect_expression(expr_btype, space, true, loc); | |
02c19a1a | 13822 | |
13823 | Bexpression* bexpr = tree_to_expr(this->expr_->get_tree(context)); | |
13824 | Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc); | |
13825 | decl = gogo->backend()->compound_statement(decl, assn); | |
13826 | space = gogo->backend()->var_expression(space_temp, loc); | |
13827 | Bexpression* ret = gogo->backend()->compound_expression(decl, space, loc); | |
13828 | return expr_to_tree(ret); | |
e440a328 | 13829 | } |
13830 | ||
2c809f8f | 13831 | // Dump ast representation for a heap expression. |
d751bb78 | 13832 | |
13833 | void | |
2c809f8f | 13834 | Heap_expression::do_dump_expression( |
d751bb78 | 13835 | Ast_dump_context* ast_dump_context) const |
13836 | { | |
13837 | ast_dump_context->ostream() << "&("; | |
13838 | ast_dump_context->dump_expression(this->expr_); | |
13839 | ast_dump_context->ostream() << ")"; | |
13840 | } | |
13841 | ||
2c809f8f | 13842 | // Allocate an expression on the heap. |
e440a328 | 13843 | |
13844 | Expression* | |
2c809f8f | 13845 | Expression::make_heap_expression(Expression* expr, Location location) |
e440a328 | 13846 | { |
2c809f8f | 13847 | return new Heap_expression(expr, location); |
e440a328 | 13848 | } |
13849 | ||
13850 | // Class Receive_expression. | |
13851 | ||
13852 | // Return the type of a receive expression. | |
13853 | ||
13854 | Type* | |
13855 | Receive_expression::do_type() | |
13856 | { | |
13857 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13858 | if (channel_type == NULL) | |
13859 | return Type::make_error_type(); | |
13860 | return channel_type->element_type(); | |
13861 | } | |
13862 | ||
13863 | // Check types for a receive expression. | |
13864 | ||
13865 | void | |
13866 | Receive_expression::do_check_types(Gogo*) | |
13867 | { | |
13868 | Type* type = this->channel_->type(); | |
5c13bd80 | 13869 | if (type->is_error()) |
e440a328 | 13870 | { |
13871 | this->set_is_error(); | |
13872 | return; | |
13873 | } | |
13874 | if (type->channel_type() == NULL) | |
13875 | { | |
13876 | this->report_error(_("expected channel")); | |
13877 | return; | |
13878 | } | |
13879 | if (!type->channel_type()->may_receive()) | |
13880 | { | |
13881 | this->report_error(_("invalid receive on send-only channel")); | |
13882 | return; | |
13883 | } | |
13884 | } | |
13885 | ||
2c809f8f | 13886 | // Flattening for receive expressions creates a temporary variable to store |
13887 | // received data in for receives. | |
13888 | ||
13889 | Expression* | |
13890 | Receive_expression::do_flatten(Gogo*, Named_object*, | |
13891 | Statement_inserter* inserter) | |
13892 | { | |
13893 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13894 | if (channel_type == NULL) | |
13895 | { | |
13896 | go_assert(saw_errors()); | |
13897 | return this; | |
13898 | } | |
13899 | ||
13900 | Type* element_type = channel_type->element_type(); | |
13901 | if (this->temp_receiver_ == NULL) | |
13902 | { | |
13903 | this->temp_receiver_ = Statement::make_temporary(element_type, NULL, | |
13904 | this->location()); | |
13905 | this->temp_receiver_->set_is_address_taken(); | |
13906 | inserter->insert(this->temp_receiver_); | |
13907 | } | |
13908 | ||
13909 | return this; | |
13910 | } | |
13911 | ||
e440a328 | 13912 | // Get a tree for a receive expression. |
13913 | ||
13914 | tree | |
13915 | Receive_expression::do_get_tree(Translate_context* context) | |
13916 | { | |
f24f10bb | 13917 | Location loc = this->location(); |
13918 | ||
e440a328 | 13919 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 13920 | if (channel_type == NULL) |
13921 | { | |
c484d925 | 13922 | go_assert(this->channel_->type()->is_error()); |
5b8368f4 | 13923 | return error_mark_node; |
13924 | } | |
f24f10bb | 13925 | Expression* td = Expression::make_type_descriptor(channel_type, loc); |
e440a328 | 13926 | |
2c809f8f | 13927 | Expression* recv_ref = |
13928 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13929 | Expression* recv_addr = | |
13930 | Expression::make_temporary_reference(this->temp_receiver_, loc); | |
13931 | recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc); | |
13932 | Expression* recv = | |
13933 | Runtime::make_call(Runtime::RECEIVE, loc, 3, | |
13934 | td, this->channel_, recv_addr); | |
13935 | recv = Expression::make_compound(recv, recv_ref, loc); | |
13936 | return recv->get_tree(context); | |
e440a328 | 13937 | } |
13938 | ||
d751bb78 | 13939 | // Dump ast representation for a receive expression. |
13940 | ||
13941 | void | |
13942 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13943 | { | |
13944 | ast_dump_context->ostream() << " <- " ; | |
13945 | ast_dump_context->dump_expression(channel_); | |
13946 | } | |
13947 | ||
e440a328 | 13948 | // Make a receive expression. |
13949 | ||
13950 | Receive_expression* | |
b13c66cd | 13951 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 13952 | { |
13953 | return new Receive_expression(channel, location); | |
13954 | } | |
13955 | ||
e440a328 | 13956 | // An expression which evaluates to a pointer to the type descriptor |
13957 | // of a type. | |
13958 | ||
13959 | class Type_descriptor_expression : public Expression | |
13960 | { | |
13961 | public: | |
b13c66cd | 13962 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 13963 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
13964 | type_(type) | |
13965 | { } | |
13966 | ||
13967 | protected: | |
13968 | Type* | |
13969 | do_type() | |
13970 | { return Type::make_type_descriptor_ptr_type(); } | |
13971 | ||
f9ca30f9 | 13972 | bool |
13973 | do_is_immutable() const | |
13974 | { return true; } | |
13975 | ||
e440a328 | 13976 | void |
13977 | do_determine_type(const Type_context*) | |
13978 | { } | |
13979 | ||
13980 | Expression* | |
13981 | do_copy() | |
13982 | { return this; } | |
13983 | ||
13984 | tree | |
13985 | do_get_tree(Translate_context* context) | |
a1d23b41 | 13986 | { |
175a4612 | 13987 | Bexpression* ret = this->type_->type_descriptor_pointer(context->gogo(), |
13988 | this->location()); | |
13989 | return expr_to_tree(ret); | |
a1d23b41 | 13990 | } |
e440a328 | 13991 | |
d751bb78 | 13992 | void |
13993 | do_dump_expression(Ast_dump_context*) const; | |
13994 | ||
e440a328 | 13995 | private: |
13996 | // The type for which this is the descriptor. | |
13997 | Type* type_; | |
13998 | }; | |
13999 | ||
d751bb78 | 14000 | // Dump ast representation for a type descriptor expression. |
14001 | ||
14002 | void | |
14003 | Type_descriptor_expression::do_dump_expression( | |
14004 | Ast_dump_context* ast_dump_context) const | |
14005 | { | |
14006 | ast_dump_context->dump_type(this->type_); | |
14007 | } | |
14008 | ||
e440a328 | 14009 | // Make a type descriptor expression. |
14010 | ||
14011 | Expression* | |
b13c66cd | 14012 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14013 | { |
14014 | return new Type_descriptor_expression(type, location); | |
14015 | } | |
14016 | ||
14017 | // An expression which evaluates to some characteristic of a type. | |
14018 | // This is only used to initialize fields of a type descriptor. Using | |
14019 | // a new expression class is slightly inefficient but gives us a good | |
14020 | // separation between the frontend and the middle-end with regard to | |
14021 | // how types are laid out. | |
14022 | ||
14023 | class Type_info_expression : public Expression | |
14024 | { | |
14025 | public: | |
14026 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14027 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14028 | type_(type), type_info_(type_info) |
14029 | { } | |
14030 | ||
14031 | protected: | |
0e168074 | 14032 | bool |
14033 | do_is_immutable() const | |
14034 | { return true; } | |
14035 | ||
e440a328 | 14036 | Type* |
14037 | do_type(); | |
14038 | ||
14039 | void | |
14040 | do_determine_type(const Type_context*) | |
14041 | { } | |
14042 | ||
14043 | Expression* | |
14044 | do_copy() | |
14045 | { return this; } | |
14046 | ||
14047 | tree | |
14048 | do_get_tree(Translate_context* context); | |
14049 | ||
d751bb78 | 14050 | void |
14051 | do_dump_expression(Ast_dump_context*) const; | |
14052 | ||
e440a328 | 14053 | private: |
14054 | // The type for which we are getting information. | |
14055 | Type* type_; | |
14056 | // What information we want. | |
14057 | Type_info type_info_; | |
14058 | }; | |
14059 | ||
14060 | // The type is chosen to match what the type descriptor struct | |
14061 | // expects. | |
14062 | ||
14063 | Type* | |
14064 | Type_info_expression::do_type() | |
14065 | { | |
14066 | switch (this->type_info_) | |
14067 | { | |
14068 | case TYPE_INFO_SIZE: | |
14069 | return Type::lookup_integer_type("uintptr"); | |
14070 | case TYPE_INFO_ALIGNMENT: | |
14071 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14072 | return Type::lookup_integer_type("uint8"); | |
14073 | default: | |
c3e6f413 | 14074 | go_unreachable(); |
e440a328 | 14075 | } |
14076 | } | |
14077 | ||
14078 | // Return type information in GENERIC. | |
14079 | ||
14080 | tree | |
14081 | Type_info_expression::do_get_tree(Translate_context* context) | |
14082 | { | |
927a01eb | 14083 | Btype* btype = this->type_->get_backend(context->gogo()); |
14084 | Gogo* gogo = context->gogo(); | |
14085 | size_t val; | |
14086 | switch (this->type_info_) | |
e440a328 | 14087 | { |
927a01eb | 14088 | case TYPE_INFO_SIZE: |
14089 | val = gogo->backend()->type_size(btype); | |
14090 | break; | |
14091 | case TYPE_INFO_ALIGNMENT: | |
14092 | val = gogo->backend()->type_alignment(btype); | |
14093 | break; | |
14094 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14095 | val = gogo->backend()->type_field_alignment(btype); | |
14096 | break; | |
14097 | default: | |
14098 | go_unreachable(); | |
e440a328 | 14099 | } |
287cdcf4 | 14100 | mpz_t cst; |
14101 | mpz_init_set_ui(cst, val); | |
14102 | Btype* int_btype = this->type()->get_backend(gogo); | |
14103 | Bexpression* ret = | |
14104 | gogo->backend()->integer_constant_expression(int_btype, cst); | |
14105 | mpz_clear(cst); | |
14106 | return expr_to_tree(ret); | |
e440a328 | 14107 | } |
14108 | ||
d751bb78 | 14109 | // Dump ast representation for a type info expression. |
14110 | ||
14111 | void | |
14112 | Type_info_expression::do_dump_expression( | |
14113 | Ast_dump_context* ast_dump_context) const | |
14114 | { | |
14115 | ast_dump_context->ostream() << "typeinfo("; | |
14116 | ast_dump_context->dump_type(this->type_); | |
14117 | ast_dump_context->ostream() << ","; | |
14118 | ast_dump_context->ostream() << | |
14119 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14120 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
14121 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
14122 | : "unknown"); | |
14123 | ast_dump_context->ostream() << ")"; | |
14124 | } | |
14125 | ||
e440a328 | 14126 | // Make a type info expression. |
14127 | ||
14128 | Expression* | |
14129 | Expression::make_type_info(Type* type, Type_info type_info) | |
14130 | { | |
14131 | return new Type_info_expression(type, type_info); | |
14132 | } | |
14133 | ||
35a54f17 | 14134 | // An expression that evaluates to some characteristic of a slice. |
14135 | // This is used when indexing, bound-checking, or nil checking a slice. | |
14136 | ||
14137 | class Slice_info_expression : public Expression | |
14138 | { | |
14139 | public: | |
14140 | Slice_info_expression(Expression* slice, Slice_info slice_info, | |
14141 | Location location) | |
14142 | : Expression(EXPRESSION_SLICE_INFO, location), | |
14143 | slice_(slice), slice_info_(slice_info) | |
14144 | { } | |
14145 | ||
14146 | protected: | |
14147 | Type* | |
14148 | do_type(); | |
14149 | ||
14150 | void | |
14151 | do_determine_type(const Type_context*) | |
14152 | { } | |
14153 | ||
14154 | Expression* | |
14155 | do_copy() | |
14156 | { | |
14157 | return new Slice_info_expression(this->slice_->copy(), this->slice_info_, | |
14158 | this->location()); | |
14159 | } | |
14160 | ||
14161 | tree | |
14162 | do_get_tree(Translate_context* context); | |
14163 | ||
14164 | void | |
14165 | do_dump_expression(Ast_dump_context*) const; | |
14166 | ||
14167 | void | |
14168 | do_issue_nil_check() | |
14169 | { this->slice_->issue_nil_check(); } | |
14170 | ||
14171 | private: | |
14172 | // The slice for which we are getting information. | |
14173 | Expression* slice_; | |
14174 | // What information we want. | |
14175 | Slice_info slice_info_; | |
14176 | }; | |
14177 | ||
14178 | // Return the type of the slice info. | |
14179 | ||
14180 | Type* | |
14181 | Slice_info_expression::do_type() | |
14182 | { | |
14183 | switch (this->slice_info_) | |
14184 | { | |
14185 | case SLICE_INFO_VALUE_POINTER: | |
14186 | return Type::make_pointer_type( | |
14187 | this->slice_->type()->array_type()->element_type()); | |
14188 | case SLICE_INFO_LENGTH: | |
14189 | case SLICE_INFO_CAPACITY: | |
14190 | return Type::lookup_integer_type("int"); | |
14191 | default: | |
14192 | go_unreachable(); | |
14193 | } | |
14194 | } | |
14195 | ||
14196 | // Return slice information in GENERIC. | |
14197 | ||
14198 | tree | |
14199 | Slice_info_expression::do_get_tree(Translate_context* context) | |
14200 | { | |
14201 | Gogo* gogo = context->gogo(); | |
14202 | ||
14203 | Bexpression* bslice = tree_to_expr(this->slice_->get_tree(context)); | |
14204 | Bexpression* ret; | |
14205 | switch (this->slice_info_) | |
14206 | { | |
14207 | case SLICE_INFO_VALUE_POINTER: | |
14208 | case SLICE_INFO_LENGTH: | |
14209 | case SLICE_INFO_CAPACITY: | |
14210 | ret = gogo->backend()->struct_field_expression(bslice, this->slice_info_, | |
14211 | this->location()); | |
14212 | break; | |
14213 | default: | |
14214 | go_unreachable(); | |
14215 | } | |
14216 | return expr_to_tree(ret); | |
14217 | } | |
14218 | ||
14219 | // Dump ast representation for a type info expression. | |
14220 | ||
14221 | void | |
14222 | Slice_info_expression::do_dump_expression( | |
14223 | Ast_dump_context* ast_dump_context) const | |
14224 | { | |
14225 | ast_dump_context->ostream() << "sliceinfo("; | |
14226 | this->slice_->dump_expression(ast_dump_context); | |
14227 | ast_dump_context->ostream() << ","; | |
14228 | ast_dump_context->ostream() << | |
14229 | (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values" | |
14230 | : this->slice_info_ == SLICE_INFO_LENGTH ? "length" | |
14231 | : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity " | |
14232 | : "unknown"); | |
14233 | ast_dump_context->ostream() << ")"; | |
14234 | } | |
14235 | ||
14236 | // Make a slice info expression. | |
14237 | ||
14238 | Expression* | |
14239 | Expression::make_slice_info(Expression* slice, Slice_info slice_info, | |
14240 | Location location) | |
14241 | { | |
14242 | return new Slice_info_expression(slice, slice_info, location); | |
14243 | } | |
14244 | ||
2c809f8f | 14245 | // An expression that represents a slice value: a struct with value pointer, |
14246 | // length, and capacity fields. | |
14247 | ||
14248 | class Slice_value_expression : public Expression | |
14249 | { | |
14250 | public: | |
14251 | Slice_value_expression(Type* type, Expression* valptr, Expression* len, | |
14252 | Expression* cap, Location location) | |
14253 | : Expression(EXPRESSION_SLICE_VALUE, location), | |
14254 | type_(type), valptr_(valptr), len_(len), cap_(cap) | |
14255 | { } | |
14256 | ||
14257 | protected: | |
14258 | int | |
14259 | do_traverse(Traverse*); | |
14260 | ||
14261 | Type* | |
14262 | do_type() | |
14263 | { return this->type_; } | |
14264 | ||
14265 | void | |
14266 | do_determine_type(const Type_context*) | |
14267 | { go_unreachable(); } | |
14268 | ||
14269 | Expression* | |
14270 | do_copy() | |
14271 | { | |
14272 | return new Slice_value_expression(this->type_, this->valptr_->copy(), | |
14273 | this->len_->copy(), this->cap_->copy(), | |
14274 | this->location()); | |
14275 | } | |
14276 | ||
14277 | tree | |
14278 | do_get_tree(Translate_context* context); | |
14279 | ||
14280 | void | |
14281 | do_dump_expression(Ast_dump_context*) const; | |
14282 | ||
14283 | private: | |
14284 | // The type of the slice value. | |
14285 | Type* type_; | |
14286 | // The pointer to the values in the slice. | |
14287 | Expression* valptr_; | |
14288 | // The length of the slice. | |
14289 | Expression* len_; | |
14290 | // The capacity of the slice. | |
14291 | Expression* cap_; | |
14292 | }; | |
14293 | ||
14294 | int | |
14295 | Slice_value_expression::do_traverse(Traverse* traverse) | |
14296 | { | |
14297 | if (Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT | |
14298 | || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT | |
14299 | || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT) | |
14300 | return TRAVERSE_EXIT; | |
14301 | return TRAVERSE_CONTINUE; | |
14302 | } | |
14303 | ||
14304 | tree | |
14305 | Slice_value_expression::do_get_tree(Translate_context* context) | |
14306 | { | |
14307 | std::vector<Bexpression*> vals(3); | |
14308 | vals[0] = tree_to_expr(this->valptr_->get_tree(context)); | |
14309 | vals[1] = tree_to_expr(this->len_->get_tree(context)); | |
14310 | vals[2] = tree_to_expr(this->cap_->get_tree(context)); | |
14311 | ||
14312 | Gogo* gogo = context->gogo(); | |
14313 | Btype* btype = this->type_->get_backend(gogo); | |
14314 | Bexpression* ret = | |
14315 | gogo->backend()->constructor_expression(btype, vals, this->location()); | |
14316 | return expr_to_tree(ret); | |
14317 | } | |
14318 | ||
14319 | void | |
14320 | Slice_value_expression::do_dump_expression( | |
14321 | Ast_dump_context* ast_dump_context) const | |
14322 | { | |
14323 | ast_dump_context->ostream() << "slicevalue("; | |
14324 | ast_dump_context->ostream() << "values: "; | |
14325 | this->valptr_->dump_expression(ast_dump_context); | |
14326 | ast_dump_context->ostream() << ", length: "; | |
14327 | this->len_->dump_expression(ast_dump_context); | |
14328 | ast_dump_context->ostream() << ", capacity: "; | |
14329 | this->cap_->dump_expression(ast_dump_context); | |
14330 | ast_dump_context->ostream() << ")"; | |
14331 | } | |
14332 | ||
14333 | Expression* | |
14334 | Expression::make_slice_value(Type* at, Expression* valptr, Expression* len, | |
14335 | Expression* cap, Location location) | |
14336 | { | |
14337 | go_assert(at->is_slice_type()); | |
14338 | return new Slice_value_expression(at, valptr, len, cap, location); | |
14339 | } | |
2387f644 | 14340 | |
14341 | // An expression that evaluates to some characteristic of a non-empty interface. | |
14342 | // This is used to access the method table or underlying object of an interface. | |
14343 | ||
14344 | class Interface_info_expression : public Expression | |
14345 | { | |
14346 | public: | |
14347 | Interface_info_expression(Expression* iface, Interface_info iface_info, | |
2c809f8f | 14348 | Location location) |
2387f644 | 14349 | : Expression(EXPRESSION_INTERFACE_INFO, location), |
14350 | iface_(iface), iface_info_(iface_info) | |
14351 | { } | |
14352 | ||
14353 | protected: | |
14354 | Type* | |
14355 | do_type(); | |
14356 | ||
14357 | void | |
14358 | do_determine_type(const Type_context*) | |
14359 | { } | |
14360 | ||
14361 | Expression* | |
14362 | do_copy() | |
14363 | { | |
14364 | return new Interface_info_expression(this->iface_->copy(), | |
14365 | this->iface_info_, this->location()); | |
14366 | } | |
14367 | ||
14368 | tree | |
14369 | do_get_tree(Translate_context* context); | |
14370 | ||
14371 | void | |
14372 | do_dump_expression(Ast_dump_context*) const; | |
14373 | ||
14374 | void | |
14375 | do_issue_nil_check() | |
14376 | { this->iface_->issue_nil_check(); } | |
14377 | ||
14378 | private: | |
14379 | // The interface for which we are getting information. | |
14380 | Expression* iface_; | |
14381 | // What information we want. | |
14382 | Interface_info iface_info_; | |
14383 | }; | |
14384 | ||
14385 | // Return the type of the interface info. | |
14386 | ||
14387 | Type* | |
14388 | Interface_info_expression::do_type() | |
14389 | { | |
14390 | switch (this->iface_info_) | |
14391 | { | |
14392 | case INTERFACE_INFO_METHODS: | |
14393 | { | |
2c809f8f | 14394 | Type* pdt = Type::make_type_descriptor_ptr_type(); |
14395 | if (this->iface_->type()->interface_type()->is_empty()) | |
14396 | return pdt; | |
14397 | ||
2387f644 | 14398 | Location loc = this->location(); |
14399 | Struct_field_list* sfl = new Struct_field_list(); | |
2387f644 | 14400 | sfl->push_back( |
14401 | Struct_field(Typed_identifier("__type_descriptor", pdt, loc))); | |
14402 | ||
14403 | Interface_type* itype = this->iface_->type()->interface_type(); | |
14404 | for (Typed_identifier_list::const_iterator p = itype->methods()->begin(); | |
14405 | p != itype->methods()->end(); | |
14406 | ++p) | |
14407 | { | |
14408 | Function_type* ft = p->type()->function_type(); | |
14409 | go_assert(ft->receiver() == NULL); | |
14410 | ||
14411 | const Typed_identifier_list* params = ft->parameters(); | |
14412 | Typed_identifier_list* mparams = new Typed_identifier_list(); | |
14413 | if (params != NULL) | |
14414 | mparams->reserve(params->size() + 1); | |
14415 | Type* vt = Type::make_pointer_type(Type::make_void_type()); | |
14416 | mparams->push_back(Typed_identifier("", vt, ft->location())); | |
14417 | if (params != NULL) | |
14418 | { | |
14419 | for (Typed_identifier_list::const_iterator pp = params->begin(); | |
14420 | pp != params->end(); | |
14421 | ++pp) | |
14422 | mparams->push_back(*pp); | |
14423 | } | |
14424 | ||
14425 | Typed_identifier_list* mresults = (ft->results() == NULL | |
14426 | ? NULL | |
14427 | : ft->results()->copy()); | |
14428 | Backend_function_type* mft = | |
14429 | Type::make_backend_function_type(NULL, mparams, mresults, | |
14430 | ft->location()); | |
14431 | ||
14432 | std::string fname = Gogo::unpack_hidden_name(p->name()); | |
14433 | sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc))); | |
14434 | } | |
14435 | ||
14436 | return Type::make_pointer_type(Type::make_struct_type(sfl, loc)); | |
14437 | } | |
14438 | case INTERFACE_INFO_OBJECT: | |
14439 | return Type::make_pointer_type(Type::make_void_type()); | |
14440 | default: | |
14441 | go_unreachable(); | |
14442 | } | |
14443 | } | |
14444 | ||
14445 | // Return interface information in GENERIC. | |
14446 | ||
14447 | tree | |
14448 | Interface_info_expression::do_get_tree(Translate_context* context) | |
14449 | { | |
14450 | Gogo* gogo = context->gogo(); | |
14451 | ||
14452 | Bexpression* biface = tree_to_expr(this->iface_->get_tree(context)); | |
14453 | Bexpression* ret; | |
14454 | switch (this->iface_info_) | |
14455 | { | |
14456 | case INTERFACE_INFO_METHODS: | |
14457 | case INTERFACE_INFO_OBJECT: | |
14458 | ret = gogo->backend()->struct_field_expression(biface, this->iface_info_, | |
14459 | this->location()); | |
14460 | break; | |
14461 | default: | |
14462 | go_unreachable(); | |
14463 | } | |
14464 | return expr_to_tree(ret); | |
14465 | } | |
14466 | ||
14467 | // Dump ast representation for an interface info expression. | |
14468 | ||
14469 | void | |
14470 | Interface_info_expression::do_dump_expression( | |
14471 | Ast_dump_context* ast_dump_context) const | |
14472 | { | |
2c809f8f | 14473 | bool is_empty = this->iface_->type()->interface_type()->is_empty(); |
2387f644 | 14474 | ast_dump_context->ostream() << "interfaceinfo("; |
14475 | this->iface_->dump_expression(ast_dump_context); | |
14476 | ast_dump_context->ostream() << ","; | |
14477 | ast_dump_context->ostream() << | |
2c809f8f | 14478 | (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods" |
14479 | : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor" | |
2387f644 | 14480 | : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object" |
14481 | : "unknown"); | |
14482 | ast_dump_context->ostream() << ")"; | |
14483 | } | |
14484 | ||
14485 | // Make an interface info expression. | |
14486 | ||
14487 | Expression* | |
14488 | Expression::make_interface_info(Expression* iface, Interface_info iface_info, | |
14489 | Location location) | |
14490 | { | |
14491 | return new Interface_info_expression(iface, iface_info, location); | |
14492 | } | |
14493 | ||
2c809f8f | 14494 | // An expression that represents an interface value. The first field is either |
14495 | // a type descriptor for an empty interface or a pointer to the interface method | |
14496 | // table for a non-empty interface. The second field is always the object. | |
14497 | ||
14498 | class Interface_value_expression : public Expression | |
14499 | { | |
14500 | public: | |
14501 | Interface_value_expression(Type* type, Expression* first_field, | |
14502 | Expression* obj, Location location) | |
14503 | : Expression(EXPRESSION_INTERFACE_VALUE, location), | |
14504 | type_(type), first_field_(first_field), obj_(obj) | |
14505 | { } | |
14506 | ||
14507 | protected: | |
14508 | int | |
14509 | do_traverse(Traverse*); | |
14510 | ||
14511 | Type* | |
14512 | do_type() | |
14513 | { return this->type_; } | |
14514 | ||
14515 | void | |
14516 | do_determine_type(const Type_context*) | |
14517 | { go_unreachable(); } | |
14518 | ||
14519 | Expression* | |
14520 | do_copy() | |
14521 | { | |
14522 | return new Interface_value_expression(this->type_, | |
14523 | this->first_field_->copy(), | |
14524 | this->obj_->copy(), this->location()); | |
14525 | } | |
14526 | ||
14527 | tree | |
14528 | do_get_tree(Translate_context* context); | |
14529 | ||
14530 | void | |
14531 | do_dump_expression(Ast_dump_context*) const; | |
14532 | ||
14533 | private: | |
14534 | // The type of the interface value. | |
14535 | Type* type_; | |
14536 | // The first field of the interface (either a type descriptor or a pointer | |
14537 | // to the method table. | |
14538 | Expression* first_field_; | |
14539 | // The underlying object of the interface. | |
14540 | Expression* obj_; | |
14541 | }; | |
14542 | ||
14543 | int | |
14544 | Interface_value_expression::do_traverse(Traverse* traverse) | |
14545 | { | |
14546 | if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT | |
14547 | || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT) | |
14548 | return TRAVERSE_EXIT; | |
14549 | return TRAVERSE_CONTINUE; | |
14550 | } | |
14551 | ||
14552 | tree | |
14553 | Interface_value_expression::do_get_tree(Translate_context* context) | |
14554 | { | |
14555 | std::vector<Bexpression*> vals(2); | |
14556 | vals[0] = tree_to_expr(this->first_field_->get_tree(context)); | |
14557 | vals[1] = tree_to_expr(this->obj_->get_tree(context)); | |
14558 | ||
14559 | Gogo* gogo = context->gogo(); | |
14560 | Btype* btype = this->type_->get_backend(gogo); | |
14561 | Bexpression* ret = | |
14562 | gogo->backend()->constructor_expression(btype, vals, this->location()); | |
14563 | return expr_to_tree(ret); | |
14564 | } | |
14565 | ||
14566 | void | |
14567 | Interface_value_expression::do_dump_expression( | |
14568 | Ast_dump_context* ast_dump_context) const | |
14569 | { | |
14570 | ast_dump_context->ostream() << "interfacevalue("; | |
14571 | ast_dump_context->ostream() << | |
14572 | (this->type_->interface_type()->is_empty() | |
14573 | ? "type_descriptor: " | |
14574 | : "methods: "); | |
14575 | this->first_field_->dump_expression(ast_dump_context); | |
14576 | ast_dump_context->ostream() << ", object: "; | |
14577 | this->obj_->dump_expression(ast_dump_context); | |
14578 | ast_dump_context->ostream() << ")"; | |
14579 | } | |
14580 | ||
14581 | Expression* | |
14582 | Expression::make_interface_value(Type* type, Expression* first_value, | |
14583 | Expression* object, Location location) | |
14584 | { | |
14585 | return new Interface_value_expression(type, first_value, object, location); | |
14586 | } | |
14587 | ||
14588 | // An interface method table for a pair of types: an interface type and a type | |
14589 | // that implements that interface. | |
14590 | ||
14591 | class Interface_mtable_expression : public Expression | |
14592 | { | |
14593 | public: | |
14594 | Interface_mtable_expression(Interface_type* itype, Type* type, | |
14595 | bool is_pointer, Location location) | |
14596 | : Expression(EXPRESSION_INTERFACE_MTABLE, location), | |
14597 | itype_(itype), type_(type), is_pointer_(is_pointer), | |
14598 | method_table_type_(NULL), bvar_(NULL) | |
14599 | { } | |
14600 | ||
14601 | protected: | |
14602 | int | |
14603 | do_traverse(Traverse*); | |
14604 | ||
14605 | Type* | |
14606 | do_type(); | |
14607 | ||
14608 | bool | |
14609 | is_immutable() const | |
14610 | { return true; } | |
14611 | ||
14612 | void | |
14613 | do_determine_type(const Type_context*) | |
14614 | { go_unreachable(); } | |
14615 | ||
14616 | Expression* | |
14617 | do_copy() | |
14618 | { | |
14619 | return new Interface_mtable_expression(this->itype_, this->type_, | |
14620 | this->is_pointer_, this->location()); | |
14621 | } | |
14622 | ||
14623 | bool | |
14624 | do_is_addressable() const | |
14625 | { return true; } | |
14626 | ||
14627 | tree | |
14628 | do_get_tree(Translate_context* context); | |
14629 | ||
14630 | void | |
14631 | do_dump_expression(Ast_dump_context*) const; | |
14632 | ||
14633 | private: | |
14634 | // The interface type for which the methods are defined. | |
14635 | Interface_type* itype_; | |
14636 | // The type to construct the interface method table for. | |
14637 | Type* type_; | |
14638 | // Whether this table contains the method set for the receiver type or the | |
14639 | // pointer receiver type. | |
14640 | bool is_pointer_; | |
14641 | // The type of the method table. | |
14642 | Type* method_table_type_; | |
14643 | // The backend variable that refers to the interface method table. | |
14644 | Bvariable* bvar_; | |
14645 | }; | |
14646 | ||
14647 | int | |
14648 | Interface_mtable_expression::do_traverse(Traverse* traverse) | |
14649 | { | |
14650 | if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT | |
14651 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
14652 | return TRAVERSE_EXIT; | |
14653 | return TRAVERSE_CONTINUE; | |
14654 | } | |
14655 | ||
14656 | Type* | |
14657 | Interface_mtable_expression::do_type() | |
14658 | { | |
14659 | if (this->method_table_type_ != NULL) | |
14660 | return this->method_table_type_; | |
14661 | ||
14662 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14663 | go_assert(!interface_methods->empty()); | |
14664 | ||
14665 | Struct_field_list* sfl = new Struct_field_list; | |
14666 | Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(), | |
14667 | this->location()); | |
14668 | sfl->push_back(Struct_field(tid)); | |
14669 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14670 | p != interface_methods->end(); | |
14671 | ++p) | |
14672 | sfl->push_back(Struct_field(*p)); | |
14673 | this->method_table_type_ = Type::make_struct_type(sfl, this->location()); | |
14674 | return this->method_table_type_; | |
14675 | } | |
14676 | ||
14677 | tree | |
14678 | Interface_mtable_expression::do_get_tree(Translate_context* context) | |
14679 | { | |
14680 | Gogo* gogo = context->gogo(); | |
14681 | Bexpression* ret; | |
14682 | Location loc = Linemap::predeclared_location(); | |
14683 | if (this->bvar_ != NULL) | |
14684 | { | |
14685 | ret = gogo->backend()->var_expression(this->bvar_, this->location()); | |
14686 | return expr_to_tree(ret); | |
14687 | } | |
14688 | ||
14689 | const Typed_identifier_list* interface_methods = this->itype_->methods(); | |
14690 | go_assert(!interface_methods->empty()); | |
14691 | ||
14692 | std::string mangled_name = ((this->is_pointer_ ? "__go_pimt__" : "__go_imt_") | |
14693 | + this->itype_->mangled_name(gogo) | |
14694 | + "__" | |
14695 | + this->type_->mangled_name(gogo)); | |
14696 | ||
14697 | // See whether this interface has any hidden methods. | |
14698 | bool has_hidden_methods = false; | |
14699 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14700 | p != interface_methods->end(); | |
14701 | ++p) | |
14702 | { | |
14703 | if (Gogo::is_hidden_name(p->name())) | |
14704 | { | |
14705 | has_hidden_methods = true; | |
14706 | break; | |
14707 | } | |
14708 | } | |
14709 | ||
14710 | // We already know that the named type is convertible to the | |
14711 | // interface. If the interface has hidden methods, and the named | |
14712 | // type is defined in a different package, then the interface | |
14713 | // conversion table will be defined by that other package. | |
14714 | if (has_hidden_methods | |
14715 | && this->type_->named_type() != NULL | |
14716 | && this->type_->named_type()->named_object()->package() != NULL) | |
14717 | { | |
14718 | Btype* btype = this->type()->get_backend(gogo); | |
14719 | this->bvar_ = | |
14720 | gogo->backend()->immutable_struct_reference(mangled_name, btype, loc); | |
14721 | ret = gogo->backend()->var_expression(this->bvar_, this->location()); | |
14722 | return expr_to_tree(ret); | |
14723 | } | |
14724 | ||
14725 | // The first element is the type descriptor. | |
14726 | Type* td_type; | |
14727 | if (!this->is_pointer_) | |
14728 | td_type = this->type_; | |
14729 | else | |
14730 | td_type = Type::make_pointer_type(this->type_); | |
14731 | ||
14732 | // Build an interface method table for a type: a type descriptor followed by a | |
14733 | // list of function pointers, one for each interface method. This is used for | |
14734 | // interfaces. | |
14735 | Expression_list* svals = new Expression_list(); | |
14736 | svals->push_back(Expression::make_type_descriptor(td_type, loc)); | |
14737 | ||
14738 | Named_type* nt = this->type_->named_type(); | |
14739 | Struct_type* st = this->type_->struct_type(); | |
14740 | go_assert(nt != NULL || st != NULL); | |
14741 | ||
14742 | for (Typed_identifier_list::const_iterator p = interface_methods->begin(); | |
14743 | p != interface_methods->end(); | |
14744 | ++p) | |
14745 | { | |
14746 | bool is_ambiguous; | |
14747 | Method* m; | |
14748 | if (nt != NULL) | |
14749 | m = nt->method_function(p->name(), &is_ambiguous); | |
14750 | else | |
14751 | m = st->method_function(p->name(), &is_ambiguous); | |
14752 | go_assert(m != NULL); | |
14753 | Named_object* no = m->named_object(); | |
14754 | ||
14755 | go_assert(no->is_function() || no->is_function_declaration()); | |
14756 | svals->push_back(Expression::make_func_code_reference(no, loc)); | |
14757 | } | |
14758 | ||
14759 | Btype* btype = this->type()->get_backend(gogo); | |
14760 | Expression* mtable = Expression::make_struct_composite_literal(this->type(), | |
14761 | svals, loc); | |
14762 | Bexpression* ctor = tree_to_expr(mtable->get_tree(context)); | |
14763 | ||
14764 | bool is_public = has_hidden_methods && this->type_->named_type() != NULL; | |
14765 | this->bvar_ = gogo->backend()->immutable_struct(mangled_name, false, | |
14766 | !is_public, btype, loc); | |
14767 | gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false, | |
14768 | !is_public, btype, loc, ctor); | |
14769 | ret = gogo->backend()->var_expression(this->bvar_, loc); | |
14770 | return expr_to_tree(ret); | |
14771 | } | |
14772 | ||
14773 | void | |
14774 | Interface_mtable_expression::do_dump_expression( | |
14775 | Ast_dump_context* ast_dump_context) const | |
14776 | { | |
14777 | ast_dump_context->ostream() << "__go_" | |
14778 | << (this->is_pointer_ ? "pimt__" : "imt_"); | |
14779 | ast_dump_context->dump_type(this->itype_); | |
14780 | ast_dump_context->ostream() << "__"; | |
14781 | ast_dump_context->dump_type(this->type_); | |
14782 | } | |
14783 | ||
14784 | Expression* | |
14785 | Expression::make_interface_mtable_ref(Interface_type* itype, Type* type, | |
14786 | bool is_pointer, Location location) | |
14787 | { | |
14788 | return new Interface_mtable_expression(itype, type, is_pointer, location); | |
14789 | } | |
14790 | ||
e440a328 | 14791 | // An expression which evaluates to the offset of a field within a |
14792 | // struct. This, like Type_info_expression, q.v., is only used to | |
14793 | // initialize fields of a type descriptor. | |
14794 | ||
14795 | class Struct_field_offset_expression : public Expression | |
14796 | { | |
14797 | public: | |
14798 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 14799 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
14800 | Linemap::predeclared_location()), | |
e440a328 | 14801 | type_(type), field_(field) |
14802 | { } | |
14803 | ||
14804 | protected: | |
14805 | Type* | |
14806 | do_type() | |
14807 | { return Type::lookup_integer_type("uintptr"); } | |
14808 | ||
14809 | void | |
14810 | do_determine_type(const Type_context*) | |
14811 | { } | |
14812 | ||
14813 | Expression* | |
14814 | do_copy() | |
14815 | { return this; } | |
14816 | ||
14817 | tree | |
14818 | do_get_tree(Translate_context* context); | |
14819 | ||
d751bb78 | 14820 | void |
14821 | do_dump_expression(Ast_dump_context*) const; | |
14822 | ||
e440a328 | 14823 | private: |
14824 | // The type of the struct. | |
14825 | Struct_type* type_; | |
14826 | // The field. | |
14827 | const Struct_field* field_; | |
14828 | }; | |
14829 | ||
14830 | // Return a struct field offset in GENERIC. | |
14831 | ||
14832 | tree | |
14833 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
14834 | { | |
e440a328 | 14835 | const Struct_field_list* fields = this->type_->fields(); |
e440a328 | 14836 | Struct_field_list::const_iterator p; |
2c8bda43 | 14837 | unsigned i = 0; |
e440a328 | 14838 | for (p = fields->begin(); |
14839 | p != fields->end(); | |
2c8bda43 | 14840 | ++p, ++i) |
14841 | if (&*p == this->field_) | |
14842 | break; | |
c484d925 | 14843 | go_assert(&*p == this->field_); |
e440a328 | 14844 | |
2c8bda43 | 14845 | Gogo* gogo = context->gogo(); |
14846 | Btype* btype = this->type_->get_backend(gogo); | |
14847 | ||
14848 | size_t offset = gogo->backend()->type_field_offset(btype, i); | |
14849 | mpz_t offsetval; | |
14850 | mpz_init_set_ui(offsetval, offset); | |
14851 | Type* uptr_type = Type::lookup_integer_type("uintptr"); | |
14852 | Expression* ret = Expression::make_integer(&offsetval, uptr_type, | |
14853 | Linemap::predeclared_location()); | |
14854 | mpz_clear(offsetval); | |
14855 | return ret->get_tree(context); | |
e440a328 | 14856 | } |
14857 | ||
d751bb78 | 14858 | // Dump ast representation for a struct field offset expression. |
14859 | ||
14860 | void | |
14861 | Struct_field_offset_expression::do_dump_expression( | |
14862 | Ast_dump_context* ast_dump_context) const | |
14863 | { | |
14864 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 14865 | ast_dump_context->dump_type(this->type_); |
14866 | ast_dump_context->ostream() << '.'; | |
14867 | ast_dump_context->ostream() << | |
14868 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 14869 | ast_dump_context->ostream() << ")"; |
14870 | } | |
14871 | ||
e440a328 | 14872 | // Make an expression for a struct field offset. |
14873 | ||
14874 | Expression* | |
14875 | Expression::make_struct_field_offset(Struct_type* type, | |
14876 | const Struct_field* field) | |
14877 | { | |
14878 | return new Struct_field_offset_expression(type, field); | |
14879 | } | |
14880 | ||
a9182619 | 14881 | // An expression which evaluates to a pointer to the map descriptor of |
14882 | // a map type. | |
14883 | ||
14884 | class Map_descriptor_expression : public Expression | |
14885 | { | |
14886 | public: | |
b13c66cd | 14887 | Map_descriptor_expression(Map_type* type, Location location) |
a9182619 | 14888 | : Expression(EXPRESSION_MAP_DESCRIPTOR, location), |
14889 | type_(type) | |
14890 | { } | |
14891 | ||
14892 | protected: | |
14893 | Type* | |
14894 | do_type() | |
14895 | { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } | |
14896 | ||
14897 | void | |
14898 | do_determine_type(const Type_context*) | |
14899 | { } | |
14900 | ||
14901 | Expression* | |
14902 | do_copy() | |
14903 | { return this; } | |
14904 | ||
14905 | tree | |
14906 | do_get_tree(Translate_context* context) | |
14907 | { | |
175a4612 | 14908 | Bexpression* ret = this->type_->map_descriptor_pointer(context->gogo(), |
14909 | this->location()); | |
14910 | return expr_to_tree(ret); | |
a9182619 | 14911 | } |
14912 | ||
d751bb78 | 14913 | void |
14914 | do_dump_expression(Ast_dump_context*) const; | |
14915 | ||
a9182619 | 14916 | private: |
14917 | // The type for which this is the descriptor. | |
14918 | Map_type* type_; | |
14919 | }; | |
14920 | ||
d751bb78 | 14921 | // Dump ast representation for a map descriptor expression. |
14922 | ||
14923 | void | |
14924 | Map_descriptor_expression::do_dump_expression( | |
14925 | Ast_dump_context* ast_dump_context) const | |
14926 | { | |
14927 | ast_dump_context->ostream() << "map_descriptor("; | |
14928 | ast_dump_context->dump_type(this->type_); | |
14929 | ast_dump_context->ostream() << ")"; | |
14930 | } | |
14931 | ||
a9182619 | 14932 | // Make a map descriptor expression. |
14933 | ||
14934 | Expression* | |
b13c66cd | 14935 | Expression::make_map_descriptor(Map_type* type, Location location) |
a9182619 | 14936 | { |
14937 | return new Map_descriptor_expression(type, location); | |
14938 | } | |
14939 | ||
e440a328 | 14940 | // An expression which evaluates to the address of an unnamed label. |
14941 | ||
14942 | class Label_addr_expression : public Expression | |
14943 | { | |
14944 | public: | |
b13c66cd | 14945 | Label_addr_expression(Label* label, Location location) |
e440a328 | 14946 | : Expression(EXPRESSION_LABEL_ADDR, location), |
14947 | label_(label) | |
14948 | { } | |
14949 | ||
14950 | protected: | |
14951 | Type* | |
14952 | do_type() | |
14953 | { return Type::make_pointer_type(Type::make_void_type()); } | |
14954 | ||
14955 | void | |
14956 | do_determine_type(const Type_context*) | |
14957 | { } | |
14958 | ||
14959 | Expression* | |
14960 | do_copy() | |
14961 | { return new Label_addr_expression(this->label_, this->location()); } | |
14962 | ||
14963 | tree | |
6e193e6f | 14964 | do_get_tree(Translate_context* context) |
14965 | { | |
e8816003 | 14966 | return expr_to_tree(this->label_->get_addr(context, this->location())); |
6e193e6f | 14967 | } |
e440a328 | 14968 | |
d751bb78 | 14969 | void |
14970 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14971 | { ast_dump_context->ostream() << this->label_->name(); } | |
14972 | ||
e440a328 | 14973 | private: |
14974 | // The label whose address we are taking. | |
14975 | Label* label_; | |
14976 | }; | |
14977 | ||
14978 | // Make an expression for the address of an unnamed label. | |
14979 | ||
14980 | Expression* | |
b13c66cd | 14981 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 14982 | { |
14983 | return new Label_addr_expression(label, location); | |
14984 | } | |
14985 | ||
283a177b | 14986 | // Conditional expressions. |
14987 | ||
14988 | class Conditional_expression : public Expression | |
14989 | { | |
14990 | public: | |
14991 | Conditional_expression(Expression* cond, Expression* then_expr, | |
14992 | Expression* else_expr, Location location) | |
14993 | : Expression(EXPRESSION_CONDITIONAL, location), | |
14994 | cond_(cond), then_(then_expr), else_(else_expr) | |
14995 | {} | |
14996 | ||
14997 | protected: | |
2c809f8f | 14998 | int |
14999 | do_traverse(Traverse*); | |
15000 | ||
283a177b | 15001 | Type* |
15002 | do_type(); | |
15003 | ||
15004 | void | |
2c809f8f | 15005 | do_determine_type(const Type_context*); |
283a177b | 15006 | |
15007 | Expression* | |
15008 | do_copy() | |
15009 | { | |
15010 | return new Conditional_expression(this->cond_->copy(), this->then_->copy(), | |
15011 | this->else_->copy(), this->location()); | |
15012 | } | |
15013 | ||
15014 | tree | |
15015 | do_get_tree(Translate_context* context); | |
15016 | ||
15017 | void | |
15018 | do_dump_expression(Ast_dump_context*) const; | |
15019 | ||
15020 | private: | |
15021 | // The condition to be checked. | |
15022 | Expression* cond_; | |
15023 | // The expression to execute if the condition is true. | |
15024 | Expression* then_; | |
15025 | // The expression to execute if the condition is false. | |
15026 | Expression* else_; | |
15027 | }; | |
15028 | ||
2c809f8f | 15029 | // Traversal. |
15030 | ||
15031 | int | |
15032 | Conditional_expression::do_traverse(Traverse* traverse) | |
15033 | { | |
15034 | if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT | |
15035 | || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT | |
15036 | || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT) | |
15037 | return TRAVERSE_EXIT; | |
15038 | return TRAVERSE_CONTINUE; | |
15039 | } | |
15040 | ||
283a177b | 15041 | // Return the type of the conditional expression. |
15042 | ||
15043 | Type* | |
15044 | Conditional_expression::do_type() | |
15045 | { | |
15046 | Type* result_type = Type::make_void_type(); | |
2c809f8f | 15047 | if (Type::are_identical(this->then_->type(), this->else_->type(), false, |
15048 | NULL)) | |
283a177b | 15049 | result_type = this->then_->type(); |
15050 | else if (this->then_->is_nil_expression() | |
15051 | || this->else_->is_nil_expression()) | |
15052 | result_type = (!this->then_->is_nil_expression() | |
15053 | ? this->then_->type() | |
15054 | : this->else_->type()); | |
15055 | return result_type; | |
15056 | } | |
15057 | ||
2c809f8f | 15058 | // Determine type for a conditional expression. |
15059 | ||
15060 | void | |
15061 | Conditional_expression::do_determine_type(const Type_context* context) | |
15062 | { | |
15063 | this->cond_->determine_type_no_context(); | |
15064 | this->then_->determine_type(context); | |
15065 | this->else_->determine_type(context); | |
15066 | } | |
15067 | ||
283a177b | 15068 | // Get the backend representation of a conditional expression. |
15069 | ||
15070 | tree | |
15071 | Conditional_expression::do_get_tree(Translate_context* context) | |
15072 | { | |
15073 | Gogo* gogo = context->gogo(); | |
15074 | Btype* result_btype = this->type()->get_backend(gogo); | |
15075 | Bexpression* cond = tree_to_expr(this->cond_->get_tree(context)); | |
15076 | Bexpression* then = tree_to_expr(this->then_->get_tree(context)); | |
15077 | Bexpression* belse = tree_to_expr(this->else_->get_tree(context)); | |
15078 | Bexpression* ret = | |
15079 | gogo->backend()->conditional_expression(result_btype, cond, then, belse, | |
15080 | this->location()); | |
15081 | return expr_to_tree(ret); | |
15082 | } | |
15083 | ||
15084 | // Dump ast representation of a conditional expression. | |
15085 | ||
15086 | void | |
15087 | Conditional_expression::do_dump_expression( | |
15088 | Ast_dump_context* ast_dump_context) const | |
15089 | { | |
15090 | ast_dump_context->ostream() << "("; | |
15091 | ast_dump_context->dump_expression(this->cond_); | |
15092 | ast_dump_context->ostream() << " ? "; | |
15093 | ast_dump_context->dump_expression(this->then_); | |
15094 | ast_dump_context->ostream() << " : "; | |
15095 | ast_dump_context->dump_expression(this->else_); | |
15096 | ast_dump_context->ostream() << ") "; | |
15097 | } | |
15098 | ||
15099 | // Make a conditional expression. | |
15100 | ||
15101 | Expression* | |
15102 | Expression::make_conditional(Expression* cond, Expression* then, | |
15103 | Expression* else_expr, Location location) | |
15104 | { | |
15105 | return new Conditional_expression(cond, then, else_expr, location); | |
15106 | } | |
15107 | ||
2c809f8f | 15108 | // Compound expressions. |
15109 | ||
15110 | class Compound_expression : public Expression | |
15111 | { | |
15112 | public: | |
15113 | Compound_expression(Expression* init, Expression* expr, Location location) | |
15114 | : Expression(EXPRESSION_COMPOUND, location), init_(init), expr_(expr) | |
15115 | {} | |
15116 | ||
15117 | protected: | |
15118 | int | |
15119 | do_traverse(Traverse*); | |
15120 | ||
15121 | Type* | |
15122 | do_type(); | |
15123 | ||
15124 | void | |
15125 | do_determine_type(const Type_context*); | |
15126 | ||
15127 | Expression* | |
15128 | do_copy() | |
15129 | { | |
15130 | return new Compound_expression(this->init_->copy(), this->expr_->copy(), | |
15131 | this->location()); | |
15132 | } | |
15133 | ||
15134 | tree | |
15135 | do_get_tree(Translate_context* context); | |
15136 | ||
15137 | void | |
15138 | do_dump_expression(Ast_dump_context*) const; | |
15139 | ||
15140 | private: | |
15141 | // The expression that is evaluated first and discarded. | |
15142 | Expression* init_; | |
15143 | // The expression that is evaluated and returned. | |
15144 | Expression* expr_; | |
15145 | }; | |
15146 | ||
15147 | // Traversal. | |
15148 | ||
15149 | int | |
15150 | Compound_expression::do_traverse(Traverse* traverse) | |
15151 | { | |
15152 | if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT | |
15153 | || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
15154 | return TRAVERSE_EXIT; | |
15155 | return TRAVERSE_CONTINUE; | |
15156 | } | |
15157 | ||
15158 | // Return the type of the compound expression. | |
15159 | ||
15160 | Type* | |
15161 | Compound_expression::do_type() | |
15162 | { | |
15163 | return this->expr_->type(); | |
15164 | } | |
15165 | ||
15166 | // Determine type for a compound expression. | |
15167 | ||
15168 | void | |
15169 | Compound_expression::do_determine_type(const Type_context* context) | |
15170 | { | |
15171 | this->init_->determine_type_no_context(); | |
15172 | this->expr_->determine_type(context); | |
15173 | } | |
15174 | ||
15175 | // Get the backend representation of a compound expression. | |
15176 | ||
15177 | tree | |
15178 | Compound_expression::do_get_tree(Translate_context* context) | |
15179 | { | |
15180 | Gogo* gogo = context->gogo(); | |
15181 | Bexpression* binit = tree_to_expr(this->init_->get_tree(context)); | |
15182 | Bstatement* init_stmt = gogo->backend()->expression_statement(binit); | |
15183 | Bexpression* bexpr = tree_to_expr(this->expr_->get_tree(context)); | |
15184 | Bexpression* ret = gogo->backend()->compound_expression(init_stmt, bexpr, | |
15185 | this->location()); | |
15186 | return expr_to_tree(ret); | |
15187 | } | |
15188 | ||
15189 | // Dump ast representation of a conditional expression. | |
15190 | ||
15191 | void | |
15192 | Compound_expression::do_dump_expression( | |
15193 | Ast_dump_context* ast_dump_context) const | |
15194 | { | |
15195 | ast_dump_context->ostream() << "("; | |
15196 | ast_dump_context->dump_expression(this->init_); | |
15197 | ast_dump_context->ostream() << ","; | |
15198 | ast_dump_context->dump_expression(this->expr_); | |
15199 | ast_dump_context->ostream() << ") "; | |
15200 | } | |
15201 | ||
15202 | // Make a compound expression. | |
15203 | ||
15204 | Expression* | |
15205 | Expression::make_compound(Expression* init, Expression* expr, Location location) | |
15206 | { | |
15207 | return new Compound_expression(init, expr, location); | |
15208 | } | |
15209 | ||
e440a328 | 15210 | // Import an expression. This comes at the end in order to see the |
15211 | // various class definitions. | |
15212 | ||
15213 | Expression* | |
15214 | Expression::import_expression(Import* imp) | |
15215 | { | |
15216 | int c = imp->peek_char(); | |
15217 | if (imp->match_c_string("- ") | |
15218 | || imp->match_c_string("! ") | |
15219 | || imp->match_c_string("^ ")) | |
15220 | return Unary_expression::do_import(imp); | |
15221 | else if (c == '(') | |
15222 | return Binary_expression::do_import(imp); | |
15223 | else if (imp->match_c_string("true") | |
15224 | || imp->match_c_string("false")) | |
15225 | return Boolean_expression::do_import(imp); | |
15226 | else if (c == '"') | |
15227 | return String_expression::do_import(imp); | |
15228 | else if (c == '-' || (c >= '0' && c <= '9')) | |
15229 | { | |
15230 | // This handles integers, floats and complex constants. | |
15231 | return Integer_expression::do_import(imp); | |
15232 | } | |
15233 | else if (imp->match_c_string("nil")) | |
15234 | return Nil_expression::do_import(imp); | |
15235 | else if (imp->match_c_string("convert")) | |
15236 | return Type_conversion_expression::do_import(imp); | |
15237 | else | |
15238 | { | |
15239 | error_at(imp->location(), "import error: expected expression"); | |
15240 | return Expression::make_error(imp->location()); | |
15241 | } | |
15242 | } | |
15243 | ||
15244 | // Class Expression_list. | |
15245 | ||
15246 | // Traverse the list. | |
15247 | ||
15248 | int | |
15249 | Expression_list::traverse(Traverse* traverse) | |
15250 | { | |
15251 | for (Expression_list::iterator p = this->begin(); | |
15252 | p != this->end(); | |
15253 | ++p) | |
15254 | { | |
15255 | if (*p != NULL) | |
15256 | { | |
15257 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
15258 | return TRAVERSE_EXIT; | |
15259 | } | |
15260 | } | |
15261 | return TRAVERSE_CONTINUE; | |
15262 | } | |
15263 | ||
15264 | // Copy the list. | |
15265 | ||
15266 | Expression_list* | |
15267 | Expression_list::copy() | |
15268 | { | |
15269 | Expression_list* ret = new Expression_list(); | |
15270 | for (Expression_list::iterator p = this->begin(); | |
15271 | p != this->end(); | |
15272 | ++p) | |
15273 | { | |
15274 | if (*p == NULL) | |
15275 | ret->push_back(NULL); | |
15276 | else | |
15277 | ret->push_back((*p)->copy()); | |
15278 | } | |
15279 | return ret; | |
15280 | } | |
15281 | ||
15282 | // Return whether an expression list has an error expression. | |
15283 | ||
15284 | bool | |
15285 | Expression_list::contains_error() const | |
15286 | { | |
15287 | for (Expression_list::const_iterator p = this->begin(); | |
15288 | p != this->end(); | |
15289 | ++p) | |
15290 | if (*p != NULL && (*p)->is_error_expression()) | |
15291 | return true; | |
15292 | return false; | |
15293 | } | |
0c77715b | 15294 | |
15295 | // Class Numeric_constant. | |
15296 | ||
15297 | // Destructor. | |
15298 | ||
15299 | Numeric_constant::~Numeric_constant() | |
15300 | { | |
15301 | this->clear(); | |
15302 | } | |
15303 | ||
15304 | // Copy constructor. | |
15305 | ||
15306 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
15307 | : classification_(a.classification_), type_(a.type_) | |
15308 | { | |
15309 | switch (a.classification_) | |
15310 | { | |
15311 | case NC_INVALID: | |
15312 | break; | |
15313 | case NC_INT: | |
15314 | case NC_RUNE: | |
15315 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15316 | break; | |
15317 | case NC_FLOAT: | |
15318 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15319 | break; | |
15320 | case NC_COMPLEX: | |
15321 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
15322 | GMP_RNDN); | |
15323 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
15324 | GMP_RNDN); | |
15325 | break; | |
15326 | default: | |
15327 | go_unreachable(); | |
15328 | } | |
15329 | } | |
15330 | ||
15331 | // Assignment operator. | |
15332 | ||
15333 | Numeric_constant& | |
15334 | Numeric_constant::operator=(const Numeric_constant& a) | |
15335 | { | |
15336 | this->clear(); | |
15337 | this->classification_ = a.classification_; | |
15338 | this->type_ = a.type_; | |
15339 | switch (a.classification_) | |
15340 | { | |
15341 | case NC_INVALID: | |
15342 | break; | |
15343 | case NC_INT: | |
15344 | case NC_RUNE: | |
15345 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
15346 | break; | |
15347 | case NC_FLOAT: | |
15348 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
15349 | break; | |
15350 | case NC_COMPLEX: | |
15351 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
15352 | GMP_RNDN); | |
15353 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
15354 | GMP_RNDN); | |
15355 | break; | |
15356 | default: | |
15357 | go_unreachable(); | |
15358 | } | |
15359 | return *this; | |
15360 | } | |
15361 | ||
15362 | // Clear the contents. | |
15363 | ||
15364 | void | |
15365 | Numeric_constant::clear() | |
15366 | { | |
15367 | switch (this->classification_) | |
15368 | { | |
15369 | case NC_INVALID: | |
15370 | break; | |
15371 | case NC_INT: | |
15372 | case NC_RUNE: | |
15373 | mpz_clear(this->u_.int_val); | |
15374 | break; | |
15375 | case NC_FLOAT: | |
15376 | mpfr_clear(this->u_.float_val); | |
15377 | break; | |
15378 | case NC_COMPLEX: | |
15379 | mpfr_clear(this->u_.complex_val.real); | |
15380 | mpfr_clear(this->u_.complex_val.imag); | |
15381 | break; | |
15382 | default: | |
15383 | go_unreachable(); | |
15384 | } | |
15385 | this->classification_ = NC_INVALID; | |
15386 | } | |
15387 | ||
15388 | // Set to an unsigned long value. | |
15389 | ||
15390 | void | |
15391 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
15392 | { | |
15393 | this->clear(); | |
15394 | this->classification_ = NC_INT; | |
15395 | this->type_ = type; | |
15396 | mpz_init_set_ui(this->u_.int_val, val); | |
15397 | } | |
15398 | ||
15399 | // Set to an integer value. | |
15400 | ||
15401 | void | |
15402 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
15403 | { | |
15404 | this->clear(); | |
15405 | this->classification_ = NC_INT; | |
15406 | this->type_ = type; | |
15407 | mpz_init_set(this->u_.int_val, val); | |
15408 | } | |
15409 | ||
15410 | // Set to a rune value. | |
15411 | ||
15412 | void | |
15413 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
15414 | { | |
15415 | this->clear(); | |
15416 | this->classification_ = NC_RUNE; | |
15417 | this->type_ = type; | |
15418 | mpz_init_set(this->u_.int_val, val); | |
15419 | } | |
15420 | ||
15421 | // Set to a floating point value. | |
15422 | ||
15423 | void | |
15424 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
15425 | { | |
15426 | this->clear(); | |
15427 | this->classification_ = NC_FLOAT; | |
15428 | this->type_ = type; | |
833b523c | 15429 | // Numeric constants do not have negative zero values, so remove |
15430 | // them here. They also don't have infinity or NaN values, but we | |
15431 | // should never see them here. | |
15432 | if (mpfr_zero_p(val)) | |
15433 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
15434 | else | |
15435 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 15436 | } |
15437 | ||
15438 | // Set to a complex value. | |
15439 | ||
15440 | void | |
15441 | Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag) | |
15442 | { | |
15443 | this->clear(); | |
15444 | this->classification_ = NC_COMPLEX; | |
15445 | this->type_ = type; | |
15446 | mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN); | |
15447 | mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN); | |
15448 | } | |
15449 | ||
15450 | // Get an int value. | |
15451 | ||
15452 | void | |
15453 | Numeric_constant::get_int(mpz_t* val) const | |
15454 | { | |
15455 | go_assert(this->is_int()); | |
15456 | mpz_init_set(*val, this->u_.int_val); | |
15457 | } | |
15458 | ||
15459 | // Get a rune value. | |
15460 | ||
15461 | void | |
15462 | Numeric_constant::get_rune(mpz_t* val) const | |
15463 | { | |
15464 | go_assert(this->is_rune()); | |
15465 | mpz_init_set(*val, this->u_.int_val); | |
15466 | } | |
15467 | ||
15468 | // Get a floating point value. | |
15469 | ||
15470 | void | |
15471 | Numeric_constant::get_float(mpfr_t* val) const | |
15472 | { | |
15473 | go_assert(this->is_float()); | |
15474 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15475 | } | |
15476 | ||
15477 | // Get a complex value. | |
15478 | ||
15479 | void | |
15480 | Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const | |
15481 | { | |
15482 | go_assert(this->is_complex()); | |
15483 | mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN); | |
15484 | mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN); | |
15485 | } | |
15486 | ||
15487 | // Express value as unsigned long if possible. | |
15488 | ||
15489 | Numeric_constant::To_unsigned_long | |
15490 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
15491 | { | |
15492 | switch (this->classification_) | |
15493 | { | |
15494 | case NC_INT: | |
15495 | case NC_RUNE: | |
15496 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
15497 | case NC_FLOAT: | |
15498 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
15499 | case NC_COMPLEX: | |
15500 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15501 | return NC_UL_NOTINT; | |
15502 | return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val); | |
15503 | default: | |
15504 | go_unreachable(); | |
15505 | } | |
15506 | } | |
15507 | ||
15508 | // Express integer value as unsigned long if possible. | |
15509 | ||
15510 | Numeric_constant::To_unsigned_long | |
15511 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
15512 | unsigned long *val) const | |
15513 | { | |
15514 | if (mpz_sgn(ival) < 0) | |
15515 | return NC_UL_NEGATIVE; | |
15516 | unsigned long ui = mpz_get_ui(ival); | |
15517 | if (mpz_cmp_ui(ival, ui) != 0) | |
15518 | return NC_UL_BIG; | |
15519 | *val = ui; | |
15520 | return NC_UL_VALID; | |
15521 | } | |
15522 | ||
15523 | // Express floating point value as unsigned long if possible. | |
15524 | ||
15525 | Numeric_constant::To_unsigned_long | |
15526 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
15527 | unsigned long *val) const | |
15528 | { | |
15529 | if (!mpfr_integer_p(fval)) | |
15530 | return NC_UL_NOTINT; | |
15531 | mpz_t ival; | |
15532 | mpz_init(ival); | |
15533 | mpfr_get_z(ival, fval, GMP_RNDN); | |
15534 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
15535 | mpz_clear(ival); | |
15536 | return ret; | |
15537 | } | |
15538 | ||
15539 | // Convert value to integer if possible. | |
15540 | ||
15541 | bool | |
15542 | Numeric_constant::to_int(mpz_t* val) const | |
15543 | { | |
15544 | switch (this->classification_) | |
15545 | { | |
15546 | case NC_INT: | |
15547 | case NC_RUNE: | |
15548 | mpz_init_set(*val, this->u_.int_val); | |
15549 | return true; | |
15550 | case NC_FLOAT: | |
15551 | if (!mpfr_integer_p(this->u_.float_val)) | |
15552 | return false; | |
15553 | mpz_init(*val); | |
15554 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
15555 | return true; | |
15556 | case NC_COMPLEX: | |
15557 | if (!mpfr_zero_p(this->u_.complex_val.imag) | |
15558 | || !mpfr_integer_p(this->u_.complex_val.real)) | |
15559 | return false; | |
15560 | mpz_init(*val); | |
15561 | mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN); | |
15562 | return true; | |
15563 | default: | |
15564 | go_unreachable(); | |
15565 | } | |
15566 | } | |
15567 | ||
15568 | // Convert value to floating point if possible. | |
15569 | ||
15570 | bool | |
15571 | Numeric_constant::to_float(mpfr_t* val) const | |
15572 | { | |
15573 | switch (this->classification_) | |
15574 | { | |
15575 | case NC_INT: | |
15576 | case NC_RUNE: | |
15577 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
15578 | return true; | |
15579 | case NC_FLOAT: | |
15580 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
15581 | return true; | |
15582 | case NC_COMPLEX: | |
15583 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15584 | return false; | |
15585 | mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN); | |
15586 | return true; | |
15587 | default: | |
15588 | go_unreachable(); | |
15589 | } | |
15590 | } | |
15591 | ||
15592 | // Convert value to complex. | |
15593 | ||
15594 | bool | |
15595 | Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const | |
15596 | { | |
15597 | switch (this->classification_) | |
15598 | { | |
15599 | case NC_INT: | |
15600 | case NC_RUNE: | |
15601 | mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN); | |
15602 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
15603 | return true; | |
15604 | case NC_FLOAT: | |
15605 | mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN); | |
15606 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
15607 | return true; | |
15608 | case NC_COMPLEX: | |
15609 | mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN); | |
15610 | mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN); | |
15611 | return true; | |
15612 | default: | |
15613 | go_unreachable(); | |
15614 | } | |
15615 | } | |
15616 | ||
15617 | // Get the type. | |
15618 | ||
15619 | Type* | |
15620 | Numeric_constant::type() const | |
15621 | { | |
15622 | if (this->type_ != NULL) | |
15623 | return this->type_; | |
15624 | switch (this->classification_) | |
15625 | { | |
15626 | case NC_INT: | |
15627 | return Type::make_abstract_integer_type(); | |
15628 | case NC_RUNE: | |
15629 | return Type::make_abstract_character_type(); | |
15630 | case NC_FLOAT: | |
15631 | return Type::make_abstract_float_type(); | |
15632 | case NC_COMPLEX: | |
15633 | return Type::make_abstract_complex_type(); | |
15634 | default: | |
15635 | go_unreachable(); | |
15636 | } | |
15637 | } | |
15638 | ||
15639 | // If the constant can be expressed in TYPE, then set the type of the | |
15640 | // constant to TYPE and return true. Otherwise return false, and, if | |
15641 | // ISSUE_ERROR is true, report an appropriate error message. | |
15642 | ||
15643 | bool | |
15644 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
15645 | { | |
15646 | bool ret; | |
15647 | if (type == NULL) | |
15648 | ret = true; | |
15649 | else if (type->integer_type() != NULL) | |
15650 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
15651 | else if (type->float_type() != NULL) | |
15652 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
15653 | else if (type->complex_type() != NULL) | |
15654 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
15655 | else | |
15656 | go_unreachable(); | |
15657 | if (ret) | |
15658 | this->type_ = type; | |
15659 | return ret; | |
15660 | } | |
15661 | ||
15662 | // Check whether the constant can be expressed in an integer type. | |
15663 | ||
15664 | bool | |
15665 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
15666 | Location location) const | |
15667 | { | |
15668 | mpz_t val; | |
15669 | switch (this->classification_) | |
15670 | { | |
15671 | case NC_INT: | |
15672 | case NC_RUNE: | |
15673 | mpz_init_set(val, this->u_.int_val); | |
15674 | break; | |
15675 | ||
15676 | case NC_FLOAT: | |
15677 | if (!mpfr_integer_p(this->u_.float_val)) | |
15678 | { | |
15679 | if (issue_error) | |
15680 | error_at(location, "floating point constant truncated to integer"); | |
15681 | return false; | |
15682 | } | |
15683 | mpz_init(val); | |
15684 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
15685 | break; | |
15686 | ||
15687 | case NC_COMPLEX: | |
15688 | if (!mpfr_integer_p(this->u_.complex_val.real) | |
15689 | || !mpfr_zero_p(this->u_.complex_val.imag)) | |
15690 | { | |
15691 | if (issue_error) | |
15692 | error_at(location, "complex constant truncated to integer"); | |
15693 | return false; | |
15694 | } | |
15695 | mpz_init(val); | |
15696 | mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN); | |
15697 | break; | |
15698 | ||
15699 | default: | |
15700 | go_unreachable(); | |
15701 | } | |
15702 | ||
15703 | bool ret; | |
15704 | if (type->is_abstract()) | |
15705 | ret = true; | |
15706 | else | |
15707 | { | |
15708 | int bits = mpz_sizeinbase(val, 2); | |
15709 | if (type->is_unsigned()) | |
15710 | { | |
15711 | // For an unsigned type we can only accept a nonnegative | |
15712 | // number, and we must be able to represents at least BITS. | |
15713 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
15714 | } | |
15715 | else | |
15716 | { | |
15717 | // For a signed type we need an extra bit to indicate the | |
15718 | // sign. We have to handle the most negative integer | |
15719 | // specially. | |
15720 | ret = (bits + 1 <= type->bits() | |
15721 | || (bits <= type->bits() | |
15722 | && mpz_sgn(val) < 0 | |
15723 | && (mpz_scan1(val, 0) | |
15724 | == static_cast<unsigned long>(type->bits() - 1)) | |
15725 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
15726 | } | |
15727 | } | |
15728 | ||
15729 | if (!ret && issue_error) | |
15730 | error_at(location, "integer constant overflow"); | |
15731 | ||
15732 | return ret; | |
15733 | } | |
15734 | ||
15735 | // Check whether the constant can be expressed in a floating point | |
15736 | // type. | |
15737 | ||
15738 | bool | |
15739 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 15740 | Location location) |
0c77715b | 15741 | { |
15742 | mpfr_t val; | |
15743 | switch (this->classification_) | |
15744 | { | |
15745 | case NC_INT: | |
15746 | case NC_RUNE: | |
15747 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
15748 | break; | |
15749 | ||
15750 | case NC_FLOAT: | |
15751 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
15752 | break; | |
15753 | ||
15754 | case NC_COMPLEX: | |
15755 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15756 | { | |
15757 | if (issue_error) | |
15758 | error_at(location, "complex constant truncated to float"); | |
15759 | return false; | |
15760 | } | |
15761 | mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN); | |
15762 | break; | |
15763 | ||
15764 | default: | |
15765 | go_unreachable(); | |
15766 | } | |
15767 | ||
15768 | bool ret; | |
15769 | if (type->is_abstract()) | |
15770 | ret = true; | |
15771 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
15772 | { | |
15773 | // A NaN or Infinity always fits in the range of the type. | |
15774 | ret = true; | |
15775 | } | |
15776 | else | |
15777 | { | |
15778 | mp_exp_t exp = mpfr_get_exp(val); | |
15779 | mp_exp_t max_exp; | |
15780 | switch (type->bits()) | |
15781 | { | |
15782 | case 32: | |
15783 | max_exp = 128; | |
15784 | break; | |
15785 | case 64: | |
15786 | max_exp = 1024; | |
15787 | break; | |
15788 | default: | |
15789 | go_unreachable(); | |
15790 | } | |
15791 | ||
15792 | ret = exp <= max_exp; | |
d0bcce51 | 15793 | |
15794 | if (ret) | |
15795 | { | |
15796 | // Round the constant to the desired type. | |
15797 | mpfr_t t; | |
15798 | mpfr_init(t); | |
15799 | switch (type->bits()) | |
15800 | { | |
15801 | case 32: | |
15802 | mpfr_set_prec(t, 24); | |
15803 | break; | |
15804 | case 64: | |
15805 | mpfr_set_prec(t, 53); | |
15806 | break; | |
15807 | default: | |
15808 | go_unreachable(); | |
15809 | } | |
15810 | mpfr_set(t, val, GMP_RNDN); | |
15811 | mpfr_set(val, t, GMP_RNDN); | |
15812 | mpfr_clear(t); | |
15813 | ||
15814 | this->set_float(type, val); | |
15815 | } | |
0c77715b | 15816 | } |
15817 | ||
15818 | mpfr_clear(val); | |
15819 | ||
15820 | if (!ret && issue_error) | |
15821 | error_at(location, "floating point constant overflow"); | |
15822 | ||
15823 | return ret; | |
15824 | } | |
15825 | ||
15826 | // Check whether the constant can be expressed in a complex type. | |
15827 | ||
15828 | bool | |
15829 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 15830 | Location location) |
0c77715b | 15831 | { |
15832 | if (type->is_abstract()) | |
15833 | return true; | |
15834 | ||
15835 | mp_exp_t max_exp; | |
15836 | switch (type->bits()) | |
15837 | { | |
15838 | case 64: | |
15839 | max_exp = 128; | |
15840 | break; | |
15841 | case 128: | |
15842 | max_exp = 1024; | |
15843 | break; | |
15844 | default: | |
15845 | go_unreachable(); | |
15846 | } | |
15847 | ||
15848 | mpfr_t real; | |
d0bcce51 | 15849 | mpfr_t imag; |
0c77715b | 15850 | switch (this->classification_) |
15851 | { | |
15852 | case NC_INT: | |
15853 | case NC_RUNE: | |
15854 | mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN); | |
d0bcce51 | 15855 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 15856 | break; |
15857 | ||
15858 | case NC_FLOAT: | |
15859 | mpfr_init_set(real, this->u_.float_val, GMP_RNDN); | |
d0bcce51 | 15860 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 15861 | break; |
15862 | ||
15863 | case NC_COMPLEX: | |
0c77715b | 15864 | mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN); |
d0bcce51 | 15865 | mpfr_init_set(imag, this->u_.complex_val.imag, GMP_RNDN); |
0c77715b | 15866 | break; |
15867 | ||
15868 | default: | |
15869 | go_unreachable(); | |
15870 | } | |
15871 | ||
d0bcce51 | 15872 | bool ret = true; |
15873 | if (!mpfr_nan_p(real) | |
15874 | && !mpfr_inf_p(real) | |
15875 | && !mpfr_zero_p(real) | |
15876 | && mpfr_get_exp(real) > max_exp) | |
15877 | { | |
15878 | if (issue_error) | |
15879 | error_at(location, "complex real part overflow"); | |
15880 | ret = false; | |
15881 | } | |
0c77715b | 15882 | |
d0bcce51 | 15883 | if (!mpfr_nan_p(imag) |
15884 | && !mpfr_inf_p(imag) | |
15885 | && !mpfr_zero_p(imag) | |
15886 | && mpfr_get_exp(imag) > max_exp) | |
15887 | { | |
15888 | if (issue_error) | |
15889 | error_at(location, "complex imaginary part overflow"); | |
15890 | ret = false; | |
15891 | } | |
0c77715b | 15892 | |
d0bcce51 | 15893 | if (ret) |
15894 | { | |
15895 | // Round the constant to the desired type. | |
15896 | mpfr_t t; | |
15897 | mpfr_init(t); | |
15898 | switch (type->bits()) | |
15899 | { | |
15900 | case 64: | |
15901 | mpfr_set_prec(t, 24); | |
15902 | break; | |
15903 | case 128: | |
15904 | mpfr_set_prec(t, 53); | |
15905 | break; | |
15906 | default: | |
15907 | go_unreachable(); | |
15908 | } | |
15909 | mpfr_set(t, real, GMP_RNDN); | |
15910 | mpfr_set(real, t, GMP_RNDN); | |
15911 | mpfr_set(t, imag, GMP_RNDN); | |
15912 | mpfr_set(imag, t, GMP_RNDN); | |
15913 | mpfr_clear(t); | |
15914 | ||
15915 | this->set_complex(type, real, imag); | |
15916 | } | |
15917 | ||
15918 | mpfr_clear(real); | |
15919 | mpfr_clear(imag); | |
0c77715b | 15920 | |
15921 | return ret; | |
15922 | } | |
15923 | ||
15924 | // Return an Expression for this value. | |
15925 | ||
15926 | Expression* | |
15927 | Numeric_constant::expression(Location loc) const | |
15928 | { | |
15929 | switch (this->classification_) | |
15930 | { | |
15931 | case NC_INT: | |
15932 | return Expression::make_integer(&this->u_.int_val, this->type_, loc); | |
15933 | case NC_RUNE: | |
15934 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
15935 | case NC_FLOAT: | |
15936 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
15937 | case NC_COMPLEX: | |
15938 | return Expression::make_complex(&this->u_.complex_val.real, | |
15939 | &this->u_.complex_val.imag, | |
15940 | this->type_, loc); | |
15941 | default: | |
15942 | go_unreachable(); | |
15943 | } | |
15944 | } |