Branch data Line data Source code
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 : 44875 : Bvariable::get_tree (location_t location) const
63 : : {
64 : 44875 : if (error_operand_p (this->t_))
65 : 0 : return error_mark_node;
66 : :
67 : 44875 : TREE_USED (this->t_) = 1;
68 : 44875 : if (this->orig_type_ == NULL || TREE_TYPE (this->t_) == this->orig_type_)
69 : : {
70 : 44869 : return this->t_;
71 : : }
72 : :
73 : : // Return *(orig_type*)&decl. */
74 : 6 : tree t = build_fold_addr_expr_loc (location, this->t_);
75 : 6 : t = fold_build1_loc (location, NOP_EXPR,
76 : 6 : build_pointer_type (this->orig_type_), t);
77 : 6 : return build_fold_indirect_ref_loc (location, t);
78 : : }
79 : :
80 : : Bvariable *
81 : 8211 : Bvariable::error_variable ()
82 : : {
83 : 8211 : 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 : 339346 : get_identifier_from_string (const std::string &str)
96 : : {
97 : 339346 : 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 : 5120 : 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 : 5120 : }
326 : :
327 : : void
328 : 0 : debug (tree t)
329 : : {
330 : 0 : debug_tree (t);
331 : 0 : };
332 : :
333 : : void
334 : 0 : debug (Bvariable *t)
335 : : {
336 : 0 : debug_tree (t->get_decl ());
337 : 0 : };
338 : :
339 : : tree
340 : 12036 : get_identifier_node (const std::string &str)
341 : : {
342 : 12036 : return get_identifier_with_length (str.data (), str.length ());
343 : : }
344 : :
345 : : tree
346 : 6092 : wchar_type ()
347 : : {
348 : 6092 : static tree wchar;
349 : :
350 : 6092 : if (wchar == NULL_TREE)
351 : : {
352 : 4254 : wchar = make_unsigned_type (32);
353 : 4254 : TYPE_STRING_FLAG (wchar) = 1;
354 : : }
355 : :
356 : 6092 : return wchar;
357 : : }
358 : :
359 : : // Get an unnamed integer type.
360 : :
361 : : int
362 : 52922 : get_pointer_size ()
363 : : {
364 : 52922 : return POINTER_SIZE;
365 : : }
366 : :
367 : : tree
368 : 0 : raw_str_type ()
369 : : {
370 : 0 : tree char_ptr = build_pointer_type (char_type_node);
371 : 0 : tree const_char_type = build_qualified_type (char_ptr, TYPE_QUAL_CONST);
372 : 0 : return const_char_type;
373 : : }
374 : :
375 : : tree
376 : 170971 : integer_type (bool is_unsigned, int bits)
377 : : {
378 : 170971 : tree type;
379 : 170971 : if (is_unsigned)
380 : : {
381 : 96019 : if (bits == INT_TYPE_SIZE)
382 : 13134 : type = unsigned_type_node;
383 : 82885 : else if (bits == SHORT_TYPE_SIZE)
384 : 9247 : type = short_unsigned_type_node;
385 : 73638 : else if (bits == LONG_TYPE_SIZE)
386 : 53538 : type = long_unsigned_type_node;
387 : 20100 : else if (bits == LONG_LONG_TYPE_SIZE)
388 : 0 : type = long_long_unsigned_type_node;
389 : : else
390 : 20100 : type = make_unsigned_type (bits);
391 : : }
392 : : else
393 : : {
394 : 74952 : if (bits == INT_TYPE_SIZE)
395 : 38129 : type = integer_type_node;
396 : 36823 : else if (bits == SHORT_TYPE_SIZE)
397 : 5631 : type = short_integer_type_node;
398 : 31192 : else if (bits == LONG_TYPE_SIZE)
399 : 16351 : type = long_integer_type_node;
400 : 14841 : else if (bits == LONG_LONG_TYPE_SIZE)
401 : 0 : type = long_long_integer_type_node;
402 : : else
403 : 14841 : type = make_signed_type (bits);
404 : : }
405 : 170971 : return type;
406 : : }
407 : :
408 : : // Get an unnamed float type.
409 : :
410 : : tree
411 : 16134 : float_type (int bits)
412 : : {
413 : 16134 : tree type;
414 : 16134 : if (bits == TYPE_PRECISION (float_type_node))
415 : : type = float_type_node;
416 : 8239 : else if (bits == TYPE_PRECISION (double_type_node))
417 : : type = double_type_node;
418 : 0 : else if (bits == TYPE_PRECISION (long_double_type_node))
419 : : type = long_double_type_node;
420 : : else
421 : : {
422 : 0 : type = make_node (REAL_TYPE);
423 : 0 : TYPE_PRECISION (type) = bits;
424 : 0 : layout_type (type);
425 : : }
426 : 16134 : return type;
427 : : }
428 : :
429 : : // Get a pointer type.
430 : :
431 : : tree
432 : 6942 : pointer_type (tree to_type)
433 : : {
434 : 6942 : if (error_operand_p (to_type))
435 : 0 : return error_mark_node;
436 : 6942 : tree type = build_pointer_type (to_type);
437 : 6942 : return type;
438 : : }
439 : :
440 : : // Get a reference type.
441 : :
442 : : tree
443 : 7062 : reference_type (tree to_type)
444 : : {
445 : 7062 : if (error_operand_p (to_type))
446 : 0 : return error_mark_node;
447 : 7062 : tree type = build_reference_type (to_type);
448 : 7062 : return type;
449 : : }
450 : :
451 : : // Get immutable type
452 : :
453 : : tree
454 : 34379 : immutable_type (tree base)
455 : : {
456 : 34379 : if (error_operand_p (base))
457 : 22 : return error_mark_node;
458 : 34357 : tree constified = build_qualified_type (base, TYPE_QUAL_CONST);
459 : 34357 : return constified;
460 : : }
461 : :
462 : : // Make a function type.
463 : :
464 : : tree
465 : 20209 : function_type (const typed_identifier &receiver,
466 : : const std::vector<typed_identifier> ¶meters,
467 : : const std::vector<typed_identifier> &results, tree result_struct,
468 : : location_t)
469 : : {
470 : 20209 : tree args = NULL_TREE;
471 : 20209 : tree *pp = &args;
472 : 20209 : if (receiver.type != NULL_TREE)
473 : : {
474 : 0 : tree t = receiver.type;
475 : 0 : if (error_operand_p (t))
476 : 0 : return error_mark_node;
477 : 0 : *pp = tree_cons (NULL_TREE, t, NULL_TREE);
478 : 0 : pp = &TREE_CHAIN (*pp);
479 : : }
480 : :
481 : 32907 : for (const auto &p : parameters)
482 : : {
483 : 12698 : tree t = p.type;
484 : 12698 : if (error_operand_p (t))
485 : 0 : return error_mark_node;
486 : 12698 : *pp = tree_cons (NULL_TREE, t, NULL_TREE);
487 : 12698 : 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 : 20209 : *pp = void_list_node;
493 : :
494 : 20209 : tree result;
495 : 20209 : if (results.empty ())
496 : 88 : result = void_type_node;
497 : 20121 : else if (results.size () == 1)
498 : 20121 : result = results.front ().type;
499 : : else
500 : : {
501 : 0 : gcc_assert (result_struct != NULL);
502 : : result = result_struct;
503 : : }
504 : 20209 : if (error_operand_p (result))
505 : 0 : return error_mark_node;
506 : :
507 : 20209 : tree fntype = build_function_type (result, args);
508 : 20209 : if (error_operand_p (fntype))
509 : 0 : return error_mark_node;
510 : :
511 : 20209 : return build_pointer_type (fntype);
512 : : }
513 : :
514 : : tree
515 : 816 : function_type_variadic (const typed_identifier &receiver,
516 : : const std::vector<typed_identifier> ¶meters,
517 : : const std::vector<typed_identifier> &results,
518 : : tree result_struct, location_t)
519 : : {
520 : 816 : size_t n = parameters.size () + (receiver.type != NULL_TREE ? 1 : 0);
521 : 816 : tree *args = XALLOCAVEC (tree, n);
522 : 816 : size_t offs = 0;
523 : 816 : if (error_operand_p (receiver.type))
524 : 0 : return error_mark_node;
525 : :
526 : 816 : if (receiver.type != NULL_TREE)
527 : 0 : args[offs++] = receiver.type;
528 : :
529 : 1632 : for (const auto &p : parameters)
530 : : {
531 : 816 : tree t = p.type;
532 : 816 : if (error_operand_p (t))
533 : 0 : return error_mark_node;
534 : 816 : args[offs++] = t;
535 : : }
536 : :
537 : 816 : tree result;
538 : 816 : if (results.empty ())
539 : 795 : result = void_type_node;
540 : 21 : else if (results.size () == 1)
541 : 21 : result = results.front ().type;
542 : : else
543 : : {
544 : 0 : gcc_assert (result_struct != NULL_TREE);
545 : : result = result_struct;
546 : : }
547 : 816 : if (error_operand_p (result))
548 : 0 : return error_mark_node;
549 : :
550 : 816 : tree fntype = build_varargs_function_type_array (result, n, args);
551 : 816 : if (error_operand_p (fntype))
552 : 0 : return error_mark_node;
553 : :
554 : 816 : return build_pointer_type (fntype);
555 : : }
556 : :
557 : : tree
558 : 88 : function_ptr_type (tree result_type, const std::vector<tree> ¶meters,
559 : : location_t /* locus */)
560 : : {
561 : 88 : tree args = NULL_TREE;
562 : 88 : tree *pp = &args;
563 : :
564 : 184 : for (auto ¶m : parameters)
565 : : {
566 : 96 : if (error_operand_p (param))
567 : 0 : return error_mark_node;
568 : :
569 : 96 : *pp = tree_cons (NULL_TREE, param, NULL_TREE);
570 : 96 : pp = &TREE_CHAIN (*pp);
571 : : }
572 : :
573 : 88 : *pp = void_list_node;
574 : :
575 : 88 : tree result = result_type;
576 : 88 : if (result != void_type_node && int_size_in_bytes (result) == 0)
577 : 40 : result = void_type_node;
578 : :
579 : 88 : tree fntype = build_function_type (result, args);
580 : 88 : if (error_operand_p (fntype))
581 : 0 : return error_mark_node;
582 : :
583 : 88 : return build_pointer_type (fntype);
584 : : }
585 : :
586 : : // Make a struct type.
587 : :
588 : : tree
589 : 36322 : struct_type (const std::vector<typed_identifier> &fields, bool layout)
590 : : {
591 : 36322 : return fill_in_fields (make_node (RECORD_TYPE), fields, layout);
592 : : }
593 : :
594 : : // Make a union type.
595 : :
596 : : tree
597 : 2627 : union_type (const std::vector<typed_identifier> &fields, bool layout)
598 : : {
599 : 2627 : 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 : 38949 : fill_in_fields (tree fill, const std::vector<typed_identifier> &fields,
606 : : bool layout)
607 : : {
608 : 38949 : tree field_trees = NULL_TREE;
609 : 38949 : tree *pp = &field_trees;
610 : 99564 : for (const auto &p : fields)
611 : : {
612 : 60659 : tree name_tree = get_identifier_from_string (p.name);
613 : 60659 : tree type_tree = p.type;
614 : 60659 : if (error_operand_p (type_tree))
615 : 44 : return error_mark_node;
616 : 60615 : tree field = build_decl (p.location, FIELD_DECL, name_tree, type_tree);
617 : 60615 : DECL_CONTEXT (field) = fill;
618 : 60615 : *pp = field;
619 : 60615 : pp = &DECL_CHAIN (field);
620 : : }
621 : 38905 : TYPE_FIELDS (fill) = field_trees;
622 : :
623 : 38905 : if (layout)
624 : 24310 : 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 : 38905 : SET_TYPE_STRUCTURAL_EQUALITY (fill);
631 : :
632 : 38905 : return fill;
633 : : }
634 : :
635 : : // Make an array type.
636 : :
637 : : tree
638 : 4528 : array_type (tree element_type, tree length)
639 : : {
640 : 4528 : return fill_in_array (make_node (ARRAY_TYPE), element_type, length);
641 : : }
642 : :
643 : : // Fill in an array type.
644 : :
645 : : tree
646 : 4528 : fill_in_array (tree fill, tree element_type, tree length_tree)
647 : : {
648 : 4528 : if (error_operand_p (element_type) || error_operand_p (length_tree))
649 : 66 : return error_mark_node;
650 : :
651 : 4462 : gcc_assert (TYPE_SIZE (element_type) != NULL_TREE);
652 : :
653 : 4462 : 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 : 4462 : tree index_type_tree = build_index_type (
658 : : fold_build2 (MINUS_EXPR, sizetype, length_tree, size_one_node));
659 : :
660 : 4462 : TREE_TYPE (fill) = element_type;
661 : 4462 : TYPE_DOMAIN (fill) = index_type_tree;
662 : 4462 : TYPE_ADDR_SPACE (fill) = TYPE_ADDR_SPACE (element_type);
663 : 4462 : layout_type (fill);
664 : :
665 : 4462 : if (TYPE_STRUCTURAL_EQUALITY_P (element_type))
666 : 48 : SET_TYPE_STRUCTURAL_EQUALITY (fill);
667 : 4414 : else if (TYPE_CANONICAL (element_type) != element_type
668 : 4414 : || TYPE_CANONICAL (index_type_tree) != index_type_tree)
669 : 66 : TYPE_CANONICAL (fill) = build_array_type (TYPE_CANONICAL (element_type),
670 : 66 : TYPE_CANONICAL (index_type_tree));
671 : :
672 : : return fill;
673 : : }
674 : :
675 : : // Return a named version of a type.
676 : :
677 : : tree
678 : 240742 : named_type (const std::string &name, tree type, location_t location)
679 : : {
680 : 240742 : if (error_operand_p (type))
681 : 44 : 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 : 357084 : if (TYPE_NAME (type) == NULL_TREE && location == BUILTINS_LOCATION
687 : 328579 : && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == REAL_TYPE
688 : : || TREE_CODE (type) == COMPLEX_TYPE
689 : : || TREE_CODE (type) == BOOLEAN_TYPE))
690 : : {
691 : 154962 : tree decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
692 : : get_identifier_from_string (name), type);
693 : 77481 : TYPE_NAME (type) = decl;
694 : 77481 : return type;
695 : : }
696 : :
697 : 163217 : tree copy = build_variant_type_copy (type);
698 : 163217 : tree decl
699 : 163217 : = build_decl (location, TYPE_DECL, get_identifier_from_string (name), copy);
700 : 163217 : DECL_ORIGINAL_TYPE (decl) = type;
701 : 163217 : TYPE_NAME (copy) = decl;
702 : 163217 : return copy;
703 : : }
704 : :
705 : : // Return the size of a type.
706 : :
707 : : int64_t
708 : 34016 : type_size (tree t)
709 : : {
710 : 34016 : if (error_operand_p (t))
711 : : return 1;
712 : 34016 : if (t == void_type_node)
713 : : return 0;
714 : 34016 : t = TYPE_SIZE_UNIT (t);
715 : 34016 : gcc_assert (tree_fits_uhwi_p (t));
716 : 34016 : unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW (t);
717 : 34016 : int64_t ret = static_cast<int64_t> (val_wide);
718 : 34016 : 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 : 0 : type_alignment (tree t)
727 : : {
728 : 0 : if (error_operand_p (t))
729 : : return 1;
730 : 0 : return TYPE_ALIGN_UNIT (t);
731 : : }
732 : :
733 : : // Return the alignment of a struct field of type BTYPE.
734 : :
735 : : int64_t
736 : 0 : type_field_alignment (tree t)
737 : : {
738 : 0 : if (error_operand_p (t))
739 : : return 1;
740 : 0 : return rust_field_alignment (t);
741 : : }
742 : :
743 : : // Return the offset of a field in a struct.
744 : :
745 : : int64_t
746 : 0 : type_field_offset (tree struct_tree, size_t index)
747 : : {
748 : 0 : if (error_operand_p (struct_tree))
749 : : return 0;
750 : 0 : gcc_assert (TREE_CODE (struct_tree) == RECORD_TYPE);
751 : 0 : tree field = TYPE_FIELDS (struct_tree);
752 : 0 : for (; index > 0; --index)
753 : : {
754 : 0 : field = DECL_CHAIN (field);
755 : 0 : gcc_assert (field != NULL_TREE);
756 : : }
757 : 0 : HOST_WIDE_INT offset_wide = int_byte_position (field);
758 : 0 : int64_t ret = static_cast<int64_t> (offset_wide);
759 : 0 : gcc_assert (ret == offset_wide);
760 : 0 : return ret;
761 : : }
762 : :
763 : : // Return the zero value for a type.
764 : :
765 : : tree
766 : 111 : zero_expression (tree t)
767 : : {
768 : 111 : tree ret;
769 : 111 : if (error_operand_p (t))
770 : 0 : ret = error_mark_node;
771 : : else
772 : 111 : ret = build_zero_cst (t);
773 : 111 : return ret;
774 : : }
775 : :
776 : : // An expression that references a variable.
777 : :
778 : : tree
779 : 37735 : var_expression (Bvariable *var, location_t location)
780 : : {
781 : 37735 : return var->get_tree (location);
782 : : }
783 : :
784 : : // Return a typed value as a constant floating-point number.
785 : :
786 : : tree
787 : 0 : float_constant_expression (tree t, mpfr_t val)
788 : : {
789 : 0 : tree ret;
790 : 0 : if (error_operand_p (t))
791 : 0 : return error_mark_node;
792 : :
793 : 0 : REAL_VALUE_TYPE r1;
794 : 0 : real_from_mpfr (&r1, val, t, GMP_RNDN);
795 : 0 : REAL_VALUE_TYPE r2;
796 : 0 : real_convert (&r2, TYPE_MODE (t), &r1);
797 : 0 : ret = build_real (t, r2);
798 : 0 : return ret;
799 : : }
800 : :
801 : : // Make a constant string expression.
802 : :
803 : : tree
804 : 1597 : string_constant_expression (const std::string &val)
805 : : {
806 : 1597 : tree index_type = build_index_type (size_int (val.length ()));
807 : 1597 : tree const_char_type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
808 : 1597 : tree string_type = build_array_type (const_char_type, index_type);
809 : 1597 : TYPE_STRING_FLAG (string_type) = 1;
810 : 1597 : tree string_val = build_string (val.length (), val.data ());
811 : 1597 : TREE_TYPE (string_val) = string_type;
812 : :
813 : 1597 : return string_val;
814 : : }
815 : :
816 : : tree
817 : 193 : wchar_constant_expression (wchar_t c)
818 : : {
819 : 193 : return build_int_cst (wchar_type (), c);
820 : : }
821 : :
822 : : tree
823 : 210 : char_constant_expression (char c)
824 : : {
825 : 210 : return build_int_cst (char_type_node, c);
826 : : }
827 : :
828 : : // Make a constant boolean expression.
829 : :
830 : : tree
831 : 3289 : boolean_constant_expression (bool val)
832 : : {
833 : 3289 : 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 : 0 : convert_expression (tree type_tree, tree expr_tree, location_t location)
840 : : {
841 : 0 : if (error_operand_p (type_tree) || error_operand_p (expr_tree))
842 : 0 : return error_mark_node;
843 : :
844 : 0 : tree ret;
845 : 0 : 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 : 0 : else if (TREE_CODE (type_tree) == INTEGER_TYPE)
851 : 0 : ret = convert_to_integer (type_tree, expr_tree);
852 : 0 : else if (TREE_CODE (type_tree) == REAL_TYPE)
853 : 0 : ret = convert_to_real (type_tree, expr_tree);
854 : 0 : else if (TREE_CODE (type_tree) == COMPLEX_TYPE)
855 : 0 : ret = convert_to_complex (type_tree, expr_tree);
856 : 0 : else if (TREE_CODE (type_tree) == POINTER_TYPE
857 : 0 : && TREE_CODE (TREE_TYPE (expr_tree)) == INTEGER_TYPE)
858 : 0 : ret = convert_to_pointer (type_tree, expr_tree);
859 : 0 : else if (TREE_CODE (type_tree) == RECORD_TYPE
860 : 0 : || TREE_CODE (type_tree) == ARRAY_TYPE)
861 : 0 : ret = fold_build1_loc (location, VIEW_CONVERT_EXPR, type_tree, expr_tree);
862 : : else
863 : 0 : 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 : 8082 : struct_field_expression (tree struct_tree, size_t index, location_t location)
872 : : {
873 : 8082 : if (error_operand_p (struct_tree))
874 : 0 : return error_mark_node;
875 : 8082 : gcc_assert (TREE_CODE (TREE_TYPE (struct_tree)) == RECORD_TYPE
876 : : || TREE_CODE (TREE_TYPE (struct_tree)) == UNION_TYPE);
877 : 8082 : tree field = TYPE_FIELDS (TREE_TYPE (struct_tree));
878 : 8082 : 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 : 0 : return error_mark_node;
883 : : }
884 : 25288 : for (unsigned int i = index; i > 0; --i)
885 : : {
886 : 17206 : field = DECL_CHAIN (field);
887 : 17206 : gcc_assert (field != NULL_TREE);
888 : : }
889 : 8082 : if (error_operand_p (TREE_TYPE (field)))
890 : 0 : return error_mark_node;
891 : 8082 : tree ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (field),
892 : : struct_tree, field, NULL_TREE);
893 : 8082 : if (TREE_CONSTANT (struct_tree))
894 : 308 : TREE_CONSTANT (ret) = 1;
895 : : return ret;
896 : : }
897 : :
898 : : // Return an expression that executes BSTAT before BEXPR.
899 : :
900 : : tree
901 : 111 : compound_expression (tree stat, tree expr, location_t location)
902 : : {
903 : 111 : if (error_operand_p (stat) || error_operand_p (expr))
904 : 0 : return error_mark_node;
905 : 111 : tree ret
906 : 111 : = fold_build2_loc (location, COMPOUND_EXPR, TREE_TYPE (expr), stat, expr);
907 : 111 : 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 : 0 : conditional_expression (tree, tree type_tree, tree cond_expr, tree then_expr,
915 : : tree else_expr, location_t location)
916 : : {
917 : 0 : if (error_operand_p (type_tree) || error_operand_p (cond_expr)
918 : 0 : || error_operand_p (then_expr) || error_operand_p (else_expr))
919 : 0 : return error_mark_node;
920 : 0 : tree ret = build3_loc (location, COND_EXPR, type_tree, cond_expr, then_expr,
921 : : else_expr);
922 : 0 : 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 : 150 : operator_to_tree_code (NegationOperator op)
930 : : {
931 : 150 : switch (op)
932 : : {
933 : : case NegationOperator::NEGATE:
934 : : return NEGATE_EXPR;
935 : 127 : case NegationOperator::NOT:
936 : 127 : return BIT_NOT_EXPR;
937 : 0 : default:
938 : 0 : 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 : 1473 : operator_to_tree_code (ArithmeticOrLogicalOperator op, bool floating_point)
948 : : {
949 : 1473 : switch (op)
950 : : {
951 : : case ArithmeticOrLogicalOperator::ADD:
952 : : return PLUS_EXPR;
953 : 62 : case ArithmeticOrLogicalOperator::SUBTRACT:
954 : 62 : return MINUS_EXPR;
955 : 501 : case ArithmeticOrLogicalOperator::MULTIPLY:
956 : 501 : return MULT_EXPR;
957 : 49 : case ArithmeticOrLogicalOperator::DIVIDE:
958 : 49 : if (floating_point)
959 : : return RDIV_EXPR;
960 : : else
961 : 39 : return TRUNC_DIV_EXPR;
962 : 47 : case ArithmeticOrLogicalOperator::MODULUS:
963 : 47 : return TRUNC_MOD_EXPR;
964 : 379 : case ArithmeticOrLogicalOperator::BITWISE_AND:
965 : 379 : return BIT_AND_EXPR;
966 : 36 : case ArithmeticOrLogicalOperator::BITWISE_OR:
967 : 36 : return BIT_IOR_EXPR;
968 : 24 : case ArithmeticOrLogicalOperator::BITWISE_XOR:
969 : 24 : return BIT_XOR_EXPR;
970 : 40 : case ArithmeticOrLogicalOperator::LEFT_SHIFT:
971 : 40 : return LSHIFT_EXPR;
972 : 28 : case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
973 : 28 : return RSHIFT_EXPR;
974 : 0 : default:
975 : 0 : rust_unreachable ();
976 : : }
977 : : }
978 : :
979 : : static enum tree_code
980 : 1915 : operator_to_tree_code (ComparisonOperator op)
981 : : {
982 : 1915 : 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 : 0 : default:
997 : 0 : rust_unreachable ();
998 : : }
999 : : }
1000 : :
1001 : : static enum tree_code
1002 : 316 : operator_to_tree_code (LazyBooleanOperator op)
1003 : : {
1004 : 316 : switch (op)
1005 : : {
1006 : : case LazyBooleanOperator::LOGICAL_OR:
1007 : : return TRUTH_ORIF_EXPR;
1008 : 261 : case LazyBooleanOperator::LOGICAL_AND:
1009 : 261 : return TRUTH_ANDIF_EXPR;
1010 : 0 : default:
1011 : 0 : rust_unreachable ();
1012 : : }
1013 : : }
1014 : :
1015 : : /* Returns true if the type of EXP is a floating point type.
1016 : : False otherwise. */
1017 : : bool
1018 : 4503 : is_floating_point (tree exp)
1019 : : {
1020 : 4503 : return FLOAT_TYPE_P (TREE_TYPE (exp));
1021 : : }
1022 : :
1023 : : // Return an expression for the negation operation OP EXPR.
1024 : : tree
1025 : 150 : 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 : 150 : if (error_operand_p (expr_tree))
1030 : 0 : return error_mark_node;
1031 : :
1032 : : /* For negation operators, the resulting type should be the same as its
1033 : : operand. */
1034 : 150 : auto tree_type = TREE_TYPE (expr_tree);
1035 : 150 : auto original_type = tree_type;
1036 : 150 : 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 : 150 : bool floating_point = is_floating_point (expr_tree);
1041 : 150 : auto extended_type = NULL_TREE;
1042 : 150 : if (floating_point)
1043 : : {
1044 : 0 : extended_type = excess_precision_type (tree_type);
1045 : 0 : if (extended_type != NULL_TREE)
1046 : : {
1047 : 0 : expr_tree = convert (extended_type, expr_tree);
1048 : 0 : tree_type = extended_type;
1049 : : }
1050 : : }
1051 : :
1052 : : /* Construct a new tree and build an expression from it. */
1053 : 150 : auto new_tree = fold_build1_loc (location, tree_code, tree_type, expr_tree);
1054 : 150 : if (floating_point && extended_type != NULL_TREE)
1055 : 0 : new_tree = convert (original_type, expr_tree);
1056 : : return new_tree;
1057 : : }
1058 : :
1059 : : tree
1060 : 1473 : 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 : 1473 : if (error_operand_p (left) || error_operand_p (right))
1066 : 0 : return error_mark_node;
1067 : :
1068 : : // unwrap the const decls if set
1069 : 1473 : if (TREE_CODE (left) == CONST_DECL)
1070 : 198 : left = DECL_INITIAL (left);
1071 : 1473 : if (TREE_CODE (right) == CONST_DECL)
1072 : 32 : right = DECL_INITIAL (right);
1073 : :
1074 : : /* We need to determine if we're doing floating point arithmetics of integer
1075 : : arithmetics. */
1076 : 1473 : bool floating_point = is_floating_point (left);
1077 : 1473 : auto ret = NULL_TREE;
1078 : :
1079 : : /* For arithmetic or logical operators, the resulting type should be the same
1080 : : as the lhs operand. */
1081 : 1473 : auto tree_type = TREE_TYPE (left);
1082 : 1473 : auto original_type = tree_type;
1083 : 1473 : 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 : 1473 : auto extended_type = NULL_TREE;
1088 : 1473 : if (floating_point)
1089 : : {
1090 : 222 : extended_type = excess_precision_type (tree_type);
1091 : 222 : if (extended_type != NULL_TREE)
1092 : : {
1093 : 0 : left = convert (extended_type, left);
1094 : 0 : right = convert (extended_type, right);
1095 : 0 : tree_type = extended_type;
1096 : : }
1097 : : }
1098 : :
1099 : 1473 : ret = fold_build2_loc (location, tree_code, tree_type, left, right);
1100 : 1473 : TREE_CONSTANT (ret) = TREE_CONSTANT (left) & TREE_CONSTANT (right);
1101 : :
1102 : : // TODO: How do we handle floating point?
1103 : 1473 : if (floating_point && extended_type != NULL_TREE)
1104 : 0 : ret = convert (original_type, ret);
1105 : :
1106 : 1473 : if (op == ArithmeticOrLogicalOperator::DIVIDE
1107 : 1473 : && (integer_zerop (right) || fixed_zerop (right)))
1108 : : {
1109 : 6 : rust_error_at (location, "division by zero");
1110 : : }
1111 : 1467 : else if (op == ArithmeticOrLogicalOperator::LEFT_SHIFT
1112 : 40 : && TREE_CODE (right) == INTEGER_CST
1113 : 1503 : && (compare_tree_int (right, TYPE_PRECISION (TREE_TYPE (ret))) >= 0))
1114 : : {
1115 : 2 : rust_error_at (location, "left shift count >= width of type");
1116 : : }
1117 : :
1118 : : return ret;
1119 : : }
1120 : :
1121 : : static bool
1122 : 2674 : is_overflowing_expr (ArithmeticOrLogicalOperator op)
1123 : : {
1124 : 2674 : switch (op)
1125 : : {
1126 : : case ArithmeticOrLogicalOperator::ADD:
1127 : : case ArithmeticOrLogicalOperator::SUBTRACT:
1128 : : case ArithmeticOrLogicalOperator::MULTIPLY:
1129 : : return true;
1130 : 0 : default:
1131 : 0 : return false;
1132 : : }
1133 : : }
1134 : :
1135 : : static std::pair<tree, tree>
1136 : 2418 : fetch_overflow_builtins (ArithmeticOrLogicalOperator op)
1137 : : {
1138 : 2418 : auto builtin_ctx = Rust::Compile::BuiltinsContext::get ();
1139 : :
1140 : 2418 : auto builtin = NULL_TREE;
1141 : 2418 : auto abort = NULL_TREE;
1142 : :
1143 : 2418 : switch (op)
1144 : : {
1145 : 1501 : case ArithmeticOrLogicalOperator::ADD:
1146 : 1501 : builtin_ctx.lookup_simple_builtin ("__builtin_add_overflow", &builtin);
1147 : 1501 : break;
1148 : 820 : case ArithmeticOrLogicalOperator::SUBTRACT:
1149 : 820 : builtin_ctx.lookup_simple_builtin ("__builtin_sub_overflow", &builtin);
1150 : 820 : break;
1151 : 97 : case ArithmeticOrLogicalOperator::MULTIPLY:
1152 : 97 : builtin_ctx.lookup_simple_builtin ("__builtin_mul_overflow", &builtin);
1153 : 97 : break;
1154 : 0 : default:
1155 : 0 : rust_unreachable ();
1156 : 2418 : break;
1157 : 2418 : };
1158 : :
1159 : 2418 : builtin_ctx.lookup_simple_builtin ("__builtin_abort", &abort);
1160 : :
1161 : 2418 : rust_assert (abort);
1162 : 2418 : rust_assert (builtin);
1163 : :
1164 : 2418 : return {abort, builtin};
1165 : 2418 : }
1166 : :
1167 : : // Return an expression for the arithmetic or logical operation LEFT OP RIGHT
1168 : : // with overflow checking when possible
1169 : : tree
1170 : 2880 : arithmetic_or_logical_expression_checked (ArithmeticOrLogicalOperator op,
1171 : : tree left, tree right,
1172 : : location_t location,
1173 : : Bvariable *receiver_var)
1174 : : {
1175 : : /* Check if either expression is an error, in which case we return an error
1176 : : expression. */
1177 : 2880 : if (error_operand_p (left) || error_operand_p (right))
1178 : 0 : return error_mark_node;
1179 : :
1180 : : // FIXME: Add `if (!debug_mode)`
1181 : : // No overflow checks for floating point operations or divisions. In that
1182 : : // case, simply assign the result of the operation to the receiver variable
1183 : 2880 : if (is_floating_point (left) || !is_overflowing_expr (op))
1184 : 462 : return assignment_statement (
1185 : : receiver_var->get_tree (location),
1186 : 462 : arithmetic_or_logical_expression (op, left, right, location), location);
1187 : :
1188 : 2418 : auto receiver = receiver_var->get_tree (location);
1189 : 2418 : TREE_ADDRESSABLE (receiver) = 1;
1190 : 2418 : auto result_ref = build_fold_addr_expr_loc (location, receiver);
1191 : :
1192 : 2418 : auto builtins = fetch_overflow_builtins (op);
1193 : 2418 : auto abort = builtins.first;
1194 : 2418 : auto builtin = builtins.second;
1195 : :
1196 : 2418 : auto abort_call = build_call_expr_loc (location, abort, 0);
1197 : :
1198 : 2418 : auto builtin_call
1199 : 2418 : = build_call_expr_loc (location, builtin, 3, left, right, result_ref);
1200 : 2418 : auto overflow_check
1201 : 2418 : = build2_loc (location, EQ_EXPR, boolean_type_node, builtin_call,
1202 : : boolean_constant_expression (true));
1203 : :
1204 : 2418 : auto if_block = build3_loc (location, COND_EXPR, void_type_node,
1205 : : overflow_check, abort_call, NULL_TREE);
1206 : :
1207 : 2418 : return if_block;
1208 : : }
1209 : :
1210 : : // Return an expression for the comparison operation LEFT OP RIGHT.
1211 : : tree
1212 : 1915 : comparison_expression (ComparisonOperator op, tree left_tree, tree right_tree,
1213 : : location_t location)
1214 : : {
1215 : : /* Check if either expression is an error, in which case we return an error
1216 : : expression. */
1217 : 1915 : if (error_operand_p (left_tree) || error_operand_p (right_tree))
1218 : 0 : return error_mark_node;
1219 : :
1220 : : /* For comparison operators, the resulting type should be boolean. */
1221 : 1915 : auto tree_type = boolean_type_node;
1222 : 1915 : auto tree_code = operator_to_tree_code (op);
1223 : :
1224 : : /* Construct a new tree and build an expression from it. */
1225 : 1915 : auto new_tree
1226 : 1915 : = fold_build2_loc (location, tree_code, tree_type, left_tree, right_tree);
1227 : 1915 : return new_tree;
1228 : : }
1229 : :
1230 : : // Return an expression for the lazy boolean operation LEFT OP RIGHT.
1231 : : tree
1232 : 316 : lazy_boolean_expression (LazyBooleanOperator op, tree left_tree,
1233 : : tree right_tree, location_t location)
1234 : : {
1235 : : /* Check if either expression is an error, in which case we return an error
1236 : : expression. */
1237 : 316 : if (error_operand_p (left_tree) || error_operand_p (right_tree))
1238 : 0 : return error_mark_node;
1239 : :
1240 : : /* For lazy boolean operators, the resulting type should be the same as the
1241 : : rhs operand. */
1242 : 316 : auto tree_type = TREE_TYPE (right_tree);
1243 : 316 : auto tree_code = operator_to_tree_code (op);
1244 : :
1245 : : /* Construct a new tree and build an expression from it. */
1246 : 316 : auto new_tree
1247 : 316 : = fold_build2_loc (location, tree_code, tree_type, left_tree, right_tree);
1248 : 316 : return new_tree;
1249 : : }
1250 : :
1251 : : // Return an expression that constructs BTYPE with VALS.
1252 : :
1253 : : tree
1254 : 12712 : constructor_expression (tree type_tree, bool is_variant,
1255 : : const std::vector<tree> &vals, int union_index,
1256 : : location_t location)
1257 : : {
1258 : 12712 : if (error_operand_p (type_tree))
1259 : 22 : return error_mark_node;
1260 : :
1261 : 12690 : vec<constructor_elt, va_gc> *init;
1262 : 12690 : vec_alloc (init, vals.size ());
1263 : :
1264 : 12690 : tree sink = NULL_TREE;
1265 : 12690 : bool is_constant = true;
1266 : 12690 : tree field = TYPE_FIELDS (type_tree);
1267 : :
1268 : 12690 : if (is_variant)
1269 : : {
1270 : 469 : gcc_assert (union_index != -1);
1271 : 469 : gcc_assert (TREE_CODE (type_tree) == UNION_TYPE);
1272 : :
1273 : 903 : for (int i = 0; i < union_index; i++)
1274 : : {
1275 : 434 : gcc_assert (field != NULL_TREE);
1276 : 434 : field = DECL_CHAIN (field);
1277 : : }
1278 : :
1279 : 469 : tree nested_ctor
1280 : 469 : = constructor_expression (TREE_TYPE (field), false, vals, -1, location);
1281 : :
1282 : 469 : constructor_elt empty = {NULL, NULL};
1283 : 469 : constructor_elt *elt = init->quick_push (empty);
1284 : 469 : elt->index = field;
1285 : 469 : elt->value = convert_tree (TREE_TYPE (field), nested_ctor, location);
1286 : 469 : if (!TREE_CONSTANT (elt->value))
1287 : 266 : is_constant = false;
1288 : : }
1289 : : else
1290 : : {
1291 : 12221 : if (union_index != -1)
1292 : : {
1293 : 89 : gcc_assert (TREE_CODE (type_tree) == UNION_TYPE);
1294 : 89 : tree val = vals.front ();
1295 : 197 : for (int i = 0; i < union_index; i++)
1296 : : {
1297 : 108 : gcc_assert (field != NULL_TREE);
1298 : 108 : field = DECL_CHAIN (field);
1299 : : }
1300 : :
1301 : 89 : if (TREE_TYPE (field) == error_mark_node || error_operand_p (val))
1302 : : return error_mark_node;
1303 : :
1304 : 89 : if (int_size_in_bytes (TREE_TYPE (field)) == 0)
1305 : : {
1306 : : // GIMPLE cannot represent indices of zero-sized types so
1307 : : // trying to construct a map with zero-sized keys might lead
1308 : : // to errors. Instead, we evaluate each expression that
1309 : : // would have been added as a map element for its
1310 : : // side-effects and construct an empty map.
1311 : 0 : append_to_statement_list (val, &sink);
1312 : : }
1313 : : else
1314 : : {
1315 : 89 : constructor_elt empty = {NULL, NULL};
1316 : 89 : constructor_elt *elt = init->quick_push (empty);
1317 : 89 : elt->index = field;
1318 : 89 : elt->value = convert_tree (TREE_TYPE (field), val, location);
1319 : 89 : if (!TREE_CONSTANT (elt->value))
1320 : 49 : is_constant = false;
1321 : : }
1322 : : }
1323 : : else
1324 : : {
1325 : 12132 : gcc_assert (TREE_CODE (type_tree) == RECORD_TYPE);
1326 : 12132 : for (std::vector<tree>::const_iterator p = vals.begin ();
1327 : 22803 : p != vals.end (); ++p, field = DECL_CHAIN (field))
1328 : : {
1329 : 10671 : gcc_assert (field != NULL_TREE);
1330 : 10671 : tree val = (*p);
1331 : 10671 : if (TREE_TYPE (field) == error_mark_node || error_operand_p (val))
1332 : 0 : return error_mark_node;
1333 : :
1334 : 10671 : if (int_size_in_bytes (TREE_TYPE (field)) == 0)
1335 : : {
1336 : : // GIMPLE cannot represent indices of zero-sized types so
1337 : : // trying to construct a map with zero-sized keys might lead
1338 : : // to errors. Instead, we evaluate each expression that
1339 : : // would have been added as a map element for its
1340 : : // side-effects and construct an empty map.
1341 : 102 : append_to_statement_list (val, &sink);
1342 : 102 : continue;
1343 : : }
1344 : :
1345 : 10569 : constructor_elt empty = {NULL, NULL};
1346 : 10569 : constructor_elt *elt = init->quick_push (empty);
1347 : 10569 : elt->index = field;
1348 : 10569 : elt->value = convert_tree (TREE_TYPE (field), val, location);
1349 : 10569 : if (!TREE_CONSTANT (elt->value))
1350 : 3056 : is_constant = false;
1351 : : }
1352 : : // gcc_assert (field == NULL_TREE);
1353 : : }
1354 : : }
1355 : :
1356 : 12690 : tree ret = build_constructor (type_tree, init);
1357 : 12690 : if (is_constant)
1358 : 10696 : TREE_CONSTANT (ret) = 1;
1359 : 12690 : if (sink != NULL_TREE)
1360 : 2 : ret = fold_build2_loc (location, COMPOUND_EXPR, type_tree, sink, ret);
1361 : : return ret;
1362 : : }
1363 : :
1364 : : tree
1365 : 422 : array_constructor_expression (tree type_tree,
1366 : : const std::vector<unsigned long> &indexes,
1367 : : const std::vector<tree> &vals,
1368 : : location_t location)
1369 : : {
1370 : 422 : if (error_operand_p (type_tree))
1371 : 0 : return error_mark_node;
1372 : :
1373 : 422 : gcc_assert (indexes.size () == vals.size ());
1374 : :
1375 : 422 : tree element_type = TREE_TYPE (type_tree);
1376 : 422 : HOST_WIDE_INT element_size = int_size_in_bytes (element_type);
1377 : 422 : vec<constructor_elt, va_gc> *init;
1378 : 844 : vec_alloc (init, element_size == 0 ? 0 : vals.size ());
1379 : :
1380 : 422 : tree sink = NULL_TREE;
1381 : 422 : bool is_constant = true;
1382 : 2291 : for (size_t i = 0; i < vals.size (); ++i)
1383 : : {
1384 : 1869 : tree index = size_int (indexes[i]);
1385 : 1869 : tree val = vals[i];
1386 : :
1387 : 1869 : if (error_operand_p (index) || error_operand_p (val))
1388 : 0 : return error_mark_node;
1389 : :
1390 : 1869 : if (element_size == 0)
1391 : : {
1392 : : // GIMPLE cannot represent arrays of zero-sized types so trying
1393 : : // to construct an array of zero-sized values might lead to errors.
1394 : : // Instead, we evaluate each expression that would have been added as
1395 : : // an array value for its side-effects and construct an empty array.
1396 : 0 : append_to_statement_list (val, &sink);
1397 : 0 : continue;
1398 : : }
1399 : :
1400 : 1869 : if (!TREE_CONSTANT (val))
1401 : 16 : is_constant = false;
1402 : :
1403 : 1869 : constructor_elt empty = {NULL, NULL};
1404 : 1869 : constructor_elt *elt = init->quick_push (empty);
1405 : 1869 : elt->index = index;
1406 : 1869 : elt->value = val;
1407 : : }
1408 : :
1409 : 422 : tree ret = build_constructor (type_tree, init);
1410 : 422 : if (is_constant)
1411 : 408 : TREE_CONSTANT (ret) = 1;
1412 : 422 : if (sink != NULL_TREE)
1413 : 0 : ret = fold_build2_loc (location, COMPOUND_EXPR, type_tree, sink, ret);
1414 : : return ret;
1415 : : }
1416 : :
1417 : : // Build insns to create an array, initialize all elements of the array to
1418 : : // value, and return it
1419 : : tree
1420 : 111 : array_initializer (tree fndecl, tree block, tree array_type, tree length,
1421 : : tree value, tree *tmp, location_t locus)
1422 : : {
1423 : 111 : std::vector<tree> stmts;
1424 : :
1425 : : // Temporary array we initialize with the desired value.
1426 : 111 : tree t = NULL_TREE;
1427 : 111 : Bvariable *tmp_array = temporary_variable (fndecl, block, array_type,
1428 : : NULL_TREE, true, locus, &t);
1429 : 111 : tree arr = tmp_array->get_tree (locus);
1430 : 111 : stmts.push_back (t);
1431 : :
1432 : : // Temporary for the array length used for initialization loop guard.
1433 : 111 : Bvariable *tmp_len = temporary_variable (fndecl, block, size_type_node,
1434 : : length, true, locus, &t);
1435 : 111 : tree len = tmp_len->get_tree (locus);
1436 : 111 : stmts.push_back (t);
1437 : :
1438 : : // Temporary variable for pointer used to initialize elements.
1439 : 111 : tree ptr_type = pointer_type (TREE_TYPE (array_type));
1440 : 111 : tree ptr_init
1441 : 111 : = build1_loc (locus, ADDR_EXPR, ptr_type,
1442 : : array_index_expression (arr, integer_zero_node, locus));
1443 : 111 : Bvariable *tmp_ptr
1444 : 111 : = temporary_variable (fndecl, block, ptr_type, ptr_init, false, locus, &t);
1445 : 111 : tree ptr = tmp_ptr->get_tree (locus);
1446 : 111 : stmts.push_back (t);
1447 : :
1448 : : // push statement list for the loop
1449 : 111 : std::vector<tree> loop_stmts;
1450 : :
1451 : : // Loop exit condition:
1452 : : // if (length == 0) break;
1453 : 111 : t = comparison_expression (ComparisonOperator::EQUAL, len,
1454 : 111 : zero_expression (TREE_TYPE (len)), locus);
1455 : :
1456 : 111 : t = exit_expression (t, locus);
1457 : 111 : loop_stmts.push_back (t);
1458 : :
1459 : : // Assign value to the current pointer position
1460 : : // *ptr = value;
1461 : 111 : t = assignment_statement (build_fold_indirect_ref (ptr), value, locus);
1462 : 111 : loop_stmts.push_back (t);
1463 : :
1464 : : // Move pointer to next element
1465 : : // ptr++;
1466 : 111 : tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptr_type));
1467 : 111 : t = build2 (POSTINCREMENT_EXPR, ptr_type, ptr, convert (ptr_type, size));
1468 : 111 : loop_stmts.push_back (t);
1469 : :
1470 : : // Decrement loop counter.
1471 : : // length--;
1472 : 111 : t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (len), len,
1473 : 111 : convert (TREE_TYPE (len), integer_one_node));
1474 : 111 : loop_stmts.push_back (t);
1475 : :
1476 : : // pop statments and finish loop
1477 : 111 : tree loop_body = statement_list (loop_stmts);
1478 : 111 : stmts.push_back (loop_expression (loop_body, locus));
1479 : :
1480 : : // Return the temporary in the provided pointer and the statement list which
1481 : : // initializes it.
1482 : 111 : *tmp = tmp_array->get_tree (locus);
1483 : 111 : return statement_list (stmts);
1484 : 111 : }
1485 : :
1486 : : // Return an expression representing ARRAY[INDEX]
1487 : :
1488 : : tree
1489 : 281 : array_index_expression (tree array_tree, tree index_tree, location_t location)
1490 : : {
1491 : 281 : if (error_operand_p (array_tree) || error_operand_p (index_tree))
1492 : 0 : return error_mark_node;
1493 : :
1494 : : // A function call that returns a zero sized object will have been
1495 : : // changed to return void. If we see void here, assume we are
1496 : : // dealing with a zero sized type and just evaluate the operands.
1497 : 281 : tree ret;
1498 : 281 : if (TREE_TYPE (array_tree) != void_type_node)
1499 : 281 : ret = build4_loc (location, ARRAY_REF, TREE_TYPE (TREE_TYPE (array_tree)),
1500 : : array_tree, index_tree, NULL_TREE, NULL_TREE);
1501 : : else
1502 : 0 : ret = fold_build2_loc (location, COMPOUND_EXPR, void_type_node, array_tree,
1503 : : index_tree);
1504 : :
1505 : : return ret;
1506 : : }
1507 : :
1508 : : // Create an expression for a call to FN_EXPR with FN_ARGS.
1509 : : tree
1510 : 11754 : call_expression (tree fn, const std::vector<tree> &fn_args, tree chain_expr,
1511 : : location_t location)
1512 : : {
1513 : 11754 : if (error_operand_p (fn))
1514 : 24 : return error_mark_node;
1515 : :
1516 : 11730 : gcc_assert (FUNCTION_POINTER_TYPE_P (TREE_TYPE (fn)));
1517 : 11730 : tree rettype = TREE_TYPE (TREE_TYPE (TREE_TYPE (fn)));
1518 : :
1519 : 11730 : size_t nargs = fn_args.size ();
1520 : 11730 : tree *args = nargs == 0 ? NULL : new tree[nargs];
1521 : 24776 : for (size_t i = 0; i < nargs; ++i)
1522 : : {
1523 : 13046 : args[i] = fn_args.at (i);
1524 : : }
1525 : :
1526 : 11730 : tree fndecl = fn;
1527 : 11730 : if (TREE_CODE (fndecl) == ADDR_EXPR)
1528 : 11504 : fndecl = TREE_OPERAND (fndecl, 0);
1529 : :
1530 : : // This is to support builtin math functions when using 80387 math.
1531 : 11730 : tree excess_type = NULL_TREE;
1532 : 8422 : if (optimize && TREE_CODE (fndecl) == FUNCTION_DECL
1533 : 8240 : && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
1534 : 498 : && DECL_IS_UNDECLARED_BUILTIN (fndecl) && nargs > 0
1535 : 12216 : && ((SCALAR_FLOAT_TYPE_P (rettype)
1536 : 252 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (args[0])))
1537 : 234 : || (COMPLEX_FLOAT_TYPE_P (rettype)
1538 : 0 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (args[0])))))
1539 : : {
1540 : 252 : excess_type = excess_precision_type (TREE_TYPE (args[0]));
1541 : 252 : if (excess_type != NULL_TREE)
1542 : : {
1543 : 0 : tree excess_fndecl
1544 : 0 : = mathfn_built_in (excess_type, DECL_FUNCTION_CODE (fndecl));
1545 : 0 : if (excess_fndecl == NULL_TREE)
1546 : : excess_type = NULL_TREE;
1547 : : else
1548 : : {
1549 : 0 : fn = build_fold_addr_expr_loc (location, excess_fndecl);
1550 : 0 : for (size_t i = 0; i < nargs; ++i)
1551 : : {
1552 : 0 : if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (args[i]))
1553 : 0 : || COMPLEX_FLOAT_TYPE_P (TREE_TYPE (args[i])))
1554 : 0 : args[i] = ::convert (excess_type, args[i]);
1555 : : }
1556 : : }
1557 : : }
1558 : : }
1559 : :
1560 : 11730 : tree ret
1561 : 11730 : = build_call_array_loc (location,
1562 : : excess_type != NULL_TREE ? excess_type : rettype,
1563 : : fn, nargs, args);
1564 : :
1565 : : // check for deprecated function usage
1566 : 11730 : if (fndecl && TREE_DEPRECATED (fndecl))
1567 : : {
1568 : : // set up the call-site information for `warn_deprecated_use`
1569 : 10 : input_location = location;
1570 : 10 : warn_deprecated_use (fndecl, NULL_TREE);
1571 : : }
1572 : :
1573 : 11730 : if (chain_expr)
1574 : 0 : CALL_EXPR_STATIC_CHAIN (ret) = chain_expr;
1575 : :
1576 : 11730 : if (excess_type != NULL_TREE)
1577 : : {
1578 : : // Calling convert here can undo our excess precision change.
1579 : : // That may or may not be a bug in convert_to_real.
1580 : 0 : ret = build1_loc (location, NOP_EXPR, rettype, ret);
1581 : : }
1582 : :
1583 : 11730 : delete[] args;
1584 : : return ret;
1585 : : }
1586 : :
1587 : : // Variable initialization.
1588 : :
1589 : : tree
1590 : 10872 : init_statement (tree, Bvariable *var, tree init_tree)
1591 : : {
1592 : 10872 : tree var_tree = var->get_decl ();
1593 : 10872 : if (error_operand_p (var_tree) || error_operand_p (init_tree))
1594 : 28 : return error_mark_node;
1595 : 10844 : gcc_assert (TREE_CODE (var_tree) == VAR_DECL);
1596 : :
1597 : : // To avoid problems with GNU ld, we don't make zero-sized
1598 : : // externally visible variables. That might lead us to doing an
1599 : : // initialization of a zero-sized expression to a non-zero sized
1600 : : // variable, or vice-versa. Avoid crashes by omitting the
1601 : : // initializer. Such initializations don't mean anything anyhow.
1602 : 21401 : if (int_size_in_bytes (TREE_TYPE (var_tree)) != 0 && init_tree != NULL_TREE
1603 : 10557 : && TREE_TYPE (init_tree) != void_type_node
1604 : 21401 : && int_size_in_bytes (TREE_TYPE (init_tree)) != 0)
1605 : : {
1606 : 10551 : DECL_INITIAL (var_tree) = init_tree;
1607 : 10551 : init_tree = NULL_TREE;
1608 : : }
1609 : :
1610 : 10844 : tree ret = build1_loc (DECL_SOURCE_LOCATION (var_tree), DECL_EXPR,
1611 : : void_type_node, var_tree);
1612 : 10844 : if (init_tree != NULL_TREE)
1613 : 293 : ret = build2_loc (DECL_SOURCE_LOCATION (var_tree), COMPOUND_EXPR,
1614 : : void_type_node, init_tree, ret);
1615 : :
1616 : : return ret;
1617 : : }
1618 : :
1619 : : // Assignment.
1620 : :
1621 : : tree
1622 : 9321 : assignment_statement (tree lhs, tree rhs, location_t location)
1623 : : {
1624 : 9321 : if (error_operand_p (lhs) || error_operand_p (rhs))
1625 : 119 : return error_mark_node;
1626 : :
1627 : : // To avoid problems with GNU ld, we don't make zero-sized
1628 : : // externally visible variables. That might lead us to doing an
1629 : : // assignment of a zero-sized expression to a non-zero sized
1630 : : // expression; avoid crashes here by avoiding assignments of
1631 : : // zero-sized expressions. Such assignments don't really mean
1632 : : // anything anyhow.
1633 : 9202 : if (TREE_TYPE (lhs) == void_type_node
1634 : 9202 : || int_size_in_bytes (TREE_TYPE (lhs)) == 0
1635 : 6233 : || TREE_TYPE (rhs) == void_type_node
1636 : 15435 : || int_size_in_bytes (TREE_TYPE (rhs)) == 0)
1637 : 2992 : return compound_statement (lhs, rhs);
1638 : :
1639 : 6210 : rhs = convert_tree (TREE_TYPE (lhs), rhs, location);
1640 : :
1641 : 6210 : return fold_build2_loc (location, MODIFY_EXPR, void_type_node, lhs, rhs);
1642 : : }
1643 : :
1644 : : // Return.
1645 : :
1646 : : tree
1647 : 19822 : return_statement (tree fntree, tree val, location_t location)
1648 : : {
1649 : 19822 : if (error_operand_p (fntree))
1650 : 0 : return error_mark_node;
1651 : :
1652 : 19822 : tree result = DECL_RESULT (fntree);
1653 : 19822 : if (error_operand_p (result))
1654 : 0 : return error_mark_node;
1655 : :
1656 : 19822 : if (error_operand_p (val))
1657 : 74 : return error_mark_node;
1658 : :
1659 : 19748 : tree set
1660 : 19748 : = fold_build2_loc (location, MODIFY_EXPR, void_type_node, result, val);
1661 : 19748 : return fold_build1_loc (location, RETURN_EXPR, void_type_node, set);
1662 : : }
1663 : :
1664 : : // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
1665 : : // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
1666 : : // NULL, it will always be executed. This is used for handling defers in Rust
1667 : : // functions. In C++, the resulting code is of this form:
1668 : : // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
1669 : :
1670 : : tree
1671 : 8 : exception_handler_statement (tree try_stmt, tree except_stmt, tree finally_stmt,
1672 : : location_t location)
1673 : : {
1674 : 16 : if (error_operand_p (try_stmt) || error_operand_p (except_stmt)
1675 : 16 : || error_operand_p (finally_stmt))
1676 : 0 : return error_mark_node;
1677 : :
1678 : 8 : if (except_stmt != NULL_TREE)
1679 : 4 : try_stmt = build2_loc (location, TRY_CATCH_EXPR, void_type_node, try_stmt,
1680 : : build2_loc (location, CATCH_EXPR, void_type_node,
1681 : : NULL, except_stmt));
1682 : 8 : if (finally_stmt != NULL_TREE)
1683 : 4 : try_stmt = build2_loc (location, TRY_FINALLY_EXPR, void_type_node, try_stmt,
1684 : : finally_stmt);
1685 : : return try_stmt;
1686 : : }
1687 : :
1688 : : // If.
1689 : :
1690 : : tree
1691 : 1883 : if_statement (tree, tree cond_tree, tree then_tree, tree else_tree,
1692 : : location_t location)
1693 : : {
1694 : 3766 : if (error_operand_p (cond_tree) || error_operand_p (then_tree)
1695 : 3766 : || error_operand_p (else_tree))
1696 : 0 : return error_mark_node;
1697 : 1883 : tree ret = build3_loc (location, COND_EXPR, void_type_node, cond_tree,
1698 : : then_tree, else_tree);
1699 : 1883 : return ret;
1700 : : }
1701 : :
1702 : : // Loops
1703 : :
1704 : : tree
1705 : 301 : loop_expression (tree body, location_t locus)
1706 : : {
1707 : 301 : return fold_build1_loc (locus, LOOP_EXPR, void_type_node, body);
1708 : : }
1709 : :
1710 : : tree
1711 : 243 : exit_expression (tree cond_tree, location_t locus)
1712 : : {
1713 : 243 : return fold_build1_loc (locus, EXIT_EXPR, void_type_node, cond_tree);
1714 : : }
1715 : :
1716 : : // Pair of statements.
1717 : :
1718 : : tree
1719 : 3030 : compound_statement (tree s1, tree s2)
1720 : : {
1721 : 3030 : if (error_operand_p (s1) || error_operand_p (s2))
1722 : 0 : return error_mark_node;
1723 : :
1724 : 3030 : tree stmt_list = NULL_TREE;
1725 : 3030 : append_to_statement_list (s1, &stmt_list);
1726 : 3030 : append_to_statement_list (s2, &stmt_list);
1727 : :
1728 : : // If neither statement has any side effects, stmt_list can be NULL
1729 : : // at this point.
1730 : 3030 : if (stmt_list == NULL_TREE)
1731 : 2908 : stmt_list = integer_zero_node;
1732 : :
1733 : 3030 : return stmt_list;
1734 : : }
1735 : :
1736 : : // List of statements.
1737 : :
1738 : : tree
1739 : 222 : statement_list (const std::vector<tree> &statements)
1740 : : {
1741 : 222 : tree stmt_list = NULL_TREE;
1742 : 1110 : for (tree t : statements)
1743 : : {
1744 : 888 : if (error_operand_p (t))
1745 : 0 : return error_mark_node;
1746 : 888 : append_to_statement_list (t, &stmt_list);
1747 : : }
1748 : 222 : return stmt_list;
1749 : : }
1750 : :
1751 : : // Make a block. For some reason gcc uses a dual structure for
1752 : : // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
1753 : : // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
1754 : : // the Bblock.
1755 : :
1756 : : tree
1757 : 27282 : block (tree fndecl, tree enclosing, const std::vector<Bvariable *> &vars,
1758 : : location_t start_location, location_t)
1759 : : {
1760 : 27282 : tree block_tree = make_node (BLOCK);
1761 : 27282 : if (enclosing == NULL)
1762 : : {
1763 : 19909 : gcc_assert (fndecl != NULL_TREE);
1764 : :
1765 : : // We may have already created a block for local variables when
1766 : : // we take the address of a parameter.
1767 : 19909 : if (DECL_INITIAL (fndecl) == NULL_TREE)
1768 : : {
1769 : 19901 : BLOCK_SUPERCONTEXT (block_tree) = fndecl;
1770 : 19901 : DECL_INITIAL (fndecl) = block_tree;
1771 : : }
1772 : : else
1773 : : {
1774 : 8 : tree superblock_tree = DECL_INITIAL (fndecl);
1775 : 8 : BLOCK_SUPERCONTEXT (block_tree) = superblock_tree;
1776 : 8 : tree *pp;
1777 : 12 : for (pp = &BLOCK_SUBBLOCKS (superblock_tree); *pp != NULL_TREE;
1778 : 4 : pp = &BLOCK_CHAIN (*pp))
1779 : : ;
1780 : 8 : *pp = block_tree;
1781 : : }
1782 : : }
1783 : : else
1784 : : {
1785 : 7373 : tree superblock_tree = BIND_EXPR_BLOCK (enclosing);
1786 : 7373 : gcc_assert (TREE_CODE (superblock_tree) == BLOCK);
1787 : :
1788 : 7373 : BLOCK_SUPERCONTEXT (block_tree) = superblock_tree;
1789 : 7373 : tree *pp;
1790 : 11343 : for (pp = &BLOCK_SUBBLOCKS (superblock_tree); *pp != NULL_TREE;
1791 : 3970 : pp = &BLOCK_CHAIN (*pp))
1792 : : ;
1793 : 7373 : *pp = block_tree;
1794 : : }
1795 : :
1796 : : // Chain the variables of the scope together so they are all connected
1797 : : // to the block.
1798 : 27282 : tree *pp = &BLOCK_VARS (block_tree);
1799 : 27708 : for (Bvariable *bv : vars)
1800 : : {
1801 : 426 : *pp = bv->get_decl ();
1802 : 426 : if (!error_operand_p (*pp))
1803 : 426 : pp = &DECL_CHAIN (*pp);
1804 : : }
1805 : 27282 : *pp = NULL_TREE;
1806 : :
1807 : 27282 : TREE_USED (block_tree) = 1;
1808 : :
1809 : 27282 : tree bind_tree = build3_loc (start_location, BIND_EXPR, void_type_node,
1810 : 27282 : BLOCK_VARS (block_tree), NULL_TREE, block_tree);
1811 : 27282 : TREE_SIDE_EFFECTS (bind_tree) = 1;
1812 : 27282 : return bind_tree;
1813 : : }
1814 : :
1815 : : // Add statements to a block.
1816 : :
1817 : : void
1818 : 27266 : block_add_statements (tree bind_tree, const std::vector<tree> &statements)
1819 : : {
1820 : 27266 : tree stmt_list = NULL_TREE;
1821 : 109452 : for (tree s : statements)
1822 : : {
1823 : 82186 : if (!error_operand_p (s))
1824 : 79156 : append_to_statement_list (s, &stmt_list);
1825 : : }
1826 : :
1827 : 27266 : gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
1828 : 27266 : BIND_EXPR_BODY (bind_tree) = stmt_list;
1829 : 27266 : }
1830 : :
1831 : : // This is not static because we declare it with GTY(()) in rust-c.h.
1832 : : tree rust_non_zero_struct;
1833 : :
1834 : : // Return a type corresponding to TYPE with non-zero size.
1835 : :
1836 : : tree
1837 : 6 : non_zero_size_type (tree type)
1838 : : {
1839 : 6 : if (int_size_in_bytes (type) != 0)
1840 : : return type;
1841 : :
1842 : 6 : switch (TREE_CODE (type))
1843 : : {
1844 : 6 : case RECORD_TYPE:
1845 : 6 : if (TYPE_FIELDS (type) != NULL_TREE)
1846 : : {
1847 : 0 : tree ns = make_node (RECORD_TYPE);
1848 : 0 : tree field_trees = NULL_TREE;
1849 : 0 : tree *pp = &field_trees;
1850 : 0 : for (tree field = TYPE_FIELDS (type); field != NULL_TREE;
1851 : 0 : field = DECL_CHAIN (field))
1852 : : {
1853 : 0 : tree ft = TREE_TYPE (field);
1854 : 0 : if (field == TYPE_FIELDS (type))
1855 : 0 : ft = non_zero_size_type (ft);
1856 : 0 : tree f = build_decl (DECL_SOURCE_LOCATION (field), FIELD_DECL,
1857 : 0 : DECL_NAME (field), ft);
1858 : 0 : DECL_CONTEXT (f) = ns;
1859 : 0 : *pp = f;
1860 : 0 : pp = &DECL_CHAIN (f);
1861 : : }
1862 : 0 : TYPE_FIELDS (ns) = field_trees;
1863 : 0 : layout_type (ns);
1864 : 0 : return ns;
1865 : : }
1866 : :
1867 : 6 : if (rust_non_zero_struct == NULL_TREE)
1868 : : {
1869 : 4 : type = make_node (RECORD_TYPE);
1870 : 4 : tree field = build_decl (UNKNOWN_LOCATION, FIELD_DECL,
1871 : : get_identifier ("dummy"), boolean_type_node);
1872 : 4 : DECL_CONTEXT (field) = type;
1873 : 4 : TYPE_FIELDS (type) = field;
1874 : 4 : layout_type (type);
1875 : 4 : rust_non_zero_struct = type;
1876 : : }
1877 : 6 : return rust_non_zero_struct;
1878 : :
1879 : 0 : case ARRAY_TYPE: {
1880 : 0 : tree element_type = non_zero_size_type (TREE_TYPE (type));
1881 : 0 : return build_array_type_nelts (element_type, 1);
1882 : : }
1883 : :
1884 : 0 : default:
1885 : 0 : rust_unreachable ();
1886 : : }
1887 : :
1888 : : rust_unreachable ();
1889 : : }
1890 : :
1891 : : // Convert EXPR_TREE to TYPE_TREE. Sometimes the same unnamed Rust type
1892 : : // can be created multiple times and thus have multiple tree
1893 : : // representations. Make sure this does not confuse the middle-end.
1894 : :
1895 : : tree
1896 : 20962 : convert_tree (tree type_tree, tree expr_tree, location_t location)
1897 : : {
1898 : 20962 : if (type_tree == TREE_TYPE (expr_tree))
1899 : : return expr_tree;
1900 : :
1901 : 4648 : if (error_operand_p (type_tree) || error_operand_p (expr_tree))
1902 : 0 : return error_mark_node;
1903 : :
1904 : 4648 : if (POINTER_TYPE_P (type_tree) || INTEGRAL_TYPE_P (type_tree)
1905 : 4648 : || SCALAR_FLOAT_TYPE_P (type_tree) || COMPLEX_FLOAT_TYPE_P (type_tree))
1906 : 3986 : return fold_convert_loc (location, type_tree, expr_tree);
1907 : 662 : else if (TREE_CODE (type_tree) == RECORD_TYPE
1908 : : || TREE_CODE (type_tree) == UNION_TYPE
1909 : 662 : || TREE_CODE (type_tree) == ARRAY_TYPE)
1910 : : {
1911 : 662 : gcc_assert (int_size_in_bytes (type_tree)
1912 : : == int_size_in_bytes (TREE_TYPE (expr_tree)));
1913 : 662 : if (TYPE_MAIN_VARIANT (type_tree)
1914 : 662 : == TYPE_MAIN_VARIANT (TREE_TYPE (expr_tree)))
1915 : 630 : return fold_build1_loc (location, NOP_EXPR, type_tree, expr_tree);
1916 : 32 : return fold_build1_loc (location, VIEW_CONVERT_EXPR, type_tree,
1917 : 32 : expr_tree);
1918 : : }
1919 : :
1920 : 0 : rust_unreachable ();
1921 : : }
1922 : :
1923 : : // Make a global variable.
1924 : :
1925 : : Bvariable *
1926 : 55 : global_variable (const std::string &var_name, const std::string &asm_name,
1927 : : tree type_tree, bool is_external, bool is_hidden,
1928 : : bool in_unique_section, location_t location)
1929 : : {
1930 : 55 : if (error_operand_p (type_tree))
1931 : 0 : return Bvariable::error_variable ();
1932 : :
1933 : : // The GNU linker does not like dynamic variables with zero size.
1934 : 55 : tree orig_type_tree = type_tree;
1935 : 55 : if ((is_external || !is_hidden) && int_size_in_bytes (type_tree) == 0)
1936 : 6 : type_tree = non_zero_size_type (type_tree);
1937 : :
1938 : 110 : tree decl = build_decl (location, VAR_DECL,
1939 : : get_identifier_from_string (var_name), type_tree);
1940 : 55 : if (is_external)
1941 : 0 : DECL_EXTERNAL (decl) = 1;
1942 : : else
1943 : 55 : TREE_STATIC (decl) = 1;
1944 : 55 : if (!is_hidden)
1945 : : {
1946 : 55 : TREE_PUBLIC (decl) = 1;
1947 : 110 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
1948 : : }
1949 : : else
1950 : : {
1951 : 0 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
1952 : : }
1953 : :
1954 : 55 : TREE_USED (decl) = 1;
1955 : :
1956 : 55 : if (in_unique_section)
1957 : 55 : resolve_unique_section (decl, 0, 1);
1958 : :
1959 : 55 : rust_preserve_from_gc (decl);
1960 : :
1961 : 55 : return new Bvariable (decl, orig_type_tree);
1962 : : }
1963 : :
1964 : : // Set the initial value of a global variable.
1965 : :
1966 : : void
1967 : 55 : global_variable_set_init (Bvariable *var, tree expr_tree)
1968 : : {
1969 : 55 : if (error_operand_p (expr_tree))
1970 : : return;
1971 : 55 : gcc_assert (TREE_CONSTANT (expr_tree));
1972 : 55 : tree var_decl = var->get_decl ();
1973 : 55 : if (error_operand_p (var_decl))
1974 : : return;
1975 : 55 : DECL_INITIAL (var_decl) = expr_tree;
1976 : :
1977 : : // If this variable goes in a unique section, it may need to go into
1978 : : // a different one now that DECL_INITIAL is set.
1979 : 55 : if (symtab_node::get (var_decl)
1980 : 55 : && symtab_node::get (var_decl)->implicit_section)
1981 : : {
1982 : 55 : set_decl_section_name (var_decl, (const char *) NULL);
1983 : 55 : resolve_unique_section (var_decl, compute_reloc_for_constant (expr_tree),
1984 : : 1);
1985 : : }
1986 : : }
1987 : :
1988 : : // Make a local variable.
1989 : :
1990 : : Bvariable *
1991 : 0 : local_variable (tree function, const std::string &name, tree type_tree,
1992 : : Bvariable *decl_var, location_t location)
1993 : : {
1994 : 0 : if (error_operand_p (type_tree))
1995 : 0 : return Bvariable::error_variable ();
1996 : 0 : tree decl = build_decl (location, VAR_DECL, get_identifier_from_string (name),
1997 : : type_tree);
1998 : 0 : DECL_CONTEXT (decl) = function;
1999 : :
2000 : 0 : if (decl_var != NULL)
2001 : : {
2002 : 0 : DECL_HAS_VALUE_EXPR_P (decl) = 1;
2003 : 0 : SET_DECL_VALUE_EXPR (decl, decl_var->get_decl ());
2004 : : }
2005 : 0 : rust_preserve_from_gc (decl);
2006 : 0 : return new Bvariable (decl);
2007 : : }
2008 : :
2009 : : // Make a function parameter variable.
2010 : :
2011 : : Bvariable *
2012 : 12433 : parameter_variable (tree function, const std::string &name, tree type_tree,
2013 : : location_t location)
2014 : : {
2015 : 12433 : if (error_operand_p (type_tree))
2016 : 0 : return Bvariable::error_variable ();
2017 : 24866 : tree decl = build_decl (location, PARM_DECL,
2018 : : get_identifier_from_string (name), type_tree);
2019 : 12433 : DECL_CONTEXT (decl) = function;
2020 : 12433 : DECL_ARG_TYPE (decl) = type_tree;
2021 : :
2022 : 12433 : rust_preserve_from_gc (decl);
2023 : 12433 : return new Bvariable (decl);
2024 : : }
2025 : :
2026 : : // Make a static chain variable.
2027 : :
2028 : : Bvariable *
2029 : 0 : static_chain_variable (tree fndecl, const std::string &name, tree type_tree,
2030 : : location_t location)
2031 : : {
2032 : 0 : if (error_operand_p (type_tree))
2033 : 0 : return Bvariable::error_variable ();
2034 : 0 : tree decl = build_decl (location, PARM_DECL,
2035 : : get_identifier_from_string (name), type_tree);
2036 : 0 : DECL_CONTEXT (decl) = fndecl;
2037 : 0 : DECL_ARG_TYPE (decl) = type_tree;
2038 : 0 : TREE_USED (decl) = 1;
2039 : 0 : DECL_ARTIFICIAL (decl) = 1;
2040 : 0 : DECL_IGNORED_P (decl) = 1;
2041 : 0 : TREE_READONLY (decl) = 1;
2042 : :
2043 : 0 : struct function *f = DECL_STRUCT_FUNCTION (fndecl);
2044 : 0 : if (f == NULL)
2045 : : {
2046 : 0 : push_struct_function (fndecl);
2047 : 0 : pop_cfun ();
2048 : 0 : f = DECL_STRUCT_FUNCTION (fndecl);
2049 : : }
2050 : 0 : gcc_assert (f->static_chain_decl == NULL);
2051 : 0 : f->static_chain_decl = decl;
2052 : 0 : DECL_STATIC_CHAIN (fndecl) = 1;
2053 : :
2054 : 0 : rust_preserve_from_gc (decl);
2055 : 0 : return new Bvariable (decl);
2056 : : }
2057 : :
2058 : : // Make a temporary variable.
2059 : :
2060 : : Bvariable *
2061 : 26087 : temporary_variable (tree fndecl, tree bind_tree, tree type_tree, tree init_tree,
2062 : : bool is_address_taken, location_t location,
2063 : : tree *pstatement)
2064 : : {
2065 : 26087 : gcc_assert (fndecl != NULL_TREE);
2066 : 52174 : if (error_operand_p (type_tree) || error_operand_p (init_tree)
2067 : 52166 : || error_operand_p (fndecl))
2068 : : {
2069 : 8 : *pstatement = error_mark_node;
2070 : 8 : return Bvariable::error_variable ();
2071 : : }
2072 : :
2073 : 26079 : tree var;
2074 : : // We can only use create_tmp_var if the type is not addressable.
2075 : 26079 : if (!TREE_ADDRESSABLE (type_tree))
2076 : : {
2077 : 26079 : if (DECL_STRUCT_FUNCTION (fndecl) == NULL)
2078 : 16541 : push_struct_function (fndecl);
2079 : : else
2080 : 9538 : push_cfun (DECL_STRUCT_FUNCTION (fndecl));
2081 : :
2082 : 26079 : var = create_tmp_var (type_tree, "RUSTTMP");
2083 : 26079 : pop_cfun ();
2084 : : }
2085 : : else
2086 : : {
2087 : 0 : gcc_assert (bind_tree != NULL_TREE);
2088 : 0 : var = build_decl (location, VAR_DECL, create_tmp_var_name ("RUSTTMP"),
2089 : : type_tree);
2090 : 0 : DECL_ARTIFICIAL (var) = 1;
2091 : 0 : DECL_IGNORED_P (var) = 1;
2092 : 0 : TREE_USED (var) = 1;
2093 : 0 : DECL_CONTEXT (var) = fndecl;
2094 : :
2095 : : // We have to add this variable to the BLOCK and the BIND_EXPR.
2096 : 0 : gcc_assert (TREE_CODE (bind_tree) == BIND_EXPR);
2097 : 0 : tree block_tree = BIND_EXPR_BLOCK (bind_tree);
2098 : 0 : gcc_assert (TREE_CODE (block_tree) == BLOCK);
2099 : 0 : DECL_CHAIN (var) = BLOCK_VARS (block_tree);
2100 : 0 : BLOCK_VARS (block_tree) = var;
2101 : 0 : BIND_EXPR_VARS (bind_tree) = BLOCK_VARS (block_tree);
2102 : : }
2103 : :
2104 : 45662 : if (type_size (type_tree) != 0 && init_tree != NULL_TREE
2105 : 29704 : && TREE_TYPE (init_tree) != void_type_node)
2106 : 3625 : DECL_INITIAL (var) = convert_tree (type_tree, init_tree, location);
2107 : :
2108 : 26079 : if (is_address_taken)
2109 : 3528 : TREE_ADDRESSABLE (var) = 1;
2110 : :
2111 : 26079 : *pstatement = build1_loc (location, DECL_EXPR, void_type_node, var);
2112 : :
2113 : : // For a zero sized type, don't initialize VAR with BINIT, but still
2114 : : // evaluate BINIT for its side effects.
2115 : 26079 : if (init_tree != NULL_TREE
2116 : 26079 : && (type_size (type_tree) == 0
2117 : 3625 : || TREE_TYPE (init_tree) == void_type_node))
2118 : 38 : *pstatement = compound_statement (init_tree, *pstatement);
2119 : :
2120 : 26079 : return new Bvariable (var);
2121 : : }
2122 : :
2123 : : // Make a label.
2124 : :
2125 : : tree
2126 : 667 : label (tree func_tree, const std::string &name, location_t location)
2127 : : {
2128 : 667 : tree decl;
2129 : 667 : if (name.empty ())
2130 : : {
2131 : 631 : if (DECL_STRUCT_FUNCTION (func_tree) == NULL)
2132 : 0 : push_struct_function (func_tree);
2133 : : else
2134 : 631 : push_cfun (DECL_STRUCT_FUNCTION (func_tree));
2135 : :
2136 : 631 : decl = create_artificial_label (location);
2137 : :
2138 : 631 : pop_cfun ();
2139 : : }
2140 : : else
2141 : : {
2142 : 36 : tree id = get_identifier_from_string (name);
2143 : 36 : decl = build_decl (location, LABEL_DECL, id, void_type_node);
2144 : 36 : DECL_CONTEXT (decl) = func_tree;
2145 : : }
2146 : 667 : return decl;
2147 : : }
2148 : :
2149 : : // Make a statement which defines a label.
2150 : :
2151 : : tree
2152 : 667 : label_definition_statement (tree label)
2153 : : {
2154 : 667 : return fold_build1_loc (DECL_SOURCE_LOCATION (label), LABEL_EXPR,
2155 : 667 : void_type_node, label);
2156 : : }
2157 : :
2158 : : // Make a goto statement.
2159 : :
2160 : : tree
2161 : 28 : goto_statement (tree label, location_t location)
2162 : : {
2163 : 28 : return fold_build1_loc (location, GOTO_EXPR, void_type_node, label);
2164 : : }
2165 : :
2166 : : // Get the address of a label.
2167 : :
2168 : : tree
2169 : 0 : label_address (tree label, location_t location)
2170 : : {
2171 : 0 : TREE_USED (label) = 1;
2172 : 0 : TREE_ADDRESSABLE (label) = 1;
2173 : 0 : tree ret = fold_convert_loc (location, ptr_type_node,
2174 : : build_fold_addr_expr_loc (location, label));
2175 : 0 : return ret;
2176 : : }
2177 : :
2178 : : // Declare or define a new function.
2179 : :
2180 : : tree
2181 : 20815 : function (tree functype, const std::string &name, const std::string &asm_name,
2182 : : unsigned int flags, location_t location)
2183 : : {
2184 : 20815 : if (error_operand_p (functype))
2185 : 0 : return error_mark_node;
2186 : :
2187 : 20815 : gcc_assert (FUNCTION_POINTER_TYPE_P (functype));
2188 : 20815 : functype = TREE_TYPE (functype);
2189 : 20815 : tree id = get_identifier_from_string (name);
2190 : 20815 : if (error_operand_p (id))
2191 : 0 : return error_mark_node;
2192 : :
2193 : 20815 : tree decl = build_decl (location, FUNCTION_DECL, id, functype);
2194 : 20815 : if (!asm_name.empty ())
2195 : 9190 : SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
2196 : :
2197 : 20815 : if ((flags & function_is_declaration) != 0)
2198 : 913 : DECL_EXTERNAL (decl) = 1;
2199 : : else
2200 : : {
2201 : 19902 : tree restype = TREE_TYPE (functype);
2202 : 19902 : tree resdecl = build_decl (location, RESULT_DECL, NULL_TREE, restype);
2203 : 19902 : DECL_ARTIFICIAL (resdecl) = 1;
2204 : 19902 : DECL_IGNORED_P (resdecl) = 1;
2205 : 19902 : DECL_CONTEXT (resdecl) = decl;
2206 : 19902 : DECL_RESULT (decl) = resdecl;
2207 : : }
2208 : 20815 : if ((flags & function_is_uninlinable) != 0)
2209 : 0 : DECL_UNINLINABLE (decl) = 1;
2210 : 20815 : if ((flags & function_does_not_return) != 0)
2211 : 0 : TREE_THIS_VOLATILE (decl) = 1;
2212 : 20815 : if ((flags & function_in_unique_section) != 0)
2213 : 0 : resolve_unique_section (decl, 0, 1);
2214 : :
2215 : 20815 : rust_preserve_from_gc (decl);
2216 : 20815 : return decl;
2217 : : }
2218 : :
2219 : : // Create a statement that runs all deferred calls for FUNCTION. This should
2220 : : // be a statement that looks like this in C++:
2221 : : // finish:
2222 : : // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
2223 : :
2224 : : tree
2225 : 0 : function_defer_statement (tree function, tree undefer_tree, tree defer_tree,
2226 : : location_t location)
2227 : : {
2228 : 0 : if (error_operand_p (undefer_tree) || error_operand_p (defer_tree)
2229 : 0 : || error_operand_p (function))
2230 : 0 : return error_mark_node;
2231 : :
2232 : 0 : if (DECL_STRUCT_FUNCTION (function) == NULL)
2233 : 0 : push_struct_function (function);
2234 : : else
2235 : 0 : push_cfun (DECL_STRUCT_FUNCTION (function));
2236 : :
2237 : 0 : tree stmt_list = NULL;
2238 : 0 : tree label = Backend::label (function, "", location);
2239 : 0 : tree label_def = label_definition_statement (label);
2240 : 0 : append_to_statement_list (label_def, &stmt_list);
2241 : :
2242 : 0 : tree jump_stmt = goto_statement (label, location);
2243 : 0 : tree catch_body
2244 : 0 : = build2 (COMPOUND_EXPR, void_type_node, defer_tree, jump_stmt);
2245 : 0 : catch_body = build2 (CATCH_EXPR, void_type_node, NULL, catch_body);
2246 : 0 : tree try_catch
2247 : 0 : = build2 (TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
2248 : 0 : append_to_statement_list (try_catch, &stmt_list);
2249 : 0 : pop_cfun ();
2250 : :
2251 : 0 : return stmt_list;
2252 : : }
2253 : :
2254 : : // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
2255 : : // This will only be called for a function definition.
2256 : :
2257 : : bool
2258 : 13314 : function_set_parameters (tree function,
2259 : : const std::vector<Bvariable *> ¶m_vars)
2260 : : {
2261 : 13314 : if (error_operand_p (function))
2262 : : return false;
2263 : :
2264 : 13314 : tree params = NULL_TREE;
2265 : 13314 : tree *pp = ¶ms;
2266 : 25747 : for (Bvariable *bv : param_vars)
2267 : : {
2268 : 12434 : *pp = bv->get_decl ();
2269 : 12434 : gcc_assert (!error_operand_p (*pp));
2270 : 12433 : pp = &DECL_CHAIN (*pp);
2271 : : }
2272 : 13313 : *pp = NULL_TREE;
2273 : 13313 : DECL_ARGUMENTS (function) = params;
2274 : 13313 : return true;
2275 : : }
2276 : :
2277 : : // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
2278 : : // FUNCTION_DECLS, and VARIABLE_DECLS declared globally, as well as
2279 : : // emit early debugging information.
2280 : :
2281 : : void
2282 : 4253 : write_global_definitions (const std::vector<tree> &type_decls,
2283 : : const std::vector<tree> &constant_decls,
2284 : : const std::vector<tree> &function_decls,
2285 : : const std::vector<Bvariable *> &variable_decls)
2286 : : {
2287 : 4253 : size_t count_definitions = type_decls.size () + constant_decls.size ()
2288 : 4253 : + function_decls.size () + variable_decls.size ();
2289 : :
2290 : 4253 : tree *defs = new tree[count_definitions];
2291 : :
2292 : : // Convert all non-erroneous declarations into Gimple form.
2293 : 4253 : size_t i = 0;
2294 : 4308 : for (Bvariable *bv : variable_decls)
2295 : : {
2296 : 55 : tree v = bv->get_decl ();
2297 : 55 : if (error_operand_p (v))
2298 : 0 : continue;
2299 : 55 : defs[i] = v;
2300 : 55 : rust_preserve_from_gc (defs[i]);
2301 : 55 : ++i;
2302 : : }
2303 : :
2304 : 85970 : for (tree type_tree : type_decls)
2305 : : {
2306 : 81717 : if (!error_operand_p (type_tree) && IS_TYPE_OR_DECL_P (type_tree))
2307 : : {
2308 : 81717 : defs[i] = TYPE_NAME (type_tree);
2309 : 81717 : gcc_assert (defs[i] != NULL);
2310 : 81717 : rust_preserve_from_gc (defs[i]);
2311 : 81717 : ++i;
2312 : : }
2313 : : }
2314 : 4796 : for (tree t : constant_decls)
2315 : : {
2316 : 543 : if (!error_operand_p (t))
2317 : : {
2318 : 535 : defs[i] = t;
2319 : 535 : rust_preserve_from_gc (defs[i]);
2320 : 535 : ++i;
2321 : : }
2322 : : }
2323 : 19585 : for (tree decl : function_decls)
2324 : : {
2325 : 15332 : if (!error_operand_p (decl))
2326 : : {
2327 : 15332 : rust_preserve_from_gc (decl);
2328 : 15332 : if (DECL_STRUCT_FUNCTION (decl) == NULL)
2329 : 3344 : allocate_struct_function (decl, false);
2330 : 15332 : dump_function (TDI_original, decl);
2331 : 15332 : cgraph_node::finalize_function (decl, true);
2332 : :
2333 : 15332 : defs[i] = decl;
2334 : 15332 : ++i;
2335 : : }
2336 : : }
2337 : :
2338 : : // Pass everything back to the middle-end.
2339 : :
2340 : 4253 : wrapup_global_declarations (defs, i);
2341 : :
2342 : 4253 : delete[] defs;
2343 : 4253 : }
2344 : :
2345 : : } // namespace Backend
|