Debian Codegraph @

gcc/rust/rust-gcc.cc

raw
1 // rust-gcc.cc -- Rust frontend to gcc IR.
2 // Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 // Contributed by Ian Lance Taylor, Google.
4 // forked from gccgo
5
6 // This file is part of GCC.
7
8 // GCC is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 3, or (at your option) any later
11 // version.
12
13 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 // for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with GCC; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 #include "rust-system.h"
23
24 // This has to be included outside of extern "C", so we have to
25 // include it here before tree.h includes it later.
26 #include <gmp.h>
27
28 #include "tree.h"
29 #include "opts.h"
30 #include "fold-const.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "varasm.h"
34 #include "tree-iterator.h"
35 #include "tm.h"
36 #include "function.h"
37 #include "cgraph.h"
38 #include "convert.h"
39 #include "gimple-expr.h"
40 #include "gimplify.h"
41 #include "langhooks.h"
42 #include "toplev.h"
43 #include "output.h"
44 #include "realmpfr.h"
45 #include "builtins.h"
46 #include "print-tree.h"
47 #include "attribs.h"
48
49 #include "rust-location.h"
50 #include "rust-linemap.h"
51 #include "rust-backend.h"
52 #include "rust-object-export.h"
53 #include "rust-gcc.h"
54
55 #include "backend/rust-tree.h"
56 #include "backend/rust-builtins.h"
57
58 // Get the tree of a variable for use as an expression. If this is a
59 // zero-sized global, create an expression that refers to the decl but
60 // has zero size.
61 tree
62 Bvariable::get_tree (location_t location) const
63 {
64 if (error_operand_p (this->t_))
65 return error_mark_node;
66
67 TREE_USED (this->t_) = 1;
68 if (this->orig_type_ == NULL || TREE_TYPE (this->t_) == this->orig_type_)
69 {
70 return this->t_;
71 }
72
73 // Return *(orig_type*)&decl. */
74 tree t = build_fold_addr_expr_loc (location, this->t_);
75 t = fold_build1_loc (location, NOP_EXPR,
76 build_pointer_type (this->orig_type_), t);
77 return build_fold_indirect_ref_loc (location, t);
78 }
79
80 Bvariable *
81 Bvariable::error_variable ()
82 {
83 return new Bvariable (error_mark_node);
84 }
85
86 // This file implements the interface between the Rust frontend proper
87 // and the gcc IR. This implements specific instantiations of
88 // abstract classes defined by the Rust frontend proper. The Rust
89 // frontend proper class methods of these classes to generate the
90 // backend representation.
91
92 // A helper function to create a GCC identifier from a C++ string.
93
94 static inline tree
95 get_identifier_from_string (const std::string &str)
96 {
97 return get_identifier_with_length (str.data (), str.length ());
98 }
99
100 namespace Backend {
101
102 // Define the built-in functions that are exposed to GCCRust.
103
104 void
105 init ()
106 {
107 /* We need to define the fetch_and_add functions, since we use them
108 for ++ and --. */
109 // tree t = this->integer_type (true, BITS_PER_UNIT)->get_tree ();
110 // tree p = build_pointer_type (build_qualified_type (t, TYPE_QUAL_VOLATILE));
111 // this->define_builtin (BUILT_IN_SYNC_ADD_AND_FETCH_1,
112 // "__sync_fetch_and_add_1",
113 // NULL, build_function_type_list (t, p, t, NULL_TREE), 0);
114
115 // t = this->integer_type (true, BITS_PER_UNIT * 2)->get_tree ();
116 // p = build_pointer_type (build_qualified_type (t, TYPE_QUAL_VOLATILE));
117 // this->define_builtin (BUILT_IN_SYNC_ADD_AND_FETCH_2,
118 // "__sync_fetch_and_add_2",
119 // NULL, build_function_type_list (t, p, t, NULL_TREE), 0);
120
121 // t = this->integer_type (true, BITS_PER_UNIT * 4)->get_tree ();
122 // p = build_pointer_type (build_qualified_type (t, TYPE_QUAL_VOLATILE));
123 // this->define_builtin (BUILT_IN_SYNC_ADD_AND_FETCH_4,
124 // "__sync_fetch_and_add_4",
125 // NULL, build_function_type_list (t, p, t, NULL_TREE), 0);
126
127 // t = this->integer_type (true, BITS_PER_UNIT * 8)->get_tree ();
128 // p = build_pointer_type (build_qualified_type (t, TYPE_QUAL_VOLATILE));
129 // this->define_builtin (BUILT_IN_SYNC_ADD_AND_FETCH_8,
130 // "__sync_fetch_and_add_8",
131 // NULL, build_function_type_list (t, p, t, NULL_TREE), 0);
132
133 // // We use __builtin_expect for magic import functions.
134 // this->define_builtin (BUILT_IN_EXPECT, "__builtin_expect", NULL,
135 // build_function_type_list (long_integer_type_node,
136 // long_integer_type_node,
137 // long_integer_type_node,
138 // NULL_TREE),
139 // builtin_const);
140
141 // // We use __builtin_memcmp for struct comparisons.
142 // this->define_builtin (BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
143 // build_function_type_list (integer_type_node,
144 // const_ptr_type_node,
145 // const_ptr_type_node,
146 // size_type_node, NULL_TREE),
147 // 0);
148
149 // // We use __builtin_memmove for copying data.
150 // this->define_builtin (BUILT_IN_MEMMOVE, "__builtin_memmove", "memmove",
151 // build_function_type_list (void_type_node, ptr_type_node,
152 // const_ptr_type_node,
153 // size_type_node, NULL_TREE),
154 // 0);
155
156 // // We use __builtin_memset for zeroing data.
157 // this->define_builtin (BUILT_IN_MEMSET, "__builtin_memset", "memset",
158 // build_function_type_list (void_type_node, ptr_type_node,
159 // integer_type_node,
160 // size_type_node, NULL_TREE),
161 // 0);
162
163 // // Used by runtime/internal/sys and math/bits.
164 // this->define_builtin (BUILT_IN_CTZ, "__builtin_ctz", "ctz",
165 // build_function_type_list (integer_type_node,
166 // unsigned_type_node,
167 // NULL_TREE),
168 // builtin_const);
169 // this->define_builtin (BUILT_IN_CTZLL, "__builtin_ctzll", "ctzll",
170 // build_function_type_list (integer_type_node,
171 // long_long_unsigned_type_node,
172 // NULL_TREE),
173 // builtin_const);
174 // this->define_builtin (BUILT_IN_CLZ, "__builtin_clz", "clz",
175 // build_function_type_list (integer_type_node,
176 // unsigned_type_node,
177 // NULL_TREE),
178 // builtin_const);
179 // this->define_builtin (BUILT_IN_CLZLL, "__builtin_clzll", "clzll",
180 // build_function_type_list (integer_type_node,
181 // long_long_unsigned_type_node,
182 // NULL_TREE),
183 // builtin_const);
184 // this->define_builtin (BUILT_IN_POPCOUNT, "__builtin_popcount", "popcount",
185 // build_function_type_list (integer_type_node,
186 // unsigned_type_node,
187 // NULL_TREE),
188 // builtin_const);
189 // this->define_builtin (BUILT_IN_POPCOUNTLL, "__builtin_popcountll",
190 // "popcountll",
191 // build_function_type_list (integer_type_node,
192 // long_long_unsigned_type_node,
193 // NULL_TREE),
194 // builtin_const);
195 // this->define_builtin (BUILT_IN_BSWAP16, "__builtin_bswap16", "bswap16",
196 // build_function_type_list (uint16_type_node,
197 // uint16_type_node, NULL_TREE),
198 // builtin_const);
199 // this->define_builtin (BUILT_IN_BSWAP32, "__builtin_bswap32", "bswap32",
200 // build_function_type_list (uint32_type_node,
201 // uint32_type_node, NULL_TREE),
202 // builtin_const);
203 // this->define_builtin (BUILT_IN_BSWAP64, "__builtin_bswap64", "bswap64",
204 // build_function_type_list (uint64_type_node,
205 // uint64_type_node, NULL_TREE),
206 // builtin_const);
207
208 // We provide some functions for the math library.
209
210 // We use __builtin_return_address in the thunk we build for
211 // functions which call recover, and for runtime.getcallerpc.
212 // t = build_function_type_list (ptr_type_node, unsigned_type_node,
213 // NULL_TREE); this->define_builtin (BUILT_IN_RETURN_ADDRESS,
214 // "__builtin_return_address",
215 // NULL, t, 0);
216
217 // The runtime calls __builtin_dwarf_cfa for runtime.getcallersp.
218 // t = build_function_type_list (ptr_type_node, NULL_TREE);
219 // this->define_builtin (BUILT_IN_DWARF_CFA, "__builtin_dwarf_cfa", NULL, t,
220 // 0);
221
222 // The runtime calls __builtin_extract_return_addr when recording
223 // the address to which a function returns.
224 // this->define_builtin (
225 // BUILT_IN_EXTRACT_RETURN_ADDR, "__builtin_extract_return_addr", NULL,
226 // build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE), 0);
227
228 // The compiler uses __builtin_trap for some exception handling
229 // cases.
230 // this->define_builtin (BUILT_IN_TRAP, "__builtin_trap", NULL,
231 // build_function_type (void_type_node, void_list_node),
232 // builtin_noreturn);
233
234 // The runtime uses __builtin_prefetch.
235 // this->define_builtin (BUILT_IN_PREFETCH, "__builtin_prefetch", NULL,
236 // build_varargs_function_type_list (void_type_node,
237 // const_ptr_type_node,
238 // NULL_TREE),
239 // builtin_novops);
240
241 // The compiler uses __builtin_unreachable for cases that cannot
242 // occur.
243 // this->define_builtin (BUILT_IN_UNREACHABLE, "__builtin_unreachable", NULL,
244 // build_function_type (void_type_node, void_list_node),
245 // builtin_const | builtin_noreturn);
246
247 // We provide some atomic functions.
248 // t = build_function_type_list (uint32_type_node, ptr_type_node,
249 // integer_type_node, NULL_TREE);
250 // this->define_builtin (BUILT_IN_ATOMIC_LOAD_4, "__atomic_load_4", NULL, t,
251 // 0);
252
253 // t = build_function_type_list (uint64_type_node, ptr_type_node,
254 // integer_type_node, NULL_TREE);
255 // this->define_builtin (BUILT_IN_ATOMIC_LOAD_8, "__atomic_load_8", NULL, t,
256 // 0);
257
258 // t = build_function_type_list (void_type_node, ptr_type_node,
259 // uint32_type_node,
260 // integer_type_node, NULL_TREE);
261 // this->define_builtin (BUILT_IN_ATOMIC_STORE_4, "__atomic_store_4", NULL, t,
262 // 0);
263
264 // t = build_function_type_list (void_type_node, ptr_type_node,
265 // uint64_type_node,
266 // integer_type_node, NULL_TREE);
267 // this->define_builtin (BUILT_IN_ATOMIC_STORE_8, "__atomic_store_8", NULL, t,
268 // 0);
269
270 // t = build_function_type_list (uint32_type_node, ptr_type_node,
271 // uint32_type_node, integer_type_node, NULL_TREE);
272 // this->define_builtin (BUILT_IN_ATOMIC_EXCHANGE_4, "__atomic_exchange_4",
273 // NULL,
274 // t, 0);
275
276 // t = build_function_type_list (uint64_type_node, ptr_type_node,
277 // uint64_type_node, integer_type_node, NULL_TREE);
278 // this->define_builtin (BUILT_IN_ATOMIC_EXCHANGE_8, "__atomic_exchange_8",
279 // NULL,
280 // t, 0);
281
282 // t = build_function_type_list (boolean_type_node, ptr_type_node,
283 // ptr_type_node,
284 // uint32_type_node, boolean_type_node,
285 // integer_type_node, integer_type_node,
286 // NULL_TREE);
287 // this->define_builtin (BUILT_IN_ATOMIC_COMPARE_EXCHANGE_4,
288 // "__atomic_compare_exchange_4", NULL, t, 0);
289
290 // t = build_function_type_list (boolean_type_node, ptr_type_node,
291 // ptr_type_node,
292 // uint64_type_node, boolean_type_node,
293 // integer_type_node, integer_type_node,
294 // NULL_TREE);
295 // this->define_builtin (BUILT_IN_ATOMIC_COMPARE_EXCHANGE_8,
296 // "__atomic_compare_exchange_8", NULL, t, 0);
297
298 // t = build_function_type_list (uint32_type_node, ptr_type_node,
299 // uint32_type_node, integer_type_node, NULL_TREE);
300 // this->define_builtin (BUILT_IN_ATOMIC_ADD_FETCH_4, "__atomic_add_fetch_4",
301 // NULL, t, 0);
302
303 // t = build_function_type_list (uint64_type_node, ptr_type_node,
304 // uint64_type_node, integer_type_node, NULL_TREE);
305 // this->define_builtin (BUILT_IN_ATOMIC_ADD_FETCH_8, "__atomic_add_fetch_8",
306 // NULL, t, 0);
307
308 // t = build_function_type_list (unsigned_char_type_node, ptr_type_node,
309 // unsigned_char_type_node, integer_type_node,
310 // NULL_TREE);
311 // this->define_builtin (BUILT_IN_ATOMIC_AND_FETCH_1, "__atomic_and_fetch_1",
312 // NULL, t, 0);
313 // this->define_builtin (BUILT_IN_ATOMIC_FETCH_AND_1, "__atomic_fetch_and_1",
314 // NULL, t, 0);
315
316 // t = build_function_type_list (unsigned_char_type_node, ptr_type_node,
317 // unsigned_char_type_node, integer_type_node,
318 // NULL_TREE);
319 // this->define_builtin (BUILT_IN_ATOMIC_OR_FETCH_1, "__atomic_or_fetch_1",
320 // NULL,
321 // t, 0);
322 // this->define_builtin (BUILT_IN_ATOMIC_FETCH_OR_1, "__atomic_fetch_or_1",
323 // NULL,
324 // t, 0);
325 }
326
327 void
328 debug (tree t)
329 {
330 debug_tree (t);
331 };
332
333 void
334 debug (Bvariable *t)
335 {
336 debug_tree (t->get_decl ());
337 };
338
339 tree
340 get_identifier_node (const std::string &str)
341 {
342 return get_identifier_with_length (str.data (), str.length ());
343 }
344
345 tree
346 wchar_type ()
347 {
348 static tree wchar;
349
350 if (wchar == NULL_TREE)
351 {
352 wchar = make_unsigned_type (32);
353 TYPE_STRING_FLAG (wchar) = 1;
354 }
355
356 return wchar;
357 }
358
359 // Get an unnamed integer type.
360
361 int
362 get_pointer_size ()
363 {
364 return POINTER_SIZE;
365 }
366
367 tree
368 raw_str_type ()
369 {
370 tree char_ptr = build_pointer_type (char_type_node);
371 tree const_char_type = build_qualified_type (char_ptr, TYPE_QUAL_CONST);
372 return const_char_type;
373 }
374
375 tree
376 integer_type (bool is_unsigned, int bits)
377 {
378 tree type;
379 if (is_unsigned)
380 {
381 if (bits == INT_TYPE_SIZE)
382 type = unsigned_type_node;
383 else if (bits == SHORT_TYPE_SIZE)
384 type = short_unsigned_type_node;
385 else if (bits == LONG_TYPE_SIZE)
386 type = long_unsigned_type_node;
387 else if (bits == LONG_LONG_TYPE_SIZE)
388 type = long_long_unsigned_type_node;
389 else
390 type = make_unsigned_type (bits);
391 }
392 else
393 {
394 if (bits == INT_TYPE_SIZE)
395 type = integer_type_node;
396 else if (bits == SHORT_TYPE_SIZE)
397 type = short_integer_type_node;
398 else if (bits == LONG_TYPE_SIZE)
399 type = long_integer_type_node;
400 else if (bits == LONG_LONG_TYPE_SIZE)
401 type = long_long_integer_type_node;
402 else
403 type = make_signed_type (bits);
404 }
405 return type;
406 }
407
408 // Get an unnamed float type.
409
410 tree
411 float_type (int bits)
412 {
413 tree type;
414 if (bits == TYPE_PRECISION (float_type_node))
415 type = float_type_node;
416 else if (bits == TYPE_PRECISION (double_type_node))
417 type = double_type_node;
418 else if (bits == TYPE_PRECISION (long_double_type_node))
419 type = long_double_type_node;
420 else
421 {
422 type = make_node (REAL_TYPE);
423 TYPE_PRECISION (type) = bits;
424 layout_type (type);
425 }
426 return type;
427 }
428
429 // Get a pointer type.
430
431 tree
432 pointer_type (tree to_type)
433 {
434 if (error_operand_p (to_type))
435 return error_mark_node;
436 tree type = build_pointer_type (to_type);
437 return type;
438 }
439
440 // Get a reference type.
441
442 tree
443 reference_type (tree to_type)
444 {
445 if (error_operand_p (to_type))
446 return error_mark_node;
447 tree type = build_reference_type (to_type);
448 return type;
449 }
450
451 // Get immutable type
452
453 tree
454 immutable_type (tree base)
455 {
456 if (error_operand_p (base))
457 return error_mark_node;
458 tree constified = build_qualified_type (base, TYPE_QUAL_CONST);
459 return constified;
460 }
461
462 // Make a function type.
463
464 tree
465 function_type (const typed_identifier &receiver,
466 const std::vector<typed_identifier> &parameters,
467 const std::vector<typed_identifier> &results, tree result_struct,
468 location_t)
469 {
470 tree args = NULL_TREE;
471 tree *pp = &args;
472 if (receiver.type != NULL_TREE)
473 {
474 tree t = receiver.type;
475 if (error_operand_p (t))
476 return error_mark_node;
477 *pp = tree_cons (NULL_TREE, t, NULL_TREE);
478 pp = &TREE_CHAIN (*pp);
479 }
480
481 for (const auto &p : parameters)
482 {
483 tree t = p.type;
484 if (error_operand_p (t))
485 return error_mark_node;
486 *pp = tree_cons (NULL_TREE, t, NULL_TREE);
487 pp = &TREE_CHAIN (*pp);
488 }
489
490 // Varargs is handled entirely at the Rust level. When converted to
491 // GENERIC functions are not varargs.
492 *pp = void_list_node;
493
494 tree result;
495 if (results.empty ())
496 result = void_type_node;
497 else if (results.size () == 1)
498 result = results.front ().type;
499 else
500 {
501 gcc_assert (result_struct != NULL);
502 result = result_struct;
503 }
504 if (error_operand_p (result))
505 return error_mark_node;
506
507 tree fntype = build_function_type (result, args);
508 if (error_operand_p (fntype))
509 return error_mark_node;
510
511 return build_pointer_type (fntype);
512 }
513
514 tree
515 function_type_variadic (const typed_identifier &receiver,
516 const std::vector<typed_identifier> &parameters,
517 const std::vector<typed_identifier> &results,
518 tree result_struct, location_t)
519 {
520 size_t n = parameters.size () + (receiver.type != NULL_TREE ? 1 : 0);
521 tree *args = XALLOCAVEC (tree, n);
522 size_t offs = 0;
523 if (error_operand_p (receiver.type))
524 return error_mark_node;
525
526 if (receiver.type != NULL_TREE)
527 args[offs++] = receiver.type;
528
529 for (const auto &p : parameters)
530 {
531 tree t = p.type;
532 if (error_operand_p (t))
533 return error_mark_node;
534 args[offs++] = t;
535 }
536
537 tree result;
538 if (results.empty ())
539 result = void_type_node;
540 else if (results.size () == 1)
541 result = results.front ().type;
542 else
543 {
544 gcc_assert (result_struct != NULL_TREE);
545 result = result_struct;
546 }
547 if (error_operand_p (result))
548 return error_mark_node;
549
550 tree fntype = build_varargs_function_type_array (result, n, args);
551 if (error_operand_p (fntype))
552 return error_mark_node;
553
554 return build_pointer_type (fntype);
555 }
556
557 tree
558 function_ptr_type (tree result_type, const std::vector<tree> &parameters,
559 location_t /* locus */)
560 {
561 tree args = NULL_TREE;
562 tree *pp = &args;
563
564 for (auto &param : parameters)
565 {
566 if (error_operand_p (param))
567 return error_mark_node;
568
569 *pp = tree_cons (NULL_TREE, param, NULL_TREE);
570 pp = &TREE_CHAIN (*pp);
571 }
572
573 *pp = void_list_node;
574
575 tree result = result_type;
576 if (result != void_type_node && int_size_in_bytes (result) == 0)
577 result = void_type_node;
578
579 tree fntype = build_function_type (result, args);
580 if (error_operand_p (fntype))
581 return error_mark_node;
582
583 return build_pointer_type (fntype);
584 }
585
586 // Make a struct type.
587
588 tree
589 struct_type (const std::vector<typed_identifier> &fields, bool layout)
590 {
591 return fill_in_fields (make_node (RECORD_TYPE), fields, layout);
592 }
593
594 // Make a union type.
595
596 tree
597 union_type (const std::vector<typed_identifier> &fields, bool layout)
598 {
599 return fill_in_fields (make_node (UNION_TYPE), fields, layout);
600 }
601
602 // Fill in the fields of a struct or union type.
603
604 tree
605 fill_in_fields (tree fill, const std::vector<typed_identifier> &fields,
606 bool layout)
607 {
608 tree field_trees = NULL_TREE;
609 tree *pp = &field_trees;
610 for (const auto &p : fields)
611 {
612 tree name_tree = get_identifier_from_string (p.name);
613 tree type_tree = p.type;
614 if (error_operand_p (type_tree))
615 return error_mark_node;
616 tree field = build_decl (p.location, FIELD_DECL, name_tree, type_tree);
617 DECL_CONTEXT (field) = fill;
618 *pp = field;
619 pp = &DECL_CHAIN (field);
620 }
621 TYPE_FIELDS (fill) = field_trees;
622
623 if (layout)
624 layout_type (fill);
625
626 // Because Rust permits converting between named struct types and
627 // equivalent struct types, for which we use VIEW_CONVERT_EXPR, and
628 // because we don't try to maintain TYPE_CANONICAL for struct types,
629 // we need to tell the middle-end to use structural equality.
630 SET_TYPE_STRUCTURAL_EQUALITY (fill);
631
632 return fill;
633 }
634
635 // Make an array type.
636
637 tree
638 array_type (tree element_type, tree length)
639 {
640 return fill_in_array (make_node (ARRAY_TYPE), element_type, length);
641 }
642
643 // Fill in an array type.
644
645 tree
646 fill_in_array (tree fill, tree element_type, tree length_tree)
647 {
648 if (error_operand_p (element_type) || error_operand_p (length_tree))
649 return error_mark_node;
650
651 gcc_assert (TYPE_SIZE (element_type) != NULL_TREE);
652
653 length_tree = fold_convert (sizetype, length_tree);
654
655 // build_index_type takes the maximum index, which is one less than
656 // the length.
657 tree index_type_tree = build_index_type (
658 fold_build2 (MINUS_EXPR, sizetype, length_tree, size_one_node));
659
660 TREE_TYPE (fill) = element_type;
661 TYPE_DOMAIN (fill) = index_type_tree;
662 TYPE_ADDR_SPACE (fill) = TYPE_ADDR_SPACE (element_type);
663 layout_type (fill);
664
665 if (TYPE_STRUCTURAL_EQUALITY_P (element_type))
666 SET_TYPE_STRUCTURAL_EQUALITY (fill);
667 else if (TYPE_CANONICAL (element_type) != element_type
668 || TYPE_CANONICAL (index_type_tree) != index_type_tree)
669 TYPE_CANONICAL (fill) = build_array_type (TYPE_CANONICAL (element_type),
670 TYPE_CANONICAL (index_type_tree));
671
672 return fill;
673 }
674
675 // Return a named version of a type.
676
677 tree
678 named_type (const std::string &name, tree type, location_t location)
679 {
680 if (error_operand_p (type))
681 return error_mark_node;
682
683 // The middle-end expects a basic type to have a name. In Rust every
684 // basic type will have a name. The first time we see a basic type,
685 // give it whatever Rust name we have at this point.
686 if (TYPE_NAME (type) == NULL_TREE && location == BUILTINS_LOCATION
687 && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == REAL_TYPE
688 || TREE_CODE (type) == COMPLEX_TYPE
689 || TREE_CODE (type) == BOOLEAN_TYPE))
690 {
691 tree decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
692 get_identifier_from_string (name), type);
693 TYPE_NAME (type) = decl;
694 return type;
695 }
696
697 tree copy = build_variant_type_copy (type);
698 tree decl
699 = build_decl (location, TYPE_DECL, get_identifier_from_string (name), copy);
700 DECL_ORIGINAL_TYPE (decl) = type;
701 TYPE_NAME (copy) = decl;
702 return copy;
703 }
704
705 // Return the size of a type.
706
707 int64_t
708 type_size (tree t)
709 {
710 if (error_operand_p (t))
711 return 1;
712 if (t == void_type_node)
713 return 0;
714 t = TYPE_SIZE_UNIT (t);
715 gcc_assert (tree_fits_uhwi_p (t));
716 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW (t);
717 int64_t ret = static_cast<int64_t> (val_wide);
718 if (ret < 0 || static_cast<unsigned HOST_WIDE_INT> (ret) != val_wide)
719 return -1;
720 return ret;
721 }
722
723 // Return the alignment of a type.
724
725 int64_t
726 type_alignment (tree t)
727 {
728 if (error_operand_p (t))
729 return 1;
730 return TYPE_ALIGN_UNIT (t);
731 }
732
733 // Return the alignment of a struct field of type BTYPE.
734
735 int64_t
736 type_field_alignment (tree t)
737 {
738 if (error_operand_p (t))
739 return 1;
740 return rust_field_alignment (t);
741 }
742
743 // Return the offset of a field in a struct.
744
745 int64_t
746 type_field_offset (tree struct_tree, size_t index)
747 {
748 if (error_operand_p (struct_tree))
749 return 0;
750 gcc_assert (TREE_CODE (struct_tree) == RECORD_TYPE);
751 tree field = TYPE_FIELDS (struct_tree);
752 for (; index > 0; --index)
753 {
754 field = DECL_CHAIN (field);
755 gcc_assert (field != NULL_TREE);
756 }
757 HOST_WIDE_INT offset_wide = int_byte_position (field);
758 int64_t ret = static_cast<int64_t> (offset_wide);
759 gcc_assert (ret == offset_wide);
760 return ret;
761 }
762
763 // Return the zero value for a type.
764
765 tree
766 zero_expression (tree t)
767 {
768 tree ret;
769 if (error_operand_p (t))
770 ret = error_mark_node;
771 else
772 ret = build_zero_cst (t);
773 return ret;
774 }
775
776 // An expression that references a variable.
777
778 tree
779 var_expression (Bvariable *var, location_t location)
780 {
781 return var->get_tree (location);
782 }
783
784 // Return a typed value as a constant floating-point number.
785
786 tree
787 float_constant_expression (tree t, mpfr_t val)
788 {
789 tree ret;
790 if (error_operand_p (t))
791 return error_mark_node;
792
793 REAL_VALUE_TYPE r1;
794 real_from_mpfr (&r1, val, t, GMP_RNDN);
795 REAL_VALUE_TYPE r2;
796 real_convert (&r2, TYPE_MODE (t), &r1);
797 ret = build_real (t, r2);
798 return ret;
799 }
800
801 // Make a constant string expression.
802
803 tree
804 string_constant_expression (const std::string &val)
805 {
806 tree index_type = build_index_type (size_int (val.length ()));
807 tree const_char_type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
808 tree string_type = build_array_type (const_char_type, index_type);
809 TYPE_STRING_FLAG (string_type) = 1;
810 tree string_val = build_string (val.length (), val.data ());
811 TREE_TYPE (string_val) = string_type;
812
813 return string_val;
814 }
815
816 tree
817 wchar_constant_expression (wchar_t c)
818 {
819 return build_int_cst (wchar_type (), c);
820 }
821
822 tree
823 char_constant_expression (char c)
824 {
825 return build_int_cst (char_type_node, c);
826 }
827
828 // Make a constant boolean expression.
829
830 tree
831 boolean_constant_expression (bool val)
832 {
833 return val ? boolean_true_node : boolean_false_node;
834 }
835
836 // An expression that converts an expression to a different type.
837
838 tree
839 convert_expression (tree type_tree, tree expr_tree, location_t location)
840 {
841 if (error_operand_p (type_tree) || error_operand_p (expr_tree))
842 return error_mark_node;
843
844 tree ret;
845 if (type_size (type_tree) == 0 || TREE_TYPE (expr_tree) == void_type_node)
846 {
847 // Do not convert zero-sized types.
848 ret = expr_tree;
849 }
850 else if (TREE_CODE (type_tree) == INTEGER_TYPE)
851 ret = convert_to_integer (type_tree, expr_tree);
852 else if (TREE_CODE (type_tree) == REAL_TYPE)
853 ret = convert_to_real (type_tree, expr_tree);
854 else if (TREE_CODE (type_tree) == COMPLEX_TYPE)
855 ret = convert_to_complex (type_tree, expr_tree);
856 else if (TREE_CODE (type_tree) == POINTER_TYPE
857 && TREE_CODE (TREE_TYPE (expr_tree)) == INTEGER_TYPE)
858 ret = convert_to_pointer (type_tree, expr_tree);
859 else if (TREE_CODE (type_tree) == RECORD_TYPE
860 || TREE_CODE (type_tree) == ARRAY_TYPE)
861 ret = fold_build1_loc (location, VIEW_CONVERT_EXPR, type_tree, expr_tree);
862 else
863 ret = fold_convert_loc (location, type_tree, expr_tree);
864
865 return ret;
866 }
867
868 // Return an expression for the field at INDEX in BSTRUCT.
869
870 tree
871 struct_field_expression (tree struct_tree, size_t index, location_t location)
872 {
873 if (error_operand_p (struct_tree))
874 return error_mark_node;
875 gcc_assert (TREE_CODE (TREE_TYPE (struct_tree)) == RECORD_TYPE
876 || TREE_CODE (TREE_TYPE (struct_tree)) == UNION_TYPE);
877 tree field = TYPE_FIELDS (TREE_TYPE (struct_tree));
878 if (field == NULL_TREE)
879 {
880 // This can happen for a type which refers to itself indirectly
881 // and then turns out to be erroneous.
882 return error_mark_node;
883 }
884 for (unsigned int i = index; i > 0; --i)
885 {
886 field = DECL_CHAIN (field);
887 gcc_assert (field != NULL_TREE);
888 }
889 if (error_operand_p (TREE_TYPE (field)))
890 return error_mark_node;
891 tree ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (field),
892 struct_tree, field, NULL_TREE);
893 if (TREE_CONSTANT (struct_tree))
894 TREE_CONSTANT (ret) = 1;
895 return ret;
896 }
897
898 // Return an expression that executes BSTAT before BEXPR.
899
900 tree
901 compound_expression (tree stat, tree expr, location_t location)
902 {
903 if (error_operand_p (stat) || error_operand_p (expr))
904 return error_mark_node;
905 tree ret
906 = fold_build2_loc (location, COMPOUND_EXPR, TREE_TYPE (expr), stat, expr);
907 return ret;
908 }
909
910 // Return an expression that executes THEN_EXPR if CONDITION is true, or
911 // ELSE_EXPR otherwise.
912
913 tree
914 conditional_expression (tree, tree type_tree, tree cond_expr, tree then_expr,
915 tree else_expr, location_t location)
916 {
917 if (error_operand_p (type_tree) || error_operand_p (cond_expr)
918 || error_operand_p (then_expr) || error_operand_p (else_expr))
919 return error_mark_node;
920 tree ret = build3_loc (location, COND_EXPR, type_tree, cond_expr, then_expr,
921 else_expr);
922 return ret;
923 }
924
925 /* Helper function that converts rust operators to equivalent GCC tree_code.
926 Note that CompoundAssignmentOperator don't get their corresponding tree_code,
927 because they get compiled away when we lower AST to HIR. */
928 static enum tree_code
929 operator_to_tree_code (NegationOperator op)
930 {
931 switch (op)
932 {
933 case NegationOperator::NEGATE:
934 return NEGATE_EXPR;
935 case NegationOperator::NOT:
936 return BIT_NOT_EXPR;
937 default:
938 rust_unreachable ();
939 }
940 }
941
942 /* Note that GCC tree code distinguishes floating point division and integer
943 division. These two types of division are represented as the same rust
944 operator, and can only be distinguished via context(i.e. the TREE_TYPE of the
945 operands). */
946 static enum tree_code
947 operator_to_tree_code (ArithmeticOrLogicalOperator op, bool floating_point)
948 {
949 switch (op)
950 {
951 case ArithmeticOrLogicalOperator::ADD:
952 return PLUS_EXPR;
953 case ArithmeticOrLogicalOperator::SUBTRACT:
954 return MINUS_EXPR;
955 case ArithmeticOrLogicalOperator::MULTIPLY:
956 return MULT_EXPR;
957 case ArithmeticOrLogicalOperator::DIVIDE:
958 if (floating_point)
959 return RDIV_EXPR;
960 else
961 return TRUNC_DIV_EXPR;
962 case ArithmeticOrLogicalOperator::MODULUS:
963 return TRUNC_MOD_EXPR;
964 case ArithmeticOrLogicalOperator::BITWISE_AND:
965 return BIT_AND_EXPR;
966 case ArithmeticOrLogicalOperator::BITWISE_OR:
967 return BIT_IOR_EXPR;
968 case ArithmeticOrLogicalOperator::BITWISE_XOR:
969 return BIT_XOR_EXPR;
970 case ArithmeticOrLogicalOperator::LEFT_SHIFT:
971 return LSHIFT_EXPR;
972 case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
973 return RSHIFT_EXPR;
974 default:
975 rust_unreachable ();
976 }
977 }
978
979 static enum tree_code
980 operator_to_tree_code (ComparisonOperator op)
981 {
982 switch (op)
983 {
984 case ComparisonOperator::EQUAL:
985 return EQ_EXPR;
986 case ComparisonOperator::NOT_EQUAL:
987 return NE_EXPR;
988 case ComparisonOperator::GREATER_THAN:
989 return GT_EXPR;
990 case ComparisonOperator::LESS_THAN:
991 return LT_EXPR;
992 case ComparisonOperator::GREATER_OR_EQUAL:
993 return GE_EXPR;
994 case ComparisonOperator::LESS_OR_EQUAL:
995 return LE_EXPR;
996 default:
997 rust_unreachable ();
998 }
999 }
1000
1001 static enum tree_code
1002 operator_to_tree_code (LazyBooleanOperator op)
1003 {
1004 switch (op)
1005 {
1006 case LazyBooleanOperator::LOGICAL_OR:
1007 return TRUTH_ORIF_EXPR;
1008 case LazyBooleanOperator::LOGICAL_AND:
1009 return TRUTH_ANDIF_EXPR;
1010 default:
1011 rust_unreachable ();
1012 }
1013 }
1014
1015 /* Returns true if the type of EXP is a floating point type.
1016 False otherwise. */
1017 bool
1018 is_floating_point (tree exp)
1019 {
1020 return FLOAT_TYPE_P (TREE_TYPE (exp));
1021 }
1022
1023 // Return an expression for the negation operation OP EXPR.
1024 tree
1025 negation_expression (NegationOperator op, tree expr_tree, location_t location)
1026 {
1027 /* Check if the expression is an error, in which case we return an error
1028 expression. */
1029 if (error_operand_p (expr_tree))
1030 return error_mark_node;
1031
1032 /* For negation operators, the resulting type should be the same as its
1033 operand. */
1034 auto tree_type = TREE_TYPE (expr_tree);
1035 auto original_type = tree_type;
1036 auto tree_code = operator_to_tree_code (op);
1037
1038 /* For floating point operations we may need to extend the precision of type.
1039 For example, a 64-bit machine may not support operations on float32. */
1040 bool floating_point = is_floating_point (expr_tree);
1041 auto extended_type = NULL_TREE;
1042 if (floating_point)
1043 {
1044 extended_type = excess_precision_type (tree_type);
1045 if (extended_type != NULL_TREE)
1046 {
1047 expr_tree = convert (extended_type, expr_tree);
1048 tree_type = extended_type;
1049 }
1050 }
1051
1052 /* Construct a new tree and build an expression from it. */
1053 auto new_tree = fold_build1_loc (location, tree_code, tree_type, expr_tree);
1054 if (floating_point && extended_type != NULL_TREE)
1055 new_tree = convert (original_type, expr_tree);
1056 return new_tree;
1057 }
1058
1059 tree
1060 arithmetic_or_logical_expression (ArithmeticOrLogicalOperator op, tree left,
1061 tree right, location_t location)
1062 {
1063 /* Check if either expression is an error, in which case we return an error
1064 expression. */
1065 if (error_operand_p (left) || error_operand_p (right))
1066 return error_mark_node;
1067
1068 // unwrap the const decls if set
1069 if (TREE_CODE (left) == CONST_DECL)
1070 left = DECL_INITIAL (left);
1071 if (TREE_CODE (right) == CONST_DECL)
1072 right = DECL_INITIAL (right);
1073
1074 /* We need to determine if we're doing floating point arithmetics of integer
1075 arithmetics. */
1076 bool floating_point = is_floating_point (left);
1077 auto ret = NULL_TREE;
1078
1079 /* For arithmetic or logical operators, the resulting type should be the same
1080 as the lhs operand. */
1081 auto tree_type = TREE_TYPE (left);
1082 auto original_type = tree_type;
1083 auto tree_code = operator_to_tree_code (op, floating_point);
1084
1085 /* For floating point operations we may need to extend the precision of type.
1086 For example, a 64-bit machine may not support operations on float32. */
1087 auto extended_type = NULL_TREE;
1088 if (floating_point)
1089 {
1090 extended_type = excess_precision_type (tree_type);
1091 if (extended_type != NULL_TREE)
1092 {
1093 left = convert (extended_type, left);
1094 right = convert (extended_type, right);
1095 tree_type = extended_type;
1096 }
1097 }
1098
1099 ret = fold_build2_loc (location, tree_code, tree_type, left, right);
1100 TREE_CONSTANT (ret) = TREE_CONSTANT (left) & TREE_CONSTANT (right);
1101
1102 // TODO: How do we handle floating point?
1103 if (floating_point && extended_type != NULL_TREE)
1104 ret = convert (original_type, ret);
1105
1106 if (op == ArithmeticOrLogicalOperator::DIVIDE
1107 && (integer_zerop (right) || fixed_zerop (right)))
1108 {
1109 rust_error_at (location, "division by zero");
1110 }
1111 else if (op == ArithmeticOrLogicalOperator::LEFT_SHIFT
1112 && (compare_tree_int (right, TYPE_PRECISION (TREE_TYPE (ret))) >= 0))
1113 {
1114 rust_error_at (location, "left shift count >= width of type");
1115 }
1116
1117 return ret;
1118 }
1119
1120 static bool
1121 is_overflowing_expr (ArithmeticOrLogicalOperator op)
1122 {
1123 switch (op)
1124 {
1125 case ArithmeticOrLogicalOperator::ADD:
1126 case ArithmeticOrLogicalOperator::SUBTRACT:
1127 case ArithmeticOrLogicalOperator::MULTIPLY:
1128 return true;
1129 default:
1130 return false;
1131 }
1132 }
1133
1134 static std::pair<tree, tree>
1135 fetch_overflow_builtins (ArithmeticOrLogicalOperator op)
1136 {
1137 auto builtin_ctx = Rust::Compile::BuiltinsContext::get ();
1138
1139 auto builtin = NULL_TREE;
1140 auto abort = NULL_TREE;
1141
1142 switch (op)
1143 {
1144 case ArithmeticOrLogicalOperator::ADD:
1145 builtin_ctx.lookup_simple_builtin ("__builtin_add_overflow", &builtin);
1146 break;
1147 case ArithmeticOrLogicalOperator::SUBTRACT:
1148 builtin_ctx.lookup_simple_builtin ("__builtin_sub_overflow", &builtin);
1149 break;
1150 case ArithmeticOrLogicalOperator::MULTIPLY:
1151 builtin_ctx.lookup_simple_builtin ("__builtin_mul_overflow", &builtin);
1152 break;
1153 default:
1154 rust_unreachable ();
1155 break;
1156 };
1157
1158 builtin_ctx.lookup_simple_builtin ("__builtin_abort", &abort);
1159
1160 rust_assert (abort);
1161 rust_assert (builtin);
1162
1163 return {abort, builtin};
1164 }
1165
1166 // Return an expression for the arithmetic or logical operation LEFT OP RIGHT
1167 // with overflow checking when possible
1168 tree
1169 arithmetic_or_logical_expression_checked (ArithmeticOrLogicalOperator op,
1170 tree left, tree right,
1171 location_t location,
1172 Bvariable *receiver_var)
1173 {
1174 /* Check if either expression is an error, in which case we return an error
1175 expression. */
1176 if (error_operand_p (left) || error_operand_p (right))
1177 return error_mark_node;
1178
1179 // FIXME: Add `if (!debug_mode)`
1180 // No overflow checks for floating point operations or divisions. In that
1181 // case, simply assign the result of the operation to the receiver variable
1182 if (is_floating_point (left) || !is_overflowing_expr (op))
1183 return assignment_statement (
1184 receiver_var->get_tree (location),
1185 arithmetic_or_logical_expression (op, left, right, location), location);
1186
1187 auto receiver = receiver_var->get_tree (location);
1188 TREE_ADDRESSABLE (receiver) = 1;
1189 auto result_ref = build_fold_addr_expr_loc (location, receiver);
1190
1191 auto builtins = fetch_overflow_builtins (op);
1192 auto abort = builtins.first;
1193 auto builtin = builtins.second;
1194
1195 auto abort_call = build_call_expr_loc (location, abort, 0);
1196
1197 auto builtin_call
1198 = build_call_expr_loc (location, builtin, 3, left, right, result_ref);
1199 auto overflow_check
1200 = build2_loc (location, EQ_EXPR, boolean_type_node, builtin_call,
1201 boolean_constant_expression (true));
1202
1203 auto if_block = build3_loc (location, COND_EXPR, void_type_node,
1204 overflow_check, abort_call, NULL_TREE);
1205
1206 return if_block;
1207 }
1208
1209 // Return an expression for the comparison operation LEFT OP RIGHT.
1210 tree
1211 comparison_expression (ComparisonOperator op, tree left_tree, tree right_tree,
1212 location_t location)
1213 {
1214 /* Check if either expression is an error, in which case we return an error
1215 expression. */
1216 if (error_operand_p (left_tree) || error_operand_p (right_tree))
1217 return error_mark_node;
1218
1219 /* For comparison operators, the resulting type should be boolean. */
1220 auto tree_type = boolean_type_node;
1221 auto tree_code = operator_to_tree_code (op);
1222
1223 /* Construct a new tree and build an expression from it. */
1224 auto new_tree
1225 = fold_build2_loc (location, tree_code, tree_type, left_tree, right_tree);
1226 return new_tree;
1227 }
1228
1229 // Return an expression for the lazy boolean operation LEFT OP RIGHT.
1230 tree
1231 lazy_boolean_expression (LazyBooleanOperator op, tree left_tree,
1232 tree right_tree, location_t location)
1233 {
1234 /* Check if either expression is an error, in which case we return an error
1235 expression. */
1236 if (error_operand_p (left_tree) || error_operand_p (right_tree))
1237 return error_mark_node;
1238
1239 /* For lazy boolean operators, the resulting type should be the same as the
1240 rhs operand. */
1241 auto tree_type = TREE_TYPE (right_tree);
1242 auto tree_code = operator_to_tree_code (op);
1243
1244 /* Construct a new tree and build an expression from it. */
1245 auto new_tree
1246 = fold_build2_loc (location, tree_code, tree_type, left_tree, right_tree);
1247 return new_tree;
1248 }
1249
1250 // Return an expression that constructs BTYPE with VALS.
1251
1252 tree
1253 constructor_expression (tree type_tree, bool is_variant,
1254 const std::vector<tree> &vals, int union_index,
1255 location_t location)
1256 {
1257 if (error_operand_p (type_tree))
1258 return error_mark_node;
1259
1260 vec<constructor_elt, va_gc> *init;
1261 vec_alloc (init, vals.size ());
1262
1263 tree sink = NULL_TREE;
1264 bool is_constant = true;
1265 tree field = TYPE_FIELDS (type_tree);
1266
1267 if (is_variant)
1268 {
1269 gcc_assert (union_index != -1);
1270 gcc_assert (TREE_CODE (type_tree) == UNION_TYPE);
1271
1272 for (int i = 0; i < union_index; i++)
1273 {
1274 gcc_assert (field != NULL_TREE);
1275 field = DECL_CHAIN (field);
1276 }
1277
1278 tree nested_ctor
1279 = constructor_expression (TREE_TYPE (field), false, vals, -1, location);
1280
1281 constructor_elt empty = {NULL, NULL};
1282 constructor_elt *elt = init->quick_push (empty);
1283 elt->index = field;
1284 elt->value = convert_tree (TREE_TYPE (field), nested_ctor, location);
1285 if (!TREE_CONSTANT (elt->value))
1286 is_constant = false;
1287 }
1288 else
1289 {
1290 if (union_index != -1)
1291 {
1292 gcc_assert (TREE_CODE (type_tree) == UNION_TYPE);
1293 tree val = vals.front ();
1294 for (int i = 0; i < union_index; i++)
1295 {
1296 gcc_assert (field != NULL_TREE);
1297 field = DECL_CHAIN (field);
1298 }
1299
1300 if (TREE_TYPE (field) == error_mark_node || error_operand_p (val))
1301 return error_mark_node;
1302
1303 if (int_size_in_bytes (TREE_TYPE (field)) == 0)
1304 {
1305 // GIMPLE cannot represent indices of zero-sized types so
1306 // trying to construct a map with zero-sized keys might lead
1307 // to errors. Instead, we evaluate each expression that
1308 // would have been added as a map element for its
1309 // side-effects and construct an empty map.
1310 append_to_statement_list (val, &sink);
1311 }
1312 else
1313 {
1314 constructor_elt empty = {NULL, NULL};
1315 constructor_elt *elt = init->quick_push (empty);
1316 elt->index = field;
1317 elt->value = convert_tree (TREE_TYPE (field), val, location);
1318 if (!TREE_CONSTANT (elt->value))
1319 is_constant = false;
1320 }
1321 }
1322 else
1323 {
1324 gcc_assert (TREE_CODE (type_tree) == RECORD_TYPE);
1325 for (std::vector<tree>::const_iterator p = vals.begin ();
1326 p != vals.end (); ++p, field = DECL_CHAIN (field))
1327 {
1328 gcc_assert (field != NULL_TREE);
1329 tree val = (*p);
1330 if (TREE_TYPE (field) == error_mark_node || error_operand_p (val))
1331 return error_mark_node;
1332
1333 if (int_size_in_bytes (TREE_TYPE (field)) == 0)
1334 {
1335 // GIMPLE cannot represent indices of zero-sized types so
1336 // trying to construct a map with zero-sized keys might lead
1337 // to errors. Instead, we evaluate each expression that
1338 // would have been added as a map element for its
1339 // side-effects and construct an empty map.
1340 append_to_statement_list (val, &sink);
1341 continue;
1342 }
1343
1344 constructor_elt empty = {NULL, NULL};
1345 constructor_elt *elt = init->quick_push (empty);
1346 elt->index = field;
1347 elt->value = convert_tree (TREE_TYPE (field), val, location);
1348 if (!TREE_CONSTANT (elt->value))
1349 is_constant = false;
1350 }
1351 // gcc_assert (field == NULL_TREE);
1352 }
1353 }
1354
1355 tree ret = build_constructor (type_tree, init);
1356 if (is_constant)
1357 TREE_CONSTANT (ret) = 1;
1358 if (sink != NULL_TREE)
1359 ret = fold_build2_loc (location, COMPOUND_EXPR, type_tree, sink, ret);
1360 return ret;
1361 }
1362
1363 tree
1364 array_constructor_expression (tree type_tree,
1365 const std::vector<unsigned long> &indexes,
1366 const std::vector<tree> &vals,
1367 location_t location)
1368 {
1369 if (error_operand_p (type_tree))
1370 return error_mark_node;
1371
1372 gcc_assert (indexes.size () == vals.size ());
1373
1374 tree element_type = TREE_TYPE (type_tree);
1375 HOST_WIDE_INT element_size = int_size_in_bytes (element_type);
1376 vec<constructor_elt, va_gc> *init;
1377 vec_alloc (init, element_size == 0 ? 0 : vals.size ());
1378
1379 tree sink = NULL_TREE;
1380 bool is_constant = true;
1381 for (size_t i = 0; i < vals.size (); ++i)
1382 {
1383 tree index = size_int (indexes[i]);
1384 tree val = vals[i];
1385
1386 if (error_operand_p (index) || error_operand_p (val))
1387 return error_mark_node;
1388
1389 if (element_size == 0)
1390 {
1391 // GIMPLE cannot represent arrays of zero-sized types so trying
1392 // to construct an array of zero-sized values might lead to errors.
1393 // Instead, we evaluate each expression that would have been added as
1394 // an array value for its side-effects and construct an empty array.
1395 append_to_statement_list (val, &sink);
1396 continue;
1397 }
1398
1399 if (!TREE_CONSTANT (val))
1400 is_constant = false;
1401
1402 constructor_elt empty = {NULL, NULL};
1403 constructor_elt *elt = init->quick_push (empty);
1404 elt->index = index;
1405 elt->value = val;
1406 }
1407
1408 tree ret = build_constructor (type_tree, init);
1409 if (is_constant)
1410 TREE_CONSTANT (ret) = 1;
1411 if (sink != NULL_TREE)
1412 ret = fold_build2_loc (location, COMPOUND_EXPR, type_tree, sink, ret);
1413 return ret;
1414 }
1415
1416 // Build insns to create an array, initialize all elements of the array to
1417 // value, and return it
1418 tree
1419 array_initializer (tree fndecl, tree block, tree array_type, tree length,
1420 tree value, tree *tmp, location_t locus)
1421 {
1422 std::vector<tree> stmts;
1423
1424 // Temporary array we initialize with the desired value.
1425 tree t = NULL_TREE;
1426 Bvariable *tmp_array = temporary_variable (fndecl, block, array_type,
1427 NULL_TREE, true, locus, &t);
1428 tree arr = tmp_array->get_tree (locus);
1429 stmts.push_back (t);
1430
1431 // Temporary for the array length used for initialization loop guard.
1432 Bvariable *tmp_len = temporary_variable (fndecl, block, size_type_node,
1433 length, true, locus, &t);
1434 tree len = tmp_len->get_tree (locus);
1435 stmts.push_back (t);
1436
1437 // Temporary variable for pointer used to initialize elements.
1438 tree ptr_type = pointer_type (TREE_TYPE (array_type));
1439 tree ptr_init
1440 = build1_loc (locus, ADDR_EXPR, ptr_type,
1441 array_index_expression (arr, integer_zero_node, locus));
1442 Bvariable *tmp_ptr
1443 = temporary_variable (fndecl, block, ptr_type, ptr_init, false, locus, &t);
1444 tree ptr = tmp_ptr->get_tree (locus);
1445 stmts.push_back (t);
1446
1447 // push statement list for the loop
1448 std::vector<tree> loop_stmts;
1449
1450 // Loop exit condition:
1451 // if (length == 0) break;
1452 t = comparison_expression (ComparisonOperator::EQUAL, len,
1453 zero_expression (TREE_TYPE (len)), locus);
1454
1455 t = exit_expression (t, locus);
1456 loop_stmts.push_back (t);
1457
1458 // Assign value to the current pointer position
1459 // *ptr = value;
1460 t = assignment_statement (build_fold_indirect_ref (ptr), value, locus);
1461 loop_stmts.push_back (t);
1462
1463 // Move pointer to next element
1464 // ptr++;
1465 tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptr_type));
1466 t = build2 (POSTINCREMENT_EXPR, ptr_type, ptr, convert (ptr_type, size));
1467 loop_stmts.push_back (t);
1468
1469 // Decrement loop counter.
1470 // length--;
1471 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (len), len,
1472 convert (TREE_TYPE (len), integer_one_node));
1473 loop_stmts.push_back (t);
1474
1475 // pop statments and finish loop
1476 tree loop_body = statement_list (loop_stmts);
1477 stmts.push_back (loop_expression (loop_body, locus));
1478
1479 // Return the temporary in the provided pointer and the statement list which
1480 // initializes it.
1481 *tmp = tmp_array->get_tree (locus);
1482 return statement_list (stmts);
1483 }
1484
1485 // Return an expression representing ARRAY[INDEX]
1486
1487 tree
1488 array_index_expression (tree array_tree, tree index_tree, location_t location)
1489 {
1490 if (error_operand_p (array_tree) || error_operand_p (index_tree))
1491 return error_mark_node;
1492
1493 // A function call that returns a zero sized object will have been
1494 // changed to return void. If we see void here, assume we are
1495 // dealing with a zero sized type and just evaluate the operands.
1496 tree ret;
1497 if (TREE_TYPE (array_tree) != void_type_node)
1498 ret = build4_loc (location, ARRAY_REF, TREE_TYPE (TREE_TYPE (array_tree)),
1499 array_tree, index_tree, NULL_TREE, NULL_TREE);
1500 else
1501 ret = fold_build2_loc (location, COMPOUND_EXPR, void_type_node, array_tree,
1502 index_tree);
1503
1504 return ret;
1505 }
1506
1507 // Create an expression for a call to FN_EXPR with FN_ARGS.
1508 tree
1509 call_expression (tree fn, const std::vector<tree> &fn_args, tree chain_expr,
1510 location_t location)
1511 {
1512 if (error_operand_p (fn))
1513 return error_mark_node;
1514
1515 gcc_assert (FUNCTION_POINTER_TYPE_P (TREE_TYPE (fn)));
1516 tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
1517
1518 size_t nargs = fn_args.size ();
1519 tree *args = nargs == 0 ? NULL : new tree[nargs];
1520 for (size_t i = 0; i < nargs; ++i)
1521 {
1522 args[i] = fn_args.at (i);
1523 }
1524
1525 tree fndecl = fn;
1526 if (TREE_CODE (fndecl) == ADDR_EXPR)
1527 fndecl = TREE_OPERAND (fndecl, 0);
1528
1529 // This is to support builtin math functions when using 80387 math.
1530 tree excess_type = NULL_TREE;
1531 if (optimize && TREE_CODE (fndecl) == FUNCTION_DECL
1532 && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1533 && DECL_IS_UNDECLARED_BUILTIN (fndecl) && nargs > 0
1534 && ((SCALAR_FLOAT_TYPE_P (rettype)
1535 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (args[0])))
1536 || (COMPLEX_FLOAT_TYPE_P (rettype)
1537 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (args[0])))))
1538 {
1539 excess_type = excess_precision_type (TREE_TYPE (args[0]));
1540 if (excess_type != NULL_TREE)
1541 {
1542 tree excess_fndecl
1543 = mathfn_built_in (excess_type, DECL_FUNCTION_CODE (fndecl));
1544 if (excess_fndecl == NULL_TREE)
1545 excess_type = NULL_TREE;
1546 else
1547 {
1548 fn = build_fold_addr_expr_loc (location, excess_fndecl);
1549 for (size_t i = 0; i < nargs; ++i)
1550 {
1551 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (args[i]))
1552 || COMPLEX_FLOAT_TYPE_P (TREE_TYPE (args[i])))
1553 args[i] = ::convert (excess_type, args[i]);
1554 }
1555 }
1556 }
1557 }
1558
1559 tree ret
1560 = build_call_array_loc (location,
1561 excess_type != NULL_TREE ? excess_type : rettype,
1562 fn, nargs, args);
1563
1564 // check for deprecated function usage
1565 if (fndecl && TREE_DEPRECATED (fndecl))
1566 {
1567 // set up the call-site information for `warn_deprecated_use`
1568 input_location = location;
1569 warn_deprecated_use (fndecl, NULL_TREE);
1570 }
1571
1572 if (chain_expr)
1573 CALL_EXPR_STATIC_CHAIN (ret) = chain_expr;
1574
1575 if (excess_type != NULL_TREE)
1576 {
1577 // Calling convert here can undo our excess precision change.
1578 // That may or may not be a bug in convert_to_real.
1579 ret = build1_loc (location, NOP_EXPR, rettype, ret);
1580 }
1581
1582 delete[] args;
1583 return ret;
1584 }
1585
1586 // Variable initialization.
1587
1588 tree
1589 init_statement (tree, Bvariable *var, tree init_tree)
1590 {
1591 tree var_tree = var->get_decl ();
1592 if (error_operand_p (var_tree) || error_operand_p (init_tree))
1593 return error_mark_node;
1594 gcc_assert (TREE_CODE (var_tree) == VAR_DECL);
1595
1596 // To avoid problems with GNU ld, we don't make zero-sized
1597 // externally visible variables. That might lead us to doing an
1598 // initialization of a zero-sized expression to a non-zero sized
1599 // variable, or vice-versa. Avoid crashes by omitting the
1600 // initializer. Such initializations don't mean anything anyhow.
1601 if (int_size_in_bytes (TREE_TYPE (var_tree)) != 0 && init_tree != NULL_TREE
1602 && TREE_TYPE (init_tree) != void_type_node
1603 && int_size_in_bytes (TREE_TYPE (init_tree)) != 0)
1604 {
1605 DECL_INITIAL (var_tree) = init_tree;
1606 init_tree = NULL_TREE;
1607 }
1608
1609 tree ret = build1_loc (DECL_SOURCE_LOCATION (var_tree), DECL_EXPR,
1610 void_type_node, var_tree);
1611 if (init_tree != NULL_TREE)
1612 ret = build2_loc (DECL_SOURCE_LOCATION (var_tree), COMPOUND_EXPR,
1613 void_type_node, init_tree, ret);
1614
1615 return ret;
1616 }
1617
1618 // Assignment.
1619
1620 tree
1621 assignment_statement (tree lhs, tree rhs, location_t location)
1622 {
1623 if (error_operand_p (lhs) || error_operand_p (rhs))
1624 return error_mark_node;
1625
1626 // To avoid problems with GNU ld, we don't make zero-sized
1627 // externally visible variables. That might lead us to doing an
1628 // assignment of a zero-sized expression to a non-zero sized
1629 // expression; avoid crashes here by avoiding assignments of
1630 // zero-sized expressions. Such assignments don't really mean
1631 // anything anyhow.
1632 if (TREE_TYPE (lhs) == void_type_node
1633 || int_size_in_bytes (TREE_TYPE (lhs)) == 0
1634 || TREE_TYPE (rhs) == void_type_node
1635 || int_size_in_bytes (TREE_TYPE (rhs)) == 0)
1636 return compound_statement (lhs, rhs);
1637
1638 rhs = convert_tree (TREE_TYPE (lhs), rhs, location);
1639
1640 return fold_build2_loc (location, MODIFY_EXPR, void_type_node, lhs, rhs);
1641 }
1642
1643 // Return.
1644
1645 tree
1646 return_statement (tree fntree, tree val, location_t location)
1647 {
1648 if (error_operand_p (fntree))
1649 return error_mark_node;
1650
1651 tree result = DECL_RESULT (fntree);
1652 if (error_operand_p (result))
1653 return error_mark_node;
1654
1655 if (error_operand_p (val))
1656 return error_mark_node;
1657
1658 tree set
1659 = fold_build2_loc (location, MODIFY_EXPR, void_type_node, result, val);
1660 return fold_build1_loc (location, RETURN_EXPR, void_type_node, set);
1661 }
1662
1663 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
1664 // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
1665 // NULL, it will always be executed. This is used for handling defers in Rust
1666 // functions. In C++, the resulting code is of this form:
1667 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
1668
1669 tree
1670 exception_handler_statement (tree try_stmt, tree except_stmt, tree finally_stmt,
1671 location_t location)
1672 {
1673 if (error_operand_p (try_stmt) || error_operand_p (except_stmt)
1674 || error_operand_p (finally_stmt))
1675 return error_mark_node;
1676
1677 if (except_stmt != NULL_TREE)
1678 try_stmt = build2_loc (location, TRY_CATCH_EXPR, void_type_node, try_stmt,
1679 build2_loc (location, CATCH_EXPR, void_type_node,
1680 NULL, except_stmt));
1681 if (finally_stmt != NULL_TREE)
1682 try_stmt = build2_loc (location, TRY_FINALLY_EXPR, void_type_node, try_stmt,
1683 finally_stmt);
1684 return try_stmt;
1685 }
1686
1687 // If.
1688
1689 tree
1690 if_statement (tree, tree cond_tree, tree then_tree, tree else_tree,
1691 location_t location)
1692 {
1693 if (error_operand_p (cond_tree) || error_operand_p (then_tree)
1694 || error_operand_p (else_tree))
1695 return error_mark_node;
1696 tree ret = build3_loc (location, COND_EXPR, void_type_node, cond_tree,
1697 then_tree, else_tree);
1698 return ret;
1699 }
1700
1701 // Loops
1702
1703 tree
1704 loop_expression (tree body, location_t locus)
1705 {
1706 return fold_build1_loc (locus, LOOP_EXPR, void_type_node, body);
1707 }
1708
1709 tree
1710 exit_expression (tree cond_tree, location_t locus)
1711 {
1712 return fold_build1_loc (locus, EXIT_EXPR, void_type_node, cond_tree);
1713 }
1714
1715 // Pair of statements.
1716
1717 tree
1718 compound_statement (tree s1, tree s2)
1719 {
1720 if (error_operand_p (s1) || error_operand_p (s2))
1721 return error_mark_node;
1722
1723 tree stmt_list = NULL_TREE;
1724 append_to_statement_list (s1, &stmt_list);
1725 append_to_statement_list (s2, &stmt_list);
1726
1727 // If neither statement has any side effects, stmt_list can be NULL
1728 // at this point.
1729 if (stmt_list == NULL_TREE)
1730 stmt_list = integer_zero_node;
1731
1732 return stmt_list;
1733 }
1734
1735 // List of statements.
1736
1737 tree
1738 statement_list (const std::vector<tree> &statements)
1739 {
1740 tree stmt_list = NULL_TREE;
1741 for (tree t : statements)
1742 {
1743 if (error_operand_p (t))
1744 return error_mark_node;
1745 append_to_statement_list (t, &stmt_list);
1746 }
1747 return stmt_list;
1748 }
1749
1750 // Make a block. For some reason gcc uses a dual structure for
1751 // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1752 // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1753 // the Bblock.
1754
1755 tree
1756 block (tree fndecl, tree enclosing, const std::vector<Bvariable *> &vars,
1757 location_t start_location, location_t)
1758 {
1759 tree block_tree = make_node (BLOCK);
1760 if (enclosing == NULL)
1761 {
1762 gcc_assert (fndecl != NULL_TREE);
1763
1764 // We may have already created a block for local variables when
1765 // we take the address of a parameter.
1766 if (DECL_INITIAL (fndecl) == NULL_TREE)
1767 {
1768 BLOCK_SUPERCONTEXT (block_tree) = fndecl;
1769 DECL_INITIAL (fndecl) = block_tree;
1770 }
1771 else
1772 {
1773 tree superblock_tree = DECL_INITIAL (fndecl);
1774 BLOCK_SUPERCONTEXT (block_tree) = superblock_tree;
1775 tree *pp;
1776 for (pp = &BLOCK_SUBBLOCKS (superblock_tree); *pp != NULL_TREE;
1777 pp = &BLOCK_CHAIN (*pp))
1778 ;
1779 *pp = block_tree;
1780 }
1781 }
1782 else
1783 {
1784 tree superblock_tree = BIND_EXPR_BLOCK (enclosing);
1785 gcc_assert (TREE_CODE (superblock_tree) == BLOCK);
1786
1787 BLOCK_SUPERCONTEXT (block_tree) = superblock_tree;
1788 tree *pp;
1789 for (pp = &BLOCK_SUBBLOCKS (superblock_tree); *pp != NULL_TREE;
1790 pp = &BLOCK_CHAIN (*pp))
1791 ;
1792 *pp = block_tree;
1793 }
1794
1795 // Chain the variables of the scope together so they are all connected
1796 // to the block.
1797 tree *pp = &BLOCK_VARS (block_tree);
1798 for (Bvariable *bv : vars)
1799 {
1800 *pp = bv->get_decl ();
1801 if (!error_operand_p (*pp))
1802 pp = &DECL_CHAIN (*pp);
1803 }
1804 *pp = NULL_TREE;
1805
1806 TREE_USED (block_tree) = 1;
1807
1808 tree bind_tree = build3_loc (start_location, BIND_EXPR, void_type_node,
1809 BLOCK_VARS (block_tree), NULL_TREE, block_tree);
1810 TREE_SIDE_EFFECTS (bind_tree) = 1;
1811 return bind_tree;
1812 }
1813
1814 // Add statements to a block.
1815
1816 void
1817 block_add_statements (tree bind_tree, const std::vector<tree> &statements)
1818 {
1819 tree stmt_list = NULL_TREE;
1820 for (tree s : statements)
1821 {
1822 if (!error_operand_p (s))
1823 append_to_statement_list (s, &stmt_list);
1824 }
1825
1826 gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
1827 BIND_EXPR_BODY (bind_tree) = stmt_list;
1828 }
1829
1830 // This is not static because we declare it with GTY(()) in rust-c.h.
1831 tree rust_non_zero_struct;
1832
1833 // Return a type corresponding to TYPE with non-zero size.
1834
1835 tree
1836 non_zero_size_type (tree type)
1837 {
1838 if (int_size_in_bytes (type) != 0)
1839 return type;
1840
1841 switch (TREE_CODE (type))
1842 {
1843 case RECORD_TYPE:
1844 if (TYPE_FIELDS (type) != NULL_TREE)
1845 {
1846 tree ns = make_node (RECORD_TYPE);
1847 tree field_trees = NULL_TREE;
1848 tree *pp = &field_trees;
1849 for (tree field = TYPE_FIELDS (type); field != NULL_TREE;
1850 field = DECL_CHAIN (field))
1851 {
1852 tree ft = TREE_TYPE (field);
1853 if (field == TYPE_FIELDS (type))
1854 ft = non_zero_size_type (ft);
1855 tree f = build_decl (DECL_SOURCE_LOCATION (field), FIELD_DECL,
1856 DECL_NAME (field), ft);
1857 DECL_CONTEXT (f) = ns;
1858 *pp = f;
1859 pp = &DECL_CHAIN (f);
1860 }
1861 TYPE_FIELDS (ns) = field_trees;
1862 layout_type (ns);
1863 return ns;
1864 }
1865
1866 if (rust_non_zero_struct == NULL_TREE)
1867 {
1868 type = make_node (RECORD_TYPE);
1869 tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1870 get_identifier ("dummy"), boolean_type_node);
1871 DECL_CONTEXT (field) = type;
1872 TYPE_FIELDS (type) = field;
1873 layout_type (type);
1874 rust_non_zero_struct = type;
1875 }
1876 return rust_non_zero_struct;
1877
1878 case ARRAY_TYPE: {
1879 tree element_type = non_zero_size_type (TREE_TYPE (type));
1880 return build_array_type_nelts (element_type, 1);
1881 }
1882
1883 default:
1884 rust_unreachable ();
1885 }
1886
1887 rust_unreachable ();
1888 }
1889
1890 // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Rust type
1891 // can be created multiple times and thus have multiple tree
1892 // representations. Make sure this does not confuse the middle-end.
1893
1894 tree
1895 convert_tree (tree type_tree, tree expr_tree, location_t location)
1896 {
1897 if (type_tree == TREE_TYPE (expr_tree))
1898 return expr_tree;
1899
1900 if (error_operand_p (type_tree) || error_operand_p (expr_tree))
1901 return error_mark_node;
1902
1903 if (POINTER_TYPE_P (type_tree) || INTEGRAL_TYPE_P (type_tree)
1904 || SCALAR_FLOAT_TYPE_P (type_tree) || COMPLEX_FLOAT_TYPE_P (type_tree))
1905 return fold_convert_loc (location, type_tree, expr_tree);
1906 else if (TREE_CODE (type_tree) == RECORD_TYPE
1907 || TREE_CODE (type_tree) == UNION_TYPE
1908 || TREE_CODE (type_tree) == ARRAY_TYPE)
1909 {
1910 gcc_assert (int_size_in_bytes (type_tree)
1911 == int_size_in_bytes (TREE_TYPE (expr_tree)));
1912 if (TYPE_MAIN_VARIANT (type_tree)
1913 == TYPE_MAIN_VARIANT (TREE_TYPE (expr_tree)))
1914 return fold_build1_loc (location, NOP_EXPR, type_tree, expr_tree);
1915 return fold_build1_loc (location, VIEW_CONVERT_EXPR, type_tree,
1916 expr_tree);
1917 }
1918
1919 rust_unreachable ();
1920 }
1921
1922 // Make a global variable.
1923
1924 Bvariable *
1925 global_variable (const std::string &var_name, const std::string &asm_name,
1926 tree type_tree, bool is_external, bool is_hidden,
1927 bool in_unique_section, location_t location)
1928 {
1929 if (error_operand_p (type_tree))
1930 return Bvariable::error_variable ();
1931
1932 // The GNU linker does not like dynamic variables with zero size.
1933 tree orig_type_tree = type_tree;
1934 if ((is_external || !is_hidden) && int_size_in_bytes (type_tree) == 0)
1935 type_tree = non_zero_size_type (type_tree);
1936
1937 tree decl = build_decl (location, VAR_DECL,
1938 get_identifier_from_string (var_name), type_tree);
1939 if (is_external)
1940 DECL_EXTERNAL (decl) = 1;
1941 else
1942 TREE_STATIC (decl) = 1;
1943 if (!is_hidden)
1944 {
1945 TREE_PUBLIC (decl) = 1;
1946 SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
1947 }
1948 else
1949 {
1950 SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
1951 }
1952
1953 TREE_USED (decl) = 1;
1954
1955 if (in_unique_section)
1956 resolve_unique_section (decl, 0, 1);
1957
1958 rust_preserve_from_gc (decl);
1959
1960 return new Bvariable (decl, orig_type_tree);
1961 }
1962
1963 // Set the initial value of a global variable.
1964
1965 void
1966 global_variable_set_init (Bvariable *var, tree expr_tree)
1967 {
1968 if (error_operand_p (expr_tree))
1969 return;
1970 gcc_assert (TREE_CONSTANT (expr_tree));
1971 tree var_decl = var->get_decl ();
1972 if (error_operand_p (var_decl))
1973 return;
1974 DECL_INITIAL (var_decl) = expr_tree;
1975
1976 // If this variable goes in a unique section, it may need to go into
1977 // a different one now that DECL_INITIAL is set.
1978 if (symtab_node::get (var_decl)
1979 && symtab_node::get (var_decl)->implicit_section)
1980 {
1981 set_decl_section_name (var_decl, (const char *) NULL);
1982 resolve_unique_section (var_decl, compute_reloc_for_constant (expr_tree),
1983 1);
1984 }
1985 }
1986
1987 // Make a local variable.
1988
1989 Bvariable *
1990 local_variable (tree function, const std::string &name, tree type_tree,
1991 Bvariable *decl_var, location_t location)
1992 {
1993 if (error_operand_p (type_tree))
1994 return Bvariable::error_variable ();
1995 tree decl = build_decl (location, VAR_DECL, get_identifier_from_string (name),
1996 type_tree);
1997 DECL_CONTEXT (decl) = function;
1998
1999 if (decl_var != NULL)
2000 {
2001 DECL_HAS_VALUE_EXPR_P (decl) = 1;
2002 SET_DECL_VALUE_EXPR (decl, decl_var->get_decl ());
2003 }
2004 rust_preserve_from_gc (decl);
2005 return new Bvariable (decl);
2006 }
2007
2008 // Make a function parameter variable.
2009
2010 Bvariable *
2011 parameter_variable (tree function, const std::string &name, tree type_tree,
2012 location_t location)
2013 {
2014 if (error_operand_p (type_tree))
2015 return Bvariable::error_variable ();
2016 tree decl = build_decl (location, PARM_DECL,
2017 get_identifier_from_string (name), type_tree);
2018 DECL_CONTEXT (decl) = function;
2019 DECL_ARG_TYPE (decl) = type_tree;
2020
2021 rust_preserve_from_gc (decl);
2022 return new Bvariable (decl);
2023 }
2024
2025 // Make a static chain variable.
2026
2027 Bvariable *
2028 static_chain_variable (tree fndecl, const std::string &name, tree type_tree,
2029 location_t location)
2030 {
2031 if (error_operand_p (type_tree))
2032 return Bvariable::error_variable ();
2033 tree decl = build_decl (location, PARM_DECL,
2034 get_identifier_from_string (name), type_tree);
2035 DECL_CONTEXT (decl) = fndecl;
2036 DECL_ARG_TYPE (decl) = type_tree;
2037 TREE_USED (decl) = 1;
2038 DECL_ARTIFICIAL (decl) = 1;
2039 DECL_IGNORED_P (decl) = 1;
2040 TREE_READONLY (decl) = 1;
2041
2042 struct function *f = DECL_STRUCT_FUNCTION (fndecl);
2043 if (f == NULL)
2044 {
2045 push_struct_function (fndecl);
2046 pop_cfun ();
2047 f = DECL_STRUCT_FUNCTION (fndecl);
2048 }
2049 gcc_assert (f->static_chain_decl == NULL);
2050 f->static_chain_decl = decl;
2051 DECL_STATIC_CHAIN (fndecl) = 1;
2052
2053 rust_preserve_from_gc (decl);
2054 return new Bvariable (decl);
2055 }
2056
2057 // Make a temporary variable.
2058
2059 Bvariable *
2060 temporary_variable (tree fndecl, tree bind_tree, tree type_tree, tree init_tree,
2061 bool is_address_taken, location_t location,
2062 tree *pstatement)
2063 {
2064 gcc_assert (fndecl != NULL_TREE);
2065 if (error_operand_p (type_tree) || error_operand_p (init_tree)
2066 || error_operand_p (fndecl))
2067 {
2068 *pstatement = error_mark_node;
2069 return Bvariable::error_variable ();
2070 }
2071
2072 tree var;
2073 // We can only use create_tmp_var if the type is not addressable.
2074 if (!TREE_ADDRESSABLE (type_tree))
2075 {
2076 if (DECL_STRUCT_FUNCTION (fndecl) == NULL)
2077 push_struct_function (fndecl);
2078 else
2079 push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2080
2081 var = create_tmp_var (type_tree, "RUSTTMP");
2082 pop_cfun ();
2083 }
2084 else
2085 {
2086 gcc_assert (bind_tree != NULL_TREE);
2087 var = build_decl (location, VAR_DECL, create_tmp_var_name ("RUSTTMP"),
2088 type_tree);
2089 DECL_ARTIFICIAL (var) = 1;
2090 DECL_IGNORED_P (var) = 1;
2091 TREE_USED (var) = 1;
2092 DECL_CONTEXT (var) = fndecl;
2093
2094 // We have to add this variable to the BLOCK and the BIND_EXPR.
2095 gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
2096 tree block_tree = BIND_EXPR_BLOCK (bind_tree);
2097 gcc_assert (TREE_CODE (block_tree) == BLOCK);
2098 DECL_CHAIN (var) = BLOCK_VARS (block_tree);
2099 BLOCK_VARS (block_tree) = var;
2100 BIND_EXPR_VARS (bind_tree) = BLOCK_VARS (block_tree);
2101 }
2102
2103 if (type_size (type_tree) != 0 && init_tree != NULL_TREE
2104 && TREE_TYPE (init_tree) != void_type_node)
2105 DECL_INITIAL (var) = convert_tree (type_tree, init_tree, location);
2106
2107 if (is_address_taken)
2108 TREE_ADDRESSABLE (var) = 1;
2109
2110 *pstatement = build1_loc (location, DECL_EXPR, void_type_node, var);
2111
2112 // For a zero sized type, don't initialize VAR with BINIT, but still
2113 // evaluate BINIT for its side effects.
2114 if (init_tree != NULL_TREE
2115 && (type_size (type_tree) == 0
2116 || TREE_TYPE (init_tree) == void_type_node))
2117 *pstatement = compound_statement (init_tree, *pstatement);
2118
2119 return new Bvariable (var);
2120 }
2121
2122 // Make a label.
2123
2124 tree
2125 label (tree func_tree, const std::string &name, location_t location)
2126 {
2127 tree decl;
2128 if (name.empty ())
2129 {
2130 if (DECL_STRUCT_FUNCTION (func_tree) == NULL)
2131 push_struct_function (func_tree);
2132 else
2133 push_cfun (DECL_STRUCT_FUNCTION (func_tree));
2134
2135 decl = create_artificial_label (location);
2136
2137 pop_cfun ();
2138 }
2139 else
2140 {
2141 tree id = get_identifier_from_string (name);
2142 decl = build_decl (location, LABEL_DECL, id, void_type_node);
2143 DECL_CONTEXT (decl) = func_tree;
2144 }
2145 return decl;
2146 }
2147
2148 // Make a statement which defines a label.
2149
2150 tree
2151 label_definition_statement (tree label)
2152 {
2153 return fold_build1_loc (DECL_SOURCE_LOCATION (label), LABEL_EXPR,
2154 void_type_node, label);
2155 }
2156
2157 // Make a goto statement.
2158
2159 tree
2160 goto_statement (tree label, location_t location)
2161 {
2162 return fold_build1_loc (location, GOTO_EXPR, void_type_node, label);
2163 }
2164
2165 // Get the address of a label.
2166
2167 tree
2168 label_address (tree label, location_t location)
2169 {
2170 TREE_USED (label) = 1;
2171 TREE_ADDRESSABLE (label) = 1;
2172 tree ret = fold_convert_loc (location, ptr_type_node,
2173 build_fold_addr_expr_loc (location, label));
2174 return ret;
2175 }
2176
2177 // Declare or define a new function.
2178
2179 tree
2180 function (tree functype, const std::string &name, const std::string &asm_name,
2181 unsigned int flags, location_t location)
2182 {
2183 if (error_operand_p (functype))
2184 return error_mark_node;
2185
2186 gcc_assert (FUNCTION_POINTER_TYPE_P (functype));
2187 functype = TREE_TYPE (functype);
2188 tree id = get_identifier_from_string (name);
2189 if (error_operand_p (id))
2190 return error_mark_node;
2191
2192 tree decl = build_decl (location, FUNCTION_DECL, id, functype);
2193 if (!asm_name.empty ())
2194 SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
2195
2196 if ((flags & function_is_declaration) != 0)
2197 DECL_EXTERNAL (decl) = 1;
2198 else
2199 {
2200 tree restype = TREE_TYPE (functype);
2201 tree resdecl = build_decl (location, RESULT_DECL, NULL_TREE, restype);
2202 DECL_ARTIFICIAL (resdecl) = 1;
2203 DECL_IGNORED_P (resdecl) = 1;
2204 DECL_CONTEXT (resdecl) = decl;
2205 DECL_RESULT (decl) = resdecl;
2206 }
2207 if ((flags & function_is_uninlinable) != 0)
2208 DECL_UNINLINABLE (decl) = 1;
2209 if ((flags & function_does_not_return) != 0)
2210 TREE_THIS_VOLATILE (decl) = 1;
2211 if ((flags & function_in_unique_section) != 0)
2212 resolve_unique_section (decl, 0, 1);
2213
2214 rust_preserve_from_gc (decl);
2215 return decl;
2216 }
2217
2218 // Create a statement that runs all deferred calls for FUNCTION. This should
2219 // be a statement that looks like this in C++:
2220 // finish:
2221 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2222
2223 tree
2224 function_defer_statement (tree function, tree undefer_tree, tree defer_tree,
2225 location_t location)
2226 {
2227 if (error_operand_p (undefer_tree) || error_operand_p (defer_tree)
2228 || error_operand_p (function))
2229 return error_mark_node;
2230
2231 if (DECL_STRUCT_FUNCTION (function) == NULL)
2232 push_struct_function (function);
2233 else
2234 push_cfun (DECL_STRUCT_FUNCTION (function));
2235
2236 tree stmt_list = NULL;
2237 tree label = Backend::label (function, "", location);
2238 tree label_def = label_definition_statement (label);
2239 append_to_statement_list (label_def, &stmt_list);
2240
2241 tree jump_stmt = goto_statement (label, location);
2242 tree catch_body
2243 = build2 (COMPOUND_EXPR, void_type_node, defer_tree, jump_stmt);
2244 catch_body = build2 (CATCH_EXPR, void_type_node, NULL, catch_body);
2245 tree try_catch
2246 = build2 (TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2247 append_to_statement_list (try_catch, &stmt_list);
2248 pop_cfun ();
2249
2250 return stmt_list;
2251 }
2252
2253 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2254 // This will only be called for a function definition.
2255
2256 bool
2257 function_set_parameters (tree function,
2258 const std::vector<Bvariable *> &param_vars)
2259 {
2260 if (error_operand_p (function))
2261 return false;
2262
2263 tree params = NULL_TREE;
2264 tree *pp = &params;
2265 for (Bvariable *bv : param_vars)
2266 {
2267 *pp = bv->get_decl ();
2268 gcc_assert (!error_operand_p (*pp));
2269 pp = &DECL_CHAIN (*pp);
2270 }
2271 *pp = NULL_TREE;
2272 DECL_ARGUMENTS (function) = params;
2273 return true;
2274 }
2275
2276 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2277 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
2278 // emit early debugging information.
2279
2280 void
2281 write_global_definitions (const std::vector<tree> &type_decls,
2282 const std::vector<tree> &constant_decls,
2283 const std::vector<tree> &function_decls,
2284 const std::vector<Bvariable *> &variable_decls)
2285 {
2286 size_t count_definitions = type_decls.size () + constant_decls.size ()
2287 + function_decls.size () + variable_decls.size ();
2288
2289 tree *defs = new tree[count_definitions];
2290
2291 // Convert all non-erroneous declarations into Gimple form.
2292 size_t i = 0;
2293 for (Bvariable *bv : variable_decls)
2294 {
2295 tree v = bv->get_decl ();
2296 if (error_operand_p (v))
2297 continue;
2298 defs[i] = v;
2299 rust_preserve_from_gc (defs[i]);
2300 ++i;
2301 }
2302
2303 for (tree type_tree : type_decls)
2304 {
2305 if (!error_operand_p (type_tree) && IS_TYPE_OR_DECL_P (type_tree))
2306 {
2307 defs[i] = TYPE_NAME (type_tree);
2308 gcc_assert (defs[i] != NULL);
2309 rust_preserve_from_gc (defs[i]);
2310 ++i;
2311 }
2312 }
2313 for (tree t : constant_decls)
2314 {
2315 if (!error_operand_p (t))
2316 {
2317 defs[i] = t;
2318 rust_preserve_from_gc (defs[i]);
2319 ++i;
2320 }
2321 }
2322 for (tree decl : function_decls)
2323 {
2324 if (!error_operand_p (decl))
2325 {
2326 rust_preserve_from_gc (decl);
2327 if (DECL_STRUCT_FUNCTION (decl) == NULL)
2328 allocate_struct_function (decl, false);
2329 dump_function (TDI_original, decl);
2330 cgraph_node::finalize_function (decl, true);
2331
2332 defs[i] = decl;
2333 ++i;
2334 }
2335 }
2336
2337 // Pass everything back to the middle-end.
2338
2339 wrapup_global_declarations (defs, i);
2340
2341 delete[] defs;
2342 }
2343
2344 } // namespace Backend