about summary refs log tree commit diff
path: root/src/librustsyntax/parse/parser.rs
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2012-05-14 14:13:32 -0700
committerTim Chevalier <chevalier@alum.wellesley.edu>2012-05-14 14:26:10 -0700
commit5428a22b95ccd8c84a402e2f5cb4aa53ddc6f4d4 (patch)
tree5d5b8f41d0011249fd613c23d6033d05f36101c3 /src/librustsyntax/parse/parser.rs
parent89cd2f6bd01e66f41b8dc49dd09930aa86cd2bd9 (diff)
downloadrust-5428a22b95ccd8c84a402e2f5cb4aa53ddc6f4d4.tar.gz
rust-5428a22b95ccd8c84a402e2f5cb4aa53ddc6f4d4.zip
First cut at dtors for classes
Classes with dtors should compile now. Haven't yet tested
whether they actually run correctly.

Beginnings of support for #2295, though that won't be done until
there's more test cases and resources are removed.
Diffstat (limited to 'src/librustsyntax/parse/parser.rs')
-rw-r--r--src/librustsyntax/parse/parser.rs61
1 files changed, 44 insertions, 17 deletions
diff --git a/src/librustsyntax/parse/parser.rs b/src/librustsyntax/parse/parser.rs
index 125dbb927f4..7c1e3a85a29 100644
--- a/src/librustsyntax/parse/parser.rs
+++ b/src/librustsyntax/parse/parser.rs
@@ -1940,14 +1940,24 @@ fn parse_item_class(p: parser) -> item_info {
     let mut ms: [@class_member] = [];
     let ctor_id = p.get_id();
     let mut the_ctor : option<(fn_decl, blk, codemap::span)> = none;
+    let mut the_dtor : option<(blk, codemap::span)> = none;
     while p.token != token::RBRACE {
         alt parse_class_item(p, class_path) {
-            ctor_decl(a_fn_decl, blk, s) {
-               the_ctor = some((a_fn_decl, blk, s));
-            }
-            members(mms) { ms += mms; }
+          ctor_decl(a_fn_decl, blk, s) {
+            the_ctor = some((a_fn_decl, blk, s));
+          }
+          dtor_decl(blk, s) {
+            the_dtor = some((blk, s));
+          }
+          members(mms) { ms += mms; }
        }
     }
+    let actual_dtor = option::map(the_dtor) {|dtor|
+       let (d_body, d_s) = dtor;
+       {node: {id: p.get_id(),
+               self_id: p.get_id(),
+               body: d_body},
+               span: d_s}};
     p.bump();
     alt the_ctor {
       some((ct_d, ct_b, ct_s)) {
@@ -1957,7 +1967,7 @@ fn parse_item_class(p: parser) -> item_info {
                     self_id: p.get_id(),
                     dec: ct_d,
                     body: ct_b},
-             span: ct_s}, rp),
+                     span: ct_s}, actual_dtor, rp),
         none)
       }
        /*
@@ -1982,24 +1992,41 @@ fn parse_single_class_item(p: parser, vis: visibility)
    }
 }
 
-// lets us identify the constructor declaration at
-// parse time
+/*
+  So that we can distinguish a class ctor or dtor
+  from other class members
+ */
 enum class_contents { ctor_decl(fn_decl, blk, codemap::span),
+                      dtor_decl(blk, codemap::span),
                       members([@class_member]) }
 
+fn parse_ctor(p: parser, result_ty: ast::ty_) -> class_contents {
+  // Can ctors/dtors have attrs? FIXME
+  let lo = p.last_span.lo;
+  let (decl_, _) = parse_fn_decl(p, impure_fn, parse_arg);
+  let decl = {output: @{id: p.get_id(),
+                        node: result_ty, span: decl_.output.span}
+              with decl_};
+  let body = parse_block(p);
+  ctor_decl(decl, body, mk_sp(lo, p.last_span.hi))
+}
+
+fn parse_dtor(p: parser) -> class_contents {
+  // Can ctors/dtors have attrs? FIXME
+  let lo = p.last_span.lo;
+  let body = parse_block(p);
+  dtor_decl(body, mk_sp(lo, p.last_span.hi))
+}
+
 fn parse_class_item(p:parser, class_name_with_tps: @path)
     -> class_contents {
     if eat_keyword(p, "new") {
-        let lo = p.last_span.lo;
-        // Can ctors have attrs?
-            // result type is always the type of the class
-        let (decl_, _) = parse_fn_decl(p, impure_fn, parse_arg);
-        let decl = {output: @{id: p.get_id(),
-                      node: ty_path(class_name_with_tps, p.get_id()),
-                      span: decl_.output.span}
-                    with decl_};
-        let body = parse_block(p);
-        ret ctor_decl(decl, body, mk_sp(lo, p.last_span.hi));
+       // result type is always the type of the class
+       ret parse_ctor(p, ty_path(class_name_with_tps,
+                              p.get_id()));
+    }
+    else if eat_keyword(p, "drop") {
+      ret parse_dtor(p);
     }
     else if eat_keyword(p, "priv") {
             expect(p, token::LBRACE);