about summary refs log tree commit diff
path: root/src/comp/middle
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2011-04-20 11:59:10 -0700
committerPatrick Walton <pcwalton@mimiga.net>2011-04-20 11:59:10 -0700
commitcac7524c1aa63961973e3607d72a49dfccb09448 (patch)
tree1412cd27f95bafd462485fd11e37eba11778f59a /src/comp/middle
parent38e842244c33ced2bc248a0b0911b507336829c9 (diff)
downloadrust-cac7524c1aa63961973e3607d72a49dfccb09448.tar.gz
rust-cac7524c1aa63961973e3607d72a49dfccb09448.zip
rustc: Remove all manual type construction outside ty.rs
Diffstat (limited to 'src/comp/middle')
-rw-r--r--src/comp/middle/trans.rs8
-rw-r--r--src/comp/middle/ty.rs38
-rw-r--r--src/comp/middle/typeck.rs55
3 files changed, 56 insertions, 45 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index fbdbf0b2a61..439e563ebac 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -2843,12 +2843,12 @@ fn trans_lit(@crate_ctxt cx, &ast.lit lit, &ast.ann ann) -> ValueRef {
 fn target_type(@crate_ctxt cx, @ty.t t) -> @ty.t {
     alt (t.struct) {
         case (ty.ty_int) {
-            auto tm = ty.ty_machine(cx.sess.get_targ_cfg().int_type);
-            ret @rec(struct=tm with *t);
+            auto struct_ty = ty.mk_mach(cx.sess.get_targ_cfg().int_type);
+            ret ty.copy_cname(struct_ty, t);
         }
         case (ty.ty_uint) {
-            auto tm = ty.ty_machine(cx.sess.get_targ_cfg().uint_type);
-            ret @rec(struct=tm with *t);
+            auto struct_ty = ty.mk_mach(cx.sess.get_targ_cfg().uint_type);
+            ret ty.copy_cname(struct_ty, t);
         }
         case (_) { /* fall through */ }
     }
diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs
index 8ad579ae61e..df85ed27331 100644
--- a/src/comp/middle/ty.rs
+++ b/src/comp/middle/ty.rs
@@ -386,10 +386,6 @@ fn walk_ty(ty_walk walker, @t ty) {
 type ty_fold = fn(@t) -> @t;
 
 fn fold_ty(ty_fold fld, @t ty_0) -> @t {
-    fn rewrap(@t orig, &sty new) -> @t {
-        ret @rec(struct=new, cname=orig.cname);
-    }
-
     auto ty = ty_0;
     alt (ty.struct) {
         case (ty_nil)           { /* no-op */ }
@@ -403,23 +399,25 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
         case (ty_type)          { /* no-op */ }
         case (ty_native)        { /* no-op */ }
         case (ty_box(?tm)) {
-            ty = rewrap(ty, ty_box(rec(ty=fold_ty(fld, tm.ty), mut=tm.mut)));
+            ty = copy_cname(mk_box(rec(ty=fold_ty(fld, tm.ty), mut=tm.mut)),
+                            ty);
         }
         case (ty_vec(?tm)) {
-            ty = rewrap(ty, ty_vec(rec(ty=fold_ty(fld, tm.ty), mut=tm.mut)));
+            ty = copy_cname(mk_vec(rec(ty=fold_ty(fld, tm.ty), mut=tm.mut)),
+                            ty);
         }
         case (ty_port(?subty)) {
-            ty = rewrap(ty, ty_port(fold_ty(fld, subty)));
+            ty = copy_cname(mk_port(fold_ty(fld, subty)), ty);
         }
         case (ty_chan(?subty)) {
-            ty = rewrap(ty, ty_chan(fold_ty(fld, subty)));
+            ty = copy_cname(mk_chan(fold_ty(fld, subty)), ty);
         }
         case (ty_tag(?tid, ?subtys)) {
             let vec[@t] new_subtys = vec();
             for (@t subty in subtys) {
                 new_subtys += vec(fold_ty(fld, subty));
             }
-            ty = rewrap(ty, ty_tag(tid, new_subtys));
+            ty = copy_cname(mk_tag(tid, new_subtys), ty);
         }
         case (ty_tup(?mts)) {
             let vec[mt] new_mts = vec();
@@ -427,7 +425,7 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
                 auto new_subty = fold_ty(fld, tm.ty);
                 new_mts += vec(rec(ty=new_subty, mut=tm.mut));
             }
-            ty = rewrap(ty, ty_tup(new_mts));
+            ty = copy_cname(mk_tup(new_mts), ty);
         }
         case (ty_rec(?fields)) {
             let vec[field] new_fields = vec();
@@ -436,7 +434,7 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
                 auto new_mt = rec(ty=new_ty, mut=fl.mt.mut);
                 new_fields += vec(rec(ident=fl.ident, mt=new_mt));
             }
-            ty = rewrap(ty, ty_rec(new_fields));
+            ty = copy_cname(mk_rec(new_fields), ty);
         }
         case (ty_fn(?proto, ?args, ?ret_ty)) {
             let vec[arg] new_args = vec();
@@ -444,7 +442,7 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
                 auto new_ty = fold_ty(fld, a.ty);
                 new_args += vec(rec(mode=a.mode, ty=new_ty));
             }
-            ty = rewrap(ty, ty_fn(proto, new_args, fold_ty(fld, ret_ty)));
+            ty = copy_cname(mk_fn(proto, new_args, fold_ty(fld, ret_ty)), ty);
         }
         case (ty_native_fn(?abi, ?args, ?ret_ty)) {
             let vec[arg] new_args = vec();
@@ -452,8 +450,8 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
                 auto new_ty = fold_ty(fld, a.ty);
                 new_args += vec(rec(mode=a.mode, ty=new_ty));
             }
-            ty = rewrap(ty, ty_native_fn(abi, new_args,
-                                         fold_ty(fld, ret_ty)));
+            ty = copy_cname(mk_native_fn(abi, new_args, fold_ty(fld, ret_ty)),
+                            ty);
         }
         case (ty_obj(?methods)) {
             let vec[method] new_methods = vec();
@@ -466,7 +464,7 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
                                        inputs=new_args,
                                        output=fold_ty(fld, m.output)));
             }
-            ty = rewrap(ty, ty_obj(new_methods));
+            ty = copy_cname(mk_obj(new_methods), ty);
         }
         case (ty_var(_))         { /* no-op */ }
         case (ty_local(_))       { /* no-op */ }
@@ -479,6 +477,16 @@ fn fold_ty(ty_fold fld, @t ty_0) -> @t {
 
 // Type utilities
 
+fn rename(@t typ, str new_cname) -> @t {
+    ret @rec(struct=typ.struct, cname=some[str](new_cname));
+}
+
+// Returns a type with the structural part taken from `struct_ty` and the
+// canonical name from `cname_ty`.
+fn copy_cname(@t struct_ty, @t cname_ty) -> @t {
+    ret @rec(struct=struct_ty.struct, cname=cname_ty.cname);
+}
+
 // FIXME: remove me when == works on these tags.
 fn mode_is_alias(ast.mode m) -> bool {
     alt (m) {
diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs
index 0759c80910a..a7213b68cc5 100644
--- a/src/comp/middle/typeck.rs
+++ b/src/comp/middle/typeck.rs
@@ -226,26 +226,26 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
     }
 
     auto mut = ast.imm;
-    auto sty;
+    auto typ;
     auto cname = none[str];
     alt (ast_ty.node) {
-        case (ast.ty_nil)          { sty = ty.ty_nil; }
-        case (ast.ty_bool)         { sty = ty.ty_bool; }
-        case (ast.ty_int)          { sty = ty.ty_int; }
-        case (ast.ty_uint)         { sty = ty.ty_uint; }
-        case (ast.ty_float)        { sty = ty.ty_float; }
-        case (ast.ty_machine(?tm)) { sty = ty.ty_machine(tm); }
-        case (ast.ty_char)         { sty = ty.ty_char; }
-        case (ast.ty_str)          { sty = ty.ty_str; }
-        case (ast.ty_box(?mt)) { sty = ty.ty_box(ast_mt_to_mt(getter, mt)); }
-        case (ast.ty_vec(?mt)) { sty = ty.ty_vec(ast_mt_to_mt(getter, mt)); }
+        case (ast.ty_nil)          { typ = ty.mk_nil(); }
+        case (ast.ty_bool)         { typ = ty.mk_bool(); }
+        case (ast.ty_int)          { typ = ty.mk_int(); }
+        case (ast.ty_uint)         { typ = ty.mk_uint(); }
+        case (ast.ty_float)        { typ = ty.mk_float(); }
+        case (ast.ty_machine(?tm)) { typ = ty.mk_mach(tm); }
+        case (ast.ty_char)         { typ = ty.mk_char(); }
+        case (ast.ty_str)          { typ = ty.mk_str(); }
+        case (ast.ty_box(?mt)) { typ = ty.mk_box(ast_mt_to_mt(getter, mt)); }
+        case (ast.ty_vec(?mt)) { typ = ty.mk_vec(ast_mt_to_mt(getter, mt)); }
 
         case (ast.ty_port(?t)) {
-            sty = ty.ty_port(ast_ty_to_ty(getter, t));
+            typ = ty.mk_port(ast_ty_to_ty(getter, t));
         }
 
         case (ast.ty_chan(?t)) {
-            sty = ty.ty_chan(ast_ty_to_ty(getter, t));
+            typ = ty.mk_chan(ast_ty_to_ty(getter, t));
         }
 
         case (ast.ty_tup(?fields)) {
@@ -253,7 +253,7 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
             for (ast.mt field in fields) {
                 _vec.push[ty.mt](flds, ast_mt_to_mt(getter, field));
             }
-            sty = ty.ty_tup(flds);
+            typ = ty.mk_tup(flds);
         }
         case (ast.ty_rec(?fields)) {
             let vec[field] flds = vec();
@@ -261,28 +261,28 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
                 _vec.push[field](flds, rec(ident=f.ident,
                                         mt=ast_mt_to_mt(getter, f.mt)));
             }
-            sty = ty.ty_rec(flds);
+            typ = ty.mk_rec(flds);
         }
 
         case (ast.ty_fn(?proto, ?inputs, ?output)) {
             auto f = bind ast_arg_to_arg(getter, _);
             auto i = _vec.map[ast.ty_arg, arg](f, inputs);
-            sty = ty.ty_fn(proto, i, ast_ty_to_ty(getter, output));
+            typ = ty.mk_fn(proto, i, ast_ty_to_ty(getter, output));
         }
 
         case (ast.ty_path(?path, ?def)) {
             check (def != none[ast.def]);
             alt (option.get[ast.def](def)) {
                 case (ast.def_ty(?id)) {
-                    sty = instantiate(getter, id, path.node.types).struct;
+                    typ = instantiate(getter, id, path.node.types);
                 }
                 case (ast.def_native_ty(?id)) {
-                    sty = getter(id)._1.struct;
+                    typ = getter(id)._1;
                 }
                 case (ast.def_obj(?id))     {
-                    sty = instantiate(getter, id, path.node.types).struct;
+                    typ = instantiate(getter, id, path.node.types);
                 }
-                case (ast.def_ty_arg(?id))  { sty = ty.ty_param(id); }
+                case (ast.def_ty_arg(?id))  { typ = ty.mk_param(id); }
                 case (_)                    { fail; }
             }
 
@@ -302,11 +302,14 @@ fn ast_ty_to_ty(ty_getter getter, &@ast.ty ast_ty) -> @ty.t {
                                       output=out));
             }
 
-            sty = ty.ty_obj(ty.sort_methods(tmeths));
+            typ = ty.mk_obj(ty.sort_methods(tmeths));
         }
     }
 
-    auto typ = @rec(struct=sty, cname=cname);
+    alt (cname) {
+        case (none[str]) { /* no-op */ }
+        case (some[str](?cname_str)) { typ = ty.rename(typ, cname_str); }
+    }
     ret typ;
 }
 
@@ -427,8 +430,8 @@ fn collect_item_types(session.session sess, @ast.crate crate)
         auto methods =
             _vec.map[@ast.method,method](f, obj_info.methods);
 
-        auto t_obj = @rec(struct=ty.ty_obj(ty.sort_methods(methods)),
-                          cname=some[str](id));
+        auto t_obj = ty.mk_obj(ty.sort_methods(methods));
+        t_obj = ty.rename(t_obj, id);
         auto ty_param_count = _vec.len[ast.ty_param](ty_params);
         ret tup(ty_param_count, t_obj);
     }
@@ -547,7 +550,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
                     ret type_cache.get(def_id);
                 }
 
-                auto t = @rec(struct=ty.ty_native, cname=none[str]);
+                auto t = ty.mk_native();
                 auto tpt = tup(0u, t);
                 type_cache.insert(def_id, tpt);
                 ret tpt;
@@ -769,7 +772,7 @@ fn collect_item_types(session.session sess, @ast.crate crate)
         alt (ob.dtor) {
             case (some[@ast.method](?d)) {
                 let vec[arg] inputs = vec();
-                let @ty.t output = @rec(struct=ty.ty_nil, cname=none[str]);
+                let @ty.t output = ty.mk_nil();
                 auto dtor_tfn = ty.mk_fn(ast.proto_fn, inputs, output);
                 auto d_ = rec(ann=triv_ann(dtor_tfn) with d.node);
                 dtor = some[@ast.method](@rec(node=d_ with *d));