]>
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 | |
83 | void | |
84 | Expression::do_discarding_value() | |
85 | { | |
a7549a6a | 86 | this->unused_value_error(); |
e440a328 | 87 | } |
88 | ||
89 | // This virtual function is called to export expressions. This will | |
90 | // only be used by expressions which may be constant. | |
91 | ||
92 | void | |
93 | Expression::do_export(Export*) const | |
94 | { | |
c3e6f413 | 95 | go_unreachable(); |
e440a328 | 96 | } |
97 | ||
a7549a6a | 98 | // Give an error saying that the value of the expression is not used. |
e440a328 | 99 | |
100 | void | |
a7549a6a | 101 | Expression::unused_value_error() |
e440a328 | 102 | { |
a7549a6a | 103 | error_at(this->location(), "value computed is not used"); |
e440a328 | 104 | } |
105 | ||
106 | // Note that this expression is an error. This is called by children | |
107 | // when they discover an error. | |
108 | ||
109 | void | |
110 | Expression::set_is_error() | |
111 | { | |
112 | this->classification_ = EXPRESSION_ERROR; | |
113 | } | |
114 | ||
115 | // For children to call to report an error conveniently. | |
116 | ||
117 | void | |
118 | Expression::report_error(const char* msg) | |
119 | { | |
120 | error_at(this->location_, "%s", msg); | |
121 | this->set_is_error(); | |
122 | } | |
123 | ||
124 | // Set types of variables and constants. This is implemented by the | |
125 | // child class. | |
126 | ||
127 | void | |
128 | Expression::determine_type(const Type_context* context) | |
129 | { | |
130 | this->do_determine_type(context); | |
131 | } | |
132 | ||
133 | // Set types when there is no context. | |
134 | ||
135 | void | |
136 | Expression::determine_type_no_context() | |
137 | { | |
138 | Type_context context; | |
139 | this->do_determine_type(&context); | |
140 | } | |
141 | ||
142 | // Return a tree handling any conversions which must be done during | |
143 | // assignment. | |
144 | ||
145 | tree | |
146 | Expression::convert_for_assignment(Translate_context* context, Type* lhs_type, | |
147 | Type* rhs_type, tree rhs_tree, | |
b13c66cd | 148 | Location location) |
e440a328 | 149 | { |
5c13bd80 | 150 | if (lhs_type->is_error() || rhs_type->is_error()) |
e440a328 | 151 | return error_mark_node; |
152 | ||
e440a328 | 153 | if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node) |
154 | return error_mark_node; | |
155 | ||
156 | Gogo* gogo = context->gogo(); | |
157 | ||
9f0e0513 | 158 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 159 | if (lhs_type_tree == error_mark_node) |
160 | return error_mark_node; | |
161 | ||
54211955 | 162 | if (lhs_type->forwarded() != rhs_type->forwarded() |
163 | && lhs_type->interface_type() != NULL) | |
e440a328 | 164 | { |
165 | if (rhs_type->interface_type() == NULL) | |
166 | return Expression::convert_type_to_interface(context, lhs_type, | |
167 | rhs_type, rhs_tree, | |
168 | location); | |
169 | else | |
170 | return Expression::convert_interface_to_interface(context, lhs_type, | |
171 | rhs_type, rhs_tree, | |
172 | false, location); | |
173 | } | |
54211955 | 174 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
175 | && rhs_type->interface_type() != NULL) | |
e440a328 | 176 | return Expression::convert_interface_to_type(context, lhs_type, rhs_type, |
177 | rhs_tree, location); | |
411eb89e | 178 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 179 | { |
180 | // Assigning nil to an open array. | |
c484d925 | 181 | go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE); |
e440a328 | 182 | |
183 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
184 | ||
e82e4eb5 | 185 | constructor_elt empty = {NULL, NULL}; |
186 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, empty); | |
e440a328 | 187 | tree field = TYPE_FIELDS(lhs_type_tree); |
c484d925 | 188 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 189 | "__values") == 0); |
190 | elt->index = field; | |
191 | elt->value = fold_convert(TREE_TYPE(field), null_pointer_node); | |
192 | ||
e82e4eb5 | 193 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 194 | field = DECL_CHAIN(field); |
c484d925 | 195 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 196 | "__count") == 0); |
197 | elt->index = field; | |
198 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
199 | ||
e82e4eb5 | 200 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 201 | field = DECL_CHAIN(field); |
c484d925 | 202 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 203 | "__capacity") == 0); |
204 | elt->index = field; | |
205 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
206 | ||
207 | tree val = build_constructor(lhs_type_tree, init); | |
208 | TREE_CONSTANT(val) = 1; | |
209 | ||
210 | return val; | |
211 | } | |
212 | else if (rhs_type->is_nil_type()) | |
213 | { | |
214 | // The left hand side should be a pointer type at the tree | |
215 | // level. | |
c484d925 | 216 | go_assert(POINTER_TYPE_P(lhs_type_tree)); |
e440a328 | 217 | return fold_convert(lhs_type_tree, null_pointer_node); |
218 | } | |
219 | else if (lhs_type_tree == TREE_TYPE(rhs_tree)) | |
220 | { | |
221 | // No conversion is needed. | |
222 | return rhs_tree; | |
223 | } | |
224 | else if (POINTER_TYPE_P(lhs_type_tree) | |
225 | || INTEGRAL_TYPE_P(lhs_type_tree) | |
226 | || SCALAR_FLOAT_TYPE_P(lhs_type_tree) | |
227 | || COMPLEX_FLOAT_TYPE_P(lhs_type_tree)) | |
b13c66cd | 228 | return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree); |
3e785901 | 229 | else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE |
230 | && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE) | |
231 | || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE | |
232 | && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE)) | |
e440a328 | 233 | { |
bb92f513 | 234 | // Avoid confusion from zero sized variables which may be |
235 | // represented as non-zero-sized. | |
236 | if (int_size_in_bytes(lhs_type_tree) == 0 | |
237 | || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0) | |
238 | return rhs_tree; | |
239 | ||
e440a328 | 240 | // This conversion must be permitted by Go, or we wouldn't have |
241 | // gotten here. | |
c484d925 | 242 | go_assert(int_size_in_bytes(lhs_type_tree) |
bb92f513 | 243 | == int_size_in_bytes(TREE_TYPE(rhs_tree))); |
b13c66cd | 244 | return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR, |
245 | lhs_type_tree, rhs_tree); | |
e440a328 | 246 | } |
247 | else | |
248 | { | |
c484d925 | 249 | go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree))); |
e440a328 | 250 | return rhs_tree; |
251 | } | |
252 | } | |
253 | ||
254 | // Return a tree for a conversion from a non-interface type to an | |
255 | // interface type. | |
256 | ||
257 | tree | |
258 | Expression::convert_type_to_interface(Translate_context* context, | |
259 | Type* lhs_type, Type* rhs_type, | |
b13c66cd | 260 | tree rhs_tree, Location location) |
e440a328 | 261 | { |
262 | Gogo* gogo = context->gogo(); | |
263 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
264 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
265 | ||
266 | // Since RHS_TYPE is a static type, we can create the interface | |
267 | // method table at compile time. | |
268 | ||
269 | // When setting an interface to nil, we just set both fields to | |
270 | // NULL. | |
271 | if (rhs_type->is_nil_type()) | |
63697958 | 272 | { |
273 | Btype* lhs_btype = lhs_type->get_backend(gogo); | |
274 | return expr_to_tree(gogo->backend()->zero_expression(lhs_btype)); | |
275 | } | |
e440a328 | 276 | |
277 | // This should have been checked already. | |
c484d925 | 278 | go_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); |
e440a328 | 279 | |
9f0e0513 | 280 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 281 | if (lhs_type_tree == error_mark_node) |
282 | return error_mark_node; | |
283 | ||
284 | // An interface is a tuple. If LHS_TYPE is an empty interface type, | |
285 | // then the first field is the type descriptor for RHS_TYPE. | |
286 | // Otherwise it is the interface method table for RHS_TYPE. | |
287 | tree first_field_value; | |
288 | if (lhs_is_empty) | |
a1d23b41 | 289 | first_field_value = rhs_type->type_descriptor_pointer(gogo, location); |
e440a328 | 290 | else |
291 | { | |
292 | // Build the interface method table for this interface and this | |
293 | // object type: a list of function pointers for each interface | |
294 | // method. | |
295 | Named_type* rhs_named_type = rhs_type->named_type(); | |
c0cab2ec | 296 | Struct_type* rhs_struct_type = rhs_type->struct_type(); |
e440a328 | 297 | bool is_pointer = false; |
c0cab2ec | 298 | if (rhs_named_type == NULL && rhs_struct_type == NULL) |
e440a328 | 299 | { |
300 | rhs_named_type = rhs_type->deref()->named_type(); | |
c0cab2ec | 301 | rhs_struct_type = rhs_type->deref()->struct_type(); |
e440a328 | 302 | is_pointer = true; |
303 | } | |
304 | tree method_table; | |
c0cab2ec | 305 | if (rhs_named_type != NULL) |
e440a328 | 306 | method_table = |
307 | rhs_named_type->interface_method_table(gogo, lhs_interface_type, | |
308 | is_pointer); | |
c0cab2ec | 309 | else if (rhs_struct_type != NULL) |
310 | method_table = | |
311 | rhs_struct_type->interface_method_table(gogo, lhs_interface_type, | |
312 | is_pointer); | |
313 | else | |
314 | method_table = null_pointer_node; | |
b13c66cd | 315 | first_field_value = fold_convert_loc(location.gcc_location(), |
316 | const_ptr_type_node, method_table); | |
e440a328 | 317 | } |
84b7d3c6 | 318 | if (first_field_value == error_mark_node) |
319 | return error_mark_node; | |
e440a328 | 320 | |
321 | // Start building a constructor for the value we will return. | |
322 | ||
323 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
324 | ||
e82e4eb5 | 325 | constructor_elt empty = {NULL, NULL}; |
326 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, empty); | |
e440a328 | 327 | tree field = TYPE_FIELDS(lhs_type_tree); |
c484d925 | 328 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 329 | (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0); |
330 | elt->index = field; | |
b13c66cd | 331 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
332 | first_field_value); | |
e440a328 | 333 | |
e82e4eb5 | 334 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 335 | field = DECL_CHAIN(field); |
c484d925 | 336 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 337 | elt->index = field; |
338 | ||
339 | if (rhs_type->points_to() != NULL) | |
340 | { | |
341 | // We are assigning a pointer to the interface; the interface | |
342 | // holds the pointer itself. | |
343 | elt->value = rhs_tree; | |
344 | return build_constructor(lhs_type_tree, init); | |
345 | } | |
346 | ||
347 | // We are assigning a non-pointer value to the interface; the | |
348 | // interface gets a copy of the value in the heap. | |
349 | ||
350 | tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree)); | |
351 | ||
352 | tree space = gogo->allocate_memory(rhs_type, object_size, location); | |
b13c66cd | 353 | space = fold_convert_loc(location.gcc_location(), |
354 | build_pointer_type(TREE_TYPE(rhs_tree)), space); | |
e440a328 | 355 | space = save_expr(space); |
356 | ||
b13c66cd | 357 | tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space); |
e440a328 | 358 | TREE_THIS_NOTRAP(ref) = 1; |
b13c66cd | 359 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
360 | void_type_node, ref, rhs_tree); | |
e440a328 | 361 | |
b13c66cd | 362 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
363 | space); | |
e440a328 | 364 | |
365 | return build2(COMPOUND_EXPR, lhs_type_tree, set, | |
366 | build_constructor(lhs_type_tree, init)); | |
367 | } | |
368 | ||
369 | // Return a tree for the type descriptor of RHS_TREE, which has | |
370 | // interface type RHS_TYPE. If RHS_TREE is nil the result will be | |
371 | // NULL. | |
372 | ||
373 | tree | |
374 | Expression::get_interface_type_descriptor(Translate_context*, | |
375 | Type* rhs_type, tree rhs_tree, | |
b13c66cd | 376 | Location location) |
e440a328 | 377 | { |
378 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
c484d925 | 379 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 380 | tree rhs_field = TYPE_FIELDS(rhs_type_tree); |
381 | tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
382 | NULL_TREE); | |
383 | if (rhs_type->interface_type()->is_empty()) | |
384 | { | |
c484d925 | 385 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), |
e440a328 | 386 | "__type_descriptor") == 0); |
387 | return v; | |
388 | } | |
389 | ||
c484d925 | 390 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods") |
e440a328 | 391 | == 0); |
c484d925 | 392 | go_assert(POINTER_TYPE_P(TREE_TYPE(v))); |
e440a328 | 393 | v = save_expr(v); |
b13c66cd | 394 | tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v); |
c484d925 | 395 | go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE); |
e440a328 | 396 | tree f = TYPE_FIELDS(TREE_TYPE(v1)); |
c484d925 | 397 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor") |
e440a328 | 398 | == 0); |
399 | v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE); | |
400 | ||
b13c66cd | 401 | tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node, |
402 | v, fold_convert_loc(location.gcc_location(), | |
403 | TREE_TYPE(v), | |
404 | null_pointer_node)); | |
405 | tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1), | |
406 | null_pointer_node); | |
407 | return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1), | |
e440a328 | 408 | eq, n, v1); |
409 | } | |
410 | ||
411 | // Return a tree for the conversion of an interface type to an | |
412 | // interface type. | |
413 | ||
414 | tree | |
415 | Expression::convert_interface_to_interface(Translate_context* context, | |
416 | Type *lhs_type, Type *rhs_type, | |
417 | tree rhs_tree, bool for_type_guard, | |
b13c66cd | 418 | Location location) |
e440a328 | 419 | { |
420 | Gogo* gogo = context->gogo(); | |
421 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
422 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
423 | ||
9f0e0513 | 424 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 425 | if (lhs_type_tree == error_mark_node) |
426 | return error_mark_node; | |
427 | ||
428 | // In the general case this requires runtime examination of the type | |
429 | // method table to match it up with the interface methods. | |
430 | ||
431 | // FIXME: If all of the methods in the right hand side interface | |
432 | // also appear in the left hand side interface, then we don't need | |
433 | // to do a runtime check, although we still need to build a new | |
434 | // method table. | |
435 | ||
436 | // Get the type descriptor for the right hand side. This will be | |
437 | // NULL for a nil interface. | |
438 | ||
439 | if (!DECL_P(rhs_tree)) | |
440 | rhs_tree = save_expr(rhs_tree); | |
441 | ||
442 | tree rhs_type_descriptor = | |
443 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
444 | location); | |
445 | ||
446 | // The result is going to be a two element constructor. | |
447 | ||
448 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
449 | ||
e82e4eb5 | 450 | constructor_elt empty = {NULL, NULL}; |
451 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, empty); | |
e440a328 | 452 | tree field = TYPE_FIELDS(lhs_type_tree); |
453 | elt->index = field; | |
454 | ||
455 | if (for_type_guard) | |
456 | { | |
457 | // A type assertion fails when converting a nil interface. | |
a1d23b41 | 458 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, |
459 | location); | |
e440a328 | 460 | static tree assert_interface_decl; |
461 | tree call = Gogo::call_builtin(&assert_interface_decl, | |
462 | location, | |
463 | "__go_assert_interface", | |
464 | 2, | |
465 | ptr_type_node, | |
466 | TREE_TYPE(lhs_type_descriptor), | |
467 | lhs_type_descriptor, | |
468 | TREE_TYPE(rhs_type_descriptor), | |
469 | rhs_type_descriptor); | |
5fb82b5e | 470 | if (call == error_mark_node) |
471 | return error_mark_node; | |
e440a328 | 472 | // This will panic if the interface conversion fails. |
473 | TREE_NOTHROW(assert_interface_decl) = 0; | |
b13c66cd | 474 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
475 | call); | |
e440a328 | 476 | } |
477 | else if (lhs_is_empty) | |
478 | { | |
479 | // A convertion to an empty interface always succeeds, and the | |
480 | // first field is just the type descriptor of the object. | |
c484d925 | 481 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 482 | "__type_descriptor") == 0); |
7172c949 | 483 | elt->value = fold_convert_loc(location.gcc_location(), |
484 | TREE_TYPE(field), rhs_type_descriptor); | |
e440a328 | 485 | } |
486 | else | |
487 | { | |
488 | // A conversion to a non-empty interface may fail, but unlike a | |
489 | // type assertion converting nil will always succeed. | |
c484d925 | 490 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") |
e440a328 | 491 | == 0); |
a1d23b41 | 492 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, |
493 | location); | |
e440a328 | 494 | static tree convert_interface_decl; |
495 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
496 | location, | |
497 | "__go_convert_interface", | |
498 | 2, | |
499 | ptr_type_node, | |
500 | TREE_TYPE(lhs_type_descriptor), | |
501 | lhs_type_descriptor, | |
502 | TREE_TYPE(rhs_type_descriptor), | |
503 | rhs_type_descriptor); | |
5fb82b5e | 504 | if (call == error_mark_node) |
505 | return error_mark_node; | |
e440a328 | 506 | // This will panic if the interface conversion fails. |
507 | TREE_NOTHROW(convert_interface_decl) = 0; | |
b13c66cd | 508 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
509 | call); | |
e440a328 | 510 | } |
511 | ||
512 | // The second field is simply the object pointer. | |
513 | ||
e82e4eb5 | 514 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 515 | field = DECL_CHAIN(field); |
c484d925 | 516 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 517 | elt->index = field; |
518 | ||
519 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
c484d925 | 520 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 521 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); |
c484d925 | 522 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); |
e440a328 | 523 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, |
524 | NULL_TREE); | |
525 | ||
526 | return build_constructor(lhs_type_tree, init); | |
527 | } | |
528 | ||
529 | // Return a tree for the conversion of an interface type to a | |
530 | // non-interface type. | |
531 | ||
532 | tree | |
533 | Expression::convert_interface_to_type(Translate_context* context, | |
534 | Type *lhs_type, Type* rhs_type, | |
b13c66cd | 535 | tree rhs_tree, Location location) |
e440a328 | 536 | { |
537 | Gogo* gogo = context->gogo(); | |
538 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
539 | ||
9f0e0513 | 540 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 541 | if (lhs_type_tree == error_mark_node) |
542 | return error_mark_node; | |
543 | ||
544 | // Call a function to check that the type is valid. The function | |
545 | // will panic with an appropriate runtime type error if the type is | |
546 | // not valid. | |
547 | ||
a1d23b41 | 548 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location); |
e440a328 | 549 | |
550 | if (!DECL_P(rhs_tree)) | |
551 | rhs_tree = save_expr(rhs_tree); | |
552 | ||
553 | tree rhs_type_descriptor = | |
554 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
555 | location); | |
556 | ||
a1d23b41 | 557 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo, |
558 | location); | |
e440a328 | 559 | |
560 | static tree check_interface_type_decl; | |
561 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
562 | location, | |
563 | "__go_check_interface_type", | |
564 | 3, | |
565 | void_type_node, | |
566 | TREE_TYPE(lhs_type_descriptor), | |
567 | lhs_type_descriptor, | |
568 | TREE_TYPE(rhs_type_descriptor), | |
569 | rhs_type_descriptor, | |
570 | TREE_TYPE(rhs_inter_descriptor), | |
571 | rhs_inter_descriptor); | |
5fb82b5e | 572 | if (call == error_mark_node) |
573 | return error_mark_node; | |
e440a328 | 574 | // This call will panic if the conversion is invalid. |
575 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
576 | ||
577 | // If the call succeeds, pull out the value. | |
c484d925 | 578 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 579 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); |
c484d925 | 580 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); |
e440a328 | 581 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, |
582 | NULL_TREE); | |
583 | ||
584 | // If the value is a pointer, then it is the value we want. | |
585 | // Otherwise it points to the value. | |
586 | if (lhs_type->points_to() == NULL) | |
587 | { | |
b13c66cd | 588 | val = fold_convert_loc(location.gcc_location(), |
589 | build_pointer_type(lhs_type_tree), val); | |
590 | val = build_fold_indirect_ref_loc(location.gcc_location(), val); | |
e440a328 | 591 | } |
592 | ||
593 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
b13c66cd | 594 | fold_convert_loc(location.gcc_location(), lhs_type_tree, val)); |
e440a328 | 595 | } |
596 | ||
597 | // Convert an expression to a tree. This is implemented by the child | |
598 | // class. Not that it is not in general safe to call this multiple | |
599 | // times for a single expression, but that we don't catch such errors. | |
600 | ||
601 | tree | |
602 | Expression::get_tree(Translate_context* context) | |
603 | { | |
604 | // The child may have marked this expression as having an error. | |
605 | if (this->classification_ == EXPRESSION_ERROR) | |
606 | return error_mark_node; | |
607 | ||
608 | return this->do_get_tree(context); | |
609 | } | |
610 | ||
611 | // Return a tree for VAL in TYPE. | |
612 | ||
613 | tree | |
614 | Expression::integer_constant_tree(mpz_t val, tree type) | |
615 | { | |
616 | if (type == error_mark_node) | |
617 | return error_mark_node; | |
618 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
619 | return double_int_to_tree(type, | |
620 | mpz_get_double_int(type, val, true)); | |
621 | else if (TREE_CODE(type) == REAL_TYPE) | |
622 | { | |
623 | mpfr_t fval; | |
624 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
625 | tree ret = Expression::float_constant_tree(fval, type); | |
626 | mpfr_clear(fval); | |
627 | return ret; | |
628 | } | |
629 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
630 | { | |
631 | mpfr_t fval; | |
632 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
633 | tree real = Expression::float_constant_tree(fval, TREE_TYPE(type)); | |
634 | mpfr_clear(fval); | |
635 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
636 | integer_zero_node); | |
637 | return build_complex(type, real, imag); | |
638 | } | |
639 | else | |
c3e6f413 | 640 | go_unreachable(); |
e440a328 | 641 | } |
642 | ||
643 | // Return a tree for VAL in TYPE. | |
644 | ||
645 | tree | |
646 | Expression::float_constant_tree(mpfr_t val, tree type) | |
647 | { | |
648 | if (type == error_mark_node) | |
649 | return error_mark_node; | |
650 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
651 | { | |
652 | mpz_t ival; | |
653 | mpz_init(ival); | |
654 | mpfr_get_z(ival, val, GMP_RNDN); | |
655 | tree ret = Expression::integer_constant_tree(ival, type); | |
656 | mpz_clear(ival); | |
657 | return ret; | |
658 | } | |
659 | else if (TREE_CODE(type) == REAL_TYPE) | |
660 | { | |
661 | REAL_VALUE_TYPE r1; | |
662 | real_from_mpfr(&r1, val, type, GMP_RNDN); | |
663 | REAL_VALUE_TYPE r2; | |
664 | real_convert(&r2, TYPE_MODE(type), &r1); | |
665 | return build_real(type, r2); | |
666 | } | |
667 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
668 | { | |
669 | REAL_VALUE_TYPE r1; | |
670 | real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN); | |
671 | REAL_VALUE_TYPE r2; | |
672 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
673 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
674 | integer_zero_node); | |
675 | return build_complex(type, build_real(TREE_TYPE(type), r2), imag); | |
676 | } | |
677 | else | |
c3e6f413 | 678 | go_unreachable(); |
e440a328 | 679 | } |
680 | ||
681 | // Return a tree for REAL/IMAG in TYPE. | |
682 | ||
683 | tree | |
684 | Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type) | |
685 | { | |
f690b0bb | 686 | if (type == error_mark_node) |
687 | return error_mark_node; | |
688 | else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE) | |
689 | return Expression::float_constant_tree(real, type); | |
690 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
e440a328 | 691 | { |
692 | REAL_VALUE_TYPE r1; | |
693 | real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN); | |
694 | REAL_VALUE_TYPE r2; | |
695 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
696 | ||
697 | REAL_VALUE_TYPE r3; | |
698 | real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN); | |
699 | REAL_VALUE_TYPE r4; | |
700 | real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3); | |
701 | ||
702 | return build_complex(type, build_real(TREE_TYPE(type), r2), | |
703 | build_real(TREE_TYPE(type), r4)); | |
704 | } | |
705 | else | |
c3e6f413 | 706 | go_unreachable(); |
e440a328 | 707 | } |
708 | ||
709 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
710 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
711 | // If SOFAR is not NULL, it is or'red into the result. The return | |
712 | // value may be NULL if SOFAR is NULL. | |
713 | ||
714 | tree | |
715 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
b13c66cd | 716 | Location loc) |
e440a328 | 717 | { |
718 | tree val_type = TREE_TYPE(val); | |
719 | tree ret = NULL_TREE; | |
720 | ||
721 | if (!TYPE_UNSIGNED(val_type)) | |
722 | { | |
b13c66cd | 723 | ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val, |
e440a328 | 724 | build_int_cst(val_type, 0)); |
725 | if (ret == boolean_false_node) | |
726 | ret = NULL_TREE; | |
727 | } | |
728 | ||
c3068ac0 | 729 | HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type); |
730 | HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type); | |
731 | go_assert(val_type_size != -1 && bound_type_size != -1); | |
732 | if (val_type_size > bound_type_size | |
733 | || (val_type_size == bound_type_size | |
734 | && TYPE_UNSIGNED(val_type) | |
735 | && !TYPE_UNSIGNED(bound_type))) | |
e440a328 | 736 | { |
737 | tree max = TYPE_MAX_VALUE(bound_type); | |
b13c66cd | 738 | tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node, |
739 | val, fold_convert_loc(loc.gcc_location(), | |
740 | val_type, max)); | |
e440a328 | 741 | if (big == boolean_false_node) |
742 | ; | |
743 | else if (ret == NULL_TREE) | |
744 | ret = big; | |
745 | else | |
b13c66cd | 746 | ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
747 | boolean_type_node, ret, big); | |
e440a328 | 748 | } |
749 | ||
750 | if (ret == NULL_TREE) | |
751 | return sofar; | |
752 | else if (sofar == NULL_TREE) | |
753 | return ret; | |
754 | else | |
b13c66cd | 755 | return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node, |
e440a328 | 756 | sofar, ret); |
757 | } | |
758 | ||
d751bb78 | 759 | void |
760 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
761 | { | |
762 | this->do_dump_expression(ast_dump_context); | |
763 | } | |
764 | ||
e440a328 | 765 | // Error expressions. This are used to avoid cascading errors. |
766 | ||
767 | class Error_expression : public Expression | |
768 | { | |
769 | public: | |
b13c66cd | 770 | Error_expression(Location location) |
e440a328 | 771 | : Expression(EXPRESSION_ERROR, location) |
772 | { } | |
773 | ||
774 | protected: | |
775 | bool | |
776 | do_is_constant() const | |
777 | { return true; } | |
778 | ||
779 | bool | |
0c77715b | 780 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 781 | { |
0c77715b | 782 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 783 | return true; |
784 | } | |
785 | ||
786 | void | |
787 | do_discarding_value() | |
788 | { } | |
789 | ||
790 | Type* | |
791 | do_type() | |
792 | { return Type::make_error_type(); } | |
793 | ||
794 | void | |
795 | do_determine_type(const Type_context*) | |
796 | { } | |
797 | ||
798 | Expression* | |
799 | do_copy() | |
800 | { return this; } | |
801 | ||
802 | bool | |
803 | do_is_addressable() const | |
804 | { return true; } | |
805 | ||
806 | tree | |
807 | do_get_tree(Translate_context*) | |
808 | { return error_mark_node; } | |
d751bb78 | 809 | |
810 | void | |
811 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 812 | }; |
813 | ||
d751bb78 | 814 | // Dump the ast representation for an error expression to a dump context. |
815 | ||
816 | void | |
817 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
818 | { | |
819 | ast_dump_context->ostream() << "_Error_" ; | |
820 | } | |
821 | ||
e440a328 | 822 | Expression* |
b13c66cd | 823 | Expression::make_error(Location location) |
e440a328 | 824 | { |
825 | return new Error_expression(location); | |
826 | } | |
827 | ||
828 | // An expression which is really a type. This is used during parsing. | |
829 | // It is an error if these survive after lowering. | |
830 | ||
831 | class | |
832 | Type_expression : public Expression | |
833 | { | |
834 | public: | |
b13c66cd | 835 | Type_expression(Type* type, Location location) |
e440a328 | 836 | : Expression(EXPRESSION_TYPE, location), |
837 | type_(type) | |
838 | { } | |
839 | ||
840 | protected: | |
841 | int | |
842 | do_traverse(Traverse* traverse) | |
843 | { return Type::traverse(this->type_, traverse); } | |
844 | ||
845 | Type* | |
846 | do_type() | |
847 | { return this->type_; } | |
848 | ||
849 | void | |
850 | do_determine_type(const Type_context*) | |
851 | { } | |
852 | ||
853 | void | |
854 | do_check_types(Gogo*) | |
855 | { this->report_error(_("invalid use of type")); } | |
856 | ||
857 | Expression* | |
858 | do_copy() | |
859 | { return this; } | |
860 | ||
861 | tree | |
862 | do_get_tree(Translate_context*) | |
c3e6f413 | 863 | { go_unreachable(); } |
e440a328 | 864 | |
d751bb78 | 865 | void do_dump_expression(Ast_dump_context*) const; |
866 | ||
e440a328 | 867 | private: |
868 | // The type which we are representing as an expression. | |
869 | Type* type_; | |
870 | }; | |
871 | ||
d751bb78 | 872 | void |
873 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
874 | { | |
875 | ast_dump_context->dump_type(this->type_); | |
876 | } | |
877 | ||
e440a328 | 878 | Expression* |
b13c66cd | 879 | Expression::make_type(Type* type, Location location) |
e440a328 | 880 | { |
881 | return new Type_expression(type, location); | |
882 | } | |
883 | ||
e03bdf36 | 884 | // Class Parser_expression. |
885 | ||
886 | Type* | |
887 | Parser_expression::do_type() | |
888 | { | |
889 | // We should never really ask for the type of a Parser_expression. | |
890 | // However, it can happen, at least when we have an invalid const | |
891 | // whose initializer refers to the const itself. In that case we | |
892 | // may ask for the type when lowering the const itself. | |
c484d925 | 893 | go_assert(saw_errors()); |
e03bdf36 | 894 | return Type::make_error_type(); |
895 | } | |
896 | ||
e440a328 | 897 | // Class Var_expression. |
898 | ||
899 | // Lower a variable expression. Here we just make sure that the | |
900 | // initialization expression of the variable has been lowered. This | |
901 | // ensures that we will be able to determine the type of the variable | |
902 | // if necessary. | |
903 | ||
904 | Expression* | |
ceeb4318 | 905 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
906 | Statement_inserter* inserter, int) | |
e440a328 | 907 | { |
908 | if (this->variable_->is_variable()) | |
909 | { | |
910 | Variable* var = this->variable_->var_value(); | |
911 | // This is either a local variable or a global variable. A | |
912 | // reference to a variable which is local to an enclosing | |
913 | // function will be a reference to a field in a closure. | |
914 | if (var->is_global()) | |
ceeb4318 | 915 | { |
916 | function = NULL; | |
917 | inserter = NULL; | |
918 | } | |
919 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 920 | } |
921 | return this; | |
922 | } | |
923 | ||
e440a328 | 924 | // Return the type of a reference to a variable. |
925 | ||
926 | Type* | |
927 | Var_expression::do_type() | |
928 | { | |
929 | if (this->variable_->is_variable()) | |
930 | return this->variable_->var_value()->type(); | |
931 | else if (this->variable_->is_result_variable()) | |
932 | return this->variable_->result_var_value()->type(); | |
933 | else | |
c3e6f413 | 934 | go_unreachable(); |
e440a328 | 935 | } |
936 | ||
0ab09e06 | 937 | // Determine the type of a reference to a variable. |
938 | ||
939 | void | |
940 | Var_expression::do_determine_type(const Type_context*) | |
941 | { | |
942 | if (this->variable_->is_variable()) | |
943 | this->variable_->var_value()->determine_type(); | |
944 | } | |
945 | ||
e440a328 | 946 | // Something takes the address of this variable. This means that we |
947 | // may want to move the variable onto the heap. | |
948 | ||
949 | void | |
950 | Var_expression::do_address_taken(bool escapes) | |
951 | { | |
952 | if (!escapes) | |
f325319b | 953 | { |
954 | if (this->variable_->is_variable()) | |
955 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
956 | else if (this->variable_->is_result_variable()) | |
957 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
958 | else | |
959 | go_unreachable(); | |
960 | } | |
e440a328 | 961 | else |
f325319b | 962 | { |
963 | if (this->variable_->is_variable()) | |
964 | this->variable_->var_value()->set_address_taken(); | |
965 | else if (this->variable_->is_result_variable()) | |
966 | this->variable_->result_var_value()->set_address_taken(); | |
967 | else | |
968 | go_unreachable(); | |
969 | } | |
e440a328 | 970 | } |
971 | ||
972 | // Get the tree for a reference to a variable. | |
973 | ||
974 | tree | |
975 | Var_expression::do_get_tree(Translate_context* context) | |
976 | { | |
fe2f84cf | 977 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
978 | context->function()); | |
979 | tree ret = var_to_tree(bvar); | |
980 | if (ret == error_mark_node) | |
981 | return error_mark_node; | |
982 | bool is_in_heap; | |
983 | if (this->variable_->is_variable()) | |
984 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
985 | else if (this->variable_->is_result_variable()) | |
986 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
987 | else | |
c3e6f413 | 988 | go_unreachable(); |
fe2f84cf | 989 | if (is_in_heap) |
990 | { | |
b13c66cd | 991 | ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret); |
fe2f84cf | 992 | TREE_THIS_NOTRAP(ret) = 1; |
993 | } | |
994 | return ret; | |
e440a328 | 995 | } |
996 | ||
d751bb78 | 997 | // Ast dump for variable expression. |
998 | ||
999 | void | |
1000 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1001 | { | |
1002 | ast_dump_context->ostream() << this->variable_->name() ; | |
1003 | } | |
1004 | ||
e440a328 | 1005 | // Make a reference to a variable in an expression. |
1006 | ||
1007 | Expression* | |
b13c66cd | 1008 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 1009 | { |
1010 | if (var->is_sink()) | |
1011 | return Expression::make_sink(location); | |
1012 | ||
1013 | // FIXME: Creating a new object for each reference to a variable is | |
1014 | // wasteful. | |
1015 | return new Var_expression(var, location); | |
1016 | } | |
1017 | ||
1018 | // Class Temporary_reference_expression. | |
1019 | ||
1020 | // The type. | |
1021 | ||
1022 | Type* | |
1023 | Temporary_reference_expression::do_type() | |
1024 | { | |
1025 | return this->statement_->type(); | |
1026 | } | |
1027 | ||
1028 | // Called if something takes the address of this temporary variable. | |
1029 | // We never have to move temporary variables to the heap, but we do | |
1030 | // need to know that they must live in the stack rather than in a | |
1031 | // register. | |
1032 | ||
1033 | void | |
1034 | Temporary_reference_expression::do_address_taken(bool) | |
1035 | { | |
1036 | this->statement_->set_is_address_taken(); | |
1037 | } | |
1038 | ||
1039 | // Get a tree referring to the variable. | |
1040 | ||
1041 | tree | |
eefc1ed3 | 1042 | Temporary_reference_expression::do_get_tree(Translate_context* context) |
e440a328 | 1043 | { |
eefc1ed3 | 1044 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
1045 | ||
1046 | // The gcc backend can't represent the same set of recursive types | |
1047 | // that the Go frontend can. In some cases this means that a | |
1048 | // temporary variable won't have the right backend type. Correct | |
1049 | // that here by adding a type cast. We need to use base() to push | |
1050 | // the circularity down one level. | |
1051 | tree ret = var_to_tree(bvar); | |
ceeb4318 | 1052 | if (!this->is_lvalue_ |
1053 | && POINTER_TYPE_P(TREE_TYPE(ret)) | |
1054 | && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret)))) | |
eefc1ed3 | 1055 | { |
9f0e0513 | 1056 | Btype* type_btype = this->type()->base()->get_backend(context->gogo()); |
1057 | tree type_tree = type_to_tree(type_btype); | |
b13c66cd | 1058 | ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret); |
eefc1ed3 | 1059 | } |
1060 | return ret; | |
e440a328 | 1061 | } |
1062 | ||
d751bb78 | 1063 | // Ast dump for temporary reference. |
1064 | ||
1065 | void | |
1066 | Temporary_reference_expression::do_dump_expression( | |
1067 | Ast_dump_context* ast_dump_context) const | |
1068 | { | |
1069 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
1070 | } | |
1071 | ||
e440a328 | 1072 | // Make a reference to a temporary variable. |
1073 | ||
ceeb4318 | 1074 | Temporary_reference_expression* |
e440a328 | 1075 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 1076 | Location location) |
e440a328 | 1077 | { |
1078 | return new Temporary_reference_expression(statement, location); | |
1079 | } | |
1080 | ||
e9d3367e | 1081 | // Class Set_and_use_temporary_expression. |
1082 | ||
1083 | // Return the type. | |
1084 | ||
1085 | Type* | |
1086 | Set_and_use_temporary_expression::do_type() | |
1087 | { | |
1088 | return this->statement_->type(); | |
1089 | } | |
1090 | ||
1091 | // Take the address. | |
1092 | ||
1093 | void | |
1094 | Set_and_use_temporary_expression::do_address_taken(bool) | |
1095 | { | |
1096 | this->statement_->set_is_address_taken(); | |
1097 | } | |
1098 | ||
1099 | // Return the backend representation. | |
1100 | ||
1101 | tree | |
1102 | Set_and_use_temporary_expression::do_get_tree(Translate_context* context) | |
1103 | { | |
1104 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
1105 | tree var_tree = var_to_tree(bvar); | |
1106 | tree expr_tree = this->expr_->get_tree(context); | |
1107 | if (var_tree == error_mark_node || expr_tree == error_mark_node) | |
1108 | return error_mark_node; | |
1109 | Location loc = this->location(); | |
1110 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree), | |
1111 | build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node, | |
1112 | var_tree, expr_tree), | |
1113 | var_tree); | |
1114 | } | |
1115 | ||
1116 | // Dump. | |
1117 | ||
1118 | void | |
1119 | Set_and_use_temporary_expression::do_dump_expression( | |
1120 | Ast_dump_context* ast_dump_context) const | |
1121 | { | |
1122 | ast_dump_context->ostream() << '('; | |
1123 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
1124 | ast_dump_context->ostream() << " = "; | |
1125 | this->expr_->dump_expression(ast_dump_context); | |
1126 | ast_dump_context->ostream() << ')'; | |
1127 | } | |
1128 | ||
1129 | // Make a set-and-use temporary. | |
1130 | ||
1131 | Set_and_use_temporary_expression* | |
1132 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
1133 | Expression* expr, Location location) | |
1134 | { | |
1135 | return new Set_and_use_temporary_expression(statement, expr, location); | |
1136 | } | |
1137 | ||
e440a328 | 1138 | // A sink expression--a use of the blank identifier _. |
1139 | ||
1140 | class Sink_expression : public Expression | |
1141 | { | |
1142 | public: | |
b13c66cd | 1143 | Sink_expression(Location location) |
e440a328 | 1144 | : Expression(EXPRESSION_SINK, location), |
1145 | type_(NULL), var_(NULL_TREE) | |
1146 | { } | |
1147 | ||
1148 | protected: | |
1149 | void | |
1150 | do_discarding_value() | |
1151 | { } | |
1152 | ||
1153 | Type* | |
1154 | do_type(); | |
1155 | ||
1156 | void | |
1157 | do_determine_type(const Type_context*); | |
1158 | ||
1159 | Expression* | |
1160 | do_copy() | |
1161 | { return new Sink_expression(this->location()); } | |
1162 | ||
1163 | tree | |
1164 | do_get_tree(Translate_context*); | |
1165 | ||
d751bb78 | 1166 | void |
1167 | do_dump_expression(Ast_dump_context*) const; | |
1168 | ||
e440a328 | 1169 | private: |
1170 | // The type of this sink variable. | |
1171 | Type* type_; | |
1172 | // The temporary variable we generate. | |
1173 | tree var_; | |
1174 | }; | |
1175 | ||
1176 | // Return the type of a sink expression. | |
1177 | ||
1178 | Type* | |
1179 | Sink_expression::do_type() | |
1180 | { | |
1181 | if (this->type_ == NULL) | |
1182 | return Type::make_sink_type(); | |
1183 | return this->type_; | |
1184 | } | |
1185 | ||
1186 | // Determine the type of a sink expression. | |
1187 | ||
1188 | void | |
1189 | Sink_expression::do_determine_type(const Type_context* context) | |
1190 | { | |
1191 | if (context->type != NULL) | |
1192 | this->type_ = context->type; | |
1193 | } | |
1194 | ||
1195 | // Return a temporary variable for a sink expression. This will | |
1196 | // presumably be a write-only variable which the middle-end will drop. | |
1197 | ||
1198 | tree | |
1199 | Sink_expression::do_get_tree(Translate_context* context) | |
1200 | { | |
1201 | if (this->var_ == NULL_TREE) | |
1202 | { | |
c484d925 | 1203 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
9f0e0513 | 1204 | Btype* bt = this->type_->get_backend(context->gogo()); |
1205 | this->var_ = create_tmp_var(type_to_tree(bt), "blank"); | |
e440a328 | 1206 | } |
1207 | return this->var_; | |
1208 | } | |
1209 | ||
d751bb78 | 1210 | // Ast dump for sink expression. |
1211 | ||
1212 | void | |
1213 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1214 | { | |
1215 | ast_dump_context->ostream() << "_" ; | |
1216 | } | |
1217 | ||
e440a328 | 1218 | // Make a sink expression. |
1219 | ||
1220 | Expression* | |
b13c66cd | 1221 | Expression::make_sink(Location location) |
e440a328 | 1222 | { |
1223 | return new Sink_expression(location); | |
1224 | } | |
1225 | ||
1226 | // Class Func_expression. | |
1227 | ||
1228 | // FIXME: Can a function expression appear in a constant expression? | |
1229 | // The value is unchanging. Initializing a constant to the address of | |
1230 | // a function seems like it could work, though there might be little | |
1231 | // point to it. | |
1232 | ||
e440a328 | 1233 | // Traversal. |
1234 | ||
1235 | int | |
1236 | Func_expression::do_traverse(Traverse* traverse) | |
1237 | { | |
1238 | return (this->closure_ == NULL | |
1239 | ? TRAVERSE_CONTINUE | |
1240 | : Expression::traverse(&this->closure_, traverse)); | |
1241 | } | |
1242 | ||
1243 | // Return the type of a function expression. | |
1244 | ||
1245 | Type* | |
1246 | Func_expression::do_type() | |
1247 | { | |
1248 | if (this->function_->is_function()) | |
1249 | return this->function_->func_value()->type(); | |
1250 | else if (this->function_->is_function_declaration()) | |
1251 | return this->function_->func_declaration_value()->type(); | |
1252 | else | |
c3e6f413 | 1253 | go_unreachable(); |
e440a328 | 1254 | } |
1255 | ||
1256 | // Get the tree for a function expression without evaluating the | |
1257 | // closure. | |
1258 | ||
1259 | tree | |
1260 | Func_expression::get_tree_without_closure(Gogo* gogo) | |
1261 | { | |
1262 | Function_type* fntype; | |
1263 | if (this->function_->is_function()) | |
1264 | fntype = this->function_->func_value()->type(); | |
1265 | else if (this->function_->is_function_declaration()) | |
1266 | fntype = this->function_->func_declaration_value()->type(); | |
1267 | else | |
c3e6f413 | 1268 | go_unreachable(); |
e440a328 | 1269 | |
1270 | // Builtin functions are handled specially by Call_expression. We | |
1271 | // can't take their address. | |
1272 | if (fntype->is_builtin()) | |
1273 | { | |
cb0e02f3 | 1274 | error_at(this->location(), |
1275 | "invalid use of special builtin function %qs; must be called", | |
e440a328 | 1276 | this->function_->name().c_str()); |
1277 | return error_mark_node; | |
1278 | } | |
1279 | ||
1280 | Named_object* no = this->function_; | |
9d6f3721 | 1281 | |
1282 | tree id = no->get_id(gogo); | |
1283 | if (id == error_mark_node) | |
1284 | return error_mark_node; | |
1285 | ||
e440a328 | 1286 | tree fndecl; |
1287 | if (no->is_function()) | |
1288 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1289 | else if (no->is_function_declaration()) | |
1290 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1291 | else | |
c3e6f413 | 1292 | go_unreachable(); |
e440a328 | 1293 | |
9d6f3721 | 1294 | if (fndecl == error_mark_node) |
1295 | return error_mark_node; | |
1296 | ||
b13c66cd | 1297 | return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl); |
e440a328 | 1298 | } |
1299 | ||
1300 | // Get the tree for a function expression. This is used when we take | |
1301 | // the address of a function rather than simply calling it. If the | |
1302 | // function has a closure, we must use a trampoline. | |
1303 | ||
1304 | tree | |
1305 | Func_expression::do_get_tree(Translate_context* context) | |
1306 | { | |
1307 | Gogo* gogo = context->gogo(); | |
1308 | ||
1309 | tree fnaddr = this->get_tree_without_closure(gogo); | |
1310 | if (fnaddr == error_mark_node) | |
1311 | return error_mark_node; | |
1312 | ||
c484d925 | 1313 | go_assert(TREE_CODE(fnaddr) == ADDR_EXPR |
e440a328 | 1314 | && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL); |
1315 | TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1; | |
1316 | ||
2010c17a | 1317 | // If there is no closure, that is all have to do. |
1318 | if (this->closure_ == NULL) | |
1319 | return fnaddr; | |
e440a328 | 1320 | |
2010c17a | 1321 | go_assert(this->function_->func_value()->enclosing() != NULL); |
1322 | ||
1323 | // Get the value of the closure. This will be a pointer to space | |
1324 | // allocated on the heap. | |
1325 | tree closure_tree = this->closure_->get_tree(context); | |
1326 | if (closure_tree == error_mark_node) | |
1327 | return error_mark_node; | |
1328 | go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree))); | |
e440a328 | 1329 | |
1330 | // Now we need to build some code on the heap. This code will load | |
1331 | // the static chain pointer with the closure and then jump to the | |
1332 | // body of the function. The normal gcc approach is to build the | |
1333 | // code on the stack. Unfortunately we can not do that, as Go | |
1334 | // permits us to return the function pointer. | |
1335 | ||
1336 | return gogo->make_trampoline(fnaddr, closure_tree, this->location()); | |
1337 | } | |
1338 | ||
d751bb78 | 1339 | // Ast dump for function. |
1340 | ||
1341 | void | |
1342 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1343 | { | |
8b1c301d | 1344 | ast_dump_context->ostream() << this->function_->name(); |
1345 | if (this->closure_ != NULL) | |
1346 | { | |
1347 | ast_dump_context->ostream() << " {closure = "; | |
1348 | this->closure_->dump_expression(ast_dump_context); | |
1349 | ast_dump_context->ostream() << "}"; | |
1350 | } | |
d751bb78 | 1351 | } |
1352 | ||
e440a328 | 1353 | // Make a reference to a function in an expression. |
1354 | ||
1355 | Expression* | |
1356 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1357 | Location location) |
e440a328 | 1358 | { |
1359 | return new Func_expression(function, closure, location); | |
1360 | } | |
1361 | ||
1362 | // Class Unknown_expression. | |
1363 | ||
1364 | // Return the name of an unknown expression. | |
1365 | ||
1366 | const std::string& | |
1367 | Unknown_expression::name() const | |
1368 | { | |
1369 | return this->named_object_->name(); | |
1370 | } | |
1371 | ||
1372 | // Lower a reference to an unknown name. | |
1373 | ||
1374 | Expression* | |
ceeb4318 | 1375 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1376 | { |
b13c66cd | 1377 | Location location = this->location(); |
e440a328 | 1378 | Named_object* no = this->named_object_; |
deded542 | 1379 | Named_object* real; |
1380 | if (!no->is_unknown()) | |
1381 | real = no; | |
1382 | else | |
e440a328 | 1383 | { |
deded542 | 1384 | real = no->unknown_value()->real_named_object(); |
1385 | if (real == NULL) | |
1386 | { | |
1387 | if (this->is_composite_literal_key_) | |
1388 | return this; | |
acf8e158 | 1389 | if (!this->no_error_message_) |
1390 | error_at(location, "reference to undefined name %qs", | |
1391 | this->named_object_->message_name().c_str()); | |
deded542 | 1392 | return Expression::make_error(location); |
1393 | } | |
e440a328 | 1394 | } |
1395 | switch (real->classification()) | |
1396 | { | |
1397 | case Named_object::NAMED_OBJECT_CONST: | |
1398 | return Expression::make_const_reference(real, location); | |
1399 | case Named_object::NAMED_OBJECT_TYPE: | |
1400 | return Expression::make_type(real->type_value(), location); | |
1401 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1402 | if (this->is_composite_literal_key_) | |
1403 | return this; | |
acf8e158 | 1404 | if (!this->no_error_message_) |
1405 | error_at(location, "reference to undefined type %qs", | |
1406 | real->message_name().c_str()); | |
e440a328 | 1407 | return Expression::make_error(location); |
1408 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1409 | real->var_value()->set_is_used(); |
e440a328 | 1410 | return Expression::make_var_reference(real, location); |
1411 | case Named_object::NAMED_OBJECT_FUNC: | |
1412 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1413 | return Expression::make_func_reference(real, NULL, location); | |
1414 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1415 | if (this->is_composite_literal_key_) | |
1416 | return this; | |
acf8e158 | 1417 | if (!this->no_error_message_) |
1418 | error_at(location, "unexpected reference to package"); | |
e440a328 | 1419 | return Expression::make_error(location); |
1420 | default: | |
c3e6f413 | 1421 | go_unreachable(); |
e440a328 | 1422 | } |
1423 | } | |
1424 | ||
d751bb78 | 1425 | // Dump the ast representation for an unknown expression to a dump context. |
1426 | ||
1427 | void | |
1428 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1429 | { | |
1430 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1431 | << ")"; | |
d751bb78 | 1432 | } |
1433 | ||
e440a328 | 1434 | // Make a reference to an unknown name. |
1435 | ||
acf8e158 | 1436 | Unknown_expression* |
b13c66cd | 1437 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1438 | { |
e440a328 | 1439 | return new Unknown_expression(no, location); |
1440 | } | |
1441 | ||
1442 | // A boolean expression. | |
1443 | ||
1444 | class Boolean_expression : public Expression | |
1445 | { | |
1446 | public: | |
b13c66cd | 1447 | Boolean_expression(bool val, Location location) |
e440a328 | 1448 | : Expression(EXPRESSION_BOOLEAN, location), |
1449 | val_(val), type_(NULL) | |
1450 | { } | |
1451 | ||
1452 | static Expression* | |
1453 | do_import(Import*); | |
1454 | ||
1455 | protected: | |
1456 | bool | |
1457 | do_is_constant() const | |
1458 | { return true; } | |
1459 | ||
1460 | Type* | |
1461 | do_type(); | |
1462 | ||
1463 | void | |
1464 | do_determine_type(const Type_context*); | |
1465 | ||
1466 | Expression* | |
1467 | do_copy() | |
1468 | { return this; } | |
1469 | ||
1470 | tree | |
1471 | do_get_tree(Translate_context*) | |
1472 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1473 | ||
1474 | void | |
1475 | do_export(Export* exp) const | |
1476 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1477 | ||
d751bb78 | 1478 | void |
1479 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1480 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1481 | ||
e440a328 | 1482 | private: |
1483 | // The constant. | |
1484 | bool val_; | |
1485 | // The type as determined by context. | |
1486 | Type* type_; | |
1487 | }; | |
1488 | ||
1489 | // Get the type. | |
1490 | ||
1491 | Type* | |
1492 | Boolean_expression::do_type() | |
1493 | { | |
1494 | if (this->type_ == NULL) | |
1495 | this->type_ = Type::make_boolean_type(); | |
1496 | return this->type_; | |
1497 | } | |
1498 | ||
1499 | // Set the type from the context. | |
1500 | ||
1501 | void | |
1502 | Boolean_expression::do_determine_type(const Type_context* context) | |
1503 | { | |
1504 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1505 | ; | |
1506 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1507 | this->type_ = context->type; | |
1508 | else if (!context->may_be_abstract) | |
1509 | this->type_ = Type::lookup_bool_type(); | |
1510 | } | |
1511 | ||
1512 | // Import a boolean constant. | |
1513 | ||
1514 | Expression* | |
1515 | Boolean_expression::do_import(Import* imp) | |
1516 | { | |
1517 | if (imp->peek_char() == 't') | |
1518 | { | |
1519 | imp->require_c_string("true"); | |
1520 | return Expression::make_boolean(true, imp->location()); | |
1521 | } | |
1522 | else | |
1523 | { | |
1524 | imp->require_c_string("false"); | |
1525 | return Expression::make_boolean(false, imp->location()); | |
1526 | } | |
1527 | } | |
1528 | ||
1529 | // Make a boolean expression. | |
1530 | ||
1531 | Expression* | |
b13c66cd | 1532 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1533 | { |
1534 | return new Boolean_expression(val, location); | |
1535 | } | |
1536 | ||
1537 | // Class String_expression. | |
1538 | ||
1539 | // Get the type. | |
1540 | ||
1541 | Type* | |
1542 | String_expression::do_type() | |
1543 | { | |
1544 | if (this->type_ == NULL) | |
1545 | this->type_ = Type::make_string_type(); | |
1546 | return this->type_; | |
1547 | } | |
1548 | ||
1549 | // Set the type from the context. | |
1550 | ||
1551 | void | |
1552 | String_expression::do_determine_type(const Type_context* context) | |
1553 | { | |
1554 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1555 | ; | |
1556 | else if (context->type != NULL && context->type->is_string_type()) | |
1557 | this->type_ = context->type; | |
1558 | else if (!context->may_be_abstract) | |
1559 | this->type_ = Type::lookup_string_type(); | |
1560 | } | |
1561 | ||
1562 | // Build a string constant. | |
1563 | ||
1564 | tree | |
1565 | String_expression::do_get_tree(Translate_context* context) | |
1566 | { | |
1567 | return context->gogo()->go_string_constant_tree(this->val_); | |
1568 | } | |
1569 | ||
8b1c301d | 1570 | // Write string literal to string dump. |
e440a328 | 1571 | |
1572 | void | |
8b1c301d | 1573 | String_expression::export_string(String_dump* exp, |
1574 | const String_expression* str) | |
e440a328 | 1575 | { |
1576 | std::string s; | |
8b1c301d | 1577 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1578 | s += '"'; |
8b1c301d | 1579 | for (std::string::const_iterator p = str->val_.begin(); |
1580 | p != str->val_.end(); | |
e440a328 | 1581 | ++p) |
1582 | { | |
1583 | if (*p == '\\' || *p == '"') | |
1584 | { | |
1585 | s += '\\'; | |
1586 | s += *p; | |
1587 | } | |
1588 | else if (*p >= 0x20 && *p < 0x7f) | |
1589 | s += *p; | |
1590 | else if (*p == '\n') | |
1591 | s += "\\n"; | |
1592 | else if (*p == '\t') | |
1593 | s += "\\t"; | |
1594 | else | |
1595 | { | |
1596 | s += "\\x"; | |
1597 | unsigned char c = *p; | |
1598 | unsigned int dig = c >> 4; | |
1599 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1600 | dig = c & 0xf; | |
1601 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1602 | } | |
1603 | } | |
1604 | s += '"'; | |
1605 | exp->write_string(s); | |
1606 | } | |
1607 | ||
8b1c301d | 1608 | // Export a string expression. |
1609 | ||
1610 | void | |
1611 | String_expression::do_export(Export* exp) const | |
1612 | { | |
1613 | String_expression::export_string(exp, this); | |
1614 | } | |
1615 | ||
e440a328 | 1616 | // Import a string expression. |
1617 | ||
1618 | Expression* | |
1619 | String_expression::do_import(Import* imp) | |
1620 | { | |
1621 | imp->require_c_string("\""); | |
1622 | std::string val; | |
1623 | while (true) | |
1624 | { | |
1625 | int c = imp->get_char(); | |
1626 | if (c == '"' || c == -1) | |
1627 | break; | |
1628 | if (c != '\\') | |
1629 | val += static_cast<char>(c); | |
1630 | else | |
1631 | { | |
1632 | c = imp->get_char(); | |
1633 | if (c == '\\' || c == '"') | |
1634 | val += static_cast<char>(c); | |
1635 | else if (c == 'n') | |
1636 | val += '\n'; | |
1637 | else if (c == 't') | |
1638 | val += '\t'; | |
1639 | else if (c == 'x') | |
1640 | { | |
1641 | c = imp->get_char(); | |
1642 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1643 | c = imp->get_char(); | |
1644 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1645 | char v = (vh << 4) | vl; | |
1646 | val += v; | |
1647 | } | |
1648 | else | |
1649 | { | |
1650 | error_at(imp->location(), "bad string constant"); | |
1651 | return Expression::make_error(imp->location()); | |
1652 | } | |
1653 | } | |
1654 | } | |
1655 | return Expression::make_string(val, imp->location()); | |
1656 | } | |
1657 | ||
d751bb78 | 1658 | // Ast dump for string expression. |
1659 | ||
1660 | void | |
1661 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1662 | { | |
8b1c301d | 1663 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1664 | } |
1665 | ||
e440a328 | 1666 | // Make a string expression. |
1667 | ||
1668 | Expression* | |
b13c66cd | 1669 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1670 | { |
1671 | return new String_expression(val, location); | |
1672 | } | |
1673 | ||
1674 | // Make an integer expression. | |
1675 | ||
1676 | class Integer_expression : public Expression | |
1677 | { | |
1678 | public: | |
5d4b8566 | 1679 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1680 | Location location) | |
e440a328 | 1681 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1682 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1683 | { mpz_init_set(this->val_, *val); } |
1684 | ||
1685 | static Expression* | |
1686 | do_import(Import*); | |
1687 | ||
8b1c301d | 1688 | // Write VAL to string dump. |
e440a328 | 1689 | static void |
8b1c301d | 1690 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1691 | |
d751bb78 | 1692 | // Write VAL to dump context. |
1693 | static void | |
1694 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1695 | ||
e440a328 | 1696 | protected: |
1697 | bool | |
1698 | do_is_constant() const | |
1699 | { return true; } | |
1700 | ||
1701 | bool | |
0c77715b | 1702 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1703 | |
1704 | Type* | |
1705 | do_type(); | |
1706 | ||
1707 | void | |
1708 | do_determine_type(const Type_context* context); | |
1709 | ||
1710 | void | |
1711 | do_check_types(Gogo*); | |
1712 | ||
1713 | tree | |
1714 | do_get_tree(Translate_context*); | |
1715 | ||
1716 | Expression* | |
1717 | do_copy() | |
5d4b8566 | 1718 | { |
1719 | if (this->is_character_constant_) | |
1720 | return Expression::make_character(&this->val_, this->type_, | |
1721 | this->location()); | |
1722 | else | |
1723 | return Expression::make_integer(&this->val_, this->type_, | |
1724 | this->location()); | |
1725 | } | |
e440a328 | 1726 | |
1727 | void | |
1728 | do_export(Export*) const; | |
1729 | ||
d751bb78 | 1730 | void |
1731 | do_dump_expression(Ast_dump_context*) const; | |
1732 | ||
e440a328 | 1733 | private: |
1734 | // The integer value. | |
1735 | mpz_t val_; | |
1736 | // The type so far. | |
1737 | Type* type_; | |
5d4b8566 | 1738 | // Whether this is a character constant. |
1739 | bool is_character_constant_; | |
e440a328 | 1740 | }; |
1741 | ||
0c77715b | 1742 | // Return a numeric constant for this expression. We have to mark |
1743 | // this as a character when appropriate. | |
e440a328 | 1744 | |
1745 | bool | |
0c77715b | 1746 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1747 | { |
0c77715b | 1748 | if (this->is_character_constant_) |
1749 | nc->set_rune(this->type_, this->val_); | |
1750 | else | |
1751 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1752 | return true; |
1753 | } | |
1754 | ||
1755 | // Return the current type. If we haven't set the type yet, we return | |
1756 | // an abstract integer type. | |
1757 | ||
1758 | Type* | |
1759 | Integer_expression::do_type() | |
1760 | { | |
1761 | if (this->type_ == NULL) | |
5d4b8566 | 1762 | { |
1763 | if (this->is_character_constant_) | |
1764 | this->type_ = Type::make_abstract_character_type(); | |
1765 | else | |
1766 | this->type_ = Type::make_abstract_integer_type(); | |
1767 | } | |
e440a328 | 1768 | return this->type_; |
1769 | } | |
1770 | ||
1771 | // Set the type of the integer value. Here we may switch from an | |
1772 | // abstract type to a real type. | |
1773 | ||
1774 | void | |
1775 | Integer_expression::do_determine_type(const Type_context* context) | |
1776 | { | |
1777 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1778 | ; | |
0c77715b | 1779 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1780 | this->type_ = context->type; |
1781 | else if (!context->may_be_abstract) | |
5d4b8566 | 1782 | { |
1783 | if (this->is_character_constant_) | |
1784 | this->type_ = Type::lookup_integer_type("int32"); | |
1785 | else | |
1786 | this->type_ = Type::lookup_integer_type("int"); | |
1787 | } | |
e440a328 | 1788 | } |
1789 | ||
e440a328 | 1790 | // Check the type of an integer constant. |
1791 | ||
1792 | void | |
1793 | Integer_expression::do_check_types(Gogo*) | |
1794 | { | |
0c77715b | 1795 | Type* type = this->type_; |
1796 | if (type == NULL) | |
e440a328 | 1797 | return; |
0c77715b | 1798 | Numeric_constant nc; |
1799 | if (this->is_character_constant_) | |
1800 | nc.set_rune(NULL, this->val_); | |
1801 | else | |
1802 | nc.set_int(NULL, this->val_); | |
1803 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1804 | this->set_is_error(); |
1805 | } | |
1806 | ||
1807 | // Get a tree for an integer constant. | |
1808 | ||
1809 | tree | |
1810 | Integer_expression::do_get_tree(Translate_context* context) | |
1811 | { | |
1812 | Gogo* gogo = context->gogo(); | |
1813 | tree type; | |
1814 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
9f0e0513 | 1815 | type = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 1816 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
1817 | { | |
1818 | // We are converting to an abstract floating point type. | |
9f0e0513 | 1819 | Type* ftype = Type::lookup_float_type("float64"); |
1820 | type = type_to_tree(ftype->get_backend(gogo)); | |
e440a328 | 1821 | } |
1822 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1823 | { | |
1824 | // We are converting to an abstract complex type. | |
9f0e0513 | 1825 | Type* ctype = Type::lookup_complex_type("complex128"); |
1826 | type = type_to_tree(ctype->get_backend(gogo)); | |
e440a328 | 1827 | } |
1828 | else | |
1829 | { | |
1830 | // If we still have an abstract type here, then this is being | |
1831 | // used in a constant expression which didn't get reduced for | |
1832 | // some reason. Use a type which will fit the value. We use <, | |
1833 | // not <=, because we need an extra bit for the sign bit. | |
1834 | int bits = mpz_sizeinbase(this->val_, 2); | |
1835 | if (bits < INT_TYPE_SIZE) | |
9f0e0513 | 1836 | { |
1837 | Type* t = Type::lookup_integer_type("int"); | |
1838 | type = type_to_tree(t->get_backend(gogo)); | |
1839 | } | |
e440a328 | 1840 | else if (bits < 64) |
9f0e0513 | 1841 | { |
1842 | Type* t = Type::lookup_integer_type("int64"); | |
1843 | type = type_to_tree(t->get_backend(gogo)); | |
1844 | } | |
e440a328 | 1845 | else |
1846 | type = long_long_integer_type_node; | |
1847 | } | |
1848 | return Expression::integer_constant_tree(this->val_, type); | |
1849 | } | |
1850 | ||
1851 | // Write VAL to export data. | |
1852 | ||
1853 | void | |
8b1c301d | 1854 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 1855 | { |
1856 | char* s = mpz_get_str(NULL, 10, val); | |
1857 | exp->write_c_string(s); | |
1858 | free(s); | |
1859 | } | |
1860 | ||
1861 | // Export an integer in a constant expression. | |
1862 | ||
1863 | void | |
1864 | Integer_expression::do_export(Export* exp) const | |
1865 | { | |
1866 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 1867 | if (this->is_character_constant_) |
1868 | exp->write_c_string("'"); | |
e440a328 | 1869 | // A trailing space lets us reliably identify the end of the number. |
1870 | exp->write_c_string(" "); | |
1871 | } | |
1872 | ||
1873 | // Import an integer, floating point, or complex value. This handles | |
1874 | // all these types because they all start with digits. | |
1875 | ||
1876 | Expression* | |
1877 | Integer_expression::do_import(Import* imp) | |
1878 | { | |
1879 | std::string num = imp->read_identifier(); | |
1880 | imp->require_c_string(" "); | |
1881 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1882 | { | |
1883 | mpfr_t real; | |
1884 | size_t plus_pos = num.find('+', 1); | |
1885 | size_t minus_pos = num.find('-', 1); | |
1886 | size_t pos; | |
1887 | if (plus_pos == std::string::npos) | |
1888 | pos = minus_pos; | |
1889 | else if (minus_pos == std::string::npos) | |
1890 | pos = plus_pos; | |
1891 | else | |
1892 | { | |
1893 | error_at(imp->location(), "bad number in import data: %qs", | |
1894 | num.c_str()); | |
1895 | return Expression::make_error(imp->location()); | |
1896 | } | |
1897 | if (pos == std::string::npos) | |
1898 | mpfr_set_ui(real, 0, GMP_RNDN); | |
1899 | else | |
1900 | { | |
1901 | std::string real_str = num.substr(0, pos); | |
1902 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
1903 | { | |
1904 | error_at(imp->location(), "bad number in import data: %qs", | |
1905 | real_str.c_str()); | |
1906 | return Expression::make_error(imp->location()); | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | std::string imag_str; | |
1911 | if (pos == std::string::npos) | |
1912 | imag_str = num; | |
1913 | else | |
1914 | imag_str = num.substr(pos); | |
1915 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
1916 | mpfr_t imag; | |
1917 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
1918 | { | |
1919 | error_at(imp->location(), "bad number in import data: %qs", | |
1920 | imag_str.c_str()); | |
1921 | return Expression::make_error(imp->location()); | |
1922 | } | |
1923 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
1924 | imp->location()); | |
1925 | mpfr_clear(real); | |
1926 | mpfr_clear(imag); | |
1927 | return ret; | |
1928 | } | |
1929 | else if (num.find('.') == std::string::npos | |
1930 | && num.find('E') == std::string::npos) | |
1931 | { | |
5d4b8566 | 1932 | bool is_character_constant = (!num.empty() |
1933 | && num[num.length() - 1] == '\''); | |
1934 | if (is_character_constant) | |
1935 | num = num.substr(0, num.length() - 1); | |
e440a328 | 1936 | mpz_t val; |
1937 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
1938 | { | |
1939 | error_at(imp->location(), "bad number in import data: %qs", | |
1940 | num.c_str()); | |
1941 | return Expression::make_error(imp->location()); | |
1942 | } | |
5d4b8566 | 1943 | Expression* ret; |
1944 | if (is_character_constant) | |
1945 | ret = Expression::make_character(&val, NULL, imp->location()); | |
1946 | else | |
1947 | ret = Expression::make_integer(&val, NULL, imp->location()); | |
e440a328 | 1948 | mpz_clear(val); |
1949 | return ret; | |
1950 | } | |
1951 | else | |
1952 | { | |
1953 | mpfr_t val; | |
1954 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
1955 | { | |
1956 | error_at(imp->location(), "bad number in import data: %qs", | |
1957 | num.c_str()); | |
1958 | return Expression::make_error(imp->location()); | |
1959 | } | |
1960 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
1961 | mpfr_clear(val); | |
1962 | return ret; | |
1963 | } | |
1964 | } | |
d751bb78 | 1965 | // Ast dump for integer expression. |
1966 | ||
1967 | void | |
1968 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1969 | { | |
5d4b8566 | 1970 | if (this->is_character_constant_) |
1971 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 1972 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 1973 | if (this->is_character_constant_) |
1974 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 1975 | } |
1976 | ||
e440a328 | 1977 | // Build a new integer value. |
1978 | ||
1979 | Expression* | |
5d4b8566 | 1980 | Expression::make_integer(const mpz_t* val, Type* type, Location location) |
1981 | { | |
1982 | return new Integer_expression(val, type, false, location); | |
1983 | } | |
1984 | ||
1985 | // Build a new character constant value. | |
1986 | ||
1987 | Expression* | |
1988 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 1989 | { |
5d4b8566 | 1990 | return new Integer_expression(val, type, true, location); |
e440a328 | 1991 | } |
1992 | ||
1993 | // Floats. | |
1994 | ||
1995 | class Float_expression : public Expression | |
1996 | { | |
1997 | public: | |
b13c66cd | 1998 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 1999 | : Expression(EXPRESSION_FLOAT, location), |
2000 | type_(type) | |
2001 | { | |
2002 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2003 | } | |
2004 | ||
e440a328 | 2005 | // Write VAL to export data. |
2006 | static void | |
8b1c301d | 2007 | export_float(String_dump* exp, const mpfr_t val); |
2008 | ||
d751bb78 | 2009 | // Write VAL to dump file. |
2010 | static void | |
2011 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2012 | |
2013 | protected: | |
2014 | bool | |
2015 | do_is_constant() const | |
2016 | { return true; } | |
2017 | ||
2018 | bool | |
0c77715b | 2019 | do_numeric_constant_value(Numeric_constant* nc) const |
2020 | { | |
2021 | nc->set_float(this->type_, this->val_); | |
2022 | return true; | |
2023 | } | |
e440a328 | 2024 | |
2025 | Type* | |
2026 | do_type(); | |
2027 | ||
2028 | void | |
2029 | do_determine_type(const Type_context*); | |
2030 | ||
2031 | void | |
2032 | do_check_types(Gogo*); | |
2033 | ||
2034 | Expression* | |
2035 | do_copy() | |
2036 | { return Expression::make_float(&this->val_, this->type_, | |
2037 | this->location()); } | |
2038 | ||
2039 | tree | |
2040 | do_get_tree(Translate_context*); | |
2041 | ||
2042 | void | |
2043 | do_export(Export*) const; | |
2044 | ||
d751bb78 | 2045 | void |
2046 | do_dump_expression(Ast_dump_context*) const; | |
2047 | ||
e440a328 | 2048 | private: |
2049 | // The floating point value. | |
2050 | mpfr_t val_; | |
2051 | // The type so far. | |
2052 | Type* type_; | |
2053 | }; | |
2054 | ||
e440a328 | 2055 | // Return the current type. If we haven't set the type yet, we return |
2056 | // an abstract float type. | |
2057 | ||
2058 | Type* | |
2059 | Float_expression::do_type() | |
2060 | { | |
2061 | if (this->type_ == NULL) | |
2062 | this->type_ = Type::make_abstract_float_type(); | |
2063 | return this->type_; | |
2064 | } | |
2065 | ||
2066 | // Set the type of the float value. Here we may switch from an | |
2067 | // abstract type to a real type. | |
2068 | ||
2069 | void | |
2070 | Float_expression::do_determine_type(const Type_context* context) | |
2071 | { | |
2072 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2073 | ; | |
2074 | else if (context->type != NULL | |
2075 | && (context->type->integer_type() != NULL | |
2076 | || context->type->float_type() != NULL | |
2077 | || context->type->complex_type() != NULL)) | |
2078 | this->type_ = context->type; | |
2079 | else if (!context->may_be_abstract) | |
48080209 | 2080 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2081 | } |
2082 | ||
e440a328 | 2083 | // Check the type of a float value. |
2084 | ||
2085 | void | |
2086 | Float_expression::do_check_types(Gogo*) | |
2087 | { | |
0c77715b | 2088 | Type* type = this->type_; |
2089 | if (type == NULL) | |
e440a328 | 2090 | return; |
0c77715b | 2091 | Numeric_constant nc; |
2092 | nc.set_float(NULL, this->val_); | |
2093 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2094 | this->set_is_error(); |
e440a328 | 2095 | } |
2096 | ||
2097 | // Get a tree for a float constant. | |
2098 | ||
2099 | tree | |
2100 | Float_expression::do_get_tree(Translate_context* context) | |
2101 | { | |
2102 | Gogo* gogo = context->gogo(); | |
2103 | tree type; | |
2104 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
9f0e0513 | 2105 | type = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2106 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2107 | { | |
2108 | // We have an abstract integer type. We just hope for the best. | |
9f0e0513 | 2109 | type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo)); |
e440a328 | 2110 | } |
2111 | else | |
2112 | { | |
2113 | // If we still have an abstract type here, then this is being | |
2114 | // used in a constant expression which didn't get reduced. We | |
2115 | // just use float64 and hope for the best. | |
9f0e0513 | 2116 | Type* ft = Type::lookup_float_type("float64"); |
2117 | type = type_to_tree(ft->get_backend(gogo)); | |
e440a328 | 2118 | } |
2119 | return Expression::float_constant_tree(this->val_, type); | |
2120 | } | |
2121 | ||
8b1c301d | 2122 | // Write a floating point number to a string dump. |
e440a328 | 2123 | |
2124 | void | |
8b1c301d | 2125 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2126 | { |
2127 | mp_exp_t exponent; | |
2128 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2129 | if (*s == '-') | |
2130 | exp->write_c_string("-"); | |
2131 | exp->write_c_string("0."); | |
2132 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2133 | mpfr_free_str(s); | |
2134 | char buf[30]; | |
2135 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2136 | exp->write_c_string(buf); | |
2137 | } | |
2138 | ||
2139 | // Export a floating point number in a constant expression. | |
2140 | ||
2141 | void | |
2142 | Float_expression::do_export(Export* exp) const | |
2143 | { | |
2144 | Float_expression::export_float(exp, this->val_); | |
2145 | // A trailing space lets us reliably identify the end of the number. | |
2146 | exp->write_c_string(" "); | |
2147 | } | |
2148 | ||
d751bb78 | 2149 | // Dump a floating point number to the dump file. |
2150 | ||
2151 | void | |
2152 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2153 | { | |
8b1c301d | 2154 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2155 | } |
2156 | ||
e440a328 | 2157 | // Make a float expression. |
2158 | ||
2159 | Expression* | |
b13c66cd | 2160 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2161 | { |
2162 | return new Float_expression(val, type, location); | |
2163 | } | |
2164 | ||
2165 | // Complex numbers. | |
2166 | ||
2167 | class Complex_expression : public Expression | |
2168 | { | |
2169 | public: | |
2170 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2171 | Location location) |
e440a328 | 2172 | : Expression(EXPRESSION_COMPLEX, location), |
2173 | type_(type) | |
2174 | { | |
2175 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2176 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2177 | } | |
2178 | ||
8b1c301d | 2179 | // Write REAL/IMAG to string dump. |
e440a328 | 2180 | static void |
8b1c301d | 2181 | export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val); |
e440a328 | 2182 | |
d751bb78 | 2183 | // Write REAL/IMAG to dump context. |
2184 | static void | |
2185 | dump_complex(Ast_dump_context* ast_dump_context, | |
2186 | const mpfr_t real, const mpfr_t val); | |
2187 | ||
e440a328 | 2188 | protected: |
2189 | bool | |
2190 | do_is_constant() const | |
2191 | { return true; } | |
2192 | ||
2193 | bool | |
0c77715b | 2194 | do_numeric_constant_value(Numeric_constant* nc) const |
2195 | { | |
2196 | nc->set_complex(this->type_, this->real_, this->imag_); | |
2197 | return true; | |
2198 | } | |
e440a328 | 2199 | |
2200 | Type* | |
2201 | do_type(); | |
2202 | ||
2203 | void | |
2204 | do_determine_type(const Type_context*); | |
2205 | ||
2206 | void | |
2207 | do_check_types(Gogo*); | |
2208 | ||
2209 | Expression* | |
2210 | do_copy() | |
2211 | { | |
2212 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2213 | this->location()); | |
2214 | } | |
2215 | ||
2216 | tree | |
2217 | do_get_tree(Translate_context*); | |
2218 | ||
2219 | void | |
2220 | do_export(Export*) const; | |
2221 | ||
d751bb78 | 2222 | void |
2223 | do_dump_expression(Ast_dump_context*) const; | |
2224 | ||
e440a328 | 2225 | private: |
2226 | // The real part. | |
2227 | mpfr_t real_; | |
2228 | // The imaginary part; | |
2229 | mpfr_t imag_; | |
2230 | // The type if known. | |
2231 | Type* type_; | |
2232 | }; | |
2233 | ||
e440a328 | 2234 | // Return the current type. If we haven't set the type yet, we return |
2235 | // an abstract complex type. | |
2236 | ||
2237 | Type* | |
2238 | Complex_expression::do_type() | |
2239 | { | |
2240 | if (this->type_ == NULL) | |
2241 | this->type_ = Type::make_abstract_complex_type(); | |
2242 | return this->type_; | |
2243 | } | |
2244 | ||
2245 | // Set the type of the complex value. Here we may switch from an | |
2246 | // abstract type to a real type. | |
2247 | ||
2248 | void | |
2249 | Complex_expression::do_determine_type(const Type_context* context) | |
2250 | { | |
2251 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2252 | ; | |
2253 | else if (context->type != NULL | |
2254 | && context->type->complex_type() != NULL) | |
2255 | this->type_ = context->type; | |
2256 | else if (!context->may_be_abstract) | |
48080209 | 2257 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2258 | } |
2259 | ||
e440a328 | 2260 | // Check the type of a complex value. |
2261 | ||
2262 | void | |
2263 | Complex_expression::do_check_types(Gogo*) | |
2264 | { | |
0c77715b | 2265 | Type* type = this->type_; |
2266 | if (type == NULL) | |
e440a328 | 2267 | return; |
0c77715b | 2268 | Numeric_constant nc; |
2269 | nc.set_complex(NULL, this->real_, this->imag_); | |
2270 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2271 | this->set_is_error(); |
2272 | } | |
2273 | ||
2274 | // Get a tree for a complex constant. | |
2275 | ||
2276 | tree | |
2277 | Complex_expression::do_get_tree(Translate_context* context) | |
2278 | { | |
2279 | Gogo* gogo = context->gogo(); | |
2280 | tree type; | |
2281 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
9f0e0513 | 2282 | type = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2283 | else |
2284 | { | |
2285 | // If we still have an abstract type here, this this is being | |
2286 | // used in a constant expression which didn't get reduced. We | |
2287 | // just use complex128 and hope for the best. | |
9f0e0513 | 2288 | Type* ct = Type::lookup_complex_type("complex128"); |
2289 | type = type_to_tree(ct->get_backend(gogo)); | |
e440a328 | 2290 | } |
2291 | return Expression::complex_constant_tree(this->real_, this->imag_, type); | |
2292 | } | |
2293 | ||
2294 | // Write REAL/IMAG to export data. | |
2295 | ||
2296 | void | |
8b1c301d | 2297 | Complex_expression::export_complex(String_dump* exp, const mpfr_t real, |
e440a328 | 2298 | const mpfr_t imag) |
2299 | { | |
2300 | if (!mpfr_zero_p(real)) | |
2301 | { | |
2302 | Float_expression::export_float(exp, real); | |
2303 | if (mpfr_sgn(imag) > 0) | |
2304 | exp->write_c_string("+"); | |
2305 | } | |
2306 | Float_expression::export_float(exp, imag); | |
2307 | exp->write_c_string("i"); | |
2308 | } | |
2309 | ||
2310 | // Export a complex number in a constant expression. | |
2311 | ||
2312 | void | |
2313 | Complex_expression::do_export(Export* exp) const | |
2314 | { | |
2315 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2316 | // A trailing space lets us reliably identify the end of the number. | |
2317 | exp->write_c_string(" "); | |
2318 | } | |
2319 | ||
d751bb78 | 2320 | // Dump a complex expression to the dump file. |
2321 | ||
2322 | void | |
2323 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2324 | { | |
8b1c301d | 2325 | Complex_expression::export_complex(ast_dump_context, |
d751bb78 | 2326 | this->real_, |
2327 | this->imag_); | |
2328 | } | |
2329 | ||
e440a328 | 2330 | // Make a complex expression. |
2331 | ||
2332 | Expression* | |
2333 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2334 | Location location) |
e440a328 | 2335 | { |
2336 | return new Complex_expression(real, imag, type, location); | |
2337 | } | |
2338 | ||
d5b605df | 2339 | // Find a named object in an expression. |
2340 | ||
2341 | class Find_named_object : public Traverse | |
2342 | { | |
2343 | public: | |
2344 | Find_named_object(Named_object* no) | |
2345 | : Traverse(traverse_expressions), | |
2346 | no_(no), found_(false) | |
2347 | { } | |
2348 | ||
2349 | // Whether we found the object. | |
2350 | bool | |
2351 | found() const | |
2352 | { return this->found_; } | |
2353 | ||
2354 | protected: | |
2355 | int | |
2356 | expression(Expression**); | |
2357 | ||
2358 | private: | |
2359 | // The object we are looking for. | |
2360 | Named_object* no_; | |
2361 | // Whether we found it. | |
2362 | bool found_; | |
2363 | }; | |
2364 | ||
e440a328 | 2365 | // A reference to a const in an expression. |
2366 | ||
2367 | class Const_expression : public Expression | |
2368 | { | |
2369 | public: | |
b13c66cd | 2370 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2371 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2372 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2373 | { } |
2374 | ||
d5b605df | 2375 | Named_object* |
2376 | named_object() | |
2377 | { return this->constant_; } | |
2378 | ||
a7f064d5 | 2379 | // Check that the initializer does not refer to the constant itself. |
2380 | void | |
2381 | check_for_init_loop(); | |
2382 | ||
e440a328 | 2383 | protected: |
ba4aedd4 | 2384 | int |
2385 | do_traverse(Traverse*); | |
2386 | ||
e440a328 | 2387 | Expression* |
ceeb4318 | 2388 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2389 | |
2390 | bool | |
2391 | do_is_constant() const | |
2392 | { return true; } | |
2393 | ||
2394 | bool | |
0c77715b | 2395 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2396 | |
2397 | bool | |
af6b489a | 2398 | do_string_constant_value(std::string* val) const; |
e440a328 | 2399 | |
2400 | Type* | |
2401 | do_type(); | |
2402 | ||
2403 | // The type of a const is set by the declaration, not the use. | |
2404 | void | |
2405 | do_determine_type(const Type_context*); | |
2406 | ||
2407 | void | |
2408 | do_check_types(Gogo*); | |
2409 | ||
2410 | Expression* | |
2411 | do_copy() | |
2412 | { return this; } | |
2413 | ||
2414 | tree | |
2415 | do_get_tree(Translate_context* context); | |
2416 | ||
2417 | // When exporting a reference to a const as part of a const | |
2418 | // expression, we export the value. We ignore the fact that it has | |
2419 | // a name. | |
2420 | void | |
2421 | do_export(Export* exp) const | |
2422 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2423 | ||
d751bb78 | 2424 | void |
2425 | do_dump_expression(Ast_dump_context*) const; | |
2426 | ||
e440a328 | 2427 | private: |
2428 | // The constant. | |
2429 | Named_object* constant_; | |
2430 | // The type of this reference. This is used if the constant has an | |
2431 | // abstract type. | |
2432 | Type* type_; | |
13e818f5 | 2433 | // Used to prevent infinite recursion when a constant incorrectly |
2434 | // refers to itself. | |
2435 | mutable bool seen_; | |
e440a328 | 2436 | }; |
2437 | ||
ba4aedd4 | 2438 | // Traversal. |
2439 | ||
2440 | int | |
2441 | Const_expression::do_traverse(Traverse* traverse) | |
2442 | { | |
2443 | if (this->type_ != NULL) | |
2444 | return Type::traverse(this->type_, traverse); | |
2445 | return TRAVERSE_CONTINUE; | |
2446 | } | |
2447 | ||
e440a328 | 2448 | // Lower a constant expression. This is where we convert the |
2449 | // predeclared constant iota into an integer value. | |
2450 | ||
2451 | Expression* | |
ceeb4318 | 2452 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2453 | Statement_inserter*, int iota_value) | |
e440a328 | 2454 | { |
2455 | if (this->constant_->const_value()->expr()->classification() | |
2456 | == EXPRESSION_IOTA) | |
2457 | { | |
2458 | if (iota_value == -1) | |
2459 | { | |
2460 | error_at(this->location(), | |
2461 | "iota is only defined in const declarations"); | |
2462 | iota_value = 0; | |
2463 | } | |
2464 | mpz_t val; | |
2465 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2466 | Expression* ret = Expression::make_integer(&val, NULL, | |
2467 | this->location()); | |
2468 | mpz_clear(val); | |
2469 | return ret; | |
2470 | } | |
2471 | ||
2472 | // Make sure that the constant itself has been lowered. | |
2473 | gogo->lower_constant(this->constant_); | |
2474 | ||
2475 | return this; | |
2476 | } | |
2477 | ||
0c77715b | 2478 | // Return a numeric constant value. |
e440a328 | 2479 | |
2480 | bool | |
0c77715b | 2481 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2482 | { |
13e818f5 | 2483 | if (this->seen_) |
2484 | return false; | |
2485 | ||
e440a328 | 2486 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2487 | |
13e818f5 | 2488 | this->seen_ = true; |
2489 | ||
0c77715b | 2490 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2491 | |
13e818f5 | 2492 | this->seen_ = false; |
2493 | ||
e440a328 | 2494 | Type* ctype; |
2495 | if (this->type_ != NULL) | |
2496 | ctype = this->type_; | |
2497 | else | |
2498 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2499 | if (r && ctype != NULL) |
2500 | { | |
0c77715b | 2501 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2502 | return false; |
e440a328 | 2503 | } |
e440a328 | 2504 | |
e440a328 | 2505 | return r; |
2506 | } | |
2507 | ||
af6b489a | 2508 | bool |
2509 | Const_expression::do_string_constant_value(std::string* val) const | |
2510 | { | |
2511 | if (this->seen_) | |
2512 | return false; | |
2513 | ||
2514 | Expression* e = this->constant_->const_value()->expr(); | |
2515 | ||
2516 | this->seen_ = true; | |
2517 | bool ok = e->string_constant_value(val); | |
2518 | this->seen_ = false; | |
2519 | ||
2520 | return ok; | |
2521 | } | |
2522 | ||
e440a328 | 2523 | // Return the type of the const reference. |
2524 | ||
2525 | Type* | |
2526 | Const_expression::do_type() | |
2527 | { | |
2528 | if (this->type_ != NULL) | |
2529 | return this->type_; | |
13e818f5 | 2530 | |
2f78f012 | 2531 | Named_constant* nc = this->constant_->const_value(); |
2532 | ||
2533 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2534 | { |
2535 | this->report_error(_("constant refers to itself")); | |
2536 | this->type_ = Type::make_error_type(); | |
2537 | return this->type_; | |
2538 | } | |
2539 | ||
2540 | this->seen_ = true; | |
2541 | ||
e440a328 | 2542 | Type* ret = nc->type(); |
13e818f5 | 2543 | |
e440a328 | 2544 | if (ret != NULL) |
13e818f5 | 2545 | { |
2546 | this->seen_ = false; | |
2547 | return ret; | |
2548 | } | |
2549 | ||
e440a328 | 2550 | // During parsing, a named constant may have a NULL type, but we |
2551 | // must not return a NULL type here. | |
13e818f5 | 2552 | ret = nc->expr()->type(); |
2553 | ||
2554 | this->seen_ = false; | |
2555 | ||
2556 | return ret; | |
e440a328 | 2557 | } |
2558 | ||
2559 | // Set the type of the const reference. | |
2560 | ||
2561 | void | |
2562 | Const_expression::do_determine_type(const Type_context* context) | |
2563 | { | |
2564 | Type* ctype = this->constant_->const_value()->type(); | |
2565 | Type* cetype = (ctype != NULL | |
2566 | ? ctype | |
2567 | : this->constant_->const_value()->expr()->type()); | |
2568 | if (ctype != NULL && !ctype->is_abstract()) | |
2569 | ; | |
2570 | else if (context->type != NULL | |
0c77715b | 2571 | && context->type->is_numeric_type() |
2572 | && cetype->is_numeric_type()) | |
e440a328 | 2573 | this->type_ = context->type; |
2574 | else if (context->type != NULL | |
2575 | && context->type->is_string_type() | |
2576 | && cetype->is_string_type()) | |
2577 | this->type_ = context->type; | |
2578 | else if (context->type != NULL | |
2579 | && context->type->is_boolean_type() | |
2580 | && cetype->is_boolean_type()) | |
2581 | this->type_ = context->type; | |
2582 | else if (!context->may_be_abstract) | |
2583 | { | |
2584 | if (cetype->is_abstract()) | |
2585 | cetype = cetype->make_non_abstract_type(); | |
2586 | this->type_ = cetype; | |
2587 | } | |
2588 | } | |
2589 | ||
a7f064d5 | 2590 | // Check for a loop in which the initializer of a constant refers to |
2591 | // the constant itself. | |
e440a328 | 2592 | |
2593 | void | |
a7f064d5 | 2594 | Const_expression::check_for_init_loop() |
e440a328 | 2595 | { |
5c13bd80 | 2596 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2597 | return; |
2598 | ||
a7f064d5 | 2599 | if (this->seen_) |
2600 | { | |
2601 | this->report_error(_("constant refers to itself")); | |
2602 | this->type_ = Type::make_error_type(); | |
2603 | return; | |
2604 | } | |
2605 | ||
d5b605df | 2606 | Expression* init = this->constant_->const_value()->expr(); |
2607 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2608 | |
2609 | this->seen_ = true; | |
d5b605df | 2610 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2611 | this->seen_ = false; |
2612 | ||
d5b605df | 2613 | if (find_named_object.found()) |
2614 | { | |
5c13bd80 | 2615 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2616 | { |
2617 | this->report_error(_("constant refers to itself")); | |
2618 | this->type_ = Type::make_error_type(); | |
2619 | } | |
d5b605df | 2620 | return; |
2621 | } | |
a7f064d5 | 2622 | } |
2623 | ||
2624 | // Check types of a const reference. | |
2625 | ||
2626 | void | |
2627 | Const_expression::do_check_types(Gogo*) | |
2628 | { | |
5c13bd80 | 2629 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2630 | return; |
2631 | ||
2632 | this->check_for_init_loop(); | |
d5b605df | 2633 | |
0c77715b | 2634 | // Check that numeric constant fits in type. |
2635 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2636 | { |
0c77715b | 2637 | Numeric_constant nc; |
2638 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2639 | { |
0c77715b | 2640 | if (!nc.set_type(this->type_, true, this->location())) |
2641 | this->set_is_error(); | |
e440a328 | 2642 | } |
e440a328 | 2643 | } |
2644 | } | |
2645 | ||
2646 | // Return a tree for the const reference. | |
2647 | ||
2648 | tree | |
2649 | Const_expression::do_get_tree(Translate_context* context) | |
2650 | { | |
2651 | Gogo* gogo = context->gogo(); | |
2652 | tree type_tree; | |
2653 | if (this->type_ == NULL) | |
2654 | type_tree = NULL_TREE; | |
2655 | else | |
2656 | { | |
9f0e0513 | 2657 | type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2658 | if (type_tree == error_mark_node) |
2659 | return error_mark_node; | |
2660 | } | |
2661 | ||
2662 | // If the type has been set for this expression, but the underlying | |
2663 | // object is an abstract int or float, we try to get the abstract | |
2664 | // value. Otherwise we may lose something in the conversion. | |
2665 | if (this->type_ != NULL | |
0c77715b | 2666 | && this->type_->is_numeric_type() |
a68492b4 | 2667 | && (this->constant_->const_value()->type() == NULL |
2668 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2669 | { |
2670 | Expression* expr = this->constant_->const_value()->expr(); | |
0c77715b | 2671 | Numeric_constant nc; |
2672 | if (expr->numeric_constant_value(&nc) | |
2673 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2674 | { |
0c77715b | 2675 | Expression* e = nc.expression(this->location()); |
2676 | return e->get_tree(context); | |
e440a328 | 2677 | } |
e440a328 | 2678 | } |
2679 | ||
2680 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2681 | if (this->type_ == NULL | |
2682 | || const_tree == error_mark_node | |
2683 | || TREE_TYPE(const_tree) == error_mark_node) | |
2684 | return const_tree; | |
2685 | ||
2686 | tree ret; | |
2687 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2688 | ret = fold_convert(type_tree, const_tree); | |
2689 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2690 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2691 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2692 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2693 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2694 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2695 | else | |
c3e6f413 | 2696 | go_unreachable(); |
e440a328 | 2697 | return ret; |
2698 | } | |
2699 | ||
d751bb78 | 2700 | // Dump ast representation for constant expression. |
2701 | ||
2702 | void | |
2703 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2704 | { | |
2705 | ast_dump_context->ostream() << this->constant_->name(); | |
2706 | } | |
2707 | ||
e440a328 | 2708 | // Make a reference to a constant in an expression. |
2709 | ||
2710 | Expression* | |
2711 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2712 | Location location) |
e440a328 | 2713 | { |
2714 | return new Const_expression(constant, location); | |
2715 | } | |
2716 | ||
d5b605df | 2717 | // Find a named object in an expression. |
2718 | ||
2719 | int | |
2720 | Find_named_object::expression(Expression** pexpr) | |
2721 | { | |
2722 | switch ((*pexpr)->classification()) | |
2723 | { | |
2724 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2725 | { |
2726 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2727 | if (ce->named_object() == this->no_) | |
2728 | break; | |
2729 | ||
2730 | // We need to check a constant initializer explicitly, as | |
2731 | // loops here will not be caught by the loop checking for | |
2732 | // variable initializers. | |
2733 | ce->check_for_init_loop(); | |
2734 | ||
2735 | return TRAVERSE_CONTINUE; | |
2736 | } | |
2737 | ||
d5b605df | 2738 | case Expression::EXPRESSION_VAR_REFERENCE: |
2739 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2740 | break; | |
2741 | return TRAVERSE_CONTINUE; | |
2742 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2743 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2744 | break; | |
2745 | return TRAVERSE_CONTINUE; | |
2746 | default: | |
2747 | return TRAVERSE_CONTINUE; | |
2748 | } | |
2749 | this->found_ = true; | |
2750 | return TRAVERSE_EXIT; | |
2751 | } | |
2752 | ||
e440a328 | 2753 | // The nil value. |
2754 | ||
2755 | class Nil_expression : public Expression | |
2756 | { | |
2757 | public: | |
b13c66cd | 2758 | Nil_expression(Location location) |
e440a328 | 2759 | : Expression(EXPRESSION_NIL, location) |
2760 | { } | |
2761 | ||
2762 | static Expression* | |
2763 | do_import(Import*); | |
2764 | ||
2765 | protected: | |
2766 | bool | |
2767 | do_is_constant() const | |
2768 | { return true; } | |
2769 | ||
2770 | Type* | |
2771 | do_type() | |
2772 | { return Type::make_nil_type(); } | |
2773 | ||
2774 | void | |
2775 | do_determine_type(const Type_context*) | |
2776 | { } | |
2777 | ||
2778 | Expression* | |
2779 | do_copy() | |
2780 | { return this; } | |
2781 | ||
2782 | tree | |
2783 | do_get_tree(Translate_context*) | |
2784 | { return null_pointer_node; } | |
2785 | ||
2786 | void | |
2787 | do_export(Export* exp) const | |
2788 | { exp->write_c_string("nil"); } | |
d751bb78 | 2789 | |
2790 | void | |
2791 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2792 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 2793 | }; |
2794 | ||
2795 | // Import a nil expression. | |
2796 | ||
2797 | Expression* | |
2798 | Nil_expression::do_import(Import* imp) | |
2799 | { | |
2800 | imp->require_c_string("nil"); | |
2801 | return Expression::make_nil(imp->location()); | |
2802 | } | |
2803 | ||
2804 | // Make a nil expression. | |
2805 | ||
2806 | Expression* | |
b13c66cd | 2807 | Expression::make_nil(Location location) |
e440a328 | 2808 | { |
2809 | return new Nil_expression(location); | |
2810 | } | |
2811 | ||
2812 | // The value of the predeclared constant iota. This is little more | |
2813 | // than a marker. This will be lowered to an integer in | |
2814 | // Const_expression::do_lower, which is where we know the value that | |
2815 | // it should have. | |
2816 | ||
2817 | class Iota_expression : public Parser_expression | |
2818 | { | |
2819 | public: | |
b13c66cd | 2820 | Iota_expression(Location location) |
e440a328 | 2821 | : Parser_expression(EXPRESSION_IOTA, location) |
2822 | { } | |
2823 | ||
2824 | protected: | |
2825 | Expression* | |
ceeb4318 | 2826 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 2827 | { go_unreachable(); } |
e440a328 | 2828 | |
2829 | // There should only ever be one of these. | |
2830 | Expression* | |
2831 | do_copy() | |
c3e6f413 | 2832 | { go_unreachable(); } |
d751bb78 | 2833 | |
2834 | void | |
2835 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2836 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 2837 | }; |
2838 | ||
2839 | // Make an iota expression. This is only called for one case: the | |
2840 | // value of the predeclared constant iota. | |
2841 | ||
2842 | Expression* | |
2843 | Expression::make_iota() | |
2844 | { | |
b13c66cd | 2845 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 2846 | return &iota_expression; |
2847 | } | |
2848 | ||
2849 | // A type conversion expression. | |
2850 | ||
2851 | class Type_conversion_expression : public Expression | |
2852 | { | |
2853 | public: | |
2854 | Type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 2855 | Location location) |
e440a328 | 2856 | : Expression(EXPRESSION_CONVERSION, location), |
2857 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2858 | { } | |
2859 | ||
2860 | // Return the type to which we are converting. | |
2861 | Type* | |
2862 | type() const | |
2863 | { return this->type_; } | |
2864 | ||
2865 | // Return the expression which we are converting. | |
2866 | Expression* | |
2867 | expr() const | |
2868 | { return this->expr_; } | |
2869 | ||
2870 | // Permit converting from one function type to another. This is | |
2871 | // used internally for method expressions. | |
2872 | void | |
2873 | set_may_convert_function_types() | |
2874 | { | |
2875 | this->may_convert_function_types_ = true; | |
2876 | } | |
2877 | ||
2878 | // Import a type conversion expression. | |
2879 | static Expression* | |
2880 | do_import(Import*); | |
2881 | ||
2882 | protected: | |
2883 | int | |
2884 | do_traverse(Traverse* traverse); | |
2885 | ||
2886 | Expression* | |
ceeb4318 | 2887 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2888 | |
2889 | bool | |
2890 | do_is_constant() const | |
2891 | { return this->expr_->is_constant(); } | |
2892 | ||
2893 | bool | |
0c77715b | 2894 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 2895 | |
2896 | bool | |
2897 | do_string_constant_value(std::string*) const; | |
2898 | ||
2899 | Type* | |
2900 | do_type() | |
2901 | { return this->type_; } | |
2902 | ||
2903 | void | |
2904 | do_determine_type(const Type_context*) | |
2905 | { | |
2906 | Type_context subcontext(this->type_, false); | |
2907 | this->expr_->determine_type(&subcontext); | |
2908 | } | |
2909 | ||
2910 | void | |
2911 | do_check_types(Gogo*); | |
2912 | ||
2913 | Expression* | |
2914 | do_copy() | |
2915 | { | |
2916 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
2917 | this->location()); | |
2918 | } | |
2919 | ||
2920 | tree | |
2921 | do_get_tree(Translate_context* context); | |
2922 | ||
2923 | void | |
2924 | do_export(Export*) const; | |
2925 | ||
d751bb78 | 2926 | void |
2927 | do_dump_expression(Ast_dump_context*) const; | |
2928 | ||
e440a328 | 2929 | private: |
2930 | // The type to convert to. | |
2931 | Type* type_; | |
2932 | // The expression to convert. | |
2933 | Expression* expr_; | |
2934 | // True if this is permitted to convert function types. This is | |
2935 | // used internally for method expressions. | |
2936 | bool may_convert_function_types_; | |
2937 | }; | |
2938 | ||
2939 | // Traversal. | |
2940 | ||
2941 | int | |
2942 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
2943 | { | |
2944 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
2945 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
2946 | return TRAVERSE_EXIT; | |
2947 | return TRAVERSE_CONTINUE; | |
2948 | } | |
2949 | ||
2950 | // Convert to a constant at lowering time. | |
2951 | ||
2952 | Expression* | |
ceeb4318 | 2953 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
2954 | Statement_inserter*, int) | |
e440a328 | 2955 | { |
2956 | Type* type = this->type_; | |
2957 | Expression* val = this->expr_; | |
b13c66cd | 2958 | Location location = this->location(); |
e440a328 | 2959 | |
0c77715b | 2960 | if (type->is_numeric_type()) |
e440a328 | 2961 | { |
0c77715b | 2962 | Numeric_constant nc; |
2963 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 2964 | { |
0c77715b | 2965 | if (!nc.set_type(type, true, location)) |
2966 | return Expression::make_error(location); | |
2967 | return nc.expression(location); | |
e440a328 | 2968 | } |
e440a328 | 2969 | } |
2970 | ||
55072f2b | 2971 | if (type->is_slice_type()) |
e440a328 | 2972 | { |
2973 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 2974 | bool is_byte = (element_type->integer_type() != NULL |
2975 | && element_type->integer_type()->is_byte()); | |
2976 | bool is_rune = (element_type->integer_type() != NULL | |
2977 | && element_type->integer_type()->is_rune()); | |
2978 | if (is_byte || is_rune) | |
e440a328 | 2979 | { |
2980 | std::string s; | |
2981 | if (val->string_constant_value(&s)) | |
2982 | { | |
2983 | Expression_list* vals = new Expression_list(); | |
2984 | if (is_byte) | |
2985 | { | |
2986 | for (std::string::const_iterator p = s.begin(); | |
2987 | p != s.end(); | |
2988 | p++) | |
2989 | { | |
2990 | mpz_t val; | |
2991 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
2992 | Expression* v = Expression::make_integer(&val, | |
2993 | element_type, | |
2994 | location); | |
2995 | vals->push_back(v); | |
2996 | mpz_clear(val); | |
2997 | } | |
2998 | } | |
2999 | else | |
3000 | { | |
3001 | const char *p = s.data(); | |
3002 | const char *pend = s.data() + s.length(); | |
3003 | while (p < pend) | |
3004 | { | |
3005 | unsigned int c; | |
3006 | int adv = Lex::fetch_char(p, &c); | |
3007 | if (adv == 0) | |
3008 | { | |
3009 | warning_at(this->location(), 0, | |
3010 | "invalid UTF-8 encoding"); | |
3011 | adv = 1; | |
3012 | } | |
3013 | p += adv; | |
3014 | mpz_t val; | |
3015 | mpz_init_set_ui(val, c); | |
3016 | Expression* v = Expression::make_integer(&val, | |
3017 | element_type, | |
3018 | location); | |
3019 | vals->push_back(v); | |
3020 | mpz_clear(val); | |
3021 | } | |
3022 | } | |
3023 | ||
3024 | return Expression::make_slice_composite_literal(type, vals, | |
3025 | location); | |
3026 | } | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | return this; | |
3031 | } | |
3032 | ||
0c77715b | 3033 | // Return the constant numeric value if there is one. |
e440a328 | 3034 | |
3035 | bool | |
0c77715b | 3036 | Type_conversion_expression::do_numeric_constant_value( |
3037 | Numeric_constant* nc) const | |
e440a328 | 3038 | { |
0c77715b | 3039 | if (!this->type_->is_numeric_type()) |
e440a328 | 3040 | return false; |
0c77715b | 3041 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3042 | return false; |
0c77715b | 3043 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3044 | } |
3045 | ||
3046 | // Return the constant string value if there is one. | |
3047 | ||
3048 | bool | |
3049 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3050 | { | |
3051 | if (this->type_->is_string_type() | |
3052 | && this->expr_->type()->integer_type() != NULL) | |
3053 | { | |
0c77715b | 3054 | Numeric_constant nc; |
3055 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3056 | { |
0c77715b | 3057 | unsigned long ival; |
3058 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3059 | { |
0c77715b | 3060 | val->clear(); |
3061 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3062 | return true; |
3063 | } | |
3064 | } | |
e440a328 | 3065 | } |
3066 | ||
3067 | // FIXME: Could handle conversion from const []int here. | |
3068 | ||
3069 | return false; | |
3070 | } | |
3071 | ||
3072 | // Check that types are convertible. | |
3073 | ||
3074 | void | |
3075 | Type_conversion_expression::do_check_types(Gogo*) | |
3076 | { | |
3077 | Type* type = this->type_; | |
3078 | Type* expr_type = this->expr_->type(); | |
3079 | std::string reason; | |
3080 | ||
5c13bd80 | 3081 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3082 | { |
842f6425 | 3083 | this->set_is_error(); |
3084 | return; | |
3085 | } | |
3086 | ||
e440a328 | 3087 | if (this->may_convert_function_types_ |
3088 | && type->function_type() != NULL | |
3089 | && expr_type->function_type() != NULL) | |
3090 | return; | |
3091 | ||
3092 | if (Type::are_convertible(type, expr_type, &reason)) | |
3093 | return; | |
3094 | ||
3095 | error_at(this->location(), "%s", reason.c_str()); | |
3096 | this->set_is_error(); | |
3097 | } | |
3098 | ||
3099 | // Get a tree for a type conversion. | |
3100 | ||
3101 | tree | |
3102 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3103 | { | |
3104 | Gogo* gogo = context->gogo(); | |
9f0e0513 | 3105 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 3106 | tree expr_tree = this->expr_->get_tree(context); |
3107 | ||
3108 | if (type_tree == error_mark_node | |
3109 | || expr_tree == error_mark_node | |
3110 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3111 | return error_mark_node; | |
3112 | ||
3113 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3114 | return fold_convert(type_tree, expr_tree); | |
3115 | ||
3116 | Type* type = this->type_; | |
3117 | Type* expr_type = this->expr_->type(); | |
3118 | tree ret; | |
3119 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3120 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3121 | expr_tree, this->location()); | |
3122 | else if (type->integer_type() != NULL) | |
3123 | { | |
3124 | if (expr_type->integer_type() != NULL | |
3125 | || expr_type->float_type() != NULL | |
3126 | || expr_type->is_unsafe_pointer_type()) | |
3127 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3128 | else | |
c3e6f413 | 3129 | go_unreachable(); |
e440a328 | 3130 | } |
3131 | else if (type->float_type() != NULL) | |
3132 | { | |
3133 | if (expr_type->integer_type() != NULL | |
3134 | || expr_type->float_type() != NULL) | |
3135 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3136 | else | |
c3e6f413 | 3137 | go_unreachable(); |
e440a328 | 3138 | } |
3139 | else if (type->complex_type() != NULL) | |
3140 | { | |
3141 | if (expr_type->complex_type() != NULL) | |
3142 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3143 | else | |
c3e6f413 | 3144 | go_unreachable(); |
e440a328 | 3145 | } |
3146 | else if (type->is_string_type() | |
3147 | && expr_type->integer_type() != NULL) | |
3148 | { | |
3149 | expr_tree = fold_convert(integer_type_node, expr_tree); | |
3150 | if (host_integerp(expr_tree, 0)) | |
3151 | { | |
3152 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3153 | std::string s; | |
3154 | Lex::append_char(intval, true, &s, this->location()); | |
3155 | Expression* se = Expression::make_string(s, this->location()); | |
3156 | return se->get_tree(context); | |
3157 | } | |
3158 | ||
3159 | static tree int_to_string_fndecl; | |
3160 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3161 | this->location(), | |
3162 | "__go_int_to_string", | |
3163 | 1, | |
3164 | type_tree, | |
3165 | integer_type_node, | |
3166 | fold_convert(integer_type_node, expr_tree)); | |
3167 | } | |
55072f2b | 3168 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3169 | { |
e440a328 | 3170 | if (!DECL_P(expr_tree)) |
3171 | expr_tree = save_expr(expr_tree); | |
55072f2b | 3172 | Array_type* a = expr_type->array_type(); |
e440a328 | 3173 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3174 | go_assert(e->integer_type() != NULL); |
e440a328 | 3175 | tree valptr = fold_convert(const_ptr_type_node, |
3176 | a->value_pointer_tree(gogo, expr_tree)); | |
3177 | tree len = a->length_tree(gogo, expr_tree); | |
b13c66cd | 3178 | len = fold_convert_loc(this->location().gcc_location(), integer_type_node, |
3179 | len); | |
60963afd | 3180 | if (e->integer_type()->is_byte()) |
e440a328 | 3181 | { |
3182 | static tree byte_array_to_string_fndecl; | |
3183 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3184 | this->location(), | |
3185 | "__go_byte_array_to_string", | |
3186 | 2, | |
3187 | type_tree, | |
3188 | const_ptr_type_node, | |
3189 | valptr, | |
9581e91d | 3190 | integer_type_node, |
e440a328 | 3191 | len); |
3192 | } | |
3193 | else | |
3194 | { | |
60963afd | 3195 | go_assert(e->integer_type()->is_rune()); |
e440a328 | 3196 | static tree int_array_to_string_fndecl; |
3197 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3198 | this->location(), | |
3199 | "__go_int_array_to_string", | |
3200 | 2, | |
3201 | type_tree, | |
3202 | const_ptr_type_node, | |
3203 | valptr, | |
9581e91d | 3204 | integer_type_node, |
e440a328 | 3205 | len); |
3206 | } | |
3207 | } | |
411eb89e | 3208 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3209 | { |
3210 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3211 | go_assert(e->integer_type() != NULL); |
60963afd | 3212 | if (e->integer_type()->is_byte()) |
e440a328 | 3213 | { |
ef43e66c | 3214 | tree string_to_byte_array_fndecl = NULL_TREE; |
e440a328 | 3215 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, |
3216 | this->location(), | |
3217 | "__go_string_to_byte_array", | |
3218 | 1, | |
3219 | type_tree, | |
3220 | TREE_TYPE(expr_tree), | |
3221 | expr_tree); | |
3222 | } | |
3223 | else | |
3224 | { | |
60963afd | 3225 | go_assert(e->integer_type()->is_rune()); |
ef43e66c | 3226 | tree string_to_int_array_fndecl = NULL_TREE; |
e440a328 | 3227 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, |
3228 | this->location(), | |
3229 | "__go_string_to_int_array", | |
3230 | 1, | |
3231 | type_tree, | |
3232 | TREE_TYPE(expr_tree), | |
3233 | expr_tree); | |
3234 | } | |
3235 | } | |
3236 | else if ((type->is_unsafe_pointer_type() | |
3237 | && expr_type->points_to() != NULL) | |
3238 | || (expr_type->is_unsafe_pointer_type() | |
3239 | && type->points_to() != NULL)) | |
3240 | ret = fold_convert(type_tree, expr_tree); | |
3241 | else if (type->is_unsafe_pointer_type() | |
3242 | && expr_type->integer_type() != NULL) | |
3243 | ret = convert_to_pointer(type_tree, expr_tree); | |
3244 | else if (this->may_convert_function_types_ | |
3245 | && type->function_type() != NULL | |
3246 | && expr_type->function_type() != NULL) | |
b13c66cd | 3247 | ret = fold_convert_loc(this->location().gcc_location(), type_tree, |
3248 | expr_tree); | |
e440a328 | 3249 | else |
3250 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3251 | expr_tree, this->location()); | |
3252 | ||
3253 | return ret; | |
3254 | } | |
3255 | ||
3256 | // Output a type conversion in a constant expression. | |
3257 | ||
3258 | void | |
3259 | Type_conversion_expression::do_export(Export* exp) const | |
3260 | { | |
3261 | exp->write_c_string("convert("); | |
3262 | exp->write_type(this->type_); | |
3263 | exp->write_c_string(", "); | |
3264 | this->expr_->export_expression(exp); | |
3265 | exp->write_c_string(")"); | |
3266 | } | |
3267 | ||
3268 | // Import a type conversion or a struct construction. | |
3269 | ||
3270 | Expression* | |
3271 | Type_conversion_expression::do_import(Import* imp) | |
3272 | { | |
3273 | imp->require_c_string("convert("); | |
3274 | Type* type = imp->read_type(); | |
3275 | imp->require_c_string(", "); | |
3276 | Expression* val = Expression::import_expression(imp); | |
3277 | imp->require_c_string(")"); | |
3278 | return Expression::make_cast(type, val, imp->location()); | |
3279 | } | |
3280 | ||
d751bb78 | 3281 | // Dump ast representation for a type conversion expression. |
3282 | ||
3283 | void | |
3284 | Type_conversion_expression::do_dump_expression( | |
3285 | Ast_dump_context* ast_dump_context) const | |
3286 | { | |
3287 | ast_dump_context->dump_type(this->type_); | |
3288 | ast_dump_context->ostream() << "("; | |
3289 | ast_dump_context->dump_expression(this->expr_); | |
3290 | ast_dump_context->ostream() << ") "; | |
3291 | } | |
3292 | ||
e440a328 | 3293 | // Make a type cast expression. |
3294 | ||
3295 | Expression* | |
b13c66cd | 3296 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3297 | { |
3298 | if (type->is_error_type() || val->is_error_expression()) | |
3299 | return Expression::make_error(location); | |
3300 | return new Type_conversion_expression(type, val, location); | |
3301 | } | |
3302 | ||
9581e91d | 3303 | // An unsafe type conversion, used to pass values to builtin functions. |
3304 | ||
3305 | class Unsafe_type_conversion_expression : public Expression | |
3306 | { | |
3307 | public: | |
3308 | Unsafe_type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 3309 | Location location) |
9581e91d | 3310 | : Expression(EXPRESSION_UNSAFE_CONVERSION, location), |
3311 | type_(type), expr_(expr) | |
3312 | { } | |
3313 | ||
3314 | protected: | |
3315 | int | |
3316 | do_traverse(Traverse* traverse); | |
3317 | ||
3318 | Type* | |
3319 | do_type() | |
3320 | { return this->type_; } | |
3321 | ||
3322 | void | |
3323 | do_determine_type(const Type_context*) | |
a9182619 | 3324 | { this->expr_->determine_type_no_context(); } |
9581e91d | 3325 | |
3326 | Expression* | |
3327 | do_copy() | |
3328 | { | |
3329 | return new Unsafe_type_conversion_expression(this->type_, | |
3330 | this->expr_->copy(), | |
3331 | this->location()); | |
3332 | } | |
3333 | ||
3334 | tree | |
3335 | do_get_tree(Translate_context*); | |
3336 | ||
d751bb78 | 3337 | void |
3338 | do_dump_expression(Ast_dump_context*) const; | |
3339 | ||
9581e91d | 3340 | private: |
3341 | // The type to convert to. | |
3342 | Type* type_; | |
3343 | // The expression to convert. | |
3344 | Expression* expr_; | |
3345 | }; | |
3346 | ||
3347 | // Traversal. | |
3348 | ||
3349 | int | |
3350 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3351 | { | |
3352 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3353 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3354 | return TRAVERSE_EXIT; | |
3355 | return TRAVERSE_CONTINUE; | |
3356 | } | |
3357 | ||
3358 | // Convert to backend representation. | |
3359 | ||
3360 | tree | |
3361 | Unsafe_type_conversion_expression::do_get_tree(Translate_context* context) | |
3362 | { | |
3363 | // We are only called for a limited number of cases. | |
3364 | ||
3365 | Type* t = this->type_; | |
3366 | Type* et = this->expr_->type(); | |
3367 | ||
9f0e0513 | 3368 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
9581e91d | 3369 | tree expr_tree = this->expr_->get_tree(context); |
3370 | if (type_tree == error_mark_node || expr_tree == error_mark_node) | |
3371 | return error_mark_node; | |
3372 | ||
b13c66cd | 3373 | Location loc = this->location(); |
9581e91d | 3374 | |
3375 | bool use_view_convert = false; | |
411eb89e | 3376 | if (t->is_slice_type()) |
9581e91d | 3377 | { |
411eb89e | 3378 | go_assert(et->is_slice_type()); |
9581e91d | 3379 | use_view_convert = true; |
3380 | } | |
3381 | else if (t->map_type() != NULL) | |
c484d925 | 3382 | go_assert(et->map_type() != NULL); |
9581e91d | 3383 | else if (t->channel_type() != NULL) |
c484d925 | 3384 | go_assert(et->channel_type() != NULL); |
09ea332d | 3385 | else if (t->points_to() != NULL) |
c484d925 | 3386 | go_assert(et->points_to() != NULL || et->is_nil_type()); |
9581e91d | 3387 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3388 | go_assert(t->points_to() != NULL); |
9581e91d | 3389 | else if (t->interface_type() != NULL && !t->interface_type()->is_empty()) |
3390 | { | |
c484d925 | 3391 | go_assert(et->interface_type() != NULL |
9581e91d | 3392 | && !et->interface_type()->is_empty()); |
3393 | use_view_convert = true; | |
3394 | } | |
3395 | else if (t->interface_type() != NULL && t->interface_type()->is_empty()) | |
3396 | { | |
c484d925 | 3397 | go_assert(et->interface_type() != NULL |
9581e91d | 3398 | && et->interface_type()->is_empty()); |
3399 | use_view_convert = true; | |
3400 | } | |
588e3cf9 | 3401 | else if (t->integer_type() != NULL) |
3402 | { | |
c484d925 | 3403 | go_assert(et->is_boolean_type() |
588e3cf9 | 3404 | || et->integer_type() != NULL |
3405 | || et->function_type() != NULL | |
3406 | || et->points_to() != NULL | |
3407 | || et->map_type() != NULL | |
3408 | || et->channel_type() != NULL); | |
3409 | return convert_to_integer(type_tree, expr_tree); | |
3410 | } | |
9581e91d | 3411 | else |
c3e6f413 | 3412 | go_unreachable(); |
9581e91d | 3413 | |
3414 | if (use_view_convert) | |
b13c66cd | 3415 | return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree, |
3416 | expr_tree); | |
9581e91d | 3417 | else |
b13c66cd | 3418 | return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree); |
9581e91d | 3419 | } |
3420 | ||
d751bb78 | 3421 | // Dump ast representation for an unsafe type conversion expression. |
3422 | ||
3423 | void | |
3424 | Unsafe_type_conversion_expression::do_dump_expression( | |
3425 | Ast_dump_context* ast_dump_context) const | |
3426 | { | |
3427 | ast_dump_context->dump_type(this->type_); | |
3428 | ast_dump_context->ostream() << "("; | |
3429 | ast_dump_context->dump_expression(this->expr_); | |
3430 | ast_dump_context->ostream() << ") "; | |
3431 | } | |
3432 | ||
9581e91d | 3433 | // Make an unsafe type conversion expression. |
3434 | ||
3435 | Expression* | |
3436 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3437 | Location location) |
9581e91d | 3438 | { |
3439 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3440 | } | |
3441 | ||
e440a328 | 3442 | // Unary expressions. |
3443 | ||
3444 | class Unary_expression : public Expression | |
3445 | { | |
3446 | public: | |
b13c66cd | 3447 | Unary_expression(Operator op, Expression* expr, Location location) |
e440a328 | 3448 | : Expression(EXPRESSION_UNARY, location), |
09ea332d | 3449 | op_(op), escapes_(true), create_temp_(false), expr_(expr) |
e440a328 | 3450 | { } |
3451 | ||
3452 | // Return the operator. | |
3453 | Operator | |
3454 | op() const | |
3455 | { return this->op_; } | |
3456 | ||
3457 | // Return the operand. | |
3458 | Expression* | |
3459 | operand() const | |
3460 | { return this->expr_; } | |
3461 | ||
3462 | // Record that an address expression does not escape. | |
3463 | void | |
3464 | set_does_not_escape() | |
3465 | { | |
c484d925 | 3466 | go_assert(this->op_ == OPERATOR_AND); |
e440a328 | 3467 | this->escapes_ = false; |
3468 | } | |
3469 | ||
09ea332d | 3470 | // Record that this is an address expression which should create a |
3471 | // temporary variable if necessary. This is used for method calls. | |
3472 | void | |
3473 | set_create_temp() | |
3474 | { | |
3475 | go_assert(this->op_ == OPERATOR_AND); | |
3476 | this->create_temp_ = true; | |
3477 | } | |
3478 | ||
0c77715b | 3479 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3480 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3481 | static bool |
0c77715b | 3482 | eval_constant(Operator op, const Numeric_constant* unc, |
3483 | Location, Numeric_constant* nc); | |
e440a328 | 3484 | |
3485 | static Expression* | |
3486 | do_import(Import*); | |
3487 | ||
3488 | protected: | |
3489 | int | |
3490 | do_traverse(Traverse* traverse) | |
3491 | { return Expression::traverse(&this->expr_, traverse); } | |
3492 | ||
3493 | Expression* | |
ceeb4318 | 3494 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 3495 | |
3496 | bool | |
3497 | do_is_constant() const; | |
3498 | ||
3499 | bool | |
0c77715b | 3500 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 3501 | |
3502 | Type* | |
3503 | do_type(); | |
3504 | ||
3505 | void | |
3506 | do_determine_type(const Type_context*); | |
3507 | ||
3508 | void | |
3509 | do_check_types(Gogo*); | |
3510 | ||
3511 | Expression* | |
3512 | do_copy() | |
3513 | { | |
3514 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3515 | this->location()); | |
3516 | } | |
3517 | ||
baef9f7a | 3518 | bool |
3519 | do_must_eval_subexpressions_in_order(int*) const | |
3520 | { return this->op_ == OPERATOR_MULT; } | |
3521 | ||
e440a328 | 3522 | bool |
3523 | do_is_addressable() const | |
3524 | { return this->op_ == OPERATOR_MULT; } | |
3525 | ||
3526 | tree | |
3527 | do_get_tree(Translate_context*); | |
3528 | ||
3529 | void | |
3530 | do_export(Export*) const; | |
3531 | ||
d751bb78 | 3532 | void |
3533 | do_dump_expression(Ast_dump_context*) const; | |
3534 | ||
e440a328 | 3535 | private: |
3536 | // The unary operator to apply. | |
3537 | Operator op_; | |
3538 | // Normally true. False if this is an address expression which does | |
3539 | // not escape the current function. | |
3540 | bool escapes_; | |
09ea332d | 3541 | // True if this is an address expression which should create a |
3542 | // temporary variable if necessary. | |
3543 | bool create_temp_; | |
e440a328 | 3544 | // The operand. |
3545 | Expression* expr_; | |
3546 | }; | |
3547 | ||
3548 | // If we are taking the address of a composite literal, and the | |
3549 | // contents are not constant, then we want to make a heap composite | |
3550 | // instead. | |
3551 | ||
3552 | Expression* | |
ceeb4318 | 3553 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3554 | { |
b13c66cd | 3555 | Location loc = this->location(); |
e440a328 | 3556 | Operator op = this->op_; |
3557 | Expression* expr = this->expr_; | |
3558 | ||
3559 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3560 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3561 | ||
3562 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3563 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3564 | // analysis here? This case is found in math/unsafe.go and is | |
3565 | // therefore worth special casing. | |
3566 | if (op == OPERATOR_MULT) | |
3567 | { | |
3568 | Expression* e = expr; | |
3569 | while (e->classification() == EXPRESSION_CONVERSION) | |
3570 | { | |
3571 | Type_conversion_expression* te | |
3572 | = static_cast<Type_conversion_expression*>(e); | |
3573 | e = te->expr(); | |
3574 | } | |
3575 | ||
3576 | if (e->classification() == EXPRESSION_UNARY) | |
3577 | { | |
3578 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3579 | if (ue->op_ == OPERATOR_AND) | |
3580 | { | |
3581 | if (e == expr) | |
3582 | { | |
3583 | // *&x == x. | |
3584 | return ue->expr_; | |
3585 | } | |
3586 | ue->set_does_not_escape(); | |
3587 | } | |
3588 | } | |
3589 | } | |
3590 | ||
55661ce9 | 3591 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3592 | // having to deal with TYPE_VOID in other places. | |
3593 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3594 | { | |
3595 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3596 | return Expression::make_error(this->location()); | |
3597 | } | |
3598 | ||
59a401fe | 3599 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3600 | { |
0c77715b | 3601 | Numeric_constant nc; |
3602 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3603 | { |
0c77715b | 3604 | Numeric_constant result; |
3605 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3606 | return result.expression(loc); | |
e440a328 | 3607 | } |
3608 | } | |
3609 | ||
3610 | return this; | |
3611 | } | |
3612 | ||
3613 | // Return whether a unary expression is a constant. | |
3614 | ||
3615 | bool | |
3616 | Unary_expression::do_is_constant() const | |
3617 | { | |
3618 | if (this->op_ == OPERATOR_MULT) | |
3619 | { | |
3620 | // Indirecting through a pointer is only constant if the object | |
3621 | // to which the expression points is constant, but we currently | |
3622 | // have no way to determine that. | |
3623 | return false; | |
3624 | } | |
3625 | else if (this->op_ == OPERATOR_AND) | |
3626 | { | |
3627 | // Taking the address of a variable is constant if it is a | |
3628 | // global variable, not constant otherwise. In other cases | |
3629 | // taking the address is probably not a constant. | |
3630 | Var_expression* ve = this->expr_->var_expression(); | |
3631 | if (ve != NULL) | |
3632 | { | |
3633 | Named_object* no = ve->named_object(); | |
3634 | return no->is_variable() && no->var_value()->is_global(); | |
3635 | } | |
3636 | return false; | |
3637 | } | |
3638 | else | |
3639 | return this->expr_->is_constant(); | |
3640 | } | |
3641 | ||
0c77715b | 3642 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3643 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3644 | |
3645 | bool | |
0c77715b | 3646 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3647 | Location location, Numeric_constant* nc) | |
e440a328 | 3648 | { |
3649 | switch (op) | |
3650 | { | |
3651 | case OPERATOR_PLUS: | |
0c77715b | 3652 | *nc = *unc; |
e440a328 | 3653 | return true; |
0c77715b | 3654 | |
e440a328 | 3655 | case OPERATOR_MINUS: |
0c77715b | 3656 | if (unc->is_int() || unc->is_rune()) |
3657 | break; | |
3658 | else if (unc->is_float()) | |
3659 | { | |
3660 | mpfr_t uval; | |
3661 | unc->get_float(&uval); | |
3662 | mpfr_t val; | |
3663 | mpfr_init(val); | |
3664 | mpfr_neg(val, uval, GMP_RNDN); | |
3665 | nc->set_float(unc->type(), val); | |
3666 | mpfr_clear(uval); | |
3667 | mpfr_clear(val); | |
3668 | return true; | |
3669 | } | |
3670 | else if (unc->is_complex()) | |
3671 | { | |
3672 | mpfr_t ureal, uimag; | |
3673 | unc->get_complex(&ureal, &uimag); | |
3674 | mpfr_t real, imag; | |
3675 | mpfr_init(real); | |
3676 | mpfr_init(imag); | |
3677 | mpfr_neg(real, ureal, GMP_RNDN); | |
3678 | mpfr_neg(imag, uimag, GMP_RNDN); | |
3679 | nc->set_complex(unc->type(), real, imag); | |
3680 | mpfr_clear(ureal); | |
3681 | mpfr_clear(uimag); | |
3682 | mpfr_clear(real); | |
3683 | mpfr_clear(imag); | |
3684 | return true; | |
3685 | } | |
e440a328 | 3686 | else |
0c77715b | 3687 | go_unreachable(); |
e440a328 | 3688 | |
0c77715b | 3689 | case OPERATOR_XOR: |
3690 | break; | |
68448d53 | 3691 | |
59a401fe | 3692 | case OPERATOR_NOT: |
e440a328 | 3693 | case OPERATOR_AND: |
3694 | case OPERATOR_MULT: | |
3695 | return false; | |
0c77715b | 3696 | |
e440a328 | 3697 | default: |
c3e6f413 | 3698 | go_unreachable(); |
e440a328 | 3699 | } |
e440a328 | 3700 | |
0c77715b | 3701 | if (!unc->is_int() && !unc->is_rune()) |
3702 | return false; | |
3703 | ||
3704 | mpz_t uval; | |
8387e1df | 3705 | if (unc->is_rune()) |
3706 | unc->get_rune(&uval); | |
3707 | else | |
3708 | unc->get_int(&uval); | |
0c77715b | 3709 | mpz_t val; |
3710 | mpz_init(val); | |
e440a328 | 3711 | |
e440a328 | 3712 | switch (op) |
3713 | { | |
e440a328 | 3714 | case OPERATOR_MINUS: |
0c77715b | 3715 | mpz_neg(val, uval); |
3716 | break; | |
3717 | ||
e440a328 | 3718 | case OPERATOR_NOT: |
0c77715b | 3719 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3720 | break; | |
3721 | ||
e440a328 | 3722 | case OPERATOR_XOR: |
0c77715b | 3723 | { |
3724 | Type* utype = unc->type(); | |
3725 | if (utype->integer_type() == NULL | |
3726 | || utype->integer_type()->is_abstract()) | |
3727 | mpz_com(val, uval); | |
3728 | else | |
3729 | { | |
3730 | // The number of HOST_WIDE_INTs that it takes to represent | |
3731 | // UVAL. | |
3732 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3733 | + HOST_BITS_PER_WIDE_INT | |
3734 | - 1) | |
3735 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3736 | |
0c77715b | 3737 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3738 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3739 | ||
3740 | size_t obits = utype->integer_type()->bits(); | |
3741 | ||
3742 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3743 | { | |
3744 | mpz_t adj; | |
3745 | mpz_init_set_ui(adj, 1); | |
3746 | mpz_mul_2exp(adj, adj, obits); | |
3747 | mpz_add(uval, uval, adj); | |
3748 | mpz_clear(adj); | |
3749 | } | |
3750 | ||
3751 | size_t ecount; | |
3752 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3753 | go_assert(ecount <= count); | |
3754 | ||
3755 | // Trim down to the number of words required by the type. | |
3756 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3757 | / HOST_BITS_PER_WIDE_INT); | |
3758 | go_assert(ocount <= count); | |
3759 | ||
3760 | for (size_t i = 0; i < ocount; ++i) | |
3761 | phwi[i] = ~phwi[i]; | |
3762 | ||
3763 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3764 | if (clearbits != 0) | |
3765 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3766 | >> clearbits); | |
3767 | ||
3768 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3769 | ||
3770 | if (!utype->integer_type()->is_unsigned() | |
3771 | && mpz_tstbit(val, obits - 1)) | |
3772 | { | |
3773 | mpz_t adj; | |
3774 | mpz_init_set_ui(adj, 1); | |
3775 | mpz_mul_2exp(adj, adj, obits); | |
3776 | mpz_sub(val, val, adj); | |
3777 | mpz_clear(adj); | |
3778 | } | |
3779 | ||
3780 | delete[] phwi; | |
3781 | } | |
3782 | } | |
3783 | break; | |
e440a328 | 3784 | |
e440a328 | 3785 | default: |
c3e6f413 | 3786 | go_unreachable(); |
e440a328 | 3787 | } |
e440a328 | 3788 | |
0c77715b | 3789 | if (unc->is_rune()) |
3790 | nc->set_rune(NULL, val); | |
e440a328 | 3791 | else |
0c77715b | 3792 | nc->set_int(NULL, val); |
e440a328 | 3793 | |
0c77715b | 3794 | mpz_clear(uval); |
3795 | mpz_clear(val); | |
e440a328 | 3796 | |
0c77715b | 3797 | return nc->set_type(unc->type(), true, location); |
e440a328 | 3798 | } |
3799 | ||
0c77715b | 3800 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 3801 | |
3802 | bool | |
0c77715b | 3803 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 3804 | { |
0c77715b | 3805 | Numeric_constant unc; |
3806 | if (!this->expr_->numeric_constant_value(&unc)) | |
3807 | return false; | |
3808 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
3809 | nc); | |
e440a328 | 3810 | } |
3811 | ||
3812 | // Return the type of a unary expression. | |
3813 | ||
3814 | Type* | |
3815 | Unary_expression::do_type() | |
3816 | { | |
3817 | switch (this->op_) | |
3818 | { | |
3819 | case OPERATOR_PLUS: | |
3820 | case OPERATOR_MINUS: | |
3821 | case OPERATOR_NOT: | |
3822 | case OPERATOR_XOR: | |
3823 | return this->expr_->type(); | |
3824 | ||
3825 | case OPERATOR_AND: | |
3826 | return Type::make_pointer_type(this->expr_->type()); | |
3827 | ||
3828 | case OPERATOR_MULT: | |
3829 | { | |
3830 | Type* subtype = this->expr_->type(); | |
3831 | Type* points_to = subtype->points_to(); | |
3832 | if (points_to == NULL) | |
3833 | return Type::make_error_type(); | |
3834 | return points_to; | |
3835 | } | |
3836 | ||
3837 | default: | |
c3e6f413 | 3838 | go_unreachable(); |
e440a328 | 3839 | } |
3840 | } | |
3841 | ||
3842 | // Determine abstract types for a unary expression. | |
3843 | ||
3844 | void | |
3845 | Unary_expression::do_determine_type(const Type_context* context) | |
3846 | { | |
3847 | switch (this->op_) | |
3848 | { | |
3849 | case OPERATOR_PLUS: | |
3850 | case OPERATOR_MINUS: | |
3851 | case OPERATOR_NOT: | |
3852 | case OPERATOR_XOR: | |
3853 | this->expr_->determine_type(context); | |
3854 | break; | |
3855 | ||
3856 | case OPERATOR_AND: | |
3857 | // Taking the address of something. | |
3858 | { | |
3859 | Type* subtype = (context->type == NULL | |
3860 | ? NULL | |
3861 | : context->type->points_to()); | |
3862 | Type_context subcontext(subtype, false); | |
3863 | this->expr_->determine_type(&subcontext); | |
3864 | } | |
3865 | break; | |
3866 | ||
3867 | case OPERATOR_MULT: | |
3868 | // Indirecting through a pointer. | |
3869 | { | |
3870 | Type* subtype = (context->type == NULL | |
3871 | ? NULL | |
3872 | : Type::make_pointer_type(context->type)); | |
3873 | Type_context subcontext(subtype, false); | |
3874 | this->expr_->determine_type(&subcontext); | |
3875 | } | |
3876 | break; | |
3877 | ||
3878 | default: | |
c3e6f413 | 3879 | go_unreachable(); |
e440a328 | 3880 | } |
3881 | } | |
3882 | ||
3883 | // Check types for a unary expression. | |
3884 | ||
3885 | void | |
3886 | Unary_expression::do_check_types(Gogo*) | |
3887 | { | |
9fe897ef | 3888 | Type* type = this->expr_->type(); |
5c13bd80 | 3889 | if (type->is_error()) |
9fe897ef | 3890 | { |
3891 | this->set_is_error(); | |
3892 | return; | |
3893 | } | |
3894 | ||
e440a328 | 3895 | switch (this->op_) |
3896 | { | |
3897 | case OPERATOR_PLUS: | |
3898 | case OPERATOR_MINUS: | |
9fe897ef | 3899 | if (type->integer_type() == NULL |
3900 | && type->float_type() == NULL | |
3901 | && type->complex_type() == NULL) | |
3902 | this->report_error(_("expected numeric type")); | |
e440a328 | 3903 | break; |
3904 | ||
3905 | case OPERATOR_NOT: | |
59a401fe | 3906 | if (!type->is_boolean_type()) |
3907 | this->report_error(_("expected boolean type")); | |
3908 | break; | |
3909 | ||
e440a328 | 3910 | case OPERATOR_XOR: |
9fe897ef | 3911 | if (type->integer_type() == NULL |
3912 | && !type->is_boolean_type()) | |
3913 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 3914 | break; |
3915 | ||
3916 | case OPERATOR_AND: | |
3917 | if (!this->expr_->is_addressable()) | |
09ea332d | 3918 | { |
3919 | if (!this->create_temp_) | |
3920 | this->report_error(_("invalid operand for unary %<&%>")); | |
3921 | } | |
e440a328 | 3922 | else |
3923 | this->expr_->address_taken(this->escapes_); | |
3924 | break; | |
3925 | ||
3926 | case OPERATOR_MULT: | |
3927 | // Indirecting through a pointer. | |
9fe897ef | 3928 | if (type->points_to() == NULL) |
3929 | this->report_error(_("expected pointer")); | |
e440a328 | 3930 | break; |
3931 | ||
3932 | default: | |
c3e6f413 | 3933 | go_unreachable(); |
e440a328 | 3934 | } |
3935 | } | |
3936 | ||
3937 | // Get a tree for a unary expression. | |
3938 | ||
3939 | tree | |
3940 | Unary_expression::do_get_tree(Translate_context* context) | |
3941 | { | |
e9d3367e | 3942 | Location loc = this->location(); |
3943 | ||
3944 | // Taking the address of a set-and-use-temporary expression requires | |
3945 | // setting the temporary and then taking the address. | |
3946 | if (this->op_ == OPERATOR_AND) | |
3947 | { | |
3948 | Set_and_use_temporary_expression* sut = | |
3949 | this->expr_->set_and_use_temporary_expression(); | |
3950 | if (sut != NULL) | |
3951 | { | |
3952 | Temporary_statement* temp = sut->temporary(); | |
3953 | Bvariable* bvar = temp->get_backend_variable(context); | |
3954 | tree var_tree = var_to_tree(bvar); | |
3955 | Expression* val = sut->expression(); | |
3956 | tree val_tree = val->get_tree(context); | |
3957 | if (var_tree == error_mark_node || val_tree == error_mark_node) | |
3958 | return error_mark_node; | |
3959 | tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(), | |
3960 | var_tree); | |
3961 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
3962 | TREE_TYPE(addr_tree), | |
3963 | build2_loc(sut->location().gcc_location(), | |
3964 | MODIFY_EXPR, void_type_node, | |
3965 | var_tree, val_tree), | |
3966 | addr_tree); | |
3967 | } | |
3968 | } | |
3969 | ||
e440a328 | 3970 | tree expr = this->expr_->get_tree(context); |
3971 | if (expr == error_mark_node) | |
3972 | return error_mark_node; | |
3973 | ||
e440a328 | 3974 | switch (this->op_) |
3975 | { | |
3976 | case OPERATOR_PLUS: | |
3977 | return expr; | |
3978 | ||
3979 | case OPERATOR_MINUS: | |
3980 | { | |
3981 | tree type = TREE_TYPE(expr); | |
3982 | tree compute_type = excess_precision_type(type); | |
3983 | if (compute_type != NULL_TREE) | |
3984 | expr = ::convert(compute_type, expr); | |
b13c66cd | 3985 | tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR, |
e440a328 | 3986 | (compute_type != NULL_TREE |
3987 | ? compute_type | |
3988 | : type), | |
3989 | expr); | |
3990 | if (compute_type != NULL_TREE) | |
3991 | ret = ::convert(type, ret); | |
3992 | return ret; | |
3993 | } | |
3994 | ||
3995 | case OPERATOR_NOT: | |
3996 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
b13c66cd | 3997 | return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR, |
3998 | TREE_TYPE(expr), expr); | |
e440a328 | 3999 | else |
b13c66cd | 4000 | return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node, |
4001 | expr, build_int_cst(TREE_TYPE(expr), 0)); | |
e440a328 | 4002 | |
4003 | case OPERATOR_XOR: | |
b13c66cd | 4004 | return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr), |
4005 | expr); | |
e440a328 | 4006 | |
4007 | case OPERATOR_AND: | |
09ea332d | 4008 | if (!this->create_temp_) |
4009 | { | |
4010 | // We should not see a non-constant constructor here; cases | |
4011 | // where we would see one should have been moved onto the | |
4012 | // heap at parse time. Taking the address of a nonconstant | |
4013 | // constructor will not do what the programmer expects. | |
4014 | go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
4015 | go_assert(TREE_CODE(expr) != ADDR_EXPR); | |
4016 | } | |
e440a328 | 4017 | |
4018 | // Build a decl for a constant constructor. | |
4019 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
4020 | { | |
b13c66cd | 4021 | tree decl = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 4022 | create_tmp_var_name("C"), TREE_TYPE(expr)); |
4023 | DECL_EXTERNAL(decl) = 0; | |
4024 | TREE_PUBLIC(decl) = 0; | |
4025 | TREE_READONLY(decl) = 1; | |
4026 | TREE_CONSTANT(decl) = 1; | |
4027 | TREE_STATIC(decl) = 1; | |
4028 | TREE_ADDRESSABLE(decl) = 1; | |
4029 | DECL_ARTIFICIAL(decl) = 1; | |
4030 | DECL_INITIAL(decl) = expr; | |
4031 | rest_of_decl_compilation(decl, 1, 0); | |
4032 | expr = decl; | |
4033 | } | |
4034 | ||
09ea332d | 4035 | if (this->create_temp_ |
4036 | && !TREE_ADDRESSABLE(TREE_TYPE(expr)) | |
dd28fd36 | 4037 | && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr)) |
09ea332d | 4038 | && TREE_CODE(expr) != INDIRECT_REF |
4039 | && TREE_CODE(expr) != COMPONENT_REF) | |
4040 | { | |
fc81003d | 4041 | if (current_function_decl != NULL) |
4042 | { | |
4043 | tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr)); | |
4044 | DECL_IGNORED_P(tmp) = 1; | |
4045 | DECL_INITIAL(tmp) = expr; | |
4046 | TREE_ADDRESSABLE(tmp) = 1; | |
4047 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4048 | build_pointer_type(TREE_TYPE(expr)), | |
4049 | build1_loc(loc.gcc_location(), DECL_EXPR, | |
4050 | void_type_node, tmp), | |
4051 | build_fold_addr_expr_loc(loc.gcc_location(), | |
4052 | tmp)); | |
4053 | } | |
4054 | else | |
4055 | { | |
4056 | tree tmp = build_decl(loc.gcc_location(), VAR_DECL, | |
4057 | create_tmp_var_name("A"), TREE_TYPE(expr)); | |
4058 | DECL_EXTERNAL(tmp) = 0; | |
4059 | TREE_PUBLIC(tmp) = 0; | |
4060 | TREE_STATIC(tmp) = 1; | |
4061 | DECL_ARTIFICIAL(tmp) = 1; | |
4062 | TREE_ADDRESSABLE(tmp) = 1; | |
4063 | tree make_tmp; | |
4064 | if (!TREE_CONSTANT(expr)) | |
4065 | make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR, | |
4066 | void_type_node, tmp, expr); | |
4067 | else | |
4068 | { | |
4069 | TREE_READONLY(tmp) = 1; | |
4070 | TREE_CONSTANT(tmp) = 1; | |
4071 | DECL_INITIAL(tmp) = expr; | |
4072 | make_tmp = NULL_TREE; | |
4073 | } | |
4074 | rest_of_decl_compilation(tmp, 1, 0); | |
4075 | tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp); | |
4076 | if (make_tmp == NULL_TREE) | |
4077 | return addr; | |
4078 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4079 | TREE_TYPE(addr), make_tmp, addr); | |
4080 | } | |
09ea332d | 4081 | } |
4082 | ||
b13c66cd | 4083 | return build_fold_addr_expr_loc(loc.gcc_location(), expr); |
e440a328 | 4084 | |
4085 | case OPERATOR_MULT: | |
4086 | { | |
c484d925 | 4087 | go_assert(POINTER_TYPE_P(TREE_TYPE(expr))); |
e440a328 | 4088 | |
4089 | // If we are dereferencing the pointer to a large struct, we | |
4090 | // need to check for nil. We don't bother to check for small | |
4091 | // structs because we expect the system to crash on a nil | |
4092 | // pointer dereference. | |
19b4f09b | 4093 | tree target_type_tree = TREE_TYPE(TREE_TYPE(expr)); |
4094 | if (!VOID_TYPE_P(target_type_tree)) | |
e440a328 | 4095 | { |
19b4f09b | 4096 | HOST_WIDE_INT s = int_size_in_bytes(target_type_tree); |
4097 | if (s == -1 || s >= 4096) | |
4098 | { | |
4099 | if (!DECL_P(expr)) | |
4100 | expr = save_expr(expr); | |
4101 | tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR, | |
4102 | boolean_type_node, | |
4103 | expr, | |
4104 | fold_convert(TREE_TYPE(expr), | |
4105 | null_pointer_node)); | |
4106 | tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
4107 | loc); | |
4108 | expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4109 | TREE_TYPE(expr), build3(COND_EXPR, | |
4110 | void_type_node, | |
4111 | compare, crash, | |
4112 | NULL_TREE), | |
4113 | expr); | |
4114 | } | |
e440a328 | 4115 | } |
4116 | ||
4117 | // If the type of EXPR is a recursive pointer type, then we | |
4118 | // need to insert a cast before indirecting. | |
19b4f09b | 4119 | if (VOID_TYPE_P(target_type_tree)) |
e440a328 | 4120 | { |
4121 | Type* pt = this->expr_->type()->points_to(); | |
9f0e0513 | 4122 | tree ind = type_to_tree(pt->get_backend(context->gogo())); |
b13c66cd | 4123 | expr = fold_convert_loc(loc.gcc_location(), |
4124 | build_pointer_type(ind), expr); | |
e440a328 | 4125 | } |
4126 | ||
b13c66cd | 4127 | return build_fold_indirect_ref_loc(loc.gcc_location(), expr); |
e440a328 | 4128 | } |
4129 | ||
4130 | default: | |
c3e6f413 | 4131 | go_unreachable(); |
e440a328 | 4132 | } |
4133 | } | |
4134 | ||
4135 | // Export a unary expression. | |
4136 | ||
4137 | void | |
4138 | Unary_expression::do_export(Export* exp) const | |
4139 | { | |
4140 | switch (this->op_) | |
4141 | { | |
4142 | case OPERATOR_PLUS: | |
4143 | exp->write_c_string("+ "); | |
4144 | break; | |
4145 | case OPERATOR_MINUS: | |
4146 | exp->write_c_string("- "); | |
4147 | break; | |
4148 | case OPERATOR_NOT: | |
4149 | exp->write_c_string("! "); | |
4150 | break; | |
4151 | case OPERATOR_XOR: | |
4152 | exp->write_c_string("^ "); | |
4153 | break; | |
4154 | case OPERATOR_AND: | |
4155 | case OPERATOR_MULT: | |
4156 | default: | |
c3e6f413 | 4157 | go_unreachable(); |
e440a328 | 4158 | } |
4159 | this->expr_->export_expression(exp); | |
4160 | } | |
4161 | ||
4162 | // Import a unary expression. | |
4163 | ||
4164 | Expression* | |
4165 | Unary_expression::do_import(Import* imp) | |
4166 | { | |
4167 | Operator op; | |
4168 | switch (imp->get_char()) | |
4169 | { | |
4170 | case '+': | |
4171 | op = OPERATOR_PLUS; | |
4172 | break; | |
4173 | case '-': | |
4174 | op = OPERATOR_MINUS; | |
4175 | break; | |
4176 | case '!': | |
4177 | op = OPERATOR_NOT; | |
4178 | break; | |
4179 | case '^': | |
4180 | op = OPERATOR_XOR; | |
4181 | break; | |
4182 | default: | |
c3e6f413 | 4183 | go_unreachable(); |
e440a328 | 4184 | } |
4185 | imp->require_c_string(" "); | |
4186 | Expression* expr = Expression::import_expression(imp); | |
4187 | return Expression::make_unary(op, expr, imp->location()); | |
4188 | } | |
4189 | ||
d751bb78 | 4190 | // Dump ast representation of an unary expression. |
4191 | ||
4192 | void | |
4193 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4194 | { | |
4195 | ast_dump_context->dump_operator(this->op_); | |
4196 | ast_dump_context->ostream() << "("; | |
4197 | ast_dump_context->dump_expression(this->expr_); | |
4198 | ast_dump_context->ostream() << ") "; | |
4199 | } | |
4200 | ||
e440a328 | 4201 | // Make a unary expression. |
4202 | ||
4203 | Expression* | |
b13c66cd | 4204 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4205 | { |
4206 | return new Unary_expression(op, expr, location); | |
4207 | } | |
4208 | ||
4209 | // If this is an indirection through a pointer, return the expression | |
4210 | // being pointed through. Otherwise return this. | |
4211 | ||
4212 | Expression* | |
4213 | Expression::deref() | |
4214 | { | |
4215 | if (this->classification_ == EXPRESSION_UNARY) | |
4216 | { | |
4217 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4218 | if (ue->op() == OPERATOR_MULT) | |
4219 | return ue->operand(); | |
4220 | } | |
4221 | return this; | |
4222 | } | |
4223 | ||
4224 | // Class Binary_expression. | |
4225 | ||
4226 | // Traversal. | |
4227 | ||
4228 | int | |
4229 | Binary_expression::do_traverse(Traverse* traverse) | |
4230 | { | |
4231 | int t = Expression::traverse(&this->left_, traverse); | |
4232 | if (t == TRAVERSE_EXIT) | |
4233 | return TRAVERSE_EXIT; | |
4234 | return Expression::traverse(&this->right_, traverse); | |
4235 | } | |
4236 | ||
0c77715b | 4237 | // Return the type to use for a binary operation on operands of |
4238 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4239 | // such may be NULL or abstract. | |
4240 | ||
4241 | bool | |
4242 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4243 | Type* right_type, Type** result_type) | |
4244 | { | |
4245 | if (left_type != right_type | |
4246 | && !left_type->is_abstract() | |
4247 | && !right_type->is_abstract() | |
4248 | && left_type->base() != right_type->base() | |
4249 | && op != OPERATOR_LSHIFT | |
4250 | && op != OPERATOR_RSHIFT) | |
4251 | { | |
4252 | // May be a type error--let it be diagnosed elsewhere. | |
4253 | return false; | |
4254 | } | |
4255 | ||
4256 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4257 | { | |
4258 | if (left_type->integer_type() != NULL) | |
4259 | *result_type = left_type; | |
4260 | else | |
4261 | *result_type = Type::make_abstract_integer_type(); | |
4262 | } | |
4263 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4264 | *result_type = left_type; | |
4265 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4266 | *result_type = right_type; | |
4267 | else if (!left_type->is_abstract()) | |
4268 | *result_type = left_type; | |
4269 | else if (!right_type->is_abstract()) | |
4270 | *result_type = right_type; | |
4271 | else if (left_type->complex_type() != NULL) | |
4272 | *result_type = left_type; | |
4273 | else if (right_type->complex_type() != NULL) | |
4274 | *result_type = right_type; | |
4275 | else if (left_type->float_type() != NULL) | |
4276 | *result_type = left_type; | |
4277 | else if (right_type->float_type() != NULL) | |
4278 | *result_type = right_type; | |
4279 | else if (left_type->integer_type() != NULL | |
4280 | && left_type->integer_type()->is_rune()) | |
4281 | *result_type = left_type; | |
4282 | else if (right_type->integer_type() != NULL | |
4283 | && right_type->integer_type()->is_rune()) | |
4284 | *result_type = right_type; | |
4285 | else | |
4286 | *result_type = left_type; | |
4287 | ||
4288 | return true; | |
4289 | } | |
4290 | ||
4291 | // Convert an integer comparison code and an operator to a boolean | |
4292 | // value. | |
e440a328 | 4293 | |
4294 | bool | |
0c77715b | 4295 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4296 | { |
e440a328 | 4297 | switch (op) |
4298 | { | |
4299 | case OPERATOR_EQEQ: | |
0c77715b | 4300 | return cmp == 0; |
4301 | break; | |
e440a328 | 4302 | case OPERATOR_NOTEQ: |
0c77715b | 4303 | return cmp != 0; |
4304 | break; | |
e440a328 | 4305 | case OPERATOR_LT: |
0c77715b | 4306 | return cmp < 0; |
4307 | break; | |
e440a328 | 4308 | case OPERATOR_LE: |
0c77715b | 4309 | return cmp <= 0; |
e440a328 | 4310 | case OPERATOR_GT: |
0c77715b | 4311 | return cmp > 0; |
e440a328 | 4312 | case OPERATOR_GE: |
0c77715b | 4313 | return cmp >= 0; |
e440a328 | 4314 | default: |
c3e6f413 | 4315 | go_unreachable(); |
e440a328 | 4316 | } |
4317 | } | |
4318 | ||
0c77715b | 4319 | // Compare constants according to OP. |
e440a328 | 4320 | |
4321 | bool | |
0c77715b | 4322 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4323 | Numeric_constant* right_nc, | |
4324 | Location location, bool* result) | |
e440a328 | 4325 | { |
0c77715b | 4326 | Type* left_type = left_nc->type(); |
4327 | Type* right_type = right_nc->type(); | |
4328 | ||
4329 | Type* type; | |
4330 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4331 | return false; | |
4332 | ||
4333 | // When comparing an untyped operand to a typed operand, we are | |
4334 | // effectively coercing the untyped operand to the other operand's | |
4335 | // type, so make sure that is valid. | |
4336 | if (!left_nc->set_type(type, true, location) | |
4337 | || !right_nc->set_type(type, true, location)) | |
4338 | return false; | |
4339 | ||
4340 | bool ret; | |
4341 | int cmp; | |
4342 | if (type->complex_type() != NULL) | |
4343 | { | |
4344 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4345 | return false; | |
4346 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4347 | } | |
4348 | else if (type->float_type() != NULL) | |
4349 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4350 | else |
0c77715b | 4351 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4352 | ||
4353 | if (ret) | |
4354 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4355 | ||
4356 | return ret; | |
4357 | } | |
4358 | ||
4359 | // Compare integer constants. | |
4360 | ||
4361 | bool | |
4362 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4363 | const Numeric_constant* right_nc, | |
4364 | int* cmp) | |
4365 | { | |
4366 | mpz_t left_val; | |
4367 | if (!left_nc->to_int(&left_val)) | |
4368 | return false; | |
4369 | mpz_t right_val; | |
4370 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4371 | { |
0c77715b | 4372 | mpz_clear(left_val); |
4373 | return false; | |
e440a328 | 4374 | } |
0c77715b | 4375 | |
4376 | *cmp = mpz_cmp(left_val, right_val); | |
4377 | ||
4378 | mpz_clear(left_val); | |
4379 | mpz_clear(right_val); | |
4380 | ||
4381 | return true; | |
4382 | } | |
4383 | ||
4384 | // Compare floating point constants. | |
4385 | ||
4386 | bool | |
4387 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4388 | const Numeric_constant* right_nc, | |
4389 | int* cmp) | |
4390 | { | |
4391 | mpfr_t left_val; | |
4392 | if (!left_nc->to_float(&left_val)) | |
4393 | return false; | |
4394 | mpfr_t right_val; | |
4395 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4396 | { |
0c77715b | 4397 | mpfr_clear(left_val); |
4398 | return false; | |
4399 | } | |
4400 | ||
4401 | // We already coerced both operands to the same type. If that type | |
4402 | // is not an abstract type, we need to round the values accordingly. | |
4403 | Type* type = left_nc->type(); | |
4404 | if (!type->is_abstract() && type->float_type() != NULL) | |
4405 | { | |
4406 | int bits = type->float_type()->bits(); | |
4407 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4408 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4409 | } |
0c77715b | 4410 | |
4411 | *cmp = mpfr_cmp(left_val, right_val); | |
4412 | ||
4413 | mpfr_clear(left_val); | |
4414 | mpfr_clear(right_val); | |
4415 | ||
4416 | return true; | |
e440a328 | 4417 | } |
4418 | ||
0c77715b | 4419 | // Compare complex constants. Complex numbers may only be compared |
4420 | // for equality. | |
e440a328 | 4421 | |
4422 | bool | |
0c77715b | 4423 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4424 | const Numeric_constant* right_nc, | |
4425 | int* cmp) | |
e440a328 | 4426 | { |
0c77715b | 4427 | mpfr_t left_real, left_imag; |
4428 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4429 | return false; | |
4430 | mpfr_t right_real, right_imag; | |
4431 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4432 | { |
0c77715b | 4433 | mpfr_clear(left_real); |
4434 | mpfr_clear(left_imag); | |
4435 | return false; | |
e440a328 | 4436 | } |
0c77715b | 4437 | |
4438 | // We already coerced both operands to the same type. If that type | |
4439 | // is not an abstract type, we need to round the values accordingly. | |
4440 | Type* type = left_nc->type(); | |
4441 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4442 | { |
0c77715b | 4443 | int bits = type->complex_type()->bits(); |
4444 | mpfr_prec_round(left_real, bits / 2, GMP_RNDN); | |
4445 | mpfr_prec_round(left_imag, bits / 2, GMP_RNDN); | |
4446 | mpfr_prec_round(right_real, bits / 2, GMP_RNDN); | |
4447 | mpfr_prec_round(right_imag, bits / 2, GMP_RNDN); | |
e440a328 | 4448 | } |
0c77715b | 4449 | |
4450 | *cmp = (mpfr_cmp(left_real, right_real) != 0 | |
4451 | || mpfr_cmp(left_imag, right_imag) != 0); | |
4452 | ||
4453 | mpfr_clear(left_real); | |
4454 | mpfr_clear(left_imag); | |
4455 | mpfr_clear(right_real); | |
4456 | mpfr_clear(right_imag); | |
4457 | ||
4458 | return true; | |
e440a328 | 4459 | } |
4460 | ||
0c77715b | 4461 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4462 | // true if this could be done, false if not. Issue errors at LOCATION | |
4463 | // as appropriate. | |
e440a328 | 4464 | |
4465 | bool | |
0c77715b | 4466 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4467 | Numeric_constant* right_nc, | |
4468 | Location location, Numeric_constant* nc) | |
e440a328 | 4469 | { |
e440a328 | 4470 | switch (op) |
4471 | { | |
4472 | case OPERATOR_OROR: | |
4473 | case OPERATOR_ANDAND: | |
4474 | case OPERATOR_EQEQ: | |
4475 | case OPERATOR_NOTEQ: | |
4476 | case OPERATOR_LT: | |
4477 | case OPERATOR_LE: | |
4478 | case OPERATOR_GT: | |
4479 | case OPERATOR_GE: | |
9767e2d3 | 4480 | // These return boolean values, not numeric. |
4481 | return false; | |
0c77715b | 4482 | default: |
4483 | break; | |
4484 | } | |
4485 | ||
4486 | Type* left_type = left_nc->type(); | |
4487 | Type* right_type = right_nc->type(); | |
4488 | ||
4489 | Type* type; | |
4490 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4491 | return false; | |
4492 | ||
4493 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4494 | ||
4495 | // When combining an untyped operand with a typed operand, we are | |
4496 | // effectively coercing the untyped operand to the other operand's | |
4497 | // type, so make sure that is valid. | |
4498 | if (!left_nc->set_type(type, true, location)) | |
4499 | return false; | |
4500 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4501 | return false; | |
4502 | ||
4503 | bool r; | |
4504 | if (type->complex_type() != NULL) | |
4505 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4506 | else if (type->float_type() != NULL) | |
4507 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4508 | else | |
4509 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4510 | ||
4511 | if (r) | |
4512 | r = nc->set_type(type, true, location); | |
4513 | ||
4514 | return r; | |
4515 | } | |
4516 | ||
4517 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4518 | // integer operations. Return true if this could be done, false if | |
4519 | // not. | |
4520 | ||
4521 | bool | |
4522 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4523 | const Numeric_constant* right_nc, | |
4524 | Location location, Numeric_constant* nc) | |
4525 | { | |
4526 | mpz_t left_val; | |
4527 | if (!left_nc->to_int(&left_val)) | |
4528 | return false; | |
4529 | mpz_t right_val; | |
4530 | if (!right_nc->to_int(&right_val)) | |
4531 | { | |
4532 | mpz_clear(left_val); | |
e440a328 | 4533 | return false; |
0c77715b | 4534 | } |
4535 | ||
4536 | mpz_t val; | |
4537 | mpz_init(val); | |
4538 | ||
4539 | switch (op) | |
4540 | { | |
e440a328 | 4541 | case OPERATOR_PLUS: |
4542 | mpz_add(val, left_val, right_val); | |
4543 | break; | |
4544 | case OPERATOR_MINUS: | |
4545 | mpz_sub(val, left_val, right_val); | |
4546 | break; | |
4547 | case OPERATOR_OR: | |
4548 | mpz_ior(val, left_val, right_val); | |
4549 | break; | |
4550 | case OPERATOR_XOR: | |
4551 | mpz_xor(val, left_val, right_val); | |
4552 | break; | |
4553 | case OPERATOR_MULT: | |
4554 | mpz_mul(val, left_val, right_val); | |
4555 | break; | |
4556 | case OPERATOR_DIV: | |
4557 | if (mpz_sgn(right_val) != 0) | |
4558 | mpz_tdiv_q(val, left_val, right_val); | |
4559 | else | |
4560 | { | |
4561 | error_at(location, "division by zero"); | |
4562 | mpz_set_ui(val, 0); | |
e440a328 | 4563 | } |
4564 | break; | |
4565 | case OPERATOR_MOD: | |
4566 | if (mpz_sgn(right_val) != 0) | |
4567 | mpz_tdiv_r(val, left_val, right_val); | |
4568 | else | |
4569 | { | |
4570 | error_at(location, "division by zero"); | |
4571 | mpz_set_ui(val, 0); | |
e440a328 | 4572 | } |
4573 | break; | |
4574 | case OPERATOR_LSHIFT: | |
4575 | { | |
4576 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4577 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4578 | mpz_mul_2exp(val, left_val, shift); | |
4579 | else | |
e440a328 | 4580 | { |
4581 | error_at(location, "shift count overflow"); | |
4582 | mpz_set_ui(val, 0); | |
e440a328 | 4583 | } |
e440a328 | 4584 | break; |
4585 | } | |
4586 | break; | |
4587 | case OPERATOR_RSHIFT: | |
4588 | { | |
4589 | unsigned long shift = mpz_get_ui(right_val); | |
4590 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4591 | { | |
4592 | error_at(location, "shift count overflow"); | |
4593 | mpz_set_ui(val, 0); | |
e440a328 | 4594 | } |
e440a328 | 4595 | else |
0c77715b | 4596 | { |
4597 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4598 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4599 | else | |
4600 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4601 | } | |
e440a328 | 4602 | break; |
4603 | } | |
4604 | break; | |
4605 | case OPERATOR_AND: | |
4606 | mpz_and(val, left_val, right_val); | |
4607 | break; | |
4608 | case OPERATOR_BITCLEAR: | |
4609 | { | |
4610 | mpz_t tval; | |
4611 | mpz_init(tval); | |
4612 | mpz_com(tval, right_val); | |
4613 | mpz_and(val, left_val, tval); | |
4614 | mpz_clear(tval); | |
4615 | } | |
4616 | break; | |
4617 | default: | |
c3e6f413 | 4618 | go_unreachable(); |
e440a328 | 4619 | } |
4620 | ||
0c77715b | 4621 | mpz_clear(left_val); |
4622 | mpz_clear(right_val); | |
e440a328 | 4623 | |
0c77715b | 4624 | if (left_nc->is_rune() |
4625 | || (op != OPERATOR_LSHIFT | |
4626 | && op != OPERATOR_RSHIFT | |
4627 | && right_nc->is_rune())) | |
4628 | nc->set_rune(NULL, val); | |
4629 | else | |
4630 | nc->set_int(NULL, val); | |
4631 | ||
4632 | mpz_clear(val); | |
e440a328 | 4633 | |
4634 | return true; | |
4635 | } | |
4636 | ||
0c77715b | 4637 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4638 | // floating point operations. Return true if this could be done, | |
4639 | // false if not. | |
e440a328 | 4640 | |
4641 | bool | |
0c77715b | 4642 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4643 | const Numeric_constant* right_nc, | |
4644 | Location location, Numeric_constant* nc) | |
e440a328 | 4645 | { |
0c77715b | 4646 | mpfr_t left_val; |
4647 | if (!left_nc->to_float(&left_val)) | |
4648 | return false; | |
4649 | mpfr_t right_val; | |
4650 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4651 | { |
0c77715b | 4652 | mpfr_clear(left_val); |
e440a328 | 4653 | return false; |
0c77715b | 4654 | } |
4655 | ||
4656 | mpfr_t val; | |
4657 | mpfr_init(val); | |
4658 | ||
4659 | bool ret = true; | |
4660 | switch (op) | |
4661 | { | |
e440a328 | 4662 | case OPERATOR_PLUS: |
4663 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4664 | break; | |
4665 | case OPERATOR_MINUS: | |
4666 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4667 | break; | |
4668 | case OPERATOR_OR: | |
4669 | case OPERATOR_XOR: | |
4670 | case OPERATOR_AND: | |
4671 | case OPERATOR_BITCLEAR: | |
0c77715b | 4672 | case OPERATOR_MOD: |
4673 | case OPERATOR_LSHIFT: | |
4674 | case OPERATOR_RSHIFT: | |
4675 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4676 | ret = false; | |
4677 | break; | |
e440a328 | 4678 | case OPERATOR_MULT: |
4679 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4680 | break; | |
4681 | case OPERATOR_DIV: | |
0c77715b | 4682 | if (!mpfr_zero_p(right_val)) |
4683 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4684 | else | |
4685 | { | |
4686 | error_at(location, "division by zero"); | |
4687 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4688 | } | |
e440a328 | 4689 | break; |
e440a328 | 4690 | default: |
c3e6f413 | 4691 | go_unreachable(); |
e440a328 | 4692 | } |
4693 | ||
0c77715b | 4694 | mpfr_clear(left_val); |
4695 | mpfr_clear(right_val); | |
e440a328 | 4696 | |
0c77715b | 4697 | nc->set_float(NULL, val); |
4698 | mpfr_clear(val); | |
e440a328 | 4699 | |
0c77715b | 4700 | return ret; |
e440a328 | 4701 | } |
4702 | ||
0c77715b | 4703 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4704 | // complex operations. Return true if this could be done, false if | |
4705 | // not. | |
e440a328 | 4706 | |
4707 | bool | |
0c77715b | 4708 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4709 | const Numeric_constant* right_nc, | |
4710 | Location location, Numeric_constant* nc) | |
e440a328 | 4711 | { |
0c77715b | 4712 | mpfr_t left_real, left_imag; |
4713 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4714 | return false; | |
4715 | mpfr_t right_real, right_imag; | |
4716 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4717 | { |
0c77715b | 4718 | mpfr_clear(left_real); |
4719 | mpfr_clear(left_imag); | |
e440a328 | 4720 | return false; |
0c77715b | 4721 | } |
4722 | ||
4723 | mpfr_t real, imag; | |
4724 | mpfr_init(real); | |
4725 | mpfr_init(imag); | |
4726 | ||
4727 | bool ret = true; | |
4728 | switch (op) | |
4729 | { | |
e440a328 | 4730 | case OPERATOR_PLUS: |
4731 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4732 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4733 | break; | |
4734 | case OPERATOR_MINUS: | |
4735 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4736 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4737 | break; | |
4738 | case OPERATOR_OR: | |
4739 | case OPERATOR_XOR: | |
4740 | case OPERATOR_AND: | |
4741 | case OPERATOR_BITCLEAR: | |
0c77715b | 4742 | case OPERATOR_MOD: |
4743 | case OPERATOR_LSHIFT: | |
4744 | case OPERATOR_RSHIFT: | |
4745 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4746 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4747 | ret = false; | |
4748 | break; | |
e440a328 | 4749 | case OPERATOR_MULT: |
4750 | { | |
4751 | // You might think that multiplying two complex numbers would | |
4752 | // be simple, and you would be right, until you start to think | |
4753 | // about getting the right answer for infinity. If one | |
4754 | // operand here is infinity and the other is anything other | |
4755 | // than zero or NaN, then we are going to wind up subtracting | |
4756 | // two infinity values. That will give us a NaN, but the | |
4757 | // correct answer is infinity. | |
4758 | ||
4759 | mpfr_t lrrr; | |
4760 | mpfr_init(lrrr); | |
4761 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4762 | ||
4763 | mpfr_t lrri; | |
4764 | mpfr_init(lrri); | |
4765 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4766 | ||
4767 | mpfr_t lirr; | |
4768 | mpfr_init(lirr); | |
4769 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4770 | ||
4771 | mpfr_t liri; | |
4772 | mpfr_init(liri); | |
4773 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4774 | ||
4775 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4776 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4777 | ||
4778 | // If we get NaN on both sides, check whether it should really | |
4779 | // be infinity. The rule is that if either side of the | |
4780 | // complex number is infinity, then the whole value is | |
4781 | // infinity, even if the other side is NaN. So the only case | |
4782 | // we have to fix is the one in which both sides are NaN. | |
4783 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4784 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4785 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4786 | { | |
4787 | bool is_infinity = false; | |
4788 | ||
4789 | mpfr_t lr; | |
4790 | mpfr_t li; | |
4791 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4792 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4793 | ||
4794 | mpfr_t rr; | |
4795 | mpfr_t ri; | |
4796 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4797 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4798 | ||
4799 | // If the left side is infinity, then the result is | |
4800 | // infinity. | |
4801 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4802 | { | |
4803 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4804 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4805 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4806 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4807 | if (mpfr_nan_p(rr)) | |
4808 | { | |
4809 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4810 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4811 | } | |
4812 | if (mpfr_nan_p(ri)) | |
4813 | { | |
4814 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4815 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4816 | } | |
4817 | is_infinity = true; | |
4818 | } | |
4819 | ||
4820 | // If the right side is infinity, then the result is | |
4821 | // infinity. | |
4822 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4823 | { | |
4824 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4825 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4826 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4827 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4828 | if (mpfr_nan_p(lr)) | |
4829 | { | |
4830 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4831 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4832 | } | |
4833 | if (mpfr_nan_p(li)) | |
4834 | { | |
4835 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4836 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4837 | } | |
4838 | is_infinity = true; | |
4839 | } | |
4840 | ||
4841 | // If we got an overflow in the intermediate computations, | |
4842 | // then the result is infinity. | |
4843 | if (!is_infinity | |
4844 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4845 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4846 | { | |
4847 | if (mpfr_nan_p(lr)) | |
4848 | { | |
4849 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4850 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4851 | } | |
4852 | if (mpfr_nan_p(li)) | |
4853 | { | |
4854 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4855 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4856 | } | |
4857 | if (mpfr_nan_p(rr)) | |
4858 | { | |
4859 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4860 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4861 | } | |
4862 | if (mpfr_nan_p(ri)) | |
4863 | { | |
4864 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4865 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4866 | } | |
4867 | is_infinity = true; | |
4868 | } | |
4869 | ||
4870 | if (is_infinity) | |
4871 | { | |
4872 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4873 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4874 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4875 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4876 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4877 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4878 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4879 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4880 | } | |
4881 | ||
4882 | mpfr_clear(lr); | |
4883 | mpfr_clear(li); | |
4884 | mpfr_clear(rr); | |
4885 | mpfr_clear(ri); | |
4886 | } | |
4887 | ||
4888 | mpfr_clear(lrrr); | |
4889 | mpfr_clear(lrri); | |
4890 | mpfr_clear(lirr); | |
4891 | mpfr_clear(liri); | |
4892 | } | |
4893 | break; | |
4894 | case OPERATOR_DIV: | |
4895 | { | |
4896 | // For complex division we want to avoid having an | |
4897 | // intermediate overflow turn the whole result in a NaN. We | |
4898 | // scale the values to try to avoid this. | |
4899 | ||
4900 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
0c77715b | 4901 | { |
4902 | error_at(location, "division by zero"); | |
4903 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4904 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4905 | break; | |
4906 | } | |
e440a328 | 4907 | |
4908 | mpfr_t rra; | |
4909 | mpfr_t ria; | |
4910 | mpfr_init(rra); | |
4911 | mpfr_init(ria); | |
4912 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4913 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4914 | mpfr_t t; | |
4915 | mpfr_init(t); | |
4916 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4917 | ||
4918 | mpfr_t rr; | |
4919 | mpfr_t ri; | |
4920 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4921 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4922 | long ilogbw = 0; | |
4923 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4924 | { | |
4925 | ilogbw = mpfr_get_exp(t); | |
4926 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4927 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4928 | } | |
4929 | ||
4930 | mpfr_t denom; | |
4931 | mpfr_init(denom); | |
4932 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4933 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4934 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4935 | ||
4936 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4937 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4938 | mpfr_add(real, real, t, GMP_RNDN); | |
4939 | mpfr_div(real, real, denom, GMP_RNDN); | |
4940 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4941 | ||
4942 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4943 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4944 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4945 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
4946 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
4947 | ||
4948 | // If we wind up with NaN on both sides, check whether we | |
4949 | // should really have infinity. The rule is that if either | |
4950 | // side of the complex number is infinity, then the whole | |
4951 | // value is infinity, even if the other side is NaN. So the | |
4952 | // only case we have to fix is the one in which both sides are | |
4953 | // NaN. | |
4954 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4955 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4956 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4957 | { | |
4958 | if (mpfr_zero_p(denom)) | |
4959 | { | |
4960 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
4961 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
4962 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
4963 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
4964 | } | |
4965 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
4966 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
4967 | { | |
4968 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
4969 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
4970 | ||
4971 | mpfr_t t2; | |
4972 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
4973 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
4974 | ||
4975 | mpfr_t t3; | |
4976 | mpfr_init(t3); | |
4977 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
4978 | ||
4979 | mpfr_t t4; | |
4980 | mpfr_init(t4); | |
4981 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
4982 | ||
4983 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4984 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
4985 | ||
4986 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
4987 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
4988 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4989 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
4990 | ||
4991 | mpfr_clear(t2); | |
4992 | mpfr_clear(t3); | |
4993 | mpfr_clear(t4); | |
4994 | } | |
4995 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
4996 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
4997 | { | |
4998 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4999 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
5000 | ||
5001 | mpfr_t t2; | |
5002 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5003 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
5004 | ||
5005 | mpfr_t t3; | |
5006 | mpfr_init(t3); | |
5007 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
5008 | ||
5009 | mpfr_t t4; | |
5010 | mpfr_init(t4); | |
5011 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
5012 | ||
5013 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5014 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5015 | mpfr_mul(real, real, t3, GMP_RNDN); | |
5016 | ||
5017 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
5018 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
5019 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5020 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5021 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
5022 | ||
5023 | mpfr_clear(t2); | |
5024 | mpfr_clear(t3); | |
5025 | mpfr_clear(t4); | |
5026 | } | |
5027 | } | |
5028 | ||
5029 | mpfr_clear(denom); | |
5030 | mpfr_clear(rr); | |
5031 | mpfr_clear(ri); | |
5032 | mpfr_clear(t); | |
5033 | mpfr_clear(rra); | |
5034 | mpfr_clear(ria); | |
5035 | } | |
5036 | break; | |
e440a328 | 5037 | default: |
c3e6f413 | 5038 | go_unreachable(); |
e440a328 | 5039 | } |
5040 | ||
0c77715b | 5041 | mpfr_clear(left_real); |
5042 | mpfr_clear(left_imag); | |
5043 | mpfr_clear(right_real); | |
5044 | mpfr_clear(right_imag); | |
e440a328 | 5045 | |
0c77715b | 5046 | nc->set_complex(NULL, real, imag); |
5047 | mpfr_clear(real); | |
5048 | mpfr_clear(imag); | |
e440a328 | 5049 | |
0c77715b | 5050 | return ret; |
e440a328 | 5051 | } |
5052 | ||
5053 | // Lower a binary expression. We have to evaluate constant | |
5054 | // expressions now, in order to implement Go's unlimited precision | |
5055 | // constants. | |
5056 | ||
5057 | Expression* | |
e9d3367e | 5058 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5059 | Statement_inserter* inserter, int) | |
e440a328 | 5060 | { |
b13c66cd | 5061 | Location location = this->location(); |
e440a328 | 5062 | Operator op = this->op_; |
5063 | Expression* left = this->left_; | |
5064 | Expression* right = this->right_; | |
5065 | ||
5066 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5067 | || op == OPERATOR_NOTEQ | |
5068 | || op == OPERATOR_LT | |
5069 | || op == OPERATOR_LE | |
5070 | || op == OPERATOR_GT | |
5071 | || op == OPERATOR_GE); | |
5072 | ||
0c77715b | 5073 | // Numeric constant expressions. |
e440a328 | 5074 | { |
0c77715b | 5075 | Numeric_constant left_nc; |
5076 | Numeric_constant right_nc; | |
5077 | if (left->numeric_constant_value(&left_nc) | |
5078 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5079 | { |
0c77715b | 5080 | if (is_comparison) |
e440a328 | 5081 | { |
0c77715b | 5082 | bool result; |
5083 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5084 | &right_nc, location, | |
5085 | &result)) | |
5086 | return this; | |
e90c9dfc | 5087 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5088 | Expression::make_boolean(result, |
5089 | location), | |
5090 | location); | |
e440a328 | 5091 | } |
5092 | else | |
5093 | { | |
0c77715b | 5094 | Numeric_constant nc; |
5095 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5096 | location, &nc)) | |
5097 | return this; | |
5098 | return nc.expression(location); | |
e440a328 | 5099 | } |
5100 | } | |
e440a328 | 5101 | } |
5102 | ||
5103 | // String constant expressions. | |
315fa98d | 5104 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5105 | { |
5106 | std::string left_string; | |
5107 | std::string right_string; | |
5108 | if (left->string_constant_value(&left_string) | |
5109 | && right->string_constant_value(&right_string)) | |
315fa98d | 5110 | { |
5111 | if (op == OPERATOR_PLUS) | |
5112 | return Expression::make_string(left_string + right_string, | |
5113 | location); | |
5114 | else if (is_comparison) | |
5115 | { | |
5116 | int cmp = left_string.compare(right_string); | |
0c77715b | 5117 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5118 | return Expression::make_boolean(r, location); |
b40dc774 | 5119 | } |
5120 | } | |
b40dc774 | 5121 | } |
5122 | ||
e9d3367e | 5123 | // Lower struct and array comparisons. |
5124 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) | |
5125 | { | |
5126 | if (left->type()->struct_type() != NULL) | |
5127 | return this->lower_struct_comparison(gogo, inserter); | |
5128 | else if (left->type()->array_type() != NULL | |
5129 | && !left->type()->is_slice_type()) | |
5130 | return this->lower_array_comparison(gogo, inserter); | |
5131 | } | |
5132 | ||
e440a328 | 5133 | return this; |
5134 | } | |
5135 | ||
e9d3367e | 5136 | // Lower a struct comparison. |
5137 | ||
5138 | Expression* | |
5139 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5140 | Statement_inserter* inserter) | |
5141 | { | |
5142 | Struct_type* st = this->left_->type()->struct_type(); | |
5143 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5144 | if (st2 == NULL) | |
5145 | return this; | |
5146 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5147 | return this; | |
5148 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5149 | this->right_->type(), NULL)) | |
5150 | return this; | |
5151 | ||
5152 | // See if we can compare using memcmp. As a heuristic, we use | |
5153 | // memcmp rather than field references and comparisons if there are | |
5154 | // more than two fields. | |
113ef6a5 | 5155 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5156 | return this->lower_compare_to_memcmp(gogo, inserter); |
5157 | ||
5158 | Location loc = this->location(); | |
5159 | ||
5160 | Expression* left = this->left_; | |
5161 | Temporary_statement* left_temp = NULL; | |
5162 | if (left->var_expression() == NULL | |
5163 | && left->temporary_reference_expression() == NULL) | |
5164 | { | |
5165 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5166 | inserter->insert(left_temp); | |
5167 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5168 | } | |
5169 | ||
5170 | Expression* right = this->right_; | |
5171 | Temporary_statement* right_temp = NULL; | |
5172 | if (right->var_expression() == NULL | |
5173 | && right->temporary_reference_expression() == NULL) | |
5174 | { | |
5175 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5176 | inserter->insert(right_temp); | |
5177 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5178 | } | |
5179 | ||
5180 | Expression* ret = Expression::make_boolean(true, loc); | |
5181 | const Struct_field_list* fields = st->fields(); | |
5182 | unsigned int field_index = 0; | |
5183 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5184 | pf != fields->end(); | |
5185 | ++pf, ++field_index) | |
5186 | { | |
f5165c05 | 5187 | if (Gogo::is_sink_name(pf->field_name())) |
5188 | continue; | |
5189 | ||
e9d3367e | 5190 | if (field_index > 0) |
5191 | { | |
5192 | if (left_temp == NULL) | |
5193 | left = left->copy(); | |
5194 | else | |
5195 | left = Expression::make_temporary_reference(left_temp, loc); | |
5196 | if (right_temp == NULL) | |
5197 | right = right->copy(); | |
5198 | else | |
5199 | right = Expression::make_temporary_reference(right_temp, loc); | |
5200 | } | |
5201 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5202 | loc); | |
5203 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5204 | loc); | |
5205 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5206 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5207 | } | |
5208 | ||
5209 | if (this->op_ == OPERATOR_NOTEQ) | |
5210 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5211 | ||
5212 | return ret; | |
5213 | } | |
5214 | ||
5215 | // Lower an array comparison. | |
5216 | ||
5217 | Expression* | |
5218 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5219 | Statement_inserter* inserter) | |
5220 | { | |
5221 | Array_type* at = this->left_->type()->array_type(); | |
5222 | Array_type* at2 = this->right_->type()->array_type(); | |
5223 | if (at2 == NULL) | |
5224 | return this; | |
5225 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5226 | return this; | |
5227 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5228 | this->right_->type(), NULL)) | |
5229 | return this; | |
5230 | ||
5231 | // Call memcmp directly if possible. This may let the middle-end | |
5232 | // optimize the call. | |
113ef6a5 | 5233 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5234 | return this->lower_compare_to_memcmp(gogo, inserter); |
5235 | ||
5236 | // Call the array comparison function. | |
5237 | Named_object* hash_fn; | |
5238 | Named_object* equal_fn; | |
5239 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5240 | &hash_fn, &equal_fn); | |
5241 | ||
5242 | Location loc = this->location(); | |
5243 | ||
5244 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5245 | ||
5246 | Expression_list* args = new Expression_list(); | |
5247 | args->push_back(this->operand_address(inserter, this->left_)); | |
5248 | args->push_back(this->operand_address(inserter, this->right_)); | |
5249 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5250 | ||
5251 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5252 | ||
5253 | if (this->op_ == OPERATOR_NOTEQ) | |
5254 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5255 | ||
5256 | return ret; | |
5257 | } | |
5258 | ||
5259 | // Lower a struct or array comparison to a call to memcmp. | |
5260 | ||
5261 | Expression* | |
5262 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5263 | { | |
5264 | Location loc = this->location(); | |
5265 | ||
5266 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5267 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5268 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5269 | TYPE_INFO_SIZE); | |
5270 | ||
5271 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
5272 | ||
5273 | mpz_t zval; | |
5274 | mpz_init_set_ui(zval, 0); | |
5275 | Expression* zero = Expression::make_integer(&zval, NULL, loc); | |
5276 | mpz_clear(zval); | |
5277 | ||
5278 | return Expression::make_binary(this->op_, call, zero, loc); | |
5279 | } | |
5280 | ||
5281 | // Return the address of EXPR, cast to unsafe.Pointer. | |
5282 | ||
5283 | Expression* | |
5284 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5285 | Expression* expr) | |
5286 | { | |
5287 | Location loc = this->location(); | |
5288 | ||
5289 | if (!expr->is_addressable()) | |
5290 | { | |
5291 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5292 | loc); | |
5293 | inserter->insert(temp); | |
5294 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5295 | } | |
5296 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5297 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5298 | Type* void_type = Type::make_void_type(); | |
5299 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5300 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5301 | } | |
5302 | ||
0c77715b | 5303 | // Return the numeric constant value, if it has one. |
e440a328 | 5304 | |
5305 | bool | |
0c77715b | 5306 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5307 | { |
0c77715b | 5308 | Numeric_constant left_nc; |
5309 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5310 | return false; | |
5311 | Numeric_constant right_nc; | |
5312 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5313 | return false; | |
9767e2d3 | 5314 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5315 | this->location(), nc); |
e440a328 | 5316 | } |
5317 | ||
5318 | // Note that the value is being discarded. | |
5319 | ||
5320 | void | |
5321 | Binary_expression::do_discarding_value() | |
5322 | { | |
5323 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
5324 | this->right_->discarding_value(); | |
5325 | else | |
a7549a6a | 5326 | this->unused_value_error(); |
e440a328 | 5327 | } |
5328 | ||
5329 | // Get type. | |
5330 | ||
5331 | Type* | |
5332 | Binary_expression::do_type() | |
5333 | { | |
5f5fea79 | 5334 | if (this->classification() == EXPRESSION_ERROR) |
5335 | return Type::make_error_type(); | |
5336 | ||
e440a328 | 5337 | switch (this->op_) |
5338 | { | |
e440a328 | 5339 | case OPERATOR_EQEQ: |
5340 | case OPERATOR_NOTEQ: | |
5341 | case OPERATOR_LT: | |
5342 | case OPERATOR_LE: | |
5343 | case OPERATOR_GT: | |
5344 | case OPERATOR_GE: | |
e90c9dfc | 5345 | if (this->type_ == NULL) |
5346 | this->type_ = Type::make_boolean_type(); | |
5347 | return this->type_; | |
e440a328 | 5348 | |
5349 | case OPERATOR_PLUS: | |
5350 | case OPERATOR_MINUS: | |
5351 | case OPERATOR_OR: | |
5352 | case OPERATOR_XOR: | |
5353 | case OPERATOR_MULT: | |
5354 | case OPERATOR_DIV: | |
5355 | case OPERATOR_MOD: | |
5356 | case OPERATOR_AND: | |
5357 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5358 | case OPERATOR_OROR: |
5359 | case OPERATOR_ANDAND: | |
e440a328 | 5360 | { |
0c77715b | 5361 | Type* type; |
5362 | if (!Binary_expression::operation_type(this->op_, | |
5363 | this->left_->type(), | |
5364 | this->right_->type(), | |
5365 | &type)) | |
5366 | return Type::make_error_type(); | |
5367 | return type; | |
e440a328 | 5368 | } |
5369 | ||
5370 | case OPERATOR_LSHIFT: | |
5371 | case OPERATOR_RSHIFT: | |
5372 | return this->left_->type(); | |
5373 | ||
5374 | default: | |
c3e6f413 | 5375 | go_unreachable(); |
e440a328 | 5376 | } |
5377 | } | |
5378 | ||
5379 | // Set type for a binary expression. | |
5380 | ||
5381 | void | |
5382 | Binary_expression::do_determine_type(const Type_context* context) | |
5383 | { | |
5384 | Type* tleft = this->left_->type(); | |
5385 | Type* tright = this->right_->type(); | |
5386 | ||
5387 | // Both sides should have the same type, except for the shift | |
5388 | // operations. For a comparison, we should ignore the incoming | |
5389 | // type. | |
5390 | ||
5391 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5392 | || this->op_ == OPERATOR_RSHIFT); | |
5393 | ||
5394 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5395 | || this->op_ == OPERATOR_NOTEQ | |
5396 | || this->op_ == OPERATOR_LT | |
5397 | || this->op_ == OPERATOR_LE | |
5398 | || this->op_ == OPERATOR_GT | |
5399 | || this->op_ == OPERATOR_GE); | |
5400 | ||
5401 | Type_context subcontext(*context); | |
5402 | ||
5403 | if (is_comparison) | |
5404 | { | |
5405 | // In a comparison, the context does not determine the types of | |
5406 | // the operands. | |
5407 | subcontext.type = NULL; | |
5408 | } | |
5409 | ||
5410 | // Set the context for the left hand operand. | |
5411 | if (is_shift_op) | |
5412 | { | |
b40dc774 | 5413 | // The right hand operand of a shift plays no role in |
5414 | // determining the type of the left hand operand. | |
e440a328 | 5415 | } |
5416 | else if (!tleft->is_abstract()) | |
5417 | subcontext.type = tleft; | |
5418 | else if (!tright->is_abstract()) | |
5419 | subcontext.type = tright; | |
5420 | else if (subcontext.type == NULL) | |
5421 | { | |
5422 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5423 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5424 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5425 | { | |
5426 | // Both sides have an abstract integer, abstract float, or | |
5427 | // abstract complex type. Just let CONTEXT determine | |
5428 | // whether they may remain abstract or not. | |
5429 | } | |
5430 | else if (tleft->complex_type() != NULL) | |
5431 | subcontext.type = tleft; | |
5432 | else if (tright->complex_type() != NULL) | |
5433 | subcontext.type = tright; | |
5434 | else if (tleft->float_type() != NULL) | |
5435 | subcontext.type = tleft; | |
5436 | else if (tright->float_type() != NULL) | |
5437 | subcontext.type = tright; | |
5438 | else | |
5439 | subcontext.type = tleft; | |
f58a23ae | 5440 | |
5441 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5442 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5443 | } |
5444 | ||
5445 | this->left_->determine_type(&subcontext); | |
5446 | ||
e440a328 | 5447 | if (is_shift_op) |
5448 | { | |
b40dc774 | 5449 | // We may have inherited an unusable type for the shift operand. |
5450 | // Give a useful error if that happened. | |
5451 | if (tleft->is_abstract() | |
5452 | && subcontext.type != NULL | |
5453 | && (this->left_->type()->integer_type() == NULL | |
5454 | || (subcontext.type->integer_type() == NULL | |
5455 | && subcontext.type->float_type() == NULL | |
a7bb3691 | 5456 | && subcontext.type->complex_type() == NULL |
5457 | && subcontext.type->interface_type() == NULL))) | |
b40dc774 | 5458 | this->report_error(("invalid context-determined non-integer type " |
5459 | "for shift operand")); | |
5460 | ||
5461 | // The context for the right hand operand is the same as for the | |
5462 | // left hand operand, except for a shift operator. | |
e440a328 | 5463 | subcontext.type = Type::lookup_integer_type("uint"); |
5464 | subcontext.may_be_abstract = false; | |
5465 | } | |
5466 | ||
5467 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5468 | |
5469 | if (is_comparison) | |
5470 | { | |
5471 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5472 | ; | |
5473 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5474 | this->type_ = context->type; | |
5475 | else if (!context->may_be_abstract) | |
5476 | this->type_ = Type::lookup_bool_type(); | |
5477 | } | |
e440a328 | 5478 | } |
5479 | ||
5480 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5481 | // OTYPE is the type of the other operand. Return whether the |
5482 | // operation is OK. This should not be used for shift. | |
e440a328 | 5483 | |
5484 | bool | |
be8b5eee | 5485 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5486 | Location location) |
e440a328 | 5487 | { |
5488 | switch (op) | |
5489 | { | |
5490 | case OPERATOR_OROR: | |
5491 | case OPERATOR_ANDAND: | |
5492 | if (!type->is_boolean_type()) | |
5493 | { | |
5494 | error_at(location, "expected boolean type"); | |
5495 | return false; | |
5496 | } | |
5497 | break; | |
5498 | ||
5499 | case OPERATOR_EQEQ: | |
5500 | case OPERATOR_NOTEQ: | |
e9d3367e | 5501 | { |
5502 | std::string reason; | |
5503 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5504 | { | |
5505 | error_at(location, "%s", reason.c_str()); | |
5506 | return false; | |
5507 | } | |
5508 | } | |
e440a328 | 5509 | break; |
5510 | ||
5511 | case OPERATOR_LT: | |
5512 | case OPERATOR_LE: | |
5513 | case OPERATOR_GT: | |
5514 | case OPERATOR_GE: | |
e9d3367e | 5515 | { |
5516 | std::string reason; | |
5517 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5518 | { | |
5519 | error_at(location, "%s", reason.c_str()); | |
5520 | return false; | |
5521 | } | |
5522 | } | |
e440a328 | 5523 | break; |
5524 | ||
5525 | case OPERATOR_PLUS: | |
5526 | case OPERATOR_PLUSEQ: | |
5527 | if (type->integer_type() == NULL | |
5528 | && type->float_type() == NULL | |
5529 | && type->complex_type() == NULL | |
5530 | && !type->is_string_type()) | |
5531 | { | |
5532 | error_at(location, | |
5533 | "expected integer, floating, complex, or string type"); | |
5534 | return false; | |
5535 | } | |
5536 | break; | |
5537 | ||
5538 | case OPERATOR_MINUS: | |
5539 | case OPERATOR_MINUSEQ: | |
5540 | case OPERATOR_MULT: | |
5541 | case OPERATOR_MULTEQ: | |
5542 | case OPERATOR_DIV: | |
5543 | case OPERATOR_DIVEQ: | |
5544 | if (type->integer_type() == NULL | |
5545 | && type->float_type() == NULL | |
5546 | && type->complex_type() == NULL) | |
5547 | { | |
5548 | error_at(location, "expected integer, floating, or complex type"); | |
5549 | return false; | |
5550 | } | |
5551 | break; | |
5552 | ||
5553 | case OPERATOR_MOD: | |
5554 | case OPERATOR_MODEQ: | |
5555 | case OPERATOR_OR: | |
5556 | case OPERATOR_OREQ: | |
5557 | case OPERATOR_AND: | |
5558 | case OPERATOR_ANDEQ: | |
5559 | case OPERATOR_XOR: | |
5560 | case OPERATOR_XOREQ: | |
5561 | case OPERATOR_BITCLEAR: | |
5562 | case OPERATOR_BITCLEAREQ: | |
5563 | if (type->integer_type() == NULL) | |
5564 | { | |
5565 | error_at(location, "expected integer type"); | |
5566 | return false; | |
5567 | } | |
5568 | break; | |
5569 | ||
5570 | default: | |
c3e6f413 | 5571 | go_unreachable(); |
e440a328 | 5572 | } |
5573 | ||
5574 | return true; | |
5575 | } | |
5576 | ||
5577 | // Check types. | |
5578 | ||
5579 | void | |
5580 | Binary_expression::do_check_types(Gogo*) | |
5581 | { | |
5f5fea79 | 5582 | if (this->classification() == EXPRESSION_ERROR) |
5583 | return; | |
5584 | ||
e440a328 | 5585 | Type* left_type = this->left_->type(); |
5586 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5587 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5588 | { |
5589 | this->set_is_error(); | |
5590 | return; | |
5591 | } | |
e440a328 | 5592 | |
5593 | if (this->op_ == OPERATOR_EQEQ | |
5594 | || this->op_ == OPERATOR_NOTEQ | |
5595 | || this->op_ == OPERATOR_LT | |
5596 | || this->op_ == OPERATOR_LE | |
5597 | || this->op_ == OPERATOR_GT | |
5598 | || this->op_ == OPERATOR_GE) | |
5599 | { | |
5600 | if (!Type::are_assignable(left_type, right_type, NULL) | |
5601 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5602 | { | |
5603 | this->report_error(_("incompatible types in binary expression")); | |
5604 | return; | |
5605 | } | |
5606 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5607 | right_type, |
e440a328 | 5608 | this->location()) |
5609 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5610 | left_type, |
e440a328 | 5611 | this->location())) |
5612 | { | |
5613 | this->set_is_error(); | |
5614 | return; | |
5615 | } | |
5616 | } | |
5617 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5618 | { | |
5619 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
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 | { | |
5628 | this->set_is_error(); | |
5629 | return; | |
5630 | } | |
5631 | } | |
5632 | else | |
5633 | { | |
5634 | if (left_type->integer_type() == NULL) | |
5635 | this->report_error(_("shift of non-integer operand")); | |
5636 | ||
5637 | if (!right_type->is_abstract() | |
5638 | && (right_type->integer_type() == NULL | |
5639 | || !right_type->integer_type()->is_unsigned())) | |
5640 | this->report_error(_("shift count not unsigned integer")); | |
5641 | else | |
5642 | { | |
0c77715b | 5643 | Numeric_constant nc; |
5644 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5645 | { |
0c77715b | 5646 | mpz_t val; |
5647 | if (!nc.to_int(&val)) | |
5648 | this->report_error(_("shift count not unsigned integer")); | |
5649 | else | |
a4eba91b | 5650 | { |
0c77715b | 5651 | if (mpz_sgn(val) < 0) |
5652 | { | |
5653 | this->report_error(_("negative shift count")); | |
5654 | mpz_set_ui(val, 0); | |
5655 | Location rloc = this->right_->location(); | |
5656 | this->right_ = Expression::make_integer(&val, right_type, | |
5657 | rloc); | |
5658 | } | |
5659 | mpz_clear(val); | |
a4eba91b | 5660 | } |
e440a328 | 5661 | } |
e440a328 | 5662 | } |
5663 | } | |
5664 | } | |
5665 | ||
5666 | // Get a tree for a binary expression. | |
5667 | ||
5668 | tree | |
5669 | Binary_expression::do_get_tree(Translate_context* context) | |
5670 | { | |
5671 | tree left = this->left_->get_tree(context); | |
5672 | tree right = this->right_->get_tree(context); | |
5673 | ||
5674 | if (left == error_mark_node || right == error_mark_node) | |
5675 | return error_mark_node; | |
5676 | ||
5677 | enum tree_code code; | |
5678 | bool use_left_type = true; | |
5679 | bool is_shift_op = false; | |
29a2d1d8 | 5680 | bool is_idiv_op = false; |
e440a328 | 5681 | switch (this->op_) |
5682 | { | |
5683 | case OPERATOR_EQEQ: | |
5684 | case OPERATOR_NOTEQ: | |
5685 | case OPERATOR_LT: | |
5686 | case OPERATOR_LE: | |
5687 | case OPERATOR_GT: | |
5688 | case OPERATOR_GE: | |
e90c9dfc | 5689 | return Expression::comparison_tree(context, this->type_, this->op_, |
e440a328 | 5690 | this->left_->type(), left, |
5691 | this->right_->type(), right, | |
5692 | this->location()); | |
5693 | ||
5694 | case OPERATOR_OROR: | |
5695 | code = TRUTH_ORIF_EXPR; | |
5696 | use_left_type = false; | |
5697 | break; | |
5698 | case OPERATOR_ANDAND: | |
5699 | code = TRUTH_ANDIF_EXPR; | |
5700 | use_left_type = false; | |
5701 | break; | |
5702 | case OPERATOR_PLUS: | |
5703 | code = PLUS_EXPR; | |
5704 | break; | |
5705 | case OPERATOR_MINUS: | |
5706 | code = MINUS_EXPR; | |
5707 | break; | |
5708 | case OPERATOR_OR: | |
5709 | code = BIT_IOR_EXPR; | |
5710 | break; | |
5711 | case OPERATOR_XOR: | |
5712 | code = BIT_XOR_EXPR; | |
5713 | break; | |
5714 | case OPERATOR_MULT: | |
5715 | code = MULT_EXPR; | |
5716 | break; | |
5717 | case OPERATOR_DIV: | |
5718 | { | |
5719 | Type *t = this->left_->type(); | |
5720 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5721 | code = RDIV_EXPR; | |
5722 | else | |
29a2d1d8 | 5723 | { |
5724 | code = TRUNC_DIV_EXPR; | |
5725 | is_idiv_op = true; | |
5726 | } | |
e440a328 | 5727 | } |
5728 | break; | |
5729 | case OPERATOR_MOD: | |
5730 | code = TRUNC_MOD_EXPR; | |
29a2d1d8 | 5731 | is_idiv_op = true; |
e440a328 | 5732 | break; |
5733 | case OPERATOR_LSHIFT: | |
5734 | code = LSHIFT_EXPR; | |
5735 | is_shift_op = true; | |
5736 | break; | |
5737 | case OPERATOR_RSHIFT: | |
5738 | code = RSHIFT_EXPR; | |
5739 | is_shift_op = true; | |
5740 | break; | |
5741 | case OPERATOR_AND: | |
5742 | code = BIT_AND_EXPR; | |
5743 | break; | |
5744 | case OPERATOR_BITCLEAR: | |
5745 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5746 | code = BIT_AND_EXPR; | |
5747 | break; | |
5748 | default: | |
c3e6f413 | 5749 | go_unreachable(); |
e440a328 | 5750 | } |
5751 | ||
29a2d1d8 | 5752 | location_t gccloc = this->location().gcc_location(); |
e440a328 | 5753 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); |
5754 | ||
5755 | if (this->left_->type()->is_string_type()) | |
5756 | { | |
c484d925 | 5757 | go_assert(this->op_ == OPERATOR_PLUS); |
9f0e0513 | 5758 | Type* st = Type::make_string_type(); |
5759 | tree string_type = type_to_tree(st->get_backend(context->gogo())); | |
e440a328 | 5760 | static tree string_plus_decl; |
5761 | return Gogo::call_builtin(&string_plus_decl, | |
5762 | this->location(), | |
5763 | "__go_string_plus", | |
5764 | 2, | |
5765 | string_type, | |
5766 | string_type, | |
5767 | left, | |
5768 | string_type, | |
5769 | right); | |
5770 | } | |
5771 | ||
5772 | tree compute_type = excess_precision_type(type); | |
5773 | if (compute_type != NULL_TREE) | |
5774 | { | |
5775 | left = ::convert(compute_type, left); | |
5776 | right = ::convert(compute_type, right); | |
5777 | } | |
5778 | ||
5779 | tree eval_saved = NULL_TREE; | |
29a2d1d8 | 5780 | if (is_shift_op |
5781 | || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow))) | |
e440a328 | 5782 | { |
e440a328 | 5783 | // Make sure the values are evaluated. |
29a2d1d8 | 5784 | if (!DECL_P(left)) |
a7a70f31 | 5785 | { |
5786 | left = save_expr(left); | |
5787 | eval_saved = left; | |
5788 | } | |
29a2d1d8 | 5789 | if (!DECL_P(right)) |
a7a70f31 | 5790 | { |
5791 | right = save_expr(right); | |
5792 | if (eval_saved == NULL_TREE) | |
5793 | eval_saved = right; | |
5794 | else | |
29a2d1d8 | 5795 | eval_saved = fold_build2_loc(gccloc, COMPOUND_EXPR, |
a7a70f31 | 5796 | void_type_node, eval_saved, right); |
5797 | } | |
e440a328 | 5798 | } |
5799 | ||
29a2d1d8 | 5800 | tree ret = fold_build2_loc(gccloc, code, |
e440a328 | 5801 | compute_type != NULL_TREE ? compute_type : type, |
5802 | left, right); | |
5803 | ||
5804 | if (compute_type != NULL_TREE) | |
5805 | ret = ::convert(type, ret); | |
5806 | ||
5807 | // In Go, a shift larger than the size of the type is well-defined. | |
5808 | // This is not true in GENERIC, so we need to insert a conditional. | |
5809 | if (is_shift_op) | |
5810 | { | |
c484d925 | 5811 | go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); |
5812 | go_assert(this->left_->type()->integer_type() != NULL); | |
e440a328 | 5813 | int bits = TYPE_PRECISION(TREE_TYPE(left)); |
5814 | ||
5815 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
5816 | build_int_cst_type(TREE_TYPE(right), bits)); | |
5817 | ||
29a2d1d8 | 5818 | tree overflow_result = fold_convert_loc(gccloc, TREE_TYPE(left), |
e440a328 | 5819 | integer_zero_node); |
5820 | if (this->op_ == OPERATOR_RSHIFT | |
5821 | && !this->left_->type()->integer_type()->is_unsigned()) | |
5822 | { | |
b13c66cd | 5823 | tree neg = |
29a2d1d8 | 5824 | fold_build2_loc(gccloc, LT_EXPR, boolean_type_node, |
5825 | left, | |
5826 | fold_convert_loc(gccloc, TREE_TYPE(left), | |
b13c66cd | 5827 | integer_zero_node)); |
5828 | tree neg_one = | |
29a2d1d8 | 5829 | fold_build2_loc(gccloc, MINUS_EXPR, TREE_TYPE(left), |
5830 | fold_convert_loc(gccloc, TREE_TYPE(left), | |
b13c66cd | 5831 | integer_zero_node), |
29a2d1d8 | 5832 | fold_convert_loc(gccloc, TREE_TYPE(left), |
b13c66cd | 5833 | integer_one_node)); |
5834 | overflow_result = | |
29a2d1d8 | 5835 | fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left), |
5836 | neg, neg_one, overflow_result); | |
5837 | } | |
5838 | ||
5839 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left), | |
5840 | compare, ret, overflow_result); | |
5841 | ||
5842 | if (eval_saved != NULL_TREE) | |
5843 | ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), | |
5844 | eval_saved, ret); | |
5845 | } | |
5846 | ||
5847 | // Add checks for division by zero and division overflow as needed. | |
5848 | if (is_idiv_op) | |
5849 | { | |
5850 | if (go_check_divide_zero) | |
5851 | { | |
5852 | // right == 0 | |
5853 | tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
5854 | right, | |
5855 | fold_convert_loc(gccloc, | |
5856 | TREE_TYPE(right), | |
5857 | integer_zero_node)); | |
5858 | ||
5859 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0 | |
5860 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; | |
5861 | tree panic = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), | |
5862 | Gogo::runtime_error(errcode, | |
5863 | this->location()), | |
5864 | fold_convert_loc(gccloc, TREE_TYPE(ret), | |
5865 | integer_zero_node)); | |
5866 | ||
5867 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
5868 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5869 | check, panic, ret); | |
b13c66cd | 5870 | } |
5871 | ||
29a2d1d8 | 5872 | if (go_check_divide_overflow) |
5873 | { | |
5874 | // right == -1 | |
5875 | // FIXME: It would be nice to say that this test is expected | |
5876 | // to return false. | |
5877 | tree m1 = integer_minus_one_node; | |
5878 | tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
5879 | right, | |
5880 | fold_convert_loc(gccloc, | |
5881 | TREE_TYPE(right), | |
5882 | m1)); | |
5883 | ||
5884 | tree overflow; | |
5885 | if (TYPE_UNSIGNED(TREE_TYPE(ret))) | |
5886 | { | |
5887 | // An unsigned -1 is the largest possible number, so | |
5888 | // dividing is always 1 or 0. | |
5889 | tree cmp = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
5890 | left, right); | |
5891 | if (this->op_ == OPERATOR_DIV) | |
5892 | overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5893 | cmp, | |
5894 | fold_convert_loc(gccloc, | |
5895 | TREE_TYPE(ret), | |
5896 | integer_one_node), | |
5897 | fold_convert_loc(gccloc, | |
5898 | TREE_TYPE(ret), | |
5899 | integer_zero_node)); | |
5900 | else | |
5901 | overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5902 | cmp, | |
5903 | fold_convert_loc(gccloc, | |
5904 | TREE_TYPE(ret), | |
5905 | integer_zero_node), | |
5906 | left); | |
5907 | } | |
5908 | else | |
5909 | { | |
5910 | // Computing left / -1 is the same as computing - left, | |
5911 | // which does not overflow since Go sets -fwrapv. | |
5912 | if (this->op_ == OPERATOR_DIV) | |
5913 | overflow = fold_build1_loc(gccloc, NEGATE_EXPR, TREE_TYPE(left), | |
5914 | left); | |
5915 | else | |
5916 | overflow = integer_zero_node; | |
5917 | } | |
5918 | overflow = fold_convert_loc(gccloc, TREE_TYPE(ret), overflow); | |
5919 | ||
5920 | // right == -1 ? - left : ret | |
5921 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
5922 | check, overflow, ret); | |
5923 | } | |
e440a328 | 5924 | |
a7a70f31 | 5925 | if (eval_saved != NULL_TREE) |
29a2d1d8 | 5926 | ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), |
5927 | eval_saved, ret); | |
e440a328 | 5928 | } |
5929 | ||
5930 | return ret; | |
5931 | } | |
5932 | ||
5933 | // Export a binary expression. | |
5934 | ||
5935 | void | |
5936 | Binary_expression::do_export(Export* exp) const | |
5937 | { | |
5938 | exp->write_c_string("("); | |
5939 | this->left_->export_expression(exp); | |
5940 | switch (this->op_) | |
5941 | { | |
5942 | case OPERATOR_OROR: | |
5943 | exp->write_c_string(" || "); | |
5944 | break; | |
5945 | case OPERATOR_ANDAND: | |
5946 | exp->write_c_string(" && "); | |
5947 | break; | |
5948 | case OPERATOR_EQEQ: | |
5949 | exp->write_c_string(" == "); | |
5950 | break; | |
5951 | case OPERATOR_NOTEQ: | |
5952 | exp->write_c_string(" != "); | |
5953 | break; | |
5954 | case OPERATOR_LT: | |
5955 | exp->write_c_string(" < "); | |
5956 | break; | |
5957 | case OPERATOR_LE: | |
5958 | exp->write_c_string(" <= "); | |
5959 | break; | |
5960 | case OPERATOR_GT: | |
5961 | exp->write_c_string(" > "); | |
5962 | break; | |
5963 | case OPERATOR_GE: | |
5964 | exp->write_c_string(" >= "); | |
5965 | break; | |
5966 | case OPERATOR_PLUS: | |
5967 | exp->write_c_string(" + "); | |
5968 | break; | |
5969 | case OPERATOR_MINUS: | |
5970 | exp->write_c_string(" - "); | |
5971 | break; | |
5972 | case OPERATOR_OR: | |
5973 | exp->write_c_string(" | "); | |
5974 | break; | |
5975 | case OPERATOR_XOR: | |
5976 | exp->write_c_string(" ^ "); | |
5977 | break; | |
5978 | case OPERATOR_MULT: | |
5979 | exp->write_c_string(" * "); | |
5980 | break; | |
5981 | case OPERATOR_DIV: | |
5982 | exp->write_c_string(" / "); | |
5983 | break; | |
5984 | case OPERATOR_MOD: | |
5985 | exp->write_c_string(" % "); | |
5986 | break; | |
5987 | case OPERATOR_LSHIFT: | |
5988 | exp->write_c_string(" << "); | |
5989 | break; | |
5990 | case OPERATOR_RSHIFT: | |
5991 | exp->write_c_string(" >> "); | |
5992 | break; | |
5993 | case OPERATOR_AND: | |
5994 | exp->write_c_string(" & "); | |
5995 | break; | |
5996 | case OPERATOR_BITCLEAR: | |
5997 | exp->write_c_string(" &^ "); | |
5998 | break; | |
5999 | default: | |
c3e6f413 | 6000 | go_unreachable(); |
e440a328 | 6001 | } |
6002 | this->right_->export_expression(exp); | |
6003 | exp->write_c_string(")"); | |
6004 | } | |
6005 | ||
6006 | // Import a binary expression. | |
6007 | ||
6008 | Expression* | |
6009 | Binary_expression::do_import(Import* imp) | |
6010 | { | |
6011 | imp->require_c_string("("); | |
6012 | ||
6013 | Expression* left = Expression::import_expression(imp); | |
6014 | ||
6015 | Operator op; | |
6016 | if (imp->match_c_string(" || ")) | |
6017 | { | |
6018 | op = OPERATOR_OROR; | |
6019 | imp->advance(4); | |
6020 | } | |
6021 | else if (imp->match_c_string(" && ")) | |
6022 | { | |
6023 | op = OPERATOR_ANDAND; | |
6024 | imp->advance(4); | |
6025 | } | |
6026 | else if (imp->match_c_string(" == ")) | |
6027 | { | |
6028 | op = OPERATOR_EQEQ; | |
6029 | imp->advance(4); | |
6030 | } | |
6031 | else if (imp->match_c_string(" != ")) | |
6032 | { | |
6033 | op = OPERATOR_NOTEQ; | |
6034 | imp->advance(4); | |
6035 | } | |
6036 | else if (imp->match_c_string(" < ")) | |
6037 | { | |
6038 | op = OPERATOR_LT; | |
6039 | imp->advance(3); | |
6040 | } | |
6041 | else if (imp->match_c_string(" <= ")) | |
6042 | { | |
6043 | op = OPERATOR_LE; | |
6044 | imp->advance(4); | |
6045 | } | |
6046 | else if (imp->match_c_string(" > ")) | |
6047 | { | |
6048 | op = OPERATOR_GT; | |
6049 | imp->advance(3); | |
6050 | } | |
6051 | else if (imp->match_c_string(" >= ")) | |
6052 | { | |
6053 | op = OPERATOR_GE; | |
6054 | imp->advance(4); | |
6055 | } | |
6056 | else if (imp->match_c_string(" + ")) | |
6057 | { | |
6058 | op = OPERATOR_PLUS; | |
6059 | imp->advance(3); | |
6060 | } | |
6061 | else if (imp->match_c_string(" - ")) | |
6062 | { | |
6063 | op = OPERATOR_MINUS; | |
6064 | imp->advance(3); | |
6065 | } | |
6066 | else if (imp->match_c_string(" | ")) | |
6067 | { | |
6068 | op = OPERATOR_OR; | |
6069 | imp->advance(3); | |
6070 | } | |
6071 | else if (imp->match_c_string(" ^ ")) | |
6072 | { | |
6073 | op = OPERATOR_XOR; | |
6074 | imp->advance(3); | |
6075 | } | |
6076 | else if (imp->match_c_string(" * ")) | |
6077 | { | |
6078 | op = OPERATOR_MULT; | |
6079 | imp->advance(3); | |
6080 | } | |
6081 | else if (imp->match_c_string(" / ")) | |
6082 | { | |
6083 | op = OPERATOR_DIV; | |
6084 | imp->advance(3); | |
6085 | } | |
6086 | else if (imp->match_c_string(" % ")) | |
6087 | { | |
6088 | op = OPERATOR_MOD; | |
6089 | imp->advance(3); | |
6090 | } | |
6091 | else if (imp->match_c_string(" << ")) | |
6092 | { | |
6093 | op = OPERATOR_LSHIFT; | |
6094 | imp->advance(4); | |
6095 | } | |
6096 | else if (imp->match_c_string(" >> ")) | |
6097 | { | |
6098 | op = OPERATOR_RSHIFT; | |
6099 | imp->advance(4); | |
6100 | } | |
6101 | else if (imp->match_c_string(" & ")) | |
6102 | { | |
6103 | op = OPERATOR_AND; | |
6104 | imp->advance(3); | |
6105 | } | |
6106 | else if (imp->match_c_string(" &^ ")) | |
6107 | { | |
6108 | op = OPERATOR_BITCLEAR; | |
6109 | imp->advance(4); | |
6110 | } | |
6111 | else | |
6112 | { | |
6113 | error_at(imp->location(), "unrecognized binary operator"); | |
6114 | return Expression::make_error(imp->location()); | |
6115 | } | |
6116 | ||
6117 | Expression* right = Expression::import_expression(imp); | |
6118 | ||
6119 | imp->require_c_string(")"); | |
6120 | ||
6121 | return Expression::make_binary(op, left, right, imp->location()); | |
6122 | } | |
6123 | ||
d751bb78 | 6124 | // Dump ast representation of a binary expression. |
6125 | ||
6126 | void | |
6127 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6128 | { | |
6129 | ast_dump_context->ostream() << "("; | |
6130 | ast_dump_context->dump_expression(this->left_); | |
6131 | ast_dump_context->ostream() << " "; | |
6132 | ast_dump_context->dump_operator(this->op_); | |
6133 | ast_dump_context->ostream() << " "; | |
6134 | ast_dump_context->dump_expression(this->right_); | |
6135 | ast_dump_context->ostream() << ") "; | |
6136 | } | |
6137 | ||
e440a328 | 6138 | // Make a binary expression. |
6139 | ||
6140 | Expression* | |
6141 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6142 | Location location) |
e440a328 | 6143 | { |
6144 | return new Binary_expression(op, left, right, location); | |
6145 | } | |
6146 | ||
6147 | // Implement a comparison. | |
6148 | ||
6149 | tree | |
e90c9dfc | 6150 | Expression::comparison_tree(Translate_context* context, Type* result_type, |
6151 | Operator op, Type* left_type, tree left_tree, | |
e440a328 | 6152 | Type* right_type, tree right_tree, |
b13c66cd | 6153 | Location location) |
e440a328 | 6154 | { |
6155 | enum tree_code code; | |
6156 | switch (op) | |
6157 | { | |
6158 | case OPERATOR_EQEQ: | |
6159 | code = EQ_EXPR; | |
6160 | break; | |
6161 | case OPERATOR_NOTEQ: | |
6162 | code = NE_EXPR; | |
6163 | break; | |
6164 | case OPERATOR_LT: | |
6165 | code = LT_EXPR; | |
6166 | break; | |
6167 | case OPERATOR_LE: | |
6168 | code = LE_EXPR; | |
6169 | break; | |
6170 | case OPERATOR_GT: | |
6171 | code = GT_EXPR; | |
6172 | break; | |
6173 | case OPERATOR_GE: | |
6174 | code = GE_EXPR; | |
6175 | break; | |
6176 | default: | |
c3e6f413 | 6177 | go_unreachable(); |
e440a328 | 6178 | } |
6179 | ||
15c67ee2 | 6180 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6181 | { |
9f0e0513 | 6182 | Type* st = Type::make_string_type(); |
6183 | tree string_type = type_to_tree(st->get_backend(context->gogo())); | |
e440a328 | 6184 | static tree string_compare_decl; |
6185 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
6186 | location, | |
6187 | "__go_strcmp", | |
6188 | 2, | |
6189 | integer_type_node, | |
6190 | string_type, | |
6191 | left_tree, | |
6192 | string_type, | |
6193 | right_tree); | |
6194 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6195 | } | |
15c67ee2 | 6196 | else if ((left_type->interface_type() != NULL |
6197 | && right_type->interface_type() == NULL | |
6198 | && !right_type->is_nil_type()) | |
6199 | || (left_type->interface_type() == NULL | |
6200 | && !left_type->is_nil_type() | |
6201 | && right_type->interface_type() != NULL)) | |
e440a328 | 6202 | { |
6203 | // Comparing an interface value to a non-interface value. | |
6204 | if (left_type->interface_type() == NULL) | |
6205 | { | |
6206 | std::swap(left_type, right_type); | |
6207 | std::swap(left_tree, right_tree); | |
6208 | } | |
6209 | ||
6210 | // The right operand is not an interface. We need to take its | |
6211 | // address if it is not a pointer. | |
6212 | tree make_tmp; | |
6213 | tree arg; | |
6214 | if (right_type->points_to() != NULL) | |
6215 | { | |
6216 | make_tmp = NULL_TREE; | |
6217 | arg = right_tree; | |
6218 | } | |
dd28fd36 | 6219 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) |
6220 | || (TREE_CODE(right_tree) != CONST_DECL | |
6221 | && DECL_P(right_tree))) | |
e440a328 | 6222 | { |
6223 | make_tmp = NULL_TREE; | |
b13c66cd | 6224 | arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree); |
e440a328 | 6225 | if (DECL_P(right_tree)) |
6226 | TREE_ADDRESSABLE(right_tree) = 1; | |
6227 | } | |
6228 | else | |
6229 | { | |
6230 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
6231 | get_name(right_tree)); | |
6232 | DECL_IGNORED_P(tmp) = 0; | |
6233 | DECL_INITIAL(tmp) = right_tree; | |
6234 | TREE_ADDRESSABLE(tmp) = 1; | |
6235 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
b13c66cd | 6236 | SET_EXPR_LOCATION(make_tmp, location.gcc_location()); |
6237 | arg = build_fold_addr_expr_loc(location.gcc_location(), tmp); | |
e440a328 | 6238 | } |
b13c66cd | 6239 | arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg); |
e440a328 | 6240 | |
a1d23b41 | 6241 | tree descriptor = right_type->type_descriptor_pointer(context->gogo(), |
6242 | location); | |
e440a328 | 6243 | |
6244 | if (left_type->interface_type()->is_empty()) | |
6245 | { | |
6246 | static tree empty_interface_value_compare_decl; | |
6247 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6248 | location, | |
6249 | "__go_empty_interface_value_compare", | |
6250 | 3, | |
6251 | integer_type_node, | |
6252 | TREE_TYPE(left_tree), | |
6253 | left_tree, | |
6254 | TREE_TYPE(descriptor), | |
6255 | descriptor, | |
6256 | ptr_type_node, | |
6257 | arg); | |
5fb82b5e | 6258 | if (left_tree == error_mark_node) |
6259 | return error_mark_node; | |
e440a328 | 6260 | // This can panic if the type is not comparable. |
6261 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6262 | } | |
6263 | else | |
6264 | { | |
6265 | static tree interface_value_compare_decl; | |
6266 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6267 | location, | |
6268 | "__go_interface_value_compare", | |
6269 | 3, | |
6270 | integer_type_node, | |
6271 | TREE_TYPE(left_tree), | |
6272 | left_tree, | |
6273 | TREE_TYPE(descriptor), | |
6274 | descriptor, | |
6275 | ptr_type_node, | |
6276 | arg); | |
5fb82b5e | 6277 | if (left_tree == error_mark_node) |
6278 | return error_mark_node; | |
e440a328 | 6279 | // This can panic if the type is not comparable. |
6280 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6281 | } | |
6282 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6283 | ||
6284 | if (make_tmp != NULL_TREE) | |
6285 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6286 | left_tree); | |
6287 | } | |
6288 | else if (left_type->interface_type() != NULL | |
6289 | && right_type->interface_type() != NULL) | |
6290 | { | |
739bad04 | 6291 | if (left_type->interface_type()->is_empty() |
6292 | && right_type->interface_type()->is_empty()) | |
e440a328 | 6293 | { |
e440a328 | 6294 | static tree empty_interface_compare_decl; |
6295 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6296 | location, | |
6297 | "__go_empty_interface_compare", | |
6298 | 2, | |
6299 | integer_type_node, | |
6300 | TREE_TYPE(left_tree), | |
6301 | left_tree, | |
6302 | TREE_TYPE(right_tree), | |
6303 | right_tree); | |
5fb82b5e | 6304 | if (left_tree == error_mark_node) |
6305 | return error_mark_node; | |
e440a328 | 6306 | // This can panic if the type is uncomparable. |
6307 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6308 | } | |
739bad04 | 6309 | else if (!left_type->interface_type()->is_empty() |
6310 | && !right_type->interface_type()->is_empty()) | |
e440a328 | 6311 | { |
e440a328 | 6312 | static tree interface_compare_decl; |
6313 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6314 | location, | |
6315 | "__go_interface_compare", | |
6316 | 2, | |
6317 | integer_type_node, | |
6318 | TREE_TYPE(left_tree), | |
6319 | left_tree, | |
6320 | TREE_TYPE(right_tree), | |
6321 | right_tree); | |
5fb82b5e | 6322 | if (left_tree == error_mark_node) |
6323 | return error_mark_node; | |
e440a328 | 6324 | // This can panic if the type is uncomparable. |
6325 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6326 | } | |
739bad04 | 6327 | else |
6328 | { | |
6329 | if (left_type->interface_type()->is_empty()) | |
6330 | { | |
c484d925 | 6331 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
739bad04 | 6332 | std::swap(left_type, right_type); |
6333 | std::swap(left_tree, right_tree); | |
6334 | } | |
c484d925 | 6335 | go_assert(!left_type->interface_type()->is_empty()); |
6336 | go_assert(right_type->interface_type()->is_empty()); | |
739bad04 | 6337 | static tree interface_empty_compare_decl; |
6338 | left_tree = Gogo::call_builtin(&interface_empty_compare_decl, | |
6339 | location, | |
6340 | "__go_interface_empty_compare", | |
6341 | 2, | |
6342 | integer_type_node, | |
6343 | TREE_TYPE(left_tree), | |
6344 | left_tree, | |
6345 | TREE_TYPE(right_tree), | |
6346 | right_tree); | |
6347 | if (left_tree == error_mark_node) | |
6348 | return error_mark_node; | |
6349 | // This can panic if the type is uncomparable. | |
6350 | TREE_NOTHROW(interface_empty_compare_decl) = 0; | |
6351 | } | |
6352 | ||
e440a328 | 6353 | right_tree = build_int_cst_type(integer_type_node, 0); |
6354 | } | |
6355 | ||
6356 | if (left_type->is_nil_type() | |
6357 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6358 | { | |
6359 | std::swap(left_type, right_type); | |
6360 | std::swap(left_tree, right_tree); | |
6361 | } | |
6362 | ||
6363 | if (right_type->is_nil_type()) | |
6364 | { | |
6365 | if (left_type->array_type() != NULL | |
6366 | && left_type->array_type()->length() == NULL) | |
6367 | { | |
6368 | Array_type* at = left_type->array_type(); | |
6369 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6370 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6371 | } | |
6372 | else if (left_type->interface_type() != NULL) | |
6373 | { | |
6374 | // An interface is nil if the first field is nil. | |
6375 | tree left_type_tree = TREE_TYPE(left_tree); | |
c484d925 | 6376 | go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); |
e440a328 | 6377 | tree field = TYPE_FIELDS(left_type_tree); |
6378 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6379 | field, NULL_TREE); | |
6380 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6381 | } | |
6382 | else | |
6383 | { | |
c484d925 | 6384 | go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); |
e440a328 | 6385 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); |
6386 | } | |
6387 | } | |
6388 | ||
d8ccb1e3 | 6389 | if (left_tree == error_mark_node || right_tree == error_mark_node) |
6390 | return error_mark_node; | |
6391 | ||
e90c9dfc | 6392 | tree result_type_tree; |
6393 | if (result_type == NULL) | |
6394 | result_type_tree = boolean_type_node; | |
6395 | else | |
6396 | result_type_tree = type_to_tree(result_type->get_backend(context->gogo())); | |
6397 | ||
6398 | tree ret = fold_build2(code, result_type_tree, left_tree, right_tree); | |
e440a328 | 6399 | if (CAN_HAVE_LOCATION_P(ret)) |
b13c66cd | 6400 | SET_EXPR_LOCATION(ret, location.gcc_location()); |
e440a328 | 6401 | return ret; |
6402 | } | |
6403 | ||
6404 | // Class Bound_method_expression. | |
6405 | ||
6406 | // Traversal. | |
6407 | ||
6408 | int | |
6409 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6410 | { | |
e0659c9e | 6411 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6412 | } |
6413 | ||
6414 | // Return the type of a bound method expression. The type of this | |
6415 | // object is really the type of the method with no receiver. We | |
6416 | // should be able to get away with just returning the type of the | |
6417 | // method. | |
6418 | ||
6419 | Type* | |
6420 | Bound_method_expression::do_type() | |
6421 | { | |
e0659c9e | 6422 | if (this->method_->is_function()) |
6423 | return this->method_->func_value()->type(); | |
6424 | else if (this->method_->is_function_declaration()) | |
6425 | return this->method_->func_declaration_value()->type(); | |
6426 | else | |
6427 | return Type::make_error_type(); | |
e440a328 | 6428 | } |
6429 | ||
6430 | // Determine the types of a method expression. | |
6431 | ||
6432 | void | |
6433 | Bound_method_expression::do_determine_type(const Type_context*) | |
6434 | { | |
e0659c9e | 6435 | Function_type* fntype = this->type()->function_type(); |
e440a328 | 6436 | if (fntype == NULL || !fntype->is_method()) |
6437 | this->expr_->determine_type_no_context(); | |
6438 | else | |
6439 | { | |
6440 | Type_context subcontext(fntype->receiver()->type(), false); | |
6441 | this->expr_->determine_type(&subcontext); | |
6442 | } | |
6443 | } | |
6444 | ||
6445 | // Check the types of a method expression. | |
6446 | ||
6447 | void | |
6448 | Bound_method_expression::do_check_types(Gogo*) | |
6449 | { | |
e0659c9e | 6450 | if (!this->method_->is_function() |
6451 | && !this->method_->is_function_declaration()) | |
e440a328 | 6452 | this->report_error(_("object is not a method")); |
6453 | else | |
6454 | { | |
e0659c9e | 6455 | Type* rtype = this->type()->function_type()->receiver()->type()->deref(); |
e440a328 | 6456 | Type* etype = (this->expr_type_ != NULL |
6457 | ? this->expr_type_ | |
6458 | : this->expr_->type()); | |
6459 | etype = etype->deref(); | |
07ba8be5 | 6460 | if (!Type::are_identical(rtype, etype, true, NULL)) |
e440a328 | 6461 | this->report_error(_("method type does not match object type")); |
6462 | } | |
6463 | } | |
6464 | ||
6465 | // Get the tree for a method expression. There is no standard tree | |
6466 | // representation for this. The only places it may currently be used | |
6467 | // are in a Call_expression or a Go_statement, which will take it | |
6468 | // apart directly. So this has nothing to do at present. | |
6469 | ||
6470 | tree | |
6471 | Bound_method_expression::do_get_tree(Translate_context*) | |
6472 | { | |
d40405e2 | 6473 | error_at(this->location(), "reference to method other than calling it"); |
6474 | return error_mark_node; | |
e440a328 | 6475 | } |
6476 | ||
d751bb78 | 6477 | // Dump ast representation of a bound method expression. |
6478 | ||
6479 | void | |
6480 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6481 | const | |
6482 | { | |
6483 | if (this->expr_type_ != NULL) | |
6484 | ast_dump_context->ostream() << "("; | |
6485 | ast_dump_context->dump_expression(this->expr_); | |
6486 | if (this->expr_type_ != NULL) | |
6487 | { | |
6488 | ast_dump_context->ostream() << ":"; | |
6489 | ast_dump_context->dump_type(this->expr_type_); | |
6490 | ast_dump_context->ostream() << ")"; | |
6491 | } | |
6492 | ||
e0659c9e | 6493 | ast_dump_context->ostream() << "." << this->method_->name(); |
d751bb78 | 6494 | } |
6495 | ||
e440a328 | 6496 | // Make a method expression. |
6497 | ||
6498 | Bound_method_expression* | |
e0659c9e | 6499 | Expression::make_bound_method(Expression* expr, Named_object* method, |
b13c66cd | 6500 | Location location) |
e440a328 | 6501 | { |
6502 | return new Bound_method_expression(expr, method, location); | |
6503 | } | |
6504 | ||
6505 | // Class Builtin_call_expression. This is used for a call to a | |
6506 | // builtin function. | |
6507 | ||
6508 | class Builtin_call_expression : public Call_expression | |
6509 | { | |
6510 | public: | |
6511 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6512 | bool is_varargs, Location location); |
e440a328 | 6513 | |
6514 | protected: | |
6515 | // This overrides Call_expression::do_lower. | |
6516 | Expression* | |
ceeb4318 | 6517 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 6518 | |
6519 | bool | |
6520 | do_is_constant() const; | |
6521 | ||
6522 | bool | |
0c77715b | 6523 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 6524 | |
a7549a6a | 6525 | void |
6526 | do_discarding_value(); | |
6527 | ||
e440a328 | 6528 | Type* |
6529 | do_type(); | |
6530 | ||
6531 | void | |
6532 | do_determine_type(const Type_context*); | |
6533 | ||
6534 | void | |
6535 | do_check_types(Gogo*); | |
6536 | ||
6537 | Expression* | |
6538 | do_copy() | |
6539 | { | |
6540 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6541 | this->args()->copy(), | |
6542 | this->is_varargs(), | |
6543 | this->location()); | |
6544 | } | |
6545 | ||
6546 | tree | |
6547 | do_get_tree(Translate_context*); | |
6548 | ||
6549 | void | |
6550 | do_export(Export*) const; | |
6551 | ||
6552 | virtual bool | |
6553 | do_is_recover_call() const; | |
6554 | ||
6555 | virtual void | |
6556 | do_set_recover_arg(Expression*); | |
6557 | ||
6558 | private: | |
6559 | // The builtin functions. | |
6560 | enum Builtin_function_code | |
6561 | { | |
6562 | BUILTIN_INVALID, | |
6563 | ||
6564 | // Predeclared builtin functions. | |
6565 | BUILTIN_APPEND, | |
6566 | BUILTIN_CAP, | |
6567 | BUILTIN_CLOSE, | |
48080209 | 6568 | BUILTIN_COMPLEX, |
e440a328 | 6569 | BUILTIN_COPY, |
1cce762f | 6570 | BUILTIN_DELETE, |
e440a328 | 6571 | BUILTIN_IMAG, |
6572 | BUILTIN_LEN, | |
6573 | BUILTIN_MAKE, | |
6574 | BUILTIN_NEW, | |
6575 | BUILTIN_PANIC, | |
6576 | BUILTIN_PRINT, | |
6577 | BUILTIN_PRINTLN, | |
6578 | BUILTIN_REAL, | |
6579 | BUILTIN_RECOVER, | |
6580 | ||
6581 | // Builtin functions from the unsafe package. | |
6582 | BUILTIN_ALIGNOF, | |
6583 | BUILTIN_OFFSETOF, | |
6584 | BUILTIN_SIZEOF | |
6585 | }; | |
6586 | ||
6587 | Expression* | |
6588 | one_arg() const; | |
6589 | ||
6590 | bool | |
6591 | check_one_arg(); | |
6592 | ||
6593 | static Type* | |
6594 | real_imag_type(Type*); | |
6595 | ||
6596 | static Type* | |
48080209 | 6597 | complex_type(Type*); |
e440a328 | 6598 | |
a9182619 | 6599 | Expression* |
6600 | lower_make(); | |
6601 | ||
6602 | bool | |
6603 | check_int_value(Expression*); | |
6604 | ||
e440a328 | 6605 | // A pointer back to the general IR structure. This avoids a global |
6606 | // variable, or passing it around everywhere. | |
6607 | Gogo* gogo_; | |
6608 | // The builtin function being called. | |
6609 | Builtin_function_code code_; | |
0f914071 | 6610 | // Used to stop endless loops when the length of an array uses len |
6611 | // or cap of the array itself. | |
6612 | mutable bool seen_; | |
e440a328 | 6613 | }; |
6614 | ||
6615 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6616 | Expression* fn, | |
6617 | Expression_list* args, | |
6618 | bool is_varargs, | |
b13c66cd | 6619 | Location location) |
e440a328 | 6620 | : Call_expression(fn, args, is_varargs, location), |
0f914071 | 6621 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 6622 | { |
6623 | Func_expression* fnexp = this->fn()->func_expression(); | |
c484d925 | 6624 | go_assert(fnexp != NULL); |
e440a328 | 6625 | const std::string& name(fnexp->named_object()->name()); |
6626 | if (name == "append") | |
6627 | this->code_ = BUILTIN_APPEND; | |
6628 | else if (name == "cap") | |
6629 | this->code_ = BUILTIN_CAP; | |
6630 | else if (name == "close") | |
6631 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6632 | else if (name == "complex") |
6633 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6634 | else if (name == "copy") |
6635 | this->code_ = BUILTIN_COPY; | |
1cce762f | 6636 | else if (name == "delete") |
6637 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 6638 | else if (name == "imag") |
6639 | this->code_ = BUILTIN_IMAG; | |
6640 | else if (name == "len") | |
6641 | this->code_ = BUILTIN_LEN; | |
6642 | else if (name == "make") | |
6643 | this->code_ = BUILTIN_MAKE; | |
6644 | else if (name == "new") | |
6645 | this->code_ = BUILTIN_NEW; | |
6646 | else if (name == "panic") | |
6647 | this->code_ = BUILTIN_PANIC; | |
6648 | else if (name == "print") | |
6649 | this->code_ = BUILTIN_PRINT; | |
6650 | else if (name == "println") | |
6651 | this->code_ = BUILTIN_PRINTLN; | |
6652 | else if (name == "real") | |
6653 | this->code_ = BUILTIN_REAL; | |
6654 | else if (name == "recover") | |
6655 | this->code_ = BUILTIN_RECOVER; | |
6656 | else if (name == "Alignof") | |
6657 | this->code_ = BUILTIN_ALIGNOF; | |
6658 | else if (name == "Offsetof") | |
6659 | this->code_ = BUILTIN_OFFSETOF; | |
6660 | else if (name == "Sizeof") | |
6661 | this->code_ = BUILTIN_SIZEOF; | |
6662 | else | |
c3e6f413 | 6663 | go_unreachable(); |
e440a328 | 6664 | } |
6665 | ||
6666 | // Return whether this is a call to recover. This is a virtual | |
6667 | // function called from the parent class. | |
6668 | ||
6669 | bool | |
6670 | Builtin_call_expression::do_is_recover_call() const | |
6671 | { | |
6672 | if (this->classification() == EXPRESSION_ERROR) | |
6673 | return false; | |
6674 | return this->code_ == BUILTIN_RECOVER; | |
6675 | } | |
6676 | ||
6677 | // Set the argument for a call to recover. | |
6678 | ||
6679 | void | |
6680 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6681 | { | |
6682 | const Expression_list* args = this->args(); | |
c484d925 | 6683 | go_assert(args == NULL || args->empty()); |
e440a328 | 6684 | Expression_list* new_args = new Expression_list(); |
6685 | new_args->push_back(arg); | |
6686 | this->set_args(new_args); | |
6687 | } | |
6688 | ||
e440a328 | 6689 | // Lower a builtin call expression. This turns new and make into |
6690 | // specific expressions. We also convert to a constant if we can. | |
6691 | ||
6692 | Expression* | |
ceeb4318 | 6693 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
6694 | Statement_inserter* inserter, int) | |
e440a328 | 6695 | { |
a9182619 | 6696 | if (this->classification() == EXPRESSION_ERROR) |
6697 | return this; | |
6698 | ||
b13c66cd | 6699 | Location loc = this->location(); |
1cce762f | 6700 | |
a8725655 | 6701 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
6702 | { | |
6703 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 6704 | return Expression::make_error(loc); |
a8725655 | 6705 | } |
6706 | ||
1cce762f | 6707 | if (this->is_constant()) |
e440a328 | 6708 | { |
0c77715b | 6709 | Numeric_constant nc; |
6710 | if (this->numeric_constant_value(&nc)) | |
6711 | return nc.expression(loc); | |
e440a328 | 6712 | } |
1cce762f | 6713 | |
6714 | switch (this->code_) | |
e440a328 | 6715 | { |
1cce762f | 6716 | default: |
6717 | break; | |
6718 | ||
6719 | case BUILTIN_NEW: | |
6720 | { | |
6721 | const Expression_list* args = this->args(); | |
6722 | if (args == NULL || args->size() < 1) | |
6723 | this->report_error(_("not enough arguments")); | |
6724 | else if (args->size() > 1) | |
6725 | this->report_error(_("too many arguments")); | |
6726 | else | |
6727 | { | |
6728 | Expression* arg = args->front(); | |
6729 | if (!arg->is_type_expression()) | |
6730 | { | |
6731 | error_at(arg->location(), "expected type"); | |
6732 | this->set_is_error(); | |
6733 | } | |
6734 | else | |
6735 | return Expression::make_allocation(arg->type(), loc); | |
6736 | } | |
6737 | } | |
6738 | break; | |
6739 | ||
6740 | case BUILTIN_MAKE: | |
6741 | return this->lower_make(); | |
6742 | ||
6743 | case BUILTIN_RECOVER: | |
e440a328 | 6744 | if (function != NULL) |
6745 | function->func_value()->set_calls_recover(); | |
6746 | else | |
6747 | { | |
6748 | // Calling recover outside of a function always returns the | |
6749 | // nil empty interface. | |
823c7e3d | 6750 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 6751 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 6752 | } |
1cce762f | 6753 | break; |
6754 | ||
6755 | case BUILTIN_APPEND: | |
6756 | { | |
6757 | // Lower the varargs. | |
6758 | const Expression_list* args = this->args(); | |
6759 | if (args == NULL || args->empty()) | |
e440a328 | 6760 | return this; |
1cce762f | 6761 | Type* slice_type = args->front()->type(); |
6762 | if (!slice_type->is_slice_type()) | |
6763 | { | |
6764 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
6765 | this->set_is_error(); | |
6766 | return this; | |
6767 | } | |
19fd40c3 | 6768 | Type* element_type = slice_type->array_type()->element_type(); |
6769 | this->lower_varargs(gogo, function, inserter, | |
6770 | Type::make_array_type(element_type, NULL), | |
6771 | 2); | |
1cce762f | 6772 | } |
6773 | break; | |
6774 | ||
6775 | case BUILTIN_DELETE: | |
6776 | { | |
6777 | // Lower to a runtime function call. | |
6778 | const Expression_list* args = this->args(); | |
6779 | if (args == NULL || args->size() < 2) | |
6780 | this->report_error(_("not enough arguments")); | |
6781 | else if (args->size() > 2) | |
6782 | this->report_error(_("too many arguments")); | |
6783 | else if (args->front()->type()->map_type() == NULL) | |
6784 | this->report_error(_("argument 1 must be a map")); | |
6785 | else | |
6786 | { | |
6787 | // Since this function returns no value it must appear in | |
6788 | // a statement by itself, so we don't have to worry about | |
6789 | // order of evaluation of values around it. Evaluate the | |
6790 | // map first to get order of evaluation right. | |
6791 | Map_type* mt = args->front()->type()->map_type(); | |
6792 | Temporary_statement* map_temp = | |
6793 | Statement::make_temporary(mt, args->front(), loc); | |
6794 | inserter->insert(map_temp); | |
6795 | ||
6796 | Temporary_statement* key_temp = | |
6797 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
6798 | inserter->insert(key_temp); | |
6799 | ||
6800 | Expression* e1 = Expression::make_temporary_reference(map_temp, | |
6801 | loc); | |
6802 | Expression* e2 = Expression::make_temporary_reference(key_temp, | |
6803 | loc); | |
6804 | e2 = Expression::make_unary(OPERATOR_AND, e2, loc); | |
6805 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), | |
6806 | 2, e1, e2); | |
6807 | } | |
6808 | } | |
6809 | break; | |
e440a328 | 6810 | } |
6811 | ||
6812 | return this; | |
6813 | } | |
6814 | ||
a9182619 | 6815 | // Lower a make expression. |
6816 | ||
6817 | Expression* | |
6818 | Builtin_call_expression::lower_make() | |
6819 | { | |
b13c66cd | 6820 | Location loc = this->location(); |
a9182619 | 6821 | |
6822 | const Expression_list* args = this->args(); | |
6823 | if (args == NULL || args->size() < 1) | |
6824 | { | |
6825 | this->report_error(_("not enough arguments")); | |
6826 | return Expression::make_error(this->location()); | |
6827 | } | |
6828 | ||
6829 | Expression_list::const_iterator parg = args->begin(); | |
6830 | ||
6831 | Expression* first_arg = *parg; | |
6832 | if (!first_arg->is_type_expression()) | |
6833 | { | |
6834 | error_at(first_arg->location(), "expected type"); | |
6835 | this->set_is_error(); | |
6836 | return Expression::make_error(this->location()); | |
6837 | } | |
6838 | Type* type = first_arg->type(); | |
6839 | ||
6840 | bool is_slice = false; | |
6841 | bool is_map = false; | |
6842 | bool is_chan = false; | |
411eb89e | 6843 | if (type->is_slice_type()) |
a9182619 | 6844 | is_slice = true; |
6845 | else if (type->map_type() != NULL) | |
6846 | is_map = true; | |
6847 | else if (type->channel_type() != NULL) | |
6848 | is_chan = true; | |
6849 | else | |
6850 | { | |
6851 | this->report_error(_("invalid type for make function")); | |
6852 | return Expression::make_error(this->location()); | |
6853 | } | |
6854 | ||
ac84c822 | 6855 | bool have_big_args = false; |
6856 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
6857 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
6858 | ||
a9182619 | 6859 | ++parg; |
6860 | Expression* len_arg; | |
6861 | if (parg == args->end()) | |
6862 | { | |
6863 | if (is_slice) | |
6864 | { | |
6865 | this->report_error(_("length required when allocating a slice")); | |
6866 | return Expression::make_error(this->location()); | |
6867 | } | |
6868 | ||
6869 | mpz_t zval; | |
6870 | mpz_init_set_ui(zval, 0); | |
6871 | len_arg = Expression::make_integer(&zval, NULL, loc); | |
6872 | mpz_clear(zval); | |
6873 | } | |
6874 | else | |
6875 | { | |
6876 | len_arg = *parg; | |
6877 | if (!this->check_int_value(len_arg)) | |
6878 | { | |
6879 | this->report_error(_("bad size for make")); | |
6880 | return Expression::make_error(this->location()); | |
6881 | } | |
ac84c822 | 6882 | if (len_arg->type()->integer_type() != NULL |
6883 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
6884 | have_big_args = true; | |
a9182619 | 6885 | ++parg; |
6886 | } | |
6887 | ||
6888 | Expression* cap_arg = NULL; | |
6889 | if (is_slice && parg != args->end()) | |
6890 | { | |
6891 | cap_arg = *parg; | |
6892 | if (!this->check_int_value(cap_arg)) | |
6893 | { | |
6894 | this->report_error(_("bad capacity when making slice")); | |
6895 | return Expression::make_error(this->location()); | |
6896 | } | |
ac84c822 | 6897 | if (cap_arg->type()->integer_type() != NULL |
6898 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
6899 | have_big_args = true; | |
a9182619 | 6900 | ++parg; |
6901 | } | |
6902 | ||
6903 | if (parg != args->end()) | |
6904 | { | |
6905 | this->report_error(_("too many arguments to make")); | |
6906 | return Expression::make_error(this->location()); | |
6907 | } | |
6908 | ||
b13c66cd | 6909 | Location type_loc = first_arg->location(); |
a9182619 | 6910 | Expression* type_arg; |
6911 | if (is_slice || is_chan) | |
6912 | type_arg = Expression::make_type_descriptor(type, type_loc); | |
6913 | else if (is_map) | |
6914 | type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); | |
6915 | else | |
6916 | go_unreachable(); | |
6917 | ||
6918 | Expression* call; | |
6919 | if (is_slice) | |
6920 | { | |
6921 | if (cap_arg == NULL) | |
ac84c822 | 6922 | call = Runtime::make_call((have_big_args |
6923 | ? Runtime::MAKESLICE1BIG | |
6924 | : Runtime::MAKESLICE1), | |
6925 | loc, 2, type_arg, len_arg); | |
a9182619 | 6926 | else |
ac84c822 | 6927 | call = Runtime::make_call((have_big_args |
6928 | ? Runtime::MAKESLICE2BIG | |
6929 | : Runtime::MAKESLICE2), | |
6930 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 6931 | } |
6932 | else if (is_map) | |
ac84c822 | 6933 | call = Runtime::make_call((have_big_args |
6934 | ? Runtime::MAKEMAPBIG | |
6935 | : Runtime::MAKEMAP), | |
6936 | loc, 2, type_arg, len_arg); | |
a9182619 | 6937 | else if (is_chan) |
ac84c822 | 6938 | call = Runtime::make_call((have_big_args |
6939 | ? Runtime::MAKECHANBIG | |
6940 | : Runtime::MAKECHAN), | |
6941 | loc, 2, type_arg, len_arg); | |
a9182619 | 6942 | else |
6943 | go_unreachable(); | |
6944 | ||
6945 | return Expression::make_unsafe_cast(type, call, loc); | |
6946 | } | |
6947 | ||
6948 | // Return whether an expression has an integer value. Report an error | |
6949 | // if not. This is used when handling calls to the predeclared make | |
6950 | // function. | |
6951 | ||
6952 | bool | |
6953 | Builtin_call_expression::check_int_value(Expression* e) | |
6954 | { | |
6955 | if (e->type()->integer_type() != NULL) | |
6956 | return true; | |
6957 | ||
6958 | // Check for a floating point constant with integer value. | |
0c77715b | 6959 | Numeric_constant nc; |
6960 | mpz_t ival; | |
6961 | if (e->numeric_constant_value(&nc) && nc.to_int(&ival)) | |
a9182619 | 6962 | { |
a9182619 | 6963 | mpz_clear(ival); |
0c77715b | 6964 | return true; |
a9182619 | 6965 | } |
6966 | ||
a9182619 | 6967 | return false; |
6968 | } | |
6969 | ||
e440a328 | 6970 | // Return the type of the real or imag functions, given the type of |
6971 | // the argument. We need to map complex to float, complex64 to | |
6972 | // float32, and complex128 to float64, so it has to be done by name. | |
6973 | // This returns NULL if it can't figure out the type. | |
6974 | ||
6975 | Type* | |
6976 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
6977 | { | |
6978 | if (arg_type == NULL || arg_type->is_abstract()) | |
6979 | return NULL; | |
6980 | Named_type* nt = arg_type->named_type(); | |
6981 | if (nt == NULL) | |
6982 | return NULL; | |
6983 | while (nt->real_type()->named_type() != NULL) | |
6984 | nt = nt->real_type()->named_type(); | |
48080209 | 6985 | if (nt->name() == "complex64") |
e440a328 | 6986 | return Type::lookup_float_type("float32"); |
6987 | else if (nt->name() == "complex128") | |
6988 | return Type::lookup_float_type("float64"); | |
6989 | else | |
6990 | return NULL; | |
6991 | } | |
6992 | ||
48080209 | 6993 | // Return the type of the complex function, given the type of one of the |
e440a328 | 6994 | // argments. Like real_imag_type, we have to map by name. |
6995 | ||
6996 | Type* | |
48080209 | 6997 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 6998 | { |
6999 | if (arg_type == NULL || arg_type->is_abstract()) | |
7000 | return NULL; | |
7001 | Named_type* nt = arg_type->named_type(); | |
7002 | if (nt == NULL) | |
7003 | return NULL; | |
7004 | while (nt->real_type()->named_type() != NULL) | |
7005 | nt = nt->real_type()->named_type(); | |
48080209 | 7006 | if (nt->name() == "float32") |
e440a328 | 7007 | return Type::lookup_complex_type("complex64"); |
7008 | else if (nt->name() == "float64") | |
7009 | return Type::lookup_complex_type("complex128"); | |
7010 | else | |
7011 | return NULL; | |
7012 | } | |
7013 | ||
7014 | // Return a single argument, or NULL if there isn't one. | |
7015 | ||
7016 | Expression* | |
7017 | Builtin_call_expression::one_arg() const | |
7018 | { | |
7019 | const Expression_list* args = this->args(); | |
aa615cb3 | 7020 | if (args == NULL || args->size() != 1) |
e440a328 | 7021 | return NULL; |
7022 | return args->front(); | |
7023 | } | |
7024 | ||
83921647 | 7025 | // A traversal class which looks for a call or receive expression. |
7026 | ||
7027 | class Find_call_expression : public Traverse | |
7028 | { | |
7029 | public: | |
7030 | Find_call_expression() | |
7031 | : Traverse(traverse_expressions), | |
7032 | found_(false) | |
7033 | { } | |
7034 | ||
7035 | int | |
7036 | expression(Expression**); | |
7037 | ||
7038 | bool | |
7039 | found() | |
7040 | { return this->found_; } | |
7041 | ||
7042 | private: | |
7043 | bool found_; | |
7044 | }; | |
7045 | ||
7046 | int | |
7047 | Find_call_expression::expression(Expression** pexpr) | |
7048 | { | |
7049 | if ((*pexpr)->call_expression() != NULL | |
7050 | || (*pexpr)->receive_expression() != NULL) | |
7051 | { | |
7052 | this->found_ = true; | |
7053 | return TRAVERSE_EXIT; | |
7054 | } | |
7055 | return TRAVERSE_CONTINUE; | |
7056 | } | |
7057 | ||
7058 | // Return whether this is constant: len of a string constant, or len | |
7059 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7060 | // unsafe.Alignof. | |
e440a328 | 7061 | |
7062 | bool | |
7063 | Builtin_call_expression::do_is_constant() const | |
7064 | { | |
7065 | switch (this->code_) | |
7066 | { | |
7067 | case BUILTIN_LEN: | |
7068 | case BUILTIN_CAP: | |
7069 | { | |
0f914071 | 7070 | if (this->seen_) |
7071 | return false; | |
7072 | ||
e440a328 | 7073 | Expression* arg = this->one_arg(); |
7074 | if (arg == NULL) | |
7075 | return false; | |
7076 | Type* arg_type = arg->type(); | |
7077 | ||
7078 | if (arg_type->points_to() != NULL | |
7079 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7080 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7081 | arg_type = arg_type->points_to(); |
7082 | ||
83921647 | 7083 | // The len and cap functions are only constant if there are no |
7084 | // function calls or channel operations in the arguments. | |
7085 | // Otherwise we have to make the call. | |
7086 | if (!arg->is_constant()) | |
7087 | { | |
7088 | Find_call_expression find_call; | |
7089 | Expression::traverse(&arg, &find_call); | |
7090 | if (find_call.found()) | |
7091 | return false; | |
7092 | } | |
7093 | ||
e440a328 | 7094 | if (arg_type->array_type() != NULL |
7095 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7096 | return true; |
e440a328 | 7097 | |
7098 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7099 | { |
7100 | this->seen_ = true; | |
7101 | bool ret = arg->is_constant(); | |
7102 | this->seen_ = false; | |
7103 | return ret; | |
7104 | } | |
e440a328 | 7105 | } |
7106 | break; | |
7107 | ||
7108 | case BUILTIN_SIZEOF: | |
7109 | case BUILTIN_ALIGNOF: | |
7110 | return this->one_arg() != NULL; | |
7111 | ||
7112 | case BUILTIN_OFFSETOF: | |
7113 | { | |
7114 | Expression* arg = this->one_arg(); | |
7115 | if (arg == NULL) | |
7116 | return false; | |
7117 | return arg->field_reference_expression() != NULL; | |
7118 | } | |
7119 | ||
48080209 | 7120 | case BUILTIN_COMPLEX: |
e440a328 | 7121 | { |
7122 | const Expression_list* args = this->args(); | |
7123 | if (args != NULL && args->size() == 2) | |
7124 | return args->front()->is_constant() && args->back()->is_constant(); | |
7125 | } | |
7126 | break; | |
7127 | ||
7128 | case BUILTIN_REAL: | |
7129 | case BUILTIN_IMAG: | |
7130 | { | |
7131 | Expression* arg = this->one_arg(); | |
7132 | return arg != NULL && arg->is_constant(); | |
7133 | } | |
7134 | ||
7135 | default: | |
7136 | break; | |
7137 | } | |
7138 | ||
7139 | return false; | |
7140 | } | |
7141 | ||
0c77715b | 7142 | // Return a numeric constant if possible. |
e440a328 | 7143 | |
7144 | bool | |
0c77715b | 7145 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7146 | { |
7147 | if (this->code_ == BUILTIN_LEN | |
7148 | || this->code_ == BUILTIN_CAP) | |
7149 | { | |
7150 | Expression* arg = this->one_arg(); | |
7151 | if (arg == NULL) | |
7152 | return false; | |
7153 | Type* arg_type = arg->type(); | |
7154 | ||
7155 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7156 | { | |
7157 | std::string sval; | |
7158 | if (arg->string_constant_value(&sval)) | |
7159 | { | |
0c77715b | 7160 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7161 | sval.length()); | |
e440a328 | 7162 | return true; |
7163 | } | |
7164 | } | |
7165 | ||
7166 | if (arg_type->points_to() != NULL | |
7167 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7168 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7169 | arg_type = arg_type->points_to(); |
7170 | ||
7171 | if (arg_type->array_type() != NULL | |
7172 | && arg_type->array_type()->length() != NULL) | |
7173 | { | |
0f914071 | 7174 | if (this->seen_) |
7175 | return false; | |
e440a328 | 7176 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7177 | this->seen_ = true; |
0c77715b | 7178 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7179 | this->seen_ = false; |
7180 | if (r) | |
e440a328 | 7181 | { |
0c77715b | 7182 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7183 | this->location())) | |
7184 | r = false; | |
e440a328 | 7185 | } |
0c77715b | 7186 | return r; |
e440a328 | 7187 | } |
7188 | } | |
7189 | else if (this->code_ == BUILTIN_SIZEOF | |
7190 | || this->code_ == BUILTIN_ALIGNOF) | |
7191 | { | |
7192 | Expression* arg = this->one_arg(); | |
7193 | if (arg == NULL) | |
7194 | return false; | |
7195 | Type* arg_type = arg->type(); | |
5c13bd80 | 7196 | if (arg_type->is_error()) |
e440a328 | 7197 | return false; |
7198 | if (arg_type->is_abstract()) | |
7199 | return false; | |
9aa9e2df | 7200 | if (arg_type->named_type() != NULL) |
7201 | arg_type->named_type()->convert(this->gogo_); | |
927a01eb | 7202 | |
7203 | unsigned int ret; | |
e440a328 | 7204 | if (this->code_ == BUILTIN_SIZEOF) |
7205 | { | |
927a01eb | 7206 | if (!arg_type->backend_type_size(this->gogo_, &ret)) |
e440a328 | 7207 | return false; |
7208 | } | |
7209 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7210 | { | |
637bd3af | 7211 | if (arg->field_reference_expression() == NULL) |
927a01eb | 7212 | { |
7213 | if (!arg_type->backend_type_align(this->gogo_, &ret)) | |
7214 | return false; | |
7215 | } | |
637bd3af | 7216 | else |
e440a328 | 7217 | { |
7218 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7219 | // the type of f when it is used as a field in a struct. | |
927a01eb | 7220 | if (!arg_type->backend_type_field_align(this->gogo_, &ret)) |
7221 | return false; | |
e440a328 | 7222 | } |
e440a328 | 7223 | } |
7224 | else | |
c3e6f413 | 7225 | go_unreachable(); |
927a01eb | 7226 | |
7ba86326 | 7227 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
7228 | static_cast<unsigned long>(ret)); | |
e440a328 | 7229 | return true; |
7230 | } | |
7231 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7232 | { | |
7233 | Expression* arg = this->one_arg(); | |
7234 | if (arg == NULL) | |
7235 | return false; | |
7236 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7237 | if (farg == NULL) | |
7238 | return false; | |
7239 | Expression* struct_expr = farg->expr(); | |
7240 | Type* st = struct_expr->type(); | |
7241 | if (st->struct_type() == NULL) | |
7242 | return false; | |
9aa9e2df | 7243 | if (st->named_type() != NULL) |
7244 | st->named_type()->convert(this->gogo_); | |
927a01eb | 7245 | unsigned int offset; |
7246 | if (!st->struct_type()->backend_field_offset(this->gogo_, | |
7247 | farg->field_index(), | |
7248 | &offset)) | |
e440a328 | 7249 | return false; |
7ba86326 | 7250 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
7251 | static_cast<unsigned long>(offset)); | |
e440a328 | 7252 | return true; |
7253 | } | |
0c77715b | 7254 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7255 | { |
7256 | Expression* arg = this->one_arg(); | |
7257 | if (arg == NULL) | |
7258 | return false; | |
7259 | ||
0c77715b | 7260 | Numeric_constant argnc; |
7261 | if (!arg->numeric_constant_value(&argnc)) | |
7262 | return false; | |
7263 | ||
e440a328 | 7264 | mpfr_t real; |
7265 | mpfr_t imag; | |
0c77715b | 7266 | if (!argnc.to_complex(&real, &imag)) |
7267 | return false; | |
e440a328 | 7268 | |
0c77715b | 7269 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7270 | if (this->code_ == BUILTIN_REAL) | |
7271 | nc->set_float(type, real); | |
7272 | else | |
7273 | nc->set_float(type, imag); | |
7274 | return true; | |
e440a328 | 7275 | } |
0c77715b | 7276 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7277 | { |
7278 | const Expression_list* args = this->args(); | |
7279 | if (args == NULL || args->size() != 2) | |
7280 | return false; | |
7281 | ||
0c77715b | 7282 | Numeric_constant rnc; |
7283 | if (!args->front()->numeric_constant_value(&rnc)) | |
7284 | return false; | |
7285 | Numeric_constant inc; | |
7286 | if (!args->back()->numeric_constant_value(&inc)) | |
7287 | return false; | |
7288 | ||
7289 | if (rnc.type() != NULL | |
7290 | && !rnc.type()->is_abstract() | |
7291 | && inc.type() != NULL | |
7292 | && !inc.type()->is_abstract() | |
7293 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7294 | return false; | |
7295 | ||
e440a328 | 7296 | mpfr_t r; |
0c77715b | 7297 | if (!rnc.to_float(&r)) |
7298 | return false; | |
7299 | mpfr_t i; | |
7300 | if (!inc.to_float(&i)) | |
e440a328 | 7301 | { |
7302 | mpfr_clear(r); | |
7303 | return false; | |
7304 | } | |
7305 | ||
0c77715b | 7306 | Type* arg_type = rnc.type(); |
7307 | if (arg_type == NULL || arg_type->is_abstract()) | |
7308 | arg_type = inc.type(); | |
e440a328 | 7309 | |
0c77715b | 7310 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7311 | nc->set_complex(type, r, i); | |
e440a328 | 7312 | |
7313 | mpfr_clear(r); | |
7314 | mpfr_clear(i); | |
7315 | ||
0c77715b | 7316 | return true; |
e440a328 | 7317 | } |
7318 | ||
7319 | return false; | |
7320 | } | |
7321 | ||
a7549a6a | 7322 | // Give an error if we are discarding the value of an expression which |
7323 | // should not normally be discarded. We don't give an error for | |
7324 | // discarding the value of an ordinary function call, but we do for | |
7325 | // builtin functions, purely for consistency with the gc compiler. | |
7326 | ||
7327 | void | |
7328 | Builtin_call_expression::do_discarding_value() | |
7329 | { | |
7330 | switch (this->code_) | |
7331 | { | |
7332 | case BUILTIN_INVALID: | |
7333 | default: | |
7334 | go_unreachable(); | |
7335 | ||
7336 | case BUILTIN_APPEND: | |
7337 | case BUILTIN_CAP: | |
7338 | case BUILTIN_COMPLEX: | |
7339 | case BUILTIN_IMAG: | |
7340 | case BUILTIN_LEN: | |
7341 | case BUILTIN_MAKE: | |
7342 | case BUILTIN_NEW: | |
7343 | case BUILTIN_REAL: | |
7344 | case BUILTIN_ALIGNOF: | |
7345 | case BUILTIN_OFFSETOF: | |
7346 | case BUILTIN_SIZEOF: | |
7347 | this->unused_value_error(); | |
7348 | break; | |
7349 | ||
7350 | case BUILTIN_CLOSE: | |
7351 | case BUILTIN_COPY: | |
1cce762f | 7352 | case BUILTIN_DELETE: |
a7549a6a | 7353 | case BUILTIN_PANIC: |
7354 | case BUILTIN_PRINT: | |
7355 | case BUILTIN_PRINTLN: | |
7356 | case BUILTIN_RECOVER: | |
7357 | break; | |
7358 | } | |
7359 | } | |
7360 | ||
e440a328 | 7361 | // Return the type. |
7362 | ||
7363 | Type* | |
7364 | Builtin_call_expression::do_type() | |
7365 | { | |
7366 | switch (this->code_) | |
7367 | { | |
7368 | case BUILTIN_INVALID: | |
7369 | default: | |
c3e6f413 | 7370 | go_unreachable(); |
e440a328 | 7371 | |
7372 | case BUILTIN_NEW: | |
7373 | case BUILTIN_MAKE: | |
7374 | { | |
7375 | const Expression_list* args = this->args(); | |
7376 | if (args == NULL || args->empty()) | |
7377 | return Type::make_error_type(); | |
7378 | return Type::make_pointer_type(args->front()->type()); | |
7379 | } | |
7380 | ||
7381 | case BUILTIN_CAP: | |
7382 | case BUILTIN_COPY: | |
7383 | case BUILTIN_LEN: | |
7ba86326 | 7384 | return Type::lookup_integer_type("int"); |
7385 | ||
e440a328 | 7386 | case BUILTIN_ALIGNOF: |
7387 | case BUILTIN_OFFSETOF: | |
7388 | case BUILTIN_SIZEOF: | |
7ba86326 | 7389 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7390 | |
7391 | case BUILTIN_CLOSE: | |
1cce762f | 7392 | case BUILTIN_DELETE: |
e440a328 | 7393 | case BUILTIN_PANIC: |
7394 | case BUILTIN_PRINT: | |
7395 | case BUILTIN_PRINTLN: | |
7396 | return Type::make_void_type(); | |
7397 | ||
e440a328 | 7398 | case BUILTIN_RECOVER: |
823c7e3d | 7399 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7400 | |
7401 | case BUILTIN_APPEND: | |
7402 | { | |
7403 | const Expression_list* args = this->args(); | |
7404 | if (args == NULL || args->empty()) | |
7405 | return Type::make_error_type(); | |
7406 | return args->front()->type(); | |
7407 | } | |
7408 | ||
7409 | case BUILTIN_REAL: | |
7410 | case BUILTIN_IMAG: | |
7411 | { | |
7412 | Expression* arg = this->one_arg(); | |
7413 | if (arg == NULL) | |
7414 | return Type::make_error_type(); | |
7415 | Type* t = arg->type(); | |
7416 | if (t->is_abstract()) | |
7417 | t = t->make_non_abstract_type(); | |
7418 | t = Builtin_call_expression::real_imag_type(t); | |
7419 | if (t == NULL) | |
7420 | t = Type::make_error_type(); | |
7421 | return t; | |
7422 | } | |
7423 | ||
48080209 | 7424 | case BUILTIN_COMPLEX: |
e440a328 | 7425 | { |
7426 | const Expression_list* args = this->args(); | |
7427 | if (args == NULL || args->size() != 2) | |
7428 | return Type::make_error_type(); | |
7429 | Type* t = args->front()->type(); | |
7430 | if (t->is_abstract()) | |
7431 | { | |
7432 | t = args->back()->type(); | |
7433 | if (t->is_abstract()) | |
7434 | t = t->make_non_abstract_type(); | |
7435 | } | |
48080209 | 7436 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7437 | if (t == NULL) |
7438 | t = Type::make_error_type(); | |
7439 | return t; | |
7440 | } | |
7441 | } | |
7442 | } | |
7443 | ||
7444 | // Determine the type. | |
7445 | ||
7446 | void | |
7447 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7448 | { | |
fb94b0ca | 7449 | if (!this->determining_types()) |
7450 | return; | |
7451 | ||
e440a328 | 7452 | this->fn()->determine_type_no_context(); |
7453 | ||
7454 | const Expression_list* args = this->args(); | |
7455 | ||
7456 | bool is_print; | |
7457 | Type* arg_type = NULL; | |
7458 | switch (this->code_) | |
7459 | { | |
7460 | case BUILTIN_PRINT: | |
7461 | case BUILTIN_PRINTLN: | |
7462 | // Do not force a large integer constant to "int". | |
7463 | is_print = true; | |
7464 | break; | |
7465 | ||
7466 | case BUILTIN_REAL: | |
7467 | case BUILTIN_IMAG: | |
48080209 | 7468 | arg_type = Builtin_call_expression::complex_type(context->type); |
e440a328 | 7469 | is_print = false; |
7470 | break; | |
7471 | ||
48080209 | 7472 | case BUILTIN_COMPLEX: |
e440a328 | 7473 | { |
48080209 | 7474 | // For the complex function the type of one operand can |
e440a328 | 7475 | // determine the type of the other, as in a binary expression. |
7476 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
7477 | if (args != NULL && args->size() == 2) | |
7478 | { | |
7479 | Type* t1 = args->front()->type(); | |
c849bb59 | 7480 | Type* t2 = args->back()->type(); |
e440a328 | 7481 | if (!t1->is_abstract()) |
7482 | arg_type = t1; | |
7483 | else if (!t2->is_abstract()) | |
7484 | arg_type = t2; | |
7485 | } | |
7486 | is_print = false; | |
7487 | } | |
7488 | break; | |
7489 | ||
7490 | default: | |
7491 | is_print = false; | |
7492 | break; | |
7493 | } | |
7494 | ||
7495 | if (args != NULL) | |
7496 | { | |
7497 | for (Expression_list::const_iterator pa = args->begin(); | |
7498 | pa != args->end(); | |
7499 | ++pa) | |
7500 | { | |
7501 | Type_context subcontext; | |
7502 | subcontext.type = arg_type; | |
7503 | ||
7504 | if (is_print) | |
7505 | { | |
7506 | // We want to print large constants, we so can't just | |
7507 | // use the appropriate nonabstract type. Use uint64 for | |
7508 | // an integer if we know it is nonnegative, otherwise | |
7509 | // use int64 for a integer, otherwise use float64 for a | |
7510 | // float or complex128 for a complex. | |
7511 | Type* want_type = NULL; | |
7512 | Type* atype = (*pa)->type(); | |
7513 | if (atype->is_abstract()) | |
7514 | { | |
7515 | if (atype->integer_type() != NULL) | |
7516 | { | |
0c77715b | 7517 | Numeric_constant nc; |
7518 | if (this->numeric_constant_value(&nc)) | |
7519 | { | |
7520 | mpz_t val; | |
7521 | if (nc.to_int(&val)) | |
7522 | { | |
7523 | if (mpz_sgn(val) >= 0) | |
7524 | want_type = Type::lookup_integer_type("uint64"); | |
7525 | mpz_clear(val); | |
7526 | } | |
7527 | } | |
7528 | if (want_type == NULL) | |
e440a328 | 7529 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 7530 | } |
7531 | else if (atype->float_type() != NULL) | |
7532 | want_type = Type::lookup_float_type("float64"); | |
7533 | else if (atype->complex_type() != NULL) | |
7534 | want_type = Type::lookup_complex_type("complex128"); | |
7535 | else if (atype->is_abstract_string_type()) | |
7536 | want_type = Type::lookup_string_type(); | |
7537 | else if (atype->is_abstract_boolean_type()) | |
7538 | want_type = Type::lookup_bool_type(); | |
7539 | else | |
c3e6f413 | 7540 | go_unreachable(); |
e440a328 | 7541 | subcontext.type = want_type; |
7542 | } | |
7543 | } | |
7544 | ||
7545 | (*pa)->determine_type(&subcontext); | |
7546 | } | |
7547 | } | |
7548 | } | |
7549 | ||
7550 | // If there is exactly one argument, return true. Otherwise give an | |
7551 | // error message and return false. | |
7552 | ||
7553 | bool | |
7554 | Builtin_call_expression::check_one_arg() | |
7555 | { | |
7556 | const Expression_list* args = this->args(); | |
7557 | if (args == NULL || args->size() < 1) | |
7558 | { | |
7559 | this->report_error(_("not enough arguments")); | |
7560 | return false; | |
7561 | } | |
7562 | else if (args->size() > 1) | |
7563 | { | |
7564 | this->report_error(_("too many arguments")); | |
7565 | return false; | |
7566 | } | |
7567 | if (args->front()->is_error_expression() | |
5c13bd80 | 7568 | || args->front()->type()->is_error()) |
e440a328 | 7569 | { |
7570 | this->set_is_error(); | |
7571 | return false; | |
7572 | } | |
7573 | return true; | |
7574 | } | |
7575 | ||
7576 | // Check argument types for a builtin function. | |
7577 | ||
7578 | void | |
7579 | Builtin_call_expression::do_check_types(Gogo*) | |
7580 | { | |
375646ea | 7581 | if (this->is_error_expression()) |
7582 | return; | |
e440a328 | 7583 | switch (this->code_) |
7584 | { | |
7585 | case BUILTIN_INVALID: | |
7586 | case BUILTIN_NEW: | |
7587 | case BUILTIN_MAKE: | |
cd238b8d | 7588 | case BUILTIN_DELETE: |
e440a328 | 7589 | return; |
7590 | ||
7591 | case BUILTIN_LEN: | |
7592 | case BUILTIN_CAP: | |
7593 | { | |
7594 | // The single argument may be either a string or an array or a | |
7595 | // map or a channel, or a pointer to a closed array. | |
7596 | if (this->check_one_arg()) | |
7597 | { | |
7598 | Type* arg_type = this->one_arg()->type(); | |
7599 | if (arg_type->points_to() != NULL | |
7600 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7601 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7602 | arg_type = arg_type->points_to(); |
7603 | if (this->code_ == BUILTIN_CAP) | |
7604 | { | |
5c13bd80 | 7605 | if (!arg_type->is_error() |
e440a328 | 7606 | && arg_type->array_type() == NULL |
7607 | && arg_type->channel_type() == NULL) | |
7608 | this->report_error(_("argument must be array or slice " | |
7609 | "or channel")); | |
7610 | } | |
7611 | else | |
7612 | { | |
5c13bd80 | 7613 | if (!arg_type->is_error() |
e440a328 | 7614 | && !arg_type->is_string_type() |
7615 | && arg_type->array_type() == NULL | |
7616 | && arg_type->map_type() == NULL | |
7617 | && arg_type->channel_type() == NULL) | |
7618 | this->report_error(_("argument must be string or " | |
7619 | "array or slice or map or channel")); | |
7620 | } | |
7621 | } | |
7622 | } | |
7623 | break; | |
7624 | ||
7625 | case BUILTIN_PRINT: | |
7626 | case BUILTIN_PRINTLN: | |
7627 | { | |
7628 | const Expression_list* args = this->args(); | |
7629 | if (args == NULL) | |
7630 | { | |
7631 | if (this->code_ == BUILTIN_PRINT) | |
7632 | warning_at(this->location(), 0, | |
7633 | "no arguments for builtin function %<%s%>", | |
7634 | (this->code_ == BUILTIN_PRINT | |
7635 | ? "print" | |
7636 | : "println")); | |
7637 | } | |
7638 | else | |
7639 | { | |
7640 | for (Expression_list::const_iterator p = args->begin(); | |
7641 | p != args->end(); | |
7642 | ++p) | |
7643 | { | |
7644 | Type* type = (*p)->type(); | |
5c13bd80 | 7645 | if (type->is_error() |
e440a328 | 7646 | || type->is_string_type() |
7647 | || type->integer_type() != NULL | |
7648 | || type->float_type() != NULL | |
7649 | || type->complex_type() != NULL | |
7650 | || type->is_boolean_type() | |
7651 | || type->points_to() != NULL | |
7652 | || type->interface_type() != NULL | |
7653 | || type->channel_type() != NULL | |
7654 | || type->map_type() != NULL | |
7655 | || type->function_type() != NULL | |
411eb89e | 7656 | || type->is_slice_type()) |
e440a328 | 7657 | ; |
acf8e158 | 7658 | else if ((*p)->is_type_expression()) |
7659 | { | |
7660 | // If this is a type expression it's going to give | |
7661 | // an error anyhow, so we don't need one here. | |
7662 | } | |
e440a328 | 7663 | else |
7664 | this->report_error(_("unsupported argument type to " | |
7665 | "builtin function")); | |
7666 | } | |
7667 | } | |
7668 | } | |
7669 | break; | |
7670 | ||
7671 | case BUILTIN_CLOSE: | |
e440a328 | 7672 | if (this->check_one_arg()) |
7673 | { | |
7674 | if (this->one_arg()->type()->channel_type() == NULL) | |
7675 | this->report_error(_("argument must be channel")); | |
5202d986 | 7676 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
7677 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 7678 | } |
7679 | break; | |
7680 | ||
7681 | case BUILTIN_PANIC: | |
7682 | case BUILTIN_SIZEOF: | |
7683 | case BUILTIN_ALIGNOF: | |
7684 | this->check_one_arg(); | |
7685 | break; | |
7686 | ||
7687 | case BUILTIN_RECOVER: | |
7688 | if (this->args() != NULL && !this->args()->empty()) | |
7689 | this->report_error(_("too many arguments")); | |
7690 | break; | |
7691 | ||
7692 | case BUILTIN_OFFSETOF: | |
7693 | if (this->check_one_arg()) | |
7694 | { | |
7695 | Expression* arg = this->one_arg(); | |
7696 | if (arg->field_reference_expression() == NULL) | |
7697 | this->report_error(_("argument must be a field reference")); | |
7698 | } | |
7699 | break; | |
7700 | ||
7701 | case BUILTIN_COPY: | |
7702 | { | |
7703 | const Expression_list* args = this->args(); | |
7704 | if (args == NULL || args->size() < 2) | |
7705 | { | |
7706 | this->report_error(_("not enough arguments")); | |
7707 | break; | |
7708 | } | |
7709 | else if (args->size() > 2) | |
7710 | { | |
7711 | this->report_error(_("too many arguments")); | |
7712 | break; | |
7713 | } | |
7714 | Type* arg1_type = args->front()->type(); | |
7715 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 7716 | if (arg1_type->is_error() || arg2_type->is_error()) |
e440a328 | 7717 | break; |
7718 | ||
7719 | Type* e1; | |
411eb89e | 7720 | if (arg1_type->is_slice_type()) |
e440a328 | 7721 | e1 = arg1_type->array_type()->element_type(); |
7722 | else | |
7723 | { | |
7724 | this->report_error(_("left argument must be a slice")); | |
7725 | break; | |
7726 | } | |
7727 | ||
411eb89e | 7728 | if (arg2_type->is_slice_type()) |
60963afd | 7729 | { |
7730 | Type* e2 = arg2_type->array_type()->element_type(); | |
7731 | if (!Type::are_identical(e1, e2, true, NULL)) | |
7732 | this->report_error(_("element types must be the same")); | |
7733 | } | |
e440a328 | 7734 | else if (arg2_type->is_string_type()) |
e440a328 | 7735 | { |
60963afd | 7736 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
7737 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 7738 | } |
60963afd | 7739 | else |
7740 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 7741 | } |
7742 | break; | |
7743 | ||
7744 | case BUILTIN_APPEND: | |
7745 | { | |
7746 | const Expression_list* args = this->args(); | |
b0d311a1 | 7747 | if (args == NULL || args->size() < 2) |
e440a328 | 7748 | { |
7749 | this->report_error(_("not enough arguments")); | |
7750 | break; | |
7751 | } | |
0b7755ec | 7752 | if (args->size() > 2) |
7753 | { | |
7754 | this->report_error(_("too many arguments")); | |
7755 | break; | |
7756 | } | |
cd238b8d | 7757 | if (args->front()->type()->is_error() |
7758 | || args->back()->type()->is_error()) | |
7759 | break; | |
7760 | ||
7761 | Array_type* at = args->front()->type()->array_type(); | |
7762 | Type* e = at->element_type(); | |
4fd4fcf4 | 7763 | |
7764 | // The language permits appending a string to a []byte, as a | |
7765 | // special case. | |
7766 | if (args->back()->type()->is_string_type()) | |
7767 | { | |
60963afd | 7768 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 7769 | break; |
7770 | } | |
7771 | ||
19fd40c3 | 7772 | // The language says that the second argument must be |
7773 | // assignable to a slice of the element type of the first | |
7774 | // argument. We already know the first argument is a slice | |
7775 | // type. | |
cd238b8d | 7776 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 7777 | std::string reason; |
19fd40c3 | 7778 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 7779 | { |
7780 | if (reason.empty()) | |
19fd40c3 | 7781 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 7782 | else |
7783 | { | |
19fd40c3 | 7784 | error_at(this->location(), "argument 2 has invalid type (%s)", |
e440a328 | 7785 | reason.c_str()); |
7786 | this->set_is_error(); | |
7787 | } | |
7788 | } | |
7789 | break; | |
7790 | } | |
7791 | ||
7792 | case BUILTIN_REAL: | |
7793 | case BUILTIN_IMAG: | |
7794 | if (this->check_one_arg()) | |
7795 | { | |
7796 | if (this->one_arg()->type()->complex_type() == NULL) | |
7797 | this->report_error(_("argument must have complex type")); | |
7798 | } | |
7799 | break; | |
7800 | ||
48080209 | 7801 | case BUILTIN_COMPLEX: |
e440a328 | 7802 | { |
7803 | const Expression_list* args = this->args(); | |
7804 | if (args == NULL || args->size() < 2) | |
7805 | this->report_error(_("not enough arguments")); | |
7806 | else if (args->size() > 2) | |
7807 | this->report_error(_("too many arguments")); | |
7808 | else if (args->front()->is_error_expression() | |
5c13bd80 | 7809 | || args->front()->type()->is_error() |
e440a328 | 7810 | || args->back()->is_error_expression() |
5c13bd80 | 7811 | || args->back()->type()->is_error()) |
e440a328 | 7812 | this->set_is_error(); |
7813 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 7814 | args->back()->type(), true, NULL)) |
48080209 | 7815 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 7816 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 7817 | this->report_error(_("complex arguments must have " |
e440a328 | 7818 | "floating-point type")); |
7819 | } | |
7820 | break; | |
7821 | ||
7822 | default: | |
c3e6f413 | 7823 | go_unreachable(); |
e440a328 | 7824 | } |
7825 | } | |
7826 | ||
7827 | // Return the tree for a builtin function. | |
7828 | ||
7829 | tree | |
7830 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
7831 | { | |
7832 | Gogo* gogo = context->gogo(); | |
b13c66cd | 7833 | Location location = this->location(); |
e440a328 | 7834 | switch (this->code_) |
7835 | { | |
7836 | case BUILTIN_INVALID: | |
7837 | case BUILTIN_NEW: | |
7838 | case BUILTIN_MAKE: | |
c3e6f413 | 7839 | go_unreachable(); |
e440a328 | 7840 | |
7841 | case BUILTIN_LEN: | |
7842 | case BUILTIN_CAP: | |
7843 | { | |
7844 | const Expression_list* args = this->args(); | |
c484d925 | 7845 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 7846 | Expression* arg = *args->begin(); |
7847 | Type* arg_type = arg->type(); | |
0f914071 | 7848 | |
7849 | if (this->seen_) | |
7850 | { | |
c484d925 | 7851 | go_assert(saw_errors()); |
0f914071 | 7852 | return error_mark_node; |
7853 | } | |
7854 | this->seen_ = true; | |
7855 | ||
e440a328 | 7856 | tree arg_tree = arg->get_tree(context); |
0f914071 | 7857 | |
7858 | this->seen_ = false; | |
7859 | ||
e440a328 | 7860 | if (arg_tree == error_mark_node) |
7861 | return error_mark_node; | |
7862 | ||
7863 | if (arg_type->points_to() != NULL) | |
7864 | { | |
7865 | arg_type = arg_type->points_to(); | |
c484d925 | 7866 | go_assert(arg_type->array_type() != NULL |
411eb89e | 7867 | && !arg_type->is_slice_type()); |
c484d925 | 7868 | go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); |
e440a328 | 7869 | arg_tree = build_fold_indirect_ref(arg_tree); |
7870 | } | |
7871 | ||
7872 | tree val_tree; | |
7873 | if (this->code_ == BUILTIN_LEN) | |
7874 | { | |
7875 | if (arg_type->is_string_type()) | |
7876 | val_tree = String_type::length_tree(gogo, arg_tree); | |
7877 | else if (arg_type->array_type() != NULL) | |
0f914071 | 7878 | { |
7879 | if (this->seen_) | |
7880 | { | |
c484d925 | 7881 | go_assert(saw_errors()); |
0f914071 | 7882 | return error_mark_node; |
7883 | } | |
7884 | this->seen_ = true; | |
7885 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
7886 | this->seen_ = false; | |
7887 | } | |
e440a328 | 7888 | else if (arg_type->map_type() != NULL) |
7889 | { | |
9f0e0513 | 7890 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 7891 | static tree map_len_fndecl; |
7892 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
7893 | location, | |
7894 | "__go_map_len", | |
7895 | 1, | |
9581e91d | 7896 | integer_type_node, |
9f0e0513 | 7897 | arg_type_tree, |
e440a328 | 7898 | arg_tree); |
7899 | } | |
7900 | else if (arg_type->channel_type() != NULL) | |
7901 | { | |
9f0e0513 | 7902 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 7903 | static tree chan_len_fndecl; |
7904 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
7905 | location, | |
7906 | "__go_chan_len", | |
7907 | 1, | |
9581e91d | 7908 | integer_type_node, |
9f0e0513 | 7909 | arg_type_tree, |
e440a328 | 7910 | arg_tree); |
7911 | } | |
7912 | else | |
c3e6f413 | 7913 | go_unreachable(); |
e440a328 | 7914 | } |
7915 | else | |
7916 | { | |
7917 | if (arg_type->array_type() != NULL) | |
0f914071 | 7918 | { |
7919 | if (this->seen_) | |
7920 | { | |
c484d925 | 7921 | go_assert(saw_errors()); |
0f914071 | 7922 | return error_mark_node; |
7923 | } | |
7924 | this->seen_ = true; | |
7925 | val_tree = arg_type->array_type()->capacity_tree(gogo, | |
7926 | arg_tree); | |
7927 | this->seen_ = false; | |
7928 | } | |
e440a328 | 7929 | else if (arg_type->channel_type() != NULL) |
7930 | { | |
9f0e0513 | 7931 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 7932 | static tree chan_cap_fndecl; |
7933 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
7934 | location, | |
7935 | "__go_chan_cap", | |
7936 | 1, | |
9581e91d | 7937 | integer_type_node, |
9f0e0513 | 7938 | arg_type_tree, |
e440a328 | 7939 | arg_tree); |
7940 | } | |
7941 | else | |
c3e6f413 | 7942 | go_unreachable(); |
e440a328 | 7943 | } |
7944 | ||
d8ccb1e3 | 7945 | if (val_tree == error_mark_node) |
7946 | return error_mark_node; | |
7947 | ||
9f0e0513 | 7948 | Type* int_type = Type::lookup_integer_type("int"); |
7949 | tree type_tree = type_to_tree(int_type->get_backend(gogo)); | |
e440a328 | 7950 | if (type_tree == TREE_TYPE(val_tree)) |
7951 | return val_tree; | |
7952 | else | |
7953 | return fold(convert_to_integer(type_tree, val_tree)); | |
7954 | } | |
7955 | ||
7956 | case BUILTIN_PRINT: | |
7957 | case BUILTIN_PRINTLN: | |
7958 | { | |
7959 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
7960 | tree stmt_list = NULL_TREE; | |
7961 | ||
7962 | const Expression_list* call_args = this->args(); | |
7963 | if (call_args != NULL) | |
7964 | { | |
7965 | for (Expression_list::const_iterator p = call_args->begin(); | |
7966 | p != call_args->end(); | |
7967 | ++p) | |
7968 | { | |
7969 | if (is_ln && p != call_args->begin()) | |
7970 | { | |
7971 | static tree print_space_fndecl; | |
7972 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
7973 | location, | |
7974 | "__go_print_space", | |
7975 | 0, | |
7976 | void_type_node); | |
5fb82b5e | 7977 | if (call == error_mark_node) |
7978 | return error_mark_node; | |
e440a328 | 7979 | append_to_statement_list(call, &stmt_list); |
7980 | } | |
7981 | ||
7982 | Type* type = (*p)->type(); | |
7983 | ||
7984 | tree arg = (*p)->get_tree(context); | |
7985 | if (arg == error_mark_node) | |
7986 | return error_mark_node; | |
7987 | ||
7988 | tree* pfndecl; | |
7989 | const char* fnname; | |
7990 | if (type->is_string_type()) | |
7991 | { | |
7992 | static tree print_string_fndecl; | |
7993 | pfndecl = &print_string_fndecl; | |
7994 | fnname = "__go_print_string"; | |
7995 | } | |
7996 | else if (type->integer_type() != NULL | |
7997 | && type->integer_type()->is_unsigned()) | |
7998 | { | |
7999 | static tree print_uint64_fndecl; | |
8000 | pfndecl = &print_uint64_fndecl; | |
8001 | fnname = "__go_print_uint64"; | |
8002 | Type* itype = Type::lookup_integer_type("uint64"); | |
9f0e0513 | 8003 | Btype* bitype = itype->get_backend(gogo); |
b13c66cd | 8004 | arg = fold_convert_loc(location.gcc_location(), |
8005 | type_to_tree(bitype), arg); | |
e440a328 | 8006 | } |
8007 | else if (type->integer_type() != NULL) | |
8008 | { | |
8009 | static tree print_int64_fndecl; | |
8010 | pfndecl = &print_int64_fndecl; | |
8011 | fnname = "__go_print_int64"; | |
8012 | Type* itype = Type::lookup_integer_type("int64"); | |
9f0e0513 | 8013 | Btype* bitype = itype->get_backend(gogo); |
b13c66cd | 8014 | arg = fold_convert_loc(location.gcc_location(), |
8015 | type_to_tree(bitype), arg); | |
e440a328 | 8016 | } |
8017 | else if (type->float_type() != NULL) | |
8018 | { | |
8019 | static tree print_double_fndecl; | |
8020 | pfndecl = &print_double_fndecl; | |
8021 | fnname = "__go_print_double"; | |
b13c66cd | 8022 | arg = fold_convert_loc(location.gcc_location(), |
8023 | double_type_node, arg); | |
e440a328 | 8024 | } |
8025 | else if (type->complex_type() != NULL) | |
8026 | { | |
8027 | static tree print_complex_fndecl; | |
8028 | pfndecl = &print_complex_fndecl; | |
8029 | fnname = "__go_print_complex"; | |
b13c66cd | 8030 | arg = fold_convert_loc(location.gcc_location(), |
8031 | complex_double_type_node, arg); | |
e440a328 | 8032 | } |
8033 | else if (type->is_boolean_type()) | |
8034 | { | |
8035 | static tree print_bool_fndecl; | |
8036 | pfndecl = &print_bool_fndecl; | |
8037 | fnname = "__go_print_bool"; | |
8038 | } | |
8039 | else if (type->points_to() != NULL | |
8040 | || type->channel_type() != NULL | |
8041 | || type->map_type() != NULL | |
8042 | || type->function_type() != NULL) | |
8043 | { | |
8044 | static tree print_pointer_fndecl; | |
8045 | pfndecl = &print_pointer_fndecl; | |
8046 | fnname = "__go_print_pointer"; | |
b13c66cd | 8047 | arg = fold_convert_loc(location.gcc_location(), |
8048 | ptr_type_node, arg); | |
e440a328 | 8049 | } |
8050 | else if (type->interface_type() != NULL) | |
8051 | { | |
8052 | if (type->interface_type()->is_empty()) | |
8053 | { | |
8054 | static tree print_empty_interface_fndecl; | |
8055 | pfndecl = &print_empty_interface_fndecl; | |
8056 | fnname = "__go_print_empty_interface"; | |
8057 | } | |
8058 | else | |
8059 | { | |
8060 | static tree print_interface_fndecl; | |
8061 | pfndecl = &print_interface_fndecl; | |
8062 | fnname = "__go_print_interface"; | |
8063 | } | |
8064 | } | |
411eb89e | 8065 | else if (type->is_slice_type()) |
e440a328 | 8066 | { |
8067 | static tree print_slice_fndecl; | |
8068 | pfndecl = &print_slice_fndecl; | |
8069 | fnname = "__go_print_slice"; | |
8070 | } | |
8071 | else | |
cd238b8d | 8072 | { |
8073 | go_assert(saw_errors()); | |
8074 | return error_mark_node; | |
8075 | } | |
e440a328 | 8076 | |
8077 | tree call = Gogo::call_builtin(pfndecl, | |
8078 | location, | |
8079 | fnname, | |
8080 | 1, | |
8081 | void_type_node, | |
8082 | TREE_TYPE(arg), | |
8083 | arg); | |
5fb82b5e | 8084 | if (call == error_mark_node) |
8085 | return error_mark_node; | |
8086 | append_to_statement_list(call, &stmt_list); | |
e440a328 | 8087 | } |
8088 | } | |
8089 | ||
8090 | if (is_ln) | |
8091 | { | |
8092 | static tree print_nl_fndecl; | |
8093 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
8094 | location, | |
8095 | "__go_print_nl", | |
8096 | 0, | |
8097 | void_type_node); | |
5fb82b5e | 8098 | if (call == error_mark_node) |
8099 | return error_mark_node; | |
e440a328 | 8100 | append_to_statement_list(call, &stmt_list); |
8101 | } | |
8102 | ||
8103 | return stmt_list; | |
8104 | } | |
8105 | ||
8106 | case BUILTIN_PANIC: | |
8107 | { | |
8108 | const Expression_list* args = this->args(); | |
c484d925 | 8109 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8110 | Expression* arg = args->front(); |
8111 | tree arg_tree = arg->get_tree(context); | |
8112 | if (arg_tree == error_mark_node) | |
8113 | return error_mark_node; | |
b13c66cd | 8114 | Type *empty = |
823c7e3d | 8115 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8116 | arg_tree = Expression::convert_for_assignment(context, empty, |
8117 | arg->type(), | |
8118 | arg_tree, location); | |
8119 | static tree panic_fndecl; | |
8120 | tree call = Gogo::call_builtin(&panic_fndecl, | |
8121 | location, | |
8122 | "__go_panic", | |
8123 | 1, | |
8124 | void_type_node, | |
8125 | TREE_TYPE(arg_tree), | |
8126 | arg_tree); | |
5fb82b5e | 8127 | if (call == error_mark_node) |
8128 | return error_mark_node; | |
e440a328 | 8129 | // This function will throw an exception. |
8130 | TREE_NOTHROW(panic_fndecl) = 0; | |
8131 | // This function will not return. | |
8132 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
8133 | return call; | |
8134 | } | |
8135 | ||
8136 | case BUILTIN_RECOVER: | |
8137 | { | |
8138 | // The argument is set when building recover thunks. It's a | |
8139 | // boolean value which is true if we can recover a value now. | |
8140 | const Expression_list* args = this->args(); | |
c484d925 | 8141 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8142 | Expression* arg = args->front(); |
8143 | tree arg_tree = arg->get_tree(context); | |
8144 | if (arg_tree == error_mark_node) | |
8145 | return error_mark_node; | |
8146 | ||
b13c66cd | 8147 | Type *empty = |
823c7e3d | 8148 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
9f0e0513 | 8149 | tree empty_tree = type_to_tree(empty->get_backend(context->gogo())); |
e440a328 | 8150 | |
8151 | Type* nil_type = Type::make_nil_type(); | |
8152 | Expression* nil = Expression::make_nil(location); | |
8153 | tree nil_tree = nil->get_tree(context); | |
8154 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
8155 | empty, | |
8156 | nil_type, | |
8157 | nil_tree, | |
8158 | location); | |
8159 | ||
8160 | // We need to handle a deferred call to recover specially, | |
8161 | // because it changes whether it can recover a panic or not. | |
8162 | // See test7 in test/recover1.go. | |
8163 | tree call; | |
8164 | if (this->is_deferred()) | |
8165 | { | |
8166 | static tree deferred_recover_fndecl; | |
8167 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
8168 | location, | |
8169 | "__go_deferred_recover", | |
8170 | 0, | |
8171 | empty_tree); | |
8172 | } | |
8173 | else | |
8174 | { | |
8175 | static tree recover_fndecl; | |
8176 | call = Gogo::call_builtin(&recover_fndecl, | |
8177 | location, | |
8178 | "__go_recover", | |
8179 | 0, | |
8180 | empty_tree); | |
8181 | } | |
5fb82b5e | 8182 | if (call == error_mark_node) |
8183 | return error_mark_node; | |
b13c66cd | 8184 | return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree, |
8185 | arg_tree, call, empty_nil_tree); | |
e440a328 | 8186 | } |
8187 | ||
8188 | case BUILTIN_CLOSE: | |
e440a328 | 8189 | { |
8190 | const Expression_list* args = this->args(); | |
c484d925 | 8191 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8192 | Expression* arg = args->front(); |
8193 | tree arg_tree = arg->get_tree(context); | |
8194 | if (arg_tree == error_mark_node) | |
8195 | return error_mark_node; | |
0dc2f918 | 8196 | static tree close_fndecl; |
8197 | return Gogo::call_builtin(&close_fndecl, | |
8198 | location, | |
8199 | "__go_builtin_close", | |
8200 | 1, | |
8201 | void_type_node, | |
8202 | TREE_TYPE(arg_tree), | |
8203 | arg_tree); | |
e440a328 | 8204 | } |
8205 | ||
8206 | case BUILTIN_SIZEOF: | |
8207 | case BUILTIN_OFFSETOF: | |
8208 | case BUILTIN_ALIGNOF: | |
8209 | { | |
0c77715b | 8210 | Numeric_constant nc; |
8211 | unsigned long val; | |
8212 | if (!this->numeric_constant_value(&nc) | |
8213 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8214 | { |
c484d925 | 8215 | go_assert(saw_errors()); |
7f1d9abd | 8216 | return error_mark_node; |
8217 | } | |
7ba86326 | 8218 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
8219 | tree type = type_to_tree(uintptr_type->get_backend(gogo)); | |
0c77715b | 8220 | return build_int_cst(type, val); |
e440a328 | 8221 | } |
8222 | ||
8223 | case BUILTIN_COPY: | |
8224 | { | |
8225 | const Expression_list* args = this->args(); | |
c484d925 | 8226 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8227 | Expression* arg1 = args->front(); |
8228 | Expression* arg2 = args->back(); | |
8229 | ||
8230 | tree arg1_tree = arg1->get_tree(context); | |
8231 | tree arg2_tree = arg2->get_tree(context); | |
8232 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8233 | return error_mark_node; | |
8234 | ||
8235 | Type* arg1_type = arg1->type(); | |
8236 | Array_type* at = arg1_type->array_type(); | |
8237 | arg1_tree = save_expr(arg1_tree); | |
8238 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
8239 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
d8ccb1e3 | 8240 | if (arg1_val == error_mark_node || arg1_len == error_mark_node) |
8241 | return error_mark_node; | |
e440a328 | 8242 | |
8243 | Type* arg2_type = arg2->type(); | |
8244 | tree arg2_val; | |
8245 | tree arg2_len; | |
411eb89e | 8246 | if (arg2_type->is_slice_type()) |
e440a328 | 8247 | { |
8248 | at = arg2_type->array_type(); | |
8249 | arg2_tree = save_expr(arg2_tree); | |
8250 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8251 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8252 | } | |
8253 | else | |
8254 | { | |
8255 | arg2_tree = save_expr(arg2_tree); | |
8256 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8257 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8258 | } | |
d8ccb1e3 | 8259 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8260 | return error_mark_node; | |
e440a328 | 8261 | |
8262 | arg1_len = save_expr(arg1_len); | |
8263 | arg2_len = save_expr(arg2_len); | |
b13c66cd | 8264 | tree len = fold_build3_loc(location.gcc_location(), COND_EXPR, |
8265 | TREE_TYPE(arg1_len), | |
8266 | fold_build2_loc(location.gcc_location(), | |
8267 | LT_EXPR, boolean_type_node, | |
e440a328 | 8268 | arg1_len, arg2_len), |
8269 | arg1_len, arg2_len); | |
8270 | len = save_expr(len); | |
8271 | ||
8272 | Type* element_type = at->element_type(); | |
9f0e0513 | 8273 | Btype* element_btype = element_type->get_backend(gogo); |
8274 | tree element_type_tree = type_to_tree(element_btype); | |
d8ccb1e3 | 8275 | if (element_type_tree == error_mark_node) |
8276 | return error_mark_node; | |
e440a328 | 8277 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
b13c66cd | 8278 | tree bytecount = fold_convert_loc(location.gcc_location(), |
8279 | TREE_TYPE(element_size), len); | |
8280 | bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR, | |
e440a328 | 8281 | TREE_TYPE(element_size), |
8282 | bytecount, element_size); | |
b13c66cd | 8283 | bytecount = fold_convert_loc(location.gcc_location(), size_type_node, |
8284 | bytecount); | |
e440a328 | 8285 | |
b13c66cd | 8286 | arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node, |
8287 | arg1_val); | |
8288 | arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node, | |
8289 | arg2_val); | |
3991cb03 | 8290 | |
8291 | static tree copy_fndecl; | |
8292 | tree call = Gogo::call_builtin(©_fndecl, | |
8293 | location, | |
8294 | "__go_copy", | |
8295 | 3, | |
8296 | void_type_node, | |
8297 | ptr_type_node, | |
8298 | arg1_val, | |
8299 | ptr_type_node, | |
8300 | arg2_val, | |
8301 | size_type_node, | |
8302 | bytecount); | |
8303 | if (call == error_mark_node) | |
8304 | return error_mark_node; | |
e440a328 | 8305 | |
b13c66cd | 8306 | return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, |
8307 | TREE_TYPE(len), call, len); | |
e440a328 | 8308 | } |
8309 | ||
8310 | case BUILTIN_APPEND: | |
8311 | { | |
8312 | const Expression_list* args = this->args(); | |
c484d925 | 8313 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8314 | Expression* arg1 = args->front(); |
8315 | Expression* arg2 = args->back(); | |
8316 | ||
8317 | tree arg1_tree = arg1->get_tree(context); | |
8318 | tree arg2_tree = arg2->get_tree(context); | |
8319 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8320 | return error_mark_node; | |
8321 | ||
9d44fbe3 | 8322 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8323 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8324 | |
4fd4fcf4 | 8325 | tree arg2_val; |
8326 | tree arg2_len; | |
8327 | tree element_size; | |
8328 | if (arg2->type()->is_string_type() | |
60963afd | 8329 | && element_type->integer_type() != NULL |
8330 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8331 | { |
8332 | arg2_tree = save_expr(arg2_tree); | |
8333 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8334 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8335 | element_size = size_int(1); | |
8336 | } | |
8337 | else | |
8338 | { | |
8339 | arg2_tree = Expression::convert_for_assignment(context, at, | |
8340 | arg2->type(), | |
8341 | arg2_tree, | |
8342 | location); | |
8343 | if (arg2_tree == error_mark_node) | |
8344 | return error_mark_node; | |
8345 | ||
8346 | arg2_tree = save_expr(arg2_tree); | |
8347 | ||
8348 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8349 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8350 | ||
8351 | Btype* element_btype = element_type->get_backend(gogo); | |
8352 | tree element_type_tree = type_to_tree(element_btype); | |
8353 | if (element_type_tree == error_mark_node) | |
8354 | return error_mark_node; | |
8355 | element_size = TYPE_SIZE_UNIT(element_type_tree); | |
8356 | } | |
ed64c8e5 | 8357 | |
b13c66cd | 8358 | arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node, |
8359 | arg2_val); | |
8360 | arg2_len = fold_convert_loc(location.gcc_location(), size_type_node, | |
8361 | arg2_len); | |
8362 | element_size = fold_convert_loc(location.gcc_location(), size_type_node, | |
3991cb03 | 8363 | element_size); |
e440a328 | 8364 | |
4fd4fcf4 | 8365 | if (arg2_val == error_mark_node |
8366 | || arg2_len == error_mark_node | |
8367 | || element_size == error_mark_node) | |
8368 | return error_mark_node; | |
8369 | ||
e440a328 | 8370 | // We rebuild the decl each time since the slice types may |
8371 | // change. | |
8372 | tree append_fndecl = NULL_TREE; | |
8373 | return Gogo::call_builtin(&append_fndecl, | |
8374 | location, | |
8375 | "__go_append", | |
3991cb03 | 8376 | 4, |
e440a328 | 8377 | TREE_TYPE(arg1_tree), |
e440a328 | 8378 | TREE_TYPE(arg1_tree), |
8379 | arg1_tree, | |
3991cb03 | 8380 | ptr_type_node, |
8381 | arg2_val, | |
8382 | size_type_node, | |
8383 | arg2_len, | |
8384 | size_type_node, | |
8385 | element_size); | |
e440a328 | 8386 | } |
8387 | ||
8388 | case BUILTIN_REAL: | |
8389 | case BUILTIN_IMAG: | |
8390 | { | |
8391 | const Expression_list* args = this->args(); | |
c484d925 | 8392 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8393 | Expression* arg = args->front(); |
8394 | tree arg_tree = arg->get_tree(context); | |
8395 | if (arg_tree == error_mark_node) | |
8396 | return error_mark_node; | |
c484d925 | 8397 | go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); |
e440a328 | 8398 | if (this->code_ == BUILTIN_REAL) |
b13c66cd | 8399 | return fold_build1_loc(location.gcc_location(), REALPART_EXPR, |
e440a328 | 8400 | TREE_TYPE(TREE_TYPE(arg_tree)), |
8401 | arg_tree); | |
8402 | else | |
b13c66cd | 8403 | return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR, |
e440a328 | 8404 | TREE_TYPE(TREE_TYPE(arg_tree)), |
8405 | arg_tree); | |
8406 | } | |
8407 | ||
48080209 | 8408 | case BUILTIN_COMPLEX: |
e440a328 | 8409 | { |
8410 | const Expression_list* args = this->args(); | |
c484d925 | 8411 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8412 | tree r = args->front()->get_tree(context); |
8413 | tree i = args->back()->get_tree(context); | |
8414 | if (r == error_mark_node || i == error_mark_node) | |
8415 | return error_mark_node; | |
c484d925 | 8416 | go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) |
e440a328 | 8417 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); |
c484d925 | 8418 | go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); |
b13c66cd | 8419 | return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR, |
e440a328 | 8420 | build_complex_type(TREE_TYPE(r)), |
8421 | r, i); | |
8422 | } | |
8423 | ||
8424 | default: | |
c3e6f413 | 8425 | go_unreachable(); |
e440a328 | 8426 | } |
8427 | } | |
8428 | ||
8429 | // We have to support exporting a builtin call expression, because | |
8430 | // code can set a constant to the result of a builtin expression. | |
8431 | ||
8432 | void | |
8433 | Builtin_call_expression::do_export(Export* exp) const | |
8434 | { | |
0c77715b | 8435 | Numeric_constant nc; |
8436 | if (!this->numeric_constant_value(&nc)) | |
8437 | { | |
8438 | error_at(this->location(), "value is not constant"); | |
8439 | return; | |
8440 | } | |
e440a328 | 8441 | |
0c77715b | 8442 | if (nc.is_int()) |
e440a328 | 8443 | { |
0c77715b | 8444 | mpz_t val; |
8445 | nc.get_int(&val); | |
e440a328 | 8446 | Integer_expression::export_integer(exp, val); |
0c77715b | 8447 | mpz_clear(val); |
e440a328 | 8448 | } |
0c77715b | 8449 | else if (nc.is_float()) |
e440a328 | 8450 | { |
8451 | mpfr_t fval; | |
0c77715b | 8452 | nc.get_float(&fval); |
8453 | Float_expression::export_float(exp, fval); | |
e440a328 | 8454 | mpfr_clear(fval); |
8455 | } | |
0c77715b | 8456 | else if (nc.is_complex()) |
e440a328 | 8457 | { |
8458 | mpfr_t real; | |
8459 | mpfr_t imag; | |
0c77715b | 8460 | Complex_expression::export_complex(exp, real, imag); |
e440a328 | 8461 | mpfr_clear(real); |
8462 | mpfr_clear(imag); | |
8463 | } | |
0c77715b | 8464 | else |
8465 | go_unreachable(); | |
e440a328 | 8466 | |
8467 | // A trailing space lets us reliably identify the end of the number. | |
8468 | exp->write_c_string(" "); | |
8469 | } | |
8470 | ||
8471 | // Class Call_expression. | |
8472 | ||
8473 | // Traversal. | |
8474 | ||
8475 | int | |
8476 | Call_expression::do_traverse(Traverse* traverse) | |
8477 | { | |
8478 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8479 | return TRAVERSE_EXIT; | |
8480 | if (this->args_ != NULL) | |
8481 | { | |
8482 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8483 | return TRAVERSE_EXIT; | |
8484 | } | |
8485 | return TRAVERSE_CONTINUE; | |
8486 | } | |
8487 | ||
8488 | // Lower a call statement. | |
8489 | ||
8490 | Expression* | |
ceeb4318 | 8491 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
8492 | Statement_inserter* inserter, int) | |
e440a328 | 8493 | { |
b13c66cd | 8494 | Location loc = this->location(); |
09ea332d | 8495 | |
ceeb4318 | 8496 | // A type cast can look like a function call. |
e440a328 | 8497 | if (this->fn_->is_type_expression() |
8498 | && this->args_ != NULL | |
8499 | && this->args_->size() == 1) | |
8500 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 8501 | loc); |
e440a328 | 8502 | |
8503 | // Recognize a call to a builtin function. | |
8504 | Func_expression* fne = this->fn_->func_expression(); | |
8505 | if (fne != NULL | |
8506 | && fne->named_object()->is_function_declaration() | |
8507 | && fne->named_object()->func_declaration_value()->type()->is_builtin()) | |
8508 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
09ea332d | 8509 | this->is_varargs_, loc); |
e440a328 | 8510 | |
8511 | // Handle an argument which is a call to a function which returns | |
8512 | // multiple results. | |
8513 | if (this->args_ != NULL | |
8514 | && this->args_->size() == 1 | |
8515 | && this->args_->front()->call_expression() != NULL | |
8516 | && this->fn_->type()->function_type() != NULL) | |
8517 | { | |
8518 | Function_type* fntype = this->fn_->type()->function_type(); | |
8519 | size_t rc = this->args_->front()->call_expression()->result_count(); | |
8520 | if (rc > 1 | |
8521 | && fntype->parameters() != NULL | |
8522 | && (fntype->parameters()->size() == rc | |
8523 | || (fntype->is_varargs() | |
8524 | && fntype->parameters()->size() - 1 <= rc))) | |
8525 | { | |
8526 | Call_expression* call = this->args_->front()->call_expression(); | |
8527 | Expression_list* args = new Expression_list; | |
8528 | for (size_t i = 0; i < rc; ++i) | |
8529 | args->push_back(Expression::make_call_result(call, i)); | |
8530 | // We can't return a new call expression here, because this | |
42535814 | 8531 | // one may be referenced by Call_result expressions. We |
8532 | // also can't delete the old arguments, because we may still | |
8533 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8534 | this->args_ = args; |
8535 | } | |
8536 | } | |
8537 | ||
ceeb4318 | 8538 | // If this call returns multiple results, create a temporary |
8539 | // variable for each result. | |
8540 | size_t rc = this->result_count(); | |
8541 | if (rc > 1 && this->results_ == NULL) | |
8542 | { | |
8543 | std::vector<Temporary_statement*>* temps = | |
8544 | new std::vector<Temporary_statement*>; | |
8545 | temps->reserve(rc); | |
8546 | const Typed_identifier_list* results = | |
8547 | this->fn_->type()->function_type()->results(); | |
8548 | for (Typed_identifier_list::const_iterator p = results->begin(); | |
8549 | p != results->end(); | |
8550 | ++p) | |
8551 | { | |
8552 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 8553 | NULL, loc); |
ceeb4318 | 8554 | inserter->insert(temp); |
8555 | temps->push_back(temp); | |
8556 | } | |
8557 | this->results_ = temps; | |
8558 | } | |
8559 | ||
e440a328 | 8560 | // Handle a call to a varargs function by packaging up the extra |
8561 | // parameters. | |
8562 | if (this->fn_->type()->function_type() != NULL | |
8563 | && this->fn_->type()->function_type()->is_varargs()) | |
8564 | { | |
8565 | Function_type* fntype = this->fn_->type()->function_type(); | |
8566 | const Typed_identifier_list* parameters = fntype->parameters(); | |
c484d925 | 8567 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 8568 | Type* varargs_type = parameters->back().type(); |
09ea332d | 8569 | this->lower_varargs(gogo, function, inserter, varargs_type, |
8570 | parameters->size()); | |
8571 | } | |
8572 | ||
8573 | // If this is call to a method, call the method directly passing the | |
8574 | // object as the first parameter. | |
8575 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8576 | if (bme != NULL) | |
8577 | { | |
8578 | Named_object* method = bme->method(); | |
8579 | Expression* first_arg = bme->first_argument(); | |
8580 | ||
8581 | // We always pass a pointer when calling a method. | |
8582 | if (first_arg->type()->points_to() == NULL | |
8583 | && !first_arg->type()->is_error()) | |
8584 | { | |
8585 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
8586 | // We may need to create a temporary variable so that we can | |
8587 | // take the address. We can't do that here because it will | |
8588 | // mess up the order of evaluation. | |
8589 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
8590 | ue->set_create_temp(); | |
8591 | } | |
8592 | ||
8593 | // If we are calling a method which was inherited from an | |
8594 | // embedded struct, and the method did not get a stub, then the | |
8595 | // first type may be wrong. | |
8596 | Type* fatype = bme->first_argument_type(); | |
8597 | if (fatype != NULL) | |
8598 | { | |
8599 | if (fatype->points_to() == NULL) | |
8600 | fatype = Type::make_pointer_type(fatype); | |
8601 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
8602 | } | |
8603 | ||
8604 | Expression_list* new_args = new Expression_list(); | |
8605 | new_args->push_back(first_arg); | |
8606 | if (this->args_ != NULL) | |
8607 | { | |
8608 | for (Expression_list::const_iterator p = this->args_->begin(); | |
8609 | p != this->args_->end(); | |
8610 | ++p) | |
8611 | new_args->push_back(*p); | |
8612 | } | |
8613 | ||
8614 | // We have to change in place because this structure may be | |
8615 | // referenced by Call_result_expressions. We can't delete the | |
8616 | // old arguments, because we may be traversing them up in some | |
8617 | // caller. FIXME. | |
8618 | this->args_ = new_args; | |
8619 | this->fn_ = Expression::make_func_reference(method, NULL, | |
8620 | bme->location()); | |
e440a328 | 8621 | } |
8622 | ||
8623 | return this; | |
8624 | } | |
8625 | ||
8626 | // Lower a call to a varargs function. FUNCTION is the function in | |
8627 | // which the call occurs--it's not the function we are calling. | |
8628 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8629 | // PARAM_COUNT is the number of parameters of the function we are | |
8630 | // calling; the last of these parameters will be the varargs | |
8631 | // parameter. | |
8632 | ||
09ea332d | 8633 | void |
e440a328 | 8634 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 8635 | Statement_inserter* inserter, |
e440a328 | 8636 | Type* varargs_type, size_t param_count) |
8637 | { | |
8638 | if (this->varargs_are_lowered_) | |
09ea332d | 8639 | return; |
e440a328 | 8640 | |
b13c66cd | 8641 | Location loc = this->location(); |
e440a328 | 8642 | |
c484d925 | 8643 | go_assert(param_count > 0); |
411eb89e | 8644 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 8645 | |
8646 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8647 | if (arg_count < param_count - 1) | |
8648 | { | |
8649 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 8650 | return; |
e440a328 | 8651 | } |
8652 | ||
8653 | Expression_list* old_args = this->args_; | |
8654 | Expression_list* new_args = new Expression_list(); | |
8655 | bool push_empty_arg = false; | |
8656 | if (old_args == NULL || old_args->empty()) | |
8657 | { | |
c484d925 | 8658 | go_assert(param_count == 1); |
e440a328 | 8659 | push_empty_arg = true; |
8660 | } | |
8661 | else | |
8662 | { | |
8663 | Expression_list::const_iterator pa; | |
8664 | int i = 1; | |
8665 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8666 | { | |
8667 | if (static_cast<size_t>(i) == param_count) | |
8668 | break; | |
8669 | new_args->push_back(*pa); | |
8670 | } | |
8671 | ||
8672 | // We have reached the varargs parameter. | |
8673 | ||
8674 | bool issued_error = false; | |
8675 | if (pa == old_args->end()) | |
8676 | push_empty_arg = true; | |
8677 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8678 | new_args->push_back(*pa); | |
8679 | else if (this->is_varargs_) | |
8680 | { | |
a6645f74 | 8681 | if ((*pa)->type()->is_slice_type()) |
8682 | this->report_error(_("too many arguments")); | |
8683 | else | |
8684 | { | |
8685 | error_at(this->location(), | |
8686 | _("invalid use of %<...%> with non-slice")); | |
8687 | this->set_is_error(); | |
8688 | } | |
09ea332d | 8689 | return; |
e440a328 | 8690 | } |
e440a328 | 8691 | else |
8692 | { | |
8693 | Type* element_type = varargs_type->array_type()->element_type(); | |
8694 | Expression_list* vals = new Expression_list; | |
8695 | for (; pa != old_args->end(); ++pa, ++i) | |
8696 | { | |
8697 | // Check types here so that we get a better message. | |
8698 | Type* patype = (*pa)->type(); | |
b13c66cd | 8699 | Location paloc = (*pa)->location(); |
e440a328 | 8700 | if (!this->check_argument_type(i, element_type, patype, |
8701 | paloc, issued_error)) | |
8702 | continue; | |
8703 | vals->push_back(*pa); | |
8704 | } | |
8705 | Expression* val = | |
8706 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
09ea332d | 8707 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 8708 | new_args->push_back(val); |
8709 | } | |
8710 | } | |
8711 | ||
8712 | if (push_empty_arg) | |
8713 | new_args->push_back(Expression::make_nil(loc)); | |
8714 | ||
8715 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 8716 | // be referenced by Call_result expressions. FIXME. We can't |
8717 | // delete OLD_ARGS because we may have both a Call_expression and a | |
8718 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 8719 | this->args_ = new_args; |
8720 | this->varargs_are_lowered_ = true; | |
e440a328 | 8721 | } |
8722 | ||
ceeb4318 | 8723 | // Get the function type. This can return NULL in error cases. |
e440a328 | 8724 | |
8725 | Function_type* | |
8726 | Call_expression::get_function_type() const | |
8727 | { | |
8728 | return this->fn_->type()->function_type(); | |
8729 | } | |
8730 | ||
8731 | // Return the number of values which this call will return. | |
8732 | ||
8733 | size_t | |
8734 | Call_expression::result_count() const | |
8735 | { | |
8736 | const Function_type* fntype = this->get_function_type(); | |
8737 | if (fntype == NULL) | |
8738 | return 0; | |
8739 | if (fntype->results() == NULL) | |
8740 | return 0; | |
8741 | return fntype->results()->size(); | |
8742 | } | |
8743 | ||
ceeb4318 | 8744 | // Return the temporary which holds a result. |
8745 | ||
8746 | Temporary_statement* | |
8747 | Call_expression::result(size_t i) const | |
8748 | { | |
cd238b8d | 8749 | if (this->results_ == NULL || this->results_->size() <= i) |
8750 | { | |
8751 | go_assert(saw_errors()); | |
8752 | return NULL; | |
8753 | } | |
ceeb4318 | 8754 | return (*this->results_)[i]; |
8755 | } | |
8756 | ||
e440a328 | 8757 | // Return whether this is a call to the predeclared function recover. |
8758 | ||
8759 | bool | |
8760 | Call_expression::is_recover_call() const | |
8761 | { | |
8762 | return this->do_is_recover_call(); | |
8763 | } | |
8764 | ||
8765 | // Set the argument to the recover function. | |
8766 | ||
8767 | void | |
8768 | Call_expression::set_recover_arg(Expression* arg) | |
8769 | { | |
8770 | this->do_set_recover_arg(arg); | |
8771 | } | |
8772 | ||
8773 | // Virtual functions also implemented by Builtin_call_expression. | |
8774 | ||
8775 | bool | |
8776 | Call_expression::do_is_recover_call() const | |
8777 | { | |
8778 | return false; | |
8779 | } | |
8780 | ||
8781 | void | |
8782 | Call_expression::do_set_recover_arg(Expression*) | |
8783 | { | |
c3e6f413 | 8784 | go_unreachable(); |
e440a328 | 8785 | } |
8786 | ||
ceeb4318 | 8787 | // We have found an error with this call expression; return true if |
8788 | // we should report it. | |
8789 | ||
8790 | bool | |
8791 | Call_expression::issue_error() | |
8792 | { | |
8793 | if (this->issued_error_) | |
8794 | return false; | |
8795 | else | |
8796 | { | |
8797 | this->issued_error_ = true; | |
8798 | return true; | |
8799 | } | |
8800 | } | |
8801 | ||
e440a328 | 8802 | // Get the type. |
8803 | ||
8804 | Type* | |
8805 | Call_expression::do_type() | |
8806 | { | |
8807 | if (this->type_ != NULL) | |
8808 | return this->type_; | |
8809 | ||
8810 | Type* ret; | |
8811 | Function_type* fntype = this->get_function_type(); | |
8812 | if (fntype == NULL) | |
8813 | return Type::make_error_type(); | |
8814 | ||
8815 | const Typed_identifier_list* results = fntype->results(); | |
8816 | if (results == NULL) | |
8817 | ret = Type::make_void_type(); | |
8818 | else if (results->size() == 1) | |
8819 | ret = results->begin()->type(); | |
8820 | else | |
8821 | ret = Type::make_call_multiple_result_type(this); | |
8822 | ||
8823 | this->type_ = ret; | |
8824 | ||
8825 | return this->type_; | |
8826 | } | |
8827 | ||
8828 | // Determine types for a call expression. We can use the function | |
8829 | // parameter types to set the types of the arguments. | |
8830 | ||
8831 | void | |
8832 | Call_expression::do_determine_type(const Type_context*) | |
8833 | { | |
fb94b0ca | 8834 | if (!this->determining_types()) |
8835 | return; | |
8836 | ||
e440a328 | 8837 | this->fn_->determine_type_no_context(); |
8838 | Function_type* fntype = this->get_function_type(); | |
8839 | const Typed_identifier_list* parameters = NULL; | |
8840 | if (fntype != NULL) | |
8841 | parameters = fntype->parameters(); | |
8842 | if (this->args_ != NULL) | |
8843 | { | |
8844 | Typed_identifier_list::const_iterator pt; | |
8845 | if (parameters != NULL) | |
8846 | pt = parameters->begin(); | |
09ea332d | 8847 | bool first = true; |
e440a328 | 8848 | for (Expression_list::const_iterator pa = this->args_->begin(); |
8849 | pa != this->args_->end(); | |
8850 | ++pa) | |
8851 | { | |
09ea332d | 8852 | if (first) |
8853 | { | |
8854 | first = false; | |
8855 | // If this is a method, the first argument is the | |
8856 | // receiver. | |
8857 | if (fntype != NULL && fntype->is_method()) | |
8858 | { | |
8859 | Type* rtype = fntype->receiver()->type(); | |
8860 | // The receiver is always passed as a pointer. | |
8861 | if (rtype->points_to() == NULL) | |
8862 | rtype = Type::make_pointer_type(rtype); | |
8863 | Type_context subcontext(rtype, false); | |
8864 | (*pa)->determine_type(&subcontext); | |
8865 | continue; | |
8866 | } | |
8867 | } | |
8868 | ||
e440a328 | 8869 | if (parameters != NULL && pt != parameters->end()) |
8870 | { | |
8871 | Type_context subcontext(pt->type(), false); | |
8872 | (*pa)->determine_type(&subcontext); | |
8873 | ++pt; | |
8874 | } | |
8875 | else | |
8876 | (*pa)->determine_type_no_context(); | |
8877 | } | |
8878 | } | |
8879 | } | |
8880 | ||
fb94b0ca | 8881 | // Called when determining types for a Call_expression. Return true |
8882 | // if we should go ahead, false if they have already been determined. | |
8883 | ||
8884 | bool | |
8885 | Call_expression::determining_types() | |
8886 | { | |
8887 | if (this->types_are_determined_) | |
8888 | return false; | |
8889 | else | |
8890 | { | |
8891 | this->types_are_determined_ = true; | |
8892 | return true; | |
8893 | } | |
8894 | } | |
8895 | ||
e440a328 | 8896 | // Check types for parameter I. |
8897 | ||
8898 | bool | |
8899 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
8900 | const Type* argument_type, | |
b13c66cd | 8901 | Location argument_location, |
e440a328 | 8902 | bool issued_error) |
8903 | { | |
8904 | std::string reason; | |
053ee6ca | 8905 | bool ok; |
8906 | if (this->are_hidden_fields_ok_) | |
8907 | ok = Type::are_assignable_hidden_ok(parameter_type, argument_type, | |
8908 | &reason); | |
8909 | else | |
8910 | ok = Type::are_assignable(parameter_type, argument_type, &reason); | |
8911 | if (!ok) | |
e440a328 | 8912 | { |
8913 | if (!issued_error) | |
8914 | { | |
8915 | if (reason.empty()) | |
8916 | error_at(argument_location, "argument %d has incompatible type", i); | |
8917 | else | |
8918 | error_at(argument_location, | |
8919 | "argument %d has incompatible type (%s)", | |
8920 | i, reason.c_str()); | |
8921 | } | |
8922 | this->set_is_error(); | |
8923 | return false; | |
8924 | } | |
8925 | return true; | |
8926 | } | |
8927 | ||
8928 | // Check types. | |
8929 | ||
8930 | void | |
8931 | Call_expression::do_check_types(Gogo*) | |
8932 | { | |
a6645f74 | 8933 | if (this->classification() == EXPRESSION_ERROR) |
8934 | return; | |
8935 | ||
e440a328 | 8936 | Function_type* fntype = this->get_function_type(); |
8937 | if (fntype == NULL) | |
8938 | { | |
5c13bd80 | 8939 | if (!this->fn_->type()->is_error()) |
e440a328 | 8940 | this->report_error(_("expected function")); |
8941 | return; | |
8942 | } | |
8943 | ||
09ea332d | 8944 | bool is_method = fntype->is_method(); |
8945 | if (is_method) | |
e440a328 | 8946 | { |
09ea332d | 8947 | go_assert(this->args_ != NULL && !this->args_->empty()); |
8948 | Type* rtype = fntype->receiver()->type(); | |
8949 | Expression* first_arg = this->args_->front(); | |
8950 | // The language permits copying hidden fields for a method | |
8951 | // receiver. We dereference the values since receivers are | |
8952 | // always passed as pointers. | |
8953 | std::string reason; | |
8954 | if (!Type::are_assignable_hidden_ok(rtype->deref(), | |
8955 | first_arg->type()->deref(), | |
8956 | &reason)) | |
e440a328 | 8957 | { |
09ea332d | 8958 | if (reason.empty()) |
8959 | this->report_error(_("incompatible type for receiver")); | |
8960 | else | |
e440a328 | 8961 | { |
09ea332d | 8962 | error_at(this->location(), |
8963 | "incompatible type for receiver (%s)", | |
8964 | reason.c_str()); | |
8965 | this->set_is_error(); | |
e440a328 | 8966 | } |
8967 | } | |
8968 | } | |
8969 | ||
8970 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 8971 | // we don't have to worry about it here unless something is wrong. |
8972 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
8973 | { | |
8974 | if (!fntype->is_varargs()) | |
8975 | { | |
8976 | error_at(this->location(), | |
8977 | _("invalid use of %<...%> calling non-variadic function")); | |
8978 | this->set_is_error(); | |
8979 | return; | |
8980 | } | |
8981 | } | |
e440a328 | 8982 | |
8983 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8984 | if (this->args_ == NULL) | |
8985 | { | |
8986 | if (parameters != NULL && !parameters->empty()) | |
8987 | this->report_error(_("not enough arguments")); | |
8988 | } | |
8989 | else if (parameters == NULL) | |
09ea332d | 8990 | { |
8991 | if (!is_method || this->args_->size() > 1) | |
8992 | this->report_error(_("too many arguments")); | |
8993 | } | |
e440a328 | 8994 | else |
8995 | { | |
8996 | int i = 0; | |
09ea332d | 8997 | Expression_list::const_iterator pa = this->args_->begin(); |
8998 | if (is_method) | |
8999 | ++pa; | |
9000 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9001 | pt != parameters->end(); | |
9002 | ++pt, ++pa, ++i) | |
e440a328 | 9003 | { |
09ea332d | 9004 | if (pa == this->args_->end()) |
e440a328 | 9005 | { |
09ea332d | 9006 | this->report_error(_("not enough arguments")); |
e440a328 | 9007 | return; |
9008 | } | |
9009 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9010 | (*pa)->location(), false); | |
9011 | } | |
09ea332d | 9012 | if (pa != this->args_->end()) |
9013 | this->report_error(_("too many arguments")); | |
e440a328 | 9014 | } |
9015 | } | |
9016 | ||
9017 | // Return whether we have to use a temporary variable to ensure that | |
9018 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9019 | // results then it will inevitably be executed last. |
e440a328 | 9020 | |
9021 | bool | |
9022 | Call_expression::do_must_eval_in_order() const | |
9023 | { | |
ceeb4318 | 9024 | return this->result_count() > 0; |
e440a328 | 9025 | } |
9026 | ||
e440a328 | 9027 | // Get the function and the first argument to use when calling an |
9028 | // interface method. | |
9029 | ||
9030 | tree | |
9031 | Call_expression::interface_method_function( | |
9032 | Translate_context* context, | |
9033 | Interface_field_reference_expression* interface_method, | |
9034 | tree* first_arg_ptr) | |
9035 | { | |
9036 | tree expr = interface_method->expr()->get_tree(context); | |
9037 | if (expr == error_mark_node) | |
9038 | return error_mark_node; | |
9039 | expr = save_expr(expr); | |
9040 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
9041 | if (first_arg == error_mark_node) | |
9042 | return error_mark_node; | |
9043 | *first_arg_ptr = first_arg; | |
9044 | return interface_method->get_function_tree(context, expr); | |
9045 | } | |
9046 | ||
9047 | // Build the call expression. | |
9048 | ||
9049 | tree | |
9050 | Call_expression::do_get_tree(Translate_context* context) | |
9051 | { | |
9052 | if (this->tree_ != NULL_TREE) | |
9053 | return this->tree_; | |
9054 | ||
9055 | Function_type* fntype = this->get_function_type(); | |
9056 | if (fntype == NULL) | |
9057 | return error_mark_node; | |
9058 | ||
9059 | if (this->fn_->is_error_expression()) | |
9060 | return error_mark_node; | |
9061 | ||
9062 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9063 | Location location = this->location(); |
e440a328 | 9064 | |
9065 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9066 | Interface_field_reference_expression* interface_method = |
9067 | this->fn_->interface_field_reference_expression(); | |
9068 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9069 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9070 | |
9071 | int nargs; | |
9072 | tree* args; | |
9073 | if (this->args_ == NULL || this->args_->empty()) | |
9074 | { | |
09ea332d | 9075 | nargs = is_interface_method ? 1 : 0; |
e440a328 | 9076 | args = nargs == 0 ? NULL : new tree[nargs]; |
9077 | } | |
09ea332d | 9078 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9079 | { | |
9080 | // Passing a receiver parameter. | |
9081 | go_assert(!is_interface_method | |
9082 | && fntype->is_method() | |
9083 | && this->args_->size() == 1); | |
9084 | nargs = 1; | |
9085 | args = new tree[nargs]; | |
9086 | args[0] = this->args_->front()->get_tree(context); | |
9087 | } | |
e440a328 | 9088 | else |
9089 | { | |
9090 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9091 | |
9092 | nargs = this->args_->size(); | |
09ea332d | 9093 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9094 | nargs += i; |
9095 | args = new tree[nargs]; | |
9096 | ||
9097 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9098 | Expression_list::const_iterator pe = this->args_->begin(); |
9099 | if (!is_interface_method && fntype->is_method()) | |
9100 | { | |
9101 | args[i] = (*pe)->get_tree(context); | |
9102 | ++pe; | |
9103 | ++i; | |
9104 | } | |
9105 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9106 | { |
c484d925 | 9107 | go_assert(pp != params->end()); |
e440a328 | 9108 | tree arg_val = (*pe)->get_tree(context); |
9109 | args[i] = Expression::convert_for_assignment(context, | |
9110 | pp->type(), | |
9111 | (*pe)->type(), | |
9112 | arg_val, | |
9113 | location); | |
9114 | if (args[i] == error_mark_node) | |
cf609de4 | 9115 | { |
9116 | delete[] args; | |
9117 | return error_mark_node; | |
9118 | } | |
e440a328 | 9119 | } |
c484d925 | 9120 | go_assert(pp == params->end()); |
9121 | go_assert(i == nargs); | |
e440a328 | 9122 | } |
9123 | ||
9f0e0513 | 9124 | tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo)))); |
e440a328 | 9125 | if (rettype == error_mark_node) |
cf609de4 | 9126 | { |
9127 | delete[] args; | |
9128 | return error_mark_node; | |
9129 | } | |
e440a328 | 9130 | |
9131 | tree fn; | |
9132 | if (has_closure) | |
9133 | fn = func->get_tree_without_closure(gogo); | |
09ea332d | 9134 | else if (!is_interface_method) |
e440a328 | 9135 | fn = this->fn_->get_tree(context); |
e440a328 | 9136 | else |
09ea332d | 9137 | fn = this->interface_method_function(context, interface_method, &args[0]); |
e440a328 | 9138 | |
9139 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
cf609de4 | 9140 | { |
9141 | delete[] args; | |
9142 | return error_mark_node; | |
9143 | } | |
e440a328 | 9144 | |
e440a328 | 9145 | tree fndecl = fn; |
9146 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
9147 | fndecl = TREE_OPERAND(fndecl, 0); | |
9aa9e2df | 9148 | |
9149 | // Add a type cast in case the type of the function is a recursive | |
9150 | // type which refers to itself. | |
9151 | if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl)) | |
9152 | { | |
9f0e0513 | 9153 | tree fnt = type_to_tree(fntype->get_backend(gogo)); |
9aa9e2df | 9154 | if (fnt == error_mark_node) |
9155 | return error_mark_node; | |
b13c66cd | 9156 | fn = fold_convert_loc(location.gcc_location(), fnt, fn); |
9aa9e2df | 9157 | } |
9158 | ||
9159 | // This is to support builtin math functions when using 80387 math. | |
e440a328 | 9160 | tree excess_type = NULL_TREE; |
68e1881d | 9161 | if (optimize |
9162 | && TREE_CODE(fndecl) == FUNCTION_DECL | |
e440a328 | 9163 | && DECL_IS_BUILTIN(fndecl) |
9164 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
9165 | && nargs > 0 | |
9166 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
9167 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
9168 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
9169 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
9170 | { | |
9171 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
9172 | if (excess_type != NULL_TREE) | |
9173 | { | |
9174 | tree excess_fndecl = mathfn_built_in(excess_type, | |
9175 | DECL_FUNCTION_CODE(fndecl)); | |
9176 | if (excess_fndecl == NULL_TREE) | |
9177 | excess_type = NULL_TREE; | |
9178 | else | |
9179 | { | |
b13c66cd | 9180 | fn = build_fold_addr_expr_loc(location.gcc_location(), |
9181 | excess_fndecl); | |
e440a328 | 9182 | for (int i = 0; i < nargs; ++i) |
26ae0101 | 9183 | { |
9184 | if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i])) | |
9185 | || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i]))) | |
9186 | args[i] = ::convert(excess_type, args[i]); | |
9187 | } | |
e440a328 | 9188 | } |
9189 | } | |
9190 | } | |
9191 | ||
9192 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, | |
9193 | fn, nargs, args); | |
9194 | delete[] args; | |
9195 | ||
b13c66cd | 9196 | SET_EXPR_LOCATION(ret, location.gcc_location()); |
e440a328 | 9197 | |
9198 | if (has_closure) | |
9199 | { | |
9200 | tree closure_tree = func->closure()->get_tree(context); | |
9201 | if (closure_tree != error_mark_node) | |
9202 | CALL_EXPR_STATIC_CHAIN(ret) = closure_tree; | |
9203 | } | |
9204 | ||
9205 | // If this is a recursive function type which returns itself, as in | |
9206 | // type F func() F | |
9207 | // we have used ptr_type_node for the return type. Add a cast here | |
9208 | // to the correct type. | |
9209 | if (TREE_TYPE(ret) == ptr_type_node) | |
9210 | { | |
9f0e0513 | 9211 | tree t = type_to_tree(this->type()->base()->get_backend(gogo)); |
b13c66cd | 9212 | ret = fold_convert_loc(location.gcc_location(), t, ret); |
e440a328 | 9213 | } |
9214 | ||
9215 | if (excess_type != NULL_TREE) | |
9216 | { | |
9217 | // Calling convert here can undo our excess precision change. | |
9218 | // That may or may not be a bug in convert_to_real. | |
9219 | ret = build1(NOP_EXPR, rettype, ret); | |
9220 | } | |
9221 | ||
ceeb4318 | 9222 | if (this->results_ != NULL) |
9223 | ret = this->set_results(context, ret); | |
e440a328 | 9224 | |
9225 | this->tree_ = ret; | |
9226 | ||
9227 | return ret; | |
9228 | } | |
9229 | ||
ceeb4318 | 9230 | // Set the result variables if this call returns multiple results. |
9231 | ||
9232 | tree | |
9233 | Call_expression::set_results(Translate_context* context, tree call_tree) | |
9234 | { | |
9235 | tree stmt_list = NULL_TREE; | |
9236 | ||
9237 | call_tree = save_expr(call_tree); | |
9238 | ||
9239 | if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE) | |
9240 | { | |
9241 | go_assert(saw_errors()); | |
9242 | return call_tree; | |
9243 | } | |
9244 | ||
b13c66cd | 9245 | Location loc = this->location(); |
ceeb4318 | 9246 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); |
9247 | size_t rc = this->result_count(); | |
9248 | for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field)) | |
9249 | { | |
9250 | go_assert(field != NULL_TREE); | |
9251 | ||
9252 | Temporary_statement* temp = this->result(i); | |
cd238b8d | 9253 | if (temp == NULL) |
9254 | { | |
9255 | go_assert(saw_errors()); | |
9256 | return error_mark_node; | |
9257 | } | |
ceeb4318 | 9258 | Temporary_reference_expression* ref = |
9259 | Expression::make_temporary_reference(temp, loc); | |
9260 | ref->set_is_lvalue(); | |
9261 | tree temp_tree = ref->get_tree(context); | |
9262 | if (temp_tree == error_mark_node) | |
423d1705 | 9263 | return error_mark_node; |
ceeb4318 | 9264 | |
b13c66cd | 9265 | tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF, |
9266 | TREE_TYPE(field), call_tree, field, NULL_TREE); | |
9267 | tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR, | |
9268 | void_type_node, temp_tree, val_tree); | |
ceeb4318 | 9269 | |
9270 | append_to_statement_list(set_tree, &stmt_list); | |
9271 | } | |
9272 | go_assert(field == NULL_TREE); | |
9273 | ||
9274 | return save_expr(stmt_list); | |
9275 | } | |
9276 | ||
d751bb78 | 9277 | // Dump ast representation for a call expressin. |
9278 | ||
9279 | void | |
9280 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9281 | { | |
9282 | this->fn_->dump_expression(ast_dump_context); | |
9283 | ast_dump_context->ostream() << "("; | |
9284 | if (args_ != NULL) | |
9285 | ast_dump_context->dump_expression_list(this->args_); | |
9286 | ||
9287 | ast_dump_context->ostream() << ") "; | |
9288 | } | |
9289 | ||
e440a328 | 9290 | // Make a call expression. |
9291 | ||
9292 | Call_expression* | |
9293 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9294 | Location location) |
e440a328 | 9295 | { |
9296 | return new Call_expression(fn, args, is_varargs, location); | |
9297 | } | |
9298 | ||
9299 | // A single result from a call which returns multiple results. | |
9300 | ||
9301 | class Call_result_expression : public Expression | |
9302 | { | |
9303 | public: | |
9304 | Call_result_expression(Call_expression* call, unsigned int index) | |
9305 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
9306 | call_(call), index_(index) | |
9307 | { } | |
9308 | ||
9309 | protected: | |
9310 | int | |
9311 | do_traverse(Traverse*); | |
9312 | ||
9313 | Type* | |
9314 | do_type(); | |
9315 | ||
9316 | void | |
9317 | do_determine_type(const Type_context*); | |
9318 | ||
9319 | void | |
9320 | do_check_types(Gogo*); | |
9321 | ||
9322 | Expression* | |
9323 | do_copy() | |
9324 | { | |
9325 | return new Call_result_expression(this->call_->call_expression(), | |
9326 | this->index_); | |
9327 | } | |
9328 | ||
9329 | bool | |
9330 | do_must_eval_in_order() const | |
9331 | { return true; } | |
9332 | ||
9333 | tree | |
9334 | do_get_tree(Translate_context*); | |
9335 | ||
d751bb78 | 9336 | void |
9337 | do_dump_expression(Ast_dump_context*) const; | |
9338 | ||
e440a328 | 9339 | private: |
9340 | // The underlying call expression. | |
9341 | Expression* call_; | |
9342 | // Which result we want. | |
9343 | unsigned int index_; | |
9344 | }; | |
9345 | ||
9346 | // Traverse a call result. | |
9347 | ||
9348 | int | |
9349 | Call_result_expression::do_traverse(Traverse* traverse) | |
9350 | { | |
9351 | if (traverse->remember_expression(this->call_)) | |
9352 | { | |
9353 | // We have already traversed the call expression. | |
9354 | return TRAVERSE_CONTINUE; | |
9355 | } | |
9356 | return Expression::traverse(&this->call_, traverse); | |
9357 | } | |
9358 | ||
9359 | // Get the type. | |
9360 | ||
9361 | Type* | |
9362 | Call_result_expression::do_type() | |
9363 | { | |
425dd051 | 9364 | if (this->classification() == EXPRESSION_ERROR) |
9365 | return Type::make_error_type(); | |
9366 | ||
e440a328 | 9367 | // THIS->CALL_ can be replaced with a temporary reference due to |
9368 | // Call_expression::do_must_eval_in_order when there is an error. | |
9369 | Call_expression* ce = this->call_->call_expression(); | |
9370 | if (ce == NULL) | |
5e85f268 | 9371 | { |
9372 | this->set_is_error(); | |
9373 | return Type::make_error_type(); | |
9374 | } | |
e440a328 | 9375 | Function_type* fntype = ce->get_function_type(); |
9376 | if (fntype == NULL) | |
5e85f268 | 9377 | { |
e37658e2 | 9378 | if (ce->issue_error()) |
99b3f06f | 9379 | { |
9380 | if (!ce->fn()->type()->is_error()) | |
9381 | this->report_error(_("expected function")); | |
9382 | } | |
5e85f268 | 9383 | this->set_is_error(); |
9384 | return Type::make_error_type(); | |
9385 | } | |
e440a328 | 9386 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9387 | if (results == NULL || results->size() < 2) |
7b8d861f | 9388 | { |
ceeb4318 | 9389 | if (ce->issue_error()) |
9390 | this->report_error(_("number of results does not match " | |
9391 | "number of values")); | |
7b8d861f | 9392 | return Type::make_error_type(); |
9393 | } | |
e440a328 | 9394 | Typed_identifier_list::const_iterator pr = results->begin(); |
9395 | for (unsigned int i = 0; i < this->index_; ++i) | |
9396 | { | |
9397 | if (pr == results->end()) | |
425dd051 | 9398 | break; |
e440a328 | 9399 | ++pr; |
9400 | } | |
9401 | if (pr == results->end()) | |
425dd051 | 9402 | { |
ceeb4318 | 9403 | if (ce->issue_error()) |
9404 | this->report_error(_("number of results does not match " | |
9405 | "number of values")); | |
425dd051 | 9406 | return Type::make_error_type(); |
9407 | } | |
e440a328 | 9408 | return pr->type(); |
9409 | } | |
9410 | ||
425dd051 | 9411 | // Check the type. Just make sure that we trigger the warning in |
9412 | // do_type. | |
e440a328 | 9413 | |
9414 | void | |
9415 | Call_result_expression::do_check_types(Gogo*) | |
9416 | { | |
425dd051 | 9417 | this->type(); |
e440a328 | 9418 | } |
9419 | ||
9420 | // Determine the type. We have nothing to do here, but the 0 result | |
9421 | // needs to pass down to the caller. | |
9422 | ||
9423 | void | |
9424 | Call_result_expression::do_determine_type(const Type_context*) | |
9425 | { | |
fb94b0ca | 9426 | this->call_->determine_type_no_context(); |
e440a328 | 9427 | } |
9428 | ||
ceeb4318 | 9429 | // Return the tree. We just refer to the temporary set by the call |
9430 | // expression. We don't do this at lowering time because it makes it | |
9431 | // hard to evaluate the call at the right time. | |
e440a328 | 9432 | |
9433 | tree | |
9434 | Call_result_expression::do_get_tree(Translate_context* context) | |
9435 | { | |
ceeb4318 | 9436 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 9437 | if (ce == NULL) |
9438 | { | |
9439 | go_assert(this->call_->is_error_expression()); | |
9440 | return error_mark_node; | |
9441 | } | |
ceeb4318 | 9442 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 9443 | if (ts == NULL) |
9444 | { | |
9445 | go_assert(saw_errors()); | |
9446 | return error_mark_node; | |
9447 | } | |
ceeb4318 | 9448 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
9449 | return ref->get_tree(context); | |
e440a328 | 9450 | } |
9451 | ||
d751bb78 | 9452 | // Dump ast representation for a call result expression. |
9453 | ||
9454 | void | |
9455 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9456 | const | |
9457 | { | |
9458 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
9459 | // (struct) and the fields are referenced instead. | |
9460 | ast_dump_context->ostream() << this->index_ << "@("; | |
9461 | ast_dump_context->dump_expression(this->call_); | |
9462 | ast_dump_context->ostream() << ")"; | |
9463 | } | |
9464 | ||
e440a328 | 9465 | // Make a reference to a single result of a call which returns |
9466 | // multiple results. | |
9467 | ||
9468 | Expression* | |
9469 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9470 | { | |
9471 | return new Call_result_expression(call, index); | |
9472 | } | |
9473 | ||
9474 | // Class Index_expression. | |
9475 | ||
9476 | // Traversal. | |
9477 | ||
9478 | int | |
9479 | Index_expression::do_traverse(Traverse* traverse) | |
9480 | { | |
9481 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9482 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9483 | || (this->end_ != NULL | |
9484 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
9485 | return TRAVERSE_EXIT; | |
9486 | return TRAVERSE_CONTINUE; | |
9487 | } | |
9488 | ||
9489 | // Lower an index expression. This converts the generic index | |
9490 | // expression into an array index, a string index, or a map index. | |
9491 | ||
9492 | Expression* | |
ceeb4318 | 9493 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 9494 | { |
b13c66cd | 9495 | Location location = this->location(); |
e440a328 | 9496 | Expression* left = this->left_; |
9497 | Expression* start = this->start_; | |
9498 | Expression* end = this->end_; | |
9499 | ||
9500 | Type* type = left->type(); | |
5c13bd80 | 9501 | if (type->is_error()) |
e440a328 | 9502 | return Expression::make_error(location); |
b0cf7ddd | 9503 | else if (left->is_type_expression()) |
9504 | { | |
9505 | error_at(location, "attempt to index type expression"); | |
9506 | return Expression::make_error(location); | |
9507 | } | |
e440a328 | 9508 | else if (type->array_type() != NULL) |
9509 | return Expression::make_array_index(left, start, end, location); | |
9510 | else if (type->points_to() != NULL | |
9511 | && type->points_to()->array_type() != NULL | |
411eb89e | 9512 | && !type->points_to()->is_slice_type()) |
e440a328 | 9513 | { |
9514 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9515 | location); | |
9516 | return Expression::make_array_index(deref, start, end, location); | |
9517 | } | |
9518 | else if (type->is_string_type()) | |
9519 | return Expression::make_string_index(left, start, end, location); | |
9520 | else if (type->map_type() != NULL) | |
9521 | { | |
9522 | if (end != NULL) | |
9523 | { | |
9524 | error_at(location, "invalid slice of map"); | |
9525 | return Expression::make_error(location); | |
9526 | } | |
6d4c2432 | 9527 | Map_index_expression* ret = Expression::make_map_index(left, start, |
9528 | location); | |
e440a328 | 9529 | if (this->is_lvalue_) |
9530 | ret->set_is_lvalue(); | |
9531 | return ret; | |
9532 | } | |
9533 | else | |
9534 | { | |
9535 | error_at(location, | |
9536 | "attempt to index object which is not array, string, or map"); | |
9537 | return Expression::make_error(location); | |
9538 | } | |
9539 | } | |
9540 | ||
d751bb78 | 9541 | // Write an indexed expression (expr[expr:expr] or expr[expr]) to a |
9542 | // dump context | |
9543 | ||
9544 | void | |
9545 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
9546 | const Expression* expr, | |
9547 | const Expression* start, | |
9548 | const Expression* end) | |
9549 | { | |
9550 | expr->dump_expression(ast_dump_context); | |
9551 | ast_dump_context->ostream() << "["; | |
9552 | start->dump_expression(ast_dump_context); | |
9553 | if (end != NULL) | |
9554 | { | |
9555 | ast_dump_context->ostream() << ":"; | |
9556 | end->dump_expression(ast_dump_context); | |
9557 | } | |
9558 | ast_dump_context->ostream() << "]"; | |
9559 | } | |
9560 | ||
9561 | // Dump ast representation for an index expression. | |
9562 | ||
9563 | void | |
9564 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
9565 | const | |
9566 | { | |
9567 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
9568 | this->start_, this->end_); | |
9569 | } | |
9570 | ||
e440a328 | 9571 | // Make an index expression. |
9572 | ||
9573 | Expression* | |
9574 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
b13c66cd | 9575 | Location location) |
e440a328 | 9576 | { |
9577 | return new Index_expression(left, start, end, location); | |
9578 | } | |
9579 | ||
9580 | // An array index. This is used for both indexing and slicing. | |
9581 | ||
9582 | class Array_index_expression : public Expression | |
9583 | { | |
9584 | public: | |
9585 | Array_index_expression(Expression* array, Expression* start, | |
b13c66cd | 9586 | Expression* end, Location location) |
e440a328 | 9587 | : Expression(EXPRESSION_ARRAY_INDEX, location), |
9588 | array_(array), start_(start), end_(end), type_(NULL) | |
9589 | { } | |
9590 | ||
9591 | protected: | |
9592 | int | |
9593 | do_traverse(Traverse*); | |
9594 | ||
9595 | Type* | |
9596 | do_type(); | |
9597 | ||
9598 | void | |
9599 | do_determine_type(const Type_context*); | |
9600 | ||
9601 | void | |
9602 | do_check_types(Gogo*); | |
9603 | ||
9604 | Expression* | |
9605 | do_copy() | |
9606 | { | |
9607 | return Expression::make_array_index(this->array_->copy(), | |
9608 | this->start_->copy(), | |
9609 | (this->end_ == NULL | |
9610 | ? NULL | |
9611 | : this->end_->copy()), | |
9612 | this->location()); | |
9613 | } | |
9614 | ||
baef9f7a | 9615 | bool |
9616 | do_must_eval_subexpressions_in_order(int* skip) const | |
9617 | { | |
9618 | *skip = 1; | |
9619 | return true; | |
9620 | } | |
9621 | ||
e440a328 | 9622 | bool |
9623 | do_is_addressable() const; | |
9624 | ||
9625 | void | |
9626 | do_address_taken(bool escapes) | |
9627 | { this->array_->address_taken(escapes); } | |
9628 | ||
9629 | tree | |
9630 | do_get_tree(Translate_context*); | |
9631 | ||
d751bb78 | 9632 | void |
9633 | do_dump_expression(Ast_dump_context*) const; | |
9634 | ||
e440a328 | 9635 | private: |
9636 | // The array we are getting a value from. | |
9637 | Expression* array_; | |
9638 | // The start or only index. | |
9639 | Expression* start_; | |
9640 | // The end index of a slice. This may be NULL for a simple array | |
9641 | // index, or it may be a nil expression for the length of the array. | |
9642 | Expression* end_; | |
9643 | // The type of the expression. | |
9644 | Type* type_; | |
9645 | }; | |
9646 | ||
9647 | // Array index traversal. | |
9648 | ||
9649 | int | |
9650 | Array_index_expression::do_traverse(Traverse* traverse) | |
9651 | { | |
9652 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9653 | return TRAVERSE_EXIT; | |
9654 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9655 | return TRAVERSE_EXIT; | |
9656 | if (this->end_ != NULL) | |
9657 | { | |
9658 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9659 | return TRAVERSE_EXIT; | |
9660 | } | |
9661 | return TRAVERSE_CONTINUE; | |
9662 | } | |
9663 | ||
9664 | // Return the type of an array index. | |
9665 | ||
9666 | Type* | |
9667 | Array_index_expression::do_type() | |
9668 | { | |
9669 | if (this->type_ == NULL) | |
9670 | { | |
9671 | Array_type* type = this->array_->type()->array_type(); | |
9672 | if (type == NULL) | |
9673 | this->type_ = Type::make_error_type(); | |
9674 | else if (this->end_ == NULL) | |
9675 | this->type_ = type->element_type(); | |
411eb89e | 9676 | else if (type->is_slice_type()) |
e440a328 | 9677 | { |
9678 | // A slice of a slice has the same type as the original | |
9679 | // slice. | |
9680 | this->type_ = this->array_->type()->deref(); | |
9681 | } | |
9682 | else | |
9683 | { | |
9684 | // A slice of an array is a slice. | |
9685 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
9686 | } | |
9687 | } | |
9688 | return this->type_; | |
9689 | } | |
9690 | ||
9691 | // Set the type of an array index. | |
9692 | ||
9693 | void | |
9694 | Array_index_expression::do_determine_type(const Type_context*) | |
9695 | { | |
9696 | this->array_->determine_type_no_context(); | |
7917ad68 | 9697 | this->start_->determine_type_no_context(); |
e440a328 | 9698 | if (this->end_ != NULL) |
7917ad68 | 9699 | this->end_->determine_type_no_context(); |
e440a328 | 9700 | } |
9701 | ||
9702 | // Check types of an array index. | |
9703 | ||
9704 | void | |
9705 | Array_index_expression::do_check_types(Gogo*) | |
9706 | { | |
9707 | if (this->start_->type()->integer_type() == NULL) | |
9708 | this->report_error(_("index must be integer")); | |
9709 | if (this->end_ != NULL | |
9710 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 9711 | && !this->end_->type()->is_error() |
9712 | && !this->end_->is_nil_expression() | |
9713 | && !this->end_->is_error_expression()) | |
e440a328 | 9714 | this->report_error(_("slice end must be integer")); |
9715 | ||
9716 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 9717 | if (array_type == NULL) |
9718 | { | |
c484d925 | 9719 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 9720 | return; |
9721 | } | |
e440a328 | 9722 | |
9723 | unsigned int int_bits = | |
9724 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
9725 | ||
0c77715b | 9726 | Numeric_constant lvalnc; |
e440a328 | 9727 | mpz_t lval; |
e440a328 | 9728 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 9729 | && array_type->length()->numeric_constant_value(&lvalnc) |
9730 | && lvalnc.to_int(&lval)); | |
9731 | Numeric_constant inc; | |
e440a328 | 9732 | mpz_t ival; |
0c77715b | 9733 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 9734 | { |
9735 | if (mpz_sgn(ival) < 0 | |
9736 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9737 | || (lval_valid | |
9738 | && (this->end_ == NULL | |
9739 | ? mpz_cmp(ival, lval) >= 0 | |
9740 | : mpz_cmp(ival, lval) > 0))) | |
9741 | { | |
9742 | error_at(this->start_->location(), "array index out of bounds"); | |
9743 | this->set_is_error(); | |
9744 | } | |
0c77715b | 9745 | mpz_clear(ival); |
e440a328 | 9746 | } |
9747 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9748 | { | |
0c77715b | 9749 | Numeric_constant enc; |
9750 | mpz_t eval; | |
9751 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 9752 | { |
0c77715b | 9753 | if (mpz_sgn(eval) < 0 |
9754 | || mpz_sizeinbase(eval, 2) >= int_bits | |
9755 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 9756 | { |
9757 | error_at(this->end_->location(), "array index out of bounds"); | |
9758 | this->set_is_error(); | |
9759 | } | |
0c77715b | 9760 | mpz_clear(eval); |
e440a328 | 9761 | } |
9762 | } | |
0c77715b | 9763 | if (lval_valid) |
9764 | mpz_clear(lval); | |
e440a328 | 9765 | |
9766 | // A slice of an array requires an addressable array. A slice of a | |
9767 | // slice is always possible. | |
411eb89e | 9768 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 9769 | { |
9770 | if (!this->array_->is_addressable()) | |
8da39c3b | 9771 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 9772 | else |
9773 | this->array_->address_taken(true); | |
9774 | } | |
e440a328 | 9775 | } |
9776 | ||
9777 | // Return whether this expression is addressable. | |
9778 | ||
9779 | bool | |
9780 | Array_index_expression::do_is_addressable() const | |
9781 | { | |
9782 | // A slice expression is not addressable. | |
9783 | if (this->end_ != NULL) | |
9784 | return false; | |
9785 | ||
9786 | // An index into a slice is addressable. | |
411eb89e | 9787 | if (this->array_->type()->is_slice_type()) |
e440a328 | 9788 | return true; |
9789 | ||
9790 | // An index into an array is addressable if the array is | |
9791 | // addressable. | |
9792 | return this->array_->is_addressable(); | |
9793 | } | |
9794 | ||
9795 | // Get a tree for an array index. | |
9796 | ||
9797 | tree | |
9798 | Array_index_expression::do_get_tree(Translate_context* context) | |
9799 | { | |
9800 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9801 | Location loc = this->location(); |
e440a328 | 9802 | |
9803 | Array_type* array_type = this->array_->type()->array_type(); | |
d8cd8e2d | 9804 | if (array_type == NULL) |
9805 | { | |
c484d925 | 9806 | go_assert(this->array_->type()->is_error()); |
d8cd8e2d | 9807 | return error_mark_node; |
9808 | } | |
e440a328 | 9809 | |
9f0e0513 | 9810 | tree type_tree = type_to_tree(array_type->get_backend(gogo)); |
c65212a0 | 9811 | if (type_tree == error_mark_node) |
9812 | return error_mark_node; | |
e440a328 | 9813 | |
9814 | tree array_tree = this->array_->get_tree(context); | |
9815 | if (array_tree == error_mark_node) | |
9816 | return error_mark_node; | |
9817 | ||
9818 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
9819 | array_tree = save_expr(array_tree); | |
a04bfdfc | 9820 | |
9821 | tree length_tree = NULL_TREE; | |
9822 | if (this->end_ == NULL || this->end_->is_nil_expression()) | |
9823 | { | |
9824 | length_tree = array_type->length_tree(gogo, array_tree); | |
9825 | if (length_tree == error_mark_node) | |
9826 | return error_mark_node; | |
9827 | length_tree = save_expr(length_tree); | |
9828 | } | |
9829 | ||
9830 | tree capacity_tree = NULL_TREE; | |
9831 | if (this->end_ != NULL) | |
9832 | { | |
9833 | capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
9834 | if (capacity_tree == error_mark_node) | |
9835 | return error_mark_node; | |
9836 | capacity_tree = save_expr(capacity_tree); | |
9837 | } | |
9838 | ||
9839 | tree length_type = (length_tree != NULL_TREE | |
9840 | ? TREE_TYPE(length_tree) | |
9841 | : TREE_TYPE(capacity_tree)); | |
e440a328 | 9842 | |
9843 | tree bad_index = boolean_false_node; | |
9844 | ||
9845 | tree start_tree = this->start_->get_tree(context); | |
9846 | if (start_tree == error_mark_node) | |
9847 | return error_mark_node; | |
9848 | if (!DECL_P(start_tree)) | |
9849 | start_tree = save_expr(start_tree); | |
9850 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9851 | start_tree = convert_to_integer(length_type, start_tree); | |
9852 | ||
9853 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9854 | loc); | |
9855 | ||
b13c66cd | 9856 | start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree); |
9857 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, | |
9858 | boolean_type_node, bad_index, | |
9859 | fold_build2_loc(loc.gcc_location(), | |
e440a328 | 9860 | (this->end_ == NULL |
9861 | ? GE_EXPR | |
9862 | : GT_EXPR), | |
9863 | boolean_type_node, start_tree, | |
a04bfdfc | 9864 | (this->end_ == NULL |
9865 | ? length_tree | |
9866 | : capacity_tree))); | |
e440a328 | 9867 | |
9868 | int code = (array_type->length() != NULL | |
9869 | ? (this->end_ == NULL | |
9870 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
9871 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
9872 | : (this->end_ == NULL | |
9873 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
9874 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
9875 | tree crash = Gogo::runtime_error(code, loc); | |
9876 | ||
9877 | if (this->end_ == NULL) | |
9878 | { | |
9879 | // Simple array indexing. This has to return an l-value, so | |
9880 | // wrap the index check into START_TREE. | |
9881 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
9882 | build3(COND_EXPR, void_type_node, | |
9883 | bad_index, crash, NULL_TREE), | |
9884 | start_tree); | |
b13c66cd | 9885 | start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree); |
e440a328 | 9886 | |
9887 | if (array_type->length() != NULL) | |
9888 | { | |
9889 | // Fixed array. | |
9890 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
9891 | start_tree, NULL_TREE, NULL_TREE); | |
9892 | } | |
9893 | else | |
9894 | { | |
9895 | // Open array. | |
9896 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9f0e0513 | 9897 | Type* element_type = array_type->element_type(); |
9898 | Btype* belement_type = element_type->get_backend(gogo); | |
9899 | tree element_type_tree = type_to_tree(belement_type); | |
c65212a0 | 9900 | if (element_type_tree == error_mark_node) |
9901 | return error_mark_node; | |
e440a328 | 9902 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
b13c66cd | 9903 | tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype, |
e440a328 | 9904 | start_tree, element_size); |
b13c66cd | 9905 | tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
e440a328 | 9906 | TREE_TYPE(values), values, offset); |
9907 | return build_fold_indirect_ref(ptr); | |
9908 | } | |
9909 | } | |
9910 | ||
9911 | // Array slice. | |
9912 | ||
e440a328 | 9913 | tree end_tree; |
9914 | if (this->end_->is_nil_expression()) | |
9915 | end_tree = length_tree; | |
9916 | else | |
9917 | { | |
9918 | end_tree = this->end_->get_tree(context); | |
9919 | if (end_tree == error_mark_node) | |
9920 | return error_mark_node; | |
9921 | if (!DECL_P(end_tree)) | |
9922 | end_tree = save_expr(end_tree); | |
9923 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9924 | end_tree = convert_to_integer(length_type, end_tree); | |
9925 | ||
9926 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
9927 | loc); | |
9928 | ||
b13c66cd | 9929 | end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree); |
e440a328 | 9930 | |
b13c66cd | 9931 | tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
9932 | boolean_type_node, | |
9933 | fold_build2_loc(loc.gcc_location(), | |
9934 | LT_EXPR, boolean_type_node, | |
e440a328 | 9935 | end_tree, start_tree), |
b13c66cd | 9936 | fold_build2_loc(loc.gcc_location(), |
9937 | GT_EXPR, boolean_type_node, | |
e440a328 | 9938 | end_tree, capacity_tree)); |
b13c66cd | 9939 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
9940 | boolean_type_node, bad_index, bad_end); | |
e440a328 | 9941 | } |
9942 | ||
9f0e0513 | 9943 | Type* element_type = array_type->element_type(); |
9944 | tree element_type_tree = type_to_tree(element_type->get_backend(gogo)); | |
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); |
9948 | ||
b13c66cd | 9949 | tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype, |
9950 | fold_convert_loc(loc.gcc_location(), sizetype, | |
9951 | start_tree), | |
e440a328 | 9952 | element_size); |
9953 | ||
9954 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
c65212a0 | 9955 | if (value_pointer == error_mark_node) |
9956 | return error_mark_node; | |
e440a328 | 9957 | |
b13c66cd | 9958 | value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
e440a328 | 9959 | TREE_TYPE(value_pointer), |
9960 | value_pointer, offset); | |
9961 | ||
b13c66cd | 9962 | tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR, |
9963 | length_type, end_tree, start_tree); | |
e440a328 | 9964 | |
b13c66cd | 9965 | tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR, |
9966 | length_type, capacity_tree, | |
9967 | start_tree); | |
e440a328 | 9968 | |
9f0e0513 | 9969 | tree struct_tree = type_to_tree(this->type()->get_backend(gogo)); |
c484d925 | 9970 | go_assert(TREE_CODE(struct_tree) == RECORD_TYPE); |
e440a328 | 9971 | |
9972 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
9973 | ||
e82e4eb5 | 9974 | constructor_elt empty = {NULL, NULL}; |
9975 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, empty); | |
e440a328 | 9976 | tree field = TYPE_FIELDS(struct_tree); |
c484d925 | 9977 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 9978 | elt->index = field; |
9979 | elt->value = value_pointer; | |
9980 | ||
e82e4eb5 | 9981 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 9982 | field = DECL_CHAIN(field); |
c484d925 | 9983 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 9984 | elt->index = field; |
b13c66cd | 9985 | elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field), |
9986 | result_length_tree); | |
e440a328 | 9987 | |
e82e4eb5 | 9988 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 9989 | field = DECL_CHAIN(field); |
c484d925 | 9990 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); |
e440a328 | 9991 | elt->index = field; |
b13c66cd | 9992 | elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field), |
9993 | result_capacity_tree); | |
e440a328 | 9994 | |
9995 | tree constructor = build_constructor(struct_tree, init); | |
9996 | ||
9997 | if (TREE_CONSTANT(value_pointer) | |
9998 | && TREE_CONSTANT(result_length_tree) | |
9999 | && TREE_CONSTANT(result_capacity_tree)) | |
10000 | TREE_CONSTANT(constructor) = 1; | |
10001 | ||
b13c66cd | 10002 | return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, |
10003 | TREE_TYPE(constructor), | |
e440a328 | 10004 | build3(COND_EXPR, void_type_node, |
10005 | bad_index, crash, NULL_TREE), | |
10006 | constructor); | |
10007 | } | |
10008 | ||
d751bb78 | 10009 | // Dump ast representation for an array index expression. |
10010 | ||
10011 | void | |
10012 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10013 | const | |
10014 | { | |
10015 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
10016 | this->start_, this->end_); | |
10017 | } | |
10018 | ||
e440a328 | 10019 | // Make an array index expression. END may be NULL. |
10020 | ||
10021 | Expression* | |
10022 | Expression::make_array_index(Expression* array, Expression* start, | |
b13c66cd | 10023 | Expression* end, Location location) |
e440a328 | 10024 | { |
e440a328 | 10025 | return new Array_index_expression(array, start, end, location); |
10026 | } | |
10027 | ||
10028 | // A string index. This is used for both indexing and slicing. | |
10029 | ||
10030 | class String_index_expression : public Expression | |
10031 | { | |
10032 | public: | |
10033 | String_index_expression(Expression* string, Expression* start, | |
b13c66cd | 10034 | Expression* end, Location location) |
e440a328 | 10035 | : Expression(EXPRESSION_STRING_INDEX, location), |
10036 | string_(string), start_(start), end_(end) | |
10037 | { } | |
10038 | ||
10039 | protected: | |
10040 | int | |
10041 | do_traverse(Traverse*); | |
10042 | ||
10043 | Type* | |
10044 | do_type(); | |
10045 | ||
10046 | void | |
10047 | do_determine_type(const Type_context*); | |
10048 | ||
10049 | void | |
10050 | do_check_types(Gogo*); | |
10051 | ||
10052 | Expression* | |
10053 | do_copy() | |
10054 | { | |
10055 | return Expression::make_string_index(this->string_->copy(), | |
10056 | this->start_->copy(), | |
10057 | (this->end_ == NULL | |
10058 | ? NULL | |
10059 | : this->end_->copy()), | |
10060 | this->location()); | |
10061 | } | |
10062 | ||
baef9f7a | 10063 | bool |
10064 | do_must_eval_subexpressions_in_order(int* skip) const | |
10065 | { | |
10066 | *skip = 1; | |
10067 | return true; | |
10068 | } | |
10069 | ||
e440a328 | 10070 | tree |
10071 | do_get_tree(Translate_context*); | |
10072 | ||
d751bb78 | 10073 | void |
10074 | do_dump_expression(Ast_dump_context*) const; | |
10075 | ||
e440a328 | 10076 | private: |
10077 | // The string we are getting a value from. | |
10078 | Expression* string_; | |
10079 | // The start or only index. | |
10080 | Expression* start_; | |
10081 | // The end index of a slice. This may be NULL for a single index, | |
10082 | // or it may be a nil expression for the length of the string. | |
10083 | Expression* end_; | |
10084 | }; | |
10085 | ||
10086 | // String index traversal. | |
10087 | ||
10088 | int | |
10089 | String_index_expression::do_traverse(Traverse* traverse) | |
10090 | { | |
10091 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10092 | return TRAVERSE_EXIT; | |
10093 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10094 | return TRAVERSE_EXIT; | |
10095 | if (this->end_ != NULL) | |
10096 | { | |
10097 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10098 | return TRAVERSE_EXIT; | |
10099 | } | |
10100 | return TRAVERSE_CONTINUE; | |
10101 | } | |
10102 | ||
10103 | // Return the type of a string index. | |
10104 | ||
10105 | Type* | |
10106 | String_index_expression::do_type() | |
10107 | { | |
10108 | if (this->end_ == NULL) | |
10109 | return Type::lookup_integer_type("uint8"); | |
10110 | else | |
7672d35f | 10111 | return this->string_->type(); |
e440a328 | 10112 | } |
10113 | ||
10114 | // Determine the type of a string index. | |
10115 | ||
10116 | void | |
10117 | String_index_expression::do_determine_type(const Type_context*) | |
10118 | { | |
10119 | this->string_->determine_type_no_context(); | |
93000773 | 10120 | this->start_->determine_type_no_context(); |
e440a328 | 10121 | if (this->end_ != NULL) |
93000773 | 10122 | this->end_->determine_type_no_context(); |
e440a328 | 10123 | } |
10124 | ||
10125 | // Check types of a string index. | |
10126 | ||
10127 | void | |
10128 | String_index_expression::do_check_types(Gogo*) | |
10129 | { | |
10130 | if (this->start_->type()->integer_type() == NULL) | |
10131 | this->report_error(_("index must be integer")); | |
10132 | if (this->end_ != NULL | |
10133 | && this->end_->type()->integer_type() == NULL | |
10134 | && !this->end_->is_nil_expression()) | |
10135 | this->report_error(_("slice end must be integer")); | |
10136 | ||
10137 | std::string sval; | |
10138 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10139 | ||
0c77715b | 10140 | Numeric_constant inc; |
e440a328 | 10141 | mpz_t ival; |
0c77715b | 10142 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10143 | { |
10144 | if (mpz_sgn(ival) < 0 | |
10145 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
10146 | { | |
10147 | error_at(this->start_->location(), "string index out of bounds"); | |
10148 | this->set_is_error(); | |
10149 | } | |
0c77715b | 10150 | mpz_clear(ival); |
e440a328 | 10151 | } |
10152 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10153 | { | |
0c77715b | 10154 | Numeric_constant enc; |
10155 | mpz_t eval; | |
10156 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10157 | { |
0c77715b | 10158 | if (mpz_sgn(eval) < 0 |
10159 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10160 | { |
10161 | error_at(this->end_->location(), "string index out of bounds"); | |
10162 | this->set_is_error(); | |
10163 | } | |
0c77715b | 10164 | mpz_clear(eval); |
e440a328 | 10165 | } |
10166 | } | |
e440a328 | 10167 | } |
10168 | ||
10169 | // Get a tree for a string index. | |
10170 | ||
10171 | tree | |
10172 | String_index_expression::do_get_tree(Translate_context* context) | |
10173 | { | |
b13c66cd | 10174 | Location loc = this->location(); |
e440a328 | 10175 | |
10176 | tree string_tree = this->string_->get_tree(context); | |
10177 | if (string_tree == error_mark_node) | |
10178 | return error_mark_node; | |
10179 | ||
10180 | if (this->string_->type()->points_to() != NULL) | |
10181 | string_tree = build_fold_indirect_ref(string_tree); | |
10182 | if (!DECL_P(string_tree)) | |
10183 | string_tree = save_expr(string_tree); | |
10184 | tree string_type = TREE_TYPE(string_tree); | |
10185 | ||
10186 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
10187 | length_tree = save_expr(length_tree); | |
10188 | tree length_type = TREE_TYPE(length_tree); | |
10189 | ||
10190 | tree bad_index = boolean_false_node; | |
10191 | ||
10192 | tree start_tree = this->start_->get_tree(context); | |
10193 | if (start_tree == error_mark_node) | |
10194 | return error_mark_node; | |
10195 | if (!DECL_P(start_tree)) | |
10196 | start_tree = save_expr(start_tree); | |
10197 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
10198 | start_tree = convert_to_integer(length_type, start_tree); | |
10199 | ||
10200 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
10201 | loc); | |
10202 | ||
b13c66cd | 10203 | start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree); |
e440a328 | 10204 | |
10205 | int code = (this->end_ == NULL | |
10206 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10207 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
10208 | tree crash = Gogo::runtime_error(code, loc); | |
10209 | ||
10210 | if (this->end_ == NULL) | |
10211 | { | |
b13c66cd | 10212 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
10213 | boolean_type_node, bad_index, | |
10214 | fold_build2_loc(loc.gcc_location(), GE_EXPR, | |
e440a328 | 10215 | boolean_type_node, |
10216 | start_tree, length_tree)); | |
10217 | ||
10218 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
b13c66cd | 10219 | tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
10220 | TREE_TYPE(bytes_tree), | |
e440a328 | 10221 | bytes_tree, |
b13c66cd | 10222 | fold_convert_loc(loc.gcc_location(), sizetype, |
10223 | start_tree)); | |
10224 | tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr); | |
e440a328 | 10225 | |
10226 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
10227 | build3(COND_EXPR, void_type_node, | |
10228 | bad_index, crash, NULL_TREE), | |
10229 | index); | |
10230 | } | |
10231 | else | |
10232 | { | |
10233 | tree end_tree; | |
10234 | if (this->end_->is_nil_expression()) | |
10235 | end_tree = build_int_cst(length_type, -1); | |
10236 | else | |
10237 | { | |
10238 | end_tree = this->end_->get_tree(context); | |
10239 | if (end_tree == error_mark_node) | |
10240 | return error_mark_node; | |
10241 | if (!DECL_P(end_tree)) | |
10242 | end_tree = save_expr(end_tree); | |
10243 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
10244 | end_tree = convert_to_integer(length_type, end_tree); | |
10245 | ||
10246 | bad_index = Expression::check_bounds(end_tree, length_type, | |
10247 | bad_index, loc); | |
10248 | ||
b13c66cd | 10249 | end_tree = fold_convert_loc(loc.gcc_location(), length_type, |
10250 | end_tree); | |
e440a328 | 10251 | } |
10252 | ||
10253 | static tree strslice_fndecl; | |
10254 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
10255 | loc, | |
10256 | "__go_string_slice", | |
10257 | 3, | |
10258 | string_type, | |
10259 | string_type, | |
10260 | string_tree, | |
10261 | length_type, | |
10262 | start_tree, | |
10263 | length_type, | |
10264 | end_tree); | |
5fb82b5e | 10265 | if (ret == error_mark_node) |
10266 | return error_mark_node; | |
e440a328 | 10267 | // This will panic if the bounds are out of range for the |
10268 | // string. | |
10269 | TREE_NOTHROW(strslice_fndecl) = 0; | |
10270 | ||
10271 | if (bad_index == boolean_false_node) | |
10272 | return ret; | |
10273 | else | |
10274 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
10275 | build3(COND_EXPR, void_type_node, | |
10276 | bad_index, crash, NULL_TREE), | |
10277 | ret); | |
10278 | } | |
10279 | } | |
10280 | ||
d751bb78 | 10281 | // Dump ast representation for a string index expression. |
10282 | ||
10283 | void | |
10284 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10285 | const | |
10286 | { | |
10287 | Index_expression::dump_index_expression(ast_dump_context, this->string_, | |
10288 | this->start_, this->end_); | |
10289 | } | |
10290 | ||
e440a328 | 10291 | // Make a string index expression. END may be NULL. |
10292 | ||
10293 | Expression* | |
10294 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10295 | Expression* end, Location location) |
e440a328 | 10296 | { |
10297 | return new String_index_expression(string, start, end, location); | |
10298 | } | |
10299 | ||
10300 | // Class Map_index. | |
10301 | ||
10302 | // Get the type of the map. | |
10303 | ||
10304 | Map_type* | |
10305 | Map_index_expression::get_map_type() const | |
10306 | { | |
10307 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 10308 | if (mt == NULL) |
c484d925 | 10309 | go_assert(saw_errors()); |
e440a328 | 10310 | return mt; |
10311 | } | |
10312 | ||
10313 | // Map index traversal. | |
10314 | ||
10315 | int | |
10316 | Map_index_expression::do_traverse(Traverse* traverse) | |
10317 | { | |
10318 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
10319 | return TRAVERSE_EXIT; | |
10320 | return Expression::traverse(&this->index_, traverse); | |
10321 | } | |
10322 | ||
10323 | // Return the type of a map index. | |
10324 | ||
10325 | Type* | |
10326 | Map_index_expression::do_type() | |
10327 | { | |
c7524fae | 10328 | Map_type* mt = this->get_map_type(); |
10329 | if (mt == NULL) | |
10330 | return Type::make_error_type(); | |
10331 | Type* type = mt->val_type(); | |
e440a328 | 10332 | // If this map index is in a tuple assignment, we actually return a |
10333 | // pointer to the value type. Tuple_map_assignment_statement is | |
10334 | // responsible for handling this correctly. We need to get the type | |
10335 | // right in case this gets assigned to a temporary variable. | |
10336 | if (this->is_in_tuple_assignment_) | |
10337 | type = Type::make_pointer_type(type); | |
10338 | return type; | |
10339 | } | |
10340 | ||
10341 | // Fix the type of a map index. | |
10342 | ||
10343 | void | |
10344 | Map_index_expression::do_determine_type(const Type_context*) | |
10345 | { | |
10346 | this->map_->determine_type_no_context(); | |
c7524fae | 10347 | Map_type* mt = this->get_map_type(); |
10348 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
10349 | Type_context subcontext(key_type, false); | |
e440a328 | 10350 | this->index_->determine_type(&subcontext); |
10351 | } | |
10352 | ||
10353 | // Check types of a map index. | |
10354 | ||
10355 | void | |
10356 | Map_index_expression::do_check_types(Gogo*) | |
10357 | { | |
10358 | std::string reason; | |
c7524fae | 10359 | Map_type* mt = this->get_map_type(); |
10360 | if (mt == NULL) | |
10361 | return; | |
10362 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 10363 | { |
10364 | if (reason.empty()) | |
10365 | this->report_error(_("incompatible type for map index")); | |
10366 | else | |
10367 | { | |
10368 | error_at(this->location(), "incompatible type for map index (%s)", | |
10369 | reason.c_str()); | |
10370 | this->set_is_error(); | |
10371 | } | |
10372 | } | |
10373 | } | |
10374 | ||
10375 | // Get a tree for a map index. | |
10376 | ||
10377 | tree | |
10378 | Map_index_expression::do_get_tree(Translate_context* context) | |
10379 | { | |
10380 | Map_type* type = this->get_map_type(); | |
c7524fae | 10381 | if (type == NULL) |
10382 | return error_mark_node; | |
e440a328 | 10383 | |
10384 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
10385 | if (valptr == error_mark_node) | |
10386 | return error_mark_node; | |
10387 | valptr = save_expr(valptr); | |
10388 | ||
10389 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
10390 | ||
10391 | if (this->is_lvalue_) | |
10392 | return build_fold_indirect_ref(valptr); | |
10393 | else if (this->is_in_tuple_assignment_) | |
10394 | { | |
10395 | // Tuple_map_assignment_statement is responsible for using this | |
10396 | // appropriately. | |
10397 | return valptr; | |
10398 | } | |
10399 | else | |
10400 | { | |
63697958 | 10401 | Gogo* gogo = context->gogo(); |
10402 | Btype* val_btype = type->val_type()->get_backend(gogo); | |
10403 | Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); | |
e440a328 | 10404 | return fold_build3(COND_EXPR, val_type_tree, |
10405 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
10406 | fold_convert(TREE_TYPE(valptr), | |
10407 | null_pointer_node)), | |
63697958 | 10408 | expr_to_tree(val_zero), |
e440a328 | 10409 | build_fold_indirect_ref(valptr)); |
10410 | } | |
10411 | } | |
10412 | ||
10413 | // Get a tree for the map index. This returns a tree which evaluates | |
10414 | // to a pointer to a value. The pointer will be NULL if the key is | |
10415 | // not in the map. | |
10416 | ||
10417 | tree | |
10418 | Map_index_expression::get_value_pointer(Translate_context* context, | |
10419 | bool insert) | |
10420 | { | |
10421 | Map_type* type = this->get_map_type(); | |
c7524fae | 10422 | if (type == NULL) |
10423 | return error_mark_node; | |
e440a328 | 10424 | |
10425 | tree map_tree = this->map_->get_tree(context); | |
10426 | tree index_tree = this->index_->get_tree(context); | |
10427 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
10428 | this->index_->type(), | |
10429 | index_tree, | |
10430 | this->location()); | |
10431 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
10432 | return error_mark_node; | |
10433 | ||
10434 | if (this->map_->type()->points_to() != NULL) | |
10435 | map_tree = build_fold_indirect_ref(map_tree); | |
10436 | ||
10437 | // We need to pass in a pointer to the key, so stuff it into a | |
10438 | // variable. | |
746d2e73 | 10439 | tree tmp; |
10440 | tree make_tmp; | |
10441 | if (current_function_decl != NULL) | |
10442 | { | |
10443 | tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
10444 | DECL_IGNORED_P(tmp) = 0; | |
10445 | DECL_INITIAL(tmp) = index_tree; | |
10446 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
10447 | TREE_ADDRESSABLE(tmp) = 1; | |
10448 | } | |
10449 | else | |
10450 | { | |
b13c66cd | 10451 | tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
10452 | create_tmp_var_name("M"), | |
746d2e73 | 10453 | TREE_TYPE(index_tree)); |
10454 | DECL_EXTERNAL(tmp) = 0; | |
10455 | TREE_PUBLIC(tmp) = 0; | |
10456 | TREE_STATIC(tmp) = 1; | |
10457 | DECL_ARTIFICIAL(tmp) = 1; | |
10458 | if (!TREE_CONSTANT(index_tree)) | |
b13c66cd | 10459 | make_tmp = fold_build2_loc(this->location().gcc_location(), |
10460 | INIT_EXPR, void_type_node, | |
746d2e73 | 10461 | tmp, index_tree); |
10462 | else | |
10463 | { | |
10464 | TREE_READONLY(tmp) = 1; | |
10465 | TREE_CONSTANT(tmp) = 1; | |
10466 | DECL_INITIAL(tmp) = index_tree; | |
10467 | make_tmp = NULL_TREE; | |
10468 | } | |
10469 | rest_of_decl_compilation(tmp, 1, 0); | |
10470 | } | |
b13c66cd | 10471 | tree tmpref = |
10472 | fold_convert_loc(this->location().gcc_location(), const_ptr_type_node, | |
10473 | build_fold_addr_expr_loc(this->location().gcc_location(), | |
10474 | tmp)); | |
e440a328 | 10475 | |
10476 | static tree map_index_fndecl; | |
10477 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
10478 | this->location(), | |
10479 | "__go_map_index", | |
10480 | 3, | |
10481 | const_ptr_type_node, | |
10482 | TREE_TYPE(map_tree), | |
10483 | map_tree, | |
10484 | const_ptr_type_node, | |
10485 | tmpref, | |
10486 | boolean_type_node, | |
10487 | (insert | |
10488 | ? boolean_true_node | |
10489 | : boolean_false_node)); | |
5fb82b5e | 10490 | if (call == error_mark_node) |
10491 | return error_mark_node; | |
e440a328 | 10492 | // This can panic on a map of interface type if the interface holds |
10493 | // an uncomparable or unhashable type. | |
10494 | TREE_NOTHROW(map_index_fndecl) = 0; | |
10495 | ||
9f0e0513 | 10496 | Type* val_type = type->val_type(); |
10497 | tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo())); | |
e440a328 | 10498 | if (val_type_tree == error_mark_node) |
10499 | return error_mark_node; | |
10500 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
10501 | ||
b13c66cd | 10502 | tree ret = fold_convert_loc(this->location().gcc_location(), |
10503 | ptr_val_type_tree, call); | |
746d2e73 | 10504 | if (make_tmp != NULL_TREE) |
10505 | ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret); | |
10506 | return ret; | |
e440a328 | 10507 | } |
10508 | ||
d751bb78 | 10509 | // Dump ast representation for a map index expression |
10510 | ||
10511 | void | |
10512 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10513 | const | |
10514 | { | |
10515 | Index_expression::dump_index_expression(ast_dump_context, | |
10516 | this->map_, this->index_, NULL); | |
10517 | } | |
10518 | ||
e440a328 | 10519 | // Make a map index expression. |
10520 | ||
10521 | Map_index_expression* | |
10522 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 10523 | Location location) |
e440a328 | 10524 | { |
10525 | return new Map_index_expression(map, index, location); | |
10526 | } | |
10527 | ||
10528 | // Class Field_reference_expression. | |
10529 | ||
10530 | // Return the type of a field reference. | |
10531 | ||
10532 | Type* | |
10533 | Field_reference_expression::do_type() | |
10534 | { | |
b0e628fb | 10535 | Type* type = this->expr_->type(); |
5c13bd80 | 10536 | if (type->is_error()) |
b0e628fb | 10537 | return type; |
10538 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 10539 | go_assert(struct_type != NULL); |
e440a328 | 10540 | return struct_type->field(this->field_index_)->type(); |
10541 | } | |
10542 | ||
10543 | // Check the types for a field reference. | |
10544 | ||
10545 | void | |
10546 | Field_reference_expression::do_check_types(Gogo*) | |
10547 | { | |
b0e628fb | 10548 | Type* type = this->expr_->type(); |
5c13bd80 | 10549 | if (type->is_error()) |
b0e628fb | 10550 | return; |
10551 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 10552 | go_assert(struct_type != NULL); |
10553 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 10554 | } |
10555 | ||
10556 | // Get a tree for a field reference. | |
10557 | ||
10558 | tree | |
10559 | Field_reference_expression::do_get_tree(Translate_context* context) | |
10560 | { | |
10561 | tree struct_tree = this->expr_->get_tree(context); | |
10562 | if (struct_tree == error_mark_node | |
10563 | || TREE_TYPE(struct_tree) == error_mark_node) | |
10564 | return error_mark_node; | |
c484d925 | 10565 | go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); |
e440a328 | 10566 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); |
b1d655d5 | 10567 | if (field == NULL_TREE) |
10568 | { | |
10569 | // This can happen for a type which refers to itself indirectly | |
10570 | // and then turns out to be erroneous. | |
c484d925 | 10571 | go_assert(saw_errors()); |
b1d655d5 | 10572 | return error_mark_node; |
10573 | } | |
e440a328 | 10574 | for (unsigned int i = this->field_index_; i > 0; --i) |
10575 | { | |
10576 | field = DECL_CHAIN(field); | |
c484d925 | 10577 | go_assert(field != NULL_TREE); |
e440a328 | 10578 | } |
c35179ff | 10579 | if (TREE_TYPE(field) == error_mark_node) |
10580 | return error_mark_node; | |
e440a328 | 10581 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, |
10582 | NULL_TREE); | |
10583 | } | |
10584 | ||
d751bb78 | 10585 | // Dump ast representation for a field reference expression. |
10586 | ||
10587 | void | |
10588 | Field_reference_expression::do_dump_expression( | |
10589 | Ast_dump_context* ast_dump_context) const | |
10590 | { | |
10591 | this->expr_->dump_expression(ast_dump_context); | |
10592 | ast_dump_context->ostream() << "." << this->field_index_; | |
10593 | } | |
10594 | ||
e440a328 | 10595 | // Make a reference to a qualified identifier in an expression. |
10596 | ||
10597 | Field_reference_expression* | |
10598 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 10599 | Location location) |
e440a328 | 10600 | { |
10601 | return new Field_reference_expression(expr, field_index, location); | |
10602 | } | |
10603 | ||
10604 | // Class Interface_field_reference_expression. | |
10605 | ||
10606 | // Return a tree for the pointer to the function to call. | |
10607 | ||
10608 | tree | |
10609 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
10610 | tree expr) | |
10611 | { | |
10612 | if (this->expr_->type()->points_to() != NULL) | |
10613 | expr = build_fold_indirect_ref(expr); | |
10614 | ||
10615 | tree expr_type = TREE_TYPE(expr); | |
c484d925 | 10616 | go_assert(TREE_CODE(expr_type) == RECORD_TYPE); |
e440a328 | 10617 | |
10618 | tree field = TYPE_FIELDS(expr_type); | |
c484d925 | 10619 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); |
e440a328 | 10620 | |
10621 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
c484d925 | 10622 | go_assert(POINTER_TYPE_P(TREE_TYPE(table))); |
e440a328 | 10623 | |
10624 | table = build_fold_indirect_ref(table); | |
c484d925 | 10625 | go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); |
e440a328 | 10626 | |
10627 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
10628 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
10629 | field != NULL_TREE; | |
10630 | field = DECL_CHAIN(field)) | |
10631 | { | |
10632 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
10633 | break; | |
10634 | } | |
c484d925 | 10635 | go_assert(field != NULL_TREE); |
e440a328 | 10636 | |
10637 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
10638 | } | |
10639 | ||
10640 | // Return a tree for the first argument to pass to the interface | |
10641 | // function. | |
10642 | ||
10643 | tree | |
10644 | Interface_field_reference_expression::get_underlying_object_tree( | |
10645 | Translate_context*, | |
10646 | tree expr) | |
10647 | { | |
10648 | if (this->expr_->type()->points_to() != NULL) | |
10649 | expr = build_fold_indirect_ref(expr); | |
10650 | ||
10651 | tree expr_type = TREE_TYPE(expr); | |
c484d925 | 10652 | go_assert(TREE_CODE(expr_type) == RECORD_TYPE); |
e440a328 | 10653 | |
10654 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
c484d925 | 10655 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 10656 | |
10657 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
10658 | } | |
10659 | ||
10660 | // Traversal. | |
10661 | ||
10662 | int | |
10663 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
10664 | { | |
10665 | return Expression::traverse(&this->expr_, traverse); | |
10666 | } | |
10667 | ||
10668 | // Return the type of an interface field reference. | |
10669 | ||
10670 | Type* | |
10671 | Interface_field_reference_expression::do_type() | |
10672 | { | |
10673 | Type* expr_type = this->expr_->type(); | |
10674 | ||
10675 | Type* points_to = expr_type->points_to(); | |
10676 | if (points_to != NULL) | |
10677 | expr_type = points_to; | |
10678 | ||
10679 | Interface_type* interface_type = expr_type->interface_type(); | |
10680 | if (interface_type == NULL) | |
10681 | return Type::make_error_type(); | |
10682 | ||
10683 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
10684 | if (method == NULL) | |
10685 | return Type::make_error_type(); | |
10686 | ||
10687 | return method->type(); | |
10688 | } | |
10689 | ||
10690 | // Determine types. | |
10691 | ||
10692 | void | |
10693 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
10694 | { | |
10695 | this->expr_->determine_type_no_context(); | |
10696 | } | |
10697 | ||
10698 | // Check the types for an interface field reference. | |
10699 | ||
10700 | void | |
10701 | Interface_field_reference_expression::do_check_types(Gogo*) | |
10702 | { | |
10703 | Type* type = this->expr_->type(); | |
10704 | ||
10705 | Type* points_to = type->points_to(); | |
10706 | if (points_to != NULL) | |
10707 | type = points_to; | |
10708 | ||
10709 | Interface_type* interface_type = type->interface_type(); | |
10710 | if (interface_type == NULL) | |
5c491127 | 10711 | { |
10712 | if (!type->is_error_type()) | |
10713 | this->report_error(_("expected interface or pointer to interface")); | |
10714 | } | |
e440a328 | 10715 | else |
10716 | { | |
10717 | const Typed_identifier* method = | |
10718 | interface_type->find_method(this->name_); | |
10719 | if (method == NULL) | |
10720 | { | |
10721 | error_at(this->location(), "method %qs not in interface", | |
10722 | Gogo::message_name(this->name_).c_str()); | |
10723 | this->set_is_error(); | |
10724 | } | |
10725 | } | |
10726 | } | |
10727 | ||
10728 | // Get a tree for a reference to a field in an interface. There is no | |
10729 | // standard tree type representation for this: it's a function | |
10730 | // attached to its first argument, like a Bound_method_expression. | |
10731 | // The only places it may currently be used are in a Call_expression | |
10732 | // or a Go_statement, which will take it apart directly. So this has | |
10733 | // nothing to do at present. | |
10734 | ||
10735 | tree | |
10736 | Interface_field_reference_expression::do_get_tree(Translate_context*) | |
10737 | { | |
11bbe026 | 10738 | error_at(this->location(), "reference to method other than calling it"); |
10739 | return error_mark_node; | |
e440a328 | 10740 | } |
10741 | ||
d751bb78 | 10742 | // Dump ast representation for an interface field reference. |
10743 | ||
10744 | void | |
10745 | Interface_field_reference_expression::do_dump_expression( | |
10746 | Ast_dump_context* ast_dump_context) const | |
10747 | { | |
10748 | this->expr_->dump_expression(ast_dump_context); | |
10749 | ast_dump_context->ostream() << "." << this->name_; | |
10750 | } | |
10751 | ||
e440a328 | 10752 | // Make a reference to a field in an interface. |
10753 | ||
10754 | Expression* | |
10755 | Expression::make_interface_field_reference(Expression* expr, | |
10756 | const std::string& field, | |
b13c66cd | 10757 | Location location) |
e440a328 | 10758 | { |
10759 | return new Interface_field_reference_expression(expr, field, location); | |
10760 | } | |
10761 | ||
10762 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
10763 | // is lowered after we know the type of the left hand side. | |
10764 | ||
10765 | class Selector_expression : public Parser_expression | |
10766 | { | |
10767 | public: | |
10768 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 10769 | Location location) |
e440a328 | 10770 | : Parser_expression(EXPRESSION_SELECTOR, location), |
10771 | left_(left), name_(name) | |
10772 | { } | |
10773 | ||
10774 | protected: | |
10775 | int | |
10776 | do_traverse(Traverse* traverse) | |
10777 | { return Expression::traverse(&this->left_, traverse); } | |
10778 | ||
10779 | Expression* | |
ceeb4318 | 10780 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 10781 | |
10782 | Expression* | |
10783 | do_copy() | |
10784 | { | |
10785 | return new Selector_expression(this->left_->copy(), this->name_, | |
10786 | this->location()); | |
10787 | } | |
10788 | ||
d751bb78 | 10789 | void |
10790 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
10791 | ||
e440a328 | 10792 | private: |
10793 | Expression* | |
10794 | lower_method_expression(Gogo*); | |
10795 | ||
10796 | // The expression on the left hand side. | |
10797 | Expression* left_; | |
10798 | // The name on the right hand side. | |
10799 | std::string name_; | |
10800 | }; | |
10801 | ||
10802 | // Lower a selector expression once we know the real type of the left | |
10803 | // hand side. | |
10804 | ||
10805 | Expression* | |
ceeb4318 | 10806 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
10807 | int) | |
e440a328 | 10808 | { |
10809 | Expression* left = this->left_; | |
10810 | if (left->is_type_expression()) | |
10811 | return this->lower_method_expression(gogo); | |
10812 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
10813 | this->location()); | |
10814 | } | |
10815 | ||
10816 | // Lower a method expression T.M or (*T).M. We turn this into a | |
10817 | // function literal. | |
10818 | ||
10819 | Expression* | |
10820 | Selector_expression::lower_method_expression(Gogo* gogo) | |
10821 | { | |
b13c66cd | 10822 | Location location = this->location(); |
e440a328 | 10823 | Type* type = this->left_->type(); |
10824 | const std::string& name(this->name_); | |
10825 | ||
10826 | bool is_pointer; | |
10827 | if (type->points_to() == NULL) | |
10828 | is_pointer = false; | |
10829 | else | |
10830 | { | |
10831 | is_pointer = true; | |
10832 | type = type->points_to(); | |
10833 | } | |
10834 | Named_type* nt = type->named_type(); | |
10835 | if (nt == NULL) | |
10836 | { | |
10837 | error_at(location, | |
10838 | ("method expression requires named type or " | |
10839 | "pointer to named type")); | |
10840 | return Expression::make_error(location); | |
10841 | } | |
10842 | ||
10843 | bool is_ambiguous; | |
10844 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 10845 | const Typed_identifier* imethod = NULL; |
dcc8506b | 10846 | if (method == NULL && !is_pointer) |
ab1468c3 | 10847 | { |
10848 | Interface_type* it = nt->interface_type(); | |
10849 | if (it != NULL) | |
10850 | imethod = it->find_method(name); | |
10851 | } | |
10852 | ||
10853 | if (method == NULL && imethod == NULL) | |
e440a328 | 10854 | { |
10855 | if (!is_ambiguous) | |
dcc8506b | 10856 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
10857 | is_pointer ? "*" : "", | |
e440a328 | 10858 | nt->message_name().c_str(), |
10859 | Gogo::message_name(name).c_str()); | |
10860 | else | |
dcc8506b | 10861 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 10862 | Gogo::message_name(name).c_str(), |
dcc8506b | 10863 | is_pointer ? "*" : "", |
e440a328 | 10864 | nt->message_name().c_str()); |
10865 | return Expression::make_error(location); | |
10866 | } | |
10867 | ||
ab1468c3 | 10868 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 10869 | { |
10870 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
10871 | nt->message_name().c_str(), | |
10872 | Gogo::message_name(name).c_str()); | |
10873 | return Expression::make_error(location); | |
10874 | } | |
10875 | ||
10876 | // Build a new function type in which the receiver becomes the first | |
10877 | // argument. | |
ab1468c3 | 10878 | Function_type* method_type; |
10879 | if (method != NULL) | |
10880 | { | |
10881 | method_type = method->type(); | |
c484d925 | 10882 | go_assert(method_type->is_method()); |
ab1468c3 | 10883 | } |
10884 | else | |
10885 | { | |
10886 | method_type = imethod->type()->function_type(); | |
c484d925 | 10887 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 10888 | } |
e440a328 | 10889 | |
10890 | const char* const receiver_name = "$this"; | |
10891 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
10892 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
10893 | location)); | |
10894 | ||
10895 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
10896 | if (method_parameters != NULL) | |
10897 | { | |
f470da59 | 10898 | int i = 0; |
e440a328 | 10899 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
10900 | p != method_parameters->end(); | |
f470da59 | 10901 | ++p, ++i) |
10902 | { | |
68883531 | 10903 | if (!p->name().empty()) |
f470da59 | 10904 | parameters->push_back(*p); |
10905 | else | |
10906 | { | |
10907 | char buf[20]; | |
10908 | snprintf(buf, sizeof buf, "$param%d", i); | |
10909 | parameters->push_back(Typed_identifier(buf, p->type(), | |
10910 | p->location())); | |
10911 | } | |
10912 | } | |
e440a328 | 10913 | } |
10914 | ||
10915 | const Typed_identifier_list* method_results = method_type->results(); | |
10916 | Typed_identifier_list* results; | |
10917 | if (method_results == NULL) | |
10918 | results = NULL; | |
10919 | else | |
10920 | { | |
10921 | results = new Typed_identifier_list(); | |
10922 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
10923 | p != method_results->end(); | |
10924 | ++p) | |
10925 | results->push_back(*p); | |
10926 | } | |
10927 | ||
10928 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
10929 | location); | |
10930 | if (method_type->is_varargs()) | |
10931 | fntype->set_is_varargs(); | |
10932 | ||
10933 | // We generate methods which always takes a pointer to the receiver | |
10934 | // as their first argument. If this is for a pointer type, we can | |
10935 | // simply reuse the existing function. We use an internal hack to | |
10936 | // get the right type. | |
10937 | ||
ab1468c3 | 10938 | if (method != NULL && is_pointer) |
e440a328 | 10939 | { |
10940 | Named_object* mno = (method->needs_stub_method() | |
10941 | ? method->stub_object() | |
10942 | : method->named_object()); | |
10943 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
10944 | f = Expression::make_cast(fntype, f, location); | |
10945 | Type_conversion_expression* tce = | |
10946 | static_cast<Type_conversion_expression*>(f); | |
10947 | tce->set_may_convert_function_types(); | |
10948 | return f; | |
10949 | } | |
10950 | ||
10951 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
10952 | location); | |
10953 | ||
10954 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 10955 | go_assert(vno != NULL); |
e440a328 | 10956 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 10957 | Expression* bm; |
10958 | if (method != NULL) | |
10959 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
10960 | else | |
10961 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 10962 | |
10963 | // Even though we found the method above, if it has an error type we | |
10964 | // may see an error here. | |
10965 | if (bm->is_error_expression()) | |
463fe805 | 10966 | { |
10967 | gogo->finish_function(location); | |
10968 | return bm; | |
10969 | } | |
e440a328 | 10970 | |
10971 | Expression_list* args; | |
f470da59 | 10972 | if (parameters->size() <= 1) |
e440a328 | 10973 | args = NULL; |
10974 | else | |
10975 | { | |
10976 | args = new Expression_list(); | |
f470da59 | 10977 | Typed_identifier_list::const_iterator p = parameters->begin(); |
10978 | ++p; | |
10979 | for (; p != parameters->end(); ++p) | |
e440a328 | 10980 | { |
10981 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 10982 | go_assert(vno != NULL); |
e440a328 | 10983 | args->push_back(Expression::make_var_reference(vno, location)); |
10984 | } | |
10985 | } | |
10986 | ||
ceeb4318 | 10987 | gogo->start_block(location); |
10988 | ||
e440a328 | 10989 | Call_expression* call = Expression::make_call(bm, args, |
10990 | method_type->is_varargs(), | |
10991 | location); | |
10992 | ||
10993 | size_t count = call->result_count(); | |
10994 | Statement* s; | |
10995 | if (count == 0) | |
a7549a6a | 10996 | s = Statement::make_statement(call, true); |
e440a328 | 10997 | else |
10998 | { | |
10999 | Expression_list* retvals = new Expression_list(); | |
11000 | if (count <= 1) | |
11001 | retvals->push_back(call); | |
11002 | else | |
11003 | { | |
11004 | for (size_t i = 0; i < count; ++i) | |
11005 | retvals->push_back(Expression::make_call_result(call, i)); | |
11006 | } | |
be2fc38d | 11007 | s = Statement::make_return_statement(retvals, location); |
e440a328 | 11008 | } |
11009 | gogo->add_statement(s); | |
11010 | ||
ceeb4318 | 11011 | Block* b = gogo->finish_block(location); |
11012 | ||
11013 | gogo->add_block(b, location); | |
11014 | ||
11015 | // Lower the call in case there are multiple results. | |
11016 | gogo->lower_block(no, b); | |
11017 | ||
e440a328 | 11018 | gogo->finish_function(location); |
11019 | ||
11020 | return Expression::make_func_reference(no, NULL, location); | |
11021 | } | |
11022 | ||
d751bb78 | 11023 | // Dump the ast for a selector expression. |
11024 | ||
11025 | void | |
11026 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11027 | const | |
11028 | { | |
11029 | ast_dump_context->dump_expression(this->left_); | |
11030 | ast_dump_context->ostream() << "."; | |
11031 | ast_dump_context->ostream() << this->name_; | |
11032 | } | |
11033 | ||
e440a328 | 11034 | // Make a selector expression. |
11035 | ||
11036 | Expression* | |
11037 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 11038 | Location location) |
e440a328 | 11039 | { |
11040 | return new Selector_expression(left, name, location); | |
11041 | } | |
11042 | ||
11043 | // Implement the builtin function new. | |
11044 | ||
11045 | class Allocation_expression : public Expression | |
11046 | { | |
11047 | public: | |
b13c66cd | 11048 | Allocation_expression(Type* type, Location location) |
e440a328 | 11049 | : Expression(EXPRESSION_ALLOCATION, location), |
11050 | type_(type) | |
11051 | { } | |
11052 | ||
11053 | protected: | |
11054 | int | |
11055 | do_traverse(Traverse* traverse) | |
11056 | { return Type::traverse(this->type_, traverse); } | |
11057 | ||
11058 | Type* | |
11059 | do_type() | |
11060 | { return Type::make_pointer_type(this->type_); } | |
11061 | ||
11062 | void | |
11063 | do_determine_type(const Type_context*) | |
11064 | { } | |
11065 | ||
e440a328 | 11066 | Expression* |
11067 | do_copy() | |
11068 | { return new Allocation_expression(this->type_, this->location()); } | |
11069 | ||
11070 | tree | |
11071 | do_get_tree(Translate_context*); | |
11072 | ||
d751bb78 | 11073 | void |
11074 | do_dump_expression(Ast_dump_context*) const; | |
11075 | ||
e440a328 | 11076 | private: |
11077 | // The type we are allocating. | |
11078 | Type* type_; | |
11079 | }; | |
11080 | ||
e440a328 | 11081 | // Return a tree for an allocation expression. |
11082 | ||
11083 | tree | |
11084 | Allocation_expression::do_get_tree(Translate_context* context) | |
11085 | { | |
9f0e0513 | 11086 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
19824ddb | 11087 | if (type_tree == error_mark_node) |
11088 | return error_mark_node; | |
e440a328 | 11089 | tree size_tree = TYPE_SIZE_UNIT(type_tree); |
11090 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
11091 | this->location()); | |
19824ddb | 11092 | if (space == error_mark_node) |
11093 | return error_mark_node; | |
e440a328 | 11094 | return fold_convert(build_pointer_type(type_tree), space); |
11095 | } | |
11096 | ||
d751bb78 | 11097 | // Dump ast representation for an allocation expression. |
11098 | ||
11099 | void | |
11100 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11101 | const | |
11102 | { | |
11103 | ast_dump_context->ostream() << "new("; | |
11104 | ast_dump_context->dump_type(this->type_); | |
11105 | ast_dump_context->ostream() << ")"; | |
11106 | } | |
11107 | ||
e440a328 | 11108 | // Make an allocation expression. |
11109 | ||
11110 | Expression* | |
b13c66cd | 11111 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 11112 | { |
11113 | return new Allocation_expression(type, location); | |
11114 | } | |
11115 | ||
e440a328 | 11116 | // Construct a struct. |
11117 | ||
11118 | class Struct_construction_expression : public Expression | |
11119 | { | |
11120 | public: | |
11121 | Struct_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 11122 | Location location) |
e440a328 | 11123 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), |
0c4f5a19 | 11124 | type_(type), vals_(vals), traverse_order_(NULL) |
e440a328 | 11125 | { } |
11126 | ||
0c4f5a19 | 11127 | // Set the traversal order, used to ensure that we implement the |
11128 | // order of evaluation rules. Takes ownership of the argument. | |
11129 | void | |
11130 | set_traverse_order(std::vector<int>* traverse_order) | |
11131 | { this->traverse_order_ = traverse_order; } | |
11132 | ||
e440a328 | 11133 | // Return whether this is a constant initializer. |
11134 | bool | |
11135 | is_constant_struct() const; | |
11136 | ||
11137 | protected: | |
11138 | int | |
11139 | do_traverse(Traverse* traverse); | |
11140 | ||
11141 | Type* | |
11142 | do_type() | |
11143 | { return this->type_; } | |
11144 | ||
11145 | void | |
11146 | do_determine_type(const Type_context*); | |
11147 | ||
11148 | void | |
11149 | do_check_types(Gogo*); | |
11150 | ||
11151 | Expression* | |
11152 | do_copy() | |
11153 | { | |
0c4f5a19 | 11154 | Struct_construction_expression* ret = |
11155 | new Struct_construction_expression(this->type_, this->vals_->copy(), | |
11156 | this->location()); | |
11157 | if (this->traverse_order_ != NULL) | |
11158 | ret->set_traverse_order(this->traverse_order_); | |
11159 | return ret; | |
e440a328 | 11160 | } |
11161 | ||
e440a328 | 11162 | tree |
11163 | do_get_tree(Translate_context*); | |
11164 | ||
11165 | void | |
11166 | do_export(Export*) const; | |
11167 | ||
d751bb78 | 11168 | void |
11169 | do_dump_expression(Ast_dump_context*) const; | |
11170 | ||
e440a328 | 11171 | private: |
11172 | // The type of the struct to construct. | |
11173 | Type* type_; | |
11174 | // The list of values, in order of the fields in the struct. A NULL | |
11175 | // entry means that the field should be zero-initialized. | |
11176 | Expression_list* vals_; | |
0c4f5a19 | 11177 | // If not NULL, the order in which to traverse vals_. This is used |
11178 | // so that we implement the order of evaluation rules correctly. | |
11179 | std::vector<int>* traverse_order_; | |
e440a328 | 11180 | }; |
11181 | ||
11182 | // Traversal. | |
11183 | ||
11184 | int | |
11185 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
11186 | { | |
0c4f5a19 | 11187 | if (this->vals_ != NULL) |
11188 | { | |
11189 | if (this->traverse_order_ == NULL) | |
11190 | { | |
11191 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11192 | return TRAVERSE_EXIT; | |
11193 | } | |
11194 | else | |
11195 | { | |
11196 | for (std::vector<int>::const_iterator p = | |
11197 | this->traverse_order_->begin(); | |
11198 | p != this->traverse_order_->end(); | |
11199 | ++p) | |
11200 | { | |
11201 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
11202 | == TRAVERSE_EXIT) | |
11203 | return TRAVERSE_EXIT; | |
11204 | } | |
11205 | } | |
11206 | } | |
e440a328 | 11207 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
11208 | return TRAVERSE_EXIT; | |
11209 | return TRAVERSE_CONTINUE; | |
11210 | } | |
11211 | ||
11212 | // Return whether this is a constant initializer. | |
11213 | ||
11214 | bool | |
11215 | Struct_construction_expression::is_constant_struct() const | |
11216 | { | |
11217 | if (this->vals_ == NULL) | |
11218 | return true; | |
11219 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11220 | pv != this->vals_->end(); | |
11221 | ++pv) | |
11222 | { | |
11223 | if (*pv != NULL | |
11224 | && !(*pv)->is_constant() | |
11225 | && (!(*pv)->is_composite_literal() | |
11226 | || (*pv)->is_nonconstant_composite_literal())) | |
11227 | return false; | |
11228 | } | |
11229 | ||
11230 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11231 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11232 | pf != fields->end(); | |
11233 | ++pf) | |
11234 | { | |
11235 | // There are no constant constructors for interfaces. | |
11236 | if (pf->type()->interface_type() != NULL) | |
11237 | return false; | |
11238 | } | |
11239 | ||
11240 | return true; | |
11241 | } | |
11242 | ||
11243 | // Final type determination. | |
11244 | ||
11245 | void | |
11246 | Struct_construction_expression::do_determine_type(const Type_context*) | |
11247 | { | |
11248 | if (this->vals_ == NULL) | |
11249 | return; | |
11250 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11251 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11252 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11253 | pf != fields->end(); | |
11254 | ++pf, ++pv) | |
11255 | { | |
11256 | if (pv == this->vals_->end()) | |
11257 | return; | |
11258 | if (*pv != NULL) | |
11259 | { | |
11260 | Type_context subcontext(pf->type(), false); | |
11261 | (*pv)->determine_type(&subcontext); | |
11262 | } | |
11263 | } | |
a6cb4c0e | 11264 | // Extra values are an error we will report elsewhere; we still want |
11265 | // to determine the type to avoid knockon errors. | |
11266 | for (; pv != this->vals_->end(); ++pv) | |
11267 | (*pv)->determine_type_no_context(); | |
e440a328 | 11268 | } |
11269 | ||
11270 | // Check types. | |
11271 | ||
11272 | void | |
11273 | Struct_construction_expression::do_check_types(Gogo*) | |
11274 | { | |
11275 | if (this->vals_ == NULL) | |
11276 | return; | |
11277 | ||
11278 | Struct_type* st = this->type_->struct_type(); | |
11279 | if (this->vals_->size() > st->field_count()) | |
11280 | { | |
11281 | this->report_error(_("too many expressions for struct")); | |
11282 | return; | |
11283 | } | |
11284 | ||
11285 | const Struct_field_list* fields = st->fields(); | |
11286 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11287 | int i = 0; | |
11288 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
11289 | pf != fields->end(); | |
11290 | ++pf, ++pv, ++i) | |
11291 | { | |
11292 | if (pv == this->vals_->end()) | |
11293 | { | |
11294 | this->report_error(_("too few expressions for struct")); | |
11295 | break; | |
11296 | } | |
11297 | ||
11298 | if (*pv == NULL) | |
11299 | continue; | |
11300 | ||
11301 | std::string reason; | |
11302 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
11303 | { | |
11304 | if (reason.empty()) | |
11305 | error_at((*pv)->location(), | |
11306 | "incompatible type for field %d in struct construction", | |
11307 | i + 1); | |
11308 | else | |
11309 | error_at((*pv)->location(), | |
11310 | ("incompatible type for field %d in " | |
11311 | "struct construction (%s)"), | |
11312 | i + 1, reason.c_str()); | |
11313 | this->set_is_error(); | |
11314 | } | |
11315 | } | |
c484d925 | 11316 | go_assert(pv == this->vals_->end()); |
e440a328 | 11317 | } |
11318 | ||
11319 | // Return a tree for constructing a struct. | |
11320 | ||
11321 | tree | |
11322 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
11323 | { | |
11324 | Gogo* gogo = context->gogo(); | |
11325 | ||
11326 | if (this->vals_ == NULL) | |
63697958 | 11327 | { |
11328 | Btype* btype = this->type_->get_backend(gogo); | |
11329 | return expr_to_tree(gogo->backend()->zero_expression(btype)); | |
11330 | } | |
e440a328 | 11331 | |
9f0e0513 | 11332 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 11333 | if (type_tree == error_mark_node) |
11334 | return error_mark_node; | |
c484d925 | 11335 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 11336 | |
11337 | bool is_constant = true; | |
11338 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
11339 | VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc, | |
11340 | fields->size()); | |
11341 | Struct_field_list::const_iterator pf = fields->begin(); | |
11342 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11343 | for (tree field = TYPE_FIELDS(type_tree); | |
11344 | field != NULL_TREE; | |
11345 | field = DECL_CHAIN(field), ++pf) | |
11346 | { | |
c484d925 | 11347 | go_assert(pf != fields->end()); |
e440a328 | 11348 | |
63697958 | 11349 | Btype* fbtype = pf->type()->get_backend(gogo); |
11350 | ||
e440a328 | 11351 | tree val; |
11352 | if (pv == this->vals_->end()) | |
63697958 | 11353 | val = expr_to_tree(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 11354 | else if (*pv == NULL) |
11355 | { | |
63697958 | 11356 | val = expr_to_tree(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 11357 | ++pv; |
11358 | } | |
11359 | else | |
11360 | { | |
11361 | val = Expression::convert_for_assignment(context, pf->type(), | |
11362 | (*pv)->type(), | |
11363 | (*pv)->get_tree(context), | |
11364 | this->location()); | |
11365 | ++pv; | |
11366 | } | |
11367 | ||
11368 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
11369 | return error_mark_node; | |
11370 | ||
e82e4eb5 | 11371 | constructor_elt empty = {NULL, NULL}; |
11372 | constructor_elt* elt = VEC_quick_push(constructor_elt, elts, empty); | |
e440a328 | 11373 | elt->index = field; |
11374 | elt->value = val; | |
11375 | if (!TREE_CONSTANT(val)) | |
11376 | is_constant = false; | |
11377 | } | |
c484d925 | 11378 | go_assert(pf == fields->end()); |
e440a328 | 11379 | |
11380 | tree ret = build_constructor(type_tree, elts); | |
11381 | if (is_constant) | |
11382 | TREE_CONSTANT(ret) = 1; | |
11383 | return ret; | |
11384 | } | |
11385 | ||
11386 | // Export a struct construction. | |
11387 | ||
11388 | void | |
11389 | Struct_construction_expression::do_export(Export* exp) const | |
11390 | { | |
11391 | exp->write_c_string("convert("); | |
11392 | exp->write_type(this->type_); | |
11393 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11394 | pv != this->vals_->end(); | |
11395 | ++pv) | |
11396 | { | |
11397 | exp->write_c_string(", "); | |
11398 | if (*pv != NULL) | |
11399 | (*pv)->export_expression(exp); | |
11400 | } | |
11401 | exp->write_c_string(")"); | |
11402 | } | |
11403 | ||
d751bb78 | 11404 | // Dump ast representation of a struct construction expression. |
11405 | ||
11406 | void | |
11407 | Struct_construction_expression::do_dump_expression( | |
11408 | Ast_dump_context* ast_dump_context) const | |
11409 | { | |
d751bb78 | 11410 | ast_dump_context->dump_type(this->type_); |
11411 | ast_dump_context->ostream() << "{"; | |
11412 | ast_dump_context->dump_expression_list(this->vals_); | |
11413 | ast_dump_context->ostream() << "}"; | |
11414 | } | |
11415 | ||
e440a328 | 11416 | // Make a struct composite literal. This used by the thunk code. |
11417 | ||
11418 | Expression* | |
11419 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 11420 | Location location) |
e440a328 | 11421 | { |
c484d925 | 11422 | go_assert(type->struct_type() != NULL); |
e440a328 | 11423 | return new Struct_construction_expression(type, vals, location); |
11424 | } | |
11425 | ||
11426 | // Construct an array. This class is not used directly; instead we | |
11427 | // use the child classes, Fixed_array_construction_expression and | |
11428 | // Open_array_construction_expression. | |
11429 | ||
11430 | class Array_construction_expression : public Expression | |
11431 | { | |
11432 | protected: | |
11433 | Array_construction_expression(Expression_classification classification, | |
ffe743ca | 11434 | Type* type, |
11435 | const std::vector<unsigned long>* indexes, | |
11436 | Expression_list* vals, Location location) | |
e440a328 | 11437 | : Expression(classification, location), |
ffe743ca | 11438 | type_(type), indexes_(indexes), vals_(vals) |
11439 | { go_assert(indexes == NULL || indexes->size() == vals->size()); } | |
e440a328 | 11440 | |
11441 | public: | |
11442 | // Return whether this is a constant initializer. | |
11443 | bool | |
11444 | is_constant_array() const; | |
11445 | ||
11446 | // Return the number of elements. | |
11447 | size_t | |
11448 | element_count() const | |
11449 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
11450 | ||
11451 | protected: | |
11452 | int | |
11453 | do_traverse(Traverse* traverse); | |
11454 | ||
11455 | Type* | |
11456 | do_type() | |
11457 | { return this->type_; } | |
11458 | ||
11459 | void | |
11460 | do_determine_type(const Type_context*); | |
11461 | ||
11462 | void | |
11463 | do_check_types(Gogo*); | |
11464 | ||
e440a328 | 11465 | void |
11466 | do_export(Export*) const; | |
11467 | ||
ffe743ca | 11468 | // The indexes. |
11469 | const std::vector<unsigned long>* | |
11470 | indexes() | |
11471 | { return this->indexes_; } | |
11472 | ||
e440a328 | 11473 | // The list of values. |
11474 | Expression_list* | |
11475 | vals() | |
11476 | { return this->vals_; } | |
11477 | ||
11478 | // Get a constructor tree for the array values. | |
11479 | tree | |
11480 | get_constructor_tree(Translate_context* context, tree type_tree); | |
11481 | ||
d751bb78 | 11482 | void |
11483 | do_dump_expression(Ast_dump_context*) const; | |
11484 | ||
e440a328 | 11485 | private: |
11486 | // The type of the array to construct. | |
11487 | Type* type_; | |
ffe743ca | 11488 | // The list of indexes into the array, one for each value. This may |
11489 | // be NULL, in which case the indexes start at zero and increment. | |
11490 | const std::vector<unsigned long>* indexes_; | |
11491 | // The list of values. This may be NULL if there are no values. | |
e440a328 | 11492 | Expression_list* vals_; |
11493 | }; | |
11494 | ||
11495 | // Traversal. | |
11496 | ||
11497 | int | |
11498 | Array_construction_expression::do_traverse(Traverse* traverse) | |
11499 | { | |
11500 | if (this->vals_ != NULL | |
11501 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11502 | return TRAVERSE_EXIT; | |
11503 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11504 | return TRAVERSE_EXIT; | |
11505 | return TRAVERSE_CONTINUE; | |
11506 | } | |
11507 | ||
11508 | // Return whether this is a constant initializer. | |
11509 | ||
11510 | bool | |
11511 | Array_construction_expression::is_constant_array() const | |
11512 | { | |
11513 | if (this->vals_ == NULL) | |
11514 | return true; | |
11515 | ||
11516 | // There are no constant constructors for interfaces. | |
11517 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
11518 | return false; | |
11519 | ||
11520 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11521 | pv != this->vals_->end(); | |
11522 | ++pv) | |
11523 | { | |
11524 | if (*pv != NULL | |
11525 | && !(*pv)->is_constant() | |
11526 | && (!(*pv)->is_composite_literal() | |
11527 | || (*pv)->is_nonconstant_composite_literal())) | |
11528 | return false; | |
11529 | } | |
11530 | return true; | |
11531 | } | |
11532 | ||
11533 | // Final type determination. | |
11534 | ||
11535 | void | |
11536 | Array_construction_expression::do_determine_type(const Type_context*) | |
11537 | { | |
11538 | if (this->vals_ == NULL) | |
11539 | return; | |
11540 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
11541 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11542 | pv != this->vals_->end(); | |
11543 | ++pv) | |
11544 | { | |
11545 | if (*pv != NULL) | |
11546 | (*pv)->determine_type(&subcontext); | |
11547 | } | |
11548 | } | |
11549 | ||
11550 | // Check types. | |
11551 | ||
11552 | void | |
11553 | Array_construction_expression::do_check_types(Gogo*) | |
11554 | { | |
11555 | if (this->vals_ == NULL) | |
11556 | return; | |
11557 | ||
11558 | Array_type* at = this->type_->array_type(); | |
11559 | int i = 0; | |
11560 | Type* element_type = at->element_type(); | |
11561 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11562 | pv != this->vals_->end(); | |
11563 | ++pv, ++i) | |
11564 | { | |
11565 | if (*pv != NULL | |
11566 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
11567 | { | |
11568 | error_at((*pv)->location(), | |
11569 | "incompatible type for element %d in composite literal", | |
11570 | i + 1); | |
11571 | this->set_is_error(); | |
11572 | } | |
11573 | } | |
e440a328 | 11574 | } |
11575 | ||
11576 | // Get a constructor tree for the array values. | |
11577 | ||
11578 | tree | |
11579 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
11580 | tree type_tree) | |
11581 | { | |
11582 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11583 | (this->vals_ == NULL | |
11584 | ? 0 | |
11585 | : this->vals_->size())); | |
11586 | Type* element_type = this->type_->array_type()->element_type(); | |
11587 | bool is_constant = true; | |
11588 | if (this->vals_ != NULL) | |
11589 | { | |
11590 | size_t i = 0; | |
ffe743ca | 11591 | std::vector<unsigned long>::const_iterator pi; |
11592 | if (this->indexes_ != NULL) | |
11593 | pi = this->indexes_->begin(); | |
e440a328 | 11594 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
11595 | pv != this->vals_->end(); | |
11596 | ++pv, ++i) | |
11597 | { | |
ffe743ca | 11598 | if (this->indexes_ != NULL) |
11599 | go_assert(pi != this->indexes_->end()); | |
e82e4eb5 | 11600 | constructor_elt empty = {NULL, NULL}; |
11601 | constructor_elt* elt = VEC_quick_push(constructor_elt, values, empty); | |
ffe743ca | 11602 | |
11603 | if (this->indexes_ == NULL) | |
11604 | elt->index = size_int(i); | |
11605 | else | |
11606 | elt->index = size_int(*pi); | |
11607 | ||
e440a328 | 11608 | if (*pv == NULL) |
63697958 | 11609 | { |
11610 | Gogo* gogo = context->gogo(); | |
11611 | Btype* ebtype = element_type->get_backend(gogo); | |
11612 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
11613 | elt->value = expr_to_tree(zv); | |
11614 | } | |
e440a328 | 11615 | else |
11616 | { | |
11617 | tree value_tree = (*pv)->get_tree(context); | |
11618 | elt->value = Expression::convert_for_assignment(context, | |
11619 | element_type, | |
11620 | (*pv)->type(), | |
11621 | value_tree, | |
11622 | this->location()); | |
11623 | } | |
11624 | if (elt->value == error_mark_node) | |
11625 | return error_mark_node; | |
11626 | if (!TREE_CONSTANT(elt->value)) | |
11627 | is_constant = false; | |
ffe743ca | 11628 | if (this->indexes_ != NULL) |
11629 | ++pi; | |
e440a328 | 11630 | } |
ffe743ca | 11631 | if (this->indexes_ != NULL) |
11632 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 11633 | } |
11634 | ||
11635 | tree ret = build_constructor(type_tree, values); | |
11636 | if (is_constant) | |
11637 | TREE_CONSTANT(ret) = 1; | |
11638 | return ret; | |
11639 | } | |
11640 | ||
11641 | // Export an array construction. | |
11642 | ||
11643 | void | |
11644 | Array_construction_expression::do_export(Export* exp) const | |
11645 | { | |
11646 | exp->write_c_string("convert("); | |
11647 | exp->write_type(this->type_); | |
11648 | if (this->vals_ != NULL) | |
11649 | { | |
ffe743ca | 11650 | std::vector<unsigned long>::const_iterator pi; |
11651 | if (this->indexes_ != NULL) | |
11652 | pi = this->indexes_->begin(); | |
e440a328 | 11653 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
11654 | pv != this->vals_->end(); | |
11655 | ++pv) | |
11656 | { | |
11657 | exp->write_c_string(", "); | |
ffe743ca | 11658 | |
11659 | if (this->indexes_ != NULL) | |
11660 | { | |
11661 | char buf[100]; | |
11662 | snprintf(buf, sizeof buf, "%lu", *pi); | |
11663 | exp->write_c_string(buf); | |
11664 | exp->write_c_string(":"); | |
11665 | } | |
11666 | ||
e440a328 | 11667 | if (*pv != NULL) |
11668 | (*pv)->export_expression(exp); | |
ffe743ca | 11669 | |
11670 | if (this->indexes_ != NULL) | |
11671 | ++pi; | |
e440a328 | 11672 | } |
11673 | } | |
11674 | exp->write_c_string(")"); | |
11675 | } | |
11676 | ||
d751bb78 | 11677 | // Dump ast representation of an array construction expressin. |
11678 | ||
11679 | void | |
11680 | Array_construction_expression::do_dump_expression( | |
11681 | Ast_dump_context* ast_dump_context) const | |
11682 | { | |
ffe743ca | 11683 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 11684 | |
11685 | ast_dump_context->ostream() << "[" ; | |
11686 | if (length != NULL) | |
11687 | { | |
11688 | ast_dump_context->dump_expression(length); | |
11689 | } | |
11690 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 11691 | ast_dump_context->dump_type(this->type_); |
11692 | ast_dump_context->ostream() << "{" ; | |
ffe743ca | 11693 | if (this->indexes_ == NULL) |
11694 | ast_dump_context->dump_expression_list(this->vals_); | |
11695 | else | |
11696 | { | |
11697 | Expression_list::const_iterator pv = this->vals_->begin(); | |
11698 | for (std::vector<unsigned long>::const_iterator pi = | |
11699 | this->indexes_->begin(); | |
11700 | pi != this->indexes_->end(); | |
11701 | ++pi, ++pv) | |
11702 | { | |
11703 | if (pi != this->indexes_->begin()) | |
11704 | ast_dump_context->ostream() << ", "; | |
11705 | ast_dump_context->ostream() << *pi << ':'; | |
11706 | ast_dump_context->dump_expression(*pv); | |
11707 | } | |
11708 | } | |
d751bb78 | 11709 | ast_dump_context->ostream() << "}" ; |
11710 | ||
11711 | } | |
11712 | ||
e440a328 | 11713 | // Construct a fixed array. |
11714 | ||
11715 | class Fixed_array_construction_expression : | |
11716 | public Array_construction_expression | |
11717 | { | |
11718 | public: | |
ffe743ca | 11719 | Fixed_array_construction_expression(Type* type, |
11720 | const std::vector<unsigned long>* indexes, | |
11721 | Expression_list* vals, Location location) | |
e440a328 | 11722 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, |
ffe743ca | 11723 | type, indexes, vals, location) |
11724 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 11725 | |
11726 | protected: | |
11727 | Expression* | |
11728 | do_copy() | |
11729 | { | |
11730 | return new Fixed_array_construction_expression(this->type(), | |
ffe743ca | 11731 | this->indexes(), |
e440a328 | 11732 | (this->vals() == NULL |
11733 | ? NULL | |
11734 | : this->vals()->copy()), | |
11735 | this->location()); | |
11736 | } | |
11737 | ||
11738 | tree | |
11739 | do_get_tree(Translate_context*); | |
11740 | }; | |
11741 | ||
11742 | // Return a tree for constructing a fixed array. | |
11743 | ||
11744 | tree | |
11745 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
11746 | { | |
9f0e0513 | 11747 | Type* type = this->type(); |
11748 | Btype* btype = type->get_backend(context->gogo()); | |
11749 | return this->get_constructor_tree(context, type_to_tree(btype)); | |
e440a328 | 11750 | } |
11751 | ||
11752 | // Construct an open array. | |
11753 | ||
11754 | class Open_array_construction_expression : public Array_construction_expression | |
11755 | { | |
11756 | public: | |
ffe743ca | 11757 | Open_array_construction_expression(Type* type, |
11758 | const std::vector<unsigned long>* indexes, | |
11759 | Expression_list* vals, Location location) | |
e440a328 | 11760 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, |
ffe743ca | 11761 | type, indexes, vals, location) |
11762 | { go_assert(type->is_slice_type()); } | |
e440a328 | 11763 | |
11764 | protected: | |
11765 | // Note that taking the address of an open array literal is invalid. | |
11766 | ||
11767 | Expression* | |
11768 | do_copy() | |
11769 | { | |
11770 | return new Open_array_construction_expression(this->type(), | |
ffe743ca | 11771 | this->indexes(), |
e440a328 | 11772 | (this->vals() == NULL |
11773 | ? NULL | |
11774 | : this->vals()->copy()), | |
11775 | this->location()); | |
11776 | } | |
11777 | ||
11778 | tree | |
11779 | do_get_tree(Translate_context*); | |
11780 | }; | |
11781 | ||
11782 | // Return a tree for constructing an open array. | |
11783 | ||
11784 | tree | |
11785 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
11786 | { | |
f9c68f17 | 11787 | Array_type* array_type = this->type()->array_type(); |
11788 | if (array_type == NULL) | |
11789 | { | |
c484d925 | 11790 | go_assert(this->type()->is_error()); |
f9c68f17 | 11791 | return error_mark_node; |
11792 | } | |
11793 | ||
11794 | Type* element_type = array_type->element_type(); | |
9f0e0513 | 11795 | Btype* belement_type = element_type->get_backend(context->gogo()); |
11796 | tree element_type_tree = type_to_tree(belement_type); | |
3d60812e | 11797 | if (element_type_tree == error_mark_node) |
11798 | return error_mark_node; | |
11799 | ||
e440a328 | 11800 | tree values; |
11801 | tree length_tree; | |
11802 | if (this->vals() == NULL || this->vals()->empty()) | |
11803 | { | |
11804 | // We need to create a unique value. | |
11805 | tree max = size_int(0); | |
11806 | tree constructor_type = build_array_type(element_type_tree, | |
11807 | build_index_type(max)); | |
11808 | if (constructor_type == error_mark_node) | |
11809 | return error_mark_node; | |
11810 | VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1); | |
e82e4eb5 | 11811 | constructor_elt empty = {NULL, NULL}; |
11812 | constructor_elt* elt = VEC_quick_push(constructor_elt, vec, empty); | |
e440a328 | 11813 | elt->index = size_int(0); |
63697958 | 11814 | Gogo* gogo = context->gogo(); |
11815 | Btype* btype = element_type->get_backend(gogo); | |
11816 | elt->value = expr_to_tree(gogo->backend()->zero_expression(btype)); | |
e440a328 | 11817 | values = build_constructor(constructor_type, vec); |
11818 | if (TREE_CONSTANT(elt->value)) | |
11819 | TREE_CONSTANT(values) = 1; | |
11820 | length_tree = size_int(0); | |
11821 | } | |
11822 | else | |
11823 | { | |
ffe743ca | 11824 | unsigned long max_index; |
11825 | if (this->indexes() == NULL) | |
11826 | max_index = this->vals()->size() - 1; | |
11827 | else | |
00773463 | 11828 | max_index = this->indexes()->back(); |
ffe743ca | 11829 | tree max_tree = size_int(max_index); |
e440a328 | 11830 | tree constructor_type = build_array_type(element_type_tree, |
ffe743ca | 11831 | build_index_type(max_tree)); |
e440a328 | 11832 | if (constructor_type == error_mark_node) |
11833 | return error_mark_node; | |
11834 | values = this->get_constructor_tree(context, constructor_type); | |
ffe743ca | 11835 | length_tree = size_int(max_index + 1); |
e440a328 | 11836 | } |
11837 | ||
11838 | if (values == error_mark_node) | |
11839 | return error_mark_node; | |
11840 | ||
11841 | bool is_constant_initializer = TREE_CONSTANT(values); | |
d8829beb | 11842 | |
11843 | // We have to copy the initial values into heap memory if we are in | |
11844 | // a function or if the values are not constants. We also have to | |
11845 | // copy them if they may contain pointers in a non-constant context, | |
11846 | // as otherwise the garbage collector won't see them. | |
11847 | bool copy_to_heap = (context->function() != NULL | |
11848 | || !is_constant_initializer | |
11849 | || (element_type->has_pointer() | |
11850 | && !context->is_const())); | |
e440a328 | 11851 | |
11852 | if (is_constant_initializer) | |
11853 | { | |
b13c66cd | 11854 | tree tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 11855 | create_tmp_var_name("C"), TREE_TYPE(values)); |
11856 | DECL_EXTERNAL(tmp) = 0; | |
11857 | TREE_PUBLIC(tmp) = 0; | |
11858 | TREE_STATIC(tmp) = 1; | |
11859 | DECL_ARTIFICIAL(tmp) = 1; | |
d8829beb | 11860 | if (copy_to_heap) |
e440a328 | 11861 | { |
d8829beb | 11862 | // If we are not copying the value to the heap, we will only |
11863 | // initialize the value once, so we can use this directly | |
11864 | // rather than copying it. In that case we can't make it | |
11865 | // read-only, because the program is permitted to change it. | |
e440a328 | 11866 | TREE_READONLY(tmp) = 1; |
11867 | TREE_CONSTANT(tmp) = 1; | |
11868 | } | |
11869 | DECL_INITIAL(tmp) = values; | |
11870 | rest_of_decl_compilation(tmp, 1, 0); | |
11871 | values = tmp; | |
11872 | } | |
11873 | ||
11874 | tree space; | |
11875 | tree set; | |
d8829beb | 11876 | if (!copy_to_heap) |
e440a328 | 11877 | { |
d8829beb | 11878 | // the initializer will only run once. |
e440a328 | 11879 | space = build_fold_addr_expr(values); |
11880 | set = NULL_TREE; | |
11881 | } | |
11882 | else | |
11883 | { | |
11884 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
11885 | space = context->gogo()->allocate_memory(element_type, memsize, | |
11886 | this->location()); | |
11887 | space = save_expr(space); | |
11888 | ||
11889 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
b13c66cd | 11890 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
11891 | s); | |
e440a328 | 11892 | TREE_THIS_NOTRAP(ref) = 1; |
11893 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
11894 | } | |
11895 | ||
11896 | // Build a constructor for the open array. | |
11897 | ||
9f0e0513 | 11898 | tree type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
3d60812e | 11899 | if (type_tree == error_mark_node) |
11900 | return error_mark_node; | |
c484d925 | 11901 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 11902 | |
11903 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
11904 | ||
e82e4eb5 | 11905 | constructor_elt empty = {NULL, NULL}; |
11906 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, empty); | |
e440a328 | 11907 | tree field = TYPE_FIELDS(type_tree); |
c484d925 | 11908 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 11909 | elt->index = field; |
11910 | elt->value = fold_convert(TREE_TYPE(field), space); | |
11911 | ||
e82e4eb5 | 11912 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 11913 | field = DECL_CHAIN(field); |
c484d925 | 11914 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 11915 | elt->index = field; |
11916 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
11917 | ||
e82e4eb5 | 11918 | elt = VEC_quick_push(constructor_elt, init, empty); |
e440a328 | 11919 | field = DECL_CHAIN(field); |
c484d925 | 11920 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); |
e440a328 | 11921 | elt->index = field; |
11922 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
11923 | ||
11924 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 11925 | if (constructor == error_mark_node) |
11926 | return error_mark_node; | |
d8829beb | 11927 | if (!copy_to_heap) |
e440a328 | 11928 | TREE_CONSTANT(constructor) = 1; |
11929 | ||
11930 | if (set == NULL_TREE) | |
11931 | return constructor; | |
11932 | else | |
11933 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
11934 | } | |
11935 | ||
11936 | // Make a slice composite literal. This is used by the type | |
11937 | // descriptor code. | |
11938 | ||
11939 | Expression* | |
11940 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 11941 | Location location) |
e440a328 | 11942 | { |
411eb89e | 11943 | go_assert(type->is_slice_type()); |
ffe743ca | 11944 | return new Open_array_construction_expression(type, NULL, vals, location); |
e440a328 | 11945 | } |
11946 | ||
11947 | // Construct a map. | |
11948 | ||
11949 | class Map_construction_expression : public Expression | |
11950 | { | |
11951 | public: | |
11952 | Map_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 11953 | Location location) |
e440a328 | 11954 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), |
11955 | type_(type), vals_(vals) | |
c484d925 | 11956 | { go_assert(vals == NULL || vals->size() % 2 == 0); } |
e440a328 | 11957 | |
11958 | protected: | |
11959 | int | |
11960 | do_traverse(Traverse* traverse); | |
11961 | ||
11962 | Type* | |
11963 | do_type() | |
11964 | { return this->type_; } | |
11965 | ||
11966 | void | |
11967 | do_determine_type(const Type_context*); | |
11968 | ||
11969 | void | |
11970 | do_check_types(Gogo*); | |
11971 | ||
11972 | Expression* | |
11973 | do_copy() | |
11974 | { | |
11975 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
11976 | this->location()); | |
11977 | } | |
11978 | ||
11979 | tree | |
11980 | do_get_tree(Translate_context*); | |
11981 | ||
11982 | void | |
11983 | do_export(Export*) const; | |
11984 | ||
d751bb78 | 11985 | void |
11986 | do_dump_expression(Ast_dump_context*) const; | |
11987 | ||
e440a328 | 11988 | private: |
11989 | // The type of the map to construct. | |
11990 | Type* type_; | |
11991 | // The list of values. | |
11992 | Expression_list* vals_; | |
11993 | }; | |
11994 | ||
11995 | // Traversal. | |
11996 | ||
11997 | int | |
11998 | Map_construction_expression::do_traverse(Traverse* traverse) | |
11999 | { | |
12000 | if (this->vals_ != NULL | |
12001 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12002 | return TRAVERSE_EXIT; | |
12003 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12004 | return TRAVERSE_EXIT; | |
12005 | return TRAVERSE_CONTINUE; | |
12006 | } | |
12007 | ||
12008 | // Final type determination. | |
12009 | ||
12010 | void | |
12011 | Map_construction_expression::do_determine_type(const Type_context*) | |
12012 | { | |
12013 | if (this->vals_ == NULL) | |
12014 | return; | |
12015 | ||
12016 | Map_type* mt = this->type_->map_type(); | |
12017 | Type_context key_context(mt->key_type(), false); | |
12018 | Type_context val_context(mt->val_type(), false); | |
12019 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12020 | pv != this->vals_->end(); | |
12021 | ++pv) | |
12022 | { | |
12023 | (*pv)->determine_type(&key_context); | |
12024 | ++pv; | |
12025 | (*pv)->determine_type(&val_context); | |
12026 | } | |
12027 | } | |
12028 | ||
12029 | // Check types. | |
12030 | ||
12031 | void | |
12032 | Map_construction_expression::do_check_types(Gogo*) | |
12033 | { | |
12034 | if (this->vals_ == NULL) | |
12035 | return; | |
12036 | ||
12037 | Map_type* mt = this->type_->map_type(); | |
12038 | int i = 0; | |
12039 | Type* key_type = mt->key_type(); | |
12040 | Type* val_type = mt->val_type(); | |
12041 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12042 | pv != this->vals_->end(); | |
12043 | ++pv, ++i) | |
12044 | { | |
12045 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
12046 | { | |
12047 | error_at((*pv)->location(), | |
12048 | "incompatible type for element %d key in map construction", | |
12049 | i + 1); | |
12050 | this->set_is_error(); | |
12051 | } | |
12052 | ++pv; | |
12053 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
12054 | { | |
12055 | error_at((*pv)->location(), | |
12056 | ("incompatible type for element %d value " | |
12057 | "in map construction"), | |
12058 | i + 1); | |
12059 | this->set_is_error(); | |
12060 | } | |
12061 | } | |
12062 | } | |
12063 | ||
12064 | // Return a tree for constructing a map. | |
12065 | ||
12066 | tree | |
12067 | Map_construction_expression::do_get_tree(Translate_context* context) | |
12068 | { | |
12069 | Gogo* gogo = context->gogo(); | |
b13c66cd | 12070 | Location loc = this->location(); |
e440a328 | 12071 | |
12072 | Map_type* mt = this->type_->map_type(); | |
12073 | ||
12074 | // Build a struct to hold the key and value. | |
12075 | tree struct_type = make_node(RECORD_TYPE); | |
12076 | ||
12077 | Type* key_type = mt->key_type(); | |
12078 | tree id = get_identifier("__key"); | |
9f0e0513 | 12079 | tree key_type_tree = type_to_tree(key_type->get_backend(gogo)); |
5845bde6 | 12080 | if (key_type_tree == error_mark_node) |
12081 | return error_mark_node; | |
b13c66cd | 12082 | tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id, |
12083 | key_type_tree); | |
e440a328 | 12084 | DECL_CONTEXT(key_field) = struct_type; |
12085 | TYPE_FIELDS(struct_type) = key_field; | |
12086 | ||
12087 | Type* val_type = mt->val_type(); | |
12088 | id = get_identifier("__val"); | |
9f0e0513 | 12089 | tree val_type_tree = type_to_tree(val_type->get_backend(gogo)); |
5845bde6 | 12090 | if (val_type_tree == error_mark_node) |
12091 | return error_mark_node; | |
b13c66cd | 12092 | tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id, |
12093 | val_type_tree); | |
e440a328 | 12094 | DECL_CONTEXT(val_field) = struct_type; |
12095 | DECL_CHAIN(key_field) = val_field; | |
12096 | ||
12097 | layout_type(struct_type); | |
12098 | ||
12099 | bool is_constant = true; | |
12100 | size_t i = 0; | |
12101 | tree valaddr; | |
12102 | tree make_tmp; | |
12103 | ||
12104 | if (this->vals_ == NULL || this->vals_->empty()) | |
12105 | { | |
12106 | valaddr = null_pointer_node; | |
12107 | make_tmp = NULL_TREE; | |
12108 | } | |
12109 | else | |
12110 | { | |
12111 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
12112 | this->vals_->size() / 2); | |
12113 | ||
12114 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12115 | pv != this->vals_->end(); | |
12116 | ++pv, ++i) | |
12117 | { | |
12118 | bool one_is_constant = true; | |
12119 | ||
12120 | VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2); | |
12121 | ||
e82e4eb5 | 12122 | constructor_elt empty = {NULL, NULL}; |
12123 | constructor_elt* elt = VEC_quick_push(constructor_elt, one, empty); | |
e440a328 | 12124 | elt->index = key_field; |
12125 | tree val_tree = (*pv)->get_tree(context); | |
12126 | elt->value = Expression::convert_for_assignment(context, key_type, | |
12127 | (*pv)->type(), | |
12128 | val_tree, loc); | |
12129 | if (elt->value == error_mark_node) | |
12130 | return error_mark_node; | |
12131 | if (!TREE_CONSTANT(elt->value)) | |
12132 | one_is_constant = false; | |
12133 | ||
12134 | ++pv; | |
12135 | ||
e82e4eb5 | 12136 | elt = VEC_quick_push(constructor_elt, one, empty); |
e440a328 | 12137 | elt->index = val_field; |
12138 | val_tree = (*pv)->get_tree(context); | |
12139 | elt->value = Expression::convert_for_assignment(context, val_type, | |
12140 | (*pv)->type(), | |
12141 | val_tree, loc); | |
12142 | if (elt->value == error_mark_node) | |
12143 | return error_mark_node; | |
12144 | if (!TREE_CONSTANT(elt->value)) | |
12145 | one_is_constant = false; | |
12146 | ||
e82e4eb5 | 12147 | elt = VEC_quick_push(constructor_elt, values, empty); |
e440a328 | 12148 | elt->index = size_int(i); |
12149 | elt->value = build_constructor(struct_type, one); | |
12150 | if (one_is_constant) | |
12151 | TREE_CONSTANT(elt->value) = 1; | |
12152 | else | |
12153 | is_constant = false; | |
12154 | } | |
12155 | ||
12156 | tree index_type = build_index_type(size_int(i - 1)); | |
12157 | tree array_type = build_array_type(struct_type, index_type); | |
12158 | tree init = build_constructor(array_type, values); | |
12159 | if (is_constant) | |
12160 | TREE_CONSTANT(init) = 1; | |
12161 | tree tmp; | |
12162 | if (current_function_decl != NULL) | |
12163 | { | |
12164 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
12165 | DECL_INITIAL(tmp) = init; | |
b13c66cd | 12166 | make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR, |
12167 | void_type_node, tmp); | |
e440a328 | 12168 | TREE_ADDRESSABLE(tmp) = 1; |
12169 | } | |
12170 | else | |
12171 | { | |
b13c66cd | 12172 | tmp = build_decl(loc.gcc_location(), VAR_DECL, |
12173 | create_tmp_var_name("M"), array_type); | |
e440a328 | 12174 | DECL_EXTERNAL(tmp) = 0; |
12175 | TREE_PUBLIC(tmp) = 0; | |
12176 | TREE_STATIC(tmp) = 1; | |
12177 | DECL_ARTIFICIAL(tmp) = 1; | |
12178 | if (!TREE_CONSTANT(init)) | |
b13c66cd | 12179 | make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR, |
12180 | void_type_node, tmp, init); | |
e440a328 | 12181 | else |
12182 | { | |
12183 | TREE_READONLY(tmp) = 1; | |
12184 | TREE_CONSTANT(tmp) = 1; | |
12185 | DECL_INITIAL(tmp) = init; | |
12186 | make_tmp = NULL_TREE; | |
12187 | } | |
12188 | rest_of_decl_compilation(tmp, 1, 0); | |
12189 | } | |
12190 | ||
12191 | valaddr = build_fold_addr_expr(tmp); | |
12192 | } | |
12193 | ||
2b5f213d | 12194 | tree descriptor = mt->map_descriptor_pointer(gogo, loc); |
e440a328 | 12195 | |
9f0e0513 | 12196 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
5845bde6 | 12197 | if (type_tree == error_mark_node) |
12198 | return error_mark_node; | |
e440a328 | 12199 | |
12200 | static tree construct_map_fndecl; | |
12201 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
12202 | loc, | |
12203 | "__go_construct_map", | |
12204 | 6, | |
12205 | type_tree, | |
12206 | TREE_TYPE(descriptor), | |
12207 | descriptor, | |
12208 | sizetype, | |
12209 | size_int(i), | |
12210 | sizetype, | |
12211 | TYPE_SIZE_UNIT(struct_type), | |
12212 | sizetype, | |
12213 | byte_position(val_field), | |
12214 | sizetype, | |
12215 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
12216 | const_ptr_type_node, | |
12217 | fold_convert(const_ptr_type_node, valaddr)); | |
5fb82b5e | 12218 | if (call == error_mark_node) |
12219 | return error_mark_node; | |
e440a328 | 12220 | |
12221 | tree ret; | |
12222 | if (make_tmp == NULL) | |
12223 | ret = call; | |
12224 | else | |
b13c66cd | 12225 | ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree, |
12226 | make_tmp, call); | |
e440a328 | 12227 | return ret; |
12228 | } | |
12229 | ||
12230 | // Export an array construction. | |
12231 | ||
12232 | void | |
12233 | Map_construction_expression::do_export(Export* exp) const | |
12234 | { | |
12235 | exp->write_c_string("convert("); | |
12236 | exp->write_type(this->type_); | |
12237 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12238 | pv != this->vals_->end(); | |
12239 | ++pv) | |
12240 | { | |
12241 | exp->write_c_string(", "); | |
12242 | (*pv)->export_expression(exp); | |
12243 | } | |
12244 | exp->write_c_string(")"); | |
12245 | } | |
12246 | ||
d751bb78 | 12247 | // Dump ast representation for a map construction expression. |
12248 | ||
12249 | void | |
12250 | Map_construction_expression::do_dump_expression( | |
12251 | Ast_dump_context* ast_dump_context) const | |
12252 | { | |
d751bb78 | 12253 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 12254 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 12255 | ast_dump_context->ostream() << "}"; |
12256 | } | |
12257 | ||
e440a328 | 12258 | // A general composite literal. This is lowered to a type specific |
12259 | // version. | |
12260 | ||
12261 | class Composite_literal_expression : public Parser_expression | |
12262 | { | |
12263 | public: | |
12264 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
b13c66cd | 12265 | Expression_list* vals, Location location) |
e440a328 | 12266 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), |
12267 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys) | |
12268 | { } | |
12269 | ||
12270 | protected: | |
12271 | int | |
12272 | do_traverse(Traverse* traverse); | |
12273 | ||
12274 | Expression* | |
ceeb4318 | 12275 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 12276 | |
12277 | Expression* | |
12278 | do_copy() | |
12279 | { | |
12280 | return new Composite_literal_expression(this->type_, this->depth_, | |
12281 | this->has_keys_, | |
12282 | (this->vals_ == NULL | |
12283 | ? NULL | |
12284 | : this->vals_->copy()), | |
12285 | this->location()); | |
12286 | } | |
12287 | ||
d751bb78 | 12288 | void |
12289 | do_dump_expression(Ast_dump_context*) const; | |
12290 | ||
e440a328 | 12291 | private: |
12292 | Expression* | |
81c4b26b | 12293 | lower_struct(Gogo*, Type*); |
e440a328 | 12294 | |
12295 | Expression* | |
113ef6a5 | 12296 | lower_array(Type*); |
e440a328 | 12297 | |
12298 | Expression* | |
ffe743ca | 12299 | make_array(Type*, const std::vector<unsigned long>*, Expression_list*); |
e440a328 | 12300 | |
12301 | Expression* | |
ceeb4318 | 12302 | lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); |
e440a328 | 12303 | |
12304 | // The type of the composite literal. | |
12305 | Type* type_; | |
12306 | // The depth within a list of composite literals within a composite | |
12307 | // literal, when the type is omitted. | |
12308 | int depth_; | |
12309 | // The values to put in the composite literal. | |
12310 | Expression_list* vals_; | |
12311 | // If this is true, then VALS_ is a list of pairs: a key and a | |
12312 | // value. In an array initializer, a missing key will be NULL. | |
12313 | bool has_keys_; | |
12314 | }; | |
12315 | ||
12316 | // Traversal. | |
12317 | ||
12318 | int | |
12319 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
12320 | { | |
12321 | if (this->vals_ != NULL | |
12322 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12323 | return TRAVERSE_EXIT; | |
12324 | return Type::traverse(this->type_, traverse); | |
12325 | } | |
12326 | ||
12327 | // Lower a generic composite literal into a specific version based on | |
12328 | // the type. | |
12329 | ||
12330 | Expression* | |
ceeb4318 | 12331 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
12332 | Statement_inserter* inserter, int) | |
e440a328 | 12333 | { |
12334 | Type* type = this->type_; | |
12335 | ||
12336 | for (int depth = this->depth_; depth > 0; --depth) | |
12337 | { | |
12338 | if (type->array_type() != NULL) | |
12339 | type = type->array_type()->element_type(); | |
12340 | else if (type->map_type() != NULL) | |
12341 | type = type->map_type()->val_type(); | |
12342 | else | |
12343 | { | |
5c13bd80 | 12344 | if (!type->is_error()) |
e440a328 | 12345 | error_at(this->location(), |
12346 | ("may only omit types within composite literals " | |
12347 | "of slice, array, or map type")); | |
12348 | return Expression::make_error(this->location()); | |
12349 | } | |
12350 | } | |
12351 | ||
e00772b3 | 12352 | Type *pt = type->points_to(); |
12353 | bool is_pointer = false; | |
12354 | if (pt != NULL) | |
12355 | { | |
12356 | is_pointer = true; | |
12357 | type = pt; | |
12358 | } | |
12359 | ||
12360 | Expression* ret; | |
5c13bd80 | 12361 | if (type->is_error()) |
e440a328 | 12362 | return Expression::make_error(this->location()); |
12363 | else if (type->struct_type() != NULL) | |
e00772b3 | 12364 | ret = this->lower_struct(gogo, type); |
e440a328 | 12365 | else if (type->array_type() != NULL) |
113ef6a5 | 12366 | ret = this->lower_array(type); |
e440a328 | 12367 | else if (type->map_type() != NULL) |
e00772b3 | 12368 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 12369 | else |
12370 | { | |
12371 | error_at(this->location(), | |
12372 | ("expected struct, slice, array, or map type " | |
12373 | "for composite literal")); | |
12374 | return Expression::make_error(this->location()); | |
12375 | } | |
e00772b3 | 12376 | |
12377 | if (is_pointer) | |
12378 | ret = Expression::make_heap_composite(ret, this->location()); | |
12379 | ||
12380 | return ret; | |
e440a328 | 12381 | } |
12382 | ||
12383 | // Lower a struct composite literal. | |
12384 | ||
12385 | Expression* | |
81c4b26b | 12386 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 12387 | { |
b13c66cd | 12388 | Location location = this->location(); |
e440a328 | 12389 | Struct_type* st = type->struct_type(); |
12390 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 12391 | { |
e6013c28 | 12392 | if (this->vals_ != NULL |
12393 | && !this->vals_->empty() | |
12394 | && type->named_type() != NULL | |
12395 | && type->named_type()->named_object()->package() != NULL) | |
12396 | { | |
12397 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
12398 | pf != st->fields()->end(); | |
12399 | ++pf) | |
07daa4e7 | 12400 | { |
e6013c28 | 12401 | if (Gogo::is_hidden_name(pf->field_name())) |
07daa4e7 | 12402 | error_at(this->location(), |
e6013c28 | 12403 | "assignment of unexported field %qs in %qs literal", |
12404 | Gogo::message_name(pf->field_name()).c_str(), | |
12405 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12406 | } |
12407 | } | |
12408 | ||
12409 | return new Struct_construction_expression(type, this->vals_, location); | |
12410 | } | |
e440a328 | 12411 | |
12412 | size_t field_count = st->field_count(); | |
12413 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 12414 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 12415 | Expression_list::const_iterator p = this->vals_->begin(); |
12416 | while (p != this->vals_->end()) | |
12417 | { | |
12418 | Expression* name_expr = *p; | |
12419 | ||
12420 | ++p; | |
c484d925 | 12421 | go_assert(p != this->vals_->end()); |
e440a328 | 12422 | Expression* val = *p; |
12423 | ||
12424 | ++p; | |
12425 | ||
12426 | if (name_expr == NULL) | |
12427 | { | |
12428 | error_at(val->location(), "mixture of field and value initializers"); | |
12429 | return Expression::make_error(location); | |
12430 | } | |
12431 | ||
12432 | bool bad_key = false; | |
12433 | std::string name; | |
81c4b26b | 12434 | const Named_object* no = NULL; |
e440a328 | 12435 | switch (name_expr->classification()) |
12436 | { | |
12437 | case EXPRESSION_UNKNOWN_REFERENCE: | |
12438 | name = name_expr->unknown_expression()->name(); | |
12439 | break; | |
12440 | ||
12441 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 12442 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 12443 | break; |
12444 | ||
12445 | case EXPRESSION_TYPE: | |
12446 | { | |
12447 | Type* t = name_expr->type(); | |
12448 | Named_type* nt = t->named_type(); | |
12449 | if (nt == NULL) | |
12450 | bad_key = true; | |
12451 | else | |
81c4b26b | 12452 | no = nt->named_object(); |
e440a328 | 12453 | } |
12454 | break; | |
12455 | ||
12456 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 12457 | no = name_expr->var_expression()->named_object(); |
e440a328 | 12458 | break; |
12459 | ||
12460 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 12461 | no = name_expr->func_expression()->named_object(); |
e440a328 | 12462 | break; |
12463 | ||
12464 | case EXPRESSION_UNARY: | |
12465 | // If there is a local variable around with the same name as | |
12466 | // the field, and this occurs in the closure, then the | |
12467 | // parser may turn the field reference into an indirection | |
12468 | // through the closure. FIXME: This is a mess. | |
12469 | { | |
12470 | bad_key = true; | |
12471 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
12472 | if (ue->op() == OPERATOR_MULT) | |
12473 | { | |
12474 | Field_reference_expression* fre = | |
12475 | ue->operand()->field_reference_expression(); | |
12476 | if (fre != NULL) | |
12477 | { | |
12478 | Struct_type* st = | |
12479 | fre->expr()->type()->deref()->struct_type(); | |
12480 | if (st != NULL) | |
12481 | { | |
12482 | const Struct_field* sf = st->field(fre->field_index()); | |
12483 | name = sf->field_name(); | |
2d29d278 | 12484 | |
12485 | // See below. FIXME. | |
12486 | if (!Gogo::is_hidden_name(name) | |
12487 | && name[0] >= 'a' | |
12488 | && name[0] <= 'z') | |
12489 | { | |
12490 | if (gogo->lookup_global(name.c_str()) != NULL) | |
12491 | name = gogo->pack_hidden_name(name, false); | |
12492 | } | |
12493 | ||
e440a328 | 12494 | char buf[20]; |
12495 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
12496 | size_t buflen = strlen(buf); | |
12497 | if (name.compare(name.length() - buflen, buflen, buf) | |
12498 | == 0) | |
12499 | { | |
12500 | name = name.substr(0, name.length() - buflen); | |
12501 | bad_key = false; | |
12502 | } | |
12503 | } | |
12504 | } | |
12505 | } | |
12506 | } | |
12507 | break; | |
12508 | ||
12509 | default: | |
12510 | bad_key = true; | |
12511 | break; | |
12512 | } | |
12513 | if (bad_key) | |
12514 | { | |
12515 | error_at(name_expr->location(), "expected struct field name"); | |
12516 | return Expression::make_error(location); | |
12517 | } | |
12518 | ||
81c4b26b | 12519 | if (no != NULL) |
12520 | { | |
12521 | name = no->name(); | |
12522 | ||
12523 | // A predefined name won't be packed. If it starts with a | |
12524 | // lower case letter we need to check for that case, because | |
2d29d278 | 12525 | // the field name will be packed. FIXME. |
81c4b26b | 12526 | if (!Gogo::is_hidden_name(name) |
12527 | && name[0] >= 'a' | |
12528 | && name[0] <= 'z') | |
12529 | { | |
12530 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
12531 | if (gno == no) | |
12532 | name = gogo->pack_hidden_name(name, false); | |
12533 | } | |
12534 | } | |
12535 | ||
e440a328 | 12536 | unsigned int index; |
12537 | const Struct_field* sf = st->find_local_field(name, &index); | |
12538 | if (sf == NULL) | |
12539 | { | |
12540 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
12541 | Gogo::message_name(name).c_str(), | |
12542 | (type->named_type() != NULL | |
12543 | ? type->named_type()->message_name().c_str() | |
12544 | : "unnamed struct")); | |
12545 | return Expression::make_error(location); | |
12546 | } | |
12547 | if (vals[index] != NULL) | |
12548 | { | |
12549 | error_at(name_expr->location(), | |
12550 | "duplicate value for field %qs in %qs", | |
12551 | Gogo::message_name(name).c_str(), | |
12552 | (type->named_type() != NULL | |
12553 | ? type->named_type()->message_name().c_str() | |
12554 | : "unnamed struct")); | |
12555 | return Expression::make_error(location); | |
12556 | } | |
12557 | ||
07daa4e7 | 12558 | if (type->named_type() != NULL |
12559 | && type->named_type()->named_object()->package() != NULL | |
12560 | && Gogo::is_hidden_name(sf->field_name())) | |
12561 | error_at(name_expr->location(), | |
12562 | "assignment of unexported field %qs in %qs literal", | |
12563 | Gogo::message_name(sf->field_name()).c_str(), | |
12564 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 12565 | |
e440a328 | 12566 | vals[index] = val; |
0c4f5a19 | 12567 | traverse_order->push_back(index); |
e440a328 | 12568 | } |
12569 | ||
12570 | Expression_list* list = new Expression_list; | |
12571 | list->reserve(field_count); | |
12572 | for (size_t i = 0; i < field_count; ++i) | |
12573 | list->push_back(vals[i]); | |
12574 | ||
0c4f5a19 | 12575 | Struct_construction_expression* ret = |
12576 | new Struct_construction_expression(type, list, location); | |
12577 | ret->set_traverse_order(traverse_order); | |
12578 | return ret; | |
e440a328 | 12579 | } |
12580 | ||
00773463 | 12581 | // Used to sort an index/value array. |
12582 | ||
12583 | class Index_value_compare | |
12584 | { | |
12585 | public: | |
12586 | bool | |
12587 | operator()(const std::pair<unsigned long, Expression*>& a, | |
12588 | const std::pair<unsigned long, Expression*>& b) | |
12589 | { return a.first < b.first; } | |
12590 | }; | |
12591 | ||
e440a328 | 12592 | // Lower an array composite literal. |
12593 | ||
12594 | Expression* | |
113ef6a5 | 12595 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 12596 | { |
b13c66cd | 12597 | Location location = this->location(); |
e440a328 | 12598 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 12599 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 12600 | |
ffe743ca | 12601 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
12602 | indexes->reserve(this->vals_->size()); | |
00773463 | 12603 | bool indexes_out_of_order = false; |
ffe743ca | 12604 | Expression_list* vals = new Expression_list(); |
12605 | vals->reserve(this->vals_->size()); | |
e440a328 | 12606 | unsigned long index = 0; |
12607 | Expression_list::const_iterator p = this->vals_->begin(); | |
12608 | while (p != this->vals_->end()) | |
12609 | { | |
12610 | Expression* index_expr = *p; | |
12611 | ||
12612 | ++p; | |
c484d925 | 12613 | go_assert(p != this->vals_->end()); |
e440a328 | 12614 | Expression* val = *p; |
12615 | ||
12616 | ++p; | |
12617 | ||
ffe743ca | 12618 | if (index_expr == NULL) |
12619 | { | |
12620 | if (!indexes->empty()) | |
12621 | indexes->push_back(index); | |
12622 | } | |
12623 | else | |
e440a328 | 12624 | { |
ffe743ca | 12625 | if (indexes->empty() && !vals->empty()) |
12626 | { | |
12627 | for (size_t i = 0; i < vals->size(); ++i) | |
12628 | indexes->push_back(i); | |
12629 | } | |
12630 | ||
0c77715b | 12631 | Numeric_constant nc; |
12632 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 12633 | { |
e440a328 | 12634 | error_at(index_expr->location(), |
12635 | "index expression is not integer constant"); | |
12636 | return Expression::make_error(location); | |
12637 | } | |
6f6d9955 | 12638 | |
0c77715b | 12639 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 12640 | { |
0c77715b | 12641 | case Numeric_constant::NC_UL_VALID: |
12642 | break; | |
12643 | case Numeric_constant::NC_UL_NOTINT: | |
12644 | error_at(index_expr->location(), | |
12645 | "index expression is not integer constant"); | |
12646 | return Expression::make_error(location); | |
12647 | case Numeric_constant::NC_UL_NEGATIVE: | |
e440a328 | 12648 | error_at(index_expr->location(), "index expression is negative"); |
12649 | return Expression::make_error(location); | |
0c77715b | 12650 | case Numeric_constant::NC_UL_BIG: |
e440a328 | 12651 | error_at(index_expr->location(), "index value overflow"); |
12652 | return Expression::make_error(location); | |
0c77715b | 12653 | default: |
12654 | go_unreachable(); | |
e440a328 | 12655 | } |
6f6d9955 | 12656 | |
12657 | Named_type* ntype = Type::lookup_integer_type("int"); | |
12658 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 12659 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
12660 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 12661 | { |
6f6d9955 | 12662 | error_at(index_expr->location(), "index value overflow"); |
12663 | return Expression::make_error(location); | |
12664 | } | |
12665 | ||
ffe743ca | 12666 | if (std::find(indexes->begin(), indexes->end(), index) |
12667 | != indexes->end()) | |
e440a328 | 12668 | { |
ffe743ca | 12669 | error_at(index_expr->location(), "duplicate value for index %lu", |
e440a328 | 12670 | index); |
12671 | return Expression::make_error(location); | |
12672 | } | |
ffe743ca | 12673 | |
00773463 | 12674 | if (!indexes->empty() && index < indexes->back()) |
12675 | indexes_out_of_order = true; | |
12676 | ||
ffe743ca | 12677 | indexes->push_back(index); |
e440a328 | 12678 | } |
12679 | ||
ffe743ca | 12680 | vals->push_back(val); |
12681 | ||
e440a328 | 12682 | ++index; |
12683 | } | |
12684 | ||
ffe743ca | 12685 | if (indexes->empty()) |
12686 | { | |
12687 | delete indexes; | |
12688 | indexes = NULL; | |
12689 | } | |
e440a328 | 12690 | |
00773463 | 12691 | if (indexes_out_of_order) |
12692 | { | |
12693 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
12694 | ||
12695 | V v; | |
12696 | v.reserve(indexes->size()); | |
12697 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
12698 | for (Expression_list::const_iterator pe = vals->begin(); | |
12699 | pe != vals->end(); | |
12700 | ++pe, ++pi) | |
12701 | v.push_back(std::make_pair(*pi, *pe)); | |
12702 | ||
12703 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
12704 | ||
12705 | delete indexes; | |
12706 | delete vals; | |
12707 | indexes = new std::vector<unsigned long>(); | |
12708 | indexes->reserve(v.size()); | |
12709 | vals = new Expression_list(); | |
12710 | vals->reserve(v.size()); | |
12711 | ||
12712 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
12713 | { | |
12714 | indexes->push_back(p->first); | |
12715 | vals->push_back(p->second); | |
12716 | } | |
12717 | } | |
12718 | ||
ffe743ca | 12719 | return this->make_array(type, indexes, vals); |
e440a328 | 12720 | } |
12721 | ||
12722 | // Actually build the array composite literal. This handles | |
12723 | // [...]{...}. | |
12724 | ||
12725 | Expression* | |
ffe743ca | 12726 | Composite_literal_expression::make_array( |
12727 | Type* type, | |
12728 | const std::vector<unsigned long>* indexes, | |
12729 | Expression_list* vals) | |
e440a328 | 12730 | { |
b13c66cd | 12731 | Location location = this->location(); |
e440a328 | 12732 | Array_type* at = type->array_type(); |
ffe743ca | 12733 | |
e440a328 | 12734 | if (at->length() != NULL && at->length()->is_nil_expression()) |
12735 | { | |
ffe743ca | 12736 | size_t size; |
12737 | if (vals == NULL) | |
12738 | size = 0; | |
00773463 | 12739 | else if (indexes != NULL) |
12740 | size = indexes->back() + 1; | |
12741 | else | |
ffe743ca | 12742 | { |
12743 | size = vals->size(); | |
12744 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
12745 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
12746 | && size >> (it->bits() - 1) != 0) | |
12747 | { | |
12748 | error_at(location, "too many elements in composite literal"); | |
12749 | return Expression::make_error(location); | |
12750 | } | |
12751 | } | |
ffe743ca | 12752 | |
e440a328 | 12753 | mpz_t vlen; |
12754 | mpz_init_set_ui(vlen, size); | |
12755 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
12756 | mpz_clear(vlen); | |
12757 | at = Type::make_array_type(at->element_type(), elen); | |
12758 | type = at; | |
12759 | } | |
ffe743ca | 12760 | else if (at->length() != NULL |
12761 | && !at->length()->is_error_expression() | |
12762 | && this->vals_ != NULL) | |
12763 | { | |
12764 | Numeric_constant nc; | |
12765 | unsigned long val; | |
12766 | if (at->length()->numeric_constant_value(&nc) | |
12767 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
12768 | { | |
12769 | if (indexes == NULL) | |
12770 | { | |
12771 | if (this->vals_->size() > val) | |
12772 | { | |
12773 | error_at(location, "too many elements in composite literal"); | |
12774 | return Expression::make_error(location); | |
12775 | } | |
12776 | } | |
12777 | else | |
12778 | { | |
00773463 | 12779 | unsigned long max = indexes->back(); |
ffe743ca | 12780 | if (max >= val) |
12781 | { | |
12782 | error_at(location, | |
12783 | ("some element keys in composite literal " | |
12784 | "are out of range")); | |
12785 | return Expression::make_error(location); | |
12786 | } | |
12787 | } | |
12788 | } | |
12789 | } | |
12790 | ||
e440a328 | 12791 | if (at->length() != NULL) |
ffe743ca | 12792 | return new Fixed_array_construction_expression(type, indexes, vals, |
12793 | location); | |
e440a328 | 12794 | else |
ffe743ca | 12795 | return new Open_array_construction_expression(type, indexes, vals, |
12796 | location); | |
e440a328 | 12797 | } |
12798 | ||
12799 | // Lower a map composite literal. | |
12800 | ||
12801 | Expression* | |
a287720d | 12802 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 12803 | Statement_inserter* inserter, |
a287720d | 12804 | Type* type) |
e440a328 | 12805 | { |
b13c66cd | 12806 | Location location = this->location(); |
e440a328 | 12807 | if (this->vals_ != NULL) |
12808 | { | |
12809 | if (!this->has_keys_) | |
12810 | { | |
12811 | error_at(location, "map composite literal must have keys"); | |
12812 | return Expression::make_error(location); | |
12813 | } | |
12814 | ||
a287720d | 12815 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 12816 | p != this->vals_->end(); |
12817 | p += 2) | |
12818 | { | |
12819 | if (*p == NULL) | |
12820 | { | |
12821 | ++p; | |
12822 | error_at((*p)->location(), | |
12823 | "map composite literal must have keys for every value"); | |
12824 | return Expression::make_error(location); | |
12825 | } | |
a287720d | 12826 | // Make sure we have lowered the key; it may not have been |
12827 | // lowered in order to handle keys for struct composite | |
12828 | // literals. Lower it now to get the right error message. | |
12829 | if ((*p)->unknown_expression() != NULL) | |
12830 | { | |
12831 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 12832 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 12833 | go_assert((*p)->is_error_expression()); |
a287720d | 12834 | return Expression::make_error(location); |
12835 | } | |
e440a328 | 12836 | } |
12837 | } | |
12838 | ||
12839 | return new Map_construction_expression(type, this->vals_, location); | |
12840 | } | |
12841 | ||
d751bb78 | 12842 | // Dump ast representation for a composite literal expression. |
12843 | ||
12844 | void | |
12845 | Composite_literal_expression::do_dump_expression( | |
12846 | Ast_dump_context* ast_dump_context) const | |
12847 | { | |
8b1c301d | 12848 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 12849 | ast_dump_context->dump_type(this->type_); |
12850 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 12851 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 12852 | ast_dump_context->ostream() << "})"; |
12853 | } | |
12854 | ||
e440a328 | 12855 | // Make a composite literal expression. |
12856 | ||
12857 | Expression* | |
12858 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
12859 | Expression_list* vals, | |
b13c66cd | 12860 | Location location) |
e440a328 | 12861 | { |
12862 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
12863 | location); | |
12864 | } | |
12865 | ||
12866 | // Return whether this expression is a composite literal. | |
12867 | ||
12868 | bool | |
12869 | Expression::is_composite_literal() const | |
12870 | { | |
12871 | switch (this->classification_) | |
12872 | { | |
12873 | case EXPRESSION_COMPOSITE_LITERAL: | |
12874 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
12875 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
12876 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
12877 | case EXPRESSION_MAP_CONSTRUCTION: | |
12878 | return true; | |
12879 | default: | |
12880 | return false; | |
12881 | } | |
12882 | } | |
12883 | ||
12884 | // Return whether this expression is a composite literal which is not | |
12885 | // constant. | |
12886 | ||
12887 | bool | |
12888 | Expression::is_nonconstant_composite_literal() const | |
12889 | { | |
12890 | switch (this->classification_) | |
12891 | { | |
12892 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
12893 | { | |
12894 | const Struct_construction_expression *psce = | |
12895 | static_cast<const Struct_construction_expression*>(this); | |
12896 | return !psce->is_constant_struct(); | |
12897 | } | |
12898 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
12899 | { | |
12900 | const Fixed_array_construction_expression *pace = | |
12901 | static_cast<const Fixed_array_construction_expression*>(this); | |
12902 | return !pace->is_constant_array(); | |
12903 | } | |
12904 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
12905 | { | |
12906 | const Open_array_construction_expression *pace = | |
12907 | static_cast<const Open_array_construction_expression*>(this); | |
12908 | return !pace->is_constant_array(); | |
12909 | } | |
12910 | case EXPRESSION_MAP_CONSTRUCTION: | |
12911 | return true; | |
12912 | default: | |
12913 | return false; | |
12914 | } | |
12915 | } | |
12916 | ||
12917 | // Return true if this is a reference to a local variable. | |
12918 | ||
12919 | bool | |
12920 | Expression::is_local_variable() const | |
12921 | { | |
12922 | const Var_expression* ve = this->var_expression(); | |
12923 | if (ve == NULL) | |
12924 | return false; | |
12925 | const Named_object* no = ve->named_object(); | |
12926 | return (no->is_result_variable() | |
12927 | || (no->is_variable() && !no->var_value()->is_global())); | |
12928 | } | |
12929 | ||
12930 | // Class Type_guard_expression. | |
12931 | ||
12932 | // Traversal. | |
12933 | ||
12934 | int | |
12935 | Type_guard_expression::do_traverse(Traverse* traverse) | |
12936 | { | |
12937 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
12938 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12939 | return TRAVERSE_EXIT; | |
12940 | return TRAVERSE_CONTINUE; | |
12941 | } | |
12942 | ||
12943 | // Check types of a type guard expression. The expression must have | |
12944 | // an interface type, but the actual type conversion is checked at run | |
12945 | // time. | |
12946 | ||
12947 | void | |
12948 | Type_guard_expression::do_check_types(Gogo*) | |
12949 | { | |
e440a328 | 12950 | Type* expr_type = this->expr_->type(); |
7e9da23f | 12951 | if (expr_type->interface_type() == NULL) |
f725ade8 | 12952 | { |
5c13bd80 | 12953 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 12954 | this->report_error(_("type assertion only valid for interface types")); |
12955 | this->set_is_error(); | |
12956 | } | |
e440a328 | 12957 | else if (this->type_->interface_type() == NULL) |
12958 | { | |
12959 | std::string reason; | |
12960 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
12961 | &reason)) | |
12962 | { | |
5c13bd80 | 12963 | if (!this->type_->is_error()) |
e440a328 | 12964 | { |
f725ade8 | 12965 | if (reason.empty()) |
12966 | this->report_error(_("impossible type assertion: " | |
12967 | "type does not implement interface")); | |
12968 | else | |
12969 | error_at(this->location(), | |
12970 | ("impossible type assertion: " | |
12971 | "type does not implement interface (%s)"), | |
12972 | reason.c_str()); | |
e440a328 | 12973 | } |
f725ade8 | 12974 | this->set_is_error(); |
e440a328 | 12975 | } |
12976 | } | |
12977 | } | |
12978 | ||
12979 | // Return a tree for a type guard expression. | |
12980 | ||
12981 | tree | |
12982 | Type_guard_expression::do_get_tree(Translate_context* context) | |
12983 | { | |
e440a328 | 12984 | tree expr_tree = this->expr_->get_tree(context); |
12985 | if (expr_tree == error_mark_node) | |
12986 | return error_mark_node; | |
7e9da23f | 12987 | if (this->type_->interface_type() != NULL) |
e440a328 | 12988 | return Expression::convert_interface_to_interface(context, this->type_, |
12989 | this->expr_->type(), | |
12990 | expr_tree, true, | |
12991 | this->location()); | |
12992 | else | |
12993 | return Expression::convert_for_assignment(context, this->type_, | |
12994 | this->expr_->type(), expr_tree, | |
12995 | this->location()); | |
12996 | } | |
12997 | ||
d751bb78 | 12998 | // Dump ast representation for a type guard expression. |
12999 | ||
13000 | void | |
13001 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
13002 | const | |
13003 | { | |
13004 | this->expr_->dump_expression(ast_dump_context); | |
13005 | ast_dump_context->ostream() << "."; | |
13006 | ast_dump_context->dump_type(this->type_); | |
13007 | } | |
13008 | ||
e440a328 | 13009 | // Make a type guard expression. |
13010 | ||
13011 | Expression* | |
13012 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 13013 | Location location) |
e440a328 | 13014 | { |
13015 | return new Type_guard_expression(expr, type, location); | |
13016 | } | |
13017 | ||
13018 | // Class Heap_composite_expression. | |
13019 | ||
13020 | // When you take the address of a composite literal, it is allocated | |
13021 | // on the heap. This class implements that. | |
13022 | ||
13023 | class Heap_composite_expression : public Expression | |
13024 | { | |
13025 | public: | |
b13c66cd | 13026 | Heap_composite_expression(Expression* expr, Location location) |
e440a328 | 13027 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), |
13028 | expr_(expr) | |
13029 | { } | |
13030 | ||
13031 | protected: | |
13032 | int | |
13033 | do_traverse(Traverse* traverse) | |
13034 | { return Expression::traverse(&this->expr_, traverse); } | |
13035 | ||
13036 | Type* | |
13037 | do_type() | |
13038 | { return Type::make_pointer_type(this->expr_->type()); } | |
13039 | ||
13040 | void | |
13041 | do_determine_type(const Type_context*) | |
13042 | { this->expr_->determine_type_no_context(); } | |
13043 | ||
13044 | Expression* | |
13045 | do_copy() | |
13046 | { | |
13047 | return Expression::make_heap_composite(this->expr_->copy(), | |
13048 | this->location()); | |
13049 | } | |
13050 | ||
13051 | tree | |
13052 | do_get_tree(Translate_context*); | |
13053 | ||
13054 | // We only export global objects, and the parser does not generate | |
13055 | // this in global scope. | |
13056 | void | |
13057 | do_export(Export*) const | |
c3e6f413 | 13058 | { go_unreachable(); } |
e440a328 | 13059 | |
d751bb78 | 13060 | void |
13061 | do_dump_expression(Ast_dump_context*) const; | |
13062 | ||
e440a328 | 13063 | private: |
13064 | // The composite literal which is being put on the heap. | |
13065 | Expression* expr_; | |
13066 | }; | |
13067 | ||
13068 | // Return a tree which allocates a composite literal on the heap. | |
13069 | ||
13070 | tree | |
13071 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
13072 | { | |
13073 | tree expr_tree = this->expr_->get_tree(context); | |
6d3ed74c | 13074 | if (expr_tree == error_mark_node || TREE_TYPE(expr_tree) == error_mark_node) |
e440a328 | 13075 | return error_mark_node; |
13076 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
c484d925 | 13077 | go_assert(TREE_CODE(expr_size) == INTEGER_CST); |
e440a328 | 13078 | tree space = context->gogo()->allocate_memory(this->expr_->type(), |
13079 | expr_size, this->location()); | |
13080 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
13081 | space = save_expr(space); | |
b13c66cd | 13082 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
13083 | space); | |
e440a328 | 13084 | TREE_THIS_NOTRAP(ref) = 1; |
13085 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
13086 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
13087 | space); | |
b13c66cd | 13088 | SET_EXPR_LOCATION(ret, this->location().gcc_location()); |
e440a328 | 13089 | return ret; |
13090 | } | |
13091 | ||
d751bb78 | 13092 | // Dump ast representation for a heap composite expression. |
13093 | ||
13094 | void | |
13095 | Heap_composite_expression::do_dump_expression( | |
13096 | Ast_dump_context* ast_dump_context) const | |
13097 | { | |
13098 | ast_dump_context->ostream() << "&("; | |
13099 | ast_dump_context->dump_expression(this->expr_); | |
13100 | ast_dump_context->ostream() << ")"; | |
13101 | } | |
13102 | ||
e440a328 | 13103 | // Allocate a composite literal on the heap. |
13104 | ||
13105 | Expression* | |
b13c66cd | 13106 | Expression::make_heap_composite(Expression* expr, Location location) |
e440a328 | 13107 | { |
13108 | return new Heap_composite_expression(expr, location); | |
13109 | } | |
13110 | ||
13111 | // Class Receive_expression. | |
13112 | ||
13113 | // Return the type of a receive expression. | |
13114 | ||
13115 | Type* | |
13116 | Receive_expression::do_type() | |
13117 | { | |
13118 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
13119 | if (channel_type == NULL) | |
13120 | return Type::make_error_type(); | |
13121 | return channel_type->element_type(); | |
13122 | } | |
13123 | ||
13124 | // Check types for a receive expression. | |
13125 | ||
13126 | void | |
13127 | Receive_expression::do_check_types(Gogo*) | |
13128 | { | |
13129 | Type* type = this->channel_->type(); | |
5c13bd80 | 13130 | if (type->is_error()) |
e440a328 | 13131 | { |
13132 | this->set_is_error(); | |
13133 | return; | |
13134 | } | |
13135 | if (type->channel_type() == NULL) | |
13136 | { | |
13137 | this->report_error(_("expected channel")); | |
13138 | return; | |
13139 | } | |
13140 | if (!type->channel_type()->may_receive()) | |
13141 | { | |
13142 | this->report_error(_("invalid receive on send-only channel")); | |
13143 | return; | |
13144 | } | |
13145 | } | |
13146 | ||
13147 | // Get a tree for a receive expression. | |
13148 | ||
13149 | tree | |
13150 | Receive_expression::do_get_tree(Translate_context* context) | |
13151 | { | |
f24f10bb | 13152 | Location loc = this->location(); |
13153 | ||
e440a328 | 13154 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 13155 | if (channel_type == NULL) |
13156 | { | |
c484d925 | 13157 | go_assert(this->channel_->type()->is_error()); |
5b8368f4 | 13158 | return error_mark_node; |
13159 | } | |
f24f10bb | 13160 | |
13161 | Expression* td = Expression::make_type_descriptor(channel_type, loc); | |
13162 | tree td_tree = td->get_tree(context); | |
13163 | ||
e440a328 | 13164 | Type* element_type = channel_type->element_type(); |
9f0e0513 | 13165 | Btype* element_type_btype = element_type->get_backend(context->gogo()); |
13166 | tree element_type_tree = type_to_tree(element_type_btype); | |
e440a328 | 13167 | |
13168 | tree channel = this->channel_->get_tree(context); | |
13169 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
13170 | return error_mark_node; | |
13171 | ||
f24f10bb | 13172 | return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc); |
e440a328 | 13173 | } |
13174 | ||
d751bb78 | 13175 | // Dump ast representation for a receive expression. |
13176 | ||
13177 | void | |
13178 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13179 | { | |
13180 | ast_dump_context->ostream() << " <- " ; | |
13181 | ast_dump_context->dump_expression(channel_); | |
13182 | } | |
13183 | ||
e440a328 | 13184 | // Make a receive expression. |
13185 | ||
13186 | Receive_expression* | |
b13c66cd | 13187 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 13188 | { |
13189 | return new Receive_expression(channel, location); | |
13190 | } | |
13191 | ||
e440a328 | 13192 | // An expression which evaluates to a pointer to the type descriptor |
13193 | // of a type. | |
13194 | ||
13195 | class Type_descriptor_expression : public Expression | |
13196 | { | |
13197 | public: | |
b13c66cd | 13198 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 13199 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
13200 | type_(type) | |
13201 | { } | |
13202 | ||
13203 | protected: | |
13204 | Type* | |
13205 | do_type() | |
13206 | { return Type::make_type_descriptor_ptr_type(); } | |
13207 | ||
13208 | void | |
13209 | do_determine_type(const Type_context*) | |
13210 | { } | |
13211 | ||
13212 | Expression* | |
13213 | do_copy() | |
13214 | { return this; } | |
13215 | ||
13216 | tree | |
13217 | do_get_tree(Translate_context* context) | |
a1d23b41 | 13218 | { |
13219 | return this->type_->type_descriptor_pointer(context->gogo(), | |
13220 | this->location()); | |
13221 | } | |
e440a328 | 13222 | |
d751bb78 | 13223 | void |
13224 | do_dump_expression(Ast_dump_context*) const; | |
13225 | ||
e440a328 | 13226 | private: |
13227 | // The type for which this is the descriptor. | |
13228 | Type* type_; | |
13229 | }; | |
13230 | ||
d751bb78 | 13231 | // Dump ast representation for a type descriptor expression. |
13232 | ||
13233 | void | |
13234 | Type_descriptor_expression::do_dump_expression( | |
13235 | Ast_dump_context* ast_dump_context) const | |
13236 | { | |
13237 | ast_dump_context->dump_type(this->type_); | |
13238 | } | |
13239 | ||
e440a328 | 13240 | // Make a type descriptor expression. |
13241 | ||
13242 | Expression* | |
b13c66cd | 13243 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 13244 | { |
13245 | return new Type_descriptor_expression(type, location); | |
13246 | } | |
13247 | ||
13248 | // An expression which evaluates to some characteristic of a type. | |
13249 | // This is only used to initialize fields of a type descriptor. Using | |
13250 | // a new expression class is slightly inefficient but gives us a good | |
13251 | // separation between the frontend and the middle-end with regard to | |
13252 | // how types are laid out. | |
13253 | ||
13254 | class Type_info_expression : public Expression | |
13255 | { | |
13256 | public: | |
13257 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 13258 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 13259 | type_(type), type_info_(type_info) |
13260 | { } | |
13261 | ||
13262 | protected: | |
13263 | Type* | |
13264 | do_type(); | |
13265 | ||
13266 | void | |
13267 | do_determine_type(const Type_context*) | |
13268 | { } | |
13269 | ||
13270 | Expression* | |
13271 | do_copy() | |
13272 | { return this; } | |
13273 | ||
13274 | tree | |
13275 | do_get_tree(Translate_context* context); | |
13276 | ||
d751bb78 | 13277 | void |
13278 | do_dump_expression(Ast_dump_context*) const; | |
13279 | ||
e440a328 | 13280 | private: |
13281 | // The type for which we are getting information. | |
13282 | Type* type_; | |
13283 | // What information we want. | |
13284 | Type_info type_info_; | |
13285 | }; | |
13286 | ||
13287 | // The type is chosen to match what the type descriptor struct | |
13288 | // expects. | |
13289 | ||
13290 | Type* | |
13291 | Type_info_expression::do_type() | |
13292 | { | |
13293 | switch (this->type_info_) | |
13294 | { | |
13295 | case TYPE_INFO_SIZE: | |
13296 | return Type::lookup_integer_type("uintptr"); | |
13297 | case TYPE_INFO_ALIGNMENT: | |
13298 | case TYPE_INFO_FIELD_ALIGNMENT: | |
13299 | return Type::lookup_integer_type("uint8"); | |
13300 | default: | |
c3e6f413 | 13301 | go_unreachable(); |
e440a328 | 13302 | } |
13303 | } | |
13304 | ||
13305 | // Return type information in GENERIC. | |
13306 | ||
13307 | tree | |
13308 | Type_info_expression::do_get_tree(Translate_context* context) | |
13309 | { | |
927a01eb | 13310 | Btype* btype = this->type_->get_backend(context->gogo()); |
13311 | Gogo* gogo = context->gogo(); | |
13312 | size_t val; | |
13313 | switch (this->type_info_) | |
e440a328 | 13314 | { |
927a01eb | 13315 | case TYPE_INFO_SIZE: |
13316 | val = gogo->backend()->type_size(btype); | |
13317 | break; | |
13318 | case TYPE_INFO_ALIGNMENT: | |
13319 | val = gogo->backend()->type_alignment(btype); | |
13320 | break; | |
13321 | case TYPE_INFO_FIELD_ALIGNMENT: | |
13322 | val = gogo->backend()->type_field_alignment(btype); | |
13323 | break; | |
13324 | default: | |
13325 | go_unreachable(); | |
e440a328 | 13326 | } |
927a01eb | 13327 | tree val_type_tree = type_to_tree(this->type()->get_backend(gogo)); |
13328 | go_assert(val_type_tree != error_mark_node); | |
13329 | return build_int_cstu(val_type_tree, val); | |
e440a328 | 13330 | } |
13331 | ||
d751bb78 | 13332 | // Dump ast representation for a type info expression. |
13333 | ||
13334 | void | |
13335 | Type_info_expression::do_dump_expression( | |
13336 | Ast_dump_context* ast_dump_context) const | |
13337 | { | |
13338 | ast_dump_context->ostream() << "typeinfo("; | |
13339 | ast_dump_context->dump_type(this->type_); | |
13340 | ast_dump_context->ostream() << ","; | |
13341 | ast_dump_context->ostream() << | |
13342 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
13343 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
13344 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
13345 | : "unknown"); | |
13346 | ast_dump_context->ostream() << ")"; | |
13347 | } | |
13348 | ||
e440a328 | 13349 | // Make a type info expression. |
13350 | ||
13351 | Expression* | |
13352 | Expression::make_type_info(Type* type, Type_info type_info) | |
13353 | { | |
13354 | return new Type_info_expression(type, type_info); | |
13355 | } | |
13356 | ||
13357 | // An expression which evaluates to the offset of a field within a | |
13358 | // struct. This, like Type_info_expression, q.v., is only used to | |
13359 | // initialize fields of a type descriptor. | |
13360 | ||
13361 | class Struct_field_offset_expression : public Expression | |
13362 | { | |
13363 | public: | |
13364 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 13365 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
13366 | Linemap::predeclared_location()), | |
e440a328 | 13367 | type_(type), field_(field) |
13368 | { } | |
13369 | ||
13370 | protected: | |
13371 | Type* | |
13372 | do_type() | |
13373 | { return Type::lookup_integer_type("uintptr"); } | |
13374 | ||
13375 | void | |
13376 | do_determine_type(const Type_context*) | |
13377 | { } | |
13378 | ||
13379 | Expression* | |
13380 | do_copy() | |
13381 | { return this; } | |
13382 | ||
13383 | tree | |
13384 | do_get_tree(Translate_context* context); | |
13385 | ||
d751bb78 | 13386 | void |
13387 | do_dump_expression(Ast_dump_context*) const; | |
13388 | ||
e440a328 | 13389 | private: |
13390 | // The type of the struct. | |
13391 | Struct_type* type_; | |
13392 | // The field. | |
13393 | const Struct_field* field_; | |
13394 | }; | |
13395 | ||
13396 | // Return a struct field offset in GENERIC. | |
13397 | ||
13398 | tree | |
13399 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
13400 | { | |
9f0e0513 | 13401 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
e440a328 | 13402 | if (type_tree == error_mark_node) |
13403 | return error_mark_node; | |
13404 | ||
9f0e0513 | 13405 | tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
c484d925 | 13406 | go_assert(val_type_tree != error_mark_node); |
e440a328 | 13407 | |
13408 | const Struct_field_list* fields = this->type_->fields(); | |
13409 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
13410 | Struct_field_list::const_iterator p; | |
13411 | for (p = fields->begin(); | |
13412 | p != fields->end(); | |
13413 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
13414 | { | |
c484d925 | 13415 | go_assert(struct_field_tree != NULL_TREE); |
e440a328 | 13416 | if (&*p == this->field_) |
13417 | break; | |
13418 | } | |
c484d925 | 13419 | go_assert(&*p == this->field_); |
e440a328 | 13420 | |
13421 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
13422 | byte_position(struct_field_tree)); | |
13423 | } | |
13424 | ||
d751bb78 | 13425 | // Dump ast representation for a struct field offset expression. |
13426 | ||
13427 | void | |
13428 | Struct_field_offset_expression::do_dump_expression( | |
13429 | Ast_dump_context* ast_dump_context) const | |
13430 | { | |
13431 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 13432 | ast_dump_context->dump_type(this->type_); |
13433 | ast_dump_context->ostream() << '.'; | |
13434 | ast_dump_context->ostream() << | |
13435 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 13436 | ast_dump_context->ostream() << ")"; |
13437 | } | |
13438 | ||
e440a328 | 13439 | // Make an expression for a struct field offset. |
13440 | ||
13441 | Expression* | |
13442 | Expression::make_struct_field_offset(Struct_type* type, | |
13443 | const Struct_field* field) | |
13444 | { | |
13445 | return new Struct_field_offset_expression(type, field); | |
13446 | } | |
13447 | ||
a9182619 | 13448 | // An expression which evaluates to a pointer to the map descriptor of |
13449 | // a map type. | |
13450 | ||
13451 | class Map_descriptor_expression : public Expression | |
13452 | { | |
13453 | public: | |
b13c66cd | 13454 | Map_descriptor_expression(Map_type* type, Location location) |
a9182619 | 13455 | : Expression(EXPRESSION_MAP_DESCRIPTOR, location), |
13456 | type_(type) | |
13457 | { } | |
13458 | ||
13459 | protected: | |
13460 | Type* | |
13461 | do_type() | |
13462 | { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } | |
13463 | ||
13464 | void | |
13465 | do_determine_type(const Type_context*) | |
13466 | { } | |
13467 | ||
13468 | Expression* | |
13469 | do_copy() | |
13470 | { return this; } | |
13471 | ||
13472 | tree | |
13473 | do_get_tree(Translate_context* context) | |
13474 | { | |
13475 | return this->type_->map_descriptor_pointer(context->gogo(), | |
13476 | this->location()); | |
13477 | } | |
13478 | ||
d751bb78 | 13479 | void |
13480 | do_dump_expression(Ast_dump_context*) const; | |
13481 | ||
a9182619 | 13482 | private: |
13483 | // The type for which this is the descriptor. | |
13484 | Map_type* type_; | |
13485 | }; | |
13486 | ||
d751bb78 | 13487 | // Dump ast representation for a map descriptor expression. |
13488 | ||
13489 | void | |
13490 | Map_descriptor_expression::do_dump_expression( | |
13491 | Ast_dump_context* ast_dump_context) const | |
13492 | { | |
13493 | ast_dump_context->ostream() << "map_descriptor("; | |
13494 | ast_dump_context->dump_type(this->type_); | |
13495 | ast_dump_context->ostream() << ")"; | |
13496 | } | |
13497 | ||
a9182619 | 13498 | // Make a map descriptor expression. |
13499 | ||
13500 | Expression* | |
b13c66cd | 13501 | Expression::make_map_descriptor(Map_type* type, Location location) |
a9182619 | 13502 | { |
13503 | return new Map_descriptor_expression(type, location); | |
13504 | } | |
13505 | ||
e440a328 | 13506 | // An expression which evaluates to the address of an unnamed label. |
13507 | ||
13508 | class Label_addr_expression : public Expression | |
13509 | { | |
13510 | public: | |
b13c66cd | 13511 | Label_addr_expression(Label* label, Location location) |
e440a328 | 13512 | : Expression(EXPRESSION_LABEL_ADDR, location), |
13513 | label_(label) | |
13514 | { } | |
13515 | ||
13516 | protected: | |
13517 | Type* | |
13518 | do_type() | |
13519 | { return Type::make_pointer_type(Type::make_void_type()); } | |
13520 | ||
13521 | void | |
13522 | do_determine_type(const Type_context*) | |
13523 | { } | |
13524 | ||
13525 | Expression* | |
13526 | do_copy() | |
13527 | { return new Label_addr_expression(this->label_, this->location()); } | |
13528 | ||
13529 | tree | |
6e193e6f | 13530 | do_get_tree(Translate_context* context) |
13531 | { | |
e8816003 | 13532 | return expr_to_tree(this->label_->get_addr(context, this->location())); |
6e193e6f | 13533 | } |
e440a328 | 13534 | |
d751bb78 | 13535 | void |
13536 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
13537 | { ast_dump_context->ostream() << this->label_->name(); } | |
13538 | ||
e440a328 | 13539 | private: |
13540 | // The label whose address we are taking. | |
13541 | Label* label_; | |
13542 | }; | |
13543 | ||
13544 | // Make an expression for the address of an unnamed label. | |
13545 | ||
13546 | Expression* | |
b13c66cd | 13547 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 13548 | { |
13549 | return new Label_addr_expression(label, location); | |
13550 | } | |
13551 | ||
13552 | // Import an expression. This comes at the end in order to see the | |
13553 | // various class definitions. | |
13554 | ||
13555 | Expression* | |
13556 | Expression::import_expression(Import* imp) | |
13557 | { | |
13558 | int c = imp->peek_char(); | |
13559 | if (imp->match_c_string("- ") | |
13560 | || imp->match_c_string("! ") | |
13561 | || imp->match_c_string("^ ")) | |
13562 | return Unary_expression::do_import(imp); | |
13563 | else if (c == '(') | |
13564 | return Binary_expression::do_import(imp); | |
13565 | else if (imp->match_c_string("true") | |
13566 | || imp->match_c_string("false")) | |
13567 | return Boolean_expression::do_import(imp); | |
13568 | else if (c == '"') | |
13569 | return String_expression::do_import(imp); | |
13570 | else if (c == '-' || (c >= '0' && c <= '9')) | |
13571 | { | |
13572 | // This handles integers, floats and complex constants. | |
13573 | return Integer_expression::do_import(imp); | |
13574 | } | |
13575 | else if (imp->match_c_string("nil")) | |
13576 | return Nil_expression::do_import(imp); | |
13577 | else if (imp->match_c_string("convert")) | |
13578 | return Type_conversion_expression::do_import(imp); | |
13579 | else | |
13580 | { | |
13581 | error_at(imp->location(), "import error: expected expression"); | |
13582 | return Expression::make_error(imp->location()); | |
13583 | } | |
13584 | } | |
13585 | ||
13586 | // Class Expression_list. | |
13587 | ||
13588 | // Traverse the list. | |
13589 | ||
13590 | int | |
13591 | Expression_list::traverse(Traverse* traverse) | |
13592 | { | |
13593 | for (Expression_list::iterator p = this->begin(); | |
13594 | p != this->end(); | |
13595 | ++p) | |
13596 | { | |
13597 | if (*p != NULL) | |
13598 | { | |
13599 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
13600 | return TRAVERSE_EXIT; | |
13601 | } | |
13602 | } | |
13603 | return TRAVERSE_CONTINUE; | |
13604 | } | |
13605 | ||
13606 | // Copy the list. | |
13607 | ||
13608 | Expression_list* | |
13609 | Expression_list::copy() | |
13610 | { | |
13611 | Expression_list* ret = new Expression_list(); | |
13612 | for (Expression_list::iterator p = this->begin(); | |
13613 | p != this->end(); | |
13614 | ++p) | |
13615 | { | |
13616 | if (*p == NULL) | |
13617 | ret->push_back(NULL); | |
13618 | else | |
13619 | ret->push_back((*p)->copy()); | |
13620 | } | |
13621 | return ret; | |
13622 | } | |
13623 | ||
13624 | // Return whether an expression list has an error expression. | |
13625 | ||
13626 | bool | |
13627 | Expression_list::contains_error() const | |
13628 | { | |
13629 | for (Expression_list::const_iterator p = this->begin(); | |
13630 | p != this->end(); | |
13631 | ++p) | |
13632 | if (*p != NULL && (*p)->is_error_expression()) | |
13633 | return true; | |
13634 | return false; | |
13635 | } | |
0c77715b | 13636 | |
13637 | // Class Numeric_constant. | |
13638 | ||
13639 | // Destructor. | |
13640 | ||
13641 | Numeric_constant::~Numeric_constant() | |
13642 | { | |
13643 | this->clear(); | |
13644 | } | |
13645 | ||
13646 | // Copy constructor. | |
13647 | ||
13648 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
13649 | : classification_(a.classification_), type_(a.type_) | |
13650 | { | |
13651 | switch (a.classification_) | |
13652 | { | |
13653 | case NC_INVALID: | |
13654 | break; | |
13655 | case NC_INT: | |
13656 | case NC_RUNE: | |
13657 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
13658 | break; | |
13659 | case NC_FLOAT: | |
13660 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
13661 | break; | |
13662 | case NC_COMPLEX: | |
13663 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
13664 | GMP_RNDN); | |
13665 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
13666 | GMP_RNDN); | |
13667 | break; | |
13668 | default: | |
13669 | go_unreachable(); | |
13670 | } | |
13671 | } | |
13672 | ||
13673 | // Assignment operator. | |
13674 | ||
13675 | Numeric_constant& | |
13676 | Numeric_constant::operator=(const Numeric_constant& a) | |
13677 | { | |
13678 | this->clear(); | |
13679 | this->classification_ = a.classification_; | |
13680 | this->type_ = a.type_; | |
13681 | switch (a.classification_) | |
13682 | { | |
13683 | case NC_INVALID: | |
13684 | break; | |
13685 | case NC_INT: | |
13686 | case NC_RUNE: | |
13687 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
13688 | break; | |
13689 | case NC_FLOAT: | |
13690 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
13691 | break; | |
13692 | case NC_COMPLEX: | |
13693 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
13694 | GMP_RNDN); | |
13695 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
13696 | GMP_RNDN); | |
13697 | break; | |
13698 | default: | |
13699 | go_unreachable(); | |
13700 | } | |
13701 | return *this; | |
13702 | } | |
13703 | ||
13704 | // Clear the contents. | |
13705 | ||
13706 | void | |
13707 | Numeric_constant::clear() | |
13708 | { | |
13709 | switch (this->classification_) | |
13710 | { | |
13711 | case NC_INVALID: | |
13712 | break; | |
13713 | case NC_INT: | |
13714 | case NC_RUNE: | |
13715 | mpz_clear(this->u_.int_val); | |
13716 | break; | |
13717 | case NC_FLOAT: | |
13718 | mpfr_clear(this->u_.float_val); | |
13719 | break; | |
13720 | case NC_COMPLEX: | |
13721 | mpfr_clear(this->u_.complex_val.real); | |
13722 | mpfr_clear(this->u_.complex_val.imag); | |
13723 | break; | |
13724 | default: | |
13725 | go_unreachable(); | |
13726 | } | |
13727 | this->classification_ = NC_INVALID; | |
13728 | } | |
13729 | ||
13730 | // Set to an unsigned long value. | |
13731 | ||
13732 | void | |
13733 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
13734 | { | |
13735 | this->clear(); | |
13736 | this->classification_ = NC_INT; | |
13737 | this->type_ = type; | |
13738 | mpz_init_set_ui(this->u_.int_val, val); | |
13739 | } | |
13740 | ||
13741 | // Set to an integer value. | |
13742 | ||
13743 | void | |
13744 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
13745 | { | |
13746 | this->clear(); | |
13747 | this->classification_ = NC_INT; | |
13748 | this->type_ = type; | |
13749 | mpz_init_set(this->u_.int_val, val); | |
13750 | } | |
13751 | ||
13752 | // Set to a rune value. | |
13753 | ||
13754 | void | |
13755 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
13756 | { | |
13757 | this->clear(); | |
13758 | this->classification_ = NC_RUNE; | |
13759 | this->type_ = type; | |
13760 | mpz_init_set(this->u_.int_val, val); | |
13761 | } | |
13762 | ||
13763 | // Set to a floating point value. | |
13764 | ||
13765 | void | |
13766 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
13767 | { | |
13768 | this->clear(); | |
13769 | this->classification_ = NC_FLOAT; | |
13770 | this->type_ = type; | |
833b523c | 13771 | // Numeric constants do not have negative zero values, so remove |
13772 | // them here. They also don't have infinity or NaN values, but we | |
13773 | // should never see them here. | |
13774 | if (mpfr_zero_p(val)) | |
13775 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
13776 | else | |
13777 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 13778 | } |
13779 | ||
13780 | // Set to a complex value. | |
13781 | ||
13782 | void | |
13783 | Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag) | |
13784 | { | |
13785 | this->clear(); | |
13786 | this->classification_ = NC_COMPLEX; | |
13787 | this->type_ = type; | |
13788 | mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN); | |
13789 | mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN); | |
13790 | } | |
13791 | ||
13792 | // Get an int value. | |
13793 | ||
13794 | void | |
13795 | Numeric_constant::get_int(mpz_t* val) const | |
13796 | { | |
13797 | go_assert(this->is_int()); | |
13798 | mpz_init_set(*val, this->u_.int_val); | |
13799 | } | |
13800 | ||
13801 | // Get a rune value. | |
13802 | ||
13803 | void | |
13804 | Numeric_constant::get_rune(mpz_t* val) const | |
13805 | { | |
13806 | go_assert(this->is_rune()); | |
13807 | mpz_init_set(*val, this->u_.int_val); | |
13808 | } | |
13809 | ||
13810 | // Get a floating point value. | |
13811 | ||
13812 | void | |
13813 | Numeric_constant::get_float(mpfr_t* val) const | |
13814 | { | |
13815 | go_assert(this->is_float()); | |
13816 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
13817 | } | |
13818 | ||
13819 | // Get a complex value. | |
13820 | ||
13821 | void | |
13822 | Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const | |
13823 | { | |
13824 | go_assert(this->is_complex()); | |
13825 | mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN); | |
13826 | mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN); | |
13827 | } | |
13828 | ||
13829 | // Express value as unsigned long if possible. | |
13830 | ||
13831 | Numeric_constant::To_unsigned_long | |
13832 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
13833 | { | |
13834 | switch (this->classification_) | |
13835 | { | |
13836 | case NC_INT: | |
13837 | case NC_RUNE: | |
13838 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
13839 | case NC_FLOAT: | |
13840 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
13841 | case NC_COMPLEX: | |
13842 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
13843 | return NC_UL_NOTINT; | |
13844 | return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val); | |
13845 | default: | |
13846 | go_unreachable(); | |
13847 | } | |
13848 | } | |
13849 | ||
13850 | // Express integer value as unsigned long if possible. | |
13851 | ||
13852 | Numeric_constant::To_unsigned_long | |
13853 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
13854 | unsigned long *val) const | |
13855 | { | |
13856 | if (mpz_sgn(ival) < 0) | |
13857 | return NC_UL_NEGATIVE; | |
13858 | unsigned long ui = mpz_get_ui(ival); | |
13859 | if (mpz_cmp_ui(ival, ui) != 0) | |
13860 | return NC_UL_BIG; | |
13861 | *val = ui; | |
13862 | return NC_UL_VALID; | |
13863 | } | |
13864 | ||
13865 | // Express floating point value as unsigned long if possible. | |
13866 | ||
13867 | Numeric_constant::To_unsigned_long | |
13868 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
13869 | unsigned long *val) const | |
13870 | { | |
13871 | if (!mpfr_integer_p(fval)) | |
13872 | return NC_UL_NOTINT; | |
13873 | mpz_t ival; | |
13874 | mpz_init(ival); | |
13875 | mpfr_get_z(ival, fval, GMP_RNDN); | |
13876 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
13877 | mpz_clear(ival); | |
13878 | return ret; | |
13879 | } | |
13880 | ||
13881 | // Convert value to integer if possible. | |
13882 | ||
13883 | bool | |
13884 | Numeric_constant::to_int(mpz_t* val) const | |
13885 | { | |
13886 | switch (this->classification_) | |
13887 | { | |
13888 | case NC_INT: | |
13889 | case NC_RUNE: | |
13890 | mpz_init_set(*val, this->u_.int_val); | |
13891 | return true; | |
13892 | case NC_FLOAT: | |
13893 | if (!mpfr_integer_p(this->u_.float_val)) | |
13894 | return false; | |
13895 | mpz_init(*val); | |
13896 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
13897 | return true; | |
13898 | case NC_COMPLEX: | |
13899 | if (!mpfr_zero_p(this->u_.complex_val.imag) | |
13900 | || !mpfr_integer_p(this->u_.complex_val.real)) | |
13901 | return false; | |
13902 | mpz_init(*val); | |
13903 | mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN); | |
13904 | return true; | |
13905 | default: | |
13906 | go_unreachable(); | |
13907 | } | |
13908 | } | |
13909 | ||
13910 | // Convert value to floating point if possible. | |
13911 | ||
13912 | bool | |
13913 | Numeric_constant::to_float(mpfr_t* val) const | |
13914 | { | |
13915 | switch (this->classification_) | |
13916 | { | |
13917 | case NC_INT: | |
13918 | case NC_RUNE: | |
13919 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
13920 | return true; | |
13921 | case NC_FLOAT: | |
13922 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
13923 | return true; | |
13924 | case NC_COMPLEX: | |
13925 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
13926 | return false; | |
13927 | mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN); | |
13928 | return true; | |
13929 | default: | |
13930 | go_unreachable(); | |
13931 | } | |
13932 | } | |
13933 | ||
13934 | // Convert value to complex. | |
13935 | ||
13936 | bool | |
13937 | Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const | |
13938 | { | |
13939 | switch (this->classification_) | |
13940 | { | |
13941 | case NC_INT: | |
13942 | case NC_RUNE: | |
13943 | mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN); | |
13944 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
13945 | return true; | |
13946 | case NC_FLOAT: | |
13947 | mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN); | |
13948 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
13949 | return true; | |
13950 | case NC_COMPLEX: | |
13951 | mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN); | |
13952 | mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN); | |
13953 | return true; | |
13954 | default: | |
13955 | go_unreachable(); | |
13956 | } | |
13957 | } | |
13958 | ||
13959 | // Get the type. | |
13960 | ||
13961 | Type* | |
13962 | Numeric_constant::type() const | |
13963 | { | |
13964 | if (this->type_ != NULL) | |
13965 | return this->type_; | |
13966 | switch (this->classification_) | |
13967 | { | |
13968 | case NC_INT: | |
13969 | return Type::make_abstract_integer_type(); | |
13970 | case NC_RUNE: | |
13971 | return Type::make_abstract_character_type(); | |
13972 | case NC_FLOAT: | |
13973 | return Type::make_abstract_float_type(); | |
13974 | case NC_COMPLEX: | |
13975 | return Type::make_abstract_complex_type(); | |
13976 | default: | |
13977 | go_unreachable(); | |
13978 | } | |
13979 | } | |
13980 | ||
13981 | // If the constant can be expressed in TYPE, then set the type of the | |
13982 | // constant to TYPE and return true. Otherwise return false, and, if | |
13983 | // ISSUE_ERROR is true, report an appropriate error message. | |
13984 | ||
13985 | bool | |
13986 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
13987 | { | |
13988 | bool ret; | |
13989 | if (type == NULL) | |
13990 | ret = true; | |
13991 | else if (type->integer_type() != NULL) | |
13992 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
13993 | else if (type->float_type() != NULL) | |
13994 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
13995 | else if (type->complex_type() != NULL) | |
13996 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
13997 | else | |
13998 | go_unreachable(); | |
13999 | if (ret) | |
14000 | this->type_ = type; | |
14001 | return ret; | |
14002 | } | |
14003 | ||
14004 | // Check whether the constant can be expressed in an integer type. | |
14005 | ||
14006 | bool | |
14007 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
14008 | Location location) const | |
14009 | { | |
14010 | mpz_t val; | |
14011 | switch (this->classification_) | |
14012 | { | |
14013 | case NC_INT: | |
14014 | case NC_RUNE: | |
14015 | mpz_init_set(val, this->u_.int_val); | |
14016 | break; | |
14017 | ||
14018 | case NC_FLOAT: | |
14019 | if (!mpfr_integer_p(this->u_.float_val)) | |
14020 | { | |
14021 | if (issue_error) | |
14022 | error_at(location, "floating point constant truncated to integer"); | |
14023 | return false; | |
14024 | } | |
14025 | mpz_init(val); | |
14026 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
14027 | break; | |
14028 | ||
14029 | case NC_COMPLEX: | |
14030 | if (!mpfr_integer_p(this->u_.complex_val.real) | |
14031 | || !mpfr_zero_p(this->u_.complex_val.imag)) | |
14032 | { | |
14033 | if (issue_error) | |
14034 | error_at(location, "complex constant truncated to integer"); | |
14035 | return false; | |
14036 | } | |
14037 | mpz_init(val); | |
14038 | mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN); | |
14039 | break; | |
14040 | ||
14041 | default: | |
14042 | go_unreachable(); | |
14043 | } | |
14044 | ||
14045 | bool ret; | |
14046 | if (type->is_abstract()) | |
14047 | ret = true; | |
14048 | else | |
14049 | { | |
14050 | int bits = mpz_sizeinbase(val, 2); | |
14051 | if (type->is_unsigned()) | |
14052 | { | |
14053 | // For an unsigned type we can only accept a nonnegative | |
14054 | // number, and we must be able to represents at least BITS. | |
14055 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
14056 | } | |
14057 | else | |
14058 | { | |
14059 | // For a signed type we need an extra bit to indicate the | |
14060 | // sign. We have to handle the most negative integer | |
14061 | // specially. | |
14062 | ret = (bits + 1 <= type->bits() | |
14063 | || (bits <= type->bits() | |
14064 | && mpz_sgn(val) < 0 | |
14065 | && (mpz_scan1(val, 0) | |
14066 | == static_cast<unsigned long>(type->bits() - 1)) | |
14067 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
14068 | } | |
14069 | } | |
14070 | ||
14071 | if (!ret && issue_error) | |
14072 | error_at(location, "integer constant overflow"); | |
14073 | ||
14074 | return ret; | |
14075 | } | |
14076 | ||
14077 | // Check whether the constant can be expressed in a floating point | |
14078 | // type. | |
14079 | ||
14080 | bool | |
14081 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
14082 | Location location) const | |
14083 | { | |
14084 | mpfr_t val; | |
14085 | switch (this->classification_) | |
14086 | { | |
14087 | case NC_INT: | |
14088 | case NC_RUNE: | |
14089 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
14090 | break; | |
14091 | ||
14092 | case NC_FLOAT: | |
14093 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
14094 | break; | |
14095 | ||
14096 | case NC_COMPLEX: | |
14097 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
14098 | { | |
14099 | if (issue_error) | |
14100 | error_at(location, "complex constant truncated to float"); | |
14101 | return false; | |
14102 | } | |
14103 | mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN); | |
14104 | break; | |
14105 | ||
14106 | default: | |
14107 | go_unreachable(); | |
14108 | } | |
14109 | ||
14110 | bool ret; | |
14111 | if (type->is_abstract()) | |
14112 | ret = true; | |
14113 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
14114 | { | |
14115 | // A NaN or Infinity always fits in the range of the type. | |
14116 | ret = true; | |
14117 | } | |
14118 | else | |
14119 | { | |
14120 | mp_exp_t exp = mpfr_get_exp(val); | |
14121 | mp_exp_t max_exp; | |
14122 | switch (type->bits()) | |
14123 | { | |
14124 | case 32: | |
14125 | max_exp = 128; | |
14126 | break; | |
14127 | case 64: | |
14128 | max_exp = 1024; | |
14129 | break; | |
14130 | default: | |
14131 | go_unreachable(); | |
14132 | } | |
14133 | ||
14134 | ret = exp <= max_exp; | |
14135 | } | |
14136 | ||
14137 | mpfr_clear(val); | |
14138 | ||
14139 | if (!ret && issue_error) | |
14140 | error_at(location, "floating point constant overflow"); | |
14141 | ||
14142 | return ret; | |
14143 | } | |
14144 | ||
14145 | // Check whether the constant can be expressed in a complex type. | |
14146 | ||
14147 | bool | |
14148 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
14149 | Location location) const | |
14150 | { | |
14151 | if (type->is_abstract()) | |
14152 | return true; | |
14153 | ||
14154 | mp_exp_t max_exp; | |
14155 | switch (type->bits()) | |
14156 | { | |
14157 | case 64: | |
14158 | max_exp = 128; | |
14159 | break; | |
14160 | case 128: | |
14161 | max_exp = 1024; | |
14162 | break; | |
14163 | default: | |
14164 | go_unreachable(); | |
14165 | } | |
14166 | ||
14167 | mpfr_t real; | |
14168 | switch (this->classification_) | |
14169 | { | |
14170 | case NC_INT: | |
14171 | case NC_RUNE: | |
14172 | mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN); | |
14173 | break; | |
14174 | ||
14175 | case NC_FLOAT: | |
14176 | mpfr_init_set(real, this->u_.float_val, GMP_RNDN); | |
14177 | break; | |
14178 | ||
14179 | case NC_COMPLEX: | |
14180 | if (!mpfr_nan_p(this->u_.complex_val.imag) | |
14181 | && !mpfr_inf_p(this->u_.complex_val.imag) | |
14182 | && !mpfr_zero_p(this->u_.complex_val.imag)) | |
14183 | { | |
14184 | if (mpfr_get_exp(this->u_.complex_val.imag) > max_exp) | |
14185 | { | |
14186 | if (issue_error) | |
14187 | error_at(location, "complex imaginary part overflow"); | |
14188 | return false; | |
14189 | } | |
14190 | } | |
14191 | mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN); | |
14192 | break; | |
14193 | ||
14194 | default: | |
14195 | go_unreachable(); | |
14196 | } | |
14197 | ||
14198 | bool ret; | |
14199 | if (mpfr_nan_p(real) || mpfr_inf_p(real) || mpfr_zero_p(real)) | |
14200 | ret = true; | |
14201 | else | |
14202 | ret = mpfr_get_exp(real) <= max_exp; | |
14203 | ||
14204 | mpfr_clear(real); | |
14205 | ||
14206 | if (!ret && issue_error) | |
14207 | error_at(location, "complex real part overflow"); | |
14208 | ||
14209 | return ret; | |
14210 | } | |
14211 | ||
14212 | // Return an Expression for this value. | |
14213 | ||
14214 | Expression* | |
14215 | Numeric_constant::expression(Location loc) const | |
14216 | { | |
14217 | switch (this->classification_) | |
14218 | { | |
14219 | case NC_INT: | |
14220 | return Expression::make_integer(&this->u_.int_val, this->type_, loc); | |
14221 | case NC_RUNE: | |
14222 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
14223 | case NC_FLOAT: | |
14224 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
14225 | case NC_COMPLEX: | |
14226 | return Expression::make_complex(&this->u_.complex_val.real, | |
14227 | &this->u_.complex_val.imag, | |
14228 | this->type_, loc); | |
14229 | default: | |
14230 | go_unreachable(); | |
14231 | } | |
14232 | } |