]>
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 <gmp.h> |
12 | ||
e440a328 | 13 | #include "toplev.h" |
14 | #include "intl.h" | |
15 | #include "tree.h" | |
16 | #include "gimple.h" | |
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 | ||
143 | // Return a tree handling any conversions which must be done during | |
144 | // assignment. | |
145 | ||
146 | tree | |
147 | Expression::convert_for_assignment(Translate_context* context, Type* lhs_type, | |
148 | Type* rhs_type, tree rhs_tree, | |
b13c66cd | 149 | Location location) |
e440a328 | 150 | { |
5c13bd80 | 151 | if (lhs_type->is_error() || rhs_type->is_error()) |
e440a328 | 152 | return error_mark_node; |
153 | ||
e440a328 | 154 | if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node) |
155 | return error_mark_node; | |
156 | ||
157 | Gogo* gogo = context->gogo(); | |
158 | ||
9f0e0513 | 159 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 160 | if (lhs_type_tree == error_mark_node) |
161 | return error_mark_node; | |
162 | ||
54211955 | 163 | if (lhs_type->forwarded() != rhs_type->forwarded() |
164 | && lhs_type->interface_type() != NULL) | |
e440a328 | 165 | { |
166 | if (rhs_type->interface_type() == NULL) | |
167 | return Expression::convert_type_to_interface(context, lhs_type, | |
168 | rhs_type, rhs_tree, | |
169 | location); | |
170 | else | |
171 | return Expression::convert_interface_to_interface(context, lhs_type, | |
172 | rhs_type, rhs_tree, | |
173 | false, location); | |
174 | } | |
54211955 | 175 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
176 | && rhs_type->interface_type() != NULL) | |
e440a328 | 177 | return Expression::convert_interface_to_type(context, lhs_type, rhs_type, |
178 | rhs_tree, location); | |
411eb89e | 179 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 180 | { |
181 | // Assigning nil to an open array. | |
c484d925 | 182 | go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE); |
e440a328 | 183 | |
95f84544 | 184 | vec<constructor_elt, va_gc> *init; |
185 | vec_alloc(init, 3); | |
e440a328 | 186 | |
e82e4eb5 | 187 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 188 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 189 | tree field = TYPE_FIELDS(lhs_type_tree); |
c484d925 | 190 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 191 | "__values") == 0); |
192 | elt->index = field; | |
193 | elt->value = fold_convert(TREE_TYPE(field), null_pointer_node); | |
194 | ||
95f84544 | 195 | elt = init->quick_push(empty); |
e440a328 | 196 | field = DECL_CHAIN(field); |
c484d925 | 197 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 198 | "__count") == 0); |
199 | elt->index = field; | |
200 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
201 | ||
95f84544 | 202 | elt = init->quick_push(empty); |
e440a328 | 203 | field = DECL_CHAIN(field); |
c484d925 | 204 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 205 | "__capacity") == 0); |
206 | elt->index = field; | |
207 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
208 | ||
209 | tree val = build_constructor(lhs_type_tree, init); | |
210 | TREE_CONSTANT(val) = 1; | |
211 | ||
212 | return val; | |
213 | } | |
214 | else if (rhs_type->is_nil_type()) | |
215 | { | |
216 | // The left hand side should be a pointer type at the tree | |
217 | // level. | |
c484d925 | 218 | go_assert(POINTER_TYPE_P(lhs_type_tree)); |
e440a328 | 219 | return fold_convert(lhs_type_tree, null_pointer_node); |
220 | } | |
221 | else if (lhs_type_tree == TREE_TYPE(rhs_tree)) | |
222 | { | |
223 | // No conversion is needed. | |
224 | return rhs_tree; | |
225 | } | |
226 | else if (POINTER_TYPE_P(lhs_type_tree) | |
227 | || INTEGRAL_TYPE_P(lhs_type_tree) | |
228 | || SCALAR_FLOAT_TYPE_P(lhs_type_tree) | |
229 | || COMPLEX_FLOAT_TYPE_P(lhs_type_tree)) | |
b13c66cd | 230 | return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree); |
3e785901 | 231 | else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE |
232 | && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE) | |
233 | || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE | |
234 | && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE)) | |
e440a328 | 235 | { |
bb92f513 | 236 | // Avoid confusion from zero sized variables which may be |
237 | // represented as non-zero-sized. | |
238 | if (int_size_in_bytes(lhs_type_tree) == 0 | |
239 | || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0) | |
240 | return rhs_tree; | |
241 | ||
e440a328 | 242 | // This conversion must be permitted by Go, or we wouldn't have |
243 | // gotten here. | |
c484d925 | 244 | go_assert(int_size_in_bytes(lhs_type_tree) |
bb92f513 | 245 | == int_size_in_bytes(TREE_TYPE(rhs_tree))); |
b13c66cd | 246 | return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR, |
247 | lhs_type_tree, rhs_tree); | |
e440a328 | 248 | } |
249 | else | |
250 | { | |
c484d925 | 251 | go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree))); |
e440a328 | 252 | return rhs_tree; |
253 | } | |
254 | } | |
255 | ||
256 | // Return a tree for a conversion from a non-interface type to an | |
257 | // interface type. | |
258 | ||
259 | tree | |
260 | Expression::convert_type_to_interface(Translate_context* context, | |
261 | Type* lhs_type, Type* rhs_type, | |
b13c66cd | 262 | tree rhs_tree, Location location) |
e440a328 | 263 | { |
264 | Gogo* gogo = context->gogo(); | |
265 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
266 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
267 | ||
268 | // Since RHS_TYPE is a static type, we can create the interface | |
269 | // method table at compile time. | |
270 | ||
271 | // When setting an interface to nil, we just set both fields to | |
272 | // NULL. | |
273 | if (rhs_type->is_nil_type()) | |
63697958 | 274 | { |
275 | Btype* lhs_btype = lhs_type->get_backend(gogo); | |
276 | return expr_to_tree(gogo->backend()->zero_expression(lhs_btype)); | |
277 | } | |
e440a328 | 278 | |
279 | // This should have been checked already. | |
c484d925 | 280 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 281 | |
9f0e0513 | 282 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 283 | if (lhs_type_tree == error_mark_node) |
284 | return error_mark_node; | |
285 | ||
286 | // An interface is a tuple. If LHS_TYPE is an empty interface type, | |
287 | // then the first field is the type descriptor for RHS_TYPE. | |
288 | // Otherwise it is the interface method table for RHS_TYPE. | |
289 | tree first_field_value; | |
290 | if (lhs_is_empty) | |
a1d23b41 | 291 | first_field_value = rhs_type->type_descriptor_pointer(gogo, location); |
e440a328 | 292 | else |
293 | { | |
294 | // Build the interface method table for this interface and this | |
295 | // object type: a list of function pointers for each interface | |
296 | // method. | |
297 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 298 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 299 | bool is_pointer = false; |
c0cab2ec | 300 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 301 | { |
302 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 303 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 304 | is_pointer = true; |
305 | } | |
306 | tree method_table; | |
c0cab2ec | 307 | if (rhs_named_type != NULL) |
e440a328 | 308 | method_table = |
309 | rhs_named_type->interface_method_table(gogo, lhs_interface_type, | |
310 | is_pointer); | |
c0cab2ec | 311 | else if (rhs_struct_type != NULL) |
312 | method_table = | |
313 | rhs_struct_type->interface_method_table(gogo, lhs_interface_type, | |
314 | is_pointer); | |
315 | else | |
316 | method_table = null_pointer_node; | |
b13c66cd | 317 | first_field_value = fold_convert_loc(location.gcc_location(), |
318 | const_ptr_type_node, method_table); | |
e440a328 | 319 | } |
84b7d3c6 | 320 | if (first_field_value == error_mark_node) |
321 | return error_mark_node; | |
e440a328 | 322 | |
323 | // Start building a constructor for the value we will return. | |
324 | ||
95f84544 | 325 | vec<constructor_elt, va_gc> *init; |
326 | vec_alloc(init, 2); | |
e440a328 | 327 | |
e82e4eb5 | 328 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 329 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 330 | tree field = TYPE_FIELDS(lhs_type_tree); |
c484d925 | 331 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 332 | (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0); |
333 | elt->index = field; | |
b13c66cd | 334 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
335 | first_field_value); | |
e440a328 | 336 | |
95f84544 | 337 | elt = init->quick_push(empty); |
e440a328 | 338 | field = DECL_CHAIN(field); |
c484d925 | 339 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 340 | elt->index = field; |
341 | ||
342 | if (rhs_type->points_to() != NULL) | |
343 | { | |
344 | // We are assigning a pointer to the interface; the interface | |
345 | // holds the pointer itself. | |
346 | elt->value = rhs_tree; | |
347 | return build_constructor(lhs_type_tree, init); | |
348 | } | |
349 | ||
350 | // We are assigning a non-pointer value to the interface; the | |
351 | // interface gets a copy of the value in the heap. | |
352 | ||
353 | tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree)); | |
354 | ||
355 | tree space = gogo->allocate_memory(rhs_type, object_size, location); | |
b13c66cd | 356 | space = fold_convert_loc(location.gcc_location(), |
357 | build_pointer_type(TREE_TYPE(rhs_tree)), space); | |
e440a328 | 358 | space = save_expr(space); |
359 | ||
b13c66cd | 360 | tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space); |
e440a328 | 361 | TREE_THIS_NOTRAP(ref) = 1; |
b13c66cd | 362 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
363 | void_type_node, ref, rhs_tree); | |
e440a328 | 364 | |
b13c66cd | 365 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
366 | space); | |
e440a328 | 367 | |
368 | return build2(COMPOUND_EXPR, lhs_type_tree, set, | |
369 | build_constructor(lhs_type_tree, init)); | |
370 | } | |
371 | ||
372 | // Return a tree for the type descriptor of RHS_TREE, which has | |
373 | // interface type RHS_TYPE. If RHS_TREE is nil the result will be | |
374 | // NULL. | |
375 | ||
376 | tree | |
377 | Expression::get_interface_type_descriptor(Translate_context*, | |
378 | Type* rhs_type, tree rhs_tree, | |
b13c66cd | 379 | Location location) |
e440a328 | 380 | { |
381 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
c484d925 | 382 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 383 | tree rhs_field = TYPE_FIELDS(rhs_type_tree); |
384 | tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
385 | NULL_TREE); | |
386 | if (rhs_type->interface_type()->is_empty()) | |
387 | { | |
c484d925 | 388 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), |
e440a328 | 389 | "__type_descriptor") == 0); |
390 | return v; | |
391 | } | |
392 | ||
c484d925 | 393 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods") |
e440a328 | 394 | == 0); |
c484d925 | 395 | go_assert(POINTER_TYPE_P(TREE_TYPE(v))); |
e440a328 | 396 | v = save_expr(v); |
b13c66cd | 397 | tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v); |
c484d925 | 398 | go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE); |
e440a328 | 399 | tree f = TYPE_FIELDS(TREE_TYPE(v1)); |
c484d925 | 400 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor") |
e440a328 | 401 | == 0); |
402 | v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE); | |
403 | ||
b13c66cd | 404 | tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node, |
405 | v, fold_convert_loc(location.gcc_location(), | |
406 | TREE_TYPE(v), | |
407 | null_pointer_node)); | |
408 | tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1), | |
409 | null_pointer_node); | |
410 | return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1), | |
e440a328 | 411 | eq, n, v1); |
412 | } | |
413 | ||
414 | // Return a tree for the conversion of an interface type to an | |
415 | // interface type. | |
416 | ||
417 | tree | |
418 | Expression::convert_interface_to_interface(Translate_context* context, | |
419 | Type *lhs_type, Type *rhs_type, | |
420 | tree rhs_tree, bool for_type_guard, | |
b13c66cd | 421 | Location location) |
e440a328 | 422 | { |
423 | Gogo* gogo = context->gogo(); | |
424 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
425 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
426 | ||
9f0e0513 | 427 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 428 | if (lhs_type_tree == error_mark_node) |
429 | return error_mark_node; | |
430 | ||
431 | // In the general case this requires runtime examination of the type | |
432 | // method table to match it up with the interface methods. | |
433 | ||
434 | // FIXME: If all of the methods in the right hand side interface | |
435 | // also appear in the left hand side interface, then we don't need | |
436 | // to do a runtime check, although we still need to build a new | |
437 | // method table. | |
438 | ||
439 | // Get the type descriptor for the right hand side. This will be | |
440 | // NULL for a nil interface. | |
441 | ||
442 | if (!DECL_P(rhs_tree)) | |
443 | rhs_tree = save_expr(rhs_tree); | |
444 | ||
445 | tree rhs_type_descriptor = | |
446 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
447 | location); | |
448 | ||
449 | // The result is going to be a two element constructor. | |
450 | ||
95f84544 | 451 | vec<constructor_elt, va_gc> *init; |
452 | vec_alloc (init, 2); | |
e440a328 | 453 | |
e82e4eb5 | 454 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 455 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 456 | tree field = TYPE_FIELDS(lhs_type_tree); |
457 | elt->index = field; | |
458 | ||
459 | if (for_type_guard) | |
460 | { | |
461 | // A type assertion fails when converting a nil interface. | |
a1d23b41 | 462 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, |
463 | location); | |
e440a328 | 464 | static tree assert_interface_decl; |
465 | tree call = Gogo::call_builtin(&assert_interface_decl, | |
466 | location, | |
467 | "__go_assert_interface", | |
468 | 2, | |
469 | ptr_type_node, | |
470 | TREE_TYPE(lhs_type_descriptor), | |
471 | lhs_type_descriptor, | |
472 | TREE_TYPE(rhs_type_descriptor), | |
473 | rhs_type_descriptor); | |
5fb82b5e | 474 | if (call == error_mark_node) |
475 | return error_mark_node; | |
e440a328 | 476 | // This will panic if the interface conversion fails. |
477 | TREE_NOTHROW(assert_interface_decl) = 0; | |
b13c66cd | 478 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
479 | call); | |
e440a328 | 480 | } |
481 | else if (lhs_is_empty) | |
482 | { | |
483 | // A convertion to an empty interface always succeeds, and the | |
484 | // first field is just the type descriptor of the object. | |
c484d925 | 485 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 486 | "__type_descriptor") == 0); |
7172c949 | 487 | elt->value = fold_convert_loc(location.gcc_location(), |
488 | TREE_TYPE(field), rhs_type_descriptor); | |
e440a328 | 489 | } |
490 | else | |
491 | { | |
492 | // A conversion to a non-empty interface may fail, but unlike a | |
493 | // type assertion converting nil will always succeed. | |
c484d925 | 494 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") |
e440a328 | 495 | == 0); |
a1d23b41 | 496 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, |
497 | location); | |
e440a328 | 498 | static tree convert_interface_decl; |
499 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
500 | location, | |
501 | "__go_convert_interface", | |
502 | 2, | |
503 | ptr_type_node, | |
504 | TREE_TYPE(lhs_type_descriptor), | |
505 | lhs_type_descriptor, | |
506 | TREE_TYPE(rhs_type_descriptor), | |
507 | rhs_type_descriptor); | |
5fb82b5e | 508 | if (call == error_mark_node) |
509 | return error_mark_node; | |
e440a328 | 510 | // This will panic if the interface conversion fails. |
511 | TREE_NOTHROW(convert_interface_decl) = 0; | |
b13c66cd | 512 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
513 | call); | |
e440a328 | 514 | } |
515 | ||
516 | // The second field is simply the object pointer. | |
517 | ||
95f84544 | 518 | elt = init->quick_push(empty); |
e440a328 | 519 | field = DECL_CHAIN(field); |
c484d925 | 520 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 521 | elt->index = field; |
522 | ||
523 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
c484d925 | 524 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 525 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); |
c484d925 | 526 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); |
e440a328 | 527 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, |
528 | NULL_TREE); | |
529 | ||
530 | return build_constructor(lhs_type_tree, init); | |
531 | } | |
532 | ||
533 | // Return a tree for the conversion of an interface type to a | |
534 | // non-interface type. | |
535 | ||
536 | tree | |
537 | Expression::convert_interface_to_type(Translate_context* context, | |
538 | Type *lhs_type, Type* rhs_type, | |
b13c66cd | 539 | tree rhs_tree, Location location) |
e440a328 | 540 | { |
541 | Gogo* gogo = context->gogo(); | |
542 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
543 | ||
9f0e0513 | 544 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 545 | if (lhs_type_tree == error_mark_node) |
546 | return error_mark_node; | |
547 | ||
548 | // Call a function to check that the type is valid. The function | |
549 | // will panic with an appropriate runtime type error if the type is | |
550 | // not valid. | |
551 | ||
a1d23b41 | 552 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location); |
e440a328 | 553 | |
554 | if (!DECL_P(rhs_tree)) | |
555 | rhs_tree = save_expr(rhs_tree); | |
556 | ||
557 | tree rhs_type_descriptor = | |
558 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
559 | location); | |
560 | ||
a1d23b41 | 561 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo, |
562 | location); | |
e440a328 | 563 | |
564 | static tree check_interface_type_decl; | |
565 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
566 | location, | |
567 | "__go_check_interface_type", | |
568 | 3, | |
569 | void_type_node, | |
570 | TREE_TYPE(lhs_type_descriptor), | |
571 | lhs_type_descriptor, | |
572 | TREE_TYPE(rhs_type_descriptor), | |
573 | rhs_type_descriptor, | |
574 | TREE_TYPE(rhs_inter_descriptor), | |
575 | rhs_inter_descriptor); | |
5fb82b5e | 576 | if (call == error_mark_node) |
577 | return error_mark_node; | |
e440a328 | 578 | // This call will panic if the conversion is invalid. |
579 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
580 | ||
581 | // If the call succeeds, pull out the value. | |
c484d925 | 582 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 583 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); |
c484d925 | 584 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); |
e440a328 | 585 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, |
586 | NULL_TREE); | |
587 | ||
588 | // If the value is a pointer, then it is the value we want. | |
589 | // Otherwise it points to the value. | |
590 | if (lhs_type->points_to() == NULL) | |
591 | { | |
b13c66cd | 592 | val = fold_convert_loc(location.gcc_location(), |
593 | build_pointer_type(lhs_type_tree), val); | |
594 | val = build_fold_indirect_ref_loc(location.gcc_location(), val); | |
e440a328 | 595 | } |
596 | ||
597 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
b13c66cd | 598 | fold_convert_loc(location.gcc_location(), lhs_type_tree, val)); |
e440a328 | 599 | } |
600 | ||
601 | // Convert an expression to a tree. This is implemented by the child | |
602 | // class. Not that it is not in general safe to call this multiple | |
603 | // times for a single expression, but that we don't catch such errors. | |
604 | ||
605 | tree | |
606 | Expression::get_tree(Translate_context* context) | |
607 | { | |
608 | // The child may have marked this expression as having an error. | |
609 | if (this->classification_ == EXPRESSION_ERROR) | |
610 | return error_mark_node; | |
611 | ||
612 | return this->do_get_tree(context); | |
613 | } | |
614 | ||
615 | // Return a tree for VAL in TYPE. | |
616 | ||
617 | tree | |
618 | Expression::integer_constant_tree(mpz_t val, tree type) | |
619 | { | |
620 | if (type == error_mark_node) | |
621 | return error_mark_node; | |
622 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
623 | return double_int_to_tree(type, | |
624 | mpz_get_double_int(type, val, true)); | |
625 | else if (TREE_CODE(type) == REAL_TYPE) | |
626 | { | |
627 | mpfr_t fval; | |
628 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
629 | tree ret = Expression::float_constant_tree(fval, type); | |
630 | mpfr_clear(fval); | |
631 | return ret; | |
632 | } | |
633 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
634 | { | |
635 | mpfr_t fval; | |
636 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
637 | tree real = Expression::float_constant_tree(fval, TREE_TYPE(type)); | |
638 | mpfr_clear(fval); | |
639 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
640 | integer_zero_node); | |
641 | return build_complex(type, real, imag); | |
642 | } | |
643 | else | |
c3e6f413 | 644 | go_unreachable(); |
e440a328 | 645 | } |
646 | ||
647 | // Return a tree for VAL in TYPE. | |
648 | ||
649 | tree | |
650 | Expression::float_constant_tree(mpfr_t val, tree type) | |
651 | { | |
652 | if (type == error_mark_node) | |
653 | return error_mark_node; | |
654 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
655 | { | |
656 | mpz_t ival; | |
657 | mpz_init(ival); | |
658 | mpfr_get_z(ival, val, GMP_RNDN); | |
659 | tree ret = Expression::integer_constant_tree(ival, type); | |
660 | mpz_clear(ival); | |
661 | return ret; | |
662 | } | |
663 | else if (TREE_CODE(type) == REAL_TYPE) | |
664 | { | |
665 | REAL_VALUE_TYPE r1; | |
666 | real_from_mpfr(&r1, val, type, GMP_RNDN); | |
667 | REAL_VALUE_TYPE r2; | |
668 | real_convert(&r2, TYPE_MODE(type), &r1); | |
669 | return build_real(type, r2); | |
670 | } | |
671 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
672 | { | |
673 | REAL_VALUE_TYPE r1; | |
674 | real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN); | |
675 | REAL_VALUE_TYPE r2; | |
676 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
677 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
678 | integer_zero_node); | |
679 | return build_complex(type, build_real(TREE_TYPE(type), r2), imag); | |
680 | } | |
681 | else | |
c3e6f413 | 682 | go_unreachable(); |
e440a328 | 683 | } |
684 | ||
685 | // Return a tree for REAL/IMAG in TYPE. | |
686 | ||
687 | tree | |
688 | Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type) | |
689 | { | |
f690b0bb | 690 | if (type == error_mark_node) |
691 | return error_mark_node; | |
692 | else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE) | |
693 | return Expression::float_constant_tree(real, type); | |
694 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
e440a328 | 695 | { |
696 | REAL_VALUE_TYPE r1; | |
697 | real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN); | |
698 | REAL_VALUE_TYPE r2; | |
699 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
700 | ||
701 | REAL_VALUE_TYPE r3; | |
702 | real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN); | |
703 | REAL_VALUE_TYPE r4; | |
704 | real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3); | |
705 | ||
706 | return build_complex(type, build_real(TREE_TYPE(type), r2), | |
707 | build_real(TREE_TYPE(type), r4)); | |
708 | } | |
709 | else | |
c3e6f413 | 710 | go_unreachable(); |
e440a328 | 711 | } |
712 | ||
713 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
714 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
715 | // If SOFAR is not NULL, it is or'red into the result. The return | |
716 | // value may be NULL if SOFAR is NULL. | |
717 | ||
718 | tree | |
719 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
b13c66cd | 720 | Location loc) |
e440a328 | 721 | { |
722 | tree val_type = TREE_TYPE(val); | |
723 | tree ret = NULL_TREE; | |
724 | ||
725 | if (!TYPE_UNSIGNED(val_type)) | |
726 | { | |
b13c66cd | 727 | ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val, |
e440a328 | 728 | build_int_cst(val_type, 0)); |
729 | if (ret == boolean_false_node) | |
730 | ret = NULL_TREE; | |
731 | } | |
732 | ||
c3068ac0 | 733 | HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type); |
734 | HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type); | |
735 | go_assert(val_type_size != -1 && bound_type_size != -1); | |
736 | if (val_type_size > bound_type_size | |
737 | || (val_type_size == bound_type_size | |
738 | && TYPE_UNSIGNED(val_type) | |
739 | && !TYPE_UNSIGNED(bound_type))) | |
e440a328 | 740 | { |
741 | tree max = TYPE_MAX_VALUE(bound_type); | |
b13c66cd | 742 | tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node, |
743 | val, fold_convert_loc(loc.gcc_location(), | |
744 | val_type, max)); | |
e440a328 | 745 | if (big == boolean_false_node) |
746 | ; | |
747 | else if (ret == NULL_TREE) | |
748 | ret = big; | |
749 | else | |
b13c66cd | 750 | ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
751 | boolean_type_node, ret, big); | |
e440a328 | 752 | } |
753 | ||
754 | if (ret == NULL_TREE) | |
755 | return sofar; | |
756 | else if (sofar == NULL_TREE) | |
757 | return ret; | |
758 | else | |
b13c66cd | 759 | return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node, |
e440a328 | 760 | sofar, ret); |
761 | } | |
762 | ||
d751bb78 | 763 | void |
764 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
765 | { | |
766 | this->do_dump_expression(ast_dump_context); | |
767 | } | |
768 | ||
e440a328 | 769 | // Error expressions. This are used to avoid cascading errors. |
770 | ||
771 | class Error_expression : public Expression | |
772 | { | |
773 | public: | |
b13c66cd | 774 | Error_expression(Location location) |
e440a328 | 775 | : Expression(EXPRESSION_ERROR, location) |
776 | { } | |
777 | ||
778 | protected: | |
779 | bool | |
780 | do_is_constant() const | |
781 | { return true; } | |
782 | ||
783 | bool | |
0c77715b | 784 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 785 | { |
0c77715b | 786 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 787 | return true; |
788 | } | |
789 | ||
4f2138d7 | 790 | bool |
e440a328 | 791 | do_discarding_value() |
4f2138d7 | 792 | { return true; } |
e440a328 | 793 | |
794 | Type* | |
795 | do_type() | |
796 | { return Type::make_error_type(); } | |
797 | ||
798 | void | |
799 | do_determine_type(const Type_context*) | |
800 | { } | |
801 | ||
802 | Expression* | |
803 | do_copy() | |
804 | { return this; } | |
805 | ||
806 | bool | |
807 | do_is_addressable() const | |
808 | { return true; } | |
809 | ||
810 | tree | |
811 | do_get_tree(Translate_context*) | |
812 | { return error_mark_node; } | |
d751bb78 | 813 | |
814 | void | |
815 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 816 | }; |
817 | ||
d751bb78 | 818 | // Dump the ast representation for an error expression to a dump context. |
819 | ||
820 | void | |
821 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
822 | { | |
823 | ast_dump_context->ostream() << "_Error_" ; | |
824 | } | |
825 | ||
e440a328 | 826 | Expression* |
b13c66cd | 827 | Expression::make_error(Location location) |
e440a328 | 828 | { |
829 | return new Error_expression(location); | |
830 | } | |
831 | ||
832 | // An expression which is really a type. This is used during parsing. | |
833 | // It is an error if these survive after lowering. | |
834 | ||
835 | class | |
836 | Type_expression : public Expression | |
837 | { | |
838 | public: | |
b13c66cd | 839 | Type_expression(Type* type, Location location) |
e440a328 | 840 | : Expression(EXPRESSION_TYPE, location), |
841 | type_(type) | |
842 | { } | |
843 | ||
844 | protected: | |
845 | int | |
846 | do_traverse(Traverse* traverse) | |
847 | { return Type::traverse(this->type_, traverse); } | |
848 | ||
849 | Type* | |
850 | do_type() | |
851 | { return this->type_; } | |
852 | ||
853 | void | |
854 | do_determine_type(const Type_context*) | |
855 | { } | |
856 | ||
857 | void | |
858 | do_check_types(Gogo*) | |
859 | { this->report_error(_("invalid use of type")); } | |
860 | ||
861 | Expression* | |
862 | do_copy() | |
863 | { return this; } | |
864 | ||
865 | tree | |
866 | do_get_tree(Translate_context*) | |
c3e6f413 | 867 | { go_unreachable(); } |
e440a328 | 868 | |
d751bb78 | 869 | void do_dump_expression(Ast_dump_context*) const; |
870 | ||
e440a328 | 871 | private: |
872 | // The type which we are representing as an expression. | |
873 | Type* type_; | |
874 | }; | |
875 | ||
d751bb78 | 876 | void |
877 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
878 | { | |
879 | ast_dump_context->dump_type(this->type_); | |
880 | } | |
881 | ||
e440a328 | 882 | Expression* |
b13c66cd | 883 | Expression::make_type(Type* type, Location location) |
e440a328 | 884 | { |
885 | return new Type_expression(type, location); | |
886 | } | |
887 | ||
e03bdf36 | 888 | // Class Parser_expression. |
889 | ||
890 | Type* | |
891 | Parser_expression::do_type() | |
892 | { | |
893 | // We should never really ask for the type of a Parser_expression. | |
894 | // However, it can happen, at least when we have an invalid const | |
895 | // whose initializer refers to the const itself. In that case we | |
896 | // may ask for the type when lowering the const itself. | |
c484d925 | 897 | go_assert(saw_errors()); |
e03bdf36 | 898 | return Type::make_error_type(); |
899 | } | |
900 | ||
e440a328 | 901 | // Class Var_expression. |
902 | ||
903 | // Lower a variable expression. Here we just make sure that the | |
904 | // initialization expression of the variable has been lowered. This | |
905 | // ensures that we will be able to determine the type of the variable | |
906 | // if necessary. | |
907 | ||
908 | Expression* | |
ceeb4318 | 909 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
910 | Statement_inserter* inserter, int) | |
e440a328 | 911 | { |
912 | if (this->variable_->is_variable()) | |
913 | { | |
914 | Variable* var = this->variable_->var_value(); | |
915 | // This is either a local variable or a global variable. A | |
916 | // reference to a variable which is local to an enclosing | |
917 | // function will be a reference to a field in a closure. | |
918 | if (var->is_global()) | |
ceeb4318 | 919 | { |
920 | function = NULL; | |
921 | inserter = NULL; | |
922 | } | |
923 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 924 | } |
925 | return this; | |
926 | } | |
927 | ||
e440a328 | 928 | // Return the type of a reference to a variable. |
929 | ||
930 | Type* | |
931 | Var_expression::do_type() | |
932 | { | |
933 | if (this->variable_->is_variable()) | |
934 | return this->variable_->var_value()->type(); | |
935 | else if (this->variable_->is_result_variable()) | |
936 | return this->variable_->result_var_value()->type(); | |
937 | else | |
c3e6f413 | 938 | go_unreachable(); |
e440a328 | 939 | } |
940 | ||
0ab09e06 | 941 | // Determine the type of a reference to a variable. |
942 | ||
943 | void | |
944 | Var_expression::do_determine_type(const Type_context*) | |
945 | { | |
946 | if (this->variable_->is_variable()) | |
947 | this->variable_->var_value()->determine_type(); | |
948 | } | |
949 | ||
e440a328 | 950 | // Something takes the address of this variable. This means that we |
951 | // may want to move the variable onto the heap. | |
952 | ||
953 | void | |
954 | Var_expression::do_address_taken(bool escapes) | |
955 | { | |
956 | if (!escapes) | |
f325319b | 957 | { |
958 | if (this->variable_->is_variable()) | |
959 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
960 | else if (this->variable_->is_result_variable()) | |
961 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
962 | else | |
963 | go_unreachable(); | |
964 | } | |
e440a328 | 965 | else |
f325319b | 966 | { |
967 | if (this->variable_->is_variable()) | |
968 | this->variable_->var_value()->set_address_taken(); | |
969 | else if (this->variable_->is_result_variable()) | |
970 | this->variable_->result_var_value()->set_address_taken(); | |
971 | else | |
972 | go_unreachable(); | |
973 | } | |
e440a328 | 974 | } |
975 | ||
976 | // Get the tree for a reference to a variable. | |
977 | ||
978 | tree | |
979 | Var_expression::do_get_tree(Translate_context* context) | |
980 | { | |
fe2f84cf | 981 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
982 | context->function()); | |
983 | tree ret = var_to_tree(bvar); | |
984 | if (ret == error_mark_node) | |
985 | return error_mark_node; | |
986 | bool is_in_heap; | |
987 | if (this->variable_->is_variable()) | |
988 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
989 | else if (this->variable_->is_result_variable()) | |
990 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
991 | else | |
c3e6f413 | 992 | go_unreachable(); |
fe2f84cf | 993 | if (is_in_heap) |
994 | { | |
b13c66cd | 995 | ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret); |
fe2f84cf | 996 | TREE_THIS_NOTRAP(ret) = 1; |
997 | } | |
998 | return ret; | |
e440a328 | 999 | } |
1000 | ||
d751bb78 | 1001 | // Ast dump for variable expression. |
1002 | ||
1003 | void | |
1004 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1005 | { | |
1006 | ast_dump_context->ostream() << this->variable_->name() ; | |
1007 | } | |
1008 | ||
e440a328 | 1009 | // Make a reference to a variable in an expression. |
1010 | ||
1011 | Expression* | |
b13c66cd | 1012 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 1013 | { |
1014 | if (var->is_sink()) | |
1015 | return Expression::make_sink(location); | |
1016 | ||
1017 | // FIXME: Creating a new object for each reference to a variable is | |
1018 | // wasteful. | |
1019 | return new Var_expression(var, location); | |
1020 | } | |
1021 | ||
1022 | // Class Temporary_reference_expression. | |
1023 | ||
1024 | // The type. | |
1025 | ||
1026 | Type* | |
1027 | Temporary_reference_expression::do_type() | |
1028 | { | |
1029 | return this->statement_->type(); | |
1030 | } | |
1031 | ||
1032 | // Called if something takes the address of this temporary variable. | |
1033 | // We never have to move temporary variables to the heap, but we do | |
1034 | // need to know that they must live in the stack rather than in a | |
1035 | // register. | |
1036 | ||
1037 | void | |
1038 | Temporary_reference_expression::do_address_taken(bool) | |
1039 | { | |
1040 | this->statement_->set_is_address_taken(); | |
1041 | } | |
1042 | ||
1043 | // Get a tree referring to the variable. | |
1044 | ||
1045 | tree | |
eefc1ed3 | 1046 | Temporary_reference_expression::do_get_tree(Translate_context* context) |
e440a328 | 1047 | { |
eefc1ed3 | 1048 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
1049 | ||
1050 | // The gcc backend can't represent the same set of recursive types | |
1051 | // that the Go frontend can. In some cases this means that a | |
1052 | // temporary variable won't have the right backend type. Correct | |
1053 | // that here by adding a type cast. We need to use base() to push | |
1054 | // the circularity down one level. | |
1055 | tree ret = var_to_tree(bvar); | |
ceeb4318 | 1056 | if (!this->is_lvalue_ |
1057 | && POINTER_TYPE_P(TREE_TYPE(ret)) | |
1058 | && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret)))) | |
eefc1ed3 | 1059 | { |
9f0e0513 | 1060 | Btype* type_btype = this->type()->base()->get_backend(context->gogo()); |
1061 | tree type_tree = type_to_tree(type_btype); | |
b13c66cd | 1062 | ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret); |
eefc1ed3 | 1063 | } |
1064 | return ret; | |
e440a328 | 1065 | } |
1066 | ||
d751bb78 | 1067 | // Ast dump for temporary reference. |
1068 | ||
1069 | void | |
1070 | Temporary_reference_expression::do_dump_expression( | |
1071 | Ast_dump_context* ast_dump_context) const | |
1072 | { | |
1073 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
1074 | } | |
1075 | ||
e440a328 | 1076 | // Make a reference to a temporary variable. |
1077 | ||
ceeb4318 | 1078 | Temporary_reference_expression* |
e440a328 | 1079 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 1080 | Location location) |
e440a328 | 1081 | { |
1082 | return new Temporary_reference_expression(statement, location); | |
1083 | } | |
1084 | ||
e9d3367e | 1085 | // Class Set_and_use_temporary_expression. |
1086 | ||
1087 | // Return the type. | |
1088 | ||
1089 | Type* | |
1090 | Set_and_use_temporary_expression::do_type() | |
1091 | { | |
1092 | return this->statement_->type(); | |
1093 | } | |
1094 | ||
1095 | // Take the address. | |
1096 | ||
1097 | void | |
1098 | Set_and_use_temporary_expression::do_address_taken(bool) | |
1099 | { | |
1100 | this->statement_->set_is_address_taken(); | |
1101 | } | |
1102 | ||
1103 | // Return the backend representation. | |
1104 | ||
1105 | tree | |
1106 | Set_and_use_temporary_expression::do_get_tree(Translate_context* context) | |
1107 | { | |
1108 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
1109 | tree var_tree = var_to_tree(bvar); | |
1110 | tree expr_tree = this->expr_->get_tree(context); | |
1111 | if (var_tree == error_mark_node || expr_tree == error_mark_node) | |
1112 | return error_mark_node; | |
1113 | Location loc = this->location(); | |
1114 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree), | |
1115 | build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node, | |
1116 | var_tree, expr_tree), | |
1117 | var_tree); | |
1118 | } | |
1119 | ||
1120 | // Dump. | |
1121 | ||
1122 | void | |
1123 | Set_and_use_temporary_expression::do_dump_expression( | |
1124 | Ast_dump_context* ast_dump_context) const | |
1125 | { | |
1126 | ast_dump_context->ostream() << '('; | |
1127 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
1128 | ast_dump_context->ostream() << " = "; | |
1129 | this->expr_->dump_expression(ast_dump_context); | |
1130 | ast_dump_context->ostream() << ')'; | |
1131 | } | |
1132 | ||
1133 | // Make a set-and-use temporary. | |
1134 | ||
1135 | Set_and_use_temporary_expression* | |
1136 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
1137 | Expression* expr, Location location) | |
1138 | { | |
1139 | return new Set_and_use_temporary_expression(statement, expr, location); | |
1140 | } | |
1141 | ||
e440a328 | 1142 | // A sink expression--a use of the blank identifier _. |
1143 | ||
1144 | class Sink_expression : public Expression | |
1145 | { | |
1146 | public: | |
b13c66cd | 1147 | Sink_expression(Location location) |
e440a328 | 1148 | : Expression(EXPRESSION_SINK, location), |
1149 | type_(NULL), var_(NULL_TREE) | |
1150 | { } | |
1151 | ||
1152 | protected: | |
4f2138d7 | 1153 | bool |
e440a328 | 1154 | do_discarding_value() |
4f2138d7 | 1155 | { return true; } |
e440a328 | 1156 | |
1157 | Type* | |
1158 | do_type(); | |
1159 | ||
1160 | void | |
1161 | do_determine_type(const Type_context*); | |
1162 | ||
1163 | Expression* | |
1164 | do_copy() | |
1165 | { return new Sink_expression(this->location()); } | |
1166 | ||
1167 | tree | |
1168 | do_get_tree(Translate_context*); | |
1169 | ||
d751bb78 | 1170 | void |
1171 | do_dump_expression(Ast_dump_context*) const; | |
1172 | ||
e440a328 | 1173 | private: |
1174 | // The type of this sink variable. | |
1175 | Type* type_; | |
1176 | // The temporary variable we generate. | |
1177 | tree var_; | |
1178 | }; | |
1179 | ||
1180 | // Return the type of a sink expression. | |
1181 | ||
1182 | Type* | |
1183 | Sink_expression::do_type() | |
1184 | { | |
1185 | if (this->type_ == NULL) | |
1186 | return Type::make_sink_type(); | |
1187 | return this->type_; | |
1188 | } | |
1189 | ||
1190 | // Determine the type of a sink expression. | |
1191 | ||
1192 | void | |
1193 | Sink_expression::do_determine_type(const Type_context* context) | |
1194 | { | |
1195 | if (context->type != NULL) | |
1196 | this->type_ = context->type; | |
1197 | } | |
1198 | ||
1199 | // Return a temporary variable for a sink expression. This will | |
1200 | // presumably be a write-only variable which the middle-end will drop. | |
1201 | ||
1202 | tree | |
1203 | Sink_expression::do_get_tree(Translate_context* context) | |
1204 | { | |
1205 | if (this->var_ == NULL_TREE) | |
1206 | { | |
c484d925 | 1207 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
9f0e0513 | 1208 | Btype* bt = this->type_->get_backend(context->gogo()); |
1209 | this->var_ = create_tmp_var(type_to_tree(bt), "blank"); | |
e440a328 | 1210 | } |
1211 | return this->var_; | |
1212 | } | |
1213 | ||
d751bb78 | 1214 | // Ast dump for sink expression. |
1215 | ||
1216 | void | |
1217 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1218 | { | |
1219 | ast_dump_context->ostream() << "_" ; | |
1220 | } | |
1221 | ||
e440a328 | 1222 | // Make a sink expression. |
1223 | ||
1224 | Expression* | |
b13c66cd | 1225 | Expression::make_sink(Location location) |
e440a328 | 1226 | { |
1227 | return new Sink_expression(location); | |
1228 | } | |
1229 | ||
1230 | // Class Func_expression. | |
1231 | ||
1232 | // FIXME: Can a function expression appear in a constant expression? | |
1233 | // The value is unchanging. Initializing a constant to the address of | |
1234 | // a function seems like it could work, though there might be little | |
1235 | // point to it. | |
1236 | ||
e440a328 | 1237 | // Traversal. |
1238 | ||
1239 | int | |
1240 | Func_expression::do_traverse(Traverse* traverse) | |
1241 | { | |
1242 | return (this->closure_ == NULL | |
1243 | ? TRAVERSE_CONTINUE | |
1244 | : Expression::traverse(&this->closure_, traverse)); | |
1245 | } | |
1246 | ||
1247 | // Return the type of a function expression. | |
1248 | ||
1249 | Type* | |
1250 | Func_expression::do_type() | |
1251 | { | |
1252 | if (this->function_->is_function()) | |
1253 | return this->function_->func_value()->type(); | |
1254 | else if (this->function_->is_function_declaration()) | |
1255 | return this->function_->func_declaration_value()->type(); | |
1256 | else | |
c3e6f413 | 1257 | go_unreachable(); |
e440a328 | 1258 | } |
1259 | ||
1260 | // Get the tree for a function expression without evaluating the | |
1261 | // closure. | |
1262 | ||
1263 | tree | |
1264 | Func_expression::get_tree_without_closure(Gogo* gogo) | |
1265 | { | |
1266 | Function_type* fntype; | |
1267 | if (this->function_->is_function()) | |
1268 | fntype = this->function_->func_value()->type(); | |
1269 | else if (this->function_->is_function_declaration()) | |
1270 | fntype = this->function_->func_declaration_value()->type(); | |
1271 | else | |
c3e6f413 | 1272 | go_unreachable(); |
e440a328 | 1273 | |
1274 | // Builtin functions are handled specially by Call_expression. We | |
1275 | // can't take their address. | |
1276 | if (fntype->is_builtin()) | |
1277 | { | |
cb0e02f3 | 1278 | error_at(this->location(), |
1279 | "invalid use of special builtin function %qs; must be called", | |
e440a328 | 1280 | this->function_->name().c_str()); |
1281 | return error_mark_node; | |
1282 | } | |
1283 | ||
1284 | Named_object* no = this->function_; | |
9d6f3721 | 1285 | |
1286 | tree id = no->get_id(gogo); | |
1287 | if (id == error_mark_node) | |
1288 | return error_mark_node; | |
1289 | ||
e440a328 | 1290 | tree fndecl; |
1291 | if (no->is_function()) | |
1292 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1293 | else if (no->is_function_declaration()) | |
1294 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1295 | else | |
c3e6f413 | 1296 | go_unreachable(); |
e440a328 | 1297 | |
9d6f3721 | 1298 | if (fndecl == error_mark_node) |
1299 | return error_mark_node; | |
1300 | ||
b13c66cd | 1301 | return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl); |
e440a328 | 1302 | } |
1303 | ||
1304 | // Get the tree for a function expression. This is used when we take | |
1305 | // the address of a function rather than simply calling it. If the | |
1306 | // function has a closure, we must use a trampoline. | |
1307 | ||
1308 | tree | |
1309 | Func_expression::do_get_tree(Translate_context* context) | |
1310 | { | |
1311 | Gogo* gogo = context->gogo(); | |
1312 | ||
1313 | tree fnaddr = this->get_tree_without_closure(gogo); | |
1314 | if (fnaddr == error_mark_node) | |
1315 | return error_mark_node; | |
1316 | ||
c484d925 | 1317 | go_assert(TREE_CODE(fnaddr) == ADDR_EXPR |
e440a328 | 1318 | && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL); |
1319 | TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1; | |
1320 | ||
2010c17a | 1321 | // If there is no closure, that is all have to do. |
1322 | if (this->closure_ == NULL) | |
1323 | return fnaddr; | |
e440a328 | 1324 | |
2010c17a | 1325 | go_assert(this->function_->func_value()->enclosing() != NULL); |
1326 | ||
1327 | // Get the value of the closure. This will be a pointer to space | |
1328 | // allocated on the heap. | |
1329 | tree closure_tree = this->closure_->get_tree(context); | |
1330 | if (closure_tree == error_mark_node) | |
1331 | return error_mark_node; | |
1332 | go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree))); | |
e440a328 | 1333 | |
1334 | // Now we need to build some code on the heap. This code will load | |
1335 | // the static chain pointer with the closure and then jump to the | |
1336 | // body of the function. The normal gcc approach is to build the | |
1337 | // code on the stack. Unfortunately we can not do that, as Go | |
1338 | // permits us to return the function pointer. | |
1339 | ||
1340 | return gogo->make_trampoline(fnaddr, closure_tree, this->location()); | |
1341 | } | |
1342 | ||
d751bb78 | 1343 | // Ast dump for function. |
1344 | ||
1345 | void | |
1346 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1347 | { | |
8b1c301d | 1348 | ast_dump_context->ostream() << this->function_->name(); |
1349 | if (this->closure_ != NULL) | |
1350 | { | |
1351 | ast_dump_context->ostream() << " {closure = "; | |
1352 | this->closure_->dump_expression(ast_dump_context); | |
1353 | ast_dump_context->ostream() << "}"; | |
1354 | } | |
d751bb78 | 1355 | } |
1356 | ||
e440a328 | 1357 | // Make a reference to a function in an expression. |
1358 | ||
1359 | Expression* | |
1360 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1361 | Location location) |
e440a328 | 1362 | { |
1363 | return new Func_expression(function, closure, location); | |
1364 | } | |
1365 | ||
1366 | // Class Unknown_expression. | |
1367 | ||
1368 | // Return the name of an unknown expression. | |
1369 | ||
1370 | const std::string& | |
1371 | Unknown_expression::name() const | |
1372 | { | |
1373 | return this->named_object_->name(); | |
1374 | } | |
1375 | ||
1376 | // Lower a reference to an unknown name. | |
1377 | ||
1378 | Expression* | |
ceeb4318 | 1379 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1380 | { |
b13c66cd | 1381 | Location location = this->location(); |
e440a328 | 1382 | Named_object* no = this->named_object_; |
deded542 | 1383 | Named_object* real; |
1384 | if (!no->is_unknown()) | |
1385 | real = no; | |
1386 | else | |
e440a328 | 1387 | { |
deded542 | 1388 | real = no->unknown_value()->real_named_object(); |
1389 | if (real == NULL) | |
1390 | { | |
1391 | if (this->is_composite_literal_key_) | |
1392 | return this; | |
acf8e158 | 1393 | if (!this->no_error_message_) |
1394 | error_at(location, "reference to undefined name %qs", | |
1395 | this->named_object_->message_name().c_str()); | |
deded542 | 1396 | return Expression::make_error(location); |
1397 | } | |
e440a328 | 1398 | } |
1399 | switch (real->classification()) | |
1400 | { | |
1401 | case Named_object::NAMED_OBJECT_CONST: | |
1402 | return Expression::make_const_reference(real, location); | |
1403 | case Named_object::NAMED_OBJECT_TYPE: | |
1404 | return Expression::make_type(real->type_value(), location); | |
1405 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1406 | if (this->is_composite_literal_key_) | |
1407 | return this; | |
acf8e158 | 1408 | if (!this->no_error_message_) |
1409 | error_at(location, "reference to undefined type %qs", | |
1410 | real->message_name().c_str()); | |
e440a328 | 1411 | return Expression::make_error(location); |
1412 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1413 | real->var_value()->set_is_used(); |
e440a328 | 1414 | return Expression::make_var_reference(real, location); |
1415 | case Named_object::NAMED_OBJECT_FUNC: | |
1416 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1417 | return Expression::make_func_reference(real, NULL, location); | |
1418 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1419 | if (this->is_composite_literal_key_) | |
1420 | return this; | |
acf8e158 | 1421 | if (!this->no_error_message_) |
1422 | error_at(location, "unexpected reference to package"); | |
e440a328 | 1423 | return Expression::make_error(location); |
1424 | default: | |
c3e6f413 | 1425 | go_unreachable(); |
e440a328 | 1426 | } |
1427 | } | |
1428 | ||
d751bb78 | 1429 | // Dump the ast representation for an unknown expression to a dump context. |
1430 | ||
1431 | void | |
1432 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1433 | { | |
1434 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1435 | << ")"; | |
d751bb78 | 1436 | } |
1437 | ||
e440a328 | 1438 | // Make a reference to an unknown name. |
1439 | ||
acf8e158 | 1440 | Unknown_expression* |
b13c66cd | 1441 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1442 | { |
e440a328 | 1443 | return new Unknown_expression(no, location); |
1444 | } | |
1445 | ||
1446 | // A boolean expression. | |
1447 | ||
1448 | class Boolean_expression : public Expression | |
1449 | { | |
1450 | public: | |
b13c66cd | 1451 | Boolean_expression(bool val, Location location) |
e440a328 | 1452 | : Expression(EXPRESSION_BOOLEAN, location), |
1453 | val_(val), type_(NULL) | |
1454 | { } | |
1455 | ||
1456 | static Expression* | |
1457 | do_import(Import*); | |
1458 | ||
1459 | protected: | |
1460 | bool | |
1461 | do_is_constant() const | |
1462 | { return true; } | |
1463 | ||
1464 | Type* | |
1465 | do_type(); | |
1466 | ||
1467 | void | |
1468 | do_determine_type(const Type_context*); | |
1469 | ||
1470 | Expression* | |
1471 | do_copy() | |
1472 | { return this; } | |
1473 | ||
1474 | tree | |
1475 | do_get_tree(Translate_context*) | |
1476 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1477 | ||
1478 | void | |
1479 | do_export(Export* exp) const | |
1480 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1481 | ||
d751bb78 | 1482 | void |
1483 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1484 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1485 | ||
e440a328 | 1486 | private: |
1487 | // The constant. | |
1488 | bool val_; | |
1489 | // The type as determined by context. | |
1490 | Type* type_; | |
1491 | }; | |
1492 | ||
1493 | // Get the type. | |
1494 | ||
1495 | Type* | |
1496 | Boolean_expression::do_type() | |
1497 | { | |
1498 | if (this->type_ == NULL) | |
1499 | this->type_ = Type::make_boolean_type(); | |
1500 | return this->type_; | |
1501 | } | |
1502 | ||
1503 | // Set the type from the context. | |
1504 | ||
1505 | void | |
1506 | Boolean_expression::do_determine_type(const Type_context* context) | |
1507 | { | |
1508 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1509 | ; | |
1510 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1511 | this->type_ = context->type; | |
1512 | else if (!context->may_be_abstract) | |
1513 | this->type_ = Type::lookup_bool_type(); | |
1514 | } | |
1515 | ||
1516 | // Import a boolean constant. | |
1517 | ||
1518 | Expression* | |
1519 | Boolean_expression::do_import(Import* imp) | |
1520 | { | |
1521 | if (imp->peek_char() == 't') | |
1522 | { | |
1523 | imp->require_c_string("true"); | |
1524 | return Expression::make_boolean(true, imp->location()); | |
1525 | } | |
1526 | else | |
1527 | { | |
1528 | imp->require_c_string("false"); | |
1529 | return Expression::make_boolean(false, imp->location()); | |
1530 | } | |
1531 | } | |
1532 | ||
1533 | // Make a boolean expression. | |
1534 | ||
1535 | Expression* | |
b13c66cd | 1536 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1537 | { |
1538 | return new Boolean_expression(val, location); | |
1539 | } | |
1540 | ||
1541 | // Class String_expression. | |
1542 | ||
1543 | // Get the type. | |
1544 | ||
1545 | Type* | |
1546 | String_expression::do_type() | |
1547 | { | |
1548 | if (this->type_ == NULL) | |
1549 | this->type_ = Type::make_string_type(); | |
1550 | return this->type_; | |
1551 | } | |
1552 | ||
1553 | // Set the type from the context. | |
1554 | ||
1555 | void | |
1556 | String_expression::do_determine_type(const Type_context* context) | |
1557 | { | |
1558 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1559 | ; | |
1560 | else if (context->type != NULL && context->type->is_string_type()) | |
1561 | this->type_ = context->type; | |
1562 | else if (!context->may_be_abstract) | |
1563 | this->type_ = Type::lookup_string_type(); | |
1564 | } | |
1565 | ||
1566 | // Build a string constant. | |
1567 | ||
1568 | tree | |
1569 | String_expression::do_get_tree(Translate_context* context) | |
1570 | { | |
1571 | return context->gogo()->go_string_constant_tree(this->val_); | |
1572 | } | |
1573 | ||
8b1c301d | 1574 | // Write string literal to string dump. |
e440a328 | 1575 | |
1576 | void | |
8b1c301d | 1577 | String_expression::export_string(String_dump* exp, |
1578 | const String_expression* str) | |
e440a328 | 1579 | { |
1580 | std::string s; | |
8b1c301d | 1581 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1582 | s += '"'; |
8b1c301d | 1583 | for (std::string::const_iterator p = str->val_.begin(); |
1584 | p != str->val_.end(); | |
e440a328 | 1585 | ++p) |
1586 | { | |
1587 | if (*p == '\\' || *p == '"') | |
1588 | { | |
1589 | s += '\\'; | |
1590 | s += *p; | |
1591 | } | |
1592 | else if (*p >= 0x20 && *p < 0x7f) | |
1593 | s += *p; | |
1594 | else if (*p == '\n') | |
1595 | s += "\\n"; | |
1596 | else if (*p == '\t') | |
1597 | s += "\\t"; | |
1598 | else | |
1599 | { | |
1600 | s += "\\x"; | |
1601 | unsigned char c = *p; | |
1602 | unsigned int dig = c >> 4; | |
1603 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1604 | dig = c & 0xf; | |
1605 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1606 | } | |
1607 | } | |
1608 | s += '"'; | |
1609 | exp->write_string(s); | |
1610 | } | |
1611 | ||
8b1c301d | 1612 | // Export a string expression. |
1613 | ||
1614 | void | |
1615 | String_expression::do_export(Export* exp) const | |
1616 | { | |
1617 | String_expression::export_string(exp, this); | |
1618 | } | |
1619 | ||
e440a328 | 1620 | // Import a string expression. |
1621 | ||
1622 | Expression* | |
1623 | String_expression::do_import(Import* imp) | |
1624 | { | |
1625 | imp->require_c_string("\""); | |
1626 | std::string val; | |
1627 | while (true) | |
1628 | { | |
1629 | int c = imp->get_char(); | |
1630 | if (c == '"' || c == -1) | |
1631 | break; | |
1632 | if (c != '\\') | |
1633 | val += static_cast<char>(c); | |
1634 | else | |
1635 | { | |
1636 | c = imp->get_char(); | |
1637 | if (c == '\\' || c == '"') | |
1638 | val += static_cast<char>(c); | |
1639 | else if (c == 'n') | |
1640 | val += '\n'; | |
1641 | else if (c == 't') | |
1642 | val += '\t'; | |
1643 | else if (c == 'x') | |
1644 | { | |
1645 | c = imp->get_char(); | |
1646 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1647 | c = imp->get_char(); | |
1648 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1649 | char v = (vh << 4) | vl; | |
1650 | val += v; | |
1651 | } | |
1652 | else | |
1653 | { | |
1654 | error_at(imp->location(), "bad string constant"); | |
1655 | return Expression::make_error(imp->location()); | |
1656 | } | |
1657 | } | |
1658 | } | |
1659 | return Expression::make_string(val, imp->location()); | |
1660 | } | |
1661 | ||
d751bb78 | 1662 | // Ast dump for string expression. |
1663 | ||
1664 | void | |
1665 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1666 | { | |
8b1c301d | 1667 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1668 | } |
1669 | ||
e440a328 | 1670 | // Make a string expression. |
1671 | ||
1672 | Expression* | |
b13c66cd | 1673 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1674 | { |
1675 | return new String_expression(val, location); | |
1676 | } | |
1677 | ||
1678 | // Make an integer expression. | |
1679 | ||
1680 | class Integer_expression : public Expression | |
1681 | { | |
1682 | public: | |
5d4b8566 | 1683 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1684 | Location location) | |
e440a328 | 1685 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1686 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1687 | { mpz_init_set(this->val_, *val); } |
1688 | ||
1689 | static Expression* | |
1690 | do_import(Import*); | |
1691 | ||
8b1c301d | 1692 | // Write VAL to string dump. |
e440a328 | 1693 | static void |
8b1c301d | 1694 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1695 | |
d751bb78 | 1696 | // Write VAL to dump context. |
1697 | static void | |
1698 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1699 | ||
e440a328 | 1700 | protected: |
1701 | bool | |
1702 | do_is_constant() const | |
1703 | { return true; } | |
1704 | ||
1705 | bool | |
0c77715b | 1706 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1707 | |
1708 | Type* | |
1709 | do_type(); | |
1710 | ||
1711 | void | |
1712 | do_determine_type(const Type_context* context); | |
1713 | ||
1714 | void | |
1715 | do_check_types(Gogo*); | |
1716 | ||
1717 | tree | |
1718 | do_get_tree(Translate_context*); | |
1719 | ||
1720 | Expression* | |
1721 | do_copy() | |
5d4b8566 | 1722 | { |
1723 | if (this->is_character_constant_) | |
1724 | return Expression::make_character(&this->val_, this->type_, | |
1725 | this->location()); | |
1726 | else | |
1727 | return Expression::make_integer(&this->val_, this->type_, | |
1728 | this->location()); | |
1729 | } | |
e440a328 | 1730 | |
1731 | void | |
1732 | do_export(Export*) const; | |
1733 | ||
d751bb78 | 1734 | void |
1735 | do_dump_expression(Ast_dump_context*) const; | |
1736 | ||
e440a328 | 1737 | private: |
1738 | // The integer value. | |
1739 | mpz_t val_; | |
1740 | // The type so far. | |
1741 | Type* type_; | |
5d4b8566 | 1742 | // Whether this is a character constant. |
1743 | bool is_character_constant_; | |
e440a328 | 1744 | }; |
1745 | ||
0c77715b | 1746 | // Return a numeric constant for this expression. We have to mark |
1747 | // this as a character when appropriate. | |
e440a328 | 1748 | |
1749 | bool | |
0c77715b | 1750 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1751 | { |
0c77715b | 1752 | if (this->is_character_constant_) |
1753 | nc->set_rune(this->type_, this->val_); | |
1754 | else | |
1755 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1756 | return true; |
1757 | } | |
1758 | ||
1759 | // Return the current type. If we haven't set the type yet, we return | |
1760 | // an abstract integer type. | |
1761 | ||
1762 | Type* | |
1763 | Integer_expression::do_type() | |
1764 | { | |
1765 | if (this->type_ == NULL) | |
5d4b8566 | 1766 | { |
1767 | if (this->is_character_constant_) | |
1768 | this->type_ = Type::make_abstract_character_type(); | |
1769 | else | |
1770 | this->type_ = Type::make_abstract_integer_type(); | |
1771 | } | |
e440a328 | 1772 | return this->type_; |
1773 | } | |
1774 | ||
1775 | // Set the type of the integer value. Here we may switch from an | |
1776 | // abstract type to a real type. | |
1777 | ||
1778 | void | |
1779 | Integer_expression::do_determine_type(const Type_context* context) | |
1780 | { | |
1781 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1782 | ; | |
0c77715b | 1783 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1784 | this->type_ = context->type; |
1785 | else if (!context->may_be_abstract) | |
5d4b8566 | 1786 | { |
1787 | if (this->is_character_constant_) | |
1788 | this->type_ = Type::lookup_integer_type("int32"); | |
1789 | else | |
1790 | this->type_ = Type::lookup_integer_type("int"); | |
1791 | } | |
e440a328 | 1792 | } |
1793 | ||
e440a328 | 1794 | // Check the type of an integer constant. |
1795 | ||
1796 | void | |
1797 | Integer_expression::do_check_types(Gogo*) | |
1798 | { | |
0c77715b | 1799 | Type* type = this->type_; |
1800 | if (type == NULL) | |
e440a328 | 1801 | return; |
0c77715b | 1802 | Numeric_constant nc; |
1803 | if (this->is_character_constant_) | |
1804 | nc.set_rune(NULL, this->val_); | |
1805 | else | |
1806 | nc.set_int(NULL, this->val_); | |
1807 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1808 | this->set_is_error(); |
1809 | } | |
1810 | ||
1811 | // Get a tree for an integer constant. | |
1812 | ||
1813 | tree | |
1814 | Integer_expression::do_get_tree(Translate_context* context) | |
1815 | { | |
1816 | Gogo* gogo = context->gogo(); | |
1817 | tree type; | |
1818 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
9f0e0513 | 1819 | type = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 1820 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
1821 | { | |
1822 | // We are converting to an abstract floating point type. | |
9f0e0513 | 1823 | Type* ftype = Type::lookup_float_type("float64"); |
1824 | type = type_to_tree(ftype->get_backend(gogo)); | |
e440a328 | 1825 | } |
1826 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1827 | { | |
1828 | // We are converting to an abstract complex type. | |
9f0e0513 | 1829 | Type* ctype = Type::lookup_complex_type("complex128"); |
1830 | type = type_to_tree(ctype->get_backend(gogo)); | |
e440a328 | 1831 | } |
1832 | else | |
1833 | { | |
1834 | // If we still have an abstract type here, then this is being | |
1835 | // used in a constant expression which didn't get reduced for | |
1836 | // some reason. Use a type which will fit the value. We use <, | |
1837 | // not <=, because we need an extra bit for the sign bit. | |
1838 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 1839 | Type* int_type = Type::lookup_integer_type("int"); |
1840 | if (bits < int_type->integer_type()->bits()) | |
1841 | type = type_to_tree(int_type->get_backend(gogo)); | |
e440a328 | 1842 | else if (bits < 64) |
9f0e0513 | 1843 | { |
1844 | Type* t = Type::lookup_integer_type("int64"); | |
1845 | type = type_to_tree(t->get_backend(gogo)); | |
1846 | } | |
e440a328 | 1847 | else |
1848 | type = long_long_integer_type_node; | |
1849 | } | |
1850 | return Expression::integer_constant_tree(this->val_, type); | |
1851 | } | |
1852 | ||
1853 | // Write VAL to export data. | |
1854 | ||
1855 | void | |
8b1c301d | 1856 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 1857 | { |
1858 | char* s = mpz_get_str(NULL, 10, val); | |
1859 | exp->write_c_string(s); | |
1860 | free(s); | |
1861 | } | |
1862 | ||
1863 | // Export an integer in a constant expression. | |
1864 | ||
1865 | void | |
1866 | Integer_expression::do_export(Export* exp) const | |
1867 | { | |
1868 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 1869 | if (this->is_character_constant_) |
1870 | exp->write_c_string("'"); | |
e440a328 | 1871 | // A trailing space lets us reliably identify the end of the number. |
1872 | exp->write_c_string(" "); | |
1873 | } | |
1874 | ||
1875 | // Import an integer, floating point, or complex value. This handles | |
1876 | // all these types because they all start with digits. | |
1877 | ||
1878 | Expression* | |
1879 | Integer_expression::do_import(Import* imp) | |
1880 | { | |
1881 | std::string num = imp->read_identifier(); | |
1882 | imp->require_c_string(" "); | |
1883 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1884 | { | |
1885 | mpfr_t real; | |
1886 | size_t plus_pos = num.find('+', 1); | |
1887 | size_t minus_pos = num.find('-', 1); | |
1888 | size_t pos; | |
1889 | if (plus_pos == std::string::npos) | |
1890 | pos = minus_pos; | |
1891 | else if (minus_pos == std::string::npos) | |
1892 | pos = plus_pos; | |
1893 | else | |
1894 | { | |
1895 | error_at(imp->location(), "bad number in import data: %qs", | |
1896 | num.c_str()); | |
1897 | return Expression::make_error(imp->location()); | |
1898 | } | |
1899 | if (pos == std::string::npos) | |
1900 | mpfr_set_ui(real, 0, GMP_RNDN); | |
1901 | else | |
1902 | { | |
1903 | std::string real_str = num.substr(0, pos); | |
1904 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
1905 | { | |
1906 | error_at(imp->location(), "bad number in import data: %qs", | |
1907 | real_str.c_str()); | |
1908 | return Expression::make_error(imp->location()); | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | std::string imag_str; | |
1913 | if (pos == std::string::npos) | |
1914 | imag_str = num; | |
1915 | else | |
1916 | imag_str = num.substr(pos); | |
1917 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
1918 | mpfr_t imag; | |
1919 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
1920 | { | |
1921 | error_at(imp->location(), "bad number in import data: %qs", | |
1922 | imag_str.c_str()); | |
1923 | return Expression::make_error(imp->location()); | |
1924 | } | |
1925 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
1926 | imp->location()); | |
1927 | mpfr_clear(real); | |
1928 | mpfr_clear(imag); | |
1929 | return ret; | |
1930 | } | |
1931 | else if (num.find('.') == std::string::npos | |
1932 | && num.find('E') == std::string::npos) | |
1933 | { | |
5d4b8566 | 1934 | bool is_character_constant = (!num.empty() |
1935 | && num[num.length() - 1] == '\''); | |
1936 | if (is_character_constant) | |
1937 | num = num.substr(0, num.length() - 1); | |
e440a328 | 1938 | mpz_t val; |
1939 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
1940 | { | |
1941 | error_at(imp->location(), "bad number in import data: %qs", | |
1942 | num.c_str()); | |
1943 | return Expression::make_error(imp->location()); | |
1944 | } | |
5d4b8566 | 1945 | Expression* ret; |
1946 | if (is_character_constant) | |
1947 | ret = Expression::make_character(&val, NULL, imp->location()); | |
1948 | else | |
1949 | ret = Expression::make_integer(&val, NULL, imp->location()); | |
e440a328 | 1950 | mpz_clear(val); |
1951 | return ret; | |
1952 | } | |
1953 | else | |
1954 | { | |
1955 | mpfr_t val; | |
1956 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
1957 | { | |
1958 | error_at(imp->location(), "bad number in import data: %qs", | |
1959 | num.c_str()); | |
1960 | return Expression::make_error(imp->location()); | |
1961 | } | |
1962 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
1963 | mpfr_clear(val); | |
1964 | return ret; | |
1965 | } | |
1966 | } | |
d751bb78 | 1967 | // Ast dump for integer expression. |
1968 | ||
1969 | void | |
1970 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1971 | { | |
5d4b8566 | 1972 | if (this->is_character_constant_) |
1973 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 1974 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 1975 | if (this->is_character_constant_) |
1976 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 1977 | } |
1978 | ||
e440a328 | 1979 | // Build a new integer value. |
1980 | ||
1981 | Expression* | |
5d4b8566 | 1982 | Expression::make_integer(const mpz_t* val, Type* type, Location location) |
1983 | { | |
1984 | return new Integer_expression(val, type, false, location); | |
1985 | } | |
1986 | ||
1987 | // Build a new character constant value. | |
1988 | ||
1989 | Expression* | |
1990 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 1991 | { |
5d4b8566 | 1992 | return new Integer_expression(val, type, true, location); |
e440a328 | 1993 | } |
1994 | ||
1995 | // Floats. | |
1996 | ||
1997 | class Float_expression : public Expression | |
1998 | { | |
1999 | public: | |
b13c66cd | 2000 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2001 | : Expression(EXPRESSION_FLOAT, location), |
2002 | type_(type) | |
2003 | { | |
2004 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2005 | } | |
2006 | ||
e440a328 | 2007 | // Write VAL to export data. |
2008 | static void | |
8b1c301d | 2009 | export_float(String_dump* exp, const mpfr_t val); |
2010 | ||
d751bb78 | 2011 | // Write VAL to dump file. |
2012 | static void | |
2013 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2014 | |
2015 | protected: | |
2016 | bool | |
2017 | do_is_constant() const | |
2018 | { return true; } | |
2019 | ||
2020 | bool | |
0c77715b | 2021 | do_numeric_constant_value(Numeric_constant* nc) const |
2022 | { | |
2023 | nc->set_float(this->type_, this->val_); | |
2024 | return true; | |
2025 | } | |
e440a328 | 2026 | |
2027 | Type* | |
2028 | do_type(); | |
2029 | ||
2030 | void | |
2031 | do_determine_type(const Type_context*); | |
2032 | ||
2033 | void | |
2034 | do_check_types(Gogo*); | |
2035 | ||
2036 | Expression* | |
2037 | do_copy() | |
2038 | { return Expression::make_float(&this->val_, this->type_, | |
2039 | this->location()); } | |
2040 | ||
2041 | tree | |
2042 | do_get_tree(Translate_context*); | |
2043 | ||
2044 | void | |
2045 | do_export(Export*) const; | |
2046 | ||
d751bb78 | 2047 | void |
2048 | do_dump_expression(Ast_dump_context*) const; | |
2049 | ||
e440a328 | 2050 | private: |
2051 | // The floating point value. | |
2052 | mpfr_t val_; | |
2053 | // The type so far. | |
2054 | Type* type_; | |
2055 | }; | |
2056 | ||
e440a328 | 2057 | // Return the current type. If we haven't set the type yet, we return |
2058 | // an abstract float type. | |
2059 | ||
2060 | Type* | |
2061 | Float_expression::do_type() | |
2062 | { | |
2063 | if (this->type_ == NULL) | |
2064 | this->type_ = Type::make_abstract_float_type(); | |
2065 | return this->type_; | |
2066 | } | |
2067 | ||
2068 | // Set the type of the float value. Here we may switch from an | |
2069 | // abstract type to a real type. | |
2070 | ||
2071 | void | |
2072 | Float_expression::do_determine_type(const Type_context* context) | |
2073 | { | |
2074 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2075 | ; | |
2076 | else if (context->type != NULL | |
2077 | && (context->type->integer_type() != NULL | |
2078 | || context->type->float_type() != NULL | |
2079 | || context->type->complex_type() != NULL)) | |
2080 | this->type_ = context->type; | |
2081 | else if (!context->may_be_abstract) | |
48080209 | 2082 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2083 | } |
2084 | ||
e440a328 | 2085 | // Check the type of a float value. |
2086 | ||
2087 | void | |
2088 | Float_expression::do_check_types(Gogo*) | |
2089 | { | |
0c77715b | 2090 | Type* type = this->type_; |
2091 | if (type == NULL) | |
e440a328 | 2092 | return; |
0c77715b | 2093 | Numeric_constant nc; |
2094 | nc.set_float(NULL, this->val_); | |
2095 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2096 | this->set_is_error(); |
e440a328 | 2097 | } |
2098 | ||
2099 | // Get a tree for a float constant. | |
2100 | ||
2101 | tree | |
2102 | Float_expression::do_get_tree(Translate_context* context) | |
2103 | { | |
2104 | Gogo* gogo = context->gogo(); | |
2105 | tree type; | |
2106 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
9f0e0513 | 2107 | type = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2108 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2109 | { | |
2110 | // We have an abstract integer type. We just hope for the best. | |
9f0e0513 | 2111 | type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo)); |
e440a328 | 2112 | } |
2113 | else | |
2114 | { | |
2115 | // If we still have an abstract type here, then this is being | |
2116 | // used in a constant expression which didn't get reduced. We | |
2117 | // just use float64 and hope for the best. | |
9f0e0513 | 2118 | Type* ft = Type::lookup_float_type("float64"); |
2119 | type = type_to_tree(ft->get_backend(gogo)); | |
e440a328 | 2120 | } |
2121 | return Expression::float_constant_tree(this->val_, type); | |
2122 | } | |
2123 | ||
8b1c301d | 2124 | // Write a floating point number to a string dump. |
e440a328 | 2125 | |
2126 | void | |
8b1c301d | 2127 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2128 | { |
2129 | mp_exp_t exponent; | |
2130 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2131 | if (*s == '-') | |
2132 | exp->write_c_string("-"); | |
2133 | exp->write_c_string("0."); | |
2134 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2135 | mpfr_free_str(s); | |
2136 | char buf[30]; | |
2137 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2138 | exp->write_c_string(buf); | |
2139 | } | |
2140 | ||
2141 | // Export a floating point number in a constant expression. | |
2142 | ||
2143 | void | |
2144 | Float_expression::do_export(Export* exp) const | |
2145 | { | |
2146 | Float_expression::export_float(exp, this->val_); | |
2147 | // A trailing space lets us reliably identify the end of the number. | |
2148 | exp->write_c_string(" "); | |
2149 | } | |
2150 | ||
d751bb78 | 2151 | // Dump a floating point number to the dump file. |
2152 | ||
2153 | void | |
2154 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2155 | { | |
8b1c301d | 2156 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2157 | } |
2158 | ||
e440a328 | 2159 | // Make a float expression. |
2160 | ||
2161 | Expression* | |
b13c66cd | 2162 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2163 | { |
2164 | return new Float_expression(val, type, location); | |
2165 | } | |
2166 | ||
2167 | // Complex numbers. | |
2168 | ||
2169 | class Complex_expression : public Expression | |
2170 | { | |
2171 | public: | |
2172 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2173 | Location location) |
e440a328 | 2174 | : Expression(EXPRESSION_COMPLEX, location), |
2175 | type_(type) | |
2176 | { | |
2177 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2178 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2179 | } | |
2180 | ||
8b1c301d | 2181 | // Write REAL/IMAG to string dump. |
e440a328 | 2182 | static void |
8b1c301d | 2183 | export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val); |
e440a328 | 2184 | |
d751bb78 | 2185 | // Write REAL/IMAG to dump context. |
2186 | static void | |
2187 | dump_complex(Ast_dump_context* ast_dump_context, | |
2188 | const mpfr_t real, const mpfr_t val); | |
2189 | ||
e440a328 | 2190 | protected: |
2191 | bool | |
2192 | do_is_constant() const | |
2193 | { return true; } | |
2194 | ||
2195 | bool | |
0c77715b | 2196 | do_numeric_constant_value(Numeric_constant* nc) const |
2197 | { | |
2198 | nc->set_complex(this->type_, this->real_, this->imag_); | |
2199 | return true; | |
2200 | } | |
e440a328 | 2201 | |
2202 | Type* | |
2203 | do_type(); | |
2204 | ||
2205 | void | |
2206 | do_determine_type(const Type_context*); | |
2207 | ||
2208 | void | |
2209 | do_check_types(Gogo*); | |
2210 | ||
2211 | Expression* | |
2212 | do_copy() | |
2213 | { | |
2214 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2215 | this->location()); | |
2216 | } | |
2217 | ||
2218 | tree | |
2219 | do_get_tree(Translate_context*); | |
2220 | ||
2221 | void | |
2222 | do_export(Export*) const; | |
2223 | ||
d751bb78 | 2224 | void |
2225 | do_dump_expression(Ast_dump_context*) const; | |
2226 | ||
e440a328 | 2227 | private: |
2228 | // The real part. | |
2229 | mpfr_t real_; | |
2230 | // The imaginary part; | |
2231 | mpfr_t imag_; | |
2232 | // The type if known. | |
2233 | Type* type_; | |
2234 | }; | |
2235 | ||
e440a328 | 2236 | // Return the current type. If we haven't set the type yet, we return |
2237 | // an abstract complex type. | |
2238 | ||
2239 | Type* | |
2240 | Complex_expression::do_type() | |
2241 | { | |
2242 | if (this->type_ == NULL) | |
2243 | this->type_ = Type::make_abstract_complex_type(); | |
2244 | return this->type_; | |
2245 | } | |
2246 | ||
2247 | // Set the type of the complex value. Here we may switch from an | |
2248 | // abstract type to a real type. | |
2249 | ||
2250 | void | |
2251 | Complex_expression::do_determine_type(const Type_context* context) | |
2252 | { | |
2253 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2254 | ; | |
2255 | else if (context->type != NULL | |
2256 | && context->type->complex_type() != NULL) | |
2257 | this->type_ = context->type; | |
2258 | else if (!context->may_be_abstract) | |
48080209 | 2259 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2260 | } |
2261 | ||
e440a328 | 2262 | // Check the type of a complex value. |
2263 | ||
2264 | void | |
2265 | Complex_expression::do_check_types(Gogo*) | |
2266 | { | |
0c77715b | 2267 | Type* type = this->type_; |
2268 | if (type == NULL) | |
e440a328 | 2269 | return; |
0c77715b | 2270 | Numeric_constant nc; |
2271 | nc.set_complex(NULL, this->real_, this->imag_); | |
2272 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2273 | this->set_is_error(); |
2274 | } | |
2275 | ||
2276 | // Get a tree for a complex constant. | |
2277 | ||
2278 | tree | |
2279 | Complex_expression::do_get_tree(Translate_context* context) | |
2280 | { | |
2281 | Gogo* gogo = context->gogo(); | |
2282 | tree type; | |
2283 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
9f0e0513 | 2284 | type = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2285 | else |
2286 | { | |
2287 | // If we still have an abstract type here, this this is being | |
2288 | // used in a constant expression which didn't get reduced. We | |
2289 | // just use complex128 and hope for the best. | |
9f0e0513 | 2290 | Type* ct = Type::lookup_complex_type("complex128"); |
2291 | type = type_to_tree(ct->get_backend(gogo)); | |
e440a328 | 2292 | } |
2293 | return Expression::complex_constant_tree(this->real_, this->imag_, type); | |
2294 | } | |
2295 | ||
2296 | // Write REAL/IMAG to export data. | |
2297 | ||
2298 | void | |
8b1c301d | 2299 | Complex_expression::export_complex(String_dump* exp, const mpfr_t real, |
e440a328 | 2300 | const mpfr_t imag) |
2301 | { | |
2302 | if (!mpfr_zero_p(real)) | |
2303 | { | |
2304 | Float_expression::export_float(exp, real); | |
2305 | if (mpfr_sgn(imag) > 0) | |
2306 | exp->write_c_string("+"); | |
2307 | } | |
2308 | Float_expression::export_float(exp, imag); | |
2309 | exp->write_c_string("i"); | |
2310 | } | |
2311 | ||
2312 | // Export a complex number in a constant expression. | |
2313 | ||
2314 | void | |
2315 | Complex_expression::do_export(Export* exp) const | |
2316 | { | |
2317 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2318 | // A trailing space lets us reliably identify the end of the number. | |
2319 | exp->write_c_string(" "); | |
2320 | } | |
2321 | ||
d751bb78 | 2322 | // Dump a complex expression to the dump file. |
2323 | ||
2324 | void | |
2325 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2326 | { | |
8b1c301d | 2327 | Complex_expression::export_complex(ast_dump_context, |
d751bb78 | 2328 | this->real_, |
2329 | this->imag_); | |
2330 | } | |
2331 | ||
e440a328 | 2332 | // Make a complex expression. |
2333 | ||
2334 | Expression* | |
2335 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2336 | Location location) |
e440a328 | 2337 | { |
2338 | return new Complex_expression(real, imag, type, location); | |
2339 | } | |
2340 | ||
d5b605df | 2341 | // Find a named object in an expression. |
2342 | ||
2343 | class Find_named_object : public Traverse | |
2344 | { | |
2345 | public: | |
2346 | Find_named_object(Named_object* no) | |
2347 | : Traverse(traverse_expressions), | |
2348 | no_(no), found_(false) | |
2349 | { } | |
2350 | ||
2351 | // Whether we found the object. | |
2352 | bool | |
2353 | found() const | |
2354 | { return this->found_; } | |
2355 | ||
2356 | protected: | |
2357 | int | |
2358 | expression(Expression**); | |
2359 | ||
2360 | private: | |
2361 | // The object we are looking for. | |
2362 | Named_object* no_; | |
2363 | // Whether we found it. | |
2364 | bool found_; | |
2365 | }; | |
2366 | ||
e440a328 | 2367 | // A reference to a const in an expression. |
2368 | ||
2369 | class Const_expression : public Expression | |
2370 | { | |
2371 | public: | |
b13c66cd | 2372 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2373 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2374 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2375 | { } |
2376 | ||
d5b605df | 2377 | Named_object* |
2378 | named_object() | |
2379 | { return this->constant_; } | |
2380 | ||
a7f064d5 | 2381 | // Check that the initializer does not refer to the constant itself. |
2382 | void | |
2383 | check_for_init_loop(); | |
2384 | ||
e440a328 | 2385 | protected: |
ba4aedd4 | 2386 | int |
2387 | do_traverse(Traverse*); | |
2388 | ||
e440a328 | 2389 | Expression* |
ceeb4318 | 2390 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2391 | |
2392 | bool | |
2393 | do_is_constant() const | |
2394 | { return true; } | |
2395 | ||
2396 | bool | |
0c77715b | 2397 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2398 | |
2399 | bool | |
af6b489a | 2400 | do_string_constant_value(std::string* val) const; |
e440a328 | 2401 | |
2402 | Type* | |
2403 | do_type(); | |
2404 | ||
2405 | // The type of a const is set by the declaration, not the use. | |
2406 | void | |
2407 | do_determine_type(const Type_context*); | |
2408 | ||
2409 | void | |
2410 | do_check_types(Gogo*); | |
2411 | ||
2412 | Expression* | |
2413 | do_copy() | |
2414 | { return this; } | |
2415 | ||
2416 | tree | |
2417 | do_get_tree(Translate_context* context); | |
2418 | ||
2419 | // When exporting a reference to a const as part of a const | |
2420 | // expression, we export the value. We ignore the fact that it has | |
2421 | // a name. | |
2422 | void | |
2423 | do_export(Export* exp) const | |
2424 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2425 | ||
d751bb78 | 2426 | void |
2427 | do_dump_expression(Ast_dump_context*) const; | |
2428 | ||
e440a328 | 2429 | private: |
2430 | // The constant. | |
2431 | Named_object* constant_; | |
2432 | // The type of this reference. This is used if the constant has an | |
2433 | // abstract type. | |
2434 | Type* type_; | |
13e818f5 | 2435 | // Used to prevent infinite recursion when a constant incorrectly |
2436 | // refers to itself. | |
2437 | mutable bool seen_; | |
e440a328 | 2438 | }; |
2439 | ||
ba4aedd4 | 2440 | // Traversal. |
2441 | ||
2442 | int | |
2443 | Const_expression::do_traverse(Traverse* traverse) | |
2444 | { | |
2445 | if (this->type_ != NULL) | |
2446 | return Type::traverse(this->type_, traverse); | |
2447 | return TRAVERSE_CONTINUE; | |
2448 | } | |
2449 | ||
e440a328 | 2450 | // Lower a constant expression. This is where we convert the |
2451 | // predeclared constant iota into an integer value. | |
2452 | ||
2453 | Expression* | |
ceeb4318 | 2454 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2455 | Statement_inserter*, int iota_value) | |
e440a328 | 2456 | { |
2457 | if (this->constant_->const_value()->expr()->classification() | |
2458 | == EXPRESSION_IOTA) | |
2459 | { | |
2460 | if (iota_value == -1) | |
2461 | { | |
2462 | error_at(this->location(), | |
2463 | "iota is only defined in const declarations"); | |
2464 | iota_value = 0; | |
2465 | } | |
2466 | mpz_t val; | |
2467 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2468 | Expression* ret = Expression::make_integer(&val, NULL, | |
2469 | this->location()); | |
2470 | mpz_clear(val); | |
2471 | return ret; | |
2472 | } | |
2473 | ||
2474 | // Make sure that the constant itself has been lowered. | |
2475 | gogo->lower_constant(this->constant_); | |
2476 | ||
2477 | return this; | |
2478 | } | |
2479 | ||
0c77715b | 2480 | // Return a numeric constant value. |
e440a328 | 2481 | |
2482 | bool | |
0c77715b | 2483 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2484 | { |
13e818f5 | 2485 | if (this->seen_) |
2486 | return false; | |
2487 | ||
e440a328 | 2488 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2489 | |
13e818f5 | 2490 | this->seen_ = true; |
2491 | ||
0c77715b | 2492 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2493 | |
13e818f5 | 2494 | this->seen_ = false; |
2495 | ||
e440a328 | 2496 | Type* ctype; |
2497 | if (this->type_ != NULL) | |
2498 | ctype = this->type_; | |
2499 | else | |
2500 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2501 | if (r && ctype != NULL) |
2502 | { | |
0c77715b | 2503 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2504 | return false; |
e440a328 | 2505 | } |
e440a328 | 2506 | |
e440a328 | 2507 | return r; |
2508 | } | |
2509 | ||
af6b489a | 2510 | bool |
2511 | Const_expression::do_string_constant_value(std::string* val) const | |
2512 | { | |
2513 | if (this->seen_) | |
2514 | return false; | |
2515 | ||
2516 | Expression* e = this->constant_->const_value()->expr(); | |
2517 | ||
2518 | this->seen_ = true; | |
2519 | bool ok = e->string_constant_value(val); | |
2520 | this->seen_ = false; | |
2521 | ||
2522 | return ok; | |
2523 | } | |
2524 | ||
e440a328 | 2525 | // Return the type of the const reference. |
2526 | ||
2527 | Type* | |
2528 | Const_expression::do_type() | |
2529 | { | |
2530 | if (this->type_ != NULL) | |
2531 | return this->type_; | |
13e818f5 | 2532 | |
2f78f012 | 2533 | Named_constant* nc = this->constant_->const_value(); |
2534 | ||
2535 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2536 | { |
2537 | this->report_error(_("constant refers to itself")); | |
2538 | this->type_ = Type::make_error_type(); | |
2539 | return this->type_; | |
2540 | } | |
2541 | ||
2542 | this->seen_ = true; | |
2543 | ||
e440a328 | 2544 | Type* ret = nc->type(); |
13e818f5 | 2545 | |
e440a328 | 2546 | if (ret != NULL) |
13e818f5 | 2547 | { |
2548 | this->seen_ = false; | |
2549 | return ret; | |
2550 | } | |
2551 | ||
e440a328 | 2552 | // During parsing, a named constant may have a NULL type, but we |
2553 | // must not return a NULL type here. | |
13e818f5 | 2554 | ret = nc->expr()->type(); |
2555 | ||
2556 | this->seen_ = false; | |
2557 | ||
2558 | return ret; | |
e440a328 | 2559 | } |
2560 | ||
2561 | // Set the type of the const reference. | |
2562 | ||
2563 | void | |
2564 | Const_expression::do_determine_type(const Type_context* context) | |
2565 | { | |
2566 | Type* ctype = this->constant_->const_value()->type(); | |
2567 | Type* cetype = (ctype != NULL | |
2568 | ? ctype | |
2569 | : this->constant_->const_value()->expr()->type()); | |
2570 | if (ctype != NULL && !ctype->is_abstract()) | |
2571 | ; | |
2572 | else if (context->type != NULL | |
0c77715b | 2573 | && context->type->is_numeric_type() |
2574 | && cetype->is_numeric_type()) | |
e440a328 | 2575 | this->type_ = context->type; |
2576 | else if (context->type != NULL | |
2577 | && context->type->is_string_type() | |
2578 | && cetype->is_string_type()) | |
2579 | this->type_ = context->type; | |
2580 | else if (context->type != NULL | |
2581 | && context->type->is_boolean_type() | |
2582 | && cetype->is_boolean_type()) | |
2583 | this->type_ = context->type; | |
2584 | else if (!context->may_be_abstract) | |
2585 | { | |
2586 | if (cetype->is_abstract()) | |
2587 | cetype = cetype->make_non_abstract_type(); | |
2588 | this->type_ = cetype; | |
2589 | } | |
2590 | } | |
2591 | ||
a7f064d5 | 2592 | // Check for a loop in which the initializer of a constant refers to |
2593 | // the constant itself. | |
e440a328 | 2594 | |
2595 | void | |
a7f064d5 | 2596 | Const_expression::check_for_init_loop() |
e440a328 | 2597 | { |
5c13bd80 | 2598 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2599 | return; |
2600 | ||
a7f064d5 | 2601 | if (this->seen_) |
2602 | { | |
2603 | this->report_error(_("constant refers to itself")); | |
2604 | this->type_ = Type::make_error_type(); | |
2605 | return; | |
2606 | } | |
2607 | ||
d5b605df | 2608 | Expression* init = this->constant_->const_value()->expr(); |
2609 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2610 | |
2611 | this->seen_ = true; | |
d5b605df | 2612 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2613 | this->seen_ = false; |
2614 | ||
d5b605df | 2615 | if (find_named_object.found()) |
2616 | { | |
5c13bd80 | 2617 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2618 | { |
2619 | this->report_error(_("constant refers to itself")); | |
2620 | this->type_ = Type::make_error_type(); | |
2621 | } | |
d5b605df | 2622 | return; |
2623 | } | |
a7f064d5 | 2624 | } |
2625 | ||
2626 | // Check types of a const reference. | |
2627 | ||
2628 | void | |
2629 | Const_expression::do_check_types(Gogo*) | |
2630 | { | |
5c13bd80 | 2631 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2632 | return; |
2633 | ||
2634 | this->check_for_init_loop(); | |
d5b605df | 2635 | |
0c77715b | 2636 | // Check that numeric constant fits in type. |
2637 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2638 | { |
0c77715b | 2639 | Numeric_constant nc; |
2640 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2641 | { |
0c77715b | 2642 | if (!nc.set_type(this->type_, true, this->location())) |
2643 | this->set_is_error(); | |
e440a328 | 2644 | } |
e440a328 | 2645 | } |
2646 | } | |
2647 | ||
2648 | // Return a tree for the const reference. | |
2649 | ||
2650 | tree | |
2651 | Const_expression::do_get_tree(Translate_context* context) | |
2652 | { | |
2653 | Gogo* gogo = context->gogo(); | |
2654 | tree type_tree; | |
2655 | if (this->type_ == NULL) | |
2656 | type_tree = NULL_TREE; | |
2657 | else | |
2658 | { | |
9f0e0513 | 2659 | type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2660 | if (type_tree == error_mark_node) |
2661 | return error_mark_node; | |
2662 | } | |
2663 | ||
2664 | // If the type has been set for this expression, but the underlying | |
2665 | // object is an abstract int or float, we try to get the abstract | |
2666 | // value. Otherwise we may lose something in the conversion. | |
2667 | if (this->type_ != NULL | |
0c77715b | 2668 | && this->type_->is_numeric_type() |
a68492b4 | 2669 | && (this->constant_->const_value()->type() == NULL |
2670 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2671 | { |
2672 | Expression* expr = this->constant_->const_value()->expr(); | |
0c77715b | 2673 | Numeric_constant nc; |
2674 | if (expr->numeric_constant_value(&nc) | |
2675 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2676 | { |
0c77715b | 2677 | Expression* e = nc.expression(this->location()); |
2678 | return e->get_tree(context); | |
e440a328 | 2679 | } |
e440a328 | 2680 | } |
2681 | ||
2682 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2683 | if (this->type_ == NULL | |
2684 | || const_tree == error_mark_node | |
2685 | || TREE_TYPE(const_tree) == error_mark_node) | |
2686 | return const_tree; | |
2687 | ||
2688 | tree ret; | |
2689 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2690 | ret = fold_convert(type_tree, const_tree); | |
2691 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2692 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2693 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2694 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2695 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2696 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2697 | else | |
c3e6f413 | 2698 | go_unreachable(); |
e440a328 | 2699 | return ret; |
2700 | } | |
2701 | ||
d751bb78 | 2702 | // Dump ast representation for constant expression. |
2703 | ||
2704 | void | |
2705 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2706 | { | |
2707 | ast_dump_context->ostream() << this->constant_->name(); | |
2708 | } | |
2709 | ||
e440a328 | 2710 | // Make a reference to a constant in an expression. |
2711 | ||
2712 | Expression* | |
2713 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2714 | Location location) |
e440a328 | 2715 | { |
2716 | return new Const_expression(constant, location); | |
2717 | } | |
2718 | ||
d5b605df | 2719 | // Find a named object in an expression. |
2720 | ||
2721 | int | |
2722 | Find_named_object::expression(Expression** pexpr) | |
2723 | { | |
2724 | switch ((*pexpr)->classification()) | |
2725 | { | |
2726 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2727 | { |
2728 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2729 | if (ce->named_object() == this->no_) | |
2730 | break; | |
2731 | ||
2732 | // We need to check a constant initializer explicitly, as | |
2733 | // loops here will not be caught by the loop checking for | |
2734 | // variable initializers. | |
2735 | ce->check_for_init_loop(); | |
2736 | ||
2737 | return TRAVERSE_CONTINUE; | |
2738 | } | |
2739 | ||
d5b605df | 2740 | case Expression::EXPRESSION_VAR_REFERENCE: |
2741 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2742 | break; | |
2743 | return TRAVERSE_CONTINUE; | |
2744 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2745 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2746 | break; | |
2747 | return TRAVERSE_CONTINUE; | |
2748 | default: | |
2749 | return TRAVERSE_CONTINUE; | |
2750 | } | |
2751 | this->found_ = true; | |
2752 | return TRAVERSE_EXIT; | |
2753 | } | |
2754 | ||
e440a328 | 2755 | // The nil value. |
2756 | ||
2757 | class Nil_expression : public Expression | |
2758 | { | |
2759 | public: | |
b13c66cd | 2760 | Nil_expression(Location location) |
e440a328 | 2761 | : Expression(EXPRESSION_NIL, location) |
2762 | { } | |
2763 | ||
2764 | static Expression* | |
2765 | do_import(Import*); | |
2766 | ||
2767 | protected: | |
2768 | bool | |
2769 | do_is_constant() const | |
2770 | { return true; } | |
2771 | ||
2772 | Type* | |
2773 | do_type() | |
2774 | { return Type::make_nil_type(); } | |
2775 | ||
2776 | void | |
2777 | do_determine_type(const Type_context*) | |
2778 | { } | |
2779 | ||
2780 | Expression* | |
2781 | do_copy() | |
2782 | { return this; } | |
2783 | ||
2784 | tree | |
2785 | do_get_tree(Translate_context*) | |
2786 | { return null_pointer_node; } | |
2787 | ||
2788 | void | |
2789 | do_export(Export* exp) const | |
2790 | { exp->write_c_string("nil"); } | |
d751bb78 | 2791 | |
2792 | void | |
2793 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2794 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 2795 | }; |
2796 | ||
2797 | // Import a nil expression. | |
2798 | ||
2799 | Expression* | |
2800 | Nil_expression::do_import(Import* imp) | |
2801 | { | |
2802 | imp->require_c_string("nil"); | |
2803 | return Expression::make_nil(imp->location()); | |
2804 | } | |
2805 | ||
2806 | // Make a nil expression. | |
2807 | ||
2808 | Expression* | |
b13c66cd | 2809 | Expression::make_nil(Location location) |
e440a328 | 2810 | { |
2811 | return new Nil_expression(location); | |
2812 | } | |
2813 | ||
2814 | // The value of the predeclared constant iota. This is little more | |
2815 | // than a marker. This will be lowered to an integer in | |
2816 | // Const_expression::do_lower, which is where we know the value that | |
2817 | // it should have. | |
2818 | ||
2819 | class Iota_expression : public Parser_expression | |
2820 | { | |
2821 | public: | |
b13c66cd | 2822 | Iota_expression(Location location) |
e440a328 | 2823 | : Parser_expression(EXPRESSION_IOTA, location) |
2824 | { } | |
2825 | ||
2826 | protected: | |
2827 | Expression* | |
ceeb4318 | 2828 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 2829 | { go_unreachable(); } |
e440a328 | 2830 | |
2831 | // There should only ever be one of these. | |
2832 | Expression* | |
2833 | do_copy() | |
c3e6f413 | 2834 | { go_unreachable(); } |
d751bb78 | 2835 | |
2836 | void | |
2837 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2838 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 2839 | }; |
2840 | ||
2841 | // Make an iota expression. This is only called for one case: the | |
2842 | // value of the predeclared constant iota. | |
2843 | ||
2844 | Expression* | |
2845 | Expression::make_iota() | |
2846 | { | |
b13c66cd | 2847 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 2848 | return &iota_expression; |
2849 | } | |
2850 | ||
2851 | // A type conversion expression. | |
2852 | ||
2853 | class Type_conversion_expression : public Expression | |
2854 | { | |
2855 | public: | |
2856 | Type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 2857 | Location location) |
e440a328 | 2858 | : Expression(EXPRESSION_CONVERSION, location), |
2859 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2860 | { } | |
2861 | ||
2862 | // Return the type to which we are converting. | |
2863 | Type* | |
2864 | type() const | |
2865 | { return this->type_; } | |
2866 | ||
2867 | // Return the expression which we are converting. | |
2868 | Expression* | |
2869 | expr() const | |
2870 | { return this->expr_; } | |
2871 | ||
2872 | // Permit converting from one function type to another. This is | |
2873 | // used internally for method expressions. | |
2874 | void | |
2875 | set_may_convert_function_types() | |
2876 | { | |
2877 | this->may_convert_function_types_ = true; | |
2878 | } | |
2879 | ||
2880 | // Import a type conversion expression. | |
2881 | static Expression* | |
2882 | do_import(Import*); | |
2883 | ||
2884 | protected: | |
2885 | int | |
2886 | do_traverse(Traverse* traverse); | |
2887 | ||
2888 | Expression* | |
ceeb4318 | 2889 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2890 | |
2891 | bool | |
2892 | do_is_constant() const | |
2893 | { return this->expr_->is_constant(); } | |
2894 | ||
2895 | bool | |
0c77715b | 2896 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 2897 | |
2898 | bool | |
2899 | do_string_constant_value(std::string*) const; | |
2900 | ||
2901 | Type* | |
2902 | do_type() | |
2903 | { return this->type_; } | |
2904 | ||
2905 | void | |
2906 | do_determine_type(const Type_context*) | |
2907 | { | |
2908 | Type_context subcontext(this->type_, false); | |
2909 | this->expr_->determine_type(&subcontext); | |
2910 | } | |
2911 | ||
2912 | void | |
2913 | do_check_types(Gogo*); | |
2914 | ||
2915 | Expression* | |
2916 | do_copy() | |
2917 | { | |
2918 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
2919 | this->location()); | |
2920 | } | |
2921 | ||
2922 | tree | |
2923 | do_get_tree(Translate_context* context); | |
2924 | ||
2925 | void | |
2926 | do_export(Export*) const; | |
2927 | ||
d751bb78 | 2928 | void |
2929 | do_dump_expression(Ast_dump_context*) const; | |
2930 | ||
e440a328 | 2931 | private: |
2932 | // The type to convert to. | |
2933 | Type* type_; | |
2934 | // The expression to convert. | |
2935 | Expression* expr_; | |
2936 | // True if this is permitted to convert function types. This is | |
2937 | // used internally for method expressions. | |
2938 | bool may_convert_function_types_; | |
2939 | }; | |
2940 | ||
2941 | // Traversal. | |
2942 | ||
2943 | int | |
2944 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
2945 | { | |
2946 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
2947 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
2948 | return TRAVERSE_EXIT; | |
2949 | return TRAVERSE_CONTINUE; | |
2950 | } | |
2951 | ||
2952 | // Convert to a constant at lowering time. | |
2953 | ||
2954 | Expression* | |
ceeb4318 | 2955 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
2956 | Statement_inserter*, int) | |
e440a328 | 2957 | { |
2958 | Type* type = this->type_; | |
2959 | Expression* val = this->expr_; | |
b13c66cd | 2960 | Location location = this->location(); |
e440a328 | 2961 | |
0c77715b | 2962 | if (type->is_numeric_type()) |
e440a328 | 2963 | { |
0c77715b | 2964 | Numeric_constant nc; |
2965 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 2966 | { |
0c77715b | 2967 | if (!nc.set_type(type, true, location)) |
2968 | return Expression::make_error(location); | |
2969 | return nc.expression(location); | |
e440a328 | 2970 | } |
e440a328 | 2971 | } |
2972 | ||
55072f2b | 2973 | if (type->is_slice_type()) |
e440a328 | 2974 | { |
2975 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 2976 | bool is_byte = (element_type->integer_type() != NULL |
2977 | && element_type->integer_type()->is_byte()); | |
2978 | bool is_rune = (element_type->integer_type() != NULL | |
2979 | && element_type->integer_type()->is_rune()); | |
2980 | if (is_byte || is_rune) | |
e440a328 | 2981 | { |
2982 | std::string s; | |
2983 | if (val->string_constant_value(&s)) | |
2984 | { | |
2985 | Expression_list* vals = new Expression_list(); | |
2986 | if (is_byte) | |
2987 | { | |
2988 | for (std::string::const_iterator p = s.begin(); | |
2989 | p != s.end(); | |
2990 | p++) | |
2991 | { | |
2992 | mpz_t val; | |
2993 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
2994 | Expression* v = Expression::make_integer(&val, | |
2995 | element_type, | |
2996 | location); | |
2997 | vals->push_back(v); | |
2998 | mpz_clear(val); | |
2999 | } | |
3000 | } | |
3001 | else | |
3002 | { | |
3003 | const char *p = s.data(); | |
3004 | const char *pend = s.data() + s.length(); | |
3005 | while (p < pend) | |
3006 | { | |
3007 | unsigned int c; | |
3008 | int adv = Lex::fetch_char(p, &c); | |
3009 | if (adv == 0) | |
3010 | { | |
3011 | warning_at(this->location(), 0, | |
3012 | "invalid UTF-8 encoding"); | |
3013 | adv = 1; | |
3014 | } | |
3015 | p += adv; | |
3016 | mpz_t val; | |
3017 | mpz_init_set_ui(val, c); | |
3018 | Expression* v = Expression::make_integer(&val, | |
3019 | element_type, | |
3020 | location); | |
3021 | vals->push_back(v); | |
3022 | mpz_clear(val); | |
3023 | } | |
3024 | } | |
3025 | ||
3026 | return Expression::make_slice_composite_literal(type, vals, | |
3027 | location); | |
3028 | } | |
3029 | } | |
3030 | } | |
3031 | ||
3032 | return this; | |
3033 | } | |
3034 | ||
0c77715b | 3035 | // Return the constant numeric value if there is one. |
e440a328 | 3036 | |
3037 | bool | |
0c77715b | 3038 | Type_conversion_expression::do_numeric_constant_value( |
3039 | Numeric_constant* nc) const | |
e440a328 | 3040 | { |
0c77715b | 3041 | if (!this->type_->is_numeric_type()) |
e440a328 | 3042 | return false; |
0c77715b | 3043 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3044 | return false; |
0c77715b | 3045 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3046 | } |
3047 | ||
3048 | // Return the constant string value if there is one. | |
3049 | ||
3050 | bool | |
3051 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3052 | { | |
3053 | if (this->type_->is_string_type() | |
3054 | && this->expr_->type()->integer_type() != NULL) | |
3055 | { | |
0c77715b | 3056 | Numeric_constant nc; |
3057 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3058 | { |
0c77715b | 3059 | unsigned long ival; |
3060 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3061 | { |
0c77715b | 3062 | val->clear(); |
3063 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3064 | return true; |
3065 | } | |
3066 | } | |
e440a328 | 3067 | } |
3068 | ||
3069 | // FIXME: Could handle conversion from const []int here. | |
3070 | ||
3071 | return false; | |
3072 | } | |
3073 | ||
3074 | // Check that types are convertible. | |
3075 | ||
3076 | void | |
3077 | Type_conversion_expression::do_check_types(Gogo*) | |
3078 | { | |
3079 | Type* type = this->type_; | |
3080 | Type* expr_type = this->expr_->type(); | |
3081 | std::string reason; | |
3082 | ||
5c13bd80 | 3083 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3084 | { |
842f6425 | 3085 | this->set_is_error(); |
3086 | return; | |
3087 | } | |
3088 | ||
e440a328 | 3089 | if (this->may_convert_function_types_ |
3090 | && type->function_type() != NULL | |
3091 | && expr_type->function_type() != NULL) | |
3092 | return; | |
3093 | ||
3094 | if (Type::are_convertible(type, expr_type, &reason)) | |
3095 | return; | |
3096 | ||
3097 | error_at(this->location(), "%s", reason.c_str()); | |
3098 | this->set_is_error(); | |
3099 | } | |
3100 | ||
3101 | // Get a tree for a type conversion. | |
3102 | ||
3103 | tree | |
3104 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3105 | { | |
3106 | Gogo* gogo = context->gogo(); | |
9f0e0513 | 3107 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 3108 | tree expr_tree = this->expr_->get_tree(context); |
3109 | ||
3110 | if (type_tree == error_mark_node | |
3111 | || expr_tree == error_mark_node | |
3112 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3113 | return error_mark_node; | |
3114 | ||
3115 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3116 | return fold_convert(type_tree, expr_tree); | |
3117 | ||
3118 | Type* type = this->type_; | |
3119 | Type* expr_type = this->expr_->type(); | |
3120 | tree ret; | |
3121 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3122 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3123 | expr_tree, this->location()); | |
3124 | else if (type->integer_type() != NULL) | |
3125 | { | |
3126 | if (expr_type->integer_type() != NULL | |
3127 | || expr_type->float_type() != NULL | |
3128 | || expr_type->is_unsafe_pointer_type()) | |
3129 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3130 | else | |
c3e6f413 | 3131 | go_unreachable(); |
e440a328 | 3132 | } |
3133 | else if (type->float_type() != NULL) | |
3134 | { | |
3135 | if (expr_type->integer_type() != NULL | |
3136 | || expr_type->float_type() != NULL) | |
3137 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3138 | else | |
c3e6f413 | 3139 | go_unreachable(); |
e440a328 | 3140 | } |
3141 | else if (type->complex_type() != NULL) | |
3142 | { | |
3143 | if (expr_type->complex_type() != NULL) | |
3144 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3145 | else | |
c3e6f413 | 3146 | go_unreachable(); |
e440a328 | 3147 | } |
3148 | else if (type->is_string_type() | |
3149 | && expr_type->integer_type() != NULL) | |
3150 | { | |
1b1f2abf | 3151 | Type* int_type = Type::lookup_integer_type("int"); |
3152 | tree int_type_tree = type_to_tree(int_type->get_backend(gogo)); | |
3153 | ||
3154 | expr_tree = fold_convert(int_type_tree, expr_tree); | |
e440a328 | 3155 | if (host_integerp(expr_tree, 0)) |
3156 | { | |
3157 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3158 | std::string s; | |
3159 | Lex::append_char(intval, true, &s, this->location()); | |
3160 | Expression* se = Expression::make_string(s, this->location()); | |
3161 | return se->get_tree(context); | |
3162 | } | |
3163 | ||
3164 | static tree int_to_string_fndecl; | |
3165 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3166 | this->location(), | |
3167 | "__go_int_to_string", | |
3168 | 1, | |
3169 | type_tree, | |
1b1f2abf | 3170 | int_type_tree, |
3171 | expr_tree); | |
e440a328 | 3172 | } |
55072f2b | 3173 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3174 | { |
e440a328 | 3175 | if (!DECL_P(expr_tree)) |
3176 | expr_tree = save_expr(expr_tree); | |
1b1f2abf | 3177 | |
3178 | Type* int_type = Type::lookup_integer_type("int"); | |
3179 | tree int_type_tree = type_to_tree(int_type->get_backend(gogo)); | |
3180 | ||
55072f2b | 3181 | Array_type* a = expr_type->array_type(); |
e440a328 | 3182 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3183 | go_assert(e->integer_type() != NULL); |
e440a328 | 3184 | tree valptr = fold_convert(const_ptr_type_node, |
3185 | a->value_pointer_tree(gogo, expr_tree)); | |
3186 | tree len = a->length_tree(gogo, expr_tree); | |
1b1f2abf | 3187 | len = fold_convert_loc(this->location().gcc_location(), int_type_tree, |
b13c66cd | 3188 | len); |
60963afd | 3189 | if (e->integer_type()->is_byte()) |
e440a328 | 3190 | { |
3191 | static tree byte_array_to_string_fndecl; | |
3192 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3193 | this->location(), | |
3194 | "__go_byte_array_to_string", | |
3195 | 2, | |
3196 | type_tree, | |
3197 | const_ptr_type_node, | |
3198 | valptr, | |
1b1f2abf | 3199 | int_type_tree, |
e440a328 | 3200 | len); |
3201 | } | |
3202 | else | |
3203 | { | |
60963afd | 3204 | go_assert(e->integer_type()->is_rune()); |
e440a328 | 3205 | static tree int_array_to_string_fndecl; |
3206 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3207 | this->location(), | |
3208 | "__go_int_array_to_string", | |
3209 | 2, | |
3210 | type_tree, | |
3211 | const_ptr_type_node, | |
3212 | valptr, | |
1b1f2abf | 3213 | int_type_tree, |
e440a328 | 3214 | len); |
3215 | } | |
3216 | } | |
411eb89e | 3217 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3218 | { |
3219 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3220 | go_assert(e->integer_type() != NULL); |
60963afd | 3221 | if (e->integer_type()->is_byte()) |
e440a328 | 3222 | { |
ef43e66c | 3223 | tree string_to_byte_array_fndecl = NULL_TREE; |
e440a328 | 3224 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, |
3225 | this->location(), | |
3226 | "__go_string_to_byte_array", | |
3227 | 1, | |
3228 | type_tree, | |
3229 | TREE_TYPE(expr_tree), | |
3230 | expr_tree); | |
3231 | } | |
3232 | else | |
3233 | { | |
60963afd | 3234 | go_assert(e->integer_type()->is_rune()); |
ef43e66c | 3235 | tree string_to_int_array_fndecl = NULL_TREE; |
e440a328 | 3236 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, |
3237 | this->location(), | |
3238 | "__go_string_to_int_array", | |
3239 | 1, | |
3240 | type_tree, | |
3241 | TREE_TYPE(expr_tree), | |
3242 | expr_tree); | |
3243 | } | |
3244 | } | |
3245 | else if ((type->is_unsafe_pointer_type() | |
3246 | && expr_type->points_to() != NULL) | |
3247 | || (expr_type->is_unsafe_pointer_type() | |
3248 | && type->points_to() != NULL)) | |
3249 | ret = fold_convert(type_tree, expr_tree); | |
3250 | else if (type->is_unsafe_pointer_type() | |
3251 | && expr_type->integer_type() != NULL) | |
3252 | ret = convert_to_pointer(type_tree, expr_tree); | |
3253 | else if (this->may_convert_function_types_ | |
3254 | && type->function_type() != NULL | |
3255 | && expr_type->function_type() != NULL) | |
b13c66cd | 3256 | ret = fold_convert_loc(this->location().gcc_location(), type_tree, |
3257 | expr_tree); | |
e440a328 | 3258 | else |
3259 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3260 | expr_tree, this->location()); | |
3261 | ||
3262 | return ret; | |
3263 | } | |
3264 | ||
3265 | // Output a type conversion in a constant expression. | |
3266 | ||
3267 | void | |
3268 | Type_conversion_expression::do_export(Export* exp) const | |
3269 | { | |
3270 | exp->write_c_string("convert("); | |
3271 | exp->write_type(this->type_); | |
3272 | exp->write_c_string(", "); | |
3273 | this->expr_->export_expression(exp); | |
3274 | exp->write_c_string(")"); | |
3275 | } | |
3276 | ||
3277 | // Import a type conversion or a struct construction. | |
3278 | ||
3279 | Expression* | |
3280 | Type_conversion_expression::do_import(Import* imp) | |
3281 | { | |
3282 | imp->require_c_string("convert("); | |
3283 | Type* type = imp->read_type(); | |
3284 | imp->require_c_string(", "); | |
3285 | Expression* val = Expression::import_expression(imp); | |
3286 | imp->require_c_string(")"); | |
3287 | return Expression::make_cast(type, val, imp->location()); | |
3288 | } | |
3289 | ||
d751bb78 | 3290 | // Dump ast representation for a type conversion expression. |
3291 | ||
3292 | void | |
3293 | Type_conversion_expression::do_dump_expression( | |
3294 | Ast_dump_context* ast_dump_context) const | |
3295 | { | |
3296 | ast_dump_context->dump_type(this->type_); | |
3297 | ast_dump_context->ostream() << "("; | |
3298 | ast_dump_context->dump_expression(this->expr_); | |
3299 | ast_dump_context->ostream() << ") "; | |
3300 | } | |
3301 | ||
e440a328 | 3302 | // Make a type cast expression. |
3303 | ||
3304 | Expression* | |
b13c66cd | 3305 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3306 | { |
3307 | if (type->is_error_type() || val->is_error_expression()) | |
3308 | return Expression::make_error(location); | |
3309 | return new Type_conversion_expression(type, val, location); | |
3310 | } | |
3311 | ||
9581e91d | 3312 | // An unsafe type conversion, used to pass values to builtin functions. |
3313 | ||
3314 | class Unsafe_type_conversion_expression : public Expression | |
3315 | { | |
3316 | public: | |
3317 | Unsafe_type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 3318 | Location location) |
9581e91d | 3319 | : Expression(EXPRESSION_UNSAFE_CONVERSION, location), |
3320 | type_(type), expr_(expr) | |
3321 | { } | |
3322 | ||
3323 | protected: | |
3324 | int | |
3325 | do_traverse(Traverse* traverse); | |
3326 | ||
3327 | Type* | |
3328 | do_type() | |
3329 | { return this->type_; } | |
3330 | ||
3331 | void | |
3332 | do_determine_type(const Type_context*) | |
a9182619 | 3333 | { this->expr_->determine_type_no_context(); } |
9581e91d | 3334 | |
3335 | Expression* | |
3336 | do_copy() | |
3337 | { | |
3338 | return new Unsafe_type_conversion_expression(this->type_, | |
3339 | this->expr_->copy(), | |
3340 | this->location()); | |
3341 | } | |
3342 | ||
3343 | tree | |
3344 | do_get_tree(Translate_context*); | |
3345 | ||
d751bb78 | 3346 | void |
3347 | do_dump_expression(Ast_dump_context*) const; | |
3348 | ||
9581e91d | 3349 | private: |
3350 | // The type to convert to. | |
3351 | Type* type_; | |
3352 | // The expression to convert. | |
3353 | Expression* expr_; | |
3354 | }; | |
3355 | ||
3356 | // Traversal. | |
3357 | ||
3358 | int | |
3359 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3360 | { | |
3361 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3362 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3363 | return TRAVERSE_EXIT; | |
3364 | return TRAVERSE_CONTINUE; | |
3365 | } | |
3366 | ||
3367 | // Convert to backend representation. | |
3368 | ||
3369 | tree | |
3370 | Unsafe_type_conversion_expression::do_get_tree(Translate_context* context) | |
3371 | { | |
3372 | // We are only called for a limited number of cases. | |
3373 | ||
3374 | Type* t = this->type_; | |
3375 | Type* et = this->expr_->type(); | |
3376 | ||
9f0e0513 | 3377 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
9581e91d | 3378 | tree expr_tree = this->expr_->get_tree(context); |
3379 | if (type_tree == error_mark_node || expr_tree == error_mark_node) | |
3380 | return error_mark_node; | |
3381 | ||
b13c66cd | 3382 | Location loc = this->location(); |
9581e91d | 3383 | |
3384 | bool use_view_convert = false; | |
411eb89e | 3385 | if (t->is_slice_type()) |
9581e91d | 3386 | { |
411eb89e | 3387 | go_assert(et->is_slice_type()); |
9581e91d | 3388 | use_view_convert = true; |
3389 | } | |
3390 | else if (t->map_type() != NULL) | |
c484d925 | 3391 | go_assert(et->map_type() != NULL); |
9581e91d | 3392 | else if (t->channel_type() != NULL) |
c484d925 | 3393 | go_assert(et->channel_type() != NULL); |
09ea332d | 3394 | else if (t->points_to() != NULL) |
c484d925 | 3395 | go_assert(et->points_to() != NULL || et->is_nil_type()); |
9581e91d | 3396 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3397 | go_assert(t->points_to() != NULL); |
9581e91d | 3398 | else if (t->interface_type() != NULL && !t->interface_type()->is_empty()) |
3399 | { | |
c484d925 | 3400 | go_assert(et->interface_type() != NULL |
9581e91d | 3401 | && !et->interface_type()->is_empty()); |
3402 | use_view_convert = true; | |
3403 | } | |
3404 | else if (t->interface_type() != NULL && t->interface_type()->is_empty()) | |
3405 | { | |
c484d925 | 3406 | go_assert(et->interface_type() != NULL |
9581e91d | 3407 | && et->interface_type()->is_empty()); |
3408 | use_view_convert = true; | |
3409 | } | |
588e3cf9 | 3410 | else if (t->integer_type() != NULL) |
3411 | { | |
c484d925 | 3412 | go_assert(et->is_boolean_type() |
588e3cf9 | 3413 | || et->integer_type() != NULL |
3414 | || et->function_type() != NULL | |
3415 | || et->points_to() != NULL | |
3416 | || et->map_type() != NULL | |
3417 | || et->channel_type() != NULL); | |
3418 | return convert_to_integer(type_tree, expr_tree); | |
3419 | } | |
9581e91d | 3420 | else |
c3e6f413 | 3421 | go_unreachable(); |
9581e91d | 3422 | |
3423 | if (use_view_convert) | |
b13c66cd | 3424 | return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree, |
3425 | expr_tree); | |
9581e91d | 3426 | else |
b13c66cd | 3427 | return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree); |
9581e91d | 3428 | } |
3429 | ||
d751bb78 | 3430 | // Dump ast representation for an unsafe type conversion expression. |
3431 | ||
3432 | void | |
3433 | Unsafe_type_conversion_expression::do_dump_expression( | |
3434 | Ast_dump_context* ast_dump_context) const | |
3435 | { | |
3436 | ast_dump_context->dump_type(this->type_); | |
3437 | ast_dump_context->ostream() << "("; | |
3438 | ast_dump_context->dump_expression(this->expr_); | |
3439 | ast_dump_context->ostream() << ") "; | |
3440 | } | |
3441 | ||
9581e91d | 3442 | // Make an unsafe type conversion expression. |
3443 | ||
3444 | Expression* | |
3445 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3446 | Location location) |
9581e91d | 3447 | { |
3448 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3449 | } | |
3450 | ||
e440a328 | 3451 | // Unary expressions. |
3452 | ||
3453 | class Unary_expression : public Expression | |
3454 | { | |
3455 | public: | |
b13c66cd | 3456 | Unary_expression(Operator op, Expression* expr, Location location) |
e440a328 | 3457 | : Expression(EXPRESSION_UNARY, location), |
09ea332d | 3458 | op_(op), escapes_(true), create_temp_(false), expr_(expr) |
e440a328 | 3459 | { } |
3460 | ||
3461 | // Return the operator. | |
3462 | Operator | |
3463 | op() const | |
3464 | { return this->op_; } | |
3465 | ||
3466 | // Return the operand. | |
3467 | Expression* | |
3468 | operand() const | |
3469 | { return this->expr_; } | |
3470 | ||
3471 | // Record that an address expression does not escape. | |
3472 | void | |
3473 | set_does_not_escape() | |
3474 | { | |
c484d925 | 3475 | go_assert(this->op_ == OPERATOR_AND); |
e440a328 | 3476 | this->escapes_ = false; |
3477 | } | |
3478 | ||
09ea332d | 3479 | // Record that this is an address expression which should create a |
3480 | // temporary variable if necessary. This is used for method calls. | |
3481 | void | |
3482 | set_create_temp() | |
3483 | { | |
3484 | go_assert(this->op_ == OPERATOR_AND); | |
3485 | this->create_temp_ = true; | |
3486 | } | |
3487 | ||
0c77715b | 3488 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3489 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3490 | static bool |
0c77715b | 3491 | eval_constant(Operator op, const Numeric_constant* unc, |
3492 | Location, Numeric_constant* nc); | |
e440a328 | 3493 | |
3494 | static Expression* | |
3495 | do_import(Import*); | |
3496 | ||
3497 | protected: | |
3498 | int | |
3499 | do_traverse(Traverse* traverse) | |
3500 | { return Expression::traverse(&this->expr_, traverse); } | |
3501 | ||
3502 | Expression* | |
ceeb4318 | 3503 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 3504 | |
3505 | bool | |
3506 | do_is_constant() const; | |
3507 | ||
3508 | bool | |
0c77715b | 3509 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 3510 | |
3511 | Type* | |
3512 | do_type(); | |
3513 | ||
3514 | void | |
3515 | do_determine_type(const Type_context*); | |
3516 | ||
3517 | void | |
3518 | do_check_types(Gogo*); | |
3519 | ||
3520 | Expression* | |
3521 | do_copy() | |
3522 | { | |
3523 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3524 | this->location()); | |
3525 | } | |
3526 | ||
baef9f7a | 3527 | bool |
3528 | do_must_eval_subexpressions_in_order(int*) const | |
3529 | { return this->op_ == OPERATOR_MULT; } | |
3530 | ||
e440a328 | 3531 | bool |
3532 | do_is_addressable() const | |
3533 | { return this->op_ == OPERATOR_MULT; } | |
3534 | ||
3535 | tree | |
3536 | do_get_tree(Translate_context*); | |
3537 | ||
3538 | void | |
3539 | do_export(Export*) const; | |
3540 | ||
d751bb78 | 3541 | void |
3542 | do_dump_expression(Ast_dump_context*) const; | |
3543 | ||
e440a328 | 3544 | private: |
3545 | // The unary operator to apply. | |
3546 | Operator op_; | |
3547 | // Normally true. False if this is an address expression which does | |
3548 | // not escape the current function. | |
3549 | bool escapes_; | |
09ea332d | 3550 | // True if this is an address expression which should create a |
3551 | // temporary variable if necessary. | |
3552 | bool create_temp_; | |
e440a328 | 3553 | // The operand. |
3554 | Expression* expr_; | |
3555 | }; | |
3556 | ||
3557 | // If we are taking the address of a composite literal, and the | |
3558 | // contents are not constant, then we want to make a heap composite | |
3559 | // instead. | |
3560 | ||
3561 | Expression* | |
ceeb4318 | 3562 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3563 | { |
b13c66cd | 3564 | Location loc = this->location(); |
e440a328 | 3565 | Operator op = this->op_; |
3566 | Expression* expr = this->expr_; | |
3567 | ||
3568 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3569 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3570 | ||
3571 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3572 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3573 | // analysis here? This case is found in math/unsafe.go and is | |
3574 | // therefore worth special casing. | |
3575 | if (op == OPERATOR_MULT) | |
3576 | { | |
3577 | Expression* e = expr; | |
3578 | while (e->classification() == EXPRESSION_CONVERSION) | |
3579 | { | |
3580 | Type_conversion_expression* te | |
3581 | = static_cast<Type_conversion_expression*>(e); | |
3582 | e = te->expr(); | |
3583 | } | |
3584 | ||
3585 | if (e->classification() == EXPRESSION_UNARY) | |
3586 | { | |
3587 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3588 | if (ue->op_ == OPERATOR_AND) | |
3589 | { | |
3590 | if (e == expr) | |
3591 | { | |
3592 | // *&x == x. | |
3593 | return ue->expr_; | |
3594 | } | |
3595 | ue->set_does_not_escape(); | |
3596 | } | |
3597 | } | |
3598 | } | |
3599 | ||
55661ce9 | 3600 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3601 | // having to deal with TYPE_VOID in other places. | |
3602 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3603 | { | |
3604 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3605 | return Expression::make_error(this->location()); | |
3606 | } | |
3607 | ||
59a401fe | 3608 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3609 | { |
0c77715b | 3610 | Numeric_constant nc; |
3611 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3612 | { |
0c77715b | 3613 | Numeric_constant result; |
3614 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3615 | return result.expression(loc); | |
e440a328 | 3616 | } |
3617 | } | |
3618 | ||
3619 | return this; | |
3620 | } | |
3621 | ||
3622 | // Return whether a unary expression is a constant. | |
3623 | ||
3624 | bool | |
3625 | Unary_expression::do_is_constant() const | |
3626 | { | |
3627 | if (this->op_ == OPERATOR_MULT) | |
3628 | { | |
3629 | // Indirecting through a pointer is only constant if the object | |
3630 | // to which the expression points is constant, but we currently | |
3631 | // have no way to determine that. | |
3632 | return false; | |
3633 | } | |
3634 | else if (this->op_ == OPERATOR_AND) | |
3635 | { | |
3636 | // Taking the address of a variable is constant if it is a | |
3637 | // global variable, not constant otherwise. In other cases | |
3638 | // taking the address is probably not a constant. | |
3639 | Var_expression* ve = this->expr_->var_expression(); | |
3640 | if (ve != NULL) | |
3641 | { | |
3642 | Named_object* no = ve->named_object(); | |
3643 | return no->is_variable() && no->var_value()->is_global(); | |
3644 | } | |
3645 | return false; | |
3646 | } | |
3647 | else | |
3648 | return this->expr_->is_constant(); | |
3649 | } | |
3650 | ||
0c77715b | 3651 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3652 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3653 | |
3654 | bool | |
0c77715b | 3655 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3656 | Location location, Numeric_constant* nc) | |
e440a328 | 3657 | { |
3658 | switch (op) | |
3659 | { | |
3660 | case OPERATOR_PLUS: | |
0c77715b | 3661 | *nc = *unc; |
e440a328 | 3662 | return true; |
0c77715b | 3663 | |
e440a328 | 3664 | case OPERATOR_MINUS: |
0c77715b | 3665 | if (unc->is_int() || unc->is_rune()) |
3666 | break; | |
3667 | else if (unc->is_float()) | |
3668 | { | |
3669 | mpfr_t uval; | |
3670 | unc->get_float(&uval); | |
3671 | mpfr_t val; | |
3672 | mpfr_init(val); | |
3673 | mpfr_neg(val, uval, GMP_RNDN); | |
3674 | nc->set_float(unc->type(), val); | |
3675 | mpfr_clear(uval); | |
3676 | mpfr_clear(val); | |
3677 | return true; | |
3678 | } | |
3679 | else if (unc->is_complex()) | |
3680 | { | |
3681 | mpfr_t ureal, uimag; | |
3682 | unc->get_complex(&ureal, &uimag); | |
3683 | mpfr_t real, imag; | |
3684 | mpfr_init(real); | |
3685 | mpfr_init(imag); | |
3686 | mpfr_neg(real, ureal, GMP_RNDN); | |
3687 | mpfr_neg(imag, uimag, GMP_RNDN); | |
3688 | nc->set_complex(unc->type(), real, imag); | |
3689 | mpfr_clear(ureal); | |
3690 | mpfr_clear(uimag); | |
3691 | mpfr_clear(real); | |
3692 | mpfr_clear(imag); | |
3693 | return true; | |
3694 | } | |
e440a328 | 3695 | else |
0c77715b | 3696 | go_unreachable(); |
e440a328 | 3697 | |
0c77715b | 3698 | case OPERATOR_XOR: |
3699 | break; | |
68448d53 | 3700 | |
59a401fe | 3701 | case OPERATOR_NOT: |
e440a328 | 3702 | case OPERATOR_AND: |
3703 | case OPERATOR_MULT: | |
3704 | return false; | |
0c77715b | 3705 | |
e440a328 | 3706 | default: |
c3e6f413 | 3707 | go_unreachable(); |
e440a328 | 3708 | } |
e440a328 | 3709 | |
0c77715b | 3710 | if (!unc->is_int() && !unc->is_rune()) |
3711 | return false; | |
3712 | ||
3713 | mpz_t uval; | |
8387e1df | 3714 | if (unc->is_rune()) |
3715 | unc->get_rune(&uval); | |
3716 | else | |
3717 | unc->get_int(&uval); | |
0c77715b | 3718 | mpz_t val; |
3719 | mpz_init(val); | |
e440a328 | 3720 | |
e440a328 | 3721 | switch (op) |
3722 | { | |
e440a328 | 3723 | case OPERATOR_MINUS: |
0c77715b | 3724 | mpz_neg(val, uval); |
3725 | break; | |
3726 | ||
e440a328 | 3727 | case OPERATOR_NOT: |
0c77715b | 3728 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3729 | break; | |
3730 | ||
e440a328 | 3731 | case OPERATOR_XOR: |
0c77715b | 3732 | { |
3733 | Type* utype = unc->type(); | |
3734 | if (utype->integer_type() == NULL | |
3735 | || utype->integer_type()->is_abstract()) | |
3736 | mpz_com(val, uval); | |
3737 | else | |
3738 | { | |
3739 | // The number of HOST_WIDE_INTs that it takes to represent | |
3740 | // UVAL. | |
3741 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3742 | + HOST_BITS_PER_WIDE_INT | |
3743 | - 1) | |
3744 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3745 | |
0c77715b | 3746 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3747 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3748 | ||
3749 | size_t obits = utype->integer_type()->bits(); | |
3750 | ||
3751 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3752 | { | |
3753 | mpz_t adj; | |
3754 | mpz_init_set_ui(adj, 1); | |
3755 | mpz_mul_2exp(adj, adj, obits); | |
3756 | mpz_add(uval, uval, adj); | |
3757 | mpz_clear(adj); | |
3758 | } | |
3759 | ||
3760 | size_t ecount; | |
3761 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3762 | go_assert(ecount <= count); | |
3763 | ||
3764 | // Trim down to the number of words required by the type. | |
3765 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3766 | / HOST_BITS_PER_WIDE_INT); | |
3767 | go_assert(ocount <= count); | |
3768 | ||
3769 | for (size_t i = 0; i < ocount; ++i) | |
3770 | phwi[i] = ~phwi[i]; | |
3771 | ||
3772 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3773 | if (clearbits != 0) | |
3774 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3775 | >> clearbits); | |
3776 | ||
3777 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3778 | ||
3779 | if (!utype->integer_type()->is_unsigned() | |
3780 | && mpz_tstbit(val, obits - 1)) | |
3781 | { | |
3782 | mpz_t adj; | |
3783 | mpz_init_set_ui(adj, 1); | |
3784 | mpz_mul_2exp(adj, adj, obits); | |
3785 | mpz_sub(val, val, adj); | |
3786 | mpz_clear(adj); | |
3787 | } | |
3788 | ||
3789 | delete[] phwi; | |
3790 | } | |
3791 | } | |
3792 | break; | |
e440a328 | 3793 | |
e440a328 | 3794 | default: |
c3e6f413 | 3795 | go_unreachable(); |
e440a328 | 3796 | } |
e440a328 | 3797 | |
0c77715b | 3798 | if (unc->is_rune()) |
3799 | nc->set_rune(NULL, val); | |
e440a328 | 3800 | else |
0c77715b | 3801 | nc->set_int(NULL, val); |
e440a328 | 3802 | |
0c77715b | 3803 | mpz_clear(uval); |
3804 | mpz_clear(val); | |
e440a328 | 3805 | |
0c77715b | 3806 | return nc->set_type(unc->type(), true, location); |
e440a328 | 3807 | } |
3808 | ||
0c77715b | 3809 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 3810 | |
3811 | bool | |
0c77715b | 3812 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 3813 | { |
0c77715b | 3814 | Numeric_constant unc; |
3815 | if (!this->expr_->numeric_constant_value(&unc)) | |
3816 | return false; | |
3817 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
3818 | nc); | |
e440a328 | 3819 | } |
3820 | ||
3821 | // Return the type of a unary expression. | |
3822 | ||
3823 | Type* | |
3824 | Unary_expression::do_type() | |
3825 | { | |
3826 | switch (this->op_) | |
3827 | { | |
3828 | case OPERATOR_PLUS: | |
3829 | case OPERATOR_MINUS: | |
3830 | case OPERATOR_NOT: | |
3831 | case OPERATOR_XOR: | |
3832 | return this->expr_->type(); | |
3833 | ||
3834 | case OPERATOR_AND: | |
3835 | return Type::make_pointer_type(this->expr_->type()); | |
3836 | ||
3837 | case OPERATOR_MULT: | |
3838 | { | |
3839 | Type* subtype = this->expr_->type(); | |
3840 | Type* points_to = subtype->points_to(); | |
3841 | if (points_to == NULL) | |
3842 | return Type::make_error_type(); | |
3843 | return points_to; | |
3844 | } | |
3845 | ||
3846 | default: | |
c3e6f413 | 3847 | go_unreachable(); |
e440a328 | 3848 | } |
3849 | } | |
3850 | ||
3851 | // Determine abstract types for a unary expression. | |
3852 | ||
3853 | void | |
3854 | Unary_expression::do_determine_type(const Type_context* context) | |
3855 | { | |
3856 | switch (this->op_) | |
3857 | { | |
3858 | case OPERATOR_PLUS: | |
3859 | case OPERATOR_MINUS: | |
3860 | case OPERATOR_NOT: | |
3861 | case OPERATOR_XOR: | |
3862 | this->expr_->determine_type(context); | |
3863 | break; | |
3864 | ||
3865 | case OPERATOR_AND: | |
3866 | // Taking the address of something. | |
3867 | { | |
3868 | Type* subtype = (context->type == NULL | |
3869 | ? NULL | |
3870 | : context->type->points_to()); | |
3871 | Type_context subcontext(subtype, false); | |
3872 | this->expr_->determine_type(&subcontext); | |
3873 | } | |
3874 | break; | |
3875 | ||
3876 | case OPERATOR_MULT: | |
3877 | // Indirecting through a pointer. | |
3878 | { | |
3879 | Type* subtype = (context->type == NULL | |
3880 | ? NULL | |
3881 | : Type::make_pointer_type(context->type)); | |
3882 | Type_context subcontext(subtype, false); | |
3883 | this->expr_->determine_type(&subcontext); | |
3884 | } | |
3885 | break; | |
3886 | ||
3887 | default: | |
c3e6f413 | 3888 | go_unreachable(); |
e440a328 | 3889 | } |
3890 | } | |
3891 | ||
3892 | // Check types for a unary expression. | |
3893 | ||
3894 | void | |
3895 | Unary_expression::do_check_types(Gogo*) | |
3896 | { | |
9fe897ef | 3897 | Type* type = this->expr_->type(); |
5c13bd80 | 3898 | if (type->is_error()) |
9fe897ef | 3899 | { |
3900 | this->set_is_error(); | |
3901 | return; | |
3902 | } | |
3903 | ||
e440a328 | 3904 | switch (this->op_) |
3905 | { | |
3906 | case OPERATOR_PLUS: | |
3907 | case OPERATOR_MINUS: | |
9fe897ef | 3908 | if (type->integer_type() == NULL |
3909 | && type->float_type() == NULL | |
3910 | && type->complex_type() == NULL) | |
3911 | this->report_error(_("expected numeric type")); | |
e440a328 | 3912 | break; |
3913 | ||
3914 | case OPERATOR_NOT: | |
59a401fe | 3915 | if (!type->is_boolean_type()) |
3916 | this->report_error(_("expected boolean type")); | |
3917 | break; | |
3918 | ||
e440a328 | 3919 | case OPERATOR_XOR: |
9fe897ef | 3920 | if (type->integer_type() == NULL |
3921 | && !type->is_boolean_type()) | |
3922 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 3923 | break; |
3924 | ||
3925 | case OPERATOR_AND: | |
3926 | if (!this->expr_->is_addressable()) | |
09ea332d | 3927 | { |
3928 | if (!this->create_temp_) | |
3929 | this->report_error(_("invalid operand for unary %<&%>")); | |
3930 | } | |
e440a328 | 3931 | else |
3932 | this->expr_->address_taken(this->escapes_); | |
3933 | break; | |
3934 | ||
3935 | case OPERATOR_MULT: | |
3936 | // Indirecting through a pointer. | |
9fe897ef | 3937 | if (type->points_to() == NULL) |
3938 | this->report_error(_("expected pointer")); | |
e440a328 | 3939 | break; |
3940 | ||
3941 | default: | |
c3e6f413 | 3942 | go_unreachable(); |
e440a328 | 3943 | } |
3944 | } | |
3945 | ||
3946 | // Get a tree for a unary expression. | |
3947 | ||
3948 | tree | |
3949 | Unary_expression::do_get_tree(Translate_context* context) | |
3950 | { | |
1b1f2abf | 3951 | Gogo* gogo = context->gogo(); |
e9d3367e | 3952 | Location loc = this->location(); |
3953 | ||
3954 | // Taking the address of a set-and-use-temporary expression requires | |
3955 | // setting the temporary and then taking the address. | |
3956 | if (this->op_ == OPERATOR_AND) | |
3957 | { | |
3958 | Set_and_use_temporary_expression* sut = | |
3959 | this->expr_->set_and_use_temporary_expression(); | |
3960 | if (sut != NULL) | |
3961 | { | |
3962 | Temporary_statement* temp = sut->temporary(); | |
3963 | Bvariable* bvar = temp->get_backend_variable(context); | |
3964 | tree var_tree = var_to_tree(bvar); | |
3965 | Expression* val = sut->expression(); | |
3966 | tree val_tree = val->get_tree(context); | |
3967 | if (var_tree == error_mark_node || val_tree == error_mark_node) | |
3968 | return error_mark_node; | |
3969 | tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(), | |
3970 | var_tree); | |
3971 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
3972 | TREE_TYPE(addr_tree), | |
3973 | build2_loc(sut->location().gcc_location(), | |
3974 | MODIFY_EXPR, void_type_node, | |
3975 | var_tree, val_tree), | |
3976 | addr_tree); | |
3977 | } | |
3978 | } | |
3979 | ||
e440a328 | 3980 | tree expr = this->expr_->get_tree(context); |
3981 | if (expr == error_mark_node) | |
3982 | return error_mark_node; | |
3983 | ||
e440a328 | 3984 | switch (this->op_) |
3985 | { | |
3986 | case OPERATOR_PLUS: | |
3987 | return expr; | |
3988 | ||
3989 | case OPERATOR_MINUS: | |
3990 | { | |
3991 | tree type = TREE_TYPE(expr); | |
3992 | tree compute_type = excess_precision_type(type); | |
3993 | if (compute_type != NULL_TREE) | |
3994 | expr = ::convert(compute_type, expr); | |
b13c66cd | 3995 | tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR, |
e440a328 | 3996 | (compute_type != NULL_TREE |
3997 | ? compute_type | |
3998 | : type), | |
3999 | expr); | |
4000 | if (compute_type != NULL_TREE) | |
4001 | ret = ::convert(type, ret); | |
4002 | return ret; | |
4003 | } | |
4004 | ||
4005 | case OPERATOR_NOT: | |
4006 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
b13c66cd | 4007 | return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR, |
4008 | TREE_TYPE(expr), expr); | |
e440a328 | 4009 | else |
b13c66cd | 4010 | return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node, |
4011 | expr, build_int_cst(TREE_TYPE(expr), 0)); | |
e440a328 | 4012 | |
4013 | case OPERATOR_XOR: | |
b13c66cd | 4014 | return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr), |
4015 | expr); | |
e440a328 | 4016 | |
4017 | case OPERATOR_AND: | |
09ea332d | 4018 | if (!this->create_temp_) |
4019 | { | |
4020 | // We should not see a non-constant constructor here; cases | |
4021 | // where we would see one should have been moved onto the | |
4022 | // heap at parse time. Taking the address of a nonconstant | |
4023 | // constructor will not do what the programmer expects. | |
4024 | go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
4025 | go_assert(TREE_CODE(expr) != ADDR_EXPR); | |
4026 | } | |
e440a328 | 4027 | |
4028 | // Build a decl for a constant constructor. | |
4029 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
4030 | { | |
b13c66cd | 4031 | tree decl = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 4032 | create_tmp_var_name("C"), TREE_TYPE(expr)); |
4033 | DECL_EXTERNAL(decl) = 0; | |
4034 | TREE_PUBLIC(decl) = 0; | |
4035 | TREE_READONLY(decl) = 1; | |
4036 | TREE_CONSTANT(decl) = 1; | |
4037 | TREE_STATIC(decl) = 1; | |
4038 | TREE_ADDRESSABLE(decl) = 1; | |
4039 | DECL_ARTIFICIAL(decl) = 1; | |
4040 | DECL_INITIAL(decl) = expr; | |
4041 | rest_of_decl_compilation(decl, 1, 0); | |
4042 | expr = decl; | |
4043 | } | |
4044 | ||
09ea332d | 4045 | if (this->create_temp_ |
4046 | && !TREE_ADDRESSABLE(TREE_TYPE(expr)) | |
dd28fd36 | 4047 | && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr)) |
09ea332d | 4048 | && TREE_CODE(expr) != INDIRECT_REF |
4049 | && TREE_CODE(expr) != COMPONENT_REF) | |
4050 | { | |
fc81003d | 4051 | if (current_function_decl != NULL) |
4052 | { | |
4053 | tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr)); | |
4054 | DECL_IGNORED_P(tmp) = 1; | |
4055 | DECL_INITIAL(tmp) = expr; | |
4056 | TREE_ADDRESSABLE(tmp) = 1; | |
4057 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4058 | build_pointer_type(TREE_TYPE(expr)), | |
4059 | build1_loc(loc.gcc_location(), DECL_EXPR, | |
4060 | void_type_node, tmp), | |
4061 | build_fold_addr_expr_loc(loc.gcc_location(), | |
4062 | tmp)); | |
4063 | } | |
4064 | else | |
4065 | { | |
4066 | tree tmp = build_decl(loc.gcc_location(), VAR_DECL, | |
4067 | create_tmp_var_name("A"), TREE_TYPE(expr)); | |
4068 | DECL_EXTERNAL(tmp) = 0; | |
4069 | TREE_PUBLIC(tmp) = 0; | |
4070 | TREE_STATIC(tmp) = 1; | |
4071 | DECL_ARTIFICIAL(tmp) = 1; | |
4072 | TREE_ADDRESSABLE(tmp) = 1; | |
4073 | tree make_tmp; | |
4074 | if (!TREE_CONSTANT(expr)) | |
4075 | make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR, | |
4076 | void_type_node, tmp, expr); | |
4077 | else | |
4078 | { | |
4079 | TREE_READONLY(tmp) = 1; | |
4080 | TREE_CONSTANT(tmp) = 1; | |
4081 | DECL_INITIAL(tmp) = expr; | |
4082 | make_tmp = NULL_TREE; | |
4083 | } | |
4084 | rest_of_decl_compilation(tmp, 1, 0); | |
4085 | tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp); | |
4086 | if (make_tmp == NULL_TREE) | |
4087 | return addr; | |
4088 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4089 | TREE_TYPE(addr), make_tmp, addr); | |
4090 | } | |
09ea332d | 4091 | } |
4092 | ||
b13c66cd | 4093 | return build_fold_addr_expr_loc(loc.gcc_location(), expr); |
e440a328 | 4094 | |
4095 | case OPERATOR_MULT: | |
4096 | { | |
c484d925 | 4097 | go_assert(POINTER_TYPE_P(TREE_TYPE(expr))); |
e440a328 | 4098 | |
4099 | // If we are dereferencing the pointer to a large struct, we | |
4100 | // need to check for nil. We don't bother to check for small | |
4101 | // structs because we expect the system to crash on a nil | |
4102 | // pointer dereference. | |
19b4f09b | 4103 | tree target_type_tree = TREE_TYPE(TREE_TYPE(expr)); |
4104 | if (!VOID_TYPE_P(target_type_tree)) | |
e440a328 | 4105 | { |
19b4f09b | 4106 | HOST_WIDE_INT s = int_size_in_bytes(target_type_tree); |
4107 | if (s == -1 || s >= 4096) | |
4108 | { | |
4109 | if (!DECL_P(expr)) | |
4110 | expr = save_expr(expr); | |
4111 | tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR, | |
4112 | boolean_type_node, | |
4113 | expr, | |
4114 | fold_convert(TREE_TYPE(expr), | |
4115 | null_pointer_node)); | |
1b1f2abf | 4116 | tree crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
19b4f09b | 4117 | loc); |
4118 | expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4119 | TREE_TYPE(expr), build3(COND_EXPR, | |
4120 | void_type_node, | |
4121 | compare, crash, | |
4122 | NULL_TREE), | |
4123 | expr); | |
4124 | } | |
e440a328 | 4125 | } |
4126 | ||
4127 | // If the type of EXPR is a recursive pointer type, then we | |
4128 | // need to insert a cast before indirecting. | |
19b4f09b | 4129 | if (VOID_TYPE_P(target_type_tree)) |
e440a328 | 4130 | { |
4131 | Type* pt = this->expr_->type()->points_to(); | |
1b1f2abf | 4132 | tree ind = type_to_tree(pt->get_backend(gogo)); |
b13c66cd | 4133 | expr = fold_convert_loc(loc.gcc_location(), |
4134 | build_pointer_type(ind), expr); | |
e440a328 | 4135 | } |
4136 | ||
b13c66cd | 4137 | return build_fold_indirect_ref_loc(loc.gcc_location(), expr); |
e440a328 | 4138 | } |
4139 | ||
4140 | default: | |
c3e6f413 | 4141 | go_unreachable(); |
e440a328 | 4142 | } |
4143 | } | |
4144 | ||
4145 | // Export a unary expression. | |
4146 | ||
4147 | void | |
4148 | Unary_expression::do_export(Export* exp) const | |
4149 | { | |
4150 | switch (this->op_) | |
4151 | { | |
4152 | case OPERATOR_PLUS: | |
4153 | exp->write_c_string("+ "); | |
4154 | break; | |
4155 | case OPERATOR_MINUS: | |
4156 | exp->write_c_string("- "); | |
4157 | break; | |
4158 | case OPERATOR_NOT: | |
4159 | exp->write_c_string("! "); | |
4160 | break; | |
4161 | case OPERATOR_XOR: | |
4162 | exp->write_c_string("^ "); | |
4163 | break; | |
4164 | case OPERATOR_AND: | |
4165 | case OPERATOR_MULT: | |
4166 | default: | |
c3e6f413 | 4167 | go_unreachable(); |
e440a328 | 4168 | } |
4169 | this->expr_->export_expression(exp); | |
4170 | } | |
4171 | ||
4172 | // Import a unary expression. | |
4173 | ||
4174 | Expression* | |
4175 | Unary_expression::do_import(Import* imp) | |
4176 | { | |
4177 | Operator op; | |
4178 | switch (imp->get_char()) | |
4179 | { | |
4180 | case '+': | |
4181 | op = OPERATOR_PLUS; | |
4182 | break; | |
4183 | case '-': | |
4184 | op = OPERATOR_MINUS; | |
4185 | break; | |
4186 | case '!': | |
4187 | op = OPERATOR_NOT; | |
4188 | break; | |
4189 | case '^': | |
4190 | op = OPERATOR_XOR; | |
4191 | break; | |
4192 | default: | |
c3e6f413 | 4193 | go_unreachable(); |
e440a328 | 4194 | } |
4195 | imp->require_c_string(" "); | |
4196 | Expression* expr = Expression::import_expression(imp); | |
4197 | return Expression::make_unary(op, expr, imp->location()); | |
4198 | } | |
4199 | ||
d751bb78 | 4200 | // Dump ast representation of an unary expression. |
4201 | ||
4202 | void | |
4203 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4204 | { | |
4205 | ast_dump_context->dump_operator(this->op_); | |
4206 | ast_dump_context->ostream() << "("; | |
4207 | ast_dump_context->dump_expression(this->expr_); | |
4208 | ast_dump_context->ostream() << ") "; | |
4209 | } | |
4210 | ||
e440a328 | 4211 | // Make a unary expression. |
4212 | ||
4213 | Expression* | |
b13c66cd | 4214 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4215 | { |
4216 | return new Unary_expression(op, expr, location); | |
4217 | } | |
4218 | ||
4219 | // If this is an indirection through a pointer, return the expression | |
4220 | // being pointed through. Otherwise return this. | |
4221 | ||
4222 | Expression* | |
4223 | Expression::deref() | |
4224 | { | |
4225 | if (this->classification_ == EXPRESSION_UNARY) | |
4226 | { | |
4227 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4228 | if (ue->op() == OPERATOR_MULT) | |
4229 | return ue->operand(); | |
4230 | } | |
4231 | return this; | |
4232 | } | |
4233 | ||
4234 | // Class Binary_expression. | |
4235 | ||
4236 | // Traversal. | |
4237 | ||
4238 | int | |
4239 | Binary_expression::do_traverse(Traverse* traverse) | |
4240 | { | |
4241 | int t = Expression::traverse(&this->left_, traverse); | |
4242 | if (t == TRAVERSE_EXIT) | |
4243 | return TRAVERSE_EXIT; | |
4244 | return Expression::traverse(&this->right_, traverse); | |
4245 | } | |
4246 | ||
0c77715b | 4247 | // Return the type to use for a binary operation on operands of |
4248 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4249 | // such may be NULL or abstract. | |
4250 | ||
4251 | bool | |
4252 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4253 | Type* right_type, Type** result_type) | |
4254 | { | |
4255 | if (left_type != right_type | |
4256 | && !left_type->is_abstract() | |
4257 | && !right_type->is_abstract() | |
4258 | && left_type->base() != right_type->base() | |
4259 | && op != OPERATOR_LSHIFT | |
4260 | && op != OPERATOR_RSHIFT) | |
4261 | { | |
4262 | // May be a type error--let it be diagnosed elsewhere. | |
4263 | return false; | |
4264 | } | |
4265 | ||
4266 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4267 | { | |
4268 | if (left_type->integer_type() != NULL) | |
4269 | *result_type = left_type; | |
4270 | else | |
4271 | *result_type = Type::make_abstract_integer_type(); | |
4272 | } | |
4273 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4274 | *result_type = left_type; | |
4275 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4276 | *result_type = right_type; | |
4277 | else if (!left_type->is_abstract()) | |
4278 | *result_type = left_type; | |
4279 | else if (!right_type->is_abstract()) | |
4280 | *result_type = right_type; | |
4281 | else if (left_type->complex_type() != NULL) | |
4282 | *result_type = left_type; | |
4283 | else if (right_type->complex_type() != NULL) | |
4284 | *result_type = right_type; | |
4285 | else if (left_type->float_type() != NULL) | |
4286 | *result_type = left_type; | |
4287 | else if (right_type->float_type() != NULL) | |
4288 | *result_type = right_type; | |
4289 | else if (left_type->integer_type() != NULL | |
4290 | && left_type->integer_type()->is_rune()) | |
4291 | *result_type = left_type; | |
4292 | else if (right_type->integer_type() != NULL | |
4293 | && right_type->integer_type()->is_rune()) | |
4294 | *result_type = right_type; | |
4295 | else | |
4296 | *result_type = left_type; | |
4297 | ||
4298 | return true; | |
4299 | } | |
4300 | ||
4301 | // Convert an integer comparison code and an operator to a boolean | |
4302 | // value. | |
e440a328 | 4303 | |
4304 | bool | |
0c77715b | 4305 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4306 | { |
e440a328 | 4307 | switch (op) |
4308 | { | |
4309 | case OPERATOR_EQEQ: | |
0c77715b | 4310 | return cmp == 0; |
4311 | break; | |
e440a328 | 4312 | case OPERATOR_NOTEQ: |
0c77715b | 4313 | return cmp != 0; |
4314 | break; | |
e440a328 | 4315 | case OPERATOR_LT: |
0c77715b | 4316 | return cmp < 0; |
4317 | break; | |
e440a328 | 4318 | case OPERATOR_LE: |
0c77715b | 4319 | return cmp <= 0; |
e440a328 | 4320 | case OPERATOR_GT: |
0c77715b | 4321 | return cmp > 0; |
e440a328 | 4322 | case OPERATOR_GE: |
0c77715b | 4323 | return cmp >= 0; |
e440a328 | 4324 | default: |
c3e6f413 | 4325 | go_unreachable(); |
e440a328 | 4326 | } |
4327 | } | |
4328 | ||
0c77715b | 4329 | // Compare constants according to OP. |
e440a328 | 4330 | |
4331 | bool | |
0c77715b | 4332 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4333 | Numeric_constant* right_nc, | |
4334 | Location location, bool* result) | |
e440a328 | 4335 | { |
0c77715b | 4336 | Type* left_type = left_nc->type(); |
4337 | Type* right_type = right_nc->type(); | |
4338 | ||
4339 | Type* type; | |
4340 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4341 | return false; | |
4342 | ||
4343 | // When comparing an untyped operand to a typed operand, we are | |
4344 | // effectively coercing the untyped operand to the other operand's | |
4345 | // type, so make sure that is valid. | |
4346 | if (!left_nc->set_type(type, true, location) | |
4347 | || !right_nc->set_type(type, true, location)) | |
4348 | return false; | |
4349 | ||
4350 | bool ret; | |
4351 | int cmp; | |
4352 | if (type->complex_type() != NULL) | |
4353 | { | |
4354 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4355 | return false; | |
4356 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4357 | } | |
4358 | else if (type->float_type() != NULL) | |
4359 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4360 | else |
0c77715b | 4361 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4362 | ||
4363 | if (ret) | |
4364 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4365 | ||
4366 | return ret; | |
4367 | } | |
4368 | ||
4369 | // Compare integer constants. | |
4370 | ||
4371 | bool | |
4372 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4373 | const Numeric_constant* right_nc, | |
4374 | int* cmp) | |
4375 | { | |
4376 | mpz_t left_val; | |
4377 | if (!left_nc->to_int(&left_val)) | |
4378 | return false; | |
4379 | mpz_t right_val; | |
4380 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4381 | { |
0c77715b | 4382 | mpz_clear(left_val); |
4383 | return false; | |
e440a328 | 4384 | } |
0c77715b | 4385 | |
4386 | *cmp = mpz_cmp(left_val, right_val); | |
4387 | ||
4388 | mpz_clear(left_val); | |
4389 | mpz_clear(right_val); | |
4390 | ||
4391 | return true; | |
4392 | } | |
4393 | ||
4394 | // Compare floating point constants. | |
4395 | ||
4396 | bool | |
4397 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4398 | const Numeric_constant* right_nc, | |
4399 | int* cmp) | |
4400 | { | |
4401 | mpfr_t left_val; | |
4402 | if (!left_nc->to_float(&left_val)) | |
4403 | return false; | |
4404 | mpfr_t right_val; | |
4405 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4406 | { |
0c77715b | 4407 | mpfr_clear(left_val); |
4408 | return false; | |
4409 | } | |
4410 | ||
4411 | // We already coerced both operands to the same type. If that type | |
4412 | // is not an abstract type, we need to round the values accordingly. | |
4413 | Type* type = left_nc->type(); | |
4414 | if (!type->is_abstract() && type->float_type() != NULL) | |
4415 | { | |
4416 | int bits = type->float_type()->bits(); | |
4417 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4418 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4419 | } |
0c77715b | 4420 | |
4421 | *cmp = mpfr_cmp(left_val, right_val); | |
4422 | ||
4423 | mpfr_clear(left_val); | |
4424 | mpfr_clear(right_val); | |
4425 | ||
4426 | return true; | |
e440a328 | 4427 | } |
4428 | ||
0c77715b | 4429 | // Compare complex constants. Complex numbers may only be compared |
4430 | // for equality. | |
e440a328 | 4431 | |
4432 | bool | |
0c77715b | 4433 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4434 | const Numeric_constant* right_nc, | |
4435 | int* cmp) | |
e440a328 | 4436 | { |
0c77715b | 4437 | mpfr_t left_real, left_imag; |
4438 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4439 | return false; | |
4440 | mpfr_t right_real, right_imag; | |
4441 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4442 | { |
0c77715b | 4443 | mpfr_clear(left_real); |
4444 | mpfr_clear(left_imag); | |
4445 | return false; | |
e440a328 | 4446 | } |
0c77715b | 4447 | |
4448 | // We already coerced both operands to the same type. If that type | |
4449 | // is not an abstract type, we need to round the values accordingly. | |
4450 | Type* type = left_nc->type(); | |
4451 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4452 | { |
0c77715b | 4453 | int bits = type->complex_type()->bits(); |
4454 | mpfr_prec_round(left_real, bits / 2, GMP_RNDN); | |
4455 | mpfr_prec_round(left_imag, bits / 2, GMP_RNDN); | |
4456 | mpfr_prec_round(right_real, bits / 2, GMP_RNDN); | |
4457 | mpfr_prec_round(right_imag, bits / 2, GMP_RNDN); | |
e440a328 | 4458 | } |
0c77715b | 4459 | |
4460 | *cmp = (mpfr_cmp(left_real, right_real) != 0 | |
4461 | || mpfr_cmp(left_imag, right_imag) != 0); | |
4462 | ||
4463 | mpfr_clear(left_real); | |
4464 | mpfr_clear(left_imag); | |
4465 | mpfr_clear(right_real); | |
4466 | mpfr_clear(right_imag); | |
4467 | ||
4468 | return true; | |
e440a328 | 4469 | } |
4470 | ||
0c77715b | 4471 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4472 | // true if this could be done, false if not. Issue errors at LOCATION | |
4473 | // as appropriate. | |
e440a328 | 4474 | |
4475 | bool | |
0c77715b | 4476 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4477 | Numeric_constant* right_nc, | |
4478 | Location location, Numeric_constant* nc) | |
e440a328 | 4479 | { |
e440a328 | 4480 | switch (op) |
4481 | { | |
4482 | case OPERATOR_OROR: | |
4483 | case OPERATOR_ANDAND: | |
4484 | case OPERATOR_EQEQ: | |
4485 | case OPERATOR_NOTEQ: | |
4486 | case OPERATOR_LT: | |
4487 | case OPERATOR_LE: | |
4488 | case OPERATOR_GT: | |
4489 | case OPERATOR_GE: | |
9767e2d3 | 4490 | // These return boolean values, not numeric. |
4491 | return false; | |
0c77715b | 4492 | default: |
4493 | break; | |
4494 | } | |
4495 | ||
4496 | Type* left_type = left_nc->type(); | |
4497 | Type* right_type = right_nc->type(); | |
4498 | ||
4499 | Type* type; | |
4500 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4501 | return false; | |
4502 | ||
4503 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4504 | ||
4505 | // When combining an untyped operand with a typed operand, we are | |
4506 | // effectively coercing the untyped operand to the other operand's | |
4507 | // type, so make sure that is valid. | |
4508 | if (!left_nc->set_type(type, true, location)) | |
4509 | return false; | |
4510 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4511 | return false; | |
4512 | ||
4513 | bool r; | |
4514 | if (type->complex_type() != NULL) | |
4515 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4516 | else if (type->float_type() != NULL) | |
4517 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4518 | else | |
4519 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4520 | ||
4521 | if (r) | |
4522 | r = nc->set_type(type, true, location); | |
4523 | ||
4524 | return r; | |
4525 | } | |
4526 | ||
4527 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4528 | // integer operations. Return true if this could be done, false if | |
4529 | // not. | |
4530 | ||
4531 | bool | |
4532 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4533 | const Numeric_constant* right_nc, | |
4534 | Location location, Numeric_constant* nc) | |
4535 | { | |
4536 | mpz_t left_val; | |
4537 | if (!left_nc->to_int(&left_val)) | |
4538 | return false; | |
4539 | mpz_t right_val; | |
4540 | if (!right_nc->to_int(&right_val)) | |
4541 | { | |
4542 | mpz_clear(left_val); | |
e440a328 | 4543 | return false; |
0c77715b | 4544 | } |
4545 | ||
4546 | mpz_t val; | |
4547 | mpz_init(val); | |
4548 | ||
4549 | switch (op) | |
4550 | { | |
e440a328 | 4551 | case OPERATOR_PLUS: |
4552 | mpz_add(val, left_val, right_val); | |
4553 | break; | |
4554 | case OPERATOR_MINUS: | |
4555 | mpz_sub(val, left_val, right_val); | |
4556 | break; | |
4557 | case OPERATOR_OR: | |
4558 | mpz_ior(val, left_val, right_val); | |
4559 | break; | |
4560 | case OPERATOR_XOR: | |
4561 | mpz_xor(val, left_val, right_val); | |
4562 | break; | |
4563 | case OPERATOR_MULT: | |
4564 | mpz_mul(val, left_val, right_val); | |
4565 | break; | |
4566 | case OPERATOR_DIV: | |
4567 | if (mpz_sgn(right_val) != 0) | |
4568 | mpz_tdiv_q(val, left_val, right_val); | |
4569 | else | |
4570 | { | |
4571 | error_at(location, "division by zero"); | |
4572 | mpz_set_ui(val, 0); | |
e440a328 | 4573 | } |
4574 | break; | |
4575 | case OPERATOR_MOD: | |
4576 | if (mpz_sgn(right_val) != 0) | |
4577 | mpz_tdiv_r(val, left_val, right_val); | |
4578 | else | |
4579 | { | |
4580 | error_at(location, "division by zero"); | |
4581 | mpz_set_ui(val, 0); | |
e440a328 | 4582 | } |
4583 | break; | |
4584 | case OPERATOR_LSHIFT: | |
4585 | { | |
4586 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4587 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4588 | mpz_mul_2exp(val, left_val, shift); | |
4589 | else | |
e440a328 | 4590 | { |
4591 | error_at(location, "shift count overflow"); | |
4592 | mpz_set_ui(val, 0); | |
e440a328 | 4593 | } |
e440a328 | 4594 | break; |
4595 | } | |
4596 | break; | |
4597 | case OPERATOR_RSHIFT: | |
4598 | { | |
4599 | unsigned long shift = mpz_get_ui(right_val); | |
4600 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4601 | { | |
4602 | error_at(location, "shift count overflow"); | |
4603 | mpz_set_ui(val, 0); | |
e440a328 | 4604 | } |
e440a328 | 4605 | else |
0c77715b | 4606 | { |
4607 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4608 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4609 | else | |
4610 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4611 | } | |
e440a328 | 4612 | break; |
4613 | } | |
4614 | break; | |
4615 | case OPERATOR_AND: | |
4616 | mpz_and(val, left_val, right_val); | |
4617 | break; | |
4618 | case OPERATOR_BITCLEAR: | |
4619 | { | |
4620 | mpz_t tval; | |
4621 | mpz_init(tval); | |
4622 | mpz_com(tval, right_val); | |
4623 | mpz_and(val, left_val, tval); | |
4624 | mpz_clear(tval); | |
4625 | } | |
4626 | break; | |
4627 | default: | |
c3e6f413 | 4628 | go_unreachable(); |
e440a328 | 4629 | } |
4630 | ||
0c77715b | 4631 | mpz_clear(left_val); |
4632 | mpz_clear(right_val); | |
e440a328 | 4633 | |
0c77715b | 4634 | if (left_nc->is_rune() |
4635 | || (op != OPERATOR_LSHIFT | |
4636 | && op != OPERATOR_RSHIFT | |
4637 | && right_nc->is_rune())) | |
4638 | nc->set_rune(NULL, val); | |
4639 | else | |
4640 | nc->set_int(NULL, val); | |
4641 | ||
4642 | mpz_clear(val); | |
e440a328 | 4643 | |
4644 | return true; | |
4645 | } | |
4646 | ||
0c77715b | 4647 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4648 | // floating point operations. Return true if this could be done, | |
4649 | // false if not. | |
e440a328 | 4650 | |
4651 | bool | |
0c77715b | 4652 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4653 | const Numeric_constant* right_nc, | |
4654 | Location location, Numeric_constant* nc) | |
e440a328 | 4655 | { |
0c77715b | 4656 | mpfr_t left_val; |
4657 | if (!left_nc->to_float(&left_val)) | |
4658 | return false; | |
4659 | mpfr_t right_val; | |
4660 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4661 | { |
0c77715b | 4662 | mpfr_clear(left_val); |
e440a328 | 4663 | return false; |
0c77715b | 4664 | } |
4665 | ||
4666 | mpfr_t val; | |
4667 | mpfr_init(val); | |
4668 | ||
4669 | bool ret = true; | |
4670 | switch (op) | |
4671 | { | |
e440a328 | 4672 | case OPERATOR_PLUS: |
4673 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4674 | break; | |
4675 | case OPERATOR_MINUS: | |
4676 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4677 | break; | |
4678 | case OPERATOR_OR: | |
4679 | case OPERATOR_XOR: | |
4680 | case OPERATOR_AND: | |
4681 | case OPERATOR_BITCLEAR: | |
0c77715b | 4682 | case OPERATOR_MOD: |
4683 | case OPERATOR_LSHIFT: | |
4684 | case OPERATOR_RSHIFT: | |
4685 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4686 | ret = false; | |
4687 | break; | |
e440a328 | 4688 | case OPERATOR_MULT: |
4689 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4690 | break; | |
4691 | case OPERATOR_DIV: | |
0c77715b | 4692 | if (!mpfr_zero_p(right_val)) |
4693 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4694 | else | |
4695 | { | |
4696 | error_at(location, "division by zero"); | |
4697 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4698 | } | |
e440a328 | 4699 | break; |
e440a328 | 4700 | default: |
c3e6f413 | 4701 | go_unreachable(); |
e440a328 | 4702 | } |
4703 | ||
0c77715b | 4704 | mpfr_clear(left_val); |
4705 | mpfr_clear(right_val); | |
e440a328 | 4706 | |
0c77715b | 4707 | nc->set_float(NULL, val); |
4708 | mpfr_clear(val); | |
e440a328 | 4709 | |
0c77715b | 4710 | return ret; |
e440a328 | 4711 | } |
4712 | ||
0c77715b | 4713 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4714 | // complex operations. Return true if this could be done, false if | |
4715 | // not. | |
e440a328 | 4716 | |
4717 | bool | |
0c77715b | 4718 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4719 | const Numeric_constant* right_nc, | |
4720 | Location location, Numeric_constant* nc) | |
e440a328 | 4721 | { |
0c77715b | 4722 | mpfr_t left_real, left_imag; |
4723 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4724 | return false; | |
4725 | mpfr_t right_real, right_imag; | |
4726 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4727 | { |
0c77715b | 4728 | mpfr_clear(left_real); |
4729 | mpfr_clear(left_imag); | |
e440a328 | 4730 | return false; |
0c77715b | 4731 | } |
4732 | ||
4733 | mpfr_t real, imag; | |
4734 | mpfr_init(real); | |
4735 | mpfr_init(imag); | |
4736 | ||
4737 | bool ret = true; | |
4738 | switch (op) | |
4739 | { | |
e440a328 | 4740 | case OPERATOR_PLUS: |
4741 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4742 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4743 | break; | |
4744 | case OPERATOR_MINUS: | |
4745 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4746 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4747 | break; | |
4748 | case OPERATOR_OR: | |
4749 | case OPERATOR_XOR: | |
4750 | case OPERATOR_AND: | |
4751 | case OPERATOR_BITCLEAR: | |
0c77715b | 4752 | case OPERATOR_MOD: |
4753 | case OPERATOR_LSHIFT: | |
4754 | case OPERATOR_RSHIFT: | |
4755 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4756 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4757 | ret = false; | |
4758 | break; | |
e440a328 | 4759 | case OPERATOR_MULT: |
4760 | { | |
4761 | // You might think that multiplying two complex numbers would | |
4762 | // be simple, and you would be right, until you start to think | |
4763 | // about getting the right answer for infinity. If one | |
4764 | // operand here is infinity and the other is anything other | |
4765 | // than zero or NaN, then we are going to wind up subtracting | |
4766 | // two infinity values. That will give us a NaN, but the | |
4767 | // correct answer is infinity. | |
4768 | ||
4769 | mpfr_t lrrr; | |
4770 | mpfr_init(lrrr); | |
4771 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4772 | ||
4773 | mpfr_t lrri; | |
4774 | mpfr_init(lrri); | |
4775 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4776 | ||
4777 | mpfr_t lirr; | |
4778 | mpfr_init(lirr); | |
4779 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4780 | ||
4781 | mpfr_t liri; | |
4782 | mpfr_init(liri); | |
4783 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4784 | ||
4785 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4786 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4787 | ||
4788 | // If we get NaN on both sides, check whether it should really | |
4789 | // be infinity. The rule is that if either side of the | |
4790 | // complex number is infinity, then the whole value is | |
4791 | // infinity, even if the other side is NaN. So the only case | |
4792 | // we have to fix is the one in which both sides are NaN. | |
4793 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4794 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4795 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4796 | { | |
4797 | bool is_infinity = false; | |
4798 | ||
4799 | mpfr_t lr; | |
4800 | mpfr_t li; | |
4801 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4802 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4803 | ||
4804 | mpfr_t rr; | |
4805 | mpfr_t ri; | |
4806 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4807 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4808 | ||
4809 | // If the left side is infinity, then the result is | |
4810 | // infinity. | |
4811 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4812 | { | |
4813 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4814 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4815 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4816 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4817 | if (mpfr_nan_p(rr)) | |
4818 | { | |
4819 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4820 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4821 | } | |
4822 | if (mpfr_nan_p(ri)) | |
4823 | { | |
4824 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4825 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4826 | } | |
4827 | is_infinity = true; | |
4828 | } | |
4829 | ||
4830 | // If the right side is infinity, then the result is | |
4831 | // infinity. | |
4832 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4833 | { | |
4834 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4835 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4836 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4837 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4838 | if (mpfr_nan_p(lr)) | |
4839 | { | |
4840 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4841 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4842 | } | |
4843 | if (mpfr_nan_p(li)) | |
4844 | { | |
4845 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4846 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4847 | } | |
4848 | is_infinity = true; | |
4849 | } | |
4850 | ||
4851 | // If we got an overflow in the intermediate computations, | |
4852 | // then the result is infinity. | |
4853 | if (!is_infinity | |
4854 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4855 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4856 | { | |
4857 | if (mpfr_nan_p(lr)) | |
4858 | { | |
4859 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4860 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4861 | } | |
4862 | if (mpfr_nan_p(li)) | |
4863 | { | |
4864 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4865 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4866 | } | |
4867 | if (mpfr_nan_p(rr)) | |
4868 | { | |
4869 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4870 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4871 | } | |
4872 | if (mpfr_nan_p(ri)) | |
4873 | { | |
4874 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4875 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4876 | } | |
4877 | is_infinity = true; | |
4878 | } | |
4879 | ||
4880 | if (is_infinity) | |
4881 | { | |
4882 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4883 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4884 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4885 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4886 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4887 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4888 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4889 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4890 | } | |
4891 | ||
4892 | mpfr_clear(lr); | |
4893 | mpfr_clear(li); | |
4894 | mpfr_clear(rr); | |
4895 | mpfr_clear(ri); | |
4896 | } | |
4897 | ||
4898 | mpfr_clear(lrrr); | |
4899 | mpfr_clear(lrri); | |
4900 | mpfr_clear(lirr); | |
4901 | mpfr_clear(liri); | |
4902 | } | |
4903 | break; | |
4904 | case OPERATOR_DIV: | |
4905 | { | |
4906 | // For complex division we want to avoid having an | |
4907 | // intermediate overflow turn the whole result in a NaN. We | |
4908 | // scale the values to try to avoid this. | |
4909 | ||
4910 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
0c77715b | 4911 | { |
4912 | error_at(location, "division by zero"); | |
4913 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4914 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4915 | break; | |
4916 | } | |
e440a328 | 4917 | |
4918 | mpfr_t rra; | |
4919 | mpfr_t ria; | |
4920 | mpfr_init(rra); | |
4921 | mpfr_init(ria); | |
4922 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4923 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4924 | mpfr_t t; | |
4925 | mpfr_init(t); | |
4926 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4927 | ||
4928 | mpfr_t rr; | |
4929 | mpfr_t ri; | |
4930 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4931 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4932 | long ilogbw = 0; | |
4933 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4934 | { | |
4935 | ilogbw = mpfr_get_exp(t); | |
4936 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4937 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4938 | } | |
4939 | ||
4940 | mpfr_t denom; | |
4941 | mpfr_init(denom); | |
4942 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4943 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4944 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4945 | ||
4946 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4947 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4948 | mpfr_add(real, real, t, GMP_RNDN); | |
4949 | mpfr_div(real, real, denom, GMP_RNDN); | |
4950 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4951 | ||
4952 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4953 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4954 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4955 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
4956 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
4957 | ||
4958 | // If we wind up with NaN on both sides, check whether we | |
4959 | // should really have infinity. The rule is that if either | |
4960 | // side of the complex number is infinity, then the whole | |
4961 | // value is infinity, even if the other side is NaN. So the | |
4962 | // only case we have to fix is the one in which both sides are | |
4963 | // NaN. | |
4964 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4965 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4966 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4967 | { | |
4968 | if (mpfr_zero_p(denom)) | |
4969 | { | |
4970 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
4971 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
4972 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
4973 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
4974 | } | |
4975 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
4976 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
4977 | { | |
4978 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
4979 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
4980 | ||
4981 | mpfr_t t2; | |
4982 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
4983 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
4984 | ||
4985 | mpfr_t t3; | |
4986 | mpfr_init(t3); | |
4987 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
4988 | ||
4989 | mpfr_t t4; | |
4990 | mpfr_init(t4); | |
4991 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
4992 | ||
4993 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4994 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
4995 | ||
4996 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
4997 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
4998 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4999 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
5000 | ||
5001 | mpfr_clear(t2); | |
5002 | mpfr_clear(t3); | |
5003 | mpfr_clear(t4); | |
5004 | } | |
5005 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
5006 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
5007 | { | |
5008 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
5009 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
5010 | ||
5011 | mpfr_t t2; | |
5012 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5013 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
5014 | ||
5015 | mpfr_t t3; | |
5016 | mpfr_init(t3); | |
5017 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
5018 | ||
5019 | mpfr_t t4; | |
5020 | mpfr_init(t4); | |
5021 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
5022 | ||
5023 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5024 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5025 | mpfr_mul(real, real, t3, GMP_RNDN); | |
5026 | ||
5027 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
5028 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
5029 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5030 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5031 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
5032 | ||
5033 | mpfr_clear(t2); | |
5034 | mpfr_clear(t3); | |
5035 | mpfr_clear(t4); | |
5036 | } | |
5037 | } | |
5038 | ||
5039 | mpfr_clear(denom); | |
5040 | mpfr_clear(rr); | |
5041 | mpfr_clear(ri); | |
5042 | mpfr_clear(t); | |
5043 | mpfr_clear(rra); | |
5044 | mpfr_clear(ria); | |
5045 | } | |
5046 | break; | |
e440a328 | 5047 | default: |
c3e6f413 | 5048 | go_unreachable(); |
e440a328 | 5049 | } |
5050 | ||
0c77715b | 5051 | mpfr_clear(left_real); |
5052 | mpfr_clear(left_imag); | |
5053 | mpfr_clear(right_real); | |
5054 | mpfr_clear(right_imag); | |
e440a328 | 5055 | |
0c77715b | 5056 | nc->set_complex(NULL, real, imag); |
5057 | mpfr_clear(real); | |
5058 | mpfr_clear(imag); | |
e440a328 | 5059 | |
0c77715b | 5060 | return ret; |
e440a328 | 5061 | } |
5062 | ||
5063 | // Lower a binary expression. We have to evaluate constant | |
5064 | // expressions now, in order to implement Go's unlimited precision | |
5065 | // constants. | |
5066 | ||
5067 | Expression* | |
e9d3367e | 5068 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5069 | Statement_inserter* inserter, int) | |
e440a328 | 5070 | { |
b13c66cd | 5071 | Location location = this->location(); |
e440a328 | 5072 | Operator op = this->op_; |
5073 | Expression* left = this->left_; | |
5074 | Expression* right = this->right_; | |
5075 | ||
5076 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5077 | || op == OPERATOR_NOTEQ | |
5078 | || op == OPERATOR_LT | |
5079 | || op == OPERATOR_LE | |
5080 | || op == OPERATOR_GT | |
5081 | || op == OPERATOR_GE); | |
5082 | ||
0c77715b | 5083 | // Numeric constant expressions. |
e440a328 | 5084 | { |
0c77715b | 5085 | Numeric_constant left_nc; |
5086 | Numeric_constant right_nc; | |
5087 | if (left->numeric_constant_value(&left_nc) | |
5088 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5089 | { |
0c77715b | 5090 | if (is_comparison) |
e440a328 | 5091 | { |
0c77715b | 5092 | bool result; |
5093 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5094 | &right_nc, location, | |
5095 | &result)) | |
5096 | return this; | |
e90c9dfc | 5097 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5098 | Expression::make_boolean(result, |
5099 | location), | |
5100 | location); | |
e440a328 | 5101 | } |
5102 | else | |
5103 | { | |
0c77715b | 5104 | Numeric_constant nc; |
5105 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5106 | location, &nc)) | |
5107 | return this; | |
5108 | return nc.expression(location); | |
e440a328 | 5109 | } |
5110 | } | |
e440a328 | 5111 | } |
5112 | ||
5113 | // String constant expressions. | |
315fa98d | 5114 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5115 | { |
5116 | std::string left_string; | |
5117 | std::string right_string; | |
5118 | if (left->string_constant_value(&left_string) | |
5119 | && right->string_constant_value(&right_string)) | |
315fa98d | 5120 | { |
5121 | if (op == OPERATOR_PLUS) | |
5122 | return Expression::make_string(left_string + right_string, | |
5123 | location); | |
5124 | else if (is_comparison) | |
5125 | { | |
5126 | int cmp = left_string.compare(right_string); | |
0c77715b | 5127 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5128 | return Expression::make_boolean(r, location); |
b40dc774 | 5129 | } |
5130 | } | |
b40dc774 | 5131 | } |
5132 | ||
e9d3367e | 5133 | // Lower struct and array comparisons. |
5134 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) | |
5135 | { | |
5136 | if (left->type()->struct_type() != NULL) | |
5137 | return this->lower_struct_comparison(gogo, inserter); | |
5138 | else if (left->type()->array_type() != NULL | |
5139 | && !left->type()->is_slice_type()) | |
5140 | return this->lower_array_comparison(gogo, inserter); | |
5141 | } | |
5142 | ||
e440a328 | 5143 | return this; |
5144 | } | |
5145 | ||
e9d3367e | 5146 | // Lower a struct comparison. |
5147 | ||
5148 | Expression* | |
5149 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5150 | Statement_inserter* inserter) | |
5151 | { | |
5152 | Struct_type* st = this->left_->type()->struct_type(); | |
5153 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5154 | if (st2 == NULL) | |
5155 | return this; | |
5156 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5157 | return this; | |
5158 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5159 | this->right_->type(), NULL)) | |
5160 | return this; | |
5161 | ||
5162 | // See if we can compare using memcmp. As a heuristic, we use | |
5163 | // memcmp rather than field references and comparisons if there are | |
5164 | // more than two fields. | |
113ef6a5 | 5165 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5166 | return this->lower_compare_to_memcmp(gogo, inserter); |
5167 | ||
5168 | Location loc = this->location(); | |
5169 | ||
5170 | Expression* left = this->left_; | |
5171 | Temporary_statement* left_temp = NULL; | |
5172 | if (left->var_expression() == NULL | |
5173 | && left->temporary_reference_expression() == NULL) | |
5174 | { | |
5175 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5176 | inserter->insert(left_temp); | |
5177 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5178 | } | |
5179 | ||
5180 | Expression* right = this->right_; | |
5181 | Temporary_statement* right_temp = NULL; | |
5182 | if (right->var_expression() == NULL | |
5183 | && right->temporary_reference_expression() == NULL) | |
5184 | { | |
5185 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5186 | inserter->insert(right_temp); | |
5187 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5188 | } | |
5189 | ||
5190 | Expression* ret = Expression::make_boolean(true, loc); | |
5191 | const Struct_field_list* fields = st->fields(); | |
5192 | unsigned int field_index = 0; | |
5193 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5194 | pf != fields->end(); | |
5195 | ++pf, ++field_index) | |
5196 | { | |
f5165c05 | 5197 | if (Gogo::is_sink_name(pf->field_name())) |
5198 | continue; | |
5199 | ||
e9d3367e | 5200 | if (field_index > 0) |
5201 | { | |
5202 | if (left_temp == NULL) | |
5203 | left = left->copy(); | |
5204 | else | |
5205 | left = Expression::make_temporary_reference(left_temp, loc); | |
5206 | if (right_temp == NULL) | |
5207 | right = right->copy(); | |
5208 | else | |
5209 | right = Expression::make_temporary_reference(right_temp, loc); | |
5210 | } | |
5211 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5212 | loc); | |
5213 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5214 | loc); | |
5215 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5216 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5217 | } | |
5218 | ||
5219 | if (this->op_ == OPERATOR_NOTEQ) | |
5220 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5221 | ||
5222 | return ret; | |
5223 | } | |
5224 | ||
5225 | // Lower an array comparison. | |
5226 | ||
5227 | Expression* | |
5228 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5229 | Statement_inserter* inserter) | |
5230 | { | |
5231 | Array_type* at = this->left_->type()->array_type(); | |
5232 | Array_type* at2 = this->right_->type()->array_type(); | |
5233 | if (at2 == NULL) | |
5234 | return this; | |
5235 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5236 | return this; | |
5237 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5238 | this->right_->type(), NULL)) | |
5239 | return this; | |
5240 | ||
5241 | // Call memcmp directly if possible. This may let the middle-end | |
5242 | // optimize the call. | |
113ef6a5 | 5243 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5244 | return this->lower_compare_to_memcmp(gogo, inserter); |
5245 | ||
5246 | // Call the array comparison function. | |
5247 | Named_object* hash_fn; | |
5248 | Named_object* equal_fn; | |
5249 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5250 | &hash_fn, &equal_fn); | |
5251 | ||
5252 | Location loc = this->location(); | |
5253 | ||
5254 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5255 | ||
5256 | Expression_list* args = new Expression_list(); | |
5257 | args->push_back(this->operand_address(inserter, this->left_)); | |
5258 | args->push_back(this->operand_address(inserter, this->right_)); | |
5259 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5260 | ||
5261 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5262 | ||
5263 | if (this->op_ == OPERATOR_NOTEQ) | |
5264 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5265 | ||
5266 | return ret; | |
5267 | } | |
5268 | ||
5269 | // Lower a struct or array comparison to a call to memcmp. | |
5270 | ||
5271 | Expression* | |
5272 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5273 | { | |
5274 | Location loc = this->location(); | |
5275 | ||
5276 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5277 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5278 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5279 | TYPE_INFO_SIZE); | |
5280 | ||
5281 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
5282 | ||
5283 | mpz_t zval; | |
5284 | mpz_init_set_ui(zval, 0); | |
5285 | Expression* zero = Expression::make_integer(&zval, NULL, loc); | |
5286 | mpz_clear(zval); | |
5287 | ||
5288 | return Expression::make_binary(this->op_, call, zero, loc); | |
5289 | } | |
5290 | ||
5291 | // Return the address of EXPR, cast to unsafe.Pointer. | |
5292 | ||
5293 | Expression* | |
5294 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5295 | Expression* expr) | |
5296 | { | |
5297 | Location loc = this->location(); | |
5298 | ||
5299 | if (!expr->is_addressable()) | |
5300 | { | |
5301 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5302 | loc); | |
5303 | inserter->insert(temp); | |
5304 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5305 | } | |
5306 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5307 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5308 | Type* void_type = Type::make_void_type(); | |
5309 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5310 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5311 | } | |
5312 | ||
0c77715b | 5313 | // Return the numeric constant value, if it has one. |
e440a328 | 5314 | |
5315 | bool | |
0c77715b | 5316 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5317 | { |
0c77715b | 5318 | Numeric_constant left_nc; |
5319 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5320 | return false; | |
5321 | Numeric_constant right_nc; | |
5322 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5323 | return false; | |
9767e2d3 | 5324 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5325 | this->location(), nc); |
e440a328 | 5326 | } |
5327 | ||
5328 | // Note that the value is being discarded. | |
5329 | ||
4f2138d7 | 5330 | bool |
e440a328 | 5331 | Binary_expression::do_discarding_value() |
5332 | { | |
5333 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5334 | return this->right_->discarding_value(); |
e440a328 | 5335 | else |
4f2138d7 | 5336 | { |
5337 | this->unused_value_error(); | |
5338 | return false; | |
5339 | } | |
e440a328 | 5340 | } |
5341 | ||
5342 | // Get type. | |
5343 | ||
5344 | Type* | |
5345 | Binary_expression::do_type() | |
5346 | { | |
5f5fea79 | 5347 | if (this->classification() == EXPRESSION_ERROR) |
5348 | return Type::make_error_type(); | |
5349 | ||
e440a328 | 5350 | switch (this->op_) |
5351 | { | |
e440a328 | 5352 | case OPERATOR_EQEQ: |
5353 | case OPERATOR_NOTEQ: | |
5354 | case OPERATOR_LT: | |
5355 | case OPERATOR_LE: | |
5356 | case OPERATOR_GT: | |
5357 | case OPERATOR_GE: | |
e90c9dfc | 5358 | if (this->type_ == NULL) |
5359 | this->type_ = Type::make_boolean_type(); | |
5360 | return this->type_; | |
e440a328 | 5361 | |
5362 | case OPERATOR_PLUS: | |
5363 | case OPERATOR_MINUS: | |
5364 | case OPERATOR_OR: | |
5365 | case OPERATOR_XOR: | |
5366 | case OPERATOR_MULT: | |
5367 | case OPERATOR_DIV: | |
5368 | case OPERATOR_MOD: | |
5369 | case OPERATOR_AND: | |
5370 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5371 | case OPERATOR_OROR: |
5372 | case OPERATOR_ANDAND: | |
e440a328 | 5373 | { |
0c77715b | 5374 | Type* type; |
5375 | if (!Binary_expression::operation_type(this->op_, | |
5376 | this->left_->type(), | |
5377 | this->right_->type(), | |
5378 | &type)) | |
5379 | return Type::make_error_type(); | |
5380 | return type; | |
e440a328 | 5381 | } |
5382 | ||
5383 | case OPERATOR_LSHIFT: | |
5384 | case OPERATOR_RSHIFT: | |
5385 | return this->left_->type(); | |
5386 | ||
5387 | default: | |
c3e6f413 | 5388 | go_unreachable(); |
e440a328 | 5389 | } |
5390 | } | |
5391 | ||
5392 | // Set type for a binary expression. | |
5393 | ||
5394 | void | |
5395 | Binary_expression::do_determine_type(const Type_context* context) | |
5396 | { | |
5397 | Type* tleft = this->left_->type(); | |
5398 | Type* tright = this->right_->type(); | |
5399 | ||
5400 | // Both sides should have the same type, except for the shift | |
5401 | // operations. For a comparison, we should ignore the incoming | |
5402 | // type. | |
5403 | ||
5404 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5405 | || this->op_ == OPERATOR_RSHIFT); | |
5406 | ||
5407 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5408 | || this->op_ == OPERATOR_NOTEQ | |
5409 | || this->op_ == OPERATOR_LT | |
5410 | || this->op_ == OPERATOR_LE | |
5411 | || this->op_ == OPERATOR_GT | |
5412 | || this->op_ == OPERATOR_GE); | |
5413 | ||
5414 | Type_context subcontext(*context); | |
5415 | ||
5416 | if (is_comparison) | |
5417 | { | |
5418 | // In a comparison, the context does not determine the types of | |
5419 | // the operands. | |
5420 | subcontext.type = NULL; | |
5421 | } | |
5422 | ||
5423 | // Set the context for the left hand operand. | |
5424 | if (is_shift_op) | |
5425 | { | |
b40dc774 | 5426 | // The right hand operand of a shift plays no role in |
5427 | // determining the type of the left hand operand. | |
e440a328 | 5428 | } |
5429 | else if (!tleft->is_abstract()) | |
5430 | subcontext.type = tleft; | |
5431 | else if (!tright->is_abstract()) | |
5432 | subcontext.type = tright; | |
5433 | else if (subcontext.type == NULL) | |
5434 | { | |
5435 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5436 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5437 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5438 | { | |
5439 | // Both sides have an abstract integer, abstract float, or | |
5440 | // abstract complex type. Just let CONTEXT determine | |
5441 | // whether they may remain abstract or not. | |
5442 | } | |
5443 | else if (tleft->complex_type() != NULL) | |
5444 | subcontext.type = tleft; | |
5445 | else if (tright->complex_type() != NULL) | |
5446 | subcontext.type = tright; | |
5447 | else if (tleft->float_type() != NULL) | |
5448 | subcontext.type = tleft; | |
5449 | else if (tright->float_type() != NULL) | |
5450 | subcontext.type = tright; | |
5451 | else | |
5452 | subcontext.type = tleft; | |
f58a23ae | 5453 | |
5454 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5455 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5456 | } |
5457 | ||
5458 | this->left_->determine_type(&subcontext); | |
5459 | ||
e440a328 | 5460 | if (is_shift_op) |
5461 | { | |
b40dc774 | 5462 | // We may have inherited an unusable type for the shift operand. |
5463 | // Give a useful error if that happened. | |
5464 | if (tleft->is_abstract() | |
5465 | && subcontext.type != NULL | |
5466 | && (this->left_->type()->integer_type() == NULL | |
5467 | || (subcontext.type->integer_type() == NULL | |
5468 | && subcontext.type->float_type() == NULL | |
a7bb3691 | 5469 | && subcontext.type->complex_type() == NULL |
5470 | && subcontext.type->interface_type() == NULL))) | |
b40dc774 | 5471 | this->report_error(("invalid context-determined non-integer type " |
5472 | "for shift operand")); | |
5473 | ||
5474 | // The context for the right hand operand is the same as for the | |
5475 | // left hand operand, except for a shift operator. | |
e440a328 | 5476 | subcontext.type = Type::lookup_integer_type("uint"); |
5477 | subcontext.may_be_abstract = false; | |
5478 | } | |
5479 | ||
5480 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5481 | |
5482 | if (is_comparison) | |
5483 | { | |
5484 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5485 | ; | |
5486 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5487 | this->type_ = context->type; | |
5488 | else if (!context->may_be_abstract) | |
5489 | this->type_ = Type::lookup_bool_type(); | |
5490 | } | |
e440a328 | 5491 | } |
5492 | ||
5493 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5494 | // OTYPE is the type of the other operand. Return whether the |
5495 | // operation is OK. This should not be used for shift. | |
e440a328 | 5496 | |
5497 | bool | |
be8b5eee | 5498 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5499 | Location location) |
e440a328 | 5500 | { |
5501 | switch (op) | |
5502 | { | |
5503 | case OPERATOR_OROR: | |
5504 | case OPERATOR_ANDAND: | |
5505 | if (!type->is_boolean_type()) | |
5506 | { | |
5507 | error_at(location, "expected boolean type"); | |
5508 | return false; | |
5509 | } | |
5510 | break; | |
5511 | ||
5512 | case OPERATOR_EQEQ: | |
5513 | case OPERATOR_NOTEQ: | |
e9d3367e | 5514 | { |
5515 | std::string reason; | |
5516 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5517 | { | |
5518 | error_at(location, "%s", reason.c_str()); | |
5519 | return false; | |
5520 | } | |
5521 | } | |
e440a328 | 5522 | break; |
5523 | ||
5524 | case OPERATOR_LT: | |
5525 | case OPERATOR_LE: | |
5526 | case OPERATOR_GT: | |
5527 | case OPERATOR_GE: | |
e9d3367e | 5528 | { |
5529 | std::string reason; | |
5530 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5531 | { | |
5532 | error_at(location, "%s", reason.c_str()); | |
5533 | return false; | |
5534 | } | |
5535 | } | |
e440a328 | 5536 | break; |
5537 | ||
5538 | case OPERATOR_PLUS: | |
5539 | case OPERATOR_PLUSEQ: | |
5540 | if (type->integer_type() == NULL | |
5541 | && type->float_type() == NULL | |
5542 | && type->complex_type() == NULL | |
5543 | && !type->is_string_type()) | |
5544 | { | |
5545 | error_at(location, | |
5546 | "expected integer, floating, complex, or string type"); | |
5547 | return false; | |
5548 | } | |
5549 | break; | |
5550 | ||
5551 | case OPERATOR_MINUS: | |
5552 | case OPERATOR_MINUSEQ: | |
5553 | case OPERATOR_MULT: | |
5554 | case OPERATOR_MULTEQ: | |
5555 | case OPERATOR_DIV: | |
5556 | case OPERATOR_DIVEQ: | |
5557 | if (type->integer_type() == NULL | |
5558 | && type->float_type() == NULL | |
5559 | && type->complex_type() == NULL) | |
5560 | { | |
5561 | error_at(location, "expected integer, floating, or complex type"); | |
5562 | return false; | |
5563 | } | |
5564 | break; | |
5565 | ||
5566 | case OPERATOR_MOD: | |
5567 | case OPERATOR_MODEQ: | |
5568 | case OPERATOR_OR: | |
5569 | case OPERATOR_OREQ: | |
5570 | case OPERATOR_AND: | |
5571 | case OPERATOR_ANDEQ: | |
5572 | case OPERATOR_XOR: | |
5573 | case OPERATOR_XOREQ: | |
5574 | case OPERATOR_BITCLEAR: | |
5575 | case OPERATOR_BITCLEAREQ: | |
5576 | if (type->integer_type() == NULL) | |
5577 | { | |
5578 | error_at(location, "expected integer type"); | |
5579 | return false; | |
5580 | } | |
5581 | break; | |
5582 | ||
5583 | default: | |
c3e6f413 | 5584 | go_unreachable(); |
e440a328 | 5585 | } |
5586 | ||
5587 | return true; | |
5588 | } | |
5589 | ||
5590 | // Check types. | |
5591 | ||
5592 | void | |
5593 | Binary_expression::do_check_types(Gogo*) | |
5594 | { | |
5f5fea79 | 5595 | if (this->classification() == EXPRESSION_ERROR) |
5596 | return; | |
5597 | ||
e440a328 | 5598 | Type* left_type = this->left_->type(); |
5599 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5600 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5601 | { |
5602 | this->set_is_error(); | |
5603 | return; | |
5604 | } | |
e440a328 | 5605 | |
5606 | if (this->op_ == OPERATOR_EQEQ | |
5607 | || this->op_ == OPERATOR_NOTEQ | |
5608 | || this->op_ == OPERATOR_LT | |
5609 | || this->op_ == OPERATOR_LE | |
5610 | || this->op_ == OPERATOR_GT | |
5611 | || this->op_ == OPERATOR_GE) | |
5612 | { | |
907c5ecd | 5613 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5614 | { | |
5615 | this->report_error(_("invalid comparison of nil with nil")); | |
5616 | return; | |
5617 | } | |
e440a328 | 5618 | if (!Type::are_assignable(left_type, right_type, NULL) |
5619 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5620 | { | |
5621 | this->report_error(_("incompatible types in binary expression")); | |
5622 | return; | |
5623 | } | |
5624 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5625 | right_type, |
e440a328 | 5626 | this->location()) |
5627 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5628 | left_type, |
e440a328 | 5629 | this->location())) |
5630 | { | |
5631 | this->set_is_error(); | |
5632 | return; | |
5633 | } | |
5634 | } | |
5635 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5636 | { | |
5637 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5638 | { | |
5639 | this->report_error(_("incompatible types in binary expression")); | |
5640 | return; | |
5641 | } | |
5642 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5643 | right_type, |
e440a328 | 5644 | this->location())) |
5645 | { | |
5646 | this->set_is_error(); | |
5647 | return; | |
5648 | } | |
5649 | } | |
5650 | else | |
5651 | { | |
5652 | if (left_type->integer_type() == NULL) | |
5653 | this->report_error(_("shift of non-integer operand")); | |
5654 | ||
5655 | if (!right_type->is_abstract() | |
5656 | && (right_type->integer_type() == NULL | |
5657 | || !right_type->integer_type()->is_unsigned())) | |
5658 | this->report_error(_("shift count not unsigned integer")); | |
5659 | else | |
5660 | { | |
0c77715b | 5661 | Numeric_constant nc; |
5662 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5663 | { |
0c77715b | 5664 | mpz_t val; |
5665 | if (!nc.to_int(&val)) | |
5666 | this->report_error(_("shift count not unsigned integer")); | |
5667 | else | |
a4eba91b | 5668 | { |
0c77715b | 5669 | if (mpz_sgn(val) < 0) |
5670 | { | |
5671 | this->report_error(_("negative shift count")); | |
5672 | mpz_set_ui(val, 0); | |
5673 | Location rloc = this->right_->location(); | |
5674 | this->right_ = Expression::make_integer(&val, right_type, | |
5675 | rloc); | |
5676 | } | |
5677 | mpz_clear(val); | |
a4eba91b | 5678 | } |
e440a328 | 5679 | } |
e440a328 | 5680 | } |
5681 | } | |
5682 | } | |
5683 | ||
5684 | // Get a tree for a binary expression. | |
5685 | ||
5686 | tree | |
5687 | Binary_expression::do_get_tree(Translate_context* context) | |
5688 | { | |
1b1f2abf | 5689 | Gogo* gogo = context->gogo(); |
5690 | ||
e440a328 | 5691 | tree left = this->left_->get_tree(context); |
5692 | tree right = this->right_->get_tree(context); | |
5693 | ||
5694 | if (left == error_mark_node || right == error_mark_node) | |
5695 | return error_mark_node; | |
5696 | ||
5697 | enum tree_code code; | |
5698 | bool use_left_type = true; | |
5699 | bool is_shift_op = false; | |
29a2d1d8 | 5700 | bool is_idiv_op = false; |
e440a328 | 5701 | switch (this->op_) |
5702 | { | |
5703 | case OPERATOR_EQEQ: | |
5704 | case OPERATOR_NOTEQ: | |
5705 | case OPERATOR_LT: | |
5706 | case OPERATOR_LE: | |
5707 | case OPERATOR_GT: | |
5708 | case OPERATOR_GE: | |
e90c9dfc | 5709 | return Expression::comparison_tree(context, this->type_, this->op_, |
e440a328 | 5710 | this->left_->type(), left, |
5711 | this->right_->type(), right, | |
5712 | this->location()); | |
5713 | ||
5714 | case OPERATOR_OROR: | |
5715 | code = TRUTH_ORIF_EXPR; | |
5716 | use_left_type = false; | |
5717 | break; | |
5718 | case OPERATOR_ANDAND: | |
5719 | code = TRUTH_ANDIF_EXPR; | |
5720 | use_left_type = false; | |
5721 | break; | |
5722 | case OPERATOR_PLUS: | |
5723 | code = PLUS_EXPR; | |
5724 | break; | |
5725 | case OPERATOR_MINUS: | |
5726 | code = MINUS_EXPR; | |
5727 | break; | |
5728 | case OPERATOR_OR: | |
5729 | code = BIT_IOR_EXPR; | |
5730 | break; | |
5731 | case OPERATOR_XOR: | |
5732 | code = BIT_XOR_EXPR; | |
5733 | break; | |
5734 | case OPERATOR_MULT: | |
5735 | code = MULT_EXPR; | |
5736 | break; | |
5737 | case OPERATOR_DIV: | |
5738 | { | |
5739 | Type *t = this->left_->type(); | |
5740 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5741 | code = RDIV_EXPR; | |
5742 | else | |
29a2d1d8 | 5743 | { |
5744 | code = TRUNC_DIV_EXPR; | |
5745 | is_idiv_op = true; | |
5746 | } | |
e440a328 | 5747 | } |
5748 | break; | |
5749 | case OPERATOR_MOD: | |
5750 | code = TRUNC_MOD_EXPR; | |
29a2d1d8 | 5751 | is_idiv_op = true; |
e440a328 | 5752 | break; |
5753 | case OPERATOR_LSHIFT: | |
5754 | code = LSHIFT_EXPR; | |
5755 | is_shift_op = true; | |
5756 | break; | |
5757 | case OPERATOR_RSHIFT: | |
5758 | code = RSHIFT_EXPR; | |
5759 | is_shift_op = true; | |
5760 | break; | |
5761 | case OPERATOR_AND: | |
5762 | code = BIT_AND_EXPR; | |
5763 | break; | |
5764 | case OPERATOR_BITCLEAR: | |
5765 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5766 | code = BIT_AND_EXPR; | |
5767 | break; | |
5768 | default: | |
c3e6f413 | 5769 | go_unreachable(); |
e440a328 | 5770 | } |
5771 | ||
29a2d1d8 | 5772 | location_t gccloc = this->location().gcc_location(); |
e440a328 | 5773 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); |
5774 | ||
5775 | if (this->left_->type()->is_string_type()) | |
5776 | { | |
c484d925 | 5777 | go_assert(this->op_ == OPERATOR_PLUS); |
9f0e0513 | 5778 | Type* st = Type::make_string_type(); |
1b1f2abf | 5779 | tree string_type = type_to_tree(st->get_backend(gogo)); |
e440a328 | 5780 | static tree string_plus_decl; |
5781 | return Gogo::call_builtin(&string_plus_decl, | |
5782 | this->location(), | |
5783 | "__go_string_plus", | |
5784 | 2, | |
5785 | string_type, | |
5786 | string_type, | |
5787 | left, | |
5788 | string_type, | |
5789 | right); | |
5790 | } | |
5791 | ||
5792 | tree compute_type = excess_precision_type(type); | |
5793 | if (compute_type != NULL_TREE) | |
5794 | { | |
5795 | left = ::convert(compute_type, left); | |
5796 | right = ::convert(compute_type, right); | |
5797 | } | |
5798 | ||
5799 | tree eval_saved = NULL_TREE; | |
29a2d1d8 | 5800 | if (is_shift_op |
5801 | || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow))) | |
e440a328 | 5802 | { |
e440a328 | 5803 | // Make sure the values are evaluated. |
29a2d1d8 | 5804 | if (!DECL_P(left)) |
a7a70f31 | 5805 | { |
5806 | left = save_expr(left); | |
5807 | eval_saved = left; | |
5808 | } | |
29a2d1d8 | 5809 | if (!DECL_P(right)) |
a7a70f31 | 5810 | { |
5811 | right = save_expr(right); | |
5812 | if (eval_saved == NULL_TREE) | |
5813 | eval_saved = right; | |
5814 | else | |
29a2d1d8 | 5815 | eval_saved = fold_build2_loc(gccloc, COMPOUND_EXPR, |
a7a70f31 | 5816 | void_type_node, eval_saved, right); |
5817 | } | |
e440a328 | 5818 | } |
5819 | ||
29a2d1d8 | 5820 | tree ret = fold_build2_loc(gccloc, code, |
e440a328 | 5821 | compute_type != NULL_TREE ? compute_type : type, |
5822 | left, right); | |
5823 | ||
5824 | if (compute_type != NULL_TREE) | |
5825 | ret = ::convert(type, ret); | |
5826 | ||
5827 | // In Go, a shift larger than the size of the type is well-defined. | |
5828 | // This is not true in GENERIC, so we need to insert a conditional. | |
5829 | if (is_shift_op) | |
5830 | { | |
c484d925 | 5831 | go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); |
5832 | go_assert(this->left_->type()->integer_type() != NULL); | |
e440a328 | 5833 | int bits = TYPE_PRECISION(TREE_TYPE(left)); |
5834 | ||
5835 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
5836 | build_int_cst_type(TREE_TYPE(right), bits)); | |
5837 | ||
29a2d1d8 | 5838 | tree overflow_result = fold_convert_loc(gccloc, TREE_TYPE(left), |
e440a328 | 5839 | integer_zero_node); |
5840 | if (this->op_ == OPERATOR_RSHIFT | |
5841 | && !this->left_->type()->integer_type()->is_unsigned()) | |
5842 | { | |
b13c66cd | 5843 | tree neg = |
29a2d1d8 | 5844 | fold_build2_loc(gccloc, LT_EXPR, boolean_type_node, |
5845 | left, | |
5846 | fold_convert_loc(gccloc, TREE_TYPE(left), | |
b13c66cd | 5847 | integer_zero_node)); |
5848 | tree neg_one = | |
29a2d1d8 | 5849 | fold_build2_loc(gccloc, MINUS_EXPR, TREE_TYPE(left), |
5850 | fold_convert_loc(gccloc, TREE_TYPE(left), | |
b13c66cd | 5851 | integer_zero_node), |
29a2d1d8 | 5852 | fold_convert_loc(gccloc, TREE_TYPE(left), |
b13c66cd | 5853 | integer_one_node)); |
5854 | overflow_result = | |
29a2d1d8 | 5855 | fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left), |
5856 | neg, neg_one, overflow_result); | |
5857 | } | |
5858 | ||
5859 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left), | |
5860 | compare, ret, overflow_result); | |
5861 | ||
5862 | if (eval_saved != NULL_TREE) | |
5863 | ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), | |
5864 | eval_saved, ret); | |
5865 | } | |
5866 | ||
5867 | // Add checks for division by zero and division overflow as needed. | |
5868 | if (is_idiv_op) | |
5869 | { | |
5870 | if (go_check_divide_zero) | |
5871 | { | |
5872 | // right == 0 | |
5873 | tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
5874 | right, | |
5875 | fold_convert_loc(gccloc, | |
5876 | TREE_TYPE(right), | |
5877 | integer_zero_node)); | |
5878 | ||
5879 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0 | |
5880 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; | |
5881 | tree panic = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), | |
1b1f2abf | 5882 | gogo->runtime_error(errcode, |
29a2d1d8 | 5883 | this->location()), |
5884 | fold_convert_loc(gccloc, TREE_TYPE(ret), | |
5885 | integer_zero_node)); | |
5886 | ||
5887 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
5888 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5889 | check, panic, ret); | |
b13c66cd | 5890 | } |
5891 | ||
29a2d1d8 | 5892 | if (go_check_divide_overflow) |
5893 | { | |
5894 | // right == -1 | |
5895 | // FIXME: It would be nice to say that this test is expected | |
5896 | // to return false. | |
5897 | tree m1 = integer_minus_one_node; | |
5898 | tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
5899 | right, | |
5900 | fold_convert_loc(gccloc, | |
5901 | TREE_TYPE(right), | |
5902 | m1)); | |
5903 | ||
5904 | tree overflow; | |
5905 | if (TYPE_UNSIGNED(TREE_TYPE(ret))) | |
5906 | { | |
5907 | // An unsigned -1 is the largest possible number, so | |
5908 | // dividing is always 1 or 0. | |
5909 | tree cmp = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
5910 | left, right); | |
5911 | if (this->op_ == OPERATOR_DIV) | |
5912 | overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5913 | cmp, | |
5914 | fold_convert_loc(gccloc, | |
5915 | TREE_TYPE(ret), | |
5916 | integer_one_node), | |
5917 | fold_convert_loc(gccloc, | |
5918 | TREE_TYPE(ret), | |
5919 | integer_zero_node)); | |
5920 | else | |
5921 | overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5922 | cmp, | |
5923 | fold_convert_loc(gccloc, | |
5924 | TREE_TYPE(ret), | |
5925 | integer_zero_node), | |
5926 | left); | |
5927 | } | |
5928 | else | |
5929 | { | |
5930 | // Computing left / -1 is the same as computing - left, | |
5931 | // which does not overflow since Go sets -fwrapv. | |
5932 | if (this->op_ == OPERATOR_DIV) | |
5933 | overflow = fold_build1_loc(gccloc, NEGATE_EXPR, TREE_TYPE(left), | |
5934 | left); | |
5935 | else | |
5936 | overflow = integer_zero_node; | |
5937 | } | |
5938 | overflow = fold_convert_loc(gccloc, TREE_TYPE(ret), overflow); | |
5939 | ||
5940 | // right == -1 ? - left : ret | |
5941 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5942 | check, overflow, ret); | |
5943 | } | |
e440a328 | 5944 | |
a7a70f31 | 5945 | if (eval_saved != NULL_TREE) |
29a2d1d8 | 5946 | ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), |
5947 | eval_saved, ret); | |
e440a328 | 5948 | } |
5949 | ||
5950 | return ret; | |
5951 | } | |
5952 | ||
5953 | // Export a binary expression. | |
5954 | ||
5955 | void | |
5956 | Binary_expression::do_export(Export* exp) const | |
5957 | { | |
5958 | exp->write_c_string("("); | |
5959 | this->left_->export_expression(exp); | |
5960 | switch (this->op_) | |
5961 | { | |
5962 | case OPERATOR_OROR: | |
5963 | exp->write_c_string(" || "); | |
5964 | break; | |
5965 | case OPERATOR_ANDAND: | |
5966 | exp->write_c_string(" && "); | |
5967 | break; | |
5968 | case OPERATOR_EQEQ: | |
5969 | exp->write_c_string(" == "); | |
5970 | break; | |
5971 | case OPERATOR_NOTEQ: | |
5972 | exp->write_c_string(" != "); | |
5973 | break; | |
5974 | case OPERATOR_LT: | |
5975 | exp->write_c_string(" < "); | |
5976 | break; | |
5977 | case OPERATOR_LE: | |
5978 | exp->write_c_string(" <= "); | |
5979 | break; | |
5980 | case OPERATOR_GT: | |
5981 | exp->write_c_string(" > "); | |
5982 | break; | |
5983 | case OPERATOR_GE: | |
5984 | exp->write_c_string(" >= "); | |
5985 | break; | |
5986 | case OPERATOR_PLUS: | |
5987 | exp->write_c_string(" + "); | |
5988 | break; | |
5989 | case OPERATOR_MINUS: | |
5990 | exp->write_c_string(" - "); | |
5991 | break; | |
5992 | case OPERATOR_OR: | |
5993 | exp->write_c_string(" | "); | |
5994 | break; | |
5995 | case OPERATOR_XOR: | |
5996 | exp->write_c_string(" ^ "); | |
5997 | break; | |
5998 | case OPERATOR_MULT: | |
5999 | exp->write_c_string(" * "); | |
6000 | break; | |
6001 | case OPERATOR_DIV: | |
6002 | exp->write_c_string(" / "); | |
6003 | break; | |
6004 | case OPERATOR_MOD: | |
6005 | exp->write_c_string(" % "); | |
6006 | break; | |
6007 | case OPERATOR_LSHIFT: | |
6008 | exp->write_c_string(" << "); | |
6009 | break; | |
6010 | case OPERATOR_RSHIFT: | |
6011 | exp->write_c_string(" >> "); | |
6012 | break; | |
6013 | case OPERATOR_AND: | |
6014 | exp->write_c_string(" & "); | |
6015 | break; | |
6016 | case OPERATOR_BITCLEAR: | |
6017 | exp->write_c_string(" &^ "); | |
6018 | break; | |
6019 | default: | |
c3e6f413 | 6020 | go_unreachable(); |
e440a328 | 6021 | } |
6022 | this->right_->export_expression(exp); | |
6023 | exp->write_c_string(")"); | |
6024 | } | |
6025 | ||
6026 | // Import a binary expression. | |
6027 | ||
6028 | Expression* | |
6029 | Binary_expression::do_import(Import* imp) | |
6030 | { | |
6031 | imp->require_c_string("("); | |
6032 | ||
6033 | Expression* left = Expression::import_expression(imp); | |
6034 | ||
6035 | Operator op; | |
6036 | if (imp->match_c_string(" || ")) | |
6037 | { | |
6038 | op = OPERATOR_OROR; | |
6039 | imp->advance(4); | |
6040 | } | |
6041 | else if (imp->match_c_string(" && ")) | |
6042 | { | |
6043 | op = OPERATOR_ANDAND; | |
6044 | imp->advance(4); | |
6045 | } | |
6046 | else if (imp->match_c_string(" == ")) | |
6047 | { | |
6048 | op = OPERATOR_EQEQ; | |
6049 | imp->advance(4); | |
6050 | } | |
6051 | else if (imp->match_c_string(" != ")) | |
6052 | { | |
6053 | op = OPERATOR_NOTEQ; | |
6054 | imp->advance(4); | |
6055 | } | |
6056 | else if (imp->match_c_string(" < ")) | |
6057 | { | |
6058 | op = OPERATOR_LT; | |
6059 | imp->advance(3); | |
6060 | } | |
6061 | else if (imp->match_c_string(" <= ")) | |
6062 | { | |
6063 | op = OPERATOR_LE; | |
6064 | imp->advance(4); | |
6065 | } | |
6066 | else if (imp->match_c_string(" > ")) | |
6067 | { | |
6068 | op = OPERATOR_GT; | |
6069 | imp->advance(3); | |
6070 | } | |
6071 | else if (imp->match_c_string(" >= ")) | |
6072 | { | |
6073 | op = OPERATOR_GE; | |
6074 | imp->advance(4); | |
6075 | } | |
6076 | else if (imp->match_c_string(" + ")) | |
6077 | { | |
6078 | op = OPERATOR_PLUS; | |
6079 | imp->advance(3); | |
6080 | } | |
6081 | else if (imp->match_c_string(" - ")) | |
6082 | { | |
6083 | op = OPERATOR_MINUS; | |
6084 | imp->advance(3); | |
6085 | } | |
6086 | else if (imp->match_c_string(" | ")) | |
6087 | { | |
6088 | op = OPERATOR_OR; | |
6089 | imp->advance(3); | |
6090 | } | |
6091 | else if (imp->match_c_string(" ^ ")) | |
6092 | { | |
6093 | op = OPERATOR_XOR; | |
6094 | imp->advance(3); | |
6095 | } | |
6096 | else if (imp->match_c_string(" * ")) | |
6097 | { | |
6098 | op = OPERATOR_MULT; | |
6099 | imp->advance(3); | |
6100 | } | |
6101 | else if (imp->match_c_string(" / ")) | |
6102 | { | |
6103 | op = OPERATOR_DIV; | |
6104 | imp->advance(3); | |
6105 | } | |
6106 | else if (imp->match_c_string(" % ")) | |
6107 | { | |
6108 | op = OPERATOR_MOD; | |
6109 | imp->advance(3); | |
6110 | } | |
6111 | else if (imp->match_c_string(" << ")) | |
6112 | { | |
6113 | op = OPERATOR_LSHIFT; | |
6114 | imp->advance(4); | |
6115 | } | |
6116 | else if (imp->match_c_string(" >> ")) | |
6117 | { | |
6118 | op = OPERATOR_RSHIFT; | |
6119 | imp->advance(4); | |
6120 | } | |
6121 | else if (imp->match_c_string(" & ")) | |
6122 | { | |
6123 | op = OPERATOR_AND; | |
6124 | imp->advance(3); | |
6125 | } | |
6126 | else if (imp->match_c_string(" &^ ")) | |
6127 | { | |
6128 | op = OPERATOR_BITCLEAR; | |
6129 | imp->advance(4); | |
6130 | } | |
6131 | else | |
6132 | { | |
6133 | error_at(imp->location(), "unrecognized binary operator"); | |
6134 | return Expression::make_error(imp->location()); | |
6135 | } | |
6136 | ||
6137 | Expression* right = Expression::import_expression(imp); | |
6138 | ||
6139 | imp->require_c_string(")"); | |
6140 | ||
6141 | return Expression::make_binary(op, left, right, imp->location()); | |
6142 | } | |
6143 | ||
d751bb78 | 6144 | // Dump ast representation of a binary expression. |
6145 | ||
6146 | void | |
6147 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6148 | { | |
6149 | ast_dump_context->ostream() << "("; | |
6150 | ast_dump_context->dump_expression(this->left_); | |
6151 | ast_dump_context->ostream() << " "; | |
6152 | ast_dump_context->dump_operator(this->op_); | |
6153 | ast_dump_context->ostream() << " "; | |
6154 | ast_dump_context->dump_expression(this->right_); | |
6155 | ast_dump_context->ostream() << ") "; | |
6156 | } | |
6157 | ||
e440a328 | 6158 | // Make a binary expression. |
6159 | ||
6160 | Expression* | |
6161 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6162 | Location location) |
e440a328 | 6163 | { |
6164 | return new Binary_expression(op, left, right, location); | |
6165 | } | |
6166 | ||
6167 | // Implement a comparison. | |
6168 | ||
6169 | tree | |
e90c9dfc | 6170 | Expression::comparison_tree(Translate_context* context, Type* result_type, |
6171 | Operator op, Type* left_type, tree left_tree, | |
e440a328 | 6172 | Type* right_type, tree right_tree, |
b13c66cd | 6173 | Location location) |
e440a328 | 6174 | { |
1b1f2abf | 6175 | Type* int_type = Type::lookup_integer_type("int"); |
6176 | tree int_type_tree = type_to_tree(int_type->get_backend(context->gogo())); | |
6177 | ||
e440a328 | 6178 | enum tree_code code; |
6179 | switch (op) | |
6180 | { | |
6181 | case OPERATOR_EQEQ: | |
6182 | code = EQ_EXPR; | |
6183 | break; | |
6184 | case OPERATOR_NOTEQ: | |
6185 | code = NE_EXPR; | |
6186 | break; | |
6187 | case OPERATOR_LT: | |
6188 | code = LT_EXPR; | |
6189 | break; | |
6190 | case OPERATOR_LE: | |
6191 | code = LE_EXPR; | |
6192 | break; | |
6193 | case OPERATOR_GT: | |
6194 | code = GT_EXPR; | |
6195 | break; | |
6196 | case OPERATOR_GE: | |
6197 | code = GE_EXPR; | |
6198 | break; | |
6199 | default: | |
c3e6f413 | 6200 | go_unreachable(); |
e440a328 | 6201 | } |
6202 | ||
15c67ee2 | 6203 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6204 | { |
9f0e0513 | 6205 | Type* st = Type::make_string_type(); |
6206 | tree string_type = type_to_tree(st->get_backend(context->gogo())); | |
e440a328 | 6207 | static tree string_compare_decl; |
6208 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
6209 | location, | |
6210 | "__go_strcmp", | |
6211 | 2, | |
1b1f2abf | 6212 | int_type_tree, |
e440a328 | 6213 | string_type, |
6214 | left_tree, | |
6215 | string_type, | |
6216 | right_tree); | |
1b1f2abf | 6217 | right_tree = build_int_cst_type(int_type_tree, 0); |
e440a328 | 6218 | } |
15c67ee2 | 6219 | else if ((left_type->interface_type() != NULL |
6220 | && right_type->interface_type() == NULL | |
6221 | && !right_type->is_nil_type()) | |
6222 | || (left_type->interface_type() == NULL | |
6223 | && !left_type->is_nil_type() | |
6224 | && right_type->interface_type() != NULL)) | |
e440a328 | 6225 | { |
6226 | // Comparing an interface value to a non-interface value. | |
6227 | if (left_type->interface_type() == NULL) | |
6228 | { | |
6229 | std::swap(left_type, right_type); | |
6230 | std::swap(left_tree, right_tree); | |
6231 | } | |
6232 | ||
6233 | // The right operand is not an interface. We need to take its | |
6234 | // address if it is not a pointer. | |
6235 | tree make_tmp; | |
6236 | tree arg; | |
6237 | if (right_type->points_to() != NULL) | |
6238 | { | |
6239 | make_tmp = NULL_TREE; | |
6240 | arg = right_tree; | |
6241 | } | |
dd28fd36 | 6242 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) |
6243 | || (TREE_CODE(right_tree) != CONST_DECL | |
6244 | && DECL_P(right_tree))) | |
e440a328 | 6245 | { |
6246 | make_tmp = NULL_TREE; | |
b13c66cd | 6247 | arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree); |
e440a328 | 6248 | if (DECL_P(right_tree)) |
6249 | TREE_ADDRESSABLE(right_tree) = 1; | |
6250 | } | |
6251 | else | |
6252 | { | |
6253 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
6254 | get_name(right_tree)); | |
6255 | DECL_IGNORED_P(tmp) = 0; | |
6256 | DECL_INITIAL(tmp) = right_tree; | |
6257 | TREE_ADDRESSABLE(tmp) = 1; | |
6258 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
b13c66cd | 6259 | SET_EXPR_LOCATION(make_tmp, location.gcc_location()); |
6260 | arg = build_fold_addr_expr_loc(location.gcc_location(), tmp); | |
e440a328 | 6261 | } |
b13c66cd | 6262 | arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg); |
e440a328 | 6263 | |
a1d23b41 | 6264 | tree descriptor = right_type->type_descriptor_pointer(context->gogo(), |
6265 | location); | |
e440a328 | 6266 | |
6267 | if (left_type->interface_type()->is_empty()) | |
6268 | { | |
6269 | static tree empty_interface_value_compare_decl; | |
6270 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6271 | location, | |
6272 | "__go_empty_interface_value_compare", | |
6273 | 3, | |
1b1f2abf | 6274 | int_type_tree, |
e440a328 | 6275 | TREE_TYPE(left_tree), |
6276 | left_tree, | |
6277 | TREE_TYPE(descriptor), | |
6278 | descriptor, | |
6279 | ptr_type_node, | |
6280 | arg); | |
5fb82b5e | 6281 | if (left_tree == error_mark_node) |
6282 | return error_mark_node; | |
e440a328 | 6283 | // This can panic if the type is not comparable. |
6284 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6285 | } | |
6286 | else | |
6287 | { | |
6288 | static tree interface_value_compare_decl; | |
6289 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6290 | location, | |
6291 | "__go_interface_value_compare", | |
6292 | 3, | |
1b1f2abf | 6293 | int_type_tree, |
e440a328 | 6294 | TREE_TYPE(left_tree), |
6295 | left_tree, | |
6296 | TREE_TYPE(descriptor), | |
6297 | descriptor, | |
6298 | ptr_type_node, | |
6299 | arg); | |
5fb82b5e | 6300 | if (left_tree == error_mark_node) |
6301 | return error_mark_node; | |
e440a328 | 6302 | // This can panic if the type is not comparable. |
6303 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6304 | } | |
1b1f2abf | 6305 | right_tree = build_int_cst_type(int_type_tree, 0); |
e440a328 | 6306 | |
6307 | if (make_tmp != NULL_TREE) | |
6308 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6309 | left_tree); | |
6310 | } | |
6311 | else if (left_type->interface_type() != NULL | |
6312 | && right_type->interface_type() != NULL) | |
6313 | { | |
739bad04 | 6314 | if (left_type->interface_type()->is_empty() |
6315 | && right_type->interface_type()->is_empty()) | |
e440a328 | 6316 | { |
e440a328 | 6317 | static tree empty_interface_compare_decl; |
6318 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6319 | location, | |
6320 | "__go_empty_interface_compare", | |
6321 | 2, | |
1b1f2abf | 6322 | int_type_tree, |
e440a328 | 6323 | TREE_TYPE(left_tree), |
6324 | left_tree, | |
6325 | TREE_TYPE(right_tree), | |
6326 | right_tree); | |
5fb82b5e | 6327 | if (left_tree == error_mark_node) |
6328 | return error_mark_node; | |
e440a328 | 6329 | // This can panic if the type is uncomparable. |
6330 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6331 | } | |
739bad04 | 6332 | else if (!left_type->interface_type()->is_empty() |
6333 | && !right_type->interface_type()->is_empty()) | |
e440a328 | 6334 | { |
e440a328 | 6335 | static tree interface_compare_decl; |
6336 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6337 | location, | |
6338 | "__go_interface_compare", | |
6339 | 2, | |
1b1f2abf | 6340 | int_type_tree, |
e440a328 | 6341 | TREE_TYPE(left_tree), |
6342 | left_tree, | |
6343 | TREE_TYPE(right_tree), | |
6344 | right_tree); | |
5fb82b5e | 6345 | if (left_tree == error_mark_node) |
6346 | return error_mark_node; | |
e440a328 | 6347 | // This can panic if the type is uncomparable. |
6348 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6349 | } | |
739bad04 | 6350 | else |
6351 | { | |
6352 | if (left_type->interface_type()->is_empty()) | |
6353 | { | |
c484d925 | 6354 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
739bad04 | 6355 | std::swap(left_type, right_type); |
6356 | std::swap(left_tree, right_tree); | |
6357 | } | |
c484d925 | 6358 | go_assert(!left_type->interface_type()->is_empty()); |
6359 | go_assert(right_type->interface_type()->is_empty()); | |
739bad04 | 6360 | static tree interface_empty_compare_decl; |
6361 | left_tree = Gogo::call_builtin(&interface_empty_compare_decl, | |
6362 | location, | |
6363 | "__go_interface_empty_compare", | |
6364 | 2, | |
1b1f2abf | 6365 | int_type_tree, |
739bad04 | 6366 | TREE_TYPE(left_tree), |
6367 | left_tree, | |
6368 | TREE_TYPE(right_tree), | |
6369 | right_tree); | |
6370 | if (left_tree == error_mark_node) | |
6371 | return error_mark_node; | |
6372 | // This can panic if the type is uncomparable. | |
6373 | TREE_NOTHROW(interface_empty_compare_decl) = 0; | |
6374 | } | |
6375 | ||
1b1f2abf | 6376 | right_tree = build_int_cst_type(int_type_tree, 0); |
e440a328 | 6377 | } |
6378 | ||
6379 | if (left_type->is_nil_type() | |
6380 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6381 | { | |
6382 | std::swap(left_type, right_type); | |
6383 | std::swap(left_tree, right_tree); | |
6384 | } | |
6385 | ||
6386 | if (right_type->is_nil_type()) | |
6387 | { | |
6388 | if (left_type->array_type() != NULL | |
6389 | && left_type->array_type()->length() == NULL) | |
6390 | { | |
6391 | Array_type* at = left_type->array_type(); | |
6392 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6393 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6394 | } | |
6395 | else if (left_type->interface_type() != NULL) | |
6396 | { | |
6397 | // An interface is nil if the first field is nil. | |
6398 | tree left_type_tree = TREE_TYPE(left_tree); | |
c484d925 | 6399 | go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); |
e440a328 | 6400 | tree field = TYPE_FIELDS(left_type_tree); |
6401 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6402 | field, NULL_TREE); | |
6403 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6404 | } | |
6405 | else | |
6406 | { | |
c484d925 | 6407 | go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); |
e440a328 | 6408 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); |
6409 | } | |
6410 | } | |
6411 | ||
d8ccb1e3 | 6412 | if (left_tree == error_mark_node || right_tree == error_mark_node) |
6413 | return error_mark_node; | |
6414 | ||
e90c9dfc | 6415 | tree result_type_tree; |
6416 | if (result_type == NULL) | |
6417 | result_type_tree = boolean_type_node; | |
6418 | else | |
6419 | result_type_tree = type_to_tree(result_type->get_backend(context->gogo())); | |
6420 | ||
6421 | tree ret = fold_build2(code, result_type_tree, left_tree, right_tree); | |
e440a328 | 6422 | if (CAN_HAVE_LOCATION_P(ret)) |
b13c66cd | 6423 | SET_EXPR_LOCATION(ret, location.gcc_location()); |
e440a328 | 6424 | return ret; |
6425 | } | |
6426 | ||
6427 | // Class Bound_method_expression. | |
6428 | ||
6429 | // Traversal. | |
6430 | ||
6431 | int | |
6432 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6433 | { | |
e0659c9e | 6434 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6435 | } |
6436 | ||
6437 | // Return the type of a bound method expression. The type of this | |
6438 | // object is really the type of the method with no receiver. We | |
6439 | // should be able to get away with just returning the type of the | |
6440 | // method. | |
6441 | ||
6442 | Type* | |
6443 | Bound_method_expression::do_type() | |
6444 | { | |
e0659c9e | 6445 | if (this->method_->is_function()) |
6446 | return this->method_->func_value()->type(); | |
6447 | else if (this->method_->is_function_declaration()) | |
6448 | return this->method_->func_declaration_value()->type(); | |
6449 | else | |
6450 | return Type::make_error_type(); | |
e440a328 | 6451 | } |
6452 | ||
6453 | // Determine the types of a method expression. | |
6454 | ||
6455 | void | |
6456 | Bound_method_expression::do_determine_type(const Type_context*) | |
6457 | { | |
e0659c9e | 6458 | Function_type* fntype = this->type()->function_type(); |
e440a328 | 6459 | if (fntype == NULL || !fntype->is_method()) |
6460 | this->expr_->determine_type_no_context(); | |
6461 | else | |
6462 | { | |
6463 | Type_context subcontext(fntype->receiver()->type(), false); | |
6464 | this->expr_->determine_type(&subcontext); | |
6465 | } | |
6466 | } | |
6467 | ||
6468 | // Check the types of a method expression. | |
6469 | ||
6470 | void | |
6471 | Bound_method_expression::do_check_types(Gogo*) | |
6472 | { | |
e0659c9e | 6473 | if (!this->method_->is_function() |
6474 | && !this->method_->is_function_declaration()) | |
e440a328 | 6475 | this->report_error(_("object is not a method")); |
6476 | else | |
6477 | { | |
e0659c9e | 6478 | Type* rtype = this->type()->function_type()->receiver()->type()->deref(); |
e440a328 | 6479 | Type* etype = (this->expr_type_ != NULL |
6480 | ? this->expr_type_ | |
6481 | : this->expr_->type()); | |
6482 | etype = etype->deref(); | |
07ba8be5 | 6483 | if (!Type::are_identical(rtype, etype, true, NULL)) |
e440a328 | 6484 | this->report_error(_("method type does not match object type")); |
6485 | } | |
6486 | } | |
6487 | ||
6488 | // Get the tree for a method expression. There is no standard tree | |
6489 | // representation for this. The only places it may currently be used | |
6490 | // are in a Call_expression or a Go_statement, which will take it | |
6491 | // apart directly. So this has nothing to do at present. | |
6492 | ||
6493 | tree | |
6494 | Bound_method_expression::do_get_tree(Translate_context*) | |
6495 | { | |
d40405e2 | 6496 | error_at(this->location(), "reference to method other than calling it"); |
6497 | return error_mark_node; | |
e440a328 | 6498 | } |
6499 | ||
d751bb78 | 6500 | // Dump ast representation of a bound method expression. |
6501 | ||
6502 | void | |
6503 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6504 | const | |
6505 | { | |
6506 | if (this->expr_type_ != NULL) | |
6507 | ast_dump_context->ostream() << "("; | |
6508 | ast_dump_context->dump_expression(this->expr_); | |
6509 | if (this->expr_type_ != NULL) | |
6510 | { | |
6511 | ast_dump_context->ostream() << ":"; | |
6512 | ast_dump_context->dump_type(this->expr_type_); | |
6513 | ast_dump_context->ostream() << ")"; | |
6514 | } | |
6515 | ||
e0659c9e | 6516 | ast_dump_context->ostream() << "." << this->method_->name(); |
d751bb78 | 6517 | } |
6518 | ||
e440a328 | 6519 | // Make a method expression. |
6520 | ||
6521 | Bound_method_expression* | |
e0659c9e | 6522 | Expression::make_bound_method(Expression* expr, Named_object* method, |
b13c66cd | 6523 | Location location) |
e440a328 | 6524 | { |
6525 | return new Bound_method_expression(expr, method, location); | |
6526 | } | |
6527 | ||
6528 | // Class Builtin_call_expression. This is used for a call to a | |
6529 | // builtin function. | |
6530 | ||
6531 | class Builtin_call_expression : public Call_expression | |
6532 | { | |
6533 | public: | |
6534 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6535 | bool is_varargs, Location location); |
e440a328 | 6536 | |
6537 | protected: | |
6538 | // This overrides Call_expression::do_lower. | |
6539 | Expression* | |
ceeb4318 | 6540 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6541 | |
6542 | bool | |
6543 | do_is_constant() const; | |
6544 | ||
6545 | bool | |
0c77715b | 6546 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6547 | |
4f2138d7 | 6548 | bool |
a7549a6a | 6549 | do_discarding_value(); |
6550 | ||
e440a328 | 6551 | Type* |
6552 | do_type(); | |
6553 | ||
6554 | void | |
6555 | do_determine_type(const Type_context*); | |
6556 | ||
6557 | void | |
6558 | do_check_types(Gogo*); | |
6559 | ||
6560 | Expression* | |
6561 | do_copy() | |
6562 | { | |
6563 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6564 | this->args()->copy(), | |
6565 | this->is_varargs(), | |
6566 | this->location()); | |
6567 | } | |
6568 | ||
6569 | tree | |
6570 | do_get_tree(Translate_context*); | |
6571 | ||
6572 | void | |
6573 | do_export(Export*) const; | |
6574 | ||
6575 | virtual bool | |
6576 | do_is_recover_call() const; | |
6577 | ||
6578 | virtual void | |
6579 | do_set_recover_arg(Expression*); | |
6580 | ||
6581 | private: | |
6582 | // The builtin functions. | |
6583 | enum Builtin_function_code | |
6584 | { | |
6585 | BUILTIN_INVALID, | |
6586 | ||
6587 | // Predeclared builtin functions. | |
6588 | BUILTIN_APPEND, | |
6589 | BUILTIN_CAP, | |
6590 | BUILTIN_CLOSE, | |
48080209 | 6591 | BUILTIN_COMPLEX, |
e440a328 | 6592 | BUILTIN_COPY, |
1cce762f | 6593 | BUILTIN_DELETE, |
e440a328 | 6594 | BUILTIN_IMAG, |
6595 | BUILTIN_LEN, | |
6596 | BUILTIN_MAKE, | |
6597 | BUILTIN_NEW, | |
6598 | BUILTIN_PANIC, | |
6599 | BUILTIN_PRINT, | |
6600 | BUILTIN_PRINTLN, | |
6601 | BUILTIN_REAL, | |
6602 | BUILTIN_RECOVER, | |
6603 | ||
6604 | // Builtin functions from the unsafe package. | |
6605 | BUILTIN_ALIGNOF, | |
6606 | BUILTIN_OFFSETOF, | |
6607 | BUILTIN_SIZEOF | |
6608 | }; | |
6609 | ||
6610 | Expression* | |
6611 | one_arg() const; | |
6612 | ||
6613 | bool | |
6614 | check_one_arg(); | |
6615 | ||
6616 | static Type* | |
6617 | real_imag_type(Type*); | |
6618 | ||
6619 | static Type* | |
48080209 | 6620 | complex_type(Type*); |
e440a328 | 6621 | |
a9182619 | 6622 | Expression* |
6623 | lower_make(); | |
6624 | ||
6625 | bool | |
6626 | check_int_value(Expression*); | |
6627 | ||
e440a328 | 6628 | // A pointer back to the general IR structure. This avoids a global |
6629 | // variable, or passing it around everywhere. | |
6630 | Gogo* gogo_; | |
6631 | // The builtin function being called. | |
6632 | Builtin_function_code code_; | |
0f914071 | 6633 | // Used to stop endless loops when the length of an array uses len |
6634 | // or cap of the array itself. | |
6635 | mutable bool seen_; | |
e440a328 | 6636 | }; |
6637 | ||
6638 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6639 | Expression* fn, | |
6640 | Expression_list* args, | |
6641 | bool is_varargs, | |
b13c66cd | 6642 | Location location) |
e440a328 | 6643 | : Call_expression(fn, args, is_varargs, location), |
0f914071 | 6644 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 6645 | { |
6646 | Func_expression* fnexp = this->fn()->func_expression(); | |
c484d925 | 6647 | go_assert(fnexp != NULL); |
e440a328 | 6648 | const std::string& name(fnexp->named_object()->name()); |
6649 | if (name == "append") | |
6650 | this->code_ = BUILTIN_APPEND; | |
6651 | else if (name == "cap") | |
6652 | this->code_ = BUILTIN_CAP; | |
6653 | else if (name == "close") | |
6654 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6655 | else if (name == "complex") |
6656 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6657 | else if (name == "copy") |
6658 | this->code_ = BUILTIN_COPY; | |
1cce762f | 6659 | else if (name == "delete") |
6660 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 6661 | else if (name == "imag") |
6662 | this->code_ = BUILTIN_IMAG; | |
6663 | else if (name == "len") | |
6664 | this->code_ = BUILTIN_LEN; | |
6665 | else if (name == "make") | |
6666 | this->code_ = BUILTIN_MAKE; | |
6667 | else if (name == "new") | |
6668 | this->code_ = BUILTIN_NEW; | |
6669 | else if (name == "panic") | |
6670 | this->code_ = BUILTIN_PANIC; | |
6671 | else if (name == "print") | |
6672 | this->code_ = BUILTIN_PRINT; | |
6673 | else if (name == "println") | |
6674 | this->code_ = BUILTIN_PRINTLN; | |
6675 | else if (name == "real") | |
6676 | this->code_ = BUILTIN_REAL; | |
6677 | else if (name == "recover") | |
6678 | this->code_ = BUILTIN_RECOVER; | |
6679 | else if (name == "Alignof") | |
6680 | this->code_ = BUILTIN_ALIGNOF; | |
6681 | else if (name == "Offsetof") | |
6682 | this->code_ = BUILTIN_OFFSETOF; | |
6683 | else if (name == "Sizeof") | |
6684 | this->code_ = BUILTIN_SIZEOF; | |
6685 | else | |
c3e6f413 | 6686 | go_unreachable(); |
e440a328 | 6687 | } |
6688 | ||
6689 | // Return whether this is a call to recover. This is a virtual | |
6690 | // function called from the parent class. | |
6691 | ||
6692 | bool | |
6693 | Builtin_call_expression::do_is_recover_call() const | |
6694 | { | |
6695 | if (this->classification() == EXPRESSION_ERROR) | |
6696 | return false; | |
6697 | return this->code_ == BUILTIN_RECOVER; | |
6698 | } | |
6699 | ||
6700 | // Set the argument for a call to recover. | |
6701 | ||
6702 | void | |
6703 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6704 | { | |
6705 | const Expression_list* args = this->args(); | |
c484d925 | 6706 | go_assert(args == NULL || args->empty()); |
e440a328 | 6707 | Expression_list* new_args = new Expression_list(); |
6708 | new_args->push_back(arg); | |
6709 | this->set_args(new_args); | |
6710 | } | |
6711 | ||
e440a328 | 6712 | // Lower a builtin call expression. This turns new and make into |
6713 | // specific expressions. We also convert to a constant if we can. | |
6714 | ||
6715 | Expression* | |
ceeb4318 | 6716 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
6717 | Statement_inserter* inserter, int) | |
e440a328 | 6718 | { |
a9182619 | 6719 | if (this->classification() == EXPRESSION_ERROR) |
6720 | return this; | |
6721 | ||
b13c66cd | 6722 | Location loc = this->location(); |
1cce762f | 6723 | |
a8725655 | 6724 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
6725 | { | |
6726 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 6727 | return Expression::make_error(loc); |
a8725655 | 6728 | } |
6729 | ||
1cce762f | 6730 | if (this->is_constant()) |
e440a328 | 6731 | { |
0c77715b | 6732 | Numeric_constant nc; |
6733 | if (this->numeric_constant_value(&nc)) | |
6734 | return nc.expression(loc); | |
e440a328 | 6735 | } |
1cce762f | 6736 | |
6737 | switch (this->code_) | |
e440a328 | 6738 | { |
1cce762f | 6739 | default: |
6740 | break; | |
6741 | ||
6742 | case BUILTIN_NEW: | |
6743 | { | |
6744 | const Expression_list* args = this->args(); | |
6745 | if (args == NULL || args->size() < 1) | |
6746 | this->report_error(_("not enough arguments")); | |
6747 | else if (args->size() > 1) | |
6748 | this->report_error(_("too many arguments")); | |
6749 | else | |
6750 | { | |
6751 | Expression* arg = args->front(); | |
6752 | if (!arg->is_type_expression()) | |
6753 | { | |
6754 | error_at(arg->location(), "expected type"); | |
6755 | this->set_is_error(); | |
6756 | } | |
6757 | else | |
6758 | return Expression::make_allocation(arg->type(), loc); | |
6759 | } | |
6760 | } | |
6761 | break; | |
6762 | ||
6763 | case BUILTIN_MAKE: | |
6764 | return this->lower_make(); | |
6765 | ||
6766 | case BUILTIN_RECOVER: | |
e440a328 | 6767 | if (function != NULL) |
6768 | function->func_value()->set_calls_recover(); | |
6769 | else | |
6770 | { | |
6771 | // Calling recover outside of a function always returns the | |
6772 | // nil empty interface. | |
823c7e3d | 6773 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 6774 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 6775 | } |
1cce762f | 6776 | break; |
6777 | ||
6778 | case BUILTIN_APPEND: | |
6779 | { | |
6780 | // Lower the varargs. | |
6781 | const Expression_list* args = this->args(); | |
6782 | if (args == NULL || args->empty()) | |
e440a328 | 6783 | return this; |
1cce762f | 6784 | Type* slice_type = args->front()->type(); |
6785 | if (!slice_type->is_slice_type()) | |
6786 | { | |
6787 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
6788 | this->set_is_error(); | |
6789 | return this; | |
6790 | } | |
19fd40c3 | 6791 | Type* element_type = slice_type->array_type()->element_type(); |
6792 | this->lower_varargs(gogo, function, inserter, | |
6793 | Type::make_array_type(element_type, NULL), | |
6794 | 2); | |
1cce762f | 6795 | } |
6796 | break; | |
6797 | ||
6798 | case BUILTIN_DELETE: | |
6799 | { | |
6800 | // Lower to a runtime function call. | |
6801 | const Expression_list* args = this->args(); | |
6802 | if (args == NULL || args->size() < 2) | |
6803 | this->report_error(_("not enough arguments")); | |
6804 | else if (args->size() > 2) | |
6805 | this->report_error(_("too many arguments")); | |
6806 | else if (args->front()->type()->map_type() == NULL) | |
6807 | this->report_error(_("argument 1 must be a map")); | |
6808 | else | |
6809 | { | |
6810 | // Since this function returns no value it must appear in | |
6811 | // a statement by itself, so we don't have to worry about | |
6812 | // order of evaluation of values around it. Evaluate the | |
6813 | // map first to get order of evaluation right. | |
6814 | Map_type* mt = args->front()->type()->map_type(); | |
6815 | Temporary_statement* map_temp = | |
6816 | Statement::make_temporary(mt, args->front(), loc); | |
6817 | inserter->insert(map_temp); | |
6818 | ||
6819 | Temporary_statement* key_temp = | |
6820 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
6821 | inserter->insert(key_temp); | |
6822 | ||
6823 | Expression* e1 = Expression::make_temporary_reference(map_temp, | |
6824 | loc); | |
6825 | Expression* e2 = Expression::make_temporary_reference(key_temp, | |
6826 | loc); | |
6827 | e2 = Expression::make_unary(OPERATOR_AND, e2, loc); | |
6828 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), | |
6829 | 2, e1, e2); | |
6830 | } | |
6831 | } | |
6832 | break; | |
e440a328 | 6833 | } |
6834 | ||
6835 | return this; | |
6836 | } | |
6837 | ||
a9182619 | 6838 | // Lower a make expression. |
6839 | ||
6840 | Expression* | |
6841 | Builtin_call_expression::lower_make() | |
6842 | { | |
b13c66cd | 6843 | Location loc = this->location(); |
a9182619 | 6844 | |
6845 | const Expression_list* args = this->args(); | |
6846 | if (args == NULL || args->size() < 1) | |
6847 | { | |
6848 | this->report_error(_("not enough arguments")); | |
6849 | return Expression::make_error(this->location()); | |
6850 | } | |
6851 | ||
6852 | Expression_list::const_iterator parg = args->begin(); | |
6853 | ||
6854 | Expression* first_arg = *parg; | |
6855 | if (!first_arg->is_type_expression()) | |
6856 | { | |
6857 | error_at(first_arg->location(), "expected type"); | |
6858 | this->set_is_error(); | |
6859 | return Expression::make_error(this->location()); | |
6860 | } | |
6861 | Type* type = first_arg->type(); | |
6862 | ||
6863 | bool is_slice = false; | |
6864 | bool is_map = false; | |
6865 | bool is_chan = false; | |
411eb89e | 6866 | if (type->is_slice_type()) |
a9182619 | 6867 | is_slice = true; |
6868 | else if (type->map_type() != NULL) | |
6869 | is_map = true; | |
6870 | else if (type->channel_type() != NULL) | |
6871 | is_chan = true; | |
6872 | else | |
6873 | { | |
6874 | this->report_error(_("invalid type for make function")); | |
6875 | return Expression::make_error(this->location()); | |
6876 | } | |
6877 | ||
ac84c822 | 6878 | bool have_big_args = false; |
6879 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
6880 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
6881 | ||
a9182619 | 6882 | ++parg; |
6883 | Expression* len_arg; | |
6884 | if (parg == args->end()) | |
6885 | { | |
6886 | if (is_slice) | |
6887 | { | |
6888 | this->report_error(_("length required when allocating a slice")); | |
6889 | return Expression::make_error(this->location()); | |
6890 | } | |
6891 | ||
6892 | mpz_t zval; | |
6893 | mpz_init_set_ui(zval, 0); | |
6894 | len_arg = Expression::make_integer(&zval, NULL, loc); | |
6895 | mpz_clear(zval); | |
6896 | } | |
6897 | else | |
6898 | { | |
6899 | len_arg = *parg; | |
6900 | if (!this->check_int_value(len_arg)) | |
6901 | { | |
6902 | this->report_error(_("bad size for make")); | |
6903 | return Expression::make_error(this->location()); | |
6904 | } | |
ac84c822 | 6905 | if (len_arg->type()->integer_type() != NULL |
6906 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
6907 | have_big_args = true; | |
a9182619 | 6908 | ++parg; |
6909 | } | |
6910 | ||
6911 | Expression* cap_arg = NULL; | |
6912 | if (is_slice && parg != args->end()) | |
6913 | { | |
6914 | cap_arg = *parg; | |
6915 | if (!this->check_int_value(cap_arg)) | |
6916 | { | |
6917 | this->report_error(_("bad capacity when making slice")); | |
6918 | return Expression::make_error(this->location()); | |
6919 | } | |
ac84c822 | 6920 | if (cap_arg->type()->integer_type() != NULL |
6921 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
6922 | have_big_args = true; | |
a9182619 | 6923 | ++parg; |
6924 | } | |
6925 | ||
6926 | if (parg != args->end()) | |
6927 | { | |
6928 | this->report_error(_("too many arguments to make")); | |
6929 | return Expression::make_error(this->location()); | |
6930 | } | |
6931 | ||
b13c66cd | 6932 | Location type_loc = first_arg->location(); |
a9182619 | 6933 | Expression* type_arg; |
6934 | if (is_slice || is_chan) | |
6935 | type_arg = Expression::make_type_descriptor(type, type_loc); | |
6936 | else if (is_map) | |
6937 | type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); | |
6938 | else | |
6939 | go_unreachable(); | |
6940 | ||
6941 | Expression* call; | |
6942 | if (is_slice) | |
6943 | { | |
6944 | if (cap_arg == NULL) | |
ac84c822 | 6945 | call = Runtime::make_call((have_big_args |
6946 | ? Runtime::MAKESLICE1BIG | |
6947 | : Runtime::MAKESLICE1), | |
6948 | loc, 2, type_arg, len_arg); | |
a9182619 | 6949 | else |
ac84c822 | 6950 | call = Runtime::make_call((have_big_args |
6951 | ? Runtime::MAKESLICE2BIG | |
6952 | : Runtime::MAKESLICE2), | |
6953 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 6954 | } |
6955 | else if (is_map) | |
ac84c822 | 6956 | call = Runtime::make_call((have_big_args |
6957 | ? Runtime::MAKEMAPBIG | |
6958 | : Runtime::MAKEMAP), | |
6959 | loc, 2, type_arg, len_arg); | |
a9182619 | 6960 | else if (is_chan) |
ac84c822 | 6961 | call = Runtime::make_call((have_big_args |
6962 | ? Runtime::MAKECHANBIG | |
6963 | : Runtime::MAKECHAN), | |
6964 | loc, 2, type_arg, len_arg); | |
a9182619 | 6965 | else |
6966 | go_unreachable(); | |
6967 | ||
6968 | return Expression::make_unsafe_cast(type, call, loc); | |
6969 | } | |
6970 | ||
6971 | // Return whether an expression has an integer value. Report an error | |
6972 | // if not. This is used when handling calls to the predeclared make | |
6973 | // function. | |
6974 | ||
6975 | bool | |
6976 | Builtin_call_expression::check_int_value(Expression* e) | |
6977 | { | |
6978 | if (e->type()->integer_type() != NULL) | |
6979 | return true; | |
6980 | ||
6981 | // Check for a floating point constant with integer value. | |
0c77715b | 6982 | Numeric_constant nc; |
6983 | mpz_t ival; | |
6984 | if (e->numeric_constant_value(&nc) && nc.to_int(&ival)) | |
a9182619 | 6985 | { |
a9182619 | 6986 | mpz_clear(ival); |
0c77715b | 6987 | return true; |
a9182619 | 6988 | } |
6989 | ||
a9182619 | 6990 | return false; |
6991 | } | |
6992 | ||
e440a328 | 6993 | // Return the type of the real or imag functions, given the type of |
6994 | // the argument. We need to map complex to float, complex64 to | |
6995 | // float32, and complex128 to float64, so it has to be done by name. | |
6996 | // This returns NULL if it can't figure out the type. | |
6997 | ||
6998 | Type* | |
6999 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7000 | { | |
7001 | if (arg_type == NULL || arg_type->is_abstract()) | |
7002 | return NULL; | |
7003 | Named_type* nt = arg_type->named_type(); | |
7004 | if (nt == NULL) | |
7005 | return NULL; | |
7006 | while (nt->real_type()->named_type() != NULL) | |
7007 | nt = nt->real_type()->named_type(); | |
48080209 | 7008 | if (nt->name() == "complex64") |
e440a328 | 7009 | return Type::lookup_float_type("float32"); |
7010 | else if (nt->name() == "complex128") | |
7011 | return Type::lookup_float_type("float64"); | |
7012 | else | |
7013 | return NULL; | |
7014 | } | |
7015 | ||
48080209 | 7016 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7017 | // argments. Like real_imag_type, we have to map by name. |
7018 | ||
7019 | Type* | |
48080209 | 7020 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7021 | { |
7022 | if (arg_type == NULL || arg_type->is_abstract()) | |
7023 | return NULL; | |
7024 | Named_type* nt = arg_type->named_type(); | |
7025 | if (nt == NULL) | |
7026 | return NULL; | |
7027 | while (nt->real_type()->named_type() != NULL) | |
7028 | nt = nt->real_type()->named_type(); | |
48080209 | 7029 | if (nt->name() == "float32") |
e440a328 | 7030 | return Type::lookup_complex_type("complex64"); |
7031 | else if (nt->name() == "float64") | |
7032 | return Type::lookup_complex_type("complex128"); | |
7033 | else | |
7034 | return NULL; | |
7035 | } | |
7036 | ||
7037 | // Return a single argument, or NULL if there isn't one. | |
7038 | ||
7039 | Expression* | |
7040 | Builtin_call_expression::one_arg() const | |
7041 | { | |
7042 | const Expression_list* args = this->args(); | |
aa615cb3 | 7043 | if (args == NULL || args->size() != 1) |
e440a328 | 7044 | return NULL; |
7045 | return args->front(); | |
7046 | } | |
7047 | ||
83921647 | 7048 | // A traversal class which looks for a call or receive expression. |
7049 | ||
7050 | class Find_call_expression : public Traverse | |
7051 | { | |
7052 | public: | |
7053 | Find_call_expression() | |
7054 | : Traverse(traverse_expressions), | |
7055 | found_(false) | |
7056 | { } | |
7057 | ||
7058 | int | |
7059 | expression(Expression**); | |
7060 | ||
7061 | bool | |
7062 | found() | |
7063 | { return this->found_; } | |
7064 | ||
7065 | private: | |
7066 | bool found_; | |
7067 | }; | |
7068 | ||
7069 | int | |
7070 | Find_call_expression::expression(Expression** pexpr) | |
7071 | { | |
7072 | if ((*pexpr)->call_expression() != NULL | |
7073 | || (*pexpr)->receive_expression() != NULL) | |
7074 | { | |
7075 | this->found_ = true; | |
7076 | return TRAVERSE_EXIT; | |
7077 | } | |
7078 | return TRAVERSE_CONTINUE; | |
7079 | } | |
7080 | ||
7081 | // Return whether this is constant: len of a string constant, or len | |
7082 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7083 | // unsafe.Alignof. | |
e440a328 | 7084 | |
7085 | bool | |
7086 | Builtin_call_expression::do_is_constant() const | |
7087 | { | |
7088 | switch (this->code_) | |
7089 | { | |
7090 | case BUILTIN_LEN: | |
7091 | case BUILTIN_CAP: | |
7092 | { | |
0f914071 | 7093 | if (this->seen_) |
7094 | return false; | |
7095 | ||
e440a328 | 7096 | Expression* arg = this->one_arg(); |
7097 | if (arg == NULL) | |
7098 | return false; | |
7099 | Type* arg_type = arg->type(); | |
7100 | ||
7101 | if (arg_type->points_to() != NULL | |
7102 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7103 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7104 | arg_type = arg_type->points_to(); |
7105 | ||
83921647 | 7106 | // The len and cap functions are only constant if there are no |
7107 | // function calls or channel operations in the arguments. | |
7108 | // Otherwise we have to make the call. | |
7109 | if (!arg->is_constant()) | |
7110 | { | |
7111 | Find_call_expression find_call; | |
7112 | Expression::traverse(&arg, &find_call); | |
7113 | if (find_call.found()) | |
7114 | return false; | |
7115 | } | |
7116 | ||
e440a328 | 7117 | if (arg_type->array_type() != NULL |
7118 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7119 | return true; |
e440a328 | 7120 | |
7121 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7122 | { |
7123 | this->seen_ = true; | |
7124 | bool ret = arg->is_constant(); | |
7125 | this->seen_ = false; | |
7126 | return ret; | |
7127 | } | |
e440a328 | 7128 | } |
7129 | break; | |
7130 | ||
7131 | case BUILTIN_SIZEOF: | |
7132 | case BUILTIN_ALIGNOF: | |
7133 | return this->one_arg() != NULL; | |
7134 | ||
7135 | case BUILTIN_OFFSETOF: | |
7136 | { | |
7137 | Expression* arg = this->one_arg(); | |
7138 | if (arg == NULL) | |
7139 | return false; | |
7140 | return arg->field_reference_expression() != NULL; | |
7141 | } | |
7142 | ||
48080209 | 7143 | case BUILTIN_COMPLEX: |
e440a328 | 7144 | { |
7145 | const Expression_list* args = this->args(); | |
7146 | if (args != NULL && args->size() == 2) | |
7147 | return args->front()->is_constant() && args->back()->is_constant(); | |
7148 | } | |
7149 | break; | |
7150 | ||
7151 | case BUILTIN_REAL: | |
7152 | case BUILTIN_IMAG: | |
7153 | { | |
7154 | Expression* arg = this->one_arg(); | |
7155 | return arg != NULL && arg->is_constant(); | |
7156 | } | |
7157 | ||
7158 | default: | |
7159 | break; | |
7160 | } | |
7161 | ||
7162 | return false; | |
7163 | } | |
7164 | ||
0c77715b | 7165 | // Return a numeric constant if possible. |
e440a328 | 7166 | |
7167 | bool | |
0c77715b | 7168 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7169 | { |
7170 | if (this->code_ == BUILTIN_LEN | |
7171 | || this->code_ == BUILTIN_CAP) | |
7172 | { | |
7173 | Expression* arg = this->one_arg(); | |
7174 | if (arg == NULL) | |
7175 | return false; | |
7176 | Type* arg_type = arg->type(); | |
7177 | ||
7178 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7179 | { | |
7180 | std::string sval; | |
7181 | if (arg->string_constant_value(&sval)) | |
7182 | { | |
0c77715b | 7183 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7184 | sval.length()); | |
e440a328 | 7185 | return true; |
7186 | } | |
7187 | } | |
7188 | ||
7189 | if (arg_type->points_to() != NULL | |
7190 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7191 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7192 | arg_type = arg_type->points_to(); |
7193 | ||
7194 | if (arg_type->array_type() != NULL | |
7195 | && arg_type->array_type()->length() != NULL) | |
7196 | { | |
0f914071 | 7197 | if (this->seen_) |
7198 | return false; | |
e440a328 | 7199 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7200 | this->seen_ = true; |
0c77715b | 7201 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7202 | this->seen_ = false; |
7203 | if (r) | |
e440a328 | 7204 | { |
0c77715b | 7205 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7206 | this->location())) | |
7207 | r = false; | |
e440a328 | 7208 | } |
0c77715b | 7209 | return r; |
e440a328 | 7210 | } |
7211 | } | |
7212 | else if (this->code_ == BUILTIN_SIZEOF | |
7213 | || this->code_ == BUILTIN_ALIGNOF) | |
7214 | { | |
7215 | Expression* arg = this->one_arg(); | |
7216 | if (arg == NULL) | |
7217 | return false; | |
7218 | Type* arg_type = arg->type(); | |
5c13bd80 | 7219 | if (arg_type->is_error()) |
e440a328 | 7220 | return false; |
7221 | if (arg_type->is_abstract()) | |
7222 | return false; | |
9aa9e2df | 7223 | if (arg_type->named_type() != NULL) |
7224 | arg_type->named_type()->convert(this->gogo_); | |
927a01eb | 7225 | |
7226 | unsigned int ret; | |
e440a328 | 7227 | if (this->code_ == BUILTIN_SIZEOF) |
7228 | { | |
927a01eb | 7229 | if (!arg_type->backend_type_size(this->gogo_, &ret)) |
e440a328 | 7230 | return false; |
7231 | } | |
7232 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7233 | { | |
637bd3af | 7234 | if (arg->field_reference_expression() == NULL) |
927a01eb | 7235 | { |
7236 | if (!arg_type->backend_type_align(this->gogo_, &ret)) | |
7237 | return false; | |
7238 | } | |
637bd3af | 7239 | else |
e440a328 | 7240 | { |
7241 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7242 | // the type of f when it is used as a field in a struct. | |
927a01eb | 7243 | if (!arg_type->backend_type_field_align(this->gogo_, &ret)) |
7244 | return false; | |
e440a328 | 7245 | } |
e440a328 | 7246 | } |
7247 | else | |
c3e6f413 | 7248 | go_unreachable(); |
927a01eb | 7249 | |
7ba86326 | 7250 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
7251 | static_cast<unsigned long>(ret)); | |
e440a328 | 7252 | return true; |
7253 | } | |
7254 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7255 | { | |
7256 | Expression* arg = this->one_arg(); | |
7257 | if (arg == NULL) | |
7258 | return false; | |
7259 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7260 | if (farg == NULL) | |
7261 | return false; | |
7262 | Expression* struct_expr = farg->expr(); | |
7263 | Type* st = struct_expr->type(); | |
7264 | if (st->struct_type() == NULL) | |
7265 | return false; | |
9aa9e2df | 7266 | if (st->named_type() != NULL) |
7267 | st->named_type()->convert(this->gogo_); | |
927a01eb | 7268 | unsigned int offset; |
7269 | if (!st->struct_type()->backend_field_offset(this->gogo_, | |
7270 | farg->field_index(), | |
7271 | &offset)) | |
e440a328 | 7272 | return false; |
7ba86326 | 7273 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
7274 | static_cast<unsigned long>(offset)); | |
e440a328 | 7275 | return true; |
7276 | } | |
0c77715b | 7277 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7278 | { |
7279 | Expression* arg = this->one_arg(); | |
7280 | if (arg == NULL) | |
7281 | return false; | |
7282 | ||
0c77715b | 7283 | Numeric_constant argnc; |
7284 | if (!arg->numeric_constant_value(&argnc)) | |
7285 | return false; | |
7286 | ||
e440a328 | 7287 | mpfr_t real; |
7288 | mpfr_t imag; | |
0c77715b | 7289 | if (!argnc.to_complex(&real, &imag)) |
7290 | return false; | |
e440a328 | 7291 | |
0c77715b | 7292 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7293 | if (this->code_ == BUILTIN_REAL) | |
7294 | nc->set_float(type, real); | |
7295 | else | |
7296 | nc->set_float(type, imag); | |
7297 | return true; | |
e440a328 | 7298 | } |
0c77715b | 7299 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7300 | { |
7301 | const Expression_list* args = this->args(); | |
7302 | if (args == NULL || args->size() != 2) | |
7303 | return false; | |
7304 | ||
0c77715b | 7305 | Numeric_constant rnc; |
7306 | if (!args->front()->numeric_constant_value(&rnc)) | |
7307 | return false; | |
7308 | Numeric_constant inc; | |
7309 | if (!args->back()->numeric_constant_value(&inc)) | |
7310 | return false; | |
7311 | ||
7312 | if (rnc.type() != NULL | |
7313 | && !rnc.type()->is_abstract() | |
7314 | && inc.type() != NULL | |
7315 | && !inc.type()->is_abstract() | |
7316 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7317 | return false; | |
7318 | ||
e440a328 | 7319 | mpfr_t r; |
0c77715b | 7320 | if (!rnc.to_float(&r)) |
7321 | return false; | |
7322 | mpfr_t i; | |
7323 | if (!inc.to_float(&i)) | |
e440a328 | 7324 | { |
7325 | mpfr_clear(r); | |
7326 | return false; | |
7327 | } | |
7328 | ||
0c77715b | 7329 | Type* arg_type = rnc.type(); |
7330 | if (arg_type == NULL || arg_type->is_abstract()) | |
7331 | arg_type = inc.type(); | |
e440a328 | 7332 | |
0c77715b | 7333 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7334 | nc->set_complex(type, r, i); | |
e440a328 | 7335 | |
7336 | mpfr_clear(r); | |
7337 | mpfr_clear(i); | |
7338 | ||
0c77715b | 7339 | return true; |
e440a328 | 7340 | } |
7341 | ||
7342 | return false; | |
7343 | } | |
7344 | ||
a7549a6a | 7345 | // Give an error if we are discarding the value of an expression which |
7346 | // should not normally be discarded. We don't give an error for | |
7347 | // discarding the value of an ordinary function call, but we do for | |
7348 | // builtin functions, purely for consistency with the gc compiler. | |
7349 | ||
4f2138d7 | 7350 | bool |
a7549a6a | 7351 | Builtin_call_expression::do_discarding_value() |
7352 | { | |
7353 | switch (this->code_) | |
7354 | { | |
7355 | case BUILTIN_INVALID: | |
7356 | default: | |
7357 | go_unreachable(); | |
7358 | ||
7359 | case BUILTIN_APPEND: | |
7360 | case BUILTIN_CAP: | |
7361 | case BUILTIN_COMPLEX: | |
7362 | case BUILTIN_IMAG: | |
7363 | case BUILTIN_LEN: | |
7364 | case BUILTIN_MAKE: | |
7365 | case BUILTIN_NEW: | |
7366 | case BUILTIN_REAL: | |
7367 | case BUILTIN_ALIGNOF: | |
7368 | case BUILTIN_OFFSETOF: | |
7369 | case BUILTIN_SIZEOF: | |
7370 | this->unused_value_error(); | |
4f2138d7 | 7371 | return false; |
a7549a6a | 7372 | |
7373 | case BUILTIN_CLOSE: | |
7374 | case BUILTIN_COPY: | |
1cce762f | 7375 | case BUILTIN_DELETE: |
a7549a6a | 7376 | case BUILTIN_PANIC: |
7377 | case BUILTIN_PRINT: | |
7378 | case BUILTIN_PRINTLN: | |
7379 | case BUILTIN_RECOVER: | |
4f2138d7 | 7380 | return true; |
a7549a6a | 7381 | } |
7382 | } | |
7383 | ||
e440a328 | 7384 | // Return the type. |
7385 | ||
7386 | Type* | |
7387 | Builtin_call_expression::do_type() | |
7388 | { | |
7389 | switch (this->code_) | |
7390 | { | |
7391 | case BUILTIN_INVALID: | |
7392 | default: | |
c3e6f413 | 7393 | go_unreachable(); |
e440a328 | 7394 | |
7395 | case BUILTIN_NEW: | |
7396 | case BUILTIN_MAKE: | |
7397 | { | |
7398 | const Expression_list* args = this->args(); | |
7399 | if (args == NULL || args->empty()) | |
7400 | return Type::make_error_type(); | |
7401 | return Type::make_pointer_type(args->front()->type()); | |
7402 | } | |
7403 | ||
7404 | case BUILTIN_CAP: | |
7405 | case BUILTIN_COPY: | |
7406 | case BUILTIN_LEN: | |
7ba86326 | 7407 | return Type::lookup_integer_type("int"); |
7408 | ||
e440a328 | 7409 | case BUILTIN_ALIGNOF: |
7410 | case BUILTIN_OFFSETOF: | |
7411 | case BUILTIN_SIZEOF: | |
7ba86326 | 7412 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7413 | |
7414 | case BUILTIN_CLOSE: | |
1cce762f | 7415 | case BUILTIN_DELETE: |
e440a328 | 7416 | case BUILTIN_PANIC: |
7417 | case BUILTIN_PRINT: | |
7418 | case BUILTIN_PRINTLN: | |
7419 | return Type::make_void_type(); | |
7420 | ||
e440a328 | 7421 | case BUILTIN_RECOVER: |
823c7e3d | 7422 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7423 | |
7424 | case BUILTIN_APPEND: | |
7425 | { | |
7426 | const Expression_list* args = this->args(); | |
7427 | if (args == NULL || args->empty()) | |
7428 | return Type::make_error_type(); | |
7429 | return args->front()->type(); | |
7430 | } | |
7431 | ||
7432 | case BUILTIN_REAL: | |
7433 | case BUILTIN_IMAG: | |
7434 | { | |
7435 | Expression* arg = this->one_arg(); | |
7436 | if (arg == NULL) | |
7437 | return Type::make_error_type(); | |
7438 | Type* t = arg->type(); | |
7439 | if (t->is_abstract()) | |
7440 | t = t->make_non_abstract_type(); | |
7441 | t = Builtin_call_expression::real_imag_type(t); | |
7442 | if (t == NULL) | |
7443 | t = Type::make_error_type(); | |
7444 | return t; | |
7445 | } | |
7446 | ||
48080209 | 7447 | case BUILTIN_COMPLEX: |
e440a328 | 7448 | { |
7449 | const Expression_list* args = this->args(); | |
7450 | if (args == NULL || args->size() != 2) | |
7451 | return Type::make_error_type(); | |
7452 | Type* t = args->front()->type(); | |
7453 | if (t->is_abstract()) | |
7454 | { | |
7455 | t = args->back()->type(); | |
7456 | if (t->is_abstract()) | |
7457 | t = t->make_non_abstract_type(); | |
7458 | } | |
48080209 | 7459 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7460 | if (t == NULL) |
7461 | t = Type::make_error_type(); | |
7462 | return t; | |
7463 | } | |
7464 | } | |
7465 | } | |
7466 | ||
7467 | // Determine the type. | |
7468 | ||
7469 | void | |
7470 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7471 | { | |
fb94b0ca | 7472 | if (!this->determining_types()) |
7473 | return; | |
7474 | ||
e440a328 | 7475 | this->fn()->determine_type_no_context(); |
7476 | ||
7477 | const Expression_list* args = this->args(); | |
7478 | ||
7479 | bool is_print; | |
7480 | Type* arg_type = NULL; | |
7481 | switch (this->code_) | |
7482 | { | |
7483 | case BUILTIN_PRINT: | |
7484 | case BUILTIN_PRINTLN: | |
7485 | // Do not force a large integer constant to "int". | |
7486 | is_print = true; | |
7487 | break; | |
7488 | ||
7489 | case BUILTIN_REAL: | |
7490 | case BUILTIN_IMAG: | |
48080209 | 7491 | arg_type = Builtin_call_expression::complex_type(context->type); |
e440a328 | 7492 | is_print = false; |
7493 | break; | |
7494 | ||
48080209 | 7495 | case BUILTIN_COMPLEX: |
e440a328 | 7496 | { |
48080209 | 7497 | // For the complex function the type of one operand can |
e440a328 | 7498 | // determine the type of the other, as in a binary expression. |
7499 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
7500 | if (args != NULL && args->size() == 2) | |
7501 | { | |
7502 | Type* t1 = args->front()->type(); | |
c849bb59 | 7503 | Type* t2 = args->back()->type(); |
e440a328 | 7504 | if (!t1->is_abstract()) |
7505 | arg_type = t1; | |
7506 | else if (!t2->is_abstract()) | |
7507 | arg_type = t2; | |
7508 | } | |
7509 | is_print = false; | |
7510 | } | |
7511 | break; | |
7512 | ||
7513 | default: | |
7514 | is_print = false; | |
7515 | break; | |
7516 | } | |
7517 | ||
7518 | if (args != NULL) | |
7519 | { | |
7520 | for (Expression_list::const_iterator pa = args->begin(); | |
7521 | pa != args->end(); | |
7522 | ++pa) | |
7523 | { | |
7524 | Type_context subcontext; | |
7525 | subcontext.type = arg_type; | |
7526 | ||
7527 | if (is_print) | |
7528 | { | |
7529 | // We want to print large constants, we so can't just | |
7530 | // use the appropriate nonabstract type. Use uint64 for | |
7531 | // an integer if we know it is nonnegative, otherwise | |
7532 | // use int64 for a integer, otherwise use float64 for a | |
7533 | // float or complex128 for a complex. | |
7534 | Type* want_type = NULL; | |
7535 | Type* atype = (*pa)->type(); | |
7536 | if (atype->is_abstract()) | |
7537 | { | |
7538 | if (atype->integer_type() != NULL) | |
7539 | { | |
0c77715b | 7540 | Numeric_constant nc; |
7541 | if (this->numeric_constant_value(&nc)) | |
7542 | { | |
7543 | mpz_t val; | |
7544 | if (nc.to_int(&val)) | |
7545 | { | |
7546 | if (mpz_sgn(val) >= 0) | |
7547 | want_type = Type::lookup_integer_type("uint64"); | |
7548 | mpz_clear(val); | |
7549 | } | |
7550 | } | |
7551 | if (want_type == NULL) | |
e440a328 | 7552 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 7553 | } |
7554 | else if (atype->float_type() != NULL) | |
7555 | want_type = Type::lookup_float_type("float64"); | |
7556 | else if (atype->complex_type() != NULL) | |
7557 | want_type = Type::lookup_complex_type("complex128"); | |
7558 | else if (atype->is_abstract_string_type()) | |
7559 | want_type = Type::lookup_string_type(); | |
7560 | else if (atype->is_abstract_boolean_type()) | |
7561 | want_type = Type::lookup_bool_type(); | |
7562 | else | |
c3e6f413 | 7563 | go_unreachable(); |
e440a328 | 7564 | subcontext.type = want_type; |
7565 | } | |
7566 | } | |
7567 | ||
7568 | (*pa)->determine_type(&subcontext); | |
7569 | } | |
7570 | } | |
7571 | } | |
7572 | ||
7573 | // If there is exactly one argument, return true. Otherwise give an | |
7574 | // error message and return false. | |
7575 | ||
7576 | bool | |
7577 | Builtin_call_expression::check_one_arg() | |
7578 | { | |
7579 | const Expression_list* args = this->args(); | |
7580 | if (args == NULL || args->size() < 1) | |
7581 | { | |
7582 | this->report_error(_("not enough arguments")); | |
7583 | return false; | |
7584 | } | |
7585 | else if (args->size() > 1) | |
7586 | { | |
7587 | this->report_error(_("too many arguments")); | |
7588 | return false; | |
7589 | } | |
7590 | if (args->front()->is_error_expression() | |
5c13bd80 | 7591 | || args->front()->type()->is_error()) |
e440a328 | 7592 | { |
7593 | this->set_is_error(); | |
7594 | return false; | |
7595 | } | |
7596 | return true; | |
7597 | } | |
7598 | ||
7599 | // Check argument types for a builtin function. | |
7600 | ||
7601 | void | |
7602 | Builtin_call_expression::do_check_types(Gogo*) | |
7603 | { | |
375646ea | 7604 | if (this->is_error_expression()) |
7605 | return; | |
e440a328 | 7606 | switch (this->code_) |
7607 | { | |
7608 | case BUILTIN_INVALID: | |
7609 | case BUILTIN_NEW: | |
7610 | case BUILTIN_MAKE: | |
cd238b8d | 7611 | case BUILTIN_DELETE: |
e440a328 | 7612 | return; |
7613 | ||
7614 | case BUILTIN_LEN: | |
7615 | case BUILTIN_CAP: | |
7616 | { | |
7617 | // The single argument may be either a string or an array or a | |
7618 | // map or a channel, or a pointer to a closed array. | |
7619 | if (this->check_one_arg()) | |
7620 | { | |
7621 | Type* arg_type = this->one_arg()->type(); | |
7622 | if (arg_type->points_to() != NULL | |
7623 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7624 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7625 | arg_type = arg_type->points_to(); |
7626 | if (this->code_ == BUILTIN_CAP) | |
7627 | { | |
5c13bd80 | 7628 | if (!arg_type->is_error() |
e440a328 | 7629 | && arg_type->array_type() == NULL |
7630 | && arg_type->channel_type() == NULL) | |
7631 | this->report_error(_("argument must be array or slice " | |
7632 | "or channel")); | |
7633 | } | |
7634 | else | |
7635 | { | |
5c13bd80 | 7636 | if (!arg_type->is_error() |
e440a328 | 7637 | && !arg_type->is_string_type() |
7638 | && arg_type->array_type() == NULL | |
7639 | && arg_type->map_type() == NULL | |
7640 | && arg_type->channel_type() == NULL) | |
7641 | this->report_error(_("argument must be string or " | |
7642 | "array or slice or map or channel")); | |
7643 | } | |
7644 | } | |
7645 | } | |
7646 | break; | |
7647 | ||
7648 | case BUILTIN_PRINT: | |
7649 | case BUILTIN_PRINTLN: | |
7650 | { | |
7651 | const Expression_list* args = this->args(); | |
7652 | if (args == NULL) | |
7653 | { | |
7654 | if (this->code_ == BUILTIN_PRINT) | |
7655 | warning_at(this->location(), 0, | |
7656 | "no arguments for builtin function %<%s%>", | |
7657 | (this->code_ == BUILTIN_PRINT | |
7658 | ? "print" | |
7659 | : "println")); | |
7660 | } | |
7661 | else | |
7662 | { | |
7663 | for (Expression_list::const_iterator p = args->begin(); | |
7664 | p != args->end(); | |
7665 | ++p) | |
7666 | { | |
7667 | Type* type = (*p)->type(); | |
5c13bd80 | 7668 | if (type->is_error() |
e440a328 | 7669 | || type->is_string_type() |
7670 | || type->integer_type() != NULL | |
7671 | || type->float_type() != NULL | |
7672 | || type->complex_type() != NULL | |
7673 | || type->is_boolean_type() | |
7674 | || type->points_to() != NULL | |
7675 | || type->interface_type() != NULL | |
7676 | || type->channel_type() != NULL | |
7677 | || type->map_type() != NULL | |
7678 | || type->function_type() != NULL | |
411eb89e | 7679 | || type->is_slice_type()) |
e440a328 | 7680 | ; |
acf8e158 | 7681 | else if ((*p)->is_type_expression()) |
7682 | { | |
7683 | // If this is a type expression it's going to give | |
7684 | // an error anyhow, so we don't need one here. | |
7685 | } | |
e440a328 | 7686 | else |
7687 | this->report_error(_("unsupported argument type to " | |
7688 | "builtin function")); | |
7689 | } | |
7690 | } | |
7691 | } | |
7692 | break; | |
7693 | ||
7694 | case BUILTIN_CLOSE: | |
e440a328 | 7695 | if (this->check_one_arg()) |
7696 | { | |
7697 | if (this->one_arg()->type()->channel_type() == NULL) | |
7698 | this->report_error(_("argument must be channel")); | |
5202d986 | 7699 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
7700 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 7701 | } |
7702 | break; | |
7703 | ||
7704 | case BUILTIN_PANIC: | |
7705 | case BUILTIN_SIZEOF: | |
7706 | case BUILTIN_ALIGNOF: | |
7707 | this->check_one_arg(); | |
7708 | break; | |
7709 | ||
7710 | case BUILTIN_RECOVER: | |
7711 | if (this->args() != NULL && !this->args()->empty()) | |
7712 | this->report_error(_("too many arguments")); | |
7713 | break; | |
7714 | ||
7715 | case BUILTIN_OFFSETOF: | |
7716 | if (this->check_one_arg()) | |
7717 | { | |
7718 | Expression* arg = this->one_arg(); | |
7719 | if (arg->field_reference_expression() == NULL) | |
7720 | this->report_error(_("argument must be a field reference")); | |
7721 | } | |
7722 | break; | |
7723 | ||
7724 | case BUILTIN_COPY: | |
7725 | { | |
7726 | const Expression_list* args = this->args(); | |
7727 | if (args == NULL || args->size() < 2) | |
7728 | { | |
7729 | this->report_error(_("not enough arguments")); | |
7730 | break; | |
7731 | } | |
7732 | else if (args->size() > 2) | |
7733 | { | |
7734 | this->report_error(_("too many arguments")); | |
7735 | break; | |
7736 | } | |
7737 | Type* arg1_type = args->front()->type(); | |
7738 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 7739 | if (arg1_type->is_error() || arg2_type->is_error()) |
e440a328 | 7740 | break; |
7741 | ||
7742 | Type* e1; | |
411eb89e | 7743 | if (arg1_type->is_slice_type()) |
e440a328 | 7744 | e1 = arg1_type->array_type()->element_type(); |
7745 | else | |
7746 | { | |
7747 | this->report_error(_("left argument must be a slice")); | |
7748 | break; | |
7749 | } | |
7750 | ||
411eb89e | 7751 | if (arg2_type->is_slice_type()) |
60963afd | 7752 | { |
7753 | Type* e2 = arg2_type->array_type()->element_type(); | |
7754 | if (!Type::are_identical(e1, e2, true, NULL)) | |
7755 | this->report_error(_("element types must be the same")); | |
7756 | } | |
e440a328 | 7757 | else if (arg2_type->is_string_type()) |
e440a328 | 7758 | { |
60963afd | 7759 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
7760 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 7761 | } |
60963afd | 7762 | else |
7763 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 7764 | } |
7765 | break; | |
7766 | ||
7767 | case BUILTIN_APPEND: | |
7768 | { | |
7769 | const Expression_list* args = this->args(); | |
b0d311a1 | 7770 | if (args == NULL || args->size() < 2) |
e440a328 | 7771 | { |
7772 | this->report_error(_("not enough arguments")); | |
7773 | break; | |
7774 | } | |
0b7755ec | 7775 | if (args->size() > 2) |
7776 | { | |
7777 | this->report_error(_("too many arguments")); | |
7778 | break; | |
7779 | } | |
cd238b8d | 7780 | if (args->front()->type()->is_error() |
7781 | || args->back()->type()->is_error()) | |
7782 | break; | |
7783 | ||
7784 | Array_type* at = args->front()->type()->array_type(); | |
7785 | Type* e = at->element_type(); | |
4fd4fcf4 | 7786 | |
7787 | // The language permits appending a string to a []byte, as a | |
7788 | // special case. | |
7789 | if (args->back()->type()->is_string_type()) | |
7790 | { | |
60963afd | 7791 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 7792 | break; |
7793 | } | |
7794 | ||
19fd40c3 | 7795 | // The language says that the second argument must be |
7796 | // assignable to a slice of the element type of the first | |
7797 | // argument. We already know the first argument is a slice | |
7798 | // type. | |
cd238b8d | 7799 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 7800 | std::string reason; |
19fd40c3 | 7801 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 7802 | { |
7803 | if (reason.empty()) | |
19fd40c3 | 7804 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 7805 | else |
7806 | { | |
19fd40c3 | 7807 | error_at(this->location(), "argument 2 has invalid type (%s)", |
e440a328 | 7808 | reason.c_str()); |
7809 | this->set_is_error(); | |
7810 | } | |
7811 | } | |
7812 | break; | |
7813 | } | |
7814 | ||
7815 | case BUILTIN_REAL: | |
7816 | case BUILTIN_IMAG: | |
7817 | if (this->check_one_arg()) | |
7818 | { | |
7819 | if (this->one_arg()->type()->complex_type() == NULL) | |
7820 | this->report_error(_("argument must have complex type")); | |
7821 | } | |
7822 | break; | |
7823 | ||
48080209 | 7824 | case BUILTIN_COMPLEX: |
e440a328 | 7825 | { |
7826 | const Expression_list* args = this->args(); | |
7827 | if (args == NULL || args->size() < 2) | |
7828 | this->report_error(_("not enough arguments")); | |
7829 | else if (args->size() > 2) | |
7830 | this->report_error(_("too many arguments")); | |
7831 | else if (args->front()->is_error_expression() | |
5c13bd80 | 7832 | || args->front()->type()->is_error() |
e440a328 | 7833 | || args->back()->is_error_expression() |
5c13bd80 | 7834 | || args->back()->type()->is_error()) |
e440a328 | 7835 | this->set_is_error(); |
7836 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 7837 | args->back()->type(), true, NULL)) |
48080209 | 7838 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 7839 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 7840 | this->report_error(_("complex arguments must have " |
e440a328 | 7841 | "floating-point type")); |
7842 | } | |
7843 | break; | |
7844 | ||
7845 | default: | |
c3e6f413 | 7846 | go_unreachable(); |
e440a328 | 7847 | } |
7848 | } | |
7849 | ||
7850 | // Return the tree for a builtin function. | |
7851 | ||
7852 | tree | |
7853 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
7854 | { | |
7855 | Gogo* gogo = context->gogo(); | |
b13c66cd | 7856 | Location location = this->location(); |
e440a328 | 7857 | switch (this->code_) |
7858 | { | |
7859 | case BUILTIN_INVALID: | |
7860 | case BUILTIN_NEW: | |
7861 | case BUILTIN_MAKE: | |
c3e6f413 | 7862 | go_unreachable(); |
e440a328 | 7863 | |
7864 | case BUILTIN_LEN: | |
7865 | case BUILTIN_CAP: | |
7866 | { | |
7867 | const Expression_list* args = this->args(); | |
c484d925 | 7868 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 7869 | Expression* arg = *args->begin(); |
7870 | Type* arg_type = arg->type(); | |
0f914071 | 7871 | |
7872 | if (this->seen_) | |
7873 | { | |
c484d925 | 7874 | go_assert(saw_errors()); |
0f914071 | 7875 | return error_mark_node; |
7876 | } | |
7877 | this->seen_ = true; | |
7878 | ||
e440a328 | 7879 | tree arg_tree = arg->get_tree(context); |
0f914071 | 7880 | |
7881 | this->seen_ = false; | |
7882 | ||
e440a328 | 7883 | if (arg_tree == error_mark_node) |
7884 | return error_mark_node; | |
7885 | ||
7886 | if (arg_type->points_to() != NULL) | |
7887 | { | |
7888 | arg_type = arg_type->points_to(); | |
c484d925 | 7889 | go_assert(arg_type->array_type() != NULL |
411eb89e | 7890 | && !arg_type->is_slice_type()); |
c484d925 | 7891 | go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); |
e440a328 | 7892 | arg_tree = build_fold_indirect_ref(arg_tree); |
7893 | } | |
7894 | ||
1b1f2abf | 7895 | Type* int_type = Type::lookup_integer_type("int"); |
7896 | tree int_type_tree = type_to_tree(int_type->get_backend(gogo)); | |
7897 | ||
e440a328 | 7898 | tree val_tree; |
7899 | if (this->code_ == BUILTIN_LEN) | |
7900 | { | |
7901 | if (arg_type->is_string_type()) | |
7902 | val_tree = String_type::length_tree(gogo, arg_tree); | |
7903 | else if (arg_type->array_type() != NULL) | |
0f914071 | 7904 | { |
7905 | if (this->seen_) | |
7906 | { | |
c484d925 | 7907 | go_assert(saw_errors()); |
0f914071 | 7908 | return error_mark_node; |
7909 | } | |
7910 | this->seen_ = true; | |
7911 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
7912 | this->seen_ = false; | |
7913 | } | |
e440a328 | 7914 | else if (arg_type->map_type() != NULL) |
7915 | { | |
9f0e0513 | 7916 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 7917 | static tree map_len_fndecl; |
7918 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
7919 | location, | |
7920 | "__go_map_len", | |
7921 | 1, | |
1b1f2abf | 7922 | int_type_tree, |
9f0e0513 | 7923 | arg_type_tree, |
e440a328 | 7924 | arg_tree); |
7925 | } | |
7926 | else if (arg_type->channel_type() != NULL) | |
7927 | { | |
9f0e0513 | 7928 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 7929 | static tree chan_len_fndecl; |
7930 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
7931 | location, | |
7932 | "__go_chan_len", | |
7933 | 1, | |
1b1f2abf | 7934 | int_type_tree, |
9f0e0513 | 7935 | arg_type_tree, |
e440a328 | 7936 | arg_tree); |
7937 | } | |
7938 | else | |
c3e6f413 | 7939 | go_unreachable(); |
e440a328 | 7940 | } |
7941 | else | |
7942 | { | |
7943 | if (arg_type->array_type() != NULL) | |
0f914071 | 7944 | { |
7945 | if (this->seen_) | |
7946 | { | |
c484d925 | 7947 | go_assert(saw_errors()); |
0f914071 | 7948 | return error_mark_node; |
7949 | } | |
7950 | this->seen_ = true; | |
7951 | val_tree = arg_type->array_type()->capacity_tree(gogo, | |
7952 | arg_tree); | |
7953 | this->seen_ = false; | |
7954 | } | |
e440a328 | 7955 | else if (arg_type->channel_type() != NULL) |
7956 | { | |
9f0e0513 | 7957 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 7958 | static tree chan_cap_fndecl; |
7959 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
7960 | location, | |
7961 | "__go_chan_cap", | |
7962 | 1, | |
1b1f2abf | 7963 | int_type_tree, |
9f0e0513 | 7964 | arg_type_tree, |
e440a328 | 7965 | arg_tree); |
7966 | } | |
7967 | else | |
c3e6f413 | 7968 | go_unreachable(); |
e440a328 | 7969 | } |
7970 | ||
1b1f2abf | 7971 | return fold_convert_loc(location.gcc_location(), int_type_tree, |
7972 | val_tree); | |
e440a328 | 7973 | } |
7974 | ||
7975 | case BUILTIN_PRINT: | |
7976 | case BUILTIN_PRINTLN: | |
7977 | { | |
7978 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
7979 | tree stmt_list = NULL_TREE; | |
7980 | ||
7981 | const Expression_list* call_args = this->args(); | |
7982 | if (call_args != NULL) | |
7983 | { | |
7984 | for (Expression_list::const_iterator p = call_args->begin(); | |
7985 | p != call_args->end(); | |
7986 | ++p) | |
7987 | { | |
7988 | if (is_ln && p != call_args->begin()) | |
7989 | { | |
7990 | static tree print_space_fndecl; | |
7991 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
7992 | location, | |
7993 | "__go_print_space", | |
7994 | 0, | |
7995 | void_type_node); | |
5fb82b5e | 7996 | if (call == error_mark_node) |
7997 | return error_mark_node; | |
e440a328 | 7998 | append_to_statement_list(call, &stmt_list); |
7999 | } | |
8000 | ||
8001 | Type* type = (*p)->type(); | |
8002 | ||
8003 | tree arg = (*p)->get_tree(context); | |
8004 | if (arg == error_mark_node) | |
8005 | return error_mark_node; | |
8006 | ||
8007 | tree* pfndecl; | |
8008 | const char* fnname; | |
8009 | if (type->is_string_type()) | |
8010 | { | |
8011 | static tree print_string_fndecl; | |
8012 | pfndecl = &print_string_fndecl; | |
8013 | fnname = "__go_print_string"; | |
8014 | } | |
8015 | else if (type->integer_type() != NULL | |
8016 | && type->integer_type()->is_unsigned()) | |
8017 | { | |
8018 | static tree print_uint64_fndecl; | |
8019 | pfndecl = &print_uint64_fndecl; | |
8020 | fnname = "__go_print_uint64"; | |
8021 | Type* itype = Type::lookup_integer_type("uint64"); | |
9f0e0513 | 8022 | Btype* bitype = itype->get_backend(gogo); |
b13c66cd | 8023 | arg = fold_convert_loc(location.gcc_location(), |
8024 | type_to_tree(bitype), arg); | |
e440a328 | 8025 | } |
8026 | else if (type->integer_type() != NULL) | |
8027 | { | |
8028 | static tree print_int64_fndecl; | |
8029 | pfndecl = &print_int64_fndecl; | |
8030 | fnname = "__go_print_int64"; | |
8031 | Type* itype = Type::lookup_integer_type("int64"); | |
9f0e0513 | 8032 | Btype* bitype = itype->get_backend(gogo); |
b13c66cd | 8033 | arg = fold_convert_loc(location.gcc_location(), |
8034 | type_to_tree(bitype), arg); | |
e440a328 | 8035 | } |
8036 | else if (type->float_type() != NULL) | |
8037 | { | |
8038 | static tree print_double_fndecl; | |
8039 | pfndecl = &print_double_fndecl; | |
8040 | fnname = "__go_print_double"; | |
b13c66cd | 8041 | arg = fold_convert_loc(location.gcc_location(), |
8042 | double_type_node, arg); | |
e440a328 | 8043 | } |
8044 | else if (type->complex_type() != NULL) | |
8045 | { | |
8046 | static tree print_complex_fndecl; | |
8047 | pfndecl = &print_complex_fndecl; | |
8048 | fnname = "__go_print_complex"; | |
b13c66cd | 8049 | arg = fold_convert_loc(location.gcc_location(), |
8050 | complex_double_type_node, arg); | |
e440a328 | 8051 | } |
8052 | else if (type->is_boolean_type()) | |
8053 | { | |
8054 | static tree print_bool_fndecl; | |
8055 | pfndecl = &print_bool_fndecl; | |
8056 | fnname = "__go_print_bool"; | |
8057 | } | |
8058 | else if (type->points_to() != NULL | |
8059 | || type->channel_type() != NULL | |
8060 | || type->map_type() != NULL | |
8061 | || type->function_type() != NULL) | |
8062 | { | |
8063 | static tree print_pointer_fndecl; | |
8064 | pfndecl = &print_pointer_fndecl; | |
8065 | fnname = "__go_print_pointer"; | |
b13c66cd | 8066 | arg = fold_convert_loc(location.gcc_location(), |
8067 | ptr_type_node, arg); | |
e440a328 | 8068 | } |
8069 | else if (type->interface_type() != NULL) | |
8070 | { | |
8071 | if (type->interface_type()->is_empty()) | |
8072 | { | |
8073 | static tree print_empty_interface_fndecl; | |
8074 | pfndecl = &print_empty_interface_fndecl; | |
8075 | fnname = "__go_print_empty_interface"; | |
8076 | } | |
8077 | else | |
8078 | { | |
8079 | static tree print_interface_fndecl; | |
8080 | pfndecl = &print_interface_fndecl; | |
8081 | fnname = "__go_print_interface"; | |
8082 | } | |
8083 | } | |
411eb89e | 8084 | else if (type->is_slice_type()) |
e440a328 | 8085 | { |
8086 | static tree print_slice_fndecl; | |
8087 | pfndecl = &print_slice_fndecl; | |
8088 | fnname = "__go_print_slice"; | |
8089 | } | |
8090 | else | |
cd238b8d | 8091 | { |
8092 | go_assert(saw_errors()); | |
8093 | return error_mark_node; | |
8094 | } | |
e440a328 | 8095 | |
8096 | tree call = Gogo::call_builtin(pfndecl, | |
8097 | location, | |
8098 | fnname, | |
8099 | 1, | |
8100 | void_type_node, | |
8101 | TREE_TYPE(arg), | |
8102 | arg); | |
5fb82b5e | 8103 | if (call == error_mark_node) |
8104 | return error_mark_node; | |
8105 | append_to_statement_list(call, &stmt_list); | |
e440a328 | 8106 | } |
8107 | } | |
8108 | ||
8109 | if (is_ln) | |
8110 | { | |
8111 | static tree print_nl_fndecl; | |
8112 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
8113 | location, | |
8114 | "__go_print_nl", | |
8115 | 0, | |
8116 | void_type_node); | |
5fb82b5e | 8117 | if (call == error_mark_node) |
8118 | return error_mark_node; | |
e440a328 | 8119 | append_to_statement_list(call, &stmt_list); |
8120 | } | |
8121 | ||
8122 | return stmt_list; | |
8123 | } | |
8124 | ||
8125 | case BUILTIN_PANIC: | |
8126 | { | |
8127 | const Expression_list* args = this->args(); | |
c484d925 | 8128 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8129 | Expression* arg = args->front(); |
8130 | tree arg_tree = arg->get_tree(context); | |
8131 | if (arg_tree == error_mark_node) | |
8132 | return error_mark_node; | |
b13c66cd | 8133 | Type *empty = |
823c7e3d | 8134 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8135 | arg_tree = Expression::convert_for_assignment(context, empty, |
8136 | arg->type(), | |
8137 | arg_tree, location); | |
8138 | static tree panic_fndecl; | |
8139 | tree call = Gogo::call_builtin(&panic_fndecl, | |
8140 | location, | |
8141 | "__go_panic", | |
8142 | 1, | |
8143 | void_type_node, | |
8144 | TREE_TYPE(arg_tree), | |
8145 | arg_tree); | |
5fb82b5e | 8146 | if (call == error_mark_node) |
8147 | return error_mark_node; | |
e440a328 | 8148 | // This function will throw an exception. |
8149 | TREE_NOTHROW(panic_fndecl) = 0; | |
8150 | // This function will not return. | |
8151 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
8152 | return call; | |
8153 | } | |
8154 | ||
8155 | case BUILTIN_RECOVER: | |
8156 | { | |
8157 | // The argument is set when building recover thunks. It's a | |
8158 | // boolean value which is true if we can recover a value now. | |
8159 | const Expression_list* args = this->args(); | |
c484d925 | 8160 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8161 | Expression* arg = args->front(); |
8162 | tree arg_tree = arg->get_tree(context); | |
8163 | if (arg_tree == error_mark_node) | |
8164 | return error_mark_node; | |
8165 | ||
b13c66cd | 8166 | Type *empty = |
823c7e3d | 8167 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
9f0e0513 | 8168 | tree empty_tree = type_to_tree(empty->get_backend(context->gogo())); |
e440a328 | 8169 | |
8170 | Type* nil_type = Type::make_nil_type(); | |
8171 | Expression* nil = Expression::make_nil(location); | |
8172 | tree nil_tree = nil->get_tree(context); | |
8173 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
8174 | empty, | |
8175 | nil_type, | |
8176 | nil_tree, | |
8177 | location); | |
8178 | ||
8179 | // We need to handle a deferred call to recover specially, | |
8180 | // because it changes whether it can recover a panic or not. | |
8181 | // See test7 in test/recover1.go. | |
8182 | tree call; | |
8183 | if (this->is_deferred()) | |
8184 | { | |
8185 | static tree deferred_recover_fndecl; | |
8186 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
8187 | location, | |
8188 | "__go_deferred_recover", | |
8189 | 0, | |
8190 | empty_tree); | |
8191 | } | |
8192 | else | |
8193 | { | |
8194 | static tree recover_fndecl; | |
8195 | call = Gogo::call_builtin(&recover_fndecl, | |
8196 | location, | |
8197 | "__go_recover", | |
8198 | 0, | |
8199 | empty_tree); | |
8200 | } | |
5fb82b5e | 8201 | if (call == error_mark_node) |
8202 | return error_mark_node; | |
b13c66cd | 8203 | return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree, |
8204 | arg_tree, call, empty_nil_tree); | |
e440a328 | 8205 | } |
8206 | ||
8207 | case BUILTIN_CLOSE: | |
e440a328 | 8208 | { |
8209 | const Expression_list* args = this->args(); | |
c484d925 | 8210 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8211 | Expression* arg = args->front(); |
8212 | tree arg_tree = arg->get_tree(context); | |
8213 | if (arg_tree == error_mark_node) | |
8214 | return error_mark_node; | |
0dc2f918 | 8215 | static tree close_fndecl; |
8216 | return Gogo::call_builtin(&close_fndecl, | |
8217 | location, | |
8218 | "__go_builtin_close", | |
8219 | 1, | |
8220 | void_type_node, | |
8221 | TREE_TYPE(arg_tree), | |
8222 | arg_tree); | |
e440a328 | 8223 | } |
8224 | ||
8225 | case BUILTIN_SIZEOF: | |
8226 | case BUILTIN_OFFSETOF: | |
8227 | case BUILTIN_ALIGNOF: | |
8228 | { | |
0c77715b | 8229 | Numeric_constant nc; |
8230 | unsigned long val; | |
8231 | if (!this->numeric_constant_value(&nc) | |
8232 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8233 | { |
c484d925 | 8234 | go_assert(saw_errors()); |
7f1d9abd | 8235 | return error_mark_node; |
8236 | } | |
7ba86326 | 8237 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
8238 | tree type = type_to_tree(uintptr_type->get_backend(gogo)); | |
0c77715b | 8239 | return build_int_cst(type, val); |
e440a328 | 8240 | } |
8241 | ||
8242 | case BUILTIN_COPY: | |
8243 | { | |
8244 | const Expression_list* args = this->args(); | |
c484d925 | 8245 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8246 | Expression* arg1 = args->front(); |
8247 | Expression* arg2 = args->back(); | |
8248 | ||
8249 | tree arg1_tree = arg1->get_tree(context); | |
8250 | tree arg2_tree = arg2->get_tree(context); | |
8251 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8252 | return error_mark_node; | |
8253 | ||
8254 | Type* arg1_type = arg1->type(); | |
8255 | Array_type* at = arg1_type->array_type(); | |
8256 | arg1_tree = save_expr(arg1_tree); | |
8257 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
8258 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
d8ccb1e3 | 8259 | if (arg1_val == error_mark_node || arg1_len == error_mark_node) |
8260 | return error_mark_node; | |
e440a328 | 8261 | |
8262 | Type* arg2_type = arg2->type(); | |
8263 | tree arg2_val; | |
8264 | tree arg2_len; | |
411eb89e | 8265 | if (arg2_type->is_slice_type()) |
e440a328 | 8266 | { |
8267 | at = arg2_type->array_type(); | |
8268 | arg2_tree = save_expr(arg2_tree); | |
8269 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8270 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8271 | } | |
8272 | else | |
8273 | { | |
8274 | arg2_tree = save_expr(arg2_tree); | |
8275 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8276 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8277 | } | |
d8ccb1e3 | 8278 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8279 | return error_mark_node; | |
e440a328 | 8280 | |
8281 | arg1_len = save_expr(arg1_len); | |
8282 | arg2_len = save_expr(arg2_len); | |
b13c66cd | 8283 | tree len = fold_build3_loc(location.gcc_location(), COND_EXPR, |
8284 | TREE_TYPE(arg1_len), | |
8285 | fold_build2_loc(location.gcc_location(), | |
8286 | LT_EXPR, boolean_type_node, | |
e440a328 | 8287 | arg1_len, arg2_len), |
8288 | arg1_len, arg2_len); | |
8289 | len = save_expr(len); | |
8290 | ||
8291 | Type* element_type = at->element_type(); | |
9f0e0513 | 8292 | Btype* element_btype = element_type->get_backend(gogo); |
8293 | tree element_type_tree = type_to_tree(element_btype); | |
d8ccb1e3 | 8294 | if (element_type_tree == error_mark_node) |
8295 | return error_mark_node; | |
e440a328 | 8296 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
b13c66cd | 8297 | tree bytecount = fold_convert_loc(location.gcc_location(), |
8298 | TREE_TYPE(element_size), len); | |
8299 | bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR, | |
e440a328 | 8300 | TREE_TYPE(element_size), |
8301 | bytecount, element_size); | |
b13c66cd | 8302 | bytecount = fold_convert_loc(location.gcc_location(), size_type_node, |
8303 | bytecount); | |
e440a328 | 8304 | |
b13c66cd | 8305 | arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node, |
8306 | arg1_val); | |
8307 | arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node, | |
8308 | arg2_val); | |
3991cb03 | 8309 | |
8310 | static tree copy_fndecl; | |
8311 | tree call = Gogo::call_builtin(©_fndecl, | |
8312 | location, | |
8313 | "__go_copy", | |
8314 | 3, | |
8315 | void_type_node, | |
8316 | ptr_type_node, | |
8317 | arg1_val, | |
8318 | ptr_type_node, | |
8319 | arg2_val, | |
8320 | size_type_node, | |
8321 | bytecount); | |
8322 | if (call == error_mark_node) | |
8323 | return error_mark_node; | |
e440a328 | 8324 | |
b13c66cd | 8325 | return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, |
8326 | TREE_TYPE(len), call, len); | |
e440a328 | 8327 | } |
8328 | ||
8329 | case BUILTIN_APPEND: | |
8330 | { | |
8331 | const Expression_list* args = this->args(); | |
c484d925 | 8332 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8333 | Expression* arg1 = args->front(); |
8334 | Expression* arg2 = args->back(); | |
8335 | ||
8336 | tree arg1_tree = arg1->get_tree(context); | |
8337 | tree arg2_tree = arg2->get_tree(context); | |
8338 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8339 | return error_mark_node; | |
8340 | ||
9d44fbe3 | 8341 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8342 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8343 | |
4fd4fcf4 | 8344 | tree arg2_val; |
8345 | tree arg2_len; | |
8346 | tree element_size; | |
8347 | if (arg2->type()->is_string_type() | |
60963afd | 8348 | && element_type->integer_type() != NULL |
8349 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8350 | { |
8351 | arg2_tree = save_expr(arg2_tree); | |
8352 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8353 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8354 | element_size = size_int(1); | |
8355 | } | |
8356 | else | |
8357 | { | |
8358 | arg2_tree = Expression::convert_for_assignment(context, at, | |
8359 | arg2->type(), | |
8360 | arg2_tree, | |
8361 | location); | |
8362 | if (arg2_tree == error_mark_node) | |
8363 | return error_mark_node; | |
8364 | ||
8365 | arg2_tree = save_expr(arg2_tree); | |
8366 | ||
8367 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8368 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8369 | ||
8370 | Btype* element_btype = element_type->get_backend(gogo); | |
8371 | tree element_type_tree = type_to_tree(element_btype); | |
8372 | if (element_type_tree == error_mark_node) | |
8373 | return error_mark_node; | |
8374 | element_size = TYPE_SIZE_UNIT(element_type_tree); | |
8375 | } | |
ed64c8e5 | 8376 | |
b13c66cd | 8377 | arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node, |
8378 | arg2_val); | |
8379 | arg2_len = fold_convert_loc(location.gcc_location(), size_type_node, | |
8380 | arg2_len); | |
8381 | element_size = fold_convert_loc(location.gcc_location(), size_type_node, | |
3991cb03 | 8382 | element_size); |
e440a328 | 8383 | |
4fd4fcf4 | 8384 | if (arg2_val == error_mark_node |
8385 | || arg2_len == error_mark_node | |
8386 | || element_size == error_mark_node) | |
8387 | return error_mark_node; | |
8388 | ||
e440a328 | 8389 | // We rebuild the decl each time since the slice types may |
8390 | // change. | |
8391 | tree append_fndecl = NULL_TREE; | |
8392 | return Gogo::call_builtin(&append_fndecl, | |
8393 | location, | |
8394 | "__go_append", | |
3991cb03 | 8395 | 4, |
e440a328 | 8396 | TREE_TYPE(arg1_tree), |
e440a328 | 8397 | TREE_TYPE(arg1_tree), |
8398 | arg1_tree, | |
3991cb03 | 8399 | ptr_type_node, |
8400 | arg2_val, | |
8401 | size_type_node, | |
8402 | arg2_len, | |
8403 | size_type_node, | |
8404 | element_size); | |
e440a328 | 8405 | } |
8406 | ||
8407 | case BUILTIN_REAL: | |
8408 | case BUILTIN_IMAG: | |
8409 | { | |
8410 | const Expression_list* args = this->args(); | |
c484d925 | 8411 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8412 | Expression* arg = args->front(); |
8413 | tree arg_tree = arg->get_tree(context); | |
8414 | if (arg_tree == error_mark_node) | |
8415 | return error_mark_node; | |
c484d925 | 8416 | go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); |
e440a328 | 8417 | if (this->code_ == BUILTIN_REAL) |
b13c66cd | 8418 | return fold_build1_loc(location.gcc_location(), REALPART_EXPR, |
e440a328 | 8419 | TREE_TYPE(TREE_TYPE(arg_tree)), |
8420 | arg_tree); | |
8421 | else | |
b13c66cd | 8422 | return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR, |
e440a328 | 8423 | TREE_TYPE(TREE_TYPE(arg_tree)), |
8424 | arg_tree); | |
8425 | } | |
8426 | ||
48080209 | 8427 | case BUILTIN_COMPLEX: |
e440a328 | 8428 | { |
8429 | const Expression_list* args = this->args(); | |
c484d925 | 8430 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8431 | tree r = args->front()->get_tree(context); |
8432 | tree i = args->back()->get_tree(context); | |
8433 | if (r == error_mark_node || i == error_mark_node) | |
8434 | return error_mark_node; | |
c484d925 | 8435 | go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) |
e440a328 | 8436 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); |
c484d925 | 8437 | go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); |
b13c66cd | 8438 | return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR, |
e440a328 | 8439 | build_complex_type(TREE_TYPE(r)), |
8440 | r, i); | |
8441 | } | |
8442 | ||
8443 | default: | |
c3e6f413 | 8444 | go_unreachable(); |
e440a328 | 8445 | } |
8446 | } | |
8447 | ||
8448 | // We have to support exporting a builtin call expression, because | |
8449 | // code can set a constant to the result of a builtin expression. | |
8450 | ||
8451 | void | |
8452 | Builtin_call_expression::do_export(Export* exp) const | |
8453 | { | |
0c77715b | 8454 | Numeric_constant nc; |
8455 | if (!this->numeric_constant_value(&nc)) | |
8456 | { | |
8457 | error_at(this->location(), "value is not constant"); | |
8458 | return; | |
8459 | } | |
e440a328 | 8460 | |
0c77715b | 8461 | if (nc.is_int()) |
e440a328 | 8462 | { |
0c77715b | 8463 | mpz_t val; |
8464 | nc.get_int(&val); | |
e440a328 | 8465 | Integer_expression::export_integer(exp, val); |
0c77715b | 8466 | mpz_clear(val); |
e440a328 | 8467 | } |
0c77715b | 8468 | else if (nc.is_float()) |
e440a328 | 8469 | { |
8470 | mpfr_t fval; | |
0c77715b | 8471 | nc.get_float(&fval); |
8472 | Float_expression::export_float(exp, fval); | |
e440a328 | 8473 | mpfr_clear(fval); |
8474 | } | |
0c77715b | 8475 | else if (nc.is_complex()) |
e440a328 | 8476 | { |
8477 | mpfr_t real; | |
8478 | mpfr_t imag; | |
0c77715b | 8479 | Complex_expression::export_complex(exp, real, imag); |
e440a328 | 8480 | mpfr_clear(real); |
8481 | mpfr_clear(imag); | |
8482 | } | |
0c77715b | 8483 | else |
8484 | go_unreachable(); | |
e440a328 | 8485 | |
8486 | // A trailing space lets us reliably identify the end of the number. | |
8487 | exp->write_c_string(" "); | |
8488 | } | |
8489 | ||
8490 | // Class Call_expression. | |
8491 | ||
8492 | // Traversal. | |
8493 | ||
8494 | int | |
8495 | Call_expression::do_traverse(Traverse* traverse) | |
8496 | { | |
8497 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8498 | return TRAVERSE_EXIT; | |
8499 | if (this->args_ != NULL) | |
8500 | { | |
8501 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8502 | return TRAVERSE_EXIT; | |
8503 | } | |
8504 | return TRAVERSE_CONTINUE; | |
8505 | } | |
8506 | ||
8507 | // Lower a call statement. | |
8508 | ||
8509 | Expression* | |
ceeb4318 | 8510 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
8511 | Statement_inserter* inserter, int) | |
e440a328 | 8512 | { |
b13c66cd | 8513 | Location loc = this->location(); |
09ea332d | 8514 | |
ceeb4318 | 8515 | // A type cast can look like a function call. |
e440a328 | 8516 | if (this->fn_->is_type_expression() |
8517 | && this->args_ != NULL | |
8518 | && this->args_->size() == 1) | |
8519 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 8520 | loc); |
e440a328 | 8521 | |
8522 | // Recognize a call to a builtin function. | |
8523 | Func_expression* fne = this->fn_->func_expression(); | |
8524 | if (fne != NULL | |
8525 | && fne->named_object()->is_function_declaration() | |
8526 | && fne->named_object()->func_declaration_value()->type()->is_builtin()) | |
8527 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
09ea332d | 8528 | this->is_varargs_, loc); |
e440a328 | 8529 | |
8530 | // Handle an argument which is a call to a function which returns | |
8531 | // multiple results. | |
8532 | if (this->args_ != NULL | |
8533 | && this->args_->size() == 1 | |
8534 | && this->args_->front()->call_expression() != NULL | |
8535 | && this->fn_->type()->function_type() != NULL) | |
8536 | { | |
8537 | Function_type* fntype = this->fn_->type()->function_type(); | |
8538 | size_t rc = this->args_->front()->call_expression()->result_count(); | |
8539 | if (rc > 1 | |
8540 | && fntype->parameters() != NULL | |
8541 | && (fntype->parameters()->size() == rc | |
8542 | || (fntype->is_varargs() | |
8543 | && fntype->parameters()->size() - 1 <= rc))) | |
8544 | { | |
8545 | Call_expression* call = this->args_->front()->call_expression(); | |
8546 | Expression_list* args = new Expression_list; | |
8547 | for (size_t i = 0; i < rc; ++i) | |
8548 | args->push_back(Expression::make_call_result(call, i)); | |
8549 | // We can't return a new call expression here, because this | |
42535814 | 8550 | // one may be referenced by Call_result expressions. We |
8551 | // also can't delete the old arguments, because we may still | |
8552 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8553 | this->args_ = args; |
8554 | } | |
8555 | } | |
8556 | ||
ceeb4318 | 8557 | // If this call returns multiple results, create a temporary |
8558 | // variable for each result. | |
8559 | size_t rc = this->result_count(); | |
8560 | if (rc > 1 && this->results_ == NULL) | |
8561 | { | |
8562 | std::vector<Temporary_statement*>* temps = | |
8563 | new std::vector<Temporary_statement*>; | |
8564 | temps->reserve(rc); | |
8565 | const Typed_identifier_list* results = | |
8566 | this->fn_->type()->function_type()->results(); | |
8567 | for (Typed_identifier_list::const_iterator p = results->begin(); | |
8568 | p != results->end(); | |
8569 | ++p) | |
8570 | { | |
8571 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 8572 | NULL, loc); |
ceeb4318 | 8573 | inserter->insert(temp); |
8574 | temps->push_back(temp); | |
8575 | } | |
8576 | this->results_ = temps; | |
8577 | } | |
8578 | ||
e440a328 | 8579 | // Handle a call to a varargs function by packaging up the extra |
8580 | // parameters. | |
8581 | if (this->fn_->type()->function_type() != NULL | |
8582 | && this->fn_->type()->function_type()->is_varargs()) | |
8583 | { | |
8584 | Function_type* fntype = this->fn_->type()->function_type(); | |
8585 | const Typed_identifier_list* parameters = fntype->parameters(); | |
c484d925 | 8586 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 8587 | Type* varargs_type = parameters->back().type(); |
09ea332d | 8588 | this->lower_varargs(gogo, function, inserter, varargs_type, |
8589 | parameters->size()); | |
8590 | } | |
8591 | ||
8592 | // If this is call to a method, call the method directly passing the | |
8593 | // object as the first parameter. | |
8594 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8595 | if (bme != NULL) | |
8596 | { | |
8597 | Named_object* method = bme->method(); | |
8598 | Expression* first_arg = bme->first_argument(); | |
8599 | ||
8600 | // We always pass a pointer when calling a method. | |
8601 | if (first_arg->type()->points_to() == NULL | |
8602 | && !first_arg->type()->is_error()) | |
8603 | { | |
8604 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
8605 | // We may need to create a temporary variable so that we can | |
8606 | // take the address. We can't do that here because it will | |
8607 | // mess up the order of evaluation. | |
8608 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
8609 | ue->set_create_temp(); | |
8610 | } | |
8611 | ||
8612 | // If we are calling a method which was inherited from an | |
8613 | // embedded struct, and the method did not get a stub, then the | |
8614 | // first type may be wrong. | |
8615 | Type* fatype = bme->first_argument_type(); | |
8616 | if (fatype != NULL) | |
8617 | { | |
8618 | if (fatype->points_to() == NULL) | |
8619 | fatype = Type::make_pointer_type(fatype); | |
8620 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
8621 | } | |
8622 | ||
8623 | Expression_list* new_args = new Expression_list(); | |
8624 | new_args->push_back(first_arg); | |
8625 | if (this->args_ != NULL) | |
8626 | { | |
8627 | for (Expression_list::const_iterator p = this->args_->begin(); | |
8628 | p != this->args_->end(); | |
8629 | ++p) | |
8630 | new_args->push_back(*p); | |
8631 | } | |
8632 | ||
8633 | // We have to change in place because this structure may be | |
8634 | // referenced by Call_result_expressions. We can't delete the | |
8635 | // old arguments, because we may be traversing them up in some | |
8636 | // caller. FIXME. | |
8637 | this->args_ = new_args; | |
8638 | this->fn_ = Expression::make_func_reference(method, NULL, | |
8639 | bme->location()); | |
e440a328 | 8640 | } |
8641 | ||
8642 | return this; | |
8643 | } | |
8644 | ||
8645 | // Lower a call to a varargs function. FUNCTION is the function in | |
8646 | // which the call occurs--it's not the function we are calling. | |
8647 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8648 | // PARAM_COUNT is the number of parameters of the function we are | |
8649 | // calling; the last of these parameters will be the varargs | |
8650 | // parameter. | |
8651 | ||
09ea332d | 8652 | void |
e440a328 | 8653 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 8654 | Statement_inserter* inserter, |
e440a328 | 8655 | Type* varargs_type, size_t param_count) |
8656 | { | |
8657 | if (this->varargs_are_lowered_) | |
09ea332d | 8658 | return; |
e440a328 | 8659 | |
b13c66cd | 8660 | Location loc = this->location(); |
e440a328 | 8661 | |
c484d925 | 8662 | go_assert(param_count > 0); |
411eb89e | 8663 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 8664 | |
8665 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8666 | if (arg_count < param_count - 1) | |
8667 | { | |
8668 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 8669 | return; |
e440a328 | 8670 | } |
8671 | ||
8672 | Expression_list* old_args = this->args_; | |
8673 | Expression_list* new_args = new Expression_list(); | |
8674 | bool push_empty_arg = false; | |
8675 | if (old_args == NULL || old_args->empty()) | |
8676 | { | |
c484d925 | 8677 | go_assert(param_count == 1); |
e440a328 | 8678 | push_empty_arg = true; |
8679 | } | |
8680 | else | |
8681 | { | |
8682 | Expression_list::const_iterator pa; | |
8683 | int i = 1; | |
8684 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8685 | { | |
8686 | if (static_cast<size_t>(i) == param_count) | |
8687 | break; | |
8688 | new_args->push_back(*pa); | |
8689 | } | |
8690 | ||
8691 | // We have reached the varargs parameter. | |
8692 | ||
8693 | bool issued_error = false; | |
8694 | if (pa == old_args->end()) | |
8695 | push_empty_arg = true; | |
8696 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8697 | new_args->push_back(*pa); | |
8698 | else if (this->is_varargs_) | |
8699 | { | |
a6645f74 | 8700 | if ((*pa)->type()->is_slice_type()) |
8701 | this->report_error(_("too many arguments")); | |
8702 | else | |
8703 | { | |
8704 | error_at(this->location(), | |
8705 | _("invalid use of %<...%> with non-slice")); | |
8706 | this->set_is_error(); | |
8707 | } | |
09ea332d | 8708 | return; |
e440a328 | 8709 | } |
e440a328 | 8710 | else |
8711 | { | |
8712 | Type* element_type = varargs_type->array_type()->element_type(); | |
8713 | Expression_list* vals = new Expression_list; | |
8714 | for (; pa != old_args->end(); ++pa, ++i) | |
8715 | { | |
8716 | // Check types here so that we get a better message. | |
8717 | Type* patype = (*pa)->type(); | |
b13c66cd | 8718 | Location paloc = (*pa)->location(); |
e440a328 | 8719 | if (!this->check_argument_type(i, element_type, patype, |
8720 | paloc, issued_error)) | |
8721 | continue; | |
8722 | vals->push_back(*pa); | |
8723 | } | |
8724 | Expression* val = | |
8725 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
09ea332d | 8726 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 8727 | new_args->push_back(val); |
8728 | } | |
8729 | } | |
8730 | ||
8731 | if (push_empty_arg) | |
8732 | new_args->push_back(Expression::make_nil(loc)); | |
8733 | ||
8734 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 8735 | // be referenced by Call_result expressions. FIXME. We can't |
8736 | // delete OLD_ARGS because we may have both a Call_expression and a | |
8737 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 8738 | this->args_ = new_args; |
8739 | this->varargs_are_lowered_ = true; | |
e440a328 | 8740 | } |
8741 | ||
ceeb4318 | 8742 | // Get the function type. This can return NULL in error cases. |
e440a328 | 8743 | |
8744 | Function_type* | |
8745 | Call_expression::get_function_type() const | |
8746 | { | |
8747 | return this->fn_->type()->function_type(); | |
8748 | } | |
8749 | ||
8750 | // Return the number of values which this call will return. | |
8751 | ||
8752 | size_t | |
8753 | Call_expression::result_count() const | |
8754 | { | |
8755 | const Function_type* fntype = this->get_function_type(); | |
8756 | if (fntype == NULL) | |
8757 | return 0; | |
8758 | if (fntype->results() == NULL) | |
8759 | return 0; | |
8760 | return fntype->results()->size(); | |
8761 | } | |
8762 | ||
ceeb4318 | 8763 | // Return the temporary which holds a result. |
8764 | ||
8765 | Temporary_statement* | |
8766 | Call_expression::result(size_t i) const | |
8767 | { | |
cd238b8d | 8768 | if (this->results_ == NULL || this->results_->size() <= i) |
8769 | { | |
8770 | go_assert(saw_errors()); | |
8771 | return NULL; | |
8772 | } | |
ceeb4318 | 8773 | return (*this->results_)[i]; |
8774 | } | |
8775 | ||
e440a328 | 8776 | // Return whether this is a call to the predeclared function recover. |
8777 | ||
8778 | bool | |
8779 | Call_expression::is_recover_call() const | |
8780 | { | |
8781 | return this->do_is_recover_call(); | |
8782 | } | |
8783 | ||
8784 | // Set the argument to the recover function. | |
8785 | ||
8786 | void | |
8787 | Call_expression::set_recover_arg(Expression* arg) | |
8788 | { | |
8789 | this->do_set_recover_arg(arg); | |
8790 | } | |
8791 | ||
8792 | // Virtual functions also implemented by Builtin_call_expression. | |
8793 | ||
8794 | bool | |
8795 | Call_expression::do_is_recover_call() const | |
8796 | { | |
8797 | return false; | |
8798 | } | |
8799 | ||
8800 | void | |
8801 | Call_expression::do_set_recover_arg(Expression*) | |
8802 | { | |
c3e6f413 | 8803 | go_unreachable(); |
e440a328 | 8804 | } |
8805 | ||
ceeb4318 | 8806 | // We have found an error with this call expression; return true if |
8807 | // we should report it. | |
8808 | ||
8809 | bool | |
8810 | Call_expression::issue_error() | |
8811 | { | |
8812 | if (this->issued_error_) | |
8813 | return false; | |
8814 | else | |
8815 | { | |
8816 | this->issued_error_ = true; | |
8817 | return true; | |
8818 | } | |
8819 | } | |
8820 | ||
e440a328 | 8821 | // Get the type. |
8822 | ||
8823 | Type* | |
8824 | Call_expression::do_type() | |
8825 | { | |
8826 | if (this->type_ != NULL) | |
8827 | return this->type_; | |
8828 | ||
8829 | Type* ret; | |
8830 | Function_type* fntype = this->get_function_type(); | |
8831 | if (fntype == NULL) | |
8832 | return Type::make_error_type(); | |
8833 | ||
8834 | const Typed_identifier_list* results = fntype->results(); | |
8835 | if (results == NULL) | |
8836 | ret = Type::make_void_type(); | |
8837 | else if (results->size() == 1) | |
8838 | ret = results->begin()->type(); | |
8839 | else | |
8840 | ret = Type::make_call_multiple_result_type(this); | |
8841 | ||
8842 | this->type_ = ret; | |
8843 | ||
8844 | return this->type_; | |
8845 | } | |
8846 | ||
8847 | // Determine types for a call expression. We can use the function | |
8848 | // parameter types to set the types of the arguments. | |
8849 | ||
8850 | void | |
8851 | Call_expression::do_determine_type(const Type_context*) | |
8852 | { | |
fb94b0ca | 8853 | if (!this->determining_types()) |
8854 | return; | |
8855 | ||
e440a328 | 8856 | this->fn_->determine_type_no_context(); |
8857 | Function_type* fntype = this->get_function_type(); | |
8858 | const Typed_identifier_list* parameters = NULL; | |
8859 | if (fntype != NULL) | |
8860 | parameters = fntype->parameters(); | |
8861 | if (this->args_ != NULL) | |
8862 | { | |
8863 | Typed_identifier_list::const_iterator pt; | |
8864 | if (parameters != NULL) | |
8865 | pt = parameters->begin(); | |
09ea332d | 8866 | bool first = true; |
e440a328 | 8867 | for (Expression_list::const_iterator pa = this->args_->begin(); |
8868 | pa != this->args_->end(); | |
8869 | ++pa) | |
8870 | { | |
09ea332d | 8871 | if (first) |
8872 | { | |
8873 | first = false; | |
8874 | // If this is a method, the first argument is the | |
8875 | // receiver. | |
8876 | if (fntype != NULL && fntype->is_method()) | |
8877 | { | |
8878 | Type* rtype = fntype->receiver()->type(); | |
8879 | // The receiver is always passed as a pointer. | |
8880 | if (rtype->points_to() == NULL) | |
8881 | rtype = Type::make_pointer_type(rtype); | |
8882 | Type_context subcontext(rtype, false); | |
8883 | (*pa)->determine_type(&subcontext); | |
8884 | continue; | |
8885 | } | |
8886 | } | |
8887 | ||
e440a328 | 8888 | if (parameters != NULL && pt != parameters->end()) |
8889 | { | |
8890 | Type_context subcontext(pt->type(), false); | |
8891 | (*pa)->determine_type(&subcontext); | |
8892 | ++pt; | |
8893 | } | |
8894 | else | |
8895 | (*pa)->determine_type_no_context(); | |
8896 | } | |
8897 | } | |
8898 | } | |
8899 | ||
fb94b0ca | 8900 | // Called when determining types for a Call_expression. Return true |
8901 | // if we should go ahead, false if they have already been determined. | |
8902 | ||
8903 | bool | |
8904 | Call_expression::determining_types() | |
8905 | { | |
8906 | if (this->types_are_determined_) | |
8907 | return false; | |
8908 | else | |
8909 | { | |
8910 | this->types_are_determined_ = true; | |
8911 | return true; | |
8912 | } | |
8913 | } | |
8914 | ||
e440a328 | 8915 | // Check types for parameter I. |
8916 | ||
8917 | bool | |
8918 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
8919 | const Type* argument_type, | |
b13c66cd | 8920 | Location argument_location, |
e440a328 | 8921 | bool issued_error) |
8922 | { | |
8923 | std::string reason; | |
053ee6ca | 8924 | bool ok; |
8925 | if (this->are_hidden_fields_ok_) | |
8926 | ok = Type::are_assignable_hidden_ok(parameter_type, argument_type, | |
8927 | &reason); | |
8928 | else | |
8929 | ok = Type::are_assignable(parameter_type, argument_type, &reason); | |
8930 | if (!ok) | |
e440a328 | 8931 | { |
8932 | if (!issued_error) | |
8933 | { | |
8934 | if (reason.empty()) | |
8935 | error_at(argument_location, "argument %d has incompatible type", i); | |
8936 | else | |
8937 | error_at(argument_location, | |
8938 | "argument %d has incompatible type (%s)", | |
8939 | i, reason.c_str()); | |
8940 | } | |
8941 | this->set_is_error(); | |
8942 | return false; | |
8943 | } | |
8944 | return true; | |
8945 | } | |
8946 | ||
8947 | // Check types. | |
8948 | ||
8949 | void | |
8950 | Call_expression::do_check_types(Gogo*) | |
8951 | { | |
a6645f74 | 8952 | if (this->classification() == EXPRESSION_ERROR) |
8953 | return; | |
8954 | ||
e440a328 | 8955 | Function_type* fntype = this->get_function_type(); |
8956 | if (fntype == NULL) | |
8957 | { | |
5c13bd80 | 8958 | if (!this->fn_->type()->is_error()) |
e440a328 | 8959 | this->report_error(_("expected function")); |
8960 | return; | |
8961 | } | |
8962 | ||
09ea332d | 8963 | bool is_method = fntype->is_method(); |
8964 | if (is_method) | |
e440a328 | 8965 | { |
09ea332d | 8966 | go_assert(this->args_ != NULL && !this->args_->empty()); |
8967 | Type* rtype = fntype->receiver()->type(); | |
8968 | Expression* first_arg = this->args_->front(); | |
8969 | // The language permits copying hidden fields for a method | |
8970 | // receiver. We dereference the values since receivers are | |
8971 | // always passed as pointers. | |
8972 | std::string reason; | |
8973 | if (!Type::are_assignable_hidden_ok(rtype->deref(), | |
8974 | first_arg->type()->deref(), | |
8975 | &reason)) | |
e440a328 | 8976 | { |
09ea332d | 8977 | if (reason.empty()) |
8978 | this->report_error(_("incompatible type for receiver")); | |
8979 | else | |
e440a328 | 8980 | { |
09ea332d | 8981 | error_at(this->location(), |
8982 | "incompatible type for receiver (%s)", | |
8983 | reason.c_str()); | |
8984 | this->set_is_error(); | |
e440a328 | 8985 | } |
8986 | } | |
8987 | } | |
8988 | ||
8989 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 8990 | // we don't have to worry about it here unless something is wrong. |
8991 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
8992 | { | |
8993 | if (!fntype->is_varargs()) | |
8994 | { | |
8995 | error_at(this->location(), | |
8996 | _("invalid use of %<...%> calling non-variadic function")); | |
8997 | this->set_is_error(); | |
8998 | return; | |
8999 | } | |
9000 | } | |
e440a328 | 9001 | |
9002 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9003 | if (this->args_ == NULL) | |
9004 | { | |
9005 | if (parameters != NULL && !parameters->empty()) | |
9006 | this->report_error(_("not enough arguments")); | |
9007 | } | |
9008 | else if (parameters == NULL) | |
09ea332d | 9009 | { |
9010 | if (!is_method || this->args_->size() > 1) | |
9011 | this->report_error(_("too many arguments")); | |
9012 | } | |
e440a328 | 9013 | else |
9014 | { | |
9015 | int i = 0; | |
09ea332d | 9016 | Expression_list::const_iterator pa = this->args_->begin(); |
9017 | if (is_method) | |
9018 | ++pa; | |
9019 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9020 | pt != parameters->end(); | |
9021 | ++pt, ++pa, ++i) | |
e440a328 | 9022 | { |
09ea332d | 9023 | if (pa == this->args_->end()) |
e440a328 | 9024 | { |
09ea332d | 9025 | this->report_error(_("not enough arguments")); |
e440a328 | 9026 | return; |
9027 | } | |
9028 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9029 | (*pa)->location(), false); | |
9030 | } | |
09ea332d | 9031 | if (pa != this->args_->end()) |
9032 | this->report_error(_("too many arguments")); | |
e440a328 | 9033 | } |
9034 | } | |
9035 | ||
9036 | // Return whether we have to use a temporary variable to ensure that | |
9037 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9038 | // results then it will inevitably be executed last. |
e440a328 | 9039 | |
9040 | bool | |
9041 | Call_expression::do_must_eval_in_order() const | |
9042 | { | |
ceeb4318 | 9043 | return this->result_count() > 0; |
e440a328 | 9044 | } |
9045 | ||
e440a328 | 9046 | // Get the function and the first argument to use when calling an |
9047 | // interface method. | |
9048 | ||
9049 | tree | |
9050 | Call_expression::interface_method_function( | |
9051 | Translate_context* context, | |
9052 | Interface_field_reference_expression* interface_method, | |
9053 | tree* first_arg_ptr) | |
9054 | { | |
9055 | tree expr = interface_method->expr()->get_tree(context); | |
9056 | if (expr == error_mark_node) | |
9057 | return error_mark_node; | |
9058 | expr = save_expr(expr); | |
9059 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
9060 | if (first_arg == error_mark_node) | |
9061 | return error_mark_node; | |
9062 | *first_arg_ptr = first_arg; | |
9063 | return interface_method->get_function_tree(context, expr); | |
9064 | } | |
9065 | ||
9066 | // Build the call expression. | |
9067 | ||
9068 | tree | |
9069 | Call_expression::do_get_tree(Translate_context* context) | |
9070 | { | |
9071 | if (this->tree_ != NULL_TREE) | |
9072 | return this->tree_; | |
9073 | ||
9074 | Function_type* fntype = this->get_function_type(); | |
9075 | if (fntype == NULL) | |
9076 | return error_mark_node; | |
9077 | ||
9078 | if (this->fn_->is_error_expression()) | |
9079 | return error_mark_node; | |
9080 | ||
9081 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9082 | Location location = this->location(); |
e440a328 | 9083 | |
9084 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9085 | Interface_field_reference_expression* interface_method = |
9086 | this->fn_->interface_field_reference_expression(); | |
9087 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9088 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9089 | |
9090 | int nargs; | |
9091 | tree* args; | |
9092 | if (this->args_ == NULL || this->args_->empty()) | |
9093 | { | |
09ea332d | 9094 | nargs = is_interface_method ? 1 : 0; |
e440a328 | 9095 | args = nargs == 0 ? NULL : new tree[nargs]; |
9096 | } | |
09ea332d | 9097 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9098 | { | |
9099 | // Passing a receiver parameter. | |
9100 | go_assert(!is_interface_method | |
9101 | && fntype->is_method() | |
9102 | && this->args_->size() == 1); | |
9103 | nargs = 1; | |
9104 | args = new tree[nargs]; | |
9105 | args[0] = this->args_->front()->get_tree(context); | |
9106 | } | |
e440a328 | 9107 | else |
9108 | { | |
9109 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9110 | |
9111 | nargs = this->args_->size(); | |
09ea332d | 9112 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9113 | nargs += i; |
9114 | args = new tree[nargs]; | |
9115 | ||
9116 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9117 | Expression_list::const_iterator pe = this->args_->begin(); |
9118 | if (!is_interface_method && fntype->is_method()) | |
9119 | { | |
9120 | args[i] = (*pe)->get_tree(context); | |
9121 | ++pe; | |
9122 | ++i; | |
9123 | } | |
9124 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9125 | { |
c484d925 | 9126 | go_assert(pp != params->end()); |
e440a328 | 9127 | tree arg_val = (*pe)->get_tree(context); |
9128 | args[i] = Expression::convert_for_assignment(context, | |
9129 | pp->type(), | |
9130 | (*pe)->type(), | |
9131 | arg_val, | |
9132 | location); | |
9133 | if (args[i] == error_mark_node) | |
cf609de4 | 9134 | { |
9135 | delete[] args; | |
9136 | return error_mark_node; | |
9137 | } | |
e440a328 | 9138 | } |
c484d925 | 9139 | go_assert(pp == params->end()); |
9140 | go_assert(i == nargs); | |
e440a328 | 9141 | } |
9142 | ||
9f0e0513 | 9143 | tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo)))); |
e440a328 | 9144 | if (rettype == error_mark_node) |
cf609de4 | 9145 | { |
9146 | delete[] args; | |
9147 | return error_mark_node; | |
9148 | } | |
e440a328 | 9149 | |
9150 | tree fn; | |
9151 | if (has_closure) | |
9152 | fn = func->get_tree_without_closure(gogo); | |
09ea332d | 9153 | else if (!is_interface_method) |
e440a328 | 9154 | fn = this->fn_->get_tree(context); |
e440a328 | 9155 | else |
09ea332d | 9156 | fn = this->interface_method_function(context, interface_method, &args[0]); |
e440a328 | 9157 | |
9158 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
cf609de4 | 9159 | { |
9160 | delete[] args; | |
9161 | return error_mark_node; | |
9162 | } | |
e440a328 | 9163 | |
e440a328 | 9164 | tree fndecl = fn; |
9165 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
9166 | fndecl = TREE_OPERAND(fndecl, 0); | |
9aa9e2df | 9167 | |
9168 | // Add a type cast in case the type of the function is a recursive | |
9169 | // type which refers to itself. | |
9170 | if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl)) | |
9171 | { | |
9f0e0513 | 9172 | tree fnt = type_to_tree(fntype->get_backend(gogo)); |
9aa9e2df | 9173 | if (fnt == error_mark_node) |
9174 | return error_mark_node; | |
b13c66cd | 9175 | fn = fold_convert_loc(location.gcc_location(), fnt, fn); |
9aa9e2df | 9176 | } |
9177 | ||
9178 | // This is to support builtin math functions when using 80387 math. | |
e440a328 | 9179 | tree excess_type = NULL_TREE; |
68e1881d | 9180 | if (optimize |
9181 | && TREE_CODE(fndecl) == FUNCTION_DECL | |
e440a328 | 9182 | && DECL_IS_BUILTIN(fndecl) |
9183 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
9184 | && nargs > 0 | |
9185 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
9186 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
9187 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
9188 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
9189 | { | |
9190 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
9191 | if (excess_type != NULL_TREE) | |
9192 | { | |
9193 | tree excess_fndecl = mathfn_built_in(excess_type, | |
9194 | DECL_FUNCTION_CODE(fndecl)); | |
9195 | if (excess_fndecl == NULL_TREE) | |
9196 | excess_type = NULL_TREE; | |
9197 | else | |
9198 | { | |
b13c66cd | 9199 | fn = build_fold_addr_expr_loc(location.gcc_location(), |
9200 | excess_fndecl); | |
e440a328 | 9201 | for (int i = 0; i < nargs; ++i) |
26ae0101 | 9202 | { |
9203 | if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i])) | |
9204 | || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i]))) | |
9205 | args[i] = ::convert(excess_type, args[i]); | |
9206 | } | |
e440a328 | 9207 | } |
9208 | } | |
9209 | } | |
9210 | ||
d0bcce51 | 9211 | if (func == NULL) |
9212 | fn = save_expr(fn); | |
9213 | ||
e440a328 | 9214 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, |
9215 | fn, nargs, args); | |
9216 | delete[] args; | |
9217 | ||
b13c66cd | 9218 | SET_EXPR_LOCATION(ret, location.gcc_location()); |
e440a328 | 9219 | |
9220 | if (has_closure) | |
9221 | { | |
9222 | tree closure_tree = func->closure()->get_tree(context); | |
9223 | if (closure_tree != error_mark_node) | |
9224 | CALL_EXPR_STATIC_CHAIN(ret) = closure_tree; | |
9225 | } | |
9226 | ||
9227 | // If this is a recursive function type which returns itself, as in | |
9228 | // type F func() F | |
9229 | // we have used ptr_type_node for the return type. Add a cast here | |
9230 | // to the correct type. | |
9231 | if (TREE_TYPE(ret) == ptr_type_node) | |
9232 | { | |
9f0e0513 | 9233 | tree t = type_to_tree(this->type()->base()->get_backend(gogo)); |
b13c66cd | 9234 | ret = fold_convert_loc(location.gcc_location(), t, ret); |
e440a328 | 9235 | } |
9236 | ||
9237 | if (excess_type != NULL_TREE) | |
9238 | { | |
9239 | // Calling convert here can undo our excess precision change. | |
9240 | // That may or may not be a bug in convert_to_real. | |
9241 | ret = build1(NOP_EXPR, rettype, ret); | |
9242 | } | |
9243 | ||
ceeb4318 | 9244 | if (this->results_ != NULL) |
9245 | ret = this->set_results(context, ret); | |
e440a328 | 9246 | |
d0bcce51 | 9247 | // We can't unwind the stack past a call to nil, so we need to |
9248 | // insert an explicit check so that the panic can be recovered. | |
9249 | if (func == NULL) | |
9250 | { | |
9251 | tree compare = fold_build2_loc(location.gcc_location(), EQ_EXPR, | |
9252 | boolean_type_node, fn, | |
9253 | fold_convert_loc(location.gcc_location(), | |
9254 | TREE_TYPE(fn), | |
9255 | null_pointer_node)); | |
9256 | tree crash = build3_loc(location.gcc_location(), COND_EXPR, | |
9257 | void_type_node, compare, | |
9258 | gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
9259 | location), | |
9260 | NULL_TREE); | |
9261 | ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
9262 | TREE_TYPE(ret), crash, ret); | |
9263 | } | |
9264 | ||
e440a328 | 9265 | this->tree_ = ret; |
9266 | ||
9267 | return ret; | |
9268 | } | |
9269 | ||
ceeb4318 | 9270 | // Set the result variables if this call returns multiple results. |
9271 | ||
9272 | tree | |
9273 | Call_expression::set_results(Translate_context* context, tree call_tree) | |
9274 | { | |
9275 | tree stmt_list = NULL_TREE; | |
9276 | ||
9277 | call_tree = save_expr(call_tree); | |
9278 | ||
9279 | if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE) | |
9280 | { | |
9281 | go_assert(saw_errors()); | |
9282 | return call_tree; | |
9283 | } | |
9284 | ||
b13c66cd | 9285 | Location loc = this->location(); |
ceeb4318 | 9286 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); |
9287 | size_t rc = this->result_count(); | |
9288 | for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field)) | |
9289 | { | |
9290 | go_assert(field != NULL_TREE); | |
9291 | ||
9292 | Temporary_statement* temp = this->result(i); | |
cd238b8d | 9293 | if (temp == NULL) |
9294 | { | |
9295 | go_assert(saw_errors()); | |
9296 | return error_mark_node; | |
9297 | } | |
ceeb4318 | 9298 | Temporary_reference_expression* ref = |
9299 | Expression::make_temporary_reference(temp, loc); | |
9300 | ref->set_is_lvalue(); | |
9301 | tree temp_tree = ref->get_tree(context); | |
9302 | if (temp_tree == error_mark_node) | |
423d1705 | 9303 | return error_mark_node; |
ceeb4318 | 9304 | |
b13c66cd | 9305 | tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF, |
9306 | TREE_TYPE(field), call_tree, field, NULL_TREE); | |
9307 | tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR, | |
9308 | void_type_node, temp_tree, val_tree); | |
ceeb4318 | 9309 | |
9310 | append_to_statement_list(set_tree, &stmt_list); | |
9311 | } | |
9312 | go_assert(field == NULL_TREE); | |
9313 | ||
9314 | return save_expr(stmt_list); | |
9315 | } | |
9316 | ||
d751bb78 | 9317 | // Dump ast representation for a call expressin. |
9318 | ||
9319 | void | |
9320 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9321 | { | |
9322 | this->fn_->dump_expression(ast_dump_context); | |
9323 | ast_dump_context->ostream() << "("; | |
9324 | if (args_ != NULL) | |
9325 | ast_dump_context->dump_expression_list(this->args_); | |
9326 | ||
9327 | ast_dump_context->ostream() << ") "; | |
9328 | } | |
9329 | ||
e440a328 | 9330 | // Make a call expression. |
9331 | ||
9332 | Call_expression* | |
9333 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9334 | Location location) |
e440a328 | 9335 | { |
9336 | return new Call_expression(fn, args, is_varargs, location); | |
9337 | } | |
9338 | ||
9339 | // A single result from a call which returns multiple results. | |
9340 | ||
9341 | class Call_result_expression : public Expression | |
9342 | { | |
9343 | public: | |
9344 | Call_result_expression(Call_expression* call, unsigned int index) | |
9345 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
9346 | call_(call), index_(index) | |
9347 | { } | |
9348 | ||
9349 | protected: | |
9350 | int | |
9351 | do_traverse(Traverse*); | |
9352 | ||
9353 | Type* | |
9354 | do_type(); | |
9355 | ||
9356 | void | |
9357 | do_determine_type(const Type_context*); | |
9358 | ||
9359 | void | |
9360 | do_check_types(Gogo*); | |
9361 | ||
9362 | Expression* | |
9363 | do_copy() | |
9364 | { | |
9365 | return new Call_result_expression(this->call_->call_expression(), | |
9366 | this->index_); | |
9367 | } | |
9368 | ||
9369 | bool | |
9370 | do_must_eval_in_order() const | |
9371 | { return true; } | |
9372 | ||
9373 | tree | |
9374 | do_get_tree(Translate_context*); | |
9375 | ||
d751bb78 | 9376 | void |
9377 | do_dump_expression(Ast_dump_context*) const; | |
9378 | ||
e440a328 | 9379 | private: |
9380 | // The underlying call expression. | |
9381 | Expression* call_; | |
9382 | // Which result we want. | |
9383 | unsigned int index_; | |
9384 | }; | |
9385 | ||
9386 | // Traverse a call result. | |
9387 | ||
9388 | int | |
9389 | Call_result_expression::do_traverse(Traverse* traverse) | |
9390 | { | |
9391 | if (traverse->remember_expression(this->call_)) | |
9392 | { | |
9393 | // We have already traversed the call expression. | |
9394 | return TRAVERSE_CONTINUE; | |
9395 | } | |
9396 | return Expression::traverse(&this->call_, traverse); | |
9397 | } | |
9398 | ||
9399 | // Get the type. | |
9400 | ||
9401 | Type* | |
9402 | Call_result_expression::do_type() | |
9403 | { | |
425dd051 | 9404 | if (this->classification() == EXPRESSION_ERROR) |
9405 | return Type::make_error_type(); | |
9406 | ||
e440a328 | 9407 | // THIS->CALL_ can be replaced with a temporary reference due to |
9408 | // Call_expression::do_must_eval_in_order when there is an error. | |
9409 | Call_expression* ce = this->call_->call_expression(); | |
9410 | if (ce == NULL) | |
5e85f268 | 9411 | { |
9412 | this->set_is_error(); | |
9413 | return Type::make_error_type(); | |
9414 | } | |
e440a328 | 9415 | Function_type* fntype = ce->get_function_type(); |
9416 | if (fntype == NULL) | |
5e85f268 | 9417 | { |
e37658e2 | 9418 | if (ce->issue_error()) |
99b3f06f | 9419 | { |
9420 | if (!ce->fn()->type()->is_error()) | |
9421 | this->report_error(_("expected function")); | |
9422 | } | |
5e85f268 | 9423 | this->set_is_error(); |
9424 | return Type::make_error_type(); | |
9425 | } | |
e440a328 | 9426 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9427 | if (results == NULL || results->size() < 2) |
7b8d861f | 9428 | { |
ceeb4318 | 9429 | if (ce->issue_error()) |
9430 | this->report_error(_("number of results does not match " | |
9431 | "number of values")); | |
7b8d861f | 9432 | return Type::make_error_type(); |
9433 | } | |
e440a328 | 9434 | Typed_identifier_list::const_iterator pr = results->begin(); |
9435 | for (unsigned int i = 0; i < this->index_; ++i) | |
9436 | { | |
9437 | if (pr == results->end()) | |
425dd051 | 9438 | break; |
e440a328 | 9439 | ++pr; |
9440 | } | |
9441 | if (pr == results->end()) | |
425dd051 | 9442 | { |
ceeb4318 | 9443 | if (ce->issue_error()) |
9444 | this->report_error(_("number of results does not match " | |
9445 | "number of values")); | |
425dd051 | 9446 | return Type::make_error_type(); |
9447 | } | |
e440a328 | 9448 | return pr->type(); |
9449 | } | |
9450 | ||
425dd051 | 9451 | // Check the type. Just make sure that we trigger the warning in |
9452 | // do_type. | |
e440a328 | 9453 | |
9454 | void | |
9455 | Call_result_expression::do_check_types(Gogo*) | |
9456 | { | |
425dd051 | 9457 | this->type(); |
e440a328 | 9458 | } |
9459 | ||
9460 | // Determine the type. We have nothing to do here, but the 0 result | |
9461 | // needs to pass down to the caller. | |
9462 | ||
9463 | void | |
9464 | Call_result_expression::do_determine_type(const Type_context*) | |
9465 | { | |
fb94b0ca | 9466 | this->call_->determine_type_no_context(); |
e440a328 | 9467 | } |
9468 | ||
ceeb4318 | 9469 | // Return the tree. We just refer to the temporary set by the call |
9470 | // expression. We don't do this at lowering time because it makes it | |
9471 | // hard to evaluate the call at the right time. | |
e440a328 | 9472 | |
9473 | tree | |
9474 | Call_result_expression::do_get_tree(Translate_context* context) | |
9475 | { | |
ceeb4318 | 9476 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 9477 | if (ce == NULL) |
9478 | { | |
9479 | go_assert(this->call_->is_error_expression()); | |
9480 | return error_mark_node; | |
9481 | } | |
ceeb4318 | 9482 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 9483 | if (ts == NULL) |
9484 | { | |
9485 | go_assert(saw_errors()); | |
9486 | return error_mark_node; | |
9487 | } | |
ceeb4318 | 9488 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
9489 | return ref->get_tree(context); | |
e440a328 | 9490 | } |
9491 | ||
d751bb78 | 9492 | // Dump ast representation for a call result expression. |
9493 | ||
9494 | void | |
9495 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9496 | const | |
9497 | { | |
9498 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
9499 | // (struct) and the fields are referenced instead. | |
9500 | ast_dump_context->ostream() << this->index_ << "@("; | |
9501 | ast_dump_context->dump_expression(this->call_); | |
9502 | ast_dump_context->ostream() << ")"; | |
9503 | } | |
9504 | ||
e440a328 | 9505 | // Make a reference to a single result of a call which returns |
9506 | // multiple results. | |
9507 | ||
9508 | Expression* | |
9509 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9510 | { | |
9511 | return new Call_result_expression(call, index); | |
9512 | } | |
9513 | ||
9514 | // Class Index_expression. | |
9515 | ||
9516 | // Traversal. | |
9517 | ||
9518 | int | |
9519 | Index_expression::do_traverse(Traverse* traverse) | |
9520 | { | |
9521 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9522 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9523 | || (this->end_ != NULL | |
9524 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
9525 | return TRAVERSE_EXIT; | |
9526 | return TRAVERSE_CONTINUE; | |
9527 | } | |
9528 | ||
9529 | // Lower an index expression. This converts the generic index | |
9530 | // expression into an array index, a string index, or a map index. | |
9531 | ||
9532 | Expression* | |
ceeb4318 | 9533 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 9534 | { |
b13c66cd | 9535 | Location location = this->location(); |
e440a328 | 9536 | Expression* left = this->left_; |
9537 | Expression* start = this->start_; | |
9538 | Expression* end = this->end_; | |
9539 | ||
9540 | Type* type = left->type(); | |
5c13bd80 | 9541 | if (type->is_error()) |
e440a328 | 9542 | return Expression::make_error(location); |
b0cf7ddd | 9543 | else if (left->is_type_expression()) |
9544 | { | |
9545 | error_at(location, "attempt to index type expression"); | |
9546 | return Expression::make_error(location); | |
9547 | } | |
e440a328 | 9548 | else if (type->array_type() != NULL) |
9549 | return Expression::make_array_index(left, start, end, location); | |
9550 | else if (type->points_to() != NULL | |
9551 | && type->points_to()->array_type() != NULL | |
411eb89e | 9552 | && !type->points_to()->is_slice_type()) |
e440a328 | 9553 | { |
9554 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9555 | location); | |
9556 | return Expression::make_array_index(deref, start, end, location); | |
9557 | } | |
9558 | else if (type->is_string_type()) | |
9559 | return Expression::make_string_index(left, start, end, location); | |
9560 | else if (type->map_type() != NULL) | |
9561 | { | |
9562 | if (end != NULL) | |
9563 | { | |
9564 | error_at(location, "invalid slice of map"); | |
9565 | return Expression::make_error(location); | |
9566 | } | |
6d4c2432 | 9567 | Map_index_expression* ret = Expression::make_map_index(left, start, |
9568 | location); | |
e440a328 | 9569 | if (this->is_lvalue_) |
9570 | ret->set_is_lvalue(); | |
9571 | return ret; | |
9572 | } | |
9573 | else | |
9574 | { | |
9575 | error_at(location, | |
9576 | "attempt to index object which is not array, string, or map"); | |
9577 | return Expression::make_error(location); | |
9578 | } | |
9579 | } | |
9580 | ||
d751bb78 | 9581 | // Write an indexed expression (expr[expr:expr] or expr[expr]) to a |
9582 | // dump context | |
9583 | ||
9584 | void | |
9585 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
9586 | const Expression* expr, | |
9587 | const Expression* start, | |
9588 | const Expression* end) | |
9589 | { | |
9590 | expr->dump_expression(ast_dump_context); | |
9591 | ast_dump_context->ostream() << "["; | |
9592 | start->dump_expression(ast_dump_context); | |
9593 | if (end != NULL) | |
9594 | { | |
9595 | ast_dump_context->ostream() << ":"; | |
9596 | end->dump_expression(ast_dump_context); | |
9597 | } | |
9598 | ast_dump_context->ostream() << "]"; | |
9599 | } | |
9600 | ||
9601 | // Dump ast representation for an index expression. | |
9602 | ||
9603 | void | |
9604 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9605 | const | |
9606 | { | |
9607 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
9608 | this->start_, this->end_); | |
9609 | } | |
9610 | ||
e440a328 | 9611 | // Make an index expression. |
9612 | ||
9613 | Expression* | |
9614 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
b13c66cd | 9615 | Location location) |
e440a328 | 9616 | { |
9617 | return new Index_expression(left, start, end, location); | |
9618 | } | |
9619 | ||
9620 | // An array index. This is used for both indexing and slicing. | |
9621 | ||
9622 | class Array_index_expression : public Expression | |
9623 | { | |
9624 | public: | |
9625 | Array_index_expression(Expression* array, Expression* start, | |
b13c66cd | 9626 | Expression* end, Location location) |
e440a328 | 9627 | : Expression(EXPRESSION_ARRAY_INDEX, location), |
9628 | array_(array), start_(start), end_(end), type_(NULL) | |
9629 | { } | |
9630 | ||
9631 | protected: | |
9632 | int | |
9633 | do_traverse(Traverse*); | |
9634 | ||
9635 | Type* | |
9636 | do_type(); | |
9637 | ||
9638 | void | |
9639 | do_determine_type(const Type_context*); | |
9640 | ||
9641 | void | |
9642 | do_check_types(Gogo*); | |
9643 | ||
9644 | Expression* | |
9645 | do_copy() | |
9646 | { | |
9647 | return Expression::make_array_index(this->array_->copy(), | |
9648 | this->start_->copy(), | |
9649 | (this->end_ == NULL | |
9650 | ? NULL | |
9651 | : this->end_->copy()), | |
9652 | this->location()); | |
9653 | } | |
9654 | ||
baef9f7a | 9655 | bool |
9656 | do_must_eval_subexpressions_in_order(int* skip) const | |
9657 | { | |
9658 | *skip = 1; | |
9659 | return true; | |
9660 | } | |
9661 | ||
e440a328 | 9662 | bool |
9663 | do_is_addressable() const; | |
9664 | ||
9665 | void | |
9666 | do_address_taken(bool escapes) | |
9667 | { this->array_->address_taken(escapes); } | |
9668 | ||
9669 | tree | |
9670 | do_get_tree(Translate_context*); | |
9671 | ||
d751bb78 | 9672 | void |
9673 | do_dump_expression(Ast_dump_context*) const; | |
9674 | ||
e440a328 | 9675 | private: |
9676 | // The array we are getting a value from. | |
9677 | Expression* array_; | |
9678 | // The start or only index. | |
9679 | Expression* start_; | |
9680 | // The end index of a slice. This may be NULL for a simple array | |
9681 | // index, or it may be a nil expression for the length of the array. | |
9682 | Expression* end_; | |
9683 | // The type of the expression. | |
9684 | Type* type_; | |
9685 | }; | |
9686 | ||
9687 | // Array index traversal. | |
9688 | ||
9689 | int | |
9690 | Array_index_expression::do_traverse(Traverse* traverse) | |
9691 | { | |
9692 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9693 | return TRAVERSE_EXIT; | |
9694 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9695 | return TRAVERSE_EXIT; | |
9696 | if (this->end_ != NULL) | |
9697 | { | |
9698 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9699 | return TRAVERSE_EXIT; | |
9700 | } | |
9701 | return TRAVERSE_CONTINUE; | |
9702 | } | |
9703 | ||
9704 | // Return the type of an array index. | |
9705 | ||
9706 | Type* | |
9707 | Array_index_expression::do_type() | |
9708 | { | |
9709 | if (this->type_ == NULL) | |
9710 | { | |
9711 | Array_type* type = this->array_->type()->array_type(); | |
9712 | if (type == NULL) | |
9713 | this->type_ = Type::make_error_type(); | |
9714 | else if (this->end_ == NULL) | |
9715 | this->type_ = type->element_type(); | |
411eb89e | 9716 | else if (type->is_slice_type()) |
e440a328 | 9717 | { |
9718 | // A slice of a slice has the same type as the original | |
9719 | // slice. | |
9720 | this->type_ = this->array_->type()->deref(); | |
9721 | } | |
9722 | else | |
9723 | { | |
9724 | // A slice of an array is a slice. | |
9725 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
9726 | } | |
9727 | } | |
9728 | return this->type_; | |
9729 | } | |
9730 | ||
9731 | // Set the type of an array index. | |
9732 | ||
9733 | void | |
9734 | Array_index_expression::do_determine_type(const Type_context*) | |
9735 | { | |
9736 | this->array_->determine_type_no_context(); | |
7917ad68 | 9737 | this->start_->determine_type_no_context(); |
e440a328 | 9738 | if (this->end_ != NULL) |
7917ad68 | 9739 | this->end_->determine_type_no_context(); |
e440a328 | 9740 | } |
9741 | ||
9742 | // Check types of an array index. | |
9743 | ||
9744 | void | |
9745 | Array_index_expression::do_check_types(Gogo*) | |
9746 | { | |
9747 | if (this->start_->type()->integer_type() == NULL) | |
9748 | this->report_error(_("index must be integer")); | |
9749 | if (this->end_ != NULL | |
9750 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 9751 | && !this->end_->type()->is_error() |
9752 | && !this->end_->is_nil_expression() | |
9753 | && !this->end_->is_error_expression()) | |
e440a328 | 9754 | this->report_error(_("slice end must be integer")); |
9755 | ||
9756 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 9757 | if (array_type == NULL) |
9758 | { | |
c484d925 | 9759 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 9760 | return; |
9761 | } | |
e440a328 | 9762 | |
9763 | unsigned int int_bits = | |
9764 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
9765 | ||
0c77715b | 9766 | Numeric_constant lvalnc; |
e440a328 | 9767 | mpz_t lval; |
e440a328 | 9768 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 9769 | && array_type->length()->numeric_constant_value(&lvalnc) |
9770 | && lvalnc.to_int(&lval)); | |
9771 | Numeric_constant inc; | |
e440a328 | 9772 | mpz_t ival; |
0bd5d859 | 9773 | bool ival_valid = false; |
0c77715b | 9774 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 9775 | { |
0bd5d859 | 9776 | ival_valid = true; |
e440a328 | 9777 | if (mpz_sgn(ival) < 0 |
9778 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9779 | || (lval_valid | |
9780 | && (this->end_ == NULL | |
9781 | ? mpz_cmp(ival, lval) >= 0 | |
9782 | : mpz_cmp(ival, lval) > 0))) | |
9783 | { | |
9784 | error_at(this->start_->location(), "array index out of bounds"); | |
9785 | this->set_is_error(); | |
9786 | } | |
9787 | } | |
9788 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9789 | { | |
0c77715b | 9790 | Numeric_constant enc; |
9791 | mpz_t eval; | |
9792 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 9793 | { |
0c77715b | 9794 | if (mpz_sgn(eval) < 0 |
9795 | || mpz_sizeinbase(eval, 2) >= int_bits | |
9796 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 9797 | { |
9798 | error_at(this->end_->location(), "array index out of bounds"); | |
9799 | this->set_is_error(); | |
9800 | } | |
0bd5d859 | 9801 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
9802 | this->report_error(_("inverted slice range")); | |
0c77715b | 9803 | mpz_clear(eval); |
e440a328 | 9804 | } |
9805 | } | |
0bd5d859 | 9806 | if (ival_valid) |
9807 | mpz_clear(ival); | |
0c77715b | 9808 | if (lval_valid) |
9809 | mpz_clear(lval); | |
e440a328 | 9810 | |
9811 | // A slice of an array requires an addressable array. A slice of a | |
9812 | // slice is always possible. | |
411eb89e | 9813 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 9814 | { |
9815 | if (!this->array_->is_addressable()) | |
8da39c3b | 9816 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 9817 | else |
9818 | this->array_->address_taken(true); | |
9819 | } | |
e440a328 | 9820 | } |
9821 | ||
9822 | // Return whether this expression is addressable. | |
9823 | ||
9824 | bool | |
9825 | Array_index_expression::do_is_addressable() const | |
9826 | { | |
9827 | // A slice expression is not addressable. | |
9828 | if (this->end_ != NULL) | |
9829 | return false; | |
9830 | ||
9831 | // An index into a slice is addressable. | |
411eb89e | 9832 | if (this->array_->type()->is_slice_type()) |
e440a328 | 9833 | return true; |
9834 | ||
9835 | // An index into an array is addressable if the array is | |
9836 | // addressable. | |
9837 | return this->array_->is_addressable(); | |
9838 | } | |
9839 | ||
9840 | // Get a tree for an array index. | |
9841 | ||
9842 | tree | |
9843 | Array_index_expression::do_get_tree(Translate_context* context) | |
9844 | { | |
9845 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9846 | Location loc = this->location(); |
e440a328 | 9847 | |
9848 | Array_type* array_type = this->array_->type()->array_type(); | |
d8cd8e2d | 9849 | if (array_type == NULL) |
9850 | { | |
c484d925 | 9851 | go_assert(this->array_->type()->is_error()); |
d8cd8e2d | 9852 | return error_mark_node; |
9853 | } | |
e440a328 | 9854 | |
9f0e0513 | 9855 | tree type_tree = type_to_tree(array_type->get_backend(gogo)); |
c65212a0 | 9856 | if (type_tree == error_mark_node) |
9857 | return error_mark_node; | |
e440a328 | 9858 | |
9859 | tree array_tree = this->array_->get_tree(context); | |
9860 | if (array_tree == error_mark_node) | |
9861 | return error_mark_node; | |
9862 | ||
9863 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
9864 | array_tree = save_expr(array_tree); | |
a04bfdfc | 9865 | |
9866 | tree length_tree = NULL_TREE; | |
9867 | if (this->end_ == NULL || this->end_->is_nil_expression()) | |
9868 | { | |
9869 | length_tree = array_type->length_tree(gogo, array_tree); | |
9870 | if (length_tree == error_mark_node) | |
9871 | return error_mark_node; | |
9872 | length_tree = save_expr(length_tree); | |
9873 | } | |
9874 | ||
9875 | tree capacity_tree = NULL_TREE; | |
9876 | if (this->end_ != NULL) | |
9877 | { | |
9878 | capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
9879 | if (capacity_tree == error_mark_node) | |
9880 | return error_mark_node; | |
9881 | capacity_tree = save_expr(capacity_tree); | |
9882 | } | |
9883 | ||
9884 | tree length_type = (length_tree != NULL_TREE | |
9885 | ? TREE_TYPE(length_tree) | |
9886 | : TREE_TYPE(capacity_tree)); | |
e440a328 | 9887 | |
9888 | tree bad_index = boolean_false_node; | |
9889 | ||
9890 | tree start_tree = this->start_->get_tree(context); | |
9891 | if (start_tree == error_mark_node) | |
9892 | return error_mark_node; | |
9893 | if (!DECL_P(start_tree)) | |
9894 | start_tree = save_expr(start_tree); | |
9895 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9896 | start_tree = convert_to_integer(length_type, start_tree); | |
9897 | ||
9898 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9899 | loc); | |
9900 | ||
b13c66cd | 9901 | start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree); |
9902 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, | |
9903 | boolean_type_node, bad_index, | |
9904 | fold_build2_loc(loc.gcc_location(), | |
e440a328 | 9905 | (this->end_ == NULL |
9906 | ? GE_EXPR | |
9907 | : GT_EXPR), | |
9908 | boolean_type_node, start_tree, | |
a04bfdfc | 9909 | (this->end_ == NULL |
9910 | ? length_tree | |
9911 | : capacity_tree))); | |
e440a328 | 9912 | |
9913 | int code = (array_type->length() != NULL | |
9914 | ? (this->end_ == NULL | |
9915 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
9916 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
9917 | : (this->end_ == NULL | |
9918 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
9919 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
1b1f2abf | 9920 | tree crash = gogo->runtime_error(code, loc); |
e440a328 | 9921 | |
9922 | if (this->end_ == NULL) | |
9923 | { | |
9924 | // Simple array indexing. This has to return an l-value, so | |
9925 | // wrap the index check into START_TREE. | |
9926 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
9927 | build3(COND_EXPR, void_type_node, | |
9928 | bad_index, crash, NULL_TREE), | |
9929 | start_tree); | |
b13c66cd | 9930 | start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree); |
e440a328 | 9931 | |
9932 | if (array_type->length() != NULL) | |
9933 | { | |
9934 | // Fixed array. | |
9935 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
9936 | start_tree, NULL_TREE, NULL_TREE); | |
9937 | } | |
9938 | else | |
9939 | { | |
9940 | // Open array. | |
9941 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9f0e0513 | 9942 | Type* element_type = array_type->element_type(); |
9943 | Btype* belement_type = element_type->get_backend(gogo); | |
9944 | tree element_type_tree = type_to_tree(belement_type); | |
c65212a0 | 9945 | if (element_type_tree == error_mark_node) |
9946 | return error_mark_node; | |
e440a328 | 9947 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
b13c66cd | 9948 | tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype, |
e440a328 | 9949 | start_tree, element_size); |
b13c66cd | 9950 | tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
e440a328 | 9951 | TREE_TYPE(values), values, offset); |
9952 | return build_fold_indirect_ref(ptr); | |
9953 | } | |
9954 | } | |
9955 | ||
9956 | // Array slice. | |
9957 | ||
e440a328 | 9958 | tree end_tree; |
9959 | if (this->end_->is_nil_expression()) | |
9960 | end_tree = length_tree; | |
9961 | else | |
9962 | { | |
9963 | end_tree = this->end_->get_tree(context); | |
9964 | if (end_tree == error_mark_node) | |
9965 | return error_mark_node; | |
9966 | if (!DECL_P(end_tree)) | |
9967 | end_tree = save_expr(end_tree); | |
9968 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9969 | end_tree = convert_to_integer(length_type, end_tree); | |
9970 | ||
9971 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
9972 | loc); | |
9973 | ||
b13c66cd | 9974 | end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree); |
e440a328 | 9975 | |
b13c66cd | 9976 | tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
9977 | boolean_type_node, | |
9978 | fold_build2_loc(loc.gcc_location(), | |
9979 | LT_EXPR, boolean_type_node, | |
e440a328 | 9980 | end_tree, start_tree), |
b13c66cd | 9981 | fold_build2_loc(loc.gcc_location(), |
9982 | GT_EXPR, boolean_type_node, | |
e440a328 | 9983 | end_tree, capacity_tree)); |
b13c66cd | 9984 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
9985 | boolean_type_node, bad_index, bad_end); | |
e440a328 | 9986 | } |
9987 | ||
9f0e0513 | 9988 | Type* element_type = array_type->element_type(); |
9989 | tree element_type_tree = type_to_tree(element_type->get_backend(gogo)); | |
c65212a0 | 9990 | if (element_type_tree == error_mark_node) |
9991 | return error_mark_node; | |
e440a328 | 9992 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9993 | ||
b13c66cd | 9994 | tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype, |
9995 | fold_convert_loc(loc.gcc_location(), sizetype, | |
9996 | start_tree), | |
e440a328 | 9997 | element_size); |
9998 | ||
9999 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
c65212a0 | 10000 | if (value_pointer == error_mark_node) |
10001 | return error_mark_node; | |
e440a328 | 10002 | |
b13c66cd | 10003 | value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
e440a328 | 10004 | TREE_TYPE(value_pointer), |
10005 | value_pointer, offset); | |
10006 | ||
b13c66cd | 10007 | tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR, |
10008 | length_type, end_tree, start_tree); | |
e440a328 | 10009 | |
b13c66cd | 10010 | tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR, |
10011 | length_type, capacity_tree, | |
10012 | start_tree); | |
e440a328 | 10013 | |
9f0e0513 | 10014 | tree struct_tree = type_to_tree(this->type()->get_backend(gogo)); |
c484d925 | 10015 | go_assert(TREE_CODE(struct_tree) == RECORD_TYPE); |
e440a328 | 10016 | |
95f84544 | 10017 | vec<constructor_elt, va_gc> *init; |
10018 | vec_alloc (init, 3); | |
e440a328 | 10019 | |
e82e4eb5 | 10020 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 10021 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 10022 | tree field = TYPE_FIELDS(struct_tree); |
c484d925 | 10023 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 10024 | elt->index = field; |
10025 | elt->value = value_pointer; | |
10026 | ||
95f84544 | 10027 | elt = init->quick_push(empty); |
e440a328 | 10028 | field = DECL_CHAIN(field); |
c484d925 | 10029 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 10030 | elt->index = field; |
b13c66cd | 10031 | elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field), |
10032 | result_length_tree); | |
e440a328 | 10033 | |
95f84544 | 10034 | elt = init->quick_push(empty); |
e440a328 | 10035 | field = DECL_CHAIN(field); |
c484d925 | 10036 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); |
e440a328 | 10037 | elt->index = field; |
b13c66cd | 10038 | elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field), |
10039 | result_capacity_tree); | |
e440a328 | 10040 | |
10041 | tree constructor = build_constructor(struct_tree, init); | |
10042 | ||
10043 | if (TREE_CONSTANT(value_pointer) | |
10044 | && TREE_CONSTANT(result_length_tree) | |
10045 | && TREE_CONSTANT(result_capacity_tree)) | |
10046 | TREE_CONSTANT(constructor) = 1; | |
10047 | ||
b13c66cd | 10048 | return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, |
10049 | TREE_TYPE(constructor), | |
e440a328 | 10050 | build3(COND_EXPR, void_type_node, |
10051 | bad_index, crash, NULL_TREE), | |
10052 | constructor); | |
10053 | } | |
10054 | ||
d751bb78 | 10055 | // Dump ast representation for an array index expression. |
10056 | ||
10057 | void | |
10058 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10059 | const | |
10060 | { | |
10061 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
10062 | this->start_, this->end_); | |
10063 | } | |
10064 | ||
e440a328 | 10065 | // Make an array index expression. END may be NULL. |
10066 | ||
10067 | Expression* | |
10068 | Expression::make_array_index(Expression* array, Expression* start, | |
b13c66cd | 10069 | Expression* end, Location location) |
e440a328 | 10070 | { |
e440a328 | 10071 | return new Array_index_expression(array, start, end, location); |
10072 | } | |
10073 | ||
10074 | // A string index. This is used for both indexing and slicing. | |
10075 | ||
10076 | class String_index_expression : public Expression | |
10077 | { | |
10078 | public: | |
10079 | String_index_expression(Expression* string, Expression* start, | |
b13c66cd | 10080 | Expression* end, Location location) |
e440a328 | 10081 | : Expression(EXPRESSION_STRING_INDEX, location), |
10082 | string_(string), start_(start), end_(end) | |
10083 | { } | |
10084 | ||
10085 | protected: | |
10086 | int | |
10087 | do_traverse(Traverse*); | |
10088 | ||
10089 | Type* | |
10090 | do_type(); | |
10091 | ||
10092 | void | |
10093 | do_determine_type(const Type_context*); | |
10094 | ||
10095 | void | |
10096 | do_check_types(Gogo*); | |
10097 | ||
10098 | Expression* | |
10099 | do_copy() | |
10100 | { | |
10101 | return Expression::make_string_index(this->string_->copy(), | |
10102 | this->start_->copy(), | |
10103 | (this->end_ == NULL | |
10104 | ? NULL | |
10105 | : this->end_->copy()), | |
10106 | this->location()); | |
10107 | } | |
10108 | ||
baef9f7a | 10109 | bool |
10110 | do_must_eval_subexpressions_in_order(int* skip) const | |
10111 | { | |
10112 | *skip = 1; | |
10113 | return true; | |
10114 | } | |
10115 | ||
e440a328 | 10116 | tree |
10117 | do_get_tree(Translate_context*); | |
10118 | ||
d751bb78 | 10119 | void |
10120 | do_dump_expression(Ast_dump_context*) const; | |
10121 | ||
e440a328 | 10122 | private: |
10123 | // The string we are getting a value from. | |
10124 | Expression* string_; | |
10125 | // The start or only index. | |
10126 | Expression* start_; | |
10127 | // The end index of a slice. This may be NULL for a single index, | |
10128 | // or it may be a nil expression for the length of the string. | |
10129 | Expression* end_; | |
10130 | }; | |
10131 | ||
10132 | // String index traversal. | |
10133 | ||
10134 | int | |
10135 | String_index_expression::do_traverse(Traverse* traverse) | |
10136 | { | |
10137 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10138 | return TRAVERSE_EXIT; | |
10139 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10140 | return TRAVERSE_EXIT; | |
10141 | if (this->end_ != NULL) | |
10142 | { | |
10143 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10144 | return TRAVERSE_EXIT; | |
10145 | } | |
10146 | return TRAVERSE_CONTINUE; | |
10147 | } | |
10148 | ||
10149 | // Return the type of a string index. | |
10150 | ||
10151 | Type* | |
10152 | String_index_expression::do_type() | |
10153 | { | |
10154 | if (this->end_ == NULL) | |
10155 | return Type::lookup_integer_type("uint8"); | |
10156 | else | |
7672d35f | 10157 | return this->string_->type(); |
e440a328 | 10158 | } |
10159 | ||
10160 | // Determine the type of a string index. | |
10161 | ||
10162 | void | |
10163 | String_index_expression::do_determine_type(const Type_context*) | |
10164 | { | |
10165 | this->string_->determine_type_no_context(); | |
93000773 | 10166 | this->start_->determine_type_no_context(); |
e440a328 | 10167 | if (this->end_ != NULL) |
93000773 | 10168 | this->end_->determine_type_no_context(); |
e440a328 | 10169 | } |
10170 | ||
10171 | // Check types of a string index. | |
10172 | ||
10173 | void | |
10174 | String_index_expression::do_check_types(Gogo*) | |
10175 | { | |
10176 | if (this->start_->type()->integer_type() == NULL) | |
10177 | this->report_error(_("index must be integer")); | |
10178 | if (this->end_ != NULL | |
10179 | && this->end_->type()->integer_type() == NULL | |
10180 | && !this->end_->is_nil_expression()) | |
10181 | this->report_error(_("slice end must be integer")); | |
10182 | ||
10183 | std::string sval; | |
10184 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10185 | ||
0c77715b | 10186 | Numeric_constant inc; |
e440a328 | 10187 | mpz_t ival; |
0bd5d859 | 10188 | bool ival_valid = false; |
0c77715b | 10189 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10190 | { |
0bd5d859 | 10191 | ival_valid = true; |
e440a328 | 10192 | if (mpz_sgn(ival) < 0 |
10193 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
10194 | { | |
10195 | error_at(this->start_->location(), "string index out of bounds"); | |
10196 | this->set_is_error(); | |
10197 | } | |
10198 | } | |
10199 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10200 | { | |
0c77715b | 10201 | Numeric_constant enc; |
10202 | mpz_t eval; | |
10203 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10204 | { |
0c77715b | 10205 | if (mpz_sgn(eval) < 0 |
10206 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10207 | { |
10208 | error_at(this->end_->location(), "string index out of bounds"); | |
10209 | this->set_is_error(); | |
10210 | } | |
0bd5d859 | 10211 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10212 | this->report_error(_("inverted slice range")); | |
0c77715b | 10213 | mpz_clear(eval); |
e440a328 | 10214 | } |
10215 | } | |
0bd5d859 | 10216 | if (ival_valid) |
10217 | mpz_clear(ival); | |
e440a328 | 10218 | } |
10219 | ||
10220 | // Get a tree for a string index. | |
10221 | ||
10222 | tree | |
10223 | String_index_expression::do_get_tree(Translate_context* context) | |
10224 | { | |
b13c66cd | 10225 | Location loc = this->location(); |
e440a328 | 10226 | |
10227 | tree string_tree = this->string_->get_tree(context); | |
10228 | if (string_tree == error_mark_node) | |
10229 | return error_mark_node; | |
10230 | ||
10231 | if (this->string_->type()->points_to() != NULL) | |
10232 | string_tree = build_fold_indirect_ref(string_tree); | |
10233 | if (!DECL_P(string_tree)) | |
10234 | string_tree = save_expr(string_tree); | |
10235 | tree string_type = TREE_TYPE(string_tree); | |
10236 | ||
10237 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
10238 | length_tree = save_expr(length_tree); | |
1b1f2abf | 10239 | |
10240 | Type* int_type = Type::lookup_integer_type("int"); | |
10241 | tree length_type = type_to_tree(int_type->get_backend(context->gogo())); | |
e440a328 | 10242 | |
10243 | tree bad_index = boolean_false_node; | |
10244 | ||
10245 | tree start_tree = this->start_->get_tree(context); | |
10246 | if (start_tree == error_mark_node) | |
10247 | return error_mark_node; | |
10248 | if (!DECL_P(start_tree)) | |
10249 | start_tree = save_expr(start_tree); | |
10250 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
10251 | start_tree = convert_to_integer(length_type, start_tree); | |
10252 | ||
10253 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
10254 | loc); | |
10255 | ||
b13c66cd | 10256 | start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree); |
e440a328 | 10257 | |
10258 | int code = (this->end_ == NULL | |
10259 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10260 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
1b1f2abf | 10261 | tree crash = context->gogo()->runtime_error(code, loc); |
e440a328 | 10262 | |
10263 | if (this->end_ == NULL) | |
10264 | { | |
b13c66cd | 10265 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
10266 | boolean_type_node, bad_index, | |
10267 | fold_build2_loc(loc.gcc_location(), GE_EXPR, | |
e440a328 | 10268 | boolean_type_node, |
10269 | start_tree, length_tree)); | |
10270 | ||
10271 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
b13c66cd | 10272 | tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
10273 | TREE_TYPE(bytes_tree), | |
e440a328 | 10274 | bytes_tree, |
b13c66cd | 10275 | fold_convert_loc(loc.gcc_location(), sizetype, |
10276 | start_tree)); | |
10277 | tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr); | |
e440a328 | 10278 | |
10279 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
10280 | build3(COND_EXPR, void_type_node, | |
10281 | bad_index, crash, NULL_TREE), | |
10282 | index); | |
10283 | } | |
10284 | else | |
10285 | { | |
10286 | tree end_tree; | |
10287 | if (this->end_->is_nil_expression()) | |
10288 | end_tree = build_int_cst(length_type, -1); | |
10289 | else | |
10290 | { | |
10291 | end_tree = this->end_->get_tree(context); | |
10292 | if (end_tree == error_mark_node) | |
10293 | return error_mark_node; | |
10294 | if (!DECL_P(end_tree)) | |
10295 | end_tree = save_expr(end_tree); | |
10296 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
10297 | end_tree = convert_to_integer(length_type, end_tree); | |
10298 | ||
10299 | bad_index = Expression::check_bounds(end_tree, length_type, | |
10300 | bad_index, loc); | |
10301 | ||
b13c66cd | 10302 | end_tree = fold_convert_loc(loc.gcc_location(), length_type, |
10303 | end_tree); | |
e440a328 | 10304 | } |
10305 | ||
10306 | static tree strslice_fndecl; | |
10307 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
10308 | loc, | |
10309 | "__go_string_slice", | |
10310 | 3, | |
10311 | string_type, | |
10312 | string_type, | |
10313 | string_tree, | |
10314 | length_type, | |
10315 | start_tree, | |
10316 | length_type, | |
10317 | end_tree); | |
5fb82b5e | 10318 | if (ret == error_mark_node) |
10319 | return error_mark_node; | |
e440a328 | 10320 | // This will panic if the bounds are out of range for the |
10321 | // string. | |
10322 | TREE_NOTHROW(strslice_fndecl) = 0; | |
10323 | ||
10324 | if (bad_index == boolean_false_node) | |
10325 | return ret; | |
10326 | else | |
10327 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
10328 | build3(COND_EXPR, void_type_node, | |
10329 | bad_index, crash, NULL_TREE), | |
10330 | ret); | |
10331 | } | |
10332 | } | |
10333 | ||
d751bb78 | 10334 | // Dump ast representation for a string index expression. |
10335 | ||
10336 | void | |
10337 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10338 | const | |
10339 | { | |
10340 | Index_expression::dump_index_expression(ast_dump_context, this->string_, | |
10341 | this->start_, this->end_); | |
10342 | } | |
10343 | ||
e440a328 | 10344 | // Make a string index expression. END may be NULL. |
10345 | ||
10346 | Expression* | |
10347 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10348 | Expression* end, Location location) |
e440a328 | 10349 | { |
10350 | return new String_index_expression(string, start, end, location); | |
10351 | } | |
10352 | ||
10353 | // Class Map_index. | |
10354 | ||
10355 | // Get the type of the map. | |
10356 | ||
10357 | Map_type* | |
10358 | Map_index_expression::get_map_type() const | |
10359 | { | |
10360 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 10361 | if (mt == NULL) |
c484d925 | 10362 | go_assert(saw_errors()); |
e440a328 | 10363 | return mt; |
10364 | } | |
10365 | ||
10366 | // Map index traversal. | |
10367 | ||
10368 | int | |
10369 | Map_index_expression::do_traverse(Traverse* traverse) | |
10370 | { | |
10371 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
10372 | return TRAVERSE_EXIT; | |
10373 | return Expression::traverse(&this->index_, traverse); | |
10374 | } | |
10375 | ||
10376 | // Return the type of a map index. | |
10377 | ||
10378 | Type* | |
10379 | Map_index_expression::do_type() | |
10380 | { | |
c7524fae | 10381 | Map_type* mt = this->get_map_type(); |
10382 | if (mt == NULL) | |
10383 | return Type::make_error_type(); | |
10384 | Type* type = mt->val_type(); | |
e440a328 | 10385 | // If this map index is in a tuple assignment, we actually return a |
10386 | // pointer to the value type. Tuple_map_assignment_statement is | |
10387 | // responsible for handling this correctly. We need to get the type | |
10388 | // right in case this gets assigned to a temporary variable. | |
10389 | if (this->is_in_tuple_assignment_) | |
10390 | type = Type::make_pointer_type(type); | |
10391 | return type; | |
10392 | } | |
10393 | ||
10394 | // Fix the type of a map index. | |
10395 | ||
10396 | void | |
10397 | Map_index_expression::do_determine_type(const Type_context*) | |
10398 | { | |
10399 | this->map_->determine_type_no_context(); | |
c7524fae | 10400 | Map_type* mt = this->get_map_type(); |
10401 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
10402 | Type_context subcontext(key_type, false); | |
e440a328 | 10403 | this->index_->determine_type(&subcontext); |
10404 | } | |
10405 | ||
10406 | // Check types of a map index. | |
10407 | ||
10408 | void | |
10409 | Map_index_expression::do_check_types(Gogo*) | |
10410 | { | |
10411 | std::string reason; | |
c7524fae | 10412 | Map_type* mt = this->get_map_type(); |
10413 | if (mt == NULL) | |
10414 | return; | |
10415 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 10416 | { |
10417 | if (reason.empty()) | |
10418 | this->report_error(_("incompatible type for map index")); | |
10419 | else | |
10420 | { | |
10421 | error_at(this->location(), "incompatible type for map index (%s)", | |
10422 | reason.c_str()); | |
10423 | this->set_is_error(); | |
10424 | } | |
10425 | } | |
10426 | } | |
10427 | ||
10428 | // Get a tree for a map index. | |
10429 | ||
10430 | tree | |
10431 | Map_index_expression::do_get_tree(Translate_context* context) | |
10432 | { | |
10433 | Map_type* type = this->get_map_type(); | |
c7524fae | 10434 | if (type == NULL) |
10435 | return error_mark_node; | |
e440a328 | 10436 | |
10437 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
10438 | if (valptr == error_mark_node) | |
10439 | return error_mark_node; | |
10440 | valptr = save_expr(valptr); | |
10441 | ||
10442 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
10443 | ||
10444 | if (this->is_lvalue_) | |
10445 | return build_fold_indirect_ref(valptr); | |
10446 | else if (this->is_in_tuple_assignment_) | |
10447 | { | |
10448 | // Tuple_map_assignment_statement is responsible for using this | |
10449 | // appropriately. | |
10450 | return valptr; | |
10451 | } | |
10452 | else | |
10453 | { | |
63697958 | 10454 | Gogo* gogo = context->gogo(); |
10455 | Btype* val_btype = type->val_type()->get_backend(gogo); | |
10456 | Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); | |
e440a328 | 10457 | return fold_build3(COND_EXPR, val_type_tree, |
10458 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
10459 | fold_convert(TREE_TYPE(valptr), | |
10460 | null_pointer_node)), | |
63697958 | 10461 | expr_to_tree(val_zero), |
e440a328 | 10462 | build_fold_indirect_ref(valptr)); |
10463 | } | |
10464 | } | |
10465 | ||
10466 | // Get a tree for the map index. This returns a tree which evaluates | |
10467 | // to a pointer to a value. The pointer will be NULL if the key is | |
10468 | // not in the map. | |
10469 | ||
10470 | tree | |
10471 | Map_index_expression::get_value_pointer(Translate_context* context, | |
10472 | bool insert) | |
10473 | { | |
10474 | Map_type* type = this->get_map_type(); | |
c7524fae | 10475 | if (type == NULL) |
10476 | return error_mark_node; | |
e440a328 | 10477 | |
10478 | tree map_tree = this->map_->get_tree(context); | |
10479 | tree index_tree = this->index_->get_tree(context); | |
10480 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
10481 | this->index_->type(), | |
10482 | index_tree, | |
10483 | this->location()); | |
10484 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
10485 | return error_mark_node; | |
10486 | ||
10487 | if (this->map_->type()->points_to() != NULL) | |
10488 | map_tree = build_fold_indirect_ref(map_tree); | |
10489 | ||
10490 | // We need to pass in a pointer to the key, so stuff it into a | |
10491 | // variable. | |
746d2e73 | 10492 | tree tmp; |
10493 | tree make_tmp; | |
10494 | if (current_function_decl != NULL) | |
10495 | { | |
10496 | tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
10497 | DECL_IGNORED_P(tmp) = 0; | |
10498 | DECL_INITIAL(tmp) = index_tree; | |
10499 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
10500 | TREE_ADDRESSABLE(tmp) = 1; | |
10501 | } | |
10502 | else | |
10503 | { | |
b13c66cd | 10504 | tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
10505 | create_tmp_var_name("M"), | |
746d2e73 | 10506 | TREE_TYPE(index_tree)); |
10507 | DECL_EXTERNAL(tmp) = 0; | |
10508 | TREE_PUBLIC(tmp) = 0; | |
10509 | TREE_STATIC(tmp) = 1; | |
10510 | DECL_ARTIFICIAL(tmp) = 1; | |
10511 | if (!TREE_CONSTANT(index_tree)) | |
b13c66cd | 10512 | make_tmp = fold_build2_loc(this->location().gcc_location(), |
10513 | INIT_EXPR, void_type_node, | |
746d2e73 | 10514 | tmp, index_tree); |
10515 | else | |
10516 | { | |
10517 | TREE_READONLY(tmp) = 1; | |
10518 | TREE_CONSTANT(tmp) = 1; | |
10519 | DECL_INITIAL(tmp) = index_tree; | |
10520 | make_tmp = NULL_TREE; | |
10521 | } | |
10522 | rest_of_decl_compilation(tmp, 1, 0); | |
10523 | } | |
b13c66cd | 10524 | tree tmpref = |
10525 | fold_convert_loc(this->location().gcc_location(), const_ptr_type_node, | |
10526 | build_fold_addr_expr_loc(this->location().gcc_location(), | |
10527 | tmp)); | |
e440a328 | 10528 | |
10529 | static tree map_index_fndecl; | |
10530 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
10531 | this->location(), | |
10532 | "__go_map_index", | |
10533 | 3, | |
10534 | const_ptr_type_node, | |
10535 | TREE_TYPE(map_tree), | |
10536 | map_tree, | |
10537 | const_ptr_type_node, | |
10538 | tmpref, | |
10539 | boolean_type_node, | |
10540 | (insert | |
10541 | ? boolean_true_node | |
10542 | : boolean_false_node)); | |
5fb82b5e | 10543 | if (call == error_mark_node) |
10544 | return error_mark_node; | |
e440a328 | 10545 | // This can panic on a map of interface type if the interface holds |
10546 | // an uncomparable or unhashable type. | |
10547 | TREE_NOTHROW(map_index_fndecl) = 0; | |
10548 | ||
9f0e0513 | 10549 | Type* val_type = type->val_type(); |
10550 | tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo())); | |
e440a328 | 10551 | if (val_type_tree == error_mark_node) |
10552 | return error_mark_node; | |
10553 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
10554 | ||
b13c66cd | 10555 | tree ret = fold_convert_loc(this->location().gcc_location(), |
10556 | ptr_val_type_tree, call); | |
746d2e73 | 10557 | if (make_tmp != NULL_TREE) |
10558 | ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret); | |
10559 | return ret; | |
e440a328 | 10560 | } |
10561 | ||
d751bb78 | 10562 | // Dump ast representation for a map index expression |
10563 | ||
10564 | void | |
10565 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10566 | const | |
10567 | { | |
10568 | Index_expression::dump_index_expression(ast_dump_context, | |
10569 | this->map_, this->index_, NULL); | |
10570 | } | |
10571 | ||
e440a328 | 10572 | // Make a map index expression. |
10573 | ||
10574 | Map_index_expression* | |
10575 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 10576 | Location location) |
e440a328 | 10577 | { |
10578 | return new Map_index_expression(map, index, location); | |
10579 | } | |
10580 | ||
10581 | // Class Field_reference_expression. | |
10582 | ||
149eabc5 | 10583 | // Lower a field reference expression. There is nothing to lower, but |
10584 | // this is where we generate the tracking information for fields with | |
10585 | // the magic go:"track" tag. | |
10586 | ||
10587 | Expression* | |
10588 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
10589 | Statement_inserter* inserter, int) | |
10590 | { | |
10591 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
10592 | if (struct_type == NULL) | |
10593 | { | |
10594 | // Error will be reported elsewhere. | |
10595 | return this; | |
10596 | } | |
10597 | const Struct_field* field = struct_type->field(this->field_index_); | |
10598 | if (field == NULL) | |
10599 | return this; | |
10600 | if (!field->has_tag()) | |
10601 | return this; | |
10602 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
10603 | return this; | |
10604 | ||
10605 | // We have found a reference to a tracked field. Build a call to | |
10606 | // the runtime function __go_fieldtrack with a string that describes | |
10607 | // the field. FIXME: We should only call this once per referenced | |
10608 | // field per function, not once for each reference to the field. | |
10609 | ||
10610 | if (this->called_fieldtrack_) | |
10611 | return this; | |
10612 | this->called_fieldtrack_ = true; | |
10613 | ||
10614 | Location loc = this->location(); | |
10615 | ||
10616 | std::string s = "fieldtrack \""; | |
10617 | Named_type* nt = this->expr_->type()->named_type(); | |
10618 | if (nt == NULL || nt->named_object()->package() == NULL) | |
10619 | s.append(gogo->pkgpath()); | |
10620 | else | |
10621 | s.append(nt->named_object()->package()->pkgpath()); | |
10622 | s.push_back('.'); | |
10623 | if (nt != NULL) | |
5c29ad36 | 10624 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 10625 | s.push_back('.'); |
10626 | s.append(field->field_name()); | |
10627 | s.push_back('"'); | |
10628 | ||
10629 | // We can't use a string here, because internally a string holds a | |
10630 | // pointer to the actual bytes; when the linker garbage collects the | |
10631 | // string, it won't garbage collect the bytes. So we use a | |
10632 | // [...]byte. | |
10633 | ||
10634 | mpz_t val; | |
10635 | mpz_init_set_ui(val, s.length()); | |
10636 | Expression* length_expr = Expression::make_integer(&val, NULL, loc); | |
10637 | mpz_clear(val); | |
10638 | ||
10639 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
10640 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
10641 | ||
10642 | Expression_list* bytes = new Expression_list(); | |
10643 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
10644 | { | |
10645 | mpz_init_set_ui(val, *p); | |
10646 | Expression* byte = Expression::make_integer(&val, NULL, loc); | |
10647 | mpz_clear(val); | |
10648 | bytes->push_back(byte); | |
10649 | } | |
10650 | ||
10651 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
10652 | bytes, loc); | |
10653 | ||
10654 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
10655 | ||
10656 | static int count; | |
10657 | char buf[50]; | |
10658 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
10659 | ++count; | |
10660 | ||
10661 | Named_object* no = gogo->add_variable(buf, var); | |
10662 | e = Expression::make_var_reference(no, loc); | |
10663 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
10664 | ||
10665 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
10666 | inserter->insert(Statement::make_statement(call, false)); | |
10667 | ||
10668 | // Put this function, and the global variable we just created, into | |
10669 | // unique sections. This will permit the linker to garbage collect | |
10670 | // them if they are not referenced. The effect is that the only | |
10671 | // strings, indicating field references, that will wind up in the | |
10672 | // executable will be those for functions that are actually needed. | |
66a6be58 | 10673 | if (function != NULL) |
10674 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 10675 | var->set_in_unique_section(); |
10676 | ||
10677 | return this; | |
10678 | } | |
10679 | ||
e440a328 | 10680 | // Return the type of a field reference. |
10681 | ||
10682 | Type* | |
10683 | Field_reference_expression::do_type() | |
10684 | { | |
b0e628fb | 10685 | Type* type = this->expr_->type(); |
5c13bd80 | 10686 | if (type->is_error()) |
b0e628fb | 10687 | return type; |
10688 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 10689 | go_assert(struct_type != NULL); |
e440a328 | 10690 | return struct_type->field(this->field_index_)->type(); |
10691 | } | |
10692 | ||
10693 | // Check the types for a field reference. | |
10694 | ||
10695 | void | |
10696 | Field_reference_expression::do_check_types(Gogo*) | |
10697 | { | |
b0e628fb | 10698 | Type* type = this->expr_->type(); |
5c13bd80 | 10699 | if (type->is_error()) |
b0e628fb | 10700 | return; |
10701 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 10702 | go_assert(struct_type != NULL); |
10703 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 10704 | } |
10705 | ||
10706 | // Get a tree for a field reference. | |
10707 | ||
10708 | tree | |
10709 | Field_reference_expression::do_get_tree(Translate_context* context) | |
10710 | { | |
10711 | tree struct_tree = this->expr_->get_tree(context); | |
10712 | if (struct_tree == error_mark_node | |
10713 | || TREE_TYPE(struct_tree) == error_mark_node) | |
10714 | return error_mark_node; | |
c484d925 | 10715 | go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); |
e440a328 | 10716 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); |
b1d655d5 | 10717 | if (field == NULL_TREE) |
10718 | { | |
10719 | // This can happen for a type which refers to itself indirectly | |
10720 | // and then turns out to be erroneous. | |
c484d925 | 10721 | go_assert(saw_errors()); |
b1d655d5 | 10722 | return error_mark_node; |
10723 | } | |
e440a328 | 10724 | for (unsigned int i = this->field_index_; i > 0; --i) |
10725 | { | |
10726 | field = DECL_CHAIN(field); | |
c484d925 | 10727 | go_assert(field != NULL_TREE); |
e440a328 | 10728 | } |
c35179ff | 10729 | if (TREE_TYPE(field) == error_mark_node) |
10730 | return error_mark_node; | |
e440a328 | 10731 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, |
10732 | NULL_TREE); | |
10733 | } | |
10734 | ||
d751bb78 | 10735 | // Dump ast representation for a field reference expression. |
10736 | ||
10737 | void | |
10738 | Field_reference_expression::do_dump_expression( | |
10739 | Ast_dump_context* ast_dump_context) const | |
10740 | { | |
10741 | this->expr_->dump_expression(ast_dump_context); | |
10742 | ast_dump_context->ostream() << "." << this->field_index_; | |
10743 | } | |
10744 | ||
e440a328 | 10745 | // Make a reference to a qualified identifier in an expression. |
10746 | ||
10747 | Field_reference_expression* | |
10748 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 10749 | Location location) |
e440a328 | 10750 | { |
10751 | return new Field_reference_expression(expr, field_index, location); | |
10752 | } | |
10753 | ||
10754 | // Class Interface_field_reference_expression. | |
10755 | ||
10756 | // Return a tree for the pointer to the function to call. | |
10757 | ||
10758 | tree | |
10759 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
10760 | tree expr) | |
10761 | { | |
10762 | if (this->expr_->type()->points_to() != NULL) | |
10763 | expr = build_fold_indirect_ref(expr); | |
10764 | ||
10765 | tree expr_type = TREE_TYPE(expr); | |
c484d925 | 10766 | go_assert(TREE_CODE(expr_type) == RECORD_TYPE); |
e440a328 | 10767 | |
10768 | tree field = TYPE_FIELDS(expr_type); | |
c484d925 | 10769 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); |
e440a328 | 10770 | |
10771 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
c484d925 | 10772 | go_assert(POINTER_TYPE_P(TREE_TYPE(table))); |
e440a328 | 10773 | |
10774 | table = build_fold_indirect_ref(table); | |
c484d925 | 10775 | go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); |
e440a328 | 10776 | |
10777 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
10778 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
10779 | field != NULL_TREE; | |
10780 | field = DECL_CHAIN(field)) | |
10781 | { | |
10782 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
10783 | break; | |
10784 | } | |
c484d925 | 10785 | go_assert(field != NULL_TREE); |
e440a328 | 10786 | |
10787 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
10788 | } | |
10789 | ||
10790 | // Return a tree for the first argument to pass to the interface | |
10791 | // function. | |
10792 | ||
10793 | tree | |
10794 | Interface_field_reference_expression::get_underlying_object_tree( | |
10795 | Translate_context*, | |
10796 | tree expr) | |
10797 | { | |
10798 | if (this->expr_->type()->points_to() != NULL) | |
10799 | expr = build_fold_indirect_ref(expr); | |
10800 | ||
10801 | tree expr_type = TREE_TYPE(expr); | |
c484d925 | 10802 | go_assert(TREE_CODE(expr_type) == RECORD_TYPE); |
e440a328 | 10803 | |
10804 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
c484d925 | 10805 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 10806 | |
10807 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
10808 | } | |
10809 | ||
10810 | // Traversal. | |
10811 | ||
10812 | int | |
10813 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
10814 | { | |
10815 | return Expression::traverse(&this->expr_, traverse); | |
10816 | } | |
10817 | ||
10818 | // Return the type of an interface field reference. | |
10819 | ||
10820 | Type* | |
10821 | Interface_field_reference_expression::do_type() | |
10822 | { | |
10823 | Type* expr_type = this->expr_->type(); | |
10824 | ||
10825 | Type* points_to = expr_type->points_to(); | |
10826 | if (points_to != NULL) | |
10827 | expr_type = points_to; | |
10828 | ||
10829 | Interface_type* interface_type = expr_type->interface_type(); | |
10830 | if (interface_type == NULL) | |
10831 | return Type::make_error_type(); | |
10832 | ||
10833 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
10834 | if (method == NULL) | |
10835 | return Type::make_error_type(); | |
10836 | ||
10837 | return method->type(); | |
10838 | } | |
10839 | ||
10840 | // Determine types. | |
10841 | ||
10842 | void | |
10843 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
10844 | { | |
10845 | this->expr_->determine_type_no_context(); | |
10846 | } | |
10847 | ||
10848 | // Check the types for an interface field reference. | |
10849 | ||
10850 | void | |
10851 | Interface_field_reference_expression::do_check_types(Gogo*) | |
10852 | { | |
10853 | Type* type = this->expr_->type(); | |
10854 | ||
10855 | Type* points_to = type->points_to(); | |
10856 | if (points_to != NULL) | |
10857 | type = points_to; | |
10858 | ||
10859 | Interface_type* interface_type = type->interface_type(); | |
10860 | if (interface_type == NULL) | |
5c491127 | 10861 | { |
10862 | if (!type->is_error_type()) | |
10863 | this->report_error(_("expected interface or pointer to interface")); | |
10864 | } | |
e440a328 | 10865 | else |
10866 | { | |
10867 | const Typed_identifier* method = | |
10868 | interface_type->find_method(this->name_); | |
10869 | if (method == NULL) | |
10870 | { | |
10871 | error_at(this->location(), "method %qs not in interface", | |
10872 | Gogo::message_name(this->name_).c_str()); | |
10873 | this->set_is_error(); | |
10874 | } | |
10875 | } | |
10876 | } | |
10877 | ||
10878 | // Get a tree for a reference to a field in an interface. There is no | |
10879 | // standard tree type representation for this: it's a function | |
10880 | // attached to its first argument, like a Bound_method_expression. | |
10881 | // The only places it may currently be used are in a Call_expression | |
10882 | // or a Go_statement, which will take it apart directly. So this has | |
10883 | // nothing to do at present. | |
10884 | ||
10885 | tree | |
10886 | Interface_field_reference_expression::do_get_tree(Translate_context*) | |
10887 | { | |
11bbe026 | 10888 | error_at(this->location(), "reference to method other than calling it"); |
10889 | return error_mark_node; | |
e440a328 | 10890 | } |
10891 | ||
d751bb78 | 10892 | // Dump ast representation for an interface field reference. |
10893 | ||
10894 | void | |
10895 | Interface_field_reference_expression::do_dump_expression( | |
10896 | Ast_dump_context* ast_dump_context) const | |
10897 | { | |
10898 | this->expr_->dump_expression(ast_dump_context); | |
10899 | ast_dump_context->ostream() << "." << this->name_; | |
10900 | } | |
10901 | ||
e440a328 | 10902 | // Make a reference to a field in an interface. |
10903 | ||
10904 | Expression* | |
10905 | Expression::make_interface_field_reference(Expression* expr, | |
10906 | const std::string& field, | |
b13c66cd | 10907 | Location location) |
e440a328 | 10908 | { |
10909 | return new Interface_field_reference_expression(expr, field, location); | |
10910 | } | |
10911 | ||
10912 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
10913 | // is lowered after we know the type of the left hand side. | |
10914 | ||
10915 | class Selector_expression : public Parser_expression | |
10916 | { | |
10917 | public: | |
10918 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 10919 | Location location) |
e440a328 | 10920 | : Parser_expression(EXPRESSION_SELECTOR, location), |
10921 | left_(left), name_(name) | |
10922 | { } | |
10923 | ||
10924 | protected: | |
10925 | int | |
10926 | do_traverse(Traverse* traverse) | |
10927 | { return Expression::traverse(&this->left_, traverse); } | |
10928 | ||
10929 | Expression* | |
ceeb4318 | 10930 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 10931 | |
10932 | Expression* | |
10933 | do_copy() | |
10934 | { | |
10935 | return new Selector_expression(this->left_->copy(), this->name_, | |
10936 | this->location()); | |
10937 | } | |
10938 | ||
d751bb78 | 10939 | void |
10940 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
10941 | ||
e440a328 | 10942 | private: |
10943 | Expression* | |
10944 | lower_method_expression(Gogo*); | |
10945 | ||
10946 | // The expression on the left hand side. | |
10947 | Expression* left_; | |
10948 | // The name on the right hand side. | |
10949 | std::string name_; | |
10950 | }; | |
10951 | ||
10952 | // Lower a selector expression once we know the real type of the left | |
10953 | // hand side. | |
10954 | ||
10955 | Expression* | |
ceeb4318 | 10956 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
10957 | int) | |
e440a328 | 10958 | { |
10959 | Expression* left = this->left_; | |
10960 | if (left->is_type_expression()) | |
10961 | return this->lower_method_expression(gogo); | |
10962 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
10963 | this->location()); | |
10964 | } | |
10965 | ||
10966 | // Lower a method expression T.M or (*T).M. We turn this into a | |
10967 | // function literal. | |
10968 | ||
10969 | Expression* | |
10970 | Selector_expression::lower_method_expression(Gogo* gogo) | |
10971 | { | |
b13c66cd | 10972 | Location location = this->location(); |
e440a328 | 10973 | Type* type = this->left_->type(); |
10974 | const std::string& name(this->name_); | |
10975 | ||
10976 | bool is_pointer; | |
10977 | if (type->points_to() == NULL) | |
10978 | is_pointer = false; | |
10979 | else | |
10980 | { | |
10981 | is_pointer = true; | |
10982 | type = type->points_to(); | |
10983 | } | |
10984 | Named_type* nt = type->named_type(); | |
10985 | if (nt == NULL) | |
10986 | { | |
10987 | error_at(location, | |
10988 | ("method expression requires named type or " | |
10989 | "pointer to named type")); | |
10990 | return Expression::make_error(location); | |
10991 | } | |
10992 | ||
10993 | bool is_ambiguous; | |
10994 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 10995 | const Typed_identifier* imethod = NULL; |
dcc8506b | 10996 | if (method == NULL && !is_pointer) |
ab1468c3 | 10997 | { |
10998 | Interface_type* it = nt->interface_type(); | |
10999 | if (it != NULL) | |
11000 | imethod = it->find_method(name); | |
11001 | } | |
11002 | ||
11003 | if (method == NULL && imethod == NULL) | |
e440a328 | 11004 | { |
11005 | if (!is_ambiguous) | |
dcc8506b | 11006 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
11007 | is_pointer ? "*" : "", | |
e440a328 | 11008 | nt->message_name().c_str(), |
11009 | Gogo::message_name(name).c_str()); | |
11010 | else | |
dcc8506b | 11011 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 11012 | Gogo::message_name(name).c_str(), |
dcc8506b | 11013 | is_pointer ? "*" : "", |
e440a328 | 11014 | nt->message_name().c_str()); |
11015 | return Expression::make_error(location); | |
11016 | } | |
11017 | ||
ab1468c3 | 11018 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 11019 | { |
11020 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
11021 | nt->message_name().c_str(), | |
11022 | Gogo::message_name(name).c_str()); | |
11023 | return Expression::make_error(location); | |
11024 | } | |
11025 | ||
11026 | // Build a new function type in which the receiver becomes the first | |
11027 | // argument. | |
ab1468c3 | 11028 | Function_type* method_type; |
11029 | if (method != NULL) | |
11030 | { | |
11031 | method_type = method->type(); | |
c484d925 | 11032 | go_assert(method_type->is_method()); |
ab1468c3 | 11033 | } |
11034 | else | |
11035 | { | |
11036 | method_type = imethod->type()->function_type(); | |
c484d925 | 11037 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 11038 | } |
e440a328 | 11039 | |
11040 | const char* const receiver_name = "$this"; | |
11041 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
11042 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
11043 | location)); | |
11044 | ||
11045 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
11046 | if (method_parameters != NULL) | |
11047 | { | |
f470da59 | 11048 | int i = 0; |
e440a328 | 11049 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
11050 | p != method_parameters->end(); | |
f470da59 | 11051 | ++p, ++i) |
11052 | { | |
68883531 | 11053 | if (!p->name().empty()) |
f470da59 | 11054 | parameters->push_back(*p); |
11055 | else | |
11056 | { | |
11057 | char buf[20]; | |
11058 | snprintf(buf, sizeof buf, "$param%d", i); | |
11059 | parameters->push_back(Typed_identifier(buf, p->type(), | |
11060 | p->location())); | |
11061 | } | |
11062 | } | |
e440a328 | 11063 | } |
11064 | ||
11065 | const Typed_identifier_list* method_results = method_type->results(); | |
11066 | Typed_identifier_list* results; | |
11067 | if (method_results == NULL) | |
11068 | results = NULL; | |
11069 | else | |
11070 | { | |
11071 | results = new Typed_identifier_list(); | |
11072 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
11073 | p != method_results->end(); | |
11074 | ++p) | |
11075 | results->push_back(*p); | |
11076 | } | |
11077 | ||
11078 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
11079 | location); | |
11080 | if (method_type->is_varargs()) | |
11081 | fntype->set_is_varargs(); | |
11082 | ||
11083 | // We generate methods which always takes a pointer to the receiver | |
11084 | // as their first argument. If this is for a pointer type, we can | |
11085 | // simply reuse the existing function. We use an internal hack to | |
11086 | // get the right type. | |
11087 | ||
ab1468c3 | 11088 | if (method != NULL && is_pointer) |
e440a328 | 11089 | { |
11090 | Named_object* mno = (method->needs_stub_method() | |
11091 | ? method->stub_object() | |
11092 | : method->named_object()); | |
11093 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
11094 | f = Expression::make_cast(fntype, f, location); | |
11095 | Type_conversion_expression* tce = | |
11096 | static_cast<Type_conversion_expression*>(f); | |
11097 | tce->set_may_convert_function_types(); | |
11098 | return f; | |
11099 | } | |
11100 | ||
11101 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
11102 | location); | |
11103 | ||
11104 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 11105 | go_assert(vno != NULL); |
e440a328 | 11106 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 11107 | Expression* bm; |
11108 | if (method != NULL) | |
11109 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
11110 | else | |
11111 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 11112 | |
11113 | // Even though we found the method above, if it has an error type we | |
11114 | // may see an error here. | |
11115 | if (bm->is_error_expression()) | |
463fe805 | 11116 | { |
11117 | gogo->finish_function(location); | |
11118 | return bm; | |
11119 | } | |
e440a328 | 11120 | |
11121 | Expression_list* args; | |
f470da59 | 11122 | if (parameters->size() <= 1) |
e440a328 | 11123 | args = NULL; |
11124 | else | |
11125 | { | |
11126 | args = new Expression_list(); | |
f470da59 | 11127 | Typed_identifier_list::const_iterator p = parameters->begin(); |
11128 | ++p; | |
11129 | for (; p != parameters->end(); ++p) | |
e440a328 | 11130 | { |
11131 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 11132 | go_assert(vno != NULL); |
e440a328 | 11133 | args->push_back(Expression::make_var_reference(vno, location)); |
11134 | } | |
11135 | } | |
11136 | ||
ceeb4318 | 11137 | gogo->start_block(location); |
11138 | ||
e440a328 | 11139 | Call_expression* call = Expression::make_call(bm, args, |
11140 | method_type->is_varargs(), | |
11141 | location); | |
11142 | ||
11143 | size_t count = call->result_count(); | |
11144 | Statement* s; | |
11145 | if (count == 0) | |
a7549a6a | 11146 | s = Statement::make_statement(call, true); |
e440a328 | 11147 | else |
11148 | { | |
11149 | Expression_list* retvals = new Expression_list(); | |
11150 | if (count <= 1) | |
11151 | retvals->push_back(call); | |
11152 | else | |
11153 | { | |
11154 | for (size_t i = 0; i < count; ++i) | |
11155 | retvals->push_back(Expression::make_call_result(call, i)); | |
11156 | } | |
be2fc38d | 11157 | s = Statement::make_return_statement(retvals, location); |
e440a328 | 11158 | } |
11159 | gogo->add_statement(s); | |
11160 | ||
ceeb4318 | 11161 | Block* b = gogo->finish_block(location); |
11162 | ||
11163 | gogo->add_block(b, location); | |
11164 | ||
11165 | // Lower the call in case there are multiple results. | |
11166 | gogo->lower_block(no, b); | |
11167 | ||
e440a328 | 11168 | gogo->finish_function(location); |
11169 | ||
11170 | return Expression::make_func_reference(no, NULL, location); | |
11171 | } | |
11172 | ||
d751bb78 | 11173 | // Dump the ast for a selector expression. |
11174 | ||
11175 | void | |
11176 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11177 | const | |
11178 | { | |
11179 | ast_dump_context->dump_expression(this->left_); | |
11180 | ast_dump_context->ostream() << "."; | |
11181 | ast_dump_context->ostream() << this->name_; | |
11182 | } | |
11183 | ||
e440a328 | 11184 | // Make a selector expression. |
11185 | ||
11186 | Expression* | |
11187 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 11188 | Location location) |
e440a328 | 11189 | { |
11190 | return new Selector_expression(left, name, location); | |
11191 | } | |
11192 | ||
11193 | // Implement the builtin function new. | |
11194 | ||
11195 | class Allocation_expression : public Expression | |
11196 | { | |
11197 | public: | |
b13c66cd | 11198 | Allocation_expression(Type* type, Location location) |
e440a328 | 11199 | : Expression(EXPRESSION_ALLOCATION, location), |
11200 | type_(type) | |
11201 | { } | |
11202 | ||
11203 | protected: | |
11204 | int | |
11205 | do_traverse(Traverse* traverse) | |
11206 | { return Type::traverse(this->type_, traverse); } | |
11207 | ||
11208 | Type* | |
11209 | do_type() | |
11210 | { return Type::make_pointer_type(this->type_); } | |
11211 | ||
11212 | void | |
11213 | do_determine_type(const Type_context*) | |
11214 | { } | |
11215 | ||
e440a328 | 11216 | Expression* |
11217 | do_copy() | |
11218 | { return new Allocation_expression(this->type_, this->location()); } | |
11219 | ||
11220 | tree | |
11221 | do_get_tree(Translate_context*); | |
11222 | ||
d751bb78 | 11223 | void |
11224 | do_dump_expression(Ast_dump_context*) const; | |
11225 | ||
e440a328 | 11226 | private: |
11227 | // The type we are allocating. | |
11228 | Type* type_; | |
11229 | }; | |
11230 | ||
e440a328 | 11231 | // Return a tree for an allocation expression. |
11232 | ||
11233 | tree | |
11234 | Allocation_expression::do_get_tree(Translate_context* context) | |
11235 | { | |
9f0e0513 | 11236 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
19824ddb | 11237 | if (type_tree == error_mark_node) |
11238 | return error_mark_node; | |
e440a328 | 11239 | tree size_tree = TYPE_SIZE_UNIT(type_tree); |
11240 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
11241 | this->location()); | |
19824ddb | 11242 | if (space == error_mark_node) |
11243 | return error_mark_node; | |
e440a328 | 11244 | return fold_convert(build_pointer_type(type_tree), space); |
11245 | } | |
11246 | ||
d751bb78 | 11247 | // Dump ast representation for an allocation expression. |
11248 | ||
11249 | void | |
11250 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11251 | const | |
11252 | { | |
11253 | ast_dump_context->ostream() << "new("; | |
11254 | ast_dump_context->dump_type(this->type_); | |
11255 | ast_dump_context->ostream() << ")"; | |
11256 | } | |
11257 | ||
e440a328 | 11258 | // Make an allocation expression. |
11259 | ||
11260 | Expression* | |
b13c66cd | 11261 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 11262 | { |
11263 | return new Allocation_expression(type, location); | |
11264 | } | |
11265 | ||
e440a328 | 11266 | // Construct a struct. |
11267 | ||
11268 | class Struct_construction_expression : public Expression | |
11269 | { | |
11270 | public: | |
11271 | Struct_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 11272 | Location location) |
e440a328 | 11273 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), |
0c4f5a19 | 11274 | type_(type), vals_(vals), traverse_order_(NULL) |
e440a328 | 11275 | { } |
11276 | ||
0c4f5a19 | 11277 | // Set the traversal order, used to ensure that we implement the |
11278 | // order of evaluation rules. Takes ownership of the argument. | |
11279 | void | |
11280 | set_traverse_order(std::vector<int>* traverse_order) | |
11281 | { this->traverse_order_ = traverse_order; } | |
11282 | ||
e440a328 | 11283 | // Return whether this is a constant initializer. |
11284 | bool | |
11285 | is_constant_struct() const; | |
11286 | ||
11287 | protected: | |
11288 | int | |
11289 | do_traverse(Traverse* traverse); | |
11290 | ||
11291 | Type* | |
11292 | do_type() | |
11293 | { return this->type_; } | |
11294 | ||
11295 | void | |
11296 | do_determine_type(const Type_context*); | |
11297 | ||
11298 | void | |
11299 | do_check_types(Gogo*); | |
11300 | ||
11301 | Expression* | |
11302 | do_copy() | |
11303 | { | |
0c4f5a19 | 11304 | Struct_construction_expression* ret = |
11305 | new Struct_construction_expression(this->type_, this->vals_->copy(), | |
11306 | this->location()); | |
11307 | if (this->traverse_order_ != NULL) | |
11308 | ret->set_traverse_order(this->traverse_order_); | |
11309 | return ret; | |
e440a328 | 11310 | } |
11311 | ||
e440a328 | 11312 | tree |
11313 | do_get_tree(Translate_context*); | |
11314 | ||
11315 | void | |
11316 | do_export(Export*) const; | |
11317 | ||
d751bb78 | 11318 | void |
11319 | do_dump_expression(Ast_dump_context*) const; | |
11320 | ||
e440a328 | 11321 | private: |
11322 | // The type of the struct to construct. | |
11323 | Type* type_; | |
11324 | // The list of values, in order of the fields in the struct. A NULL | |
11325 | // entry means that the field should be zero-initialized. | |
11326 | Expression_list* vals_; | |
0c4f5a19 | 11327 | // If not NULL, the order in which to traverse vals_. This is used |
11328 | // so that we implement the order of evaluation rules correctly. | |
11329 | std::vector<int>* traverse_order_; | |
e440a328 | 11330 | }; |
11331 | ||
11332 | // Traversal. | |
11333 | ||
11334 | int | |
11335 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
11336 | { | |
0c4f5a19 | 11337 | if (this->vals_ != NULL) |
11338 | { | |
11339 | if (this->traverse_order_ == NULL) | |
11340 | { | |
11341 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11342 | return TRAVERSE_EXIT; | |
11343 | } | |
11344 | else | |
11345 | { | |
11346 | for (std::vector<int>::const_iterator p = | |
11347 | this->traverse_order_->begin(); | |
11348 | p != this->traverse_order_->end(); | |
11349 | ++p) | |
11350 | { | |
11351 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
11352 | == TRAVERSE_EXIT) | |
11353 | return TRAVERSE_EXIT; | |
11354 | } | |
11355 | } | |
11356 | } | |
e440a328 | 11357 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
11358 | return TRAVERSE_EXIT; | |
11359 | return TRAVERSE_CONTINUE; | |
11360 | } | |
11361 | ||
11362 | // Return whether this is a constant initializer. | |
11363 | ||
11364 | bool | |
11365 | Struct_construction_expression::is_constant_struct() const | |
11366 | { | |
11367 | if (this->vals_ == NULL) | |
11368 | return true; | |
11369 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11370 | pv != this->vals_->end(); | |
11371 | ++pv) | |
11372 | { | |
11373 | if (*pv != NULL | |
11374 | && !(*pv)->is_constant() | |
11375 | && (!(*pv)->is_composite_literal() | |
11376 | || (*pv)->is_nonconstant_composite_literal())) | |
11377 | return false; | |
11378 | } | |
11379 | ||
11380 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11381 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11382 | pf != fields->end(); | |
11383 | ++pf) | |
11384 | { | |
11385 | // There are no constant constructors for interfaces. | |
11386 | if (pf->type()->interface_type() != NULL) | |
11387 | return false; | |
11388 | } | |
11389 | ||
11390 | return true; | |
11391 | } | |
11392 | ||
11393 | // Final type determination. | |
11394 | ||
11395 | void | |
11396 | Struct_construction_expression::do_determine_type(const Type_context*) | |
11397 | { | |
11398 | if (this->vals_ == NULL) | |
11399 | return; | |
11400 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11401 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11402 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11403 | pf != fields->end(); | |
11404 | ++pf, ++pv) | |
11405 | { | |
11406 | if (pv == this->vals_->end()) | |
11407 | return; | |
11408 | if (*pv != NULL) | |
11409 | { | |
11410 | Type_context subcontext(pf->type(), false); | |
11411 | (*pv)->determine_type(&subcontext); | |
11412 | } | |
11413 | } | |
a6cb4c0e | 11414 | // Extra values are an error we will report elsewhere; we still want |
11415 | // to determine the type to avoid knockon errors. | |
11416 | for (; pv != this->vals_->end(); ++pv) | |
11417 | (*pv)->determine_type_no_context(); | |
e440a328 | 11418 | } |
11419 | ||
11420 | // Check types. | |
11421 | ||
11422 | void | |
11423 | Struct_construction_expression::do_check_types(Gogo*) | |
11424 | { | |
11425 | if (this->vals_ == NULL) | |
11426 | return; | |
11427 | ||
11428 | Struct_type* st = this->type_->struct_type(); | |
11429 | if (this->vals_->size() > st->field_count()) | |
11430 | { | |
11431 | this->report_error(_("too many expressions for struct")); | |
11432 | return; | |
11433 | } | |
11434 | ||
11435 | const Struct_field_list* fields = st->fields(); | |
11436 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11437 | int i = 0; | |
11438 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11439 | pf != fields->end(); | |
11440 | ++pf, ++pv, ++i) | |
11441 | { | |
11442 | if (pv == this->vals_->end()) | |
11443 | { | |
11444 | this->report_error(_("too few expressions for struct")); | |
11445 | break; | |
11446 | } | |
11447 | ||
11448 | if (*pv == NULL) | |
11449 | continue; | |
11450 | ||
11451 | std::string reason; | |
11452 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
11453 | { | |
11454 | if (reason.empty()) | |
11455 | error_at((*pv)->location(), | |
11456 | "incompatible type for field %d in struct construction", | |
11457 | i + 1); | |
11458 | else | |
11459 | error_at((*pv)->location(), | |
11460 | ("incompatible type for field %d in " | |
11461 | "struct construction (%s)"), | |
11462 | i + 1, reason.c_str()); | |
11463 | this->set_is_error(); | |
11464 | } | |
11465 | } | |
c484d925 | 11466 | go_assert(pv == this->vals_->end()); |
e440a328 | 11467 | } |
11468 | ||
11469 | // Return a tree for constructing a struct. | |
11470 | ||
11471 | tree | |
11472 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
11473 | { | |
11474 | Gogo* gogo = context->gogo(); | |
11475 | ||
11476 | if (this->vals_ == NULL) | |
63697958 | 11477 | { |
11478 | Btype* btype = this->type_->get_backend(gogo); | |
11479 | return expr_to_tree(gogo->backend()->zero_expression(btype)); | |
11480 | } | |
e440a328 | 11481 | |
9f0e0513 | 11482 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 11483 | if (type_tree == error_mark_node) |
11484 | return error_mark_node; | |
c484d925 | 11485 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 11486 | |
11487 | bool is_constant = true; | |
11488 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
95f84544 | 11489 | vec<constructor_elt, va_gc> *elts; |
11490 | vec_alloc (elts, fields->size()); | |
e440a328 | 11491 | Struct_field_list::const_iterator pf = fields->begin(); |
11492 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11493 | for (tree field = TYPE_FIELDS(type_tree); | |
11494 | field != NULL_TREE; | |
11495 | field = DECL_CHAIN(field), ++pf) | |
11496 | { | |
c484d925 | 11497 | go_assert(pf != fields->end()); |
e440a328 | 11498 | |
63697958 | 11499 | Btype* fbtype = pf->type()->get_backend(gogo); |
11500 | ||
e440a328 | 11501 | tree val; |
11502 | if (pv == this->vals_->end()) | |
63697958 | 11503 | val = expr_to_tree(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 11504 | else if (*pv == NULL) |
11505 | { | |
63697958 | 11506 | val = expr_to_tree(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 11507 | ++pv; |
11508 | } | |
11509 | else | |
11510 | { | |
11511 | val = Expression::convert_for_assignment(context, pf->type(), | |
11512 | (*pv)->type(), | |
11513 | (*pv)->get_tree(context), | |
11514 | this->location()); | |
11515 | ++pv; | |
11516 | } | |
11517 | ||
11518 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
11519 | return error_mark_node; | |
11520 | ||
e82e4eb5 | 11521 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 11522 | constructor_elt* elt = elts->quick_push(empty); |
e440a328 | 11523 | elt->index = field; |
11524 | elt->value = val; | |
11525 | if (!TREE_CONSTANT(val)) | |
11526 | is_constant = false; | |
11527 | } | |
c484d925 | 11528 | go_assert(pf == fields->end()); |
e440a328 | 11529 | |
11530 | tree ret = build_constructor(type_tree, elts); | |
11531 | if (is_constant) | |
11532 | TREE_CONSTANT(ret) = 1; | |
11533 | return ret; | |
11534 | } | |
11535 | ||
11536 | // Export a struct construction. | |
11537 | ||
11538 | void | |
11539 | Struct_construction_expression::do_export(Export* exp) const | |
11540 | { | |
11541 | exp->write_c_string("convert("); | |
11542 | exp->write_type(this->type_); | |
11543 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11544 | pv != this->vals_->end(); | |
11545 | ++pv) | |
11546 | { | |
11547 | exp->write_c_string(", "); | |
11548 | if (*pv != NULL) | |
11549 | (*pv)->export_expression(exp); | |
11550 | } | |
11551 | exp->write_c_string(")"); | |
11552 | } | |
11553 | ||
d751bb78 | 11554 | // Dump ast representation of a struct construction expression. |
11555 | ||
11556 | void | |
11557 | Struct_construction_expression::do_dump_expression( | |
11558 | Ast_dump_context* ast_dump_context) const | |
11559 | { | |
d751bb78 | 11560 | ast_dump_context->dump_type(this->type_); |
11561 | ast_dump_context->ostream() << "{"; | |
11562 | ast_dump_context->dump_expression_list(this->vals_); | |
11563 | ast_dump_context->ostream() << "}"; | |
11564 | } | |
11565 | ||
e440a328 | 11566 | // Make a struct composite literal. This used by the thunk code. |
11567 | ||
11568 | Expression* | |
11569 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 11570 | Location location) |
e440a328 | 11571 | { |
c484d925 | 11572 | go_assert(type->struct_type() != NULL); |
e440a328 | 11573 | return new Struct_construction_expression(type, vals, location); |
11574 | } | |
11575 | ||
11576 | // Construct an array. This class is not used directly; instead we | |
11577 | // use the child classes, Fixed_array_construction_expression and | |
11578 | // Open_array_construction_expression. | |
11579 | ||
11580 | class Array_construction_expression : public Expression | |
11581 | { | |
11582 | protected: | |
11583 | Array_construction_expression(Expression_classification classification, | |
ffe743ca | 11584 | Type* type, |
11585 | const std::vector<unsigned long>* indexes, | |
11586 | Expression_list* vals, Location location) | |
e440a328 | 11587 | : Expression(classification, location), |
ffe743ca | 11588 | type_(type), indexes_(indexes), vals_(vals) |
11589 | { go_assert(indexes == NULL || indexes->size() == vals->size()); } | |
e440a328 | 11590 | |
11591 | public: | |
11592 | // Return whether this is a constant initializer. | |
11593 | bool | |
11594 | is_constant_array() const; | |
11595 | ||
11596 | // Return the number of elements. | |
11597 | size_t | |
11598 | element_count() const | |
11599 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
11600 | ||
11601 | protected: | |
11602 | int | |
11603 | do_traverse(Traverse* traverse); | |
11604 | ||
11605 | Type* | |
11606 | do_type() | |
11607 | { return this->type_; } | |
11608 | ||
11609 | void | |
11610 | do_determine_type(const Type_context*); | |
11611 | ||
11612 | void | |
11613 | do_check_types(Gogo*); | |
11614 | ||
e440a328 | 11615 | void |
11616 | do_export(Export*) const; | |
11617 | ||
ffe743ca | 11618 | // The indexes. |
11619 | const std::vector<unsigned long>* | |
11620 | indexes() | |
11621 | { return this->indexes_; } | |
11622 | ||
e440a328 | 11623 | // The list of values. |
11624 | Expression_list* | |
11625 | vals() | |
11626 | { return this->vals_; } | |
11627 | ||
11628 | // Get a constructor tree for the array values. | |
11629 | tree | |
11630 | get_constructor_tree(Translate_context* context, tree type_tree); | |
11631 | ||
d751bb78 | 11632 | void |
11633 | do_dump_expression(Ast_dump_context*) const; | |
11634 | ||
e440a328 | 11635 | private: |
11636 | // The type of the array to construct. | |
11637 | Type* type_; | |
ffe743ca | 11638 | // The list of indexes into the array, one for each value. This may |
11639 | // be NULL, in which case the indexes start at zero and increment. | |
11640 | const std::vector<unsigned long>* indexes_; | |
11641 | // The list of values. This may be NULL if there are no values. | |
e440a328 | 11642 | Expression_list* vals_; |
11643 | }; | |
11644 | ||
11645 | // Traversal. | |
11646 | ||
11647 | int | |
11648 | Array_construction_expression::do_traverse(Traverse* traverse) | |
11649 | { | |
11650 | if (this->vals_ != NULL | |
11651 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11652 | return TRAVERSE_EXIT; | |
11653 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11654 | return TRAVERSE_EXIT; | |
11655 | return TRAVERSE_CONTINUE; | |
11656 | } | |
11657 | ||
11658 | // Return whether this is a constant initializer. | |
11659 | ||
11660 | bool | |
11661 | Array_construction_expression::is_constant_array() const | |
11662 | { | |
11663 | if (this->vals_ == NULL) | |
11664 | return true; | |
11665 | ||
11666 | // There are no constant constructors for interfaces. | |
11667 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
11668 | return false; | |
11669 | ||
11670 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11671 | pv != this->vals_->end(); | |
11672 | ++pv) | |
11673 | { | |
11674 | if (*pv != NULL | |
11675 | && !(*pv)->is_constant() | |
11676 | && (!(*pv)->is_composite_literal() | |
11677 | || (*pv)->is_nonconstant_composite_literal())) | |
11678 | return false; | |
11679 | } | |
11680 | return true; | |
11681 | } | |
11682 | ||
11683 | // Final type determination. | |
11684 | ||
11685 | void | |
11686 | Array_construction_expression::do_determine_type(const Type_context*) | |
11687 | { | |
11688 | if (this->vals_ == NULL) | |
11689 | return; | |
11690 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
11691 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11692 | pv != this->vals_->end(); | |
11693 | ++pv) | |
11694 | { | |
11695 | if (*pv != NULL) | |
11696 | (*pv)->determine_type(&subcontext); | |
11697 | } | |
11698 | } | |
11699 | ||
11700 | // Check types. | |
11701 | ||
11702 | void | |
11703 | Array_construction_expression::do_check_types(Gogo*) | |
11704 | { | |
11705 | if (this->vals_ == NULL) | |
11706 | return; | |
11707 | ||
11708 | Array_type* at = this->type_->array_type(); | |
11709 | int i = 0; | |
11710 | Type* element_type = at->element_type(); | |
11711 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11712 | pv != this->vals_->end(); | |
11713 | ++pv, ++i) | |
11714 | { | |
11715 | if (*pv != NULL | |
11716 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
11717 | { | |
11718 | error_at((*pv)->location(), | |
11719 | "incompatible type for element %d in composite literal", | |
11720 | i + 1); | |
11721 | this->set_is_error(); | |
11722 | } | |
11723 | } | |
e440a328 | 11724 | } |
11725 | ||
11726 | // Get a constructor tree for the array values. | |
11727 | ||
11728 | tree | |
11729 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
11730 | tree type_tree) | |
11731 | { | |
95f84544 | 11732 | vec<constructor_elt, va_gc> *values; |
11733 | vec_alloc (values, (this->vals_ == NULL ? 0 : this->vals_->size())); | |
e440a328 | 11734 | Type* element_type = this->type_->array_type()->element_type(); |
11735 | bool is_constant = true; | |
11736 | if (this->vals_ != NULL) | |
11737 | { | |
11738 | size_t i = 0; | |
ffe743ca | 11739 | std::vector<unsigned long>::const_iterator pi; |
11740 | if (this->indexes_ != NULL) | |
11741 | pi = this->indexes_->begin(); | |
e440a328 | 11742 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
11743 | pv != this->vals_->end(); | |
11744 | ++pv, ++i) | |
11745 | { | |
ffe743ca | 11746 | if (this->indexes_ != NULL) |
11747 | go_assert(pi != this->indexes_->end()); | |
e82e4eb5 | 11748 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 11749 | constructor_elt* elt = values->quick_push(empty); |
ffe743ca | 11750 | |
11751 | if (this->indexes_ == NULL) | |
11752 | elt->index = size_int(i); | |
11753 | else | |
11754 | elt->index = size_int(*pi); | |
11755 | ||
e440a328 | 11756 | if (*pv == NULL) |
63697958 | 11757 | { |
11758 | Gogo* gogo = context->gogo(); | |
11759 | Btype* ebtype = element_type->get_backend(gogo); | |
11760 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
11761 | elt->value = expr_to_tree(zv); | |
11762 | } | |
e440a328 | 11763 | else |
11764 | { | |
11765 | tree value_tree = (*pv)->get_tree(context); | |
11766 | elt->value = Expression::convert_for_assignment(context, | |
11767 | element_type, | |
11768 | (*pv)->type(), | |
11769 | value_tree, | |
11770 | this->location()); | |
11771 | } | |
11772 | if (elt->value == error_mark_node) | |
11773 | return error_mark_node; | |
11774 | if (!TREE_CONSTANT(elt->value)) | |
11775 | is_constant = false; | |
ffe743ca | 11776 | if (this->indexes_ != NULL) |
11777 | ++pi; | |
e440a328 | 11778 | } |
ffe743ca | 11779 | if (this->indexes_ != NULL) |
11780 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 11781 | } |
11782 | ||
11783 | tree ret = build_constructor(type_tree, values); | |
11784 | if (is_constant) | |
11785 | TREE_CONSTANT(ret) = 1; | |
11786 | return ret; | |
11787 | } | |
11788 | ||
11789 | // Export an array construction. | |
11790 | ||
11791 | void | |
11792 | Array_construction_expression::do_export(Export* exp) const | |
11793 | { | |
11794 | exp->write_c_string("convert("); | |
11795 | exp->write_type(this->type_); | |
11796 | if (this->vals_ != NULL) | |
11797 | { | |
ffe743ca | 11798 | std::vector<unsigned long>::const_iterator pi; |
11799 | if (this->indexes_ != NULL) | |
11800 | pi = this->indexes_->begin(); | |
e440a328 | 11801 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
11802 | pv != this->vals_->end(); | |
11803 | ++pv) | |
11804 | { | |
11805 | exp->write_c_string(", "); | |
ffe743ca | 11806 | |
11807 | if (this->indexes_ != NULL) | |
11808 | { | |
11809 | char buf[100]; | |
11810 | snprintf(buf, sizeof buf, "%lu", *pi); | |
11811 | exp->write_c_string(buf); | |
11812 | exp->write_c_string(":"); | |
11813 | } | |
11814 | ||
e440a328 | 11815 | if (*pv != NULL) |
11816 | (*pv)->export_expression(exp); | |
ffe743ca | 11817 | |
11818 | if (this->indexes_ != NULL) | |
11819 | ++pi; | |
e440a328 | 11820 | } |
11821 | } | |
11822 | exp->write_c_string(")"); | |
11823 | } | |
11824 | ||
d751bb78 | 11825 | // Dump ast representation of an array construction expressin. |
11826 | ||
11827 | void | |
11828 | Array_construction_expression::do_dump_expression( | |
11829 | Ast_dump_context* ast_dump_context) const | |
11830 | { | |
ffe743ca | 11831 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 11832 | |
11833 | ast_dump_context->ostream() << "[" ; | |
11834 | if (length != NULL) | |
11835 | { | |
11836 | ast_dump_context->dump_expression(length); | |
11837 | } | |
11838 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 11839 | ast_dump_context->dump_type(this->type_); |
11840 | ast_dump_context->ostream() << "{" ; | |
ffe743ca | 11841 | if (this->indexes_ == NULL) |
11842 | ast_dump_context->dump_expression_list(this->vals_); | |
11843 | else | |
11844 | { | |
11845 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11846 | for (std::vector<unsigned long>::const_iterator pi = | |
11847 | this->indexes_->begin(); | |
11848 | pi != this->indexes_->end(); | |
11849 | ++pi, ++pv) | |
11850 | { | |
11851 | if (pi != this->indexes_->begin()) | |
11852 | ast_dump_context->ostream() << ", "; | |
11853 | ast_dump_context->ostream() << *pi << ':'; | |
11854 | ast_dump_context->dump_expression(*pv); | |
11855 | } | |
11856 | } | |
d751bb78 | 11857 | ast_dump_context->ostream() << "}" ; |
11858 | ||
11859 | } | |
11860 | ||
e440a328 | 11861 | // Construct a fixed array. |
11862 | ||
11863 | class Fixed_array_construction_expression : | |
11864 | public Array_construction_expression | |
11865 | { | |
11866 | public: | |
ffe743ca | 11867 | Fixed_array_construction_expression(Type* type, |
11868 | const std::vector<unsigned long>* indexes, | |
11869 | Expression_list* vals, Location location) | |
e440a328 | 11870 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, |
ffe743ca | 11871 | type, indexes, vals, location) |
11872 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 11873 | |
11874 | protected: | |
11875 | Expression* | |
11876 | do_copy() | |
11877 | { | |
11878 | return new Fixed_array_construction_expression(this->type(), | |
ffe743ca | 11879 | this->indexes(), |
e440a328 | 11880 | (this->vals() == NULL |
11881 | ? NULL | |
11882 | : this->vals()->copy()), | |
11883 | this->location()); | |
11884 | } | |
11885 | ||
11886 | tree | |
11887 | do_get_tree(Translate_context*); | |
11888 | }; | |
11889 | ||
11890 | // Return a tree for constructing a fixed array. | |
11891 | ||
11892 | tree | |
11893 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
11894 | { | |
9f0e0513 | 11895 | Type* type = this->type(); |
11896 | Btype* btype = type->get_backend(context->gogo()); | |
11897 | return this->get_constructor_tree(context, type_to_tree(btype)); | |
e440a328 | 11898 | } |
11899 | ||
11900 | // Construct an open array. | |
11901 | ||
11902 | class Open_array_construction_expression : public Array_construction_expression | |
11903 | { | |
11904 | public: | |
ffe743ca | 11905 | Open_array_construction_expression(Type* type, |
11906 | const std::vector<unsigned long>* indexes, | |
11907 | Expression_list* vals, Location location) | |
e440a328 | 11908 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, |
ffe743ca | 11909 | type, indexes, vals, location) |
11910 | { go_assert(type->is_slice_type()); } | |
e440a328 | 11911 | |
11912 | protected: | |
11913 | // Note that taking the address of an open array literal is invalid. | |
11914 | ||
11915 | Expression* | |
11916 | do_copy() | |
11917 | { | |
11918 | return new Open_array_construction_expression(this->type(), | |
ffe743ca | 11919 | this->indexes(), |
e440a328 | 11920 | (this->vals() == NULL |
11921 | ? NULL | |
11922 | : this->vals()->copy()), | |
11923 | this->location()); | |
11924 | } | |
11925 | ||
11926 | tree | |
11927 | do_get_tree(Translate_context*); | |
11928 | }; | |
11929 | ||
11930 | // Return a tree for constructing an open array. | |
11931 | ||
11932 | tree | |
11933 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
11934 | { | |
f9c68f17 | 11935 | Array_type* array_type = this->type()->array_type(); |
11936 | if (array_type == NULL) | |
11937 | { | |
c484d925 | 11938 | go_assert(this->type()->is_error()); |
f9c68f17 | 11939 | return error_mark_node; |
11940 | } | |
11941 | ||
11942 | Type* element_type = array_type->element_type(); | |
9f0e0513 | 11943 | Btype* belement_type = element_type->get_backend(context->gogo()); |
11944 | tree element_type_tree = type_to_tree(belement_type); | |
3d60812e | 11945 | if (element_type_tree == error_mark_node) |
11946 | return error_mark_node; | |
11947 | ||
e440a328 | 11948 | tree values; |
11949 | tree length_tree; | |
11950 | if (this->vals() == NULL || this->vals()->empty()) | |
11951 | { | |
11952 | // We need to create a unique value. | |
11953 | tree max = size_int(0); | |
11954 | tree constructor_type = build_array_type(element_type_tree, | |
11955 | build_index_type(max)); | |
11956 | if (constructor_type == error_mark_node) | |
11957 | return error_mark_node; | |
95f84544 | 11958 | vec<constructor_elt, va_gc> *vec; |
11959 | vec_alloc(vec, 1); | |
e82e4eb5 | 11960 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 11961 | constructor_elt* elt = vec->quick_push(empty); |
e440a328 | 11962 | elt->index = size_int(0); |
63697958 | 11963 | Gogo* gogo = context->gogo(); |
11964 | Btype* btype = element_type->get_backend(gogo); | |
11965 | elt->value = expr_to_tree(gogo->backend()->zero_expression(btype)); | |
e440a328 | 11966 | values = build_constructor(constructor_type, vec); |
11967 | if (TREE_CONSTANT(elt->value)) | |
11968 | TREE_CONSTANT(values) = 1; | |
11969 | length_tree = size_int(0); | |
11970 | } | |
11971 | else | |
11972 | { | |
ffe743ca | 11973 | unsigned long max_index; |
11974 | if (this->indexes() == NULL) | |
11975 | max_index = this->vals()->size() - 1; | |
11976 | else | |
00773463 | 11977 | max_index = this->indexes()->back(); |
ffe743ca | 11978 | tree max_tree = size_int(max_index); |
e440a328 | 11979 | tree constructor_type = build_array_type(element_type_tree, |
ffe743ca | 11980 | build_index_type(max_tree)); |
e440a328 | 11981 | if (constructor_type == error_mark_node) |
11982 | return error_mark_node; | |
11983 | values = this->get_constructor_tree(context, constructor_type); | |
ffe743ca | 11984 | length_tree = size_int(max_index + 1); |
e440a328 | 11985 | } |
11986 | ||
11987 | if (values == error_mark_node) | |
11988 | return error_mark_node; | |
11989 | ||
11990 | bool is_constant_initializer = TREE_CONSTANT(values); | |
d8829beb | 11991 | |
11992 | // We have to copy the initial values into heap memory if we are in | |
11993 | // a function or if the values are not constants. We also have to | |
11994 | // copy them if they may contain pointers in a non-constant context, | |
11995 | // as otherwise the garbage collector won't see them. | |
11996 | bool copy_to_heap = (context->function() != NULL | |
11997 | || !is_constant_initializer | |
11998 | || (element_type->has_pointer() | |
11999 | && !context->is_const())); | |
e440a328 | 12000 | |
12001 | if (is_constant_initializer) | |
12002 | { | |
b13c66cd | 12003 | tree tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 12004 | create_tmp_var_name("C"), TREE_TYPE(values)); |
12005 | DECL_EXTERNAL(tmp) = 0; | |
12006 | TREE_PUBLIC(tmp) = 0; | |
12007 | TREE_STATIC(tmp) = 1; | |
12008 | DECL_ARTIFICIAL(tmp) = 1; | |
d8829beb | 12009 | if (copy_to_heap) |
e440a328 | 12010 | { |
d8829beb | 12011 | // If we are not copying the value to the heap, we will only |
12012 | // initialize the value once, so we can use this directly | |
12013 | // rather than copying it. In that case we can't make it | |
12014 | // read-only, because the program is permitted to change it. | |
e440a328 | 12015 | TREE_READONLY(tmp) = 1; |
12016 | TREE_CONSTANT(tmp) = 1; | |
12017 | } | |
12018 | DECL_INITIAL(tmp) = values; | |
12019 | rest_of_decl_compilation(tmp, 1, 0); | |
12020 | values = tmp; | |
12021 | } | |
12022 | ||
12023 | tree space; | |
12024 | tree set; | |
d8829beb | 12025 | if (!copy_to_heap) |
e440a328 | 12026 | { |
d8829beb | 12027 | // the initializer will only run once. |
e440a328 | 12028 | space = build_fold_addr_expr(values); |
12029 | set = NULL_TREE; | |
12030 | } | |
12031 | else | |
12032 | { | |
12033 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
12034 | space = context->gogo()->allocate_memory(element_type, memsize, | |
12035 | this->location()); | |
12036 | space = save_expr(space); | |
12037 | ||
12038 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
b13c66cd | 12039 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
12040 | s); | |
e440a328 | 12041 | TREE_THIS_NOTRAP(ref) = 1; |
12042 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
12043 | } | |
12044 | ||
12045 | // Build a constructor for the open array. | |
12046 | ||
9f0e0513 | 12047 | tree type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
3d60812e | 12048 | if (type_tree == error_mark_node) |
12049 | return error_mark_node; | |
c484d925 | 12050 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 12051 | |
95f84544 | 12052 | vec<constructor_elt, va_gc> *init; |
12053 | vec_alloc(init, 3); | |
e440a328 | 12054 | |
e82e4eb5 | 12055 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12056 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 12057 | tree field = TYPE_FIELDS(type_tree); |
c484d925 | 12058 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 12059 | elt->index = field; |
12060 | elt->value = fold_convert(TREE_TYPE(field), space); | |
12061 | ||
95f84544 | 12062 | elt = init->quick_push(empty); |
e440a328 | 12063 | field = DECL_CHAIN(field); |
c484d925 | 12064 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 12065 | elt->index = field; |
12066 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
12067 | ||
95f84544 | 12068 | elt = init->quick_push(empty); |
e440a328 | 12069 | field = DECL_CHAIN(field); |
c484d925 | 12070 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); |
e440a328 | 12071 | elt->index = field; |
12072 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
12073 | ||
12074 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 12075 | if (constructor == error_mark_node) |
12076 | return error_mark_node; | |
d8829beb | 12077 | if (!copy_to_heap) |
e440a328 | 12078 | TREE_CONSTANT(constructor) = 1; |
12079 | ||
12080 | if (set == NULL_TREE) | |
12081 | return constructor; | |
12082 | else | |
12083 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
12084 | } | |
12085 | ||
12086 | // Make a slice composite literal. This is used by the type | |
12087 | // descriptor code. | |
12088 | ||
12089 | Expression* | |
12090 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12091 | Location location) |
e440a328 | 12092 | { |
411eb89e | 12093 | go_assert(type->is_slice_type()); |
ffe743ca | 12094 | return new Open_array_construction_expression(type, NULL, vals, location); |
e440a328 | 12095 | } |
12096 | ||
12097 | // Construct a map. | |
12098 | ||
12099 | class Map_construction_expression : public Expression | |
12100 | { | |
12101 | public: | |
12102 | Map_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 12103 | Location location) |
e440a328 | 12104 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), |
12105 | type_(type), vals_(vals) | |
c484d925 | 12106 | { go_assert(vals == NULL || vals->size() % 2 == 0); } |
e440a328 | 12107 | |
12108 | protected: | |
12109 | int | |
12110 | do_traverse(Traverse* traverse); | |
12111 | ||
12112 | Type* | |
12113 | do_type() | |
12114 | { return this->type_; } | |
12115 | ||
12116 | void | |
12117 | do_determine_type(const Type_context*); | |
12118 | ||
12119 | void | |
12120 | do_check_types(Gogo*); | |
12121 | ||
12122 | Expression* | |
12123 | do_copy() | |
12124 | { | |
12125 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
12126 | this->location()); | |
12127 | } | |
12128 | ||
12129 | tree | |
12130 | do_get_tree(Translate_context*); | |
12131 | ||
12132 | void | |
12133 | do_export(Export*) const; | |
12134 | ||
d751bb78 | 12135 | void |
12136 | do_dump_expression(Ast_dump_context*) const; | |
12137 | ||
e440a328 | 12138 | private: |
12139 | // The type of the map to construct. | |
12140 | Type* type_; | |
12141 | // The list of values. | |
12142 | Expression_list* vals_; | |
12143 | }; | |
12144 | ||
12145 | // Traversal. | |
12146 | ||
12147 | int | |
12148 | Map_construction_expression::do_traverse(Traverse* traverse) | |
12149 | { | |
12150 | if (this->vals_ != NULL | |
12151 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12152 | return TRAVERSE_EXIT; | |
12153 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12154 | return TRAVERSE_EXIT; | |
12155 | return TRAVERSE_CONTINUE; | |
12156 | } | |
12157 | ||
12158 | // Final type determination. | |
12159 | ||
12160 | void | |
12161 | Map_construction_expression::do_determine_type(const Type_context*) | |
12162 | { | |
12163 | if (this->vals_ == NULL) | |
12164 | return; | |
12165 | ||
12166 | Map_type* mt = this->type_->map_type(); | |
12167 | Type_context key_context(mt->key_type(), false); | |
12168 | Type_context val_context(mt->val_type(), false); | |
12169 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12170 | pv != this->vals_->end(); | |
12171 | ++pv) | |
12172 | { | |
12173 | (*pv)->determine_type(&key_context); | |
12174 | ++pv; | |
12175 | (*pv)->determine_type(&val_context); | |
12176 | } | |
12177 | } | |
12178 | ||
12179 | // Check types. | |
12180 | ||
12181 | void | |
12182 | Map_construction_expression::do_check_types(Gogo*) | |
12183 | { | |
12184 | if (this->vals_ == NULL) | |
12185 | return; | |
12186 | ||
12187 | Map_type* mt = this->type_->map_type(); | |
12188 | int i = 0; | |
12189 | Type* key_type = mt->key_type(); | |
12190 | Type* val_type = mt->val_type(); | |
12191 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12192 | pv != this->vals_->end(); | |
12193 | ++pv, ++i) | |
12194 | { | |
12195 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
12196 | { | |
12197 | error_at((*pv)->location(), | |
12198 | "incompatible type for element %d key in map construction", | |
12199 | i + 1); | |
12200 | this->set_is_error(); | |
12201 | } | |
12202 | ++pv; | |
12203 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
12204 | { | |
12205 | error_at((*pv)->location(), | |
12206 | ("incompatible type for element %d value " | |
12207 | "in map construction"), | |
12208 | i + 1); | |
12209 | this->set_is_error(); | |
12210 | } | |
12211 | } | |
12212 | } | |
12213 | ||
12214 | // Return a tree for constructing a map. | |
12215 | ||
12216 | tree | |
12217 | Map_construction_expression::do_get_tree(Translate_context* context) | |
12218 | { | |
12219 | Gogo* gogo = context->gogo(); | |
b13c66cd | 12220 | Location loc = this->location(); |
e440a328 | 12221 | |
12222 | Map_type* mt = this->type_->map_type(); | |
12223 | ||
12224 | // Build a struct to hold the key and value. | |
12225 | tree struct_type = make_node(RECORD_TYPE); | |
12226 | ||
12227 | Type* key_type = mt->key_type(); | |
12228 | tree id = get_identifier("__key"); | |
9f0e0513 | 12229 | tree key_type_tree = type_to_tree(key_type->get_backend(gogo)); |
5845bde6 | 12230 | if (key_type_tree == error_mark_node) |
12231 | return error_mark_node; | |
b13c66cd | 12232 | tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id, |
12233 | key_type_tree); | |
e440a328 | 12234 | DECL_CONTEXT(key_field) = struct_type; |
12235 | TYPE_FIELDS(struct_type) = key_field; | |
12236 | ||
12237 | Type* val_type = mt->val_type(); | |
12238 | id = get_identifier("__val"); | |
9f0e0513 | 12239 | tree val_type_tree = type_to_tree(val_type->get_backend(gogo)); |
5845bde6 | 12240 | if (val_type_tree == error_mark_node) |
12241 | return error_mark_node; | |
b13c66cd | 12242 | tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id, |
12243 | val_type_tree); | |
e440a328 | 12244 | DECL_CONTEXT(val_field) = struct_type; |
12245 | DECL_CHAIN(key_field) = val_field; | |
12246 | ||
12247 | layout_type(struct_type); | |
12248 | ||
12249 | bool is_constant = true; | |
12250 | size_t i = 0; | |
12251 | tree valaddr; | |
12252 | tree make_tmp; | |
12253 | ||
12254 | if (this->vals_ == NULL || this->vals_->empty()) | |
12255 | { | |
12256 | valaddr = null_pointer_node; | |
12257 | make_tmp = NULL_TREE; | |
12258 | } | |
12259 | else | |
12260 | { | |
95f84544 | 12261 | vec<constructor_elt, va_gc> *values; |
12262 | vec_alloc(values, this->vals_->size() / 2); | |
e440a328 | 12263 | |
12264 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12265 | pv != this->vals_->end(); | |
12266 | ++pv, ++i) | |
12267 | { | |
12268 | bool one_is_constant = true; | |
12269 | ||
95f84544 | 12270 | vec<constructor_elt, va_gc> *one; |
12271 | vec_alloc(one, 2); | |
e440a328 | 12272 | |
e82e4eb5 | 12273 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12274 | constructor_elt* elt = one->quick_push(empty); |
e440a328 | 12275 | elt->index = key_field; |
12276 | tree val_tree = (*pv)->get_tree(context); | |
12277 | elt->value = Expression::convert_for_assignment(context, key_type, | |
12278 | (*pv)->type(), | |
12279 | val_tree, loc); | |
12280 | if (elt->value == error_mark_node) | |
12281 | return error_mark_node; | |
12282 | if (!TREE_CONSTANT(elt->value)) | |
12283 | one_is_constant = false; | |
12284 | ||
12285 | ++pv; | |
12286 | ||
95f84544 | 12287 | elt = one->quick_push(empty); |
e440a328 | 12288 | elt->index = val_field; |
12289 | val_tree = (*pv)->get_tree(context); | |
12290 | elt->value = Expression::convert_for_assignment(context, val_type, | |
12291 | (*pv)->type(), | |
12292 | val_tree, loc); | |
12293 | if (elt->value == error_mark_node) | |
12294 | return error_mark_node; | |
12295 | if (!TREE_CONSTANT(elt->value)) | |
12296 | one_is_constant = false; | |
12297 | ||
95f84544 | 12298 | elt = values->quick_push(empty); |
e440a328 | 12299 | elt->index = size_int(i); |
12300 | elt->value = build_constructor(struct_type, one); | |
12301 | if (one_is_constant) | |
12302 | TREE_CONSTANT(elt->value) = 1; | |
12303 | else | |
12304 | is_constant = false; | |
12305 | } | |
12306 | ||
12307 | tree index_type = build_index_type(size_int(i - 1)); | |
12308 | tree array_type = build_array_type(struct_type, index_type); | |
12309 | tree init = build_constructor(array_type, values); | |
12310 | if (is_constant) | |
12311 | TREE_CONSTANT(init) = 1; | |
12312 | tree tmp; | |
12313 | if (current_function_decl != NULL) | |
12314 | { | |
12315 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
12316 | DECL_INITIAL(tmp) = init; | |
b13c66cd | 12317 | make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR, |
12318 | void_type_node, tmp); | |
e440a328 | 12319 | TREE_ADDRESSABLE(tmp) = 1; |
12320 | } | |
12321 | else | |
12322 | { | |
b13c66cd | 12323 | tmp = build_decl(loc.gcc_location(), VAR_DECL, |
12324 | create_tmp_var_name("M"), array_type); | |
e440a328 | 12325 | DECL_EXTERNAL(tmp) = 0; |
12326 | TREE_PUBLIC(tmp) = 0; | |
12327 | TREE_STATIC(tmp) = 1; | |
12328 | DECL_ARTIFICIAL(tmp) = 1; | |
12329 | if (!TREE_CONSTANT(init)) | |
b13c66cd | 12330 | make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR, |
12331 | void_type_node, tmp, init); | |
e440a328 | 12332 | else |
12333 | { | |
12334 | TREE_READONLY(tmp) = 1; | |
12335 | TREE_CONSTANT(tmp) = 1; | |
12336 | DECL_INITIAL(tmp) = init; | |
12337 | make_tmp = NULL_TREE; | |
12338 | } | |
12339 | rest_of_decl_compilation(tmp, 1, 0); | |
12340 | } | |
12341 | ||
12342 | valaddr = build_fold_addr_expr(tmp); | |
12343 | } | |
12344 | ||
2b5f213d | 12345 | tree descriptor = mt->map_descriptor_pointer(gogo, loc); |
e440a328 | 12346 | |
9f0e0513 | 12347 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
5845bde6 | 12348 | if (type_tree == error_mark_node) |
12349 | return error_mark_node; | |
e440a328 | 12350 | |
12351 | static tree construct_map_fndecl; | |
12352 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
12353 | loc, | |
12354 | "__go_construct_map", | |
12355 | 6, | |
12356 | type_tree, | |
12357 | TREE_TYPE(descriptor), | |
12358 | descriptor, | |
12359 | sizetype, | |
12360 | size_int(i), | |
12361 | sizetype, | |
12362 | TYPE_SIZE_UNIT(struct_type), | |
12363 | sizetype, | |
12364 | byte_position(val_field), | |
12365 | sizetype, | |
12366 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
12367 | const_ptr_type_node, | |
12368 | fold_convert(const_ptr_type_node, valaddr)); | |
5fb82b5e | 12369 | if (call == error_mark_node) |
12370 | return error_mark_node; | |
e440a328 | 12371 | |
12372 | tree ret; | |
12373 | if (make_tmp == NULL) | |
12374 | ret = call; | |
12375 | else | |
b13c66cd | 12376 | ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree, |
12377 | make_tmp, call); | |
e440a328 | 12378 | return ret; |
12379 | } | |
12380 | ||
12381 | // Export an array construction. | |
12382 | ||
12383 | void | |
12384 | Map_construction_expression::do_export(Export* exp) const | |
12385 | { | |
12386 | exp->write_c_string("convert("); | |
12387 | exp->write_type(this->type_); | |
12388 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12389 | pv != this->vals_->end(); | |
12390 | ++pv) | |
12391 | { | |
12392 | exp->write_c_string(", "); | |
12393 | (*pv)->export_expression(exp); | |
12394 | } | |
12395 | exp->write_c_string(")"); | |
12396 | } | |
12397 | ||
d751bb78 | 12398 | // Dump ast representation for a map construction expression. |
12399 | ||
12400 | void | |
12401 | Map_construction_expression::do_dump_expression( | |
12402 | Ast_dump_context* ast_dump_context) const | |
12403 | { | |
d751bb78 | 12404 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 12405 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 12406 | ast_dump_context->ostream() << "}"; |
12407 | } | |
12408 | ||
e440a328 | 12409 | // A general composite literal. This is lowered to a type specific |
12410 | // version. | |
12411 | ||
12412 | class Composite_literal_expression : public Parser_expression | |
12413 | { | |
12414 | public: | |
12415 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
b13c66cd | 12416 | Expression_list* vals, Location location) |
e440a328 | 12417 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), |
12418 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys) | |
12419 | { } | |
12420 | ||
12421 | protected: | |
12422 | int | |
12423 | do_traverse(Traverse* traverse); | |
12424 | ||
12425 | Expression* | |
ceeb4318 | 12426 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12427 | |
12428 | Expression* | |
12429 | do_copy() | |
12430 | { | |
12431 | return new Composite_literal_expression(this->type_, this->depth_, | |
12432 | this->has_keys_, | |
12433 | (this->vals_ == NULL | |
12434 | ? NULL | |
12435 | : this->vals_->copy()), | |
12436 | this->location()); | |
12437 | } | |
12438 | ||
d751bb78 | 12439 | void |
12440 | do_dump_expression(Ast_dump_context*) const; | |
12441 | ||
e440a328 | 12442 | private: |
12443 | Expression* | |
81c4b26b | 12444 | lower_struct(Gogo*, Type*); |
e440a328 | 12445 | |
12446 | Expression* | |
113ef6a5 | 12447 | lower_array(Type*); |
e440a328 | 12448 | |
12449 | Expression* | |
ffe743ca | 12450 | make_array(Type*, const std::vector<unsigned long>*, Expression_list*); |
e440a328 | 12451 | |
12452 | Expression* | |
ceeb4318 | 12453 | lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); |
e440a328 | 12454 | |
12455 | // The type of the composite literal. | |
12456 | Type* type_; | |
12457 | // The depth within a list of composite literals within a composite | |
12458 | // literal, when the type is omitted. | |
12459 | int depth_; | |
12460 | // The values to put in the composite literal. | |
12461 | Expression_list* vals_; | |
12462 | // If this is true, then VALS_ is a list of pairs: a key and a | |
12463 | // value. In an array initializer, a missing key will be NULL. | |
12464 | bool has_keys_; | |
12465 | }; | |
12466 | ||
12467 | // Traversal. | |
12468 | ||
12469 | int | |
12470 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
12471 | { | |
12472 | if (this->vals_ != NULL | |
12473 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12474 | return TRAVERSE_EXIT; | |
12475 | return Type::traverse(this->type_, traverse); | |
12476 | } | |
12477 | ||
12478 | // Lower a generic composite literal into a specific version based on | |
12479 | // the type. | |
12480 | ||
12481 | Expression* | |
ceeb4318 | 12482 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
12483 | Statement_inserter* inserter, int) | |
e440a328 | 12484 | { |
12485 | Type* type = this->type_; | |
12486 | ||
12487 | for (int depth = this->depth_; depth > 0; --depth) | |
12488 | { | |
12489 | if (type->array_type() != NULL) | |
12490 | type = type->array_type()->element_type(); | |
12491 | else if (type->map_type() != NULL) | |
12492 | type = type->map_type()->val_type(); | |
12493 | else | |
12494 | { | |
5c13bd80 | 12495 | if (!type->is_error()) |
e440a328 | 12496 | error_at(this->location(), |
12497 | ("may only omit types within composite literals " | |
12498 | "of slice, array, or map type")); | |
12499 | return Expression::make_error(this->location()); | |
12500 | } | |
12501 | } | |
12502 | ||
e00772b3 | 12503 | Type *pt = type->points_to(); |
12504 | bool is_pointer = false; | |
12505 | if (pt != NULL) | |
12506 | { | |
12507 | is_pointer = true; | |
12508 | type = pt; | |
12509 | } | |
12510 | ||
12511 | Expression* ret; | |
5c13bd80 | 12512 | if (type->is_error()) |
e440a328 | 12513 | return Expression::make_error(this->location()); |
12514 | else if (type->struct_type() != NULL) | |
e00772b3 | 12515 | ret = this->lower_struct(gogo, type); |
e440a328 | 12516 | else if (type->array_type() != NULL) |
113ef6a5 | 12517 | ret = this->lower_array(type); |
e440a328 | 12518 | else if (type->map_type() != NULL) |
e00772b3 | 12519 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 12520 | else |
12521 | { | |
12522 | error_at(this->location(), | |
12523 | ("expected struct, slice, array, or map type " | |
12524 | "for composite literal")); | |
12525 | return Expression::make_error(this->location()); | |
12526 | } | |
e00772b3 | 12527 | |
12528 | if (is_pointer) | |
12529 | ret = Expression::make_heap_composite(ret, this->location()); | |
12530 | ||
12531 | return ret; | |
e440a328 | 12532 | } |
12533 | ||
12534 | // Lower a struct composite literal. | |
12535 | ||
12536 | Expression* | |
81c4b26b | 12537 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 12538 | { |
b13c66cd | 12539 | Location location = this->location(); |
e440a328 | 12540 | Struct_type* st = type->struct_type(); |
12541 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 12542 | { |
e6013c28 | 12543 | if (this->vals_ != NULL |
12544 | && !this->vals_->empty() | |
12545 | && type->named_type() != NULL | |
12546 | && type->named_type()->named_object()->package() != NULL) | |
12547 | { | |
12548 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
12549 | pf != st->fields()->end(); | |
12550 | ++pf) | |
07daa4e7 | 12551 | { |
e6013c28 | 12552 | if (Gogo::is_hidden_name(pf->field_name())) |
07daa4e7 | 12553 | error_at(this->location(), |
e6013c28 | 12554 | "assignment of unexported field %qs in %qs literal", |
12555 | Gogo::message_name(pf->field_name()).c_str(), | |
12556 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12557 | } |
12558 | } | |
12559 | ||
12560 | return new Struct_construction_expression(type, this->vals_, location); | |
12561 | } | |
e440a328 | 12562 | |
12563 | size_t field_count = st->field_count(); | |
12564 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 12565 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 12566 | Expression_list::const_iterator p = this->vals_->begin(); |
12567 | while (p != this->vals_->end()) | |
12568 | { | |
12569 | Expression* name_expr = *p; | |
12570 | ||
12571 | ++p; | |
c484d925 | 12572 | go_assert(p != this->vals_->end()); |
e440a328 | 12573 | Expression* val = *p; |
12574 | ||
12575 | ++p; | |
12576 | ||
12577 | if (name_expr == NULL) | |
12578 | { | |
12579 | error_at(val->location(), "mixture of field and value initializers"); | |
12580 | return Expression::make_error(location); | |
12581 | } | |
12582 | ||
12583 | bool bad_key = false; | |
12584 | std::string name; | |
81c4b26b | 12585 | const Named_object* no = NULL; |
e440a328 | 12586 | switch (name_expr->classification()) |
12587 | { | |
12588 | case EXPRESSION_UNKNOWN_REFERENCE: | |
12589 | name = name_expr->unknown_expression()->name(); | |
12590 | break; | |
12591 | ||
12592 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 12593 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 12594 | break; |
12595 | ||
12596 | case EXPRESSION_TYPE: | |
12597 | { | |
12598 | Type* t = name_expr->type(); | |
12599 | Named_type* nt = t->named_type(); | |
12600 | if (nt == NULL) | |
12601 | bad_key = true; | |
12602 | else | |
81c4b26b | 12603 | no = nt->named_object(); |
e440a328 | 12604 | } |
12605 | break; | |
12606 | ||
12607 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 12608 | no = name_expr->var_expression()->named_object(); |
e440a328 | 12609 | break; |
12610 | ||
12611 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 12612 | no = name_expr->func_expression()->named_object(); |
e440a328 | 12613 | break; |
12614 | ||
12615 | case EXPRESSION_UNARY: | |
12616 | // If there is a local variable around with the same name as | |
12617 | // the field, and this occurs in the closure, then the | |
12618 | // parser may turn the field reference into an indirection | |
12619 | // through the closure. FIXME: This is a mess. | |
12620 | { | |
12621 | bad_key = true; | |
12622 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
12623 | if (ue->op() == OPERATOR_MULT) | |
12624 | { | |
12625 | Field_reference_expression* fre = | |
12626 | ue->operand()->field_reference_expression(); | |
12627 | if (fre != NULL) | |
12628 | { | |
12629 | Struct_type* st = | |
12630 | fre->expr()->type()->deref()->struct_type(); | |
12631 | if (st != NULL) | |
12632 | { | |
12633 | const Struct_field* sf = st->field(fre->field_index()); | |
12634 | name = sf->field_name(); | |
2d29d278 | 12635 | |
12636 | // See below. FIXME. | |
12637 | if (!Gogo::is_hidden_name(name) | |
12638 | && name[0] >= 'a' | |
12639 | && name[0] <= 'z') | |
12640 | { | |
12641 | if (gogo->lookup_global(name.c_str()) != NULL) | |
12642 | name = gogo->pack_hidden_name(name, false); | |
12643 | } | |
12644 | ||
e440a328 | 12645 | char buf[20]; |
12646 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
12647 | size_t buflen = strlen(buf); | |
12648 | if (name.compare(name.length() - buflen, buflen, buf) | |
12649 | == 0) | |
12650 | { | |
12651 | name = name.substr(0, name.length() - buflen); | |
12652 | bad_key = false; | |
12653 | } | |
12654 | } | |
12655 | } | |
12656 | } | |
12657 | } | |
12658 | break; | |
12659 | ||
12660 | default: | |
12661 | bad_key = true; | |
12662 | break; | |
12663 | } | |
12664 | if (bad_key) | |
12665 | { | |
12666 | error_at(name_expr->location(), "expected struct field name"); | |
12667 | return Expression::make_error(location); | |
12668 | } | |
12669 | ||
81c4b26b | 12670 | if (no != NULL) |
12671 | { | |
12672 | name = no->name(); | |
12673 | ||
12674 | // A predefined name won't be packed. If it starts with a | |
12675 | // lower case letter we need to check for that case, because | |
2d29d278 | 12676 | // the field name will be packed. FIXME. |
81c4b26b | 12677 | if (!Gogo::is_hidden_name(name) |
12678 | && name[0] >= 'a' | |
12679 | && name[0] <= 'z') | |
12680 | { | |
12681 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
12682 | if (gno == no) | |
12683 | name = gogo->pack_hidden_name(name, false); | |
12684 | } | |
12685 | } | |
12686 | ||
e440a328 | 12687 | unsigned int index; |
12688 | const Struct_field* sf = st->find_local_field(name, &index); | |
12689 | if (sf == NULL) | |
12690 | { | |
12691 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
12692 | Gogo::message_name(name).c_str(), | |
12693 | (type->named_type() != NULL | |
12694 | ? type->named_type()->message_name().c_str() | |
12695 | : "unnamed struct")); | |
12696 | return Expression::make_error(location); | |
12697 | } | |
12698 | if (vals[index] != NULL) | |
12699 | { | |
12700 | error_at(name_expr->location(), | |
12701 | "duplicate value for field %qs in %qs", | |
12702 | Gogo::message_name(name).c_str(), | |
12703 | (type->named_type() != NULL | |
12704 | ? type->named_type()->message_name().c_str() | |
12705 | : "unnamed struct")); | |
12706 | return Expression::make_error(location); | |
12707 | } | |
12708 | ||
07daa4e7 | 12709 | if (type->named_type() != NULL |
12710 | && type->named_type()->named_object()->package() != NULL | |
12711 | && Gogo::is_hidden_name(sf->field_name())) | |
12712 | error_at(name_expr->location(), | |
12713 | "assignment of unexported field %qs in %qs literal", | |
12714 | Gogo::message_name(sf->field_name()).c_str(), | |
12715 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12716 | |
e440a328 | 12717 | vals[index] = val; |
0c4f5a19 | 12718 | traverse_order->push_back(index); |
e440a328 | 12719 | } |
12720 | ||
12721 | Expression_list* list = new Expression_list; | |
12722 | list->reserve(field_count); | |
12723 | for (size_t i = 0; i < field_count; ++i) | |
12724 | list->push_back(vals[i]); | |
12725 | ||
0c4f5a19 | 12726 | Struct_construction_expression* ret = |
12727 | new Struct_construction_expression(type, list, location); | |
12728 | ret->set_traverse_order(traverse_order); | |
12729 | return ret; | |
e440a328 | 12730 | } |
12731 | ||
00773463 | 12732 | // Used to sort an index/value array. |
12733 | ||
12734 | class Index_value_compare | |
12735 | { | |
12736 | public: | |
12737 | bool | |
12738 | operator()(const std::pair<unsigned long, Expression*>& a, | |
12739 | const std::pair<unsigned long, Expression*>& b) | |
12740 | { return a.first < b.first; } | |
12741 | }; | |
12742 | ||
e440a328 | 12743 | // Lower an array composite literal. |
12744 | ||
12745 | Expression* | |
113ef6a5 | 12746 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 12747 | { |
b13c66cd | 12748 | Location location = this->location(); |
e440a328 | 12749 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 12750 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 12751 | |
ffe743ca | 12752 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
12753 | indexes->reserve(this->vals_->size()); | |
00773463 | 12754 | bool indexes_out_of_order = false; |
ffe743ca | 12755 | Expression_list* vals = new Expression_list(); |
12756 | vals->reserve(this->vals_->size()); | |
e440a328 | 12757 | unsigned long index = 0; |
12758 | Expression_list::const_iterator p = this->vals_->begin(); | |
12759 | while (p != this->vals_->end()) | |
12760 | { | |
12761 | Expression* index_expr = *p; | |
12762 | ||
12763 | ++p; | |
c484d925 | 12764 | go_assert(p != this->vals_->end()); |
e440a328 | 12765 | Expression* val = *p; |
12766 | ||
12767 | ++p; | |
12768 | ||
ffe743ca | 12769 | if (index_expr == NULL) |
12770 | { | |
12771 | if (!indexes->empty()) | |
12772 | indexes->push_back(index); | |
12773 | } | |
12774 | else | |
e440a328 | 12775 | { |
ffe743ca | 12776 | if (indexes->empty() && !vals->empty()) |
12777 | { | |
12778 | for (size_t i = 0; i < vals->size(); ++i) | |
12779 | indexes->push_back(i); | |
12780 | } | |
12781 | ||
0c77715b | 12782 | Numeric_constant nc; |
12783 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 12784 | { |
e440a328 | 12785 | error_at(index_expr->location(), |
12786 | "index expression is not integer constant"); | |
12787 | return Expression::make_error(location); | |
12788 | } | |
6f6d9955 | 12789 | |
0c77715b | 12790 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 12791 | { |
0c77715b | 12792 | case Numeric_constant::NC_UL_VALID: |
12793 | break; | |
12794 | case Numeric_constant::NC_UL_NOTINT: | |
12795 | error_at(index_expr->location(), | |
12796 | "index expression is not integer constant"); | |
12797 | return Expression::make_error(location); | |
12798 | case Numeric_constant::NC_UL_NEGATIVE: | |
e440a328 | 12799 | error_at(index_expr->location(), "index expression is negative"); |
12800 | return Expression::make_error(location); | |
0c77715b | 12801 | case Numeric_constant::NC_UL_BIG: |
e440a328 | 12802 | error_at(index_expr->location(), "index value overflow"); |
12803 | return Expression::make_error(location); | |
0c77715b | 12804 | default: |
12805 | go_unreachable(); | |
e440a328 | 12806 | } |
6f6d9955 | 12807 | |
12808 | Named_type* ntype = Type::lookup_integer_type("int"); | |
12809 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 12810 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
12811 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 12812 | { |
6f6d9955 | 12813 | error_at(index_expr->location(), "index value overflow"); |
12814 | return Expression::make_error(location); | |
12815 | } | |
12816 | ||
ffe743ca | 12817 | if (std::find(indexes->begin(), indexes->end(), index) |
12818 | != indexes->end()) | |
e440a328 | 12819 | { |
ffe743ca | 12820 | error_at(index_expr->location(), "duplicate value for index %lu", |
e440a328 | 12821 | index); |
12822 | return Expression::make_error(location); | |
12823 | } | |
ffe743ca | 12824 | |
00773463 | 12825 | if (!indexes->empty() && index < indexes->back()) |
12826 | indexes_out_of_order = true; | |
12827 | ||
ffe743ca | 12828 | indexes->push_back(index); |
e440a328 | 12829 | } |
12830 | ||
ffe743ca | 12831 | vals->push_back(val); |
12832 | ||
e440a328 | 12833 | ++index; |
12834 | } | |
12835 | ||
ffe743ca | 12836 | if (indexes->empty()) |
12837 | { | |
12838 | delete indexes; | |
12839 | indexes = NULL; | |
12840 | } | |
e440a328 | 12841 | |
00773463 | 12842 | if (indexes_out_of_order) |
12843 | { | |
12844 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
12845 | ||
12846 | V v; | |
12847 | v.reserve(indexes->size()); | |
12848 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
12849 | for (Expression_list::const_iterator pe = vals->begin(); | |
12850 | pe != vals->end(); | |
12851 | ++pe, ++pi) | |
12852 | v.push_back(std::make_pair(*pi, *pe)); | |
12853 | ||
12854 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
12855 | ||
12856 | delete indexes; | |
12857 | delete vals; | |
12858 | indexes = new std::vector<unsigned long>(); | |
12859 | indexes->reserve(v.size()); | |
12860 | vals = new Expression_list(); | |
12861 | vals->reserve(v.size()); | |
12862 | ||
12863 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
12864 | { | |
12865 | indexes->push_back(p->first); | |
12866 | vals->push_back(p->second); | |
12867 | } | |
12868 | } | |
12869 | ||
ffe743ca | 12870 | return this->make_array(type, indexes, vals); |
e440a328 | 12871 | } |
12872 | ||
12873 | // Actually build the array composite literal. This handles | |
12874 | // [...]{...}. | |
12875 | ||
12876 | Expression* | |
ffe743ca | 12877 | Composite_literal_expression::make_array( |
12878 | Type* type, | |
12879 | const std::vector<unsigned long>* indexes, | |
12880 | Expression_list* vals) | |
e440a328 | 12881 | { |
b13c66cd | 12882 | Location location = this->location(); |
e440a328 | 12883 | Array_type* at = type->array_type(); |
ffe743ca | 12884 | |
e440a328 | 12885 | if (at->length() != NULL && at->length()->is_nil_expression()) |
12886 | { | |
ffe743ca | 12887 | size_t size; |
12888 | if (vals == NULL) | |
12889 | size = 0; | |
00773463 | 12890 | else if (indexes != NULL) |
12891 | size = indexes->back() + 1; | |
12892 | else | |
ffe743ca | 12893 | { |
12894 | size = vals->size(); | |
12895 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
12896 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
12897 | && size >> (it->bits() - 1) != 0) | |
12898 | { | |
12899 | error_at(location, "too many elements in composite literal"); | |
12900 | return Expression::make_error(location); | |
12901 | } | |
12902 | } | |
ffe743ca | 12903 | |
e440a328 | 12904 | mpz_t vlen; |
12905 | mpz_init_set_ui(vlen, size); | |
12906 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
12907 | mpz_clear(vlen); | |
12908 | at = Type::make_array_type(at->element_type(), elen); | |
12909 | type = at; | |
12910 | } | |
ffe743ca | 12911 | else if (at->length() != NULL |
12912 | && !at->length()->is_error_expression() | |
12913 | && this->vals_ != NULL) | |
12914 | { | |
12915 | Numeric_constant nc; | |
12916 | unsigned long val; | |
12917 | if (at->length()->numeric_constant_value(&nc) | |
12918 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
12919 | { | |
12920 | if (indexes == NULL) | |
12921 | { | |
12922 | if (this->vals_->size() > val) | |
12923 | { | |
12924 | error_at(location, "too many elements in composite literal"); | |
12925 | return Expression::make_error(location); | |
12926 | } | |
12927 | } | |
12928 | else | |
12929 | { | |
00773463 | 12930 | unsigned long max = indexes->back(); |
ffe743ca | 12931 | if (max >= val) |
12932 | { | |
12933 | error_at(location, | |
12934 | ("some element keys in composite literal " | |
12935 | "are out of range")); | |
12936 | return Expression::make_error(location); | |
12937 | } | |
12938 | } | |
12939 | } | |
12940 | } | |
12941 | ||
e440a328 | 12942 | if (at->length() != NULL) |
ffe743ca | 12943 | return new Fixed_array_construction_expression(type, indexes, vals, |
12944 | location); | |
e440a328 | 12945 | else |
ffe743ca | 12946 | return new Open_array_construction_expression(type, indexes, vals, |
12947 | location); | |
e440a328 | 12948 | } |
12949 | ||
12950 | // Lower a map composite literal. | |
12951 | ||
12952 | Expression* | |
a287720d | 12953 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 12954 | Statement_inserter* inserter, |
a287720d | 12955 | Type* type) |
e440a328 | 12956 | { |
b13c66cd | 12957 | Location location = this->location(); |
e440a328 | 12958 | if (this->vals_ != NULL) |
12959 | { | |
12960 | if (!this->has_keys_) | |
12961 | { | |
12962 | error_at(location, "map composite literal must have keys"); | |
12963 | return Expression::make_error(location); | |
12964 | } | |
12965 | ||
a287720d | 12966 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 12967 | p != this->vals_->end(); |
12968 | p += 2) | |
12969 | { | |
12970 | if (*p == NULL) | |
12971 | { | |
12972 | ++p; | |
12973 | error_at((*p)->location(), | |
12974 | "map composite literal must have keys for every value"); | |
12975 | return Expression::make_error(location); | |
12976 | } | |
a287720d | 12977 | // Make sure we have lowered the key; it may not have been |
12978 | // lowered in order to handle keys for struct composite | |
12979 | // literals. Lower it now to get the right error message. | |
12980 | if ((*p)->unknown_expression() != NULL) | |
12981 | { | |
12982 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 12983 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 12984 | go_assert((*p)->is_error_expression()); |
a287720d | 12985 | return Expression::make_error(location); |
12986 | } | |
e440a328 | 12987 | } |
12988 | } | |
12989 | ||
12990 | return new Map_construction_expression(type, this->vals_, location); | |
12991 | } | |
12992 | ||
d751bb78 | 12993 | // Dump ast representation for a composite literal expression. |
12994 | ||
12995 | void | |
12996 | Composite_literal_expression::do_dump_expression( | |
12997 | Ast_dump_context* ast_dump_context) const | |
12998 | { | |
8b1c301d | 12999 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13000 | ast_dump_context->dump_type(this->type_); |
13001 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13002 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13003 | ast_dump_context->ostream() << "})"; |
13004 | } | |
13005 | ||
e440a328 | 13006 | // Make a composite literal expression. |
13007 | ||
13008 | Expression* | |
13009 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
13010 | Expression_list* vals, | |
b13c66cd | 13011 | Location location) |
e440a328 | 13012 | { |
13013 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
13014 | location); | |
13015 | } | |
13016 | ||
13017 | // Return whether this expression is a composite literal. | |
13018 | ||
13019 | bool | |
13020 | Expression::is_composite_literal() const | |
13021 | { | |
13022 | switch (this->classification_) | |
13023 | { | |
13024 | case EXPRESSION_COMPOSITE_LITERAL: | |
13025 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13026 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13027 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
13028 | case EXPRESSION_MAP_CONSTRUCTION: | |
13029 | return true; | |
13030 | default: | |
13031 | return false; | |
13032 | } | |
13033 | } | |
13034 | ||
13035 | // Return whether this expression is a composite literal which is not | |
13036 | // constant. | |
13037 | ||
13038 | bool | |
13039 | Expression::is_nonconstant_composite_literal() const | |
13040 | { | |
13041 | switch (this->classification_) | |
13042 | { | |
13043 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13044 | { | |
13045 | const Struct_construction_expression *psce = | |
13046 | static_cast<const Struct_construction_expression*>(this); | |
13047 | return !psce->is_constant_struct(); | |
13048 | } | |
13049 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13050 | { | |
13051 | const Fixed_array_construction_expression *pace = | |
13052 | static_cast<const Fixed_array_construction_expression*>(this); | |
13053 | return !pace->is_constant_array(); | |
13054 | } | |
13055 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
13056 | { | |
13057 | const Open_array_construction_expression *pace = | |
13058 | static_cast<const Open_array_construction_expression*>(this); | |
13059 | return !pace->is_constant_array(); | |
13060 | } | |
13061 | case EXPRESSION_MAP_CONSTRUCTION: | |
13062 | return true; | |
13063 | default: | |
13064 | return false; | |
13065 | } | |
13066 | } | |
13067 | ||
13068 | // Return true if this is a reference to a local variable. | |
13069 | ||
13070 | bool | |
13071 | Expression::is_local_variable() const | |
13072 | { | |
13073 | const Var_expression* ve = this->var_expression(); | |
13074 | if (ve == NULL) | |
13075 | return false; | |
13076 | const Named_object* no = ve->named_object(); | |
13077 | return (no->is_result_variable() | |
13078 | || (no->is_variable() && !no->var_value()->is_global())); | |
13079 | } | |
13080 | ||
13081 | // Class Type_guard_expression. | |
13082 | ||
13083 | // Traversal. | |
13084 | ||
13085 | int | |
13086 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13087 | { | |
13088 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13089 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13090 | return TRAVERSE_EXIT; | |
13091 | return TRAVERSE_CONTINUE; | |
13092 | } | |
13093 | ||
13094 | // Check types of a type guard expression. The expression must have | |
13095 | // an interface type, but the actual type conversion is checked at run | |
13096 | // time. | |
13097 | ||
13098 | void | |
13099 | Type_guard_expression::do_check_types(Gogo*) | |
13100 | { | |
e440a328 | 13101 | Type* expr_type = this->expr_->type(); |
7e9da23f | 13102 | if (expr_type->interface_type() == NULL) |
f725ade8 | 13103 | { |
5c13bd80 | 13104 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 13105 | this->report_error(_("type assertion only valid for interface types")); |
13106 | this->set_is_error(); | |
13107 | } | |
e440a328 | 13108 | else if (this->type_->interface_type() == NULL) |
13109 | { | |
13110 | std::string reason; | |
13111 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
13112 | &reason)) | |
13113 | { | |
5c13bd80 | 13114 | if (!this->type_->is_error()) |
e440a328 | 13115 | { |
f725ade8 | 13116 | if (reason.empty()) |
13117 | this->report_error(_("impossible type assertion: " | |
13118 | "type does not implement interface")); | |
13119 | else | |
13120 | error_at(this->location(), | |
13121 | ("impossible type assertion: " | |
13122 | "type does not implement interface (%s)"), | |
13123 | reason.c_str()); | |
e440a328 | 13124 | } |
f725ade8 | 13125 | this->set_is_error(); |
e440a328 | 13126 | } |
13127 | } | |
13128 | } | |
13129 | ||
13130 | // Return a tree for a type guard expression. | |
13131 | ||
13132 | tree | |
13133 | Type_guard_expression::do_get_tree(Translate_context* context) | |
13134 | { | |
e440a328 | 13135 | tree expr_tree = this->expr_->get_tree(context); |
13136 | if (expr_tree == error_mark_node) | |
13137 | return error_mark_node; | |
7e9da23f | 13138 | if (this->type_->interface_type() != NULL) |
e440a328 | 13139 | return Expression::convert_interface_to_interface(context, this->type_, |
13140 | this->expr_->type(), | |
13141 | expr_tree, true, | |
13142 | this->location()); | |
13143 | else | |
13144 | return Expression::convert_for_assignment(context, this->type_, | |
13145 | this->expr_->type(), expr_tree, | |
13146 | this->location()); | |
13147 | } | |
13148 | ||
d751bb78 | 13149 | // Dump ast representation for a type guard expression. |
13150 | ||
13151 | void | |
13152 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
13153 | const | |
13154 | { | |
13155 | this->expr_->dump_expression(ast_dump_context); | |
13156 | ast_dump_context->ostream() << "."; | |
13157 | ast_dump_context->dump_type(this->type_); | |
13158 | } | |
13159 | ||
e440a328 | 13160 | // Make a type guard expression. |
13161 | ||
13162 | Expression* | |
13163 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 13164 | Location location) |
e440a328 | 13165 | { |
13166 | return new Type_guard_expression(expr, type, location); | |
13167 | } | |
13168 | ||
13169 | // Class Heap_composite_expression. | |
13170 | ||
13171 | // When you take the address of a composite literal, it is allocated | |
13172 | // on the heap. This class implements that. | |
13173 | ||
13174 | class Heap_composite_expression : public Expression | |
13175 | { | |
13176 | public: | |
b13c66cd | 13177 | Heap_composite_expression(Expression* expr, Location location) |
e440a328 | 13178 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), |
13179 | expr_(expr) | |
13180 | { } | |
13181 | ||
13182 | protected: | |
13183 | int | |
13184 | do_traverse(Traverse* traverse) | |
13185 | { return Expression::traverse(&this->expr_, traverse); } | |
13186 | ||
13187 | Type* | |
13188 | do_type() | |
13189 | { return Type::make_pointer_type(this->expr_->type()); } | |
13190 | ||
13191 | void | |
13192 | do_determine_type(const Type_context*) | |
13193 | { this->expr_->determine_type_no_context(); } | |
13194 | ||
13195 | Expression* | |
13196 | do_copy() | |
13197 | { | |
13198 | return Expression::make_heap_composite(this->expr_->copy(), | |
13199 | this->location()); | |
13200 | } | |
13201 | ||
13202 | tree | |
13203 | do_get_tree(Translate_context*); | |
13204 | ||
13205 | // We only export global objects, and the parser does not generate | |
13206 | // this in global scope. | |
13207 | void | |
13208 | do_export(Export*) const | |
c3e6f413 | 13209 | { go_unreachable(); } |
e440a328 | 13210 | |
d751bb78 | 13211 | void |
13212 | do_dump_expression(Ast_dump_context*) const; | |
13213 | ||
e440a328 | 13214 | private: |
13215 | // The composite literal which is being put on the heap. | |
13216 | Expression* expr_; | |
13217 | }; | |
13218 | ||
13219 | // Return a tree which allocates a composite literal on the heap. | |
13220 | ||
13221 | tree | |
13222 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
13223 | { | |
13224 | tree expr_tree = this->expr_->get_tree(context); | |
6d3ed74c | 13225 | if (expr_tree == error_mark_node || TREE_TYPE(expr_tree) == error_mark_node) |
e440a328 | 13226 | return error_mark_node; |
13227 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
c484d925 | 13228 | go_assert(TREE_CODE(expr_size) == INTEGER_CST); |
e440a328 | 13229 | tree space = context->gogo()->allocate_memory(this->expr_->type(), |
13230 | expr_size, this->location()); | |
13231 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
13232 | space = save_expr(space); | |
b13c66cd | 13233 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
13234 | space); | |
e440a328 | 13235 | TREE_THIS_NOTRAP(ref) = 1; |
13236 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
13237 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
13238 | space); | |
b13c66cd | 13239 | SET_EXPR_LOCATION(ret, this->location().gcc_location()); |
e440a328 | 13240 | return ret; |
13241 | } | |
13242 | ||
d751bb78 | 13243 | // Dump ast representation for a heap composite expression. |
13244 | ||
13245 | void | |
13246 | Heap_composite_expression::do_dump_expression( | |
13247 | Ast_dump_context* ast_dump_context) const | |
13248 | { | |
13249 | ast_dump_context->ostream() << "&("; | |
13250 | ast_dump_context->dump_expression(this->expr_); | |
13251 | ast_dump_context->ostream() << ")"; | |
13252 | } | |
13253 | ||
e440a328 | 13254 | // Allocate a composite literal on the heap. |
13255 | ||
13256 | Expression* | |
b13c66cd | 13257 | Expression::make_heap_composite(Expression* expr, Location location) |
e440a328 | 13258 | { |
13259 | return new Heap_composite_expression(expr, location); | |
13260 | } | |
13261 | ||
13262 | // Class Receive_expression. | |
13263 | ||
13264 | // Return the type of a receive expression. | |
13265 | ||
13266 | Type* | |
13267 | Receive_expression::do_type() | |
13268 | { | |
13269 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13270 | if (channel_type == NULL) | |
13271 | return Type::make_error_type(); | |
13272 | return channel_type->element_type(); | |
13273 | } | |
13274 | ||
13275 | // Check types for a receive expression. | |
13276 | ||
13277 | void | |
13278 | Receive_expression::do_check_types(Gogo*) | |
13279 | { | |
13280 | Type* type = this->channel_->type(); | |
5c13bd80 | 13281 | if (type->is_error()) |
e440a328 | 13282 | { |
13283 | this->set_is_error(); | |
13284 | return; | |
13285 | } | |
13286 | if (type->channel_type() == NULL) | |
13287 | { | |
13288 | this->report_error(_("expected channel")); | |
13289 | return; | |
13290 | } | |
13291 | if (!type->channel_type()->may_receive()) | |
13292 | { | |
13293 | this->report_error(_("invalid receive on send-only channel")); | |
13294 | return; | |
13295 | } | |
13296 | } | |
13297 | ||
13298 | // Get a tree for a receive expression. | |
13299 | ||
13300 | tree | |
13301 | Receive_expression::do_get_tree(Translate_context* context) | |
13302 | { | |
f24f10bb | 13303 | Location loc = this->location(); |
13304 | ||
e440a328 | 13305 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 13306 | if (channel_type == NULL) |
13307 | { | |
c484d925 | 13308 | go_assert(this->channel_->type()->is_error()); |
5b8368f4 | 13309 | return error_mark_node; |
13310 | } | |
f24f10bb | 13311 | |
13312 | Expression* td = Expression::make_type_descriptor(channel_type, loc); | |
13313 | tree td_tree = td->get_tree(context); | |
13314 | ||
e440a328 | 13315 | Type* element_type = channel_type->element_type(); |
9f0e0513 | 13316 | Btype* element_type_btype = element_type->get_backend(context->gogo()); |
13317 | tree element_type_tree = type_to_tree(element_type_btype); | |
e440a328 | 13318 | |
13319 | tree channel = this->channel_->get_tree(context); | |
13320 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
13321 | return error_mark_node; | |
13322 | ||
f24f10bb | 13323 | return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc); |
e440a328 | 13324 | } |
13325 | ||
d751bb78 | 13326 | // Dump ast representation for a receive expression. |
13327 | ||
13328 | void | |
13329 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13330 | { | |
13331 | ast_dump_context->ostream() << " <- " ; | |
13332 | ast_dump_context->dump_expression(channel_); | |
13333 | } | |
13334 | ||
e440a328 | 13335 | // Make a receive expression. |
13336 | ||
13337 | Receive_expression* | |
b13c66cd | 13338 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 13339 | { |
13340 | return new Receive_expression(channel, location); | |
13341 | } | |
13342 | ||
e440a328 | 13343 | // An expression which evaluates to a pointer to the type descriptor |
13344 | // of a type. | |
13345 | ||
13346 | class Type_descriptor_expression : public Expression | |
13347 | { | |
13348 | public: | |
b13c66cd | 13349 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 13350 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
13351 | type_(type) | |
13352 | { } | |
13353 | ||
13354 | protected: | |
13355 | Type* | |
13356 | do_type() | |
13357 | { return Type::make_type_descriptor_ptr_type(); } | |
13358 | ||
13359 | void | |
13360 | do_determine_type(const Type_context*) | |
13361 | { } | |
13362 | ||
13363 | Expression* | |
13364 | do_copy() | |
13365 | { return this; } | |
13366 | ||
13367 | tree | |
13368 | do_get_tree(Translate_context* context) | |
a1d23b41 | 13369 | { |
13370 | return this->type_->type_descriptor_pointer(context->gogo(), | |
13371 | this->location()); | |
13372 | } | |
e440a328 | 13373 | |
d751bb78 | 13374 | void |
13375 | do_dump_expression(Ast_dump_context*) const; | |
13376 | ||
e440a328 | 13377 | private: |
13378 | // The type for which this is the descriptor. | |
13379 | Type* type_; | |
13380 | }; | |
13381 | ||
d751bb78 | 13382 | // Dump ast representation for a type descriptor expression. |
13383 | ||
13384 | void | |
13385 | Type_descriptor_expression::do_dump_expression( | |
13386 | Ast_dump_context* ast_dump_context) const | |
13387 | { | |
13388 | ast_dump_context->dump_type(this->type_); | |
13389 | } | |
13390 | ||
e440a328 | 13391 | // Make a type descriptor expression. |
13392 | ||
13393 | Expression* | |
b13c66cd | 13394 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 13395 | { |
13396 | return new Type_descriptor_expression(type, location); | |
13397 | } | |
13398 | ||
13399 | // An expression which evaluates to some characteristic of a type. | |
13400 | // This is only used to initialize fields of a type descriptor. Using | |
13401 | // a new expression class is slightly inefficient but gives us a good | |
13402 | // separation between the frontend and the middle-end with regard to | |
13403 | // how types are laid out. | |
13404 | ||
13405 | class Type_info_expression : public Expression | |
13406 | { | |
13407 | public: | |
13408 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 13409 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 13410 | type_(type), type_info_(type_info) |
13411 | { } | |
13412 | ||
13413 | protected: | |
13414 | Type* | |
13415 | do_type(); | |
13416 | ||
13417 | void | |
13418 | do_determine_type(const Type_context*) | |
13419 | { } | |
13420 | ||
13421 | Expression* | |
13422 | do_copy() | |
13423 | { return this; } | |
13424 | ||
13425 | tree | |
13426 | do_get_tree(Translate_context* context); | |
13427 | ||
d751bb78 | 13428 | void |
13429 | do_dump_expression(Ast_dump_context*) const; | |
13430 | ||
e440a328 | 13431 | private: |
13432 | // The type for which we are getting information. | |
13433 | Type* type_; | |
13434 | // What information we want. | |
13435 | Type_info type_info_; | |
13436 | }; | |
13437 | ||
13438 | // The type is chosen to match what the type descriptor struct | |
13439 | // expects. | |
13440 | ||
13441 | Type* | |
13442 | Type_info_expression::do_type() | |
13443 | { | |
13444 | switch (this->type_info_) | |
13445 | { | |
13446 | case TYPE_INFO_SIZE: | |
13447 | return Type::lookup_integer_type("uintptr"); | |
13448 | case TYPE_INFO_ALIGNMENT: | |
13449 | case TYPE_INFO_FIELD_ALIGNMENT: | |
13450 | return Type::lookup_integer_type("uint8"); | |
13451 | default: | |
c3e6f413 | 13452 | go_unreachable(); |
e440a328 | 13453 | } |
13454 | } | |
13455 | ||
13456 | // Return type information in GENERIC. | |
13457 | ||
13458 | tree | |
13459 | Type_info_expression::do_get_tree(Translate_context* context) | |
13460 | { | |
927a01eb | 13461 | Btype* btype = this->type_->get_backend(context->gogo()); |
13462 | Gogo* gogo = context->gogo(); | |
13463 | size_t val; | |
13464 | switch (this->type_info_) | |
e440a328 | 13465 | { |
927a01eb | 13466 | case TYPE_INFO_SIZE: |
13467 | val = gogo->backend()->type_size(btype); | |
13468 | break; | |
13469 | case TYPE_INFO_ALIGNMENT: | |
13470 | val = gogo->backend()->type_alignment(btype); | |
13471 | break; | |
13472 | case TYPE_INFO_FIELD_ALIGNMENT: | |
13473 | val = gogo->backend()->type_field_alignment(btype); | |
13474 | break; | |
13475 | default: | |
13476 | go_unreachable(); | |
e440a328 | 13477 | } |
927a01eb | 13478 | tree val_type_tree = type_to_tree(this->type()->get_backend(gogo)); |
13479 | go_assert(val_type_tree != error_mark_node); | |
13480 | return build_int_cstu(val_type_tree, val); | |
e440a328 | 13481 | } |
13482 | ||
d751bb78 | 13483 | // Dump ast representation for a type info expression. |
13484 | ||
13485 | void | |
13486 | Type_info_expression::do_dump_expression( | |
13487 | Ast_dump_context* ast_dump_context) const | |
13488 | { | |
13489 | ast_dump_context->ostream() << "typeinfo("; | |
13490 | ast_dump_context->dump_type(this->type_); | |
13491 | ast_dump_context->ostream() << ","; | |
13492 | ast_dump_context->ostream() << | |
13493 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
13494 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
13495 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
13496 | : "unknown"); | |
13497 | ast_dump_context->ostream() << ")"; | |
13498 | } | |
13499 | ||
e440a328 | 13500 | // Make a type info expression. |
13501 | ||
13502 | Expression* | |
13503 | Expression::make_type_info(Type* type, Type_info type_info) | |
13504 | { | |
13505 | return new Type_info_expression(type, type_info); | |
13506 | } | |
13507 | ||
13508 | // An expression which evaluates to the offset of a field within a | |
13509 | // struct. This, like Type_info_expression, q.v., is only used to | |
13510 | // initialize fields of a type descriptor. | |
13511 | ||
13512 | class Struct_field_offset_expression : public Expression | |
13513 | { | |
13514 | public: | |
13515 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 13516 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
13517 | Linemap::predeclared_location()), | |
e440a328 | 13518 | type_(type), field_(field) |
13519 | { } | |
13520 | ||
13521 | protected: | |
13522 | Type* | |
13523 | do_type() | |
13524 | { return Type::lookup_integer_type("uintptr"); } | |
13525 | ||
13526 | void | |
13527 | do_determine_type(const Type_context*) | |
13528 | { } | |
13529 | ||
13530 | Expression* | |
13531 | do_copy() | |
13532 | { return this; } | |
13533 | ||
13534 | tree | |
13535 | do_get_tree(Translate_context* context); | |
13536 | ||
d751bb78 | 13537 | void |
13538 | do_dump_expression(Ast_dump_context*) const; | |
13539 | ||
e440a328 | 13540 | private: |
13541 | // The type of the struct. | |
13542 | Struct_type* type_; | |
13543 | // The field. | |
13544 | const Struct_field* field_; | |
13545 | }; | |
13546 | ||
13547 | // Return a struct field offset in GENERIC. | |
13548 | ||
13549 | tree | |
13550 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
13551 | { | |
9f0e0513 | 13552 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
e440a328 | 13553 | if (type_tree == error_mark_node) |
13554 | return error_mark_node; | |
13555 | ||
9f0e0513 | 13556 | tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
c484d925 | 13557 | go_assert(val_type_tree != error_mark_node); |
e440a328 | 13558 | |
13559 | const Struct_field_list* fields = this->type_->fields(); | |
13560 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
13561 | Struct_field_list::const_iterator p; | |
13562 | for (p = fields->begin(); | |
13563 | p != fields->end(); | |
13564 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
13565 | { | |
c484d925 | 13566 | go_assert(struct_field_tree != NULL_TREE); |
e440a328 | 13567 | if (&*p == this->field_) |
13568 | break; | |
13569 | } | |
c484d925 | 13570 | go_assert(&*p == this->field_); |
e440a328 | 13571 | |
13572 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
13573 | byte_position(struct_field_tree)); | |
13574 | } | |
13575 | ||
d751bb78 | 13576 | // Dump ast representation for a struct field offset expression. |
13577 | ||
13578 | void | |
13579 | Struct_field_offset_expression::do_dump_expression( | |
13580 | Ast_dump_context* ast_dump_context) const | |
13581 | { | |
13582 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 13583 | ast_dump_context->dump_type(this->type_); |
13584 | ast_dump_context->ostream() << '.'; | |
13585 | ast_dump_context->ostream() << | |
13586 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 13587 | ast_dump_context->ostream() << ")"; |
13588 | } | |
13589 | ||
e440a328 | 13590 | // Make an expression for a struct field offset. |
13591 | ||
13592 | Expression* | |
13593 | Expression::make_struct_field_offset(Struct_type* type, | |
13594 | const Struct_field* field) | |
13595 | { | |
13596 | return new Struct_field_offset_expression(type, field); | |
13597 | } | |
13598 | ||
a9182619 | 13599 | // An expression which evaluates to a pointer to the map descriptor of |
13600 | // a map type. | |
13601 | ||
13602 | class Map_descriptor_expression : public Expression | |
13603 | { | |
13604 | public: | |
b13c66cd | 13605 | Map_descriptor_expression(Map_type* type, Location location) |
a9182619 | 13606 | : Expression(EXPRESSION_MAP_DESCRIPTOR, location), |
13607 | type_(type) | |
13608 | { } | |
13609 | ||
13610 | protected: | |
13611 | Type* | |
13612 | do_type() | |
13613 | { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } | |
13614 | ||
13615 | void | |
13616 | do_determine_type(const Type_context*) | |
13617 | { } | |
13618 | ||
13619 | Expression* | |
13620 | do_copy() | |
13621 | { return this; } | |
13622 | ||
13623 | tree | |
13624 | do_get_tree(Translate_context* context) | |
13625 | { | |
13626 | return this->type_->map_descriptor_pointer(context->gogo(), | |
13627 | this->location()); | |
13628 | } | |
13629 | ||
d751bb78 | 13630 | void |
13631 | do_dump_expression(Ast_dump_context*) const; | |
13632 | ||
a9182619 | 13633 | private: |
13634 | // The type for which this is the descriptor. | |
13635 | Map_type* type_; | |
13636 | }; | |
13637 | ||
d751bb78 | 13638 | // Dump ast representation for a map descriptor expression. |
13639 | ||
13640 | void | |
13641 | Map_descriptor_expression::do_dump_expression( | |
13642 | Ast_dump_context* ast_dump_context) const | |
13643 | { | |
13644 | ast_dump_context->ostream() << "map_descriptor("; | |
13645 | ast_dump_context->dump_type(this->type_); | |
13646 | ast_dump_context->ostream() << ")"; | |
13647 | } | |
13648 | ||
a9182619 | 13649 | // Make a map descriptor expression. |
13650 | ||
13651 | Expression* | |
b13c66cd | 13652 | Expression::make_map_descriptor(Map_type* type, Location location) |
a9182619 | 13653 | { |
13654 | return new Map_descriptor_expression(type, location); | |
13655 | } | |
13656 | ||
e440a328 | 13657 | // An expression which evaluates to the address of an unnamed label. |
13658 | ||
13659 | class Label_addr_expression : public Expression | |
13660 | { | |
13661 | public: | |
b13c66cd | 13662 | Label_addr_expression(Label* label, Location location) |
e440a328 | 13663 | : Expression(EXPRESSION_LABEL_ADDR, location), |
13664 | label_(label) | |
13665 | { } | |
13666 | ||
13667 | protected: | |
13668 | Type* | |
13669 | do_type() | |
13670 | { return Type::make_pointer_type(Type::make_void_type()); } | |
13671 | ||
13672 | void | |
13673 | do_determine_type(const Type_context*) | |
13674 | { } | |
13675 | ||
13676 | Expression* | |
13677 | do_copy() | |
13678 | { return new Label_addr_expression(this->label_, this->location()); } | |
13679 | ||
13680 | tree | |
6e193e6f | 13681 | do_get_tree(Translate_context* context) |
13682 | { | |
e8816003 | 13683 | return expr_to_tree(this->label_->get_addr(context, this->location())); |
6e193e6f | 13684 | } |
e440a328 | 13685 | |
d751bb78 | 13686 | void |
13687 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13688 | { ast_dump_context->ostream() << this->label_->name(); } | |
13689 | ||
e440a328 | 13690 | private: |
13691 | // The label whose address we are taking. | |
13692 | Label* label_; | |
13693 | }; | |
13694 | ||
13695 | // Make an expression for the address of an unnamed label. | |
13696 | ||
13697 | Expression* | |
b13c66cd | 13698 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 13699 | { |
13700 | return new Label_addr_expression(label, location); | |
13701 | } | |
13702 | ||
13703 | // Import an expression. This comes at the end in order to see the | |
13704 | // various class definitions. | |
13705 | ||
13706 | Expression* | |
13707 | Expression::import_expression(Import* imp) | |
13708 | { | |
13709 | int c = imp->peek_char(); | |
13710 | if (imp->match_c_string("- ") | |
13711 | || imp->match_c_string("! ") | |
13712 | || imp->match_c_string("^ ")) | |
13713 | return Unary_expression::do_import(imp); | |
13714 | else if (c == '(') | |
13715 | return Binary_expression::do_import(imp); | |
13716 | else if (imp->match_c_string("true") | |
13717 | || imp->match_c_string("false")) | |
13718 | return Boolean_expression::do_import(imp); | |
13719 | else if (c == '"') | |
13720 | return String_expression::do_import(imp); | |
13721 | else if (c == '-' || (c >= '0' && c <= '9')) | |
13722 | { | |
13723 | // This handles integers, floats and complex constants. | |
13724 | return Integer_expression::do_import(imp); | |
13725 | } | |
13726 | else if (imp->match_c_string("nil")) | |
13727 | return Nil_expression::do_import(imp); | |
13728 | else if (imp->match_c_string("convert")) | |
13729 | return Type_conversion_expression::do_import(imp); | |
13730 | else | |
13731 | { | |
13732 | error_at(imp->location(), "import error: expected expression"); | |
13733 | return Expression::make_error(imp->location()); | |
13734 | } | |
13735 | } | |
13736 | ||
13737 | // Class Expression_list. | |
13738 | ||
13739 | // Traverse the list. | |
13740 | ||
13741 | int | |
13742 | Expression_list::traverse(Traverse* traverse) | |
13743 | { | |
13744 | for (Expression_list::iterator p = this->begin(); | |
13745 | p != this->end(); | |
13746 | ++p) | |
13747 | { | |
13748 | if (*p != NULL) | |
13749 | { | |
13750 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13751 | return TRAVERSE_EXIT; | |
13752 | } | |
13753 | } | |
13754 | return TRAVERSE_CONTINUE; | |
13755 | } | |
13756 | ||
13757 | // Copy the list. | |
13758 | ||
13759 | Expression_list* | |
13760 | Expression_list::copy() | |
13761 | { | |
13762 | Expression_list* ret = new Expression_list(); | |
13763 | for (Expression_list::iterator p = this->begin(); | |
13764 | p != this->end(); | |
13765 | ++p) | |
13766 | { | |
13767 | if (*p == NULL) | |
13768 | ret->push_back(NULL); | |
13769 | else | |
13770 | ret->push_back((*p)->copy()); | |
13771 | } | |
13772 | return ret; | |
13773 | } | |
13774 | ||
13775 | // Return whether an expression list has an error expression. | |
13776 | ||
13777 | bool | |
13778 | Expression_list::contains_error() const | |
13779 | { | |
13780 | for (Expression_list::const_iterator p = this->begin(); | |
13781 | p != this->end(); | |
13782 | ++p) | |
13783 | if (*p != NULL && (*p)->is_error_expression()) | |
13784 | return true; | |
13785 | return false; | |
13786 | } | |
0c77715b | 13787 | |
13788 | // Class Numeric_constant. | |
13789 | ||
13790 | // Destructor. | |
13791 | ||
13792 | Numeric_constant::~Numeric_constant() | |
13793 | { | |
13794 | this->clear(); | |
13795 | } | |
13796 | ||
13797 | // Copy constructor. | |
13798 | ||
13799 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
13800 | : classification_(a.classification_), type_(a.type_) | |
13801 | { | |
13802 | switch (a.classification_) | |
13803 | { | |
13804 | case NC_INVALID: | |
13805 | break; | |
13806 | case NC_INT: | |
13807 | case NC_RUNE: | |
13808 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
13809 | break; | |
13810 | case NC_FLOAT: | |
13811 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
13812 | break; | |
13813 | case NC_COMPLEX: | |
13814 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
13815 | GMP_RNDN); | |
13816 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
13817 | GMP_RNDN); | |
13818 | break; | |
13819 | default: | |
13820 | go_unreachable(); | |
13821 | } | |
13822 | } | |
13823 | ||
13824 | // Assignment operator. | |
13825 | ||
13826 | Numeric_constant& | |
13827 | Numeric_constant::operator=(const Numeric_constant& a) | |
13828 | { | |
13829 | this->clear(); | |
13830 | this->classification_ = a.classification_; | |
13831 | this->type_ = a.type_; | |
13832 | switch (a.classification_) | |
13833 | { | |
13834 | case NC_INVALID: | |
13835 | break; | |
13836 | case NC_INT: | |
13837 | case NC_RUNE: | |
13838 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
13839 | break; | |
13840 | case NC_FLOAT: | |
13841 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
13842 | break; | |
13843 | case NC_COMPLEX: | |
13844 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
13845 | GMP_RNDN); | |
13846 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
13847 | GMP_RNDN); | |
13848 | break; | |
13849 | default: | |
13850 | go_unreachable(); | |
13851 | } | |
13852 | return *this; | |
13853 | } | |
13854 | ||
13855 | // Clear the contents. | |
13856 | ||
13857 | void | |
13858 | Numeric_constant::clear() | |
13859 | { | |
13860 | switch (this->classification_) | |
13861 | { | |
13862 | case NC_INVALID: | |
13863 | break; | |
13864 | case NC_INT: | |
13865 | case NC_RUNE: | |
13866 | mpz_clear(this->u_.int_val); | |
13867 | break; | |
13868 | case NC_FLOAT: | |
13869 | mpfr_clear(this->u_.float_val); | |
13870 | break; | |
13871 | case NC_COMPLEX: | |
13872 | mpfr_clear(this->u_.complex_val.real); | |
13873 | mpfr_clear(this->u_.complex_val.imag); | |
13874 | break; | |
13875 | default: | |
13876 | go_unreachable(); | |
13877 | } | |
13878 | this->classification_ = NC_INVALID; | |
13879 | } | |
13880 | ||
13881 | // Set to an unsigned long value. | |
13882 | ||
13883 | void | |
13884 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
13885 | { | |
13886 | this->clear(); | |
13887 | this->classification_ = NC_INT; | |
13888 | this->type_ = type; | |
13889 | mpz_init_set_ui(this->u_.int_val, val); | |
13890 | } | |
13891 | ||
13892 | // Set to an integer value. | |
13893 | ||
13894 | void | |
13895 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
13896 | { | |
13897 | this->clear(); | |
13898 | this->classification_ = NC_INT; | |
13899 | this->type_ = type; | |
13900 | mpz_init_set(this->u_.int_val, val); | |
13901 | } | |
13902 | ||
13903 | // Set to a rune value. | |
13904 | ||
13905 | void | |
13906 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
13907 | { | |
13908 | this->clear(); | |
13909 | this->classification_ = NC_RUNE; | |
13910 | this->type_ = type; | |
13911 | mpz_init_set(this->u_.int_val, val); | |
13912 | } | |
13913 | ||
13914 | // Set to a floating point value. | |
13915 | ||
13916 | void | |
13917 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
13918 | { | |
13919 | this->clear(); | |
13920 | this->classification_ = NC_FLOAT; | |
13921 | this->type_ = type; | |
833b523c | 13922 | // Numeric constants do not have negative zero values, so remove |
13923 | // them here. They also don't have infinity or NaN values, but we | |
13924 | // should never see them here. | |
13925 | if (mpfr_zero_p(val)) | |
13926 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
13927 | else | |
13928 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 13929 | } |
13930 | ||
13931 | // Set to a complex value. | |
13932 | ||
13933 | void | |
13934 | Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag) | |
13935 | { | |
13936 | this->clear(); | |
13937 | this->classification_ = NC_COMPLEX; | |
13938 | this->type_ = type; | |
13939 | mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN); | |
13940 | mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN); | |
13941 | } | |
13942 | ||
13943 | // Get an int value. | |
13944 | ||
13945 | void | |
13946 | Numeric_constant::get_int(mpz_t* val) const | |
13947 | { | |
13948 | go_assert(this->is_int()); | |
13949 | mpz_init_set(*val, this->u_.int_val); | |
13950 | } | |
13951 | ||
13952 | // Get a rune value. | |
13953 | ||
13954 | void | |
13955 | Numeric_constant::get_rune(mpz_t* val) const | |
13956 | { | |
13957 | go_assert(this->is_rune()); | |
13958 | mpz_init_set(*val, this->u_.int_val); | |
13959 | } | |
13960 | ||
13961 | // Get a floating point value. | |
13962 | ||
13963 | void | |
13964 | Numeric_constant::get_float(mpfr_t* val) const | |
13965 | { | |
13966 | go_assert(this->is_float()); | |
13967 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
13968 | } | |
13969 | ||
13970 | // Get a complex value. | |
13971 | ||
13972 | void | |
13973 | Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const | |
13974 | { | |
13975 | go_assert(this->is_complex()); | |
13976 | mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN); | |
13977 | mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN); | |
13978 | } | |
13979 | ||
13980 | // Express value as unsigned long if possible. | |
13981 | ||
13982 | Numeric_constant::To_unsigned_long | |
13983 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
13984 | { | |
13985 | switch (this->classification_) | |
13986 | { | |
13987 | case NC_INT: | |
13988 | case NC_RUNE: | |
13989 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
13990 | case NC_FLOAT: | |
13991 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
13992 | case NC_COMPLEX: | |
13993 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
13994 | return NC_UL_NOTINT; | |
13995 | return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val); | |
13996 | default: | |
13997 | go_unreachable(); | |
13998 | } | |
13999 | } | |
14000 | ||
14001 | // Express integer value as unsigned long if possible. | |
14002 | ||
14003 | Numeric_constant::To_unsigned_long | |
14004 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
14005 | unsigned long *val) const | |
14006 | { | |
14007 | if (mpz_sgn(ival) < 0) | |
14008 | return NC_UL_NEGATIVE; | |
14009 | unsigned long ui = mpz_get_ui(ival); | |
14010 | if (mpz_cmp_ui(ival, ui) != 0) | |
14011 | return NC_UL_BIG; | |
14012 | *val = ui; | |
14013 | return NC_UL_VALID; | |
14014 | } | |
14015 | ||
14016 | // Express floating point value as unsigned long if possible. | |
14017 | ||
14018 | Numeric_constant::To_unsigned_long | |
14019 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
14020 | unsigned long *val) const | |
14021 | { | |
14022 | if (!mpfr_integer_p(fval)) | |
14023 | return NC_UL_NOTINT; | |
14024 | mpz_t ival; | |
14025 | mpz_init(ival); | |
14026 | mpfr_get_z(ival, fval, GMP_RNDN); | |
14027 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
14028 | mpz_clear(ival); | |
14029 | return ret; | |
14030 | } | |
14031 | ||
14032 | // Convert value to integer if possible. | |
14033 | ||
14034 | bool | |
14035 | Numeric_constant::to_int(mpz_t* val) const | |
14036 | { | |
14037 | switch (this->classification_) | |
14038 | { | |
14039 | case NC_INT: | |
14040 | case NC_RUNE: | |
14041 | mpz_init_set(*val, this->u_.int_val); | |
14042 | return true; | |
14043 | case NC_FLOAT: | |
14044 | if (!mpfr_integer_p(this->u_.float_val)) | |
14045 | return false; | |
14046 | mpz_init(*val); | |
14047 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
14048 | return true; | |
14049 | case NC_COMPLEX: | |
14050 | if (!mpfr_zero_p(this->u_.complex_val.imag) | |
14051 | || !mpfr_integer_p(this->u_.complex_val.real)) | |
14052 | return false; | |
14053 | mpz_init(*val); | |
14054 | mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN); | |
14055 | return true; | |
14056 | default: | |
14057 | go_unreachable(); | |
14058 | } | |
14059 | } | |
14060 | ||
14061 | // Convert value to floating point if possible. | |
14062 | ||
14063 | bool | |
14064 | Numeric_constant::to_float(mpfr_t* val) const | |
14065 | { | |
14066 | switch (this->classification_) | |
14067 | { | |
14068 | case NC_INT: | |
14069 | case NC_RUNE: | |
14070 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
14071 | return true; | |
14072 | case NC_FLOAT: | |
14073 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
14074 | return true; | |
14075 | case NC_COMPLEX: | |
14076 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
14077 | return false; | |
14078 | mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN); | |
14079 | return true; | |
14080 | default: | |
14081 | go_unreachable(); | |
14082 | } | |
14083 | } | |
14084 | ||
14085 | // Convert value to complex. | |
14086 | ||
14087 | bool | |
14088 | Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const | |
14089 | { | |
14090 | switch (this->classification_) | |
14091 | { | |
14092 | case NC_INT: | |
14093 | case NC_RUNE: | |
14094 | mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN); | |
14095 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
14096 | return true; | |
14097 | case NC_FLOAT: | |
14098 | mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN); | |
14099 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
14100 | return true; | |
14101 | case NC_COMPLEX: | |
14102 | mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN); | |
14103 | mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN); | |
14104 | return true; | |
14105 | default: | |
14106 | go_unreachable(); | |
14107 | } | |
14108 | } | |
14109 | ||
14110 | // Get the type. | |
14111 | ||
14112 | Type* | |
14113 | Numeric_constant::type() const | |
14114 | { | |
14115 | if (this->type_ != NULL) | |
14116 | return this->type_; | |
14117 | switch (this->classification_) | |
14118 | { | |
14119 | case NC_INT: | |
14120 | return Type::make_abstract_integer_type(); | |
14121 | case NC_RUNE: | |
14122 | return Type::make_abstract_character_type(); | |
14123 | case NC_FLOAT: | |
14124 | return Type::make_abstract_float_type(); | |
14125 | case NC_COMPLEX: | |
14126 | return Type::make_abstract_complex_type(); | |
14127 | default: | |
14128 | go_unreachable(); | |
14129 | } | |
14130 | } | |
14131 | ||
14132 | // If the constant can be expressed in TYPE, then set the type of the | |
14133 | // constant to TYPE and return true. Otherwise return false, and, if | |
14134 | // ISSUE_ERROR is true, report an appropriate error message. | |
14135 | ||
14136 | bool | |
14137 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
14138 | { | |
14139 | bool ret; | |
14140 | if (type == NULL) | |
14141 | ret = true; | |
14142 | else if (type->integer_type() != NULL) | |
14143 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
14144 | else if (type->float_type() != NULL) | |
14145 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
14146 | else if (type->complex_type() != NULL) | |
14147 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
14148 | else | |
14149 | go_unreachable(); | |
14150 | if (ret) | |
14151 | this->type_ = type; | |
14152 | return ret; | |
14153 | } | |
14154 | ||
14155 | // Check whether the constant can be expressed in an integer type. | |
14156 | ||
14157 | bool | |
14158 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
14159 | Location location) const | |
14160 | { | |
14161 | mpz_t val; | |
14162 | switch (this->classification_) | |
14163 | { | |
14164 | case NC_INT: | |
14165 | case NC_RUNE: | |
14166 | mpz_init_set(val, this->u_.int_val); | |
14167 | break; | |
14168 | ||
14169 | case NC_FLOAT: | |
14170 | if (!mpfr_integer_p(this->u_.float_val)) | |
14171 | { | |
14172 | if (issue_error) | |
14173 | error_at(location, "floating point constant truncated to integer"); | |
14174 | return false; | |
14175 | } | |
14176 | mpz_init(val); | |
14177 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
14178 | break; | |
14179 | ||
14180 | case NC_COMPLEX: | |
14181 | if (!mpfr_integer_p(this->u_.complex_val.real) | |
14182 | || !mpfr_zero_p(this->u_.complex_val.imag)) | |
14183 | { | |
14184 | if (issue_error) | |
14185 | error_at(location, "complex constant truncated to integer"); | |
14186 | return false; | |
14187 | } | |
14188 | mpz_init(val); | |
14189 | mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN); | |
14190 | break; | |
14191 | ||
14192 | default: | |
14193 | go_unreachable(); | |
14194 | } | |
14195 | ||
14196 | bool ret; | |
14197 | if (type->is_abstract()) | |
14198 | ret = true; | |
14199 | else | |
14200 | { | |
14201 | int bits = mpz_sizeinbase(val, 2); | |
14202 | if (type->is_unsigned()) | |
14203 | { | |
14204 | // For an unsigned type we can only accept a nonnegative | |
14205 | // number, and we must be able to represents at least BITS. | |
14206 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
14207 | } | |
14208 | else | |
14209 | { | |
14210 | // For a signed type we need an extra bit to indicate the | |
14211 | // sign. We have to handle the most negative integer | |
14212 | // specially. | |
14213 | ret = (bits + 1 <= type->bits() | |
14214 | || (bits <= type->bits() | |
14215 | && mpz_sgn(val) < 0 | |
14216 | && (mpz_scan1(val, 0) | |
14217 | == static_cast<unsigned long>(type->bits() - 1)) | |
14218 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
14219 | } | |
14220 | } | |
14221 | ||
14222 | if (!ret && issue_error) | |
14223 | error_at(location, "integer constant overflow"); | |
14224 | ||
14225 | return ret; | |
14226 | } | |
14227 | ||
14228 | // Check whether the constant can be expressed in a floating point | |
14229 | // type. | |
14230 | ||
14231 | bool | |
14232 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 14233 | Location location) |
0c77715b | 14234 | { |
14235 | mpfr_t val; | |
14236 | switch (this->classification_) | |
14237 | { | |
14238 | case NC_INT: | |
14239 | case NC_RUNE: | |
14240 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
14241 | break; | |
14242 | ||
14243 | case NC_FLOAT: | |
14244 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
14245 | break; | |
14246 | ||
14247 | case NC_COMPLEX: | |
14248 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
14249 | { | |
14250 | if (issue_error) | |
14251 | error_at(location, "complex constant truncated to float"); | |
14252 | return false; | |
14253 | } | |
14254 | mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN); | |
14255 | break; | |
14256 | ||
14257 | default: | |
14258 | go_unreachable(); | |
14259 | } | |
14260 | ||
14261 | bool ret; | |
14262 | if (type->is_abstract()) | |
14263 | ret = true; | |
14264 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
14265 | { | |
14266 | // A NaN or Infinity always fits in the range of the type. | |
14267 | ret = true; | |
14268 | } | |
14269 | else | |
14270 | { | |
14271 | mp_exp_t exp = mpfr_get_exp(val); | |
14272 | mp_exp_t max_exp; | |
14273 | switch (type->bits()) | |
14274 | { | |
14275 | case 32: | |
14276 | max_exp = 128; | |
14277 | break; | |
14278 | case 64: | |
14279 | max_exp = 1024; | |
14280 | break; | |
14281 | default: | |
14282 | go_unreachable(); | |
14283 | } | |
14284 | ||
14285 | ret = exp <= max_exp; | |
d0bcce51 | 14286 | |
14287 | if (ret) | |
14288 | { | |
14289 | // Round the constant to the desired type. | |
14290 | mpfr_t t; | |
14291 | mpfr_init(t); | |
14292 | switch (type->bits()) | |
14293 | { | |
14294 | case 32: | |
14295 | mpfr_set_prec(t, 24); | |
14296 | break; | |
14297 | case 64: | |
14298 | mpfr_set_prec(t, 53); | |
14299 | break; | |
14300 | default: | |
14301 | go_unreachable(); | |
14302 | } | |
14303 | mpfr_set(t, val, GMP_RNDN); | |
14304 | mpfr_set(val, t, GMP_RNDN); | |
14305 | mpfr_clear(t); | |
14306 | ||
14307 | this->set_float(type, val); | |
14308 | } | |
0c77715b | 14309 | } |
14310 | ||
14311 | mpfr_clear(val); | |
14312 | ||
14313 | if (!ret && issue_error) | |
14314 | error_at(location, "floating point constant overflow"); | |
14315 | ||
14316 | return ret; | |
14317 | } | |
14318 | ||
14319 | // Check whether the constant can be expressed in a complex type. | |
14320 | ||
14321 | bool | |
14322 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 14323 | Location location) |
0c77715b | 14324 | { |
14325 | if (type->is_abstract()) | |
14326 | return true; | |
14327 | ||
14328 | mp_exp_t max_exp; | |
14329 | switch (type->bits()) | |
14330 | { | |
14331 | case 64: | |
14332 | max_exp = 128; | |
14333 | break; | |
14334 | case 128: | |
14335 | max_exp = 1024; | |
14336 | break; | |
14337 | default: | |
14338 | go_unreachable(); | |
14339 | } | |
14340 | ||
14341 | mpfr_t real; | |
d0bcce51 | 14342 | mpfr_t imag; |
0c77715b | 14343 | switch (this->classification_) |
14344 | { | |
14345 | case NC_INT: | |
14346 | case NC_RUNE: | |
14347 | mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN); | |
d0bcce51 | 14348 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 14349 | break; |
14350 | ||
14351 | case NC_FLOAT: | |
14352 | mpfr_init_set(real, this->u_.float_val, GMP_RNDN); | |
d0bcce51 | 14353 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 14354 | break; |
14355 | ||
14356 | case NC_COMPLEX: | |
0c77715b | 14357 | mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN); |
d0bcce51 | 14358 | mpfr_init_set(imag, this->u_.complex_val.imag, GMP_RNDN); |
0c77715b | 14359 | break; |
14360 | ||
14361 | default: | |
14362 | go_unreachable(); | |
14363 | } | |
14364 | ||
d0bcce51 | 14365 | bool ret = true; |
14366 | if (!mpfr_nan_p(real) | |
14367 | && !mpfr_inf_p(real) | |
14368 | && !mpfr_zero_p(real) | |
14369 | && mpfr_get_exp(real) > max_exp) | |
14370 | { | |
14371 | if (issue_error) | |
14372 | error_at(location, "complex real part overflow"); | |
14373 | ret = false; | |
14374 | } | |
0c77715b | 14375 | |
d0bcce51 | 14376 | if (!mpfr_nan_p(imag) |
14377 | && !mpfr_inf_p(imag) | |
14378 | && !mpfr_zero_p(imag) | |
14379 | && mpfr_get_exp(imag) > max_exp) | |
14380 | { | |
14381 | if (issue_error) | |
14382 | error_at(location, "complex imaginary part overflow"); | |
14383 | ret = false; | |
14384 | } | |
0c77715b | 14385 | |
d0bcce51 | 14386 | if (ret) |
14387 | { | |
14388 | // Round the constant to the desired type. | |
14389 | mpfr_t t; | |
14390 | mpfr_init(t); | |
14391 | switch (type->bits()) | |
14392 | { | |
14393 | case 64: | |
14394 | mpfr_set_prec(t, 24); | |
14395 | break; | |
14396 | case 128: | |
14397 | mpfr_set_prec(t, 53); | |
14398 | break; | |
14399 | default: | |
14400 | go_unreachable(); | |
14401 | } | |
14402 | mpfr_set(t, real, GMP_RNDN); | |
14403 | mpfr_set(real, t, GMP_RNDN); | |
14404 | mpfr_set(t, imag, GMP_RNDN); | |
14405 | mpfr_set(imag, t, GMP_RNDN); | |
14406 | mpfr_clear(t); | |
14407 | ||
14408 | this->set_complex(type, real, imag); | |
14409 | } | |
14410 | ||
14411 | mpfr_clear(real); | |
14412 | mpfr_clear(imag); | |
0c77715b | 14413 | |
14414 | return ret; | |
14415 | } | |
14416 | ||
14417 | // Return an Expression for this value. | |
14418 | ||
14419 | Expression* | |
14420 | Numeric_constant::expression(Location loc) const | |
14421 | { | |
14422 | switch (this->classification_) | |
14423 | { | |
14424 | case NC_INT: | |
14425 | return Expression::make_integer(&this->u_.int_val, this->type_, loc); | |
14426 | case NC_RUNE: | |
14427 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
14428 | case NC_FLOAT: | |
14429 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
14430 | case NC_COMPLEX: | |
14431 | return Expression::make_complex(&this->u_.complex_val.real, | |
14432 | &this->u_.complex_val.imag, | |
14433 | this->type_, loc); | |
14434 | default: | |
14435 | go_unreachable(); | |
14436 | } | |
14437 | } |