]>
Commit | Line | Data |
---|---|---|
e440a328 | 1 | // expressions.cc -- Go frontend expression handling. |
2 | ||
3 | // Copyright 2009 The Go Authors. All rights reserved. | |
4 | // Use of this source code is governed by a BSD-style | |
5 | // license that can be found in the LICENSE file. | |
6 | ||
7 | #include "go-system.h" | |
8 | ||
ffe743ca | 9 | #include <algorithm> |
10 | ||
e440a328 | 11 | #include "toplev.h" |
12 | #include "intl.h" | |
13 | #include "tree.h" | |
14 | #include "gimple.h" | |
15 | #include "tree-iterator.h" | |
16 | #include "convert.h" | |
17 | #include "real.h" | |
18 | #include "realmpfr.h" | |
e440a328 | 19 | |
e440a328 | 20 | #include "go-c.h" |
21 | #include "gogo.h" | |
22 | #include "types.h" | |
23 | #include "export.h" | |
24 | #include "import.h" | |
25 | #include "statements.h" | |
26 | #include "lex.h" | |
a9182619 | 27 | #include "runtime.h" |
6e193e6f | 28 | #include "backend.h" |
e440a328 | 29 | #include "expressions.h" |
d751bb78 | 30 | #include "ast-dump.h" |
e440a328 | 31 | |
32 | // Class Expression. | |
33 | ||
34 | Expression::Expression(Expression_classification classification, | |
b13c66cd | 35 | Location location) |
e440a328 | 36 | : classification_(classification), location_(location) |
37 | { | |
38 | } | |
39 | ||
40 | Expression::~Expression() | |
41 | { | |
42 | } | |
43 | ||
e440a328 | 44 | // Traverse the expressions. |
45 | ||
46 | int | |
47 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
48 | { | |
49 | Expression* expr = *pexpr; | |
50 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
51 | { | |
52 | int t = traverse->expression(pexpr); | |
53 | if (t == TRAVERSE_EXIT) | |
54 | return TRAVERSE_EXIT; | |
55 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
56 | return TRAVERSE_CONTINUE; | |
57 | } | |
58 | return expr->do_traverse(traverse); | |
59 | } | |
60 | ||
61 | // Traverse subexpressions of this expression. | |
62 | ||
63 | int | |
64 | Expression::traverse_subexpressions(Traverse* traverse) | |
65 | { | |
66 | return this->do_traverse(traverse); | |
67 | } | |
68 | ||
69 | // Default implementation for do_traverse for child classes. | |
70 | ||
71 | int | |
72 | Expression::do_traverse(Traverse*) | |
73 | { | |
74 | return TRAVERSE_CONTINUE; | |
75 | } | |
76 | ||
77 | // This virtual function is called by the parser if the value of this | |
a7549a6a | 78 | // expression is being discarded. By default, we give an error. |
79 | // Expressions with side effects override. | |
e440a328 | 80 | |
4f2138d7 | 81 | bool |
e440a328 | 82 | Expression::do_discarding_value() |
83 | { | |
a7549a6a | 84 | this->unused_value_error(); |
4f2138d7 | 85 | return false; |
e440a328 | 86 | } |
87 | ||
88 | // This virtual function is called to export expressions. This will | |
89 | // only be used by expressions which may be constant. | |
90 | ||
91 | void | |
92 | Expression::do_export(Export*) const | |
93 | { | |
c3e6f413 | 94 | go_unreachable(); |
e440a328 | 95 | } |
96 | ||
a7549a6a | 97 | // Give an error saying that the value of the expression is not used. |
e440a328 | 98 | |
99 | void | |
a7549a6a | 100 | Expression::unused_value_error() |
e440a328 | 101 | { |
4f2138d7 | 102 | this->report_error(_("value computed is not used")); |
e440a328 | 103 | } |
104 | ||
105 | // Note that this expression is an error. This is called by children | |
106 | // when they discover an error. | |
107 | ||
108 | void | |
109 | Expression::set_is_error() | |
110 | { | |
111 | this->classification_ = EXPRESSION_ERROR; | |
112 | } | |
113 | ||
114 | // For children to call to report an error conveniently. | |
115 | ||
116 | void | |
117 | Expression::report_error(const char* msg) | |
118 | { | |
119 | error_at(this->location_, "%s", msg); | |
120 | this->set_is_error(); | |
121 | } | |
122 | ||
123 | // Set types of variables and constants. This is implemented by the | |
124 | // child class. | |
125 | ||
126 | void | |
127 | Expression::determine_type(const Type_context* context) | |
128 | { | |
129 | this->do_determine_type(context); | |
130 | } | |
131 | ||
132 | // Set types when there is no context. | |
133 | ||
134 | void | |
135 | Expression::determine_type_no_context() | |
136 | { | |
137 | Type_context context; | |
138 | this->do_determine_type(&context); | |
139 | } | |
140 | ||
141 | // Return a tree handling any conversions which must be done during | |
142 | // assignment. | |
143 | ||
144 | tree | |
145 | Expression::convert_for_assignment(Translate_context* context, Type* lhs_type, | |
146 | Type* rhs_type, tree rhs_tree, | |
b13c66cd | 147 | Location location) |
e440a328 | 148 | { |
5c13bd80 | 149 | if (lhs_type->is_error() || rhs_type->is_error()) |
e440a328 | 150 | return error_mark_node; |
151 | ||
e440a328 | 152 | if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node) |
153 | return error_mark_node; | |
154 | ||
155 | Gogo* gogo = context->gogo(); | |
156 | ||
9f0e0513 | 157 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 158 | if (lhs_type_tree == error_mark_node) |
159 | return error_mark_node; | |
160 | ||
54211955 | 161 | if (lhs_type->forwarded() != rhs_type->forwarded() |
162 | && lhs_type->interface_type() != NULL) | |
e440a328 | 163 | { |
164 | if (rhs_type->interface_type() == NULL) | |
165 | return Expression::convert_type_to_interface(context, lhs_type, | |
166 | rhs_type, rhs_tree, | |
167 | location); | |
168 | else | |
169 | return Expression::convert_interface_to_interface(context, lhs_type, | |
170 | rhs_type, rhs_tree, | |
171 | false, location); | |
172 | } | |
54211955 | 173 | else if (lhs_type->forwarded() != rhs_type->forwarded() |
174 | && rhs_type->interface_type() != NULL) | |
e440a328 | 175 | return Expression::convert_interface_to_type(context, lhs_type, rhs_type, |
176 | rhs_tree, location); | |
411eb89e | 177 | else if (lhs_type->is_slice_type() && rhs_type->is_nil_type()) |
e440a328 | 178 | { |
179 | // Assigning nil to an open array. | |
c484d925 | 180 | go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE); |
e440a328 | 181 | |
95f84544 | 182 | vec<constructor_elt, va_gc> *init; |
183 | vec_alloc(init, 3); | |
e440a328 | 184 | |
e82e4eb5 | 185 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 186 | constructor_elt* elt = init->quick_push(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 | ||
95f84544 | 193 | elt = init->quick_push(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 | ||
95f84544 | 200 | elt = init->quick_push(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 | ||
95f84544 | 323 | vec<constructor_elt, va_gc> *init; |
324 | vec_alloc(init, 2); | |
e440a328 | 325 | |
e82e4eb5 | 326 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 327 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 328 | tree field = TYPE_FIELDS(lhs_type_tree); |
c484d925 | 329 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 330 | (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0); |
331 | elt->index = field; | |
b13c66cd | 332 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
333 | first_field_value); | |
e440a328 | 334 | |
95f84544 | 335 | elt = init->quick_push(empty); |
e440a328 | 336 | field = DECL_CHAIN(field); |
c484d925 | 337 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 338 | elt->index = field; |
339 | ||
340 | if (rhs_type->points_to() != NULL) | |
341 | { | |
342 | // We are assigning a pointer to the interface; the interface | |
343 | // holds the pointer itself. | |
344 | elt->value = rhs_tree; | |
345 | return build_constructor(lhs_type_tree, init); | |
346 | } | |
347 | ||
348 | // We are assigning a non-pointer value to the interface; the | |
349 | // interface gets a copy of the value in the heap. | |
350 | ||
351 | tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree)); | |
352 | ||
353 | tree space = gogo->allocate_memory(rhs_type, object_size, location); | |
b13c66cd | 354 | space = fold_convert_loc(location.gcc_location(), |
355 | build_pointer_type(TREE_TYPE(rhs_tree)), space); | |
e440a328 | 356 | space = save_expr(space); |
357 | ||
b13c66cd | 358 | tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space); |
e440a328 | 359 | TREE_THIS_NOTRAP(ref) = 1; |
b13c66cd | 360 | tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR, |
361 | void_type_node, ref, rhs_tree); | |
e440a328 | 362 | |
b13c66cd | 363 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
364 | space); | |
e440a328 | 365 | |
366 | return build2(COMPOUND_EXPR, lhs_type_tree, set, | |
367 | build_constructor(lhs_type_tree, init)); | |
368 | } | |
369 | ||
370 | // Return a tree for the type descriptor of RHS_TREE, which has | |
371 | // interface type RHS_TYPE. If RHS_TREE is nil the result will be | |
372 | // NULL. | |
373 | ||
374 | tree | |
375 | Expression::get_interface_type_descriptor(Translate_context*, | |
376 | Type* rhs_type, tree rhs_tree, | |
b13c66cd | 377 | Location location) |
e440a328 | 378 | { |
379 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
c484d925 | 380 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 381 | tree rhs_field = TYPE_FIELDS(rhs_type_tree); |
382 | tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
383 | NULL_TREE); | |
384 | if (rhs_type->interface_type()->is_empty()) | |
385 | { | |
c484d925 | 386 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), |
e440a328 | 387 | "__type_descriptor") == 0); |
388 | return v; | |
389 | } | |
390 | ||
c484d925 | 391 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods") |
e440a328 | 392 | == 0); |
c484d925 | 393 | go_assert(POINTER_TYPE_P(TREE_TYPE(v))); |
e440a328 | 394 | v = save_expr(v); |
b13c66cd | 395 | tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v); |
c484d925 | 396 | go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE); |
e440a328 | 397 | tree f = TYPE_FIELDS(TREE_TYPE(v1)); |
c484d925 | 398 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor") |
e440a328 | 399 | == 0); |
400 | v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE); | |
401 | ||
b13c66cd | 402 | tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node, |
403 | v, fold_convert_loc(location.gcc_location(), | |
404 | TREE_TYPE(v), | |
405 | null_pointer_node)); | |
406 | tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1), | |
407 | null_pointer_node); | |
408 | return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1), | |
e440a328 | 409 | eq, n, v1); |
410 | } | |
411 | ||
412 | // Return a tree for the conversion of an interface type to an | |
413 | // interface type. | |
414 | ||
415 | tree | |
416 | Expression::convert_interface_to_interface(Translate_context* context, | |
417 | Type *lhs_type, Type *rhs_type, | |
418 | tree rhs_tree, bool for_type_guard, | |
b13c66cd | 419 | Location location) |
e440a328 | 420 | { |
421 | Gogo* gogo = context->gogo(); | |
422 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
423 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
424 | ||
9f0e0513 | 425 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 426 | if (lhs_type_tree == error_mark_node) |
427 | return error_mark_node; | |
428 | ||
429 | // In the general case this requires runtime examination of the type | |
430 | // method table to match it up with the interface methods. | |
431 | ||
432 | // FIXME: If all of the methods in the right hand side interface | |
433 | // also appear in the left hand side interface, then we don't need | |
434 | // to do a runtime check, although we still need to build a new | |
435 | // method table. | |
436 | ||
437 | // Get the type descriptor for the right hand side. This will be | |
438 | // NULL for a nil interface. | |
439 | ||
440 | if (!DECL_P(rhs_tree)) | |
441 | rhs_tree = save_expr(rhs_tree); | |
442 | ||
443 | tree rhs_type_descriptor = | |
444 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
445 | location); | |
446 | ||
447 | // The result is going to be a two element constructor. | |
448 | ||
95f84544 | 449 | vec<constructor_elt, va_gc> *init; |
450 | vec_alloc (init, 2); | |
e440a328 | 451 | |
e82e4eb5 | 452 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 453 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 454 | tree field = TYPE_FIELDS(lhs_type_tree); |
455 | elt->index = field; | |
456 | ||
457 | if (for_type_guard) | |
458 | { | |
459 | // A type assertion fails when converting a nil interface. | |
a1d23b41 | 460 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, |
461 | location); | |
e440a328 | 462 | static tree assert_interface_decl; |
463 | tree call = Gogo::call_builtin(&assert_interface_decl, | |
464 | location, | |
465 | "__go_assert_interface", | |
466 | 2, | |
467 | ptr_type_node, | |
468 | TREE_TYPE(lhs_type_descriptor), | |
469 | lhs_type_descriptor, | |
470 | TREE_TYPE(rhs_type_descriptor), | |
471 | rhs_type_descriptor); | |
5fb82b5e | 472 | if (call == error_mark_node) |
473 | return error_mark_node; | |
e440a328 | 474 | // This will panic if the interface conversion fails. |
475 | TREE_NOTHROW(assert_interface_decl) = 0; | |
b13c66cd | 476 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
477 | call); | |
e440a328 | 478 | } |
479 | else if (lhs_is_empty) | |
480 | { | |
481 | // A convertion to an empty interface always succeeds, and the | |
482 | // first field is just the type descriptor of the object. | |
c484d925 | 483 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), |
e440a328 | 484 | "__type_descriptor") == 0); |
7172c949 | 485 | elt->value = fold_convert_loc(location.gcc_location(), |
486 | TREE_TYPE(field), rhs_type_descriptor); | |
e440a328 | 487 | } |
488 | else | |
489 | { | |
490 | // A conversion to a non-empty interface may fail, but unlike a | |
491 | // type assertion converting nil will always succeed. | |
c484d925 | 492 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") |
e440a328 | 493 | == 0); |
a1d23b41 | 494 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, |
495 | location); | |
e440a328 | 496 | static tree convert_interface_decl; |
497 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
498 | location, | |
499 | "__go_convert_interface", | |
500 | 2, | |
501 | ptr_type_node, | |
502 | TREE_TYPE(lhs_type_descriptor), | |
503 | lhs_type_descriptor, | |
504 | TREE_TYPE(rhs_type_descriptor), | |
505 | rhs_type_descriptor); | |
5fb82b5e | 506 | if (call == error_mark_node) |
507 | return error_mark_node; | |
e440a328 | 508 | // This will panic if the interface conversion fails. |
509 | TREE_NOTHROW(convert_interface_decl) = 0; | |
b13c66cd | 510 | elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field), |
511 | call); | |
e440a328 | 512 | } |
513 | ||
514 | // The second field is simply the object pointer. | |
515 | ||
95f84544 | 516 | elt = init->quick_push(empty); |
e440a328 | 517 | field = DECL_CHAIN(field); |
c484d925 | 518 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 519 | elt->index = field; |
520 | ||
521 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
c484d925 | 522 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 523 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); |
c484d925 | 524 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); |
e440a328 | 525 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, |
526 | NULL_TREE); | |
527 | ||
528 | return build_constructor(lhs_type_tree, init); | |
529 | } | |
530 | ||
531 | // Return a tree for the conversion of an interface type to a | |
532 | // non-interface type. | |
533 | ||
534 | tree | |
535 | Expression::convert_interface_to_type(Translate_context* context, | |
536 | Type *lhs_type, Type* rhs_type, | |
b13c66cd | 537 | tree rhs_tree, Location location) |
e440a328 | 538 | { |
539 | Gogo* gogo = context->gogo(); | |
540 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
541 | ||
9f0e0513 | 542 | tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo)); |
e440a328 | 543 | if (lhs_type_tree == error_mark_node) |
544 | return error_mark_node; | |
545 | ||
546 | // Call a function to check that the type is valid. The function | |
547 | // will panic with an appropriate runtime type error if the type is | |
548 | // not valid. | |
549 | ||
a1d23b41 | 550 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location); |
e440a328 | 551 | |
552 | if (!DECL_P(rhs_tree)) | |
553 | rhs_tree = save_expr(rhs_tree); | |
554 | ||
555 | tree rhs_type_descriptor = | |
556 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
557 | location); | |
558 | ||
a1d23b41 | 559 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo, |
560 | location); | |
e440a328 | 561 | |
562 | static tree check_interface_type_decl; | |
563 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
564 | location, | |
565 | "__go_check_interface_type", | |
566 | 3, | |
567 | void_type_node, | |
568 | TREE_TYPE(lhs_type_descriptor), | |
569 | lhs_type_descriptor, | |
570 | TREE_TYPE(rhs_type_descriptor), | |
571 | rhs_type_descriptor, | |
572 | TREE_TYPE(rhs_inter_descriptor), | |
573 | rhs_inter_descriptor); | |
5fb82b5e | 574 | if (call == error_mark_node) |
575 | return error_mark_node; | |
e440a328 | 576 | // This call will panic if the conversion is invalid. |
577 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
578 | ||
579 | // If the call succeeds, pull out the value. | |
c484d925 | 580 | go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); |
e440a328 | 581 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); |
c484d925 | 582 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); |
e440a328 | 583 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, |
584 | NULL_TREE); | |
585 | ||
586 | // If the value is a pointer, then it is the value we want. | |
587 | // Otherwise it points to the value. | |
588 | if (lhs_type->points_to() == NULL) | |
589 | { | |
b13c66cd | 590 | val = fold_convert_loc(location.gcc_location(), |
591 | build_pointer_type(lhs_type_tree), val); | |
592 | val = build_fold_indirect_ref_loc(location.gcc_location(), val); | |
e440a328 | 593 | } |
594 | ||
595 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
b13c66cd | 596 | fold_convert_loc(location.gcc_location(), lhs_type_tree, val)); |
e440a328 | 597 | } |
598 | ||
599 | // Convert an expression to a tree. This is implemented by the child | |
600 | // class. Not that it is not in general safe to call this multiple | |
601 | // times for a single expression, but that we don't catch such errors. | |
602 | ||
603 | tree | |
604 | Expression::get_tree(Translate_context* context) | |
605 | { | |
606 | // The child may have marked this expression as having an error. | |
607 | if (this->classification_ == EXPRESSION_ERROR) | |
608 | return error_mark_node; | |
609 | ||
610 | return this->do_get_tree(context); | |
611 | } | |
612 | ||
48c2a53a | 613 | // Return a backend expression for VAL. |
614 | Bexpression* | |
615 | Expression::backend_numeric_constant_expression(Translate_context* context, | |
616 | Numeric_constant* val) | |
e440a328 | 617 | { |
48c2a53a | 618 | Gogo* gogo = context->gogo(); |
619 | Type* type = val->type(); | |
620 | if (type == NULL) | |
621 | return gogo->backend()->error_expression(); | |
e440a328 | 622 | |
48c2a53a | 623 | Btype* btype = type->get_backend(gogo); |
624 | Bexpression* ret; | |
625 | if (type->integer_type() != NULL) | |
e440a328 | 626 | { |
627 | mpz_t ival; | |
48c2a53a | 628 | if (!val->to_int(&ival)) |
629 | { | |
630 | go_assert(saw_errors()); | |
631 | return gogo->backend()->error_expression(); | |
632 | } | |
633 | ret = gogo->backend()->integer_constant_expression(btype, ival); | |
e440a328 | 634 | mpz_clear(ival); |
e440a328 | 635 | } |
48c2a53a | 636 | else if (type->float_type() != NULL) |
e440a328 | 637 | { |
48c2a53a | 638 | mpfr_t fval; |
639 | if (!val->to_float(&fval)) | |
640 | { | |
641 | go_assert(saw_errors()); | |
642 | return gogo->backend()->error_expression(); | |
643 | } | |
644 | ret = gogo->backend()->float_constant_expression(btype, fval); | |
645 | mpfr_clear(fval); | |
e440a328 | 646 | } |
48c2a53a | 647 | else if (type->complex_type() != NULL) |
e440a328 | 648 | { |
48c2a53a | 649 | mpfr_t real; |
650 | mpfr_t imag; | |
651 | if (!val->to_complex(&real, &imag)) | |
652 | { | |
653 | go_assert(saw_errors()); | |
654 | return gogo->backend()->error_expression(); | |
655 | } | |
656 | ret = gogo->backend()->complex_constant_expression(btype, real, imag); | |
657 | mpfr_clear(real); | |
658 | mpfr_clear(imag); | |
e440a328 | 659 | } |
660 | else | |
c3e6f413 | 661 | go_unreachable(); |
e440a328 | 662 | |
48c2a53a | 663 | return ret; |
e440a328 | 664 | } |
665 | ||
666 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
667 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
668 | // If SOFAR is not NULL, it is or'red into the result. The return | |
669 | // value may be NULL if SOFAR is NULL. | |
670 | ||
671 | tree | |
672 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
b13c66cd | 673 | Location loc) |
e440a328 | 674 | { |
675 | tree val_type = TREE_TYPE(val); | |
676 | tree ret = NULL_TREE; | |
677 | ||
678 | if (!TYPE_UNSIGNED(val_type)) | |
679 | { | |
b13c66cd | 680 | ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val, |
e440a328 | 681 | build_int_cst(val_type, 0)); |
682 | if (ret == boolean_false_node) | |
683 | ret = NULL_TREE; | |
684 | } | |
685 | ||
c3068ac0 | 686 | HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type); |
687 | HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type); | |
688 | go_assert(val_type_size != -1 && bound_type_size != -1); | |
689 | if (val_type_size > bound_type_size | |
690 | || (val_type_size == bound_type_size | |
691 | && TYPE_UNSIGNED(val_type) | |
692 | && !TYPE_UNSIGNED(bound_type))) | |
e440a328 | 693 | { |
694 | tree max = TYPE_MAX_VALUE(bound_type); | |
b13c66cd | 695 | tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node, |
696 | val, fold_convert_loc(loc.gcc_location(), | |
697 | val_type, max)); | |
e440a328 | 698 | if (big == boolean_false_node) |
699 | ; | |
700 | else if (ret == NULL_TREE) | |
701 | ret = big; | |
702 | else | |
b13c66cd | 703 | ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
704 | boolean_type_node, ret, big); | |
e440a328 | 705 | } |
706 | ||
707 | if (ret == NULL_TREE) | |
708 | return sofar; | |
709 | else if (sofar == NULL_TREE) | |
710 | return ret; | |
711 | else | |
b13c66cd | 712 | return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node, |
e440a328 | 713 | sofar, ret); |
714 | } | |
715 | ||
d751bb78 | 716 | void |
717 | Expression::dump_expression(Ast_dump_context* ast_dump_context) const | |
718 | { | |
719 | this->do_dump_expression(ast_dump_context); | |
720 | } | |
721 | ||
e440a328 | 722 | // Error expressions. This are used to avoid cascading errors. |
723 | ||
724 | class Error_expression : public Expression | |
725 | { | |
726 | public: | |
b13c66cd | 727 | Error_expression(Location location) |
e440a328 | 728 | : Expression(EXPRESSION_ERROR, location) |
729 | { } | |
730 | ||
731 | protected: | |
732 | bool | |
733 | do_is_constant() const | |
734 | { return true; } | |
735 | ||
736 | bool | |
0c77715b | 737 | do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 738 | { |
0c77715b | 739 | nc->set_unsigned_long(NULL, 0); |
e440a328 | 740 | return true; |
741 | } | |
742 | ||
4f2138d7 | 743 | bool |
e440a328 | 744 | do_discarding_value() |
4f2138d7 | 745 | { return true; } |
e440a328 | 746 | |
747 | Type* | |
748 | do_type() | |
749 | { return Type::make_error_type(); } | |
750 | ||
751 | void | |
752 | do_determine_type(const Type_context*) | |
753 | { } | |
754 | ||
755 | Expression* | |
756 | do_copy() | |
757 | { return this; } | |
758 | ||
759 | bool | |
760 | do_is_addressable() const | |
761 | { return true; } | |
762 | ||
763 | tree | |
764 | do_get_tree(Translate_context*) | |
765 | { return error_mark_node; } | |
d751bb78 | 766 | |
767 | void | |
768 | do_dump_expression(Ast_dump_context*) const; | |
e440a328 | 769 | }; |
770 | ||
d751bb78 | 771 | // Dump the ast representation for an error expression to a dump context. |
772 | ||
773 | void | |
774 | Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
775 | { | |
776 | ast_dump_context->ostream() << "_Error_" ; | |
777 | } | |
778 | ||
e440a328 | 779 | Expression* |
b13c66cd | 780 | Expression::make_error(Location location) |
e440a328 | 781 | { |
782 | return new Error_expression(location); | |
783 | } | |
784 | ||
785 | // An expression which is really a type. This is used during parsing. | |
786 | // It is an error if these survive after lowering. | |
787 | ||
788 | class | |
789 | Type_expression : public Expression | |
790 | { | |
791 | public: | |
b13c66cd | 792 | Type_expression(Type* type, Location location) |
e440a328 | 793 | : Expression(EXPRESSION_TYPE, location), |
794 | type_(type) | |
795 | { } | |
796 | ||
797 | protected: | |
798 | int | |
799 | do_traverse(Traverse* traverse) | |
800 | { return Type::traverse(this->type_, traverse); } | |
801 | ||
802 | Type* | |
803 | do_type() | |
804 | { return this->type_; } | |
805 | ||
806 | void | |
807 | do_determine_type(const Type_context*) | |
808 | { } | |
809 | ||
810 | void | |
811 | do_check_types(Gogo*) | |
812 | { this->report_error(_("invalid use of type")); } | |
813 | ||
814 | Expression* | |
815 | do_copy() | |
816 | { return this; } | |
817 | ||
818 | tree | |
819 | do_get_tree(Translate_context*) | |
c3e6f413 | 820 | { go_unreachable(); } |
e440a328 | 821 | |
d751bb78 | 822 | void do_dump_expression(Ast_dump_context*) const; |
823 | ||
e440a328 | 824 | private: |
825 | // The type which we are representing as an expression. | |
826 | Type* type_; | |
827 | }; | |
828 | ||
d751bb78 | 829 | void |
830 | Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
831 | { | |
832 | ast_dump_context->dump_type(this->type_); | |
833 | } | |
834 | ||
e440a328 | 835 | Expression* |
b13c66cd | 836 | Expression::make_type(Type* type, Location location) |
e440a328 | 837 | { |
838 | return new Type_expression(type, location); | |
839 | } | |
840 | ||
e03bdf36 | 841 | // Class Parser_expression. |
842 | ||
843 | Type* | |
844 | Parser_expression::do_type() | |
845 | { | |
846 | // We should never really ask for the type of a Parser_expression. | |
847 | // However, it can happen, at least when we have an invalid const | |
848 | // whose initializer refers to the const itself. In that case we | |
849 | // may ask for the type when lowering the const itself. | |
c484d925 | 850 | go_assert(saw_errors()); |
e03bdf36 | 851 | return Type::make_error_type(); |
852 | } | |
853 | ||
e440a328 | 854 | // Class Var_expression. |
855 | ||
856 | // Lower a variable expression. Here we just make sure that the | |
857 | // initialization expression of the variable has been lowered. This | |
858 | // ensures that we will be able to determine the type of the variable | |
859 | // if necessary. | |
860 | ||
861 | Expression* | |
ceeb4318 | 862 | Var_expression::do_lower(Gogo* gogo, Named_object* function, |
863 | Statement_inserter* inserter, int) | |
e440a328 | 864 | { |
865 | if (this->variable_->is_variable()) | |
866 | { | |
867 | Variable* var = this->variable_->var_value(); | |
868 | // This is either a local variable or a global variable. A | |
869 | // reference to a variable which is local to an enclosing | |
870 | // function will be a reference to a field in a closure. | |
871 | if (var->is_global()) | |
ceeb4318 | 872 | { |
873 | function = NULL; | |
874 | inserter = NULL; | |
875 | } | |
876 | var->lower_init_expression(gogo, function, inserter); | |
e440a328 | 877 | } |
878 | return this; | |
879 | } | |
880 | ||
e440a328 | 881 | // Return the type of a reference to a variable. |
882 | ||
883 | Type* | |
884 | Var_expression::do_type() | |
885 | { | |
886 | if (this->variable_->is_variable()) | |
887 | return this->variable_->var_value()->type(); | |
888 | else if (this->variable_->is_result_variable()) | |
889 | return this->variable_->result_var_value()->type(); | |
890 | else | |
c3e6f413 | 891 | go_unreachable(); |
e440a328 | 892 | } |
893 | ||
0ab09e06 | 894 | // Determine the type of a reference to a variable. |
895 | ||
896 | void | |
897 | Var_expression::do_determine_type(const Type_context*) | |
898 | { | |
899 | if (this->variable_->is_variable()) | |
900 | this->variable_->var_value()->determine_type(); | |
901 | } | |
902 | ||
e440a328 | 903 | // Something takes the address of this variable. This means that we |
904 | // may want to move the variable onto the heap. | |
905 | ||
906 | void | |
907 | Var_expression::do_address_taken(bool escapes) | |
908 | { | |
909 | if (!escapes) | |
f325319b | 910 | { |
911 | if (this->variable_->is_variable()) | |
912 | this->variable_->var_value()->set_non_escaping_address_taken(); | |
913 | else if (this->variable_->is_result_variable()) | |
914 | this->variable_->result_var_value()->set_non_escaping_address_taken(); | |
915 | else | |
916 | go_unreachable(); | |
917 | } | |
e440a328 | 918 | else |
f325319b | 919 | { |
920 | if (this->variable_->is_variable()) | |
921 | this->variable_->var_value()->set_address_taken(); | |
922 | else if (this->variable_->is_result_variable()) | |
923 | this->variable_->result_var_value()->set_address_taken(); | |
924 | else | |
925 | go_unreachable(); | |
926 | } | |
e440a328 | 927 | } |
928 | ||
929 | // Get the tree for a reference to a variable. | |
930 | ||
931 | tree | |
932 | Var_expression::do_get_tree(Translate_context* context) | |
933 | { | |
fe2f84cf | 934 | Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(), |
935 | context->function()); | |
fe2f84cf | 936 | bool is_in_heap; |
c6777780 | 937 | Location loc = this->location(); |
fe2f84cf | 938 | if (this->variable_->is_variable()) |
939 | is_in_heap = this->variable_->var_value()->is_in_heap(); | |
940 | else if (this->variable_->is_result_variable()) | |
941 | is_in_heap = this->variable_->result_var_value()->is_in_heap(); | |
942 | else | |
c3e6f413 | 943 | go_unreachable(); |
c6777780 | 944 | |
945 | Bexpression* ret = context->backend()->var_expression(bvar, loc); | |
fe2f84cf | 946 | if (is_in_heap) |
c6777780 | 947 | ret = context->backend()->indirect_expression(ret, true, loc); |
948 | return expr_to_tree(ret); | |
e440a328 | 949 | } |
950 | ||
d751bb78 | 951 | // Ast dump for variable expression. |
952 | ||
953 | void | |
954 | Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
955 | { | |
956 | ast_dump_context->ostream() << this->variable_->name() ; | |
957 | } | |
958 | ||
e440a328 | 959 | // Make a reference to a variable in an expression. |
960 | ||
961 | Expression* | |
b13c66cd | 962 | Expression::make_var_reference(Named_object* var, Location location) |
e440a328 | 963 | { |
964 | if (var->is_sink()) | |
965 | return Expression::make_sink(location); | |
966 | ||
967 | // FIXME: Creating a new object for each reference to a variable is | |
968 | // wasteful. | |
969 | return new Var_expression(var, location); | |
970 | } | |
971 | ||
972 | // Class Temporary_reference_expression. | |
973 | ||
974 | // The type. | |
975 | ||
976 | Type* | |
977 | Temporary_reference_expression::do_type() | |
978 | { | |
979 | return this->statement_->type(); | |
980 | } | |
981 | ||
982 | // Called if something takes the address of this temporary variable. | |
983 | // We never have to move temporary variables to the heap, but we do | |
984 | // need to know that they must live in the stack rather than in a | |
985 | // register. | |
986 | ||
987 | void | |
988 | Temporary_reference_expression::do_address_taken(bool) | |
989 | { | |
990 | this->statement_->set_is_address_taken(); | |
991 | } | |
992 | ||
993 | // Get a tree referring to the variable. | |
994 | ||
995 | tree | |
eefc1ed3 | 996 | Temporary_reference_expression::do_get_tree(Translate_context* context) |
e440a328 | 997 | { |
cd440cff | 998 | Gogo* gogo = context->gogo(); |
eefc1ed3 | 999 | Bvariable* bvar = this->statement_->get_backend_variable(context); |
cd440cff | 1000 | Bexpression* ret = gogo->backend()->var_expression(bvar, this->location()); |
eefc1ed3 | 1001 | |
cd440cff | 1002 | // The backend can't always represent the same set of recursive types |
eefc1ed3 | 1003 | // that the Go frontend can. In some cases this means that a |
1004 | // temporary variable won't have the right backend type. Correct | |
1005 | // that here by adding a type cast. We need to use base() to push | |
1006 | // the circularity down one level. | |
cd440cff | 1007 | Type* stype = this->statement_->type(); |
ceeb4318 | 1008 | if (!this->is_lvalue_ |
cd440cff | 1009 | && stype->has_pointer() |
1010 | && stype->deref()->is_void_type()) | |
eefc1ed3 | 1011 | { |
cd440cff | 1012 | Btype* btype = this->type()->base()->get_backend(gogo); |
1013 | ret = gogo->backend()->convert_expression(btype, ret, this->location()); | |
eefc1ed3 | 1014 | } |
cd440cff | 1015 | return expr_to_tree(ret); |
e440a328 | 1016 | } |
1017 | ||
d751bb78 | 1018 | // Ast dump for temporary reference. |
1019 | ||
1020 | void | |
1021 | Temporary_reference_expression::do_dump_expression( | |
1022 | Ast_dump_context* ast_dump_context) const | |
1023 | { | |
1024 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
1025 | } | |
1026 | ||
e440a328 | 1027 | // Make a reference to a temporary variable. |
1028 | ||
ceeb4318 | 1029 | Temporary_reference_expression* |
e440a328 | 1030 | Expression::make_temporary_reference(Temporary_statement* statement, |
b13c66cd | 1031 | Location location) |
e440a328 | 1032 | { |
1033 | return new Temporary_reference_expression(statement, location); | |
1034 | } | |
1035 | ||
e9d3367e | 1036 | // Class Set_and_use_temporary_expression. |
1037 | ||
1038 | // Return the type. | |
1039 | ||
1040 | Type* | |
1041 | Set_and_use_temporary_expression::do_type() | |
1042 | { | |
1043 | return this->statement_->type(); | |
1044 | } | |
1045 | ||
0afbb937 | 1046 | // Determine the type of the expression. |
1047 | ||
1048 | void | |
1049 | Set_and_use_temporary_expression::do_determine_type( | |
1050 | const Type_context* context) | |
1051 | { | |
1052 | this->expr_->determine_type(context); | |
1053 | } | |
1054 | ||
e9d3367e | 1055 | // Take the address. |
1056 | ||
1057 | void | |
1058 | Set_and_use_temporary_expression::do_address_taken(bool) | |
1059 | { | |
1060 | this->statement_->set_is_address_taken(); | |
1061 | } | |
1062 | ||
1063 | // Return the backend representation. | |
1064 | ||
1065 | tree | |
1066 | Set_and_use_temporary_expression::do_get_tree(Translate_context* context) | |
1067 | { | |
1068 | Bvariable* bvar = this->statement_->get_backend_variable(context); | |
1069 | tree var_tree = var_to_tree(bvar); | |
1070 | tree expr_tree = this->expr_->get_tree(context); | |
1071 | if (var_tree == error_mark_node || expr_tree == error_mark_node) | |
1072 | return error_mark_node; | |
1073 | Location loc = this->location(); | |
1074 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree), | |
1075 | build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node, | |
1076 | var_tree, expr_tree), | |
1077 | var_tree); | |
1078 | } | |
1079 | ||
1080 | // Dump. | |
1081 | ||
1082 | void | |
1083 | Set_and_use_temporary_expression::do_dump_expression( | |
1084 | Ast_dump_context* ast_dump_context) const | |
1085 | { | |
1086 | ast_dump_context->ostream() << '('; | |
1087 | ast_dump_context->dump_temp_variable_name(this->statement_); | |
1088 | ast_dump_context->ostream() << " = "; | |
1089 | this->expr_->dump_expression(ast_dump_context); | |
1090 | ast_dump_context->ostream() << ')'; | |
1091 | } | |
1092 | ||
1093 | // Make a set-and-use temporary. | |
1094 | ||
1095 | Set_and_use_temporary_expression* | |
1096 | Expression::make_set_and_use_temporary(Temporary_statement* statement, | |
1097 | Expression* expr, Location location) | |
1098 | { | |
1099 | return new Set_and_use_temporary_expression(statement, expr, location); | |
1100 | } | |
1101 | ||
e440a328 | 1102 | // A sink expression--a use of the blank identifier _. |
1103 | ||
1104 | class Sink_expression : public Expression | |
1105 | { | |
1106 | public: | |
b13c66cd | 1107 | Sink_expression(Location location) |
e440a328 | 1108 | : Expression(EXPRESSION_SINK, location), |
1109 | type_(NULL), var_(NULL_TREE) | |
1110 | { } | |
1111 | ||
1112 | protected: | |
4f2138d7 | 1113 | bool |
e440a328 | 1114 | do_discarding_value() |
4f2138d7 | 1115 | { return true; } |
e440a328 | 1116 | |
1117 | Type* | |
1118 | do_type(); | |
1119 | ||
1120 | void | |
1121 | do_determine_type(const Type_context*); | |
1122 | ||
1123 | Expression* | |
1124 | do_copy() | |
1125 | { return new Sink_expression(this->location()); } | |
1126 | ||
1127 | tree | |
1128 | do_get_tree(Translate_context*); | |
1129 | ||
d751bb78 | 1130 | void |
1131 | do_dump_expression(Ast_dump_context*) const; | |
1132 | ||
e440a328 | 1133 | private: |
1134 | // The type of this sink variable. | |
1135 | Type* type_; | |
1136 | // The temporary variable we generate. | |
1137 | tree var_; | |
1138 | }; | |
1139 | ||
1140 | // Return the type of a sink expression. | |
1141 | ||
1142 | Type* | |
1143 | Sink_expression::do_type() | |
1144 | { | |
1145 | if (this->type_ == NULL) | |
1146 | return Type::make_sink_type(); | |
1147 | return this->type_; | |
1148 | } | |
1149 | ||
1150 | // Determine the type of a sink expression. | |
1151 | ||
1152 | void | |
1153 | Sink_expression::do_determine_type(const Type_context* context) | |
1154 | { | |
1155 | if (context->type != NULL) | |
1156 | this->type_ = context->type; | |
1157 | } | |
1158 | ||
1159 | // Return a temporary variable for a sink expression. This will | |
1160 | // presumably be a write-only variable which the middle-end will drop. | |
1161 | ||
1162 | tree | |
1163 | Sink_expression::do_get_tree(Translate_context* context) | |
1164 | { | |
1165 | if (this->var_ == NULL_TREE) | |
1166 | { | |
c484d925 | 1167 | go_assert(this->type_ != NULL && !this->type_->is_sink_type()); |
9f0e0513 | 1168 | Btype* bt = this->type_->get_backend(context->gogo()); |
1169 | this->var_ = create_tmp_var(type_to_tree(bt), "blank"); | |
e440a328 | 1170 | } |
1171 | return this->var_; | |
1172 | } | |
1173 | ||
d751bb78 | 1174 | // Ast dump for sink expression. |
1175 | ||
1176 | void | |
1177 | Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1178 | { | |
1179 | ast_dump_context->ostream() << "_" ; | |
1180 | } | |
1181 | ||
e440a328 | 1182 | // Make a sink expression. |
1183 | ||
1184 | Expression* | |
b13c66cd | 1185 | Expression::make_sink(Location location) |
e440a328 | 1186 | { |
1187 | return new Sink_expression(location); | |
1188 | } | |
1189 | ||
1190 | // Class Func_expression. | |
1191 | ||
1192 | // FIXME: Can a function expression appear in a constant expression? | |
1193 | // The value is unchanging. Initializing a constant to the address of | |
1194 | // a function seems like it could work, though there might be little | |
1195 | // point to it. | |
1196 | ||
e440a328 | 1197 | // Traversal. |
1198 | ||
1199 | int | |
1200 | Func_expression::do_traverse(Traverse* traverse) | |
1201 | { | |
1202 | return (this->closure_ == NULL | |
1203 | ? TRAVERSE_CONTINUE | |
1204 | : Expression::traverse(&this->closure_, traverse)); | |
1205 | } | |
1206 | ||
1207 | // Return the type of a function expression. | |
1208 | ||
1209 | Type* | |
1210 | Func_expression::do_type() | |
1211 | { | |
1212 | if (this->function_->is_function()) | |
1213 | return this->function_->func_value()->type(); | |
1214 | else if (this->function_->is_function_declaration()) | |
1215 | return this->function_->func_declaration_value()->type(); | |
1216 | else | |
c3e6f413 | 1217 | go_unreachable(); |
e440a328 | 1218 | } |
1219 | ||
8381eda7 | 1220 | // Get the tree for the code of a function expression. |
e440a328 | 1221 | |
1222 | tree | |
8381eda7 | 1223 | Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc) |
e440a328 | 1224 | { |
1225 | Function_type* fntype; | |
8381eda7 | 1226 | if (no->is_function()) |
1227 | fntype = no->func_value()->type(); | |
1228 | else if (no->is_function_declaration()) | |
1229 | fntype = no->func_declaration_value()->type(); | |
e440a328 | 1230 | else |
c3e6f413 | 1231 | go_unreachable(); |
e440a328 | 1232 | |
1233 | // Builtin functions are handled specially by Call_expression. We | |
1234 | // can't take their address. | |
1235 | if (fntype->is_builtin()) | |
1236 | { | |
8381eda7 | 1237 | error_at(loc, |
cb0e02f3 | 1238 | "invalid use of special builtin function %qs; must be called", |
8381eda7 | 1239 | no->message_name().c_str()); |
e440a328 | 1240 | return error_mark_node; |
1241 | } | |
1242 | ||
9d6f3721 | 1243 | tree id = no->get_id(gogo); |
1244 | if (id == error_mark_node) | |
1245 | return error_mark_node; | |
1246 | ||
e440a328 | 1247 | tree fndecl; |
1248 | if (no->is_function()) | |
1249 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1250 | else if (no->is_function_declaration()) | |
1251 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1252 | else | |
c3e6f413 | 1253 | go_unreachable(); |
e440a328 | 1254 | |
9d6f3721 | 1255 | if (fndecl == error_mark_node) |
1256 | return error_mark_node; | |
1257 | ||
8381eda7 | 1258 | return build_fold_addr_expr_loc(loc.gcc_location(), fndecl); |
e440a328 | 1259 | } |
1260 | ||
1261 | // Get the tree for a function expression. This is used when we take | |
8381eda7 | 1262 | // the address of a function rather than simply calling it. A func |
1263 | // value is represented as a pointer to a block of memory. The first | |
1264 | // word of that memory is a pointer to the function code. The | |
1265 | // remaining parts of that memory are the addresses of variables that | |
1266 | // the function closes over. | |
e440a328 | 1267 | |
1268 | tree | |
1269 | Func_expression::do_get_tree(Translate_context* context) | |
1270 | { | |
8381eda7 | 1271 | // If there is no closure, just use the function descriptor. |
2010c17a | 1272 | if (this->closure_ == NULL) |
8381eda7 | 1273 | { |
1274 | Gogo* gogo = context->gogo(); | |
1275 | Named_object* no = this->function_; | |
1276 | Expression* descriptor; | |
1277 | if (no->is_function()) | |
1278 | descriptor = no->func_value()->descriptor(gogo, no); | |
1279 | else if (no->is_function_declaration()) | |
1280 | { | |
1281 | if (no->func_declaration_value()->type()->is_builtin()) | |
1282 | { | |
1283 | error_at(this->location(), | |
1284 | ("invalid use of special builtin function %qs; " | |
1285 | "must be called"), | |
1286 | no->message_name().c_str()); | |
1287 | return error_mark_node; | |
1288 | } | |
1289 | descriptor = no->func_declaration_value()->descriptor(gogo, no); | |
1290 | } | |
1291 | else | |
1292 | go_unreachable(); | |
2010c17a | 1293 | |
8381eda7 | 1294 | tree dtree = descriptor->get_tree(context); |
1295 | if (dtree == error_mark_node) | |
1296 | return error_mark_node; | |
1297 | return build_fold_addr_expr_loc(this->location().gcc_location(), dtree); | |
1298 | } | |
e440a328 | 1299 | |
8381eda7 | 1300 | go_assert(this->function_->func_value()->enclosing() != NULL); |
e440a328 | 1301 | |
8381eda7 | 1302 | // If there is a closure, then the closure is itself the function |
1303 | // expression. It is a pointer to a struct whose first field points | |
1304 | // to the function code and whose remaining fields are the addresses | |
1305 | // of the closed-over variables. | |
1306 | return this->closure_->get_tree(context); | |
e440a328 | 1307 | } |
1308 | ||
d751bb78 | 1309 | // Ast dump for function. |
1310 | ||
1311 | void | |
1312 | Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1313 | { | |
8b1c301d | 1314 | ast_dump_context->ostream() << this->function_->name(); |
1315 | if (this->closure_ != NULL) | |
1316 | { | |
1317 | ast_dump_context->ostream() << " {closure = "; | |
1318 | this->closure_->dump_expression(ast_dump_context); | |
1319 | ast_dump_context->ostream() << "}"; | |
1320 | } | |
d751bb78 | 1321 | } |
1322 | ||
e440a328 | 1323 | // Make a reference to a function in an expression. |
1324 | ||
1325 | Expression* | |
1326 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
b13c66cd | 1327 | Location location) |
e440a328 | 1328 | { |
1329 | return new Func_expression(function, closure, location); | |
1330 | } | |
1331 | ||
c6837989 | 1332 | // Class Func_descriptor_expression. |
8381eda7 | 1333 | |
c6837989 | 1334 | // Constructor. |
8381eda7 | 1335 | |
c6837989 | 1336 | Func_descriptor_expression::Func_descriptor_expression(Named_object* fn) |
1337 | : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()), | |
f8bdf81a | 1338 | fn_(fn), dvar_(NULL) |
c6837989 | 1339 | { |
1340 | go_assert(!fn->is_function() || !fn->func_value()->needs_closure()); | |
1341 | } | |
8381eda7 | 1342 | |
c6837989 | 1343 | // Traversal. |
8381eda7 | 1344 | |
c6837989 | 1345 | int |
1346 | Func_descriptor_expression::do_traverse(Traverse*) | |
1347 | { | |
1348 | return TRAVERSE_CONTINUE; | |
1349 | } | |
8381eda7 | 1350 | |
1351 | // All function descriptors have the same type. | |
1352 | ||
1353 | Type* Func_descriptor_expression::descriptor_type; | |
1354 | ||
1355 | void | |
1356 | Func_descriptor_expression::make_func_descriptor_type() | |
1357 | { | |
1358 | if (Func_descriptor_expression::descriptor_type != NULL) | |
1359 | return; | |
1360 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
1361 | Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type); | |
1362 | Func_descriptor_expression::descriptor_type = | |
1363 | Type::make_builtin_named_type("functionDescriptor", struct_type); | |
1364 | } | |
1365 | ||
1366 | Type* | |
1367 | Func_descriptor_expression::do_type() | |
1368 | { | |
1369 | Func_descriptor_expression::make_func_descriptor_type(); | |
1370 | return Func_descriptor_expression::descriptor_type; | |
1371 | } | |
1372 | ||
1373 | // The tree for a function descriptor. | |
1374 | ||
1375 | tree | |
1376 | Func_descriptor_expression::do_get_tree(Translate_context* context) | |
1377 | { | |
1378 | if (this->dvar_ != NULL) | |
1379 | return var_to_tree(this->dvar_); | |
1380 | ||
1381 | Gogo* gogo = context->gogo(); | |
1382 | Named_object* no = this->fn_; | |
1383 | Location loc = no->location(); | |
1384 | ||
1385 | std::string var_name; | |
1386 | if (no->package() == NULL) | |
1387 | var_name = gogo->pkgpath_symbol(); | |
1388 | else | |
1389 | var_name = no->package()->pkgpath_symbol(); | |
1390 | var_name.push_back('.'); | |
1391 | var_name.append(Gogo::unpack_hidden_name(no->name())); | |
1392 | var_name.append("$descriptor"); | |
1393 | ||
1394 | Btype* btype = this->type()->get_backend(gogo); | |
1395 | ||
1396 | Bvariable* bvar; | |
1397 | if (no->package() != NULL | |
1398 | || Linemap::is_predeclared_location(no->location())) | |
f8bdf81a | 1399 | bvar = context->backend()->immutable_struct_reference(var_name, btype, |
1400 | loc); | |
8381eda7 | 1401 | else |
1402 | { | |
1403 | Location bloc = Linemap::predeclared_location(); | |
1404 | bool is_hidden = ((no->is_function() | |
1405 | && no->func_value()->enclosing() != NULL) | |
1406 | || Gogo::is_thunk(no)); | |
1407 | bvar = context->backend()->immutable_struct(var_name, is_hidden, false, | |
1408 | btype, bloc); | |
1409 | Expression_list* vals = new Expression_list(); | |
f8bdf81a | 1410 | vals->push_back(Expression::make_func_code_reference(this->fn_, bloc)); |
8381eda7 | 1411 | Expression* init = |
1412 | Expression::make_struct_composite_literal(this->type(), vals, bloc); | |
1413 | Translate_context bcontext(gogo, NULL, NULL, NULL); | |
1414 | bcontext.set_is_const(); | |
1415 | Bexpression* binit = tree_to_expr(init->get_tree(&bcontext)); | |
1416 | context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden, | |
1417 | false, btype, bloc, binit); | |
1418 | } | |
1419 | ||
1420 | this->dvar_ = bvar; | |
1421 | return var_to_tree(bvar); | |
1422 | } | |
1423 | ||
c6837989 | 1424 | // Print a function descriptor expression. |
1425 | ||
1426 | void | |
1427 | Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const | |
1428 | { | |
1429 | context->ostream() << "[descriptor " << this->fn_->name() << "]"; | |
1430 | } | |
1431 | ||
8381eda7 | 1432 | // Make a function descriptor expression. |
1433 | ||
c6837989 | 1434 | Func_descriptor_expression* |
1435 | Expression::make_func_descriptor(Named_object* fn) | |
8381eda7 | 1436 | { |
c6837989 | 1437 | return new Func_descriptor_expression(fn); |
8381eda7 | 1438 | } |
1439 | ||
1440 | // Make the function descriptor type, so that it can be converted. | |
1441 | ||
1442 | void | |
1443 | Expression::make_func_descriptor_type() | |
1444 | { | |
1445 | Func_descriptor_expression::make_func_descriptor_type(); | |
1446 | } | |
1447 | ||
1448 | // A reference to just the code of a function. | |
1449 | ||
1450 | class Func_code_reference_expression : public Expression | |
1451 | { | |
1452 | public: | |
1453 | Func_code_reference_expression(Named_object* function, Location location) | |
1454 | : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location), | |
1455 | function_(function) | |
1456 | { } | |
1457 | ||
1458 | protected: | |
1459 | int | |
1460 | do_traverse(Traverse*) | |
1461 | { return TRAVERSE_CONTINUE; } | |
1462 | ||
1463 | Type* | |
1464 | do_type() | |
1465 | { return Type::make_pointer_type(Type::make_void_type()); } | |
1466 | ||
1467 | void | |
1468 | do_determine_type(const Type_context*) | |
1469 | { } | |
1470 | ||
1471 | Expression* | |
1472 | do_copy() | |
1473 | { | |
1474 | return Expression::make_func_code_reference(this->function_, | |
1475 | this->location()); | |
1476 | } | |
1477 | ||
1478 | tree | |
1479 | do_get_tree(Translate_context*); | |
1480 | ||
1481 | void | |
1482 | do_dump_expression(Ast_dump_context* context) const | |
1483 | { context->ostream() << "[raw " << this->function_->name() << "]" ; } | |
1484 | ||
1485 | private: | |
1486 | // The function. | |
1487 | Named_object* function_; | |
1488 | }; | |
1489 | ||
1490 | // Get the tree for a reference to function code. | |
1491 | ||
1492 | tree | |
1493 | Func_code_reference_expression::do_get_tree(Translate_context* context) | |
1494 | { | |
1495 | return Func_expression::get_code_pointer(context->gogo(), this->function_, | |
1496 | this->location()); | |
1497 | } | |
1498 | ||
1499 | // Make a reference to the code of a function. | |
1500 | ||
1501 | Expression* | |
1502 | Expression::make_func_code_reference(Named_object* function, Location location) | |
1503 | { | |
1504 | return new Func_code_reference_expression(function, location); | |
1505 | } | |
1506 | ||
e440a328 | 1507 | // Class Unknown_expression. |
1508 | ||
1509 | // Return the name of an unknown expression. | |
1510 | ||
1511 | const std::string& | |
1512 | Unknown_expression::name() const | |
1513 | { | |
1514 | return this->named_object_->name(); | |
1515 | } | |
1516 | ||
1517 | // Lower a reference to an unknown name. | |
1518 | ||
1519 | Expression* | |
ceeb4318 | 1520 | Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 1521 | { |
b13c66cd | 1522 | Location location = this->location(); |
e440a328 | 1523 | Named_object* no = this->named_object_; |
deded542 | 1524 | Named_object* real; |
1525 | if (!no->is_unknown()) | |
1526 | real = no; | |
1527 | else | |
e440a328 | 1528 | { |
deded542 | 1529 | real = no->unknown_value()->real_named_object(); |
1530 | if (real == NULL) | |
1531 | { | |
1532 | if (this->is_composite_literal_key_) | |
1533 | return this; | |
acf8e158 | 1534 | if (!this->no_error_message_) |
1535 | error_at(location, "reference to undefined name %qs", | |
1536 | this->named_object_->message_name().c_str()); | |
deded542 | 1537 | return Expression::make_error(location); |
1538 | } | |
e440a328 | 1539 | } |
1540 | switch (real->classification()) | |
1541 | { | |
1542 | case Named_object::NAMED_OBJECT_CONST: | |
1543 | return Expression::make_const_reference(real, location); | |
1544 | case Named_object::NAMED_OBJECT_TYPE: | |
1545 | return Expression::make_type(real->type_value(), location); | |
1546 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1547 | if (this->is_composite_literal_key_) | |
1548 | return this; | |
acf8e158 | 1549 | if (!this->no_error_message_) |
1550 | error_at(location, "reference to undefined type %qs", | |
1551 | real->message_name().c_str()); | |
e440a328 | 1552 | return Expression::make_error(location); |
1553 | case Named_object::NAMED_OBJECT_VAR: | |
7d834090 | 1554 | real->var_value()->set_is_used(); |
e440a328 | 1555 | return Expression::make_var_reference(real, location); |
1556 | case Named_object::NAMED_OBJECT_FUNC: | |
1557 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1558 | return Expression::make_func_reference(real, NULL, location); | |
1559 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1560 | if (this->is_composite_literal_key_) | |
1561 | return this; | |
acf8e158 | 1562 | if (!this->no_error_message_) |
1563 | error_at(location, "unexpected reference to package"); | |
e440a328 | 1564 | return Expression::make_error(location); |
1565 | default: | |
c3e6f413 | 1566 | go_unreachable(); |
e440a328 | 1567 | } |
1568 | } | |
1569 | ||
d751bb78 | 1570 | // Dump the ast representation for an unknown expression to a dump context. |
1571 | ||
1572 | void | |
1573 | Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1574 | { | |
1575 | ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name() | |
1576 | << ")"; | |
d751bb78 | 1577 | } |
1578 | ||
e440a328 | 1579 | // Make a reference to an unknown name. |
1580 | ||
acf8e158 | 1581 | Unknown_expression* |
b13c66cd | 1582 | Expression::make_unknown_reference(Named_object* no, Location location) |
e440a328 | 1583 | { |
e440a328 | 1584 | return new Unknown_expression(no, location); |
1585 | } | |
1586 | ||
1587 | // A boolean expression. | |
1588 | ||
1589 | class Boolean_expression : public Expression | |
1590 | { | |
1591 | public: | |
b13c66cd | 1592 | Boolean_expression(bool val, Location location) |
e440a328 | 1593 | : Expression(EXPRESSION_BOOLEAN, location), |
1594 | val_(val), type_(NULL) | |
1595 | { } | |
1596 | ||
1597 | static Expression* | |
1598 | do_import(Import*); | |
1599 | ||
1600 | protected: | |
1601 | bool | |
1602 | do_is_constant() const | |
1603 | { return true; } | |
1604 | ||
1605 | Type* | |
1606 | do_type(); | |
1607 | ||
1608 | void | |
1609 | do_determine_type(const Type_context*); | |
1610 | ||
1611 | Expression* | |
1612 | do_copy() | |
1613 | { return this; } | |
1614 | ||
1615 | tree | |
1616 | do_get_tree(Translate_context*) | |
1617 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1618 | ||
1619 | void | |
1620 | do_export(Export* exp) const | |
1621 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1622 | ||
d751bb78 | 1623 | void |
1624 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1625 | { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); } | |
1626 | ||
e440a328 | 1627 | private: |
1628 | // The constant. | |
1629 | bool val_; | |
1630 | // The type as determined by context. | |
1631 | Type* type_; | |
1632 | }; | |
1633 | ||
1634 | // Get the type. | |
1635 | ||
1636 | Type* | |
1637 | Boolean_expression::do_type() | |
1638 | { | |
1639 | if (this->type_ == NULL) | |
1640 | this->type_ = Type::make_boolean_type(); | |
1641 | return this->type_; | |
1642 | } | |
1643 | ||
1644 | // Set the type from the context. | |
1645 | ||
1646 | void | |
1647 | Boolean_expression::do_determine_type(const Type_context* context) | |
1648 | { | |
1649 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1650 | ; | |
1651 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1652 | this->type_ = context->type; | |
1653 | else if (!context->may_be_abstract) | |
1654 | this->type_ = Type::lookup_bool_type(); | |
1655 | } | |
1656 | ||
1657 | // Import a boolean constant. | |
1658 | ||
1659 | Expression* | |
1660 | Boolean_expression::do_import(Import* imp) | |
1661 | { | |
1662 | if (imp->peek_char() == 't') | |
1663 | { | |
1664 | imp->require_c_string("true"); | |
1665 | return Expression::make_boolean(true, imp->location()); | |
1666 | } | |
1667 | else | |
1668 | { | |
1669 | imp->require_c_string("false"); | |
1670 | return Expression::make_boolean(false, imp->location()); | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | // Make a boolean expression. | |
1675 | ||
1676 | Expression* | |
b13c66cd | 1677 | Expression::make_boolean(bool val, Location location) |
e440a328 | 1678 | { |
1679 | return new Boolean_expression(val, location); | |
1680 | } | |
1681 | ||
1682 | // Class String_expression. | |
1683 | ||
1684 | // Get the type. | |
1685 | ||
1686 | Type* | |
1687 | String_expression::do_type() | |
1688 | { | |
1689 | if (this->type_ == NULL) | |
1690 | this->type_ = Type::make_string_type(); | |
1691 | return this->type_; | |
1692 | } | |
1693 | ||
1694 | // Set the type from the context. | |
1695 | ||
1696 | void | |
1697 | String_expression::do_determine_type(const Type_context* context) | |
1698 | { | |
1699 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1700 | ; | |
1701 | else if (context->type != NULL && context->type->is_string_type()) | |
1702 | this->type_ = context->type; | |
1703 | else if (!context->may_be_abstract) | |
1704 | this->type_ = Type::lookup_string_type(); | |
1705 | } | |
1706 | ||
1707 | // Build a string constant. | |
1708 | ||
1709 | tree | |
1710 | String_expression::do_get_tree(Translate_context* context) | |
1711 | { | |
1712 | return context->gogo()->go_string_constant_tree(this->val_); | |
1713 | } | |
1714 | ||
8b1c301d | 1715 | // Write string literal to string dump. |
e440a328 | 1716 | |
1717 | void | |
8b1c301d | 1718 | String_expression::export_string(String_dump* exp, |
1719 | const String_expression* str) | |
e440a328 | 1720 | { |
1721 | std::string s; | |
8b1c301d | 1722 | s.reserve(str->val_.length() * 4 + 2); |
e440a328 | 1723 | s += '"'; |
8b1c301d | 1724 | for (std::string::const_iterator p = str->val_.begin(); |
1725 | p != str->val_.end(); | |
e440a328 | 1726 | ++p) |
1727 | { | |
1728 | if (*p == '\\' || *p == '"') | |
1729 | { | |
1730 | s += '\\'; | |
1731 | s += *p; | |
1732 | } | |
1733 | else if (*p >= 0x20 && *p < 0x7f) | |
1734 | s += *p; | |
1735 | else if (*p == '\n') | |
1736 | s += "\\n"; | |
1737 | else if (*p == '\t') | |
1738 | s += "\\t"; | |
1739 | else | |
1740 | { | |
1741 | s += "\\x"; | |
1742 | unsigned char c = *p; | |
1743 | unsigned int dig = c >> 4; | |
1744 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1745 | dig = c & 0xf; | |
1746 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1747 | } | |
1748 | } | |
1749 | s += '"'; | |
1750 | exp->write_string(s); | |
1751 | } | |
1752 | ||
8b1c301d | 1753 | // Export a string expression. |
1754 | ||
1755 | void | |
1756 | String_expression::do_export(Export* exp) const | |
1757 | { | |
1758 | String_expression::export_string(exp, this); | |
1759 | } | |
1760 | ||
e440a328 | 1761 | // Import a string expression. |
1762 | ||
1763 | Expression* | |
1764 | String_expression::do_import(Import* imp) | |
1765 | { | |
1766 | imp->require_c_string("\""); | |
1767 | std::string val; | |
1768 | while (true) | |
1769 | { | |
1770 | int c = imp->get_char(); | |
1771 | if (c == '"' || c == -1) | |
1772 | break; | |
1773 | if (c != '\\') | |
1774 | val += static_cast<char>(c); | |
1775 | else | |
1776 | { | |
1777 | c = imp->get_char(); | |
1778 | if (c == '\\' || c == '"') | |
1779 | val += static_cast<char>(c); | |
1780 | else if (c == 'n') | |
1781 | val += '\n'; | |
1782 | else if (c == 't') | |
1783 | val += '\t'; | |
1784 | else if (c == 'x') | |
1785 | { | |
1786 | c = imp->get_char(); | |
1787 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1788 | c = imp->get_char(); | |
1789 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1790 | char v = (vh << 4) | vl; | |
1791 | val += v; | |
1792 | } | |
1793 | else | |
1794 | { | |
1795 | error_at(imp->location(), "bad string constant"); | |
1796 | return Expression::make_error(imp->location()); | |
1797 | } | |
1798 | } | |
1799 | } | |
1800 | return Expression::make_string(val, imp->location()); | |
1801 | } | |
1802 | ||
d751bb78 | 1803 | // Ast dump for string expression. |
1804 | ||
1805 | void | |
1806 | String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
1807 | { | |
8b1c301d | 1808 | String_expression::export_string(ast_dump_context, this); |
d751bb78 | 1809 | } |
1810 | ||
e440a328 | 1811 | // Make a string expression. |
1812 | ||
1813 | Expression* | |
b13c66cd | 1814 | Expression::make_string(const std::string& val, Location location) |
e440a328 | 1815 | { |
1816 | return new String_expression(val, location); | |
1817 | } | |
1818 | ||
1819 | // Make an integer expression. | |
1820 | ||
1821 | class Integer_expression : public Expression | |
1822 | { | |
1823 | public: | |
5d4b8566 | 1824 | Integer_expression(const mpz_t* val, Type* type, bool is_character_constant, |
1825 | Location location) | |
e440a328 | 1826 | : Expression(EXPRESSION_INTEGER, location), |
5d4b8566 | 1827 | type_(type), is_character_constant_(is_character_constant) |
e440a328 | 1828 | { mpz_init_set(this->val_, *val); } |
1829 | ||
1830 | static Expression* | |
1831 | do_import(Import*); | |
1832 | ||
8b1c301d | 1833 | // Write VAL to string dump. |
e440a328 | 1834 | static void |
8b1c301d | 1835 | export_integer(String_dump* exp, const mpz_t val); |
e440a328 | 1836 | |
d751bb78 | 1837 | // Write VAL to dump context. |
1838 | static void | |
1839 | dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val); | |
1840 | ||
e440a328 | 1841 | protected: |
1842 | bool | |
1843 | do_is_constant() const | |
1844 | { return true; } | |
1845 | ||
1846 | bool | |
0c77715b | 1847 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 1848 | |
1849 | Type* | |
1850 | do_type(); | |
1851 | ||
1852 | void | |
1853 | do_determine_type(const Type_context* context); | |
1854 | ||
1855 | void | |
1856 | do_check_types(Gogo*); | |
1857 | ||
1858 | tree | |
1859 | do_get_tree(Translate_context*); | |
1860 | ||
1861 | Expression* | |
1862 | do_copy() | |
5d4b8566 | 1863 | { |
1864 | if (this->is_character_constant_) | |
1865 | return Expression::make_character(&this->val_, this->type_, | |
1866 | this->location()); | |
1867 | else | |
1868 | return Expression::make_integer(&this->val_, this->type_, | |
1869 | this->location()); | |
1870 | } | |
e440a328 | 1871 | |
1872 | void | |
1873 | do_export(Export*) const; | |
1874 | ||
d751bb78 | 1875 | void |
1876 | do_dump_expression(Ast_dump_context*) const; | |
1877 | ||
e440a328 | 1878 | private: |
1879 | // The integer value. | |
1880 | mpz_t val_; | |
1881 | // The type so far. | |
1882 | Type* type_; | |
5d4b8566 | 1883 | // Whether this is a character constant. |
1884 | bool is_character_constant_; | |
e440a328 | 1885 | }; |
1886 | ||
0c77715b | 1887 | // Return a numeric constant for this expression. We have to mark |
1888 | // this as a character when appropriate. | |
e440a328 | 1889 | |
1890 | bool | |
0c77715b | 1891 | Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 1892 | { |
0c77715b | 1893 | if (this->is_character_constant_) |
1894 | nc->set_rune(this->type_, this->val_); | |
1895 | else | |
1896 | nc->set_int(this->type_, this->val_); | |
e440a328 | 1897 | return true; |
1898 | } | |
1899 | ||
1900 | // Return the current type. If we haven't set the type yet, we return | |
1901 | // an abstract integer type. | |
1902 | ||
1903 | Type* | |
1904 | Integer_expression::do_type() | |
1905 | { | |
1906 | if (this->type_ == NULL) | |
5d4b8566 | 1907 | { |
1908 | if (this->is_character_constant_) | |
1909 | this->type_ = Type::make_abstract_character_type(); | |
1910 | else | |
1911 | this->type_ = Type::make_abstract_integer_type(); | |
1912 | } | |
e440a328 | 1913 | return this->type_; |
1914 | } | |
1915 | ||
1916 | // Set the type of the integer value. Here we may switch from an | |
1917 | // abstract type to a real type. | |
1918 | ||
1919 | void | |
1920 | Integer_expression::do_determine_type(const Type_context* context) | |
1921 | { | |
1922 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1923 | ; | |
0c77715b | 1924 | else if (context->type != NULL && context->type->is_numeric_type()) |
e440a328 | 1925 | this->type_ = context->type; |
1926 | else if (!context->may_be_abstract) | |
5d4b8566 | 1927 | { |
1928 | if (this->is_character_constant_) | |
1929 | this->type_ = Type::lookup_integer_type("int32"); | |
1930 | else | |
1931 | this->type_ = Type::lookup_integer_type("int"); | |
1932 | } | |
e440a328 | 1933 | } |
1934 | ||
e440a328 | 1935 | // Check the type of an integer constant. |
1936 | ||
1937 | void | |
1938 | Integer_expression::do_check_types(Gogo*) | |
1939 | { | |
0c77715b | 1940 | Type* type = this->type_; |
1941 | if (type == NULL) | |
e440a328 | 1942 | return; |
0c77715b | 1943 | Numeric_constant nc; |
1944 | if (this->is_character_constant_) | |
1945 | nc.set_rune(NULL, this->val_); | |
1946 | else | |
1947 | nc.set_int(NULL, this->val_); | |
1948 | if (!nc.set_type(type, true, this->location())) | |
e440a328 | 1949 | this->set_is_error(); |
1950 | } | |
1951 | ||
1952 | // Get a tree for an integer constant. | |
1953 | ||
1954 | tree | |
1955 | Integer_expression::do_get_tree(Translate_context* context) | |
1956 | { | |
48c2a53a | 1957 | Type* resolved_type = NULL; |
e440a328 | 1958 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 1959 | resolved_type = this->type_; |
e440a328 | 1960 | else if (this->type_ != NULL && this->type_->float_type() != NULL) |
1961 | { | |
1962 | // We are converting to an abstract floating point type. | |
48c2a53a | 1963 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 1964 | } |
1965 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1966 | { | |
1967 | // We are converting to an abstract complex type. | |
48c2a53a | 1968 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 1969 | } |
1970 | else | |
1971 | { | |
1972 | // If we still have an abstract type here, then this is being | |
1973 | // used in a constant expression which didn't get reduced for | |
1974 | // some reason. Use a type which will fit the value. We use <, | |
1975 | // not <=, because we need an extra bit for the sign bit. | |
1976 | int bits = mpz_sizeinbase(this->val_, 2); | |
1b1f2abf | 1977 | Type* int_type = Type::lookup_integer_type("int"); |
1978 | if (bits < int_type->integer_type()->bits()) | |
48c2a53a | 1979 | resolved_type = int_type; |
e440a328 | 1980 | else if (bits < 64) |
48c2a53a | 1981 | resolved_type = Type::lookup_integer_type("int64"); |
e440a328 | 1982 | else |
48c2a53a | 1983 | { |
1984 | if (!saw_errors()) | |
1985 | error_at(this->location(), | |
1986 | "unknown type for large integer constant"); | |
1987 | Bexpression* ret = context->gogo()->backend()->error_expression(); | |
1988 | return expr_to_tree(ret); | |
1989 | } | |
e440a328 | 1990 | } |
48c2a53a | 1991 | Numeric_constant nc; |
1992 | nc.set_int(resolved_type, this->val_); | |
1993 | Bexpression* ret = | |
1994 | Expression::backend_numeric_constant_expression(context, &nc); | |
1995 | return expr_to_tree(ret); | |
e440a328 | 1996 | } |
1997 | ||
1998 | // Write VAL to export data. | |
1999 | ||
2000 | void | |
8b1c301d | 2001 | Integer_expression::export_integer(String_dump* exp, const mpz_t val) |
e440a328 | 2002 | { |
2003 | char* s = mpz_get_str(NULL, 10, val); | |
2004 | exp->write_c_string(s); | |
2005 | free(s); | |
2006 | } | |
2007 | ||
2008 | // Export an integer in a constant expression. | |
2009 | ||
2010 | void | |
2011 | Integer_expression::do_export(Export* exp) const | |
2012 | { | |
2013 | Integer_expression::export_integer(exp, this->val_); | |
5d4b8566 | 2014 | if (this->is_character_constant_) |
2015 | exp->write_c_string("'"); | |
e440a328 | 2016 | // A trailing space lets us reliably identify the end of the number. |
2017 | exp->write_c_string(" "); | |
2018 | } | |
2019 | ||
2020 | // Import an integer, floating point, or complex value. This handles | |
2021 | // all these types because they all start with digits. | |
2022 | ||
2023 | Expression* | |
2024 | Integer_expression::do_import(Import* imp) | |
2025 | { | |
2026 | std::string num = imp->read_identifier(); | |
2027 | imp->require_c_string(" "); | |
2028 | if (!num.empty() && num[num.length() - 1] == 'i') | |
2029 | { | |
2030 | mpfr_t real; | |
2031 | size_t plus_pos = num.find('+', 1); | |
2032 | size_t minus_pos = num.find('-', 1); | |
2033 | size_t pos; | |
2034 | if (plus_pos == std::string::npos) | |
2035 | pos = minus_pos; | |
2036 | else if (minus_pos == std::string::npos) | |
2037 | pos = plus_pos; | |
2038 | else | |
2039 | { | |
2040 | error_at(imp->location(), "bad number in import data: %qs", | |
2041 | num.c_str()); | |
2042 | return Expression::make_error(imp->location()); | |
2043 | } | |
2044 | if (pos == std::string::npos) | |
2045 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2046 | else | |
2047 | { | |
2048 | std::string real_str = num.substr(0, pos); | |
2049 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
2050 | { | |
2051 | error_at(imp->location(), "bad number in import data: %qs", | |
2052 | real_str.c_str()); | |
2053 | return Expression::make_error(imp->location()); | |
2054 | } | |
2055 | } | |
2056 | ||
2057 | std::string imag_str; | |
2058 | if (pos == std::string::npos) | |
2059 | imag_str = num; | |
2060 | else | |
2061 | imag_str = num.substr(pos); | |
2062 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
2063 | mpfr_t imag; | |
2064 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
2065 | { | |
2066 | error_at(imp->location(), "bad number in import data: %qs", | |
2067 | imag_str.c_str()); | |
2068 | return Expression::make_error(imp->location()); | |
2069 | } | |
2070 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
2071 | imp->location()); | |
2072 | mpfr_clear(real); | |
2073 | mpfr_clear(imag); | |
2074 | return ret; | |
2075 | } | |
2076 | else if (num.find('.') == std::string::npos | |
2077 | && num.find('E') == std::string::npos) | |
2078 | { | |
5d4b8566 | 2079 | bool is_character_constant = (!num.empty() |
2080 | && num[num.length() - 1] == '\''); | |
2081 | if (is_character_constant) | |
2082 | num = num.substr(0, num.length() - 1); | |
e440a328 | 2083 | mpz_t val; |
2084 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
2085 | { | |
2086 | error_at(imp->location(), "bad number in import data: %qs", | |
2087 | num.c_str()); | |
2088 | return Expression::make_error(imp->location()); | |
2089 | } | |
5d4b8566 | 2090 | Expression* ret; |
2091 | if (is_character_constant) | |
2092 | ret = Expression::make_character(&val, NULL, imp->location()); | |
2093 | else | |
2094 | ret = Expression::make_integer(&val, NULL, imp->location()); | |
e440a328 | 2095 | mpz_clear(val); |
2096 | return ret; | |
2097 | } | |
2098 | else | |
2099 | { | |
2100 | mpfr_t val; | |
2101 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
2102 | { | |
2103 | error_at(imp->location(), "bad number in import data: %qs", | |
2104 | num.c_str()); | |
2105 | return Expression::make_error(imp->location()); | |
2106 | } | |
2107 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
2108 | mpfr_clear(val); | |
2109 | return ret; | |
2110 | } | |
2111 | } | |
d751bb78 | 2112 | // Ast dump for integer expression. |
2113 | ||
2114 | void | |
2115 | Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2116 | { | |
5d4b8566 | 2117 | if (this->is_character_constant_) |
2118 | ast_dump_context->ostream() << '\''; | |
8b1c301d | 2119 | Integer_expression::export_integer(ast_dump_context, this->val_); |
5d4b8566 | 2120 | if (this->is_character_constant_) |
2121 | ast_dump_context->ostream() << '\''; | |
d751bb78 | 2122 | } |
2123 | ||
e440a328 | 2124 | // Build a new integer value. |
2125 | ||
2126 | Expression* | |
5d4b8566 | 2127 | Expression::make_integer(const mpz_t* val, Type* type, Location location) |
2128 | { | |
2129 | return new Integer_expression(val, type, false, location); | |
2130 | } | |
2131 | ||
2132 | // Build a new character constant value. | |
2133 | ||
2134 | Expression* | |
2135 | Expression::make_character(const mpz_t* val, Type* type, Location location) | |
e440a328 | 2136 | { |
5d4b8566 | 2137 | return new Integer_expression(val, type, true, location); |
e440a328 | 2138 | } |
2139 | ||
2140 | // Floats. | |
2141 | ||
2142 | class Float_expression : public Expression | |
2143 | { | |
2144 | public: | |
b13c66cd | 2145 | Float_expression(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2146 | : Expression(EXPRESSION_FLOAT, location), |
2147 | type_(type) | |
2148 | { | |
2149 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
2150 | } | |
2151 | ||
e440a328 | 2152 | // Write VAL to export data. |
2153 | static void | |
8b1c301d | 2154 | export_float(String_dump* exp, const mpfr_t val); |
2155 | ||
d751bb78 | 2156 | // Write VAL to dump file. |
2157 | static void | |
2158 | dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val); | |
e440a328 | 2159 | |
2160 | protected: | |
2161 | bool | |
2162 | do_is_constant() const | |
2163 | { return true; } | |
2164 | ||
2165 | bool | |
0c77715b | 2166 | do_numeric_constant_value(Numeric_constant* nc) const |
2167 | { | |
2168 | nc->set_float(this->type_, this->val_); | |
2169 | return true; | |
2170 | } | |
e440a328 | 2171 | |
2172 | Type* | |
2173 | do_type(); | |
2174 | ||
2175 | void | |
2176 | do_determine_type(const Type_context*); | |
2177 | ||
2178 | void | |
2179 | do_check_types(Gogo*); | |
2180 | ||
2181 | Expression* | |
2182 | do_copy() | |
2183 | { return Expression::make_float(&this->val_, this->type_, | |
2184 | this->location()); } | |
2185 | ||
2186 | tree | |
2187 | do_get_tree(Translate_context*); | |
2188 | ||
2189 | void | |
2190 | do_export(Export*) const; | |
2191 | ||
d751bb78 | 2192 | void |
2193 | do_dump_expression(Ast_dump_context*) const; | |
2194 | ||
e440a328 | 2195 | private: |
2196 | // The floating point value. | |
2197 | mpfr_t val_; | |
2198 | // The type so far. | |
2199 | Type* type_; | |
2200 | }; | |
2201 | ||
e440a328 | 2202 | // Return the current type. If we haven't set the type yet, we return |
2203 | // an abstract float type. | |
2204 | ||
2205 | Type* | |
2206 | Float_expression::do_type() | |
2207 | { | |
2208 | if (this->type_ == NULL) | |
2209 | this->type_ = Type::make_abstract_float_type(); | |
2210 | return this->type_; | |
2211 | } | |
2212 | ||
2213 | // Set the type of the float value. Here we may switch from an | |
2214 | // abstract type to a real type. | |
2215 | ||
2216 | void | |
2217 | Float_expression::do_determine_type(const Type_context* context) | |
2218 | { | |
2219 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2220 | ; | |
2221 | else if (context->type != NULL | |
2222 | && (context->type->integer_type() != NULL | |
2223 | || context->type->float_type() != NULL | |
2224 | || context->type->complex_type() != NULL)) | |
2225 | this->type_ = context->type; | |
2226 | else if (!context->may_be_abstract) | |
48080209 | 2227 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 2228 | } |
2229 | ||
e440a328 | 2230 | // Check the type of a float value. |
2231 | ||
2232 | void | |
2233 | Float_expression::do_check_types(Gogo*) | |
2234 | { | |
0c77715b | 2235 | Type* type = this->type_; |
2236 | if (type == NULL) | |
e440a328 | 2237 | return; |
0c77715b | 2238 | Numeric_constant nc; |
2239 | nc.set_float(NULL, this->val_); | |
2240 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2241 | this->set_is_error(); |
e440a328 | 2242 | } |
2243 | ||
2244 | // Get a tree for a float constant. | |
2245 | ||
2246 | tree | |
2247 | Float_expression::do_get_tree(Translate_context* context) | |
2248 | { | |
48c2a53a | 2249 | Type* resolved_type; |
e440a328 | 2250 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2251 | resolved_type = this->type_; |
e440a328 | 2252 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) |
2253 | { | |
2254 | // We have an abstract integer type. We just hope for the best. | |
48c2a53a | 2255 | resolved_type = Type::lookup_integer_type("int"); |
2256 | } | |
2257 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
2258 | { | |
2259 | // We are converting to an abstract complex type. | |
2260 | resolved_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 2261 | } |
2262 | else | |
2263 | { | |
2264 | // If we still have an abstract type here, then this is being | |
2265 | // used in a constant expression which didn't get reduced. We | |
2266 | // just use float64 and hope for the best. | |
48c2a53a | 2267 | resolved_type = Type::lookup_float_type("float64"); |
e440a328 | 2268 | } |
48c2a53a | 2269 | |
2270 | Numeric_constant nc; | |
2271 | nc.set_float(resolved_type, this->val_); | |
2272 | Bexpression* ret = | |
2273 | Expression::backend_numeric_constant_expression(context, &nc); | |
2274 | return expr_to_tree(ret); | |
e440a328 | 2275 | } |
2276 | ||
8b1c301d | 2277 | // Write a floating point number to a string dump. |
e440a328 | 2278 | |
2279 | void | |
8b1c301d | 2280 | Float_expression::export_float(String_dump *exp, const mpfr_t val) |
e440a328 | 2281 | { |
2282 | mp_exp_t exponent; | |
2283 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2284 | if (*s == '-') | |
2285 | exp->write_c_string("-"); | |
2286 | exp->write_c_string("0."); | |
2287 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2288 | mpfr_free_str(s); | |
2289 | char buf[30]; | |
2290 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2291 | exp->write_c_string(buf); | |
2292 | } | |
2293 | ||
2294 | // Export a floating point number in a constant expression. | |
2295 | ||
2296 | void | |
2297 | Float_expression::do_export(Export* exp) const | |
2298 | { | |
2299 | Float_expression::export_float(exp, this->val_); | |
2300 | // A trailing space lets us reliably identify the end of the number. | |
2301 | exp->write_c_string(" "); | |
2302 | } | |
2303 | ||
d751bb78 | 2304 | // Dump a floating point number to the dump file. |
2305 | ||
2306 | void | |
2307 | Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2308 | { | |
8b1c301d | 2309 | Float_expression::export_float(ast_dump_context, this->val_); |
d751bb78 | 2310 | } |
2311 | ||
e440a328 | 2312 | // Make a float expression. |
2313 | ||
2314 | Expression* | |
b13c66cd | 2315 | Expression::make_float(const mpfr_t* val, Type* type, Location location) |
e440a328 | 2316 | { |
2317 | return new Float_expression(val, type, location); | |
2318 | } | |
2319 | ||
2320 | // Complex numbers. | |
2321 | ||
2322 | class Complex_expression : public Expression | |
2323 | { | |
2324 | public: | |
2325 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2326 | Location location) |
e440a328 | 2327 | : Expression(EXPRESSION_COMPLEX, location), |
2328 | type_(type) | |
2329 | { | |
2330 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2331 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2332 | } | |
2333 | ||
8b1c301d | 2334 | // Write REAL/IMAG to string dump. |
e440a328 | 2335 | static void |
8b1c301d | 2336 | export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val); |
e440a328 | 2337 | |
d751bb78 | 2338 | // Write REAL/IMAG to dump context. |
2339 | static void | |
2340 | dump_complex(Ast_dump_context* ast_dump_context, | |
2341 | const mpfr_t real, const mpfr_t val); | |
2342 | ||
e440a328 | 2343 | protected: |
2344 | bool | |
2345 | do_is_constant() const | |
2346 | { return true; } | |
2347 | ||
2348 | bool | |
0c77715b | 2349 | do_numeric_constant_value(Numeric_constant* nc) const |
2350 | { | |
2351 | nc->set_complex(this->type_, this->real_, this->imag_); | |
2352 | return true; | |
2353 | } | |
e440a328 | 2354 | |
2355 | Type* | |
2356 | do_type(); | |
2357 | ||
2358 | void | |
2359 | do_determine_type(const Type_context*); | |
2360 | ||
2361 | void | |
2362 | do_check_types(Gogo*); | |
2363 | ||
2364 | Expression* | |
2365 | do_copy() | |
2366 | { | |
2367 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2368 | this->location()); | |
2369 | } | |
2370 | ||
2371 | tree | |
2372 | do_get_tree(Translate_context*); | |
2373 | ||
2374 | void | |
2375 | do_export(Export*) const; | |
2376 | ||
d751bb78 | 2377 | void |
2378 | do_dump_expression(Ast_dump_context*) const; | |
2379 | ||
e440a328 | 2380 | private: |
2381 | // The real part. | |
2382 | mpfr_t real_; | |
2383 | // The imaginary part; | |
2384 | mpfr_t imag_; | |
2385 | // The type if known. | |
2386 | Type* type_; | |
2387 | }; | |
2388 | ||
e440a328 | 2389 | // Return the current type. If we haven't set the type yet, we return |
2390 | // an abstract complex type. | |
2391 | ||
2392 | Type* | |
2393 | Complex_expression::do_type() | |
2394 | { | |
2395 | if (this->type_ == NULL) | |
2396 | this->type_ = Type::make_abstract_complex_type(); | |
2397 | return this->type_; | |
2398 | } | |
2399 | ||
2400 | // Set the type of the complex value. Here we may switch from an | |
2401 | // abstract type to a real type. | |
2402 | ||
2403 | void | |
2404 | Complex_expression::do_determine_type(const Type_context* context) | |
2405 | { | |
2406 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2407 | ; | |
2408 | else if (context->type != NULL | |
2409 | && context->type->complex_type() != NULL) | |
2410 | this->type_ = context->type; | |
2411 | else if (!context->may_be_abstract) | |
48080209 | 2412 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2413 | } |
2414 | ||
e440a328 | 2415 | // Check the type of a complex value. |
2416 | ||
2417 | void | |
2418 | Complex_expression::do_check_types(Gogo*) | |
2419 | { | |
0c77715b | 2420 | Type* type = this->type_; |
2421 | if (type == NULL) | |
e440a328 | 2422 | return; |
0c77715b | 2423 | Numeric_constant nc; |
2424 | nc.set_complex(NULL, this->real_, this->imag_); | |
2425 | if (!nc.set_type(this->type_, true, this->location())) | |
e440a328 | 2426 | this->set_is_error(); |
2427 | } | |
2428 | ||
2429 | // Get a tree for a complex constant. | |
2430 | ||
2431 | tree | |
2432 | Complex_expression::do_get_tree(Translate_context* context) | |
2433 | { | |
48c2a53a | 2434 | Type* resolved_type; |
e440a328 | 2435 | if (this->type_ != NULL && !this->type_->is_abstract()) |
48c2a53a | 2436 | resolved_type = this->type_; |
2437 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2438 | { | |
2439 | // We are converting to an abstract integer type. | |
2440 | resolved_type = Type::lookup_integer_type("int"); | |
2441 | } | |
2442 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
2443 | { | |
2444 | // We are converting to an abstract float type. | |
2445 | resolved_type = Type::lookup_float_type("float64"); | |
2446 | } | |
e440a328 | 2447 | else |
2448 | { | |
2449 | // If we still have an abstract type here, this this is being | |
2450 | // used in a constant expression which didn't get reduced. We | |
2451 | // just use complex128 and hope for the best. | |
48c2a53a | 2452 | resolved_type = Type::lookup_complex_type("complex128"); |
e440a328 | 2453 | } |
48c2a53a | 2454 | |
2455 | Numeric_constant nc; | |
2456 | nc.set_complex(resolved_type, this->real_, this->imag_); | |
2457 | Bexpression* ret = | |
2458 | Expression::backend_numeric_constant_expression(context, &nc); | |
2459 | return expr_to_tree(ret); | |
e440a328 | 2460 | } |
2461 | ||
2462 | // Write REAL/IMAG to export data. | |
2463 | ||
2464 | void | |
8b1c301d | 2465 | Complex_expression::export_complex(String_dump* exp, const mpfr_t real, |
e440a328 | 2466 | const mpfr_t imag) |
2467 | { | |
2468 | if (!mpfr_zero_p(real)) | |
2469 | { | |
2470 | Float_expression::export_float(exp, real); | |
2471 | if (mpfr_sgn(imag) > 0) | |
2472 | exp->write_c_string("+"); | |
2473 | } | |
2474 | Float_expression::export_float(exp, imag); | |
2475 | exp->write_c_string("i"); | |
2476 | } | |
2477 | ||
2478 | // Export a complex number in a constant expression. | |
2479 | ||
2480 | void | |
2481 | Complex_expression::do_export(Export* exp) const | |
2482 | { | |
2483 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2484 | // A trailing space lets us reliably identify the end of the number. | |
2485 | exp->write_c_string(" "); | |
2486 | } | |
2487 | ||
d751bb78 | 2488 | // Dump a complex expression to the dump file. |
2489 | ||
2490 | void | |
2491 | Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2492 | { | |
8b1c301d | 2493 | Complex_expression::export_complex(ast_dump_context, |
d751bb78 | 2494 | this->real_, |
2495 | this->imag_); | |
2496 | } | |
2497 | ||
e440a328 | 2498 | // Make a complex expression. |
2499 | ||
2500 | Expression* | |
2501 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
b13c66cd | 2502 | Location location) |
e440a328 | 2503 | { |
2504 | return new Complex_expression(real, imag, type, location); | |
2505 | } | |
2506 | ||
d5b605df | 2507 | // Find a named object in an expression. |
2508 | ||
2509 | class Find_named_object : public Traverse | |
2510 | { | |
2511 | public: | |
2512 | Find_named_object(Named_object* no) | |
2513 | : Traverse(traverse_expressions), | |
2514 | no_(no), found_(false) | |
2515 | { } | |
2516 | ||
2517 | // Whether we found the object. | |
2518 | bool | |
2519 | found() const | |
2520 | { return this->found_; } | |
2521 | ||
2522 | protected: | |
2523 | int | |
2524 | expression(Expression**); | |
2525 | ||
2526 | private: | |
2527 | // The object we are looking for. | |
2528 | Named_object* no_; | |
2529 | // Whether we found it. | |
2530 | bool found_; | |
2531 | }; | |
2532 | ||
e440a328 | 2533 | // A reference to a const in an expression. |
2534 | ||
2535 | class Const_expression : public Expression | |
2536 | { | |
2537 | public: | |
b13c66cd | 2538 | Const_expression(Named_object* constant, Location location) |
e440a328 | 2539 | : Expression(EXPRESSION_CONST_REFERENCE, location), |
13e818f5 | 2540 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2541 | { } |
2542 | ||
d5b605df | 2543 | Named_object* |
2544 | named_object() | |
2545 | { return this->constant_; } | |
2546 | ||
a7f064d5 | 2547 | // Check that the initializer does not refer to the constant itself. |
2548 | void | |
2549 | check_for_init_loop(); | |
2550 | ||
e440a328 | 2551 | protected: |
ba4aedd4 | 2552 | int |
2553 | do_traverse(Traverse*); | |
2554 | ||
e440a328 | 2555 | Expression* |
ceeb4318 | 2556 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 2557 | |
2558 | bool | |
2559 | do_is_constant() const | |
2560 | { return true; } | |
2561 | ||
2562 | bool | |
0c77715b | 2563 | do_numeric_constant_value(Numeric_constant* nc) const; |
e440a328 | 2564 | |
2565 | bool | |
af6b489a | 2566 | do_string_constant_value(std::string* val) const; |
e440a328 | 2567 | |
2568 | Type* | |
2569 | do_type(); | |
2570 | ||
2571 | // The type of a const is set by the declaration, not the use. | |
2572 | void | |
2573 | do_determine_type(const Type_context*); | |
2574 | ||
2575 | void | |
2576 | do_check_types(Gogo*); | |
2577 | ||
2578 | Expression* | |
2579 | do_copy() | |
2580 | { return this; } | |
2581 | ||
2582 | tree | |
2583 | do_get_tree(Translate_context* context); | |
2584 | ||
2585 | // When exporting a reference to a const as part of a const | |
2586 | // expression, we export the value. We ignore the fact that it has | |
2587 | // a name. | |
2588 | void | |
2589 | do_export(Export* exp) const | |
2590 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2591 | ||
d751bb78 | 2592 | void |
2593 | do_dump_expression(Ast_dump_context*) const; | |
2594 | ||
e440a328 | 2595 | private: |
2596 | // The constant. | |
2597 | Named_object* constant_; | |
2598 | // The type of this reference. This is used if the constant has an | |
2599 | // abstract type. | |
2600 | Type* type_; | |
13e818f5 | 2601 | // Used to prevent infinite recursion when a constant incorrectly |
2602 | // refers to itself. | |
2603 | mutable bool seen_; | |
e440a328 | 2604 | }; |
2605 | ||
ba4aedd4 | 2606 | // Traversal. |
2607 | ||
2608 | int | |
2609 | Const_expression::do_traverse(Traverse* traverse) | |
2610 | { | |
2611 | if (this->type_ != NULL) | |
2612 | return Type::traverse(this->type_, traverse); | |
2613 | return TRAVERSE_CONTINUE; | |
2614 | } | |
2615 | ||
e440a328 | 2616 | // Lower a constant expression. This is where we convert the |
2617 | // predeclared constant iota into an integer value. | |
2618 | ||
2619 | Expression* | |
ceeb4318 | 2620 | Const_expression::do_lower(Gogo* gogo, Named_object*, |
2621 | Statement_inserter*, int iota_value) | |
e440a328 | 2622 | { |
2623 | if (this->constant_->const_value()->expr()->classification() | |
2624 | == EXPRESSION_IOTA) | |
2625 | { | |
2626 | if (iota_value == -1) | |
2627 | { | |
2628 | error_at(this->location(), | |
2629 | "iota is only defined in const declarations"); | |
2630 | iota_value = 0; | |
2631 | } | |
2632 | mpz_t val; | |
2633 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2634 | Expression* ret = Expression::make_integer(&val, NULL, | |
2635 | this->location()); | |
2636 | mpz_clear(val); | |
2637 | return ret; | |
2638 | } | |
2639 | ||
2640 | // Make sure that the constant itself has been lowered. | |
2641 | gogo->lower_constant(this->constant_); | |
2642 | ||
2643 | return this; | |
2644 | } | |
2645 | ||
0c77715b | 2646 | // Return a numeric constant value. |
e440a328 | 2647 | |
2648 | bool | |
0c77715b | 2649 | Const_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 2650 | { |
13e818f5 | 2651 | if (this->seen_) |
2652 | return false; | |
2653 | ||
e440a328 | 2654 | Expression* e = this->constant_->const_value()->expr(); |
0c77715b | 2655 | |
13e818f5 | 2656 | this->seen_ = true; |
2657 | ||
0c77715b | 2658 | bool r = e->numeric_constant_value(nc); |
e440a328 | 2659 | |
13e818f5 | 2660 | this->seen_ = false; |
2661 | ||
e440a328 | 2662 | Type* ctype; |
2663 | if (this->type_ != NULL) | |
2664 | ctype = this->type_; | |
2665 | else | |
2666 | ctype = this->constant_->const_value()->type(); | |
e440a328 | 2667 | if (r && ctype != NULL) |
2668 | { | |
0c77715b | 2669 | if (!nc->set_type(ctype, false, this->location())) |
e440a328 | 2670 | return false; |
e440a328 | 2671 | } |
e440a328 | 2672 | |
e440a328 | 2673 | return r; |
2674 | } | |
2675 | ||
af6b489a | 2676 | bool |
2677 | Const_expression::do_string_constant_value(std::string* val) const | |
2678 | { | |
2679 | if (this->seen_) | |
2680 | return false; | |
2681 | ||
2682 | Expression* e = this->constant_->const_value()->expr(); | |
2683 | ||
2684 | this->seen_ = true; | |
2685 | bool ok = e->string_constant_value(val); | |
2686 | this->seen_ = false; | |
2687 | ||
2688 | return ok; | |
2689 | } | |
2690 | ||
e440a328 | 2691 | // Return the type of the const reference. |
2692 | ||
2693 | Type* | |
2694 | Const_expression::do_type() | |
2695 | { | |
2696 | if (this->type_ != NULL) | |
2697 | return this->type_; | |
13e818f5 | 2698 | |
2f78f012 | 2699 | Named_constant* nc = this->constant_->const_value(); |
2700 | ||
2701 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2702 | { |
2703 | this->report_error(_("constant refers to itself")); | |
2704 | this->type_ = Type::make_error_type(); | |
2705 | return this->type_; | |
2706 | } | |
2707 | ||
2708 | this->seen_ = true; | |
2709 | ||
e440a328 | 2710 | Type* ret = nc->type(); |
13e818f5 | 2711 | |
e440a328 | 2712 | if (ret != NULL) |
13e818f5 | 2713 | { |
2714 | this->seen_ = false; | |
2715 | return ret; | |
2716 | } | |
2717 | ||
e440a328 | 2718 | // During parsing, a named constant may have a NULL type, but we |
2719 | // must not return a NULL type here. | |
13e818f5 | 2720 | ret = nc->expr()->type(); |
2721 | ||
2722 | this->seen_ = false; | |
2723 | ||
2724 | return ret; | |
e440a328 | 2725 | } |
2726 | ||
2727 | // Set the type of the const reference. | |
2728 | ||
2729 | void | |
2730 | Const_expression::do_determine_type(const Type_context* context) | |
2731 | { | |
2732 | Type* ctype = this->constant_->const_value()->type(); | |
2733 | Type* cetype = (ctype != NULL | |
2734 | ? ctype | |
2735 | : this->constant_->const_value()->expr()->type()); | |
2736 | if (ctype != NULL && !ctype->is_abstract()) | |
2737 | ; | |
2738 | else if (context->type != NULL | |
0c77715b | 2739 | && context->type->is_numeric_type() |
2740 | && cetype->is_numeric_type()) | |
e440a328 | 2741 | this->type_ = context->type; |
2742 | else if (context->type != NULL | |
2743 | && context->type->is_string_type() | |
2744 | && cetype->is_string_type()) | |
2745 | this->type_ = context->type; | |
2746 | else if (context->type != NULL | |
2747 | && context->type->is_boolean_type() | |
2748 | && cetype->is_boolean_type()) | |
2749 | this->type_ = context->type; | |
2750 | else if (!context->may_be_abstract) | |
2751 | { | |
2752 | if (cetype->is_abstract()) | |
2753 | cetype = cetype->make_non_abstract_type(); | |
2754 | this->type_ = cetype; | |
2755 | } | |
2756 | } | |
2757 | ||
a7f064d5 | 2758 | // Check for a loop in which the initializer of a constant refers to |
2759 | // the constant itself. | |
e440a328 | 2760 | |
2761 | void | |
a7f064d5 | 2762 | Const_expression::check_for_init_loop() |
e440a328 | 2763 | { |
5c13bd80 | 2764 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2765 | return; |
2766 | ||
a7f064d5 | 2767 | if (this->seen_) |
2768 | { | |
2769 | this->report_error(_("constant refers to itself")); | |
2770 | this->type_ = Type::make_error_type(); | |
2771 | return; | |
2772 | } | |
2773 | ||
d5b605df | 2774 | Expression* init = this->constant_->const_value()->expr(); |
2775 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2776 | |
2777 | this->seen_ = true; | |
d5b605df | 2778 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2779 | this->seen_ = false; |
2780 | ||
d5b605df | 2781 | if (find_named_object.found()) |
2782 | { | |
5c13bd80 | 2783 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2784 | { |
2785 | this->report_error(_("constant refers to itself")); | |
2786 | this->type_ = Type::make_error_type(); | |
2787 | } | |
d5b605df | 2788 | return; |
2789 | } | |
a7f064d5 | 2790 | } |
2791 | ||
2792 | // Check types of a const reference. | |
2793 | ||
2794 | void | |
2795 | Const_expression::do_check_types(Gogo*) | |
2796 | { | |
5c13bd80 | 2797 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2798 | return; |
2799 | ||
2800 | this->check_for_init_loop(); | |
d5b605df | 2801 | |
0c77715b | 2802 | // Check that numeric constant fits in type. |
2803 | if (this->type_ != NULL && this->type_->is_numeric_type()) | |
e440a328 | 2804 | { |
0c77715b | 2805 | Numeric_constant nc; |
2806 | if (this->constant_->const_value()->expr()->numeric_constant_value(&nc)) | |
e440a328 | 2807 | { |
0c77715b | 2808 | if (!nc.set_type(this->type_, true, this->location())) |
2809 | this->set_is_error(); | |
e440a328 | 2810 | } |
e440a328 | 2811 | } |
2812 | } | |
2813 | ||
2814 | // Return a tree for the const reference. | |
2815 | ||
2816 | tree | |
2817 | Const_expression::do_get_tree(Translate_context* context) | |
2818 | { | |
2819 | Gogo* gogo = context->gogo(); | |
2820 | tree type_tree; | |
2821 | if (this->type_ == NULL) | |
2822 | type_tree = NULL_TREE; | |
2823 | else | |
2824 | { | |
9f0e0513 | 2825 | type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 2826 | if (type_tree == error_mark_node) |
2827 | return error_mark_node; | |
2828 | } | |
2829 | ||
2830 | // If the type has been set for this expression, but the underlying | |
2831 | // object is an abstract int or float, we try to get the abstract | |
2832 | // value. Otherwise we may lose something in the conversion. | |
2833 | if (this->type_ != NULL | |
0c77715b | 2834 | && this->type_->is_numeric_type() |
a68492b4 | 2835 | && (this->constant_->const_value()->type() == NULL |
2836 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2837 | { |
2838 | Expression* expr = this->constant_->const_value()->expr(); | |
0c77715b | 2839 | Numeric_constant nc; |
2840 | if (expr->numeric_constant_value(&nc) | |
2841 | && nc.set_type(this->type_, false, this->location())) | |
e440a328 | 2842 | { |
0c77715b | 2843 | Expression* e = nc.expression(this->location()); |
2844 | return e->get_tree(context); | |
e440a328 | 2845 | } |
e440a328 | 2846 | } |
2847 | ||
2848 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2849 | if (this->type_ == NULL | |
2850 | || const_tree == error_mark_node | |
2851 | || TREE_TYPE(const_tree) == error_mark_node) | |
2852 | return const_tree; | |
2853 | ||
2854 | tree ret; | |
2855 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2856 | ret = fold_convert(type_tree, const_tree); | |
2857 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2858 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2859 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2860 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2861 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2862 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2863 | else | |
c3e6f413 | 2864 | go_unreachable(); |
e440a328 | 2865 | return ret; |
2866 | } | |
2867 | ||
d751bb78 | 2868 | // Dump ast representation for constant expression. |
2869 | ||
2870 | void | |
2871 | Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2872 | { | |
2873 | ast_dump_context->ostream() << this->constant_->name(); | |
2874 | } | |
2875 | ||
e440a328 | 2876 | // Make a reference to a constant in an expression. |
2877 | ||
2878 | Expression* | |
2879 | Expression::make_const_reference(Named_object* constant, | |
b13c66cd | 2880 | Location location) |
e440a328 | 2881 | { |
2882 | return new Const_expression(constant, location); | |
2883 | } | |
2884 | ||
d5b605df | 2885 | // Find a named object in an expression. |
2886 | ||
2887 | int | |
2888 | Find_named_object::expression(Expression** pexpr) | |
2889 | { | |
2890 | switch ((*pexpr)->classification()) | |
2891 | { | |
2892 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2893 | { |
2894 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2895 | if (ce->named_object() == this->no_) | |
2896 | break; | |
2897 | ||
2898 | // We need to check a constant initializer explicitly, as | |
2899 | // loops here will not be caught by the loop checking for | |
2900 | // variable initializers. | |
2901 | ce->check_for_init_loop(); | |
2902 | ||
2903 | return TRAVERSE_CONTINUE; | |
2904 | } | |
2905 | ||
d5b605df | 2906 | case Expression::EXPRESSION_VAR_REFERENCE: |
2907 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2908 | break; | |
2909 | return TRAVERSE_CONTINUE; | |
2910 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2911 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2912 | break; | |
2913 | return TRAVERSE_CONTINUE; | |
2914 | default: | |
2915 | return TRAVERSE_CONTINUE; | |
2916 | } | |
2917 | this->found_ = true; | |
2918 | return TRAVERSE_EXIT; | |
2919 | } | |
2920 | ||
e440a328 | 2921 | // The nil value. |
2922 | ||
2923 | class Nil_expression : public Expression | |
2924 | { | |
2925 | public: | |
b13c66cd | 2926 | Nil_expression(Location location) |
e440a328 | 2927 | : Expression(EXPRESSION_NIL, location) |
2928 | { } | |
2929 | ||
2930 | static Expression* | |
2931 | do_import(Import*); | |
2932 | ||
2933 | protected: | |
2934 | bool | |
2935 | do_is_constant() const | |
2936 | { return true; } | |
2937 | ||
2938 | Type* | |
2939 | do_type() | |
2940 | { return Type::make_nil_type(); } | |
2941 | ||
2942 | void | |
2943 | do_determine_type(const Type_context*) | |
2944 | { } | |
2945 | ||
2946 | Expression* | |
2947 | do_copy() | |
2948 | { return this; } | |
2949 | ||
2950 | tree | |
2951 | do_get_tree(Translate_context*) | |
2952 | { return null_pointer_node; } | |
2953 | ||
2954 | void | |
2955 | do_export(Export* exp) const | |
2956 | { exp->write_c_string("nil"); } | |
d751bb78 | 2957 | |
2958 | void | |
2959 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
2960 | { ast_dump_context->ostream() << "nil"; } | |
e440a328 | 2961 | }; |
2962 | ||
2963 | // Import a nil expression. | |
2964 | ||
2965 | Expression* | |
2966 | Nil_expression::do_import(Import* imp) | |
2967 | { | |
2968 | imp->require_c_string("nil"); | |
2969 | return Expression::make_nil(imp->location()); | |
2970 | } | |
2971 | ||
2972 | // Make a nil expression. | |
2973 | ||
2974 | Expression* | |
b13c66cd | 2975 | Expression::make_nil(Location location) |
e440a328 | 2976 | { |
2977 | return new Nil_expression(location); | |
2978 | } | |
2979 | ||
2980 | // The value of the predeclared constant iota. This is little more | |
2981 | // than a marker. This will be lowered to an integer in | |
2982 | // Const_expression::do_lower, which is where we know the value that | |
2983 | // it should have. | |
2984 | ||
2985 | class Iota_expression : public Parser_expression | |
2986 | { | |
2987 | public: | |
b13c66cd | 2988 | Iota_expression(Location location) |
e440a328 | 2989 | : Parser_expression(EXPRESSION_IOTA, location) |
2990 | { } | |
2991 | ||
2992 | protected: | |
2993 | Expression* | |
ceeb4318 | 2994 | do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
c3e6f413 | 2995 | { go_unreachable(); } |
e440a328 | 2996 | |
2997 | // There should only ever be one of these. | |
2998 | Expression* | |
2999 | do_copy() | |
c3e6f413 | 3000 | { go_unreachable(); } |
d751bb78 | 3001 | |
3002 | void | |
3003 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
3004 | { ast_dump_context->ostream() << "iota"; } | |
e440a328 | 3005 | }; |
3006 | ||
3007 | // Make an iota expression. This is only called for one case: the | |
3008 | // value of the predeclared constant iota. | |
3009 | ||
3010 | Expression* | |
3011 | Expression::make_iota() | |
3012 | { | |
b13c66cd | 3013 | static Iota_expression iota_expression(Linemap::unknown_location()); |
e440a328 | 3014 | return &iota_expression; |
3015 | } | |
3016 | ||
3017 | // A type conversion expression. | |
3018 | ||
3019 | class Type_conversion_expression : public Expression | |
3020 | { | |
3021 | public: | |
3022 | Type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 3023 | Location location) |
e440a328 | 3024 | : Expression(EXPRESSION_CONVERSION, location), |
3025 | type_(type), expr_(expr), may_convert_function_types_(false) | |
3026 | { } | |
3027 | ||
3028 | // Return the type to which we are converting. | |
3029 | Type* | |
3030 | type() const | |
3031 | { return this->type_; } | |
3032 | ||
3033 | // Return the expression which we are converting. | |
3034 | Expression* | |
3035 | expr() const | |
3036 | { return this->expr_; } | |
3037 | ||
3038 | // Permit converting from one function type to another. This is | |
3039 | // used internally for method expressions. | |
3040 | void | |
3041 | set_may_convert_function_types() | |
3042 | { | |
3043 | this->may_convert_function_types_ = true; | |
3044 | } | |
3045 | ||
3046 | // Import a type conversion expression. | |
3047 | static Expression* | |
3048 | do_import(Import*); | |
3049 | ||
3050 | protected: | |
3051 | int | |
3052 | do_traverse(Traverse* traverse); | |
3053 | ||
3054 | Expression* | |
ceeb4318 | 3055 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 3056 | |
3057 | bool | |
3058 | do_is_constant() const | |
3059 | { return this->expr_->is_constant(); } | |
3060 | ||
3061 | bool | |
0c77715b | 3062 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 3063 | |
3064 | bool | |
3065 | do_string_constant_value(std::string*) const; | |
3066 | ||
3067 | Type* | |
3068 | do_type() | |
3069 | { return this->type_; } | |
3070 | ||
3071 | void | |
3072 | do_determine_type(const Type_context*) | |
3073 | { | |
3074 | Type_context subcontext(this->type_, false); | |
3075 | this->expr_->determine_type(&subcontext); | |
3076 | } | |
3077 | ||
3078 | void | |
3079 | do_check_types(Gogo*); | |
3080 | ||
3081 | Expression* | |
3082 | do_copy() | |
3083 | { | |
3084 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
3085 | this->location()); | |
3086 | } | |
3087 | ||
3088 | tree | |
3089 | do_get_tree(Translate_context* context); | |
3090 | ||
3091 | void | |
3092 | do_export(Export*) const; | |
3093 | ||
d751bb78 | 3094 | void |
3095 | do_dump_expression(Ast_dump_context*) const; | |
3096 | ||
e440a328 | 3097 | private: |
3098 | // The type to convert to. | |
3099 | Type* type_; | |
3100 | // The expression to convert. | |
3101 | Expression* expr_; | |
3102 | // True if this is permitted to convert function types. This is | |
3103 | // used internally for method expressions. | |
3104 | bool may_convert_function_types_; | |
3105 | }; | |
3106 | ||
3107 | // Traversal. | |
3108 | ||
3109 | int | |
3110 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3111 | { | |
3112 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3113 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3114 | return TRAVERSE_EXIT; | |
3115 | return TRAVERSE_CONTINUE; | |
3116 | } | |
3117 | ||
3118 | // Convert to a constant at lowering time. | |
3119 | ||
3120 | Expression* | |
ceeb4318 | 3121 | Type_conversion_expression::do_lower(Gogo*, Named_object*, |
3122 | Statement_inserter*, int) | |
e440a328 | 3123 | { |
3124 | Type* type = this->type_; | |
3125 | Expression* val = this->expr_; | |
b13c66cd | 3126 | Location location = this->location(); |
e440a328 | 3127 | |
0c77715b | 3128 | if (type->is_numeric_type()) |
e440a328 | 3129 | { |
0c77715b | 3130 | Numeric_constant nc; |
3131 | if (val->numeric_constant_value(&nc)) | |
e440a328 | 3132 | { |
0c77715b | 3133 | if (!nc.set_type(type, true, location)) |
3134 | return Expression::make_error(location); | |
3135 | return nc.expression(location); | |
e440a328 | 3136 | } |
e440a328 | 3137 | } |
3138 | ||
55072f2b | 3139 | if (type->is_slice_type()) |
e440a328 | 3140 | { |
3141 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
60963afd | 3142 | bool is_byte = (element_type->integer_type() != NULL |
3143 | && element_type->integer_type()->is_byte()); | |
3144 | bool is_rune = (element_type->integer_type() != NULL | |
3145 | && element_type->integer_type()->is_rune()); | |
3146 | if (is_byte || is_rune) | |
e440a328 | 3147 | { |
3148 | std::string s; | |
3149 | if (val->string_constant_value(&s)) | |
3150 | { | |
3151 | Expression_list* vals = new Expression_list(); | |
3152 | if (is_byte) | |
3153 | { | |
3154 | for (std::string::const_iterator p = s.begin(); | |
3155 | p != s.end(); | |
3156 | p++) | |
3157 | { | |
3158 | mpz_t val; | |
3159 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
3160 | Expression* v = Expression::make_integer(&val, | |
3161 | element_type, | |
3162 | location); | |
3163 | vals->push_back(v); | |
3164 | mpz_clear(val); | |
3165 | } | |
3166 | } | |
3167 | else | |
3168 | { | |
3169 | const char *p = s.data(); | |
3170 | const char *pend = s.data() + s.length(); | |
3171 | while (p < pend) | |
3172 | { | |
3173 | unsigned int c; | |
3174 | int adv = Lex::fetch_char(p, &c); | |
3175 | if (adv == 0) | |
3176 | { | |
3177 | warning_at(this->location(), 0, | |
3178 | "invalid UTF-8 encoding"); | |
3179 | adv = 1; | |
3180 | } | |
3181 | p += adv; | |
3182 | mpz_t val; | |
3183 | mpz_init_set_ui(val, c); | |
3184 | Expression* v = Expression::make_integer(&val, | |
3185 | element_type, | |
3186 | location); | |
3187 | vals->push_back(v); | |
3188 | mpz_clear(val); | |
3189 | } | |
3190 | } | |
3191 | ||
3192 | return Expression::make_slice_composite_literal(type, vals, | |
3193 | location); | |
3194 | } | |
3195 | } | |
3196 | } | |
3197 | ||
3198 | return this; | |
3199 | } | |
3200 | ||
0c77715b | 3201 | // Return the constant numeric value if there is one. |
e440a328 | 3202 | |
3203 | bool | |
0c77715b | 3204 | Type_conversion_expression::do_numeric_constant_value( |
3205 | Numeric_constant* nc) const | |
e440a328 | 3206 | { |
0c77715b | 3207 | if (!this->type_->is_numeric_type()) |
e440a328 | 3208 | return false; |
0c77715b | 3209 | if (!this->expr_->numeric_constant_value(nc)) |
e440a328 | 3210 | return false; |
0c77715b | 3211 | return nc->set_type(this->type_, false, this->location()); |
e440a328 | 3212 | } |
3213 | ||
3214 | // Return the constant string value if there is one. | |
3215 | ||
3216 | bool | |
3217 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3218 | { | |
3219 | if (this->type_->is_string_type() | |
3220 | && this->expr_->type()->integer_type() != NULL) | |
3221 | { | |
0c77715b | 3222 | Numeric_constant nc; |
3223 | if (this->expr_->numeric_constant_value(&nc)) | |
e440a328 | 3224 | { |
0c77715b | 3225 | unsigned long ival; |
3226 | if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID) | |
e440a328 | 3227 | { |
0c77715b | 3228 | val->clear(); |
3229 | Lex::append_char(ival, true, val, this->location()); | |
e440a328 | 3230 | return true; |
3231 | } | |
3232 | } | |
e440a328 | 3233 | } |
3234 | ||
3235 | // FIXME: Could handle conversion from const []int here. | |
3236 | ||
3237 | return false; | |
3238 | } | |
3239 | ||
3240 | // Check that types are convertible. | |
3241 | ||
3242 | void | |
3243 | Type_conversion_expression::do_check_types(Gogo*) | |
3244 | { | |
3245 | Type* type = this->type_; | |
3246 | Type* expr_type = this->expr_->type(); | |
3247 | std::string reason; | |
3248 | ||
5c13bd80 | 3249 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3250 | { |
842f6425 | 3251 | this->set_is_error(); |
3252 | return; | |
3253 | } | |
3254 | ||
e440a328 | 3255 | if (this->may_convert_function_types_ |
3256 | && type->function_type() != NULL | |
3257 | && expr_type->function_type() != NULL) | |
3258 | return; | |
3259 | ||
3260 | if (Type::are_convertible(type, expr_type, &reason)) | |
3261 | return; | |
3262 | ||
3263 | error_at(this->location(), "%s", reason.c_str()); | |
3264 | this->set_is_error(); | |
3265 | } | |
3266 | ||
3267 | // Get a tree for a type conversion. | |
3268 | ||
3269 | tree | |
3270 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3271 | { | |
3272 | Gogo* gogo = context->gogo(); | |
9f0e0513 | 3273 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 3274 | tree expr_tree = this->expr_->get_tree(context); |
3275 | ||
3276 | if (type_tree == error_mark_node | |
3277 | || expr_tree == error_mark_node | |
3278 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3279 | return error_mark_node; | |
3280 | ||
3281 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3282 | return fold_convert(type_tree, expr_tree); | |
3283 | ||
3284 | Type* type = this->type_; | |
3285 | Type* expr_type = this->expr_->type(); | |
3286 | tree ret; | |
3287 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3288 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3289 | expr_tree, this->location()); | |
3290 | else if (type->integer_type() != NULL) | |
3291 | { | |
3292 | if (expr_type->integer_type() != NULL | |
3293 | || expr_type->float_type() != NULL | |
3294 | || expr_type->is_unsafe_pointer_type()) | |
3295 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3296 | else | |
c3e6f413 | 3297 | go_unreachable(); |
e440a328 | 3298 | } |
3299 | else if (type->float_type() != NULL) | |
3300 | { | |
3301 | if (expr_type->integer_type() != NULL | |
3302 | || expr_type->float_type() != NULL) | |
3303 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3304 | else | |
c3e6f413 | 3305 | go_unreachable(); |
e440a328 | 3306 | } |
3307 | else if (type->complex_type() != NULL) | |
3308 | { | |
3309 | if (expr_type->complex_type() != NULL) | |
3310 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3311 | else | |
c3e6f413 | 3312 | go_unreachable(); |
e440a328 | 3313 | } |
3314 | else if (type->is_string_type() | |
3315 | && expr_type->integer_type() != NULL) | |
3316 | { | |
1b1f2abf | 3317 | Type* int_type = Type::lookup_integer_type("int"); |
3318 | tree int_type_tree = type_to_tree(int_type->get_backend(gogo)); | |
3319 | ||
3320 | expr_tree = fold_convert(int_type_tree, expr_tree); | |
e440a328 | 3321 | if (host_integerp(expr_tree, 0)) |
3322 | { | |
3323 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3324 | std::string s; | |
3325 | Lex::append_char(intval, true, &s, this->location()); | |
3326 | Expression* se = Expression::make_string(s, this->location()); | |
3327 | return se->get_tree(context); | |
3328 | } | |
3329 | ||
3330 | static tree int_to_string_fndecl; | |
3331 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3332 | this->location(), | |
3333 | "__go_int_to_string", | |
3334 | 1, | |
3335 | type_tree, | |
1b1f2abf | 3336 | int_type_tree, |
3337 | expr_tree); | |
e440a328 | 3338 | } |
55072f2b | 3339 | else if (type->is_string_type() && expr_type->is_slice_type()) |
e440a328 | 3340 | { |
e440a328 | 3341 | if (!DECL_P(expr_tree)) |
3342 | expr_tree = save_expr(expr_tree); | |
1b1f2abf | 3343 | |
3344 | Type* int_type = Type::lookup_integer_type("int"); | |
3345 | tree int_type_tree = type_to_tree(int_type->get_backend(gogo)); | |
3346 | ||
55072f2b | 3347 | Array_type* a = expr_type->array_type(); |
e440a328 | 3348 | Type* e = a->element_type()->forwarded(); |
c484d925 | 3349 | go_assert(e->integer_type() != NULL); |
e440a328 | 3350 | tree valptr = fold_convert(const_ptr_type_node, |
3351 | a->value_pointer_tree(gogo, expr_tree)); | |
3352 | tree len = a->length_tree(gogo, expr_tree); | |
1b1f2abf | 3353 | len = fold_convert_loc(this->location().gcc_location(), int_type_tree, |
b13c66cd | 3354 | len); |
60963afd | 3355 | if (e->integer_type()->is_byte()) |
e440a328 | 3356 | { |
3357 | static tree byte_array_to_string_fndecl; | |
3358 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3359 | this->location(), | |
3360 | "__go_byte_array_to_string", | |
3361 | 2, | |
3362 | type_tree, | |
3363 | const_ptr_type_node, | |
3364 | valptr, | |
1b1f2abf | 3365 | int_type_tree, |
e440a328 | 3366 | len); |
3367 | } | |
3368 | else | |
3369 | { | |
60963afd | 3370 | go_assert(e->integer_type()->is_rune()); |
e440a328 | 3371 | static tree int_array_to_string_fndecl; |
3372 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3373 | this->location(), | |
3374 | "__go_int_array_to_string", | |
3375 | 2, | |
3376 | type_tree, | |
3377 | const_ptr_type_node, | |
3378 | valptr, | |
1b1f2abf | 3379 | int_type_tree, |
e440a328 | 3380 | len); |
3381 | } | |
3382 | } | |
411eb89e | 3383 | else if (type->is_slice_type() && expr_type->is_string_type()) |
e440a328 | 3384 | { |
3385 | Type* e = type->array_type()->element_type()->forwarded(); | |
c484d925 | 3386 | go_assert(e->integer_type() != NULL); |
60963afd | 3387 | if (e->integer_type()->is_byte()) |
e440a328 | 3388 | { |
ef43e66c | 3389 | tree string_to_byte_array_fndecl = NULL_TREE; |
e440a328 | 3390 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, |
3391 | this->location(), | |
3392 | "__go_string_to_byte_array", | |
3393 | 1, | |
3394 | type_tree, | |
3395 | TREE_TYPE(expr_tree), | |
3396 | expr_tree); | |
3397 | } | |
3398 | else | |
3399 | { | |
60963afd | 3400 | go_assert(e->integer_type()->is_rune()); |
ef43e66c | 3401 | tree string_to_int_array_fndecl = NULL_TREE; |
e440a328 | 3402 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, |
3403 | this->location(), | |
3404 | "__go_string_to_int_array", | |
3405 | 1, | |
3406 | type_tree, | |
3407 | TREE_TYPE(expr_tree), | |
3408 | expr_tree); | |
3409 | } | |
3410 | } | |
3411 | else if ((type->is_unsafe_pointer_type() | |
3412 | && expr_type->points_to() != NULL) | |
3413 | || (expr_type->is_unsafe_pointer_type() | |
3414 | && type->points_to() != NULL)) | |
3415 | ret = fold_convert(type_tree, expr_tree); | |
3416 | else if (type->is_unsafe_pointer_type() | |
3417 | && expr_type->integer_type() != NULL) | |
3418 | ret = convert_to_pointer(type_tree, expr_tree); | |
3419 | else if (this->may_convert_function_types_ | |
3420 | && type->function_type() != NULL | |
3421 | && expr_type->function_type() != NULL) | |
b13c66cd | 3422 | ret = fold_convert_loc(this->location().gcc_location(), type_tree, |
3423 | expr_tree); | |
e440a328 | 3424 | else |
3425 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3426 | expr_tree, this->location()); | |
3427 | ||
3428 | return ret; | |
3429 | } | |
3430 | ||
3431 | // Output a type conversion in a constant expression. | |
3432 | ||
3433 | void | |
3434 | Type_conversion_expression::do_export(Export* exp) const | |
3435 | { | |
3436 | exp->write_c_string("convert("); | |
3437 | exp->write_type(this->type_); | |
3438 | exp->write_c_string(", "); | |
3439 | this->expr_->export_expression(exp); | |
3440 | exp->write_c_string(")"); | |
3441 | } | |
3442 | ||
3443 | // Import a type conversion or a struct construction. | |
3444 | ||
3445 | Expression* | |
3446 | Type_conversion_expression::do_import(Import* imp) | |
3447 | { | |
3448 | imp->require_c_string("convert("); | |
3449 | Type* type = imp->read_type(); | |
3450 | imp->require_c_string(", "); | |
3451 | Expression* val = Expression::import_expression(imp); | |
3452 | imp->require_c_string(")"); | |
3453 | return Expression::make_cast(type, val, imp->location()); | |
3454 | } | |
3455 | ||
d751bb78 | 3456 | // Dump ast representation for a type conversion expression. |
3457 | ||
3458 | void | |
3459 | Type_conversion_expression::do_dump_expression( | |
3460 | Ast_dump_context* ast_dump_context) const | |
3461 | { | |
3462 | ast_dump_context->dump_type(this->type_); | |
3463 | ast_dump_context->ostream() << "("; | |
3464 | ast_dump_context->dump_expression(this->expr_); | |
3465 | ast_dump_context->ostream() << ") "; | |
3466 | } | |
3467 | ||
e440a328 | 3468 | // Make a type cast expression. |
3469 | ||
3470 | Expression* | |
b13c66cd | 3471 | Expression::make_cast(Type* type, Expression* val, Location location) |
e440a328 | 3472 | { |
3473 | if (type->is_error_type() || val->is_error_expression()) | |
3474 | return Expression::make_error(location); | |
3475 | return new Type_conversion_expression(type, val, location); | |
3476 | } | |
3477 | ||
9581e91d | 3478 | // An unsafe type conversion, used to pass values to builtin functions. |
3479 | ||
3480 | class Unsafe_type_conversion_expression : public Expression | |
3481 | { | |
3482 | public: | |
3483 | Unsafe_type_conversion_expression(Type* type, Expression* expr, | |
b13c66cd | 3484 | Location location) |
9581e91d | 3485 | : Expression(EXPRESSION_UNSAFE_CONVERSION, location), |
3486 | type_(type), expr_(expr) | |
3487 | { } | |
3488 | ||
3489 | protected: | |
3490 | int | |
3491 | do_traverse(Traverse* traverse); | |
3492 | ||
3493 | Type* | |
3494 | do_type() | |
3495 | { return this->type_; } | |
3496 | ||
3497 | void | |
3498 | do_determine_type(const Type_context*) | |
a9182619 | 3499 | { this->expr_->determine_type_no_context(); } |
9581e91d | 3500 | |
3501 | Expression* | |
3502 | do_copy() | |
3503 | { | |
3504 | return new Unsafe_type_conversion_expression(this->type_, | |
3505 | this->expr_->copy(), | |
3506 | this->location()); | |
3507 | } | |
3508 | ||
3509 | tree | |
3510 | do_get_tree(Translate_context*); | |
3511 | ||
d751bb78 | 3512 | void |
3513 | do_dump_expression(Ast_dump_context*) const; | |
3514 | ||
9581e91d | 3515 | private: |
3516 | // The type to convert to. | |
3517 | Type* type_; | |
3518 | // The expression to convert. | |
3519 | Expression* expr_; | |
3520 | }; | |
3521 | ||
3522 | // Traversal. | |
3523 | ||
3524 | int | |
3525 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3526 | { | |
3527 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3528 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3529 | return TRAVERSE_EXIT; | |
3530 | return TRAVERSE_CONTINUE; | |
3531 | } | |
3532 | ||
3533 | // Convert to backend representation. | |
3534 | ||
3535 | tree | |
3536 | Unsafe_type_conversion_expression::do_get_tree(Translate_context* context) | |
3537 | { | |
3538 | // We are only called for a limited number of cases. | |
3539 | ||
3540 | Type* t = this->type_; | |
3541 | Type* et = this->expr_->type(); | |
3542 | ||
9f0e0513 | 3543 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
9581e91d | 3544 | tree expr_tree = this->expr_->get_tree(context); |
3545 | if (type_tree == error_mark_node || expr_tree == error_mark_node) | |
3546 | return error_mark_node; | |
3547 | ||
b13c66cd | 3548 | Location loc = this->location(); |
9581e91d | 3549 | |
3550 | bool use_view_convert = false; | |
411eb89e | 3551 | if (t->is_slice_type()) |
9581e91d | 3552 | { |
411eb89e | 3553 | go_assert(et->is_slice_type()); |
9581e91d | 3554 | use_view_convert = true; |
3555 | } | |
3556 | else if (t->map_type() != NULL) | |
c484d925 | 3557 | go_assert(et->map_type() != NULL); |
9581e91d | 3558 | else if (t->channel_type() != NULL) |
c484d925 | 3559 | go_assert(et->channel_type() != NULL); |
09ea332d | 3560 | else if (t->points_to() != NULL) |
c484d925 | 3561 | go_assert(et->points_to() != NULL || et->is_nil_type()); |
9581e91d | 3562 | else if (et->is_unsafe_pointer_type()) |
c484d925 | 3563 | go_assert(t->points_to() != NULL); |
9581e91d | 3564 | else if (t->interface_type() != NULL && !t->interface_type()->is_empty()) |
3565 | { | |
c484d925 | 3566 | go_assert(et->interface_type() != NULL |
9581e91d | 3567 | && !et->interface_type()->is_empty()); |
3568 | use_view_convert = true; | |
3569 | } | |
3570 | else if (t->interface_type() != NULL && t->interface_type()->is_empty()) | |
3571 | { | |
c484d925 | 3572 | go_assert(et->interface_type() != NULL |
9581e91d | 3573 | && et->interface_type()->is_empty()); |
3574 | use_view_convert = true; | |
3575 | } | |
588e3cf9 | 3576 | else if (t->integer_type() != NULL) |
3577 | { | |
c484d925 | 3578 | go_assert(et->is_boolean_type() |
588e3cf9 | 3579 | || et->integer_type() != NULL |
3580 | || et->function_type() != NULL | |
3581 | || et->points_to() != NULL | |
3582 | || et->map_type() != NULL | |
3583 | || et->channel_type() != NULL); | |
3584 | return convert_to_integer(type_tree, expr_tree); | |
3585 | } | |
9581e91d | 3586 | else |
c3e6f413 | 3587 | go_unreachable(); |
9581e91d | 3588 | |
3589 | if (use_view_convert) | |
b13c66cd | 3590 | return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree, |
3591 | expr_tree); | |
9581e91d | 3592 | else |
b13c66cd | 3593 | return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree); |
9581e91d | 3594 | } |
3595 | ||
d751bb78 | 3596 | // Dump ast representation for an unsafe type conversion expression. |
3597 | ||
3598 | void | |
3599 | Unsafe_type_conversion_expression::do_dump_expression( | |
3600 | Ast_dump_context* ast_dump_context) const | |
3601 | { | |
3602 | ast_dump_context->dump_type(this->type_); | |
3603 | ast_dump_context->ostream() << "("; | |
3604 | ast_dump_context->dump_expression(this->expr_); | |
3605 | ast_dump_context->ostream() << ") "; | |
3606 | } | |
3607 | ||
9581e91d | 3608 | // Make an unsafe type conversion expression. |
3609 | ||
3610 | Expression* | |
3611 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
b13c66cd | 3612 | Location location) |
9581e91d | 3613 | { |
3614 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3615 | } | |
3616 | ||
e440a328 | 3617 | // Unary expressions. |
3618 | ||
3619 | class Unary_expression : public Expression | |
3620 | { | |
3621 | public: | |
b13c66cd | 3622 | Unary_expression(Operator op, Expression* expr, Location location) |
e440a328 | 3623 | : Expression(EXPRESSION_UNARY, location), |
09ea332d | 3624 | op_(op), escapes_(true), create_temp_(false), expr_(expr) |
e440a328 | 3625 | { } |
3626 | ||
3627 | // Return the operator. | |
3628 | Operator | |
3629 | op() const | |
3630 | { return this->op_; } | |
3631 | ||
3632 | // Return the operand. | |
3633 | Expression* | |
3634 | operand() const | |
3635 | { return this->expr_; } | |
3636 | ||
3637 | // Record that an address expression does not escape. | |
3638 | void | |
3639 | set_does_not_escape() | |
3640 | { | |
c484d925 | 3641 | go_assert(this->op_ == OPERATOR_AND); |
e440a328 | 3642 | this->escapes_ = false; |
3643 | } | |
3644 | ||
09ea332d | 3645 | // Record that this is an address expression which should create a |
3646 | // temporary variable if necessary. This is used for method calls. | |
3647 | void | |
3648 | set_create_temp() | |
3649 | { | |
3650 | go_assert(this->op_ == OPERATOR_AND); | |
3651 | this->create_temp_ = true; | |
3652 | } | |
3653 | ||
0c77715b | 3654 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3655 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3656 | static bool |
0c77715b | 3657 | eval_constant(Operator op, const Numeric_constant* unc, |
3658 | Location, Numeric_constant* nc); | |
e440a328 | 3659 | |
3660 | static Expression* | |
3661 | do_import(Import*); | |
3662 | ||
3663 | protected: | |
3664 | int | |
3665 | do_traverse(Traverse* traverse) | |
3666 | { return Expression::traverse(&this->expr_, traverse); } | |
3667 | ||
3668 | Expression* | |
ceeb4318 | 3669 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 3670 | |
3671 | bool | |
3672 | do_is_constant() const; | |
3673 | ||
3674 | bool | |
0c77715b | 3675 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 3676 | |
3677 | Type* | |
3678 | do_type(); | |
3679 | ||
3680 | void | |
3681 | do_determine_type(const Type_context*); | |
3682 | ||
3683 | void | |
3684 | do_check_types(Gogo*); | |
3685 | ||
3686 | Expression* | |
3687 | do_copy() | |
3688 | { | |
3689 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3690 | this->location()); | |
3691 | } | |
3692 | ||
baef9f7a | 3693 | bool |
3694 | do_must_eval_subexpressions_in_order(int*) const | |
3695 | { return this->op_ == OPERATOR_MULT; } | |
3696 | ||
e440a328 | 3697 | bool |
3698 | do_is_addressable() const | |
3699 | { return this->op_ == OPERATOR_MULT; } | |
3700 | ||
3701 | tree | |
3702 | do_get_tree(Translate_context*); | |
3703 | ||
3704 | void | |
3705 | do_export(Export*) const; | |
3706 | ||
d751bb78 | 3707 | void |
3708 | do_dump_expression(Ast_dump_context*) const; | |
3709 | ||
e440a328 | 3710 | private: |
3711 | // The unary operator to apply. | |
3712 | Operator op_; | |
3713 | // Normally true. False if this is an address expression which does | |
3714 | // not escape the current function. | |
3715 | bool escapes_; | |
09ea332d | 3716 | // True if this is an address expression which should create a |
3717 | // temporary variable if necessary. | |
3718 | bool create_temp_; | |
e440a328 | 3719 | // The operand. |
3720 | Expression* expr_; | |
3721 | }; | |
3722 | ||
3723 | // If we are taking the address of a composite literal, and the | |
3724 | // contents are not constant, then we want to make a heap composite | |
3725 | // instead. | |
3726 | ||
3727 | Expression* | |
ceeb4318 | 3728 | Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 3729 | { |
b13c66cd | 3730 | Location loc = this->location(); |
e440a328 | 3731 | Operator op = this->op_; |
3732 | Expression* expr = this->expr_; | |
3733 | ||
3734 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3735 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3736 | ||
3737 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3738 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3739 | // analysis here? This case is found in math/unsafe.go and is | |
3740 | // therefore worth special casing. | |
3741 | if (op == OPERATOR_MULT) | |
3742 | { | |
3743 | Expression* e = expr; | |
3744 | while (e->classification() == EXPRESSION_CONVERSION) | |
3745 | { | |
3746 | Type_conversion_expression* te | |
3747 | = static_cast<Type_conversion_expression*>(e); | |
3748 | e = te->expr(); | |
3749 | } | |
3750 | ||
3751 | if (e->classification() == EXPRESSION_UNARY) | |
3752 | { | |
3753 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3754 | if (ue->op_ == OPERATOR_AND) | |
3755 | { | |
3756 | if (e == expr) | |
3757 | { | |
3758 | // *&x == x. | |
3759 | return ue->expr_; | |
3760 | } | |
3761 | ue->set_does_not_escape(); | |
3762 | } | |
3763 | } | |
3764 | } | |
3765 | ||
55661ce9 | 3766 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3767 | // having to deal with TYPE_VOID in other places. | |
3768 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3769 | { | |
3770 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3771 | return Expression::make_error(this->location()); | |
3772 | } | |
3773 | ||
59a401fe | 3774 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR) |
e440a328 | 3775 | { |
0c77715b | 3776 | Numeric_constant nc; |
3777 | if (expr->numeric_constant_value(&nc)) | |
e440a328 | 3778 | { |
0c77715b | 3779 | Numeric_constant result; |
3780 | if (Unary_expression::eval_constant(op, &nc, loc, &result)) | |
3781 | return result.expression(loc); | |
e440a328 | 3782 | } |
3783 | } | |
3784 | ||
3785 | return this; | |
3786 | } | |
3787 | ||
3788 | // Return whether a unary expression is a constant. | |
3789 | ||
3790 | bool | |
3791 | Unary_expression::do_is_constant() const | |
3792 | { | |
3793 | if (this->op_ == OPERATOR_MULT) | |
3794 | { | |
3795 | // Indirecting through a pointer is only constant if the object | |
3796 | // to which the expression points is constant, but we currently | |
3797 | // have no way to determine that. | |
3798 | return false; | |
3799 | } | |
3800 | else if (this->op_ == OPERATOR_AND) | |
3801 | { | |
3802 | // Taking the address of a variable is constant if it is a | |
3803 | // global variable, not constant otherwise. In other cases | |
3804 | // taking the address is probably not a constant. | |
3805 | Var_expression* ve = this->expr_->var_expression(); | |
3806 | if (ve != NULL) | |
3807 | { | |
3808 | Named_object* no = ve->named_object(); | |
3809 | return no->is_variable() && no->var_value()->is_global(); | |
3810 | } | |
3811 | return false; | |
3812 | } | |
3813 | else | |
3814 | return this->expr_->is_constant(); | |
3815 | } | |
3816 | ||
0c77715b | 3817 | // Apply unary opcode OP to UNC, setting NC. Return true if this |
3818 | // could be done, false if not. Issue errors for overflow. | |
e440a328 | 3819 | |
3820 | bool | |
0c77715b | 3821 | Unary_expression::eval_constant(Operator op, const Numeric_constant* unc, |
3822 | Location location, Numeric_constant* nc) | |
e440a328 | 3823 | { |
3824 | switch (op) | |
3825 | { | |
3826 | case OPERATOR_PLUS: | |
0c77715b | 3827 | *nc = *unc; |
e440a328 | 3828 | return true; |
0c77715b | 3829 | |
e440a328 | 3830 | case OPERATOR_MINUS: |
0c77715b | 3831 | if (unc->is_int() || unc->is_rune()) |
3832 | break; | |
3833 | else if (unc->is_float()) | |
3834 | { | |
3835 | mpfr_t uval; | |
3836 | unc->get_float(&uval); | |
3837 | mpfr_t val; | |
3838 | mpfr_init(val); | |
3839 | mpfr_neg(val, uval, GMP_RNDN); | |
3840 | nc->set_float(unc->type(), val); | |
3841 | mpfr_clear(uval); | |
3842 | mpfr_clear(val); | |
3843 | return true; | |
3844 | } | |
3845 | else if (unc->is_complex()) | |
3846 | { | |
3847 | mpfr_t ureal, uimag; | |
3848 | unc->get_complex(&ureal, &uimag); | |
3849 | mpfr_t real, imag; | |
3850 | mpfr_init(real); | |
3851 | mpfr_init(imag); | |
3852 | mpfr_neg(real, ureal, GMP_RNDN); | |
3853 | mpfr_neg(imag, uimag, GMP_RNDN); | |
3854 | nc->set_complex(unc->type(), real, imag); | |
3855 | mpfr_clear(ureal); | |
3856 | mpfr_clear(uimag); | |
3857 | mpfr_clear(real); | |
3858 | mpfr_clear(imag); | |
3859 | return true; | |
3860 | } | |
e440a328 | 3861 | else |
0c77715b | 3862 | go_unreachable(); |
e440a328 | 3863 | |
0c77715b | 3864 | case OPERATOR_XOR: |
3865 | break; | |
68448d53 | 3866 | |
59a401fe | 3867 | case OPERATOR_NOT: |
e440a328 | 3868 | case OPERATOR_AND: |
3869 | case OPERATOR_MULT: | |
3870 | return false; | |
0c77715b | 3871 | |
e440a328 | 3872 | default: |
c3e6f413 | 3873 | go_unreachable(); |
e440a328 | 3874 | } |
e440a328 | 3875 | |
0c77715b | 3876 | if (!unc->is_int() && !unc->is_rune()) |
3877 | return false; | |
3878 | ||
3879 | mpz_t uval; | |
8387e1df | 3880 | if (unc->is_rune()) |
3881 | unc->get_rune(&uval); | |
3882 | else | |
3883 | unc->get_int(&uval); | |
0c77715b | 3884 | mpz_t val; |
3885 | mpz_init(val); | |
e440a328 | 3886 | |
e440a328 | 3887 | switch (op) |
3888 | { | |
e440a328 | 3889 | case OPERATOR_MINUS: |
0c77715b | 3890 | mpz_neg(val, uval); |
3891 | break; | |
3892 | ||
e440a328 | 3893 | case OPERATOR_NOT: |
0c77715b | 3894 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); |
3895 | break; | |
3896 | ||
e440a328 | 3897 | case OPERATOR_XOR: |
0c77715b | 3898 | { |
3899 | Type* utype = unc->type(); | |
3900 | if (utype->integer_type() == NULL | |
3901 | || utype->integer_type()->is_abstract()) | |
3902 | mpz_com(val, uval); | |
3903 | else | |
3904 | { | |
3905 | // The number of HOST_WIDE_INTs that it takes to represent | |
3906 | // UVAL. | |
3907 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3908 | + HOST_BITS_PER_WIDE_INT | |
3909 | - 1) | |
3910 | / HOST_BITS_PER_WIDE_INT); | |
e440a328 | 3911 | |
0c77715b | 3912 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; |
3913 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3914 | ||
3915 | size_t obits = utype->integer_type()->bits(); | |
3916 | ||
3917 | if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0) | |
3918 | { | |
3919 | mpz_t adj; | |
3920 | mpz_init_set_ui(adj, 1); | |
3921 | mpz_mul_2exp(adj, adj, obits); | |
3922 | mpz_add(uval, uval, adj); | |
3923 | mpz_clear(adj); | |
3924 | } | |
3925 | ||
3926 | size_t ecount; | |
3927 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3928 | go_assert(ecount <= count); | |
3929 | ||
3930 | // Trim down to the number of words required by the type. | |
3931 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3932 | / HOST_BITS_PER_WIDE_INT); | |
3933 | go_assert(ocount <= count); | |
3934 | ||
3935 | for (size_t i = 0; i < ocount; ++i) | |
3936 | phwi[i] = ~phwi[i]; | |
3937 | ||
3938 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3939 | if (clearbits != 0) | |
3940 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3941 | >> clearbits); | |
3942 | ||
3943 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3944 | ||
3945 | if (!utype->integer_type()->is_unsigned() | |
3946 | && mpz_tstbit(val, obits - 1)) | |
3947 | { | |
3948 | mpz_t adj; | |
3949 | mpz_init_set_ui(adj, 1); | |
3950 | mpz_mul_2exp(adj, adj, obits); | |
3951 | mpz_sub(val, val, adj); | |
3952 | mpz_clear(adj); | |
3953 | } | |
3954 | ||
3955 | delete[] phwi; | |
3956 | } | |
3957 | } | |
3958 | break; | |
e440a328 | 3959 | |
e440a328 | 3960 | default: |
c3e6f413 | 3961 | go_unreachable(); |
e440a328 | 3962 | } |
e440a328 | 3963 | |
0c77715b | 3964 | if (unc->is_rune()) |
3965 | nc->set_rune(NULL, val); | |
e440a328 | 3966 | else |
0c77715b | 3967 | nc->set_int(NULL, val); |
e440a328 | 3968 | |
0c77715b | 3969 | mpz_clear(uval); |
3970 | mpz_clear(val); | |
e440a328 | 3971 | |
0c77715b | 3972 | return nc->set_type(unc->type(), true, location); |
e440a328 | 3973 | } |
3974 | ||
0c77715b | 3975 | // Return the integral constant value of a unary expression, if it has one. |
e440a328 | 3976 | |
3977 | bool | |
0c77715b | 3978 | Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 3979 | { |
0c77715b | 3980 | Numeric_constant unc; |
3981 | if (!this->expr_->numeric_constant_value(&unc)) | |
3982 | return false; | |
3983 | return Unary_expression::eval_constant(this->op_, &unc, this->location(), | |
3984 | nc); | |
e440a328 | 3985 | } |
3986 | ||
3987 | // Return the type of a unary expression. | |
3988 | ||
3989 | Type* | |
3990 | Unary_expression::do_type() | |
3991 | { | |
3992 | switch (this->op_) | |
3993 | { | |
3994 | case OPERATOR_PLUS: | |
3995 | case OPERATOR_MINUS: | |
3996 | case OPERATOR_NOT: | |
3997 | case OPERATOR_XOR: | |
3998 | return this->expr_->type(); | |
3999 | ||
4000 | case OPERATOR_AND: | |
4001 | return Type::make_pointer_type(this->expr_->type()); | |
4002 | ||
4003 | case OPERATOR_MULT: | |
4004 | { | |
4005 | Type* subtype = this->expr_->type(); | |
4006 | Type* points_to = subtype->points_to(); | |
4007 | if (points_to == NULL) | |
4008 | return Type::make_error_type(); | |
4009 | return points_to; | |
4010 | } | |
4011 | ||
4012 | default: | |
c3e6f413 | 4013 | go_unreachable(); |
e440a328 | 4014 | } |
4015 | } | |
4016 | ||
4017 | // Determine abstract types for a unary expression. | |
4018 | ||
4019 | void | |
4020 | Unary_expression::do_determine_type(const Type_context* context) | |
4021 | { | |
4022 | switch (this->op_) | |
4023 | { | |
4024 | case OPERATOR_PLUS: | |
4025 | case OPERATOR_MINUS: | |
4026 | case OPERATOR_NOT: | |
4027 | case OPERATOR_XOR: | |
4028 | this->expr_->determine_type(context); | |
4029 | break; | |
4030 | ||
4031 | case OPERATOR_AND: | |
4032 | // Taking the address of something. | |
4033 | { | |
4034 | Type* subtype = (context->type == NULL | |
4035 | ? NULL | |
4036 | : context->type->points_to()); | |
4037 | Type_context subcontext(subtype, false); | |
4038 | this->expr_->determine_type(&subcontext); | |
4039 | } | |
4040 | break; | |
4041 | ||
4042 | case OPERATOR_MULT: | |
4043 | // Indirecting through a pointer. | |
4044 | { | |
4045 | Type* subtype = (context->type == NULL | |
4046 | ? NULL | |
4047 | : Type::make_pointer_type(context->type)); | |
4048 | Type_context subcontext(subtype, false); | |
4049 | this->expr_->determine_type(&subcontext); | |
4050 | } | |
4051 | break; | |
4052 | ||
4053 | default: | |
c3e6f413 | 4054 | go_unreachable(); |
e440a328 | 4055 | } |
4056 | } | |
4057 | ||
4058 | // Check types for a unary expression. | |
4059 | ||
4060 | void | |
4061 | Unary_expression::do_check_types(Gogo*) | |
4062 | { | |
9fe897ef | 4063 | Type* type = this->expr_->type(); |
5c13bd80 | 4064 | if (type->is_error()) |
9fe897ef | 4065 | { |
4066 | this->set_is_error(); | |
4067 | return; | |
4068 | } | |
4069 | ||
e440a328 | 4070 | switch (this->op_) |
4071 | { | |
4072 | case OPERATOR_PLUS: | |
4073 | case OPERATOR_MINUS: | |
9fe897ef | 4074 | if (type->integer_type() == NULL |
4075 | && type->float_type() == NULL | |
4076 | && type->complex_type() == NULL) | |
4077 | this->report_error(_("expected numeric type")); | |
e440a328 | 4078 | break; |
4079 | ||
4080 | case OPERATOR_NOT: | |
59a401fe | 4081 | if (!type->is_boolean_type()) |
4082 | this->report_error(_("expected boolean type")); | |
4083 | break; | |
4084 | ||
e440a328 | 4085 | case OPERATOR_XOR: |
9fe897ef | 4086 | if (type->integer_type() == NULL |
4087 | && !type->is_boolean_type()) | |
4088 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 4089 | break; |
4090 | ||
4091 | case OPERATOR_AND: | |
4092 | if (!this->expr_->is_addressable()) | |
09ea332d | 4093 | { |
4094 | if (!this->create_temp_) | |
4095 | this->report_error(_("invalid operand for unary %<&%>")); | |
4096 | } | |
e440a328 | 4097 | else |
4098 | this->expr_->address_taken(this->escapes_); | |
4099 | break; | |
4100 | ||
4101 | case OPERATOR_MULT: | |
4102 | // Indirecting through a pointer. | |
9fe897ef | 4103 | if (type->points_to() == NULL) |
4104 | this->report_error(_("expected pointer")); | |
e440a328 | 4105 | break; |
4106 | ||
4107 | default: | |
c3e6f413 | 4108 | go_unreachable(); |
e440a328 | 4109 | } |
4110 | } | |
4111 | ||
4112 | // Get a tree for a unary expression. | |
4113 | ||
4114 | tree | |
4115 | Unary_expression::do_get_tree(Translate_context* context) | |
4116 | { | |
1b1f2abf | 4117 | Gogo* gogo = context->gogo(); |
e9d3367e | 4118 | Location loc = this->location(); |
4119 | ||
4120 | // Taking the address of a set-and-use-temporary expression requires | |
4121 | // setting the temporary and then taking the address. | |
4122 | if (this->op_ == OPERATOR_AND) | |
4123 | { | |
4124 | Set_and_use_temporary_expression* sut = | |
4125 | this->expr_->set_and_use_temporary_expression(); | |
4126 | if (sut != NULL) | |
4127 | { | |
4128 | Temporary_statement* temp = sut->temporary(); | |
4129 | Bvariable* bvar = temp->get_backend_variable(context); | |
4130 | tree var_tree = var_to_tree(bvar); | |
4131 | Expression* val = sut->expression(); | |
4132 | tree val_tree = val->get_tree(context); | |
4133 | if (var_tree == error_mark_node || val_tree == error_mark_node) | |
4134 | return error_mark_node; | |
4135 | tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(), | |
4136 | var_tree); | |
4137 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4138 | TREE_TYPE(addr_tree), | |
4139 | build2_loc(sut->location().gcc_location(), | |
4140 | MODIFY_EXPR, void_type_node, | |
4141 | var_tree, val_tree), | |
4142 | addr_tree); | |
4143 | } | |
4144 | } | |
4145 | ||
e440a328 | 4146 | tree expr = this->expr_->get_tree(context); |
4147 | if (expr == error_mark_node) | |
4148 | return error_mark_node; | |
4149 | ||
e440a328 | 4150 | switch (this->op_) |
4151 | { | |
4152 | case OPERATOR_PLUS: | |
4153 | return expr; | |
4154 | ||
4155 | case OPERATOR_MINUS: | |
4156 | { | |
4157 | tree type = TREE_TYPE(expr); | |
4158 | tree compute_type = excess_precision_type(type); | |
4159 | if (compute_type != NULL_TREE) | |
4160 | expr = ::convert(compute_type, expr); | |
b13c66cd | 4161 | tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR, |
e440a328 | 4162 | (compute_type != NULL_TREE |
4163 | ? compute_type | |
4164 | : type), | |
4165 | expr); | |
4166 | if (compute_type != NULL_TREE) | |
4167 | ret = ::convert(type, ret); | |
4168 | return ret; | |
4169 | } | |
4170 | ||
4171 | case OPERATOR_NOT: | |
4172 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
b13c66cd | 4173 | return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR, |
4174 | TREE_TYPE(expr), expr); | |
e440a328 | 4175 | else |
b13c66cd | 4176 | return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node, |
4177 | expr, build_int_cst(TREE_TYPE(expr), 0)); | |
e440a328 | 4178 | |
4179 | case OPERATOR_XOR: | |
b13c66cd | 4180 | return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr), |
4181 | expr); | |
e440a328 | 4182 | |
4183 | case OPERATOR_AND: | |
09ea332d | 4184 | if (!this->create_temp_) |
4185 | { | |
4186 | // We should not see a non-constant constructor here; cases | |
4187 | // where we would see one should have been moved onto the | |
4188 | // heap at parse time. Taking the address of a nonconstant | |
4189 | // constructor will not do what the programmer expects. | |
4190 | go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
4191 | go_assert(TREE_CODE(expr) != ADDR_EXPR); | |
4192 | } | |
e440a328 | 4193 | |
4194 | // Build a decl for a constant constructor. | |
4195 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
4196 | { | |
b13c66cd | 4197 | tree decl = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 4198 | create_tmp_var_name("C"), TREE_TYPE(expr)); |
4199 | DECL_EXTERNAL(decl) = 0; | |
4200 | TREE_PUBLIC(decl) = 0; | |
4201 | TREE_READONLY(decl) = 1; | |
4202 | TREE_CONSTANT(decl) = 1; | |
4203 | TREE_STATIC(decl) = 1; | |
4204 | TREE_ADDRESSABLE(decl) = 1; | |
4205 | DECL_ARTIFICIAL(decl) = 1; | |
4206 | DECL_INITIAL(decl) = expr; | |
4207 | rest_of_decl_compilation(decl, 1, 0); | |
4208 | expr = decl; | |
4209 | } | |
4210 | ||
09ea332d | 4211 | if (this->create_temp_ |
4212 | && !TREE_ADDRESSABLE(TREE_TYPE(expr)) | |
dd28fd36 | 4213 | && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr)) |
09ea332d | 4214 | && TREE_CODE(expr) != INDIRECT_REF |
4215 | && TREE_CODE(expr) != COMPONENT_REF) | |
4216 | { | |
fc81003d | 4217 | if (current_function_decl != NULL) |
4218 | { | |
4219 | tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr)); | |
4220 | DECL_IGNORED_P(tmp) = 1; | |
4221 | DECL_INITIAL(tmp) = expr; | |
4222 | TREE_ADDRESSABLE(tmp) = 1; | |
4223 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4224 | build_pointer_type(TREE_TYPE(expr)), | |
4225 | build1_loc(loc.gcc_location(), DECL_EXPR, | |
4226 | void_type_node, tmp), | |
4227 | build_fold_addr_expr_loc(loc.gcc_location(), | |
4228 | tmp)); | |
4229 | } | |
4230 | else | |
4231 | { | |
4232 | tree tmp = build_decl(loc.gcc_location(), VAR_DECL, | |
4233 | create_tmp_var_name("A"), TREE_TYPE(expr)); | |
4234 | DECL_EXTERNAL(tmp) = 0; | |
4235 | TREE_PUBLIC(tmp) = 0; | |
4236 | TREE_STATIC(tmp) = 1; | |
4237 | DECL_ARTIFICIAL(tmp) = 1; | |
4238 | TREE_ADDRESSABLE(tmp) = 1; | |
4239 | tree make_tmp; | |
4240 | if (!TREE_CONSTANT(expr)) | |
4241 | make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR, | |
4242 | void_type_node, tmp, expr); | |
4243 | else | |
4244 | { | |
4245 | TREE_READONLY(tmp) = 1; | |
4246 | TREE_CONSTANT(tmp) = 1; | |
4247 | DECL_INITIAL(tmp) = expr; | |
4248 | make_tmp = NULL_TREE; | |
4249 | } | |
4250 | rest_of_decl_compilation(tmp, 1, 0); | |
4251 | tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp); | |
4252 | if (make_tmp == NULL_TREE) | |
4253 | return addr; | |
4254 | return build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4255 | TREE_TYPE(addr), make_tmp, addr); | |
4256 | } | |
09ea332d | 4257 | } |
4258 | ||
b13c66cd | 4259 | return build_fold_addr_expr_loc(loc.gcc_location(), expr); |
e440a328 | 4260 | |
4261 | case OPERATOR_MULT: | |
4262 | { | |
c484d925 | 4263 | go_assert(POINTER_TYPE_P(TREE_TYPE(expr))); |
e440a328 | 4264 | |
4265 | // If we are dereferencing the pointer to a large struct, we | |
4266 | // need to check for nil. We don't bother to check for small | |
4267 | // structs because we expect the system to crash on a nil | |
4268 | // pointer dereference. | |
19b4f09b | 4269 | tree target_type_tree = TREE_TYPE(TREE_TYPE(expr)); |
4270 | if (!VOID_TYPE_P(target_type_tree)) | |
e440a328 | 4271 | { |
19b4f09b | 4272 | HOST_WIDE_INT s = int_size_in_bytes(target_type_tree); |
4273 | if (s == -1 || s >= 4096) | |
4274 | { | |
4275 | if (!DECL_P(expr)) | |
4276 | expr = save_expr(expr); | |
4277 | tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR, | |
4278 | boolean_type_node, | |
4279 | expr, | |
4280 | fold_convert(TREE_TYPE(expr), | |
4281 | null_pointer_node)); | |
1b1f2abf | 4282 | tree crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, |
19b4f09b | 4283 | loc); |
4284 | expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
4285 | TREE_TYPE(expr), build3(COND_EXPR, | |
4286 | void_type_node, | |
4287 | compare, crash, | |
4288 | NULL_TREE), | |
4289 | expr); | |
4290 | } | |
e440a328 | 4291 | } |
4292 | ||
4293 | // If the type of EXPR is a recursive pointer type, then we | |
4294 | // need to insert a cast before indirecting. | |
19b4f09b | 4295 | if (VOID_TYPE_P(target_type_tree)) |
e440a328 | 4296 | { |
4297 | Type* pt = this->expr_->type()->points_to(); | |
1b1f2abf | 4298 | tree ind = type_to_tree(pt->get_backend(gogo)); |
b13c66cd | 4299 | expr = fold_convert_loc(loc.gcc_location(), |
4300 | build_pointer_type(ind), expr); | |
e440a328 | 4301 | } |
4302 | ||
b13c66cd | 4303 | return build_fold_indirect_ref_loc(loc.gcc_location(), expr); |
e440a328 | 4304 | } |
4305 | ||
4306 | default: | |
c3e6f413 | 4307 | go_unreachable(); |
e440a328 | 4308 | } |
4309 | } | |
4310 | ||
4311 | // Export a unary expression. | |
4312 | ||
4313 | void | |
4314 | Unary_expression::do_export(Export* exp) const | |
4315 | { | |
4316 | switch (this->op_) | |
4317 | { | |
4318 | case OPERATOR_PLUS: | |
4319 | exp->write_c_string("+ "); | |
4320 | break; | |
4321 | case OPERATOR_MINUS: | |
4322 | exp->write_c_string("- "); | |
4323 | break; | |
4324 | case OPERATOR_NOT: | |
4325 | exp->write_c_string("! "); | |
4326 | break; | |
4327 | case OPERATOR_XOR: | |
4328 | exp->write_c_string("^ "); | |
4329 | break; | |
4330 | case OPERATOR_AND: | |
4331 | case OPERATOR_MULT: | |
4332 | default: | |
c3e6f413 | 4333 | go_unreachable(); |
e440a328 | 4334 | } |
4335 | this->expr_->export_expression(exp); | |
4336 | } | |
4337 | ||
4338 | // Import a unary expression. | |
4339 | ||
4340 | Expression* | |
4341 | Unary_expression::do_import(Import* imp) | |
4342 | { | |
4343 | Operator op; | |
4344 | switch (imp->get_char()) | |
4345 | { | |
4346 | case '+': | |
4347 | op = OPERATOR_PLUS; | |
4348 | break; | |
4349 | case '-': | |
4350 | op = OPERATOR_MINUS; | |
4351 | break; | |
4352 | case '!': | |
4353 | op = OPERATOR_NOT; | |
4354 | break; | |
4355 | case '^': | |
4356 | op = OPERATOR_XOR; | |
4357 | break; | |
4358 | default: | |
c3e6f413 | 4359 | go_unreachable(); |
e440a328 | 4360 | } |
4361 | imp->require_c_string(" "); | |
4362 | Expression* expr = Expression::import_expression(imp); | |
4363 | return Expression::make_unary(op, expr, imp->location()); | |
4364 | } | |
4365 | ||
d751bb78 | 4366 | // Dump ast representation of an unary expression. |
4367 | ||
4368 | void | |
4369 | Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
4370 | { | |
4371 | ast_dump_context->dump_operator(this->op_); | |
4372 | ast_dump_context->ostream() << "("; | |
4373 | ast_dump_context->dump_expression(this->expr_); | |
4374 | ast_dump_context->ostream() << ") "; | |
4375 | } | |
4376 | ||
e440a328 | 4377 | // Make a unary expression. |
4378 | ||
4379 | Expression* | |
b13c66cd | 4380 | Expression::make_unary(Operator op, Expression* expr, Location location) |
e440a328 | 4381 | { |
4382 | return new Unary_expression(op, expr, location); | |
4383 | } | |
4384 | ||
4385 | // If this is an indirection through a pointer, return the expression | |
4386 | // being pointed through. Otherwise return this. | |
4387 | ||
4388 | Expression* | |
4389 | Expression::deref() | |
4390 | { | |
4391 | if (this->classification_ == EXPRESSION_UNARY) | |
4392 | { | |
4393 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4394 | if (ue->op() == OPERATOR_MULT) | |
4395 | return ue->operand(); | |
4396 | } | |
4397 | return this; | |
4398 | } | |
4399 | ||
4400 | // Class Binary_expression. | |
4401 | ||
4402 | // Traversal. | |
4403 | ||
4404 | int | |
4405 | Binary_expression::do_traverse(Traverse* traverse) | |
4406 | { | |
4407 | int t = Expression::traverse(&this->left_, traverse); | |
4408 | if (t == TRAVERSE_EXIT) | |
4409 | return TRAVERSE_EXIT; | |
4410 | return Expression::traverse(&this->right_, traverse); | |
4411 | } | |
4412 | ||
0c77715b | 4413 | // Return the type to use for a binary operation on operands of |
4414 | // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as | |
4415 | // such may be NULL or abstract. | |
4416 | ||
4417 | bool | |
4418 | Binary_expression::operation_type(Operator op, Type* left_type, | |
4419 | Type* right_type, Type** result_type) | |
4420 | { | |
4421 | if (left_type != right_type | |
4422 | && !left_type->is_abstract() | |
4423 | && !right_type->is_abstract() | |
4424 | && left_type->base() != right_type->base() | |
4425 | && op != OPERATOR_LSHIFT | |
4426 | && op != OPERATOR_RSHIFT) | |
4427 | { | |
4428 | // May be a type error--let it be diagnosed elsewhere. | |
4429 | return false; | |
4430 | } | |
4431 | ||
4432 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4433 | { | |
4434 | if (left_type->integer_type() != NULL) | |
4435 | *result_type = left_type; | |
4436 | else | |
4437 | *result_type = Type::make_abstract_integer_type(); | |
4438 | } | |
4439 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
4440 | *result_type = left_type; | |
4441 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
4442 | *result_type = right_type; | |
4443 | else if (!left_type->is_abstract()) | |
4444 | *result_type = left_type; | |
4445 | else if (!right_type->is_abstract()) | |
4446 | *result_type = right_type; | |
4447 | else if (left_type->complex_type() != NULL) | |
4448 | *result_type = left_type; | |
4449 | else if (right_type->complex_type() != NULL) | |
4450 | *result_type = right_type; | |
4451 | else if (left_type->float_type() != NULL) | |
4452 | *result_type = left_type; | |
4453 | else if (right_type->float_type() != NULL) | |
4454 | *result_type = right_type; | |
4455 | else if (left_type->integer_type() != NULL | |
4456 | && left_type->integer_type()->is_rune()) | |
4457 | *result_type = left_type; | |
4458 | else if (right_type->integer_type() != NULL | |
4459 | && right_type->integer_type()->is_rune()) | |
4460 | *result_type = right_type; | |
4461 | else | |
4462 | *result_type = left_type; | |
4463 | ||
4464 | return true; | |
4465 | } | |
4466 | ||
4467 | // Convert an integer comparison code and an operator to a boolean | |
4468 | // value. | |
e440a328 | 4469 | |
4470 | bool | |
0c77715b | 4471 | Binary_expression::cmp_to_bool(Operator op, int cmp) |
e440a328 | 4472 | { |
e440a328 | 4473 | switch (op) |
4474 | { | |
4475 | case OPERATOR_EQEQ: | |
0c77715b | 4476 | return cmp == 0; |
4477 | break; | |
e440a328 | 4478 | case OPERATOR_NOTEQ: |
0c77715b | 4479 | return cmp != 0; |
4480 | break; | |
e440a328 | 4481 | case OPERATOR_LT: |
0c77715b | 4482 | return cmp < 0; |
4483 | break; | |
e440a328 | 4484 | case OPERATOR_LE: |
0c77715b | 4485 | return cmp <= 0; |
e440a328 | 4486 | case OPERATOR_GT: |
0c77715b | 4487 | return cmp > 0; |
e440a328 | 4488 | case OPERATOR_GE: |
0c77715b | 4489 | return cmp >= 0; |
e440a328 | 4490 | default: |
c3e6f413 | 4491 | go_unreachable(); |
e440a328 | 4492 | } |
4493 | } | |
4494 | ||
0c77715b | 4495 | // Compare constants according to OP. |
e440a328 | 4496 | |
4497 | bool | |
0c77715b | 4498 | Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc, |
4499 | Numeric_constant* right_nc, | |
4500 | Location location, bool* result) | |
e440a328 | 4501 | { |
0c77715b | 4502 | Type* left_type = left_nc->type(); |
4503 | Type* right_type = right_nc->type(); | |
4504 | ||
4505 | Type* type; | |
4506 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4507 | return false; | |
4508 | ||
4509 | // When comparing an untyped operand to a typed operand, we are | |
4510 | // effectively coercing the untyped operand to the other operand's | |
4511 | // type, so make sure that is valid. | |
4512 | if (!left_nc->set_type(type, true, location) | |
4513 | || !right_nc->set_type(type, true, location)) | |
4514 | return false; | |
4515 | ||
4516 | bool ret; | |
4517 | int cmp; | |
4518 | if (type->complex_type() != NULL) | |
4519 | { | |
4520 | if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ) | |
4521 | return false; | |
4522 | ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp); | |
4523 | } | |
4524 | else if (type->float_type() != NULL) | |
4525 | ret = Binary_expression::compare_float(left_nc, right_nc, &cmp); | |
e440a328 | 4526 | else |
0c77715b | 4527 | ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp); |
4528 | ||
4529 | if (ret) | |
4530 | *result = Binary_expression::cmp_to_bool(op, cmp); | |
4531 | ||
4532 | return ret; | |
4533 | } | |
4534 | ||
4535 | // Compare integer constants. | |
4536 | ||
4537 | bool | |
4538 | Binary_expression::compare_integer(const Numeric_constant* left_nc, | |
4539 | const Numeric_constant* right_nc, | |
4540 | int* cmp) | |
4541 | { | |
4542 | mpz_t left_val; | |
4543 | if (!left_nc->to_int(&left_val)) | |
4544 | return false; | |
4545 | mpz_t right_val; | |
4546 | if (!right_nc->to_int(&right_val)) | |
e440a328 | 4547 | { |
0c77715b | 4548 | mpz_clear(left_val); |
4549 | return false; | |
e440a328 | 4550 | } |
0c77715b | 4551 | |
4552 | *cmp = mpz_cmp(left_val, right_val); | |
4553 | ||
4554 | mpz_clear(left_val); | |
4555 | mpz_clear(right_val); | |
4556 | ||
4557 | return true; | |
4558 | } | |
4559 | ||
4560 | // Compare floating point constants. | |
4561 | ||
4562 | bool | |
4563 | Binary_expression::compare_float(const Numeric_constant* left_nc, | |
4564 | const Numeric_constant* right_nc, | |
4565 | int* cmp) | |
4566 | { | |
4567 | mpfr_t left_val; | |
4568 | if (!left_nc->to_float(&left_val)) | |
4569 | return false; | |
4570 | mpfr_t right_val; | |
4571 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4572 | { |
0c77715b | 4573 | mpfr_clear(left_val); |
4574 | return false; | |
4575 | } | |
4576 | ||
4577 | // We already coerced both operands to the same type. If that type | |
4578 | // is not an abstract type, we need to round the values accordingly. | |
4579 | Type* type = left_nc->type(); | |
4580 | if (!type->is_abstract() && type->float_type() != NULL) | |
4581 | { | |
4582 | int bits = type->float_type()->bits(); | |
4583 | mpfr_prec_round(left_val, bits, GMP_RNDN); | |
4584 | mpfr_prec_round(right_val, bits, GMP_RNDN); | |
e440a328 | 4585 | } |
0c77715b | 4586 | |
4587 | *cmp = mpfr_cmp(left_val, right_val); | |
4588 | ||
4589 | mpfr_clear(left_val); | |
4590 | mpfr_clear(right_val); | |
4591 | ||
4592 | return true; | |
e440a328 | 4593 | } |
4594 | ||
0c77715b | 4595 | // Compare complex constants. Complex numbers may only be compared |
4596 | // for equality. | |
e440a328 | 4597 | |
4598 | bool | |
0c77715b | 4599 | Binary_expression::compare_complex(const Numeric_constant* left_nc, |
4600 | const Numeric_constant* right_nc, | |
4601 | int* cmp) | |
e440a328 | 4602 | { |
0c77715b | 4603 | mpfr_t left_real, left_imag; |
4604 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4605 | return false; | |
4606 | mpfr_t right_real, right_imag; | |
4607 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4608 | { |
0c77715b | 4609 | mpfr_clear(left_real); |
4610 | mpfr_clear(left_imag); | |
4611 | return false; | |
e440a328 | 4612 | } |
0c77715b | 4613 | |
4614 | // We already coerced both operands to the same type. If that type | |
4615 | // is not an abstract type, we need to round the values accordingly. | |
4616 | Type* type = left_nc->type(); | |
4617 | if (!type->is_abstract() && type->complex_type() != NULL) | |
e440a328 | 4618 | { |
0c77715b | 4619 | int bits = type->complex_type()->bits(); |
4620 | mpfr_prec_round(left_real, bits / 2, GMP_RNDN); | |
4621 | mpfr_prec_round(left_imag, bits / 2, GMP_RNDN); | |
4622 | mpfr_prec_round(right_real, bits / 2, GMP_RNDN); | |
4623 | mpfr_prec_round(right_imag, bits / 2, GMP_RNDN); | |
e440a328 | 4624 | } |
0c77715b | 4625 | |
4626 | *cmp = (mpfr_cmp(left_real, right_real) != 0 | |
4627 | || mpfr_cmp(left_imag, right_imag) != 0); | |
4628 | ||
4629 | mpfr_clear(left_real); | |
4630 | mpfr_clear(left_imag); | |
4631 | mpfr_clear(right_real); | |
4632 | mpfr_clear(right_imag); | |
4633 | ||
4634 | return true; | |
e440a328 | 4635 | } |
4636 | ||
0c77715b | 4637 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return |
4638 | // true if this could be done, false if not. Issue errors at LOCATION | |
4639 | // as appropriate. | |
e440a328 | 4640 | |
4641 | bool | |
0c77715b | 4642 | Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc, |
4643 | Numeric_constant* right_nc, | |
4644 | Location location, Numeric_constant* nc) | |
e440a328 | 4645 | { |
e440a328 | 4646 | switch (op) |
4647 | { | |
4648 | case OPERATOR_OROR: | |
4649 | case OPERATOR_ANDAND: | |
4650 | case OPERATOR_EQEQ: | |
4651 | case OPERATOR_NOTEQ: | |
4652 | case OPERATOR_LT: | |
4653 | case OPERATOR_LE: | |
4654 | case OPERATOR_GT: | |
4655 | case OPERATOR_GE: | |
9767e2d3 | 4656 | // These return boolean values, not numeric. |
4657 | return false; | |
0c77715b | 4658 | default: |
4659 | break; | |
4660 | } | |
4661 | ||
4662 | Type* left_type = left_nc->type(); | |
4663 | Type* right_type = right_nc->type(); | |
4664 | ||
4665 | Type* type; | |
4666 | if (!Binary_expression::operation_type(op, left_type, right_type, &type)) | |
4667 | return false; | |
4668 | ||
4669 | bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT; | |
4670 | ||
4671 | // When combining an untyped operand with a typed operand, we are | |
4672 | // effectively coercing the untyped operand to the other operand's | |
4673 | // type, so make sure that is valid. | |
4674 | if (!left_nc->set_type(type, true, location)) | |
4675 | return false; | |
4676 | if (!is_shift && !right_nc->set_type(type, true, location)) | |
4677 | return false; | |
4678 | ||
4679 | bool r; | |
4680 | if (type->complex_type() != NULL) | |
4681 | r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc); | |
4682 | else if (type->float_type() != NULL) | |
4683 | r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc); | |
4684 | else | |
4685 | r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc); | |
4686 | ||
4687 | if (r) | |
4688 | r = nc->set_type(type, true, location); | |
4689 | ||
4690 | return r; | |
4691 | } | |
4692 | ||
4693 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using | |
4694 | // integer operations. Return true if this could be done, false if | |
4695 | // not. | |
4696 | ||
4697 | bool | |
4698 | Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc, | |
4699 | const Numeric_constant* right_nc, | |
4700 | Location location, Numeric_constant* nc) | |
4701 | { | |
4702 | mpz_t left_val; | |
4703 | if (!left_nc->to_int(&left_val)) | |
4704 | return false; | |
4705 | mpz_t right_val; | |
4706 | if (!right_nc->to_int(&right_val)) | |
4707 | { | |
4708 | mpz_clear(left_val); | |
e440a328 | 4709 | return false; |
0c77715b | 4710 | } |
4711 | ||
4712 | mpz_t val; | |
4713 | mpz_init(val); | |
4714 | ||
4715 | switch (op) | |
4716 | { | |
e440a328 | 4717 | case OPERATOR_PLUS: |
4718 | mpz_add(val, left_val, right_val); | |
4719 | break; | |
4720 | case OPERATOR_MINUS: | |
4721 | mpz_sub(val, left_val, right_val); | |
4722 | break; | |
4723 | case OPERATOR_OR: | |
4724 | mpz_ior(val, left_val, right_val); | |
4725 | break; | |
4726 | case OPERATOR_XOR: | |
4727 | mpz_xor(val, left_val, right_val); | |
4728 | break; | |
4729 | case OPERATOR_MULT: | |
4730 | mpz_mul(val, left_val, right_val); | |
4731 | break; | |
4732 | case OPERATOR_DIV: | |
4733 | if (mpz_sgn(right_val) != 0) | |
4734 | mpz_tdiv_q(val, left_val, right_val); | |
4735 | else | |
4736 | { | |
4737 | error_at(location, "division by zero"); | |
4738 | mpz_set_ui(val, 0); | |
e440a328 | 4739 | } |
4740 | break; | |
4741 | case OPERATOR_MOD: | |
4742 | if (mpz_sgn(right_val) != 0) | |
4743 | mpz_tdiv_r(val, left_val, right_val); | |
4744 | else | |
4745 | { | |
4746 | error_at(location, "division by zero"); | |
4747 | mpz_set_ui(val, 0); | |
e440a328 | 4748 | } |
4749 | break; | |
4750 | case OPERATOR_LSHIFT: | |
4751 | { | |
4752 | unsigned long shift = mpz_get_ui(right_val); | |
0c77715b | 4753 | if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000) |
4754 | mpz_mul_2exp(val, left_val, shift); | |
4755 | else | |
e440a328 | 4756 | { |
4757 | error_at(location, "shift count overflow"); | |
4758 | mpz_set_ui(val, 0); | |
e440a328 | 4759 | } |
e440a328 | 4760 | break; |
4761 | } | |
4762 | break; | |
4763 | case OPERATOR_RSHIFT: | |
4764 | { | |
4765 | unsigned long shift = mpz_get_ui(right_val); | |
4766 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4767 | { | |
4768 | error_at(location, "shift count overflow"); | |
4769 | mpz_set_ui(val, 0); | |
e440a328 | 4770 | } |
e440a328 | 4771 | else |
0c77715b | 4772 | { |
4773 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4774 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4775 | else | |
4776 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4777 | } | |
e440a328 | 4778 | break; |
4779 | } | |
4780 | break; | |
4781 | case OPERATOR_AND: | |
4782 | mpz_and(val, left_val, right_val); | |
4783 | break; | |
4784 | case OPERATOR_BITCLEAR: | |
4785 | { | |
4786 | mpz_t tval; | |
4787 | mpz_init(tval); | |
4788 | mpz_com(tval, right_val); | |
4789 | mpz_and(val, left_val, tval); | |
4790 | mpz_clear(tval); | |
4791 | } | |
4792 | break; | |
4793 | default: | |
c3e6f413 | 4794 | go_unreachable(); |
e440a328 | 4795 | } |
4796 | ||
0c77715b | 4797 | mpz_clear(left_val); |
4798 | mpz_clear(right_val); | |
e440a328 | 4799 | |
0c77715b | 4800 | if (left_nc->is_rune() |
4801 | || (op != OPERATOR_LSHIFT | |
4802 | && op != OPERATOR_RSHIFT | |
4803 | && right_nc->is_rune())) | |
4804 | nc->set_rune(NULL, val); | |
4805 | else | |
4806 | nc->set_int(NULL, val); | |
4807 | ||
4808 | mpz_clear(val); | |
e440a328 | 4809 | |
4810 | return true; | |
4811 | } | |
4812 | ||
0c77715b | 4813 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4814 | // floating point operations. Return true if this could be done, | |
4815 | // false if not. | |
e440a328 | 4816 | |
4817 | bool | |
0c77715b | 4818 | Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc, |
4819 | const Numeric_constant* right_nc, | |
4820 | Location location, Numeric_constant* nc) | |
e440a328 | 4821 | { |
0c77715b | 4822 | mpfr_t left_val; |
4823 | if (!left_nc->to_float(&left_val)) | |
4824 | return false; | |
4825 | mpfr_t right_val; | |
4826 | if (!right_nc->to_float(&right_val)) | |
e440a328 | 4827 | { |
0c77715b | 4828 | mpfr_clear(left_val); |
e440a328 | 4829 | return false; |
0c77715b | 4830 | } |
4831 | ||
4832 | mpfr_t val; | |
4833 | mpfr_init(val); | |
4834 | ||
4835 | bool ret = true; | |
4836 | switch (op) | |
4837 | { | |
e440a328 | 4838 | case OPERATOR_PLUS: |
4839 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4840 | break; | |
4841 | case OPERATOR_MINUS: | |
4842 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4843 | break; | |
4844 | case OPERATOR_OR: | |
4845 | case OPERATOR_XOR: | |
4846 | case OPERATOR_AND: | |
4847 | case OPERATOR_BITCLEAR: | |
0c77715b | 4848 | case OPERATOR_MOD: |
4849 | case OPERATOR_LSHIFT: | |
4850 | case OPERATOR_RSHIFT: | |
4851 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4852 | ret = false; | |
4853 | break; | |
e440a328 | 4854 | case OPERATOR_MULT: |
4855 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4856 | break; | |
4857 | case OPERATOR_DIV: | |
0c77715b | 4858 | if (!mpfr_zero_p(right_val)) |
4859 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4860 | else | |
4861 | { | |
4862 | error_at(location, "division by zero"); | |
4863 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4864 | } | |
e440a328 | 4865 | break; |
e440a328 | 4866 | default: |
c3e6f413 | 4867 | go_unreachable(); |
e440a328 | 4868 | } |
4869 | ||
0c77715b | 4870 | mpfr_clear(left_val); |
4871 | mpfr_clear(right_val); | |
e440a328 | 4872 | |
0c77715b | 4873 | nc->set_float(NULL, val); |
4874 | mpfr_clear(val); | |
e440a328 | 4875 | |
0c77715b | 4876 | return ret; |
e440a328 | 4877 | } |
4878 | ||
0c77715b | 4879 | // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using |
4880 | // complex operations. Return true if this could be done, false if | |
4881 | // not. | |
e440a328 | 4882 | |
4883 | bool | |
0c77715b | 4884 | Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc, |
4885 | const Numeric_constant* right_nc, | |
4886 | Location location, Numeric_constant* nc) | |
e440a328 | 4887 | { |
0c77715b | 4888 | mpfr_t left_real, left_imag; |
4889 | if (!left_nc->to_complex(&left_real, &left_imag)) | |
4890 | return false; | |
4891 | mpfr_t right_real, right_imag; | |
4892 | if (!right_nc->to_complex(&right_real, &right_imag)) | |
e440a328 | 4893 | { |
0c77715b | 4894 | mpfr_clear(left_real); |
4895 | mpfr_clear(left_imag); | |
e440a328 | 4896 | return false; |
0c77715b | 4897 | } |
4898 | ||
4899 | mpfr_t real, imag; | |
4900 | mpfr_init(real); | |
4901 | mpfr_init(imag); | |
4902 | ||
4903 | bool ret = true; | |
4904 | switch (op) | |
4905 | { | |
e440a328 | 4906 | case OPERATOR_PLUS: |
4907 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4908 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4909 | break; | |
4910 | case OPERATOR_MINUS: | |
4911 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4912 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4913 | break; | |
4914 | case OPERATOR_OR: | |
4915 | case OPERATOR_XOR: | |
4916 | case OPERATOR_AND: | |
4917 | case OPERATOR_BITCLEAR: | |
0c77715b | 4918 | case OPERATOR_MOD: |
4919 | case OPERATOR_LSHIFT: | |
4920 | case OPERATOR_RSHIFT: | |
4921 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4922 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4923 | ret = false; | |
4924 | break; | |
e440a328 | 4925 | case OPERATOR_MULT: |
4926 | { | |
4927 | // You might think that multiplying two complex numbers would | |
4928 | // be simple, and you would be right, until you start to think | |
4929 | // about getting the right answer for infinity. If one | |
4930 | // operand here is infinity and the other is anything other | |
4931 | // than zero or NaN, then we are going to wind up subtracting | |
4932 | // two infinity values. That will give us a NaN, but the | |
4933 | // correct answer is infinity. | |
4934 | ||
4935 | mpfr_t lrrr; | |
4936 | mpfr_init(lrrr); | |
4937 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4938 | ||
4939 | mpfr_t lrri; | |
4940 | mpfr_init(lrri); | |
4941 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4942 | ||
4943 | mpfr_t lirr; | |
4944 | mpfr_init(lirr); | |
4945 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4946 | ||
4947 | mpfr_t liri; | |
4948 | mpfr_init(liri); | |
4949 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4950 | ||
4951 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4952 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4953 | ||
4954 | // If we get NaN on both sides, check whether it should really | |
4955 | // be infinity. The rule is that if either side of the | |
4956 | // complex number is infinity, then the whole value is | |
4957 | // infinity, even if the other side is NaN. So the only case | |
4958 | // we have to fix is the one in which both sides are NaN. | |
4959 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4960 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4961 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4962 | { | |
4963 | bool is_infinity = false; | |
4964 | ||
4965 | mpfr_t lr; | |
4966 | mpfr_t li; | |
4967 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4968 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4969 | ||
4970 | mpfr_t rr; | |
4971 | mpfr_t ri; | |
4972 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4973 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4974 | ||
4975 | // If the left side is infinity, then the result is | |
4976 | // infinity. | |
4977 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4978 | { | |
4979 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4980 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4981 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4982 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4983 | if (mpfr_nan_p(rr)) | |
4984 | { | |
4985 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4986 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4987 | } | |
4988 | if (mpfr_nan_p(ri)) | |
4989 | { | |
4990 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4991 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4992 | } | |
4993 | is_infinity = true; | |
4994 | } | |
4995 | ||
4996 | // If the right side is infinity, then the result is | |
4997 | // infinity. | |
4998 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4999 | { | |
5000 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
5001 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
5002 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5003 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
5004 | if (mpfr_nan_p(lr)) | |
5005 | { | |
5006 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
5007 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
5008 | } | |
5009 | if (mpfr_nan_p(li)) | |
5010 | { | |
5011 | mpfr_set_ui(li, 0, GMP_RNDN); | |
5012 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
5013 | } | |
5014 | is_infinity = true; | |
5015 | } | |
5016 | ||
5017 | // If we got an overflow in the intermediate computations, | |
5018 | // then the result is infinity. | |
5019 | if (!is_infinity | |
5020 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
5021 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
5022 | { | |
5023 | if (mpfr_nan_p(lr)) | |
5024 | { | |
5025 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
5026 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
5027 | } | |
5028 | if (mpfr_nan_p(li)) | |
5029 | { | |
5030 | mpfr_set_ui(li, 0, GMP_RNDN); | |
5031 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
5032 | } | |
5033 | if (mpfr_nan_p(rr)) | |
5034 | { | |
5035 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
5036 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
5037 | } | |
5038 | if (mpfr_nan_p(ri)) | |
5039 | { | |
5040 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
5041 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
5042 | } | |
5043 | is_infinity = true; | |
5044 | } | |
5045 | ||
5046 | if (is_infinity) | |
5047 | { | |
5048 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
5049 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
5050 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
5051 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
5052 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
5053 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
5054 | mpfr_set_inf(real, mpfr_sgn(real)); | |
5055 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
5056 | } | |
5057 | ||
5058 | mpfr_clear(lr); | |
5059 | mpfr_clear(li); | |
5060 | mpfr_clear(rr); | |
5061 | mpfr_clear(ri); | |
5062 | } | |
5063 | ||
5064 | mpfr_clear(lrrr); | |
5065 | mpfr_clear(lrri); | |
5066 | mpfr_clear(lirr); | |
5067 | mpfr_clear(liri); | |
5068 | } | |
5069 | break; | |
5070 | case OPERATOR_DIV: | |
5071 | { | |
5072 | // For complex division we want to avoid having an | |
5073 | // intermediate overflow turn the whole result in a NaN. We | |
5074 | // scale the values to try to avoid this. | |
5075 | ||
5076 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
0c77715b | 5077 | { |
5078 | error_at(location, "division by zero"); | |
5079 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5080 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5081 | break; | |
5082 | } | |
e440a328 | 5083 | |
5084 | mpfr_t rra; | |
5085 | mpfr_t ria; | |
5086 | mpfr_init(rra); | |
5087 | mpfr_init(ria); | |
5088 | mpfr_abs(rra, right_real, GMP_RNDN); | |
5089 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
5090 | mpfr_t t; | |
5091 | mpfr_init(t); | |
5092 | mpfr_max(t, rra, ria, GMP_RNDN); | |
5093 | ||
5094 | mpfr_t rr; | |
5095 | mpfr_t ri; | |
5096 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
5097 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
5098 | long ilogbw = 0; | |
5099 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
5100 | { | |
5101 | ilogbw = mpfr_get_exp(t); | |
5102 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
5103 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
5104 | } | |
5105 | ||
5106 | mpfr_t denom; | |
5107 | mpfr_init(denom); | |
5108 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
5109 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
5110 | mpfr_add(denom, denom, t, GMP_RNDN); | |
5111 | ||
5112 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
5113 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
5114 | mpfr_add(real, real, t, GMP_RNDN); | |
5115 | mpfr_div(real, real, denom, GMP_RNDN); | |
5116 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
5117 | ||
5118 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
5119 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
5120 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
5121 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
5122 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
5123 | ||
5124 | // If we wind up with NaN on both sides, check whether we | |
5125 | // should really have infinity. The rule is that if either | |
5126 | // side of the complex number is infinity, then the whole | |
5127 | // value is infinity, even if the other side is NaN. So the | |
5128 | // only case we have to fix is the one in which both sides are | |
5129 | // NaN. | |
5130 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
5131 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
5132 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
5133 | { | |
5134 | if (mpfr_zero_p(denom)) | |
5135 | { | |
5136 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
5137 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
5138 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
5139 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
5140 | } | |
5141 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
5142 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
5143 | { | |
5144 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
5145 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
5146 | ||
5147 | mpfr_t t2; | |
5148 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
5149 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
5150 | ||
5151 | mpfr_t t3; | |
5152 | mpfr_init(t3); | |
5153 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
5154 | ||
5155 | mpfr_t t4; | |
5156 | mpfr_init(t4); | |
5157 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
5158 | ||
5159 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5160 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
5161 | ||
5162 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
5163 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
5164 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5165 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
5166 | ||
5167 | mpfr_clear(t2); | |
5168 | mpfr_clear(t3); | |
5169 | mpfr_clear(t4); | |
5170 | } | |
5171 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
5172 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
5173 | { | |
5174 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
5175 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
5176 | ||
5177 | mpfr_t t2; | |
5178 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5179 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
5180 | ||
5181 | mpfr_t t3; | |
5182 | mpfr_init(t3); | |
5183 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
5184 | ||
5185 | mpfr_t t4; | |
5186 | mpfr_init(t4); | |
5187 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
5188 | ||
5189 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5190 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5191 | mpfr_mul(real, real, t3, GMP_RNDN); | |
5192 | ||
5193 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
5194 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
5195 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5196 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5197 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
5198 | ||
5199 | mpfr_clear(t2); | |
5200 | mpfr_clear(t3); | |
5201 | mpfr_clear(t4); | |
5202 | } | |
5203 | } | |
5204 | ||
5205 | mpfr_clear(denom); | |
5206 | mpfr_clear(rr); | |
5207 | mpfr_clear(ri); | |
5208 | mpfr_clear(t); | |
5209 | mpfr_clear(rra); | |
5210 | mpfr_clear(ria); | |
5211 | } | |
5212 | break; | |
e440a328 | 5213 | default: |
c3e6f413 | 5214 | go_unreachable(); |
e440a328 | 5215 | } |
5216 | ||
0c77715b | 5217 | mpfr_clear(left_real); |
5218 | mpfr_clear(left_imag); | |
5219 | mpfr_clear(right_real); | |
5220 | mpfr_clear(right_imag); | |
e440a328 | 5221 | |
0c77715b | 5222 | nc->set_complex(NULL, real, imag); |
5223 | mpfr_clear(real); | |
5224 | mpfr_clear(imag); | |
e440a328 | 5225 | |
0c77715b | 5226 | return ret; |
e440a328 | 5227 | } |
5228 | ||
5229 | // Lower a binary expression. We have to evaluate constant | |
5230 | // expressions now, in order to implement Go's unlimited precision | |
5231 | // constants. | |
5232 | ||
5233 | Expression* | |
e9d3367e | 5234 | Binary_expression::do_lower(Gogo* gogo, Named_object*, |
5235 | Statement_inserter* inserter, int) | |
e440a328 | 5236 | { |
b13c66cd | 5237 | Location location = this->location(); |
e440a328 | 5238 | Operator op = this->op_; |
5239 | Expression* left = this->left_; | |
5240 | Expression* right = this->right_; | |
5241 | ||
5242 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5243 | || op == OPERATOR_NOTEQ | |
5244 | || op == OPERATOR_LT | |
5245 | || op == OPERATOR_LE | |
5246 | || op == OPERATOR_GT | |
5247 | || op == OPERATOR_GE); | |
5248 | ||
0c77715b | 5249 | // Numeric constant expressions. |
e440a328 | 5250 | { |
0c77715b | 5251 | Numeric_constant left_nc; |
5252 | Numeric_constant right_nc; | |
5253 | if (left->numeric_constant_value(&left_nc) | |
5254 | && right->numeric_constant_value(&right_nc)) | |
e440a328 | 5255 | { |
0c77715b | 5256 | if (is_comparison) |
e440a328 | 5257 | { |
0c77715b | 5258 | bool result; |
5259 | if (!Binary_expression::compare_constant(op, &left_nc, | |
5260 | &right_nc, location, | |
5261 | &result)) | |
5262 | return this; | |
e90c9dfc | 5263 | return Expression::make_cast(Type::make_boolean_type(), |
0c77715b | 5264 | Expression::make_boolean(result, |
5265 | location), | |
5266 | location); | |
e440a328 | 5267 | } |
5268 | else | |
5269 | { | |
0c77715b | 5270 | Numeric_constant nc; |
5271 | if (!Binary_expression::eval_constant(op, &left_nc, &right_nc, | |
5272 | location, &nc)) | |
5273 | return this; | |
5274 | return nc.expression(location); | |
e440a328 | 5275 | } |
5276 | } | |
e440a328 | 5277 | } |
5278 | ||
5279 | // String constant expressions. | |
315fa98d | 5280 | if (left->type()->is_string_type() && right->type()->is_string_type()) |
e440a328 | 5281 | { |
5282 | std::string left_string; | |
5283 | std::string right_string; | |
5284 | if (left->string_constant_value(&left_string) | |
5285 | && right->string_constant_value(&right_string)) | |
315fa98d | 5286 | { |
5287 | if (op == OPERATOR_PLUS) | |
5288 | return Expression::make_string(left_string + right_string, | |
5289 | location); | |
5290 | else if (is_comparison) | |
5291 | { | |
5292 | int cmp = left_string.compare(right_string); | |
0c77715b | 5293 | bool r = Binary_expression::cmp_to_bool(op, cmp); |
e90c9dfc | 5294 | return Expression::make_boolean(r, location); |
b40dc774 | 5295 | } |
5296 | } | |
b40dc774 | 5297 | } |
5298 | ||
e9d3367e | 5299 | // Lower struct and array comparisons. |
5300 | if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) | |
5301 | { | |
5302 | if (left->type()->struct_type() != NULL) | |
5303 | return this->lower_struct_comparison(gogo, inserter); | |
5304 | else if (left->type()->array_type() != NULL | |
5305 | && !left->type()->is_slice_type()) | |
5306 | return this->lower_array_comparison(gogo, inserter); | |
5307 | } | |
5308 | ||
e440a328 | 5309 | return this; |
5310 | } | |
5311 | ||
e9d3367e | 5312 | // Lower a struct comparison. |
5313 | ||
5314 | Expression* | |
5315 | Binary_expression::lower_struct_comparison(Gogo* gogo, | |
5316 | Statement_inserter* inserter) | |
5317 | { | |
5318 | Struct_type* st = this->left_->type()->struct_type(); | |
5319 | Struct_type* st2 = this->right_->type()->struct_type(); | |
5320 | if (st2 == NULL) | |
5321 | return this; | |
5322 | if (st != st2 && !Type::are_identical(st, st2, false, NULL)) | |
5323 | return this; | |
5324 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5325 | this->right_->type(), NULL)) | |
5326 | return this; | |
5327 | ||
5328 | // See if we can compare using memcmp. As a heuristic, we use | |
5329 | // memcmp rather than field references and comparisons if there are | |
5330 | // more than two fields. | |
113ef6a5 | 5331 | if (st->compare_is_identity(gogo) && st->total_field_count() > 2) |
e9d3367e | 5332 | return this->lower_compare_to_memcmp(gogo, inserter); |
5333 | ||
5334 | Location loc = this->location(); | |
5335 | ||
5336 | Expression* left = this->left_; | |
5337 | Temporary_statement* left_temp = NULL; | |
5338 | if (left->var_expression() == NULL | |
5339 | && left->temporary_reference_expression() == NULL) | |
5340 | { | |
5341 | left_temp = Statement::make_temporary(left->type(), NULL, loc); | |
5342 | inserter->insert(left_temp); | |
5343 | left = Expression::make_set_and_use_temporary(left_temp, left, loc); | |
5344 | } | |
5345 | ||
5346 | Expression* right = this->right_; | |
5347 | Temporary_statement* right_temp = NULL; | |
5348 | if (right->var_expression() == NULL | |
5349 | && right->temporary_reference_expression() == NULL) | |
5350 | { | |
5351 | right_temp = Statement::make_temporary(right->type(), NULL, loc); | |
5352 | inserter->insert(right_temp); | |
5353 | right = Expression::make_set_and_use_temporary(right_temp, right, loc); | |
5354 | } | |
5355 | ||
5356 | Expression* ret = Expression::make_boolean(true, loc); | |
5357 | const Struct_field_list* fields = st->fields(); | |
5358 | unsigned int field_index = 0; | |
5359 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
5360 | pf != fields->end(); | |
5361 | ++pf, ++field_index) | |
5362 | { | |
f5165c05 | 5363 | if (Gogo::is_sink_name(pf->field_name())) |
5364 | continue; | |
5365 | ||
e9d3367e | 5366 | if (field_index > 0) |
5367 | { | |
5368 | if (left_temp == NULL) | |
5369 | left = left->copy(); | |
5370 | else | |
5371 | left = Expression::make_temporary_reference(left_temp, loc); | |
5372 | if (right_temp == NULL) | |
5373 | right = right->copy(); | |
5374 | else | |
5375 | right = Expression::make_temporary_reference(right_temp, loc); | |
5376 | } | |
5377 | Expression* f1 = Expression::make_field_reference(left, field_index, | |
5378 | loc); | |
5379 | Expression* f2 = Expression::make_field_reference(right, field_index, | |
5380 | loc); | |
5381 | Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc); | |
5382 | ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc); | |
5383 | } | |
5384 | ||
5385 | if (this->op_ == OPERATOR_NOTEQ) | |
5386 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5387 | ||
5388 | return ret; | |
5389 | } | |
5390 | ||
5391 | // Lower an array comparison. | |
5392 | ||
5393 | Expression* | |
5394 | Binary_expression::lower_array_comparison(Gogo* gogo, | |
5395 | Statement_inserter* inserter) | |
5396 | { | |
5397 | Array_type* at = this->left_->type()->array_type(); | |
5398 | Array_type* at2 = this->right_->type()->array_type(); | |
5399 | if (at2 == NULL) | |
5400 | return this; | |
5401 | if (at != at2 && !Type::are_identical(at, at2, false, NULL)) | |
5402 | return this; | |
5403 | if (!Type::are_compatible_for_comparison(true, this->left_->type(), | |
5404 | this->right_->type(), NULL)) | |
5405 | return this; | |
5406 | ||
5407 | // Call memcmp directly if possible. This may let the middle-end | |
5408 | // optimize the call. | |
113ef6a5 | 5409 | if (at->compare_is_identity(gogo)) |
e9d3367e | 5410 | return this->lower_compare_to_memcmp(gogo, inserter); |
5411 | ||
5412 | // Call the array comparison function. | |
5413 | Named_object* hash_fn; | |
5414 | Named_object* equal_fn; | |
5415 | at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL, | |
5416 | &hash_fn, &equal_fn); | |
5417 | ||
5418 | Location loc = this->location(); | |
5419 | ||
5420 | Expression* func = Expression::make_func_reference(equal_fn, NULL, loc); | |
5421 | ||
5422 | Expression_list* args = new Expression_list(); | |
5423 | args->push_back(this->operand_address(inserter, this->left_)); | |
5424 | args->push_back(this->operand_address(inserter, this->right_)); | |
5425 | args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE)); | |
5426 | ||
5427 | Expression* ret = Expression::make_call(func, args, false, loc); | |
5428 | ||
5429 | if (this->op_ == OPERATOR_NOTEQ) | |
5430 | ret = Expression::make_unary(OPERATOR_NOT, ret, loc); | |
5431 | ||
5432 | return ret; | |
5433 | } | |
5434 | ||
5435 | // Lower a struct or array comparison to a call to memcmp. | |
5436 | ||
5437 | Expression* | |
5438 | Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter) | |
5439 | { | |
5440 | Location loc = this->location(); | |
5441 | ||
5442 | Expression* a1 = this->operand_address(inserter, this->left_); | |
5443 | Expression* a2 = this->operand_address(inserter, this->right_); | |
5444 | Expression* len = Expression::make_type_info(this->left_->type(), | |
5445 | TYPE_INFO_SIZE); | |
5446 | ||
5447 | Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len); | |
5448 | ||
5449 | mpz_t zval; | |
5450 | mpz_init_set_ui(zval, 0); | |
5451 | Expression* zero = Expression::make_integer(&zval, NULL, loc); | |
5452 | mpz_clear(zval); | |
5453 | ||
5454 | return Expression::make_binary(this->op_, call, zero, loc); | |
5455 | } | |
5456 | ||
5457 | // Return the address of EXPR, cast to unsafe.Pointer. | |
5458 | ||
5459 | Expression* | |
5460 | Binary_expression::operand_address(Statement_inserter* inserter, | |
5461 | Expression* expr) | |
5462 | { | |
5463 | Location loc = this->location(); | |
5464 | ||
5465 | if (!expr->is_addressable()) | |
5466 | { | |
5467 | Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL, | |
5468 | loc); | |
5469 | inserter->insert(temp); | |
5470 | expr = Expression::make_set_and_use_temporary(temp, expr, loc); | |
5471 | } | |
5472 | expr = Expression::make_unary(OPERATOR_AND, expr, loc); | |
5473 | static_cast<Unary_expression*>(expr)->set_does_not_escape(); | |
5474 | Type* void_type = Type::make_void_type(); | |
5475 | Type* unsafe_pointer_type = Type::make_pointer_type(void_type); | |
5476 | return Expression::make_cast(unsafe_pointer_type, expr, loc); | |
5477 | } | |
5478 | ||
0c77715b | 5479 | // Return the numeric constant value, if it has one. |
e440a328 | 5480 | |
5481 | bool | |
0c77715b | 5482 | Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 5483 | { |
0c77715b | 5484 | Numeric_constant left_nc; |
5485 | if (!this->left_->numeric_constant_value(&left_nc)) | |
5486 | return false; | |
5487 | Numeric_constant right_nc; | |
5488 | if (!this->right_->numeric_constant_value(&right_nc)) | |
5489 | return false; | |
9767e2d3 | 5490 | return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc, |
0c77715b | 5491 | this->location(), nc); |
e440a328 | 5492 | } |
5493 | ||
5494 | // Note that the value is being discarded. | |
5495 | ||
4f2138d7 | 5496 | bool |
e440a328 | 5497 | Binary_expression::do_discarding_value() |
5498 | { | |
5499 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
4f2138d7 | 5500 | return this->right_->discarding_value(); |
e440a328 | 5501 | else |
4f2138d7 | 5502 | { |
5503 | this->unused_value_error(); | |
5504 | return false; | |
5505 | } | |
e440a328 | 5506 | } |
5507 | ||
5508 | // Get type. | |
5509 | ||
5510 | Type* | |
5511 | Binary_expression::do_type() | |
5512 | { | |
5f5fea79 | 5513 | if (this->classification() == EXPRESSION_ERROR) |
5514 | return Type::make_error_type(); | |
5515 | ||
e440a328 | 5516 | switch (this->op_) |
5517 | { | |
e440a328 | 5518 | case OPERATOR_EQEQ: |
5519 | case OPERATOR_NOTEQ: | |
5520 | case OPERATOR_LT: | |
5521 | case OPERATOR_LE: | |
5522 | case OPERATOR_GT: | |
5523 | case OPERATOR_GE: | |
e90c9dfc | 5524 | if (this->type_ == NULL) |
5525 | this->type_ = Type::make_boolean_type(); | |
5526 | return this->type_; | |
e440a328 | 5527 | |
5528 | case OPERATOR_PLUS: | |
5529 | case OPERATOR_MINUS: | |
5530 | case OPERATOR_OR: | |
5531 | case OPERATOR_XOR: | |
5532 | case OPERATOR_MULT: | |
5533 | case OPERATOR_DIV: | |
5534 | case OPERATOR_MOD: | |
5535 | case OPERATOR_AND: | |
5536 | case OPERATOR_BITCLEAR: | |
e90c9dfc | 5537 | case OPERATOR_OROR: |
5538 | case OPERATOR_ANDAND: | |
e440a328 | 5539 | { |
0c77715b | 5540 | Type* type; |
5541 | if (!Binary_expression::operation_type(this->op_, | |
5542 | this->left_->type(), | |
5543 | this->right_->type(), | |
5544 | &type)) | |
5545 | return Type::make_error_type(); | |
5546 | return type; | |
e440a328 | 5547 | } |
5548 | ||
5549 | case OPERATOR_LSHIFT: | |
5550 | case OPERATOR_RSHIFT: | |
5551 | return this->left_->type(); | |
5552 | ||
5553 | default: | |
c3e6f413 | 5554 | go_unreachable(); |
e440a328 | 5555 | } |
5556 | } | |
5557 | ||
5558 | // Set type for a binary expression. | |
5559 | ||
5560 | void | |
5561 | Binary_expression::do_determine_type(const Type_context* context) | |
5562 | { | |
5563 | Type* tleft = this->left_->type(); | |
5564 | Type* tright = this->right_->type(); | |
5565 | ||
5566 | // Both sides should have the same type, except for the shift | |
5567 | // operations. For a comparison, we should ignore the incoming | |
5568 | // type. | |
5569 | ||
5570 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5571 | || this->op_ == OPERATOR_RSHIFT); | |
5572 | ||
5573 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5574 | || this->op_ == OPERATOR_NOTEQ | |
5575 | || this->op_ == OPERATOR_LT | |
5576 | || this->op_ == OPERATOR_LE | |
5577 | || this->op_ == OPERATOR_GT | |
5578 | || this->op_ == OPERATOR_GE); | |
5579 | ||
5580 | Type_context subcontext(*context); | |
5581 | ||
5582 | if (is_comparison) | |
5583 | { | |
5584 | // In a comparison, the context does not determine the types of | |
5585 | // the operands. | |
5586 | subcontext.type = NULL; | |
5587 | } | |
5588 | ||
5589 | // Set the context for the left hand operand. | |
5590 | if (is_shift_op) | |
5591 | { | |
b40dc774 | 5592 | // The right hand operand of a shift plays no role in |
5593 | // determining the type of the left hand operand. | |
e440a328 | 5594 | } |
5595 | else if (!tleft->is_abstract()) | |
5596 | subcontext.type = tleft; | |
5597 | else if (!tright->is_abstract()) | |
5598 | subcontext.type = tright; | |
5599 | else if (subcontext.type == NULL) | |
5600 | { | |
5601 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5602 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5603 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5604 | { | |
5605 | // Both sides have an abstract integer, abstract float, or | |
5606 | // abstract complex type. Just let CONTEXT determine | |
5607 | // whether they may remain abstract or not. | |
5608 | } | |
5609 | else if (tleft->complex_type() != NULL) | |
5610 | subcontext.type = tleft; | |
5611 | else if (tright->complex_type() != NULL) | |
5612 | subcontext.type = tright; | |
5613 | else if (tleft->float_type() != NULL) | |
5614 | subcontext.type = tleft; | |
5615 | else if (tright->float_type() != NULL) | |
5616 | subcontext.type = tright; | |
5617 | else | |
5618 | subcontext.type = tleft; | |
f58a23ae | 5619 | |
5620 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5621 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5622 | } |
5623 | ||
5624 | this->left_->determine_type(&subcontext); | |
5625 | ||
e440a328 | 5626 | if (is_shift_op) |
5627 | { | |
b40dc774 | 5628 | // We may have inherited an unusable type for the shift operand. |
5629 | // Give a useful error if that happened. | |
5630 | if (tleft->is_abstract() | |
5631 | && subcontext.type != NULL | |
8ab6effb | 5632 | && !subcontext.may_be_abstract |
f6bc81e6 | 5633 | && subcontext.type->interface_type() == NULL |
8ab6effb | 5634 | && subcontext.type->integer_type() == NULL) |
b40dc774 | 5635 | this->report_error(("invalid context-determined non-integer type " |
8ab6effb | 5636 | "for left operand of shift")); |
b40dc774 | 5637 | |
5638 | // The context for the right hand operand is the same as for the | |
5639 | // left hand operand, except for a shift operator. | |
e440a328 | 5640 | subcontext.type = Type::lookup_integer_type("uint"); |
5641 | subcontext.may_be_abstract = false; | |
5642 | } | |
5643 | ||
5644 | this->right_->determine_type(&subcontext); | |
e90c9dfc | 5645 | |
5646 | if (is_comparison) | |
5647 | { | |
5648 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
5649 | ; | |
5650 | else if (context->type != NULL && context->type->is_boolean_type()) | |
5651 | this->type_ = context->type; | |
5652 | else if (!context->may_be_abstract) | |
5653 | this->type_ = Type::lookup_bool_type(); | |
5654 | } | |
e440a328 | 5655 | } |
5656 | ||
5657 | // Report an error if the binary operator OP does not support TYPE. | |
be8b5eee | 5658 | // OTYPE is the type of the other operand. Return whether the |
5659 | // operation is OK. This should not be used for shift. | |
e440a328 | 5660 | |
5661 | bool | |
be8b5eee | 5662 | Binary_expression::check_operator_type(Operator op, Type* type, Type* otype, |
b13c66cd | 5663 | Location location) |
e440a328 | 5664 | { |
5665 | switch (op) | |
5666 | { | |
5667 | case OPERATOR_OROR: | |
5668 | case OPERATOR_ANDAND: | |
5669 | if (!type->is_boolean_type()) | |
5670 | { | |
5671 | error_at(location, "expected boolean type"); | |
5672 | return false; | |
5673 | } | |
5674 | break; | |
5675 | ||
5676 | case OPERATOR_EQEQ: | |
5677 | case OPERATOR_NOTEQ: | |
e9d3367e | 5678 | { |
5679 | std::string reason; | |
5680 | if (!Type::are_compatible_for_comparison(true, type, otype, &reason)) | |
5681 | { | |
5682 | error_at(location, "%s", reason.c_str()); | |
5683 | return false; | |
5684 | } | |
5685 | } | |
e440a328 | 5686 | break; |
5687 | ||
5688 | case OPERATOR_LT: | |
5689 | case OPERATOR_LE: | |
5690 | case OPERATOR_GT: | |
5691 | case OPERATOR_GE: | |
e9d3367e | 5692 | { |
5693 | std::string reason; | |
5694 | if (!Type::are_compatible_for_comparison(false, type, otype, &reason)) | |
5695 | { | |
5696 | error_at(location, "%s", reason.c_str()); | |
5697 | return false; | |
5698 | } | |
5699 | } | |
e440a328 | 5700 | break; |
5701 | ||
5702 | case OPERATOR_PLUS: | |
5703 | case OPERATOR_PLUSEQ: | |
5704 | if (type->integer_type() == NULL | |
5705 | && type->float_type() == NULL | |
5706 | && type->complex_type() == NULL | |
5707 | && !type->is_string_type()) | |
5708 | { | |
5709 | error_at(location, | |
5710 | "expected integer, floating, complex, or string type"); | |
5711 | return false; | |
5712 | } | |
5713 | break; | |
5714 | ||
5715 | case OPERATOR_MINUS: | |
5716 | case OPERATOR_MINUSEQ: | |
5717 | case OPERATOR_MULT: | |
5718 | case OPERATOR_MULTEQ: | |
5719 | case OPERATOR_DIV: | |
5720 | case OPERATOR_DIVEQ: | |
5721 | if (type->integer_type() == NULL | |
5722 | && type->float_type() == NULL | |
5723 | && type->complex_type() == NULL) | |
5724 | { | |
5725 | error_at(location, "expected integer, floating, or complex type"); | |
5726 | return false; | |
5727 | } | |
5728 | break; | |
5729 | ||
5730 | case OPERATOR_MOD: | |
5731 | case OPERATOR_MODEQ: | |
5732 | case OPERATOR_OR: | |
5733 | case OPERATOR_OREQ: | |
5734 | case OPERATOR_AND: | |
5735 | case OPERATOR_ANDEQ: | |
5736 | case OPERATOR_XOR: | |
5737 | case OPERATOR_XOREQ: | |
5738 | case OPERATOR_BITCLEAR: | |
5739 | case OPERATOR_BITCLEAREQ: | |
5740 | if (type->integer_type() == NULL) | |
5741 | { | |
5742 | error_at(location, "expected integer type"); | |
5743 | return false; | |
5744 | } | |
5745 | break; | |
5746 | ||
5747 | default: | |
c3e6f413 | 5748 | go_unreachable(); |
e440a328 | 5749 | } |
5750 | ||
5751 | return true; | |
5752 | } | |
5753 | ||
5754 | // Check types. | |
5755 | ||
5756 | void | |
5757 | Binary_expression::do_check_types(Gogo*) | |
5758 | { | |
5f5fea79 | 5759 | if (this->classification() == EXPRESSION_ERROR) |
5760 | return; | |
5761 | ||
e440a328 | 5762 | Type* left_type = this->left_->type(); |
5763 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5764 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5765 | { |
5766 | this->set_is_error(); | |
5767 | return; | |
5768 | } | |
e440a328 | 5769 | |
5770 | if (this->op_ == OPERATOR_EQEQ | |
5771 | || this->op_ == OPERATOR_NOTEQ | |
5772 | || this->op_ == OPERATOR_LT | |
5773 | || this->op_ == OPERATOR_LE | |
5774 | || this->op_ == OPERATOR_GT | |
5775 | || this->op_ == OPERATOR_GE) | |
5776 | { | |
907c5ecd | 5777 | if (left_type->is_nil_type() && right_type->is_nil_type()) |
5778 | { | |
5779 | this->report_error(_("invalid comparison of nil with nil")); | |
5780 | return; | |
5781 | } | |
e440a328 | 5782 | if (!Type::are_assignable(left_type, right_type, NULL) |
5783 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5784 | { | |
5785 | this->report_error(_("incompatible types in binary expression")); | |
5786 | return; | |
5787 | } | |
5788 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5789 | right_type, |
e440a328 | 5790 | this->location()) |
5791 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
be8b5eee | 5792 | left_type, |
e440a328 | 5793 | this->location())) |
5794 | { | |
5795 | this->set_is_error(); | |
5796 | return; | |
5797 | } | |
5798 | } | |
5799 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5800 | { | |
5801 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5802 | { | |
5803 | this->report_error(_("incompatible types in binary expression")); | |
5804 | return; | |
5805 | } | |
5806 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
be8b5eee | 5807 | right_type, |
e440a328 | 5808 | this->location())) |
5809 | { | |
5810 | this->set_is_error(); | |
5811 | return; | |
5812 | } | |
5c65b19d | 5813 | if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD) |
5814 | { | |
5815 | // Division by a zero integer constant is an error. | |
5816 | Numeric_constant rconst; | |
5817 | unsigned long rval; | |
5818 | if (left_type->integer_type() != NULL | |
5819 | && this->right_->numeric_constant_value(&rconst) | |
5820 | && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID | |
5821 | && rval == 0) | |
5822 | { | |
5823 | this->report_error(_("integer division by zero")); | |
5824 | return; | |
5825 | } | |
5826 | } | |
e440a328 | 5827 | } |
5828 | else | |
5829 | { | |
5830 | if (left_type->integer_type() == NULL) | |
5831 | this->report_error(_("shift of non-integer operand")); | |
5832 | ||
5833 | if (!right_type->is_abstract() | |
5834 | && (right_type->integer_type() == NULL | |
5835 | || !right_type->integer_type()->is_unsigned())) | |
5836 | this->report_error(_("shift count not unsigned integer")); | |
5837 | else | |
5838 | { | |
0c77715b | 5839 | Numeric_constant nc; |
5840 | if (this->right_->numeric_constant_value(&nc)) | |
e440a328 | 5841 | { |
0c77715b | 5842 | mpz_t val; |
5843 | if (!nc.to_int(&val)) | |
5844 | this->report_error(_("shift count not unsigned integer")); | |
5845 | else | |
a4eba91b | 5846 | { |
0c77715b | 5847 | if (mpz_sgn(val) < 0) |
5848 | { | |
5849 | this->report_error(_("negative shift count")); | |
5850 | mpz_set_ui(val, 0); | |
5851 | Location rloc = this->right_->location(); | |
5852 | this->right_ = Expression::make_integer(&val, right_type, | |
5853 | rloc); | |
5854 | } | |
5855 | mpz_clear(val); | |
a4eba91b | 5856 | } |
e440a328 | 5857 | } |
e440a328 | 5858 | } |
5859 | } | |
5860 | } | |
5861 | ||
5862 | // Get a tree for a binary expression. | |
5863 | ||
5864 | tree | |
5865 | Binary_expression::do_get_tree(Translate_context* context) | |
5866 | { | |
1b1f2abf | 5867 | Gogo* gogo = context->gogo(); |
5868 | ||
e440a328 | 5869 | tree left = this->left_->get_tree(context); |
5870 | tree right = this->right_->get_tree(context); | |
5871 | ||
5872 | if (left == error_mark_node || right == error_mark_node) | |
5873 | return error_mark_node; | |
5874 | ||
5875 | enum tree_code code; | |
5876 | bool use_left_type = true; | |
5877 | bool is_shift_op = false; | |
29a2d1d8 | 5878 | bool is_idiv_op = false; |
e440a328 | 5879 | switch (this->op_) |
5880 | { | |
5881 | case OPERATOR_EQEQ: | |
5882 | case OPERATOR_NOTEQ: | |
5883 | case OPERATOR_LT: | |
5884 | case OPERATOR_LE: | |
5885 | case OPERATOR_GT: | |
5886 | case OPERATOR_GE: | |
e90c9dfc | 5887 | return Expression::comparison_tree(context, this->type_, this->op_, |
e440a328 | 5888 | this->left_->type(), left, |
5889 | this->right_->type(), right, | |
5890 | this->location()); | |
5891 | ||
5892 | case OPERATOR_OROR: | |
5893 | code = TRUTH_ORIF_EXPR; | |
5894 | use_left_type = false; | |
5895 | break; | |
5896 | case OPERATOR_ANDAND: | |
5897 | code = TRUTH_ANDIF_EXPR; | |
5898 | use_left_type = false; | |
5899 | break; | |
5900 | case OPERATOR_PLUS: | |
5901 | code = PLUS_EXPR; | |
5902 | break; | |
5903 | case OPERATOR_MINUS: | |
5904 | code = MINUS_EXPR; | |
5905 | break; | |
5906 | case OPERATOR_OR: | |
5907 | code = BIT_IOR_EXPR; | |
5908 | break; | |
5909 | case OPERATOR_XOR: | |
5910 | code = BIT_XOR_EXPR; | |
5911 | break; | |
5912 | case OPERATOR_MULT: | |
5913 | code = MULT_EXPR; | |
5914 | break; | |
5915 | case OPERATOR_DIV: | |
5916 | { | |
5917 | Type *t = this->left_->type(); | |
5918 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5919 | code = RDIV_EXPR; | |
5920 | else | |
29a2d1d8 | 5921 | { |
5922 | code = TRUNC_DIV_EXPR; | |
5923 | is_idiv_op = true; | |
5924 | } | |
e440a328 | 5925 | } |
5926 | break; | |
5927 | case OPERATOR_MOD: | |
5928 | code = TRUNC_MOD_EXPR; | |
29a2d1d8 | 5929 | is_idiv_op = true; |
e440a328 | 5930 | break; |
5931 | case OPERATOR_LSHIFT: | |
5932 | code = LSHIFT_EXPR; | |
5933 | is_shift_op = true; | |
5934 | break; | |
5935 | case OPERATOR_RSHIFT: | |
5936 | code = RSHIFT_EXPR; | |
5937 | is_shift_op = true; | |
5938 | break; | |
5939 | case OPERATOR_AND: | |
5940 | code = BIT_AND_EXPR; | |
5941 | break; | |
5942 | case OPERATOR_BITCLEAR: | |
5943 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5944 | code = BIT_AND_EXPR; | |
5945 | break; | |
5946 | default: | |
c3e6f413 | 5947 | go_unreachable(); |
e440a328 | 5948 | } |
5949 | ||
29a2d1d8 | 5950 | location_t gccloc = this->location().gcc_location(); |
e440a328 | 5951 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); |
5952 | ||
5953 | if (this->left_->type()->is_string_type()) | |
5954 | { | |
c484d925 | 5955 | go_assert(this->op_ == OPERATOR_PLUS); |
9f0e0513 | 5956 | Type* st = Type::make_string_type(); |
1b1f2abf | 5957 | tree string_type = type_to_tree(st->get_backend(gogo)); |
e440a328 | 5958 | static tree string_plus_decl; |
5959 | return Gogo::call_builtin(&string_plus_decl, | |
5960 | this->location(), | |
5961 | "__go_string_plus", | |
5962 | 2, | |
5963 | string_type, | |
5964 | string_type, | |
5965 | left, | |
5966 | string_type, | |
5967 | right); | |
5968 | } | |
5969 | ||
5970 | tree compute_type = excess_precision_type(type); | |
5971 | if (compute_type != NULL_TREE) | |
5972 | { | |
5973 | left = ::convert(compute_type, left); | |
5974 | right = ::convert(compute_type, right); | |
5975 | } | |
5976 | ||
5977 | tree eval_saved = NULL_TREE; | |
29a2d1d8 | 5978 | if (is_shift_op |
5979 | || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow))) | |
e440a328 | 5980 | { |
e440a328 | 5981 | // Make sure the values are evaluated. |
29a2d1d8 | 5982 | if (!DECL_P(left)) |
a7a70f31 | 5983 | { |
5984 | left = save_expr(left); | |
5985 | eval_saved = left; | |
5986 | } | |
29a2d1d8 | 5987 | if (!DECL_P(right)) |
a7a70f31 | 5988 | { |
5989 | right = save_expr(right); | |
5990 | if (eval_saved == NULL_TREE) | |
5991 | eval_saved = right; | |
5992 | else | |
29a2d1d8 | 5993 | eval_saved = fold_build2_loc(gccloc, COMPOUND_EXPR, |
a7a70f31 | 5994 | void_type_node, eval_saved, right); |
5995 | } | |
e440a328 | 5996 | } |
5997 | ||
29a2d1d8 | 5998 | tree ret = fold_build2_loc(gccloc, code, |
e440a328 | 5999 | compute_type != NULL_TREE ? compute_type : type, |
6000 | left, right); | |
6001 | ||
6002 | if (compute_type != NULL_TREE) | |
6003 | ret = ::convert(type, ret); | |
6004 | ||
6005 | // In Go, a shift larger than the size of the type is well-defined. | |
6006 | // This is not true in GENERIC, so we need to insert a conditional. | |
6007 | if (is_shift_op) | |
6008 | { | |
c484d925 | 6009 | go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); |
6010 | go_assert(this->left_->type()->integer_type() != NULL); | |
e440a328 | 6011 | int bits = TYPE_PRECISION(TREE_TYPE(left)); |
6012 | ||
6013 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
6014 | build_int_cst_type(TREE_TYPE(right), bits)); | |
6015 | ||
29a2d1d8 | 6016 | tree overflow_result = fold_convert_loc(gccloc, TREE_TYPE(left), |
e440a328 | 6017 | integer_zero_node); |
6018 | if (this->op_ == OPERATOR_RSHIFT | |
6019 | && !this->left_->type()->integer_type()->is_unsigned()) | |
6020 | { | |
b13c66cd | 6021 | tree neg = |
29a2d1d8 | 6022 | fold_build2_loc(gccloc, LT_EXPR, boolean_type_node, |
6023 | left, | |
6024 | fold_convert_loc(gccloc, TREE_TYPE(left), | |
b13c66cd | 6025 | integer_zero_node)); |
6026 | tree neg_one = | |
29a2d1d8 | 6027 | fold_build2_loc(gccloc, MINUS_EXPR, TREE_TYPE(left), |
6028 | fold_convert_loc(gccloc, TREE_TYPE(left), | |
b13c66cd | 6029 | integer_zero_node), |
29a2d1d8 | 6030 | fold_convert_loc(gccloc, TREE_TYPE(left), |
b13c66cd | 6031 | integer_one_node)); |
6032 | overflow_result = | |
29a2d1d8 | 6033 | fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left), |
6034 | neg, neg_one, overflow_result); | |
6035 | } | |
6036 | ||
6037 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left), | |
6038 | compare, ret, overflow_result); | |
6039 | ||
6040 | if (eval_saved != NULL_TREE) | |
6041 | ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), | |
6042 | eval_saved, ret); | |
6043 | } | |
6044 | ||
6045 | // Add checks for division by zero and division overflow as needed. | |
6046 | if (is_idiv_op) | |
6047 | { | |
6048 | if (go_check_divide_zero) | |
6049 | { | |
6050 | // right == 0 | |
6051 | tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
6052 | right, | |
6053 | fold_convert_loc(gccloc, | |
6054 | TREE_TYPE(right), | |
6055 | integer_zero_node)); | |
6056 | ||
6057 | // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0 | |
6058 | int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO; | |
6059 | tree panic = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), | |
1b1f2abf | 6060 | gogo->runtime_error(errcode, |
29a2d1d8 | 6061 | this->location()), |
6062 | fold_convert_loc(gccloc, TREE_TYPE(ret), | |
6063 | integer_zero_node)); | |
6064 | ||
6065 | // right == 0 ? (__go_runtime_error(...), 0) : ret | |
6066 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
6067 | check, panic, ret); | |
b13c66cd | 6068 | } |
6069 | ||
29a2d1d8 | 6070 | if (go_check_divide_overflow) |
6071 | { | |
6072 | // right == -1 | |
6073 | // FIXME: It would be nice to say that this test is expected | |
6074 | // to return false. | |
6075 | tree m1 = integer_minus_one_node; | |
6076 | tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
6077 | right, | |
6078 | fold_convert_loc(gccloc, | |
6079 | TREE_TYPE(right), | |
6080 | m1)); | |
6081 | ||
6082 | tree overflow; | |
6083 | if (TYPE_UNSIGNED(TREE_TYPE(ret))) | |
6084 | { | |
6085 | // An unsigned -1 is the largest possible number, so | |
6086 | // dividing is always 1 or 0. | |
6087 | tree cmp = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node, | |
6088 | left, right); | |
6089 | if (this->op_ == OPERATOR_DIV) | |
6090 | overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
6091 | cmp, | |
6092 | fold_convert_loc(gccloc, | |
6093 | TREE_TYPE(ret), | |
6094 | integer_one_node), | |
6095 | fold_convert_loc(gccloc, | |
6096 | TREE_TYPE(ret), | |
6097 | integer_zero_node)); | |
6098 | else | |
6099 | overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
6100 | cmp, | |
6101 | fold_convert_loc(gccloc, | |
6102 | TREE_TYPE(ret), | |
6103 | integer_zero_node), | |
6104 | left); | |
6105 | } | |
6106 | else | |
6107 | { | |
6108 | // Computing left / -1 is the same as computing - left, | |
6109 | // which does not overflow since Go sets -fwrapv. | |
6110 | if (this->op_ == OPERATOR_DIV) | |
6111 | overflow = fold_build1_loc(gccloc, NEGATE_EXPR, TREE_TYPE(left), | |
6112 | left); | |
6113 | else | |
6114 | overflow = integer_zero_node; | |
6115 | } | |
6116 | overflow = fold_convert_loc(gccloc, TREE_TYPE(ret), overflow); | |
6117 | ||
6118 | // right == -1 ? - left : ret | |
6119 | ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret), | |
6120 | check, overflow, ret); | |
6121 | } | |
e440a328 | 6122 | |
a7a70f31 | 6123 | if (eval_saved != NULL_TREE) |
29a2d1d8 | 6124 | ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret), |
6125 | eval_saved, ret); | |
e440a328 | 6126 | } |
6127 | ||
6128 | return ret; | |
6129 | } | |
6130 | ||
6131 | // Export a binary expression. | |
6132 | ||
6133 | void | |
6134 | Binary_expression::do_export(Export* exp) const | |
6135 | { | |
6136 | exp->write_c_string("("); | |
6137 | this->left_->export_expression(exp); | |
6138 | switch (this->op_) | |
6139 | { | |
6140 | case OPERATOR_OROR: | |
6141 | exp->write_c_string(" || "); | |
6142 | break; | |
6143 | case OPERATOR_ANDAND: | |
6144 | exp->write_c_string(" && "); | |
6145 | break; | |
6146 | case OPERATOR_EQEQ: | |
6147 | exp->write_c_string(" == "); | |
6148 | break; | |
6149 | case OPERATOR_NOTEQ: | |
6150 | exp->write_c_string(" != "); | |
6151 | break; | |
6152 | case OPERATOR_LT: | |
6153 | exp->write_c_string(" < "); | |
6154 | break; | |
6155 | case OPERATOR_LE: | |
6156 | exp->write_c_string(" <= "); | |
6157 | break; | |
6158 | case OPERATOR_GT: | |
6159 | exp->write_c_string(" > "); | |
6160 | break; | |
6161 | case OPERATOR_GE: | |
6162 | exp->write_c_string(" >= "); | |
6163 | break; | |
6164 | case OPERATOR_PLUS: | |
6165 | exp->write_c_string(" + "); | |
6166 | break; | |
6167 | case OPERATOR_MINUS: | |
6168 | exp->write_c_string(" - "); | |
6169 | break; | |
6170 | case OPERATOR_OR: | |
6171 | exp->write_c_string(" | "); | |
6172 | break; | |
6173 | case OPERATOR_XOR: | |
6174 | exp->write_c_string(" ^ "); | |
6175 | break; | |
6176 | case OPERATOR_MULT: | |
6177 | exp->write_c_string(" * "); | |
6178 | break; | |
6179 | case OPERATOR_DIV: | |
6180 | exp->write_c_string(" / "); | |
6181 | break; | |
6182 | case OPERATOR_MOD: | |
6183 | exp->write_c_string(" % "); | |
6184 | break; | |
6185 | case OPERATOR_LSHIFT: | |
6186 | exp->write_c_string(" << "); | |
6187 | break; | |
6188 | case OPERATOR_RSHIFT: | |
6189 | exp->write_c_string(" >> "); | |
6190 | break; | |
6191 | case OPERATOR_AND: | |
6192 | exp->write_c_string(" & "); | |
6193 | break; | |
6194 | case OPERATOR_BITCLEAR: | |
6195 | exp->write_c_string(" &^ "); | |
6196 | break; | |
6197 | default: | |
c3e6f413 | 6198 | go_unreachable(); |
e440a328 | 6199 | } |
6200 | this->right_->export_expression(exp); | |
6201 | exp->write_c_string(")"); | |
6202 | } | |
6203 | ||
6204 | // Import a binary expression. | |
6205 | ||
6206 | Expression* | |
6207 | Binary_expression::do_import(Import* imp) | |
6208 | { | |
6209 | imp->require_c_string("("); | |
6210 | ||
6211 | Expression* left = Expression::import_expression(imp); | |
6212 | ||
6213 | Operator op; | |
6214 | if (imp->match_c_string(" || ")) | |
6215 | { | |
6216 | op = OPERATOR_OROR; | |
6217 | imp->advance(4); | |
6218 | } | |
6219 | else if (imp->match_c_string(" && ")) | |
6220 | { | |
6221 | op = OPERATOR_ANDAND; | |
6222 | imp->advance(4); | |
6223 | } | |
6224 | else if (imp->match_c_string(" == ")) | |
6225 | { | |
6226 | op = OPERATOR_EQEQ; | |
6227 | imp->advance(4); | |
6228 | } | |
6229 | else if (imp->match_c_string(" != ")) | |
6230 | { | |
6231 | op = OPERATOR_NOTEQ; | |
6232 | imp->advance(4); | |
6233 | } | |
6234 | else if (imp->match_c_string(" < ")) | |
6235 | { | |
6236 | op = OPERATOR_LT; | |
6237 | imp->advance(3); | |
6238 | } | |
6239 | else if (imp->match_c_string(" <= ")) | |
6240 | { | |
6241 | op = OPERATOR_LE; | |
6242 | imp->advance(4); | |
6243 | } | |
6244 | else if (imp->match_c_string(" > ")) | |
6245 | { | |
6246 | op = OPERATOR_GT; | |
6247 | imp->advance(3); | |
6248 | } | |
6249 | else if (imp->match_c_string(" >= ")) | |
6250 | { | |
6251 | op = OPERATOR_GE; | |
6252 | imp->advance(4); | |
6253 | } | |
6254 | else if (imp->match_c_string(" + ")) | |
6255 | { | |
6256 | op = OPERATOR_PLUS; | |
6257 | imp->advance(3); | |
6258 | } | |
6259 | else if (imp->match_c_string(" - ")) | |
6260 | { | |
6261 | op = OPERATOR_MINUS; | |
6262 | imp->advance(3); | |
6263 | } | |
6264 | else if (imp->match_c_string(" | ")) | |
6265 | { | |
6266 | op = OPERATOR_OR; | |
6267 | imp->advance(3); | |
6268 | } | |
6269 | else if (imp->match_c_string(" ^ ")) | |
6270 | { | |
6271 | op = OPERATOR_XOR; | |
6272 | imp->advance(3); | |
6273 | } | |
6274 | else if (imp->match_c_string(" * ")) | |
6275 | { | |
6276 | op = OPERATOR_MULT; | |
6277 | imp->advance(3); | |
6278 | } | |
6279 | else if (imp->match_c_string(" / ")) | |
6280 | { | |
6281 | op = OPERATOR_DIV; | |
6282 | imp->advance(3); | |
6283 | } | |
6284 | else if (imp->match_c_string(" % ")) | |
6285 | { | |
6286 | op = OPERATOR_MOD; | |
6287 | imp->advance(3); | |
6288 | } | |
6289 | else if (imp->match_c_string(" << ")) | |
6290 | { | |
6291 | op = OPERATOR_LSHIFT; | |
6292 | imp->advance(4); | |
6293 | } | |
6294 | else if (imp->match_c_string(" >> ")) | |
6295 | { | |
6296 | op = OPERATOR_RSHIFT; | |
6297 | imp->advance(4); | |
6298 | } | |
6299 | else if (imp->match_c_string(" & ")) | |
6300 | { | |
6301 | op = OPERATOR_AND; | |
6302 | imp->advance(3); | |
6303 | } | |
6304 | else if (imp->match_c_string(" &^ ")) | |
6305 | { | |
6306 | op = OPERATOR_BITCLEAR; | |
6307 | imp->advance(4); | |
6308 | } | |
6309 | else | |
6310 | { | |
6311 | error_at(imp->location(), "unrecognized binary operator"); | |
6312 | return Expression::make_error(imp->location()); | |
6313 | } | |
6314 | ||
6315 | Expression* right = Expression::import_expression(imp); | |
6316 | ||
6317 | imp->require_c_string(")"); | |
6318 | ||
6319 | return Expression::make_binary(op, left, right, imp->location()); | |
6320 | } | |
6321 | ||
d751bb78 | 6322 | // Dump ast representation of a binary expression. |
6323 | ||
6324 | void | |
6325 | Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
6326 | { | |
6327 | ast_dump_context->ostream() << "("; | |
6328 | ast_dump_context->dump_expression(this->left_); | |
6329 | ast_dump_context->ostream() << " "; | |
6330 | ast_dump_context->dump_operator(this->op_); | |
6331 | ast_dump_context->ostream() << " "; | |
6332 | ast_dump_context->dump_expression(this->right_); | |
6333 | ast_dump_context->ostream() << ") "; | |
6334 | } | |
6335 | ||
e440a328 | 6336 | // Make a binary expression. |
6337 | ||
6338 | Expression* | |
6339 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
b13c66cd | 6340 | Location location) |
e440a328 | 6341 | { |
6342 | return new Binary_expression(op, left, right, location); | |
6343 | } | |
6344 | ||
6345 | // Implement a comparison. | |
6346 | ||
6347 | tree | |
e90c9dfc | 6348 | Expression::comparison_tree(Translate_context* context, Type* result_type, |
6349 | Operator op, Type* left_type, tree left_tree, | |
e440a328 | 6350 | Type* right_type, tree right_tree, |
b13c66cd | 6351 | Location location) |
e440a328 | 6352 | { |
1b1f2abf | 6353 | Type* int_type = Type::lookup_integer_type("int"); |
6354 | tree int_type_tree = type_to_tree(int_type->get_backend(context->gogo())); | |
6355 | ||
e440a328 | 6356 | enum tree_code code; |
6357 | switch (op) | |
6358 | { | |
6359 | case OPERATOR_EQEQ: | |
6360 | code = EQ_EXPR; | |
6361 | break; | |
6362 | case OPERATOR_NOTEQ: | |
6363 | code = NE_EXPR; | |
6364 | break; | |
6365 | case OPERATOR_LT: | |
6366 | code = LT_EXPR; | |
6367 | break; | |
6368 | case OPERATOR_LE: | |
6369 | code = LE_EXPR; | |
6370 | break; | |
6371 | case OPERATOR_GT: | |
6372 | code = GT_EXPR; | |
6373 | break; | |
6374 | case OPERATOR_GE: | |
6375 | code = GE_EXPR; | |
6376 | break; | |
6377 | default: | |
c3e6f413 | 6378 | go_unreachable(); |
e440a328 | 6379 | } |
6380 | ||
15c67ee2 | 6381 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6382 | { |
9f0e0513 | 6383 | Type* st = Type::make_string_type(); |
6384 | tree string_type = type_to_tree(st->get_backend(context->gogo())); | |
e440a328 | 6385 | static tree string_compare_decl; |
6386 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
6387 | location, | |
6388 | "__go_strcmp", | |
6389 | 2, | |
1b1f2abf | 6390 | int_type_tree, |
e440a328 | 6391 | string_type, |
6392 | left_tree, | |
6393 | string_type, | |
6394 | right_tree); | |
1b1f2abf | 6395 | right_tree = build_int_cst_type(int_type_tree, 0); |
e440a328 | 6396 | } |
15c67ee2 | 6397 | else if ((left_type->interface_type() != NULL |
6398 | && right_type->interface_type() == NULL | |
6399 | && !right_type->is_nil_type()) | |
6400 | || (left_type->interface_type() == NULL | |
6401 | && !left_type->is_nil_type() | |
6402 | && right_type->interface_type() != NULL)) | |
e440a328 | 6403 | { |
6404 | // Comparing an interface value to a non-interface value. | |
6405 | if (left_type->interface_type() == NULL) | |
6406 | { | |
6407 | std::swap(left_type, right_type); | |
6408 | std::swap(left_tree, right_tree); | |
6409 | } | |
6410 | ||
6411 | // The right operand is not an interface. We need to take its | |
6412 | // address if it is not a pointer. | |
6413 | tree make_tmp; | |
6414 | tree arg; | |
6415 | if (right_type->points_to() != NULL) | |
6416 | { | |
6417 | make_tmp = NULL_TREE; | |
6418 | arg = right_tree; | |
6419 | } | |
dd28fd36 | 6420 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) |
6421 | || (TREE_CODE(right_tree) != CONST_DECL | |
6422 | && DECL_P(right_tree))) | |
e440a328 | 6423 | { |
6424 | make_tmp = NULL_TREE; | |
b13c66cd | 6425 | arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree); |
e440a328 | 6426 | if (DECL_P(right_tree)) |
6427 | TREE_ADDRESSABLE(right_tree) = 1; | |
6428 | } | |
6429 | else | |
6430 | { | |
6431 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
6432 | get_name(right_tree)); | |
6433 | DECL_IGNORED_P(tmp) = 0; | |
6434 | DECL_INITIAL(tmp) = right_tree; | |
6435 | TREE_ADDRESSABLE(tmp) = 1; | |
6436 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
b13c66cd | 6437 | SET_EXPR_LOCATION(make_tmp, location.gcc_location()); |
6438 | arg = build_fold_addr_expr_loc(location.gcc_location(), tmp); | |
e440a328 | 6439 | } |
b13c66cd | 6440 | arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg); |
e440a328 | 6441 | |
a1d23b41 | 6442 | tree descriptor = right_type->type_descriptor_pointer(context->gogo(), |
6443 | location); | |
e440a328 | 6444 | |
6445 | if (left_type->interface_type()->is_empty()) | |
6446 | { | |
6447 | static tree empty_interface_value_compare_decl; | |
6448 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6449 | location, | |
6450 | "__go_empty_interface_value_compare", | |
6451 | 3, | |
1b1f2abf | 6452 | int_type_tree, |
e440a328 | 6453 | TREE_TYPE(left_tree), |
6454 | left_tree, | |
6455 | TREE_TYPE(descriptor), | |
6456 | descriptor, | |
6457 | ptr_type_node, | |
6458 | arg); | |
5fb82b5e | 6459 | if (left_tree == error_mark_node) |
6460 | return error_mark_node; | |
e440a328 | 6461 | // This can panic if the type is not comparable. |
6462 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6463 | } | |
6464 | else | |
6465 | { | |
6466 | static tree interface_value_compare_decl; | |
6467 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6468 | location, | |
6469 | "__go_interface_value_compare", | |
6470 | 3, | |
1b1f2abf | 6471 | int_type_tree, |
e440a328 | 6472 | TREE_TYPE(left_tree), |
6473 | left_tree, | |
6474 | TREE_TYPE(descriptor), | |
6475 | descriptor, | |
6476 | ptr_type_node, | |
6477 | arg); | |
5fb82b5e | 6478 | if (left_tree == error_mark_node) |
6479 | return error_mark_node; | |
e440a328 | 6480 | // This can panic if the type is not comparable. |
6481 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6482 | } | |
1b1f2abf | 6483 | right_tree = build_int_cst_type(int_type_tree, 0); |
e440a328 | 6484 | |
6485 | if (make_tmp != NULL_TREE) | |
6486 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6487 | left_tree); | |
6488 | } | |
6489 | else if (left_type->interface_type() != NULL | |
6490 | && right_type->interface_type() != NULL) | |
6491 | { | |
739bad04 | 6492 | if (left_type->interface_type()->is_empty() |
6493 | && right_type->interface_type()->is_empty()) | |
e440a328 | 6494 | { |
e440a328 | 6495 | static tree empty_interface_compare_decl; |
6496 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6497 | location, | |
6498 | "__go_empty_interface_compare", | |
6499 | 2, | |
1b1f2abf | 6500 | int_type_tree, |
e440a328 | 6501 | TREE_TYPE(left_tree), |
6502 | left_tree, | |
6503 | TREE_TYPE(right_tree), | |
6504 | right_tree); | |
5fb82b5e | 6505 | if (left_tree == error_mark_node) |
6506 | return error_mark_node; | |
e440a328 | 6507 | // This can panic if the type is uncomparable. |
6508 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6509 | } | |
739bad04 | 6510 | else if (!left_type->interface_type()->is_empty() |
6511 | && !right_type->interface_type()->is_empty()) | |
e440a328 | 6512 | { |
e440a328 | 6513 | static tree interface_compare_decl; |
6514 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6515 | location, | |
6516 | "__go_interface_compare", | |
6517 | 2, | |
1b1f2abf | 6518 | int_type_tree, |
e440a328 | 6519 | TREE_TYPE(left_tree), |
6520 | left_tree, | |
6521 | TREE_TYPE(right_tree), | |
6522 | right_tree); | |
5fb82b5e | 6523 | if (left_tree == error_mark_node) |
6524 | return error_mark_node; | |
e440a328 | 6525 | // This can panic if the type is uncomparable. |
6526 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6527 | } | |
739bad04 | 6528 | else |
6529 | { | |
6530 | if (left_type->interface_type()->is_empty()) | |
6531 | { | |
c484d925 | 6532 | go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); |
739bad04 | 6533 | std::swap(left_type, right_type); |
6534 | std::swap(left_tree, right_tree); | |
6535 | } | |
c484d925 | 6536 | go_assert(!left_type->interface_type()->is_empty()); |
6537 | go_assert(right_type->interface_type()->is_empty()); | |
739bad04 | 6538 | static tree interface_empty_compare_decl; |
6539 | left_tree = Gogo::call_builtin(&interface_empty_compare_decl, | |
6540 | location, | |
6541 | "__go_interface_empty_compare", | |
6542 | 2, | |
1b1f2abf | 6543 | int_type_tree, |
739bad04 | 6544 | TREE_TYPE(left_tree), |
6545 | left_tree, | |
6546 | TREE_TYPE(right_tree), | |
6547 | right_tree); | |
6548 | if (left_tree == error_mark_node) | |
6549 | return error_mark_node; | |
6550 | // This can panic if the type is uncomparable. | |
6551 | TREE_NOTHROW(interface_empty_compare_decl) = 0; | |
6552 | } | |
6553 | ||
1b1f2abf | 6554 | right_tree = build_int_cst_type(int_type_tree, 0); |
e440a328 | 6555 | } |
6556 | ||
6557 | if (left_type->is_nil_type() | |
6558 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6559 | { | |
6560 | std::swap(left_type, right_type); | |
6561 | std::swap(left_tree, right_tree); | |
6562 | } | |
6563 | ||
6564 | if (right_type->is_nil_type()) | |
6565 | { | |
6566 | if (left_type->array_type() != NULL | |
6567 | && left_type->array_type()->length() == NULL) | |
6568 | { | |
6569 | Array_type* at = left_type->array_type(); | |
6570 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6571 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6572 | } | |
6573 | else if (left_type->interface_type() != NULL) | |
6574 | { | |
6575 | // An interface is nil if the first field is nil. | |
6576 | tree left_type_tree = TREE_TYPE(left_tree); | |
c484d925 | 6577 | go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); |
e440a328 | 6578 | tree field = TYPE_FIELDS(left_type_tree); |
6579 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6580 | field, NULL_TREE); | |
6581 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6582 | } | |
6583 | else | |
6584 | { | |
c484d925 | 6585 | go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); |
e440a328 | 6586 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); |
6587 | } | |
6588 | } | |
6589 | ||
d8ccb1e3 | 6590 | if (left_tree == error_mark_node || right_tree == error_mark_node) |
6591 | return error_mark_node; | |
6592 | ||
e90c9dfc | 6593 | tree result_type_tree; |
6594 | if (result_type == NULL) | |
6595 | result_type_tree = boolean_type_node; | |
6596 | else | |
6597 | result_type_tree = type_to_tree(result_type->get_backend(context->gogo())); | |
6598 | ||
6599 | tree ret = fold_build2(code, result_type_tree, left_tree, right_tree); | |
e440a328 | 6600 | if (CAN_HAVE_LOCATION_P(ret)) |
b13c66cd | 6601 | SET_EXPR_LOCATION(ret, location.gcc_location()); |
e440a328 | 6602 | return ret; |
6603 | } | |
6604 | ||
6605 | // Class Bound_method_expression. | |
6606 | ||
6607 | // Traversal. | |
6608 | ||
6609 | int | |
6610 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6611 | { | |
e0659c9e | 6612 | return Expression::traverse(&this->expr_, traverse); |
e440a328 | 6613 | } |
6614 | ||
0afbb937 | 6615 | // Lower the expression. If this is a method value rather than being |
6616 | // called, and the method is accessed via a pointer, we may need to | |
6617 | // add nil checks. Introduce a temporary variable so that those nil | |
6618 | // checks do not cause multiple evaluation. | |
6619 | ||
6620 | Expression* | |
6621 | Bound_method_expression::do_lower(Gogo*, Named_object*, | |
6622 | Statement_inserter* inserter, int) | |
6623 | { | |
6624 | // For simplicity we use a temporary for every call to an embedded | |
6625 | // method, even though some of them might be pure value methods and | |
6626 | // not require a temporary. | |
6627 | if (this->expr_->var_expression() == NULL | |
6628 | && this->expr_->temporary_reference_expression() == NULL | |
6629 | && this->expr_->set_and_use_temporary_expression() == NULL | |
6630 | && (this->method_->field_indexes() != NULL | |
6631 | || (this->method_->is_value_method() | |
6632 | && this->expr_->type()->points_to() != NULL))) | |
6633 | { | |
6634 | Temporary_statement* temp = | |
6635 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
6636 | inserter->insert(temp); | |
6637 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
6638 | this->location()); | |
6639 | } | |
6640 | return this; | |
6641 | } | |
6642 | ||
e440a328 | 6643 | // Return the type of a bound method expression. The type of this |
0afbb937 | 6644 | // object is simply the type of the method with no receiver. |
e440a328 | 6645 | |
6646 | Type* | |
6647 | Bound_method_expression::do_type() | |
6648 | { | |
0afbb937 | 6649 | Named_object* fn = this->method_->named_object(); |
6650 | Function_type* fntype; | |
6651 | if (fn->is_function()) | |
6652 | fntype = fn->func_value()->type(); | |
6653 | else if (fn->is_function_declaration()) | |
6654 | fntype = fn->func_declaration_value()->type(); | |
e0659c9e | 6655 | else |
6656 | return Type::make_error_type(); | |
0afbb937 | 6657 | return fntype->copy_without_receiver(); |
e440a328 | 6658 | } |
6659 | ||
6660 | // Determine the types of a method expression. | |
6661 | ||
6662 | void | |
6663 | Bound_method_expression::do_determine_type(const Type_context*) | |
6664 | { | |
0afbb937 | 6665 | Named_object* fn = this->method_->named_object(); |
6666 | Function_type* fntype; | |
6667 | if (fn->is_function()) | |
6668 | fntype = fn->func_value()->type(); | |
6669 | else if (fn->is_function_declaration()) | |
6670 | fntype = fn->func_declaration_value()->type(); | |
6671 | else | |
6672 | fntype = NULL; | |
e440a328 | 6673 | if (fntype == NULL || !fntype->is_method()) |
6674 | this->expr_->determine_type_no_context(); | |
6675 | else | |
6676 | { | |
6677 | Type_context subcontext(fntype->receiver()->type(), false); | |
6678 | this->expr_->determine_type(&subcontext); | |
6679 | } | |
6680 | } | |
6681 | ||
6682 | // Check the types of a method expression. | |
6683 | ||
6684 | void | |
6685 | Bound_method_expression::do_check_types(Gogo*) | |
6686 | { | |
0afbb937 | 6687 | Named_object* fn = this->method_->named_object(); |
6688 | if (!fn->is_function() && !fn->is_function_declaration()) | |
6689 | { | |
6690 | this->report_error(_("object is not a method")); | |
6691 | return; | |
6692 | } | |
6693 | ||
6694 | Function_type* fntype; | |
6695 | if (fn->is_function()) | |
6696 | fntype = fn->func_value()->type(); | |
6697 | else if (fn->is_function_declaration()) | |
6698 | fntype = fn->func_declaration_value()->type(); | |
e440a328 | 6699 | else |
0afbb937 | 6700 | go_unreachable(); |
6701 | Type* rtype = fntype->receiver()->type()->deref(); | |
6702 | Type* etype = (this->expr_type_ != NULL | |
6703 | ? this->expr_type_ | |
6704 | : this->expr_->type()); | |
6705 | etype = etype->deref(); | |
6706 | if (!Type::are_identical(rtype, etype, true, NULL)) | |
6707 | this->report_error(_("method type does not match object type")); | |
6708 | } | |
6709 | ||
6710 | // If a bound method expression is not simply called, then it is | |
6711 | // represented as a closure. The closure will hold a single variable, | |
6712 | // the receiver to pass to the method. The function will be a simple | |
6713 | // thunk that pulls that value from the closure and calls the method | |
6714 | // with the remaining arguments. | |
6715 | // | |
6716 | // Because method values are not common, we don't build all thunks for | |
6717 | // every methods, but instead only build them as we need them. In | |
6718 | // particular, we even build them on demand for methods defined in | |
6719 | // other packages. | |
6720 | ||
6721 | Bound_method_expression::Method_value_thunks | |
6722 | Bound_method_expression::method_value_thunks; | |
6723 | ||
6724 | // Find or create the thunk for METHOD. | |
6725 | ||
6726 | Named_object* | |
6727 | Bound_method_expression::create_thunk(Gogo* gogo, const Method* method, | |
6728 | Named_object* fn) | |
6729 | { | |
6730 | std::pair<Named_object*, Named_object*> val(fn, NULL); | |
6731 | std::pair<Method_value_thunks::iterator, bool> ins = | |
6732 | Bound_method_expression::method_value_thunks.insert(val); | |
6733 | if (!ins.second) | |
6734 | { | |
6735 | // We have seen this method before. | |
6736 | go_assert(ins.first->second != NULL); | |
6737 | return ins.first->second; | |
6738 | } | |
6739 | ||
6740 | Location loc = fn->location(); | |
6741 | ||
6742 | Function_type* orig_fntype; | |
6743 | if (fn->is_function()) | |
6744 | orig_fntype = fn->func_value()->type(); | |
6745 | else if (fn->is_function_declaration()) | |
6746 | orig_fntype = fn->func_declaration_value()->type(); | |
6747 | else | |
6748 | orig_fntype = NULL; | |
6749 | ||
6750 | if (orig_fntype == NULL || !orig_fntype->is_method()) | |
e440a328 | 6751 | { |
0afbb937 | 6752 | ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name()); |
6753 | return ins.first->second; | |
e440a328 | 6754 | } |
0afbb937 | 6755 | |
6756 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 6757 | // The type here is wrong--it should be the C function type. But it |
6758 | // doesn't really matter. | |
0afbb937 | 6759 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
6760 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
6761 | sfl->push_back(Struct_field(Typed_identifier("val.1", | |
6762 | orig_fntype->receiver()->type(), | |
6763 | loc))); | |
6764 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
6765 | closure_type = Type::make_pointer_type(closure_type); | |
6766 | ||
f8bdf81a | 6767 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 6768 | |
6769 | Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, | |
6770 | false, loc); | |
6771 | ||
f8bdf81a | 6772 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
6773 | cvar->set_is_used(); | |
6774 | Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); | |
6775 | new_no->func_value()->set_closure_var(cp); | |
0afbb937 | 6776 | |
f8bdf81a | 6777 | gogo->start_block(loc); |
0afbb937 | 6778 | |
6779 | // Field 0 of the closure is the function code pointer, field 1 is | |
6780 | // the value on which to invoke the method. | |
6781 | Expression* arg = Expression::make_var_reference(cp, loc); | |
6782 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
6783 | arg = Expression::make_field_reference(arg, 1, loc); | |
6784 | ||
6785 | Expression* bme = Expression::make_bound_method(arg, method, fn, loc); | |
6786 | ||
6787 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
6788 | Expression_list* args; | |
6789 | if (orig_params == NULL || orig_params->empty()) | |
6790 | args = NULL; | |
6791 | else | |
6792 | { | |
6793 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
6794 | args = new Expression_list(); | |
6795 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 6796 | p != new_params->end(); |
0afbb937 | 6797 | ++p) |
6798 | { | |
6799 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
6800 | go_assert(p_no != NULL | |
6801 | && p_no->is_variable() | |
6802 | && p_no->var_value()->is_parameter()); | |
6803 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
6804 | } | |
6805 | } | |
6806 | ||
6807 | Call_expression* call = Expression::make_call(bme, args, | |
6808 | orig_fntype->is_varargs(), | |
6809 | loc); | |
6810 | call->set_varargs_are_lowered(); | |
6811 | ||
6812 | Statement* s = Statement::make_return_from_call(call, loc); | |
6813 | gogo->add_statement(s); | |
6814 | Block* b = gogo->finish_block(loc); | |
6815 | gogo->add_block(b, loc); | |
6816 | gogo->lower_block(new_no, b); | |
6817 | gogo->finish_function(loc); | |
6818 | ||
6819 | ins.first->second = new_no; | |
6820 | return new_no; | |
6821 | } | |
6822 | ||
6823 | // Return an expression to check *REF for nil while dereferencing | |
6824 | // according to FIELD_INDEXES. Update *REF to build up the field | |
6825 | // reference. This is a static function so that we don't have to | |
6826 | // worry about declaring Field_indexes in expressions.h. | |
6827 | ||
6828 | static Expression* | |
6829 | bme_check_nil(const Method::Field_indexes* field_indexes, Location loc, | |
6830 | Expression** ref) | |
6831 | { | |
6832 | if (field_indexes == NULL) | |
6833 | return Expression::make_boolean(false, loc); | |
6834 | Expression* cond = bme_check_nil(field_indexes->next, loc, ref); | |
6835 | Struct_type* stype = (*ref)->type()->deref()->struct_type(); | |
6836 | go_assert(stype != NULL | |
6837 | && field_indexes->field_index < stype->field_count()); | |
6838 | if ((*ref)->type()->struct_type() == NULL) | |
6839 | { | |
6840 | go_assert((*ref)->type()->points_to() != NULL); | |
6841 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref, | |
6842 | Expression::make_nil(loc), | |
6843 | loc); | |
6844 | cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc); | |
6845 | *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc); | |
6846 | go_assert((*ref)->type()->struct_type() == stype); | |
6847 | } | |
6848 | *ref = Expression::make_field_reference(*ref, field_indexes->field_index, | |
6849 | loc); | |
6850 | return cond; | |
e440a328 | 6851 | } |
6852 | ||
0afbb937 | 6853 | // Get the tree for a method value. |
e440a328 | 6854 | |
6855 | tree | |
0afbb937 | 6856 | Bound_method_expression::do_get_tree(Translate_context* context) |
e440a328 | 6857 | { |
0afbb937 | 6858 | Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(), |
6859 | this->method_, | |
6860 | this->function_); | |
6861 | if (thunk->is_erroneous()) | |
6862 | { | |
6863 | go_assert(saw_errors()); | |
6864 | return error_mark_node; | |
6865 | } | |
6866 | ||
6867 | // FIXME: We should lower this earlier, but we can't lower it in the | |
6868 | // lowering pass because at that point we don't know whether we need | |
6869 | // to create the thunk or not. If the expression is called, we | |
6870 | // don't need the thunk. | |
6871 | ||
6872 | Location loc = this->location(); | |
6873 | ||
6874 | // If the method expects a value, and we have a pointer, we need to | |
6875 | // dereference the pointer. | |
6876 | ||
6877 | Named_object* fn = this->method_->named_object(); | |
6878 | Function_type* fntype; | |
6879 | if (fn->is_function()) | |
6880 | fntype = fn->func_value()->type(); | |
6881 | else if (fn->is_function_declaration()) | |
6882 | fntype = fn->func_declaration_value()->type(); | |
6883 | else | |
6884 | go_unreachable(); | |
6885 | ||
6886 | Expression* val = this->expr_; | |
6887 | if (fntype->receiver()->type()->points_to() == NULL | |
6888 | && val->type()->points_to() != NULL) | |
6889 | val = Expression::make_unary(OPERATOR_MULT, val, loc); | |
6890 | ||
6891 | // Note that we are ignoring this->expr_type_ here. The thunk will | |
6892 | // expect a closure whose second field has type this->expr_type_ (if | |
6893 | // that is not NULL). We are going to pass it a closure whose | |
6894 | // second field has type this->expr_->type(). Since | |
6895 | // this->expr_type_ is only not-NULL for pointer types, we can get | |
6896 | // away with this. | |
6897 | ||
6898 | Struct_field_list* fields = new Struct_field_list(); | |
6899 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
6900 | thunk->func_value()->type(), | |
6901 | loc))); | |
6902 | fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc))); | |
6903 | Struct_type* st = Type::make_struct_type(fields, loc); | |
6904 | ||
6905 | Expression_list* vals = new Expression_list(); | |
6906 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
6907 | vals->push_back(val); | |
6908 | ||
6909 | Expression* ret = Expression::make_struct_composite_literal(st, vals, loc); | |
6910 | ret = Expression::make_heap_composite(ret, loc); | |
6911 | ||
6912 | tree ret_tree = ret->get_tree(context); | |
6913 | ||
6914 | Expression* nil_check = NULL; | |
6915 | ||
6916 | // See whether the expression or any embedded pointers are nil. | |
6917 | ||
6918 | Expression* expr = this->expr_; | |
6919 | if (this->method_->field_indexes() != NULL) | |
6920 | { | |
6921 | // Note that we are evaluating this->expr_ twice, but that is OK | |
6922 | // because in the lowering pass we forced it into a temporary | |
6923 | // variable. | |
6924 | Expression* ref = expr; | |
6925 | nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref); | |
6926 | expr = ref; | |
6927 | } | |
6928 | ||
6929 | if (this->method_->is_value_method() && expr->type()->points_to() != NULL) | |
6930 | { | |
6931 | Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr, | |
6932 | Expression::make_nil(loc), | |
6933 | loc); | |
6934 | if (nil_check == NULL) | |
6935 | nil_check = n; | |
6936 | else | |
6937 | nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc); | |
6938 | } | |
6939 | ||
6940 | if (nil_check != NULL) | |
6941 | { | |
6942 | tree nil_check_tree = nil_check->get_tree(context); | |
6943 | tree crash = | |
6944 | context->gogo()->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, loc); | |
6945 | if (ret_tree == error_mark_node | |
6946 | || nil_check_tree == error_mark_node | |
6947 | || crash == error_mark_node) | |
6948 | return error_mark_node; | |
6949 | ||
6950 | ret_tree = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
6951 | TREE_TYPE(ret_tree), | |
6952 | build3_loc(loc.gcc_location(), COND_EXPR, | |
6953 | void_type_node, nil_check_tree, | |
6954 | crash, NULL_TREE), | |
6955 | ret_tree); | |
6956 | } | |
6957 | ||
6958 | return ret_tree; | |
e440a328 | 6959 | } |
6960 | ||
d751bb78 | 6961 | // Dump ast representation of a bound method expression. |
6962 | ||
6963 | void | |
6964 | Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
6965 | const | |
6966 | { | |
6967 | if (this->expr_type_ != NULL) | |
6968 | ast_dump_context->ostream() << "("; | |
6969 | ast_dump_context->dump_expression(this->expr_); | |
6970 | if (this->expr_type_ != NULL) | |
6971 | { | |
6972 | ast_dump_context->ostream() << ":"; | |
6973 | ast_dump_context->dump_type(this->expr_type_); | |
6974 | ast_dump_context->ostream() << ")"; | |
6975 | } | |
6976 | ||
0afbb937 | 6977 | ast_dump_context->ostream() << "." << this->function_->name(); |
d751bb78 | 6978 | } |
6979 | ||
e440a328 | 6980 | // Make a method expression. |
6981 | ||
6982 | Bound_method_expression* | |
0afbb937 | 6983 | Expression::make_bound_method(Expression* expr, const Method* method, |
6984 | Named_object* function, Location location) | |
e440a328 | 6985 | { |
0afbb937 | 6986 | return new Bound_method_expression(expr, method, function, location); |
e440a328 | 6987 | } |
6988 | ||
6989 | // Class Builtin_call_expression. This is used for a call to a | |
6990 | // builtin function. | |
6991 | ||
6992 | class Builtin_call_expression : public Call_expression | |
6993 | { | |
6994 | public: | |
6995 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
b13c66cd | 6996 | bool is_varargs, Location location); |
e440a328 | 6997 | |
6998 | protected: | |
6999 | // This overrides Call_expression::do_lower. | |
7000 | Expression* | |
ceeb4318 | 7001 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 7002 | |
7003 | bool | |
7004 | do_is_constant() const; | |
7005 | ||
7006 | bool | |
0c77715b | 7007 | do_numeric_constant_value(Numeric_constant*) const; |
e440a328 | 7008 | |
4f2138d7 | 7009 | bool |
a7549a6a | 7010 | do_discarding_value(); |
7011 | ||
e440a328 | 7012 | Type* |
7013 | do_type(); | |
7014 | ||
7015 | void | |
7016 | do_determine_type(const Type_context*); | |
7017 | ||
7018 | void | |
7019 | do_check_types(Gogo*); | |
7020 | ||
7021 | Expression* | |
7022 | do_copy() | |
7023 | { | |
7024 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
7025 | this->args()->copy(), | |
7026 | this->is_varargs(), | |
7027 | this->location()); | |
7028 | } | |
7029 | ||
7030 | tree | |
7031 | do_get_tree(Translate_context*); | |
7032 | ||
7033 | void | |
7034 | do_export(Export*) const; | |
7035 | ||
7036 | virtual bool | |
7037 | do_is_recover_call() const; | |
7038 | ||
7039 | virtual void | |
7040 | do_set_recover_arg(Expression*); | |
7041 | ||
7042 | private: | |
7043 | // The builtin functions. | |
7044 | enum Builtin_function_code | |
7045 | { | |
7046 | BUILTIN_INVALID, | |
7047 | ||
7048 | // Predeclared builtin functions. | |
7049 | BUILTIN_APPEND, | |
7050 | BUILTIN_CAP, | |
7051 | BUILTIN_CLOSE, | |
48080209 | 7052 | BUILTIN_COMPLEX, |
e440a328 | 7053 | BUILTIN_COPY, |
1cce762f | 7054 | BUILTIN_DELETE, |
e440a328 | 7055 | BUILTIN_IMAG, |
7056 | BUILTIN_LEN, | |
7057 | BUILTIN_MAKE, | |
7058 | BUILTIN_NEW, | |
7059 | BUILTIN_PANIC, | |
7060 | BUILTIN_PRINT, | |
7061 | BUILTIN_PRINTLN, | |
7062 | BUILTIN_REAL, | |
7063 | BUILTIN_RECOVER, | |
7064 | ||
7065 | // Builtin functions from the unsafe package. | |
7066 | BUILTIN_ALIGNOF, | |
7067 | BUILTIN_OFFSETOF, | |
7068 | BUILTIN_SIZEOF | |
7069 | }; | |
7070 | ||
7071 | Expression* | |
7072 | one_arg() const; | |
7073 | ||
7074 | bool | |
7075 | check_one_arg(); | |
7076 | ||
7077 | static Type* | |
7078 | real_imag_type(Type*); | |
7079 | ||
7080 | static Type* | |
48080209 | 7081 | complex_type(Type*); |
e440a328 | 7082 | |
a9182619 | 7083 | Expression* |
7084 | lower_make(); | |
7085 | ||
7086 | bool | |
1ad00fd4 | 7087 | check_int_value(Expression*, bool is_length); |
a9182619 | 7088 | |
e440a328 | 7089 | // A pointer back to the general IR structure. This avoids a global |
7090 | // variable, or passing it around everywhere. | |
7091 | Gogo* gogo_; | |
7092 | // The builtin function being called. | |
7093 | Builtin_function_code code_; | |
0f914071 | 7094 | // Used to stop endless loops when the length of an array uses len |
7095 | // or cap of the array itself. | |
7096 | mutable bool seen_; | |
e440a328 | 7097 | }; |
7098 | ||
7099 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
7100 | Expression* fn, | |
7101 | Expression_list* args, | |
7102 | bool is_varargs, | |
b13c66cd | 7103 | Location location) |
e440a328 | 7104 | : Call_expression(fn, args, is_varargs, location), |
0f914071 | 7105 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 7106 | { |
7107 | Func_expression* fnexp = this->fn()->func_expression(); | |
c484d925 | 7108 | go_assert(fnexp != NULL); |
e440a328 | 7109 | const std::string& name(fnexp->named_object()->name()); |
7110 | if (name == "append") | |
7111 | this->code_ = BUILTIN_APPEND; | |
7112 | else if (name == "cap") | |
7113 | this->code_ = BUILTIN_CAP; | |
7114 | else if (name == "close") | |
7115 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 7116 | else if (name == "complex") |
7117 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 7118 | else if (name == "copy") |
7119 | this->code_ = BUILTIN_COPY; | |
1cce762f | 7120 | else if (name == "delete") |
7121 | this->code_ = BUILTIN_DELETE; | |
e440a328 | 7122 | else if (name == "imag") |
7123 | this->code_ = BUILTIN_IMAG; | |
7124 | else if (name == "len") | |
7125 | this->code_ = BUILTIN_LEN; | |
7126 | else if (name == "make") | |
7127 | this->code_ = BUILTIN_MAKE; | |
7128 | else if (name == "new") | |
7129 | this->code_ = BUILTIN_NEW; | |
7130 | else if (name == "panic") | |
7131 | this->code_ = BUILTIN_PANIC; | |
7132 | else if (name == "print") | |
7133 | this->code_ = BUILTIN_PRINT; | |
7134 | else if (name == "println") | |
7135 | this->code_ = BUILTIN_PRINTLN; | |
7136 | else if (name == "real") | |
7137 | this->code_ = BUILTIN_REAL; | |
7138 | else if (name == "recover") | |
7139 | this->code_ = BUILTIN_RECOVER; | |
7140 | else if (name == "Alignof") | |
7141 | this->code_ = BUILTIN_ALIGNOF; | |
7142 | else if (name == "Offsetof") | |
7143 | this->code_ = BUILTIN_OFFSETOF; | |
7144 | else if (name == "Sizeof") | |
7145 | this->code_ = BUILTIN_SIZEOF; | |
7146 | else | |
c3e6f413 | 7147 | go_unreachable(); |
e440a328 | 7148 | } |
7149 | ||
7150 | // Return whether this is a call to recover. This is a virtual | |
7151 | // function called from the parent class. | |
7152 | ||
7153 | bool | |
7154 | Builtin_call_expression::do_is_recover_call() const | |
7155 | { | |
7156 | if (this->classification() == EXPRESSION_ERROR) | |
7157 | return false; | |
7158 | return this->code_ == BUILTIN_RECOVER; | |
7159 | } | |
7160 | ||
7161 | // Set the argument for a call to recover. | |
7162 | ||
7163 | void | |
7164 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
7165 | { | |
7166 | const Expression_list* args = this->args(); | |
c484d925 | 7167 | go_assert(args == NULL || args->empty()); |
e440a328 | 7168 | Expression_list* new_args = new Expression_list(); |
7169 | new_args->push_back(arg); | |
7170 | this->set_args(new_args); | |
7171 | } | |
7172 | ||
e440a328 | 7173 | // Lower a builtin call expression. This turns new and make into |
7174 | // specific expressions. We also convert to a constant if we can. | |
7175 | ||
7176 | Expression* | |
ceeb4318 | 7177 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, |
7178 | Statement_inserter* inserter, int) | |
e440a328 | 7179 | { |
a9182619 | 7180 | if (this->classification() == EXPRESSION_ERROR) |
7181 | return this; | |
7182 | ||
b13c66cd | 7183 | Location loc = this->location(); |
1cce762f | 7184 | |
a8725655 | 7185 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
7186 | { | |
7187 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
1cce762f | 7188 | return Expression::make_error(loc); |
a8725655 | 7189 | } |
7190 | ||
393ba00b | 7191 | if (this->code_ == BUILTIN_OFFSETOF) |
7192 | { | |
7193 | Expression* arg = this->one_arg(); | |
7194 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7195 | while (farg != NULL) | |
7196 | { | |
7197 | if (!farg->implicit()) | |
7198 | break; | |
7199 | // When the selector refers to an embedded field, | |
7200 | // it must not be reached through pointer indirections. | |
7201 | if (farg->expr()->deref() != farg->expr()) | |
7202 | { | |
7203 | this->report_error(_("argument of Offsetof implies indirection of an embedded field")); | |
7204 | return this; | |
7205 | } | |
7206 | // Go up until we reach the original base. | |
7207 | farg = farg->expr()->field_reference_expression(); | |
7208 | } | |
7209 | } | |
7210 | ||
1cce762f | 7211 | if (this->is_constant()) |
e440a328 | 7212 | { |
0c77715b | 7213 | Numeric_constant nc; |
7214 | if (this->numeric_constant_value(&nc)) | |
7215 | return nc.expression(loc); | |
e440a328 | 7216 | } |
1cce762f | 7217 | |
7218 | switch (this->code_) | |
e440a328 | 7219 | { |
1cce762f | 7220 | default: |
7221 | break; | |
7222 | ||
7223 | case BUILTIN_NEW: | |
7224 | { | |
7225 | const Expression_list* args = this->args(); | |
7226 | if (args == NULL || args->size() < 1) | |
7227 | this->report_error(_("not enough arguments")); | |
7228 | else if (args->size() > 1) | |
7229 | this->report_error(_("too many arguments")); | |
7230 | else | |
7231 | { | |
7232 | Expression* arg = args->front(); | |
7233 | if (!arg->is_type_expression()) | |
7234 | { | |
7235 | error_at(arg->location(), "expected type"); | |
7236 | this->set_is_error(); | |
7237 | } | |
7238 | else | |
7239 | return Expression::make_allocation(arg->type(), loc); | |
7240 | } | |
7241 | } | |
7242 | break; | |
7243 | ||
7244 | case BUILTIN_MAKE: | |
7245 | return this->lower_make(); | |
7246 | ||
7247 | case BUILTIN_RECOVER: | |
e440a328 | 7248 | if (function != NULL) |
7249 | function->func_value()->set_calls_recover(); | |
7250 | else | |
7251 | { | |
7252 | // Calling recover outside of a function always returns the | |
7253 | // nil empty interface. | |
823c7e3d | 7254 | Type* eface = Type::make_empty_interface_type(loc); |
1cce762f | 7255 | return Expression::make_cast(eface, Expression::make_nil(loc), loc); |
e440a328 | 7256 | } |
1cce762f | 7257 | break; |
7258 | ||
7259 | case BUILTIN_APPEND: | |
7260 | { | |
7261 | // Lower the varargs. | |
7262 | const Expression_list* args = this->args(); | |
7263 | if (args == NULL || args->empty()) | |
e440a328 | 7264 | return this; |
1cce762f | 7265 | Type* slice_type = args->front()->type(); |
7266 | if (!slice_type->is_slice_type()) | |
7267 | { | |
7268 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
7269 | this->set_is_error(); | |
7270 | return this; | |
7271 | } | |
19fd40c3 | 7272 | Type* element_type = slice_type->array_type()->element_type(); |
7273 | this->lower_varargs(gogo, function, inserter, | |
7274 | Type::make_array_type(element_type, NULL), | |
7275 | 2); | |
1cce762f | 7276 | } |
7277 | break; | |
7278 | ||
7279 | case BUILTIN_DELETE: | |
7280 | { | |
7281 | // Lower to a runtime function call. | |
7282 | const Expression_list* args = this->args(); | |
7283 | if (args == NULL || args->size() < 2) | |
7284 | this->report_error(_("not enough arguments")); | |
7285 | else if (args->size() > 2) | |
7286 | this->report_error(_("too many arguments")); | |
7287 | else if (args->front()->type()->map_type() == NULL) | |
7288 | this->report_error(_("argument 1 must be a map")); | |
7289 | else | |
7290 | { | |
7291 | // Since this function returns no value it must appear in | |
7292 | // a statement by itself, so we don't have to worry about | |
7293 | // order of evaluation of values around it. Evaluate the | |
7294 | // map first to get order of evaluation right. | |
7295 | Map_type* mt = args->front()->type()->map_type(); | |
7296 | Temporary_statement* map_temp = | |
7297 | Statement::make_temporary(mt, args->front(), loc); | |
7298 | inserter->insert(map_temp); | |
7299 | ||
7300 | Temporary_statement* key_temp = | |
7301 | Statement::make_temporary(mt->key_type(), args->back(), loc); | |
7302 | inserter->insert(key_temp); | |
7303 | ||
7304 | Expression* e1 = Expression::make_temporary_reference(map_temp, | |
7305 | loc); | |
7306 | Expression* e2 = Expression::make_temporary_reference(key_temp, | |
7307 | loc); | |
7308 | e2 = Expression::make_unary(OPERATOR_AND, e2, loc); | |
7309 | return Runtime::make_call(Runtime::MAPDELETE, this->location(), | |
7310 | 2, e1, e2); | |
7311 | } | |
7312 | } | |
7313 | break; | |
e440a328 | 7314 | } |
7315 | ||
7316 | return this; | |
7317 | } | |
7318 | ||
a9182619 | 7319 | // Lower a make expression. |
7320 | ||
7321 | Expression* | |
7322 | Builtin_call_expression::lower_make() | |
7323 | { | |
b13c66cd | 7324 | Location loc = this->location(); |
a9182619 | 7325 | |
7326 | const Expression_list* args = this->args(); | |
7327 | if (args == NULL || args->size() < 1) | |
7328 | { | |
7329 | this->report_error(_("not enough arguments")); | |
7330 | return Expression::make_error(this->location()); | |
7331 | } | |
7332 | ||
7333 | Expression_list::const_iterator parg = args->begin(); | |
7334 | ||
7335 | Expression* first_arg = *parg; | |
7336 | if (!first_arg->is_type_expression()) | |
7337 | { | |
7338 | error_at(first_arg->location(), "expected type"); | |
7339 | this->set_is_error(); | |
7340 | return Expression::make_error(this->location()); | |
7341 | } | |
7342 | Type* type = first_arg->type(); | |
7343 | ||
7344 | bool is_slice = false; | |
7345 | bool is_map = false; | |
7346 | bool is_chan = false; | |
411eb89e | 7347 | if (type->is_slice_type()) |
a9182619 | 7348 | is_slice = true; |
7349 | else if (type->map_type() != NULL) | |
7350 | is_map = true; | |
7351 | else if (type->channel_type() != NULL) | |
7352 | is_chan = true; | |
7353 | else | |
7354 | { | |
7355 | this->report_error(_("invalid type for make function")); | |
7356 | return Expression::make_error(this->location()); | |
7357 | } | |
7358 | ||
ac84c822 | 7359 | bool have_big_args = false; |
7360 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); | |
7361 | int uintptr_bits = uintptr_type->integer_type()->bits(); | |
7362 | ||
f6bc81e6 | 7363 | Type_context int_context(Type::lookup_integer_type("int"), false); |
7364 | ||
a9182619 | 7365 | ++parg; |
7366 | Expression* len_arg; | |
7367 | if (parg == args->end()) | |
7368 | { | |
7369 | if (is_slice) | |
7370 | { | |
7371 | this->report_error(_("length required when allocating a slice")); | |
7372 | return Expression::make_error(this->location()); | |
7373 | } | |
7374 | ||
7375 | mpz_t zval; | |
7376 | mpz_init_set_ui(zval, 0); | |
7377 | len_arg = Expression::make_integer(&zval, NULL, loc); | |
7378 | mpz_clear(zval); | |
7379 | } | |
7380 | else | |
7381 | { | |
7382 | len_arg = *parg; | |
f6bc81e6 | 7383 | len_arg->determine_type(&int_context); |
1ad00fd4 | 7384 | if (!this->check_int_value(len_arg, true)) |
7385 | return Expression::make_error(this->location()); | |
ac84c822 | 7386 | if (len_arg->type()->integer_type() != NULL |
7387 | && len_arg->type()->integer_type()->bits() > uintptr_bits) | |
7388 | have_big_args = true; | |
a9182619 | 7389 | ++parg; |
7390 | } | |
7391 | ||
7392 | Expression* cap_arg = NULL; | |
7393 | if (is_slice && parg != args->end()) | |
7394 | { | |
7395 | cap_arg = *parg; | |
f6bc81e6 | 7396 | cap_arg->determine_type(&int_context); |
1ad00fd4 | 7397 | if (!this->check_int_value(cap_arg, false)) |
7398 | return Expression::make_error(this->location()); | |
7399 | ||
7400 | Numeric_constant nclen; | |
7401 | Numeric_constant nccap; | |
7402 | unsigned long vlen; | |
7403 | unsigned long vcap; | |
7404 | if (len_arg->numeric_constant_value(&nclen) | |
7405 | && cap_arg->numeric_constant_value(&nccap) | |
7406 | && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID | |
7407 | && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID | |
7408 | && vlen > vcap) | |
a9182619 | 7409 | { |
1ad00fd4 | 7410 | this->report_error(_("len larger than cap")); |
a9182619 | 7411 | return Expression::make_error(this->location()); |
7412 | } | |
1ad00fd4 | 7413 | |
ac84c822 | 7414 | if (cap_arg->type()->integer_type() != NULL |
7415 | && cap_arg->type()->integer_type()->bits() > uintptr_bits) | |
7416 | have_big_args = true; | |
a9182619 | 7417 | ++parg; |
7418 | } | |
7419 | ||
7420 | if (parg != args->end()) | |
7421 | { | |
7422 | this->report_error(_("too many arguments to make")); | |
7423 | return Expression::make_error(this->location()); | |
7424 | } | |
7425 | ||
b13c66cd | 7426 | Location type_loc = first_arg->location(); |
a9182619 | 7427 | Expression* type_arg; |
7428 | if (is_slice || is_chan) | |
7429 | type_arg = Expression::make_type_descriptor(type, type_loc); | |
7430 | else if (is_map) | |
7431 | type_arg = Expression::make_map_descriptor(type->map_type(), type_loc); | |
7432 | else | |
7433 | go_unreachable(); | |
7434 | ||
7435 | Expression* call; | |
7436 | if (is_slice) | |
7437 | { | |
7438 | if (cap_arg == NULL) | |
ac84c822 | 7439 | call = Runtime::make_call((have_big_args |
7440 | ? Runtime::MAKESLICE1BIG | |
7441 | : Runtime::MAKESLICE1), | |
7442 | loc, 2, type_arg, len_arg); | |
a9182619 | 7443 | else |
ac84c822 | 7444 | call = Runtime::make_call((have_big_args |
7445 | ? Runtime::MAKESLICE2BIG | |
7446 | : Runtime::MAKESLICE2), | |
7447 | loc, 3, type_arg, len_arg, cap_arg); | |
a9182619 | 7448 | } |
7449 | else if (is_map) | |
ac84c822 | 7450 | call = Runtime::make_call((have_big_args |
7451 | ? Runtime::MAKEMAPBIG | |
7452 | : Runtime::MAKEMAP), | |
7453 | loc, 2, type_arg, len_arg); | |
a9182619 | 7454 | else if (is_chan) |
ac84c822 | 7455 | call = Runtime::make_call((have_big_args |
7456 | ? Runtime::MAKECHANBIG | |
7457 | : Runtime::MAKECHAN), | |
7458 | loc, 2, type_arg, len_arg); | |
a9182619 | 7459 | else |
7460 | go_unreachable(); | |
7461 | ||
7462 | return Expression::make_unsafe_cast(type, call, loc); | |
7463 | } | |
7464 | ||
7465 | // Return whether an expression has an integer value. Report an error | |
7466 | // if not. This is used when handling calls to the predeclared make | |
7467 | // function. | |
7468 | ||
7469 | bool | |
1ad00fd4 | 7470 | Builtin_call_expression::check_int_value(Expression* e, bool is_length) |
a9182619 | 7471 | { |
0c77715b | 7472 | Numeric_constant nc; |
1ad00fd4 | 7473 | if (e->numeric_constant_value(&nc)) |
a9182619 | 7474 | { |
1ad00fd4 | 7475 | unsigned long v; |
7476 | switch (nc.to_unsigned_long(&v)) | |
7477 | { | |
7478 | case Numeric_constant::NC_UL_VALID: | |
7479 | return true; | |
7480 | case Numeric_constant::NC_UL_NOTINT: | |
7481 | error_at(e->location(), "non-integer %s argument to make", | |
7482 | is_length ? "len" : "cap"); | |
7483 | return false; | |
7484 | case Numeric_constant::NC_UL_NEGATIVE: | |
7485 | error_at(e->location(), "negative %s argument to make", | |
7486 | is_length ? "len" : "cap"); | |
7487 | return false; | |
7488 | case Numeric_constant::NC_UL_BIG: | |
7489 | // We don't want to give a compile-time error for a 64-bit | |
7490 | // value on a 32-bit target. | |
7491 | return true; | |
7492 | } | |
a9182619 | 7493 | } |
7494 | ||
1ad00fd4 | 7495 | if (e->type()->integer_type() != NULL) |
7496 | return true; | |
7497 | ||
7498 | error_at(e->location(), "non-integer %s argument to make", | |
7499 | is_length ? "len" : "cap"); | |
a9182619 | 7500 | return false; |
7501 | } | |
7502 | ||
e440a328 | 7503 | // Return the type of the real or imag functions, given the type of |
7504 | // the argument. We need to map complex to float, complex64 to | |
7505 | // float32, and complex128 to float64, so it has to be done by name. | |
7506 | // This returns NULL if it can't figure out the type. | |
7507 | ||
7508 | Type* | |
7509 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
7510 | { | |
7511 | if (arg_type == NULL || arg_type->is_abstract()) | |
7512 | return NULL; | |
7513 | Named_type* nt = arg_type->named_type(); | |
7514 | if (nt == NULL) | |
7515 | return NULL; | |
7516 | while (nt->real_type()->named_type() != NULL) | |
7517 | nt = nt->real_type()->named_type(); | |
48080209 | 7518 | if (nt->name() == "complex64") |
e440a328 | 7519 | return Type::lookup_float_type("float32"); |
7520 | else if (nt->name() == "complex128") | |
7521 | return Type::lookup_float_type("float64"); | |
7522 | else | |
7523 | return NULL; | |
7524 | } | |
7525 | ||
48080209 | 7526 | // Return the type of the complex function, given the type of one of the |
e440a328 | 7527 | // argments. Like real_imag_type, we have to map by name. |
7528 | ||
7529 | Type* | |
48080209 | 7530 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 7531 | { |
7532 | if (arg_type == NULL || arg_type->is_abstract()) | |
7533 | return NULL; | |
7534 | Named_type* nt = arg_type->named_type(); | |
7535 | if (nt == NULL) | |
7536 | return NULL; | |
7537 | while (nt->real_type()->named_type() != NULL) | |
7538 | nt = nt->real_type()->named_type(); | |
48080209 | 7539 | if (nt->name() == "float32") |
e440a328 | 7540 | return Type::lookup_complex_type("complex64"); |
7541 | else if (nt->name() == "float64") | |
7542 | return Type::lookup_complex_type("complex128"); | |
7543 | else | |
7544 | return NULL; | |
7545 | } | |
7546 | ||
7547 | // Return a single argument, or NULL if there isn't one. | |
7548 | ||
7549 | Expression* | |
7550 | Builtin_call_expression::one_arg() const | |
7551 | { | |
7552 | const Expression_list* args = this->args(); | |
aa615cb3 | 7553 | if (args == NULL || args->size() != 1) |
e440a328 | 7554 | return NULL; |
7555 | return args->front(); | |
7556 | } | |
7557 | ||
83921647 | 7558 | // A traversal class which looks for a call or receive expression. |
7559 | ||
7560 | class Find_call_expression : public Traverse | |
7561 | { | |
7562 | public: | |
7563 | Find_call_expression() | |
7564 | : Traverse(traverse_expressions), | |
7565 | found_(false) | |
7566 | { } | |
7567 | ||
7568 | int | |
7569 | expression(Expression**); | |
7570 | ||
7571 | bool | |
7572 | found() | |
7573 | { return this->found_; } | |
7574 | ||
7575 | private: | |
7576 | bool found_; | |
7577 | }; | |
7578 | ||
7579 | int | |
7580 | Find_call_expression::expression(Expression** pexpr) | |
7581 | { | |
7582 | if ((*pexpr)->call_expression() != NULL | |
7583 | || (*pexpr)->receive_expression() != NULL) | |
7584 | { | |
7585 | this->found_ = true; | |
7586 | return TRAVERSE_EXIT; | |
7587 | } | |
7588 | return TRAVERSE_CONTINUE; | |
7589 | } | |
7590 | ||
7591 | // Return whether this is constant: len of a string constant, or len | |
7592 | // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof, | |
7593 | // unsafe.Alignof. | |
e440a328 | 7594 | |
7595 | bool | |
7596 | Builtin_call_expression::do_is_constant() const | |
7597 | { | |
7598 | switch (this->code_) | |
7599 | { | |
7600 | case BUILTIN_LEN: | |
7601 | case BUILTIN_CAP: | |
7602 | { | |
0f914071 | 7603 | if (this->seen_) |
7604 | return false; | |
7605 | ||
e440a328 | 7606 | Expression* arg = this->one_arg(); |
7607 | if (arg == NULL) | |
7608 | return false; | |
7609 | Type* arg_type = arg->type(); | |
7610 | ||
7611 | if (arg_type->points_to() != NULL | |
7612 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7613 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7614 | arg_type = arg_type->points_to(); |
7615 | ||
83921647 | 7616 | // The len and cap functions are only constant if there are no |
7617 | // function calls or channel operations in the arguments. | |
7618 | // Otherwise we have to make the call. | |
7619 | if (!arg->is_constant()) | |
7620 | { | |
7621 | Find_call_expression find_call; | |
7622 | Expression::traverse(&arg, &find_call); | |
7623 | if (find_call.found()) | |
7624 | return false; | |
7625 | } | |
7626 | ||
e440a328 | 7627 | if (arg_type->array_type() != NULL |
7628 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7629 | return true; |
e440a328 | 7630 | |
7631 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7632 | { |
7633 | this->seen_ = true; | |
7634 | bool ret = arg->is_constant(); | |
7635 | this->seen_ = false; | |
7636 | return ret; | |
7637 | } | |
e440a328 | 7638 | } |
7639 | break; | |
7640 | ||
7641 | case BUILTIN_SIZEOF: | |
7642 | case BUILTIN_ALIGNOF: | |
7643 | return this->one_arg() != NULL; | |
7644 | ||
7645 | case BUILTIN_OFFSETOF: | |
7646 | { | |
7647 | Expression* arg = this->one_arg(); | |
7648 | if (arg == NULL) | |
7649 | return false; | |
7650 | return arg->field_reference_expression() != NULL; | |
7651 | } | |
7652 | ||
48080209 | 7653 | case BUILTIN_COMPLEX: |
e440a328 | 7654 | { |
7655 | const Expression_list* args = this->args(); | |
7656 | if (args != NULL && args->size() == 2) | |
7657 | return args->front()->is_constant() && args->back()->is_constant(); | |
7658 | } | |
7659 | break; | |
7660 | ||
7661 | case BUILTIN_REAL: | |
7662 | case BUILTIN_IMAG: | |
7663 | { | |
7664 | Expression* arg = this->one_arg(); | |
7665 | return arg != NULL && arg->is_constant(); | |
7666 | } | |
7667 | ||
7668 | default: | |
7669 | break; | |
7670 | } | |
7671 | ||
7672 | return false; | |
7673 | } | |
7674 | ||
0c77715b | 7675 | // Return a numeric constant if possible. |
e440a328 | 7676 | |
7677 | bool | |
0c77715b | 7678 | Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const |
e440a328 | 7679 | { |
7680 | if (this->code_ == BUILTIN_LEN | |
7681 | || this->code_ == BUILTIN_CAP) | |
7682 | { | |
7683 | Expression* arg = this->one_arg(); | |
7684 | if (arg == NULL) | |
7685 | return false; | |
7686 | Type* arg_type = arg->type(); | |
7687 | ||
7688 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7689 | { | |
7690 | std::string sval; | |
7691 | if (arg->string_constant_value(&sval)) | |
7692 | { | |
0c77715b | 7693 | nc->set_unsigned_long(Type::lookup_integer_type("int"), |
7694 | sval.length()); | |
e440a328 | 7695 | return true; |
7696 | } | |
7697 | } | |
7698 | ||
7699 | if (arg_type->points_to() != NULL | |
7700 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 7701 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 7702 | arg_type = arg_type->points_to(); |
7703 | ||
7704 | if (arg_type->array_type() != NULL | |
7705 | && arg_type->array_type()->length() != NULL) | |
7706 | { | |
0f914071 | 7707 | if (this->seen_) |
7708 | return false; | |
e440a328 | 7709 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7710 | this->seen_ = true; |
0c77715b | 7711 | bool r = e->numeric_constant_value(nc); |
0f914071 | 7712 | this->seen_ = false; |
7713 | if (r) | |
e440a328 | 7714 | { |
0c77715b | 7715 | if (!nc->set_type(Type::lookup_integer_type("int"), false, |
7716 | this->location())) | |
7717 | r = false; | |
e440a328 | 7718 | } |
0c77715b | 7719 | return r; |
e440a328 | 7720 | } |
7721 | } | |
7722 | else if (this->code_ == BUILTIN_SIZEOF | |
7723 | || this->code_ == BUILTIN_ALIGNOF) | |
7724 | { | |
7725 | Expression* arg = this->one_arg(); | |
7726 | if (arg == NULL) | |
7727 | return false; | |
7728 | Type* arg_type = arg->type(); | |
5c13bd80 | 7729 | if (arg_type->is_error()) |
e440a328 | 7730 | return false; |
7731 | if (arg_type->is_abstract()) | |
7732 | return false; | |
927a01eb | 7733 | |
7734 | unsigned int ret; | |
e440a328 | 7735 | if (this->code_ == BUILTIN_SIZEOF) |
7736 | { | |
927a01eb | 7737 | if (!arg_type->backend_type_size(this->gogo_, &ret)) |
e440a328 | 7738 | return false; |
7739 | } | |
7740 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7741 | { | |
637bd3af | 7742 | if (arg->field_reference_expression() == NULL) |
927a01eb | 7743 | { |
7744 | if (!arg_type->backend_type_align(this->gogo_, &ret)) | |
7745 | return false; | |
7746 | } | |
637bd3af | 7747 | else |
e440a328 | 7748 | { |
7749 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7750 | // the type of f when it is used as a field in a struct. | |
927a01eb | 7751 | if (!arg_type->backend_type_field_align(this->gogo_, &ret)) |
7752 | return false; | |
e440a328 | 7753 | } |
e440a328 | 7754 | } |
7755 | else | |
c3e6f413 | 7756 | go_unreachable(); |
927a01eb | 7757 | |
7ba86326 | 7758 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
7759 | static_cast<unsigned long>(ret)); | |
e440a328 | 7760 | return true; |
7761 | } | |
7762 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7763 | { | |
7764 | Expression* arg = this->one_arg(); | |
7765 | if (arg == NULL) | |
7766 | return false; | |
7767 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7768 | if (farg == NULL) | |
7769 | return false; | |
9a4bd570 | 7770 | unsigned int total_offset = 0; |
7771 | while (true) | |
7772 | { | |
7773 | Expression* struct_expr = farg->expr(); | |
7774 | Type* st = struct_expr->type(); | |
7775 | if (st->struct_type() == NULL) | |
7776 | return false; | |
7777 | if (st->named_type() != NULL) | |
7778 | st->named_type()->convert(this->gogo_); | |
7779 | unsigned int offset; | |
7780 | if (!st->struct_type()->backend_field_offset(this->gogo_, | |
7781 | farg->field_index(), | |
7782 | &offset)) | |
7783 | return false; | |
7784 | total_offset += offset; | |
7785 | if (farg->implicit() && struct_expr->field_reference_expression() != NULL) | |
7786 | { | |
7787 | // Go up until we reach the original base. | |
7788 | farg = struct_expr->field_reference_expression(); | |
7789 | continue; | |
7790 | } | |
7791 | break; | |
7792 | } | |
7ba86326 | 7793 | nc->set_unsigned_long(Type::lookup_integer_type("uintptr"), |
9a4bd570 | 7794 | static_cast<unsigned long>(total_offset)); |
e440a328 | 7795 | return true; |
7796 | } | |
0c77715b | 7797 | else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) |
e440a328 | 7798 | { |
7799 | Expression* arg = this->one_arg(); | |
7800 | if (arg == NULL) | |
7801 | return false; | |
7802 | ||
0c77715b | 7803 | Numeric_constant argnc; |
7804 | if (!arg->numeric_constant_value(&argnc)) | |
7805 | return false; | |
7806 | ||
e440a328 | 7807 | mpfr_t real; |
7808 | mpfr_t imag; | |
0c77715b | 7809 | if (!argnc.to_complex(&real, &imag)) |
7810 | return false; | |
e440a328 | 7811 | |
0c77715b | 7812 | Type* type = Builtin_call_expression::real_imag_type(argnc.type()); |
7813 | if (this->code_ == BUILTIN_REAL) | |
7814 | nc->set_float(type, real); | |
7815 | else | |
7816 | nc->set_float(type, imag); | |
7817 | return true; | |
e440a328 | 7818 | } |
0c77715b | 7819 | else if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7820 | { |
7821 | const Expression_list* args = this->args(); | |
7822 | if (args == NULL || args->size() != 2) | |
7823 | return false; | |
7824 | ||
0c77715b | 7825 | Numeric_constant rnc; |
7826 | if (!args->front()->numeric_constant_value(&rnc)) | |
7827 | return false; | |
7828 | Numeric_constant inc; | |
7829 | if (!args->back()->numeric_constant_value(&inc)) | |
7830 | return false; | |
7831 | ||
7832 | if (rnc.type() != NULL | |
7833 | && !rnc.type()->is_abstract() | |
7834 | && inc.type() != NULL | |
7835 | && !inc.type()->is_abstract() | |
7836 | && !Type::are_identical(rnc.type(), inc.type(), false, NULL)) | |
7837 | return false; | |
7838 | ||
e440a328 | 7839 | mpfr_t r; |
0c77715b | 7840 | if (!rnc.to_float(&r)) |
7841 | return false; | |
7842 | mpfr_t i; | |
7843 | if (!inc.to_float(&i)) | |
e440a328 | 7844 | { |
7845 | mpfr_clear(r); | |
7846 | return false; | |
7847 | } | |
7848 | ||
0c77715b | 7849 | Type* arg_type = rnc.type(); |
7850 | if (arg_type == NULL || arg_type->is_abstract()) | |
7851 | arg_type = inc.type(); | |
e440a328 | 7852 | |
0c77715b | 7853 | Type* type = Builtin_call_expression::complex_type(arg_type); |
7854 | nc->set_complex(type, r, i); | |
e440a328 | 7855 | |
7856 | mpfr_clear(r); | |
7857 | mpfr_clear(i); | |
7858 | ||
0c77715b | 7859 | return true; |
e440a328 | 7860 | } |
7861 | ||
7862 | return false; | |
7863 | } | |
7864 | ||
a7549a6a | 7865 | // Give an error if we are discarding the value of an expression which |
7866 | // should not normally be discarded. We don't give an error for | |
7867 | // discarding the value of an ordinary function call, but we do for | |
7868 | // builtin functions, purely for consistency with the gc compiler. | |
7869 | ||
4f2138d7 | 7870 | bool |
a7549a6a | 7871 | Builtin_call_expression::do_discarding_value() |
7872 | { | |
7873 | switch (this->code_) | |
7874 | { | |
7875 | case BUILTIN_INVALID: | |
7876 | default: | |
7877 | go_unreachable(); | |
7878 | ||
7879 | case BUILTIN_APPEND: | |
7880 | case BUILTIN_CAP: | |
7881 | case BUILTIN_COMPLEX: | |
7882 | case BUILTIN_IMAG: | |
7883 | case BUILTIN_LEN: | |
7884 | case BUILTIN_MAKE: | |
7885 | case BUILTIN_NEW: | |
7886 | case BUILTIN_REAL: | |
7887 | case BUILTIN_ALIGNOF: | |
7888 | case BUILTIN_OFFSETOF: | |
7889 | case BUILTIN_SIZEOF: | |
7890 | this->unused_value_error(); | |
4f2138d7 | 7891 | return false; |
a7549a6a | 7892 | |
7893 | case BUILTIN_CLOSE: | |
7894 | case BUILTIN_COPY: | |
1cce762f | 7895 | case BUILTIN_DELETE: |
a7549a6a | 7896 | case BUILTIN_PANIC: |
7897 | case BUILTIN_PRINT: | |
7898 | case BUILTIN_PRINTLN: | |
7899 | case BUILTIN_RECOVER: | |
4f2138d7 | 7900 | return true; |
a7549a6a | 7901 | } |
7902 | } | |
7903 | ||
e440a328 | 7904 | // Return the type. |
7905 | ||
7906 | Type* | |
7907 | Builtin_call_expression::do_type() | |
7908 | { | |
7909 | switch (this->code_) | |
7910 | { | |
7911 | case BUILTIN_INVALID: | |
7912 | default: | |
c3e6f413 | 7913 | go_unreachable(); |
e440a328 | 7914 | |
7915 | case BUILTIN_NEW: | |
7916 | case BUILTIN_MAKE: | |
7917 | { | |
7918 | const Expression_list* args = this->args(); | |
7919 | if (args == NULL || args->empty()) | |
7920 | return Type::make_error_type(); | |
7921 | return Type::make_pointer_type(args->front()->type()); | |
7922 | } | |
7923 | ||
7924 | case BUILTIN_CAP: | |
7925 | case BUILTIN_COPY: | |
7926 | case BUILTIN_LEN: | |
7ba86326 | 7927 | return Type::lookup_integer_type("int"); |
7928 | ||
e440a328 | 7929 | case BUILTIN_ALIGNOF: |
7930 | case BUILTIN_OFFSETOF: | |
7931 | case BUILTIN_SIZEOF: | |
7ba86326 | 7932 | return Type::lookup_integer_type("uintptr"); |
e440a328 | 7933 | |
7934 | case BUILTIN_CLOSE: | |
1cce762f | 7935 | case BUILTIN_DELETE: |
e440a328 | 7936 | case BUILTIN_PANIC: |
7937 | case BUILTIN_PRINT: | |
7938 | case BUILTIN_PRINTLN: | |
7939 | return Type::make_void_type(); | |
7940 | ||
e440a328 | 7941 | case BUILTIN_RECOVER: |
823c7e3d | 7942 | return Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 7943 | |
7944 | case BUILTIN_APPEND: | |
7945 | { | |
7946 | const Expression_list* args = this->args(); | |
7947 | if (args == NULL || args->empty()) | |
7948 | return Type::make_error_type(); | |
7949 | return args->front()->type(); | |
7950 | } | |
7951 | ||
7952 | case BUILTIN_REAL: | |
7953 | case BUILTIN_IMAG: | |
7954 | { | |
7955 | Expression* arg = this->one_arg(); | |
7956 | if (arg == NULL) | |
7957 | return Type::make_error_type(); | |
7958 | Type* t = arg->type(); | |
7959 | if (t->is_abstract()) | |
7960 | t = t->make_non_abstract_type(); | |
7961 | t = Builtin_call_expression::real_imag_type(t); | |
7962 | if (t == NULL) | |
7963 | t = Type::make_error_type(); | |
7964 | return t; | |
7965 | } | |
7966 | ||
48080209 | 7967 | case BUILTIN_COMPLEX: |
e440a328 | 7968 | { |
7969 | const Expression_list* args = this->args(); | |
7970 | if (args == NULL || args->size() != 2) | |
7971 | return Type::make_error_type(); | |
7972 | Type* t = args->front()->type(); | |
7973 | if (t->is_abstract()) | |
7974 | { | |
7975 | t = args->back()->type(); | |
7976 | if (t->is_abstract()) | |
7977 | t = t->make_non_abstract_type(); | |
7978 | } | |
48080209 | 7979 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7980 | if (t == NULL) |
7981 | t = Type::make_error_type(); | |
7982 | return t; | |
7983 | } | |
7984 | } | |
7985 | } | |
7986 | ||
7987 | // Determine the type. | |
7988 | ||
7989 | void | |
7990 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7991 | { | |
fb94b0ca | 7992 | if (!this->determining_types()) |
7993 | return; | |
7994 | ||
e440a328 | 7995 | this->fn()->determine_type_no_context(); |
7996 | ||
7997 | const Expression_list* args = this->args(); | |
7998 | ||
7999 | bool is_print; | |
8000 | Type* arg_type = NULL; | |
8001 | switch (this->code_) | |
8002 | { | |
8003 | case BUILTIN_PRINT: | |
8004 | case BUILTIN_PRINTLN: | |
8005 | // Do not force a large integer constant to "int". | |
8006 | is_print = true; | |
8007 | break; | |
8008 | ||
8009 | case BUILTIN_REAL: | |
8010 | case BUILTIN_IMAG: | |
48080209 | 8011 | arg_type = Builtin_call_expression::complex_type(context->type); |
f6bc81e6 | 8012 | if (arg_type == NULL) |
8013 | arg_type = Type::lookup_complex_type("complex128"); | |
e440a328 | 8014 | is_print = false; |
8015 | break; | |
8016 | ||
48080209 | 8017 | case BUILTIN_COMPLEX: |
e440a328 | 8018 | { |
48080209 | 8019 | // For the complex function the type of one operand can |
e440a328 | 8020 | // determine the type of the other, as in a binary expression. |
8021 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
f6bc81e6 | 8022 | if (arg_type == NULL) |
8023 | arg_type = Type::lookup_float_type("float64"); | |
e440a328 | 8024 | if (args != NULL && args->size() == 2) |
8025 | { | |
8026 | Type* t1 = args->front()->type(); | |
c849bb59 | 8027 | Type* t2 = args->back()->type(); |
e440a328 | 8028 | if (!t1->is_abstract()) |
8029 | arg_type = t1; | |
8030 | else if (!t2->is_abstract()) | |
8031 | arg_type = t2; | |
8032 | } | |
8033 | is_print = false; | |
8034 | } | |
8035 | break; | |
8036 | ||
8037 | default: | |
8038 | is_print = false; | |
8039 | break; | |
8040 | } | |
8041 | ||
8042 | if (args != NULL) | |
8043 | { | |
8044 | for (Expression_list::const_iterator pa = args->begin(); | |
8045 | pa != args->end(); | |
8046 | ++pa) | |
8047 | { | |
8048 | Type_context subcontext; | |
8049 | subcontext.type = arg_type; | |
8050 | ||
8051 | if (is_print) | |
8052 | { | |
8053 | // We want to print large constants, we so can't just | |
8054 | // use the appropriate nonabstract type. Use uint64 for | |
8055 | // an integer if we know it is nonnegative, otherwise | |
8056 | // use int64 for a integer, otherwise use float64 for a | |
8057 | // float or complex128 for a complex. | |
8058 | Type* want_type = NULL; | |
8059 | Type* atype = (*pa)->type(); | |
8060 | if (atype->is_abstract()) | |
8061 | { | |
8062 | if (atype->integer_type() != NULL) | |
8063 | { | |
0c77715b | 8064 | Numeric_constant nc; |
8065 | if (this->numeric_constant_value(&nc)) | |
8066 | { | |
8067 | mpz_t val; | |
8068 | if (nc.to_int(&val)) | |
8069 | { | |
8070 | if (mpz_sgn(val) >= 0) | |
8071 | want_type = Type::lookup_integer_type("uint64"); | |
8072 | mpz_clear(val); | |
8073 | } | |
8074 | } | |
8075 | if (want_type == NULL) | |
e440a328 | 8076 | want_type = Type::lookup_integer_type("int64"); |
e440a328 | 8077 | } |
8078 | else if (atype->float_type() != NULL) | |
8079 | want_type = Type::lookup_float_type("float64"); | |
8080 | else if (atype->complex_type() != NULL) | |
8081 | want_type = Type::lookup_complex_type("complex128"); | |
8082 | else if (atype->is_abstract_string_type()) | |
8083 | want_type = Type::lookup_string_type(); | |
8084 | else if (atype->is_abstract_boolean_type()) | |
8085 | want_type = Type::lookup_bool_type(); | |
8086 | else | |
c3e6f413 | 8087 | go_unreachable(); |
e440a328 | 8088 | subcontext.type = want_type; |
8089 | } | |
8090 | } | |
8091 | ||
8092 | (*pa)->determine_type(&subcontext); | |
8093 | } | |
8094 | } | |
8095 | } | |
8096 | ||
8097 | // If there is exactly one argument, return true. Otherwise give an | |
8098 | // error message and return false. | |
8099 | ||
8100 | bool | |
8101 | Builtin_call_expression::check_one_arg() | |
8102 | { | |
8103 | const Expression_list* args = this->args(); | |
8104 | if (args == NULL || args->size() < 1) | |
8105 | { | |
8106 | this->report_error(_("not enough arguments")); | |
8107 | return false; | |
8108 | } | |
8109 | else if (args->size() > 1) | |
8110 | { | |
8111 | this->report_error(_("too many arguments")); | |
8112 | return false; | |
8113 | } | |
8114 | if (args->front()->is_error_expression() | |
5c13bd80 | 8115 | || args->front()->type()->is_error()) |
e440a328 | 8116 | { |
8117 | this->set_is_error(); | |
8118 | return false; | |
8119 | } | |
8120 | return true; | |
8121 | } | |
8122 | ||
8123 | // Check argument types for a builtin function. | |
8124 | ||
8125 | void | |
8126 | Builtin_call_expression::do_check_types(Gogo*) | |
8127 | { | |
375646ea | 8128 | if (this->is_error_expression()) |
8129 | return; | |
e440a328 | 8130 | switch (this->code_) |
8131 | { | |
8132 | case BUILTIN_INVALID: | |
8133 | case BUILTIN_NEW: | |
8134 | case BUILTIN_MAKE: | |
cd238b8d | 8135 | case BUILTIN_DELETE: |
e440a328 | 8136 | return; |
8137 | ||
8138 | case BUILTIN_LEN: | |
8139 | case BUILTIN_CAP: | |
8140 | { | |
8141 | // The single argument may be either a string or an array or a | |
8142 | // map or a channel, or a pointer to a closed array. | |
8143 | if (this->check_one_arg()) | |
8144 | { | |
8145 | Type* arg_type = this->one_arg()->type(); | |
8146 | if (arg_type->points_to() != NULL | |
8147 | && arg_type->points_to()->array_type() != NULL | |
411eb89e | 8148 | && !arg_type->points_to()->is_slice_type()) |
e440a328 | 8149 | arg_type = arg_type->points_to(); |
8150 | if (this->code_ == BUILTIN_CAP) | |
8151 | { | |
5c13bd80 | 8152 | if (!arg_type->is_error() |
e440a328 | 8153 | && arg_type->array_type() == NULL |
8154 | && arg_type->channel_type() == NULL) | |
8155 | this->report_error(_("argument must be array or slice " | |
8156 | "or channel")); | |
8157 | } | |
8158 | else | |
8159 | { | |
5c13bd80 | 8160 | if (!arg_type->is_error() |
e440a328 | 8161 | && !arg_type->is_string_type() |
8162 | && arg_type->array_type() == NULL | |
8163 | && arg_type->map_type() == NULL | |
8164 | && arg_type->channel_type() == NULL) | |
8165 | this->report_error(_("argument must be string or " | |
8166 | "array or slice or map or channel")); | |
8167 | } | |
8168 | } | |
8169 | } | |
8170 | break; | |
8171 | ||
8172 | case BUILTIN_PRINT: | |
8173 | case BUILTIN_PRINTLN: | |
8174 | { | |
8175 | const Expression_list* args = this->args(); | |
8176 | if (args == NULL) | |
8177 | { | |
8178 | if (this->code_ == BUILTIN_PRINT) | |
8179 | warning_at(this->location(), 0, | |
8180 | "no arguments for builtin function %<%s%>", | |
8181 | (this->code_ == BUILTIN_PRINT | |
8182 | ? "print" | |
8183 | : "println")); | |
8184 | } | |
8185 | else | |
8186 | { | |
8187 | for (Expression_list::const_iterator p = args->begin(); | |
8188 | p != args->end(); | |
8189 | ++p) | |
8190 | { | |
8191 | Type* type = (*p)->type(); | |
5c13bd80 | 8192 | if (type->is_error() |
e440a328 | 8193 | || type->is_string_type() |
8194 | || type->integer_type() != NULL | |
8195 | || type->float_type() != NULL | |
8196 | || type->complex_type() != NULL | |
8197 | || type->is_boolean_type() | |
8198 | || type->points_to() != NULL | |
8199 | || type->interface_type() != NULL | |
8200 | || type->channel_type() != NULL | |
8201 | || type->map_type() != NULL | |
8202 | || type->function_type() != NULL | |
411eb89e | 8203 | || type->is_slice_type()) |
e440a328 | 8204 | ; |
acf8e158 | 8205 | else if ((*p)->is_type_expression()) |
8206 | { | |
8207 | // If this is a type expression it's going to give | |
8208 | // an error anyhow, so we don't need one here. | |
8209 | } | |
e440a328 | 8210 | else |
8211 | this->report_error(_("unsupported argument type to " | |
8212 | "builtin function")); | |
8213 | } | |
8214 | } | |
8215 | } | |
8216 | break; | |
8217 | ||
8218 | case BUILTIN_CLOSE: | |
e440a328 | 8219 | if (this->check_one_arg()) |
8220 | { | |
8221 | if (this->one_arg()->type()->channel_type() == NULL) | |
8222 | this->report_error(_("argument must be channel")); | |
5202d986 | 8223 | else if (!this->one_arg()->type()->channel_type()->may_send()) |
8224 | this->report_error(_("cannot close receive-only channel")); | |
e440a328 | 8225 | } |
8226 | break; | |
8227 | ||
8228 | case BUILTIN_PANIC: | |
8229 | case BUILTIN_SIZEOF: | |
8230 | case BUILTIN_ALIGNOF: | |
8231 | this->check_one_arg(); | |
8232 | break; | |
8233 | ||
8234 | case BUILTIN_RECOVER: | |
8235 | if (this->args() != NULL && !this->args()->empty()) | |
8236 | this->report_error(_("too many arguments")); | |
8237 | break; | |
8238 | ||
8239 | case BUILTIN_OFFSETOF: | |
8240 | if (this->check_one_arg()) | |
8241 | { | |
8242 | Expression* arg = this->one_arg(); | |
8243 | if (arg->field_reference_expression() == NULL) | |
8244 | this->report_error(_("argument must be a field reference")); | |
8245 | } | |
8246 | break; | |
8247 | ||
8248 | case BUILTIN_COPY: | |
8249 | { | |
8250 | const Expression_list* args = this->args(); | |
8251 | if (args == NULL || args->size() < 2) | |
8252 | { | |
8253 | this->report_error(_("not enough arguments")); | |
8254 | break; | |
8255 | } | |
8256 | else if (args->size() > 2) | |
8257 | { | |
8258 | this->report_error(_("too many arguments")); | |
8259 | break; | |
8260 | } | |
8261 | Type* arg1_type = args->front()->type(); | |
8262 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 8263 | if (arg1_type->is_error() || arg2_type->is_error()) |
e440a328 | 8264 | break; |
8265 | ||
8266 | Type* e1; | |
411eb89e | 8267 | if (arg1_type->is_slice_type()) |
e440a328 | 8268 | e1 = arg1_type->array_type()->element_type(); |
8269 | else | |
8270 | { | |
8271 | this->report_error(_("left argument must be a slice")); | |
8272 | break; | |
8273 | } | |
8274 | ||
411eb89e | 8275 | if (arg2_type->is_slice_type()) |
60963afd | 8276 | { |
8277 | Type* e2 = arg2_type->array_type()->element_type(); | |
8278 | if (!Type::are_identical(e1, e2, true, NULL)) | |
8279 | this->report_error(_("element types must be the same")); | |
8280 | } | |
e440a328 | 8281 | else if (arg2_type->is_string_type()) |
e440a328 | 8282 | { |
60963afd | 8283 | if (e1->integer_type() == NULL || !e1->integer_type()->is_byte()) |
8284 | this->report_error(_("first argument must be []byte")); | |
e440a328 | 8285 | } |
60963afd | 8286 | else |
8287 | this->report_error(_("second argument must be slice or string")); | |
e440a328 | 8288 | } |
8289 | break; | |
8290 | ||
8291 | case BUILTIN_APPEND: | |
8292 | { | |
8293 | const Expression_list* args = this->args(); | |
b0d311a1 | 8294 | if (args == NULL || args->size() < 2) |
e440a328 | 8295 | { |
8296 | this->report_error(_("not enough arguments")); | |
8297 | break; | |
8298 | } | |
0b7755ec | 8299 | if (args->size() > 2) |
8300 | { | |
8301 | this->report_error(_("too many arguments")); | |
8302 | break; | |
8303 | } | |
cd238b8d | 8304 | if (args->front()->type()->is_error() |
8305 | || args->back()->type()->is_error()) | |
8306 | break; | |
8307 | ||
8308 | Array_type* at = args->front()->type()->array_type(); | |
8309 | Type* e = at->element_type(); | |
4fd4fcf4 | 8310 | |
8311 | // The language permits appending a string to a []byte, as a | |
8312 | // special case. | |
8313 | if (args->back()->type()->is_string_type()) | |
8314 | { | |
60963afd | 8315 | if (e->integer_type() != NULL && e->integer_type()->is_byte()) |
4fd4fcf4 | 8316 | break; |
8317 | } | |
8318 | ||
19fd40c3 | 8319 | // The language says that the second argument must be |
8320 | // assignable to a slice of the element type of the first | |
8321 | // argument. We already know the first argument is a slice | |
8322 | // type. | |
cd238b8d | 8323 | Type* arg2_type = Type::make_array_type(e, NULL); |
e440a328 | 8324 | std::string reason; |
19fd40c3 | 8325 | if (!Type::are_assignable(arg2_type, args->back()->type(), &reason)) |
e440a328 | 8326 | { |
8327 | if (reason.empty()) | |
19fd40c3 | 8328 | this->report_error(_("argument 2 has invalid type")); |
e440a328 | 8329 | else |
8330 | { | |
19fd40c3 | 8331 | error_at(this->location(), "argument 2 has invalid type (%s)", |
e440a328 | 8332 | reason.c_str()); |
8333 | this->set_is_error(); | |
8334 | } | |
8335 | } | |
8336 | break; | |
8337 | } | |
8338 | ||
8339 | case BUILTIN_REAL: | |
8340 | case BUILTIN_IMAG: | |
8341 | if (this->check_one_arg()) | |
8342 | { | |
8343 | if (this->one_arg()->type()->complex_type() == NULL) | |
8344 | this->report_error(_("argument must have complex type")); | |
8345 | } | |
8346 | break; | |
8347 | ||
48080209 | 8348 | case BUILTIN_COMPLEX: |
e440a328 | 8349 | { |
8350 | const Expression_list* args = this->args(); | |
8351 | if (args == NULL || args->size() < 2) | |
8352 | this->report_error(_("not enough arguments")); | |
8353 | else if (args->size() > 2) | |
8354 | this->report_error(_("too many arguments")); | |
8355 | else if (args->front()->is_error_expression() | |
5c13bd80 | 8356 | || args->front()->type()->is_error() |
e440a328 | 8357 | || args->back()->is_error_expression() |
5c13bd80 | 8358 | || args->back()->type()->is_error()) |
e440a328 | 8359 | this->set_is_error(); |
8360 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 8361 | args->back()->type(), true, NULL)) |
48080209 | 8362 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 8363 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 8364 | this->report_error(_("complex arguments must have " |
e440a328 | 8365 | "floating-point type")); |
8366 | } | |
8367 | break; | |
8368 | ||
8369 | default: | |
c3e6f413 | 8370 | go_unreachable(); |
e440a328 | 8371 | } |
8372 | } | |
8373 | ||
8374 | // Return the tree for a builtin function. | |
8375 | ||
8376 | tree | |
8377 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
8378 | { | |
8379 | Gogo* gogo = context->gogo(); | |
b13c66cd | 8380 | Location location = this->location(); |
e440a328 | 8381 | switch (this->code_) |
8382 | { | |
8383 | case BUILTIN_INVALID: | |
8384 | case BUILTIN_NEW: | |
8385 | case BUILTIN_MAKE: | |
c3e6f413 | 8386 | go_unreachable(); |
e440a328 | 8387 | |
8388 | case BUILTIN_LEN: | |
8389 | case BUILTIN_CAP: | |
8390 | { | |
8391 | const Expression_list* args = this->args(); | |
c484d925 | 8392 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8393 | Expression* arg = *args->begin(); |
8394 | Type* arg_type = arg->type(); | |
0f914071 | 8395 | |
8396 | if (this->seen_) | |
8397 | { | |
c484d925 | 8398 | go_assert(saw_errors()); |
0f914071 | 8399 | return error_mark_node; |
8400 | } | |
8401 | this->seen_ = true; | |
8402 | ||
e440a328 | 8403 | tree arg_tree = arg->get_tree(context); |
0f914071 | 8404 | |
8405 | this->seen_ = false; | |
8406 | ||
e440a328 | 8407 | if (arg_tree == error_mark_node) |
8408 | return error_mark_node; | |
8409 | ||
8410 | if (arg_type->points_to() != NULL) | |
8411 | { | |
8412 | arg_type = arg_type->points_to(); | |
c484d925 | 8413 | go_assert(arg_type->array_type() != NULL |
411eb89e | 8414 | && !arg_type->is_slice_type()); |
c484d925 | 8415 | go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); |
e440a328 | 8416 | arg_tree = build_fold_indirect_ref(arg_tree); |
8417 | } | |
8418 | ||
1b1f2abf | 8419 | Type* int_type = Type::lookup_integer_type("int"); |
8420 | tree int_type_tree = type_to_tree(int_type->get_backend(gogo)); | |
8421 | ||
e440a328 | 8422 | tree val_tree; |
8423 | if (this->code_ == BUILTIN_LEN) | |
8424 | { | |
8425 | if (arg_type->is_string_type()) | |
8426 | val_tree = String_type::length_tree(gogo, arg_tree); | |
8427 | else if (arg_type->array_type() != NULL) | |
0f914071 | 8428 | { |
8429 | if (this->seen_) | |
8430 | { | |
c484d925 | 8431 | go_assert(saw_errors()); |
0f914071 | 8432 | return error_mark_node; |
8433 | } | |
8434 | this->seen_ = true; | |
8435 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
8436 | this->seen_ = false; | |
8437 | } | |
e440a328 | 8438 | else if (arg_type->map_type() != NULL) |
8439 | { | |
9f0e0513 | 8440 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 8441 | static tree map_len_fndecl; |
8442 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
8443 | location, | |
8444 | "__go_map_len", | |
8445 | 1, | |
1b1f2abf | 8446 | int_type_tree, |
9f0e0513 | 8447 | arg_type_tree, |
e440a328 | 8448 | arg_tree); |
8449 | } | |
8450 | else if (arg_type->channel_type() != NULL) | |
8451 | { | |
9f0e0513 | 8452 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 8453 | static tree chan_len_fndecl; |
8454 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
8455 | location, | |
8456 | "__go_chan_len", | |
8457 | 1, | |
1b1f2abf | 8458 | int_type_tree, |
9f0e0513 | 8459 | arg_type_tree, |
e440a328 | 8460 | arg_tree); |
8461 | } | |
8462 | else | |
c3e6f413 | 8463 | go_unreachable(); |
e440a328 | 8464 | } |
8465 | else | |
8466 | { | |
8467 | if (arg_type->array_type() != NULL) | |
0f914071 | 8468 | { |
8469 | if (this->seen_) | |
8470 | { | |
c484d925 | 8471 | go_assert(saw_errors()); |
0f914071 | 8472 | return error_mark_node; |
8473 | } | |
8474 | this->seen_ = true; | |
8475 | val_tree = arg_type->array_type()->capacity_tree(gogo, | |
8476 | arg_tree); | |
8477 | this->seen_ = false; | |
8478 | } | |
e440a328 | 8479 | else if (arg_type->channel_type() != NULL) |
8480 | { | |
9f0e0513 | 8481 | tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo)); |
e440a328 | 8482 | static tree chan_cap_fndecl; |
8483 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
8484 | location, | |
8485 | "__go_chan_cap", | |
8486 | 1, | |
1b1f2abf | 8487 | int_type_tree, |
9f0e0513 | 8488 | arg_type_tree, |
e440a328 | 8489 | arg_tree); |
8490 | } | |
8491 | else | |
c3e6f413 | 8492 | go_unreachable(); |
e440a328 | 8493 | } |
8494 | ||
1b1f2abf | 8495 | return fold_convert_loc(location.gcc_location(), int_type_tree, |
8496 | val_tree); | |
e440a328 | 8497 | } |
8498 | ||
8499 | case BUILTIN_PRINT: | |
8500 | case BUILTIN_PRINTLN: | |
8501 | { | |
8502 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
8503 | tree stmt_list = NULL_TREE; | |
8504 | ||
8505 | const Expression_list* call_args = this->args(); | |
8506 | if (call_args != NULL) | |
8507 | { | |
8508 | for (Expression_list::const_iterator p = call_args->begin(); | |
8509 | p != call_args->end(); | |
8510 | ++p) | |
8511 | { | |
8512 | if (is_ln && p != call_args->begin()) | |
8513 | { | |
8514 | static tree print_space_fndecl; | |
8515 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
8516 | location, | |
8517 | "__go_print_space", | |
8518 | 0, | |
8519 | void_type_node); | |
5fb82b5e | 8520 | if (call == error_mark_node) |
8521 | return error_mark_node; | |
e440a328 | 8522 | append_to_statement_list(call, &stmt_list); |
8523 | } | |
8524 | ||
8525 | Type* type = (*p)->type(); | |
8526 | ||
8527 | tree arg = (*p)->get_tree(context); | |
8528 | if (arg == error_mark_node) | |
8529 | return error_mark_node; | |
8530 | ||
8531 | tree* pfndecl; | |
8532 | const char* fnname; | |
8533 | if (type->is_string_type()) | |
8534 | { | |
8535 | static tree print_string_fndecl; | |
8536 | pfndecl = &print_string_fndecl; | |
8537 | fnname = "__go_print_string"; | |
8538 | } | |
8539 | else if (type->integer_type() != NULL | |
8540 | && type->integer_type()->is_unsigned()) | |
8541 | { | |
8542 | static tree print_uint64_fndecl; | |
8543 | pfndecl = &print_uint64_fndecl; | |
8544 | fnname = "__go_print_uint64"; | |
8545 | Type* itype = Type::lookup_integer_type("uint64"); | |
9f0e0513 | 8546 | Btype* bitype = itype->get_backend(gogo); |
b13c66cd | 8547 | arg = fold_convert_loc(location.gcc_location(), |
8548 | type_to_tree(bitype), arg); | |
e440a328 | 8549 | } |
8550 | else if (type->integer_type() != NULL) | |
8551 | { | |
8552 | static tree print_int64_fndecl; | |
8553 | pfndecl = &print_int64_fndecl; | |
8554 | fnname = "__go_print_int64"; | |
8555 | Type* itype = Type::lookup_integer_type("int64"); | |
9f0e0513 | 8556 | Btype* bitype = itype->get_backend(gogo); |
b13c66cd | 8557 | arg = fold_convert_loc(location.gcc_location(), |
8558 | type_to_tree(bitype), arg); | |
e440a328 | 8559 | } |
8560 | else if (type->float_type() != NULL) | |
8561 | { | |
8562 | static tree print_double_fndecl; | |
8563 | pfndecl = &print_double_fndecl; | |
8564 | fnname = "__go_print_double"; | |
b13c66cd | 8565 | arg = fold_convert_loc(location.gcc_location(), |
8566 | double_type_node, arg); | |
e440a328 | 8567 | } |
8568 | else if (type->complex_type() != NULL) | |
8569 | { | |
8570 | static tree print_complex_fndecl; | |
8571 | pfndecl = &print_complex_fndecl; | |
8572 | fnname = "__go_print_complex"; | |
b13c66cd | 8573 | arg = fold_convert_loc(location.gcc_location(), |
8574 | complex_double_type_node, arg); | |
e440a328 | 8575 | } |
8576 | else if (type->is_boolean_type()) | |
8577 | { | |
8578 | static tree print_bool_fndecl; | |
8579 | pfndecl = &print_bool_fndecl; | |
8580 | fnname = "__go_print_bool"; | |
8581 | } | |
8582 | else if (type->points_to() != NULL | |
8583 | || type->channel_type() != NULL | |
8584 | || type->map_type() != NULL | |
8585 | || type->function_type() != NULL) | |
8586 | { | |
8587 | static tree print_pointer_fndecl; | |
8588 | pfndecl = &print_pointer_fndecl; | |
8589 | fnname = "__go_print_pointer"; | |
b13c66cd | 8590 | arg = fold_convert_loc(location.gcc_location(), |
8591 | ptr_type_node, arg); | |
e440a328 | 8592 | } |
8593 | else if (type->interface_type() != NULL) | |
8594 | { | |
8595 | if (type->interface_type()->is_empty()) | |
8596 | { | |
8597 | static tree print_empty_interface_fndecl; | |
8598 | pfndecl = &print_empty_interface_fndecl; | |
8599 | fnname = "__go_print_empty_interface"; | |
8600 | } | |
8601 | else | |
8602 | { | |
8603 | static tree print_interface_fndecl; | |
8604 | pfndecl = &print_interface_fndecl; | |
8605 | fnname = "__go_print_interface"; | |
8606 | } | |
8607 | } | |
411eb89e | 8608 | else if (type->is_slice_type()) |
e440a328 | 8609 | { |
8610 | static tree print_slice_fndecl; | |
8611 | pfndecl = &print_slice_fndecl; | |
8612 | fnname = "__go_print_slice"; | |
8613 | } | |
8614 | else | |
cd238b8d | 8615 | { |
8616 | go_assert(saw_errors()); | |
8617 | return error_mark_node; | |
8618 | } | |
e440a328 | 8619 | |
8620 | tree call = Gogo::call_builtin(pfndecl, | |
8621 | location, | |
8622 | fnname, | |
8623 | 1, | |
8624 | void_type_node, | |
8625 | TREE_TYPE(arg), | |
8626 | arg); | |
5fb82b5e | 8627 | if (call == error_mark_node) |
8628 | return error_mark_node; | |
8629 | append_to_statement_list(call, &stmt_list); | |
e440a328 | 8630 | } |
8631 | } | |
8632 | ||
8633 | if (is_ln) | |
8634 | { | |
8635 | static tree print_nl_fndecl; | |
8636 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
8637 | location, | |
8638 | "__go_print_nl", | |
8639 | 0, | |
8640 | void_type_node); | |
5fb82b5e | 8641 | if (call == error_mark_node) |
8642 | return error_mark_node; | |
e440a328 | 8643 | append_to_statement_list(call, &stmt_list); |
8644 | } | |
8645 | ||
8646 | return stmt_list; | |
8647 | } | |
8648 | ||
8649 | case BUILTIN_PANIC: | |
8650 | { | |
8651 | const Expression_list* args = this->args(); | |
c484d925 | 8652 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8653 | Expression* arg = args->front(); |
8654 | tree arg_tree = arg->get_tree(context); | |
8655 | if (arg_tree == error_mark_node) | |
8656 | return error_mark_node; | |
b13c66cd | 8657 | Type *empty = |
823c7e3d | 8658 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
e440a328 | 8659 | arg_tree = Expression::convert_for_assignment(context, empty, |
8660 | arg->type(), | |
8661 | arg_tree, location); | |
8662 | static tree panic_fndecl; | |
8663 | tree call = Gogo::call_builtin(&panic_fndecl, | |
8664 | location, | |
8665 | "__go_panic", | |
8666 | 1, | |
8667 | void_type_node, | |
8668 | TREE_TYPE(arg_tree), | |
8669 | arg_tree); | |
5fb82b5e | 8670 | if (call == error_mark_node) |
8671 | return error_mark_node; | |
e440a328 | 8672 | // This function will throw an exception. |
8673 | TREE_NOTHROW(panic_fndecl) = 0; | |
8674 | // This function will not return. | |
8675 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
8676 | return call; | |
8677 | } | |
8678 | ||
8679 | case BUILTIN_RECOVER: | |
8680 | { | |
8681 | // The argument is set when building recover thunks. It's a | |
8682 | // boolean value which is true if we can recover a value now. | |
8683 | const Expression_list* args = this->args(); | |
c484d925 | 8684 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8685 | Expression* arg = args->front(); |
8686 | tree arg_tree = arg->get_tree(context); | |
8687 | if (arg_tree == error_mark_node) | |
8688 | return error_mark_node; | |
8689 | ||
b13c66cd | 8690 | Type *empty = |
823c7e3d | 8691 | Type::make_empty_interface_type(Linemap::predeclared_location()); |
9f0e0513 | 8692 | tree empty_tree = type_to_tree(empty->get_backend(context->gogo())); |
e440a328 | 8693 | |
8694 | Type* nil_type = Type::make_nil_type(); | |
8695 | Expression* nil = Expression::make_nil(location); | |
8696 | tree nil_tree = nil->get_tree(context); | |
8697 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
8698 | empty, | |
8699 | nil_type, | |
8700 | nil_tree, | |
8701 | location); | |
8702 | ||
8703 | // We need to handle a deferred call to recover specially, | |
8704 | // because it changes whether it can recover a panic or not. | |
8705 | // See test7 in test/recover1.go. | |
8706 | tree call; | |
8707 | if (this->is_deferred()) | |
8708 | { | |
8709 | static tree deferred_recover_fndecl; | |
8710 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
8711 | location, | |
8712 | "__go_deferred_recover", | |
8713 | 0, | |
8714 | empty_tree); | |
8715 | } | |
8716 | else | |
8717 | { | |
8718 | static tree recover_fndecl; | |
8719 | call = Gogo::call_builtin(&recover_fndecl, | |
8720 | location, | |
8721 | "__go_recover", | |
8722 | 0, | |
8723 | empty_tree); | |
8724 | } | |
5fb82b5e | 8725 | if (call == error_mark_node) |
8726 | return error_mark_node; | |
b13c66cd | 8727 | return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree, |
8728 | arg_tree, call, empty_nil_tree); | |
e440a328 | 8729 | } |
8730 | ||
8731 | case BUILTIN_CLOSE: | |
e440a328 | 8732 | { |
8733 | const Expression_list* args = this->args(); | |
c484d925 | 8734 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8735 | Expression* arg = args->front(); |
8736 | tree arg_tree = arg->get_tree(context); | |
8737 | if (arg_tree == error_mark_node) | |
8738 | return error_mark_node; | |
0dc2f918 | 8739 | static tree close_fndecl; |
8740 | return Gogo::call_builtin(&close_fndecl, | |
8741 | location, | |
8742 | "__go_builtin_close", | |
8743 | 1, | |
8744 | void_type_node, | |
8745 | TREE_TYPE(arg_tree), | |
8746 | arg_tree); | |
e440a328 | 8747 | } |
8748 | ||
8749 | case BUILTIN_SIZEOF: | |
8750 | case BUILTIN_OFFSETOF: | |
8751 | case BUILTIN_ALIGNOF: | |
8752 | { | |
0c77715b | 8753 | Numeric_constant nc; |
8754 | unsigned long val; | |
8755 | if (!this->numeric_constant_value(&nc) | |
8756 | || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID) | |
7f1d9abd | 8757 | { |
c484d925 | 8758 | go_assert(saw_errors()); |
7f1d9abd | 8759 | return error_mark_node; |
8760 | } | |
7ba86326 | 8761 | Type* uintptr_type = Type::lookup_integer_type("uintptr"); |
8762 | tree type = type_to_tree(uintptr_type->get_backend(gogo)); | |
0c77715b | 8763 | return build_int_cst(type, val); |
e440a328 | 8764 | } |
8765 | ||
8766 | case BUILTIN_COPY: | |
8767 | { | |
8768 | const Expression_list* args = this->args(); | |
c484d925 | 8769 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8770 | Expression* arg1 = args->front(); |
8771 | Expression* arg2 = args->back(); | |
8772 | ||
8773 | tree arg1_tree = arg1->get_tree(context); | |
8774 | tree arg2_tree = arg2->get_tree(context); | |
8775 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8776 | return error_mark_node; | |
8777 | ||
8778 | Type* arg1_type = arg1->type(); | |
8779 | Array_type* at = arg1_type->array_type(); | |
8780 | arg1_tree = save_expr(arg1_tree); | |
8781 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
8782 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
d8ccb1e3 | 8783 | if (arg1_val == error_mark_node || arg1_len == error_mark_node) |
8784 | return error_mark_node; | |
e440a328 | 8785 | |
8786 | Type* arg2_type = arg2->type(); | |
8787 | tree arg2_val; | |
8788 | tree arg2_len; | |
411eb89e | 8789 | if (arg2_type->is_slice_type()) |
e440a328 | 8790 | { |
8791 | at = arg2_type->array_type(); | |
8792 | arg2_tree = save_expr(arg2_tree); | |
8793 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8794 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8795 | } | |
8796 | else | |
8797 | { | |
8798 | arg2_tree = save_expr(arg2_tree); | |
8799 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8800 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8801 | } | |
d8ccb1e3 | 8802 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8803 | return error_mark_node; | |
e440a328 | 8804 | |
8805 | arg1_len = save_expr(arg1_len); | |
8806 | arg2_len = save_expr(arg2_len); | |
b13c66cd | 8807 | tree len = fold_build3_loc(location.gcc_location(), COND_EXPR, |
8808 | TREE_TYPE(arg1_len), | |
8809 | fold_build2_loc(location.gcc_location(), | |
8810 | LT_EXPR, boolean_type_node, | |
e440a328 | 8811 | arg1_len, arg2_len), |
8812 | arg1_len, arg2_len); | |
8813 | len = save_expr(len); | |
8814 | ||
8815 | Type* element_type = at->element_type(); | |
9f0e0513 | 8816 | Btype* element_btype = element_type->get_backend(gogo); |
8817 | tree element_type_tree = type_to_tree(element_btype); | |
d8ccb1e3 | 8818 | if (element_type_tree == error_mark_node) |
8819 | return error_mark_node; | |
e440a328 | 8820 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
b13c66cd | 8821 | tree bytecount = fold_convert_loc(location.gcc_location(), |
8822 | TREE_TYPE(element_size), len); | |
8823 | bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR, | |
e440a328 | 8824 | TREE_TYPE(element_size), |
8825 | bytecount, element_size); | |
b13c66cd | 8826 | bytecount = fold_convert_loc(location.gcc_location(), size_type_node, |
8827 | bytecount); | |
e440a328 | 8828 | |
b13c66cd | 8829 | arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node, |
8830 | arg1_val); | |
8831 | arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node, | |
8832 | arg2_val); | |
3991cb03 | 8833 | |
8834 | static tree copy_fndecl; | |
8835 | tree call = Gogo::call_builtin(©_fndecl, | |
8836 | location, | |
8837 | "__go_copy", | |
8838 | 3, | |
8839 | void_type_node, | |
8840 | ptr_type_node, | |
8841 | arg1_val, | |
8842 | ptr_type_node, | |
8843 | arg2_val, | |
8844 | size_type_node, | |
8845 | bytecount); | |
8846 | if (call == error_mark_node) | |
8847 | return error_mark_node; | |
e440a328 | 8848 | |
b13c66cd | 8849 | return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR, |
8850 | TREE_TYPE(len), call, len); | |
e440a328 | 8851 | } |
8852 | ||
8853 | case BUILTIN_APPEND: | |
8854 | { | |
8855 | const Expression_list* args = this->args(); | |
c484d925 | 8856 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8857 | Expression* arg1 = args->front(); |
8858 | Expression* arg2 = args->back(); | |
8859 | ||
8860 | tree arg1_tree = arg1->get_tree(context); | |
8861 | tree arg2_tree = arg2->get_tree(context); | |
8862 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8863 | return error_mark_node; | |
8864 | ||
9d44fbe3 | 8865 | Array_type* at = arg1->type()->array_type(); |
4fd4fcf4 | 8866 | Type* element_type = at->element_type()->forwarded(); |
9d44fbe3 | 8867 | |
4fd4fcf4 | 8868 | tree arg2_val; |
8869 | tree arg2_len; | |
8870 | tree element_size; | |
8871 | if (arg2->type()->is_string_type() | |
60963afd | 8872 | && element_type->integer_type() != NULL |
8873 | && element_type->integer_type()->is_byte()) | |
4fd4fcf4 | 8874 | { |
8875 | arg2_tree = save_expr(arg2_tree); | |
8876 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8877 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8878 | element_size = size_int(1); | |
8879 | } | |
8880 | else | |
8881 | { | |
8882 | arg2_tree = Expression::convert_for_assignment(context, at, | |
8883 | arg2->type(), | |
8884 | arg2_tree, | |
8885 | location); | |
8886 | if (arg2_tree == error_mark_node) | |
8887 | return error_mark_node; | |
8888 | ||
8889 | arg2_tree = save_expr(arg2_tree); | |
8890 | ||
8891 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8892 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8893 | ||
8894 | Btype* element_btype = element_type->get_backend(gogo); | |
8895 | tree element_type_tree = type_to_tree(element_btype); | |
8896 | if (element_type_tree == error_mark_node) | |
8897 | return error_mark_node; | |
8898 | element_size = TYPE_SIZE_UNIT(element_type_tree); | |
8899 | } | |
ed64c8e5 | 8900 | |
b13c66cd | 8901 | arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node, |
8902 | arg2_val); | |
8903 | arg2_len = fold_convert_loc(location.gcc_location(), size_type_node, | |
8904 | arg2_len); | |
8905 | element_size = fold_convert_loc(location.gcc_location(), size_type_node, | |
3991cb03 | 8906 | element_size); |
e440a328 | 8907 | |
4fd4fcf4 | 8908 | if (arg2_val == error_mark_node |
8909 | || arg2_len == error_mark_node | |
8910 | || element_size == error_mark_node) | |
8911 | return error_mark_node; | |
8912 | ||
e440a328 | 8913 | // We rebuild the decl each time since the slice types may |
8914 | // change. | |
8915 | tree append_fndecl = NULL_TREE; | |
8916 | return Gogo::call_builtin(&append_fndecl, | |
8917 | location, | |
8918 | "__go_append", | |
3991cb03 | 8919 | 4, |
e440a328 | 8920 | TREE_TYPE(arg1_tree), |
e440a328 | 8921 | TREE_TYPE(arg1_tree), |
8922 | arg1_tree, | |
3991cb03 | 8923 | ptr_type_node, |
8924 | arg2_val, | |
8925 | size_type_node, | |
8926 | arg2_len, | |
8927 | size_type_node, | |
8928 | element_size); | |
e440a328 | 8929 | } |
8930 | ||
8931 | case BUILTIN_REAL: | |
8932 | case BUILTIN_IMAG: | |
8933 | { | |
8934 | const Expression_list* args = this->args(); | |
c484d925 | 8935 | go_assert(args != NULL && args->size() == 1); |
e440a328 | 8936 | Expression* arg = args->front(); |
8937 | tree arg_tree = arg->get_tree(context); | |
8938 | if (arg_tree == error_mark_node) | |
8939 | return error_mark_node; | |
c484d925 | 8940 | go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); |
e440a328 | 8941 | if (this->code_ == BUILTIN_REAL) |
b13c66cd | 8942 | return fold_build1_loc(location.gcc_location(), REALPART_EXPR, |
e440a328 | 8943 | TREE_TYPE(TREE_TYPE(arg_tree)), |
8944 | arg_tree); | |
8945 | else | |
b13c66cd | 8946 | return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR, |
e440a328 | 8947 | TREE_TYPE(TREE_TYPE(arg_tree)), |
8948 | arg_tree); | |
8949 | } | |
8950 | ||
48080209 | 8951 | case BUILTIN_COMPLEX: |
e440a328 | 8952 | { |
8953 | const Expression_list* args = this->args(); | |
c484d925 | 8954 | go_assert(args != NULL && args->size() == 2); |
e440a328 | 8955 | tree r = args->front()->get_tree(context); |
8956 | tree i = args->back()->get_tree(context); | |
8957 | if (r == error_mark_node || i == error_mark_node) | |
8958 | return error_mark_node; | |
c484d925 | 8959 | go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) |
e440a328 | 8960 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); |
c484d925 | 8961 | go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); |
b13c66cd | 8962 | return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR, |
e440a328 | 8963 | build_complex_type(TREE_TYPE(r)), |
8964 | r, i); | |
8965 | } | |
8966 | ||
8967 | default: | |
c3e6f413 | 8968 | go_unreachable(); |
e440a328 | 8969 | } |
8970 | } | |
8971 | ||
8972 | // We have to support exporting a builtin call expression, because | |
8973 | // code can set a constant to the result of a builtin expression. | |
8974 | ||
8975 | void | |
8976 | Builtin_call_expression::do_export(Export* exp) const | |
8977 | { | |
0c77715b | 8978 | Numeric_constant nc; |
8979 | if (!this->numeric_constant_value(&nc)) | |
8980 | { | |
8981 | error_at(this->location(), "value is not constant"); | |
8982 | return; | |
8983 | } | |
e440a328 | 8984 | |
0c77715b | 8985 | if (nc.is_int()) |
e440a328 | 8986 | { |
0c77715b | 8987 | mpz_t val; |
8988 | nc.get_int(&val); | |
e440a328 | 8989 | Integer_expression::export_integer(exp, val); |
0c77715b | 8990 | mpz_clear(val); |
e440a328 | 8991 | } |
0c77715b | 8992 | else if (nc.is_float()) |
e440a328 | 8993 | { |
8994 | mpfr_t fval; | |
0c77715b | 8995 | nc.get_float(&fval); |
8996 | Float_expression::export_float(exp, fval); | |
e440a328 | 8997 | mpfr_clear(fval); |
8998 | } | |
0c77715b | 8999 | else if (nc.is_complex()) |
e440a328 | 9000 | { |
9001 | mpfr_t real; | |
9002 | mpfr_t imag; | |
0c77715b | 9003 | Complex_expression::export_complex(exp, real, imag); |
e440a328 | 9004 | mpfr_clear(real); |
9005 | mpfr_clear(imag); | |
9006 | } | |
0c77715b | 9007 | else |
9008 | go_unreachable(); | |
e440a328 | 9009 | |
9010 | // A trailing space lets us reliably identify the end of the number. | |
9011 | exp->write_c_string(" "); | |
9012 | } | |
9013 | ||
9014 | // Class Call_expression. | |
9015 | ||
8381eda7 | 9016 | // A Go function can be viewed in a couple of different ways. The |
9017 | // code of a Go function becomes a backend function with parameters | |
9018 | // whose types are simply the backend representation of the Go types. | |
9019 | // If there are multiple results, they are returned as a backend | |
9020 | // struct. | |
9021 | ||
9022 | // However, when Go code refers to a function other than simply | |
9023 | // calling it, the backend type of that function is actually a struct. | |
9024 | // The first field of the struct points to the Go function code | |
9025 | // (sometimes a wrapper as described below). The remaining fields | |
9026 | // hold addresses of closed-over variables. This struct is called a | |
9027 | // closure. | |
9028 | ||
9029 | // There are a few cases to consider. | |
9030 | ||
9031 | // A direct function call of a known function in package scope. In | |
9032 | // this case there are no closed-over variables, and we know the name | |
9033 | // of the function code. We can simply produce a backend call to the | |
9034 | // function directly, and not worry about the closure. | |
9035 | ||
9036 | // A direct function call of a known function literal. In this case | |
9037 | // we know the function code and we know the closure. We generate the | |
9038 | // function code such that it expects an additional final argument of | |
9039 | // the closure type. We pass the closure as the last argument, after | |
9040 | // the other arguments. | |
9041 | ||
9042 | // An indirect function call. In this case we have a closure. We | |
9043 | // load the pointer to the function code from the first field of the | |
9044 | // closure. We pass the address of the closure as the last argument. | |
9045 | ||
9046 | // A call to a method of an interface. Type methods are always at | |
9047 | // package scope, so we call the function directly, and don't worry | |
9048 | // about the closure. | |
9049 | ||
9050 | // This means that for a function at package scope we have two cases. | |
9051 | // One is the direct call, which has no closure. The other is the | |
9052 | // indirect call, which does have a closure. We can't simply ignore | |
9053 | // the closure, even though it is the last argument, because that will | |
9054 | // fail on targets where the function pops its arguments. So when | |
9055 | // generating a closure for a package-scope function we set the | |
9056 | // function code pointer in the closure to point to a wrapper | |
9057 | // function. This wrapper function accepts a final argument that | |
9058 | // points to the closure, ignores it, and calls the real function as a | |
9059 | // direct function call. This wrapper will normally be efficient, and | |
9060 | // can often simply be a tail call to the real function. | |
9061 | ||
9062 | // We don't use GCC's static chain pointer because 1) we don't need | |
9063 | // it; 2) GCC only permits using a static chain to call a known | |
9064 | // function, so we can't use it for an indirect call anyhow. Since we | |
9065 | // can't use it for an indirect call, we may as well not worry about | |
9066 | // using it for a direct call either. | |
9067 | ||
9068 | // We pass the closure last rather than first because it means that | |
9069 | // the function wrapper we put into a closure for a package-scope | |
9070 | // function can normally just be a tail call to the real function. | |
9071 | ||
9072 | // For method expressions we generate a wrapper that loads the | |
9073 | // receiver from the closure and then calls the method. This | |
9074 | // unfortunately forces reshuffling the arguments, since there is a | |
9075 | // new first argument, but we can't avoid reshuffling either for | |
9076 | // method expressions or for indirect calls of package-scope | |
9077 | // functions, and since the latter are more common we reshuffle for | |
9078 | // method expressions. | |
9079 | ||
9080 | // Note that the Go code retains the Go types. The extra final | |
9081 | // argument only appears when we convert to the backend | |
9082 | // representation. | |
9083 | ||
e440a328 | 9084 | // Traversal. |
9085 | ||
9086 | int | |
9087 | Call_expression::do_traverse(Traverse* traverse) | |
9088 | { | |
9089 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
9090 | return TRAVERSE_EXIT; | |
9091 | if (this->args_ != NULL) | |
9092 | { | |
9093 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
9094 | return TRAVERSE_EXIT; | |
9095 | } | |
9096 | return TRAVERSE_CONTINUE; | |
9097 | } | |
9098 | ||
9099 | // Lower a call statement. | |
9100 | ||
9101 | Expression* | |
ceeb4318 | 9102 | Call_expression::do_lower(Gogo* gogo, Named_object* function, |
9103 | Statement_inserter* inserter, int) | |
e440a328 | 9104 | { |
b13c66cd | 9105 | Location loc = this->location(); |
09ea332d | 9106 | |
ceeb4318 | 9107 | // A type cast can look like a function call. |
e440a328 | 9108 | if (this->fn_->is_type_expression() |
9109 | && this->args_ != NULL | |
9110 | && this->args_->size() == 1) | |
9111 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
09ea332d | 9112 | loc); |
e440a328 | 9113 | |
88f06749 | 9114 | // Because do_type will return an error type and thus prevent future |
9115 | // errors, check for that case now to ensure that the error gets | |
9116 | // reported. | |
37448b10 | 9117 | Function_type* fntype = this->get_function_type(); |
9118 | if (fntype == NULL) | |
88f06749 | 9119 | { |
9120 | if (!this->fn_->type()->is_error()) | |
9121 | this->report_error(_("expected function")); | |
9122 | return Expression::make_error(loc); | |
9123 | } | |
9124 | ||
e440a328 | 9125 | // Handle an argument which is a call to a function which returns |
9126 | // multiple results. | |
9127 | if (this->args_ != NULL | |
9128 | && this->args_->size() == 1 | |
37448b10 | 9129 | && this->args_->front()->call_expression() != NULL) |
e440a328 | 9130 | { |
e440a328 | 9131 | size_t rc = this->args_->front()->call_expression()->result_count(); |
9132 | if (rc > 1 | |
37448b10 | 9133 | && ((fntype->parameters() != NULL |
9134 | && (fntype->parameters()->size() == rc | |
9135 | || (fntype->is_varargs() | |
9136 | && fntype->parameters()->size() - 1 <= rc))) | |
9137 | || fntype->is_builtin())) | |
e440a328 | 9138 | { |
9139 | Call_expression* call = this->args_->front()->call_expression(); | |
9140 | Expression_list* args = new Expression_list; | |
9141 | for (size_t i = 0; i < rc; ++i) | |
9142 | args->push_back(Expression::make_call_result(call, i)); | |
9143 | // We can't return a new call expression here, because this | |
42535814 | 9144 | // one may be referenced by Call_result expressions. We |
9145 | // also can't delete the old arguments, because we may still | |
9146 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 9147 | this->args_ = args; |
9148 | } | |
9149 | } | |
9150 | ||
37448b10 | 9151 | // Recognize a call to a builtin function. |
9152 | if (fntype->is_builtin()) | |
9153 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
9154 | this->is_varargs_, loc); | |
9155 | ||
ceeb4318 | 9156 | // If this call returns multiple results, create a temporary |
9157 | // variable for each result. | |
9158 | size_t rc = this->result_count(); | |
9159 | if (rc > 1 && this->results_ == NULL) | |
9160 | { | |
9161 | std::vector<Temporary_statement*>* temps = | |
9162 | new std::vector<Temporary_statement*>; | |
9163 | temps->reserve(rc); | |
37448b10 | 9164 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 9165 | for (Typed_identifier_list::const_iterator p = results->begin(); |
9166 | p != results->end(); | |
9167 | ++p) | |
9168 | { | |
9169 | Temporary_statement* temp = Statement::make_temporary(p->type(), | |
09ea332d | 9170 | NULL, loc); |
ceeb4318 | 9171 | inserter->insert(temp); |
9172 | temps->push_back(temp); | |
9173 | } | |
9174 | this->results_ = temps; | |
9175 | } | |
9176 | ||
e440a328 | 9177 | // Handle a call to a varargs function by packaging up the extra |
9178 | // parameters. | |
37448b10 | 9179 | if (fntype->is_varargs()) |
e440a328 | 9180 | { |
e440a328 | 9181 | const Typed_identifier_list* parameters = fntype->parameters(); |
c484d925 | 9182 | go_assert(parameters != NULL && !parameters->empty()); |
e440a328 | 9183 | Type* varargs_type = parameters->back().type(); |
09ea332d | 9184 | this->lower_varargs(gogo, function, inserter, varargs_type, |
9185 | parameters->size()); | |
9186 | } | |
9187 | ||
9188 | // If this is call to a method, call the method directly passing the | |
9189 | // object as the first parameter. | |
9190 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
9191 | if (bme != NULL) | |
9192 | { | |
0afbb937 | 9193 | Named_object* methodfn = bme->function(); |
09ea332d | 9194 | Expression* first_arg = bme->first_argument(); |
9195 | ||
9196 | // We always pass a pointer when calling a method. | |
9197 | if (first_arg->type()->points_to() == NULL | |
9198 | && !first_arg->type()->is_error()) | |
9199 | { | |
9200 | first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc); | |
9201 | // We may need to create a temporary variable so that we can | |
9202 | // take the address. We can't do that here because it will | |
9203 | // mess up the order of evaluation. | |
9204 | Unary_expression* ue = static_cast<Unary_expression*>(first_arg); | |
9205 | ue->set_create_temp(); | |
9206 | } | |
9207 | ||
9208 | // If we are calling a method which was inherited from an | |
9209 | // embedded struct, and the method did not get a stub, then the | |
9210 | // first type may be wrong. | |
9211 | Type* fatype = bme->first_argument_type(); | |
9212 | if (fatype != NULL) | |
9213 | { | |
9214 | if (fatype->points_to() == NULL) | |
9215 | fatype = Type::make_pointer_type(fatype); | |
9216 | first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc); | |
9217 | } | |
9218 | ||
9219 | Expression_list* new_args = new Expression_list(); | |
9220 | new_args->push_back(first_arg); | |
9221 | if (this->args_ != NULL) | |
9222 | { | |
9223 | for (Expression_list::const_iterator p = this->args_->begin(); | |
9224 | p != this->args_->end(); | |
9225 | ++p) | |
9226 | new_args->push_back(*p); | |
9227 | } | |
9228 | ||
9229 | // We have to change in place because this structure may be | |
9230 | // referenced by Call_result_expressions. We can't delete the | |
9231 | // old arguments, because we may be traversing them up in some | |
9232 | // caller. FIXME. | |
9233 | this->args_ = new_args; | |
0afbb937 | 9234 | this->fn_ = Expression::make_func_reference(methodfn, NULL, |
09ea332d | 9235 | bme->location()); |
e440a328 | 9236 | } |
9237 | ||
9238 | return this; | |
9239 | } | |
9240 | ||
9241 | // Lower a call to a varargs function. FUNCTION is the function in | |
9242 | // which the call occurs--it's not the function we are calling. | |
9243 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
9244 | // PARAM_COUNT is the number of parameters of the function we are | |
9245 | // calling; the last of these parameters will be the varargs | |
9246 | // parameter. | |
9247 | ||
09ea332d | 9248 | void |
e440a328 | 9249 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, |
ceeb4318 | 9250 | Statement_inserter* inserter, |
e440a328 | 9251 | Type* varargs_type, size_t param_count) |
9252 | { | |
9253 | if (this->varargs_are_lowered_) | |
09ea332d | 9254 | return; |
e440a328 | 9255 | |
b13c66cd | 9256 | Location loc = this->location(); |
e440a328 | 9257 | |
c484d925 | 9258 | go_assert(param_count > 0); |
411eb89e | 9259 | go_assert(varargs_type->is_slice_type()); |
e440a328 | 9260 | |
9261 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
9262 | if (arg_count < param_count - 1) | |
9263 | { | |
9264 | // Not enough arguments; will be caught in check_types. | |
09ea332d | 9265 | return; |
e440a328 | 9266 | } |
9267 | ||
9268 | Expression_list* old_args = this->args_; | |
9269 | Expression_list* new_args = new Expression_list(); | |
9270 | bool push_empty_arg = false; | |
9271 | if (old_args == NULL || old_args->empty()) | |
9272 | { | |
c484d925 | 9273 | go_assert(param_count == 1); |
e440a328 | 9274 | push_empty_arg = true; |
9275 | } | |
9276 | else | |
9277 | { | |
9278 | Expression_list::const_iterator pa; | |
9279 | int i = 1; | |
9280 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
9281 | { | |
9282 | if (static_cast<size_t>(i) == param_count) | |
9283 | break; | |
9284 | new_args->push_back(*pa); | |
9285 | } | |
9286 | ||
9287 | // We have reached the varargs parameter. | |
9288 | ||
9289 | bool issued_error = false; | |
9290 | if (pa == old_args->end()) | |
9291 | push_empty_arg = true; | |
9292 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
9293 | new_args->push_back(*pa); | |
9294 | else if (this->is_varargs_) | |
9295 | { | |
a6645f74 | 9296 | if ((*pa)->type()->is_slice_type()) |
9297 | this->report_error(_("too many arguments")); | |
9298 | else | |
9299 | { | |
9300 | error_at(this->location(), | |
9301 | _("invalid use of %<...%> with non-slice")); | |
9302 | this->set_is_error(); | |
9303 | } | |
09ea332d | 9304 | return; |
e440a328 | 9305 | } |
e440a328 | 9306 | else |
9307 | { | |
9308 | Type* element_type = varargs_type->array_type()->element_type(); | |
9309 | Expression_list* vals = new Expression_list; | |
9310 | for (; pa != old_args->end(); ++pa, ++i) | |
9311 | { | |
9312 | // Check types here so that we get a better message. | |
9313 | Type* patype = (*pa)->type(); | |
b13c66cd | 9314 | Location paloc = (*pa)->location(); |
e440a328 | 9315 | if (!this->check_argument_type(i, element_type, patype, |
9316 | paloc, issued_error)) | |
9317 | continue; | |
9318 | vals->push_back(*pa); | |
9319 | } | |
9320 | Expression* val = | |
9321 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
09ea332d | 9322 | gogo->lower_expression(function, inserter, &val); |
e440a328 | 9323 | new_args->push_back(val); |
9324 | } | |
9325 | } | |
9326 | ||
9327 | if (push_empty_arg) | |
9328 | new_args->push_back(Expression::make_nil(loc)); | |
9329 | ||
9330 | // We can't return a new call expression here, because this one may | |
6d4c2432 | 9331 | // be referenced by Call_result expressions. FIXME. We can't |
9332 | // delete OLD_ARGS because we may have both a Call_expression and a | |
9333 | // Builtin_call_expression which refer to them. FIXME. | |
e440a328 | 9334 | this->args_ = new_args; |
9335 | this->varargs_are_lowered_ = true; | |
e440a328 | 9336 | } |
9337 | ||
ceeb4318 | 9338 | // Get the function type. This can return NULL in error cases. |
e440a328 | 9339 | |
9340 | Function_type* | |
9341 | Call_expression::get_function_type() const | |
9342 | { | |
9343 | return this->fn_->type()->function_type(); | |
9344 | } | |
9345 | ||
9346 | // Return the number of values which this call will return. | |
9347 | ||
9348 | size_t | |
9349 | Call_expression::result_count() const | |
9350 | { | |
9351 | const Function_type* fntype = this->get_function_type(); | |
9352 | if (fntype == NULL) | |
9353 | return 0; | |
9354 | if (fntype->results() == NULL) | |
9355 | return 0; | |
9356 | return fntype->results()->size(); | |
9357 | } | |
9358 | ||
ceeb4318 | 9359 | // Return the temporary which holds a result. |
9360 | ||
9361 | Temporary_statement* | |
9362 | Call_expression::result(size_t i) const | |
9363 | { | |
cd238b8d | 9364 | if (this->results_ == NULL || this->results_->size() <= i) |
9365 | { | |
9366 | go_assert(saw_errors()); | |
9367 | return NULL; | |
9368 | } | |
ceeb4318 | 9369 | return (*this->results_)[i]; |
9370 | } | |
9371 | ||
e440a328 | 9372 | // Return whether this is a call to the predeclared function recover. |
9373 | ||
9374 | bool | |
9375 | Call_expression::is_recover_call() const | |
9376 | { | |
9377 | return this->do_is_recover_call(); | |
9378 | } | |
9379 | ||
9380 | // Set the argument to the recover function. | |
9381 | ||
9382 | void | |
9383 | Call_expression::set_recover_arg(Expression* arg) | |
9384 | { | |
9385 | this->do_set_recover_arg(arg); | |
9386 | } | |
9387 | ||
9388 | // Virtual functions also implemented by Builtin_call_expression. | |
9389 | ||
9390 | bool | |
9391 | Call_expression::do_is_recover_call() const | |
9392 | { | |
9393 | return false; | |
9394 | } | |
9395 | ||
9396 | void | |
9397 | Call_expression::do_set_recover_arg(Expression*) | |
9398 | { | |
c3e6f413 | 9399 | go_unreachable(); |
e440a328 | 9400 | } |
9401 | ||
ceeb4318 | 9402 | // We have found an error with this call expression; return true if |
9403 | // we should report it. | |
9404 | ||
9405 | bool | |
9406 | Call_expression::issue_error() | |
9407 | { | |
9408 | if (this->issued_error_) | |
9409 | return false; | |
9410 | else | |
9411 | { | |
9412 | this->issued_error_ = true; | |
9413 | return true; | |
9414 | } | |
9415 | } | |
9416 | ||
e440a328 | 9417 | // Get the type. |
9418 | ||
9419 | Type* | |
9420 | Call_expression::do_type() | |
9421 | { | |
9422 | if (this->type_ != NULL) | |
9423 | return this->type_; | |
9424 | ||
9425 | Type* ret; | |
9426 | Function_type* fntype = this->get_function_type(); | |
9427 | if (fntype == NULL) | |
9428 | return Type::make_error_type(); | |
9429 | ||
9430 | const Typed_identifier_list* results = fntype->results(); | |
9431 | if (results == NULL) | |
9432 | ret = Type::make_void_type(); | |
9433 | else if (results->size() == 1) | |
9434 | ret = results->begin()->type(); | |
9435 | else | |
9436 | ret = Type::make_call_multiple_result_type(this); | |
9437 | ||
9438 | this->type_ = ret; | |
9439 | ||
9440 | return this->type_; | |
9441 | } | |
9442 | ||
9443 | // Determine types for a call expression. We can use the function | |
9444 | // parameter types to set the types of the arguments. | |
9445 | ||
9446 | void | |
9447 | Call_expression::do_determine_type(const Type_context*) | |
9448 | { | |
fb94b0ca | 9449 | if (!this->determining_types()) |
9450 | return; | |
9451 | ||
e440a328 | 9452 | this->fn_->determine_type_no_context(); |
9453 | Function_type* fntype = this->get_function_type(); | |
9454 | const Typed_identifier_list* parameters = NULL; | |
9455 | if (fntype != NULL) | |
9456 | parameters = fntype->parameters(); | |
9457 | if (this->args_ != NULL) | |
9458 | { | |
9459 | Typed_identifier_list::const_iterator pt; | |
9460 | if (parameters != NULL) | |
9461 | pt = parameters->begin(); | |
09ea332d | 9462 | bool first = true; |
e440a328 | 9463 | for (Expression_list::const_iterator pa = this->args_->begin(); |
9464 | pa != this->args_->end(); | |
9465 | ++pa) | |
9466 | { | |
09ea332d | 9467 | if (first) |
9468 | { | |
9469 | first = false; | |
9470 | // If this is a method, the first argument is the | |
9471 | // receiver. | |
9472 | if (fntype != NULL && fntype->is_method()) | |
9473 | { | |
9474 | Type* rtype = fntype->receiver()->type(); | |
9475 | // The receiver is always passed as a pointer. | |
9476 | if (rtype->points_to() == NULL) | |
9477 | rtype = Type::make_pointer_type(rtype); | |
9478 | Type_context subcontext(rtype, false); | |
9479 | (*pa)->determine_type(&subcontext); | |
9480 | continue; | |
9481 | } | |
9482 | } | |
9483 | ||
e440a328 | 9484 | if (parameters != NULL && pt != parameters->end()) |
9485 | { | |
9486 | Type_context subcontext(pt->type(), false); | |
9487 | (*pa)->determine_type(&subcontext); | |
9488 | ++pt; | |
9489 | } | |
9490 | else | |
9491 | (*pa)->determine_type_no_context(); | |
9492 | } | |
9493 | } | |
9494 | } | |
9495 | ||
fb94b0ca | 9496 | // Called when determining types for a Call_expression. Return true |
9497 | // if we should go ahead, false if they have already been determined. | |
9498 | ||
9499 | bool | |
9500 | Call_expression::determining_types() | |
9501 | { | |
9502 | if (this->types_are_determined_) | |
9503 | return false; | |
9504 | else | |
9505 | { | |
9506 | this->types_are_determined_ = true; | |
9507 | return true; | |
9508 | } | |
9509 | } | |
9510 | ||
e440a328 | 9511 | // Check types for parameter I. |
9512 | ||
9513 | bool | |
9514 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
9515 | const Type* argument_type, | |
b13c66cd | 9516 | Location argument_location, |
e440a328 | 9517 | bool issued_error) |
9518 | { | |
9519 | std::string reason; | |
053ee6ca | 9520 | bool ok; |
9521 | if (this->are_hidden_fields_ok_) | |
9522 | ok = Type::are_assignable_hidden_ok(parameter_type, argument_type, | |
9523 | &reason); | |
9524 | else | |
9525 | ok = Type::are_assignable(parameter_type, argument_type, &reason); | |
9526 | if (!ok) | |
e440a328 | 9527 | { |
9528 | if (!issued_error) | |
9529 | { | |
9530 | if (reason.empty()) | |
9531 | error_at(argument_location, "argument %d has incompatible type", i); | |
9532 | else | |
9533 | error_at(argument_location, | |
9534 | "argument %d has incompatible type (%s)", | |
9535 | i, reason.c_str()); | |
9536 | } | |
9537 | this->set_is_error(); | |
9538 | return false; | |
9539 | } | |
9540 | return true; | |
9541 | } | |
9542 | ||
9543 | // Check types. | |
9544 | ||
9545 | void | |
9546 | Call_expression::do_check_types(Gogo*) | |
9547 | { | |
a6645f74 | 9548 | if (this->classification() == EXPRESSION_ERROR) |
9549 | return; | |
9550 | ||
e440a328 | 9551 | Function_type* fntype = this->get_function_type(); |
9552 | if (fntype == NULL) | |
9553 | { | |
5c13bd80 | 9554 | if (!this->fn_->type()->is_error()) |
e440a328 | 9555 | this->report_error(_("expected function")); |
9556 | return; | |
9557 | } | |
9558 | ||
09ea332d | 9559 | bool is_method = fntype->is_method(); |
9560 | if (is_method) | |
e440a328 | 9561 | { |
09ea332d | 9562 | go_assert(this->args_ != NULL && !this->args_->empty()); |
9563 | Type* rtype = fntype->receiver()->type(); | |
9564 | Expression* first_arg = this->args_->front(); | |
9565 | // The language permits copying hidden fields for a method | |
9566 | // receiver. We dereference the values since receivers are | |
9567 | // always passed as pointers. | |
9568 | std::string reason; | |
9569 | if (!Type::are_assignable_hidden_ok(rtype->deref(), | |
9570 | first_arg->type()->deref(), | |
9571 | &reason)) | |
e440a328 | 9572 | { |
09ea332d | 9573 | if (reason.empty()) |
9574 | this->report_error(_("incompatible type for receiver")); | |
9575 | else | |
e440a328 | 9576 | { |
09ea332d | 9577 | error_at(this->location(), |
9578 | "incompatible type for receiver (%s)", | |
9579 | reason.c_str()); | |
9580 | this->set_is_error(); | |
e440a328 | 9581 | } |
9582 | } | |
9583 | } | |
9584 | ||
9585 | // Note that varargs was handled by the lower_varargs() method, so | |
a6645f74 | 9586 | // we don't have to worry about it here unless something is wrong. |
9587 | if (this->is_varargs_ && !this->varargs_are_lowered_) | |
9588 | { | |
9589 | if (!fntype->is_varargs()) | |
9590 | { | |
9591 | error_at(this->location(), | |
9592 | _("invalid use of %<...%> calling non-variadic function")); | |
9593 | this->set_is_error(); | |
9594 | return; | |
9595 | } | |
9596 | } | |
e440a328 | 9597 | |
9598 | const Typed_identifier_list* parameters = fntype->parameters(); | |
9599 | if (this->args_ == NULL) | |
9600 | { | |
9601 | if (parameters != NULL && !parameters->empty()) | |
9602 | this->report_error(_("not enough arguments")); | |
9603 | } | |
9604 | else if (parameters == NULL) | |
09ea332d | 9605 | { |
9606 | if (!is_method || this->args_->size() > 1) | |
9607 | this->report_error(_("too many arguments")); | |
9608 | } | |
e440a328 | 9609 | else |
9610 | { | |
9611 | int i = 0; | |
09ea332d | 9612 | Expression_list::const_iterator pa = this->args_->begin(); |
9613 | if (is_method) | |
9614 | ++pa; | |
9615 | for (Typed_identifier_list::const_iterator pt = parameters->begin(); | |
9616 | pt != parameters->end(); | |
9617 | ++pt, ++pa, ++i) | |
e440a328 | 9618 | { |
09ea332d | 9619 | if (pa == this->args_->end()) |
e440a328 | 9620 | { |
09ea332d | 9621 | this->report_error(_("not enough arguments")); |
e440a328 | 9622 | return; |
9623 | } | |
9624 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
9625 | (*pa)->location(), false); | |
9626 | } | |
09ea332d | 9627 | if (pa != this->args_->end()) |
9628 | this->report_error(_("too many arguments")); | |
e440a328 | 9629 | } |
9630 | } | |
9631 | ||
9632 | // Return whether we have to use a temporary variable to ensure that | |
9633 | // we evaluate this call expression in order. If the call returns no | |
ceeb4318 | 9634 | // results then it will inevitably be executed last. |
e440a328 | 9635 | |
9636 | bool | |
9637 | Call_expression::do_must_eval_in_order() const | |
9638 | { | |
ceeb4318 | 9639 | return this->result_count() > 0; |
e440a328 | 9640 | } |
9641 | ||
e440a328 | 9642 | // Get the function and the first argument to use when calling an |
9643 | // interface method. | |
9644 | ||
9645 | tree | |
9646 | Call_expression::interface_method_function( | |
9647 | Translate_context* context, | |
9648 | Interface_field_reference_expression* interface_method, | |
9649 | tree* first_arg_ptr) | |
9650 | { | |
9651 | tree expr = interface_method->expr()->get_tree(context); | |
9652 | if (expr == error_mark_node) | |
9653 | return error_mark_node; | |
9654 | expr = save_expr(expr); | |
9655 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
9656 | if (first_arg == error_mark_node) | |
9657 | return error_mark_node; | |
9658 | *first_arg_ptr = first_arg; | |
9659 | return interface_method->get_function_tree(context, expr); | |
9660 | } | |
9661 | ||
9662 | // Build the call expression. | |
9663 | ||
9664 | tree | |
9665 | Call_expression::do_get_tree(Translate_context* context) | |
9666 | { | |
9667 | if (this->tree_ != NULL_TREE) | |
9668 | return this->tree_; | |
9669 | ||
9670 | Function_type* fntype = this->get_function_type(); | |
9671 | if (fntype == NULL) | |
9672 | return error_mark_node; | |
9673 | ||
9674 | if (this->fn_->is_error_expression()) | |
9675 | return error_mark_node; | |
9676 | ||
9677 | Gogo* gogo = context->gogo(); | |
b13c66cd | 9678 | Location location = this->location(); |
e440a328 | 9679 | |
9680 | Func_expression* func = this->fn_->func_expression(); | |
e440a328 | 9681 | Interface_field_reference_expression* interface_method = |
9682 | this->fn_->interface_field_reference_expression(); | |
9683 | const bool has_closure = func != NULL && func->closure() != NULL; | |
09ea332d | 9684 | const bool is_interface_method = interface_method != NULL; |
e440a328 | 9685 | |
f8bdf81a | 9686 | bool has_closure_arg; |
8381eda7 | 9687 | if (has_closure) |
f8bdf81a | 9688 | has_closure_arg = true; |
8381eda7 | 9689 | else if (func != NULL) |
f8bdf81a | 9690 | has_closure_arg = false; |
8381eda7 | 9691 | else if (is_interface_method) |
f8bdf81a | 9692 | has_closure_arg = false; |
8381eda7 | 9693 | else |
f8bdf81a | 9694 | has_closure_arg = true; |
8381eda7 | 9695 | |
e440a328 | 9696 | int nargs; |
9697 | tree* args; | |
9698 | if (this->args_ == NULL || this->args_->empty()) | |
9699 | { | |
f8bdf81a | 9700 | nargs = is_interface_method ? 1 : 0; |
e440a328 | 9701 | args = nargs == 0 ? NULL : new tree[nargs]; |
9702 | } | |
09ea332d | 9703 | else if (fntype->parameters() == NULL || fntype->parameters()->empty()) |
9704 | { | |
9705 | // Passing a receiver parameter. | |
9706 | go_assert(!is_interface_method | |
9707 | && fntype->is_method() | |
9708 | && this->args_->size() == 1); | |
f8bdf81a | 9709 | nargs = 1; |
09ea332d | 9710 | args = new tree[nargs]; |
9711 | args[0] = this->args_->front()->get_tree(context); | |
9712 | } | |
e440a328 | 9713 | else |
9714 | { | |
9715 | const Typed_identifier_list* params = fntype->parameters(); | |
e440a328 | 9716 | |
9717 | nargs = this->args_->size(); | |
09ea332d | 9718 | int i = is_interface_method ? 1 : 0; |
e440a328 | 9719 | nargs += i; |
9720 | args = new tree[nargs]; | |
9721 | ||
9722 | Typed_identifier_list::const_iterator pp = params->begin(); | |
09ea332d | 9723 | Expression_list::const_iterator pe = this->args_->begin(); |
9724 | if (!is_interface_method && fntype->is_method()) | |
9725 | { | |
9726 | args[i] = (*pe)->get_tree(context); | |
9727 | ++pe; | |
9728 | ++i; | |
9729 | } | |
9730 | for (; pe != this->args_->end(); ++pe, ++pp, ++i) | |
e440a328 | 9731 | { |
c484d925 | 9732 | go_assert(pp != params->end()); |
e440a328 | 9733 | tree arg_val = (*pe)->get_tree(context); |
9734 | args[i] = Expression::convert_for_assignment(context, | |
9735 | pp->type(), | |
9736 | (*pe)->type(), | |
9737 | arg_val, | |
9738 | location); | |
9739 | if (args[i] == error_mark_node) | |
8381eda7 | 9740 | return error_mark_node; |
e440a328 | 9741 | } |
c484d925 | 9742 | go_assert(pp == params->end()); |
f8bdf81a | 9743 | go_assert(i == nargs); |
e440a328 | 9744 | } |
9745 | ||
8381eda7 | 9746 | tree fntype_tree = type_to_tree(fntype->get_backend(gogo)); |
9747 | if (fntype_tree == error_mark_node) | |
9748 | return error_mark_node; | |
9749 | go_assert(POINTER_TYPE_P(fntype_tree)); | |
9750 | if (TREE_TYPE(fntype_tree) == error_mark_node) | |
9751 | return error_mark_node; | |
9752 | go_assert(TREE_CODE(TREE_TYPE(fntype_tree)) == RECORD_TYPE); | |
9753 | tree fnfield_type = TREE_TYPE(TYPE_FIELDS(TREE_TYPE(fntype_tree))); | |
9754 | if (fnfield_type == error_mark_node) | |
9755 | return error_mark_node; | |
9756 | go_assert(FUNCTION_POINTER_TYPE_P(fnfield_type)); | |
9757 | tree rettype = TREE_TYPE(TREE_TYPE(fnfield_type)); | |
e440a328 | 9758 | if (rettype == error_mark_node) |
8381eda7 | 9759 | return error_mark_node; |
e440a328 | 9760 | |
9761 | tree fn; | |
f8bdf81a | 9762 | tree closure_tree; |
8381eda7 | 9763 | if (func != NULL) |
9764 | { | |
9765 | Named_object* no = func->named_object(); | |
8381eda7 | 9766 | fn = Func_expression::get_code_pointer(gogo, no, location); |
f8bdf81a | 9767 | if (!has_closure) |
9768 | closure_tree = NULL_TREE; | |
9769 | else | |
8381eda7 | 9770 | { |
f8bdf81a | 9771 | closure_tree = func->closure()->get_tree(context); |
9772 | if (closure_tree == error_mark_node) | |
9773 | return error_mark_node; | |
8381eda7 | 9774 | } |
9775 | } | |
09ea332d | 9776 | else if (!is_interface_method) |
8381eda7 | 9777 | { |
f8bdf81a | 9778 | closure_tree = this->fn_->get_tree(context); |
8381eda7 | 9779 | if (closure_tree == error_mark_node) |
9780 | return error_mark_node; | |
9781 | tree fnc = fold_convert_loc(location.gcc_location(), fntype_tree, | |
9782 | closure_tree); | |
9783 | go_assert(POINTER_TYPE_P(TREE_TYPE(fnc)) | |
9784 | && (TREE_CODE(TREE_TYPE(TREE_TYPE(fnc))) | |
9785 | == RECORD_TYPE)); | |
9786 | tree field = TYPE_FIELDS(TREE_TYPE(TREE_TYPE(fnc))); | |
9787 | fn = fold_build3_loc(location.gcc_location(), COMPONENT_REF, | |
9788 | TREE_TYPE(field), | |
9789 | build_fold_indirect_ref_loc(location.gcc_location(), | |
9790 | fnc), | |
9791 | field, NULL_TREE); | |
8381eda7 | 9792 | } |
e440a328 | 9793 | else |
cf609de4 | 9794 | { |
8381eda7 | 9795 | fn = this->interface_method_function(context, interface_method, |
9796 | &args[0]); | |
9797 | if (fn == error_mark_node) | |
9798 | return error_mark_node; | |
f8bdf81a | 9799 | closure_tree = NULL_TREE; |
cf609de4 | 9800 | } |
e440a328 | 9801 | |
8381eda7 | 9802 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) |
9803 | return error_mark_node; | |
9804 | ||
e440a328 | 9805 | tree fndecl = fn; |
9806 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
9807 | fndecl = TREE_OPERAND(fndecl, 0); | |
9aa9e2df | 9808 | |
9809 | // Add a type cast in case the type of the function is a recursive | |
9810 | // type which refers to itself. | |
9811 | if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl)) | |
8381eda7 | 9812 | fn = fold_convert_loc(location.gcc_location(), fnfield_type, fn); |
9aa9e2df | 9813 | |
9814 | // This is to support builtin math functions when using 80387 math. | |
e440a328 | 9815 | tree excess_type = NULL_TREE; |
68e1881d | 9816 | if (optimize |
9817 | && TREE_CODE(fndecl) == FUNCTION_DECL | |
e440a328 | 9818 | && DECL_IS_BUILTIN(fndecl) |
9819 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
9820 | && nargs > 0 | |
9821 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
9822 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
9823 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
9824 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
9825 | { | |
9826 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
9827 | if (excess_type != NULL_TREE) | |
9828 | { | |
9829 | tree excess_fndecl = mathfn_built_in(excess_type, | |
9830 | DECL_FUNCTION_CODE(fndecl)); | |
9831 | if (excess_fndecl == NULL_TREE) | |
9832 | excess_type = NULL_TREE; | |
9833 | else | |
9834 | { | |
b13c66cd | 9835 | fn = build_fold_addr_expr_loc(location.gcc_location(), |
9836 | excess_fndecl); | |
e440a328 | 9837 | for (int i = 0; i < nargs; ++i) |
26ae0101 | 9838 | { |
9839 | if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i])) | |
9840 | || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i]))) | |
9841 | args[i] = ::convert(excess_type, args[i]); | |
9842 | } | |
e440a328 | 9843 | } |
9844 | } | |
9845 | } | |
9846 | ||
d0bcce51 | 9847 | if (func == NULL) |
9848 | fn = save_expr(fn); | |
9849 | ||
f8bdf81a | 9850 | if (!has_closure_arg) |
9851 | go_assert(closure_tree == NULL_TREE); | |
9852 | else | |
9853 | { | |
9854 | // Pass the closure argument by calling the function function | |
9855 | // __go_set_closure. In the order_evaluations pass we have | |
9856 | // ensured that if any parameters contain call expressions, they | |
9857 | // will have been moved out to temporary variables. | |
9858 | ||
9859 | go_assert(closure_tree != NULL_TREE); | |
9860 | closure_tree = fold_convert_loc(location.gcc_location(), ptr_type_node, | |
9861 | closure_tree); | |
9862 | static tree set_closure_fndecl; | |
9863 | tree set_closure = Gogo::call_builtin(&set_closure_fndecl, | |
9864 | location, | |
9865 | "__go_set_closure", | |
9866 | 1, | |
9867 | void_type_node, | |
9868 | ptr_type_node, | |
9869 | closure_tree); | |
9870 | if (set_closure == error_mark_node) | |
9871 | return error_mark_node; | |
9872 | fn = build2_loc(location.gcc_location(), COMPOUND_EXPR, | |
9873 | TREE_TYPE(fn), set_closure, fn); | |
9874 | } | |
9875 | ||
e440a328 | 9876 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, |
9877 | fn, nargs, args); | |
9878 | delete[] args; | |
9879 | ||
b13c66cd | 9880 | SET_EXPR_LOCATION(ret, location.gcc_location()); |
e440a328 | 9881 | |
e440a328 | 9882 | // If this is a recursive function type which returns itself, as in |
9883 | // type F func() F | |
9884 | // we have used ptr_type_node for the return type. Add a cast here | |
9885 | // to the correct type. | |
9886 | if (TREE_TYPE(ret) == ptr_type_node) | |
9887 | { | |
9f0e0513 | 9888 | tree t = type_to_tree(this->type()->base()->get_backend(gogo)); |
b13c66cd | 9889 | ret = fold_convert_loc(location.gcc_location(), t, ret); |
e440a328 | 9890 | } |
9891 | ||
9892 | if (excess_type != NULL_TREE) | |
9893 | { | |
9894 | // Calling convert here can undo our excess precision change. | |
9895 | // That may or may not be a bug in convert_to_real. | |
9896 | ret = build1(NOP_EXPR, rettype, ret); | |
9897 | } | |
9898 | ||
ceeb4318 | 9899 | if (this->results_ != NULL) |
9900 | ret = this->set_results(context, ret); | |
e440a328 | 9901 | |
9902 | this->tree_ = ret; | |
9903 | ||
9904 | return ret; | |
9905 | } | |
9906 | ||
ceeb4318 | 9907 | // Set the result variables if this call returns multiple results. |
9908 | ||
9909 | tree | |
9910 | Call_expression::set_results(Translate_context* context, tree call_tree) | |
9911 | { | |
9912 | tree stmt_list = NULL_TREE; | |
9913 | ||
9914 | call_tree = save_expr(call_tree); | |
9915 | ||
9916 | if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE) | |
9917 | { | |
9918 | go_assert(saw_errors()); | |
9919 | return call_tree; | |
9920 | } | |
9921 | ||
b13c66cd | 9922 | Location loc = this->location(); |
ceeb4318 | 9923 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); |
9924 | size_t rc = this->result_count(); | |
9925 | for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field)) | |
9926 | { | |
9927 | go_assert(field != NULL_TREE); | |
9928 | ||
9929 | Temporary_statement* temp = this->result(i); | |
cd238b8d | 9930 | if (temp == NULL) |
9931 | { | |
9932 | go_assert(saw_errors()); | |
9933 | return error_mark_node; | |
9934 | } | |
ceeb4318 | 9935 | Temporary_reference_expression* ref = |
9936 | Expression::make_temporary_reference(temp, loc); | |
9937 | ref->set_is_lvalue(); | |
9938 | tree temp_tree = ref->get_tree(context); | |
9939 | if (temp_tree == error_mark_node) | |
423d1705 | 9940 | return error_mark_node; |
ceeb4318 | 9941 | |
b13c66cd | 9942 | tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF, |
9943 | TREE_TYPE(field), call_tree, field, NULL_TREE); | |
9944 | tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR, | |
9945 | void_type_node, temp_tree, val_tree); | |
ceeb4318 | 9946 | |
9947 | append_to_statement_list(set_tree, &stmt_list); | |
9948 | } | |
9949 | go_assert(field == NULL_TREE); | |
9950 | ||
9951 | return save_expr(stmt_list); | |
9952 | } | |
9953 | ||
d751bb78 | 9954 | // Dump ast representation for a call expressin. |
9955 | ||
9956 | void | |
9957 | Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
9958 | { | |
9959 | this->fn_->dump_expression(ast_dump_context); | |
9960 | ast_dump_context->ostream() << "("; | |
9961 | if (args_ != NULL) | |
9962 | ast_dump_context->dump_expression_list(this->args_); | |
9963 | ||
9964 | ast_dump_context->ostream() << ") "; | |
9965 | } | |
9966 | ||
e440a328 | 9967 | // Make a call expression. |
9968 | ||
9969 | Call_expression* | |
9970 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
b13c66cd | 9971 | Location location) |
e440a328 | 9972 | { |
9973 | return new Call_expression(fn, args, is_varargs, location); | |
9974 | } | |
9975 | ||
9976 | // A single result from a call which returns multiple results. | |
9977 | ||
9978 | class Call_result_expression : public Expression | |
9979 | { | |
9980 | public: | |
9981 | Call_result_expression(Call_expression* call, unsigned int index) | |
9982 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
9983 | call_(call), index_(index) | |
9984 | { } | |
9985 | ||
9986 | protected: | |
9987 | int | |
9988 | do_traverse(Traverse*); | |
9989 | ||
9990 | Type* | |
9991 | do_type(); | |
9992 | ||
9993 | void | |
9994 | do_determine_type(const Type_context*); | |
9995 | ||
9996 | void | |
9997 | do_check_types(Gogo*); | |
9998 | ||
9999 | Expression* | |
10000 | do_copy() | |
10001 | { | |
10002 | return new Call_result_expression(this->call_->call_expression(), | |
10003 | this->index_); | |
10004 | } | |
10005 | ||
10006 | bool | |
10007 | do_must_eval_in_order() const | |
10008 | { return true; } | |
10009 | ||
10010 | tree | |
10011 | do_get_tree(Translate_context*); | |
10012 | ||
d751bb78 | 10013 | void |
10014 | do_dump_expression(Ast_dump_context*) const; | |
10015 | ||
e440a328 | 10016 | private: |
10017 | // The underlying call expression. | |
10018 | Expression* call_; | |
10019 | // Which result we want. | |
10020 | unsigned int index_; | |
10021 | }; | |
10022 | ||
10023 | // Traverse a call result. | |
10024 | ||
10025 | int | |
10026 | Call_result_expression::do_traverse(Traverse* traverse) | |
10027 | { | |
10028 | if (traverse->remember_expression(this->call_)) | |
10029 | { | |
10030 | // We have already traversed the call expression. | |
10031 | return TRAVERSE_CONTINUE; | |
10032 | } | |
10033 | return Expression::traverse(&this->call_, traverse); | |
10034 | } | |
10035 | ||
10036 | // Get the type. | |
10037 | ||
10038 | Type* | |
10039 | Call_result_expression::do_type() | |
10040 | { | |
425dd051 | 10041 | if (this->classification() == EXPRESSION_ERROR) |
10042 | return Type::make_error_type(); | |
10043 | ||
e440a328 | 10044 | // THIS->CALL_ can be replaced with a temporary reference due to |
10045 | // Call_expression::do_must_eval_in_order when there is an error. | |
10046 | Call_expression* ce = this->call_->call_expression(); | |
10047 | if (ce == NULL) | |
5e85f268 | 10048 | { |
10049 | this->set_is_error(); | |
10050 | return Type::make_error_type(); | |
10051 | } | |
e440a328 | 10052 | Function_type* fntype = ce->get_function_type(); |
10053 | if (fntype == NULL) | |
5e85f268 | 10054 | { |
e37658e2 | 10055 | if (ce->issue_error()) |
99b3f06f | 10056 | { |
10057 | if (!ce->fn()->type()->is_error()) | |
10058 | this->report_error(_("expected function")); | |
10059 | } | |
5e85f268 | 10060 | this->set_is_error(); |
10061 | return Type::make_error_type(); | |
10062 | } | |
e440a328 | 10063 | const Typed_identifier_list* results = fntype->results(); |
ceeb4318 | 10064 | if (results == NULL || results->size() < 2) |
7b8d861f | 10065 | { |
ceeb4318 | 10066 | if (ce->issue_error()) |
10067 | this->report_error(_("number of results does not match " | |
10068 | "number of values")); | |
7b8d861f | 10069 | return Type::make_error_type(); |
10070 | } | |
e440a328 | 10071 | Typed_identifier_list::const_iterator pr = results->begin(); |
10072 | for (unsigned int i = 0; i < this->index_; ++i) | |
10073 | { | |
10074 | if (pr == results->end()) | |
425dd051 | 10075 | break; |
e440a328 | 10076 | ++pr; |
10077 | } | |
10078 | if (pr == results->end()) | |
425dd051 | 10079 | { |
ceeb4318 | 10080 | if (ce->issue_error()) |
10081 | this->report_error(_("number of results does not match " | |
10082 | "number of values")); | |
425dd051 | 10083 | return Type::make_error_type(); |
10084 | } | |
e440a328 | 10085 | return pr->type(); |
10086 | } | |
10087 | ||
425dd051 | 10088 | // Check the type. Just make sure that we trigger the warning in |
10089 | // do_type. | |
e440a328 | 10090 | |
10091 | void | |
10092 | Call_result_expression::do_check_types(Gogo*) | |
10093 | { | |
425dd051 | 10094 | this->type(); |
e440a328 | 10095 | } |
10096 | ||
10097 | // Determine the type. We have nothing to do here, but the 0 result | |
10098 | // needs to pass down to the caller. | |
10099 | ||
10100 | void | |
10101 | Call_result_expression::do_determine_type(const Type_context*) | |
10102 | { | |
fb94b0ca | 10103 | this->call_->determine_type_no_context(); |
e440a328 | 10104 | } |
10105 | ||
ceeb4318 | 10106 | // Return the tree. We just refer to the temporary set by the call |
10107 | // expression. We don't do this at lowering time because it makes it | |
10108 | // hard to evaluate the call at the right time. | |
e440a328 | 10109 | |
10110 | tree | |
10111 | Call_result_expression::do_get_tree(Translate_context* context) | |
10112 | { | |
ceeb4318 | 10113 | Call_expression* ce = this->call_->call_expression(); |
cd238b8d | 10114 | if (ce == NULL) |
10115 | { | |
10116 | go_assert(this->call_->is_error_expression()); | |
10117 | return error_mark_node; | |
10118 | } | |
ceeb4318 | 10119 | Temporary_statement* ts = ce->result(this->index_); |
cd238b8d | 10120 | if (ts == NULL) |
10121 | { | |
10122 | go_assert(saw_errors()); | |
10123 | return error_mark_node; | |
10124 | } | |
ceeb4318 | 10125 | Expression* ref = Expression::make_temporary_reference(ts, this->location()); |
10126 | return ref->get_tree(context); | |
e440a328 | 10127 | } |
10128 | ||
d751bb78 | 10129 | // Dump ast representation for a call result expression. |
10130 | ||
10131 | void | |
10132 | Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10133 | const | |
10134 | { | |
10135 | // FIXME: Wouldn't it be better if the call is assigned to a temporary | |
10136 | // (struct) and the fields are referenced instead. | |
10137 | ast_dump_context->ostream() << this->index_ << "@("; | |
10138 | ast_dump_context->dump_expression(this->call_); | |
10139 | ast_dump_context->ostream() << ")"; | |
10140 | } | |
10141 | ||
e440a328 | 10142 | // Make a reference to a single result of a call which returns |
10143 | // multiple results. | |
10144 | ||
10145 | Expression* | |
10146 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
10147 | { | |
10148 | return new Call_result_expression(call, index); | |
10149 | } | |
10150 | ||
10151 | // Class Index_expression. | |
10152 | ||
10153 | // Traversal. | |
10154 | ||
10155 | int | |
10156 | Index_expression::do_traverse(Traverse* traverse) | |
10157 | { | |
10158 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
10159 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
10160 | || (this->end_ != NULL | |
10161 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
10162 | return TRAVERSE_EXIT; | |
10163 | return TRAVERSE_CONTINUE; | |
10164 | } | |
10165 | ||
10166 | // Lower an index expression. This converts the generic index | |
10167 | // expression into an array index, a string index, or a map index. | |
10168 | ||
10169 | Expression* | |
ceeb4318 | 10170 | Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int) |
e440a328 | 10171 | { |
b13c66cd | 10172 | Location location = this->location(); |
e440a328 | 10173 | Expression* left = this->left_; |
10174 | Expression* start = this->start_; | |
10175 | Expression* end = this->end_; | |
10176 | ||
10177 | Type* type = left->type(); | |
5c13bd80 | 10178 | if (type->is_error()) |
e440a328 | 10179 | return Expression::make_error(location); |
b0cf7ddd | 10180 | else if (left->is_type_expression()) |
10181 | { | |
10182 | error_at(location, "attempt to index type expression"); | |
10183 | return Expression::make_error(location); | |
10184 | } | |
e440a328 | 10185 | else if (type->array_type() != NULL) |
10186 | return Expression::make_array_index(left, start, end, location); | |
10187 | else if (type->points_to() != NULL | |
10188 | && type->points_to()->array_type() != NULL | |
411eb89e | 10189 | && !type->points_to()->is_slice_type()) |
e440a328 | 10190 | { |
10191 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
10192 | location); | |
10193 | return Expression::make_array_index(deref, start, end, location); | |
10194 | } | |
10195 | else if (type->is_string_type()) | |
10196 | return Expression::make_string_index(left, start, end, location); | |
10197 | else if (type->map_type() != NULL) | |
10198 | { | |
10199 | if (end != NULL) | |
10200 | { | |
10201 | error_at(location, "invalid slice of map"); | |
10202 | return Expression::make_error(location); | |
10203 | } | |
6d4c2432 | 10204 | Map_index_expression* ret = Expression::make_map_index(left, start, |
10205 | location); | |
e440a328 | 10206 | if (this->is_lvalue_) |
10207 | ret->set_is_lvalue(); | |
10208 | return ret; | |
10209 | } | |
10210 | else | |
10211 | { | |
10212 | error_at(location, | |
10213 | "attempt to index object which is not array, string, or map"); | |
10214 | return Expression::make_error(location); | |
10215 | } | |
10216 | } | |
10217 | ||
d751bb78 | 10218 | // Write an indexed expression (expr[expr:expr] or expr[expr]) to a |
10219 | // dump context | |
10220 | ||
10221 | void | |
10222 | Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context, | |
10223 | const Expression* expr, | |
10224 | const Expression* start, | |
10225 | const Expression* end) | |
10226 | { | |
10227 | expr->dump_expression(ast_dump_context); | |
10228 | ast_dump_context->ostream() << "["; | |
10229 | start->dump_expression(ast_dump_context); | |
10230 | if (end != NULL) | |
10231 | { | |
10232 | ast_dump_context->ostream() << ":"; | |
10233 | end->dump_expression(ast_dump_context); | |
10234 | } | |
10235 | ast_dump_context->ostream() << "]"; | |
10236 | } | |
10237 | ||
10238 | // Dump ast representation for an index expression. | |
10239 | ||
10240 | void | |
10241 | Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10242 | const | |
10243 | { | |
10244 | Index_expression::dump_index_expression(ast_dump_context, this->left_, | |
10245 | this->start_, this->end_); | |
10246 | } | |
10247 | ||
e440a328 | 10248 | // Make an index expression. |
10249 | ||
10250 | Expression* | |
10251 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
b13c66cd | 10252 | Location location) |
e440a328 | 10253 | { |
10254 | return new Index_expression(left, start, end, location); | |
10255 | } | |
10256 | ||
10257 | // An array index. This is used for both indexing and slicing. | |
10258 | ||
10259 | class Array_index_expression : public Expression | |
10260 | { | |
10261 | public: | |
10262 | Array_index_expression(Expression* array, Expression* start, | |
b13c66cd | 10263 | Expression* end, Location location) |
e440a328 | 10264 | : Expression(EXPRESSION_ARRAY_INDEX, location), |
10265 | array_(array), start_(start), end_(end), type_(NULL) | |
10266 | { } | |
10267 | ||
10268 | protected: | |
10269 | int | |
10270 | do_traverse(Traverse*); | |
10271 | ||
10272 | Type* | |
10273 | do_type(); | |
10274 | ||
10275 | void | |
10276 | do_determine_type(const Type_context*); | |
10277 | ||
10278 | void | |
10279 | do_check_types(Gogo*); | |
10280 | ||
10281 | Expression* | |
10282 | do_copy() | |
10283 | { | |
10284 | return Expression::make_array_index(this->array_->copy(), | |
10285 | this->start_->copy(), | |
10286 | (this->end_ == NULL | |
10287 | ? NULL | |
10288 | : this->end_->copy()), | |
10289 | this->location()); | |
10290 | } | |
10291 | ||
baef9f7a | 10292 | bool |
10293 | do_must_eval_subexpressions_in_order(int* skip) const | |
10294 | { | |
10295 | *skip = 1; | |
10296 | return true; | |
10297 | } | |
10298 | ||
e440a328 | 10299 | bool |
10300 | do_is_addressable() const; | |
10301 | ||
10302 | void | |
10303 | do_address_taken(bool escapes) | |
10304 | { this->array_->address_taken(escapes); } | |
10305 | ||
10306 | tree | |
10307 | do_get_tree(Translate_context*); | |
10308 | ||
d751bb78 | 10309 | void |
10310 | do_dump_expression(Ast_dump_context*) const; | |
10311 | ||
e440a328 | 10312 | private: |
10313 | // The array we are getting a value from. | |
10314 | Expression* array_; | |
10315 | // The start or only index. | |
10316 | Expression* start_; | |
10317 | // The end index of a slice. This may be NULL for a simple array | |
10318 | // index, or it may be a nil expression for the length of the array. | |
10319 | Expression* end_; | |
10320 | // The type of the expression. | |
10321 | Type* type_; | |
10322 | }; | |
10323 | ||
10324 | // Array index traversal. | |
10325 | ||
10326 | int | |
10327 | Array_index_expression::do_traverse(Traverse* traverse) | |
10328 | { | |
10329 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
10330 | return TRAVERSE_EXIT; | |
10331 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10332 | return TRAVERSE_EXIT; | |
10333 | if (this->end_ != NULL) | |
10334 | { | |
10335 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10336 | return TRAVERSE_EXIT; | |
10337 | } | |
10338 | return TRAVERSE_CONTINUE; | |
10339 | } | |
10340 | ||
10341 | // Return the type of an array index. | |
10342 | ||
10343 | Type* | |
10344 | Array_index_expression::do_type() | |
10345 | { | |
10346 | if (this->type_ == NULL) | |
10347 | { | |
10348 | Array_type* type = this->array_->type()->array_type(); | |
10349 | if (type == NULL) | |
10350 | this->type_ = Type::make_error_type(); | |
10351 | else if (this->end_ == NULL) | |
10352 | this->type_ = type->element_type(); | |
411eb89e | 10353 | else if (type->is_slice_type()) |
e440a328 | 10354 | { |
10355 | // A slice of a slice has the same type as the original | |
10356 | // slice. | |
10357 | this->type_ = this->array_->type()->deref(); | |
10358 | } | |
10359 | else | |
10360 | { | |
10361 | // A slice of an array is a slice. | |
10362 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
10363 | } | |
10364 | } | |
10365 | return this->type_; | |
10366 | } | |
10367 | ||
10368 | // Set the type of an array index. | |
10369 | ||
10370 | void | |
10371 | Array_index_expression::do_determine_type(const Type_context*) | |
10372 | { | |
10373 | this->array_->determine_type_no_context(); | |
7917ad68 | 10374 | this->start_->determine_type_no_context(); |
e440a328 | 10375 | if (this->end_ != NULL) |
7917ad68 | 10376 | this->end_->determine_type_no_context(); |
e440a328 | 10377 | } |
10378 | ||
10379 | // Check types of an array index. | |
10380 | ||
10381 | void | |
10382 | Array_index_expression::do_check_types(Gogo*) | |
10383 | { | |
f6bc81e6 | 10384 | Numeric_constant nc; |
10385 | unsigned long v; | |
10386 | if (this->start_->type()->integer_type() == NULL | |
10387 | && !this->start_->type()->is_error() | |
10388 | && (!this->start_->numeric_constant_value(&nc) | |
10389 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10390 | this->report_error(_("index must be integer")); |
10391 | if (this->end_ != NULL | |
10392 | && this->end_->type()->integer_type() == NULL | |
99b3f06f | 10393 | && !this->end_->type()->is_error() |
10394 | && !this->end_->is_nil_expression() | |
f6bc81e6 | 10395 | && !this->end_->is_error_expression() |
10396 | && (!this->end_->numeric_constant_value(&nc) | |
10397 | || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT)) | |
e440a328 | 10398 | this->report_error(_("slice end must be integer")); |
10399 | ||
10400 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 10401 | if (array_type == NULL) |
10402 | { | |
c484d925 | 10403 | go_assert(this->array_->type()->is_error()); |
f9c68f17 | 10404 | return; |
10405 | } | |
e440a328 | 10406 | |
10407 | unsigned int int_bits = | |
10408 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
10409 | ||
0c77715b | 10410 | Numeric_constant lvalnc; |
e440a328 | 10411 | mpz_t lval; |
e440a328 | 10412 | bool lval_valid = (array_type->length() != NULL |
0c77715b | 10413 | && array_type->length()->numeric_constant_value(&lvalnc) |
10414 | && lvalnc.to_int(&lval)); | |
10415 | Numeric_constant inc; | |
e440a328 | 10416 | mpz_t ival; |
0bd5d859 | 10417 | bool ival_valid = false; |
0c77715b | 10418 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10419 | { |
0bd5d859 | 10420 | ival_valid = true; |
e440a328 | 10421 | if (mpz_sgn(ival) < 0 |
10422 | || mpz_sizeinbase(ival, 2) >= int_bits | |
10423 | || (lval_valid | |
10424 | && (this->end_ == NULL | |
10425 | ? mpz_cmp(ival, lval) >= 0 | |
10426 | : mpz_cmp(ival, lval) > 0))) | |
10427 | { | |
10428 | error_at(this->start_->location(), "array index out of bounds"); | |
10429 | this->set_is_error(); | |
10430 | } | |
10431 | } | |
10432 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10433 | { | |
0c77715b | 10434 | Numeric_constant enc; |
10435 | mpz_t eval; | |
10436 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10437 | { |
0c77715b | 10438 | if (mpz_sgn(eval) < 0 |
10439 | || mpz_sizeinbase(eval, 2) >= int_bits | |
10440 | || (lval_valid && mpz_cmp(eval, lval) > 0)) | |
e440a328 | 10441 | { |
10442 | error_at(this->end_->location(), "array index out of bounds"); | |
10443 | this->set_is_error(); | |
10444 | } | |
0bd5d859 | 10445 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10446 | this->report_error(_("inverted slice range")); | |
0c77715b | 10447 | mpz_clear(eval); |
e440a328 | 10448 | } |
10449 | } | |
0bd5d859 | 10450 | if (ival_valid) |
10451 | mpz_clear(ival); | |
0c77715b | 10452 | if (lval_valid) |
10453 | mpz_clear(lval); | |
e440a328 | 10454 | |
10455 | // A slice of an array requires an addressable array. A slice of a | |
10456 | // slice is always possible. | |
411eb89e | 10457 | if (this->end_ != NULL && !array_type->is_slice_type()) |
88ec30c8 | 10458 | { |
10459 | if (!this->array_->is_addressable()) | |
8da39c3b | 10460 | this->report_error(_("slice of unaddressable value")); |
88ec30c8 | 10461 | else |
10462 | this->array_->address_taken(true); | |
10463 | } | |
e440a328 | 10464 | } |
10465 | ||
10466 | // Return whether this expression is addressable. | |
10467 | ||
10468 | bool | |
10469 | Array_index_expression::do_is_addressable() const | |
10470 | { | |
10471 | // A slice expression is not addressable. | |
10472 | if (this->end_ != NULL) | |
10473 | return false; | |
10474 | ||
10475 | // An index into a slice is addressable. | |
411eb89e | 10476 | if (this->array_->type()->is_slice_type()) |
e440a328 | 10477 | return true; |
10478 | ||
10479 | // An index into an array is addressable if the array is | |
10480 | // addressable. | |
10481 | return this->array_->is_addressable(); | |
10482 | } | |
10483 | ||
10484 | // Get a tree for an array index. | |
10485 | ||
10486 | tree | |
10487 | Array_index_expression::do_get_tree(Translate_context* context) | |
10488 | { | |
10489 | Gogo* gogo = context->gogo(); | |
b13c66cd | 10490 | Location loc = this->location(); |
e440a328 | 10491 | |
10492 | Array_type* array_type = this->array_->type()->array_type(); | |
d8cd8e2d | 10493 | if (array_type == NULL) |
10494 | { | |
c484d925 | 10495 | go_assert(this->array_->type()->is_error()); |
d8cd8e2d | 10496 | return error_mark_node; |
10497 | } | |
e440a328 | 10498 | |
9f0e0513 | 10499 | tree type_tree = type_to_tree(array_type->get_backend(gogo)); |
c65212a0 | 10500 | if (type_tree == error_mark_node) |
10501 | return error_mark_node; | |
e440a328 | 10502 | |
10503 | tree array_tree = this->array_->get_tree(context); | |
10504 | if (array_tree == error_mark_node) | |
10505 | return error_mark_node; | |
10506 | ||
10507 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
10508 | array_tree = save_expr(array_tree); | |
a04bfdfc | 10509 | |
10510 | tree length_tree = NULL_TREE; | |
10511 | if (this->end_ == NULL || this->end_->is_nil_expression()) | |
10512 | { | |
10513 | length_tree = array_type->length_tree(gogo, array_tree); | |
10514 | if (length_tree == error_mark_node) | |
10515 | return error_mark_node; | |
10516 | length_tree = save_expr(length_tree); | |
10517 | } | |
10518 | ||
10519 | tree capacity_tree = NULL_TREE; | |
10520 | if (this->end_ != NULL) | |
10521 | { | |
10522 | capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
10523 | if (capacity_tree == error_mark_node) | |
10524 | return error_mark_node; | |
10525 | capacity_tree = save_expr(capacity_tree); | |
10526 | } | |
10527 | ||
10528 | tree length_type = (length_tree != NULL_TREE | |
10529 | ? TREE_TYPE(length_tree) | |
10530 | : TREE_TYPE(capacity_tree)); | |
e440a328 | 10531 | |
10532 | tree bad_index = boolean_false_node; | |
10533 | ||
10534 | tree start_tree = this->start_->get_tree(context); | |
10535 | if (start_tree == error_mark_node) | |
10536 | return error_mark_node; | |
10537 | if (!DECL_P(start_tree)) | |
10538 | start_tree = save_expr(start_tree); | |
10539 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
10540 | start_tree = convert_to_integer(length_type, start_tree); | |
10541 | ||
10542 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
10543 | loc); | |
10544 | ||
b13c66cd | 10545 | start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree); |
10546 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, | |
10547 | boolean_type_node, bad_index, | |
10548 | fold_build2_loc(loc.gcc_location(), | |
e440a328 | 10549 | (this->end_ == NULL |
10550 | ? GE_EXPR | |
10551 | : GT_EXPR), | |
10552 | boolean_type_node, start_tree, | |
a04bfdfc | 10553 | (this->end_ == NULL |
10554 | ? length_tree | |
10555 | : capacity_tree))); | |
e440a328 | 10556 | |
10557 | int code = (array_type->length() != NULL | |
10558 | ? (this->end_ == NULL | |
10559 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
10560 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
10561 | : (this->end_ == NULL | |
10562 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
10563 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
1b1f2abf | 10564 | tree crash = gogo->runtime_error(code, loc); |
e440a328 | 10565 | |
10566 | if (this->end_ == NULL) | |
10567 | { | |
10568 | // Simple array indexing. This has to return an l-value, so | |
10569 | // wrap the index check into START_TREE. | |
10570 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
10571 | build3(COND_EXPR, void_type_node, | |
10572 | bad_index, crash, NULL_TREE), | |
10573 | start_tree); | |
b13c66cd | 10574 | start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree); |
e440a328 | 10575 | |
10576 | if (array_type->length() != NULL) | |
10577 | { | |
10578 | // Fixed array. | |
10579 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
10580 | start_tree, NULL_TREE, NULL_TREE); | |
10581 | } | |
10582 | else | |
10583 | { | |
10584 | // Open array. | |
10585 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9f0e0513 | 10586 | Type* element_type = array_type->element_type(); |
10587 | Btype* belement_type = element_type->get_backend(gogo); | |
10588 | tree element_type_tree = type_to_tree(belement_type); | |
c65212a0 | 10589 | if (element_type_tree == error_mark_node) |
10590 | return error_mark_node; | |
e440a328 | 10591 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
b13c66cd | 10592 | tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype, |
e440a328 | 10593 | start_tree, element_size); |
b13c66cd | 10594 | tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
e440a328 | 10595 | TREE_TYPE(values), values, offset); |
10596 | return build_fold_indirect_ref(ptr); | |
10597 | } | |
10598 | } | |
10599 | ||
10600 | // Array slice. | |
10601 | ||
e440a328 | 10602 | tree end_tree; |
10603 | if (this->end_->is_nil_expression()) | |
10604 | end_tree = length_tree; | |
10605 | else | |
10606 | { | |
10607 | end_tree = this->end_->get_tree(context); | |
10608 | if (end_tree == error_mark_node) | |
10609 | return error_mark_node; | |
10610 | if (!DECL_P(end_tree)) | |
10611 | end_tree = save_expr(end_tree); | |
10612 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
10613 | end_tree = convert_to_integer(length_type, end_tree); | |
10614 | ||
10615 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
10616 | loc); | |
10617 | ||
b13c66cd | 10618 | end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree); |
e440a328 | 10619 | |
b13c66cd | 10620 | tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
10621 | boolean_type_node, | |
10622 | fold_build2_loc(loc.gcc_location(), | |
10623 | LT_EXPR, boolean_type_node, | |
e440a328 | 10624 | end_tree, start_tree), |
b13c66cd | 10625 | fold_build2_loc(loc.gcc_location(), |
10626 | GT_EXPR, boolean_type_node, | |
e440a328 | 10627 | end_tree, capacity_tree)); |
b13c66cd | 10628 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
10629 | boolean_type_node, bad_index, bad_end); | |
e440a328 | 10630 | } |
10631 | ||
9f0e0513 | 10632 | Type* element_type = array_type->element_type(); |
10633 | tree element_type_tree = type_to_tree(element_type->get_backend(gogo)); | |
c65212a0 | 10634 | if (element_type_tree == error_mark_node) |
10635 | return error_mark_node; | |
e440a328 | 10636 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
10637 | ||
b13c66cd | 10638 | tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype, |
10639 | fold_convert_loc(loc.gcc_location(), sizetype, | |
10640 | start_tree), | |
e440a328 | 10641 | element_size); |
10642 | ||
10643 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
c65212a0 | 10644 | if (value_pointer == error_mark_node) |
10645 | return error_mark_node; | |
e440a328 | 10646 | |
b13c66cd | 10647 | value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
e440a328 | 10648 | TREE_TYPE(value_pointer), |
10649 | value_pointer, offset); | |
10650 | ||
b13c66cd | 10651 | tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR, |
10652 | length_type, end_tree, start_tree); | |
e440a328 | 10653 | |
b13c66cd | 10654 | tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR, |
10655 | length_type, capacity_tree, | |
10656 | start_tree); | |
e440a328 | 10657 | |
9f0e0513 | 10658 | tree struct_tree = type_to_tree(this->type()->get_backend(gogo)); |
c484d925 | 10659 | go_assert(TREE_CODE(struct_tree) == RECORD_TYPE); |
e440a328 | 10660 | |
95f84544 | 10661 | vec<constructor_elt, va_gc> *init; |
10662 | vec_alloc (init, 3); | |
e440a328 | 10663 | |
e82e4eb5 | 10664 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 10665 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 10666 | tree field = TYPE_FIELDS(struct_tree); |
c484d925 | 10667 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 10668 | elt->index = field; |
10669 | elt->value = value_pointer; | |
10670 | ||
95f84544 | 10671 | elt = init->quick_push(empty); |
e440a328 | 10672 | field = DECL_CHAIN(field); |
c484d925 | 10673 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 10674 | elt->index = field; |
b13c66cd | 10675 | elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field), |
10676 | result_length_tree); | |
e440a328 | 10677 | |
95f84544 | 10678 | elt = init->quick_push(empty); |
e440a328 | 10679 | field = DECL_CHAIN(field); |
c484d925 | 10680 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); |
e440a328 | 10681 | elt->index = field; |
b13c66cd | 10682 | elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field), |
10683 | result_capacity_tree); | |
e440a328 | 10684 | |
10685 | tree constructor = build_constructor(struct_tree, init); | |
10686 | ||
10687 | if (TREE_CONSTANT(value_pointer) | |
10688 | && TREE_CONSTANT(result_length_tree) | |
10689 | && TREE_CONSTANT(result_capacity_tree)) | |
10690 | TREE_CONSTANT(constructor) = 1; | |
10691 | ||
b13c66cd | 10692 | return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, |
10693 | TREE_TYPE(constructor), | |
e440a328 | 10694 | build3(COND_EXPR, void_type_node, |
10695 | bad_index, crash, NULL_TREE), | |
10696 | constructor); | |
10697 | } | |
10698 | ||
d751bb78 | 10699 | // Dump ast representation for an array index expression. |
10700 | ||
10701 | void | |
10702 | Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10703 | const | |
10704 | { | |
10705 | Index_expression::dump_index_expression(ast_dump_context, this->array_, | |
10706 | this->start_, this->end_); | |
10707 | } | |
10708 | ||
e440a328 | 10709 | // Make an array index expression. END may be NULL. |
10710 | ||
10711 | Expression* | |
10712 | Expression::make_array_index(Expression* array, Expression* start, | |
b13c66cd | 10713 | Expression* end, Location location) |
e440a328 | 10714 | { |
e440a328 | 10715 | return new Array_index_expression(array, start, end, location); |
10716 | } | |
10717 | ||
10718 | // A string index. This is used for both indexing and slicing. | |
10719 | ||
10720 | class String_index_expression : public Expression | |
10721 | { | |
10722 | public: | |
10723 | String_index_expression(Expression* string, Expression* start, | |
b13c66cd | 10724 | Expression* end, Location location) |
e440a328 | 10725 | : Expression(EXPRESSION_STRING_INDEX, location), |
10726 | string_(string), start_(start), end_(end) | |
10727 | { } | |
10728 | ||
10729 | protected: | |
10730 | int | |
10731 | do_traverse(Traverse*); | |
10732 | ||
10733 | Type* | |
10734 | do_type(); | |
10735 | ||
10736 | void | |
10737 | do_determine_type(const Type_context*); | |
10738 | ||
10739 | void | |
10740 | do_check_types(Gogo*); | |
10741 | ||
10742 | Expression* | |
10743 | do_copy() | |
10744 | { | |
10745 | return Expression::make_string_index(this->string_->copy(), | |
10746 | this->start_->copy(), | |
10747 | (this->end_ == NULL | |
10748 | ? NULL | |
10749 | : this->end_->copy()), | |
10750 | this->location()); | |
10751 | } | |
10752 | ||
baef9f7a | 10753 | bool |
10754 | do_must_eval_subexpressions_in_order(int* skip) const | |
10755 | { | |
10756 | *skip = 1; | |
10757 | return true; | |
10758 | } | |
10759 | ||
e440a328 | 10760 | tree |
10761 | do_get_tree(Translate_context*); | |
10762 | ||
d751bb78 | 10763 | void |
10764 | do_dump_expression(Ast_dump_context*) const; | |
10765 | ||
e440a328 | 10766 | private: |
10767 | // The string we are getting a value from. | |
10768 | Expression* string_; | |
10769 | // The start or only index. | |
10770 | Expression* start_; | |
10771 | // The end index of a slice. This may be NULL for a single index, | |
10772 | // or it may be a nil expression for the length of the string. | |
10773 | Expression* end_; | |
10774 | }; | |
10775 | ||
10776 | // String index traversal. | |
10777 | ||
10778 | int | |
10779 | String_index_expression::do_traverse(Traverse* traverse) | |
10780 | { | |
10781 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
10782 | return TRAVERSE_EXIT; | |
10783 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
10784 | return TRAVERSE_EXIT; | |
10785 | if (this->end_ != NULL) | |
10786 | { | |
10787 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
10788 | return TRAVERSE_EXIT; | |
10789 | } | |
10790 | return TRAVERSE_CONTINUE; | |
10791 | } | |
10792 | ||
10793 | // Return the type of a string index. | |
10794 | ||
10795 | Type* | |
10796 | String_index_expression::do_type() | |
10797 | { | |
10798 | if (this->end_ == NULL) | |
10799 | return Type::lookup_integer_type("uint8"); | |
10800 | else | |
7672d35f | 10801 | return this->string_->type(); |
e440a328 | 10802 | } |
10803 | ||
10804 | // Determine the type of a string index. | |
10805 | ||
10806 | void | |
10807 | String_index_expression::do_determine_type(const Type_context*) | |
10808 | { | |
10809 | this->string_->determine_type_no_context(); | |
93000773 | 10810 | this->start_->determine_type_no_context(); |
e440a328 | 10811 | if (this->end_ != NULL) |
93000773 | 10812 | this->end_->determine_type_no_context(); |
e440a328 | 10813 | } |
10814 | ||
10815 | // Check types of a string index. | |
10816 | ||
10817 | void | |
10818 | String_index_expression::do_check_types(Gogo*) | |
10819 | { | |
10820 | if (this->start_->type()->integer_type() == NULL) | |
10821 | this->report_error(_("index must be integer")); | |
10822 | if (this->end_ != NULL | |
10823 | && this->end_->type()->integer_type() == NULL | |
10824 | && !this->end_->is_nil_expression()) | |
10825 | this->report_error(_("slice end must be integer")); | |
10826 | ||
10827 | std::string sval; | |
10828 | bool sval_valid = this->string_->string_constant_value(&sval); | |
10829 | ||
0c77715b | 10830 | Numeric_constant inc; |
e440a328 | 10831 | mpz_t ival; |
0bd5d859 | 10832 | bool ival_valid = false; |
0c77715b | 10833 | if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival)) |
e440a328 | 10834 | { |
0bd5d859 | 10835 | ival_valid = true; |
e440a328 | 10836 | if (mpz_sgn(ival) < 0 |
10837 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
10838 | { | |
10839 | error_at(this->start_->location(), "string index out of bounds"); | |
10840 | this->set_is_error(); | |
10841 | } | |
10842 | } | |
10843 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
10844 | { | |
0c77715b | 10845 | Numeric_constant enc; |
10846 | mpz_t eval; | |
10847 | if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval)) | |
e440a328 | 10848 | { |
0c77715b | 10849 | if (mpz_sgn(eval) < 0 |
10850 | || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0)) | |
e440a328 | 10851 | { |
10852 | error_at(this->end_->location(), "string index out of bounds"); | |
10853 | this->set_is_error(); | |
10854 | } | |
0bd5d859 | 10855 | else if (ival_valid && mpz_cmp(ival, eval) > 0) |
10856 | this->report_error(_("inverted slice range")); | |
0c77715b | 10857 | mpz_clear(eval); |
e440a328 | 10858 | } |
10859 | } | |
0bd5d859 | 10860 | if (ival_valid) |
10861 | mpz_clear(ival); | |
e440a328 | 10862 | } |
10863 | ||
10864 | // Get a tree for a string index. | |
10865 | ||
10866 | tree | |
10867 | String_index_expression::do_get_tree(Translate_context* context) | |
10868 | { | |
b13c66cd | 10869 | Location loc = this->location(); |
e440a328 | 10870 | |
10871 | tree string_tree = this->string_->get_tree(context); | |
10872 | if (string_tree == error_mark_node) | |
10873 | return error_mark_node; | |
10874 | ||
10875 | if (this->string_->type()->points_to() != NULL) | |
10876 | string_tree = build_fold_indirect_ref(string_tree); | |
10877 | if (!DECL_P(string_tree)) | |
10878 | string_tree = save_expr(string_tree); | |
10879 | tree string_type = TREE_TYPE(string_tree); | |
10880 | ||
10881 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
10882 | length_tree = save_expr(length_tree); | |
1b1f2abf | 10883 | |
10884 | Type* int_type = Type::lookup_integer_type("int"); | |
10885 | tree length_type = type_to_tree(int_type->get_backend(context->gogo())); | |
e440a328 | 10886 | |
10887 | tree bad_index = boolean_false_node; | |
10888 | ||
10889 | tree start_tree = this->start_->get_tree(context); | |
10890 | if (start_tree == error_mark_node) | |
10891 | return error_mark_node; | |
10892 | if (!DECL_P(start_tree)) | |
10893 | start_tree = save_expr(start_tree); | |
10894 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
10895 | start_tree = convert_to_integer(length_type, start_tree); | |
10896 | ||
10897 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
10898 | loc); | |
10899 | ||
b13c66cd | 10900 | start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree); |
e440a328 | 10901 | |
10902 | int code = (this->end_ == NULL | |
10903 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
10904 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
1b1f2abf | 10905 | tree crash = context->gogo()->runtime_error(code, loc); |
e440a328 | 10906 | |
10907 | if (this->end_ == NULL) | |
10908 | { | |
b13c66cd | 10909 | bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, |
10910 | boolean_type_node, bad_index, | |
10911 | fold_build2_loc(loc.gcc_location(), GE_EXPR, | |
e440a328 | 10912 | boolean_type_node, |
10913 | start_tree, length_tree)); | |
10914 | ||
10915 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
b13c66cd | 10916 | tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR, |
10917 | TREE_TYPE(bytes_tree), | |
e440a328 | 10918 | bytes_tree, |
b13c66cd | 10919 | fold_convert_loc(loc.gcc_location(), sizetype, |
10920 | start_tree)); | |
10921 | tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr); | |
e440a328 | 10922 | |
10923 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
10924 | build3(COND_EXPR, void_type_node, | |
10925 | bad_index, crash, NULL_TREE), | |
10926 | index); | |
10927 | } | |
10928 | else | |
10929 | { | |
10930 | tree end_tree; | |
10931 | if (this->end_->is_nil_expression()) | |
10932 | end_tree = build_int_cst(length_type, -1); | |
10933 | else | |
10934 | { | |
10935 | end_tree = this->end_->get_tree(context); | |
10936 | if (end_tree == error_mark_node) | |
10937 | return error_mark_node; | |
10938 | if (!DECL_P(end_tree)) | |
10939 | end_tree = save_expr(end_tree); | |
10940 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
10941 | end_tree = convert_to_integer(length_type, end_tree); | |
10942 | ||
10943 | bad_index = Expression::check_bounds(end_tree, length_type, | |
10944 | bad_index, loc); | |
10945 | ||
b13c66cd | 10946 | end_tree = fold_convert_loc(loc.gcc_location(), length_type, |
10947 | end_tree); | |
e440a328 | 10948 | } |
10949 | ||
10950 | static tree strslice_fndecl; | |
10951 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
10952 | loc, | |
10953 | "__go_string_slice", | |
10954 | 3, | |
10955 | string_type, | |
10956 | string_type, | |
10957 | string_tree, | |
10958 | length_type, | |
10959 | start_tree, | |
10960 | length_type, | |
10961 | end_tree); | |
5fb82b5e | 10962 | if (ret == error_mark_node) |
10963 | return error_mark_node; | |
e440a328 | 10964 | // This will panic if the bounds are out of range for the |
10965 | // string. | |
10966 | TREE_NOTHROW(strslice_fndecl) = 0; | |
10967 | ||
10968 | if (bad_index == boolean_false_node) | |
10969 | return ret; | |
10970 | else | |
10971 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
10972 | build3(COND_EXPR, void_type_node, | |
10973 | bad_index, crash, NULL_TREE), | |
10974 | ret); | |
10975 | } | |
10976 | } | |
10977 | ||
d751bb78 | 10978 | // Dump ast representation for a string index expression. |
10979 | ||
10980 | void | |
10981 | String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
10982 | const | |
10983 | { | |
10984 | Index_expression::dump_index_expression(ast_dump_context, this->string_, | |
10985 | this->start_, this->end_); | |
10986 | } | |
10987 | ||
e440a328 | 10988 | // Make a string index expression. END may be NULL. |
10989 | ||
10990 | Expression* | |
10991 | Expression::make_string_index(Expression* string, Expression* start, | |
b13c66cd | 10992 | Expression* end, Location location) |
e440a328 | 10993 | { |
10994 | return new String_index_expression(string, start, end, location); | |
10995 | } | |
10996 | ||
10997 | // Class Map_index. | |
10998 | ||
10999 | // Get the type of the map. | |
11000 | ||
11001 | Map_type* | |
11002 | Map_index_expression::get_map_type() const | |
11003 | { | |
11004 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 11005 | if (mt == NULL) |
c484d925 | 11006 | go_assert(saw_errors()); |
e440a328 | 11007 | return mt; |
11008 | } | |
11009 | ||
11010 | // Map index traversal. | |
11011 | ||
11012 | int | |
11013 | Map_index_expression::do_traverse(Traverse* traverse) | |
11014 | { | |
11015 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
11016 | return TRAVERSE_EXIT; | |
11017 | return Expression::traverse(&this->index_, traverse); | |
11018 | } | |
11019 | ||
11020 | // Return the type of a map index. | |
11021 | ||
11022 | Type* | |
11023 | Map_index_expression::do_type() | |
11024 | { | |
c7524fae | 11025 | Map_type* mt = this->get_map_type(); |
11026 | if (mt == NULL) | |
11027 | return Type::make_error_type(); | |
11028 | Type* type = mt->val_type(); | |
e440a328 | 11029 | // If this map index is in a tuple assignment, we actually return a |
11030 | // pointer to the value type. Tuple_map_assignment_statement is | |
11031 | // responsible for handling this correctly. We need to get the type | |
11032 | // right in case this gets assigned to a temporary variable. | |
11033 | if (this->is_in_tuple_assignment_) | |
11034 | type = Type::make_pointer_type(type); | |
11035 | return type; | |
11036 | } | |
11037 | ||
11038 | // Fix the type of a map index. | |
11039 | ||
11040 | void | |
11041 | Map_index_expression::do_determine_type(const Type_context*) | |
11042 | { | |
11043 | this->map_->determine_type_no_context(); | |
c7524fae | 11044 | Map_type* mt = this->get_map_type(); |
11045 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
11046 | Type_context subcontext(key_type, false); | |
e440a328 | 11047 | this->index_->determine_type(&subcontext); |
11048 | } | |
11049 | ||
11050 | // Check types of a map index. | |
11051 | ||
11052 | void | |
11053 | Map_index_expression::do_check_types(Gogo*) | |
11054 | { | |
11055 | std::string reason; | |
c7524fae | 11056 | Map_type* mt = this->get_map_type(); |
11057 | if (mt == NULL) | |
11058 | return; | |
11059 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 11060 | { |
11061 | if (reason.empty()) | |
11062 | this->report_error(_("incompatible type for map index")); | |
11063 | else | |
11064 | { | |
11065 | error_at(this->location(), "incompatible type for map index (%s)", | |
11066 | reason.c_str()); | |
11067 | this->set_is_error(); | |
11068 | } | |
11069 | } | |
11070 | } | |
11071 | ||
11072 | // Get a tree for a map index. | |
11073 | ||
11074 | tree | |
11075 | Map_index_expression::do_get_tree(Translate_context* context) | |
11076 | { | |
11077 | Map_type* type = this->get_map_type(); | |
c7524fae | 11078 | if (type == NULL) |
11079 | return error_mark_node; | |
e440a328 | 11080 | |
11081 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
11082 | if (valptr == error_mark_node) | |
11083 | return error_mark_node; | |
11084 | valptr = save_expr(valptr); | |
11085 | ||
11086 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
11087 | ||
11088 | if (this->is_lvalue_) | |
11089 | return build_fold_indirect_ref(valptr); | |
11090 | else if (this->is_in_tuple_assignment_) | |
11091 | { | |
11092 | // Tuple_map_assignment_statement is responsible for using this | |
11093 | // appropriately. | |
11094 | return valptr; | |
11095 | } | |
11096 | else | |
11097 | { | |
63697958 | 11098 | Gogo* gogo = context->gogo(); |
11099 | Btype* val_btype = type->val_type()->get_backend(gogo); | |
11100 | Bexpression* val_zero = gogo->backend()->zero_expression(val_btype); | |
e440a328 | 11101 | return fold_build3(COND_EXPR, val_type_tree, |
11102 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
11103 | fold_convert(TREE_TYPE(valptr), | |
11104 | null_pointer_node)), | |
63697958 | 11105 | expr_to_tree(val_zero), |
e440a328 | 11106 | build_fold_indirect_ref(valptr)); |
11107 | } | |
11108 | } | |
11109 | ||
11110 | // Get a tree for the map index. This returns a tree which evaluates | |
11111 | // to a pointer to a value. The pointer will be NULL if the key is | |
11112 | // not in the map. | |
11113 | ||
11114 | tree | |
11115 | Map_index_expression::get_value_pointer(Translate_context* context, | |
11116 | bool insert) | |
11117 | { | |
11118 | Map_type* type = this->get_map_type(); | |
c7524fae | 11119 | if (type == NULL) |
11120 | return error_mark_node; | |
e440a328 | 11121 | |
11122 | tree map_tree = this->map_->get_tree(context); | |
11123 | tree index_tree = this->index_->get_tree(context); | |
11124 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
11125 | this->index_->type(), | |
11126 | index_tree, | |
11127 | this->location()); | |
11128 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
11129 | return error_mark_node; | |
11130 | ||
11131 | if (this->map_->type()->points_to() != NULL) | |
11132 | map_tree = build_fold_indirect_ref(map_tree); | |
11133 | ||
11134 | // We need to pass in a pointer to the key, so stuff it into a | |
11135 | // variable. | |
746d2e73 | 11136 | tree tmp; |
11137 | tree make_tmp; | |
11138 | if (current_function_decl != NULL) | |
11139 | { | |
11140 | tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
11141 | DECL_IGNORED_P(tmp) = 0; | |
11142 | DECL_INITIAL(tmp) = index_tree; | |
11143 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
11144 | TREE_ADDRESSABLE(tmp) = 1; | |
11145 | } | |
11146 | else | |
11147 | { | |
b13c66cd | 11148 | tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
11149 | create_tmp_var_name("M"), | |
746d2e73 | 11150 | TREE_TYPE(index_tree)); |
11151 | DECL_EXTERNAL(tmp) = 0; | |
11152 | TREE_PUBLIC(tmp) = 0; | |
11153 | TREE_STATIC(tmp) = 1; | |
11154 | DECL_ARTIFICIAL(tmp) = 1; | |
11155 | if (!TREE_CONSTANT(index_tree)) | |
b13c66cd | 11156 | make_tmp = fold_build2_loc(this->location().gcc_location(), |
11157 | INIT_EXPR, void_type_node, | |
746d2e73 | 11158 | tmp, index_tree); |
11159 | else | |
11160 | { | |
11161 | TREE_READONLY(tmp) = 1; | |
11162 | TREE_CONSTANT(tmp) = 1; | |
11163 | DECL_INITIAL(tmp) = index_tree; | |
11164 | make_tmp = NULL_TREE; | |
11165 | } | |
11166 | rest_of_decl_compilation(tmp, 1, 0); | |
11167 | } | |
b13c66cd | 11168 | tree tmpref = |
11169 | fold_convert_loc(this->location().gcc_location(), const_ptr_type_node, | |
11170 | build_fold_addr_expr_loc(this->location().gcc_location(), | |
11171 | tmp)); | |
e440a328 | 11172 | |
11173 | static tree map_index_fndecl; | |
11174 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
11175 | this->location(), | |
11176 | "__go_map_index", | |
11177 | 3, | |
11178 | const_ptr_type_node, | |
11179 | TREE_TYPE(map_tree), | |
11180 | map_tree, | |
11181 | const_ptr_type_node, | |
11182 | tmpref, | |
11183 | boolean_type_node, | |
11184 | (insert | |
11185 | ? boolean_true_node | |
11186 | : boolean_false_node)); | |
5fb82b5e | 11187 | if (call == error_mark_node) |
11188 | return error_mark_node; | |
e440a328 | 11189 | // This can panic on a map of interface type if the interface holds |
11190 | // an uncomparable or unhashable type. | |
11191 | TREE_NOTHROW(map_index_fndecl) = 0; | |
11192 | ||
9f0e0513 | 11193 | Type* val_type = type->val_type(); |
11194 | tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo())); | |
e440a328 | 11195 | if (val_type_tree == error_mark_node) |
11196 | return error_mark_node; | |
11197 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
11198 | ||
b13c66cd | 11199 | tree ret = fold_convert_loc(this->location().gcc_location(), |
11200 | ptr_val_type_tree, call); | |
746d2e73 | 11201 | if (make_tmp != NULL_TREE) |
11202 | ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret); | |
11203 | return ret; | |
e440a328 | 11204 | } |
11205 | ||
d751bb78 | 11206 | // Dump ast representation for a map index expression |
11207 | ||
11208 | void | |
11209 | Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
11210 | const | |
11211 | { | |
11212 | Index_expression::dump_index_expression(ast_dump_context, | |
11213 | this->map_, this->index_, NULL); | |
11214 | } | |
11215 | ||
e440a328 | 11216 | // Make a map index expression. |
11217 | ||
11218 | Map_index_expression* | |
11219 | Expression::make_map_index(Expression* map, Expression* index, | |
b13c66cd | 11220 | Location location) |
e440a328 | 11221 | { |
11222 | return new Map_index_expression(map, index, location); | |
11223 | } | |
11224 | ||
11225 | // Class Field_reference_expression. | |
11226 | ||
149eabc5 | 11227 | // Lower a field reference expression. There is nothing to lower, but |
11228 | // this is where we generate the tracking information for fields with | |
11229 | // the magic go:"track" tag. | |
11230 | ||
11231 | Expression* | |
11232 | Field_reference_expression::do_lower(Gogo* gogo, Named_object* function, | |
11233 | Statement_inserter* inserter, int) | |
11234 | { | |
11235 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
11236 | if (struct_type == NULL) | |
11237 | { | |
11238 | // Error will be reported elsewhere. | |
11239 | return this; | |
11240 | } | |
11241 | const Struct_field* field = struct_type->field(this->field_index_); | |
11242 | if (field == NULL) | |
11243 | return this; | |
11244 | if (!field->has_tag()) | |
11245 | return this; | |
11246 | if (field->tag().find("go:\"track\"") == std::string::npos) | |
11247 | return this; | |
11248 | ||
11249 | // We have found a reference to a tracked field. Build a call to | |
11250 | // the runtime function __go_fieldtrack with a string that describes | |
11251 | // the field. FIXME: We should only call this once per referenced | |
11252 | // field per function, not once for each reference to the field. | |
11253 | ||
11254 | if (this->called_fieldtrack_) | |
11255 | return this; | |
11256 | this->called_fieldtrack_ = true; | |
11257 | ||
11258 | Location loc = this->location(); | |
11259 | ||
11260 | std::string s = "fieldtrack \""; | |
11261 | Named_type* nt = this->expr_->type()->named_type(); | |
11262 | if (nt == NULL || nt->named_object()->package() == NULL) | |
11263 | s.append(gogo->pkgpath()); | |
11264 | else | |
11265 | s.append(nt->named_object()->package()->pkgpath()); | |
11266 | s.push_back('.'); | |
11267 | if (nt != NULL) | |
5c29ad36 | 11268 | s.append(Gogo::unpack_hidden_name(nt->name())); |
149eabc5 | 11269 | s.push_back('.'); |
11270 | s.append(field->field_name()); | |
11271 | s.push_back('"'); | |
11272 | ||
11273 | // We can't use a string here, because internally a string holds a | |
11274 | // pointer to the actual bytes; when the linker garbage collects the | |
11275 | // string, it won't garbage collect the bytes. So we use a | |
11276 | // [...]byte. | |
11277 | ||
11278 | mpz_t val; | |
11279 | mpz_init_set_ui(val, s.length()); | |
11280 | Expression* length_expr = Expression::make_integer(&val, NULL, loc); | |
11281 | mpz_clear(val); | |
11282 | ||
11283 | Type* byte_type = gogo->lookup_global("byte")->type_value(); | |
11284 | Type* array_type = Type::make_array_type(byte_type, length_expr); | |
11285 | ||
11286 | Expression_list* bytes = new Expression_list(); | |
11287 | for (std::string::const_iterator p = s.begin(); p != s.end(); p++) | |
11288 | { | |
11289 | mpz_init_set_ui(val, *p); | |
11290 | Expression* byte = Expression::make_integer(&val, NULL, loc); | |
11291 | mpz_clear(val); | |
11292 | bytes->push_back(byte); | |
11293 | } | |
11294 | ||
11295 | Expression* e = Expression::make_composite_literal(array_type, 0, false, | |
62750cd5 | 11296 | bytes, false, loc); |
149eabc5 | 11297 | |
11298 | Variable* var = new Variable(array_type, e, true, false, false, loc); | |
11299 | ||
11300 | static int count; | |
11301 | char buf[50]; | |
11302 | snprintf(buf, sizeof buf, "fieldtrack.%d", count); | |
11303 | ++count; | |
11304 | ||
11305 | Named_object* no = gogo->add_variable(buf, var); | |
11306 | e = Expression::make_var_reference(no, loc); | |
11307 | e = Expression::make_unary(OPERATOR_AND, e, loc); | |
11308 | ||
11309 | Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e); | |
11310 | inserter->insert(Statement::make_statement(call, false)); | |
11311 | ||
11312 | // Put this function, and the global variable we just created, into | |
11313 | // unique sections. This will permit the linker to garbage collect | |
11314 | // them if they are not referenced. The effect is that the only | |
11315 | // strings, indicating field references, that will wind up in the | |
11316 | // executable will be those for functions that are actually needed. | |
66a6be58 | 11317 | if (function != NULL) |
11318 | function->func_value()->set_in_unique_section(); | |
149eabc5 | 11319 | var->set_in_unique_section(); |
11320 | ||
11321 | return this; | |
11322 | } | |
11323 | ||
e440a328 | 11324 | // Return the type of a field reference. |
11325 | ||
11326 | Type* | |
11327 | Field_reference_expression::do_type() | |
11328 | { | |
b0e628fb | 11329 | Type* type = this->expr_->type(); |
5c13bd80 | 11330 | if (type->is_error()) |
b0e628fb | 11331 | return type; |
11332 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11333 | go_assert(struct_type != NULL); |
e440a328 | 11334 | return struct_type->field(this->field_index_)->type(); |
11335 | } | |
11336 | ||
11337 | // Check the types for a field reference. | |
11338 | ||
11339 | void | |
11340 | Field_reference_expression::do_check_types(Gogo*) | |
11341 | { | |
b0e628fb | 11342 | Type* type = this->expr_->type(); |
5c13bd80 | 11343 | if (type->is_error()) |
b0e628fb | 11344 | return; |
11345 | Struct_type* struct_type = type->struct_type(); | |
c484d925 | 11346 | go_assert(struct_type != NULL); |
11347 | go_assert(struct_type->field(this->field_index_) != NULL); | |
e440a328 | 11348 | } |
11349 | ||
11350 | // Get a tree for a field reference. | |
11351 | ||
11352 | tree | |
11353 | Field_reference_expression::do_get_tree(Translate_context* context) | |
11354 | { | |
11355 | tree struct_tree = this->expr_->get_tree(context); | |
11356 | if (struct_tree == error_mark_node | |
11357 | || TREE_TYPE(struct_tree) == error_mark_node) | |
11358 | return error_mark_node; | |
c484d925 | 11359 | go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); |
e440a328 | 11360 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); |
b1d655d5 | 11361 | if (field == NULL_TREE) |
11362 | { | |
11363 | // This can happen for a type which refers to itself indirectly | |
11364 | // and then turns out to be erroneous. | |
c484d925 | 11365 | go_assert(saw_errors()); |
b1d655d5 | 11366 | return error_mark_node; |
11367 | } | |
e440a328 | 11368 | for (unsigned int i = this->field_index_; i > 0; --i) |
11369 | { | |
11370 | field = DECL_CHAIN(field); | |
c484d925 | 11371 | go_assert(field != NULL_TREE); |
e440a328 | 11372 | } |
c35179ff | 11373 | if (TREE_TYPE(field) == error_mark_node) |
11374 | return error_mark_node; | |
e440a328 | 11375 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, |
11376 | NULL_TREE); | |
11377 | } | |
11378 | ||
d751bb78 | 11379 | // Dump ast representation for a field reference expression. |
11380 | ||
11381 | void | |
11382 | Field_reference_expression::do_dump_expression( | |
11383 | Ast_dump_context* ast_dump_context) const | |
11384 | { | |
11385 | this->expr_->dump_expression(ast_dump_context); | |
11386 | ast_dump_context->ostream() << "." << this->field_index_; | |
11387 | } | |
11388 | ||
e440a328 | 11389 | // Make a reference to a qualified identifier in an expression. |
11390 | ||
11391 | Field_reference_expression* | |
11392 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
b13c66cd | 11393 | Location location) |
e440a328 | 11394 | { |
11395 | return new Field_reference_expression(expr, field_index, location); | |
11396 | } | |
11397 | ||
11398 | // Class Interface_field_reference_expression. | |
11399 | ||
11400 | // Return a tree for the pointer to the function to call. | |
11401 | ||
11402 | tree | |
11403 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
11404 | tree expr) | |
11405 | { | |
11406 | if (this->expr_->type()->points_to() != NULL) | |
11407 | expr = build_fold_indirect_ref(expr); | |
11408 | ||
11409 | tree expr_type = TREE_TYPE(expr); | |
c484d925 | 11410 | go_assert(TREE_CODE(expr_type) == RECORD_TYPE); |
e440a328 | 11411 | |
11412 | tree field = TYPE_FIELDS(expr_type); | |
c484d925 | 11413 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); |
e440a328 | 11414 | |
11415 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
c484d925 | 11416 | go_assert(POINTER_TYPE_P(TREE_TYPE(table))); |
e440a328 | 11417 | |
11418 | table = build_fold_indirect_ref(table); | |
c484d925 | 11419 | go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); |
e440a328 | 11420 | |
11421 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
11422 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
11423 | field != NULL_TREE; | |
11424 | field = DECL_CHAIN(field)) | |
11425 | { | |
11426 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
11427 | break; | |
11428 | } | |
c484d925 | 11429 | go_assert(field != NULL_TREE); |
e440a328 | 11430 | |
11431 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
11432 | } | |
11433 | ||
11434 | // Return a tree for the first argument to pass to the interface | |
11435 | // function. | |
11436 | ||
11437 | tree | |
11438 | Interface_field_reference_expression::get_underlying_object_tree( | |
11439 | Translate_context*, | |
11440 | tree expr) | |
11441 | { | |
11442 | if (this->expr_->type()->points_to() != NULL) | |
11443 | expr = build_fold_indirect_ref(expr); | |
11444 | ||
11445 | tree expr_type = TREE_TYPE(expr); | |
c484d925 | 11446 | go_assert(TREE_CODE(expr_type) == RECORD_TYPE); |
e440a328 | 11447 | |
11448 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
c484d925 | 11449 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); |
e440a328 | 11450 | |
11451 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
11452 | } | |
11453 | ||
11454 | // Traversal. | |
11455 | ||
11456 | int | |
11457 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
11458 | { | |
11459 | return Expression::traverse(&this->expr_, traverse); | |
11460 | } | |
11461 | ||
0afbb937 | 11462 | // Lower the expression. If this expression is not called, we need to |
11463 | // evaluate the expression twice when converting to the backend | |
11464 | // interface. So introduce a temporary variable if necessary. | |
11465 | ||
11466 | Expression* | |
11467 | Interface_field_reference_expression::do_lower(Gogo*, Named_object*, | |
11468 | Statement_inserter* inserter, | |
11469 | int) | |
11470 | { | |
11471 | if (this->expr_->var_expression() == NULL | |
11472 | && this->expr_->temporary_reference_expression() == NULL | |
11473 | && this->expr_->set_and_use_temporary_expression() == NULL) | |
11474 | { | |
11475 | Temporary_statement* temp = | |
11476 | Statement::make_temporary(this->expr_->type(), NULL, this->location()); | |
11477 | inserter->insert(temp); | |
11478 | this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_, | |
11479 | this->location()); | |
11480 | } | |
11481 | return this; | |
11482 | } | |
11483 | ||
e440a328 | 11484 | // Return the type of an interface field reference. |
11485 | ||
11486 | Type* | |
11487 | Interface_field_reference_expression::do_type() | |
11488 | { | |
11489 | Type* expr_type = this->expr_->type(); | |
11490 | ||
11491 | Type* points_to = expr_type->points_to(); | |
11492 | if (points_to != NULL) | |
11493 | expr_type = points_to; | |
11494 | ||
11495 | Interface_type* interface_type = expr_type->interface_type(); | |
11496 | if (interface_type == NULL) | |
11497 | return Type::make_error_type(); | |
11498 | ||
11499 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
11500 | if (method == NULL) | |
11501 | return Type::make_error_type(); | |
11502 | ||
11503 | return method->type(); | |
11504 | } | |
11505 | ||
11506 | // Determine types. | |
11507 | ||
11508 | void | |
11509 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
11510 | { | |
11511 | this->expr_->determine_type_no_context(); | |
11512 | } | |
11513 | ||
11514 | // Check the types for an interface field reference. | |
11515 | ||
11516 | void | |
11517 | Interface_field_reference_expression::do_check_types(Gogo*) | |
11518 | { | |
11519 | Type* type = this->expr_->type(); | |
11520 | ||
11521 | Type* points_to = type->points_to(); | |
11522 | if (points_to != NULL) | |
11523 | type = points_to; | |
11524 | ||
11525 | Interface_type* interface_type = type->interface_type(); | |
11526 | if (interface_type == NULL) | |
5c491127 | 11527 | { |
11528 | if (!type->is_error_type()) | |
11529 | this->report_error(_("expected interface or pointer to interface")); | |
11530 | } | |
e440a328 | 11531 | else |
11532 | { | |
11533 | const Typed_identifier* method = | |
11534 | interface_type->find_method(this->name_); | |
11535 | if (method == NULL) | |
11536 | { | |
11537 | error_at(this->location(), "method %qs not in interface", | |
11538 | Gogo::message_name(this->name_).c_str()); | |
11539 | this->set_is_error(); | |
11540 | } | |
11541 | } | |
11542 | } | |
11543 | ||
0afbb937 | 11544 | // If an interface field reference is not simply called, then it is |
11545 | // represented as a closure. The closure will hold a single variable, | |
11546 | // the value of the interface on which the method should be called. | |
11547 | // The function will be a simple thunk that pulls the value from the | |
11548 | // closure and calls the method with the remaining arguments. | |
11549 | ||
11550 | // Because method values are not common, we don't build all thunks for | |
11551 | // all possible interface methods, but instead only build them as we | |
11552 | // need them. In particular, we even build them on demand for | |
11553 | // interface methods defined in other packages. | |
11554 | ||
11555 | Interface_field_reference_expression::Interface_method_thunks | |
11556 | Interface_field_reference_expression::interface_method_thunks; | |
11557 | ||
11558 | // Find or create the thunk to call method NAME on TYPE. | |
11559 | ||
11560 | Named_object* | |
11561 | Interface_field_reference_expression::create_thunk(Gogo* gogo, | |
11562 | Interface_type* type, | |
11563 | const std::string& name) | |
11564 | { | |
11565 | std::pair<Interface_type*, Method_thunks*> val(type, NULL); | |
11566 | std::pair<Interface_method_thunks::iterator, bool> ins = | |
11567 | Interface_field_reference_expression::interface_method_thunks.insert(val); | |
11568 | if (ins.second) | |
11569 | { | |
11570 | // This is the first time we have seen this interface. | |
11571 | ins.first->second = new Method_thunks(); | |
11572 | } | |
11573 | ||
11574 | for (Method_thunks::const_iterator p = ins.first->second->begin(); | |
11575 | p != ins.first->second->end(); | |
11576 | p++) | |
11577 | if (p->first == name) | |
11578 | return p->second; | |
11579 | ||
11580 | Location loc = type->location(); | |
11581 | ||
11582 | const Typed_identifier* method_id = type->find_method(name); | |
11583 | if (method_id == NULL) | |
11584 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11585 | ||
11586 | Function_type* orig_fntype = method_id->type()->function_type(); | |
11587 | if (orig_fntype == NULL) | |
11588 | return Named_object::make_erroneous_name(Gogo::thunk_name()); | |
11589 | ||
11590 | Struct_field_list* sfl = new Struct_field_list(); | |
f8bdf81a | 11591 | // The type here is wrong--it should be the C function type. But it |
11592 | // doesn't really matter. | |
0afbb937 | 11593 | Type* vt = Type::make_pointer_type(Type::make_void_type()); |
11594 | sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc))); | |
11595 | sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc))); | |
11596 | Type* closure_type = Type::make_struct_type(sfl, loc); | |
11597 | closure_type = Type::make_pointer_type(closure_type); | |
11598 | ||
f8bdf81a | 11599 | Function_type* new_fntype = orig_fntype->copy_with_names(); |
0afbb937 | 11600 | |
11601 | Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype, | |
11602 | false, loc); | |
11603 | ||
f8bdf81a | 11604 | Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc); |
11605 | cvar->set_is_used(); | |
11606 | Named_object* cp = Named_object::make_variable("$closure", NULL, cvar); | |
11607 | new_no->func_value()->set_closure_var(cp); | |
0afbb937 | 11608 | |
f8bdf81a | 11609 | gogo->start_block(loc); |
0afbb937 | 11610 | |
11611 | // Field 0 of the closure is the function code pointer, field 1 is | |
11612 | // the value on which to invoke the method. | |
11613 | Expression* arg = Expression::make_var_reference(cp, loc); | |
11614 | arg = Expression::make_unary(OPERATOR_MULT, arg, loc); | |
11615 | arg = Expression::make_field_reference(arg, 1, loc); | |
11616 | ||
11617 | Expression *ifre = Expression::make_interface_field_reference(arg, name, | |
11618 | loc); | |
11619 | ||
11620 | const Typed_identifier_list* orig_params = orig_fntype->parameters(); | |
11621 | Expression_list* args; | |
11622 | if (orig_params == NULL || orig_params->empty()) | |
11623 | args = NULL; | |
11624 | else | |
11625 | { | |
11626 | const Typed_identifier_list* new_params = new_fntype->parameters(); | |
11627 | args = new Expression_list(); | |
11628 | for (Typed_identifier_list::const_iterator p = new_params->begin(); | |
f8bdf81a | 11629 | p != new_params->end(); |
0afbb937 | 11630 | ++p) |
11631 | { | |
11632 | Named_object* p_no = gogo->lookup(p->name(), NULL); | |
11633 | go_assert(p_no != NULL | |
11634 | && p_no->is_variable() | |
11635 | && p_no->var_value()->is_parameter()); | |
11636 | args->push_back(Expression::make_var_reference(p_no, loc)); | |
11637 | } | |
11638 | } | |
11639 | ||
11640 | Call_expression* call = Expression::make_call(ifre, args, | |
11641 | orig_fntype->is_varargs(), | |
11642 | loc); | |
11643 | call->set_varargs_are_lowered(); | |
11644 | ||
11645 | Statement* s = Statement::make_return_from_call(call, loc); | |
11646 | gogo->add_statement(s); | |
11647 | Block* b = gogo->finish_block(loc); | |
11648 | gogo->add_block(b, loc); | |
11649 | gogo->lower_block(new_no, b); | |
11650 | gogo->finish_function(loc); | |
11651 | ||
11652 | ins.first->second->push_back(std::make_pair(name, new_no)); | |
11653 | return new_no; | |
11654 | } | |
11655 | ||
11656 | // Get a tree for a method value. | |
e440a328 | 11657 | |
11658 | tree | |
0afbb937 | 11659 | Interface_field_reference_expression::do_get_tree(Translate_context* context) |
e440a328 | 11660 | { |
0afbb937 | 11661 | Interface_type* type = this->expr_->type()->interface_type(); |
11662 | if (type == NULL) | |
11663 | { | |
11664 | go_assert(saw_errors()); | |
11665 | return error_mark_node; | |
11666 | } | |
11667 | ||
11668 | Named_object* thunk = | |
11669 | Interface_field_reference_expression::create_thunk(context->gogo(), | |
11670 | type, this->name_); | |
11671 | if (thunk->is_erroneous()) | |
11672 | { | |
11673 | go_assert(saw_errors()); | |
11674 | return error_mark_node; | |
11675 | } | |
11676 | ||
11677 | // FIXME: We should lower this earlier, but we can't it lower it in | |
11678 | // the lowering pass because at that point we don't know whether we | |
11679 | // need to create the thunk or not. If the expression is called, we | |
11680 | // don't need the thunk. | |
11681 | ||
11682 | Location loc = this->location(); | |
11683 | ||
11684 | Struct_field_list* fields = new Struct_field_list(); | |
11685 | fields->push_back(Struct_field(Typed_identifier("fn.0", | |
11686 | thunk->func_value()->type(), | |
11687 | loc))); | |
11688 | fields->push_back(Struct_field(Typed_identifier("val.1", | |
11689 | this->expr_->type(), | |
11690 | loc))); | |
11691 | Struct_type* st = Type::make_struct_type(fields, loc); | |
11692 | ||
11693 | Expression_list* vals = new Expression_list(); | |
11694 | vals->push_back(Expression::make_func_code_reference(thunk, loc)); | |
11695 | vals->push_back(this->expr_); | |
11696 | ||
11697 | Expression* expr = Expression::make_struct_composite_literal(st, vals, loc); | |
11698 | expr = Expression::make_heap_composite(expr, loc); | |
11699 | ||
11700 | tree closure_tree = expr->get_tree(context); | |
11701 | ||
11702 | // Note that we are evaluating this->expr_ twice, but that is OK | |
11703 | // because in the lowering pass we forced it into a temporary | |
11704 | // variable. | |
11705 | tree expr_tree = this->expr_->get_tree(context); | |
11706 | tree nil_check_tree = Expression::comparison_tree(context, | |
11707 | Type::lookup_bool_type(), | |
11708 | OPERATOR_EQEQ, | |
11709 | this->expr_->type(), | |
11710 | expr_tree, | |
11711 | Type::make_nil_type(), | |
11712 | null_pointer_node, | |
11713 | loc); | |
11714 | tree crash = context->gogo()->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
11715 | loc); | |
11716 | if (closure_tree == error_mark_node | |
11717 | || nil_check_tree == error_mark_node | |
11718 | || crash == error_mark_node) | |
11719 | return error_mark_node; | |
11720 | return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, | |
11721 | TREE_TYPE(closure_tree), | |
11722 | build3_loc(loc.gcc_location(), COND_EXPR, | |
11723 | void_type_node, nil_check_tree, crash, | |
11724 | NULL_TREE), | |
11725 | closure_tree); | |
e440a328 | 11726 | } |
11727 | ||
d751bb78 | 11728 | // Dump ast representation for an interface field reference. |
11729 | ||
11730 | void | |
11731 | Interface_field_reference_expression::do_dump_expression( | |
11732 | Ast_dump_context* ast_dump_context) const | |
11733 | { | |
11734 | this->expr_->dump_expression(ast_dump_context); | |
11735 | ast_dump_context->ostream() << "." << this->name_; | |
11736 | } | |
11737 | ||
e440a328 | 11738 | // Make a reference to a field in an interface. |
11739 | ||
11740 | Expression* | |
11741 | Expression::make_interface_field_reference(Expression* expr, | |
11742 | const std::string& field, | |
b13c66cd | 11743 | Location location) |
e440a328 | 11744 | { |
11745 | return new Interface_field_reference_expression(expr, field, location); | |
11746 | } | |
11747 | ||
11748 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
11749 | // is lowered after we know the type of the left hand side. | |
11750 | ||
11751 | class Selector_expression : public Parser_expression | |
11752 | { | |
11753 | public: | |
11754 | Selector_expression(Expression* left, const std::string& name, | |
b13c66cd | 11755 | Location location) |
e440a328 | 11756 | : Parser_expression(EXPRESSION_SELECTOR, location), |
11757 | left_(left), name_(name) | |
11758 | { } | |
11759 | ||
11760 | protected: | |
11761 | int | |
11762 | do_traverse(Traverse* traverse) | |
11763 | { return Expression::traverse(&this->left_, traverse); } | |
11764 | ||
11765 | Expression* | |
ceeb4318 | 11766 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 11767 | |
11768 | Expression* | |
11769 | do_copy() | |
11770 | { | |
11771 | return new Selector_expression(this->left_->copy(), this->name_, | |
11772 | this->location()); | |
11773 | } | |
11774 | ||
d751bb78 | 11775 | void |
11776 | do_dump_expression(Ast_dump_context* ast_dump_context) const; | |
11777 | ||
e440a328 | 11778 | private: |
11779 | Expression* | |
11780 | lower_method_expression(Gogo*); | |
11781 | ||
11782 | // The expression on the left hand side. | |
11783 | Expression* left_; | |
11784 | // The name on the right hand side. | |
11785 | std::string name_; | |
11786 | }; | |
11787 | ||
11788 | // Lower a selector expression once we know the real type of the left | |
11789 | // hand side. | |
11790 | ||
11791 | Expression* | |
ceeb4318 | 11792 | Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*, |
11793 | int) | |
e440a328 | 11794 | { |
11795 | Expression* left = this->left_; | |
11796 | if (left->is_type_expression()) | |
11797 | return this->lower_method_expression(gogo); | |
11798 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
11799 | this->location()); | |
11800 | } | |
11801 | ||
11802 | // Lower a method expression T.M or (*T).M. We turn this into a | |
11803 | // function literal. | |
11804 | ||
11805 | Expression* | |
11806 | Selector_expression::lower_method_expression(Gogo* gogo) | |
11807 | { | |
b13c66cd | 11808 | Location location = this->location(); |
e440a328 | 11809 | Type* type = this->left_->type(); |
11810 | const std::string& name(this->name_); | |
11811 | ||
11812 | bool is_pointer; | |
11813 | if (type->points_to() == NULL) | |
11814 | is_pointer = false; | |
11815 | else | |
11816 | { | |
11817 | is_pointer = true; | |
11818 | type = type->points_to(); | |
11819 | } | |
11820 | Named_type* nt = type->named_type(); | |
11821 | if (nt == NULL) | |
11822 | { | |
11823 | error_at(location, | |
11824 | ("method expression requires named type or " | |
11825 | "pointer to named type")); | |
11826 | return Expression::make_error(location); | |
11827 | } | |
11828 | ||
11829 | bool is_ambiguous; | |
11830 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 11831 | const Typed_identifier* imethod = NULL; |
dcc8506b | 11832 | if (method == NULL && !is_pointer) |
ab1468c3 | 11833 | { |
11834 | Interface_type* it = nt->interface_type(); | |
11835 | if (it != NULL) | |
11836 | imethod = it->find_method(name); | |
11837 | } | |
11838 | ||
11839 | if (method == NULL && imethod == NULL) | |
e440a328 | 11840 | { |
11841 | if (!is_ambiguous) | |
dcc8506b | 11842 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
11843 | is_pointer ? "*" : "", | |
e440a328 | 11844 | nt->message_name().c_str(), |
11845 | Gogo::message_name(name).c_str()); | |
11846 | else | |
dcc8506b | 11847 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 11848 | Gogo::message_name(name).c_str(), |
dcc8506b | 11849 | is_pointer ? "*" : "", |
e440a328 | 11850 | nt->message_name().c_str()); |
11851 | return Expression::make_error(location); | |
11852 | } | |
11853 | ||
ab1468c3 | 11854 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 11855 | { |
11856 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
11857 | nt->message_name().c_str(), | |
11858 | Gogo::message_name(name).c_str()); | |
11859 | return Expression::make_error(location); | |
11860 | } | |
11861 | ||
11862 | // Build a new function type in which the receiver becomes the first | |
11863 | // argument. | |
ab1468c3 | 11864 | Function_type* method_type; |
11865 | if (method != NULL) | |
11866 | { | |
11867 | method_type = method->type(); | |
c484d925 | 11868 | go_assert(method_type->is_method()); |
ab1468c3 | 11869 | } |
11870 | else | |
11871 | { | |
11872 | method_type = imethod->type()->function_type(); | |
c484d925 | 11873 | go_assert(method_type != NULL && !method_type->is_method()); |
ab1468c3 | 11874 | } |
e440a328 | 11875 | |
11876 | const char* const receiver_name = "$this"; | |
11877 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
11878 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
11879 | location)); | |
11880 | ||
11881 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
11882 | if (method_parameters != NULL) | |
11883 | { | |
f470da59 | 11884 | int i = 0; |
e440a328 | 11885 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); |
11886 | p != method_parameters->end(); | |
f470da59 | 11887 | ++p, ++i) |
11888 | { | |
68883531 | 11889 | if (!p->name().empty()) |
f470da59 | 11890 | parameters->push_back(*p); |
11891 | else | |
11892 | { | |
11893 | char buf[20]; | |
11894 | snprintf(buf, sizeof buf, "$param%d", i); | |
11895 | parameters->push_back(Typed_identifier(buf, p->type(), | |
11896 | p->location())); | |
11897 | } | |
11898 | } | |
e440a328 | 11899 | } |
11900 | ||
11901 | const Typed_identifier_list* method_results = method_type->results(); | |
11902 | Typed_identifier_list* results; | |
11903 | if (method_results == NULL) | |
11904 | results = NULL; | |
11905 | else | |
11906 | { | |
11907 | results = new Typed_identifier_list(); | |
11908 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
11909 | p != method_results->end(); | |
11910 | ++p) | |
11911 | results->push_back(*p); | |
11912 | } | |
11913 | ||
11914 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
11915 | location); | |
11916 | if (method_type->is_varargs()) | |
11917 | fntype->set_is_varargs(); | |
11918 | ||
11919 | // We generate methods which always takes a pointer to the receiver | |
11920 | // as their first argument. If this is for a pointer type, we can | |
11921 | // simply reuse the existing function. We use an internal hack to | |
11922 | // get the right type. | |
8381eda7 | 11923 | // FIXME: This optimization is disabled because it doesn't yet work |
11924 | // with function descriptors when the method expression is not | |
11925 | // directly called. | |
11926 | if (method != NULL && is_pointer && false) | |
e440a328 | 11927 | { |
11928 | Named_object* mno = (method->needs_stub_method() | |
11929 | ? method->stub_object() | |
11930 | : method->named_object()); | |
11931 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
11932 | f = Expression::make_cast(fntype, f, location); | |
11933 | Type_conversion_expression* tce = | |
11934 | static_cast<Type_conversion_expression*>(f); | |
11935 | tce->set_may_convert_function_types(); | |
11936 | return f; | |
11937 | } | |
11938 | ||
11939 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
11940 | location); | |
11941 | ||
11942 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
c484d925 | 11943 | go_assert(vno != NULL); |
e440a328 | 11944 | Expression* ve = Expression::make_var_reference(vno, location); |
ab1468c3 | 11945 | Expression* bm; |
11946 | if (method != NULL) | |
11947 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
11948 | else | |
11949 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 11950 | |
11951 | // Even though we found the method above, if it has an error type we | |
11952 | // may see an error here. | |
11953 | if (bm->is_error_expression()) | |
463fe805 | 11954 | { |
11955 | gogo->finish_function(location); | |
11956 | return bm; | |
11957 | } | |
e440a328 | 11958 | |
11959 | Expression_list* args; | |
f470da59 | 11960 | if (parameters->size() <= 1) |
e440a328 | 11961 | args = NULL; |
11962 | else | |
11963 | { | |
11964 | args = new Expression_list(); | |
f470da59 | 11965 | Typed_identifier_list::const_iterator p = parameters->begin(); |
11966 | ++p; | |
11967 | for (; p != parameters->end(); ++p) | |
e440a328 | 11968 | { |
11969 | vno = gogo->lookup(p->name(), NULL); | |
c484d925 | 11970 | go_assert(vno != NULL); |
e440a328 | 11971 | args->push_back(Expression::make_var_reference(vno, location)); |
11972 | } | |
11973 | } | |
11974 | ||
ceeb4318 | 11975 | gogo->start_block(location); |
11976 | ||
e440a328 | 11977 | Call_expression* call = Expression::make_call(bm, args, |
11978 | method_type->is_varargs(), | |
11979 | location); | |
11980 | ||
0afbb937 | 11981 | Statement* s = Statement::make_return_from_call(call, location); |
e440a328 | 11982 | gogo->add_statement(s); |
11983 | ||
ceeb4318 | 11984 | Block* b = gogo->finish_block(location); |
11985 | ||
11986 | gogo->add_block(b, location); | |
11987 | ||
11988 | // Lower the call in case there are multiple results. | |
11989 | gogo->lower_block(no, b); | |
11990 | ||
e440a328 | 11991 | gogo->finish_function(location); |
11992 | ||
11993 | return Expression::make_func_reference(no, NULL, location); | |
11994 | } | |
11995 | ||
d751bb78 | 11996 | // Dump the ast for a selector expression. |
11997 | ||
11998 | void | |
11999 | Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12000 | const | |
12001 | { | |
12002 | ast_dump_context->dump_expression(this->left_); | |
12003 | ast_dump_context->ostream() << "."; | |
12004 | ast_dump_context->ostream() << this->name_; | |
12005 | } | |
12006 | ||
e440a328 | 12007 | // Make a selector expression. |
12008 | ||
12009 | Expression* | |
12010 | Expression::make_selector(Expression* left, const std::string& name, | |
b13c66cd | 12011 | Location location) |
e440a328 | 12012 | { |
12013 | return new Selector_expression(left, name, location); | |
12014 | } | |
12015 | ||
12016 | // Implement the builtin function new. | |
12017 | ||
12018 | class Allocation_expression : public Expression | |
12019 | { | |
12020 | public: | |
b13c66cd | 12021 | Allocation_expression(Type* type, Location location) |
e440a328 | 12022 | : Expression(EXPRESSION_ALLOCATION, location), |
12023 | type_(type) | |
12024 | { } | |
12025 | ||
12026 | protected: | |
12027 | int | |
12028 | do_traverse(Traverse* traverse) | |
12029 | { return Type::traverse(this->type_, traverse); } | |
12030 | ||
12031 | Type* | |
12032 | do_type() | |
12033 | { return Type::make_pointer_type(this->type_); } | |
12034 | ||
12035 | void | |
12036 | do_determine_type(const Type_context*) | |
12037 | { } | |
12038 | ||
e440a328 | 12039 | Expression* |
12040 | do_copy() | |
12041 | { return new Allocation_expression(this->type_, this->location()); } | |
12042 | ||
12043 | tree | |
12044 | do_get_tree(Translate_context*); | |
12045 | ||
d751bb78 | 12046 | void |
12047 | do_dump_expression(Ast_dump_context*) const; | |
12048 | ||
e440a328 | 12049 | private: |
12050 | // The type we are allocating. | |
12051 | Type* type_; | |
12052 | }; | |
12053 | ||
e440a328 | 12054 | // Return a tree for an allocation expression. |
12055 | ||
12056 | tree | |
12057 | Allocation_expression::do_get_tree(Translate_context* context) | |
12058 | { | |
9f0e0513 | 12059 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
19824ddb | 12060 | if (type_tree == error_mark_node) |
12061 | return error_mark_node; | |
e440a328 | 12062 | tree size_tree = TYPE_SIZE_UNIT(type_tree); |
12063 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
12064 | this->location()); | |
19824ddb | 12065 | if (space == error_mark_node) |
12066 | return error_mark_node; | |
e440a328 | 12067 | return fold_convert(build_pointer_type(type_tree), space); |
12068 | } | |
12069 | ||
d751bb78 | 12070 | // Dump ast representation for an allocation expression. |
12071 | ||
12072 | void | |
12073 | Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
12074 | const | |
12075 | { | |
12076 | ast_dump_context->ostream() << "new("; | |
12077 | ast_dump_context->dump_type(this->type_); | |
12078 | ast_dump_context->ostream() << ")"; | |
12079 | } | |
12080 | ||
e440a328 | 12081 | // Make an allocation expression. |
12082 | ||
12083 | Expression* | |
b13c66cd | 12084 | Expression::make_allocation(Type* type, Location location) |
e440a328 | 12085 | { |
12086 | return new Allocation_expression(type, location); | |
12087 | } | |
12088 | ||
e440a328 | 12089 | // Construct a struct. |
12090 | ||
12091 | class Struct_construction_expression : public Expression | |
12092 | { | |
12093 | public: | |
12094 | Struct_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 12095 | Location location) |
e440a328 | 12096 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), |
0c4f5a19 | 12097 | type_(type), vals_(vals), traverse_order_(NULL) |
e440a328 | 12098 | { } |
12099 | ||
0c4f5a19 | 12100 | // Set the traversal order, used to ensure that we implement the |
12101 | // order of evaluation rules. Takes ownership of the argument. | |
12102 | void | |
12103 | set_traverse_order(std::vector<int>* traverse_order) | |
12104 | { this->traverse_order_ = traverse_order; } | |
12105 | ||
e440a328 | 12106 | // Return whether this is a constant initializer. |
12107 | bool | |
12108 | is_constant_struct() const; | |
12109 | ||
12110 | protected: | |
12111 | int | |
12112 | do_traverse(Traverse* traverse); | |
12113 | ||
12114 | Type* | |
12115 | do_type() | |
12116 | { return this->type_; } | |
12117 | ||
12118 | void | |
12119 | do_determine_type(const Type_context*); | |
12120 | ||
12121 | void | |
12122 | do_check_types(Gogo*); | |
12123 | ||
12124 | Expression* | |
12125 | do_copy() | |
12126 | { | |
0c4f5a19 | 12127 | Struct_construction_expression* ret = |
12128 | new Struct_construction_expression(this->type_, this->vals_->copy(), | |
12129 | this->location()); | |
12130 | if (this->traverse_order_ != NULL) | |
12131 | ret->set_traverse_order(this->traverse_order_); | |
12132 | return ret; | |
e440a328 | 12133 | } |
12134 | ||
e440a328 | 12135 | tree |
12136 | do_get_tree(Translate_context*); | |
12137 | ||
12138 | void | |
12139 | do_export(Export*) const; | |
12140 | ||
d751bb78 | 12141 | void |
12142 | do_dump_expression(Ast_dump_context*) const; | |
12143 | ||
e440a328 | 12144 | private: |
12145 | // The type of the struct to construct. | |
12146 | Type* type_; | |
12147 | // The list of values, in order of the fields in the struct. A NULL | |
12148 | // entry means that the field should be zero-initialized. | |
12149 | Expression_list* vals_; | |
0c4f5a19 | 12150 | // If not NULL, the order in which to traverse vals_. This is used |
12151 | // so that we implement the order of evaluation rules correctly. | |
12152 | std::vector<int>* traverse_order_; | |
e440a328 | 12153 | }; |
12154 | ||
12155 | // Traversal. | |
12156 | ||
12157 | int | |
12158 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
12159 | { | |
0c4f5a19 | 12160 | if (this->vals_ != NULL) |
12161 | { | |
12162 | if (this->traverse_order_ == NULL) | |
12163 | { | |
12164 | if (this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12165 | return TRAVERSE_EXIT; | |
12166 | } | |
12167 | else | |
12168 | { | |
12169 | for (std::vector<int>::const_iterator p = | |
12170 | this->traverse_order_->begin(); | |
12171 | p != this->traverse_order_->end(); | |
12172 | ++p) | |
12173 | { | |
12174 | if (Expression::traverse(&this->vals_->at(*p), traverse) | |
12175 | == TRAVERSE_EXIT) | |
12176 | return TRAVERSE_EXIT; | |
12177 | } | |
12178 | } | |
12179 | } | |
e440a328 | 12180 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) |
12181 | return TRAVERSE_EXIT; | |
12182 | return TRAVERSE_CONTINUE; | |
12183 | } | |
12184 | ||
12185 | // Return whether this is a constant initializer. | |
12186 | ||
12187 | bool | |
12188 | Struct_construction_expression::is_constant_struct() const | |
12189 | { | |
12190 | if (this->vals_ == NULL) | |
12191 | return true; | |
12192 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12193 | pv != this->vals_->end(); | |
12194 | ++pv) | |
12195 | { | |
12196 | if (*pv != NULL | |
12197 | && !(*pv)->is_constant() | |
12198 | && (!(*pv)->is_composite_literal() | |
12199 | || (*pv)->is_nonconstant_composite_literal())) | |
12200 | return false; | |
12201 | } | |
12202 | ||
12203 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12204 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12205 | pf != fields->end(); | |
12206 | ++pf) | |
12207 | { | |
12208 | // There are no constant constructors for interfaces. | |
12209 | if (pf->type()->interface_type() != NULL) | |
12210 | return false; | |
12211 | } | |
12212 | ||
12213 | return true; | |
12214 | } | |
12215 | ||
12216 | // Final type determination. | |
12217 | ||
12218 | void | |
12219 | Struct_construction_expression::do_determine_type(const Type_context*) | |
12220 | { | |
12221 | if (this->vals_ == NULL) | |
12222 | return; | |
12223 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
12224 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12225 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12226 | pf != fields->end(); | |
12227 | ++pf, ++pv) | |
12228 | { | |
12229 | if (pv == this->vals_->end()) | |
12230 | return; | |
12231 | if (*pv != NULL) | |
12232 | { | |
12233 | Type_context subcontext(pf->type(), false); | |
12234 | (*pv)->determine_type(&subcontext); | |
12235 | } | |
12236 | } | |
a6cb4c0e | 12237 | // Extra values are an error we will report elsewhere; we still want |
12238 | // to determine the type to avoid knockon errors. | |
12239 | for (; pv != this->vals_->end(); ++pv) | |
12240 | (*pv)->determine_type_no_context(); | |
e440a328 | 12241 | } |
12242 | ||
12243 | // Check types. | |
12244 | ||
12245 | void | |
12246 | Struct_construction_expression::do_check_types(Gogo*) | |
12247 | { | |
12248 | if (this->vals_ == NULL) | |
12249 | return; | |
12250 | ||
12251 | Struct_type* st = this->type_->struct_type(); | |
12252 | if (this->vals_->size() > st->field_count()) | |
12253 | { | |
12254 | this->report_error(_("too many expressions for struct")); | |
12255 | return; | |
12256 | } | |
12257 | ||
12258 | const Struct_field_list* fields = st->fields(); | |
12259 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12260 | int i = 0; | |
12261 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
12262 | pf != fields->end(); | |
12263 | ++pf, ++pv, ++i) | |
12264 | { | |
12265 | if (pv == this->vals_->end()) | |
12266 | { | |
12267 | this->report_error(_("too few expressions for struct")); | |
12268 | break; | |
12269 | } | |
12270 | ||
12271 | if (*pv == NULL) | |
12272 | continue; | |
12273 | ||
12274 | std::string reason; | |
12275 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
12276 | { | |
12277 | if (reason.empty()) | |
12278 | error_at((*pv)->location(), | |
12279 | "incompatible type for field %d in struct construction", | |
12280 | i + 1); | |
12281 | else | |
12282 | error_at((*pv)->location(), | |
12283 | ("incompatible type for field %d in " | |
12284 | "struct construction (%s)"), | |
12285 | i + 1, reason.c_str()); | |
12286 | this->set_is_error(); | |
12287 | } | |
12288 | } | |
c484d925 | 12289 | go_assert(pv == this->vals_->end()); |
e440a328 | 12290 | } |
12291 | ||
12292 | // Return a tree for constructing a struct. | |
12293 | ||
12294 | tree | |
12295 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
12296 | { | |
12297 | Gogo* gogo = context->gogo(); | |
12298 | ||
12299 | if (this->vals_ == NULL) | |
63697958 | 12300 | { |
12301 | Btype* btype = this->type_->get_backend(gogo); | |
12302 | return expr_to_tree(gogo->backend()->zero_expression(btype)); | |
12303 | } | |
e440a328 | 12304 | |
9f0e0513 | 12305 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
e440a328 | 12306 | if (type_tree == error_mark_node) |
12307 | return error_mark_node; | |
c484d925 | 12308 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 12309 | |
12310 | bool is_constant = true; | |
12311 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
95f84544 | 12312 | vec<constructor_elt, va_gc> *elts; |
12313 | vec_alloc (elts, fields->size()); | |
e440a328 | 12314 | Struct_field_list::const_iterator pf = fields->begin(); |
12315 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12316 | for (tree field = TYPE_FIELDS(type_tree); | |
12317 | field != NULL_TREE; | |
12318 | field = DECL_CHAIN(field), ++pf) | |
12319 | { | |
c484d925 | 12320 | go_assert(pf != fields->end()); |
e440a328 | 12321 | |
63697958 | 12322 | Btype* fbtype = pf->type()->get_backend(gogo); |
12323 | ||
e440a328 | 12324 | tree val; |
12325 | if (pv == this->vals_->end()) | |
63697958 | 12326 | val = expr_to_tree(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12327 | else if (*pv == NULL) |
12328 | { | |
63697958 | 12329 | val = expr_to_tree(gogo->backend()->zero_expression(fbtype)); |
e440a328 | 12330 | ++pv; |
12331 | } | |
12332 | else | |
12333 | { | |
12334 | val = Expression::convert_for_assignment(context, pf->type(), | |
12335 | (*pv)->type(), | |
12336 | (*pv)->get_tree(context), | |
12337 | this->location()); | |
12338 | ++pv; | |
12339 | } | |
12340 | ||
12341 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
12342 | return error_mark_node; | |
12343 | ||
e82e4eb5 | 12344 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12345 | constructor_elt* elt = elts->quick_push(empty); |
e440a328 | 12346 | elt->index = field; |
12347 | elt->value = val; | |
12348 | if (!TREE_CONSTANT(val)) | |
12349 | is_constant = false; | |
12350 | } | |
c484d925 | 12351 | go_assert(pf == fields->end()); |
e440a328 | 12352 | |
12353 | tree ret = build_constructor(type_tree, elts); | |
12354 | if (is_constant) | |
12355 | TREE_CONSTANT(ret) = 1; | |
12356 | return ret; | |
12357 | } | |
12358 | ||
12359 | // Export a struct construction. | |
12360 | ||
12361 | void | |
12362 | Struct_construction_expression::do_export(Export* exp) const | |
12363 | { | |
12364 | exp->write_c_string("convert("); | |
12365 | exp->write_type(this->type_); | |
12366 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12367 | pv != this->vals_->end(); | |
12368 | ++pv) | |
12369 | { | |
12370 | exp->write_c_string(", "); | |
12371 | if (*pv != NULL) | |
12372 | (*pv)->export_expression(exp); | |
12373 | } | |
12374 | exp->write_c_string(")"); | |
12375 | } | |
12376 | ||
d751bb78 | 12377 | // Dump ast representation of a struct construction expression. |
12378 | ||
12379 | void | |
12380 | Struct_construction_expression::do_dump_expression( | |
12381 | Ast_dump_context* ast_dump_context) const | |
12382 | { | |
d751bb78 | 12383 | ast_dump_context->dump_type(this->type_); |
12384 | ast_dump_context->ostream() << "{"; | |
12385 | ast_dump_context->dump_expression_list(this->vals_); | |
12386 | ast_dump_context->ostream() << "}"; | |
12387 | } | |
12388 | ||
e440a328 | 12389 | // Make a struct composite literal. This used by the thunk code. |
12390 | ||
12391 | Expression* | |
12392 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12393 | Location location) |
e440a328 | 12394 | { |
c484d925 | 12395 | go_assert(type->struct_type() != NULL); |
e440a328 | 12396 | return new Struct_construction_expression(type, vals, location); |
12397 | } | |
12398 | ||
12399 | // Construct an array. This class is not used directly; instead we | |
12400 | // use the child classes, Fixed_array_construction_expression and | |
12401 | // Open_array_construction_expression. | |
12402 | ||
12403 | class Array_construction_expression : public Expression | |
12404 | { | |
12405 | protected: | |
12406 | Array_construction_expression(Expression_classification classification, | |
ffe743ca | 12407 | Type* type, |
12408 | const std::vector<unsigned long>* indexes, | |
12409 | Expression_list* vals, Location location) | |
e440a328 | 12410 | : Expression(classification, location), |
ffe743ca | 12411 | type_(type), indexes_(indexes), vals_(vals) |
12412 | { go_assert(indexes == NULL || indexes->size() == vals->size()); } | |
e440a328 | 12413 | |
12414 | public: | |
12415 | // Return whether this is a constant initializer. | |
12416 | bool | |
12417 | is_constant_array() const; | |
12418 | ||
12419 | // Return the number of elements. | |
12420 | size_t | |
12421 | element_count() const | |
12422 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
12423 | ||
12424 | protected: | |
12425 | int | |
12426 | do_traverse(Traverse* traverse); | |
12427 | ||
12428 | Type* | |
12429 | do_type() | |
12430 | { return this->type_; } | |
12431 | ||
12432 | void | |
12433 | do_determine_type(const Type_context*); | |
12434 | ||
12435 | void | |
12436 | do_check_types(Gogo*); | |
12437 | ||
e440a328 | 12438 | void |
12439 | do_export(Export*) const; | |
12440 | ||
ffe743ca | 12441 | // The indexes. |
12442 | const std::vector<unsigned long>* | |
12443 | indexes() | |
12444 | { return this->indexes_; } | |
12445 | ||
e440a328 | 12446 | // The list of values. |
12447 | Expression_list* | |
12448 | vals() | |
12449 | { return this->vals_; } | |
12450 | ||
12451 | // Get a constructor tree for the array values. | |
12452 | tree | |
12453 | get_constructor_tree(Translate_context* context, tree type_tree); | |
12454 | ||
d751bb78 | 12455 | void |
12456 | do_dump_expression(Ast_dump_context*) const; | |
12457 | ||
e440a328 | 12458 | private: |
12459 | // The type of the array to construct. | |
12460 | Type* type_; | |
ffe743ca | 12461 | // The list of indexes into the array, one for each value. This may |
12462 | // be NULL, in which case the indexes start at zero and increment. | |
12463 | const std::vector<unsigned long>* indexes_; | |
12464 | // The list of values. This may be NULL if there are no values. | |
e440a328 | 12465 | Expression_list* vals_; |
12466 | }; | |
12467 | ||
12468 | // Traversal. | |
12469 | ||
12470 | int | |
12471 | Array_construction_expression::do_traverse(Traverse* traverse) | |
12472 | { | |
12473 | if (this->vals_ != NULL | |
12474 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12475 | return TRAVERSE_EXIT; | |
12476 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12477 | return TRAVERSE_EXIT; | |
12478 | return TRAVERSE_CONTINUE; | |
12479 | } | |
12480 | ||
12481 | // Return whether this is a constant initializer. | |
12482 | ||
12483 | bool | |
12484 | Array_construction_expression::is_constant_array() const | |
12485 | { | |
12486 | if (this->vals_ == NULL) | |
12487 | return true; | |
12488 | ||
12489 | // There are no constant constructors for interfaces. | |
12490 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
12491 | return false; | |
12492 | ||
12493 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12494 | pv != this->vals_->end(); | |
12495 | ++pv) | |
12496 | { | |
12497 | if (*pv != NULL | |
12498 | && !(*pv)->is_constant() | |
12499 | && (!(*pv)->is_composite_literal() | |
12500 | || (*pv)->is_nonconstant_composite_literal())) | |
12501 | return false; | |
12502 | } | |
12503 | return true; | |
12504 | } | |
12505 | ||
12506 | // Final type determination. | |
12507 | ||
12508 | void | |
12509 | Array_construction_expression::do_determine_type(const Type_context*) | |
12510 | { | |
12511 | if (this->vals_ == NULL) | |
12512 | return; | |
12513 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
12514 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12515 | pv != this->vals_->end(); | |
12516 | ++pv) | |
12517 | { | |
12518 | if (*pv != NULL) | |
12519 | (*pv)->determine_type(&subcontext); | |
12520 | } | |
12521 | } | |
12522 | ||
12523 | // Check types. | |
12524 | ||
12525 | void | |
12526 | Array_construction_expression::do_check_types(Gogo*) | |
12527 | { | |
12528 | if (this->vals_ == NULL) | |
12529 | return; | |
12530 | ||
12531 | Array_type* at = this->type_->array_type(); | |
12532 | int i = 0; | |
12533 | Type* element_type = at->element_type(); | |
12534 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12535 | pv != this->vals_->end(); | |
12536 | ++pv, ++i) | |
12537 | { | |
12538 | if (*pv != NULL | |
12539 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
12540 | { | |
12541 | error_at((*pv)->location(), | |
12542 | "incompatible type for element %d in composite literal", | |
12543 | i + 1); | |
12544 | this->set_is_error(); | |
12545 | } | |
12546 | } | |
e440a328 | 12547 | } |
12548 | ||
12549 | // Get a constructor tree for the array values. | |
12550 | ||
12551 | tree | |
12552 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
12553 | tree type_tree) | |
12554 | { | |
95f84544 | 12555 | vec<constructor_elt, va_gc> *values; |
12556 | vec_alloc (values, (this->vals_ == NULL ? 0 : this->vals_->size())); | |
e440a328 | 12557 | Type* element_type = this->type_->array_type()->element_type(); |
12558 | bool is_constant = true; | |
12559 | if (this->vals_ != NULL) | |
12560 | { | |
12561 | size_t i = 0; | |
ffe743ca | 12562 | std::vector<unsigned long>::const_iterator pi; |
12563 | if (this->indexes_ != NULL) | |
12564 | pi = this->indexes_->begin(); | |
e440a328 | 12565 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12566 | pv != this->vals_->end(); | |
12567 | ++pv, ++i) | |
12568 | { | |
ffe743ca | 12569 | if (this->indexes_ != NULL) |
12570 | go_assert(pi != this->indexes_->end()); | |
e82e4eb5 | 12571 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12572 | constructor_elt* elt = values->quick_push(empty); |
ffe743ca | 12573 | |
12574 | if (this->indexes_ == NULL) | |
12575 | elt->index = size_int(i); | |
12576 | else | |
12577 | elt->index = size_int(*pi); | |
12578 | ||
e440a328 | 12579 | if (*pv == NULL) |
63697958 | 12580 | { |
12581 | Gogo* gogo = context->gogo(); | |
12582 | Btype* ebtype = element_type->get_backend(gogo); | |
12583 | Bexpression *zv = gogo->backend()->zero_expression(ebtype); | |
12584 | elt->value = expr_to_tree(zv); | |
12585 | } | |
e440a328 | 12586 | else |
12587 | { | |
12588 | tree value_tree = (*pv)->get_tree(context); | |
12589 | elt->value = Expression::convert_for_assignment(context, | |
12590 | element_type, | |
12591 | (*pv)->type(), | |
12592 | value_tree, | |
12593 | this->location()); | |
12594 | } | |
12595 | if (elt->value == error_mark_node) | |
12596 | return error_mark_node; | |
12597 | if (!TREE_CONSTANT(elt->value)) | |
12598 | is_constant = false; | |
ffe743ca | 12599 | if (this->indexes_ != NULL) |
12600 | ++pi; | |
e440a328 | 12601 | } |
ffe743ca | 12602 | if (this->indexes_ != NULL) |
12603 | go_assert(pi == this->indexes_->end()); | |
e440a328 | 12604 | } |
12605 | ||
12606 | tree ret = build_constructor(type_tree, values); | |
12607 | if (is_constant) | |
12608 | TREE_CONSTANT(ret) = 1; | |
12609 | return ret; | |
12610 | } | |
12611 | ||
12612 | // Export an array construction. | |
12613 | ||
12614 | void | |
12615 | Array_construction_expression::do_export(Export* exp) const | |
12616 | { | |
12617 | exp->write_c_string("convert("); | |
12618 | exp->write_type(this->type_); | |
12619 | if (this->vals_ != NULL) | |
12620 | { | |
ffe743ca | 12621 | std::vector<unsigned long>::const_iterator pi; |
12622 | if (this->indexes_ != NULL) | |
12623 | pi = this->indexes_->begin(); | |
e440a328 | 12624 | for (Expression_list::const_iterator pv = this->vals_->begin(); |
12625 | pv != this->vals_->end(); | |
12626 | ++pv) | |
12627 | { | |
12628 | exp->write_c_string(", "); | |
ffe743ca | 12629 | |
12630 | if (this->indexes_ != NULL) | |
12631 | { | |
12632 | char buf[100]; | |
12633 | snprintf(buf, sizeof buf, "%lu", *pi); | |
12634 | exp->write_c_string(buf); | |
12635 | exp->write_c_string(":"); | |
12636 | } | |
12637 | ||
e440a328 | 12638 | if (*pv != NULL) |
12639 | (*pv)->export_expression(exp); | |
ffe743ca | 12640 | |
12641 | if (this->indexes_ != NULL) | |
12642 | ++pi; | |
e440a328 | 12643 | } |
12644 | } | |
12645 | exp->write_c_string(")"); | |
12646 | } | |
12647 | ||
d751bb78 | 12648 | // Dump ast representation of an array construction expressin. |
12649 | ||
12650 | void | |
12651 | Array_construction_expression::do_dump_expression( | |
12652 | Ast_dump_context* ast_dump_context) const | |
12653 | { | |
ffe743ca | 12654 | Expression* length = this->type_->array_type()->length(); |
8b1c301d | 12655 | |
12656 | ast_dump_context->ostream() << "[" ; | |
12657 | if (length != NULL) | |
12658 | { | |
12659 | ast_dump_context->dump_expression(length); | |
12660 | } | |
12661 | ast_dump_context->ostream() << "]" ; | |
d751bb78 | 12662 | ast_dump_context->dump_type(this->type_); |
12663 | ast_dump_context->ostream() << "{" ; | |
ffe743ca | 12664 | if (this->indexes_ == NULL) |
12665 | ast_dump_context->dump_expression_list(this->vals_); | |
12666 | else | |
12667 | { | |
12668 | Expression_list::const_iterator pv = this->vals_->begin(); | |
12669 | for (std::vector<unsigned long>::const_iterator pi = | |
12670 | this->indexes_->begin(); | |
12671 | pi != this->indexes_->end(); | |
12672 | ++pi, ++pv) | |
12673 | { | |
12674 | if (pi != this->indexes_->begin()) | |
12675 | ast_dump_context->ostream() << ", "; | |
12676 | ast_dump_context->ostream() << *pi << ':'; | |
12677 | ast_dump_context->dump_expression(*pv); | |
12678 | } | |
12679 | } | |
d751bb78 | 12680 | ast_dump_context->ostream() << "}" ; |
12681 | ||
12682 | } | |
12683 | ||
e440a328 | 12684 | // Construct a fixed array. |
12685 | ||
12686 | class Fixed_array_construction_expression : | |
12687 | public Array_construction_expression | |
12688 | { | |
12689 | public: | |
ffe743ca | 12690 | Fixed_array_construction_expression(Type* type, |
12691 | const std::vector<unsigned long>* indexes, | |
12692 | Expression_list* vals, Location location) | |
e440a328 | 12693 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, |
ffe743ca | 12694 | type, indexes, vals, location) |
12695 | { go_assert(type->array_type() != NULL && !type->is_slice_type()); } | |
e440a328 | 12696 | |
12697 | protected: | |
12698 | Expression* | |
12699 | do_copy() | |
12700 | { | |
12701 | return new Fixed_array_construction_expression(this->type(), | |
ffe743ca | 12702 | this->indexes(), |
e440a328 | 12703 | (this->vals() == NULL |
12704 | ? NULL | |
12705 | : this->vals()->copy()), | |
12706 | this->location()); | |
12707 | } | |
12708 | ||
12709 | tree | |
12710 | do_get_tree(Translate_context*); | |
12711 | }; | |
12712 | ||
12713 | // Return a tree for constructing a fixed array. | |
12714 | ||
12715 | tree | |
12716 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
12717 | { | |
9f0e0513 | 12718 | Type* type = this->type(); |
12719 | Btype* btype = type->get_backend(context->gogo()); | |
12720 | return this->get_constructor_tree(context, type_to_tree(btype)); | |
e440a328 | 12721 | } |
12722 | ||
12723 | // Construct an open array. | |
12724 | ||
12725 | class Open_array_construction_expression : public Array_construction_expression | |
12726 | { | |
12727 | public: | |
ffe743ca | 12728 | Open_array_construction_expression(Type* type, |
12729 | const std::vector<unsigned long>* indexes, | |
12730 | Expression_list* vals, Location location) | |
e440a328 | 12731 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, |
ffe743ca | 12732 | type, indexes, vals, location) |
12733 | { go_assert(type->is_slice_type()); } | |
e440a328 | 12734 | |
12735 | protected: | |
12736 | // Note that taking the address of an open array literal is invalid. | |
12737 | ||
12738 | Expression* | |
12739 | do_copy() | |
12740 | { | |
12741 | return new Open_array_construction_expression(this->type(), | |
ffe743ca | 12742 | this->indexes(), |
e440a328 | 12743 | (this->vals() == NULL |
12744 | ? NULL | |
12745 | : this->vals()->copy()), | |
12746 | this->location()); | |
12747 | } | |
12748 | ||
12749 | tree | |
12750 | do_get_tree(Translate_context*); | |
12751 | }; | |
12752 | ||
12753 | // Return a tree for constructing an open array. | |
12754 | ||
12755 | tree | |
12756 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
12757 | { | |
f9c68f17 | 12758 | Array_type* array_type = this->type()->array_type(); |
12759 | if (array_type == NULL) | |
12760 | { | |
c484d925 | 12761 | go_assert(this->type()->is_error()); |
f9c68f17 | 12762 | return error_mark_node; |
12763 | } | |
12764 | ||
12765 | Type* element_type = array_type->element_type(); | |
9f0e0513 | 12766 | Btype* belement_type = element_type->get_backend(context->gogo()); |
12767 | tree element_type_tree = type_to_tree(belement_type); | |
3d60812e | 12768 | if (element_type_tree == error_mark_node) |
12769 | return error_mark_node; | |
12770 | ||
e440a328 | 12771 | tree values; |
12772 | tree length_tree; | |
12773 | if (this->vals() == NULL || this->vals()->empty()) | |
12774 | { | |
12775 | // We need to create a unique value. | |
12776 | tree max = size_int(0); | |
12777 | tree constructor_type = build_array_type(element_type_tree, | |
12778 | build_index_type(max)); | |
12779 | if (constructor_type == error_mark_node) | |
12780 | return error_mark_node; | |
95f84544 | 12781 | vec<constructor_elt, va_gc> *vec; |
12782 | vec_alloc(vec, 1); | |
e82e4eb5 | 12783 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12784 | constructor_elt* elt = vec->quick_push(empty); |
e440a328 | 12785 | elt->index = size_int(0); |
63697958 | 12786 | Gogo* gogo = context->gogo(); |
12787 | Btype* btype = element_type->get_backend(gogo); | |
12788 | elt->value = expr_to_tree(gogo->backend()->zero_expression(btype)); | |
e440a328 | 12789 | values = build_constructor(constructor_type, vec); |
12790 | if (TREE_CONSTANT(elt->value)) | |
12791 | TREE_CONSTANT(values) = 1; | |
12792 | length_tree = size_int(0); | |
12793 | } | |
12794 | else | |
12795 | { | |
ffe743ca | 12796 | unsigned long max_index; |
12797 | if (this->indexes() == NULL) | |
12798 | max_index = this->vals()->size() - 1; | |
12799 | else | |
00773463 | 12800 | max_index = this->indexes()->back(); |
ffe743ca | 12801 | tree max_tree = size_int(max_index); |
e440a328 | 12802 | tree constructor_type = build_array_type(element_type_tree, |
ffe743ca | 12803 | build_index_type(max_tree)); |
e440a328 | 12804 | if (constructor_type == error_mark_node) |
12805 | return error_mark_node; | |
12806 | values = this->get_constructor_tree(context, constructor_type); | |
ffe743ca | 12807 | length_tree = size_int(max_index + 1); |
e440a328 | 12808 | } |
12809 | ||
12810 | if (values == error_mark_node) | |
12811 | return error_mark_node; | |
12812 | ||
12813 | bool is_constant_initializer = TREE_CONSTANT(values); | |
d8829beb | 12814 | |
12815 | // We have to copy the initial values into heap memory if we are in | |
12816 | // a function or if the values are not constants. We also have to | |
12817 | // copy them if they may contain pointers in a non-constant context, | |
12818 | // as otherwise the garbage collector won't see them. | |
12819 | bool copy_to_heap = (context->function() != NULL | |
12820 | || !is_constant_initializer | |
12821 | || (element_type->has_pointer() | |
12822 | && !context->is_const())); | |
e440a328 | 12823 | |
12824 | if (is_constant_initializer) | |
12825 | { | |
b13c66cd | 12826 | tree tmp = build_decl(this->location().gcc_location(), VAR_DECL, |
e440a328 | 12827 | create_tmp_var_name("C"), TREE_TYPE(values)); |
12828 | DECL_EXTERNAL(tmp) = 0; | |
12829 | TREE_PUBLIC(tmp) = 0; | |
12830 | TREE_STATIC(tmp) = 1; | |
12831 | DECL_ARTIFICIAL(tmp) = 1; | |
d8829beb | 12832 | if (copy_to_heap) |
e440a328 | 12833 | { |
d8829beb | 12834 | // If we are not copying the value to the heap, we will only |
12835 | // initialize the value once, so we can use this directly | |
12836 | // rather than copying it. In that case we can't make it | |
12837 | // read-only, because the program is permitted to change it. | |
e440a328 | 12838 | TREE_READONLY(tmp) = 1; |
12839 | TREE_CONSTANT(tmp) = 1; | |
12840 | } | |
12841 | DECL_INITIAL(tmp) = values; | |
12842 | rest_of_decl_compilation(tmp, 1, 0); | |
12843 | values = tmp; | |
12844 | } | |
12845 | ||
12846 | tree space; | |
12847 | tree set; | |
d8829beb | 12848 | if (!copy_to_heap) |
e440a328 | 12849 | { |
d8829beb | 12850 | // the initializer will only run once. |
e440a328 | 12851 | space = build_fold_addr_expr(values); |
12852 | set = NULL_TREE; | |
12853 | } | |
12854 | else | |
12855 | { | |
12856 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
12857 | space = context->gogo()->allocate_memory(element_type, memsize, | |
12858 | this->location()); | |
12859 | space = save_expr(space); | |
12860 | ||
12861 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
b13c66cd | 12862 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
12863 | s); | |
e440a328 | 12864 | TREE_THIS_NOTRAP(ref) = 1; |
12865 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
12866 | } | |
12867 | ||
12868 | // Build a constructor for the open array. | |
12869 | ||
9f0e0513 | 12870 | tree type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
3d60812e | 12871 | if (type_tree == error_mark_node) |
12872 | return error_mark_node; | |
c484d925 | 12873 | go_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
e440a328 | 12874 | |
95f84544 | 12875 | vec<constructor_elt, va_gc> *init; |
12876 | vec_alloc(init, 3); | |
e440a328 | 12877 | |
e82e4eb5 | 12878 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 12879 | constructor_elt* elt = init->quick_push(empty); |
e440a328 | 12880 | tree field = TYPE_FIELDS(type_tree); |
c484d925 | 12881 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); |
e440a328 | 12882 | elt->index = field; |
12883 | elt->value = fold_convert(TREE_TYPE(field), space); | |
12884 | ||
95f84544 | 12885 | elt = init->quick_push(empty); |
e440a328 | 12886 | field = DECL_CHAIN(field); |
c484d925 | 12887 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); |
e440a328 | 12888 | elt->index = field; |
12889 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
12890 | ||
95f84544 | 12891 | elt = init->quick_push(empty); |
e440a328 | 12892 | field = DECL_CHAIN(field); |
c484d925 | 12893 | go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); |
e440a328 | 12894 | elt->index = field; |
12895 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
12896 | ||
12897 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 12898 | if (constructor == error_mark_node) |
12899 | return error_mark_node; | |
d8829beb | 12900 | if (!copy_to_heap) |
e440a328 | 12901 | TREE_CONSTANT(constructor) = 1; |
12902 | ||
12903 | if (set == NULL_TREE) | |
12904 | return constructor; | |
12905 | else | |
12906 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
12907 | } | |
12908 | ||
12909 | // Make a slice composite literal. This is used by the type | |
12910 | // descriptor code. | |
12911 | ||
12912 | Expression* | |
12913 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
b13c66cd | 12914 | Location location) |
e440a328 | 12915 | { |
411eb89e | 12916 | go_assert(type->is_slice_type()); |
ffe743ca | 12917 | return new Open_array_construction_expression(type, NULL, vals, location); |
e440a328 | 12918 | } |
12919 | ||
12920 | // Construct a map. | |
12921 | ||
12922 | class Map_construction_expression : public Expression | |
12923 | { | |
12924 | public: | |
12925 | Map_construction_expression(Type* type, Expression_list* vals, | |
b13c66cd | 12926 | Location location) |
e440a328 | 12927 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), |
12928 | type_(type), vals_(vals) | |
c484d925 | 12929 | { go_assert(vals == NULL || vals->size() % 2 == 0); } |
e440a328 | 12930 | |
12931 | protected: | |
12932 | int | |
12933 | do_traverse(Traverse* traverse); | |
12934 | ||
12935 | Type* | |
12936 | do_type() | |
12937 | { return this->type_; } | |
12938 | ||
12939 | void | |
12940 | do_determine_type(const Type_context*); | |
12941 | ||
12942 | void | |
12943 | do_check_types(Gogo*); | |
12944 | ||
12945 | Expression* | |
12946 | do_copy() | |
12947 | { | |
12948 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
12949 | this->location()); | |
12950 | } | |
12951 | ||
12952 | tree | |
12953 | do_get_tree(Translate_context*); | |
12954 | ||
12955 | void | |
12956 | do_export(Export*) const; | |
12957 | ||
d751bb78 | 12958 | void |
12959 | do_dump_expression(Ast_dump_context*) const; | |
12960 | ||
e440a328 | 12961 | private: |
12962 | // The type of the map to construct. | |
12963 | Type* type_; | |
12964 | // The list of values. | |
12965 | Expression_list* vals_; | |
12966 | }; | |
12967 | ||
12968 | // Traversal. | |
12969 | ||
12970 | int | |
12971 | Map_construction_expression::do_traverse(Traverse* traverse) | |
12972 | { | |
12973 | if (this->vals_ != NULL | |
12974 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
12975 | return TRAVERSE_EXIT; | |
12976 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12977 | return TRAVERSE_EXIT; | |
12978 | return TRAVERSE_CONTINUE; | |
12979 | } | |
12980 | ||
12981 | // Final type determination. | |
12982 | ||
12983 | void | |
12984 | Map_construction_expression::do_determine_type(const Type_context*) | |
12985 | { | |
12986 | if (this->vals_ == NULL) | |
12987 | return; | |
12988 | ||
12989 | Map_type* mt = this->type_->map_type(); | |
12990 | Type_context key_context(mt->key_type(), false); | |
12991 | Type_context val_context(mt->val_type(), false); | |
12992 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
12993 | pv != this->vals_->end(); | |
12994 | ++pv) | |
12995 | { | |
12996 | (*pv)->determine_type(&key_context); | |
12997 | ++pv; | |
12998 | (*pv)->determine_type(&val_context); | |
12999 | } | |
13000 | } | |
13001 | ||
13002 | // Check types. | |
13003 | ||
13004 | void | |
13005 | Map_construction_expression::do_check_types(Gogo*) | |
13006 | { | |
13007 | if (this->vals_ == NULL) | |
13008 | return; | |
13009 | ||
13010 | Map_type* mt = this->type_->map_type(); | |
13011 | int i = 0; | |
13012 | Type* key_type = mt->key_type(); | |
13013 | Type* val_type = mt->val_type(); | |
13014 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13015 | pv != this->vals_->end(); | |
13016 | ++pv, ++i) | |
13017 | { | |
13018 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
13019 | { | |
13020 | error_at((*pv)->location(), | |
13021 | "incompatible type for element %d key in map construction", | |
13022 | i + 1); | |
13023 | this->set_is_error(); | |
13024 | } | |
13025 | ++pv; | |
13026 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
13027 | { | |
13028 | error_at((*pv)->location(), | |
13029 | ("incompatible type for element %d value " | |
13030 | "in map construction"), | |
13031 | i + 1); | |
13032 | this->set_is_error(); | |
13033 | } | |
13034 | } | |
13035 | } | |
13036 | ||
13037 | // Return a tree for constructing a map. | |
13038 | ||
13039 | tree | |
13040 | Map_construction_expression::do_get_tree(Translate_context* context) | |
13041 | { | |
13042 | Gogo* gogo = context->gogo(); | |
b13c66cd | 13043 | Location loc = this->location(); |
e440a328 | 13044 | |
13045 | Map_type* mt = this->type_->map_type(); | |
13046 | ||
13047 | // Build a struct to hold the key and value. | |
13048 | tree struct_type = make_node(RECORD_TYPE); | |
13049 | ||
13050 | Type* key_type = mt->key_type(); | |
13051 | tree id = get_identifier("__key"); | |
9f0e0513 | 13052 | tree key_type_tree = type_to_tree(key_type->get_backend(gogo)); |
5845bde6 | 13053 | if (key_type_tree == error_mark_node) |
13054 | return error_mark_node; | |
b13c66cd | 13055 | tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id, |
13056 | key_type_tree); | |
e440a328 | 13057 | DECL_CONTEXT(key_field) = struct_type; |
13058 | TYPE_FIELDS(struct_type) = key_field; | |
13059 | ||
13060 | Type* val_type = mt->val_type(); | |
13061 | id = get_identifier("__val"); | |
9f0e0513 | 13062 | tree val_type_tree = type_to_tree(val_type->get_backend(gogo)); |
5845bde6 | 13063 | if (val_type_tree == error_mark_node) |
13064 | return error_mark_node; | |
b13c66cd | 13065 | tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id, |
13066 | val_type_tree); | |
e440a328 | 13067 | DECL_CONTEXT(val_field) = struct_type; |
13068 | DECL_CHAIN(key_field) = val_field; | |
13069 | ||
13070 | layout_type(struct_type); | |
13071 | ||
13072 | bool is_constant = true; | |
13073 | size_t i = 0; | |
13074 | tree valaddr; | |
13075 | tree make_tmp; | |
13076 | ||
13077 | if (this->vals_ == NULL || this->vals_->empty()) | |
13078 | { | |
13079 | valaddr = null_pointer_node; | |
13080 | make_tmp = NULL_TREE; | |
13081 | } | |
13082 | else | |
13083 | { | |
95f84544 | 13084 | vec<constructor_elt, va_gc> *values; |
13085 | vec_alloc(values, this->vals_->size() / 2); | |
e440a328 | 13086 | |
13087 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13088 | pv != this->vals_->end(); | |
13089 | ++pv, ++i) | |
13090 | { | |
13091 | bool one_is_constant = true; | |
13092 | ||
95f84544 | 13093 | vec<constructor_elt, va_gc> *one; |
13094 | vec_alloc(one, 2); | |
e440a328 | 13095 | |
e82e4eb5 | 13096 | constructor_elt empty = {NULL, NULL}; |
95f84544 | 13097 | constructor_elt* elt = one->quick_push(empty); |
e440a328 | 13098 | elt->index = key_field; |
13099 | tree val_tree = (*pv)->get_tree(context); | |
13100 | elt->value = Expression::convert_for_assignment(context, key_type, | |
13101 | (*pv)->type(), | |
13102 | val_tree, loc); | |
13103 | if (elt->value == error_mark_node) | |
13104 | return error_mark_node; | |
13105 | if (!TREE_CONSTANT(elt->value)) | |
13106 | one_is_constant = false; | |
13107 | ||
13108 | ++pv; | |
13109 | ||
95f84544 | 13110 | elt = one->quick_push(empty); |
e440a328 | 13111 | elt->index = val_field; |
13112 | val_tree = (*pv)->get_tree(context); | |
13113 | elt->value = Expression::convert_for_assignment(context, val_type, | |
13114 | (*pv)->type(), | |
13115 | val_tree, loc); | |
13116 | if (elt->value == error_mark_node) | |
13117 | return error_mark_node; | |
13118 | if (!TREE_CONSTANT(elt->value)) | |
13119 | one_is_constant = false; | |
13120 | ||
95f84544 | 13121 | elt = values->quick_push(empty); |
e440a328 | 13122 | elt->index = size_int(i); |
13123 | elt->value = build_constructor(struct_type, one); | |
13124 | if (one_is_constant) | |
13125 | TREE_CONSTANT(elt->value) = 1; | |
13126 | else | |
13127 | is_constant = false; | |
13128 | } | |
13129 | ||
13130 | tree index_type = build_index_type(size_int(i - 1)); | |
13131 | tree array_type = build_array_type(struct_type, index_type); | |
13132 | tree init = build_constructor(array_type, values); | |
13133 | if (is_constant) | |
13134 | TREE_CONSTANT(init) = 1; | |
13135 | tree tmp; | |
13136 | if (current_function_decl != NULL) | |
13137 | { | |
13138 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
13139 | DECL_INITIAL(tmp) = init; | |
b13c66cd | 13140 | make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR, |
13141 | void_type_node, tmp); | |
e440a328 | 13142 | TREE_ADDRESSABLE(tmp) = 1; |
13143 | } | |
13144 | else | |
13145 | { | |
b13c66cd | 13146 | tmp = build_decl(loc.gcc_location(), VAR_DECL, |
13147 | create_tmp_var_name("M"), array_type); | |
e440a328 | 13148 | DECL_EXTERNAL(tmp) = 0; |
13149 | TREE_PUBLIC(tmp) = 0; | |
13150 | TREE_STATIC(tmp) = 1; | |
13151 | DECL_ARTIFICIAL(tmp) = 1; | |
13152 | if (!TREE_CONSTANT(init)) | |
b13c66cd | 13153 | make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR, |
13154 | void_type_node, tmp, init); | |
e440a328 | 13155 | else |
13156 | { | |
13157 | TREE_READONLY(tmp) = 1; | |
13158 | TREE_CONSTANT(tmp) = 1; | |
13159 | DECL_INITIAL(tmp) = init; | |
13160 | make_tmp = NULL_TREE; | |
13161 | } | |
13162 | rest_of_decl_compilation(tmp, 1, 0); | |
13163 | } | |
13164 | ||
13165 | valaddr = build_fold_addr_expr(tmp); | |
13166 | } | |
13167 | ||
2b5f213d | 13168 | tree descriptor = mt->map_descriptor_pointer(gogo, loc); |
e440a328 | 13169 | |
9f0e0513 | 13170 | tree type_tree = type_to_tree(this->type_->get_backend(gogo)); |
5845bde6 | 13171 | if (type_tree == error_mark_node) |
13172 | return error_mark_node; | |
e440a328 | 13173 | |
13174 | static tree construct_map_fndecl; | |
13175 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
13176 | loc, | |
13177 | "__go_construct_map", | |
13178 | 6, | |
13179 | type_tree, | |
13180 | TREE_TYPE(descriptor), | |
13181 | descriptor, | |
13182 | sizetype, | |
13183 | size_int(i), | |
13184 | sizetype, | |
13185 | TYPE_SIZE_UNIT(struct_type), | |
13186 | sizetype, | |
13187 | byte_position(val_field), | |
13188 | sizetype, | |
13189 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
13190 | const_ptr_type_node, | |
13191 | fold_convert(const_ptr_type_node, valaddr)); | |
5fb82b5e | 13192 | if (call == error_mark_node) |
13193 | return error_mark_node; | |
e440a328 | 13194 | |
13195 | tree ret; | |
13196 | if (make_tmp == NULL) | |
13197 | ret = call; | |
13198 | else | |
b13c66cd | 13199 | ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree, |
13200 | make_tmp, call); | |
e440a328 | 13201 | return ret; |
13202 | } | |
13203 | ||
13204 | // Export an array construction. | |
13205 | ||
13206 | void | |
13207 | Map_construction_expression::do_export(Export* exp) const | |
13208 | { | |
13209 | exp->write_c_string("convert("); | |
13210 | exp->write_type(this->type_); | |
13211 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
13212 | pv != this->vals_->end(); | |
13213 | ++pv) | |
13214 | { | |
13215 | exp->write_c_string(", "); | |
13216 | (*pv)->export_expression(exp); | |
13217 | } | |
13218 | exp->write_c_string(")"); | |
13219 | } | |
13220 | ||
d751bb78 | 13221 | // Dump ast representation for a map construction expression. |
13222 | ||
13223 | void | |
13224 | Map_construction_expression::do_dump_expression( | |
13225 | Ast_dump_context* ast_dump_context) const | |
13226 | { | |
d751bb78 | 13227 | ast_dump_context->ostream() << "{" ; |
8b1c301d | 13228 | ast_dump_context->dump_expression_list(this->vals_, true); |
d751bb78 | 13229 | ast_dump_context->ostream() << "}"; |
13230 | } | |
13231 | ||
e440a328 | 13232 | // A general composite literal. This is lowered to a type specific |
13233 | // version. | |
13234 | ||
13235 | class Composite_literal_expression : public Parser_expression | |
13236 | { | |
13237 | public: | |
13238 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
62750cd5 | 13239 | Expression_list* vals, bool all_are_names, |
13240 | Location location) | |
e440a328 | 13241 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), |
62750cd5 | 13242 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys), |
13243 | all_are_names_(all_are_names) | |
e440a328 | 13244 | { } |
13245 | ||
13246 | protected: | |
13247 | int | |
13248 | do_traverse(Traverse* traverse); | |
13249 | ||
13250 | Expression* | |
ceeb4318 | 13251 | do_lower(Gogo*, Named_object*, Statement_inserter*, int); |
e440a328 | 13252 | |
13253 | Expression* | |
13254 | do_copy() | |
13255 | { | |
13256 | return new Composite_literal_expression(this->type_, this->depth_, | |
13257 | this->has_keys_, | |
13258 | (this->vals_ == NULL | |
13259 | ? NULL | |
13260 | : this->vals_->copy()), | |
62750cd5 | 13261 | this->all_are_names_, |
e440a328 | 13262 | this->location()); |
13263 | } | |
13264 | ||
d751bb78 | 13265 | void |
13266 | do_dump_expression(Ast_dump_context*) const; | |
13267 | ||
e440a328 | 13268 | private: |
13269 | Expression* | |
81c4b26b | 13270 | lower_struct(Gogo*, Type*); |
e440a328 | 13271 | |
13272 | Expression* | |
113ef6a5 | 13273 | lower_array(Type*); |
e440a328 | 13274 | |
13275 | Expression* | |
ffe743ca | 13276 | make_array(Type*, const std::vector<unsigned long>*, Expression_list*); |
e440a328 | 13277 | |
13278 | Expression* | |
ceeb4318 | 13279 | lower_map(Gogo*, Named_object*, Statement_inserter*, Type*); |
e440a328 | 13280 | |
13281 | // The type of the composite literal. | |
13282 | Type* type_; | |
13283 | // The depth within a list of composite literals within a composite | |
13284 | // literal, when the type is omitted. | |
13285 | int depth_; | |
13286 | // The values to put in the composite literal. | |
13287 | Expression_list* vals_; | |
13288 | // If this is true, then VALS_ is a list of pairs: a key and a | |
13289 | // value. In an array initializer, a missing key will be NULL. | |
13290 | bool has_keys_; | |
62750cd5 | 13291 | // If this is true, then HAS_KEYS_ is true, and every key is a |
13292 | // simple identifier. | |
13293 | bool all_are_names_; | |
e440a328 | 13294 | }; |
13295 | ||
13296 | // Traversal. | |
13297 | ||
13298 | int | |
13299 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
13300 | { | |
13301 | if (this->vals_ != NULL | |
13302 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
13303 | return TRAVERSE_EXIT; | |
13304 | return Type::traverse(this->type_, traverse); | |
13305 | } | |
13306 | ||
13307 | // Lower a generic composite literal into a specific version based on | |
13308 | // the type. | |
13309 | ||
13310 | Expression* | |
ceeb4318 | 13311 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, |
13312 | Statement_inserter* inserter, int) | |
e440a328 | 13313 | { |
13314 | Type* type = this->type_; | |
13315 | ||
13316 | for (int depth = this->depth_; depth > 0; --depth) | |
13317 | { | |
13318 | if (type->array_type() != NULL) | |
13319 | type = type->array_type()->element_type(); | |
13320 | else if (type->map_type() != NULL) | |
13321 | type = type->map_type()->val_type(); | |
13322 | else | |
13323 | { | |
5c13bd80 | 13324 | if (!type->is_error()) |
e440a328 | 13325 | error_at(this->location(), |
13326 | ("may only omit types within composite literals " | |
13327 | "of slice, array, or map type")); | |
13328 | return Expression::make_error(this->location()); | |
13329 | } | |
13330 | } | |
13331 | ||
e00772b3 | 13332 | Type *pt = type->points_to(); |
13333 | bool is_pointer = false; | |
13334 | if (pt != NULL) | |
13335 | { | |
13336 | is_pointer = true; | |
13337 | type = pt; | |
13338 | } | |
13339 | ||
13340 | Expression* ret; | |
5c13bd80 | 13341 | if (type->is_error()) |
e440a328 | 13342 | return Expression::make_error(this->location()); |
13343 | else if (type->struct_type() != NULL) | |
e00772b3 | 13344 | ret = this->lower_struct(gogo, type); |
e440a328 | 13345 | else if (type->array_type() != NULL) |
113ef6a5 | 13346 | ret = this->lower_array(type); |
e440a328 | 13347 | else if (type->map_type() != NULL) |
e00772b3 | 13348 | ret = this->lower_map(gogo, function, inserter, type); |
e440a328 | 13349 | else |
13350 | { | |
13351 | error_at(this->location(), | |
13352 | ("expected struct, slice, array, or map type " | |
13353 | "for composite literal")); | |
13354 | return Expression::make_error(this->location()); | |
13355 | } | |
e00772b3 | 13356 | |
13357 | if (is_pointer) | |
13358 | ret = Expression::make_heap_composite(ret, this->location()); | |
13359 | ||
13360 | return ret; | |
e440a328 | 13361 | } |
13362 | ||
13363 | // Lower a struct composite literal. | |
13364 | ||
13365 | Expression* | |
81c4b26b | 13366 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 13367 | { |
b13c66cd | 13368 | Location location = this->location(); |
e440a328 | 13369 | Struct_type* st = type->struct_type(); |
13370 | if (this->vals_ == NULL || !this->has_keys_) | |
07daa4e7 | 13371 | { |
e6013c28 | 13372 | if (this->vals_ != NULL |
13373 | && !this->vals_->empty() | |
13374 | && type->named_type() != NULL | |
13375 | && type->named_type()->named_object()->package() != NULL) | |
13376 | { | |
13377 | for (Struct_field_list::const_iterator pf = st->fields()->begin(); | |
13378 | pf != st->fields()->end(); | |
13379 | ++pf) | |
07daa4e7 | 13380 | { |
e6013c28 | 13381 | if (Gogo::is_hidden_name(pf->field_name())) |
07daa4e7 | 13382 | error_at(this->location(), |
e6013c28 | 13383 | "assignment of unexported field %qs in %qs literal", |
13384 | Gogo::message_name(pf->field_name()).c_str(), | |
13385 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13386 | } |
13387 | } | |
13388 | ||
13389 | return new Struct_construction_expression(type, this->vals_, location); | |
13390 | } | |
e440a328 | 13391 | |
13392 | size_t field_count = st->field_count(); | |
13393 | std::vector<Expression*> vals(field_count); | |
0c4f5a19 | 13394 | std::vector<int>* traverse_order = new(std::vector<int>); |
e440a328 | 13395 | Expression_list::const_iterator p = this->vals_->begin(); |
62750cd5 | 13396 | Expression* external_expr = NULL; |
13397 | const Named_object* external_no = NULL; | |
e440a328 | 13398 | while (p != this->vals_->end()) |
13399 | { | |
13400 | Expression* name_expr = *p; | |
13401 | ||
13402 | ++p; | |
c484d925 | 13403 | go_assert(p != this->vals_->end()); |
e440a328 | 13404 | Expression* val = *p; |
13405 | ||
13406 | ++p; | |
13407 | ||
13408 | if (name_expr == NULL) | |
13409 | { | |
13410 | error_at(val->location(), "mixture of field and value initializers"); | |
13411 | return Expression::make_error(location); | |
13412 | } | |
13413 | ||
13414 | bool bad_key = false; | |
13415 | std::string name; | |
81c4b26b | 13416 | const Named_object* no = NULL; |
e440a328 | 13417 | switch (name_expr->classification()) |
13418 | { | |
13419 | case EXPRESSION_UNKNOWN_REFERENCE: | |
13420 | name = name_expr->unknown_expression()->name(); | |
13421 | break; | |
13422 | ||
13423 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 13424 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 13425 | break; |
13426 | ||
13427 | case EXPRESSION_TYPE: | |
13428 | { | |
13429 | Type* t = name_expr->type(); | |
13430 | Named_type* nt = t->named_type(); | |
13431 | if (nt == NULL) | |
13432 | bad_key = true; | |
13433 | else | |
81c4b26b | 13434 | no = nt->named_object(); |
e440a328 | 13435 | } |
13436 | break; | |
13437 | ||
13438 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 13439 | no = name_expr->var_expression()->named_object(); |
e440a328 | 13440 | break; |
13441 | ||
13442 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 13443 | no = name_expr->func_expression()->named_object(); |
e440a328 | 13444 | break; |
13445 | ||
13446 | case EXPRESSION_UNARY: | |
13447 | // If there is a local variable around with the same name as | |
13448 | // the field, and this occurs in the closure, then the | |
13449 | // parser may turn the field reference into an indirection | |
13450 | // through the closure. FIXME: This is a mess. | |
13451 | { | |
13452 | bad_key = true; | |
13453 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
13454 | if (ue->op() == OPERATOR_MULT) | |
13455 | { | |
13456 | Field_reference_expression* fre = | |
13457 | ue->operand()->field_reference_expression(); | |
13458 | if (fre != NULL) | |
13459 | { | |
13460 | Struct_type* st = | |
13461 | fre->expr()->type()->deref()->struct_type(); | |
13462 | if (st != NULL) | |
13463 | { | |
13464 | const Struct_field* sf = st->field(fre->field_index()); | |
13465 | name = sf->field_name(); | |
2d29d278 | 13466 | |
13467 | // See below. FIXME. | |
13468 | if (!Gogo::is_hidden_name(name) | |
13469 | && name[0] >= 'a' | |
13470 | && name[0] <= 'z') | |
13471 | { | |
13472 | if (gogo->lookup_global(name.c_str()) != NULL) | |
13473 | name = gogo->pack_hidden_name(name, false); | |
13474 | } | |
13475 | ||
e440a328 | 13476 | char buf[20]; |
13477 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
13478 | size_t buflen = strlen(buf); | |
13479 | if (name.compare(name.length() - buflen, buflen, buf) | |
13480 | == 0) | |
13481 | { | |
13482 | name = name.substr(0, name.length() - buflen); | |
13483 | bad_key = false; | |
13484 | } | |
13485 | } | |
13486 | } | |
13487 | } | |
13488 | } | |
13489 | break; | |
13490 | ||
13491 | default: | |
13492 | bad_key = true; | |
13493 | break; | |
13494 | } | |
13495 | if (bad_key) | |
13496 | { | |
13497 | error_at(name_expr->location(), "expected struct field name"); | |
13498 | return Expression::make_error(location); | |
13499 | } | |
13500 | ||
81c4b26b | 13501 | if (no != NULL) |
13502 | { | |
62750cd5 | 13503 | if (no->package() != NULL && external_expr == NULL) |
13504 | { | |
13505 | external_expr = name_expr; | |
13506 | external_no = no; | |
13507 | } | |
13508 | ||
81c4b26b | 13509 | name = no->name(); |
13510 | ||
13511 | // A predefined name won't be packed. If it starts with a | |
13512 | // lower case letter we need to check for that case, because | |
2d29d278 | 13513 | // the field name will be packed. FIXME. |
81c4b26b | 13514 | if (!Gogo::is_hidden_name(name) |
13515 | && name[0] >= 'a' | |
13516 | && name[0] <= 'z') | |
13517 | { | |
13518 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
13519 | if (gno == no) | |
13520 | name = gogo->pack_hidden_name(name, false); | |
13521 | } | |
13522 | } | |
13523 | ||
e440a328 | 13524 | unsigned int index; |
13525 | const Struct_field* sf = st->find_local_field(name, &index); | |
13526 | if (sf == NULL) | |
13527 | { | |
13528 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
13529 | Gogo::message_name(name).c_str(), | |
13530 | (type->named_type() != NULL | |
13531 | ? type->named_type()->message_name().c_str() | |
13532 | : "unnamed struct")); | |
13533 | return Expression::make_error(location); | |
13534 | } | |
13535 | if (vals[index] != NULL) | |
13536 | { | |
13537 | error_at(name_expr->location(), | |
13538 | "duplicate value for field %qs in %qs", | |
13539 | Gogo::message_name(name).c_str(), | |
13540 | (type->named_type() != NULL | |
13541 | ? type->named_type()->message_name().c_str() | |
13542 | : "unnamed struct")); | |
13543 | return Expression::make_error(location); | |
13544 | } | |
13545 | ||
07daa4e7 | 13546 | if (type->named_type() != NULL |
13547 | && type->named_type()->named_object()->package() != NULL | |
13548 | && Gogo::is_hidden_name(sf->field_name())) | |
13549 | error_at(name_expr->location(), | |
13550 | "assignment of unexported field %qs in %qs literal", | |
13551 | Gogo::message_name(sf->field_name()).c_str(), | |
13552 | type->named_type()->message_name().c_str()); | |
07daa4e7 | 13553 | |
e440a328 | 13554 | vals[index] = val; |
0c4f5a19 | 13555 | traverse_order->push_back(index); |
e440a328 | 13556 | } |
13557 | ||
62750cd5 | 13558 | if (!this->all_are_names_) |
13559 | { | |
13560 | // This is a weird case like bug462 in the testsuite. | |
13561 | if (external_expr == NULL) | |
13562 | error_at(this->location(), "unknown field in %qs literal", | |
13563 | (type->named_type() != NULL | |
13564 | ? type->named_type()->message_name().c_str() | |
13565 | : "unnamed struct")); | |
13566 | else | |
13567 | error_at(external_expr->location(), "unknown field %qs in %qs", | |
13568 | external_no->message_name().c_str(), | |
13569 | (type->named_type() != NULL | |
13570 | ? type->named_type()->message_name().c_str() | |
13571 | : "unnamed struct")); | |
13572 | return Expression::make_error(location); | |
13573 | } | |
13574 | ||
e440a328 | 13575 | Expression_list* list = new Expression_list; |
13576 | list->reserve(field_count); | |
13577 | for (size_t i = 0; i < field_count; ++i) | |
13578 | list->push_back(vals[i]); | |
13579 | ||
0c4f5a19 | 13580 | Struct_construction_expression* ret = |
13581 | new Struct_construction_expression(type, list, location); | |
13582 | ret->set_traverse_order(traverse_order); | |
13583 | return ret; | |
e440a328 | 13584 | } |
13585 | ||
00773463 | 13586 | // Used to sort an index/value array. |
13587 | ||
13588 | class Index_value_compare | |
13589 | { | |
13590 | public: | |
13591 | bool | |
13592 | operator()(const std::pair<unsigned long, Expression*>& a, | |
13593 | const std::pair<unsigned long, Expression*>& b) | |
13594 | { return a.first < b.first; } | |
13595 | }; | |
13596 | ||
e440a328 | 13597 | // Lower an array composite literal. |
13598 | ||
13599 | Expression* | |
113ef6a5 | 13600 | Composite_literal_expression::lower_array(Type* type) |
e440a328 | 13601 | { |
b13c66cd | 13602 | Location location = this->location(); |
e440a328 | 13603 | if (this->vals_ == NULL || !this->has_keys_) |
ffe743ca | 13604 | return this->make_array(type, NULL, this->vals_); |
e440a328 | 13605 | |
ffe743ca | 13606 | std::vector<unsigned long>* indexes = new std::vector<unsigned long>; |
13607 | indexes->reserve(this->vals_->size()); | |
00773463 | 13608 | bool indexes_out_of_order = false; |
ffe743ca | 13609 | Expression_list* vals = new Expression_list(); |
13610 | vals->reserve(this->vals_->size()); | |
e440a328 | 13611 | unsigned long index = 0; |
13612 | Expression_list::const_iterator p = this->vals_->begin(); | |
13613 | while (p != this->vals_->end()) | |
13614 | { | |
13615 | Expression* index_expr = *p; | |
13616 | ||
13617 | ++p; | |
c484d925 | 13618 | go_assert(p != this->vals_->end()); |
e440a328 | 13619 | Expression* val = *p; |
13620 | ||
13621 | ++p; | |
13622 | ||
ffe743ca | 13623 | if (index_expr == NULL) |
13624 | { | |
13625 | if (!indexes->empty()) | |
13626 | indexes->push_back(index); | |
13627 | } | |
13628 | else | |
e440a328 | 13629 | { |
ffe743ca | 13630 | if (indexes->empty() && !vals->empty()) |
13631 | { | |
13632 | for (size_t i = 0; i < vals->size(); ++i) | |
13633 | indexes->push_back(i); | |
13634 | } | |
13635 | ||
0c77715b | 13636 | Numeric_constant nc; |
13637 | if (!index_expr->numeric_constant_value(&nc)) | |
e440a328 | 13638 | { |
e440a328 | 13639 | error_at(index_expr->location(), |
13640 | "index expression is not integer constant"); | |
13641 | return Expression::make_error(location); | |
13642 | } | |
6f6d9955 | 13643 | |
0c77715b | 13644 | switch (nc.to_unsigned_long(&index)) |
e440a328 | 13645 | { |
0c77715b | 13646 | case Numeric_constant::NC_UL_VALID: |
13647 | break; | |
13648 | case Numeric_constant::NC_UL_NOTINT: | |
13649 | error_at(index_expr->location(), | |
13650 | "index expression is not integer constant"); | |
13651 | return Expression::make_error(location); | |
13652 | case Numeric_constant::NC_UL_NEGATIVE: | |
e440a328 | 13653 | error_at(index_expr->location(), "index expression is negative"); |
13654 | return Expression::make_error(location); | |
0c77715b | 13655 | case Numeric_constant::NC_UL_BIG: |
e440a328 | 13656 | error_at(index_expr->location(), "index value overflow"); |
13657 | return Expression::make_error(location); | |
0c77715b | 13658 | default: |
13659 | go_unreachable(); | |
e440a328 | 13660 | } |
6f6d9955 | 13661 | |
13662 | Named_type* ntype = Type::lookup_integer_type("int"); | |
13663 | Integer_type* inttype = ntype->integer_type(); | |
0c77715b | 13664 | if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8) |
13665 | && index >> (inttype->bits() - 1) != 0) | |
6f6d9955 | 13666 | { |
6f6d9955 | 13667 | error_at(index_expr->location(), "index value overflow"); |
13668 | return Expression::make_error(location); | |
13669 | } | |
13670 | ||
ffe743ca | 13671 | if (std::find(indexes->begin(), indexes->end(), index) |
13672 | != indexes->end()) | |
e440a328 | 13673 | { |
ffe743ca | 13674 | error_at(index_expr->location(), "duplicate value for index %lu", |
e440a328 | 13675 | index); |
13676 | return Expression::make_error(location); | |
13677 | } | |
ffe743ca | 13678 | |
00773463 | 13679 | if (!indexes->empty() && index < indexes->back()) |
13680 | indexes_out_of_order = true; | |
13681 | ||
ffe743ca | 13682 | indexes->push_back(index); |
e440a328 | 13683 | } |
13684 | ||
ffe743ca | 13685 | vals->push_back(val); |
13686 | ||
e440a328 | 13687 | ++index; |
13688 | } | |
13689 | ||
ffe743ca | 13690 | if (indexes->empty()) |
13691 | { | |
13692 | delete indexes; | |
13693 | indexes = NULL; | |
13694 | } | |
e440a328 | 13695 | |
00773463 | 13696 | if (indexes_out_of_order) |
13697 | { | |
13698 | typedef std::vector<std::pair<unsigned long, Expression*> > V; | |
13699 | ||
13700 | V v; | |
13701 | v.reserve(indexes->size()); | |
13702 | std::vector<unsigned long>::const_iterator pi = indexes->begin(); | |
13703 | for (Expression_list::const_iterator pe = vals->begin(); | |
13704 | pe != vals->end(); | |
13705 | ++pe, ++pi) | |
13706 | v.push_back(std::make_pair(*pi, *pe)); | |
13707 | ||
13708 | std::sort(v.begin(), v.end(), Index_value_compare()); | |
13709 | ||
13710 | delete indexes; | |
13711 | delete vals; | |
13712 | indexes = new std::vector<unsigned long>(); | |
13713 | indexes->reserve(v.size()); | |
13714 | vals = new Expression_list(); | |
13715 | vals->reserve(v.size()); | |
13716 | ||
13717 | for (V::const_iterator p = v.begin(); p != v.end(); ++p) | |
13718 | { | |
13719 | indexes->push_back(p->first); | |
13720 | vals->push_back(p->second); | |
13721 | } | |
13722 | } | |
13723 | ||
ffe743ca | 13724 | return this->make_array(type, indexes, vals); |
e440a328 | 13725 | } |
13726 | ||
13727 | // Actually build the array composite literal. This handles | |
13728 | // [...]{...}. | |
13729 | ||
13730 | Expression* | |
ffe743ca | 13731 | Composite_literal_expression::make_array( |
13732 | Type* type, | |
13733 | const std::vector<unsigned long>* indexes, | |
13734 | Expression_list* vals) | |
e440a328 | 13735 | { |
b13c66cd | 13736 | Location location = this->location(); |
e440a328 | 13737 | Array_type* at = type->array_type(); |
ffe743ca | 13738 | |
e440a328 | 13739 | if (at->length() != NULL && at->length()->is_nil_expression()) |
13740 | { | |
ffe743ca | 13741 | size_t size; |
13742 | if (vals == NULL) | |
13743 | size = 0; | |
00773463 | 13744 | else if (indexes != NULL) |
13745 | size = indexes->back() + 1; | |
13746 | else | |
ffe743ca | 13747 | { |
13748 | size = vals->size(); | |
13749 | Integer_type* it = Type::lookup_integer_type("int")->integer_type(); | |
13750 | if (sizeof(size) <= static_cast<size_t>(it->bits() * 8) | |
13751 | && size >> (it->bits() - 1) != 0) | |
13752 | { | |
13753 | error_at(location, "too many elements in composite literal"); | |
13754 | return Expression::make_error(location); | |
13755 | } | |
13756 | } | |
ffe743ca | 13757 | |
e440a328 | 13758 | mpz_t vlen; |
13759 | mpz_init_set_ui(vlen, size); | |
13760 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
13761 | mpz_clear(vlen); | |
13762 | at = Type::make_array_type(at->element_type(), elen); | |
13763 | type = at; | |
13764 | } | |
ffe743ca | 13765 | else if (at->length() != NULL |
13766 | && !at->length()->is_error_expression() | |
13767 | && this->vals_ != NULL) | |
13768 | { | |
13769 | Numeric_constant nc; | |
13770 | unsigned long val; | |
13771 | if (at->length()->numeric_constant_value(&nc) | |
13772 | && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID) | |
13773 | { | |
13774 | if (indexes == NULL) | |
13775 | { | |
13776 | if (this->vals_->size() > val) | |
13777 | { | |
13778 | error_at(location, "too many elements in composite literal"); | |
13779 | return Expression::make_error(location); | |
13780 | } | |
13781 | } | |
13782 | else | |
13783 | { | |
00773463 | 13784 | unsigned long max = indexes->back(); |
ffe743ca | 13785 | if (max >= val) |
13786 | { | |
13787 | error_at(location, | |
13788 | ("some element keys in composite literal " | |
13789 | "are out of range")); | |
13790 | return Expression::make_error(location); | |
13791 | } | |
13792 | } | |
13793 | } | |
13794 | } | |
13795 | ||
e440a328 | 13796 | if (at->length() != NULL) |
ffe743ca | 13797 | return new Fixed_array_construction_expression(type, indexes, vals, |
13798 | location); | |
e440a328 | 13799 | else |
ffe743ca | 13800 | return new Open_array_construction_expression(type, indexes, vals, |
13801 | location); | |
e440a328 | 13802 | } |
13803 | ||
13804 | // Lower a map composite literal. | |
13805 | ||
13806 | Expression* | |
a287720d | 13807 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
ceeb4318 | 13808 | Statement_inserter* inserter, |
a287720d | 13809 | Type* type) |
e440a328 | 13810 | { |
b13c66cd | 13811 | Location location = this->location(); |
e440a328 | 13812 | if (this->vals_ != NULL) |
13813 | { | |
13814 | if (!this->has_keys_) | |
13815 | { | |
13816 | error_at(location, "map composite literal must have keys"); | |
13817 | return Expression::make_error(location); | |
13818 | } | |
13819 | ||
a287720d | 13820 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 13821 | p != this->vals_->end(); |
13822 | p += 2) | |
13823 | { | |
13824 | if (*p == NULL) | |
13825 | { | |
13826 | ++p; | |
13827 | error_at((*p)->location(), | |
13828 | "map composite literal must have keys for every value"); | |
13829 | return Expression::make_error(location); | |
13830 | } | |
a287720d | 13831 | // Make sure we have lowered the key; it may not have been |
13832 | // lowered in order to handle keys for struct composite | |
13833 | // literals. Lower it now to get the right error message. | |
13834 | if ((*p)->unknown_expression() != NULL) | |
13835 | { | |
13836 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
ceeb4318 | 13837 | gogo->lower_expression(function, inserter, &*p); |
c484d925 | 13838 | go_assert((*p)->is_error_expression()); |
a287720d | 13839 | return Expression::make_error(location); |
13840 | } | |
e440a328 | 13841 | } |
13842 | } | |
13843 | ||
13844 | return new Map_construction_expression(type, this->vals_, location); | |
13845 | } | |
13846 | ||
d751bb78 | 13847 | // Dump ast representation for a composite literal expression. |
13848 | ||
13849 | void | |
13850 | Composite_literal_expression::do_dump_expression( | |
13851 | Ast_dump_context* ast_dump_context) const | |
13852 | { | |
8b1c301d | 13853 | ast_dump_context->ostream() << "composite("; |
d751bb78 | 13854 | ast_dump_context->dump_type(this->type_); |
13855 | ast_dump_context->ostream() << ", {"; | |
8b1c301d | 13856 | ast_dump_context->dump_expression_list(this->vals_, this->has_keys_); |
d751bb78 | 13857 | ast_dump_context->ostream() << "})"; |
13858 | } | |
13859 | ||
e440a328 | 13860 | // Make a composite literal expression. |
13861 | ||
13862 | Expression* | |
13863 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
62750cd5 | 13864 | Expression_list* vals, bool all_are_names, |
b13c66cd | 13865 | Location location) |
e440a328 | 13866 | { |
13867 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
62750cd5 | 13868 | all_are_names, location); |
e440a328 | 13869 | } |
13870 | ||
13871 | // Return whether this expression is a composite literal. | |
13872 | ||
13873 | bool | |
13874 | Expression::is_composite_literal() const | |
13875 | { | |
13876 | switch (this->classification_) | |
13877 | { | |
13878 | case EXPRESSION_COMPOSITE_LITERAL: | |
13879 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13880 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13881 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
13882 | case EXPRESSION_MAP_CONSTRUCTION: | |
13883 | return true; | |
13884 | default: | |
13885 | return false; | |
13886 | } | |
13887 | } | |
13888 | ||
13889 | // Return whether this expression is a composite literal which is not | |
13890 | // constant. | |
13891 | ||
13892 | bool | |
13893 | Expression::is_nonconstant_composite_literal() const | |
13894 | { | |
13895 | switch (this->classification_) | |
13896 | { | |
13897 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
13898 | { | |
13899 | const Struct_construction_expression *psce = | |
13900 | static_cast<const Struct_construction_expression*>(this); | |
13901 | return !psce->is_constant_struct(); | |
13902 | } | |
13903 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
13904 | { | |
13905 | const Fixed_array_construction_expression *pace = | |
13906 | static_cast<const Fixed_array_construction_expression*>(this); | |
13907 | return !pace->is_constant_array(); | |
13908 | } | |
13909 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
13910 | { | |
13911 | const Open_array_construction_expression *pace = | |
13912 | static_cast<const Open_array_construction_expression*>(this); | |
13913 | return !pace->is_constant_array(); | |
13914 | } | |
13915 | case EXPRESSION_MAP_CONSTRUCTION: | |
13916 | return true; | |
13917 | default: | |
13918 | return false; | |
13919 | } | |
13920 | } | |
13921 | ||
13922 | // Return true if this is a reference to a local variable. | |
13923 | ||
13924 | bool | |
13925 | Expression::is_local_variable() const | |
13926 | { | |
13927 | const Var_expression* ve = this->var_expression(); | |
13928 | if (ve == NULL) | |
13929 | return false; | |
13930 | const Named_object* no = ve->named_object(); | |
13931 | return (no->is_result_variable() | |
13932 | || (no->is_variable() && !no->var_value()->is_global())); | |
13933 | } | |
13934 | ||
13935 | // Class Type_guard_expression. | |
13936 | ||
13937 | // Traversal. | |
13938 | ||
13939 | int | |
13940 | Type_guard_expression::do_traverse(Traverse* traverse) | |
13941 | { | |
13942 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
13943 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
13944 | return TRAVERSE_EXIT; | |
13945 | return TRAVERSE_CONTINUE; | |
13946 | } | |
13947 | ||
13948 | // Check types of a type guard expression. The expression must have | |
13949 | // an interface type, but the actual type conversion is checked at run | |
13950 | // time. | |
13951 | ||
13952 | void | |
13953 | Type_guard_expression::do_check_types(Gogo*) | |
13954 | { | |
e440a328 | 13955 | Type* expr_type = this->expr_->type(); |
7e9da23f | 13956 | if (expr_type->interface_type() == NULL) |
f725ade8 | 13957 | { |
5c13bd80 | 13958 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 13959 | this->report_error(_("type assertion only valid for interface types")); |
13960 | this->set_is_error(); | |
13961 | } | |
e440a328 | 13962 | else if (this->type_->interface_type() == NULL) |
13963 | { | |
13964 | std::string reason; | |
13965 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
13966 | &reason)) | |
13967 | { | |
5c13bd80 | 13968 | if (!this->type_->is_error()) |
e440a328 | 13969 | { |
f725ade8 | 13970 | if (reason.empty()) |
13971 | this->report_error(_("impossible type assertion: " | |
13972 | "type does not implement interface")); | |
13973 | else | |
13974 | error_at(this->location(), | |
13975 | ("impossible type assertion: " | |
13976 | "type does not implement interface (%s)"), | |
13977 | reason.c_str()); | |
e440a328 | 13978 | } |
f725ade8 | 13979 | this->set_is_error(); |
e440a328 | 13980 | } |
13981 | } | |
13982 | } | |
13983 | ||
13984 | // Return a tree for a type guard expression. | |
13985 | ||
13986 | tree | |
13987 | Type_guard_expression::do_get_tree(Translate_context* context) | |
13988 | { | |
e440a328 | 13989 | tree expr_tree = this->expr_->get_tree(context); |
13990 | if (expr_tree == error_mark_node) | |
13991 | return error_mark_node; | |
7e9da23f | 13992 | if (this->type_->interface_type() != NULL) |
e440a328 | 13993 | return Expression::convert_interface_to_interface(context, this->type_, |
13994 | this->expr_->type(), | |
13995 | expr_tree, true, | |
13996 | this->location()); | |
13997 | else | |
13998 | return Expression::convert_for_assignment(context, this->type_, | |
13999 | this->expr_->type(), expr_tree, | |
14000 | this->location()); | |
14001 | } | |
14002 | ||
d751bb78 | 14003 | // Dump ast representation for a type guard expression. |
14004 | ||
14005 | void | |
14006 | Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context) | |
14007 | const | |
14008 | { | |
14009 | this->expr_->dump_expression(ast_dump_context); | |
14010 | ast_dump_context->ostream() << "."; | |
14011 | ast_dump_context->dump_type(this->type_); | |
14012 | } | |
14013 | ||
e440a328 | 14014 | // Make a type guard expression. |
14015 | ||
14016 | Expression* | |
14017 | Expression::make_type_guard(Expression* expr, Type* type, | |
b13c66cd | 14018 | Location location) |
e440a328 | 14019 | { |
14020 | return new Type_guard_expression(expr, type, location); | |
14021 | } | |
14022 | ||
14023 | // Class Heap_composite_expression. | |
14024 | ||
14025 | // When you take the address of a composite literal, it is allocated | |
14026 | // on the heap. This class implements that. | |
14027 | ||
14028 | class Heap_composite_expression : public Expression | |
14029 | { | |
14030 | public: | |
b13c66cd | 14031 | Heap_composite_expression(Expression* expr, Location location) |
e440a328 | 14032 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), |
14033 | expr_(expr) | |
14034 | { } | |
14035 | ||
14036 | protected: | |
14037 | int | |
14038 | do_traverse(Traverse* traverse) | |
14039 | { return Expression::traverse(&this->expr_, traverse); } | |
14040 | ||
14041 | Type* | |
14042 | do_type() | |
14043 | { return Type::make_pointer_type(this->expr_->type()); } | |
14044 | ||
14045 | void | |
14046 | do_determine_type(const Type_context*) | |
14047 | { this->expr_->determine_type_no_context(); } | |
14048 | ||
14049 | Expression* | |
14050 | do_copy() | |
14051 | { | |
14052 | return Expression::make_heap_composite(this->expr_->copy(), | |
14053 | this->location()); | |
14054 | } | |
14055 | ||
14056 | tree | |
14057 | do_get_tree(Translate_context*); | |
14058 | ||
14059 | // We only export global objects, and the parser does not generate | |
14060 | // this in global scope. | |
14061 | void | |
14062 | do_export(Export*) const | |
c3e6f413 | 14063 | { go_unreachable(); } |
e440a328 | 14064 | |
d751bb78 | 14065 | void |
14066 | do_dump_expression(Ast_dump_context*) const; | |
14067 | ||
e440a328 | 14068 | private: |
14069 | // The composite literal which is being put on the heap. | |
14070 | Expression* expr_; | |
14071 | }; | |
14072 | ||
14073 | // Return a tree which allocates a composite literal on the heap. | |
14074 | ||
14075 | tree | |
14076 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
14077 | { | |
14078 | tree expr_tree = this->expr_->get_tree(context); | |
6d3ed74c | 14079 | if (expr_tree == error_mark_node || TREE_TYPE(expr_tree) == error_mark_node) |
e440a328 | 14080 | return error_mark_node; |
14081 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
c484d925 | 14082 | go_assert(TREE_CODE(expr_size) == INTEGER_CST); |
e440a328 | 14083 | tree space = context->gogo()->allocate_memory(this->expr_->type(), |
14084 | expr_size, this->location()); | |
14085 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
14086 | space = save_expr(space); | |
b13c66cd | 14087 | tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(), |
14088 | space); | |
e440a328 | 14089 | TREE_THIS_NOTRAP(ref) = 1; |
14090 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
14091 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
14092 | space); | |
b13c66cd | 14093 | SET_EXPR_LOCATION(ret, this->location().gcc_location()); |
e440a328 | 14094 | return ret; |
14095 | } | |
14096 | ||
d751bb78 | 14097 | // Dump ast representation for a heap composite expression. |
14098 | ||
14099 | void | |
14100 | Heap_composite_expression::do_dump_expression( | |
14101 | Ast_dump_context* ast_dump_context) const | |
14102 | { | |
14103 | ast_dump_context->ostream() << "&("; | |
14104 | ast_dump_context->dump_expression(this->expr_); | |
14105 | ast_dump_context->ostream() << ")"; | |
14106 | } | |
14107 | ||
e440a328 | 14108 | // Allocate a composite literal on the heap. |
14109 | ||
14110 | Expression* | |
b13c66cd | 14111 | Expression::make_heap_composite(Expression* expr, Location location) |
e440a328 | 14112 | { |
14113 | return new Heap_composite_expression(expr, location); | |
14114 | } | |
14115 | ||
14116 | // Class Receive_expression. | |
14117 | ||
14118 | // Return the type of a receive expression. | |
14119 | ||
14120 | Type* | |
14121 | Receive_expression::do_type() | |
14122 | { | |
14123 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
14124 | if (channel_type == NULL) | |
14125 | return Type::make_error_type(); | |
14126 | return channel_type->element_type(); | |
14127 | } | |
14128 | ||
14129 | // Check types for a receive expression. | |
14130 | ||
14131 | void | |
14132 | Receive_expression::do_check_types(Gogo*) | |
14133 | { | |
14134 | Type* type = this->channel_->type(); | |
5c13bd80 | 14135 | if (type->is_error()) |
e440a328 | 14136 | { |
14137 | this->set_is_error(); | |
14138 | return; | |
14139 | } | |
14140 | if (type->channel_type() == NULL) | |
14141 | { | |
14142 | this->report_error(_("expected channel")); | |
14143 | return; | |
14144 | } | |
14145 | if (!type->channel_type()->may_receive()) | |
14146 | { | |
14147 | this->report_error(_("invalid receive on send-only channel")); | |
14148 | return; | |
14149 | } | |
14150 | } | |
14151 | ||
14152 | // Get a tree for a receive expression. | |
14153 | ||
14154 | tree | |
14155 | Receive_expression::do_get_tree(Translate_context* context) | |
14156 | { | |
f24f10bb | 14157 | Location loc = this->location(); |
14158 | ||
e440a328 | 14159 | Channel_type* channel_type = this->channel_->type()->channel_type(); |
5b8368f4 | 14160 | if (channel_type == NULL) |
14161 | { | |
c484d925 | 14162 | go_assert(this->channel_->type()->is_error()); |
5b8368f4 | 14163 | return error_mark_node; |
14164 | } | |
f24f10bb | 14165 | |
14166 | Expression* td = Expression::make_type_descriptor(channel_type, loc); | |
14167 | tree td_tree = td->get_tree(context); | |
14168 | ||
e440a328 | 14169 | Type* element_type = channel_type->element_type(); |
9f0e0513 | 14170 | Btype* element_type_btype = element_type->get_backend(context->gogo()); |
14171 | tree element_type_tree = type_to_tree(element_type_btype); | |
e440a328 | 14172 | |
14173 | tree channel = this->channel_->get_tree(context); | |
14174 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
14175 | return error_mark_node; | |
14176 | ||
f24f10bb | 14177 | return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc); |
e440a328 | 14178 | } |
14179 | ||
d751bb78 | 14180 | // Dump ast representation for a receive expression. |
14181 | ||
14182 | void | |
14183 | Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14184 | { | |
14185 | ast_dump_context->ostream() << " <- " ; | |
14186 | ast_dump_context->dump_expression(channel_); | |
14187 | } | |
14188 | ||
e440a328 | 14189 | // Make a receive expression. |
14190 | ||
14191 | Receive_expression* | |
b13c66cd | 14192 | Expression::make_receive(Expression* channel, Location location) |
e440a328 | 14193 | { |
14194 | return new Receive_expression(channel, location); | |
14195 | } | |
14196 | ||
e440a328 | 14197 | // An expression which evaluates to a pointer to the type descriptor |
14198 | // of a type. | |
14199 | ||
14200 | class Type_descriptor_expression : public Expression | |
14201 | { | |
14202 | public: | |
b13c66cd | 14203 | Type_descriptor_expression(Type* type, Location location) |
e440a328 | 14204 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), |
14205 | type_(type) | |
14206 | { } | |
14207 | ||
14208 | protected: | |
14209 | Type* | |
14210 | do_type() | |
14211 | { return Type::make_type_descriptor_ptr_type(); } | |
14212 | ||
14213 | void | |
14214 | do_determine_type(const Type_context*) | |
14215 | { } | |
14216 | ||
14217 | Expression* | |
14218 | do_copy() | |
14219 | { return this; } | |
14220 | ||
14221 | tree | |
14222 | do_get_tree(Translate_context* context) | |
a1d23b41 | 14223 | { |
14224 | return this->type_->type_descriptor_pointer(context->gogo(), | |
14225 | this->location()); | |
14226 | } | |
e440a328 | 14227 | |
d751bb78 | 14228 | void |
14229 | do_dump_expression(Ast_dump_context*) const; | |
14230 | ||
e440a328 | 14231 | private: |
14232 | // The type for which this is the descriptor. | |
14233 | Type* type_; | |
14234 | }; | |
14235 | ||
d751bb78 | 14236 | // Dump ast representation for a type descriptor expression. |
14237 | ||
14238 | void | |
14239 | Type_descriptor_expression::do_dump_expression( | |
14240 | Ast_dump_context* ast_dump_context) const | |
14241 | { | |
14242 | ast_dump_context->dump_type(this->type_); | |
14243 | } | |
14244 | ||
e440a328 | 14245 | // Make a type descriptor expression. |
14246 | ||
14247 | Expression* | |
b13c66cd | 14248 | Expression::make_type_descriptor(Type* type, Location location) |
e440a328 | 14249 | { |
14250 | return new Type_descriptor_expression(type, location); | |
14251 | } | |
14252 | ||
14253 | // An expression which evaluates to some characteristic of a type. | |
14254 | // This is only used to initialize fields of a type descriptor. Using | |
14255 | // a new expression class is slightly inefficient but gives us a good | |
14256 | // separation between the frontend and the middle-end with regard to | |
14257 | // how types are laid out. | |
14258 | ||
14259 | class Type_info_expression : public Expression | |
14260 | { | |
14261 | public: | |
14262 | Type_info_expression(Type* type, Type_info type_info) | |
b13c66cd | 14263 | : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()), |
e440a328 | 14264 | type_(type), type_info_(type_info) |
14265 | { } | |
14266 | ||
14267 | protected: | |
14268 | Type* | |
14269 | do_type(); | |
14270 | ||
14271 | void | |
14272 | do_determine_type(const Type_context*) | |
14273 | { } | |
14274 | ||
14275 | Expression* | |
14276 | do_copy() | |
14277 | { return this; } | |
14278 | ||
14279 | tree | |
14280 | do_get_tree(Translate_context* context); | |
14281 | ||
d751bb78 | 14282 | void |
14283 | do_dump_expression(Ast_dump_context*) const; | |
14284 | ||
e440a328 | 14285 | private: |
14286 | // The type for which we are getting information. | |
14287 | Type* type_; | |
14288 | // What information we want. | |
14289 | Type_info type_info_; | |
14290 | }; | |
14291 | ||
14292 | // The type is chosen to match what the type descriptor struct | |
14293 | // expects. | |
14294 | ||
14295 | Type* | |
14296 | Type_info_expression::do_type() | |
14297 | { | |
14298 | switch (this->type_info_) | |
14299 | { | |
14300 | case TYPE_INFO_SIZE: | |
14301 | return Type::lookup_integer_type("uintptr"); | |
14302 | case TYPE_INFO_ALIGNMENT: | |
14303 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14304 | return Type::lookup_integer_type("uint8"); | |
14305 | default: | |
c3e6f413 | 14306 | go_unreachable(); |
e440a328 | 14307 | } |
14308 | } | |
14309 | ||
14310 | // Return type information in GENERIC. | |
14311 | ||
14312 | tree | |
14313 | Type_info_expression::do_get_tree(Translate_context* context) | |
14314 | { | |
927a01eb | 14315 | Btype* btype = this->type_->get_backend(context->gogo()); |
14316 | Gogo* gogo = context->gogo(); | |
14317 | size_t val; | |
14318 | switch (this->type_info_) | |
e440a328 | 14319 | { |
927a01eb | 14320 | case TYPE_INFO_SIZE: |
14321 | val = gogo->backend()->type_size(btype); | |
14322 | break; | |
14323 | case TYPE_INFO_ALIGNMENT: | |
14324 | val = gogo->backend()->type_alignment(btype); | |
14325 | break; | |
14326 | case TYPE_INFO_FIELD_ALIGNMENT: | |
14327 | val = gogo->backend()->type_field_alignment(btype); | |
14328 | break; | |
14329 | default: | |
14330 | go_unreachable(); | |
e440a328 | 14331 | } |
927a01eb | 14332 | tree val_type_tree = type_to_tree(this->type()->get_backend(gogo)); |
14333 | go_assert(val_type_tree != error_mark_node); | |
14334 | return build_int_cstu(val_type_tree, val); | |
e440a328 | 14335 | } |
14336 | ||
d751bb78 | 14337 | // Dump ast representation for a type info expression. |
14338 | ||
14339 | void | |
14340 | Type_info_expression::do_dump_expression( | |
14341 | Ast_dump_context* ast_dump_context) const | |
14342 | { | |
14343 | ast_dump_context->ostream() << "typeinfo("; | |
14344 | ast_dump_context->dump_type(this->type_); | |
14345 | ast_dump_context->ostream() << ","; | |
14346 | ast_dump_context->ostream() << | |
14347 | (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment" | |
14348 | : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment" | |
14349 | : this->type_info_ == TYPE_INFO_SIZE ? "size " | |
14350 | : "unknown"); | |
14351 | ast_dump_context->ostream() << ")"; | |
14352 | } | |
14353 | ||
e440a328 | 14354 | // Make a type info expression. |
14355 | ||
14356 | Expression* | |
14357 | Expression::make_type_info(Type* type, Type_info type_info) | |
14358 | { | |
14359 | return new Type_info_expression(type, type_info); | |
14360 | } | |
14361 | ||
14362 | // An expression which evaluates to the offset of a field within a | |
14363 | // struct. This, like Type_info_expression, q.v., is only used to | |
14364 | // initialize fields of a type descriptor. | |
14365 | ||
14366 | class Struct_field_offset_expression : public Expression | |
14367 | { | |
14368 | public: | |
14369 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
b13c66cd | 14370 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, |
14371 | Linemap::predeclared_location()), | |
e440a328 | 14372 | type_(type), field_(field) |
14373 | { } | |
14374 | ||
14375 | protected: | |
14376 | Type* | |
14377 | do_type() | |
14378 | { return Type::lookup_integer_type("uintptr"); } | |
14379 | ||
14380 | void | |
14381 | do_determine_type(const Type_context*) | |
14382 | { } | |
14383 | ||
14384 | Expression* | |
14385 | do_copy() | |
14386 | { return this; } | |
14387 | ||
14388 | tree | |
14389 | do_get_tree(Translate_context* context); | |
14390 | ||
d751bb78 | 14391 | void |
14392 | do_dump_expression(Ast_dump_context*) const; | |
14393 | ||
e440a328 | 14394 | private: |
14395 | // The type of the struct. | |
14396 | Struct_type* type_; | |
14397 | // The field. | |
14398 | const Struct_field* field_; | |
14399 | }; | |
14400 | ||
14401 | // Return a struct field offset in GENERIC. | |
14402 | ||
14403 | tree | |
14404 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
14405 | { | |
9f0e0513 | 14406 | tree type_tree = type_to_tree(this->type_->get_backend(context->gogo())); |
e440a328 | 14407 | if (type_tree == error_mark_node) |
14408 | return error_mark_node; | |
14409 | ||
9f0e0513 | 14410 | tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo())); |
c484d925 | 14411 | go_assert(val_type_tree != error_mark_node); |
e440a328 | 14412 | |
14413 | const Struct_field_list* fields = this->type_->fields(); | |
14414 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
14415 | Struct_field_list::const_iterator p; | |
14416 | for (p = fields->begin(); | |
14417 | p != fields->end(); | |
14418 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
14419 | { | |
c484d925 | 14420 | go_assert(struct_field_tree != NULL_TREE); |
e440a328 | 14421 | if (&*p == this->field_) |
14422 | break; | |
14423 | } | |
c484d925 | 14424 | go_assert(&*p == this->field_); |
e440a328 | 14425 | |
14426 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
14427 | byte_position(struct_field_tree)); | |
14428 | } | |
14429 | ||
d751bb78 | 14430 | // Dump ast representation for a struct field offset expression. |
14431 | ||
14432 | void | |
14433 | Struct_field_offset_expression::do_dump_expression( | |
14434 | Ast_dump_context* ast_dump_context) const | |
14435 | { | |
14436 | ast_dump_context->ostream() << "unsafe.Offsetof("; | |
2d29d278 | 14437 | ast_dump_context->dump_type(this->type_); |
14438 | ast_dump_context->ostream() << '.'; | |
14439 | ast_dump_context->ostream() << | |
14440 | Gogo::message_name(this->field_->field_name()); | |
d751bb78 | 14441 | ast_dump_context->ostream() << ")"; |
14442 | } | |
14443 | ||
e440a328 | 14444 | // Make an expression for a struct field offset. |
14445 | ||
14446 | Expression* | |
14447 | Expression::make_struct_field_offset(Struct_type* type, | |
14448 | const Struct_field* field) | |
14449 | { | |
14450 | return new Struct_field_offset_expression(type, field); | |
14451 | } | |
14452 | ||
a9182619 | 14453 | // An expression which evaluates to a pointer to the map descriptor of |
14454 | // a map type. | |
14455 | ||
14456 | class Map_descriptor_expression : public Expression | |
14457 | { | |
14458 | public: | |
b13c66cd | 14459 | Map_descriptor_expression(Map_type* type, Location location) |
a9182619 | 14460 | : Expression(EXPRESSION_MAP_DESCRIPTOR, location), |
14461 | type_(type) | |
14462 | { } | |
14463 | ||
14464 | protected: | |
14465 | Type* | |
14466 | do_type() | |
14467 | { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); } | |
14468 | ||
14469 | void | |
14470 | do_determine_type(const Type_context*) | |
14471 | { } | |
14472 | ||
14473 | Expression* | |
14474 | do_copy() | |
14475 | { return this; } | |
14476 | ||
14477 | tree | |
14478 | do_get_tree(Translate_context* context) | |
14479 | { | |
14480 | return this->type_->map_descriptor_pointer(context->gogo(), | |
14481 | this->location()); | |
14482 | } | |
14483 | ||
d751bb78 | 14484 | void |
14485 | do_dump_expression(Ast_dump_context*) const; | |
14486 | ||
a9182619 | 14487 | private: |
14488 | // The type for which this is the descriptor. | |
14489 | Map_type* type_; | |
14490 | }; | |
14491 | ||
d751bb78 | 14492 | // Dump ast representation for a map descriptor expression. |
14493 | ||
14494 | void | |
14495 | Map_descriptor_expression::do_dump_expression( | |
14496 | Ast_dump_context* ast_dump_context) const | |
14497 | { | |
14498 | ast_dump_context->ostream() << "map_descriptor("; | |
14499 | ast_dump_context->dump_type(this->type_); | |
14500 | ast_dump_context->ostream() << ")"; | |
14501 | } | |
14502 | ||
a9182619 | 14503 | // Make a map descriptor expression. |
14504 | ||
14505 | Expression* | |
b13c66cd | 14506 | Expression::make_map_descriptor(Map_type* type, Location location) |
a9182619 | 14507 | { |
14508 | return new Map_descriptor_expression(type, location); | |
14509 | } | |
14510 | ||
e440a328 | 14511 | // An expression which evaluates to the address of an unnamed label. |
14512 | ||
14513 | class Label_addr_expression : public Expression | |
14514 | { | |
14515 | public: | |
b13c66cd | 14516 | Label_addr_expression(Label* label, Location location) |
e440a328 | 14517 | : Expression(EXPRESSION_LABEL_ADDR, location), |
14518 | label_(label) | |
14519 | { } | |
14520 | ||
14521 | protected: | |
14522 | Type* | |
14523 | do_type() | |
14524 | { return Type::make_pointer_type(Type::make_void_type()); } | |
14525 | ||
14526 | void | |
14527 | do_determine_type(const Type_context*) | |
14528 | { } | |
14529 | ||
14530 | Expression* | |
14531 | do_copy() | |
14532 | { return new Label_addr_expression(this->label_, this->location()); } | |
14533 | ||
14534 | tree | |
6e193e6f | 14535 | do_get_tree(Translate_context* context) |
14536 | { | |
e8816003 | 14537 | return expr_to_tree(this->label_->get_addr(context, this->location())); |
6e193e6f | 14538 | } |
e440a328 | 14539 | |
d751bb78 | 14540 | void |
14541 | do_dump_expression(Ast_dump_context* ast_dump_context) const | |
14542 | { ast_dump_context->ostream() << this->label_->name(); } | |
14543 | ||
e440a328 | 14544 | private: |
14545 | // The label whose address we are taking. | |
14546 | Label* label_; | |
14547 | }; | |
14548 | ||
14549 | // Make an expression for the address of an unnamed label. | |
14550 | ||
14551 | Expression* | |
b13c66cd | 14552 | Expression::make_label_addr(Label* label, Location location) |
e440a328 | 14553 | { |
14554 | return new Label_addr_expression(label, location); | |
14555 | } | |
14556 | ||
14557 | // Import an expression. This comes at the end in order to see the | |
14558 | // various class definitions. | |
14559 | ||
14560 | Expression* | |
14561 | Expression::import_expression(Import* imp) | |
14562 | { | |
14563 | int c = imp->peek_char(); | |
14564 | if (imp->match_c_string("- ") | |
14565 | || imp->match_c_string("! ") | |
14566 | || imp->match_c_string("^ ")) | |
14567 | return Unary_expression::do_import(imp); | |
14568 | else if (c == '(') | |
14569 | return Binary_expression::do_import(imp); | |
14570 | else if (imp->match_c_string("true") | |
14571 | || imp->match_c_string("false")) | |
14572 | return Boolean_expression::do_import(imp); | |
14573 | else if (c == '"') | |
14574 | return String_expression::do_import(imp); | |
14575 | else if (c == '-' || (c >= '0' && c <= '9')) | |
14576 | { | |
14577 | // This handles integers, floats and complex constants. | |
14578 | return Integer_expression::do_import(imp); | |
14579 | } | |
14580 | else if (imp->match_c_string("nil")) | |
14581 | return Nil_expression::do_import(imp); | |
14582 | else if (imp->match_c_string("convert")) | |
14583 | return Type_conversion_expression::do_import(imp); | |
14584 | else | |
14585 | { | |
14586 | error_at(imp->location(), "import error: expected expression"); | |
14587 | return Expression::make_error(imp->location()); | |
14588 | } | |
14589 | } | |
14590 | ||
14591 | // Class Expression_list. | |
14592 | ||
14593 | // Traverse the list. | |
14594 | ||
14595 | int | |
14596 | Expression_list::traverse(Traverse* traverse) | |
14597 | { | |
14598 | for (Expression_list::iterator p = this->begin(); | |
14599 | p != this->end(); | |
14600 | ++p) | |
14601 | { | |
14602 | if (*p != NULL) | |
14603 | { | |
14604 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
14605 | return TRAVERSE_EXIT; | |
14606 | } | |
14607 | } | |
14608 | return TRAVERSE_CONTINUE; | |
14609 | } | |
14610 | ||
14611 | // Copy the list. | |
14612 | ||
14613 | Expression_list* | |
14614 | Expression_list::copy() | |
14615 | { | |
14616 | Expression_list* ret = new Expression_list(); | |
14617 | for (Expression_list::iterator p = this->begin(); | |
14618 | p != this->end(); | |
14619 | ++p) | |
14620 | { | |
14621 | if (*p == NULL) | |
14622 | ret->push_back(NULL); | |
14623 | else | |
14624 | ret->push_back((*p)->copy()); | |
14625 | } | |
14626 | return ret; | |
14627 | } | |
14628 | ||
14629 | // Return whether an expression list has an error expression. | |
14630 | ||
14631 | bool | |
14632 | Expression_list::contains_error() const | |
14633 | { | |
14634 | for (Expression_list::const_iterator p = this->begin(); | |
14635 | p != this->end(); | |
14636 | ++p) | |
14637 | if (*p != NULL && (*p)->is_error_expression()) | |
14638 | return true; | |
14639 | return false; | |
14640 | } | |
0c77715b | 14641 | |
14642 | // Class Numeric_constant. | |
14643 | ||
14644 | // Destructor. | |
14645 | ||
14646 | Numeric_constant::~Numeric_constant() | |
14647 | { | |
14648 | this->clear(); | |
14649 | } | |
14650 | ||
14651 | // Copy constructor. | |
14652 | ||
14653 | Numeric_constant::Numeric_constant(const Numeric_constant& a) | |
14654 | : classification_(a.classification_), type_(a.type_) | |
14655 | { | |
14656 | switch (a.classification_) | |
14657 | { | |
14658 | case NC_INVALID: | |
14659 | break; | |
14660 | case NC_INT: | |
14661 | case NC_RUNE: | |
14662 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
14663 | break; | |
14664 | case NC_FLOAT: | |
14665 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
14666 | break; | |
14667 | case NC_COMPLEX: | |
14668 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
14669 | GMP_RNDN); | |
14670 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
14671 | GMP_RNDN); | |
14672 | break; | |
14673 | default: | |
14674 | go_unreachable(); | |
14675 | } | |
14676 | } | |
14677 | ||
14678 | // Assignment operator. | |
14679 | ||
14680 | Numeric_constant& | |
14681 | Numeric_constant::operator=(const Numeric_constant& a) | |
14682 | { | |
14683 | this->clear(); | |
14684 | this->classification_ = a.classification_; | |
14685 | this->type_ = a.type_; | |
14686 | switch (a.classification_) | |
14687 | { | |
14688 | case NC_INVALID: | |
14689 | break; | |
14690 | case NC_INT: | |
14691 | case NC_RUNE: | |
14692 | mpz_init_set(this->u_.int_val, a.u_.int_val); | |
14693 | break; | |
14694 | case NC_FLOAT: | |
14695 | mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN); | |
14696 | break; | |
14697 | case NC_COMPLEX: | |
14698 | mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real, | |
14699 | GMP_RNDN); | |
14700 | mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag, | |
14701 | GMP_RNDN); | |
14702 | break; | |
14703 | default: | |
14704 | go_unreachable(); | |
14705 | } | |
14706 | return *this; | |
14707 | } | |
14708 | ||
14709 | // Clear the contents. | |
14710 | ||
14711 | void | |
14712 | Numeric_constant::clear() | |
14713 | { | |
14714 | switch (this->classification_) | |
14715 | { | |
14716 | case NC_INVALID: | |
14717 | break; | |
14718 | case NC_INT: | |
14719 | case NC_RUNE: | |
14720 | mpz_clear(this->u_.int_val); | |
14721 | break; | |
14722 | case NC_FLOAT: | |
14723 | mpfr_clear(this->u_.float_val); | |
14724 | break; | |
14725 | case NC_COMPLEX: | |
14726 | mpfr_clear(this->u_.complex_val.real); | |
14727 | mpfr_clear(this->u_.complex_val.imag); | |
14728 | break; | |
14729 | default: | |
14730 | go_unreachable(); | |
14731 | } | |
14732 | this->classification_ = NC_INVALID; | |
14733 | } | |
14734 | ||
14735 | // Set to an unsigned long value. | |
14736 | ||
14737 | void | |
14738 | Numeric_constant::set_unsigned_long(Type* type, unsigned long val) | |
14739 | { | |
14740 | this->clear(); | |
14741 | this->classification_ = NC_INT; | |
14742 | this->type_ = type; | |
14743 | mpz_init_set_ui(this->u_.int_val, val); | |
14744 | } | |
14745 | ||
14746 | // Set to an integer value. | |
14747 | ||
14748 | void | |
14749 | Numeric_constant::set_int(Type* type, const mpz_t val) | |
14750 | { | |
14751 | this->clear(); | |
14752 | this->classification_ = NC_INT; | |
14753 | this->type_ = type; | |
14754 | mpz_init_set(this->u_.int_val, val); | |
14755 | } | |
14756 | ||
14757 | // Set to a rune value. | |
14758 | ||
14759 | void | |
14760 | Numeric_constant::set_rune(Type* type, const mpz_t val) | |
14761 | { | |
14762 | this->clear(); | |
14763 | this->classification_ = NC_RUNE; | |
14764 | this->type_ = type; | |
14765 | mpz_init_set(this->u_.int_val, val); | |
14766 | } | |
14767 | ||
14768 | // Set to a floating point value. | |
14769 | ||
14770 | void | |
14771 | Numeric_constant::set_float(Type* type, const mpfr_t val) | |
14772 | { | |
14773 | this->clear(); | |
14774 | this->classification_ = NC_FLOAT; | |
14775 | this->type_ = type; | |
833b523c | 14776 | // Numeric constants do not have negative zero values, so remove |
14777 | // them here. They also don't have infinity or NaN values, but we | |
14778 | // should never see them here. | |
14779 | if (mpfr_zero_p(val)) | |
14780 | mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN); | |
14781 | else | |
14782 | mpfr_init_set(this->u_.float_val, val, GMP_RNDN); | |
0c77715b | 14783 | } |
14784 | ||
14785 | // Set to a complex value. | |
14786 | ||
14787 | void | |
14788 | Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag) | |
14789 | { | |
14790 | this->clear(); | |
14791 | this->classification_ = NC_COMPLEX; | |
14792 | this->type_ = type; | |
14793 | mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN); | |
14794 | mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN); | |
14795 | } | |
14796 | ||
14797 | // Get an int value. | |
14798 | ||
14799 | void | |
14800 | Numeric_constant::get_int(mpz_t* val) const | |
14801 | { | |
14802 | go_assert(this->is_int()); | |
14803 | mpz_init_set(*val, this->u_.int_val); | |
14804 | } | |
14805 | ||
14806 | // Get a rune value. | |
14807 | ||
14808 | void | |
14809 | Numeric_constant::get_rune(mpz_t* val) const | |
14810 | { | |
14811 | go_assert(this->is_rune()); | |
14812 | mpz_init_set(*val, this->u_.int_val); | |
14813 | } | |
14814 | ||
14815 | // Get a floating point value. | |
14816 | ||
14817 | void | |
14818 | Numeric_constant::get_float(mpfr_t* val) const | |
14819 | { | |
14820 | go_assert(this->is_float()); | |
14821 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
14822 | } | |
14823 | ||
14824 | // Get a complex value. | |
14825 | ||
14826 | void | |
14827 | Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const | |
14828 | { | |
14829 | go_assert(this->is_complex()); | |
14830 | mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN); | |
14831 | mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN); | |
14832 | } | |
14833 | ||
14834 | // Express value as unsigned long if possible. | |
14835 | ||
14836 | Numeric_constant::To_unsigned_long | |
14837 | Numeric_constant::to_unsigned_long(unsigned long* val) const | |
14838 | { | |
14839 | switch (this->classification_) | |
14840 | { | |
14841 | case NC_INT: | |
14842 | case NC_RUNE: | |
14843 | return this->mpz_to_unsigned_long(this->u_.int_val, val); | |
14844 | case NC_FLOAT: | |
14845 | return this->mpfr_to_unsigned_long(this->u_.float_val, val); | |
14846 | case NC_COMPLEX: | |
14847 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
14848 | return NC_UL_NOTINT; | |
14849 | return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val); | |
14850 | default: | |
14851 | go_unreachable(); | |
14852 | } | |
14853 | } | |
14854 | ||
14855 | // Express integer value as unsigned long if possible. | |
14856 | ||
14857 | Numeric_constant::To_unsigned_long | |
14858 | Numeric_constant::mpz_to_unsigned_long(const mpz_t ival, | |
14859 | unsigned long *val) const | |
14860 | { | |
14861 | if (mpz_sgn(ival) < 0) | |
14862 | return NC_UL_NEGATIVE; | |
14863 | unsigned long ui = mpz_get_ui(ival); | |
14864 | if (mpz_cmp_ui(ival, ui) != 0) | |
14865 | return NC_UL_BIG; | |
14866 | *val = ui; | |
14867 | return NC_UL_VALID; | |
14868 | } | |
14869 | ||
14870 | // Express floating point value as unsigned long if possible. | |
14871 | ||
14872 | Numeric_constant::To_unsigned_long | |
14873 | Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval, | |
14874 | unsigned long *val) const | |
14875 | { | |
14876 | if (!mpfr_integer_p(fval)) | |
14877 | return NC_UL_NOTINT; | |
14878 | mpz_t ival; | |
14879 | mpz_init(ival); | |
14880 | mpfr_get_z(ival, fval, GMP_RNDN); | |
14881 | To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val); | |
14882 | mpz_clear(ival); | |
14883 | return ret; | |
14884 | } | |
14885 | ||
14886 | // Convert value to integer if possible. | |
14887 | ||
14888 | bool | |
14889 | Numeric_constant::to_int(mpz_t* val) const | |
14890 | { | |
14891 | switch (this->classification_) | |
14892 | { | |
14893 | case NC_INT: | |
14894 | case NC_RUNE: | |
14895 | mpz_init_set(*val, this->u_.int_val); | |
14896 | return true; | |
14897 | case NC_FLOAT: | |
14898 | if (!mpfr_integer_p(this->u_.float_val)) | |
14899 | return false; | |
14900 | mpz_init(*val); | |
14901 | mpfr_get_z(*val, this->u_.float_val, GMP_RNDN); | |
14902 | return true; | |
14903 | case NC_COMPLEX: | |
14904 | if (!mpfr_zero_p(this->u_.complex_val.imag) | |
14905 | || !mpfr_integer_p(this->u_.complex_val.real)) | |
14906 | return false; | |
14907 | mpz_init(*val); | |
14908 | mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN); | |
14909 | return true; | |
14910 | default: | |
14911 | go_unreachable(); | |
14912 | } | |
14913 | } | |
14914 | ||
14915 | // Convert value to floating point if possible. | |
14916 | ||
14917 | bool | |
14918 | Numeric_constant::to_float(mpfr_t* val) const | |
14919 | { | |
14920 | switch (this->classification_) | |
14921 | { | |
14922 | case NC_INT: | |
14923 | case NC_RUNE: | |
14924 | mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN); | |
14925 | return true; | |
14926 | case NC_FLOAT: | |
14927 | mpfr_init_set(*val, this->u_.float_val, GMP_RNDN); | |
14928 | return true; | |
14929 | case NC_COMPLEX: | |
14930 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
14931 | return false; | |
14932 | mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN); | |
14933 | return true; | |
14934 | default: | |
14935 | go_unreachable(); | |
14936 | } | |
14937 | } | |
14938 | ||
14939 | // Convert value to complex. | |
14940 | ||
14941 | bool | |
14942 | Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const | |
14943 | { | |
14944 | switch (this->classification_) | |
14945 | { | |
14946 | case NC_INT: | |
14947 | case NC_RUNE: | |
14948 | mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN); | |
14949 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
14950 | return true; | |
14951 | case NC_FLOAT: | |
14952 | mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN); | |
14953 | mpfr_init_set_ui(*vi, 0, GMP_RNDN); | |
14954 | return true; | |
14955 | case NC_COMPLEX: | |
14956 | mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN); | |
14957 | mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN); | |
14958 | return true; | |
14959 | default: | |
14960 | go_unreachable(); | |
14961 | } | |
14962 | } | |
14963 | ||
14964 | // Get the type. | |
14965 | ||
14966 | Type* | |
14967 | Numeric_constant::type() const | |
14968 | { | |
14969 | if (this->type_ != NULL) | |
14970 | return this->type_; | |
14971 | switch (this->classification_) | |
14972 | { | |
14973 | case NC_INT: | |
14974 | return Type::make_abstract_integer_type(); | |
14975 | case NC_RUNE: | |
14976 | return Type::make_abstract_character_type(); | |
14977 | case NC_FLOAT: | |
14978 | return Type::make_abstract_float_type(); | |
14979 | case NC_COMPLEX: | |
14980 | return Type::make_abstract_complex_type(); | |
14981 | default: | |
14982 | go_unreachable(); | |
14983 | } | |
14984 | } | |
14985 | ||
14986 | // If the constant can be expressed in TYPE, then set the type of the | |
14987 | // constant to TYPE and return true. Otherwise return false, and, if | |
14988 | // ISSUE_ERROR is true, report an appropriate error message. | |
14989 | ||
14990 | bool | |
14991 | Numeric_constant::set_type(Type* type, bool issue_error, Location loc) | |
14992 | { | |
14993 | bool ret; | |
14994 | if (type == NULL) | |
14995 | ret = true; | |
14996 | else if (type->integer_type() != NULL) | |
14997 | ret = this->check_int_type(type->integer_type(), issue_error, loc); | |
14998 | else if (type->float_type() != NULL) | |
14999 | ret = this->check_float_type(type->float_type(), issue_error, loc); | |
15000 | else if (type->complex_type() != NULL) | |
15001 | ret = this->check_complex_type(type->complex_type(), issue_error, loc); | |
15002 | else | |
15003 | go_unreachable(); | |
15004 | if (ret) | |
15005 | this->type_ = type; | |
15006 | return ret; | |
15007 | } | |
15008 | ||
15009 | // Check whether the constant can be expressed in an integer type. | |
15010 | ||
15011 | bool | |
15012 | Numeric_constant::check_int_type(Integer_type* type, bool issue_error, | |
15013 | Location location) const | |
15014 | { | |
15015 | mpz_t val; | |
15016 | switch (this->classification_) | |
15017 | { | |
15018 | case NC_INT: | |
15019 | case NC_RUNE: | |
15020 | mpz_init_set(val, this->u_.int_val); | |
15021 | break; | |
15022 | ||
15023 | case NC_FLOAT: | |
15024 | if (!mpfr_integer_p(this->u_.float_val)) | |
15025 | { | |
15026 | if (issue_error) | |
15027 | error_at(location, "floating point constant truncated to integer"); | |
15028 | return false; | |
15029 | } | |
15030 | mpz_init(val); | |
15031 | mpfr_get_z(val, this->u_.float_val, GMP_RNDN); | |
15032 | break; | |
15033 | ||
15034 | case NC_COMPLEX: | |
15035 | if (!mpfr_integer_p(this->u_.complex_val.real) | |
15036 | || !mpfr_zero_p(this->u_.complex_val.imag)) | |
15037 | { | |
15038 | if (issue_error) | |
15039 | error_at(location, "complex constant truncated to integer"); | |
15040 | return false; | |
15041 | } | |
15042 | mpz_init(val); | |
15043 | mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN); | |
15044 | break; | |
15045 | ||
15046 | default: | |
15047 | go_unreachable(); | |
15048 | } | |
15049 | ||
15050 | bool ret; | |
15051 | if (type->is_abstract()) | |
15052 | ret = true; | |
15053 | else | |
15054 | { | |
15055 | int bits = mpz_sizeinbase(val, 2); | |
15056 | if (type->is_unsigned()) | |
15057 | { | |
15058 | // For an unsigned type we can only accept a nonnegative | |
15059 | // number, and we must be able to represents at least BITS. | |
15060 | ret = mpz_sgn(val) >= 0 && bits <= type->bits(); | |
15061 | } | |
15062 | else | |
15063 | { | |
15064 | // For a signed type we need an extra bit to indicate the | |
15065 | // sign. We have to handle the most negative integer | |
15066 | // specially. | |
15067 | ret = (bits + 1 <= type->bits() | |
15068 | || (bits <= type->bits() | |
15069 | && mpz_sgn(val) < 0 | |
15070 | && (mpz_scan1(val, 0) | |
15071 | == static_cast<unsigned long>(type->bits() - 1)) | |
15072 | && mpz_scan0(val, type->bits()) == ULONG_MAX)); | |
15073 | } | |
15074 | } | |
15075 | ||
15076 | if (!ret && issue_error) | |
15077 | error_at(location, "integer constant overflow"); | |
15078 | ||
15079 | return ret; | |
15080 | } | |
15081 | ||
15082 | // Check whether the constant can be expressed in a floating point | |
15083 | // type. | |
15084 | ||
15085 | bool | |
15086 | Numeric_constant::check_float_type(Float_type* type, bool issue_error, | |
d0bcce51 | 15087 | Location location) |
0c77715b | 15088 | { |
15089 | mpfr_t val; | |
15090 | switch (this->classification_) | |
15091 | { | |
15092 | case NC_INT: | |
15093 | case NC_RUNE: | |
15094 | mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN); | |
15095 | break; | |
15096 | ||
15097 | case NC_FLOAT: | |
15098 | mpfr_init_set(val, this->u_.float_val, GMP_RNDN); | |
15099 | break; | |
15100 | ||
15101 | case NC_COMPLEX: | |
15102 | if (!mpfr_zero_p(this->u_.complex_val.imag)) | |
15103 | { | |
15104 | if (issue_error) | |
15105 | error_at(location, "complex constant truncated to float"); | |
15106 | return false; | |
15107 | } | |
15108 | mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN); | |
15109 | break; | |
15110 | ||
15111 | default: | |
15112 | go_unreachable(); | |
15113 | } | |
15114 | ||
15115 | bool ret; | |
15116 | if (type->is_abstract()) | |
15117 | ret = true; | |
15118 | else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
15119 | { | |
15120 | // A NaN or Infinity always fits in the range of the type. | |
15121 | ret = true; | |
15122 | } | |
15123 | else | |
15124 | { | |
15125 | mp_exp_t exp = mpfr_get_exp(val); | |
15126 | mp_exp_t max_exp; | |
15127 | switch (type->bits()) | |
15128 | { | |
15129 | case 32: | |
15130 | max_exp = 128; | |
15131 | break; | |
15132 | case 64: | |
15133 | max_exp = 1024; | |
15134 | break; | |
15135 | default: | |
15136 | go_unreachable(); | |
15137 | } | |
15138 | ||
15139 | ret = exp <= max_exp; | |
d0bcce51 | 15140 | |
15141 | if (ret) | |
15142 | { | |
15143 | // Round the constant to the desired type. | |
15144 | mpfr_t t; | |
15145 | mpfr_init(t); | |
15146 | switch (type->bits()) | |
15147 | { | |
15148 | case 32: | |
15149 | mpfr_set_prec(t, 24); | |
15150 | break; | |
15151 | case 64: | |
15152 | mpfr_set_prec(t, 53); | |
15153 | break; | |
15154 | default: | |
15155 | go_unreachable(); | |
15156 | } | |
15157 | mpfr_set(t, val, GMP_RNDN); | |
15158 | mpfr_set(val, t, GMP_RNDN); | |
15159 | mpfr_clear(t); | |
15160 | ||
15161 | this->set_float(type, val); | |
15162 | } | |
0c77715b | 15163 | } |
15164 | ||
15165 | mpfr_clear(val); | |
15166 | ||
15167 | if (!ret && issue_error) | |
15168 | error_at(location, "floating point constant overflow"); | |
15169 | ||
15170 | return ret; | |
15171 | } | |
15172 | ||
15173 | // Check whether the constant can be expressed in a complex type. | |
15174 | ||
15175 | bool | |
15176 | Numeric_constant::check_complex_type(Complex_type* type, bool issue_error, | |
d0bcce51 | 15177 | Location location) |
0c77715b | 15178 | { |
15179 | if (type->is_abstract()) | |
15180 | return true; | |
15181 | ||
15182 | mp_exp_t max_exp; | |
15183 | switch (type->bits()) | |
15184 | { | |
15185 | case 64: | |
15186 | max_exp = 128; | |
15187 | break; | |
15188 | case 128: | |
15189 | max_exp = 1024; | |
15190 | break; | |
15191 | default: | |
15192 | go_unreachable(); | |
15193 | } | |
15194 | ||
15195 | mpfr_t real; | |
d0bcce51 | 15196 | mpfr_t imag; |
0c77715b | 15197 | switch (this->classification_) |
15198 | { | |
15199 | case NC_INT: | |
15200 | case NC_RUNE: | |
15201 | mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN); | |
d0bcce51 | 15202 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 15203 | break; |
15204 | ||
15205 | case NC_FLOAT: | |
15206 | mpfr_init_set(real, this->u_.float_val, GMP_RNDN); | |
d0bcce51 | 15207 | mpfr_init_set_ui(imag, 0, GMP_RNDN); |
0c77715b | 15208 | break; |
15209 | ||
15210 | case NC_COMPLEX: | |
0c77715b | 15211 | mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN); |
d0bcce51 | 15212 | mpfr_init_set(imag, this->u_.complex_val.imag, GMP_RNDN); |
0c77715b | 15213 | break; |
15214 | ||
15215 | default: | |
15216 | go_unreachable(); | |
15217 | } | |
15218 | ||
d0bcce51 | 15219 | bool ret = true; |
15220 | if (!mpfr_nan_p(real) | |
15221 | && !mpfr_inf_p(real) | |
15222 | && !mpfr_zero_p(real) | |
15223 | && mpfr_get_exp(real) > max_exp) | |
15224 | { | |
15225 | if (issue_error) | |
15226 | error_at(location, "complex real part overflow"); | |
15227 | ret = false; | |
15228 | } | |
0c77715b | 15229 | |
d0bcce51 | 15230 | if (!mpfr_nan_p(imag) |
15231 | && !mpfr_inf_p(imag) | |
15232 | && !mpfr_zero_p(imag) | |
15233 | && mpfr_get_exp(imag) > max_exp) | |
15234 | { | |
15235 | if (issue_error) | |
15236 | error_at(location, "complex imaginary part overflow"); | |
15237 | ret = false; | |
15238 | } | |
0c77715b | 15239 | |
d0bcce51 | 15240 | if (ret) |
15241 | { | |
15242 | // Round the constant to the desired type. | |
15243 | mpfr_t t; | |
15244 | mpfr_init(t); | |
15245 | switch (type->bits()) | |
15246 | { | |
15247 | case 64: | |
15248 | mpfr_set_prec(t, 24); | |
15249 | break; | |
15250 | case 128: | |
15251 | mpfr_set_prec(t, 53); | |
15252 | break; | |
15253 | default: | |
15254 | go_unreachable(); | |
15255 | } | |
15256 | mpfr_set(t, real, GMP_RNDN); | |
15257 | mpfr_set(real, t, GMP_RNDN); | |
15258 | mpfr_set(t, imag, GMP_RNDN); | |
15259 | mpfr_set(imag, t, GMP_RNDN); | |
15260 | mpfr_clear(t); | |
15261 | ||
15262 | this->set_complex(type, real, imag); | |
15263 | } | |
15264 | ||
15265 | mpfr_clear(real); | |
15266 | mpfr_clear(imag); | |
0c77715b | 15267 | |
15268 | return ret; | |
15269 | } | |
15270 | ||
15271 | // Return an Expression for this value. | |
15272 | ||
15273 | Expression* | |
15274 | Numeric_constant::expression(Location loc) const | |
15275 | { | |
15276 | switch (this->classification_) | |
15277 | { | |
15278 | case NC_INT: | |
15279 | return Expression::make_integer(&this->u_.int_val, this->type_, loc); | |
15280 | case NC_RUNE: | |
15281 | return Expression::make_character(&this->u_.int_val, this->type_, loc); | |
15282 | case NC_FLOAT: | |
15283 | return Expression::make_float(&this->u_.float_val, this->type_, loc); | |
15284 | case NC_COMPLEX: | |
15285 | return Expression::make_complex(&this->u_.complex_val.real, | |
15286 | &this->u_.complex_val.imag, | |
15287 | this->type_, loc); | |
15288 | default: | |
15289 | go_unreachable(); | |
15290 | } | |
15291 | } |