about summary refs log tree commit diff
path: root/src/libsyntax/ext/pipes/proto.rs
blob: 79072a2f577ff69c0dd988a305b38d5d010973cc (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2012 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;
use codemap::span;
use ext::base::ext_ctxt;
use ext::pipes::ast_builder::{append_types, ext_ctxt_ast_builder, path};

#[deriving(Eq)]
pub enum direction { send, recv }

impl ToStr for direction {
    fn to_str(&self) -> ~str {
        match *self {
          send => ~"Send",
          recv => ~"Recv"
        }
    }
}

pub impl direction {
    fn reverse(&self) -> direction {
        match *self {
          send => recv,
          recv => send
        }
    }
}

pub struct next_state {
    state: ~str,
    tys: ~[@ast::Ty],
}

// name, span, data, current state, next state
pub struct message(~str, span, ~[@ast::Ty], state, Option<next_state>);

pub impl message {
    fn name(&mut self) -> ~str {
        match *self {
          message(ref id, _, _, _, _) => copy *id
        }
    }

    fn span(&mut self) -> span {
        match *self {
          message(_, span, _, _, _) => span
        }
    }

    /// Return the type parameters actually used by this message
    fn get_generics(&self) -> ast::Generics {
        match *self {
          message(_, _, _, this, _) => copy this.generics
        }
    }
}

pub type state = @state_;

pub struct state_ {
    id: uint,
    name: ~str,
    ident: ast::ident,
    span: span,
    dir: direction,
    generics: ast::Generics,
    messages: @mut ~[message],
    proto: protocol
}

pub impl state_ {
    fn add_message(@self, name: ~str, span: span,
                   data: ~[@ast::Ty], next: Option<next_state>) {
        self.messages.push(message(name, span, data, self,
                                   next));
    }

    fn filename(&self) -> ~str {
        self.proto.filename()
    }

    fn data_name(&self) -> ast::ident {
        self.ident
    }

    /// Returns the type that is used for the messages.
    fn to_ty(&self, cx: @ext_ctxt) -> @ast::Ty {
        cx.ty_path_ast_builder
            (path(~[cx.ident_of(self.name)],self.span).add_tys(
                cx.ty_vars(&self.generics.ty_params)))
    }

    /// Iterate over the states that can be reached in one message
    /// from this state.
    fn reachable(&self, f: &fn(state) -> bool) {
        for self.messages.each |m| {
            match *m {
              message(_, _, _, _, Some(next_state { state: ref id, _ })) => {
                let state = self.proto.get_state((*id));
                if !f(state) { break }
              }
              _ => ()
            }
        }
    }
}

pub type protocol = @mut protocol_;

pub fn protocol(name: ~str, span: span) -> protocol {
    @mut protocol_(name, span)
}

pub fn protocol_(name: ~str, span: span) -> protocol_ {
    protocol_ {
        name: name,
        span: span,
        states: @mut ~[],
        bounded: None
    }
}

pub struct protocol_ {
    name: ~str,
    span: span,
    states: @mut ~[state],

    bounded: Option<bool>,
}

pub impl protocol_ {
    /// Get a state.
    fn get_state(&mut self, name: ~str) -> state {
        self.states.find(|i| i.name == name).get()
    }

    fn get_state_by_id(&mut self, id: uint) -> state { self.states[id] }

    fn has_state(&mut self, name: ~str) -> bool {
        self.states.find(|i| i.name == name).is_some()
    }

    fn filename(&mut self) -> ~str {
        ~"proto://" + self.name
    }

    fn num_states(&mut self) -> uint {
        let states = &mut *self.states;
        states.len()
    }

    fn has_ty_params(&mut self) -> bool {
        for self.states.each |s| {
            if s.generics.ty_params.len() > 0 {
                return true;
            }
        }
        false
    }
    fn is_bounded(&mut self) -> bool {
        let bounded = self.bounded.get();
        bounded
    }
}

pub impl protocol_ {
    fn add_state_poly(@mut self,
                      name: ~str,
                      ident: ast::ident,
                      dir: direction,
                      generics: ast::Generics)
                   -> state {
        let messages = @mut ~[];
        let states = &*self.states;

        let state = @state_ {
            id: states.len(),
            name: name,
            ident: ident,
            span: self.span,
            dir: dir,
            generics: generics,
            messages: messages,
            proto: self
        };

        self.states.push(state);
        state
    }
}

pub trait visitor<Tproto, Tstate, Tmessage> {
    fn visit_proto(&self, proto: protocol, st: &[Tstate]) -> Tproto;
    fn visit_state(&self, state: state, m: &[Tmessage]) -> Tstate;
    fn visit_message(&self, name: ~str, spane: span, tys: &[@ast::Ty],
                     this: state, next: Option<next_state>) -> Tmessage;
}

pub fn visit<Tproto, Tstate, Tmessage, V: visitor<Tproto, Tstate, Tmessage>>(
    proto: protocol, visitor: V) -> Tproto {

    // the copy keywords prevent recursive use of dvec
    let states = do (copy proto.states).map_to_vec |&s| {
        let messages = do (copy s.messages).map_to_vec |&m| {
            let message(name, span, tys, this, next) = m;
            visitor.visit_message(name, span, tys, this, next)
        };
        visitor.visit_state(s, messages)
    };
    visitor.visit_proto(proto, states)
}