Debian Codegraph @

gcc/rust/backend/rust-compile-type.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
2
3 // This file is part of GCC.
4
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
9
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include "rust-compile-type.h"
20 #include "rust-compile-expr.h"
21 #include "rust-constexpr.h"
22 #include "rust-gcc.h"
23
24 #include "tree.h"
25 #include "stor-layout.h"
26
27 namespace Rust {
28 namespace Compile {
29
30 static const std::string RUST_ENUM_DISR_FIELD_NAME = "RUST$ENUM$DISR";
31
32 TyTyResolveCompile::TyTyResolveCompile (Context *ctx, bool trait_object_mode)
33 : ctx (ctx), trait_object_mode (trait_object_mode),
34 translated (error_mark_node)
35 {}
36
37 tree
38 TyTyResolveCompile::compile (Context *ctx, const TyTy::BaseType *ty,
39 bool trait_object_mode)
40 {
41 TyTyResolveCompile compiler (ctx, trait_object_mode);
42 const TyTy::BaseType *destructured = ty->destructure ();
43 destructured->accept_vis (compiler);
44
45 if (compiler.translated != error_mark_node
46 && TYPE_NAME (compiler.translated) != NULL)
47 {
48 // canonicalize the type
49 compiler.translated = ctx->insert_compiled_type (compiler.translated);
50 }
51
52 return compiler.translated;
53 }
54
55 // see: gcc/c/c-decl.cc:8230-8241
56 // https://github.com/Rust-GCC/gccrs/blob/0024bc2f028369b871a65ceb11b2fddfb0f9c3aa/gcc/c/c-decl.c#L8229-L8241
57 tree
58 TyTyResolveCompile::get_implicit_enumeral_node_type (TyTy::BaseType *repr)
59 {
60 // static tree enum_node = NULL_TREE;
61 // if (enum_node == NULL_TREE)
62 // {
63 // enum_node = make_node (ENUMERAL_TYPE);
64 // SET_TYPE_MODE (enum_node, TYPE_MODE (unsigned_type_node));
65 // SET_TYPE_ALIGN (enum_node, TYPE_ALIGN (unsigned_type_node));
66 // TYPE_USER_ALIGN (enum_node) = 0;
67 // TYPE_UNSIGNED (enum_node) = 1;
68 // TYPE_PRECISION (enum_node) = TYPE_PRECISION (unsigned_type_node);
69 // TYPE_MIN_VALUE (enum_node) = TYPE_MIN_VALUE (unsigned_type_node);
70 // TYPE_MAX_VALUE (enum_node) = TYPE_MAX_VALUE (unsigned_type_node);
71
72 // // tree identifier = ctx->get_backend ()->get_identifier_node
73 // // ("enumeral"); tree enum_decl
74 // // = build_decl (BUILTINS_LOCATION, TYPE_DECL, identifier,
75 // enum_node);
76 // // TYPE_NAME (enum_node) = enum_decl;
77 // }
78 // return enum_node;
79
80 return compile (ctx, repr);
81 }
82
83 tree
84 TyTyResolveCompile::get_unit_type (Context *ctx)
85 {
86 static tree unit_type;
87 if (unit_type == nullptr)
88 {
89 auto cn = ctx->get_mappings ().get_current_crate ();
90 auto &c = ctx->get_mappings ().get_ast_crate (cn);
91 location_t locus = BUILTINS_LOCATION;
92 if (c.items.size () > 0)
93 {
94 auto &item = c.items[0];
95 locus = item->get_locus ();
96 }
97
98 auto unit_type_node = Backend::struct_type ({});
99 unit_type = Backend::named_type ("()", unit_type_node, locus);
100 }
101 return unit_type;
102 }
103
104 void
105 TyTyResolveCompile::visit (const TyTy::ErrorType &)
106 {
107 translated = error_mark_node;
108 }
109
110 void
111 TyTyResolveCompile::visit (const TyTy::InferType &type)
112 {
113 const TyTy::BaseType *orig = &type;
114 TyTy::BaseType *lookup = nullptr;
115 bool ok = ctx->get_tyctx ()->lookup_type (type.get_ref (), &lookup);
116 if (!ok)
117 {
118 translated = error_mark_node;
119 return;
120 }
121
122 if (orig == lookup)
123 {
124 translated = error_mark_node;
125 return;
126 }
127
128 translated = TyTyResolveCompile::compile (ctx, lookup);
129 }
130
131 void
132 TyTyResolveCompile::visit (const TyTy::ParamType &)
133 {
134 translated = error_mark_node;
135 }
136
137 void
138 TyTyResolveCompile::visit (const TyTy::ProjectionType &type)
139 {
140 translated = error_mark_node;
141 }
142
143 void
144 TyTyResolveCompile::visit (const TyTy::PlaceholderType &type)
145 {
146 translated = error_mark_node;
147 }
148
149 void
150 TyTyResolveCompile::visit (const TyTy::ClosureType &type)
151 {
152 auto &mappings = ctx->get_mappings ();
153
154 std::vector<Backend::typed_identifier> fields;
155
156 size_t i = 0;
157 for (const auto &capture : type.get_captures ())
158 {
159 // lookup the HirId
160 tl::optional<HirId> hid = mappings.lookup_node_to_hir (capture);
161 rust_assert (hid.has_value ());
162 auto ref = hid.value ();
163
164 // lookup the var decl type
165 TyTy::BaseType *lookup = nullptr;
166 bool found = ctx->get_tyctx ()->lookup_type (ref, &lookup);
167 rust_assert (found);
168
169 // FIXME get the var pattern name
170 std::string mappings_name = "capture_" + std::to_string (i);
171
172 // FIXME
173 // this should be based on the closure move-ability
174 tree decl_type = TyTyResolveCompile::compile (ctx, lookup);
175 tree capture_type = build_reference_type (decl_type);
176 fields.push_back (Backend::typed_identifier (mappings_name, capture_type,
177 type.get_ident ().locus));
178 }
179
180 tree type_record = Backend::struct_type (fields);
181 RS_CLOSURE_FLAG (type_record) = 1;
182
183 std::string named_struct_str
184 = type.get_ident ().path.get () + "::{{closure}}";
185 translated = Backend::named_type (named_struct_str, type_record,
186 type.get_ident ().locus);
187 }
188
189 void
190 TyTyResolveCompile::visit (const TyTy::FnType &type)
191 {
192 Backend::typed_identifier receiver;
193 std::vector<Backend::typed_identifier> parameters;
194 std::vector<Backend::typed_identifier> results;
195
196 // we can only return unit-type if its not the C ABI because it will expect
197 // void
198 auto hir_type = type.get_return_type ()->destructure ();
199 bool return_is_unit = hir_type->is_unit ();
200 bool is_c_abi = type.get_abi () == ABI::C;
201 bool should_be_void = is_c_abi && return_is_unit;
202 if (!should_be_void)
203 {
204 auto ret = TyTyResolveCompile::compile (ctx, hir_type, trait_object_mode);
205 location_t return_type_locus
206 = ctx->get_mappings ().lookup_location (hir_type->get_ref ());
207 results.push_back (
208 Backend::typed_identifier ("_", ret, return_type_locus));
209 }
210
211 for (auto &param_pair : type.get_params ())
212 {
213 auto param_tyty = param_pair.get_type ();
214 auto compiled_param_type
215 = TyTyResolveCompile::compile (ctx, param_tyty, trait_object_mode);
216
217 auto compiled_param = Backend::typed_identifier (
218 param_pair.get_pattern ().as_string (), compiled_param_type,
219 ctx->get_mappings ().lookup_location (param_tyty->get_ref ()));
220
221 parameters.push_back (compiled_param);
222 }
223
224 if (!type.is_variadic ())
225 translated = Backend::function_type (receiver, parameters, results, NULL,
226 type.get_ident ().locus);
227 else
228 translated
229 = Backend::function_type_variadic (receiver, parameters, results, NULL,
230 type.get_ident ().locus);
231 }
232
233 void
234 TyTyResolveCompile::visit (const TyTy::FnPtr &type)
235 {
236 tree result_type = TyTyResolveCompile::compile (ctx, type.get_return_type ());
237
238 std::vector<tree> parameters;
239
240 auto &params = type.get_params ();
241 for (auto &p : params)
242 {
243 tree pty = TyTyResolveCompile::compile (ctx, p.get_tyty ());
244 parameters.push_back (pty);
245 }
246
247 translated = Backend::function_ptr_type (result_type, parameters,
248 type.get_ident ().locus);
249 }
250
251 void
252 TyTyResolveCompile::visit (const TyTy::ADTType &type)
253 {
254 tree type_record = error_mark_node;
255 if (!type.is_enum ())
256 {
257 rust_assert (type.number_of_variants () == 1);
258
259 TyTy::VariantDef &variant = *type.get_variants ().at (0);
260 std::vector<Backend::typed_identifier> fields;
261 for (size_t i = 0; i < variant.num_fields (); i++)
262 {
263 const TyTy::StructFieldType *field = variant.get_field_at_index (i);
264 tree compiled_field_ty
265 = TyTyResolveCompile::compile (ctx, field->get_field_type ());
266
267 Backend::typed_identifier f (field->get_name (), compiled_field_ty,
268 ctx->get_mappings ().lookup_location (
269 type.get_ty_ref ()));
270 fields.push_back (std::move (f));
271 }
272
273 type_record = type.is_union () ? Backend::union_type (fields, false)
274 : Backend::struct_type (fields, false);
275 }
276 else
277 {
278 // see:
279 // https://github.com/bminor/binutils-gdb/blob/527b8861cd472385fa9160a91dd6d65a25c41987/gdb/dwarf2/read.c#L9010-L9241
280 //
281 // enums are actually a big union so for example the rust enum:
282 //
283 // enum AnEnum {
284 // A,
285 // B,
286 // C (char),
287 // D { x: i64, y: i64 },
288 // }
289 //
290 // we actually turn this into
291 //
292 // union {
293 // struct A { int RUST$ENUM$DISR; }; <- this is a data-less variant
294 // struct B { int RUST$ENUM$DISR; }; <- this is a data-less variant
295 // struct C { int RUST$ENUM$DISR; char __0; };
296 // struct D { int RUST$ENUM$DISR; i64 x; i64 y; };
297 // }
298 //
299 // Ada, qual_union_types might still work for this but I am not 100% sure.
300 // I ran into some issues lets reuse our normal union and ask Ada people
301 // about it.
302 //
303 // I think the above is actually wrong and it should actually be this
304 //
305 // struct {
306 // int RUST$ENUM$DISR; // take into account the repr for this TODO
307 // union {
308 // // Variant A
309 // struct {
310 // // No additional fields
311 // } A;
312
313 // // Variant B
314 // struct {
315 // // No additional fields
316 // } B;
317
318 // // Variant C
319 // struct {
320 // char c;
321 // } C;
322
323 // // Variant D
324 // struct {
325 // int64_t x;
326 // int64_t y;
327 // } D;
328 // } payload; // The union of all variant data
329 // };
330
331 std::vector<tree> variant_records;
332 for (auto &variant : type.get_variants ())
333 {
334 std::vector<Backend::typed_identifier> fields;
335 for (size_t i = 0; i < variant->num_fields (); i++)
336 {
337 const TyTy::StructFieldType *field
338 = variant->get_field_at_index (i);
339 tree compiled_field_ty
340 = TyTyResolveCompile::compile (ctx, field->get_field_type ());
341
342 std::string field_name = field->get_name ();
343 if (variant->get_variant_type ()
344 == TyTy::VariantDef::VariantType::TUPLE)
345 field_name = "__" + field->get_name ();
346
347 Backend::typed_identifier f (
348 field_name, compiled_field_ty,
349 ctx->get_mappings ().lookup_location (type.get_ty_ref ()));
350 fields.push_back (std::move (f));
351 }
352
353 tree variant_record = Backend::struct_type (fields);
354 tree named_variant_record
355 = Backend::named_type (variant->get_ident ().path.get (),
356 variant_record, variant->get_ident ().locus);
357
358 // add them to the list
359 variant_records.push_back (named_variant_record);
360 }
361
362 // now we need to make the actual union, but first we need to make
363 // named_type TYPE_DECL's out of the variants
364
365 size_t i = 0;
366 std::vector<Backend::typed_identifier> enum_fields;
367 for (auto &variant_record : variant_records)
368 {
369 TyTy::VariantDef *variant = type.get_variants ().at (i++);
370 std::string implicit_variant_name = variant->get_identifier ();
371
372 Backend::typed_identifier f (implicit_variant_name, variant_record,
373 ctx->get_mappings ().lookup_location (
374 type.get_ty_ref ()));
375 enum_fields.push_back (std::move (f));
376 }
377
378 //
379 location_t locus = ctx->get_mappings ().lookup_location (type.get_ref ());
380
381 // finally make the union or the enum
382 tree variants_union = Backend::union_type (enum_fields, false);
383 layout_type (variants_union);
384 tree named_union_record
385 = Backend::named_type ("payload", variants_union, locus);
386
387 // create the overall struct
388 tree enumeral_type = TyTyResolveCompile::get_implicit_enumeral_node_type (
389 type.get_repr_options ().repr);
390 Backend::typed_identifier discrim (RUST_ENUM_DISR_FIELD_NAME,
391 enumeral_type, locus);
392 Backend::typed_identifier variants_union_field ("payload",
393 named_union_record,
394 locus);
395
396 std::vector<Backend::typed_identifier> fields
397 = {discrim, variants_union_field};
398 type_record = Backend::struct_type (fields, false);
399 }
400
401 // Handle repr options
402 // TODO: "packed" should only narrow type alignment and "align" should only
403 // widen it. Do we need to check and enforce this here, or is it taken care of
404 // later on in the gcc middle-end?
405 TyTy::ADTType::ReprOptions repr = type.get_repr_options ();
406 if (repr.pack)
407 {
408 TYPE_PACKED (type_record) = 1;
409 if (repr.pack > 1)
410 {
411 SET_TYPE_ALIGN (type_record, repr.pack * 8);
412 TYPE_USER_ALIGN (type_record) = 1;
413 }
414 }
415 else if (repr.align)
416 {
417 SET_TYPE_ALIGN (type_record, repr.align * 8);
418 TYPE_USER_ALIGN (type_record) = 1;
419 }
420 layout_type (type_record);
421
422 std::string named_struct_str
423 = type.get_ident ().path.get () + type.subst_as_string ();
424 translated = Backend::named_type (named_struct_str, type_record,
425 type.get_ident ().locus);
426 }
427
428 void
429 TyTyResolveCompile::visit (const TyTy::TupleType &type)
430 {
431 if (type.num_fields () == 0)
432 {
433 translated = get_unit_type (ctx);
434 return;
435 }
436
437 // create implicit struct
438 std::vector<Backend::typed_identifier> fields;
439 for (size_t i = 0; i < type.num_fields (); i++)
440 {
441 TyTy::BaseType *field = type.get_field (i);
442 tree compiled_field_ty = TyTyResolveCompile::compile (ctx, field);
443
444 // rustc uses the convention __N, where N is an integer, to
445 // name the fields of a tuple. We follow this as well,
446 // because this is used by GDB. One further reason to prefer
447 // this, rather than simply emitting the integer, is that this
448 // approach makes it simpler to use a C-only debugger, or
449 // GDB's C mode, when debugging Rust.
450 Backend::typed_identifier f ("__" + std::to_string (i), compiled_field_ty,
451 ctx->get_mappings ().lookup_location (
452 type.get_ty_ref ()));
453 fields.push_back (std::move (f));
454 }
455
456 tree struct_type_record = Backend::struct_type (fields);
457 translated = Backend::named_type (type.as_string (), struct_type_record,
458 type.get_ident ().locus);
459 }
460
461 void
462 TyTyResolveCompile::visit (const TyTy::ArrayType &type)
463 {
464 tree element_type
465 = TyTyResolveCompile::compile (ctx, type.get_element_type ());
466
467 ctx->push_const_context ();
468
469 HIR::Expr &hir_capacity_expr = type.get_capacity_expr ();
470 TyTy::BaseType *capacity_expr_ty = nullptr;
471 bool ok = ctx->get_tyctx ()->lookup_type (
472 hir_capacity_expr.get_mappings ().get_hirid (), &capacity_expr_ty);
473 rust_assert (ok);
474 tree capacity_expr = HIRCompileBase::compile_constant_expr (
475 ctx, hir_capacity_expr.get_mappings ().get_hirid (), capacity_expr_ty,
476 capacity_expr_ty, Resolver::CanonicalPath::create_empty (),
477 hir_capacity_expr, type.get_locus (), hir_capacity_expr.get_locus ());
478
479 ctx->pop_const_context ();
480
481 tree folded_capacity_expr = fold_expr (capacity_expr);
482
483 translated = Backend::array_type (element_type, folded_capacity_expr);
484 if (translated != error_mark_node)
485 translated = ctx->insert_compiled_type (translated);
486 }
487
488 void
489 TyTyResolveCompile::visit (const TyTy::SliceType &type)
490 {
491 tree type_record = create_slice_type_record (type);
492
493 std::string named_struct_str
494 = std::string ("[") + type.get_element_type ()->get_name () + "]";
495 translated = Backend::named_type (named_struct_str, type_record,
496 type.get_ident ().locus);
497 }
498
499 void
500 TyTyResolveCompile::visit (const TyTy::BoolType &)
501 {
502 translated
503 = Backend::named_type ("bool", boolean_type_node, BUILTINS_LOCATION);
504 }
505
506 void
507 TyTyResolveCompile::visit (const TyTy::IntType &type)
508 {
509 switch (type.get_int_kind ())
510 {
511 case TyTy::IntType::I8:
512 translated = Backend::named_type ("i8", Backend::integer_type (false, 8),
513 BUILTINS_LOCATION);
514 return;
515
516 case TyTy::IntType::I16:
517 translated
518 = Backend::named_type ("i16", Backend::integer_type (false, 16),
519 BUILTINS_LOCATION);
520 return;
521
522 case TyTy::IntType::I32:
523 translated
524 = Backend::named_type ("i32", Backend::integer_type (false, 32),
525 BUILTINS_LOCATION);
526 return;
527
528 case TyTy::IntType::I64:
529 translated
530 = Backend::named_type ("i64", Backend::integer_type (false, 64),
531 BUILTINS_LOCATION);
532 return;
533
534 case TyTy::IntType::I128:
535 translated
536 = Backend::named_type ("i128", Backend::integer_type (false, 128),
537 BUILTINS_LOCATION);
538 return;
539 }
540 }
541
542 void
543 TyTyResolveCompile::visit (const TyTy::UintType &type)
544 {
545 switch (type.get_uint_kind ())
546 {
547 case TyTy::UintType::U8:
548 translated = Backend::named_type ("u8", Backend::integer_type (true, 8),
549 BUILTINS_LOCATION);
550 return;
551
552 case TyTy::UintType::U16:
553 translated = Backend::named_type ("u16", Backend::integer_type (true, 16),
554 BUILTINS_LOCATION);
555 return;
556
557 case TyTy::UintType::U32:
558 translated = Backend::named_type ("u32", Backend::integer_type (true, 32),
559 BUILTINS_LOCATION);
560 return;
561
562 case TyTy::UintType::U64:
563 translated = Backend::named_type ("u64", Backend::integer_type (true, 64),
564 BUILTINS_LOCATION);
565 return;
566
567 case TyTy::UintType::U128:
568 translated
569 = Backend::named_type ("u128", Backend::integer_type (true, 128),
570 BUILTINS_LOCATION);
571 return;
572 }
573 }
574
575 void
576 TyTyResolveCompile::visit (const TyTy::FloatType &type)
577 {
578 switch (type.get_float_kind ())
579 {
580 case TyTy::FloatType::F32:
581 translated = Backend::named_type ("f32", Backend::float_type (32),
582 BUILTINS_LOCATION);
583 return;
584
585 case TyTy::FloatType::F64:
586 translated = Backend::named_type ("f64", Backend::float_type (64),
587 BUILTINS_LOCATION);
588 return;
589 }
590 }
591
592 void
593 TyTyResolveCompile::visit (const TyTy::USizeType &)
594 {
595 translated
596 = Backend::named_type ("usize",
597 Backend::integer_type (true,
598 Backend::get_pointer_size ()),
599 BUILTINS_LOCATION);
600 }
601
602 void
603 TyTyResolveCompile::visit (const TyTy::ISizeType &)
604 {
605 translated
606 = Backend::named_type ("isize",
607 Backend::integer_type (false,
608 Backend::get_pointer_size ()),
609 BUILTINS_LOCATION);
610 }
611
612 void
613 TyTyResolveCompile::visit (const TyTy::CharType &)
614 {
615 translated
616 = Backend::named_type ("char", Backend::wchar_type (), BUILTINS_LOCATION);
617 }
618
619 void
620 TyTyResolveCompile::visit (const TyTy::ReferenceType &type)
621 {
622 const TyTy::SliceType *slice = nullptr;
623 const TyTy::StrType *str = nullptr;
624 const TyTy::DynamicObjectType *dyn = nullptr;
625 if (type.is_dyn_slice_type (&slice))
626 {
627 tree type_record = create_slice_type_record (*slice);
628 std::string dyn_slice_type_str
629 = std::string (type.is_mutable () ? "&mut " : "&") + "["
630 + slice->get_element_type ()->get_name () + "]";
631
632 translated = Backend::named_type (dyn_slice_type_str, type_record,
633 slice->get_locus ());
634
635 return;
636 }
637 else if (type.is_dyn_str_type (&str))
638 {
639 tree type_record = create_str_type_record (*str);
640 std::string dyn_str_type_str
641 = std::string (type.is_mutable () ? "&mut " : "&") + "str";
642
643 translated = Backend::named_type (dyn_str_type_str, type_record,
644 str->get_locus ());
645
646 return;
647 }
648 else if (type.is_dyn_obj_type (&dyn))
649 {
650 tree type_record = create_dyn_obj_record (*dyn);
651 std::string dyn_str_type_str
652 = std::string (type.is_mutable () ? "&mut " : "& ") + dyn->get_name ();
653
654 translated = Backend::named_type (dyn_str_type_str, type_record,
655 dyn->get_locus ());
656
657 return;
658 }
659
660 tree base_compiled_type
661 = TyTyResolveCompile::compile (ctx, type.get_base (), trait_object_mode);
662 if (type.is_mutable ())
663 {
664 translated = Backend::reference_type (base_compiled_type);
665 }
666 else
667 {
668 auto base = Backend::immutable_type (base_compiled_type);
669 translated = Backend::reference_type (base);
670 }
671 }
672
673 void
674 TyTyResolveCompile::visit (const TyTy::PointerType &type)
675 {
676 const TyTy::SliceType *slice = nullptr;
677 const TyTy::StrType *str = nullptr;
678 const TyTy::DynamicObjectType *dyn = nullptr;
679 if (type.is_dyn_slice_type (&slice))
680 {
681 tree type_record = create_slice_type_record (*slice);
682 std::string dyn_slice_type_str
683 = std::string (type.is_mutable () ? "*mut " : "*const ") + "["
684 + slice->get_element_type ()->get_name () + "]";
685
686 translated = Backend::named_type (dyn_slice_type_str, type_record,
687 slice->get_locus ());
688
689 return;
690 }
691 else if (type.is_dyn_str_type (&str))
692 {
693 tree type_record = create_str_type_record (*str);
694 std::string dyn_str_type_str
695 = std::string (type.is_mutable () ? "*mut " : "*const ") + "str";
696
697 translated = Backend::named_type (dyn_str_type_str, type_record,
698 str->get_locus ());
699
700 return;
701 }
702 else if (type.is_dyn_obj_type (&dyn))
703 {
704 tree type_record = create_dyn_obj_record (*dyn);
705 std::string dyn_str_type_str
706 = std::string (type.is_mutable () ? "*mut " : "*const ")
707 + dyn->get_name ();
708
709 translated = Backend::named_type (dyn_str_type_str, type_record,
710 dyn->get_locus ());
711
712 return;
713 }
714
715 tree base_compiled_type
716 = TyTyResolveCompile::compile (ctx, type.get_base (), trait_object_mode);
717 if (type.is_mutable ())
718 {
719 translated = Backend::pointer_type (base_compiled_type);
720 }
721 else
722 {
723 auto base = Backend::immutable_type (base_compiled_type);
724 translated = Backend::pointer_type (base);
725 }
726 }
727
728 void
729 TyTyResolveCompile::visit (const TyTy::StrType &type)
730 {
731 tree raw_str = create_str_type_record (type);
732 translated = Backend::named_type ("str", raw_str, BUILTINS_LOCATION);
733 }
734
735 void
736 TyTyResolveCompile::visit (const TyTy::NeverType &)
737 {
738 translated = get_unit_type (ctx);
739 }
740
741 void
742 TyTyResolveCompile::visit (const TyTy::DynamicObjectType &type)
743 {
744 if (trait_object_mode)
745 {
746 translated = Backend::integer_type (true, Backend::get_pointer_size ());
747 return;
748 }
749
750 tree type_record = create_dyn_obj_record (type);
751 translated = Backend::named_type (type.get_name (), type_record,
752 type.get_ident ().locus);
753 }
754
755 void
756 TyTyResolveCompile::visit (const TyTy::OpaqueType &type)
757 {
758 translated = error_mark_node;
759 }
760
761 tree
762 TyTyResolveCompile::create_dyn_obj_record (const TyTy::DynamicObjectType &type)
763 {
764 // create implicit struct
765 auto items = type.get_object_items ();
766 std::vector<Backend::typed_identifier> fields;
767
768 tree uint = Backend::integer_type (true, Backend::get_pointer_size ());
769 tree uintptr_ty = build_pointer_type (uint);
770
771 Backend::typed_identifier f ("pointer", uintptr_ty,
772 ctx->get_mappings ().lookup_location (
773 type.get_ty_ref ()));
774 fields.push_back (std::move (f));
775
776 tree vtable_size = build_int_cst (size_type_node, items.size ());
777 tree vtable_type = Backend::array_type (uintptr_ty, vtable_size);
778 Backend::typed_identifier vtf ("vtable", vtable_type,
779 ctx->get_mappings ().lookup_location (
780 type.get_ty_ref ()));
781 fields.push_back (std::move (vtf));
782
783 tree record = Backend::struct_type (fields);
784 RS_DST_FLAG (record) = 1;
785 TYPE_MAIN_VARIANT (record) = ctx->insert_main_variant (record);
786
787 return record;
788 }
789
790 tree
791 TyTyResolveCompile::create_slice_type_record (const TyTy::SliceType &type)
792 {
793 // lookup usize
794 TyTy::BaseType *usize = nullptr;
795 bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
796 rust_assert (ok);
797
798 tree element_type
799 = TyTyResolveCompile::compile (ctx, type.get_element_type ());
800 tree data_field_ty = build_pointer_type (element_type);
801 Backend::typed_identifier data_field ("data", data_field_ty,
802 type.get_locus ());
803
804 tree len_field_ty = TyTyResolveCompile::compile (ctx, usize);
805 Backend::typed_identifier len_field ("len", len_field_ty, type.get_locus ());
806
807 tree record = Backend::struct_type ({data_field, len_field});
808 RS_DST_FLAG (record) = 1;
809 TYPE_MAIN_VARIANT (record) = ctx->insert_main_variant (record);
810
811 return record;
812 }
813
814 tree
815 TyTyResolveCompile::create_str_type_record (const TyTy::StrType &type)
816 {
817 // lookup usize
818 TyTy::BaseType *usize = nullptr;
819 bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
820 rust_assert (ok);
821
822 tree char_ptr = build_pointer_type (char_type_node);
823 tree const_char_type = build_qualified_type (char_ptr, TYPE_QUAL_CONST);
824
825 tree element_type = const_char_type;
826 tree data_field_ty = build_pointer_type (element_type);
827 Backend::typed_identifier data_field ("data", data_field_ty,
828 type.get_locus ());
829
830 tree len_field_ty = TyTyResolveCompile::compile (ctx, usize);
831 Backend::typed_identifier len_field ("len", len_field_ty, type.get_locus ());
832
833 tree record = Backend::struct_type ({data_field, len_field});
834 RS_DST_FLAG (record) = 1;
835 TYPE_MAIN_VARIANT (record) = ctx->insert_main_variant (record);
836
837 return record;
838 }
839
840 } // namespace Compile
841 } // namespace Rust