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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
use rustc_index::vec::{Idx, IndexVec};
use rustc::hir::def_id::CrateNum;
use rustc::mir;
use rustc::session::config::DebugInfo;
use rustc::ty::{self, TyCtxt};
use rustc::ty::layout::{LayoutOf, Size, VariantIdx};
use crate::traits::*;
use syntax_pos::{BytePos, Span, Symbol};
use syntax::symbol::kw;
use super::{FunctionCx, LocalRef};
use super::OperandValue;
pub struct FunctionDebugContext<D> {
pub scopes: IndexVec<mir::SourceScope, DebugScope<D>>,
pub source_locations_enabled: bool,
pub defining_crate: CrateNum,
}
#[derive(Copy, Clone)]
pub enum VariableKind {
ArgumentVariable(usize /*index*/),
LocalVariable,
}
#[derive(Clone, Copy, Debug)]
pub struct DebugScope<D> {
pub scope_metadata: Option<D>,
// Start and end offsets of the file to which this DIScope belongs.
// These are used to quickly determine whether some span refers to the same file.
pub file_start_pos: BytePos,
pub file_end_pos: BytePos,
}
impl<D> DebugScope<D> {
pub fn is_valid(&self) -> bool {
!self.scope_metadata.is_none()
}
}
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
pub fn set_debug_loc(
&mut self,
bx: &mut Bx,
source_info: mir::SourceInfo
) {
let (scope, span) = self.debug_loc(source_info);
if let Some(debug_context) = &mut self.debug_context {
// FIXME(eddyb) get rid of this unwrap somehow.
bx.set_source_location(debug_context, scope.unwrap(), span);
}
}
pub fn debug_loc(&self, source_info: mir::SourceInfo) -> (Option<Bx::DIScope>, Span) {
// Bail out if debug info emission is not enabled.
match self.debug_context {
None => return (None, source_info.span),
Some(_) => {}
}
// In order to have a good line stepping behavior in debugger, we overwrite debug
// locations of macro expansions with that of the outermost expansion site
// (unless the crate is being compiled with `-Z debug-macros`).
if !source_info.span.from_expansion() ||
self.cx.sess().opts.debugging_opts.debug_macros {
let scope = self.scope_metadata_for_loc(source_info.scope, source_info.span.lo());
(scope, source_info.span)
} else {
// Walk up the macro expansion chain until we reach a non-expanded span.
// We also stop at the function body level because no line stepping can occur
// at the level above that.
let span = syntax_pos::hygiene::walk_chain(source_info.span, self.mir.span.ctxt());
let scope = self.scope_metadata_for_loc(source_info.scope, span.lo());
// Use span of the outermost expansion site, while keeping the original lexical scope.
(scope, span)
}
}
// DILocations inherit source file name from the parent DIScope. Due to macro expansions
// it may so happen that the current span belongs to a different file than the DIScope
// corresponding to span's containing source scope. If so, we need to create a DIScope
// "extension" into that file.
fn scope_metadata_for_loc(&self, scope_id: mir::SourceScope, pos: BytePos)
-> Option<Bx::DIScope> {
let debug_context = self.debug_context.as_ref()?;
let scope_metadata = debug_context.scopes[scope_id].scope_metadata;
if pos < debug_context.scopes[scope_id].file_start_pos ||
pos >= debug_context.scopes[scope_id].file_end_pos {
let sm = self.cx.sess().source_map();
let defining_crate = debug_context.defining_crate;
Some(self.cx.extend_scope_to_file(
scope_metadata.unwrap(),
&sm.lookup_char_pos(pos).file,
defining_crate
))
} else {
scope_metadata
}
}
/// Apply debuginfo and/or name, after creating the `alloca` for a local,
/// or initializing the local with an operand (whichever applies).
// FIXME(eddyb) use `llvm.dbg.value` (which would work for operands),
// not just `llvm.dbg.declare` (which requires `alloca`).
pub fn debug_introduce_local(&self, bx: &mut Bx, local: mir::Local) {
// FIXME(eddyb) maybe name the return place as `_0` or `return`?
if local == mir::RETURN_PLACE {
return;
}
let vars = match &self.per_local_var_debug_info {
Some(per_local) => &per_local[local],
None => return,
};
let whole_local_var = vars.iter().find(|var| {
var.place.projection.is_empty()
});
let has_proj = || vars.iter().any(|var| {
!var.place.projection.is_empty()
});
let (fallback_var, kind) = if self.mir.local_kind(local) == mir::LocalKind::Arg {
let arg_index = local.index() - 1;
// Add debuginfo even to unnamed arguments.
// FIXME(eddyb) is this really needed?
let var = if arg_index == 0 && has_proj() {
// Hide closure environments from debuginfo.
// FIXME(eddyb) shouldn't `ArgumentVariable` indices
// be offset to account for the hidden environment?
None
} else {
Some(VarDebugInfo {
name: kw::Invalid,
source_info: self.mir.local_decls[local].source_info,
place: local.into(),
})
};
(var, VariableKind::ArgumentVariable(arg_index + 1))
} else {
(None, VariableKind::LocalVariable)
};
let local_ref = &self.locals[local];
if !bx.sess().fewer_names() {
let name = match whole_local_var.or(fallback_var.as_ref()) {
Some(var) if var.name != kw::Invalid => var.name.to_string(),
_ => format!("{:?}", local),
};
match local_ref {
LocalRef::Place(place) |
LocalRef::UnsizedPlace(place) => {
bx.set_var_name(place.llval, &name);
}
LocalRef::Operand(Some(operand)) => match operand.val {
OperandValue::Ref(x, ..) |
OperandValue::Immediate(x) => {
bx.set_var_name(x, &name);
}
OperandValue::Pair(a, b) => {
// FIXME(eddyb) these are scalar components,
// maybe extract the high-level fields?
bx.set_var_name(a, &(name.clone() + ".0"));
bx.set_var_name(b, &(name + ".1"));
}
}
LocalRef::Operand(None) => {}
}
}
if bx.sess().opts.debuginfo != DebugInfo::Full {
return;
}
let debug_context = match &self.debug_context {
Some(debug_context) => debug_context,
None => return,
};
// FIXME(eddyb) add debuginfo for unsized places too.
let base = match local_ref {
LocalRef::Place(place) => place,
_ => return,
};
let vars = vars.iter().chain(if whole_local_var.is_none() {
fallback_var.as_ref()
} else {
None
});
for var in vars {
let mut layout = base.layout;
let mut direct_offset = Size::ZERO;
// FIXME(eddyb) use smallvec here.
let mut indirect_offsets = vec![];
let kind = if var.place.projection.is_empty() {
kind
} else {
VariableKind::LocalVariable
};
for elem in &var.place.projection[..] {
match *elem {
mir::ProjectionElem::Deref => {
indirect_offsets.push(Size::ZERO);
layout = bx.cx().layout_of(
layout.ty.builtin_deref(true)
.unwrap_or_else(|| {
span_bug!(
var.source_info.span,
"cannot deref `{}`",
layout.ty,
)
}).ty,
);
}
mir::ProjectionElem::Field(field, _) => {
let i = field.index();
let offset = indirect_offsets.last_mut()
.unwrap_or(&mut direct_offset);
*offset += layout.fields.offset(i);
layout = layout.field(bx.cx(), i);
}
mir::ProjectionElem::Downcast(_, variant) => {
layout = layout.for_variant(bx.cx(), variant);
}
_ => span_bug!(
var.source_info.span,
"unsupported var debuginfo place `{:?}`",
var.place,
),
}
}
let (scope, span) = self.debug_loc(var.source_info);
if let Some(scope) = scope {
bx.declare_local(debug_context, var.name, layout.ty, scope,
base.llval, direct_offset, &indirect_offsets, kind, span);
}
}
}
pub fn debug_introduce_locals(&self, bx: &mut Bx) {
if bx.sess().opts.debuginfo == DebugInfo::Full || !bx.sess().fewer_names() {
for local in self.locals.indices() {
self.debug_introduce_local(bx, local);
}
}
}
}
pub fn per_local_var_debug_info(
tcx: TyCtxt<'tcx>,
body: &mir::Body<'tcx>,
) -> Option<IndexVec<mir::Local, Vec<VarDebugInfo<'tcx>>>> {
if tcx.sess.opts.debuginfo == DebugInfo::Full || !tcx.sess.fewer_names() {
let mut per_local = IndexVec::from_elem(vec![], &body.local_decls);
for (local, decl) in body.local_decls.iter_enumerated() {
if let Some(name) = decl.name {
per_local[local].push(VarDebugInfo {
name,
source_info: mir::SourceInfo {
span: decl.source_info.span,
scope: decl.visibility_scope,
},
place: local.into(),
});
}
}
let upvar_debuginfo = &body.__upvar_debuginfo_codegen_only_do_not_use;
if !upvar_debuginfo.is_empty() {
let env_arg = mir::Local::new(1);
let mut env_projs = vec![];
let pin_did = tcx.lang_items().pin_type();
match body.local_decls[env_arg].ty.kind {
ty::RawPtr(_) |
ty::Ref(..) => {
env_projs.push(mir::ProjectionElem::Deref);
}
ty::Adt(def, substs) if Some(def.did) == pin_did => {
if let ty::Ref(..) = substs.type_at(0).kind {
env_projs.push(mir::ProjectionElem::Field(
mir::Field::new(0),
// HACK(eddyb) field types aren't used or needed here.
tcx.types.err,
));
env_projs.push(mir::ProjectionElem::Deref);
}
}
_ => {}
}
let extra_locals = {
let upvars = upvar_debuginfo
.iter()
.enumerate()
.map(|(i, upvar)| {
let source_info = mir::SourceInfo {
span: body.span,
scope: mir::OUTERMOST_SOURCE_SCOPE,
};
(None, i, upvar.debug_name, upvar.by_ref, source_info)
});
let generator_fields = body.generator_layout.as_ref().map(|generator_layout| {
generator_layout.variant_fields.iter()
.enumerate()
.flat_map(move |(variant_idx, fields)| {
let variant_idx = Some(VariantIdx::from(variant_idx));
fields.iter()
.enumerate()
.filter_map(move |(i, field)| {
let decl = &generator_layout.
__local_debuginfo_codegen_only_do_not_use[*field];
if let Some(name) = decl.name {
let source_info = mir::SourceInfo {
span: decl.source_info.span,
scope: decl.visibility_scope,
};
Some((variant_idx, i, name, false, source_info))
} else {
None
}
})
})
}).into_iter().flatten();
upvars.chain(generator_fields)
};
for (variant_idx, field, name, by_ref, source_info) in extra_locals {
let mut projs = env_projs.clone();
if let Some(variant_idx) = variant_idx {
projs.push(mir::ProjectionElem::Downcast(None, variant_idx));
}
projs.push(mir::ProjectionElem::Field(
mir::Field::new(field),
// HACK(eddyb) field types aren't used or needed here.
tcx.types.err,
));
if by_ref {
projs.push(mir::ProjectionElem::Deref);
}
per_local[env_arg].push(VarDebugInfo {
name,
source_info,
place: mir::Place {
base: mir::PlaceBase::Local(env_arg),
projection: tcx.intern_place_elems(&projs),
},
});
}
}
Some(per_local)
} else {
None
}
}
/// Debug information relatating to an user variable.
// FIXME(eddyb) move this to the MIR bodies themselves.
#[derive(Clone)]
pub struct VarDebugInfo<'tcx> {
pub name: Symbol,
/// Source info of the user variable, including the scope
/// within which the variable is visible (to debuginfo)
/// (see `LocalDecl`'s `source_info` field for more details).
pub source_info: mir::SourceInfo,
/// Where the data for this user variable is to be found.
pub place: mir::Place<'tcx>,
}
|