1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use llvm::{AvailableExternallyLinkage, InternalLinkage, SetLinkage};
use metadata::csearch;
use middle::astencode;
use middle::subst::Substs;
use trans::base::{push_ctxt, trans_item, get_item_val, trans_fn};
use trans::common::*;
use middle::ty;
use syntax::ast;
use syntax::ast_util::{local_def, PostExpansionMethod};
fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
-> Option<ast::DefId> {
let _icx = push_ctxt("maybe_instantiate_inline");
match ccx.external().borrow().get(&fn_id) {
Some(&Some(node_id)) => {
debug!("maybe_instantiate_inline({}): already inline as node id {}",
ty::item_path_str(ccx.tcx(), fn_id), node_id);
return Some(local_def(node_id));
}
Some(&None) => {
return None;
}
None => {
}
}
let csearch_result =
csearch::maybe_get_item_ast(
ccx.tcx(), fn_id,
|a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
let inline_def = match csearch_result {
csearch::not_found => {
ccx.external().borrow_mut().insert(fn_id, None);
return None;
}
csearch::found(&ast::IIItem(ref item)) => {
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
trans_item(ccx, &**item);
let linkage = match item.node {
ast::ItemFn(_, _, _, ref generics, _) => {
if generics.is_type_parameterized() {
None
} else {
if ccx.sess().opts.cg.codegen_units == 1 {
Some(InternalLinkage)
} else {
Some(AvailableExternallyLinkage)
}
}
}
ast::ItemConst(..) => None,
_ => unreachable!(),
};
match linkage {
Some(linkage) => {
let g = get_item_val(ccx, item.id);
SetLinkage(g, linkage);
}
None => {}
}
local_def(item.id)
}
csearch::found(&ast::IIForeign(ref item)) => {
ccx.external().borrow_mut().insert(fn_id, Some(item.id));
ccx.external_srcs().borrow_mut().insert(item.id, fn_id);
local_def(item.id)
}
csearch::found_parent(parent_id, &ast::IIItem(ref item)) => {
ccx.external().borrow_mut().insert(parent_id, Some(item.id));
ccx.external_srcs().borrow_mut().insert(item.id, parent_id);
let mut my_id = 0;
match item.node {
ast::ItemEnum(_, _) => {
let vs_here = ty::enum_variants(ccx.tcx(), local_def(item.id));
let vs_there = ty::enum_variants(ccx.tcx(), parent_id);
for (here, there) in vs_here.iter().zip(vs_there.iter()) {
if there.id == fn_id { my_id = here.id.node; }
ccx.external().borrow_mut().insert(there.id, Some(here.id.node));
}
}
ast::ItemStruct(ref struct_def, _) => {
match struct_def.ctor_id {
None => {}
Some(ctor_id) => {
ccx.external().borrow_mut().insert(fn_id, Some(ctor_id));
my_id = ctor_id;
}
}
}
_ => ccx.sess().bug("maybe_instantiate_inline: item has a \
non-enum, non-struct parent")
}
trans_item(ccx, &**item);
local_def(my_id)
}
csearch::found_parent(_, _) => {
ccx.sess().bug("maybe_get_item_ast returned a found_parent \
with a non-item parent");
}
csearch::found(&ast::IITraitItem(_, ref trait_item)) => {
match *trait_item {
ast::RequiredMethod(_) => ccx.sess().bug("found RequiredMethod IITraitItem"),
ast::ProvidedMethod(ref mth) => {
ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
local_def(mth.id)
}
ast::TypeTraitItem(_) => {
ccx.sess().bug("found TypeTraitItem IITraitItem")
}
}
}
csearch::found(&ast::IIImplItem(impl_did, ref impl_item)) => {
match *impl_item {
ast::MethodImplItem(ref mth) => {
ccx.external().borrow_mut().insert(fn_id, Some(mth.id));
ccx.external_srcs().borrow_mut().insert(mth.id, fn_id);
ccx.stats().n_inlines.set(ccx.stats().n_inlines.get() + 1);
let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
let unparameterized = impl_tpt.generics.types.is_empty() &&
mth.pe_generics().ty_params.is_empty();
if unparameterized {
let llfn = get_item_val(ccx, mth.id);
trans_fn(ccx,
&*mth.pe_fn_decl(),
&*mth.pe_body(),
llfn,
&Substs::trans_empty(),
mth.id,
&[]);
SetLinkage(llfn, InternalLinkage);
}
local_def(mth.id)
}
ast::TypeImplItem(_) => {
ccx.sess().bug("found TypeImplItem IIImplItem")
}
}
}
};
return Some(inline_def);
}
pub fn get_local_instance(ccx: &CrateContext, fn_id: ast::DefId)
-> Option<ast::DefId> {
if fn_id.krate == ast::LOCAL_CRATE {
Some(fn_id)
} else {
instantiate_inline(ccx, fn_id)
}
}
pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId) -> ast::DefId {
get_local_instance(ccx, fn_id).unwrap_or(fn_id)
}