2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::expand::allocator::{
|
2023-08-06 14:20:31 +00:00
|
|
|
ALLOCATOR_METHODS, AllocatorMethod, AllocatorMethodInput, AllocatorTy, global_fn_name,
|
2020-02-29 17:37:32 +00:00
|
|
|
};
|
|
|
|
use rustc_ast::ptr::P;
|
2022-08-17 02:34:33 +00:00
|
|
|
use rustc_ast::{
|
|
|
|
self as ast, AttrVec, Expr, Fn, FnHeader, FnSig, Generics, ItemKind, Mutability, Param, Safety,
|
2024-05-17 17:17:48 +00:00
|
|
|
Stmt, StmtKind, Ty, TyKind,
|
|
|
|
};
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::base::{Annotatable, ExtCtxt};
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{Ident, Span, Symbol, kw, sym};
|
2022-11-23 00:55:16 +00:00
|
|
|
use thin_vec::{ThinVec, thin_vec};
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2023-06-21 11:01:53 +00:00
|
|
|
use crate::errors;
|
2019-10-08 12:15:26 +00:00
|
|
|
use crate::util::check_builtin_macro_attribute;
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-04-25 21:56:48 +00:00
|
|
|
pub(crate) fn expand(
|
2019-07-18 21:24:58 +00:00
|
|
|
ecx: &mut ExtCtxt<'_>,
|
2019-07-18 23:10:36 +00:00
|
|
|
_span: Span,
|
2019-07-18 21:24:58 +00:00
|
|
|
meta_item: &ast::MetaItem,
|
2021-03-25 19:40:50 +00:00
|
|
|
item: Annotatable,
|
2019-07-18 21:24:58 +00:00
|
|
|
) -> Vec<Annotatable> {
|
2019-07-18 23:10:36 +00:00
|
|
|
check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
|
2018-05-20 22:04:00 +00:00
|
|
|
|
2021-03-25 19:40:50 +00:00
|
|
|
let orig_item = item.clone();
|
2020-11-23 00:32:39 +00:00
|
|
|
|
|
|
|
// Allow using `#[global_allocator]` on an item statement
|
2021-03-25 19:40:50 +00:00
|
|
|
// FIXME - if we get deref patterns, use them to reduce duplication here
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
let (item, ident, is_stmt, ty_span) = if let Annotatable::Item(item) = &item
|
|
|
|
&& let ItemKind::Static(box ast::StaticItem { ident, ty, .. }) = &item.kind
|
2022-12-06 13:22:36 +00:00
|
|
|
{
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
(item, *ident, false, ecx.with_def_site_ctxt(ty.span))
|
2022-12-06 13:22:36 +00:00
|
|
|
} else if let Annotatable::Stmt(stmt) = &item
|
|
|
|
&& let StmtKind::Item(item) = &stmt.kind
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
&& let ItemKind::Static(box ast::StaticItem { ident, ty, .. }) = &item.kind
|
2022-12-06 13:22:36 +00:00
|
|
|
{
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
(item, *ident, true, ecx.with_def_site_ctxt(ty.span))
|
2022-12-06 13:22:36 +00:00
|
|
|
} else {
|
2023-12-18 09:54:03 +00:00
|
|
|
ecx.dcx().emit_err(errors::AllocMustStatics { span: item.span() });
|
2022-12-17 23:29:25 +00:00
|
|
|
return vec![orig_item];
|
2022-12-06 13:22:36 +00:00
|
|
|
};
|
2019-07-18 21:24:58 +00:00
|
|
|
|
|
|
|
// Generate a bunch of new items using the AllocFnFactory
|
2019-09-14 20:17:11 +00:00
|
|
|
let span = ecx.with_def_site_ctxt(item.span);
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
let f = AllocFnFactory { span, ty_span, global: ident, cx: ecx };
|
2019-07-18 21:24:58 +00:00
|
|
|
|
2019-07-18 23:51:07 +00:00
|
|
|
// Generate item statements for the allocator methods.
|
|
|
|
let stmts = ALLOCATOR_METHODS.iter().map(|method| f.allocator_fn(method)).collect();
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2019-07-18 23:51:07 +00:00
|
|
|
// Generate anonymous constant serving as container for the allocator methods.
|
2022-11-23 00:55:16 +00:00
|
|
|
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
|
2019-07-18 23:51:07 +00:00
|
|
|
let const_body = ecx.expr_block(ecx.block(span, stmts));
|
2019-09-14 20:16:51 +00:00
|
|
|
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
|
2020-11-23 00:32:39 +00:00
|
|
|
let const_item = if is_stmt {
|
|
|
|
Annotatable::Stmt(P(ecx.stmt_item(span, const_item)))
|
|
|
|
} else {
|
|
|
|
Annotatable::Item(const_item)
|
|
|
|
};
|
2018-06-06 00:24:10 +00:00
|
|
|
|
2019-07-18 23:51:07 +00:00
|
|
|
// Return the original item and the new methods.
|
2020-11-23 00:32:39 +00:00
|
|
|
vec![orig_item, const_item]
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 21:24:58 +00:00
|
|
|
struct AllocFnFactory<'a, 'b> {
|
2017-06-03 21:54:08 +00:00
|
|
|
span: Span,
|
2021-12-15 01:40:08 +00:00
|
|
|
ty_span: Span,
|
2017-06-03 21:54:08 +00:00
|
|
|
global: Ident,
|
2024-09-11 23:25:11 +00:00
|
|
|
cx: &'a ExtCtxt<'b>,
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 21:24:58 +00:00
|
|
|
impl AllocFnFactory<'_, '_> {
|
2019-07-18 23:51:07 +00:00
|
|
|
fn allocator_fn(&self, method: &AllocatorMethod) -> Stmt {
|
2022-11-23 00:55:16 +00:00
|
|
|
let mut abi_args = ThinVec::new();
|
2023-08-06 14:20:31 +00:00
|
|
|
let args = method.inputs.iter().map(|input| self.arg_ty(input, &mut abi_args)).collect();
|
2017-06-03 21:54:08 +00:00
|
|
|
let result = self.call_allocator(method.name, args);
|
2023-08-06 06:06:23 +00:00
|
|
|
let output_ty = self.ret_ty(&method.output);
|
2020-02-15 03:10:59 +00:00
|
|
|
let decl = self.cx.fn_decl(abi_args, ast::FnRetTy::Ty(output_ty));
|
2024-05-17 17:17:48 +00:00
|
|
|
let header = FnHeader { safety: Safety::Unsafe(self.span), ..FnHeader::default() };
|
2020-08-12 21:02:14 +00:00
|
|
|
let sig = FnSig { decl, header, span: self.span };
|
2023-08-06 06:06:23 +00:00
|
|
|
let body = Some(self.cx.block_expr(result));
|
2021-11-07 08:43:49 +00:00
|
|
|
let kind = ItemKind::Fn(Box::new(Fn {
|
|
|
|
defaultness: ast::Defaultness::Final,
|
2021-08-05 01:53:21 +00:00
|
|
|
sig,
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
ident: Ident::from_str_and_span(&global_fn_name(method.name), self.span),
|
2021-11-07 08:43:49 +00:00
|
|
|
generics: Generics::default(),
|
2025-01-09 00:38:25 +00:00
|
|
|
contract: None,
|
2021-11-07 08:43:49 +00:00
|
|
|
body,
|
2024-07-26 10:04:02 +00:00
|
|
|
define_opaque: None,
|
2021-11-07 08:43:49 +00:00
|
|
|
}));
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-20 22:47:43 +00:00
|
|
|
let item = self.cx.item(self.span, self.attrs(), kind);
|
2021-12-15 01:40:08 +00:00
|
|
|
self.cx.stmt_item(self.ty_span, item)
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 00:55:16 +00:00
|
|
|
fn call_allocator(&self, method: Symbol, mut args: ThinVec<P<Expr>>) -> P<Expr> {
|
2020-07-08 01:04:10 +00:00
|
|
|
let method = self.cx.std_path(&[sym::alloc, sym::GlobalAlloc, method]);
|
2021-12-15 01:40:08 +00:00
|
|
|
let method = self.cx.expr_path(self.cx.path(self.ty_span, method));
|
|
|
|
let allocator = self.cx.path_ident(self.ty_span, self.global);
|
2017-06-03 21:54:08 +00:00
|
|
|
let allocator = self.cx.expr_path(allocator);
|
2021-12-15 01:40:08 +00:00
|
|
|
let allocator = self.cx.expr_addr_of(self.ty_span, allocator);
|
2017-06-03 21:54:08 +00:00
|
|
|
args.insert(0, allocator);
|
|
|
|
|
2021-12-15 01:40:08 +00:00
|
|
|
self.cx.expr_call(self.ty_span, method, args)
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 02:34:33 +00:00
|
|
|
fn attrs(&self) -> AttrVec {
|
Avoid more `MetaItem`-to-`Attribute` conversions.
There is code for converting `Attribute` (syntactic) to `MetaItem`
(semantic). There is also code for the reverse direction. The reverse
direction isn't really necessary; it's currently only used when
generating attributes, e.g. in `derive` code.
This commit adds some new functions for creating `Attributes`s directly,
without involving `MetaItem`s: `mk_attr_word`, `mk_attr_name_value_str`,
`mk_attr_nested_word`, and
`ExtCtxt::attr_{word,name_value_str,nested_word}`.
These new methods replace the old functions for creating `Attribute`s:
`mk_attr_inner`, `mk_attr_outer`, and `ExtCtxt::attribute`. Those
functions took `MetaItem`s as input, and relied on many other functions
that created `MetaItems`, which are also removed: `mk_name_value_item`,
`mk_list_item`, `mk_word_item`, `mk_nested_word_item`,
`{MetaItem,MetaItemKind,NestedMetaItem}::token_trees`,
`MetaItemKind::attr_args`, `MetaItemLit::{from_lit_kind,to_token}`,
`ExtCtxt::meta_word`.
Overall this cuts more than 100 lines of code and makes thing simpler.
2022-11-29 07:43:44 +00:00
|
|
|
thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span)]
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-06 14:20:31 +00:00
|
|
|
fn arg_ty(&self, input: &AllocatorMethodInput, args: &mut ThinVec<Param>) -> P<Expr> {
|
|
|
|
match input.ty {
|
2017-06-03 21:54:08 +00:00
|
|
|
AllocatorTy::Layout => {
|
2023-08-06 14:20:31 +00:00
|
|
|
// If an allocator method is ever introduced having multiple
|
|
|
|
// Layout arguments, these argument names need to be
|
|
|
|
// disambiguated somehow. Currently the generated code would
|
|
|
|
// fail to compile with "identifier is bound more than once in
|
|
|
|
// this parameter list".
|
|
|
|
let size = Ident::from_str_and_span("size", self.span);
|
|
|
|
let align = Ident::from_str_and_span("align", self.span);
|
|
|
|
|
2019-09-14 20:16:51 +00:00
|
|
|
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
|
2017-06-03 21:54:08 +00:00
|
|
|
let ty_usize = self.cx.ty_path(usize);
|
2019-08-27 11:24:32 +00:00
|
|
|
args.push(self.cx.param(self.span, size, ty_usize.clone()));
|
|
|
|
args.push(self.cx.param(self.span, align, ty_usize));
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2020-07-08 01:04:10 +00:00
|
|
|
let layout_new =
|
|
|
|
self.cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
|
2019-07-18 23:51:07 +00:00
|
|
|
let layout_new = self.cx.expr_path(self.cx.path(self.span, layout_new));
|
2017-06-03 21:54:08 +00:00
|
|
|
let size = self.cx.expr_ident(self.span, size);
|
|
|
|
let align = self.cx.expr_ident(self.span, align);
|
2022-11-23 00:55:16 +00:00
|
|
|
let layout = self.cx.expr_call(self.span, layout_new, thin_vec![size, align]);
|
2017-06-03 21:54:08 +00:00
|
|
|
layout
|
|
|
|
}
|
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
AllocatorTy::Ptr => {
|
2023-08-06 14:20:31 +00:00
|
|
|
let ident = Ident::from_str_and_span(input.name, self.span);
|
2019-08-27 11:24:32 +00:00
|
|
|
args.push(self.cx.param(self.span, ident, self.ptr_u8()));
|
2023-08-06 06:06:23 +00:00
|
|
|
self.cx.expr_ident(self.span, ident)
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
AllocatorTy::Usize => {
|
2023-08-06 14:20:31 +00:00
|
|
|
let ident = Ident::from_str_and_span(input.name, self.span);
|
2019-08-27 11:24:32 +00:00
|
|
|
args.push(self.cx.param(self.span, ident, self.usize()));
|
2017-06-03 21:54:08 +00:00
|
|
|
self.cx.expr_ident(self.span, ident)
|
|
|
|
}
|
|
|
|
|
2018-04-21 22:16:59 +00:00
|
|
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => {
|
2017-06-03 21:54:08 +00:00
|
|
|
panic!("can't convert AllocatorTy to an argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-06 06:06:23 +00:00
|
|
|
fn ret_ty(&self, ty: &AllocatorTy) -> P<Ty> {
|
2017-06-03 21:54:08 +00:00
|
|
|
match *ty {
|
2023-08-06 06:06:23 +00:00
|
|
|
AllocatorTy::ResultPtr => self.ptr_u8(),
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2023-08-06 06:06:23 +00:00
|
|
|
AllocatorTy::Unit => self.cx.ty(self.span, TyKind::Tup(ThinVec::new())),
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2018-04-13 20:58:16 +00:00
|
|
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
2019-07-22 20:52:13 +00:00
|
|
|
panic!("can't convert `AllocatorTy` to an output")
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
fn usize(&self) -> P<Ty> {
|
2019-09-14 20:16:51 +00:00
|
|
|
let usize = self.cx.path_ident(self.span, Ident::new(sym::usize, self.span));
|
2018-04-03 15:12:57 +00:00
|
|
|
self.cx.ty_path(usize)
|
|
|
|
}
|
|
|
|
|
2017-06-03 21:54:08 +00:00
|
|
|
fn ptr_u8(&self) -> P<Ty> {
|
2019-09-14 20:16:51 +00:00
|
|
|
let u8 = self.cx.path_ident(self.span, Ident::new(sym::u8, self.span));
|
2017-06-03 21:54:08 +00:00
|
|
|
let ty_u8 = self.cx.ty_path(u8);
|
2019-12-16 16:28:40 +00:00
|
|
|
self.cx.ty_ptr(self.span, ty_u8, Mutability::Mut)
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
}
|