@@ -4,58 +4,73 @@ import attr;
4
4
5
5
export strip_unconfigured_items;
6
6
export metas_in_cfg;
7
+ export strip_items;
8
+
9
+ type in_cfg_pred = fn @( [ ast:: attribute ] ) -> bool ;
10
+
11
+ type ctxt = @{
12
+ in_cfg : in_cfg_pred
13
+ } ;
7
14
8
15
// Support conditional compilation by transforming the AST, stripping out
9
16
// any items that do not belong in the current configuration
10
17
fn strip_unconfigured_items ( crate : @ast:: crate ) -> @ast:: crate {
11
- let cfg = crate . node. config ;
18
+ strip_items ( crate ) { |attrs|
19
+ in_cfg ( crate . node. config , attrs)
20
+ }
21
+ }
22
+
23
+ fn strip_items ( crate : @ast:: crate , in_cfg : in_cfg_pred )
24
+ -> @ast:: crate {
25
+
26
+ let ctxt = @{ in_cfg: in_cfg} ;
12
27
13
28
let precursor =
14
- { fold_mod: bind fold_mod ( cfg , _, _) ,
15
- fold_block: bind fold_block ( cfg , _, _) ,
16
- fold_native_mod: bind fold_native_mod ( cfg , _, _)
29
+ { fold_mod: bind fold_mod ( ctxt , _, _) ,
30
+ fold_block: bind fold_block ( ctxt , _, _) ,
31
+ fold_native_mod: bind fold_native_mod ( ctxt , _, _)
17
32
with * fold:: default_ast_fold ( ) } ;
18
33
19
34
let fold = fold:: make_fold ( precursor) ;
20
35
let res = @fold. fold_crate ( * crate ) ;
21
36
ret res;
22
37
}
23
38
24
- fn filter_item ( cfg : ast :: crate_cfg , & & item: @ast:: item ) ->
39
+ fn filter_item ( cx : ctxt , & & item: @ast:: item ) ->
25
40
option:: t < @ast:: item > {
26
- if item_in_cfg ( cfg , item) { option:: some ( item) } else { option:: none }
41
+ if item_in_cfg ( cx , item) { option:: some ( item) } else { option:: none }
27
42
}
28
43
29
- fn fold_mod ( cfg : ast :: crate_cfg , m : ast:: _mod , fld : fold:: ast_fold ) ->
44
+ fn fold_mod ( cx : ctxt , m : ast:: _mod , fld : fold:: ast_fold ) ->
30
45
ast:: _mod {
31
- let filter = bind filter_item ( cfg , _) ;
46
+ let filter = bind filter_item ( cx , _) ;
32
47
let filtered_items = vec:: filter_map ( m. items , filter) ;
33
48
ret { view_items : vec:: map ( m. view_items , fld. fold_view_item ) ,
34
49
items : vec:: map ( filtered_items, fld. fold_item ) } ;
35
50
}
36
51
37
- fn filter_native_item ( cfg : ast :: crate_cfg , & & item: @ast:: native_item ) ->
52
+ fn filter_native_item ( cx : ctxt , & & item: @ast:: native_item ) ->
38
53
option:: t < @ast:: native_item > {
39
- if native_item_in_cfg ( cfg , item) {
54
+ if native_item_in_cfg ( cx , item) {
40
55
option:: some ( item)
41
56
} else { option:: none }
42
57
}
43
58
44
- fn fold_native_mod ( cfg : ast :: crate_cfg , nm : ast:: native_mod ,
59
+ fn fold_native_mod ( cx : ctxt , nm : ast:: native_mod ,
45
60
fld : fold:: ast_fold ) -> ast:: native_mod {
46
- let filter = bind filter_native_item ( cfg , _) ;
61
+ let filter = bind filter_native_item ( cx , _) ;
47
62
let filtered_items = vec:: filter_map ( nm. items , filter) ;
48
63
ret { view_items : vec:: map ( nm. view_items , fld. fold_view_item ) ,
49
64
items : filtered_items} ;
50
65
}
51
66
52
- fn filter_stmt ( cfg : ast :: crate_cfg , & & stmt: @ast:: stmt ) ->
67
+ fn filter_stmt ( cx : ctxt , & & stmt: @ast:: stmt ) ->
53
68
option:: t < @ast:: stmt > {
54
69
alt stmt. node {
55
70
ast:: stmt_decl ( decl, _) {
56
71
alt decl. node {
57
72
ast:: decl_item ( item) {
58
- if item_in_cfg ( cfg , item) {
73
+ if item_in_cfg ( cx , item) {
59
74
option:: some ( stmt)
60
75
} else { option:: none }
61
76
}
@@ -66,9 +81,9 @@ fn filter_stmt(cfg: ast::crate_cfg, &&stmt: @ast::stmt) ->
66
81
}
67
82
}
68
83
69
- fn fold_block ( cfg : ast :: crate_cfg , b : ast:: blk_ , fld : fold:: ast_fold ) ->
84
+ fn fold_block ( cx : ctxt , b : ast:: blk_ , fld : fold:: ast_fold ) ->
70
85
ast:: blk_ {
71
- let filter = bind filter_stmt ( cfg , _) ;
86
+ let filter = bind filter_stmt ( cx , _) ;
72
87
let filtered_stmts = vec:: filter_map ( b. stmts , filter) ;
73
88
ret { view_items : b. view_items ,
74
89
stmts : vec:: map ( filtered_stmts, fld. fold_stmt ) ,
@@ -77,12 +92,12 @@ fn fold_block(cfg: ast::crate_cfg, b: ast::blk_, fld: fold::ast_fold) ->
77
92
rules : b. rules } ;
78
93
}
79
94
80
- fn item_in_cfg ( cfg : ast :: crate_cfg , item : @ast:: item ) -> bool {
81
- ret in_cfg ( cfg , item. attrs ) ;
95
+ fn item_in_cfg ( cx : ctxt , item : @ast:: item ) -> bool {
96
+ ret cx . in_cfg ( item. attrs ) ;
82
97
}
83
98
84
- fn native_item_in_cfg ( cfg : ast :: crate_cfg , item : @ast:: native_item ) -> bool {
85
- ret in_cfg ( cfg , item. attrs ) ;
99
+ fn native_item_in_cfg ( cx : ctxt , item : @ast:: native_item ) -> bool {
100
+ ret cx . in_cfg ( item. attrs ) ;
86
101
}
87
102
88
103
// Determine if an item should be translated in the current crate
0 commit comments