blob: 8deab1be3d3a93f6c04534baaab6c79a47c92ef6 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001// See doc.rs for documentation.
2mod doc;
3
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -08004use rustc_codegen_ssa::mir::debuginfo::VariableKind::*;
Inna Palantff3f07a2019-07-11 16:15:26 -07005
Matthew Maurer859223d2020-03-27 12:47:38 -07006use self::metadata::{file_metadata, type_metadata, TypeMap};
Inna Palantff3f07a2019-07-11 16:15:26 -07007use self::namespace::mangled_name_of_instance;
Matthew Maurer859223d2020-03-27 12:47:38 -07008use self::type_names::compute_debuginfo_type_name;
9use self::utils::{create_DIArray, is_node_local_to_unit, span_start, DIB};
Inna Palantff3f07a2019-07-11 16:15:26 -070010
11use crate::llvm;
Matthew Maurer859223d2020-03-27 12:47:38 -070012use crate::llvm::debuginfo::{
Matthew Maurer15a65602020-04-24 14:05:21 -070013 DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DISPFlags, DIScope, DIType, DIVariable,
Matthew Maurer859223d2020-03-27 12:47:38 -070014};
Matthew Maurer859223d2020-03-27 12:47:38 -070015use rustc::ty::subst::{GenericArgKind, SubstsRef};
16use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
Inna Palantff3f07a2019-07-11 16:15:26 -070017
Matthew Maurerf4d8f812020-03-27 13:14:30 -070018use crate::abi::FnAbi;
Inna Palantff3f07a2019-07-11 16:15:26 -070019use crate::builder::Builder;
Matthew Maurer859223d2020-03-27 12:47:38 -070020use crate::common::CodegenCx;
Inna Palantff3f07a2019-07-11 16:15:26 -070021use crate::value::Value;
Inna Palantff3f07a2019-07-11 16:15:26 -070022use rustc::mir;
23use rustc::session::config::{self, DebugInfo};
Matthew Maurer15a65602020-04-24 14:05:21 -070024use rustc::ty::{self, Instance, ParamEnv, Ty};
Matthew Maurer859223d2020-03-27 12:47:38 -070025use rustc_codegen_ssa::debuginfo::type_names;
26use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind};
27use rustc_data_structures::fx::{FxHashMap, FxHashSet};
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -080028use rustc_index::vec::IndexVec;
Inna Palantff3f07a2019-07-11 16:15:26 -070029
30use libc::c_uint;
Matthew Maurer859223d2020-03-27 12:47:38 -070031use log::debug;
Inna Palantff3f07a2019-07-11 16:15:26 -070032use std::cell::RefCell;
Inna Palantff3f07a2019-07-11 16:15:26 -070033
Matthew Maurer859223d2020-03-27 12:47:38 -070034use rustc::ty::layout::{self, HasTyCtxt, LayoutOf, Size};
Matthew Maurer15a65602020-04-24 14:05:21 -070035use rustc_ast::ast;
Inna Palantff3f07a2019-07-11 16:15:26 -070036use rustc_codegen_ssa::traits::*;
Matthew Maurer859223d2020-03-27 12:47:38 -070037use rustc_span::symbol::Symbol;
Matthew Maurer15a65602020-04-24 14:05:21 -070038use rustc_span::{self, BytePos, Span};
Matthew Maurer859223d2020-03-27 12:47:38 -070039use smallvec::SmallVec;
Inna Palantff3f07a2019-07-11 16:15:26 -070040
Inna Palantff3f07a2019-07-11 16:15:26 -070041mod create_scope_map;
Matthew Maurer859223d2020-03-27 12:47:38 -070042pub mod gdb;
43pub mod metadata;
44mod namespace;
Inna Palantff3f07a2019-07-11 16:15:26 -070045mod source_loc;
Matthew Maurer859223d2020-03-27 12:47:38 -070046mod utils;
Inna Palantff3f07a2019-07-11 16:15:26 -070047
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -080048pub use self::create_scope_map::compute_mir_scopes;
Inna Palantff3f07a2019-07-11 16:15:26 -070049pub use self::metadata::create_global_var_metadata;
50pub use self::metadata::extend_scope_to_file;
Inna Palantff3f07a2019-07-11 16:15:26 -070051
52#[allow(non_upper_case_globals)]
53const DW_TAG_auto_variable: c_uint = 0x100;
54#[allow(non_upper_case_globals)]
55const DW_TAG_arg_variable: c_uint = 0x101;
56
57/// A context object for maintaining all state needed by the debuginfo module.
58pub struct CrateDebugContext<'a, 'tcx> {
59 llcontext: &'a llvm::Context,
60 llmod: &'a llvm::Module,
61 builder: &'a mut DIBuilder<'a>,
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -080062 created_files: RefCell<FxHashMap<(Option<String>, Option<String>), &'a DIFile>>,
Inna Palantff3f07a2019-07-11 16:15:26 -070063 created_enum_disr_types: RefCell<FxHashMap<(DefId, layout::Primitive), &'a DIType>>,
64
65 type_map: RefCell<TypeMap<'a, 'tcx>>,
66 namespace_map: RefCell<DefIdMap<&'a DIScope>>,
67
68 // This collection is used to assert that composite types (structs, enums,
69 // ...) have their members only set once:
70 composite_types_completed: RefCell<FxHashSet<&'a DIType>>,
71}
72
73impl Drop for CrateDebugContext<'a, 'tcx> {
74 fn drop(&mut self) {
75 unsafe {
76 llvm::LLVMRustDIBuilderDispose(&mut *(self.builder as *mut _));
77 }
78 }
79}
80
81impl<'a, 'tcx> CrateDebugContext<'a, 'tcx> {
82 pub fn new(llmod: &'a llvm::Module) -> Self {
83 debug!("CrateDebugContext::new");
84 let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) };
85 // DIBuilder inherits context from the module, so we'd better use the same one
86 let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) };
87 CrateDebugContext {
88 llcontext,
89 llmod,
90 builder,
91 created_files: Default::default(),
92 created_enum_disr_types: Default::default(),
93 type_map: Default::default(),
94 namespace_map: RefCell::new(Default::default()),
95 composite_types_completed: Default::default(),
96 }
97 }
98}
99
100/// Creates any deferred debug metadata nodes
101pub fn finalize(cx: &CodegenCx<'_, '_>) {
102 if cx.dbg_cx.is_none() {
103 return;
104 }
105
106 debug!("finalize");
107
108 if gdb::needs_gdb_debug_scripts_section(cx) {
109 // Add a .debug_gdb_scripts section to this compile-unit. This will
110 // cause GDB to try and load the gdb_load_rust_pretty_printers.py file,
111 // which activates the Rust pretty printers for binary this section is
112 // contained in.
113 gdb::get_or_insert_gdb_debug_scripts_section_global(cx);
114 }
115
116 unsafe {
117 llvm::LLVMRustDIBuilderFinalize(DIB(cx));
118 // Debuginfo generation in LLVM by default uses a higher
119 // version of dwarf than macOS currently understands. We can
120 // instruct LLVM to emit an older version of dwarf, however,
121 // for macOS to understand. For more info see #11352
122 // This can be overridden using --llvm-opts -dwarf-version,N.
123 // Android has the same issue (#22398)
Matthew Maurer859223d2020-03-27 12:47:38 -0700124 if cx.sess().target.target.options.is_like_osx
125 || cx.sess().target.target.options.is_like_android
126 {
127 llvm::LLVMRustAddModuleFlag(cx.llmod, "Dwarf Version\0".as_ptr().cast(), 2)
Inna Palantff3f07a2019-07-11 16:15:26 -0700128 }
129
130 // Indicate that we want CodeView debug information on MSVC
131 if cx.sess().target.target.options.is_like_msvc {
Matthew Maurer859223d2020-03-27 12:47:38 -0700132 llvm::LLVMRustAddModuleFlag(cx.llmod, "CodeView\0".as_ptr().cast(), 1)
Inna Palantff3f07a2019-07-11 16:15:26 -0700133 }
134
135 // Prevent bitcode readers from deleting the debug info.
136 let ptr = "Debug Info Version\0".as_ptr();
Matthew Maurer859223d2020-03-27 12:47:38 -0700137 llvm::LLVMRustAddModuleFlag(cx.llmod, ptr.cast(), llvm::LLVMRustDebugMetadataVersion());
Inna Palantff3f07a2019-07-11 16:15:26 -0700138 };
139}
140
Matthew Maurer15a65602020-04-24 14:05:21 -0700141impl DebugInfoBuilderMethods for Builder<'a, 'll, 'tcx> {
142 // FIXME(eddyb) find a common convention for all of the debuginfo-related
143 // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
144 fn dbg_var_addr(
Inna Palantff3f07a2019-07-11 16:15:26 -0700145 &mut self,
Matthew Maurer15a65602020-04-24 14:05:21 -0700146 dbg_var: &'ll DIVariable,
Inna Palantff3f07a2019-07-11 16:15:26 -0700147 scope_metadata: &'ll DIScope,
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800148 variable_alloca: Self::Value,
149 direct_offset: Size,
150 indirect_offsets: &[Size],
Inna Palantff3f07a2019-07-11 16:15:26 -0700151 span: Span,
152 ) {
Inna Palantff3f07a2019-07-11 16:15:26 -0700153 let cx = self.cx();
154
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800155 // Convert the direct and indirect offsets to address ops.
Matthew Maurer15a65602020-04-24 14:05:21 -0700156 // FIXME(eddyb) use `const`s instead of getting the values via FFI,
157 // the values should match the ones in the DWARF standard anyway.
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800158 let op_deref = || unsafe { llvm::LLVMRustDIBuilderCreateOpDeref() };
159 let op_plus_uconst = || unsafe { llvm::LLVMRustDIBuilderCreateOpPlusUconst() };
160 let mut addr_ops = SmallVec::<[_; 8]>::new();
Inna Palantff3f07a2019-07-11 16:15:26 -0700161
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800162 if direct_offset.bytes() > 0 {
163 addr_ops.push(op_plus_uconst());
164 addr_ops.push(direct_offset.bytes() as i64);
165 }
166 for &offset in indirect_offsets {
167 addr_ops.push(op_deref());
168 if offset.bytes() > 0 {
169 addr_ops.push(op_plus_uconst());
170 addr_ops.push(offset.bytes() as i64);
Inna Palantff3f07a2019-07-11 16:15:26 -0700171 }
172 }
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800173
Matthew Maurer15a65602020-04-24 14:05:21 -0700174 // FIXME(eddyb) maybe this information could be extracted from `dbg_var`,
175 // to avoid having to pass it down in both places?
176 // NB: `var` doesn't seem to know about the column, so that's a limitation.
177 let dbg_loc = cx.create_debug_loc(scope_metadata, span);
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800178 unsafe {
Matthew Maurer15a65602020-04-24 14:05:21 -0700179 // FIXME(eddyb) replace `llvm.dbg.declare` with `llvm.dbg.addr`.
180 llvm::LLVMRustDIBuilderInsertDeclareAtEnd(
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800181 DIB(cx),
182 variable_alloca,
Matthew Maurer15a65602020-04-24 14:05:21 -0700183 dbg_var,
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800184 addr_ops.as_ptr(),
185 addr_ops.len() as c_uint,
Matthew Maurer15a65602020-04-24 14:05:21 -0700186 dbg_loc,
Matthew Maurer859223d2020-03-27 12:47:38 -0700187 self.llbb(),
188 );
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800189 }
Inna Palantff3f07a2019-07-11 16:15:26 -0700190 }
191
Matthew Maurer15a65602020-04-24 14:05:21 -0700192 fn set_source_location(&mut self, scope: &'ll DIScope, span: Span) {
193 debug!("set_source_location: {}", self.sess().source_map().span_to_string(span));
194
195 let dbg_loc = self.cx().create_debug_loc(scope, span);
196
197 unsafe {
198 llvm::LLVMSetCurrentDebugLocation(self.llbuilder, dbg_loc);
199 }
Inna Palantff3f07a2019-07-11 16:15:26 -0700200 }
201 fn insert_reference_to_gdb_debug_scripts_section_global(&mut self) {
202 gdb::insert_reference_to_gdb_debug_scripts_section_global(self)
203 }
204
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800205 fn set_var_name(&mut self, value: &'ll Value, name: &str) {
Chih-Hung Hsieh8cd2c992019-12-19 15:08:11 -0800206 // Avoid wasting time if LLVM value names aren't even enabled.
207 if self.sess().fewer_names() {
208 return;
209 }
210
211 // Only function parameters and instructions are local to a function,
212 // don't change the name of anything else (e.g. globals).
213 let param_or_inst = unsafe {
Matthew Maurer859223d2020-03-27 12:47:38 -0700214 llvm::LLVMIsAArgument(value).is_some() || llvm::LLVMIsAInstruction(value).is_some()
Chih-Hung Hsieh8cd2c992019-12-19 15:08:11 -0800215 };
216 if !param_or_inst {
217 return;
218 }
219
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700220 // Avoid replacing the name if it already exists.
221 // While we could combine the names somehow, it'd
222 // get noisy quick, and the usefulness is dubious.
223 if llvm::get_value_name(value).is_empty() {
224 llvm::set_value_name(value, name.as_bytes());
Inna Palantff3f07a2019-07-11 16:15:26 -0700225 }
226 }
227}
228
229impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
230 fn create_function_debug_context(
231 &self,
232 instance: Instance<'tcx>,
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700233 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
Inna Palantff3f07a2019-07-11 16:15:26 -0700234 llfn: &'ll Value,
Chih-Hung Hsiehda60c852019-12-19 14:56:55 -0800235 mir: &mir::Body<'_>,
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800236 ) -> Option<FunctionDebugContext<&'ll DIScope>> {
Inna Palantff3f07a2019-07-11 16:15:26 -0700237 if self.sess().opts.debuginfo == DebugInfo::None {
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800238 return None;
Inna Palantff3f07a2019-07-11 16:15:26 -0700239 }
240
Inna Palantff3f07a2019-07-11 16:15:26 -0700241 let span = mir.span;
242
243 // This can be the case for functions inlined from another crate
244 if span.is_dummy() {
245 // FIXME(simulacrum): Probably can't happen; remove.
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800246 return None;
Inna Palantff3f07a2019-07-11 16:15:26 -0700247 }
248
249 let def_id = instance.def_id();
250 let containing_scope = get_containing_scope(self, instance);
251 let loc = span_start(self, span);
252 let file_metadata = file_metadata(self, &loc.file.name, def_id.krate);
253
254 let function_type_metadata = unsafe {
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700255 let fn_signature = get_function_signature(self, fn_abi);
Inna Palantff3f07a2019-07-11 16:15:26 -0700256 llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(self), file_metadata, fn_signature)
257 };
258
259 // Find the enclosing function, in case this is a closure.
260 let def_key = self.tcx().def_key(def_id);
261 let mut name = def_key.disambiguated_data.data.to_string();
262
263 let enclosing_fn_def_id = self.tcx().closure_base_def_id(def_id);
264
265 // Get_template_parameters() will append a `<...>` clause to the function
266 // name if necessary.
267 let generics = self.tcx().generics_of(enclosing_fn_def_id);
268 let substs = instance.substs.truncate_to(self.tcx(), generics);
Matthew Maurer859223d2020-03-27 12:47:38 -0700269 let template_parameters =
270 get_template_parameters(self, &generics, substs, file_metadata, &mut name);
Inna Palantff3f07a2019-07-11 16:15:26 -0700271
272 // Get the linkage_name, which is just the symbol name
273 let linkage_name = mangled_name_of_instance(self, instance);
Matthew Maurer15a65602020-04-24 14:05:21 -0700274 let linkage_name = linkage_name.name.as_str();
Inna Palantff3f07a2019-07-11 16:15:26 -0700275
Matthew Maurer15a65602020-04-24 14:05:21 -0700276 // FIXME(eddyb) does this need to be separate from `loc.line` for some reason?
277 let scope_line = loc.line;
Inna Palantff3f07a2019-07-11 16:15:26 -0700278
279 let mut flags = DIFlags::FlagPrototyped;
280
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700281 if fn_abi.ret.layout.abi.is_uninhabited() {
Inna Palantff3f07a2019-07-11 16:15:26 -0700282 flags |= DIFlags::FlagNoReturn;
283 }
284
285 let mut spflags = DISPFlags::SPFlagDefinition;
286 if is_node_local_to_unit(self, def_id) {
287 spflags |= DISPFlags::SPFlagLocalToUnit;
288 }
289 if self.sess().opts.optimize != config::OptLevel::No {
290 spflags |= DISPFlags::SPFlagOptimized;
291 }
292 if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) {
293 if id == def_id {
294 spflags |= DISPFlags::SPFlagMainSubprogram;
295 }
296 }
297
298 let fn_metadata = unsafe {
299 llvm::LLVMRustDIBuilderCreateFunction(
300 DIB(self),
301 containing_scope,
Matthew Maurer15a65602020-04-24 14:05:21 -0700302 name.as_ptr().cast(),
303 name.len(),
304 linkage_name.as_ptr().cast(),
305 linkage_name.len(),
Inna Palantff3f07a2019-07-11 16:15:26 -0700306 file_metadata,
307 loc.line as c_uint,
308 function_type_metadata,
309 scope_line as c_uint,
310 flags,
311 spflags,
312 llfn,
313 template_parameters,
Matthew Maurer859223d2020-03-27 12:47:38 -0700314 None,
315 )
Inna Palantff3f07a2019-07-11 16:15:26 -0700316 };
317
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800318 // Initialize fn debug context (including scopes).
319 // FIXME(eddyb) figure out a way to not need `Option` for `scope_metadata`.
320 let null_scope = DebugScope {
321 scope_metadata: None,
322 file_start_pos: BytePos(0),
Matthew Maurer859223d2020-03-27 12:47:38 -0700323 file_end_pos: BytePos(0),
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800324 };
325 let mut fn_debug_context = FunctionDebugContext {
326 scopes: IndexVec::from_elem(null_scope, &mir.source_scopes),
Inna Palantff3f07a2019-07-11 16:15:26 -0700327 defining_crate: def_id.krate,
328 };
329
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800330 // Fill in all the scopes, with the information from the MIR body.
331 compute_mir_scopes(self, mir, fn_metadata, &mut fn_debug_context);
332
333 return Some(fn_debug_context);
Inna Palantff3f07a2019-07-11 16:15:26 -0700334
335 fn get_function_signature<'ll, 'tcx>(
336 cx: &CodegenCx<'ll, 'tcx>,
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700337 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
Inna Palantff3f07a2019-07-11 16:15:26 -0700338 ) -> &'ll DIArray {
339 if cx.sess().opts.debuginfo == DebugInfo::Limited {
340 return create_DIArray(DIB(cx), &[]);
341 }
342
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700343 let mut signature = Vec::with_capacity(fn_abi.args.len() + 1);
Inna Palantff3f07a2019-07-11 16:15:26 -0700344
345 // Return type -- llvm::DIBuilder wants this at index 0
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700346 signature.push(if fn_abi.ret.is_ignore() {
347 None
Inna Palantff3f07a2019-07-11 16:15:26 -0700348 } else {
Matthew Maurer859223d2020-03-27 12:47:38 -0700349 Some(type_metadata(cx, fn_abi.ret.layout.ty, rustc_span::DUMMY_SP))
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700350 });
Inna Palantff3f07a2019-07-11 16:15:26 -0700351
352 // Arguments types
353 if cx.sess().target.target.options.is_like_msvc {
354 // FIXME(#42800):
355 // There is a bug in MSDIA that leads to a crash when it encounters
356 // a fixed-size array of `u8` or something zero-sized in a
357 // function-type (see #40477).
358 // As a workaround, we replace those fixed-size arrays with a
359 // pointer-type. So a function `fn foo(a: u8, b: [u8; 4])` would
360 // appear as `fn foo(a: u8, b: *const u8)` in debuginfo,
361 // and a function `fn bar(x: [(); 7])` as `fn bar(x: *const ())`.
362 // This transformed type is wrong, but these function types are
363 // already inaccurate due to ABI adjustments (see #42800).
Matthew Maurerf4d8f812020-03-27 13:14:30 -0700364 signature.extend(fn_abi.args.iter().map(|arg| {
365 let t = arg.layout.ty;
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800366 let t = match t.kind {
Inna Palantff3f07a2019-07-11 16:15:26 -0700367 ty::Array(ct, _)
Matthew Maurer859223d2020-03-27 12:47:38 -0700368 if (ct == cx.tcx.types.u8) || cx.layout_of(ct).is_zst() =>
369 {
Inna Palantff3f07a2019-07-11 16:15:26 -0700370 cx.tcx.mk_imm_ptr(ct)
371 }
Matthew Maurer859223d2020-03-27 12:47:38 -0700372 _ => t,
Inna Palantff3f07a2019-07-11 16:15:26 -0700373 };
Matthew Maurer859223d2020-03-27 12:47:38 -0700374 Some(type_metadata(cx, t, rustc_span::DUMMY_SP))
Inna Palantff3f07a2019-07-11 16:15:26 -0700375 }));
376 } else {
Matthew Maurer859223d2020-03-27 12:47:38 -0700377 signature.extend(
378 fn_abi
379 .args
380 .iter()
381 .map(|arg| Some(type_metadata(cx, arg.layout.ty, rustc_span::DUMMY_SP))),
382 );
Inna Palantff3f07a2019-07-11 16:15:26 -0700383 }
384
Inna Palantff3f07a2019-07-11 16:15:26 -0700385 create_DIArray(DIB(cx), &signature[..])
386 }
387
388 fn get_template_parameters<'ll, 'tcx>(
389 cx: &CodegenCx<'ll, 'tcx>,
390 generics: &ty::Generics,
391 substs: SubstsRef<'tcx>,
392 file_metadata: &'ll DIFile,
393 name_to_append_suffix_to: &mut String,
394 ) -> &'ll DIArray {
395 if substs.types().next().is_none() {
396 return create_DIArray(DIB(cx), &[]);
397 }
398
399 name_to_append_suffix_to.push('<');
400 for (i, actual_type) in substs.types().enumerate() {
401 if i != 0 {
402 name_to_append_suffix_to.push_str(",");
403 }
404
405 let actual_type =
406 cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), actual_type);
407 // Add actual type name to <...> clause of function name
Matthew Maurer859223d2020-03-27 12:47:38 -0700408 let actual_type_name = compute_debuginfo_type_name(cx.tcx(), actual_type, true);
Inna Palantff3f07a2019-07-11 16:15:26 -0700409 name_to_append_suffix_to.push_str(&actual_type_name[..]);
410 }
411 name_to_append_suffix_to.push('>');
412
413 // Again, only create type information if full debuginfo is enabled
414 let template_params: Vec<_> = if cx.sess().opts.debuginfo == DebugInfo::Full {
415 let names = get_parameter_names(cx, generics);
Matthew Maurer859223d2020-03-27 12:47:38 -0700416 substs
417 .iter()
418 .zip(names)
419 .filter_map(|(kind, name)| {
420 if let GenericArgKind::Type(ty) = kind.unpack() {
421 let actual_type =
422 cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
423 let actual_type_metadata =
424 type_metadata(cx, actual_type, rustc_span::DUMMY_SP);
Matthew Maurer15a65602020-04-24 14:05:21 -0700425 let name = name.as_str();
Matthew Maurer859223d2020-03-27 12:47:38 -0700426 Some(unsafe {
427 Some(llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
428 DIB(cx),
429 None,
Matthew Maurer15a65602020-04-24 14:05:21 -0700430 name.as_ptr().cast(),
431 name.len(),
Matthew Maurer859223d2020-03-27 12:47:38 -0700432 actual_type_metadata,
433 file_metadata,
434 0,
435 0,
436 ))
437 })
438 } else {
439 None
440 }
441 })
442 .collect()
Inna Palantff3f07a2019-07-11 16:15:26 -0700443 } else {
444 vec![]
445 };
446
447 return create_DIArray(DIB(cx), &template_params[..]);
448 }
449
Matthew Maurer859223d2020-03-27 12:47:38 -0700450 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
451 let mut names = generics
452 .parent
453 .map_or(vec![], |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
Inna Palantff3f07a2019-07-11 16:15:26 -0700454 names.extend(generics.params.iter().map(|param| param.name));
455 names
456 }
457
458 fn get_containing_scope<'ll, 'tcx>(
459 cx: &CodegenCx<'ll, 'tcx>,
460 instance: Instance<'tcx>,
461 ) -> &'ll DIScope {
462 // First, let's see if this is a method within an inherent impl. Because
463 // if yes, we want to make the result subroutine DIE a child of the
464 // subroutine's self-type.
465 let self_type = cx.tcx.impl_of_method(instance.def_id()).and_then(|impl_def_id| {
466 // If the method does *not* belong to a trait, proceed
467 if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
468 let impl_self_ty = cx.tcx.subst_and_normalize_erasing_regions(
469 instance.substs,
470 ty::ParamEnv::reveal_all(),
471 &cx.tcx.type_of(impl_def_id),
472 );
473
474 // Only "class" methods are generally understood by LLVM,
475 // so avoid methods on other types (e.g., `<*mut T>::null`).
Chih-Hung Hsieh2ccedcd2019-12-19 15:10:50 -0800476 match impl_self_ty.kind {
Inna Palantff3f07a2019-07-11 16:15:26 -0700477 ty::Adt(def, ..) if !def.is_box() => {
Matthew Maurer859223d2020-03-27 12:47:38 -0700478 Some(type_metadata(cx, impl_self_ty, rustc_span::DUMMY_SP))
Inna Palantff3f07a2019-07-11 16:15:26 -0700479 }
Matthew Maurer859223d2020-03-27 12:47:38 -0700480 _ => None,
Inna Palantff3f07a2019-07-11 16:15:26 -0700481 }
482 } else {
483 // For trait method impls we still use the "parallel namespace"
484 // strategy
485 None
486 }
487 });
488
489 self_type.unwrap_or_else(|| {
Matthew Maurer859223d2020-03-27 12:47:38 -0700490 namespace::item_namespace(
491 cx,
492 DefId {
493 krate: instance.def_id().krate,
494 index: cx
495 .tcx
496 .def_key(instance.def_id())
497 .parent
498 .expect("get_containing_scope: missing parent?"),
499 },
500 )
Inna Palantff3f07a2019-07-11 16:15:26 -0700501 })
502 }
503 }
504
Matthew Maurer859223d2020-03-27 12:47:38 -0700505 fn create_vtable_metadata(&self, ty: Ty<'tcx>, vtable: Self::Value) {
Inna Palantff3f07a2019-07-11 16:15:26 -0700506 metadata::create_vtable_metadata(self, ty, vtable)
507 }
508
Inna Palantff3f07a2019-07-11 16:15:26 -0700509 fn extend_scope_to_file(
Matthew Maurer859223d2020-03-27 12:47:38 -0700510 &self,
511 scope_metadata: &'ll DIScope,
512 file: &rustc_span::SourceFile,
513 defining_crate: CrateNum,
514 ) -> &'ll DILexicalBlock {
515 metadata::extend_scope_to_file(&self, scope_metadata, file, defining_crate)
516 }
Inna Palantff3f07a2019-07-11 16:15:26 -0700517
518 fn debuginfo_finalize(&self) {
519 finalize(self)
520 }
Matthew Maurer15a65602020-04-24 14:05:21 -0700521
522 // FIXME(eddyb) find a common convention for all of the debuginfo-related
523 // names (choose between `dbg`, `debug`, `debuginfo`, `debug_info` etc.).
524 fn create_dbg_var(
525 &self,
526 dbg_context: &FunctionDebugContext<&'ll DIScope>,
527 variable_name: ast::Name,
528 variable_type: Ty<'tcx>,
529 scope_metadata: &'ll DIScope,
530 variable_kind: VariableKind,
531 span: Span,
532 ) -> &'ll DIVariable {
533 let loc = span_start(self, span);
534 let file_metadata = file_metadata(self, &loc.file.name, dbg_context.defining_crate);
535
536 let type_metadata = type_metadata(self, variable_type, span);
537
538 let (argument_index, dwarf_tag) = match variable_kind {
539 ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable),
540 LocalVariable => (0, DW_TAG_auto_variable),
541 };
542 let align = self.align_of(variable_type);
543
544 let name = variable_name.as_str();
545 unsafe {
546 llvm::LLVMRustDIBuilderCreateVariable(
547 DIB(self),
548 dwarf_tag,
549 scope_metadata,
550 name.as_ptr().cast(),
551 name.len(),
552 file_metadata,
553 loc.line as c_uint,
554 type_metadata,
555 true,
556 DIFlags::FlagZero,
557 argument_index,
558 align.bytes() as u32,
559 )
560 }
561 }
Inna Palantff3f07a2019-07-11 16:15:26 -0700562}