2019-02-06 13:55:03 +00:00
|
|
|
use log::debug;
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2018-05-20 22:04:00 +00:00
|
|
|
use syntax::{
|
|
|
|
ast::{
|
|
|
|
self, Arg, Attribute, Crate, Expr, FnHeader, Generics, Ident, Item, ItemKind,
|
2018-08-02 19:30:43 +00:00
|
|
|
Mac, Mod, Mutability, Ty, TyKind, Unsafety, VisibilityKind,
|
2018-05-20 22:04:00 +00:00
|
|
|
},
|
|
|
|
attr,
|
2018-08-18 10:14:03 +00:00
|
|
|
source_map::{
|
2019-06-18 22:08:45 +00:00
|
|
|
respan, ExpnInfo, ExpnKind,
|
2018-05-20 22:04:00 +00:00
|
|
|
},
|
|
|
|
ext::{
|
2019-07-16 18:06:17 +00:00
|
|
|
allocator::{AllocatorKind, AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS},
|
2019-06-30 12:58:56 +00:00
|
|
|
base::{ExtCtxt, MacroKind, Resolver},
|
2018-05-20 22:04:00 +00:00
|
|
|
build::AstBuilder,
|
|
|
|
expand::ExpansionConfig,
|
2019-07-15 22:04:05 +00:00
|
|
|
hygiene::ExpnId,
|
2018-05-20 22:04:00 +00:00
|
|
|
},
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
mut_visit::{self, MutVisitor},
|
2018-05-20 22:04:00 +00:00
|
|
|
parse::ParseSess,
|
|
|
|
ptr::P,
|
2019-06-17 19:18:56 +00:00
|
|
|
symbol::{kw, sym}
|
2018-05-20 22:04:00 +00:00
|
|
|
};
|
|
|
|
use syntax_pos::Span;
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2018-04-13 20:58:16 +00:00
|
|
|
pub fn modify(
|
|
|
|
sess: &ParseSess,
|
2018-07-11 10:08:49 +00:00
|
|
|
resolver: &mut dyn Resolver,
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
krate: &mut Crate,
|
2018-04-13 20:58:16 +00:00
|
|
|
crate_name: String,
|
2019-07-16 18:06:17 +00:00
|
|
|
handler: &errors::Handler,
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
) {
|
2017-06-03 21:54:08 +00:00
|
|
|
ExpandAllocatorDirectives {
|
2017-08-07 05:54:09 +00:00
|
|
|
handler,
|
|
|
|
sess,
|
|
|
|
resolver,
|
2017-06-03 21:54:08 +00:00
|
|
|
found: false,
|
2018-04-13 20:58:16 +00:00
|
|
|
crate_name: Some(crate_name),
|
2018-05-20 22:04:00 +00:00
|
|
|
in_submod: -1, // -1 to account for the "root" module
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
}.visit_crate(krate);
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ExpandAllocatorDirectives<'a> {
|
|
|
|
found: bool,
|
2019-07-16 18:06:17 +00:00
|
|
|
handler: &'a errors::Handler,
|
2017-06-03 21:54:08 +00:00
|
|
|
sess: &'a ParseSess,
|
2018-07-11 10:08:49 +00:00
|
|
|
resolver: &'a mut dyn Resolver,
|
2018-04-13 20:58:16 +00:00
|
|
|
crate_name: Option<String>,
|
2018-05-20 22:04:00 +00:00
|
|
|
|
|
|
|
// For now, we disallow `global_allocator` in submodules because hygiene is hard. Keep track of
|
|
|
|
// whether we are in a submodule or not. If `in_submod > 0` we are in a submodule.
|
|
|
|
in_submod: isize,
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 13:55:03 +00:00
|
|
|
impl MutVisitor for ExpandAllocatorDirectives<'_> {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
fn flat_map_item(&mut self, item: P<Item>) -> SmallVec<[P<Item>; 1]> {
|
2018-06-23 00:59:29 +00:00
|
|
|
debug!("in submodule {}", self.in_submod);
|
2018-05-20 22:04:00 +00:00
|
|
|
|
2019-06-17 19:18:56 +00:00
|
|
|
if !attr::contains_name(&item.attrs, sym::global_allocator) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
return mut_visit::noop_flat_map_item(item, self);
|
2019-06-17 19:18:56 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 21:54:08 +00:00
|
|
|
match item.node {
|
|
|
|
ItemKind::Static(..) => {}
|
|
|
|
_ => {
|
2018-04-13 20:58:16 +00:00
|
|
|
self.handler
|
|
|
|
.span_err(item.span, "allocators must be statics");
|
2018-08-13 19:15:16 +00:00
|
|
|
return smallvec![item];
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:04:00 +00:00
|
|
|
if self.in_submod > 0 {
|
|
|
|
self.handler
|
|
|
|
.span_err(item.span, "`global_allocator` cannot be used in submodules");
|
2018-08-13 19:15:16 +00:00
|
|
|
return smallvec![item];
|
2018-05-20 22:04:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 21:54:08 +00:00
|
|
|
if self.found {
|
2018-05-20 22:04:00 +00:00
|
|
|
self.handler
|
2019-07-22 20:52:13 +00:00
|
|
|
.span_err(item.span, "cannot define more than one `#[global_allocator]`");
|
2018-08-13 19:15:16 +00:00
|
|
|
return smallvec![item];
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
self.found = true;
|
|
|
|
|
2019-07-06 18:02:45 +00:00
|
|
|
// Create a new expansion for the generated allocator code.
|
2019-07-15 22:04:05 +00:00
|
|
|
let span = item.span.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable(
|
2019-06-30 12:58:56 +00:00
|
|
|
ExpnKind::Macro(MacroKind::Attr, sym::global_allocator), item.span, self.sess.edition,
|
2019-07-06 18:02:45 +00:00
|
|
|
[sym::rustc_attrs][..].into(),
|
2019-06-17 19:18:56 +00:00
|
|
|
));
|
2018-04-13 20:58:16 +00:00
|
|
|
|
|
|
|
// Create an expansion config
|
|
|
|
let ecfg = ExpansionConfig::default(self.crate_name.take().unwrap());
|
|
|
|
|
|
|
|
// Generate a bunch of new items using the AllocFnFactory
|
2017-06-03 21:54:08 +00:00
|
|
|
let mut f = AllocFnFactory {
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2017-06-03 21:54:08 +00:00
|
|
|
kind: AllocatorKind::Global,
|
|
|
|
global: item.ident,
|
2019-05-17 08:37:53 +00:00
|
|
|
core: Ident::with_empty_ctxt(sym::core),
|
2017-06-03 21:54:08 +00:00
|
|
|
cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
|
|
|
|
};
|
2018-04-13 20:58:16 +00:00
|
|
|
|
2018-05-20 22:04:00 +00:00
|
|
|
// We will generate a new submodule. To `use` the static from that module, we need to get
|
|
|
|
// the `super::...` path.
|
2019-05-13 19:46:20 +00:00
|
|
|
let super_path = f.cx.path(f.span, vec![Ident::with_empty_ctxt(kw::Super), f.global]);
|
2018-05-20 22:04:00 +00:00
|
|
|
|
|
|
|
// Generate the items in the submodule
|
|
|
|
let mut items = vec![
|
|
|
|
// import `core` to use allocators
|
|
|
|
f.cx.item_extern_crate(f.span, f.core),
|
|
|
|
// `use` the `global_allocator` in `super`
|
|
|
|
f.cx.item_use_simple(
|
|
|
|
f.span,
|
|
|
|
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
|
|
|
|
super_path,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Add the allocator methods to the submodule
|
|
|
|
items.extend(
|
|
|
|
ALLOCATOR_METHODS
|
|
|
|
.iter()
|
|
|
|
.map(|method| f.allocator_fn(method)),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Generate the submodule itself
|
|
|
|
let name = f.kind.fn_name("allocator_abi");
|
2019-05-17 00:44:51 +00:00
|
|
|
let allocator_abi = Ident::from_str(&name).gensym();
|
2018-05-20 22:04:00 +00:00
|
|
|
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
let module = f.cx.monotonic_expander().flat_map_item(module).pop().unwrap();
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2018-05-20 22:04:00 +00:00
|
|
|
// Return the item and new submodule
|
2018-08-30 09:42:16 +00:00
|
|
|
smallvec![item, module]
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:04:00 +00:00
|
|
|
// If we enter a submodule, take note.
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
fn visit_mod(&mut self, m: &mut Mod) {
|
2018-06-23 00:59:29 +00:00
|
|
|
debug!("enter submodule");
|
2018-05-20 22:04:00 +00:00
|
|
|
self.in_submod += 1;
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
mut_visit::noop_visit_mod(m, self);
|
2018-05-20 22:04:00 +00:00
|
|
|
self.in_submod -= 1;
|
2018-06-23 00:59:29 +00:00
|
|
|
debug!("exit submodule");
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
2018-06-06 00:24:10 +00:00
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
// `visit_mac` is disabled by default. Enable it here.
|
|
|
|
fn visit_mac(&mut self, mac: &mut Mac) {
|
|
|
|
mut_visit::noop_visit_mac(mac, self)
|
2018-06-06 00:24:10 +00:00
|
|
|
}
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AllocFnFactory<'a> {
|
|
|
|
span: Span,
|
|
|
|
kind: AllocatorKind,
|
|
|
|
global: Ident,
|
2018-04-03 15:12:57 +00:00
|
|
|
core: Ident,
|
2017-06-03 21:54:08 +00:00
|
|
|
cx: ExtCtxt<'a>,
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:55:03 +00:00
|
|
|
impl AllocFnFactory<'_> {
|
2017-06-03 21:54:08 +00:00
|
|
|
fn allocator_fn(&self, method: &AllocatorMethod) -> P<Item> {
|
|
|
|
let mut abi_args = Vec::new();
|
|
|
|
let mut i = 0;
|
|
|
|
let ref mut mk = || {
|
|
|
|
let name = Ident::from_str(&format!("arg{}", i));
|
|
|
|
i += 1;
|
|
|
|
name
|
|
|
|
};
|
2018-04-13 20:58:16 +00:00
|
|
|
let args = method
|
|
|
|
.inputs
|
|
|
|
.iter()
|
|
|
|
.map(|ty| self.arg_ty(ty, &mut abi_args, mk))
|
|
|
|
.collect();
|
2017-06-03 21:54:08 +00:00
|
|
|
let result = self.call_allocator(method.name, args);
|
2018-04-03 15:12:57 +00:00
|
|
|
let (output_ty, output_expr) = self.ret_ty(&method.output, result);
|
2018-04-13 20:58:16 +00:00
|
|
|
let kind = ItemKind::Fn(
|
|
|
|
self.cx.fn_decl(abi_args, ast::FunctionRetTy::Ty(output_ty)),
|
2018-05-17 05:55:18 +00:00
|
|
|
FnHeader {
|
|
|
|
unsafety: Unsafety::Unsafe,
|
|
|
|
..FnHeader::default()
|
|
|
|
},
|
2018-04-13 20:58:16 +00:00
|
|
|
Generics::default(),
|
|
|
|
self.cx.block_expr(output_expr),
|
|
|
|
);
|
|
|
|
self.cx.item(
|
|
|
|
self.span,
|
|
|
|
Ident::from_str(&self.kind.fn_name(method.name)),
|
|
|
|
self.attrs(),
|
|
|
|
kind,
|
|
|
|
)
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call_allocator(&self, method: &str, mut args: Vec<P<Expr>>) -> P<Expr> {
|
2018-04-13 20:58:16 +00:00
|
|
|
let method = self.cx.path(
|
|
|
|
self.span,
|
|
|
|
vec![
|
|
|
|
self.core,
|
|
|
|
Ident::from_str("alloc"),
|
|
|
|
Ident::from_str("GlobalAlloc"),
|
|
|
|
Ident::from_str(method),
|
|
|
|
],
|
|
|
|
);
|
2017-06-03 21:54:08 +00:00
|
|
|
let method = self.cx.expr_path(method);
|
|
|
|
let allocator = self.cx.path_ident(self.span, self.global);
|
|
|
|
let allocator = self.cx.expr_path(allocator);
|
|
|
|
let allocator = self.cx.expr_addr_of(self.span, allocator);
|
|
|
|
args.insert(0, allocator);
|
|
|
|
|
|
|
|
self.cx.expr_call(self.span, method, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn attrs(&self) -> Vec<Attribute> {
|
2019-05-22 02:42:23 +00:00
|
|
|
let special = sym::rustc_std_internal_symbol;
|
2017-11-01 20:16:36 +00:00
|
|
|
let special = self.cx.meta_word(self.span, special);
|
2018-08-23 07:33:32 +00:00
|
|
|
vec![self.cx.attribute(self.span, special)]
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:58:16 +00:00
|
|
|
fn arg_ty(
|
|
|
|
&self,
|
|
|
|
ty: &AllocatorTy,
|
|
|
|
args: &mut Vec<Arg>,
|
2018-07-11 10:08:49 +00:00
|
|
|
ident: &mut dyn FnMut() -> Ident,
|
2018-04-13 20:58:16 +00:00
|
|
|
) -> P<Expr> {
|
2017-06-03 21:54:08 +00:00
|
|
|
match *ty {
|
|
|
|
AllocatorTy::Layout => {
|
2019-05-17 08:37:53 +00:00
|
|
|
let usize = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::usize));
|
2017-06-03 21:54:08 +00:00
|
|
|
let ty_usize = self.cx.ty_path(usize);
|
|
|
|
let size = ident();
|
|
|
|
let align = ident();
|
|
|
|
args.push(self.cx.arg(self.span, size, ty_usize.clone()));
|
|
|
|
args.push(self.cx.arg(self.span, align, ty_usize));
|
|
|
|
|
2018-04-13 20:58:16 +00:00
|
|
|
let layout_new = self.cx.path(
|
|
|
|
self.span,
|
|
|
|
vec![
|
|
|
|
self.core,
|
|
|
|
Ident::from_str("alloc"),
|
|
|
|
Ident::from_str("Layout"),
|
|
|
|
Ident::from_str("from_size_align_unchecked"),
|
|
|
|
],
|
|
|
|
);
|
2017-06-03 21:54:08 +00:00
|
|
|
let layout_new = self.cx.expr_path(layout_new);
|
|
|
|
let size = self.cx.expr_ident(self.span, size);
|
|
|
|
let align = self.cx.expr_ident(self.span, align);
|
2018-04-13 20:58:16 +00:00
|
|
|
let layout = self.cx.expr_call(self.span, layout_new, vec![size, align]);
|
2017-06-03 21:54:08 +00:00
|
|
|
layout
|
|
|
|
}
|
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
AllocatorTy::Ptr => {
|
2017-06-03 21:54:08 +00:00
|
|
|
let ident = ident();
|
|
|
|
args.push(self.cx.arg(self.span, ident, self.ptr_u8()));
|
2018-04-03 15:12:57 +00:00
|
|
|
let arg = self.cx.expr_ident(self.span, ident);
|
2018-05-31 06:57:43 +00:00
|
|
|
self.cx.expr_cast(self.span, arg, self.ptr_u8())
|
2017-06-03 21:54:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
AllocatorTy::Usize => {
|
2017-06-03 21:54:08 +00:00
|
|
|
let ident = ident();
|
2018-04-03 15:12:57 +00:00
|
|
|
args.push(self.cx.arg(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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
|
2017-06-03 21:54:08 +00:00
|
|
|
match *ty {
|
|
|
|
AllocatorTy::ResultPtr => {
|
|
|
|
// We're creating:
|
|
|
|
//
|
2018-04-03 15:12:57 +00:00
|
|
|
// #expr as *mut u8
|
2017-06-03 21:54:08 +00:00
|
|
|
|
2018-04-03 15:12:57 +00:00
|
|
|
let expr = self.cx.expr_cast(self.span, expr, self.ptr_u8());
|
2017-06-03 21:54:08 +00:00
|
|
|
(self.ptr_u8(), expr)
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:58:16 +00:00
|
|
|
AllocatorTy::Unit => (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr),
|
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-05-17 08:37:53 +00:00
|
|
|
let usize = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::usize));
|
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-05-17 08:37:53 +00:00
|
|
|
let u8 = self.cx.path_ident(self.span, Ident::with_empty_ctxt(sym::u8));
|
2017-06-03 21:54:08 +00:00
|
|
|
let ty_u8 = self.cx.ty_path(u8);
|
|
|
|
self.cx.ty_ptr(self.span, ty_u8, Mutability::Mutable)
|
|
|
|
}
|
|
|
|
}
|