forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdebuginfo.rs
More file actions
1229 lines (1057 loc) · 42.1 KB
/
Copy pathdebuginfo.rs
File metadata and controls
1229 lines (1057 loc) · 42.1 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
# Debug Info Module
This module serves the purpose of generating debug symbols. We use LLVM's
[source level debugging](http://llvm.org/docs/SourceLevelDebugging.html) features for generating
the debug information. The general principle is this:
Given the right metadata in the LLVM IR, the LLVM code generator is able to create DWARF debug
symbols for the given code. The [metadata](http://llvm.org/docs/LangRef.html#metadata-type) is
structured much like DWARF *debugging information entries* (DIE), representing type information
such as datatype layout, function signatures, block layout, variable location and scope information,
etc. It is the purpose of this module to generate correct metadata and insert it into the LLVM IR.
As the exact format of metadata trees may change between different LLVM versions, we now use LLVM
[DIBuilder](http://llvm.org/docs/doxygen/html/classllvm_1_1DIBuilder.html) to create metadata
where possible. This will hopefully ease the adaption of this module to future LLVM versions.
The public API of the module is a set of functions that will insert the correct metadata into the
LLVM IR when called with the right parameters. The module is thus driven from an outside client with
functions like `debuginfo::local_var_metadata(bcx: block, local: &ast::local)`.
Internally the module will try to reuse already created metadata by utilizing a cache. The way to
get a shared metadata node when needed is thus to just call the corresponding function in this
module:
let file_metadata = file_metadata(crate_context, path);
The function will take care of probing the cache for an existing node for that exact file path.
All private state used by the module is stored within a DebugContext struct, which in turn is
contained in the CrateContext.
This file consists of three conceptual sections:
1. The public interface of the module
2. Module-internal metadata creation functions
3. Minor utility functions
*/
use driver::session;
use lib::llvm::llvm;
use lib::llvm::{ModuleRef, ContextRef};
use lib::llvm::debuginfo::*;
use middle::trans::common::*;
use middle::trans::machine;
use middle::trans::type_of;
use middle::trans::type_::Type;
use middle::trans::adt;
use middle::trans;
use middle::ty;
use util::ppaux::ty_to_str;
use std::hashmap::HashMap;
use std::libc::{c_uint, c_ulonglong, c_longlong};
use std::ptr;
use std::vec;
use syntax::codemap::span;
use syntax::{ast, codemap, ast_util, ast_map};
static DW_LANG_RUST: int = 0x9000;
static DW_TAG_auto_variable: int = 0x100;
static DW_TAG_arg_variable: int = 0x101;
static DW_ATE_boolean: int = 0x02;
static DW_ATE_float: int = 0x04;
static DW_ATE_signed: int = 0x05;
static DW_ATE_signed_char: int = 0x06;
static DW_ATE_unsigned: int = 0x07;
static DW_ATE_unsigned_char: int = 0x08;
//=-------------------------------------------------------------------------------------------------
// Public Interface of debuginfo module
//=-------------------------------------------------------------------------------------------------
/// A context object for maintaining all state needed by the debuginfo module.
pub struct DebugContext {
crate_file: ~str,
llcontext: ContextRef,
builder: DIBuilderRef,
curr_loc: (uint, uint),
created_files: HashMap<~str, DIFile>,
created_functions: HashMap<ast::node_id, DISubprogram>,
created_blocks: HashMap<ast::node_id, DILexicalBlock>,
created_types: HashMap<uint, DIType>
}
impl DebugContext {
pub fn new(llmod: ModuleRef, crate: ~str) -> DebugContext {
debug!("DebugContext::new");
let builder = unsafe { llvm::LLVMDIBuilderCreate(llmod) };
// DIBuilder inherits context from the module, so we'd better use the same one
let llcontext = unsafe { llvm::LLVMGetModuleContext(llmod) };
return DebugContext {
crate_file: crate,
llcontext: llcontext,
builder: builder,
curr_loc: (0, 0),
created_files: HashMap::new(),
created_functions: HashMap::new(),
created_blocks: HashMap::new(),
created_types: HashMap::new(),
};
}
}
/// Create any deferred debug metadata nodes
pub fn finalize(cx: @mut CrateContext) {
debug!("finalize");
compile_unit_metadata(cx);
unsafe {
llvm::LLVMDIBuilderFinalize(DIB(cx));
llvm::LLVMDIBuilderDispose(DIB(cx));
};
}
/// Creates debug information for the given local variable.
///
/// Adds the created metadata nodes directly to the crate's IR.
/// The return value should be ignored if called from outside of the debuginfo module.
pub fn create_local_var_metadata(bcx: @mut Block, local: @ast::Local) -> DIVariable {
let cx = bcx.ccx();
let ident = match local.pat.node {
ast::pat_ident(_, ref pth, _) => ast_util::path_to_ident(pth),
// FIXME this should be handled (#2533)
_ => {
bcx.sess().span_note(local.span, "debuginfo for pattern bindings NYI");
return ptr::null();
}
};
let name: &str = cx.sess.str_of(ident);
debug!("create_local_var_metadata: %s", name);
let loc = span_start(cx, local.span);
let ty = node_id_type(bcx, local.id);
let type_metadata = type_metadata(cx, ty, local.ty.span);
let file_metadata = file_metadata(cx, loc.file.name);
let context = match bcx.parent {
None => create_function_metadata(bcx.fcx),
Some(_) => lexical_block_metadata(bcx)
};
let var_metadata = do name.as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreateLocalVariable(
DIB(cx),
DW_TAG_auto_variable as u32,
context,
name,
file_metadata,
loc.line as c_uint,
type_metadata,
false,
0,
0)
}
};
// FIXME(#6814) Should use `pat_util::pat_bindings` for pats like (a, b) etc
let llptr = match bcx.fcx.lllocals.find_copy(&local.pat.id) {
Some(v) => v,
None => {
bcx.tcx().sess.span_bug(
local.span,
fmt!("No entry in lllocals table for %?", local.id));
}
};
set_debug_location(cx, lexical_block_metadata(bcx), loc.line, loc.col.to_uint());
unsafe {
let instr = llvm::LLVMDIBuilderInsertDeclareAtEnd(DIB(cx), llptr, var_metadata, bcx.llbb);
llvm::LLVMSetInstDebugLocation(trans::build::B(bcx).llbuilder, instr);
}
return var_metadata;
}
/// Creates debug information for the given function argument.
///
/// Adds the created metadata nodes directly to the crate's IR.
/// The return value should be ignored if called from outside of the debuginfo module.
pub fn create_argument_metadata(bcx: @mut Block, arg: ast::arg, span: span) -> Option<DIVariable> {
debug!("create_argument_metadata");
if true {
// XXX create_argument_metadata disabled for now because "node_id_type(bcx, arg.id)" below
// blows up:
// "error: internal compiler error: node_id_to_type: no type for node `arg (id=10)`"
return None;
}
let fcx = bcx.fcx;
let cx = fcx.ccx;
let loc = span_start(cx, span);
if "<intrinsic>" == loc.file.name {
return None;
}
let ty = node_id_type(bcx, arg.id);
let type_metadata = type_metadata(cx, ty, arg.ty.span);
let file_metadata = file_metadata(cx, loc.file.name);
let context = create_function_metadata(fcx);
match arg.pat.node {
ast::pat_ident(_, ref path, _) => {
// XXX: This is wrong; it should work for multiple bindings.
let ident = path.idents.last();
let name: &str = cx.sess.str_of(*ident);
let var_metadata = do name.as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreateLocalVariable(
DIB(cx),
DW_TAG_arg_variable as u32,
context,
name,
file_metadata,
loc.line as c_uint,
type_metadata,
false,
0,
0)
// XXX need to pass in a real argument number
}
};
let llptr = fcx.llargs.get_copy(&arg.id);
set_debug_location(cx, lexical_block_metadata(bcx), loc.line, loc.col.to_uint());
unsafe {
let instr = llvm::LLVMDIBuilderInsertDeclareAtEnd(
DIB(cx), llptr, var_metadata, bcx.llbb);
llvm::LLVMSetInstDebugLocation(trans::build::B(bcx).llbuilder, instr);
}
return Some(var_metadata);
}
_ => {
return None;
}
}
}
/// Sets the current debug location at the beginning of the span
///
/// Maps to a call to llvm::LLVMSetCurrentDebugLocation(...)
pub fn update_source_pos(bcx: @mut Block, span: span) {
if !bcx.sess().opts.debuginfo || (*span.lo == 0 && *span.hi == 0) {
return;
}
debug!("update_source_pos: %s", bcx.sess().codemap.span_to_str(span));
let loc = span_start(bcx.ccx(), span);
set_debug_location(bcx.ccx(), lexical_block_metadata(bcx), loc.line, loc.col.to_uint())
}
/// Creates debug information for the given function.
///
/// Adds the created metadata nodes directly to the crate's IR.
/// The return value should be ignored if called from outside of the debuginfo module.
pub fn create_function_metadata(fcx: &FunctionContext) -> DISubprogram {
let cx = fcx.ccx;
let span = fcx.span.get();
let fnitem = cx.tcx.items.get_copy(&fcx.id);
let (ident, ret_ty, id) = match fnitem {
ast_map::node_item(item, _) => {
match item.node {
ast::item_fn(ast::fn_decl { output: ty, _}, _, _, _, _) => {
(item.ident, ty, item.id)
}
_ => fcx.ccx.sess.span_bug(item.span,
"create_function_metadata: item bound to non-function")
}
}
ast_map::node_method(
@ast::method {
decl: ast::fn_decl { output: ty, _ },
id: id,
ident: ident,
_
},
_,
_) => {
(ident, ty, id)
}
ast_map::node_expr(ref expr) => {
match expr.node {
ast::expr_fn_block(ref decl, _) => {
let name = gensym_name("fn");
(name, decl.output, expr.id)
}
_ => fcx.ccx.sess.span_bug(expr.span,
"create_function_metadata: expected an expr_fn_block here")
}
}
ast_map::node_trait_method(
@ast::provided(
@ast::method {
decl: ast::fn_decl { output: ty, _ },
id: id,
ident: ident,
_
}),
_,
_) => {
(ident, ty, id)
}
_ => fcx.ccx.sess.bug("create_function_metadata: unexpected sort of node")
};
match dbg_cx(cx).created_functions.find(&id) {
Some(fn_metadata) => return *fn_metadata,
None => ()
}
debug!("create_function_metadata: %s, %s",
cx.sess.str_of(ident),
cx.sess.codemap.span_to_str(span));
let loc = span_start(cx, span);
let file_metadata = file_metadata(cx, loc.file.name);
let return_type_metadata = if cx.sess.opts.extra_debuginfo {
match ret_ty.node {
ast::ty_nil => ptr::null(),
_ => type_metadata(cx, ty::node_id_to_type(cx.tcx, id), ret_ty.span)
}
} else {
ptr::null()
};
let fn_ty = unsafe {
llvm::LLVMDIBuilderCreateSubroutineType(
DIB(cx),
file_metadata,
create_DIArray(DIB(cx), [return_type_metadata]))
};
let fn_metadata =
do cx.sess.str_of(ident).as_c_str |name| {
do cx.sess.str_of(ident).as_c_str |linkage| {
unsafe {
llvm::LLVMDIBuilderCreateFunction(
DIB(cx),
file_metadata,
name,
linkage,
file_metadata,
loc.line as c_uint,
fn_ty,
false,
true,
loc.line as c_uint,
FlagPrototyped as c_uint,
cx.sess.opts.optimize != session::No,
fcx.llfn,
ptr::null(),
ptr::null())
}
}};
dbg_cx(cx).created_functions.insert(id, fn_metadata);
return fn_metadata;
}
//=-------------------------------------------------------------------------------------------------
// Module-Internal debug info creation functions
//=-------------------------------------------------------------------------------------------------
fn create_DIArray(builder: DIBuilderRef, arr: &[DIDescriptor]) -> DIArray {
return unsafe {
llvm::LLVMDIBuilderGetOrCreateArray(builder, vec::raw::to_ptr(arr), arr.len() as u32)
};
}
fn compile_unit_metadata(cx: @mut CrateContext) {
let dcx = dbg_cx(cx);
let crate_name: &str = dcx.crate_file;
debug!("compile_unit_metadata: %?", crate_name);
let work_dir = cx.sess.working_dir.to_str();
let producer = fmt!("rustc version %s", env!("CFG_VERSION"));
do crate_name.as_c_str |crate_name| {
do work_dir.as_c_str |work_dir| {
do producer.as_c_str |producer| {
do "".as_c_str |flags| {
do "".as_c_str |split_name| {
unsafe {
llvm::LLVMDIBuilderCreateCompileUnit(dcx.builder,
DW_LANG_RUST as c_uint, crate_name, work_dir, producer,
cx.sess.opts.optimize != session::No,
flags, 0, split_name);
}
}}}}};
}
fn file_metadata(cx: &mut CrateContext, full_path: &str) -> DIFile {
match dbg_cx(cx).created_files.find_equiv(&full_path) {
Some(file_metadata) => return *file_metadata,
None => ()
}
debug!("file_metadata: %s", full_path);
let work_dir = cx.sess.working_dir.to_str();
let file_name =
if full_path.starts_with(work_dir) {
full_path.slice(work_dir.len() + 1u, full_path.len())
} else {
full_path
};
let file_metadata =
do file_name.as_c_str |file_name| {
do work_dir.as_c_str |work_dir| {
unsafe {
llvm::LLVMDIBuilderCreateFile(DIB(cx), file_name, work_dir)
}
}};
dbg_cx(cx).created_files.insert(full_path.to_owned(), file_metadata);
return file_metadata;
}
/// Get or create the lexical block metadata node for the given LLVM basic block.
fn lexical_block_metadata(bcx: @mut Block) -> DILexicalBlock {
let cx = bcx.ccx();
let mut bcx = bcx;
// Search up the tree of basic blocks until we find one that knows the containing lexical block.
while bcx.node_info.is_none() {
match bcx.parent {
Some(b) => bcx = b,
None => cx.sess.bug("debuginfo: Could not find lexical block for LLVM basic block.")
}
}
let span = bcx.node_info.get().span;
let id = bcx.node_info.get().id;
// Check whether we already have a cache entry for this node id
match dbg_cx(cx).created_blocks.find(&id) {
Some(block) => return *block,
None => ()
}
debug!("lexical_block_metadata: %s", bcx.sess().codemap.span_to_str(span));
let parent = match bcx.parent {
None => create_function_metadata(bcx.fcx),
Some(b) => lexical_block_metadata(b)
};
let loc = span_start(cx, span);
let file_metadata = file_metadata(cx, loc.file.name);
let lexical_block_metadata = unsafe {
llvm::LLVMDIBuilderCreateLexicalBlock(
DIB(cx),
parent,
file_metadata,
loc.line as c_uint,
loc.col.to_uint() as c_uint)
};
dbg_cx(cx).created_blocks.insert(id, lexical_block_metadata);
return lexical_block_metadata;
}
fn basic_type_metadata(cx: &mut CrateContext, t: ty::t) -> DIType {
debug!("basic_type_metadata: %?", ty::get(t));
let (name, encoding) = match ty::get(t).sty {
ty::ty_nil | ty::ty_bot => (~"uint", DW_ATE_unsigned),
ty::ty_bool => (~"bool", DW_ATE_boolean),
ty::ty_int(int_ty) => match int_ty {
ast::ty_i => (~"int", DW_ATE_signed),
ast::ty_char => (~"char", DW_ATE_signed_char),
ast::ty_i8 => (~"i8", DW_ATE_signed),
ast::ty_i16 => (~"i16", DW_ATE_signed),
ast::ty_i32 => (~"i32", DW_ATE_signed),
ast::ty_i64 => (~"i64", DW_ATE_signed)
},
ty::ty_uint(uint_ty) => match uint_ty {
ast::ty_u => (~"uint", DW_ATE_unsigned),
ast::ty_u8 => (~"u8", DW_ATE_unsigned),
ast::ty_u16 => (~"u16", DW_ATE_unsigned),
ast::ty_u32 => (~"u32", DW_ATE_unsigned),
ast::ty_u64 => (~"u64", DW_ATE_unsigned)
},
ty::ty_float(float_ty) => match float_ty {
ast::ty_f => (~"float", DW_ATE_float),
ast::ty_f32 => (~"f32", DW_ATE_float),
ast::ty_f64 => (~"f64", DW_ATE_float)
},
_ => cx.sess.bug("debuginfo::basic_type_metadata - t is invalid type")
};
let llvm_type = type_of::type_of(cx, t);
let (size, align) = size_and_align_of(cx, llvm_type);
let ty_metadata = do name.as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreateBasicType(
DIB(cx),
name,
bytes_to_bits(size),
bytes_to_bits(align),
encoding as c_uint)
}
};
return ty_metadata;
}
fn pointer_type_metadata(cx: &mut CrateContext,
pointer_type: ty::t,
pointee_type_metadata: DIType)
-> DIType {
let pointer_llvm_type = type_of::type_of(cx, pointer_type);
let (pointer_size, pointer_align) = size_and_align_of(cx, pointer_llvm_type);
let name = ty_to_str(cx.tcx, pointer_type);
let ptr_metadata = do name.as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreatePointerType(
DIB(cx),
pointee_type_metadata,
bytes_to_bits(pointer_size),
bytes_to_bits(pointer_align),
name)
}
};
return ptr_metadata;
}
fn struct_metadata(cx: &mut CrateContext,
struct_type: ty::t,
fields: ~[ty::field],
span: span)
-> DICompositeType {
let struct_name = ty_to_str(cx.tcx, struct_type);
debug!("struct_metadata: %s", struct_name);
let struct_llvm_type = type_of::type_of(cx, struct_type);
let field_llvm_types = do fields.map |field| { type_of::type_of(cx, field.mt.ty) };
let field_names = do fields.map |field| { cx.sess.str_of(field.ident).to_owned() };
let field_types_metadata = do fields.map |field| {
type_metadata(cx, field.mt.ty, span)
};
return composite_type_metadata(
cx,
struct_llvm_type,
struct_name,
field_llvm_types,
field_names,
field_types_metadata,
span);
}
fn tuple_metadata(cx: &mut CrateContext,
tuple_type: ty::t,
component_types: &[ty::t],
span: span)
-> DICompositeType {
let tuple_name = ty_to_str(cx.tcx, tuple_type);
let tuple_llvm_type = type_of::type_of(cx, tuple_type);
let component_names = do component_types.map |_| { ~"" };
let component_llvm_types = do component_types.map |it| { type_of::type_of(cx, *it) };
let component_types_metadata = do component_types.map |it| {
type_metadata(cx, *it, span)
};
return composite_type_metadata(
cx,
tuple_llvm_type,
tuple_name,
component_llvm_types,
component_names,
component_types_metadata,
span);
}
fn enum_metadata(cx: &mut CrateContext,
enum_type: ty::t,
enum_def_id: ast::def_id,
// _substs is only needed in the other version. Will go away with new snapshot.
_substs: &ty::substs,
span: span)
-> DIType {
let enum_name = ty_to_str(cx.tcx, enum_type);
// For empty enums there is an early exit. Just describe it as an empty struct with the
// appropriate type name
if ty::type_is_empty(cx.tcx, enum_type) {
return composite_type_metadata(cx, Type::nil(), enum_name, [], [], [], span);
}
// Prepare some data (llvm type, size, align, ...) about the discriminant. This data will be
// needed in all of the following cases.
let discriminant_llvm_type = Type::enum_discrim(cx);
let (discriminant_size, discriminant_align) = size_and_align_of(cx, discriminant_llvm_type);
assert!(Type::enum_discrim(cx) == cx.int_type);
let discriminant_type_metadata = type_metadata(cx, ty::mk_int(), span);
let variants: &[@ty::VariantInfo] = *ty::enum_variants(cx.tcx, enum_def_id);
let enumerators_metadata: ~[DIDescriptor] = variants
.iter()
.transform(|v| {
let name: &str = cx.sess.str_of(v.name);
let discriminant_value = v.disr_val as c_ulonglong;
do name.as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreateEnumerator(
DIB(cx),
name,
discriminant_value)
}
}
})
.collect();
let loc = span_start(cx, span);
let file_metadata = file_metadata(cx, loc.file.name);
let discriminant_type_metadata = do enum_name.as_c_str |enum_name| {
unsafe {
llvm::LLVMDIBuilderCreateEnumerationType(
DIB(cx),
file_metadata,
enum_name,
file_metadata,
loc.line as c_uint,
bytes_to_bits(discriminant_size),
bytes_to_bits(discriminant_align),
create_DIArray(DIB(cx), enumerators_metadata),
discriminant_type_metadata)
}
};
let type_rep = adt::represent_type(cx, enum_type);
match *type_rep {
adt::CEnum(*) => {
return discriminant_type_metadata;
}
adt::Univariant(ref struct_def, _) => {
assert!(variants.len() == 1);
return adt_struct_metadata(cx, struct_def, variants[0], None, span);
}
adt::General(ref struct_defs) => {
let variants_member_metadata: ~[DIDescriptor] = do struct_defs
.iter()
.enumerate()
.transform |(i, struct_def)| {
let variant_type_metadata = adt_struct_metadata(
cx,
struct_def,
variants[i],
Some(discriminant_type_metadata),
span);
do "".as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreateMemberType(
DIB(cx),
file_metadata,
name,
file_metadata,
loc.line as c_uint,
bytes_to_bits(struct_def.size as uint),
bytes_to_bits(struct_def.align as uint),
bytes_to_bits(0),
0,
variant_type_metadata)
}
}
}.collect();
let enum_llvm_type = type_of::type_of(cx, enum_type);
let (enum_type_size, enum_type_align) = size_and_align_of(cx, enum_llvm_type);
return do enum_name.as_c_str |enum_name| {
unsafe {
llvm::LLVMDIBuilderCreateUnionType(
DIB(cx),
file_metadata,
enum_name,
file_metadata,
loc.line as c_uint,
bytes_to_bits(enum_type_size),
bytes_to_bits(enum_type_align),
0, // Flags
create_DIArray(DIB(cx), variants_member_metadata),
0) // RuntimeLang
}};
}
adt::NullablePointer { nonnull: ref struct_def, nndiscr, _ } => {
return adt_struct_metadata(cx, struct_def, variants[nndiscr], None, span);
}
}
fn adt_struct_metadata(cx: &mut CrateContext,
struct_def: &adt::Struct,
variant_info: &ty::VariantInfo,
discriminant_type_metadata: Option<DIType>,
span: span)
-> DICompositeType
{
let arg_llvm_types: ~[Type] = do struct_def.fields.map |&ty| { type_of::type_of(cx, ty) };
let arg_metadata: ~[DIType] = do struct_def.fields.iter().enumerate()
.transform |(i, &ty)| {
match discriminant_type_metadata {
Some(metadata) if i == 0 => metadata,
_ => type_metadata(cx, ty, span)
}
}.collect();
let mut arg_names = match variant_info.arg_names {
Some(ref names) => do names.map |ident| { cx.sess.str_of(*ident).to_owned() },
None => do variant_info.args.map |_| { ~"" }
};
if discriminant_type_metadata.is_some() {
arg_names.insert(0, ~"");
}
let variant_llvm_type = Type::struct_(arg_llvm_types, struct_def.packed);
let variant_name: &str = cx.sess.str_of(variant_info.name);
return composite_type_metadata(
cx,
variant_llvm_type,
variant_name,
arg_llvm_types,
arg_names,
arg_metadata,
span);
}
}
/// Creates debug information for a composite type, that is, anything that results in a LLVM struct.
///
/// Examples of Rust types to use this are: structs, tuples, boxes, vecs, and enums.
fn composite_type_metadata(cx: &mut CrateContext,
composite_llvm_type: Type,
composite_type_name: &str,
member_llvm_types: &[Type],
member_names: &[~str],
member_type_metadata: &[DIType],
span: span)
-> DICompositeType {
let loc = span_start(cx, span);
let file_metadata = file_metadata(cx, loc.file.name);
let (composite_size, composite_align) = size_and_align_of(cx, composite_llvm_type);
let member_metadata: ~[DIDescriptor] = member_llvm_types
.iter()
.enumerate()
.transform(|(i, &member_llvm_type)| {
let (member_size, member_align) = size_and_align_of(cx, member_llvm_type);
let member_offset = machine::llelement_offset(cx, composite_llvm_type, i);
let member_name: &str = member_names[i];
do member_name.as_c_str |member_name| {
unsafe {
llvm::LLVMDIBuilderCreateMemberType(
DIB(cx),
file_metadata,
member_name,
file_metadata,
loc.line as c_uint,
bytes_to_bits(member_size),
bytes_to_bits(member_align),
bytes_to_bits(member_offset),
0,
member_type_metadata[i])
}
}
})
.collect();
return do composite_type_name.as_c_str |name| {
unsafe {
llvm::LLVMDIBuilderCreateStructType(
DIB(cx),
file_metadata,
name,
file_metadata,
loc.line as c_uint,
bytes_to_bits(composite_size),
bytes_to_bits(composite_align),
0,
ptr::null(),
create_DIArray(DIB(cx), member_metadata),
0,
ptr::null())
}
};
}
fn boxed_type_metadata(cx: &mut CrateContext,
content_type_name: Option<&str>,
content_llvm_type: Type,
content_type_metadata: DIType,
span: span)
-> DICompositeType {
let box_type_name = match content_type_name {
Some(content_type_name) => fmt!("Boxed<%s>", content_type_name),
None => ~"BoxedType"
};
let box_llvm_type = Type::box(cx, &content_llvm_type);
let member_llvm_types = box_llvm_type.field_types();
let member_names = [~"refcnt", ~"tydesc", ~"prev", ~"next", ~"val"];
assert!(box_layout_is_correct(cx, member_llvm_types, content_llvm_type));
let int_type = ty::mk_int();
let nil_pointer_type = ty::mk_nil_ptr(cx.tcx);
let member_types_metadata = [
type_metadata(cx, int_type, span),
type_metadata(cx, nil_pointer_type, span),
type_metadata(cx, nil_pointer_type, span),
type_metadata(cx, nil_pointer_type, span),
content_type_metadata
];
return composite_type_metadata(
cx,
box_llvm_type,
box_type_name,
member_llvm_types,
member_names,
member_types_metadata,
span);
// Unfortunately, we cannot assert anything but the correct types here---and not whether the
// 'next' and 'prev' pointers are in the correct order.
fn box_layout_is_correct(cx: &CrateContext,
member_llvm_types: &[Type],
content_llvm_type: Type)
-> bool {
member_llvm_types.len() == 5 &&
member_llvm_types[0] == cx.int_type &&
member_llvm_types[1] == cx.tydesc_type.ptr_to() &&
member_llvm_types[2] == Type::i8().ptr_to() &&
member_llvm_types[3] == Type::i8().ptr_to() &&
member_llvm_types[4] == content_llvm_type
}
}
fn fixed_vec_metadata(cx: &mut CrateContext,
element_type: ty::t,
len: uint,
span: span)
-> DIType {
let element_type_metadata = type_metadata(cx, element_type, span);
let element_llvm_type = type_of::type_of(cx, element_type);
let (element_type_size, element_type_align) = size_and_align_of(cx, element_llvm_type);
let subrange = unsafe {
llvm::LLVMDIBuilderGetOrCreateSubrange(
DIB(cx),
0,
len as c_longlong)
};
let subscripts = create_DIArray(DIB(cx), [subrange]);
return unsafe {
llvm::LLVMDIBuilderCreateArrayType(
DIB(cx),
bytes_to_bits(element_type_size * len),
bytes_to_bits(element_type_align),
element_type_metadata,
subscripts)
};
}
fn vec_metadata(cx: &mut CrateContext,
element_type: ty::t,
span: span)
-> DICompositeType {
let element_type_metadata = type_metadata(cx, element_type, span);
let element_llvm_type = type_of::type_of(cx, element_type);
let (element_size, element_align) = size_and_align_of(cx, element_llvm_type);
let vec_llvm_type = Type::vec(cx.sess.targ_cfg.arch, &element_llvm_type);
let vec_type_name: &str = fmt!("[%s]", ty_to_str(cx.tcx, element_type));
let member_llvm_types = vec_llvm_type.field_types();
let member_names = &[~"fill", ~"alloc", ~"elements"];
let int_type_metadata = type_metadata(cx, ty::mk_int(), span);
let array_type_metadata = unsafe {
llvm::LLVMDIBuilderCreateArrayType(
DIB(cx),
bytes_to_bits(element_size),
bytes_to_bits(element_align),
element_type_metadata,
create_DIArray(DIB(cx), [llvm::LLVMDIBuilderGetOrCreateSubrange(DIB(cx), 0, 0)]))
};
// fill alloc elements
let member_type_metadata = &[int_type_metadata, int_type_metadata, array_type_metadata];
return composite_type_metadata(
cx,
vec_llvm_type,
vec_type_name,
member_llvm_types,
member_names,
member_type_metadata,
span);
}
fn boxed_vec_metadata(cx: &mut CrateContext,
element_type: ty::t,
span: span)
-> DICompositeType {
let element_llvm_type = type_of::type_of(cx, element_type);
let vec_llvm_type = Type::vec(cx.sess.targ_cfg.arch, &element_llvm_type);
let vec_type_name: &str = fmt!("[%s]", ty_to_str(cx.tcx, element_type));
let vec_metadata = vec_metadata(cx, element_type, span);
return boxed_type_metadata(
cx,
Some(vec_type_name),
vec_llvm_type,
vec_metadata,
span);
}
fn vec_slice_metadata(cx: &mut CrateContext,
vec_type: ty::t,
element_type: ty::t,
span: span)
-> DICompositeType {
debug!("vec_slice_metadata: %?", ty::get(vec_type));
let slice_llvm_type = type_of::type_of(cx, vec_type);
let slice_type_name = ty_to_str(cx.tcx, vec_type);
let member_llvm_types = slice_llvm_type.field_types();
let member_names = &[~"data_ptr", ~"size_in_bytes"];
assert!(slice_layout_is_correct(cx, member_llvm_types, element_type));
let data_ptr_type = ty::mk_ptr(cx.tcx, ty::mt { ty: element_type, mutbl: ast::m_imm });
let member_type_metadata = &[type_metadata(cx, data_ptr_type, span),
type_metadata(cx, ty::mk_uint(), span)];
return composite_type_metadata(
cx,
slice_llvm_type,
slice_type_name,
member_llvm_types,
member_names,
member_type_metadata,
span);
fn slice_layout_is_correct(cx: &mut CrateContext,
member_llvm_types: &[Type],
element_type: ty::t)