Skip to content

Commit 8c88e8e

Browse files
bors[bot]philbertyCohenArthur
authored
Merge #1015 #1018
1015: Add code generation for the slice type r=philberty a=philberty This type must respect the layout of the FatPtr type in libcore. Rust implements slices using Rustc types in libcore and uses a neat trick. Addresses #849 1018: builtin-macros: Add more documentation for defining builtins r=CohenArthur a=CohenArthur `@mvvsmk` you might find this a little more clear. Sorry about the confusion! Co-authored-by: Philip Herron <[email protected]> Co-authored-by: Arthur Cohen <[email protected]>
3 parents a50fcbc + 040b2ec + c62e9eb commit 8c88e8e

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,38 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
349349
void
350350
TyTyResolveCompile::visit (const TyTy::SliceType &type)
351351
{
352-
// TODO
353-
gcc_unreachable ();
352+
if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type))
353+
return;
354+
355+
std::vector<Backend::typed_identifier> fields;
356+
357+
tree element_type
358+
= TyTyResolveCompile::compile (ctx, type.get_element_type ());
359+
tree data_field_ty = build_pointer_type (element_type);
360+
Backend::typed_identifier data_field ("data", data_field_ty, Location ());
361+
fields.push_back (std::move (data_field));
362+
363+
// lookup usize
364+
TyTy::BaseType *usize = nullptr;
365+
bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
366+
rust_assert (ok);
367+
368+
tree len_field_ty = TyTyResolveCompile::compile (ctx, usize);
369+
Backend::typed_identifier len_field ("len", len_field_ty, Location ());
370+
fields.push_back (std::move (len_field));
371+
372+
tree type_record = ctx->get_backend ()->struct_type (fields);
373+
374+
std::string named_struct_str
375+
= std::string ("[") + type.get_element_type ()->get_name () + "]";
376+
tree named_struct
377+
= ctx->get_backend ()->named_type (named_struct_str, type_record,
378+
type.get_ident ().locus);
379+
380+
ctx->push_type (named_struct);
381+
translated = named_struct;
382+
383+
ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
354384
}
355385

356386
void

gcc/rust/expand/rust-macro-builtins.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@
2222
#include "rust-ast.h"
2323
#include "rust-location.h"
2424

25+
/**
26+
* This class provides a list of builtin macros implemented by the compiler.
27+
* The functions defined are called "builtin transcribers" in that they replace
28+
* the transcribing part of a macro definition.
29+
*
30+
* Like regular macro transcribers, they are responsible for building and
31+
* returning an AST fragment: basically a vector of AST nodes put together.
32+
*
33+
* Unlike regular declarative macros where each match arm has its own associated
34+
* transcriber, builtin transcribers are responsible for handling all match arms
35+
* of the macro. This means that you should take extra care when implementing a
36+
* builtin containing multiple match arms: You will probably need to do some
37+
* lookahead in order to determine which match arm the user intended to use.
38+
*
39+
* An example of this is the `assert!()` macro:
40+
*
41+
* ```
42+
* macro_rules! assert {
43+
* ($cond:expr $(,)?) => {{ ... }};
44+
* ($cond : expr, $ ($arg : tt) +) = > {{ ... }};
45+
* }
46+
* ```
47+
*
48+
* If more tokens exist beyond the optional comma, they need to be handled as
49+
* a token-tree for a custom panic message.
50+
*
51+
* These builtin macros with empty transcribers are defined in the standard
52+
* library. They are marked with a special attribute, `#[rustc_builtin_macro]`.
53+
* When this attribute is present on a macro definition, the compiler should
54+
* look for an associated transcriber in the mappings. Meaning that you must
55+
* remember to insert your transcriber in the `builtin_macros` map of the
56+
*`Mappings`.
57+
*
58+
* This map is built as a static variable in the `insert_macro_def()` method
59+
* of the `Mappings` class.
60+
*/
61+
2562
namespace Rust {
2663
class MacroBuiltin
2764
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// { dg-additional-options "-w" }
2+
struct FatPtr<T> {
3+
data: *const T,
4+
len: usize,
5+
}
6+
7+
union Repr<T> {
8+
rust: *const [T],
9+
rust_mut: *mut [T],
10+
raw: FatPtr<T>,
11+
}
12+
13+
const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
14+
unsafe {
15+
let a = FatPtr { data, len };
16+
let b = Repr { raw: a };
17+
b.rust
18+
}
19+
}
20+
21+
fn main() -> i32 {
22+
let a = 123;
23+
let b: *const i32 = &a;
24+
let c = slice_from_raw_parts(b, 1);
25+
26+
0
27+
}

0 commit comments

Comments
 (0)