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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2016 Kyle Mayes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Finding C declarations.

use std::vec;
use std::collections::{HashSet};
use std::str::{FromStr};

use super::{Entity, EntityKind, Type, TypeKind};

//================================================
// Enums
//================================================

// DefinitionValue _______________________________

/// The value of a C preprocessor definition.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum DefinitionValue {
    /// An integer.
    Integer(bool, u64),
    /// A floating point number.
    Real(f64),
}

impl DefinitionValue {
    //- Constructors -----------------------------

    fn from_entity(entity: Entity) -> Option<DefinitionValue> {
        let mut tokens = entity.get_range().unwrap().tokenize();
        if tokens.last().map_or(false, |t| t.get_spelling() == "#") {
            tokens.pop();
        }

        let (negated, number) = if tokens.len() == 2 {
            (false, tokens[1].get_spelling())
        } else if tokens.len() == 3 && tokens[1].get_spelling() == "-" {
            (true, tokens[2].get_spelling())
        } else {
            return None;
        };

        if let Ok(integer) = u64::from_str(&number) {
            Some(DefinitionValue::Integer(negated, integer))
        } else if let Ok(real) = f64::from_str(&number) {
            if negated {
                Some(DefinitionValue::Real(-real))
            } else {
                Some(DefinitionValue::Real(real))
            }
        } else {
            None
        }
    }
}

//================================================
// Structs
//================================================

// Declaration ___________________________________

/// A C declaration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Declaration<'tu> {
    /// The name of the declaration.
    pub name: String,
    /// The entity that describes the declaration (e.g., contains the fields of a struct).
    pub entity: Entity<'tu>,
    /// The entity the declaration originated from if it differs from `entity`.
    pub source: Option<Entity<'tu>>,
}

impl<'tu> Declaration<'tu> {
    //- Constructors -----------------------------

    fn new(name: String, entity: Entity<'tu>, source: Option<Entity<'tu>>) -> Declaration<'tu> {
        Declaration { name, entity, source }
    }
}

// Definition ____________________________________

/// A C preprocessor definition.
#[derive(Clone, Debug, PartialEq)]
pub struct Definition<'tu> {
    /// The name of the definition.
    pub name: String,
    /// The value of the definition.
    pub value: DefinitionValue,
    /// The entity that describes the definition.
    pub entity: Entity<'tu>,
}

impl<'tu> Definition<'tu> {
    //- Constructors -----------------------------

    fn new(name: String, value: DefinitionValue, entity: Entity<'tu>) -> Definition<'tu> {
        Definition { name, value, entity }
    }
}

// Definitions ___________________________________

/// An iterator over preprocessor definition declarations.
#[allow(missing_debug_implementations)]
pub struct Definitions<'tu> {
    entities: vec::IntoIter<Entity<'tu>>,
    seen: HashSet<String>,
}

impl<'tu> Definitions<'tu> {
    //- Constructors -----------------------------

    fn new(entities: vec::IntoIter<Entity<'tu>>) -> Definitions<'tu> {
        Definitions { entities, seen: HashSet::new() }
    }
}

impl<'tu> Iterator for Definitions<'tu> {
    type Item = Definition<'tu>;

    fn next(&mut self) -> Option<Definition<'tu>> {
        for entity in &mut self.entities {
            if entity.get_kind() == EntityKind::MacroDefinition {
                let name = entity.get_name().unwrap();
                if !self.seen.contains(&name) {
                    if let Some(value) = DefinitionValue::from_entity(entity) {
                        self.seen.insert(name.clone());
                        return Some(Definition::new(name, value, entity));
                    }
                }
            }
        }
        None
    }
}

// Enums _________________________________________

/// An iterator over enum declarations.
#[allow(missing_debug_implementations)]
pub struct Enums<'tu> {
    entities: vec::IntoIter<Entity<'tu>>,
    seen: HashSet<String>,
}

impl<'tu> Enums<'tu> {
    //- Constructors -----------------------------

    fn new(entities: vec::IntoIter<Entity<'tu>>) -> Enums<'tu> {
        Enums { entities, seen: HashSet::new() }
    }
}

impl<'tu> Iterator for Enums<'tu> {
    type Item = Declaration<'tu>;

    fn next(&mut self) -> Option<Declaration<'tu>> {
        next(&mut self.entities, &mut self.seen, EntityKind::EnumDecl, "enum ")
    }
}

// Functions _____________________________________

/// An iterator over function declarations.
#[allow(missing_debug_implementations)]
pub struct Functions<'tu> {
    entities: vec::IntoIter<Entity<'tu>>,
    seen: HashSet<String>,
}

impl<'tu> Functions<'tu> {
    //- Constructors -----------------------------

    fn new(entities: vec::IntoIter<Entity<'tu>>) -> Functions<'tu> {
        Functions { entities, seen: HashSet::new() }
    }
}

impl<'tu> Iterator for Functions<'tu> {
    type Item = Declaration<'tu>;

    fn next(&mut self) -> Option<Declaration<'tu>> {
        for entity in &mut self.entities {
            if entity.get_kind() == EntityKind::FunctionDecl {
                let name = entity.get_name().unwrap();
                if !self.seen.contains(&name) {
                    self.seen.insert(name.clone());
                    return Some(Declaration::new(name, entity, None));
                }
            }
        }
        None
    }
}

// Structs _______________________________________

/// An iterator over struct declarations.
#[allow(missing_debug_implementations)]
pub struct Structs<'tu> {
    entities: vec::IntoIter<Entity<'tu>>,
    seen: HashSet<String>,
}

impl<'tu> Structs<'tu> {
    //- Constructors -----------------------------

    fn new(entities: vec::IntoIter<Entity<'tu>>) -> Structs<'tu> {
        Structs { entities, seen: HashSet::new() }
    }
}

impl<'tu> Iterator for Structs<'tu> {
    type Item = Declaration<'tu>;

    fn next(&mut self) -> Option<Declaration<'tu>> {
        next(&mut self.entities, &mut self.seen, EntityKind::StructDecl, "struct ")
    }
}

// Typedefs ______________________________________

/// An iterator over typedef declarations.
#[allow(missing_debug_implementations)]
pub struct Typedefs<'tu> {
    entities: vec::IntoIter<Entity<'tu>>,
    seen: HashSet<String>,
}

impl<'tu> Typedefs<'tu> {
    //- Constructors -----------------------------

    fn new(entities: vec::IntoIter<Entity<'tu>>) -> Typedefs<'tu> {
        Typedefs { entities, seen: HashSet::new() }
    }
}

impl<'tu> Iterator for Typedefs<'tu> {
    type Item = Declaration<'tu>;

    fn next(&mut self) -> Option<Declaration<'tu>> {
        for entity in &mut self.entities {
            if entity.get_kind() == EntityKind::TypedefDecl {
                let name = entity.get_name().unwrap();
                if !self.seen.contains(&name) {
                    let underlying = entity.get_typedef_underlying_type().unwrap();
                    let display = entity.get_type().unwrap().get_display_name();

                    let typedef = !is_elaborated(underlying) ||
                        underlying.get_result_type().is_some() ||
                        is_alias(underlying, &display);

                    if typedef {
                        self.seen.insert(name.clone());
                        return Some(Declaration::new(name, entity, None));
                    }
                }
            }
        }
        None
    }
}

// Unions ________________________________________

/// An iterator over struct declarations.
#[allow(missing_debug_implementations)]
pub struct Unions<'tu> {
    entities: vec::IntoIter<Entity<'tu>>,
    seen: HashSet<String>,
}

impl<'tu> Unions<'tu> {
    //- Constructors -----------------------------

    fn new(entities: vec::IntoIter<Entity<'tu>>) -> Unions<'tu> {
        Unions { entities, seen: HashSet::new() }
    }
}

impl<'tu> Iterator for Unions<'tu> {
    type Item = Declaration<'tu>;

    fn next(&mut self) -> Option<Declaration<'tu>> {
        next(&mut self.entities, &mut self.seen, EntityKind::UnionDecl, "union ")
    }
}

//================================================
// Functions
//================================================

fn is(type_: Type, prefix: &str) -> bool {
    is_elaborated(type_) && type_.get_display_name().starts_with(prefix)
}

fn is_alias(type_: Type, name: &str) -> bool {
    for prefix in &["enum ", "struct ", "union "] {
        let display = type_.get_display_name();

        if display.starts_with(prefix) && &display[prefix.len()..] != name {
            return true;
        }
    }

    false
}

fn is_elaborated(type_: Type) -> bool {
    type_.is_elaborated().unwrap_or(type_.get_kind() == TypeKind::Unexposed)
}

fn next<'tu>(
    entities: &mut vec::IntoIter<Entity<'tu>>,
    seen: &mut HashSet<String>,
    kind: EntityKind,
    prefix: &str,
) -> Option<Declaration<'tu>> {
    for entity in entities {
        if entity.get_kind() == kind {
            if let Some(name) = entity.get_name() {
                if !seen.contains(&name) {
                    seen.insert(name);
                    if entity.get_child(0).is_some() {
                        return Some(Declaration::new(entity.get_name().unwrap(), entity, None));
                    }
                }
            }
        } else if entity.get_kind() == EntityKind::TypedefDecl {
            let underlying = entity.get_typedef_underlying_type().unwrap();
            let name = entity.get_name().unwrap();

            if is(underlying, prefix) && !seen.contains(&name) {
                let declaration = underlying.get_declaration().unwrap();

                let complete = declaration.get_type().map_or(false, |t| t.get_sizeof().is_ok());
                let anonymous = declaration.get_display_name().is_none();
                let same = entity.get_display_name() == declaration.get_display_name();

                seen.insert(name);
                if complete && (anonymous || same) {
                    let name = entity.get_name().unwrap();
                    return Some(Declaration::new(name, declaration, Some(entity)));
                }
            }
        }
    }
    None
}

/// Returns an iterator over the simple preprocessor definitions in the supplied entities.
///
/// Simple preprocessor definitions are those that consist only of a single integer or floating
/// point literal, optionally negated.
///
/// If a preprocessor definition is encountered multiple times, only the first instance is included.
pub fn find_definitions<'tu, E: Into<Vec<Entity<'tu>>>>(entities: E) -> Definitions<'tu> {
    Definitions::new(entities.into().into_iter())
}

/// Returns an iterator over the enums in the supplied entities.
///
/// If an enum is encountered multiple times, only the first instance is included.
pub fn find_enums<'tu, E: Into<Vec<Entity<'tu>>>>(entities: E) -> Enums<'tu> {
    Enums::new(entities.into().into_iter())
}

/// Returns an iterator over the functions in the supplied entities.
///
/// If a function is encountered multiple times, only the first instance is included.
pub fn find_functions<'tu, E: Into<Vec<Entity<'tu>>>>(entities: E) -> Functions<'tu> {
    Functions::new(entities.into().into_iter())
}

/// Returns an iterator over the structs in the supplied entities.
///
/// If a struct is encountered multiple times, only the first instance is included.
pub fn find_structs<'tu, E: Into<Vec<Entity<'tu>>>>(entities: E) -> Structs<'tu> {
    Structs::new(entities.into().into_iter())
}

/// Returns an iterator over the typedefs in the supplied entities.
///
/// If a typedef is encountered multiple times, only the first instance is included.
pub fn find_typedefs<'tu, E: Into<Vec<Entity<'tu>>>>(entities: E) -> Typedefs<'tu> {
    Typedefs::new(entities.into().into_iter())
}

/// Returns an iterator over the unions in the supplied entities.
///
/// If a union is encountered multiple times, only the first instance is included.
pub fn find_unions<'tu, E: Into<Vec<Entity<'tu>>>>(entities: E) -> Unions<'tu> {
    Unions::new(entities.into().into_iter())
}