about summary refs log tree commit diff
path: root/src/libsyntax/ext/deriving/to_str.rs
blob: f155cb3790faab26bba2724b64ae9fa3f1d431e6 (plain)
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
// 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.

use ast::{meta_item, item, expr};
use codemap::span;
use ext::base::ext_ctxt;
use ext::build;
use ext::deriving::generic::*;

pub fn expand_deriving_to_str(cx: @ext_ctxt,
                              span: span,
                              mitem: @meta_item,
                              in_items: ~[@item])
    -> ~[@item] {
    let trait_def = TraitDef {
        path: Path::new(~[~"core", ~"to_str", ~"ToStr"]),
        additional_bounds: ~[],
        generics: LifetimeBounds::empty(),
        methods: ~[
            MethodDef {
                name: ~"to_str",
                generics: LifetimeBounds::empty(),
                explicit_self: borrowed_explicit_self(),
                args: ~[],
                ret_ty: Ptr(~Literal(Path::new_local(~"str")), Owned),
                const_nonmatching: false,
                combine_substructure: to_str_substructure
            }
        ]
    };

    expand_deriving_generic(cx, span, mitem, in_items, &trait_def)
}

fn to_str_substructure(cx: @ext_ctxt, span: span, substr: &Substructure) -> @expr {
    match substr.self_args {
        [self_obj] => {
            let self_addr = build::mk_addr_of(cx, span, self_obj);
            build::mk_call_global(cx, span,
                                  ~[cx.ident_of("core"),
                                    cx.ident_of("sys"),
                                    cx.ident_of("log_str")],
                                  ~[self_addr])
        }
        _ => cx.span_bug(span, "Invalid number of arguments in `deriving(ToStr)`")
    }
}