Skip to content

Commit f89856b

Browse files
committed
rustc: move function arguments into hir::Body.
1 parent e64f64a commit f89856b

File tree

50 files changed

+545
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+545
-568
lines changed

src/librustc/dep_graph/graph.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ impl DepGraph {
5151
}
5252
}
5353

54+
/// True if we are actually building the full dep-graph.
55+
#[inline]
56+
pub fn is_fully_enabled(&self) -> bool {
57+
self.data.thread.is_fully_enabled()
58+
}
59+
5460
pub fn query(&self) -> DepGraphQuery<DefId> {
5561
self.data.thread.query()
5662
}

src/librustc/hir/intravisit.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_node_i
395395
}
396396

397397
pub fn walk_body<'v, V: Visitor<'v>>(visitor: &mut V, body: &'v Body) {
398+
for argument in &body.arguments {
399+
visitor.visit_id(argument.id);
400+
visitor.visit_pat(&argument.pat);
401+
}
398402
visitor.visit_expr(&body.value);
399403
}
400404

@@ -680,9 +684,12 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
680684
visitor.visit_name(foreign_item.span, foreign_item.name);
681685

682686
match foreign_item.node {
683-
ForeignItemFn(ref function_declaration, ref generics) => {
687+
ForeignItemFn(ref function_declaration, ref names, ref generics) => {
688+
visitor.visit_generics(generics);
684689
visitor.visit_fn_decl(function_declaration);
685-
visitor.visit_generics(generics)
690+
for name in names {
691+
visitor.visit_name(name.span, name.node);
692+
}
686693
}
687694
ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
688695
}
@@ -750,18 +757,8 @@ pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionR
750757
}
751758

752759
pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
753-
for argument in &function_declaration.inputs {
754-
visitor.visit_id(argument.id);
755-
visitor.visit_pat(&argument.pat);
756-
visitor.visit_ty(&argument.ty)
757-
}
758-
walk_fn_ret_ty(visitor, &function_declaration.output)
759-
}
760-
761-
pub fn walk_fn_decl_nopat<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
762-
for argument in &function_declaration.inputs {
763-
visitor.visit_id(argument.id);
764-
visitor.visit_ty(&argument.ty)
760+
for ty in &function_declaration.inputs {
761+
visitor.visit_ty(ty)
765762
}
766763
walk_fn_ret_ty(visitor, &function_declaration.output)
767764
}
@@ -799,12 +796,15 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
799796
visitor.visit_ty(ty);
800797
walk_list!(visitor, visit_nested_body, default);
801798
}
802-
TraitItemKind::Method(ref sig, None) => {
799+
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
803800
visitor.visit_id(trait_item.id);
804801
visitor.visit_generics(&sig.generics);
805802
visitor.visit_fn_decl(&sig.decl);
803+
for name in names {
804+
visitor.visit_name(name.span, name.node);
805+
}
806806
}
807-
TraitItemKind::Method(ref sig, Some(body_id)) => {
807+
TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {
808808
visitor.visit_fn(FnKind::Method(trait_item.name,
809809
sig,
810810
None,
@@ -1113,16 +1113,3 @@ impl<'a, 'ast> Visitor<'ast> for IdRangeComputingVisitor<'a, 'ast> {
11131113
self.result.add(id);
11141114
}
11151115
}
1116-
1117-
/// Computes the id range for a single fn body, ignoring nested items.
1118-
pub fn compute_id_range_for_fn_body<'v>(fk: FnKind<'v>,
1119-
decl: &'v FnDecl,
1120-
body: BodyId,
1121-
sp: Span,
1122-
id: NodeId,
1123-
map: &map::Map<'v>)
1124-
-> IdRange {
1125-
let mut visitor = IdRangeComputingVisitor::new(map);
1126-
visitor.visit_fn(fk, decl, body, sp, id);
1127-
visitor.result()
1128-
}

src/librustc/hir/lowering.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ impl<'a> LoweringContext<'a> {
170170
visit::walk_crate(&mut item_lowerer, c);
171171
}
172172

173-
fn record_body(&mut self, value: hir::Expr) -> hir::BodyId {
173+
fn record_body(&mut self, value: hir::Expr, decl: Option<&FnDecl>)
174+
-> hir::BodyId {
174175
let body = hir::Body {
176+
arguments: decl.map_or(hir_vec![], |decl| {
177+
decl.inputs.iter().map(|x| self.lower_arg(x)).collect()
178+
}),
175179
value: value
176180
};
177181
let id = body.id();
@@ -310,11 +314,11 @@ impl<'a> LoweringContext<'a> {
310314
TyKind::Array(ref ty, ref length) => {
311315
let length = self.lower_expr(length);
312316
hir::TyArray(self.lower_ty(ty),
313-
self.record_body(length))
317+
self.record_body(length, None))
314318
}
315319
TyKind::Typeof(ref expr) => {
316320
let expr = self.lower_expr(expr);
317-
hir::TyTypeof(self.record_body(expr))
321+
hir::TyTypeof(self.record_body(expr, None))
318322
}
319323
TyKind::PolyTraitRef(ref bounds) => {
320324
hir::TyPolyTraitRef(self.lower_bounds(bounds))
@@ -343,7 +347,7 @@ impl<'a> LoweringContext<'a> {
343347
data: self.lower_variant_data(&v.node.data),
344348
disr_expr: v.node.disr_expr.as_ref().map(|e| {
345349
let e = self.lower_expr(e);
346-
self.record_body(e)
350+
self.record_body(e, None)
347351
}),
348352
},
349353
span: v.span,
@@ -532,13 +536,24 @@ impl<'a> LoweringContext<'a> {
532536
hir::Arg {
533537
id: arg.id,
534538
pat: self.lower_pat(&arg.pat),
535-
ty: self.lower_ty(&arg.ty),
536539
}
537540
}
538541

542+
fn lower_fn_args_to_names(&mut self, decl: &FnDecl)
543+
-> hir::HirVec<Spanned<Name>> {
544+
decl.inputs.iter().map(|arg| {
545+
match arg.pat.node {
546+
PatKind::Ident(_, ident, None) => {
547+
respan(ident.span, ident.node.name)
548+
}
549+
_ => respan(arg.pat.span, keywords::Invalid.name()),
550+
}
551+
}).collect()
552+
}
553+
539554
fn lower_fn_decl(&mut self, decl: &FnDecl) -> P<hir::FnDecl> {
540555
P(hir::FnDecl {
541-
inputs: decl.inputs.iter().map(|x| self.lower_arg(x)).collect(),
556+
inputs: decl.inputs.iter().map(|arg| self.lower_ty(&arg.ty)).collect(),
542557
output: match decl.output {
543558
FunctionRetTy::Ty(ref ty) => hir::Return(self.lower_ty(ty)),
544559
FunctionRetTy::Default(span) => hir::DefaultReturn(span),
@@ -869,17 +884,17 @@ impl<'a> LoweringContext<'a> {
869884
let value = self.lower_expr(e);
870885
hir::ItemStatic(self.lower_ty(t),
871886
self.lower_mutability(m),
872-
self.record_body(value))
887+
self.record_body(value, None))
873888
}
874889
ItemKind::Const(ref t, ref e) => {
875890
let value = self.lower_expr(e);
876891
hir::ItemConst(self.lower_ty(t),
877-
self.record_body(value))
892+
self.record_body(value, None))
878893
}
879894
ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
880895
let body = self.lower_block(body);
881896
let body = self.expr_block(body, ThinVec::new());
882-
let body_id = self.record_body(body);
897+
let body_id = self.record_body(body, Some(decl));
883898
hir::ItemFn(self.lower_fn_decl(decl),
884899
self.lower_unsafety(unsafety),
885900
self.lower_constness(constness),
@@ -948,16 +963,20 @@ impl<'a> LoweringContext<'a> {
948963
hir::TraitItemKind::Const(this.lower_ty(ty),
949964
default.as_ref().map(|x| {
950965
let value = this.lower_expr(x);
951-
this.record_body(value)
966+
this.record_body(value, None)
952967
}))
953968
}
954-
TraitItemKind::Method(ref sig, ref body) => {
969+
TraitItemKind::Method(ref sig, None) => {
970+
let names = this.lower_fn_args_to_names(&sig.decl);
955971
hir::TraitItemKind::Method(this.lower_method_sig(sig),
956-
body.as_ref().map(|x| {
957-
let body = this.lower_block(x);
958-
let expr = this.expr_block(body, ThinVec::new());
959-
this.record_body(expr)
960-
}))
972+
hir::TraitMethod::Required(names))
973+
}
974+
TraitItemKind::Method(ref sig, Some(ref body)) => {
975+
let body = this.lower_block(body);
976+
let expr = this.expr_block(body, ThinVec::new());
977+
let body_id = this.record_body(expr, Some(&sig.decl));
978+
hir::TraitItemKind::Method(this.lower_method_sig(sig),
979+
hir::TraitMethod::Provided(body_id))
961980
}
962981
TraitItemKind::Type(ref bounds, ref default) => {
963982
hir::TraitItemKind::Type(this.lower_bounds(bounds),
@@ -1005,13 +1024,13 @@ impl<'a> LoweringContext<'a> {
10051024
node: match i.node {
10061025
ImplItemKind::Const(ref ty, ref expr) => {
10071026
let value = this.lower_expr(expr);
1008-
let body_id = this.record_body(value);
1027+
let body_id = this.record_body(value, None);
10091028
hir::ImplItemKind::Const(this.lower_ty(ty), body_id)
10101029
}
10111030
ImplItemKind::Method(ref sig, ref body) => {
10121031
let body = this.lower_block(body);
10131032
let expr = this.expr_block(body, ThinVec::new());
1014-
let body_id = this.record_body(expr);
1033+
let body_id = this.record_body(expr, Some(&sig.decl));
10151034
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
10161035
}
10171036
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
@@ -1097,7 +1116,9 @@ impl<'a> LoweringContext<'a> {
10971116
attrs: this.lower_attrs(&i.attrs),
10981117
node: match i.node {
10991118
ForeignItemKind::Fn(ref fdec, ref generics) => {
1100-
hir::ForeignItemFn(this.lower_fn_decl(fdec), this.lower_generics(generics))
1119+
hir::ForeignItemFn(this.lower_fn_decl(fdec),
1120+
this.lower_fn_args_to_names(fdec),
1121+
this.lower_generics(generics))
11011122
}
11021123
ForeignItemKind::Static(ref t, m) => {
11031124
hir::ForeignItemStatic(this.lower_ty(t), m)
@@ -1367,7 +1388,7 @@ impl<'a> LoweringContext<'a> {
13671388
ExprKind::Repeat(ref expr, ref count) => {
13681389
let expr = P(self.lower_expr(expr));
13691390
let count = self.lower_expr(count);
1370-
hir::ExprRepeat(expr, self.record_body(count))
1391+
hir::ExprRepeat(expr, self.record_body(count, None))
13711392
}
13721393
ExprKind::Tup(ref elts) => {
13731394
hir::ExprTup(elts.iter().map(|x| self.lower_expr(x)).collect())
@@ -1450,7 +1471,7 @@ impl<'a> LoweringContext<'a> {
14501471
let expr = this.lower_expr(body);
14511472
hir::ExprClosure(this.lower_capture_clause(capture_clause),
14521473
this.lower_fn_decl(decl),
1453-
this.record_body(expr),
1474+
this.record_body(expr, Some(decl)),
14541475
fn_decl_span)
14551476
})
14561477
}
@@ -1734,13 +1755,7 @@ impl<'a> LoweringContext<'a> {
17341755
// `::std::option::Option::Some(<pat>) => <body>`
17351756
let pat_arm = {
17361757
let body_block = self.lower_block(body);
1737-
let body_span = body_block.span;
1738-
let body_expr = P(hir::Expr {
1739-
id: self.next_id(),
1740-
node: hir::ExprBlock(body_block),
1741-
span: body_span,
1742-
attrs: ThinVec::new(),
1743-
});
1758+
let body_expr = P(self.expr_block(body_block, ThinVec::new()));
17441759
let pat = self.lower_pat(pat);
17451760
let some_pat = self.pat_some(e.span, pat);
17461761

src/librustc/hir/map/blocks.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ impl MaybeFnLike for ast::Item {
6262

6363
impl MaybeFnLike for ast::TraitItem {
6464
fn is_fn_like(&self) -> bool {
65-
match self.node { ast::TraitItemKind::Method(_, Some(_)) => true, _ => false, }
65+
match self.node {
66+
ast::TraitItemKind::Method(_, ast::TraitMethod::Provided(_)) => true,
67+
_ => false,
68+
}
6669
}
6770
}
6871

@@ -252,7 +255,7 @@ impl<'a> FnLikeNode<'a> {
252255
_ => bug!("item FnLikeNode that is not fn-like"),
253256
},
254257
map::NodeTraitItem(ti) => match ti.node {
255-
ast::TraitItemKind::Method(ref sig, Some(body)) => {
258+
ast::TraitItemKind::Method(ref sig, ast::TraitMethod::Provided(body)) => {
256259
method(ti.id, ti.name, sig, None, body, ti.span, &ti.attrs)
257260
}
258261
_ => bug!("trait method FnLikeNode that is not fn-like"),

src/librustc/hir/map/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ impl<'ast> Map<'ast> {
389389
fn is_trait_item_body(&self, node_id: NodeId, item: &TraitItem) -> bool {
390390
match item.node {
391391
TraitItemKind::Const(_, Some(body)) |
392-
TraitItemKind::Method(_, Some(body)) => body.node_id == node_id,
392+
TraitItemKind::Method(_, TraitMethod::Provided(body)) => {
393+
body.node_id == node_id
394+
}
393395
_ => false
394396
}
395397
}

src/librustc/hir/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ pub struct BodyId {
868868
/// The body of a function or constant value.
869869
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
870870
pub struct Body {
871+
pub arguments: HirVec<Arg>,
871872
pub value: Expr
872873
}
873874

@@ -1102,14 +1103,24 @@ pub struct TraitItem {
11021103
pub span: Span,
11031104
}
11041105

1106+
/// A trait method's body (or just argument names).
1107+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1108+
pub enum TraitMethod {
1109+
/// No default body in the trait, just a signature.
1110+
Required(HirVec<Spanned<Name>>),
1111+
1112+
/// Both signature and body are provided in the trait.
1113+
Provided(BodyId),
1114+
}
1115+
11051116
/// Represents a trait method or associated constant or type
11061117
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
11071118
pub enum TraitItemKind {
11081119
/// An associated constant with an optional value (otherwise `impl`s
11091120
/// must contain a value)
11101121
Const(P<Ty>, Option<BodyId>),
11111122
/// A method with an optional body
1112-
Method(MethodSig, Option<BodyId>),
1123+
Method(MethodSig, TraitMethod),
11131124
/// An associated type with (possibly empty) bounds and optional concrete
11141125
/// type
11151126
Type(TyParamBounds, Option<P<Ty>>),
@@ -1248,15 +1259,14 @@ pub struct InlineAsm {
12481259
/// represents an argument in a function header
12491260
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12501261
pub struct Arg {
1251-
pub ty: P<Ty>,
12521262
pub pat: P<Pat>,
12531263
pub id: NodeId,
12541264
}
12551265

12561266
/// Represents the header (not the body) of a function declaration
12571267
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
12581268
pub struct FnDecl {
1259-
pub inputs: HirVec<Arg>,
1269+
pub inputs: HirVec<P<Ty>>,
12601270
pub output: FunctionRetTy,
12611271
pub variadic: bool,
12621272
}
@@ -1639,7 +1649,7 @@ pub struct ForeignItem {
16391649
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
16401650
pub enum ForeignItem_ {
16411651
/// A foreign function
1642-
ForeignItemFn(P<FnDecl>, Generics),
1652+
ForeignItemFn(P<FnDecl>, HirVec<Spanned<Name>>, Generics),
16431653
/// A foreign static item (`static ext: u8`), with optional mutability
16441654
/// (the boolean is true when mutable)
16451655
ForeignItemStatic(P<Ty>, bool),

0 commit comments

Comments
 (0)