From 3b06df4e35bbde6975575cd137e6f5f65d475d86 Mon Sep 17 00:00:00 2001
From: Michael Woerister <michaelwoerister@gmail>
Date: Mon, 8 Jul 2013 17:27:07 +0200
Subject: [PATCH] debuginfo: Added support for struct-style enums.

---
 src/librustc/metadata/decoder.rs         | 15 +++--
 src/librustc/middle/trans/debuginfo.rs   |  8 ++-
 src/librustc/middle/trans/machine.rs     | 39 -------------
 src/librustc/middle/ty.rs                | 23 ++++++--
 src/librustc/middle/typeck/check/mod.rs  | 37 ++++++------
 src/test/debug-info/struct-style-enum.rs | 71 ++++++++++++++++++++++++
 src/test/debug-info/tuple-style-enum.rs  | 44 ++++++++++++---
 7 files changed, 161 insertions(+), 76 deletions(-)
 create mode 100644 src/test/debug-info/struct-style-enum.rs

diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs
index caa170605de..e748ae89a9d 100644
--- a/src/librustc/metadata/decoder.rs
+++ b/src/librustc/metadata/decoder.rs
@@ -753,11 +753,16 @@ pub fn get_enum_variants(intr: @ident_interner, cdata: cmd, id: ast::node_id,
           Some(val) => { disr_val = val; }
           _         => { /* empty */ }
         }
-        infos.push(@ty::VariantInfo_{args: arg_tys,
-                       ctor_ty: ctor_ty, name: name,
-                  // I'm not even sure if we encode visibility
-                  // for variants -- TEST -- tjc
-                  id: *did, disr_val: disr_val, vis: ast::inherited});
+        infos.push(@ty::VariantInfo_{
+            args: arg_tys,
+            arg_names: None,
+            ctor_ty: ctor_ty,
+            name: name,
+            // I'm not even sure if we encode visibility
+            // for variants -- TEST -- tjc
+            id: *did,
+            disr_val: disr_val,
+            vis: ast::inherited});
         disr_val += 1;
     }
     return infos;
diff --git a/src/librustc/middle/trans/debuginfo.rs b/src/librustc/middle/trans/debuginfo.rs
index 34ebdd81bc0..3c163e149d2 100644
--- a/src/librustc/middle/trans/debuginfo.rs
+++ b/src/librustc/middle/trans/debuginfo.rs
@@ -612,9 +612,13 @@ fn create_enum_md(cx: &mut CrateContext,
 
         let raw_types : &[ty::t] = vi.args;
         let arg_types = do raw_types.map |&raw_type| { ty::subst(cx.tcx, substs, raw_type) };
-        
+
         let mut arg_llvm_types = do arg_types.map |&ty| { type_of::type_of(cx, ty) };
-        let mut arg_names = arg_types.map(|_| ~"");
+        let mut arg_names = match vi.arg_names {
+            Some(ref names) => do names.map |ident| { cx.sess.str_of(*ident).to_owned() },
+            None => do arg_types.map |_| { ~"" }
+        };
+
         let mut arg_md = do arg_types.map |&ty| { get_or_create_type(cx, ty, span) };
 
         if !is_univariant {
diff --git a/src/librustc/middle/trans/machine.rs b/src/librustc/middle/trans/machine.rs
index c0b57a22065..0bf0a522a65 100644
--- a/src/librustc/middle/trans/machine.rs
+++ b/src/librustc/middle/trans/machine.rs
@@ -119,42 +119,3 @@ pub fn llelement_offset(cx: &CrateContext, struct_ty: Type, element: uint) -> ui
         return llvm::LLVMOffsetOfElement(cx.td.lltd, struct_ty.to_ref(), element as u32) as uint;
     }
 }
-
-// Computes the size of the data part of an enum.
-pub fn static_size_of_enum(cx: &mut CrateContext, t: ty::t) -> uint {
-    if cx.enum_sizes.contains_key(&t) {
-        return cx.enum_sizes.get_copy(&t);
-    }
-
-    debug!("static_size_of_enum %s", ty_to_str(cx.tcx, t));
-
-    match ty::get(t).sty {
-        ty::ty_enum(tid, ref substs) => {
-            // Compute max(variant sizes).
-            let mut max_size = 0;
-            let variants = ty::enum_variants(cx.tcx, tid);
-            for variants.iter().advance |variant| {
-                if variant.args.len() == 0 {
-                    loop;
-                }
-
-                let lltypes = variant.args.map(|&variant_arg| {
-                    let substituted = ty::subst(cx.tcx, substs, variant_arg);
-                    type_of::sizing_type_of(cx, substituted)
-                });
-
-                debug!("static_size_of_enum: variant %s type %s",
-                       cx.tcx.sess.str_of(variant.name),
-                       cx.tn.type_to_str(Type::struct_(lltypes, false)));
-
-                let this_size = llsize_of_real(cx, Type::struct_(lltypes, false));
-                if max_size < this_size {
-                    max_size = this_size;
-                }
-            }
-            cx.enum_sizes.insert(t, max_size);
-            return max_size;
-        }
-        _ => cx.sess.bug("static_size_of_enum called on non-enum")
-    }
-}
diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs
index 50f331f7e7d..9438d60a480 100644
--- a/src/librustc/middle/ty.rs
+++ b/src/librustc/middle/ty.rs
@@ -3694,6 +3694,7 @@ fn struct_ctor_id(cx: ctxt, struct_did: ast::def_id) -> Option<ast::def_id> {
 #[deriving(Clone)]
 pub struct VariantInfo_ {
     args: ~[t],
+    arg_names: Option<~[ast::ident]>,
     ctor_ty: t,
     name: ast::ident,
     id: ast::def_id,
@@ -3875,6 +3876,7 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[VariantInfo] {
                         }
                         @VariantInfo_{
                             args: arg_tys,
+                            arg_names: None,
                             ctor_ty: ctor_ty,
                             name: variant.node.name,
                             id: ast_util::local_def(variant.node.id),
@@ -3883,13 +3885,21 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[VariantInfo] {
                          }
                     },
                     ast::struct_variant_kind(struct_def) => {
-                        let arg_tys =
-                            // Is this check needed for structs too, or are they always guaranteed
-                            // to have a valid constructor function?
-                            if struct_def.fields.len() > 0 {
-                                ty_fn_args(ctor_ty).map(|a| *a)
+
+                        let fields : &[@struct_field] = struct_def.fields;
+
+                        let (arg_tys, arg_names) =
+                            if fields.len() > 0 {
+                                let arg_tys = ty_fn_args(ctor_ty).map(|a| *a);
+                                let arg_names = do fields.map |field| { match field.node.kind {
+                                    named_field(ident, _visibility) => ident,
+                                    unnamed_field => cx.sess.bug(
+                                        "enum_variants: all fields in struct must have a name")
+                                }};
+
+                                (arg_tys, Some(arg_names))
                             } else {
-                                ~[]
+                                (~[], None)
                             };
 
                         assert!(variant.node.disr_expr.is_none());
@@ -3897,6 +3907,7 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[VariantInfo] {
 
                         @VariantInfo_{
                             args: arg_tys,
+                            arg_names: arg_names,
                             ctor_ty: ctor_ty,
                             name: variant.node.name,
                             id: ast_util::local_def(variant.node.id),
diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs
index 4a9e0fddbe7..0e1f8e617ff 100644
--- a/src/librustc/middle/typeck/check/mod.rs
+++ b/src/librustc/middle/typeck/check/mod.rs
@@ -3177,29 +3177,34 @@ pub fn check_enum_variants(ccx: @mut CrateCtxt,
             let this_disr_val = *disr_val;
             *disr_val += 1;
 
-            let arg_tys = match v.node.kind {
+            let (arg_tys, arg_names) = match v.node.kind {
                 ast::tuple_variant_kind(ref args) if args.len() > 0u => {
-                    Some(ty::ty_fn_args(ctor_ty).map(|a| *a))
+                    (ty::ty_fn_args(ctor_ty).map(|a| *a), None)
                 }
                 ast::tuple_variant_kind(_) => {
-                    Some(~[])
+                    (~[], None)
                 }
-                ast::struct_variant_kind(_) => {
-                    Some(ty::lookup_struct_fields(
-                        ccx.tcx, local_def(v.node.id)).map(|cf|
-                            ty::node_id_to_type(ccx.tcx, cf.id.node)))
+                ast::struct_variant_kind(struct_def) => {
+                    let tys = ty::ty_fn_args(ctor_ty).map(|a| *a);
+                    let names = do struct_def.fields.map |field| { match field.node.kind {
+                        ast::named_field(ident, _visibility) => ident,
+                        ast::unnamed_field => ccx.tcx.sess.bug(
+                            "enum_variants: all fields in struct must have a name")
+                    }};
+
+                    (tys, Some(names))
                 }
             };
 
-            match arg_tys {
-                None => {}
-                Some(arg_tys) => {
-                    variants.push(
-                        @VariantInfo_{args: arg_tys, ctor_ty: ctor_ty,
-                          name: v.node.name, id: local_def(v.node.id),
-                          disr_val: this_disr_val, vis: v.node.vis});
-                }
-            }
+            variants.push(@VariantInfo_{
+                    args: arg_tys,
+                    arg_names: arg_names,
+                    ctor_ty: ctor_ty,
+                    name: v.node.name,
+                    id: local_def(v.node.id),
+                    disr_val: this_disr_val,
+                    vis: v.node.vis
+            });
         }
     }
 
diff --git a/src/test/debug-info/struct-style-enum.rs b/src/test/debug-info/struct-style-enum.rs
new file mode 100644
index 00000000000..2be15e0f80f
--- /dev/null
+++ b/src/test/debug-info/struct-style-enum.rs
@@ -0,0 +1,71 @@
+// Copyright 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.
+
+// compile-flags:-Z extra-debug-info
+// debugger:set print union on
+// debugger:break zzz
+// debugger:run
+// debugger:finish
+
+// debugger:print case1
+// check:$1 = {{Case1, a = 0, b = 31868, c = 31868, d = 31868, e = 31868}, {Case1, a = 0, b = 2088533116, c = 2088533116}, {Case1, a = 0, b = 8970181431921507452}}
+
+// debugger:print case2
+// check:$2 = {{Case2, a = 0, b = 4369, c = 4369, d = 4369, e = 4369}, {Case2, a = 0, b = 286331153, c = 286331153}, {Case2, a = 0, b = 1229782938247303441}}
+
+// debugger:print case3
+// check:$3 = {{Case3, a = 0, b = 22873, c = 22873, d = 22873, e = 22873}, {Case3, a = 0, b = 1499027801, c = 1499027801}, {Case3, a = 0, b = 6438275382588823897}}
+
+// debugger:print univariant
+// check:$4 = {{a = -1}}
+
+// The first element is to ensure proper alignment, irrespective of the machines word size. Since
+// the size of the discriminant value is machine dependent, this has be taken into account when
+// datatype layout should be predictable as in this case.
+enum Regular {
+    Case1 { a: u64, b: u16, c: u16, d: u16, e: u16},
+    Case2 { a: u64, b: u32, c: u32},
+    Case3 { a: u64, b: u64 }
+}
+
+enum Univariant {
+    TheOnlyCase { a: i64 }
+}
+
+fn main() {
+
+    // In order to avoid endianess trouble all of the following test values consist of a single
+    // repeated byte. This way each interpretation of the union should look the same, no matter if
+    // this is a big or little endian machine.
+
+    // 0b0111110001111100011111000111110001111100011111000111110001111100 = 8970181431921507452
+    // 0b01111100011111000111110001111100 = 2088533116
+    // 0b0111110001111100 = 31868
+    // 0b01111100 = 124
+    let case1 = Case1 { a: 0, b: 31868, c: 31868, d: 31868, e: 31868 };
+
+    // 0b0001000100010001000100010001000100010001000100010001000100010001 = 1229782938247303441
+    // 0b00010001000100010001000100010001 = 286331153
+    // 0b0001000100010001 = 4369
+    // 0b00010001 = 17
+    let case2 = Case2 { a: 0, b: 286331153, c: 286331153 };
+
+    // 0b0101100101011001010110010101100101011001010110010101100101011001 = 6438275382588823897
+    // 0b01011001010110010101100101011001 = 1499027801
+    // 0b0101100101011001 = 22873
+    // 0b01011001 = 89
+    let case3 = Case3 { a: 0, b: 6438275382588823897 };
+
+    let univariant = TheOnlyCase { a: -1 };
+
+    zzz();
+}
+
+fn zzz() {()}
\ No newline at end of file
diff --git a/src/test/debug-info/tuple-style-enum.rs b/src/test/debug-info/tuple-style-enum.rs
index 5c12649dea7..43b97a9bbfc 100644
--- a/src/test/debug-info/tuple-style-enum.rs
+++ b/src/test/debug-info/tuple-style-enum.rs
@@ -14,16 +14,25 @@
 // debugger:run
 // debugger:finish
 
-// d ebugger:print case2
-// c heck:$1 = {Case1, 0, 1}
+// debugger:print case1
+// check:$1 = {{Case1, 0, 31868, 31868, 31868, 31868}, {Case1, 0, 2088533116, 2088533116}, {Case1, 0, 8970181431921507452}}
+
+// debugger:print case2
+// check:$2 = {{Case2, 0, 4369, 4369, 4369, 4369}, {Case2, 0, 286331153, 286331153}, {Case2, 0, 1229782938247303441}}
+
+// debugger:print case3
+// check:$3 = {{Case3, 0, 22873, 22873, 22873, 22873}, {Case3, 0, 1499027801, 1499027801}, {Case3, 0, 6438275382588823897}}
 
 // debugger:print univariant
-// check:$1 = {{-1}}
-
+// check:$4 = {{-1}}
 
+// The first element is to ensure proper alignment, irrespective of the machines word size. Since
+// the size of the discriminant value is machine dependent, this has be taken into account when
+// datatype layout should be predictable as in this case.
 enum Regular {
-    Case1(i32, i64),
-    Case2(bool, i16, i32)
+    Case1(u64, u16, u16, u16, u16),
+    Case2(u64, u32, u32),
+    Case3(u64, u64)
 }
 
 enum Univariant {
@@ -32,8 +41,27 @@ enum Univariant {
 
 fn main() {
 
-    let case1 = Case1(110, 220);
-    let case2 = Case2(false, 2, 3);
+    // In order to avoid endianess trouble all of the following test values consist of a single
+    // repeated byte. This way each interpretation of the union should look the same, no matter if
+    // this is a big or little endian machine.
+
+    // 0b0111110001111100011111000111110001111100011111000111110001111100 = 8970181431921507452
+    // 0b01111100011111000111110001111100 = 2088533116
+    // 0b0111110001111100 = 31868
+    // 0b01111100 = 124
+    let case1 = Case1(0, 31868, 31868, 31868, 31868);
+
+    // 0b0001000100010001000100010001000100010001000100010001000100010001 = 1229782938247303441
+    // 0b00010001000100010001000100010001 = 286331153
+    // 0b0001000100010001 = 4369
+    // 0b00010001 = 17
+    let case2 = Case2(0, 286331153, 286331153);
+
+    // 0b0101100101011001010110010101100101011001010110010101100101011001 = 6438275382588823897
+    // 0b01011001010110010101100101011001 = 1499027801
+    // 0b0101100101011001 = 22873
+    // 0b01011001 = 89
+    let case3 = Case3(0, 6438275382588823897);
 
     let univariant = TheOnlyCase(-1);