]> git.lizzy.rs Git - rust.git/blob - src/types.rs
Merge pull request #128 from marcusklaas/subexpr
[rust.git] / src / types.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use visitor::FmtVisitor;
12
13 use syntax::ast;
14 use syntax::parse::token;
15 use syntax::print::pprust;
16
17 impl<'a> FmtVisitor<'a> {
18     pub fn rewrite_pred(&self, predicate: &ast::WherePredicate) -> String {
19         // TODO dead spans
20         // TODO assumes we'll always fit on one line...
21         match predicate {
22             &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
23                                                                           ref bounded_ty,
24                                                                           ref bounds,
25                                                                           ..}) => {
26                 if bound_lifetimes.len() > 0 {
27                     format!("for<{}> {}: {}",
28                             bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
29                             pprust::ty_to_string(bounded_ty),
30                             bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "))
31
32                 } else {
33                     format!("{}: {}",
34                             pprust::ty_to_string(bounded_ty),
35                             bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "))
36                 }
37             }
38             &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
39                                                                             ref bounds,
40                                                                             ..}) => {
41                 format!("{}: {}",
42                         pprust::lifetime_to_string(lifetime),
43                         bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect(" + "))
44             }
45             &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
46                 format!("{} = {}", pprust::path_to_string(path), pprust::ty_to_string(ty))
47             }
48         }
49     }
50
51     pub fn rewrite_lifetime_def(&self, lifetime: &ast::LifetimeDef) -> String {
52         if lifetime.bounds.len() == 0 {
53             return pprust::lifetime_to_string(&lifetime.lifetime);
54         }
55
56         format!("{}: {}",
57                 pprust::lifetime_to_string(&lifetime.lifetime),
58                 lifetime.bounds.iter().map(|l| pprust::lifetime_to_string(l)).collect::<Vec<_>>().connect(" + "))
59     }
60
61     pub fn rewrite_ty_bound(&self, bound: &ast::TyParamBound) -> String {
62         match *bound {
63             ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
64                 self.rewrite_poly_trait_ref(tref)
65             }
66             ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
67                 format!("?{}", self.rewrite_poly_trait_ref(tref))
68             }
69             ast::TyParamBound::RegionTyParamBound(ref l) => {
70                 pprust::lifetime_to_string(l)
71             }
72         }
73     }
74
75     pub fn rewrite_ty_param(&self, ty_param: &ast::TyParam) -> String {
76         let mut result = String::with_capacity(128);
77         result.push_str(&token::get_ident(ty_param.ident));
78         if ty_param.bounds.len() > 0 {
79             result.push_str(": ");
80             result.push_str(&ty_param.bounds.iter().map(|b| self.rewrite_ty_bound(b)).collect::<Vec<_>>().connect(" + "));
81         }
82         if let Some(ref def) = ty_param.default {
83             result.push_str(" = ");
84             result.push_str(&pprust::ty_to_string(&def));
85         }
86
87         result
88     }
89
90     fn rewrite_poly_trait_ref(&self, t: &ast::PolyTraitRef) -> String {
91         if t.bound_lifetimes.len() > 0 {
92             format!("for<{}> {}",
93                     t.bound_lifetimes.iter().map(|l| self.rewrite_lifetime_def(l)).collect::<Vec<_>>().connect(", "),
94                     pprust::path_to_string(&t.trait_ref.path))
95
96         } else {
97             pprust::path_to_string(&t.trait_ref.path)
98         }
99     }
100 }