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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use c_ast::*;

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum SomeId {
    Stmt(CStmtId),
    Expr(CExprId),
    Decl(CDeclId),
    Type(CTypeId),
}

macro_rules! from_some_id {
    ( $field_type:ty, $con_name:ident, $proj_name:ident ) => {
        impl From<$field_type> for SomeId {
            fn from(a: $field_type) -> Self {
                SomeId::$con_name(a)
            }
        }
        impl SomeId {
            pub fn $proj_name(self) -> Option<$field_type> {
                match self {
                    SomeId::$con_name(x) => Some(x),
                    _ => None,
                }
            }
        }
    };
}

from_some_id!(CExprId, Expr, expr);
from_some_id!(CStmtId, Stmt, stmt);
from_some_id!(CDeclId, Decl, decl);
from_some_id!(CTypeId, Type, type_);

/// Like the vec macro except that it calls the into method on all list elements
macro_rules! intos {
    ( $( $x:expr ),* ) => { vec![ $( $x.into(), )* ] };
}

fn immediate_expr_children(kind: &CExprKind) -> Vec<SomeId> {
    use c_ast::CExprKind::*;
    match *kind {
        BadExpr => vec![],
        DesignatedInitExpr(..) => vec![], // the relevant information will be found in the semantic initializer
        ShuffleVector(..) | ConvertVector(..) => vec![],
        OffsetOf(..) | Literal(..) | ImplicitValueInit(..) => vec![],
        DeclRef(..) => vec![], // don't follow references back!
        Unary(_ty, _op, subexpr, _) => intos![subexpr],
        UnaryType(_ty, _op, opt_expr_id, _) => opt_expr_id.iter().map(|&x| x.into()).collect(),
        Binary(_ty, _op, lhs, rhs, _, _) => intos![lhs, rhs],
        Call(_, f, ref args) => {
            let mut res = intos![f];
            for &a in args {
                res.push(a.into())
            }
            res
        }
        ArraySubscript(_, l, r, _) => intos![l, r],
        Conditional(_, c, t, e) => intos![c, t, e],
        BinaryConditional(_, c, t) => intos![c, t],
        InitList(_, ref xs, _, _) => xs.iter().map(|&x| x.into()).collect(),
        ImplicitCast(_, e, _, _, _)
        | ExplicitCast(_, e, _, _, _)
        | Member(_, e, _, _, _)
        | CompoundLiteral(_, e)
        | Predefined(_, e)
        | VAArg(_, e) => intos![e],
        Statements(_, s) => vec![s.into()],
    }
}

fn immediate_expr_children_all_types(kind: &CExprKind) -> Vec<SomeId> {
    use c_ast::CExprKind::*;
    match *kind {
        BadExpr => vec![],
        DesignatedInitExpr(..) => vec![], // the relevant information will be found in the semantic initializer
        ShuffleVector(_, ref kids) | ConvertVector(_, ref kids) => {
            kids.iter().map(|&x| x.into()).collect()
        }
        OffsetOf(..) | Literal(..) | ImplicitValueInit(..) => vec![],
        DeclRef(..) => vec![], // don't follow references back!
        Unary(_ty, _op, subexpr, _) => intos![subexpr],
        UnaryType(_ty, _op, opt_expr_id, qty) => {
            let mut res = intos![qty.ctype];
            if let Some(expr_id) = opt_expr_id {
                res.push(expr_id.into());
            }
            res
        }
        Binary(_ty, _op, lhs, rhs, _, _) => intos![lhs, rhs],
        Call(_, f, ref args) => {
            let mut res = intos![f];
            for &a in args {
                res.push(a.into())
            }
            res
        }
        ArraySubscript(_, l, r, _) => intos![l, r],
        Conditional(_, c, t, e) => intos![c, t, e],
        BinaryConditional(_, c, t) => intos![c, t],
        InitList(_, ref xs, _, _) => xs.iter().map(|&x| x.into()).collect(),
        ImplicitCast(_, e, _, _, _) | Member(_, e, _, _, _) | Predefined(_, e) => intos![e],
        // Normally we don't step into the result type annotation field, because it's not really
        // part of the expression.  But for `ExplicitCast`, the result type is actually the cast's
        // target type as written by the user.  The other expr kinds here work similarly.
        ExplicitCast(qty, e, _, _, _) | CompoundLiteral(qty, e) | VAArg(qty, e) => {
            intos![qty.ctype, e]
        }
        Statements(_, s) => vec![s.into()],
    }
}

fn immediate_decl_children(kind: &CDeclKind) -> Vec<SomeId> {
    use c_ast::CDeclKind::*;
    match *kind {
        Function {
            typ,
            ref parameters,
            body,
            ..
        } => {
            let mut res = intos![typ];
            res.extend(parameters.iter().map(|&x| -> SomeId { x.into() }));
            res.extend(body.iter().map(|&x| -> SomeId { x.into() }));
            res
        }
        Variable {
            typ, initializer, ..
        } => {
            let mut res = intos![typ.ctype];
            for x in initializer {
                res.push(x.into())
            }
            res
        }
        Enum {
            ref variants,
            integral_type,
            ..
        } => {
            let mut res: Vec<SomeId> = variants.iter().map(|&x| x.into()).collect();
            if let Some(qty) = integral_type {
                res.push(qty.ctype.into());
            }
            res
        }
        EnumConstant { .. } => vec![],
        Typedef { typ, .. } => intos![typ.ctype],
        Struct { ref fields, .. } => fields.iter().flat_map(|x| x).map(|&x| x.into()).collect(),
        Union { ref fields, .. } => fields.iter().flat_map(|x| x).map(|&x| x.into()).collect(),
        Field { typ, .. } => intos![typ.ctype],
    }
}

fn immediate_stmt_children(kind: &CStmtKind) -> Vec<SomeId> {
    use c_ast::CStmtKind::*;
    match *kind {
        Expr(e) => intos![e],
        Label(s) => intos![s],
        Case(e, s, _) => intos![e, s],
        Default(s) => intos![s],

        // Compound statements (6.8.2)
        Compound(ref xs) => xs.iter().map(|&x| x.into()).collect(),
        Empty => vec![],
        If {
            scrutinee: e,
            true_variant: s,
            false_variant: opt_s,
        } => {
            let mut res = intos![e, s];
            for &x in &opt_s {
                res.push(x.into())
            }
            res
        }
        Switch {
            scrutinee: e,
            body: s,
        } => intos![e, s],

        While {
            condition: e,
            body: s,
        } => intos![e, s],
        DoWhile {
            body: s,
            condition: e,
        } => intos![s, e],
        ForLoop {
            init: a,
            condition: b,
            increment: c,
            body: d,
        } => {
            let mut res = vec![];
            for &x in &a {
                res.push(x.into())
            }
            for &x in &b {
                res.push(x.into())
            }
            for &x in &c {
                res.push(x.into())
            }
            res.push(d.into());
            res
        }
        Goto(_) => vec![], // Don't follow the reference to the label
        Break => vec![],
        Continue => vec![],
        Return(ref opt_e) => opt_e.iter().map(|&x| x.into()).collect(),

        Decls(ref decl_ids) => decl_ids.iter().map(|&x| x.into()).collect(),

        Asm {
            ref inputs,
            ref outputs,
            ..
        } => {
            let mut res = vec![];
            for list in vec![inputs, outputs] {
                for elt in list {
                    res.push(elt.expression.into())
                }
            }
            res
        }
    }
}

fn immediate_type_children(kind: &CTypeKind) -> Vec<SomeId> {
    use c_ast::CTypeKind::*;
    match *kind {
        Elaborated(_) => vec![], // These are references to previous definitions
        TypeOfExpr(e) => intos![e],
        Void | Bool | Short | Int | Long | LongLong | UShort | UInt | ULong | ULongLong | SChar
        | UChar | Char | Double | LongDouble | Float | Int128 | UInt128 | BuiltinFn | Half => {
            vec![]
        }

        Pointer(qtype) | Attributed(qtype, _) | BlockPointer(qtype) | Vector(qtype, _) => {
            intos![qtype.ctype]
        }

        Decayed(ctype)
        | Paren(ctype)
        | TypeOf(ctype)
        | Complex(ctype)
        | ConstantArray(ctype, _)
        | IncompleteArray(ctype) => intos![ctype],

        Struct(decl_id) | Union(decl_id) | Enum(decl_id) | Typedef(decl_id) => intos![decl_id],

        VariableArray(elt, cnt) => {
            let mut res = intos![elt];
            for x in cnt {
                res.push(x.into())
            }
            res
        }
        Function(ret, ref params, _, _, _) => {
            let mut res = intos![ret.ctype];
            for p in params {
                res.push(p.ctype.into())
            }
            res
        }
    }
}

fn immediate_children(context: &TypedAstContext, s_or_e: SomeId) -> Vec<SomeId> {
    match s_or_e {
        SomeId::Stmt(stmt_id) => immediate_stmt_children(&context[stmt_id].kind),
        SomeId::Expr(expr_id) => immediate_expr_children(&context[expr_id].kind),
        SomeId::Decl(decl_id) => immediate_decl_children(&context[decl_id].kind),
        SomeId::Type(type_id) => immediate_type_children(&context[type_id].kind),
    }
}

fn immediate_children_all_types(context: &TypedAstContext, s_or_e: SomeId) -> Vec<SomeId> {
    match s_or_e {
        SomeId::Stmt(stmt_id) => immediate_stmt_children(&context[stmt_id].kind),
        SomeId::Expr(expr_id) => immediate_expr_children_all_types(&context[expr_id].kind),
        SomeId::Decl(decl_id) => immediate_decl_children(&context[decl_id].kind),
        SomeId::Type(type_id) => immediate_type_children(&context[type_id].kind),
    }
}

pub struct DFExpr<'context> {
    context: &'context TypedAstContext,
    stack: Vec<SomeId>,
}

impl<'context> DFExpr<'context> {
    pub fn new(context: &'context TypedAstContext, start: SomeId) -> Self {
        DFExpr {
            context,
            stack: vec![start],
        }
    }
    pub fn prune(&mut self, n: usize) {
        let new_len = self.stack.len() - n;
        self.stack.truncate(new_len)
    }
}

impl<'context> Iterator for DFExpr<'context> {
    type Item = SomeId;
    fn next(&mut self) -> Option<Self::Item> {
        let result = self.stack.pop();

        if let Some(i) = result {
            // Compute list of immediate children
            let children = immediate_children(self.context, i);
            // Add children in reverse order since we visit the end of the stack first
            self.stack.extend(children.into_iter().rev())
        }

        result
    }
}

/// Depth-first traversal of all AST nodes.  After visiting each node, iteration proceeds to nodes
/// that are "contained in" that node.  For example, after visiting a `CExprKind::Binary`, it will
/// visit the LHS and RHS expression nodes, but it will not visit the LHS, RHS, and result type
/// nodes that are also referenced from the `Binary` expression.
pub struct DFNodes<'context> {
    context: &'context TypedAstContext,
    stack: Vec<SomeId>,
}

impl<'context> DFNodes<'context> {
    pub fn new(context: &'context TypedAstContext, start: SomeId) -> Self {
        DFNodes {
            context,
            stack: vec![start],
        }
    }
    pub fn prune(&mut self, n: usize) {
        let new_len = self.stack.len() - n;
        self.stack.truncate(new_len)
    }
}

impl<'context> Iterator for DFNodes<'context> {
    type Item = SomeId;
    fn next(&mut self) -> Option<Self::Item> {
        let result = self.stack.pop();

        if let Some(i) = result {
            // Compute list of immediate children
            let children = immediate_children_all_types(self.context, i);
            // Add children in reverse order since we visit the end of the stack first
            self.stack.extend(children.into_iter().rev())
        }

        result
    }
}